SDK

Esta página aún no está en español, así que se muestra en inglés.

AO_Boot

The Active Object that decides which firmware starts after a reset: otadata, the boot request, the leave before a reset, the home hold and the factory reset.

What it is

After every reset the bootloader picks a firmware. Two small records in the flash hold that choice. otadata names the kodeOS slot that starts when nothing else is asked. The boot request, a sector of the apps region, names one app to start instead. AO_Boot owns both records and is the only code that writes them. Which images the flash holds is AO_AppMgr's business (sdk/kaos/appmgr). Which one starts is AO_Boot's.

It runs in kodeOS and inside every app, and the build role decides what it writes. In an app it claims the records and writes nothing, so any reset comes back to kodeOS. In kodeOS it confirms a new update once the system is up, counts crashes, works out why the board started and how KodeOS ended before it, carries out four requests (switch slot, run an app, report, exit), says when kodeOS has stayed up a minute and runs the factory reset. Every restart it makes passes through a short leave first. A hold of the bottom button publishes HOME_SIG and, in an app, a hold of 3 s resets the board home even when the app is hung. In both roles it also writes an app's own stored strings (kode_sys_nvs_set()), so an AO never waits on the flash.

How it is connected

stateDiagram-v2
[*] --> Boot_active : claim, subscribe the hold, act on the role
Boot_active --> Boot_leaving : slot switch, app run, exit, home
Boot_active --> Boot_resetting : FACTORY_RESET_REQ_SIG
Boot_resetting --> Boot_shipping : a reset that ends in ship mode
Boot_active --> Boot_active : info, hold, defer, store
Boot_leaving --> [*] : khal_restart() after 45 ms
AO_Boot's two states
sequenceDiagram
autonumber
box "kodeOS"
  participant B as AO_Boot
  participant L as AO_Log
  participant N as AO_Net
  participant R as AO_Radio
end
B->>L: LOG_FLUSH_REQ_SIG, 40 ms bound
L-->>B: BOOT_LOG_FLUSHED_SIG
B->>N: NET_LEAVE_REQ_SIG, 200 ms bound
N-->>B: BOOT_NET_LEFT_SIG
B->>R: RADIO_PARK_REQ_SIG, 250 ms bound
R-->>B: BOOT_RADIO_PARKED_SIG
Note over B: wait 45 ms, then khal_restart()
The leave before a reset

AO_Boot in numbers:

ThingValue
Priority12
Queue8 events (KQP_BOOT_QUEUE_LEN)
Stack4096 bytes (KQP_AO_STACK_MEDIUM)
ClaimsKBSP_RES_OTADATA, KBSP_RES_APPS_BOOTREQ
Reads and writeskdrv_slot and kdrv_apps (sdk/kdrv/apps)
Subscribes toBUTTON_HOME_HOLD_SIG with KODEOS_HOME_HOLD, else BUTTON_HOLD_SIG, from AO_Buttons (sdk/kaos/buttons)
Storeskset/zone, kset/rescued, and an app's strings in kapp

In both roles its initial transition also restores the wall clock from the RTC or from the last value AO_Power kept (sdk/kaos/power).

The role

KODEOS_BOOT_ROLE_APP is the default, so a firmware that forgets to say what it is behaves like an app and cannot keep the board. KODEOS_BOOT_ROLE_OS is kodeOS itself. The initial transition then does four things in order:

  1. Work out why the board started. The bootloader marks a request TRIED before it jumps to an app, so a TRIED request means the board is back from an app. AO_Boot sets the reason KBOOT_FROM_APP, remembers the app's offset and clears the request. Any other state sets KBOOT_COLD, and a request still present is cleared as stale.
  2. Detect a failed update. Running from factory while otadata is not blank means an update died before it confirmed. AO_Boot first reads what became of the other slot (kboot_other_t) while otadata still says, stores kset/rescued, then sets KBOOT_UNEXPECTED and clears otadata, so the board stays on factory. The stored mark carries the rescue across a boot that comes before the stable minute, when otadata is already blank.
  3. Classify how the app ended, from the chip's reset reason (boot_app_end()). The same reason says how KodeOS itself ended when no app ran before the boot (boot_os_end()), since a fault after an app is the app's. After a panic or the interrupt or task watchdog, the resets a core dump explains (boot_dump_explains()), AO_Boot reads the coredump partition once with kdrv_slot_coredump() and keeps whether it holds a dump of a KodeOS image in flash (boot_os_dump()). The log says how kodeOS ended before this boot: <panic|watchdog>. The interface's fault notice reads both answers (internals/gui/sheets).
  4. Count crashes. The slot keeps a count of kodeOS crashes in a row. A panic or a watchdog reset adds one, a brownout changes nothing and any other reset clears it, and a reset after an app counts nothing. At KODE_BOOT_CRASHES_MAX (3) the slot is given up and the board restarts into the other one. Running from factory with nothing to go back to only logs it.

