[PATCH v2 3/4] image-commands: support Chromium OS image-type creation

Brian Norris computersforpeace at gmail.com
Sat Jan 16 22:07:05 EST 2021


See the previous patches, which implemented the cros-vbutil
verified-boot payload-packing tool, and extended ptgen for the CrOS
kernel partition type. With these, it's now possible to package kernel +
rootfs to make disk images that can boot a Chrome OS-based system (e.g.,
Chromebooks, or even a few AP models).

gen_image_vboot.sh borrows a bit of structure from gen_image_generic.sh,
but I didn't feel it fit well to try and add new flags to the latter,
given the difference in its FAT kernel packaging and our raw kernel
partition packing.

Signed-off-by: Brian Norris <computersforpeace at gmail.com>
---
 include/image-commands.mk                     | 18 ++++++++++
 .../base-files/files/lib/upgrade/common.sh    |  4 ++-
 scripts/gen_image_vboot.sh                    | 36 +++++++++++++++++++
 3 files changed, 57 insertions(+), 1 deletion(-)
 create mode 100755 scripts/gen_image_vboot.sh

diff --git a/include/image-commands.mk b/include/image-commands.mk
index 979eafb15734..f02d8e79fce6 100644
--- a/include/image-commands.mk
+++ b/include/image-commands.mk
@@ -172,6 +172,24 @@ define Build/fit
 	@mv $@.new $@
 endef
 
+define Build/cros-image
+	$(SCRIPT_DIR)/gen_image_vboot.sh \
+		  $@ \
+		  $(CONFIG_TARGET_KERNEL_PARTSIZE) \
+		  $(CONFIG_TARGET_KERNEL_PARTSIZE) $(IMAGE_KERNEL) \
+		  $(CONFIG_TARGET_ROOTFS_PARTSIZE) $(IMAGE_ROOTFS)
+endef
+
+# NB: Chrome OS bootloaders replace the '%U' in command lines with the UUID of
+# the kernel partition it chooses to boot from. This gives a flexible way to
+# consistently build and sign kernels that always use the subsequent
+# (PARTNROFF=1) partition as their rootfs.
+define Build/cros-vboot
+	$(STAGING_DIR_HOST)/bin/cros-vbutil \
+		-k $@ -c "root=PARTUUID=%U/PARTNROFF=1" -o $@.new
+	@mv $@.new $@
+endef
+
 define Build/gzip
 	gzip -f -9n -c $@ $(1) > $@.new
 	@mv $@.new $@
diff --git a/package/base-files/files/lib/upgrade/common.sh b/package/base-files/files/lib/upgrade/common.sh
index c28bae48a15c..a2ee8d1675a6 100644
--- a/package/base-files/files/lib/upgrade/common.sh
+++ b/package/base-files/files/lib/upgrade/common.sh
@@ -178,9 +178,11 @@ export_bootdevice() {
 					fi
 				done
 			;;
+			PARTUUID=????????-????-????-????-??????????01/PARTNROFF=1 | \
 			PARTUUID=????????-????-????-????-??????????02)
 				uuid="${rootpart#PARTUUID=}"
-				uuid="${uuid%02}00"
+				uuid="${uuid%/PARTNROFF=1}"
+				uuid="${uuid%0?}00"
 				for disk in $(find /dev -type b); do
 					set -- $(dd if=$disk bs=1 skip=568 count=16 2>/dev/null | hexdump -v -e '8/1 "%02x "" "2/1 "%02x""-"6/1 "%02x"')
 					if [ "$4$3$2$1-$6$5-$8$7-$9" = "$uuid" ]; then
diff --git a/scripts/gen_image_vboot.sh b/scripts/gen_image_vboot.sh
new file mode 100755
index 000000000000..ae267ba3fbb9
--- /dev/null
+++ b/scripts/gen_image_vboot.sh
@@ -0,0 +1,36 @@
+#!/usr/bin/env bash
+# Copyright (C) 2021 OpenWrt.org
+set -e -x
+[ $# == 6 ] || {
+    echo "SYNTAX: $0 <file> <bootpart size> <kernel size> <kernel image> <rootfs size> <rootfs image>"
+    exit 1
+}
+
+OUTPUT="$1"
+BOOTPARTSIZE="$2"
+KERNELSIZE="$3"
+KERNELIMAGE="$4"
+ROOTFSSIZE="$5"
+ROOTFSIMAGE="$6"
+
+rm -f "${OUTPUT}"
+
+head=16
+sect=63
+
+# create partition table
+set $(ptgen -o "${OUTPUT}" -h $head -s $sect -g -p ${BOOTPARTSIZE}m -T cros_kernel -p ${KERNELSIZE}m -p ${ROOTFSSIZE}m)
+
+BOOTOFFSET="$(($1 / 512))"
+BOOTSIZE="$2"
+KERNELOFFSET="$(($3 / 512))"
+KERNELSIZE="$4"
+ROOTFSOFFSET="$(($5 / 512))"
+ROOTFSSIZE="$(($6 / 512))"
+
+mkfs.fat -n boot -C "${OUTPUT}.boot" -S 512 "$((BOOTSIZE / 1024))"
+
+dd if="${OUTPUT}.boot" of="${OUTPUT}" bs=512 seek="${BOOTOFFSET}" conv=notrunc
+rm -f "${OUTPUT}.boot"
+dd if="${KERNELIMAGE}" of="${OUTPUT}" bs=512 seek="${KERNELOFFSET}" conv=notrunc
+dd if="${ROOTFSIMAGE}" of="${OUTPUT}" bs=512 seek="${ROOTFSOFFSET}" conv=notrunc
-- 
2.29.2




More information about the openwrt-devel mailing list