Commit Graph

238 Commits

Author SHA1 Message Date
Fabio Utzig 8af7f793ae Add copy_done check after first boot
This was previously removed because it failed on multi-image tests due
to some image having finished an upgrade and having it's flag already
set. A new method was added that checks that at least one copy_done flag
is unset, thus not all upgrades have finished.

Signed-off-by: Fabio Utzig <utzig@apache.org>
2019-07-30 11:39:09 -05:00
David Brown 1e1be9b368 Update Cargo.lock files
Recent versions of the Rust compile tool `cargo` insert a comment at the
top of `Cargo.lock` indicating that this file is auto-generated.  Commit
versions of these files with this comment in, so that users of recent
versions of Rust will not have sprious diffs everytime they compile.

Older versions of Rust will remove these lines.  However, this is less
likely to be an issue, as we now depend on at least Rust 1.31 in order
to have Rust 2018 support.  We recommend using 'rustup' to keep the Rust
install up to date.

Signed-off-by: David Brown <david.brown@linaro.org>
2019-07-25 16:24:20 -05:00
Tamas Ban 0c9266f66a sim: Remove copy_done check from revert_with_fails test
In case of multi image boot this check fails, because
copy_done flag set per images when an image swap has finished.
When second image update is interrupted then copy_done flag
already has set for first image.

Change-Id: Ic97dd5e4c5cdb5a5a94971f3ca84bfe0d7583dd4
Signed-off-by: Tamas Ban <tamas.ban@arm.com>
Signed-off-by: David Vincze <david.vincze@arm.com>
2019-07-23 09:11:34 -05:00
David Vincze b75c12a431 Boot: Extend flash layout for multiple images
This patch introduces the BOOT_IMAGE_NUMBER macro and current_image
variable to support multiple updatable images and the associated
extended flash layout.

The FLASH_AREA_IMAGE_* object-like macros are replaced with
function-like ones and therefore some functions have been updated,
because the labels of a switch statement and the initialization
values of objects with static storage duration have to be constant
expressions.

Change-Id: Ib7b26ec3c94233e52db4f97825ddb6a3e55bb1d3
Signed-off-by: David Vincze <david.vincze@arm.com>
2019-07-23 09:11:34 -05:00
David Vincze 6c9b416cd8 sim: Support non-continuous area ID mapping
Support non-continuous image flash area ID mapping. It was assumed
that the flash area IDs are subsequent and increasing numbers which
might not be true in all cases.

Change-Id: I0d1285d6fcf1e83a64611c9ad4f65abd002c25d3
Signed-off-by: David Vincze <david.vincze@arm.com>
2019-07-23 09:11:34 -05:00
Fabio Utzig 9771028579 Add ed25519 verification to sim
Signed-off-by: Fabio Utzig <utzig@apache.org>
2019-06-13 19:21:05 -03:00
Fabio Utzig fc07eab118 Add interruption at revert step of a test+revert
This extends the test+revert case with an interruption on the revert
stage, as it was previously only interrupted on the test stage. For
simplicity the interruption happens on the same interruption point for
both test and revert stages.

Signed-off-by: Fabio Utzig <utzig@apache.org>
Signed-off-by: Christopher Collins <ccollins@apache.org>
2019-05-31 10:15:08 -07:00
Fabio Utzig 28d012c1c5 Increase size of swap testing images
Make images slightly larger to allow more swap status metadata to be
written to flash, to increase amount of debugging info and possibility
of failures on random write fails.

Signed-off-by: Fabio Utzig <utzig@apache.org>
Signed-off-by: Christopher Collins <ccollins@apache.org>
2019-05-31 10:15:08 -07:00
Christopher Collins a1c1204f65 Fix double swap on interrupted revert
This fixes #480.

When mcuboot rewrites image trailers during a swap, some information is
lost.  If a reset occurs before the swap completes, mcuboot may not be
able to determine what which swap type to resume upon startup.
Specifically, if a "revert" swap gets interupted, mcuboot will perform
an extraneous swap on the subsequent boot.  See
https://github.com/JuulLabs-OSS/mcuboot/issues/480 for details.

This commit adds an additional field to the image trailer: `swap-type`.
The new trailer structure is illustrated below:

