Commit Graph

174 Commits

Author SHA1 Message Date
David Brown 8d0afa737c boot: bootutil add cap for slot 0 validation
Add a capability to query if MCUboot has been built to verify slot 0.

Signed-off-by: David Brown <david.brown@linaro.org>
2019-01-12 14:35:54 -07:00
David Brown c4a60a3686 boot: bootutil: Add caps for encryption
Add a capability query for the two encryption options.

Signed-off-by: David Brown <david.brown@linaro.org>
2019-01-12 14:35:54 -07:00
Emanuele Di Santo 9f1933d1a5 boot: zephyr: migrate to new log subystem
The old log subsystem has been deprecated in Zephyr.
Migrate to the new subsystem to avoid compilation warnings.

In-place log processing is selected as it is required as MCUBoot is
one thread application.

Signed-off-by: Emanuele Di Santo <emdi@nordicsemi.no>
Signed-off-by: Andrzej Puzdrowski <andrzej.puzdrowski@nordicsemi.no>
2019-01-10 19:11:15 +01:00
Emanuele Di Santo 2733f515cd boot: bootutil: remove unused BOOT_LOG_LEVEL macros
Remove unnused BOOT_LOG_LEVEL macros.
These are not honored, nor usable since they are supposed to be
defined before including the header in which they are defined.

Signed-off-by: Emanuele Di Santo <emdi@nordicsemi.no>
2019-01-10 19:11:15 +01:00
Emanuele Di Santo 0752d8aee4 boot: bootutil: include mcuboot_logging.h unconditionally
The mcuboot_logging.h header includes the header for the log subystem,
which needs to be included regardless of whether logging is enabled
for logging macros to compile correctly.

Signed-off-by: Emanuele Di Santo <emdi@nordicsemi.no>
2019-01-10 19:11:15 +01:00
Fabio Utzig a32f1af386 Fix overwrite-only under Zephyr
As reported by issue #384, some #ifdefery was wrongly done, which broke
overwrite-only mode under Zephyr.

Signed-off-by: Fabio Utzig <utzig@apache.org>
2019-01-07 10:59:20 -02:00
Christopher Collins 4b2591285a boot/bootutil: Add missing #include
Mynewt builds require the `mcuboot_config.h` file to translate syscfg
setting names to MCUboot setting names.  This change fixes a build error
that occurs when MCUboot is built with mbedTLS support:

Error: In file included from keys/bootkeys/src/bootkeys.c:2:0:
repos/mcuboot/boot/bootutil/include/bootutil/enc_key.h:29:10: fatal error: tinycrypt/aes.h: No such file or directory
 #include "tinycrypt/aes.h"
          ^~~~~~~~~~~~~~~~~

Signed-off-by: Christopher Collins <ccollins@apache.org>
2019-01-04 14:57:41 -08:00
Fabio Utzig 38f5ffea35 Add key unwrap functionality using tinycrypt
This implements AES key unwrapping following RFC-3394 section 2.2.2
using tinycrypt for AES-128 decryption. It is enabled when ECDSA was
chosen as the signature algorithm.

Signed-off-by: Fabio Utzig <utzig@apache.org>
2019-01-03 11:22:05 -02:00
Fabio Utzig 338a19f70d Add boostrapping from slot1 support
This adds a new option that allows copying slot0, from the contents
of slot1, if slot0 is found out to be erased and not validated, and
the contents of slot1 are validated.

This mechanism basically enables a device to "bootstrap" from the
contents of an external flash that has a valid image, given that the
only flashed thing in the internal flash is the bootloader.

Signed-off-by: Fabio Utzig <utzig@apache.org>
2018-12-27 10:58:50 -02:00
Rajavardhan Gundi 1b8d7fb355 Use BUILD_ASSERT_MSG instead of _Static_assert in Zephyr
Signed-off-by: Rajavardhan Gundi <rajavardhan.gundi@intel.com>
2018-12-27 08:13:41 -02:00
Timo Kröger e280e1166e Remove unused hal includes from bootutil
Signed-off-by: Timo Kröger <timokroeger93@gmail.com>
2018-12-21 06:47:40 -02:00
Fabio Utzig 2bd980a50d Add support for slots with different sector sizes
This adds bootutil support for slots on different flash devices
the happen to have different sector sizes.

