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 <leah@libreboot.org>
master
Leah Rowe 2025-01-29 03:41:55 +00:00
parent 71f6b631e3
commit db9f7cf588
1 changed files with 3 additions and 1 deletions

View File

@ -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 */