esp_app_desc.c 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * SPDX-FileCopyrightText: 2022-2023 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 <string.h>
  9. #include "esp_app_desc.h"
  10. #include "sdkconfig.h"
  11. // Application version info
  12. const __attribute__((weak)) __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_app_get_description(void)
  46. {
  47. return &esp_app_desc;
  48. }
  49. char app_elf_sha256_str[CONFIG_APP_RETRIEVE_LEN_ELF_SHA + 1] = { 0 };
  50. /* The esp_app_desc.app_elf_sha256 should be possible to print in panic handler or core dump during cache is disabled.
  51. * But because the cache is disabled the reading esp_app_desc.app_elf_sha256 is not right and
  52. * can lead to a complete lock-up of the CPU.
  53. * For this reason we do a reading of esp_app_desc.app_elf_sha256 and convert to string while start up in esp_system_init_app_elf_sha256()
  54. * and keep it in the static app_elf_sha256_str variable.
  55. */
  56. __attribute__((constructor)) void esp_app_format_init_elf_sha256(void)
  57. {
  58. if (*((int *)&app_elf_sha256_str) != 0) {
  59. // app_elf_sha256_str is already set
  60. return;
  61. }
  62. // At -O2 optimization level, GCC optimizes out the copying of the first byte of the app_elf_sha256,
  63. // because it is zero at compile time, and only modified afterwards by esptool.
  64. // Casting to volatile disables the optimization.
  65. const volatile char* src = (const volatile char*)esp_app_desc.app_elf_sha256;
  66. for (size_t i = 0; i < sizeof(app_elf_sha256_str) / 2; ++i) {
  67. char c = src[i];
  68. for (size_t s = 0; s < 2; ++s) {
  69. char val = (c >> 4) & 0xF;
  70. app_elf_sha256_str[2 * i + s] = (val < 10) ? ('0' + val) : ('a' + val - 10);
  71. c <<= 4;
  72. }
  73. }
  74. app_elf_sha256_str[sizeof(app_elf_sha256_str) - 1] = 0;
  75. }
  76. int esp_app_get_elf_sha256(char* dst, size_t size)
  77. {
  78. if (dst == NULL || size < 2) {
  79. return 0;
  80. }
  81. esp_app_format_init_elf_sha256();
  82. size_t n = MIN(size, sizeof(app_elf_sha256_str));
  83. memcpy(dst, app_elf_sha256_str, n);
  84. dst[n - 1] = 0;
  85. return n;
  86. }