Commit Graph

685 Commits

Author SHA1 Message Date
Leah Rowe 84a9e8f89b util/nvmutil: reduce checksum report verbosity
only print a message what arg_part is set. this
means that a checksum error message won't be printed
on cat commands.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 01:38:22 +00:00
Leah Rowe 007dece09e util/nvmutil: unified io flags
don't hardcode it per command logically. do it in
the command table instead.

this also fixes a bug where the cat commands did
not set the permissions read-only.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 01:29:12 +00:00
Leah Rowe bb9010fdc1 util/nvmutil: require good checksum on cat
since the cat command can be used to create bad
gbe files, if the checksums don't match. my rule
is that nvmutil must never be used to destroy
data, only correct it (e.g. a file with just one
valid part can have it copied to the other part,
but you can't copy a bad part - and i removed
the "brick" command).

i *did* disable checksum requirements on the
dump command. with this, you can check the nvm
area and it tells you what the correct checksum
could be. then you could just correct it in a
hex editor if you wanted to, quite easily.

the idea is to slow down the act of destroying
or corrupting data as much as possible. someone
wily enough can use a hex editor to patch up some
files just fine.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 01:04:28 +00:00
Leah Rowe 08b1d95874 util/nvmutil: initialise w in gbe_cat_buf
no build error at the moment, nor would there be if
using clang or gcc, but i imagine some buggy compilers
might complain.

remember: portability. i also want this code to compile
on old, buggy compilers.

logically, this initialisation is redundant.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 00:53:53 +00:00
Leah Rowe 8f3bc13ac5 util/nvmutil: simplify the cat command
the current test is a bit over-engineered, so
i simplified it.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 00:46:18 +00:00
Leah Rowe aa67cf087d util/nvmutil: add cat16 and cat128 commands
these take any file size of gbe file: 8KB, 16KB
or 128KB. so does the normal cat.

then you can use cat, cat16 or cat128. these
output to stdout, the corresponding size in KB.

0xFF padding used on the larger files. on the
larger files, the first 4KB of each half is the
GbE parts, and everything else is 0xFF padding.

now you can resize gbe files easily, example:

./nvmutil gbe128.bin > gbe8.bin

yes

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-09 23:47:22 +00:00
Leah Rowe 66f1640552 util/nvmutil: nope. rename out back to cat.
it *is* cat. it's catting two GbE parts. so its cat.

(two 4KB areas, plus padding when i add cat16/cat128)

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-09 22:54:38 +00:00
Leah Rowe 724c9bb36c util/nvmutil: rename cat to out
it doesn't cat. it outputs one file.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-09 22:34:28 +00:00
Leah Rowe 3d6e2637d6 util/nvmutil: remove stale comment
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-09 22:25:39 +00:00
Leah Rowe 1e45d19f5d util/nvmutil: added a "cat" command
with this, you can read 16KB and 128KB files, and output
them to stdout, but it outputs 8KB

for example:

./nvmutil gbe128.bin > gbe8.bin

now you have a 8KB file

i could probably easily add cat16 and cat128 too.

nvmutil reads two 4KB parts regardless of GbE file
size (one from the first 4KB of each half of the
file), so this was easy to implement.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-09 22:18:51 +00:00
Leah Rowe 7d64b8ea8d util/nvmutil: allow dump without good checksums
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-09 21:49:07 +00:00
Leah Rowe 45ea92a077 util/nvmutil: fix bad cast conversion
don't cast unsigned to signed.

no behaviour is changed, but this will prevent some
silly compilers complaining about -Wsign-conversion

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-09 21:12:59 +00:00
Leah Rowe b5af1bf3ac util/nvmutil: add guard in rhex()
i removed this before, but it's good to put it
here defensively, in case i ever mess up
the urandom read function again.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-09 21:08:36 +00:00
Leah Rowe f8ddb6ef84 util/nvmutil: fix EINTR detection on urandom read
i forgot to handle it in the previous refactor

not really a problem in practise, since the first
read probably succeeds anyway.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-09 21:06:18 +00:00
Leah Rowe c59b3b7638 util/nvmutil: reorder some functions linearly
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-09 20:47:45 +00:00
Leah Rowe 0f9ce929b4 util/nvmutil: tidy up gbe/urandom reading
split them up into their own functions, since they
no longer operate according to the same policy.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-09 20:41:12 +00:00
Leah Rowe 1f4ab21403 util/nvmutil: fix a bad comment
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-09 18:11:34 +00:00
Leah Rowe 61968ec2b9 util/nvmutil: explicitly reset file descriptors
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-09 17:53:56 +00:00
Leah Rowe b69863e51f util/nvmutil: fix indentation in rhex()
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-09 17:46:46 +00:00
Leah Rowe cd63f1a7f3 util/nvmutil: remove unused variable in rhex
on bsd

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-09 17:45:50 +00:00
Leah Rowe 646e349893 util/nvmutil: fix bsd build issue
urandom_fd is unavailable on bsd

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-09 17:44:48 +00:00
Leah Rowe 85cc3071bb util/nvmutil: even safer rhex()
also handles possible overflows in read_gbe_file_exact

