Commit Graph

327 Commits

Author SHA1 Message Date
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 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
Leah Rowe 45413f1209 util/nvmutil: say part number on read/write report
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-02-23 09:34:51 +00:00
Leah Rowe a2c5715589 util/grubpo: new util, for grub PO files
this is the program I recently wrote, that generated
the submodule entries for the GRUB PO file fix

this utility is for reference only. i'll probably do
away with the fix at some point, replacing it with
my own git-based submodule repository, containing the
PO files. this would make things easier, and then
that repository would contain the utility instead.

i'm just putting this in lbmk for now, so that we
have it somewhere.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-02-19 12:24:47 +00:00
Leah Rowe 5a8ad2d311 util/nvmutil: general code cleanup
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-02-19 12:23:57 +00:00
Leah Rowe a95cf1feeb util/spkmodem_recv: general code cleanup
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-02-19 12:21:35 +00:00
Leah Rowe e9a910b33c config/git: import suckless sbase
i currently use the output of sha512sum in several
places of xbmk, which is a bit unreliable in case
output changes.

other cases where i use util outputs in variables
are probably reliable, because i'm using mostly
posix utilities in those.

to mitigate this, i now import suckless sbase, which
has a reasonable sha512sum implementation.

*every* binary it builds is being placed in build.list,
because i'll probably start using more of them.

for example, i may start modifying the "date"
implementation, adding the GNU-specific options that
i need as mentioned on init.sh

i'm importing it in util/ because the sha512sum
util is needed for verifying project sources, so
if sbase itself is a "project source", that means
we can into a chicken and egg bootstrapping problem.

this is sbase at revision:
055cc1ae1b3a13c3d8f25af0a4a3316590efcd48

Signed-off-by: Leah Rowe <leah@libreboot.org>
2025-10-04 09:20:12 +01:00
Leah Rowe 44a1cc9ef8 util/nvmutil: use x, not ?, for random characters
A user reported that '?' causes an error on zsh. See:

https://codeberg.org/libreboot/lbmk/issues/261

For example:

./mk inject libreboot-XXXXXX.tar.xz setmac ??:??:??:??:??:??

The user got:

 zsh: no matches found: ??:??:??:??:??:??

The mitigation here is to double-quote, e.g.:

./mk inject libreboot-XXXXXX.tar.xz setmac "??:??:??:??:??:??"

However, a lot of people won't do that. Therefore, I will
retain the current behaviour but support x/X for randomness.

Now lbmk uses x by default, instead. I will now update the
documentation, accordingly.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2025-05-03 06:23:10 +01:00
Leah Rowe d7312260e7 util/nvmutil: remove excessive comments
Signed-off-by: Leah Rowe <leah@libreboot.org>
2025-02-25 13:29:16 +00:00
Leah Rowe d2cb954933 util/nvmutil: Fix bad error messages on R/W
The messages didn't really make sense.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2025-02-10 20:27:48 +00:00
Leah Rowe e1e515bd22 util/nvmutil: hardened pledge on help output
Signed-off-by: Leah Rowe <leah@libreboot.org>
2025-02-07 12:31:46 +00:00
Leah Rowe a98490573b util/nvmutil: only set mac_updated at the end
after setting the checksum too

this is functionally no different, but setting it
at the start didn't sit right with me.

it's more logically correct to set it at the end,
in case any error did not result in an exit.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2025-01-29 04:45:57 +00:00
Leah Rowe 8a43535513 util/nvmutil: Fix bad comparison
We're checking if errno is ENOTDIR, not setting it;
the previous code would always return true, and then
set errno 0, which in the context of this code was
actually OK, so this patch makes no functional difference
in practise.

However, I'm a stickler for technical correctness. I caught
this when trying to compile with clang, because clang is
quite pedantic about checking for exactly this type of bug.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2025-01-29 04:10:52 +00:00
Leah Rowe a65a0c2f96 util/nvmutil: allow ./nvm gbe MAC
previously, if the user ran:

./nvm GBE [MAC address]

it would error, treating the MAC as a command

now if only 3 arguments are provided, and the
3rd argument ins't a valid command, it's treated
as a MAC address and validated accordingly.

this should make nvmutil easier to use, because
I imagine a lot of users forget to use setmac

there's no reason we should be so pedantic. we
should allow it to be used flexibly like this

Signed-off-by: Leah Rowe <leah@libreboot.org>
2025-01-29 04:07:54 +00:00
Leah Rowe 96356ce94f util/nvmutil: move "e" to swap()
we only use it there, so we should only define it
there. it's used to detect host CPU endianness.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2025-01-29 03:47:20 +00:00
Leah Rowe b1d8975959 util/nvmutil: Only read up to 4KB on larger gbe
On the 16KB and 128KB files, we still only need to
operate on 4KB at the start of each block, where the
block size is larger than 4KB.

The reason we deal with the entire 4KB block is because
the nvm words (in the 128 byte section) can define an
extended nvm area anywhere after 128 bytes, within the
128 byte block.

We could systematically read where that is being handled,
and handle it; we could then allocate less memory, and
read/write fewer bytes, but many block devices like SSDs
and flash drives have at least a 4KB erase block anyway,
so it's kinda pointless. saving memory would be nice, but
I don't really want to bloat the code.

This is a nice easy optimisation, to avoid wasting an
additional 8KB of memory when handling 16KB files, and
additional 120KB if handling 128KB files, since nf is
what determines how much memory will be allocated.

the alternative would be to use an mmap, and then we
could reasonably handle the idea above for only writing,
surgically, what we need: nvm words and extended nvm
words.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2025-01-29 03:41:55 +00:00
Leah Rowe 6821659bcb util/nvmutil: fix minor mistake (line break)
Signed-off-by: Leah Rowe <leah@libreboot.org>
2025-01-27 18:46:36 +00:00
Leah Rowe 3bb7520f6d util/nvmutil: do setmac if only filename given
./nvm gbe.bin

with this patch, the above example does the same as:

./nvm gbe.bin setmac

now you can simply specify the gbe file, and it will
randomise the mac address within it, and update the
nvm checksum word.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2025-01-27 18:40:44 +00:00
Leah Rowe a08748a9ed util/nvmutil: don't say write not needed if errno
otherwise, the output is confusing

Signed-off-by: Leah Rowe <leah@libreboot.org>
2025-01-27 05:54:06 +00:00
Leah Rowe 6841a351eb util/nvmutil: print dump *after* modification
this way, we still get an error exit for example
when trying to invalidate an already invalid
checksum; this error exit was disabled by the
last revisions.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2025-01-27 05:50:34 +00:00
Leah Rowe da0a6c216c util/nvmutil: verbosely print the written MAC
This is for user friendliness. Otherwise, many users
might try to dump afterward if they specified a random
MAC address.

This saves the user from having to re-run with the dump
command, thus saving time for the user.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2025-01-27 05:37:02 +00:00
Leah Rowe db5879c6b5 util/nvmutil: minor cleanup in cmd_dump
Signed-off-by: Leah Rowe <leah@libreboot.org>
2025-01-27 05:29:14 +00:00
Leah Rowe bd7215d1eb util/nvmutil: show nvm words written on writeGbe
Signed-off-by: Leah Rowe <leah@libreboot.org>
2025-01-27 05:26:59 +00:00
Leah Rowe c70117c79c util/nvmutil: clean up readonly check on writeGbe
Signed-off-by: Leah Rowe <leah@libreboot.org>
2025-01-27 05:18:38 +00:00
Leah Rowe cf5a63e65c util/nvmutil: Remove useless gbeFileChanged var
We don't need it.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2025-01-27 05:13:20 +00:00