bootloader_common.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. #include "rom/spi_flash.h"
  21. #include "rom/crc.h"
  22. #include "rom/ets_sys.h"
  23. #include "rom/gpio.h"
  24. #include "esp_flash_data_types.h"
  25. #include "esp_secure_boot.h"
  26. #include "esp_flash_partitions.h"
  27. #include "bootloader_flash.h"
  28. #include "bootloader_common.h"
  29. #include "soc/gpio_periph.h"
  30. #include "esp_image_format.h"
  31. #include "bootloader_sha.h"
  32. #define ESP_PARTITION_HASH_LEN 32 /* SHA-256 digest length */
  33. static const char* TAG = "boot_comm";
  34. uint32_t bootloader_common_ota_select_crc(const esp_ota_select_entry_t *s)
  35. {
  36. return crc32_le(UINT32_MAX, (uint8_t*)&s->ota_seq, 4);
  37. }
  38. bool bootloader_common_ota_select_valid(const esp_ota_select_entry_t *s)
  39. {
  40. return s->ota_seq != UINT32_MAX && s->crc == bootloader_common_ota_select_crc(s);
  41. }
  42. esp_comm_gpio_hold_t bootloader_common_check_long_hold_gpio(uint32_t num_pin, uint32_t delay_sec)
  43. {
  44. gpio_pad_select_gpio(num_pin);
  45. if (GPIO_PIN_MUX_REG[num_pin]) {
  46. PIN_INPUT_ENABLE(GPIO_PIN_MUX_REG[num_pin]);
  47. }
  48. gpio_pad_pullup(num_pin);
  49. uint32_t tm_start = esp_log_early_timestamp();
  50. if (GPIO_INPUT_GET(num_pin) == 1) {
  51. return GPIO_NOT_HOLD;
  52. }
  53. do {
  54. if (GPIO_INPUT_GET(num_pin) != 0) {
  55. return GPIO_SHORT_HOLD;
  56. }
  57. } while (delay_sec > ((esp_log_early_timestamp() - tm_start) / 1000L));
  58. return GPIO_LONG_HOLD;
  59. }
  60. // Search for a label in the list. list = "nvs1, nvs2, otadata, nvs"; label = "nvs".
  61. bool bootloader_common_label_search(const char *list, char *label)
  62. {
  63. if (list == NULL || label == NULL) {
  64. return false;
  65. }
  66. const char *sub_list_start_like_label = strstr(list, label);
  67. while (sub_list_start_like_label != NULL) {
  68. // ["," or " "] + label + ["," or " " or "\0"]
  69. // first character before the label found there must be a delimiter ["," or " "].
  70. int idx_first = sub_list_start_like_label - list;
  71. if (idx_first == 0 || (idx_first != 0 && (list[idx_first - 1] == ',' || list[idx_first - 1] == ' '))) {
  72. // next character after the label found there must be a delimiter ["," or " " or "\0"].
  73. int len_label = strlen(label);
  74. if (sub_list_start_like_label[len_label] == 0 ||
  75. sub_list_start_like_label[len_label] == ',' ||
  76. sub_list_start_like_label[len_label] == ' ') {
  77. return true;
  78. }
  79. }
  80. // [start_delim] + label + [end_delim] was not found.
  81. // Position is moving to next delimiter if it is not the end of list.
  82. int pos_delim = strcspn(sub_list_start_like_label, ", ");
  83. if (pos_delim == strlen(sub_list_start_like_label)) {
  84. break;
  85. }
  86. sub_list_start_like_label = strstr(&sub_list_start_like_label[pos_delim], label);
  87. }
  88. return false;
  89. }
  90. bool bootloader_common_erase_part_type_data(const char *list_erase, bool ota_data_erase)
  91. {
  92. const esp_partition_info_t *partitions;
  93. const char *marker;
  94. esp_err_t err;
  95. int num_partitions;
  96. bool ret = true;
  97. partitions = bootloader_mmap(ESP_PARTITION_TABLE_OFFSET, ESP_PARTITION_TABLE_MAX_LEN);
  98. if (!partitions) {
  99. ESP_LOGE(TAG, "bootloader_mmap(0x%x, 0x%x) failed", ESP_PARTITION_TABLE_OFFSET, ESP_PARTITION_TABLE_MAX_LEN);
  100. return false;
  101. }
  102. ESP_LOGD(TAG, "mapped partition table 0x%x at 0x%x", ESP_PARTITION_TABLE_OFFSET, (intptr_t)partitions);
  103. err = esp_partition_table_verify(partitions, true, &num_partitions);
  104. if (err != ESP_OK) {
  105. ESP_LOGE(TAG, "Failed to verify partition table");
  106. ret = false;
  107. } else {
  108. ESP_LOGI(TAG, "## Label Usage Offset Length Cleaned");
  109. for (int i = 0; i < num_partitions; i++) {
  110. const esp_partition_info_t *partition = &partitions[i];
  111. char label[sizeof(partition->label) + 1] = {0};
  112. if (partition->type == PART_TYPE_DATA) {
  113. bool fl_ota_data_erase = false;
  114. if (ota_data_erase == true && partition->subtype == PART_SUBTYPE_DATA_OTA) {
  115. fl_ota_data_erase = true;
  116. }
  117. // partition->label is not null-terminated string.
  118. strncpy(label, (char *)&partition->label, sizeof(label) - 1);
  119. if (fl_ota_data_erase == true || (bootloader_common_label_search(list_erase, label) == true)) {
  120. err = esp_rom_spiflash_erase_area(partition->pos.offset, partition->pos.size);
  121. if (err != ESP_OK) {
  122. ret = false;
  123. marker = "err";
  124. } else {
  125. marker = "yes";
  126. }
  127. } else {
  128. marker = "no";
  129. }
  130. ESP_LOGI(TAG, "%2d %-16s data %08x %08x [%s]", i, partition->label,
  131. partition->pos.offset, partition->pos.size, marker);
  132. }
  133. }
  134. }
  135. bootloader_munmap(partitions);
  136. return ret;
  137. }
  138. esp_err_t bootloader_common_get_sha256_of_partition (uint32_t address, uint32_t size, int type, uint8_t *out_sha_256)
  139. {
  140. if (out_sha_256 == NULL || size == 0) {
  141. return ESP_ERR_INVALID_ARG;
  142. }
  143. if (type == PART_TYPE_APP) {
  144. const esp_partition_pos_t partition_pos = {
  145. .offset = address,
  146. .size = size,
  147. };
  148. esp_image_metadata_t data;
  149. // Function esp_image_verify() verifies and fills the structure data.
  150. // here important to get: image_digest, image_len, hash_appended.
  151. if (esp_image_verify(ESP_IMAGE_VERIFY_SILENT, &partition_pos, &data) != ESP_OK) {
  152. return ESP_ERR_IMAGE_INVALID;
  153. }
  154. if (data.image.hash_appended) {
  155. memcpy(out_sha_256, data.image_digest, ESP_PARTITION_HASH_LEN);
  156. return ESP_OK;
  157. }
  158. // If image doesn't have a appended hash then hash calculates for entire image.
  159. size = data.image_len;
  160. }
  161. // If image is type by data then hash is calculated for entire image.
  162. const void *partition_bin = bootloader_mmap(address, size);
  163. if (partition_bin == NULL) {
  164. ESP_LOGE(TAG, "bootloader_mmap(0x%x, 0x%x) failed", address, size);
  165. return ESP_FAIL;
  166. }
  167. bootloader_sha256_handle_t sha_handle = bootloader_sha256_start();
  168. if (sha_handle == NULL) {
  169. bootloader_munmap(partition_bin);
  170. return ESP_ERR_NO_MEM;
  171. }
  172. bootloader_sha256_data(sha_handle, partition_bin, size);
  173. bootloader_sha256_finish(sha_handle, out_sha_256);
  174. bootloader_munmap(partition_bin);
  175. return ESP_OK;
  176. }