From 328495cd629ed53a0d9e046f3e2faa435c608d12 Mon Sep 17 00:00:00 2001
From: "Luke T. Shumaker" <lukeshu@lukeshu.com>
Date: Sat, 13 Jan 2024 15:54:01 -0700
Subject: [PATCH 2/4] 10_linux, 20_linux_xen: detect parabola initramfs
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Based on the following downstream commits:

 - https://gitlab.archlinux.org/archlinux/packaging/packages/grub/-/commit/438b56c08a536202ae922c8a3f6023bd9aea7330
 - https://gitlab.archlinux.org/archlinux/packaging/packages/grub/-/commit/1c268927020d5ee37055250ee3de3ce4fdae23eb
 - https://gitlab.archlinux.org/archlinux/packaging/packages/grub/-/commit/fa9d34525f424546cf83cf396ef7d6d4b817640b
 - https://gitlab.archlinux.org/archlinux/packaging/packages/grub/-/commit/5347cbc7b8122e4f36068c12a649960240c4be07
 - https://gitlab.archlinux.org/archlinux/packaging/packages/grub/-/commit/80d4490bc85d2b35a6e08c1bb3211391e70f24ca

 - https://git.parabola.nu/abslibre.git/commit/?id=575127b3d909f886e87526c3e9369fe690d862c2
 - https://git.parabola.nu/abslibre.git/commit/?id=be608b5fc736ea2249b1224f2f6999cb76ca5059
 - https://git.parabola.nu/abslibre.git/commit/?id=cfc5b3757f11b7760d5246cd181f084328cae912
 - https://git.parabola.nu/abslibre.git/commit/?id=ede60ed00d4c781adadca99ddef19be1bf56e8aa
 - https://git.parabola.nu/abslibre.git/commit/?id=eaf3d6f308094aca4f87ed111c2580e81b18f269
 - https://git.parabola.nu/abslibre.git/commit/?id=d3fff86fb7d77716f3ca171b633bd462202a3b18
 - https://git.parabola.nu/abslibre.git/commit/?id=856b2c7c881ecf9ef2607a9a4c4466635fea857f

Co-Authored-By: Keshav Amburay <the.ridikulus.rat@gmail.com> (2014)
Co-Authored-By: Tobias Powalowski <tpowa@archlinux.org> (2014)
Co-Authored-By: Christian Hesse <eworm@archlinux.org> (2016, 2021)
Co-Authored-By: André Fabian Silva Delgado <emulatorman@parabola.nu> (2014)
Co-Authored-By: Márcio Alexandre Silva Delgado <coadde@parabola.nu> (2014-2015)
Co-Authored-By: Omar Vega Ramos <ovruni@gnu.org.pe> (2021)
---
 util/grub.d/10_linux.in     | 37 ++++++++++++++++++++++++++++++++-----
 util/grub.d/20_linux_xen.in | 24 +++++++++++++++++++-----
 2 files changed, 51 insertions(+), 10 deletions(-)

diff --git a/util/grub.d/10_linux.in b/util/grub.d/10_linux.in
index cc393be7e..772966b89 100644
--- a/util/grub.d/10_linux.in
+++ b/util/grub.d/10_linux.in
@@ -93,10 +93,14 @@ linux_entry ()
   fi
   if [ x$type != xsimple ] ; then
       case $type in
+	  booster)
+	      title="$(gettext_printf "%s, %s kernel (booster initramfs)" "${os}" "${version}")" ;;
+	  fallback)
+	      title="$(gettext_printf "%s, %s kernel (fallback initramfs)" "${os}" "${version}")" ;;
 	  recovery)
-	      title="$(gettext_printf "%s, with Linux %s (recovery mode)" "${os}" "${version}")" ;;
+	      title="$(gettext_printf "%s, %s kernel (recovery mode)" "${os}" "${version}")" ;;
 	  *)
-	      title="$(gettext_printf "%s, with Linux %s" "${os}" "${version}")" ;;
+	      title="$(gettext_printf "%s, %s kernel" "${os}" "${version}")" ;;
       esac
       if [ x"$title" = x"$GRUB_ACTUAL_DEFAULT" ] || [ x"Previous Linux versions>$title" = x"$GRUB_ACTUAL_DEFAULT" ]; then
 	  replacement_title="$(echo "Advanced options for ${OS}" | sed 's,>,>>,g')>$(echo "$title" | sed 's,>,>>,g')"
@@ -108,7 +112,7 @@ linux_entry ()
   else
       echo "menuentry '$(echo "$os" | grub_quote)' ${CLASS} \$menuentry_id_option 'gnulinux-simple-$boot_device_id' {" | sed "s/^/$submenu_indentation/"
   fi      
-  if [ x$type != xrecovery ] ; then
+  if [ x$type != xrecovery ] && [ x$type != xfallback ] ; then
       save_default_entry | grub_add_tab
   fi
 
@@ -140,7 +144,7 @@ linux_entry ()
     fi
     printf '%s\n' "${prepare_boot_cache}" | sed "s/^/$submenu_indentation/"
   fi
-  message="$(gettext_printf "Loading Linux %s ..." ${version})"
+  message="$(gettext_printf "Loading %s kernel ..." ${version})"
   sed "s/^/$submenu_indentation/" << EOF
 	echo	'$(echo "$message" | grub_quote)'
 	linux	${rel_dirname}/${basename} root=${linux_root_device_thisversion} ro ${args}
@@ -210,7 +214,7 @@ for linux in ${reverse_sorted_list}; do
   basename=`basename $linux`
   dirname=`dirname $linux`
   rel_dirname=`make_system_path_relative_to_its_root $dirname`
-  version=`echo $basename | sed -e "s,^[^0-9]*-,,g"`
+  version=`echo $basename | sed -e "s,vmlinuz-,,g"`
   alt_version=`echo $version | sed -e "s,\.old$,,g"`
   linux_root_device_thisversion="${LINUX_ROOT_DEVICE}"
 
@@ -299,6 +303,29 @@ for linux in ${reverse_sorted_list}; do
 
   linux_entry "${OS}" "${version}" advanced \
               "${GRUB_CMDLINE_LINUX} ${GRUB_CMDLINE_LINUX_DEFAULT}"
+
+  if test -e "${dirname}/initramfs-${version}-fallback.img" ; then
+    initrd="${initrd_early} initramfs-${version}-fallback.img"
+
+    if test -n "${initrd}" ; then
+      gettext_printf "Found fallback initramfs image: %s\n" "${dirname}/${initrd}" >&2
+    fi
+
+    linux_entry "${OS}" "${version}" fallback \
+                "${GRUB_CMDLINE_LINUX} ${GRUB_CMDLINE_LINUX_DEFAULT}"
+  fi
+
+  if test -e "${dirname}/booster-${version}.img" ; then
+    initrd="${initrd_early} booster-${version}.img"
+
+    if test -n "${initrd}" ; then
+      gettext_printf "Found booster initrd image(s) in %s:%s\n" "${dirname}" "${initrd_extra} ${initrd}" >&2
+    fi
+
+    linux_entry "${OS}" "${version}" booster \
+                "${GRUB_CMDLINE_LINUX} ${GRUB_CMDLINE_LINUX_DEFAULT}"
+  fi
+
   if [ "x${GRUB_DISABLE_RECOVERY}" != "xtrue" ]; then
     linux_entry "${OS}" "${version}" recovery \
                 "${GRUB_CMDLINE_LINUX_RECOVERY} ${GRUB_CMDLINE_LINUX}"
diff --git a/util/grub.d/20_linux_xen.in b/util/grub.d/20_linux_xen.in
index 94dd8be13..abc2fd7b4 100644
--- a/util/grub.d/20_linux_xen.in
+++ b/util/grub.d/20_linux_xen.in
@@ -120,9 +120,11 @@ linux_entry_xsm ()
   fi
   if [ x$type != xsimple ] ; then
       if [ x$type = xrecovery ] ; then
-	  title="$(gettext_printf "%s, with Xen %s and Linux %s (recovery mode)" "${os}" "${entry_xen_version}" "${version}")"
+	  title="$(gettext_printf "%s, with Xen %s and %s kernel (recovery mode)" "${os}" "${entry_xen_version}" "${version}")"
+      elif [ x$type = xfallback ] ; then
+	  title="$(gettext_printf "%s, with Xen %s and %s kernel (fallback initramfs)" "${os}" "${entry_ xen_version}" "${version}")"
       else
-	  title="$(gettext_printf "%s, with Xen %s and Linux %s" "${os}" "${entry_xen_version}" "${version}")"
+	  title="$(gettext_printf "%s, with Xen %s and %s kernel" "${os}" "${entry_xen_version}" "${version}")"
       fi
       replacement_title="$(echo "Advanced options for ${OS}" | sed 's,>,>>,g')>$(echo "$title" | sed 's,>,>>,g')"
       if [ x"Xen ${entry_xen_version}>$title" = x"$GRUB_ACTUAL_DEFAULT" ]; then
@@ -135,7 +137,7 @@ linux_entry_xsm ()
       title="$(gettext_printf "%s, with Xen hypervisor" "${os}")"
       echo "menuentry '$(echo "$title" | grub_quote)' ${CLASS} \$menuentry_id_option 'xen-gnulinux-simple-$boot_device_id' {" | sed "s/^/$submenu_indentation/"
   fi
-  if [ x$type != xrecovery ] ; then
+  if [ x$type != xrecovery ] && [ x$type != xfallback ] ; then
       save_default_entry | grub_add_tab | sed "s/^/$submenu_indentation/"
   fi
 
@@ -144,7 +146,7 @@ linux_entry_xsm ()
   fi
   printf '%s\n' "${prepare_boot_cache}" | sed "s/^/$submenu_indentation/"
   xmessage="$(gettext_printf "Loading Xen %s ..." ${entry_xen_version})"
-  lmessage="$(gettext_printf "Loading Linux %s ..." ${version})"
+  lmessage="$(gettext_printf "Loading %s kernel ..." ${version})"
   sed "s/^/$submenu_indentation/" << EOF
 	echo	'$(echo "$xmessage" | grub_quote)'
         if [ "\$grub_platform" = "pc" -o "\$grub_platform" = "" ]; then
@@ -290,7 +292,7 @@ for current_xen in ${reverse_sorted_xen_list}; do
 	basename=`basename $linux`
 	dirname=`dirname $linux`
 	rel_dirname=`make_system_path_relative_to_its_root $dirname`
-	version=`echo $basename | sed -e "s,^[^0-9]*-,,g"`
+	version=`echo $basename | sed -e "s,vmlinuz-,,g"`
 	alt_version=`echo $version | sed -e "s,\.old$,,g"`
 	linux_root_device_thisversion="${LINUX_ROOT_DEVICE}"
 
@@ -361,6 +363,18 @@ for current_xen in ${reverse_sorted_xen_list}; do
 
 	linux_entry "${OS}" "${version}" "${xen_version}" advanced \
 	    "${GRUB_CMDLINE_LINUX} ${GRUB_CMDLINE_LINUX_DEFAULT}" "${GRUB_CMDLINE_XEN} ${GRUB_CMDLINE_XEN_DEFAULT}"
+
+	if test -e "${dirname}/initramfs-${version}-fallback.img" ; then
+	    initrd="initramfs-${version}-fallback.img"
+
+	    if test -n "${initrd}" ; then
+	        gettext_printf "Found fallback initramfs image: %s\n" "${dirname}/${initrd}" >&2
+	    fi
+
+	    linux_entry "${OS}" "${version}" "${xen_version}" fallback \
+	        "${GRUB_CMDLINE_LINUX} ${GRUB_CMDLINE_LINUX_DEFAULT}" "${GRUB_CMDLINE_XEN} ${GRUB_CMDLINE_XEN_DEFAULT}"
+	fi
+
 	if [ "x${GRUB_DISABLE_RECOVERY}" != "xtrue" ]; then
 	    linux_entry "${OS}" "${version}" "${xen_version}" recovery \
 		"${GRUB_CMDLINE_LINUX_RECOVERY} ${GRUB_CMDLINE_LINUX}" "${GRUB_CMDLINE_XEN}"
-- 
2.43.0

