attempt to add persistent storage to hosted mode PDDB

This will create a 100MiB file in tools/ppdb-images/hosted.bin
(sorry!!) which is persistent storage for hosted mode.

Delete the file if you want to start over from scratch.

It's...pretty brittle. I expect problems, but maybe it will at
least get things moving.
This commit is contained in:
bunnie 2022-03-24 03:21:00 +08:00
parent b50dbf0a69
commit 0bcc93c78e
3 changed files with 35 additions and 3 deletions

View file

@ -602,6 +602,7 @@ mod implementation {
pub fn set_usb_disable(&mut self, state: bool) {
self.usb_disable = state;
}
#[allow(dead_code)]
pub fn tts_sleep_indicate(&mut self) {
}
}

View file

@ -5,7 +5,9 @@ use std::sync::Once;
use std::mem::MaybeUninit;
use std::fs::File;
use std::fs::OpenOptions;
use std::io::prelude::*;
use std::io::SeekFrom;
// This is considered bad practice for Rust to use a global singleton.
// However, this hack puts the burden of emulation on the emulator, while
@ -16,6 +18,7 @@ use std::io::prelude::*;
// Note that this is a concurrently accessed, unsafe, unchecked vector.
struct FlashSingleton {
memory: Vec::<u8>,
disk: File,
}
fn flashmem() -> &'static mut FlashSingleton {
@ -24,12 +27,33 @@ fn flashmem() -> &'static mut FlashSingleton {
unsafe {
ONCE.call_once(|| {
let mut disk = OpenOptions::new()
.read(true)
.write(true)
.create(true)
.open("../tools/pddb-images/hosted.bin")
.expect("Can't open a PDDB image file for writing");
let mut memory = Vec::<u8>::with_capacity(PDDB_A_LEN);
for _ in 0..PDDB_A_LEN {
memory.push(0xFF);
if disk.metadata().unwrap().len() == 0 {
for _ in 0..PDDB_A_LEN {
memory.push(0xFF);
}
disk.write(&memory).expect("couldn't create initial disk image");
} else {
match disk.read_to_end(&mut memory) {
Ok(bytes_read) => {
assert!(bytes_read == PDDB_A_LEN, "PDDB disk image is of an incorrect size, refusing to run!");
}
_ => {
panic!("Can't read PDDB disk image, refusing to run!");
}
}
}
let flashmem = FlashSingleton {
memory,
disk,
};
SINGLETON.write(flashmem);
});
@ -100,12 +124,20 @@ impl HostedSpinor {
) {
*dst = src;
}
flashmem().disk.seek(SeekFrom::Start(offset as u64)).expect("couldn't seek PDDB");
flashmem().disk.write(data).expect("couldn't write PDDB");
Ok(())
}
pub fn bulk_erase(&self, start: u32, len: u32) -> Result<(), xous::Error> {
for b in flashmem().memory.as_mut_slice()[(start - xous::PDDB_LOC) as usize .. (start - xous::PDDB_LOC + len) as usize].iter_mut() {
*b = 0xFF;
}
flashmem().disk.seek(SeekFrom::Start(start as u64)).expect("couldn't seek PDDB");
let mut blank = Vec::<u8>::with_capacity(len as usize);
for _ in 0..len {
blank.push(0xFF);
}
flashmem().disk.write(&blank).expect("couldn't write PDDB");
Ok(())
}
}

View file

@ -468,7 +468,6 @@ fn xmain() -> ! {
}
});
// spawn a delayed mount command, shortly after boot. There's too much going on at boot, and it blocks other things from coming up.
#[cfg(not(any(windows, unix)))] // skip this step if running in hosted mode
let _ = thread::spawn({
let my_cid = my_cid.clone();
move || {