Skip to content

Commit ec66c47

Browse files
committed
First version of the bootloader
1 parent 887c90b commit ec66c47

45 files changed

Lines changed: 5888 additions & 8 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,20 @@
4141
*.su
4242
*.idb
4343
*.pdb
44+
*.gdb
45+
46+
# automatically generated keys
47+
*.der
48+
*.pem
49+
50+
# automatically generated source files
51+
src/ed25519_pub_key.c
52+
53+
54+
55+
56+
# tags etc.
57+
cscope.out
58+
tags
59+
4460

45-
# Kernel Module Compile Results
46-
*.mod*
47-
*.cmd
48-
.tmp_versions/
49-
modules.order
50-
Module.symvers
51-
Mkfile.old
52-
dkms.conf

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "lib/wolfssl"]
2+
path = lib/wolfssl
3+
url = https://github.com/wolfSSL/wolfssl.git

Makefile

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
CROSS_COMPILE:=arm-none-eabi-
2+
CC:=$(CROSS_COMPILE)gcc
3+
LD:=$(CROSS_COMPILE)gcc
4+
5+
6+
OBJCOPY:=$(CROSS_COMPILE)objcopy
7+
SIZE:=$(CROSS_COMPILE)size
8+
BOOT_IMG?=test-app/image.bin
9+
BOOTLOADER_SIZE?=0x10000
10+
SIGN?=ED25519
11+
TARGET?=stm32f4
12+
DEBUG?=0
13+
14+
LSCRIPT:=hal/$(TARGET).ld
15+
16+
OBJS:= \
17+
./hal/$(TARGET).o \
18+
./lib/bootutil/src/loader.o \
19+
./lib/bootutil/src/image_validate.o \
20+
./lib/bootutil/src/bootutil_misc.o \
21+
./src/run.o \
22+
./src/mem.o \
23+
./src/keys.o \
24+
./src/crypto.o \
25+
./src/startup_bl.o \
26+
./src/main.o \
27+
./lib/wolfssl/wolfcrypt/src/sha256.o \
28+
./lib/wolfssl/wolfcrypt/src/hash.o \
29+
./lib/wolfssl/wolfcrypt/src/wolfmath.o \
30+
./lib/wolfssl/wolfcrypt/src/fe_low_mem.o
31+
32+
CFLAGS:=-mcpu=cortex-m3 -mthumb -Wall -Wno-main -Wstack-usage=1024 -ffreestanding -Wno-unused \
33+
-Ilib/bootutil/include -Iinclude/ -Ilib/wolfssl -nostartfiles \
34+
-DBOOT_MAX_IMG_SECTORS=256 -DWOLFBOOT_VALIDATE_SLOT0 -DWOLFBOOT_USE_FLASHAREA_GET_SECTORS \
35+
-nostdlib \
36+
-DWOLFSSL_USER_SETTINGS \
37+
-DPLATFORM_$(TARGET)
38+
39+
ifeq ($(SIGN),ED25519)
40+
OBJS+= ./lib/wolfssl/wolfcrypt/src/sha512.o \
41+
./lib/wolfssl/wolfcrypt/src/ed25519.o \
42+
./lib/wolfssl/wolfcrypt/src/ge_low_mem.o \
43+
./src/ed25519_pub_key.o
44+
CFLAGS+=-DBOOT_SIGN_ED25519
45+
endif
46+
47+
ifeq ($(SIGN),EC256)
48+
OBJS+= ./ext/wolfssl/wolfcrypt/src/ecc.o
49+
CFLAGS+=-DBOOT_SIGN_EC256
50+
endif
51+
52+
53+
54+
ifeq ($(DEBUG),1)
55+
CFLAGS+=-O0 -g -ggdb3 -DDEBUG=1
56+
else
57+
CFLAGS+=-Os
58+
endif
59+
60+
61+
62+
LDFLAGS:=-T $(LSCRIPT) -Wl,-gc-sections -Wl,-Map=wolfboot.map -ffreestanding -nostartfiles -mcpu=cortex-m3 -mthumb -nostdlib
63+
64+
all: factory.bin
65+
66+
67+
wolfboot.bin: wolfboot.elf
68+
$(OBJCOPY) -O binary $^ $@
69+
$(SIZE) wolfboot.elf
70+
71+
align: wolfboot-align.bin
72+
73+
wolfboot-align.bin: wolfboot.elf
74+
$(OBJCOPY) -O binary $^ $@ --pad-to=$(BOOTLOADER_SIZE) --gap-fill=255
75+
$(SIZE) wolfboot.elf
76+
77+
test-app/image.bin:
78+
make -C test-app TARGET=$(TARGET)
79+
80+
tools/ed25519/ed25519_sign:
81+
make -C tools/ed25519
82+
83+
ed25519.der: tools/ed25519/ed25519_sign
84+
tools/ed25519/ed25519_keygen src/ed25519_pub_key.c
85+
86+
87+
factory.bin: $(BOOT_IMG) wolfboot-align.bin tools/ed25519/ed25519_sign ed25519.der
88+
tools/ed25519/ed25519_sign $(BOOT_IMG) ed25519.der 1
89+
cat wolfboot-align.bin $(BOOT_IMG).v1.signed > $@
90+
91+
second.img: $(BOOT_IMG) wolfboot-align.bin tools/ed25519/ed25519_sign ed25519.der
92+
tools/ed25519/ed25519_sign $(BOOT_IMG) ed25519.der 1 65536
93+
tools/ed25519/ed25519_sign $(BOOT_IMG) ed25519.der 2
94+
cat wolfboot-align.bin $(BOOT_IMG).v1.signed $(BOOT_IMG).v2.signed > $@
95+
96+
wolfboot.elf: $(OBJS) $(LSCRIPT)
97+
grep stat $(OBJS)
98+
$(LD) $(LDFLAGS) -Wl,--start-group $(OBJS) -Wl,--end-group -o $@
99+
100+
src/ed25519_pub_key.c: ed25519.der
101+
102+
keys: ed25519.der
103+
104+
105+
clean:
106+
rm -f *.bin *.elf $(OBJS) wolfboot.map *.bin
107+
make -C test-app clean
108+
109+
distclean: clean
110+
make -C tools/ed25519 clean
111+
rm -f *.pem *.der tags ./src/ed25519_pub_key.c
112+