Confirming waits for the system to work. kdrv_slot_confirm_self() clears the PENDING_VERIFY mark of a freshly updated image. This is mandatory: esp_ota_begin() refuses to open an update session from an unconfirmed partition. AO_Boot confirms when the interface draws its first frame (BOOT_FIRST_FRAME_SIG), or KODE_BOOT_CONFIRM_MS (15 s) after boot_system_up(), so a board with no glass confirms too. Every deliberate restart and the wake from OFF confirm first. The crash count goes back to zero once kodeOS has stayed up KODE_BOOT_STABLE_MS (60 s).

The stable minute settles a kodeOS update. At KODE_BOOT_STABLE_MS, once per boot and in the kodeOS role only, AO_Boot publishes BOOT_STABLE_SIG. Its BootStableEvt carries running, where the partition running starts, and other, what became of the slot that is not running:

otherMeaning
KBOOT_OTHER_NEVER_RANNever launched: no switch to it took effect
KBOOT_OTHER_UNCONFIRMEDLaunched and never confirmed itself; the bootloader brought the board back
KBOOT_OTHER_GIVEN_UPLaunched, then given up on after it kept crashing

AO_AppMgr compares running with the slot it wrote and closes the install record of an install dst=os (sdk/kaos/appmgr). Publishing it erases kset/rescued. A switch that never reaches the leave (no inactive slot, no valid image in it, or otadata that will not write) publishes the bare BOOT_OS_SWITCH_FAIL_SIG instead, so the install that asked does not wait for a reset that never comes.

The leave

Every deliberate restart does its own write first (the slot, the boot request, or nothing), records the role of the firmware it goes to and enters Boot_leaving. There AO_Boot runs these steps:

  1. Ask AO_Log to write the lines already posted to it (log_flush_request()), so a line handed over before the jump, such as an install's last record, reaches the Kode channel's and the BLE console's rings before their ports go down.
  2. Ask AO_Net to stop the Kode channel cleanly.
  3. Ask AO_Radio to hang up Bluetooth and park the C5.
  4. Wait 45 ms (KODE_RESTART_DRAIN_MS), so the reply to the command that caused the reset leaves the console.
  5. Call khal_restart().

Each of the first three steps has its own bound on the leave timer, and an app that does not link AO_Net or AO_Radio skips that step. A silent neighbour delays the reset by its bound and no more. A full AO_Log queue skips the first step.

In Boot_leaving every other restart request and every home gesture is logged and dropped: the first request wins and the board resets once.

The hold

Which hold AO_Boot answers depends on the build. With KODEOS_HOME_HOLD (app role, on by default) it subscribes to BUTTON_HOME_HOLD_SIG, which AO_Buttons publishes once a press of the bottom button reaches KODE_HOME_HOLD_MS (3 s). The ordinary hold of the same button, BUTTON_HOLD_SIG at KODE_BUTTON_HOLD_MS (600 ms), then stays the app's own. Without the option, which is kodeOS and an app that turns it off, AO_Boot answers BUTTON_HOLD_SIG. Either way it publishes HOME_SIG, which the launcher of kodeOS or an AO of yours answers.

With KODEOS_HOME_HOLD it then arms the home timer for 200 ms (KODE_SYS_HOME_GRACE_MS) and resets the board home when it expires. An app that needs the time to save calls kode_sys_home_defer() from its HOME_SIG handler, writes, and calls kode_sys_home_now(). The wait is capped at 3 s from HOME_SIG, once, so the board resets 6 s after the press at the latest, plus the leave.

The app's store

