Skip to main content
AKUMA.9
ST Employee

How to use hide protection (HDP) on STM32U3 flash memory

Introduction

Hide protection (HDP) is a flash memory security feature that protects a selected flash area containing sensitive code or data. The protected area stays hidden and inaccessible to unauthorized access until the next reset.

Secure firmware can access the protected area while HDP is open. After secure firmware closes access to that area, further instruction fetches, reads, writes, and erase operations are denied until the next reset.

1. Purpose

HDP is used to protect firmware components that require a higher level of security during execution. It helps ensure that critical code and data cannot be accessed or altered by unauthorized software, thereby reducing exposure to sensitive assets at runtime.

Typical use cases

  • Secure boot or startup firmware.
  • Cryptographic functions and related sensitive data.
  • Proprietary algorithms and intellectual property.
  • Trusted software components that must remain protected while the device is running.

HDP is different from RDP and WRP

  • RDP is a broader readout and debug protection mechanism.
  • WRP is a write-protection mechanism that blocks flash modification.
  • HDP controls access to selected secure flash areas within a reset cycle.

HDP does not erase the flash content. It does not permanently destroy the protected code or data. After reset, the HDP access-control state returns to its reset configuration, so secure startup can intentionally use the protected content again before closing it.

1.1 Runtime HDP behavior​​​​​​​

Secure and nonsecure access

HDP works together with the flash security attribution:

  • Secure firmware can use a secure HDP area while it is open.
  • Nonsecure firmware cannot access secure flash.
  • Nonsecure firmware must not depend on HDP content.
  • Secure firmware must stop using an area before closing it, unless the required code has been copied to executable RAM or placed outside the area.

For STM32U3, the HDP access-disable control is open at the device-defined open value. Access is denied when the applicable HDPx_ACCDIS or HDPxEXT_ACCDIS value is changed from that open value. The exact register fields and encoding must come from the target device reference manual and HAL definitions.

Reset behavior

Closing HDP is temporary for the current reset cycle. After a system reset, the access-control fields return to their reset state according to the device configuration.

Therefore, a secure startup sequence can:

  • Access protected code or data.
  • Use it for secure initialization.
  • Close HDP.
  • Continue without accessing the closed area.
  • Repeat the process after the next reset if the protected content is needed again.

Extended HDP

An HDP extension enlarges the protected area beyond the initial HDP range. It has its own configuration and access-control behavior, so it can be closed separately from the initial HDP area.

2. Scope

This article explains what HDP is, how it behaves at runtime, and which memory layout and security settings are required to run the FLASH_HideProtection_TrustZone example on the STM32U385RG device.

The article focuses on:

  • Flash layout and bank usage
  • Secure and nonsecure partitioning
  • GTZC and MPCBB SRAM attribution
  • Option-byte settings required by the example
  • Linker placement and execution staging
  • Runtime verification of HDP and extended HDP behavior

The same general approach applies across the STM32U3 family, but the exact settings depend on the selected device, the application memory map, and the target security architecture.

3. Flash layout

The STM32U3 family includes devices with different flash sizes and page counts. The bank address ranges and the number of pages per bank depends on the device group.

Device group

Total flash

Bank 1 address range

Bank 2 address range

Pages per bank

STM32U345 (STM32U335/345)

256 KiB

0x08000000-0x0801FFFF

0x08020000-0x0803FFFF

32

STM32U366 (STM32U356/366)

512 KiB

0x08000000-0x0803FFFF

0x08040000-0x0807FFFF

64

STM32U385 (STM32U375/385)

1 MiB

0x08000000-0x0807FFFF

0x08080000-0x080FFFFF

128

STM32U3A6 (STM32U396/3A6)

1.5 MiB

0x08000000-0x080BFFFF

0x080C0000-0x0817FFFF

192

STM32U3C5 (STM32U3B5/3C5)

2 MiB

0x08000000-0x080FFFFF

0x08100000-0x081FFFFF

256

 

Table 1: STM32U3 flash organization by device group

For the reference example, the target device is STM32U385RG.

In this article:

  • Secure flash in bank 1 is shown through the secure alias address space, using addresses that start at 0x0C000000.
  • Nonsecure application in bank 2 is shown through the nonsecure flash alias address space, using addresses that start at 0x080C0000.

Area

Address range

Use

