Storage: lfs config fingerprinting. RTC: fix data collision in lock register, refactor and cleanup. (#928)

This commit is contained in:
あく 2021-12-24 17:33:58 +03:00 committed by GitHub
parent 79e0aed1e6
commit 7cea359be8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 119 additions and 95 deletions

View file

@ -8,7 +8,6 @@
#include "portmacro.h"
#include "storage/filesystem-api-defines.h"
#include "storage/storage.h"
#include <furi-hal-lock.h>
#include <stdint.h>
#include <power/power_service/power.h>
#include "helpers/desktop_animation.h"
@ -155,14 +154,14 @@ int32_t desktop_srv(void* p) {
bool loaded = LOAD_DESKTOP_SETTINGS(&desktop->settings);
if(!loaded) {
furi_hal_lock_set(false);
furi_hal_rtc_reset_flag(FuriHalRtcFlagLock);
memset(&desktop->settings, 0, sizeof(desktop->settings));
SAVE_DESKTOP_SETTINGS(&desktop->settings);
}
scene_manager_next_scene(desktop->scene_manager, DesktopSceneMain);
if(furi_hal_lock_get()) {
if(furi_hal_rtc_is_flag_set(FuriHalRtcFlagLock)) {
furi_hal_usb_disable();
scene_manager_set_scene_state(
desktop->scene_manager, DesktopSceneLocked, DesktopLockedWithPin);

View file

@ -82,7 +82,7 @@ void desktop_start_new_idle_animation(DesktopAnimation* animation) {
DolphinStats stats = dolphin_stats(dolphin);
furi_record_close("dolphin");
furi_assert((stats.level >= 1) && (stats.level <= 3));
furi_check((stats.level >= 1) && (stats.level <= 3));
AnimationList_t animation_list;
AnimationList_init(animation_list);

View file

@ -2,7 +2,6 @@
#include "../views/desktop_lock_menu.h"
#include <toolbox/saved_struct.h>
#include <stdbool.h>
#include <furi-hal-lock.h>
void desktop_scene_lock_menu_callback(DesktopLockMenuEvent event, void* context) {
Desktop* desktop = (Desktop*)context;
@ -33,7 +32,7 @@ bool desktop_scene_lock_menu_on_event(void* context, SceneManagerEvent event) {
break;
case DesktopLockMenuEventPinLock:
if(desktop->settings.pincode.length > 0) {
furi_hal_lock_set(true);
furi_hal_rtc_set_flag(FuriHalRtcFlagLock);
furi_hal_usb_disable();
scene_manager_set_scene_state(
desktop->scene_manager, DesktopSceneLocked, DesktopLockedWithPin);

View file

@ -2,7 +2,6 @@
#include "../views/desktop_locked.h"
#include "desktop/helpers/desktop_animation.h"
#include "desktop/views/desktop_main.h"
#include <furi-hal-lock.h>
void desktop_scene_locked_callback(DesktopLockedEvent event, void* context) {
Desktop* desktop = (Desktop*)context;
@ -56,7 +55,7 @@ static bool desktop_scene_locked_check_pin(Desktop* desktop, DesktopMainEvent ev
if(match) {
desktop->pincode_buffer.length = 0;
furi_hal_usb_enable();
furi_hal_lock_set(false);
furi_hal_rtc_reset_flag(FuriHalRtcFlagLock);
desktop_main_unlocked(desktop->main_view);
}

View file

@ -103,8 +103,7 @@ int32_t passport_app(void* p) {
gui_add_view_port(gui, view_port, GuiLayerFullscreen);
view_port_update(view_port);
osStatus_t status = osSemaphoreAcquire(semaphore, osWaitForever);
furi_assert(status == osOK);
furi_check(osSemaphoreAcquire(semaphore, osWaitForever) == osOK);
gui_remove_view_port(gui, view_port);
view_port_free(view_port);

View file

@ -161,12 +161,36 @@ static LFSData* storage_int_lfs_data_alloc() {
return lfs_data;
};
static bool storage_int_is_fingerprint_valid(LFSData* lfs_data) {
bool value = true;
uint32_t os_fingerprint = 0;
os_fingerprint |= ((lfs_data->start_page & 0xFF) << 0);
os_fingerprint |= ((lfs_data->config.block_count & 0xFF) << 8);
os_fingerprint |= ((LFS_DISK_VERSION_MAJOR & 0xFFFF) << 16);
uint32_t rtc_fingerprint = furi_hal_rtc_get_register(FuriHalRtcRegisterLfsFingerprint);
if(rtc_fingerprint == 0) {
FURI_LOG_I(TAG, "Storing LFS fingerprint in RTC");
furi_hal_rtc_set_register(FuriHalRtcRegisterLfsFingerprint, os_fingerprint);
} else if(rtc_fingerprint != os_fingerprint) {
FURI_LOG_E(TAG, "LFS fingerprint mismatch");
furi_hal_rtc_set_register(FuriHalRtcRegisterLfsFingerprint, os_fingerprint);
value = false;
}
return value;
}
static void storage_int_lfs_mount(LFSData* lfs_data, StorageData* storage) {
int err;
lfs_t* lfs = &lfs_data->lfs;
if(furi_hal_rtc_is_flag_set(FuriHalRtcFlagFactoryReset)) {
// Factory reset
bool need_format = furi_hal_rtc_is_flag_set(FuriHalRtcFlagFactoryReset) ||
!storage_int_is_fingerprint_valid(lfs_data);
if(need_format) {
// Format storage
err = lfs_format(lfs, &lfs_data->config);
if(err == 0) {
FURI_LOG_I(TAG, "Factory reset: Format successful, trying to mount");

View file

@ -1,5 +1,5 @@
#include <furi-hal-bootloader.h>
#include <stm32wbxx_ll_rtc.h>
#include <furi-hal-rtc.h>
#include <furi.h>
#define TAG "FuriHalBoot"
@ -11,15 +11,15 @@
void furi_hal_bootloader_init() {
#ifndef DEBUG
LL_RTC_BAK_SetRegister(RTC, LL_RTC_BKP_DR0, BOOT_REQUEST_TAINTED);
furi_hal_rtc_set_register(FuriHalRtcRegisterBoot, BOOT_REQUEST_TAINTED);
#endif
FURI_LOG_I(TAG, "Init OK");
}
void furi_hal_bootloader_set_mode(FuriHalBootloaderMode mode) {
if (mode == FuriHalBootloaderModeNormal) {
LL_RTC_BAK_SetRegister(RTC, LL_RTC_BKP_DR0, BOOT_REQUEST_CLEAN);
furi_hal_rtc_set_register(FuriHalRtcRegisterBoot, BOOT_REQUEST_CLEAN);
} else if (mode == FuriHalBootloaderModeDFU) {
LL_RTC_BAK_SetRegister(RTC, LL_RTC_BKP_DR0, BOOT_REQUEST_DFU);
furi_hal_rtc_set_register(FuriHalRtcRegisterBoot, BOOT_REQUEST_DFU);
}
}

View file

@ -1,17 +0,0 @@
#include "furi-hal-lock.h"
#include <stm32wbxx_ll_rtc.h>
#define FLIPPER_LOCKED_VALUE 0x5432FAFA
bool furi_hal_lock_get() {
return FLIPPER_LOCKED_VALUE == LL_RTC_BAK_GetRegister(RTC, LL_RTC_BKP_DR3);
}
void furi_hal_lock_set(bool locked) {
if (locked) {
LL_RTC_BAK_SetRegister(RTC, LL_RTC_BKP_DR3, FLIPPER_LOCKED_VALUE);
} else {
LL_RTC_BAK_SetRegister(RTC, LL_RTC_BKP_DR3, 0);
}
}

View file

@ -6,10 +6,6 @@
#define TAG "FuriHalRtc"
#define FURI_HAL_RTC_BOOT_FLAGS_REG LL_RTC_BKP_DR0
#define FURI_HAL_RTC_BOOT_VERSION_REG LL_RTC_BKP_DR1
#define FURI_HAL_RTC_SYSTEM_REG LL_RTC_BKP_DR2
typedef struct {
uint8_t log_level:4;
uint8_t log_reserved:4;
@ -17,6 +13,8 @@ typedef struct {
uint16_t reserved;
} DeveloperReg;
_Static_assert(sizeof(DeveloperReg) == 4, "DeveloperReg size mismatch");
void furi_hal_rtc_init() {
if(LL_RCC_GetRTCClockSource() != LL_RCC_RTC_CLKSOURCE_LSE) {
LL_RCC_ForceBackupDomainReset();
@ -38,33 +36,46 @@ void furi_hal_rtc_init() {
FURI_LOG_I(TAG, "Init OK");
}
uint32_t furi_hal_rtc_get_register(FuriHalRtcRegister reg) {
return LL_RTC_BAK_GetRegister(RTC, reg);
}
void furi_hal_rtc_set_register(FuriHalRtcRegister reg, uint32_t value) {
LL_RTC_BAK_SetRegister(RTC, reg, value);
}
void furi_hal_rtc_set_log_level(uint8_t level) {
uint32_t data = LL_RTC_BAK_GetRegister(RTC, FURI_HAL_RTC_SYSTEM_REG);
((DeveloperReg*)&data)->log_level = level;
LL_RTC_BAK_SetRegister(RTC, FURI_HAL_RTC_SYSTEM_REG, data);
uint32_t data_reg = furi_hal_rtc_get_register(FuriHalRtcRegisterSystem);
DeveloperReg* data = (DeveloperReg*)&data_reg;
data->log_level = level;
furi_hal_rtc_set_register(FuriHalRtcRegisterSystem, data_reg);
furi_log_set_level(level);
}
uint8_t furi_hal_rtc_get_log_level() {
uint32_t data = LL_RTC_BAK_GetRegister(RTC, FURI_HAL_RTC_SYSTEM_REG);
return ((DeveloperReg*)&data)->log_level;
uint32_t data_reg = furi_hal_rtc_get_register(FuriHalRtcRegisterSystem);
DeveloperReg* data = (DeveloperReg*)&data_reg;
return data->log_level;
}
void furi_hal_rtc_set_flag(FuriHalRtcFlag flag) {
uint32_t data = LL_RTC_BAK_GetRegister(RTC, FURI_HAL_RTC_SYSTEM_REG);
((DeveloperReg*)&data)->flags |= flag;
LL_RTC_BAK_SetRegister(RTC, FURI_HAL_RTC_SYSTEM_REG, data);
uint32_t data_reg = furi_hal_rtc_get_register(FuriHalRtcRegisterSystem);
DeveloperReg* data = (DeveloperReg*)&data_reg;
data->flags |= flag;
furi_hal_rtc_set_register(FuriHalRtcRegisterSystem, data_reg);
}
void furi_hal_rtc_reset_flag(FuriHalRtcFlag flag) {
uint32_t data = LL_RTC_BAK_GetRegister(RTC, FURI_HAL_RTC_SYSTEM_REG);
((DeveloperReg*)&data)->flags &= ~flag;
LL_RTC_BAK_SetRegister(RTC, FURI_HAL_RTC_SYSTEM_REG, data);
uint32_t data_reg = furi_hal_rtc_get_register(FuriHalRtcRegisterSystem);
DeveloperReg* data = (DeveloperReg*)&data_reg;
data->flags &= ~flag;
furi_hal_rtc_set_register(FuriHalRtcRegisterSystem, data_reg);
}
bool furi_hal_rtc_is_flag_set(FuriHalRtcFlag flag) {
uint32_t data = LL_RTC_BAK_GetRegister(RTC, FURI_HAL_RTC_SYSTEM_REG);
return ((DeveloperReg*)&data)->flags & flag;
uint32_t data_reg = furi_hal_rtc_get_register(FuriHalRtcRegisterSystem);
DeveloperReg* data = (DeveloperReg*)&data_reg;
return data->flags & flag;
}
void furi_hal_rtc_set_datetime(FuriHalRtcDateTime* datetime) {

View file

@ -1,4 +1,5 @@
#include <furi-hal-version.h>
#include <furi-hal-rtc.h>
#include <furi.h>
#include <stm32wbxx.h>
@ -193,6 +194,9 @@ void furi_hal_version_init() {
break;
default: furi_crash(NULL);
}
furi_hal_rtc_set_register(FuriHalRtcRegisterSystemVersion, (uint32_t)version_get());
FURI_LOG_I(TAG, "Init OK");
}
@ -283,7 +287,7 @@ const struct Version* furi_hal_version_get_bootloader_version(void) {
return 0;
#else
/* Backup register which points to structure in flash memory */
return (const struct Version*)LL_RTC_BAK_GetRegister(RTC, LL_RTC_BKP_DR1);
return (const struct Version*)furi_hal_rtc_get_register(FuriHalRtcRegisterBootVersion);
#endif
}

View file

@ -1,5 +1,5 @@
#include <furi-hal-bootloader.h>
#include <stm32wbxx_ll_rtc.h>
#include <furi-hal-rtc.h>
#include <furi.h>
#define TAG "FuriHalBoot"
@ -11,15 +11,15 @@
void furi_hal_bootloader_init() {
#ifndef DEBUG
LL_RTC_BAK_SetRegister(RTC, LL_RTC_BKP_DR0, BOOT_REQUEST_TAINTED);
furi_hal_rtc_set_register(FuriHalRtcRegisterBoot, BOOT_REQUEST_TAINTED);
#endif
FURI_LOG_I(TAG, "Init OK");
}
void furi_hal_bootloader_set_mode(FuriHalBootloaderMode mode) {
if (mode == FuriHalBootloaderModeNormal) {
LL_RTC_BAK_SetRegister(RTC, LL_RTC_BKP_DR0, BOOT_REQUEST_CLEAN);
furi_hal_rtc_set_register(FuriHalRtcRegisterBoot, BOOT_REQUEST_CLEAN);
} else if (mode == FuriHalBootloaderModeDFU) {
LL_RTC_BAK_SetRegister(RTC, LL_RTC_BKP_DR0, BOOT_REQUEST_DFU);
furi_hal_rtc_set_register(FuriHalRtcRegisterBoot, BOOT_REQUEST_DFU);
}
}

View file

@ -1,17 +0,0 @@
#include "furi-hal-lock.h"
#include <stm32wbxx_ll_rtc.h>
#define FLIPPER_LOCKED_VALUE 0x5432FAFA
bool furi_hal_lock_get() {
return FLIPPER_LOCKED_VALUE == LL_RTC_BAK_GetRegister(RTC, LL_RTC_BKP_DR3);
}
void furi_hal_lock_set(bool locked) {
if (locked) {
LL_RTC_BAK_SetRegister(RTC, LL_RTC_BKP_DR3, FLIPPER_LOCKED_VALUE);
} else {
LL_RTC_BAK_SetRegister(RTC, LL_RTC_BKP_DR3, 0);
}
}

View file

@ -6,10 +6,6 @@
#define TAG "FuriHalRtc"
#define FURI_HAL_RTC_BOOT_FLAGS_REG LL_RTC_BKP_DR0
#define FURI_HAL_RTC_BOOT_VERSION_REG LL_RTC_BKP_DR1
#define FURI_HAL_RTC_SYSTEM_REG LL_RTC_BKP_DR2
typedef struct {
uint8_t log_level:4;
uint8_t log_reserved:4;
@ -17,6 +13,8 @@ typedef struct {
uint16_t reserved;
} DeveloperReg;
_Static_assert(sizeof(DeveloperReg) == 4, "DeveloperReg size mismatch");
void furi_hal_rtc_init() {
if(LL_RCC_GetRTCClockSource() != LL_RCC_RTC_CLKSOURCE_LSE) {
LL_RCC_ForceBackupDomainReset();
@ -38,33 +36,46 @@ void furi_hal_rtc_init() {
FURI_LOG_I(TAG, "Init OK");
}
uint32_t furi_hal_rtc_get_register(FuriHalRtcRegister reg) {
return LL_RTC_BAK_GetRegister(RTC, reg);
}
void furi_hal_rtc_set_register(FuriHalRtcRegister reg, uint32_t value) {
LL_RTC_BAK_SetRegister(RTC, reg, value);
}
void furi_hal_rtc_set_log_level(uint8_t level) {
uint32_t data = LL_RTC_BAK_GetRegister(RTC, FURI_HAL_RTC_SYSTEM_REG);
((DeveloperReg*)&data)->log_level = level;
LL_RTC_BAK_SetRegister(RTC, FURI_HAL_RTC_SYSTEM_REG, data);
uint32_t data_reg = furi_hal_rtc_get_register(FuriHalRtcRegisterSystem);
DeveloperReg* data = (DeveloperReg*)&data_reg;
data->log_level = level;
furi_hal_rtc_set_register(FuriHalRtcRegisterSystem, data_reg);
furi_log_set_level(level);
}
uint8_t furi_hal_rtc_get_log_level() {
uint32_t data = LL_RTC_BAK_GetRegister(RTC, FURI_HAL_RTC_SYSTEM_REG);
return ((DeveloperReg*)&data)->log_level;
uint32_t data_reg = furi_hal_rtc_get_register(FuriHalRtcRegisterSystem);
DeveloperReg* data = (DeveloperReg*)&data_reg;
return data->log_level;
}
void furi_hal_rtc_set_flag(FuriHalRtcFlag flag) {
uint32_t data = LL_RTC_BAK_GetRegister(RTC, FURI_HAL_RTC_SYSTEM_REG);
((DeveloperReg*)&data)->flags |= flag;
LL_RTC_BAK_SetRegister(RTC, FURI_HAL_RTC_SYSTEM_REG, data);
uint32_t data_reg = furi_hal_rtc_get_register(FuriHalRtcRegisterSystem);
DeveloperReg* data = (DeveloperReg*)&data_reg;
data->flags |= flag;
furi_hal_rtc_set_register(FuriHalRtcRegisterSystem, data_reg);
}
void furi_hal_rtc_reset_flag(FuriHalRtcFlag flag) {
uint32_t data = LL_RTC_BAK_GetRegister(RTC, FURI_HAL_RTC_SYSTEM_REG);
((DeveloperReg*)&data)->flags &= ~flag;
LL_RTC_BAK_SetRegister(RTC, FURI_HAL_RTC_SYSTEM_REG, data);
uint32_t data_reg = furi_hal_rtc_get_register(FuriHalRtcRegisterSystem);
DeveloperReg* data = (DeveloperReg*)&data_reg;
data->flags &= ~flag;
furi_hal_rtc_set_register(FuriHalRtcRegisterSystem, data_reg);
}
bool furi_hal_rtc_is_flag_set(FuriHalRtcFlag flag) {
uint32_t data = LL_RTC_BAK_GetRegister(RTC, FURI_HAL_RTC_SYSTEM_REG);
return ((DeveloperReg*)&data)->flags & flag;
uint32_t data_reg = furi_hal_rtc_get_register(FuriHalRtcRegisterSystem);
DeveloperReg* data = (DeveloperReg*)&data_reg;
return data->flags & flag;
}
void furi_hal_rtc_set_datetime(FuriHalRtcDateTime* datetime) {

View file

@ -1,4 +1,5 @@
#include <furi-hal-version.h>
#include <furi-hal-rtc.h>
#include <furi.h>
#include <stm32wbxx.h>
@ -193,6 +194,9 @@ void furi_hal_version_init() {
break;
default: furi_crash(NULL);
}
furi_hal_rtc_set_register(FuriHalRtcRegisterSystemVersion, (uint32_t)version_get());
FURI_LOG_I(TAG, "Init OK");
}
@ -283,7 +287,7 @@ const struct Version* furi_hal_version_get_bootloader_version(void) {
return 0;
#else
/* Backup register which points to structure in flash memory */
return (const struct Version*)LL_RTC_BAK_GetRegister(RTC, LL_RTC_BKP_DR1);
return (const struct Version*)furi_hal_rtc_get_register(FuriHalRtcRegisterBootVersion);
#endif
}

View file

@ -1,5 +0,0 @@
#pragma once
#include <stdbool.h>
bool furi_hal_lock_get();
void furi_hal_lock_set(bool locked);

View file

@ -28,11 +28,24 @@ typedef struct {
typedef enum {
FuriHalRtcFlagDebug = (1<<0),
FuriHalRtcFlagFactoryReset = (1<<1),
FuriHalRtcFlagLock = (1<<2),
} FuriHalRtcFlag;
typedef enum {
FuriHalRtcRegisterBoot,
FuriHalRtcRegisterBootVersion,
FuriHalRtcRegisterSystem,
FuriHalRtcRegisterSystemVersion,
FuriHalRtcRegisterLfsFingerprint,
} FuriHalRtcRegister;
/** Initialize RTC subsystem */
void furi_hal_rtc_init();
uint32_t furi_hal_rtc_get_register(FuriHalRtcRegister reg);
void furi_hal_rtc_set_register(FuriHalRtcRegister reg, uint32_t value);
void furi_hal_rtc_set_log_level(uint8_t level);
uint8_t furi_hal_rtc_get_log_level();