Commit Graph

3193 Commits

Author SHA1 Message Date
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
Leah Rowe d267cd36cc util/nvmutil: remove redundant comment
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 16:03:23 +00:00
Leah Rowe 7fefeba076 util/nvmutil: use const char in usage
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 15:58:46 +00:00
Leah Rowe f1ee2ff630 util/nvmutil: tidy up set_cmd
reduced indentation

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 15:56:06 +00:00
Leah Rowe 6d8cf8909f util/nvmutil: split up set_cmd
the second half of the function is mostly checking
arguments, and has the hack to set cmd to cmd_setmac
if cmd is unset (no command specified) but argc is
above two.

stylistically, this is more consistent.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 15:50:20 +00:00
Leah Rowe 9f12aa3b91 util/nvmutil: tidy up final cmd check in set_cmd
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 15:47:01 +00:00
Leah Rowe 572b7758cf util/nvmutil: tidy up rhex
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 15:43:11 +00:00
Leah Rowe 403fd260b5 util/nvmutil: reduce the size of rnum to 12
we only need 12 bytes at a maximum

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 15:41:21 +00:00
Leah Rowe d9d628e146 util/nvmutil: tidy up check_mac_separator
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 15:40:33 +00:00
Leah Rowe 036ac6dc39 util/nvmutil: rename openFiles to open_files
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 15:31:31 +00:00
Leah Rowe 48638e9a22 util/nvm: make variables and functions static
i don't intend for this to be used in a library

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 15:20:20 +00:00
Leah Rowe dbfd77b388 util/nvmutil: don't use spaces on err_if
this is inconsistent with the style in the rest of
the code.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 15:08:57 +00:00
Leah Rowe bfb77077b3 util/nvmutil: use const for string literal in op
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 15:06:39 +00:00
Leah Rowe a8a9ce32f1 util/nvmutil: no designated initialisers in op
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 15:04:39 +00:00
Leah Rowe 5517cf923b util/nvmutil: declare one prototype per line
in the same order that functions are declared

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 15:02:15 +00:00
Leah Rowe e4e5022ab3 util/nvmutil: don't declare variables in for loops
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 14:50:33 +00:00
Leah Rowe 198843c2e3 util/nvmutil: use size_t on op for loop
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 14:43:16 +00:00
Leah Rowe 4edcc26849 util/nvmutil: make global variable names clearer
each variable is declared on its own line.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 14:37:10 +00:00
Leah Rowe 91879b7606 util/nvmutil: count the number of items in op
don't hardcode it

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 14:33:46 +00:00
Leah Rowe d37726c353 util/nvmutil: set fname earlier
this fixes the bug where if you specify an invalid command
such as:

./nvm gbe brick 9

part 9 doesn't exist, but fname isn't yet set, here.

same thing applys when running those pledge commands on
openbsd.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 14:24:19 +00:00
Leah Rowe 65a2f352ee util/nvmutil: obey the 79-character rule
only 79 characters or less, per line.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 14:15:36 +00:00
Leah Rowe b8091c7bdc util/nvmutil: use spaces when calling err_if
this is separate from other function calls. err_if
is used as though it was an if, where we always add
a space. it's just a quirk of my coding style.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 14:14:06 +00:00
Leah Rowe 23cdfdd00a util/nvmutil: consistent variable/function naming
use the same naming scheme throughout

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 14:12:52 +00:00