diff options
author | Luke T. Shumaker <lukeshu@lukeshu.com> | 2025-07-03 08:00:50 -0600 |
---|---|---|
committer | Luke T. Shumaker <lukeshu@lukeshu.com> | 2025-07-03 08:00:50 -0600 |
commit | 1d5a211e1ae3645e9c13163b76fea0a3c87f46f1 (patch) | |
tree | 610128434e35b9b6028609d7cb5d67bcd13b3379 /flashimg/cpu_main/fs_harness_flash_bin.c | |
parent | 1be02f3e2379b2cd06dbae775504948ff37bf89a (diff) | |
parent | ad2ef1642096665be998e83f9b6c4b7de308b644 (diff) |
Diffstat (limited to 'flashimg/cpu_main/fs_harness_flash_bin.c')
-rw-r--r-- | flashimg/cpu_main/fs_harness_flash_bin.c | 179 |
1 files changed, 33 insertions, 146 deletions
diff --git a/flashimg/cpu_main/fs_harness_flash_bin.c b/flashimg/cpu_main/fs_harness_flash_bin.c index 7b41c86..0fa674a 100644 --- a/flashimg/cpu_main/fs_harness_flash_bin.c +++ b/flashimg/cpu_main/fs_harness_flash_bin.c @@ -14,7 +14,10 @@ #include <util9p/static.h> -#define IMPLEMENTATION_FOR_FS_HARNESS_FLASH_BIN YES +#define IMPLEMENTATION_FOR_FLASHIO_H YES /* so we can reuse the buffer */ +#include "flashio.h" + +#define IMPLEMENTATION_FOR_FS_HARNESS_FLASH_BIN_H YES #include "fs_harness_flash_bin.h" LO_IMPLEMENTATION_C(lib9p_srv_file, struct flash_file, flash_file); @@ -64,15 +67,9 @@ static_assert(DATA_HSIZE % FLASH_SECTOR_SIZE == 0); * * @param buf : a scratch buffer that is at least FLASH_SECTOR_SIZE */ -static void ab_flash_initialize(void *buf) { - assert(buf); - - memset(buf, 0xFF, FLASH_SECTOR_SIZE); - +static void ab_flash_initialize(void) { log_infoln("erasing upper flash..."); for (size_t off = DATA_HSIZE; off < DATA_SIZE; off += FLASH_SECTOR_SIZE) { - if (memcmp(buf, DATA_START+off, FLASH_SECTOR_SIZE) == 0) - continue; bool saved = cr_save_and_disable_interrupts(); flash_range_erase(off, FLASH_SECTOR_SIZE); cr_restore_interrupts(saved); @@ -80,134 +77,23 @@ static void ab_flash_initialize(void *buf) { log_debugln("... erased"); } -/** - * Write `dat` to flash sector `pos`+(DATA_SIZE/2) (i.e. `pos` is a - * sector in the lower half, but this function writes to the upper - * half). - * - * @param pos : start-position of the sector to write to - * @param dat : the FLASH_SECTOR_SIZE bytes to write - */ -static void ab_flash_write_sector(size_t pos, uint8_t *dat) { - assert(pos < DATA_HSIZE); - assert(pos % FLASH_SECTOR_SIZE == 0); - assert(dat); - - pos += DATA_HSIZE; - - log_infoln("write flash sector @ ", (base16_u32_, pos), "..."); - if (memcmp(dat, DATA_START+pos, FLASH_SECTOR_SIZE) != 0) { - bool saved = cr_save_and_disable_interrupts(); - flash_range_erase(pos, FLASH_SECTOR_SIZE); - flash_range_program(pos, dat, FLASH_SECTOR_SIZE); - cr_restore_interrupts(saved); - } - log_debugln("... written"); -} - -/* io_preader_to, io_pwriter, io_closer ***************************************/ - -LO_IMPLEMENTATION_STATIC(io_preader_to, struct flashio, flashio); -LO_IMPLEMENTATION_STATIC(io_pwriter, struct flashio, flashio); -LO_IMPLEMENTATION_STATIC(io_closer, struct flashio, flashio); - -/** read from anywhere on the chip */ -static size_t_and_error flashio_pread_to(struct flashio *self, lo_interface io_writer dst, uoff_t _src_offset, size_t count) { - assert(self); - - if (_src_offset > DATA_SIZE) - return ERROR_AND(size_t, 0, error_new(E_POSIX_EINVAL, "offset is past the chip size")); - size_t src_offset = (size_t) _src_offset; - - if (src_offset == DATA_SIZE) - return ERROR_AND(size_t, 0, error_new(E_EOF)); - - /* Assume that somewhere down the line the pointer we pass to - * io_write() will be passed to DMA. We don't want the DMA - * engine to hit (slow) XIP (for instance, this can cause - * reads/writes to the SSP to get out of sync with eachother). - * So, copy the data to a buffer in (fast) RAM first. It's - * lame that the DMA engine can only have a DREQ on one side - * of the channel. - */ - size_t sector_base = LM_ROUND_DOWN(src_offset, FLASH_SECTOR_SIZE); - if (src_offset + count > sector_base + FLASH_SECTOR_SIZE) - count = (sector_base + FLASH_SECTOR_SIZE) - src_offset; - assert(src_offset + count <= DATA_SIZE); - - if (!self->rbuf.ok || self->rbuf.pos != sector_base) { - self->rbuf.ok = true; - self->rbuf.pos = sector_base; - memcpy(self->rbuf.dat, DATA_START+sector_base, FLASH_SECTOR_SIZE); - } - - return io_write(dst, &self->rbuf.dat[src_offset-sector_base], count); -} - -/** takes offsets in the lower half, writes to the upper half */ -static size_t_and_error flashio_pwritev(struct flashio *self, const struct wr_iovec *iov, int iovcnt, uoff_t _offset) { - assert(self); - assert(iov); - assert(iovcnt > 0); - - size_t total_count = 0; - for (int i = 0; i < iovcnt; i++) - total_count += iov[i].iov_len; - assert(total_count > 0); - - if (_offset >= DATA_HSIZE) - return ERROR_AND(size_t, 0, error_new(E_POSIX_ENOSPC, "cannot write past half the chip size")); - size_t offset = (size_t) _offset; - - size_t total_done = 0; - for (int i = 0; i < iovcnt; i++) { - size_t iov_done = 0; - while (iov_done < iov[i].iov_len) { - if (offset >= DATA_HSIZE) - return ERROR_AND(size_t, total_done, error_new(E_POSIX_ENOSPC, "cannot write past half the chip size")); - size_t sector_base = LM_ROUND_DOWN(offset, FLASH_SECTOR_SIZE); - size_t len = iov[i].iov_len - iov_done; - if (offset + len > sector_base + FLASH_SECTOR_SIZE) - len = (sector_base + FLASH_SECTOR_SIZE) - offset; - assert(offset + len <= DATA_HSIZE); - - if (self->wbuf.ok && self->wbuf.pos != sector_base) - ab_flash_write_sector(self->wbuf.pos, self->wbuf.dat); - if (!self->wbuf.ok || self->wbuf.pos != sector_base) { - self->wbuf.ok = true; - self->wbuf.pos = sector_base; - if (len != FLASH_SECTOR_SIZE) - /* Don't bother with a read if we're just going to overwrite it. */ - memcpy(self->wbuf.dat, DATA_START+DATA_HSIZE+sector_base, FLASH_SECTOR_SIZE); - } - memcpy(&self->wbuf.dat[offset-sector_base], iov[i].iov_write_from+iov_done, len); - total_done += len; - iov_done += len; - offset += len; - } - } - return ERROR_AND(size_t, total_done, ERROR_NULL); -} +/* srv_file *******************************************************************/ -static error flashio_close(struct flashio *self) { +#ifdef NDEBUG +#define flash_file_assert(self) ((void)0) +#else +void flash_file_assert(struct flash_file *self) { assert(self); - - if (self->finalize) { - if (self->wbuf.ok) - ab_flash_write_sector(self->wbuf.pos, self->wbuf.dat); - ab_flash_finalize(self->wbuf.dat); - } - - return ERROR_NULL; + assert(self->name); + assert(self->io); } - -/* srv_file *******************************************************************/ +#endif void flash_file_free(struct flash_file *self) { - assert(self); + flash_file_assert(self); } struct lib9p_qid flash_file_qid(struct flash_file *self) { - assert(self); + flash_file_assert(self); return (struct lib9p_qid){ .type = LIB9P_QT_FILE|LIB9P_QT_EXCL|LIB9P_QT_APPEND, @@ -217,7 +103,7 @@ struct lib9p_qid flash_file_qid(struct flash_file *self) { } error flash_file_stat(struct flash_file *self, struct lib9p_srv_ctx *ctx, struct lib9p_srv_stat *out) { - assert(self); + flash_file_assert(self); assert(ctx); assert(out); @@ -236,13 +122,13 @@ error flash_file_stat(struct flash_file *self, struct lib9p_srv_ctx *ctx, struct return ERROR_NULL; } error flash_file_wstat(struct flash_file *self, struct lib9p_srv_ctx *ctx, struct lib9p_srv_stat) { - assert(self); + flash_file_assert(self); assert(ctx); return error_new(E_POSIX_EROFS, "read-only part of filesystem"); } error flash_file_remove(struct flash_file *self, struct lib9p_srv_ctx *ctx) { - assert(self); + flash_file_assert(self); assert(ctx); return error_new(E_POSIX_EROFS, "read-only part of filesystem"); @@ -255,13 +141,11 @@ static error flash_handle_ihex_eof(void *); lib9p_srv_fio_or_error flash_file_fopen(struct flash_file *self, struct lib9p_srv_ctx *ctx, bool LM_UNUSED(rd), bool wr, bool LM_UNUSED(trunc)) { - assert(self); + flash_file_assert(self); assert(ctx); - memset(&self->io, 0, sizeof(self->io)); - if (wr) { - ab_flash_initialize(self->io.wbuf.dat); - } + if (wr) + ab_flash_initialize(); memset(&self->ihex, 0, sizeof(self->ihex)); self->ihex.handle_arg = self; @@ -277,26 +161,29 @@ static struct lib9p_qid flash_file_ioqid(struct flash_file *self) { return flash_file_qid(self); } static uint32_t flash_file_iounit(struct flash_file *self) { - assert(self); + flash_file_assert(self); return FLASH_SECTOR_SIZE; } static void flash_file_iofree(struct flash_file *self) { - assert(self); + flash_file_assert(self); - error err = flashio_close(&self->io); + error err = flashio_flush(self->io); assert(ERROR_IS_NULL(err)); err = ihex_decoder_close(&self->ihex); assert(ERROR_IS_NULL(err)); + + if (self->finalize) + ab_flash_finalize(self->io->dat); } static error flash_file_pread(struct flash_file *self, struct lib9p_srv_ctx *ctx, lo_interface io_writer dst, uint64_t byte_offset, uint32_t byte_count) { - assert(self); + flash_file_assert(self); assert(ctx); - return flashio_pread_to(&self->io, dst, byte_offset, byte_count).err; + return flashio_pread_to(self->io, dst, byte_offset, byte_count).err; } static error flash_handle_ihex_data(void *_self, uint32_t off, uint8_t count, uint8_t *dat) { @@ -306,13 +193,13 @@ static error flash_handle_ihex_data(void *_self, uint32_t off, uint8_t count, ui return error_new(E_POSIX_ENOSPC, "cannot write outside of the first half of the chip: ", (base16_u32_, off), " is outside of [", (base16_u32_, XIP_BASE), ",", (base16_u32_, XIP_BASE + DATA_HSIZE), ")"); - return flashio_pwritev(&self->io, + return flashio_pwritev(self->io, &((struct wr_iovec){.iov_write_from = dat, .iov_len = count}), 1, - off - XIP_BASE).err; + off - XIP_BASE + DATA_HSIZE).err; } static error flash_handle_ihex_eof(void *_self) { struct flash_file *self = _self; - self->io.finalize = true; + self->finalize = true; return ERROR_NULL; } @@ -321,7 +208,7 @@ static uint32_t_or_error flash_file_pwrite(struct flash_file *self, struct lib9p const void *buf, uint32_t byte_count, uint64_t LM_UNUSED(byte_offset)) { - assert(self); + flash_file_assert(self); assert(ctx); size_t_and_error r = ihex_decoder_writev(&self->ihex, |