diff --git a/applications/desktop/desktop.c b/applications/desktop/desktop.c index aa82cdbb..9d4df2a7 100644 --- a/applications/desktop/desktop.c +++ b/applications/desktop/desktop.c @@ -8,7 +8,6 @@ #include "portmacro.h" #include "storage/filesystem-api-defines.h" #include "storage/storage.h" -#include #include #include #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); diff --git a/applications/desktop/helpers/desktop_animation.c b/applications/desktop/helpers/desktop_animation.c index 40f3b7e4..5fb26512 100644 --- a/applications/desktop/helpers/desktop_animation.c +++ b/applications/desktop/helpers/desktop_animation.c @@ -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); diff --git a/applications/desktop/scenes/desktop_scene_lock_menu.c b/applications/desktop/scenes/desktop_scene_lock_menu.c index 8a73739f..ee7ff9b4 100644 --- a/applications/desktop/scenes/desktop_scene_lock_menu.c +++ b/applications/desktop/scenes/desktop_scene_lock_menu.c @@ -2,7 +2,6 @@ #include "../views/desktop_lock_menu.h" #include #include -#include 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); diff --git a/applications/desktop/scenes/desktop_scene_locked.c b/applications/desktop/scenes/desktop_scene_locked.c index 78000acf..c6f73b6a 100644 --- a/applications/desktop/scenes/desktop_scene_locked.c +++ b/applications/desktop/scenes/desktop_scene_locked.c @@ -2,7 +2,6 @@ #include "../views/desktop_locked.h" #include "desktop/helpers/desktop_animation.h" #include "desktop/views/desktop_main.h" -#include 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); } diff --git a/applications/dolphin/passport/passport.c b/applications/dolphin/passport/passport.c index 4be35566..17cba7fd 100644 --- a/applications/dolphin/passport/passport.c +++ b/applications/dolphin/passport/passport.c @@ -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); diff --git a/applications/storage/storages/storage-int.c b/applications/storage/storages/storage-int.c index 097934b6..3351be3b 100644 --- a/applications/storage/storages/storage-int.c +++ b/applications/storage/storages/storage-int.c @@ -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"); diff --git a/firmware/targets/f6/furi-hal/furi-hal-bootloader.c b/firmware/targets/f6/furi-hal/furi-hal-bootloader.c index b5e7cb5c..eca6f947 100644 --- a/firmware/targets/f6/furi-hal/furi-hal-bootloader.c +++ b/firmware/targets/f6/furi-hal/furi-hal-bootloader.c @@ -1,5 +1,5 @@ #include -#include +#include #include #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); } } diff --git a/firmware/targets/f6/furi-hal/furi-hal-lock.c b/firmware/targets/f6/furi-hal/furi-hal-lock.c deleted file mode 100644 index 0f519380..00000000 --- a/firmware/targets/f6/furi-hal/furi-hal-lock.c +++ /dev/null @@ -1,17 +0,0 @@ -#include "furi-hal-lock.h" -#include - -#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); - } -} - diff --git a/firmware/targets/f6/furi-hal/furi-hal-rtc.c b/firmware/targets/f6/furi-hal/furi-hal-rtc.c index 20e77e13..45498077 100644 --- a/firmware/targets/f6/furi-hal/furi-hal-rtc.c +++ b/firmware/targets/f6/furi-hal/furi-hal-rtc.c @@ -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) { diff --git a/firmware/targets/f6/furi-hal/furi-hal-version.c b/firmware/targets/f6/furi-hal/furi-hal-version.c index 8f5f26d9..218a6be4 100644 --- a/firmware/targets/f6/furi-hal/furi-hal-version.c +++ b/firmware/targets/f6/furi-hal/furi-hal-version.c @@ -1,4 +1,5 @@ #include +#include #include #include @@ -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 } diff --git a/firmware/targets/f7/furi-hal/furi-hal-bootloader.c b/firmware/targets/f7/furi-hal/furi-hal-bootloader.c index b5e7cb5c..eca6f947 100644 --- a/firmware/targets/f7/furi-hal/furi-hal-bootloader.c +++ b/firmware/targets/f7/furi-hal/furi-hal-bootloader.c @@ -1,5 +1,5 @@ #include -#include +#include #include #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); } } diff --git a/firmware/targets/f7/furi-hal/furi-hal-lock.c b/firmware/targets/f7/furi-hal/furi-hal-lock.c deleted file mode 100644 index 0f519380..00000000 --- a/firmware/targets/f7/furi-hal/furi-hal-lock.c +++ /dev/null @@ -1,17 +0,0 @@ -#include "furi-hal-lock.h" -#include - -#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); - } -} - diff --git a/firmware/targets/f7/furi-hal/furi-hal-rtc.c b/firmware/targets/f7/furi-hal/furi-hal-rtc.c index 20e77e13..45498077 100644 --- a/firmware/targets/f7/furi-hal/furi-hal-rtc.c +++ b/firmware/targets/f7/furi-hal/furi-hal-rtc.c @@ -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) { diff --git a/firmware/targets/f7/furi-hal/furi-hal-version.c b/firmware/targets/f7/furi-hal/furi-hal-version.c index f38e6cdc..bd561c72 100644 --- a/firmware/targets/f7/furi-hal/furi-hal-version.c +++ b/firmware/targets/f7/furi-hal/furi-hal-version.c @@ -1,4 +1,5 @@ #include +#include #include #include @@ -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 } diff --git a/firmware/targets/furi-hal-include/furi-hal-lock.h b/firmware/targets/furi-hal-include/furi-hal-lock.h deleted file mode 100644 index d07ce571..00000000 --- a/firmware/targets/furi-hal-include/furi-hal-lock.h +++ /dev/null @@ -1,5 +0,0 @@ -#pragma once -#include - -bool furi_hal_lock_get(); -void furi_hal_lock_set(bool locked); diff --git a/firmware/targets/furi-hal-include/furi-hal-rtc.h b/firmware/targets/furi-hal-include/furi-hal-rtc.h index cc2f56f5..36bf218c 100644 --- a/firmware/targets/furi-hal-include/furi-hal-rtc.h +++ b/firmware/targets/furi-hal-include/furi-hal-rtc.h @@ -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();