Skip to content

Commit bc454d9

Browse files
dgarskedanielinux
authored andcommitted
Peer review fixes
1 parent 00313b3 commit bc454d9

6 files changed

Lines changed: 107 additions & 6 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ wolfboot.bin: wolfboot.elf
336336
$(Q)$(OBJCOPY) $(OBJCOPY_FLAGS) -O binary $^ $@
337337
ifeq ($(TARGET),nxp_lpc54s0xx)
338338
@echo "\t[LPC] enhanced boot block"
339-
$(Q)python3 -c "import struct,os;f=open('$@','r+b');sz=os.path.getsize('$@');f.seek(0x24);f.write(struct.pack('<2I',0xEDDC94BD,0x160));f.seek(0x160);f.write(struct.pack('<25I',0xFEEDA5A5,3,0x10000000,sz-4,0,0,0,0,0,0xEDDC94BD,0,0,0,0x001640EF,0,0,0x1301001D,0,0,0,0x00000100,0,0,0x04030050,0x14110D09));f.seek(0);d=f.read(28);w=struct.unpack('<7I',d);s=sum(w)&0xFFFFFFFF;ck=(0x100000000-s)&0xFFFFFFFF;f.seek(0x1C);f.write(struct.pack('<I',ck));f.close();print('\tvector checksum: 0x%08X'%ck)"
339+
$(Q)python3 tools/scripts/lpc54s0xx_patch_boot_block.py $@
340340
endif
341341
@echo
342342
@echo "\t[SIZE]"

config/examples/nxp_lpc54s0xx.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# Boot: ROM boot loads wolfBoot from external SPIFI QSPI flash at 0x10000000
55
# HAL: Bare-metal (hal/nxp_lpc54s0xx.c) — no NXP MCUXpresso SDK required
66
#
7-
# Flash layout (SPIFI QSPI, 16 MB total):
7+
# Flash layout (SPIFI QSPI, 4 MB total — Winbond W25Q32JV):
88
# 0x10000000 wolfBoot (up to BOOT partition base)
99
# 0x10010000 BOOT partition (960 KB, signed application)
1010
# 0x10100000 UPDATE partition (960 KB)

hal/nxp_lpc54s0xx.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,9 @@ static void RAMFUNCTION spifi_wait_busy(void)
440440
SPIFI_IDATA = 0x00; /* expect BUSY=0 */
441441
SPIFI_CLIMIT = (saved_climit & 0xFFFFFF00) | W25Q_STATUS_BUSY; /* mask bit 0 */
442442

443+
/* Callers (hal_flash_write / hal_flash_erase) always issue a non-MCMD
444+
* command before reaching here, so MCINIT is clear and the reset path in
445+
* spifi_set_cmd() does not run — IDATA/CLIMIT programmed above survive. */
443446
spifi_set_cmd(CMD_READ_STATUS); /* POLL mode command */
444447

445448
/* SPIFI hardware polls flash status internally.

test-app/syscalls.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,15 @@
2222
*/
2323

2424
#include <stdint.h>
25+
#include <stddef.h>
2526
#include <stdarg.h>
2627
#include <sys/stat.h>
2728

