Commit Graph

1138 Commits

Author SHA1 Message Date
Marti Bolivar c50926f3c1 bootutil: optionally use flash_area_get_sectors()
Add a typedef which lets us use flash_sector or flash_area to contain
the sectors within the boot_data global. When
MCUBOOT_USE_FLASH_AREA_GET_SECTORS is defined, this is struct
flash_sector.

Also add struct boot_loader_state accessors to handle this case, and
make the appropriate changes to where the sectors are allocated to use
the new typedef.

Finally, ensure MCUBOOT_USE_FLASH_AREA_GET_SECTORS is defined in the
Zephyr Makefile, since flash_area_get_sectors() is already provided
there.

This lets mcuboot users convert to the new flash API gradually.

Signed-off-by: Marti Bolivar <marti.bolivar@linaro.org>
2017-06-15 13:24:15 -04:00
Marti Bolivar e10a7399d9 bootutil: add accessor for write_sz
This finishes making the struct contents opaque.

Signed-off-by: Marti Bolivar <marti.bolivar@linaro.org>
2017-06-15 13:24:15 -04:00
Marti Bolivar 135b8f6009 bootutil: consolidate helpers for old flash API
Move helpers which are needed for using flash_area_to_sectors() to the
end of file. This is just to keep things clean when we add support for
flash_area_get_sectors().

Signed-off-by: Marti Bolivar <marti.bolivar@linaro.org>
2017-06-15 13:24:15 -04:00
Marti Bolivar 8489865cb1 bootutil: tweak state representation
Make num_sectors a size_t in struct boot_loader_state.

Signed-off-by: Marti Bolivar <marti.bolivar@linaro.org>
2017-06-15 13:24:15 -04:00
Marti Bolivar c0b47916f5 bootutil: keep image areas open while bootloading
Keep both image areas and the scratch area open while we are
bootloading. This fixes up a hack and will make it easier to use
flash_area_get_sectors() later.

Signed-off-by: Marti Bolivar <marti.bolivar@linaro.org>
2017-06-15 13:24:15 -04:00
Marti Bolivar ea08887858 bootutil: add image and area offset abstractions
Add abstractions for calculating the starting offset of a sector from
the beginning of its image, and the starting offset of an image slot
from the beginning of its flash device.

Using this tweaks a check in boot_swap_sectors(), but doesn't change
its outcome.

Signed-off-by: Marti Bolivar <marti.bolivar@linaro.org>
2017-06-15 13:24:15 -04:00
Marti Bolivar cca28a9a56 bootutil: add abstraction for area initialization
Add an abstraction for initializing bootloader state for a particular
flash area.

For now, we preserve some existing hacky behavior related to the
scratch area. This will get cleaned up more later in the series.

Signed-off-by: Marti Bolivar <marti.bolivar@linaro.org>
2017-06-15 13:24:15 -04:00
Marti Bolivar d3269fd67c bootutil: add accessors for sector size and number
Taking the opportunity to move some signed integers over to unsigned
size_t as we go. (Depending on compiler settings, signed / unsigned
comparisons can generate warnings, so it's nice to use the signedness
we mean when possible).

Having boot_img_set_num_sectors() is just an intermediate step; this
gets cleaned up as we go forward.

Signed-off-by: Marti Bolivar <marti.bolivar@linaro.org>
2017-06-15 13:24:14 -04:00
Marti Bolivar e258715a2a bootutil: add flash device ID accessor
Signed-off-by: Marti Bolivar <marti.bolivar@linaro.org>
2017-06-15 13:24:14 -04:00
Marti Bolivar f804f62e1f bootutil: add private image header accessor
Signed-off-by: Marti Bolivar <marti.bolivar@linaro.org>
2017-06-15 13:24:14 -04:00
Marti Bolivar 9b1f8bb553 bootutil: move loader state definition to private header
This begins some preparatory work to using flash_area_get_sectors() in
loader.c. Subsequent commits will add and use additional accessors for
the contents of this struct.

Making the struct contents opaque will allow it to contain struct
flash_area or struct flash_sector values in its sectors field. This
will allow use of either flash_area_get_sectors() or the
now-deprecated flash_area_to_sectors().

Signed-off-by: Marti Bolivar <marti.bolivar@linaro.org>
2017-06-15 13:24:14 -04:00
Marti Bolivar 1c0ddca7b1 zephyr: implement flash_area_get_sectors()
Support the new flash API which better maps to the underlying
hardware.

Signed-off-by: Marti Bolivar <marti.bolivar@linaro.org>
2017-06-15 13:24:14 -04:00
Marti Bolivar ed2eaf1498 zephyr: implement flash_device_base()
Implement new flash API which allows users to learn the physical base
addresses of flash devices.

Signed-off-by: Marti Bolivar <marti.bolivar@linaro.org>
2017-06-14 17:07:54 -04:00
Marti Bolivar a2e1b03d09 flash_map: declare flash_area_get_sectors()
The current flash_map.h API treats flash areas and flash sectors
synonymously.  This isn't accurate; a flash area comprises one or more
sectors.

