@@ -143,6 +143,13 @@ enum stm32l4_flash_reg_index {
143
143
STM32_FLASH_REG_INDEX_NUM ,
144
144
};
145
145
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
+
146
153
static const uint32_t stm32l4_flash_regs [STM32_FLASH_REG_INDEX_NUM ] = {
147
154
[STM32_FLASH_ACR_INDEX ] = 0x000 ,
148
155
[STM32_FLASH_KEYR_INDEX ] = 0x008 ,
@@ -199,6 +206,8 @@ struct stm32l4_flash_bank {
199
206
const struct stm32l4_part_info * part_info ;
200
207
const uint32_t * flash_regs ;
201
208
bool otp_enabled ;
209
+ enum stm32l4_rdp rdp ;
210
+ bool tzen ;
202
211
};
203
212
204
213
enum stm32_bank_id {
@@ -610,6 +619,35 @@ static inline bool stm32l4_otp_is_enabled(struct flash_bank *bank)
610
619
return stm32l4_info -> otp_enabled ;
611
620
}
612
621
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
+
613
651
static inline uint32_t stm32l4_get_flash_reg (struct flash_bank * bank , uint32_t reg_offset )
614
652
{
615
653
struct stm32l4_flash_bank * stm32l4_info = bank -> driver_priv ;
@@ -1332,6 +1370,22 @@ static int stm32l4_probe(struct flash_bank *bank)
1332
1370
1333
1371
LOG_INFO ("device idcode = 0x%08" PRIx32 " (%s)" , stm32l4_info -> idcode , device_info );
1334
1372
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
+
1335
1389
if (stm32l4_is_otp (bank )) {
1336
1390
bank -> size = part_info -> otp_size ;
1337
1391
@@ -1347,7 +1401,6 @@ static int stm32l4_probe(struct flash_bank *bank)
1347
1401
return ERROR_FAIL ;
1348
1402
}
1349
1403
1350
-
1351
1404
stm32l4_info -> probed = true;
1352
1405
return ERROR_OK ;
1353
1406
} else if (bank -> base != STM32_FLASH_BANK_BASE ) {
@@ -1379,11 +1432,6 @@ static int stm32l4_probe(struct flash_bank *bank)
1379
1432
/* did we assign a flash size? */
1380
1433
assert ((flash_size_kb != 0xffff ) && flash_size_kb );
1381
1434
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
-
1387
1435
stm32l4_info -> bank1_sectors = 0 ;
1388
1436
stm32l4_info -> hole_sectors = 0 ;
1389
1437
@@ -1783,7 +1831,8 @@ COMMAND_HANDLER(stm32l4_handle_lock_command)
1783
1831
1784
1832
/* set readout protection level 1 by erasing the RDP option byte */
1785
1833
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 ) {
1787
1836
command_print (CMD , "%s failed to lock device" , bank -> driver -> name );
1788
1837
return ERROR_OK ;
1789
1838
}
@@ -1817,7 +1866,7 @@ COMMAND_HANDLER(stm32l4_handle_unlock_command)
1817
1866
1818
1867
struct stm32l4_flash_bank * stm32l4_info = bank -> driver_priv ;
1819
1868
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 ) {
1821
1870
command_print (CMD , "%s failed to unlock device" , bank -> driver -> name );
1822
1871
return ERROR_OK ;
1823
1872
}
0 commit comments