Skip to content

Commit c95bc8a

Browse files
committed
Generate hash statically
1 parent 5f9214f commit c95bc8a

File tree

4 files changed

+35
-50
lines changed

4 files changed

+35
-50
lines changed

.gitignore

-3
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,3 @@ __pycache__
168168

169169
# IOLogger logs
170170
*_log.csv
171-
172-
# Build_Count
173-
Marlin/Build_Count.h

Marlin/Configuration.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -1836,7 +1836,7 @@
18361836
#define EEPROM_BOOT_SILENT // Keep M503 quiet and only give errors during first load
18371837
#if ENABLED(EEPROM_SETTINGS)
18381838
//#define EEPROM_AUTO_INIT // Init EEPROM automatically on any errors.
1839-
#define EEPROM_CHECK_BUILD_COUNT // Use and check build count, Defaults loaded on mismatach.
1839+
//#define EEPROM_INIT_NOW // Init EEPROM on first boot.
18401840
#endif
18411841

18421842
//

Marlin/src/module/settings.cpp

+34-33
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,6 @@
159159
#include "../lcd/extui/dgus/DGUSDisplayDef.h"
160160
#endif
161161

162-
#ifdef EEPROM_CHECK_BUILD_COUNT
163-
#include "../../Build_Count.h"
164-
#endif
165-
166162
#pragma pack(push, 1) // No padding between variables
167163

