Commit Graph

3612 Commits

Author SHA1 Message Date
Leah Rowe
4e7a9e41a6 util/spkmodem-recv: extensive commenting
and with this, i'm now pretty much done modifying grub's
crappy code. this experiment started in 2023 has now
pretty much concluded.

the original GNU code was poorly written, hardcoded
everywhere, and not documented or commented at all.

i had to learn what the code is doing through inference
instead, and i'm pretty sure that these explanations
cover everything. i hope?

maybe the frenchman can explain anything i missed. haha.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-12 21:02:51 +00:00
Leah Rowe
29dc8312c6 util/spkmodem: explain what the defines are
and calculate some of them instead of hard coding

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-12 20:29:37 +00:00
Leah Rowe
2a32e498c8 useful comments
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-12 20:06:23 +00:00
Leah Rowe
8ccaff0d8e util/spkmodem-recv: also cache sep_pos in decode
yet another optimisation for weaker compilers - but
some modern compilers may not optimise well for this
code either.

this reduces the amount of references to the struct,
which is very expensive (48000 times per second) on
very old CPUs.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-12 19:57:24 +00:00
Leah Rowe
9f41591a46 util/spkmodem-recv: optimise decode_pulse
the frame[] array is never actually used meaningfully.

that setting of frame[ringpos] on the decode_state is
only set here, but then the value isn't really used at
all. the entire size of the annay is used for sizeof
in print_stats, but then we can just declare that
manually. since we also know that this value never
changes, we can use a global define for the sizeof entry
in print_stats, thereby simplifying operation further

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-12 19:54:52 +00:00
Leah Rowe
a7c69c3233 util/spkmodem-recv: clean up decode_pulse
make it easier to read by clearer variable naming.

this change also reduces memory accesses (fewer struct
dereferences - see: struct decoder_state), when using
much weaker/older compilers that don't optimise
properly. this, in the most active part of the code,
which is called.... 48000 times a second. peanuts on
modern CPUs, but on old (early 90s) CPUs it makes a
big difference.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-12 19:34:44 +00:00
Leah Rowe
3633878e1f util/spkmodem-recv: byte swap on big endian CPU
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-12 19:09:58 +00:00
Leah Rowe
594a5a02cd util/spkmodem-recv: remove errno define
may break on modern systems (macro)

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-12 18:45:32 +00:00
Leah Rowe
9375ecc6a4 add endianness check to spkmodem-recv
a bit dirty. should handle this at runtime.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-12 18:37:11 +00:00
Leah Rowe
f7fc5b1651 util/spkmodem-recv: properly handle stdin err
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-12 18:34:43 +00:00
Leah Rowe
22af92b473 another correction
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-12 18:25:34 +00:00
Leah Rowe
5b92b00bad util/nvmutil: fix regression on openbsd
when i removed arc4random integration, i forgot
to change this line back. oops!

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-12 18:23:17 +00:00
Leah Rowe
1cce4871e2 TODO
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-12 17:55:54 +00:00
Leah Rowe
cde28a8fbb TODO
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-12 17:52:05 +00:00
Leah Rowe
08b9595bc5 TODO
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-12 17:31:25 +00:00
Leah Rowe
ab9ee73350 util/nvmutil: mitigate buggy libc i/o
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-12 16:34:33 +00:00
Leah Rowe
303c382eae util/nvmutil: implement zero-byte r/w timeout
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-12 16:24:36 +00:00
Leah Rowe
9656e78c3a TODO
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-12 16:16:09 +00:00
Leah Rowe
571c474866 util/nvmutil: add some useful comments
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-12 16:10:28 +00:00
Leah Rowe
1fb720e1e6 util/nvmutil: split up rw_file_exact
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-12 16:03:17 +00:00
Leah Rowe
48f124a2e8 fix indentation
i was editting this in another editor

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-12 15:34:10 +00:00
Leah Rowe
f04b796dcc util/nvmutil: add jitter to fallback_rand entropy
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-12 14:36:01 +00:00
Leah Rowe
38c3889f67 util/nvmutil: improved entropy in fallback_rand
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-12 14:32:46 +00:00
Leah Rowe
1cee54ee2b util/nvmutil: remove /dev/random fallback
only use the old fallback, or /dev/urandom

/dev/random blocks on some older unix machines,
or in embedded environments that may never
have enough entropy, causing the code to hang.

urandom is most certainly expected to exist on
pretty much anything since the mid 90s.

