diff --git a/src/main/io/flashfs.c b/src/main/io/flashfs.c index 429ffe8ad..bfc6007dd 100644 --- a/src/main/io/flashfs.c +++ b/src/main/io/flashfs.c @@ -481,7 +481,7 @@ int flashfsIdentifyStartOfFreeSpace() /* We can choose whatever power of 2 size we like, which determines how much wastage of free space we'll have * at the end of the last written data. But smaller blocksizes will require more searching. */ - FREE_BLOCK_SIZE = 65536, + FREE_BLOCK_SIZE = 2048, /* We don't expect valid data to ever contain this many consecutive uint32_t's of all 1 bits: */ FREE_BLOCK_TEST_SIZE_INTS = 4, // i.e. 16 bytes @@ -493,16 +493,20 @@ int flashfsIdentifyStartOfFreeSpace() uint32_t ints[FREE_BLOCK_TEST_SIZE_INTS]; } testBuffer; - int left = 0; - int right = flashfsGetSize() / FREE_BLOCK_SIZE; - int mid, result = right; + int left = 0; // Smallest block index in the search region + int right = flashfsGetSize() / FREE_BLOCK_SIZE; // One past the largest block index in the search region + int mid; + int result = right; int i; bool blockErased; while (left < right) { mid = (left + right) / 2; - m25p16_readBytes(mid * FREE_BLOCK_SIZE, testBuffer.bytes, FREE_BLOCK_TEST_SIZE_BYTES); + if (m25p16_readBytes(mid * FREE_BLOCK_SIZE, testBuffer.bytes, FREE_BLOCK_TEST_SIZE_BYTES) < FREE_BLOCK_TEST_SIZE_BYTES) { + // Unexpected timeout from flash, so bail early (reporting the device fuller than it really is) + break; + } // Checking the buffer 4 bytes at a time like this is probably faster than byte-by-byte, but I didn't benchmark it :) blockErased = true;