bootloader_common.c 7.2 KB

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