K-FSW v1.2.1-59-gcaca356
KFSW: modular flight software on Zephyr, for small satellites
Loading...
Searching...
No Matches
Firmware update

Data Structures

struct  kfsw_fwu_owner
 
struct  kfsw_fwu_image
 
struct  kfsw_fwu_handle
 
struct  kfsw_fwu_status
 

Macros

#define KFSW_FWU_SHA256_SIZE   32U
 

Enumerations

enum  kfsw_fwu_state {
  KFSW_FWU_IDLE = 0 , KFSW_FWU_RECEIVING = 1 , KFSW_FWU_READY = 2 , KFSW_FWU_FAILED = 3 ,
  KFSW_FWU_VERIFIED = 4
}
 
enum  kfsw_fwu_owner_kind { KFSW_FWU_OWNER_LOCAL = 0 , KFSW_FWU_OWNER_FTP = 1 , KFSW_FWU_OWNER_LITE = 2 }
 

Functions

int kfsw_fwu_session_begin (const struct kfsw_fwu_owner *owner, const struct kfsw_fwu_image *image, uint32_t transfer_id, struct kfsw_fwu_handle *handle, struct kfsw_fwu_status *status)
 
int kfsw_fwu_session_write (const struct kfsw_fwu_handle *handle, uint32_t offset, const void *data, size_t size, struct kfsw_fwu_status *status)
 
int kfsw_fwu_session_verify (const struct kfsw_fwu_handle *handle, struct kfsw_fwu_status *status)
 
int kfsw_fwu_session_activate (const struct kfsw_fwu_handle *handle, struct kfsw_fwu_status *status)
 
int kfsw_fwu_session_abort (const struct kfsw_fwu_handle *handle, struct kfsw_fwu_status *status)
 
int kfsw_fwu_session_lookup (const struct kfsw_fwu_owner *owner, struct kfsw_fwu_status *status)
 
int kfsw_fwu_session_status (const struct kfsw_fwu_handle *handle, struct kfsw_fwu_status *status)
 
uint32_t kfsw_fwu_max_image_size (void)
 
uint32_t kfsw_fwu_slot_write_offset (void)
 
int kfsw_fwu_begin (uint32_t total_size, uint32_t expected_crc32)
 
int kfsw_fwu_write (uint32_t offset, const void *data, size_t size)
 
int kfsw_fwu_verify (void)
 
int kfsw_fwu_files_mount (void)
 
int kfsw_fwu_finish (void)
 
int kfsw_fwu_abort (void)
 
int kfsw_fwu_get_status (struct kfsw_fwu_status *status)
 
const char * kfsw_fwu_state_name (enum kfsw_fwu_state state)
 

Description

Receives a firmware image into the secondary image slot and asks the bootloader to try it on the next boot.

Images are written directly to flash. With KFSW_FWU_FILES, both slots are also available as read-only files, without using data filesystem space. The service handles the MCUboot write offset and checks upgrade scheduling.

With KFSW_FWU_RECOVERY, permanent Golden replaces primary A directly. The whole partition is available, READY schedules no swap, and finish never resets the board. Begin destroys previous A; there is no previous-A rollback. MCUboot must reject invalid A and execute Golden after an interrupted upload.

The CRC32 catches corruption in transit, not tampering. Authenticity is the bootloader's signature check.

Macro Documentation

◆ KFSW_FWU_SHA256_SIZE

#define KFSW_FWU_SHA256_SIZE   32U

#include <fwu.h>

Whole-artifact SHA-256 length in bytes.

Enumeration Documentation

◆ kfsw_fwu_owner_kind

#include <fwu.h>

Backend transfer principal.

Peer identity is supplied by the transport.

Enumerator
KFSW_FWU_OWNER_LOCAL 

Ordinary local API (peer zero), or Golden installation (peer one).

KFSW_FWU_OWNER_FTP 

Reserved firmware upload through FTP; peer is the source node.

KFSW_FWU_OWNER_LITE 

Lightweight upload; peer is the source node across connections.

◆ kfsw_fwu_state

#include <fwu.h>

Transfer state.

Enumerator
KFSW_FWU_IDLE 

No transfer in progress.

KFSW_FWU_RECEIVING 

A transfer has begun and is accepting data.

KFSW_FWU_READY 

Fully received and verified; swap scheduled only with swap integration.

