Skip to content

Commit f5377b3

Browse files
Merge pull request #1526 from JonathonHall-Purism/zip_updates
flash-gui.sh: Extend NPF archive format to ZIP, improve workflow
2 parents 133da0e + 6873df6 commit f5377b3

File tree

8 files changed

+114
-30
lines changed

8 files changed

+114
-30
lines changed

.circleci/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ commands:
1515
command: |
1616
ln -fs /usr/share/zoneinfo/America/New_York /etc/localtime
1717
apt update
18-
apt install -y build-essential zlib1g-dev uuid-dev libdigest-sha-perl libelf-dev bc bzip2 bison flex git gnupg gawk iasl m4 nasm patch python python2 python3 wget gnat cpio ccache pkg-config cmake libusb-1.0-0-dev autoconf texinfo ncurses-dev doxygen graphviz udev libudev1 libudev-dev automake libtool rsync innoextract sudo libssl-dev device-tree-compiler u-boot-tools sharutils e2fsprogs parted curl unzip imagemagick libncurses5-dev
18+
apt install -y build-essential zlib1g-dev uuid-dev libdigest-sha-perl libelf-dev bc bzip2 bison flex git gnupg gawk iasl m4 nasm patch python python2 python3 wget gnat cpio ccache pkg-config cmake libusb-1.0-0-dev autoconf texinfo ncurses-dev doxygen graphviz udev libudev1 libudev-dev automake libtool rsync innoextract sudo libssl-dev device-tree-compiler u-boot-tools sharutils e2fsprogs parted curl unzip imagemagick libncurses5-dev zip
1919
- run:
2020
name: Make Board (FULL ORDERED BUILD LOGS HERE UNTIL JOB FAILED)
2121
command: |

