Commit Graph

3626 Commits

Author SHA1 Message Date
Leah Rowe 4dc39990e1 util/spkmodem-decode: don't run decode in col_sep_tone
otherwise, it runs twice

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:39 +00:00
Leah Rowe 7e29d53667 util/spkmodem-decode: guard against silence in tone-detect
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:04 +00:00
Leah Rowe a81dde3d08 util/spkmodem-decode: split up handle_audio()
this enables the separated code to have reduced
indentation

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:02 +00:00
Leah Rowe e8d46de0e1 util/spkmodem-decode: also auto-detect separator
the tone detection currently only tracks data, not
the separator. track both instead, for improved
detection reliability.

e.g.

separator tone e.g: 9
data low tone e.g: 18
data high tone e.g: 24

two fir windows produce e.g.
freq data 9 sep 0
then 18, 9
then 24, 9
18, 9

so we take min(data, separator)

that gives 9,9,9,9

now we have the separator cluster

however, if both windows are active during transitions,
you can also capture the higher clusters, which would
allow freq_max to grow

so when you learn e.g.:

freq min = 9
freq max 24

then the learned threshold would be:

(9 + 24) / 2 = 16

and now you know how to separate the tones

fir already suppresses noise so the pulse should
be reliable. so freq/sep only go non-zero when an
actual tone exists

this should now result in being able to sync with
spkmodem encoders with no prior knowledge of the
correct tone frequences. we just use maths.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:58:19 +00:00
Leah Rowe 745e888edf util/spkmodem-decode: simplify valid_signal
since we have auto-detection now, we only need to know
that two signals exist, not that they are valid, since
the auto-detection now handles validation and fallback.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:58:19 +00:00
Leah Rowe 5b4c3c537e util/spkmodem-decode: automatic tone detection
a continuation of the previous patch. this waits for
currently one second, before defaulting to the hardcoded
value. otherwise, it tries to use whatever timing it
gets automatically.

this way, the program should now reconfigure its own
timing, without intervention by the user, if the timing
differs from sensible defaults.

this is because spkmodem is implementation-defined;
it's just however coreboot and/or GRUB happen to set
it up, and on the hardware in question.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:58:19 +00:00
Leah Rowe 78b71f867f util/spkmodem-decode: automatic tone calibration
current logic is hardcoded, as in the original spkmodem-recv.

with this change, small differences are observed and averaged,
then the detection thresholds are adjusted accordingly.

the existing macros serve as a baseline, but real signals
differ. with this change, we therefore account for possible
drift in timings, which can change in real-time; the old
code could possibly get out of sync beccause of that, which
may have resulted in corrupt characters on the screen. this
change therefore should make the output a bit more stable.

the detection window is continually adjusted, so that the
output timings don't drift.

the tolerances are automatically adjusted based on base
timings (see new define in patch)

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:58:16 +00:00
Leah Rowe d03f80b893 util-spkmodem-decode: tidy up print_stats
make the frequencies clearer in printf

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:49:05 +00:00
Leah Rowe 4f26620e88 util/spkmodem-decode: do getopt first
much cleaner. do it right after zero-init memset.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:49:05 +00:00
Leah Rowe a0c8fd519e util/spkmodem-decode: init argv0 before pledge
otherwise, it'll be empty/undefined

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:49:05 +00:00
Leah Rowe a60be4028a util/spkmodem-decode: frequency meter in debug
useful timing now displayed

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:49:05 +00:00
Leah Rowe 6eff58aa7d util/spkmodem-recv: reset char precisely on timeout
instead of when it goes above, do it precisely on the
timeout. otherwise, if by sheer chance the signal
pauses and we reset the byte - sure, ok, but it's a
bit tight and we run the risk of advancing another
frame, depending on the timing.

this is a minor edge case, probably rarely ever
triggered in practise.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:49:05 +00:00
Leah Rowe 30d5ca5146 rename util/spkmodem-recv to spkmodem-decode
it's no longer resembling the original util at all,
so a rename seems indicated. yes.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:49:05 +00:00
Leah Rowe a31cfa4592 add missing star
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:49:05 +00:00
Leah Rowe cefae03502 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-26 06:49:01 +00:00
Leah Rowe 78d583e5ec 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-26 06:47:55 +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