reflect the latest fixes to the secure boot process

also patch up some issues with sha512 reset and loose
ends on docs and updates
This commit is contained in:
bunnie 2021-07-13 00:46:31 +08:00
parent 5d62af49a5
commit a4f8346c34
9 changed files with 134 additions and 71 deletions

View file

@ -2,7 +2,9 @@ use crate::println;
pub const SIGBLOCK_SIZE: usize = 0x1000;
const VERSION_STR: &'static str = "Xous OS Loader v0.9.0\n\r";
const VERSION_STR: &'static str = "Xous OS Loader v0.9.1\n\r";
// v0.9.0 -- initial version
// v0.9.1 -- booting with hw acceleration, and "simplest signature" check on the entire xous.img blob
pub const STACK_LEN: u32 = 8192 - (7 * 4); // 7 words for backup kernel args
pub const STACK_TOP: u32 = 0x4100_0000 - STACK_LEN;
@ -243,6 +245,11 @@ impl Keyrom {
// returns true if the kernel is valid
// side-effects the "devboot" register in the gfx engine if devkeys were detected
pub fn validate_xous_img(xous_img_offset: *const u32) -> bool {
// reset the SHA block, in case we're coming out of a warm reset
let mut sha = CSR::new(utra::sha512::HW_SHA512_BASE as *mut u32);
sha.wfo(utra::sha512::POWER_ON, 1);
sha.wfo(utra::sha512::CONFIG_RESET, 1); // this reset takes ~32 CPU cycles before we can use the SHA block. not a problem.
// conjure the signature struct directly out of memory. super unsafe.
let sig_ptr = xous_img_offset as *const SignatureInFlash;
let sig: &SignatureInFlash = unsafe{sig_ptr.as_ref().unwrap()};
@ -255,13 +262,10 @@ pub fn validate_xous_img(xous_img_offset: *const u32) -> bool {
};
gfx.init(100_000_000);
// extract the second half of the frame buffer with .chunks_mut(len()/2).skip(1).next().unwrap()
// then iterate through each word, along with an index .iter_mut().enumerate()
// then insert a pattern of alternating 0101/1010 to create a "gray effect" on the bottom half of the fb
// insert a pattern of alternating 0101/1010 to create a "gray effect" on the bottom half of the fb
// note that the gray has "stripes" every 32 bits but it's visually easier to look at than stripes every other bit
for (i, word) in
gfx.fb.chunks_mut(gfx.fb.len() / 2).skip(1).next().unwrap()
.iter_mut().enumerate() {
let (_top, bottom) = gfx.fb.split_at_mut(gfx.fb.len() / 2);
for (i, word) in bottom.iter_mut().enumerate() {
if i % 2 == 0 {
*word = 0xAAAA_AAAA;
} else {

View file

@ -3,7 +3,7 @@
<device schemaVersion="1.1" xmlns:xs="http://www.w3.org/2001/XMLSchema-instance" xs:noNamespaceSchemaLocation="CMSIS-SVD.xsd" >
<vendor>litex</vendor>
<name>SOC</name>
<description><![CDATA[Litex SoC 2021-07-12 14:56:28]]></description>
<description><![CDATA[Litex SoC 2021-07-12 23:54:59]]></description>
<addressUnitBits>8</addressUnitBits>
<width>32</width>
@ -433,11 +433,27 @@ configure as output for the value to appear on the pin]]></description>
</field>
</fields>
</register>
<register>
<name>USBDISABLE</name>
<description><![CDATA[When set to ``1``, USB debug is limited by remapping all wishbone request
addresses to 0x8000_0000]]></description>
<addressOffset>0x001c</addressOffset>
<resetValue>0x00</resetValue>
<size>32</size>
<fields>
<field>
<name>usbdisable</name>
<msb>0</msb>
<bitRange>[0:0]</bitRange>
<lsb>0</lsb>
</field>
</fields>
</register>
<register>
<name>EV_STATUS</name>
<description><![CDATA[This register contains the current raw level of the event7 event trigger.
Writes to this register have no effect.]]></description>
<addressOffset>0x001c</addressOffset>
<addressOffset>0x0020</addressOffset>
<resetValue>0x00</resetValue>
<size>32</size>
<fields>
@ -503,7 +519,7 @@ Writes to this register have no effect.]]></description>
<name>EV_PENDING</name>
<description><![CDATA[When a event7 event occurs, the corresponding bit will be set in this register.
To clear the Event, set the corresponding bit in this register.]]></description>
<addressOffset>0x0020</addressOffset>
<addressOffset>0x0024</addressOffset>
<resetValue>0x00</resetValue>
<size>32</size>
<fields>
@ -577,7 +593,7 @@ edge.]]></description>
<name>EV_ENABLE</name>
<description><![CDATA[This register enables the corresponding event7 events. Write a ``0`` to this
register to disable individual events.]]></description>
<addressOffset>0x0024</addressOffset>
<addressOffset>0x0028</addressOffset>
<resetValue>0x00</resetValue>
<size>32</size>
<fields>
@ -642,7 +658,7 @@ register to disable individual events.]]></description>
</registers>
<addressBlock>
<offset>0</offset>
<size>0x28</size>
<size>0x2c</size>
<usage>registers</usage>
</addressBlock>
<interrupt>
@ -1222,10 +1238,10 @@ register to disable individual events.]]></description>
<register>
<name>GIT_GITREV</name>
<description><![CDATA[First 32-bits of the git revision. This documentation was built from git rev
``9e37625a``, so this value is 2654429786, which should be enough to check out
``ab8658b6``, so this value is 2877708470, which should be enough to check out
the exact git version used to build this firmware.]]></description>
<addressOffset>0x0014</addressOffset>
<resetValue>0x9e37625a</resetValue>
<resetValue>0xab8658b6</resetValue>
<size>32</size>
<fields>
<field>
@ -1242,7 +1258,7 @@ the exact git version used to build this firmware.]]></description>
is ``1``, then the repository this was built from has one additional commit
beyond the tag indicated in `MAJOR`, `MINOR`, and `REVISION`.]]></description>
<addressOffset>0x0018</addressOffset>
<resetValue>0x3b</resetValue>
<resetValue>0x3e</resetValue>
<size>32</size>
<fields>
<field>
@ -3485,7 +3501,7 @@ inject too fast.]]></description>
<name>SEED1</name>
<description><![CDATA[Bits 32-63 of `SEED_SEED`. Seed used for the build]]></description>
<addressOffset>0x0000</addressOffset>
<resetValue>0x55bba5d1</resetValue>
<resetValue>0x2b72d267</resetValue>
<size>32</size>
<fields>
<field>
@ -3500,7 +3516,7 @@ inject too fast.]]></description>
<name>SEED0</name>
<description><![CDATA[Bits 0-31 of `SEED_SEED`.]]></description>
<addressOffset>0x0004</addressOffset>
<resetValue>0x15927727</resetValue>
<resetValue>0x2d8ab78</resetValue>
<size>32</size>
<fields>
<field>
@ -5865,6 +5881,13 @@ savings)]]></description>
<lsb>3</lsb>
<description><![CDATA[Select SHA512/256 IV constants when set to `1`]]></description>
</field>
<field>
<name>reset</name>
<msb>4</msb>
<bitRange>[4:4]</bitRange>
<lsb>4</lsb>
<description><![CDATA[Resets the hardware. Power must be on for this to take effect.]]></description>
</field>
</fields>
</register>
<register>
@ -6259,52 +6282,59 @@ in bits]]></description>
<size>32</size>
<fields>
<field>
<name>read_count</name>
<msb>8</msb>
<bitRange>[8:0]</bitRange>
<name>reset_status</name>
<msb>0</msb>
<bitRange>[0:0]</bitRange>
<lsb>0</lsb>
<description><![CDATA[when set, the block is undergoing a soft reset]]></description>
</field>
<field>
<name>read_count</name>
<msb>9</msb>
<bitRange>[9:1]</bitRange>
<lsb>1</lsb>
<description><![CDATA[read pointer]]></description>
</field>
<field>
<name>write_count</name>
<msb>17</msb>
<bitRange>[17:9]</bitRange>
<lsb>9</lsb>
<msb>18</msb>
<bitRange>[18:10]</bitRange>
<lsb>10</lsb>
<description><![CDATA[write pointer]]></description>
</field>
<field>
<name>read_error</name>
<msb>18</msb>
<bitRange>[18:18]</bitRange>
<lsb>18</lsb>
<msb>19</msb>
<bitRange>[19:19]</bitRange>
<lsb>19</lsb>
<description><![CDATA[read error occurred]]></description>
</field>
<field>
<name>write_error</name>
<msb>19</msb>
<bitRange>[19:19]</bitRange>
<lsb>19</lsb>
<msb>20</msb>
<bitRange>[20:20]</bitRange>
<lsb>20</lsb>
<description><![CDATA[write error occurred]]></description>
</field>
<field>
<name>almost_full</name>
<msb>20</msb>
<bitRange>[20:20]</bitRange>
<lsb>20</lsb>
<msb>21</msb>
<bitRange>[21:21]</bitRange>
<lsb>21</lsb>
<description><![CDATA[almost full]]></description>
</field>
<field>
<name>almost_empty</name>
<msb>21</msb>
<bitRange>[21:21]</bitRange>
<lsb>21</lsb>
<msb>22</msb>
<bitRange>[22:22]</bitRange>
<lsb>22</lsb>
<description><![CDATA[almost empty]]></description>
</field>
<field>
<name>running</name>
<msb>22</msb>
<bitRange>[22:22]</bitRange>
<lsb>22</lsb>
<msb>23</msb>
<bitRange>[23:23]</bitRange>
<lsb>23</lsb>
<description><![CDATA[hash engine is running and controls are locked out]]></description>
</field>
</fields>
@ -6964,7 +6994,7 @@ of the PLL]]></description>
<memoryRegion>
<name>SHA512</name>
<baseAddress>0xE0002000</baseAddress>
<size>0x00000008</size>
<size>0x00000004</size>
</memoryRegion>
<memoryRegion>
<name>ENGINE</name>

