Skip to content

Commit 50e9cde

Browse files
committed
Add data sharing feature demo
This commit adds a demonstration of MCUboot's data sharing feature to this repository. See mcu-tools/mcuboot#1115 for more details.
1 parent 8280fe6 commit 50e9cde

File tree

5 files changed

+213
-1
lines changed

5 files changed

+213
-1
lines changed

README.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,3 +366,70 @@ int ret = boot_set_confirmed();
366366
```
367367

368368
In real world situations, your application should run a self test routine to ensure it can receive updates in the future (eg: the UART software works as expected, the BLE stack initializes successfully, etc).
369+
370+
## Additional Features
371+
372+
MCUboot integrates a number of additional features that are commonly used in bootloader situations. More details of these features can be found in the mcuboot documentation/code.
373+
374+
The following sections detail how to enable and use these features in your Mbed-based bootloader/application.
375+
376+
### [Direct XIP](https://www.mcuboot.com/documentation/design/#direct-xip)
377+
378+
TODO
379+
380+
### [Shared Data (experimental)](https://www.mcuboot.com/documentation/design/#boot-data-sharing)
381+
382+
From the MCUboot documentation:
383+
384+
> MCUBoot defines a mechanism for sharing boot status information (also known as measured boot) and an interface for sharing application specific information with the runtime software.
385+
386+
The data shared from the bootloader to the application is stored in a reserved section of RAM in the form of type-length-value (TLV) encoded entries. This allows you to share any arbitrary data from the bootloader to the application.
387+
388+
#### Configuration
389+
390+
Several configuration changes must be made in both the bootloader and application to enable this feature. Namely:
391+
392+
- `mcuboot.share-data` must be configured to `true`
393+
- `mcuboot.share-data-base-address` must be set to a word-aligned memory address in RAM. Typically, a small section of RAM is reserved at the end of the MCU's RAM address space for data sharing, which brings us to the next configuration parameters
394+
- `mcuboot.share-data-size` is set to the number of bytes you want to reserve in RAM for the shared data
395+
- You must add the following entries to your `target.macros_add` configuration: `MBED_RAM_START=<address>` and `MBED_RAM_SIZE=<RAM size minus reserved region size>`.
396+
397+
`MBED_RAM_START` should be the starting address of RAM as per your MCU's datasheet. `MBED_RAM_SIZE` should be the total size of your MCU's RAM minus the number of bytes you are reserving for shared data. Note that the required reserved RAM depends on how many entries you want to share with the application.
398+
399+
As mentioned in the MCUboot documentation, the data share region has a global header that is 4 bytes. Each TLV entry has a header size of 4 bytes, plus the number of bytes required to store the data you are sharing.
400+
401+
Let's say you want to reserve 512 bytes of RAM for data sharing, your MCU has a RAM address space that starts at `0x20000000` and your MCU physically has 64kB of RAM. In this case, your configuration file will look as follows:
402+
403+
```
404+
[...]
405+
"mcuboot.share-data": true,
406+
"mcuboot.share-data-base-address": "0x2000FE00",
407+
"mcuboot.share-data-size": "0x200",
408+
"target.macros_add": ["MBED_RAM_START=0x20000000",
409+
"MBED_RAM_SIZE=0xFE00"],
410+
[...]
411+
```
412+
413+
Calculations to get the above:
414+
415+
`mcuboot.share-data-size = reserved_bytes = 512`
416+
`MBED_RAM_START = 0x20000000`
417+
`mcuboot.share-data-base-address = MBED_RAM_START + total_RAM - reserved_bytes = 0x20000000 + 64kB - 512 = 0x20000000 + 0x10000 - 0x200 = 0x2000FE00`
418+
`MBED_RAM_SIZE = total_RAM - reserved_bytes = 0x10000 - 0x200 = 0xFE00`
419+
420+
Note that you will have to add this configuration to both your bootloader and application builds. Setting `MBED_RAM_SIZE` prevents initialization code from clearing the reserved RAM region at startup, which would corrupt the shared data.
421+
422+
Also note that any RAM reserved for data sharing will be unavailable to the application stack/heap (TODO: fix this, better mbed-os integration), so try to keep your reserved region as small as possible.
423+
For an example configuration that is already setup for the nRF52840_DK target, see the `mbed_app_data_sharing.json` file in this repository.
424+
425+
**Note:** With Mbed CLI 1, you can use the `--app-config mbed_app_data_sharing.json` option flag to use the data sharing configuration without renaming any files. Building the bootloader and application with this flag will demonstrate usage of the data sharing feature.
426+
427+
#### Code Changes - Bootloader
428+
429+
With `mcuboot.shared-data` enabled, the MCUboot bootloader expects a C function to be defined called `boot_save_shared_data`. This function provides a hook for you to add custom TLVs to the shared data region as you require. For an example implementation, see the `shared_data.c/.h` files in this repository.
430+
431+
#### Code Changes - Application
432+
433+
Once you have shared data setup and configured for both the bootloader and application, you are ready to access the shared data. During boot, the bootloader will populate the shared data RAM region with the information as you specify in the `boot_save_shared_data` function. The application may then read that information back.
434+
435+
An example of how to do this is built into the `mbed-mcuboot-blinky` application when `mcuboot.share-data` is set to true.

mbed_app_data_sharing.json

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
{
2+
"requires": ["bare-metal", "mbedtls", "mcuboot", "flashiap-block-device", "mbed-trace", "qspif"],
3+
"config": {
4+
"serial-bootloader-enable": {
5+
"help": "Build bootloader with serial update support",
6+
"value": 0
7+
}
8+
},
9+
"target_overrides": {
10+
"*": {
11+
"target.restrict_size": "0x20000",
12+
"target.c_lib": "small",
13+
"mcuboot.log-level": "MCUBOOT_LOG_LEVEL_DEBUG",
14+
"mbed-trace.enable": false,
15+
"mbed-trace.fea-ipv6": false,
16+
"platform.crash-capture-enabled": false,
17+
"platform.minimal-printf-enable-64-bit": false,
18+
"platform.callback-comparable": false
19+
},
20+
"NRF52840_DK": {
21+
"target.features_remove": ["CRYPTOCELL310"],
22+
"target.macros_remove": ["MBEDTLS_CONFIG_HW_SUPPORT"],
23+
"mcuboot.primary-slot-address": "0x20000",
24+
"mcuboot.slot-size": "0xC0000",
25+
"mcuboot.scratch-address": "0xE0000",
26+
"mcuboot.scratch-size": "0x20000",
27+
"mcuboot.max-img-sectors": "0x180",
28+
"mcuboot.read-granularity": 4,
29+
"qspif.QSPI_MIN_PROG_SIZE": 4,
30+
"mcuboot.share-data": true,
31+
"mcuboot.share-data-base-address": "0x2003FC00",
32+
"mcuboot.share-data-size": "0x400",
33+
"target.macros_add": ["MBED_RAM_START=0x20000000",
34+
"MBED_RAM_SIZE=0x3FC00"]
35+
},
36+
"EP_AGORA": {
37+
"target.features_remove": ["CRYPTOCELL310"],
38+
"target.macros_remove": ["MBEDTLS_CONFIG_HW_SUPPORT"],
39+
"mcuboot.primary-slot-address": "0x20000",
40+
"mcuboot.slot-size": "0xC0000",
41+
"mcuboot.scratch-address": "0xE0000",
42+
"mcuboot.scratch-size": "0x20000",
43+
"mcuboot.max-img-sectors": "0x180",
44+
"mcuboot.read-granularity": 4,
45+
"qspif.QSPI_MIN_PROG_SIZE": 4
46+
},
47+
"DISCO_L475VG_IOT01A": {
48+
"mcuboot.primary-slot-address": "0x8020000",
49+
"mcuboot.slot-size": "0xC0000",
50+
"mcuboot.scratch-address": "0x80E0000",
51+
"mcuboot.scratch-size": "0x20000",
52+
"mcuboot.max-img-sectors": "0x180",
53+
"mcuboot.read-granularity": 1,
54+
"qspif.QSPI_MIN_PROG_SIZE": 1
55+
},
56+
"NRF52_DK": {
57+
"target.components_add": ["FLASHIAP"],
58+
"target.console-uart": false,
59+
"mcuboot.primary-slot-address": "0x20000",
60+
"mcuboot.slot-size": "0xC0000",
61+
"mcuboot.scratch-address": "0xE0000",
62+
"mcuboot.scratch-size": "0x20000",
63+
"mcuboot.max-img-sectors": "0x180",
64+
"mcuboot.read-granularity": 4,
65+
"target.device_has_remove": ["STDIO_MESSAGES"],
66+
"target.macros_add": ["MBED_FAULT_HANDLER_DISABLED"]
67+
}
68+
}
69+
}

mcuboot.lib

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
https://github.com/mcu-tools/mcuboot.git
1+
https://github.com/mcu-tools/mcuboot.git

shared_data.c

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* ep-oc-mcu
3+
* Embedded Planet Open Core for Microcontrollers
4+
*
5+
* Built with ARM Mbed-OS
6+
*
7+
* Copyright (c) 2019-2021 Embedded Planet, Inc.
8+
* SPDX-License-Identifier: Apache-2.0
9+
*
10+
* Licensed under the Apache License, Version 2.0 (the "License");
11+
* you may not use this file except in compliance with the License.
12+
* You may obtain a copy of the License at
13+
*
14+
* http://www.apache.org/licenses/LICENSE-2.0
15+
*
16+
* Unless required by applicable law or agreed to in writing, software
17+
* distributed under the License is distributed on an "AS IS" BASIS,
18+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19+
* See the License for the specific language governing permissions and
20+
* limitations under the License.
21+
*/
22+
23+
#include "boot_record.h"
24+
#include "shared_data.h"
25+
26+
#if MCUBOOT_DATA_SHARING && MCUBOOT_BOOTLOADER_BUILD
27+
28+
int boot_save_shared_data(const struct image_header *hdr,
29+
const struct flash_area *fap) {
30+
31+
char msg[] = "hello world :)";
32+
char second_msg[] = "abcdefghijklmnopqrstuvwxyz012345789";
33+
34+
int result = boot_add_data_to_shared_area(TLV_MAJOR_APP_SPECIFIC, TLV_MINOR_BOOTLOADER_VERSION, strlen(msg), msg);
35+
if(result) {
36+
return result;
37+
}
38+
39+
result = boot_add_data_to_shared_area(TLV_MAJOR_APP_SPECIFIC, TLV_MINOR_MY_MESSAGE, strlen(second_msg), second_msg);
40+
41+
return result;
42+
43+
}
44+
45+
#endif /* MCUBOOT_DATA_SHARING */
46+

shared_data.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Mbed-OS Microcontroller Library
3+
* Copyright (c) 2021 Embedded Planet
4+
* Copyright (c) 2021 ARM Limited
5+
* SPDX-License-Identifier: Apache-2.0
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License
18+
*/
19+
20+
#ifndef SHARED_DATA_H_
21+
#define SHARED_DATA_H_
22+
23+
/* See boot_status.h in mcuboot sources for more information
24+
* on the format of the tlv_type field of the shared data TLVs
25+
* The upper 4 bits are the major TLV category */
26+
#define TLV_MAJOR_APP_SPECIFIC 0xF
27+
#define TLV_MINOR_BOOTLOADER_VERSION 0x10D
28+
#define TLV_MINOR_MY_MESSAGE 0x10E
29+
30+
#endif /* SHARED_DATA_H_ */

0 commit comments

Comments
 (0)