From 928cf1cf58b7cc11a388be539c448191d507425f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20van=20Dorst?= Date: Tue, 7 Oct 2025 21:12:08 +0200 Subject: [PATCH] http: upload: make sure we don't cross flash page boundaries. This causes the address pointer to wrap back to the front of the page. Which results in that the beginnen of the page can have data for the next page. --- httpd/httpd.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/httpd/httpd.c b/httpd/httpd.c index 704a3dd..b376399 100644 --- a/httpd/httpd.c +++ b/httpd/httpd.c @@ -10,6 +10,9 @@ // Upload Firmware to 1M #define FIRMWARE_UPLOAD_START 0x100000 +// SPI FLASH MEMORY PAGE SIZE. +#define FLASHMEM_PAGE_SIZE 0x100 + #define CMARK_S 6 #pragma codeseg BANK1 @@ -236,13 +239,18 @@ uint8_t stream_upload(uint16_t bptr) bindex = 0; } flash_buf[write_len++] = p[bptr++]; - if (write_len >= 256) { + if (write_len >= FLASHMEM_PAGE_SIZE) { print_string("len: "); print_short(write_len); write_char(' '); flash_region.addr = uptr; - flash_region.len = write_len; + flash_region.len = FLASHMEM_PAGE_SIZE; flash_write_bytes(flash_buf); - uptr += write_len; - write_len = 0; + uptr += FLASHMEM_PAGE_SIZE; + write_len -= FLASHMEM_PAGE_SIZE; + + // Copy the remaining byte for the next page to the beginning of the buffer. + if (write_len > 0) { + memcpy(flash_buf, flash_buf + FLASHMEM_PAGE_SIZE, write_len); + } } bindex = 0; }