2019-08-16 02:43 AM
Hi folks,
I am trying to set the RDP option bytes to have my code in flash read-protected. Instead of using ST-Link Utility, I want to set the read-out protection in the code itself (specifically, inside a self-designed bootloader). For this purpose, I use the flash functions provided by the FLASH HAL library. The problem is that I am facing a Hard Fault Exception, right after the option start bit is set (inside the function HAL_FLASH_OB_Launch). According to the reference manual, the sequence for setting the option bytes is the following:
Still, at I mentioned before, at the step 4, an exception is triggered (MPU or Execute never XN default memory map access violation has occurred on an instruction fetch). It is important to say though that after a reset, the RDP is activated (by checking with ST-Link-Utility).
Probably I am missing something. Has anyone successfully set the RDP in code? Or has faced the same problem as me?
Many Thanks
Jorge
FLASH_OBProgramInitTypeDef obConfig;
HAL_FLASHEx_OBGetConfig(&obConfig);
if (obConfig.RDPLevel == OB_RDP_LEVEL_0) {
// this is first time we run mcu after flashing firmware
obConfig.RDPLevel = OB_RDP_LEVEL_1;
HAL_FLASH_Unlock();
HAL_FLASH_OB_Unlock();
HAL_FLASHEx_OBProgram(&obConfig);
HAL_FLASH_OB_Launch();
HAL_FLASH_OB_Lock();
HAL_FLASH_Lock();
}
2019-08-16 03:57 AM
You probably have to init obConfig with sensible values...
2019-08-16 05:01 AM
Thanks for your reply. What do you mean with sensible values?
2019-08-16 05:32 AM
obConfig.RDPLevel = OB_RDP_LEVEL_1;
seems like a sensible default.
Did you check your code against FLASH_If_WriteProtectionConfig() e.g. in
Projects/STM32446E_EVAL/Applications/IAP/IAP_Main/Src/flash_if.c
?
2019-08-16 06:37 AM
Well, the only difference I see is at this function the old OB config is read first (besides, it is only about changing the write protection, not read or BOR). I set then the OptionType to OPTIONBYTE_RDP but the result is sadly the same ( Hard Fault exception).
Actually, I have already tried to modify the write protection and it worked perfectly. The problem comes when I want to increase the read out protection from level 0 to level 1. With ST-Link works it can be changed without any issue though.
HAL_StatusTypeDef FLASH_If_WriteProtectionConfig(uint32_t modifier)
{
uint32_t ProtectedSECTOR = 0xFFF;
FLASH_OBProgramInitTypeDef config_new, config_old;
HAL_StatusTypeDef result = HAL_OK;
/* Get pages write protection status ****************************************/
HAL_FLASHEx_OBGetConfig(&config_old);
/* The parameter says whether we turn the protection on or off */
config_new.WRPState = modifier;
/* We want to modify only the Write protection */
config_new.OptionType = OPTIONBYTE_WRP;
/* No read protection, keep BOR and reset settings */
config_new.RDPLevel = OB_RDP_LEVEL_0;
config_new.USERConfig = config_old.USERConfig;
/* Get pages already write protected ****************************************/
ProtectedSECTOR = config_old.WRPSector | FLASH_SECTOR_TO_BE_PROTECTED;
/* Unlock the Flash to enable the flash control register access *************/
HAL_FLASH_Unlock();
/* Unlock the Options Bytes *************************************************/
HAL_FLASH_OB_Unlock();
config_new.WRPSector = ProtectedSECTOR;
result = HAL_FLASHEx_OBProgram(&config_new);
return result;
}