It consists basically in relaxing the `boot_slots_compatible` to
allow swaps as long as the sectors that are required to fit both
images are able to fit inside scratch and both slot's sectors have
sizes that are multiple of each other.

This is now tested on the simulator and was tested in a Nordic's
pca10056 using slot0 in internal flash, and slot1 in the external
QSPI flash, configured with 4K, 8K and 16K sized sectors (the HW
is 4KB but Mynewt allows emulating multiples of that!)

Signed-off-by: Fabio Utzig <utzig@apache.org>
2018-12-18 15:16:11 -02:00
Fabio Utzig 2fc80df49f Fix handling of encrypted images
Encrypted images were known to be failing when the header size was larger
than 256 bytes because of incorrect handling of blocks sent to decryption
and hashing routines. An assert was previously added to check the header
and read block sizes matched to avoid incurring into the know error, but
it was incorrectly enabled also for non-encrypted images.

Now the handling of the header, which is not encrypted, is correctly
separated from the handling of the remaining image, when encryption is
used, to avoid ever sending header data into the decryption routines.

Signed-off-by: Fabio Utzig <utzig@apache.org>
2018-12-17 14:41:02 -02:00
Fabio Utzig e641ea5ee5 Fix overwrite only encrypted to not use local var
Use existing global var instead of using locally declared variable,
reducing RAM usage.

Signed-off-by: Fabio Utzig <utzig@apache.org>
2018-12-04 14:21:52 -02:00
Fabio Utzig 1c7d959eeb Fix rebase issue
Signed-off-by: Fabio Utzig <utzig@apache.org>
2018-12-04 14:21:52 -02:00
Yiping Peng 3393992943 Fixed an issue that supported later versions of mbedtls than mbedtls-2.6.1
Signed-off-by: Yiping Peng <836885645@qq.com>
Signed-off-by: Yiping Peng <yibingp@internal.synopsys.com>
2018-11-09 08:12:29 -03:00
Fabio Utzig ba829049ea Add bootutil support for encrypted images
This allows storing encrypted images in slot1, that are automatically
decrypted when copying to slot0 and re-encrypted when copying from slot0
to slot1.

The encryption works by applying AES-CTR-128 on the image blocks
(excluding the header and TLVs) using a random key. This random key
is itself encrypted using either RSA-OAEP-2048 or AES-KW-128 (AES keywrap
as defined by RFC3394), and appended to the image as newly defined TLVs.

AES-CTR-128 was chosen primarily for having stream cipher proporties,
which basically means that any block being encrypted/decrypted does not
depend on any other previous blocks results.

The TLV adds about 256 bytes to the image in RSA-OAEP-2048 mode, and 24
bytes in AES-KW-128 mode. Resulting sizes for a Mynewt generated mcuboot
(frdm-k64f):

- swap mode and no signing: 12KB
- adding encryption with RSA-OAEP-2048: 28KB
- adding encryption with AES-KW-128: 20KB

Some extra comments:

- AES-KW-128 requires a fairly new mbedtls with nist_kw support.
- An alternative methods which could be added later are ECIES.
- Key-wrapping seems easy enough to implement using just standard
  AES-ECB mode that it should be straight-forward to also add support to
  tinycrypt.

Signed-off-by: Fabio Utzig <utzig@apache.org>
2018-10-12 13:36:13 -03:00
Fabio Utzig 178be54bd6 Test erased flash with "flash_area_read_is_empty()"
Mynewt has recently added an encrypted flash layer driver, that runs
transparently on any flash, handling reads and writes, and bypassing
other flash operations to the HW driver. As a result of this change,
checking for erased data cannot be done by read + compare to erased_val
but need to be routed to an empty check on the lower level. To do this
Mynewt added a new flash_map function called "flash_area_read_is_empty"
which checks for erased blocks (and reads/decrypts the data as well).

This commit uses `flash_area_read_is_empty` to determine if magic,
flags and swap status are erased. For Zephyr/sim commits were added
previously that mimic this functionality by simply doing the
read/compare.

Signed-off-by: Fabio Utzig <utzig@apache.org>
2018-10-01 21:42:20 -03:00
Fabio Utzig 08fa267260 Fix copy_done setting
This compliments the previous change on `image_ok` and also removes
`flash_area_erased_val` call that is not needed anymore.

