Skip to content

Commit 75efbd9

Browse files
committed
Removed all compile-time dependency from keytools
1 parent 97fb3b6 commit 75efbd9

37 files changed

+154
-161
lines changed

Makefile

+6-3
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ ifeq ($(TARGET),ti_hercules)
8585
LSCRIPT_FLAGS+=--run_linker $(LSCRIPT)
8686
endif
8787

88+
# Environment variables for sign tool
89+
SIGN_ENV=IMAGE_HEADER_SIZE=$(IMAGE_HEADER_SIZE) WOLFBOOT_SECTOR_SIZE=$(WOLFBOOT_SECTOR_SIZE)
90+
8891

8992
MAIN_TARGET=factory.bin
9093
TARGET_H_TEMPLATE:=include/target.h.in
@@ -218,7 +221,7 @@ $(SECONDARY_PRIVATE_KEY): $(PRIVATE_KEY) keystore.der
218221
-g $(SECONDARY_PRIVATE_KEY)) || true
219222
$(Q)(test "$(FLASH_OTP_KEYSTORE)" = "1") && (make -C tools/keytools/otp) || true
220223

221-
keytools: include/target.h
224+
keytools:
222225
@echo "Building key tools"
223226
@$(MAKE) -C tools/keytools -s clean
224227
@$(MAKE) -C tools/keytools -j
@@ -238,10 +241,10 @@ test-app/image_v1_signed.bin: $(BOOT_IMG)
238241
@echo "\tSECONDARY_SIGN_OPTIONS=$(SECONDARY_SIGN_OPTIONS)"
239242
@echo "\tSECONDARY_PRIVATE_KEY=$(SECONDARY_PRIVATE_KEY)"
240243

241-
$(Q)(test $(SIGN) = NONE) || IMAGE_HEADER_SIZE=$(IMAGE_HEADER_SIZE) "$(SIGN_TOOL)" $(SIGN_OPTIONS) \
244+
$(Q)(test $(SIGN) = NONE) || $(SIGN_ENV) $(SIGN_TOOL) $(SIGN_OPTIONS) \
242245
$(SECONDARY_SIGN_OPTIONS) $(BOOT_IMG) $(PRIVATE_KEY) \
243246
$(SECONDARY_PRIVATE_KEY) 1 || true
244-
$(Q)(test $(SIGN) = NONE) && IMAGE_HEADER_SIZE=$(IMAGE_HEADER_SIZE) "$(SIGN_TOOL)" $(SIGN_OPTIONS) $(BOOT_IMG) 1 || true
247+
$(Q)(test $(SIGN) = NONE) && $(SIGN_ENV) $(SIGN_TOOL) $(SIGN_OPTIONS) $(BOOT_IMG) 1 || true
245248

246249
test-app/image.elf: wolfboot.elf
247250
$(Q)$(MAKE) -C test-app WOLFBOOT_ROOT="$(WOLFBOOT_ROOT)" image.elf