kode_sys_nvs_set(key, value) posts BOOT_NVS_SET_SIG with a BootNvsSetEvt (key, value, both copied) and returns without waiting on the flash, so an Active Object or a kode_worker job may call it. AO_Boot writes each pair as a string to the namespace kapp (KODE_NVS_NAMESPACE), one write a step, in the order asked, where kode_sys_nvs_get() reads it back. A key is at most 15 characters and not empty (KODE_SYS_NVS_KEY_MAX 16, with the terminator), a value at most 191 (KODE_SYS_NVS_VALUE_MAX 192); anything else is KODE_DENIED and nothing is posted. Nobody is answered: a pair the store refuses is a warning in the log, the store refused '<key>' (status <n>); it is not kept. Each write goes through khal_nvs_set_str_within(), held to the apps' share of the store, KODE_NVS_SHARE_MAX (16384 bytes). A pair that would pass it is not written, and the warning says '<key>' is not kept: the apps' share of the store (16384 bytes) is full.

A pair asked before kode_sys_exit_to_os() or kode_sys_home_now() is written before the board leaves, since it sits ahead of the request in the queue. One that reaches AO_Boot once the leave has begun is dropped (already leaving; this setting is dropped), and so is one during a factory reset (busy with a factory reset; this setting is dropped).

Resting with the board

AO_Boot owns no device that sleeps, but it joins the rounds of AO_LowPower as boot (sdk/kaos/lowpower). A board that went to sleep between the write of a boot request and the reset would wake into the old firmware, so AO_Boot answers a round busy while a restart is on its way and ready otherwise:

  • home pending while an app's deferral of the hold is running (kode_sys_home_defer()), which is a restart a moment away;
  • restart pending in Boot_leaving, from the write of the request to the reset;
  • factory reset while a factory reset runs, so the board cannot rest in the middle of it.

AO_Boot restores nothing when the board wakes. A round for OFF confirms a new kodeOS slot first, because the wake from OFF is a reset.

The factory reset

In the kodeOS role AO_Boot runs the factory reset, the console's factory_reset confirm=on, in Boot_resetting. It keeps the identity cache (kident: the C5's MAC and the board's hardware revision, board_rev), the station lock and the welcome app. Each step has a budget, and a silent owner ends the reset at its step. What can fail comes first, so a failed step leaves the apps and the settings where they were. The steps, in order:

  1. find: where the welcome app is, installing it from the card if need be. Nothing is erased before it is found.
  2. link: the C5 link up, when it was not.
  3. install: the welcome app written from the card.
  4. wipe: the C5's saved networks and BLE keys, and this chip's bond store.
  5. apps: every other app removed.
  6. files: AO_Storage takes every other app's images and data folders off both media (storage_apps_clear_request(), sdk/kaos/storage). It keeps the user's files, the kodeOS and C5 images, images built for other chips, the face's pack, and the welcome app's folder and its image on the card, whether or not it is installed. The step's budget is 90 s (KODE_RESET_FILES_MS). A clear refused for a transfer is asked again after 500 ms (KODE_RESET_FILES_AGAIN_MS), inside that budget; any other refusal stops the reset at this step. A file that stays, or a medium not looked at whole, is a warning (some of the apps' files stay or were not looked at; the reset goes on), and the reset goes on.
  7. nvs: every other NVS namespace erased.

The reset ends with KBOOT_RESET_RESTART, a restart into the welcome app at once, with no radio park and no clean leave of the Kode channel. With KBOOT_RESET_SHIP AO_Boot waits in Boot_shipping for the cable to leave (KODE_SHIP_UNPLUG_MS, 15 s), erases NVS again and ships. boot_factory_reset_request(end, who) asks for it, and one FACTORY_RESET_RSP_SIG answers with the step that stopped it or done. The console reaches only the restart end. While a reset runs AO_Boot answers a round of AO_LowPower busy (factory reset) and refuses every other request.

The time zone

AO_Boot owns the setting zone (kset/zone, internals/settings). It applies the stored zone in its initial transition, before an app runs, so the face, the FAT timestamps and an app's localtime_r() read the same local time from the first second.

A zone must be a POSIX TZ string, std offset [dst [offset] [,start[/time],end[/time]]], of at most 32 characters. config key="zone" refuses anything else with >err code=5 msg=value and stores nothing, so text the C library would read as no zone, or as an offset of days, never reaches the face. A stored zone that is not one, left by an older build, is ignored at boot: the clock runs in UTC and the log says zone: UTC0 (none stored). A zone the C library has no memory for is not stored either, and the log names the zone that stays.

Specifications

The requests

