From 0e4cc5c3cf858a2572cb611aa7a6d336a01f4a6f Mon Sep 17 00:00:00 2001 From: Dominic Clifton Date: Sun, 2 Dec 2018 03:14:11 +0100 Subject: [PATCH] Add flash_scan to cli --- src/main/cli/cli.c | 13 +++++++++++++ src/main/io/flashfs.c | 44 +++++++++++++++++++++++++++++++++++++++++++ src/main/io/flashfs.h | 3 +++ 3 files changed, 60 insertions(+) diff --git a/src/main/cli/cli.c b/src/main/cli/cli.c index f0ea25952..b45dc0597 100644 --- a/src/main/cli/cli.c +++ b/src/main/cli/cli.c @@ -2260,6 +2260,18 @@ static void cliFlashErase(char *cmdline) #ifdef USE_FLASH_TOOLS +static void cliFlashVerify(char *cmdline) +{ + UNUSED(cmdline); + + cliPrintLine("Verifying"); + if (flashfsVerifyEntireFlash()) { + cliPrintLine("Success"); + } else { + cliPrintLine("Failed"); + } +} + static void cliFlashWrite(char *cmdline) { const uint32_t address = atoi(cmdline); @@ -5779,6 +5791,7 @@ const clicmd_t cmdTable[] = { CLI_COMMAND_DEF("flash_info", "show flash chip info", NULL, cliFlashInfo), #ifdef USE_FLASH_TOOLS CLI_COMMAND_DEF("flash_read", NULL, "
", cliFlashRead), + CLI_COMMAND_DEF("flash_scan", "scan flash device for errors", NULL, cliFlashVerify), CLI_COMMAND_DEF("flash_write", NULL, "
", cliFlashWrite), #endif #endif diff --git a/src/main/io/flashfs.c b/src/main/io/flashfs.c index dbc2abe6b..15ae0e51b 100644 --- a/src/main/io/flashfs.c +++ b/src/main/io/flashfs.c @@ -38,6 +38,7 @@ #include "platform.h" +#include "common/printf.h" #include "drivers/flash.h" #include "io/flashfs.h" @@ -601,3 +602,46 @@ void flashfsInit(void) flashfsSeekAbs(flashfsIdentifyStartOfFreeSpace()); } } + +#ifdef USE_FLASH_TOOLS +bool flashfsVerifyEntireFlash(void) +{ + flashEraseCompletely(); + flashfsInit(); + + const flashGeometry_t *flashGeometry = flashfsGetGeometry(); + + uint32_t address = 0; + flashfsSeekAbs(address); + + const int bufferSize = 32; + char buffer[bufferSize + 1]; + + const uint32_t testLimit = flashGeometry->totalSize; + + for (address = 0; address < testLimit; address += bufferSize) { + tfp_sprintf(buffer, "%08x >> **0123456789ABCDEF**", address); + flashfsWrite((uint8_t*)buffer, strlen(buffer), true); + } + flashfsFlushSync(); + flashfsClose(); + + char expectedBuffer[bufferSize + 1]; + + flashfsSeekAbs(0); + + int verificationFailures = 0; + for (address = 0; address < testLimit; address += bufferSize) { + tfp_sprintf(expectedBuffer, "%08x >> **0123456789ABCDEF**", address); + + memset(buffer, 0, sizeof(buffer)); + int bytesRead = flashfsReadAbs(address, (uint8_t *)buffer, bufferSize); + + int result = strncmp(buffer, expectedBuffer, bufferSize); + if (result != 0 || bytesRead != bufferSize) { + verificationFailures++; + } + } + return verificationFailures == 0; +} +#endif // USE_FLASH_TOOLS diff --git a/src/main/io/flashfs.h b/src/main/io/flashfs.h index d9f7dfcd0..4a4d1fc4f 100644 --- a/src/main/io/flashfs.h +++ b/src/main/io/flashfs.h @@ -54,3 +54,6 @@ bool flashfsIsSupported(void); bool flashfsIsReady(void); bool flashfsIsEOF(void); + +bool flashfsVerifyEntireFlash(void); +