```
     0                   1                   2                   3
     0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    ~                                                               ~
    ~    Swap status (BOOT_MAX_IMG_SECTORS * min-write-size * 3)    ~
    ~                                                               ~
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    ~                 Encryption key 0 (16 octets) [*]              ~
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    ~                 Encryption key 1 (16 octets) [*]              ~
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    |           Swap size           |    0xff padding (4 octets)    |
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    |   Swap type   |           0xff padding (7 octets)             ~
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    |   Copy done   |           0xff padding (7 octets)             ~
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    |   Image OK    |           0xff padding (7 octets)             ~
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    ~                       MAGIC (16 octets)                       ~
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
```

The `swap-type` field contains one of the `BOOT_SWAP_TYPE_[...]` constants.
Every time a trailer is written, this field is written along with it.
When resuming an interrupted swap, mcuboot uses this field alone to
determine the type of swap being resumed. For new swap operations
(non-resume case), this field is not read at all; instead, mcuboot
consults the `boot_swap_tables` array to determine the swap operation to
perform (as it did prior to this commit).

Some additional changes were necessary to make all the simulated unit
tests pass:

* Before initiating a new swap operation, always write the image trailer
to the scratch area.  This step allows mcuboot to persist the
`swap-type` field somewhere before erasing the trailer in the primary
slot.  If a reset occurs immediately after the erase, mcuboot recovers
by using the trailer in the scratch area.

* Related to the above: if the scratch area is being used to hold status
bytes (because there are no spare sectors in the primary slot), erase
the scratch area immediately after the trailer gets written to the
primary slot.  This eliminates ambiguity regarding the location of the
current trailer in case a reset occurs shortly afterwards.

Signed-off-by: Christopher Collins <ccollins@apache.org>
2019-05-31 10:15:08 -07:00
Christopher Collins 2adef70e33 Use same format for scratch and slot trailer
Prior to this change, the scratch image trailer had a different format
from a slot image trailer.  Specifically:

1. The scratch trailer only contained a single set of status entries
   (three bytes); the slot trailer contained `BOOT_STATUS_MAX_ENTRIES`
   sets of status entries.

2. The scratch trailer did not contain the `copy_done` field.

This inconsistency required some extra conditional logic in the trailer
handling code.  It is simpler to just use the same trailer format
everywhere.

This commit removes this inconsistency.  Now, the scratch trailer
structure is identical to that of the slot trailer.

Signed-off-by: Christopher Collins <ccollins@apache.org>
2019-05-31 10:15:08 -07:00
Fabio Utzig 3929743408 Add simulator support for RSA-3072 sigs
Signed-off-by: Fabio Utzig <utzig@apache.org>
2019-05-16 14:01:19 -03:00
David Brown 5e6f5e0165 sim: Add multi-image config support to sim
Add support for building with multiple images to the simulator.

Signed-off-by: David Brown <david.brown@linaro.org>
2019-04-17 11:20:21 +07:00
David Brown 2bff647d8f sim: Add a K64fMulti flash device
This device is similar to the K64f device, but it is twice as large, and
has the extra partitions needed to test multi-image.

Signed-off-by: David Brown <david.brown@linaro.org>
2019-04-17 11:20:21 +07:00
David Brown 06ef06e69c sim: Base slots on number of images used
Construct the ImagesBuilder based on the number of images compiled into
the code.  If the flash device doesn't have enough areas for the test,
the test will be skipped.

Extend the FlashId to include Image2, and Image3.  Remove the unused
ones, so that these can be placed immediately after the scratcharea.
The current simulator code assumes the flash areas are numbered
contiguously, requiring these extraneous partitions to be eliminated.

Signed-off-by: David Brown <david.brown@linaro.org>
2019-04-17 11:20:21 +07:00
David Brown 4c9883b0d8 Add a capability query for image number
In addition to the binary capability flags, add a query function that
returns the number of images MCUboot has been configured to support.

Signed-off-by: David Brown <david.brown@linaro.org>
2019-04-17 11:20:21 +07:00
David Brown 5bc62c6b73 sim: Make the test builder optional
Some of the simulated devices aren't large enough to support the 5 slots
needed to test a multi-image configuration.  To allow this to work, make
the return from the `ImagesBuilder` return an option, so that it will be
able to indicate (with `None`) that this configuration isn't possible to
test, and that the test should be skipped.

Signed-off-by: David Brown <david.brown@linaro.org>
2019-04-17 11:20:21 +07:00
David Brown 96eb0deb76 sim: simflash: Transition to failure
The Rust community has decided that 'failure' is the future, instead of
'error-chain'.  Migrate the flash simulator to this new error handling
package.  The changes are relatively minor, and the end result is a
similar `FlashError` type.

