-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Implement readout protection for STM32H7 #76640
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ff0f1e5
drivers: flash: stm32 driver move sem functions for multithread
FRASTM d4e9ca2
drivers: flash: stm32 flash ex op functions are common
FRASTM 10af3ff
drivers: flash: stm32h7 with RDP protection
FRASTM e655141
tests: drivers: flash RDP testing on stm32h7
FRASTM File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -62,32 +62,77 @@ struct flash_stm32_sector_t { | |
| volatile uint32_t *sr; | ||
| }; | ||
|
|
||
| #if defined(CONFIG_MULTITHREADING) || defined(CONFIG_STM32H7_DUAL_CORE) | ||
| /* | ||
| * This is named flash_stm32_sem_take instead of flash_stm32_lock (and | ||
| * similarly for flash_stm32_sem_give) to avoid confusion with locking | ||
| * actual flash sectors. | ||
| */ | ||
| static inline void _flash_stm32_sem_take(const struct device *dev) | ||
| static __unused int write_optb(const struct device *dev, uint32_t mask, | ||
FRASTM marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| uint32_t value) | ||
| { | ||
| k_sem_take(&FLASH_STM32_PRIV(dev)->sem, K_FOREVER); | ||
| z_stm32_hsem_lock(CFG_HW_FLASH_SEMID, HSEM_LOCK_WAIT_FOREVER); | ||
| FLASH_TypeDef *regs = FLASH_STM32_REGS(dev); | ||
| int rc; | ||
|
|
||
| if (regs->OPTCR & FLASH_OPTCR_OPTLOCK) { | ||
| LOG_ERR("Option bytes locked"); | ||
| return -EIO; | ||
| } | ||
|
|
||
| if ((regs->OPTCR & mask) == value) { | ||
| /* Done already */ | ||
| return 0; | ||
| } | ||
|
|
||
| rc = flash_stm32_wait_flash_idle(dev); | ||
| if (rc < 0) { | ||
| LOG_ERR("Err flash no idle"); | ||
| return rc; | ||
| } | ||
|
|
||
| regs->OPTCR = (regs->OPTCR & ~mask) | value; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @FRASTM ping :-) |
||
| regs->OPTCR |= FLASH_OPTCR_OPTSTART; | ||
|
|
||
| /* Make sure previous write is completed. */ | ||
| barrier_dsync_fence_full(); | ||
|
|
||
| rc = flash_stm32_wait_flash_idle(dev); | ||
| if (rc < 0) { | ||
| LOG_ERR("Err flash no idle"); | ||
| return rc; | ||
| } | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
| static inline void _flash_stm32_sem_give(const struct device *dev) | ||
| #if defined(CONFIG_FLASH_STM32_READOUT_PROTECTION) | ||
| uint8_t flash_stm32_get_rdp_level(const struct device *dev) | ||
| { | ||
| z_stm32_hsem_unlock(CFG_HW_FLASH_SEMID); | ||
| k_sem_give(&FLASH_STM32_PRIV(dev)->sem); | ||
| FLASH_TypeDef *regs = FLASH_STM32_REGS(dev); | ||
|
|
||
| return (regs->OPTSR_CUR & FLASH_OPTSR_RDP_Msk) >> FLASH_OPTSR_RDP_Pos; | ||
| } | ||
|
|
||
| #define flash_stm32_sem_init(dev) k_sem_init(&FLASH_STM32_PRIV(dev)->sem, 1, 1) | ||
| #define flash_stm32_sem_take(dev) _flash_stm32_sem_take(dev) | ||
| #define flash_stm32_sem_give(dev) _flash_stm32_sem_give(dev) | ||
| #else | ||
| #define flash_stm32_sem_init(dev) | ||
| #define flash_stm32_sem_take(dev) | ||
| #define flash_stm32_sem_give(dev) | ||
| #endif | ||
| void flash_stm32_set_rdp_level(const struct device *dev, uint8_t level) | ||
| { | ||
| write_optb(dev, FLASH_OPTSR_RDP_Msk, | ||
| (uint32_t)level << FLASH_OPTSR_RDP_Pos); | ||
| } | ||
| #endif /* CONFIG_FLASH_STM32_READOUT_PROTECTION */ | ||
|
|
||
| int flash_stm32_option_bytes_lock(const struct device *dev, bool enable) | ||
erwango marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| FLASH_TypeDef *regs = FLASH_STM32_REGS(dev); | ||
|
|
||
| if (enable) { | ||
| regs->OPTCR |= FLASH_OPTCR_OPTLOCK; | ||
| } else if (regs->OPTCR & FLASH_OPTCR_OPTLOCK) { | ||
| regs->OPTKEYR = FLASH_OPT_KEY1; | ||
| regs->OPTKEYR = FLASH_OPT_KEY2; | ||
| } | ||
|
|
||
| if (enable) { | ||
| LOG_DBG("Option bytes locked"); | ||
| } else { | ||
| LOG_DBG("Option bytes unlocked"); | ||
| } | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
| bool flash_stm32_valid_range(const struct device *dev, off_t offset, uint32_t len, bool write) | ||
| { | ||
|
|
@@ -676,6 +721,9 @@ static const struct flash_driver_api flash_stm32h7_api = { | |
| #ifdef CONFIG_FLASH_PAGE_LAYOUT | ||
| .page_layout = flash_stm32_page_layout, | ||
| #endif | ||
| #ifdef CONFIG_FLASH_EX_OP_ENABLED | ||
| .ex_op = flash_stm32_ex_op, | ||
| #endif | ||
| }; | ||
|
|
||
| static int stm32h7_flash_init(const struct device *dev) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| CONFIG_FLASH_STM32_QSPI=n |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| /* | ||
| * Copyright (c) 2024 STMicroelectronics | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| &flash0 { | ||
| partitions { | ||
| compatible = "fixed-partitions"; | ||
| #address-cells = <1>; | ||
| #size-cells = <1>; | ||
|
|
||
| /* Reserve 4KiB of flash for storage_partition. */ | ||
| storage_partition: partition@f0000 { | ||
| label = "storage"; | ||
| reg = <0x000f0000 DT_SIZE_K(4)>; | ||
| }; | ||
| }; | ||
| }; |
1 change: 1 addition & 0 deletions
1
tests/drivers/flash/stm32/boards/stm32h745i_disco_stm32h745xx_m7.conf
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| CONFIG_FLASH_STM32_QSPI=n |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.