Commit Graph

507 Commits

Author SHA1 Message Date
Leah Rowe c6c4e5303e util/nvmutil: fix duplicated word in comment
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-06 19:30:55 +00:00
Leah Rowe 5d05921875 util/nvmutil: make part_modified an unsigned char
better reflects intent

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-06 19:28:32 +00:00
Leah Rowe 1aa8f33d50 util/nvmutil: fix bad string in gbe_read_part()
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-06 19:25:54 +00:00
Leah Rowe 9095722079 util/nvmutil: calculate off AFTER part validation
because we check the part first, so we need to know it's
valid before proceeding.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-06 19:23:56 +00:00
Leah Rowe 1131240d02 util/nvmutil: made a comment a bit clearer
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-06 19:21:02 +00:00
Leah Rowe 72caa5f892 util/nvmutil: fix another printf specifier
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-06 19:17:01 +00:00
Leah Rowe efdf110317 util/nvmutil: remove unnecessary casts
now that part numbers are size_t, i don't need them.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-06 19:08:55 +00:00
Leah Rowe d94fbba779 util/nvmutil: fix more bad printf statements
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-06 19:04:38 +00:00
Leah Rowe 6d2a1afbd4 util/nvmutil: fix a few bad printf statements
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-06 19:03:06 +00:00
Leah Rowe 0698c6ada4 util/nvmutil: warn about partial gbe file writes
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-06 18:55:31 +00:00
Leah Rowe 397fc78e58 util/nvmutil: use size_t for part numbers
not int

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-06 18:45:26 +00:00
Leah Rowe 82fdee91fa util/nvmutil: clean up file handling
inline check_read_or_die

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-06 18:36:29 +00:00
Leah Rowe 58b17c98fd util/nvmutil: minor cleanup
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-06 18:25:24 +00:00
Leah Rowe c8bd98c8a6 util/nvmutil: mem bound check on file read/write
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-06 18:17:07 +00:00
Leah Rowe 41f7f6352d util/nvmutil: rename gbe_bound for clarity
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-06 17:47:49 +00:00
Leah Rowe b1abef8881 util/nvmutil: make defines easier to understand
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-06 17:44:07 +00:00
Leah Rowe edca6c2cd3 util/nvmutil: tidy up set_mac_nib
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-06 17:35:00 +00:00
Leah Rowe 123e77d07f util/nvmutil: use portable printf on hexdump
lx means unsigned long, and row is size_t which often
is, but this is not guaranteed.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-06 17:31:04 +00:00
Leah Rowe c0a77a7301 util/nvmutil: make the MAC shifting easier to read
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-06 17:22:31 +00:00
Leah Rowe 596643a0d5 util/nvmutil: optimise hextonum
there is 0x20 of different between a and A

so we can just or 0x20 and compare only lowercase.

we can also cast char (which may me signed on some
systems) to unsigned, and then only check whether
it's lower than 10.

this code results in far less branching (in C),
but a good optimising compiler probably wouldn't
have cared about the old version anyway.

it's just nicer C code.

this also means we no longer need to check for
X, only x.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-06 17:07:12 +00:00
Leah Rowe 6023d17b6e util/nvmutil: fix parenthesis warning in GCC
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-06 17:01:48 +00:00
Leah Rowe 199dbad96d util/nvmutil: tidy up set_mac_nib
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-06 16:55:23 +00:00
Leah Rowe 9e32456c8c util/nvmutil: clearer intent on struct data
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-06 16:52:02 +00:00
Leah Rowe 7ae2288c10 util/nvmutil: more readable NVM defines
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-06 16:49:41 +00:00
Leah Rowe 2f0c189da9 util/nvmutil: more readable SIZE_nKB defines
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-06 16:46:01 +00:00
Leah Rowe 7f6d7526cc util/nvmutil: use arc4random on DragonFly BSD
it has arc4random, so we will use it there.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-06 16:43:18 +00:00
Leah Rowe a358a6fe09 util/nvmutil: use strlen instead of strnlen
strnlen isn't available on some older unices.

we already know the string will be null-terminated,
because it comes from argv, so runaway reads are
extremely unlikely (read: impossible).

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-06 16:41:59 +00:00
Leah Rowe a82c766b8c util/nvmutil: add bound checks for gbe read/write
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-06 16:29:45 +00:00
Leah Rowe 94f5e70366 util/nvmutil: minor code cleanup
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-06 16:05:21 +00:00
Leah Rowe 0d4cc9e324 util/nvmutil: clearer macbuff init in set_mac_nib
and 1 does the same thing as mod 2, but it's cleaner.

i also now bitshift 3 times instead of times by 8,
which again is clearer in purpose.

i line breaked after h, to make it clear that all of
the next part is being shifted in

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-06 15:51:46 +00:00
Leah Rowe 7950a31c79 util/nvmutil: don't reset rfd on openbsd
it isn't defined there

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-06 15:02:46 +00:00
Leah Rowe 99543bc632 util/nvmutil: tidy up rhex()
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-06 14:59:35 +00:00
Leah Rowe a4b9a333a9 util/nvmutil: properly handle sizeof in rhex()
sizeof is size_t, so we must act accordingly.

casting it to an int is unacceptable.

this version is also branchless.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-06 14:53:36 +00:00
Leah Rowe cc47a756f2 util/nvmutil: make rmac an array, not a literal.
this prevents reassignment.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-06 14:42:57 +00:00
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