Commit Graph

3512 Commits

Author SHA1 Message Date
Leah Rowe 5603fa51d2 util/nvmutil: fix printf c89/c90 specifiers
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 11:15:43 +00:00
Leah Rowe 7431046ea6 util/nvmutil: reset part_valid
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 11:08:42 +00:00
Leah Rowe 632c85ce1c util/nvmutil: restore errno if lseek resets it
if it resets it on success, that is!

theoretically possible. we must preserve errno.

normally i'm a bit more casual about it, but this
function is replicating libc, so i must be strict

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 11:06:31 +00:00
Leah Rowe 8d156bcf35 util/nvmutil: fix another printf specifier
ditto to last commit

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 11:02:39 +00:00
Leah Rowe fa859d80d5 util/nvmutil: fix bad print specifier (c90)
size_t may be unsigned long long, but lu
is for unsigned long. the integer is small
enough that we don't need to worry, so let's
just cast it accordingly (inside err)

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 10:58:41 +00:00
Leah Rowe 6778a4ed98 util/nvmutil: only inc num_invalid on bad checksum
this fixes a regression that i introduced

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 10:56:56 +00:00
Leah Rowe e3e02fa657 util/nvmutil: restore errno on failed offset restore
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 10:53:11 +00:00
Leah Rowe 6408570fa1 util/nvmutil: don't leave part_valid untouched
always set it. the current logic only sets it if
valid, but invalid doesn't, relying on global
initialisation. this check sets it explicitly.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 10:48:16 +00:00
Leah Rowe 090a702b74 util/nvmutil: remove unused st variable
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 10:44:27 +00:00
Leah Rowe cbd7ad13a3 util/nvmutil: check whether a file is a file
and not, say, a socket or a directory, or
a character device, or something else.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 10:35:51 +00:00
Leah Rowe 91a6395e5c util/nvmutil: preserve errno during i/o
do not clobber errno

yeah we're basically being libc now

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 10:26:12 +00:00
Leah Rowe 5a005eff9e util/nvmutil prw: always restore original offset
it currently only does so on success, but errors will
leave the file descriptor corrupted.

reset it accordingly.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 10:24:16 +00:00
Leah Rowe 890d1a2ff6 util/nvmutil: err if file offset fails
currently it returns success, if restoring a
previous offset failed. this leaves descriptor
corrupted when the caller thinks otherwise

return -1 instead, so that the caller can treat
it as an error, relying on whatever lseek had
set for errno

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 10:18:25 +00:00
Leah Rowe b56cfbcc54 util/nvmutil: fix buffer overread in prw()
edge case scenario, unlikely to actually trigger.

now impossible to trigger.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 10:14:25 +00:00
Leah Rowe 19ee28161e util/nvmutil: fix rc overflow bug in rw_file_exact
check that it's below len, not above it. that way, it
will now exit if it goes above (which it shouldn't,
but it theoretically could if the code was changed
and there was a regression or subtle edge case)

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 10:00:20 +00:00
Leah Rowe 6d27853f56 util/nvmutil: use C90 instead of C99
with the other changes made recently, super old
compilers now work.

yes, i needed to change some specifiers in printf.

typedefs provided for uint, and a define included
X OPEN SOURCE 500. and asserts for integers.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 09:25:27 +00:00
Leah Rowe f2d982e9b3 util/nvmutil: define O_BINARY flag
use it

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 09:17:45 +00:00
Leah Rowe 630852b7be util/nvmutil: fix indent on ifdefs
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 09:15:55 +00:00
Leah Rowe e9a593b2c0 util/nvmutil: define SIZE_MAX if not defined
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 09:10:43 +00:00
Leah Rowe 7620d1d126 util/nvmutil: remove arc4random for portability
just use /dev/urandom and fall back to /dev/random

this is what i was doing for years. this combined
with other changes, and the new prw() function
for i/o, means portability should be pretty high
now. i will actually start testing nvmutil on old
bsd systems from the 90s later.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 09:07:15 +00:00
Leah Rowe 21f8d323f4 util/nvmutil: portable pread/pwrite
not thread-safe

lucky we're single-threaded!

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 09:04:20 +00:00
Leah Rowe 5ae5d53751 wip
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 08:44:56 +00:00
Leah Rowe edb1508a59 util/nvmutil: more reliable stdint.h check
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 08:26:20 +00:00
Leah Rowe 2a20251ad6 util/nvmutil: reset errno before run_cmd
in case any stale errors are present.

at this point, we know that the code is likely
safe and that nothing happened, because we quite
obsessively call err() before that point.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 07:12:55 +00:00
Leah Rowe d2cd126775 util/nvmutil: explicitly check cmd nullptr
null isn't guaranteed to be zero

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 07:05:34 +00:00
Leah Rowe 978c30a961 util/nvmutil: safer SSIZE_MAX define
the current one assumes two's compliment and no
padding bits. i assert two's compliment earlier
in code, but it doesn't guarantee:

sizeof(ssize_t) == sizeof(size_t)

