bootloader_common.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. #include "sys/param.h"
  33. #define ESP_PARTITION_HASH_LEN 32 /* SHA-256 digest length */
  34. static const char* TAG = "boot_comm";
  35. uint32_t bootloader_common_ota_select_crc(const esp_ota_select_entry_t *s)
  36. {
  37. return crc32_le(UINT32_MAX, (uint8_t*)&s->ota_seq, 4);
  38. }
  39. bool bootloader_common_ota_select_invalid(const esp_ota_select_entry_t *s)
  40. {
  41. return s->ota_seq == UINT32_MAX || s->ota_state == ESP_OTA_IMG_INVALID || s->ota_state == ESP_OTA_IMG_ABORTED;
  42. }
  43. bool bootloader_common_ota_select_valid(const esp_ota_select_entry_t *s)
  44. {
  45. return bootloader_common_ota_select_invalid(s) == false && s->crc == bootloader_common_ota_select_crc(s);
  46. }
  47. esp_comm_gpio_hold_t bootloader_common_check_long_hold_gpio(uint32_t num_pin, uint32_t delay_sec)
  48. {
  49. gpio_pad_select_gpio(num_pin);
  50. if (GPIO_PIN_MUX_REG[num_pin]) {
  51. PIN_INPUT_ENABLE(GPIO_PIN_MUX_REG[num_pin]);
  52. }
  53. gpio_pad_pullup(num_pin);
  54. uint32_t tm_start = esp_log_early_timestamp();
  55. if (GPIO_INPUT_GET(num_pin) == 1) {
  56. return GPIO_NOT_HOLD;
  57. }
  58. do {
  59. if (GPIO_INPUT_GET(num_pin) != 0) {
  60. return GPIO_SHORT_HOLD;
  61. }
  62. } while (delay_sec > ((esp_log_early_timestamp() - tm_start) / 1000L));
  63. return GPIO_LONG_HOLD;
  64. }
  65. // Search for a label in the list. list = "nvs1, nvs2, otadata, nvs"; label = "nvs".
  66. bool bootloader_common_label_search(const char *list, char *label)
  67. {
  68. if (list == NULL || label == NULL) {
  69. return false;
  70. }
  71. const char *sub_list_start_like_label = strstr(list, label);
  72. while (sub_list_start_like_label != NULL) {
  73. // ["," or " "] + label + ["," or " " or "\0"]
  74. // first character before the label found there must be a delimiter ["," or " "].
  75. int idx_first = sub_list_start_like_label - list;
  76. if (idx_first == 0 || (idx_first != 0 && (list[idx_first - 1] == ',' || list[idx_first - 1] == ' '))) {
  77. // next character after the label found there must be a delimiter ["," or " " or "\0"].
  78. int len_label = strlen(label);
  79. if (sub_list_start_like_label[len_label] == 0 ||
  80. sub_list_start_like_label[len_label] == ',' ||
  81. sub_list_start_like_label[len_label] == ' ') {
  82. return true;
  83. }
  84. }
  85. // [start_delim] + label + [end_delim] was not found.
  86. // Position is moving to next delimiter if it is not the end of list.
  87. int pos_delim = strcspn(sub_list_start_like_label, ", ");
  88. if (pos_delim == strlen(sub_list_start_like_label)) {
  89. break;
  90. }
  91. sub_list_start_like_label = strstr(&sub_list_start_like_label[pos_delim], label);
  92. }
  93. return false;
  94. }
  95. bool bootloader_common_erase_part_type_data(const char *list_erase, bool ota_data_erase)
  96. {
  97. const esp_partition_info_t *partitions;
  98. const char *marker;
  99. esp_err_t err;
  100. int num_partitions;
  101. bool ret = true;
  102. partitions = bootloader_mmap(ESP_PARTITION_TABLE_OFFSET, ESP_PARTITION_TABLE_MAX_LEN);
  103. if (!partitions) {
  104. ESP_LOGE(TAG, "bootloader_mmap(0x%x, 0x%x) failed", ESP_PARTITION_TABLE_OFFSET, ESP_PARTITION_TABLE_MAX_LEN);
  105. return false;
  106. }
  107. ESP_LOGD(TAG, "mapped partition table 0x%x at 0x%x", ESP_PARTITION_TABLE_OFFSET, (intptr_t)partitions);
  108. err = esp_partition_table_verify(partitions, true, &num_partitions);
  109. if (err != ESP_OK) {
  110. ESP_LOGE(TAG, "Failed to verify partition table");
  111. ret = false;
  112. } else {
  113. ESP_LOGI(TAG, "## Label Usage Offset Length Cleaned");
  114. for (int i = 0; i < num_partitions; i++) {
  115. const esp_partition_info_t *partition = &partitions[i];
  116. char label[sizeof(partition->label) + 1] = {0};
  117. if (partition->type == PART_TYPE_DATA) {
  118. bool fl_ota_data_erase = false;
  119. if (ota_data_erase == true && partition->subtype == PART_SUBTYPE_DATA_OTA) {
  120. fl_ota_data_erase = true;
  121. }
  122. // partition->label is not null-terminated string.
  123. strncpy(label, (char *)&partition->label, sizeof(label) - 1);
  124. if (fl_ota_data_erase == true || (bootloader_common_label_search(list_erase, label) == true)) {
  125. err = bootloader_flash_erase_range(partition->pos.offset, partition->pos.size);
  126. if (err != ESP_OK) {
  127. ret = false;
  128. marker = "err";
  129. } else {
  130. marker = "yes";
  131. }
  132. } else {
  133. marker = "no";
  134. }
  135. ESP_LOGI(TAG, "%2d %-16s data %08x %08x [%s]", i, partition->label,
  136. partition->pos.offset, partition->pos.size, marker);
  137. }
  138. }
  139. }
  140. bootloader_munmap(partitions);
  141. return ret;
  142. }
  143. esp_err_t bootloader_common_get_sha256_of_partition (uint32_t address, uint32_t size, int type, uint8_t *out_sha_256)
  144. {
  145. if (out_sha_256 == NULL || size == 0) {
  146. return ESP_ERR_INVALID_ARG;
  147. }
  148. if (type == PART_TYPE_APP) {
  149. const esp_partition_pos_t partition_pos = {
  150. .offset = address,
  151. .size = size,
  152. };
  153. esp_image_metadata_t data;
  154. // Function esp_image_verify() verifies and fills the structure data.
  155. // here important to get: image_digest, image_len, hash_appended.
  156. if (esp_image_verify(ESP_IMAGE_VERIFY_SILENT, &partition_pos, &data) != ESP_OK) {
  157. return ESP_ERR_IMAGE_INVALID;
  158. }
  159. if (data.image.hash_appended) {
  160. memcpy(out_sha_256, data.image_digest, ESP_PARTITION_HASH_LEN);
  161. return ESP_OK;
  162. }
  163. // If image doesn't have a appended hash then hash calculates for entire image.
  164. size = data.image_len;
  165. }
  166. // If image is type by data then hash is calculated for entire image.
  167. const void *partition_bin = bootloader_mmap(address, size);
  168. if (partition_bin == NULL) {
  169. ESP_LOGE(TAG, "bootloader_mmap(0x%x, 0x%x) failed", address, size);
  170. return ESP_FAIL;
  171. }
  172. bootloader_sha256_handle_t sha_handle = bootloader_sha256_start();
  173. if (sha_handle == NULL) {
  174. bootloader_munmap(partition_bin);
  175. return ESP_ERR_NO_MEM;
  176. }
  177. bootloader_sha256_data(sha_handle, partition_bin, size);
  178. bootloader_sha256_finish(sha_handle, out_sha_256);
  179. bootloader_munmap(partition_bin);
  180. return ESP_OK;
  181. }
  182. int bootloader_common_select_otadata(const esp_ota_select_entry_t *two_otadata, bool *valid_two_otadata, bool max)
  183. {
  184. if (two_otadata == NULL || valid_two_otadata == NULL) {
  185. return -1;
  186. }
  187. int active_otadata = -1;
  188. if (valid_two_otadata[0] && valid_two_otadata[1]) {
  189. int condition = (max == true) ? MAX(two_otadata[0].ota_seq, two_otadata[1].ota_seq) : MIN(two_otadata[0].ota_seq, two_otadata[1].ota_seq);
  190. if (condition == two_otadata[0].ota_seq) {
  191. active_otadata = 0;
  192. } else {
  193. active_otadata = 1;
  194. }
  195. ESP_LOGD(TAG, "Both OTA copies are valid");
  196. } else {
  197. for (int i = 0; i < 2; ++i) {
  198. if (valid_two_otadata[i]) {
  199. active_otadata = i;
  200. ESP_LOGD(TAG, "Only otadata[%d] is valid", i);
  201. break;
  202. }
  203. }
  204. }
  205. return active_otadata;
  206. }
  207. int bootloader_common_get_active_otadata(esp_ota_select_entry_t *two_otadata)
  208. {
  209. if (two_otadata == NULL) {
  210. return -1;
  211. }
  212. bool valid_two_otadata[2];
  213. valid_two_otadata[0] = bootloader_common_ota_select_valid(&two_otadata[0]);
  214. valid_two_otadata[1] = bootloader_common_ota_select_valid(&two_otadata[1]);
  215. return bootloader_common_select_otadata(two_otadata, valid_two_otadata, true);
  216. }
  217. esp_err_t bootloader_common_get_partition_description(const esp_partition_pos_t *partition, esp_app_desc_t *app_desc)
  218. {
  219. if (partition == NULL || app_desc == NULL || partition->offset == 0) {
  220. return ESP_ERR_INVALID_ARG;
  221. }
  222. const uint8_t *image = bootloader_mmap(partition->offset, partition->size);
  223. if (image == NULL) {
  224. ESP_LOGE(TAG, "bootloader_mmap(0x%x, 0x%x) failed", partition->offset, partition->size);
  225. return ESP_FAIL;
  226. }
  227. memcpy(app_desc, image + sizeof(esp_image_header_t) + sizeof(esp_image_segment_header_t), sizeof(esp_app_desc_t));
  228. bootloader_munmap(image);
  229. if (app_desc->magic_word != ESP_APP_DESC_MAGIC_WORD) {
  230. return ESP_ERR_NOT_FOUND;
  231. }
  232. return ESP_OK;
  233. }