From 0d4787fb3aa1d988fd93ea839902ff2d2378d6b8 Mon Sep 17 00:00:00 2001 From: Konstantin Kondrashov Date: Wed, 14 Aug 2024 10:40:05 +0300 Subject: [PATCH] feat(spi_flash): Adds esp_flash_set_dangerous_write_protection --- .../spi_flash/include/esp_flash_internal.h | 24 +++++++++++++++++-- components/spi_flash/spi_flash_os_func_app.c | 18 +++++++++++++- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/components/spi_flash/include/esp_flash_internal.h b/components/spi_flash/include/esp_flash_internal.h index e7d35b0dfb..d07b3e2290 100644 --- a/components/spi_flash/include/esp_flash_internal.h +++ b/components/spi_flash/include/esp_flash_internal.h @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -95,7 +95,27 @@ esp_err_t esp_flash_app_enable_os_functions(esp_flash_t* chip); */ esp_err_t esp_flash_app_disable_os_functions(esp_flash_t* chip); - +/** + * @brief Set or clear dangerous write protection check on the flash chip. + * + * This function sets the runtime option to allow or disallow writing to + * dangerous areas such as the bootloader and partition table. If + * CONFIG_SPI_FLASH_DANGEROUS_WRITE_ALLOWED is not set, this function allows + * the caller to toggle the protection for specific areas. + * + * If CONFIG_SPI_FLASH_DANGEROUS_WRITE_ALLOWED is set, there is no protection + * check in the system, and this function does nothing. + * + * @param chip The flash chip on which to set the write protection. Only + * "esp_flash_default_chip" is supported. + * @param protect Set to true to enable protection against writing in dangerous + * areas (bootloader, partition table). Set to false to disable + * the protection. + * @return + * - ESP_OK: Successful operation. + * - ESP_ERR_INVALID_ARG: The chip argument is null. + */ +esp_err_t esp_flash_set_dangerous_write_protection(esp_flash_t *chip, const bool protect); #ifdef __cplusplus } diff --git a/components/spi_flash/spi_flash_os_func_app.c b/components/spi_flash/spi_flash_os_func_app.c index 1cae387281..51f0671f09 100644 --- a/components/spi_flash/spi_flash_os_func_app.c +++ b/components/spi_flash/spi_flash_os_func_app.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -350,6 +350,22 @@ esp_err_t esp_flash_app_enable_os_functions(esp_flash_t* chip) return ESP_OK; } +esp_err_t esp_flash_set_dangerous_write_protection(esp_flash_t *chip, const bool protect) +{ +#if !CONFIG_SPI_FLASH_DANGEROUS_WRITE_ALLOWED + if (chip == NULL) { + return ESP_ERR_INVALID_ARG; + } + if (chip->os_func_data != NULL) { + ((app_func_arg_t*)chip->os_func_data)->no_protect = !protect; + } +#else + (void)chip; + (void)protect; +#endif + return ESP_OK; +} + // The goal of this part is to manually insert one valid task execution interval, if the time since // last valid interval exceed the limitation (CONFIG_SPI_FLASH_ERASE_YIELD_DURATION_MS). //