mirror of
https://github.com/Next-Flip/Momentum-Firmware.git
synced 2026-05-12 18:58:36 -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;
|
||||
|
||||
if(need_format) {
|
||||
// Format storage
|
||||
err = lfs_format(lfs, &lfs_data->config);
|
||||
err = lfs_mount(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);
|
||||
FURI_LOG_I(TAG, "Factory reset: Mounted for backup");
|
||||
|
||||
// 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) {
|
||||
FURI_LOG_I(TAG, "Factory reset: Mounted");
|
||||
storage->status = StorageStatusOK;
|
||||
FURI_LOG_E(TAG, "Factory reset: Unmounted after backup");
|
||||
// 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 {
|
||||
FURI_LOG_E(TAG, "Factory reset: Mount after format failed");
|
||||
FURI_LOG_E(TAG, "Factory reset: Unmount after backup failed");
|
||||
storage->status = StorageStatusNotMounted;
|
||||
}
|
||||
} else {
|
||||
FURI_LOG_E(TAG, "Factory reset: Format failed");
|
||||
storage->status = StorageStatusNoFS;
|
||||
FURI_LOG_E(TAG, "Factory reset: Mount for backup failed");
|
||||
storage->status = StorageStatusNotMounted;
|
||||
}
|
||||
} else {
|
||||
// Normal
|
||||
|
||||
Reference in New Issue
Block a user