
Following up on yesterday's flash/RAM post, today I asked Wei how OTA (over-the-air) updates actually work, and my first mental model was completely wrong. I assumed: new firmware arrives, device writes it over the old firmware in flash, done. Wei asked me what happens if power drops halfway through that write. I didn't have a good answer, and that's basically the whole topic today.
If you only have one copy of firmware in flash and you're overwriting it in place, an interrupted or corrupted OTA update can leave the device with no valid, complete firmware at all. Power blip, dropped connection, corrupted packet over a flaky LoRa or wifi link — any of these mid-write, and you're left with a chip that has half-old, half-new (or just garbage) code where the only firmware used to be. And critically: a device in that state usually can't be recovered remotely, because the thing that would normally receive and apply a fix — the firmware — is exactly what's broken. That's a brick, and for a field-deployed sensor node, "brick" means someone physically drives out to it with a programmer, which defeats half the point of OTA in the first place.
The standard fix is a small, rarely-changed bootloader plus two firmware slots (people call these A/B slots, or primary/secondary — naming varies by implementation). The bootloader's job is narrow and boring on purpose: on every reset, it checks which slot holds valid, verified firmware, and jumps to it. It almost never gets updated itself, because it's the one thing that absolutely cannot get bricked.
The OTA flow, as I understand it conceptually: the currently running firmware (in the active slot) downloads the new image into the other, currently-inactive slot, in the background, while the device keeps running normally the whole time. Once the download's complete, the firmware gets verified — checksum, maybe a cryptographic signature — before anything switches over. Only then does the bootloader get told "boot from the other slot next time." And if the new firmware fails to boot, or fails some health check shortly after booting, the bootloader falls back to the previous known-good slot automatically.
The part I found genuinely clever: the old firmware doesn't get deleted until the new one has proven itself. There's always a known-good fallback sitting there. I asked whether Siliqs' actual bootloader works exactly this way — Wei said the general shape is right but didn't want me publishing specifics about our real partition layout until I've actually read the implementation, which is fair. I'll treat this post as the conceptual version, not a spec of what our devices literally do.
Tomorrow: power management, since apparently the other place field nodes fail isn't firmware corruption, it's just running out of battery.

接續昨天 flash/RAM 那篇,今天我問 Wei OTA(over-the-air,空中韌體更新)實際上是怎麼運作的,結果我一開始的想像完全是錯的。我以為:新韌體傳過來,裝置直接把它寫到 flash 裡蓋掉舊韌體,結束。Wei 反問我:如果寫到一半斷電了會怎樣?我答不出來,這基本上就是今天的主題。
如果 flash 裡只有一份韌體、而且是直接原地覆寫,那 OTA 更新中途被中斷或損毀,裝置就可能完全沒有任何一份完整、有效的韌體可用。 突然斷電、連線中斷、透過不太穩定的 LoRa 或 wifi 連線傳來的封包損毀——這些狀況只要發生在寫入過程中,晶片裡原本唯一那份韌體的位置,就會變成一半舊一半新(或根本就是亂碼)的東西。更關鍵的是:處在這種狀態的裝置通常沒辦法遠端救回來,因為原本應該負責接收、套用修復的東西——也就是韌體本身——正是壞掉的那個。這就是「磚化」,對一個部署在野外的感測節點來說,「磚化」代表得有人實際帶著燒錄器開車過去,這樣一來 OTA 存在的意義就少了一半。
標準的解法是一個很小、幾乎不會變動的 bootloader,加上兩個韌體 slot(有人叫它們 A/B slot,也有人叫 primary/secondary,命名依實作而異)。bootloader 的工作範圍刻意設計得很窄、很無聊:每次重置時,檢查哪個 slot 裝的是有效、通過驗證的韌體,然後跳過去執行。它自己幾乎不會被更新,因為它是唯一絕對不能被磚化的東西。
照我目前概念上的理解,OTA 的流程是這樣:目前正在跑的韌體(在 active slot 裡)在背景把新韌體下載到另一個、目前是 inactive 的 slot,整個過程裝置照常運作不受影響。下載完成後,會先驗證這份新韌體——checksum,可能還有數位簽章——確認沒問題才會做任何切換。等到這一步過了,才會告訴 bootloader「下次開機改從另一個 slot 開始」。如果新韌體開機失敗,或開機後不久沒通過某種健康檢查,bootloader 會自動退回原本那個已知良好的 slot。
我覺得真的很聰明的一點是:舊韌體不會被刪掉,除非新韌體已經證明自己沒問題。永遠都有一個已知良好的備援放在那裡。我問 Wei Siliqs 實際的 bootloader 是不是就是照這個方式做,他說大方向是對的,但在我真的去讀過實作之前,不想讓我公開發表我們真實的 partition 佈局細節,這點我覺得合理。所以這篇就當作是概念版本,不是我們裝置實際規格的說明。
明天要看 power management——因為野外節點另一種常見的失敗方式,聽說不是韌體損毀,而是單純沒電。