Signed-off-by: David Brown <david.brown@linaro.org>
2019-04-17 11:20:21 +07:00
David Brown 84b49f751e sim: Internal support for multiple images
Move the three fields associated with each "Image" into a substructure
where we can have an array.  If the array size is 1, the behavior should
be identical to the previous behavior, but this will make it possible to
support multiple images just by creating more than one of these.

Signed-off-by: David Brown <david.brown@linaro.org>
2019-04-17 11:20:21 +07:00
David Brown db505823a4 sim: Make various functions into methods
The functions `try_upgrade`, `try_revert`, `try_revert_with_fail_at`,
and `try_random_fails` make more sense as methods.  Move them into the
`Images` impl so they have access to `self` and some various fields.

Signed-off-by: David Brown <david.brown@linaro.org>
2019-04-17 11:20:21 +07:00
David Brown 761015712c sim: Rename `SimFlashMap` to `SimMultiFlash`
The name `SimFlashMap` is a bit misleading, as the Map part is more of
an implementation detail when the type really just represents multiple
flash devices.

This is just a rename, but the names `SimFlashMap` and the value
`flashmap` occur in a lot of places in the image module.

Signed-off-by: David Brown <david.brown@linaro.org>
2019-04-17 11:20:21 +07:00
David Brown ca23469e82 sim: Create an `ImageData` type
Create an `ImageData` type to represent the stored content of a single
slot.  This holds plaintext and optional ciphertext.  This fixes a bit
of unclarity with regards to these fields:

  - Before, the plaintext was an Option, even though it is always
    present.  The field is clearly always present now.

  - The mapping between slot numbers, and plain and ciphertext was not
    made clear.  Now that is spelled out in a match statement.

  - The type has a name, rather than an anonymous array that looked a
    bit too much like it had to do with slot numbers.

Signed-off-by: David Brown <david.brown@linaro.org>
2019-04-17 11:20:21 +07:00
David Brown e513324722 sim: Make `Run` into a proper builder
Rename `Run` to `ImagesBuilder`, and move its definition into the
`images` module.  This makes much more of the functionality local to
this module.  With this locality, all of the fields of `Images` can now
be made private making it easier to add future support for multiple
images.

Signed-off-by: David Brown <david.brown@linaro.org>
2019-04-17 11:20:21 +07:00
David Brown 998aa8d220 sim: Move `Images` to before code
Rust convention is to have types before implementation.  Move `Images`
up to the top to make it more prominent.  This is also a good
opportunity to add a bit of a comment to this struct.

Signed-off-by: David Brown <david.brown@linaro.org>
2019-04-17 11:20:21 +07:00
David Brown 01617af254 sim: Make `Run` into a builder for `Images`
The `Run` type is used as a builder for `Images`, describing the
particular device config.  Convert the `make...` methods to take `self`
by value to avoid needing to clone all of the fields for the new struct.

The `main` test running now needs to do a few clones of `Run` in order
to build different configurations, but the primary test runner (through
`cargo test`) is now able to avoid having to clone the entire flash
simulators.

Signed-off-by: David Brown <david.brown@linaro.org>
2019-04-17 11:20:21 +07:00
David Brown ac46e26a86 sim: Get header magic value from ManifestGen trait
Different manifest objects will use different magic values for the
header.  Allow this support by providing a query to retrieve the
appropriate magic value.

Signed-off-by: David Brown <david.brown@linaro.org>
2019-04-17 11:20:21 +07:00
David Brown 43643dd252 sim: Use a trait object for TlvGen
In preparation for adding support for differing manifest types, abstract
the TlvGen with a trait object `ManifestGen`.  This will allow alternate
implementations to be made.

Signed-off-by: David Brown <david.brown@linaro.org>
2019-04-17 11:20:21 +07:00
Andrzej Puzdrowski e575fe9ead image encryption: fix enc_state array indexing for zephyr
enc_state table was indexed with assumption that
image flash area are subsequent and increasing numbers.
It might not be true while building zephyr.

Patch introduce flash_area_id_to_image_slot() implementation for
the zephyr port and uses it to assign proper slot number.
This API is already available in MyNewt.