Secure flash bank 1

0x0C000000-0x0C07FFFF

Secure image and secure test areas

Nonsecure flash bank 2

0x08080000-0x080FFFFF

Nonsecure application

Secure NSC region

0x0C07E000-0x0C07FFFF

Secure functions callable by nonsecure code

Table 2: Reference TrustZone® flash layout for the STM32U385RG example

4. GTZC and MPCBB SRAM attribution

The example relies on consistent secure and nonsecure attribution across both flash and SRAM resources.

  • Flash watermarks and option bytes define secure and nonsecure flash boundaries.
  • SAU (Memory Region) defines which memory regions the core sees as secure, nonsecure, or nonsecure callable.
  • GTZC (Internal Peripheral) controls security and privilege attributes for protected resources.
  • MPCBB (Memory Block in SRAM) defines Secure and nonsecure attribution for SRAM blocks.

These mechanisms must agree. If the SAU, GTZC, MPCBB, linker files, and option-byte configuration are not aligned, the application can fault during startup, stack access, data access, veneer calls, or secure-to-nonsecure handoff.

4.1 SRAM and MPCBB resources

The STM32U3 reference manual lists the SRAM resources associated with MPCBB for each device group.

Device group

MPCBB instances

SRAM1

SRAM2

SRAM3

SRAM4

STM32U345 (STM32U335/345)

MPCBB1-2

16 KiB / 1

64 KiB / 4

Not listed

Not listed

STM32U366 (STM32U356/366)

MPCBB1-2

128 KiB / 8

64 KiB / 4

Not listed

Not listed

STM32U385 (STM32U375/385)

MPCBB1-2

192 KiB / 12

64 KiB / 4

Not listed

Not listed

STM32U3A6 (STM32U396/3A6)

MPCBB1-4

96 KiB / 6

64 KiB / 4

256 KiB / 16

64 KiB* / 5

STM32U3C5 (STM32U3B5/3C5)

MPCBB1-4

192 KiB / 12

64 KiB / 4

320 KiB / 20

64 KiB* / 5

Table 3: STM32U3 SRAM and MPCBB resources by device group

The STM32U385 reference project configures SRAM1 and SRAM2 only. A product that uses other SRAM resources must update the SAU, GTZC/MPCBB configuration, and linker files together.

Reference SRAM1 configuration

MPCBB_Area_Desc.AttributeConfig.MPCBB_SecConfig_array[0] = 0xFFFFFFFF;
MPCBB_Area_Desc.AttributeConfig.MPCBB_SecConfig_array[1] = 0xFFFFFFFF;
MPCBB_Area_Desc.AttributeConfig.MPCBB_SecConfig_array[2] = 0xFFFFFFFF;
MPCBB_Area_Desc.AttributeConfig.MPCBB_SecConfig_array[3] = 0xFFFFFFFF;
MPCBB_Area_Desc.AttributeConfig.MPCBB_SecConfig_array[4] = 0xFFFFFFFF;
MPCBB_Area_Desc.AttributeConfig.MPCBB_SecConfig_array[5] = 0xFFFFFFFF;
MPCBB_Area_Desc.AttributeConfig.MPCBB_SecConfig_array[6] = 0x00000000;
MPCBB_Area_Desc.AttributeConfig.MPCBB_SecConfig_array[7] = 0x00000000;
MPCBB_Area_Desc.AttributeConfig.MPCBB_SecConfig_array[8] = 0x00000000;
MPCBB_Area_Desc.AttributeConfig.MPCBB_SecConfig_array[9] = 0x00000000;
MPCBB_Area_Desc.AttributeConfig.MPCBB_SecConfig_array[10] = 0x00000000;
MPCBB_Area_Desc.AttributeConfig.MPCBB_SecConfig_array[11] = 0x00000000;
MPCBB_Area_Desc.AttributeConfig.MPCBB_PrivConfig_array[0] = 0xFFFFFFFF;
MPCBB_Area_Desc.AttributeConfig.MPCBB_PrivConfig_array[1] = 0xFFFFFFFF;
MPCBB_Area_Desc.AttributeConfig.MPCBB_PrivConfig_array[2] = 0xFFFFFFFF;
MPCBB_Area_Desc.AttributeConfig.MPCBB_PrivConfig_array[3] = 0xFFFFFFFF;
MPCBB_Area_Desc.AttributeConfig.MPCBB_PrivConfig_array[4] = 0xFFFFFFFF;
MPCBB_Area_Desc.AttributeConfig.MPCBB_PrivConfig_array[5] = 0xFFFFFFFF;
MPCBB_Area_Desc.AttributeConfig.MPCBB_PrivConfig_array[6] = 0xFFFFFFFF;
MPCBB_Area_Desc.AttributeConfig.MPCBB_PrivConfig_array[7] = 0xFFFFFFFF;
MPCBB_Area_Desc.AttributeConfig.MPCBB_PrivConfig_array[8] = 0xFFFFFFFF;
MPCBB_Area_Desc.AttributeConfig.MPCBB_PrivConfig_array[9] = 0xFFFFFFFF;
MPCBB_Area_Desc.AttributeConfig.MPCBB_PrivConfig_array[10] = 0xFFFFFFFF;
MPCBB_Area_Desc.AttributeConfig.MPCBB_PrivConfig_array[11] = 0xFFFFFFFF;
MPCBB_Area_Desc.AttributeConfig.MPCBB_LockConfig_array[0] = 0x00000000;
if (HAL_GTZC_MPCBB_ConfigMem(SRAM1_BASE, &MPCBB_Area_Desc) != HAL_OK)
{
Error_Handler();
}

