envcrc.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2001
  4. * Paolo Scaffardi, AIRVENT SAM s.p.a - RIMINI(ITALY), arsenio@tin.it
  5. */
  6. #include <errno.h>
  7. #include <stdio.h>
  8. #include <stdint.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <unistd.h>
  12. #include <linux/kconfig.h>
  13. #ifndef __ASSEMBLY__
  14. #define __ASSEMBLY__ /* Dirty trick to get only #defines */
  15. #endif
  16. #define __ASM_STUB_PROCESSOR_H__ /* don't include asm/processor. */
  17. #include <config.h>
  18. #undef __ASSEMBLY__
  19. #if defined(CONFIG_ENV_IS_IN_FLASH)
  20. # ifndef CONFIG_ENV_ADDR
  21. # define CONFIG_ENV_ADDR (CONFIG_SYS_FLASH_BASE + CONFIG_ENV_OFFSET)
  22. # endif
  23. # ifndef CONFIG_ENV_OFFSET
  24. # define CONFIG_ENV_OFFSET (CONFIG_ENV_ADDR - CONFIG_SYS_FLASH_BASE)
  25. # endif
  26. # if !defined(CONFIG_ENV_ADDR_REDUND) && defined(CONFIG_ENV_OFFSET_REDUND)
  27. # define CONFIG_ENV_ADDR_REDUND (CONFIG_SYS_FLASH_BASE + CONFIG_ENV_OFFSET_REDUND)
  28. # endif
  29. # ifndef CONFIG_ENV_SIZE
  30. # define CONFIG_ENV_SIZE CONFIG_ENV_SECT_SIZE
  31. # endif
  32. # if (CONFIG_ENV_ADDR >= CONFIG_SYS_MONITOR_BASE) && \
  33. ((CONFIG_ENV_ADDR + CONFIG_ENV_SIZE) <= (CONFIG_SYS_MONITOR_BASE + CONFIG_SYS_MONITOR_LEN))
  34. # define ENV_IS_EMBEDDED
  35. # endif
  36. #endif /* CONFIG_ENV_IS_IN_FLASH */
  37. #if defined(ENV_IS_EMBEDDED) && !defined(CONFIG_BUILD_ENVCRC)
  38. # define CONFIG_BUILD_ENVCRC
  39. #endif
  40. #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
  41. # define ENV_HEADER_SIZE (sizeof(uint32_t) + 1)
  42. #else
  43. # define ENV_HEADER_SIZE (sizeof(uint32_t))
  44. #endif
  45. #define ENV_SIZE (CONFIG_ENV_SIZE - ENV_HEADER_SIZE)
  46. #ifdef CONFIG_BUILD_ENVCRC
  47. # include <env_internal.h>
  48. extern unsigned int env_size;
  49. extern env_t embedded_environment;
  50. #endif /* CONFIG_BUILD_ENVCRC */
  51. extern uint32_t crc32(uint32_t, const unsigned char *, unsigned int);
  52. int main (int argc, char **argv)
  53. {
  54. #ifdef CONFIG_BUILD_ENVCRC
  55. unsigned char pad = 0x00;
  56. uint32_t crc;
  57. unsigned char *envptr = (unsigned char *)&embedded_environment,
  58. *dataptr = envptr + ENV_HEADER_SIZE;
  59. unsigned int datasize = ENV_SIZE;
  60. unsigned int eoe;
  61. if (argv[1] && !strncmp(argv[1], "--binary", 8)) {
  62. int ipad = 0xff;
  63. if (argv[1][8] == '=')
  64. sscanf(argv[1] + 9, "%i", &ipad);
  65. pad = ipad;
  66. }
  67. if (pad) {
  68. /* find the end of env */
  69. for (eoe = 0; eoe < datasize - 1; ++eoe)
  70. if (!dataptr[eoe] && !dataptr[eoe+1]) {
  71. eoe += 2;
  72. break;
  73. }
  74. if (eoe < datasize - 1)
  75. memset(dataptr + eoe, pad, datasize - eoe);
  76. }
  77. crc = crc32(0, dataptr, datasize);
  78. /* Check if verbose mode is activated passing a parameter to the program */
  79. if (argc > 1) {
  80. if (!strncmp(argv[1], "--binary", 8)) {
  81. int le = (argc > 2 ? !strcmp(argv[2], "le") : 1);
  82. size_t i, start, end, step;
  83. if (le) {
  84. start = 0;
  85. end = ENV_HEADER_SIZE;
  86. step = 1;
  87. } else {
  88. start = ENV_HEADER_SIZE - 1;
  89. end = -1;
  90. step = -1;
  91. }
  92. for (i = start; i != end; i += step)
  93. printf("%c", (crc & (0xFF << (i * 8))) >> (i * 8));
  94. if (fwrite(dataptr, 1, datasize, stdout) != datasize)
  95. fprintf(stderr, "fwrite() failed: %s\n", strerror(errno));
  96. } else {
  97. printf("CRC32 from offset %08X to %08X of environment = %08X\n",
  98. (unsigned int) (dataptr - envptr),
  99. (unsigned int) (dataptr - envptr) + datasize,
  100. crc);
  101. }
  102. } else {
  103. printf ("0x%08X\n", crc);
  104. }
  105. #else
  106. printf ("0\n");
  107. #endif
  108. return EXIT_SUCCESS;
  109. }