envcrc.c 3.2 KB

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