2015-07-10 06:01:56 -07:00
|
|
|
/**
|
|
|
|
* @file flash_main.cpp
|
|
|
|
* @brief Higher-level logic of saving data into internal flash memory
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @date Sep 19, 2013
|
2020-01-13 18:57:43 -08:00
|
|
|
* @author Andrey Belomutskiy, (c) 2012-2020
|
2015-07-10 06:01:56 -07:00
|
|
|
*/
|
|
|
|
|
2018-09-16 19:39:46 -07:00
|
|
|
#include "global.h"
|
2019-04-12 19:07:03 -07:00
|
|
|
#if EFI_INTERNAL_FLASH
|
2019-07-05 17:03:32 -07:00
|
|
|
#include "os_access.h"
|
2015-07-10 06:01:56 -07:00
|
|
|
#include "flash_main.h"
|
|
|
|
#include "eficonsole.h"
|
2019-07-04 00:57:21 -07:00
|
|
|
|
2020-04-25 13:32:32 -07:00
|
|
|
#include "flash_int.h"
|
2018-02-03 07:55:15 -08:00
|
|
|
#include "engine_math.h"
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2019-04-12 19:07:03 -07:00
|
|
|
#if EFI_TUNER_STUDIO
|
2015-07-10 06:01:56 -07:00
|
|
|
#include "tunerstudio.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#include "engine_controller.h"
|
|
|
|
|
|
|
|
static bool needToWriteConfiguration = false;
|
|
|
|
|
|
|
|
EXTERN_ENGINE;
|
|
|
|
static Logging* logger;
|
|
|
|
|
|
|
|
extern persistent_config_container_s persistentState;
|
|
|
|
|
|
|
|
extern engine_configuration_s *engineConfiguration;
|
|
|
|
|
2017-02-13 22:03:01 -08:00
|
|
|
/**
|
|
|
|
* https://sourceforge.net/p/rusefi/tickets/335/
|
|
|
|
*
|
2019-04-24 21:10:39 -07:00
|
|
|
* In order to preserve at least one copy of the tune in case of electrical issues address of second configuration copy
|
|
|
|
* should be in a different sector of flash since complete flash sectors are erased on write.
|
2017-02-13 22:03:01 -08:00
|
|
|
*/
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
crc_t flashStateCrc(persistent_config_container_s *state) {
|
|
|
|
return calc_crc((const crc_t*) &state->persistentConfiguration, sizeof(persistent_config_s));
|
|
|
|
}
|
|
|
|
|
|
|
|
void setNeedToWriteConfiguration(void) {
|
|
|
|
scheduleMsg(logger, "Scheduling configuration write");
|
|
|
|
needToWriteConfiguration = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool getNeedToWriteConfiguration(void) {
|
|
|
|
return needToWriteConfiguration;
|
|
|
|
}
|
|
|
|
|
|
|
|
void writeToFlashIfPending() {
|
|
|
|
if (!getNeedToWriteConfiguration()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// todo: technically we need a lock here, realistically we should be fine.
|
|
|
|
needToWriteConfiguration = false;
|
|
|
|
scheduleMsg(logger, "Writing pending configuration");
|
|
|
|
writeToFlashNow();
|
|
|
|
}
|
|
|
|
|
2020-03-07 07:23:30 -08:00
|
|
|
// Erase and write a copy of the configuration at the specified address
|
|
|
|
template <typename TStorage>
|
|
|
|
int eraseAndFlashCopy(flashaddr_t storageAddress, const TStorage& data)
|
|
|
|
{
|
2020-04-25 13:32:32 -07:00
|
|
|
intFlashErase(storageAddress, sizeof(TStorage));
|
|
|
|
return intFlashWrite(storageAddress, reinterpret_cast<const char*>(&data), sizeof(TStorage));
|
2020-03-07 07:23:30 -08:00
|
|
|
}
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
void writeToFlashNow(void) {
|
|
|
|
scheduleMsg(logger, " !!!!!!!!!!!!!!!!!!!! BE SURE NOT WRITE WITH IGNITION ON !!!!!!!!!!!!!!!!!!!!");
|
2020-03-07 07:23:30 -08:00
|
|
|
|
|
|
|
// Set up the container
|
|
|
|
persistentState.size = sizeof(persistentState);
|
2015-07-10 06:01:56 -07:00
|
|
|
persistentState.version = FLASH_DATA_VERSION;
|
2020-03-07 07:23:30 -08:00
|
|
|
persistentState.value = flashStateCrc(&persistentState);
|
|
|
|
|
|
|
|
// Flash two copies
|
|
|
|
int result1 = eraseAndFlashCopy(getFlashAddrFirstCopy(), persistentState);
|
|
|
|
int result2 = eraseAndFlashCopy(getFlashAddrSecondCopy(), persistentState);
|
|
|
|
|
|
|
|
// handle success/failure
|
|
|
|
bool isSuccess = (result1 == FLASH_RETURN_SUCCESS) && (result2 == FLASH_RETURN_SUCCESS);
|
|
|
|
|
2015-07-13 17:02:18 -07:00
|
|
|
if (isSuccess) {
|
2020-05-08 21:47:11 -07:00
|
|
|
scheduleMsg(logger, "FLASH_SUCCESS");
|
2015-07-13 17:02:18 -07:00
|
|
|
} else {
|
|
|
|
scheduleMsg(logger, "Flashing failed");
|
|
|
|
}
|
2018-02-03 07:55:15 -08:00
|
|
|
assertEngineReference();
|
2020-02-26 15:16:35 -08:00
|
|
|
|
|
|
|
#if EFI_SHAFT_POSITION_INPUT
|
2017-05-19 18:52:10 -07:00
|
|
|
resetMaxValues();
|
2020-02-26 15:16:35 -08:00
|
|
|
#endif
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool isValidCrc(persistent_config_container_s *state) {
|
|
|
|
crc_t result = flashStateCrc(state);
|
|
|
|
int isValidCrc_b = result == state->value;
|
|
|
|
return isValidCrc_b;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void doResetConfiguration(void) {
|
2017-05-15 20:28:49 -07:00
|
|
|
resetConfigurationExt(logger, engineConfiguration->engineType PASS_ENGINE_PARAMETER_SUFFIX);
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
2015-12-21 20:01:27 -08:00
|
|
|
persisted_configuration_state_e flashState;
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2017-02-13 22:03:01 -08:00
|
|
|
static persisted_configuration_state_e doReadConfiguration(flashaddr_t address, Logging * logger) {
|
|
|
|
printMsg(logger, "readFromFlash %x", address);
|
2020-04-25 13:32:32 -07:00
|
|
|
intFlashRead(address, (char *) &persistentState, sizeof(persistentState));
|
2017-02-13 22:03:01 -08:00
|
|
|
|
|
|
|
if (!isValidCrc(&persistentState)) {
|
|
|
|
return CRC_FAILED;
|
2020-03-07 07:23:30 -08:00
|
|
|
} else if (persistentState.version != FLASH_DATA_VERSION || persistentState.size != sizeof(persistentState)) {
|
2017-02-13 22:03:01 -08:00
|
|
|
return INCOMPATIBLE_VERSION;
|
|
|
|
} else {
|
|
|
|
return PC_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-03 11:02:00 -07:00
|
|
|
/**
|
|
|
|
* this method could and should be executed before we have any
|
|
|
|
* connectivity so no console output here
|
|
|
|
*/
|
2016-04-03 15:02:39 -07:00
|
|
|
persisted_configuration_state_e readConfiguration(Logging * logger) {
|
2019-12-13 15:02:24 -08:00
|
|
|
efiAssert(CUSTOM_ERR_ASSERT, getCurrentRemainingStack() > EXPECTED_REMAINING_STACK, "read f", PC_ERROR);
|
2020-02-04 18:36:38 -08:00
|
|
|
persisted_configuration_state_e result = doReadConfiguration(getFlashAddrFirstCopy(), logger);
|
2017-02-13 22:03:01 -08:00
|
|
|
if (result != PC_OK) {
|
|
|
|
printMsg(logger, "Reading second configuration copy");
|
2020-02-04 18:36:38 -08:00
|
|
|
result = doReadConfiguration(getFlashAddrSecondCopy(), logger);
|
2017-02-13 22:03:01 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (result == CRC_FAILED) {
|
2020-05-23 13:24:26 -07:00
|
|
|
// we are here on first boot on brand new chip
|
|
|
|
warning(CUSTOM_ERR_FLASH_CRC_FAILED, "flash CRC failed");
|
2017-05-15 20:28:49 -07:00
|
|
|
resetConfigurationExt(logger, DEFAULT_ENGINE_TYPE PASS_ENGINE_PARAMETER_SUFFIX);
|
2017-02-13 22:03:01 -08:00
|
|
|
} else if (result == INCOMPATIBLE_VERSION) {
|
2017-05-15 20:28:49 -07:00
|
|
|
resetConfigurationExt(logger, engineConfiguration->engineType PASS_ENGINE_PARAMETER_SUFFIX);
|
2015-07-10 06:01:56 -07:00
|
|
|
} else {
|
2015-12-21 20:01:27 -08:00
|
|
|
/**
|
|
|
|
* At this point we know that CRC and version number is what we expect. Safe to assume it's a valid configuration.
|
|
|
|
*/
|
2017-05-15 20:28:49 -07:00
|
|
|
applyNonPersistentConfiguration(logger PASS_ENGINE_PARAMETER_SUFFIX);
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
// we can only change the state after the CRC check
|
2016-12-25 08:02:42 -08:00
|
|
|
engineConfiguration->byFirmwareVersion = getRusEfiVersion();
|
2020-04-19 18:42:00 -07:00
|
|
|
memset(persistentState.persistentConfiguration.warning_message , 0, ERROR_BUFFER_SIZE);
|
2017-08-31 04:53:41 -07:00
|
|
|
validateConfiguration(PASS_ENGINE_PARAMETER_SIGNATURE);
|
2016-04-03 11:02:00 -07:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void readFromFlash(void) {
|
2016-04-03 15:02:39 -07:00
|
|
|
persisted_configuration_state_e result = readConfiguration(logger);
|
2015-12-21 20:01:27 -08:00
|
|
|
|
|
|
|
if (result == CRC_FAILED) {
|
|
|
|
printMsg(logger, "Need to reset flash to default due to CRC");
|
|
|
|
} else if (result == INCOMPATIBLE_VERSION) {
|
|
|
|
printMsg(logger, "Resetting but saving engine type [%d]", engineConfiguration->engineType);
|
|
|
|
} else {
|
|
|
|
printMsg(logger, "Got valid configuration from flash!");
|
|
|
|
}
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static void rewriteConfig(void) {
|
|
|
|
doResetConfiguration();
|
|
|
|
writeToFlashNow();
|
|
|
|
}
|
|
|
|
|
2019-04-08 08:57:16 -07:00
|
|
|
static void writeConfigCommand() {
|
|
|
|
#if EFI_TUNER_STUDIO
|
|
|
|
// on start-up rusEfi would read from working copy of TS while
|
|
|
|
// we have a lot of console commands which write into real copy of configuration directly
|
|
|
|
// we have a bit of a mess here
|
|
|
|
syncTunerStudioCopy();
|
|
|
|
#endif /* EFI_TUNER_STUDIO */
|
|
|
|
writeToFlashNow();
|
|
|
|
}
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
void initFlash(Logging *sharedLogger) {
|
|
|
|
logger = sharedLogger;
|
|
|
|
|
|
|
|
addConsoleAction("readconfig", readFromFlash);
|
|
|
|
/**
|
|
|
|
* This would write NOW (you should not be doing this while connected to real engine)
|
|
|
|
*/
|
2019-07-14 12:21:38 -07:00
|
|
|
addConsoleAction(CMD_WRITECONFIG, writeConfigCommand);
|
2019-04-12 19:07:03 -07:00
|
|
|
#if EFI_TUNER_STUDIO
|
2015-07-10 06:01:56 -07:00
|
|
|
/**
|
|
|
|
* This would schedule write to flash once the engine is stopped
|
|
|
|
*/
|
|
|
|
addConsoleAction("burnconfig", requestBurn);
|
|
|
|
#endif
|
|
|
|
addConsoleAction("resetconfig", doResetConfiguration);
|
|
|
|
addConsoleAction("rewriteconfig", rewriteConfig);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* EFI_INTERNAL_FLASH */
|