This creates:

  • Secure SRAM1 alias: 0x30000000-0x30017FFF
  • Nonsecure SRAM1 alias: 0x20018000-0x2002FFFF
  • MPCBB1 superblocks 0-5: Secure
  • MPCBB1 superblocks 6-11: Nonsecure

SRAM2 is split in the same way:

  • Secure SRAM2 alias: 0x30030000-0x30037FFF
  • Nonsecure SRAM2 alias: 0x20038000-0x2003FFFF
  • MPCBB2 superblocks 0-1: Secure
  • MPCBB2 superblocks 2-3: Nonsecure

5. Reference configuration

The configuration described in this article is specific to the FLASH_HideProtection_TrustZone example for the STM32U385RG device.

The important requirement is consistency across:

  • Option bytes
  • Flash security watermarks
  • Initial HDP and extended HDP page ranges
  • SAU partitioning
  • GTZC and MPCBB SRAM attribution
  • Linker placement
  • Secure-to-Non-secure handoff code

5.1 Flash page allocation

The reference example uses bank-1 page allocation to separate the initial HDP area, the extended HDP area, and ordinary secure flash used as a control region.

Pages

Reference use

Purpose in the example

0-9

Secure code in the initial HDP area

Shows that code can reside in the initial protected area before HDP is closed

10-14

HDP test data

Verifies that initial HDP data reads as blocked after HDP closure

15

Extended-HDP test data

Verifies that extended HDP remains readable until the extension is closed

16

Secure code used by the HDP check

Runs before extended HDP is closed and then transfers control to non-HDP code

17-19

Non-HDP test data

Proves that ordinary secure flash remains readable after HDP and extended HDP closure

20 and above

Secure code outside HDP

Holds secure code that must continue running after protected areas are closed

Table 4: Reference Flash page allocation for the HDP example

The nonsecure application does not read secure flash test ranges directly. It receives the verification result through the NSC API after the secure checks are complete.

Note: The application-specific page allocation is only a reference. The application must define its own protected code and data layout.

5.2 Option byte settings

The option bytes must be configured through STM32CubeProgrammer before running the example

The working reference configuration contains these important option-byte settings:

Setting

Value (Hex)

Meaning

RDP

0xAA

RDP level 0 during development

TZEN

0x01

TrustZone® enabled

SECBOOTADD0

0x0C000000

Secure boot/vector address

NSBOOTADD0

0x08080000

Nonsecure boot/vector address

SECWM1_PSTRT

0x00

Bank-1 secure region starts at page 0

SECWM1_PEND

0x7F

Bank 1 is secure through page 127

HDP1_PEND

0x0E

Initial HDP ends at page 14

HDP1EN

0x01

Initial HDP is enabled

SECWM2_PSTRT

0x7F

Reversed bank-2 watermark start

SECWM2_PEND

0x00

Reversed bank-2 watermark end

HDP2EN

0xB4

No bank-2 HDP area

Table 5: Reference option-byte settings for the STM32U385RG HDP example

