-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Add a CRC32 over progmem and ESP.checkFlashCRC #6566
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 13 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
3833040
Add a CRC32 over progmem and ESP.checkFlashCRC
earlephilhower 79d0197
Add example that currupts itself, comments
earlephilhower 7011265
Merge branch 'master' into crc32
earlephilhower b7e9f01
Merge branch 'master' into crc32
earlephilhower 4a50bab
Merge branch 'master' into crc32
earlephilhower 3fca89e
Update Esp.cpp
earlephilhower fcfa48c
Update Esp.cpp
earlephilhower 00929d1
Fix example astyle problems
earlephilhower 49180aa
Check linker script for CRC space in bootsector
earlephilhower 95e64e4
Merge branch 'master' into crc32
earlephilhower 800ebe5
Fix order of crc/len in linker script
earlephilhower 23ecb8e
Merge branch 'master' into crc32
earlephilhower 77d617a
Add note about what to do if CRC check fails
earlephilhower 985b6c5
Only single, flash/ram friendly crc32() function
earlephilhower 379b167
Combine the CRC calc and bin generation in 1 step
earlephilhower 51fec2c
Merge branch 'master' into crc32
earlephilhower 365a9d0
Merge branch 'master' into crc32
earlephilhower File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
libraries/esp8266/examples/CheckFlashCRC/CheckFlashCRC.ino
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
Demonstrate CRC check passing and failing by simulating a bit flip in flash. | ||
WARNING!!! You would never want to actually do this in a real application! | ||
|
||
Released to the Public Domain by Earle F. Philhower, III <[email protected]> | ||
*/ | ||
|
||
extern "C" { | ||
#include "spi_flash.h" | ||
} | ||
// Artificially create a space in PROGMEM that fills multipe sectors so | ||
// we can corrupt one without crashing the system | ||
const int corruptme[SPI_FLASH_SEC_SIZE * 4] PROGMEM = { 0 }; | ||
|
||
void setup() { | ||
Serial.begin(115200); | ||
Serial.printf("Starting\n"); | ||
Serial.printf("CRC check: %s\n", ESP.checkFlashCRC() ? "OK" : "ERROR"); | ||
Serial.printf("...Corrupting a portion of flash in the array...\n"); | ||
|
||
uint32_t ptr = (uint32_t)corruptme; | ||
// Find a page aligned spot inside the array | ||
ptr += 2 * SPI_FLASH_SEC_SIZE; | ||
ptr &= ~(SPI_FLASH_SEC_SIZE - 1); // Sectoralign | ||
uint32_t sector = ((((uint32_t)ptr - 0x40200000) / SPI_FLASH_SEC_SIZE)); | ||
|
||
// Create a sector with 1 bit set (i.e. fake corruption) | ||
uint32_t *space = (uint32_t*)calloc(SPI_FLASH_SEC_SIZE, 1); | ||
space[42] = 64; | ||
|
||
// Write it into flash at the spot in question | ||
spi_flash_erase_sector(sector); | ||
spi_flash_write(sector * SPI_FLASH_SEC_SIZE, (uint32_t*)space, SPI_FLASH_SEC_SIZE); | ||
Serial.printf("CRC check: %s\n", ESP.checkFlashCRC() ? "OK" : "ERROR"); | ||
|
||
Serial.printf("...Correcting the flash...\n"); | ||
memset(space, 0, SPI_FLASH_SEC_SIZE); | ||
spi_flash_erase_sector(sector); | ||
spi_flash_write(sector * SPI_FLASH_SEC_SIZE, (uint32_t*)space, SPI_FLASH_SEC_SIZE); | ||
Serial.printf("CRC check: %s\n", ESP.checkFlashCRC() ? "OK" : "ERROR"); | ||
} | ||
|
||
|
||
void loop() { | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
#!/usr/bin/env python3 | ||
|
||
""" | ||
# Place a CRC32 checksum and length in a generated BIN file | ||
# | ||
# Copyright (C) 2019 - Earle F. Philhower, III | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
""" | ||
|
||
from __future__ import print_function | ||
import argparse | ||
import sys | ||
|
||
def crc8266(ldata): | ||
"Return the CRC of ldata using same algorithm as eboot" | ||
crc = 0xffffffff | ||
idx = 0 | ||
while idx < len(ldata): | ||
byte = int(ldata[idx]) | ||
idx = idx + 1 | ||
for i in [0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01]: | ||
bit = crc & 0x80000000 | ||
if (byte & i) != 0: | ||
if bit == 0: | ||
bit = 1 | ||
else: | ||
bit = 0 | ||
crc = int(crc << 1) & 0xffffffff | ||
if bit != 0: | ||
crc = int(crc ^ 0x04c11db7) | ||
return crc | ||
|
||
def store_word(raw, offset, val): | ||
"Place a 4-byte word in 8266-dependent order in the raw image" | ||
raw[offset] = val & 255 | ||
raw[offset + 1] = (val >> 8) & 255 | ||
raw[offset + 2] = (val >> 16) & 255 | ||
raw[offset + 3] = (val >> 24) & 255 | ||
return raw | ||
|
||
def main(): | ||
"Main CLI interface" | ||
parser = argparse.ArgumentParser(description='Embed CRC32 and length in a generated BIN image') | ||
parser.add_argument('-b', '--bin', action='store', required=True, | ||
help='Path to the Arduino sketch.bin') | ||
parser.add_argument('-s', '--size', action='store', required=True, | ||
help='Byte offset in bin to store the size') | ||
parser.add_argument('-c', '--crc', action='store', required=True, | ||
help='Byte offset in bin to store the crc32') | ||
args = parser.parse_args() | ||
|
||
with open(args.bin, "rb") as binfile: | ||
raw = bytearray(binfile.read()) | ||
|
||
# Zero out the spots we're going to overwrite to be idempotent | ||
raw = store_word(raw, int(args.size), 0) | ||
raw = store_word(raw, int(args.crc), 0) | ||
crc = crc8266(raw) | ||
raw = store_word(raw, int(args.size), len(raw)) | ||
raw = store_word(raw, int(args.crc), int(crc)) | ||
|
||
with open(args.bin, "wb") as binfile: | ||
binfile.write(raw) | ||
|
||
print("Inserted length of " + hex(len(raw)) + " and CRC of " + hex(crc)+ " into " + args.bin) | ||
|
||
if __name__ == '__main__': | ||
sys.exit(main()) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.