Signed-off-by: Fabio Utzig <utzig@apache.org>
2018-09-27 10:52:33 -03:00
Łukasz Rymanowski a1927f4d6b Fix setting confirm flag
It was impossible to set confirm flag due to incorrect comparison.
In this point of time state->image_ok is already decoded and contains
one of following values:

If BOOT_FLAG_UNSET then we are ok to set a confirm flag.

Signed-off-by: Łukasz Rymanowski <lukasz.rymanowski@codecoup.pl>
2018-09-26 12:20:17 -03:00
Hovland, Sigvart 1d96f36d01 Fix the wrong size of array of magic word in bootutil_misc
This will fix #335 by defining the array size of the magic word and
give it to the relevant functions.

Signed-off-by: Hovland, Sigvart <sigvart.hovland@nordicsemi.no>
2018-09-26 13:22:03 +02:00
Fabio Utzig 3900001c87 Add support for flash devices erased at 0
This extends mcuboot to allow use on devices which don't follow the
typical erased at 0xff. This was tested on some previously supported
devices (erased at 0xff) and STM32L1/L0 which are erased at 0.

Signed-off-by: Fabio Utzig <utzig@apache.org>
2018-09-17 12:42:49 -03:00
Fabio Utzig 03dc9a09a3 Avoid touching the swap status area in overwrite-only
The swap status area is used to maintain information that allows
recovery when interrupted in the middle of a swap. This area has no
usefulness when doing a overwrite-only upgrade, which always copies the
entire slot.

Signed-off-by: Fabio Utzig <utzig@apache.org>
2018-06-13 10:42:40 -07:00
Fabio Utzig 48841f28ce Remove per file log level configs
This removes settings of log level on a per file basis. The log level
should be set by the global config, while per file log configuration can
still be set, don't enforce them.

Signed-off-by: Fabio Utzig <utzig@apache.org>
2018-06-04 06:54:39 -03:00
Fabio Utzig a8e4e9714f Update logging to use per platform log macros
Now bootutil_log.h was converted to use per platform logging primitives
defined according to documentation established on the template file.

Some "null" macros were left to avoid breaking per source logging level
definition when MCUBOOT_HAVE_LOGGING was not defined.

Signed-off-by: Fabio Utzig <utzig@apache.org>
2018-06-04 06:54:39 -03:00
Andrzej Puzdrowski b788c71c08 Replace mcuboot flash_map by zephyr flash_map
The patch introduce usage of zephyr flas_map module instead
of mcuboot zephyr-only implementation. Unused flash_area_to_sectors
API of former flash_map was removed as well.
Size of sector-status-update-map entry is now defined thanks to the
minimum write size supported by the flash driver.

For avoid ambiguity former zephyr-only files flash_map.c
were renamed to flash_map_extended.c (its code now implements
only addition to this what zephyr flash_map implements).

flash_map.h header include is now warped by flash_map_backedn.h headre
because implementations and include pathes are diferent in Zephyr and Mynewt.

Usage of hal_flash_align() were replaced by usage flash_area_align().
This provide consistency between MyNewt and Zephyr implementation as
this API is available in both RTOSes.

flash_map.h was moved to the simulator c-support files as now missing in
the boot/zephyr subdirectories.

f. boot_scratch_fa_device_id was removed as unused.
f. boot_img_fa_device_id was and expanded the only use of it
(on loader.c).

Signed-off-by: Andrzej Puzdrowski <andrzej.puzdrowski@nordicsemi.no>
2018-05-16 16:05:17 -03:00
Fabio Utzig b5b59f16a5 Fix leftovers from image_header v1
This removes the old "TLV size" and "keyId" fields that used to exist
in the old image header format and updates it to use `load_addr`. Also
fixes the name of reserved fields to index from 1.

This fixes #279

