Commit Graph

3254 Commits

Author SHA1 Message Date
Leah Rowe
e7cbd9441c util/nvmutil: use arc4random when available
fall back to urandom.

also add a /dev/random fallback, for older unices.

with the posix compatibility changes, combined with
this change as above, the code should be portable
now. i expect it to compile on *many* unix systems!

pretty much everything from the last 30 years.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-06 14:42:29 +00:00
Leah Rowe
95b294db05 util/nvmutil: inline pos calculation on word()
we don't need a whole function. i previously did it
for clarity, but simply setting a variable all in
one line is totally fine.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-06 13:49:33 +00:00
Leah Rowe
d89d14e911 util/nvmutil: remove pointless check
the input is already size_t, which is unsigned

there's no point in checking for negative

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-06 13:37:38 +00:00
Leah Rowe
f96a119523 util/nvmutil: cast inside check_bound, not callers
the purpose of the cast is to check whether a given
integer would underflow under any circumstance.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-06 13:33:42 +00:00
Leah Rowe
a31236b1f8 util/nvmutil: comment regarding endianness
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-06 13:30:14 +00:00
Leah Rowe
c7409cce03 util/nvmutil: clean up set_word
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-06 13:28:51 +00:00
Leah Rowe
feee6a728f util/nvmutil: use size_t for offsets in words
size_t is generally the size of the address space, so
this is more reliable for our purposes; we're only
working on small buffers, but even so, it's a good
thing to do.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-06 13:28:04 +00:00
Leah Rowe
32429f2c37 util/nvmutil: remove the swap() function
directly handle swapping in word and set_word

in my testing, x86_64 and arm64 compilers actually produce
more efficient code this way. i previously only did a big
swap on the whole buffer on big-endian CPUs, and directly
accessed without swaps on little-endian, as an optimisation.

however, the old code is actually slower than what the
compiler produces, with the new code!

portability is retained with big-endian host CPUs and
little-endian host CPUs.

this also avoids the complication of memcpy and is just
generally extremely reliable by comparison.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-06 13:02:24 +00:00
Leah Rowe
b3516e8c16 update nvmutil COPYING file (2026 copyright year)
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-04 11:25:07 +00:00
Leah Rowe
2f2295fc37 util/nvmutil: rhex: don't read twice!
we currently never read the 0th byte, so if we need
all 12, and we do when every byte is random, we
read again just to get one byte.

not really a bug, but it is a performance penalty,
so let's fix it!

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-04 10:45:00 +00:00
Leah Rowe
aceafd684a util/nvmutil: unified file read error handling
it must be read perfectly, or else

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-04 02:34:44 +00:00
Leah Rowe
947211fc3c util/nvmutil: tidy up cmd_brick
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-04 02:06:33 +00:00
Leah Rowe
2f782b8a01 util/nvmutil: minor code cleanup
mostly style changes

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-04 01:59:21 +00:00
Leah Rowe
f727675f6d util/nvmutil: provide comment about global state
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-04 01:48:41 +00:00
Leah Rowe
d6601059fb util/nvmutil: reset errno on *successful* read
otherwise, stale errno from an earlier syscall might
cause a valid read to still fail.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-04 01:43:39 +00:00
Leah Rowe
84b4ba48b4 util/nvmutil: don't reset errno in check_read_or_die
we want to debug it after the fact; this is now handled,
in the calling functions (unhandled error exceptions).

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-04 01:39:34 +00:00
Leah Rowe
94415ca73d util/nvmutil: minor code cleanup
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-04 01:39:11 +00:00
Leah Rowe
75ea8ab175 util/nvmutil: comment regarding memcpy endianness
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-04 01:32:34 +00:00
Leah Rowe
ca98d418d5 util/nvmutil: don't usleep on file reads
i don't care. it's only 30 tries.

usleep can fail, setting errno, and it can actually
take longer, depending on the environment. it poisons
errno, and makes debugging harder.

