bootloader_common.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * SPDX-FileCopyrightText: 2018-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdbool.h>
  7. #include <assert.h>
  8. #include "string.h"
  9. #include "sdkconfig.h"
  10. #include "esp_err.h"
  11. #include "esp_log.h"
  12. #if CONFIG_IDF_TARGET_ESP32
  13. #include "esp32/rom/spi_flash.h"
  14. #elif CONFIG_IDF_TARGET_ESP32S2
  15. #include "esp32s2/rom/spi_flash.h"
  16. #elif CONFIG_IDF_TARGET_ESP32S3
  17. #include "esp32s3/rom/spi_flash.h"
  18. #elif CONFIG_IDF_TARGET_ESP32C3
  19. #include "esp32c3/rom/spi_flash.h"
  20. #endif
  21. #include "esp_rom_crc.h"
  22. #include "esp_rom_gpio.h"
  23. #include "esp_rom_sys.h"
  24. #include "esp_flash_partitions.h"
  25. #include "bootloader_flash_priv.h"
  26. #include "bootloader_common.h"
  27. #include "bootloader_utility.h"
  28. #include "soc/gpio_periph.h"
  29. #include "soc/rtc.h"
  30. #include "soc/efuse_reg.h"
  31. #include "hal/gpio_ll.h"
  32. #include "esp_image_format.h"
  33. #include "bootloader_sha.h"
  34. #include "sys/param.h"
  35. #define ESP_PARTITION_HASH_LEN 32 /* SHA-256 digest length */
  36. static const char* TAG = "boot_comm";
  37. esp_comm_gpio_hold_t bootloader_common_check_long_hold_gpio(uint32_t num_pin, uint32_t delay_sec)
  38. {
  39. esp_rom_gpio_pad_select_gpio(num_pin);
  40. if (GPIO_PIN_MUX_REG[num_pin]) {
  41. PIN_INPUT_ENABLE(GPIO_PIN_MUX_REG[num_pin]);
  42. }
  43. esp_rom_gpio_pad_pullup_only(num_pin);
  44. uint32_t tm_start = esp_log_early_timestamp();
  45. if (gpio_ll_get_level(&GPIO, num_pin) == 1) {
  46. return GPIO_NOT_HOLD;
  47. }
  48. do {
  49. if (gpio_ll_get_level(&GPIO, num_pin) != 0) {
  50. return GPIO_SHORT_HOLD;
  51. }
  52. } while (delay_sec > ((esp_log_early_timestamp() - tm_start) / 1000L));
  53. return GPIO_LONG_HOLD;
  54. }
  55. // Search for a label in the list. list = "nvs1, nvs2, otadata, nvs"; label = "nvs".
  56. bool bootloader_common_label_search(const char *list, char *label)
  57. {
  58. if (list == NULL || label == NULL) {
  59. return false;
  60. }
  61. const char *sub_list_start_like_label = strstr(list, label);
  62. while (sub_list_start_like_label != NULL) {
  63. // ["," or " "] + label + ["," or " " or "\0"]
  64. // first character before the label found there must be a delimiter ["," or " "].
  65. int idx_first = sub_list_start_like_label - list;
  66. if (idx_first == 0 || (idx_first != 0 && (list[idx_first - 1] == ',' || list[idx_first - 1] == ' '))) {
  67. // next character after the label found there must be a delimiter ["," or " " or "\0"].
  68. int len_label = strlen(label);
  69. if (sub_list_start_like_label[len_label] == 0 ||
  70. sub_list_start_like_label[len_label] == ',' ||
  71. sub_list_start_like_label[len_label] == ' ') {
  72. return true;
  73. }
  74. }
  75. // [start_delim] + label + [end_delim] was not found.
  76. // Position is moving to next delimiter if it is not the end of list.
  77. size_t pos_delim = strcspn(sub_list_start_like_label, ", ");
  78. if (pos_delim == strlen(sub_list_start_like_label)) {
  79. break;
  80. }
  81. sub_list_start_like_label = strstr(&sub_list_start_like_label[pos_delim], label);
  82. }
  83. return false;
  84. }
  85. bool bootloader_common_erase_part_type_data(const char *list_erase, bool ota_data_erase)
  86. {
  87. const esp_partition_info_t *partitions;
  88. const char *marker;
  89. esp_err_t err;
  90. int num_partitions;
  91. bool ret = true;
  92. partitions = bootloader_mmap(ESP_PARTITION_TABLE_OFFSET, ESP_PARTITION_TABLE_MAX_LEN);
  93. if (!partitions) {
  94. ESP_LOGE(TAG, "bootloader_mmap(0x%x, 0x%x) failed", ESP_PARTITION_TABLE_OFFSET, ESP_PARTITION_TABLE_MAX_LEN);
  95. return false;
  96. }
  97. ESP_LOGD(TAG, "mapped partition table 0x%x at 0x%x", ESP_PARTITION_TABLE_OFFSET, (intptr_t)partitions);
  98. err = esp_partition_table_verify(partitions, true, &num_partitions);
  99. if (err != ESP_OK) {
  100. ESP_LOGE(TAG, "Failed to verify partition table");
  101. ret = false;
  102. } else {
  103. ESP_LOGI(TAG, "## Label Usage Offset Length Cleaned");
  104. for (int i = 0; i < num_partitions; i++) {
  105. const esp_partition_info_t *partition = &partitions[i];
  106. char label[sizeof(partition->label) + 1] = {0};
  107. if (partition->type == PART_TYPE_DATA) {
  108. bool fl_ota_data_erase = false;
  109. if (ota_data_erase == true && partition->subtype == PART_SUBTYPE_DATA_OTA) {
  110. fl_ota_data_erase = true;
  111. }
  112. // partition->label is not null-terminated string.
  113. strncpy(label, (char *)&partition->label, sizeof(label) - 1);
  114. if (fl_ota_data_erase == true || (bootloader_common_label_search(list_erase, label) == true)) {
  115. err = bootloader_flash_erase_range(partition->pos.offset, partition->pos.size);
  116. if (err != ESP_OK) {
  117. ret = false;
  118. marker = "err";
  119. } else {
  120. marker = "yes";
  121. }
  122. } else {
  123. marker = "no";
  124. }
  125. ESP_LOGI(TAG, "%2d %-16s data %08x %08x [%s]", i, partition->label,
  126. partition->pos.offset, partition->pos.size, marker);
  127. }
  128. }
  129. }
  130. bootloader_munmap(partitions);
  131. return ret;
  132. }
  133. esp_err_t bootloader_common_get_sha256_of_partition (uint32_t address, uint32_t size, int type, uint8_t *out_sha_256)
  134. {
  135. if (out_sha_256 == NULL || size == 0) {
  136. return ESP_ERR_INVALID_ARG;
  137. }
  138. if (type == PART_TYPE_APP) {
  139. const esp_partition_pos_t partition_pos = {
  140. .offset = address,
  141. .size = size,
  142. };
  143. esp_image_metadata_t data;
  144. if (esp_image_get_metadata(&partition_pos, &data) != ESP_OK) {
  145. return ESP_ERR_IMAGE_INVALID;
  146. }
  147. if (data.image.hash_appended) {
  148. memcpy(out_sha_256, data.image_digest, ESP_PARTITION_HASH_LEN);
  149. return ESP_OK;
  150. }
  151. // If image doesn't have a appended hash then hash calculates for entire image.
  152. size = data.image_len;
  153. }
  154. // If image is type by data then hash is calculated for entire image.
  155. return bootloader_sha256_flash_contents(address, size, out_sha_256);
  156. }
  157. void bootloader_common_vddsdio_configure(void)
  158. {
  159. #if CONFIG_BOOTLOADER_VDDSDIO_BOOST_1_9V
  160. rtc_vddsdio_config_t cfg = rtc_vddsdio_get_config();
  161. if (cfg.enable == 1 && cfg.tieh == RTC_VDDSDIO_TIEH_1_8V) { // VDDSDIO regulator is enabled @ 1.8V
  162. cfg.drefh = 3;
  163. cfg.drefm = 3;
  164. cfg.drefl = 3;
  165. cfg.force = 1;
  166. rtc_vddsdio_set_config(cfg);
  167. esp_rom_delay_us(10); // wait for regulator to become stable
  168. }
  169. #endif // CONFIG_BOOTLOADER_VDDSDIO_BOOST
  170. }
  171. RESET_REASON bootloader_common_get_reset_reason(int cpu_no)
  172. {
  173. return rtc_get_reset_reason(cpu_no);
  174. }