Commit Graph

3858 Commits

Author SHA1 Message Date
Leah Rowe 1dbd679c0c util/nvmutil: further tidy up prw()
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe e239a3107f util/nvmutil: clean up i/o functions
properly verify the value of the arguments, with
asserts.

add simpler runtime checks in-function, on prw,
rw_file_once and rw_file_exact.

variable names in english now, and the code is
cleaner, while being functionally equivalent.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe 347e39bab7 util/nvmutil: further tidy up rw_file_once
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe b4154dd29b util/nvmutil: minor cleanup: rw_file_once
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe a85f2898e2 util/nvmutil: remove dead code
useless check. will never be true.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe fdd99a9a45 util/nvmutil: remove pointless comment
itt's totally obvious

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe 5f6a721fa0 util/nvmutil: reduced indentation in rw_file_once
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe 172cbd4497 util/spkmodem-decode: add CHAR_BIT define
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe 273bd992dd spkmodem-decode: edge detection *and* amplitude
for pulses, we currently use amplitude detection.

edge detection is better, because weak / low gain
signals will be more reliable. if audio is coming
in on/from a system that does automatic gain
adjustment, this once again is more robust too.

microphones and speakers (which people often use
with spkmodem if nothing else available) often
clamp amplitude, to an extent that this software
may not detect those pulses reliably that way.

so we detect slope edges instead. this causes
very little performance penalty (use of abs(),
that's about it)

however, edge detection is inherently vulnerable
to noise, so we will also detect amplitude. this
acts as an effective noise filter, while still
improving pulse detection.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe 2aa1ccfd8a spkmodem-decode: reset calibration accumulators
in select_separator_tone, i never reset these
after computing their average.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe fd94bff968 util/spkmodem-decode: remove dead code
this check no longer applies (never triggers)

is_signal_valid already guarantees that the separator
tone is valid.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe 1b73308c51 spkmodem-decode: small cleanup in decode_pulse
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe cad667faee spkmodem-decode: don't select sep tone on bad signal
otherwise, calibration could collect garbage data.
this improves noise mitigation.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe 15079d02bd spkmodem-decode: ignore invalid frames when setting bits
bits are currently assembled even on invalid frames. this
patch fixes that - the bug is also in the GNU version.

this reduces the chance of noise/calibration from creating
corrupt character output during operation.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe 3245333c53 spkmodem-decode: reset char if separator disappears
improves reliability in the case when audio cuts out,
mic glitches, laptop audio power saving, etc.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe fe6da7a238 spkmodem-decode: fix single-tone learning bug
enforce at least two tones. this mitigates the
chance of random noise being treated as a real
tone, and reduces the chance of broken
thresholds versus freq min/max e.g.
freq min 31, max 32 and threshold 31

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:40 +00:00
Leah Rowe 9e94cc017a spkmodem-decode: enforce calibrated seperator range
we weren't actually using what we calculated. this patch
fixes that, thus preventing random noise / microphone
clicks, random artifacts and such from being treated
as real frames (the purpose of is_valid_signal is
partly noise suppression).

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:40 +00:00
Leah Rowe 9d24b78340 spkmodem-decode: fix wrong sample count
in handle audio, i do the number of samples
per frame, and one more. e.g. 241 instead of
240. this bug is in the original GNU version
too. this patch fixes it.

this means that the output could slowly go
out of sync with calculated timings. the
patch fixes that. in practise, the decoder
is not that sensitive, and the code would
adjust anyway (automatic timing adjustment),
but ideally we want to not *cause* such
issues even if we mitigate them.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:40 +00:00
Leah Rowe 8085de594b Revert "util/spkmodem-decode: fix 3-frame timeout"
This reverts commit dbf0c3ccc2.
2026-03-26 06:59:40 +00:00
Leah Rowe 1d0e44c23a util/spkmodem-decode: clarify frame count on check
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:40 +00:00
Leah Rowe a049f2df39 util/spkmodem-decode: static asserts
assert integer sizes, important in this program because
we make several implicit assumptions about word sizes,
and integers need to be of a certain size.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:40 +00:00
Leah Rowe 681f580053 util/spkmodem-decode: annotate prototypes
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:40 +00:00
Leah Rowe 7c88154cd2 util/spkmodem-decode: rename function for clarity
collect_separator should be select_separator, to bring
it in line with select_low_tone

this just makes the code a bit easier to read

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:40 +00:00
Leah Rowe fff2447606 util/spkmodem-decode: tidy up indentation
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:40 +00:00
Leah Rowe 6ed722b73b fix typo in comment
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:40 +00:00
Leah Rowe 4fff5121e8 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-26 06:59:40 +00:00
Leah Rowe feb69bd946 util/spkmodem-decode: rename auto_detect_tone
auto seems redundant. detect implies auto.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:40 +00:00
Leah Rowe 402e0ca7cc 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-26 06:59:40 +00:00
Leah Rowe 84b0b8a543 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-26 06:59:40 +00:00
Leah Rowe 011c65ddf4 spkmodem-decode: don't dump learn_samples in silence check
oops

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:40 +00:00
Leah Rowe a5d40166ce util/spkmodem-decode: split up auto_detect_tone
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:40 +00:00
Leah Rowe 68fb740da2 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-26 06:59:40 +00:00
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