Binary file not shown.

View file

@ -65,7 +65,7 @@ mod implementation {
.expect("couldn't map Engine512 CSR range");
#[cfg(not(feature = "event_wait"))]
let engine512 = Engine512 {
let mut engine512 = Engine512 {
csr: CSR::new(csr.as_mut_ptr() as *mut u32),
fifo,
};
@ -87,6 +87,13 @@ mod implementation {
// instead, we rely on every usage of this mechanism to explicitly set this enable bit before relying upon it
engine512.csr.wfo(utra::sha512::EV_ENABLE_SHA512_DONE, 1);
}
// reset the block on boot
// we don't save this as part of the susres set because
// on a "proper" resume, the block was already reset by the secure boot process
// the code path where this really becomes necessary is if we have a WDT reset or
// some sort of server-restart.
engine512.csr.wfo(utra::sha512::CONFIG_RESET, 1); // takes ~32 cycles to complete
engine512
}
@ -206,33 +213,10 @@ mod implementation {
pub(crate) fn reset(&mut self) {
self.csr.wfo(utra::sha512::POWER_ON, 1);
if self.csr.rf(utra::sha512::FIFO_RUNNING) != 0 {
// if it's running, call digest, then reset
let sha = self.fifo.as_mut_ptr() as *mut u32;
unsafe { sha.write_volatile(0x0); } // stuff a dummy byte, in case the hash was empty
#[cfg(feature = "event_wait")]
{
engine512.csr.wfo(utra::sha512::EV_ENABLE_SHA512_DONE, 1);
self.done = false;
self.csr.wfo(utra::sha512::COMMAND_HASH_PROCESS, 1);
while !self.done {
log::trace!("waiting for sha512_done (reset)");
xous::wait_event();
}
log::trace!("moving on (reset)");
}
#[cfg(not(feature = "event_wait"))]
{
self.csr.wfo(utra::sha512::COMMAND_HASH_PROCESS, 1);
while self.csr.rf(utra::sha512::EV_PENDING_SHA512_DONE) == 0 {
xous::yield_slice();
}
}
} else {
// engine is already stopped, just clear the pending bits and reset the config
}
self.csr.wfo(utra::sha512::CONFIG_RESET, 1);
self.csr.wfo(utra::sha512::EV_PENDING_SHA512_DONE, 1);
self.csr.wo(utra::sha512::CONFIG, 0); // clear all config bits, including EN, which resets the unit
while self.csr.rf(utra::sha512::FIFO_RESET_STATUS) == 1 { } // wait for the reset block to finish, if it's not already done by now
self.csr.wfo(utra::sha512::POWER_ON, 0);
}

View file

@ -206,6 +206,7 @@ mod implementation {
xl.gpio_susres.push_fixed_value(RegOrField::Reg(utra::gpio::EV_PENDING), 0xFFFF_FFFF);
xl.gpio_susres.push(RegOrField::Reg(utra::gpio::EV_ENABLE), None);
xl.gpio_susres.push(RegOrField::Field(utra::gpio::UARTSEL_UARTSEL), None);
xl.gpio_susres.push(RegOrField::Reg(utra::gpio::USBDISABLE), None);
xl.event_susres.push_fixed_value(RegOrField::Reg(utra::btevents::EV_PENDING), 0xFFFF_FFFF);
xl.event_susres.push(RegOrField::Reg(utra::btevents::EV_ENABLE), None);
@ -446,6 +447,20 @@ mod implementation {
let value = if ena {1} else {0};
self.power_csr.rmwf(utra::power::EV_PENDING_USB_ATTACH, value);
}
pub fn get_usb_disable(&self) -> bool {
if self.gpio_csr.rf(utra::gpio::USBDISABLE_USBDISABLE) != 0 {
true
} else {
false
}
}
pub fn set_usb_disable(&mut self, state: bool) {
if state {
self.gpio_csr.wfo(utra::gpio::USBDISABLE_USBDISABLE, 1);
} else {
self.gpio_csr.wfo(utra::gpio::USBDISABLE_USBDISABLE, 0);
}
}
}
}
@ -538,6 +553,13 @@ mod implementation {
}
pub fn wfi_override(&mut self, _override_: bool) {
}
pub fn get_usb_disable(&self) -> bool {
false
}
pub fn set_usb_disable(&mut self, _state: bool) {
}
}
}
@ -861,15 +883,23 @@ fn xmain() -> ! {
log::debug!("activity/period: {}/{}, {:.2}%", latest_activity, period, (latest_activity as f32 / period as f32) * 100.0);
xous::return_scalar2(msg.sender, latest_activity as usize, period as usize).expect("couldn't return activity");
}),
Some(Opcode::DebugUsbOp) => msg_blocking_scalar_unpack!(msg, update_req, _new_state, _, _, {
Some(Opcode::DebugUsbOp) => msg_blocking_scalar_unpack!(msg, update_req, new_state, _, _, {
if update_req != 0 {
// do something with new_state: this has yet to be implemented
// if new_state is true, then try to lock the USB port
// if new_state is true (not 0), then try to lock the USB port
// if false, try to unlock the USB port
if new_state != 0 {
llio.set_usb_disable(true);
} else {
llio.set_usb_disable(false);
}
}
// at this point, *read back* the new state -- don't assume it "took". The readback is always based on
// a real hardware value and not the requested value. for now, always false.
let is_locked = 0; // for now, always 0
let is_locked = if llio.get_usb_disable() {
1
} else {
0
};
// this is a performance optimization. we could always redraw the status, but, instead we only redraw when
// the status has changed. However, there is an edge case: on a resume from suspend, the status needs a redraw,

View file

@ -96,6 +96,7 @@ mod trng_cmd; use trng_cmd::*;
mod engine; use engine::*;
mod console; use console::*;
mod memtest; use memtest::*;
mod keys; use keys::*;
//mod fcc; use fcc::*;
//mod pds; // dependency of the FCC file
@ -119,6 +120,7 @@ pub struct CmdEnv {
trng_cmd: TrngCmd,
engine_cmd: Engine,
memtest_cmd: Memtest,
keys_cmd: Keys,
//fcc_cmd: Fcc,
}
@ -169,6 +171,7 @@ impl CmdEnv {
trng_cmd: TrngCmd::new(),
engine_cmd: engine,
memtest_cmd: memtest,
keys_cmd: Keys::new(),
//fcc_cmd: fcc,
}
@ -203,6 +206,7 @@ impl CmdEnv {
&mut self.engine_cmd,
&mut console_cmd,
&mut self.memtest_cmd,
&mut self.keys_cmd,
//&mut self.fcc_cmd,
];

View file

@ -32,6 +32,7 @@ impl<'a> ShellCmdApi<'a> for Test {
write!(ret, "devboot on").unwrap();
}
"devbootoff" => {
// this should do nothing if devboot was already set
env.gam.set_devboot(false).unwrap();
write!(ret, "devboot off").unwrap();
}

View file

@ -58,8 +58,8 @@ The updater encodes the offsets of every binary location in the script. When the
then the updater script should no longer be used for a given Precursor revision.
```
usage: usb_update.py [-h] [-s [SoC gateware file]] [-l [loader file]] [-k [kernel file]] [-e [EC gateware package]]
[-w [WF200 firmware package]] [--peek ADDR] [--poke ADDR DATA] [--check-poke] [--config]
usage: usb_update.py [-h] [-s [SoC gateware file]] [-l [loader file]] [-k [kernel file]] [-e [EC gateware package]] [-w [WF200 firmware package]] [--audiotest [Test audio clip]] [--peek ADDR] [--poke ADDR DATA] [--check-poke]
[--config] [-i IMAGEFILE ADDR] [-n] [-f] [--bounce]
Update/upload to a Precursor device running Xous 0.8/0.9
@ -75,10 +75,17 @@ optional arguments:
EC gateware
-w [WF200 firmware package], --wf200 [WF200 firmware package]
WF200 firmware
--audiotest [Test audio clip]
Test audio clip (must be 8kHz WAV)
--peek ADDR Inspect an address
--poke ADDR DATA Write to an address
--check-poke Read data before and after the poke
--config Print the descriptor
-i IMAGEFILE ADDR, --image IMAGEFILE ADDR
Manually specify an image and address. Offset is relative to bottom of flash.
-n, --no-verify Skip readback verification (may be necessary for large files to avoid WDT timeout). Only honored with -i.
-f, --force Ignore gitrev version on SoC and try to burn an image anyways
--bounce cycle the device through a reset
```
You can try reading the first word out of the ROM to see if `usb_update.py` is working correctly.

View file

@ -382,6 +382,9 @@ def main():
parser.add_argument(
"-f", "--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"
)
args = parser.parse_args()
if not len(sys.argv) > 1: