u2f serial upload now working

works over both physical serial port and USB serial port.
This commit is contained in:
bunnie 2025-09-29 21:58:01 +08:00
parent f8e124f6f9
commit 01cf1df330
5 changed files with 66 additions and 17 deletions

View file

@ -31,9 +31,10 @@ bao1x = [
"utralib/bao1x",
"linked_list_allocator",
"bao1x-hal/loader-baosec",
# "bao1x-hal/debug-print-usb",
"bao1x-hal/debug-print-usb",
"bao1x-api",
]
unsafe-debug = []
verbose-debug = ["bao1x-hal/debug-print-usb", "bao1x-hal/verbose-debug"]
verilator-only = []

View file

@ -67,7 +67,11 @@ pub unsafe extern "C" fn rust_entry() -> ! {
// "flash" SE0 for 1 second so as to emulate a clean re-plug event, allowing the
// stack to enumerate from a coherent state. Up until now, any attempt of the
// host to talk to us just gave us nonsense
crate::println!("No valid boot image found; enabling USB...");
if current_key.is_none() {
crate::println!("No valid boot image found; enabling USB...");
} else {
crate::println!("Boot bypassed with keypress: {:?}", current_key);
}
let (se0_port, se0_pin) = match board_type {
BoardTypeCoding::Baosec => bao1x_hal::board::setup_usb_pins(&iox),
_ => crate::platform::setup_dabao_se0_pin(&iox),

View file

@ -94,6 +94,7 @@ impl Repl {
let offset = record.address() as usize - utralib::HW_RERAM_MEM;
rram.write_slice(offset, record.data());
crate::println!("Wrote {} to 0x{:x}", record.data().len(), record.address());
crate::println_d!("{:x}", record.address());
} else {
crate::println!(
"Invalid write address {:x}, block ignored!",
@ -121,6 +122,36 @@ impl Repl {
self.local_echo = false;
}
}
#[cfg(feature = "unsafe-debug")]
"peek" => {
const COLUMNS: usize = 4;
if args.len() == 1 || args.len() == 2 {
let addr = usize::from_str_radix(&args[0], 16)
.map_err(|_| Error::help("Peek address is in hex"))?;
if addr >= utralib::HW_RERAM_MEM + bao1x_api::RRAM_STORAGE_LEN
&& addr < utralib::HW_RERAM_MEM + utralib::HW_RERAM_MEM_LEN
{
return Err(Error::help("Peek disallowed for security-related sectors"));
}
let count = if args.len() == 2 {
if let Ok(count) = u32::from_str_radix(&args[1], 10) { count } else { 1 }
} else {
1
};
// safety: it's not safe to do this, the user peeks at their own risk
let peek = unsafe { core::slice::from_raw_parts(addr as *const u32, count as usize) };
for (i, &d) in peek.iter().enumerate() {
if (i % COLUMNS) == 0 {
crate::print!("\n\r{:08x}: ", addr + i * size_of::<u32>());
}
crate::print!("{:08x} ", d);
}
crate::println!("");
} else {
return Err(Error::help("Help: peek <addr> [count], addr is in hex, count in decimal"));
}
}
"echo" => {
for word in args {
crate::print!("{} ", word);

View file

@ -3,9 +3,13 @@ use bao1x_hal::board::KeyPress;
pub fn try_boot<T: IoSetup + IoGpio>(board_type: &BoardTypeCoding, iox: &T) -> Option<KeyPress> {
if let Some(key) = crate::platform::get_key(board_type, iox) {
// skip boot if a key is pressed; record what key it is so we know to check that it has
// become *unpressed* before looking for a new press
return Some(key);
// TODO: on baosec v2, we should not get Invalid keys. However, as we wait for the new
// boards to come in this will be a thing.
if key != KeyPress::Invalid {
// skip boot if a key is pressed; record what key it is so we know to check that it has
// become *unpressed* before looking for a new press
return Some(key);
}
}
let one_way = bao1x_hal::acram::OneWayCounter::new();
@ -18,13 +22,15 @@ pub fn try_boot<T: IoSetup + IoGpio>(board_type: &BoardTypeCoding, iox: &T) -> O
seal_boot1_keys();
// loader is at the same offset as baremetal. Accept either as valid boot.
// This diverges if the signature check is successful
bao1x_hal::sigcheck::validate_image(
match bao1x_hal::sigcheck::validate_image(
bao1x_api::LOADER_START as *const u32,
bao1x_api::BOOT1_START as *const u32,
bao1x_api::BOOT1_REVOCATION_OFFSET,
true,
)
.ok();
) {
Ok(k) => crate::println!("**should be unreachable** Booted with key {}", k),
Err(e) => crate::println!("Image did not validate: {:?}", e),
}
crate::println!("No valid loader or baremetal image found. Halting!");
bao1x_hal::sigcheck::die_no_std();
}
@ -45,13 +51,15 @@ pub fn boot_or_die() -> ! {
seal_boot1_keys();
// loader is at the same offset as baremetal. Accept either as valid boot.
// This diverges if the signature check is successful
bao1x_hal::sigcheck::validate_image(
match bao1x_hal::sigcheck::validate_image(
bao1x_api::LOADER_START as *const u32,
bao1x_api::BOOT1_START as *const u32,
bao1x_api::BOOT1_REVOCATION_OFFSET,
true,
)
.ok();
) {
Ok(k) => crate::println!("**should be unreachable** Booted with key {}", k),
Err(e) => crate::println!("Image did not validate: {:?}", e),
}
crate::println!("No valid loader or baremetal image found. Halting!");
bao1x_hal::sigcheck::die_no_std();
}

View file

@ -115,6 +115,7 @@ def wait_for_response(
lines = text.split('\n')
for line in lines:
# print(line)
# Look for pattern: "Wrote 256 to 0x60070a00"
match = re.search(r'Wrote\s+(\d+)\s+to\s+(0x[0-9a-fA-F]+)', line)
if match:
@ -263,14 +264,18 @@ def send_uf2_file(
# Track failures
failed_blocks: Dict[int, TransferResult] = {}
total_retries = 0
successful_blocks = 0
echo_off = 'localecho off\r'
try:
with uf2_path.open('rb') as f:
# Send twice because the very first message is sometimes messed up in serial protocol
ser.write("localecho off\r".encode('utf-8'))
ser.flush()
ser.write("localecho off\r".encode('utf-8'))
ser.flush()
# Also send with individual flush because on the actual serial protocol, we need to slow
# down the protocol until echo is actually turned off due to the lag in processing local echo.
for _i in range(2):
for c in echo_off:
ser.write(c.encode('utf-8'))
ser.flush()
time.sleep(0.1)
for block_idx in range(total_blocks):
@ -308,6 +313,7 @@ def send_uf2_file(
if result.attempts > 1:
total_retries += result.attempts - 1
status = f"OK (Retries: {total_retries})"
successful_blocks += 1
pbar.update(block_idx + 1, status=status)
@ -323,7 +329,6 @@ def send_uf2_file(
print(f"Transfer Summary")
print(f"{'='*60}")
successful_blocks = total_blocks - len(failed_blocks)
success_rate = (successful_blocks / total_blocks * 100) if total_blocks > 0 else 0
print(f"Total blocks: {total_blocks}")