@@ -37,11 +37,19 @@ const CALIBRATION_PAGE_SIZE = FLASH_PAGE_SIZE;
3737// Temporary maintained state pointing to the next available chunk.
3838// TODO: Remove once nextAvailableChunk() is updated.
3939// Chosen by fair dice roll, guaranteed to be random.
40- const FS_START_CHUNK = 0x2b ;
40+ const FS_START_CHUNK = 0x01 ;
4141let FS_NEXT_AVAILABLE_CHUNK = FS_START_CHUNK ;
4242
4343function fsIncreaseChunkIndex ( numberOfChunks : number ) : void {
4444 FS_NEXT_AVAILABLE_CHUNK += numberOfChunks ;
45+ const unusedMap = new MemoryMap ( ) ;
46+ // Check if we are over the filesystem area
47+ if (
48+ chuckIndexAddress ( unusedMap , FS_NEXT_AVAILABLE_CHUNK ) >=
49+ getEndAddress ( unusedMap )
50+ ) {
51+ throw new Error ( 'There is no more space in the file system.' ) ;
52+ }
4553}
4654
4755function resetFileSystem ( ) : void {
@@ -64,6 +72,7 @@ export function testResetFileSystem(): void {
6472 * @returns Next available filesystem chunk.
6573 */
6674function nextAvailableChunk ( intelHexMap : object ) : number {
75+ // TODO: Check if we have run out of memory.
6776 return FS_NEXT_AVAILABLE_CHUNK ;
6877}
6978
@@ -128,10 +137,7 @@ function getPersistentPageAddress(intelHexMap: object): number {
128137 * @param chunkIndex - Index for the chunk to calculate.
129138 * @returns Address in flash for the chunk.
130139 */
131- function addressFromChunkIndexNew (
132- intelHexMap : object ,
133- chunkIndex : number
134- ) : number {
140+ function chuckIndexAddress ( intelHexMap : object , chunkIndex : number ) : number {
135141 // Chunk index starts at 1, so we need to account for that in the calculation
136142 return getStartAddress ( intelHexMap ) + ( chunkIndex - 1 ) * ChunkSizes . All ;
137143}
@@ -245,7 +251,7 @@ function addFileToIntelHex(
245251
246252 // Find next available chunk and its flash address
247253 const chunkIndex = nextAvailableChunk ( intelHexMap ) ;
248- const startAddress = addressFromChunkIndexNew ( intelHexMap , chunkIndex ) ;
254+ const startAddress = chuckIndexAddress ( intelHexMap , chunkIndex ) ;
249255 // Store in an array each file converted to file system chunks
250256 const fsFile = new FsFile ( filename , data ) ;
251257 const fileFsBytes = fsFile . getFsBytes ( chunkIndex ) ;
0 commit comments