bootloader_common.c 11 KB

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