diff --git a/arch/arm/src/nrf91/Kconfig b/arch/arm/src/nrf91/Kconfig index 878e285758..ca0b5854fe 100644 --- a/arch/arm/src/nrf91/Kconfig +++ b/arch/arm/src/nrf91/Kconfig @@ -17,6 +17,26 @@ config ARCH_CHIP_NRF9160 endchoice # NRF91 Chip Selection +# RAM size + +config NRF91_CPUAPP_MEM_RAM_256 + bool + default y + +config NRF91_CPUAPP_MEM_RAM_SIZE + hex + default 0x040000 if NRF91_CPUAPP_MEM_RAM_256 + +# FLASH size + +config NRF91_CPUAPP_MEM_FLASH_1024 + bool + default y + +config NRF91_CPUAPP_MEM_FLASH_SIZE + hex + default 0x100000 if NRF91_CPUAPP_MEM_FLASH_1024 + config NRF91_ENABLE_APPROTECT bool "nRF91 enable APPROTECT" default n diff --git a/arch/arm/src/nrf91/nrf91_flash.c b/arch/arm/src/nrf91/nrf91_flash.c new file mode 100644 index 0000000000..8e4cfdb1e7 --- /dev/null +++ b/arch/arm/src/nrf91/nrf91_flash.c @@ -0,0 +1,399 @@ +/**************************************************************************** + * arch/arm/src/nrf91/nrf91_flash.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 + +#include + +#include +#include + +#include "arm_internal.h" +#include "barriers.h" + +#include "hardware/nrf91_ficr.h" +#include "hardware/nrf91_nvmc.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define MEMORY_SYNC() ARM_ISB(); ARM_DSB() + +/* Sizes and masks */ + +#define NRF91_FLASH_ERASEDVAL (0xff) + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +static inline uint32_t nrf91_get_flash_size(void) +{ + return getreg32(NRF91_FICR_INFO_FLASH) * 1024; +} + +static inline uint32_t nrf91_get_page_size(void) +{ + return getreg32(NRF91_FICR_INFO_CODEPAGESIZE); +} + +static inline uint32_t nrf91_get_pages_num(void) +{ + return getreg32(NRF91_FICR_INFO_CODESIZE); +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: up_progmem_pagesize + * + * Description: + * Return page size + * + ****************************************************************************/ + +size_t up_progmem_pagesize(size_t page) +{ + if (page >= up_progmem_neraseblocks()) + { + return 0; + } + else + { + return nrf91_get_page_size(); + } +} + +/**************************************************************************** + * Name: up_progmem_erasesize + * + * Description: + * Return erase block size + * + ****************************************************************************/ + +size_t up_progmem_erasesize(size_t block) +{ + return up_progmem_pagesize(block); +} + +/**************************************************************************** + * Name: up_progmem_getpage + * + * Description: + * Address to page conversion + * + * Input Parameters: + * addr - Address to be converted + * + * Returned Value: + * Page or negative value on error. The following errors are reported + * (errno is not set!): + * + * -EFAULT: On invalid address + * + ****************************************************************************/ + +ssize_t up_progmem_getpage(size_t addr) +{ + if (addr >= nrf91_get_flash_size()) + { + return -EFAULT; + } + + return addr / nrf91_get_page_size(); +} + +/**************************************************************************** + * Name: up_progmem_getaddress + * + * Description: + * Page to address conversion + * + * Input Parameters: + * page - Page to be converted + * + * Returned Value: + * Base address of given page, maximum size if page is not valid. + * + ****************************************************************************/ + +size_t up_progmem_getaddress(size_t page) +{ + if (page >= up_progmem_neraseblocks()) + { + return SIZE_MAX; + } + + return page * nrf91_get_page_size(); +} + +/**************************************************************************** + * Name: up_progmem_neraseblocks + * + * Description: + * Return number of erase blocks in the available FLASH memory. + * + ****************************************************************************/ + +size_t up_progmem_neraseblocks(void) +{ + return nrf91_get_flash_size() / nrf91_get_page_size(); +} + +/**************************************************************************** + * Name: up_progmem_isuniform + * + * Description: + * Page size is uniform? Say 'yes' even though that is not strictly + * true. + * + ****************************************************************************/ + +bool up_progmem_isuniform(void) +{ + return true; +} + +/**************************************************************************** + * Name: up_progmem_eraseblock + * + * Description: + * Erase selected block. + * + * Input Parameters: + * block - Block to be erased + * + * Returned Value: + * Page size or negative value on error. The following errors are reported + * (errno is not set!): + * + * -EFAULT: On invalid page + * -EIO: On unsuccessful erase + * -EROFS: On access to write protected area + * -EACCES: Insufficient permissions (read/write protected) + * -EPERM: If operation is not permitted due to some other constraints + * (i.e. some internal block is not running etc.) + * + ****************************************************************************/ + +ssize_t up_progmem_eraseblock(size_t block) +{ + size_t page_address; + + if (block >= up_progmem_neraseblocks()) + { + return -EFAULT; + } + + page_address = up_progmem_getaddress(block); + + /* Enable erase mode */ + + putreg32(NVMC_CONFIG_EEN, NRF91_NVMC_CONFIG); + + /* Memory sync */ + + MEMORY_SYNC(); + + /* Erase the page by writting 0xffffffff into the first 32-bit word of + * the flash page + */ + + putreg32(0xffffffff, page_address); + + /* Wait for flash */ + + while (!(getreg32(NRF91_NVMC_READY) & NVMC_READY_READY)) + { + } + + /* Read only access */ + + putreg32(NVMC_CONFIG_REN, NRF91_NVMC_CONFIG); + + /* Memory sync */ + + MEMORY_SYNC(); + + /* Verify */ + + if (up_progmem_ispageerased(block) == 0) + { + return up_progmem_erasesize(block); + } + else + { + return -EIO; + } +} + +/**************************************************************************** + * Name: up_progmem_ispageerased + * + * Description: + * Checks whether a page is erased + * + * Input Parameters: + * page - Page to be checked + * + * Returned Value: + * Returns number of bytes erased or negative value on error. If it + * returns zero then complete page is empty (erased). + * + * The following errors are reported (errno is not set!) + * -EFAULT: On invalid page + * + ****************************************************************************/ + +ssize_t up_progmem_ispageerased(size_t page) +{ + size_t addr; + size_t count; + size_t bwritten = 0; + + if (page >= nrf91_get_pages_num()) + { + return -EFAULT; + } + + /* Verify */ + + for (addr = up_progmem_getaddress(page), count = up_progmem_pagesize(page); + count; count--, addr++) + { + if (getreg8(addr) != NRF91_FLASH_ERASEDVAL) + { + bwritten++; + } + } + + return bwritten; +} + +/**************************************************************************** + * Name: up_progmem_write + * + * Description: + * Program data at given address + * + * Input Parameters: + * addr - Address with or without flash offset + * buf - Pointer to buffer + * count - Number of bytes to write + * + * Returned Value: + * Bytes written or negative value on error. The following errors are + * reported (errno is not set!) + * + * EINVAL: If buflen is not aligned with the flash boundaries (i.e. + * some MCU's require per half-word or even word access) + * EFAULT: On invalid address + * EIO: On unsuccessful write + * EROFS: On access to write protected area + * EACCES: Insufficient permissions (read/write protected) + * EPERM: If operation is not permitted due to some other constraints + * (i.e. some internal block is not running etc.) + * + ****************************************************************************/ + +ssize_t up_progmem_write(size_t addr, const void *buf, size_t count) +{ + uint32_t *pword = (uint32_t *)buf; + size_t written = count; + + /* NRF91 requires word access */ + + if (count & 0x3) + { + return -EINVAL; + } + + /* Check for valid address range */ + + if ((addr + count) > nrf91_get_flash_size()) + { + return -EFAULT; + } + + /* Flash offset */ + + addr += NRF91_FLASH_BASE; + + /* Begin flashing */ + + for (; count; count -= 4, pword++, addr += 4) + { + /* Enable write */ + + putreg32(NVMC_CONFIG_WEN, NRF91_NVMC_CONFIG); + + /* Memory sync */ + + MEMORY_SYNC(); + + /* Write the word */ + + *(uint32_t *)addr = *pword; + + /* Wait for flash */ + + while (!(getreg32(NRF91_NVMC_READY) & NVMC_READY_READY)) + { + } + + /* Read only access */ + + putreg32(NVMC_CONFIG_REN, NRF91_NVMC_CONFIG); + + /* Memory sync */ + + MEMORY_SYNC(); + + /* Verify */ + + if (getreg32(addr) != *pword) + { + return -EIO; + } + } + + return written; +} + +/**************************************************************************** + * Name: up_progmem_erasestate + * + * Description: + * Return value of erase state. + * + ****************************************************************************/ + +uint8_t up_progmem_erasestate(void) +{ + return NRF91_FLASH_ERASEDVAL; +} diff --git a/boards/arm/nrf91/common/Kconfig b/boards/arm/nrf91/common/Kconfig index f72f3c094c..94be8f9830 100644 --- a/boards/arm/nrf91/common/Kconfig +++ b/boards/arm/nrf91/common/Kconfig @@ -2,3 +2,91 @@ # For a description of the syntax of this configuration file, # see the file kconfig-language.txt in the NuttX tools repository. # + +config NRF91_HAVE_OTA_PARTITION + bool + default n + +menu "Application Image Configuration" + +choice + prompt "Application Image Format" + default NRF91_APP_FORMAT_LEGACY + +config NRF91_APP_FORMAT_LEGACY + bool "Legacy format" + ---help--- + This is the legacy application image format. + +config NRF91_APP_FORMAT_MCUBOOT + bool "MCUboot-bootable format" + select NRF91_HAVE_OTA_PARTITION + ---help--- + The MCUboot support of loading the firmware images. + +endchoice # Application Image Format + +config NRF91_PROGMEM_OTA_PARTITION + bool "MTD driver" + default n + depends on NRF91_HAVE_OTA_PARTITION + select BCH + select MTD + select MTD_BYTE_WRITE + select MTD_PARTITION + select MTD_PROGMEM + select NRF91_PROGMEM + ---help--- + Initialize an MTD driver for the Flash, which will + add an entry at /dev for application access from userspace. + +if NRF91_PROGMEM_OTA_PARTITION + +config NRF91_MCUBOOT_HAVE_SCRACH + bool "Scratch partition support" + default n + +config NRF91_MCUBOOT_HEADER_SIZE + hex + default 0x200 + depends on NRF91_APP_FORMAT_MCUBOOT + +config NRF91_OTA_PRIMARY_SLOT_DEVPATH + string "Application image primary slot device path" + default "/dev/ota0" + +config NRF91_OTA_SECONDARY_SLOT_DEVPATH + string "Application image secondary slot device path" + default "/dev/ota1" + +config NRF91_OTA_PRIMARY_SLOT_OFFSET + hex "MCUboot application image primary slot offset" + default "0x10000" + +config NRF91_OTA_SECONDARY_SLOT_OFFSET + hex "MCUboot application image secondary slot offset" + default "0x80000" + +config NRF91_OTA_SLOT_SIZE + hex "MCUboot application image slot size (in bytes)" + default "0x70000" + +if NRF91_MCUBOOT_HAVE_SCRACH + +config NRF91_OTA_SCRATCH_DEVPATH + string "Scratch partition device path" + default "/dev/otascratch" + +config NRF91_OTA_SCRATCH_OFFSET + hex "MCUboot scratch partition offset" + default "0xf0000" + +config NRF91_OTA_SCRATCH_SIZE + hex "MCUboot scratch partition size (in bytes)" + default "0x10000" + +endif # NRF91_MCUBOOT_HAVE_SCRACH + +endif # NRF91_PROGMEM_OTA_PARTITION + +endmenu # Application Image Configuration diff --git a/boards/arm/nrf91/common/include/nrf91_progmem.h b/boards/arm/nrf91/common/include/nrf91_progmem.h new file mode 100644 index 0000000000..6f0885eca8 --- /dev/null +++ b/boards/arm/nrf91/common/include/nrf91_progmem.h @@ -0,0 +1,46 @@ +/**************************************************************************** + * boards/arm/nrf91/common/include/nrf91_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_NRF91_COMMON_INCLUDE_NRF91_PROGMEM_H +#define __BOARDS_ARM_NRF91_COMMON_INCLUDE_NRF91_PROGMEM_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +/**************************************************************************** + * Public Functions Prototypes + ****************************************************************************/ + +/**************************************************************************** + * Name: nrf91_mtd_initialize + * + * Description: + * Initialize MTD drivers. + * + ****************************************************************************/ + +#ifdef CONFIG_NRF91_PROGMEM +int nrf91_progmem_init(void); +#endif + +#endif /* __BOARDS_ARM_NRF91_COMMON_INCLUDE_NRF91_PROGMEM_H */ diff --git a/boards/arm/nrf91/common/scripts/flash_app.ld b/boards/arm/nrf91/common/scripts/flash_app.ld new file mode 100644 index 0000000000..746e7f172d --- /dev/null +++ b/boards/arm/nrf91/common/scripts/flash_app.ld @@ -0,0 +1,139 @@ +/**************************************************************************** + * boards/arm/nrf91/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 + +/* Shared memory configuration */ + +#ifdef CONFIG_NRF91_MODEM_SHMEM_SIZE +# define NRF91_SHMEM_SIZE (CONFIG_NRF91_MODEM_SHMEM_SIZE) +#else +# define NRF91_SHMEM_SIZE (0) +#endif + +/* FLASH and RAM start */ + +#define FLASH_CPUAPP_START_ADDR (0x00000000) +#define RAM_CPUAPP_START_ADDR (0x20000000 + NRF91_SHMEM_SIZE) + +/* Image format configuration */ + +#ifdef CONFIG_NRF91_APP_FORMAT_MCUBOOT +# define MCUBOOT_HEADER_SIZE CONFIG_NRF91_MCUBOOT_HEADER_SIZE +# ifdef CONFIG_BOARDCTL_BOOT_IMAGE +# define FLASH_OFFSET 0 +# define FLASH_IMG_SIZE CONFIG_NRF91_OTA_PRIMARY_SLOT_OFFSET +# else +# define FLASH_OFFSET (CONFIG_NRF91_OTA_PRIMARY_SLOT_OFFSET + MCUBOOT_HEADER_SIZE) +# define FLASH_IMG_SIZE (CONFIG_NRF91_OTA_SLOT_SIZE - MCUBOOT_HEADER_SIZE) +# endif +#else +# define FLASH_OFFSET 0 +# define FLASH_IMG_SIZE CONFIG_NRF91_CPUAPP_MEM_FLASH_SIZE +#endif + +MEMORY +{ + progmem (rx) : ORIGIN = FLASH_CPUAPP_START_ADDR + FLASH_OFFSET, + LENGTH = FLASH_IMG_SIZE + datamem (rwx) : ORIGIN = RAM_CPUAPP_START_ADDR, + LENGTH = CONFIG_NRF91_CPUAPP_MEM_RAM_SIZE - NRF91_SHMEM_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/nrf91/common/src/Make.defs b/boards/arm/nrf91/common/src/Make.defs index 8f0f1edeb7..21036b1bb5 100644 --- a/boards/arm/nrf91/common/src/Make.defs +++ b/boards/arm/nrf91/common/src/Make.defs @@ -24,6 +24,14 @@ ifeq ($(CONFIG_NRF91_TIMER),y) CSRCS += nrf91_timer.c endif +ifeq ($(CONFIG_BOARDCTL_BOOT_IMAGE),y) + CSRCS += nrf91_boot_image.c +endif + +ifeq ($(CONFIG_NRF91_PROGMEM),y) + CSRCS += nrf91_progmem.c +endif + DEPPATH += --dep-path src VPATH += :src CFLAGS += ${INCDIR_PREFIX}$(TOPDIR)$(DELIM)arch$(DELIM)$(CONFIG_ARCH)$(DELIM)src$(DELIM)board$(DELIM)src diff --git a/boards/arm/nrf91/common/src/nrf91_boot_image.c b/boards/arm/nrf91/common/src/nrf91_boot_image.c new file mode 100644 index 0000000000..19f43890e3 --- /dev/null +++ b/boards/arm/nrf91/common/src/nrf91_boot_image.c @@ -0,0 +1,170 @@ +/**************************************************************************** + * boards/arm/nrf91/common/src/nrf91_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 + +#include +#include +#include + +#include +#include +#include + +#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/nrf91/common/src/nrf91_progmem.c b/boards/arm/nrf91/common/src/nrf91_progmem.c new file mode 100644 index 0000000000..ab81955990 --- /dev/null +++ b/boards/arm/nrf91/common/src/nrf91_progmem.c @@ -0,0 +1,244 @@ +/**************************************************************************** + * boards/arm/nrf91/common/src/nrf91_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 + +#include +#include + +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#include "nrf91_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_NRF91_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_NRF91_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_NRF91_PROGMEM_OTA_PARTITION +static const struct ota_partition_s g_ota_partition_table[] = +{ + { + .offset = CONFIG_NRF91_OTA_PRIMARY_SLOT_OFFSET, + .size = CONFIG_NRF91_OTA_SLOT_SIZE, + .devpath = CONFIG_NRF91_OTA_PRIMARY_SLOT_DEVPATH + }, + { + .offset = CONFIG_NRF91_OTA_SECONDARY_SLOT_OFFSET, + .size = CONFIG_NRF91_OTA_SLOT_SIZE, + .devpath = CONFIG_NRF91_OTA_SECONDARY_SLOT_DEVPATH + }, +#ifdef CONFIG_NRF91_MCUBOOT_HAVE_SCRACH + { + .offset = CONFIG_NRF91_OTA_SCRATCH_OFFSET, + .size = CONFIG_NRF91_OTA_SCRATCH_SIZE, + .devpath = CONFIG_NRF91_OTA_SCRATCH_DEVPATH + } +#endif +}; +#endif + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +#ifdef CONFIG_NRF91_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_NRF91_PROGMEM_OTA_PARTITION */ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: nrf91_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 nrf91_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_NRF91_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; +} diff --git a/boards/arm/nrf91/nrf9160-dk/configs/mcuboot_app/defconfig b/boards/arm/nrf91/nrf9160-dk/configs/mcuboot_app/defconfig new file mode 100644 index 0000000000..b239a1a658 --- /dev/null +++ b/boards/arm/nrf91/nrf9160-dk/configs/mcuboot_app/defconfig @@ -0,0 +1,52 @@ +# +# This file is autogenerated: PLEASE DO NOT EDIT IT. +# +# You can use "make menuconfig" to make any modifications to the installed .config file. +# You can then do "make savedefconfig" to generate a new defconfig file that includes your +# modifications. +# +# CONFIG_NSH_DISABLE_IFCONFIG is not set +# CONFIG_NSH_DISABLE_PS is not set +# CONFIG_STANDARD_SERIAL is not set +CONFIG_ARCH="arm" +CONFIG_ARCH_BOARD="nrf9160-dk" +CONFIG_ARCH_BOARD_COMMON=y +CONFIG_ARCH_BOARD_NRF9160_DK=y +CONFIG_ARCH_CHIP="nrf91" +CONFIG_ARCH_CHIP_NRF9160=y +CONFIG_ARCH_CHIP_NRF91=y +CONFIG_ARCH_STACKDUMP=y +CONFIG_ARCH_STDARG_H=y +CONFIG_BOARD_LOOPSPERMSEC=5500 +CONFIG_BUILTIN=y +CONFIG_EXAMPLES_HELLO=y +CONFIG_EXAMPLES_MCUBOOT_SLOT_CONFIRM=y +CONFIG_EXAMPLES_MCUBOOT_SWAP_TEST=y +CONFIG_EXPERIMENTAL=y +CONFIG_FAT_LCNAMES=y +CONFIG_FAT_LFN=y +CONFIG_FS_FAT=y +CONFIG_INIT_ENTRYPOINT="nsh_main" +CONFIG_INTELHEX_BINARY=y +CONFIG_MM_REGIONS=2 +CONFIG_NRF91_APP_FORMAT_MCUBOOT=y +CONFIG_NRF91_MCUBOOT_HAVE_SCRACH=y +CONFIG_NRF91_PROGMEM_OTA_PARTITION=y +CONFIG_NRF91_UART0=y +CONFIG_NSH_ARCHINIT=y +CONFIG_NSH_BUILTIN_APPS=y +CONFIG_NSH_FILEIOSIZE=512 +CONFIG_NSH_LINELEN=64 +CONFIG_NSH_READLINE=y +CONFIG_PREALLOC_TIMERS=4 +CONFIG_RAM_SIZE=262144 +CONFIG_RAM_START=0x20000000 +CONFIG_RAW_BINARY=y +CONFIG_RR_INTERVAL=200 +CONFIG_SCHED_WAITPID=y +CONFIG_START_DAY=26 +CONFIG_START_MONTH=3 +CONFIG_SYMTAB_ORDEREDBYNAME=y +CONFIG_SYSTEM_NSH=y +CONFIG_TASK_NAME_SIZE=0 +CONFIG_UART0_SERIAL_CONSOLE=y diff --git a/boards/arm/nrf91/nrf9160-dk/configs/mcuboot_loader/defconfig b/boards/arm/nrf91/nrf9160-dk/configs/mcuboot_loader/defconfig new file mode 100644 index 0000000000..55c79a8bbb --- /dev/null +++ b/boards/arm/nrf91/nrf9160-dk/configs/mcuboot_loader/defconfig @@ -0,0 +1,42 @@ +# +# This file is autogenerated: PLEASE DO NOT EDIT IT. +# +# You can use "make menuconfig" to make any modifications to the installed .config file. +# You can then do "make savedefconfig" to generate a new defconfig file that includes your +# modifications. +# +# CONFIG_STANDARD_SERIAL is not set +CONFIG_ARCH="arm" +CONFIG_ARCH_BOARD="nrf9160-dk" +CONFIG_ARCH_BOARD_COMMON=y +CONFIG_ARCH_BOARD_NRF9160_DK=y +CONFIG_ARCH_CHIP="nrf91" +CONFIG_ARCH_CHIP_NRF9160=y +CONFIG_ARCH_CHIP_NRF91=y +CONFIG_ARCH_STACKDUMP=y +CONFIG_ARCH_STDARG_H=y +CONFIG_BOARD_LOOPSPERMSEC=5500 +CONFIG_BOOT_MCUBOOT=y +CONFIG_EXPERIMENTAL=y +CONFIG_FAT_LCNAMES=y +CONFIG_FAT_LFN=y +CONFIG_FS_FAT=y +CONFIG_INIT_ENTRYPOINT="mcuboot_loader_main" +CONFIG_INTELHEX_BINARY=y +CONFIG_MCUBOOT_BOOTLOADER=y +CONFIG_MM_REGIONS=2 +CONFIG_NRF91_APP_FORMAT_MCUBOOT=y +CONFIG_NRF91_MCUBOOT_HAVE_SCRACH=y +CONFIG_NRF91_PROGMEM_OTA_PARTITION=y +CONFIG_NRF91_UART0=y +CONFIG_PREALLOC_TIMERS=4 +CONFIG_RAM_SIZE=65536 +CONFIG_RAM_START=0x20000000 +CONFIG_RAW_BINARY=y +CONFIG_RR_INTERVAL=200 +CONFIG_SCHED_WAITPID=y +CONFIG_START_DAY=26 +CONFIG_START_MONTH=3 +CONFIG_SYMTAB_ORDEREDBYNAME=y +CONFIG_TASK_NAME_SIZE=0 +CONFIG_UART0_SERIAL_CONSOLE=y diff --git a/boards/arm/nrf91/nrf9160-dk/scripts/Make.defs b/boards/arm/nrf91/nrf9160-dk/scripts/Make.defs index 28fd4640d1..57c0e774a6 100644 --- a/boards/arm/nrf91/nrf9160-dk/scripts/Make.defs +++ b/boards/arm/nrf91/nrf9160-dk/scripts/Make.defs @@ -22,7 +22,11 @@ include $(TOPDIR)/.config include $(TOPDIR)/tools/Config.mk include $(TOPDIR)/arch/arm/src/armv8-m/Toolchain.defs -ARCHSCRIPT += $(BOARD_DIR)$(DELIM)scripts$(DELIM)flash_app.ld +ifeq ($(CONFIG_ARCH_BOARD_COMMON),y) + ARCHSCRIPT += $(BOARD_COMMON_DIR)$(DELIM)scripts$(DELIM)flash_app.ld +else + ARCHSCRIPT += $(BOARD_DIR)$(DELIM)scripts$(DELIM)flash_app.ld +endif ARCHPICFLAGS = -fpic -msingle-pic-base -mpic-register=r10 diff --git a/boards/arm/nrf91/nrf9160-dk/src/nrf91_bringup.c b/boards/arm/nrf91/nrf9160-dk/src/nrf91_bringup.c index b8ddd51684..6af65629d7 100644 --- a/boards/arm/nrf91/nrf9160-dk/src/nrf91_bringup.c +++ b/boards/arm/nrf91/nrf9160-dk/src/nrf91_bringup.c @@ -46,6 +46,10 @@ # include "nrf91_modem_at.h" #endif +#ifdef CONFIG_NRF91_PROGMEM +# include "nrf91_progmem.h" +#endif + #include "nrf9160-dk.h" /**************************************************************************** @@ -131,6 +135,14 @@ int nrf91_bringup(void) } #endif +#ifdef CONFIG_NRF91_PROGMEM + ret = nrf91_progmem_init(); + if (ret < 0) + { + syslog(LOG_ERR, "ERROR: Failed to initialize MTD progmem: %d\n", ret); + } +#endif /* CONFIG_MTD */ + UNUSED(ret); return OK; }