add uf2 over serial protocol for writing e.g. baremetal images

This commit is contained in:
bunnie 2025-09-29 18:40:57 +08:00
parent fa875158e3
commit a511e3c591
3 changed files with 113 additions and 2 deletions

View file

@ -7,6 +7,8 @@ mod asm;
mod platform;
mod repl;
mod secboot;
mod uf2;
use alloc::collections::VecDeque;
use core::{cell::RefCell, sync::atomic::Ordering};

View file

@ -19,10 +19,11 @@ impl Error {
pub struct Repl {
cmdline: String,
do_cmd: bool,
local_echo: bool,
}
impl Repl {
pub fn new() -> Self { Self { cmdline: String::new(), do_cmd: false } }
pub fn new() -> Self { Self { cmdline: String::new(), do_cmd: false, local_echo: true } }
#[allow(dead_code)]
pub fn init_cmd(&mut self, cmd: &str) {
@ -46,7 +47,9 @@ impl Repl {
// everything else
match char::from_u32(c as u32) {
Some(c) => {
crate::print!("{}", c);
if self.local_echo {
crate::print!("{}", c);
}
self.cmdline.push(c);
}
None => {
@ -73,6 +76,51 @@ impl Repl {
"boot" => {
crate::secboot::boot_or_die();
}
"uf2" => {
use base64::{Engine as _, engine::general_purpose};
if args.len() != 1 {
crate::println_d!("u2f query malformed");
return Err(Error::help("uf2 [base64 data]"));
}
match general_purpose::STANDARD.decode(&args[0]) {
Ok(uf2_data) => {
if let Some(record) = crate::uf2::Uf2Block::from_bytes(&uf2_data) {
if record.address() as usize >= bao1x_api::BAREMETAL_START
&& (record.address() as usize)
< utralib::HW_RERAM_MEM + bao1x_api::RRAM_STORAGE_LEN
&& record.family() == bao1x_api::BAOCHIP_1X_UF2_FAMILY
{
let mut rram = bao1x_hal::rram::Reram::new();
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());
} else {
crate::println!(
"Invalid write address {:x}, block ignored!",
record.address()
);
}
} else {
crate::println_d!("invalid u2f data");
}
}
Err(e) => {
crate::println_d!("Decode error {:?}", e);
return Err(Error::help("Corrupt base64"));
}
}
crate::usb::flush();
}
"localecho" => {
if args.len() != 1 {
return Err(Error::help("localecho [on | off]"));
}
if args[0] == "on" {
self.local_echo = true;
} else {
self.local_echo = false;
}
}
"echo" => {
for word in args {
crate::print!("{} ", word);

View file

@ -0,0 +1,61 @@
use core::convert::TryInto;
#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub struct Uf2Block {
pub magic_start0: u32, // should be 0x0A324655
pub magic_start1: u32, // should be 0x9E5D5157
pub flags: u32,
pub target_addr: u32,
pub payload_size: u32,
pub block_no: u32,
pub num_blocks: u32,
pub file_size_family_id: u32, // may be zero
pub data: [u8; 476], // payload data
pub magic_end: u32, // should be 0x0AB16F30
}
impl Uf2Block {
pub fn from_bytes(bytes: &[u8]) -> Option<Self> {
if bytes.len() != 512 {
return None;
}
let magic_start0 = u32::from_le_bytes(bytes[0..4].try_into().unwrap());
let magic_start1 = u32::from_le_bytes(bytes[4..8].try_into().unwrap());
let flags = u32::from_le_bytes(bytes[8..12].try_into().unwrap());
let target_addr = u32::from_le_bytes(bytes[12..16].try_into().unwrap());
let payload_size = u32::from_le_bytes(bytes[16..20].try_into().unwrap());
let block_no = u32::from_le_bytes(bytes[20..24].try_into().unwrap());
let num_blocks = u32::from_le_bytes(bytes[24..28].try_into().unwrap());
let file_size_family_id = u32::from_le_bytes(bytes[28..32].try_into().unwrap());
let mut data = [0u8; 476];
data.copy_from_slice(&bytes[32..508]);
let magic_end = u32::from_le_bytes(bytes[508..512].try_into().unwrap());
if magic_start0 != 0x0A32_4655 || magic_start1 != 0x9E5D_5157 || magic_end != 0x0AB16F30 {
None
} else {
Some(Uf2Block {
magic_start0,
magic_start1,
flags,
target_addr,
payload_size,
block_no,
num_blocks,
file_size_family_id,
data,
magic_end,
})
}
}
pub fn data(&self) -> &[u8] { &self.data[..self.payload_size as usize] }
pub fn family(&self) -> u32 { self.file_size_family_id }
pub fn address(&self) -> u32 { self.target_addr }
}