Skip to content

Commit 501897e

Browse files
dgarskedanielinux
authored andcommitted
zcu102: refuse non-removable/oversized SD targets in demo scripts
1 parent 4ad44bd commit 501897e

3 files changed

Lines changed: 81 additions & 2 deletions

File tree

src/port/amd/boards/zcu102/wolfboot-demo/README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ SD=/dev/sdX ./program-sd.sh
5151

5252
This copies `BOOT.BIN` into the FAT boot partition and `dd`s the signed v1 app to the raw `OFP_A` partition (needs root). Add `WIPE_OFP_B=1` to also clear `OFP_B` so the board boots `A:v1` fresh (handy for demoing the update). Then put the card in the ZCU102, set boot-mode `SW6 = SD`, and power on.
5353

54+
Both scripts refuse a `SD=` that is not a whole disk, is not removable/hotplug (your system drive), or is bigger than 256 GB. Override with `FORCE=1` for an unusual card reader, or raise the size cap with `MAX_GB=`.
55+
5456
## Run
5557

5658
On the serial console (PS-UART0, 115200 8N1) you should see FSBL -> wolfBoot (which verifies the signature) -> the wolfIP banner, DHCP bind, and `Ready`. A modified or unsigned `OFP_A` image fails wolfBoot's check and is not booted.
@@ -110,8 +112,8 @@ The reset after "update staged" is intentional - the app reboots so wolfBoot re-
110112
| File | Purpose |
111113
|------|---------|
112114
| `build.sh` | Build wolfBoot + sign the app (v1 + v2) + assemble `BOOT.BIN` |
113-
| `program-sd.sh` | Write `BOOT.BIN` + signed app to an SD card (`WIPE_OFP_B=1` for a clean slate) |
114-
| `partition-sd.sh` | Create the demo MBR layout on a blank card |
115+
| `program-sd.sh` | Write `BOOT.BIN` + signed app to an SD card (`WIPE_OFP_B=1` for a clean slate, `FORCE=1` to skip the target checks) |
116+
| `partition-sd.sh` | Create the demo MBR layout on a blank card (`FORCE=1` to skip the target checks) |
115117
| `update.sh` | Stage the update image + trigger it over the network |
116118
| `boot.bif.in` | bootgen template (FSBL/PMUFW/BL31/wolfBoot) |
117119
| `out/` | Build output |

src/port/amd/boards/zcu102/wolfboot-demo/partition-sd.sh

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,53 @@
99
# p4 rootfs rest (unused by this bare-metal demo)
1010
#
1111
# DESTRUCTIVE: erases the entire target disk. Double-check with lsblk!
12+
# It refuses a partition, a non-removable disk, or one bigger than MAX_GB.
1213
#
1314
# Usage: SD=/dev/sdX ./partition-sd.sh
15+
# FORCE=1 SD=... ./partition-sd.sh skip the removable/size checks
16+
# (unusual card reader)
17+
# MAX_GB=N SD=... ./partition-sd.sh raise the size cap (default 256)
1418
#
1519
set -euo pipefail
1620

1721
SD="${SD:?set SD=/dev/sdX (your card reader - NOT a board, NOT your system disk!)}"
1822
[ -b "$SD" ] || { echo "$SD is not a block device" >&2; exit 1; }
1923

24+
# Reject obviously-wrong targets before anything destructive happens: a
25+
# partition instead of a whole disk, a fixed (non-removable) drive such as your
26+
# system disk, or a device far larger than any demo SD card. FORCE=1 overrides
27+
# the removable and size checks; MAX_GB= raises the size cap.
28+
check_target_disk() {
29+
local devtype removable hotplug bytes maxbytes
30+
31+
devtype="$(lsblk -ndo TYPE "$SD" | tr -d '[:space:]')"
32+
if [ "$devtype" != "disk" ]; then
33+
echo "ERROR: $SD is a '$devtype', not a whole disk - pass the card" >&2
34+
echo " reader itself (/dev/sdX), not a partition (/dev/sdX1)." >&2
35+
exit 1
36+
fi
37+
38+
# lsblk pads a column to its (suppressed) header width, hence the tr. A
39+
# reader that reports RM=0 but HOTPLUG=1 (PCIe/rtsx, some USB bridges) is
40+
# still fine.
41+
removable="$(lsblk -ndo RM "$SD" | tr -d '[:space:]')"
42+
hotplug="$(lsblk -ndo HOTPLUG "$SD" | tr -d '[:space:]')"
43+
if [ "$removable" != 1 ] && [ "$hotplug" != 1 ] && [ "${FORCE:-0}" != 1 ]; then
44+
echo "ERROR: $SD is not removable or hotplug (RM=$removable," >&2
45+
echo " HOTPLUG=$hotplug) - refusing. Set FORCE=1 to override." >&2
46+
exit 1
47+
fi
48+
49+
bytes="$(lsblk -ndbo SIZE "$SD" | tr -d '[:space:]')"
50+
maxbytes=$(( ${MAX_GB:-256} * 1024 * 1024 * 1024 ))
51+
if [ "$bytes" -gt "$maxbytes" ] && [ "${FORCE:-0}" != 1 ]; then
52+
echo "ERROR: $SD is $((bytes / 1024 / 1024 / 1024))GB, over the" >&2
53+
echo " ${MAX_GB:-256}GB sanity limit - refusing. Set MAX_GB= or FORCE=1." >&2
54+
exit 1
55+
fi
56+
}
57+
check_target_disk
58+
2059
# Partition node suffix: /dev/sdX -> sdX1 ; /dev/mmcblkN|nvmeN|loopN -> ...p1
2160
case "$SD" in
2261
*[0-9]) P="p" ;;

