From 2693322e9d6046aaa81cc9e07e4ac4e0026f63a4 Mon Sep 17 00:00:00 2001 From: nuttxs Date: Sat, 12 Oct 2024 19:01:05 +0800 Subject: [PATCH] xtensa/esp32s3: Inspect if the MTD partition (factory/ota_0/ota_1) is mapped as text. Relocate the enum ota_img_ctrl_e and ota_img_bootseq_e to a directory visible to the application. --- arch/xtensa/include/esp32s3/partition.h | 56 +++++++++++++++++ arch/xtensa/src/esp32s3/esp32s3_partition.c | 66 +++++++++++++++------ arch/xtensa/src/esp32s3/esp32s3_spiflash.c | 21 +++++++ arch/xtensa/src/esp32s3/esp32s3_spiflash.h | 16 +++++ 4 files changed, 140 insertions(+), 19 deletions(-) create mode 100644 arch/xtensa/include/esp32s3/partition.h diff --git a/arch/xtensa/include/esp32s3/partition.h b/arch/xtensa/include/esp32s3/partition.h new file mode 100644 index 0000000000000..01b2bb5ec162c --- /dev/null +++ b/arch/xtensa/include/esp32s3/partition.h @@ -0,0 +1,56 @@ +/**************************************************************************** + * arch/xtensa/include/esp32s3/partition.h + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +#ifndef __ARCH_XTENSA_INCLUDE_ESP32S3_PARTITION_H +#define __ARCH_XTENSA_INCLUDE_ESP32S3_PARTITION_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/**************************************************************************** + * Public Types + ****************************************************************************/ + +/* OTA image operation code */ + +enum ota_img_ctrl_e +{ + OTA_IMG_GET_BOOT = 0xe1, + OTA_IMG_SET_BOOT = 0xe2, + OTA_IMG_INVALIDATE_BOOT = 0xe3, + OTA_IMG_IS_MAPPED_AS_TEXT = 0xe4, +}; + +/* OTA image boot sequency */ + +enum ota_img_bootseq_e +{ + OTA_IMG_BOOT_FACTORY = 0, + OTA_IMG_BOOT_OTA_0 = 1, + OTA_IMG_BOOT_OTA_1 = 2, + OTA_IMG_BOOT_SEQ_MAX +}; + +#endif /* __ARCH_XTENSA_INCLUDE_ESP32S3_PARTITION_H */ diff --git a/arch/xtensa/src/esp32s3/esp32s3_partition.c b/arch/xtensa/src/esp32s3/esp32s3_partition.c index edbb0c57ab582..03ec24fc1cc25 100644 --- a/arch/xtensa/src/esp32s3/esp32s3_partition.c +++ b/arch/xtensa/src/esp32s3/esp32s3_partition.c @@ -34,6 +34,7 @@ #include "esp32s3_spiflash_mtd.h" #include "esp32s3_partition.h" #include "esp32s3_spiflash.h" +#include "arch/esp32s3/partition.h" /**************************************************************************** * Pre-processor Definitions @@ -80,15 +81,6 @@ * Private Types ****************************************************************************/ -/* OTA image operation code */ - -enum ota_img_ctrl_e -{ - OTA_IMG_GET_BOOT = 0xe1, - OTA_IMG_SET_BOOT = 0xe2, - OTA_IMG_INVALIDATE_BOOT = 0xe3, -}; - /* OTA image state */ enum ota_img_state_e @@ -134,16 +126,6 @@ enum ota_img_state_e OTA_IMG_UNDEFINED = 0xffffffff, }; -/* OTA image boot sequency */ - -enum ota_img_bootseq_e -{ - OTA_IMG_BOOT_FACTORY = 0, - OTA_IMG_BOOT_OTA_0 = 1, - OTA_IMG_BOOT_OTA_1 = 2, - OTA_IMG_BOOT_SEQ_MAX -}; - /* Partition information data */ struct partition_info_priv_s @@ -403,6 +385,40 @@ static int ota_invalidate_bootseq(struct mtd_dev_priv_s *dev, int num) return 0; } +/**************************************************************************** + * Name: is_currently_mapped_as_text + * + * Description: + * Check if the MTD partition is mapped as text + * + * Input Parameters: + * dev - Partition private MTD data + * mapped - true if mapped, false if not + * + * Returned Value: + * 0 if success or a negative value if fail. + * + ****************************************************************************/ + +static int is_currently_mapped_as_text(struct mtd_dev_priv_s *dev, + bool *mapped) +{ + uint32_t currently_mapped_address; + + if (mapped == NULL) + { + ferr("ERROR: Invalid argument.\n"); + return -EINVAL; + } + + currently_mapped_address = esp32s3_get_flash_address_mapped_as_text(); + + *mapped = ((dev->offset <= currently_mapped_address) && + (currently_mapped_address < dev->offset + dev->size)); + + return OK; +} + /**************************************************************************** * Name: esp32s3_part_erase * @@ -585,6 +601,18 @@ static int esp32s3_part_ioctl(struct mtd_dev_s *dev, int cmd, } } + break; + case OTA_IMG_IS_MAPPED_AS_TEXT: + { + bool *mapped = (bool *)arg; + + ret = is_currently_mapped_as_text(mtd_priv, mapped); + if (ret < 0) + { + ferr("ERROR: Failed to check partition is mapped as text\n"); + } + } + break; default: { diff --git a/arch/xtensa/src/esp32s3/esp32s3_spiflash.c b/arch/xtensa/src/esp32s3/esp32s3_spiflash.c index aefdef704d137..9ea87609fe0b7 100644 --- a/arch/xtensa/src/esp32s3/esp32s3_spiflash.c +++ b/arch/xtensa/src/esp32s3/esp32s3_spiflash.c @@ -1374,3 +1374,24 @@ bool esp32s3_flash_encryption_enabled(void) return enabled; } + +/**************************************************************************** + * Name: esp32s3_get_flash_address_mapped_as_text + * + * Description: + * Get flash address which is currently mapped as text + * + * Input Parameters: + * None + * + * Returned Value: + * flash address which is currently mapped as text + * + ****************************************************************************/ + +uint32_t esp32s3_get_flash_address_mapped_as_text(void) +{ + uint32_t i = MMU_ADDR2PAGE((uint32_t)_stext) - + MMU_ADDR2PAGE(SOC_MMU_IBUS_VADDR_BASE); + return (FLASH_MMU_TABLE[i] & MMU_ADDRESS_MASK) * MMU_PAGE_SIZE; +} diff --git a/arch/xtensa/src/esp32s3/esp32s3_spiflash.h b/arch/xtensa/src/esp32s3/esp32s3_spiflash.h index b7a395ebeefa1..b2ad799529aba 100644 --- a/arch/xtensa/src/esp32s3/esp32s3_spiflash.h +++ b/arch/xtensa/src/esp32s3/esp32s3_spiflash.h @@ -166,6 +166,22 @@ int esp32s3_spiflash_init(void); bool esp32s3_flash_encryption_enabled(void); +/**************************************************************************** + * Name: esp32s3_get_flash_address_mapped_as_text + * + * Description: + * Get flash address which is currently mapped as text + * + * Input Parameters: + * None + * + * Returned Value: + * flash address which is currently mapped as text + * + ****************************************************************************/ + +uint32_t esp32s3_get_flash_address_mapped_as_text(void); + #ifdef __cplusplus } #endif