Figure 1: TrustZone® enable
Figure 2: Boot configuration
Figure 3: Secure Bank1 region
Figure 4: Nonsecure Bank2 region

The reversed bank-2 watermark makes bank 2 nonsecure in this layout.

These values must stay consistent with the rest of the project:

  • The secure linker origin matches SECBOOTADD0.
  • The nonsecure linker origin and SCB_NS->VTOR value match NSBOOTADD0.
  • The bank-1 security watermark keeps the HDP range inside secure flash.
  • HDP1_PEND matches the initial HDP page range used by the secure image.
  • The reversed bank-2 watermark matches the nonsecure flash image location.

Recommended best practices before changing option bytes

  • Save the current settings.
  • Keep the RDP at level 0 during development.
  • Program and read back the option bytes.
  • Confirm the boot addresses match the linker layout.

5.3 Memory organization, partitioning, and linker placement

Reference partition

The reference SAU partition uses the following nonsecure and NSC regions:

SAU region

Address range

Attribute

Region 0

0x08080000-0x080FFFFF

Nonsecure flash

Region 1

0x0BF80000-0x0BFAFFFF

Nonsecure system flash area

Region 2

0x0C07E000-0x0C07FFFF

Secure Non-secure-callable (NSC)

Region 3

0x20018000-0x2002FFFF

Nonsecure SRAM1

Region 6

0x20038000-0x2003FFFF

Nonsecure SRAM2

Table 6: Reference SAU partition for the STM32U385RG HDP example

All memory not assigned as nonsecure remains secure.

Linker placement and execution staging as an example

The key rule is that code must not continue executing from a flash range after that range is hidden by HDP. Any code needed after HDP closure must be placed in a still-readable secure flash region or copied to executable SRAM.

Secure linker file

The nonsecure linker file stm32u385xx_flash_ns.icf defines the following regions

Region

Address range

Content

ROM_region

0x0C000000-0x0C00FFFF

Secure vector table and secure code

ROM_HDP_EXT_region

0x0C010000-0x0C010FFF

.hdpext code

ROM_NON_HDP_region

0x0C014000-0x0C07DFFF

.non_hdp and secure code

ROM_NSC_region

0x0C07E000-0x0C07FFFF

CMSE NSC veneers

RAM_region

0x30000000-0x30017FFF

Secure SRAM1 data, stack, and heap

SRAM2_region

0x30030000-0x30037FFF

Secure SRAM2 runtime flash code

Table 7: Secure linker memory regions for the STM32U385RG HDP example

Nonsecure linker file

The nonsecure linker file stm32u385xx_flash_ns.icf defines the following regions:

Region

Address range

Content

ROM_region

0x08080000-0x080FFFFF

Nonsecure image

RAM_region

0x20018000-0x2002FFFF

Nonsecure data, stack, and heap

SRAM2_region

0x20038000-0x2003FFFF

Nonsecure SRAM2 area

Table 8: Nonsecure linker memory regions for the STM32U385RG HDP example

5.4 Execution placement example

The staged placement is required as instruction fetch is restricted while an HDP area or HDP extension area remains open. Secure code must therefore be executed from the currently allowed fetch region at each stage of the sequence.

Function

Section

Runs From

Why it is placed there

HDP_Activation()

.ram_code

Secure SRAM2

The runtime HDP closure path must not execute from a flash region that is being closed.

HDPExt_Activation()

.ram_code

Secure SRAM2

The extended HDP closure path must also execute from memory that remains accessible.

HDP_Check()

.hdpext

Extended HDP region

It must run after the initial HDP area is closed but before the extended HDP area is closed.

HDPExt_Check()

.non_hdp

Non-HDP secure flash

It must run after both the initial HDP area and the extended HDP area no longer permit access.

NonSecure_Init()

.non_hdp

Non-HDP secure flash

The secure-to-nonsecure handoff must continue from code outside the closed HDP ranges.

Table 9: Function placement used for staged HDP execution

6. Execution flow

The example flow described demonstrates how hide protection (HDP) and extended HDP are used to protect sensitive flash content during runtime, while still allowing the application to continue execution from safe memory regions.

The secure application first prepares the flash regions, programs known test data, and then progressively closes access to the protected areas.

At each stage, execution is intentionally moved to a memory area that remains accessible after the previous protection step.

