18 esp_err_t err = esp_partition_register_external(
nullptr, ESP_PRIMARY_BOOTLOADER_OFFSET, ESP_BOOTLOADER_SIZE,
19 "PrimaryBTLDR", ESP_PARTITION_TYPE_BOOTLOADER,
20 ESP_PARTITION_SUBTYPE_BOOTLOADER_PRIMARY, &this->bootloader_part_);
22 ESP_LOGE(TAG,
"esp_partition_register_external failed (bootloader) (err=0x%X)", err);
27 esp_image_metadata_t data = {};
28 const esp_partition_pos_t part_pos = {
29 .offset = this->bootloader_part_->address,
30 .size = this->bootloader_part_->size,
32 err = esp_image_verify(ESP_IMAGE_VERIFY, &part_pos, &data);
34 ESP_LOGE(TAG,
"esp_image_verify failed (existing bootloader) (err=0x%X)", err);
67 if (this->partition_->size < this->bootloader_part_->size) {
68 ESP_LOGE(TAG,
"Staging partition too small");
73 esp_err_t err = esp_partition_erase_range(this->partition_, 0, this->bootloader_part_->size);
75 ESP_LOGW(TAG,
"esp_partition_erase_range failed (err=0x%X)", err);
78 err = esp_ota_set_final_partition(this->update_handle_, this->bootloader_part_,
false);
80 esp_ota_abort(this->update_handle_);
81 this->update_handle_ = 0;
82 ESP_LOGE(TAG,
"esp_ota_set_final_partition failed (err=0x%X)", err);
94 if (ota_end_err != ESP_OK) {
97#ifdef USE_OTA_SIGNED_VERIFICATION_MULTI_KEY
103 if (!this->verify_signed_image_(this->partition_)) {
104 ESP_LOGE(TAG,
"Bootloader image is not signed by a trusted key; a bootloader OTA requires an "
105 "externally-signed, 4 KiB-padded bootloader.bin");
109 esp_bootloader_desc_t bootloader_desc;
110 esp_err_t desc_err = esp_ota_get_bootloader_description(this->partition_, &bootloader_desc);
111#ifdef USE_ESP32_SRAM1_AS_IRAM
112 if (desc_err != ESP_OK) {
113 ESP_LOGE(TAG,
"New bootloader does not support SRAM1 as IRAM");
117 ESP_LOGE(TAG,
"Starting bootloader update.\n"
118 " DO NOT REMOVE POWER until the update completes successfully.\n"
119 " Loss of power during this operation may render the device\n"
120 " unable to boot until it is recovered via a serial flash.");
121 esp_err_t err = esp_partition_copy(this->bootloader_part_, 0, this->partition_, 0, this->bootloader_part_->size);
123 ESP_LOGE(TAG,
"esp_partition_copy failed (err=0x%X)", err);
128 "Successfully installed the new bootloader\n"
130 (desc_err == ESP_OK) ? bootloader_desc.idf_ver :
"version unknown");
132 err = esp_partition_erase_range(this->partition_, 0, this->partition_->erase_size);
134 ESP_LOGW(TAG,
"esp_partition_erase_range failed (err=0x%X)", err);
137 esp_partition_deregister_external(this->bootloader_part_);
138 this->bootloader_part_ =
nullptr;