Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions boot/bootutil/src/image_rsa.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
* under the License.
*/

#include <string.h>

#include "syscfg/syscfg.h"

#if MYNEWT_VAL(BOOTUTIL_SIGN_RSA)
Expand Down
2 changes: 2 additions & 0 deletions boot/zephyr/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
subdir-ccflags-y += -DMCUBOOT_TARGET_CONFIG='"$(BOARD).h"'
subdir-ccflags-y += -I$(PROJECT)/boot/bootutil/include
subdir-ccflags-y += -I$(PROJECT)/boot/zephyr/include
subdir-ccflags-y += -I$(PROJECT)/boot/zephyr/targets

obj-y += main.o
obj-y += flash_map.o hal_flash.o os.o
Expand Down
43 changes: 23 additions & 20 deletions boot/zephyr/flash_map.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,18 @@
*/

#include <zephyr.h>
#include <misc/printk.h>
#include <flash.h>

#include MCUBOOT_TARGET_CONFIG

#include <flash_map/flash_map.h>
#include <hal/hal_flash.h>
#include <sysflash/sysflash.h>

#define SYS_LOG_DOMAIN "BOOTLOADER"
#define SYS_LOG_LEVEL SYS_LOG_LEVEL_INFO
#include <logging/sys_log.h>

extern struct device *boot_flash_device;

/*
Expand All @@ -34,35 +39,30 @@ extern struct device *boot_flash_device;
static const struct flash_area part_map[] = {
{
.fa_id = FLASH_AREA_IMAGE_0,
.fa_off = 0x20000,
.fa_size = 0x20000,
.fa_off = FLASH_AREA_IMAGE_0_OFFSET,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is good for now. It is likely that we will bring in the partition table code from mynewt into mcuboot, which will make all of this quite different, though.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I just wanted to improve it a bit enough to remove the hardcoded values. The best to do here is parsing mynewt's partition table, to avoid duplicating info.

.fa_size = FLASH_AREA_IMAGE_0_SIZE,
},
{
.fa_id = FLASH_AREA_IMAGE_1,
.fa_off = 0x40000,
.fa_size = 0x20000,
.fa_off = FLASH_AREA_IMAGE_1_OFFSET,
.fa_size = FLASH_AREA_IMAGE_1_SIZE,
},
{
.fa_id = FLASH_AREA_IMAGE_SCRATCH,
.fa_off = 0x60000,
.fa_size = 0x20000,
.fa_off = FLASH_AREA_IMAGE_SCRATCH_OFFSET,
.fa_size = FLASH_AREA_IMAGE_SCRATCH_SIZE,
},
};

/*
* The K64F has a simple 1MB of uniform 4KB sectors. Initially, we'll
* use the same partition layout as the Carbon board to make
* development easier.
*/

