From 6d8f48e8f3d9455f0bc94a45b4b1b0bfc7599352 Mon Sep 17 00:00:00 2001 From: iabdalkader Date: Fri, 8 Nov 2024 09:13:46 +0100 Subject: [PATCH] bootloader: Add separate XIP partitions. --- src/boot/src/common/desc.c | 4 ++-- src/boot/src/common/dfu.c | 6 ++--- src/boot/src/common/mpu.c | 29 +++++++++++++---------- src/omv/boards/OPENMV_N6/omv_bootconfig.h | 29 ----------------------- 4 files changed, 22 insertions(+), 46 deletions(-) delete mode 100644 src/omv/boards/OPENMV_N6/omv_bootconfig.h diff --git a/src/boot/src/common/desc.c b/src/boot/src/common/desc.c index e53fa744d..1aaa8742e 100644 --- a/src/boot/src/common/desc.c +++ b/src/boot/src/common/desc.c @@ -37,7 +37,7 @@ #define STR_DESC_MAX_LEN (32) #define STR_DESC_COUNT (sizeof(desc_string) / sizeof(desc_string[0])) -#define DFU_DESC_ALT_COUNT OMV_BOOT_PARTITIONS_COUNT +#define DFU_DESC_ALT_COUNT OMV_BOOT_DFU_PARTITIONS_COUNT #define DFU_DESC_FUNC_ATTR (DFU_ATTR_CAN_UPLOAD | DFU_ATTR_CAN_DOWNLOAD | DFU_ATTR_MANIFESTATION_TOLERANT) #define CFG_DESC_ITF_DFU (0) @@ -49,7 +49,7 @@ static char const *desc_string[] = { "OpenMV", // 1: Manufacturer "OpenMV Camera (DFU Mode)", // 2: Product "0123456789ABCDEF", // 3: Default Serial. - OMV_BOOT_PARTITIONS_STR, + OMV_BOOT_DFU_PARTITIONS_STR, }; static tusb_desc_device_t const desc_device = { diff --git a/src/boot/src/common/dfu.c b/src/boot/src/common/dfu.c index 7322e3f26..a268ac6ca 100644 --- a/src/boot/src/common/dfu.c +++ b/src/boot/src/common/dfu.c @@ -61,7 +61,7 @@ uint32_t tud_dfu_get_timeout_cb(uint8_t itf, uint8_t state) { // Invoked when DFU_DNLOAD is received followed by DFU_GETSTATUS (state=DFU_DNBUSY). void tud_dfu_download_cb(uint8_t itf, uint16_t block, uint8_t const *buf, uint16_t size) { - const partition_t *p = &OMV_BOOT_PARTITIONS[itf]; + const partition_t *p = &OMV_BOOT_DFU_PARTITIONS[itf]; uint32_t addr = p->start + (block * CFG_TUD_DFU_XFER_BUFSIZE); // Check if partition is writable. @@ -88,13 +88,13 @@ void tud_dfu_download_cb(uint8_t itf, uint16_t block, uint8_t const *buf, uint16 // Invoked when the download process is complete, i.e., // DFU_DNLOAD followed by DFU_GETSTATUS (state=Manifest). void tud_dfu_manifest_cb(uint8_t itf) { - port_mpu_config(&OMV_BOOT_PARTITIONS[itf], 1, 1, 1); + port_mpu_config(&OMV_BOOT_DFU_PARTITIONS[itf], 1, 1, 1); return tud_dfu_finish_flashing(DFU_STATUS_OK); } // Invoked when DFU_UPLOAD request is received. uint16_t tud_dfu_upload_cb(uint8_t itf, uint16_t block, uint8_t *buf, uint16_t size) { - const partition_t *p = &OMV_BOOT_PARTITIONS[itf]; + const partition_t *p = &OMV_BOOT_DFU_PARTITIONS[itf]; uint32_t addr = p->start + (block * CFG_TUD_DFU_XFER_BUFSIZE); if (addr + size > p->limit) { diff --git a/src/boot/src/common/mpu.c b/src/boot/src/common/mpu.c index 5c1f15560..6367e7f93 100644 --- a/src/boot/src/common/mpu.c +++ b/src/boot/src/common/mpu.c @@ -99,31 +99,36 @@ __weak void port_mpu_init(void) { #endif // Configure read-only MPU regions for boot partitions. - for (size_t i = 0; i < OMV_BOOT_PARTITIONS_COUNT; i++) { - // Note first region is the bootloader's partition, - // which normally needs to be executable. - port_mpu_config(&OMV_BOOT_PARTITIONS[i], 1, 1, (i != 0)); + for (size_t i = 0; i < OMV_BOOT_DFU_PARTITIONS_COUNT; i++) { + // Note first region is the bootloader's partition, which normally + // needs to be executable. If more executable partitions are needed + // a XN flag can be added to partition_t. + port_mpu_config(&OMV_BOOT_DFU_PARTITIONS[i], 1, 1, (i != 0)); } } __weak void port_mpu_deinit() { #if (__ARM_ARCH >= 8) ARM_MPU_Disable(); + // Disable all regions. - for (size_t i = 0; i < OMV_BOOT_PARTITIONS_COUNT; i++) { - if (OMV_BOOT_PARTITIONS[i].region != -1) { - ARM_MPU_ClrRegion(OMV_BOOT_PARTITIONS[i].region); + for (size_t i = 0; i < OMV_BOOT_DFU_PARTITIONS_COUNT; i++) { + if (OMV_BOOT_DFU_PARTITIONS[i].region != -1) { + ARM_MPU_ClrRegion(OMV_BOOT_DFU_PARTITIONS[i].region); } } - // Configure XIP MPU regions (if any). - for (size_t i = 0; i < OMV_BOOT_PARTITIONS_COUNT; i++) { - if (OMV_BOOT_PARTITIONS[i].type == PTYPE_XIP_FLASH) { - port_mpu_config(&OMV_BOOT_PARTITIONS[i], 1, 1, 0); + #ifdef OMV_BOOT_XIP_PARTITIONS_COUNT + // Configure MPU regions for XIP partitions. + for (size_t i = 0; i < OMV_BOOT_XIP_PARTITIONS_COUNT; i++) { + if (OMV_BOOT_XIP_PARTITIONS[i].type == PTYPE_XIP_FLASH) { + port_mpu_config(&OMV_BOOT_XIP_PARTITIONS[i], 1, 1, 0); } } + #endif // OMV_BOOT_XIP_PARTITIONS_COUNT + ARM_MPU_Enable(MPU_CTRL_PRIVDEFENA_Msk | MPU_CTRL_HFNMIENA_Msk); - #endif + #endif // __ARM_ARCH >= 8 } __weak void port_mpu_load_defaults() { diff --git a/src/omv/boards/OPENMV_N6/omv_bootconfig.h b/src/omv/boards/OPENMV_N6/omv_bootconfig.h deleted file mode 100644 index 7f37ec5b4..000000000 --- a/src/omv/boards/OPENMV_N6/omv_bootconfig.h +++ /dev/null @@ -1,29 +0,0 @@ -/* - * This file is part of the OpenMV project. - * - * Copyright (c) 2013-2022 Ibrahim Abdelkader - * Copyright (c) 2013-2022 Kwabena W. Agyeman - * - * This work is licensed under the MIT license, see the file LICENSE for details. - * - * Board configuration and pin definitions. - */ -#ifndef __OMV_BOOTCONFIG_H__ -#define __OMV_BOOTCONFIG_H__ - -// Bootloader version. -#define OMV_BOOT_VERSION (0xABCD0003) - -// Bootloader LED GPIO config. -#define OMV_BOOT_LED_PIN (GPIO_PIN_7) -#define OMV_BOOT_LED_PORT (GPIOA) - -// Flash layout for the bootloader. -// Flash FS sector, main FW sector, max sector. -#define OMV_BOOT_FLASH_LAYOUT {1, 2, 15} - -// Flash configuration. -#define OMV_BOOT_FLASH_ORIGIN 0x34180400 -#define OMV_BOOT_FLASH_LENGTH 128K - -#endif //__OMV_BOOTCONFIG_H__