From f202a8b20839fb9b3228ff1b420b358e04b50cdc Mon Sep 17 00:00:00 2001 From: Mingcong Bai Date: Thu, 8 Aug 2024 11:37:08 +0800 Subject: [PATCH] feat: add loong64 old-world firmware compatibility --- debian/changelog | 6 ++ ...ngarch64-efi-also-install-BOOTLOONGA.patch | 83 +++++++++++++++++++ ...h64-able-to-start-on-legacy-firmware.patch | 48 +++++++++++ debian/patches/series | 3 + 4 files changed, 140 insertions(+) create mode 100644 debian/patches/0001-grub-install-loongarch64-efi-also-install-BOOTLOONGA.patch create mode 100644 debian/patches/0001-loongarch64-able-to-start-on-legacy-firmware.patch diff --git a/debian/changelog b/debian/changelog index fe68933..cdbaac9 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,3 +1,9 @@ +grub2 (2.12-1deepin8) unstable; urgency=medium + + * (loong64) Introduce a patch for old-world firmware compatibility. + + -- Mingcong Bai Thu, 08 Aug 2024 11:34:13 +0800 + grub2 (2.12-1deepin7) unstable; urgency=medium * grub-mkrescue: use upper-case paths for EFI images. diff --git a/debian/patches/0001-grub-install-loongarch64-efi-also-install-BOOTLOONGA.patch b/debian/patches/0001-grub-install-loongarch64-efi-also-install-BOOTLOONGA.patch new file mode 100644 index 0000000..38327cc --- /dev/null +++ b/debian/patches/0001-grub-install-loongarch64-efi-also-install-BOOTLOONGA.patch @@ -0,0 +1,83 @@ +From ce9bc1a3687bba025d2a236cf1f2132d0c760b06 Mon Sep 17 00:00:00 2001 +From: Mingcong Bai +Date: Fri, 9 Aug 2024 17:31:35 +0800 +Subject: [PATCH] grub-install: (loongarch64-efi) also install + BOOTLOONGARCH.EFI + +Some old-world firmware (especially that of the BPI01000 revision) does not +recognise the standardised BOOTLOONGARCH64.EFI and only sees BOOTLOONGARCH.EFI +as the removable image name. Make a copy (since it is FAT/VFAT) to satisfy +both old- and new-world firmware. +--- + util/grub-install.c | 19 ++++++++++++++++++- + 1 file changed, 18 insertions(+), 1 deletion(-) + +diff --git a/util/grub-install.c b/util/grub-install.c +index dfcd269..81fdfb4 100644 +--- a/util/grub-install.c ++++ b/util/grub-install.c +@@ -958,6 +958,8 @@ main (int argc, char *argv[]) + int is_efi = 0; + const char *efi_distributor = NULL; + const char *efi_suffix = NULL, *efi_suffix_upper = NULL; ++ // For LoongArch old-world firmware compatibility. ++ const char *efi_suffix_ow = NULL, *efi_suffix_upper_ow = NULL; + char *efi_file = NULL; + char **grub_devices; + grub_fs_t grub_fs; +@@ -1285,6 +1287,8 @@ main (int argc, char *argv[]) + case GRUB_INSTALL_PLATFORM_LOONGARCH64_EFI: + efi_suffix = "loongarch64"; + efi_suffix_upper = "LOONGARCH64"; ++ efi_suffix_ow = "loongarch"; ++ efi_suffix_upper_ow = "LOONGARCH"; + break; + case GRUB_INSTALL_PLATFORM_RISCV32_EFI: + efi_suffix = "riscv32"; +@@ -2145,6 +2149,7 @@ main (int argc, char *argv[]) + { + char *dst = grub_util_path_concat (2, efidir, efi_file); + char *removable_file = xasprintf ("BOOT%s.EFI", efi_suffix_upper); ++ char *removable_file_ow = xasprintf ("BOOT%s.EFI", efi_suffix_upper_ow); + + if (uefi_secure_boot) + { +@@ -2222,6 +2227,10 @@ main (int argc, char *argv[]) + grub_util_info ("Secure boot: installing shim and image into rm path"); + also_install_removable (shim_signed, base_efidir, removable_file, 1); + ++ if (platform == GRUB_INSTALL_PLATFORM_LOONGARCH64_EFI) { ++ also_install_removable (shim_signed, base_efidir, removable_file_ow, 1); ++ } ++ + also_install_removable (efi_signed, base_efidir, chained_base, 1); + also_install_removable (mok_src, base_efidir, mok_file, 0); + +@@ -2251,6 +2260,10 @@ main (int argc, char *argv[]) + { + grub_util_info ("Secure boot (no shim): installing signed grub binary into rm path"); + also_install_removable (efi_signed, base_efidir, removable_file, 1); ++ ++ if (platform == GRUB_INSTALL_PLATFORM_LOONGARCH64_EFI) { ++ also_install_removable (efi_signed, base_efidir, removable_file_ow, 1); ++ } + } + } + +@@ -2274,8 +2287,12 @@ main (int argc, char *argv[]) + /* No secure boot - just install our newly-generated image */ + grub_util_info ("No Secure Boot: installing core image"); + grub_install_copy_file (imgfile, dst, 1); +- if (force_extra_removable) ++ if (force_extra_removable) { + also_install_removable (imgfile, base_efidir, removable_file, 1); ++ if (platform == GRUB_INSTALL_PLATFORM_LOONGARCH64_EFI) { ++ also_install_removable (imgfile, base_efidir, removable_file_ow, 1); ++ } ++ } + } + + grub_set_install_backup_ponr (); +-- +2.46.0 + diff --git a/debian/patches/0001-loongarch64-able-to-start-on-legacy-firmware.patch b/debian/patches/0001-loongarch64-able-to-start-on-legacy-firmware.patch new file mode 100644 index 0000000..5963429 --- /dev/null +++ b/debian/patches/0001-loongarch64-able-to-start-on-legacy-firmware.patch @@ -0,0 +1,48 @@ +From 6740b5a0d995e41d1407ed844f4f5e358df9a421 Mon Sep 17 00:00:00 2001 +From: Miao Wang +Date: Wed, 17 Jul 2024 09:16:26 +0800 +Subject: [PATCH] loongarch64: able to start on legacy firmware + +On legacy firmware, the DMW is already enabled by the firmware, and the +addresses in all the pointers and the PC register are virtual addresses. +GRUB, however, is location independent, so will not be affected. The +only problem happens at the GRUB_EFI_MAX_USABLE_ADDRESS, which should +be dynamically decided by detecting if DMW is enabled. +--- + include/grub/loongarch64/efi/memory.h | 21 ++++++++++++++++++++- + 1 file changed, 20 insertions(+), 1 deletion(-) + +diff --git a/include/grub/loongarch64/efi/memory.h b/include/grub/loongarch64/efi/memory.h +index d460267be..534ba4b32 100644 +--- a/include/grub/loongarch64/efi/memory.h ++++ b/include/grub/loongarch64/efi/memory.h +@@ -19,6 +19,25 @@ + #ifndef GRUB_MEMORY_CPU_HEADER + #include + +-#define GRUB_EFI_MAX_USABLE_ADDRESS 0xfffffffffffULL ++static inline grub_uint64_t ++grub_efi_max_usable_address(void) ++{ ++ static grub_uint64_t addr; ++ static int addr_valid = 0; ++ ++ if (!addr_valid) ++ { ++ asm volatile ("csrrd %0, 0x181" : "=r" (addr)); ++ if (addr & 0x1) ++ addr |= 0xfffffffffffUL; ++ else ++ addr = 0xfffffffffffUL; ++ addr_valid = 1; ++ } ++ ++ return addr; ++} ++ ++#define GRUB_EFI_MAX_USABLE_ADDRESS (grub_efi_max_usable_address()) + + #endif /* ! GRUB_MEMORY_CPU_HEADER */ +-- +2.46.0 + diff --git a/debian/patches/series b/debian/patches/series index 90f5d77..379e7e0 100644 --- a/debian/patches/series +++ b/debian/patches/series @@ -102,3 +102,6 @@ deepin-Also-install-x86_64-efi-to-grub-efi.patch # # https://lists.gnu.org/archive/html/grub-devel/2024-06/msg00101.html deepin/0001-util-grub-mkrescue-use-capitalised-paths-for-removab.patch +# loong64 old-world firmware compatibility +0001-loongarch64-able-to-start-on-legacy-firmware.patch +0001-grub-install-loongarch64-efi-also-install-BOOTLOONGA.patch