fix USB update flow

This commit is contained in:
bunnie 2021-08-15 16:03:18 +08:00
parent a19fc87687
commit 353311b987
4 changed files with 94 additions and 22 deletions

View file

@ -175,9 +175,9 @@
"en-audio": "Enter update password, in order to authorize a gateware update."
},
"rootkeys.gwup.finished": {
"en": "Gateware update finished.\nReboot to apply.",
"en": "Gateware update finished\nReboot at your earliest\nconvenience.",
"zh": "更新完成重启应用",
"en-audio": "Gateware update finished. Reboot to apply."
"en-audio": "Gateware update finished. Reboot at your earliest convenience."
},
"rootkeys.get_signing_password": {
"en": "Password request\nSoftware update key\nSecurity: Critical\nWhy: Sign Xous",
@ -185,9 +185,9 @@
"en-audio": "Enter update password, in order to authorize a gateware update."
},
"rootkeys.signxous.finished": {
"en": "Xous signing finished.\nReboot to apply.",
"en": "Xous signing finished.\nReboot at your earliest\nconvenience.",
"zh": "更新完成重启应用",
"en-audio": "Xous signing finished. Reboot to apply."
"en-audio": "Xous signing finished. Reboot at your earliest convenience."
},
"rootkeys.policy_request": {
"en": "Please pick a password\nretention policy:",

View file

@ -666,4 +666,39 @@ mod tests {
print!("{:x?}", &EMU_FLASH.lock().unwrap()[0x207C..0x2084]);
}
#[test]
fn test_patch_csr_area() {
let mut spinor = Spinor::new();
init_emu_flash(640);
flash_fill_rand();
// emulate the "erased" region at the end of the CSR file
for byte in EMU_FLASH.lock().unwrap()[0x27b200..0x27f000].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 = 0x0; // region bases are required to be aligned to an erase sector; guaranteed by fiat
let region_len = 0x1000 * 28; // four sectors long, one sector up
let mut patch = flash_orig.clone();
for i in 0x27efc0..0x27f000 {
patch[i] = 0x66;
}
print!("source {:x?}\n", &patch[0x27efc0..0x27f000]);
print!("target {:x?}\n", &EMU_FLASH.lock().unwrap()[0x27efc0..0x27f000]);
print!("wrong {:x?}\n", &EMU_FLASH.lock().unwrap()[0x27e000..0x27e100]);
let result = spinor.patch(&flash_orig, region_base, &patch[0x27_6000..0x27_f000], 0x27_6000);
assert!(result.is_ok(), "patch threw an error");
print!("source {:x?}\n", &patch[0x27efc0..0x27f000]);
print!("target {:x?}\n", &EMU_FLASH.lock().unwrap()[0x27efc0..0x27f000]);
print!("wrong {:x?}\n", &EMU_FLASH.lock().unwrap()[0x27e000..0x27e100]);
}
}

View file

@ -133,6 +133,7 @@ mod implementation {
}
if blank {
// skip over pages that are entirely blank
cur_addr += page.len() as u32;
continue;
}
// enable writes: set wren mode
@ -391,6 +392,11 @@ mod implementation {
}
pub(crate) fn write_region(&mut self, wr: &mut WriteRegion) -> SpinorError {
/*let log_level = log::max_level();
if wr.start >= 0x27_5000 && wr.start <= 0x28_8000 { // trigger a debug if we are doing a certain type of transaction
log::set_max_level(log::LevelFilter::Trace);
}*/
// 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
return SpinorError::InvalidRequest;
@ -406,7 +412,7 @@ mod implementation {
return SpinorError::AlignmentError;
}
self.cur_op = Some(FlashOp::EraseSector(wr.start));
log::trace!("erase: {:x?}", self.cur_op);
log::trace!("erase: {:x?}", wr.start);
let erase_result = self.call_spinor_context_blocking();
if erase_result & 0x40 != 0 {
log::error!("E_FAIL set, erase failed: result 0x{:02x}, sector addr 0x{:08x}", erase_result, wr.start);
@ -415,12 +421,16 @@ mod implementation {
// now write the data sector
self.cur_op = Some(FlashOp::WritePages(wr.start, wr.data, wr.len as usize));
log::trace!("write: len:{}, {:x?}", wr.len, self.cur_op);
log::trace!("write: len:{}, start:{:x}", wr.len, wr.start);
//let logsize = if wr.len < 0x80 { wr.len as usize } else { 0x80 };
//log::trace!("write data begin: {:02x?}", &wr.data[..logsize]);
//log::trace!("write data end: {:02x?}", &wr.data[wr.len as usize - logsize..wr.len as usize]);
let write_result = self.call_spinor_context_blocking();
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);
return SpinorError::WriteFailed;
}
//log::set_max_level(log_level);
SpinorError::NoError
} else {
// clean patch path:
@ -429,12 +439,16 @@ mod implementation {
// here, almost any data alignment and length is acceptable -- we can patch even just two bytes using
// this call.
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);
log::trace!("clean write: len:{}, start: {:x}", wr.len, wr.start);
//let logsize = if wr.len < 0x80 { wr.len as usize } else { 0x80 };
//log::trace!("clean write data begin: {:02x?}", &wr.data[..logsize]);
//log::trace!("clean write data end: {:02x?}", &wr.data[wr.len as usize - logsize..wr.len as usize]);
let write_result = self.call_spinor_context_blocking();
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);
return SpinorError::WriteFailed;
}
//log::set_max_level(log_level);
SpinorError::NoError
}
}

