Mbed-OS porting layer implementation for mcuboot
This PR provides a porting layer implementation and framework for building an mcuboot-based bootloader with Mbed-OS. Some symbols are not provided by the Mbed-OS port within mcuboot, namely:
- The secondary storage device (see below)
- The signing keys
- The encryption keys, if used
Use of this port is demonstrated by the following projects:
- https://github.com/AGlass0fMilk/mbed-mcuboot-demo (a complete mcuboot/Mbed-OS-based bootloader)
- https://github.com/AGlass0fMilk/mbed-mcuboot-blinky (example showing how to make an Mbed-OS application that is bootable by mcuboot)
Memory porting implementation:
The underlying implemenation uses Mbed's BlockDevice API as the storage backend for mcuboot's memory operations. This provides a very flexible way of configuring the location and layout of the secondary flash storage area. To build an mcuboot-based bootloader with Mbed-OS, the user must implement a hook function, mbed::BlockDevice* get_secondary_bd(), to provide the secondary BlockDevice that mcuboot will use.
The signing and encryption keys must also be provided by the user. They can be generated using the existing imgtool utility in the same manner used by Zephyr. There are no automated build steps currently provided by Mbed-OS to sign/encrypt build artifacts.
Known limitations:
The update candidate encryption features have not yet been fully tested. A truly secure implementation will require integration with Mbed's TRNG API in the future to inhibit side-channel attacks on the decryption process.
The TinyCrypt backend is currently only supported for Mbed-OS builds when building with the GCC toolchain. The new cmake-based Mbed-OS build system will fix the underlying issue (file name uniqueness).
Signed-off-by: George Beckstein <becksteing@embeddedplanet.com>
Signed-off-by: Evelyne Donnaes <evelyne.donnaes@arm.com>
Signed-off-by: Lingkai Dong <lingkai.dong@arm.com>
Co-authored-by: Lingkai Dong <lingkai.dong@arm.com>
Co-authored-by: Fabio Utzig <fabio.utzig@nordicsemi.no>
diff --git a/boot/mbed/include/flash_map_backend/flash_map_backend.h b/boot/mbed/include/flash_map_backend/flash_map_backend.h
new file mode 100644
index 0000000..8057baf
--- /dev/null
+++ b/boot/mbed/include/flash_map_backend/flash_map_backend.h
@@ -0,0 +1,148 @@
+/*
+ * Copyright (c) 2018 Nordic Semiconductor ASA
+ * Copyright (c) 2015 Runtime Inc
+ * Copyright (c) 2020 Embedded Planet
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * 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 Licens
+ */
+
+#ifndef H_UTIL_FLASH_MAP_
+#define H_UTIL_FLASH_MAP_
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+ *
+ * Provides abstraction of flash regions for type of use.
+ * I.e. dude where's my image?
+ *
+ * System will contain a map which contains flash areas. Every
+ * region will contain flash identifier, offset within flash and length.
+ *
+ * 1. This system map could be in a file within filesystem (Initializer
+ * must know/figure out where the filesystem is at).
+ * 2. Map could be at fixed location for project (compiled to code)
+ * 3. Map could be at specific place in flash (put in place at mfg time).
+ *
+ * Note that the map you use must be valid for BSP it's for,
+ * match the linker scripts when platform executes from flash,
+ * and match the target offset specified in download script.
+ */
+#include <inttypes.h>
+
+/**
+ * @brief Structure describing an area on a flash device.
+ *
+ * Multiple flash devices may be available in the system, each of
+ * which may have its own areas. For this reason, flash areas track
+ * which flash device they are part of.
+ */
+struct flash_area {
+ /**
+ * This flash area's ID; unique in the system.
+ */
+ uint8_t fa_id;
+
+ /**
+ * ID of the flash device this area is a part of.
+ */
+ uint8_t fa_device_id;
+
+ uint16_t pad16;
+
+ /**
+ * This area's offset, relative to the beginning of its flash
+ * device's storage.
+ */
+ uint32_t fa_off;
+
+ /**
+ * This area's size, in bytes.
+ */
+ uint32_t fa_size;
+};
+
+/**
+ * @brief Structure describing a sector within a flash area.
+ *
+ * Each sector has an offset relative to the start of its flash area
+ * (NOT relative to the start of its flash device), and a size. A
+ * flash area may contain sectors with different sizes.
+ */
+struct flash_sector {
+ /**
+ * Offset of this sector, from the start of its flash area (not device).
+ */
+ uint32_t fs_off;
+
+ /**
+ * Size of this sector, in bytes.
+ */
+ uint32_t fs_size;
+};
+
+/*
+ * Start using flash area.
+ */
+int flash_area_open(uint8_t id, const struct flash_area ** fapp);
+
+void flash_area_close(const struct flash_area * fap);
+
+/*
+ * Read/write/erase. Offset is relative from beginning of flash area.
+ */
+int flash_area_read(const struct flash_area * fap, uint32_t off, void *dst,
+ uint32_t len);
+int flash_area_write(const struct flash_area * fap, uint32_t off, const void *src,
+ uint32_t len);
+int flash_area_erase(const struct flash_area * fap, uint32_t off, uint32_t len);
+
+/*
+ * Alignment restriction for flash writes.
+ */
+uint8_t flash_area_align(const struct flash_area * fap);
+
+/*
+ * What is value is read from erased flash bytes.
+ */
+uint8_t flash_area_erased_val(const struct flash_area * fap);
+
+/*
+ * Given flash area ID, return info about sectors within the area.
+ */
+int flash_area_get_sectors(int fa_id, uint32_t *count,
+ struct flash_sector *sectors);
+
+
+int flash_area_id_from_image_slot(int slot);
+int flash_area_id_from_multi_image_slot(int image_index, int slot);
+
+
+int flash_area_id_to_image_slot(int area_id);
+/**
+ * Converts the specified flash area ID and image index (in multi-image setup)
+ * to an image slot index.
+ *
+ * Returns image slot index (0 or 1), or -1 if ID doesn't correspond to an image
+ * slot.
+ */
+int flash_area_id_to_multi_image_slot(int image_index, int area_id);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* H_UTIL_FLASH_MAP_ */
diff --git a/boot/mbed/include/flash_map_backend/secondary_bd.h b/boot/mbed/include/flash_map_backend/secondary_bd.h
new file mode 100644
index 0000000..c14d3ce
--- /dev/null
+++ b/boot/mbed/include/flash_map_backend/secondary_bd.h
@@ -0,0 +1,35 @@
+/*
+ * Copyright (c) 2020 Embedded Planet
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * 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 Licens
+ *
+ * Created on: Jul 30, 2020
+ * Author: gdbeckstein
+ */
+
+#ifndef MCUBOOT_BOOT_MBED_INCLUDE_FLASH_MAP_BACKEND_SECONDARY_BD_H_
+#define MCUBOOT_BOOT_MBED_INCLUDE_FLASH_MAP_BACKEND_SECONDARY_BD_H_
+
+#include "blockdevice/BlockDevice.h"
+
+/**
+ * This is implemented as a weak function and may be redefined
+ * by the application. The default case is to return the
+ * BlockDevice object returned by BlockDevice::get_default_instance();
+ *
+ * @retval secondary_bd Secondary BlockDevice where update candidates are stored
+ */
+mbed::BlockDevice* get_secondary_bd(void);
+
+#endif /* MCUBOOT_BOOT_MBED_INCLUDE_FLASH_MAP_BACKEND_SECONDARY_BD_H_ */
diff --git a/boot/mbed/include/mcuboot_config/mcuboot_assert.h b/boot/mbed/include/mcuboot_config/mcuboot_assert.h
new file mode 100644
index 0000000..8c8d52d
--- /dev/null
+++ b/boot/mbed/include/mcuboot_config/mcuboot_assert.h
@@ -0,0 +1,20 @@
+/*
+ * Copyright (c) 2018 Open Source Foundries Limited
+ *
+ * Copyright (c) 2020 Embedded Planet
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * 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 Licens
+ */
+
+#include "platform/mbed_assert.h"
diff --git a/boot/mbed/include/mcuboot_config/mcuboot_config.h b/boot/mbed/include/mcuboot_config/mcuboot_config.h
new file mode 100644
index 0000000..60a1bb4
--- /dev/null
+++ b/boot/mbed/include/mcuboot_config/mcuboot_config.h
@@ -0,0 +1,84 @@
+/*
+ * Copyright (c) 2018 Open Source Foundries Limited
+ * Copyright (c) 2019-2020 Arm Limited
+ * Copyright (c) 2019-2020 Linaro Limited
+ * Copyright (c) 2020 Embedded Planet
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+#ifndef __MCUBOOT_CONFIG_H__
+#define __MCUBOOT_CONFIG_H__
+
+/*
+ * For available configurations and their explanations,
+ * see mbed_lib.json.
+ */
+
+#define SIGNATURE_TYPE_RSA 0
+#define SIGNATURE_TYPE_EC256 1
+#define SIGNATURE_TYPE_ED25519 2
+
+/*
+ * Signature algorithm
+ */
+#if (MCUBOOT_SIGNATURE_ALGORITHM == SIGNATURE_TYPE_RSA)
+#define MCUBOOT_SIGN_RSA
+# if (MCUBOOT_RSA_SIGNATURE_LENGTH != 2048 && \
+ MCUBOOT_RSA_SIGNATURE_LENGTH != 3072)
+# error "Invalid RSA key size (must be 2048 or 3072)"
+# else
+# define MCUBOOT_SIGN_RSA_LEN MCUBOOT_RSA_SIGNATURE_LENGTH
+# endif
+#elif (MCUBOOT_SIGNATURE_ALGORITHM == SIGNATURE_TYPE_EC256)
+#define MCUBOOT_SIGN_EC256
+#elif (MCUBOOT_SIGNATURE_ALGORITHM == SIGNATURE_TYPE_ED25519)
+#define MCUBOOT_SIGN_ED25519
+#endif
+
+/*
+ * Crypto backend
+ */
+#define MBEDTLS 0
+#define TINYCRYPT 1
+
+#if (MCUBOOT_CRYPTO_BACKEND == MBEDTLS)
+#define MCUBOOT_USE_MBED_TLS
+#elif (MCUBOOT_CRYPTO_BACKEND == TINYCRYPT)
+/**
+ * XXX TinyCrypt is currently only supported in GCC builds
+ * See https://github.com/mcu-tools/mcuboot/pull/791#discussion_r515050672 for more information.
+ */
+#if !defined(__GNUC__)
+#error TinyCrypt is currently only supported in GCC builds for Mbed-OS.
+#endif
+#define MCUBOOT_USE_TINYCRYPT
+#endif
+
+/*
+ * Only one image (two slots) supported for now
+ */
+#define MCUBOOT_IMAGE_NUMBER 1
+
+/*
+ * Encrypted Images
+ */
+#if defined(MCUBOOT_ENCRYPT_RSA) || defined(MCUBOOT_ENCRYPT_EC256) || defined(MCUBOOT_ENCRYPT_X25519)
+#define MCUBOOT_ENC_IMAGES
+#endif
+
+/*
+ * Enabling this option uses newer flash map APIs. This saves RAM and
+ * avoids deprecated API usage.
+ */
+#define MCUBOOT_USE_FLASH_AREA_GET_SECTORS
+
+/*
+ * No watchdog integration for now
+ */
+#define MCUBOOT_WATCHDOG_FEED() \
+ do { \
+ } while (0)
+
+
+#endif /* __MCUBOOT_CONFIG_H__ */
diff --git a/boot/mbed/include/mcuboot_config/mcuboot_logging.h b/boot/mbed/include/mcuboot_config/mcuboot_logging.h
new file mode 100644
index 0000000..ef82ec9
--- /dev/null
+++ b/boot/mbed/include/mcuboot_config/mcuboot_logging.h
@@ -0,0 +1,86 @@
+/*
+ * Copyright (c) 2018 Nordic Semiconductor ASA
+ * Copyright (c) 2015 Runtime Inc
+ * Copyright (c) 2020 Cypress Semiconductor Corporation
+ * Copyright (c) 2020 Embedded Planet
+ * Copyright (c) 2020 ARM Limited
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * 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 Licens.
+ */
+
+#ifndef __MCUBOOT_LOGGING_H__
+#define __MCUBOOT_LOGGING_H__
+
+#define MCUBOOT_LOG_LEVEL_OFF 0
+#define MCUBOOT_LOG_LEVEL_ERROR 1
+#define MCUBOOT_LOG_LEVEL_WARNING 2
+#define MCUBOOT_LOG_LEVEL_INFO 3
+#define MCUBOOT_LOG_LEVEL_DEBUG 4
+
+/*
+ * The compiled log level determines the maximum level that can be
+ * printed.
+ */
+#ifndef MCUBOOT_LOG_LEVEL
+#define MCUBOOT_LOG_LEVEL MCUBOOT_LOG_LEVEL_OFF
+#endif
+
+#if MCUBOOT_LOG_LEVEL == MCUBOOT_LOG_LEVEL_OFF
+#define MBED_CONF_MBED_TRACE_ENABLE 0
+#else
+#define MBED_CONF_MBED_TRACE_ENABLE 1
+#define MCUBOOT_HAVE_LOGGING
+#endif
+
+#if MCUBOOT_LOG_LEVEL == MCUBOOT_LOG_LEVEL_ERROR
+#define MBED_TRACE_MAX_LEVEL TRACE_LEVEL_ERROR
+#elif MCUBOOT_LOG_LEVEL == MCUBOOT_LOG_LEVEL_WARNING
+#define MBED_TRACE_MAX_LEVEL TRACE_LEVEL_WARN
+#elif MCUBOOT_LOG_LEVEL == MCUBOOT_LOG_LEVEL_INFO
+#define MBED_TRACE_MAX_LEVEL TRACE_LEVEL_INFO
+#elif MCUBOOT_LOG_LEVEL == MCUBOOT_LOG_LEVEL_DEBUG
+#define MBED_TRACE_MAX_LEVEL TRACE_LEVEL_DEBUG
+#endif
+
+#define TRACE_GROUP "MCUb"
+#include "mbed_trace.h"
+
+#define MCUBOOT_LOG_MODULE_DECLARE(domain) /* ignore */
+#define MCUBOOT_LOG_MODULE_REGISTER(domain) /* ignore */
+
+#if MCUBOOT_LOG_LEVEL >= MCUBOOT_LOG_LEVEL_ERROR
+#define MCUBOOT_LOG_ERR tr_error
+#else
+#define MCUBOOT_LOG_ERR(...) IGNORE(__VA_ARGS__)
+#endif
+
+#if MCUBOOT_LOG_LEVEL >= MCUBOOT_LOG_LEVEL_WARNING
+#define MCUBOOT_LOG_WRN tr_warn
+#else
+#define MCUBOOT_LOG_WRN(...) IGNORE(__VA_ARGS__)
+#endif
+
+#if MCUBOOT_LOG_LEVEL >= MCUBOOT_LOG_LEVEL_INFO
+#define MCUBOOT_LOG_INF tr_info
+#else
+#define MCUBOOT_LOG_INF(...) IGNORE(__VA_ARGS__)
+#endif
+
+#if MCUBOOT_LOG_LEVEL >= MCUBOOT_LOG_LEVEL_DEBUG
+#define MCUBOOT_LOG_DBG tr_debug
+#else
+#define MCUBOOT_LOG_DBG(...) IGNORE(__VA_ARGS__)
+#endif
+
+#endif /* __MCUBOOT_LOGGING_H__ */
diff --git a/boot/mbed/include/os/os_malloc.h b/boot/mbed/include/os/os_malloc.h
new file mode 100644
index 0000000..3aa6f1e
--- /dev/null
+++ b/boot/mbed/include/os/os_malloc.h
@@ -0,0 +1 @@
+/** Not required for Mbed -- malloc calls are retargeted by the platform */
diff --git a/boot/mbed/include/sysflash/sysflash.h b/boot/mbed/include/sysflash/sysflash.h
new file mode 100644
index 0000000..e19945b
--- /dev/null
+++ b/boot/mbed/include/sysflash/sysflash.h
@@ -0,0 +1,14 @@
+/* Manual version of auto-generated version. */
+
+#ifndef __SYSFLASH_H__
+#define __SYSFLASH_H__
+
+#define PRIMARY_ID 0
+#define SECONDARY_ID 1
+#define SCRATCH_ID 2
+
+#define FLASH_AREA_IMAGE_PRIMARY(x) PRIMARY_ID
+#define FLASH_AREA_IMAGE_SECONDARY(x) SECONDARY_ID
+#define FLASH_AREA_IMAGE_SCRATCH SCRATCH_ID
+
+#endif /* __SYSFLASH_H__ */