blob: d40bb3d25b65babf2b608c79b7541dfa7c7b8304 [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
8* `mcuboot` requires that the target provides a `flash` API with ability to
Fabio Utzige2d99f82017-06-28 19:33:33 -03009 get the flash's minimum write size, and read/write/erase individual sectors.
Fabio Utzig01ccb192017-05-08 09:17:50 -030010
11* `mcuboot` doesn't bundle a cryptographic library, which means the target
12 OS must already have it bundled. The supported libraries at the moment are
Fabio Utzige2d99f82017-06-28 19:33:33 -030013 either `mbed TLS` or the set `tinycrypt` + `mbed TLS` (where `mbed TLS` is
14 used to provide functionality not existing in `tinycrypt`).
Fabio Utzig01ccb192017-05-08 09:17:50 -030015
Fabio Utzig01ccb192017-05-08 09:17:50 -030016# Steps to port
17
Fabio Utzig01ccb192017-05-08 09:17:50 -030018## Main app and calling the bootloader
19
20From the perspective of the target OS, the bootloader can be seen as a library,
21so an entry point must be provided. This is likely a typical `app` for the
22target OS, and it must call the following function to run the bootloader:
23
24```c
25int boot_go(struct boot_rsp *rsp);
26```
27
28This function is located at `boot/bootutil/loader.c` and receives a `struct
29boot_rsp` pointer. The `struct boot_rsp` is defined as:
30
31```c
32struct boot_rsp {
33 /** A pointer to the header of the image to be executed. */
34 const struct image_header *br_hdr;
35
36 /**
37 * The flash offset of the image to execute. Indicates the position of
38 * the image header.
39 */
40 uint8_t br_flash_id;
41 uint32_t br_image_addr;
42};
43```
44
45After running the management functions of the bootloader, `boot_go` returns
46an initialized `boot_rsp` which has pointers to the location of the image
47where the target firmware is located which can be used to jump to.
48
49## Flash access and flash Map
50
51* Regarding flash access the bootloader has two requirements:
52
53### hal_flash_align
54
Fabio Utzige2d99f82017-06-28 19:33:33 -030055`mcuboot` needs to know the write size (and alignment) of the flash. To get
56this information it calls `hal_flash_align`.
Fabio Utzig01ccb192017-05-08 09:17:50 -030057
Fabio Utzig41d18852017-07-10 15:27:00 -030058`uint8_t hal_flash_align(uint8_t flash_id);`
Fabio Utzig01ccb192017-05-08 09:17:50 -030059
60### flash_map
61
62The bootloader requires a `flash_map` to be able to know how the flash is
Fabio Utzige2d99f82017-06-28 19:33:33 -030063partitioned. A `flash_map` consists of `struct flash_area` entries
64specifying the partitions, where a `flash_area` defined as follows:
Fabio Utzig01ccb192017-05-08 09:17:50 -030065
66```c
67struct flash_area {
68 uint8_t fa_id; /** The slot/scratch identification */
69 uint8_t fa_device_id; /** The device id (usually there's only one) */
70 uint16_t pad16;
71 uint32_t fa_off; /** The flash offset from the beginning */
72 uint32_t fa_size; /** The size of this sector */
73};
74```
75
76`fa_id` is can be one of the following options:
77
78```c
79#define FLASH_AREA_IMAGE_0 1
80#define FLASH_AREA_IMAGE_1 2
81#define FLASH_AREA_IMAGE_SCRATCH 3
82```
83
84The functions that must be defined for working with the `flash_area`s are:
85
86```c
87/*< Opens the area for use. id is one of the `fa_id`s */
88int flash_area_open(uint8_t id, const struct flash_area **);
89void flash_area_close(const struct flash_area *);
90/*< Reads `len` bytes of flash memory at `off` to the buffer at `dst` */
91int flash_area_read(const struct flash_area *, uint32_t off, void *dst,
92 uint32_t len);
93/*< Writes `len` bytes of flash memory at `off` from the buffer at `src` */
94int flash_area_write(const struct flash_area *, uint32_t off,
95 const void *src, uint32_t len);
96/*< Erases `len` bytes of flash memory at `off` */
97int flash_area_erase(const struct flash_area *, uint32_t off, uint32_t len);
98/*< Returns this `flash_area`s alignment */
99uint8_t flash_area_align(const struct flash_area *);
Fabio Utzig41d18852017-07-10 15:27:00 -0300100/*< Initializes an array of flash_area elements for the slot's sectors */
Fabio Utzig01ccb192017-05-08 09:17:50 -0300101int flash_area_to_sectors(int idx, int *cnt, struct flash_area *ret);
102/*< Returns the `fa_id` for slot, where slot is 0 or 1 */
103int flash_area_id_from_image_slot(int slot);
104/*< Returns the slot, for the `fa_id` supplied */
105int flash_area_id_to_image_slot(int area_id);
106```
107
108## Memory management for mbed TLS
109
110`mbed TLS` employs dynamic allocation of memory, making use of the pair
111`calloc/free`. If `mbed TLS` is to be used for crypto, your target RTOS
112needs to provide this pair of function.
113
114To configure the what functions are called when allocating/deallocating
115memory `mbed TLS` uses the following call [^cite1]:
116
117```
118int mbedtls_platform_set_calloc_free (void *(*calloc_func)(size_t, size_t),
119 void (*free_func)(void *));
120```
121
122If your system already provides functions with compatible signatures, those
123can be used directly here, otherwise create new functions that glue to
124your `calloc/free` implementations.
125
126[^cite1]```https://tls.mbed.org/api/platform_8h.html```