NOTICE

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
wolfBoot - copyright (c) wolfSSL Inc.
2+
3+
wolfBoot is free software; you can redistribute it and/or modify
4+
it under the terms of the GNU General Public License version 2, as published by
5+
the Free Software Foundation.
6+
7+
wolfBoot, wolfSSL (formerly known as CyaSSL) and wolfCrypt are either licensed for use
8+
under the GPLv2 or a standard commercial license. For our users who cannot use
9+
wolfSSL under GPLv2, a commercial license to wolfBoot, wolfSSL and wolfCrypt is available.
10+
Please contact wolfSSL Inc. directly at:
11+
12+
Email: licensing@wolfssl.com
13+
Phone: +1 425 245-8247
14+
15+
More information can be found on the wolfSSL website at www.wolfssl.com.
16+
17+
This product includes software developed at
18+
The Apache Software Foundation (http://www.apache.org/)
19+
and previously released under an Open Source permissive license.
20+
21+
Portions of this software were developed at
22+
Runtime Inc, copyright 2015 and previously released under an Open Source permissive license.

hal/nrf52.c

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/* nrf52.c
2+
*
3+
* Copyright (C) 2018 wolfSSL Inc.
4+
*
5+
* This file is part of wolfBoot.
6+
*
7+
* wolfBoot is free software; you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation; either version 2 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* wolfBoot is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
20+
*/
21+
22+
#include <stdint.h>
23+
24+
/* Assembly helpers */
25+
#define DMB() __asm__ volatile ("dmb")
26+
27+
28+
/* Instantiation */
29+
#define CLOCK_CONTROL_BASE (0x40000000)
30+
#define NVMC_BASE (0x4001E000)
31+
32+
33+
/* Flash write/erase control */
34+
#define NVMC_CONFIG *((volatile uint32_t *)(NVMC_BASE + 0x504))
35+
#define NVMC_ERASEPAGE *((volatile uint32_t *)(NVMC_BASE + 0x508))
36+
#define NVMC_READY *((volatile uint32_t *)(NVMC_BASE + 0x400))
37+
#define NVMC_CONFIG_REN 0
38+
#define NVMC_CONFIG_WEN 1
39+
#define NVMC_CONFIG_EEN 2
40+
41+
#define FLASH_PAGE_SIZE (4096)
42+
43+
/* Clock control */
44+
#define TASKS_HFCLKSTART *((volatile uint32_t *)(CLOCK_CONTROL_BASE + 0x000))
45+
#define TASKS_HFCLKSTOP *((volatile uint32_t *)(CLOCK_CONTROL_BASE + 0x004))
46+
#define TASKS_HFCLKSTARTED *((volatile uint32_t *)(CLOCK_CONTROL_BASE + 0x100))
47+
48+
static void flash_wait_complete(void)
49+
{
50+
while (NVMC_READY == 0)
51+
;
52+
}
53+
54+
int hal_flash_write(uint32_t address, const uint8_t *data, int len)
55+
{
56+
int i;
57+
uint32_t val;
58+
NVMC_CONFIG = NVMC_CONFIG_WEN;
59+
flash_wait_complete();
60+
/* Set 8-bit write */
61+
for (i = 0; i < len; i++) {
62+
((uint8_t *)(address))[i] = data[i];
63+
flash_wait_complete();
64+
}
65+
return 0;
66+
}
67+
68+
void hal_flash_unlock(void)
69+
{
70+
}
71+
72+
void hal_flash_lock(void)
73+
{
74+
}
75+
76+
77+
int hal_flash_erase(uint32_t address, int len)
78+
{
79+
int start = -1, end = -1, i;
80+
uint32_t end_address = address + len;
81+
start = (address / FLASH_PAGE_SIZE);
82+
if (start == 0)
83+
return -1;
84+
end = end_address / FLASH_PAGE_SIZE;
85+
NVMC_CONFIG = NVMC_CONFIG_EEN;
86+
flash_wait_complete();
87+
for (i = start; i <= end; i++) {
88+
NVMC_ERASEPAGE = i;
89+
flash_wait_complete();
90+
}
91+
return 0;
92+
}
93+
94+
void hal_init(void)
95+
{
96+
TASKS_HFCLKSTART = 1;
97+
while(TASKS_HFCLKSTARTED == 0)
98+
;
99+
}
100+
101+
void hal_prepare_boot(void)
102+
{
103+
TASKS_HFCLKSTOP = 1;
104+
}
105+

hal/nrf52.ld

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
MEMORY
2+
{
3+
FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 0x001FFE0
4+
RAM (rwx) : ORIGIN = 0x20000000, LENGTH = 0x00010000
5+
}
6+
7+
SECTIONS
8+
{
9+
.text :
10+
{
11+
_start_text = .;
12+
KEEP(*(.isr_vector))
13+
*(.text*)
14+
*(.rodata*)
15+
*(.init*)
16+
*(.fini*)
17+
. = ALIGN(4);
18+
_end_text = .;
19+
} > FLASH
20+
21+
.edidx :
22+
{
23+
. = ALIGN(4);
24+
*(.ARM.exidx*)
25+
} > FLASH
26+
27+
_stored_data = .;
28+
29+
.data : AT (_stored_data)
30+
{
31+
_start_data = .;
32+
KEEP(*(.data*))
33+
. = ALIGN(4);
34+
_end_data = .;
35+
} > RAM
36+
37+
.bss (NOLOAD) :
38+
{
39+
_start_bss = .;
40+
__bss_start__ = .;
41+
*(.bss*)
42+
*(COMMON)
43+
. = ALIGN(4);
44+
_end_bss = .;
45+
__bss_end__ = .;
46+
_end = .;
47+
} > RAM
48+
. = ALIGN(4);
49+
}
50+
51+
END_STACK = ORIGIN(RAM) + LENGTH(RAM);

0 commit comments

Comments
 (0)