6.1 Secure initialization and memory preparation

Execution starts in the secure image. The code initializes the HAL, configures the system clock, sets up TrustZone® memory partitioning, and applies the required GTZC and MPCBB configuration.

/* Configure the Extended Hide Protection area ******************************/ ExtHDPStruct.Bank = GetBank(FLASH_USER_START_EXT_HIDE_ADDR); 
ExtHDPStruct.NbPages = GetPage(FLASH_USER_START_NON_HIDE_ADDR) -GetPage(FLASH_USER_START_EXT_HIDE_ADDR); HAL_FLASHEx_ConfigHDPExtension(&ExtHDPStruct);

The code then erases the selected user flash pages and programs them with a known test pattern (0x12345678). This allows the application to verify memory visibility after each protection step.

6.2 Close the initial HDP area from RAM

After the flash content is prepared, the secure application closes access to the initial HDP area. This operation is executed from RAM, not from flash.

#if defined(__ICCARM__)
static void HDP_Activation(void) @ ".ram_code"
#else
static void attribute((section(".ram_code"), noinline)) HDP_Activation(void)
#endif
{
/* Enable the Security Hide Protection */
HAL_FLASHEx_EnableSecHideProtection(FLASH_BANK_1);

/* Check the data in the HDP area */
HDP_Check();
}

HDP_Activation() is placed in RAM so that it can safely execute while enabling flash protection.

6.3 Verify behaviour after initial HDP closure

After the initial HDP area is closed, execution continues in HDP_Check(), which is placed in the extended HDP region.

#if defined(__ICCARM__)
static void HDP_Check(void) @ ".hdpext"
#else
static void __attribute__((section(".hdpext"), noinline)) HDP_Check(void)
#endif

The HDP area is expected to read as RAZ (Read As Zero) while it is closed. Write and page-erase access to that closed HDP area are ignored and secure WRPERR is set.

/************* Check data after Security Hide Protection enabled ************/
/* Check if the programmed data is OK
MemoryProgramStatus = 0: read as '0' because access to secure hide protection area not allowed
MemoryProgramStatus != 0: read values different to '0' ******/
Address = FLASH_USER_START_HIDE_ADDR;
while (Address < FLASH_USER_END_HIDE_ADDR)
{
data32 = *(__IO uint32_t *)Address;

if (data32 != 0U)
{
MemoryProgramStatus++;
}
Address = Address + 4U;
}

This confirms that:

  • The HDP area is hidden
  • The Extended HDP area is still accessible
  • The non-HDP area remains accessible

After this verification, the example proceeds to the next protection stage.

6.4 Close extended HDP area

Once the initial-HDP verification is complete, the secure application closes access to the extended HDP area.

#if defined(__ICCARM__)
static void HDPExt_Activation(void) @ ".ram_code"
#else
static void attribute((section(".ram_code"), noinline)) HDPExt_Activation(void) #endif
{
/* Enable the Extended Security Hide Protection */
HAL_FLASHEx_EnableHDPExtensionProtection(FLASH_BANK_1, FLASH_EXTHDP_ONLY_ACCESS_PROTECTED);

/* Check the data in the HDP area */
HDPExt_Check();
}

6.5 Verify behaviour after extended HDP closure

After the extended HDP area has been closed, execution continues from .non_hdp code in ordinary Secure flash that remains accessible.

#if defined(__ICCARM__)
static void HDPExt_Check(void) @ ".non_hdp"
#else
static void attribute((section(".non_hdp"), noinline)) HDPExt_Check(void)
#endif

This function first verifies that the extended HDP area is now hidden.

Address = FLASH_USER_START_EXT_HIDE_ADDR;
while (Address < FLASH_USER_END_EXT_HIDE_ADDR)
{
data32 = *(__IO uint32_t *)Address;

if (data32 != 0U)
{
MemoryProgramStatus++;
}
Address = Address + 4U;
}

It then checks that the non-HDP secure test pages still contain the programmed test pattern.

Address = FLASH_USER_START_NON_HIDE_ADDR;
while (Address < FLASH_USER_END_NON_HIDE_ADDR)
{
data32 = *(__IO uint32_t *)Address;

if (data32 != DATA_32)
{
MemoryProgramStatus++;
}
Address = Address + 4U;
}

