Commit graph

4007 commits

Author SHA1 Message Date
bunnie
9815e33f4e fix missing packet issue due to storage overflow 2026-04-23 00:34:35 +08:00
bunnie
4319faa9d0 improve layout for modals 2026-04-23 00:34:15 +08:00
bunnie
a40eba8199 fix hosted mode warnings 2026-04-22 12:07:49 +08:00
bunnie
8f16270efa fix handling of orientation inputs 2026-04-22 03:05:00 +08:00
bunnie
fbe6aad8bb try to improve suspend/resume stability 2026-04-22 01:22:31 +08:00
bunnie
d020bda4be ignore accelerometer "keys" in video loop 2026-04-22 01:22:18 +08:00
bunnie
8521d698d7 add hooks for pre-clock transition on bio
this hook allows the application logic to warn the BIO
app that a clock transition is coming so that glitches can be
suppressed.
2026-04-21 16:27:10 +08:00
tunnell
f22f276ead Use constant-time compare for FPGA key check
The BIP-39 backup-key confirmation flow compared the entered
key against the in-memory FPGA key with `==`, which
short-circuits on the first mismatching byte and leaks
prefix-length information through timing.

Switch to `subtle::ConstantTimeEq`. The `subtle` crate is
already a direct dependency and used in the same crate's
`backups.rs` for the same purpose.

Signed-off-by: tunnell <tunnell@users.noreply.github.com>
Assisted-by: Claude Code (Opus 4.7)
2026-04-21 03:11:44 -05:00
bunnie
2ec739e54e fix orientation flipping during QR scan mode
turns out you don't just flip everything all the time! when
you flip the board upside down, the camera also flips. so then
you end up with a flipped image which is super-confusing.

however, the overlay text needs to flip. so this adds the
cases to handle that.
2026-04-21 01:21:51 +08:00
bunnie
0d2c7233d6 improve stability of USB when not plugged in
don't try to use the USB stack when nothing is plugged in
2026-04-21 01:21:13 +08:00
tunnell
e19ba9973f Use constant-time compare for GAM auth tokens
GAM auth tokens are 128-bit capabilities bound to their owning
process. The previous `==` compare on `[u32; 4]` short-circuits
on the first mismatching element, leaking prefix-length information
through timing.

Switch to `subtle::ConstantTimeEq`, which is already present in
the dep graph via ed25519-dalek and used elsewhere in the crypto
stack.

Signed-off-by: tunnell <tunnell@users.noreply.github.com>
Assisted-by: Claude Code (Opus 4.7)
2026-04-20 08:15:32 -05:00
bunnie
0c23c5e5bc add eternal-scan feature
this is useful for aligning factory test features
2026-04-13 00:03:42 +08:00
bunnie
6eecc52961 rollover handling in rtc wake alarm routine 2026-04-07 18:02:47 +08:00
bunnie
b8194e7c83 add RTC wakeup alarm handling
a bit convoluted:

- RTC wakeup goes through the "always on" subsystem
- keyboard routes through "always on" because the system can wake
on keyboard press
- thus the keyboard mechanism is abused/re-used to pass wakeup
events throughout the system.

Other notes - a wakeup alarm sent within 2 seconds of the current
time will trigger an assert. It's not recommended to use the RTC
for very near-term wakeups because if you e.g. hit swap or have
something else going on that's very "busy" (like QR code scanning)
the system might not get to waking things up in time.

Some code can be written to handle this and set up a degenerate
wakeup, maybe, but for now, I am leaving it as an assert because
I want to remind myself that there is an edge case here.
2026-04-07 15:57:19 +08:00
bunnie
4637fc7037 fix hosted mode 2026-04-07 00:56:57 +08:00
bunnie
70543731d1 add keyboard remapping that also tracks screen flipping 2026-04-07 00:37:15 +08:00
bunnie
1ce29e80fa add ability to flip rendering of sh1107 screen
this allows a device to be flipped upside-down and have
the screen track orientation
2026-04-07 00:36:39 +08:00
bunnie
6b0972a551 add code to implement irq trigger edge updating
also improve comments so I can remember how this actually works.
2026-04-07 00:35:34 +08:00
bunnie
30ca5dec71 adjust disk space usage for lite platform 2026-04-05 23:23:11 +08:00
bunnie
6e528f9678 fix regression on wfi vs bio setting 2026-04-05 23:22:45 +08:00
bunnie
44a53e590a move error definition to api crate, fixes hosted ci 2026-04-04 00:08:39 +08:00
bunnie
f2371f49eb add implementation for owc frobbing
by default the knob is not frobbable because frobbing the
knob can lead to potential griefing

