armvirt: add EFI support
authorMathew McBride <matt@traverse.com.au>
Wed, 19 Jan 2022 02:25:23 +0000 (02:25 +0000)
committerPetr Štetiar <ynezz@true.cz>
Fri, 2 Jun 2023 09:36:05 +0000 (11:36 +0200)
EFI booting is used on newer machines compatible with the
Arm SystemReady specifications.

This commit restructures armvirt into a more 'generic'
target similar to x86.

See https://github.com/openwrt/openwrt/pull/4956
for a history of this port.

Signed-off-by: Mathew McBride <matt@traverse.com.au>
target/linux/armvirt/32/config-6.1
target/linux/armvirt/32/target.mk
target/linux/armvirt/64/target.mk
target/linux/armvirt/Makefile
target/linux/armvirt/base-files/etc/board.d/01_led [new file with mode: 0644]
target/linux/armvirt/base-files/etc/board.d/02_network [new file with mode: 0644]
target/linux/armvirt/base-files/etc/board.d/03_gpio_switches [new file with mode: 0644]
target/linux/armvirt/base-files/lib/preinit/01_sysinfo_acpi [new file with mode: 0644]
target/linux/armvirt/base-files/lib/upgrade/platform.sh [new file with mode: 0644]
target/linux/armvirt/image/Makefile
target/linux/armvirt/image/grub-efi.cfg [new file with mode: 0644]

index 931607aade71a8599a39da1639d0167bfae24fad..664ef2e05b201cccae451e98214479762118593a 100644 (file)
@@ -4,6 +4,7 @@ CONFIG_ARCH_MIGHT_HAVE_PC_PARPORT=y
 CONFIG_ARCH_MULTIPLATFORM=y
 CONFIG_ARCH_MULTI_V6_V7=y
 CONFIG_ARCH_MULTI_V7=y
+CONFIG_ARCH_MMAP_RND_BITS=8
 CONFIG_ARCH_NR_GPIO=0
 CONFIG_ARCH_OPTIONAL_KERNEL_RWX=y
 CONFIG_ARCH_OPTIONAL_KERNEL_RWX_DEFAULT=y
@@ -13,6 +14,7 @@ CONFIG_ARM=y
 CONFIG_ARM_CPU_SUSPEND=y
 CONFIG_ARM_HAS_SG_CHAIN=y
 CONFIG_ARM_HEAVY_MB=y
+# CONFIG_ARM_HIGHBANK_CPUIDLE is not set
 CONFIG_ARM_L1_CACHE_SHIFT=6
 CONFIG_ARM_L1_CACHE_SHIFT_6=y
 CONFIG_ARM_LPAE=y
index 8d42a14b7cf57c5c80db110d344e598aa5567862..df220402411cfdbe54c6ed72c7e2e6fcca4361a9 100644 (file)
@@ -1,6 +1,6 @@
 ARCH:=arm
 SUBTARGET:=32
-BOARDNAME:=QEMU ARM Virtual Machine (cortex-a15)
+BOARDNAME:=32-bit ARM QEMU Virtual Machine
 CPU_TYPE:=cortex-a15
 CPU_SUBTYPE:=neon-vfpv4
 KERNELNAME:=zImage
index 58adcc7d60238938b10c52d2b786df1e8a97b6d6..ac5a60d848b858b7f9e56660e0d936b19918ebe7 100644 (file)
@@ -1,8 +1,6 @@
 ARCH:=aarch64
 SUBTARGET:=64
-BOARDNAME:=QEMU ARMv8 Virtual Machine (cortex-a53)
-CPU_TYPE:=cortex-a53
-KERNELNAME:=Image
+BOARDNAME:=64-bit ARM machines
 
 define Target/Description
   Build multi-platform images for the ARMv8 instruction set architecture
index 3acf84e5c978aa4fca72eb533d6f8b2e771e1b6a..36c91a077fa147f786b1bd072e97321db6c4e6b5 100644 (file)
@@ -6,7 +6,7 @@ include $(TOPDIR)/rules.mk
 
 BOARD:=armvirt
 BOARDNAME:=QEMU ARM Virtual Machine