KFSW_FWU_FAILED 

Failed; use owned abort before retrying (local transfers may use local abort).

KFSW_FWU_VERIFIED 

Checked and flushed to flash; no upgrade requested yet.

Function Documentation

◆ kfsw_fwu_abort()

int kfsw_fwu_abort ( void  )

#include <fwu.h>

Abandon a transfer and return to idle.

On success the secondary slot is erased. If cleanup fails, the service retains its transfer details in the failed state; call again to retry. Erasing the rollback image during a trial boot is refused.

Return values
0Cleanup completed, or no target partition is bound.
-EBUSYA secondary-slot reader is open.
Returns
A negative errno from flash on cleanup failure.

◆ kfsw_fwu_begin()

int kfsw_fwu_begin ( uint32_t  total_size,
uint32_t  expected_crc32 
)

#include <fwu.h>

Begin a CRC-only local transfer, erasing the available slot.

Local mutation APIs refuse remote ownership (-EACCES). Begin returns -EBUSY for a conflicting session.

Parameters
total_sizeImage size in bytes.
expected_crc32CRC32 (IEEE, as produced by Zephyr's crc32_ieee) over the whole image.
Return values
0The slot is ready to receive.
-ENODEVNo target partition is bound.
-EINVALtotal_size is zero.
-EFBIGtotal_size exceeds kfsw_fwu_max_image_size.
-EBUSYAn unfinished session or another owner holds the slot, a secondary reader is open, or MCUboot needs trial rollback.
Returns
A negative errno value from the flash layer on failure.

◆ kfsw_fwu_files_mount()

int kfsw_fwu_files_mount ( void  )

#include <fwu.h>

Mount read-only MCUboot images at /kfsw/boot.

firmware_1.bin reads slot0; firmware_2.bin reads slot1. Files contain the image header, payload, and TLVs, excluding slot padding and swap metadata. A secondary reader blocks erase and upgrade requests until it closes. Image visibility checks structure, not the bootloader's signature policy. Call once during service startup, before exposing file transfers.

Returns
0 on success, or a negative errno on failure.

◆ kfsw_fwu_finish()

int kfsw_fwu_finish ( void  )

#include <fwu.h>

Verify the received image and offer it to the bootloader.

Repeated finish of a ready transfer succeeds without another boot request or counter increment. On success the image is marked to be tried once. It becomes permanent only if it confirms itself after booting; otherwise the bootloader restores the previous image.

Recovery instead leaves A ready for signature verification on an explicitly commanded reset. It neither schedules a swap nor resets the board.

Return values
0The image was accepted; swap_scheduled reports swap integration.
-EINVALNo transfer is receiving or verified.
-EBUSYA secondary-slot reader is open.
-EAGAINFewer bytes were received than declared.
-EILSEQThe CRC32 does not match what the sender declared.
-EIOThe bootloader did not schedule a swap despite being asked.
Returns
A negative errno value from the flash layer on failure.

◆ kfsw_fwu_get_status()

int kfsw_fwu_get_status ( struct kfsw_fwu_status *  status)

#include <fwu.h>

Read a consistent snapshot of the update state.

Parameters
[out]statusDestination snapshot.
Return values
0The snapshot was written.
-EINVALstatus is NULL.

◆ kfsw_fwu_max_image_size()

uint32_t kfsw_fwu_max_image_size ( void  )

#include <fwu.h>

Largest image the target slot can hold, in bytes.

Smaller than the partition: the swap offset costs one sector at the start, and the bootloader needs its trailer at the end. Recovery uses the full partition.

Returns
The maximum image size, or zero when no target is bound.

◆ kfsw_fwu_session_abort()

int kfsw_fwu_session_abort ( const struct kfsw_fwu_handle *  handle,
struct kfsw_fwu_status *  status 
)

#include <fwu.h>

Erase this owned transfer; retain identity on failure and a tombstone on success.

◆ kfsw_fwu_session_activate()

int kfsw_fwu_session_activate ( const struct kfsw_fwu_handle *  handle,
struct kfsw_fwu_status *  status 
)

#include <fwu.h>

Offer an explicitly VERIFIED image; READY replay does not schedule again.

◆ kfsw_fwu_session_begin()

int kfsw_fwu_session_begin ( const struct kfsw_fwu_owner *  owner,
const struct kfsw_fwu_image *  image,
uint32_t  transfer_id,
struct kfsw_fwu_handle *  handle,
struct kfsw_fwu_status *  status 
)

#include <fwu.h>

Acquire or resume the canonical writer, returning an atomic snapshot.

Only digest-bearing lite descriptors may resume an identical owner, descriptor and transfer ID without erasing. CRC-only begin is non-resumable. Transfer ID zero requests a backend ID for CRC-only callers; digest sessions require a nonzero ID and block size. Generation never wraps during this boot. A completed CRC-only session may be replaced by the same owner, preserving ordinary FWU upload behavior. Failed sessions require owned abort. Successful abort retains one tombstone.

All session operations validate and act under the same lock, and fill status even on operational errors. Once acquisition starts, handle is returned even if flash preparation fails, allowing owner-scoped cleanup. Refused acquisition leaves the output handle unchanged. Arguments and output pointers must be non-NULL. Owner mismatch returns -EACCES; superseded identity returns -ESTALE; conflicting begin returns -EBUSY. Handles are RAM-only and do not survive receiver reset. Digest verification establishes content integrity, not authentication.

◆ kfsw_fwu_session_lookup()

int kfsw_fwu_session_lookup ( const struct kfsw_fwu_owner *  owner,
struct kfsw_fwu_status *  status 
)

#include <fwu.h>

Resolve the current transfer owned by this principal, without mutating it.

Returns -ENOENT for no active identity, -EACCES for another owner. Bind subsequent operations to the returned handle; never resolve again on retry.

◆ kfsw_fwu_session_status()

int kfsw_fwu_session_status ( const struct kfsw_fwu_handle *  handle,
struct kfsw_fwu_status *  status 
)

#include <fwu.h>

Query this handle with the same ownership and generation checks as mutations.

◆ kfsw_fwu_session_verify()

int kfsw_fwu_session_verify ( const struct kfsw_fwu_handle *  handle,
struct kfsw_fwu_status *  status 
)

#include <fwu.h>

Flush and check exactly the stored image bytes against CRC and optional SHA-256.

◆ kfsw_fwu_session_write()

int kfsw_fwu_session_write ( const struct kfsw_fwu_handle *  handle,
uint32_t  offset,
const void *  data,
size_t  size,
struct kfsw_fwu_status *  status 
)

#include <fwu.h>

Accept the next ordered span and return its atomic snapshot.

Digest-bearing lite sessions replay only the exact last block, including a short tail, without another write. Changed duplicates and older offsets fail.

◆ kfsw_fwu_slot_write_offset()

uint32_t kfsw_fwu_slot_write_offset ( void  )

#include <fwu.h>

Byte offset within the target partition where an image is written.

Non-zero because of the bootloader's swap mode; zero in recovery mode.

Returns
The offset in bytes.

◆ kfsw_fwu_state_name()

const char * kfsw_fwu_state_name ( enum kfsw_fwu_state  state)

#include <fwu.h>

Human-readable name for a state, for shells and logs.

Parameters
stateOne of kfsw_fwu_state.
Returns
A stable lowercase name; "unknown" for an unrecognised value.

◆ kfsw_fwu_verify()

int kfsw_fwu_verify ( void  )

#include <fwu.h>

Flush received bytes and independently check the stored image CRC.

Reads exactly the declared image bytes, excluding flash padding. Does not schedule a boot. Repeated verification of a verified or ready transfer succeeds. Further writes are rejected until a new transfer begins.

Return values
0The complete image is available in flash.
-EINVALNo transfer is receiving or verified.
-EAGAINThe transfer is incomplete.
-EILSEQThe transfer CRC does not match.
Returns
A negative errno from flash on failure.

◆ kfsw_fwu_write()

int kfsw_fwu_write ( uint32_t  offset,
const void *  data,
size_t  size 
)

#include <fwu.h>

Accept the next span of image bytes.

Spans must arrive in order and without gaps.

Parameters
offsetOffset of this span within the image, from zero.
dataBytes to write.
sizeNumber of bytes.
Return values
0The span was accepted.
-EINVALdata is NULL, size is zero, or no transfer is running.
-ESPIPEoffset is not where the transfer had reached.
-EFBIGThe span would run past the declared image size.
Returns
A negative errno value from the flash layer on failure.