more specifically the owc (one way counters) control some
security state. they are all designed to "fail on increment"
but just giving someone a button to increment random counters
gives a DoS vector. So unless you need to open the API it's
recommended to not have it activated.
2026-04-03 23:43:57 +08:00
bunnie
979d00dedf attempt to debug the wfi/bio clock conundrum
the commited code works, but there's something weird going on
with the clock setting math that indicates i'm not understanding
exactly what's going on. Need to move on and solve other problems,
but this will be an outstanding issue.
2026-04-03 23:00:35 +08:00
bunnie
2f9a7cd723 reset the udma block for the camera on every frame
improves stability across multiple captures
2026-04-03 01:53:13 +08:00
bunnie
4f9584ec9e cleanup qr code capture behavior
goes to blank screen after capture instead of previous ui state
also cleans up transitions between camera and run mode
2026-04-02 03:47:46 +08:00
bunnie
ee9bec19a1 fix key mappings and delay times 2026-04-02 03:47:15 +08:00
bunnie
e46a65e21f fix precursor targets 2026-03-31 01:51:56 +08:00
bunnie
e3408f5346 fix more emulated modes 2026-03-31 01:18:53 +08:00
bunnie
5d0c490a55 fix other hosted mode 2026-03-31 00:54:57 +08:00
bunnie
97efc2a162 fix hosted mode 2026-03-31 00:22:36 +08:00
bunnie
3993a0dc88 'fix' for timeout issue on SPIM during display writes
occassionally the hardware block just goes unresponsive. the
'fix' is to reset it, and then re-initialize the interface.

I suspect this has to do with the UDMA silently dropping transactions
when there's too much contention on the bus.
2026-03-30 23:34:25 +08:00
bunnie
b7826c2ebc flesh out backup register interfaces
- adds API for retrieving & setting the ephemeral key. This is
a key that is erased by a power-on reset, but is preserved between
deep-sleep sleep/wake cycles. It is intended for securing a lock
PIN, and can be up to 192 bits long.

- adds API for checking the warm boot flags, which are also stored
in the backup registers.
2026-03-29 18:04:09 +08:00
bunnie
527f256ec1 add API call to determine if in developer mode 2026-03-26 02:12:11 +08:00
bunnie
73021cb2ed add brightness setting function 2026-03-23 18:33:43 +08:00
bunnie
d4933e96a4 make baochip boot logo drawing optional in the kernel driver 2026-03-23 17:23:58 +08:00
bunnie
4f37317427 add bitmap rendering API for baosec targets 2026-03-23 14:52:00 +08:00
bunnie
3035143172 fix hosted mode 2026-03-06 21:26:22 +08:00
bunnie
017ab2a5fd report QR code when test code is scanned 2026-03-06 19:18:05 +08:00
bunnie
3ac87613d1 add UI feedback on baosec targets for PDDB init
useful for MP flow progress monitoring
2026-03-06 19:17:43 +08:00
bunnie
90c3f095ec add support for more keyboard input configs on baosec 2026-02-26 18:29:47 +08:00
bunnie
663139b607 split ADC into its own service thread
this allows the ADC to depend on core HAL services. It's structured
so that other 'misc' services would go into the "other" module,
just to keep the overhead of threads/services down a bit. Up until
this point all the other services mapped to existing/other
server names, like the RTC, susres, etc.
2026-02-24 01:33:25 +08:00
bunnie
18e72a970c add draft ADC support to hal service 2026-02-23 18:53:37 +08:00
bunnie
5a8d5a8de5 fix allocation bug for ifram 2026-02-23 18:15:14 +08:00
bunnie
40a275232c udpate tomls to explicitly enumerate features used 2026-02-20 23:54:07 +08:00
bunnie
cdf489d2b1 fix std-implementation for code size/offset/padding specification 2026-02-20 21:02:19 +08:00
bunnie
b991a54b52 add debug call for bio 2026-02-20 02:09:09 +08:00
bunnie
90b1f2f345 cleanup warnings 2026-02-09 02:48:31 +08:00
bunnie
8a2118b8f6 fix bug in root-keys that prevents bbram provisionig from happening
this bug highlights a fairly big weakness in the rkyv-based
serialization in that there is no inherent type checking of
the sender to the receiver. In this case, a typo of just one
character lead to a regression which, probably in an earlier
version of rkyv, was not a problem because the ordering of
elements was different. In this case it lead to incorrect behavior,
which upon debugging lead to panics because we were interpreting
the data incorrectly after it was unpacked.