it removes dead code on both paths: arc4random and
urandom

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-09 17:41:29 +00:00
Leah Rowe b2a3edd170 util/nvmutil: only check n in rhex on linux
not bsd

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-09 17:33:23 +00:00
Leah Rowe 8f719f80b8 util/nvmutil: safer calculated_checksum
we rely on uint16_t wrapping, but some platforms may
behave weirdly.

cast as uint32_t and then cast back, on return, with
an explicit mask beforehand.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-09 17:08:54 +00:00
Leah Rowe 61015dbc6c util/nvmutil: much safer rhex()
n could be zero under weird regression cases

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-09 17:02:14 +00:00
Leah Rowe f34d020a20 util/nvmutil: tidy up hexdump()
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-09 16:54:42 +00:00
Leah Rowe d3b567edcf util/nvmutil: assert uint16_t as 16-bits
some platforms might get this wrong. define it
explicitly.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-09 16:52:42 +00:00
Leah Rowe afebfe7389 util/nvmutil: safer cast in nvm_word
cast buf[x] directly.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-09 16:41:40 +00:00
Leah Rowe 2d4567238a util/nvmutil: define _FILE_OFFSET_BITS
some older systems have 32-bit off_t. this makes them
have 64-bit off_t

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-09 16:34:59 +00:00
Leah Rowe 48b8be7a24 util/nvmutil: include sys/types.h
some older systems need it for pread/pwrite

it must come before stat.h

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-09 16:33:30 +00:00
Leah Rowe 31bd21a466 util/nvmutil: use even older define for pread
with this new define, we can target even older systems
from the late 90s.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-09 16:29:05 +00:00
Leah Rowe 8fc0f4fa07 util/nvmutil: tidied up a comment
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-09 15:32:26 +00:00
Leah Rowe 681967c386 util/nvmutil: don't include not-needed inttypes.h
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-09 15:30:03 +00:00
Leah Rowe 2773736dfc util/nvmutil: reset errno on urandom partial read
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-09 15:00:52 +00:00
Leah Rowe 62964b42ed util/nvmutil: consistent file location on err()
put it at the start of the message

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-09 14:47:33 +00:00
Leah Rowe f2a5f2de45 move nvmutil .gitignore rules to util/nvmutil/
i plan to release this as a standalone utility at
some point, once it's perfect (on its current
feature set)

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-09 14:38:21 +00:00
Leah Rowe 7e0b052412 util/nvmutil: remove empty ChangeLog/README
i will write a *manpage* at some point. for now, the
documentation on libreboot.org shall suffice.

i'm nearly ready to submit this code to coreboot.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-09 14:35:44 +00:00
Leah Rowe f05a273cb4 util/nvmutil: clean up the Makefile
that option there is already defined in the code

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-09 14:33:21 +00:00
Leah Rowe 3f566d3250 util/nvmutil: tidy up the main comment header
merge it into one

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-09 14:32:15 +00:00
Leah Rowe 4fd6bdc4ef util/nvmutil: say what the program does!
and with this, i'm probably done for a while

i've obsessively audited this code for a week

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-09 07:03:55 +00:00
Leah Rowe f266e2a16c util/nvmutil: standardised Makefile (add all)
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-09 06:46:12 +00:00
Leah Rowe c96254be8b util/nvmutil: Makefile cleanup
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-09 06:38:18 +00:00
Leah Rowe 6a4ad68273 util/nvmutil: add phone section to Makefile
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-09 06:33:52 +00:00
Leah Rowe ec1e6bd7e8 util/nvmutil: safer / more portable install
explicitly declare the directory path for the given
file (nvmutil), otherwise it's implementation-defined;
on some systems, /bin/nvmutil means a directory named
nvmutil could then contain nvmutil.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-09 06:31:52 +00:00
Leah Rowe 1446df01f2 util/nvmutil: create install dir in Makefile
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-09 06:30:14 +00:00
Leah Rowe 9f75a23a84 util/nvmutil: fix target in Makefile
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-09 06:28:50 +00:00
Leah Rowe d0bf316edf util/nvmutil: directly compare fd/urandom_fd
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-09 06:24:22 +00:00
Leah Rowe dbc7fadcbc util/nvmutil: allow partial reads of /dev/urandom
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-09 06:09:34 +00:00
Leah Rowe e092b2ce0b util/nvmutil: explicit cast in nvm_word
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-09 05:31:51 +00:00
Leah Rowe 3b73ea4288 util/nvmutil: clarify CFLAGS in code
makefile is correct, but lots of people don't read it.

putting it iin code helps people avoid confusion.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-09 05:25:58 +00:00