From f16a305e57ba962849e6faf2c8a596804691014e Mon Sep 17 00:00:00 2001
From: Richard Lyu <richard.lyu@suse.com>
Date: Sun, 29 Jun 2025 14:33:49 +0800
Subject: [PATCH] ShellPkg: Add post script for Shell installation

This patch adds RPM post-install and post-remove scripts for installing
Shell.efi, used to enable a UEFI Shell boot option in virtual machines. The
script is located at /usr/share/ovmf and automatically detects whether the OS
is SLES or openSUSE, then configures the appropriate UEFI boot entry for the
virtual machine.
---
 Build/Shell/.Shell_install.sh                 | 41 +++++++++++++++++++
 Build/Shell/.Shell_uninstall.sh               | 31 ++++++++++++++
 .../Shell/DEBUG_GCC5/StackCookieValues32.json |  1 +
 .../Shell/DEBUG_GCC5/StackCookieValues64.json |  1 +
 4 files changed, 74 insertions(+)
 create mode 100644 Build/Shell/.Shell_install.sh
 create mode 100644 Build/Shell/.Shell_uninstall.sh
 create mode 100644 Build/Shell/DEBUG_GCC5/StackCookieValues32.json
 create mode 100644 Build/Shell/DEBUG_GCC5/StackCookieValues64.json