-FEATURES:=fpu pci rtc usb
+FEATURES:=fpu pci pcie rtc usb boot-part rootfs-part
 FEATURES+=cpiogz ext4 ramdisk squashfs targz
 
 KERNEL_PATCHVER:=5.15
@@ -15,5 +15,8 @@ KERNEL_TESTING_PATCHVER:=6.1
 include $(INCLUDE_DIR)/target.mk
 
 DEFAULT_PACKAGES += mkf2fs e2fsprogs
+# blkid used for resolving PARTUUID
+# in sysupgrade
+DEFAULT_PACKAGES += blkid
 
 $(eval $(call BuildTarget))
diff --git a/target/linux/armvirt/base-files/etc/board.d/01_led b/target/linux/armvirt/base-files/etc/board.d/01_led
new file mode 100644 (file)
index 0000000..0250a96
--- /dev/null
@@ -0,0 +1,19 @@
+#!/bin/sh
+# SPDX-License-Identifier: GPL-2.0-or-later
+
+. /lib/functions/uci-defaults.sh
+
+board_config_update
+
+board=$(board_name)
+
+case "$board" in
+traverse,ten64)
+       ucidef_set_led_netdev "sfp1" "SFP 1" "ten64:green:sfp1:down" "eth8" "link tx rx"
+       ucidef_set_led_netdev "sfp2" "SFP 2" "ten64:green:sfp2:up" "eth9" "link tx rx"
+       ;;
+esac
+
+board_config_flush
+
+exit 0
diff --git a/target/linux/armvirt/base-files/etc/board.d/02_network b/target/linux/armvirt/base-files/etc/board.d/02_network
new file mode 100644 (file)
index 0000000..f58de1c
--- /dev/null
@@ -0,0 +1,18 @@
+#!/bin/sh
+# SPDX-License-Identifier: GPL-2.0-or-later
+
+. /lib/functions/system.sh
+. /lib/functions/uci-defaults.sh
+
+board_config_update
+
+case "$(board_name)" in
+       traverse,ten64)
+               ucidef_set_interface_lan "eth0 eth1 eth2 eth3"
+               ucidef_set_interface_wan "eth6"
+               ;;
+esac
+
+board_config_flush
+
+exit 0
diff --git a/target/linux/armvirt/base-files/etc/board.d/03_gpio_switches b/target/linux/armvirt/base-files/etc/board.d/03_gpio_switches
new file mode 100644 (file)
index 0000000..cf07bc0
--- /dev/null
@@ -0,0 +1,23 @@
+#!/bin/sh
+# SPDX-License-Identifier: GPL-2.0-or-later
+
+. /lib/functions/uci-defaults.sh
+
+board_config_update
+
+board=$(board_name)
+
+case "$board" in
+traverse,ten64)
+       ucidef_add_gpio_switch "lte_reset" "Cell Modem Reset" "376"
+       ucidef_add_gpio_switch "lte_power" "Cell Modem Power" "377"
+       ucidef_add_gpio_switch "lte_disable" "Cell Modem Airplane mode" "378"
+       ucidef_add_gpio_switch "gnss_disable" "Cell Modem Disable GNSS receiver" "379"
+       ucidef_add_gpio_switch "lower_sfp_txidsable" "Lower SFP+ TX Disable" "369"
+       ucidef_add_gpio_switch "upper_sfp_txdisable" "Upper SFP+ TX Disable" "373"
+       ;;
+esac
+
+board_config_flush
+
+exit 0
diff --git a/target/linux/armvirt/base-files/lib/preinit/01_sysinfo_acpi b/target/linux/armvirt/base-files/lib/preinit/01_sysinfo_acpi
new file mode 100644 (file)
index 0000000..1069d74
--- /dev/null
@@ -0,0 +1,52 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+
+sanitize_name_arm64() {
+       sed -e '
+               y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/;
+               s/[^a-z0-9_-]\+/-/g;
+               s/^-//;
+               s/-$//;
+       ' "$@"
+}
+
+do_sysinfo_arm64() {
+       local vendor product file
+
+       for file in sys_vendor board_vendor; do
+               vendor="$(cat /sys/devices/virtual/dmi/id/$file 2>/dev/null)"
+               case "$vendor" in
+               empty | \
+               System\ manufacturer | \
+               To\ [bB]e\ [fF]illed\ [bB]y\ O\.E\.M\.)
+                       continue
+                       ;;
+               esac
+               [ -n "$vendor" ] && break
+       done
+
+       for file in product_name board_name; do
+               product="$(cat /sys/devices/virtual/dmi/id/$file 2>/dev/null)"
+               case "$vendor:$product" in
+               ?*:empty | \
+               ?*:System\ Product\ Name | \
+               ?*:To\ [bB]e\ [fF]illed\ [bB]y\ O\.E\.M\.)
+                       continue
+                       ;;
+               ?*:?*)
+                       break
+                       ;;
+               esac
+       done
+
+       [ -d "/sys/firmware/devicetree/base" ] && return
+
+       [ -n "$vendor" -a -n "$product" ] || return
+
+       mkdir -p /tmp/sysinfo
+
+       echo "$vendor $product" > /tmp/sysinfo/model
+
+       sanitize_name_arm64 /tmp/sysinfo/model > /tmp/sysinfo/board_name
+}
+
+boot_hook_add preinit_main do_sysinfo_arm64
diff --git a/target/linux/armvirt/base-files/lib/upgrade/platform.sh b/target/linux/armvirt/base-files/lib/upgrade/platform.sh
new file mode 100644 (file)
index 0000000..8263b9c
--- /dev/null
@@ -0,0 +1,164 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+
+RAMFS_COPY_BIN="/usr/sbin/blkid"
+
+platform_check_image() {
+       local board=$(board_name)
+       local diskdev partdev diff
+       [ "$#" -gt 1 ] && return 1
+
+       v "Board is ${board}"
+
+       export_bootdevice && export_partdevice diskdev 0 || {
+               v "platform_check_image: Unable to determine upgrade device"
+               return 1
+       }
+
+       get_partitions "/dev/$diskdev" bootdisk
+
+       v "Extract boot sector from the image"
+       get_image_dd "$1" of=/tmp/image.bs count=63 bs=512b
+
+       get_partitions /tmp/image.bs image
+
+       #compare tables
+       diff="$(grep -F -x -v -f /tmp/partmap.bootdisk /tmp/partmap.image)"
+
+       rm -f /tmp/image.bs /tmp/partmap.bootdisk /tmp/partmap.image
+
+       if [ -n "$diff" ]; then
+               v "Partition layout has changed. Full image will be written."
+               ask_bool 0 "Abort" && exit 1
+               return 0
+       fi
+}
+
+platform_copy_config() {
+       local partdev parttype=ext4
+
+       if export_partdevice partdev 2; then
+               mount -t $parttype -o rw,noatime "/dev/$partdev" /mnt
+               cp -af "$UPGRADE_BACKUP" "/mnt/$BACKUP_FILE"
+               umount /mnt
+       else
+               v "ERROR: Unable to find partition to copy config data to"
+       fi
+
+       sleep 5
+}
+
+# To avoid writing over any firmware
+# files (e.g ubootefi.var or firmware/X/ aka EBBR)
+# Copy efi/openwrt and efi/boot from the new image
+# to the existing ESP
+platform_do_upgrade_efi_system_partition() {
+       local image_file=$1
+       local target_partdev=$2
+       local image_efisp_start=$3
+       local image_efisp_size=$4
+
+       v "Updating ESP on ${target_partdev}"
+       NEW_ESP_DIR="/mnt/new_esp_loop"
+       CUR_ESP_DIR="/mnt/cur_esp"
+       mkdir "${NEW_ESP_DIR}"
+       mkdir "${CUR_ESP_DIR}"
+
+       get_image_dd "$image_file" of="/tmp/new_efi_sys_part.img" \
+               skip="$image_efisp_start" count="$image_efisp_size"
+
+       mount -t vfat -o loop -o ro /tmp/new_efi_sys_part.img "${NEW_ESP_DIR}"
+       if [ ! -d "${NEW_ESP_DIR}/efi/boot" ]; then
+               v "ERROR: Image does not contain EFI boot files (/efi/boot)"
+               return 1
+       fi
+
+       mount -t vfat "/dev/$partdev" "${CUR_ESP_DIR}"
+
+       for d in $(find "${NEW_ESP_DIR}/efi/" -mindepth 1 -maxdepth 1 -type d); do
+               v "Copying ${d}"
+               newdir_bname=$(basename "${d}")
+               rm -rf "${CUR_ESP_DIR}/efi/${newdir_bname}"
+               cp -r "${d}" "${CUR_ESP_DIR}/efi"
+       done
+
+       umount "${NEW_ESP_DIR}"
+       umount "${CUR_ESP_DIR}"
+}
+
+platform_do_upgrade() {
+       local board=$(board_name)
+       local diskdev partdev diff
+
+       export_bootdevice && export_partdevice diskdev 0 || {
+               v "platform_do_upgrade: Unable to determine upgrade device"
+               return 1
+       }
+
+       sync
+
+       if [ "$UPGRADE_OPT_SAVE_PARTITIONS" = "1" ]; then
+               get_partitions "/dev/$diskdev" bootdisk
+
+               v "Extract boot sector from the image"
+               get_image_dd "$1" of=/tmp/image.bs count=63 bs=512b
+
+               get_partitions /tmp/image.bs image
+
+               #compare tables
+               diff="$(grep -F -x -v -f /tmp/partmap.bootdisk /tmp/partmap.image)"
+       else
+               diff=1
+       fi
+
+       # Only change the partition table if sysupgrade -p is set,
+       # otherwise doing so could interfere with embedded "single storage"
+       # (e.g SoC boot from SD card) setups, as well as other user
+       # created storage (like uvol)
+       if [ -n "$diff" ] && [ "${UPGRADE_OPT_SAVE_PARTITIONS}" = "0" ]; then
+               # Need to remove partitions before dd, otherwise the partitions
+               # that are added after will have minor numbers offset
+               partx -d - "/dev/$diskdev"
+
+               get_image_dd "$1" of="/dev/$diskdev" bs=4096 conv=fsync
+
+               # Separate removal and addtion is necessary; otherwise, partition 1
+               # will be missing if it overlaps with the old partition 2
+               partx -a - "/dev/$diskdev"
+
+               return 0
+       fi
+
+       #iterate over each partition from the image and write it to the boot disk
+       while read part start size; do
+               if export_partdevice partdev $part; then
+                       v "Writing image to /dev/$partdev..."
+                       if [ "$part" = "1" ]; then
+                               platform_do_upgrade_efi_system_partition \
+                                       $1 $partdev $start $size || return 1
+                       else
+                               v "Normal partition, doing DD"
+                               get_image_dd "$1" of="/dev/$partdev" ibs=512 obs=1M skip="$start" \
+                                       count="$size" conv=fsync
+                       fi
+               else
+                       v "Unable to find partition $part device, skipped."
+               fi
+       done < /tmp/partmap.image
+
+       local parttype=ext4
+
+       if (blkid > /dev/null) && export_partdevice partdev 1; then
+               part_magic_fat "/dev/$partdev" && parttype=vfat
+               mount -t $parttype -o rw,noatime "/dev/$partdev" /mnt
+               if export_partdevice partdev 2; then
+                       THIS_PART_BLKID=$(blkid -o value -s PARTUUID "/dev/${partdev}")
+                       v "Setting rootfs PARTUUID=${THIS_PART_BLKID}"
+                       sed -i "s/\(PARTUUID=\)[a-f0-9-]\+/\1${THIS_PART_BLKID}/ig" \
+                               /mnt/efi/openwrt/grub.cfg
+               fi
+               umount /mnt
+       fi
+       # Provide time for the storage medium to flush before system reset
+       # (despite the sync/umount it appears NVMe etc. do it in the background)
+       sleep 5
+}
index 5f9ef21d658beba45f035c25b17aba06d7b9f620..bd75f85996d72b42ce7c2f6f7660fa74221ad195 100644 (file)
 include $(TOPDIR)/rules.mk
 include $(INCLUDE_DIR)/image.mk
 
