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 1facea635b7b281c1873b22b4c9eb7f08a3b31ae
Author: raiden00pl <[email protected]>
AuthorDate: Tue May 30 11:00:12 2023 +0200

    nrf52: add MCUboot support
---
 arch/arm/src/nrf52/Kconfig                      |  51 +++++
 arch/arm/src/nrf52/nrf52_flash.c                |   3 +-
 boards/arm/nrf52/common/Kconfig                 |  90 ++++++++-
 boards/arm/nrf52/common/include/nrf52_progmem.h |  46 +++++
 boards/arm/nrf52/common/scripts/flash_config.ld | 127 ++++++++++++
 boards/arm/nrf52/common/src/Make.defs           |   8 +
 boards/arm/nrf52/common/src/nrf52_boot_image.c  | 170 +++++++++++++++++
 boards/arm/nrf52/common/src/nrf52_progmem.c     | 244 ++++++++++++++++++++++++
 8 files changed, 737 insertions(+), 2 deletions(-)

diff --git a/arch/arm/src/nrf52/Kconfig b/arch/arm/src/nrf52/Kconfig
index 1fc661c883..59037539e2 100644
--- a/arch/arm/src/nrf52/Kconfig
+++ b/arch/arm/src/nrf52/Kconfig
@@ -13,11 +13,15 @@ choice
 config ARCH_CHIP_NRF52832
        bool "NRF52832"
        select ARCH_FAMILY_NRF52
+       select NRF52_MEM_FLASH_512
+       select NRF52_MEM_RAM_64
        select NRF52_HAVE_BPROT
 
 config ARCH_CHIP_NRF52833
        bool "NRF52832"
        select ARCH_FAMILY_NRF52
+       select NRF52_MEM_FLASH_512
+       select NRF52_MEM_RAM_128
        select NRF52_HAVE_ACL
        select NRF52_HAVE_UART1
        select NRF52_HAVE_PORT1
@@ -28,6 +32,8 @@ config ARCH_CHIP_NRF52833
 config ARCH_CHIP_NRF52840
        bool "NRF52840"
        select ARCH_FAMILY_NRF52
+       select NRF52_MEM_FLASH_1024
+       select NRF52_MEM_RAM_256
        select NRF52_HAVE_ACL
        select NRF52_HAVE_UART1
        select NRF52_HAVE_PORT1
@@ -45,6 +51,51 @@ config ARCH_FAMILY_NRF52
        bool
        default n
 
+# RAM size
+
+config NRF52_MEM_RAM_32
+       bool
+       default n
+
+config NRF52_MEM_RAM_64
+       bool
+       default n
+
+config NRF52_MEM_RAM_128
+       bool
+       default n
+
+config NRF52_MEM_RAM_256
+       bool
+       default n
+
+config NRF52_MEM_RAM_SIZE
+       hex
+       default 0x008000 if NRF52_MEM_RAM_32
+       default 0x010000 if NRF52_MEM_RAM_64
+       default 0x020000 if NRF52_MEM_RAM_128
+       default 0x040000 if NRF52_MEM_RAM_256
+
+# FLASH size
+
+config NRF52_MEM_FLASH_256
+       bool
+       default n
+
+config NRF52_MEM_FLASH_512
+       bool
+       default n
+
+config NRF52_MEM_FLASH_1024
+       bool
+       default n
+
+config NRF52_MEM_FLASH_SIZE
+       hex
+       default 0x040000 if NRF52_MEM_FLASH_256
+       default 0x080000 if NRF52_MEM_FLASH_512
+       default 0x100000 if NRF52_MEM_FLASH_1024
+
 # Peripheral support
 
 config NRF52_HAVE_BPROT
diff --git a/arch/arm/src/nrf52/nrf52_flash.c b/arch/arm/src/nrf52/nrf52_flash.c
index 4896f5194d..3a47bfc0ab 100644
--- a/arch/arm/src/nrf52/nrf52_flash.c
+++ b/arch/arm/src/nrf52/nrf52_flash.c
@@ -47,6 +47,7 @@
 #include <nuttx/config.h>
 #include <nuttx/arch.h>
 #include <errno.h>
+#include <debug.h>
 
 #include <nuttx/config.h>
 #include <nuttx/progmem.h>
@@ -219,7 +220,7 @@ ssize_t up_progmem_eraseblock(size_t block)
 
   if (block >= up_progmem_neraseblocks())
     {
-      _err("Wrong Page number %d.\n", page);
+      _err("Wrong block number %d.\n", block);
       return -EFAULT;
     }
 
