@@ -237,6 +237,15 @@ hal_status_t FRAM_Write(uint8_t spiBank, uint32_t addr, uint8_t *buf,
237237 hal_status_t status = hal_status_ok ;
238238 uint8_t spiData [4 ];
239239
240+ /* Validate input parameters */
241+ if (buf == NULL || len == 0 ) {
242+ return hal_status_badParam ;
243+ }
244+ /* Bounds check: ensure write doesn't exceed FRAM size */
245+ if (addr >= FRAM_SIZE || (addr + len ) > FRAM_SIZE ) {
246+ return hal_status_badParam ;
247+ }
248+
240249#ifdef DEBUG_EXT_FLASH
241250 wolfBoot_printf ("fram write: addr 0x%x, dst 0x%x, len %d\n" ,
242251 addr , buf , len );
@@ -259,6 +268,15 @@ hal_status_t FRAM_Read(uint8_t spiBank, uint32_t addr, uint8_t *buf,
259268{
260269 uint8_t spiData [4 ];
261270
271+ /* Validate input parameters */
272+ if (buf == NULL || len == 0 ) {
273+ return hal_status_badParam ;
274+ }
275+ /* Bounds check: ensure read doesn't exceed FRAM size */
276+ if (addr >= FRAM_SIZE || (addr + len ) > FRAM_SIZE ) {
277+ return hal_status_badParam ;
278+ }
279+
262280#ifdef DEBUG_EXT_FLASH
263281 wolfBoot_printf ("fram read: addr 0x%x, dst 0x%x, len %d\n" ,
264282 addr , buf , len );
@@ -290,7 +308,7 @@ hal_status_t FRAM_Erase(uint8_t spiBank, uint32_t addr, uint32_t len)
290308 memset (data , FRAM_ERASE_VALUE , sizeof (data ));
291309
292310 while (len > 0 ) {
293- int erase_len = (len > ( int ) sizeof (data )) ? ( int ) sizeof (data ) : len ;
311+ uint32_t erase_len = (len > sizeof (data )) ? sizeof (data ) : len ;
294312 status = FRAM_Write (ROM_SPI_BANK , addr , data , erase_len );
295313 if (status != hal_status_ok ) {
296314 return - (int )status ; /* convert to negative error code */
@@ -333,14 +351,14 @@ int RAMFUNCTION hal_flash_erase(uint32_t address, int len)
333351#ifdef EXT_FLASH
334352void ext_flash_lock (void )
335353{
336- /* Enable writes to code memory space */
337- VOR_SYSCONFIG -> ROM_PROT |= SYSCONFIG_ROM_PROT_WREN_Msk ;
354+ /* Disable writes to code memory space */
355+ VOR_SYSCONFIG -> ROM_PROT &= ~ SYSCONFIG_ROM_PROT_WREN_Msk ;
338356}
339357
340358void ext_flash_unlock (void )
341359{
342- /* Disable writes to code memory space */
343- VOR_SYSCONFIG -> ROM_PROT &= ~ SYSCONFIG_ROM_PROT_WREN_Msk ;
360+ /* Enable writes to code memory space */
361+ VOR_SYSCONFIG -> ROM_PROT |= SYSCONFIG_ROM_PROT_WREN_Msk ;
344362}
345363
346364int ext_flash_write (uintptr_t address , const uint8_t * data , int len )
@@ -480,12 +498,12 @@ void hal_init(void)
480498 }
481499
482500 /* Disable Watchdog - should be already disabled out of reset */
483- VOR_WATCH_DOG -> WDOGLOCK = 0x1ACCE551 ;
501+ VOR_WATCH_DOG -> WDOGLOCK = WATCHDOG_UNLOCK_KEY ;
484502 VOR_WATCH_DOG -> WDOGCONTROL = 0x0 ;
485503 NVIC_ClearPendingIRQ (WATCHDOG_IRQn );
486504
487505 /* set FPU CP10 and CP11 Full Access */
488- SCB -> CPACR |= (( 0x3 << 20 )|( 0x3 << 22 ) );
506+ SCB -> CPACR |= (CPACR_CP10_FULL_ACCESS | CPACR_CP11_FULL_ACCESS );
489507
490508 /* Init EDAC */
491509 ConfigEdac (WOLFBOOT_EDAC_RAM_SCRUB , WOLFBOOT_EDAC_ROM_SCRUB );
0 commit comments