Skip to content

Commit 6405d35

Browse files
tarek-bochkatitom-van
authored andcommitted
flash/stm32l4x: probe tzen and rdp values
introduction of 'enum stm32l4_rdp' enumerating possible RDP levels for devices with and without TrustZone. also in 'stm32l4_flash_bank' structure we added and rdp and tzen members to store read values by the helper 'stm32l4_sync_rdp_tzen' these new members are used to display security and protection status while probing the flash. Change-Id: Icf883189715278a3323fe210d295047678b16592 Signed-off-by: Tarek BOCHKATI <[email protected]> Reviewed-on: http://openocd.zylin.com/5541 Tested-by: jenkins Reviewed-by: Tomas Vanek <[email protected]>
1 parent 88fa831 commit 6405d35

File tree

2 files changed

+60
-11
lines changed

2 files changed

+60
-11
lines changed

src/flash/nor/stm32l4x.c

+57-8
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,13 @@ enum stm32l4_flash_reg_index {
143143
STM32_FLASH_REG_INDEX_NUM,
144144
};
145145

146+
enum stm32l4_rdp {
147+
RDP_LEVEL_0 = 0xAA,
148+
RDP_LEVEL_0_5 = 0x55, /* for devices with TrustZone enabled */
149+
RDP_LEVEL_1 = 0x00,
150+
RDP_LEVEL_2 = 0xCC
151+
};
152+
146153
static const uint32_t stm32l4_flash_regs[STM32_FLASH_REG_INDEX_NUM] = {
147154
[STM32_FLASH_ACR_INDEX] = 0x000,
148155
[STM32_FLASH_KEYR_INDEX] = 0x008,
@@ -199,6 +206,8 @@ struct stm32l4_flash_bank {
199206
const struct stm32l4_part_info *part_info;
200207
const uint32_t *flash_regs;
201208
bool otp_enabled;
209+
enum stm32l4_rdp rdp;
210+
bool tzen;
202211
};
203212

204213
enum stm32_bank_id {
@@ -610,6 +619,35 @@ static inline bool stm32l4_otp_is_enabled(struct flash_bank *bank)
610619
return stm32l4_info->otp_enabled;
611620
}
612621

622+
static void stm32l4_sync_rdp_tzen(struct flash_bank *bank, uint32_t optr_value)
623+
{
624+
struct stm32l4_flash_bank *stm32l4_info = bank->driver_priv;
625+
626+
bool tzen = false;
627+
628+
if (stm32l4_info->part_info->flags & F_HAS_TZ)
629+
tzen = (optr_value & FLASH_TZEN) != 0;
630+
631+
uint32_t rdp = optr_value & FLASH_RDP_MASK;
632+
633+
/* for devices without TrustZone:
634+
* RDP level 0 and 2 values are to 0xAA and 0xCC
635+
* Any other value corresponds to RDP level 1
636+
* for devices with TrusZone:
637+
* RDP level 0 and 2 values are 0xAA and 0xCC
638+
* RDP level 0.5 value is 0x55 only if TZEN = 1
639+
* Any other value corresponds to RDP level 1, including 0x55 if TZEN = 0
640+
*/
641+
642+
if (rdp != RDP_LEVEL_0 && rdp != RDP_LEVEL_2) {
643+
if (!tzen || (tzen && rdp != RDP_LEVEL_0_5))
644+
rdp = RDP_LEVEL_1;
645+
}
646+
647+
stm32l4_info->tzen = tzen;
648+
stm32l4_info->rdp = rdp;
649+
}
650+
613651
static inline uint32_t stm32l4_get_flash_reg(struct flash_bank *bank, uint32_t reg_offset)
614652
{
615653
struct stm32l4_flash_bank *stm32l4_info = bank->driver_priv;
@@ -1332,6 +1370,22 @@ static int stm32l4_probe(struct flash_bank *bank)
13321370

13331371
LOG_INFO("device idcode = 0x%08" PRIx32 " (%s)", stm32l4_info->idcode, device_info);
13341372

1373+
/* read flash option register */
1374+
retval = stm32l4_read_flash_reg_by_index(bank, STM32_FLASH_OPTR_INDEX, &options);
1375+
if (retval != ERROR_OK)
1376+
return retval;
1377+
1378+
stm32l4_sync_rdp_tzen(bank, options);
1379+
1380+
if (part_info->flags & F_HAS_TZ)
1381+
LOG_INFO("TZEN = %d : TrustZone %s by option bytes",
1382+
stm32l4_info->tzen,
1383+
stm32l4_info->tzen ? "enabled" : "disabled");
1384+
1385+
LOG_INFO("RDP level %s (0x%02X)",
1386+
stm32l4_info->rdp == RDP_LEVEL_0 ? "0" : stm32l4_info->rdp == RDP_LEVEL_0_5 ? "0.5" : "1",
1387+
stm32l4_info->rdp);
1388+
13351389
if (stm32l4_is_otp(bank)) {
13361390
bank->size = part_info->otp_size;
13371391

@@ -1347,7 +1401,6 @@ static int stm32l4_probe(struct flash_bank *bank)
13471401
return ERROR_FAIL;
13481402
}
13491403

1350-
13511404
stm32l4_info->probed = true;
13521405
return ERROR_OK;
13531406
} else if (bank->base != STM32_FLASH_BANK_BASE) {
@@ -1379,11 +1432,6 @@ static int stm32l4_probe(struct flash_bank *bank)
13791432
/* did we assign a flash size? */
13801433
assert((flash_size_kb != 0xffff) && flash_size_kb);
13811434

1382-
/* read flash option register */
1383-
retval = stm32l4_read_flash_reg_by_index(bank, STM32_FLASH_OPTR_INDEX, &options);
1384-
if (retval != ERROR_OK)
1385-
return retval;
1386-
13871435
stm32l4_info->bank1_sectors = 0;
13881436
stm32l4_info->hole_sectors = 0;
13891437

@@ -1783,7 +1831,8 @@ COMMAND_HANDLER(stm32l4_handle_lock_command)
17831831

17841832
/* set readout protection level 1 by erasing the RDP option byte */
17851833
struct stm32l4_flash_bank *stm32l4_info = bank->driver_priv;
1786-
if (stm32l4_write_option(bank, stm32l4_info->flash_regs[STM32_FLASH_OPTR_INDEX], 0, 0x000000FF) != ERROR_OK) {
1834+
if (stm32l4_write_option(bank, stm32l4_info->flash_regs[STM32_FLASH_OPTR_INDEX],
1835+
RDP_LEVEL_1, FLASH_RDP_MASK) != ERROR_OK) {
17871836
command_print(CMD, "%s failed to lock device", bank->driver->name);
17881837
return ERROR_OK;
17891838
}
@@ -1817,7 +1866,7 @@ COMMAND_HANDLER(stm32l4_handle_unlock_command)
18171866

18181867
struct stm32l4_flash_bank *stm32l4_info = bank->driver_priv;
18191868
if (stm32l4_write_option(bank, stm32l4_info->flash_regs[STM32_FLASH_OPTR_INDEX],
1820-
RDP_LEVEL_0, 0x000000FF) != ERROR_OK) {
1869+
RDP_LEVEL_0, FLASH_RDP_MASK) != ERROR_OK) {
18211870
command_print(CMD, "%s failed to unlock device", bank->driver->name);
18221871
return ERROR_OK;
18231872
}

src/flash/nor/stm32l4x.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@
5656
#define OPTKEY1 0x08192A3B
5757
#define OPTKEY2 0x4C5D6E7F
5858

59-
#define RDP_LEVEL_0 0xAA
60-
#define RDP_LEVEL_1 0xBB
61-
#define RDP_LEVEL_2 0xCC
59+
/* FLASH_OPTR register bits */
60+
#define FLASH_RDP_MASK 0xFF
61+
#define FLASH_TZEN (1 << 31)
6262

6363
/* other registers */
6464
#define DBGMCU_IDCODE_G0 0x40015800

0 commit comments

Comments
 (0)