Skip to content

Commit

Permalink
Merge pull request wolfSSL#37 from danielinux/aarch64
Browse files Browse the repository at this point in the history
wolfBoot Aarch64 support (Xilinx Zynq and Raspberry Pi)
  • Loading branch information
danielinux authored Mar 23, 2020
2 parents 50c1cb7 + cd209e9 commit 6af52ad
Show file tree
Hide file tree
Showing 59 changed files with 4,696 additions and 437 deletions.
8 changes: 4 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,7 @@ src/rsa2048_pub_key.c
src/rsa4096_pub_key.c

# keygen binaries
tools/ed25519/ed25519_sign
tools/ed25519/ed25519_keygen
tools/ecc256/ecc256_sign
tools/ecc256/ecc256_keygen
tools/keytools/sign

# Vim swap files
.*.swp
Expand All @@ -84,3 +81,6 @@ config/*.ld

# Generated confiuguration file
.config
.vs
*.filters
*.user
232 changes: 232 additions & 0 deletions IDE/XilinxSDK/.cproject

Large diffs are not rendered by default.

26 changes: 26 additions & 0 deletions IDE/XilinxSDK/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>efuse_wolfboot</name>
<comment>Created by SDK v2018.2. standalone_bsp_0 - psu_cortexa53_0</comment>
<projects>
<project>standalone_bsp_0</project>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.cdt.managedbuilder.core.genmakebuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder</name>
<triggers>full,incremental,</triggers>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.cdt.core.cnature</nature>
<nature>org.eclipse.cdt.managedbuilder.core.managedBuildNature</nature>
<nature>org.eclipse.cdt.managedbuilder.core.ScannerConfigNature</nature>
</natures>
</projectDescription>
73 changes: 73 additions & 0 deletions IDE/XilinxSDK/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# Xilinx SDK wolfBoot Project

To use this example project:
1. Copy `.cproject` and `.project` into the wolfBoot root.
2. From the Xilinx SDK Import wolfBoot using "Import" -> "Existing Projects into Workspace".

## wolfBoot Configuration

A build settings template for Zynq UltraScale+ can be found here `./config/examples/zynqmp.config`. This file can be copied to wolfBoot root as `.config` for building from the command line. These template settings are also in this `.cproject` as preprocessor macros. These settings are loaded into the `target.h.in` template by the wolfBoot `make`. If not using the built-in make then the following defines will need to be manually created in `target.h`:

```
#define WOLFBOOT_SECTOR_SIZE 0x20000
#define WOLFBOOT_PARTITION_BOOT_ADDRESS 0x800000
#define WOLFBOOT_LOAD_ADDRESS 0x10000000
#define WOLFBOOT_PARTITION_SIZE 0x2A00000
#define WOLFBOOT_PARTITION_UPDATE_ADDRESS 0x3A00000
#define WOLFBOOT_PARTITION_SWAP_ADDRESS 0x63E0000
#define WOLFBOOT_DTS_BOOT_ADDRESS 0x7E0000
#define WOLFBOOT_DTS_UPDATE_ADDRESS 0x39E0000
#define WOLFBOOT_LOAD_DTS_ADDRESS 0x11800000
```

Note: If not using Position Independent Code (PIC) the linker script `ldscript.ld` must have the start address offset to match the `WOLFBOOT_LOAD_ADDRESS`.

## Signing Example

```sh
python3 ./tools/keytools/sign.py --rsa4096 --sha3 ../helloworld/Debug/helloworld.elf ./rsa4096.der 1
```

## Bootgen

Xilinx uses a `bootgen` tool for generating a boot binary image that has Xilinx headers, which the FSBL (First Stage Boot Loader) understands. See the `boot.bif` and `boot_auth.bif` as examples.

* Use "partition_owner=uboot" to prevent a partition from being loaded into RAM.
* Use "offset=" option to place the application into a specific location in flash.
* Use "load=" option to have FSBL load into specific location in RAM.

### Adding RSA Authentication

1. Generate keys:
* `bootgen.exe -generate_keys auth pem -arch zynqmp -image boot.bif`
2. Create hash for primary key:
* `bootgen.exe -image boot.bif -arch zynqmp -w -o i BOOT.BIN -efuseppkbits ppkf_hash.txt`
3. Import example project for programming eFuses:
* New BSP project (program efuses , ZCU102_hw_platform, standalone, CPU: PSU_cortexa53_0)
* Goto Xilinx Board Support Packet Settings.
* Scroll down to Supported Libraries and Check the xiskey libray
* In the system.mss pane, scroll down to Libraries and click Import Examples.
* Check the xilskey_esfuseps_zynqmp_example
4. Edit `xilskey_efuseps_zynqmp_input.h`
* 433 `#define XSK_EFUSEPS_WRITE_PPK0_HASH TRUE`
* 453 `#define XSK_EFUSEPS_PPK0_IS_SHA3 TRUE`
* 454 `#define XSK_EFUSEPS_PPK0_HASH "0000000000000000000000000000000000000000000000000000000000000000" /* from ppkf_hash.txt */``
5. Update boot.bif (see boot_auth.bif)

