19 esp_err_t err = esp_partition_register_external(
nullptr, ESP_PRIMARY_BOOTLOADER_OFFSET, ESP_BOOTLOADER_SIZE,
20 "PrimaryBTLDR", ESP_PARTITION_TYPE_BOOTLOADER,
21 ESP_PARTITION_SUBTYPE_BOOTLOADER_PRIMARY, &this->bootloader_part_);
23 ESP_LOGE(TAG,
"esp_partition_register_external failed (bootloader) (err=0x%X)", err);
28 esp_image_metadata_t data = {};
29 const esp_partition_pos_t part_pos = {
30 .offset = this->bootloader_part_->address,
31 .size = this->bootloader_part_->size,
33 err = esp_image_verify(ESP_IMAGE_VERIFY, &part_pos, &data);
35 ESP_LOGE(TAG,
"esp_image_verify failed (existing bootloader) (err=0x%X)", err);
68 if (this->partition_->size < this->bootloader_part_->size) {
69 ESP_LOGE(TAG,
"Staging partition too small");
76 esp_err_t err = esp_partition_erase_range(this->partition_, 0, this->bootloader_part_->size);
78 ESP_LOGW(TAG,
"esp_partition_erase_range failed (err=0x%X)", err);
81#ifdef USE_OTA_BLOCK_ERASE_AHEAD
84 this->erased_end_ = this->bootloader_part_->size;
87 err = esp_ota_set_final_partition(this->update_handle_, this->bootloader_part_,
false);
89 esp_ota_abort(this->update_handle_);
90 this->update_handle_ = 0;
91 ESP_LOGE(TAG,
"esp_ota_set_final_partition failed (err=0x%X)", err);
103 if (ota_end_err != ESP_OK) {
106#ifdef USE_OTA_SIGNED_VERIFICATION_MULTI_KEY
112 if (!this->verify_signed_image_(this->partition_)) {
113 ESP_LOGE(TAG,
"Bootloader image is not signed by a trusted key; a bootloader OTA requires an "
114 "externally-signed, 4 KiB-padded bootloader.bin");
118 esp_bootloader_desc_t bootloader_desc;
119 esp_err_t desc_err = esp_ota_get_bootloader_description(this->partition_, &bootloader_desc);
120#ifdef USE_ESP32_SRAM1_AS_IRAM
121 if (desc_err != ESP_OK) {
122 ESP_LOGE(TAG,
"New bootloader does not support SRAM1 as IRAM");
126 ESP_LOGE(TAG,
"Starting bootloader update.\n"
127 " DO NOT REMOVE POWER until the update completes successfully.\n"
128 " Loss of power during this operation may render the device\n"
129 " unable to boot until it is recovered via a serial flash.");
130 esp_err_t err = esp_partition_copy(this->bootloader_part_, 0, this->partition_, 0, this->bootloader_part_->size);
132 ESP_LOGE(TAG,
"esp_partition_copy failed (err=0x%X)", err);
137 "Successfully installed the new bootloader\n"
139 (desc_err == ESP_OK) ? bootloader_desc.idf_ver :
"version unknown");
141 err = esp_partition_erase_range(this->partition_, 0, this->partition_->erase_size);
143 ESP_LOGW(TAG,
"esp_partition_erase_range failed (err=0x%X)", err);
146 esp_partition_deregister_external(this->bootloader_part_);
147 this->bootloader_part_ =
nullptr;