bootloader_common.c 7.0 KB

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