blob: 5a98a784d47716a5c93562f73cba97d66a9ddf5f [file] [log] [blame]
David Brown49aa8862017-02-02 09:17:02 -07001Building and using mcuboot with Zephyr
2######################################
3
4*mcuboot* began its life as the bootloader for Mynewt. It has since
5aquired the ability to be used as a bootloader for Zephyr as well.
6There are some pretty significant differences in how apps are built
7for Zephyr, and these are documented here.
8
9Please see ``boot/bootutil/design.txt`` for documentation on the
10design and operation of the bootloader itself. This functionality
11should be the same between Mynewt and Zephyr
12
13Building the bootloader itself
14==============================
15
16The bootloader is an ordinary Zephyr application, at least from
17Zephyr's point of view. There is a bit of configuration that needs to
18be made before building it. Most of this is done in the top-level
19``Makefile`` in the source tree. There are comments there for
20guidance. It is important to select a signature algorithm, and decide
21if slot0 should be validated on every boot.
22
23There is a ``build_boot.sh`` script at the top level that can make
24building a bit easier. It assumes that the mcuboot tree is next to,
25at the same level, as the zephyr source tree. It takes a single
26argument, which is the target to build. This must match one of the
Carles Cufid7838092017-09-01 13:39:33 +020027targets in ``boot/zephyr/targets`` to be a supported board. Additionally,
28the Zephyr Device Tree board definition in ``dts/<arch>/<board>.dts`` must
29contain the flash partitions description.
David Brown49aa8862017-02-02 09:17:02 -070030
31Once this is finished building, the bootloader should reside in
Carles Cufid7838092017-09-01 13:39:33 +020032``outdir/targname/zephyr.[bin|hex]``. Use the flashing tools you have to
David Brown49aa8862017-02-02 09:17:02 -070033install this image at the beginning of the flash.
34
35Building Applications for the bootloader
36========================================
37
38In order build an application to be used within the bootloader, there
39are a few configuration changes that need to be made to it (typically
40in the app's prj.conf).
41
42- ``CONFIG_TEXT_SECTION_OFFSET`` must be set to allow room for the
43 boot image header. It must also be aligned to a boundary that the
44 particular MCU requires the vector table to be aligned on. This is
45 dependent upon the particular board you have chosen. Starting with
46 0x200 is a good way to start, since all of the boards will work with
47 this alignment.
48
49- ``CONFIG_FLASH_BASE_ADDRESS`` must be set to the base address in
50 flash where the SLOT0 lives. This should match the value found in
51 ``boot/zephyr/target/*.h`` for your target, for
52 ``FLASH_AREA_IMAGE_0_OFFSET``. Note that some targets build for a
53 higher-than-zero flash address, and this should be compensated for
54 when setting this value. It should generally be set to a small
55 amount larger than its initial value.
56
57With this, build the application as your normally would.
58
59Signing the application
60-----------------------
61
62In order to upgrade to an image (or even boot it, if
Fabio Utzig19356bf2017-05-11 16:19:36 -030063``MCUBOOT_VALIDATE_SLOT0`` is enabled), the images must be signed.
David Brown49aa8862017-02-02 09:17:02 -070064To make development easier, mcuboot is distributed with some example
65keys. It is important to stress that these should never be used for
66production, since the private key is publically available in this
67repository. See below on how to make your own signatures.
68
69There is a ``sign.sh`` script that gives some examples of how to make
70these signatures.
71
72Flashing the application
73------------------------
74
75The application itself can flashed with regular flash tools, but will
76need to be loaded at the offset of SLOT-0 for this particular target.
77These images can also be marked for upgrade, and loaded into SLOT-1,
78at which point the bootloader should perform an upgrade. It is up to
79the image to mark slot-0 as "image ok" before the next reboot,
80otherwise the bootloader will revert the application.
81
82Managing signing keys
83=====================
84
85The signing keys used by mcuboot are represented in standard formats,
86and can be generated and processed using conventional tools. However,
87the Mynewt project has developed some tools to make this easier, and
88the ``imgtool`` directory contains a small program to use these tools,
89as well as some additional tools for generating and extracting public
90keys. If you will be using your own keys, it is recommended to build
91this tool following the directions within the directory.
92
93Generating a new keypair
94------------------------
95
96Generating a keypair with imgtool is a matter of running the keygen
97subcommand::
98
99 $ imgtool keygen -k mykey.pem -t rsa-2048
100
101The argument to ``-t`` should be the desired key type. See the
102imgtool README.rst for more details on the possible keytypes.
103
104Extracting the public key
105-------------------------
106
107The generated keypair above contains both the public and the private
108key. It is necessary to extract the public key and insert it into the
109bootloader. The keys live in ``boot/zephyr/keys.c``, and can be
110extracted using imgtool::
111
112 $ imgtool getpub -k mykey.pem
113
114This will output the public key as a C array that can be dropped
115directly into the ``keys.c`` file.
116
117Once this is done, this new keypair file (``mykey.pem`` in this
118example) can be used to sign images.