Commit Graph

3785 Commits

Author SHA1 Message Date
Leah Rowe
2cc06fcdbe util/spkmodem-decode: tidy up indentation
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-13 00:50:40 +00:00
Leah Rowe
a010665c1d fix typo in comment
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-13 00:29:41 +00:00
Leah Rowe
dbf0c3ccc2 util/spkmodem-decode: fix 3-frame timeout
i accidentally left this reset here during a
previous refactor.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-13 00:13:51 +00:00
Leah Rowe
36d914149d util/spkmodem-decode: rename auto_detect_tone
auto seems redundant. detect implies auto.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-12 23:58:59 +00:00
Leah Rowe
2bb17c61d0 spkmodem-decode: learn tone per frame, not sample
the fir filter produces stable frequencies per frame,
but learning per sample (within a frame) means we
record the same value roughly 240 times.

here, we are syncing up at just the right moment
instead, and only at that moment, this increasing
both performance and reliability.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-12 23:54:01 +00:00
Leah Rowe
63f4fa1f41 spkmodem-decode: fix learn_samples increment
oops!!!

another mistake during refactoring. right now it
doesn't increment before being checked, so learning
can go forever in an infinite loop

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-12 23:42:00 +00:00
Leah Rowe
769664779a spkmodem-decode: don't dump learn_samples in silence check
oops

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-12 23:40:56 +00:00
Leah Rowe
b59cc0f1ae util/spkmodem-decode: split up auto_detect_tone
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-12 23:38:10 +00:00
Leah Rowe
f396654ae8 util/spkmodem-decode: separate silence check
i conflated two separate tests in a previous change.

the silence check was defeated by still checking f
alongside it, which would be set, thus satisfying
the condition, and proceeding, which defeats the
purpose of the silence check (ignore false signal
that is actually noise) - so in the original patch
that i wrote, the extra checks actually do nothing.

this patch fixes that, and makes the logic a bit
clearer.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-12 23:26:47 +00:00
Leah Rowe
c4fefba877 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-12 23:21:18 +00:00
Leah Rowe
88ca9e6f03 util/spkmodem-decode: guard against silence in tone-detect
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-12 23:18:26 +00:00
Leah Rowe
3189e3e079 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-12 23:14:03 +00:00
Leah Rowe
dd9e681eb9 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 ≈ 9
data low tone ≈ 18
data high tone ≈ 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-12 23:12:40 +00:00
Leah Rowe
5edd7c9fae 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-12 22:53:17 +00:00
Leah Rowe
852a6ccf11 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-12 22:44:50 +00:00
Leah Rowe
569660ada9 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-12 22:16:23 +00:00
Leah Rowe
606c452bd8 util-spkmodem-decode: tidy up print_stats
make the frequencies clearer in printf

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-12 21:53:43 +00:00
Leah Rowe
995ac0bcd7 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-12 21:50:29 +00:00
Leah Rowe
912cd99f9f util/spkmodem-decode: init argv0 before pledge
otherwise, it'll be empty/undefined

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-12 21:49:54 +00:00
Leah Rowe
6204ff3ebf util/spkmodem-decode: frequency meter in debug
useful timing now displayed

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-12 21:43:23 +00:00
Leah Rowe
c8d078f9c7 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-12 21:33:46 +00:00
Leah Rowe
10e09d6402 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-12 21:24:16 +00:00
Leah Rowe
f0466457be add missing star
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-12 21:09:18 +00:00
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