8#ifdef IOHOME_LR1121_FIRMWARE_UPDATE
12#include "lr1121_firmware_update_image.h"
14#ifdef IOHOME_LR1121_BOOTLOADER_UPDATE
17#include "lr1121_bootloader_loader_image.h"
20#include "esphome/core/application.h"
55constexpr uint32_t LR1121_FLASH_CONFIRM_WINDOW_MS = 60 * 1000;
58constexpr uint8_t WARN_IF_BLOCKING_OVER_MAX_CS = 255;
60std::string format_lr1121_fw_version(uint16_t version) {
64 snprintf(buf,
sizeof(buf),
"%u.%u",
static_cast<unsigned>(version >> 8),
static_cast<unsigned>(version & 0xFF));
68std::string format_hex16(uint16_t value) {
70 snprintf(buf,
sizeof(buf),
"0x%04X", value);
74std::string format_hex8(uint8_t value) {
76 snprintf(buf,
sizeof(buf),
"0x%02X", value);
82std::string format_lr1121_bootloader_version(uint16_t version) {
83 return version == 0 ?
"unknown" : format_hex16(version);
88enum class Lr1121SanityResult {
106Lr1121SanityResult lr1121_check_bootloader_sanity(
bool known, uint16_t known_bootloader_version, uint8_t sanity_type,
107 uint16_t sanity_bootloader_version) {
108 if (sanity_type != LR1121_UPDATER_BOOTLOADER_TYPE)
109 return Lr1121SanityResult::WRONG_TYPE;
111 if (sanity_bootloader_version != known_bootloader_version)
112 return Lr1121SanityResult::VERSION_MISMATCH;
113 return Lr1121SanityResult::OK;
116 return Lr1121SanityResult::WRONG_CHIP_FAMILY;
117 return Lr1121SanityResult::OK;
123std::string lr1121_sanity_failure_reason(Lr1121SanityResult sanity, uint16_t sanity_bootloader_version) {
125 case Lr1121SanityResult::WRONG_TYPE:
127 case Lr1121SanityResult::WRONG_CHIP_FAMILY:
128 return "bootloader version " + format_hex16(sanity_bootloader_version) +
" identifies " +
130 case Lr1121SanityResult::VERSION_MISMATCH:
132 return "bootloader version changed since boot";
140void lr1121_log_post_flash_verify_result(uint16_t new_fw, uint16_t target_fw) {
141 if (target_fw == 0) {
143 "LR1121 firmware update: now running %s; this build had no expected version to compare against",
144 format_lr1121_fw_version(new_fw).c_str());
145 }
else if (new_fw == target_fw) {
146 ESP_LOGI(
detail::TAG,
"LR1121 firmware update: success -- now running %s",
147 format_lr1121_fw_version(new_fw).c_str());
149 ESP_LOGW(
detail::TAG,
"LR1121 firmware update: post-flash version is %s, expected %s",
150 format_lr1121_fw_version(new_fw).c_str(), format_lr1121_fw_version(target_fw).c_str());
185void lr1121_log_post_write_hash(Lr1121FirmwareUpdater &updater) {
186 uint8_t hash[LR1121_UPDATER_HASH_LENGTH] = {0};
187 if (!updater.read_hash(hash,
sizeof(hash))) {
189 "LR1121 firmware update: could not read the flash fingerprint (BUSY timeout). Harmless -- it is "
190 "only an identifier, not a correctness check; the version check below is what confirms the flash.");
199 const bool degenerate = std::all_of(hash + 1, hash + LR1121_UPDATER_HASH_LENGTH, [](uint8_t b) {
return b == 0; });
202 "LR1121 firmware update: no flash fingerprint available on this bootloader (GetHash returned a "
203 "fixed non-value). Harmless -- it was only ever an identifier, never a correctness check; the "
204 "firmware version reported below is what confirms the flash worked.");
207 char hex[LR1121_UPDATER_HASH_LENGTH * 2 + 1];
208 for (
size_t i = 0; i < LR1121_UPDATER_HASH_LENGTH; i++)
209 snprintf(hex + i * 2, 3,
"%02x", hash[i]);
211 "LR1121 firmware update: flash fingerprint %s -- an identifier for the image now on the chip, useful "
212 "for comparing two boards. It is not the image's MD5 and cannot be checked against anything; the "
213 "firmware version reported below is what confirms the flash worked.",
224bool lr1121_erase_and_write_image_(Lr1121FirmwareUpdater &updater,
const char *stage_label,
const uint32_t *image,
225 size_t word_count, uint32_t &erase_elapsed_ms, uint32_t &write_elapsed_ms) {
226 ESP_LOGI(
detail::TAG,
"%s: erasing radio flash, this takes a few seconds...", stage_label);
227 const uint32_t erase_start_ms = millis();
228 if (!updater.erase_flash()) {
229 ESP_LOGE(
detail::TAG,
"%s: erase failed (BUSY timeout)", stage_label);
232 erase_elapsed_ms = millis() - erase_start_ms;
234 size_t last_logged_words = 0;
235 const size_t log_step = std::max<size_t>(word_count / 10, 1);
236 const uint32_t write_start_ms = millis();
237 const bool write_ok = updater.write_image(image, word_count, [&](
size_t done,
size_t total) {
241 if (done - last_logged_words < log_step && done != total)
243 last_logged_words = done;
244 ESP_LOGI(
detail::TAG,
"%s: flashing %zu/%zu words (%u%%)", stage_label, done, total,
245 static_cast<unsigned>((done * 100) / total));
247 write_elapsed_ms = millis() - write_start_ms;
249 ESP_LOGE(
detail::TAG,
"%s: write failed (BUSY timeout) after %" PRIu32
" ms", stage_label, write_elapsed_ms);
252 ESP_LOGI(
detail::TAG,
"%s: erase took %" PRIu32
" ms, write took %" PRIu32
" ms", stage_label, erase_elapsed_ms,
268std::string lr1121_needs_confirmation_reason(uint8_t device_type, uint16_t bootloader_version, uint16_t installed_fw,
269 uint16_t target_fw) {
270 if (bootloader_version == 0)
271 return "the bootloader version could not be read at boot, so chip identity and bootloader compatibility "
272 "cannot be verified";
273 if (device_type == 0)
274 return "the installed firmware version could not be read (radio failed to initialize, or the read itself "
277 return "no target firmware version could be determined for the configured image";
279 return "target firmware " + format_lr1121_fw_version(target_fw) +
280 " is not in this build's known bootloader-compatibility table (unverified, not refused)";
282 if (installed_fw == 0)
283 return "the installed firmware version is unknown";
284 return "target firmware " + format_lr1121_fw_version(target_fw) +
" is not newer than the installed " +
285 format_lr1121_fw_version(installed_fw);
290void IOHomeControlComponent::run_lr1121_boot_time_bootloader_read_() {
291 this->lr1121_firmware_updater_ =
new (std::nothrow) Lr1121FirmwareUpdater(
this, this->
rst_pin_, this->
busy_pin_);
292 if (this->lr1121_firmware_updater_ ==
nullptr) {
293 ESP_LOGE(
detail::TAG,
"LR1121 firmware update: failed to allocate the updater; bootloader version unknown");
298 uint16_t bootloader_version = 0;
299 if (!this->lr1121_firmware_updater_->enter_bootloader(type, bootloader_version)) {
300 ESP_LOGW(
detail::TAG,
"LR1121 firmware update: could not read the bootloader version at boot (BUSY timeout) -- "
301 "bootloader version stays unknown until the next boot");
304 this->lr1121_bootloader_chip_type_ = type;
305 this->lr1121_bootloader_version_ = bootloader_version;
306 this->lr1121_bootloader_version_known_ =
true;
312 if (!this->lr1121_firmware_updater_->reboot(
false)) {
313 ESP_LOGW(
detail::TAG,
"LR1121 firmware update: reboot-out-of-bootloader command failed to send (BUSY timeout); the "
314 "upcoming radio init's own hardware reset will recover it");
318void IOHomeControlComponent::cache_lr1121_flash_verdict_() {
319 uint8_t device_type = 0;
320 uint16_t installed_fw = 0;
321 if (this->
radio_ !=
nullptr && this->lr1121_firmware_updater_ !=
nullptr) {
326 uint8_t fw_major = 0, fw_minor = 0;
329 if (this->lr1121_firmware_updater_->read_normal_version(device_type, fw_major, fw_minor))
330 installed_fw = (
static_cast<uint16_t
>(fw_major) << 8) | fw_minor;
332 this->lr1121_installed_device_type_ = device_type;
333 this->lr1121_installed_fw_ = installed_fw;
337 this->lr1121_flash_verdict_ =
338 lr1121_flash_decision(device_type, this->lr1121_bootloader_chip_type_, this->lr1121_bootloader_version_,
339 installed_fw, LR1121_FIRMWARE_UPDATE_TARGET_VERSION,
false);
340 this->lr1121_flash_verdict_known_ =
true;
347std::string IOHomeControlComponent::describe_lr1121_flash_verdict_()
const {
348 const uint16_t target = LR1121_FIRMWARE_UPDATE_TARGET_VERSION;
349 const std::string prefix =
"Firmware update target: " + format_lr1121_fw_version(target) +
" -- ";
351 switch (this->lr1121_flash_verdict_) {
358 return prefix +
"CANNOT PROCEED: bootloader version " + format_hex16(this->lr1121_bootloader_version_) +
361 return prefix +
"CANNOT PROCEED: normal-mode chip identity byte " +
362 format_hex8(this->lr1121_installed_device_type_) +
" identifies " +
373 return prefix +
"CANNOT PROCEED: this chip's bootloader " + format_hex16(this->lr1121_bootloader_version_) +
374 " is newer than this firmware supports (needs " + format_hex16(required) +
375 ") -- there is no downgrade path";
377 std::string message = prefix +
"CANNOT PROCEED: needs bootloader " + format_hex16(required) +
", this chip has " +
378 format_hex16(this->lr1121_bootloader_version_);
379#ifndef IOHOME_LR1121_BOOTLOADER_UPDATE
386 message +=
" -- add a bootloader: sub-block to lr1121_firmware_update: to enable the (irreversible) upgrade "
392 return prefix +
"already running the configured firmware, nothing to do";
395 lr1121_needs_confirmation_reason(this->lr1121_installed_device_type_, this->lr1121_bootloader_version_,
396 this->lr1121_installed_fw_, target) +
397 " (bootloader version " + format_lr1121_bootloader_version(this->lr1121_bootloader_version_) +
")";
400 return prefix +
"ready to flash (press \"Flash LR1121 Radio Firmware\")";
408#ifdef IOHOME_LR1121_BOOTLOADER_UPDATE
409std::string IOHomeControlComponent::describe_lr1121_bootloader_refusal_(
BootloaderUpgradePath path)
const {
415 const std::string target_text = format_lr1121_fw_version(LR1121_FIRMWARE_UPDATE_TARGET_VERSION);
416 const std::string chip_text = format_hex16(this->lr1121_bootloader_version_);
418 const std::string prefix =
"LR1121 firmware update: nothing was done, the radio was not touched. ";
422 return prefix +
"Firmware " + target_text +
" needs bootloader " + required_text +
" and this chip has " +
424 ", so the bootloader has to be rewritten first. To do that, turn on the \"Allow LR1121 Bootloader "
425 "Rewrite (Irreversible)\" switch and press this button again. A bootloader rewrite cannot be undone.";
427 return prefix +
"This build does not recognise firmware " + target_text +
428 ", so it cannot tell which bootloader that image needs. The bootloader rewrite stays disabled rather "
429 "than risk an irreversible write on a guess.";
431 return prefix +
"This chip's bootloader " + chip_text +
" is already newer than firmware " + target_text +
432 " supports (that image needs " + required_text +
"), and there is no way back to an older bootloader.";
437 return this->describe_lr1121_flash_verdict_();
442std::vector<std::string> IOHomeControlComponent::lr1121_firmware_update_debug_lines_()
const {
443 std::vector<std::string> lines;
444 if (this->lr1121_bootloader_version_known_) {
445 lines.push_back(
"LR1121 bootloader version: " + format_hex16(this->lr1121_bootloader_version_));
447 lines.push_back(
"LR1121 firmware update: bootloader version could not be read at boot");
449 if (this->lr1121_flash_verdict_known_)
450 lines.push_back(this->describe_lr1121_flash_verdict_());
451#ifdef IOHOME_LR1121_BOOTLOADER_UPDATE
456 true, this->lr1121_bootloader_version_known_, this->lr1121_bootloader_version_,
457 LR1121_BOOTLOADER_LOADER_FW, LR1121_FIRMWARE_UPDATE_TARGET_VERSION);
463 lines.push_back(
"LR1121 bootloader rewrite: AVAILABLE -- needs bootloader " +
465 ", this chip has " + format_hex16(this->lr1121_bootloader_version_) +
466 ". A bootloader rewrite cannot be undone.");
468 lines.push_back(
"LR1121 bootloader rewrite: configured, but inert -- this build does not know what bootloader the "
469 "configured target requires, so it will not gamble an irreversible write on it.");
471 lines.push_back(
"LR1121 bootloader rewrite: configured, but inert -- this chip's bootloader is already newer than "
472 "the configured target needs; there is no downgrade path.");
478void IOHomeControlComponent::dump_lr1121_firmware_update_debug_()
const {
479 for (
const auto &line : this->lr1121_firmware_update_debug_lines_())
483void IOHomeControlComponent::arm_lr1121_flash_confirmation_() {
484 this->lr1121_flash_confirmation_armed_ =
true;
494 App.scheduler.set_timeout(
this, LR1121_FLASH_CONFIRM_WINDOW_MS, [
this]() {
497 if (!this->lr1121_flash_confirmation_armed_)
499 this->lr1121_flash_confirmation_armed_ =
false;
500 ESP_LOGI(
detail::TAG,
"LR1121 firmware update: confirmation window expired without a second press");
504void IOHomeControlComponent::trigger_lr1121_firmware_update() {
509 if (this->
radio_ ==
nullptr)
510 ESP_LOGW(
detail::TAG,
"LR1121 firmware update: radio_ is null (failed init); proceeding without standby");
517 ESP_LOGW(
detail::TAG,
"LR1121 firmware update: radio busy with another operation, ignoring press");
521 if (this->lr1121_firmware_updater_ ==
nullptr || !this->lr1121_flash_verdict_known_) {
522 ESP_LOGE(
detail::TAG,
"LR1121 firmware update: no cached verdict available (setup() may have failed early)");
532 ESP_LOGE(
detail::TAG,
"%s", this->describe_lr1121_flash_verdict_().c_str());
537#ifdef IOHOME_LR1121_BOOTLOADER_UPDATE
542 true, this->lr1121_bootloader_version_known_, this->lr1121_bootloader_version_,
543 LR1121_BOOTLOADER_LOADER_FW, LR1121_FIRMWARE_UPDATE_TARGET_VERSION);
545 if (!this->bootloader_rewrite_allowed_) {
546 ESP_LOGE(
detail::TAG,
"%s", this->describe_lr1121_bootloader_refusal_(upgrade_path).c_str());
552 ESP_LOGW(
detail::TAG,
"LR1121 bootloader rewrite: arming switch is on -- running the three-stage sequence now.");
553 this->run_lr1121_bootloader_upgrade_sequence_();
558 ESP_LOGE(
detail::TAG,
"%s", this->describe_lr1121_bootloader_refusal_(upgrade_path).c_str());
565 ESP_LOGE(
detail::TAG,
"%s", this->describe_lr1121_flash_verdict_().c_str());
577 const std::string confirm_suffix =
" -- press \"Flash LR1121 Radio Firmware\" again within " +
578 std::to_string(LR1121_FLASH_CONFIRM_WINDOW_MS / 1000) +
"s to " +
579 (already_installed ?
"re-flash anyway" :
"proceed anyway");
580 const std::string message = this->describe_lr1121_flash_verdict_() + confirm_suffix;
581 if (already_installed) {
586 this->arm_lr1121_flash_confirmation_();
590 this->lr1121_flash_confirmation_armed_ =
false;
591 this->run_lr1121_flash_sequence_();
594void IOHomeControlComponent::run_lr1121_flash_sequence_() {
601 this->warn_if_blocking_over_ = WARN_IF_BLOCKING_OVER_MAX_CS;
602 if (this->
radio_ !=
nullptr)
603 this->
radio_->set_mode_standby();
608 uint8_t sanity_type = 0;
609 uint16_t sanity_bootloader_version = 0;
610 if (!this->lr1121_firmware_updater_->enter_bootloader(sanity_type, sanity_bootloader_version)) {
612 "LR1121 firmware update: bootloader entry could not be confirmed (BUSY timeout on the verification "
613 "read) -- the entry sequence itself already ran, so the chip may be unconfigured; rebooting to "
614 "recover it rather than risking a silently dead radio");
619 const Lr1121SanityResult sanity = lr1121_check_bootloader_sanity(
620 this->lr1121_bootloader_version_known_, this->lr1121_bootloader_version_, sanity_type, sanity_bootloader_version);
621 if (sanity != Lr1121SanityResult::OK) {
622 const std::string sanity_reason = lr1121_sanity_failure_reason(sanity, sanity_bootloader_version);
625 "LR1121 firmware update: bootloader-entry sanity check failed (%s; read type=0x%02X bootloader=%s, "
626 "boot-time bootloader was %s) -- aborting before erasing anything",
627 sanity_reason.c_str(), sanity_type, format_hex16(sanity_bootloader_version).c_str(),
628 this->lr1121_bootloader_version_known_ ? format_hex16(this->lr1121_bootloader_version_).c_str() :
"unknown");
629 this->lr1121_firmware_updater_->reboot(
false);
633 if (!this->lr1121_bootloader_version_known_) {
639 "LR1121 firmware update: boot-time bootloader version was unknown; type check passed and bootloader "
641 format_hex16(sanity_bootloader_version).c_str());
642 this->lr1121_bootloader_chip_type_ = sanity_type;
643 this->lr1121_bootloader_version_ = sanity_bootloader_version;
644 this->lr1121_bootloader_version_known_ =
true;
647 uint32_t erase_elapsed_ms = 0, write_elapsed_ms = 0;
648 if (!lr1121_erase_and_write_image_(*this->lr1121_firmware_updater_,
"LR1121 firmware update",
649 LR1121_FIRMWARE_UPDATE_IMAGE, LR1121_FIRMWARE_UPDATE_IMAGE_WORDS, erase_elapsed_ms,
652 "LR1121 firmware update: the radio firmware is now incomplete. This is recoverable: after this "
653 "reboot, press the button again to re-flash.");
660 lr1121_log_post_write_hash(*this->lr1121_firmware_updater_);
662 if (!this->lr1121_firmware_updater_->reboot(
false)) {
663 ESP_LOGW(
detail::TAG,
"LR1121 firmware update: reboot-to-image command failed to send (BUSY timeout)");
665 uint8_t device_type = 0, fw_major = 0, fw_minor = 0;
666 if (this->lr1121_firmware_updater_->read_normal_version(device_type, fw_major, fw_minor)) {
667 const uint16_t new_fw = (
static_cast<uint16_t
>(fw_major) << 8) | fw_minor;
668 lr1121_log_post_flash_verify_result(new_fw, LR1121_FIRMWARE_UPDATE_TARGET_VERSION);
670 ESP_LOGW(
detail::TAG,
"LR1121 firmware update: could not read back the post-flash version (BUSY timeout)");
680#ifdef IOHOME_LR1121_BOOTLOADER_UPDATE
682void IOHomeControlComponent::run_lr1121_bootloader_upgrade_sequence_() {
684 this->warn_if_blocking_over_ = WARN_IF_BLOCKING_OVER_MAX_CS;
685 if (this->
radio_ !=
nullptr)
686 this->
radio_->set_mode_standby();
689 "LR1121 bootloader rewrite: starting the three-stage sequence, ~10s total. Mains power, not "
690 "battery -- do not interrupt power. Stage 2 has no recovery path in this project if power is lost.");
694 uint8_t sanity_type = 0;
695 uint16_t sanity_bootloader_version = 0;
696 if (!this->lr1121_firmware_updater_->enter_bootloader(sanity_type, sanity_bootloader_version)) {
698 "LR1121 bootloader rewrite: Stage 1a bootloader entry could not be confirmed (BUSY timeout) -- "
699 "the bootloader itself is untouched, this is recoverable: press the button again to retry.");
704 const Lr1121SanityResult sanity = lr1121_check_bootloader_sanity(
705 this->lr1121_bootloader_version_known_, this->lr1121_bootloader_version_, sanity_type, sanity_bootloader_version);
706 if (sanity != Lr1121SanityResult::OK) {
707 const std::string sanity_reason = lr1121_sanity_failure_reason(sanity, sanity_bootloader_version);
709 "LR1121 bootloader rewrite: Stage 1a sanity check failed (%s) -- aborting before erasing anything; "
710 "the bootloader is untouched, this is recoverable: press the button again to retry.",
711 sanity_reason.c_str());
712 this->lr1121_firmware_updater_->reboot(
false);
723 uint32_t erase_elapsed_ms = 0, write_elapsed_ms = 0;
724 if (!lr1121_erase_and_write_image_(
725 *this->lr1121_firmware_updater_,
"LR1121 bootloader rewrite: Stage 1a (loader write)",
726 LR1121_BOOTLOADER_LOADER_IMAGE, LR1121_BOOTLOADER_LOADER_IMAGE_WORDS, erase_elapsed_ms, write_elapsed_ms)) {
728 "LR1121 bootloader rewrite: Stage 1a failed -- the bootloader is untouched, this is recoverable: "
729 "press the button again to retry.");
736 if (!this->lr1121_firmware_updater_->reboot(
false)) {
738 "LR1121 bootloader rewrite: Stage 1b reboot-into-loader command failed to send (BUSY timeout) -- "
739 "the bootloader is untouched, this is recoverable: press the button again to retry.");
743 uint8_t loader_device_type = 0, loader_fw_major = 0, loader_fw_minor = 0;
747 if (!this->lr1121_firmware_updater_->read_normal_version(loader_device_type, loader_fw_major, loader_fw_minor)) {
749 "LR1121 bootloader rewrite: Stage 1b checkpoint failed -- could not read the chip's firmware version "
750 "after the reboot (BUSY timeout). Aborting before the irreversible write; the bootloader is "
751 "untouched, this is recoverable: press the button again to retry.");
764 if (loader_device_type != LR1121_UPDATER_LOADER_DEVICE_TYPE) {
765 const bool still_in_bootloader = loader_device_type == LR1121_UPDATER_BOOTLOADER_TYPE;
767 "LR1121 bootloader rewrite: Stage 1b checkpoint failed -- chip reports type=0x%02X, expected the "
768 "loader's 0x%02X%s. The loader is not confirmed to be running, so 0x8100 must not be sent. "
769 "Aborting before the irreversible write; the bootloader is untouched, this is recoverable: press "
770 "the button again to retry.",
771 loader_device_type, LR1121_UPDATER_LOADER_DEVICE_TYPE,
772 still_in_bootloader ?
" (0xDF means the chip never left bootloader mode)" :
"");
776 const uint16_t loader_running_fw = (
static_cast<uint16_t
>(loader_fw_major) << 8) | loader_fw_minor;
779 "LR1121 bootloader rewrite: Stage 1b checkpoint failed -- chip reports firmware %s after the "
780 "reboot, expected the loader's %s. Aborting before the irreversible write; the bootloader is "
781 "untouched, this is recoverable: press the button again to retry.",
782 format_lr1121_fw_version(loader_running_fw).c_str(), format_lr1121_fw_version(
LR1121_LOADER_2100).c_str());
787 "LR1121 bootloader rewrite: Stage 1b checkpoint passed -- chip in transceiver mode: type=0x%02X fw=%s",
788 loader_device_type, format_lr1121_fw_version(loader_running_fw).c_str());
792 "LR1121 bootloader rewrite: Stage 2 -- rewriting the bootloader now. This step cannot be undone. Do "
793 "not interrupt power.");
794 if (!this->lr1121_firmware_updater_->update_bootloader()) {
796 "LR1121 bootloader rewrite: Stage 2 UpdateBootloader timed out waiting for BUSY -- outcome "
797 "unknown, the bootloader may be mid-write. There is no recovery path in this project for this "
798 "failure. Rebooting.");
809 Lr1121UpdaterStatus updater_status;
810 if (!this->lr1121_firmware_updater_->read_updater_status(updater_status)) {
812 "LR1121 bootloader rewrite: Stage 2 status read timed out (BUSY) -- continuing to the verification "
813 "read, which is what actually decides the outcome");
814 }
else if (updater_status.command_status != Lr1121UpdaterCommandStatus::OK &&
815 updater_status.command_status != Lr1121UpdaterCommandStatus::DATA) {
817 "LR1121 bootloader rewrite: Stage 2 chip reports command_status=%u after UpdateBootloader (0=FAIL, "
818 "1=PERR) -- the chip did not accept 0x8100, which most likely means the bootloader was NOT "
819 "rewritten. The verification below decides; report this line if it appears.",
820 static_cast<unsigned>(updater_status.command_status));
822 ESP_LOGI(
detail::TAG,
"LR1121 bootloader rewrite: Stage 2 chip accepted UpdateBootloader (command_status=%u)",
823 static_cast<unsigned>(updater_status.command_status));
826 Lr1121BootloaderVerification verification;
827 if (!this->lr1121_firmware_updater_->verify_bootloader(verification)) {
829 "LR1121 bootloader rewrite: Stage 2 VerifyBootloader read timed out (BUSY) after the write already "
830 "ran -- outcome unknown. There is no recovery path in this project for this failure. Rebooting.");
834 if (!verification.all_checks_passed()) {
836 "LR1121 bootloader rewrite: Stage 2 verification failed after the write already ran (signature=%d "
837 "version=%d use_case=%d version_major=%d version_minor=%d anti_rollback=%d) -- the write already "
838 "happened; do NOT retry Stage 2. There is no recovery path in this project for this failure. "
840 verification.signature_verified, verification.version_verified, verification.use_case_verified,
841 verification.version_major_verified, verification.version_minor_verified,
842 verification.anti_rollback_verified);
847 if (!this->lr1121_firmware_updater_->updater_reboot(
false)) {
849 "LR1121 bootloader rewrite: Stage 2 post-verify reboot command failed to send (BUSY timeout) -- "
850 "the write and verification both succeeded, but the chip's resulting state cannot be confirmed. "
851 "Rebooting the ESP32.");
858 uint8_t post_update_type = 0;
859 uint16_t post_update_bootloader_version = 0;
860 const bool post_update_read_ok =
861 this->lr1121_firmware_updater_->read_bootloader_version(post_update_type, post_update_bootloader_version);
862 if (!post_update_read_ok || post_update_type != LR1121_UPDATER_BOOTLOADER_TYPE ||
865 "LR1121 bootloader rewrite: Stage 2 succeeded but the chip is not behaving as expected afterward "
866 "(read_ok=%d type=0x%02X bootloader=%s; expected to stay in the bootloader reporting 0x2101) -- "
867 "the write already happened; this is NOT the recoverable kind of failure. If the chip still "
868 "answers a bootloader-mode GetVersion with a sane version, the strap works and a transceiver "
869 "image can be written for whichever bootloader it reports -- but do not auto-retry Stage 2. "
871 post_update_read_ok, post_update_type, format_hex16(post_update_bootloader_version).c_str());
875 this->lr1121_bootloader_chip_type_ = post_update_type;
876 this->lr1121_bootloader_version_ = post_update_bootloader_version;
877 ESP_LOGI(
detail::TAG,
"LR1121 bootloader rewrite: Stage 2 complete -- bootloader is now 0x2101.");
882 uint8_t stage3_type = 0;
883 uint16_t stage3_bootloader_version = 0;
884 if (!this->lr1121_firmware_updater_->enter_bootloader(stage3_type, stage3_bootloader_version)) {
886 "LR1121 bootloader rewrite: Stage 3 bootloader entry could not be confirmed (BUSY timeout) -- the "
887 "bootloader was already rewritten successfully in Stage 2, so this is recoverable: press the "
888 "ordinary flash button again (no switch needed) once power is stable.");
892 if (stage3_type != LR1121_UPDATER_BOOTLOADER_TYPE || stage3_bootloader_version !=
LR1121_BOOTLOADER_2101) {
894 "LR1121 bootloader rewrite: Stage 3 sanity check failed (type=0x%02X bootloader=%s, expected "
895 "0x2101) -- aborting before erasing the transceiver region. The bootloader was already rewritten "
896 "successfully in Stage 2; this is recoverable: press the ordinary flash button again.",
897 stage3_type, format_hex16(stage3_bootloader_version).c_str());
901 this->lr1121_bootloader_chip_type_ = stage3_type;
902 this->lr1121_bootloader_version_ = stage3_bootloader_version;
904 if (!lr1121_erase_and_write_image_(
905 *this->lr1121_firmware_updater_,
"LR1121 bootloader rewrite: Stage 3 (transceiver write)",
906 LR1121_FIRMWARE_UPDATE_IMAGE, LR1121_FIRMWARE_UPDATE_IMAGE_WORDS, erase_elapsed_ms, write_elapsed_ms)) {
908 "LR1121 bootloader rewrite: Stage 3 failed -- the bootloader is already on 0x2101 (that part is "
909 "done and does not need to be repeated); this is recoverable: after this reboot, the ordinary "
910 "flash button (no switch needed) can retry the transceiver write.");
915 lr1121_log_post_write_hash(*this->lr1121_firmware_updater_);
917 if (!this->lr1121_firmware_updater_->reboot(
false)) {
918 ESP_LOGW(
detail::TAG,
"LR1121 bootloader rewrite: Stage 3 reboot-to-image command failed to send (BUSY timeout)");
920 uint8_t device_type = 0, fw_major = 0, fw_minor = 0;
921 if (this->lr1121_firmware_updater_->read_normal_version(device_type, fw_major, fw_minor)) {
922 const uint16_t new_fw = (
static_cast<uint16_t
>(fw_major) << 8) | fw_minor;
923 lr1121_log_post_flash_verify_result(new_fw, LR1121_FIRMWARE_UPDATE_TARGET_VERSION);
925 ESP_LOGW(
detail::TAG,
"LR1121 bootloader rewrite: could not read back the post-flash version (BUSY timeout)");
InternalGPIOPin * rst_pin_
InternalGPIOPin * busy_pin_
SX1262/LR1121 BUSY pin.
Internal helpers shared by the hub implementation .cpp files.
Pure decision logic for the LR1121 transceiver-firmware-update feature.
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 uint16_t LR1121_BOOTLOADER_2101
constexpr uint16_t LR1121_LOADER_2100
Version the lr1121_loader_2100.bin bootloader-*loader* image reports of itself.
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,...
@ AVAILABLE
Three-stage is possible. Still requires the arming switch to actually run.
@ BLOCKED_BOOTLOADER_NEWER
Target needs an OLDER bootloader. Not reachable today, likely never.
@ BLOCKED_UNKNOWN_TARGET
Block present, but this build cannot know what the target needs.
@ NOT_APPLICABLE
No block, or no upgrade needed/possible to evaluate. Keep the original verdict.
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().
@ PROCEED
Safe to erase and write.
@ REJECT_WRONG_CHIP
device_type or bootloader_version doesn't identify an LR1121.
@ NEEDS_CONFIRMATION
Not unsafe, but not an unambiguous "yes" either — needs a second press.
@ REJECT_BOOTLOADER_TOO_OLD
target_fw is known and positively incompatible with this bootloader.
@ ALREADY_INSTALLED
target_fw == installed_fw (both known) — the post-success state.
constexpr uint8_t LR1121_BOOTLOADER_TYPE_FOR_FIRMWARE_DECISIONS
LR1121 GetVersion type byte reported while running the bootloader (LR11XX_TYPE_PRODUCTION_MODE) — mus...
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,...
@ TARGET_NEEDS_OLDER
The "too new" direction – a downgrade, newly reachable once this feature ships.
LR1121 bootloader-mode-*and*-loader-mode SPI transport, standalone from the running RadioDriver.