Signed-off-by: Andrzej Puzdrowski <andrzej.puzdrowski@nordicsemi.no>
2019-03-14 16:10:54 +01:00
David Vincze 2d736ad4c5 Replace flash partitioning terminology
This change replaces the slot 0/1 terminology with primary/secondary
slot and replaces FLASH_AREA_IMAGE_0/1 with
FLASH_AREA_IMAGE_PRIMARY/SECONDARY. This naming convention may be more
understandable, fits better to MCUs with multiple images and it is an
architecture agnostic alternative as well.

Change-Id: I655a585f6ae023852c671ee6635399efe25209c9
Signed-off-by: David Vincze <david.vincze@arm.com>
Signed-off-by: David Brown <david.brown@linaro.org>
2019-03-13 15:40:21 -06:00
Andrzej Puzdrowski 419a47531b zephyr: allow dynamic numeration of flash_areas
Zephyr flash_map reworks caused that areas id exact number are
assigned dynamically.

This patch i counterpart to
https://github.com/zephyrproject-rtos/zephyr/pull/8837

Signed-off-by: Andrzej Puzdrowski <andrzej.puzdrowski@nordicsemi.no>
2019-02-10 16:08:38 +01:00
Fabio Utzig 05ab014e36 Update sim ecdsa testing to use ring signing
This removes the unsafe Tinycrypt bindings previously used for signing
with ECDSA, and relies on ring native support.

The ring library was updated to 0.14.1.

Signed-off-by: Fabio Utzig <utzig@apache.org>
2019-01-18 09:44:48 -07:00
David Brown 61a540dd4e sim: Remove cfg on `verify_image`
Remove the final conditional compilation in the simulator.  The
simulator is now always built the same way, and bases the tests it runs
on the capability queries made to the MCUboot code.  This simplifies the
simulator code a bit, and, importantly, removes the ability to have
mismatches between the configuration as compiled into the MCUboot code
and how the simulator is compiled.

Signed-off-by: David Brown <david.brown@linaro.org>
2019-01-12 14:35:54 -07:00
David Brown f38bc342f9 sim: Make `find_image` dynamic instead of cfg
Change the `find_image` to use a dynamic query instead of compile time
configuration.

Signed-off-by: David Brown <david.brown@linaro.org>
2019-01-12 14:35:54 -07:00
David Brown b8882113e4 sim: change `make_tlv` to dynamic instead of cfg
Use a runtime query of configuration instead of conditional compilation.

Signed-off-by: David Brown <david.brown@linaro.org>
2019-01-12 14:35:54 -07:00
David Brown 85904a8057 sim: Remove more cfg around validate-slot-0
Turn some more conditional compilation into runtime decisions based on
how the code being tested is compiled.

Signed-off-by: David Brown <david.brown@linaro.org>
2019-01-12 14:35:54 -07:00
David Brown 9930a3ec16 sim: Use enc capability
Now that we can dynamically query whether mcuboot is built with
encryption support, remove conditional compilation in favor of a dynamic
query.

Signed-off-by: David Brown <david.brown@linaro.org>
2019-01-12 14:35:54 -07:00
David Brown 3910ab1f24 sim: eliminate (some) overwrite cfg
Remove some compile-time configuration of the overwrite-only flag.
These will be decided by doing a dynamic query of how the mcuboot code
has been compiled.

Signed-off-by: David Brown <david.brown@linaro.org>
2019-01-12 14:35:54 -07:00
David Brown 5c9e0f1d05 sim: Move 'Image' code to a separate module
Start refactoring the image code by moving it to a separate module.
This requires some structs and fields be made public.  Otherwise, the
code is unchanged.

Signed-off-by: David Brown <david.brown@linaro.org>
2019-01-12 14:35:54 -07:00
Fabio Utzig 25d7b0fa9f Update simulator dependencies
Since logging was broken due to incompatibility between log and
env_logger versions, those crates were updated to known to be compatible
versions. Update initialization of env_logger that does not return a
Result<> anymore.

Other crates were updated to remove duplicated versions as much as
possible.

Signed-off-by: Fabio Utzig <utzig@apache.org>
2019-01-11 10:24:09 -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
David Brown 10b5de1392 sim: Idiomize for Rust 2018
Apply the changes suggested by

    cargo fix --edition-idioms

as well as a bit of cleanup of the results.  The result should be more
idiomatic Rust 2018 and a good starting point moving forward.

Signed-off-by: David Brown <david.brown@linaro.org>
2019-01-03 21:39:50 +00:00
David Brown 29b0b5ecb1 sim: fixes for 2018
Automatic migration to Rust 2018.  This is the result of running

    cargo fix --edition

