rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
flash_main.cpp
Go to the documentation of this file.
1/**
2 * @file flash_main.cpp
3 * @brief Higher-level logic of saving data into internal flash memory
4 *
5 *
6 * @date Sep 19, 2013
7 * @author Andrey Belomutskiy, (c) 2012-2020
8 */
9
10#include "pch.h"
11
12#if EFI_CONFIGURATION_STORAGE || defined(EFI_UNIT_TEST)
13#include "storage.h"
14#endif
15
16/* If any setting storage is exist */
17#if EFI_CONFIGURATION_STORAGE
18
19#include "mpu_util.h"
20#include "flash_main.h"
21#include "eficonsole.h"
22
23#include "flash_int.h"
24
25#if EFI_TUNER_STUDIO
26#include "tunerstudio.h"
27#endif
28
29
30#include "runtime_state.h"
31
32#endif // EFI_CONFIGURATION_STORAGE
33
34/* If any setting storage is exist */
35#if EFI_CONFIGURATION_STORAGE
36
38 efiPrintf("Scheduling FORCED write");
39
41}
42
44 efiPrintf("Scheduling write");
45
47}
48
52
53/* TODO: extract to persistentState method */
55 engine->configBurnTimer.reset();
56
57 // Set up the container
59 persistentState.version = FLASH_DATA_VERSION;
61
62 // Do actual write
63 auto result1 = storageWrite(EFI_SETTINGS_RECORD_ID, (uint8_t *)&persistentState, sizeof(persistentState));
65
67
68 // Some MCU have no enough flash to store two copies. First one is mandatory.
69 return ((result1 == StorageStatus::Ok) &&
70 ((result2 == StorageStatus::Ok) || (result2 == StorageStatus::NotSupported)));
71}
72
74 auto flashCrc = persistentState.getCrc();
75
76 if (flashCrc != persistentState.crc) {
77 // If the stored crc is all 1s, that probably means the flash is actually blank, not that the crc failed.
78 if (persistentState.crc == ((decltype(persistentState.crc))-1)) {
80 } else {
82 }
83 } else if (persistentState.version != FLASH_DATA_VERSION || persistentState.size != sizeof(persistentState)) {
85 } else {
86 return StorageStatus::Ok;
87 }
88}
89
90/**
91 * this method could and should be executed before we have any
92 * connectivity so no console output here
93 *
94 * in this method we read first copy of configuration in flash. if that first copy has CRC or other issues we read second copy.
95 */
97 auto firstCopy = storageRead(EFI_SETTINGS_RECORD_ID, (uint8_t *)&persistentState, sizeof(persistentState));
98 if (firstCopy == StorageStatus::Ok) {
99 firstCopy = validatePersistentState();
100 if (firstCopy == StorageStatus::Ok) {
101 return StorageStatus::Ok;
102 }
103 }
104
105 auto secondCopy = storageRead(EFI_SETTINGS_BACKUP_RECORD_ID, (uint8_t *)&persistentState, sizeof(persistentState));
106 if (secondCopy == StorageStatus::Ok) {
107 secondCopy = validatePersistentState();
108 if (secondCopy == StorageStatus::Ok) {
109 return StorageStatus::Ok;
110 }
111 }
112
113 return firstCopy;
114}
115
118
119 switch (result) {
122 efiPrintf("Need to reset flash to default due to CRC mismatch");
123 [[fallthrough]];
127 resetConfigurationExt(DEFAULT_ENGINE_TYPE);
128 break;
130 // Preserve engine type from old config
131 efiPrintf("Resetting due to version mismatch but preserving engine type [%d]", (int)engineConfiguration->engineType);
133 break;
135 // At this point we know that CRC and version number is what we expect. Safe to assume it's a valid configuration.
137 efiPrintf("Read valid configuration from flash!");
138 break;
139 }
140
141 // we can only change the state after the CRC check
144}
145
147 // force settings write to storage
149}
150
154
155static void doRewriteConfig() {
157 // force settings write to storage
159}
160
164
165void initFlash() {
166 // Init storage(s) if any
167 // Note: may take some time
168 initStorage();
169
170 addConsoleAction("readconfig", readFromFlash);
171 /**
172 * This would write NOW (you should not be doing this while connected to real engine)
173 */
175
176 addConsoleAction("ltftwrite", doWriteLTFT);
177#if EFI_TUNER_STUDIO
178 /**
179 * This would schedule write to flash once the engine is stopped
180 */
181 addConsoleAction(CMD_BURNCONFIG, requestBurn);
182#endif
184 addConsoleAction("rewriteconfig", doRewriteConfig);
185}
186
187#endif /* EFI_CONFIGURATION_STORAGE */
void preCalculate()
Definition engine.cpp:324
Timer configBurnTimer
Definition engine.h:308
void addConsoleAction(const char *token, Void callback)
Register console action without parameters.
Console package entry point header.
static EngineAccessor engine
Definition engine.h:413
void applyNonPersistentConfiguration()
void resetConfigurationExt(configuration_callback_t boardCallback, engine_type_e engineType)
static constexpr engine_configuration_s * engineConfiguration
persistent_config_container_s persistentState
bool warning(ObdCode code, const char *fmt,...)
int getRusEfiVersion()
void initFlash()
static StorageStatus validatePersistentState()
bool writeToFlashNowImpl()
void setNeedToWriteConfiguration()
static void doWriteLTFT()
static StorageStatus readConfiguration()
static void doRewriteConfig()
static void doResetConfiguration()
bool settingsLtftRequestWriteToFlash()
void readFromFlash()
void writeToFlashNow()
static void doWriteConfigurationToFlash()
@ CUSTOM_ERR_FLASH_CRC_FAILED
void resetMaxValues()
StorageStatus storageRead(StorageItemId id, uint8_t *ptr, size_t size)
Definition storage.cpp:160
bool storageRequestWriteID(StorageItemId id, bool forced)
Definition storage.cpp:186
void initStorage()
Definition storage.cpp:357
StorageStatus storageWrite(StorageItemId id, const uint8_t *ptr, size_t size)
Definition storage.cpp:142
@ EFI_SETTINGS_BACKUP_RECORD_ID
Definition storage.h:50
@ EFI_LTFT_RECORD_ID
Definition storage.h:51
@ EFI_SETTINGS_RECORD_ID
Definition storage.h:49
StorageStatus
Definition storage.h:12
void requestBurn()