Commit Graph

70 Commits

Author SHA1 Message Date
David Brown 72e7a5176f Change the image header magic
Since we've changed the meaning of several fields in the header, bump
the header to a new magic number.  The fields that are still present are
in the same place, but all of the signature and TLV information is moved
into the TLV itself, which is still immediately after the image.

As of this commit, this defines the new image header/TLV format used for
1.0.

Based on work by Marko Kiiskila <marko@runtime.io>

Signed-off-by: Marko Kiiskila <marko@runtime.io>
Signed-off-by: David Brown <david.brown@linaro.org>
2017-09-06 16:16:58 -06:00
David Brown f5b33d8b9d Place TLV size into TLV itself
To allow the signatures to be replaced, move the size of the TLV into a
small "info" header at the start of the TLV.

Note that this causes image swapping to lose robustness.  This is fixed
by a later commit.

Based on work by Marko Kiiskila <marko@runtime.io>

Signed-off-by: Marko Kiiskila <marko@runtime.io>
Signed-off-by: David Brown <david.brown@linaro.org>
JIRA: MCUB-65
2017-09-06 16:16:58 -06:00
David Brown 43cda33c5a Move key_id from header into TLV
Remove the key_id field from the image header.  There are two problems
with this field.  First, it is only an integer offset, and so causes an
unnecessarily tight coupling between the particular keys built into the
bootloader, and the key that is used to sign.  Second, it makes the
key_id part of the image header, which is included in the signature.
This makes it impossible to later sign the image with a different
signature.

Instead of the key-id, add a TLV KEYHASH entry.  This will hold the
SHA256 of the public key that the signature is against.  Each signature
placed in the TLV should be preceeded by this entry to indicate the
public key used.

The signature check will check each signature, and if the KEYHASH is
known and the signature type is supported, it will be checked.  As long
as at least one signature is considered valid, the image will be
considered signed.  This also allows the image to be signed with
multiple signatures to support having different devices with possibly
different keys compiled into the bootloaders.

Based on work by Marko Kiiskila <marko@runtime.io>

Signed-off-by: Marko Kiiskila <marko@runtime.io>
Signed-off-by: David Brown <david.brown@linaro.org>
2017-09-06 16:16:58 -06:00
David Brown 27648b8344 Renumber the TLV type values
In preparation for moving the signature related values out of the main
image header and into the TLV, renumber the existing TLV values to be
grouped together better.

The SHA256 is moved into the first group, at 0x10, and the signature
values themselves are moved to start with 0x20.

This change is the first in a series of changes toward the new v1.0
image format.  The intermediate results are all internally consistent
(meaning that the simulator, and the builtin imgtool.py will all work
together), but until all patches are applied, the image format is not
valid with any external tools.

Based on work by Marko Kiiskila <marko@runtime.io>.

Signed-off-by: Marko Kiiskila <marko@runtime.io>
Signed-off-by: David Brown <david.brown@linaro.org>
2017-09-06 16:16:58 -06:00
David Brown 9600ec84f2 sim: Update doc to explain submodules
Now that we depend on a submodule for mbed TLS code, update the docs to
explain how to fetch this.  Otherwise, the error is somewhat misleading,
just showing a missing sha256.c file.

Signed-off-by: David Brown <david.brown@linaro.org>
2017-07-21 11:50:48 -06:00
Fabio Utzig 645e514b79 Add test checking upgrade to image with bad signature
This test was added to exercise a bug which happens when a failure
verifying the image in slot 1 would cause a write to image_ok when
the image in slot 0 already was a permanent image.

Signed-off-by: Fabio Utzig <utzig@apache.org>
2017-07-17 15:36:13 -03:00
Fabio Utzig 65935d7111 Fix flash to not ignore write to non-erased area
Signed-off-by: Fabio Utzig <utzig@apache.org>
2017-07-17 15:34:36 -03:00
Fabio Utzig 7b47ef7b70 Test imgtool generated FW written to slot0
Signed-off-by: Fabio Utzig <utzig@apache.org>
2017-07-13 21:24:03 -03:00
David Brown 046a0a6224 sim: Upgrade docopt dependency
The docopt package has moved from rustc-serialize to serde.  Move to
this version, upgrading the custom deserialize we use for the alignment
argument.

Signed-off-by: David Brown <david.brown@linaro.org>
2017-07-12 19:04:18 -06:00
David Brown ccb2a1dd5e sim: Update most dependencies
Move most dependencies to the latest version at the time of this commit.
docopt is left at the current version as there is an api change in the
newer version.

Signed-off-by: David Brown <david.brown@linaro.org>
2017-07-12 19:04:18 -06:00
David Brown 0b693c0c74 sim: Enable -Werror
Enable -Werror in the build of the C code (also remove an unneeded TODO
comment).

