Initial commit
Initial commit.
This commit is contained in:
6
bootloader/mcuboot/boot/mynewt/README.md
Normal file
6
bootloader/mcuboot/boot/mynewt/README.md
Normal file
@@ -0,0 +1,6 @@
|
||||
# MCUboot - apps/boot
|
||||
|
||||
This sample app implements a bootloader for the Mynewt OS (apache.mynewt.org).
|
||||
This app requires the following Mynewt repositories:
|
||||
* @mcuboot (this one)
|
||||
* @apache-mynewt-core
|
||||
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
* 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 _BOOT_UART_H_
|
||||
#define _BOOT_UART_H_
|
||||
|
||||
int boot_uart_open(void);
|
||||
void boot_uart_close(void);
|
||||
int boot_uart_read(char *str, int cnt, int *newline);
|
||||
void boot_uart_write(const char *ptr, int cnt);
|
||||
|
||||
#endif /* _BOOT_UART_H_ */
|
||||
32
bootloader/mcuboot/boot/mynewt/boot_uart/pkg.yml
Normal file
32
bootloader/mcuboot/boot/mynewt/boot_uart/pkg.yml
Normal file
@@ -0,0 +1,32 @@
|
||||
#
|
||||
# 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.
|
||||
#
|
||||
|
||||
pkg.name: boot/mynewt/boot_uart
|
||||
pkg.description: "For interfacing with uart from boot_serial"
|
||||
pkg.author: "Apache Mynewt <dev@mynewt.apache.org>"
|
||||
pkg.homepage: "http://mynewt.apache.org/"
|
||||
pkg.keywords:
|
||||
- loader
|
||||
- boot
|
||||
- bootloader
|
||||
|
||||
pkg.deps:
|
||||
- "@apache-mynewt-core/hw/hal"
|
||||
|
||||
|
||||
178
bootloader/mcuboot/boot/mynewt/boot_uart/src/boot_uart.c
Normal file
178
bootloader/mcuboot/boot/mynewt/boot_uart/src/boot_uart.c
Normal file
@@ -0,0 +1,178 @@
|
||||
/*
|
||||
* 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 <assert.h>
|
||||
#include <stddef.h>
|
||||
#include <inttypes.h>
|
||||
#include "os/mynewt.h"
|
||||
#include <uart/uart.h>
|
||||
|
||||
/*
|
||||
* RX is a ring buffer, which gets drained constantly.
|
||||
* TX blocks until buffer has been completely transmitted.
|
||||
*/
|
||||
#define CONSOLE_HEAD_INC(cr) (((cr)->head + 1) & (sizeof((cr)->buf) - 1))
|
||||
#define CONSOLE_TAIL_INC(cr) (((cr)->tail + 1) & (sizeof((cr)->buf) - 1))
|
||||
|
||||
struct {
|
||||
uint16_t head;
|
||||
uint16_t tail;
|
||||
uint8_t buf[MYNEWT_VAL(CONSOLE_UART_RX_BUF_SIZE)];
|
||||
} bs_uart_rx;
|
||||
|
||||
struct {
|
||||
uint8_t *ptr;
|
||||
int cnt;
|
||||
} volatile bs_uart_tx;
|
||||
|
||||
static struct uart_dev *bs_uart;
|
||||
|
||||
static int bs_rx_char(void *arg, uint8_t byte);
|
||||
static int bs_tx_char(void *arg);
|
||||
|
||||
int
|
||||
boot_uart_open(void)
|
||||
{
|
||||
struct uart_conf uc = {
|
||||
.uc_speed = MYNEWT_VAL(CONSOLE_UART_BAUD),
|
||||
.uc_databits = 8,
|
||||
.uc_stopbits = 1,
|
||||
.uc_parity = UART_PARITY_NONE,
|
||||
.uc_flow_ctl = MYNEWT_VAL(CONSOLE_UART_FLOW_CONTROL),
|
||||
.uc_tx_char = bs_tx_char,
|
||||
.uc_rx_char = bs_rx_char,
|
||||
};
|
||||
|
||||
bs_uart = (struct uart_dev *)os_dev_open(MYNEWT_VAL(CONSOLE_UART_DEV),
|
||||
OS_TIMEOUT_NEVER, &uc);
|
||||
if (!bs_uart) {
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void
|
||||
boot_uart_close(void)
|
||||
{
|
||||
os_dev_close(&bs_uart->ud_dev);
|
||||
bs_uart_rx.head = 0;
|
||||
bs_uart_rx.tail = 0;
|
||||
bs_uart_tx.cnt = 0;
|
||||
}
|
||||
|
||||
static int
|
||||
bs_rx_char(void *arg, uint8_t byte)
|
||||
{
|
||||
if (CONSOLE_HEAD_INC(&bs_uart_rx) == bs_uart_rx.tail) {
|
||||
/*
|
||||
* RX queue full. Reader must drain this.
|
||||
*/
|
||||
return -1;
|
||||
}
|
||||
bs_uart_rx.buf[bs_uart_rx.head] = byte;
|
||||
bs_uart_rx.head = CONSOLE_HEAD_INC(&bs_uart_rx);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static uint8_t
|
||||
bs_pull_char(void)
|
||||
{
|
||||
uint8_t ch;
|
||||
|
||||
ch = bs_uart_rx.buf[bs_uart_rx.tail];
|
||||
bs_uart_rx.tail = CONSOLE_TAIL_INC(&bs_uart_rx);
|
||||
return ch;
|
||||
}
|
||||
|
||||
int
|
||||
boot_uart_read(char *str, int cnt, int *newline)
|
||||
{
|
||||
int i;
|
||||
int sr;
|
||||
uint8_t ch;
|
||||
|
||||
*newline = 0;
|
||||
OS_ENTER_CRITICAL(sr);
|
||||
for (i = 0; i < cnt; i++) {
|
||||
if (bs_uart_rx.head == bs_uart_rx.tail) {
|
||||
break;
|
||||
}
|
||||
|
||||
ch = bs_pull_char();
|
||||
if (ch == '\n') {
|
||||
*str = '\0';
|
||||
*newline = 1;
|
||||
break;
|
||||
}
|
||||
/*
|
||||
* Unblock interrupts allowing more incoming data.
|
||||
*/
|
||||
OS_EXIT_CRITICAL(sr);
|
||||
OS_ENTER_CRITICAL(sr);
|
||||
*str++ = ch;
|
||||
}
|
||||
OS_EXIT_CRITICAL(sr);
|
||||
if (i > 0 || *newline) {
|
||||
uart_start_rx(bs_uart);
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
static int
|
||||
bs_tx_char(void *arg)
|
||||
{
|
||||
uint8_t ch;
|
||||
|
||||
if (!bs_uart_tx.cnt) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
bs_uart_tx.cnt--;
|
||||
ch = *bs_uart_tx.ptr;
|
||||
bs_uart_tx.ptr++;
|
||||
return ch;
|
||||
}
|
||||
|
||||
#if MYNEWT_VAL(SELFTEST)
|
||||
/*
|
||||
* OS is not running, so native uart 'driver' cannot run either.
|
||||
* At the moment unit tests don't check the responses to messages it
|
||||
* sends, so we can drop the outgoing data here.
|
||||
*/
|
||||
void
|
||||
boot_uart_write(const char *ptr, int cnt)
|
||||
{
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
void
|
||||
boot_uart_write(const char *ptr, int cnt)
|
||||
{
|
||||
int sr;
|
||||
|
||||
OS_ENTER_CRITICAL(sr);
|
||||
bs_uart_tx.ptr = (uint8_t *)ptr;
|
||||
bs_uart_tx.cnt = cnt;
|
||||
OS_EXIT_CRITICAL(sr);
|
||||
|
||||
uart_start_tx(bs_uart);
|
||||
while (bs_uart_tx.cnt);
|
||||
}
|
||||
|
||||
#endif
|
||||
24
bootloader/mcuboot/boot/mynewt/boot_uart/syscfg.yml
Normal file
24
bootloader/mcuboot/boot/mynewt/boot_uart/syscfg.yml
Normal file
@@ -0,0 +1,24 @@
|
||||
# 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.
|
||||
#
|
||||
|
||||
syscfg.defs:
|
||||
CONSOLE_UART_RX_BUF_SIZE:
|
||||
description: >
|
||||
UART console receive buffer size; must be power of 2.
|
||||
value: 32
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
/*
|
||||
* Copyright (c) 2018 Nordic Semiconductor ASA
|
||||
* Copyright (c) 2015 Runtime Inc
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
#ifndef __FLASH_MAP_BACKEND_H__
|
||||
#define __FLASH_MAP_BACKEND_H__
|
||||
|
||||
#include <sysflash/sysflash.h>
|
||||
#include <flash_map/flash_map.h>
|
||||
#include <mcuboot_config/mcuboot_config.h>
|
||||
#include <unistd.h> /* off_t */
|
||||
|
||||
#if (MCUBOOT_IMAGE_NUMBER == 1)
|
||||
|
||||
#define FLASH_AREA_IMAGE_PRIMARY(x) (((x) == 0) ? \
|
||||
FLASH_AREA_IMAGE_0 : \
|
||||
FLASH_AREA_IMAGE_0)
|
||||
#if MYNEWT_VAL(BOOTUTIL_SINGLE_APPLICATION_SLOT)
|
||||
#define FLASH_AREA_IMAGE_SECONDARY FLASH_AREA_IMAGE_PRIMARY
|
||||
#else
|
||||
#define FLASH_AREA_IMAGE_SECONDARY(x) (((x) == 0) ? \
|
||||
FLASH_AREA_IMAGE_1 : \
|
||||
FLASH_AREA_IMAGE_1)
|
||||
#endif
|
||||
|
||||
#elif (MCUBOOT_IMAGE_NUMBER == 2)
|
||||
|
||||
#define FLASH_AREA_IMAGE_PRIMARY(x) (((x) == 0) ? \
|
||||
FLASH_AREA_IMAGE_0 : \
|
||||
((x) == 1) ? \
|
||||
FLASH_AREA_IMAGE_2 : \
|
||||
255)
|
||||
#define FLASH_AREA_IMAGE_SECONDARY(x) (((x) == 0) ? \
|
||||
FLASH_AREA_IMAGE_1 : \
|
||||
((x) == 1) ? \
|
||||
FLASH_AREA_IMAGE_3 : \
|
||||
255)
|
||||
|
||||
#else
|
||||
#error "Image slot and flash area mapping is not defined"
|
||||
#endif
|
||||
|
||||
int flash_area_id_from_multi_image_slot(int image_index, int slot);
|
||||
int flash_area_id_to_multi_image_slot(int image_index, int area_id);
|
||||
|
||||
struct flash_sector {
|
||||
uint32_t fs_off;
|
||||
uint32_t fs_size;
|
||||
};
|
||||
|
||||
int flash_area_sector_from_off(off_t off, struct flash_sector *sector);
|
||||
|
||||
static inline int flash_area_get_sector(const struct flash_area *fa, off_t off,
|
||||
struct flash_sector *sector)
|
||||
{
|
||||
return flash_area_sector_from_off(off, sector);
|
||||
}
|
||||
|
||||
static inline uint8_t flash_area_get_id(const struct flash_area *fa)
|
||||
{
|
||||
return fa->fa_id;
|
||||
}
|
||||
|
||||
static inline uint8_t flash_area_get_device_id(const struct flash_area *fa)
|
||||
{
|
||||
return fa->fa_device_id;
|
||||
}
|
||||
|
||||
static inline uint32_t flash_area_get_off(const struct flash_area *fa)
|
||||
{
|
||||
return fa->fa_off;
|
||||
}
|
||||
|
||||
static inline uint32_t flash_area_get_size(const struct flash_area *fa)
|
||||
{
|
||||
return fa->fa_size;
|
||||
}
|
||||
|
||||
static inline uint32_t flash_sector_get_off(const struct flash_sector *fs)
|
||||
{
|
||||
return fs->fs_off;
|
||||
}
|
||||
|
||||
#endif /* __FLASH_MAP_BACKEND_H__ */
|
||||
14
bootloader/mcuboot/boot/mynewt/flash_map_backend/pkg.yml
Normal file
14
bootloader/mcuboot/boot/mynewt/flash_map_backend/pkg.yml
Normal file
@@ -0,0 +1,14 @@
|
||||
#
|
||||
# Copyright (c) 2018 Nordic Semiconductor ASA
|
||||
#
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
#
|
||||
|
||||
pkg.name: boot/mynewt/flash_map_backend
|
||||
pkg.description: Flash_map API helper reference.
|
||||
pkg.author: "Nordic Semiconductor ASA"
|
||||
pkg.homepage: "http://mcuboot.com"
|
||||
|
||||
pkg.deps:
|
||||
- "@apache-mynewt-core/sys/flash_map"
|
||||
- "@mcuboot/boot/mynewt/mcuboot_config"
|
||||
@@ -0,0 +1,83 @@
|
||||
/*
|
||||
* 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 <flash_map/flash_map.h>
|
||||
#include <flash_map_backend/flash_map_backend.h>
|
||||
#include <hal/hal_bsp.h>
|
||||
#include <hal/hal_flash_int.h>
|
||||
|
||||
int flash_area_id_from_multi_image_slot(int image_index, int slot)
|
||||
{
|
||||
switch (slot) {
|
||||
case 0: return FLASH_AREA_IMAGE_PRIMARY(image_index);
|
||||
#ifndef MCUBOOT_SINGLE_APPLICATION_SLOT
|
||||
case 1: return FLASH_AREA_IMAGE_SECONDARY(image_index);
|
||||
#endif
|
||||
#if MCUBOOT_SWAP_USING_SCRATCH
|
||||
case 2: return FLASH_AREA_IMAGE_SCRATCH;
|
||||
#endif
|
||||
}
|
||||
return 255;
|
||||
}
|
||||
|
||||
int flash_area_id_to_multi_image_slot(int image_index, int area_id)
|
||||
{
|
||||
if (area_id == FLASH_AREA_IMAGE_PRIMARY(image_index)) {
|
||||
return 0;
|
||||
}
|
||||
#ifndef MCUBOOT_SINGLE_APPLICATION_SLOT
|
||||
if (area_id == FLASH_AREA_IMAGE_SECONDARY(image_index)) {
|
||||
return 1;
|
||||
}
|
||||
#endif
|
||||
return 255;
|
||||
}
|
||||
|
||||
int flash_area_sector_from_off(off_t off, struct flash_sector *sector)
|
||||
{
|
||||
const struct flash_area *fa;
|
||||
const struct hal_flash *hf;
|
||||
uint32_t start;
|
||||
uint32_t size;
|
||||
int rc;
|
||||
int i;
|
||||
|
||||
rc = flash_area_open(FLASH_AREA_IMAGE_0, &fa);
|
||||
if (rc != 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
rc = -1;
|
||||
hf = hal_bsp_flash_dev(fa->fa_device_id);
|
||||
for (i = 0; i < hf->hf_sector_cnt; i++) {
|
||||
hf->hf_itf->hff_sector_info(hf, i, &start, &size);
|
||||
if (start < fa->fa_off) {
|
||||
continue;
|
||||
}
|
||||
if (off >= start - fa->fa_off && off <= (start - fa->fa_off) + size) {
|
||||
sector->fs_off = start - fa->fa_off;
|
||||
sector->fs_size = size;
|
||||
rc = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
flash_area_close(fa);
|
||||
return rc;
|
||||
}
|
||||
@@ -0,0 +1,157 @@
|
||||
/*
|
||||
* 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 __MCUBOOT_CONFIG_H__
|
||||
#define __MCUBOOT_CONFIG_H__
|
||||
|
||||
#include <syscfg/syscfg.h>
|
||||
|
||||
#if MYNEWT_VAL(BOOTUTIL_IMAGE_NUMBER)
|
||||
#define MCUBOOT_IMAGE_NUMBER MYNEWT_VAL(BOOTUTIL_IMAGE_NUMBER)
|
||||
#else
|
||||
#define MCUBOOT_IMAGE_NUMBER 1
|
||||
#endif
|
||||
#if MYNEWT_VAL(BOOTUTIL_VERSION_CMP_USE_BUILD_NUMBER)
|
||||
#define MCUBOOT_VERSION_CMP_USE_BUILD_NUMBER
|
||||
#endif
|
||||
#if MYNEWT_VAL(BOOT_SERIAL)
|
||||
#define MCUBOOT_SERIAL 1
|
||||
#endif
|
||||
#if MYNEWT_VAL(BOOT_SERIAL_MGMT_ECHO)
|
||||
#define MCUBOOT_BOOT_MGMT_ECHO 1
|
||||
#endif
|
||||
#if MYNEWT_VAL(BOOTUTIL_VALIDATE_SLOT0)
|
||||
#define MCUBOOT_VALIDATE_PRIMARY_SLOT 1
|
||||
#endif
|
||||
#if MYNEWT_VAL(BOOTUTIL_USE_MBED_TLS)
|
||||
#define MCUBOOT_USE_MBED_TLS 1
|
||||
#endif
|
||||
#if MYNEWT_VAL(BOOTUTIL_USE_TINYCRYPT)
|
||||
#define MCUBOOT_USE_TINYCRYPT 1
|
||||
#endif
|
||||
#if MYNEWT_VAL(BOOTUTIL_SIGN_EC256)
|
||||
#define MCUBOOT_SIGN_EC256 1
|
||||
#endif
|
||||
#if MYNEWT_VAL(BOOTUTIL_SIGN_RSA)
|
||||
#define MCUBOOT_SIGN_RSA 1
|
||||
#define MCUBOOT_SIGN_RSA_LEN MYNEWT_VAL(BOOTUTIL_SIGN_RSA_LEN)
|
||||
#endif
|
||||
#if MYNEWT_VAL(BOOTUTIL_SIGN_ED25519)
|
||||
#define MCUBOOT_SIGN_ED25519 1
|
||||
#endif
|
||||
#if MYNEWT_VAL(BOOTUTIL_ENCRYPT_RSA)
|
||||
#define MCUBOOT_ENCRYPT_RSA 1
|
||||
#endif
|
||||
#if MYNEWT_VAL(BOOTUTIL_ENCRYPT_KW)
|
||||
#define MCUBOOT_ENCRYPT_KW 1
|
||||
#endif
|
||||
#if MYNEWT_VAL(BOOTUTIL_ENCRYPT_EC256)
|
||||
#define MCUBOOT_ENCRYPT_EC256 1
|
||||
#endif
|
||||
#if MYNEWT_VAL(BOOTUTIL_ENCRYPT_X25519)
|
||||
#define MCUBOOT_ENCRYPT_X25519 1
|
||||
#endif
|
||||
#if MYNEWT_VAL(BOOTUTIL_ENCRYPT_RSA) || MYNEWT_VAL(BOOTUTIL_ENCRYPT_KW) || \
|
||||
MYNEWT_VAL(BOOTUTIL_ENCRYPT_EC256) || MYNEWT_VAL(BOOTUTIL_ENCRYPT_X25519)
|
||||
#define MCUBOOT_ENC_IMAGES 1
|
||||
#endif
|
||||
#if MYNEWT_VAL(BOOTUTIL_SWAP_USING_MOVE)
|
||||
#define MCUBOOT_SWAP_USING_MOVE 1
|
||||
#endif
|
||||
#if MYNEWT_VAL(BOOTUTIL_SWAP_SAVE_ENCTLV)
|
||||
#define MCUBOOT_SWAP_SAVE_ENCTLV 1
|
||||
#endif
|
||||
#if MYNEWT_VAL(BOOTUTIL_OVERWRITE_ONLY)
|
||||
#define MCUBOOT_OVERWRITE_ONLY 1
|
||||
#endif
|
||||
#if MYNEWT_VAL(BOOTUTIL_OVERWRITE_ONLY_FAST)
|
||||
#define MCUBOOT_OVERWRITE_ONLY_FAST 1
|
||||
#endif
|
||||
#if MYNEWT_VAL(BOOTUTIL_SINGLE_APPLICATION_SLOT)
|
||||
#define MCUBOOT_SINGLE_APPLICATION_SLOT 1
|
||||
#endif
|
||||
#if MYNEWT_VAL(BOOTUTIL_HAVE_LOGGING)
|
||||
#define MCUBOOT_HAVE_LOGGING 1
|
||||
#endif
|
||||
#if MYNEWT_VAL(BOOTUTIL_BOOTSTRAP)
|
||||
#define MCUBOOT_BOOTSTRAP 1
|
||||
#endif
|
||||
#if MYNEWT_VAL_CHOICE(BOOTUTIL_DOWNGRADE_PREVENTION, version)
|
||||
#define MCUBOOT_DOWNGRADE_PREVENTION 1
|
||||
/* MCUBOOT_DOWNGRADE_PREVENTION_SECURITY_COUNTER is used later as bool value so it is
|
||||
* always defined, (unlike MCUBOOT_DOWNGRADE_PREVENTION which is only used in
|
||||
* preprocessor condition and my be not defined) */
|
||||
#define MCUBOOT_DOWNGRADE_PREVENTION_SECURITY_COUNTER 0
|
||||
#elif MYNEWT_VAL_CHOICE(BOOTUTIL_DOWNGRADE_PREVENTION, security_counter)
|
||||
#define MCUBOOT_DOWNGRADE_PREVENTION 1
|
||||
#define MCUBOOT_DOWNGRADE_PREVENTION_SECURITY_COUNTER 1
|
||||
#endif
|
||||
#if MYNEWT_VAL(BOOTUTIL_HW_DOWNGRADE_PREVENTION)
|
||||
#define MCUBOOT_HW_ROLLBACK_PROT 1
|
||||
#endif
|
||||
|
||||
#if MYNEWT_VAL(MCUBOOT_MEASURED_BOOT)
|
||||
#define MCUBOOT_MEASURED_BOOT 1
|
||||
#endif
|
||||
|
||||
#if MYNEWT_VAL(MCUBOOT_MEASURED_BOOT_MAX_RECORD_SZ)
|
||||
#define MAX_BOOT_RECORD_SZ MYNEWT_VAL(MCUBOOT_MEASURED_BOOT_MAX_RECORD_SZ)
|
||||
#endif
|
||||
|
||||
#if MYNEWT_VAL(MCUBOOT_DATA_SHARING)
|
||||
#define MCUBOOT_DATA_SHARING 1
|
||||
#endif
|
||||
|
||||
#if MYNEWT_VAL(MCUBOOT_SHARED_DATA_BASE)
|
||||
#define MCUBOOT_SHARED_DATA_BASE MYNEWT_VAL(MCUBOOT_SHARED_DATA_BASE)
|
||||
#endif
|
||||
|
||||
#if MYNEWT_VAL(MCUBOOT_SHARED_DATA_SIZE)
|
||||
#define MCUBOOT_SHARED_DATA_SIZE MYNEWT_VAL(MCUBOOT_SHARED_DATA_SIZE)
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Currently there is no configuration option, for this platform,
|
||||
* that enables the system specific mcumgr commands in mcuboot
|
||||
*/
|
||||
#define MCUBOOT_PERUSER_MGMT_GROUP_ENABLED 0
|
||||
|
||||
#define MCUBOOT_MAX_IMG_SECTORS MYNEWT_VAL(BOOTUTIL_MAX_IMG_SECTORS)
|
||||
|
||||
#if MYNEWT_VAL(MCU_FLASH_MIN_WRITE_SIZE) > 8
|
||||
#define MCUBOOT_BOOT_MAX_ALIGN MYNEWT_VAL(MCU_FLASH_MIN_WRITE_SIZE)
|
||||
#endif
|
||||
|
||||
#if MYNEWT_VAL(BOOTUTIL_FEED_WATCHDOG) && MYNEWT_VAL(WATCHDOG_INTERVAL)
|
||||
#include <hal/hal_watchdog.h>
|
||||
#define MCUBOOT_WATCHDOG_FEED() \
|
||||
do { \
|
||||
hal_watchdog_tickle(); \
|
||||
} while (0)
|
||||
#else
|
||||
#define MCUBOOT_WATCHDOG_FEED() do {} while (0)
|
||||
#endif
|
||||
|
||||
/*
|
||||
* No direct idle call implemented
|
||||
*/
|
||||
#define MCUBOOT_CPU_IDLE() \
|
||||
do { \
|
||||
} while (0)
|
||||
|
||||
#endif /* __MCUBOOT_CONFIG_H__ */
|
||||
@@ -0,0 +1,87 @@
|
||||
/*
|
||||
* 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 __MCUBOOT_LOGGING_H__
|
||||
#define __MCUBOOT_LOGGING_H__
|
||||
|
||||
#include <syscfg/syscfg.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#define BOOTUTIL_LOG_LEVEL_OFF 1
|
||||
#define BOOTUTIL_LOG_LEVEL_ERROR 2
|
||||
#define BOOTUTIL_LOG_LEVEL_WARNING 3
|
||||
#define BOOTUTIL_LOG_LEVEL_INFO 4
|
||||
#define BOOTUTIL_LOG_LEVEL_DEBUG 5
|
||||
|
||||
#define MCUBOOT_LOG_LEVEL_OFF BOOTUTIL_LOG_LEVEL_OFF
|
||||
#define MCUBOOT_LOG_LEVEL_ERROR BOOTUTIL_LOG_LEVEL_ERROR
|
||||
#define MCUBOOT_LOG_LEVEL_WARNING BOOTUTIL_LOG_LEVEL_WARNING
|
||||
#define MCUBOOT_LOG_LEVEL_INFO BOOTUTIL_LOG_LEVEL_INFO
|
||||
#define MCUBOOT_LOG_LEVEL_DEBUG BOOTUTIL_LOG_LEVEL_DEBUG
|
||||
|
||||
#ifndef MCUBOOT_LOG_LEVEL
|
||||
#define MCUBOOT_LOG_LEVEL MYNEWT_VAL(BOOTUTIL_LOG_LEVEL)
|
||||
#endif
|
||||
|
||||
#define MCUBOOT_LOG_MODULE_DECLARE(domain) /* ignore */
|
||||
#define MCUBOOT_LOG_MODULE_REGISTER(domain) /* ignore */
|
||||
|
||||
#if !((MCUBOOT_LOG_LEVEL >= MCUBOOT_LOG_LEVEL_OFF) && \
|
||||
(MCUBOOT_LOG_LEVEL <= MCUBOOT_LOG_LEVEL_DEBUG))
|
||||
#error "Invalid MCUBOOT_LOG_LEVEL config."
|
||||
#endif
|
||||
|
||||
#if MCUBOOT_LOG_LEVEL >= MCUBOOT_LOG_LEVEL_ERROR
|
||||
#define MCUBOOT_LOG_ERR(_fmt, ...) \
|
||||
do { \
|
||||
printf("[ERR] " _fmt "\n", ##__VA_ARGS__); \
|
||||
} while (0)
|
||||
#else
|
||||
#define MCUBOOT_LOG_ERR(...) IGNORE(__VA_ARGS__)
|
||||
#endif
|
||||
|
||||
#if MCUBOOT_LOG_LEVEL >= MCUBOOT_LOG_LEVEL_WARNING
|
||||
#define MCUBOOT_LOG_WRN(_fmt, ...) \
|
||||
do { \
|
||||
printf("[WRN] " _fmt "\n", ##__VA_ARGS__); \
|
||||
} while (0)
|
||||
#else
|
||||
#define MCUBOOT_LOG_WRN(...) IGNORE(__VA_ARGS__)
|
||||
#endif
|
||||
|
||||
#if MCUBOOT_LOG_LEVEL >= MCUBOOT_LOG_LEVEL_INFO
|
||||
#define MCUBOOT_LOG_INF(_fmt, ...) \
|
||||
do { \
|
||||
printf("[INF] " _fmt "\n", ##__VA_ARGS__); \
|
||||
} while (0)
|
||||
#else
|
||||
#define MCUBOOT_LOG_INF(...) IGNORE(__VA_ARGS__)
|
||||
#endif
|
||||
|
||||
#if MCUBOOT_LOG_LEVEL >= MCUBOOT_LOG_LEVEL_DEBUG
|
||||
#define MCUBOOT_LOG_DBG(_fmt, ...) \
|
||||
do { \
|
||||
printf("[DBG] " _fmt "\n", ##__VA_ARGS__); \
|
||||
} while (0)
|
||||
#else
|
||||
#define MCUBOOT_LOG_DBG(...) IGNORE(__VA_ARGS__)
|
||||
#endif
|
||||
|
||||
#define MCUBOOT_LOG_SIM(...) IGNORE(__VA_ARGS__)
|
||||
|
||||
#endif
|
||||
23
bootloader/mcuboot/boot/mynewt/mcuboot_config/pkg.yml
Normal file
23
bootloader/mcuboot/boot/mynewt/mcuboot_config/pkg.yml
Normal file
@@ -0,0 +1,23 @@
|
||||
#
|
||||
# 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.
|
||||
#
|
||||
|
||||
pkg.name: boot/mynewt/mcuboot_config
|
||||
pkg.description: "Mynewt's mcuboot configuration"
|
||||
pkg.author: "Apache Mynewt <dev@mynewt.apache.org>"
|
||||
pkg.homepage: "http://mynewt.apache.org/"
|
||||
159
bootloader/mcuboot/boot/mynewt/mcuboot_config/syscfg.yml
Normal file
159
bootloader/mcuboot/boot/mynewt/mcuboot_config/syscfg.yml
Normal file
@@ -0,0 +1,159 @@
|
||||
# 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.
|
||||
#
|
||||
|
||||
# Package: boot/mynewt/mcuboot_config
|
||||
|
||||
syscfg.defs:
|
||||
BOOTUTIL_IMAGE_NUMBER:
|
||||
description: 'Number of images for multi-image (0 and 1 mean single image).'
|
||||
value: 0
|
||||
BOOTUTIL_VALIDATE_SLOT0:
|
||||
description: 'Validate image at slot 0 on each boot.'
|
||||
value: 0
|
||||
BOOTUTIL_SIGN_RSA:
|
||||
description: 'Images are signed using RSA.'
|
||||
value: 0
|
||||
BOOTUTIL_SIGN_RSA_LEN:
|
||||
description: 'Key size for RSA keys (2048 or 3072).'
|
||||
value: 2048
|
||||
BOOTUTIL_SIGN_EC256:
|
||||
description: 'Images are signed using ECDSA NIST P-256.'
|
||||
value: 0
|
||||
BOOTUTIL_SIGN_ED25519:
|
||||
description: 'Images are signed using ED25519.'
|
||||
value: 0
|
||||
BOOTUTIL_ENCRYPT_RSA:
|
||||
description: 'Support for encrypted images using RSA-2048-OAEP.'
|
||||
value: 0
|
||||
BOOTUTIL_ENCRYPT_KW:
|
||||
description: 'Support for encrypted images using AES-128-Keywrap.'
|
||||
value: 0
|
||||
BOOTUTIL_ENCRYPT_EC256:
|
||||
description: 'Support for encrypted images using ECIES-P256.'
|
||||
value: 0
|
||||
BOOTUTIL_ENCRYPT_X25519:
|
||||
description: 'Support for encrypted images using ECIES-X25519.'
|
||||
value: 0
|
||||
BOOTUTIL_USE_MBED_TLS:
|
||||
description: 'Use mbed TLS for crypto operations.'
|
||||
value: 1
|
||||
BOOTUTIL_USE_TINYCRYPT:
|
||||
description: 'Use tinycrypt for crypto operations.'
|
||||
value: 0
|
||||
BOOTUTIL_SWAP_USING_MOVE:
|
||||
description: 'Perform swap without requiring scratch.'
|
||||
value: 0
|
||||
BOOTUTIL_SWAP_SAVE_ENCTLV:
|
||||
description: 'Save TLVs instead of plaintext encryption keys in swap status.'
|
||||
value: 0
|
||||
BOOTUTIL_OVERWRITE_ONLY:
|
||||
description: 'Non-swapping upgrades, copy from slot 1 to slot 0 only.'
|
||||
value: 0
|
||||
BOOTUTIL_OVERWRITE_ONLY_FAST:
|
||||
description: 'Use faster copy only upgrade.'
|
||||
value: 1
|
||||
BOOTUTIL_SINGLE_APPLICATION_SLOT:
|
||||
description: 'Set to one if there is only one slot.'
|
||||
value: 0
|
||||
BOOTUTIL_IMAGE_FORMAT_V2:
|
||||
description: 'Indicates that system is using v2 of image format.'
|
||||
value: 1
|
||||
BOOTUTIL_MAX_IMG_SECTORS:
|
||||
description: 'Maximum number of sectors that are swapped.'
|
||||
value: 128
|
||||
BOOTUTIL_DOWNGRADE_PREVENTION:
|
||||
description: >
|
||||
Select downgrade prevention strategy.
|
||||
- none downgrades are allowed
|
||||
- version:
|
||||
Prevent downgrades by enforcing incrementing version numbers.
|
||||
When this option is set, any upgrade must have greater major version
|
||||
or greater minor version with equal major version. This mechanism
|
||||
only protects against some attacks against version downgrades (for
|
||||
example, a JTAG could be used to write an older version).
|
||||
- security_counter:
|
||||
security counter is used for version eligibility check instead of pure
|
||||
version. When this option is set, any upgrade must have greater or
|
||||
equal security counter value.
|
||||
Because of the acceptance of equal values it allows for software
|
||||
downgrades to some extent.
|
||||
choices:
|
||||
- none
|
||||
- version
|
||||
- security_counter
|
||||
value: none
|
||||
BOOTUTIL_VERSION_CMP_USE_BUILD_NUMBER:
|
||||
description: >
|
||||
Use build number while comparing image version.
|
||||
By default, the image version comparison relies only on version major,
|
||||
minor and revision. Enable this option to take into account the build
|
||||
number as well.
|
||||
This only affect builds with BOOTUTIL_DOWNGRADE_PREVENTION set to version.
|
||||
value: 0
|
||||
BOOTUTIL_HW_ROLLBACK_PROT:
|
||||
description: >
|
||||
Prevent undesirable/malicious software downgrades. When this option is
|
||||
set, any upgrade must have greater or equal security counter value.
|
||||
Because of the acceptance of equal values it allows for software
|
||||
downgrade to some extent
|
||||
value: 0
|
||||
BOOTUTIL_HAVE_LOGGING:
|
||||
description: 'Enable serial logging'
|
||||
value: 0
|
||||
restrictions:
|
||||
- "!BOOTUTIL_NO_LOGGING"
|
||||
BOOTUTIL_NO_LOGGING:
|
||||
description: 'No serial logging'
|
||||
value: 1
|
||||
restrictions:
|
||||
- "!BOOTUTIL_HAVE_LOGGING"
|
||||
BOOTUTIL_LOG_LEVEL:
|
||||
description: >
|
||||
Default console log level. Valid values are:
|
||||
BOOTUTIL_LOG_LEVEL_OFF
|
||||
BOOTUTIL_LOG_LEVEL_ERROR
|
||||
BOOTUTIL_LOG_LEVEL_WARNING
|
||||
BOOTUTIL_LOG_LEVEL_INFO
|
||||
BOOTUTIL_LOG_LEVEL_DEBUG
|
||||
value: 'BOOTUTIL_LOG_LEVEL_INFO'
|
||||
BOOTUTIL_BOOTSTRAP:
|
||||
description: 'Support bootstrapping slot0 from slot1, if slot0 is empty'
|
||||
value: 0
|
||||
BOOTUTIL_FEED_WATCHDOG:
|
||||
description: 'Enable watchdog feeding while performing a swap upgrade'
|
||||
value: 0
|
||||
|
||||
MCUBOOT_MEASURED_BOOT:
|
||||
description: >
|
||||
Store the boot state/measurements in shared memory.
|
||||
If enabled, the bootloader will store certain boot measurements such as
|
||||
the hash of the firmware image in a shared memory area. This data can
|
||||
be used later by runtime services (e.g. by a device attestation service).
|
||||
value:
|
||||
MCUBOOT_MEASURED_BOOT_MAX_RECORD_SZ:
|
||||
description: the maximum size of the CBOR encoded boot record in bytes.
|
||||
value:
|
||||
MCUBOOT_DATA_SHARING:
|
||||
description: Save application specific data in shared memory (RAM).
|
||||
value:
|
||||
MCUBOOT_SHARED_DATA_BASE:
|
||||
description: RAM address of shared data
|
||||
value:
|
||||
MCUBOOT_SHARED_DATA_SIZE:
|
||||
description: Shared data size.
|
||||
value:
|
||||
51
bootloader/mcuboot/boot/mynewt/pkg.yml
Normal file
51
bootloader/mcuboot/boot/mynewt/pkg.yml
Normal file
@@ -0,0 +1,51 @@
|
||||
#
|
||||
# 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.
|
||||
#
|
||||
|
||||
pkg.name: boot/mynewt
|
||||
pkg.type: app
|
||||
pkg.description: "Mynewt port of mcuboot"
|
||||
pkg.author: "Apache Mynewt <dev@mynewt.apache.org>"
|
||||
pkg.homepage: "http://mynewt.apache.org/"
|
||||
pkg.keywords:
|
||||
- loader
|
||||
|
||||
pkg.cflags:
|
||||
- "-DMCUBOOT_MYNEWT=1"
|
||||
|
||||
pkg.deps:
|
||||
- "@mcuboot/boot/mynewt/mcuboot_config"
|
||||
- "@mcuboot/boot/bootutil"
|
||||
- "@mcuboot/boot/mynewt/flash_map_backend"
|
||||
- "@apache-mynewt-core/kernel/os"
|
||||
- "@apache-mynewt-core/sys/log/stub"
|
||||
|
||||
pkg.ign_files.!BOOTUTIL_SINGLE_APPLICATION_SLOT:
|
||||
- "single_loader.c"
|
||||
pkg.ign_files.BOOTUTIL_SINGLE_APPLICATION_SLOT:
|
||||
- "swap_scratch.c"
|
||||
|
||||
pkg.deps.BOOTUTIL_NO_LOGGING:
|
||||
- "@apache-mynewt-core/sys/console/stub"
|
||||
|
||||
pkg.deps.BOOTUTIL_HAVE_LOGGING:
|
||||
- "@apache-mynewt-core/sys/console/minimal"
|
||||
|
||||
pkg.deps.BOOT_SERIAL:
|
||||
- "@mcuboot/boot/boot_serial"
|
||||
- "@mcuboot/boot/mynewt/boot_uart"
|
||||
278
bootloader/mcuboot/boot/mynewt/src/main.c
Normal file
278
bootloader/mcuboot/boot/mynewt/src/main.c
Normal file
@@ -0,0 +1,278 @@
|
||||
/*
|
||||
* 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 "mcuboot_config/mcuboot_config.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <stddef.h>
|
||||
#include <inttypes.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include <syscfg/syscfg.h>
|
||||
#include <flash_map_backend/flash_map_backend.h>
|
||||
#include <os/os.h>
|
||||
#include <bsp/bsp.h>
|
||||
#include <hal/hal_bsp.h>
|
||||
#include <hal/hal_system.h>
|
||||
#include <hal/hal_flash.h>
|
||||
#include <hal/hal_watchdog.h>
|
||||
#include <sysinit/sysinit.h>
|
||||
#ifdef MCUBOOT_SERIAL
|
||||
#include <hal/hal_gpio.h>
|
||||
#include <hal/hal_nvreg.h>
|
||||
#include <boot_serial/boot_serial.h>
|
||||
#endif
|
||||
#if defined(MCUBOOT_SERIAL)
|
||||
#include <boot_uart/boot_uart.h>
|
||||
#endif
|
||||
#include <console/console.h>
|
||||
#include "bootutil/image.h"
|
||||
#include "bootutil/bootutil.h"
|
||||
#include "bootutil/bootutil_log.h"
|
||||
#include "bootutil/fault_injection_hardening.h"
|
||||
|
||||
#if MYNEWT_VAL(BOOT_CUSTOM_START)
|
||||
void boot_custom_start(uintptr_t flash_base, struct boot_rsp *rsp);
|
||||
#endif
|
||||
|
||||
#if MYNEWT_VAL(BOOT_PREBOOT)
|
||||
void boot_preboot(void);
|
||||
#endif
|
||||
|
||||
#if defined(MCUBOOT_SERIAL)
|
||||
#define BOOT_SERIAL_REPORT_DUR \
|
||||
(MYNEWT_VAL(OS_CPUTIME_FREQ) / MYNEWT_VAL(BOOT_SERIAL_REPORT_FREQ))
|
||||
#define BOOT_SERIAL_INPUT_MAX (512)
|
||||
|
||||
static int boot_read(char *str, int cnt, int *newline);
|
||||
static const struct boot_uart_funcs boot_uart_funcs = {
|
||||
.read = boot_read,
|
||||
.write = boot_uart_write
|
||||
};
|
||||
|
||||
static int
|
||||
boot_read(char *str, int cnt, int *newline)
|
||||
{
|
||||
#if MYNEWT_VAL(BOOT_SERIAL_REPORT_PIN) != -1
|
||||
static uint32_t tick = 0;
|
||||
|
||||
if (tick == 0) {
|
||||
/*
|
||||
* Configure GPIO line as output. This is a pin we toggle at the
|
||||
* given frequency.
|
||||
*/
|
||||
hal_gpio_init_out(MYNEWT_VAL(BOOT_SERIAL_REPORT_PIN), 0);
|
||||
tick = os_cputime_get32();
|
||||
} else {
|
||||
if (os_cputime_get32() - tick > BOOT_SERIAL_REPORT_DUR) {
|
||||
hal_gpio_toggle(MYNEWT_VAL(BOOT_SERIAL_REPORT_PIN));
|
||||
tick = os_cputime_get32();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
hal_watchdog_tickle();
|
||||
|
||||
return boot_uart_read(str, cnt, newline);
|
||||
}
|
||||
|
||||
#if MYNEWT_VAL(BOOT_SERIAL_DETECT_TIMEOUT) != 0
|
||||
|
||||
/** Don't include null-terminator in comparison. */
|
||||
#define BOOT_SERIAL_DETECT_STRING_LEN \
|
||||
(sizeof MYNEWT_VAL(BOOT_SERIAL_DETECT_STRING) - 1)
|
||||
|
||||
/**
|
||||
* Listens on the UART for the management string. Blocks for up to
|
||||
* BOOT_SERIAL_DETECT_TIMEOUT milliseconds.
|
||||
*
|
||||
* @return true if the management string was received;
|
||||
* false if the management string was not received
|
||||
* before the UART listen timeout expired.
|
||||
*/
|
||||
static bool
|
||||
serial_detect_uart_string(void)
|
||||
{
|
||||
uint32_t start_tick;
|
||||
char buf[BOOT_SERIAL_DETECT_STRING_LEN] = { 0 };
|
||||
char ch;
|
||||
int newline;
|
||||
int rc;
|
||||
|
||||
/* Calculate the timeout duration in OS cputime ticks. */
|
||||
static const uint32_t timeout_dur =
|
||||
MYNEWT_VAL(BOOT_SERIAL_DETECT_TIMEOUT) /
|
||||
(1000.0 / MYNEWT_VAL(OS_CPUTIME_FREQ));
|
||||
|
||||
rc = boot_uart_open();
|
||||
assert(rc == 0);
|
||||
|
||||
start_tick = os_cputime_get32();
|
||||
|
||||
while (1) {
|
||||
/* Read a single character from the UART. */
|
||||
rc = boot_uart_read(&ch, 1, &newline);
|
||||
if (rc > 0) {
|
||||
/* Eliminate the oldest character in the buffer to make room for
|
||||
* the new one.
|
||||
*/
|
||||
memmove(buf, buf + 1, BOOT_SERIAL_DETECT_STRING_LEN - 1);
|
||||
buf[BOOT_SERIAL_DETECT_STRING_LEN - 1] = ch;
|
||||
|
||||
/* If the full management string has been received, indicate that
|
||||
* the serial boot loader should start.
|
||||
*/
|
||||
rc = memcmp(buf,
|
||||
MYNEWT_VAL(BOOT_SERIAL_DETECT_STRING),
|
||||
BOOT_SERIAL_DETECT_STRING_LEN);
|
||||
if (rc == 0) {
|
||||
boot_uart_close();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/* Abort the listen on timeout. */
|
||||
if (os_cputime_get32() >= start_tick + timeout_dur) {
|
||||
boot_uart_close();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
static void
|
||||
serial_boot_detect(void)
|
||||
{
|
||||
/*
|
||||
* Read retained register and compare with expected magic value.
|
||||
* If it matches, await for download commands from serial.
|
||||
*/
|
||||
#if MYNEWT_VAL(BOOT_SERIAL_NVREG_INDEX) != -1
|
||||
if (hal_nvreg_read(MYNEWT_VAL(BOOT_SERIAL_NVREG_INDEX)) ==
|
||||
MYNEWT_VAL(BOOT_SERIAL_NVREG_MAGIC)) {
|
||||
|
||||
hal_nvreg_write(MYNEWT_VAL(BOOT_SERIAL_NVREG_INDEX), 0);
|
||||
goto serial_boot;
|
||||
}
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Configure a GPIO as input, and compare it against expected value.
|
||||
* If it matches, await for download commands from serial.
|
||||
*/
|
||||
#if MYNEWT_VAL(BOOT_SERIAL_DETECT_PIN) != -1
|
||||
hal_gpio_init_in(MYNEWT_VAL(BOOT_SERIAL_DETECT_PIN),
|
||||
MYNEWT_VAL(BOOT_SERIAL_DETECT_PIN_CFG));
|
||||
if (hal_gpio_read(MYNEWT_VAL(BOOT_SERIAL_DETECT_PIN)) ==
|
||||
MYNEWT_VAL(BOOT_SERIAL_DETECT_PIN_VAL)) {
|
||||
goto serial_boot;
|
||||
}
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Listen for management pattern in UART input. If detected, await for
|
||||
* download commands from serial.
|
||||
*/
|
||||
#if MYNEWT_VAL(BOOT_SERIAL_DETECT_TIMEOUT) != 0
|
||||
if (serial_detect_uart_string()) {
|
||||
goto serial_boot;
|
||||
}
|
||||
#endif
|
||||
return;
|
||||
serial_boot:
|
||||
boot_uart_open();
|
||||
boot_serial_start(&boot_uart_funcs);
|
||||
assert(0);
|
||||
}
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Temporary flash_device_base() implementation.
|
||||
*
|
||||
* TODO: remove this when mynewt needs to support flash_device_base()
|
||||
* for devices with nonzero base addresses.
|
||||
*/
|
||||
int flash_device_base(uint8_t fd_id, uintptr_t *ret)
|
||||
{
|
||||
*ret = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
mynewt_main(void)
|
||||
{
|
||||
struct boot_rsp rsp;
|
||||
uintptr_t flash_base;
|
||||
int rc;
|
||||
fih_ret fih_rc = FIH_FAILURE;
|
||||
|
||||
hal_bsp_init();
|
||||
|
||||
#if !MYNEWT_VAL(OS_SCHEDULING) && MYNEWT_VAL(WATCHDOG_INTERVAL)
|
||||
rc = hal_watchdog_init(MYNEWT_VAL(WATCHDOG_INTERVAL));
|
||||
assert(rc == 0);
|
||||
#endif
|
||||
|
||||
#if defined(MCUBOOT_SERIAL) || defined(MCUBOOT_HAVE_LOGGING) || \
|
||||
MYNEWT_VAL(CRYPTO) || MYNEWT_VAL(HASH) || MYNEWT_VAL(BOOT_MYNEWT_SYSINIT)
|
||||
/* initialize uart/crypto without os */
|
||||
os_dev_initialize_all(OS_DEV_INIT_PRIMARY);
|
||||
os_dev_initialize_all(OS_DEV_INIT_SECONDARY);
|
||||
sysinit();
|
||||
console_blocking_mode();
|
||||
#if defined(MCUBOOT_SERIAL)
|
||||
serial_boot_detect();
|
||||
hal_timer_deinit(MYNEWT_VAL(OS_CPUTIME_TIMER_NUM));
|
||||
#endif
|
||||
#else
|
||||
flash_map_init();
|
||||
#endif
|
||||
|
||||
#if MYNEWT_VAL(BOOT_PREBOOT)
|
||||
boot_preboot();
|
||||
#endif
|
||||
FIH_CALL(boot_go, fih_rc, &rsp);
|
||||
if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
|
||||
assert(fih_rc == FIH_SUCCESS);
|
||||
FIH_PANIC;
|
||||
}
|
||||
|
||||
rc = flash_device_base(rsp.br_flash_dev_id, &flash_base);
|
||||
assert(rc == 0);
|
||||
|
||||
#if MYNEWT_VAL(BOOT_CUSTOM_START)
|
||||
boot_custom_start(flash_base, &rsp);
|
||||
#else
|
||||
hal_bsp_deinit();
|
||||
hal_system_start((void *)(flash_base + rsp.br_image_off +
|
||||
rsp.br_hdr->ih_hdr_size));
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Mynewt startup code jump to mynewt_main()
|
||||
* This function is here for compatibility with
|
||||
* pre 1.12. mynewt-core that still wanted main()
|
||||
*/
|
||||
int
|
||||
main(void)
|
||||
{
|
||||
mynewt_main();
|
||||
}
|
||||
136
bootloader/mcuboot/boot/mynewt/src/single_loader.c
Normal file
136
bootloader/mcuboot/boot/mynewt/src/single_loader.c
Normal file
@@ -0,0 +1,136 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Copyright (c) 2020 Nordic Semiconductor ASA
|
||||
* Copyright (c) 2020 Arm Limited
|
||||
*/
|
||||
|
||||
#include <assert.h>
|
||||
#include "bootutil/image.h"
|
||||
#include "../../../bootutil/src/bootutil_priv.h"
|
||||
#include "bootutil/bootutil_log.h"
|
||||
#include "bootutil/bootutil_public.h"
|
||||
#include "bootutil/fault_injection_hardening.h"
|
||||
|
||||
#include "mcuboot_config/mcuboot_config.h"
|
||||
|
||||
BOOT_LOG_MODULE_DECLARE(mcuboot);
|
||||
|
||||
/* Variables passed outside of unit via poiters. */
|
||||
static const struct flash_area *_fa_p;
|
||||
static struct image_header _hdr = { 0 };
|
||||
|
||||
#if defined(MCUBOOT_VALIDATE_PRIMARY_SLOT) || defined(MCUBOOT_VALIDATE_PRIMARY_SLOT_ONCE)
|
||||
/**
|
||||
* Validate hash of a primary boot image.
|
||||
*
|
||||
* @param[in] fa_p flash area pointer
|
||||
* @param[in] hdr boot image header pointer
|
||||
*
|
||||
* @return FIH_SUCCESS on success, error code otherwise
|
||||
*/
|
||||
fih_ret
|
||||
boot_image_validate(const struct flash_area *fa_p,
|
||||
struct image_header *hdr)
|
||||
{
|
||||
static uint8_t tmpbuf[BOOT_TMPBUF_SZ];
|
||||
FIH_DECLARE(fih_rc, FIH_FAILURE);
|
||||
|
||||
/* NOTE: The first argument to boot_image_validate, for enc_state pointer,
|
||||
* is allowed to be NULL only because the single image loader compiles
|
||||
* with BOOT_IMAGE_NUMBER == 1, which excludes the code that uses
|
||||
* the pointer from compilation.
|
||||
*/
|
||||
/* Validate hash */
|
||||
if (IS_ENCRYPTED(hdr))
|
||||
{
|
||||
/* Clear the encrypted flag we didn't supply a key
|
||||
* This flag could be set if there was a decryption in place
|
||||
* was performed. We will try to validate the image, and if still
|
||||
* encrypted the validation will fail, and go in panic mode
|
||||
*/
|
||||
hdr->ih_flags &= ~(ENCRYPTIONFLAGS);
|
||||
}
|
||||
FIH_CALL(bootutil_img_validate, fih_rc, NULL, 0, hdr, fa_p, tmpbuf,
|
||||
BOOT_TMPBUF_SZ, NULL, 0, NULL);
|
||||
|
||||
FIH_RET(fih_rc);
|
||||
}
|
||||
#endif /* MCUBOOT_VALIDATE_PRIMARY_SLOT || MCUBOOT_VALIDATE_PRIMARY_SLOT_ONCE*/
|
||||
|
||||
#if defined(MCUBOOT_VALIDATE_PRIMARY_SLOT_ONCE)
|
||||
inline static fih_ret
|
||||
boot_image_validate_once(const struct flash_area *fa_p,
|
||||
struct image_header *hdr)
|
||||
{
|
||||
static struct boot_swap_state state;
|
||||
int rc;
|
||||
FIH_DECLARE(fih_rc, FIH_FAILURE);
|
||||
|
||||
memset(&state, 0, sizeof(struct boot_swap_state));
|
||||
rc = boot_read_swap_state(fa_p, &state);
|
||||
if (rc != 0)
|
||||
FIH_RET(FIH_FAILURE);
|
||||
if (state.magic != BOOT_MAGIC_GOOD
|
||||
|| state.image_ok != BOOT_FLAG_SET) {
|
||||
/* At least validate the image once */
|
||||
FIH_CALL(boot_image_validate, fih_rc, fa_p, hdr);
|
||||
if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
|
||||
FIH_RET(FIH_FAILURE);
|
||||
}
|
||||
if (state.magic != BOOT_MAGIC_GOOD) {
|
||||
rc = boot_write_magic(fa_p);
|
||||
if (rc != 0)
|
||||
FIH_RET(FIH_FAILURE);
|
||||
}
|
||||
rc = boot_write_image_ok(fa_p);
|
||||
if (rc != 0)
|
||||
FIH_RET(FIH_FAILURE);
|
||||
}
|
||||
FIH_RET(FIH_SUCCESS);
|
||||
}
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Gather information on image and prepare for booting.
|
||||
*
|
||||
* @parami[out] rsp Parameters for booting image, on success
|
||||
*
|
||||
* @return FIH_SUCCESS on success; nonzero on failure.
|
||||
*/
|
||||
fih_ret
|
||||
boot_go(struct boot_rsp *rsp)
|
||||
{
|
||||
int rc = -1;
|
||||
FIH_DECLARE(fih_rc, FIH_FAILURE);
|
||||
|
||||
rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(0), &_fa_p);
|
||||
assert(rc == 0);
|
||||
|
||||
rc = boot_image_load_header(_fa_p, &_hdr);
|
||||
if (rc != 0)
|
||||
goto out;
|
||||
|
||||
#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
|
||||
FIH_CALL(boot_image_validate, fih_rc, _fa_p, &_hdr);
|
||||
if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
|
||||
goto out;
|
||||
}
|
||||
#elif defined(MCUBOOT_VALIDATE_PRIMARY_SLOT_ONCE)
|
||||
FIH_CALL(boot_image_validate_once, fih_rc, _fa_p, &_hdr);
|
||||
if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
|
||||
goto out;
|
||||
}
|
||||
#else
|
||||
fih_rc = FIH_SUCCESS;
|
||||
#endif /* MCUBOOT_VALIDATE_PRIMARY_SLOT */
|
||||
|
||||
rsp->br_flash_dev_id = flash_area_get_device_id(_fa_p);
|
||||
rsp->br_image_off = flash_area_get_off(_fa_p);
|
||||
rsp->br_hdr = &_hdr;
|
||||
|
||||
out:
|
||||
flash_area_close(_fa_p);
|
||||
|
||||
FIH_RET(fih_rc);
|
||||
}
|
||||
47
bootloader/mcuboot/boot/mynewt/syscfg.yml
Normal file
47
bootloader/mcuboot/boot/mynewt/syscfg.yml
Normal file
@@ -0,0 +1,47 @@
|
||||
# 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.
|
||||
#
|
||||
|
||||
# Package: boot/mynewt
|
||||
|
||||
syscfg.defs:
|
||||
BOOT_LOADER:
|
||||
description: 'Set to indicate that this app is a bootloader.'
|
||||
value: 1
|
||||
BOOT_SERIAL:
|
||||
description: 'Support image upgrade over serial within bootloader'
|
||||
value: 0
|
||||
BOOT_CUSTOM_START:
|
||||
description: 'Override hal_system_start with a custom start routine'
|
||||
value:
|
||||
BOOT_PREBOOT:
|
||||
description: 'Call boot_preboot() function before booting application'
|
||||
value:
|
||||
BOOT_MYNEWT_SYSINIT:
|
||||
description: >
|
||||
When not 0 performs device initialization and calls newt
|
||||
generated sysinit() function.
|
||||
Note: this functionality is implicitly turned on when one of the
|
||||
following settings are not 0:
|
||||
MCUBOOT_SERIAL, MCUBOOT_HAVE_LOGGING, CRYPTO, HASH
|
||||
value: 0
|
||||
|
||||
syscfg.vals:
|
||||
SYSINIT_CONSTRAIN_INIT: 0
|
||||
OS_SCHEDULING: 0
|
||||
MSYS_1_BLOCK_COUNT: 0
|
||||
CONSOLE_COMPAT: 1
|
||||
Reference in New Issue
Block a user