29+
/* Forward declaration of vsnprintf. We intentionally do not include <stdio.h>
30+
* because this file redefines stdout/stderr/fputs/fflush with bare-metal
31+
* (void *) stubs that collide with the libc FILE-based prototypes. */
32+
extern int vsnprintf(char *str, size_t size, const char *fmt, va_list ap);
33+
2834
/* Provide our own errno for bare-metal.
2935
* Using the libc errno via <errno.h> can conflict with TLS-based errno
3036
* on cross-toolchains (e.g. powerpc-linux-gnu glibc). */
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#!/usr/bin/env python3
2+
# lpc54s0xx_patch_boot_block.py
3+
#
4+
# Patch a wolfBoot binary for the NXP LPC540xx / LPC54S0xx SPIFI (XIP) boot
5+
# ROM. The ROM expects an "enhanced boot block":
6+
# - offset 0x1C: vector table checksum (negated sum of the first 7 words)
7+
# - offset 0x24: boot block marker + offset to descriptor
8+
# - offset 0x160: 25-word descriptor (magic, mode, image base, image size, ...)
9+
#
10+
# Usage: lpc54s0xx_patch_boot_block.py <wolfboot.bin>
11+
#
12+
# Copyright (C) 2025 wolfSSL Inc.
13+
# This file is part of wolfBoot (GPL-2.0-or-later).
14+
15+
import os
16+
import struct
17+
import sys
18+
19+
HEADER_MARKER_OFFSET = 0x24
20+
BOOT_BLOCK_OFFSET = 0x160
21+
VECTOR_CHECKSUM_OFFSET = 0x1C
22+
IMAGE_BASE_ADDR = 0x10000000 # SPIFI XIP base
23+
24+
HEADER_MARKER_FMT = "<2I" # 0xEDDC94BD, 0x160
25+
BOOT_BLOCK_FMT = "<25I"
26+
VECTOR_TABLE_FMT = "<7I" # first 7 words covered by checksum
27+
28+
29+
def patch(path):
30+
size = os.path.getsize(path)
31+
header_marker_size = struct.calcsize(HEADER_MARKER_FMT)
32+
boot_block_size = struct.calcsize(BOOT_BLOCK_FMT)
33+
vector_table_size = struct.calcsize(VECTOR_TABLE_FMT)
34+
35+
min_size = max(
36+
vector_table_size,
37+
HEADER_MARKER_OFFSET + header_marker_size,
38+
BOOT_BLOCK_OFFSET + boot_block_size,
39+
)
40+
if size < min_size:
41+
raise SystemExit(
42+
"error: %s is too small for LPC54S0xx boot block patching "
43+
"(size=%d, need at least %d bytes)" % (path, size, min_size)
44+
)
45+
46+
with open(path, "r+b") as f:
47+
f.seek(HEADER_MARKER_OFFSET)
48+
f.write(struct.pack(HEADER_MARKER_FMT, 0xEDDC94BD, BOOT_BLOCK_OFFSET))
49+
50+
f.seek(BOOT_BLOCK_OFFSET)
51+
f.write(struct.pack(
52+
BOOT_BLOCK_FMT,
53+
0xFEEDA5A5, # magic
54+
3, # image type
55+
IMAGE_BASE_ADDR, # image base
56+
size - 4, # image size (minus CRC slot)
57+
0, 0, 0, 0, 0,
58+
0xEDDC94BD, # header marker echo
59+
0, 0, 0,
60+
0x001640EF, # SPIFI config
61+
0, 0,
62+
0x1301001D, # clock/flash timing word
63+
0, 0, 0,
64+
0x00000100, # options
65+
0, 0,
66+
0x04030050, # PLL config
67+
0x14110D09, # clock divider config
68+
))
69+
70+
f.seek(0)
71+
words = struct.unpack(VECTOR_TABLE_FMT, f.read(vector_table_size))
72+
checksum = (0x100000000 - (sum(words) & 0xFFFFFFFF)) & 0xFFFFFFFF
73+
f.seek(VECTOR_CHECKSUM_OFFSET)
74+
f.write(struct.pack("<I", checksum))
75+
76+
print("\tvector checksum: 0x%08X" % checksum)
77+
78+
79+
def main(argv):
80+
if len(argv) != 2:
81+
raise SystemExit("usage: %s <wolfboot.bin>" % argv[0])
82+
patch(argv[1])
83+
84+
85+
if __name__ == "__main__":
86+
main(sys.argv)

tools/scripts/nxp-lpc54s0xx-flash.sh

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#
3131

3232
set -e
33+
set -o pipefail
3334

3435
# Configuration (can be overridden via environment variables)
3536
CONFIG_FILE="${CONFIG_FILE:-config/examples/nxp_lpc54s0xx.config}"
@@ -110,10 +111,15 @@ parse_config() {
110111
exit 1
111112
fi
112113

113-
# Helper function to extract config value
114+
# Helper function to extract config value.
115+
# Anchor the regex to `KEY=` or `KEY?=` so e.g. SIGN does not match SIGN_ALG.
116+
# grep with --max-count=1 keeps the pipeline single-stage so pipefail catches
117+
# a truly missing key (exit 1) rather than relying on `head` to mask it.
114118
get_config_value() {
115119
local key="$1"
116-
grep -E "^${key}" "$config_file" | head -1 | sed -E "s/^${key}\??=//" | tr -d '[:space:]'
120+
local line
121+
line=$(grep -E "^${key}\\??=" "$config_file" --max-count=1) || return 0
122+
printf '%s' "${line#*=}" | tr -d '[:space:]'
117123
}
118124

119125
# Extract SIGN and HASH
@@ -146,9 +152,9 @@ parse_config() {
146152

147153
# Ensure partition addresses have 0x prefix for bash arithmetic
148154
for var in WOLFBOOT_PARTITION_BOOT_ADDRESS WOLFBOOT_PARTITION_UPDATE_ADDRESS WOLFBOOT_PARTITION_SIZE WOLFBOOT_SECTOR_SIZE; do
149-
eval "val=\$$var"
155+
local val="${!var}"
150156
if [[ ! "$val" =~ ^0x ]]; then
151-
eval "$var=\"0x\${val}\""
157+
printf -v "$var" '0x%s' "$val"
152158
fi
153159
done
154160

0 commit comments

Comments
 (0)