mirror of
https://github.com/Next-Flip/Momentum-Firmware.git
synced 2026-05-12 20:18:35 -07:00
Backup+restore internal U2F keys on factory reset
This commit is contained in:
@@ -193,22 +193,57 @@ static void storage_int_lfs_mount(LFSData* lfs_data, StorageData* storage) {
|
|||||||
was_fingerprint_outdated;
|
was_fingerprint_outdated;
|
||||||
|
|
||||||
if(need_format) {
|
if(need_format) {
|
||||||
// Format storage
|
err = lfs_mount(lfs, &lfs_data->config);
|
||||||
err = lfs_format(lfs, &lfs_data->config);
|
|
||||||
if(err == 0) {
|
if(err == 0) {
|
||||||
FURI_LOG_I(TAG, "Factory reset: Format successful, trying to mount");
|
FURI_LOG_I(TAG, "Factory reset: Mounted for backup");
|
||||||
furi_hal_rtc_reset_flag(FuriHalRtcFlagFactoryReset);
|
|
||||||
err = lfs_mount(lfs, &lfs_data->config);
|
// Backup U2F keys
|
||||||
|
lfs_file_t file;
|
||||||
|
lfs_file_open(lfs, &file, ".cnt.u2f", LFS_O_RDWR | LFS_O_CREAT);
|
||||||
|
uint8_t cnt[file.ctz.size];
|
||||||
|
lfs_file_read(lfs, &file, cnt, sizeof(cnt));
|
||||||
|
lfs_file_close(lfs, &file);
|
||||||
|
lfs_file_open(lfs, &file, ".key.u2f", LFS_O_RDWR | LFS_O_CREAT);
|
||||||
|
uint8_t key[file.ctz.size];
|
||||||
|
lfs_file_read(lfs, &file, key, sizeof(key));
|
||||||
|
lfs_file_close(lfs, &file);
|
||||||
|
|
||||||
|
err = lfs_unmount(lfs);
|
||||||
if(err == 0) {
|
if(err == 0) {
|
||||||
FURI_LOG_I(TAG, "Factory reset: Mounted");
|
FURI_LOG_E(TAG, "Factory reset: Unmounted after backup");
|
||||||
storage->status = StorageStatusOK;
|
// Format storage
|
||||||
|
err = lfs_format(lfs, &lfs_data->config);
|
||||||
|
if(err == 0) {
|
||||||
|
FURI_LOG_I(TAG, "Factory reset: Format successful, trying to mount");
|
||||||
|
furi_hal_rtc_reset_flag(FuriHalRtcFlagFactoryReset);
|
||||||
|
err = lfs_mount(lfs, &lfs_data->config);
|
||||||
|
if(err == 0) {
|
||||||
|
FURI_LOG_I(TAG, "Factory reset: Mounted");
|
||||||
|
|
||||||
|
// Restore U2F keys
|
||||||
|
lfs_file_open(lfs, &file, ".cnt.u2f", LFS_O_RDWR | LFS_O_CREAT);
|
||||||
|
lfs_file_write(lfs, &file, cnt, sizeof(cnt));
|
||||||
|
lfs_file_close(lfs, &file);
|
||||||
|
lfs_file_open(lfs, &file, ".key.u2f", LFS_O_RDWR | LFS_O_CREAT);
|
||||||
|
lfs_file_write(lfs, &file, key, sizeof(key));
|
||||||
|
lfs_file_close(lfs, &file);
|
||||||
|
|
||||||
|
storage->status = StorageStatusOK;
|
||||||
|
} else {
|
||||||
|
FURI_LOG_E(TAG, "Factory reset: Mount after format failed");
|
||||||
|
storage->status = StorageStatusNotMounted;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
FURI_LOG_E(TAG, "Factory reset: Format failed");
|
||||||
|
storage->status = StorageStatusNoFS;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
FURI_LOG_E(TAG, "Factory reset: Mount after format failed");
|
FURI_LOG_E(TAG, "Factory reset: Unmount after backup failed");
|
||||||
storage->status = StorageStatusNotMounted;
|
storage->status = StorageStatusNotMounted;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
FURI_LOG_E(TAG, "Factory reset: Format failed");
|
FURI_LOG_E(TAG, "Factory reset: Mount for backup failed");
|
||||||
storage->status = StorageStatusNoFS;
|
storage->status = StorageStatusNotMounted;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Normal
|
// Normal
|
||||||
|
|||||||
Reference in New Issue
Block a user