bootloader_common.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. // Copyright 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. //
  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 <stdbool.h>
  15. #include <assert.h>
  16. #include "string.h"
  17. #include "sdkconfig.h"
  18. #include "esp_err.h"
  19. #include "esp_log.h"
  20. #if CONFIG_IDF_TARGET_ESP32
  21. #include "esp32/rom/spi_flash.h"
  22. #include "esp32/rom/crc.h"
  23. #include "esp32/rom/gpio.h"
  24. #elif CONFIG_IDF_TARGET_ESP32S2
  25. #include "esp32s2/rom/spi_flash.h"
  26. #include "esp32s2/rom/crc.h"
  27. #include "esp32s2/rom/ets_sys.h"
  28. #include "esp32s2/rom/gpio.h"
  29. #endif
  30. #include "esp_flash_partitions.h"
  31. #include "bootloader_flash.h"
  32. #include "bootloader_common.h"
  33. #include "soc/gpio_periph.h"
  34. #include "soc/rtc.h"
  35. #include "soc/efuse_reg.h"
  36. #include "esp_image_format.h"
  37. #include "bootloader_sha.h"
  38. #include "sys/param.h"
  39. #define ESP_PARTITION_HASH_LEN 32 /* SHA-256 digest length */
  40. static const char* TAG = "boot_comm";
  41. uint32_t bootloader_common_ota_select_crc(const esp_ota_select_entry_t *s)
  42. {
  43. return crc32_le(UINT32_MAX, (uint8_t*)&s->ota_seq, 4);
  44. }
  45. bool bootloader_common_ota_select_invalid(const esp_ota_select_entry_t *s)
  46. {
  47. return s->ota_seq == UINT32_MAX || s->ota_state == ESP_OTA_IMG_INVALID || s->ota_state == ESP_OTA_IMG_ABORTED;
  48. }
  49. bool bootloader_common_ota_select_valid(const esp_ota_select_entry_t *s)
  50. {
  51. return bootloader_common_ota_select_invalid(s) == false && s->crc == bootloader_common_ota_select_crc(s);
  52. }
  53. esp_comm_gpio_hold_t bootloader_common_check_long_hold_gpio(uint32_t num_pin, uint32_t delay_sec)
  54. {
  55. gpio_pad_select_gpio(num_pin);
  56. if (GPIO_PIN_MUX_REG[num_pin]) {
  57. PIN_INPUT_ENABLE(GPIO_PIN_MUX_REG[num_pin]);
  58. }
  59. gpio_pad_pullup(num_pin);
  60. uint32_t tm_start = esp_log_early_timestamp();
  61. if (GPIO_INPUT_GET(num_pin) == 1) {
  62. return GPIO_NOT_HOLD;
  63. }
  64. do {
  65. if (GPIO_INPUT_GET(num_pin) != 0) {
  66. return GPIO_SHORT_HOLD;
  67. }
  68. } while (delay_sec > ((esp_log_early_timestamp() - tm_start) / 1000L));
  69. return GPIO_LONG_HOLD;
  70. }
  71. // Search for a label in the list. list = "nvs1, nvs2, otadata, nvs"; label = "nvs".
  72. bool bootloader_common_label_search(const char *list, char *label)
  73. {
  74. if (list == NULL || label == NULL) {
  75. return false;
  76. }
  77. const char *sub_list_start_like_label = strstr(list, label);
  78. while (sub_list_start_like_label != NULL) {
  79. // ["," or " "] + label + ["," or " " or "\0"]
  80. // first character before the label found there must be a delimiter ["," or " "].
  81. int idx_first = sub_list_start_like_label - list;
  82. if (idx_first == 0 || (idx_first != 0 && (list[idx_first - 1] == ',' || list[idx_first - 1] == ' '))) {
  83. // next character after the label found there must be a delimiter ["," or " " or "\0"].
  84. int len_label = strlen(label);
  85. if (sub_list_start_like_label[len_label] == 0 ||
  86. sub_list_start_like_label[len_label] == ',' ||
  87. sub_list_start_like_label[len_label] == ' ') {
  88. return true;
  89. }
  90. }
  91. // [start_delim] + label + [end_delim] was not found.
  92. // Position is moving to next delimiter if it is not the end of list.
  93. int pos_delim = strcspn(sub_list_start_like_label, ", ");
  94. if (pos_delim == strlen(sub_list_start_like_label)) {
  95. break;
  96. }
  97. sub_list_start_like_label = strstr(&sub_list_start_like_label[pos_delim], label);
  98. }
  99. return false;
  100. }
  101. bool bootloader_common_erase_part_type_data(const char *list_erase, bool ota_data_erase)
  102. {
  103. const esp_partition_info_t *partitions;
  104. const char *marker;
  105. esp_err_t err;
  106. int num_partitions;
  107. bool ret = true;
  108. partitions = bootloader_mmap(ESP_PARTITION_TABLE_OFFSET, ESP_PARTITION_TABLE_MAX_LEN);
  109. if (!partitions) {
  110. ESP_LOGE(TAG, "bootloader_mmap(0x%x, 0x%x) failed", ESP_PARTITION_TABLE_OFFSET, ESP_PARTITION_TABLE_MAX_LEN);
  111. return false;
  112. }
  113. ESP_LOGD(TAG, "mapped partition table 0x%x at 0x%x", ESP_PARTITION_TABLE_OFFSET, (intptr_t)partitions);
  114. err = esp_partition_table_verify(partitions, true, &num_partitions);
  115. if (err != ESP_OK) {
  116. ESP_LOGE(TAG, "Failed to verify partition table");
  117. ret = false;
  118. } else {
  119. ESP_LOGI(TAG, "## Label Usage Offset Length Cleaned");
  120. for (int i = 0; i < num_partitions; i++) {
  121. const esp_partition_info_t *partition = &partitions[i];
  122. char label[sizeof(partition->label) + 1] = {0};
  123. if (partition->type == PART_TYPE_DATA) {
  124. bool fl_ota_data_erase = false;
  125. if (ota_data_erase == true && partition->subtype == PART_SUBTYPE_DATA_OTA) {
  126. fl_ota_data_erase = true;
  127. }
  128. // partition->label is not null-terminated string.
  129. strncpy(label, (char *)&partition->label, sizeof(label) - 1);
  130. if (fl_ota_data_erase == true || (bootloader_common_label_search(list_erase, label) == true)) {
  131. err = bootloader_flash_erase_range(partition->pos.offset, partition->pos.size);
  132. if (err != ESP_OK) {
  133. ret = false;
  134. marker = "err";
  135. } else {
  136. marker = "yes";
  137. }
  138. } else {
  139. marker = "no";
  140. }
  141. ESP_LOGI(TAG, "%2d %-16s data %08x %08x [%s]", i, partition->label,
  142. partition->pos.offset, partition->pos.size, marker);
  143. }
  144. }
  145. }
  146. bootloader_munmap(partitions);
  147. return ret;
  148. }
  149. esp_err_t bootloader_common_get_sha256_of_partition (uint32_t address, uint32_t size, int type, uint8_t *out_sha_256)
  150. {
  151. if (out_sha_256 == NULL || size == 0) {
  152. return ESP_ERR_INVALID_ARG;
  153. }
  154. if (type == PART_TYPE_APP) {
  155. const esp_partition_pos_t partition_pos = {
  156. .offset = address,
  157. .size = size,
  158. };
  159. esp_image_metadata_t data;
  160. // Function esp_image_verify() verifies and fills the structure data.
  161. // here important to get: image_digest, image_len, hash_appended.
  162. if (esp_image_verify(ESP_IMAGE_VERIFY_SILENT, &partition_pos, &data) != ESP_OK) {
  163. return ESP_ERR_IMAGE_INVALID;
  164. }
  165. if (data.image.hash_appended) {
  166. memcpy(out_sha_256, data.image_digest, ESP_PARTITION_HASH_LEN);
  167. return ESP_OK;
  168. }
  169. // If image doesn't have a appended hash then hash calculates for entire image.
  170. size = data.image_len;
  171. }
  172. // If image is type by data then hash is calculated for entire image.
  173. const void *partition_bin = bootloader_mmap(address, size);
  174. if (partition_bin == NULL) {
  175. ESP_LOGE(TAG, "bootloader_mmap(0x%x, 0x%x) failed", address, size);
  176. return ESP_FAIL;
  177. }
  178. bootloader_sha256_handle_t sha_handle = bootloader_sha256_start();
  179. if (sha_handle == NULL) {
  180. bootloader_munmap(partition_bin);
  181. return ESP_ERR_NO_MEM;
  182. }
  183. bootloader_sha256_data(sha_handle, partition_bin, size);
  184. bootloader_sha256_finish(sha_handle, out_sha_256);
  185. bootloader_munmap(partition_bin);
  186. return ESP_OK;
  187. }
  188. int bootloader_common_select_otadata(const esp_ota_select_entry_t *two_otadata, bool *valid_two_otadata, bool max)
  189. {
  190. if (two_otadata == NULL || valid_two_otadata == NULL) {
  191. return -1;
  192. }
  193. int active_otadata = -1;
  194. if (valid_two_otadata[0] && valid_two_otadata[1]) {
  195. 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);
  196. if (condition == two_otadata[0].ota_seq) {
  197. active_otadata = 0;
  198. } else {
  199. active_otadata = 1;
  200. }
  201. ESP_LOGD(TAG, "Both OTA copies are valid");
  202. } else {
  203. for (int i = 0; i < 2; ++i) {
  204. if (valid_two_otadata[i]) {
  205. active_otadata = i;
  206. ESP_LOGD(TAG, "Only otadata[%d] is valid", i);
  207. break;
  208. }
  209. }
  210. }
  211. return active_otadata;
  212. }
  213. int bootloader_common_get_active_otadata(esp_ota_select_entry_t *two_otadata)
  214. {
  215. if (two_otadata == NULL) {
  216. return -1;
  217. }
  218. bool valid_two_otadata[2];
  219. valid_two_otadata[0] = bootloader_common_ota_select_valid(&two_otadata[0]);
  220. valid_two_otadata[1] = bootloader_common_ota_select_valid(&two_otadata[1]);
  221. return bootloader_common_select_otadata(two_otadata, valid_two_otadata, true);
  222. }
  223. esp_err_t bootloader_common_get_partition_description(const esp_partition_pos_t *partition, esp_app_desc_t *app_desc)
  224. {
  225. if (partition == NULL || app_desc == NULL || partition->offset == 0) {
  226. return ESP_ERR_INVALID_ARG;
  227. }
  228. const uint8_t *image = bootloader_mmap(partition->offset, partition->size);
  229. if (image == NULL) {
  230. ESP_LOGE(TAG, "bootloader_mmap(0x%x, 0x%x) failed", partition->offset, partition->size);
  231. return ESP_FAIL;
  232. }
  233. memcpy(app_desc, image + sizeof(esp_image_header_t) + sizeof(esp_image_segment_header_t), sizeof(esp_app_desc_t));
  234. bootloader_munmap(image);
  235. if (app_desc->magic_word != ESP_APP_DESC_MAGIC_WORD) {
  236. return ESP_ERR_NOT_FOUND;
  237. }
  238. return ESP_OK;
  239. }
  240. void bootloader_common_vddsdio_configure(void)
  241. {
  242. #if CONFIG_BOOTLOADER_VDDSDIO_BOOST_1_9V
  243. rtc_vddsdio_config_t cfg = rtc_vddsdio_get_config();
  244. if (cfg.enable == 1 && cfg.tieh == RTC_VDDSDIO_TIEH_1_8V) { // VDDSDIO regulator is enabled @ 1.8V
  245. cfg.drefh = 3;
  246. cfg.drefm = 3;
  247. cfg.drefl = 3;
  248. cfg.force = 1;
  249. rtc_vddsdio_set_config(cfg);
  250. ets_delay_us(10); // wait for regulator to become stable
  251. }
  252. #endif // CONFIG_BOOTLOADER_VDDSDIO_BOOST
  253. }
  254. esp_err_t bootloader_common_check_chip_validity(const esp_image_header_t* img_hdr, esp_image_type type)
  255. {
  256. esp_err_t err = ESP_OK;
  257. esp_chip_id_t chip_id = CONFIG_IDF_FIRMWARE_CHIP_ID;
  258. if (chip_id != img_hdr->chip_id) {
  259. ESP_LOGE(TAG, "mismatch chip ID, expected %d, found %d", chip_id, img_hdr->chip_id);
  260. err = ESP_FAIL;
  261. }
  262. uint8_t revision = bootloader_common_get_chip_revision();
  263. if (revision < img_hdr->min_chip_rev) {
  264. ESP_LOGE(TAG, "can't run on lower chip revision, expected %d, found %d", revision, img_hdr->min_chip_rev);
  265. err = ESP_FAIL;
  266. } else if (revision != img_hdr->min_chip_rev) {
  267. #ifdef BOOTLOADER_BUILD
  268. ESP_LOGI(TAG, "chip revision: %d, min. %s chip revision: %d", revision, type == ESP_IMAGE_BOOTLOADER ? "bootloader" : "application", img_hdr->min_chip_rev);
  269. #endif
  270. }
  271. return err;
  272. }
  273. RESET_REASON bootloader_common_get_reset_reason(int cpu_no)
  274. {
  275. return rtc_get_reset_reason(cpu_no);
  276. }
  277. #if defined( CONFIG_BOOTLOADER_SKIP_VALIDATE_IN_DEEP_SLEEP ) || defined( CONFIG_BOOTLOADER_CUSTOM_RESERVE_RTC )
  278. rtc_retain_mem_t *const rtc_retain_mem = (rtc_retain_mem_t *)(SOC_RTC_DRAM_HIGH - sizeof(rtc_retain_mem_t));
  279. static bool check_rtc_retain_mem(void)
  280. {
  281. return 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;
  282. }
  283. static void update_rtc_retain_mem_crc(void)
  284. {
  285. rtc_retain_mem->crc = crc32_le(UINT32_MAX, (uint8_t*)rtc_retain_mem, sizeof(rtc_retain_mem_t) - sizeof(rtc_retain_mem->crc));
  286. }
  287. void bootloader_common_reset_rtc_retain_mem(void)
  288. {
  289. memset(rtc_retain_mem, 0, sizeof(rtc_retain_mem_t));
  290. }
  291. uint16_t bootloader_common_get_rtc_retain_mem_reboot_counter(void)
  292. {
  293. if (check_rtc_retain_mem()) {
  294. return rtc_retain_mem->reboot_counter;
  295. }
  296. return 0;
  297. }
  298. esp_partition_pos_t* bootloader_common_get_rtc_retain_mem_partition(void)
  299. {
  300. if (check_rtc_retain_mem()) {
  301. return &rtc_retain_mem->partition;
  302. }
  303. return NULL;
  304. }
  305. void bootloader_common_update_rtc_retain_mem(esp_partition_pos_t* partition, bool reboot_counter)
  306. {
  307. if (reboot_counter) {
  308. if (!check_rtc_retain_mem()) {
  309. bootloader_common_reset_rtc_retain_mem();
  310. }
  311. if (++rtc_retain_mem->reboot_counter == 0) {
  312. // do not allow to overflow. Stop it.
  313. --rtc_retain_mem->reboot_counter;
  314. }
  315. }
  316. if (partition != NULL) {
  317. rtc_retain_mem->partition.offset = partition->offset;
  318. rtc_retain_mem->partition.size = partition->size;
  319. }
  320. update_rtc_retain_mem_crc();
  321. }
  322. rtc_retain_mem_t* bootloader_common_get_rtc_retain_mem(void)
  323. {
  324. return rtc_retain_mem;
  325. }
  326. #endif