Signed-off-by: Fabio Utzig <utzig@apache.org>
2018-05-10 08:43:52 -03:00
Christopher Collins 2844faaccc Mynewt unit tests - Add repo prefix to dep lists.
The unit tests depend on a few Mynewt packages (`test/testutil` and
`sys/console/stub`).  If there is no repo prefix in the dependency
specifier, newt assumes the package is in the local repo (mcuboot)`.
This commit adds the `@apache-mynewt-core/` prefix to these
dependencies.

Signed-off-by: Christopher Collins <ccollins@apache.org>
2018-05-01 15:05:57 -03:00
Marti Bolivar 248da08849 Move bootsim-specific assert behavior behind mcuboot_config.h
Continue removing platform-specific conditional compilation from
bootutil by adding a new MCUBOOT_HAVE_ASSERT_H configuration option
and associated header file. Right now, that's only used by the
simulator.

That leaves just bootutil_log.h with platform-specific contents, but
since it's meant to be an abstraction layer for logging, we'll let it
stand for now.

Signed-off-by: Marti Bolivar <marti@opensourcefoundries.com>
2018-04-25 18:44:03 -03:00
Marti Bolivar f9bfddd685 Move max image sector config to mcuboot_config.h
Take the opportunity to clean up a bit of platform cruft that has
gotten into bootutil by moving it to mcuboot_config.h, and ensuring it
is documented in the template config file.

Signed-off-by: Marti Bolivar <marti@opensourcefoundries.com>
2018-04-25 18:44:03 -03:00
Marti Bolivar f91bca51a6 Mandate the presence of mcuboot_config/mcuboot_config.h
Mynewt uses this file to convert MYNEWT_VAL(xxx) to MCUBOOT_xxx config
options. Zephyr currently adds config options via the compiler command
line, but it should use this instead.

As prep work for that conversion, add an empty mcuboot_config.h to the
Zephyr port, and include this file unconditionally wherever it's
needed. This takes care of the simulator as well, since that puts
boot/zephyr/include on its C file include path.

This turned up a couple of files (bootutil_priv.h and caps.c) that
were using the MCUBOOT_xxx config values without including the
file. Add the includes there, as they'll be needed later.

To make this official, add it to the requirements in the porting guide
and provide a template file porters can use while getting started.

Signed-off-by: Marti Bolivar <marti@opensourcefoundries.com>

fixup! Mandate the presence of mcuboot_config/mcuboot_config.h
2018-04-25 18:44:03 -03:00
Fabio Utzig 1b2f5ceb57 tabs -> spaces
Signed-off-by: Fabio Utzig <utzig@apache.org>
2018-04-05 18:15:35 -03:00
Fabio Utzig a1fae67d8a Add config for BOOT_MAX_IMG_SECTORS
This still defaults to 128 for compatibility, but allows users to
specify less elements to save space, or more elements for devices that
have massive flash size and/or too small sectors.

Signed-off-by: Fabio Utzig <utzig@apache.org>
2018-04-05 18:15:35 -03:00
Fabio Utzig 2a33aa26ed Add logging support for Mynewt
Signed-off-by: Fabio Utzig <utzig@apache.org>
2018-03-29 16:33:25 -03:00
Fabio Utzig 6771601435 Fix printf size warning
Signed-off-by: Fabio Utzig <utzig@apache.org>
2018-03-29 16:33:25 -03:00
Johann Fischer ed8461b604 bootutil: loader: fix BOOT_STATUS_ASSERT macro
BOOT_STATUS_ASSERT increments the variable if the macro argument
is evaluated as true, which is incorrect behavior compared to the
ASSERT macro.

Signed-off-by: Johann Fischer <j.fischer@phytec.de>
2018-02-16 16:44:17 -07:00
David Brown 785dc4c9b3 Fix RSA sigs for mbed TLS 2.7.0
In 2f8f06aa25

    commit 2f8f06aa25e9d5ee4fc9fe217543c872b39e4d05
    Author: Hanno Becker <hanno.becker@arm.com>
    Date:   Fri Sep 29 11:47:26 2017 +0100

        Don't always recompute context length in mbedtls_rsa_get_len

mbed TLS begins assuming that the 'len' field of the context has been
set properly.  Previously, we relied on this not being true, and various
tests fail for all keys without setting this.

The fix is to simply set the length based on the value we read.

fixes #235.

Signed-off-by: David Brown <david.brown@linaro.org>
2018-02-13 15:10:36 -07:00
Fabio Utzig f70e302617 Fix warn in overwrite-only with slot0 validation
Signed-off-by: Fabio Utzig <utzig@apache.org>
2018-01-10 10:35:10 -07:00
Fabio Utzig 891f4fb023 Fix EC API usage due to change in mbed-TLS
Signed-off-by: Fabio Utzig <utzig@apache.org>
2018-01-10 10:34:45 -07:00
Fabio Utzig 9c25fa76e5 Avoid using uninitialized memory in split go
Since boot_read_image_headers only failed when reading headers from
both Slots failed, it could happen that even with a failure reading
Slot 1, this data would still be used by split booting. Now when
reading image headers an extra parameter allows the caller to ask
for failure when any slot read failed.

JIRA: MCUB-56
Signed-off-by: Fabio Utzig <utzig@apache.org>
2018-01-04 10:35:26 -07:00
Fabio Utzig 539860453c Remove duplicated copy/paste residue
Signed-off-by: Fabio Utzig <utzig@apache.org>
2018-01-04 10:35:26 -07:00
Fabio Utzig 57c40f7164 Fix assert abstraction in sim
Assertions that are expected to fail under sim test, are now marked as such
using the macro ASSERT which allows to programmatically switch between normal
assert() behavior and captured assertion.

Assertion changes were moved to more appropriate owners and code duplication
was removed.

Signed-off-by: Fabio Utzig <utzig@apache.org>
2017-12-21 13:36:34 -07:00
Fabio Utzig a0e1cce57a Enable ignoring write failures to status area
The rationale behind this change is that while performing a swap
operation, writes to the status area are not important enough
to abort the operation. If the swap is not interrupted, status
writes are ignored. If the swap is interrupted, failures to write
status area are detected during the next boot and an appropriate
measure is taken.

If MCUBOOT_VALIDATE_SLOT0 is set we ignore the status write fail
and allow slot0 to be validated by its hash/signature. Otherwise,
the old behavior is maintained, which is to abort on any write
failures.

Signed-off-by: Fabio Utzig <utzig@apache.org>
2017-12-21 13:36:34 -07:00
Fabio Utzig 9b0ee9034a Enable capturing asserts inside simulator
When building in simulator mode, mock assert() to call an simulator
function which will enable other code to check if some path failed.

The assert checking interface, was added to boot_go which now
returns the low-level invocation results as well as the number of
asserts triggered.

Some new added tests check for assert() and expect it to happen. To not
abort tests, assert() was changed under sim env to not call c assert()
and instead just do some internal calculation which could be checked
by the simulator after a bootloader run and assert() catching behavior
was made optional (each test choses the behavior it needs).

Signed-off-by: Fabio Utzig <utzig@apache.org>
2017-12-21 13:36:34 -07:00
Fabio Utzig ac18eec773 Add Mynewt support for bundled tinycrypt + mbedtls
This commit removes the pseudo tinycrypt package based on symlinks
by adding a new pkg.yml to ext/tinycrypt and using it directly. It
also uses the new mbed-tls bundled ASN1 parser when EC256 is selected.

Signed-off-by: Fabio Utzig <utzig@apache.org>
2017-12-21 13:32:59 -07:00
Fabio Utzig 1a927dd591 Update bootutil_sig_verify to receive slen as size_t
This fixes a comparison issue that was previously fixed by doing a
cast. Since tinycrypt and mbed-tls both already use an unsigned for
the size, the mcuboot interface was updated to also use an unsigned
value (size_t to be more precise!).

Signed-off-by: Fabio Utzig <utzig@apache.org>
2017-12-13 14:26:08 -07:00
Fabio Utzig aaf767cf8a Fix warning in overwrite only
Signed-off-by: Fabio Utzig <utzig@apache.org>
2017-12-13 14:26:08 -07:00
Fabio Utzig cd5774b7ba Fix for gcc warnings
Fixes warnings treated as errors when upgrading gcc crate to 0.3.54.
Warnings are due to non-used variables and comparison between signed
and unsigned.

Signed-off-by: Fabio Utzig <utzig@apache.org>
2017-12-05 09:55:36 -02:00
Marti Bolivar c1f939d63f bootutil: fix edge case when not validating slot 0
When not validating slot 0, we could be booting into an empty
chip. This means we jump based on garbage data.

Try to make this better by at least checking the magic number. Without
this patch, booting with just the bootloader fails the Zephyr release
tests.

Signed-off-by: Marti Bolivar <marti@opensourcefoundries.com>
2017-11-20 18:10:29 -07:00
Marti Bolivar bf909a1fcc zephyr: port build system to CMake
Convert the Zephyr build to the new CMake-based sytem.

Signed-off-by: Marti Bolivar <marti@opensourcefoundries.com>
2017-11-20 18:10:29 -07:00