bootloader_common_loader.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * SPDX-FileCopyrightText: 2020-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "string.h"
  7. #include "sdkconfig.h"
  8. #include "esp_err.h"
  9. #include "esp_log.h"
  10. #include "esp_rom_spiflash.h"
  11. #include "esp_rom_crc.h"
  12. #include "esp_rom_gpio.h"
  13. #include "esp_flash_partitions.h"
  14. #include "bootloader_flash.h"
  15. #include "bootloader_common.h"
  16. #include "soc/gpio_periph.h"
  17. #include "soc/rtc.h"
  18. #include "soc/efuse_reg.h"
  19. #include "soc/chip_revision.h"
  20. #include "hal/efuse_hal.h"
  21. #include "hal/efuse_ll.h"
  22. #include "hal/gpio_ll.h"
  23. #include "esp_image_format.h"
  24. #include "bootloader_sha.h"
  25. #include "sys/param.h"
  26. #include "bootloader_flash_priv.h"
  27. #define ESP_PARTITION_HASH_LEN 32 /* SHA-256 digest length */
  28. #define IS_MAX_REV_SET(max_chip_rev_full) (((max_chip_rev_full) != 65535) && ((max_chip_rev_full) != 0))
  29. static const char* TAG = "boot_comm";
  30. uint32_t bootloader_common_ota_select_crc(const esp_ota_select_entry_t *s)
  31. {
  32. return esp_rom_crc32_le(UINT32_MAX, (uint8_t*)&s->ota_seq, 4);
  33. }
  34. bool bootloader_common_ota_select_invalid(const esp_ota_select_entry_t *s)
  35. {
  36. return s->ota_seq == UINT32_MAX || s->ota_state == ESP_OTA_IMG_INVALID || s->ota_state == ESP_OTA_IMG_ABORTED;
  37. }
  38. bool bootloader_common_ota_select_valid(const esp_ota_select_entry_t *s)
  39. {
  40. return bootloader_common_ota_select_invalid(s) == false && s->crc == bootloader_common_ota_select_crc(s);
  41. }
  42. int bootloader_common_get_active_otadata(esp_ota_select_entry_t *two_otadata)
  43. {
  44. if (two_otadata == NULL) {
  45. return -1;
  46. }
  47. bool valid_two_otadata[2];
  48. valid_two_otadata[0] = bootloader_common_ota_select_valid(&two_otadata[0]);
  49. valid_two_otadata[1] = bootloader_common_ota_select_valid(&two_otadata[1]);
  50. return bootloader_common_select_otadata(two_otadata, valid_two_otadata, true);
  51. }
  52. esp_err_t bootloader_common_check_chip_validity(const esp_image_header_t* img_hdr, esp_image_type type)
  53. {
  54. esp_err_t err = ESP_OK;
  55. esp_chip_id_t chip_id = CONFIG_IDF_FIRMWARE_CHIP_ID;
  56. if (chip_id != img_hdr->chip_id) {
  57. ESP_LOGE(TAG, "mismatch chip ID, expected %d, found %d", chip_id, img_hdr->chip_id);
  58. err = ESP_FAIL;
  59. } else {
  60. #ifndef CONFIG_IDF_ENV_FPGA
  61. unsigned revision = efuse_hal_chip_revision();
  62. unsigned int major_rev = revision / 100;
  63. unsigned int minor_rev = revision % 100;
  64. unsigned min_rev = img_hdr->min_chip_rev_full;
  65. if (type == ESP_IMAGE_BOOTLOADER || type == ESP_IMAGE_APPLICATION) {
  66. if (!ESP_CHIP_REV_ABOVE(revision, min_rev)) {
  67. ESP_LOGE(TAG, "Image requires chip rev >= v%d.%d, but chip is v%d.%d",
  68. min_rev / 100, min_rev % 100,
  69. major_rev, minor_rev);
  70. err = ESP_FAIL;
  71. }
  72. }
  73. if (type == ESP_IMAGE_APPLICATION) {
  74. unsigned max_rev = img_hdr->max_chip_rev_full;
  75. if ((IS_MAX_REV_SET(max_rev) && (revision > max_rev) && !efuse_ll_get_disable_wafer_version_major())) {
  76. ESP_LOGE(TAG, "Image requires chip rev <= v%d.%d, but chip is v%d.%d",
  77. max_rev / 100, max_rev % 100,
  78. major_rev, minor_rev);
  79. err = ESP_FAIL;
  80. }
  81. }
  82. #endif // CONFIG_IDF_ENV_FPGA
  83. }
  84. return err;
  85. }
  86. int bootloader_common_select_otadata(const esp_ota_select_entry_t *two_otadata, bool *valid_two_otadata, bool max)
  87. {
  88. if (two_otadata == NULL || valid_two_otadata == NULL) {
  89. return -1;
  90. }
  91. int active_otadata = -1;
  92. if (valid_two_otadata[0] && valid_two_otadata[1]) {
  93. uint32_t condition = (max == true) ? MAX(two_otadata[0].ota_seq, two_otadata[1].ota_seq) : MIN(two_otadata[0].ota_seq, two_otadata[1].ota_seq);
  94. if (condition == two_otadata[0].ota_seq) {
  95. active_otadata = 0;
  96. } else {
  97. active_otadata = 1;
  98. }
  99. ESP_LOGD(TAG, "Both OTA copies are valid");
  100. } else {
  101. for (int i = 0; i < 2; ++i) {
  102. if (valid_two_otadata[i]) {
  103. active_otadata = i;
  104. ESP_LOGD(TAG, "Only otadata[%d] is valid", i);
  105. break;
  106. }
  107. }
  108. }
  109. return active_otadata;
  110. }
  111. #if defined( CONFIG_BOOTLOADER_SKIP_VALIDATE_IN_DEEP_SLEEP ) || defined( CONFIG_BOOTLOADER_CUSTOM_RESERVE_RTC )
  112. #define RTC_RETAIN_MEM_ADDR (SOC_RTC_DRAM_HIGH - sizeof(rtc_retain_mem_t))
  113. _Static_assert(RTC_RETAIN_MEM_ADDR >= SOC_RTC_DRAM_LOW, "rtc_retain_mem_t structure size is bigger than the RTC memory size. Consider reducing RTC reserved memory size.");
  114. rtc_retain_mem_t *const rtc_retain_mem = (rtc_retain_mem_t *)RTC_RETAIN_MEM_ADDR;
  115. #ifndef BOOTLOADER_BUILD
  116. #include "heap_memory_layout.h"
  117. /* The app needs to be told this memory is reserved, important if configured to use RTC memory as heap.
  118. Note that keeping this macro here only works when other symbols in this file are referenced by the app, as
  119. this feature is otherwise 100% part of the bootloader. However this seems to happen in all apps.
  120. */
  121. SOC_RESERVE_MEMORY_REGION(RTC_RETAIN_MEM_ADDR, RTC_RETAIN_MEM_ADDR + sizeof(rtc_retain_mem_t), rtc_retain_mem);
  122. #endif
  123. static uint32_t rtc_retain_mem_size(void) {
  124. #ifdef CONFIG_BOOTLOADER_CUSTOM_RESERVE_RTC
  125. /* A custom memory has been reserved by the user, do not consider this memory into CRC calculation as it may change without
  126. * the have the user updating the CRC. Return the offset of the custom field, which is equivalent to size of the structure
  127. * minus the size of everything after (including) `custom` */
  128. return offsetof(rtc_retain_mem_t, custom);
  129. #else
  130. return sizeof(rtc_retain_mem_t) - sizeof(rtc_retain_mem->crc);
  131. #endif
  132. }
  133. static bool check_rtc_retain_mem(void)
  134. {
  135. return esp_rom_crc32_le(UINT32_MAX, (uint8_t*)rtc_retain_mem, rtc_retain_mem_size()) == rtc_retain_mem->crc && rtc_retain_mem->crc != UINT32_MAX;
  136. }
  137. static void update_rtc_retain_mem_crc(void)
  138. {
  139. rtc_retain_mem->crc = esp_rom_crc32_le(UINT32_MAX, (uint8_t*)rtc_retain_mem, rtc_retain_mem_size());
  140. }
  141. NOINLINE_ATTR void bootloader_common_reset_rtc_retain_mem(void)
  142. {
  143. #pragma GCC diagnostic push
  144. #pragma GCC diagnostic ignored "-Wstringop-overflow"
  145. #pragma GCC diagnostic ignored "-Warray-bounds"
  146. memset(rtc_retain_mem, 0, sizeof(rtc_retain_mem_t));
  147. #pragma GCC diagnostic pop
  148. }
  149. uint16_t bootloader_common_get_rtc_retain_mem_reboot_counter(void)
  150. {
  151. if (check_rtc_retain_mem()) {
  152. return rtc_retain_mem->reboot_counter;
  153. }
  154. return 0;
  155. }
  156. esp_partition_pos_t* bootloader_common_get_rtc_retain_mem_partition(void)
  157. {
  158. if (check_rtc_retain_mem()) {
  159. return &rtc_retain_mem->partition;
  160. }
  161. return NULL;
  162. }
  163. void bootloader_common_update_rtc_retain_mem(esp_partition_pos_t* partition, bool reboot_counter)
  164. {
  165. if (reboot_counter) {
  166. if (!check_rtc_retain_mem()) {
  167. bootloader_common_reset_rtc_retain_mem();
  168. }
  169. if (++rtc_retain_mem->reboot_counter == 0) {
  170. // do not allow to overflow. Stop it.
  171. --rtc_retain_mem->reboot_counter;
  172. }
  173. }
  174. if (partition != NULL) {
  175. rtc_retain_mem->partition.offset = partition->offset;
  176. rtc_retain_mem->partition.size = partition->size;
  177. }
  178. update_rtc_retain_mem_crc();
  179. }
  180. rtc_retain_mem_t* bootloader_common_get_rtc_retain_mem(void)
  181. {
  182. return rtc_retain_mem;
  183. }
  184. #endif // defined( CONFIG_BOOTLOADER_SKIP_VALIDATE_IN_DEEP_SLEEP ) || defined( CONFIG_BOOTLOADER_CUSTOM_RESERVE_RTC )