To distinguish them, add a new struct flash_sector, and a new
flash_area_get_sectors() which initializes an array of struct
flash_sector instead of struct flash area.

That done, deprecate flash_area_to_sectors().

Signed-off-by: Marti Bolivar <marti.bolivar@linaro.org>
2017-06-14 17:07:54 -04:00
Marti Bolivar d5bf57015c flash_map: declare flash_device_base()
This accessor returns the physical base address of a flash device,
given its ID. This will be used to support flash devices with nonzero
base addresses in mcuboot.

Signed-off-by: Marti Bolivar <marti.bolivar@linaro.org>
2017-06-14 17:07:54 -04:00
Marti Bolivar f0d08f23a1 flash_map: add docstrings
Add Doxygen docstrings to struct flash_area fields.

Signed-off-by: Marti Bolivar <marti.bolivar@linaro.org>
2017-06-14 17:07:54 -04:00
David Brown 6998fd5b9a doc: Fix 'pip' example
The imgtool script explicitly calls out python3.  Adjust 'pip' to
'pip3' to reflect this explicit version call as well.
2017-06-14 11:27:45 -04:00
David Brown cdb968f70a rsa: Allow RSA key to be larger than we need
Allow the size of bignums in mbed TLS to be configured larger than
needed for RSA2048.  This will waste memory holding the large numbers,
but will still work.
2017-06-13 16:02:48 -04:00
David Brown f4e904d4be rsa: Add support for RSA-PSS
The PKCS#1 standards, which define RSA signatures, are currently at
version 2.2.  Starting in v2.1, the standard defines a new signature
method RSA-PSS, which has a stronger security proof than the signature
method used in earlier versions.  The standard recommends that RSA-PSS
be used in new designs, instead of the older algorithm.

This patch implements RSA-PSS verification for a specific set of
parameters:

    - RSA-2048
    - SHA256 for both the message digest and the internal hash
    - 32-byte salt
    - 2047 bit message

Although RSA-PSS supports other parameters, due to size constraints,
this verificatino code only supports these specific parameters, and
signatures with other parameters will be considered invalid.

To encourage the use of the more secure algorithm, the default build
configuration is RSA-PSS.  BOOTUTIL_RSA_PKCS1_15 needs to be defined in
order to support the older signature algorithm.
2017-06-13 16:02:48 -04:00
David Brown 85d879f195 Merge pull request #60 from d3zd3z/pr/py-header
Make header padding optional
2017-06-13 15:59:27 -04:00
David Brown adce57d497 Merge pull request #59 from d3zd3z/pr/py-nosign
Add support for "signing" without a signature
2017-06-13 15:58:34 -04:00
David Brown 2c21f7101b imgtool: Make "included header" optional
The imgtool.py program has been assuming that the input image for
signing has a zero padded place for the header at the beginning of the
image.  This is only true for some platforms.