i could probably re-add the arc4random setup
for BSDs. i'll think about it. gotta do that
portably too.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-12 14:26:54 +00:00
Leah Rowe
1ad9ffb482 util/nvmutil: include time.h after types.h
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-12 14:16:11 +00:00
Leah Rowe
922344e81e util/nvmutil: mitigate fast calls to rand
if someone calls rhex fast enough, the timestamp
may not change. this mitigates that by adding
a counter value to the mix

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-12 14:13:55 +00:00
Leah Rowe
53c5a40007 util/nvmutil: fallback randomiser
used when a random device isn't available, on old
unix, or on certain chroot environments.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-12 14:10:29 +00:00
Leah Rowe
5ba0b98fbc util/nvmutil: correct install usage
one for directory, then copy the binary

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-12 14:01:24 +00:00
Leah Rowe
35ba3aef91 util/nvmutil: fix makefile
forgot to include the binary in the path

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-12 13:56:47 +00:00
Leah Rowe
af1a219e40 util/nvmutil: remove errno extern
may break modern systems. and all old systems that
i care about will handle errno just fine.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-12 13:54:04 +00:00
Leah Rowe
8269b05a63 util/nvmutil: use install -d instead of mkdir -p
-p isn't portable

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-12 13:53:02 +00:00
Leah Rowe
e931f66b99 util/nvmutil: fix comment
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-12 13:48:53 +00:00
Leah Rowe
fe630cba5e util/nvmutil: use chmod instead, in Makefile
yeah, why not use a tool that's been around since the
80s?

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-12 13:43:20 +00:00
Leah Rowe
478c994f34 util/nvmutil: add -m to install on Makefile
also support LDFLAGS

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-12 13:42:12 +00:00
Leah Rowe
ea4deb98b5 util/nvmutil: simplify flags on urandom
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-12 13:31:35 +00:00
Leah Rowe
55e071c381 util/nvmutil: fix mistake in random check
forgot this. oops

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-12 13:28:18 +00:00
Leah Rowe
a9e5f9ef4f util/nvmutil: portable errno
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-12 13:25:35 +00:00
Leah Rowe
87b74aadbc util/nvmutil: fix non-portable variable declaration
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-12 13:22:30 +00:00
Leah Rowe
1184dcb05c util/nvmutil: disable urandom if not found
disable random mac address generation on really old
operating systems.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-12 13:21:03 +00:00
Leah Rowe
74f2d9a1f3 util/nvmutil: use ECANCELED on checksum err
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-12 13:10:10 +00:00
Leah Rowe
a40d14fcd6 util/nvmutil: don't use errno for program state
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-12 12:52:22 +00:00
Leah Rowe
a4f5061297 util/spkmodem-recv: code cleanup
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-12 07:13:42 +00:00
Leah Rowe
2479633368 util/spkmodem-recv: optimise pulse check
the last change was good, but this code, again,
has to do these calculations 48,000 times a second.

trivial on new computers. but now try it on a
computer from 1992.

we should try to make this as fast as possible :)

older compilers especially don't optimise these
checks. this patch shifts it to one subtraction and
one unsigned comparison, rather than checking less
than or greater than both. often used in... literally
exactly this type of program.

on a good compiler this will compile to an add, cmp
and conditional jump.

less readable, but the results (set 1 or 0) make it
pretty obvious what it does, after a few seconds.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-12 05:31:44 +00:00
Leah Rowe
e23cecb496 util/spkmodem-recv: clearer pulse decoding
i turned this into abs() call earlier, but this isn't
obviously readable by some people.

make it absolutely clear what this does. also reduces
use of syscalls.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-12 05:28:34 +00:00
Leah Rowe
1d2ee1cabc util/spkmodem-recv: say what freq_sep/data are
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-12 05:25:24 +00:00
Leah Rowe
fcecb1229e util/spkmodem-recv: add a usage function
replace the err call in getopt

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-12 05:22:25 +00:00
Leah Rowe
69fb0618cc util/spkmodem-recv: tidy up the getopt loop
more knf-compliant

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-12 05:20:09 +00:00
Leah Rowe
65a6205dab util/spkmodem-recv: allow short sample reads
fread() may return short reads, whereas the current
code assumes either EOF or a full read.

change if to a while. really, it's that simple.
just loop until it's done. i probably b0rked this
myself when refactoring the GNU code.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-12 05:17:09 +00:00
Leah Rowe
59f04eba9e util/spkmodem-recv: don't exit in print_stats
i treated ftell errors as fatal, but if fttell fails
with ESPIPE, and someone's using -d, the program may
exit immediately, even though there's no problem.

instead, skip printing the offset (basically no debug).

this fixes a bug that i introduced myself, when i forked
this code, because i added that error check; the GNU
code didn't have any check whatsoever.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-12 05:13:44 +00:00
Leah Rowe
9ac0331595 util/spkmodem-recv: buffer calls to fread()
we currently read small amounts of data with fread,
repeatedly, which is quite taxing on the CPU, on
very old systems.

48khz audio. 48000 calls to fread() per second?

yeah. let's optimise this.

performance now should be roughly O(1) in practise.
this and the other recent changes means no modulo
or division, reduced branching, memory memory roads,
and lots of buffering.

the buffering here is quite conservative, so the human
won't notice any difference. we're cutting the number
of times we call fread by a factor of several thousand,
but you'll still see text scrolling down pretty quick,
with minimal lag.

the old GNU code i forked was terrible at this.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-12 05:08:44 +00:00