just remove it.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-04 01:29:30 +00:00
Leah Rowe
e28409d80b util/nvmutil: handle error after check_read_or_die
we already exit reliably in that function. the current code
is logically correct, but very weak against future changes.

this extra check is essentially redundant, but prevantative
against future changes.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-04 01:27:13 +00:00
Leah Rowe
7d6f1a6b30 util/nvmutil: handle errno after file read
errno shouldn't be set, after reading a file successfully.

if it is, that's a bug. handle it accordingly.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-04 01:23:19 +00:00
Leah Rowe
12778a0ffd util/nvmutil: simplified zero-mac-address check
it's just three words. access them directly.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-04 01:12:11 +00:00
Leah Rowe
84376fa308 util/nvmutil: don't pass h as param in cmd_setmac
it's only needed in one function (tmp variable).

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-04 01:09:56 +00:00
Leah Rowe
0d4822e700 util/nvmutil: cast SIZE_4KB on the pwrite check
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-04 01:02:42 +00:00
Leah Rowe
0d3a8749fe util/nvmutil: usleep 1 on check_read_or_die
This prevents hogging the CPU in a tight loop,
while waiting for access.

I've also reduced the number of tries to 30, rather
than 200. This is more conservative, while still
being somewhat permissive.

The addition of the usleep delay probably makes
this more reliable than the previous behaviour of
quickly spinning through 200 tries, but without
hogging CPU resources.

I *could* allow this loop to be infinite, but
I regard infinite spin-lock as an error state.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-04 00:57:48 +00:00
Leah Rowe
3d27c77a63 util/nvmutil: reset errno on successful file reads
a non-fatal error could have set errno. when we return
from check_read_or_die(), it should be assumed that
all is well.

i don't think this would mask anything important, but
it may be regarded as a preventative bug fix, since
it most likely only prevents false-positives.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-04 00:52:40 +00:00
Leah Rowe
6e08614e69 util/nvmutil: reset fd/rfd to negative one
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-04 00:51:24 +00:00
Leah Rowe
8143f95b41 util/nvmutil: reset buf in reset_global_state()
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-04 00:47:45 +00:00
Leah Rowe
3e7148a9b7 util/nvmutil: reset macbuf in reset_global_state
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-04 00:46:42 +00:00
Leah Rowe
53a680c063 util/nvmutil: reset errno in reset_global_state
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-04 00:44:47 +00:00
Leah Rowe
46a0fe6b8a util/nvmutil: Comments relating to NVM size limit
Part of the code currently assumes we only work on
the smaller NVM area.

I'm adding some comments to make this clear, for
when and if the code is ever expanded to support
operating on the Extended NVM area (just part the
main 128-byte NVM area).

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-04 00:41:08 +00:00
Leah Rowe
4b42419122 util/nvmutil: remove err_if()
use of it was preventing more verbose error messages
on exit.

the code is actually cleaner without it, and easier
to read, because of those verbose error messages.

i also added some comments to cmd_swap/copy and did
some other minor/related cleanup elsewhere.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-04 00:30:28 +00:00
Leah Rowe
6efd0429e2 util/nvmutil: improved some error messages
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-04 00:07:14 +00:00
Leah Rowe
6035a1bb6a util/nvmutil: rename valid_read
it doesn't just validate, but also exits.

rename it accordingly, to: check_read_or_die

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-04 00:01:28 +00:00
Leah Rowe
be28140741 util/nvmutil: remove redundant casts
these just bloat the code

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 23:55:46 +00:00
Leah Rowe
3b8de31f37 util/nvmutil: more thorough global_state_reset
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 23:46:01 +00:00
Leah Rowe
704a7beeb4 util/nvmutil: reset fname properly
do it after resetting global state.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 23:42:15 +00:00
Leah Rowe
489f632cab util/nvmutil: simpler mac address totalling
i know it's always going to be 3

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 23:40:28 +00:00
Leah Rowe
76d6900d69 util/nvmutil: less obscure mac address zero check
make it totally clear what's going on.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 23:37:59 +00:00
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