it's theoretically possible that size_t=64
and ssize_t=32, and then the macro would break.

this new version uses SIZE_MAX instead, without
subtraction, but halves it using a bit shift.

this may still break, but it should work nicely.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 06:56:02 +00:00
Leah Rowe a6d0146a3b util/nvmutil: fix a bad cast (or lack thereof)
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 06:53:39 +00:00
Leah Rowe e26511c8b8 util/nvmutil: err if unsupported rw_type on i/o
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 06:51:47 +00:00
Leah Rowe 0a55f286c1 util/nvmutil: rename badly named off_t assert
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 06:43:13 +00:00
Leah Rowe 0f035e208e util/nvmutil: assert two's compliment integers
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 06:35:17 +00:00
Leah Rowe 6392eb18b6 util/nvmutil: add assert for int
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 06:31:04 +00:00
Leah Rowe 83f3a059b9 util/nvmutil: add assert for off_t
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 06:30:52 +00:00
Leah Rowe 6ad7eaef32 Revert "util/nvmutil: don't use zx printf specifier"
This reverts commit ba3cf14faa.
2026-03-10 06:18:29 +00:00
Leah Rowe 90fada0a23 util/nvmutil: explain errval handling in err()
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 06:17:04 +00:00
Leah Rowe 2fda988da6 util/nvmutil: make rc size_t (not ssize_t)
i overlooked this when writing. it's comparing
to a length which is size_t, so let's avoid
an unnecessary cast.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 06:11:31 +00:00
Leah Rowe ba3cf14faa util/nvmutil: don't use zx printf specifier
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 06:10:20 +00:00
Leah Rowe 2f1f129753 util/nvmutil: add portable asserts for integers
we need this to be the case for our code, that char
and uint8_t are 8 bits, and that uint16_t and uint32_t
are 16- and 32-bit.

these asserts protect us in case it's not (it will cause
a compile time error).

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 06:07:21 +00:00
Leah Rowe 9747ca4151 util/nvmutil: abort if I/O len exceeds SSIZE_MAX
in rw_file_exact

otherwise, if length exceeds SSIZE_MAX, we could
hit an overflow

the buffers and lengths we deal with are relatively
small anyway, so this fix is preventative

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 05:33:12 +00:00
Leah Rowe 93a4ec3497 util/nvmutil: annotate the prototypes
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 05:11:32 +00:00
Leah Rowe f53a8d4f18 util/nvmutil: unified gbe file part I/O
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 04:58:24 +00:00
Leah Rowe baca2d8883 util/nvmutil: remove stale define
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 04:20:55 +00:00
Leah Rowe 80f3aac62d util/nvmutil: unified I/O: stdout, urandom and gbe
everything is a file

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 04:13:02 +00:00
Leah Rowe 6402a0fbe9 util/nvmutil: unified urandom/gbe file reading
like before, but with the newly correct logic

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 03:08:09 +00:00
Leah Rowe 4131402589 util/nvmutil: safer read_gbe_file_exact
it now retries infinitely on EINTR, except when the return
of pread is precisely zero, at which point it errs.

this is better than having an arbitrary maximum like before,
and increases robustness on unreliable file systems, e.g.
NFS shares.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 02:48:34 +00:00
Leah Rowe 0c23474322 util/nvmutil: report checksum in cmd_dump
as it should be!

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 01:51:18 +00:00
Leah Rowe 84a9e8f89b util/nvmutil: reduce checksum report verbosity
only print a message what arg_part is set. this
means that a checksum error message won't be printed
on cat commands.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 01:38:22 +00:00
Leah Rowe 007dece09e util/nvmutil: unified io flags
don't hardcode it per command logically. do it in
the command table instead.

this also fixes a bug where the cat commands did
not set the permissions read-only.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 01:29:12 +00:00
Leah Rowe bb9010fdc1 util/nvmutil: require good checksum on cat
since the cat command can be used to create bad
gbe files, if the checksums don't match. my rule
is that nvmutil must never be used to destroy
data, only correct it (e.g. a file with just one
valid part can have it copied to the other part,
but you can't copy a bad part - and i removed
the "brick" command).

i *did* disable checksum requirements on the
dump command. with this, you can check the nvm
area and it tells you what the correct checksum
could be. then you could just correct it in a
hex editor if you wanted to, quite easily.

the idea is to slow down the act of destroying
or corrupting data as much as possible. someone
wily enough can use a hex editor to patch up some
files just fine.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 01:04:28 +00:00
Leah Rowe 08b1d95874 util/nvmutil: initialise w in gbe_cat_buf
no build error at the moment, nor would there be if
using clang or gcc, but i imagine some buggy compilers
might complain.

remember: portability. i also want this code to compile
on old, buggy compilers.

logically, this initialisation is redundant.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 00:53:53 +00:00
Leah Rowe 8f3bc13ac5 util/nvmutil: simplify the cat command
the current test is a bit over-engineered, so
i simplified it.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 00:46:18 +00:00