This is an automated email from the ASF dual-hosted git repository. xiaoxiang pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit d0942f5cb3a79d1122c16e8e83b127b772dc0dcd Author: raiden00pl <raide...@railab.me> AuthorDate: Wed May 31 13:14:50 2023 +0200 nrf53: add MCUboot support --- boards/arm/nrf53/common/Kconfig | 90 ++++++++- boards/arm/nrf53/common/include/nrf53_progmem.h | 46 +++++ boards/arm/nrf53/common/scripts/flash_app.ld | 137 +++++++++++++ boards/arm/nrf53/common/scripts/flash_net.ld | 143 ++++++++++++++ boards/arm/nrf53/common/src/Make.defs | 8 + boards/arm/nrf53/common/src/nrf53_boot_image.c | 170 +++++++++++++++++ boards/arm/nrf53/common/src/nrf53_progmem.c | 244 ++++++++++++++++++++++++ 7 files changed, 837 insertions(+), 1 deletion(-) diff --git a/boards/arm/nrf53/common/Kconfig b/boards/arm/nrf53/common/Kconfig index ce537d0af2..8511dea1b1 100644 --- a/boards/arm/nrf53/common/Kconfig +++ b/boards/arm/nrf53/common/Kconfig @@ -1,4 +1,92 @@ # # For a description of the syntax of this configuration file, # see the file kconfig-language.txt in the NuttX tools repository. -# \ No newline at end of file +# + +config NRF53_HAVE_OTA_PARTITION + bool + default n + +menu "Application Image Configuration" + +choice + prompt "Application Image Format" + default NRF53_APP_FORMAT_LEGACY + +config NRF53_APP_FORMAT_LEGACY + bool "Legacy format" + ---help--- + This is the legacy application image format. + +config NRF53_APP_FORMAT_MCUBOOT + bool "MCUboot-bootable format" + select NRF53_HAVE_OTA_PARTITION + ---help--- + The MCUboot support of loading the firmware images. + +endchoice # Application Image Format + +config NRF53_PROGMEM_OTA_PARTITION + bool "MTD driver" + default n + depends on NRF53_HAVE_OTA_PARTITION + select BCH + select MTD + select MTD_BYTE_WRITE + select MTD_PARTITION + select MTD_PROGMEM + select NRF53_PROGMEM + ---help--- + Initialize an MTD driver for the Flash, which will + add an entry at /dev for application access from userspace. + +if NRF53_PROGMEM_OTA_PARTITION + +config NRF53_MCUBOOT_HAVE_SCRACH + bool "Scratch partition support" + default n + +config NRF53_MCUBOOT_HEADER_SIZE + hex + default 0x200 + depends on NRF53_APP_FORMAT_MCUBOOT + +config NRF53_OTA_PRIMARY_SLOT_DEVPATH + string "Application image primary slot device path" + default "/dev/ota0" + +config NRF53_OTA_SECONDARY_SLOT_DEVPATH + string "Application image secondary slot device path" + default "/dev/ota1" + +config NRF53_OTA_PRIMARY_SLOT_OFFSET + hex "MCUboot application image primary slot offset" + default "0x10000" + +config NRF53_OTA_SECONDARY_SLOT_OFFSET + hex "MCUboot application image secondary slot offset" + default "0x80000" + +config NRF53_OTA_SLOT_SIZE + hex "MCUboot application image slot size (in bytes)" + default "0x70000" + +if NRF53_MCUBOOT_HAVE_SCRACH + +config NRF53_OTA_SCRATCH_DEVPATH + string "Scratch partition device path" + default "/dev/otascratch" + +config NRF53_OTA_SCRATCH_OFFSET + hex "MCUboot scratch partition offset" + default "0xf0000" + +config NRF53_OTA_SCRATCH_SIZE + hex "MCUboot scratch partition size (in bytes)" + default "0x10000" + +endif # NRF53_MCUBOOT_HAVE_SCRACH + +endif # NRF53_PROGMEM_OTA_PARTITION + +endmenu # Application Image Configuration diff --git a/boards/arm/nrf53/common/include/nrf53_progmem.h b/boards/arm/nrf53/common/include/nrf53_progmem.h new file mode 100644 index 0000000000..8af05a3844 --- /dev/null +++ b/boards/arm/nrf53/common/include/nrf53_progmem.h @@ -0,0 +1,46 @@ +/**************************************************************************** + * boards/arm/nrf53/common/include/nrf53_progmem.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 __BOARDS_ARM_NRF53_COMMON_INCLUDE_NRF53_PROGMEM_H +#define __BOARDS_ARM_NRF53_COMMON_INCLUDE_NRF53_PROGMEM_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +/**************************************************************************** + * Public Functions Prototypes + ****************************************************************************/ + +/**************************************************************************** + * Name: nrf53_mtd_initialize + * + * Description: + * Initialize MTD drivers. + * + ****************************************************************************/ + +#ifdef CONFIG_NRF53_PROGMEM +int nrf53_progmem_init(void); +#endif + +#endif /* __BOARDS_ARM_NRF53_COMMON_INCLUDE_NRF53_PROGMEM_H */ diff --git a/boards/arm/nrf53/common/scripts/flash_app.ld b/boards/arm/nrf53/common/scripts/flash_app.ld new file mode 100644 index 0000000000..7bc611c60c --- /dev/null +++ b/boards/arm/nrf53/common/scripts/flash_app.ld @@ -0,0 +1,137 @@ +/**************************************************************************** + * boards/arm/nrf53/common/scripts/flash_app.ld + * + * 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. + * + ****************************************************************************/ + +#include <nuttx/config.h> + +#define FLASH_CPUAPP_START_ADDR 0x00000000 +#define RAM_CPUAPP_START_ADDR 0x20000000 + +/* Image format configuration */ + +#ifdef CONFIG_NRF53_APP_FORMAT_MCUBOOT +# define MCUBOOT_HEADER_SIZE CONFIG_NRF53_MCUBOOT_HEADER_SIZE +# ifdef CONFIG_BOARDCTL_BOOT_IMAGE +# define FLASH_OFFSET 0 +# define FLASH_IMG_SIZE CONFIG_NRF53_OTA_PRIMARY_SLOT_OFFSET +# else +# define FLASH_OFFSET (CONFIG_NRF53_OTA_PRIMARY_SLOT_OFFSET + MCUBOOT_HEADER_SIZE) +# define FLASH_IMG_SIZE (CONFIG_NRF53_OTA_SLOT_SIZE - MCUBOOT_HEADER_SIZE) +# endif +#else +# define FLASH_OFFSET 0 +# define FLASH_IMG_SIZE CONFIG_NRF53_CPUAPP_MEM_FLASH_SIZE +#endif + +/* Shared memory configuration */ + +#ifdef CONFIG_RPTUN +# define RPTUN_SHMEM 0x8000 +#else +# define RPTUN_SHMEM 0 +#endif + +MEMORY +{ + progmem (rx) : ORIGIN = FLASH_CPUAPP_START_ADDR + FLASH_OFFSET, + LENGTH = FLASH_IMG_SIZE + datamem (rwx) : ORIGIN = RAM_CPUAPP_START_ADDR, + LENGTH = CONFIG_NRF53_CPUAPP_MEM_RAM_SIZE - RPTUN_SHMEM +} + +OUTPUT_ARCH(arm) +ENTRY(__start) /* Treat __start as the anchor for dead code stripping */ +EXTERN(_vectors) /* Force the vectors to be included in the output */ +SECTIONS +{ + .text : + { + _stext = ABSOLUTE(.); + *(.vectors) + *(.text .text.*) + *(.fixup) + *(.gnu.warning) + *(.rodata .rodata.*) + *(.gnu.linkonce.t.*) + *(.glue_7) + *(.glue_7t) + *(.got) + *(.gcc_except_table) + *(.gnu.linkonce.r.*) + _etext = ABSOLUTE(.); + } > progmem + + .init_section : + { + _sinit = ABSOLUTE(.); + *(.init_array .init_array.*) + _einit = ABSOLUTE(.); + } > progmem + + .ARM.extab : + { + *(.ARM.extab*) + } > progmem + + __exidx_start = ABSOLUTE(.); + .ARM.exidx : + { + *(.ARM.exidx*) + } > progmem + __exidx_end = ABSOLUTE(.); + + _eronly = ABSOLUTE(.); + + .data : + { + _sdata = ABSOLUTE(.); + *(.data .data.*) + *(.gnu.linkonce.d.*) + CONSTRUCTORS + . = ALIGN(4); + _edata = ABSOLUTE(.); + } > datamem AT > progmem + + /* BSS */ + + .bss : + { + _sbss = ABSOLUTE(.); + *(.bss .bss.*) + *(.gnu.linkonce.b.*) + *(COMMON) + . = ALIGN(4); + _ebss = ABSOLUTE(.); + } > datamem + + /* Stabs debugging sections. */ + + .stab 0 : { *(.stab) } + .stabstr 0 : { *(.stabstr) } + .stab.excl 0 : { *(.stab.excl) } + .stab.exclstr 0 : { *(.stab.exclstr) } + .stab.index 0 : { *(.stab.index) } + .stab.indexstr 0 : { *(.stab.indexstr) } + .comment 0 : { *(.comment) } + .debug_abbrev 0 : { *(.debug_abbrev) } + .debug_info 0 : { *(.debug_info) } + .debug_line 0 : { *(.debug_line) } + .debug_pubnames 0 : { *(.debug_pubnames) } + .debug_aranges 0 : { *(.debug_aranges) } +} diff --git a/boards/arm/nrf53/common/scripts/flash_net.ld b/boards/arm/nrf53/common/scripts/flash_net.ld new file mode 100644 index 0000000000..428b73adf7 --- /dev/null +++ b/boards/arm/nrf53/common/scripts/flash_net.ld @@ -0,0 +1,143 @@ +/**************************************************************************** + * boards/arm/nrf53/common/scripts/flash_net.ld + * + * 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. + * + ****************************************************************************/ + +#include <nuttx/config.h> + +#define FLASH_CPUAPP_START_ADDR 0x00000000 +#define RAM_CPUAPP_START_ADDR 0x20000000 +#define FLASH_CPUNET_START_ADDR 0x01000000 +#define RAM_CPUNET_START_ADDR 0x21000000 + +/* Image format configuration */ + +#ifdef CONFIG_NRF53_APP_FORMAT_MCUBOOT +# define MCUBOOT_HEADER_SIZE CONFIG_NRF53_MCUBOOT_HEADER_SIZE +# ifdef CONFIG_BOARDCTL_BOOT_IMAGE +# define FLASH_OFFSET 0 +# define FLASH_IMG_SIZE CONFIG_NRF53_OTA_PRIMARY_SLOT_OFFSET +# else +# define FLASH_OFFSET (CONFIG_NRF53_OTA_PRIMARY_SLOT_OFFSET + MCUBOOT_HEADER_SIZE) +# define FLASH_IMG_SIZE (CONFIG_NRF53_OTA_SLOT_SIZE - MCUBOOT_HEADER_SIZE) +# endif +#else +# define FLASH_OFFSET 0 +# define FLASH_IMG_SIZE CONFIG_NRF53_CPUNET_MEM_FLASH_SIZE +#endif + +/* Shared memory configuration */ + +#ifdef CONFIG_RPTUN +# define RPTUN_SHMEM 0x8000 +#else +# define RPTUN_SHMEM 0 +#endif + +MEMORY +{ + app_progmem (rx) : ORIGIN = FLASH_CPUAPP_START_ADDR, + LENGTH = CONFIG_NRF53_CPUAPP_MEM_FLASH_SIZE + app_datamem (rwx) : ORIGIN = RAM_CPUAPP_START_ADDR, + LENGTH = CONFIG_NRF53_CPUAPP_MEM_RAM_SIZE - RPTUN_SHMEM + progmem (rx) : ORIGIN = FLASH_CPUNET_START_ADDR + FLASH_OFFSET, + LENGTH = FLASH_IMG_SIZE + datamem (rwx) : ORIGIN = RAM_CPUNET_START_ADDR, + LENGTH = CONFIG_NRF53_CPUNET_MEM_RAM_SIZE +} + +OUTPUT_ARCH(arm) +ENTRY(__start) /* Treat __start as the anchor for dead code stripping */ +EXTERN(_vectors) /* Force the vectors to be included in the output */ +SECTIONS +{ + .text : + { + _stext = ABSOLUTE(.); + *(.vectors) + *(.text .text.*) + *(.fixup) + *(.gnu.warning) + *(.rodata .rodata.*) + *(.gnu.linkonce.t.*) + *(.glue_7) + *(.glue_7t) + *(.got) + *(.gcc_except_table) + *(.gnu.linkonce.r.*) + _etext = ABSOLUTE(.); + } > progmem + + .init_section : + { + _sinit = ABSOLUTE(.); + *(.init_array .init_array.*) + _einit = ABSOLUTE(.); + } > progmem + + .ARM.extab : + { + *(.ARM.extab*) + } > progmem + + __exidx_start = ABSOLUTE(.); + .ARM.exidx : + { + *(.ARM.exidx*) + } > progmem + __exidx_end = ABSOLUTE(.); + + _eronly = ABSOLUTE(.); + + .data : + { + _sdata = ABSOLUTE(.); + *(.data .data.*) + *(.gnu.linkonce.d.*) + CONSTRUCTORS + . = ALIGN(4); + _edata = ABSOLUTE(.); + } > datamem AT > progmem + + /* BSS */ + + .bss : + { + _sbss = ABSOLUTE(.); + *(.bss .bss.*) + *(.gnu.linkonce.b.*) + *(COMMON) + . = ALIGN(4); + _ebss = ABSOLUTE(.); + } > datamem + + /* Stabs debugging sections. */ + + .stab 0 : { *(.stab) } + .stabstr 0 : { *(.stabstr) } + .stab.excl 0 : { *(.stab.excl) } + .stab.exclstr 0 : { *(.stab.exclstr) } + .stab.index 0 : { *(.stab.index) } + .stab.indexstr 0 : { *(.stab.indexstr) } + .comment 0 : { *(.comment) } + .debug_abbrev 0 : { *(.debug_abbrev) } + .debug_info 0 : { *(.debug_info) } + .debug_line 0 : { *(.debug_line) } + .debug_pubnames 0 : { *(.debug_pubnames) } + .debug_aranges 0 : { *(.debug_aranges) } +} diff --git a/boards/arm/nrf53/common/src/Make.defs b/boards/arm/nrf53/common/src/Make.defs index 6799653b81..b19ea5fe18 100644 --- a/boards/arm/nrf53/common/src/Make.defs +++ b/boards/arm/nrf53/common/src/Make.defs @@ -24,6 +24,14 @@ ifeq ($(CONFIG_NRF53_TIMER),y) CSRCS += nrf53_timer.c endif +ifeq ($(CONFIG_BOARDCTL_BOOT_IMAGE),y) + CSRCS += nrf53_boot_image.c +endif + +ifeq ($(CONFIG_NRF53_PROGMEM),y) + CSRCS += nrf53_progmem.c +endif + DEPPATH += --dep-path src VPATH += :src CFLAGS += $(shell $(INCDIR) "$(CC)" $(TOPDIR)$(DELIM)arch$(DELIM)$(CONFIG_ARCH)$(DELIM)src$(DELIM)board$(DELIM)src) diff --git a/boards/arm/nrf53/common/src/nrf53_boot_image.c b/boards/arm/nrf53/common/src/nrf53_boot_image.c new file mode 100644 index 0000000000..0e9bdd30fa --- /dev/null +++ b/boards/arm/nrf53/common/src/nrf53_boot_image.c @@ -0,0 +1,170 @@ +/**************************************************************************** + * boards/arm/nrf53/common/src/nrf53_boot_image.c + * + * 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. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <debug.h> +#include <stdio.h> +#include <fcntl.h> + +#include <sys/boardctl.h> +#include <nuttx/irq.h> +#include <nuttx/cache.h> + +#include "nvic.h" +#include "arm_internal.h" +#include "barriers.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +/* This structure represents the first two entries on NVIC vector table */ + +struct arm_vector_table +{ + uint32_t spr; /* Stack pointer on reset */ + uint32_t reset; /* Pointer to reset exception handler */ +}; + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +static void cleanup_arm_nvic(void); +static void systick_disable(void); + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: cleanup_arm_nvic + * + * Description: + * Acknowledge and disable all interrupts in NVIC + * + * Input Parameters: + * None + * + * Returned Value: + * None + * + ****************************************************************************/ + +static void cleanup_arm_nvic(void) +{ + int i; + + /* Allow any pending interrupts to be recognized */ + + ARM_ISB(); + cpsid(); + + /* Disable all interrupts */ + + for (i = 0; i < NR_IRQS; i += 32) + { + putreg32(0xffffffff, NVIC_IRQ_CLEAR(i)); + } + + /* Clear all pending interrupts */ + + for (i = 0; i < NR_IRQS; i += 32) + { + putreg32(0xffffffff, NVIC_IRQ_CLRPEND(i)); + } +} + +/**************************************************************************** + * Name: systick_disable + * + * Description: + * Disable the SysTick system timer + * + * Input Parameters: + * None + * + * Returned Value: + * None + * + ****************************************************************************/ + +static void systick_disable(void) +{ + putreg32(0, NVIC_SYSTICK_CTRL); + putreg32(NVIC_SYSTICK_RELOAD_MASK, NVIC_SYSTICK_RELOAD); + putreg32(0, NVIC_SYSTICK_CURRENT); +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: board_boot_image + * + * Description: + * This entry point is called by bootloader to jump to application image. + * + ****************************************************************************/ + +int board_boot_image(const char *path, uint32_t hdr_size) +{ + static struct arm_vector_table vt; + struct file file; + ssize_t bytes; + int ret; + + ret = file_open(&file, path, O_RDONLY | O_CLOEXEC); + if (ret < 0) + { + syslog(LOG_ERR, "Failed to open %s with: %d", path, ret); + return ret; + } + + bytes = file_pread(&file, &vt, sizeof(vt), hdr_size); + if (bytes != sizeof(vt)) + { + syslog(LOG_ERR, "Failed to read ARM vector table: %d", bytes); + return bytes < 0 ? bytes : -1; + } + + systick_disable(); + + cleanup_arm_nvic(); + + /* Set main and process stack pointers */ + + __asm__ __volatile__("\tmsr msp, %0\n" : : "r" (vt.spr)); + setcontrol(0x00); + ARM_ISB(); + ((void (*)(void))vt.reset)(); + + return 0; +} diff --git a/boards/arm/nrf53/common/src/nrf53_progmem.c b/boards/arm/nrf53/common/src/nrf53_progmem.c new file mode 100644 index 0000000000..ecdc8d0b74 --- /dev/null +++ b/boards/arm/nrf53/common/src/nrf53_progmem.c @@ -0,0 +1,244 @@ +/**************************************************************************** + * boards/arm/nrf53/common/src/nrf53_progmem.c + * + * 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. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <sys/mount.h> +#include <sys/param.h> + +#include <stdbool.h> +#include <stdlib.h> +#include <stdio.h> +#include <assert.h> +#include <errno.h> +#include <debug.h> + +#include <nuttx/progmem.h> +#include <nuttx/drivers/drivers.h> +#include <nuttx/fs/ioctl.h> +#include <nuttx/kmalloc.h> +#include <nuttx/mtd/mtd.h> +#include <nuttx/drivers/drivers.h> + +#include "nrf53_progmem.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define PARTITION_LABEL_LEN 16 + +/* Configuration ************************************************************/ + +/* Make sure that support for MTD partitions is enabled */ + +#ifndef CONFIG_MTD_PARTITION +# error "CONFIG_MTD_PARTITION is required" +#endif + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +#ifdef CONFIG_NRF53_PROGMEM_OTA_PARTITION +struct ota_partition_s +{ + uint32_t offset; /* Partition offset from the beginning of MTD */ + uint32_t size; /* Partition size in bytes */ + const char *devpath; /* Partition device path */ +}; +#endif + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +#ifdef CONFIG_NRF53_PROGMEM_OTA_PARTITION +static struct mtd_dev_s *progmem_alloc_mtdpart(uint32_t mtd_offset, + uint32_t mtd_size); +static int init_ota_partitions(void); +#endif + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static struct mtd_dev_s *g_progmem_mtd; + +#ifdef CONFIG_NRF53_PROGMEM_OTA_PARTITION +static const struct ota_partition_s g_ota_partition_table[] = +{ + { + .offset = CONFIG_NRF53_OTA_PRIMARY_SLOT_OFFSET, + .size = CONFIG_NRF53_OTA_SLOT_SIZE, + .devpath = CONFIG_NRF53_OTA_PRIMARY_SLOT_DEVPATH + }, + { + .offset = CONFIG_NRF53_OTA_SECONDARY_SLOT_OFFSET, + .size = CONFIG_NRF53_OTA_SLOT_SIZE, + .devpath = CONFIG_NRF53_OTA_SECONDARY_SLOT_DEVPATH + }, +#ifdef CONFIG_NRF53_MCUBOOT_HAVE_SCRACH + { + .offset = CONFIG_NRF53_OTA_SCRATCH_OFFSET, + .size = CONFIG_NRF53_OTA_SCRATCH_SIZE, + .devpath = CONFIG_NRF53_OTA_SCRATCH_DEVPATH + } +#endif +}; +#endif + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +#ifdef CONFIG_NRF53_PROGMEM_OTA_PARTITION +/**************************************************************************** + * Name: progmem_alloc_mtdpart + * + * Description: + * Allocate an MTD partition from FLASH. + * + * Input Parameters: + * mtd_offset - MTD Partition offset from the base address in FLASH. + * mtd_size - Size for the MTD partition. + * + * Returned Value: + * MTD partition data pointer on success, NULL on failure. + * + ****************************************************************************/ + +static struct mtd_dev_s *progmem_alloc_mtdpart(uint32_t mtd_offset, + uint32_t mtd_size) +{ + uint32_t blocks; + ssize_t startblock; + + ASSERT((mtd_offset % up_progmem_pagesize(0)) == 0); + ASSERT((mtd_size % up_progmem_pagesize(0)) == 0); + + finfo("\tMTD offset = 0x%"PRIx32"\n", mtd_offset); + finfo("\tMTD size = 0x%"PRIx32"\n", mtd_size); + + startblock = up_progmem_getpage(mtd_offset + up_progmem_getaddress(0)); + if (startblock < 0) + { + return NULL; + } + + blocks = mtd_size / up_progmem_pagesize(0); + + return mtd_partition(g_progmem_mtd, startblock, blocks); +} + +/**************************************************************************** + * Name: init_ota_partitions + * + * Description: + * Initialize partitions that are dedicated to firmware OTA update. + * + * Input Parameters: + * None. + * + * Returned Value: + * Zero on success; a negated errno value on failure. + * + ****************************************************************************/ + +static int init_ota_partitions(void) +{ + char path[PARTITION_LABEL_LEN + 1]; + struct mtd_dev_s *mtd = NULL; + int i = 0; + int ret = 0; + + for (i = 0; i < nitems(g_ota_partition_table); ++i) + { + const struct ota_partition_s *part = &g_ota_partition_table[i]; + mtd = progmem_alloc_mtdpart(part->offset, part->size); + + strlcpy(path, (char *)part->devpath, PARTITION_LABEL_LEN); + + finfo("INFO: [label]: %s\n", path); + finfo("INFO: [offset]: 0x%08" PRIx32 "\n", part->offset); + finfo("INFO: [size]: 0x%08" PRIx32 "\n", part->size); + + if (!mtd) + { + ferr("ERROR: Failed to create MTD partition\n"); + ret = -1; + } + + ret = register_mtddriver(path, mtd, 0777, NULL); + if (ret < 0) + { + ferr("ERROR: Failed to register MTD @ %s\n", path); + ret = -1; + } + } + + return ret; +} +#endif /* CONFIG_NRF53_PROGMEM_OTA_PARTITION */ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: nrf53_progmem_init + * + * Initialize Progmem partition. Read partition information, and use + * these data for creating MTD. + * + * Input Parameters: + * None + * + * Returned Value: + * 0 if success or a negative value if fail. + * + ****************************************************************************/ + +int nrf53_progmem_init(void) +{ + int ret = 0; + + g_progmem_mtd = progmem_initialize(); + if (g_progmem_mtd == NULL) + { + ferr("ERROR: Failed to get progmem flash MTD\n"); + ret = -EIO; + } + +#ifdef CONFIG_NRF53_PROGMEM_OTA_PARTITION + ret = init_ota_partitions(); + if (ret < 0) + { + ferr("ERROR: Failed to create OTA partition from MTD\n"); + ret = -EIO; + } +#endif + + return ret; +}