include/delta.h

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ int wb_patch_init(WB_PATCH_CTX *bm, uint8_t *src, uint32_t ssz, uint8_t *patch,
7272
int wb_patch(WB_PATCH_CTX *ctx, uint8_t *dst, uint32_t len);
7373
int wolfBoot_get_delta_info(uint8_t part, int inverse, uint32_t **img_offset,
7474
uint32_t **img_size, uint8_t **base_hash, uint16_t *base_hash_size);
75+
int wb_diff_get_sector_size(void);
7576

7677
#endif
7778

include/wolfboot/wolfboot.h

+2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ extern "C" {
3030
#endif
3131

3232
#include <stdint.h>
33+
#ifdef __WOLFBOOT
3334
#include "target.h"
35+
#endif
3436
#include "wolfboot/version.h"
3537

3638
#ifdef WOLFCRYPT_SECURE_MODE

src/delta.c

+41-8
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@
2222
#include <stdint.h>
2323
#include <string.h>
2424
#include <delta.h>
25-
#include <target.h> /* WOLFBOOT_SECTOR_SIZE */
2625

2726

2827
#define ESC 0x7f
2928

29+
3030
#if (defined(__IAR_SYSTEMS_ICC__) && (__IAR_SYSTEMS_ICC__ > 8)) || \
3131
defined(__GNUC__)
3232
#define BLOCK_HDR_PACKED __attribute__ ((packed))
@@ -46,7 +46,7 @@ struct BLOCK_HDR_PACKED block_hdr {
4646
#include "encrypt.h"
4747
#define ext_flash_check_write ext_flash_encrypt_write
4848
#define ext_flash_check_read ext_flash_decrypt_read
49-
#else
49+
#elif defined(__WOLFBOOT)
5050
#include "hal.h"
5151
#define ext_flash_check_write ext_flash_write
5252
#define ext_flash_check_read ext_flash_read
@@ -169,6 +169,36 @@ int wb_patch(WB_PATCH_CTX *ctx, uint8_t *dst, uint32_t len)
169169
return dst_off;
170170
}
171171

172+
#ifndef __WOLFBOOT
173+
174+
#include <stdio.h>
175+
#include <stdlib.h>
176+
#include <errno.h>
177+
178+
static uint32_t wolfboot_sector_size = 0;
179+
180+
int wb_diff_get_sector_size(void)
181+
{
182+
uint32_t sec_sz = 0;
183+
char *env_sector_size = NULL;
184+
env_sector_size = getenv("WOLFBOOT_SECTOR_SIZE");
185+
if (!env_sector_size) {
186+
fprintf(stderr, "Please set the WOLFBOOT_SECTOR_SIZE environment variable in\n"
187+
"order to sign a delta update.\n");
188+
exit(6);
189+
} else {
190+
sec_sz = atoi(env_sector_size);
191+
if (sec_sz == 0) {
192+
errno = 0;
193+
sec_sz = strtol(env_sector_size, NULL, 16);
194+
if (errno != 0) {
195+
fprintf(stderr, "Invalid WOLFBOOT_SECTOR_SIZE value\n");
196+
exit(6);
197+
}
198+
}
199+
}
200+
return sec_sz;
201+
}
172202

173203
int wb_diff_init(WB_DIFF_CTX *ctx, uint8_t *src_a, uint32_t len_a, uint8_t *src_b, uint32_t len_b)
174204
{
@@ -179,6 +209,8 @@ int wb_diff_init(WB_DIFF_CTX *ctx, uint8_t *src_a, uint32_t len_a, uint8_t *src_
179209
ctx->src_b = src_b;
180210
ctx->size_a = len_a;
181211
ctx->size_b = len_b;
212+
wolfboot_sector_size = wb_diff_get_sector_size();
213+
printf("WOLFBOOT_SECTOR_SIZE: %u\n", wolfboot_sector_size);
182214
return 0;
183215
}
184216

@@ -196,7 +228,7 @@ int wb_diff(WB_DIFF_CTX *ctx, uint8_t *patch, uint32_t len)
196228
return -1;
197229

198230
while ((ctx->off_b + BLOCK_HDR_SIZE < ctx->size_b) && (len > p_off + BLOCK_HDR_SIZE)) {
199-
uintptr_t page_start = ctx->off_b / WOLFBOOT_SECTOR_SIZE;
231+
uintptr_t page_start = ctx->off_b / wolfboot_sector_size;
200232
uintptr_t pa_start;
201233
found = 0;
202234
if (p_off + BLOCK_HDR_SIZE > len)
@@ -210,14 +242,14 @@ int wb_diff(WB_DIFF_CTX *ctx, uint8_t *patch, uint32_t len)
210242
* base for the sectors that have already been updated.
211243
*/
212244

213-
pa_start = WOLFBOOT_SECTOR_SIZE * page_start;
245+
pa_start = wolfboot_sector_size * page_start;
214246
pa = ctx->src_a + pa_start;
215247
while (((uintptr_t)(pa - ctx->src_a) < (uintptr_t)ctx->size_a) && (p_off < len)) {
216248
if ((uintptr_t)(ctx->size_a - (pa - ctx->src_a)) < BLOCK_HDR_SIZE)
217249
break;
218250
if ((ctx->size_b - ctx->off_b) < BLOCK_HDR_SIZE)
219251
break;
220-
if ((WOLFBOOT_SECTOR_SIZE - (ctx->off_b % WOLFBOOT_SECTOR_SIZE)) < BLOCK_HDR_SIZE)
252+
if ((wolfboot_sector_size - (ctx->off_b % wolfboot_sector_size)) < BLOCK_HDR_SIZE)
221253
break;
222254
if ((memcmp(pa, (ctx->src_b + ctx->off_b), BLOCK_HDR_SIZE) == 0)) {
223255
uintptr_t b_start;
@@ -238,7 +270,7 @@ int wb_diff(WB_DIFF_CTX *ctx, uint8_t *patch, uint32_t len)
238270
/* Stop matching if the source image size limit is hit. */
239271
break;
240272
}
241-
if ((b_start / WOLFBOOT_SECTOR_SIZE) < ((ctx->off_b + 1) / WOLFBOOT_SECTOR_SIZE)) {
273+
if ((b_start / wolfboot_sector_size) < ((ctx->off_b + 1) / wolfboot_sector_size)) {
242274
/* Stop matching when the sector bound is hit. */
243275
break;
244276
}
@@ -262,7 +294,7 @@ int wb_diff(WB_DIFF_CTX *ctx, uint8_t *patch, uint32_t len)
262294
}
263295
if (!found) {
264296
/* Try matching an earlier section in the resulting image */
265-
uintptr_t pb_end = page_start * WOLFBOOT_SECTOR_SIZE;
297+
uintptr_t pb_end = page_start * wolfboot_sector_size;
266298
pb = ctx->src_b;
267299
while (((uintptr_t)(pb - ctx->src_b) < pb_end) && (p_off < len)) {
268300
/* Check image boundary */
@@ -274,7 +306,7 @@ int wb_diff(WB_DIFF_CTX *ctx, uint8_t *patch, uint32_t len)
274306
/* Don't try matching backwards if the distance between the two
275307
* blocks is smaller than one sector.
276308
*/
277-
if (WOLFBOOT_SECTOR_SIZE > (page_start * WOLFBOOT_SECTOR_SIZE)
309+
if (wolfboot_sector_size > (page_start * wolfboot_sector_size)
278310
- (pb - ctx->src_b))
279311
break;
280312

@@ -338,5 +370,6 @@ int wb_diff(WB_DIFF_CTX *ctx, uint8_t *patch, uint32_t len)
338370
}
339371
return (int)p_off;
340372
}
373+
#endif /* __WOLFBOOT */
341374

342375
#endif /* DELTA_UPDATES */

test-app/app_hifive1.c

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <string.h>
2525
#include "hal.h"
2626
#include "wolfboot/wolfboot.h"
27+
#include "target.h"
2728

2829
/* Change to '1' to enable uart update */
2930
#define UART_UPDATE 0

test-app/app_imx_rt.c

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "fsl_debug_console.h"
2525
#include "fsl_gpio.h"
2626
#include "fsl_iomuxc.h"
27+
#include "target.h"
2728

2829
static int g_pinSet = false;
2930
extern void imx_rt_init_boot_clock(void);

test-app/app_kinetis.c

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "fsl_gpio.h"
2828
#include "fsl_clock.h"
2929
#include "wolfboot/wolfboot.h"
30+
#include "target.h"
3031

3132
/* FRDM-K64 board */
3233
#if defined(CPU_MK64FN1M0VLL12)

test-app/app_mcxa.c

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "fsl_clock.h"
3232

3333
#include "wolfboot/wolfboot.h"
34+
#include "target.h"
3435

3536
extern void hal_init(void);
3637

test-app/app_nrf52.c

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "wolfboot/wolfboot.h"
2626
#include "hal/nrf52.h"
2727
#include "printf.h"
28+
#include "target.h"
2829

2930
static const char extradata[1024 * 16] = "hi!";
3031

test-app/app_nrf5340.c

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "wolfboot/wolfboot.h"
2626
#include "hal/nrf5340.h"
2727
#include "printf.h"
28+
#include "target.h"
2829

2930
void gpiotoggle(uint32_t port, uint32_t pin)
3031
{

test-app/app_nrf5340_net.c

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "wolfboot/wolfboot.h"
2626
#include "hal/nrf5340.h"
2727
#include "printf.h"
28+
#include "target.h"
2829

2930
void gpiotoggle(uint32_t port, uint32_t pin)
3031
{

test-app/app_renesas_rx.c

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "hal.h"
2929
#include "printf.h"
3030
#include "wolfboot/wolfboot.h"
31+
#include "target.h"
3132

3233
/* route stdout to UART */
3334
int write(int fileno, char *buf, int count)

test-app/app_sim.c

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <string.h>
2727
#include <stdio.h>
2828
#include <unistd.h>
29+
#include "target.h"
2930

3031
#include "wolfboot/wolfboot.h"
3132

test-app/app_stm32f4.c

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "hal.h"
3131
#include "wolfboot/wolfboot.h"
3232
#include "spi_flash.h"
33+
#include "target.h"
3334

3435
#ifdef TARGET_stm32f4
3536

test-app/app_stm32f7.c

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "system.h"
2828
#include "wolfboot/wolfboot.h"
2929
#include "hal.h"
30+
#include "target.h"
3031

3132

3233
/* UART module */

test-app/app_stm32h5.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "uart_drv.h"
3333
#include "wolfboot/wolfboot.h"
3434
#include "keystore.h"
35+
#include "target.h"
3536

3637
#ifdef SECURE_PKCS11
3738
#include "wcs/user_settings.h"
@@ -867,4 +868,4 @@ void * _sbrk(unsigned int incr)
867868
}
868869
return old_heap;
869870
}
870-
#endif
871+
#endif

test-app/app_stm32h7.c

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include "system.h"
3939
#include "hal.h"
4040
#include "wolfboot/wolfboot.h"
41+
#include "target.h"
4142

4243
#define SET_BIT(REG, BIT) ((REG) |= (BIT))
4344
#define CLEAR_BIT(REG, BIT) ((REG) &= ~(BIT))

test-app/app_stm32l0.c

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#ifdef SPI_FLASH
3030
#include "spi_flash.h"
3131
#endif
32+
#include "target.h"
3233

3334
#ifdef TARGET_stm32l0
3435

test-app/app_stm32l4.c

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "led.h"
2626
#include "hal.h"
2727
#include "wolfboot/wolfboot.h"
28+
#include "target.h"
2829

2930
#ifdef TARGET_stm32l4
3031

test-app/app_stm32l5.c

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "uart_drv.h"
3131
#include "wolfboot/wolfboot.h"
3232
#include "wolfboot/wc_secure.h"
33+
#include "target.h"
3334

3435
#ifdef SECURE_PKCS11
3536
#include "wcs/user_settings.h"

test-app/app_stm32u5.c

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "system.h"
2929
#include "hal.h"
3030
#include "wolfboot/wolfboot.h"
31+
#include "target.h"
3132

3233
#define LED_BOOT_PIN (7) /* PH7 - Discovery - Green Led */
3334
#define LED_USR_PIN (6) /* PH6 - Discovery - Red Led */

test-app/app_stm32wb.c

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "hal.h"
2929
#include "wolfboot/wolfboot.h"
3030
#include "uart_drv.h"
31+
#include "target.h"
3132

3233
#ifdef TARGET_stm32wb
3334

tools/efi/compile_efi_linux.sh

+2-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ WORK_DIR=/tmp/wolfBoot_efi
44
BR_VER=2022.08.3
55
BR_DIR=buildroot-$BR_VER
66
IMAGE_DIR=$WORK_DIR/output
7+
. .config
78

89
if (test ! -d $WORK_DIR);then
910
mkdir -p $WORK_DIR
@@ -17,10 +18,7 @@ fi
1718
BR2_EXTERNAL=$(pwd)/tools/efi/br_ext_dir make -C $WORK_DIR/$BR_DIR tiny_defconfig O=$IMAGE_DIR
1819
make -C $WORK_DIR/$BR_DIR O=$IMAGE_DIR
1920

20-
SIGN_TOOL="python3 ./tools/keytools/sign.py"
21-
if [ -f "./tools/keytools/sign" ]; then
22-
SIGN_TOOL="./tools/keytools/sign"
23-
fi
21+
SIGN_TOOL="./tools/keytools/sign"
2422

2523
$SIGN_TOOL --ed25519 $IMAGE_DIR/images/bzImage wolfboot_signing_private_key.der 1
2624
$SIGN_TOOL --ed25519 $IMAGE_DIR/images/bzImage wolfboot_signing_private_key.der 2

tools/keytools/Makefile

+1-15
Original file line numberDiff line numberDiff line change
@@ -177,25 +177,11 @@ endif
177177

178178
.PHONY: clean all
179179

180-
all: $(WOLFBOOTDIR)/include/target.h sign keygen
180+
all: sign keygen
181181

182182
debug: CFLAGS+=$(DEBUG_FLAGS)
183183
debug: all
184184

185-
# Target.h is required for key tools
186-
$(WOLFBOOTDIR)/include/target.h: $(WOLFBOOTDIR)/include/target.h.in
187-
@cat $(WOLFBOOTDIR)/include/target.h.in | \
188-
sed -e "s/@WOLFBOOT_PARTITION_SIZE@/$(WOLFBOOT_PARTITION_SIZE)/g" | \
189-
sed -e "s/@WOLFBOOT_SECTOR_SIZE@/$(WOLFBOOT_SECTOR_SIZE)/g" | \
190-
sed -e "s/@WOLFBOOT_PARTITION_BOOT_ADDRESS@/$(WOLFBOOT_PARTITION_BOOT_ADDRESS)/g" | \
191-
sed -e "s/@WOLFBOOT_PARTITION_UPDATE_ADDRESS@/$(WOLFBOOT_PARTITION_UPDATE_ADDRESS)/g" | \
192-
sed -e "s/@WOLFBOOT_PARTITION_SWAP_ADDRESS@/$(WOLFBOOT_PARTITION_SWAP_ADDRESS)/g" | \
193-
sed -e "s/@WOLFBOOT_DTS_BOOT_ADDRESS@/$(WOLFBOOT_DTS_BOOT_ADDRESS)/g" | \
194-
sed -e "s/@WOLFBOOT_DTS_UPDATE_ADDRESS@/$(WOLFBOOT_DTS_UPDATE_ADDRESS)/g" | \
195-
sed -e "s/@WOLFBOOT_LOAD_ADDRESS@/$(WOLFBOOT_LOAD_ADDRESS)/g" | \
196-
sed -e "s/@WOLFBOOT_LOAD_DTS_ADDRESS@/$(WOLFBOOT_LOAD_DTS_ADDRESS)/g" \
197-
> $@
198-
199185
# build objects
200186
$(OBJDIR)/%.o: %.c
201187
$(Q)$(CC) $(CFLAGS) -c -o $@ $<

0 commit comments

Comments
 (0)