Skip to content

Commit

Permalink
xtensa/esp32s3: Inspect if the MTD partition (factory/ota_0/ota_1)
Browse files Browse the repository at this point in the history
is mapped as text.
Relocate the enum ota_img_ctrl_e and ota_img_bootseq_e to
a directory visible to the application.
  • Loading branch information
nuttxs committed Oct 12, 2024
1 parent 976c417 commit d50bef0
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 19 deletions.
56 changes: 56 additions & 0 deletions arch/xtensa/include/esp32s3/partition.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/****************************************************************************
* arch/xtensa/include/esp32s3/partion.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 */
63 changes: 44 additions & 19 deletions arch/xtensa/src/esp32s3/esp32s3_partition.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -403,6 +385,37 @@ 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)
{
if (mapped == NULL)
{
ferr("ERROR: Invalid argument.\n");
return -EINVAL;
}

uint32_t 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
*
Expand Down Expand Up @@ -585,6 +598,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 if the partition is mapped as text\n");
}
}

break;
default:
{
Expand Down
21 changes: 21 additions & 0 deletions arch/xtensa/src/esp32s3/esp32s3_spiflash.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
16 changes: 16 additions & 0 deletions arch/xtensa/src/esp32s3/esp32s3_spiflash.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d50bef0

Please sign in to comment.