Makefile

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ CB_OUTPUT_BASENAME := $(shell echo $(BRAND_NAME) | tr A-Z a-z)-$(BOARD)-$(HEADS_
1616
CB_OUTPUT_FILE := $(CB_OUTPUT_BASENAME).rom
1717
CB_OUTPUT_FILE_GPG_INJ := $(CB_OUTPUT_BASENAME)-gpg-injected.rom
1818
CB_BOOTBLOCK_FILE := $(CB_OUTPUT_BASENAME).bootblock
19+
CB_UPDATE_PKG_FILE := $(CB_OUTPUT_BASENAME).zip
1920
LB_OUTPUT_FILE := linuxboot-$(BOARD)-$(HEADS_GIT_VERSION).rom
2021

2122
all:
@@ -50,6 +51,12 @@ endif
5051
# By default, we are building for x86, up to a board to change this variable
5152
CONFIG_TARGET_ARCH := x86
5253

54+
# Legacy flash boards have to be handled specifically for some functionality
55+
# (e.g. they don't generate upgrade packages, lack bash, etc.) Use this to
56+
# guard behavior that is specific to legacy flash boards only. Don't use it for
57+
# behavior that might be needed for other boards, use specific configs instead.
58+
CONFIG_LEGACY_FLASH := n
59+
5360
include $(CONFIG)
5461

5562
# Unless otherwise specified, we are building for heads
@@ -159,7 +166,27 @@ payload: $(build)/$(BOARD)/bzImage $(build)/$(initrd_dir)/initrd.cpio.xz
159166

160167
ifeq ($(CONFIG_COREBOOT), y)
161168

162-
all: $(board_build)/$(CB_OUTPUT_FILE)
169+
# Legacy flash boards don't generate an update package, the only purpose of
170+
# those boards is to be flashed over vendor firmware via an exploit.
171+
ifneq ($(CONFIG_LEGACY_FLASH), y)
172+
# talos-2 builds its own update package, which is not integrated with the ZIP
173+
# method currently
174+
ifneq ($(BOARD), talos-2)
175+
# Coreboot targets create an update package that can be applied with integrity
176+
# verification before flashing (see flash-gui.sh). The ZIP package format
177+
# allows other metadata that might be needed to added in the future without
178+
# breaking backward compatibility.
179+
$(board_build)/$(CB_UPDATE_PKG_FILE): $(board_build)/$(CB_OUTPUT_FILE)
180+
rm -rf "$(board_build)/update_pkg"
181+
mkdir -p "$(board_build)/update_pkg"
182+
cp "$<" "$(board_build)/update_pkg/"
183+
cd "$(board_build)/update_pkg" && sha256sum "$(CB_OUTPUT_FILE)" >sha256sum.txt
184+
cd "$(board_build)/update_pkg" && zip -9 "$@" "$(CB_OUTPUT_FILE)" sha256sum.txt
185+
186+
all: $(board_build)/$(CB_OUTPUT_FILE) $(board_build)/$(CB_UPDATE_PKG_FILE)
187+
endif
188+
endif
189+
163190
ifneq ($(CONFIG_COREBOOT_BOOTBLOCK),)
164191
all: $(board_build)/$(CB_BOOTBLOCK_FILE)
165192
endif

boards/UNTESTED_t430-legacy-flash/UNTESTED_t430-legacy-flash.config

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ export CONFIG_BOOTSCRIPT=/bin/xx30-flash.init
3030
export CONFIG_BOARD_NAME="ThinkPad T430-legacy-flash"
3131
export CONFIG_FLASHROM_OPTIONS="--force --noverify-all -p internal --ifd --image bios"
3232

33+
CONFIG_LEGACY_FLASH=y
34+
3335
# This board is "special" in that we need a 4MB top SPI flashable ROM.
3436
# This is enough to allow the board to boot into a minimal Heads and read the full Legacy
3537
# ROM from an external USB media.

boards/talos-2/talos-2.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,6 @@ $(board_build)/$(OUTPUT_PREFIX).tgz: \
6464
rm -rf $(board_build)/pkg # cleanup in case directory exists
6565
mkdir $(board_build)/pkg
6666
cp $^ $(board_build)/pkg
67-
cd $(board_build)/pkg && sha256sum * > hashes.txt
67+
cd $(board_build)/pkg && sha256sum * > sha256sum.txt
6868
cd $(board_build)/pkg && tar zcf $@ *
6969
rm -r $(board_build)/pkg

boards/x230-legacy-flash/x230-legacy-flash.config

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ export CONFIG_BOOTSCRIPT=/bin/xx30-flash.init
3131
export CONFIG_BOARD_NAME="ThinkPad X230-legacy-flash"
3232
export CONFIG_FLASHROM_OPTIONS="--force --noverify-all -p internal --ifd --image bios"
3333

34+
CONFIG_LEGACY_FLASH=y
35+
3436
# This board is "special" in that we need a 4MB top SPI flashable ROM.
3537
# This is enough to allow the board to boot into a minimal Heads and read the full Legacy
3638
# ROM from an external USB media.

create-npf.sh

Lines changed: 0 additions & 8 deletions
This file was deleted.

initrd/bin/flash-gui.sh

Lines changed: 79 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,33 @@ if [ "$CONFIG_RESTRICTED_BOOT" = y ]; then
1313
exit 1
1414
fi
1515

16+
# Most boards use a .rom file as a "plain" update, contents of the BIOS flash
17+
UPDATE_PLAIN_EXT=rom
18+
# talos-2 uses a .tgz file for its "plain" update, contains other parts as well
19+
# as its own integrity check. This isn't integrated with the "update package"
20+
# workflow (as-is, a .tgz could be inside that package in theory) but more work
21+
# would be needed to properly integrate it.
22+
if [ "${CONFIG_BOARD%_*}" = talos-2 ]; then
23+
UPDATE_PLAIN_EXT=tgz
24+
fi
25+
26+
# Check that a glob matches exactly one thing. If so, echoes the single value.
27+
# Otherwise, fails. As always, do not quote the glob.
28+
#
29+
# E.g, locate a ROM with unknown version when only one should be present:
30+
# if ROM_FILE="$(single_glob /media/heads-*.rom)"; then
31+
# echo "ROM is $ROM_FILE"
32+
# else
33+
# echo "Failed to find a ROM" >&2
34+
# fi
35+
single_glob() {
36+
if [ "$#" -eq 1 ] && [ -f "$1" ]; then
37+
echo "$1"
38+
else
39+
return 1
40+
fi
41+
}
42+
1643
while true; do
1744
unset menu_choice
1845
whiptail $BG_COLOR_MAIN_MENU --title "Firmware Management Menu" \
@@ -30,38 +57,72 @@ while true; do
3057
;;
3158
f | c)
3259
if (whiptail $BG_COLOR_WARNING --title 'Flash the BIOS with a new ROM' \
33-
--yesno "You will need to insert a USB drive containing your BIOS image (*.rom, *.npf or *.tgz).\n\nAfter you select this file, this program will reflash your BIOS.\n\nDo you want to proceed?" 0 80); then
60+
--yesno "You will need to insert a USB drive containing your BIOS image (*.zip or\n*.$UPDATE_PLAIN_EXT).\n\nAfter you select this file, this program will reflash your BIOS.\n\nDo you want to proceed?" 0 80); then
3461
mount_usb
3562
if grep -q /media /proc/mounts; then
36-
find /media ! -path '*/\.*' -type f \( -name '*.rom' -o -name '*.tgz' -o -type f -name '*.npf' \) | sort >/tmp/filelist.txt
63+
if [ "${CONFIG_BOARD%_*}" = talos-2 ]; then
64+
find /media ! -path '*/\.*' -type f -name "*.$UPDATE_PLAIN_EXT" | sort >/tmp/filelist.txt
65+
else
66+
find /media ! -path '*/\.*' -type f \( -name "*.$UPDATE_PLAIN_EXT" -o -type f -name "*.zip" \) | sort >/tmp/filelist.txt
67+
fi
3768
file_selector "/tmp/filelist.txt" "Choose the ROM to flash"
3869
if [ "$FILE" == "" ]; then
3970
exit 1
4071
else
41-
ROM=$FILE
72+
PKG_FILE=$FILE
4273
fi
4374

