upgrade loader to use NTO write method for kernel

~~*super fast*~~
This commit is contained in:
bunnie 2025-08-10 15:49:18 +08:00
parent 7d89deacb4
commit 2f4cf44503

View file

@ -5,10 +5,6 @@ use cramium_api::*;
use cramium_hal::board::SPIM_FLASH_IFRAM_ADDR;
use cramium_hal::ifram::IframRange;
use cramium_hal::iox::Iox;
use cramium_hal::mbox::{
MBOX_PROTOCOL_REV, Mbox, MboxError, MboxToCm7Pkt, PAYLOAD_LEN_WORDS, RERAM_PAGE_SIZE_BYTES, ToCm7Op,
ToRvOp,
};
use cramium_hal::sh1107::{Mono, Oled128x128};
use cramium_hal::udma;
use cramium_hal::udma::*;
@ -23,7 +19,6 @@ use crate::SIGBLOCK_SIZE;
use crate::platform::cramium::gfx;
use crate::platform::cramium::sha512_digest::Sha512Prehash;
use crate::platform::cramium::usb::{self, SliceCursor, disable_all_irqs};
use crate::platform::delay;
// TODO:
// - Port unicode font drawing into loader
@ -161,6 +156,7 @@ pub fn process_update(perclk: u32) {
// Below is all unsafe because USB is global mutable state
unsafe {
if let Some(ref mut usb_ref) = crate::platform::cramium::usb::USB {
crate::println!("inside update");
let usb = &mut *core::ptr::addr_of_mut!(*usb_ref);
usb.reset();
let mut poweron = 0;
@ -293,7 +289,9 @@ pub fn process_update(perclk: u32) {
return;
}
crate::println!("Kernel image is good!");
let mut mbox = cramium_hal::mbox::Mbox::new();
let mut rram = cramium_hal::rram::Reram::new();
// concatenate this with e.g. .min(0x2000) to shorten the run, if needed
let test_len = sig_rec.signed_len as usize;
let check_slice = core::slice::from_raw_parts(
@ -308,47 +306,17 @@ pub fn process_update(perclk: u32) {
progress_bar(&mut sh1107, 0);
// nearest event multiple of RERAM_PAGE_SIZE_BYTES that fits into
// our available payload length
const BLOCKLEN_BYTES: usize = (PAYLOAD_LEN_WORDS * size_of::<u32>()
/ RERAM_PAGE_SIZE_BYTES)
* RERAM_PAGE_SIZE_BYTES;
// choose a constant that balances screen updates versus performance
const BLOCKLEN_BYTES: usize = 4096 * 16;
let total_len = SIGBLOCK_SIZE + test_len;
for (i, byte_chunk) in disk_access
[k_start..k_start + SIGBLOCK_SIZE + test_len as usize]
.chunks(BLOCKLEN_BYTES)
.enumerate()
{
// +2 words are for the address / length fields required by the
// protocol
let mut buffer = [0u32; BLOCKLEN_BYTES / size_of::<u32>() + 2];
for (src, dst) in byte_chunk.chunks(4).zip(buffer[2..].iter_mut()) {
*dst = u32::from_le_bytes(src.try_into().unwrap());
}
// now fill in the metadata for the protocol
buffer[0] =
(KERNEL_BASE_ADDR + i * BLOCKLEN_BYTES + utralib::HW_RERAM_MEM)
as u32;
buffer[1] = BLOCKLEN_BYTES as u32;
if i % 8 == 0 {
crate::println!("{:x}: {:x?}", buffer[0], &buffer[2..6]);
progress_bar(&mut sh1107, i * BLOCKLEN_BYTES * 100 / total_len);
}
match write_rram(&mut mbox, &buffer) {
Ok(len) => {
if len != BLOCKLEN_BYTES {
crate::println!(
"write length mismatch, got {} expected {} words",
len,
BLOCKLEN_BYTES
);
}
}
Err(e) => {
crate::println!("Write failed with {:?}", e);
break;
}
};
rram.write_slice(KERNEL_BASE_ADDR + i * BLOCKLEN_BYTES, &byte_chunk);
crate::println!("{:x}: {:x?}", i * BLOCKLEN_BYTES, &byte_chunk[..4]);
progress_bar(&mut sh1107, i * BLOCKLEN_BYTES * 100 / total_len);
}
progress_bar(&mut sh1107, 100);
cramium_hal::cache_flush();
@ -550,64 +518,3 @@ fn progress_bar(sh1107: &mut Oled128x128<'_>, percentage: usize) {
sh1107.draw();
sh1107.clear();
}
pub fn write_rram(mbox: &mut Mbox, data: &[u32]) -> Result<usize, MboxError> {
let write_pkt = MboxToCm7Pkt { version: MBOX_PROTOCOL_REV, opcode: ToCm7Op::FlashWrite, data };
match mbox.try_send(write_pkt) {
Ok(_) => {
crate::platform::delay(10);
// crate::println!("flash write sent OK");
let mut timeout = 0;
while mbox.poll_not_ready() {
timeout += 1;
if timeout > 1000 {
crate::println!("flash write timeout");
return Err(MboxError::NotReady);
}
crate::platform::delay(2);
}
// now receive the packet
let mut rx_data = [0u32; 16];
timeout = 0;
while !mbox.poll_rx_available() {
timeout += 1;
if timeout > 1000 {
crate::println!("flash handshake timeout");
return Err(MboxError::NotReady);
}
crate::platform::delay(2);
}
match mbox.try_rx(&mut rx_data) {
Ok(rx_pkt) => {
crate::platform::delay(10);
if rx_pkt.version != MBOX_PROTOCOL_REV {
crate::println!("Version mismatch {} != {}", rx_pkt.version, MBOX_PROTOCOL_REV);
}
if rx_pkt.opcode != ToRvOp::RetFlashWrite {
crate::println!(
"Opcode mismatch {} != {}",
rx_pkt.opcode as u16,
ToRvOp::RetFlashWrite as u16
);
}
if rx_pkt.len != 1 {
crate::println!("Expected length mismatch {} != {}", rx_pkt.len, 1);
} else {
// crate::println!("Wrote {} bytes", rx_data[0]);
}
Ok(rx_data[0] as usize)
}
Err(e) => {
crate::platform::delay(10);
crate::println!("Error while deserializing: {:?}\n", e);
Err(e)
}
}
}
Err(e) => {
delay(10);
crate::println!("Flash write send error: {:?}", e);
Err(e)
}
}
}