Skip to content

Commit 0d4787f

Browse files
feat(spi_flash): Adds esp_flash_set_dangerous_write_protection
1 parent 02d61c1 commit 0d4787f

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

components/spi_flash/include/esp_flash_internal.h

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -95,7 +95,27 @@ esp_err_t esp_flash_app_enable_os_functions(esp_flash_t* chip);
9595
*/
9696
esp_err_t esp_flash_app_disable_os_functions(esp_flash_t* chip);
9797

98-
98+
/**
99+
* @brief Set or clear dangerous write protection check on the flash chip.
100+
*
101+
* This function sets the runtime option to allow or disallow writing to
102+
* dangerous areas such as the bootloader and partition table. If
103+
* CONFIG_SPI_FLASH_DANGEROUS_WRITE_ALLOWED is not set, this function allows
104+
* the caller to toggle the protection for specific areas.
105+
*
106+
* If CONFIG_SPI_FLASH_DANGEROUS_WRITE_ALLOWED is set, there is no protection
107+
* check in the system, and this function does nothing.
108+
*
109+
* @param chip The flash chip on which to set the write protection. Only
110+
* "esp_flash_default_chip" is supported.
111+
* @param protect Set to true to enable protection against writing in dangerous
112+
* areas (bootloader, partition table). Set to false to disable
113+
* the protection.
114+
* @return
115+
* - ESP_OK: Successful operation.
116+
* - ESP_ERR_INVALID_ARG: The chip argument is null.
117+
*/
118+
esp_err_t esp_flash_set_dangerous_write_protection(esp_flash_t *chip, const bool protect);
99119

100120
#ifdef __cplusplus
101121
}

components/spi_flash/spi_flash_os_func_app.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -350,6 +350,22 @@ esp_err_t esp_flash_app_enable_os_functions(esp_flash_t* chip)
350350
return ESP_OK;
351351
}
352352

353+
esp_err_t esp_flash_set_dangerous_write_protection(esp_flash_t *chip, const bool protect)
354+
{
355+
#if !CONFIG_SPI_FLASH_DANGEROUS_WRITE_ALLOWED
356+
if (chip == NULL) {
357+
return ESP_ERR_INVALID_ARG;
358+
}
359+
if (chip->os_func_data != NULL) {
360+
((app_func_arg_t*)chip->os_func_data)->no_protect = !protect;
361+
}
362+
#else
363+
(void)chip;
364+
(void)protect;
365+
#endif
366+
return ESP_OK;
367+
}
368+
353369
// The goal of this part is to manually insert one valid task execution interval, if the time since
354370
// last valid interval exceed the limitation (CONFIG_SPI_FLASH_ERASE_YIELD_DURATION_MS).
355371
//

0 commit comments

Comments
 (0)