mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-15 08:30:02 +00:00
initial take at converting loader to do fallback instead of fail-hard
This commit is contained in:
parent
192109d09a
commit
a59e4f995c
1 changed files with 96 additions and 62 deletions
|
|
@ -286,73 +286,107 @@ pub fn validate_xous_img(xous_img_offset: *const u32) -> bool {
|
|||
|
||||
// select the public key
|
||||
let mut keyrom = Keyrom::new();
|
||||
let keyloc =
|
||||
if !keyrom.key_is_zero(KeyLoc::SelfSignPub) { // self-signing key takes priority
|
||||
if keyrom.key_is_dev(KeyLoc::SelfSignPub) {
|
||||
println!("Self-signed key slot, but with developer public key.");
|
||||
let mut keyloc = KeyLoc::SelfSignPub; // start from the self-sign key first, then work your way to less secure options
|
||||
loop {
|
||||
match keyloc {
|
||||
KeyLoc::SelfSignPub => {
|
||||
if !keyrom.key_is_zero(KeyLoc::SelfSignPub) { // self-signing key takes priority
|
||||
if keyrom.key_is_dev(KeyLoc::SelfSignPub) {
|
||||
println!("Self-signed key slot, but with developer public key.");
|
||||
// mainly to protect against devs who were debugging and just stuck a dev key in the secure slot, and forgot to remove it.
|
||||
gfx.msg("DEVELOPER KEY DETECTED\n\r", &mut cursor);
|
||||
gfx.set_devboot();
|
||||
}
|
||||
} else {
|
||||
keyloc = KeyLoc::ThirdPartyPub;
|
||||
continue;
|
||||
}
|
||||
},
|
||||
KeyLoc::ThirdPartyPub => {
|
||||
// policy note: set the devboot flag also if we're doing a thirdparty pubkey boot
|
||||
// reasoning: the purpose of the hash mark is to indicate if someone could have tampered
|
||||
// with the device. Once an update is installed, it should always be self-signed, as it
|
||||
// protects against the third party pubkey from being compromised and an alternate firmware
|
||||
// being installed with no visible warning. Hence, even tho thirdparty pubkey boots could
|
||||
// be more trusted, let's still flag it.
|
||||
gfx.set_devboot();
|
||||
if !keyrom.key_is_zero(KeyLoc::ThirdPartyPub) { // third party key is second in line
|
||||
if keyrom.key_is_dev(KeyLoc::ThirdPartyPub) {
|
||||
println!("Third party public key slot, but with developer public key.");
|
||||
gfx.msg("DEVELOPER KEY DETECTED\n\r", &mut cursor);
|
||||
}
|
||||
} else {
|
||||
keyloc = KeyLoc::DevPub;
|
||||
continue;
|
||||
}
|
||||
},
|
||||
KeyLoc::DevPub => {
|
||||
if keyrom.key_is_zero(KeyLoc::DevPub) {
|
||||
gfx.msg("Can't boot: No valid keys!", &mut cursor);
|
||||
loop {}
|
||||
}
|
||||
gfx.msg("DEVELOPER KEY DETECTED\n\r", &mut cursor);
|
||||
println!("Using developer public key");
|
||||
gfx.set_devboot();
|
||||
}
|
||||
println!("Using self-signed public key");
|
||||
KeyLoc::SelfSignPub
|
||||
} else if !keyrom.key_is_zero(KeyLoc::ThirdPartyPub) { // third party key is second in line
|
||||
if keyrom.key_is_dev(KeyLoc::ThirdPartyPub) {
|
||||
println!("Third party public key slot, but with developer public key.");
|
||||
gfx.msg("DEVELOPER KEY DETECTED\n\r", &mut cursor);
|
||||
gfx.set_devboot();
|
||||
}
|
||||
println!("Using third-party public key");
|
||||
KeyLoc::ThirdPartyPub
|
||||
}
|
||||
let pubkey = keyrom.read_ed25519(keyloc).unwrap_or_else(|e| {
|
||||
gfx.msg("Unable to read key: ", &mut cursor);
|
||||
gfx.msg(e, &mut cursor);
|
||||
gfx.msg("\n\r", &mut cursor);
|
||||
panic!("Unable to read key: {}", e)
|
||||
});
|
||||
|
||||
println!("Public key bytes: {:x?}", pubkey.as_bytes());
|
||||
|
||||
if sig.version != 1 {
|
||||
println!("Warning: mismatch on signature record version numbering. Ignoring and moving on...")
|
||||
}
|
||||
let signed_len = sig.signed_len;
|
||||
let image: &[u8] = unsafe{core::slice::from_raw_parts((xous_img_offset as usize + SIGBLOCK_SIZE) as *const u8, signed_len as usize)};
|
||||
let ed25519_signature = ed25519_dalek::Signature::from(sig.signature);
|
||||
|
||||
use ed25519_dalek::Verifier;
|
||||
if pubkey.verify(image, &ed25519_signature).is_ok() {
|
||||
gfx.msg("Signature check passed\n\r", &mut cursor);
|
||||
println!("Signature check passed");
|
||||
break;
|
||||
} else {
|
||||
if keyrom.key_is_zero(KeyLoc::DevPub) {
|
||||
gfx.msg("Can't boot: No valid keys!", &mut cursor);
|
||||
loop {}
|
||||
}
|
||||
gfx.msg("DEVELOPER KEY DETECTED\n\r", &mut cursor);
|
||||
println!("Using developer public key");
|
||||
gfx.set_devboot();
|
||||
KeyLoc::DevPub
|
||||
};
|
||||
let pubkey = keyrom.read_ed25519(keyloc).unwrap_or_else(|e| {
|
||||
gfx.msg("Unable to read key: ", &mut cursor);
|
||||
gfx.msg(e, &mut cursor);
|
||||
gfx.msg("\n\r", &mut cursor);
|
||||
panic!("Unable to read key: {}", e)
|
||||
});
|
||||
// signature didn't work out, setup the next key and try it
|
||||
match keyloc {
|
||||
KeyLoc::SelfSignPub => {
|
||||
keyloc = KeyLoc::ThirdPartyPub;
|
||||
continue;
|
||||
}
|
||||
KeyLoc::ThirdPartyPub => {
|
||||
// try another key and move on
|
||||
keyloc = KeyLoc::DevPub;
|
||||
continue;
|
||||
}
|
||||
KeyLoc::DevPub => {
|
||||
// we're out of keys, display a message and try to power down
|
||||
gfx.msg("Signature check failed, powering down\n\r", &mut cursor);
|
||||
println!("Signature check failed");
|
||||
let ticktimer = CSR::new(utra::ticktimer::HW_TICKTIMER_BASE as *mut u32);
|
||||
let mut power = CSR::new(utra::power::HW_POWER_BASE as *mut u32);
|
||||
let mut com = CSR::new(utra::com::HW_COM_BASE as *mut u32);
|
||||
let mut start = ticktimer.rf(utra::ticktimer::TIME0_TIME);
|
||||
loop {
|
||||
// every 15 seconds, attempt to send a power down command
|
||||
// any attempt to re-flash the system must halt the CPU before we time-out to this point!
|
||||
if ticktimer.rf(utra::ticktimer::TIME0_TIME) - start > 15_000 {
|
||||
println!("Powering down...");
|
||||
power.rmwf(utra::power::POWER_STATE, 0);
|
||||
power.rmwf(utra::power::POWER_SELF, 0);
|
||||
|
||||
println!("Public key bytes: {:x?}", pubkey.as_bytes());
|
||||
|
||||
if sig.version != 1 {
|
||||
println!("Warning: mismatch on signature record version numbering. Ignoring and moving on...")
|
||||
}
|
||||
let signed_len = sig.signed_len;
|
||||
let image: &[u8] = unsafe{core::slice::from_raw_parts((xous_img_offset as usize + SIGBLOCK_SIZE) as *const u8, signed_len as usize)};
|
||||
let ed25519_signature = ed25519_dalek::Signature::from(sig.signature);
|
||||
|
||||
use ed25519_dalek::Verifier;
|
||||
if pubkey.verify(image, &ed25519_signature).is_ok() {
|
||||
gfx.msg("Signature check passed\n\r", &mut cursor);
|
||||
println!("Signature check passed");
|
||||
} else {
|
||||
gfx.msg("Signature check failed, powering down\n\r", &mut cursor);
|
||||
println!("Signature check failed");
|
||||
let ticktimer = CSR::new(utra::ticktimer::HW_TICKTIMER_BASE as *mut u32);
|
||||
let mut power = CSR::new(utra::power::HW_POWER_BASE as *mut u32);
|
||||
let mut com = CSR::new(utra::com::HW_COM_BASE as *mut u32);
|
||||
let mut start = ticktimer.rf(utra::ticktimer::TIME0_TIME);
|
||||
loop {
|
||||
// every 15 seconds, attempt to send a power down command
|
||||
// any attempt to re-flash the system must halt the CPU before we time-out to this point!
|
||||
if ticktimer.rf(utra::ticktimer::TIME0_TIME) - start > 15_000 {
|
||||
println!("Powering down...");
|
||||
power.rmwf(utra::power::POWER_STATE, 0);
|
||||
power.rmwf(utra::power::POWER_SELF, 0);
|
||||
|
||||
// ship mode is the safest mode -- suitable for long-term storage (~years)
|
||||
com.wfo(utra::com::TX_TX, com_rs::ComState::POWER_SHIPMODE.verb as u32);
|
||||
while com.rf(utra::com::STATUS_TIP) == 1 {}
|
||||
let _ = com.rf(utra::com::RX_RX); // discard the RX result
|
||||
start = ticktimer.rf(utra::ticktimer::TIME0_TIME);
|
||||
// ship mode is the safest mode -- suitable for long-term storage (~years)
|
||||
com.wfo(utra::com::TX_TX, com_rs::ComState::POWER_SHIPMODE.verb as u32);
|
||||
while com.rf(utra::com::STATUS_TIP) == 1 {}
|
||||
let _ = com.rf(utra::com::RX_RX); // discard the RX result
|
||||
start = ticktimer.rf(utra::ticktimer::TIME0_TIME);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue