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 return ((result1 == StorageStatus::Ok) && (result2 == StorageStatus::Ok));
69}
70
72 auto flashCrc = persistentState.getCrc();
73
74 if (flashCrc != persistentState.crc) {
75 // If the stored crc is all 1s, that probably means the flash is actually blank, not that the crc failed.
76 if (persistentState.crc == ((decltype(persistentState.crc))-1)) {
78 } else {
80 }
81 } else if (persistentState.version != FLASH_DATA_VERSION || persistentState.size != sizeof(persistentState)) {
83 } else {
84 return StorageStatus::Ok;
85 }
86}
87
88/**
89 * this method could and should be executed before we have any
90 * connectivity so no console output here
91 *
92 * in this method we read first copy of configuration in flash. if that first copy has CRC or other issues we read second copy.
93 */
95 auto firstCopy = storageRead(EFI_SETTINGS_RECORD_ID, (uint8_t *)&persistentState, sizeof(persistentState));
96 if (firstCopy == StorageStatus::Ok) {
97 firstCopy = validatePersistentState();
98 if (firstCopy == StorageStatus::Ok) {
99 return StorageStatus::Ok;
100 }
101 }
102
103 auto secondCopy = storageRead(EFI_SETTINGS_BACKUP_RECORD_ID, (uint8_t *)&persistentState, sizeof(persistentState));
104 if (secondCopy == StorageStatus::Ok) {
105 secondCopy = validatePersistentState();
106 if (secondCopy == StorageStatus::Ok) {
107 return StorageStatus::Ok;
108 }
109 }
110
111 return firstCopy;
112}
113
116
117 switch (result) {
120 efiPrintf("Need to reset flash to default due to CRC mismatch");
121 [[fallthrough]];
125 resetConfigurationExt(DEFAULT_ENGINE_TYPE);
126 break;
128 // Preserve engine type from old config
129 efiPrintf("Resetting due to version mismatch but preserving engine type [%d]", (int)engineConfiguration->engineType);
131 break;
133 // At this point we know that CRC and version number is what we expect. Safe to assume it's a valid configuration.
135 efiPrintf("Read valid configuration from flash!");
136 break;
137 }
138
139 // we can only change the state after the CRC check
142}
143
145 // force settings write to storage
147}
148
152
153static void doRewriteConfig() {
155 // force settings write to storage
157}
158
162
163void initFlash() {
164 // Init storage(s) if any
165 // Note: may take some time
166 initStorage();
167
168 addConsoleAction("readconfig", readFromFlash);
169 /**
170 * This would write NOW (you should not be doing this while connected to real engine)
171 */
173
174 addConsoleAction("ltftwrite", doWriteLTFT);
175#if EFI_TUNER_STUDIO
176 /**
177 * This would schedule write to flash once the engine is stopped
178 */
179 addConsoleAction(CMD_BURNCONFIG, requestBurn);
180#endif
182 addConsoleAction("rewriteconfig", doRewriteConfig);
183}
184
185#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()