Storage: lfs config fingerprinting. RTC: fix data collision in lock register, refactor and cleanup. (#928)
This commit is contained in:
		
							parent
							
								
									79e0aed1e6
								
							
						
					
					
						commit
						7cea359be8
					
				@ -8,7 +8,6 @@
 | 
				
			|||||||
#include "portmacro.h"
 | 
					#include "portmacro.h"
 | 
				
			||||||
#include "storage/filesystem-api-defines.h"
 | 
					#include "storage/filesystem-api-defines.h"
 | 
				
			||||||
#include "storage/storage.h"
 | 
					#include "storage/storage.h"
 | 
				
			||||||
#include <furi-hal-lock.h>
 | 
					 | 
				
			||||||
#include <stdint.h>
 | 
					#include <stdint.h>
 | 
				
			||||||
#include <power/power_service/power.h>
 | 
					#include <power/power_service/power.h>
 | 
				
			||||||
#include "helpers/desktop_animation.h"
 | 
					#include "helpers/desktop_animation.h"
 | 
				
			||||||
@ -155,14 +154,14 @@ int32_t desktop_srv(void* p) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    bool loaded = LOAD_DESKTOP_SETTINGS(&desktop->settings);
 | 
					    bool loaded = LOAD_DESKTOP_SETTINGS(&desktop->settings);
 | 
				
			||||||
    if(!loaded) {
 | 
					    if(!loaded) {
 | 
				
			||||||
        furi_hal_lock_set(false);
 | 
					        furi_hal_rtc_reset_flag(FuriHalRtcFlagLock);
 | 
				
			||||||
        memset(&desktop->settings, 0, sizeof(desktop->settings));
 | 
					        memset(&desktop->settings, 0, sizeof(desktop->settings));
 | 
				
			||||||
        SAVE_DESKTOP_SETTINGS(&desktop->settings);
 | 
					        SAVE_DESKTOP_SETTINGS(&desktop->settings);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    scene_manager_next_scene(desktop->scene_manager, DesktopSceneMain);
 | 
					    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();
 | 
					        furi_hal_usb_disable();
 | 
				
			||||||
        scene_manager_set_scene_state(
 | 
					        scene_manager_set_scene_state(
 | 
				
			||||||
            desktop->scene_manager, DesktopSceneLocked, DesktopLockedWithPin);
 | 
					            desktop->scene_manager, DesktopSceneLocked, DesktopLockedWithPin);
 | 
				
			||||||
 | 
				
			|||||||
@ -82,7 +82,7 @@ void desktop_start_new_idle_animation(DesktopAnimation* animation) {
 | 
				
			|||||||
    DolphinStats stats = dolphin_stats(dolphin);
 | 
					    DolphinStats stats = dolphin_stats(dolphin);
 | 
				
			||||||
    furi_record_close("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_t animation_list;
 | 
				
			||||||
    AnimationList_init(animation_list);
 | 
					    AnimationList_init(animation_list);
 | 
				
			||||||
 | 
				
			|||||||
@ -2,7 +2,6 @@
 | 
				
			|||||||
#include "../views/desktop_lock_menu.h"
 | 
					#include "../views/desktop_lock_menu.h"
 | 
				
			||||||
#include <toolbox/saved_struct.h>
 | 
					#include <toolbox/saved_struct.h>
 | 
				
			||||||
#include <stdbool.h>
 | 
					#include <stdbool.h>
 | 
				
			||||||
#include <furi-hal-lock.h>
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
void desktop_scene_lock_menu_callback(DesktopLockMenuEvent event, void* context) {
 | 
					void desktop_scene_lock_menu_callback(DesktopLockMenuEvent event, void* context) {
 | 
				
			||||||
    Desktop* desktop = (Desktop*)context;
 | 
					    Desktop* desktop = (Desktop*)context;
 | 
				
			||||||
@ -33,7 +32,7 @@ bool desktop_scene_lock_menu_on_event(void* context, SceneManagerEvent event) {
 | 
				
			|||||||
            break;
 | 
					            break;
 | 
				
			||||||
        case DesktopLockMenuEventPinLock:
 | 
					        case DesktopLockMenuEventPinLock:
 | 
				
			||||||
            if(desktop->settings.pincode.length > 0) {
 | 
					            if(desktop->settings.pincode.length > 0) {
 | 
				
			||||||
                furi_hal_lock_set(true);
 | 
					                furi_hal_rtc_set_flag(FuriHalRtcFlagLock);
 | 
				
			||||||
                furi_hal_usb_disable();
 | 
					                furi_hal_usb_disable();
 | 
				
			||||||
                scene_manager_set_scene_state(
 | 
					                scene_manager_set_scene_state(
 | 
				
			||||||
                    desktop->scene_manager, DesktopSceneLocked, DesktopLockedWithPin);
 | 
					                    desktop->scene_manager, DesktopSceneLocked, DesktopLockedWithPin);
 | 
				
			||||||
 | 
				
			|||||||
@ -2,7 +2,6 @@
 | 
				
			|||||||
#include "../views/desktop_locked.h"
 | 
					#include "../views/desktop_locked.h"
 | 
				
			||||||
#include "desktop/helpers/desktop_animation.h"
 | 
					#include "desktop/helpers/desktop_animation.h"
 | 
				
			||||||
#include "desktop/views/desktop_main.h"
 | 
					#include "desktop/views/desktop_main.h"
 | 
				
			||||||
#include <furi-hal-lock.h>
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
void desktop_scene_locked_callback(DesktopLockedEvent event, void* context) {
 | 
					void desktop_scene_locked_callback(DesktopLockedEvent event, void* context) {
 | 
				
			||||||
    Desktop* desktop = (Desktop*)context;
 | 
					    Desktop* desktop = (Desktop*)context;
 | 
				
			||||||
@ -56,7 +55,7 @@ static bool desktop_scene_locked_check_pin(Desktop* desktop, DesktopMainEvent ev
 | 
				
			|||||||
    if(match) {
 | 
					    if(match) {
 | 
				
			||||||
        desktop->pincode_buffer.length = 0;
 | 
					        desktop->pincode_buffer.length = 0;
 | 
				
			||||||
        furi_hal_usb_enable();
 | 
					        furi_hal_usb_enable();
 | 
				
			||||||
        furi_hal_lock_set(false);
 | 
					        furi_hal_rtc_reset_flag(FuriHalRtcFlagLock);
 | 
				
			||||||
        desktop_main_unlocked(desktop->main_view);
 | 
					        desktop_main_unlocked(desktop->main_view);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -103,8 +103,7 @@ int32_t passport_app(void* p) {
 | 
				
			|||||||
    gui_add_view_port(gui, view_port, GuiLayerFullscreen);
 | 
					    gui_add_view_port(gui, view_port, GuiLayerFullscreen);
 | 
				
			||||||
    view_port_update(view_port);
 | 
					    view_port_update(view_port);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    osStatus_t status = osSemaphoreAcquire(semaphore, osWaitForever);
 | 
					    furi_check(osSemaphoreAcquire(semaphore, osWaitForever) == osOK);
 | 
				
			||||||
    furi_assert(status == osOK);
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    gui_remove_view_port(gui, view_port);
 | 
					    gui_remove_view_port(gui, view_port);
 | 
				
			||||||
    view_port_free(view_port);
 | 
					    view_port_free(view_port);
 | 
				
			||||||
 | 
				
			|||||||
@ -161,12 +161,36 @@ static LFSData* storage_int_lfs_data_alloc() {
 | 
				
			|||||||
    return lfs_data;
 | 
					    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) {
 | 
					static void storage_int_lfs_mount(LFSData* lfs_data, StorageData* storage) {
 | 
				
			||||||
    int err;
 | 
					    int err;
 | 
				
			||||||
    lfs_t* lfs = &lfs_data->lfs;
 | 
					    lfs_t* lfs = &lfs_data->lfs;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if(furi_hal_rtc_is_flag_set(FuriHalRtcFlagFactoryReset)) {
 | 
					    bool need_format = furi_hal_rtc_is_flag_set(FuriHalRtcFlagFactoryReset) ||
 | 
				
			||||||
        // Factory reset
 | 
					                       !storage_int_is_fingerprint_valid(lfs_data);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if(need_format) {
 | 
				
			||||||
 | 
					        // Format storage
 | 
				
			||||||
        err = lfs_format(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: Format successful, trying to mount");
 | 
				
			||||||
 | 
				
			|||||||
@ -1,5 +1,5 @@
 | 
				
			|||||||
#include <furi-hal-bootloader.h>
 | 
					#include <furi-hal-bootloader.h>
 | 
				
			||||||
#include <stm32wbxx_ll_rtc.h>
 | 
					#include <furi-hal-rtc.h>
 | 
				
			||||||
#include <furi.h>
 | 
					#include <furi.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#define TAG "FuriHalBoot"
 | 
					#define TAG "FuriHalBoot"
 | 
				
			||||||
@ -11,15 +11,15 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
void furi_hal_bootloader_init() {
 | 
					void furi_hal_bootloader_init() {
 | 
				
			||||||
#ifndef DEBUG
 | 
					#ifndef DEBUG
 | 
				
			||||||
    LL_RTC_BAK_SetRegister(RTC, LL_RTC_BKP_DR0, BOOT_REQUEST_TAINTED);
 | 
					    furi_hal_rtc_set_register(FuriHalRtcRegisterBoot, BOOT_REQUEST_TAINTED);
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
    FURI_LOG_I(TAG, "Init OK");
 | 
					    FURI_LOG_I(TAG, "Init OK");
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void furi_hal_bootloader_set_mode(FuriHalBootloaderMode mode) {
 | 
					void furi_hal_bootloader_set_mode(FuriHalBootloaderMode mode) {
 | 
				
			||||||
    if (mode == FuriHalBootloaderModeNormal) {
 | 
					    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) {
 | 
					    } 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);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -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);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
@ -6,10 +6,6 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
#define TAG "FuriHalRtc"
 | 
					#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 {
 | 
					typedef struct {
 | 
				
			||||||
    uint8_t log_level:4;
 | 
					    uint8_t log_level:4;
 | 
				
			||||||
    uint8_t log_reserved:4;
 | 
					    uint8_t log_reserved:4;
 | 
				
			||||||
@ -17,6 +13,8 @@ typedef struct {
 | 
				
			|||||||
    uint16_t reserved;
 | 
					    uint16_t reserved;
 | 
				
			||||||
} DeveloperReg;
 | 
					} DeveloperReg;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					_Static_assert(sizeof(DeveloperReg) == 4, "DeveloperReg size mismatch");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void furi_hal_rtc_init() {
 | 
					void furi_hal_rtc_init() {
 | 
				
			||||||
    if(LL_RCC_GetRTCClockSource() != LL_RCC_RTC_CLKSOURCE_LSE) {
 | 
					    if(LL_RCC_GetRTCClockSource() != LL_RCC_RTC_CLKSOURCE_LSE) {
 | 
				
			||||||
        LL_RCC_ForceBackupDomainReset();
 | 
					        LL_RCC_ForceBackupDomainReset();
 | 
				
			||||||
@ -38,33 +36,46 @@ void furi_hal_rtc_init() {
 | 
				
			|||||||
    FURI_LOG_I(TAG, "Init OK");
 | 
					    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) {
 | 
					void furi_hal_rtc_set_log_level(uint8_t level) {
 | 
				
			||||||
    uint32_t data = LL_RTC_BAK_GetRegister(RTC, FURI_HAL_RTC_SYSTEM_REG);
 | 
					    uint32_t data_reg = furi_hal_rtc_get_register(FuriHalRtcRegisterSystem);
 | 
				
			||||||
    ((DeveloperReg*)&data)->log_level = level;
 | 
					    DeveloperReg* data = (DeveloperReg*)&data_reg;
 | 
				
			||||||
    LL_RTC_BAK_SetRegister(RTC, FURI_HAL_RTC_SYSTEM_REG, data);
 | 
					    data->log_level = level;
 | 
				
			||||||
 | 
					    furi_hal_rtc_set_register(FuriHalRtcRegisterSystem, data_reg);
 | 
				
			||||||
    furi_log_set_level(level);
 | 
					    furi_log_set_level(level);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
uint8_t furi_hal_rtc_get_log_level() {
 | 
					uint8_t furi_hal_rtc_get_log_level() {
 | 
				
			||||||
    uint32_t data = LL_RTC_BAK_GetRegister(RTC, FURI_HAL_RTC_SYSTEM_REG);
 | 
					    uint32_t data_reg = furi_hal_rtc_get_register(FuriHalRtcRegisterSystem);
 | 
				
			||||||
    return ((DeveloperReg*)&data)->log_level;
 | 
					    DeveloperReg* data = (DeveloperReg*)&data_reg;
 | 
				
			||||||
 | 
					    return data->log_level;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void furi_hal_rtc_set_flag(FuriHalRtcFlag flag) {
 | 
					void furi_hal_rtc_set_flag(FuriHalRtcFlag flag) {
 | 
				
			||||||
    uint32_t data = LL_RTC_BAK_GetRegister(RTC, FURI_HAL_RTC_SYSTEM_REG);
 | 
					    uint32_t data_reg = furi_hal_rtc_get_register(FuriHalRtcRegisterSystem);
 | 
				
			||||||
    ((DeveloperReg*)&data)->flags |= flag;
 | 
					    DeveloperReg* data = (DeveloperReg*)&data_reg;
 | 
				
			||||||
    LL_RTC_BAK_SetRegister(RTC, FURI_HAL_RTC_SYSTEM_REG, data);
 | 
					    data->flags |= flag;
 | 
				
			||||||
 | 
					    furi_hal_rtc_set_register(FuriHalRtcRegisterSystem, data_reg);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void furi_hal_rtc_reset_flag(FuriHalRtcFlag flag) {
 | 
					void furi_hal_rtc_reset_flag(FuriHalRtcFlag flag) {
 | 
				
			||||||
    uint32_t data = LL_RTC_BAK_GetRegister(RTC, FURI_HAL_RTC_SYSTEM_REG);
 | 
					    uint32_t data_reg = furi_hal_rtc_get_register(FuriHalRtcRegisterSystem);
 | 
				
			||||||
    ((DeveloperReg*)&data)->flags &= ~flag;
 | 
					    DeveloperReg* data = (DeveloperReg*)&data_reg;
 | 
				
			||||||
    LL_RTC_BAK_SetRegister(RTC, FURI_HAL_RTC_SYSTEM_REG, data);
 | 
					    data->flags &= ~flag;
 | 
				
			||||||
 | 
					    furi_hal_rtc_set_register(FuriHalRtcRegisterSystem, data_reg);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
bool furi_hal_rtc_is_flag_set(FuriHalRtcFlag flag) {
 | 
					bool furi_hal_rtc_is_flag_set(FuriHalRtcFlag flag) {
 | 
				
			||||||
    uint32_t data = LL_RTC_BAK_GetRegister(RTC, FURI_HAL_RTC_SYSTEM_REG);
 | 
					    uint32_t data_reg = furi_hal_rtc_get_register(FuriHalRtcRegisterSystem);
 | 
				
			||||||
    return ((DeveloperReg*)&data)->flags & flag;
 | 
					    DeveloperReg* data = (DeveloperReg*)&data_reg;
 | 
				
			||||||
 | 
					    return data->flags & flag;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void furi_hal_rtc_set_datetime(FuriHalRtcDateTime* datetime) {
 | 
					void furi_hal_rtc_set_datetime(FuriHalRtcDateTime* datetime) {
 | 
				
			||||||
 | 
				
			|||||||
@ -1,4 +1,5 @@
 | 
				
			|||||||
#include <furi-hal-version.h>
 | 
					#include <furi-hal-version.h>
 | 
				
			||||||
 | 
					#include <furi-hal-rtc.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include <furi.h>
 | 
					#include <furi.h>
 | 
				
			||||||
#include <stm32wbxx.h>
 | 
					#include <stm32wbxx.h>
 | 
				
			||||||
@ -193,6 +194,9 @@ void furi_hal_version_init() {
 | 
				
			|||||||
        break;
 | 
					        break;
 | 
				
			||||||
        default: furi_crash(NULL);
 | 
					        default: furi_crash(NULL);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    furi_hal_rtc_set_register(FuriHalRtcRegisterSystemVersion, (uint32_t)version_get());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    FURI_LOG_I(TAG, "Init OK");
 | 
					    FURI_LOG_I(TAG, "Init OK");
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -283,7 +287,7 @@ const struct Version* furi_hal_version_get_bootloader_version(void) {
 | 
				
			|||||||
    return 0;
 | 
					    return 0;
 | 
				
			||||||
#else
 | 
					#else
 | 
				
			||||||
    /* Backup register which points to structure in flash memory */
 | 
					    /* 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
 | 
					#endif
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -1,5 +1,5 @@
 | 
				
			|||||||
#include <furi-hal-bootloader.h>
 | 
					#include <furi-hal-bootloader.h>
 | 
				
			||||||
#include <stm32wbxx_ll_rtc.h>
 | 
					#include <furi-hal-rtc.h>
 | 
				
			||||||
#include <furi.h>
 | 
					#include <furi.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#define TAG "FuriHalBoot"
 | 
					#define TAG "FuriHalBoot"
 | 
				
			||||||
@ -11,15 +11,15 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
void furi_hal_bootloader_init() {
 | 
					void furi_hal_bootloader_init() {
 | 
				
			||||||
#ifndef DEBUG
 | 
					#ifndef DEBUG
 | 
				
			||||||
    LL_RTC_BAK_SetRegister(RTC, LL_RTC_BKP_DR0, BOOT_REQUEST_TAINTED);
 | 
					    furi_hal_rtc_set_register(FuriHalRtcRegisterBoot, BOOT_REQUEST_TAINTED);
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
    FURI_LOG_I(TAG, "Init OK");
 | 
					    FURI_LOG_I(TAG, "Init OK");
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void furi_hal_bootloader_set_mode(FuriHalBootloaderMode mode) {
 | 
					void furi_hal_bootloader_set_mode(FuriHalBootloaderMode mode) {
 | 
				
			||||||
    if (mode == FuriHalBootloaderModeNormal) {
 | 
					    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) {
 | 
					    } 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);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -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);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
@ -6,10 +6,6 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
#define TAG "FuriHalRtc"
 | 
					#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 {
 | 
					typedef struct {
 | 
				
			||||||
    uint8_t log_level:4;
 | 
					    uint8_t log_level:4;
 | 
				
			||||||
    uint8_t log_reserved:4;
 | 
					    uint8_t log_reserved:4;
 | 
				
			||||||
@ -17,6 +13,8 @@ typedef struct {
 | 
				
			|||||||
    uint16_t reserved;
 | 
					    uint16_t reserved;
 | 
				
			||||||
} DeveloperReg;
 | 
					} DeveloperReg;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					_Static_assert(sizeof(DeveloperReg) == 4, "DeveloperReg size mismatch");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void furi_hal_rtc_init() {
 | 
					void furi_hal_rtc_init() {
 | 
				
			||||||
    if(LL_RCC_GetRTCClockSource() != LL_RCC_RTC_CLKSOURCE_LSE) {
 | 
					    if(LL_RCC_GetRTCClockSource() != LL_RCC_RTC_CLKSOURCE_LSE) {
 | 
				
			||||||
        LL_RCC_ForceBackupDomainReset();
 | 
					        LL_RCC_ForceBackupDomainReset();
 | 
				
			||||||
@ -38,33 +36,46 @@ void furi_hal_rtc_init() {
 | 
				
			|||||||
    FURI_LOG_I(TAG, "Init OK");
 | 
					    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) {
 | 
					void furi_hal_rtc_set_log_level(uint8_t level) {
 | 
				
			||||||
    uint32_t data = LL_RTC_BAK_GetRegister(RTC, FURI_HAL_RTC_SYSTEM_REG);
 | 
					    uint32_t data_reg = furi_hal_rtc_get_register(FuriHalRtcRegisterSystem);
 | 
				
			||||||
    ((DeveloperReg*)&data)->log_level = level;
 | 
					    DeveloperReg* data = (DeveloperReg*)&data_reg;
 | 
				
			||||||
    LL_RTC_BAK_SetRegister(RTC, FURI_HAL_RTC_SYSTEM_REG, data);
 | 
					    data->log_level = level;
 | 
				
			||||||
 | 
					    furi_hal_rtc_set_register(FuriHalRtcRegisterSystem, data_reg);
 | 
				
			||||||
    furi_log_set_level(level);
 | 
					    furi_log_set_level(level);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
uint8_t furi_hal_rtc_get_log_level() {
 | 
					uint8_t furi_hal_rtc_get_log_level() {
 | 
				
			||||||
    uint32_t data = LL_RTC_BAK_GetRegister(RTC, FURI_HAL_RTC_SYSTEM_REG);
 | 
					    uint32_t data_reg = furi_hal_rtc_get_register(FuriHalRtcRegisterSystem);
 | 
				
			||||||
    return ((DeveloperReg*)&data)->log_level;
 | 
					    DeveloperReg* data = (DeveloperReg*)&data_reg;
 | 
				
			||||||
 | 
					    return data->log_level;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void furi_hal_rtc_set_flag(FuriHalRtcFlag flag) {
 | 
					void furi_hal_rtc_set_flag(FuriHalRtcFlag flag) {
 | 
				
			||||||
    uint32_t data = LL_RTC_BAK_GetRegister(RTC, FURI_HAL_RTC_SYSTEM_REG);
 | 
					    uint32_t data_reg = furi_hal_rtc_get_register(FuriHalRtcRegisterSystem);
 | 
				
			||||||
    ((DeveloperReg*)&data)->flags |= flag;
 | 
					    DeveloperReg* data = (DeveloperReg*)&data_reg;
 | 
				
			||||||
    LL_RTC_BAK_SetRegister(RTC, FURI_HAL_RTC_SYSTEM_REG, data);
 | 
					    data->flags |= flag;
 | 
				
			||||||
 | 
					    furi_hal_rtc_set_register(FuriHalRtcRegisterSystem, data_reg);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void furi_hal_rtc_reset_flag(FuriHalRtcFlag flag) {
 | 
					void furi_hal_rtc_reset_flag(FuriHalRtcFlag flag) {
 | 
				
			||||||
    uint32_t data = LL_RTC_BAK_GetRegister(RTC, FURI_HAL_RTC_SYSTEM_REG);
 | 
					    uint32_t data_reg = furi_hal_rtc_get_register(FuriHalRtcRegisterSystem);
 | 
				
			||||||
    ((DeveloperReg*)&data)->flags &= ~flag;
 | 
					    DeveloperReg* data = (DeveloperReg*)&data_reg;
 | 
				
			||||||
    LL_RTC_BAK_SetRegister(RTC, FURI_HAL_RTC_SYSTEM_REG, data);
 | 
					    data->flags &= ~flag;
 | 
				
			||||||
 | 
					    furi_hal_rtc_set_register(FuriHalRtcRegisterSystem, data_reg);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
bool furi_hal_rtc_is_flag_set(FuriHalRtcFlag flag) {
 | 
					bool furi_hal_rtc_is_flag_set(FuriHalRtcFlag flag) {
 | 
				
			||||||
    uint32_t data = LL_RTC_BAK_GetRegister(RTC, FURI_HAL_RTC_SYSTEM_REG);
 | 
					    uint32_t data_reg = furi_hal_rtc_get_register(FuriHalRtcRegisterSystem);
 | 
				
			||||||
    return ((DeveloperReg*)&data)->flags & flag;
 | 
					    DeveloperReg* data = (DeveloperReg*)&data_reg;
 | 
				
			||||||
 | 
					    return data->flags & flag;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void furi_hal_rtc_set_datetime(FuriHalRtcDateTime* datetime) {
 | 
					void furi_hal_rtc_set_datetime(FuriHalRtcDateTime* datetime) {
 | 
				
			||||||
 | 
				
			|||||||
@ -1,4 +1,5 @@
 | 
				
			|||||||
#include <furi-hal-version.h>
 | 
					#include <furi-hal-version.h>
 | 
				
			||||||
 | 
					#include <furi-hal-rtc.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include <furi.h>
 | 
					#include <furi.h>
 | 
				
			||||||
#include <stm32wbxx.h>
 | 
					#include <stm32wbxx.h>
 | 
				
			||||||
@ -193,6 +194,9 @@ void furi_hal_version_init() {
 | 
				
			|||||||
        break;
 | 
					        break;
 | 
				
			||||||
        default: furi_crash(NULL);
 | 
					        default: furi_crash(NULL);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    furi_hal_rtc_set_register(FuriHalRtcRegisterSystemVersion, (uint32_t)version_get());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    FURI_LOG_I(TAG, "Init OK");
 | 
					    FURI_LOG_I(TAG, "Init OK");
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -283,7 +287,7 @@ const struct Version* furi_hal_version_get_bootloader_version(void) {
 | 
				
			|||||||
    return 0;
 | 
					    return 0;
 | 
				
			||||||
#else
 | 
					#else
 | 
				
			||||||
    /* Backup register which points to structure in flash memory */
 | 
					    /* 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
 | 
					#endif
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -1,5 +0,0 @@
 | 
				
			|||||||
#pragma once
 | 
					 | 
				
			||||||
#include <stdbool.h>
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
bool furi_hal_lock_get();
 | 
					 | 
				
			||||||
void furi_hal_lock_set(bool locked);
 | 
					 | 
				
			||||||
@ -28,11 +28,24 @@ typedef struct {
 | 
				
			|||||||
typedef enum {
 | 
					typedef enum {
 | 
				
			||||||
    FuriHalRtcFlagDebug = (1<<0),
 | 
					    FuriHalRtcFlagDebug = (1<<0),
 | 
				
			||||||
    FuriHalRtcFlagFactoryReset = (1<<1),
 | 
					    FuriHalRtcFlagFactoryReset = (1<<1),
 | 
				
			||||||
 | 
					    FuriHalRtcFlagLock = (1<<2),
 | 
				
			||||||
} FuriHalRtcFlag;
 | 
					} FuriHalRtcFlag;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					typedef enum {
 | 
				
			||||||
 | 
					    FuriHalRtcRegisterBoot,
 | 
				
			||||||
 | 
					    FuriHalRtcRegisterBootVersion,
 | 
				
			||||||
 | 
					    FuriHalRtcRegisterSystem,
 | 
				
			||||||
 | 
					    FuriHalRtcRegisterSystemVersion,
 | 
				
			||||||
 | 
					    FuriHalRtcRegisterLfsFingerprint,
 | 
				
			||||||
 | 
					} FuriHalRtcRegister;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/** Initialize RTC subsystem */
 | 
					/** Initialize RTC subsystem */
 | 
				
			||||||
void furi_hal_rtc_init();
 | 
					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);
 | 
					void furi_hal_rtc_set_log_level(uint8_t level);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
uint8_t furi_hal_rtc_get_log_level();
 | 
					uint8_t furi_hal_rtc_get_log_level();
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user