This is probably an issue to be worried about in the longer-term,
and it leaves me with a lingering question of what attack
surface exists through the deserialization mechanism for
attackers who send malformed buffers as messages.
2026-02-09 02:17:31 +08:00
bunnie
f774522e62
remove testing API that never actually worked
there was a thought to speed up CI testing by introducing a
"cheater" routine that allowed us to re-initialize the keystore.
Turns out this doesn't work because it's not possible to fully
revert a system out of developer mode - because, in my infinite
previous-self wisdom, I realized that this is something someone
might try and there is a one-way counter that logs if the
system has *ever* been in developer mode, and that has no
known bypass.

This means that even if you could repopulate the keystore, it
would be cleared on the next boot by merit of the developer
flag being set.

So - we did it the hard way, every single test we have to
re-image the system from scratch. C'est la vie.

Removing this patch eliminates a code path that could later be
abused for unforeseen ill purposes.
2026-02-08 12:55:10 +08:00
bunnie
f9ef3217f0 add test command to overwrite keys 2026-02-04 15:58:20 +08:00
bunnie
c1ae6b2e5f add more dependencies to propagate key-testing flag 2026-02-04 15:57:59 +08:00
bunnie
0aeb9daa37 add a method to force-rewrite keys
This is useful in CI to test various stages of dev-key signed
image vs secret key erase without having to go through a complete
suicide cycle on the chip.

This command is gated behind a feature flag that would only be
invoked for beta testing. I think it's still safe to have even
as a beta-signed feature because the worst you can do with this
is replace existing keys - so it could be used to DoS/brick, but not
leak secrets.
2026-02-04 15:55:34 +08:00
bunnie
e349703fd5 automatically adjust BIO clock dividers on sleep
this allows the BIO to continue to function even though
the CPU core is in WFI. It isn't running at full speed, but
for some applications (such as WS2812 LED driving) the lower
speed is perfectly adequate and you save a lot of power
by turning off the CPU core.
2026-02-02 23:37:48 +08:00
bunnie
7d7ddae73e pin the irq handler object for the iox banks 2026-02-02 01:29:19 +08:00
bunnie
0f19750831 remove autodrop on other hal service items 2026-02-02 01:28:32 +08:00
bunnie
2005293e11 remove autodrop on trng service 2026-02-02 01:28:15 +08:00
bunnie
70639470bf cleanup an unecessary static mut and make it an Atomic 2026-02-02 01:26:42 +08:00
bunnie
54c070b433 remove old comment that is no longer valid 2026-02-02 01:26:25 +08:00
bunnie
b1121daef6 security-relevant: modify collateral test to not panic on failure
The panic on failure causes incompatibility with older boot0's
that don't set up the collateral area correctly.

Make the failure more graceful, but we also promise ourselves
to add CI tests to check for this explicitly, because we can't
just rely on panics to enforce behavior.
2026-01-30 04:01:09 +08:00
bunnie
3f869ca389 bump utralib version for publication 2026-01-29 19:12:00 +08:00
bunnie
ca630c6472 add maintenance comment on USB driver 2026-01-29 18:59:29 +08:00
bunnie
88ccc7e042 fully specify the bao1x target on blitstr2 for bao1x builds 2026-01-29 18:58:55 +08:00
bunnie
b0adea6c6a fix hosted mode regression 2026-01-29 00:28:42 +08:00
bunnie
2805f7a14a fix deadlock on boot issue
io need to be active early on to get clocks to function on baosec
putting that in its own thread fixes the issue.
2026-01-29 00:09:10 +08:00
bunnie
1d3f264be4 fix hosted mode regression 2026-01-29 00:08:56 +08:00
bunnie
b2cf705c59 bump ticktimer version to drop susres dependency 2026-01-28 20:53:57 +08:00
bunnie
dc56fc7a09 fix precursor builds 2026-01-28 15:44:18 +08:00
bunnie
3bc1c23425 add missing feature flag 2026-01-28 15:31:22 +08:00
bunnie
c2640458f5 fix hosted mode and warnings 2026-01-28 15:21:39 +08:00
bunnie
3b0edbb442 add some more test routines - some of these will be culled shortly 2026-01-28 15:09:52 +08:00
bunnie
22c8ce4308 fix dangling susres api migration 2026-01-28 15:09:08 +08:00
bunnie
3e4cbe441f add apis to access clock state via the susres manager
this requires a PlatformSpecific extension to the
susres API.