```
[auth_params] ppk_select=0; spk_id=0x00000000
[pskfile] pskf.pem
[sskfile] sskf.pem
authentication=rsa
```
6. Build “boot.bin” image:
* `bootgen.exe -image boot.bif -arch zynqmp -o i BOOT.BIN -w`
Note: To generate a report of a boot.bin use the `bootgen_utility`:
`bootgen_utility -arch zynqmp -bin boot.bin -out boot.bin.txt`
### References:
* [ZAPP1319](https://www.xilinx.com/support/documentation/application_notes/xapp1319-zynq-usp-prog-nvm.pdf): Programming BBRAM and eFUSEs
* [UG1283](https://www.xilinx.com/support/documentation/sw_manuals/xilinx2018_2/ug1283-bootgen-user-guide.pdf): Bootgen User Guide
11 changes: 11 additions & 0 deletions IDE/XilinxSDK/boot.bif
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Boot BIF example for wolfBoot with signed Hello World
// Note: "partition_owner=uboot" prevents partition from being loaded to RAM
the_ROM_image:
{
[bootloader, destination_cpu=a53-0] zynqmp_fsbl.elf
[destination_cpu=pmu] pmufw.elf
[destination_device=pl] system.bit
[destination_cpu=a53-0, exception_level=el-3, trustzone] bl31.elf
[destination_cpu=a53-0, exception_level=el-2] wolfboot.elf
[destination_cpu=a53-0, offset=0x800000, partition_owner=uboot] helloworld_v1_signed.bin
}
29 changes: 29 additions & 0 deletions IDE/XilinxSDK/boot_auth.bif
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Boot BIF example with Xilinx Secure Boot authentication and wolfBoot
the_ROM_image:
{
// Boot Header Authentication Enable
[fsbl_config] bh_auth_enable

// Use the primary public key 0 and secondary public key id 0
[auth_params] ppk_select=0; spk_id=0x00000000

// primary and secondary secret (private) keys
[pskfile] pskf.pem
[sskfile] sskf.pem

[bootloader, destination_cpu=a53-0, authentication=rsa] zynqmp_fsbl.elf
[destination_cpu=pmu, authentication=rsa] pmufw.elf
[destination_device=pl, authentication=rsa] system.bit
[destination_cpu=a53-0, exception_level=el-3, trustzone, authentication=rsa] bl31.elf
[destination_cpu=a53-0, authentication=rsa, load=0x11800000] system.dtb

// ARM Device Tree (loaded to RAM at 0x11800000 by wolfBoot)
[ destination_cpu=a53-0, authentication=rsa, offset=0x007E0000, partition_owner=uboot] system.dtb

// Second Stage wolfBoot Bootloader (in RAM at 0x0)
[destination_cpu=a53-0, exception_level=el-2, authentication=rsa] wolfboot.elf

// Kernel / Application (load to RAM at 0x10000000 by wolfBoot)
// Using partition_owner=uboot prevents FSBL from loading to RAM
[destination_cpu=a53-0, offset=0x800000, partition_owner=uboot] kernel.ui
}
27 changes: 23 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ OBJS:= \
./src/libwolfboot.o
WOLFCRYPT_OBJS:=
PUBLIC_KEY_OBJS:=
UPDATE_OBJS:=

ifeq ($(SIGN),RSA4096)
SPMATH=0
Expand Down Expand Up @@ -56,7 +57,7 @@ ifeq ($(SIGN),ED25519)
./lib/wolfssl/wolfcrypt/src/wolfmath.o \
./lib/wolfssl/wolfcrypt/src/fe_low_mem.o
PUBLIC_KEY_OBJS=./src/ed25519_pub_key.o
CFLAGS+=-DWOLFBOOT_SIGN_ED25519 -nostdlib -DWOLFSSL_STATIC_MEMORY \
CFLAGS+=-DWOLFBOOT_SIGN_ED25519 -nostdlib \
-Wstack-usage=1024
LDFLAGS+=-nostdlib
endif
Expand Down Expand Up @@ -119,6 +120,9 @@ endif

ifeq ($(EXT_FLASH),1)
CFLAGS+= -DEXT_FLASH=1 -DPART_UPDATE_EXT=1 -DPART_SWAP_EXT=1
ifeq ($(NO_XIP),1)
CFLAGS+=-DPART_BOOT_EXT=1
endif
endif

ifeq ($(ALLOW_DOWNGRADE),1)
Expand Down Expand Up @@ -151,6 +155,7 @@ ifeq ($(PKA),1)
endif

OBJS+=$(PUBLIC_KEY_OBJS)
OBJS+=$(UPDATE_OBJS)

ifeq ($(WOLFTPM),1)
OBJS += lib/wolfTPM/src/tpm2.o \
Expand Down Expand Up @@ -181,7 +186,7 @@ wolfboot.hex: wolfboot.elf

align: wolfboot-align.bin

.bootloader-partition-size: FORCE
.bootloader-partition-size:
@printf "%d" $(WOLFBOOT_PARTITION_BOOT_ADDRESS) > .wolfboot-offset
@printf "%d" $(ARCH_FLASH_OFFSET) > .wolfboot-arch-offset
@expr `cat .wolfboot-offset` - `cat .wolfboot-arch-offset` > .bootloader-partition-size
Expand All @@ -202,7 +207,7 @@ test-app/image.bin: wolfboot-align.bin

standalone:
@make -C test-app TARGET=$(TARGET) EXT_FLASH=$(EXT_FLASH) SPI_FLASH=$(SPI_FLASH) ARCH=$(ARCH) \
V=$(V) RAM_CODE=$(RAM_CODE) WOLFBOOT_VERSION=$(WOLFBOOT_VERSION)\
NO_XIP=$(NO_XIP) V=$(V) RAM_CODE=$(RAM_CODE) WOLFBOOT_VERSION=$(WOLFBOOT_VERSION)\
MCUXPRESSO=$(MCUXPRESSO) MCUXPRESSO_CPU=$(MCUXPRESSO_CPU) MCUXPRESSO_DRIVERS=$(MCUXPRESSO_DRIVERS) \
MCUXPRESSO_CMSIS=$(MCUXPRESSO_CMSIS) NVM_FLASH_WRITEONCE=$(NVM_FLASH_WRITEONCE) \
FREEDOM_E_SDK=$(FREEDOM_E_SDK) standalone
Expand All @@ -223,9 +228,18 @@ rsa2048.der:
rsa4096.der:
@python3 tools/keytools/keygen.py $(KEYGEN_OPTIONS) src/rsa4096_pub_key.c

keytools:
@make -C tools/keytools

factory.bin: $(BOOT_IMG) wolfboot-align.bin $(PRIVATE_KEY)
@echo "\t[SIGN] $(BOOT_IMG)"
ifneq ("$(wildcard ./tools/keytools/sign)","")
@echo "\n./tools/keytools/sign $(SIGN_OPTIONS) $(BOOT_IMG) $(PRIVATE_KEY) 1"
$(Q)./tools/keytools/sign $(SIGN_OPTIONS) $(BOOT_IMG) $(PRIVATE_KEY) 1
else
@echo "\npython3 tools/keytools/sign.py $(SIGN_OPTIONS) $(BOOT_IMG) $(PRIVATE_KEY) 1"
$(Q)python3 tools/keytools/sign.py $(SIGN_OPTIONS) $(BOOT_IMG) $(PRIVATE_KEY) 1
endif
@echo "\t[MERGE] $@"
@cat wolfboot-align.bin test-app/image_v1_signed.bin > $@

Expand Down Expand Up @@ -255,14 +269,19 @@ clean:

distclean: clean
@rm -f *.pem *.der tags ./src/ed25519_pub_key.c ./src/ecc256_pub_key.c ./src/rsa2048_pub_key.c include/target.h
@make -C tools/keytools clean

include/target.h: include/target.h.in FORCE
@cat include/target.h.in | \
sed -e "s/##WOLFBOOT_PARTITION_SIZE##/$(WOLFBOOT_PARTITION_SIZE)/g" | \
sed -e "s/##WOLFBOOT_SECTOR_SIZE##/$(WOLFBOOT_SECTOR_SIZE)/g" | \
sed -e "s/##WOLFBOOT_PARTITION_BOOT_ADDRESS##/$(WOLFBOOT_PARTITION_BOOT_ADDRESS)/g" | \
sed -e "s/##WOLFBOOT_PARTITION_UPDATE_ADDRESS##/$(WOLFBOOT_PARTITION_UPDATE_ADDRESS)/g" | \
sed -e "s/##WOLFBOOT_PARTITION_SWAP_ADDRESS##/$(WOLFBOOT_PARTITION_SWAP_ADDRESS)/g" \
sed -e "s/##WOLFBOOT_PARTITION_SWAP_ADDRESS##/$(WOLFBOOT_PARTITION_SWAP_ADDRESS)/g" | \
sed -e "s/##WOLFBOOT_DTS_BOOT_ADDRESS##/$(WOLFBOOT_DTS_BOOT_ADDRESS)/g" | \
sed -e "s/##WOLFBOOT_DTS_UPDATE_ADDRESS##/$(WOLFBOOT_DTS_UPDATE_ADDRESS)/g" | \
sed -e "s/##WOLFBOOT_LOAD_ADDRESS##/$(WOLFBOOT_LOAD_ADDRESS)/g" | \
sed -e "s/##WOLFBOOT_LOAD_DTS_ADDRESS##/$(WOLFBOOT_LOAD_DTS_ADDRESS)/g" \
> $@

config: FORCE
Expand Down
15 changes: 6 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ microcontrollers will be added later. Relocating the interrupt vector can be dis
- Change the entry point of the firmware image to account for bootloader presence
- Equip the application with the [wolfBoot library](docs/API.md) to interact with the bootloader
- [Configure and compile](docs/compile.md) a bootable image with a single "make" command
- For help signing firmware see [wolfBoot Signing](docs/Signing.md)

### Examples provided

Expand Down Expand Up @@ -101,23 +102,19 @@ Traceback (most recent call last):
AttributeError: 'EccPrivate' object has no attribute 'sign_raw'
```

You need to install the latest wolfcrypt-pi here: https://github.com/wolfSSL/wolfcrypt-py
You need to install the latest wolfcrypt-py here: https://github.com/wolfSSL/wolfcrypt-py

Use `pip3 install wolfcrypt`.
Make sure the wolfSSL library has been built with:
```sh

```

To install based on a local wolfSSL installation use:
Or to install based on a local wolfSSL installation use:

```sh
cd youwolfssldir
./configure --enable-keygen --enable-rsa --enable-ecc --enable-ed25519 CFLAGS="-DWOLFSSL_PUBLIC_MP"
cd wolfssl
./configure --enable-keygen --enable-rsa --enable-ecc --enable-ed25519 --enable-des3 CFLAGS="-DFP_MAX_BITS=8192 -DWOLFSSL_PUBLIC_MP"
make
sudo make install

cd yourwolfcryptpydir
cd wolfcrypt-py
USE_LOCAL_WOLFSSL=/usr/local pip3 install .
```

Expand Down
19 changes: 19 additions & 0 deletions arch.mk
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## CPU Architecture selection via $ARCH

UPDATE_OBJS:=./src/update_flash.o

# check for FASTMATH or SP_MATH
ifeq ($(SPMATH),1)
MATH_OBJS:=./lib/wolfssl/wolfcrypt/src/sp_int.o
Expand Down Expand Up @@ -29,6 +31,13 @@ endif
WOLFCRYPT_OBJS+=./lib/wolfssl/wolfcrypt/src/sha256.o

## ARM
ifeq ($(ARCH),AARCH64)
CROSS_COMPILE:=aarch64-none-elf-
CFLAGS+=-DARCH_AARCH64 -march=armv8-a
OBJS+=src/boot_aarch64.o src/boot_aarch64_start.o
CFLAGS+=-DNO_QNX
endif

ifeq ($(ARCH),ARM)
CROSS_COMPILE:=arm-none-eabi-
CFLAGS+=-mthumb -mlittle-endian -mthumb-interwork -DARCH_ARM
Expand Down Expand Up @@ -149,3 +158,13 @@ ifeq ($(TARGET),stm32wb)
-DSTM32WB55xx
endif
endif


## Update mechanism
ifeq ($(ARCH),AARCH64)
CFLAGS+=-DMMU
UPDATE_OBJS:=src/update_ram.o
endif
ifeq ($(DUALBANK_SWAP),1)
UPDATE_OBJS:=src/update_flash_hwswap.o
endif
1 change: 1 addition & 0 deletions config/examples/hifive1.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
ARCH?=RISCV
TARGET?=hifive1
SIGN?=ECC256
HASH?=SHA256
FREEDOM_E_SDK?=~/src/freedom-e-sdk
DEBUG?=0
VTOR?=1
Expand Down
1 change: 1 addition & 0 deletions config/examples/kinetis-k82f.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
ARCH?=ARM
TARGET?=kinetis
SIGN?=ECC256
HASH?=SHA256
MCUXPRESSO?=$(HOME)/src/FRDM-K82F
MCUXPRESSO_CPU?=MK82FN256VLL15
MCUXPRESSO_DRIVERS?=$(MCUXPRESSO)/devices/MK82F25615
Expand Down
29 changes: 29 additions & 0 deletions config/examples/raspi3.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
ARCH?=AARCH64
TARGET?=raspi3
SIGN?=RSA4096
HASH?=SHA3
DEBUG?=1
VTOR?=1
CORTEX_M0?=0
NO_ASM?=0
EXT_FLASH?=0
SPI_FLASH?=0
ALLOW_DOWNGRADE?=0
NVM_FLASH_WRITEONCE?=0
WOLFBOOT_VERSION?=0
V?=0
SPMATH?=1
RAM_CODE?=0
DUALBANK_SWAP?=0
IMAGE_HEADER_SIZE?=256
PKA?=1
WOLFTPM?=0
WOLFBOOT_PARTITION_SIZE=0xF60000
WOLFBOOT_SECTOR_SIZE=0x20000
WOLFBOOT_PARTITION_BOOT_ADDRESS?=0xc0000
WOLFBOOT_PARTITION_UPDATE_ADDRESS?=0x10c0000
WOLFBOOT_PARTITION_SWAP_ADDRESS?=0x1000000
WOLFBOOT_LOAD_ADDRESS?=0x3000000
WOLFBOOT_LOAD_DTS_ADDRESS?=0x4000000
WOLFBOOT_DTS_BOOT_ADDRESS?=0xa0000
WOLFBOOT_DTS_UPDATE_ADDRESS?=0x10a0000
1 change: 1 addition & 0 deletions config/examples/stm32f4.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
ARCH?=ARM
TARGET?=stm32f4
SIGN?=ED25519
HASH?=SHA256
DEBUG?=0
VTOR?=1
CORTEX_M0?=0
Expand Down
1 change: 1 addition & 0 deletions config/examples/stm32f7-dualbank.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
ARCH?=ARM
TARGET?=stm32f7
SIGN?=ECC256
HASH?=SHA256
DEBUG?=0
VTOR?=1
CORTEX_M0?=0
Expand Down
1 change: 1 addition & 0 deletions config/examples/stm32g0.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
ARCH?=ARM
TARGET?=stm32g0
SIGN?=ED25519
HASH?=SHA256
DEBUG?=0
VTOR?=1
CORTEX_M0?=1
Expand Down
Loading

0 comments on commit 6af52ad

Please sign in to comment.