bootloader_common_loader.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. // Copyright 2020 Espressif Systems (Shanghai) Co., 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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "string.h"
  15. #include "sdkconfig.h"
  16. #include "esp_err.h"
  17. #include "esp_log.h"
  18. #if CONFIG_IDF_TARGET_ESP32
  19. #include "esp32/rom/spi_flash.h"
  20. #elif CONFIG_IDF_TARGET_ESP32S2
  21. #include "esp32s2/rom/spi_flash.h"
  22. #include "esp32s2/rom/ets_sys.h"
  23. #endif
  24. #include "esp_rom_crc.h"
  25. #include "esp_rom_gpio.h"
  26. #include "esp_flash_partitions.h"
  27. #include "bootloader_flash.h"
  28. #include "bootloader_common.h"
  29. #include "soc/gpio_periph.h"
  30. #include "soc/rtc.h"
  31. #include "soc/efuse_reg.h"
  32. #include "hal/gpio_ll.h"
  33. #include "esp_image_format.h"
  34. #include "bootloader_sha.h"
  35. #include "sys/param.h"
  36. #include "bootloader_flash_priv.h"
  37. #define ESP_PARTITION_HASH_LEN 32 /* SHA-256 digest length */
  38. static const char* TAG = "boot_comm";
  39. uint32_t bootloader_common_ota_select_crc(const esp_ota_select_entry_t *s)
  40. {
  41. return esp_rom_crc32_le(UINT32_MAX, (uint8_t*)&s->ota_seq, 4);
  42. }
  43. bool bootloader_common_ota_select_invalid(const esp_ota_select_entry_t *s)
  44. {
  45. return s->ota_seq == UINT32_MAX || s->ota_state == ESP_OTA_IMG_INVALID || s->ota_state == ESP_OTA_IMG_ABORTED;
  46. }
  47. bool bootloader_common_ota_select_valid(const esp_ota_select_entry_t *s)
  48. {
  49. return bootloader_common_ota_select_invalid(s) == false && s->crc == bootloader_common_ota_select_crc(s);
  50. }
  51. int bootloader_common_get_active_otadata(esp_ota_select_entry_t *two_otadata)
  52. {
  53. if (two_otadata == NULL) {
  54. return -1;
  55. }
  56. bool valid_two_otadata[2];
  57. valid_two_otadata[0] = bootloader_common_ota_select_valid(&two_otadata[0]);
  58. valid_two_otadata[1] = bootloader_common_ota_select_valid(&two_otadata[1]);
  59. return bootloader_common_select_otadata(two_otadata, valid_two_otadata, true);
  60. }
  61. esp_err_t bootloader_common_check_chip_validity(const esp_image_header_t* img_hdr, esp_image_type type)
  62. {
  63. esp_err_t err = ESP_OK;
  64. esp_chip_id_t chip_id = CONFIG_IDF_FIRMWARE_CHIP_ID;
  65. if (chip_id != img_hdr->chip_id) {
  66. ESP_LOGE(TAG, "mismatch chip ID, expected %d, found %d", chip_id, img_hdr->chip_id);
  67. err = ESP_FAIL;
  68. }
  69. uint8_t revision = bootloader_common_get_chip_revision();
  70. if (revision < img_hdr->min_chip_rev) {
  71. ESP_LOGE(TAG, "can't run on lower chip revision, expected %d, found %d", revision, img_hdr->min_chip_rev);
  72. err = ESP_FAIL;
  73. } else if (revision != img_hdr->min_chip_rev) {
  74. #ifdef BOOTLOADER_BUILD
  75. ESP_LOGI(TAG, "chip revision: %d, min. %s chip revision: %d", revision, type == ESP_IMAGE_BOOTLOADER ? "bootloader" : "application", img_hdr->min_chip_rev);
  76. #endif
  77. }
  78. return err;
  79. }
  80. int bootloader_common_select_otadata(const esp_ota_select_entry_t *two_otadata, bool *valid_two_otadata, bool max)
  81. {
  82. if (two_otadata == NULL || valid_two_otadata == NULL) {
  83. return -1;
  84. }
  85. int active_otadata = -1;
  86. if (valid_two_otadata[0] && valid_two_otadata[1]) {
  87. int 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);
  88. if (condition == two_otadata[0].ota_seq) {
  89. active_otadata = 0;
  90. } else {
  91. active_otadata = 1;
  92. }
  93. ESP_LOGD(TAG, "Both OTA copies are valid");
  94. } else {
  95. for (int i = 0; i < 2; ++i) {
  96. if (valid_two_otadata[i]) {
  97. active_otadata = i;
  98. ESP_LOGD(TAG, "Only otadata[%d] is valid", i);
  99. break;
  100. }
  101. }
  102. }
  103. return active_otadata;
  104. }
  105. esp_err_t bootloader_common_get_partition_description(const esp_partition_pos_t *partition, esp_app_desc_t *app_desc)
  106. {
  107. if (partition == NULL || app_desc == NULL || partition->offset == 0) {
  108. return ESP_ERR_INVALID_ARG;
  109. }
  110. const uint32_t app_desc_offset = sizeof(esp_image_header_t) + sizeof(esp_image_segment_header_t);
  111. const uint32_t mmap_size = app_desc_offset + sizeof(esp_app_desc_t);
  112. const uint8_t *image = bootloader_mmap(partition->offset, mmap_size);
  113. if (image == NULL) {
  114. ESP_LOGE(TAG, "bootloader_mmap(0x%x, 0x%x) failed", partition->offset, mmap_size);
  115. return ESP_FAIL;
  116. }
  117. memcpy(app_desc, image + app_desc_offset, sizeof(esp_app_desc_t));
  118. bootloader_munmap(image);
  119. if (app_desc->magic_word != ESP_APP_DESC_MAGIC_WORD) {
  120. return ESP_ERR_NOT_FOUND;
  121. }
  122. return ESP_OK;
  123. }
  124. #if defined( CONFIG_BOOTLOADER_SKIP_VALIDATE_IN_DEEP_SLEEP ) || defined( CONFIG_BOOTLOADER_CUSTOM_RESERVE_RTC )
  125. rtc_retain_mem_t *const rtc_retain_mem = (rtc_retain_mem_t *)(SOC_RTC_DRAM_HIGH - sizeof(rtc_retain_mem_t));
  126. static bool check_rtc_retain_mem(void)
  127. {
  128. return esp_rom_crc32_le(UINT32_MAX, (uint8_t*)rtc_retain_mem, sizeof(rtc_retain_mem_t) - sizeof(rtc_retain_mem->crc)) == rtc_retain_mem->crc && rtc_retain_mem->crc != UINT32_MAX;
  129. }
  130. static void update_rtc_retain_mem_crc(void)
  131. {
  132. rtc_retain_mem->crc = esp_rom_crc32_le(UINT32_MAX, (uint8_t*)rtc_retain_mem, sizeof(rtc_retain_mem_t) - sizeof(rtc_retain_mem->crc));
  133. }
  134. void bootloader_common_reset_rtc_retain_mem(void)
  135. {
  136. memset(rtc_retain_mem, 0, sizeof(rtc_retain_mem_t));
  137. }
  138. uint16_t bootloader_common_get_rtc_retain_mem_reboot_counter(void)
  139. {
  140. if (check_rtc_retain_mem()) {
  141. return rtc_retain_mem->reboot_counter;
  142. }
  143. return 0;
  144. }
  145. esp_partition_pos_t* bootloader_common_get_rtc_retain_mem_partition(void)
  146. {
  147. if (check_rtc_retain_mem()) {
  148. return &rtc_retain_mem->partition;
  149. }
  150. return NULL;
  151. }
  152. void bootloader_common_update_rtc_retain_mem(esp_partition_pos_t* partition, bool reboot_counter)
  153. {
  154. if (reboot_counter) {
  155. if (!check_rtc_retain_mem()) {
  156. bootloader_common_reset_rtc_retain_mem();
  157. }
  158. if (++rtc_retain_mem->reboot_counter == 0) {
  159. // do not allow to overflow. Stop it.
  160. --rtc_retain_mem->reboot_counter;
  161. }
  162. }
  163. if (partition != NULL) {
  164. rtc_retain_mem->partition.offset = partition->offset;
  165. rtc_retain_mem->partition.size = partition->size;
  166. }
  167. update_rtc_retain_mem_crc();
  168. }
  169. rtc_retain_mem_t* bootloader_common_get_rtc_retain_mem(void)
  170. {
  171. return rtc_retain_mem;
  172. }
  173. #endif // defined( CONFIG_BOOTLOADER_SKIP_VALIDATE_IN_DEEP_SLEEP ) || defined( CONFIG_BOOTLOADER_CUSTOM_RESERVE_RTC )