esp_app_desc.c 3.7 KB

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