From 19e16aea29e8548e984193de958e8b77507de327 Mon Sep 17 00:00:00 2001 From: bunnie Date: Sat, 10 Dec 2022 18:56:59 +0800 Subject: [PATCH] fix jtag fuse burning & add diagnostic messages - msb/lsb swap on efuse bank fixed - hazardous-debug feauter gates added - dry-run for "testing only" added --- services/jtag/Cargo.toml | 4 +++- services/jtag/src/efuse_api.rs | 12 ++++++++++-- services/jtag/src/lib.rs | 13 +++++++++++-- services/jtag/src/main.rs | 30 ++++++++++++++++++++++-------- 4 files changed, 46 insertions(+), 13 deletions(-) diff --git a/services/jtag/Cargo.toml b/services/jtag/Cargo.toml index 4cef49758..612a47418 100644 --- a/services/jtag/Cargo.toml +++ b/services/jtag/Cargo.toml @@ -26,4 +26,6 @@ precursor = ["utralib/precursor"] hosted = ["utralib/hosted"] renode = ["utralib/renode"] hazardous-debug = [] -default = ["hazardous-debug"] +dvt = [] +dry-run = [] # just prints keys but doesn't burn them +default = [] diff --git a/services/jtag/src/efuse_api.rs b/services/jtag/src/efuse_api.rs index 46567597f..7e01ebc16 100644 --- a/services/jtag/src/efuse_api.rs +++ b/services/jtag/src/efuse_api.rs @@ -111,7 +111,8 @@ impl EfusePhy { } // derive bits from bank data, to debug any bit-order issues on readout, etc. for index in 0..32 { - self.key[index] = ((self.banks[((index / 3) + 1) as usize] >> ((index % 3) * 8)) & 0xFF) as u8; + // key reversal from hw storage state is handled here + self.key[31 - index] = ((self.banks[((index / 3) + 1) as usize] >> ((index % 3) * 8)) & 0xFF) as u8; } jm.pause(2000); @@ -197,11 +198,14 @@ impl EfuseApi { // synchronizes the API state with the hardware. Needs to be called first. pub fn fetch(&mut self, jm: &mut JtagMach) { self.phy.fetch(jm); + self.key = self.phy.key(); + self.user = self.phy.user(); + self.cntl = self.phy.cntl(); } pub fn set_key(&mut self, new_key: [u8; 32]) { for i in 0..32 { - self.key[i] = new_key[i]; + self.key[i] = new_key[31 - i]; // key is reversed in byte order } } pub fn set_user(&mut self, new_user: u32) { self.user = new_user; } @@ -216,16 +220,19 @@ impl EfuseApi { // handle cntl special case if ((self.phy.banks[0] & 0x3F) as u8 ^ self.cntl) & (self.phy.banks[0] & 0x3F) as u8 != 0 { valid = false; + log::warn!("proposed efuse cntl setting is not valid {:x?} -> {:x?}", self.phy.banks[0] & 0x3F, self.cntl); } } else if index == 12 { // handle user special case if ((self.phy.banks[index] ^ add_ecc(self.user >> 8)) & self.phy.banks[index]) != 0 { + log::warn!("proposed efuse user setting is not valid {:x?} -> {:x?}", self.phy.banks[index], add_ecc(self.user >> 8)); valid = false; } } else if index == 11 { // handle user + key special case let raw_fuse: u32 = ((self.user & 0xFF) << 16) | (self.key[31] as u32) << 8 | self.key[30] as u32; if ((self.phy.banks[index] ^ add_ecc(raw_fuse)) & self.phy.banks[index]) != 0 { + log::warn!("proposed user/key overlap area setting is not valid {:x?} -> {:x?}", self.phy.banks[index], add_ecc(raw_fuse)); valid = false; } } else { @@ -236,6 +243,7 @@ impl EfuseApi { raw_fuse |= self.key[(index-1)*3 + 2-i] as u32; } if ((self.phy.banks[index] ^ add_ecc(raw_fuse)) & self.phy.banks[index]) != 0 { + log::warn!("proposed key setting is not valid {:x?} -> {:x?}", self.phy.banks[index], add_ecc(raw_fuse)); valid = false; } } diff --git a/services/jtag/src/lib.rs b/services/jtag/src/lib.rs index 232835181..d62c9d669 100644 --- a/services/jtag/src/lib.rs +++ b/services/jtag/src/lib.rs @@ -134,8 +134,17 @@ impl Jtag { pub fn burn_control_write_protect(&self) -> Result { self.mod_ctl_efuse(0x20) } - pub fn seal_device_forever(&self) -> Result { - self.mod_ctl_efuse(0x3F) + /// Sets all the fuses, except `disable_jtag` and `control_write_protect`. + /// This forces the device to: + /// - only boot from an encrypted image (so unbricking via JTAG requires you to provide an encryption key) + /// - key update disallowed + /// - key readback disallowed + /// - user bits can be read out (but they can't be updated) + /// - control bits can still be blown (so you can still set `disable_jtag` later on; + /// however, it is physically impossible to unset bits, so it is not possible to + /// enable key readback despite being able to write bits) + pub fn seal_device(&self) -> Result { + self.mod_ctl_efuse(0b00_1101) } } diff --git a/services/jtag/src/main.rs b/services/jtag/src/main.rs index 0de13bd7c..fbb52601b 100644 --- a/services/jtag/src/main.rs +++ b/services/jtag/src/main.rs @@ -510,8 +510,10 @@ fn main() -> ! { // - one connection from the key server // - one connection from shellchat for command line // - another connection from shellchat for oqc testing - #[cfg(any(feature="precursor", feature="renode"))] + #[cfg(all(any(feature="precursor", feature="renode"), not(feature="dvt")))] let jtag_sid = xns.register_name(api::SERVER_NAME_JTAG, Some(3)).expect("can't register server"); + #[cfg(all(any(feature="precursor", feature="renode"), feature="dvt"))] // dvt build has less in it + let jtag_sid = xns.register_name(api::SERVER_NAME_JTAG, Some(2)).expect("can't register server"); #[cfg(not(target_os = "xous"))] let jtag_sid = xns.register_name(api::SERVER_NAME_JTAG, Some(2)).expect("can't register server"); log::trace!("registered with NS -- {:?}", jtag_sid); @@ -585,9 +587,7 @@ fn main() -> ! { efuse_rec.user = efuse.phy_user(); efuse_rec.cntl = efuse.phy_cntl(); - for (&src, dst) in efuse.phy_key().iter().zip(efuse_rec.key.iter_mut()) { - *dst = src; - } + efuse_rec.key.copy_from_slice(&efuse.phy_key()); buffer.replace(efuse_rec).unwrap(); } Some(Opcode::WriteIr) => msg_scalar_unpack!(msg, ir, _, _, _, { @@ -607,11 +607,17 @@ fn main() -> ! { // refresh the efuse cache efuse.fetch(&mut jtag); #[cfg(feature = "hazardous-debug")] - log::info!("attempting to burn key: {:x?}", key); + log::info!("attempting to burn key: {:x?}", key); // this is easier for me to read as a human + #[cfg(feature = "hazardous-debug")] + log::info!("attempting to burn key: {:?}", key); // this is easier for import to python efuse.set_key(key); if efuse.is_valid() { log::info!("efuse key is valid to burn, proceeding. There is no return..."); - efuse.burn(&mut jtag); + if cfg!(feature = "dry-run") { + log::info!("Dry run selected, key NOT BURNED. Device will be in an inconsistent state."); + } else { + efuse.burn(&mut jtag); + } buffer.replace(EfuseResult::Success).unwrap(); } else { log::error!("efuses already burned, new key is unpatchable. Refusing to burn!"); @@ -625,7 +631,11 @@ fn main() -> ! { efuse.set_cntl(ctl as u8); if efuse.is_valid() { log::info!("control is valid to burn, proceeding. There is no return..."); - efuse.burn(&mut jtag); + if cfg!(feature = "dry-run") { + log::info!("Dry run selected, control fuses NOT BURNED. Device will be in an inconsistent state."); + } else { + efuse.burn(&mut jtag); + } susres.set_suspendable(true).unwrap(); xous::return_scalar(msg.sender, 1).unwrap(); } else { @@ -639,7 +649,11 @@ fn main() -> ! { efuse.set_user(user as u32); if efuse.is_valid() { log::info!("user fuses are valid to burn, proceeding. There is no return..."); - efuse.burn(&mut jtag); + if cfg!(feature = "dry-run") { + log::info!("Dry run selected, user fuse NOT BURNED. Device will be in an inconsistent state."); + } else { + efuse.burn(&mut jtag); + } susres.set_suspendable(true).unwrap(); xous::return_scalar(msg.sender, 1).unwrap(); } else {