Signed-off-by: David Brown <david.brown@linaro.org>
2017-07-12 15:00:57 -06:00
David Brown 704ac6f570 sim: Conditionalize rsa signature checking
Allow a build with sig-rsa set or not set.  Only add the signature to
the TLV if we are building with the signature checking.
2017-07-12 15:00:57 -06:00
David Brown f52272c76d sim: Flatten 'area' module in mcuboot-sys
It isn't necessary for the 'area' module to be exposed.  Re-export the
two definitions from the crate at the top level, and make the module
private.

Signed-off-by: David Brown <david.brown@linaro.org>
2017-07-12 15:00:57 -06:00
David Brown d2b1853f02 sim: Move csupport into mcuboot-sys
This C code is also part of the binding.  Move it into the binding crate
to reflect this.

Signed-off-by: David Brown <david.brown@linaro.org>
2017-07-12 15:00:57 -06:00
David Brown 82bf7c2d09 sim: Move mbedtls into mcuboot-sys
The mbedtls library is used by the binding crate, so move the submodule
into this directory as well.

Signed-off-by: David Brown <david.brown@linaro.org>
2017-07-12 15:00:57 -06:00
David Brown 6390277f64 sim: Naive move of binding into mcuboot-sys
Create a crate `mcuboot-sys` to hold the building and binding of the
mcuboot code.  There aren't any substantive code changes here, just
moving the code into a separate crate.

Signed-off-by: David Brown <david.brown@linaro.org>
2017-07-12 15:00:57 -06:00
David Brown 7e701d8ca3 sim: Add RSA signature verification to simulator
Using the published debug keypair, generate a signature with it, to add
to the TLV.  This verifies that signature verification works as
expected.

Signed-off-by: David Brown <david.brown@linaro.org>
2017-07-12 15:00:57 -06:00
David Brown 1e15859951 sim: Switch to bitflags from enumflags
The bitflags crate seems to be better supported, and doesn't have the
problems getting the results back into the enum type.

Signed-off-by: David Brown <david.brown@linaro.org>
2017-07-12 15:00:57 -06:00
David Brown 4243ab09f8 sim: Capture payload in TLV code
Since the signing code will also need a copy of the message, make a
local copy of it in the signature verification code, and compute the
digest all in one shot.

Signed-off-by: David Brown <david.brown@linaro.org>
2017-07-12 15:00:57 -06:00
David Brown 8054ce281a sim: Change to `ring` instead of `rust-crypto`
The ring crate's SHA256 is slightly slower than the one from
rust-crypto, but is much cleaner to use.  It also has all of the hashing
operations we need.

This crate is somewhat opinionated, e.g. will not sign messages without
also performing the signature itself.  Unfortunately, this means we'll
end up computing signatures of the same data many times, and perhaps
this should be improved, since we're always using the same data.

Signed-off-by: David Brown <david.brown@linaro.org>
2017-07-12 15:00:57 -06:00
David Brown 187dd889dc sim: Validate the SHA256 verification
Instead of stubbing out the image validation code, compile it, and add
the SHA256 TLV to the buffer.

Signed-off-by: David Brown <david.brown@linaro.org>
2017-07-12 15:00:57 -06:00
David Brown 2cbc4708e9 sim: Move simflash to be its own crate
In preparation for moving the bootutil binding into a separate crate,
move the flash simulator into its own crate.  This will allow the
binding to have access to the simulator without creating a circular
dependency.

Signed-off-by: David Brown <david.brown@linaro.org>
2017-07-12 15:00:57 -06:00
David Brown 7ddec0b5cc sim: Move 'Flash' into a trait
Make 'Flash' a trait, and move the current functionality into a type
called 'SimFlash'.  The code that runs the simulation only uses the
trait.

Signed-off-by: David Brown <david.brown@linaro.org>
2017-07-12 15:00:57 -06:00
David Brown bdb6db723d sim: Move flash pointer up into Rust
Move the reference to the Flash device up into the Rust code instead of
trying to pass it back and forth to C.  This will allow a future change
to use a fat pointer (such as a trait pointer), which ultimately will
allow different kinds of flash devices.

Signed-off-by: David Brown <david.brown@linaro.org>
2017-07-12 15:00:57 -06:00
David Brown 2784a2e2f2 sim: Update gcc dependency
Signed-off-by: David Brown <david.brown@linaro.org>
2017-07-12 15:00:57 -06:00
Fabio Utzig ebeecef4b4 Refactor of main test code
- Some helper structs added to track state
- Add new trailer checking routine
- Add extra test for upgrade+revert with failure
- Misc improvements

Signed-off-by: Fabio Utzig <utzig@apache.org>
2017-07-10 16:31:47 -06:00
Fabio Utzig a0bc9b5469 Update trailer format
This implements changes according to MCUB-14, easing the process
of making external apps parse and read/write the trailer.

Signed-off-by: Fabio Utzig <utzig@apache.org>
2017-07-06 10:03:55 -06:00
Fabio Utzig 40b4aa07de Fix offset print writing to non-erased area
Signed-off-by: Fabio Utzig <utzig@apache.org>
2017-07-06 10:03:55 -06:00
David Brown 7ad8088a57 sim: Match spacing on csupport/run.c
Re-tab run.c to match the rest of the files in the system.  This was
indented using Zephyr conventions (tabs for indent).  Re-indent using
spaces, with a 4-space indent level.

