Skip to content

Commit

Permalink
Generate hash statically
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkyhead committed Dec 21, 2021
1 parent 5f9214f commit 4b1cce8
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 48 deletions.
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,3 @@ __pycache__

# IOLogger logs
*_log.csv

# Build_Count
Marlin/Build_Count.h
2 changes: 1 addition & 1 deletion Marlin/Configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -1836,7 +1836,7 @@
#define EEPROM_BOOT_SILENT // Keep M503 quiet and only give errors during first load
#if ENABLED(EEPROM_SETTINGS)
//#define EEPROM_AUTO_INIT // Init EEPROM automatically on any errors.
#define EEPROM_CHECK_BUILD_COUNT // Use and check build count, Defaults loaded on mismatach.
//#define EEPROM_INIT_NOW // Init EEPROM on first boot.
#endif

//
Expand Down
64 changes: 33 additions & 31 deletions Marlin/src/module/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,6 @@
#include "../lcd/extui/dgus/DGUSDisplayDef.h"
#endif

#ifdef EEPROM_CHECK_BUILD_COUNT
#include "../../Build_Count.h"
#endif

#pragma pack(push, 1) // No padding between variables

#if HAS_ETHERNET
Expand Down Expand Up @@ -195,8 +191,8 @@ static const feedRate_t _DMF[] PROGMEM = DEFAULT_MAX_FEEDRATE;
*/
typedef struct SettingsDataStruct {
char version[4]; // Vnn\0
#if ENABLED(EEPROM_CHECK_BUILD_COUNT)
uint8_t build_count;
#if ENABLED(EEPROM_INIT_NOW)
uint32_t build_hash;
#endif
uint16_t crc; // Data Checksum

Expand Down Expand Up @@ -638,8 +634,12 @@ void MarlinSettings::postprocess() {
#endif

const char version[4] = EEPROM_VERSION;
#if ENABLED(EEPROM_CHECK_BUILD_COUNT)
const uint8_t build_count = BUILD_COUNT;

#if ENABLED(EEPROM_INIT_NOW)
constexpr uint32_t strhash32(const char *s, char c='\0', uint32_t h=0) {
return *s ? strhash32(s + 1, *s, (((h << 1) | (h >> 31)) ^ c)) : h;
}
constexpr uint32_t build_hash = strhash32(__DATE__ __TIME__);
#endif

bool MarlinSettings::eeprom_error, MarlinSettings::validating;
Expand All @@ -648,7 +648,11 @@ void MarlinSettings::postprocess() {

bool MarlinSettings::size_error(const uint16_t size) {
if (size != datasize()) {
DEBUG_ERROR_MSG("EEPROM datasize error. Size ",size," datasize ",datasize());
DEBUG_ERROR_MSG("EEPROM datasize error."
#if ENABLED(MARLIN_DEV_MODE)
" (Actual:", size, " Expected:", datasize(), ")"
#endif
);
return true;
}
return false;
Expand All @@ -660,8 +664,8 @@ void MarlinSettings::postprocess() {
bool MarlinSettings::save() {
float dummyf = 0;
char ver[4] = "ERR";
#if ENABLED(EEPROM_CHECK_BUILD_COUNT)
uint8_t build_count;
#if ENABLED(EEPROM_INIT_NOW)
uint32_t build_hash;
#endif
if (!EEPROM_START(EEPROM_OFFSET)) return false;

Expand All @@ -670,8 +674,8 @@ void MarlinSettings::postprocess() {
// Write or Skip version. (Flash doesn't allow rewrite without erase.)
TERN(FLASH_EEPROM_EMULATION, EEPROM_SKIP, EEPROM_WRITE)(ver);

#if ENABLED(EEPROM_CHECK_BUILD_COUNT)
EEPROM_SKIP(build_count); // Skip the build_count slot
#if ENABLED(EEPROM_INIT_NOW)
EEPROM_SKIP(build_hash); // Skip the hash slot
#endif
EEPROM_SKIP(working_crc); // Skip the checksum slot

Expand Down Expand Up @@ -1476,8 +1480,8 @@ void MarlinSettings::postprocess() {
eeprom_index = EEPROM_OFFSET;

EEPROM_WRITE(version);
#if ENABLED(EEPROM_CHECK_BUILD_COUNT)
EEPROM_WRITE(BUILD_COUNT);
#if ENABLED(EEPROM_INIT_NOW)
EEPROM_WRITE(build_hash);
#endif
EEPROM_WRITE(final_crc);

Expand Down Expand Up @@ -1511,33 +1515,31 @@ void MarlinSettings::postprocess() {

char stored_ver[4];
EEPROM_READ_ALWAYS(stored_ver);
#if ENABLED(EEPROM_CHECK_BUILD_COUNT)
uint8_t stored_build_count;
EEPROM_READ_ALWAYS(stored_build_count);
#endif


uint16_t stored_crc;
EEPROM_READ_ALWAYS(stored_crc);

// Version has to match or defaults are used
if ((strncmp(version, stored_ver, 3) != 0) || TERN0(EEPROM_CHECK_BUILD_COUNT,(stored_build_count != BUILD_COUNT))) {
if (strncmp(version, stored_ver, 3) != 0) {
if (stored_ver[3] != '\0') {
stored_ver[0] = '?';
stored_ver[1] = '\0';
}

#if ENABLED(EEPROM_CHECK_BUILD_COUNT)
DEBUG_ECHO_MSG("EEPROM version mismatch (EEPROM=", stored_ver, ".",stored_build_count , " Marlin=" EEPROM_VERSION ".", BUILD_COUNT,")");
#else
DEBUG_ECHO_MSG("EEPROM version mismatch (EEPROM=", stored_ver, " Marlin=" EEPROM_VERSION ")");
#endif
DEBUG_ECHO_MSG("EEPROM version mismatch (EEPROM=", stored_ver, " Marlin=" EEPROM_VERSION, ")");
TERN_(DWIN_CREALITY_LCD_ENHANCED, LCD_MESSAGE(MSG_ERR_EEPROM_VERSION));

IF_DISABLED(EEPROM_AUTO_INIT, ui.eeprom_alert_version());
eeprom_error = true;
}
else {

// Optionally reset on the first boot after flashing
#if ENABLED(EEPROM_INIT_NOW)
uint32_t stored_hash;
EEPROM_READ_ALWAYS(stored_hash);
if (stored_hash != build_hash) { EEPROM_FINISH(); return true; }
#endif

uint16_t stored_crc;
EEPROM_READ_ALWAYS(stored_crc);

float dummyf = 0;
working_crc = 0; // Init to 0. Accumulated by EEPROM_READ

Expand Down Expand Up @@ -2480,7 +2482,7 @@ void MarlinSettings::postprocess() {
return success;
}
reset();
#if ENABLED(EEPROM_AUTO_INIT)
#if EITHER(EEPROM_AUTO_INIT, EEPROM_INIT_NOW)
(void)save();
SERIAL_ECHO_MSG("EEPROM Initialized");
#endif
Expand Down
13 changes: 0 additions & 13 deletions buildroot/share/PlatformIO/scripts/preflight-checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,19 +85,6 @@ def sanity_check_target():
if os.path.exists(warnfile):
os.remove(warnfile)

#
# Maintain build count
#
if 'EEPROM_CHECK_BUILD_COUNT' in env['MARLIN_FEATURES']:
build_count = 0
buildcountfile = os.path.join("Marlin", "Build_Count.h")
if os.path.exists(buildcountfile):
con_adv_file = open(buildcountfile, "r")
build_count = (int(con_adv_file.read()[20:].strip())+1)%256
con_adv_file = open(buildcountfile, "w")
con_adv_file.write("#define BUILD_COUNT " + str(build_count))
con_adv_file.close

#
# Check for old files indicating an entangled Marlin (mixing old and new code)
#
Expand Down

0 comments on commit 4b1cce8

Please sign in to comment.