diff --git a/boards/arm/nrf52/common/Kconfig b/boards/arm/nrf52/common/Kconfig
index ce537d0af2..cfdc83ec02 100644
--- a/boards/arm/nrf52/common/Kconfig
+++ b/boards/arm/nrf52/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 NRF52_HAVE_OTA_PARTITION
+       bool
+       default n
+
+menu "Application Image Configuration"
+
+choice
+       prompt "Application Image Format"
+       default NRF52_APP_FORMAT_LEGACY
+
+config NRF52_APP_FORMAT_LEGACY
+       bool "Legacy format"
+       ---help---
+               This is the legacy application image format.
+
+config NRF52_APP_FORMAT_MCUBOOT
+       bool "MCUboot-bootable format"
+       select NRF52_HAVE_OTA_PARTITION
+       ---help---
+               The MCUboot support of loading the firmware images.
+
+endchoice # Application Image Format
+
+config NRF52_PROGMEM_OTA_PARTITION
+       bool "MTD driver"
+       default n
+       depends on NRF52_HAVE_OTA_PARTITION
+       select BCH
+       select MTD
+       select MTD_BYTE_WRITE
+       select MTD_PARTITION
+       select MTD_PROGMEM
+       select NRF52_PROGMEM
+       ---help---
+               Initialize an MTD driver for the Flash, which will
+               add an entry at /dev for application access from userspace.
+
+if NRF52_PROGMEM_OTA_PARTITION
+
+config NRF52_MCUBOOT_HAVE_SCRACH
+       bool "Scratch partition support"
+       default n
+
+config NRF52_MCUBOOT_HEADER_SIZE
+       hex
+       default 0x200
+       depends on NRF52_APP_FORMAT_MCUBOOT
+
+config NRF52_OTA_PRIMARY_SLOT_DEVPATH
+       string "Application image primary slot device path"
+       default "/dev/ota0"
+
+config NRF52_OTA_SECONDARY_SLOT_DEVPATH
+       string "Application image secondary slot device path"
+       default "/dev/ota1"
+
+config NRF52_OTA_PRIMARY_SLOT_OFFSET
+       hex "MCUboot application image primary slot offset"
+       default "0x10000"
+
+config NRF52_OTA_SECONDARY_SLOT_OFFSET
+       hex "MCUboot application image secondary slot offset"
+       default "0x40000"
+
+config NRF52_OTA_SLOT_SIZE
+       hex "MCUboot application image slot size (in bytes)"
+       default "0x30000"
+
+if NRF52_MCUBOOT_HAVE_SCRACH
+
+config NRF52_OTA_SCRATCH_DEVPATH
+       string "Scratch partition device path"
+       default "/dev/otascratch"
+
+config NRF52_OTA_SCRATCH_OFFSET
+       hex "MCUboot scratch partition offset"
+       default "0x70000"
+
+config NRF52_OTA_SCRATCH_SIZE
+       hex "MCUboot scratch partition size (in bytes)"
+       default "0x10000"
+
+endif # NRF52_MCUBOOT_HAVE_SCRACH
+
+endif # NRF52_PROGMEM_OTA_PARTITION
+
+endmenu # Application Image Configuration
diff --git a/boards/arm/nrf52/common/include/nrf52_progmem.h 
b/boards/arm/nrf52/common/include/nrf52_progmem.h
new file mode 100644
index 0000000000..a26d211f98
--- /dev/null
+++ b/boards/arm/nrf52/common/include/nrf52_progmem.h
@@ -0,0 +1,46 @@
+/****************************************************************************
+ * boards/arm/nrf52/common/include/nrf52_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_NRF52_COMMON_INCLUDE_NRF52_PROGMEM_H
+#define __BOARDS_ARM_NRF52_COMMON_INCLUDE_NRF52_PROGMEM_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+/****************************************************************************
+ * Public Functions Prototypes
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: nrf52_mtd_initialize
+ *
+ * Description:
+ *   Initialize MTD drivers.
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_NRF52_PROGMEM
+int nrf52_progmem_init(void);
+#endif
+
+#endif /* __BOARDS_ARM_NRF52_COMMON_INCLUDE_NRF52_PROGMEM_H */
diff --git a/boards/arm/nrf52/common/scripts/flash_config.ld 
b/boards/arm/nrf52/common/scripts/flash_config.ld
new file mode 100644
index 0000000000..ae86868fe8
--- /dev/null
+++ b/boards/arm/nrf52/common/scripts/flash_config.ld
@@ -0,0 +1,127 @@
+/****************************************************************************
+ * boards/arm/nrf52/common/scripts/flash_config.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_START_ADDR        0x00000000
+#define RAM_START_ADDR          0x20000000
+
+#ifdef CONFIG_NRF52_APP_FORMAT_MCUBOOT
+#  define MCUBOOT_HEADER_SIZE   CONFIG_NRF52_MCUBOOT_HEADER_SIZE
+#  ifdef CONFIG_BOARDCTL_BOOT_IMAGE
+#    define FLASH_OFFSET        0
+#    define FLASH_IMG_SIZE      CONFIG_NRF52_OTA_PRIMARY_SLOT_OFFSET
+#  else
+#    define FLASH_OFFSET        (CONFIG_NRF52_OTA_PRIMARY_SLOT_OFFSET + 
MCUBOOT_HEADER_SIZE)
+#    define FLASH_IMG_SIZE      (CONFIG_NRF52_OTA_SLOT_SIZE - 
MCUBOOT_HEADER_SIZE)
+#  endif
+#else
+#  define FLASH_OFFSET          0
+#  define FLASH_IMG_SIZE        CONFIG_NRF52_MEM_FLASH_SIZE
+#endif
+
+MEMORY
+{
+    progmem (rx)  : ORIGIN = FLASH_START_ADDR + FLASH_OFFSET,
+                    LENGTH = FLASH_IMG_SIZE
+    datamem (rwx) : ORIGIN = RAM_START_ADDR,
+                    LENGTH = CONFIG_NRF52_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/nrf52/common/src/Make.defs 
b/boards/arm/nrf52/common/src/Make.defs
index 22ba399142..43a03d4d85 100644
--- a/boards/arm/nrf52/common/src/Make.defs
+++ b/boards/arm/nrf52/common/src/Make.defs
@@ -24,6 +24,14 @@ ifeq ($(CONFIG_NRF52_TIMER),y)
   CSRCS += nrf52_timer.c
 endif
 
+ifeq ($(CONFIG_BOARDCTL_BOOT_IMAGE),y)
+  CSRCS += nrf52_boot_image.c
+endif
+
+ifeq ($(CONFIG_NRF52_PROGMEM),y)
+  CSRCS += nrf52_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/nrf52/common/src/nrf52_boot_image.c 
b/boards/arm/nrf52/common/src/nrf52_boot_image.c
new file mode 100644
index 0000000000..c102ea1039
--- /dev/null
+++ b/boards/arm/nrf52/common/src/nrf52_boot_image.c
@@ -0,0 +1,170 @@
+/****************************************************************************
+ * boards/arm/nrf52/common/src/nrf52_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/nrf52/common/src/nrf52_progmem.c 
b/boards/arm/nrf52/common/src/nrf52_progmem.c
new file mode 100644
index 0000000000..de7ce8e66b
--- /dev/null
+++ b/boards/arm/nrf52/common/src/nrf52_progmem.c
@@ -0,0 +1,244 @@
+/****************************************************************************
+ * boards/arm/nrf52/common/src/nrf52_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 "nrf52_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_NRF52_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_NRF52_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_NRF52_PROGMEM_OTA_PARTITION
+static const struct ota_partition_s g_ota_partition_table[] =
+{
+  {
+    .offset  = CONFIG_NRF52_OTA_PRIMARY_SLOT_OFFSET,
+    .size    = CONFIG_NRF52_OTA_SLOT_SIZE,
+    .devpath = CONFIG_NRF52_OTA_PRIMARY_SLOT_DEVPATH
+  },
+  {
+    .offset  = CONFIG_NRF52_OTA_SECONDARY_SLOT_OFFSET,
+    .size    = CONFIG_NRF52_OTA_SLOT_SIZE,
+    .devpath = CONFIG_NRF52_OTA_SECONDARY_SLOT_DEVPATH
+  },
+#ifdef CONFIG_NRF52_MCUBOOT_HAVE_SCRACH
+  {
+    .offset  = CONFIG_NRF52_OTA_SCRATCH_OFFSET,
+    .size    = CONFIG_NRF52_OTA_SCRATCH_SIZE,
+    .devpath = CONFIG_NRF52_OTA_SCRATCH_DEVPATH
+  }
+#endif
+};
+#endif
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+#ifdef CONFIG_NRF52_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_NRF52_PROGMEM_OTA_PARTITION */
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: nrf52_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 nrf52_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_NRF52_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;
+}

Reply via email to