rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
storage_flash.cpp
Go to the documentation of this file.
1/**
2 * @file storage_flash.cpp
3 * @brief Storage interface to internal MCU flash
4 *
5 * @date Sep 19, 2013
6 * @author Andrey Belomutskiy, (c) 2012-2025
7 */
8
9#include "pch.h"
10
11#include "storage.h"
12
13#if EFI_STORAGE_INT_FLASH == TRUE
14
15#include "mpu_util.h"
16#include "flash_int.h"
17
18class SettingStorageFlash : public SettingStorageBase {
19public:
20 bool isReady() override;
21 bool isIdSupported(size_t id) override;
22 StorageStatus store(size_t id, const uint8_t *ptr, size_t size) override;
23 StorageStatus read(size_t id, uint8_t *ptr, size_t size) override;
24 StorageStatus format() override;
25
26private:
27 flashaddr_t getIdAddress(size_t id);
28};
29
30flashaddr_t SettingStorageFlash::getIdAddress(size_t id) {
31 if (id == EFI_SETTINGS_RECORD_ID) {
32 return getFlashAddrFirstCopy();
33 } else if (id == EFI_SETTINGS_BACKUP_RECORD_ID) {
35 }
36
37 return 0;
38}
39
40bool SettingStorageFlash::isReady() {
41 return true;
42}
43
44bool SettingStorageFlash::isIdSupported(size_t id) {
45 return (getIdAddress(id) != 0);
46}
47
48StorageStatus SettingStorageFlash::store(size_t id, const uint8_t *ptr, size_t size) {
49 flashaddr_t addr = getIdAddress(id);
50
51 if (addr == 0) {
53 }
54
55 efiPrintf("Flash: Writing storage ID %d @0x%x... %d bytes", id, addr, size);
56 efitick_t startNt = getTimeNowNt();
57
59 // there's no wdgStop() for STM32, so we cannot disable it.
60 // we just set a long timeout of 5 secs to wait until flash is done.
61 startWatchdog(WATCHDOG_FLASH_TIMEOUT_MS);
62 }
63
65
66 auto err = intFlashErase(addr, size);
67 if (FLASH_RETURN_SUCCESS != err) {
68 efiPrintf("Flash: failed to erase flash at 0x%08x: %d", addr, err);
69 status = StorageStatus::Failed;
70 }
71
72 if (status == StorageStatus::Ok) {
73 err = intFlashWrite(addr, (const char*)ptr, size);
74 if (FLASH_RETURN_SUCCESS != err) {
75 efiPrintf("Flash: failed to write flash at 0x%08x: %d", addr, err);
76 status = StorageStatus::Failed;
77 }
78 }
79
80 efitick_t endNt = getTimeNowNt();
81 int elapsed_Ms = US2MS(NT2US(endNt - startNt));
82
84 // restart the watchdog with the default timeout
86 }
87
88 efiPrintf("SD: Write done after %d mS", elapsed_Ms);
89
90 return status;
91}
92
93StorageStatus SettingStorageFlash::read(size_t id, uint8_t *ptr, size_t size) {
94 flashaddr_t addr = getIdAddress(id);
95
96 if (addr == 0) {
98 }
99
100 efiPrintf("Flash: Reading storage ID %d @0x%x ... %d bytes", id, addr, size);
101
102 intFlashRead(addr, (char *)ptr, size);
103
104 efiPrintf("Flash: Reading done");
105
106 return StorageStatus::Ok;
107}
108
109StorageStatus SettingStorageFlash::format() {
110 /* TODO: actually erase settings erea(s)? */
111
113}
114
115static SettingStorageFlash storageFlash;
116
120
121#endif //EFI_STORAGE_SD
constexpr uint8_t addr
Definition ads1015.cpp:14
bool mcuCanFlashWhileRunning()
Definition mpu_util.cpp:10
void startWatchdog(int)
virtual StorageStatus read(size_t id, uint8_t *ptr, size_t size)=0
virtual StorageStatus store(size_t id, const uint8_t *ptr, size_t size)=0
virtual bool isReady()=0
virtual bool isIdSupported(size_t id)=0
virtual StorageStatus format()=0
return FLASH_RETURN_SUCCESS
Definition flash_int.cpp:80
efitick_t getTimeNowNt()
Definition efitime.cpp:19
int intFlashErase(flashaddr_t address, size_t size)
Erase the sectors containing the span of size bytes starting at address.
uintptr_t flashaddr_t
Address in the flash memory.
Definition flash_int.h:86
int intFlashWrite(flashaddr_t address, const char *buffer, size_t size)
Copy data from a buffer to the flash memory.
uintptr_t getFlashAddrFirstCopy(void)
Definition mpu_util.cpp:248
int intFlashRead(flashaddr_t source, char *destination, size_t size)
Copy data from the flash memory to a destination.
uintptr_t getFlashAddrSecondCopy(void)
Definition mpu_util.cpp:252
bool storageRegisterStorage(StorageType type, SettingStorageBase *storage)
Definition storage.cpp:194
@ STORAGE_INT_FLASH
Definition storage.h:38
@ EFI_SETTINGS_BACKUP_RECORD_ID
Definition storage.h:50
@ EFI_SETTINGS_RECORD_ID
Definition storage.h:49
StorageStatus
Definition storage.h:12
bool initStorageFlash()
static SettingStorageFlash storageFlash
composite packet size