The resulting code is compatible with both Rust 2015 and 2018.  Change
the edition field in the Cargo.toml file as well to begin a more
complete migration.

Signed-off-by: David Brown <david.brown@linaro.org>
2019-01-03 21:39:50 +00:00
David Brown 2821564b5d sim: mcuboot-sys: More idiomatic Rust 2018
Apply the changes suggested by

    cargo fix --edition-idioms

and cleanup the results a bit.  Eliminate `macro_use` extern crates.

Signed-off-by: David Brown <david.brown@linaro.org>
2019-01-03 21:39:50 +00:00
David Brown 65de6d177d sim: mcuboot-sys: 2018 edition fix
Automatic migration to Rust 2018:

    cargo fix --edition

Signed-off-by: David Brown <david.brown@linaro.org>
2019-01-03 21:39:50 +00:00
David Brown ea25c41af3 sim: simflash: Rust 2018 idiom updates
Apply the changes suggested by

    cargo fix --edition-idioms

as well as a bit of cleanup of the results.  The result should be more
idiomatic Rust 2018 and a good starting point moving forward.

Signed-off-by: David Brown <david.brown@linaro.org>
2019-01-03 21:39:50 +00:00
David Brown afabfcfe70 sim: simflash: Minimal 2018
Automatic migration to Rust 2018.  This is the result of running

    cargo fix --edition

The resulting code is compatible with both Rust 2015 and 2018.  Change
the edition field in the Cargo.toml file as well to begin a more
complete migration.

Signed-off-by: David Brown <david.brown@linaro.org>
2019-01-03 21:39:50 +00:00
David Brown e24244b632 sim: Update a few dependencies for Rust 2018
A few packages have updates that make them more convient to use with
Rust 2018's 'use macro'.  This is convenient as it allows control over
what macros are imported, but without these updates, these packages
require you to know the names of internal macros to import.

Signed-off-by: David Brown <david.brown@linaro.org>
2019-01-03 21:39:50 +00:00
Fabio Utzig b4d20c8c58 Update sim to run ecdsa sig + kw enc
This adds the functionality to build/run testing on images that were
signed using ECDSA and encrypted with KW, using tinycrypt.

Also when it this mode, ecdsa+kw, adds the Mbed-TLS submodule to the
build because the simulator needs to use the Mbed-TLS keywrapping
infrastructure to generate the keys sent to the image.

Signed-off-by: Fabio Utzig <utzig@apache.org>
2019-01-03 11:22:05 -02:00
Fabio Utzig d5722f52e5 Update Mbed-TLS submodule to 2.14.1
Signed-off-by: Fabio Utzig <utzig@apache.org>
2019-01-03 11:22:05 -02:00
Fabio Utzig 9b97b13a2b Enable bootstrap test in CI
Adds testing a bootstrap with RSA/ECDSA signing and KW encryption.

Signed-off-by: Fabio Utzig <utzig@apache.org>
2018-12-27 10:58:50 -02:00
Fabio Utzig 251ef1d98a Add rsa+kw testing support to simulator
Allows simulating images signed with RSA-2048 and encrypted with
AES-128-KW.

Signed-off-by: Fabio Utzig <utzig@apache.org>
2018-12-27 10:58:50 -02:00
Fabio Utzig 08fcfe9b63 Enable bootutil logging in simulator
Signed-off-by: Fabio Utzig <utzig@apache.org>
2018-12-18 15:16:11 -02:00
Fabio Utzig 6465077525 Add unsupported flash layout
This adds an external SPI flash that uses a larger sector size than
the internal flash. Currently this breaks the tests but it's being
added here to trigger a CI fail that will be fixed by adding support
for this feature in a subsequent commit.

Signed-off-by: Fabio Utzig <utzig@apache.org>
2018-12-18 15:16:11 -02:00
Fabio Utzig afb2bc90c6 Update main simulator routines for multi-flash
This adds an initial device with multiple flash (nrf52840 + SPI flash)
and updates all test routines to use a HashMap of flash devices (added
as type SimFlashMap).

Signed-off-by: Fabio Utzig <utzig@apache.org>
2018-12-18 15:16:11 -02:00
Fabio Utzig 1c9aea5814 Allow multiple flash devices in bootutil wrapper
A new type `FlashMap` that stores a HashMap of [device_id -> Flash trait]
was added, which enables multi-flash devices to be passed around.

