Commit Graph

3215 Commits

Author SHA1 Message Date
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
Leah Rowe
9b18d11878 util/nvmutil: use own implementation of err
and getprogname, written as getnvmprogname

this removes a dependency on err.h, which is non-standard.

the remaining code is posix-compliant, or ifdef'd where
use of openbsd pledge is concerned - someone could theoretically
define __OpenBSD__ that isn't and OpenBSD base maintainer, and
then use nvmutil in it, but i so don't care about that evermore
hypothetical individual.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 17:57:27 +00:00
Leah Rowe
0a08045f92 util/nvmutil: close rfd on exit
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 17:30:55 +00:00
Leah Rowe
7c0eb780a9 util/nvmutil: remove unnecessary include
dirent.h is no longer needed, because i'm no longer
making use of opendir()

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 17:13:03 +00:00
Leah Rowe
8faa36eb64 util/nvmutil: tidy up write_gbe
by handling close() in main, we can reduce the
indendation in write_gbe and generally make it
much easier to read.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 17:09:33 +00:00
Leah Rowe
86665c9d22 util/nvmutil: make the part check easier to read
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 16:53:01 +00:00
Leah Rowe
531f0c9d38 util/nvmutil: fix style inconsistency
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 16:45:54 +00:00
Leah Rowe
b03a532c7d util/nvmutil: use braces on nested ifs
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 16:43:48 +00:00
Leah Rowe
5bae73c319 util/nvmutil: remove unnecessary global assignment
these integers are already initialised to zero.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 16:39:19 +00:00
Leah Rowe
faf28691b8 util/nvmutil: make swap() easier to understand
the swap function reverses the byte order in memory, of
a loaded GbE after after reading it, or before writing
it. this is required (as detected) on big-endian CPUs,
because GbE files store bytes in little-endian order.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 16:37:55 +00:00
Leah Rowe
624589fcb1 util/nvmutil: declare arg_cmd earlier
set it after the argc check

i don't like initialising a const after
declaration, but it compiles, and it keeps
with the style used in the rest of the code.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 16:29:40 +00:00
Leah Rowe
3983dc44f4 util/nvmutil: don't make op a typedef
it's not required

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 16:26:58 +00:00
Leah Rowe
49c2940421 util/nvmutil: fix arg_cmd initialisation
i declare it, using the 3rd argument, which might
not be available if only the file name is declared.

this fixes that.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 16:20:51 +00:00
Leah Rowe
61fa94d0d2 util/nvmutil: remove COMMAND, MAC_ADDRESS, PARTN
these macros serve no purpose except to obfuscate the
code. it's actually cleaner just to refer directly to
argv, and it reduces the chance of contamination later
upon re-factoring.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 16:15:33 +00:00
Leah Rowe
b3119c8b4d util/nvmutil: rename op_t to op
and op to ops

typedefs not part of any base system e.g. openbsd
base system, or e.g. the libc, should not have _t
in them.

this is a stylistic change, and does not alter any
actual program behaviour.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 16:06:02 +00:00
Leah Rowe
f03570b674 util/nvmutil: make op a const
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 16:04:43 +00:00