Files
meshcore-tth/src/helpers/FwPatch.h
T

78 lines
3.2 KiB
C

#pragma once
// CUSTOM (TeTeHacko): in-place delta update of the running image, the core of
// FwUpdate's `format 2` packages. Plain C on purpose: the very same file runs in
// the native unit test (fake flash) and on the node, where its code is moved to
// RAM (tools/ota_ramlib.py), because a real run rewrites the flash the firmware
// executes from.
//
// The patch is a detools "in-place" patch (heatshrink 11/10, segment = one flash
// page). detools first moves the old image up by `shift` bytes, then writes the
// new image page by page from the start, reading old bytes only above the page
// it is writing. Two modes, one code path:
//
// dry no flash is touched. Reads from the shifted area are redirected to the
// untouched original, every page of the new image goes to `sink` in
// order (hash + CRC). A read the real run could not serve (data already
// overwritten) fails the run. This is `fw verify`.
// real erase/write through the callbacks, pages buffered in RAM so the flash
// only ever sees whole, word-aligned pages. This is `fw apply`.
#include <stddef.h>
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
#define FWP_PAGE 4096u
// Error codes (negative). detools errors come through as -(100 + detools code).
#define FWP_ERR_ARGS -1 // bad job (alignment, sizes)
#define FWP_ERR_SHIFT -2 // shift not a positive multiple of a page
#define FWP_ERR_OVERREAD -3 // the patch reads data a real run would have overwritten
#define FWP_ERR_ORDER -4 // a write outside the page being built
#define FWP_ERR_SIZE -5 // header sizes do not match the job
#define FWP_ERR_DETOOLS -100
typedef struct fwp_job {
// in
uintptr_t mem_base; // address of memory offset 0 (APP_START on the node)
uint32_t mem_size; // must equal the patch's memory_size
uint32_t from_size; // must equal the patch's from_size (the running image)
const uint8_t *patch; // memory-mapped patch
uint32_t patch_size;
int dry;
void (*erase)(uintptr_t addr); // real: one page
void (*write)(uintptr_t addr, const uint8_t *src, uint32_t n); // real: one page
void (*sink)(void *ctx, const uint8_t *p, uint32_t n); // dry: new image, in order
void *sink_ctx;
void (*tick)(void); // optional, called once per page (watchdog)
// out
uint32_t to_size; // size of the new image
uint32_t shift;
int detools_err;
// private
void *dt; // struct detools_apply_patch_in_place_t *
uintptr_t page_addr; // page being built (memory offset), or UINTPTR_MAX
uint32_t shift_low; // shifted area so far, [shift_low, shift_high) (dry)
uint32_t shift_high;
uint32_t written_end; // end of the new image written so far (memory offset)
int err;
} fwp_job;
// Runs the whole patch. 0 = success (to_size set), negative = FWP_ERR_*.
int fwp_run(fwp_job *j);
// Reads only the patch header: memory_size, segment_size, shift_size, from_size,
// to_size. 0 on success. Used before the transfer completes and on the PC side
// tests.
int fwp_header(const uint8_t *patch, uint32_t n, uint32_t *memory_size, uint32_t *segment_size,
uint32_t *shift, uint32_t *from_size, uint32_t *to_size);
#ifdef __cplusplus
}
#endif