Commit Graph

3523 Commits

Author SHA1 Message Date
Leah Rowe b291bbf2e5 util/nvmutil: Make rw_file_exact an ssize_t
Use its return value. Don't exit from the function,
but actually treat it like a real syscall.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 15:03:16 +00:00
Leah Rowe 4bc7ba1e4b util/nvmutil: use O_NONBLOCK on /dev/[u]random
on some systems, it is otherwise blocking, but blocking
can be disabled, making access more reliable.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 14:25:43 +00:00
Leah Rowe c953228bb0 util/nvmutil: fix possible overflow: gbe_x_offset
preventative fix, since the values are currently
quite tiny. this new check is the same, but goes
the other way to eliminate overflow.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 14:18:19 +00:00
Leah Rowe 883860d687 util/nvmutil: reset rw_file_exact errno on EINTR
this is essentially what it already did, but it
wasn't explicitly stated. now it's clearer.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 14:12:45 +00:00
Leah Rowe 6eefd80efe util/nvmutil: comment prw()
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 14:07:35 +00:00
Leah Rowe 06cb129530 util/nvmutil: better SSIZE_MAX define
the old one assumes that ssize_t is signed size_t,
which let's face it, is always true in practise,
but not actually guaranteed!

so now i'm using one that's even more pedantic.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 13:42:43 +00:00
Leah Rowe 79106c5b3d util/nvmutil: define EXIT_FAILURE/SUCCESS
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 13:34:40 +00:00
Leah Rowe adfe865afc util/nvmutil: more sensible errno init
just use errno itself as input to err

if unset, it's set to ECANCELED anyway

i really should rewrite the error handling
to not use errno at some point. it's a bit
unreliable, on some unix systems.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 13:32:29 +00:00
Leah Rowe 4202ded96c util/nvmutil: proper errno status on prw()
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 13:21:34 +00:00
Leah Rowe ee751c27ed util/nvmutil: reset errno if EINTR on lseek
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 12:32:42 +00:00
Leah Rowe bbe6de44e8 util/nvmutil: stricter errno on prw()
we want the first error to be the one shown,
when returning negative

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 11:25:27 +00:00
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