6.6 Transfer control to a nonsecure application

After all checks are completed, the secure application prepares the nonsecure environment and jumps to the nonsecure reset handler.

Nonsecure reads to secure flash are RAZ and generate a flash illegal access event, while nonsecure fetches from secure flash generate a bus error.

#if defined(__ICCARM__)
static void NonSecure_Init(void) @ ".non_hdp"
#else
static void attribute((section(".non_hdp"), noinline)) NonSecure_Init(void)
#endif
{
uint32_t vtor_ns;
funcptr_NS NonSecure_ResetHandler;

/* There shall be no need to configure VTOR_NS as it’s updated from BOOT_NS option bytes. */
vtor_ns = SCB_NS->VTOR

/* Set non-secure main stack (MSP_NS) */
__TZ_set_MSP_NS((*(uint32_t *)vtor_ns));

/* Get non-secure reset handler */
NonSecure_ResetHandler = (funcptr_NS)(*((uint32_t *)((vtor_ns) + 4U)));

/* Start non-secure state software application */
NonSecure_ResetHandler();
}

7. Runtime verification

The HDP checks prove these points:

  • Initial HDP test pages read as RAZ after the initial HDP area is closed.
  • The extended HDP page remains readable until the extended HDP area is closed.
  • The extended HDP page reads as RAZ after the extended HDP area is closed.
  • Non-HDP secure test pages remain readable after both protected areas are closed.
  • Nonsecure code receives only the secure verification status through SECURE_GetMemoryProgramStatus().

Table 10: Expected runtime verification results by protection stage:

Stage

Initial HDP test pages

Extended HDP test page or pages

Non-HDP secure test pages

Before runtime closure

Accessible

Accessible

Accessible

After initial HDP closure

Not accessible

Accessible

Accessible

After extended HDP closure

Not accessible

Not accessible

Accessible

 

HDP closure is validated here by observing the expected blocked-read behavior during secure-side checks.

A runtime fault usually indicates a configuration or placement problem such as invalid nonsecure access to secure memory, incorrect attribution, wrong vector-table setup, or executing from a hidden region.

8. Common mistakes to avoid

 

8.1 Boot and vector-table setup

  • A wrong secure boot address can prevent the secure image from starting.
  • A wrong nonsecure boot address or SCB_NS->VTOR value can break the secure-to-nonsecure handoff.

8.2 Memory attribution consistency

  • If linker, SAU, GTZC/MPCBB, and option-byte ranges do not match, the CPU can fault during stack, data, code, or veneer access.
  • If the nonsecure stack is placed in secure SRAM, the nonsecure application can fault immediately after handoff.
  • The MPCBB superblock count is device-specific. Using the wrong count can give secure or nonsecure software the wrong SRAM attribution.
  • Changing a region from secure to nonsecure does not erase its existing data. Erase sensitive data explicitly when required.

8.3 HDP page-range definition

  • If HDP1_PEND covers code that is still needed after the initial runtime closure, execution can fail immediately after that closure.
  • The initial HDP and extended HDP ranges must match both the intended test pages and the actual linker placement.

8.4 Execution placement

  • If .ram_code, .hdpext, .non_hdp, or the NSC veneers are linked to the wrong regions, the staged closure sequence will fail.
  • Always inspect the final linker map to confirm that each section is placed in the intended region.

8.5 Security semantics

  • HDP is not permanent, and it is not RDP or WRP. Reset restores the HDP access-control state, but flash content remains unchanged.
  • When an HDP area is open, instruction fetch must remain within that HDP area until it is closed. When an HDP extension area is open and no HDP area is open, instruction fetch must remain within the HDP extension area until it is closed.
  • Secure firmware must own HDP configuration. Nonsecure code should use only the defined NSC services.

Conclusion

HDP is a per-reset runtime protection mechanism for secure flash content on STM32U3 devices. Secure firmware uses protected code or data while HDP is open, closes the area when it is no longer needed, and continues execution from code that remains accessible.

A correct implementation depends on a complete configuration chain. Option bytes, flash watermarks, HDP and extended HDP ranges, TrustZone® partitioning, linker placement, GTZC/MPCBB SRAM attribution, vector-table setup, NSC exposure, and runtime control flow must all agree.  

The STM32U385 IAR reference project is a useful reference implementation that shows how these dependencies fit together.