bootloader_common_loader.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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/gpio_ll.h"
  22. #include "esp_image_format.h"
  23. #include "bootloader_sha.h"
  24. #include "sys/param.h"
  25. #include "bootloader_flash_priv.h"
  26. #define ESP_PARTITION_HASH_LEN 32 /* SHA-256 digest length */
  27. #define IS_MAX_REV_SET(max_chip_rev_full) (((max_chip_rev_full) != 65535) && ((max_chip_rev_full) != 0))
  28. static const char* TAG = "boot_comm";
  29. uint32_t bootloader_common_ota_select_crc(const esp_ota_select_entry_t *s)
  30. {
  31. return esp_rom_crc32_le(UINT32_MAX, (uint8_t*)&s->ota_seq, 4);
  32. }
  33. bool bootloader_common_ota_select_invalid(const esp_ota_select_entry_t *s)
  34. {
  35. return s->ota_seq == UINT32_MAX || s->ota_state == ESP_OTA_IMG_INVALID || s->ota_state == ESP_OTA_IMG_ABORTED;
  36. }
  37. bool bootloader_common_ota_select_valid(const esp_ota_select_entry_t *s)
  38. {
  39. return bootloader_common_ota_select_invalid(s) == false && s->crc == bootloader_common_ota_select_crc(s);
  40. }
  41. int bootloader_common_get_active_otadata(esp_ota_select_entry_t *two_otadata)
  42. {
  43. if (two_otadata == NULL) {
  44. return -1;
  45. }
  46. bool valid_two_otadata[2];
  47. valid_two_otadata[0] = bootloader_common_ota_select_valid(&two_otadata[0]);
  48. valid_two_otadata[1] = bootloader_common_ota_select_valid(&two_otadata[1]);
  49. return bootloader_common_select_otadata(two_otadata, valid_two_otadata, true);
  50. }
  51. esp_err_t bootloader_common_check_chip_validity(const esp_image_header_t* img_hdr, esp_image_type type)
  52. {
  53. esp_err_t err = ESP_OK;
  54. esp_chip_id_t chip_id = CONFIG_IDF_FIRMWARE_CHIP_ID;
  55. if (chip_id != img_hdr->chip_id) {
  56. ESP_LOGE(TAG, "mismatch chip ID, expected %d, found %d", chip_id, img_hdr->chip_id);
  57. err = ESP_FAIL;
  58. } else {
  59. #ifndef CONFIG_IDF_ENV_FPGA
  60. unsigned revision = efuse_hal_chip_revision();
  61. unsigned int major_rev = revision / 100;
  62. unsigned int minor_rev = revision % 100;
  63. unsigned min_rev = img_hdr->min_chip_rev_full;
  64. if (type == ESP_IMAGE_BOOTLOADER || type == ESP_IMAGE_APPLICATION) {
  65. if (!ESP_CHIP_REV_ABOVE(revision, min_rev)) {
  66. ESP_LOGE(TAG, "Image requires chip rev >= v%d.%d, but chip is v%d.%d",
  67. min_rev / 100, min_rev % 100,
  68. major_rev, minor_rev);
  69. err = ESP_FAIL;
  70. }
  71. }
  72. if (type == ESP_IMAGE_APPLICATION) {
  73. unsigned max_rev = img_hdr->max_chip_rev_full;
  74. if ((IS_MAX_REV_SET(max_rev) && (revision > max_rev) && !efuse_hal_get_disable_wafer_version_major())) {
  75. ESP_LOGE(TAG, "Image requires chip rev <= v%d.%d, but chip is v%d.%d",
  76. max_rev / 100, max_rev % 100,
  77. major_rev, minor_rev);
  78. err = ESP_FAIL;
  79. }
  80. }
  81. #endif // CONFIG_IDF_ENV_FPGA
  82. }
  83. return err;
  84. }
  85. int bootloader_common_select_otadata(const esp_ota_select_entry_t *two_otadata, bool *valid_two_otadata, bool max)
  86. {
  87. if (two_otadata == NULL || valid_two_otadata == NULL) {
  88. return -1;
  89. }
  90. int active_otadata = -1;
  91. if (valid_two_otadata[0] && valid_two_otadata[1]) {
  92. 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);
  93. if (condition == two_otadata[0].ota_seq) {
  94. active_otadata = 0;
  95. } else {
  96. active_otadata = 1;
  97. }
  98. ESP_LOGD(TAG, "Both OTA copies are valid");
  99. } else {
  100. for (int i = 0; i < 2; ++i) {
  101. if (valid_two_otadata[i]) {
  102. active_otadata = i;
  103. ESP_LOGD(TAG, "Only otadata[%d] is valid", i);
  104. break;
  105. }
  106. }
  107. }
  108. return active_otadata;
  109. }
  110. #if CONFIG_BOOTLOADER_RESERVE_RTC_MEM
  111. static uint32_t rtc_retain_mem_size(void) {
  112. #ifdef CONFIG_BOOTLOADER_CUSTOM_RESERVE_RTC
  113. /* A custom memory has been reserved by the user, do not consider this memory into CRC calculation as it may change without
  114. * the have the user updating the CRC. Return the offset of the custom field, which is equivalent to size of the structure
  115. * minus the size of everything after (including) `custom` */
  116. return offsetof(rtc_retain_mem_t, custom);
  117. #else
  118. return sizeof(rtc_retain_mem_t) - sizeof(bootloader_common_get_rtc_retain_mem()->crc);
  119. #endif
  120. }
  121. static bool is_retain_mem_valid(void)
  122. {
  123. rtc_retain_mem_t* rtc_retain_mem = bootloader_common_get_rtc_retain_mem();
  124. 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;
  125. }
  126. static void update_rtc_retain_mem_crc(void)
  127. {
  128. rtc_retain_mem_t* rtc_retain_mem = bootloader_common_get_rtc_retain_mem();
  129. rtc_retain_mem->crc = esp_rom_crc32_le(UINT32_MAX, (uint8_t*)rtc_retain_mem, rtc_retain_mem_size());
  130. }
  131. NOINLINE_ATTR void bootloader_common_reset_rtc_retain_mem(void)
  132. {
  133. hal_memset(bootloader_common_get_rtc_retain_mem(), 0, sizeof(rtc_retain_mem_t));
  134. }
  135. uint16_t bootloader_common_get_rtc_retain_mem_reboot_counter(void)
  136. {
  137. if (is_retain_mem_valid()) {
  138. return bootloader_common_get_rtc_retain_mem()->reboot_counter;
  139. }
  140. return 0;
  141. }
  142. void bootloader_common_set_rtc_retain_mem_factory_reset_state(void)
  143. {
  144. if (!is_retain_mem_valid()) {
  145. bootloader_common_reset_rtc_retain_mem();
  146. }
  147. bootloader_common_get_rtc_retain_mem()->flags.factory_reset_state = true;
  148. update_rtc_retain_mem_crc();
  149. }
  150. bool bootloader_common_get_rtc_retain_mem_factory_reset_state(void)
  151. {
  152. rtc_retain_mem_t* rtc_retain_mem = bootloader_common_get_rtc_retain_mem();
  153. if (is_retain_mem_valid()) {
  154. bool factory_reset_state = rtc_retain_mem->flags.factory_reset_state;
  155. if (factory_reset_state == true) {
  156. rtc_retain_mem->flags.factory_reset_state = false;
  157. update_rtc_retain_mem_crc();
  158. }
  159. return factory_reset_state;
  160. }
  161. return false;
  162. }
  163. esp_partition_pos_t* bootloader_common_get_rtc_retain_mem_partition(void)
  164. {
  165. if (is_retain_mem_valid()) {
  166. return &bootloader_common_get_rtc_retain_mem()->partition;
  167. }
  168. return NULL;
  169. }
  170. void bootloader_common_update_rtc_retain_mem(esp_partition_pos_t* partition, bool reboot_counter)
  171. {
  172. rtc_retain_mem_t* rtc_retain_mem = bootloader_common_get_rtc_retain_mem();
  173. if (reboot_counter) {
  174. if (!is_retain_mem_valid()) {
  175. bootloader_common_reset_rtc_retain_mem();
  176. }
  177. if (++rtc_retain_mem->reboot_counter == 0) {
  178. // do not allow to overflow. Stop it.
  179. --rtc_retain_mem->reboot_counter;
  180. }
  181. }
  182. if (partition != NULL) {
  183. rtc_retain_mem->partition.offset = partition->offset;
  184. rtc_retain_mem->partition.size = partition->size;
  185. }
  186. update_rtc_retain_mem_crc();
  187. }
  188. rtc_retain_mem_t* bootloader_common_get_rtc_retain_mem(void)
  189. {
  190. #ifdef BOOTLOADER_BUILD
  191. #define RTC_RETAIN_MEM_ADDR (SOC_RTC_DRAM_HIGH - sizeof(rtc_retain_mem_t))
  192. static rtc_retain_mem_t *const s_bootloader_retain_mem = (rtc_retain_mem_t *)RTC_RETAIN_MEM_ADDR;
  193. return s_bootloader_retain_mem;
  194. #else
  195. static __attribute__((section(".bootloader_data_rtc_mem"))) rtc_retain_mem_t s_bootloader_retain_mem;
  196. return &s_bootloader_retain_mem;
  197. #endif // !BOOTLOADER_BUILD
  198. }
  199. #endif // CONFIG_BOOTLOADER_RESERVE_RTC_MEM