Commit Graph

355 Commits

Author SHA1 Message Date
Leah Rowe
0d3a8749fe util/nvmutil: usleep 1 on check_read_or_die
This prevents hogging the CPU in a tight loop,
while waiting for access.

I've also reduced the number of tries to 30, rather
than 200. This is more conservative, while still
being somewhat permissive.

The addition of the usleep delay probably makes
this more reliable than the previous behaviour of
quickly spinning through 200 tries, but without
hogging CPU resources.

I *could* allow this loop to be infinite, but
I regard infinite spin-lock as an error state.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-04 00:57:48 +00:00
Leah Rowe
3d27c77a63 util/nvmutil: reset errno on successful file reads
a non-fatal error could have set errno. when we return
from check_read_or_die(), it should be assumed that
all is well.

i don't think this would mask anything important, but
it may be regarded as a preventative bug fix, since
it most likely only prevents false-positives.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-04 00:52:40 +00:00
Leah Rowe
6e08614e69 util/nvmutil: reset fd/rfd to negative one
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-04 00:51:24 +00:00
Leah Rowe
8143f95b41 util/nvmutil: reset buf in reset_global_state()
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-04 00:47:45 +00:00
Leah Rowe
3e7148a9b7 util/nvmutil: reset macbuf in reset_global_state
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-04 00:46:42 +00:00
Leah Rowe
53a680c063 util/nvmutil: reset errno in reset_global_state
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-04 00:44:47 +00:00
Leah Rowe
46a0fe6b8a util/nvmutil: Comments relating to NVM size limit
Part of the code currently assumes we only work on
the smaller NVM area.

I'm adding some comments to make this clear, for
when and if the code is ever expanded to support
operating on the Extended NVM area (just part the
main 128-byte NVM area).

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-04 00:41:08 +00:00
Leah Rowe
4b42419122 util/nvmutil: remove err_if()
use of it was preventing more verbose error messages
on exit.

the code is actually cleaner without it, and easier
to read, because of those verbose error messages.

i also added some comments to cmd_swap/copy and did
some other minor/related cleanup elsewhere.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-04 00:30:28 +00:00
Leah Rowe
6efd0429e2 util/nvmutil: improved some error messages
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-04 00:07:14 +00:00
Leah Rowe
6035a1bb6a util/nvmutil: rename valid_read
it doesn't just validate, but also exits.

rename it accordingly, to: check_read_or_die

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-04 00:01:28 +00:00
Leah Rowe
be28140741 util/nvmutil: remove redundant casts
these just bloat the code

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 23:55:46 +00:00
Leah Rowe
3b8de31f37 util/nvmutil: more thorough global_state_reset
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 23:46:01 +00:00
Leah Rowe
704a7beeb4 util/nvmutil: reset fname properly
do it after resetting global state.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 23:42:15 +00:00
Leah Rowe
489f632cab util/nvmutil: simpler mac address totalling
i know it's always going to be 3

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 23:40:28 +00:00
Leah Rowe
76d6900d69 util/nvmutil: less obscure mac address zero check
make it totally clear what's going on.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 23:37:59 +00:00
Leah Rowe
8c5c4e1b93 util/nvmutil: reset global state in one function
split it out of main. this is good hygiene and it's preparation
for a planned expansion in the future, that allows operation
on multiple files.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 23:29:45 +00:00
Leah Rowe
213626d898 util/nvmutil: reset cmd in main
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 23:24:46 +00:00
Leah Rowe
306f9087d9 util/nvmutil: reset fname in main
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 23:23:54 +00:00
Leah Rowe
d0141ca2e4 util/nvmutil: reset part_modified in main
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 23:23:28 +00:00
Leah Rowe
1e407a2a65 util/nvmutil: reset invert in main
currently redundant, but again i might expand this
in the future to allow multiple runs. putting this
here as good practise (currently redundant).

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 23:22:14 +00:00
Leah Rowe
dd109ccf07 util/nvmutil: reset mac in main
we currently only run the logic once, but i might
expand nvmutil in the future, so that it can
operate on multiple files. this would require
using a different command syntax, e.g. getop-style
syntax.

this is a preventative bug fix, resetting global
state.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 23:19:33 +00:00
Leah Rowe
790009f58e util/nvmutil: reset macbuf in parse_mac_string
we only ever use it once, so it's fine, but future
expansion of this code might trip us up.

this is therefore a preventative bug fix.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 23:17:02 +00:00
Leah Rowe
7b15b020b5 util/nvmutil: use off_t for partsize (pread/pwrite)
size_t can truncate on some platforms. it's best to use
the proper variable type (a cast is insufficient).

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 23:12:15 +00:00
Leah Rowe
ae080c35e4 util/nvmutil: clearer error messages on valid_read
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 23:09:54 +00:00
Leah Rowe
75bcc46de4 util/nvmutil: comment valid_read for clarity
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 23:00:17 +00:00
Leah Rowe
7a62ad3f62 util/nvmutil: clearer errno reset in valid_read
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 22:57:45 +00:00
Leah Rowe
3268c225d6 util/nvmutil: pass size_t to read, not ssize_t
ssize_t is signed, which could be negative.