-define Image/BuildKernel
-       $(foreach k,$(filter zImage Image,$(KERNELNAME)), \
-               cp $(KDIR)/$(KERNELNAME) $(BIN_DIR)/$(IMG_PREFIX)-$(k) \
+GRUB2_VARIANT =
+GRUB_TERMINALS =
+GRUB_SERIAL_CONFIG =
+GRUB_TERMINAL_CONFIG =
+GRUB_CONSOLE_CMDLINE = earlycon
+
+ifneq ($(CONFIG_GRUB_CONSOLE),)
+  GRUB_TERMINALS += console
+endif
+
+GRUB_SERIAL:=$(call qstrip,$(CONFIG_GRUB_SERIAL))
+
+ifneq ($(GRUB_SERIAL),)
+  GRUB_SERIAL_CONFIG := serial --unit=0 --speed=$(CONFIG_GRUB_BAUDRATE) --word=8 --parity=no --stop=1 --rtscts=$(if $(CONFIG_GRUB_FLOWCONTROL),on,off)
+  GRUB_TERMINALS += serial
+endif
+
+ifneq ($(GRUB_TERMINALS),)
+  GRUB_TERMINAL_CONFIG := terminal_input $(GRUB_TERMINALS); terminal_output $(GRUB_TERMINALS)
+endif
+
+ROOTPART:=$(call qstrip,$(CONFIG_TARGET_ROOTFS_PARTNAME))
+ROOTPART:=$(if $(ROOTPART),$(ROOTPART),PARTUUID=$(IMG_PART_SIGNATURE)-02)
+GPT_ROOTPART:=$(call qstrip,$(CONFIG_TARGET_ROOTFS_PARTNAME))
+GPT_ROOTPART:=$(if $(GPT_ROOTPART),$(GPT_ROOTPART),PARTUUID=$(shell echo $(IMG_PART_DISKGUID) | sed 's/00$$/02/'))
+
+GRUB_TIMEOUT:=$(call qstrip,$(CONFIG_GRUB_TIMEOUT))
+GRUB_TITLE:=$(call qstrip,$(CONFIG_GRUB_TITLE))
+
+BOOTOPTS:=$(call qstrip,$(CONFIG_GRUB_BOOTOPTS))
+
+define Build/combined
+  $(INSTALL_DIR) $@.boot/
+       $(CP) $(KDIR)/$(KERNEL_NAME) $@.boot/efi/openwrt/
+       -$(CP) $(STAGING_DIR_ROOT)/boot/. $@.boot/boot/
+       $(if $(filter $(1),efi),
+               $(INSTALL_DIR) $@.boot/efi/boot
+               $(CP) $(STAGING_DIR_IMAGE)/grub2/boot$(if $(CONFIG_aarch64),aa64,arm).efi $@.boot/efi/openwrt/
+               $(CP) $(STAGING_DIR_IMAGE)/grub2/boot$(if $(CONFIG_aarch64),aa64,arm).efi $@.boot/efi/boot/
        )
+       FAT_TYPE="32" PADDING="1" SIGNATURE="$(IMG_PART_SIGNATURE)" \
+               $(if $(filter $(1),efi),GUID="$(IMG_PART_DISKGUID)") $(SCRIPT_DIR)/gen_image_generic.sh \
+               $@ \
+               $(CONFIG_TARGET_KERNEL_PARTSIZE) $@.boot \
+               $(CONFIG_TARGET_ROOTFS_PARTSIZE) $(IMAGE_ROOTFS) \
+               256
 endef
 
-define Image/Build/Initramfs
-       $(foreach k,$(filter zImage Image,$(KERNELNAME)), \
-               cp $(KDIR)/$(k)-initramfs $(BIN_DIR)/$(IMG_PREFIX)-$(k)-initramfs \
-       )
+define Build/grub-config
+       rm -fR $@.boot
+       $(INSTALL_DIR) $@.boot/efi/openwrt/
+       sed \
+               -e 's#@SERIAL_CONFIG@#$(strip $(GRUB_SERIAL_CONFIG))#g' \
+               -e 's#@TERMINAL_CONFIG@#$(strip $(GRUB_TERMINAL_CONFIG))#g' \
+               -e 's#@ROOTPART@#root=$(ROOTPART) rootwait#g' \
+               -e 's#@GPT_ROOTPART@#root=$(GPT_ROOTPART) rootwait#g' \
+               -e 's#@CMDLINE@#$(BOOTOPTS) $(GRUB_CONSOLE_CMDLINE)#g' \
+               -e 's#@TIMEOUT@#$(GRUB_TIMEOUT)#g' \
+               -e 's#@TITLE@#$(GRUB_TITLE)#g' \
+               -e 's#@KERNEL_NAME@#$(KERNEL_NAME)#g' \
+               ./grub-$(1).cfg > $@.boot/efi/openwrt/grub.cfg
 endef
 
-define Image/Build/gzip
-       gzip -f9n $(BIN_DIR)/$(IMG_ROOTFS)-$(1).img
+define Build/grub-install
+       rm -fR $@.grub2
+       $(INSTALL_DIR) $@.grub2
 endef
 
-$(eval $(call Image/gzip-ext4-padded-squashfs))
+DEVICE_VARS += GRUB2_VARIANT
+define Device/efi-default
+  IMAGE/rootfs.img := append-rootfs | pad-to $(ROOTFS_PARTSIZE)
+  IMAGE/rootfs.img.gz := append-rootfs | pad-to $(ROOTFS_PARTSIZE) | gzip
+  IMAGE/combined.img := grub-config efi | combined efi | grub-install efi | append-metadata
+  IMAGE/combined.img.gz := grub-config efi | combined efi | grub-install efi | gzip | append-metadata
+  IMAGE/combined.vmdk := grub-config efi | combined efi | grub-install efi | qemu-image vmdk
+ ifeq ($(CONFIG_TARGET_IMAGES_GZIP),y)
+    IMAGES-y := rootfs.img.gz
+    IMAGES-y += combined.img.gz
+  else
+    IMAGES-y := rootfs.img
+    IMAGES-y += combined.img
+  endif
+  ifeq ($(CONFIG_VMDK_IMAGES),y)
+    IMAGES-y += combined.vmdk
+  endif
+  KERNEL := kernel-bin
+  KERNEL_INSTALL := 1
+  IMAGES := $$(IMAGES-y)
+  ARTIFACTS := $$(ARTIFACTS-y)
+  SUPPORTED_DEVICES :=
+ ifeq ($(CONFIG_arm),y)
+  KERNEL_NAME = zImage
+ endif
+endef
 
-define Image/Build
-       $(call Image/Build/$(1))
-       $(CP) $(KDIR)/root.$(1) $(BIN_DIR)/$(IMG_ROOTFS)-$(1).img
-       $(call Image/Build/gzip/$(1))
+define Device/generic
+  $(call Device/efi-default)
+  DEVICE_TITLE := Generic EFI Boot
+  GRUB2_VARIANT := generic
+  FILESYSTEMS := ext4 squashfs
+  DEVICE_PACKAGES += kmod-amazon-ena kmod-e1000e kmod-vmxnet3 kmod-rtc-rx8025 \
+       kmod-i2c-mux-pca954x kmod-gpio-pca953x partx-utils
 endef
+TARGET_DEVICES += generic
 
 $(eval $(call BuildImage))
diff --git a/target/linux/armvirt/image/grub-efi.cfg b/target/linux/armvirt/image/grub-efi.cfg
new file mode 100644 (file)
index 0000000..fd329e4
--- /dev/null
@@ -0,0 +1,14 @@
+@SERIAL_CONFIG@
+@TERMINAL_CONFIG@
+
+set default="0"
+set timeout="@TIMEOUT@"
+
+menuentry "@TITLE@" {
+       search --set=root --label kernel
+       linux /efi/openwrt/@KERNEL_NAME@ @GPT_ROOTPART@ @CMDLINE@ noinitrd
+}
+menuentry "@TITLE@ (failsafe)" {
+       search --set=root --label kernel
+       linux /efi/openwrt/@KERNEL_NAME@ failsafe=true @GPT_ROOTPART@ @CMDLINE@ noinitrd
+}