From 7ad418be7cf95a5c3c0c63b3740b20f25db32130 Mon Sep 17 00:00:00 2001 From: bunnie Date: Tue, 19 Jul 2022 05:26:48 +0800 Subject: [PATCH] fix bug where RestoreDna backup op fails to be set. --- services/root-keys/src/implementation.rs | 15 +++++++++++++++ services/root-keys/src/main.rs | 14 ++++++++++++-- services/status/src/main.rs | 1 + 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/services/root-keys/src/implementation.rs b/services/root-keys/src/implementation.rs index 25b1b1136..965167ea3 100644 --- a/services/root-keys/src/implementation.rs +++ b/services/root-keys/src/implementation.rs @@ -2652,6 +2652,21 @@ impl<'a> RootKeys { ).map_err(|_| xous::Error::InternalError)?; Ok(()) } + pub fn write_restore_dna(&mut self, mut header: BackupHeader, backup_ct: backups::BackupDataCt) -> Result<(), xous::Error> { + header.op = BackupOp::RestoreDna; // set the "restore DNA" flag + + // condense the data into a single block, to reduce read/write cycles on the block + let mut block = [0u8; size_of::() + size_of::()]; + block[..size_of::()].copy_from_slice(header.as_ref()); + block[size_of::()..].copy_from_slice(backup_ct.as_ref()); + self.spinor.patch( + self.kernel(), + self.kernel_base(), + &block, + xous::KERNEL_BACKUP_OFFSET + ).map_err(|_| xous::Error::InternalError)?; + Ok(()) + } pub fn read_backup(&mut self) -> Result<(BackupHeader, backups::BackupDataCt), xous::Error> { let mut header = BackupHeader::default(); let mut ct = backups::BackupDataCt::default(); diff --git a/services/root-keys/src/main.rs b/services/root-keys/src/main.rs index 631462d9a..bfda19984 100644 --- a/services/root-keys/src/main.rs +++ b/services/root-keys/src/main.rs @@ -344,6 +344,14 @@ mod implementation { pub fn should_prompt_for_update(&self) -> bool {true} pub fn set_prompt_for_update(&self, _state: bool) {} pub fn write_backup(&mut self, header: BackupHeader, backup_ct: backups::BackupDataCt) -> Result<(), xous::Error> { + header.op = BackupOp::Backup; + log::info!("backup header: {:?}", header); + log::info!("backup ciphertext: {:x?}", backup_ct.as_ref()); + Ok(()) + } + pub fn write_restore_dna(&mut self, mut header: BackupHeader, backup_ct: backups::BackupDataCt) -> Result<(), xous::Error> { + header.op = BackupOp::RestoreDna; + log::info!("write restore_dna called"); log::info!("backup header: {:?}", header); log::info!("backup ciphertext: {:x?}", backup_ct.as_ref()); Ok(()) @@ -1541,13 +1549,15 @@ fn main() -> ! { let (mut header, ct) = keys.read_backup() .expect("Internal error: we're doing a backup, but somehow the backup record has disappeared on us!"); if u64::from_le_bytes(header.dna) == llio.soc_dna().unwrap() { + log::info!("erasing backup block"); // we're restoring to the same device, we're done! keys.erase_backup(); } else { + log::info!("DNA is not the same; setting flag to trigger RestoreDna flow"); // one more step...have to re-encrypt the PDDB's basis to the new device's DNA. // we'll handle this *after* the reboot, because we may need a newer SoC version, etc. - header.op = BackupOp::RestoreDna; - keys.write_backup(header, ct) + header.op = BackupOp::RestoreDna; // it gets set inside the call too; could lose this line but testing is painful on this flow. + keys.write_restore_dna(header, ct) .expect("Couldn't set flag for restore with new DNA: some data loss will occur."); } log::info!("going to into reboot arc"); diff --git a/services/status/src/main.rs b/services/status/src/main.rs index 151ac96cf..ec8cfb7e8 100644 --- a/services/status/src/main.rs +++ b/services/status/src/main.rs @@ -413,6 +413,7 @@ fn wrapped_main() -> ! { let restore_header = keys.lock().unwrap().get_restore_header(); match restore_header { Ok(Some(header)) => { + log::info!("Restore header op: {:?}", header.op); match header.op { BackupOp::Restore => { // set the keyboard layout according to the restore record.