File tree Expand file tree Collapse file tree 1 file changed +23
-3
lines changed Expand file tree Collapse file tree 1 file changed +23
-3
lines changed Original file line number Diff line number Diff line change @@ -474,12 +474,32 @@ bool spi_flash_read_block(uint8_t *dest, uint32_t block) {
474
474
return true;
475
475
} else {
476
476
// Non-MBR block, get data from flash memory.
477
- uint32_t src = convert_block_to_flash_addr (block );
478
- if (src == -1 ) {
477
+ uint32_t address = convert_block_to_flash_addr (block );
478
+ if (address == -1 ) {
479
479
// bad block number
480
480
return false;
481
481
}
482
- return read_flash (src , dest , FLASH_BLOCK_SIZE );
482
+
483
+ // Mask out the lower bits that designate the address within the sector.
484
+ uint32_t this_sector = address & (~(sector_size - 1 ));
485
+ uint8_t block_index = (address / FLASH_BLOCK_SIZE ) % (sector_size / FLASH_BLOCK_SIZE );
486
+ uint8_t mask = 1 << (block_index );
487
+ // We're reading from the currently cached sector.
488
+ if (current_sector == this_sector && (mask & dirty_mask ) > 0 ) {
489
+ if (ram_cache != NULL ) {
490
+ uint8_t pages_per_block = FLASH_BLOCK_SIZE / page_size ;
491
+ for (int i = 0 ; i < pages_per_block ; i ++ ) {
492
+ memcpy (dest + i * page_size ,
493
+ ram_cache [block_index * pages_per_block + i ],
494
+ page_size );
495
+ }
496
+ return true;
497
+ } else {
498
+ uint32_t scratch_address = SCRATCH_SECTOR + block_index * FLASH_BLOCK_SIZE ;
499
+ return read_flash (scratch_address , dest , FLASH_BLOCK_SIZE );
500
+ }
501
+ }
502
+ return read_flash (address , dest , FLASH_BLOCK_SIZE );
483
503
}
484
504
}
485
505
You can’t perform that action at this time.
0 commit comments