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