The previously existing static FLASH value that was used to simulate the
raw device, was updated to using a FlashMap which enables bootutil to
interface with more than one flash device.

Signed-off-by: Fabio Utzig <utzig@apache.org>
2018-12-18 15:16:11 -02:00
Fabio Utzig 1caef137c4 Allow multiple flash device in AreaDesc
AreaDesc was modified to not receive a flash device on its constructor,
and instead a new function `add_flash_sectors` was added that allows it
to receive a flash device and id.

The `add_image` function that populates the areas also now receives a
dev_id that is used internally as fa_device_id.

Signed-off-by: Fabio Utzig <utzig@apache.org>
2018-12-18 15:16:11 -02:00
Fabio Utzig 73ffc4458d Add align/erased_val params per flash device
The previous c/rust ffi functions were hardcoding the values of align
and erased_val before each run through static globals. This adds new sim
flash functions that get the align/erased_val from the sim flash device
that is being run on, allowing that later multiple flash devices can
each use its own params.

Signed-off-by: Fabio Utzig <utzig@apache.org>
2018-12-18 15:16:11 -02:00
Fabio Utzig 269d28621d Remove redundant align/erased_val usage
A new align() function was added to SimFlash, and most functions that
were using/receiving align or erased_val parameters that had access to a
Flash trait were cleaned up so that they get the parameters directly
from the Flash device.

This will make it easier to extend for multiple Flash devices since
parameters should depend on the device in use.

Signed-off-by: Fabio Utzig <utzig@apache.org>
2018-12-18 15:16:11 -02:00
Fabio Utzig 99dfc78723 Remove usage of hal_flash_* functions
Update `flash_area_*()` functions to call `sim_flash_*()` directly
instead of using `hal_flash_*()` functions that were not part of the
main bootloader anymore.

Signed-off-by: Fabio Utzig <utzig@apache.org>
2018-12-18 15:16:11 -02:00
Fabio Utzig 455cad5aca Move from gcc to cc crate
gcc create has become deprecated: https://crates.io/crates/gcc

Signed-off-by: Fabio Utzig <utzig@apache.org>
2018-12-18 15:16:11 -02:00
Fabio Utzig e5831f6ed2 Make sim header size slightly more configurable
This adds an HDR_SIZE constant to the simulator which allows for easier
testing of images with different header sizes.

Signed-off-by: Fabio Utzig <utzig@apache.org>
2018-12-17 14:41:02 -02:00
Fabio Utzig c8d67f10b1 Add TLV gen for RSA sig+enc
Signed-off-by: Fabio Utzig <utzig@apache.org>
2018-12-17 14:41:02 -02:00
Fabio Utzig 04fd63e6ba Add build section for mbedTLS config
This makes it easier to choose the mbedTLS config file when multiple
features share files.

Signed-off-by: Fabio Utzig <utzig@apache.org>
2018-12-17 14:41:02 -02:00
Fabio Utzig 754438dfd5 Fix TLV sizes for signed images
TLV types don't need to list KEYHASH because it's already added by any
signature TLV.

Total sizes were missing the KEYHASH amount.

Add a new TLV for images signed with RSA and encrypted with RSA-OAEP.

Signed-off-by: Fabio Utzig <utzig@apache.org>
2018-12-17 14:41:02 -02:00
Fabio Utzig 9b7a25833b Fix overwrite-only encrypted images in sim
Also add overwrite-only + encrypted tests to travis-ci.

Signed-off-by: Fabio Utzig <utzig@apache.org>
2018-12-04 14:21:52 -02:00
Fabio Utzig 1e48b914a9 Add encrypted image support on sim
This adds new cargo features to allow running tests of encrypted
images with both RSA-OAEP and AES-128-KW.

When installing images on the simulated flash, both a plain and an
encrypted images are created. When encrypted image support is enabled,
verification of images in slot1 match against the encrypted image,
otherwise plain images are used.

PS: Also fixes ImageHeader to match bootutil definition.

Signed-off-by: Fabio Utzig <utzig@apache.org>
2018-10-12 13:36:13 -03:00
Fabio Utzig 35d31b4023 Add flash_area_read_is_empty to sim
Signed-off-by: Fabio Utzig <utzig@apache.org>
2018-10-01 21:42:20 -03:00
Fabio Utzig ea0290b88b Add sim support for flash erased at 0
This extends the simulator to be able to test the bootloader in devices
which use flash technologies that erase flash at 0 instead of 0xff.
Two MCU devices that have this "property" are the STM32L0x and STM32L1x
lines from ST.

