@@ -1586,16 +1586,21 @@ uint8_t *espFindUnusedFlashStart(void)
15861586
15871587uint8_t modSPIRead (uint32_t offset , uint32_t size , uint8_t * dst )
15881588{
1589- uint8_t temp [4 ] __attribute__ ((aligned (4 )));
1589+ uint8_t temp [512 ] __attribute__ ((aligned (4 )));
15901590 uint32_t toAlign ;
15911591
15921592 if (offset & 3 ) { // long align offset
15931593 toAlign = 4 - (offset & 3 );
15941594 if (toAlign > size )
15951595 toAlign = size ;
1596- if (SPI_FLASH_RESULT_OK != spi_flash_read (offset , (uint32_t * )temp , sizeof ( temp ) ))
1596+ if (SPI_FLASH_RESULT_OK != spi_flash_read (offset & ~ 3 , (uint32_t * )temp , 4 ))
15971597 return 0 ;
1598- c_memcpy (dst , temp + 4 - toAlign , toAlign );
1598+ if (size < toAlign ) {
1599+ c_memcpy (dst , temp + 4 - toAlign , size );
1600+ toAlign = size ;
1601+ }
1602+ else
1603+ c_memcpy (dst , temp + 4 - toAlign , toAlign );
15991604 dst += toAlign ;
16001605 offset += toAlign ;
16011606 size -= toAlign ;
@@ -1604,15 +1609,18 @@ uint8_t modSPIRead(uint32_t offset, uint32_t size, uint8_t *dst)
16041609 }
16051610
16061611 toAlign = size & ~3 ;
1607- if (SPI_FLASH_RESULT_OK != spi_flash_read (offset , (uint32_t * )dst , toAlign ))
1608- return 0 ;
1612+ if (toAlign ) {
1613+ if (SPI_FLASH_RESULT_OK != spi_flash_read (offset , (uint32_t * )temp , toAlign ))
1614+ return 0 ;
1615+ c_memmove (dst , temp , toAlign );
16091616
1610- dst += toAlign ;
1611- offset += toAlign ;
1612- size -= toAlign ;
1617+ dst += toAlign ;
1618+ offset += toAlign ;
1619+ size -= toAlign ;
1620+ }
16131621
16141622 if (size ) { // long align tail
1615- if (SPI_FLASH_RESULT_OK != spi_flash_read (offset , (uint32_t * )temp , sizeof ( temp ) ))
1623+ if (SPI_FLASH_RESULT_OK != spi_flash_read (offset , (uint32_t * )temp , 4 ))
16161624 return 0 ;
16171625 c_memcpy (dst , temp , size );
16181626 }
0 commit comments