src/port/amd/boards/zcu102/wolfboot-demo/program-sd.sh

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
# so the signed image is dd'd to the start of p2 (this needs root). Copying
1414
# BOOT.BIN into the FAT p1 does not.
1515
#
16+
# The script refuses a partition, a non-removable disk, or one bigger than
17+
# MAX_GB - set FORCE=1 to override those checks.
18+
#
1619
# Usage: SD=/dev/sdX ./program-sd.sh (X = your card reader, NOT a board)
1720
# WIPE_OFP_B=1 SD=/dev/sdX ./program-sd.sh also zero OFP_B so the board
1821
# boots A:v1 fresh (for a clean A->B update demo)
@@ -28,6 +31,41 @@ SD="${SD:?set SD=/dev/sdX (your SD card reader block device - double-check with
2831
[ -f "$SIGNED" ] || { echo "missing $SIGNED - run ./build.sh first" >&2; exit 1; }
2932
[ -b "$SD" ] || { echo "$SD is not a block device" >&2; exit 1; }
3033

34+
# Reject obviously-wrong targets before anything destructive happens: a
35+
# partition instead of a whole disk, a fixed (non-removable) drive such as your
36+
# system disk, or a device far larger than any demo SD card. FORCE=1 overrides
37+
# the removable and size checks; MAX_GB= raises the size cap.
38+
check_target_disk() {
39+
local devtype removable hotplug bytes maxbytes
40+
41+
devtype="$(lsblk -ndo TYPE "$SD" | tr -d '[:space:]')"
42+
if [ "$devtype" != "disk" ]; then
43+
echo "ERROR: $SD is a '$devtype', not a whole disk - pass the card" >&2
44+
echo " reader itself (/dev/sdX), not a partition (/dev/sdX1)." >&2
45+
exit 1
46+
fi
47+
48+
# lsblk pads a column to its (suppressed) header width, hence the tr. A
49+
# reader that reports RM=0 but HOTPLUG=1 (PCIe/rtsx, some USB bridges) is
50+
# still fine.
51+
removable="$(lsblk -ndo RM "$SD" | tr -d '[:space:]')"
52+
hotplug="$(lsblk -ndo HOTPLUG "$SD" | tr -d '[:space:]')"
53+
if [ "$removable" != 1 ] && [ "$hotplug" != 1 ] && [ "${FORCE:-0}" != 1 ]; then
54+
echo "ERROR: $SD is not removable or hotplug (RM=$removable," >&2
55+
echo " HOTPLUG=$hotplug) - refusing. Set FORCE=1 to override." >&2
56+
exit 1
57+
fi
58+
59+
bytes="$(lsblk -ndbo SIZE "$SD" | tr -d '[:space:]')"
60+
maxbytes=$(( ${MAX_GB:-256} * 1024 * 1024 * 1024 ))
61+
if [ "$bytes" -gt "$maxbytes" ] && [ "${FORCE:-0}" != 1 ]; then
62+
echo "ERROR: $SD is $((bytes / 1024 / 1024 / 1024))GB, over the" >&2
63+
echo " ${MAX_GB:-256}GB sanity limit - refusing. Set MAX_GB= or FORCE=1." >&2
64+
exit 1
65+
fi
66+
}
67+
check_target_disk
68+
3169
# Partition node suffix: /dev/sdX -> sdX1 ; /dev/mmcblkN|nvmeN|loopN -> ...p1
3270
case "$SD" in
3371
*[0-9]) P="p" ;;

0 commit comments

Comments
 (0)