Skip to content

Commit 7fd09fc

Browse files
When factory-resetting the device, mark empty filesystems for reformatting
1 parent 7922d67 commit 7fd09fc

File tree

2 files changed

+36
-11
lines changed

2 files changed

+36
-11
lines changed

extensions/manage/src/lib.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#![warn(non_ascii_idents, trivial_casts, unused, unused_qualifications)]
66
#![deny(unsafe_code)]
77

8-
use littlefs2_core::{Path, PathBuf};
8+
use littlefs2_core::{path, Path, PathBuf};
99
use serde::{Deserialize, Serialize};
1010
use trussed_core::{
1111
serde_extensions::{Extension, ExtensionClient, ExtensionResult},
@@ -145,3 +145,9 @@ pub trait ManageClient: ExtensionClient<ManageExtension> {
145145
}
146146

147147
impl<C: ExtensionClient<ManageExtension>> ManageClient for C {}
148+
149+
/// Empty file written to mark that a file system should be reformatted
150+
///
151+
/// When booting, if this file is written to a filesystem, it means that the previous
152+
/// power cycle was caused by a factory reset and the storage should be reformatted
153+
pub const FACTORY_RESET_MARKER_FILE: &Path = path!("/factory-reset-must-reformat");

src/manage.rs

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use trussed::{
1111
use trussed_manage::{
1212
FactoryResetClientReply, FactoryResetClientRequest, FactoryResetDeviceReply,
1313
FactoryResetDeviceRequest, ManageExtension, ManageReply, ManageRequest,
14+
FACTORY_RESET_MARKER_FILE,
1415
};
1516

1617
use crate::StagingBackend;
@@ -61,16 +62,34 @@ impl ExtensionImpl<ManageExtension> for StagingBackend {
6162
let store = platform.store();
6263

6364
for location in [Location::Internal, Location::External, Location::Volatile] {
64-
store
65-
.fs(location)
66-
.remove_dir_all_where(
67-
path!("/"),
68-
&callback(self.manage.should_preserve_file, location),
69-
)
70-
.map_err(|_err| {
71-
debug!("Failed to delete {location:?} fs: {_err:?}");
72-
Error::FunctionFailed
73-
})?;
65+
let fs = store.fs(location);
66+
fs.remove_dir_all_where(
67+
path!("/"),
68+
&callback(self.manage.should_preserve_file, location),
69+
)
70+
.map_err(|_err| {
71+
debug!("Failed to delete {location:?} fs: {_err:?}");
72+
Error::FunctionFailed
73+
})?;
74+
if location == Location::External {
75+
let is_empty = fs
76+
.read_dir_and_then(path!("/"), &mut |dir| match dir.next() {
77+
Some(Ok(_)) => Ok(false),
78+
Some(Err(err)) => Err(err),
79+
None => Ok(true),
80+
})
81+
.map_err(|_err| {
82+
debug!("Failed to check emptyness {location:?} fs: {_err:?}");
83+
Error::FunctionFailed
84+
})?;
85+
if is_empty {
86+
fs.write(FACTORY_RESET_MARKER_FILE, &[])
87+
.map_err(|_err| {
88+
debug!("Failed to write reformat instruction for {location:?} fs: {_err:?}");
89+
Error::FunctionFailed
90+
})?;
91+
}
92+
}
7493
}
7594
Ok(ManageReply::FactoryResetDevice(FactoryResetDeviceReply))
7695
}

0 commit comments

Comments
 (0)