/*
* `open` a flash area. The `area` in this case is not the individual
* sectors, but describes the particular flash area in question.
*/
int flash_area_open(uint8_t id, const struct flash_area **area)
{
int i;
printk("%s: area %d\n", __func__, id);

SYS_LOG_DBG("%s: area %d", __func__, id);

for (i = 0; i < ARRAY_SIZE(part_map); i++) {
if (id == part_map[i].fa_id)
Expand All @@ -85,20 +85,23 @@ void flash_area_close(const struct flash_area *area)
int flash_area_read(const struct flash_area *area, uint32_t off, void *dst,
uint32_t len)
{
// printk("%s: area=%d, off=%x, len=%x\n", __func__, area->fa_id, off, len);
SYS_LOG_DBG("%s: area=%d, off=%x, len=%x", __func__,
area->fa_id, off, len);
return flash_read(boot_flash_device, area->fa_off + off, dst, len);
}

int flash_area_write(const struct flash_area *area, uint32_t off, const void *src,
uint32_t len)
{
printk("%s: area=%d, off=%x, len=%x\n", __func__, area->fa_id, off, len);
SYS_LOG_DBG("%s: area=%d, off=%x, len=%x", __func__,
area->fa_id, off, len);
return flash_write(boot_flash_device, area->fa_off + off, src, len);
}

int flash_area_erase(const struct flash_area *area, uint32_t off, uint32_t len)
{
printk("%s: area=%d, off=%x, len=%x\n", __func__, area->fa_id, off, len);
SYS_LOG_DBG("%s: area=%d, off=%x, len=%x", __func__,
area->fa_id, off, len);
return flash_erase(boot_flash_device, area->fa_off + off, len);
}

Expand Down Expand Up @@ -126,7 +129,7 @@ int flash_area_to_sectors(int idx, int *cnt, struct flash_area *ret)
{
uint32_t off;

printk("%s: lookup area %d\n", __func__, idx);
SYS_LOG_DBG("%s: lookup area %d", __func__, idx);
/*
* This simple layout has uniform slots, so just fill in the
* right one.
Expand All @@ -137,13 +140,13 @@ int flash_area_to_sectors(int idx, int *cnt, struct flash_area *ret)
if (*cnt < 1)
return -1;

off = (idx - FLASH_AREA_IMAGE_0 + 1) * 0x20000;
off = (idx - FLASH_AREA_IMAGE_0 + 1) * FLASH_AREA_IMAGE_0_OFFSET;

ret->fa_id = idx;
ret->fa_device_id = 0;
ret->pad16 = 0;
ret->fa_off = off;
ret->fa_size = 0x20000;
ret->fa_size = FLASH_AREA_IMAGE_0_SIZE;

return 0;
}
11 changes: 2 additions & 9 deletions boot/zephyr/hal_flash.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,10 @@
*/

#include <zephyr.h>
#include <misc/printk.h>

#include "hal/hal_flash.h"
#include MCUBOOT_TARGET_CONFIG

#if defined(CONFIG_BOARD_FRDM_K64F)
#define FLASH_ALIGN 8
#elif defined(CONFIG_BOARD_96B_CARBON)
#define FLASH_ALIGN 1
#else
#error "Board is currently not supported by bootloader"
#endif
#include "hal/hal_flash.h"

/* All of the currently supported devices allow single byte writes. */
uint8_t hal_flash_align(uint8_t flash_id)
Expand Down
30 changes: 18 additions & 12 deletions boot/zephyr/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,22 @@
*/

#include <zephyr.h>
#include <misc/printk.h>
#include <flash.h>
#include <asm_inline.h>

#include "bootutil/image.h"
#include "bootutil/bootutil.h"
#define SYS_LOG_DOMAIN "BOOTLOADER"
#define SYS_LOG_LEVEL SYS_LOG_LEVEL_INFO
#include <logging/sys_log.h>

#if defined(CONFIG_BOARD_FRDM_K64F)
#define BOOT_FLASH "KSDK_FLASH"
#elif defined(CONFIG_BOARD_96B_CARBON)
#define BOOT_FLASH "STM32F4_FLASH"
#if defined(MCUBOOT_TARGET_CONFIG)
#include MCUBOOT_TARGET_CONFIG
#else
#error "Board is currently not supported by bootloader"
#endif

#include "bootutil/image.h"
#include "bootutil/bootutil.h"

struct device *boot_flash_device;

struct vector_table {
Expand All @@ -45,34 +46,39 @@ void main(void)
struct vector_table *vt;
int rc;

SYS_LOG_INF("Starting bootloader");

os_heap_init();

boot_flash_device = device_get_binding(BOOT_FLASH);
boot_flash_device = device_get_binding(FLASH_DRIVER_NAME);
if (!boot_flash_device) {
printk("Flash device not found\n");
SYS_LOG_ERR("Flash device not found");
while (1)
;
}

rc = boot_go(&rsp);
if (rc != 0) {
printk("Unable to find bootable image\n");
SYS_LOG_ERR("Unable to find bootable image");
while (1)
;
}

printk("Bootloader chain: 0x%x\n", rsp.br_image_addr);
SYS_LOG_INF("Bootloader chainload address: 0x%x", rsp.br_image_addr);
vt = (struct vector_table *)(rsp.br_image_addr +
rsp.br_hdr->ih_hdr_size);
irq_lock();
_MspSet(vt->msp);

SYS_LOG_INF("Setting vector table to %p", vt);

/* Not all targets set the VTOR, so just set it. */
_scs_relocate_vector_table((void *) vt);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps this can be removed, now that we're going to be setting this properly in Zephyr.


SYS_LOG_INF("Jumping to the first image slot");
((void (*)(void))vt->reset)();

printk("Never should get here\n");
SYS_LOG_ERR("Never should get here");
while (1)
;
}
1 change: 0 additions & 1 deletion boot/zephyr/os.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
*/

#include <zephyr.h>
#include <misc/printk.h>
#include <string.h>

#include "os/os_heap.h"
Expand Down
6 changes: 4 additions & 2 deletions boot/zephyr/prj.conf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
CONFIG_CONSOLE_HANDLER=y
CONFIG_PRINTK=y
CONFIG_SYS_LOG=y
CONFIG_DEBUG=y

CONFIG_MAIN_STACK_SIZE=10240
Expand All @@ -11,4 +11,6 @@ CONFIG_MBEDTLS_CFG_FILE="config-boot.h"
CONFIG_HEAP_MEM_POOL_SIZE=16384

CONFIG_FLASH=y
CONFIG_SOC_FLASH_STM32F4=y

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you do something to Zephyr to make this work? When I did it, there were two flash devices defined for the target so Kconfig picked neither.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's probably because of Linaro/zephyr@942edd4. I'm hoping the upstream version to be merged soon, then we will have the right fix at the right place.

### Disable Bluetooth by default
# CONFIG_BLUETOOTH is not set
17 changes: 0 additions & 17 deletions boot/zephyr/target.sh.example

This file was deleted.

29 changes: 29 additions & 0 deletions boot/zephyr/targets/96b_carbon.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (c) 2017 Linaro
*
* Licensed 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.
*/

/**
* @file
* @brief Bootloader device specific configuration.
*/

#define FLASH_DRIVER_NAME "STM32F4_FLASH"
#define FLASH_ALIGN 1
#define FLASH_AREA_IMAGE_0_OFFSET 0x20000
#define FLASH_AREA_IMAGE_0_SIZE 0x20000
#define FLASH_AREA_IMAGE_1_OFFSET 0x40000
#define FLASH_AREA_IMAGE_1_SIZE 0x20000
#define FLASH_AREA_IMAGE_SCRATCH_OFFSET 0x60000
#define FLASH_AREA_IMAGE_SCRATCH_SIZE 0x20000
29 changes: 29 additions & 0 deletions boot/zephyr/targets/96b_nitrogen.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (c) 2017 Linaro
*
* Licensed 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.
*/

/**
* @file
* @brief Bootloader device specific configuration.
*/

#define FLASH_DRIVER_NAME "NRF5_FLASH"
#define FLASH_ALIGN 1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sure this is really right. Having it too small can excessively wear the flash device.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, same value as used by mynewt, so fine to have that now.

#define FLASH_AREA_IMAGE_0_OFFSET 0x08000
#define FLASH_AREA_IMAGE_0_SIZE 0x3A000
#define FLASH_AREA_IMAGE_1_OFFSET 0x42000
#define FLASH_AREA_IMAGE_1_SIZE 0x3A000
#define FLASH_AREA_IMAGE_SCRATCH_OFFSET 0x7c000
#define FLASH_AREA_IMAGE_SCRATCH_SIZE 0x01000
29 changes: 29 additions & 0 deletions boot/zephyr/targets/frdm_k64f.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (c) 2017 Linaro
*
* Licensed 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.
*/

/**
* @file
* @brief Bootloader device specific configuration.
*/

#define FLASH_DRIVER_NAME "KSDK_FLASH"
#define FLASH_ALIGN 8
#define FLASH_AREA_IMAGE_0_OFFSET 0x20000
#define FLASH_AREA_IMAGE_0_SIZE 0x20000
#define FLASH_AREA_IMAGE_1_OFFSET 0x40000
#define FLASH_AREA_IMAGE_1_SIZE 0x20000
#define FLASH_AREA_IMAGE_SCRATCH_OFFSET 0x60000
#define FLASH_AREA_IMAGE_SCRATCH_SIZE 0x20000
24 changes: 20 additions & 4 deletions build_boot.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
#! /bin/bash
#!/bin/sh

source $(dirname 0)/target.sh
source ../zephyr/zephyr-env.sh
# Assume first argument is the board name (as defined in Zephyr)
BOARD=$1

make BOARD=$BOARD "$@"
if [ -z "$BOARD" ]; then
echo "Please specify the board name (as in Zephyr) as first argument."
exit 1;
fi

if [ ! -f "$(dirname $0)/boot/zephyr/targets/${BOARD}.h" ]; then
echo "Board $BOARD not yet supported, please use a supported target."
exit 1;
fi

# Check if there is a valid Zephyr environment available
if [ -z "$ZEPHYR_BASE" ]; then
echo "ZEPHYR_BASE not provided by the environment."
exit 1;
fi

make BOARD=$BOARD