SignalEventFromWhat AO_Boot does
BOOT_OS_SWITCH_SIGnoneAO_AppMgr, after a verified install dst=osRefuses with a log line and BOOT_OS_SWITCH_FAIL_SIG if the inactive slot is absent or holds no valid image. Otherwise kdrv_otadata_select_slot() verifies the image and writes otadata, then the leave and the reset; a failed write is refused the same way
BOOT_APP_RUN_SIGBootRunEvt (offset, size, mode)AO_AppMgr: app_run, install and run, mailbox adoptionkdrv_apps_req_set() writes the request, then the leave. KBOOT_RUN_ONCE writes NEW, KBOOT_RUN_STICKY writes ALWAYS. A failed write logs and does not reset
BOOT_INFO_REQ_SIGnoneAO_Console (boot_info), AO_Autotest (test component=boot)Publishes BOOT_STATE_SIG and logs the inactive slot's image
BOOT_EXIT_REQ_SIGnonekode_exit(), kode_sys_exit_to_os()The leave, then the reset. Writes nothing
BOOT_HOME_DEFER_SIGnonekode_sys_home_defer()Honoured only while the grace runs. Extends it once
BOOT_HOME_NOW_SIGnonekode_sys_home_now()Disarms the home timer, then the leave
BUTTON_HOME_HOLD_SIG, or BUTTON_HOLD_SIG without KODEOS_HOME_HOLDButtonHoldEvtAO_Buttons, subscribedOnly the bottom button counts: publishes HOME_SIG and, with KODEOS_HOME_HOLD, arms the grace
BOOT_NVS_SET_SIGBootNvsSetEvt (key, value)kode_sys_nvs_set()Writes the string to kapp. Answers nobody; dropped while leaving or during a factory reset
BOOT_LOG_FLUSHED_SIG, BOOT_NET_LEFT_SIG, BOOT_RADIO_PARKED_SIGnoneAO_Log, AO_Net, AO_RadioThe answers to the three steps of the leave
FACTORY_RESET_REQ_SIGFactoryResetReqEvt (end, who)boot_factory_reset_request(), the consoleRuns the factory reset (kodeOS role)
LOWPOWER_ROUND_SIGLowPowerRoundEvtAO_LowPower, subscribed with CONFIG_KODE_LOWPOWERAnswers ready, or busy while a restart is on its way

The private signals are BOOT_LEAVE_TMO_SIG, the bound of the leave step that is running, BOOT_HEALTHY_TMO_SIG, the confirmation and then the stable minute, and BOOT_HOME_GRACE_SIG, the grace and then the cap. A build that does not link AO_Net or AO_Radio skips that step of the leave.

What it sends

SignalEventRangeWhen
BOOT_STATE_SIGBootStateEvt: running[20], address, size, reason, otadata_blank, app_endpublishIn answer to BOOT_INFO_REQ_SIG. The console closes boot_info with it
BOOT_STABLE_SIGBootStableEvt: running, other (kboot_other_t)publishOnce per boot, kodeOS role, after KODE_BOOT_STABLE_MS
BOOT_OS_SWITCH_FAIL_SIGnonepublishA slot switch that stays on this slot
HOME_SIGnonepublishOn every bottom-button hold AO_Boot answers, in both roles
LOG_FLUSH_REQ_SIGnonepost to AO_LogFirst step of a leave
NET_LEAVE_REQ_SIGnonepost to AO_NetSecond step of a leave
RADIO_PARK_REQ_SIGRadioParkEvt (target: the role of the firmware the jump goes to)post to AO_RadioThird step of a leave

The states of a boot

ValueMeaning
KBOOT_COLDPower-on or a plain reset, and no app was running
KBOOT_FROM_APPThe bootloader tried an app and the board is back
KBOOT_UNEXPECTEDA kodeOS update never confirmed; the rescue copy on factory took over
KBOOT_APP_END_NONEThis boot is not back from an app
KBOOT_APP_END_CLEANAny way home that is not one of the next three: a restart, BOT+LEFT, the USB port opening
KBOOT_APP_END_PANICReset reason panic or CPU lockup
KBOOT_APP_END_WATCHDOGInterrupt, task or other watchdog
KBOOT_APP_END_BROWNOUTBrownout or power glitch
KBOOT_OS_END_NONENo fault of KodeOS before this boot: an app ran before it, or the reset was no panic and no watchdog. Always this in the app role
KBOOT_OS_END_PANICKodeOS ended in a panic or a CPU lockup
KBOOT_OS_END_WATCHDOGKodeOS ended in any watchdog

An unknown reset reason reads as clean, or as KBOOT_OS_END_NONE, and never as a crash.

The timings

