From db9f7cf588f7e6df981d69687f967b9a2be7099a Mon Sep 17 00:00:00 2001 From: Leah Rowe Date: Wed, 29 Jan 2025 03:41:55 +0000 Subject: [PATCH] util/nvmutil: Only read up to 4KB on larger gbe On the 16KB and 128KB files, we still only need to operate on 4KB at the start of each block, where the block size is larger than 4KB. The reason we deal with the entire 4KB block is because the nvm words (in the 128 byte section) can define an extended nvm area anywhere after 128 bytes, within the 128 byte block. We could systematically read where that is being handled, and handle it; we could then allocate less memory, and read/write fewer bytes, but many block devices like SSDs and flash drives have at least a 4KB erase block anyway, so it's kinda pointless. saving memory would be nice, but I don't really want to bloat the code. This is a nice easy optimisation, to avoid wasting an additional 8KB of memory when handling 16KB files, and additional 120KB if handling 128KB files, since nf is what determines how much memory will be allocated. the alternative would be to use an mmap, and then we could reasonably handle the idea above for only writing, surgically, what we need: nvm words and extended nvm words. Signed-off-by: Leah Rowe --- util/nvmutil/nvmutil.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/util/nvmutil/nvmutil.c b/util/nvmutil/nvmutil.c index dacfeaa..5ee990f 100644 --- a/util/nvmutil/nvmutil.c +++ b/util/nvmutil/nvmutil.c @@ -29,6 +29,7 @@ uint8_t hextonum(char chs), rhex(void); #define NVM_CHECKSUM_WORD 0x3F /* checksum word position */ #define NVM_SIZE 128 /* Area containing NVM words */ +#define SIZE_4KB 0x1000 #define SIZE_8KB 0x2000 #define SIZE_16KB 0x4000 #define SIZE_128KB 0x20000 @@ -211,7 +212,8 @@ void readGbe(void) { if ((cmd == cmd_swap) || (cmd == cmd_copy)) - nf = partsize; /* read/write the entire block */ + nf = SIZE_4KB; /* read/write the entire block */ + /* only need to do 4KB even on larger gbe files */ else nf = NVM_SIZE; /* only read/write the nvm part of the block */