Commit Graph

3785 Commits

Author SHA1 Message Date
Leah Rowe
e26511c8b8 util/nvmutil: err if unsupported rw_type on i/o
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 06:51:47 +00:00
Leah Rowe
0a55f286c1 util/nvmutil: rename badly named off_t assert
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 06:43:13 +00:00
Leah Rowe
0f035e208e util/nvmutil: assert two's compliment integers
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 06:35:17 +00:00
Leah Rowe
6392eb18b6 util/nvmutil: add assert for int
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 06:31:04 +00:00
Leah Rowe
83f3a059b9 util/nvmutil: add assert for off_t
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 06:30:52 +00:00
Leah Rowe
6ad7eaef32 Revert "util/nvmutil: don't use zx printf specifier"
This reverts commit ba3cf14faa.
2026-03-10 06:18:29 +00:00
Leah Rowe
90fada0a23 util/nvmutil: explain errval handling in err()
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 06:17:04 +00:00
Leah Rowe
2fda988da6 util/nvmutil: make rc size_t (not ssize_t)
i overlooked this when writing. it's comparing
to a length which is size_t, so let's avoid
an unnecessary cast.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 06:11:31 +00:00
Leah Rowe
ba3cf14faa util/nvmutil: don't use zx printf specifier
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 06:10:20 +00:00
Leah Rowe
2f1f129753 util/nvmutil: add portable asserts for integers
we need this to be the case for our code, that char
and uint8_t are 8 bits, and that uint16_t and uint32_t
are 16- and 32-bit.

these asserts protect us in case it's not (it will cause
a compile time error).

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 06:07:21 +00:00
Leah Rowe
9747ca4151 util/nvmutil: abort if I/O len exceeds SSIZE_MAX
in rw_file_exact

otherwise, if length exceeds SSIZE_MAX, we could
hit an overflow

the buffers and lengths we deal with are relatively
small anyway, so this fix is preventative

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 05:33:12 +00:00
Leah Rowe
93a4ec3497 util/nvmutil: annotate the prototypes
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 05:11:32 +00:00
Leah Rowe
f53a8d4f18 util/nvmutil: unified gbe file part I/O
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 04:58:24 +00:00
Leah Rowe
baca2d8883 util/nvmutil: remove stale define
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 04:20:55 +00:00
Leah Rowe
80f3aac62d util/nvmutil: unified I/O: stdout, urandom and gbe
everything is a file

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 04:13:02 +00:00
Leah Rowe
6402a0fbe9 util/nvmutil: unified urandom/gbe file reading
like before, but with the newly correct logic

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 03:08:09 +00:00
Leah Rowe
4131402589 util/nvmutil: safer read_gbe_file_exact
it now retries infinitely on EINTR, except when the return
of pread is precisely zero, at which point it errs.

this is better than having an arbitrary maximum like before,
and increases robustness on unreliable file systems, e.g.
NFS shares.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 02:48:34 +00:00
Leah Rowe
0c23474322 util/nvmutil: report checksum in cmd_dump
as it should be!

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-10 01:51:18 +00:00
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