-
Notifications
You must be signed in to change notification settings - Fork 7.5k
MPU flash write #654
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
MPU flash write #654
Conversation
Thanks for picking this back up! This LGTM; one minor question is about the discussion in #205 on whether MPU_ALLOW_FLASH_WRITE should be 'default y if FLASH', or if the user should need to know about it and turn it on. You've noted that the changed configuration is required on NXP just to enable the flash driver -- regardless of whether flash_erase() or flash_write() are called. I wonder if a better name for it would be MPU_ALLOW_FLASH_ACCESS, and make it default y if flash. |
It might be easiest to bring this in without changing the default, and a future patch could do that if we agree. Right now, without this patch, the flash drivers aren't useful, with it as is, it at least can be enabled. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of adding more configuration files prj_XXX.conf which are platform specific I would prefer to document the options instead or add them to the original config file and disable them and document that they need to be enabled.
@@ -0,0 +1,3 @@ | |||
CONFIG_CONSOLE_SHELL=y | |||
CONFIG_MPU_ALLOW_FLASH_WRITE=y | |||
CONFIG_FLASH=y |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The sample has moved to samples/mpu/mpu_test, so those files need to move...
samples/mpu/mpu_test/README.rst
Outdated
@@ -46,6 +46,15 @@ single thread: :file:`prj_single.conf`: | |||
|
|||
$ make BOARD=v2m_beetle CONF_FILE=prj_single.conf run | |||
|
|||
To build the version that allows flash write, use the supplied configuration | |||
file: :file:`prj_allow_flash_write_<x>.conf`: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
before the trailing colon, add the explanation about <x>
instead of after the code block:
file: :file:`prj_allow_flash_write_<x>.conf`, replacing `<x>` with either `mcux` or `stm32`:
samples/mpu/mpu_test/README.rst
Outdated
|
||
$ make BOARD=v2m_beetle CONF_FILE=prj_allow_flash_write_<x>.conf run | ||
|
||
<x> = mcux or stm32 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
... and delete this here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
change recommended
We need more than just flash write access though, we also need sram execute, and I think enabling sram execute should be explicit. The reason we fault when enabling the nxp flash driver is that the driver init actually tries to execute from ram. The problem code is under the shim, in ext/hal/nxp/mcux/drivers/fsl_flash.c:2695: /* Workround for some devices which doesn't need this function */
callFlashCommonBitOperation((FTFx_REG32_ACCESS_TYPE)0, 0, 0, 0) I'm not quite sure yet why the driver does this. |
0f36cad
to
1bc2fdc
Compare
I have stumbled upon the problem with MPU and mcux flash driver while playing with #542 and usb dfu class sample.
The driver uses the InRamFunctions when it executes from flash. K64F Reference Manual was not really helpful, I only found a note in Chapter 29.4.7: "The MCU must not read from the flash memory while commands are running...". Anyway, I think permanent permission to execute the code from the RAM is not a good solution. |
The way the NXP controller is implemented the entire contents of flash go away while performing flash operations. This is fairly common among lots of NOR controllers. It generally takes extra hardware to be able to read/execute from FLASH that do allow reads of the parts that aren't being programmed.
Is the concern here with rogue code making it into RAM, and then some kind of pointer fault causing it to execute at this address? Yes this is a concern, but I don't think it is a particularly common exploit. One of the things we're going to have to face here is that the MPU isn't going to be able to protect everything, and we need to figure out what is most important to protect. We can certainly make this dynamic, but it adds complexity and additional functionality, not really intended. I fear we are going to end up bikeshedding this patch series to death, and never get it in. As it is, the MPU is completely useless for any code that needs to write to flash. On NXP, it is worse, and the MPU prevents boot entirely if the flash driver is even enabled. An MPU that protects some things is better than having to disable it entirely. |
Wouldn't this also mean that they won't get tested, either? I'm beginning to lean toward the correct solution here being to not have these config options, but just a few settings that get selected according to what other features are enabled:
I'll give a little time for feedback on this proposal, and then rework the patches to do this. |
0723c92
to
38db4ed
Compare
Guilty :(. Let me get out of your way... |
Need to address @dbkinder doc comments. |
This patch adds the allow flash write CONFIG option to the NXP MPU configuration in privileged mode. Signed-off-by: Vincenzo Frascino <[email protected]> Signed-off-by: David Brown <[email protected]>
This patch adds the allow flash write CONFIG option to the ARM MPU configuration in privileged mode. Signed-off-by: Vincenzo Frascino <[email protected]> Signed-off-by: Michael Scott <[email protected]> Signed-off-by: David Brown <[email protected]>
samples/mpu/mpu_test/README.rst
Outdated
@@ -46,6 +46,10 @@ single thread: :file:`prj_single.conf`: | |||
|
|||
$ make BOARD=v2m_beetle CONF_FILE=prj_single.conf run | |||
|
|||
To build a version that allows writes to the flash device, edit | |||
``prj.conf``, and following the directions in the comments to enable |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/following/follow/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
small edit requested
This patch adds a configuration file that tests Flash writes with MPU enabled. [david.brown: Put options in prj.conf with comments as per review feedback] Signed-off-by: Vincenzo Frascino <[email protected]> Signed-off-by: David Brown <[email protected]>
Ping @dbkinder, I believe I've addressed your concern. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not quite sure yet why the driver does this.
The way the NXP controller is implemented the entire contents of flash go away while performing flash operations. This is fairly common among lots of NOR controllers. It generally takes extra hardware to be able to read/execute from FLASH that do allow reads of the parts that aren't being programmed.
My concern was why the mcux flash driver tries to execute anything from ram during init, before anything tries to erase or program flash. In other words, why we can't even boot Zephyr with the flash driver enabled. This will get fixed in the next release of mcux.
#if defined(CONFIG_MPU_ALLOW_FLASH_WRITE) | ||
#define REGION_FLASH_ATTR(size) \ | ||
(NORMAL_OUTER_INNER_NON_CACHEABLE_NON_SHAREABLE | size | \ | ||
P_RW_U_RO) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does REGION_RAM_ATTR need execute access as in the NXP MPU?
…ject-rtos#654) This bug was hit if you tried to pass the fourth encoding parameter to the function; it would get an error even if you passed 'utf8'. Also, cleaned up indentation issue in I2C.js. Signed-off-by: Geoff Gustafson <[email protected]>
This is a rebase of #205. This is needed to even enable the flash driver on NXP without faulting.