ConstantValueWhat it bounds
KODE_HOME_HOLD_MS3000 msThe press of the bottom button that is the home hold, with KODEOS_HOME_HOLD
KODE_SYS_HOME_GRACE_MS200 msFrom HOME_SIG to the reset, if no app asks for more
KODE_SYS_HOME_DEFER_MAX_MS3000 msThe longest an app can hold the reset, counted from HOME_SIG
KODE_LEAVE_LOG_MS40 msAO_Log writing the lines posted before the leave
KODE_LEAVE_NET_MS200 msAO_Net's clean stop of the Kode channel
KODE_LEAVE_RADIO_MS250 msAO_Radio parking the C5
KODE_RESTART_DRAIN_MS45 msThe pause before khal_restart()
KODE_BOOT_CONFIRM_MS15 sHow long a new kodeOS slot waits for the first frame before it confirms itself
KODE_BOOT_STABLE_MS60 sHow long kodeOS must stay up before its crash count returns to zero
KODE_BOOT_CRASHES_MAX3Crashes in a row after which the slot is given up
KODE_LEAVE_BUDGET_MS500 msA _Static_assert keeps the three leave bounds under it
KODE_RESET_FILES_MS90 sThe factory reset's files step

The calls

CallWhat it does
kode_exit()A task's way home: asks kode_sys_exit_to_os() again until it is taken, then waits for the reset
kode_sys_exit_to_os()Posts BOOT_EXIT_REQ_SIG. The polite way home, from an AO or a task
kode_sys_home_defer()KODE_OK once noted, KODE_DENIED when no hold is pending. Call it from the HOME_SIG handler; on KODE_DENIED ask again a few milliseconds later
kode_sys_home_now()The app is ready: go home now
kode_sys_nvs_set(key, value)Posts the pair for AO_Boot to store in kapp. KODE_DENIED for an unusable key or value, KODE_NO_RESOURCES when the queue has no room; safe from an AO, a task or a kode_worker job
boot_reason(), boot_app_end(), boot_app_offset()Why this boot happened, how the app before it ended, and which app it was (KODE_APP_OFFSET_NONE when none)
boot_os_end(), boot_os_dump()How KodeOS ended before this boot (kboot_os_end_t), and whether that fault left a core dump of a KodeOS image in flash. KBOOT_OS_END_NONE and false in the app role
boot_os_end_name(e)none, panic or watchdog; ? for any other value
boot_os_end_of(reason, after_app), boot_dump_explains(reason)The pure classes behind them: the end for a reset reason, and whether a core dump can explain that reset (panic, interrupt watchdog, task watchdog)
boot_os_switch_request(), boot_app_run_request(), boot_info_request(), boot_exit_evt()The four requests above, from kaos.h
boot_factory_reset_request(end, who)Ask for the factory reset; kaos.h
boot_home_pending()True while a deferral would still be honoured. Always false without KODEOS_HOME_HOLD

AO_Boot has one stored setting, zone, keeps the rescue mark kset/rescued (a u8, absent when no rescue is owed) and has no console verb of its own besides boot_info. The options are KODEOS_BOOT_ROLE_APP or _OS and KODEOS_HOME_HOLD, which depends on the app role.

Gotchas

Example code

Light the LED blue for three seconds, then hand the machine back to kodeOS. With the app running the LED turns blue, and after three seconds the screen shows the launcher of kodeOS again.

/*
 * home_after_3s.c - from Kode with love
 *
 * Lights the status LED blue, waits three seconds, then asks AO_Boot to
 * leave the app. AO_Boot runs its short leave and resets, and the bootloader
 * brings kodeOS back. On a Kode Dot the LED goes blue, then the launcher
 * returns.
 */
#include "kode.h"
 
/* Bring the system up, show a colour, wait, then go home. */
void app_main(void)
{
    kode_init();
    kode_led_set_rgb(0U, 0U, 64U);
    kode_delay(3000U);
    kode_exit();
}

Compiled against the 0.1.1 SDK with ESP-IDF v6.1 on 2026-11-11. Not run on a Kode Dot.

In the SDK

  • components/kaos/boot: boot_ao.c, the state machine, the leave and the hold.
  • components/kdrv/apps: kdrv_slot and kdrv_apps, the otadata and boot-request primitives AO_Boot calls.
  • kode.h: kode_exit(); kode_sys.h: kode_sys_exit_to_os(), kode_sys_home_defer(), kode_sys_home_now() and kode_sys_nvs_set().