9#ifdef IOHOME_LR1121_FIRMWARE_UPDATE
13#include "lr1121_firmware_update_image.h"
15#ifdef IOHOME_LR1121_BOOTLOADER_UPDATE
18#include "lr1121_bootloader_loader_image.h"
21#include "esphome/core/application.h"
56constexpr uint32_t LR1121_FLASH_CONFIRM_WINDOW_MS = 60 * 1000;
58std::string format_lr1121_fw_version(uint16_t version) {
62 snprintf(buf,
sizeof(buf),
"%u.%u",
static_cast<unsigned>(version >> 8),
static_cast<unsigned>(version & 0xFF));
66std::string format_hex16(uint16_t value) {
68 snprintf(buf,
sizeof(buf),
"0x%04X", value);
72std::string format_hex8(uint8_t value) {
74 snprintf(buf,
sizeof(buf),
"0x%02X", value);
80std::string format_lr1121_bootloader_version(uint16_t version) {
81 return version == 0 ?
"unknown" : format_hex16(version);
86enum class Lr1121SanityResult {
104Lr1121SanityResult lr1121_check_bootloader_sanity(
bool known, uint16_t known_bootloader_version, uint8_t sanity_type,
105 uint16_t sanity_bootloader_version) {
106 if (sanity_type != LR1121_UPDATER_BOOTLOADER_TYPE)
107 return Lr1121SanityResult::WRONG_TYPE;
109 if (sanity_bootloader_version != known_bootloader_version)
110 return Lr1121SanityResult::VERSION_MISMATCH;
111 return Lr1121SanityResult::OK;
114 return Lr1121SanityResult::WRONG_CHIP_FAMILY;
115 return Lr1121SanityResult::OK;
121std::string lr1121_sanity_failure_reason(Lr1121SanityResult sanity, uint16_t sanity_bootloader_version) {
123 case Lr1121SanityResult::WRONG_TYPE:
125 case Lr1121SanityResult::WRONG_CHIP_FAMILY:
126 return "bootloader version " + format_hex16(sanity_bootloader_version) +
" identifies " +
128 case Lr1121SanityResult::VERSION_MISMATCH:
130 return "bootloader version changed since boot";
138void lr1121_log_post_flash_verify_result(uint16_t new_fw, uint16_t target_fw) {
139 if (target_fw == 0) {
141 "LR1121 firmware update: now running %s; this build had no expected version to compare against",
142 format_lr1121_fw_version(new_fw).c_str());
143 }
else if (new_fw == target_fw) {
144 ESP_LOGI(
detail::TAG,
"LR1121 firmware update: success -- now running %s",
145 format_lr1121_fw_version(new_fw).c_str());
147 ESP_LOGW(
detail::TAG,
"LR1121 firmware update: post-flash version is %s, expected %s",
148 format_lr1121_fw_version(new_fw).c_str(), format_lr1121_fw_version(target_fw).c_str());
183void lr1121_log_post_write_hash(Lr1121FirmwareUpdater &updater) {
184 uint8_t hash[LR1121_UPDATER_HASH_LENGTH] = {0};
185 if (!updater.read_hash(hash,
sizeof(hash))) {
187 "LR1121 firmware update: could not read the flash fingerprint (BUSY timeout). Harmless -- it is "
188 "only an identifier, not a correctness check; the version check below is what confirms the flash.");
197 const bool degenerate = std::all_of(hash + 1, hash + LR1121_UPDATER_HASH_LENGTH, [](uint8_t b) {
return b == 0; });
200 "LR1121 firmware update: no flash fingerprint available on this bootloader (GetHash returned a "
201 "fixed non-value). Harmless -- it was only ever an identifier, never a correctness check; the "
202 "firmware version reported below is what confirms the flash worked.");
205 char hex[LR1121_UPDATER_HASH_LENGTH * 2 + 1];
206 for (
size_t i = 0; i < LR1121_UPDATER_HASH_LENGTH; i++)
207 snprintf(hex + i * 2, 3,
"%02x", hash[i]);
209 "LR1121 firmware update: flash fingerprint %s -- an identifier for the image now on the chip, useful "
210 "for comparing two boards. It is not the image's MD5 and cannot be checked against anything; the "
211 "firmware version reported below is what confirms the flash worked.",
222bool lr1121_erase_and_write_image_(Lr1121FirmwareUpdater &updater,
const char *stage_label,
const uint32_t *image,
223 size_t word_count, uint32_t &erase_elapsed_ms, uint32_t &write_elapsed_ms) {
224 ESP_LOGI(
detail::TAG,
"%s: erasing radio flash, this takes a few seconds...", stage_label);
225 const uint32_t erase_start_ms = millis();
226 if (!updater.erase_flash()) {
227 ESP_LOGE(
detail::TAG,
"%s: erase failed (BUSY timeout)", stage_label);
230 erase_elapsed_ms = millis() - erase_start_ms;
232 size_t last_logged_words = 0;
233 const size_t log_step = std::max<size_t>(word_count / 10, 1);
234 const uint32_t write_start_ms = millis();
235 const bool write_ok = updater.write_image(image, word_count, [&](
size_t done,
size_t total) {
239 if (done - last_logged_words < log_step && done != total)
241 last_logged_words = done;
242 ESP_LOGI(
detail::TAG,
"%s: flashing %zu/%zu words (%u%%)", stage_label, done, total,
243 static_cast<unsigned>((done * 100) / total));
245 write_elapsed_ms = millis() - write_start_ms;
247 ESP_LOGE(
detail::TAG,
"%s: write failed (BUSY timeout) after %" PRIu32
" ms", stage_label, write_elapsed_ms);
250 ESP_LOGI(
detail::TAG,
"%s: erase took %" PRIu32
" ms, write took %" PRIu32
" ms", stage_label, erase_elapsed_ms,
266std::string lr1121_needs_confirmation_reason(uint8_t device_type, uint16_t bootloader_version, uint16_t installed_fw,
267 uint16_t target_fw) {
268 if (bootloader_version == 0)
269 return "the bootloader version could not be read at boot, so chip identity and bootloader compatibility "
270 "cannot be verified";
271 if (device_type == 0)
272 return "the installed firmware version could not be read (radio failed to initialize, or the read itself "
275 return "no target firmware version could be determined for the configured image";
277 return "target firmware " + format_lr1121_fw_version(target_fw) +
278 " is not in this build's known bootloader-compatibility table (unverified, not refused)";
280 if (installed_fw == 0)
281 return "the installed firmware version is unknown";
282 return "target firmware " + format_lr1121_fw_version(target_fw) +
" is not newer than the installed " +
283 format_lr1121_fw_version(installed_fw);
288Lr1121FirmwareUpdateController::Lr1121FirmwareUpdateController(
RadioDriver **radio,
SpiAccess *spi,
289 InternalGPIOPin **rst_pin, InternalGPIOPin **busy_pin,
299 begin_blocking_excursion_(std::move(begin_blocking_excursion)),
302void Lr1121FirmwareUpdateController::run_boot_time_bootloader_read() {
303 this->lr1121_firmware_updater_ =
304 new (std::nothrow) Lr1121FirmwareUpdater(this->spi_, *this->rst_pin_, *this->busy_pin_);
305 if (this->lr1121_firmware_updater_ ==
nullptr) {
306 ESP_LOGE(detail::TAG,
"LR1121 firmware update: failed to allocate the updater; bootloader version unknown");
311 uint16_t bootloader_version = 0;
312 if (!this->lr1121_firmware_updater_->enter_bootloader(type, bootloader_version)) {
313 ESP_LOGW(detail::TAG,
"LR1121 firmware update: could not read the bootloader version at boot (BUSY timeout) -- "
314 "bootloader version stays unknown until the next boot");
317 this->lr1121_bootloader_chip_type_ = type;
318 this->lr1121_bootloader_version_ = bootloader_version;
319 this->lr1121_bootloader_version_known_ =
true;
325 if (!this->lr1121_firmware_updater_->reboot(
false)) {
326 ESP_LOGW(detail::TAG,
"LR1121 firmware update: reboot-out-of-bootloader command failed to send (BUSY timeout); the "
327 "upcoming radio init's own hardware reset will recover it");
331void Lr1121FirmwareUpdateController::cache_flash_verdict() {
332 uint8_t device_type = 0;
333 uint16_t installed_fw = 0;
334 if (*this->radio_ !=
nullptr && this->lr1121_firmware_updater_ !=
nullptr) {
339 uint8_t fw_major = 0, fw_minor = 0;
342 if (this->lr1121_firmware_updater_->read_normal_version(device_type, fw_major, fw_minor))
343 installed_fw = (
static_cast<uint16_t
>(fw_major) << 8) | fw_minor;
345 this->lr1121_installed_device_type_ = device_type;
346 this->lr1121_installed_fw_ = installed_fw;
350 this->lr1121_flash_verdict_ =
351 lr1121_flash_decision(device_type, this->lr1121_bootloader_chip_type_, this->lr1121_bootloader_version_,
352 installed_fw, LR1121_FIRMWARE_UPDATE_TARGET_VERSION,
false);
353 this->lr1121_flash_verdict_known_ =
true;
360std::string Lr1121FirmwareUpdateController::describe_flash_verdict()
const {
361 const uint16_t target = LR1121_FIRMWARE_UPDATE_TARGET_VERSION;
362 const std::string prefix =
"Firmware update target: " + format_lr1121_fw_version(target) +
" -- ";
364 switch (this->lr1121_flash_verdict_) {
365 case FlashDecision::REJECT_WRONG_CHIP: {
369 if (this->lr1121_bootloader_chip_type_ != LR1121_BOOTLOADER_TYPE_FOR_FIRMWARE_DECISIONS ||
371 return prefix +
"CANNOT PROCEED: bootloader version " + format_hex16(this->lr1121_bootloader_version_) +
374 return prefix +
"CANNOT PROCEED: normal-mode chip identity byte " +
375 format_hex8(this->lr1121_installed_device_type_) +
" identifies " +
378 case FlashDecision::REJECT_BOOTLOADER_TOO_OLD: {
385 BootloaderMismatch::TARGET_NEEDS_OLDER) {
386 return prefix +
"CANNOT PROCEED: this chip's bootloader " + format_hex16(this->lr1121_bootloader_version_) +
387 " is newer than this firmware supports (needs " + format_hex16(required) +
388 ") -- there is no downgrade path";
390 std::string message = prefix +
"CANNOT PROCEED: needs bootloader " + format_hex16(required) +
", this chip has " +
391 format_hex16(this->lr1121_bootloader_version_);
392#ifndef IOHOME_LR1121_BOOTLOADER_UPDATE
398 message +=
" -- add a bootloader: sub-block to lr1121_firmware_update: to enable the (irreversible) upgrade "
403 case FlashDecision::ALREADY_INSTALLED:
404 return prefix +
"already running the configured firmware, nothing to do";
405 case FlashDecision::NEEDS_CONFIRMATION:
407 lr1121_needs_confirmation_reason(this->lr1121_installed_device_type_, this->lr1121_bootloader_version_,
408 this->lr1121_installed_fw_, target) +
409 " (bootloader version " + format_lr1121_bootloader_version(this->lr1121_bootloader_version_) +
")";
410 case FlashDecision::PROCEED:
412 return prefix +
"ready to flash (press \"Flash LR1121 Radio Firmware\")";
420#ifdef IOHOME_LR1121_BOOTLOADER_UPDATE
421std::string Lr1121FirmwareUpdateController::describe_bootloader_refusal(BootloaderUpgradePath path)
const {
427 const std::string target_text = format_lr1121_fw_version(LR1121_FIRMWARE_UPDATE_TARGET_VERSION);
428 const std::string chip_text = format_hex16(this->lr1121_bootloader_version_);
430 const std::string prefix =
"LR1121 firmware update: nothing was done, the radio was not touched. ";
433 case BootloaderUpgradePath::AVAILABLE:
434 return prefix +
"Firmware " + target_text +
" needs bootloader " + required_text +
" and this chip has " +
436 ", so the bootloader has to be rewritten first. To do that, turn on the \"Allow LR1121 Bootloader "
437 "Rewrite (Irreversible)\" switch and press this button again. A bootloader rewrite cannot be undone.";
438 case BootloaderUpgradePath::BLOCKED_UNKNOWN_TARGET:
439 return prefix +
"This build does not recognise firmware " + target_text +
440 ", so it cannot tell which bootloader that image needs. The bootloader rewrite stays disabled rather "
441 "than risk an irreversible write on a guess.";
442 case BootloaderUpgradePath::BLOCKED_BOOTLOADER_NEWER:
443 return prefix +
"This chip's bootloader " + chip_text +
" is already newer than firmware " + target_text +
444 " supports (that image needs " + required_text +
"), and there is no way back to an older bootloader.";
445 case BootloaderUpgradePath::NOT_APPLICABLE:
449 return this->describe_flash_verdict();
454std::vector<std::string> Lr1121FirmwareUpdateController::debug_lines()
const {
455 std::vector<std::string> lines;
456 if (this->lr1121_bootloader_version_known_) {
457 lines.push_back(
"LR1121 bootloader version: " + format_hex16(this->lr1121_bootloader_version_));
459 lines.push_back(
"LR1121 firmware update: bootloader version could not be read at boot");
461 if (this->lr1121_flash_verdict_known_)
462 lines.push_back(this->describe_flash_verdict());
463#ifdef IOHOME_LR1121_BOOTLOADER_UPDATE
468 true, this->lr1121_bootloader_version_known_, this->lr1121_bootloader_version_,
469 LR1121_BOOTLOADER_LOADER_FW, LR1121_FIRMWARE_UPDATE_TARGET_VERSION);
470 if (upgrade_path == BootloaderUpgradePath::AVAILABLE) {
475 lines.push_back(
"LR1121 bootloader rewrite: AVAILABLE -- needs bootloader " +
477 ", this chip has " + format_hex16(this->lr1121_bootloader_version_) +
478 ". A bootloader rewrite cannot be undone.");
479 }
else if (upgrade_path == BootloaderUpgradePath::BLOCKED_UNKNOWN_TARGET) {
480 lines.push_back(
"LR1121 bootloader rewrite: configured, but inert -- this build does not know what bootloader the "
481 "configured target requires, so it will not gamble an irreversible write on it.");
482 }
else if (upgrade_path == BootloaderUpgradePath::BLOCKED_BOOTLOADER_NEWER) {
483 lines.push_back(
"LR1121 bootloader rewrite: configured, but inert -- this chip's bootloader is already newer than "
484 "the configured target needs; there is no downgrade path.");
490void Lr1121FirmwareUpdateController::dump_debug()
const {
491 for (
const auto &line : this->debug_lines())
492 ESP_LOGCONFIG(detail::TAG,
" %s", line.c_str());
495void Lr1121FirmwareUpdateController::arm_flash_confirmation_() {
496 this->lr1121_flash_confirmation_armed_ =
true;
507 App.scheduler.set_timeout(
static_cast<const void *
>(this->hub_), LR1121_FLASH_CONFIRM_WINDOW_MS, [
this]() {
510 if (!this->lr1121_flash_confirmation_armed_)
512 this->lr1121_flash_confirmation_armed_ =
false;
513 ESP_LOGI(detail::TAG,
"LR1121 firmware update: confirmation window expired without a second press");
517void Lr1121FirmwareUpdateController::trigger() {
522 if (*this->radio_ ==
nullptr)
523 ESP_LOGW(detail::TAG,
"LR1121 firmware update: radio_ is null (failed init); proceeding without standby");
530 ESP_LOGW(detail::TAG,
"LR1121 firmware update: radio busy with another operation, ignoring press");
534 if (this->lr1121_firmware_updater_ ==
nullptr || !this->lr1121_flash_verdict_known_) {
535 ESP_LOGE(detail::TAG,
"LR1121 firmware update: no cached verdict available (setup() may have failed early)");
544 if (verdict == FlashDecision::REJECT_WRONG_CHIP) {
545 ESP_LOGE(detail::TAG,
"%s", this->describe_flash_verdict().c_str());
549 if (verdict == FlashDecision::REJECT_BOOTLOADER_TOO_OLD) {
550#ifdef IOHOME_LR1121_BOOTLOADER_UPDATE
555 true, this->lr1121_bootloader_version_known_, this->lr1121_bootloader_version_,
556 LR1121_BOOTLOADER_LOADER_FW, LR1121_FIRMWARE_UPDATE_TARGET_VERSION);
557 if (upgrade_path == BootloaderUpgradePath::AVAILABLE) {
558 if (!this->bootloader_rewrite_allowed_) {
559 ESP_LOGE(detail::TAG,
"%s", this->describe_bootloader_refusal(upgrade_path).c_str());
565 ESP_LOGW(detail::TAG,
"LR1121 bootloader rewrite: arming switch is on -- running the three-stage sequence now.");
566 this->run_bootloader_upgrade_sequence_();
569 if (upgrade_path == BootloaderUpgradePath::BLOCKED_UNKNOWN_TARGET ||
570 upgrade_path == BootloaderUpgradePath::BLOCKED_BOOTLOADER_NEWER) {
571 ESP_LOGE(detail::TAG,
"%s", this->describe_bootloader_refusal(upgrade_path).c_str());
578 ESP_LOGE(detail::TAG,
"%s", this->describe_flash_verdict().c_str());
582 const bool proceeding = (verdict == FlashDecision::PROCEED) || this->lr1121_flash_confirmation_armed_;
589 const bool already_installed = (verdict == FlashDecision::ALREADY_INSTALLED);
590 const std::string confirm_suffix =
" -- press \"Flash LR1121 Radio Firmware\" again within " +
591 std::to_string(LR1121_FLASH_CONFIRM_WINDOW_MS / 1000) +
"s to " +
592 (already_installed ?
"re-flash anyway" :
"proceed anyway");
593 const std::string message = this->describe_flash_verdict() + confirm_suffix;
594 if (already_installed) {
595 ESP_LOGI(detail::TAG,
"%s", message.c_str());
597 ESP_LOGW(detail::TAG,
"%s", message.c_str());
599 this->arm_flash_confirmation_();
603 this->lr1121_flash_confirmation_armed_ =
false;
604 this->run_flash_sequence_();
607void Lr1121FirmwareUpdateController::run_flash_sequence_() {
615 this->begin_blocking_excursion_();
616 if (*this->radio_ !=
nullptr)
617 (*this->radio_)->set_mode_standby();
622 uint8_t sanity_type = 0;
623 uint16_t sanity_bootloader_version = 0;
624 if (!this->lr1121_firmware_updater_->enter_bootloader(sanity_type, sanity_bootloader_version)) {
625 ESP_LOGE(detail::TAG,
626 "LR1121 firmware update: bootloader entry could not be confirmed (BUSY timeout on the verification "
627 "read) -- the entry sequence itself already ran, so the chip may be unconfigured; rebooting to "
628 "recover it rather than risking a silently dead radio");
633 const Lr1121SanityResult sanity = lr1121_check_bootloader_sanity(
634 this->lr1121_bootloader_version_known_, this->lr1121_bootloader_version_, sanity_type, sanity_bootloader_version);
635 if (sanity != Lr1121SanityResult::OK) {
636 const std::string sanity_reason = lr1121_sanity_failure_reason(sanity, sanity_bootloader_version);
639 "LR1121 firmware update: bootloader-entry sanity check failed (%s; read type=0x%02X bootloader=%s, "
640 "boot-time bootloader was %s) -- aborting before erasing anything",
641 sanity_reason.c_str(), sanity_type, format_hex16(sanity_bootloader_version).c_str(),
642 this->lr1121_bootloader_version_known_ ? format_hex16(this->lr1121_bootloader_version_).c_str() :
"unknown");
643 this->lr1121_firmware_updater_->reboot(
false);
647 if (!this->lr1121_bootloader_version_known_) {
652 ESP_LOGI(detail::TAG,
653 "LR1121 firmware update: boot-time bootloader version was unknown; type check passed and bootloader "
655 format_hex16(sanity_bootloader_version).c_str());
656 this->lr1121_bootloader_chip_type_ = sanity_type;
657 this->lr1121_bootloader_version_ = sanity_bootloader_version;
658 this->lr1121_bootloader_version_known_ =
true;
661 uint32_t erase_elapsed_ms = 0, write_elapsed_ms = 0;
662 if (!lr1121_erase_and_write_image_(*this->lr1121_firmware_updater_,
"LR1121 firmware update",
663 LR1121_FIRMWARE_UPDATE_IMAGE, LR1121_FIRMWARE_UPDATE_IMAGE_WORDS, erase_elapsed_ms,
665 ESP_LOGE(detail::TAG,
666 "LR1121 firmware update: the radio firmware is now incomplete. This is recoverable: after this "
667 "reboot, press the button again to re-flash.");
674 lr1121_log_post_write_hash(*this->lr1121_firmware_updater_);
676 if (!this->lr1121_firmware_updater_->reboot(
false)) {
677 ESP_LOGW(detail::TAG,
"LR1121 firmware update: reboot-to-image command failed to send (BUSY timeout)");
679 uint8_t device_type = 0, fw_major = 0, fw_minor = 0;
680 if (this->lr1121_firmware_updater_->read_normal_version(device_type, fw_major, fw_minor)) {
681 const uint16_t new_fw = (
static_cast<uint16_t
>(fw_major) << 8) | fw_minor;
682 lr1121_log_post_flash_verify_result(new_fw, LR1121_FIRMWARE_UPDATE_TARGET_VERSION);
684 ESP_LOGW(detail::TAG,
"LR1121 firmware update: could not read back the post-flash version (BUSY timeout)");
694#ifdef IOHOME_LR1121_BOOTLOADER_UPDATE
696void Lr1121FirmwareUpdateController::run_bootloader_upgrade_sequence_() {
698 this->begin_blocking_excursion_();
699 if (*this->radio_ !=
nullptr)
700 (*this->radio_)->set_mode_standby();
702 ESP_LOGW(detail::TAG,
703 "LR1121 bootloader rewrite: starting the three-stage sequence, ~10s total. Mains power, not "
704 "battery -- do not interrupt power. Stage 2 has no recovery path in this project if power is lost.");
708 uint8_t sanity_type = 0;
709 uint16_t sanity_bootloader_version = 0;
710 if (!this->lr1121_firmware_updater_->enter_bootloader(sanity_type, sanity_bootloader_version)) {
711 ESP_LOGE(detail::TAG,
712 "LR1121 bootloader rewrite: Stage 1a bootloader entry could not be confirmed (BUSY timeout) -- "
713 "the bootloader itself is untouched, this is recoverable: press the button again to retry.");
718 const Lr1121SanityResult sanity = lr1121_check_bootloader_sanity(
719 this->lr1121_bootloader_version_known_, this->lr1121_bootloader_version_, sanity_type, sanity_bootloader_version);
720 if (sanity != Lr1121SanityResult::OK) {
721 const std::string sanity_reason = lr1121_sanity_failure_reason(sanity, sanity_bootloader_version);
722 ESP_LOGE(detail::TAG,
723 "LR1121 bootloader rewrite: Stage 1a sanity check failed (%s) -- aborting before erasing anything; "
724 "the bootloader is untouched, this is recoverable: press the button again to retry.",
725 sanity_reason.c_str());
726 this->lr1121_firmware_updater_->reboot(
false);
737 uint32_t erase_elapsed_ms = 0, write_elapsed_ms = 0;
738 if (!lr1121_erase_and_write_image_(
739 *this->lr1121_firmware_updater_,
"LR1121 bootloader rewrite: Stage 1a (loader write)",
740 LR1121_BOOTLOADER_LOADER_IMAGE, LR1121_BOOTLOADER_LOADER_IMAGE_WORDS, erase_elapsed_ms, write_elapsed_ms)) {
741 ESP_LOGE(detail::TAG,
742 "LR1121 bootloader rewrite: Stage 1a failed -- the bootloader is untouched, this is recoverable: "
743 "press the button again to retry.");
750 if (!this->lr1121_firmware_updater_->reboot(
false)) {
751 ESP_LOGE(detail::TAG,
752 "LR1121 bootloader rewrite: Stage 1b reboot-into-loader command failed to send (BUSY timeout) -- "
753 "the bootloader is untouched, this is recoverable: press the button again to retry.");
757 uint8_t loader_device_type = 0, loader_fw_major = 0, loader_fw_minor = 0;
761 if (!this->lr1121_firmware_updater_->read_normal_version(loader_device_type, loader_fw_major, loader_fw_minor)) {
762 ESP_LOGE(detail::TAG,
763 "LR1121 bootloader rewrite: Stage 1b checkpoint failed -- could not read the chip's firmware version "
764 "after the reboot (BUSY timeout). Aborting before the irreversible write; the bootloader is "
765 "untouched, this is recoverable: press the button again to retry.");
778 if (loader_device_type != LR1121_UPDATER_LOADER_DEVICE_TYPE) {
779 const bool still_in_bootloader = loader_device_type == LR1121_UPDATER_BOOTLOADER_TYPE;
780 ESP_LOGE(detail::TAG,
781 "LR1121 bootloader rewrite: Stage 1b checkpoint failed -- chip reports type=0x%02X, expected the "
782 "loader's 0x%02X%s. The loader is not confirmed to be running, so 0x8100 must not be sent. "
783 "Aborting before the irreversible write; the bootloader is untouched, this is recoverable: press "
784 "the button again to retry.",
785 loader_device_type, LR1121_UPDATER_LOADER_DEVICE_TYPE,
786 still_in_bootloader ?
" (0xDF means the chip never left bootloader mode)" :
"");
790 const uint16_t loader_running_fw = (
static_cast<uint16_t
>(loader_fw_major) << 8) | loader_fw_minor;
791 if (loader_running_fw != LR1121_LOADER_2100) {
792 ESP_LOGE(detail::TAG,
793 "LR1121 bootloader rewrite: Stage 1b checkpoint failed -- chip reports firmware %s after the "
794 "reboot, expected the loader's %s. Aborting before the irreversible write; the bootloader is "
795 "untouched, this is recoverable: press the button again to retry.",
796 format_lr1121_fw_version(loader_running_fw).c_str(), format_lr1121_fw_version(LR1121_LOADER_2100).c_str());
800 ESP_LOGI(detail::TAG,
801 "LR1121 bootloader rewrite: Stage 1b checkpoint passed -- chip in transceiver mode: type=0x%02X fw=%s",
802 loader_device_type, format_lr1121_fw_version(loader_running_fw).c_str());
805 ESP_LOGW(detail::TAG,
806 "LR1121 bootloader rewrite: Stage 2 -- rewriting the bootloader now. This step cannot be undone. Do "
807 "not interrupt power.");
808 if (!this->lr1121_firmware_updater_->update_bootloader()) {
809 ESP_LOGE(detail::TAG,
810 "LR1121 bootloader rewrite: Stage 2 UpdateBootloader timed out waiting for BUSY -- outcome "
811 "unknown, the bootloader may be mid-write. There is no recovery path in this project for this "
812 "failure. Rebooting.");
823 Lr1121UpdaterStatus updater_status;
824 if (!this->lr1121_firmware_updater_->read_updater_status(updater_status)) {
825 ESP_LOGW(detail::TAG,
826 "LR1121 bootloader rewrite: Stage 2 status read timed out (BUSY) -- continuing to the verification "
827 "read, which is what actually decides the outcome");
828 }
else if (updater_status.command_status != Lr1121UpdaterCommandStatus::OK &&
829 updater_status.command_status != Lr1121UpdaterCommandStatus::DATA) {
830 ESP_LOGE(detail::TAG,
831 "LR1121 bootloader rewrite: Stage 2 chip reports command_status=%u after UpdateBootloader (0=FAIL, "
832 "1=PERR) -- the chip did not accept 0x8100, which most likely means the bootloader was NOT "
833 "rewritten. The verification below decides; report this line if it appears.",
834 static_cast<unsigned>(updater_status.command_status));
836 ESP_LOGI(detail::TAG,
"LR1121 bootloader rewrite: Stage 2 chip accepted UpdateBootloader (command_status=%u)",
837 static_cast<unsigned>(updater_status.command_status));
840 Lr1121BootloaderVerification verification;
841 if (!this->lr1121_firmware_updater_->verify_bootloader(verification)) {
842 ESP_LOGE(detail::TAG,
843 "LR1121 bootloader rewrite: Stage 2 VerifyBootloader read timed out (BUSY) after the write already "
844 "ran -- outcome unknown. There is no recovery path in this project for this failure. Rebooting.");
848 if (!verification.all_checks_passed()) {
849 ESP_LOGE(detail::TAG,
850 "LR1121 bootloader rewrite: Stage 2 verification failed after the write already ran (signature=%d "
851 "version=%d use_case=%d version_major=%d version_minor=%d anti_rollback=%d) -- the write already "
852 "happened; do NOT retry Stage 2. There is no recovery path in this project for this failure. "
854 verification.signature_verified, verification.version_verified, verification.use_case_verified,
855 verification.version_major_verified, verification.version_minor_verified,
856 verification.anti_rollback_verified);
861 if (!this->lr1121_firmware_updater_->updater_reboot(
false)) {
862 ESP_LOGE(detail::TAG,
863 "LR1121 bootloader rewrite: Stage 2 post-verify reboot command failed to send (BUSY timeout) -- "
864 "the write and verification both succeeded, but the chip's resulting state cannot be confirmed. "
865 "Rebooting the ESP32.");
872 uint8_t post_update_type = 0;
873 uint16_t post_update_bootloader_version = 0;
874 const bool post_update_read_ok =
875 this->lr1121_firmware_updater_->read_bootloader_version(post_update_type, post_update_bootloader_version);
876 if (!post_update_read_ok || post_update_type != LR1121_UPDATER_BOOTLOADER_TYPE ||
877 post_update_bootloader_version != LR1121_BOOTLOADER_2101) {
878 ESP_LOGE(detail::TAG,
879 "LR1121 bootloader rewrite: Stage 2 succeeded but the chip is not behaving as expected afterward "
880 "(read_ok=%d type=0x%02X bootloader=%s; expected to stay in the bootloader reporting 0x2101) -- "
881 "the write already happened; this is NOT the recoverable kind of failure. If the chip still "
882 "answers a bootloader-mode GetVersion with a sane version, the strap works and a transceiver "
883 "image can be written for whichever bootloader it reports -- but do not auto-retry Stage 2. "
885 post_update_read_ok, post_update_type, format_hex16(post_update_bootloader_version).c_str());
889 this->lr1121_bootloader_chip_type_ = post_update_type;
890 this->lr1121_bootloader_version_ = post_update_bootloader_version;
891 ESP_LOGI(detail::TAG,
"LR1121 bootloader rewrite: Stage 2 complete -- bootloader is now 0x2101.");
896 uint8_t stage3_type = 0;
897 uint16_t stage3_bootloader_version = 0;
898 if (!this->lr1121_firmware_updater_->enter_bootloader(stage3_type, stage3_bootloader_version)) {
899 ESP_LOGE(detail::TAG,
900 "LR1121 bootloader rewrite: Stage 3 bootloader entry could not be confirmed (BUSY timeout) -- the "
901 "bootloader was already rewritten successfully in Stage 2, so this is recoverable: press the "
902 "ordinary flash button again (no switch needed) once power is stable.");
906 if (stage3_type != LR1121_UPDATER_BOOTLOADER_TYPE || stage3_bootloader_version != LR1121_BOOTLOADER_2101) {
907 ESP_LOGE(detail::TAG,
908 "LR1121 bootloader rewrite: Stage 3 sanity check failed (type=0x%02X bootloader=%s, expected "
909 "0x2101) -- aborting before erasing the transceiver region. The bootloader was already rewritten "
910 "successfully in Stage 2; this is recoverable: press the ordinary flash button again.",
911 stage3_type, format_hex16(stage3_bootloader_version).c_str());
915 this->lr1121_bootloader_chip_type_ = stage3_type;
916 this->lr1121_bootloader_version_ = stage3_bootloader_version;
918 if (!lr1121_erase_and_write_image_(
919 *this->lr1121_firmware_updater_,
"LR1121 bootloader rewrite: Stage 3 (transceiver write)",
920 LR1121_FIRMWARE_UPDATE_IMAGE, LR1121_FIRMWARE_UPDATE_IMAGE_WORDS, erase_elapsed_ms, write_elapsed_ms)) {
921 ESP_LOGE(detail::TAG,
922 "LR1121 bootloader rewrite: Stage 3 failed -- the bootloader is already on 0x2101 (that part is "
923 "done and does not need to be repeated); this is recoverable: after this reboot, the ordinary "
924 "flash button (no switch needed) can retry the transceiver write.");
929 lr1121_log_post_write_hash(*this->lr1121_firmware_updater_);
931 if (!this->lr1121_firmware_updater_->reboot(
false)) {
932 ESP_LOGW(detail::TAG,
"LR1121 bootloader rewrite: Stage 3 reboot-to-image command failed to send (BUSY timeout)");
934 uint8_t device_type = 0, fw_major = 0, fw_minor = 0;
935 if (this->lr1121_firmware_updater_->read_normal_version(device_type, fw_major, fw_minor)) {
936 const uint16_t new_fw = (
static_cast<uint16_t
>(fw_major) << 8) | fw_minor;
937 lr1121_log_post_flash_verify_result(new_fw, LR1121_FIRMWARE_UPDATE_TARGET_VERSION);
939 ESP_LOGW(detail::TAG,
"LR1121 bootloader rewrite: could not read back the post-flash version (BUSY timeout)");
The main IO-Homecontrol component.
Abstract radio driver for IO-Homecontrol.
Interface for SPI bus access.
Internal helpers shared by the hub implementation .cpp files.
Pure decision logic for the LR1121 transceiver-firmware-update feature.
LR1121 transceiver-firmware-update feature — orchestration collaborator.
constexpr const char * TAG
Shared log tag for hub-level messages.
constexpr uint16_t lr1121_required_bootloader_for(uint16_t target_fw)
Required bootloader for a known target firmware version.
constexpr BootloaderSupport lr1121_bootloader_supports_target(uint16_t target_fw, uint16_t bootloader_version)
Look up whether target_fw is known to require bootloader_version.
constexpr BootloaderUpgradePath lr1121_bootloader_upgrade_path(bool block_present, bool bootloader_version_known, uint16_t bootloader_version, uint16_t loader_fw, uint16_t target_fw)
Whether the three-stage bootloader upgrade is applicable for the current cached state.
BootloaderUpgradePath
Whether the three-stage bootloader-rewrite sequence (ADR 0021) is applicable, and if not,...
constexpr const char * lr1121_chip_family_for_bootloader(uint16_t bootloader_version)
Human-readable chip family for a bootloader version that is not one of the two LR1121 values above,...
constexpr bool lr1121_bootloader_is_lr1121(uint16_t bootloader_version)
@ UNKNOWN_TARGET
target_fw does not appear in LR1121_KNOWN_BOOTLOADER_REQUIREMENTS at all.
constexpr FlashDecision lr1121_flash_decision(uint8_t device_type, uint8_t bootloader_chip_type, uint16_t bootloader_version, uint16_t installed_fw, uint16_t target_fw, bool already_confirmed)
The single decision point for whether/how to flash target_fw.
FlashDecision
Outcome of lr1121_flash_decision().
@ NEEDS_CONFIRMATION
Not unsafe, but not an unambiguous "yes" either — needs a second press.
std::function< void()> BeginBlockingExcursionFn
Raises the hub's "operation took a long time" warning threshold for a blocking radio excursion — writ...
constexpr BootloaderMismatch lr1121_bootloader_mismatch_kind(uint16_t target_fw, uint16_t bootloader_version)
Classify a bootloader/target mismatch by direction; see BootloaderMismatch.
constexpr const char * lr1121_chip_family_for_device_type(uint8_t device_type)
Human-readable chip family for a normal-mode device_type that is not the LR1121 value above,...
LR1121 bootloader-mode-*and*-loader-mode SPI transport, standalone from the running RadioDriver.