In addition to that, a DeepSleep op is added so we're
no longer overloading the meaning of the SuspendRequest
operation.
2026-01-28 15:07:22 +08:00
bunnie
6ae6b22ff3 more cleanup from susres localization 2026-01-28 15:03:40 +08:00
bunnie
45632385d5 downgrade susres to a common api
doesn't make sense to push this to crates.io because it's not
common across all platforms, I think.
2026-01-28 00:36:05 +08:00
bunnie
5a3319ae57 optimize camera frame rate
it's now around 30Hz.
2026-01-27 22:06:18 +08:00
bunnie
8b06b212bb add oem-baosec-lite support to usb driver
there is currently no method to detect USB power connect
in this board. Considering adding a simple circuit to enable
this. This design is not meant to be battery powered but
it is possible that an aftermarket modification would introduce
a battery, in which case, vbus detection does become A Thing.
2026-01-24 23:54:21 +08:00
bunnie
bcf73733c3 oem-baosec-lite support in hal-service
this one is a little bit tricky. The lite version of baosec
forgoes the off-chip TRNG to save cost. Thus, there is an
"anti-feature" in the configuration, and so in this case
the dabao version of the server has to be selected despite
the baosec flag.
2026-01-24 23:52:37 +08:00
bunnie
e329ebeb1d oem-baosec-lite support to bao-video
remap pwm lock output. This reduces power consumption in
camera mode by removing an I/O conflict with the keyboard
scanner.
2026-01-24 23:51:22 +08:00
bunnie
10ef6bda02
add information about root key seed to regular sign_image
Three pieces of inforation are leaked about the root seed:

- whether or not the first 64 bits are all 0
- whether or not the first 64 bits are all 3's
- whether if none of the above are true

It's fairly indirect leakage, and I think it is a relatively
minor price to pay for the confidence it gives that some underlying
library behavior didn't accidentally lead to faulty keygen.

Note that these 64 bits are 64 bits out of tens of thousands
of bits that go into the keygen, because the nuisance keys
and the chaff key are all blended together to create the final
derived root key. Thus the security margin on the key is quite
large and even a full disclosure of these 64-bits is a minor
impact on overall security.
2026-01-18 20:52:51 +08:00
bunnie
cc00201774 add collateral erasure check to keystore
in no case should any baochip firmware be holding secret
collateral used by third party firmwares, thus a non-erased
state is a panic
2026-01-18 02:58:32 +08:00
bunnie
f6c7d9c89e enable USB console by default
this helps with production & ci
2026-01-17 16:23:53 +08:00
bunnie
4c3d2f677c fix hosted mode 2026-01-16 17:52:00 +08:00
bunnie
5a73d17a0a handle the no-arg case correctly 2026-01-16 17:40:17 +08:00
bunnie
2a7cffb86e add bootwait command line API to test
also refactor the test argument parsing so that i can use the
more modern version of the api
2026-01-16 17:35:41 +08:00
bunnie
2b19c4bd21 add a command for setting the bootwait flag
The keystore is a sensitive server, and the one-way counters
have security-critical propoerties. So instead of revealing a
generic one-way counter API to the world, specific, limited
requests to do things are going to be created for the keystore.

In this case, the bootwait flag would be convenient to be able
to set from userspace. The motivation for this requirement is
actually continuous integration - a normal human could just
hold down a button while turning on power, but in CI, I don't
have a robotic thumb (or an alternative electrical signal) to
simulate the nerve-pinch on boot.

Previously I was "cheating" and simulating this by nuking the
loader, which effectively forces a bootwait.

However, in production, this is *not possible* because the JTAG
port is sealed off. Thus, this may be the first of a series
of small PRs to incorporate CI tooling that clears up tech
debt like nuking the loader to get into boot wait.
2026-01-16 17:32:12 +08:00
bunnie
533607b9ec gdb-stub: disable userspace console
The serial port going to the userspace console is disabled to allow
for gdb access to the kernel.
2026-01-14 03:13:39 +08:00
bunnie
fba011db3a commit version bump so that we may publish our packages 2026-01-08 03:04:19 +08:00
bunnie
42e96d723a hook the debug info output in the "test" command
this command and its dependencies will probably be stripped out
or modified for production, so i think this is a good place to hang
these.
2026-01-08 03:02:04 +08:00
bunnie
e27b30d818 fix edge case in swapper
A particular edge case was finally reproduced & fixed. The edge
case occurs when a swap is triggered while handling an interrupt,
and an OOM happens during the context switch to/from the interrupt.

