Skip to content

RFC: API change: Add flash write protection and readout protection functions #53905

@duda-patryk

Description

@duda-patryk

Introduction

This RFC was created to discuss introducing flash write protection (WP) and readout protection (RDP) support in flash API

Problem description

Flash controllers in most MCUs (e.g. all STM devices) provide additional features like WP and RDP. With existing flash API, it's not possible to use the features.

Proposed change

I propose to extend flash API with following functions

typedef int (*flash_api_set_write_protect)(const struct device *dev,
					   off_t offset, size_t size,
					   bool enable);

typedef int (*flash_api_get_write_protect)(const struct device *dev,
					   off_t offset, size_t size,
					   bool *enabled);

typedef int (*flash_api_set_readout_protection)(const struct device *dev,
						off_t offset, size_t size,
						bool enable, bool permanent);

typedef int (*flash_api_get_readout_protection)(const struct device *dev,
						off_t offset, size_t size,
						bool *enabled, bool *permanent);

Detailed RFC

Write protection protects flash contents from accidental erases and writes due to program or human error.
Persistent flash write protection is usually implemented in two ways:

  • Sector based - protected area can be controlled per sector.
  • Range based - protected area is defined by a pair of offsets. On most devices it's possible to define more than one pair.

Readout protection protects against reading flash contents using debugger and internal bootloader. Disabling the protection usually erases protected area. It's possible to enable RDP permanently. On STM MCUs RDP is enabled on entire flash. I haven't met a device with different RDP control, but I can imagine RDP controlled on sector level.

Proposed change (Detailed)

Pull request: #53441
Implementation for STM32F4: #52980

Extend flash API with 4 functions. Two functions to set/get WP state and two functions to set/get RDP state. All functions take offset and size which is flexible interface. Driver will be responsible to translate offset and size to e.g. sectors. If it's not possible to protected requested area or protected area will be bigger than requested, the driver should return an error.

Extend flash Kconfig with following options. They are meant to increase security or prevent from doing irreversible things:

  • FLASH_BLOCK_WRITE_PROTECT_DISABLING blocks disabling flash write protection
  • FLASH_ALLOW_DISABLING_READOUT_PROTECTION allows disabling readout protection
  • FLASH_ALLOW_PERMANENT_READOUT_PROTECTION allows enabling readout protection permanently

Dependencies

Proposed change neither doesn't depend nor affects anything in Zephyr.

Concerns and Unresolved Questions

Using offset and size to describe flash area is a good way to ensure compatibility with all flash controllers but if the area we want to protect is not continuous then we need to call function multiple times. This causes multiple erase + write cycles which can affect flash lifespan if changing WP settings frequently.

Alternatives

I'm not aware of any alternatives.

Metadata

Metadata

Assignees

No one assigned

    Labels

    RFCRequest For Comments: want input from the communityarea: Flash

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions