#!/bin/sh
set -e
patch -p1 << 'EOF'
diff --git a/db/all.db b/db/all.db
index 1bacc51..765fe45 100644
--- a/db/all.db
+++ b/db/all.db
@@ -1168,6 +1168,27 @@ DTB-Id: sun7i-a20-mk808c.dtb
 U-Boot-Script-Name: bootscr.sunxi
 Required-Packages: u-boot-tools

+Machine: MNT Pocket Reform with i.MX8MP Module
+Kernel-Flavors: any
+Boot-Script-Path: /boot/boot.scr
+DTB-Id: freescale/imx8mp-mnt-pocket-reform.dtb
+U-Boot-Script-Name: bootscr.uboot-generic
+Required-Packages: u-boot-tools
+
+Machine: MNT Pocket Reform with BPI-CM4 Module
+Kernel-Flavors: any
+Boot-Script-Path: /boot/boot.scr
+DTB-Id: amlogic/meson-g12b-bananapi-cm4-mnt-pocket-reform.dtb
+U-Boot-Script-Name: bootscr.uboot-generic
+Required-Packages: u-boot-tools
+
+Machine: MNT Pocket Reform with RCORE RK3588 Module
+Kernel-Flavors: any
+DTB-Id: rockchip/rk3588-mnt-pocket-reform.dtb
+Boot-Script-Path: /boot/boot.scr
+U-Boot-Script-Name: bootscr.uboot-generic
+Required-Packages: u-boot-tools
+
 Machine: MNT Reform 2
 Kernel-Flavors: any
 Boot-Script-Path: /boot/boot.scr
@@ -1185,6 +1192,48 @@ Boot-Script-Path: /boot/boot.scr
 U-Boot-Script-Name: bootscr.uboot-generic
 Required-Packages: u-boot-tools

+Machine: MNT Reform 2 with BPI-CM4 Module
+Kernel-Flavors: any
+DTB-Id: amlogic/meson-g12b-bananapi-cm4-mnt-reform2.dtb
+Boot-Script-Path: /boot/boot.scr
+U-Boot-Script-Name: bootscr.uboot-generic
+Required-Packages: u-boot-tools
+
+Machine: MNT Reform 2 with i.MX8MP Module
+Kernel-Flavors: any
+DTB-Id: freescale/imx8mp-mnt-reform2.dtb
+Boot-Script-Path: /boot/boot.scr
+U-Boot-Script-Name: bootscr.uboot-generic
+Required-Packages: u-boot-tools
+
+Machine: MNT Reform 2 with LS1028A Module
+Kernel-Flavors: any
+DTB-Id: freescale/fsl-ls1028a-mnt-reform2.dtb
+Boot-Script-Path: /boot/boot.scr
+U-Boot-Script-Name: bootscr.uboot-generic
+Required-Packages: u-boot-tools
+
+Machine: MNT Reform 2 with RCORE RK3588 Module
+Kernel-Flavors: any
+DTB-Id: rockchip/rk3588-mnt-reform2.dtb
+Boot-Script-Path: /boot/boot.scr
+U-Boot-Script-Name: bootscr.uboot-generic
+Required-Packages: u-boot-tools
+
+Machine: MNT Reform 2 with RCORE-DSI RK3588 Module
+Kernel-Flavors: any
+DTB-Id: rockchip/rk3588-mnt-reform2-dsi.dtb
+Boot-Script-Path: /boot/boot.scr
+U-Boot-Script-Name: bootscr.uboot-generic
+Required-Packages: u-boot-tools
+
+Machine: MNT Reform Next with RCORE RK3588 Module
+Kernel-Flavors: any
+DTB-Id: rockchip/rk3588-mnt-reform-next.dtb
+Boot-Script-Path: /boot/boot.scr
+U-Boot-Script-Name: bootscr.uboot-generic
+Required-Packages: u-boot-tools
+
 Machine: MSI Primo81 tablet
 Kernel-Flavors: armmp armmp-lpae
 Boot-Script-Path: /boot/boot.scr
EOF

# fix https://bugs.debian.org/1102690
patch -p1 << 'EOF'
From ea9f0c7e91f2454c0a05d72f9ecfdb7164f89ede Mon Sep 17 00:00:00 2001
From: Dave Jones <dave.jones@canonical.com>
Date: Mon, 6 Mar 2023 13:53:51 +0000
Subject: [PATCH 1/3] Always run flash-kernel

When both flash-kernel and the kernel are upgraded together, it is
possible that the f-k trigger is unconfigured when the kernel attempts
to fire it, resulting in the invocation being lost. This change ensures
that the f-k trigger is set during *all* maintscript operations.

Related issue: LP: #1667742
---
 debian/flash-kernel.postinst | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)

diff --git a/debian/flash-kernel.postinst b/debian/flash-kernel.postinst
index 849cf12..75d08b1 100755
--- a/debian/flash-kernel.postinst
+++ b/debian/flash-kernel.postinst
@@ -34,10 +34,7 @@ case "$1" in
 			/etc/default/flash-kernel
 		ucfr flash-kernel /etc/default/flash-kernel
 		;;
-	triggered)
-		FLASH_KERNEL_NOTRIGGER=y flash-kernel
-		;;
-	abort-upgrade|abort-remove|abort-deconfigure)
+	triggered|abort-upgrade|abort-remove|abort-deconfigure)
 		;;
 	*)
 		echo "postinst called with unknown argument \`$1'" >&2
@@ -45,4 +42,15 @@ case "$1" in
 		;;
 esac
 
+case "$1" in
+	triggered)
+		# this forces flash-kernel to run rather than setting our trigger to
+		# cause a later run (as happens without _NOTRIGGER being set)
+		FLASH_KERNEL_NOTRIGGER=y flash-kernel
+		;;
+	*)
+		flash-kernel
+		;;
+esac
+
 #DEBHELPER#
-- 
GitLab


From 23379fa9bd41458b1db06a5613e559c9844369a7 Mon Sep 17 00:00:00 2001
From: Dave Jones <dave.jones@canonical.com>
Date: Mon, 6 Mar 2023 14:01:59 +0000
Subject: [PATCH 2/3] Avoid a double run of handle_dtb and --force with no
 kvers

The logic surrounding $kvers deals with three scenarios: kvers is blank
(we want to flash the latest kernel), kvers is non-blank and is the
latest kernel, and kvers is non-blank and is *not* the latest kernel.

In the second case (typical during a kernel upgrade), f-k runs
handle_dtb twice (redundantly). At the top of main() after checking
kvers is non-blank and then later after checking for --force and whether
kvers is not the latest version.

The existing logic also permits the (rather nonsensical) case of being
called with --force but with no kernel version specified. This commit
re-works the logic to avoid running handle_dtb redundantly, and to error
out with an appropriate message if called with --force but with no
kernel version.
---
 functions | 41 +++++++++++++++++++++++++----------------
 1 file changed, 25 insertions(+), 16 deletions(-)

diff --git a/functions b/functions
index a2e2b31..6f75836 100644
--- a/functions
+++ b/functions
@@ -727,22 +727,6 @@ if [ -n "$kvers" ] && [ "$FK_KERNEL_HOOK_SCRIPT" = "postrm.d" ]; then
 	fi
 fi
 
-if [ "$kvers" != "$latest_version" ] && [ "x$force" = "xyes" ]; then
-	echo "flash-kernel: forcing install of ${kvers} instead of ${latest_version}." >&2
-	echo "flash-kernel: WARNING: Installing any new kernel package might override this." >&2
-else
-	if [ -n "$kvers" ] && [ "$kvers" != "$latest_version" ]; then
-		echo "Ignoring old or unknown version $kvers (latest is $latest_version)" >&2
-		if [ "$FK_KERNEL_HOOK_SCRIPT" = "postinst.d" ]; then
-			exit 0
-		fi
-		echo "Use --force if you want version $kvers." >&2
-	fi
-	kvers="$latest_version"
-	# Make sure we install the DTB for $latest_version
-	handle_dtb
-fi
-
 # accumulate multiple calls in a trigger to only run flash-kernel once; the
 # trigger will just call flash-kernel again with FLASH_KERNEL_NOTRIGGER set to
 # force a real run
@@ -759,6 +743,31 @@ if [ -z "$FLASH_KERNEL_NOTRIGGER" ] && [ -n "$DPKG_MAINTSCRIPT_PACKAGE" ] && dpk
 	# dpkg-trigger failed for some reason, proceed to a normal run
 fi
 
+case "$kvers" in
+	"")
+		if [ "$force" = "yes" ]; then
+			echo "flash-kernel: no kernel version specified with --force." >&2
+			exit 1
+		else
+			kvers="$latest_version"
+			# Make sure we install the DTB for $latest_version
+			handle_dtb
+		fi
+		;;
+	"$latest_version")
+		# Proceed as normal; --force is basically a noop here
+		;;
+	*)
+		if [ "$force" = "yes" ]; then
+			echo "flash-kernel: forcing install of ${kvers} instead of ${latest_version}." >&2
+			echo "flash-kernel: WARNING: Installing any new kernel package might override this." >&2
+		else
+			echo "Ignoring old or unknown version $kvers (latest is $latest_version)" >&2
+			exit 0
+		fi
+		;;
+esac
+
 kfile="/boot/vmlinuz-$kvers"
 ifile="/boot/initrd.img-$kvers"
 desc="kernel $kvers"
-- 
GitLab


From 894fdf593f71f3e2fa5b93a0391edb67e86eefc6 Mon Sep 17 00:00:00 2001
From: Dave Jones <dave.jones@canonical.com>
Date: Mon, 6 Mar 2023 15:49:20 +0000
Subject: [PATCH 3/3] Defer if selected kernel is not fully "installed"

When flash-kernel runs as part of a kernel installation, that kernel may
not be in the fully "installed" state. If it's a newly installed kernel,
it may also be in "triggers-pending" in which case, while the symlinks
for vmlinuz-$ver and initrd.img-$ver exist, the initrd itself does not
yet exist (because the initramfs-tools trigger has not yet run, so the
initrd symlink is "dead").

In this case, depending on the "method" selected for the board, the f-k
run may crash (aborting apt), or may continue but flash a non-functional
boot setup. In the latter case, this usually corrects itself with a
later run of f-k but it still potentially leaves the system in an
unbootable state for some portion of an apt run (which is not ideal).

This commit adds a check on the status of the package which owns the
kernel version requested to be installed. If it is not fully
"installed", it defers the f-k run (under the assumption it will become
installed at some point in the apt run). This can be overridden with
"--force". Tests are included for the new function.

Related issues: LP: #2007827, LP: #1861292, Debian bug #781742
---
 functions      | 35 +++++++++++++++++++
 test_functions | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 129 insertions(+)

diff --git a/functions b/functions
index 6f75836..d541f41 100644
--- a/functions
+++ b/functions
@@ -597,6 +597,23 @@ android_flash() {
 	echo "done." >&2
 }
 
+get_kernel_status() {
+	# get_kernel_status(query_ver)
+	# return the dpkg package status of kernel version $query_ver
+	local query_ver="$1"
+	local ver path pkg
+	linux-version list --paths | while read -r ver path; do
+		if linux-version compare "$ver" eq "$query_ver"; then
+			pkg=$(
+				LANG=C.UTF-8 dpkg-query --search "$path" 2>/dev/null |
+				awk -F ": " -v path="$path" '$2==path { print $1; }'
+			)
+			LANG=C.UTF-8 dpkg-query --show --showformat '${db:Status-Status}' "$pkg"
+			break
+		fi
+	done
+}
+
 find_dtb_file() {
 	local dtb
 	case "$dtb_dir" in
@@ -768,6 +785,24 @@ case "$kvers" in
 		;;
 esac
 
+kstatus=$(get_kernel_status "$kvers")
+if [ "$kstatus" != "installed" ]; then
+	echo "flash-kernel: requested kernel ${kvers} is in state ${kstatus}." >&2
+	if [ -n "$DPKG_RUNNING_VERSION" ]; then
+		# We're running under dpkg; re-trigger ourselves under the assumption
+		# that the status will resolve
+		if dpkg-trigger --no-await flash-kernel; then
+			echo "flash-kernel: deferring update (trigger activated)" >&2
+			exit 0
+		else
+			echo "flash-kernel: deferral failed; proceeding with install" >&2
+		fi
+	elif [ "$force" != "yes" ]; then
+		exit 1
+	fi
+	echo "flash-kernel: WARNING: Components like the initrd may be missing or require regeneration." >&2
+fi
+
 kfile="/boot/vmlinuz-$kvers"
 ifile="/boot/initrd.img-$kvers"
 desc="kernel $kvers"
diff --git a/test_functions b/test_functions
index eeea52f..5795448 100755
--- a/test_functions
+++ b/test_functions
@@ -689,6 +689,100 @@ test_dtb_append_from() {
 }
 add_test test_dtb_append_from
 
+test_get_kernel_status() {
+    available="
+5.15.0-1021-generic /boot/vmlinuz-5.15.0-1021-generic
+5.15.0-1022-raspi /boot/vmlinuz-5.15.0-1022-raspi
+5.15.0-1022-raspi-nolpae /boot/vmlinuz-5.15.0-1022-raspi-nolpae
+5.15.0-1023-raspi /boot/vmlinuz-5.15.0-1023-raspi"
+    (
+        linux_version() {
+            case "$1" in
+                list)
+                    if [ "$2" = "--paths" ]; then
+                        echo "$available"
+                    else
+                        echo "$available" | while read -r ver path; do
+                            echo "$ver"
+                        done
+                    fi
+                    ;;
+                compare)
+                    case "$3" in
+                        eq) [ "$2" = "$4" ] ;;
+                        ne) [ "$2" != "$4" ] ;;
+                        *)
+                            echo "Unexpected arg in test-case"
+                            exit 1
+                            ;;
+                    esac
+                    ;;
+            esac
+        }
+        dpkg_query() {
+            case "$1" in
+                --search)
+                    shift
+                    while [ -n "$1" ]; do
+                        case "$1" in
+                            /boot/vmlinuz-5.15.0-*)
+                                echo "linux-image-${1#/boot/vmlinuz-}: $1"
+                                ;;
+                            *)
+                                echo "Unexpected arg in test-case"
+                                exit 1
+                                ;;
+                        esac
+                        shift
+                    done
+                    ;;
+                --show)
+                    shift
+                    while [ -n "$1" ]; do
+                        case "$1" in
+                            --showformat)
+                                shift
+                                ;;
+                            linux-image-5.15.0-1022-raspi*)
+                                echo -n installed
+                                ;;
+                            linux-image-5.15.0-1023-*)
+                                echo -n triggers-pending
+                                ;;
+                            linux-image-*)
+                                echo -n not-installed
+                                ;;
+                            *)
+                                echo "Unexpected arg in test-case"
+                                exit 1
+                                ;;
+                        esac
+                        shift
+                    done
+                    ;;
+            esac
+        }
+        # Can't define a function with a hyphen in the name but luckily, dash
+        # does permit them in alias names
+        alias dpkg-query=dpkg_query
+        alias linux-version=linux_version
+        . "$functions"
+        if [ "$(get_kernel_status 5.15.0-1022-raspi)" != "installed" ]; then
+            echo "get_kernel_status did not output the expected result" >&2
+            exit 1
+        fi
+        if [ "$(get_kernel_status 5.15.0-1023-raspi)" != "triggers-pending" ]; then
+            echo "get_kernel_status did not output the expected result" >&2
+            exit 1
+        fi
+        if [ "$(get_kernel_status 5.15.0-1021-generic)" != "not-installed" ]; then
+            echo "get_kernel_status did not output the expected result" >&2
+            exit 1
+        fi
+    )
+}
+add_test test_get_kernel_status
+
 test_main
 
 # vim:syntax=sh
-- 
GitLab
EOF