Commit Graph

592 Commits

Author SHA1 Message Date
Leah Rowe 9be83fa034 util/nvmutil: use xstrxlen for mac length
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-08 14:16:32 +00:00
Leah Rowe 4a9aea629b util/nvmutil: use own strnlen function: xstrxlen
strnlen is not available on some older systems,
so now we provide our own portable version.

this version also aborts on NULL input, unlike
the standard function.

this version also does not permit empty strings.

this version also does not permit unterminated
strings.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-08 14:15:12 +00:00
Leah Rowe 92bd44676a util/nvmutil: err if arc4random disused on bsd
arc4random is superior, so using /dev/urandom
would be a mistake. we only use that on linux,
or old/weird unix.

we would also use it on linux, but GNU prohibits
nice things (its implementations are spotty, and
old glibc doesn't have it - before 2022 there is
libbsd, but i'm not importing that).

not that it matters. we're not doing encryption.
i'm just a stickler for technical correctness.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-08 13:32:14 +00:00
Leah Rowe e5d0dee668 util/nvmutil: unified cmd_index reset
use the macro, introduced in the previous commit

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-08 13:32:14 +00:00
Leah Rowe 1ff1dca661 util/nvmutil: unified cmd validity check
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-08 13:32:14 +00:00
Leah Rowe 5517f81cbe util/nvmutil: commented some defines
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-08 13:32:13 +00:00
Leah Rowe be7c965845 util/nvmutil: rename cmd to cmd_index
to make it clearer what this variable does

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-08 13:32:13 +00:00
Leah Rowe a7f97f385c util/nvmutil: sanitize the command list
this is a guard against mistakes by future maintainers

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-08 13:32:11 +00:00
Leah Rowe 8ce1cbe7f4 util/nvmutil: never allow cmd to be negative
make cmd a size_t and make the equivalent to NULL
be the number of items in command[]

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-08 13:32:09 +00:00
Leah Rowe 0160b26aee util/nvmutil: tidy up set_cmd()
don't set args here

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-08 13:32:06 +00:00
Leah Rowe de1963fdee util/nvmutil: do cmd bound check
instead of simply checking null, just do a bound check.

this would also cover NULL (-1)

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-08 13:31:59 +00:00
Leah Rowe ccc58cefbb util/nvmutil: rename check_cmd_args
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-08 03:26:43 +00:00
Leah Rowe ec3ab4059c util/nvmutil: close random_fd only if used
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-08 03:22:50 +00:00
Leah Rowe 63fcc0891f util/nvmutil: Do not allow /dev/urandom on OBSD
There, we use arc4random_buf which does not directly
access /dev/urandom on BSD; it uses a userspace method
instead, which bypasses this.

This is therefore much more restrictive, which is
exactly the point of unveil(2) and pledge(2); restrict
your program's operation while ensuring that it has what
it needs, to help with debugging and prevent common bugs.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-08 02:47:08 +00:00
Leah Rowe b1866312bd util/nvmutil: rename print_mac_address
the current name is misleading. this function
specifically converts what's stored in memory,
in the nvm.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-08 02:41:46 +00:00
Leah Rowe 1bbc6ac890 util/nvmutil: only open /dev/urandom on setmac
otherwise, it's a pointless computation

i also added a guard to mitigate this, in the
read file function. this should have been there
anyway.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-08 02:39:53 +00:00
Leah Rowe 9bd7d04b49 util/nvmutil: say what randomiser is used
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-08 02:34:26 +00:00
Leah Rowe c1bfe6a438 util/nvmutil: rename command.args to argc
that way, it makes more sense sementantically

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-08 02:20:13 +00:00
Leah Rowe bf30cdd021 util/nvmutil: fix mistake in command
accidentally specified invert, for the brick
and setchecksum commands. oops!

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-08 02:18:34 +00:00
Leah Rowe 4cdf22cf8f util/nvmutil: minor cleanup
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-08 02:16:30 +00:00
Leah Rowe 7c3a4e253f util/nvmutil: remove stale comment
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-08 02:08:40 +00:00
Leah Rowe 26a69321ac util/nvmutil: use enum for command index
if the enum is messed up, this patch also prevents
that. this is not to catch a runtime error, but
to intentionally trip up a maintainer that screws
up, prompting them to fix their future mistake.

we previously used a pointer directly, without
even checking index/NULL - that too is now covered,
except that we now use an indice for command[] and
execute the command from that, rather than directly
declaring a pointer.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-08 02:04:10 +00:00
Leah Rowe 1d17a8ffcf util/nvmutil: call it nvmutil in makefile
a package manager by the name "nvm" exists, as
i discovered.

this is a courtesy to them.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-08 01:11:52 +00:00
Leah Rowe 6203f3ae61 util/nvmutil: print mac before setting
this way, if a user does e.g.

./nvm gbe.bin bullshit

It will say: bullshit

Right now, it just says invalid length. This
means if the user wanted to type e.g.

./nvm gbe.bin copy 0

but they typed:

./nvm gbe.bin coyp 0

Now it will tell them that it's trying
to set the MAC address "coyp".

This is because if an invalid command is given,
it's treated as a MAC address instead. This is
by design, to allow e.g.

./nvm gbe.bin xx:1x:1x:xx:xx:xx

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-08 01:11:52 +00:00
Leah Rowe 698d39dd06 util/nvmutil: store invert in command struct
handle inversion directly there

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-08 00:21:58 +00:00
Leah Rowe 50e20fb8bf util/nvmutil: make cmd an integer
point directly to the command table.

run through an intermediary function to check
bounds, for safety.

this will allow me to then set things like
the invert config directly in that struct.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-08 00:16:45 +00:00
Leah Rowe 3148d31798 util/clarity: name the gbe file half size
we need only declare it in the centralised gbe_file_offset
function, which determines whether a write to the gbe file
falls specifically within the 4KB range that is the gbe
part.

it is always half of the gbe file size, and then the first
4KB of each half stores the gbe part.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-07 13:36:46 +00:00
Leah Rowe 96cb52f21a util/nvmutil: only have newrandom on linux/oldunix
these variables newrandom and oldrandom are unused on
BSD systems, and their unused status may trigger
warnings on some compilers.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-07 13:18:54 +00:00
Leah Rowe c195e8cc2b util/nvmutil: tidy up open_dev_urandom
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-07 10:07:05 +00:00
Leah Rowe 6a505c9162 util/nvmutil: correct type on hextonum()
use uint16_t instead

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-07 03:28:15 +00:00
Leah Rowe 50ce806dfa util/nvmutil: fix a comment
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-07 03:16:20 +00:00
Leah Rowe a0da88d361 util/nvmutil: fix a comment
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-07 03:08:31 +00:00
Leah Rowe ea1046dc46 util/nvmutil: unambiguous sign conv_argv_part_num
yeah, do the verification manually, don't convert
to size_t. this avoids a bunch of theoretical
bugs that i can't be bothered to explain at 3AM

just trust me bro

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-07 03:00:35 +00:00
Leah Rowe b98f89c272 util/nvmutil: clean up hextonum()
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-07 02:50:30 +00:00
Leah Rowe b8bd1ca65a util/nvmutil: tidy up some comments
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-07 02:44:11 +00:00
Leah Rowe 49eca198da util/nvmutil: make mac_updated a uint8_t
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-07 02:38:36 +00:00
Leah Rowe ef2937edbd util/nvmutil: fix prototype indentation
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-07 02:37:46 +00:00
Leah Rowe 7bfe134a19 util/nvmutil: reset mac_buf for mac parsing
preventative fix for later, if the tool is ever expanded
to have a better command syntax, for supporting more than
one file at a time.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-07 02:35:03 +00:00
Leah Rowe 14ad5a9818 util/nvmutil: comment the unhandled errno exit
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-07 02:09:56 +00:00
Leah Rowe 90bd395cb1 util/nvmutil: tidy up set_mac_byte
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-07 02:05:57 +00:00
Leah Rowe 4619dad0b7 util/nvmutil: further mac address parsing cleanup
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-07 02:00:14 +00:00
Leah Rowe 921144856c util/nvmutil: tidy up mac parsing code
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-07 01:39:57 +00:00
Leah Rowe 86357dbe52 util/nvmutil: fix a bad comment
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-07 01:28:50 +00:00
Leah Rowe 7e88f53e99 util/nvmutil: tidy up set_mac_byte
send the mac address byte directly to check_mac_separator

functionally identicaly, but cleaner, and uses
multiplication instead of division (faster).

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-07 01:21:29 +00:00
Leah Rowe c02f3166bb util/nvmutil: make do_read a uint8_t
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-07 01:11:07 +00:00
Leah Rowe a9f5cbb542 util/nvmutil: clearer commenting about commands
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-07 01:09:52 +00:00
Leah Rowe 0084452c4a util/nvmutil: comment regarding buf
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-07 01:01:44 +00:00
Leah Rowe 364abddeab util/nvmutil: minor code cleanup
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-07 00:52:57 +00:00
Leah Rowe 68d689336b util/nvmutil: fix an err string
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-07 00:10:36 +00:00
Leah Rowe 658ed55bc1 util/nvmutil: tidy up conv_argv_part_num
no need for strlen here (1-character string).

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