Commit Graph

3230 Commits

Author SHA1 Message Date
Leah Rowe
c721d6f4f4 util/nvmutil: use EXIT_FAILURE/SUCCESS for exits
this, in conjunction with the centralised exit scheme now
used by nvmutil, means that we have portable exit status.

notwithstanding the use of non-portable unix functions, and
especially the use of non-standard err.c (which GNU and BSD
libc implementations all have anyway, as does musl).

this code should now run on essentially any computer with
Linux or BSD on it.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 03:43:06 +00:00
Leah Rowe
e4b8bb4875 util/nvmutil: make err_if a function, not a macro
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 03:38:45 +00:00
Leah Rowe
c98d5c743f util/nvmutil: properly cast void use of set_err
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 03:35:37 +00:00
Leah Rowe
7213bba0c5 util/nvmutil: don't exit with errno as status
exit with 0 or 1, as is proper.

errno is an int, but the return value on a shell
can be e.g. byte, and depending how that number (errno)
is valued, could overflow and cause a zero exit, where
you want a non-zero exit.

the code has been changed, in such a way to maintain
current behaviour (don't change errno), except that when
errno is set upon exit, the exit value is now one.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 03:31:09 +00:00
Leah Rowe
490f311d05 util/nvmutil: split up cmd_setmac
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 02:50:03 +00:00
Leah Rowe
3cbaa7ead8 util/nvmutil: rename some functions for clarity
also re-order the prototypes

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 02:32:42 +00:00
Leah Rowe
707fabab38 util/nvmutil: split up parseMacString
split it into smaller, more readable functions

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 02:25:11 +00:00
Leah Rowe
d9c307d5a3 util/nvmutil: remove useless gbe variable
use buf directly

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 01:50:21 +00:00
Leah Rowe
a7cc8143a7 util/nvmutil: cmd copy/swap: use word/setWord
this means that we make use of the boundary checks. it's just
a safer way of handling these functions.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 01:18:23 +00:00
Leah Rowe
566ae72ca3 util/nvmutil: remove variable nvmPartChanged
pointless optimisation. we know that when a user requests an
operation that would write, it will probably result in a change.

therefore, this change is the real optimisation. to avoid
writing the same half of a file twice, when using cmd_copy,
we check (in writeGbe) whether gbe part 0 and 1 are the same;
if they are, then we only loop once. this is important, because
otherwise we would call swap() twice.

this means that the optimisations in cmd_copy and cmd_swap must
be removed. the point of this and other changes is to improve
memory safety in nvmutil, so frivolous use of pointers has to go.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 01:07:59 +00:00
Leah Rowe
dfbb3c5d9e util/nvmutil: merge nvmalloc with readGbe
it's so simply now, all it does is set the gbe pointers

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 00:41:05 +00:00
Leah Rowe
2b01e023ab util/nvmutil: remove do_read
pointless code complication, that doesn't yield a noticeable
performance increase.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 00:40:07 +00:00
Leah Rowe
1bfc89e3ad util/nvmutil: read in fixed 4KB blocks
modern file systems work in 4KB blocks. reading only
a small part of it doesn't really make much difference
in terms of performance.

simplify the code instead.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 00:34:31 +00:00
Leah Rowe
1d078c9daf util/nvmutil: use fixed buffer
modern malloc implementations make the optimisation here
pretty pointless.

modern computers make this modification pointless.

i'm not planning to run nvmutil on a VAX. openbsd removed
support for it ages ago. 8KB fixed buffer is fine.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 00:20:16 +00:00
Leah Rowe
0ccb790fc0 util/nvmutil: remove pointless arg in openFiles
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-03 00:11:49 +00:00
Leah Rowe
6dd91134bd util/nvmutil: don't use xor swap
it doesn't save any time on modern systems, and it's just
confusing for some people to read. i mean, i understand it
instinctively, but normal people do it with a swap variable.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-02 23:29:00 +00:00
Leah Rowe
53e386d2b5 util/nvmutil: don't use size_t as pointer
the only reason i did this was for that xor swap, but we
can just use an intermediary value

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-02 23:26:02 +00:00
Leah Rowe
3248b8f651 util/nvmutil: don't capitalise set_err
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-02 23:04:03 +00:00
Leah Rowe
d3ca9946a9 util/nvmutil: make xopen a function, not a macro
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-02 23:02:53 +00:00
Leah Rowe
761968e3c4 util/nvmutil: merge block_unveil back with main
we always want unveil/pledge calls to be in main, when
possible, so that they are more transparent and easier
to understand when re-factoring, because it's extremely
important that these syscalls be done correctly.

main is small enough now, from other re-factoring changes,
that i'm happy to have this back in main now.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-02 22:30:19 +00:00
Leah Rowe
589ac92781 util/nvmutil: fix bad bound check
the current check is too liberal. make it sticter.

the issue is that the previous check did not take
into account that it's a check on a uint16_t array,
against nf which refers to a number of bytes.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-02 17:47:58 +00:00
Leah Rowe
51e4e43c94 util/nvmutil: tidy up the xopen macro
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-02 17:41:10 +00:00
Leah Rowe
a34e79f501 util/nvmutil: add boundary checks on word/setWord
this was the other complication with doing it as a macro.

for something this fundamental, we really want to ensure
that every access is safe.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-02 17:35:00 +00:00
Leah Rowe
4e7d48b5c5 util/nvmutil: make word/setWord a function
having this as a macro makes the code quite brittle.

better to have it as a function.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-02 17:34:37 +00:00
Leah Rowe
8707695184 util/nvmutil: rename openGbeFile to openFiles
merge the urandom handling back into this function.

it's called immediately after in main anyway, so we
may as well. this reduces the size of main.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-02 17:06:57 +00:00
Leah Rowe
c4138752c3 util/nvmutil: remove redundant unveil call
in the given call, we then do an equivalent call
immediately after that is the same, but without
unveil, so we'll just defer to that.

this changes no behaviour.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-02 17:03:00 +00:00
Leah Rowe
a4fe1bfa4d util/nvmutil: call block_unveil earlier
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-02 17:01:41 +00:00
Leah Rowe
ea1a9bc786 util/nvmutil: bring pledge in set_io_flag to main
in general, we should ensure that the pledge calls only happen
inside main. this means we can more easily see them, in future
re-factoring.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-02 16:57:50 +00:00
Leah Rowe
46b6b1feb3 util/nvmutil: call set_cmd much earlier
this will enable hardening of the pledge syscalls.

it also means that the program will error out much
earlier, when an invalid command is given, rather
than opening a bunch of files first, and it will
do so under reduced privilege already, notwithstanding
the further pledge/unveil hardening that is planned.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-02 16:46:20 +00:00
Leah Rowe
0106c38217 util/nvmutil: unveil /dev/urandom much earlier
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-02 16:41:06 +00:00
Leah Rowe
58cf5a1579 util/nvmutil: split xopen handling
same as the previous change. i'm going to harden the unveil
and pledge calls next.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-02 16:38:09 +00:00
Leah Rowe
e5d7c3e3a2 util/nvmutil: split unveil handling
urandom in main. this is because i'm going to further
harden the use of pledge and unveil in a future patch,
and this is a prerequisite.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-02 16:34:59 +00:00
Leah Rowe
896f0ea1df util/nvmutil: split cmd init to new function
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-02 16:34:18 +00:00
Leah Rowe
697eda800a util/nvmutil: split flags init to new function
main is getting much smaller now

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-02 12:55:57 +00:00
Leah Rowe
ab057e006c util/nvmutil: tidy up main()
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-02 12:50:18 +00:00
Leah Rowe
6de3968f2b util/nvmutil: split unveil code to new function
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-02 12:18:23 +00:00
Leah Rowe
9c23eac148 util/nvmutil: separate usage function
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-02 12:17:07 +00:00
Leah Rowe
7e81c5c630 Re-add x230_12mb corebootfb config
also for other variants

i removed it because it was reported broken. it's not.

the removal was always temporary, pending further testing.
next time, i will be more sceptical.

everything works fine.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-02-26 00:58:06 +00:00
Leah Rowe
1d20042f9a nvmutil: simplify readGbe and writeGbe
the for loop only contains one line

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-02-23 14:22:35 +00:00
Leah Rowe
5a414ea4d6 nvmutil: centralise all errno handling
do it in the macro. this way, if a given error is
present, it's not overridden. this enables easier
debugging.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-02-23 14:01:11 +00:00
Leah Rowe
c64a2655e9 nvmutil: rename ERR to SET_ERR, for clarity
i renamed filename to fname, so that certain lines would
still fit within 80 characters without introducing a new
line break.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-02-23 13:49:16 +00:00
Leah Rowe
678b9d859b nvmutil: split pread from readGbe
split it into readGbe_part, for code clarity.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-02-23 13:45:28 +00:00
Leah Rowe
e1d8773c54 nvmutil: split pwrite handling from writeGbe
handle it in a separate function, for clarity.

the main function just checks each part whether it
changed, and then passes control to writeGbe_part.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-02-23 13:42:27 +00:00
Leah Rowe
261c41a3c3 nvmutil: simplify a few else statements
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-02-23 13:34:35 +00:00
Leah Rowe
8cf2558a6f nvmutil: don't hardcode errno to ECANCELED
use the ERR macro instead, so that an existing value
will not be overridden. this is useful for debugging.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-02-23 13:30:15 +00:00
Leah Rowe
50de561ac4 nvmutil: explain a few parts in nvmalloc
the current code is optimised for speed, but it's a bit
esoteric, so make it easier to understand.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-02-23 11:13:09 +00:00
Leah Rowe
061e6048a8 nvmutil: split malloc handling out of readGbe
this has to do with memory allocation, not actual reading
of the gbe file into memory. split it up, for clarity.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-02-23 11:10:12 +00:00
Leah Rowe
3d408317b1 nvmutil: further simplify nr/nw error handling
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-02-23 11:01:52 +00:00
Leah Rowe
35d8d0993f util/nvmutil: simplify nr/nw error handling
when nf and nr/nw are not the same, we know there
is an error condition, so defer to the following err()
call, but use ERR() there instead of hardcoding use
of ECANCELED.

this actually improves the error handling, by being
more verbose, while reducing the amount of logic.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-02-23 09:50:26 +00:00
Leah Rowe
4473179300 util/nvmutil: remove pointless diagnostics
we only need to know the number of bytes written or
read under error conditions.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-02-23 09:47:36 +00:00