in practise, we control the value that's going in anyway.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 22:57:18 +00:00
Leah Rowe
3077f51c67 util/nvmutil: use zd for printf in valid_read
more portable

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 22:51:50 +00:00
Leah Rowe
cc51ac32d0 util/nvmutil: minor cleanup in usage()
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 22:32:24 +00:00
Leah Rowe
ae6db44543 util/nvmutil: minor cleanup in cmd_setmac
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 22:29:21 +00:00
Leah Rowe
8f81ed314a util/nvmutil: don't use err_if in usage
fname isn't set here, and fname is used in err_if

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 22:27:51 +00:00
Leah Rowe
f93a40ecb6 util/nvmutil: rename errval to nvm_errval
strtonum implementations in bsd sometimes have this
variable name. rename it to avoid conflict.

also removed the commentt errno values, since i'm
only ever setting it to valid values, as are the
syscalls that i'm using, so it should be ok.

i'm not writing a stub to check errno. that would
be far beyond the scope of nvmutil.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 21:51:13 +00:00
Leah Rowe
21b8de87ff util/nvmutil: re-try reads if errno is EINTR
this is a bit of fault tolerance. a bit bloated too,
but it should make the code more resilient.

we limited the number of retries to 200 retries.

EINTR is when the syscall (read/pread) is interrupted.

we still error out on other conditions; we also still
error out on EINTR if the number of re-tries surpasses
200.

during this re-try loop, if *another* error occurs, we
exit as normal. this is done for both files: the gbe
file, and /dev/urandom.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 21:43:32 +00:00
Leah Rowe
493e3cf069 util/nvmutil: explicitly cast on read/pread/pwrite
these functions return ssize_t, so compare explicitly
to that, when using the SIZE_4KB define for example.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 20:38:36 +00:00
Leah Rowe
af5d876bf0 util/nvmutil: remove unnecessary check
we can just use errval as argument to set_err,
because set_err itself now properly handles
errno, ensuring that is is never set to zero.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 20:30:52 +00:00
Leah Rowe
418015c232 util/nvmutil: stricter check_bound
word/set_word are only meant to operate on the nvm
area (128 bytes), but the current check is against
the entire 4KB block.

swap() only handles the nvm area, as per the design
of nvmutil.

this patch makes the boundary check truer to my real
intent, guarding against future logical errors.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 20:24:26 +00:00
Leah Rowe
80a5b08090 util/nvmutil: set errno only if x is not 0
otherwise, some minor edge cases may result in err
printing "success", when we want to signal an error.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 20:15:39 +00:00
Leah Rowe
3f7d89c401 util/nvmutil: consistent errors on close()
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 19:59:35 +00:00
Leah Rowe
fd515e4c28 util/nvmutil: print correct file name for urandom
err_if reports fname, not /dev/urandom

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 19:53:09 +00:00
Leah Rowe
f4e477b549 util/nvmutil: don't bother with strncmp
these are strings provided from the shell, which are
guaranteed to always be null-terminated.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 19:51:25 +00:00
Leah Rowe
687e64a359 util/nvmutil: get rid of arg_cmd
it's pointless.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 19:48:48 +00:00
Leah Rowe
776eee721d util/nvmutil: use memcpy in word/set_word
alignment isn't an issue, but aliasing between uintX_t
types in C means that this code may fail on some weird
systems.

using memcpy here is advisable.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 19:41:21 +00:00
Leah Rowe
d88991f6bc util/nvmutil: use strncmp instead of strcmp
set a constant maximum length. this seems reasonable,
and it's even quite generous.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 19:18:47 +00:00
Leah Rowe
bf081914a3 util/nvmutil: remove checkdir()
we don't need it. what follows is a call to open(), which
would fail anyway if the path is a directory; further, this
removes a theoretical race condition in the program, and
makes open() happen sooner, making it more likely that we
get the file first, before another program can take it.

checking whether /dev/urandom is a directory is the height
of absurdity.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 19:11:20 +00:00
Leah Rowe
4325214d82 util/nvmutil: make rhex err_if consistent
n + 1 is the same as saying sizeof(rnum) in this case.
we should be clear about that, in code. n is irrelevant
here, since it is only an index for the return value.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 19:03:58 +00:00
Leah Rowe
668dec36e9 util/nvmutil rhex: check against sizeof(rnum)
checking against -1 is incorrect, because we specifically want
to ensure that it always read the number of bytes defined by
the size of rnum.

this still covers the case where the return value is -1, and
therefore makes the error handling much stricter.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 19:00:14 +00:00
Leah Rowe
4775bb2348 util/nvmutil err: filter errval when zero
default to ECANCELED if zero

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 18:54:58 +00:00
Leah Rowe
634f4a685d util/nvmutil: always exit non-zero on err
the way err works here now is very different than
the bsd one. here, we ALWAYS exit with EXIT_FAILURE,
and we call set_err with, as argument, the first
argument given to err.

this then sets errno, but the exit value is always
consistent.

that's what happens when i control err(). i make it
even better. the original bsd one is too conservative.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 18:47:14 +00:00
Leah Rowe
3af94efc16 util/nvmutil: more consistent output in usage()
and use getnvmprogname

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 18:03:27 +00:00
Leah Rowe
2faea7d890 util/nvmutil: use getnvmprogname in usage
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 18:00:19 +00:00