2021-07-04 16:08:58 +00:00
|
|
|
#![cfg_attr(target_os = "none", no_std)]
|
|
|
|
|
#![cfg_attr(target_os = "none", no_main)]
|
|
|
|
|
|
|
|
|
|
mod api;
|
2021-07-13 14:28:46 +00:00
|
|
|
use api::*;
|
2021-07-04 16:08:58 +00:00
|
|
|
|
2021-07-15 15:00:50 +00:00
|
|
|
use num_traits::*;
|
2021-07-13 14:28:46 +00:00
|
|
|
use xous_ipc::Buffer;
|
2021-07-15 15:00:50 +00:00
|
|
|
use xous::{msg_blocking_scalar_unpack, msg_scalar_unpack};
|
2021-07-04 16:08:58 +00:00
|
|
|
|
2021-07-13 14:28:46 +00:00
|
|
|
use core::sync::atomic::{AtomicBool, Ordering};
|
2021-08-02 10:11:48 +00:00
|
|
|
#[cfg(any(target_os = "none", target_os = "xous"))]
|
2021-07-04 16:08:58 +00:00
|
|
|
mod implementation {
|
|
|
|
|
use utralib::generated::*;
|
2021-07-13 14:28:46 +00:00
|
|
|
use crate::api::*;
|
2021-07-04 16:08:58 +00:00
|
|
|
use susres::{RegManager, RegOrField, SuspendResume};
|
2021-07-15 15:00:50 +00:00
|
|
|
use core::sync::atomic::{AtomicBool, AtomicU32, Ordering};
|
|
|
|
|
use num_traits::*;
|
2021-07-04 16:08:58 +00:00
|
|
|
|
2021-07-18 10:02:46 +00:00
|
|
|
#[derive(Debug)]
|
2021-07-15 15:00:50 +00:00
|
|
|
enum FlashOp {
|
|
|
|
|
EraseSector(u32), // 4k sector
|
2022-01-04 13:57:47 +00:00
|
|
|
EraseBlock(u32), // 64k block
|
2021-07-16 18:04:22 +00:00
|
|
|
WritePages(u32, [u8; 4096], usize), // page address, data, len
|
2021-07-15 15:00:50 +00:00
|
|
|
ReadId,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static SPINOR_RUNNING: AtomicBool = AtomicBool::new(false);
|
|
|
|
|
static SPINOR_RESULT: AtomicU32 = AtomicU32::new(0);
|
2021-07-13 16:01:09 +00:00
|
|
|
fn spinor_safe_context(_irq_no: usize, arg: *mut usize) {
|
|
|
|
|
let spinor = unsafe { &mut *(arg as *mut Spinor) };
|
|
|
|
|
|
2021-07-15 15:00:50 +00:00
|
|
|
let mut result = 0;
|
|
|
|
|
match spinor.cur_op {
|
2021-07-16 18:04:22 +00:00
|
|
|
Some(FlashOp::EraseSector(sector_address)) => {
|
|
|
|
|
// enable writes: set wren mode
|
|
|
|
|
loop {
|
|
|
|
|
flash_wren(&mut spinor.csr);
|
|
|
|
|
let status = flash_rdsr(&mut spinor.csr, 1);
|
|
|
|
|
if status & 0x02 != 0 {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// issue erase command
|
|
|
|
|
flash_se4b(&mut spinor.csr, sector_address);
|
|
|
|
|
// wait for WIP bit to drop
|
|
|
|
|
loop {
|
|
|
|
|
let status = flash_rdsr(&mut spinor.csr, 1);
|
|
|
|
|
if status & 0x01 == 0 {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// get the success code for return
|
|
|
|
|
result = flash_rdscur(&mut spinor.csr);
|
|
|
|
|
// disable writes: send wrdi
|
|
|
|
|
if flash_rdsr(&mut spinor.csr, 1) & 0x02 != 0 {
|
|
|
|
|
loop {
|
|
|
|
|
flash_wrdi(&mut spinor.csr);
|
|
|
|
|
let status = flash_rdsr(&mut spinor.csr, 1);
|
|
|
|
|
if status & 0x02 == 0 {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
flash_rdsr(&mut spinor.csr, 0); // dummy read to clear the "read lock" bit
|
2021-07-15 15:00:50 +00:00
|
|
|
},
|
2022-01-04 13:57:47 +00:00
|
|
|
Some(FlashOp::EraseBlock(block_address)) => {
|
|
|
|
|
// enable writes: set wren mode
|
|
|
|
|
loop {
|
|
|
|
|
flash_wren(&mut spinor.csr);
|
|
|
|
|
let status = flash_rdsr(&mut spinor.csr, 1);
|
|
|
|
|
if status & 0x02 != 0 {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// issue erase command
|
|
|
|
|
flash_be4b(&mut spinor.csr, block_address);
|
|
|
|
|
// wait for WIP bit to drop
|
|
|
|
|
loop {
|
|
|
|
|
let status = flash_rdsr(&mut spinor.csr, 1);
|
|
|
|
|
if status & 0x01 == 0 {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// get the success code for return
|
|
|
|
|
result = flash_rdscur(&mut spinor.csr);
|
|
|
|
|
// disable writes: send wrdi
|
|
|
|
|
if flash_rdsr(&mut spinor.csr, 1) & 0x02 != 0 {
|
|
|
|
|
loop {
|
|
|
|
|
flash_wrdi(&mut spinor.csr);
|
|
|
|
|
let status = flash_rdsr(&mut spinor.csr, 1);
|
|
|
|
|
if status & 0x02 == 0 {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
flash_rdsr(&mut spinor.csr, 0); // dummy read to clear the "read lock" bit
|
|
|
|
|
},
|
2021-07-16 18:04:22 +00:00
|
|
|
Some(FlashOp::WritePages(start_addr, data, len)) => {
|
|
|
|
|
// assumption: data being written to is already erased (set to 0xFF)
|
|
|
|
|
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;
|
2021-07-17 15:33:57 +00:00
|
|
|
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);
|
|
|
|
|
|
2021-07-17 18:36:31 +00:00
|
|
|
if pre_align >= len as u32 {
|
|
|
|
|
pre_align = len as u32;
|
2021-07-17 15:33:57 +00:00
|
|
|
more_aligned_pages = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let partial_page = &data[0..pre_align as usize];
|
2021-07-18 10:02:46 +00:00
|
|
|
// check for blank writes and skip
|
2021-07-16 18:04:22 +00:00
|
|
|
let mut blank = true;
|
2021-07-17 15:33:57 +00:00
|
|
|
for word in partial_page.chunks(2) {
|
|
|
|
|
// if the data is blank, don't do a write
|
2021-07-16 18:04:22 +00:00
|
|
|
let wdata = word[0] as u32 | ((word[1] as u32) << 8);
|
|
|
|
|
if wdata != 0xFFFF {
|
2021-07-17 15:33:57 +00:00
|
|
|
blank = false; // short circuit evaluation if we find anything that's not blank
|
|
|
|
|
break;
|
2021-07-16 18:04:22 +00:00
|
|
|
}
|
|
|
|
|
}
|
2021-07-17 15:33:57 +00:00
|
|
|
if !blank {
|
2021-07-17 18:36:31 +00:00
|
|
|
// enable writes: set wren mode
|
2021-07-17 15:33:57 +00:00
|
|
|
loop {
|
|
|
|
|
flash_wren(&mut spinor.csr);
|
|
|
|
|
let status = flash_rdsr(&mut spinor.csr, 1);
|
|
|
|
|
if status & 0x02 != 0 {
|
|
|
|
|
break;
|
|
|
|
|
}
|
2021-07-16 18:04:22 +00:00
|
|
|
}
|
2021-07-18 10:02:46 +00:00
|
|
|
// 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);
|
|
|
|
|
}
|
2021-07-17 15:33:57 +00:00
|
|
|
// 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);
|
2021-07-16 18:04:22 +00:00
|
|
|
}
|
2021-07-17 15:33:57 +00:00
|
|
|
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) {
|
2021-07-18 10:02:46 +00:00
|
|
|
// check & skip writes that are blank
|
2021-07-17 15:33:57 +00:00
|
|
|
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
|
2021-08-15 08:03:18 +00:00
|
|
|
cur_addr += page.len() as u32;
|
2021-07-17 15:33:57 +00:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
// enable writes: set wren mode
|
|
|
|
|
loop {
|
|
|
|
|
flash_wren(&mut spinor.csr);
|
|
|
|
|
let status = flash_rdsr(&mut spinor.csr, 1);
|
|
|
|
|
if status & 0x02 != 0 {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-07-18 10:02:46 +00:00
|
|
|
// 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);
|
|
|
|
|
}
|
2021-07-17 15:33:57 +00:00
|
|
|
// send the data to be programmed
|
|
|
|
|
flash_pp4b(&mut spinor.csr, cur_addr, page.len() as u32);
|
|
|
|
|
cur_addr += page.len() as u32;
|
2021-07-15 15:00:50 +00:00
|
|
|
|
2021-07-17 15:33:57 +00:00
|
|
|
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
|
|
|
|
|
}
|
2021-07-16 18:04:22 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// disable writes: send wrdi
|
|
|
|
|
if flash_rdsr(&mut spinor.csr, 1) & 0x02 != 0 {
|
|
|
|
|
loop {
|
|
|
|
|
flash_wrdi(&mut spinor.csr);
|
|
|
|
|
let status = flash_rdsr(&mut spinor.csr, 1);
|
|
|
|
|
if status & 0x02 == 0 {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
flash_rdsr(&mut spinor.csr, 0); // dummy read to clear the "read lock" bit
|
2021-07-15 15:00:50 +00:00
|
|
|
},
|
|
|
|
|
Some(FlashOp::ReadId) => {
|
|
|
|
|
let upper = flash_rdid(&mut spinor.csr, 2);
|
|
|
|
|
let lower = flash_rdid(&mut spinor.csr, 1);
|
|
|
|
|
// re-assemble the ID word from the duplicated bytes read
|
|
|
|
|
result = (lower & 0xFF) | ((lower >> 8) & 0xFF00) | (upper & 0xFF_0000);
|
|
|
|
|
},
|
|
|
|
|
None => {
|
|
|
|
|
panic!("Improper entry to SPINOR safe context.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
spinor.cur_op = None;
|
|
|
|
|
SPINOR_RESULT.store(result, Ordering::Relaxed);
|
2021-07-13 16:01:09 +00:00
|
|
|
spinor.softirq.wfo(utra::spinor_soft_int::EV_PENDING_SPINOR_INT, 1);
|
2021-07-15 15:00:50 +00:00
|
|
|
SPINOR_RUNNING.store(false, Ordering::Relaxed);
|
2021-07-13 16:01:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn ecc_handler(_irq_no: usize, arg: *mut usize) {
|
|
|
|
|
let spinor = unsafe { &mut *(arg as *mut Spinor) };
|
|
|
|
|
|
|
|
|
|
xous::try_send_message(spinor.handler_conn,
|
|
|
|
|
xous::Message::new_scalar(Opcode::EccError.to_usize().unwrap(),
|
|
|
|
|
spinor.csr.rf(utra::spinor::ECC_ADDRESS_ECC_ADDRESS) as usize,
|
|
|
|
|
spinor.csr.rf(utra::spinor::ECC_STATUS_ECC_OVERFLOW) as usize,
|
|
|
|
|
0, 0)).map(|_|()).unwrap();
|
|
|
|
|
|
|
|
|
|
spinor.csr.wfo(utra::spinor::EV_PENDING_ECC_ERROR, 1);
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-15 15:00:50 +00:00
|
|
|
fn flash_rdsr(csr: &mut utralib::CSR<u32>, lock_reads: u32) -> u32 {
|
|
|
|
|
csr.wfo(utra::spinor::CMD_ARG_CMD_ARG, 0);
|
|
|
|
|
csr.wo(utra::spinor::COMMAND,
|
|
|
|
|
csr.ms(utra::spinor::COMMAND_EXEC_CMD, 1)
|
|
|
|
|
| csr.ms(utra::spinor::COMMAND_LOCK_READS, lock_reads)
|
|
|
|
|
| csr.ms(utra::spinor::COMMAND_CMD_CODE, 0x05) // RDSR
|
|
|
|
|
| csr.ms(utra::spinor::COMMAND_DUMMY_CYCLES, 4)
|
|
|
|
|
| csr.ms(utra::spinor::COMMAND_DATA_WORDS, 1)
|
|
|
|
|
| csr.ms(utra::spinor::COMMAND_HAS_ARG, 1)
|
|
|
|
|
);
|
|
|
|
|
while csr.rf(utra::spinor::STATUS_WIP) != 0 {}
|
|
|
|
|
csr.r(utra::spinor::CMD_RBK_DATA)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn flash_rdscur(csr: &mut utralib::CSR<u32>) -> u32 {
|
|
|
|
|
csr.wfo(utra::spinor::CMD_ARG_CMD_ARG, 0);
|
|
|
|
|
csr.wo(utra::spinor::COMMAND,
|
|
|
|
|
csr.ms(utra::spinor::COMMAND_EXEC_CMD, 1)
|
|
|
|
|
| csr.ms(utra::spinor::COMMAND_LOCK_READS, 1)
|
|
|
|
|
| csr.ms(utra::spinor::COMMAND_CMD_CODE, 0x2B) // RDSCUR
|
|
|
|
|
| csr.ms(utra::spinor::COMMAND_DUMMY_CYCLES, 4)
|
|
|
|
|
| csr.ms(utra::spinor::COMMAND_DATA_WORDS, 1)
|
|
|
|
|
| csr.ms(utra::spinor::COMMAND_HAS_ARG, 1)
|
|
|
|
|
);
|
|
|
|
|
while csr.rf(utra::spinor::STATUS_WIP) != 0 {}
|
|
|
|
|
csr.r(utra::spinor::CMD_RBK_DATA)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn flash_rdid(csr: &mut utralib::CSR<u32>, offset: u32) -> u32 {
|
|
|
|
|
csr.wfo(utra::spinor::CMD_ARG_CMD_ARG, 0);
|
|
|
|
|
csr.wo(utra::spinor::COMMAND,
|
|
|
|
|
csr.ms(utra::spinor::COMMAND_EXEC_CMD, 1)
|
|
|
|
|
| csr.ms(utra::spinor::COMMAND_CMD_CODE, 0x9f) // RDID
|
|
|
|
|
| csr.ms(utra::spinor::COMMAND_DUMMY_CYCLES, 4)
|
|
|
|
|
| csr.ms(utra::spinor::COMMAND_DATA_WORDS, offset) // 2 -> 0x3b3b8080, // 1 -> 0x8080c2c2
|
|
|
|
|
| csr.ms(utra::spinor::COMMAND_HAS_ARG, 1)
|
|
|
|
|
);
|
|
|
|
|
while csr.rf(utra::spinor::STATUS_WIP) != 0 {}
|
|
|
|
|
csr.r(utra::spinor::CMD_RBK_DATA)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn flash_wren(csr: &mut utralib::CSR<u32>) {
|
|
|
|
|
csr.wfo(utra::spinor::CMD_ARG_CMD_ARG, 0);
|
|
|
|
|
csr.wo(utra::spinor::COMMAND,
|
|
|
|
|
csr.ms(utra::spinor::COMMAND_EXEC_CMD, 1)
|
|
|
|
|
| csr.ms(utra::spinor::COMMAND_CMD_CODE, 0x06) // WREN
|
|
|
|
|
| csr.ms(utra::spinor::COMMAND_LOCK_READS, 1)
|
|
|
|
|
);
|
|
|
|
|
while csr.rf(utra::spinor::STATUS_WIP) != 0 {}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn flash_wrdi(csr: &mut utralib::CSR<u32>) {
|
|
|
|
|
csr.wfo(utra::spinor::CMD_ARG_CMD_ARG, 0);
|
|
|
|
|
csr.wo(utra::spinor::COMMAND,
|
|
|
|
|
csr.ms(utra::spinor::COMMAND_EXEC_CMD, 1)
|
|
|
|
|
| csr.ms(utra::spinor::COMMAND_CMD_CODE, 0x04) // WRDI
|
|
|
|
|
| csr.ms(utra::spinor::COMMAND_LOCK_READS, 1)
|
|
|
|
|
);
|
|
|
|
|
while csr.rf(utra::spinor::STATUS_WIP) != 0 {}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn flash_se4b(csr: &mut utralib::CSR<u32>, sector_address: u32) {
|
|
|
|
|
csr.wfo(utra::spinor::CMD_ARG_CMD_ARG, sector_address);
|
|
|
|
|
csr.wo(utra::spinor::COMMAND,
|
|
|
|
|
csr.ms(utra::spinor::COMMAND_EXEC_CMD, 1)
|
|
|
|
|
| csr.ms(utra::spinor::COMMAND_CMD_CODE, 0x21) // SE4B
|
|
|
|
|
| csr.ms(utra::spinor::COMMAND_HAS_ARG, 1)
|
|
|
|
|
| csr.ms(utra::spinor::COMMAND_LOCK_READS, 1)
|
|
|
|
|
);
|
|
|
|
|
while csr.rf(utra::spinor::STATUS_WIP) != 0 {}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn flash_be4b(csr: &mut utralib::CSR<u32>, block_address: u32) {
|
|
|
|
|
csr.wfo(utra::spinor::CMD_ARG_CMD_ARG, block_address);
|
|
|
|
|
csr.wo(utra::spinor::COMMAND,
|
|
|
|
|
csr.ms(utra::spinor::COMMAND_EXEC_CMD, 1)
|
|
|
|
|
| csr.ms(utra::spinor::COMMAND_CMD_CODE, 0xdc) // BE4B
|
|
|
|
|
| csr.ms(utra::spinor::COMMAND_HAS_ARG, 1)
|
|
|
|
|
| csr.ms(utra::spinor::COMMAND_LOCK_READS, 1)
|
|
|
|
|
);
|
|
|
|
|
while csr.rf(utra::spinor::STATUS_WIP) != 0 {}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn flash_pp4b(csr: &mut utralib::CSR<u32>, address: u32, data_bytes: u32) {
|
2021-07-18 10:02:46 +00:00
|
|
|
let data_words = data_bytes / 2;
|
|
|
|
|
assert!(data_words <= 128, "data_words specified is longer than one page!");
|
2021-07-15 15:00:50 +00:00
|
|
|
csr.wfo(utra::spinor::CMD_ARG_CMD_ARG, address);
|
|
|
|
|
csr.wo(utra::spinor::COMMAND,
|
|
|
|
|
csr.ms(utra::spinor::COMMAND_EXEC_CMD, 1)
|
|
|
|
|
| csr.ms(utra::spinor::COMMAND_CMD_CODE, 0x12) // PP4B
|
|
|
|
|
| csr.ms(utra::spinor::COMMAND_HAS_ARG, 1)
|
2021-07-18 10:02:46 +00:00
|
|
|
| csr.ms(utra::spinor::COMMAND_DATA_WORDS, data_words)
|
2021-07-15 15:00:50 +00:00
|
|
|
| csr.ms(utra::spinor::COMMAND_LOCK_READS, 1)
|
|
|
|
|
);
|
|
|
|
|
while csr.rf(utra::spinor::STATUS_WIP) != 0 {}
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-04 16:08:58 +00:00
|
|
|
pub struct Spinor {
|
2021-07-15 15:00:50 +00:00
|
|
|
id: u32,
|
2021-07-13 16:01:09 +00:00
|
|
|
handler_conn: xous::CID,
|
2021-07-04 16:08:58 +00:00
|
|
|
csr: utralib::CSR<u32>,
|
2021-07-13 16:01:09 +00:00
|
|
|
susres: RegManager::<{utra::spinor::SPINOR_NUMREGS}>,
|
|
|
|
|
softirq: utralib::CSR<u32>,
|
2021-07-15 15:00:50 +00:00
|
|
|
cur_op: Option<FlashOp>,
|
|
|
|
|
ticktimer: ticktimer_server::Ticktimer,
|
|
|
|
|
// TODO: refactor ecup command to use spinor to operate the reads
|
2021-07-04 16:08:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Spinor {
|
2021-07-13 16:01:09 +00:00
|
|
|
pub fn new(handler_conn: xous::CID) -> Spinor {
|
2021-07-04 16:08:58 +00:00
|
|
|
let csr = xous::syscall::map_memory(
|
2021-07-13 14:28:46 +00:00
|
|
|
xous::MemoryAddress::new(utra::spinor::HW_SPINOR_BASE),
|
2021-07-04 16:08:58 +00:00
|
|
|
None,
|
|
|
|
|
4096,
|
|
|
|
|
xous::MemoryFlags::R | xous::MemoryFlags::W,
|
|
|
|
|
)
|
2021-07-13 14:28:46 +00:00
|
|
|
.expect("couldn't map SPINOR CSR range");
|
2021-07-13 16:01:09 +00:00
|
|
|
let softirq = xous::syscall::map_memory(
|
|
|
|
|
xous::MemoryAddress::new(utra::spinor_soft_int::HW_SPINOR_SOFT_INT_BASE),
|
|
|
|
|
None,
|
|
|
|
|
4096,
|
|
|
|
|
xous::MemoryFlags::R | xous::MemoryFlags::W,
|
|
|
|
|
)
|
|
|
|
|
.expect("couldn't map SPINOR soft interrupt CSR range");
|
2021-07-04 16:08:58 +00:00
|
|
|
|
|
|
|
|
let mut spinor = Spinor {
|
2021-07-15 15:00:50 +00:00
|
|
|
id: 0,
|
2021-07-13 16:01:09 +00:00
|
|
|
handler_conn,
|
2021-07-04 16:08:58 +00:00
|
|
|
csr: CSR::new(csr.as_mut_ptr() as *mut u32),
|
2021-07-13 16:01:09 +00:00
|
|
|
softirq: CSR::new(softirq.as_mut_ptr() as *mut u32),
|
|
|
|
|
susres: RegManager::new(csr.as_mut_ptr() as *mut u32),
|
2021-07-15 15:00:50 +00:00
|
|
|
cur_op: None,
|
|
|
|
|
ticktimer: ticktimer_server::Ticktimer::new().unwrap(),
|
2021-07-04 16:08:58 +00:00
|
|
|
};
|
|
|
|
|
|
2021-07-13 16:01:09 +00:00
|
|
|
xous::claim_interrupt(
|
|
|
|
|
utra::spinor_soft_int::SPINOR_SOFT_INT_IRQ,
|
|
|
|
|
spinor_safe_context,
|
|
|
|
|
(&mut spinor) as *mut Spinor as *mut usize,
|
|
|
|
|
)
|
|
|
|
|
.expect("couldn't claim SPINOR irq");
|
|
|
|
|
spinor.softirq.wfo(utra::spinor_soft_int::EV_PENDING_SPINOR_INT, 1);
|
|
|
|
|
spinor.softirq.wfo(utra::spinor_soft_int::EV_ENABLE_SPINOR_INT, 1);
|
|
|
|
|
|
|
|
|
|
xous::claim_interrupt(
|
|
|
|
|
utra::spinor::SPINOR_IRQ,
|
|
|
|
|
ecc_handler,
|
|
|
|
|
(&mut spinor) as *mut Spinor as *mut usize,
|
|
|
|
|
)
|
|
|
|
|
.expect("couldn't claim SPINOR irq");
|
|
|
|
|
spinor.softirq.wfo(utra::spinor::EV_PENDING_ECC_ERROR, 1);
|
|
|
|
|
spinor.softirq.wfo(utra::spinor::EV_ENABLE_ECC_ERROR, 1);
|
|
|
|
|
spinor.susres.push_fixed_value(RegOrField::Reg(utra::spinor::EV_PENDING), 0xFFFF_FFFF);
|
|
|
|
|
spinor.susres.push(RegOrField::Reg(utra::spinor::EV_ENABLE), None);
|
|
|
|
|
|
2021-07-15 15:00:50 +00:00
|
|
|
// now populate the id field
|
|
|
|
|
spinor.cur_op = Some(FlashOp::ReadId);
|
|
|
|
|
SPINOR_RUNNING.store(true, Ordering::Relaxed);
|
|
|
|
|
spinor.softirq.wfo(utra::spinor_soft_int::SOFTINT_SOFTINT, 1);
|
|
|
|
|
while SPINOR_RUNNING.load(Ordering::Relaxed) {}
|
|
|
|
|
spinor.id = SPINOR_RESULT.load(Ordering::Relaxed);
|
|
|
|
|
|
2021-07-04 16:08:58 +00:00
|
|
|
spinor
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-15 15:00:50 +00:00
|
|
|
/// changes into the spinor interrupt handler context, which is "safe" for ROM operations because we guarantee
|
|
|
|
|
/// we don't touch the SPINOR block inside that interrupt context
|
2021-07-16 18:04:22 +00:00
|
|
|
/// we name it with _blocking suffix to remind ourselves that this op should full-block Xous, no exceptions, until the flash op is done.
|
|
|
|
|
fn call_spinor_context_blocking(&mut self) -> u32 {
|
2021-07-15 15:00:50 +00:00
|
|
|
if self.cur_op.is_none() {
|
2021-07-16 18:04:22 +00:00
|
|
|
log::error!("called with no spinor op set. This is an internal error...panicing!");
|
|
|
|
|
panic!("called with no spinor op set.");
|
2021-07-15 15:00:50 +00:00
|
|
|
}
|
|
|
|
|
self.ticktimer.ping_wdt();
|
|
|
|
|
SPINOR_RUNNING.store(true, Ordering::Relaxed);
|
|
|
|
|
self.softirq.wfo(utra::spinor_soft_int::SOFTINT_SOFTINT, 1);
|
|
|
|
|
while SPINOR_RUNNING.load(Ordering::Relaxed) {
|
|
|
|
|
// there is no timeout condition that makes sense. If we're in a very long flash op -- and they can take hundreds of ms --
|
|
|
|
|
// simply timing out and trying to move on could lead to hardware damage as we'd be accessing a ROM that is in progress.
|
|
|
|
|
// in other words: if the flash memory is broke, you're broke too, ain't nobody got time for that.
|
|
|
|
|
}
|
|
|
|
|
self.ticktimer.ping_wdt();
|
2021-07-18 08:04:44 +00:00
|
|
|
xous::arch::cache_flush(); // flush the CPU caches in case the SPINOR call modified memory that's cached
|
2021-07-15 15:00:50 +00:00
|
|
|
SPINOR_RESULT.load(Ordering::Relaxed)
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-17 18:36:31 +00:00
|
|
|
pub(crate) fn write_region(&mut self, wr: &mut WriteRegion) -> SpinorError {
|
2021-08-15 08:03:18 +00:00
|
|
|
/*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);
|
|
|
|
|
}*/
|
|
|
|
|
|
2021-07-18 10:02:46 +00:00
|
|
|
// log::trace!("processing write_region with {:x?}", wr);
|
2021-07-16 18:04:22 +00:00
|
|
|
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;
|
2021-07-15 15:00:50 +00:00
|
|
|
}
|
2021-07-13 14:28:46 +00:00
|
|
|
|
2021-07-16 18:04:22 +00:00
|
|
|
if !wr.clean_patch {
|
|
|
|
|
// the `lib.rs` side has ostensibly already done the following checks for us:
|
|
|
|
|
// - made sure there is some "dirty" data in this page
|
|
|
|
|
// - provided us with a copy of any existing data we want to replace after the patch within the given page
|
|
|
|
|
// - ensured that the request is aligned with an erase sector
|
|
|
|
|
let alignment_mask = SPINOR_ERASE_SIZE - 1;
|
|
|
|
|
if (wr.start & alignment_mask) != 0 {
|
|
|
|
|
return SpinorError::AlignmentError;
|
2021-07-15 15:00:50 +00:00
|
|
|
}
|
2021-07-16 18:04:22 +00:00
|
|
|
self.cur_op = Some(FlashOp::EraseSector(wr.start));
|
2021-08-15 08:03:18 +00:00
|
|
|
log::trace!("erase: {:x?}", wr.start);
|
2021-07-16 18:04:22 +00:00
|
|
|
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);
|
|
|
|
|
return SpinorError::EraseFailed;
|
2021-07-15 15:00:50 +00:00
|
|
|
}
|
2021-07-13 14:28:46 +00:00
|
|
|
|
2021-07-16 18:04:22 +00:00
|
|
|
// now write the data sector
|
|
|
|
|
self.cur_op = Some(FlashOp::WritePages(wr.start, wr.data, wr.len as usize));
|
2021-08-15 08:03:18 +00:00
|
|
|
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]);
|
2021-07-16 18:04:22 +00:00
|
|
|
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;
|
|
|
|
|
}
|
2021-08-15 08:03:18 +00:00
|
|
|
//log::set_max_level(log_level);
|
2021-07-16 18:04:22 +00:00
|
|
|
SpinorError::NoError
|
|
|
|
|
} else {
|
|
|
|
|
// clean patch path:
|
|
|
|
|
// we're just patching sectors -- the caller PROMISES the data has been erased already
|
|
|
|
|
// this function has no way of knowing, because we don't have read priveledges...
|
|
|
|
|
// 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));
|
2021-08-15 08:03:18 +00:00
|
|
|
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]);
|
2021-07-16 18:04:22 +00:00
|
|
|
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;
|
2021-07-15 15:00:50 +00:00
|
|
|
}
|
2021-08-15 08:03:18 +00:00
|
|
|
//log::set_max_level(log_level);
|
2021-07-16 18:04:22 +00:00
|
|
|
SpinorError::NoError
|
2021-07-15 15:00:50 +00:00
|
|
|
}
|
2021-07-13 14:28:46 +00:00
|
|
|
}
|
|
|
|
|
|
2022-01-04 13:57:47 +00:00
|
|
|
pub(crate) fn bulk_erase(&mut self, be: &mut BulkErase) -> SpinorError {
|
|
|
|
|
if (be.start & (SPINOR_BULK_ERASE_SIZE - 1)) != 0 {
|
|
|
|
|
log::warn!("Bulk erase start address is not block-aligned. Aborting.");
|
|
|
|
|
return SpinorError::AlignmentError;
|
|
|
|
|
}
|
|
|
|
|
if (be.len & (SPINOR_BULK_ERASE_SIZE - 1)) != 0 {
|
|
|
|
|
log::warn!("Bulk erase end address is not block-aligned. Aborting.");
|
|
|
|
|
return SpinorError::AlignmentError;
|
|
|
|
|
}
|
|
|
|
|
for block in (be.start..be.start + be.len).step_by(SPINOR_BULK_ERASE_SIZE as usize) {
|
|
|
|
|
self.cur_op = Some(FlashOp::EraseBlock(block));
|
|
|
|
|
log::trace!("bulk erase: {:x?}", block);
|
|
|
|
|
let erase_result = self.call_spinor_context_blocking();
|
|
|
|
|
if erase_result & 0x40 != 0 {
|
|
|
|
|
log::error!("E_FAIL set, erase failed: result 0x{:02x}, block addr 0x{:08x}", erase_result, block);
|
|
|
|
|
return SpinorError::EraseFailed;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return SpinorError::NoError
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-15 15:00:50 +00:00
|
|
|
pub fn suspend(&mut self) {
|
|
|
|
|
self.susres.suspend();
|
2021-07-13 14:28:46 +00:00
|
|
|
}
|
2021-07-15 15:00:50 +00:00
|
|
|
pub fn resume(&mut self) {
|
|
|
|
|
self.susres.resume();
|
|
|
|
|
self.softirq.wfo(utra::spinor_soft_int::EV_PENDING_SPINOR_INT, 1);
|
|
|
|
|
self.softirq.wfo(utra::spinor_soft_int::EV_ENABLE_SPINOR_INT, 1);
|
2021-07-13 14:28:46 +00:00
|
|
|
}
|
2021-07-04 16:08:58 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// a stub to try to avoid breaking hosted mode for as long as possible.
|
2021-08-02 10:11:48 +00:00
|
|
|
#[cfg(not(any(target_os = "none", target_os = "xous")))]
|
2021-07-04 16:08:58 +00:00
|
|
|
mod implementation {
|
2021-07-18 10:29:24 +00:00
|
|
|
use crate::api::*;
|
2021-07-04 16:08:58 +00:00
|
|
|
pub struct Spinor {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Spinor {
|
2021-07-18 10:29:24 +00:00
|
|
|
pub fn new(_conn: xous::CID) -> Spinor {
|
2021-07-04 16:08:58 +00:00
|
|
|
Spinor {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
pub fn suspend(&self) {
|
|
|
|
|
}
|
|
|
|
|
pub fn resume(&self) {
|
|
|
|
|
}
|
2021-07-18 10:29:24 +00:00
|
|
|
pub(crate) fn write_region(&mut self, _wr: &mut WriteRegion) -> SpinorError {
|
|
|
|
|
SpinorError::ImplementationError
|
|
|
|
|
}
|
2022-01-04 13:57:47 +00:00
|
|
|
pub(crate) fn bulk_erase(&mut self, _be: &mut BulkErase) -> SpinorError {
|
|
|
|
|
SpinorError::ImplementationError
|
|
|
|
|
}
|
2021-07-04 16:08:58 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-07-13 14:28:46 +00:00
|
|
|
static OP_IN_PROGRESS: AtomicBool = AtomicBool::new(false);
|
|
|
|
|
static SUSPEND_FAILURE: AtomicBool = AtomicBool::new(false);
|
|
|
|
|
static SUSPEND_PENDING: AtomicBool = AtomicBool::new(false);
|
|
|
|
|
|
|
|
|
|
fn susres_thread(sid0: usize, sid1: usize, sid2: usize, sid3: usize) {
|
|
|
|
|
let susres_sid = xous::SID::from_u32(sid0 as u32, sid1 as u32, sid2 as u32, sid3 as u32);
|
|
|
|
|
let xns = xous_names::XousNames::new().unwrap();
|
|
|
|
|
|
|
|
|
|
// register a suspend/resume listener
|
|
|
|
|
let sr_cid = xous::connect(susres_sid).expect("couldn't create suspend callback connection");
|
2022-01-10 05:45:56 +00:00
|
|
|
let mut susres = susres::Susres::new(Some(susres::SuspendOrder::Last), &xns, api::SusResOps::SuspendResume as u32, sr_cid).expect("couldn't create suspend/resume object");
|
2021-07-13 14:28:46 +00:00
|
|
|
|
2021-07-16 18:04:22 +00:00
|
|
|
let main_cid = xns.request_connection_blocking(api::SERVER_NAME_SPINOR).expect("couldn't connect to our main thread for susres coordination");
|
|
|
|
|
|
2021-07-13 14:28:46 +00:00
|
|
|
log::trace!("starting SPINOR suspend/resume manager loop");
|
|
|
|
|
loop {
|
|
|
|
|
let msg = xous::receive_message(susres_sid).unwrap();
|
|
|
|
|
log::trace!("Message: {:?}", msg);
|
|
|
|
|
match FromPrimitive::from_usize(msg.body.id()) {
|
|
|
|
|
Some(SusResOps::SuspendResume) => xous::msg_scalar_unpack!(msg, token, _, _, _, {
|
|
|
|
|
SUSPEND_PENDING.store(true, Ordering::Relaxed);
|
|
|
|
|
while OP_IN_PROGRESS.load(Ordering::Relaxed) {
|
|
|
|
|
xous::yield_slice();
|
|
|
|
|
}
|
2021-07-16 18:04:22 +00:00
|
|
|
xous::send_message(main_cid,
|
|
|
|
|
xous::Message::new_blocking_scalar(Opcode::SuspendInner.to_usize().unwrap(), 0, 0, 0, 0)).expect("couldn't send suspend message");
|
2021-07-13 14:28:46 +00:00
|
|
|
if susres.suspend_until_resume(token).expect("couldn't execute suspend/resume") == false {
|
|
|
|
|
SUSPEND_FAILURE.store(true, Ordering::Relaxed);
|
|
|
|
|
} else {
|
|
|
|
|
SUSPEND_FAILURE.store(false, Ordering::Relaxed);
|
|
|
|
|
}
|
2021-07-16 18:04:22 +00:00
|
|
|
xous::send_message(main_cid,
|
|
|
|
|
xous::Message::new_blocking_scalar(Opcode::ResumeInner.to_usize().unwrap(), 0, 0, 0, 0)).expect("couldn't send suspend message");
|
2021-07-13 14:28:46 +00:00
|
|
|
SUSPEND_PENDING.store(false, Ordering::Relaxed);
|
|
|
|
|
}),
|
|
|
|
|
Some(SusResOps::Quit) => {
|
|
|
|
|
log::info!("Received quit opcode, exiting!");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
None => {
|
|
|
|
|
log::error!("Received unknown opcode: {:?}", msg);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
xns.unregister_server(susres_sid).unwrap();
|
|
|
|
|
xous::destroy_server(susres_sid).unwrap();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-07-04 16:08:58 +00:00
|
|
|
#[xous::xous_main]
|
|
|
|
|
fn xmain() -> ! {
|
|
|
|
|
use crate::implementation::Spinor;
|
|
|
|
|
|
|
|
|
|
log_server::init_wait().unwrap();
|
2021-07-18 10:02:46 +00:00
|
|
|
log::set_max_level(log::LevelFilter::Info);
|
2021-07-13 14:28:46 +00:00
|
|
|
log::info!("my PID is {}", xous::process::id());
|
2021-07-04 16:08:58 +00:00
|
|
|
|
|
|
|
|
let xns = xous_names::XousNames::new().unwrap();
|
|
|
|
|
/*
|
|
|
|
|
Very important to track who has access to the SPINOR server, and limit access. Access to this server is essential for persistent rootkits.
|
|
|
|
|
Here is the list of servers allowed to access, and why:
|
|
|
|
|
- shellchat (for testing ONLY, remove once done)
|
2021-07-13 14:28:46 +00:00
|
|
|
- suspend/resume (for suspend locking/unlocking calls)
|
2021-08-03 08:54:42 +00:00
|
|
|
- keystore
|
2021-11-04 19:58:58 +00:00
|
|
|
- PDDB
|
2022-02-02 18:46:37 +00:00
|
|
|
- keyboard (for updating the key map setting, which needs to be loaded upstream of the PDDB)
|
2021-07-04 16:08:58 +00:00
|
|
|
*/
|
2022-02-02 18:46:37 +00:00
|
|
|
let spinor_sid = xns.register_name(api::SERVER_NAME_SPINOR, Some(5)).expect("can't register server");
|
2021-07-04 16:08:58 +00:00
|
|
|
log::trace!("registered with NS -- {:?}", spinor_sid);
|
|
|
|
|
|
2021-07-15 15:00:50 +00:00
|
|
|
let handler_conn = xous::connect(spinor_sid).expect("couldn't create interrupt handler callback connection");
|
|
|
|
|
let mut spinor = Spinor::new(handler_conn);
|
2021-07-04 16:08:58 +00:00
|
|
|
|
|
|
|
|
log::trace!("ready to accept requests");
|
|
|
|
|
|
2021-07-13 14:28:46 +00:00
|
|
|
// handle suspend/resume with a separate thread, which monitors our in-progress state
|
|
|
|
|
// we can't interrupt an erase or program operation, so the op MUST finish before we can suspend.
|
|
|
|
|
let susres_mgr_sid = xous::create_server().unwrap();
|
|
|
|
|
let (sid0, sid1, sid2, sid3) = susres_mgr_sid.to_u32();
|
|
|
|
|
xous::create_thread_4(susres_thread, sid0 as usize, sid1 as usize, sid2 as usize, sid3 as usize).expect("couldn't start susres handler thread");
|
|
|
|
|
|
2022-01-11 04:02:27 +00:00
|
|
|
let llio = llio::Llio::new(&xns);
|
2021-07-17 18:36:31 +00:00
|
|
|
|
2021-07-13 14:28:46 +00:00
|
|
|
let mut client_id: Option<[u32; 4]> = None;
|
2021-07-16 18:04:22 +00:00
|
|
|
let mut soc_token: Option<[u32; 4]> = None;
|
2021-07-15 15:00:50 +00:00
|
|
|
let mut ecc_errors: [Option<u32>; 4] = [None, None, None, None]; // just record the first few errors, until we can get `std` and a convenient Vec/Queue
|
2021-08-09 20:30:45 +00:00
|
|
|
let mut staging_write_protect: bool = false;
|
2021-07-04 16:08:58 +00:00
|
|
|
|
|
|
|
|
loop {
|
2021-07-13 14:28:46 +00:00
|
|
|
let mut msg = xous::receive_message(spinor_sid).unwrap();
|
2021-07-04 16:08:58 +00:00
|
|
|
match FromPrimitive::from_usize(msg.body.id()) {
|
2021-07-16 18:04:22 +00:00
|
|
|
Some(Opcode::SuspendInner) => msg_blocking_scalar_unpack!(msg, _, _, _, _, {
|
|
|
|
|
spinor.suspend();
|
|
|
|
|
xous::return_scalar(msg.sender, 1).unwrap();
|
|
|
|
|
}),
|
|
|
|
|
Some(Opcode::ResumeInner) => msg_blocking_scalar_unpack!(msg, _, _, _, _, {
|
|
|
|
|
spinor.resume();
|
|
|
|
|
xous::return_scalar(msg.sender, 1).unwrap();
|
|
|
|
|
}),
|
|
|
|
|
Some(Opcode::RegisterSocToken) => msg_scalar_unpack!(msg, id0, id1, id2, id3, {
|
|
|
|
|
// only the first process to claim it can have it!
|
|
|
|
|
// make sure to do it correctly at boot: this must be done extremely early in the
|
|
|
|
|
// boot process; any attempt to access this unit for functional ops before this is registered shall panic
|
|
|
|
|
// this is to mitigate a DoS attack on the legitimate registrar that gives way for the incorrect
|
|
|
|
|
// process to grab this token
|
|
|
|
|
if soc_token.is_none() {
|
|
|
|
|
soc_token = Some([id0 as u32, id1 as u32, id2 as u32, id3 as u32]);
|
|
|
|
|
}
|
|
|
|
|
}),
|
2021-08-09 20:30:45 +00:00
|
|
|
Some(Opcode::SetStagingWriteProtect) => msg_scalar_unpack!(msg, id0, id1, id2, id3, {
|
|
|
|
|
if let Some(token) = soc_token {
|
|
|
|
|
if token[0] == id0 as u32 && token[1] == id1 as u32 &&
|
|
|
|
|
token[2] == id2 as u32 && token[3] == id3 as u32 {
|
|
|
|
|
staging_write_protect = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}),
|
|
|
|
|
Some(Opcode::ClearStagingWriteProtect) => msg_scalar_unpack!(msg, id0, id1, id2, id3, {
|
|
|
|
|
if let Some(token) = soc_token {
|
|
|
|
|
if token[0] == id0 as u32 && token[1] == id1 as u32 &&
|
|
|
|
|
token[2] == id2 as u32 && token[3] == id3 as u32 {
|
|
|
|
|
staging_write_protect = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}),
|
2021-07-13 14:28:46 +00:00
|
|
|
Some(Opcode::AcquireExclusive) => msg_blocking_scalar_unpack!(msg, id0, id1, id2, id3, {
|
2021-07-16 18:04:22 +00:00
|
|
|
if soc_token.is_none() { // reject any ops until a soc token is registered
|
|
|
|
|
xous::return_scalar(msg.sender, 0).unwrap();
|
|
|
|
|
}
|
2021-07-13 14:28:46 +00:00
|
|
|
if client_id.is_none() && !SUSPEND_PENDING.load(Ordering::Relaxed) {
|
2021-07-16 18:04:22 +00:00
|
|
|
OP_IN_PROGRESS.store(true, Ordering::Relaxed); // lock out suspends when the exclusive lock is acquired
|
2021-07-17 18:36:31 +00:00
|
|
|
llio.wfi_override(true).expect("couldn't shut off WFI");
|
2021-07-13 14:28:46 +00:00
|
|
|
client_id = Some([id0 as u32, id1 as u32, id2 as u32, id3 as u32]);
|
|
|
|
|
log::trace!("giving {:x?} an exclusive lock", client_id);
|
|
|
|
|
SUSPEND_FAILURE.store(false, Ordering::Relaxed);
|
|
|
|
|
xous::return_scalar(msg.sender, 1).unwrap();
|
|
|
|
|
} else {
|
|
|
|
|
xous::return_scalar(msg.sender, 0).unwrap();
|
|
|
|
|
}
|
2021-07-04 16:08:58 +00:00
|
|
|
}),
|
2021-07-13 14:28:46 +00:00
|
|
|
Some(Opcode::ReleaseExclusive) => msg_blocking_scalar_unpack!(msg, _, _, _, _, {
|
|
|
|
|
client_id = None;
|
2021-07-16 18:04:22 +00:00
|
|
|
OP_IN_PROGRESS.store(false, Ordering::Relaxed);
|
2021-07-17 18:36:31 +00:00
|
|
|
llio.wfi_override(false).expect("couldn't restore WFI");
|
2021-07-13 14:28:46 +00:00
|
|
|
xous::return_scalar(msg.sender, 1).unwrap();
|
|
|
|
|
}),
|
|
|
|
|
Some(Opcode::AcquireSuspendLock) => msg_blocking_scalar_unpack!(msg, _, _, _, _, {
|
2021-07-16 18:04:22 +00:00
|
|
|
if client_id.is_none() && !OP_IN_PROGRESS.load(Ordering::Relaxed) {
|
2021-07-13 14:28:46 +00:00
|
|
|
SUSPEND_PENDING.store(true, Ordering::Relaxed);
|
|
|
|
|
xous::return_scalar(msg.sender, 1).expect("couldn't ack AcquireSuspendLock");
|
|
|
|
|
} else {
|
|
|
|
|
xous::return_scalar(msg.sender, 0).expect("couldn't ack AcquireSuspendLock");
|
|
|
|
|
}
|
|
|
|
|
}),
|
|
|
|
|
Some(Opcode::ReleaseSuspendLock) => msg_blocking_scalar_unpack!(msg, _, _, _, _, {
|
|
|
|
|
SUSPEND_PENDING.store(false, Ordering::Relaxed);
|
|
|
|
|
xous::return_scalar(msg.sender, 1).expect("couldn't ack ReleaseSuspendLock");
|
|
|
|
|
}),
|
|
|
|
|
Some(Opcode::WriteRegion) => {
|
|
|
|
|
let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) };
|
|
|
|
|
let mut wr = buffer.to_original::<WriteRegion, _>().unwrap();
|
2021-07-16 18:04:22 +00:00
|
|
|
let mut authorized = true;
|
|
|
|
|
if let Some(st) = soc_token {
|
2021-08-09 20:30:45 +00:00
|
|
|
if staging_write_protect && ((wr.start >= xous::SOC_REGION_LOC) && (wr.start < xous::LOADER_LOC)) ||
|
|
|
|
|
!staging_write_protect && ((wr.start >= xous::SOC_REGION_LOC) && (wr.start < xous::SOC_STAGING_GW_LOC)) {
|
2021-07-16 18:04:22 +00:00
|
|
|
// if only the holder of the ID that matches the SoC token can write to the SOC flash area
|
|
|
|
|
// other areas are not as strictly controlled because signature checks ostensibly should catch
|
|
|
|
|
// attempts to modify them. However, access to the gateware definition would allow one to rewrite
|
|
|
|
|
// the boot ROM, which would then change the trust root. Therefore, we check this region specifically.
|
|
|
|
|
if st != wr.id {
|
|
|
|
|
wr.result = Some(SpinorError::AccessDenied);
|
|
|
|
|
authorized = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// the soc token MUST be initialized early on, if not, something bad has happened.
|
|
|
|
|
wr.result = Some(SpinorError::AccessDenied);
|
|
|
|
|
authorized = false;
|
|
|
|
|
}
|
|
|
|
|
if authorized {
|
|
|
|
|
match client_id {
|
|
|
|
|
Some(id) => {
|
|
|
|
|
if wr.id == id {
|
|
|
|
|
wr.result = Some(spinor.write_region(&mut wr)); // note: this must reject out-of-bound length requests for security reasons
|
|
|
|
|
} else {
|
|
|
|
|
wr.result = Some(SpinorError::IdMismatch);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
_ => {
|
|
|
|
|
wr.result = Some(SpinorError::NoId);
|
2021-07-13 14:28:46 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
buffer.replace(wr).expect("couldn't return response code to WriteRegion");
|
|
|
|
|
},
|
2022-01-04 13:57:47 +00:00
|
|
|
Some(Opcode::BulkErase) => {
|
|
|
|
|
let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) };
|
|
|
|
|
let mut wr = buffer.to_original::<BulkErase, _>().unwrap();
|
|
|
|
|
// bounds check to within the PDDB region for bulk erases. Please use standard patching for other regions.
|
|
|
|
|
let authorized =
|
|
|
|
|
if (wr.start >= xous::PDDB_LOC) && ((wr.start + wr.len) <= (xous::PDDB_LOC + xous::PDDB_LEN)) {
|
|
|
|
|
true
|
|
|
|
|
} else {
|
|
|
|
|
false
|
|
|
|
|
};
|
|
|
|
|
if authorized {
|
|
|
|
|
match client_id {
|
|
|
|
|
Some(id) => {
|
|
|
|
|
if wr.id == id {
|
|
|
|
|
wr.result = Some(spinor.bulk_erase(&mut wr)); // note: this must reject out-of-bound length requests for security reasons
|
|
|
|
|
} else {
|
|
|
|
|
wr.result = Some(SpinorError::IdMismatch);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
_ => {
|
|
|
|
|
wr.result = Some(SpinorError::NoId);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
wr.result = Some(SpinorError::AccessDenied);
|
|
|
|
|
}
|
|
|
|
|
buffer.replace(wr).expect("couldn't return response code to WriteRegion");
|
|
|
|
|
}
|
2021-07-15 15:00:50 +00:00
|
|
|
Some(Opcode::EccError) => msg_scalar_unpack!(msg, address, _overflow, _, _, {
|
|
|
|
|
// just some stand-in code -- should probably do something more clever, e.g. a rolling log
|
|
|
|
|
// plus some error handling callback. But this is in the distant future once we have enough
|
|
|
|
|
// of a system to eventually create such errors...
|
|
|
|
|
log::error!("ECC error reported at 0x{:x}", address);
|
|
|
|
|
let mut saved = false;
|
|
|
|
|
for item in ecc_errors.iter_mut() {
|
|
|
|
|
if item.is_none() {
|
|
|
|
|
*item = Some(address as u32);
|
|
|
|
|
saved = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if !saved {
|
|
|
|
|
log::error!("ran out of slots to record ECC errors");
|
|
|
|
|
}
|
|
|
|
|
}),
|
2021-07-04 16:08:58 +00:00
|
|
|
None => {
|
|
|
|
|
log::error!("couldn't convert opcode");
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// clean up our program
|
|
|
|
|
log::trace!("main loop exit, destroying servers");
|
2021-07-13 14:28:46 +00:00
|
|
|
let quitconn = xous::connect(susres_mgr_sid).unwrap();
|
|
|
|
|
xous::send_message(quitconn, xous::Message::new_scalar(api::SusResOps::Quit.to_usize().unwrap(), 0, 0, 0, 0)).unwrap();
|
|
|
|
|
unsafe{xous::disconnect(quitconn).unwrap();}
|
|
|
|
|
|
2021-07-04 16:08:58 +00:00
|
|
|
xns.unregister_server(spinor_sid).unwrap();
|
|
|
|
|
xous::destroy_server(spinor_sid).unwrap();
|
|
|
|
|
log::trace!("quitting");
|
|
|
|
|
xous::terminate_process(0)
|
|
|
|
|
}
|