The background on this is that the hardware drivers for the swapper,
as well as much of the logical heavy lifting on e.g. what pages to
swap out, live in a userspace process known as xous-swapper.

The userspace process has to run free of interrupts - when you're
OOM'd or trying to swap, interrupts should not happen. The way to
do this in Xous is to run that code inside an interurpt context,
as interrupts are not interruptable.

Thus, the xous swapper userspace consists of two halves: a regular
userspace stub that provides some amount of control/debug over
the swapper, and a handler that gets run in a "pseudo-interrupt"
context.

Like all other interrupts, the handler "borrows" the stack of the currently
running thread - interrupt contexts don't have an explicit stack, they
just grow on top of the current stack of the current thread in the
current process at the time of the interrupt.

This is broken because when you have a swap event inside an interrupt
context, there are conditions where you might not have a stack. What
was in fact happening is that the stack pointer was being orphaned
as being the "exception stack" - which is a temporary location,
only accessible by the kernel, used for storing all the registers
going into and out of exceptions. Since no stack pointer is set up,
the moment the userspace starts to run and spill its registers to
stack, the system trips a "nested swap" event, which leads to a panic
because this is not allowed.

The fix to this is to devote two hard-wired pages, mapped in the
loader, to be the swapper's private stack area. Instead of behaving
like an interrupt and borrowing the caller's stack, the swapper instead
goes back to its hard-wired stack to do all the processing.

This should also improve the performance of swap handling in some
cases because we can now guarantee the stack is always in-memory
and doesn't need to be swapped in during swap processing.

One complication is that the "reserved memory" that the swapper
puts aside in case it *does* need to allocate extra pages during
handling can't be freed at the top of the OOM routine. Thus if
the swapper ends up using new pages, we should expect yet another
crash.

However, in practice, this doesn't seem to happen, because the
cases in which we hit needing a new page are typically related
to cases early on in boot where superpages/new page table entries
are being allocated, or if we're growing the heap that passes
data back and forth between swap and userspace. Because this call
triggers in such a specific condition, I hypothesize that actually
in practice it should generally be able to run without allocations.

One case where maybe it could hit an allocation is if the swapper
hit, for the first time, in an interrupt handler that was swapped
out, and never hit before, and the interrupt handler needed a
superpage to be allocated, thus triggering an allocation request
for a new PTE or two. I think this is probably pretty unlikely, but
if we do hit it, the fix would be to, for the purposes of targets
with "swap" enabled, to mark unmap_memory and map_memory to be
legal syscalls in interrupt contexts. These are normally illegal
because interrupts should always return - and these calls could
potentially fail or block. However, I think in this case it's
a trade-off between "we should be failing if we run out of memory"
and that failure being a potentially incorrect behavior that is guarded
against by a runtime check to discourage such calls.
2026-01-08 02:08:03 +08:00
bunnie
415fa27758 add a pair of swap test servers
these do nothing but allocate a bunch of memory and revisit the
allocated memory, to exercise swapping.
2026-01-08 02:02:23 +08:00
bunnie
cabfbde7f2 cleanup false positive warnings in latest rustc 2026-01-06 13:41:54 +08:00
bunnie
e676ccbed8 cleanup dead code warnings found by latest rustc 2026-01-06 13:41:04 +08:00
bunnie
e675ca231c cleanup warning 2026-01-05 22:02:54 +08:00
bunnie
92180af349 move the AV trng into the BIO subsystem
this also removes the board-specific hacks that track BIO usage
for the TRNG as set up by the loader
2026-01-05 21:54:30 +08:00
bunnie
bf78ba150f debug and implementation for composable I/O 2026-01-05 08:57:57 +08:00
bunnie
5f2cbd4ca4 quiet down debug 2026-01-02 12:26:27 +08:00
bunnie
5971187a62 add BIO services for resource tracking 2026-01-02 11:26:47 +08:00
bunnie
c7f835a93f fix xous implementation 2025-12-31 23:36:00 +08:00
bunnie
58df51ae43 bio implementation in bao1x-hal-service 2025-12-26 21:03:40 +08:00
bunnie
1c0d8eea17 update the xous API to handle bitmask-based setting of the BIO pins 2025-12-26 20:18:49 +08:00