mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-15 08:30:02 +00:00
debugging some timing issues
This commit is contained in:
parent
dd3db84859
commit
c483581716
2 changed files with 210 additions and 55 deletions
|
|
@ -1,5 +1,5 @@
|
|||
use crate::{ShellCmdApi, CommonEnv};
|
||||
use xous_ipc::String;
|
||||
use xous_ipc::{String, Buffer};
|
||||
|
||||
use core::sync::atomic::{AtomicU32, Ordering};
|
||||
static CB_ID: AtomicU32 = AtomicU32::new(0);
|
||||
|
|
@ -8,9 +8,11 @@ use num_traits::*;
|
|||
use engine_sha512::*;
|
||||
use digest::Digest;
|
||||
|
||||
use core::fmt::Write;
|
||||
|
||||
#[derive(num_derive::FromPrimitive, num_derive::ToPrimitive, Debug)]
|
||||
pub(crate) enum UpdateOp {
|
||||
//UpdateGateware,
|
||||
UpdateGateware,
|
||||
UpdateFirmware,
|
||||
//UpdateWf200,
|
||||
//ResetEc,
|
||||
|
|
@ -20,20 +22,29 @@ pub(crate) enum UpdateOp {
|
|||
#[derive(num_derive::FromPrimitive, num_derive::ToPrimitive, Debug)]
|
||||
pub(crate) enum UpdateResult {
|
||||
PackageValid,
|
||||
EraseProgress,
|
||||
ProgramProgress,
|
||||
EraseDone,
|
||||
ProgramDone,
|
||||
Abort,
|
||||
}
|
||||
#[derive(PartialEq, Eq)]
|
||||
enum PackageType {
|
||||
Ec,
|
||||
Wf200
|
||||
}
|
||||
|
||||
fn validate_package(pkg: &[u8]) -> bool {
|
||||
let hasher = engine_sha512::Sha512Trunc256::new(Some(FallbackStrategy::HardwareThenSoftware));
|
||||
fn validate_package(pkg: &[u8], pkg_type: PackageType) -> bool {
|
||||
let mut hasher = engine_sha512::Sha512Trunc256::new(Some(FallbackStrategy::HardwareThenSoftware));
|
||||
let mut temp: [u8; 4] = Default::default();
|
||||
temp.copy_from_slice(&pkg[0x20..0x24]);
|
||||
if temp != [0x70, 0x72, 0x65, 0x63] {
|
||||
log::error!("update package does not have the correct signature");
|
||||
return false;
|
||||
if pkg_type == PackageType::Ec {
|
||||
if temp != [0x70, 0x72, 0x65, 0x63] { // 'prec'
|
||||
log::error!("EC firmware update package does not have the correct signature");
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
if temp != [0x77, 0x66, 0x32, 0x30] { // 'wf20'
|
||||
log::error!("WF200 update package does not have the correct signature");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
temp.copy_from_slice(&pkg[0x24..0x28]);
|
||||
if u32::from_le_bytes(temp) != 1 {
|
||||
|
|
@ -44,6 +55,7 @@ fn validate_package(pkg: &[u8]) -> bool {
|
|||
let length = u32::from_le_bytes(temp);
|
||||
hasher.update(&pkg[0x20..(length as usize + 4096)]);
|
||||
let digest = hasher.finalize();
|
||||
log::debug!("digest: {:x?}", digest);
|
||||
for(&stored, &computed) in pkg[..0x20].iter().zip(digest.iter()) {
|
||||
if stored != computed {
|
||||
log::error!("update package hash mismatch");
|
||||
|
|
@ -53,12 +65,129 @@ fn validate_package(pkg: &[u8]) -> bool {
|
|||
true
|
||||
}
|
||||
|
||||
const EC_GATEWARE_BASE: u32 = 0x0;
|
||||
const EC_GATEWARE_LEN: u32 = 0x1_a000;
|
||||
const EC_FIRMWARE_BASE: u32 = 0x1_a000;
|
||||
const WF200_FIRMWARE_BASE: u32 = 0x9_C000;
|
||||
const CTRL_PAGE_LEN: u32 = 0x1000;
|
||||
const EC_GATEWARE_PAD: u32 = 0x1000;
|
||||
|
||||
/// copies an image stored in a `package` slice, starting from `pkg_offset` in the `package` with length `len`
|
||||
/// and writing to FLASH starting at a hardware offset of `flash_start`
|
||||
fn do_update(com: &mut com::Com, callback_conn: xous::CID, package: &[u8], pkg_offset: u32, flash_start: u32, image_len: u32, name: &str) -> bool {
|
||||
if (pkg_offset + image_len) > package.len() as u32 {
|
||||
log::error!("Requested image is larger than the package length");
|
||||
return false;
|
||||
}
|
||||
// erase
|
||||
let mut update_str = String::<1024>::new();
|
||||
write!(update_str, "Erasing {}...", name).unwrap();
|
||||
Buffer::into_buf(update_str).unwrap().lend(callback_conn, CB_ID.load(Ordering::Relaxed)).unwrap();
|
||||
update_str.clear();
|
||||
log::debug!("erasing from 0x{:08x}, {} bytes", flash_start, image_len);
|
||||
if com.flash_erase(flash_start, image_len).unwrap() {
|
||||
write!(update_str, "Done.").unwrap();
|
||||
} else {
|
||||
write!(update_str, "Erase failed, aborting!").unwrap();
|
||||
return false;
|
||||
}
|
||||
Buffer::into_buf(update_str).unwrap().lend(callback_conn, CB_ID.load(Ordering::Relaxed)).unwrap();
|
||||
update_str.clear();
|
||||
|
||||
// program
|
||||
write!(update_str, "Writing {}...", name).unwrap();
|
||||
Buffer::into_buf(update_str).unwrap().lend(callback_conn, CB_ID.load(Ordering::Relaxed)).unwrap();
|
||||
update_str.clear();
|
||||
|
||||
// divide into 1k chunks and send over
|
||||
let exact_chunks = package[pkg_offset as usize..(pkg_offset + image_len) as usize].chunks_exact(1024);
|
||||
let lessthan_1k = exact_chunks.remainder();
|
||||
let mut prog_addr = flash_start;
|
||||
let mut pages: [Option<[u8; 256]>; 4] = [None; 4];
|
||||
let mut progress_ctr = 0;
|
||||
for chunks in exact_chunks {
|
||||
for (full_page, dst_page) in chunks.chunks_exact(256).zip(pages.iter_mut()) {
|
||||
*dst_page = Some(
|
||||
{
|
||||
let mut alloc_page:[u8; 256] = [0; 256];
|
||||
for (&src, dst) in full_page.iter().zip(alloc_page.iter_mut()) {
|
||||
*dst = src;
|
||||
}
|
||||
alloc_page
|
||||
}
|
||||
);
|
||||
}
|
||||
log::debug!("prog 0x{:08x} 4 pages", prog_addr);
|
||||
if com.flash_program(prog_addr, pages).unwrap() == false {
|
||||
write!(update_str, "Program failed, aborting!").unwrap();
|
||||
Buffer::into_buf(update_str).unwrap().lend(callback_conn, CB_ID.load(Ordering::Relaxed)).unwrap();
|
||||
update_str.clear();
|
||||
return false;
|
||||
}
|
||||
prog_addr += 1024;
|
||||
progress_ctr += 1;
|
||||
if (progress_ctr % 16) == 0 {
|
||||
write!(update_str, "{:.1}% complete", ((prog_addr - flash_start) as f64 / image_len as f64) * 100.0).unwrap();
|
||||
Buffer::into_buf(update_str).unwrap().lend(callback_conn, CB_ID.load(Ordering::Relaxed)).unwrap();
|
||||
update_str.clear();
|
||||
}
|
||||
}
|
||||
// take the remainder that's less than 1k, and divide into 256-byte pages
|
||||
if lessthan_1k.len() > 0 {
|
||||
let fullpages = lessthan_1k.chunks_exact(256);
|
||||
let leftovers = fullpages.remainder();
|
||||
let mut pages_written = 0;
|
||||
pages = [None; 4]; // clear the pages buffer
|
||||
for (full_page, dst_page) in fullpages.zip(pages.iter_mut()) {
|
||||
*dst_page = Some(
|
||||
{
|
||||
let mut alloc_page:[u8; 256] = [0; 256];
|
||||
for (&src, dst) in full_page.iter().zip(alloc_page.iter_mut()) {
|
||||
*dst = src;
|
||||
}
|
||||
alloc_page
|
||||
}
|
||||
);
|
||||
pages_written += 1;
|
||||
}
|
||||
// take the remainder that's less than 256-bytes, pad it to 256 bytes, and stick it in the very last page
|
||||
if leftovers.len() > 0 {
|
||||
pages[pages_written] = Some({
|
||||
let mut alloc_page: [u8; 256] = [0; 256];
|
||||
for(&src, dst) in leftovers.iter().zip(alloc_page.iter_mut()) {
|
||||
*dst = src;
|
||||
}
|
||||
alloc_page
|
||||
});
|
||||
}
|
||||
let mut dbgcnt = 0;
|
||||
for pg in pages.iter() {
|
||||
if pg.is_some() {
|
||||
dbgcnt += 1;
|
||||
}
|
||||
}
|
||||
log::debug!("prog 0x{:08x} {} pages (last op)", prog_addr, dbgcnt);
|
||||
}
|
||||
if com.flash_program(prog_addr, pages).unwrap() == false {
|
||||
write!(update_str, "Program failed, aborting!").unwrap();
|
||||
Buffer::into_buf(update_str).unwrap().lend(callback_conn, CB_ID.load(Ordering::Relaxed)).unwrap();
|
||||
update_str.clear();
|
||||
false
|
||||
} else {
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
fn ecupdate_thread(sid0: usize, sid1: usize, sid2: usize, sid3: usize) {
|
||||
let sid = xous::SID::from_u32(sid0 as u32, sid1 as u32, sid2 as u32, sid3 as u32);
|
||||
let xns = xous_names::XousNames::new().unwrap();
|
||||
let com = com::Com::new(&xns).unwrap();
|
||||
let mut com = com::Com::new(&xns).unwrap();
|
||||
let callback_conn = xns.request_connection_blocking(crate::SERVER_NAME_SHELLCHAT).unwrap();
|
||||
let susres = susres::Susres::new_without_hook(&xns).unwrap();
|
||||
let mut susres = susres::Susres::new_without_hook(&xns).unwrap();
|
||||
|
||||
if com.flash_acquire().unwrap() == false {
|
||||
log::error!("couldn't acquire exclusive access to the EC updater mechanism. All other operations will fail!");
|
||||
}
|
||||
|
||||
let ec_package = xous::syscall::map_memory(
|
||||
xous::MemoryAddress::new(xous::EC_FW_PKG_LOC as usize),
|
||||
|
|
@ -76,29 +205,48 @@ fn ecupdate_thread(sid0: usize, sid1: usize, sid2: usize, sid3: usize) {
|
|||
let msg = xous::receive_message(sid).unwrap();
|
||||
log::debug!("EC updater got msg {:?}", msg);
|
||||
match FromPrimitive::from_usize(msg.body.id()) {
|
||||
Some(UpdateOp::UpdateFirmware) => {
|
||||
let package = unsafe{ ec_package.as_ptr() as &[u8]};
|
||||
if !validate_package(package) {
|
||||
Some(UpdateOp::UpdateGateware) => {
|
||||
let package = unsafe{ core::slice::from_raw_parts(ec_package.as_ptr() as *const u8, xous::EC_FW_PKG_LEN as usize)};
|
||||
|
||||
if !validate_package(package,PackageType::Ec) {
|
||||
log::error!("firmware package did not pass validation");
|
||||
xous::send_message(callback_conn,
|
||||
xous::Message::new_scalar(CB_ID.load(Ordering::Relaxed) as usize,
|
||||
UpdateResult::Abort.to_usize().unwrap(),
|
||||
0, 0, 0)).unwrap();
|
||||
} else {
|
||||
// do the update
|
||||
let mut temp: [u8; 4] = Default::default();
|
||||
temp.copy_from_slice(&package[0x28..0x2c]);
|
||||
let length = u32::from_le_bytes(temp);
|
||||
susres.set_suspendable(false).unwrap(); // block suspend/resume operations
|
||||
do_update(&mut com, callback_conn, package, CTRL_PAGE_LEN, EC_GATEWARE_BASE, EC_GATEWARE_LEN, "gateware");
|
||||
susres.set_suspendable(true).unwrap(); // resume suspend/resume operations
|
||||
|
||||
// erase
|
||||
|
||||
// program
|
||||
|
||||
// completed successfully, report the result
|
||||
xous::send_message(callback_conn,
|
||||
xous::Message::new_scalar(CB_ID.load(Ordering::Relaxed) as usize,
|
||||
UpdateResult::ProgramDone.to_usize().unwrap(),
|
||||
length as usize, 0, 0)).unwrap();
|
||||
EC_GATEWARE_LEN as usize, 0, 0)).unwrap();
|
||||
}
|
||||
},
|
||||
Some(UpdateOp::UpdateFirmware) => {
|
||||
let package = unsafe{ core::slice::from_raw_parts(ec_package.as_ptr() as *const u8, xous::EC_FW_PKG_LEN as usize)};
|
||||
let mut temp: [u8; 4] = Default::default();
|
||||
temp.copy_from_slice(&package[0x28..0x2c]);
|
||||
let length = u32::from_le_bytes(temp); // total length of package
|
||||
|
||||
if !validate_package(package,PackageType::Ec) {
|
||||
log::error!("firmware package did not pass validation");
|
||||
xous::send_message(callback_conn,
|
||||
xous::Message::new_scalar(CB_ID.load(Ordering::Relaxed) as usize,
|
||||
UpdateResult::Abort.to_usize().unwrap(),
|
||||
0, 0, 0)).unwrap();
|
||||
} else {
|
||||
susres.set_suspendable(false).unwrap(); // block suspend/resume operations
|
||||
do_update(&mut com, callback_conn, package, EC_GATEWARE_LEN + CTRL_PAGE_LEN,
|
||||
EC_FIRMWARE_BASE, length - (EC_GATEWARE_LEN), "firmware");
|
||||
susres.set_suspendable(true).unwrap(); // resume suspend/resume operations
|
||||
|
||||
xous::send_message(callback_conn,
|
||||
xous::Message::new_scalar(CB_ID.load(Ordering::Relaxed) as usize,
|
||||
UpdateResult::ProgramDone.to_usize().unwrap(),
|
||||
EC_GATEWARE_LEN as usize, 0, 0)).unwrap();
|
||||
}
|
||||
},
|
||||
Some(UpdateOp::Quit) => {
|
||||
|
|
@ -139,13 +287,14 @@ impl<'a> ShellCmdApi<'a> for EcUpdate {
|
|||
cmd_api!(ecup); // inserts boilerplate for command API
|
||||
|
||||
fn process(&mut self, args: String::<1024>, env: &mut CommonEnv) -> Result<Option<String::<1024>>, xous::Error> {
|
||||
use core::fmt::Write;
|
||||
let mut ret = String::<1024>::new();
|
||||
let helpstring = "ecup [all] [gw] [fw] [wf200]";
|
||||
|
||||
log::debug!("ecup handling {}", args.as_str().unwrap());
|
||||
let mut tokens = args.as_str().unwrap().split(' ');
|
||||
|
||||
if self.in_progress {
|
||||
log::debug!("Programming already in progress, can't double-initiate!");
|
||||
write!(ret, "Programming already in progress, can't double-initiate!").unwrap();
|
||||
} else {
|
||||
if let Some(sub_cmd) = tokens.next() {
|
||||
|
|
@ -159,6 +308,15 @@ impl<'a> ShellCmdApi<'a> for EcUpdate {
|
|||
).unwrap();
|
||||
write!(ret, "Starting EC firmware update").unwrap();
|
||||
}
|
||||
"gw" => {
|
||||
self.in_progress = true;
|
||||
let start = env.ticktimer.elapsed_ms();
|
||||
self.start_time = Some(start);
|
||||
xous::send_message(self.update_cid,
|
||||
xous::Message::new_scalar(UpdateOp::UpdateGateware.to_usize().unwrap(), 0, 0, 0, 0)
|
||||
).unwrap();
|
||||
write!(ret, "Starting EC firmware update").unwrap();
|
||||
}
|
||||
_ => {
|
||||
write!(ret, "{}", helpstring).unwrap();
|
||||
}
|
||||
|
|
@ -172,38 +330,35 @@ impl<'a> ShellCmdApi<'a> for EcUpdate {
|
|||
}
|
||||
|
||||
fn callback(&mut self, msg: &xous::MessageEnvelope, env: &mut CommonEnv) -> Result<Option<String::<1024>>, xous::Error> {
|
||||
use core::fmt::Write;
|
||||
|
||||
log::debug!("update callback");
|
||||
let mut ret = String::<1024>::new();
|
||||
|
||||
xous::msg_scalar_unpack!(msg, result_code, progress, total, _, {
|
||||
let end = env.ticktimer.elapsed_ms();
|
||||
let elapsed: f64 = (end - self.start_time.unwrap()) as f64;
|
||||
match FromPrimitive::from_usize(result_code) {
|
||||
UpdateResult::PackageValid => {
|
||||
write!(ret, "Firmware package validated in {}ms", elapsed).unwrap();
|
||||
},
|
||||
UpdateResult::EraseDone => {
|
||||
write!(ret, "Erase of {} bytes done", progress).unwrap();
|
||||
},
|
||||
UpdateResult::EraseProgress => {
|
||||
write!(ret, "Erasing {:.1}% complete", (progress as f64 / total as f64) * 100.0).unwrap();
|
||||
},
|
||||
UpdateResult::ProgramDone => {
|
||||
write!(ret, "Programming of {} bytes done in {:.1}s", progress, elapsed / 1000.0).unwrap();
|
||||
self.in_progress = false;
|
||||
},
|
||||
UpdateResult::ProgramProgress => {
|
||||
write!(ret, "Programming {:.1}% complete", (progress as f64 / total as f64) * 100.0).unwrap();
|
||||
},
|
||||
UpdateResult::Abort => {
|
||||
write!(ret, "Programming aborted in {:.1}s due to an internal error!", elapsed / 1000.0).unwrap();
|
||||
self.in_progress = false;
|
||||
if let xous::Message::Borrow(m) = &msg.body {
|
||||
// a bit of a hack: we can't route on ID because that's consumed by the outer shell dispatch loop
|
||||
// however, if the form of the message is a Borrowed buffer, treat it as a string and print it.
|
||||
let update_buf = unsafe { Buffer::from_memory_message(m) };
|
||||
let update_str = update_buf.as_flat::<String::<1024>, _>().unwrap();
|
||||
write!(ret, "{}", update_str.as_str()).unwrap();
|
||||
} else { // otherwise, unpack it and use the first argument as a sub-opcode type
|
||||
xous::msg_scalar_unpack!(msg, result_code, progress, _, _, {
|
||||
let end = env.ticktimer.elapsed_ms();
|
||||
let elapsed: f64 = (end - self.start_time.unwrap()) as f64;
|
||||
match FromPrimitive::from_usize(result_code) {
|
||||
Some(UpdateResult::PackageValid) => {
|
||||
write!(ret, "Firmware package validated in {}ms", elapsed).unwrap();
|
||||
},
|
||||
Some(UpdateResult::ProgramDone) => {
|
||||
write!(ret, "Programming of {} bytes done in {:.1}s", progress, elapsed / 1000.0).unwrap();
|
||||
self.in_progress = false;
|
||||
},
|
||||
Some(UpdateResult::Abort) => {
|
||||
write!(ret, "Programming aborted in {:.1}s due to an internal error!", elapsed / 1000.0).unwrap();
|
||||
self.in_progress = false;
|
||||
}
|
||||
_ => write!(ret, "Got unknown update callback: {:?}", result_code).unwrap()
|
||||
}
|
||||
_ => write!(ret, "Got unknown update callback: {:?}", result_code).unwrap()
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
Ok(Some(ret))
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -258,7 +258,7 @@ pub(crate) const SERVER_NAME_SHELLCHAT: &str = "_Shell chat application_";
|
|||
#[xous::xous_main]
|
||||
fn xmain() -> ! {
|
||||
log_server::init_wait().unwrap();
|
||||
log::set_max_level(log::LevelFilter::Info);
|
||||
log::set_max_level(log::LevelFilter::Debug);
|
||||
info!("my PID is {}", xous::process::id());
|
||||
|
||||
let xns = xous_names::XousNames::new().unwrap();
|
||||
|
|
|
|||
Loading…
Reference in a new issue