add more tests

This commit is contained in:
bunnie 2021-07-17 23:33:57 +08:00
parent 168d0fc2cb
commit 904f6f6f38
2 changed files with 275 additions and 32 deletions

View file

@ -163,7 +163,7 @@ impl Spinor {
let mut patch_len_aligned = patch_data.len() as u32 + patch_pre_pad;
// if the total length is not aligned to an erase block, add the padding up until the next erase block
if patch_len_aligned & align_mask != 0 {
patch_len_aligned += self.erase_alignment() - patch_len_aligned & align_mask;
patch_len_aligned += self.erase_alignment() - (patch_len_aligned & align_mask);
}
let mut cur_index = patch_index_aligned;
@ -299,7 +299,9 @@ impl Drop for Spinor {
}
}
// run with `cargo test -- --nocapture` to see the print output inside services/spinor
// run with `cargo test -- --nocapture --test-threads=1`:
// --nocapture to see the print output (while debugging)
// --test-threads=1 because the FLASH ROM is a shared state object
#[cfg(test)]
mod tests {
use super::*;
@ -329,7 +331,8 @@ mod tests {
wr.data[2] = 0xCC;
wr.data[3] = 0xDD;
let mut spinor = Spinor::new();
spinor.send_write_region(&wr);
let ret = spinor.send_write_region(&wr);
assert!(ret.is_ok(), "spinor write returned an error");
for (addr, &byte) in EMU_FLASH.lock().unwrap().iter().enumerate() {
// we should see 0xAA 0xBB 0xCC 0xDD from addresses 8-12, everyhing else as 0xFF
@ -357,7 +360,8 @@ mod tests {
wr.data[8] = 0x3;
wr.data[9] = 0x4;
wr.len = 10;
spinor.send_write_region(&wr);
let ret = spinor.send_write_region(&wr);
assert!(ret.is_ok(), "spinor write returned an error");
for (addr, &byte) in EMU_FLASH.lock().unwrap().iter().enumerate() {
// we should see 1, 2, 3, 4 from addresses 6-10, everyhing else as 0xFF
@ -419,7 +423,7 @@ mod tests {
let region_len = 0x1000 * 4; // four sectors long, one sector up
let region = &flash_orig[region_base as usize .. (region_base + region_len) as usize];
// exactly one word of patch data
// exactly one sector of patch data
let mut patch: [u8; 4096] = [0; 4096];
for (i, p) in patch.iter_mut().enumerate() {
*p = i as u8;
@ -432,10 +436,192 @@ mod tests {
for (addr, (&patched, &orig)) in EMU_FLASH.lock().unwrap().iter().zip(flash_orig.iter()).enumerate() {
// we should see a repeating pattern of 0, 1, 2... from 0x2000-0x3000
if addr < 0x2000 || addr >= 0x3000 {
assert!(patched == orig, "data disturbed: {:08x} : e{:02x} a{:02x}", addr, orig, patched); // e = expected, a = actual
assert!(patched == orig, "data disturbed: {:08x} : e.{:02x} a.{:02x}", addr, orig, patched); // e = expected, a = actual
} else {
assert!(patched == addr as u8, "data was not patched: {:08x} : e{:02x} a{:02x}", addr, addr as u8, patched);
assert!(patched == addr as u8, "data was not patched: {:08x} : e.{:02x} a.{:02x}", addr, addr as u8, patched);
}
}
}
#[test]
fn test_small_patch() {
let mut spinor = Spinor::new();
init_emu_flash(8);
flash_fill_rand();
// create our "flash region" -- normally this would be a memory-mapped block that's owned by our calling function
// here we bodge it out of the emulated flash space with some rough parameter
let mut flash_orig = Vec::<u8>::new();
flash_orig.extend(EMU_FLASH.lock().unwrap().as_slice().iter().copied()); // snag a copy of the original state, so we can be sure the patch was targeted
let region_base = 0x1000; // region bases are required to be aligned to an erase sector; guaranteed by fiat
let region_len = 0x1000 * 4; // four sectors long, one sector up
let region = &flash_orig[region_base as usize .. (region_base + region_len) as usize];
// patch two words, the minimum allowed amount
let patch: [u8; 2] = [0x33, 0xCC];
// patch region base + index = 0x2704 with two bytes of data
print!("{:x?}", &EMU_FLASH.lock().unwrap()[0x2700..0x2708]);
let result = spinor.patch(region, region_base, &patch, 0x1704);
assert!(result.is_ok(), "patch threw an error");
for (addr, (&patched, &orig)) in EMU_FLASH.lock().unwrap().iter().zip(flash_orig.iter()).enumerate() {
match addr {
0x2704 => assert!(patched == 0x33, "data was not patched: {:08x} : e.{:02x} a.{:02x}", addr, 0x33, patched),
0x2705 => assert!(patched == 0xCC, "data was not patched: {:08x} : e.{:02x} a.{:02x}", addr, 0x33, patched),
_ => assert!(patched == orig, "data disturbed: {:08x} : e.{:02x} a.{:02x}", addr, orig, patched),
}
}
print!("{:x?}", &EMU_FLASH.lock().unwrap()[0x2700..0x2708]);
}
#[test]
fn test_misaligned_start_end_full_sector() {
let mut spinor = Spinor::new();
init_emu_flash(8);
flash_fill_rand();
// create our "flash region" -- normally this would be a memory-mapped block that's owned by our calling function
// here we bodge it out of the emulated flash space with some rough parameter
let mut flash_orig = Vec::<u8>::new();
flash_orig.extend(EMU_FLASH.lock().unwrap().as_slice().iter().copied()); // snag a copy of the original state, so we can be sure the patch was targeted
let region_base = 0x1000; // region bases are required to be aligned to an erase sector; guaranteed by fiat
let region_len = 0x1000 * 4; // four sectors long, one sector up
let region = &flash_orig[region_base as usize .. (region_base + region_len) as usize];
// patch one full page
let mut patch: [u8; 4096] = [0; 4096];
for (i, p) in patch.iter_mut().enumerate() {
*p = i as u8;
}
// patch region base + index = 0x2704 with one full page of data
print!("{:x?}", &EMU_FLASH.lock().unwrap()[0x2700..0x2708]);
let result = spinor.patch(region, region_base, &patch, 0x1704);
assert!(result.is_ok(), "patch threw an error");
let mut i = 0;
for (addr, (&patched, &orig)) in EMU_FLASH.lock().unwrap().iter().zip(flash_orig.iter()).enumerate() {
if addr < 0x2704 || addr >= 0x3704 {
assert!(patched == orig, "data disturbed: {:08x} : e.{:02x} a.{:02x}", addr, orig, patched);
} else {
assert!(patched == i as u8, "data was not patched: {:08x} : e.{:02x} a.{:02x}", addr, i as u8, patched);
i += 1;
}
}
print!("{:x?}", &EMU_FLASH.lock().unwrap()[0x2700..0x2708]);
}
#[test]
fn test_misaligned_start_partial_sector() {
let mut spinor = Spinor::new();
init_emu_flash(8);
flash_fill_rand();
// create our "flash region" -- normally this would be a memory-mapped block that's owned by our calling function
// here we bodge it out of the emulated flash space with some rough parameter
let mut flash_orig = Vec::<u8>::new();
flash_orig.extend(EMU_FLASH.lock().unwrap().as_slice().iter().copied()); // snag a copy of the original state, so we can be sure the patch was targeted
let region_base = 0x1000; // region bases are required to be aligned to an erase sector; guaranteed by fiat
let region_len = 0x1000 * 4; // four sectors long, one sector up
let region = &flash_orig[region_base as usize .. (region_base + region_len) as usize];
// patch over a page boundary, but not a full page
let mut patch: [u8; 2304] = [0; 2304];
for (i, p) in patch.iter_mut().enumerate() {
*p = i as u8;
}
// patch region base + index = 0x2704 with two bytes of data
let result = spinor.patch(region, region_base, &patch, 0x1704);
assert!(result.is_ok(), "patch threw an error");
let mut i = 0;
for (addr, (&patched, &orig)) in EMU_FLASH.lock().unwrap().iter().zip(flash_orig.iter()).enumerate() {
match addr {
0x2704..=0x3003 => {assert!(patched == i as u8, "data was not patched: {:08x} : e.{:02x} a.{:02x}", addr, 0x33, patched); i+= 1;},
_ => assert!(patched == orig, "data disturbed: {:08x} : e.{:02x} a.{:02x}", addr, orig, patched),
}
}
}
#[test]
fn test_aligned_start_partial_sector() {
let mut spinor = Spinor::new();
init_emu_flash(8);
flash_fill_rand();
// create our "flash region" -- normally this would be a memory-mapped block that's owned by our calling function
// here we bodge it out of the emulated flash space with some rough parameter
let mut flash_orig = Vec::<u8>::new();
flash_orig.extend(EMU_FLASH.lock().unwrap().as_slice().iter().copied()); // snag a copy of the original state, so we can be sure the patch was targeted
let region_base = 0x1000; // region bases are required to be aligned to an erase sector; guaranteed by fiat
let region_len = 0x1000 * 4; // four sectors long, one sector up
let region = &flash_orig[region_base as usize .. (region_base + region_len) as usize];
// patch over a page boundary, but not a full page
let mut patch: [u8; 578] = [0; 578];
for (i, p) in patch.iter_mut().enumerate() {
*p = i as u8;
}
// patch region base + index = 0x2000 with 578 bytes of data
let result = spinor.patch(region, region_base, &patch, 0x1000);
assert!(result.is_ok(), "patch threw an error");
let mut i = 0;
for (addr, (&patched, &orig)) in EMU_FLASH.lock().unwrap().iter().zip(flash_orig.iter()).enumerate() {
match addr {
0x2000..=0x2241 => {assert!(patched == i as u8, "data was not patched: {:08x} : e.{:02x} a.{:02x}", addr, 0x33, patched); i+= 1;},
_ => assert!(patched == orig, "data disturbed: {:08x} : e.{:02x} a.{:02x}", addr, orig, patched),
}
}
}
#[test]
fn test_patch_edge() {
let mut spinor = Spinor::new();
init_emu_flash(8);
flash_fill_rand();
// poke a small "erased" region for patching in this test: exactly the right size for the anticipated patch of 384 bytes
for byte in EMU_FLASH.lock().unwrap()[0x1F00..0x2080].iter_mut() {
*byte = 0xFF;
}
// create our "flash region" -- normally this would be a memory-mapped block that's owned by our calling function
// here we bodge it out of the emulated flash space with some rough parameter
let mut flash_orig = Vec::<u8>::new();
flash_orig.extend(EMU_FLASH.lock().unwrap().as_slice().iter().copied()); // snag a copy of the original state, so we can be sure the patch was targeted
let region_base = 0x1000; // region bases are required to be aligned to an erase sector; guaranteed by fiat
let region_len = 0x1000 * 4; // four sectors long, one sector up
let region = &flash_orig[region_base as usize .. (region_base + region_len) as usize];
// patch over a page boundary, but not a full page
let mut patch: [u8; 384] = [0; 384];
for (i, p) in patch.iter_mut().enumerate() {
*p = i as u8;
}
// 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()[0x207C..0x2084]);
let result = spinor.patch(region, region_base, &patch, 0xF00);
assert!(result.is_ok(), "patch threw an error");
let mut i = 0;
for (addr, (&patched, &orig)) in EMU_FLASH.lock().unwrap().iter().zip(flash_orig.iter()).enumerate() {
match addr {
0x1F00..=0x207F => {assert!(patched == i as u8, "data was not patched: {:08x} : e.{:02x} a.{:02x}", addr, 0x33, patched); i+= 1;},
_ => assert!(patched == orig, "data disturbed: {:08x} : e.{:02x} a.{:02x}", addr, orig, patched),
}
}
print!("{:x?}", &EMU_FLASH.lock().unwrap()[0x1EFC..0x1F04]);
print!("{:x?}", &EMU_FLASH.lock().unwrap()[0x207C..0x2084]);
}
}

View file

@ -67,39 +67,96 @@ mod implementation {
assert!(len <= 4096, "data len is too large");
assert!((len % 2) == 0, "data is not a multiple of 2 in length: the SPI DDR interface always requires two bytes per transfer");
let mut cur_addr = start_addr;
for page in data[..len].chunks(256) {
let mut pre_align = 0;
let mut more_aligned_pages = true;
if cur_addr & 0xff != 0 {
// do a partial-page program to get us into page alignment:
// - it's OK to send an address that isn't page-aligned, but:
// - you can only write data that would program up to the end of the page
// - excess data would "wrap around" and program bytes at the beginning of the page, which is incorrect behavior
pre_align = 0x100 - (cur_addr & 0xFF);
if pre_align >= len {
pre_align = len;
more_aligned_pages = false;
}
let partial_page = &data[0..pre_align as usize];
// pre-fill the page fifo
let mut blank = true;
for word in page.chunks(2) {
for word in partial_page.chunks(2) {
// if the data is blank, don't do a write
let wdata = word[0] as u32 | ((word[1] as u32) << 8);
if wdata != 0xFFFF {
blank = false;
}
spinor.csr.wfo(utra::spinor::WDATA_WDATA, wdata);
}
if blank {
// skip over pages that are entirely blank
continue;
}
// enable writes: set wren mode
loop {
flash_wren(&mut spinor.csr);
let status = flash_rdsr(&mut spinor.csr, 1);
if status & 0x02 != 0 {
blank = false; // short circuit evaluation if we find anything that's not blank
break;
}
}
// send the data to be programmed
flash_pp4b(&mut spinor.csr, cur_addr, page.len() as u32);
cur_addr += page.len() as u32;
while (flash_rdsr(&mut spinor.csr, 1) & 0x01) != 0 {
// wait while WIP is set
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
loop {
flash_wren(&mut spinor.csr);
let status = flash_rdsr(&mut spinor.csr, 1);
if status & 0x02 != 0 {
break;
}
}
// send the data to be programmed
flash_pp4b(&mut spinor.csr, cur_addr, partial_page.len() as u32);
while (flash_rdsr(&mut spinor.csr, 1) & 0x01) != 0 {
// wait while WIP is set
}
// get the success code for return
result = flash_rdscur(&mut spinor.csr);
}
// get the success code for return
result = flash_rdscur(&mut spinor.csr);
if result & 0x20 != 0 {
break; // abort if error
cur_addr += pre_align; // increment the address, even if we "skipped" the region
}
if ((result & 0x20) == 0) && more_aligned_pages {
assert!(cur_addr & 0xff == 0, "data is not page-aligned going into the aligned write phase");
// 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
for page in data[pre_align as usize..len].chunks(256) {
// pre-fill the page fifo
let mut blank = true;
for word in page.chunks(2) {
let wdata = word[0] as u32 | ((word[1] as u32) << 8);
if wdata != 0xFFFF {
blank = false;
break;
}
}
if blank {
// skip over pages that are entirely blank
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
loop {
flash_wren(&mut spinor.csr);
let status = flash_rdsr(&mut spinor.csr, 1);
if status & 0x02 != 0 {
break;
}
}
// send the data to be programmed
flash_pp4b(&mut spinor.csr, cur_addr, page.len() as u32);
cur_addr += page.len() as u32;
while (flash_rdsr(&mut spinor.csr, 1) & 0x01) != 0 {
// wait while WIP is set
}
// get the success code for return
result = flash_rdscur(&mut spinor.csr);
if result & 0x20 != 0 {
break; // abort if error
}
}
}
// disable writes: send wrdi