View file

@ -344,7 +344,10 @@ def auto_int(x):
def main():
parser = argparse.ArgumentParser(description="Update/upload to a Precursor device running Xous 0.8/0.9")
parser.add_argument(
"-s", "--soc", required=False, help="SoC gateware", type=str, nargs='?', metavar=('SoC gateware file'), const='../precursors/soc_csr.bin'
"--soc", required=False, help="SoC gateware. Note: this will overwrite any secret keys stored in your device!", type=str, nargs='?', metavar=('SoC gateware file'), const='../precursors/soc_csr.bin'
)
parser.add_argument(
"-s", "--staging", required=False, help="Stage an update to apply", type=str, nargs='?', metavar=('SoC gateware file for staging'), const='../precursors/soc_csr.bin'
)
parser.add_argument(
"-l", "--loader", required=False, help="Loader", type=str, nargs='?', metavar=('loader file'), const='../target/riscv32imac-unknown-none-elf/release/loader.bin'
@ -377,10 +380,10 @@ def main():
"-i", "--image", required=False, help="Manually specify an image and address. Offset is relative to bottom of flash.", type=str, nargs=2, metavar=('IMAGEFILE', 'ADDR')
)
parser.add_argument(
"-n", "--no-verify", help="Skip readback verification (may be necessary for large files to avoid WDT timeout). Only honored with -i.", action='store_true'
"--verify", help="Readback verification. May fail for large files due to WDT timeout.", default=False, action='store_true'
)
parser.add_argument(
"-f", "--force", help="Ignore gitrev version on SoC and try to burn an image anyways", action="store_true"
"--force", help="Ignore gitrev version on SoC and try to burn an image anyways", action="store_true"
)
parser.add_argument(
"--bounce", help="cycle the device through a reset", action="store_true"
@ -403,10 +406,10 @@ def main():
pc_usb = PrecursorUsb(dev)
if args.no_verify:
verify = False
else:
if args.verify:
verify = True
else:
verify = False
if args.peek:
pc_usb.peek(args.peek, display=True)
@ -430,6 +433,16 @@ def main():
pc_usb.load_csrs() # prime the CSR values
if "v0.8" in pc_usb.gitrev:
LOC_SOC = 0x00000000
LOC_STAGING= 0x00280000
LOC_LOADER = 0x00500000
LOC_KERNEL = 0x00980000
LOC_WF200 = 0x07F80000
LOC_EC = 0x07FCE000
LOC_AUDIO = 0x06340000
LEN_AUDIO = 0x01C40000
elif "v0.9" in pc_usb.gitrev:
LOC_SOC = 0x00000000
LOC_STAGING= 0x00280000
LOC_LOADER = 0x00500000
LOC_KERNEL = 0x00980000
LOC_WF200 = 0x07F80000
@ -439,6 +452,7 @@ def main():
elif args.force == True:
# try the v0.8 offsets
LOC_SOC = 0x00000000
LOC_STAGING= 0x00280000
LOC_LOADER = 0x00500000
LOC_KERNEL = 0x00980000
LOC_WF200 = 0x07F80000
@ -466,31 +480,40 @@ def main():
print("Staging EC firmware package '{}' in SOC memory space...".format(args.ec))
with open(args.ec, "rb") as f:
image = f.read()
pc_usb.flash_program(LOC_EC, image)
pc_usb.flash_program(LOC_EC, image, verify=verify)
if args.wf200 != None:
print("Staging WF200 firmware package '{}' in SOC memory space...".format(args.wf200))
with open(args.wf200, "rb") as f:
image = f.read()
pc_usb.flash_program(LOC_WF200, image)
pc_usb.flash_program(LOC_WF200, image, verify=verify)
if args.staging != None:
print("Programming SoC gateware {}".format(args.soc))
with open(args.staging, "rb") as f:
image = f.read()
pc_usb.flash_program(LOC_STAGING, image, verify=verify)
if args.kernel != None:
print("Programming kernel image {}".format(args.kernel))
with open(args.kernel, "rb") as f:
image = f.read()
pc_usb.flash_program(LOC_KERNEL, image)
pc_usb.flash_program(LOC_KERNEL, image, verify=verify)
if args.loader != None:
print("Programming loader image {}".format(args.loader))
with open(args.loader, "rb") as f:
image = f.read()
pc_usb.flash_program(LOC_LOADER, image)
pc_usb.flash_program(LOC_LOADER, image, verify=verify)
if args.soc != None:
print("Programming SoC gateware {}".format(args.soc))
with open(args.soc, "rb") as f:
image = f.read()
pc_usb.flash_program(LOC_SOC, image)
print("This will overwrite any secret keys in your device. Continue? (y/n)")
confirm = input()
if len(confirm) > 0 and confirm.lower()[:1] == 'y':
print("Programming SoC gateware {}".format(args.soc))
with open(args.soc, "rb") as f:
image = f.read()
pc_usb.flash_program(LOC_SOC, image, verify=verify)
if args.audiotest != None:
print("Loading audio test clip {}".format(args.audiotest))
@ -499,7 +522,7 @@ def main():
if len(image) >= LEN_AUDIO:
print("audio file is too long, aborting audio burn!")
else:
pc_usb.flash_program(LOC_AUDIO, image)
pc_usb.flash_program(LOC_AUDIO, image, verify=verify)
print("Resuming CPU.")
pc_usb.poke(vexdbg_addr, 0x02000000)