envcrc.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 defined(CONFIG_ENV_ADDR_REDUND) && !defined(CONFIG_ENV_SIZE_REDUND)
  33. # define CONFIG_ENV_SIZE_REDUND CONFIG_ENV_SIZE
  34. # endif
  35. # if (CONFIG_ENV_ADDR >= CONFIG_SYS_MONITOR_BASE) && \
  36. ((CONFIG_ENV_ADDR + CONFIG_ENV_SIZE) <= (CONFIG_SYS_MONITOR_BASE + CONFIG_SYS_MONITOR_LEN))
  37. # define ENV_IS_EMBEDDED
  38. # endif
  39. # if defined(CONFIG_ENV_ADDR_REDUND) || defined(CONFIG_ENV_OFFSET_REDUND)
  40. # define CONFIG_SYS_REDUNDAND_ENVIRONMENT
  41. # endif
  42. #endif /* CONFIG_ENV_IS_IN_FLASH */
  43. #if defined(ENV_IS_EMBEDDED) && !defined(CONFIG_BUILD_ENVCRC)
  44. # define CONFIG_BUILD_ENVCRC
  45. #endif
  46. #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
  47. # define ENV_HEADER_SIZE (sizeof(uint32_t) + 1)
  48. #else
  49. # define ENV_HEADER_SIZE (sizeof(uint32_t))
  50. #endif
  51. #define ENV_SIZE (CONFIG_ENV_SIZE - ENV_HEADER_SIZE)
  52. #ifdef CONFIG_BUILD_ENVCRC
  53. # include <environment.h>
  54. extern unsigned int env_size;
  55. extern env_t embedded_environment;
  56. #endif /* CONFIG_BUILD_ENVCRC */
  57. extern uint32_t crc32 (uint32_t, const unsigned char *, unsigned int);
  58. int main (int argc, char **argv)
  59. {
  60. #ifdef CONFIG_BUILD_ENVCRC
  61. unsigned char pad = 0x00;
  62. uint32_t crc;
  63. unsigned char *envptr = (unsigned char *)&embedded_environment,
  64. *dataptr = envptr + ENV_HEADER_SIZE;
  65. unsigned int datasize = ENV_SIZE;
  66. unsigned int eoe;
  67. if (argv[1] && !strncmp(argv[1], "--binary", 8)) {
  68. int ipad = 0xff;
  69. if (argv[1][8] == '=')
  70. sscanf(argv[1] + 9, "%i", &ipad);
  71. pad = ipad;
  72. }
  73. if (pad) {
  74. /* find the end of env */
  75. for (eoe = 0; eoe < datasize - 1; ++eoe)
  76. if (!dataptr[eoe] && !dataptr[eoe+1]) {
  77. eoe += 2;
  78. break;
  79. }
  80. if (eoe < datasize - 1)
  81. memset(dataptr + eoe, pad, datasize - eoe);
  82. }
  83. crc = crc32 (0, dataptr, datasize);
  84. /* Check if verbose mode is activated passing a parameter to the program */
  85. if (argc > 1) {
  86. if (!strncmp(argv[1], "--binary", 8)) {
  87. int le = (argc > 2 ? !strcmp(argv[2], "le") : 1);
  88. size_t i, start, end, step;
  89. if (le) {
  90. start = 0;
  91. end = ENV_HEADER_SIZE;
  92. step = 1;
  93. } else {
  94. start = ENV_HEADER_SIZE - 1;
  95. end = -1;
  96. step = -1;
  97. }
  98. for (i = start; i != end; i += step)
  99. printf("%c", (crc & (0xFF << (i * 8))) >> (i * 8));
  100. if (fwrite(dataptr, 1, datasize, stdout) != datasize)
  101. fprintf(stderr, "fwrite() failed: %s\n", strerror(errno));
  102. } else {
  103. printf("CRC32 from offset %08X to %08X of environment = %08X\n",
  104. (unsigned int) (dataptr - envptr),
  105. (unsigned int) (dataptr - envptr) + datasize,
  106. crc);
  107. }
  108. } else {
  109. printf ("0x%08X\n", crc);
  110. }
  111. #else
  112. printf ("0\n");
  113. #endif
  114. return EXIT_SUCCESS;
  115. }