Commit Graph

403 Commits

Author SHA1 Message Date
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
Leah Rowe
8812a17683 util/nvmutil: use the invert in goodChecksum
this way, the correct part number is printed when an invalid
part is being operated on, in cmd copy or swap.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 13:38:39 +00:00
Leah Rowe
3bdefad097 util/nvmutil: readGbe: use inverted part in swap
i overlooked this in a previous modification

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 13:23:20 +00:00
Leah Rowe
50942a7a15 util/nvmutil: clean up write_mac_part
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 13:19:22 +00:00
Leah Rowe
d4231e27fa util/nvmutil: clean up cmd_dump
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 13:17:21 +00:00
Leah Rowe
9044b55c48 util/nvmutil: don't copy data in copy/swap
instead, use a single integer, set to 1 if using
these commands (otherwise set to 0) used as an XOR
mask.

use this to invert where data gets read. one quirk
with this is that if a copy operation is performed
from a part with a bad checksum, it's already done
in advance, in memory, but then the check on the
checksum in cmd_copy is now checking the other part,
which will be all zeroes, so i invert that too; this
means now when running cmd_copy, it'll complain about
an invalid part, but the part number is inverted.

it's a small price to pay, because this restores the
previous performance optimisations but without being
as unsafe.

this is also true when doing the swap.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 13:07:39 +00:00
Leah Rowe
98bc523274 util/nvmutil: re-introduce do_read in readGbe
lots of block devices use 4KB block size. it makes
sense to have this optimisation here.

i previously removed it, along with the one that
only reads the NVM area - that one is still gone,
because it was largely pointless.

because of this modification returning, i also
re-introduced the check in setWord against
nvmPartModified - otherwise, for example, running
cmd brick 0 would brick part 0 but then write
all zeroes to part 1.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 12:40:50 +00:00
Leah Rowe
6770fa8ef2 util/nvmutil: use read, not pread, on /dev/urandom
we always read from offset zero, so use read

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 12:31:53 +00:00
Leah Rowe
188a3d012b util/nvmutil: clean up checkdir
those lines at the end are a hangover from the old opendir-
based implementation.

i also made the output more verbose in that first error
check.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 04:17:25 +00:00
Leah Rowe
c64324467f util/nvmutil: use separate st variable for urandom
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 04:13:21 +00:00