Signed-off-by: Fabio Utzig <utzig@apache.org>
2018-09-17 12:42:49 -03:00
Fabio Utzig fc711e3de9 Remove usage of hal_flash_align
`hal_flash_*` functions were replaced by `flash_area_*` with similar
functionality.

Signed-off-by: Fabio Utzig <utzig@apache.org>
2018-09-17 12:42:49 -03:00
Fabio Utzig b04afa9cb3 Update mbedtls submodule to 2.12.0
This also updates the RSA test to add extra build files for zeroize, and
updates to new calloc/free configuration style.

Signed-off-by: Fabio Utzig <utzig@apache.org>
2018-09-13 06:54:30 -03:00
Fabio Utzig c354e4ac5e Update sim dependencies
This updates error-chain to remove a warning on recent rust distros.
`pem` was update to get fairly recent `error-chain` as well.

Signed-off-by: Fabio Utzig <utzig@apache.org>
2018-07-10 11:11:29 -03:00
Fabio Utzig 806af0ed87 Update old hardcoded keys to use autogenerated
The hardcoded keys for Zephyr were removed and external references were
added for the built time autogenerated variables. A hardcoded key file
was added specifically for the simulator.

Signed-off-by: Fabio Utzig <utzig@apache.org>
2018-06-06 11:07:06 -03:00
Fabio Utzig 9a4b9ba1c0 Add per platform mcuboot_logging.h files
New logging macros were added for all supported platforms, following the
documentation defined in the template config file.

Signed-off-by: Fabio Utzig <utzig@apache.org>
2018-06-04 06:54:39 -03:00
David Brown 34fe103d04 sim: Add missing license headers
These (currently) trivial files do not add license headers.  Go ahead
and add the boilerplate Apache header.

In addition, I've also added an SPDX header.  Although this is a first
for the MCUboot project, this will provide a template for adding these
headers to other files.

Fixes #282

Signed-off-by: David Brown <david.brown@linaro.org>
CC: Marti Bolivar <marti@opensourcefoundries.com>
CC: Fabio Utzig <utzig@apache.org>
2018-05-23 09:29:25 -06: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
Marti Bolivar 4ec7f7686c sim: update README.rst with information on features
The README file is missing extra information about how to run all the
different test configurations; add this in.

Signed-off-by: Marti Bolivar <marti@opensourcefoundries.com>
2018-04-25 18:44:03 -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 a4818a5565 zephyr: migrate signature type to Kconfig
Handle the CONFIG_BOOT_SIGNATURE_TYPE_xxx values in Zephyr's
mcuboot_config.h by converting them into the platform-agnostic MCUboot
definitions.

This requires some changes to the way the release test Makefile is
structured, since Kconfig symbols cannot be set from the command line.

Instead, use the OVERLAY_CONFIG feature of the Zephyr build system,
which allows specifying extra fragments to merge into the final
.config. (This is an orthogonal mechanism to setting CONF_FILE; it is
used by Zephyr's CI script sanitycheck to add additional fragments, so
it's appropriate for use by MCUboot's testing scripts as well.)

We additionally need to move to a single prj.conf file due to a
dependency issue. We can no longer determine CONF_FILE from the
signature type, since that is now determined from the final .config or
autoconf.h, which is a build output that depends on CONF_FILE.

To move to a single prj.conf:

- delete prj-p256.conf and adjust prj.conf to serve both signature types
- add a top-level mbedTLS configuration file which dispatches to
  the right sub-header depending on the key type
- as a side effect, have the simulator pick the right config file
  depending on the case

This fixes and cleans up quite a bit of the signature type handling,
which had become something of a mess over time. For example, it fixes
a bug in ECDSA mode's configuration that wasn't actually selecting
config-asn1.h, and forces the simulator to use the same mbedTLS
configuration file as builds for real hardware.

Finally, we also have to move the mbedTLS vs. TinyCrypt choice into
mcuboot_config.h at the same time as well, since CMakeLists.txt was
making that decision based on the signature type.

Signed-off-by: Marti Bolivar <marti@opensourcefoundries.com>
2018-04-25 18:44:03 -03:00
David Brown b77262e18d Bump internal mbedtls to 2.7.0
2.7.0 fixes a security issue but breaks MCU boot.  Bump to this version
so that the simulator will verify that this version works properly.

