FwUpdate: format 2 delta packages (FwPatch in RAM), base checked with the manifest
This commit is contained in:
1 parent
8d3e554ca3
commit
aaca10958e
6 files changed
+225
-43
No files matched your search
@@ -0,0 +1,214 @@
|
||||
// CUSTOM (TeTeHacko): in-place delta update, see FwPatch.h.
|
||||
//
|
||||
// C++ file on purpose: `helpers/*.cpp` is in every nRF52 env's build_src_filter,
|
||||
// a .c file would need a filter entry in each of them. detools compiles as C++
|
||||
// unchanged. Only nRF52 and the native test build it (ARDUINO without
|
||||
// NRF52_PLATFORM = ESP32/RP2040/STM32, nothing to do there).
|
||||
//
|
||||
// This translation unit compiles the vendored detools + heatshrink decoder
|
||||
// (src/helpers/fwpatch/detools, unmodified, see README.tth.md there) together
|
||||
// with the driver below. On the node tools/ota_ramlib.py moves the whole object
|
||||
// to RAM and redirects memcpy/memset to the local copies at the bottom, so not a
|
||||
// single instruction or table of the real run lives in the flash it rewrites
|
||||
// (tools/ota_check_ramfunc.sh checks the result).
|
||||
|
||||
#if defined(NRF52_PLATFORM) || !defined(ARDUINO)
|
||||
|
||||
// --- vendored code, configured here instead of editing it ------------------------
|
||||
#define HEATSHRINK_CONFIG_H // skip the vendored config, these values win
|
||||
#define HEATSHRINK_DYNAMIC_ALLOC 0
|
||||
#define HEATSHRINK_STATIC_INPUT_BUFFER_SIZE 256
|
||||
#define HEATSHRINK_STATIC_WINDOW_BITS 11 // tools/ota_pack.py must use the same
|
||||
#define HEATSHRINK_STATIC_LOOKAHEAD_BITS 10
|
||||
#define HEATSHRINK_DEBUGGING_LOGS 0
|
||||
#define HEATSHRINK_USE_INDEX 0
|
||||
|
||||
#define DETOOLS_CONFIG_FILE_IO 0
|
||||
#define DETOOLS_CONFIG_COMPRESSION_NONE 0
|
||||
#define DETOOLS_CONFIG_COMPRESSION_LZMA 0
|
||||
#define DETOOLS_CONFIG_COMPRESSION_CRLE 0
|
||||
#define DETOOLS_CONFIG_COMPRESSION_HEATSHRINK 1
|
||||
|
||||
#include "FwPatch.h"
|
||||
|
||||
#include "fwpatch/detools/detools.c"
|
||||
#include "fwpatch/detools/heatshrink/heatshrink_decoder.c"
|
||||
|
||||
// --- driver --------------------------------------------------------------------
|
||||
|
||||
// keeps GCC from turning these byte loops back into memcpy/memset calls
|
||||
#define FWP_NOLIBC __attribute__((optimize("no-tree-loop-distribute-patterns")))
|
||||
|
||||
static uint8_t g_page[FWP_PAGE];
|
||||
static struct detools_apply_patch_in_place_t g_dt;
|
||||
|
||||
#define NO_PAGE ((uintptr_t)-1)
|
||||
|
||||
FWP_NOLIBC static void fwp_copy(uint8_t *d, const uint8_t *s, uint32_t n) {
|
||||
while (n--)
|
||||
*d++ = *s++;
|
||||
}
|
||||
|
||||
static void flush_page(fwp_job *j) {
|
||||
if (j->page_addr == NO_PAGE) return;
|
||||
if (!j->dry) j->write(j->mem_base + j->page_addr, g_page, FWP_PAGE);
|
||||
j->page_addr = NO_PAGE;
|
||||
}
|
||||
|
||||
static int in_shift_phase(void) {
|
||||
return g_dt.state == detools_apply_patch_state_init_t;
|
||||
}
|
||||
|
||||
static int cb_read(void *arg, void *dst, uintptr_t src, size_t size) {
|
||||
fwp_job *j = (fwp_job *)arg;
|
||||
uint8_t *d = (uint8_t *)dst;
|
||||
if (src + size > j->mem_size) return j->err = FWP_ERR_ARGS;
|
||||
if (!in_shift_phase() && src < j->written_end) return j->err = FWP_ERR_OVERREAD;
|
||||
while (size > 0) {
|
||||
uintptr_t a = src;
|
||||
size_t n = size;
|
||||
if (j->dry && src >= j->shift_low && src < j->shift_high) {
|
||||
// the real run moved this page up by `shift`; the original still sits there
|
||||
a = src - j->shift;
|
||||
if (src + n > j->shift_high) n = j->shift_high - src;
|
||||
} else if (j->dry && src < j->shift_low && src + n > j->shift_low) {
|
||||
n = j->shift_low - src;
|
||||
}
|
||||
fwp_copy(d, (const uint8_t *)(j->mem_base + a), (uint32_t)n);
|
||||
d += n;
|
||||
src += n;
|
||||
size -= n;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int cb_erase(void *arg, uintptr_t addr, size_t size) {
|
||||
fwp_job *j = (fwp_job *)arg;
|
||||
if ((addr % FWP_PAGE) != 0 || size == 0 || size > FWP_PAGE || addr + FWP_PAGE > j->mem_size)
|
||||
return j->err = FWP_ERR_ARGS;
|
||||
flush_page(j);
|
||||
if (j->tick) j->tick();
|
||||
if (in_shift_phase()) {
|
||||
if (j->dry) {
|
||||
if (addr < j->shift_low) j->shift_low = (uint32_t)addr;
|
||||
if (addr + FWP_PAGE > j->shift_high) j->shift_high = (uint32_t)(addr + FWP_PAGE);
|
||||
}
|
||||
} else {
|
||||
if (addr != j->written_end) return j->err = FWP_ERR_ORDER;
|
||||
j->written_end = (uint32_t)(addr + size);
|
||||
}
|
||||
if (!j->dry) j->erase(j->mem_base + addr);
|
||||
for (uint32_t i = 0; i < FWP_PAGE; i++)
|
||||
g_page[i] = 0xFF;
|
||||
j->page_addr = addr;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int cb_write(void *arg, uintptr_t dst, void *src, size_t size) {
|
||||
fwp_job *j = (fwp_job *)arg;
|
||||
if (j->page_addr == NO_PAGE || dst < j->page_addr || dst + size > j->page_addr + FWP_PAGE)
|
||||
return j->err = FWP_ERR_ORDER;
|
||||
fwp_copy(&g_page[dst - j->page_addr], (const uint8_t *)src, (uint32_t)size);
|
||||
if (j->dry && !in_shift_phase()) j->sink(j->sink_ctx, (const uint8_t *)src, (uint32_t)size);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int get_usize(const uint8_t *p, uint32_t n, uint32_t *pos, uint32_t *out) {
|
||||
uint32_t v = 0, off = 0;
|
||||
uint8_t b;
|
||||
if (*pos >= n) return -1;
|
||||
b = p[(*pos)++];
|
||||
if (b & 0x40) return -1; // negative
|
||||
v = b & 0x3f;
|
||||
off = 6;
|
||||
while (b & 0x80) {
|
||||
if (*pos >= n || off > 28) return -1;
|
||||
b = p[(*pos)++];
|
||||
v |= (uint32_t)(b & 0x7f) << off;
|
||||
off += 7;
|
||||
}
|
||||
*out = v;
|
||||
return 0;
|
||||
}
|
||||
|
||||
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) {
|
||||
uint32_t pos = 1;
|
||||
if (n < 1 || ((patch[0] >> 4) & 0x7) != PATCH_TYPE_IN_PLACE) return -1;
|
||||
if ((patch[0] & 0xf) != COMPRESSION_HEATSHRINK) return -2;
|
||||
if (get_usize(patch, n, &pos, memory_size) || get_usize(patch, n, &pos, segment_size) ||
|
||||
get_usize(patch, n, &pos, shift) || get_usize(patch, n, &pos, from_size) ||
|
||||
get_usize(patch, n, &pos, to_size))
|
||||
return -3;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int fwp_run(fwp_job *j) {
|
||||
uint32_t mem, seg, shift, from, to;
|
||||
int res;
|
||||
|
||||
j->err = 0;
|
||||
j->detools_err = 0;
|
||||
j->to_size = 0;
|
||||
j->page_addr = NO_PAGE;
|
||||
j->written_end = 0;
|
||||
j->shift_low = (uint32_t)-1;
|
||||
j->shift_high = 0;
|
||||
j->dt = &g_dt;
|
||||
|
||||
if ((j->mem_base % FWP_PAGE) != 0 || (j->mem_size % FWP_PAGE) != 0) return FWP_ERR_ARGS;
|
||||
if (j->dry ? j->sink == 0 : (j->erase == 0 || j->write == 0)) return FWP_ERR_ARGS;
|
||||
if (fwp_header(j->patch, j->patch_size, &mem, &seg, &shift, &from, &to) != 0) return FWP_ERR_ARGS;
|
||||
if (mem != j->mem_size || from != j->from_size || seg != FWP_PAGE) return FWP_ERR_SIZE;
|
||||
// shift 0 would make detools erase a page before reading it (shift loop)
|
||||
if (shift == 0 || (shift % FWP_PAGE) != 0 || to == 0 || to > mem) return FWP_ERR_SHIFT;
|
||||
j->shift = shift;
|
||||
|
||||
detools_apply_patch_in_place_init(&g_dt, cb_read, cb_write, cb_erase, NULL, NULL, j->patch_size, j);
|
||||
res = detools_apply_patch_in_place_process(&g_dt, j->patch, j->patch_size);
|
||||
if (res == 0) res = detools_apply_patch_in_place_finalize(&g_dt);
|
||||
if (j->err) return j->err; // ours is the more specific diagnosis
|
||||
if (res < 0) {
|
||||
j->detools_err = -res;
|
||||
return FWP_ERR_DETOOLS - (-res);
|
||||
}
|
||||
flush_page(j);
|
||||
if ((uint32_t)res != to || j->written_end < to) return FWP_ERR_SIZE;
|
||||
j->to_size = to;
|
||||
return 0;
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
// --- RAM copies of the libc calls the compiler and detools emit -----------
|
||||
// tools/ota_ramlib.py renames memcpy/memset/memmove in this object to these, so
|
||||
// the real run never jumps back into flash libc.
|
||||
FWP_NOLIBC void *fwp_memcpy(void *d, const void *s, size_t n) {
|
||||
uint8_t *dd = (uint8_t *)d;
|
||||
const uint8_t *ss = (const uint8_t *)s;
|
||||
while (n--)
|
||||
*dd++ = *ss++;
|
||||
return d;
|
||||
}
|
||||
FWP_NOLIBC void *fwp_memset(void *d, int c, size_t n) {
|
||||
uint8_t *dd = (uint8_t *)d;
|
||||
while (n--)
|
||||
*dd++ = (uint8_t)c;
|
||||
return d;
|
||||
}
|
||||
FWP_NOLIBC void *fwp_memmove(void *d, const void *s, size_t n) {
|
||||
uint8_t *dd = (uint8_t *)d;
|
||||
const uint8_t *ss = (const uint8_t *)s;
|
||||
if (dd < ss) {
|
||||
while (n--)
|
||||
*dd++ = *ss++;
|
||||
} else {
|
||||
dd += n;
|
||||
ss += n;
|
||||
while (n--)
|
||||
*--dd = *--ss;
|
||||
}
|
||||
return d;
|
||||
}
|
||||
} // extern "C"
|
||||
|
||||
#endif
|
||||
Reference in new issue
Block a user