blob: b870bdcae54e44a909076abc53b22c21d69a4de1 [file] [log] [blame] [view]
Fabio Utzig01ccb192017-05-08 09:17:50 -03001# Porting How-To
2
3This document describes the requirements and necessary steps required to port
4`mcuboot` to a new target `OS`.
5
6# Requirements
7
Marti Bolivarf91bca52018-04-12 12:40:46 -04008* `mcuboot` requires a configuration file, which can be included as
9 mcuboot_config/mcuboot_config.h, which configures various options
10 (that begin with MCUBOOT_).
11
Fabio Utzig01ccb192017-05-08 09:17:50 -030012* `mcuboot` requires that the target provides a `flash` API with ability to
Fabio Utzige2d99f82017-06-28 19:33:33 -030013 get the flash's minimum write size, and read/write/erase individual sectors.
Fabio Utzig01ccb192017-05-08 09:17:50 -030014
15* `mcuboot` doesn't bundle a cryptographic library, which means the target
16 OS must already have it bundled. The supported libraries at the moment are
Fabio Utzige2d99f82017-06-28 19:33:33 -030017 either `mbed TLS` or the set `tinycrypt` + `mbed TLS` (where `mbed TLS` is
18 used to provide functionality not existing in `tinycrypt`).
Fabio Utzig01ccb192017-05-08 09:17:50 -030019
Fabio Utzig01ccb192017-05-08 09:17:50 -030020# Steps to port
21
Fabio Utzig01ccb192017-05-08 09:17:50 -030022## Main app and calling the bootloader
23
24From the perspective of the target OS, the bootloader can be seen as a library,
25so an entry point must be provided. This is likely a typical `app` for the
26target OS, and it must call the following function to run the bootloader:
27
28```c
29int boot_go(struct boot_rsp *rsp);
30```
31
32This function is located at `boot/bootutil/loader.c` and receives a `struct
33boot_rsp` pointer. The `struct boot_rsp` is defined as:
34
35```c
36struct boot_rsp {
37 /** A pointer to the header of the image to be executed. */
38 const struct image_header *br_hdr;
39
40 /**
41 * The flash offset of the image to execute. Indicates the position of
42 * the image header.
43 */
44 uint8_t br_flash_id;
45 uint32_t br_image_addr;
46};
47```
48
49After running the management functions of the bootloader, `boot_go` returns
50an initialized `boot_rsp` which has pointers to the location of the image
51where the target firmware is located which can be used to jump to.
52
Marti Bolivarf91bca52018-04-12 12:40:46 -040053## Configuration file
54
55You must provide a file, mcuboot_config/mcuboot_config.h. This is
56included by several files in the "library" portion of MCUboot; it
57provides preprocessor definitions that configure the library's
58build.
59
60See the file samples/mcuboot_config/mcuboot_config.template.h for a
61starting point and more information. This is a good place to convert
62settings in your environment's configuration system to those required
63by MCUboot. For example, Mynewt uses MYNEWT_VAL() and Zephyr uses
64Kconfig; these configuration systems are converted to MCUBOOT_ options
65in the following files:
66
67- boot/zephyr/include/mcuboot_config/mcuboot_config.h
68- boot/mynewt/mcuboot_config/include/mcuboot_config/mcuboot_config.h
69
Fabio Utzig01ccb192017-05-08 09:17:50 -030070## Flash access and flash Map
71
72* Regarding flash access the bootloader has two requirements:
73
74### hal_flash_align
75
Fabio Utzige2d99f82017-06-28 19:33:33 -030076`mcuboot` needs to know the write size (and alignment) of the flash. To get
77this information it calls `hal_flash_align`.
Fabio Utzig01ccb192017-05-08 09:17:50 -030078
Fabio Utzig41d18852017-07-10 15:27:00 -030079`uint8_t hal_flash_align(uint8_t flash_id);`
Fabio Utzig01ccb192017-05-08 09:17:50 -030080
81### flash_map
82
83The bootloader requires a `flash_map` to be able to know how the flash is
Fabio Utzige2d99f82017-06-28 19:33:33 -030084partitioned. A `flash_map` consists of `struct flash_area` entries
85specifying the partitions, where a `flash_area` defined as follows:
Fabio Utzig01ccb192017-05-08 09:17:50 -030086
87```c
88struct flash_area {
89 uint8_t fa_id; /** The slot/scratch identification */
90 uint8_t fa_device_id; /** The device id (usually there's only one) */
91 uint16_t pad16;
92 uint32_t fa_off; /** The flash offset from the beginning */
93 uint32_t fa_size; /** The size of this sector */
94};
95```
96
97`fa_id` is can be one of the following options:
98
99```c
100#define FLASH_AREA_IMAGE_0 1
101#define FLASH_AREA_IMAGE_1 2
102#define FLASH_AREA_IMAGE_SCRATCH 3
103```
104
105The functions that must be defined for working with the `flash_area`s are:
106
107```c
108/*< Opens the area for use. id is one of the `fa_id`s */
109int flash_area_open(uint8_t id, const struct flash_area **);
110void flash_area_close(const struct flash_area *);
111/*< Reads `len` bytes of flash memory at `off` to the buffer at `dst` */
112int flash_area_read(const struct flash_area *, uint32_t off, void *dst,
113 uint32_t len);
114/*< Writes `len` bytes of flash memory at `off` from the buffer at `src` */
115int flash_area_write(const struct flash_area *, uint32_t off,
116 const void *src, uint32_t len);
117/*< Erases `len` bytes of flash memory at `off` */
118int flash_area_erase(const struct flash_area *, uint32_t off, uint32_t len);
119/*< Returns this `flash_area`s alignment */
120uint8_t flash_area_align(const struct flash_area *);
Fabio Utzig41d18852017-07-10 15:27:00 -0300121/*< Initializes an array of flash_area elements for the slot's sectors */
Fabio Utzig01ccb192017-05-08 09:17:50 -0300122int flash_area_to_sectors(int idx, int *cnt, struct flash_area *ret);
123/*< Returns the `fa_id` for slot, where slot is 0 or 1 */
124int flash_area_id_from_image_slot(int slot);
125/*< Returns the slot, for the `fa_id` supplied */
126int flash_area_id_to_image_slot(int area_id);
127```
128
129## Memory management for mbed TLS
130
131`mbed TLS` employs dynamic allocation of memory, making use of the pair
132`calloc/free`. If `mbed TLS` is to be used for crypto, your target RTOS
133needs to provide this pair of function.
134
135To configure the what functions are called when allocating/deallocating
136memory `mbed TLS` uses the following call [^cite1]:
137
138```
139int mbedtls_platform_set_calloc_free (void *(*calloc_func)(size_t, size_t),
140 void (*free_func)(void *));
141```
142
143If your system already provides functions with compatible signatures, those
144can be used directly here, otherwise create new functions that glue to
145your `calloc/free` implementations.
146
147[^cite1]```https://tls.mbed.org/api/platform_8h.html```