Signed-off-by: David Brown <david.brown@linaro.org>
2017-06-20 15:30:36 -06:00
Marti Bolivar 51d36dd592 sim: add stricter write checking
Extend the flash emulation in the simulator to verify that the
bootloader explicitly erases flash before writing to it for a second
time.

Signed-off-by: Marti Bolivar <marti.bolivar@linaro.org>
2017-06-19 14:46:48 -06:00
Marti Bolivar 450d44955f sim: use flash_area_get_sectors()
Use the new flash sector API by default when testing.

Signed-off-by: Marti Bolivar <marti.bolivar@linaro.org>
2017-06-15 13:24:15 -04:00
David Brown 60399f69fb sim: Implement flash_area_get_sectors for sim
Implement the new flash sector query API for the simulator.  This is
generated from the data for the deprecated API.  Once the old API is
removed, the flash simulator can be changed to just return the new data
directly.

Signed-off-by: David Brown <david.brown@linaro.org>
Signed-off-by: Marti Bolivar <marti.bolivar@linaro.org>
2017-06-15 13:24:15 -04:00
Marti Bolivar 88f48d9140 Rename br_image_addr to br_image_off.
The boot response returns a flash offset, not a flash address. This is
causing confusion and leading to crashes on some platforms which don't
have flash at address 0.

Rename the field to make it more clear what its purpose is; future
patches can start fixing up usages.

Signed-off-by: Marti Bolivar <marti.bolivar@linaro.org>
2017-06-15 13:24:15 -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 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 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 dc76c9690c Merge pull request #48 from d3zd3z/pr/overwrite
Add overwrite-only upgrade
2017-05-11 15:52:06 -06:00
David Brown 2d1d7cfd47 sim: Allow Rust logging level to control C logging
Change the C logging code, when in the simulator, to query what the rust
logging level is set to.  This allows the level of logging from the C
code to be set through the environment.  For example

    RUST_LOG=bootsim=info cargo run --release runall

will enable logging at the "info" level for all of the C code as well as
the simulator code.  The C code's logging can be selected specifically
by using bootsim::api instead of just bootsim in the above.
2017-05-11 09:17:03 -06:00
David Brown 186323e47e Merge pull request #50 from utzig/fix-var-shadowing
Fix shadowing of variable in print
2017-05-10 08:58:20 -06:00
David Brown 17609d8a7e Add "upgrade only" support
Add a configuration option "BOOTUTIL_OVERWRITE_ONLY" that avoids using
the image swap code.  Instead, when an upgrade is detected in slot 1, it
is copied directly onto slot 0.  As long as the image in slot 1 is
valid, this should work robustly (it will redo it if power is lost
during the upgrade).

This doesn't protect against the case of deploying an image that fails
to boot on some devices.  But, the behavior is similar to the swap
upgrade approach when the slot 1 image is marked initially as "image
ok", but without the complexity (or need of a swap partition) of the
swap code.
2017-05-10 08:33:19 -06:00
David Brown 902d617687 Add capabilities query
Add a simple function to query the bootloader for capabilities.
Ultimately, this API should be available to the running app, but the
simulator can use this to determine what to test.
2017-05-10 08:32:42 -06:00
Fabio Utzig 5ac0a1ae29 Fix shadowing of variable in print 2017-05-10 08:49:39 -03:00
David Brown 5b35d1f467 sim: Enable warnings in sim builds
Enable -Wall so that warnings will be generated.  Remove an unneeded
declaration that this reveals.
2017-05-05 11:07:42 -06:00
David Brown 54b7779803 sim: Clean up logging
Make error the default log level in the sim shims, and add a function
name to two of the calls.
2017-05-05 11:07:42 -06:00
David Brown 75fd5dc05e sim: Generate logging within the sim
Add logging support for when running in the simulator.  Log messages are
still based on compile-time determinations, and log using printf.

Based on a patch from Marti Bolivar <marti.bolivar@linaro.org>.
2017-05-05 11:07:42 -06:00
Fabio Utzig 57652319ae Fix random reset behaviour 2017-04-25 19:54:26 -03:00
Fabio Utzig 19b2c1a927 Add extra information on errors
* Print bail out error information
* Print specific location of non-ff writes
2017-04-20 08:25:30 -03:00
David Brown 4cb2623574 Back out unneeded part of random reset test
In change

    commit bb5635e074
    Author: Fabio Utzig <utzig@utzig.org>
    Date:   Mon Apr 10 09:07:02 2017 -0300

        Add test of multiple random resets while upgrading

in addition to adding the try_random_fails, it shuffled the order that
the initial interruption test ran.  However, this test always clones
the flash device, so it doesn't matter what order the tests are run.
Revert this part of the change to avoid unnecessary complexity in the
code.
2017-04-11 08:18:18 -06:00
Fabio Utzig bb5635e074 Add test of multiple random resets while upgrading 2017-04-10 09:07:02 -03:00