bootloader_common.c 7.4 KB

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