diff --git a/Build/Shell/.Shell_install.sh b/Build/Shell/.Shell_install.sh
new file mode 100644
index 0000000000..e5fa23be0c
--- /dev/null
+++ b/Build/Shell/.Shell_install.sh
@@ -0,0 +1,41 @@
+#!/bin/bash
+
+{
+if systemd-detect-virt --quiet; then
+    EFI_PART=$(findmnt -no SOURCE /boot/efi)
+    if ! echo "$EFI_PART" | grep -q -E '^/dev/'; then
+        exit 0
+    fi
+
+    DISK=$(lsblk -no PKNAME "$EFI_PART" | head -n1)
+    DEV="/dev/$DISK"
+    PART_NUM=$(echo "$EFI_PART" | sed -E 's/.*[^0-9]([0-9]+)$/\1/')
+
+    . /etc/os-release
+    BOOT_LABEL="UEFI Shell"
+
+    if echo "$ID" | grep -qi "sles"; then
+        EFI_DIR="/boot/efi/EFI/sles"
+        EFI_PATH='\\EFI\\sles\\Shell.efi'
+		echo "sles"
+	elif echo "$ID" | grep -qi "opensuse"; then
+        EFI_DIR="/boot/efi/EFI/opensuse"
+        EFI_PATH='\\EFI\\opensuse\\Shell.efi'
+		echo "opensuse"
+    else
+        exit 0
+    fi
+
+    mkdir -p "$EFI_DIR"
+    install -m 0644 /usr/share/ovmf/Shell.efi "$EFI_DIR/Shell.efi"
+
+    efibootmgr -c -L "$BOOT_LABEL" -d "$DEV" -p "$PART_NUM" -l "$EFI_PATH"
+
+    NEW_BOOTNUM=$(efibootmgr | grep "$BOOT_LABEL" | sed -n 's/^Boot\([0-9A-Fa-f]*\)\*.*/\1/p' | head -n1)
+    CURRENT_ORDER=$(efibootmgr | grep '^BootOrder' | cut -d ' ' -f2 | tr -d '\n' | sed 's/,$//')
+    if [ -n "$NEW_BOOTNUM" ]; then
+        CLEANED_ORDER=$(echo "$CURRENT_ORDER" | sed "s/\b$NEW_BOOTNUM\b//g" | sed 's/,,*/,/g' | sed 's/^,//' | sed 's/,$//')
+        efibootmgr -o "$CLEANED_ORDER,$NEW_BOOTNUM"
+    fi
+fi
+} &> /dev/null
\ No newline at end of file
diff --git a/Build/Shell/.Shell_uninstall.sh b/Build/Shell/.Shell_uninstall.sh
new file mode 100644
index 0000000000..dcd778467f
--- /dev/null
+++ b/Build/Shell/.Shell_uninstall.sh
@@ -0,0 +1,31 @@
+#!/bin/bash
+
+{
+if systemd-detect-virt --quiet; then
+    EFI_PART=$(findmnt -no SOURCE /boot/efi)
+	if ! echo "$EFI_PART" | grep -q -E '^/dev/'; then
+		exit 0
+	fi
+
+	. /etc/os-release
+	BOOT_LABEL="UEFI Shell"
+
+	if echo "$ID" | grep -qi "sles"; then
+		DIR_NAME="sles"
+		EFI_PATH="\\EFI\\sles\\Shell.efi"
+	elif echo "$ID" | grep -qi "opensuse"; then
+		DIR_NAME="opensuse"
+		EFI_PATH="\\EFI\\opensuse\\Shell.efi"
+	else
+		exit 0
+	fi
+
+	BOOTNUM=$(efibootmgr | awk -v L="$BOOT_LABEL" -v D="$DIR" '$0 ~ L && $0 ~ D {sub(/^Boot/, "", $1); sub(/\*/, "", $1); print $1}' | head -n1)
+
+	if [ -n "$BOOTNUM" ]; then
+		efibootmgr -b "$BOOTNUM" -B
+		rm -f "/boot/efi/EFI/$DIR_NAME/Shell.efi"
+	fi
+
+fi
+} &> /dev/null
\ No newline at end of file
diff --git a/Build/Shell/DEBUG_GCC5/StackCookieValues32.json b/Build/Shell/DEBUG_GCC5/StackCookieValues32.json
new file mode 100644
index 0000000000..279006e935
--- /dev/null
+++ b/Build/Shell/DEBUG_GCC5/StackCookieValues32.json
@@ -0,0 +1 @@
+[3120319409, 3986684851, 2532066904, 2838841122, 1510610980, 3527598979, 2145389557, 915756566, 4288287152, 1592508515, 1649905414, 3214646158, 4125604801, 2636301533, 3186946058, 1297075897, 1536483215, 2684947706, 378837761, 2034357240, 1254156149, 3274923813, 1869941960, 2430363232, 2619983763, 789706441, 474468987, 4170744684, 2067453149, 80774667, 1188610392, 3484306439, 2129190303, 3706887221, 1441685697, 2832623778, 2272607630, 3766098863, 1387705257, 3531882784, 78420450, 2425693472, 2515037057, 2842949431, 2167471722, 2373850526, 2185844797, 1771878221, 3826200111, 233544227, 3019808295, 3255256900, 3737050793, 1272285847, 4114161312, 704148315, 2912601610, 3781534488, 56787233, 816583130, 2471213939, 2813874809, 2630289327, 1173288302, 1862737445, 2551923525, 1820462035, 1796829267, 1714358393, 2634249466, 176661566, 428907315, 2772923224, 1648291025, 2674956839, 2691960542, 1859704968, 709746926, 492109362, 3781180214, 4222775360, 2893670436, 2425292886, 1064615051, 3854554544, 1690467402, 356470947, 4203480635, 3958554922, 3830455836, 4051513359, 2084475517, 728710918, 2413960477, 1005365008, 117621347, 1988965873, 542004264, 1543091876, 856808939]
\ No newline at end of file
diff --git a/Build/Shell/DEBUG_GCC5/StackCookieValues64.json b/Build/Shell/DEBUG_GCC5/StackCookieValues64.json
new file mode 100644
index 0000000000..189f1bb32c
--- /dev/null
+++ b/Build/Shell/DEBUG_GCC5/StackCookieValues64.json
@@ -0,0 +1 @@
+[7518985701012212569, 2601960957474891530, 17831311744988480182, 16652208568711364861, 11779046321730877689, 4265457871546500992, 7292254499229112648, 223890800426602719, 2838072854045228586, 17406395504460044440, 15908843496796806072, 14702662319704085758, 9867044736590216777, 1826029253899249568, 13211023111777598167, 15781671485427291330, 10363743021216146144, 5806329751690313006, 15745089491775103262, 17509746045803567900, 1447711951392380165, 6118366145278105860, 4383356545218844403, 16245693987825670584, 2780554830603218012, 12970299634944553151, 3222388605624008866, 15814383424087557059, 15988086447905475558, 16116025969641329513, 6426405161833441255, 3254481667731922028, 6488541859345202975, 10574901139024748597, 3024566360722566355, 16062071326447635275, 12345606174395125886, 6794103055184511112, 11215411239298654461, 16898959837531392298, 11392129473298461016, 8804779203101922496, 18248956894608479019, 3405499931018446142, 17086893422507178606, 15658544032726530242, 8364333522488247864, 3279785515391664592, 13243140800673203277, 5966586998550012975, 16565158092620888628, 12638930544692949903, 1246241189792785842, 15194422135212677813, 12698266719810819587, 1534974055018719502, 12670636876876282922, 15558200550263511669, 3503220298365529701, 7528003967410398907, 17951113451990505790, 11966189487560109058, 9487073780776752004, 16989174121673443471, 11983187886593000791, 14034832459830322267, 7699754122092779654, 11045278550085659092, 15517258337126557177, 11994491770159532604, 12391224518810430854, 16412011954261833814, 6823393608276975560, 8049664586953865101, 105554461905525278, 1108289617734621870, 3107169899130739186, 16009603271400150224, 2287840628984514055, 16506851535775780356, 3856407398241994124, 15057357339963415331, 7421988999323764657, 16263909762531412778, 5520741619830646734, 12658567612226487844, 4150397776403010384, 4506124991939010117, 8337570680160461228, 1773277438796706851, 4411225815945427420, 14662834929794280164, 2744390976482384579, 5016066739309353833, 2446711385473505783, 7207045095118849468, 5059656042334578233, 5000969109599430964, 2861557136012695307, 4840563942385966137]
\ No newline at end of file
-- 
2.43.0

