Commit Graph

672 Commits

Author SHA1 Message Date
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
Leah Rowe 3b188b4d2b util/nvmutil: specifically enable -std=c99
I also needed: #define _POSIX_C_SOURCE 200809L

I use -pedantic with -Wall -Wextra -Werror, which
forces very strict error handling and ISO C; this
means pread and pwrite aren't available.

The define fixes this.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-09 05:18:53 +00:00
Leah Rowe 532d723ccd util/nvmutil: fix indentation in setmac
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-09 04:37:48 +00:00
Leah Rowe 14c2588772 util/nvmutil: err sooner, on bad command
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-09 04:04:12 +00:00
Leah Rowe 1a60eabcfe util/nvmutil: print expected checksums
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-09 03:56:50 +00:00
Leah Rowe c27f8b709a util/nvmutil: rename cmd helper functions
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-09 03:29:08 +00:00
Leah Rowe 2cf1d1bed8 util/nvmutil: remove MAC address short-commands
only allow the long form: setmac [MAC]

specifying gbe.bin just shows the help/usage now.
this is a safety feature, so that someone doesn't
accidentally write the gbe file. we want it to be
that the user specifically requested setmac.

setmac with mac address as the 3rd argument is
also disabled. this is done as part of a general
simplification and safety improvement to nvmutil.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-09 03:25:33 +00:00
Leah Rowe aeb076b30e util/nvmutil: remove cmd_brick
this is an extremely dangerous feature, and serves
no purpose to the user.

this change is part of a series of extreme safety
improvements, part of a larger nvmutil audit.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-09 02:23:22 +00:00
Leah Rowe 0c64907a9e util/nvmutil: remove the setchecksum command
This feature is extremely dangerous, and we should
discourage against its use.

This is part of a series of changes that I've made
to make the code safer. You should only ever run
this on a valid GbE file, and nothing else.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-09 02:19:24 +00:00
Leah Rowe f7dfb0d265 util/nvmutil: re-order functions by execution
linear, top-down order. re-order the prototypes

also some general cleanup:

argc enums now validated. ifdefs for pledge
and arc4random now use a consistent naming
scheme.

feature change:

the "dump" command now fails if both checksums
are invalid, and won't show anything.

my next commit will disable setchecksum when
both checksums are invalid. this and the other
insane auditing i've done over the last few
days has been part of a major effort to make
nvmutil extremely safe, and robust.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-09 02:12:28 +00:00
Leah Rowe a6e271c86d util/nvmutil: unified checksum update
setchecksum and setmac update the checksum.

other commands don't.

this patch unified the logic, handling it
in write_gbe based on command[].chksum_write

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-09 01:20:59 +00:00
Leah Rowe 39cdd562d8 util/nvmutil: don't pledge on OLD openbsd
only pledge/unveil where available, on versions
that have it. this patch disables it on older
versions, allowing nvmutil to compile.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-09 00:20:30 +00:00
Leah Rowe db440bd71d util/nvmutil: make write_mac_part() a void
its return value is never used, in the current code.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-08 23:42:11 +00:00
Leah Rowe 5dc3f323c3 util/nvmutil: fix typo in message
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-08 23:40:12 +00:00