44-
# is a .npf provided?
45-
if [ -z "${ROM##*.npf}" ]; then
46-
#preventive cleanup
47-
rm -rf /tmp/verified_rom >/dev/null 2>&1 || true
48-
# unzip to /tmp/verified_rom
49-
mkdir -p /tmp/verified_rom >/dev/null 2>&1 || true
50-
unzip $ROM -d /tmp/verified_rom || die "Failed to unzip ROM file"
75+
# is an update package provided?
76+
if [ -z "${PKG_FILE##*.zip}" ]; then
77+
# Unzip the package
78+
PKG_EXTRACT="/tmp/flash_gui/update_package"
79+
rm -rf "$PKG_EXTRACT"
80+
mkdir -p "$PKG_EXTRACT"
81+
# If extraction fails, delete everything and fall through to the
82+
# integrity failure prompt. This is the most likely path if the ROM
83+
# was actually corrupted in transit. Corrupting the ZIP in a way that
84+
# still extracts is possible (the sha256sum detects this) but less
85+
# likely.
86+
unzip "$PKG_FILE" -d "$PKG_EXTRACT" || rm -rf "$PKG_EXTRACT"
87+
# Older packages had /tmp/verified_rom hard-coded in the sha256sum.txt
88+
# Remove that so it's a relative path to the ROM in the package.
89+
# Ignore failure, if there is no sha256sum.txt the sha256sum will fail
90+
sed -i -e 's| /tmp/verified_rom/\+| |g' "$PKG_EXTRACT/sha256sum.txt" || true
5191
# check file integrity
52-
if (cd /tmp/verified_rom/ && sha256sum -cs /tmp/verified_rom/sha256sum.txt); then
53-
ROM="$(head -n1 /tmp/verified_rom/sha256sum.txt | cut -d ' ' -f 3)"
54-
else
92+
if ! (cd "$PKG_EXTRACT" && sha256sum -cs sha256sum.txt); then
5593
whiptail --title 'ROM Integrity Check Failed! ' \
56-
--msgbox "$ROM integrity check failed. Did not flash.\n\nPlease check your file (e.g. re-download).\n" 16 60
57-
exit
94+
--msgbox "Integrity check failed in\n$PKG_FILE.\nDid not flash.\n\nPlease check your file (e.g. re-download).\n" 16 60
95+
exit 1
5896
fi
97+
98+
# The package must contain exactly one *.rom file, flash that.
99+
if ! PACKAGE_ROM="$(single_glob "$PKG_EXTRACT/"*."$UPDATE_PLAIN_EXT")"; then
100+
whiptail --title 'BIOS Image Not Found! ' \
101+
--msgbox "A BIOS image was not found in\n$PKG_FILE.\n\nPlease check your file (e.g. re-download).\n" 16 60
102+
exit 1
103+
fi
104+
105+
if ! whiptail $BG_COLOR_WARNING --title 'Flash ROM?' \
106+
--yesno "This will replace your current ROM with:\n\n${PKG_FILE#"/media/"}\n\nDo you want to proceed?" 0 80; then
107+
exit 1
108+
fi
109+
110+
# Continue on using the verified ROM
111+
ROM="$PACKAGE_ROM"
59112
else
113+
# talos-2 uses a .tgz file for its "plain" update, contains other parts as well, validated against hashes under flash.sh
114+
# Skip prompt for hash validation for talos-2. Only method is through tgz or through bmc with individual parts
115+
if [ "${CONFIG_BOARD%_*}" != talos-2 ]; then
60116
# a rom file was provided. exit if we shall not proceed
117+
ROM="$PKG_FILE"
61118
ROM_HASH=$(sha256sum "$ROM" | awk '{print $1}') || die "Failed to hash ROM file"
62119
if ! (whiptail $CONFIG_ERROR_BG_COLOR --title 'Flash ROM without integrity check?' \
63-
--yesno "You have provided a *.rom file. The integrity of the file can not be\nchecked automatically for this file type.\n\nROM: $ROM\nSHA256SUM: $ROM_HASH\n\nIf you do not know how to check the file integrity yourself,\nyou should use a *.npf file instead.\n\nIf the file is damaged, you will not be able to boot anymore.\nDo you want to proceed flashing without file integrity check?" 0 80); then
64-
exit
120+
--yesno "You have provided a *.$UPDATE_PLAIN_EXT file. The integrity of the file can not be\nchecked automatically for this file type.\n\nROM: $ROM\nSHA256SUM: $ROM_HASH\n\nIf you do not know how to check the file integrity yourself,\nyou should use a *.zip file instead.\n\nIf the file is damaged, you will not be able to boot anymore.\nDo you want to proceed flashing without file integrity check?" 0 80); then
121+
exit 1
122+
fi
123+
else
124+
#We are on talos-2, so we have a tgz file. We will pass it directly to flash.sh which will take care of it
125+
ROM="$PKG_FILE"
65126
fi
66127
fi
67128

@@ -79,7 +140,7 @@ while true; do
79140
/bin/flash.sh "$ROM"
80141
fi
81142
whiptail --title 'ROM Flashed Successfully' \
82-
--msgbox "${ROM#"/media/"}\n\nhas been flashed successfully.\n\nPress Enter to reboot\n" 0 80
143+
--msgbox "${PKG_FILE#"/media/"}\n\nhas been flashed successfully.\n\nPress Enter to reboot\n" 0 80
83144
umount /media
84145
/bin/reboot
85146
fi

initrd/bin/flash.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ if [ "$READ" -eq 0 ] && [ "${ROM##*.}" = tgz ]; then
180180
mkdir /tmp/verified_rom
181181

182182
tar -C /tmp/verified_rom -xf $ROM || die "Rom archive $ROM could not be extracted"
183-
if ! (cd /tmp/verified_rom/ && sha256sum -cs hashes.txt); then
183+
if ! (cd /tmp/verified_rom/ && sha256sum -cs sha256sum.txt); then
184184
die "Provided tgz image did not pass hash verification"
185185
fi
186186

0 commit comments

Comments
 (0)