mirror of
https://github.com/cesanta/mongoose.git
synced 2026-12-03 21:48:22 +00:00
Merge pull request #3815 from cesanta/flash1
Introduce struct mg_flash::{read_fn,init_fn}
This commit is contained in:
18 files changed
+438
-293
No files matched your search
+206
-142
@@ -2958,103 +2958,12 @@ void mg_error(struct mg_connection *c, const char *fmt, ...) {
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#ifdef MG_OTA_PUBLIC_KEY
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
#if MG_OTA != MG_OTA_NONE && MG_OTA != MG_OTA_CUSTOM
|
||||
|
||||
static char *s_addr; // Current address to write to
|
||||
static size_t s_size; // Firmware size to flash. In-progress indicator
|
||||
static uint32_t s_crc32; // Firmware checksum
|
||||
|
||||
bool mg_ota_flash_begin(size_t new_firmware_size, struct mg_flash *flash) {
|
||||
bool ok = false;
|
||||
if (s_size) {
|
||||
MG_ERROR(("OTA already in progress. Call mg_ota_end()"));
|
||||
} else {
|
||||
size_t half = flash->size / 2;
|
||||
s_crc32 = 0;
|
||||
s_addr = (char *) flash->start + half;
|
||||
MG_DEBUG(("FW %lu bytes, max %lu", new_firmware_size, half));
|
||||
if (new_firmware_size < half) {
|
||||
ok = true;
|
||||
s_size = new_firmware_size;
|
||||
MG_INFO(("Starting OTA, firmware size %lu", s_size));
|
||||
} else {
|
||||
MG_ERROR(("Firmware %lu is too big to fit %lu", new_firmware_size, half - flash->align));
|
||||
}
|
||||
}
|
||||
return ok;
|
||||
// Read function for memory-mapped (XIP) flash, usable as mg_flash read_fn
|
||||
bool mg_flash_read_xip(void *addr, void *buf, size_t len) {
|
||||
memmove(buf, addr, len);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool mg_ota_flash_write(const void *buf, size_t len, struct mg_flash *flash) {
|
||||
bool ok = false;
|
||||
if (s_size == 0) {
|
||||
MG_ERROR(("OTA is not started, call mg_ota_begin()"));
|
||||
} else if (s_addr + MG_ROUND_UP(len, flash->align) >
|
||||
(char *) flash->start + flash->size) {
|
||||
MG_ERROR(("Flash overflow: attempting to write past the flash boundary"));
|
||||
} else {
|
||||
size_t len_aligned_down = MG_ROUND_DOWN(len, flash->align);
|
||||
if (len_aligned_down) ok = flash->write_fn(s_addr, buf, len_aligned_down);
|
||||
if (len_aligned_down < len) {
|
||||
size_t left = len - len_aligned_down;
|
||||
char tmp[flash->align];
|
||||
memset(tmp, 0xff, sizeof(tmp));
|
||||
memcpy(tmp, (char *) buf + len_aligned_down, left);
|
||||
ok = flash->write_fn(s_addr + len_aligned_down, tmp, sizeof(tmp));
|
||||
}
|
||||
s_crc32 = mg_crc32(s_crc32, (char *) buf, len); // Update CRC
|
||||
MG_DEBUG(("%#x %p %lu -> %d", s_addr - len, buf, len, ok));
|
||||
s_addr += len;
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
bool mg_ota_flash_end(struct mg_flash *flash) {
|
||||
char *base = (char *) flash->start + flash->size / 2;
|
||||
bool ok = false;
|
||||
int prev_state = MG_OTA_STATE_GET();
|
||||
if (s_size) {
|
||||
size_t size = (size_t) (s_addr - base);
|
||||
uint32_t crc32 = mg_crc32(0, base, s_size);
|
||||
if (size == s_size && crc32 == s_crc32) ok = true;
|
||||
MG_DEBUG(("CRC: %x/%x, size: %lu/%lu, status: %s", s_crc32, crc32, s_size,
|
||||
size, ok ? "ok" : "fail"));
|
||||
#ifdef MG_OTA_PUBLIC_KEY
|
||||
if (ok) {
|
||||
bool signed_fw = s_size > 68 &&
|
||||
memcmp((uint8_t *) base + s_size - 4, "MGSG", 4) == 0;
|
||||
if (signed_fw) {
|
||||
static const uint8_t s_pubkey[] = MG_OTA_PUBLIC_KEY;
|
||||
uint8_t hash[32];
|
||||
size_t fw_size = s_size - 68; // strip 64-byte sig + 4-byte magic
|
||||
mg_sha256(hash, (uint8_t *) base, fw_size);
|
||||
ok = mg_p256_verify(s_pubkey, hash, sizeof(hash),
|
||||
(uint8_t *) base + fw_size) == 1;
|
||||
MG_INFO(("Signature: %s", ok ? "ok" : "fail"));
|
||||
} else {
|
||||
ok = false;
|
||||
MG_ERROR(("Unsigned firmware rejected"));
|
||||
}
|
||||
}
|
||||
#endif
|
||||
s_size = 0;
|
||||
if (ok) MG_OTA_STATE_SET(MG_OTA_TESTING);
|
||||
if (ok) ok = flash->swap_fn();
|
||||
if (!ok) MG_OTA_STATE_SET(prev_state); // undo state in case of failure
|
||||
}
|
||||
MG_INFO(("Finishing OTA: %s", ok ? "ok" : "fail"));
|
||||
return ok;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#ifdef MG_ENABLE_LINES
|
||||
#line 1 "src/fmt.c"
|
||||
#endif
|
||||
@@ -7116,6 +7025,10 @@ bool mg_l2_pppoe_rx(struct mg_tcpip_if *ifp, enum mg_l2proto *proto,
|
||||
#define MG_LFS_FD_BASE 12000 // Starting file descriptor number
|
||||
#endif
|
||||
|
||||
#ifndef MG_LFS_FLASH
|
||||
#define MG_LFS_FLASH mg_flash // Flash used by the FS, struct mg_flash *
|
||||
#endif
|
||||
|
||||
#if LFS_USE_RAM && !defined(MG_LFS_BLOCK_SIZE)
|
||||
#define MG_LFS_BLOCK_SIZE 8192
|
||||
#endif
|
||||
@@ -7155,25 +7068,27 @@ static struct lfs_config s_cfg = {
|
||||
.lookahead_size = MG_LFS_BUF_SIZE / 8,
|
||||
};
|
||||
|
||||
static bool flash_write_buf(void *addr, const void *buf, size_t len) {
|
||||
#if LFS_USE_RAM
|
||||
memmove(addr, buf, len);
|
||||
return true;
|
||||
#else
|
||||
return mg_flash != NULL && mg_flash->write_fn(addr, buf, len);
|
||||
#endif
|
||||
}
|
||||
|
||||
static int lfs_driver_read(const struct lfs_config *cfg, lfs_block_t block,
|
||||
lfs_off_t off, void *buf, lfs_size_t len) {
|
||||
memmove(buf, &s_fs[block * cfg->block_size + off], len);
|
||||
uint8_t *src = &s_fs[block * cfg->block_size + off];
|
||||
#if LFS_USE_RAM
|
||||
memmove(buf, src, len);
|
||||
#else
|
||||
struct mg_flash *f = MG_LFS_FLASH; // Variable: &x != NULL is a warning
|
||||
if (f == NULL || !f->read_fn(src, buf, len)) return LFS_ERR_IO;
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int lfs_driver_prog(const struct lfs_config *cfg, lfs_block_t block,
|
||||
lfs_off_t off, const void *buf, lfs_size_t len) {
|
||||
uint8_t *dst = &s_fs[block * cfg->block_size + off];
|
||||
if (!flash_write_buf(dst, buf, len)) return LFS_ERR_IO;
|
||||
#if LFS_USE_RAM
|
||||
memmove(dst, buf, len);
|
||||
#else
|
||||
struct mg_flash *f = MG_LFS_FLASH;
|
||||
if (f == NULL || !f->write_fn(dst, buf, len)) return LFS_ERR_IO;
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -7199,24 +7114,24 @@ bool mg_lfs_init(size_t size) {
|
||||
if (s_ram_fs == NULL) s_ram_fs = (uint8_t *) mg_calloc(1, size);
|
||||
s_fs = s_ram_fs;
|
||||
#else
|
||||
{
|
||||
// Some flash attributes may be initialised on ota_begin
|
||||
int lev = mg_log_level;
|
||||
mg_log_level = MG_LL_NONE;
|
||||
mg_ota_begin(1);
|
||||
mg_ota_end();
|
||||
mg_log_set(lev);
|
||||
}
|
||||
if (mg_flash == NULL || mg_flash->secsz == 0 || mg_flash->size < size) {
|
||||
MG_ERROR(("mg_flash %p secsz %zu flash_size %zu < size %zu", mg_flash, mg_flash->secsz, mg_flash->size, size));
|
||||
} else if (mg_flash->write_fn == NULL) {
|
||||
MG_ERROR(("flash write_fn == NULL"));
|
||||
struct mg_flash *f = MG_LFS_FLASH;
|
||||
if (f == NULL) {
|
||||
MG_ERROR(("no flash"));
|
||||
} else if (f->init_fn != NULL && !f->init_fn(f)) {
|
||||
MG_ERROR(("flash init failed"));
|
||||
} else if (f->secsz == 0 || f->size < size) {
|
||||
MG_ERROR(("secsz %zu flash_size %zu < size %zu", f->secsz, f->size,
|
||||
size));
|
||||
} else if (f->read_fn == NULL || f->write_fn == NULL) {
|
||||
MG_ERROR(("flash read_fn or write_fn == NULL"));
|
||||
} else {
|
||||
s_cfg.block_size = mg_flash->secsz;
|
||||
s_fs = (uint8_t *) mg_flash->start + mg_flash->size - size;
|
||||
s_cfg.block_size = f->secsz;
|
||||
s_fs = (uint8_t *) f->start + f->size - size;
|
||||
}
|
||||
#endif
|
||||
if (s_fs == NULL || s_cfg.block_size == 0 || size % s_cfg.block_size != 0) {
|
||||
// s_fs can be NULL for a flash that starts at 0, e.g. an SPI flash
|
||||
if ((LFS_USE_RAM && s_fs == NULL) || s_cfg.block_size == 0 ||
|
||||
size % s_cfg.block_size != 0) {
|
||||
MG_ERROR(("s_fs %p blk_size %zu size %zu", s_fs, s_cfg.block_size));
|
||||
} else {
|
||||
s_cfg.block_count = (lfs_size_t) (size / s_cfg.block_size);
|
||||
@@ -11729,6 +11644,16 @@ void mg_tcpip_mapip(struct mg_connection *c, struct mg_addr *ip) {
|
||||
|
||||
|
||||
|
||||
#ifdef MG_OTA_PUBLIC_KEY
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
// In strict C mode, e.g. -std=c99, stdlib.h may not declare alloca()
|
||||
#if !defined(alloca) && defined(__GNUC__)
|
||||
#define alloca(size) __builtin_alloca(size)
|
||||
#endif
|
||||
|
||||
#ifndef MG_OTA_MAX_URL_LEN
|
||||
#define MG_OTA_MAX_URL_LEN 256
|
||||
#endif
|
||||
@@ -12000,6 +11925,93 @@ void mg_ota_poll(struct mg_mgr *mgr) {
|
||||
}
|
||||
}
|
||||
|
||||
static char *s_flash_addr; // Current address to write to
|
||||
static size_t s_size; // Firmware size to flash. In-progress indicator
|
||||
static uint32_t s_crc32; // Firmware checksum
|
||||
|
||||
bool mg_ota_flash_begin(size_t new_firmware_size, struct mg_flash *flash) {
|
||||
bool ok = false;
|
||||
if (s_size) {
|
||||
MG_ERROR(("OTA already in progress. Call mg_ota_end()"));
|
||||
} else {
|
||||
size_t half = flash->size / 2;
|
||||
s_crc32 = 0;
|
||||
s_flash_addr = (char *) flash->start + half;
|
||||
MG_DEBUG(("FW %lu bytes, max %lu", new_firmware_size, half));
|
||||
if (new_firmware_size < half) {
|
||||
ok = true;
|
||||
s_size = new_firmware_size;
|
||||
MG_INFO(("Starting OTA, firmware size %lu", s_size));
|
||||
} else {
|
||||
MG_ERROR(("Firmware %lu is too big to fit %lu", new_firmware_size, half - flash->align));
|
||||
}
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
bool mg_ota_flash_write(const void *buf, size_t len, struct mg_flash *flash) {
|
||||
bool ok = false;
|
||||
if (s_size == 0) {
|
||||
MG_ERROR(("OTA is not started, call mg_ota_begin()"));
|
||||
} else if (s_flash_addr + MG_ROUND_UP(len, flash->align) >
|
||||
(char *) flash->start + flash->size) {
|
||||
MG_ERROR(("Flash overflow: attempting to write past the flash boundary"));
|
||||
} else {
|
||||
size_t len_aligned_down = MG_ROUND_DOWN(len, flash->align);
|
||||
if (len_aligned_down) {
|
||||
ok = flash->write_fn(s_flash_addr, buf, len_aligned_down);
|
||||
}
|
||||
if (len_aligned_down < len) {
|
||||
size_t left = len - len_aligned_down;
|
||||
char *tmp = (char *) alloca(flash->align);
|
||||
memset(tmp, 0xff, flash->align);
|
||||
memcpy(tmp, (char *) buf + len_aligned_down, left);
|
||||
ok = flash->write_fn(s_flash_addr + len_aligned_down, tmp, flash->align);
|
||||
}
|
||||
s_crc32 = mg_crc32(s_crc32, (char *) buf, len); // Update CRC
|
||||
MG_DEBUG(("%#x %p %lu -> %d", s_flash_addr - len, buf, len, ok));
|
||||
s_flash_addr += len;
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
bool mg_ota_flash_end(struct mg_flash *flash) {
|
||||
char *base = (char *) flash->start + flash->size / 2;
|
||||
bool ok = false;
|
||||
int prev_state = MG_OTA_STATE_GET();
|
||||
if (s_size) {
|
||||
size_t size = (size_t) (s_flash_addr - base);
|
||||
uint32_t crc32 = mg_crc32(0, base, s_size);
|
||||
if (size == s_size && crc32 == s_crc32) ok = true;
|
||||
MG_DEBUG(("CRC: %x/%x, size: %lu/%lu, status: %s", s_crc32, crc32, s_size,
|
||||
size, ok ? "ok" : "fail"));
|
||||
#ifdef MG_OTA_PUBLIC_KEY
|
||||
if (ok) {
|
||||
bool signed_fw = s_size > 68 &&
|
||||
memcmp((uint8_t *) base + s_size - 4, "MGSG", 4) == 0;
|
||||
if (signed_fw) {
|
||||
static const uint8_t s_pubkey[] = MG_OTA_PUBLIC_KEY;
|
||||
uint8_t hash[32];
|
||||
size_t fw_size = s_size - 68; // strip 64-byte sig + 4-byte magic
|
||||
mg_sha256(hash, (uint8_t *) base, fw_size);
|
||||
ok = mg_p256_verify(s_pubkey, hash, sizeof(hash),
|
||||
(uint8_t *) base + fw_size) == 1;
|
||||
MG_INFO(("Signature: %s", ok ? "ok" : "fail"));
|
||||
} else {
|
||||
ok = false;
|
||||
MG_ERROR(("Unsigned firmware rejected"));
|
||||
}
|
||||
}
|
||||
#endif
|
||||
s_size = 0;
|
||||
if (ok) MG_OTA_STATE_SET(MG_OTA_TESTING);
|
||||
if (ok) ok = flash->swap_fn();
|
||||
if (!ok) MG_OTA_STATE_SET(prev_state); // undo state in case of failure
|
||||
}
|
||||
MG_INFO(("Finishing OTA: %s", ok ? "ok" : "fail"));
|
||||
return ok;
|
||||
}
|
||||
|
||||
#ifdef MG_ENABLE_LINES
|
||||
#line 1 "src/ota_ch32v307.c"
|
||||
#endif
|
||||
@@ -12019,8 +12031,10 @@ static struct mg_flash s_mg_flash_ch32v307 = {
|
||||
480 * 1024, // Size, first 320k is 0-wait
|
||||
4 * 1024, // Sector size, 4k
|
||||
4, // Align, 32 bit
|
||||
mg_flash_read_xip,
|
||||
mg_ch32v307_write,
|
||||
mg_ch32v307_swap,
|
||||
NULL, // No init
|
||||
};
|
||||
|
||||
#define FLASH_BASE 0x40022000
|
||||
@@ -12290,8 +12304,10 @@ static struct mg_flash s_mg_flash_imxrt = {
|
||||
4 * 1024 * 1024, // Size, 4mb
|
||||
MG_IMXRT_SECTOR_SIZE, // Sector size
|
||||
MG_IMXRT_PAGE_SIZE, // Align
|
||||
mg_flash_read_xip,
|
||||
mg_imxrt_write,
|
||||
mg_imxrt_swap,
|
||||
NULL, // No init
|
||||
};
|
||||
|
||||
struct mg_flexspi_lut_seq {
|
||||
@@ -12921,14 +12937,17 @@ struct mg_flash *mg_flash = &s_mg_flash_imxrt;
|
||||
|
||||
static bool mg_mcxn_write(void *, const void *, size_t);
|
||||
static bool mg_mcxn_swap(void);
|
||||
static bool mg_mcxn_init(struct mg_flash *);
|
||||
|
||||
static struct mg_flash s_mg_flash_mcxn = {
|
||||
(void *) 0, // Start, filled at init
|
||||
0, // Size, filled at init
|
||||
0, // Sector size, filled at init
|
||||
0, // Align, filled at init
|
||||
mg_flash_read_xip,
|
||||
mg_mcxn_write,
|
||||
mg_mcxn_swap,
|
||||
mg_mcxn_init,
|
||||
};
|
||||
|
||||
struct mg_flash_config {
|
||||
@@ -13070,22 +13089,27 @@ MG_IRAM static void single_bank_swap(char *p1, char *p2, size_t s, size_t ss) {
|
||||
*(volatile unsigned long *) 0xe000ed0c = 0x5fa0004;
|
||||
}
|
||||
|
||||
bool mg_ota_begin(size_t new_firmware_size) {
|
||||
// Fills flash start, size, sector size and alignment from the ROM driver
|
||||
static bool mg_mcxn_init(struct mg_flash *flash) {
|
||||
uint32_t status;
|
||||
struct mg_flash_config config;
|
||||
if ((status = mg_flash_driver->init(&config)) != 0) {
|
||||
MG_ERROR(("Flash driver init error: %lu", status));
|
||||
return false;
|
||||
}
|
||||
s_mg_flash_mcxn.start = (void *) config.addr;
|
||||
s_mg_flash_mcxn.size = config.size;
|
||||
s_mg_flash_mcxn.secsz = config.sector_size;
|
||||
s_mg_flash_mcxn.align = config.page_size;
|
||||
flash->start = (void *) config.addr;
|
||||
flash->size = config.size;
|
||||
flash->secsz = config.sector_size;
|
||||
flash->align = config.page_size;
|
||||
mg_flash_driver->deinit(&config);
|
||||
MG_DEBUG(
|
||||
("%lu-byte flash @%p, using %lu-byte sectors with %lu-byte-aligned pages",
|
||||
s_mg_flash_mcxn.size, s_mg_flash_mcxn.start, s_mg_flash_mcxn.secsz,
|
||||
s_mg_flash_mcxn.align));
|
||||
flash->size, flash->start, flash->secsz, flash->align));
|
||||
return true;
|
||||
}
|
||||
|
||||
bool mg_ota_begin(size_t new_firmware_size) {
|
||||
if (!mg_mcxn_init(&s_mg_flash_mcxn)) return false;
|
||||
return mg_ota_flash_begin(new_firmware_size, &s_mg_flash_mcxn);
|
||||
}
|
||||
|
||||
@@ -13160,8 +13184,10 @@ static bool s_mcxw71_failed;
|
||||
|
||||
static bool mg_mcxw71_write(void *, const void *, size_t);
|
||||
static bool mg_mcxw71_swap(void);
|
||||
static bool mg_mcxw71_init(struct mg_flash *);
|
||||
static struct mg_flash s_mg_flash_mcxw71 = {
|
||||
(void *) 0, 0x100000, 8192, 16, mg_mcxw71_write, mg_mcxw71_swap};
|
||||
(void *) 0, 0x100000, 8192, 16,
|
||||
mg_flash_read_xip, mg_mcxw71_write, mg_mcxw71_swap, mg_mcxw71_init};
|
||||
struct mg_flash *mg_flash = &s_mg_flash_mcxw71;
|
||||
|
||||
// Everything called while flash is busy, or after the application has been
|
||||
@@ -13233,17 +13259,22 @@ MG_IRAM static void mg_mcxw71_apply(size_t size) {
|
||||
for (;;) __NOP();
|
||||
}
|
||||
|
||||
bool mg_ota_begin(size_t new_firmware_size) {
|
||||
uint32_t status;
|
||||
if (s_mcxw71_size != 0 || new_firmware_size == 0) return false;
|
||||
status = MG_MCXW71_FLASH_API->init(&s_mcxw71_config);
|
||||
// Initialises the ROM flash API and fills the flash size
|
||||
static bool mg_mcxw71_init(struct mg_flash *flash) {
|
||||
uint32_t status = MG_MCXW71_FLASH_API->init(&s_mcxw71_config);
|
||||
if (status != 0 || s_mcxw71_config.mem[0].base != 0 ||
|
||||
s_mcxw71_config.mem[0].size < 16384 ||
|
||||
(s_mcxw71_config.mem[0].size % 16384) != 0) {
|
||||
MG_ERROR(("Flash init failed: %lu", (unsigned long) status));
|
||||
return false;
|
||||
}
|
||||
s_mg_flash_mcxw71.size = s_mcxw71_config.mem[0].size;
|
||||
flash->size = s_mcxw71_config.mem[0].size;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool mg_ota_begin(size_t new_firmware_size) {
|
||||
if (s_mcxw71_size != 0 || new_firmware_size == 0) return false;
|
||||
if (!mg_mcxw71_init(&s_mg_flash_mcxw71)) return false;
|
||||
if (!mg_ota_flash_begin(new_firmware_size, &s_mg_flash_mcxw71)) return false;
|
||||
s_mcxw71_size = new_firmware_size;
|
||||
s_mcxw71_received = s_mcxw71_pending = 0;
|
||||
@@ -13331,7 +13362,10 @@ static struct mg_flash s_mg_flash_picosdk = {
|
||||
#endif
|
||||
FLASH_SECTOR_SIZE, // Sector size, from hardware_flash
|
||||
FLASH_PAGE_SIZE, // Align, from hardware_flash
|
||||
mg_picosdk_write, mg_picosdk_swap,
|
||||
mg_flash_read_xip,
|
||||
mg_picosdk_write,
|
||||
mg_picosdk_swap,
|
||||
NULL, // No init
|
||||
};
|
||||
|
||||
#define MG_MODULO2(x, m) ((x) & ((m) -1))
|
||||
@@ -13505,8 +13539,10 @@ static struct mg_flash s_mg_flash_frdm = {(void *) 0x08000000, // Start,
|
||||
0x200000, // Size
|
||||
0x1000, // Sector size
|
||||
0x100, // Align
|
||||
mg_flash_read_xip,
|
||||
mg_frdm_write,
|
||||
mg_frdm_swap};
|
||||
mg_frdm_swap,
|
||||
NULL}; // No init
|
||||
|
||||
struct mg_flexspi_lut_seq {
|
||||
uint8_t seqNum;
|
||||
@@ -13867,14 +13903,17 @@ struct mg_flash *mg_flash = &s_mg_flash_frdm;
|
||||
|
||||
static bool mg_stm32f_write(void *, const void *, size_t);
|
||||
static bool mg_stm32f_swap(void);
|
||||
static bool mg_stm32f_init(struct mg_flash *);
|
||||
|
||||
static struct mg_flash s_mg_flash_stm32f = {
|
||||
(void *) 0x08000000, // Start
|
||||
0, // Size, FLASH_SIZE_REG
|
||||
0, // Size, FLASH_SIZE_REG, set by init
|
||||
0, // Irregular sector size
|
||||
32, // Align, 256 bit
|
||||
mg_flash_read_xip,
|
||||
mg_stm32f_write,
|
||||
mg_stm32f_swap,
|
||||
mg_stm32f_init,
|
||||
};
|
||||
|
||||
#define MG_FLASH_BASE 0x40023c00
|
||||
@@ -14084,9 +14123,15 @@ MG_IRAM static void single_bank_swap(char *p1, char *p2, size_t s, size_t ss) {
|
||||
*(volatile uint32_t *) 0xe000ed0cU = 0x5fa0004U; // NVIC_SystemReset()
|
||||
}
|
||||
|
||||
// Fills the flash size, read from the chip
|
||||
static bool mg_stm32f_init(struct mg_flash *flash) {
|
||||
flash->size = flash_size();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool mg_ota_begin(size_t new_firmware_size) {
|
||||
size_t max;
|
||||
s_mg_flash_stm32f.size = flash_size();
|
||||
mg_stm32f_init(&s_mg_flash_stm32f);
|
||||
#ifdef __ZEPHYR__
|
||||
*((uint32_t *)0xE000ED94) = 0;
|
||||
MG_DEBUG(("Jailbreak %s", *((uint32_t *)0xE000ED94) == 0 ? "successful" : "failed"));
|
||||
@@ -14123,14 +14168,17 @@ struct mg_flash *mg_flash = &s_mg_flash_stm32f;
|
||||
|
||||
static bool mg_stm32h5_write(void *, const void *, size_t);
|
||||
static bool mg_stm32h5_swap(void);
|
||||
static bool mg_stm32h5_init(struct mg_flash *);
|
||||
|
||||
static struct mg_flash s_mg_flash_stm32h5 = {
|
||||
(void *) 0x08000000, // Start
|
||||
0, // Size, FLASHSIZE_BASE
|
||||
0, // Size, FLASHSIZE_BASE, set by init
|
||||
8 * 1024, // Sector size, 8k
|
||||
16, // Align, 128 bit
|
||||
mg_flash_read_xip,
|
||||
mg_stm32h5_write,
|
||||
mg_stm32h5_swap,
|
||||
mg_stm32h5_init,
|
||||
};
|
||||
|
||||
#define MG_FLASH_BASE 0x40022000 // Base address of the flash controller
|
||||
@@ -14263,8 +14311,14 @@ static bool mg_stm32h5_write(void *addr, const void *buf, size_t len) {
|
||||
return ok;
|
||||
}
|
||||
|
||||
// Fills the flash size, read from the chip
|
||||
static bool mg_stm32h5_init(struct mg_flash *flash) {
|
||||
flash->size = flash_size();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool mg_ota_begin(size_t new_firmware_size) {
|
||||
s_mg_flash_stm32h5.size = flash_size();
|
||||
mg_stm32h5_init(&s_mg_flash_stm32h5);
|
||||
#ifdef __ZEPHYR__
|
||||
*((uint32_t *)0xE000ED94) = 0;
|
||||
MG_DEBUG(("Jailbreak %s", *((uint32_t *)0xE000ED94) == 0 ? "successful" : "failed"));
|
||||
@@ -14304,14 +14358,17 @@ struct mg_flash *mg_flash = &s_mg_flash_stm32h5;
|
||||
|
||||
static bool mg_stm32h7_write(void *, const void *, size_t);
|
||||
static bool mg_stm32h7_swap(void);
|
||||
static bool mg_stm32h7_init(struct mg_flash *);
|
||||
|
||||
static struct mg_flash s_mg_flash_stm32h7 = {
|
||||
(void *) 0x08000000, // Start
|
||||
0, // Size, FLASH_SIZE_REG
|
||||
0, // Size, FLASH_SIZE_REG, set by init
|
||||
128 * 1024, // Sector size, 128k
|
||||
32, // Align, 256 bit
|
||||
mg_flash_read_xip,
|
||||
mg_stm32h7_write,
|
||||
mg_stm32h7_swap,
|
||||
mg_stm32h7_init,
|
||||
};
|
||||
|
||||
#define FLASH_BASE1 0x52002000 // Base address for bank1
|
||||
@@ -14497,8 +14554,14 @@ MG_IRAM static void single_bank_swap(char *p1, char *p2, size_t s, size_t ss) {
|
||||
*(volatile uint32_t *) 0xe000ed0cU = 0x5fa0004U; // NVIC_SystemReset()
|
||||
}
|
||||
|
||||
// Fills the flash size, read from the chip
|
||||
static bool mg_stm32h7_init(struct mg_flash *flash) {
|
||||
flash->size = flash_size();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool mg_ota_begin(size_t new_firmware_size) {
|
||||
s_mg_flash_stm32h7.size = flash_size();
|
||||
mg_stm32h7_init(&s_mg_flash_stm32h7);
|
||||
#ifdef __ZEPHYR__
|
||||
*((uint32_t *) 0xE000ED94) = 0;
|
||||
MG_DEBUG(("Jailbreak %s", *((uint32_t *) 0xE000ED94) == 0 ? "ok" : "failed"));
|
||||
@@ -34603,6 +34666,7 @@ static void mgi_radio_frame_header(uint8_t *frame, uint16_t dst, uint16_t src,
|
||||
frame[MGI_RADIO_HDR_LEN] = type, frame[MGI_RADIO_HDR_LEN + 1] = burst;
|
||||
}
|
||||
|
||||
// Hand a radio frame to the hardware.
|
||||
static bool mgi_radio_send(struct mg_radio_node *node, const void *frame,
|
||||
size_t len) {
|
||||
return node->hw_tx(node->ifp, frame, len) == len;
|
||||
|
||||
+13
-4
@@ -1953,6 +1953,8 @@ struct dirent {
|
||||
};
|
||||
|
||||
typedef struct mg_lfs_fd DIR;
|
||||
|
||||
bool mg_lfs_init(size_t size);
|
||||
#endif
|
||||
|
||||
|
||||
@@ -4280,6 +4282,13 @@ bool mg_ota_write(const void *buf, size_t len);
|
||||
// Ends the OTA process and commits the written firmware.
|
||||
bool mg_ota_end(void);
|
||||
|
||||
// Same as above, for a given flash. OTA backends use these to implement
|
||||
// mg_ota_begin(), mg_ota_write() and mg_ota_end()
|
||||
struct mg_flash;
|
||||
bool mg_ota_flash_begin(size_t new_firmware_size, struct mg_flash *flash);
|
||||
bool mg_ota_flash_write(const void *buf, size_t len, struct mg_flash *flash);
|
||||
bool mg_ota_flash_end(struct mg_flash *flash);
|
||||
|
||||
// Push-based OTA: mg_http_stream_body() callback that flashes the request
|
||||
//
|
||||
// ```c
|
||||
@@ -4659,16 +4668,16 @@ struct mg_flash {
|
||||
size_t size; // Flash size
|
||||
size_t secsz; // Sector size
|
||||
size_t align; // Write alignment
|
||||
bool (*read_fn)(void *, void *, size_t); // Read function
|
||||
bool (*write_fn)(void *, const void *, size_t); // Write function
|
||||
bool (*swap_fn)(void); // Swap partitions
|
||||
bool (*init_fn)(struct mg_flash *); // Optional, fills runtime attributes
|
||||
};
|
||||
|
||||
extern struct mg_flash *mg_flash;
|
||||
bool mg_lfs_init(size_t size);
|
||||
|
||||
bool mg_ota_flash_begin(size_t new_firmware_size, struct mg_flash *flash);
|
||||
bool mg_ota_flash_write(const void *buf, size_t len, struct mg_flash *flash);
|
||||
bool mg_ota_flash_end(struct mg_flash *flash);
|
||||
// Reads memory-mapped (XIP) flash. Use as read_fn for such flash
|
||||
bool mg_flash_read_xip(void *addr, void *buf, size_t len);
|
||||
|
||||
|
||||
|
||||
|
||||
+4
-95
@@ -1,98 +1,7 @@
|
||||
#include "arch.h"
|
||||
#include "flash.h"
|
||||
#include "log.h"
|
||||
#include "ota.h"
|
||||
#include "util.h"
|
||||
#ifdef MG_OTA_PUBLIC_KEY
|
||||
#include "sha256.h"
|
||||
#include "tls_p256.h"
|
||||
#endif
|
||||
|
||||
#if MG_OTA != MG_OTA_NONE && MG_OTA != MG_OTA_CUSTOM
|
||||
|
||||
static char *s_addr; // Current address to write to
|
||||
static size_t s_size; // Firmware size to flash. In-progress indicator
|
||||
static uint32_t s_crc32; // Firmware checksum
|
||||
|
||||
bool mg_ota_flash_begin(size_t new_firmware_size, struct mg_flash *flash) {
|
||||
bool ok = false;
|
||||
if (s_size) {
|
||||
MG_ERROR(("OTA already in progress. Call mg_ota_end()"));
|
||||
} else {
|
||||
size_t half = flash->size / 2;
|
||||
s_crc32 = 0;
|
||||
s_addr = (char *) flash->start + half;
|
||||
MG_DEBUG(("FW %lu bytes, max %lu", new_firmware_size, half));
|
||||
if (new_firmware_size < half) {
|
||||
ok = true;
|
||||
s_size = new_firmware_size;
|
||||
MG_INFO(("Starting OTA, firmware size %lu", s_size));
|
||||
} else {
|
||||
MG_ERROR(("Firmware %lu is too big to fit %lu", new_firmware_size, half - flash->align));
|
||||
}
|
||||
}
|
||||
return ok;
|
||||
// Read function for memory-mapped (XIP) flash, usable as mg_flash read_fn
|
||||
bool mg_flash_read_xip(void *addr, void *buf, size_t len) {
|
||||
memmove(buf, addr, len);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool mg_ota_flash_write(const void *buf, size_t len, struct mg_flash *flash) {
|
||||
bool ok = false;
|
||||
if (s_size == 0) {
|
||||
MG_ERROR(("OTA is not started, call mg_ota_begin()"));
|
||||
} else if (s_addr + MG_ROUND_UP(len, flash->align) >
|
||||
(char *) flash->start + flash->size) {
|
||||
MG_ERROR(("Flash overflow: attempting to write past the flash boundary"));
|
||||
} else {
|
||||
size_t len_aligned_down = MG_ROUND_DOWN(len, flash->align);
|
||||
if (len_aligned_down) ok = flash->write_fn(s_addr, buf, len_aligned_down);
|
||||
if (len_aligned_down < len) {
|
||||
size_t left = len - len_aligned_down;
|
||||
char tmp[flash->align];
|
||||
memset(tmp, 0xff, sizeof(tmp));
|
||||
memcpy(tmp, (char *) buf + len_aligned_down, left);
|
||||
ok = flash->write_fn(s_addr + len_aligned_down, tmp, sizeof(tmp));
|
||||
}
|
||||
s_crc32 = mg_crc32(s_crc32, (char *) buf, len); // Update CRC
|
||||
MG_DEBUG(("%#x %p %lu -> %d", s_addr - len, buf, len, ok));
|
||||
s_addr += len;
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
bool mg_ota_flash_end(struct mg_flash *flash) {
|
||||
char *base = (char *) flash->start + flash->size / 2;
|
||||
bool ok = false;
|
||||
int prev_state = MG_OTA_STATE_GET();
|
||||
if (s_size) {
|
||||
size_t size = (size_t) (s_addr - base);
|
||||
uint32_t crc32 = mg_crc32(0, base, s_size);
|
||||
if (size == s_size && crc32 == s_crc32) ok = true;
|
||||
MG_DEBUG(("CRC: %x/%x, size: %lu/%lu, status: %s", s_crc32, crc32, s_size,
|
||||
size, ok ? "ok" : "fail"));
|
||||
#ifdef MG_OTA_PUBLIC_KEY
|
||||
if (ok) {
|
||||
bool signed_fw = s_size > 68 &&
|
||||
memcmp((uint8_t *) base + s_size - 4, "MGSG", 4) == 0;
|
||||
if (signed_fw) {
|
||||
static const uint8_t s_pubkey[] = MG_OTA_PUBLIC_KEY;
|
||||
uint8_t hash[32];
|
||||
size_t fw_size = s_size - 68; // strip 64-byte sig + 4-byte magic
|
||||
mg_sha256(hash, (uint8_t *) base, fw_size);
|
||||
ok = mg_p256_verify(s_pubkey, hash, sizeof(hash),
|
||||
(uint8_t *) base + fw_size) == 1;
|
||||
MG_INFO(("Signature: %s", ok ? "ok" : "fail"));
|
||||
} else {
|
||||
ok = false;
|
||||
MG_ERROR(("Unsigned firmware rejected"));
|
||||
}
|
||||
}
|
||||
#endif
|
||||
s_size = 0;
|
||||
if (ok) MG_OTA_STATE_SET(MG_OTA_TESTING);
|
||||
if (ok) ok = flash->swap_fn();
|
||||
if (!ok) MG_OTA_STATE_SET(prev_state); // undo state in case of failure
|
||||
}
|
||||
MG_INFO(("Finishing OTA: %s", ok ? "ok" : "fail"));
|
||||
return ok;
|
||||
}
|
||||
|
||||
#endif
|
||||
+4
-4
@@ -7,13 +7,13 @@ struct mg_flash {
|
||||
size_t size; // Flash size
|
||||
size_t secsz; // Sector size
|
||||
size_t align; // Write alignment
|
||||
bool (*read_fn)(void *, void *, size_t); // Read function
|
||||
bool (*write_fn)(void *, const void *, size_t); // Write function
|
||||
bool (*swap_fn)(void); // Swap partitions
|
||||
bool (*init_fn)(struct mg_flash *); // Optional, fills runtime attributes
|
||||
};
|
||||
|
||||
extern struct mg_flash *mg_flash;
|
||||
bool mg_lfs_init(size_t size);
|
||||
|
||||
bool mg_ota_flash_begin(size_t new_firmware_size, struct mg_flash *flash);
|
||||
bool mg_ota_flash_write(const void *buf, size_t len, struct mg_flash *flash);
|
||||
bool mg_ota_flash_end(struct mg_flash *flash);
|
||||
// Reads memory-mapped (XIP) flash. Use as read_fn for such flash
|
||||
bool mg_flash_read_xip(void *addr, void *buf, size_t len);
|
||||
@@ -23,6 +23,10 @@
|
||||
#define MG_LFS_FD_BASE 12000 // Starting file descriptor number
|
||||
#endif
|
||||
|
||||
#ifndef MG_LFS_FLASH
|
||||
#define MG_LFS_FLASH mg_flash // Flash used by the FS, struct mg_flash *
|
||||
#endif
|
||||
|
||||
#if LFS_USE_RAM && !defined(MG_LFS_BLOCK_SIZE)
|
||||
#define MG_LFS_BLOCK_SIZE 8192
|
||||
#endif
|
||||
@@ -62,25 +66,27 @@ static struct lfs_config s_cfg = {
|
||||
.lookahead_size = MG_LFS_BUF_SIZE / 8,
|
||||
};
|
||||
|
||||
static bool flash_write_buf(void *addr, const void *buf, size_t len) {
|
||||
#if LFS_USE_RAM
|
||||
memmove(addr, buf, len);
|
||||
return true;
|
||||
#else
|
||||
return mg_flash != NULL && mg_flash->write_fn(addr, buf, len);
|
||||
#endif
|
||||
}
|
||||
|
||||
static int lfs_driver_read(const struct lfs_config *cfg, lfs_block_t block,
|
||||
lfs_off_t off, void *buf, lfs_size_t len) {
|
||||
memmove(buf, &s_fs[block * cfg->block_size + off], len);
|
||||
uint8_t *src = &s_fs[block * cfg->block_size + off];
|
||||
#if LFS_USE_RAM
|
||||
memmove(buf, src, len);
|
||||
#else
|
||||
struct mg_flash *f = MG_LFS_FLASH; // Variable: &x != NULL is a warning
|
||||
if (f == NULL || !f->read_fn(src, buf, len)) return LFS_ERR_IO;
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int lfs_driver_prog(const struct lfs_config *cfg, lfs_block_t block,
|
||||
lfs_off_t off, const void *buf, lfs_size_t len) {
|
||||
uint8_t *dst = &s_fs[block * cfg->block_size + off];
|
||||
if (!flash_write_buf(dst, buf, len)) return LFS_ERR_IO;
|
||||
#if LFS_USE_RAM
|
||||
memmove(dst, buf, len);
|
||||
#else
|
||||
struct mg_flash *f = MG_LFS_FLASH;
|
||||
if (f == NULL || !f->write_fn(dst, buf, len)) return LFS_ERR_IO;
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -106,24 +112,24 @@ bool mg_lfs_init(size_t size) {
|
||||
if (s_ram_fs == NULL) s_ram_fs = (uint8_t *) mg_calloc(1, size);
|
||||
s_fs = s_ram_fs;
|
||||
#else
|
||||
{
|
||||
// Some flash attributes may be initialised on ota_begin
|
||||
int lev = mg_log_level;
|
||||
mg_log_level = MG_LL_NONE;
|
||||
mg_ota_begin(1);
|
||||
mg_ota_end();
|
||||
mg_log_set(lev);
|
||||
}
|
||||
if (mg_flash == NULL || mg_flash->secsz == 0 || mg_flash->size < size) {
|
||||
MG_ERROR(("mg_flash %p secsz %zu flash_size %zu < size %zu", mg_flash, mg_flash->secsz, mg_flash->size, size));
|
||||
} else if (mg_flash->write_fn == NULL) {
|
||||
MG_ERROR(("flash write_fn == NULL"));
|
||||
struct mg_flash *f = MG_LFS_FLASH;
|
||||
if (f == NULL) {
|
||||
MG_ERROR(("no flash"));
|
||||
} else if (f->init_fn != NULL && !f->init_fn(f)) {
|
||||
MG_ERROR(("flash init failed"));
|
||||
} else if (f->secsz == 0 || f->size < size) {
|
||||
MG_ERROR(("secsz %zu flash_size %zu < size %zu", f->secsz, f->size,
|
||||
size));
|
||||
} else if (f->read_fn == NULL || f->write_fn == NULL) {
|
||||
MG_ERROR(("flash read_fn or write_fn == NULL"));
|
||||
} else {
|
||||
s_cfg.block_size = mg_flash->secsz;
|
||||
s_fs = (uint8_t *) mg_flash->start + mg_flash->size - size;
|
||||
s_cfg.block_size = f->secsz;
|
||||
s_fs = (uint8_t *) f->start + f->size - size;
|
||||
}
|
||||
#endif
|
||||
if (s_fs == NULL || s_cfg.block_size == 0 || size % s_cfg.block_size != 0) {
|
||||
// s_fs can be NULL for a flash that starts at 0, e.g. an SPI flash
|
||||
if ((LFS_USE_RAM && s_fs == NULL) || s_cfg.block_size == 0 ||
|
||||
size % s_cfg.block_size != 0) {
|
||||
MG_ERROR(("s_fs %p blk_size %zu size %zu", s_fs, s_cfg.block_size));
|
||||
} else {
|
||||
s_cfg.block_count = (lfs_size_t) (size / s_cfg.block_size);
|
||||
|
||||
@@ -17,4 +17,6 @@ struct dirent {
|
||||
};
|
||||
|
||||
typedef struct mg_lfs_fd DIR;
|
||||
|
||||
bool mg_lfs_init(size_t size);
|
||||
#endif
|
||||
@@ -1,4 +1,5 @@
|
||||
#include "ota.h"
|
||||
#include "flash.h"
|
||||
#include "http.h"
|
||||
#include "json.h"
|
||||
#include "log.h"
|
||||
@@ -6,6 +7,15 @@
|
||||
#include "tls.h"
|
||||
#include "url.h"
|
||||
#include "util.h"
|
||||
#ifdef MG_OTA_PUBLIC_KEY
|
||||
#include "sha256.h"
|
||||
#include "tls_p256.h"
|
||||
#endif
|
||||
|
||||
// In strict C mode, e.g. -std=c99, stdlib.h may not declare alloca()
|
||||
#if !defined(alloca) && defined(__GNUC__)
|
||||
#define alloca(size) __builtin_alloca(size)
|
||||
#endif
|
||||
|
||||
#ifndef MG_OTA_MAX_URL_LEN
|
||||
#define MG_OTA_MAX_URL_LEN 256
|
||||
@@ -277,3 +287,90 @@ void mg_ota_poll(struct mg_mgr *mgr) {
|
||||
mg_ota_url_check(mgr, MG_OTA_URL, MG_OTA_STATUS_FN);
|
||||
}
|
||||
}
|
||||
|
||||
static char *s_flash_addr; // Current address to write to
|
||||
static size_t s_size; // Firmware size to flash. In-progress indicator
|
||||
static uint32_t s_crc32; // Firmware checksum
|
||||
|
||||
bool mg_ota_flash_begin(size_t new_firmware_size, struct mg_flash *flash) {
|
||||
bool ok = false;
|
||||
if (s_size) {
|
||||
MG_ERROR(("OTA already in progress. Call mg_ota_end()"));
|
||||
} else {
|
||||
size_t half = flash->size / 2;
|
||||
s_crc32 = 0;
|
||||
s_flash_addr = (char *) flash->start + half;
|
||||
MG_DEBUG(("FW %lu bytes, max %lu", new_firmware_size, half));
|
||||
if (new_firmware_size < half) {
|
||||
ok = true;
|
||||
s_size = new_firmware_size;
|
||||
MG_INFO(("Starting OTA, firmware size %lu", s_size));
|
||||
} else {
|
||||
MG_ERROR(("Firmware %lu is too big to fit %lu", new_firmware_size, half - flash->align));
|
||||
}
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
bool mg_ota_flash_write(const void *buf, size_t len, struct mg_flash *flash) {
|
||||
bool ok = false;
|
||||
if (s_size == 0) {
|
||||
MG_ERROR(("OTA is not started, call mg_ota_begin()"));
|
||||
} else if (s_flash_addr + MG_ROUND_UP(len, flash->align) >
|
||||
(char *) flash->start + flash->size) {
|
||||
MG_ERROR(("Flash overflow: attempting to write past the flash boundary"));
|
||||
} else {
|
||||
size_t len_aligned_down = MG_ROUND_DOWN(len, flash->align);
|
||||
if (len_aligned_down) {
|
||||
ok = flash->write_fn(s_flash_addr, buf, len_aligned_down);
|
||||
}
|
||||
if (len_aligned_down < len) {
|
||||
size_t left = len - len_aligned_down;
|
||||
char *tmp = (char *) alloca(flash->align);
|
||||
memset(tmp, 0xff, flash->align);
|
||||
memcpy(tmp, (char *) buf + len_aligned_down, left);
|
||||
ok = flash->write_fn(s_flash_addr + len_aligned_down, tmp, flash->align);
|
||||
}
|
||||
s_crc32 = mg_crc32(s_crc32, (char *) buf, len); // Update CRC
|
||||
MG_DEBUG(("%#x %p %lu -> %d", s_flash_addr - len, buf, len, ok));
|
||||
s_flash_addr += len;
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
bool mg_ota_flash_end(struct mg_flash *flash) {
|
||||
char *base = (char *) flash->start + flash->size / 2;
|
||||
bool ok = false;
|
||||
int prev_state = MG_OTA_STATE_GET();
|
||||
if (s_size) {
|
||||
size_t size = (size_t) (s_flash_addr - base);
|
||||
uint32_t crc32 = mg_crc32(0, base, s_size);
|
||||
if (size == s_size && crc32 == s_crc32) ok = true;
|
||||
MG_DEBUG(("CRC: %x/%x, size: %lu/%lu, status: %s", s_crc32, crc32, s_size,
|
||||
size, ok ? "ok" : "fail"));
|
||||
#ifdef MG_OTA_PUBLIC_KEY
|
||||
if (ok) {
|
||||
bool signed_fw = s_size > 68 &&
|
||||
memcmp((uint8_t *) base + s_size - 4, "MGSG", 4) == 0;
|
||||
if (signed_fw) {
|
||||
static const uint8_t s_pubkey[] = MG_OTA_PUBLIC_KEY;
|
||||
uint8_t hash[32];
|
||||
size_t fw_size = s_size - 68; // strip 64-byte sig + 4-byte magic
|
||||
mg_sha256(hash, (uint8_t *) base, fw_size);
|
||||
ok = mg_p256_verify(s_pubkey, hash, sizeof(hash),
|
||||
(uint8_t *) base + fw_size) == 1;
|
||||
MG_INFO(("Signature: %s", ok ? "ok" : "fail"));
|
||||
} else {
|
||||
ok = false;
|
||||
MG_ERROR(("Unsigned firmware rejected"));
|
||||
}
|
||||
}
|
||||
#endif
|
||||
s_size = 0;
|
||||
if (ok) MG_OTA_STATE_SET(MG_OTA_TESTING);
|
||||
if (ok) ok = flash->swap_fn();
|
||||
if (!ok) MG_OTA_STATE_SET(prev_state); // undo state in case of failure
|
||||
}
|
||||
MG_INFO(("Finishing OTA: %s", ok ? "ok" : "fail"));
|
||||
return ok;
|
||||
}
|
||||
@@ -75,6 +75,13 @@ bool mg_ota_write(const void *buf, size_t len);
|
||||
// Ends the OTA process and commits the written firmware.
|
||||
bool mg_ota_end(void);
|
||||
|
||||
// Same as above, for a given flash. OTA backends use these to implement
|
||||
// mg_ota_begin(), mg_ota_write() and mg_ota_end()
|
||||
struct mg_flash;
|
||||
bool mg_ota_flash_begin(size_t new_firmware_size, struct mg_flash *flash);
|
||||
bool mg_ota_flash_write(const void *buf, size_t len, struct mg_flash *flash);
|
||||
bool mg_ota_flash_end(struct mg_flash *flash);
|
||||
|
||||
// Push-based OTA: mg_http_stream_body() callback that flashes the request
|
||||
//
|
||||
// ```c
|
||||
|
||||
@@ -14,8 +14,10 @@ static struct mg_flash s_mg_flash_ch32v307 = {
|
||||
480 * 1024, // Size, first 320k is 0-wait
|
||||
4 * 1024, // Sector size, 4k
|
||||
4, // Align, 32 bit
|
||||
mg_flash_read_xip,
|
||||
mg_ch32v307_write,
|
||||
mg_ch32v307_swap,
|
||||
NULL, // No init
|
||||
};
|
||||
|
||||
#define FLASH_BASE 0x40022000
|
||||
|
||||
@@ -85,8 +85,10 @@ static struct mg_flash s_mg_flash_imxrt = {
|
||||
4 * 1024 * 1024, // Size, 4mb
|
||||
MG_IMXRT_SECTOR_SIZE, // Sector size
|
||||
MG_IMXRT_PAGE_SIZE, // Align
|
||||
mg_flash_read_xip,
|
||||
mg_imxrt_write,
|
||||
mg_imxrt_swap,
|
||||
NULL, // No init
|
||||
};
|
||||
|
||||
struct mg_flexspi_lut_seq {
|
||||
|
||||
+15
-7
@@ -13,14 +13,17 @@
|
||||
|
||||
static bool mg_mcxn_write(void *, const void *, size_t);
|
||||
static bool mg_mcxn_swap(void);
|
||||
static bool mg_mcxn_init(struct mg_flash *);
|
||||
|
||||
static struct mg_flash s_mg_flash_mcxn = {
|
||||
(void *) 0, // Start, filled at init
|
||||
0, // Size, filled at init
|
||||
0, // Sector size, filled at init
|
||||
0, // Align, filled at init
|
||||
mg_flash_read_xip,
|
||||
mg_mcxn_write,
|
||||
mg_mcxn_swap,
|
||||
mg_mcxn_init,
|
||||
};
|
||||
|
||||
struct mg_flash_config {
|
||||
@@ -162,22 +165,27 @@ MG_IRAM static void single_bank_swap(char *p1, char *p2, size_t s, size_t ss) {
|
||||
*(volatile unsigned long *) 0xe000ed0c = 0x5fa0004;
|
||||
}
|
||||
|
||||
bool mg_ota_begin(size_t new_firmware_size) {
|
||||
// Fills flash start, size, sector size and alignment from the ROM driver
|
||||
static bool mg_mcxn_init(struct mg_flash *flash) {
|
||||
uint32_t status;
|
||||
struct mg_flash_config config;
|
||||
if ((status = mg_flash_driver->init(&config)) != 0) {
|
||||
MG_ERROR(("Flash driver init error: %lu", status));
|
||||
return false;
|
||||
}
|
||||
s_mg_flash_mcxn.start = (void *) config.addr;
|
||||
s_mg_flash_mcxn.size = config.size;
|
||||
s_mg_flash_mcxn.secsz = config.sector_size;
|
||||
s_mg_flash_mcxn.align = config.page_size;
|
||||
flash->start = (void *) config.addr;
|
||||
flash->size = config.size;
|
||||
flash->secsz = config.sector_size;
|
||||
flash->align = config.page_size;
|
||||
mg_flash_driver->deinit(&config);
|
||||
MG_DEBUG(
|
||||
("%lu-byte flash @%p, using %lu-byte sectors with %lu-byte-aligned pages",
|
||||
s_mg_flash_mcxn.size, s_mg_flash_mcxn.start, s_mg_flash_mcxn.secsz,
|
||||
s_mg_flash_mcxn.align));
|
||||
flash->size, flash->start, flash->secsz, flash->align));
|
||||
return true;
|
||||
}
|
||||
|
||||
bool mg_ota_begin(size_t new_firmware_size) {
|
||||
if (!mg_mcxn_init(&s_mg_flash_mcxn)) return false;
|
||||
return mg_ota_flash_begin(new_firmware_size, &s_mg_flash_mcxn);
|
||||
}
|
||||
|
||||
|
||||
+13
-6
@@ -37,8 +37,10 @@ static bool s_mcxw71_failed;
|
||||
|
||||
static bool mg_mcxw71_write(void *, const void *, size_t);
|
||||
static bool mg_mcxw71_swap(void);
|
||||
static bool mg_mcxw71_init(struct mg_flash *);
|
||||
static struct mg_flash s_mg_flash_mcxw71 = {
|
||||
(void *) 0, 0x100000, 8192, 16, mg_mcxw71_write, mg_mcxw71_swap};
|
||||
(void *) 0, 0x100000, 8192, 16,
|
||||
mg_flash_read_xip, mg_mcxw71_write, mg_mcxw71_swap, mg_mcxw71_init};
|
||||
struct mg_flash *mg_flash = &s_mg_flash_mcxw71;
|
||||
|
||||
// Everything called while flash is busy, or after the application has been
|
||||
@@ -110,17 +112,22 @@ MG_IRAM static void mg_mcxw71_apply(size_t size) {
|
||||
for (;;) __NOP();
|
||||
}
|
||||
|
||||
bool mg_ota_begin(size_t new_firmware_size) {
|
||||
uint32_t status;
|
||||
if (s_mcxw71_size != 0 || new_firmware_size == 0) return false;
|
||||
status = MG_MCXW71_FLASH_API->init(&s_mcxw71_config);
|
||||
// Initialises the ROM flash API and fills the flash size
|
||||
static bool mg_mcxw71_init(struct mg_flash *flash) {
|
||||
uint32_t status = MG_MCXW71_FLASH_API->init(&s_mcxw71_config);
|
||||
if (status != 0 || s_mcxw71_config.mem[0].base != 0 ||
|
||||
s_mcxw71_config.mem[0].size < 16384 ||
|
||||
(s_mcxw71_config.mem[0].size % 16384) != 0) {
|
||||
MG_ERROR(("Flash init failed: %lu", (unsigned long) status));
|
||||
return false;
|
||||
}
|
||||
s_mg_flash_mcxw71.size = s_mcxw71_config.mem[0].size;
|
||||
flash->size = s_mcxw71_config.mem[0].size;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool mg_ota_begin(size_t new_firmware_size) {
|
||||
if (s_mcxw71_size != 0 || new_firmware_size == 0) return false;
|
||||
if (!mg_mcxw71_init(&s_mg_flash_mcxw71)) return false;
|
||||
if (!mg_ota_flash_begin(new_firmware_size, &s_mg_flash_mcxw71)) return false;
|
||||
s_mcxw71_size = new_firmware_size;
|
||||
s_mcxw71_received = s_mcxw71_pending = 0;
|
||||
|
||||
+4
-1
@@ -22,7 +22,10 @@ static struct mg_flash s_mg_flash_picosdk = {
|
||||
#endif
|
||||
FLASH_SECTOR_SIZE, // Sector size, from hardware_flash
|
||||
FLASH_PAGE_SIZE, // Align, from hardware_flash
|
||||
mg_picosdk_write, mg_picosdk_swap,
|
||||
mg_flash_read_xip,
|
||||
mg_picosdk_write,
|
||||
mg_picosdk_swap,
|
||||
NULL, // No init
|
||||
};
|
||||
|
||||
#define MG_MODULO2(x, m) ((x) & ((m) -1))
|
||||
|
||||
+3
-1
@@ -12,8 +12,10 @@ static struct mg_flash s_mg_flash_frdm = {(void *) 0x08000000, // Start,
|
||||
0x200000, // Size
|
||||
0x1000, // Sector size
|
||||
0x100, // Align
|
||||
mg_flash_read_xip,
|
||||
mg_frdm_write,
|
||||
mg_frdm_swap};
|
||||
mg_frdm_swap,
|
||||
NULL}; // No init
|
||||
|
||||
struct mg_flexspi_lut_seq {
|
||||
uint8_t seqNum;
|
||||
|
||||
+11
-2
@@ -7,14 +7,17 @@
|
||||
|
||||
static bool mg_stm32f_write(void *, const void *, size_t);
|
||||
static bool mg_stm32f_swap(void);
|
||||
static bool mg_stm32f_init(struct mg_flash *);
|
||||
|
||||
static struct mg_flash s_mg_flash_stm32f = {
|
||||
(void *) 0x08000000, // Start
|
||||
0, // Size, FLASH_SIZE_REG
|
||||
0, // Size, FLASH_SIZE_REG, set by init
|
||||
0, // Irregular sector size
|
||||
32, // Align, 256 bit
|
||||
mg_flash_read_xip,
|
||||
mg_stm32f_write,
|
||||
mg_stm32f_swap,
|
||||
mg_stm32f_init,
|
||||
};
|
||||
|
||||
#define MG_FLASH_BASE 0x40023c00
|
||||
@@ -224,9 +227,15 @@ MG_IRAM static void single_bank_swap(char *p1, char *p2, size_t s, size_t ss) {
|
||||
*(volatile uint32_t *) 0xe000ed0cU = 0x5fa0004U; // NVIC_SystemReset()
|
||||
}
|
||||
|
||||
// Fills the flash size, read from the chip
|
||||
static bool mg_stm32f_init(struct mg_flash *flash) {
|
||||
flash->size = flash_size();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool mg_ota_begin(size_t new_firmware_size) {
|
||||
size_t max;
|
||||
s_mg_flash_stm32f.size = flash_size();
|
||||
mg_stm32f_init(&s_mg_flash_stm32f);
|
||||
#ifdef __ZEPHYR__
|
||||
*((uint32_t *)0xE000ED94) = 0;
|
||||
MG_DEBUG(("Jailbreak %s", *((uint32_t *)0xE000ED94) == 0 ? "successful" : "failed"));
|
||||
|
||||
+11
-2
@@ -7,14 +7,17 @@
|
||||
|
||||
static bool mg_stm32h5_write(void *, const void *, size_t);
|
||||
static bool mg_stm32h5_swap(void);
|
||||
static bool mg_stm32h5_init(struct mg_flash *);
|
||||
|
||||
static struct mg_flash s_mg_flash_stm32h5 = {
|
||||
(void *) 0x08000000, // Start
|
||||
0, // Size, FLASHSIZE_BASE
|
||||
0, // Size, FLASHSIZE_BASE, set by init
|
||||
8 * 1024, // Sector size, 8k
|
||||
16, // Align, 128 bit
|
||||
mg_flash_read_xip,
|
||||
mg_stm32h5_write,
|
||||
mg_stm32h5_swap,
|
||||
mg_stm32h5_init,
|
||||
};
|
||||
|
||||
#define MG_FLASH_BASE 0x40022000 // Base address of the flash controller
|
||||
@@ -147,8 +150,14 @@ static bool mg_stm32h5_write(void *addr, const void *buf, size_t len) {
|
||||
return ok;
|
||||
}
|
||||
|
||||
// Fills the flash size, read from the chip
|
||||
static bool mg_stm32h5_init(struct mg_flash *flash) {
|
||||
flash->size = flash_size();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool mg_ota_begin(size_t new_firmware_size) {
|
||||
s_mg_flash_stm32h5.size = flash_size();
|
||||
mg_stm32h5_init(&s_mg_flash_stm32h5);
|
||||
#ifdef __ZEPHYR__
|
||||
*((uint32_t *)0xE000ED94) = 0;
|
||||
MG_DEBUG(("Jailbreak %s", *((uint32_t *)0xE000ED94) == 0 ? "successful" : "failed"));
|
||||
|
||||
+11
-2
@@ -14,14 +14,17 @@
|
||||
|
||||
static bool mg_stm32h7_write(void *, const void *, size_t);
|
||||
static bool mg_stm32h7_swap(void);
|
||||
static bool mg_stm32h7_init(struct mg_flash *);
|
||||
|
||||
static struct mg_flash s_mg_flash_stm32h7 = {
|
||||
(void *) 0x08000000, // Start
|
||||
0, // Size, FLASH_SIZE_REG
|
||||
0, // Size, FLASH_SIZE_REG, set by init
|
||||
128 * 1024, // Sector size, 128k
|
||||
32, // Align, 256 bit
|
||||
mg_flash_read_xip,
|
||||
mg_stm32h7_write,
|
||||
mg_stm32h7_swap,
|
||||
mg_stm32h7_init,
|
||||
};
|
||||
|
||||
#define FLASH_BASE1 0x52002000 // Base address for bank1
|
||||
@@ -207,8 +210,14 @@ MG_IRAM static void single_bank_swap(char *p1, char *p2, size_t s, size_t ss) {
|
||||
*(volatile uint32_t *) 0xe000ed0cU = 0x5fa0004U; // NVIC_SystemReset()
|
||||
}
|
||||
|
||||
// Fills the flash size, read from the chip
|
||||
static bool mg_stm32h7_init(struct mg_flash *flash) {
|
||||
flash->size = flash_size();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool mg_ota_begin(size_t new_firmware_size) {
|
||||
s_mg_flash_stm32h7.size = flash_size();
|
||||
mg_stm32h7_init(&s_mg_flash_stm32h7);
|
||||
#ifdef __ZEPHYR__
|
||||
*((uint32_t *) 0xE000ED94) = 0;
|
||||
MG_DEBUG(("Jailbreak %s", *((uint32_t *) 0xE000ED94) == 0 ? "ok" : "failed"));
|
||||
|
||||
+1
-1
@@ -3334,7 +3334,7 @@ static void test_http_stream_errors(void) {
|
||||
ASSERT(state.ends == 1);
|
||||
if (c.is_client) {
|
||||
ASSERT(c.send.len == 0);
|
||||
ASSERT(c.is_closing == (cases[i].code != 200));
|
||||
ASSERT(c.is_closing == (unsigned) (cases[i].code != 200));
|
||||
} else {
|
||||
ASSERT(mg_http_parse((char *) c.send.buf, c.send.len, &hm) > 0);
|
||||
ASSERT(mg_http_status(&hm) == cases[i].code);
|
||||
|
||||
Reference in new issue
Block a user