Instead, make this included header space optional.  By default, prepend
the header to the image.  If `--included-header` is specified to the
sign command, consider the bytes at the beginning of the image to be
padded space for the header.  This option is required for Zephyr builds.
2017-06-13 15:54:45 -04:00
David Brown 0f0c6a808d imgtool: Allow --key to be optional when signing
If the --key is not specified, only the SHA256 hash is added to the TLV.
This is useful for testing configurations, where the crypto has not been
fully configured.  Note that this configuration is not secure, and this
only verifies that the image has not been corrupted.
2017-06-13 15:45:06 -04:00
Fabio Utzig b119424aa8 Merge pull request #64 from utzig/fix-build-config-leftovers
Fix some leftovers after mynewt move (PR #52)
2017-06-13 13:16:04 -03:00
David Brown d6369707b9 Merge pull request #63 from utzig/fix-tinycrypt-dependency
Fix importing tinycrypt dependency on mynewt
2017-06-13 12:14:13 -04:00
David Brown 07bfc193b6 Merge pull request #53 from d3zd3z/pr/py
scripts: Initial version of Python imgtool
2017-06-13 12:11:21 -04:00
David Brown 9ebd1f5a38 Merge pull request #51 from d3zd3z/pr/logging
sim: Allow Rust logging level to control C logging
2017-06-13 12:09:26 -04:00
Fabio Utzig 3488eef598 Fix some leftovers after mynewt move (PR #52)
- caps.c was now updated (missed this one!).
- *_OVERWRITE_ONLY flag was now also updated.
2017-06-12 10:25:43 -03:00
Fabio Utzig 32befd5cb4 Fix importing tinycrypt dependency on mynewt 2017-06-12 10:08:43 -03:00
Fabio Utzig f6633ad747 Merge pull request #62 from utzig/fix-missing-mynewt-includes
Fix broken Mynewt BOOTUTIL_ -> MCUBOOT_ mapping
2017-06-10 08:12:10 -03:00
Fabio Utzig eed80b65a0 Fix broken Mynewt BOOTUTIL_ -> MCUBOOT_ mapping
Fix mapping of syscfg.yml BOOTUTIL_* vars to C #defines, which was broken
on most .c files for lacking inclusion of mynewt "config.h"
2017-06-10 08:03:05 -03:00
David Brown efb871f4b2 imgtool: Better version parsing
When parsing versions for the --version argument, allow a field to be
zero.  Also, restrict the build to just an integer to match what we
allow (rather than allow alphabetic, and then failing to parse as an
integer).

In addition, add the missing import of argparse, so that when the
version is invalid, we get nice usage rather than an error about a
missing module.

Jira: MCUB-58
2017-06-08 09:43:55 -06:00
David Brown e369fecbf1 doc: Add documentation for imgtool.py 2017-06-07 09:35:48 -06:00
David Brown 4c036158d4 imgtool: Fix key argument
Changing the name of the command line argument changes the name of the
fields used to access it.  Change the keysigning code to reflect this,
avoiding a stack dump when trying to generate keys.
2017-06-07 09:19:21 -06:00
David Brown 07916c3155 imgtool.py: Support PKCS#1.5 v2.1 RSA-PSS
Add support for the RSA-PSS signature algorithm to imgtool.py.  This
algorithm has a strong security proof, and is recommended for all new
designs.  The new algorithm is enabled by default for RSA signatures to
match the default in the bootloader also being changed.
2017-06-07 09:19:21 -06:00
David Brown 23f91ad152 scripts: Initial version of Python imgtool
This is the start of a python implementation of imgtool.  This
implements all of the functionality that was missing in the zep2newt.py
tool, namely creation of keypairs, and converting the public version of
these keys into C code.
2017-06-07 09:19:21 -06:00
David Brown 38e6a1637d Merge pull request #52 from utzig/decouple-mynewt
Make mcuboot less mynewt style
2017-06-06 10:24:06 -06:00
Fabio Utzig 19356bf45f Make mcuboot less mynewt style
Remove most of mynewt specific stuff to a separate port package. This
should make mcuboot less "mynewt'y" and slightly easier to port to.

- Mynewt specific stuff moved to boot/mynewt.
- Sample app moved from apps/boot to boot/mynewt.
- Use MYNEWT_VAL macro only on mynewt port.
- BOOTUTIL_* and MYNEWT_VAL() usage moved to MCUBOOT_ defines.
2017-06-02 10:55:27 -07:00
David Brown df3c3da4e7 Merge pull request #41 from utzig/update-status-mgmt
Update to status management
2017-05-30 16:57:01 -06:00
Fabio Utzig 9678c97128 Add comment about trailer swap logic 2017-05-24 09:16:15 -04:00
Fabio Utzig de8a38abb3 Use better naming for flags 2017-05-24 09:16:15 -04:00
Fabio Utzig 34e393ee2d Remove newline from log message 2017-05-24 09:16:15 -04:00
Fabio Utzig acfba2e86e Fix for boot_read_swap_state_by_id to close flash area on errors 2017-05-24 09:16:15 -04:00
Fabio Utzig 856f783d7d Fix some error return codes 2017-05-24 09:16:14 -04:00
Fabio Utzig 94d998c61b Remove special cases, non needed tests 2017-05-24 09:16:14 -04:00
Fabio Utzig 4cee4f758f Move trailer status size calculation to its own function 2017-05-24 09:16:14 -04:00
Fabio Utzig ba49f84f03 Add macros for image ok values 2017-05-24 09:16:14 -04:00
Fabio Utzig 2473ac0e5b Add support for swapping only fw sectors in use
Before this patch, the swapping would process all sectors in a slot
not matter what the size of the binary firmware was. This changes the
swap process to swap only sectors that are in use by firmware.

Also, if the last slot sector, which stores the trailer, is actually not
in use by the binary firmware, now trailer is never written to scratch.
`use_scratch` temp variable was added to boot_status struct to control
this (this var is never written to disk).

Random other small refactorings were applied.
2017-05-24 08:58:59 -04:00
Fabio Utzig 7ebb7c23d9 Update how trailer is handled during a swap
Before this change, trailer was handled as part of the binary image,
which during a swap was just copied around together with the image.
This had issues if some fault happened while the trailer copy was
underway.

This patch changes how trailer is handled by making by non-copying.
The trailer is now updated step-by-step based on the current status.
Magic, copy_done and image_ok are also handled by writing them
individually, not by copying.

The trailer on scratch area was reduced to include at most swap state for
one sector, since it is only used temporarily while erasing the last
sector of the slot that stores the final trailer.

Many other small fixes were applied.
2017-05-24 08:45:53 -04:00
David Brown 1d9f185b62 Revert "Merge pull request #49 from mbolivar/flash-map-updates"
This reverts commit a52d7a21f0, reversing
changes made to d21abaaa35.

Premature merge.  Will bring this back in after #41 merges.
2017-05-23 10:32:22 -06:00