Signed-off-by: David Brown <david.brown@linaro.org>
2018-02-13 15:10:36 -07:00
David Brown 59ae522541 sim: Remove extraneous `mut`
This value does not need to be mutable as it is just given to another
function immediately.

Signed-off-by: David Brown <david.brown@linaro.org>
2018-01-04 10:36:07 -07:00
David Brown 3df2a0ea15 sim: Update some dependencies
Move to newer versions of some upstream packages, lazy_static, which is
now at 1.0 (no api change), and error-chain (0.11), which eliminates a
warning during compilation.

Signed-off-by: David Brown <david.brown@linaro.org>
2018-01-04 10:36:07 -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 a91c626f89 Mark non-used functions in overwrite only mode
Signed-off-by: Fabio Utzig <utzig@apache.org>
2017-12-21 13:36:34 -07:00
Fabio Utzig eedcc450d6 Add test with write status area fail and interruption
Signed-off-by: Fabio Utzig <utzig@apache.org>
2017-12-21 13:36:34 -07:00
Fabio Utzig b841f0a0c6 Add simple status write test without interruption
This test marks the status write regions as a bad region,
causing errors, but it is allowed to complete the swap. On
restart the firmware in slot0 should be validated and work.

Signed-off-by: Fabio Utzig <utzig@apache.org>
2017-12-21 13:36:34 -07:00
Fabio Utzig fa137fc83d Add option to disable verifying contents before writes
At the moment writes always checking that they are being
performed on an erased section of memory. This patch enables
a test to disable that, to enable looking for other error
causes.

Signed-off-by: Fabio Utzig <utzig@apache.org>
2017-12-21 13:36:34 -07:00
Fabio Utzig f5c895e3fc Add configurable simulation of bad memory writes
Add new interface to enable configuring regions of the simulated
memory to fail probabilistically. An entry is based on an offset,
length and rate of failure. This only applies to write operatios
and throws an error that can be caught in tests.

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 ebdc969738 Add MCUBOOT_VALIDATE_SLOT0 as feature
Signed-off-by: Fabio Utzig <utzig@apache.org>
2017-12-21 13:36:34 -07:00
Fabio Utzig 43685c211a Enable extra crates in simflash
Signed-off-by: Fabio Utzig <utzig@apache.org>
2017-12-21 13:36:34 -07:00
Fabio Utzig d32fd64e47 Fix mbed-tls' walk_dir in build
Signed-off-by: Fabio Utzig <utzig@apache.org>
2017-12-21 13:32:59 -07:00
Fabio Utzig 70f1481af3 Remove duplicated declaration
Signed-off-by: Fabio Utzig <utzig@apache.org>
2017-12-21 13:32:59 -07:00
Fabio Utzig ba05f2a309 Add asn1 parser of mbed-tls
This bundles the asn1 parser from mbed-tls into mcuboot, which allows
adding EC crypto (tinycrypt based) functionality for target OSes that
don't bundle mbed-tls.

Signed-off-by: Fabio Utzig <utzig@apache.org>
2017-12-21 13:32:59 -07:00
Fabio Utzig 0bccf9d84c Build in std=c99 mode
Our current travis environment still uses gcc 4.8.4 which defaults to
std=gnu90. Some of tinycrypt's code requires c99.

Signed-off-by: Fabio Utzig <utzig@apache.org>
2017-12-13 14:26:08 -07:00
Fabio Utzig 181e54c382 Update cargo lock
Signed-off-by: Fabio Utzig <utzig@apache.org>
2017-12-13 14:26:08 -07:00
Fabio Utzig 80fde2f544 Add ecdsa signing support to the simulator
Signed-off-by: Fabio Utzig <utzig@apache.org>
2017-12-13 14:26:08 -07:00
Fabio Utzig 92be3fb667 Add ecdsa signing support
Since ring does not yet support ecdsa signing, a thin layer was added
to allow the simulator to call tinycrypt's signing routine.

Signed-off-by: Fabio Utzig <utzig@apache.org>
2017-12-13 14:26:08 -07:00
Fabio Utzig c786540825 Add ecdsa to build
This enables building ecdsa feature using tinycrypt (mbed still built
for ASN1). The default mbed-tls config was update to use the MCUBOOT_SIGN_*
symbols.

Signed-off-by: Fabio Utzig <utzig@apache.org>
2017-12-13 14:26:08 -07:00
Fabio Utzig 8b619bda54 Update ring+gcc crates and declare sig-ecdsa feature
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