168164
#if HAS_ETHERNET
@@ -195,8 +191,8 @@ static const feedRate_t _DMF[] PROGMEM = DEFAULT_MAX_FEEDRATE;
195191
*/
196192
typedef struct SettingsDataStruct {
197193
char version[4]; // Vnn\0
198-
#if ENABLED(EEPROM_CHECK_BUILD_COUNT)
199-
uint8_t build_count;
194+
#if ENABLED(EEPROM_INIT_NOW)
195+
uint32_t build_hash;
200196
#endif
201197
uint16_t crc; // Data Checksum
202198

@@ -638,8 +634,12 @@ void MarlinSettings::postprocess() {
638634
#endif
639635

640636
const char version[4] = EEPROM_VERSION;
641-
#if ENABLED(EEPROM_CHECK_BUILD_COUNT)
642-
const uint8_t build_count = BUILD_COUNT;
637+
638+
#if ENABLED(EEPROM_INIT_NOW)
639+
constexpr uint32_t strhash32(const char *s, char c='\0', uint32_t h=0) {
640+
return *s ? strhash32(s + 1, *s, (((h << 1) | (h >> 31)) ^ c)) : h;
641+
}
642+
constexpr uint32_t build_hash = strhash32(__DATE__ __TIME__);
643643
#endif
644644

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

649649
bool MarlinSettings::size_error(const uint16_t size) {
650650
if (size != datasize()) {
651-
DEBUG_ERROR_MSG("EEPROM datasize error. Size ",size," datasize ",datasize());
651+
DEBUG_ERROR_MSG("EEPROM datasize error."
652+
#if ENABLED(MARLIN_DEV_MODE)
653+
" (Actual:", size, " Expected:", datasize(), ")"
654+
#endif
655+
);
652656
return true;
653657
}
654658
return false;
@@ -660,20 +664,19 @@ void MarlinSettings::postprocess() {
660664
bool MarlinSettings::save() {
661665
float dummyf = 0;
662666
char ver[4] = "ERR";
663-
#if ENABLED(EEPROM_CHECK_BUILD_COUNT)
664-
uint8_t build_count;
665-
#endif
667+
666668
if (!EEPROM_START(EEPROM_OFFSET)) return false;
667669

668670
eeprom_error = false;
669671

670672
// Write or Skip version. (Flash doesn't allow rewrite without erase.)
671673
TERN(FLASH_EEPROM_EMULATION, EEPROM_SKIP, EEPROM_WRITE)(ver);
672674

673-
#if ENABLED(EEPROM_CHECK_BUILD_COUNT)
674-
EEPROM_SKIP(build_count); // Skip the build_count slot
675+
#if ENABLED(EEPROM_INIT_NOW)
676+
EEPROM_SKIP(build_hash); // Skip the hash slot
675677
#endif
676-
EEPROM_SKIP(working_crc); // Skip the checksum slot
678+
679+
EEPROM_SKIP(working_crc); // Skip the checksum slot
677680

678681
working_crc = 0; // clear before first "real data"
679682

@@ -1476,8 +1479,8 @@ void MarlinSettings::postprocess() {
14761479
eeprom_index = EEPROM_OFFSET;
14771480

14781481
EEPROM_WRITE(version);
1479-
#if ENABLED(EEPROM_CHECK_BUILD_COUNT)
1480-
EEPROM_WRITE(BUILD_COUNT);
1482+
#if ENABLED(EEPROM_INIT_NOW)
1483+
EEPROM_WRITE(build_hash);
14811484
#endif
14821485
EEPROM_WRITE(final_crc);
14831486

@@ -1511,33 +1514,31 @@ void MarlinSettings::postprocess() {
15111514

15121515
char stored_ver[4];
15131516
EEPROM_READ_ALWAYS(stored_ver);
1514-
#if ENABLED(EEPROM_CHECK_BUILD_COUNT)
1515-
uint8_t stored_build_count;
1516-
EEPROM_READ_ALWAYS(stored_build_count);
1517-
#endif
1518-
1519-
1520-
uint16_t stored_crc;
1521-
EEPROM_READ_ALWAYS(stored_crc);
15221517

15231518
// Version has to match or defaults are used
1524-
if ((strncmp(version, stored_ver, 3) != 0) || TERN0(EEPROM_CHECK_BUILD_COUNT,(stored_build_count != BUILD_COUNT))) {
1519+
if (strncmp(version, stored_ver, 3) != 0) {
15251520
if (stored_ver[3] != '\0') {
15261521
stored_ver[0] = '?';
15271522
stored_ver[1] = '\0';
15281523
}
1529-
1530-
#if ENABLED(EEPROM_CHECK_BUILD_COUNT)
1531-
DEBUG_ECHO_MSG("EEPROM version mismatch (EEPROM=", stored_ver, ".",stored_build_count , " Marlin=" EEPROM_VERSION ".", BUILD_COUNT,")");
1532-
#else
1533-
DEBUG_ECHO_MSG("EEPROM version mismatch (EEPROM=", stored_ver, " Marlin=" EEPROM_VERSION ")");
1534-
#endif
1524+
DEBUG_ECHO_MSG("EEPROM version mismatch (EEPROM=", stored_ver, " Marlin=" EEPROM_VERSION, ")");
15351525
TERN_(DWIN_CREALITY_LCD_ENHANCED, LCD_MESSAGE(MSG_ERR_EEPROM_VERSION));
15361526

15371527
IF_DISABLED(EEPROM_AUTO_INIT, ui.eeprom_alert_version());
15381528
eeprom_error = true;
15391529
}
15401530
else {
1531+
1532+
// Optionally reset on the first boot after flashing
1533+
#if ENABLED(EEPROM_INIT_NOW)
1534+
uint32_t stored_hash;
1535+
EEPROM_READ_ALWAYS(stored_hash);
1536+
if (stored_hash != build_hash) { EEPROM_FINISH(); return true; }
1537+
#endif
1538+
1539+
uint16_t stored_crc;
1540+
EEPROM_READ_ALWAYS(stored_crc);
1541+
15411542
float dummyf = 0;
15421543
working_crc = 0; // Init to 0. Accumulated by EEPROM_READ
15431544

@@ -2480,7 +2481,7 @@ void MarlinSettings::postprocess() {
24802481
return success;
24812482
}
24822483
reset();
2483-
#if ENABLED(EEPROM_AUTO_INIT)
2484+
#if EITHER(EEPROM_AUTO_INIT, EEPROM_INIT_NOW)
24842485
(void)save();
24852486
SERIAL_ECHO_MSG("EEPROM Initialized");
24862487
#endif

buildroot/share/PlatformIO/scripts/preflight-checks.py

-13
Original file line numberDiff line numberDiff line change
@@ -85,19 +85,6 @@ def sanity_check_target():
8585
if os.path.exists(warnfile):
8686
os.remove(warnfile)
8787

88-
#
89-
# Maintain build count
90-
#
91-
if 'EEPROM_CHECK_BUILD_COUNT' in env['MARLIN_FEATURES']:
92-
build_count = 0
93-
buildcountfile = os.path.join("Marlin", "Build_Count.h")
94-
if os.path.exists(buildcountfile):
95-
con_adv_file = open(buildcountfile, "r")
96-
build_count = (int(con_adv_file.read()[20:].strip())+1)%256
97-
con_adv_file = open(buildcountfile, "w")
98-
con_adv_file.write("#define BUILD_COUNT " + str(build_count))
99-
con_adv_file.close
100-
10188
#
10289
# Check for old files indicating an entangled Marlin (mixing old and new code)
10390
#

0 commit comments

Comments
 (0)