Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix readme #12

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,27 @@ The primary slot typically ends where the "mcuboot scratch region" begins (see S

**Note:** If your application uses internal flash for data storage (eg: KVStore), you must carefully configure the memory regions allocated to the primary slot, the scratch region, and your application to ensure there are no conflicts.

#### Primary Application

The primary application is the currently installed, bootable application. In this demo it is the `mbed-mcuboot-blinky` application. The application start address is configured using `target.mbed_app_start` and the size can be restricted using `target.mbed_app_size`. The primary application size **must** be restricted to avoid colliding with the scratch space region (if used)!

#### Application Header Info

The application header info section is at the beginning of the "primary memory slot".

When deciding what to boot/update, the mcuboot bootloader looks at an installed application's header info, which is a special struct prepended to the application binary. It uses this header info to validate that there is a bootable image installed in the "slot".

By default, this header is configured to be 4kB in size. This can be adjusted using the configuration parameter `mcuboot.header_size`.
The header size is set and the header is created and prepended to the application binary by `imgtool` during the signing process (explained later).

**However,** due to the way the FlashIAP block device currently works while erasing, the header_size should be configured to be the size of an erase sector (4kB in the case of an nRF52840). Erasing using the FlashIAPBlockDevice only works if the given address is erase-sector aligned!
The minimum header size is determined by the size of interrupt vector table of the target. The interrupt vector table is located at the beginning of the application. The primary application start address (`target.mbed_app_start`) must conform to the requirements set by the target's architecture. For example, [the ARMv7-M reference manual](https://static.docs.arm.com/ddi0403/eb/DDI0403E_B_armv7m_arm.pdf) states that:

This header is prepended to the application binary during the signing process (explained later).
> The Vector table must be naturally aligned to a power of two whose alignment value is greater than or equal to (Number of Exceptions supported x 4), with a minimum alignment of 128 bytes.

#### Primary Application
The resulting minimum size depends on the SoC family, since the number of implemented exceptions differ. Check the reference manual of your MCU to calculate it.

The primary application is the currently installed, bootable application. In this demo it is the `mbed-mcuboot-blinky` application. The application start address is configured using `target.mbed_app_start` and the size can be restricted using `target.mbed_app_size`. The primary application size **must** be restricted to avoid colliding with the scratch space region (if used)!
To summarize, the header size, the primary slot address and the primary application address are related according to this equation:

`mcuboot.primary-slot-address + header-size = target.mbed_app_start`

#### Application TLV Trailers

Expand Down Expand Up @@ -170,7 +176,7 @@ The next step is to sign the main application binary.
**Note:** even if the internal main application is not verified (ie: the digital signature is not checked) this step **must** be performed so the appropriate application header info is prepended to the binary. mcuboot will not execute the internal main application if this header info is missing or corrupt.

```
imgtool sign -k signing-keys.pem --align 4 -v 1.2.3+4 --header-size 4096 --pad-header -S 0xC0000 --pad mbed-mcuboot-blinky.hex signed.hex
imgtool sign -k signing-keys.pem --align 4 -v 1.2.3+4 --header-size 4096 --pad-header -S 0xC0000 mbed-mcuboot-blinky.hex signed.hex
```

Explanation of each option:
Expand All @@ -181,8 +187,6 @@ Explanation of each option:
- `--header-size 4096`: this must be the same as the value specified in `mcuboot.header-size` configuration (4096 bytes by default)
- `--pad-header`: this tells imgtool to insert the entire header, including any necessary padding bytes.
- `-S 0xC0000`: this specifies the maximum size of the application ("slot size"). It **must** be the same as the configured `mcuboot.slot-size`!
- `--pad`: this should only be used for binaries you plan on initially flashing to your target at the factory. It pads the resulting binary to the slot size and adds initialized trailer TLVs. This is not needed for update binaries.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@boraozgen Have you tested this? It was my impression that the trailer TLVs are not appended by imgtool to the signed binary unless the --pad flag is included. I will have to test booting an application that has been signed without the --pad flag...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not use --pad at all, it works fine.

Copy link
Collaborator Author

@boraozgen boraozgen May 12, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my experience --pad pads the image to fit the slot and adds the trailers just before the end of the slot. I don't know what would be a use case for this.

Copy link
Contributor

@LDong-Arm LDong-Arm Aug 24, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The --pad option causes problems on DISCO_L475VG_IOT01A. I still don't fully understand how it works, but if we want to put the board in a clean state, it's way more reliable to erase the chip

pyocd erase --chip

Then run the demo from beginning to end, without --pad.

Copy link
Contributor

@LDong-Arm LDong-Arm Aug 24, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The latest MCUboot documentation explains it:

The optional --pad argument will place a trailer on the image that indicates that the image should be considered an upgrade. Writing this image in the secondary slot will then cause the bootloader to upgrade to it.

So it's intended for the secondary slot only, and

  • without --pad, the current application needs to call boot_set_pending() to active the secondary image and let MCUboot apply it.
  • with --pad, the secondary image already comes pre-activated, so MCUboot will apply it.
  • will --pad --confirm, the secondary image comes pre-activated and pre-confirmed, so MCUboot will apply it and there will be no need to boot_set_confirmed() afterwards.

It's no replacement for erasing the primary slot into a clean state by ourselves (or the manufacturing process).

Copy link
Contributor

@LDong-Arm LDong-Arm Aug 24, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So before flashing the initial primary image at the factory, it's the manufacturer's responsibility to put the chip in a clean state by erasing it. Using pyocd erase --chip is what we can do with Mbed boards. It makes sense that MCUboot doesn't provide a utility to clean up the primary slot, because it should be a prerequisite before we even place MCUboot onto the board.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pan- FYI, removing this --pad should resolve the problem you encountered.



### Creating the update binary

Expand Down