initial take at converting loader to do fallback instead of fail-hard

This commit is contained in:
bunnie 2021-08-12 03:48:11 +08:00
parent 192109d09a
commit a59e4f995c

View file

@ -286,24 +286,41 @@ pub fn validate_xous_img(xous_img_offset: *const u32) -> bool {
// select the public key // select the public key
let mut keyrom = Keyrom::new(); let mut keyrom = Keyrom::new();
let keyloc = 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_zero(KeyLoc::SelfSignPub) { // self-signing key takes priority
if keyrom.key_is_dev(KeyLoc::SelfSignPub) { if keyrom.key_is_dev(KeyLoc::SelfSignPub) {
println!("Self-signed key slot, but with developer public key."); 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.msg("DEVELOPER KEY DETECTED\n\r", &mut cursor);
gfx.set_devboot(); gfx.set_devboot();
} }
println!("Using self-signed public key"); } else {
KeyLoc::SelfSignPub keyloc = KeyLoc::ThirdPartyPub;
} else if !keyrom.key_is_zero(KeyLoc::ThirdPartyPub) { // third party key is second in line 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) { if keyrom.key_is_dev(KeyLoc::ThirdPartyPub) {
println!("Third party public key slot, but with developer public key."); println!("Third party public key slot, but with developer public key.");
gfx.msg("DEVELOPER KEY DETECTED\n\r", &mut cursor); gfx.msg("DEVELOPER KEY DETECTED\n\r", &mut cursor);
gfx.set_devboot();
} }
println!("Using third-party public key");
KeyLoc::ThirdPartyPub
} else { } else {
keyloc = KeyLoc::DevPub;
continue;
}
},
KeyLoc::DevPub => {
if keyrom.key_is_zero(KeyLoc::DevPub) { if keyrom.key_is_zero(KeyLoc::DevPub) {
gfx.msg("Can't boot: No valid keys!", &mut cursor); gfx.msg("Can't boot: No valid keys!", &mut cursor);
loop {} loop {}
@ -311,8 +328,8 @@ pub fn validate_xous_img(xous_img_offset: *const u32) -> bool {
gfx.msg("DEVELOPER KEY DETECTED\n\r", &mut cursor); gfx.msg("DEVELOPER KEY DETECTED\n\r", &mut cursor);
println!("Using developer public key"); println!("Using developer public key");
gfx.set_devboot(); gfx.set_devboot();
KeyLoc::DevPub }
}; }
let pubkey = keyrom.read_ed25519(keyloc).unwrap_or_else(|e| { let pubkey = keyrom.read_ed25519(keyloc).unwrap_or_else(|e| {
gfx.msg("Unable to read key: ", &mut cursor); gfx.msg("Unable to read key: ", &mut cursor);
gfx.msg(e, &mut cursor); gfx.msg(e, &mut cursor);
@ -333,7 +350,21 @@ pub fn validate_xous_img(xous_img_offset: *const u32) -> bool {
if pubkey.verify(image, &ed25519_signature).is_ok() { if pubkey.verify(image, &ed25519_signature).is_ok() {
gfx.msg("Signature check passed\n\r", &mut cursor); gfx.msg("Signature check passed\n\r", &mut cursor);
println!("Signature check passed"); println!("Signature check passed");
break;
} else { } else {
// 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); gfx.msg("Signature check failed, powering down\n\r", &mut cursor);
println!("Signature check failed"); println!("Signature check failed");
let ticktimer = CSR::new(utra::ticktimer::HW_TICKTIMER_BASE as *mut u32); let ticktimer = CSR::new(utra::ticktimer::HW_TICKTIMER_BASE as *mut u32);
@ -356,6 +387,9 @@ pub fn validate_xous_img(xous_img_offset: *const u32) -> bool {
} }
} }
} }
}
}
}
// check the stack usage // check the stack usage
let stack: &[u32] = unsafe{core::slice::from_raw_parts(STACK_TOP as *const u32, (STACK_LEN as usize / core::mem::size_of::<u32>()) as usize)}; let stack: &[u32] = unsafe{core::slice::from_raw_parts(STACK_TOP as *const u32, (STACK_LEN as usize / core::mem::size_of::<u32>()) as usize)};