bootloader_common.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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 "esp32/rom/crc.h"
  21. #include "esp32/rom/gpio.h"
  22. #include "esp_secure_boot.h"
  23. #include "esp_flash_partitions.h"
  24. #include "bootloader_flash.h"
  25. #include "bootloader_common.h"
  26. #include "bootloader_utility.h"
  27. #include "soc/gpio_periph.h"
  28. #include "soc/rtc.h"
  29. #include "soc/efuse_reg.h"
  30. #include "soc/apb_ctrl_reg.h"
  31. #include "esp_image_format.h"
  32. #include "bootloader_sha.h"
  33. #include "sys/param.h"
  34. #define ESP_PARTITION_HASH_LEN 32 /* SHA-256 digest length */
  35. static const char* TAG = "boot_comm";
  36. uint32_t bootloader_common_ota_select_crc(const esp_ota_select_entry_t *s)
  37. {
  38. return crc32_le(UINT32_MAX, (uint8_t*)&s->ota_seq, 4);
  39. }
  40. bool bootloader_common_ota_select_invalid(const esp_ota_select_entry_t *s)
  41. {
  42. return s->ota_seq == UINT32_MAX || s->ota_state == ESP_OTA_IMG_INVALID || s->ota_state == ESP_OTA_IMG_ABORTED;
  43. }
  44. bool bootloader_common_ota_select_valid(const esp_ota_select_entry_t *s)
  45. {
  46. return bootloader_common_ota_select_invalid(s) == false && s->crc == bootloader_common_ota_select_crc(s);
  47. }
  48. esp_comm_gpio_hold_t bootloader_common_check_long_hold_gpio(uint32_t num_pin, uint32_t delay_sec)
  49. {
  50. gpio_pad_select_gpio(num_pin);
  51. if (GPIO_PIN_MUX_REG[num_pin]) {
  52. PIN_INPUT_ENABLE(GPIO_PIN_MUX_REG[num_pin]);
  53. }
  54. gpio_pad_pullup(num_pin);
  55. uint32_t tm_start = esp_log_early_timestamp();
  56. if (GPIO_INPUT_GET(num_pin) == 1) {
  57. return GPIO_NOT_HOLD;
  58. }
  59. do {
  60. if (GPIO_INPUT_GET(num_pin) != 0) {
  61. return GPIO_SHORT_HOLD;
  62. }
  63. } while (delay_sec > ((esp_log_early_timestamp() - tm_start) / 1000L));
  64. return GPIO_LONG_HOLD;
  65. }
  66. // Search for a label in the list. list = "nvs1, nvs2, otadata, nvs"; label = "nvs".
  67. bool bootloader_common_label_search(const char *list, char *label)
  68. {
  69. if (list == NULL || label == NULL) {
  70. return false;
  71. }
  72. const char *sub_list_start_like_label = strstr(list, label);
  73. while (sub_list_start_like_label != NULL) {
  74. // ["," or " "] + label + ["," or " " or "\0"]
  75. // first character before the label found there must be a delimiter ["," or " "].
  76. int idx_first = sub_list_start_like_label - list;
  77. if (idx_first == 0 || (idx_first != 0 && (list[idx_first - 1] == ',' || list[idx_first - 1] == ' '))) {
  78. // next character after the label found there must be a delimiter ["," or " " or "\0"].
  79. int len_label = strlen(label);
  80. if (sub_list_start_like_label[len_label] == 0 ||
  81. sub_list_start_like_label[len_label] == ',' ||
  82. sub_list_start_like_label[len_label] == ' ') {
  83. return true;
  84. }
  85. }
  86. // [start_delim] + label + [end_delim] was not found.
  87. // Position is moving to next delimiter if it is not the end of list.
  88. int pos_delim = strcspn(sub_list_start_like_label, ", ");
  89. if (pos_delim == strlen(sub_list_start_like_label)) {
  90. break;
  91. }
  92. sub_list_start_like_label = strstr(&sub_list_start_like_label[pos_delim], label);
  93. }
  94. return false;
  95. }
  96. bool bootloader_common_erase_part_type_data(const char *list_erase, bool ota_data_erase)
  97. {
  98. const esp_partition_info_t *partitions;
  99. const char *marker;
  100. esp_err_t err;
  101. int num_partitions;
  102. bool ret = true;
  103. partitions = bootloader_mmap(ESP_PARTITION_TABLE_OFFSET, ESP_PARTITION_TABLE_MAX_LEN);
  104. if (!partitions) {
  105. ESP_LOGE(TAG, "bootloader_mmap(0x%x, 0x%x) failed", ESP_PARTITION_TABLE_OFFSET, ESP_PARTITION_TABLE_MAX_LEN);
  106. return false;
  107. }
  108. ESP_LOGD(TAG, "mapped partition table 0x%x at 0x%x", ESP_PARTITION_TABLE_OFFSET, (intptr_t)partitions);
  109. err = esp_partition_table_verify(partitions, true, &num_partitions);
  110. if (err != ESP_OK) {
  111. ESP_LOGE(TAG, "Failed to verify partition table");
  112. ret = false;
  113. } else {
  114. ESP_LOGI(TAG, "## Label Usage Offset Length Cleaned");
  115. for (int i = 0; i < num_partitions; i++) {
  116. const esp_partition_info_t *partition = &partitions[i];
  117. char label[sizeof(partition->label) + 1] = {0};
  118. if (partition->type == PART_TYPE_DATA) {
  119. bool fl_ota_data_erase = false;
  120. if (ota_data_erase == true && partition->subtype == PART_SUBTYPE_DATA_OTA) {
  121. fl_ota_data_erase = true;
  122. }
  123. // partition->label is not null-terminated string.
  124. strncpy(label, (char *)&partition->label, sizeof(label) - 1);
  125. if (fl_ota_data_erase == true || (bootloader_common_label_search(list_erase, label) == true)) {
  126. err = bootloader_flash_erase_range(partition->pos.offset, partition->pos.size);
  127. if (err != ESP_OK) {
  128. ret = false;
  129. marker = "err";
  130. } else {
  131. marker = "yes";
  132. }
  133. } else {
  134. marker = "no";
  135. }
  136. ESP_LOGI(TAG, "%2d %-16s data %08x %08x [%s]", i, partition->label,
  137. partition->pos.offset, partition->pos.size, marker);
  138. }
  139. }
  140. }
  141. bootloader_munmap(partitions);
  142. return ret;
  143. }
  144. esp_err_t bootloader_common_get_sha256_of_partition (uint32_t address, uint32_t size, int type, uint8_t *out_sha_256)
  145. {
  146. if (out_sha_256 == NULL || size == 0) {
  147. return ESP_ERR_INVALID_ARG;
  148. }
  149. if (type == PART_TYPE_APP) {
  150. const esp_partition_pos_t partition_pos = {
  151. .offset = address,
  152. .size = size,
  153. };
  154. esp_image_metadata_t data;
  155. // Function esp_image_verify() verifies and fills the structure data.
  156. // here important to get: image_digest, image_len, hash_appended.
  157. if (esp_image_verify(ESP_IMAGE_VERIFY_SILENT, &partition_pos, &data) != ESP_OK) {
  158. return ESP_ERR_IMAGE_INVALID;
  159. }
  160. if (data.image.hash_appended) {
  161. memcpy(out_sha_256, data.image_digest, ESP_PARTITION_HASH_LEN);
  162. return ESP_OK;
  163. }
  164. // If image doesn't have a appended hash then hash calculates for entire image.
  165. size = data.image_len;
  166. }
  167. // If image is type by data then hash is calculated for entire image.
  168. return bootloader_sha256_flash_contents(address, size, out_sha_256);
  169. }
  170. int bootloader_common_select_otadata(const esp_ota_select_entry_t *two_otadata, bool *valid_two_otadata, bool max)
  171. {
  172. if (two_otadata == NULL || valid_two_otadata == NULL) {
  173. return -1;
  174. }
  175. int active_otadata = -1;
  176. if (valid_two_otadata[0] && valid_two_otadata[1]) {
  177. 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);
  178. if (condition == two_otadata[0].ota_seq) {
  179. active_otadata = 0;
  180. } else {
  181. active_otadata = 1;
  182. }
  183. ESP_LOGD(TAG, "Both OTA copies are valid");
  184. } else {
  185. for (int i = 0; i < 2; ++i) {
  186. if (valid_two_otadata[i]) {
  187. active_otadata = i;
  188. ESP_LOGD(TAG, "Only otadata[%d] is valid", i);
  189. break;
  190. }
  191. }
  192. }
  193. return active_otadata;
  194. }
  195. int bootloader_common_get_active_otadata(esp_ota_select_entry_t *two_otadata)
  196. {
  197. if (two_otadata == NULL) {
  198. return -1;
  199. }
  200. bool valid_two_otadata[2];
  201. valid_two_otadata[0] = bootloader_common_ota_select_valid(&two_otadata[0]);
  202. valid_two_otadata[1] = bootloader_common_ota_select_valid(&two_otadata[1]);
  203. return bootloader_common_select_otadata(two_otadata, valid_two_otadata, true);
  204. }
  205. esp_err_t bootloader_common_get_partition_description(const esp_partition_pos_t *partition, esp_app_desc_t *app_desc)
  206. {
  207. if (partition == NULL || app_desc == NULL || partition->offset == 0) {
  208. return ESP_ERR_INVALID_ARG;
  209. }
  210. const uint32_t app_desc_offset = sizeof(esp_image_header_t) + sizeof(esp_image_segment_header_t);
  211. const uint32_t mmap_size = app_desc_offset + sizeof(esp_app_desc_t);
  212. const uint8_t *image = bootloader_mmap(partition->offset, mmap_size);
  213. if (image == NULL) {
  214. ESP_LOGE(TAG, "bootloader_mmap(0x%x, 0x%x) failed", partition->offset, mmap_size);
  215. return ESP_FAIL;
  216. }
  217. memcpy(app_desc, image + app_desc_offset, sizeof(esp_app_desc_t));
  218. bootloader_munmap(image);
  219. if (app_desc->magic_word != ESP_APP_DESC_MAGIC_WORD) {
  220. return ESP_ERR_NOT_FOUND;
  221. }
  222. return ESP_OK;
  223. }
  224. void bootloader_common_vddsdio_configure()
  225. {
  226. #if CONFIG_BOOTLOADER_VDDSDIO_BOOST_1_9V
  227. rtc_vddsdio_config_t cfg = rtc_vddsdio_get_config();
  228. if (cfg.enable == 1 && cfg.tieh == RTC_VDDSDIO_TIEH_1_8V) { // VDDSDIO regulator is enabled @ 1.8V
  229. cfg.drefh = 3;
  230. cfg.drefm = 3;
  231. cfg.drefl = 3;
  232. cfg.force = 1;
  233. rtc_vddsdio_set_config(cfg);
  234. ets_delay_us(10); // wait for regulator to become stable
  235. }
  236. #endif // CONFIG_BOOTLOADER_VDDSDIO_BOOST
  237. }
  238. #ifdef CONFIG_IDF_TARGET_ESP32
  239. uint8_t bootloader_common_get_chip_revision(void)
  240. {
  241. uint8_t eco_bit0, eco_bit1, eco_bit2;
  242. eco_bit0 = (REG_READ(EFUSE_BLK0_RDATA3_REG) & 0xF000) >> 15;
  243. eco_bit1 = (REG_READ(EFUSE_BLK0_RDATA5_REG) & 0x100000) >> 20;
  244. eco_bit2 = (REG_READ(APB_CTRL_DATE_REG) & 0x80000000) >> 31;
  245. uint32_t combine_value = (eco_bit2 << 2) | (eco_bit1 << 1) | eco_bit0;
  246. uint8_t chip_ver = 0;
  247. switch (combine_value) {
  248. case 0:
  249. chip_ver = 0;
  250. break;
  251. case 1:
  252. chip_ver = 1;
  253. break;
  254. case 3:
  255. chip_ver = 2;
  256. break;
  257. case 7:
  258. chip_ver = 3;
  259. break;
  260. default:
  261. chip_ver = 0;
  262. break;
  263. }
  264. return chip_ver;
  265. }
  266. #endif
  267. esp_err_t bootloader_common_check_chip_validity(const esp_image_header_t* img_hdr, esp_image_type type)
  268. {
  269. esp_err_t err = ESP_OK;
  270. esp_chip_id_t chip_id = CONFIG_IDF_FIRMWARE_CHIP_ID;
  271. if (chip_id != img_hdr->chip_id) {
  272. ESP_LOGE(TAG, "mismatch chip ID, expected %d, found %d", chip_id, img_hdr->chip_id);
  273. err = ESP_FAIL;
  274. }
  275. uint8_t revision = bootloader_common_get_chip_revision();
  276. if (revision < img_hdr->min_chip_rev) {
  277. ESP_LOGE(TAG, "can't run on lower chip revision, expected %d, found %d", revision, img_hdr->min_chip_rev);
  278. err = ESP_FAIL;
  279. } else if (revision != img_hdr->min_chip_rev) {
  280. ESP_LOGI(TAG, "chip revision: %d, min. %s chip revision: %d", revision, type == ESP_IMAGE_BOOTLOADER ? "bootloader" : "application", img_hdr->min_chip_rev);
  281. }
  282. return err;
  283. }