Commit Graph

3785 Commits

Author SHA1 Message Date
Leah Rowe
9ba84d1117 util/nvmutil: reset errno in gbe_cat_buf
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 15:58:57 +00:00
Leah Rowe
ca37bef34f util/nvmutil: fix bad loop in command sanitizer
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 15:55:05 +00:00
Leah Rowe
9691e7e7db util/nvmutil: use set_err where appropriate
in the new file i/o functions, my own setting
of errno should be done with set_err. this
avoids clobbering what the real libc set.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 15:50:45 +00:00
Leah Rowe
59eac512eb util/nvmutil: properly set errno everywhere
i set it to ecanceled before. now i set it more
appropriately, for each type of error.

where a real syscall was called, or my file i/o
functions are used, err() is called with errno
itself as input, to avoid clobbering real errno.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 15:46:30 +00:00
Leah Rowe
3174806b3f util/nvmutil: fix /dev/random fallback
i forgot to set urandom_fd

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 15:38:30 +00:00
Leah Rowe
318c0eb95b Revert "util/nvmutil: fix check in command sanitizer"
This reverts commit b28076557b.

nope. the check was already correct before.
2026-03-10 15:36:43 +00:00
Leah Rowe
9b6b89250d util/nvmutil: don't use bad pointer cast in prw
in practise it's ok, but some compilers might complain.

all this change costs is a bit of branching inside a
loop, but compilers will sort that out.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 15:33:50 +00:00
Leah Rowe
b28076557b util/nvmutil: fix check in command sanitizer
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 15:30:10 +00:00
Leah Rowe
0b4e298cb1 util/nvmutil: use EINVAL in command sanitizen
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 15:29:23 +00:00
Leah Rowe
4819dcbc75 util/nvmutil: don't use xopen() for urandom
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 15:27:24 +00:00
Leah Rowe
9427285dce util/nvmutil: conservative use of errno
only use errno itself as the value

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 15:19:09 +00:00
Leah Rowe
1f953359cb util/nvmutil: re-try /dev/[u]random on EAGAIN
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 15:12:12 +00:00
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