esp_app_desc.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * SPDX-FileCopyrightText: 2017-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <assert.h>
  7. #include <sys/param.h>
  8. #include "esp_ota_ops.h"
  9. #include "esp_attr.h"
  10. #include "sdkconfig.h"
  11. // Application version info
  12. const __attribute__((section(".rodata_desc"))) esp_app_desc_t esp_app_desc = {
  13. .magic_word = ESP_APP_DESC_MAGIC_WORD,
  14. #ifdef CONFIG_APP_EXCLUDE_PROJECT_VER_VAR
  15. .version = "",
  16. #else
  17. .version = PROJECT_VER,
  18. #endif
  19. #ifdef CONFIG_APP_EXCLUDE_PROJECT_NAME_VAR
  20. .project_name = "",
  21. #else
  22. .project_name = PROJECT_NAME,
  23. #endif
  24. .idf_ver = IDF_VER,
  25. #ifdef CONFIG_BOOTLOADER_APP_SECURE_VERSION
  26. .secure_version = CONFIG_BOOTLOADER_APP_SECURE_VERSION,
  27. #else
  28. .secure_version = 0,
  29. #endif
  30. #ifdef CONFIG_APP_COMPILE_TIME_DATE
  31. .time = __TIME__,
  32. .date = __DATE__,
  33. #else
  34. .time = "",
  35. .date = "",
  36. #endif
  37. };
  38. #ifndef CONFIG_APP_EXCLUDE_PROJECT_VER_VAR
  39. _Static_assert(sizeof(PROJECT_VER) <= sizeof(esp_app_desc.version), "PROJECT_VER is longer than version field in structure");
  40. #endif
  41. _Static_assert(sizeof(IDF_VER) <= sizeof(esp_app_desc.idf_ver), "IDF_VER is longer than idf_ver field in structure");
  42. #ifndef CONFIG_APP_EXCLUDE_PROJECT_NAME_VAR
  43. _Static_assert(sizeof(PROJECT_NAME) <= sizeof(esp_app_desc.project_name), "PROJECT_NAME is longer than project_name field in structure");
  44. #endif
  45. const esp_app_desc_t *esp_ota_get_app_description(void)
  46. {
  47. return &esp_app_desc;
  48. }
  49. /* The following two functions may be called from the panic handler
  50. * or core dump, hence IRAM_ATTR.
  51. */
  52. static inline char IRAM_ATTR to_hex_digit(unsigned val)
  53. {
  54. return (val < 10) ? ('0' + val) : ('a' + val - 10);
  55. }
  56. __attribute__((constructor)) void esp_ota_init_app_elf_sha256(void)
  57. {
  58. esp_ota_get_app_elf_sha256(NULL, 0);
  59. }
  60. /* The esp_app_desc.app_elf_sha256 should be possible to print in panic handler during cache is disabled.
  61. * But because the cache is disabled the reading esp_app_desc.app_elf_sha256 is not right and
  62. * can lead to a complete lock-up of the CPU.
  63. * For this reason we do a reading of esp_app_desc.app_elf_sha256 while start up in esp_ota_init_app_elf_sha256()
  64. * and keep it in the static s_app_elf_sha256 value.
  65. */
  66. int IRAM_ATTR esp_ota_get_app_elf_sha256(char* dst, size_t size)
  67. {
  68. static char s_app_elf_sha256[CONFIG_APP_RETRIEVE_LEN_ELF_SHA / 2];
  69. static bool first_call = true;
  70. if (first_call) {
  71. first_call = false;
  72. // At -O2 optimization level, GCC optimizes out the copying of the first byte of the app_elf_sha256,
  73. // because it is zero at compile time, and only modified afterwards by esptool.
  74. // Casting to volatile disables the optimization.
  75. const volatile uint8_t* src = (const volatile uint8_t*)esp_app_desc.app_elf_sha256;
  76. for (size_t i = 0; i < sizeof(s_app_elf_sha256); ++i) {
  77. s_app_elf_sha256[i] = src[i];
  78. }
  79. }
  80. if (dst == NULL || size == 0) {
  81. return 0;
  82. }
  83. size_t n = MIN((size - 1) / 2, sizeof(s_app_elf_sha256));
  84. for (size_t i = 0; i < n; ++i) {
  85. dst[2*i] = to_hex_digit(s_app_elf_sha256[i] >> 4);
  86. dst[2*i + 1] = to_hex_digit(s_app_elf_sha256[i] & 0xf);
  87. }
  88. dst[2*n] = 0;
  89. return 2*n + 1;
  90. }