diff --git a/Cargo.lock b/Cargo.lock index 2936c19c9..8cad646a6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1378,10 +1378,12 @@ dependencies = [ name = "pddb" version = "0.1.0" dependencies = [ + "aes-xous", "log", "log-server", "num-derive", "num-traits", + "rand_core 0.5.1", "rkyv", "spinor", "susres", diff --git a/services/pddb/Cargo.toml b/services/pddb/Cargo.toml index 4c266daeb..faefa701d 100644 --- a/services/pddb/Cargo.toml +++ b/services/pddb/Cargo.toml @@ -18,6 +18,8 @@ susres = {path = "../susres"} rkyv = {version = "0.4.3", features = ["const_generics"], default-features = false} trng = {path = "../trng"} spinor = {path = "../spinor"} +aes-xous = {path="../aes"} +rand_core = "0.5" [target.'cfg(not(any(windows,unix)))'.dependencies] utralib = { path = "../../utralib"} diff --git a/services/pddb/src/api.rs b/services/pddb/src/api.rs index 8a6e67dac..9ba11d531 100644 --- a/services/pddb/src/api.rs +++ b/services/pddb/src/api.rs @@ -1,8 +1,14 @@ use rkyv::{Archive, Deserialize, Serialize}; pub(crate) const SERVER_NAME_PDDB: &str = "_Plausibly Deniable Database_"; -pub const PDDB_MAX_DICT_NAME_LEN: usize = 64; -pub const PDDB_MAX_KEY_NAME_LEN: usize = 256; +pub(crate) const PDDB_MAX_BASIS_NAME_LEN: usize = 64; +pub(crate) const PDDB_MAX_DICT_NAME_LEN: usize = 64; +pub(crate) const PDDB_MAX_KEY_NAME_LEN: usize = 128; +pub(crate) const PDDB_MAGIC: [u8; 4] = [0x50, 0x44, 0x44, 0x42]; +pub(crate) const PDDB_VERSION: u16 = 0; + +pub const PDDB_DEFAULT_SYSTEM_BASIS: &'static str = ".System"; +pub(crate) const PDDB_FAST_SPACE_SYSTEM_BASIS: &'static str = ".FastSpace"; #[derive(num_derive::FromPrimitive, num_derive::ToPrimitive, Debug)] pub(crate) enum Opcode { @@ -28,7 +34,7 @@ pub(crate) enum Opcode { #[derive(Archive, Serialize, Deserialize)] pub(crate) struct PddbKeyRequest { pub(crate) dict: xous_ipc::String::, // pending https://github.com/rust-lang/rust/issues/90195 - pub(crate) key: xous_ipc::String::, // pending https://github.com/rust-lang/rust/issues/90195 + pub(crate) key: xous_ipc::String::, // pending https://github.com/rust-lang/rust/issues/90195 pub(crate) token: Option<[u32; 3]>, } @@ -68,7 +74,7 @@ mod tests { } } impl PddbBuf { - pub fn from_slice_mut(slice: &mut [u8]) -> &mut PddbBuf { + pub(crate) fn from_slice_mut(slice: &mut [u8]) -> &mut PddbBuf { // this transforms the slice [u8] into a PddbBuf ref. unsafe {core::mem::transmute::<*mut u8, &mut PddbBuf>(slice.as_mut_ptr()) } } diff --git a/services/pddb/src/backend.rs b/services/pddb/src/backend.rs new file mode 100644 index 000000000..6cb993743 --- /dev/null +++ b/services/pddb/src/backend.rs @@ -0,0 +1,10 @@ +mod basis; +pub use basis::*; +mod pagetable; +pub use pagetable::*; + +#[cfg(any(target_os = "none", target_os = "xous"))] +mod hw; +pub use hw::*; + +// TODO: the alternative back-end PddbOs structures for hosted mode. \ No newline at end of file diff --git a/services/pddb/src/backend/basis.rs b/services/pddb/src/backend/basis.rs new file mode 100644 index 000000000..2368c055e --- /dev/null +++ b/services/pddb/src/backend/basis.rs @@ -0,0 +1,111 @@ +use pddb::PDDB_MAX_BASIS_NAME_LEN; + +use crate::api::{PDDB_MAX_DICT_NAME_LEN, PDDB_MAX_KEY_NAME_LEN}; + +use core::num::NonZeroU64; +use core::ops::{Deref, DerefMut}; +use core::{mem, slice}; + +pub(crate) const FREE_CACHE_SIZE: usize = 16; + +/// In basis space, the BasisRoot is located at 0 +/// The first 4GiB is reserved for the Basis Root. +/// Keys begin at the next 4GiB. +/// AAD associated with the BasisRoot consist of a bytewise concatenation of: +/// - Basis name +/// - version number (should match version inside; complicates downgrade attacks) +/// - FPGA's silicon DNA number (makes a naive raw-copy of the data to another device unusable; +/// but of course, the DNA ID can be forged minor efforts) +/// +/// As a directory structure, the basis root is designed to be read into RAM in a contiguous block. +/// it'll typically be less than a page in length, but a pathological number of dictionaries can make it +/// much longer. +#[repr(C, packed)] +pub(crate) struct BasisRoot { + /// this is stored as plaintext and generated fresh every time the block is re-encrypted + pub(crate) p_nonce: [u8; 12], + // everything below here is encrypted using AES-GCM-SIV + pub(crate) magic: [u8; 4], + pub(crate) version: u16, + pub(crate) journal_rev: u32, + pub(crate) name: [u8; PDDB_MAX_BASIS_NAME_LEN], + /// increments every time the BasisRoot is modified. This field must saturate, not roll over. + pub(crate) age: u32, + pub(crate) num_dictionaries: u32, + /// a cache of up FREE_CACHE_SIZE indicating the location of free space in the dictionary + /// allocation. This saves from having to do frequent free space search/compaction operations + /// on memory during writes and updates. + pub(crate) free_cache: [Option; FREE_CACHE_SIZE], + // dict_slice: [DictPointer], // a synthetic record that is a slice of dictionaries + // pad: [u8], // padding out to the next 4096-byte block less 16 bytes + // p_tag: [u8; 16], // auth tag output of the AES-GCM-SIV +} +impl Deref for BasisRoot { + type Target = [u8]; + fn deref(&self) -> &[u8] { + unsafe { + slice::from_raw_parts(self as *const BasisRoot as *const u8, mem::size_of::()) + as &[u8] + } + } +} + +impl DerefMut for BasisRoot { + fn deref_mut(&mut self) -> &mut [u8] { + unsafe { + slice::from_raw_parts_mut(self as *mut BasisRoot as *mut u8, mem::size_of::()) + as &mut [u8] + } + } +} + + + +pub(crate) struct DictPointer { + name: [u8; PDDB_MAX_DICT_NAME_LEN], + age: u32, // increment every time the dictionary pointer is modified. + addr: u64, // the virtual address of the dictionary +} + +#[derive(Copy, Clone)] +pub(crate) struct FreeSpace { + start: u64, + len: NonZeroU64, +} + +/// Typically individual dictionaries start out life having their own 4k-page, but they +/// can be compacted together if they seem to be static/non-changing and we need more space. +pub(crate) struct Dictionary { + p_nonce: [u8; 12], + journal_rev: u32, + num_keys: u32, + age: u32, // increment every time the dictionary definition itself is modified + // key_slice: [HashKey], // a synthetic record that is a slice of HashKeys + // pad: [u8], // padding out to the next 4096-byte block less 16 bytes + // p_tag: [u8; 16] // auth tag output of AES-GCM-SIV +} + +/// This defines a key's name, along with a pointer to its location in memory. +/// HashKeys are packed at the end of a Dictionary. +pub(crate) struct HashKey { + name: [u8; PDDB_MAX_KEY_NAME_LEN], + /// incremented every time the key is re-written to flash. saturating add. + age: u32, + /// length of the data stored in the HashKey + length: u64, + /// location of the data of the HashKey. This is always in absolute virtual memory coordinates. + /// Note that offsets relative to the `base_addr` need to account for the `nonce` and `tag` that + /// are necessitated by the page-by-page encryption of the raw data itself. + base_addr: u64, +} + +/// this is the structure of the Basis Key in RAM. The "key" and "iv" are actually never committed to +/// flash; only the "salt" is written to disk. The final "salt" is computed as the XOR of the salt on disk +/// and the user-provided "basis name". We never record the "basis name" on disk, so that the existence of +/// any Basis can be denied. +pub(crate) struct BasisKey { + salt: [u8; 16], + key: [u8; 32], // derived from lower 256 bits of sha512(bcrypt(salt, pw)) + iv: [u8; 16], // an IV derived from the upper 128 bits of the sha512 hash from above, XOR with the salt +} + diff --git a/services/pddb/src/backend/hw.rs b/services/pddb/src/backend/hw.rs new file mode 100644 index 000000000..85909f727 --- /dev/null +++ b/services/pddb/src/backend/hw.rs @@ -0,0 +1,78 @@ +use std::convert::TryInto; + +use crate::{api, backend::{BasisRoot, FREE_CACHE_SIZE}}; +use pddb::PDDB_MAX_BASIS_NAME_LEN; +use rand_core::{CryptoRng, RngCore}; +use core::ops::Deref; + +/// OS-specific PDDB structures + +pub(crate) struct PddbOs { + spinor: spinor::Spinor, + pddb_mr: xous::MemoryRange, + trng: trng::Trng, +} + +impl PddbOs { + pub fn new() -> PddbOs { + let xns = xous_names::XousNames::new().unwrap(); + let pddb = xous::syscall::map_memory( + xous::MemoryAddress::new(xous::PDDB_LOC as usize), + None, + xous::PDDB_LEN as usize, + xous::MemoryFlags::R, + ) + .expect("Couldn't map the PDDB memory range"); + + PddbOs { + spinor: spinor::Spinor::new(&xns).unwrap(), + pddb_mr: pddb, + trng: trng::Trng::new(&xns).unwrap(), + } + } + + + /// this function is dangerous in that calling it will completely erase all of the previous data + /// in the PDDB an replace it with a brand-spanking new, blank PDDB. + /// The number of servers that can connect to the Spinor crate is strictly tracked, so we borrow a reference + /// to the Spinor object allocated to the PDDB implementation for this operation. + pub fn format_pddb(&mut self) { + // step 1. Erase the entire PDDB region. + log::info!("Erasing the PDDB region"); + let blank_sector: [u8; 4096] = [0xff; 4096]; + + for offset in (0..xous::PDDB_LEN).step_by(4096) { + self.spinor.patch( + self.pddb_mr.as_slice(), + xous::PDDB_LOC, + &blank_sector, + offset + ).expect("couldn't erase memory"); + } + + // step 2. create the system basis root structure + let mut p_nonce: [u8; 12] = [0; 12]; + self.trng.fill_bytes(&mut p_nonce); + let mut name: [u8; PDDB_MAX_BASIS_NAME_LEN] = [0; PDDB_MAX_BASIS_NAME_LEN]; + write!(name, PDDB_DEFAULT_SYSTEM_BASIS).unwrap(); + let mut basis_root = BasisRoot { + p_nonce, + magic: api::PDDB_MAGIC, + version: api::PDDB_VERSION, + journal_rev: 0, + name, + age: 0, + num_dictionaries: 0, + free_cache: [None; FREE_CACHE_SIZE], + }; + // extract a slice-u8 that maps onto the basis_root record, allowing us to patch this into a FLASH page + let br_slice: &[u8] = basis_root.deref(); + + // TODO: unfortunately, we need to figure out some free space structure at this point, I think -- because + // we are getting to the point of having to allocate the free_cache data before writing it. + + // next: figure out the length of the page table + mbbb (make before break buffer) + // + space for the (fscb) free space circular buffer, and then locate the basis root there + + } +} \ No newline at end of file diff --git a/services/pddb/src/backend/pagetable.rs b/services/pddb/src/backend/pagetable.rs new file mode 100644 index 000000000..7b0d2e855 --- /dev/null +++ b/services/pddb/src/backend/pagetable.rs @@ -0,0 +1,46 @@ + +/// A Page Table Entry. Contains the address map of the corresponding entry, +/// plus a nonce, and a checksum. Due to the Page Table being deliberately +/// srtuctured to have invalid entries that don't decrypt correctly, you +/// can't use a chaining approach. Thus these entries are encrypted closer to +/// an ECB-style, thus an embedded nonce is necessary to keep identical entries +/// from appearing the same in the ciphertext domain. +/// +/// It's not clear at all if the nonce is large enough to prevent random collisions; +/// however, the sheer bulk of the page table demands a compact representation. Thus, +/// any routines downstream of the Pte shall be coded to handle potentially a much larger +/// nonce and checksum structure. +#[repr(C, packed)] +pub(crate) struct Pte { + /// the virtual address is 48 bits long + pddb_addr: [u8; 6], + /// the flags are largely TBD at this moment + flags: [u8; 2], + /// 32-bit strength of a nonce, but can be varied + nonce: [u8; 4], + /// 32-bit "weak" checksum, used only for quick scans of the PTE to determine a coarse "in" or "out" classifier + /// checksum is computed on all of the bits prior, so checksum(pddb_addr, flags, nonce) + checksum: [u8; 4], +} + +/// This structure is mapped into the top of FLASH memory, starting at +/// xous::PDDB_LOC +pub const PDDB_SIZE_PAGES: usize = xous::PDDB_LEN as usize / 4096; +#[repr(C, packed)] +pub(crate) struct PageTableInFLash { + table: [Pte; PDDB_SIZE_PAGES], +} + +/// This is the representation of a page of data on disk. Keys that span multiple +/// pages have to decrypt individual pages, subtracting the nonce, journalrev, and tag, to find +/// the actual data being retrieved. +pub(crate) struct EncryptedPage { + /// the nonce is not encrypted + p_nonce: [u8; 12], + /// journal_rev is encrypted and indicates the current journal revision for the block + journal_rev: [u8; 4], + /// data is encrypted and holds the good stuff + data: [u8; (4096 - 12 - 16 - 4)], + /// tag is the authentication tag. If the page decrypts & authenticates, we know it's a valid data block for us. + p_tag: [u8; 16], +} \ No newline at end of file diff --git a/services/pddb/src/main.rs b/services/pddb/src/main.rs index 1df338581..6772166e3 100644 --- a/services/pddb/src/main.rs +++ b/services/pddb/src/main.rs @@ -3,6 +3,8 @@ mod api; use api::*; +mod backend; +use backend::*; use num_traits::*; use xous::{msg_blocking_scalar_unpack, msg_scalar_unpack}; @@ -27,14 +29,14 @@ use xous::{msg_blocking_scalar_unpack, msg_scalar_unpack}; /// | Key /// | | /// --+- --------+--------------------- -/// /logs/matrix/alice/oct30_2021/bob.txt +/// logs:matrix/alice/oct30_2021/bob.txt /// -/// It could equally have an arbitrary name like "logs/Matrix - alice to bob Oct 30 2021"; +/// It could equally have an arbitrary name like "logs:Matrix - alice to bob Oct 30 2021"; /// as long as the string that identifies a Key is unique, it's stored in the database /// all the same. Any valid utf-8 unicode characters are acceptable. /// /// Likewise, something like this: -/// /settings/wifi/Kosagi.json +/// settings:wifi/Kosagi.json /// Would be deconstructed into the "settings" dictionary with a key of wifi/Kosagi.json. /// /// General Operation: @@ -76,6 +78,179 @@ const PDDB_BACKING_BASE: usize = 0x8000_0000; /// increase this limit my changing the constants here. const PDDB_BACKING_SIZE: usize = 0x4000_0000; +/// General flash->key structure +/// +/// The basic unit of memory is a page (4k). Keys shorter than 4k will be packed into a single +/// page, but no allocation will ever be smaller than 4k due to the erase block size of the FLASH. +/// +/// Because the flash is always encrypted, there is also no such thing as "incremental writing of data" +/// to a page, as the presence of free space (all 1's regions) are considered a side-channel. Thus, +/// the PDDB relies heavily on caching data structures in RAM, with periodic commits of encrypted data +/// back to the flash. This means it is also very easy to lose data when the system has a panic and +/// reboots ungracefully. Thus, the structure of data at rest is journaled, so that we have some +/// chance of recovering an older snapshot of the data if there is an ungraceful power-down event. +/// +/// I believe this relative unreliability compared to more traditional storage is just a fact of life for +/// an encrypted, plausibly deniable filesystem, as you have to trade-off the side channel of partially +/// erased blocks versus the finite write lifetime of flash storage. +/// +/// When a Basis is created, it starts with 64k of FLASH allocated to it. Most of this is blank (but encrypted +/// in flash), but it is important in a PD filesystem to claim some freespace for day-to-day operations. This +/// is because when you need to allocate more data, you have to make a guess as to what is free, and what +/// previously allocated but currently denied to exist (locked Basis look identical to free space when locked). +/// +/// Each Dictionary within a Basis starts with a 4k allocation as well, although most of it is free space. +/// Keys are then populated into dictionaries, with additional pages allocated to dictionaries as the keys +/// grow. +/// +/// Blocks on disk are mapped into a virtual PDDB space via a page table. The virtual PDDB space is 64-bit. +/// Each Basis gets its own 48-bit address space, allowing for up to 64k Basis. +/// The PDDB page table guarantees that no matter the order of allocation in FLASH, a Basis can linearly +/// access its blocks by doing a page table lookup. A reverse-lookup structure is constructed on boot +/// by scanning the entire PDDB, and creating a HashMap of (basis, offset) -> pddb_addr tuples. +/// +/// Page table entries don't define themselves with a Basis -- every time a Basis is opened, the entire +/// table must be scanned, brute-force decrypting every entry against the Basis key, and seeing if the +/// checksum in the table entry matches the address. If there's a match, then that entry is recorded as +/// belonging to a given Basis. +/// +/// When a system boots, it starts with two default Basis: FastSpace, and System. These Basis are protected +/// with the "device unlock" key. System is a set of low-security data that are used +/// to configure "everyday details". FastSpace Basis is a special-case Basis that is not structured as +/// a Key/Value store. Instead, it tracks a small amount of pre-allocated "free" space, that can be +/// pulled from to grow Dictionaries without requesting that the user unlock every known Basis in the system +/// to ensure none of the valuable data is overwritten. +/// +/// The .FastSpace basis should never be equal to the total amount of free space in the system, as it +/// would betray the actual amount of data available and destroy plausible deniability. The system can +/// operate with .FastSpace set to the nil set, but it would also operate without confidentiality of hidden +/// Basis, because in order to allocate new blocks it must prompt the user to enter all known Basis passwords, +/// to avoid accidentally overwriting data that should be retained (as locked Basis would appear as free space +/// and risk being overwritten). However, in order to accelerate bookkeeping, the .FastSpace basis operates +/// with an encrypted record that rotates through a set of "clean" pages (that is, pages that have been set +/// to all 1's) in a circular buffer basis. The state of where the .FastSpace record is in the "clean" pages +/// disclose nothing other than the fact that the system has been used. +/// +/// TODO: define .FastSpace records and structures. +/// +/// +/// Basis Deniability +/// +/// The existence of a Basis itself must be confidential; if we stored a list of encrypted Basis passwords +/// somewhere, a rubber-hose attacker would simply need to count the number of encrypted entries and commence +/// the beatings until the number of passwords that have been disclosed under duress match the number of +/// encrypted password entries. +/// +/// Herein lies a challenge. Standard password storage techniques demand that each password be stored +/// with a unique salt. The existence of this salt betrays that a password must, in essence, exist. +/// There are at least two methods to counter this: +/// +/// 1. Dummy salt entries. In this approach, each system is initialized with a random set of additional, dummy +/// salt/encrypted password combos. In the case of a rubber-hose attack, the attacker cannot know precisely +/// when to stop the beatings, as the few remaining entries could be dummies with absolutely no meaning. +/// 2. A single salt entry. In this approach, a single shared salt is used by /all/ passwords, and there is no +/// encrypted password list stored; instead, the the password as presented is directly used to decrypt +/// the Basis page table and if no valid entries are found we may conclude that the password provided has a typo. +/// +/// The advantage of (1) is that the usage of salt falls precisely within the traditional cryptographic +/// specification of bcrypt(). The disadvantages of (1) include: one still has to have a method to match +/// a presented password to a given salt entry. In a traditional user/pass login database, the username is +/// the plaintext key to match the salt. In this case, we would use a nickname used to refer to each Basis +/// to correlate to its salt entry. Thus, we'd have to come up with "garbage" plaintext nicknames +/// for a Basis that are not trivial for a rubber-hose attacker to dismiss as chaffe. I think this is Hard. +/// The alternative is to "brute force" the entire list of salts, guessing each one in sequence until one +/// is found to decrypt entries in the Basis page table. The problem with this is that bcrypt is designed to +/// be computationally slow, even for valid passwords. The complexity parameter is chosen so that it takes +/// about 1 second per iteration through the password function. Note that a faster CPU does not solve this +/// problem: if you CPU gets faster, you should increase your complexity, so one is always suffering the 1 +/// second penalty to try a password. This means brute forcing a list of salts necessarily incurs at +/// least a 1 second penalty per entry; thus each dummy basis adds a 1 second minimum overhead to unlocking +/// a new Basis database. This puts a downward pressure on the number of dummies to be included; however, the +/// number of dummies needs to be at least as large as the number of Basis we wish to plausibly deny. This +/// creates a negative incentive to using plausible deniability. +/// +/// The advantage of (2) is that when a user presents a password, we only need to run the bcrypt routine +/// once, and then we can immediately start checking the Basis page table for valid entries. The disadvantage +/// of (2) is that we are re-using a salt across all of a given user's passwords. Note that the salt itself +/// is from a TRNG, so between user devices, the salt performs its role. However, it means that an attacker +/// can generate a single rainbow table specific to a given user to reverse all of their passwords; and furthermore, +/// re-used passwords are trivially discoverable with the rainbow table. +/// +/// Approach (1) is probably what most crypto purists would adopt: you, dear user, /should/ understand that +/// cryptography is worth the wait. However, a second principle states that "users always pick convenience over +/// security". A 15-second wait is an eternity in the UX world, and would act as an effective deterrent to any +/// user ever using the PD function because it is too slow. Thus for v1 of the PDDB, we're going to try approach +/// (2), where a common salt is used across all the PDDB passwords, but the salt is /not/ re-used between devices. +/// This choice diminishes the overall security of the system in the case that a user chooses weak passwords, or re-uses +/// passwords, but in exchange for a great improvement in responsivity of the implementation. +/// +/// The final implementation uses a slight mod on (2), where the 128-bit common salt stored on disk is XOR'd +/// with the user-provided "basis name". Users are of course allowed to pick crappy names for their basis, and +/// re-use the names, but hopefully this adds a modicum of robustness against rainbow table attacks. +/// +/// +/// Basis Unlock Procedure +/// +/// Each Basis has a name and a passcode associated with it. The two default Basis names are +/// - `.System` +/// - `.FastSpace` +/// These are both associated with the default system unlock passcode. +/// A newly created Basis will request a name for the Basis, and a password. It is a requirement +/// that the combination of `(name, password)` be unique; this property is enforced and a system will +/// reject attempts to create identically named, identially passworded Basis. However, one can have +/// same-named Basis that have a different password, or diffently-named Basis with the same password +/// (this is generally not recommended, but it's not prohibited). +/// +/// The `name` field is XOR'd with the device-local `salt` field to form the salt for the password, +/// and the plaintext of the password itself is used as the password which is fed into the bcrypt() +/// algorithm to generate a 192-bit encrypted password, which is expanded to 256-bits using SHA-512/256, +/// and then used as the AES key for the given `(name, password)` Basis. +/// +/// +/// Journaling +/// +/// A major issue with the implementation is that even a small change in a data structure turns into +/// the mutation of at least an entire sector of data, with a sector being 4k. The reason is that +/// we can't take advantage of FLASH memory's property where 1's (erased) can be set to 0's on a byte-by-byte +/// basis: in order to mask free space signatures, the entire storage area is filled with random bytes. +/// Furthermore, any update to a part of data within a block should necessarily propagate to the entire +/// block changing, in order to avoid attacks where observations on which portion of ciphertext has changed +/// leading to a definitive conclusion about the state of the database records. Therefore, a large portion +/// of the database data will have to persist in RAM, and be updated only at regular, but widely-spaced-out +/// (to avoid FLASH wear-out), intervals. +/// +/// In the case that power is lost during an update, the system uses a 32-bit `journal_rev` number at the +/// top of every major database structure. If two competing copies of data are found, the one with the +/// highest `journal_rev` number wins. It may be possible to overflow the 32-bit number, so at some point a +/// garbage collection step needs to be made where any errant, lower journal rev blocks are overwritten +/// with random data; all the journal revs are confirmed to be the same; and then all set to 0 again. Power +/// should not be lost during this process; but it should be rare, and likely never needed. +/// +/// When writing to disk, a write-then-erase method is used: +/// 1. Required blocks for the update are taken out of the .FastSpace pool +/// 2. Detached data sections of structural records are written first (only database structural records can have detached data; +/// all stored data have individual journal revision fields associated with them), and into blocks +/// taken out of the .FastSpace pool. +/// 3. The head of the updated structural record is written, with the new latest journal rev noted. +/// 4. The page table is updated using the procedure outlined below. +/// 5. The old blocks are erased and overwritten with random data. +/// 6. The .FastSpace pool is updated with the newly freed blocks +/// +/// When updating the page table, it's important not to lose an entire page's worth of entries in case +/// power is lost when updating it. The following process is used to update a page table entry: +/// 0. At the end of the page table there is a circular buffer of blank pages (they are truly blank, +/// but this will not leak metadata about the contents of the PDDB, as it only does bookkeeping +/// on page table updates as a whole). This is known as the make-before-break buffer (mbbb). +/// 1. The previous mbbb entry is erased, if there is any, and the next entry is noted. +/// 2. The new page table entry is written into the mbbb +/// 3. The old page table entry in the page table itself is erased. +/// 4. The old page table entry is populated with the updated page table records. +/// +/// Detecting and Repairing a broken page table: +/// The special case of long run's of 1's (more than 32 bits in a row) is checked during the page table scan. +/// If a long run of 1's is detected, then a suspected corrupted page table update is flagged, and for that page the data +/// is pulled from the mbbb record. + #[xous::xous_main] fn xmain() -> ! { log_server::init_wait().unwrap(); @@ -88,6 +263,8 @@ fn xmain() -> ! { log::trace!("ready to accept requests"); + let pddb_os = PddbOs::new(); + // register a suspend/resume listener let sr_cid = xous::connect(pddb_sid).expect("couldn't create suspend callback connection"); let mut susres = susres::Susres::new(&xns, api::Opcode::SuspendResume as u32, sr_cid).expect("couldn't create suspend/resume object"); diff --git a/services/spinor/src/main.rs b/services/spinor/src/main.rs index 1da3d47d2..14faef9cf 100644 --- a/services/spinor/src/main.rs +++ b/services/spinor/src/main.rs @@ -551,9 +551,9 @@ fn xmain() -> ! { - shellchat (for testing ONLY, remove once done) - suspend/resume (for suspend locking/unlocking calls) - keystore - - PDDB (not yet written) + - PDDB */ - let spinor_sid = xns.register_name(api::SERVER_NAME_SPINOR, Some(3)).expect("can't register server"); + let spinor_sid = xns.register_name(api::SERVER_NAME_SPINOR, Some(4)).expect("can't register server"); log::trace!("registered with NS -- {:?}", spinor_sid); let handler_conn = xous::connect(spinor_sid).expect("couldn't create interrupt handler callback connection"); diff --git a/xous-rs/src/definitions.rs b/xous-rs/src/definitions.rs index 0d3911a26..ca216cf2a 100644 --- a/xous-rs/src/definitions.rs +++ b/xous-rs/src/definitions.rs @@ -27,11 +27,7 @@ pub const LOADER_FONT_LEN: u32 = 0x0045_0000; // length of font region only pub const LOADER_TOTAL_LEN: u32 = LOADER_CODE_LEN + LOADER_FONT_LEN; // code + font pub const KERNEL_LOC: u32 = 0x0098_0000; // kernel start -pub const KERNEL_LEN: u32 = 0x0038_0000; // max kernel length - -pub const PDDB_LOC: u32 = 0x00D0_0000; // PDDB start -pub const PDDB_LEN: u32 = 0x0530_0000; // this reserves space for testing structures - // pub const PDDB_LEN: u32 = 0x0728_0000; // length without testing structure +pub const KERNEL_LEN: u32 = 0x0068_0000; // max kernel length pub const EC_REGION_LOC: u32 = 0x07F8_0000; // EC update staging area pub const EC_WF200_PKG_LOC: u32 = 0x07F8_0000; @@ -40,6 +36,9 @@ pub const EC_FW_PKG_LOC: u32 = 0x07FC_E000; pub const EC_FW_PKG_LEN: u32 = 0x0003_2000; pub const EC_REGION_LEN: u32 = 0x0008_0000; +pub const PDDB_LOC: u32 = 0x0100_0000; // PDDB start +pub const PDDB_LEN: u32 = EC_REGION_LOC - PDDB_LOC; + pub mod exceptions; pub use exceptions::*; diff --git a/xtask/src/main.rs b/xtask/src/main.rs index 2094108b9..d335f6bf1 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -120,6 +120,7 @@ fn main() -> Result<(), Box> { "xous-names", "trng", "pddb", + "sha2", ]; let aestest_pkgs = ["ticktimer-server", "log-server", "aes-test"];