Commit Graph

3168 Commits

Author SHA1 Message Date
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
Leah Rowe 1c10eb524f util/nvmutil: cleaner directory checking
opendir allocates resources and causes a bunch of other
error conditions which we need to catch.

use of stat is more efficient here.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 04:11:29 +00:00
Leah Rowe b8e0513123 util/nvmutil: initialise fname to empty string
otherwise, early calls to err_if make use of a NULL string
inside err()

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 03:57:39 +00:00
Leah Rowe de5087bbd5 util/nvmutil: fix code formatting on xopen
it still had some leftovers from the old macro-style
implementation. it still compiled, but this patch
fixes the function properly.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 03:55:12 +00:00
Leah Rowe efe2635c12 nvmutil: fix the swap function
irrelevant for most users, who are on little endian
anyway, but i broke the swap function on big endian
systems. this fixes it.

the new function uses an intermediate variable instead
of xor swapping, but i accidentally left some relics of
of the old xor swaps in place. this fixes that.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 03:50:22 +00:00
Leah Rowe c721d6f4f4 util/nvmutil: use EXIT_FAILURE/SUCCESS for exits
this, in conjunction with the centralised exit scheme now
used by nvmutil, means that we have portable exit status.

notwithstanding the use of non-portable unix functions, and
especially the use of non-standard err.c (which GNU and BSD
libc implementations all have anyway, as does musl).

this code should now run on essentially any computer with
Linux or BSD on it.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 03:43:06 +00:00
Leah Rowe e4b8bb4875 util/nvmutil: make err_if a function, not a macro
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 03:38:45 +00:00
Leah Rowe c98d5c743f util/nvmutil: properly cast void use of set_err
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 03:35:37 +00:00
Leah Rowe 7213bba0c5 util/nvmutil: don't exit with errno as status
exit with 0 or 1, as is proper.

errno is an int, but the return value on a shell
can be e.g. byte, and depending how that number (errno)
is valued, could overflow and cause a zero exit, where
you want a non-zero exit.

the code has been changed, in such a way to maintain
current behaviour (don't change errno), except that when
errno is set upon exit, the exit value is now one.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 03:31:09 +00:00
Leah Rowe 490f311d05 util/nvmutil: split up cmd_setmac
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 02:50:03 +00:00
Leah Rowe 3cbaa7ead8 util/nvmutil: rename some functions for clarity
also re-order the prototypes

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 02:32:42 +00:00
Leah Rowe 707fabab38 util/nvmutil: split up parseMacString
split it into smaller, more readable functions

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 02:25:11 +00:00
Leah Rowe d9c307d5a3 util/nvmutil: remove useless gbe variable
use buf directly

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 01:50:21 +00:00
Leah Rowe a7cc8143a7 util/nvmutil: cmd copy/swap: use word/setWord
this means that we make use of the boundary checks. it's just
a safer way of handling these functions.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 01:18:23 +00:00
Leah Rowe 566ae72ca3 util/nvmutil: remove variable nvmPartChanged
pointless optimisation. we know that when a user requests an
operation that would write, it will probably result in a change.

therefore, this change is the real optimisation. to avoid
writing the same half of a file twice, when using cmd_copy,
we check (in writeGbe) whether gbe part 0 and 1 are the same;
if they are, then we only loop once. this is important, because
otherwise we would call swap() twice.

this means that the optimisations in cmd_copy and cmd_swap must
be removed. the point of this and other changes is to improve
memory safety in nvmutil, so frivolous use of pointers has to go.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 01:07:59 +00:00
Leah Rowe dfbb3c5d9e util/nvmutil: merge nvmalloc with readGbe
it's so simply now, all it does is set the gbe pointers

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 00:41:05 +00:00
Leah Rowe 2b01e023ab util/nvmutil: remove do_read
pointless code complication, that doesn't yield a noticeable
performance increase.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 00:40:07 +00:00