fixed ordering bug in spinor driver

works now!
This commit is contained in:
bunnie 2021-07-18 18:02:46 +08:00
parent 7257dbcaef
commit d98f296499
3 changed files with 65 additions and 16 deletions

View file

@ -105,7 +105,7 @@ impl<'a> ShellCmdApi<'a> for Keys {
let mut errs = 0; let mut errs = 0;
for (addr, (&rom, &reference)) in region.iter().zip(reference.iter()).enumerate() { for (addr, (&rom, &reference)) in region.iter().zip(reference.iter()).enumerate() {
if rom != reference { if rom != reference {
if errs < 256 { if (errs % 256 < 2) || (errs % 256 >= 254) {
log::error!("fill random failed 0x{:08x}: e.{:02x} o.{:02x}", addr, reference, rom); log::error!("fill random failed 0x{:08x}: e.{:02x} o.{:02x}", addr, reference, rom);
} }
errs += 1; errs += 1;
@ -113,6 +113,30 @@ impl<'a> ShellCmdApi<'a> for Keys {
} }
log::debug!("fill random data finished, errs: {}", errs); log::debug!("fill random data finished, errs: {}", errs);
// test a patch on random data
log::debug!("programmed patch test");
let patch: [u8; 4] = [0xaa, 0xbb, 0xcc, 0xdd];
self.spinor.patch(&region, region_base, &patch, 12).expect("couldn't do smallest patch");
for (addr, (&byte, &reference)) in region.iter().zip(reference.iter()).enumerate() {
match addr {
12 => if byte != 0xaa { log::error!("patch failed e.aa o.{:02x}", byte) },
13 => if byte != 0xbb { log::error!("patch failed e.bb o.{:02x}", byte) },
14 => if byte != 0xcc { log::error!("patch failed e.cc o.{:02x}", byte) },
15 => if byte != 0xdd { log::error!("patch failed e.dd o.{:02x}", byte) },
_ => if byte != reference {
if (errs % 256 < 4) || (errs % 256 >= 252) {
log::error!("patch failed, erase disturb at region offset 0x{:08x}: e.{:02x} o.{:02x}", addr, reference, byte)
}
errs += 1;
}
}
}
log::debug!("patch test finished, errs {}", errs);
// refresh the reference, otherwise this patch will throw errors
for (&src, dst) in region.iter().zip(reference.iter_mut()) {
*dst = src;
}
log::debug!("patch across a page boundary"); log::debug!("patch across a page boundary");
let bigger_patch: [u8; 16] = [0xf0, 0x0d, 0xbe, 0xef, 0xaa, 0x55, 0x99, 0x66, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff]; let bigger_patch: [u8; 16] = [0xf0, 0x0d, 0xbe, 0xef, 0xaa, 0x55, 0x99, 0x66, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff];
log::debug!("before patch: {:x?}", &region[0x0ff4..0x100c]); log::debug!("before patch: {:x?}", &region[0x0ff4..0x100c]);
@ -123,7 +147,7 @@ impl<'a> ShellCmdApi<'a> for Keys {
match addr { match addr {
0xff8..=0x1007 => { 0xff8..=0x1007 => {
if rom != bigger_patch[i] { if rom != bigger_patch[i] {
if errs < 256 { if (errs % 256 < 2) || (errs % 256 >= 254) {
log::error!("bigger patch failed 0x{:08x}: e.{:02x} o.{:02x}", addr, bigger_patch[i], rom); log::error!("bigger patch failed 0x{:08x}: e.{:02x} o.{:02x}", addr, bigger_patch[i], rom);
} }
errs += 1; errs += 1;
@ -132,7 +156,7 @@ impl<'a> ShellCmdApi<'a> for Keys {
}, },
_ => { _ => {
if rom != reference { if rom != reference {
if errs < 256 { if (errs % 256 < 2) || (errs % 256 >= 254) {
log::error!("data disturbed at 0x{:08x}: e.{:02x} o.{:02x}", addr, reference, rom); log::error!("data disturbed at 0x{:08x}: e.{:02x} o.{:02x}", addr, reference, rom);
} }
errs += 1; errs += 1;

View file

@ -612,6 +612,23 @@ mod tests {
*p = i as u8; *p = i as u8;
} }
/*
let mut blank = true;
for page in patch[0..patch.len()].chunks(256) {
for word in page.chunks(2) {
let wdata = word[0] as u32 | ((word[1] as u32) << 8);
if wdata != 0xFFFF {
blank = false;
break;
}
}
for word in page.chunks(2) {
let wdata = word[0] as u32 | ((word[1] as u32) << 8);
print!("0x{:04x} ", wdata);
}
println!("");
}
*/
// patch region base + index = 0x1F00 with 384 bytes of data (to 0x2080) // patch region base + index = 0x1F00 with 384 bytes of data (to 0x2080)
print!("{:x?}", &EMU_FLASH.lock().unwrap()[0x1EFC..0x1F04]); print!("{:x?}", &EMU_FLASH.lock().unwrap()[0x1EFC..0x1F04]);
print!("{:x?}", &EMU_FLASH.lock().unwrap()[0x207C..0x2084]); print!("{:x?}", &EMU_FLASH.lock().unwrap()[0x207C..0x2084]);

View file

@ -17,6 +17,7 @@ mod implementation {
use core::sync::atomic::{AtomicBool, AtomicU32, Ordering}; use core::sync::atomic::{AtomicBool, AtomicU32, Ordering};
use num_traits::*; use num_traits::*;
#[derive(Debug)]
enum FlashOp { enum FlashOp {
EraseSector(u32), // 4k sector EraseSector(u32), // 4k sector
WritePages(u32, [u8; 4096], usize), // page address, data, len WritePages(u32, [u8; 4096], usize), // page address, data, len
@ -82,7 +83,7 @@ mod implementation {
} }
let partial_page = &data[0..pre_align as usize]; let partial_page = &data[0..pre_align as usize];
// pre-fill the page fifo // check for blank writes and skip
let mut blank = true; let mut blank = true;
for word in partial_page.chunks(2) { for word in partial_page.chunks(2) {
// if the data is blank, don't do a write // if the data is blank, don't do a write
@ -93,10 +94,6 @@ mod implementation {
} }
} }
if !blank { if !blank {
for word in partial_page.chunks(2) {
let wdata = word[0] as u32 | ((word[1] as u32) << 8);
spinor.csr.wfo(utra::spinor::WDATA_WDATA, wdata);
}
// enable writes: set wren mode // enable writes: set wren mode
loop { loop {
flash_wren(&mut spinor.csr); flash_wren(&mut spinor.csr);
@ -105,6 +102,11 @@ mod implementation {
break; break;
} }
} }
// fill the page fifo
for word in partial_page.chunks(2) {
let wdata = word[0] as u32 | ((word[1] as u32) << 8);
spinor.csr.wfo(utra::spinor::WDATA_WDATA, wdata);
}
// send the data to be programmed // send the data to be programmed
flash_pp4b(&mut spinor.csr, cur_addr, partial_page.len() as u32); flash_pp4b(&mut spinor.csr, cur_addr, partial_page.len() as u32);
while (flash_rdsr(&mut spinor.csr, 1) & 0x01) != 0 { while (flash_rdsr(&mut spinor.csr, 1) & 0x01) != 0 {
@ -120,7 +122,7 @@ mod implementation {
// now write the remaining, aligned pages. The last chunk can be short of data, // now write the remaining, aligned pages. The last chunk can be short of data,
// that's also fine; the write will not affect bytes that are not transmitted // that's also fine; the write will not affect bytes that are not transmitted
for page in data[pre_align as usize..len].chunks(256) { for page in data[pre_align as usize..len].chunks(256) {
// pre-fill the page fifo // check & skip writes that are blank
let mut blank = true; let mut blank = true;
for word in page.chunks(2) { for word in page.chunks(2) {
let wdata = word[0] as u32 | ((word[1] as u32) << 8); let wdata = word[0] as u32 | ((word[1] as u32) << 8);
@ -133,10 +135,6 @@ mod implementation {
// skip over pages that are entirely blank // skip over pages that are entirely blank
continue; continue;
} }
for word in page.chunks(2) {
let wdata = word[0] as u32 | ((word[1] as u32) << 8);
spinor.csr.wfo(utra::spinor::WDATA_WDATA, wdata);
}
// enable writes: set wren mode // enable writes: set wren mode
loop { loop {
flash_wren(&mut spinor.csr); flash_wren(&mut spinor.csr);
@ -145,6 +143,11 @@ mod implementation {
break; break;
} }
} }
// fill the fifo
for word in page.chunks(2) {
let wdata = word[0] as u32 | ((word[1] as u32) << 8);
spinor.csr.wfo(utra::spinor::WDATA_WDATA, wdata);
}
// send the data to be programmed // send the data to be programmed
flash_pp4b(&mut spinor.csr, cur_addr, page.len() as u32); flash_pp4b(&mut spinor.csr, cur_addr, page.len() as u32);
cur_addr += page.len() as u32; cur_addr += page.len() as u32;
@ -285,12 +288,14 @@ mod implementation {
} }
fn flash_pp4b(csr: &mut utralib::CSR<u32>, address: u32, data_bytes: u32) { fn flash_pp4b(csr: &mut utralib::CSR<u32>, address: u32, data_bytes: u32) {
let data_words = data_bytes / 2;
assert!(data_words <= 128, "data_words specified is longer than one page!");
csr.wfo(utra::spinor::CMD_ARG_CMD_ARG, address); csr.wfo(utra::spinor::CMD_ARG_CMD_ARG, address);
csr.wo(utra::spinor::COMMAND, csr.wo(utra::spinor::COMMAND,
csr.ms(utra::spinor::COMMAND_EXEC_CMD, 1) csr.ms(utra::spinor::COMMAND_EXEC_CMD, 1)
| csr.ms(utra::spinor::COMMAND_CMD_CODE, 0x12) // PP4B | csr.ms(utra::spinor::COMMAND_CMD_CODE, 0x12) // PP4B
| csr.ms(utra::spinor::COMMAND_HAS_ARG, 1) | csr.ms(utra::spinor::COMMAND_HAS_ARG, 1)
| csr.ms(utra::spinor::COMMAND_DATA_WORDS, data_bytes / 2) | csr.ms(utra::spinor::COMMAND_DATA_WORDS, data_words)
| csr.ms(utra::spinor::COMMAND_LOCK_READS, 1) | csr.ms(utra::spinor::COMMAND_LOCK_READS, 1)
); );
while csr.rf(utra::spinor::STATUS_WIP) != 0 {} while csr.rf(utra::spinor::STATUS_WIP) != 0 {}
@ -386,7 +391,7 @@ mod implementation {
} }
pub(crate) fn write_region(&mut self, wr: &mut WriteRegion) -> SpinorError { pub(crate) fn write_region(&mut self, wr: &mut WriteRegion) -> SpinorError {
log::trace!("processing write_region with {:x?}", wr); // log::trace!("processing write_region with {:x?}", wr);
if wr.start + wr.len > SPINOR_SIZE_BYTES { // basic security check. this is necessary so we don't have wrap-around attacks on the SoC gateware region if wr.start + wr.len > SPINOR_SIZE_BYTES { // basic security check. this is necessary so we don't have wrap-around attacks on the SoC gateware region
return SpinorError::InvalidRequest; return SpinorError::InvalidRequest;
} }
@ -401,6 +406,7 @@ mod implementation {
return SpinorError::AlignmentError; return SpinorError::AlignmentError;
} }
self.cur_op = Some(FlashOp::EraseSector(wr.start)); self.cur_op = Some(FlashOp::EraseSector(wr.start));
log::trace!("erase: {:x?}", self.cur_op);
let erase_result = self.call_spinor_context_blocking(); let erase_result = self.call_spinor_context_blocking();
if erase_result & 0x40 != 0 { if erase_result & 0x40 != 0 {
log::error!("E_FAIL set, erase failed: result 0x{:02x}, sector addr 0x{:08x}", erase_result, wr.start); log::error!("E_FAIL set, erase failed: result 0x{:02x}, sector addr 0x{:08x}", erase_result, wr.start);
@ -409,6 +415,7 @@ mod implementation {
// now write the data sector // now write the data sector
self.cur_op = Some(FlashOp::WritePages(wr.start, wr.data, wr.len as usize)); self.cur_op = Some(FlashOp::WritePages(wr.start, wr.data, wr.len as usize));
log::trace!("write: len:{}, {:x?}", wr.len, self.cur_op);
let write_result = self.call_spinor_context_blocking(); let write_result = self.call_spinor_context_blocking();
if write_result & 0x20 != 0 { if write_result & 0x20 != 0 {
log::error!("P_FAIL set, program failed/partial abort: result 0x{:02x}, sector addr 0x{:08x}", write_result, wr.start); log::error!("P_FAIL set, program failed/partial abort: result 0x{:02x}, sector addr 0x{:08x}", write_result, wr.start);
@ -422,6 +429,7 @@ mod implementation {
// here, almost any data alignment and length is acceptable -- we can patch even just two bytes using // here, almost any data alignment and length is acceptable -- we can patch even just two bytes using
// this call. // this call.
self.cur_op = Some(FlashOp::WritePages(wr.start, wr.data, wr.len as usize)); self.cur_op = Some(FlashOp::WritePages(wr.start, wr.data, wr.len as usize));
log::trace!("clean write: len:{}, {:x?}", wr.len, self.cur_op);
let write_result = self.call_spinor_context_blocking(); let write_result = self.call_spinor_context_blocking();
if write_result & 0x20 != 0 { if write_result & 0x20 != 0 {
log::error!("P_FAIL set, program failed/partial abort: result 0x{:02x}, sector addr 0x{:08x}", write_result, wr.start); log::error!("P_FAIL set, program failed/partial abort: result 0x{:02x}, sector addr 0x{:08x}", write_result, wr.start);
@ -517,7 +525,7 @@ fn xmain() -> ! {
use crate::implementation::Spinor; use crate::implementation::Spinor;
log_server::init_wait().unwrap(); log_server::init_wait().unwrap();
log::set_max_level(log::LevelFilter::Trace); log::set_max_level(log::LevelFilter::Info);
log::info!("my PID is {}", xous::process::id()); log::info!("my PID is {}", xous::process::id());
let xns = xous_names::XousNames::new().unwrap(); let xns = xous_names::XousNames::new().unwrap();