From d870d1cdde710d2c5ec16446c25b9249c86b8f64 Mon Sep 17 00:00:00 2001 From: Jacob Wang Date: Fri, 28 Mar 2025 13:48:28 +0800 Subject: [PATCH 1/8] [CVE]update to grub2-2.02-162 to #IBWZRW update to grub2-2.02-162 for CVE-2025-0624 Project: TC2024080204 Signed-off-by: Jacob Wang --- 0592-misc-Implement-grub_strlcpy.patch | 65 + 0593-fs-ufs-Fix-a-heap-OOB-write.patch | 31 + ...Fix-stack-OOB-write-with-grub_strcpy.patch | 31 + ...itialize-name-in-grub_cpio_find_file.patch | 40 + ...ger-overflow-leads-to-heap-OOB-write.patch | 89 + ...f2fs-Set-a-grub_errno-if-mount-fails.patch | 31 + ...plus-Set-a-grub_errno-if-mount-fails.patch | 35 + ...9660-Set-a-grub_errno-if-mount-fails.patch | 33 + 0600-fs-iso9660-Fix-invalid-free.patch | 50 + 0601-fs-jfs-Fix-OOB-read-in-jfs_getent.patch | 63 + ...ead-caused-by-invalid-dir-slot-index.patch | 64 + ...40-bits-offset-and-address-for-a-dat.patch | 128 + ...ent-signed-unsigned-types-usage-in-r.patch | 85 + ...ut-of-bounds-read-for-inline-extents.patch | 46 + 0606-fs-ntfs-Fix-out-of-bounds-read.patch | 47 + ...-the-end-of-the-MFT-attribute-buffer.patch | 141 + ...helper-function-to-access-attributes.patch | 183 + 0610-fs-xfs-Fix-out-of-bounds-read.patch | 42 + ...g-failing-to-mount-sets-a-grub_errno.patch | 42 + 0612-kern-file-Ensure-file-data-is-set.patch | 32 + ...lement-filesystem-reference-counting.patch | 430 ++ ...-Reference-tracking-for-the-loopback.patch | 103 + 0615-kern-disk-Limit-recursion-depth.patch | 120 + ...tion-Limit-recursion-in-part_iterate.patch | 44 + ...pt-execute-Limit-the-recursion-depth.patch | 55 + ...et_default_ip-and-net_default_mac-va.patch | 29 + ...bles-hooks-when-interface-is-unregis.patch | 85 + ...write-in-grub_net_search_config_file.patch | 80 + ...x-stack-buffer-overflow-in-tftp_open.patch | 114 + ...eg-Do-not-permit-duplicate-SOF0-mark.patch | 33 + ...r-an-integer-overflow-in-grub_dl_ref.patch | 138 + ...r-the-SHF_INFO_LINK-flag-in-grub_dl_.patch | 34 + ...-Missing-check-for-failed-allocation.patch | 34 + 0626-commands-ls-Fix-NULL-dereference.patch | 32 + ...egister-the-check_signatures-hooks-o.patch | 31 + ...ove-variables-hooks-on-module-unload.patch | 37 + ...ove-variables-hooks-on-module-unload.patch | 34 + ...overflow-leads-to-heap-OOB-write-or-.patch | 35 + ...ger-overflow-leads-to-heap-OOB-write.patch | 53 + ...x-an-integer-overflow-when-supplying.patch | 69 + ...ack-overflow-due-to-unlimited-recurs.patch | 83 + ...-Block-the-dump-command-in-lockdown-.patch | 33 + ...isable-memory-reading-in-lockdown-mo.patch | 50 + ...-Disable-memory-reading-in-lockdown-.patch | 37 + 0637-fs-bfs-Disable-under-lockdown.patch | 52 + ...able-many-filesystems-under-lockdown.patch | 391 ++ ...afe-math-macros-to-prevent-overflows.patch | 367 ++ ...rflows-when-allocating-memory-for-ar.patch | 38 + ...turned-pointer-for-allocated-memory-.patch | 134 + ...disk-Call-grub_ieee1275_close-when-g.patch | 31 + ...afe-math-macros-to-prevent-overflows.patch | 353 ++ ...lows-when-allocating-memory-for-arra.patch | 82 + ...lows-when-assigning-returned-values-.patch | 105 + ...afe-math-macros-to-prevent-overflows.patch | 139 + ...verflows-when-allocating-memory-for-.patch | 42 + ...returned-pointer-for-allocated-memor.patch | 88 + ...ng-NULL-check-after-grub_strdup-call.patch | 24 + ...afe-math-macros-to-prevent-overflows.patch | 239 ++ ...flows-when-allocating-memory-for-arr.patch | 45 + ...urned-pointer-for-allocated-memory-i.patch | 31 + ...fs-Check-if-allocated-memory-is-NULL.patch | 32 + ...ix-potential-underflow-and-NULL-dere.patch | 32 + ...unix-getroot-Fix-potential-underflow.patch | 35 + ...e-consistent-overflow-error-messages.patch | 55 + ...ine-GRUB_EHCI_TOGGLE-as-grub_uint32_.patch | 30 + ...safe-math-to-avoid-an-integer-overfl.patch | 41 + ...dd-sanity-check-after-grub_strtoul-c.patch | 45 + ...sanity-check-after-grub_strtoul-call.patch | 57 + ...nux-Cast-left-shift-to-grub_uint32_t.patch | 30 + ...bsd-Use-safe-math-to-avoid-underflow.patch | 56 + ...-types-Make-bool-generally-available.patch | 78 + 0664-Remove-exttra-bool-definitions.patch | 45 + ...s-found-while-fuzzing-the-XFS-filesy.patch | 106 + ...ix-grub_malloc-removed-after-added-s.patch | 32 + 1001-bls-make-list.patch | 119 - ...-support-and-update-interface-to-v40.patch | 3444 ----------------- ...the-password-getter-and-additional-a.patch | 173 - ...odisk-add-OS-provided-secret-support.patch | 206 - ...retrieving-the-EFI-secret-for-crypto.patch | 217 -- ...ate-patches-in-sync-with-loongarch64.patch | 2815 -------------- 20-grub.install | 2 +- grub.macros | 14 +- grub.patches | 82 +- grub2.spec | 45 +- 84 files changed, 5998 insertions(+), 7025 deletions(-) create mode 100644 0592-misc-Implement-grub_strlcpy.patch create mode 100644 0593-fs-ufs-Fix-a-heap-OOB-write.patch create mode 100644 0594-fs-hfs-Fix-stack-OOB-write-with-grub_strcpy.patch create mode 100644 0595-fs-tar-Initialize-name-in-grub_cpio_find_file.patch create mode 100644 0596-fs-tar-Integer-overflow-leads-to-heap-OOB-write.patch create mode 100644 0597-fs-f2fs-Set-a-grub_errno-if-mount-fails.patch create mode 100644 0598-fs-hfsplus-Set-a-grub_errno-if-mount-fails.patch create mode 100644 0599-fs-iso9660-Set-a-grub_errno-if-mount-fails.patch create mode 100644 0600-fs-iso9660-Fix-invalid-free.patch create mode 100644 0601-fs-jfs-Fix-OOB-read-in-jfs_getent.patch create mode 100644 0602-fs-jfs-Fix-OOB-read-caused-by-invalid-dir-slot-index.patch create mode 100644 0603-fs-jfs-Use-full-40-bits-offset-and-address-for-a-dat.patch create mode 100644 0604-fs-jfs-Inconsistent-signed-unsigned-types-usage-in-r.patch create mode 100644 0605-fs-ext2-Fix-out-of-bounds-read-for-inline-extents.patch create mode 100644 0606-fs-ntfs-Fix-out-of-bounds-read.patch create mode 100644 0607-fs-ntfs-Track-the-end-of-the-MFT-attribute-buffer.patch create mode 100644 0608-fs-ntfs-Use-a-helper-function-to-access-attributes.patch create mode 100644 0610-fs-xfs-Fix-out-of-bounds-read.patch create mode 100644 0611-fs-xfs-Ensuring-failing-to-mount-sets-a-grub_errno.patch create mode 100644 0612-kern-file-Ensure-file-data-is-set.patch create mode 100644 0613-kern-file-Implement-filesystem-reference-counting.patch create mode 100644 0614-disk-loopback-Reference-tracking-for-the-loopback.patch create mode 100644 0615-kern-disk-Limit-recursion-depth.patch create mode 100644 0616-kern-partition-Limit-recursion-in-part_iterate.patch create mode 100644 0617-script-execute-Limit-the-recursion-depth.patch create mode 100644 0618-net-Unregister-net_default_ip-and-net_default_mac-va.patch create mode 100644 0619-net-Remove-variables-hooks-when-interface-is-unregis.patch create mode 100644 0620-net-Fix-OOB-write-in-grub_net_search_config_file.patch create mode 100644 0621-net-tftp-Fix-stack-buffer-overflow-in-tftp_open.patch create mode 100644 0622-video-readers-jpeg-Do-not-permit-duplicate-SOF0-mark.patch create mode 100644 0623-kern-dl-Fix-for-an-integer-overflow-in-grub_dl_ref.patch create mode 100644 0624-kern-dl-Check-for-the-SHF_INFO_LINK-flag-in-grub_dl_.patch create mode 100644 0625-commands-extcmd-Missing-check-for-failed-allocation.patch create mode 100644 0626-commands-ls-Fix-NULL-dereference.patch create mode 100644 0627-commands-pgp-Unregister-the-check_signatures-hooks-o.patch create mode 100644 0628-normal-Remove-variables-hooks-on-module-unload.patch create mode 100644 0629-gettext-Remove-variables-hooks-on-module-unload.patch create mode 100644 0630-gettext-Integer-overflow-leads-to-heap-OOB-write-or-.patch create mode 100644 0631-gettext-Integer-overflow-leads-to-heap-OOB-write.patch create mode 100644 0632-commands-read-Fix-an-integer-overflow-when-supplying.patch create mode 100644 0633-commands-test-Stack-overflow-due-to-unlimited-recurs.patch create mode 100644 0634-commands-minicmd-Block-the-dump-command-in-lockdown-.patch create mode 100644 0635-commands-memrw-Disable-memory-reading-in-lockdown-mo.patch create mode 100644 0636-commands-hexdump-Disable-memory-reading-in-lockdown-.patch create mode 100644 0637-fs-bfs-Disable-under-lockdown.patch create mode 100644 0638-fs-Disable-many-filesystems-under-lockdown.patch create mode 100644 0639-disk-Use-safe-math-macros-to-prevent-overflows.patch create mode 100644 0640-disk-Prevent-overflows-when-allocating-memory-for-ar.patch create mode 100644 0641-disk-Check-if-returned-pointer-for-allocated-memory-.patch create mode 100644 0642-disk-ieee1275-ofdisk-Call-grub_ieee1275_close-when-g.patch create mode 100644 0643-fs-Use-safe-math-macros-to-prevent-overflows.patch create mode 100644 0644-fs-Prevent-overflows-when-allocating-memory-for-arra.patch create mode 100644 0645-fs-Prevent-overflows-when-assigning-returned-values-.patch create mode 100644 0646-fs-zfs-Use-safe-math-macros-to-prevent-overflows.patch create mode 100644 0647-fs-zfs-Prevent-overflows-when-allocating-memory-for-.patch create mode 100644 0648-fs-zfs-Check-if-returned-pointer-for-allocated-memor.patch create mode 100644 0649-fs-zfs-Add-missing-NULL-check-after-grub_strdup-call.patch create mode 100644 0650-net-Use-safe-math-macros-to-prevent-overflows.patch create mode 100644 0651-net-Prevent-overflows-when-allocating-memory-for-arr.patch create mode 100644 0652-net-Check-if-returned-pointer-for-allocated-memory-i.patch create mode 100644 0653-fs-sfs-Check-if-allocated-memory-is-NULL.patch create mode 100644 0654-script-execute-Fix-potential-underflow-and-NULL-dere.patch create mode 100644 0655-osdep-unix-getroot-Fix-potential-underflow.patch create mode 100644 0656-misc-Ensure-consistent-overflow-error-messages.patch create mode 100644 0657-bus-usb-ehci-Define-GRUB_EHCI_TOGGLE-as-grub_uint32_.patch create mode 100644 0658-normal-menu-Use-safe-math-to-avoid-an-integer-overfl.patch create mode 100644 0659-kern-partition-Add-sanity-check-after-grub_strtoul-c.patch create mode 100644 0660-kern-misc-Add-sanity-check-after-grub_strtoul-call.patch create mode 100644 0661-loader-i386-linux-Cast-left-shift-to-grub_uint32_t.patch create mode 100644 0662-loader-i386-bsd-Use-safe-math-to-avoid-underflow.patch create mode 100644 0663-types-Make-bool-generally-available.patch create mode 100644 0664-Remove-exttra-bool-definitions.patch create mode 100644 0665-fs-xfs-Fix-issues-found-while-fuzzing-the-XFS-filesy.patch create mode 100644 0666-ieee1275-ofnet-Fix-grub_malloc-removed-after-added-s.patch delete mode 100644 1001-bls-make-list.patch delete mode 100644 1002-Add-LoongArch64-support-and-update-interface-to-v40.patch delete mode 100644 1003-cryptodisk-make-the-password-getter-and-additional-a.patch delete mode 100644 1004-cryptodisk-add-OS-provided-secret-support.patch delete mode 100644 1005-efi-Add-API-for-retrieving-the-EFI-secret-for-crypto.patch delete mode 100644 1006-Update-patches-in-sync-with-loongarch64.patch diff --git a/0592-misc-Implement-grub_strlcpy.patch b/0592-misc-Implement-grub_strlcpy.patch new file mode 100644 index 0000000..5a216fd --- /dev/null +++ b/0592-misc-Implement-grub_strlcpy.patch @@ -0,0 +1,65 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: B Horn +Date: Sat, 15 Jun 2024 02:33:08 +0100 +Subject: [PATCH] misc: Implement grub_strlcpy() + +grub_strlcpy() acts the same way as strlcpy() does on most *NIX, +returning the length of src and ensuring dest is always NUL +terminated except when size is 0. + +Signed-off-by: B Horn +Reviewed-by: Daniel Kiper +--- + include/grub/misc.h | 39 +++++++++++++++++++++++++++++++++++++++ + 1 file changed, 39 insertions(+) + +diff --git a/include/grub/misc.h b/include/grub/misc.h +index 1b722c818..88fc2a6b0 100644 +--- a/include/grub/misc.h ++++ b/include/grub/misc.h +@@ -72,6 +72,45 @@ grub_stpcpy (char *dest, const char *src) + return d - 1; + } + ++static inline grub_size_t ++grub_strlcpy (char *dest, const char *src, grub_size_t size) ++{ ++ char *d = dest; ++ grub_size_t res = 0; ++ /* ++ * We do not subtract one from size here to avoid dealing with underflowing ++ * the value, which is why to_copy is always checked to be greater than one ++ * throughout this function. ++ */ ++ grub_size_t to_copy = size; ++ ++ /* Copy size - 1 bytes to dest. */ ++ if (to_copy > 1) ++ while ((*d++ = *src++) != '\0' && ++res && --to_copy > 1) ++ ; ++ ++ /* ++ * NUL terminate if size != 0. The previous step may have copied a NUL byte ++ * if it reached the end of the string, but we know dest[size - 1] must always ++ * be a NUL byte. ++ */ ++ if (size != 0) ++ dest[size - 1] = '\0'; ++ ++ /* If there is still space in dest, but are here, we reached the end of src. */ ++ if (to_copy > 1) ++ return res; ++ ++ /* ++ * If we haven't reached the end of the string, iterate through to determine ++ * the strings total length. ++ */ ++ while (*src++ != '\0' && ++res) ++ ; ++ ++ return res; ++} ++ + /* XXX: If grub_memmove is too slow, we must implement grub_memcpy. */ + static inline void * + grub_memcpy (void *dest, const void *src, grub_size_t n) diff --git a/0593-fs-ufs-Fix-a-heap-OOB-write.patch b/0593-fs-ufs-Fix-a-heap-OOB-write.patch new file mode 100644 index 0000000..1785034 --- /dev/null +++ b/0593-fs-ufs-Fix-a-heap-OOB-write.patch @@ -0,0 +1,31 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: B Horn +Date: Sun, 12 May 2024 02:03:33 +0100 +Subject: [PATCH] fs/ufs: Fix a heap OOB write + +grub_strcpy() was used to copy a symlink name from the filesystem +image to a heap allocated buffer. This led to a OOB write to adjacent +heap allocations. Fix by using grub_strlcpy(). + +Fixes: CVE-2024-45781 + +Reported-by: B Horn +Signed-off-by: B Horn +Reviewed-by: Daniel Kiper +--- + grub-core/fs/ufs.c | 2 +- + 1 file changed, 1 insertion(+), 1 deletion(-) + +diff --git a/grub-core/fs/ufs.c b/grub-core/fs/ufs.c +index 293f027aa..0c2004fd7 100644 +--- a/grub-core/fs/ufs.c ++++ b/grub-core/fs/ufs.c +@@ -463,7 +463,7 @@ grub_ufs_lookup_symlink (struct grub_ufs_data *data, int ino) + /* Check against zero is paylindromic, no need to swap. */ + if (data->inode.nblocks == 0 + && INODE_SIZE (data) <= sizeof (data->inode.symlink)) +- grub_strcpy (symlink, (char *) data->inode.symlink); ++ grub_strlcpy (symlink, (char *) data->inode.symlink, sz); + else + { + if (grub_ufs_read_file (data, 0, 0, 0, sz, symlink) < 0) diff --git a/0594-fs-hfs-Fix-stack-OOB-write-with-grub_strcpy.patch b/0594-fs-hfs-Fix-stack-OOB-write-with-grub_strcpy.patch new file mode 100644 index 0000000..581f754 --- /dev/null +++ b/0594-fs-hfs-Fix-stack-OOB-write-with-grub_strcpy.patch @@ -0,0 +1,31 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: B Horn +Date: Sun, 12 May 2024 02:48:33 +0100 +Subject: [PATCH] fs/hfs: Fix stack OOB write with grub_strcpy() + +Replaced with grub_strlcpy(). + +Fixes: CVE-2024-45782 +Fixes: CVE-2024-56737 +Fixes: https://savannah.gnu.org/bugs/?66599 + +Reported-by: B Horn +Signed-off-by: B Horn +Reviewed-by: Daniel Kiper +--- + grub-core/fs/hfs.c | 2 +- + 1 file changed, 1 insertion(+), 1 deletion(-) + +diff --git a/grub-core/fs/hfs.c b/grub-core/fs/hfs.c +index 49d1831c8..44859fe43 100644 +--- a/grub-core/fs/hfs.c ++++ b/grub-core/fs/hfs.c +@@ -379,7 +379,7 @@ grub_hfs_mount (grub_disk_t disk) + volume name. */ + key.parent_dir = grub_cpu_to_be32_compile_time (1); + key.strlen = data->sblock.volname[0]; +- grub_strcpy ((char *) key.str, (char *) (data->sblock.volname + 1)); ++ grub_strlcpy ((char *) key.str, (char *) (data->sblock.volname + 1), sizeof (key.str)); + + if (grub_hfs_find_node (data, (char *) &key, data->cat_root, + 0, (char *) &dir, sizeof (dir)) == 0) diff --git a/0595-fs-tar-Initialize-name-in-grub_cpio_find_file.patch b/0595-fs-tar-Initialize-name-in-grub_cpio_find_file.patch new file mode 100644 index 0000000..abaa43c --- /dev/null +++ b/0595-fs-tar-Initialize-name-in-grub_cpio_find_file.patch @@ -0,0 +1,40 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: B Horn +Date: Sun, 12 May 2024 02:47:54 +0100 +Subject: [PATCH] fs/tar: Initialize name in grub_cpio_find_file() + +It was possible to iterate through grub_cpio_find_file() without +allocating name and not setting mode to GRUB_ARCHELP_ATTR_END, which +would cause the uninitialized value for name to be used as an argument +for canonicalize() in grub_archelp_dir(). + +Reported-by: B Horn +Signed-off-by: B Horn +Reviewed-by: Daniel Kiper +--- + grub-core/fs/tar.c | 5 +++++ + 1 file changed, 5 insertions(+) + +diff --git a/grub-core/fs/tar.c b/grub-core/fs/tar.c +index 4864451e1..f1e6571c2 100644 +--- a/grub-core/fs/tar.c ++++ b/grub-core/fs/tar.c +@@ -78,6 +78,7 @@ grub_cpio_find_file (struct grub_archelp_data *data, char **name, + int reread = 0, have_longname = 0, have_longlink = 0; + + data->hofs = data->next_hofs; ++ *name = NULL; + + for (reread = 0; reread < 3; reread++) + { +@@ -202,6 +203,10 @@ grub_cpio_find_file (struct grub_archelp_data *data, char **name, + } + return GRUB_ERR_NONE; + } ++ ++ if (*name == NULL) ++ return grub_error (GRUB_ERR_BAD_FS, "invalid tar archive"); ++ + return GRUB_ERR_NONE; + } + diff --git a/0596-fs-tar-Integer-overflow-leads-to-heap-OOB-write.patch b/0596-fs-tar-Integer-overflow-leads-to-heap-OOB-write.patch new file mode 100644 index 0000000..02edb94 --- /dev/null +++ b/0596-fs-tar-Integer-overflow-leads-to-heap-OOB-write.patch @@ -0,0 +1,89 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Lidong Chen +Date: Fri, 22 Nov 2024 06:27:58 +0000 +Subject: [PATCH] fs/tar: Integer overflow leads to heap OOB write + +Both namesize and linksize are derived from hd.size, a 12-digit octal +number parsed by read_number(). Later direct arithmetic calculation like +"namesize + 1" and "linksize + 1" may exceed the maximum value of +grub_size_t leading to heap OOB write. This patch fixes the issue by +using grub_add() and checking for an overflow. + +Fixes: CVE-2024-45780 + +Reported-by: Nils Langius +Signed-off-by: Lidong Chen +Reviewed-by: Daniel Kiper +Reviewed-by: Alec Brown +--- + grub-core/fs/tar.c | 23 ++++++++++++++++++----- + 1 file changed, 18 insertions(+), 5 deletions(-) + +diff --git a/grub-core/fs/tar.c b/grub-core/fs/tar.c +index f1e6571c2..28baa5845 100644 +--- a/grub-core/fs/tar.c ++++ b/grub-core/fs/tar.c +@@ -25,6 +25,7 @@ + #include + #include + #include ++#include + + GRUB_MOD_LICENSE ("GPLv3+"); + +@@ -76,6 +77,7 @@ grub_cpio_find_file (struct grub_archelp_data *data, char **name, + { + struct head hd; + int reread = 0, have_longname = 0, have_longlink = 0; ++ grub_size_t sz; + + data->hofs = data->next_hofs; + *name = NULL; +@@ -98,7 +100,11 @@ grub_cpio_find_file (struct grub_archelp_data *data, char **name, + { + grub_err_t err; + grub_size_t namesize = read_number (hd.size, sizeof (hd.size)); +- *name = grub_malloc (namesize + 1); ++ ++ if (grub_add (namesize, 1, &sz)) ++ return grub_error (GRUB_ERR_BAD_FS, N_("name size overflow")); ++ ++ *name = grub_malloc (sz); + if (*name == NULL) + return grub_errno; + err = grub_disk_read (data->disk, 0, +@@ -118,15 +124,19 @@ grub_cpio_find_file (struct grub_archelp_data *data, char **name, + { + grub_err_t err; + grub_size_t linksize = read_number (hd.size, sizeof (hd.size)); +- if (data->linkname_alloc < linksize + 1) ++ ++ if (grub_add (linksize, 1, &sz)) ++ return grub_error (GRUB_ERR_BAD_FS, N_("link size overflow")); ++ ++ if (data->linkname_alloc < sz) + { + char *n; +- n = grub_calloc (2, linksize + 1); ++ n = grub_calloc (2, sz); + if (!n) + return grub_errno; + grub_free (data->linkname); + data->linkname = n; +- data->linkname_alloc = 2 * (linksize + 1); ++ data->linkname_alloc = 2 * (sz); + } + + err = grub_disk_read (data->disk, 0, +@@ -149,7 +159,10 @@ grub_cpio_find_file (struct grub_archelp_data *data, char **name, + while (extra_size < sizeof (hd.prefix) + && hd.prefix[extra_size]) + extra_size++; +- *name = grub_malloc (sizeof (hd.name) + extra_size + 2); ++ ++ if (grub_add (sizeof (hd.name) + 2, extra_size, &sz)) ++ return grub_error (GRUB_ERR_BAD_FS, N_("long name size overflow")); ++ *name = grub_malloc (sz); + if (*name == NULL) + return grub_errno; + if (hd.prefix[0]) diff --git a/0597-fs-f2fs-Set-a-grub_errno-if-mount-fails.patch b/0597-fs-f2fs-Set-a-grub_errno-if-mount-fails.patch new file mode 100644 index 0000000..1388f11 --- /dev/null +++ b/0597-fs-f2fs-Set-a-grub_errno-if-mount-fails.patch @@ -0,0 +1,31 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: B Horn +Date: Sun, 12 May 2024 06:15:03 +0100 +Subject: [PATCH] fs/f2fs: Set a grub_errno if mount fails + +It was previously possible for grub_errno to not be set when +grub_f2fs_mount() failed if nat_bitmap_ptr() returned NULL. + +This issue is solved by ensuring a grub_errno is set in the fail case. + +Reported-by: B Horn +Signed-off-by: B Horn +Reviewed-by: Daniel Kiper +--- + grub-core/fs/f2fs.c | 3 +++ + 1 file changed, 3 insertions(+) + +diff --git a/grub-core/fs/f2fs.c b/grub-core/fs/f2fs.c +index 07ea34196..706e595d3 100644 +--- a/grub-core/fs/f2fs.c ++++ b/grub-core/fs/f2fs.c +@@ -871,6 +871,9 @@ grub_f2fs_mount (grub_disk_t disk) + return data; + + fail: ++ if (grub_errno == GRUB_ERR_NONE) ++ grub_error (GRUB_ERR_BAD_FS, "not a F2FS filesystem"); ++ + grub_free (data); + + return NULL; diff --git a/0598-fs-hfsplus-Set-a-grub_errno-if-mount-fails.patch b/0598-fs-hfsplus-Set-a-grub_errno-if-mount-fails.patch new file mode 100644 index 0000000..7f8c341 --- /dev/null +++ b/0598-fs-hfsplus-Set-a-grub_errno-if-mount-fails.patch @@ -0,0 +1,35 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: B Horn +Date: Sun, 12 May 2024 06:22:51 +0100 +Subject: [PATCH] fs/hfsplus: Set a grub_errno if mount fails + +It was possible for mount to fail but not set grub_errno. This led to +a possible double decrement of the module reference count if the NULL +page was mapped. + +Fixing in general as a similar bug was fixed in commit 61b13c187 +(fs/hfsplus: Set grub_errno to prevent NULL pointer access) and there +are likely more variants around. + +Fixes: CVE-2024-45783 + +Reported-by: B Horn +Signed-off-by: B Horn +Reviewed-by: Daniel Kiper +--- + grub-core/fs/hfsplus.c | 2 +- + 1 file changed, 1 insertion(+), 1 deletion(-) + +diff --git a/grub-core/fs/hfsplus.c b/grub-core/fs/hfsplus.c +index 8c0c80473..2edb1649d 100644 +--- a/grub-core/fs/hfsplus.c ++++ b/grub-core/fs/hfsplus.c +@@ -392,7 +392,7 @@ grub_hfsplus_mount (grub_disk_t disk) + + fail: + +- if (grub_errno == GRUB_ERR_OUT_OF_RANGE) ++ if (grub_errno == GRUB_ERR_OUT_OF_RANGE || grub_errno == GRUB_ERR_NONE) + grub_error (GRUB_ERR_BAD_FS, "not a HFS+ filesystem"); + + grub_free (data); diff --git a/0599-fs-iso9660-Set-a-grub_errno-if-mount-fails.patch b/0599-fs-iso9660-Set-a-grub_errno-if-mount-fails.patch new file mode 100644 index 0000000..a750c0a --- /dev/null +++ b/0599-fs-iso9660-Set-a-grub_errno-if-mount-fails.patch @@ -0,0 +1,33 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: B Horn +Date: Sun, 12 May 2024 06:37:08 +0100 +Subject: [PATCH] fs/iso9660: Set a grub_errno if mount fails + +It was possible for a grub_errno to not be set if mount of an ISO 9660 +filesystem failed when set_rockridge() returned 0. + +This isn't known to be exploitable as the other filesystems due to +filesystem helper checking the requested file type. Though fixing +as a precaution. + +Reported-by: B Horn +Signed-off-by: B Horn +Reviewed-by: Daniel Kiper +--- + grub-core/fs/iso9660.c | 3 +++ + 1 file changed, 3 insertions(+) + +diff --git a/grub-core/fs/iso9660.c b/grub-core/fs/iso9660.c +index a4403e29d..736d56770 100644 +--- a/grub-core/fs/iso9660.c ++++ b/grub-core/fs/iso9660.c +@@ -491,6 +491,9 @@ grub_iso9660_mount (grub_disk_t disk) + return data; + + fail: ++ if (grub_errno == GRUB_ERR_NONE) ++ grub_error (GRUB_ERR_BAD_FS, "not a ISO9660 filesystem"); ++ + grub_free (data); + return 0; + } diff --git a/0600-fs-iso9660-Fix-invalid-free.patch b/0600-fs-iso9660-Fix-invalid-free.patch new file mode 100644 index 0000000..fd223b9 --- /dev/null +++ b/0600-fs-iso9660-Fix-invalid-free.patch @@ -0,0 +1,50 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Michael Chang +Date: Fri, 31 May 2024 15:14:42 +0800 +Subject: [PATCH] fs/iso9660: Fix invalid free + +The ctx->filename can point to either a string literal or a dynamically +allocated string. The ctx->filename_alloc field is used to indicate the +type of allocation. + +An issue has been identified where ctx->filename is reassigned to +a string literal in susp_iterate_dir() but ctx->filename_alloc is not +correctly handled. This oversight causes a memory leak and an invalid +free operation later. + +The fix involves checking ctx->filename_alloc, freeing the allocated +string if necessary and clearing ctx->filename_alloc for string literals. + +Reported-by: Daniel Axtens +Signed-off-by: Michael Chang +Reviewed-by: Daniel Kiper +--- + grub-core/fs/iso9660.c | 14 ++++++++++++-- + 1 file changed, 12 insertions(+), 2 deletions(-) + +diff --git a/grub-core/fs/iso9660.c b/grub-core/fs/iso9660.c +index 736d56770..cf17702e2 100644 +--- a/grub-core/fs/iso9660.c ++++ b/grub-core/fs/iso9660.c +@@ -568,9 +568,19 @@ susp_iterate_dir (struct grub_iso9660_susp_entry *entry, + filename type is stored. */ + /* FIXME: Fix this slightly improper cast. */ + if (entry->data[0] & GRUB_ISO9660_RR_DOT) +- ctx->filename = (char *) "."; ++ { ++ if (ctx->filename_alloc) ++ grub_free (ctx->filename); ++ ctx->filename_alloc = 0; ++ ctx->filename = (char *) "."; ++ } + else if (entry->data[0] & GRUB_ISO9660_RR_DOTDOT) +- ctx->filename = (char *) ".."; ++ { ++ if (ctx->filename_alloc) ++ grub_free (ctx->filename); ++ ctx->filename_alloc = 0; ++ ctx->filename = (char *) ".."; ++ } + else if (entry->len >= 5) + { + grub_size_t off = 0, csize = 1; diff --git a/0601-fs-jfs-Fix-OOB-read-in-jfs_getent.patch b/0601-fs-jfs-Fix-OOB-read-in-jfs_getent.patch new file mode 100644 index 0000000..f19cf44 --- /dev/null +++ b/0601-fs-jfs-Fix-OOB-read-in-jfs_getent.patch @@ -0,0 +1,63 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Lidong Chen +Date: Fri, 22 Nov 2024 06:27:59 +0000 +Subject: [PATCH] fs/jfs: Fix OOB read in jfs_getent() + +The JFS fuzzing revealed an OOB read in grub_jfs_getent(). The crash +was caused by an invalid leaf nodes count, diro->dirpage->header.count, +which was larger than the maximum number of leaf nodes allowed in an +inode. This fix is to ensure that the leaf nodes count is validated in +grub_jfs_opendir() before calling grub_jfs_getent(). + +On the occasion replace existing raw numbers with newly defined constant. + +Signed-off-by: Lidong Chen +Reviewed-by: Daniel Kiper +Reviewed-by: Alec Brown +--- + grub-core/fs/jfs.c | 17 +++++++++++++++-- + 1 file changed, 15 insertions(+), 2 deletions(-) + +diff --git a/grub-core/fs/jfs.c b/grub-core/fs/jfs.c +index 20d966abf..d4207eb64 100644 +--- a/grub-core/fs/jfs.c ++++ b/grub-core/fs/jfs.c +@@ -41,6 +41,12 @@ GRUB_MOD_LICENSE ("GPLv3+"); + + #define GRUB_JFS_TREE_LEAF 2 + ++/* ++ * Define max entries stored in-line in an inode. ++ * https://jfs.sourceforge.net/project/pub/jfslayout.pdf ++ */ ++#define GRUB_JFS_INODE_INLINE_ENTRIES 8 ++ + struct grub_jfs_sblock + { + /* The magic for JFS. It should contain the string "JFS1". */ +@@ -203,9 +209,9 @@ struct grub_jfs_inode + grub_uint8_t freecnt; + grub_uint8_t freelist; + grub_uint32_t idotdot; +- grub_uint8_t sorted[8]; ++ grub_uint8_t sorted[GRUB_JFS_INODE_INLINE_ENTRIES]; + } header; +- struct grub_jfs_leaf_dirent dirents[8]; ++ struct grub_jfs_leaf_dirent dirents[GRUB_JFS_INODE_INLINE_ENTRIES]; + } GRUB_PACKED dir; + /* Fast symlink. */ + struct +@@ -453,6 +459,13 @@ grub_jfs_opendir (struct grub_jfs_data *data, struct grub_jfs_inode *inode) + /* Check if the entire tree is contained within the inode. */ + if (inode->file.tree.flags & GRUB_JFS_TREE_LEAF) + { ++ if (inode->dir.header.count > GRUB_JFS_INODE_INLINE_ENTRIES) ++ { ++ grub_free (diro); ++ grub_error (GRUB_ERR_BAD_FS, N_("invalid JFS inode")); ++ return 0; ++ } ++ + diro->leaf = inode->dir.dirents; + diro->next_leaf = (struct grub_jfs_leaf_next_dirent *) de; + diro->sorted = inode->dir.header.sorted; diff --git a/0602-fs-jfs-Fix-OOB-read-caused-by-invalid-dir-slot-index.patch b/0602-fs-jfs-Fix-OOB-read-caused-by-invalid-dir-slot-index.patch new file mode 100644 index 0000000..0ba88eb --- /dev/null +++ b/0602-fs-jfs-Fix-OOB-read-caused-by-invalid-dir-slot-index.patch @@ -0,0 +1,64 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Lidong Chen +Date: Fri, 22 Nov 2024 06:28:00 +0000 +Subject: [PATCH] fs/jfs: Fix OOB read caused by invalid dir slot index + +While fuzz testing JFS with ASAN enabled an OOB read was detected in +grub_jfs_opendir(). The issue occurred due to an invalid directory slot +index in the first entry of the sorted directory slot array in the inode +directory header. The fix ensures the slot index is validated before +accessing it. Given that an internal or a leaf node in a directory B+ +tree is a 4 KiB in size and each directory slot is always 32 bytes, the +max number of slots in a node is 128. The validation ensures that the +slot index doesn't exceed this limit. + +[1] https://jfs.sourceforge.net/project/pub/jfslayout.pdf + + JFS will allocate 4K of disk space for an internal node of the B+ tree. + An internal node looks the same as a leaf node. + - page 10 + + Fixed number of Directory Slots depending on the size of the node. These are + the slots to be used for storing the directory slot array and the directory + entries or router entries. A directory slot is always 32 bytes. + ... + A Directory Slot Array which is a sorted array of indices to the directory + slots that are currently in use. + ... + An internal or a leaf node in the directory B+ tree is a 4K page. + - page 25 + +Signed-off-by: Lidong Chen +Reviewed-by: Daniel Kiper +Reviewed-by: Alec Brown +--- + grub-core/fs/jfs.c | 9 +++++++++ + 1 file changed, 9 insertions(+) + +diff --git a/grub-core/fs/jfs.c b/grub-core/fs/jfs.c +index d4207eb64..60dd62fe2 100644 +--- a/grub-core/fs/jfs.c ++++ b/grub-core/fs/jfs.c +@@ -46,6 +46,7 @@ GRUB_MOD_LICENSE ("GPLv3+"); + * https://jfs.sourceforge.net/project/pub/jfslayout.pdf + */ + #define GRUB_JFS_INODE_INLINE_ENTRIES 8 ++#define GRUB_JFS_DIR_MAX_SLOTS 128 + + struct grub_jfs_sblock + { +@@ -481,6 +482,14 @@ grub_jfs_opendir (struct grub_jfs_data *data, struct grub_jfs_inode *inode) + return 0; + } + ++ if (inode->dir.header.sorted[0] >= GRUB_JFS_DIR_MAX_SLOTS) ++ { ++ grub_error (GRUB_ERR_BAD_FS, N_("invalid directory slot index")); ++ grub_free (diro->dirpage); ++ grub_free (diro); ++ return 0; ++ } ++ + blk = grub_le_to_cpu32 (de[inode->dir.header.sorted[0]].ex.blk2); + blk <<= (grub_le_to_cpu16 (data->sblock.log2_blksz) - GRUB_DISK_SECTOR_BITS); + diff --git a/0603-fs-jfs-Use-full-40-bits-offset-and-address-for-a-dat.patch b/0603-fs-jfs-Use-full-40-bits-offset-and-address-for-a-dat.patch new file mode 100644 index 0000000..b99b7bf --- /dev/null +++ b/0603-fs-jfs-Use-full-40-bits-offset-and-address-for-a-dat.patch @@ -0,0 +1,128 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Lidong Chen +Date: Mon, 16 Dec 2024 20:22:39 +0000 +Subject: [PATCH] fs/jfs: Use full 40 bits offset and address for a data extent + +An extent's logical offset and address are represented as a 40-bit value +split into two parts: the most significant 8 bits and the least +significant 32 bits. Currently the JFS code uses only the least +significant 32 bits value for offsets and addresses assuming the data +size will never exceed the 32-bit range. This approach ignores the most +significant 8 bits potentially leading to incorrect offsets and +addresses for larger values. The patch fixes it by incorporating the +most significant 8 bits into the calculation to get the full 40-bits +value for offsets and addresses. + +https://jfs.sourceforge.net/project/pub/jfslayout.pdf + + "off1,off2 is a 40-bit field, containing the logical offset of the first + block in the extent. + ... + addr1,addr2 is a 40-bit field, containing the address of the extent." + +Signed-off-by: Lidong Chen +Reviewed-by: Alec Brown +Reviewed-by: Ross Philipson +Reviewed-by: Daniel Kiper +--- + grub-core/fs/jfs.c | 41 +++++++++++++++++++++++++++++------------ + 1 file changed, 29 insertions(+), 12 deletions(-) + +diff --git a/grub-core/fs/jfs.c b/grub-core/fs/jfs.c +index 60dd62fe2..8a908f00d 100644 +--- a/grub-core/fs/jfs.c ++++ b/grub-core/fs/jfs.c +@@ -265,6 +265,20 @@ static grub_dl_t my_mod; + + static grub_err_t grub_jfs_lookup_symlink (struct grub_jfs_data *data, grub_uint32_t ino); + ++/* ++ * An extent's offset, physical and logical, is represented as a 40-bit value. ++ * This 40-bit value is split into two parts: ++ * - offset1: the most signficant 8 bits of the offset, ++ * - offset2: the least significant 32 bits of the offset. ++ * ++ * This function calculates and returns the 64-bit offset of an extent. ++ */ ++static grub_uint64_t ++get_ext_offset (grub_uint8_t offset1, grub_uint32_t offset2) ++{ ++ return (((grub_uint64_t) offset1 << 32) | grub_le_to_cpu32 (offset2)); ++} ++ + static grub_int64_t + getblk (struct grub_jfs_treehead *treehead, + struct grub_jfs_tree_extent *extents, +@@ -274,22 +288,25 @@ getblk (struct grub_jfs_treehead *treehead, + { + int found = -1; + int i; ++ grub_uint64_t ext_offset, ext_blk; + + for (i = 0; i < grub_le_to_cpu16 (treehead->count) - 2 && + i < max_extents; i++) + { ++ ext_offset = get_ext_offset (extents[i].offset1, extents[i].offset2); ++ ext_blk = get_ext_offset (extents[i].extent.blk1, extents[i].extent.blk2); ++ + if (treehead->flags & GRUB_JFS_TREE_LEAF) + { + /* Read the leafnode. */ +- if (grub_le_to_cpu32 (extents[i].offset2) <= blk ++ if (ext_offset <= blk + && ((grub_le_to_cpu16 (extents[i].extent.length)) + + (extents[i].extent.length2 << 16) +- + grub_le_to_cpu32 (extents[i].offset2)) > blk) +- return (blk - grub_le_to_cpu32 (extents[i].offset2) +- + grub_le_to_cpu32 (extents[i].extent.blk2)); ++ + ext_offset) > blk) ++ return (blk - ext_offset + ext_blk); + } + else +- if (blk >= grub_le_to_cpu32 (extents[i].offset2)) ++ if (blk >= ext_offset) + found = i; + } + +@@ -307,10 +324,9 @@ getblk (struct grub_jfs_treehead *treehead, + return -1; + + if (!grub_disk_read (data->disk, +- ((grub_disk_addr_t) grub_le_to_cpu32 (extents[found].extent.blk2)) +- << (grub_le_to_cpu16 (data->sblock.log2_blksz) +- - GRUB_DISK_SECTOR_BITS), 0, +- sizeof (*tree), (char *) tree)) ++ (grub_disk_addr_t) ext_blk ++ << (grub_le_to_cpu16 (data->sblock.log2_blksz) - GRUB_DISK_SECTOR_BITS), ++ 0, sizeof (*tree), (char *) tree)) + { + if (grub_memcmp (&tree->treehead, treehead, sizeof (struct grub_jfs_treehead)) || + grub_memcmp (&tree->extents, extents, 254 * sizeof (struct grub_jfs_tree_extent))) +@@ -361,7 +377,7 @@ grub_jfs_read_inode (struct grub_jfs_data *data, grub_uint32_t ino, + sizeof (iag_inodes), &iag_inodes)) + return grub_errno; + +- inoblk = grub_le_to_cpu32 (iag_inodes[inoext].blk2); ++ inoblk = get_ext_offset (iag_inodes[inoext].blk1, iag_inodes[inoext].blk2); + inoblk <<= (grub_le_to_cpu16 (data->sblock.log2_blksz) + - GRUB_DISK_SECTOR_BITS); + inoblk += inonum; +@@ -490,7 +506,8 @@ grub_jfs_opendir (struct grub_jfs_data *data, struct grub_jfs_inode *inode) + return 0; + } + +- blk = grub_le_to_cpu32 (de[inode->dir.header.sorted[0]].ex.blk2); ++ blk = get_ext_offset (de[inode->dir.header.sorted[0]].ex.blk1, ++ de[inode->dir.header.sorted[0]].ex.blk2); + blk <<= (grub_le_to_cpu16 (data->sblock.log2_blksz) - GRUB_DISK_SECTOR_BITS); + + /* Read in the nodes until we are on the leaf node level. */ +@@ -508,7 +525,7 @@ grub_jfs_opendir (struct grub_jfs_data *data, struct grub_jfs_inode *inode) + + de = (struct grub_jfs_internal_dirent *) diro->dirpage->dirent; + index = diro->dirpage->sorted[diro->dirpage->header.sindex * 32]; +- blk = (grub_le_to_cpu32 (de[index].ex.blk2) ++ blk = (get_ext_offset (de[index].ex.blk1, de[index].ex.blk2) + << (grub_le_to_cpu16 (data->sblock.log2_blksz) + - GRUB_DISK_SECTOR_BITS)); + } while (!(diro->dirpage->header.flags & GRUB_JFS_TREE_LEAF)); diff --git a/0604-fs-jfs-Inconsistent-signed-unsigned-types-usage-in-r.patch b/0604-fs-jfs-Inconsistent-signed-unsigned-types-usage-in-r.patch new file mode 100644 index 0000000..366d51d --- /dev/null +++ b/0604-fs-jfs-Inconsistent-signed-unsigned-types-usage-in-r.patch @@ -0,0 +1,85 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Lidong Chen +Date: Mon, 16 Dec 2024 20:22:40 +0000 +Subject: [PATCH] fs/jfs: Inconsistent signed/unsigned types usage in return + values + +The getblk() returns a value of type grub_int64_t which is assigned to +iagblk and inoblk, both of type grub_uint64_t, in grub_jfs_read_inode() +via grub_jfs_blkno(). This patch fixes the type mismatch in the +functions. Additionally, the getblk() will return 0 instead of -1 on +failure cases. This change is safe because grub_errno is always set in +getblk() to indicate errors and it is later checked in the callers. + +Signed-off-by: Lidong Chen +Reviewed-by: Alec Brown +Reviewed-by: Ross Philipson +Reviewed-by: Daniel Kiper +--- + grub-core/fs/jfs.c | 15 +++++++++------ + 1 file changed, 9 insertions(+), 6 deletions(-) + +diff --git a/grub-core/fs/jfs.c b/grub-core/fs/jfs.c +index 8a908f00d..739b3c540 100644 +--- a/grub-core/fs/jfs.c ++++ b/grub-core/fs/jfs.c +@@ -279,7 +279,7 @@ get_ext_offset (grub_uint8_t offset1, grub_uint32_t offset2) + return (((grub_uint64_t) offset1 << 32) | grub_le_to_cpu32 (offset2)); + } + +-static grub_int64_t ++static grub_uint64_t + getblk (struct grub_jfs_treehead *treehead, + struct grub_jfs_tree_extent *extents, + int max_extents, +@@ -290,6 +290,8 @@ getblk (struct grub_jfs_treehead *treehead, + int i; + grub_uint64_t ext_offset, ext_blk; + ++ grub_errno = GRUB_ERR_NONE; ++ + for (i = 0; i < grub_le_to_cpu16 (treehead->count) - 2 && + i < max_extents; i++) + { +@@ -312,7 +314,7 @@ getblk (struct grub_jfs_treehead *treehead, + + if (found != -1) + { +- grub_int64_t ret = -1; ++ grub_uint64_t ret = 0; + struct + { + struct grub_jfs_treehead treehead; +@@ -321,7 +323,7 @@ getblk (struct grub_jfs_treehead *treehead, + + tree = grub_zalloc (sizeof (*tree)); + if (!tree) +- return -1; ++ return 0; + + if (!grub_disk_read (data->disk, + (grub_disk_addr_t) ext_blk +@@ -334,19 +336,20 @@ getblk (struct grub_jfs_treehead *treehead, + else + { + grub_error (GRUB_ERR_BAD_FS, "jfs: infinite recursion detected"); +- ret = -1; ++ ret = 0; + } + } + grub_free (tree); + return ret; + } + +- return -1; ++ grub_error (GRUB_ERR_READ_ERROR, "jfs: block %" PRIuGRUB_UINT64_T " not found", blk); ++ return 0; + } + + /* Get the block number for the block BLK in the node INODE in the + mounted filesystem DATA. */ +-static grub_int64_t ++static grub_uint64_t + grub_jfs_blkno (struct grub_jfs_data *data, struct grub_jfs_inode *inode, + grub_uint64_t blk) + { diff --git a/0605-fs-ext2-Fix-out-of-bounds-read-for-inline-extents.patch b/0605-fs-ext2-Fix-out-of-bounds-read-for-inline-extents.patch new file mode 100644 index 0000000..853b663 --- /dev/null +++ b/0605-fs-ext2-Fix-out-of-bounds-read-for-inline-extents.patch @@ -0,0 +1,46 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Michael Chang +Date: Fri, 31 May 2024 15:14:23 +0800 +Subject: [PATCH] fs/ext2: Fix out-of-bounds read for inline extents + +When inline extents are used, i.e. the extent tree depth equals zero, +a maximum of four entries can fit into the inode's data block. If the +extent header states a number of entries greater than four the current +ext2 implementation causes an out-of-bounds read. Fix this issue by +capping the number of extents to four when reading inline extents. + +Reported-by: Daniel Axtens +Signed-off-by: Michael Chang +Reviewed-by: Daniel Kiper +--- + grub-core/fs/ext2.c | 10 +++++++++- + 1 file changed, 9 insertions(+), 1 deletion(-) + +diff --git a/grub-core/fs/ext2.c b/grub-core/fs/ext2.c +index 3d59cf131..de5c268a9 100644 +--- a/grub-core/fs/ext2.c ++++ b/grub-core/fs/ext2.c +@@ -473,6 +473,8 @@ grub_ext2_read_block (grub_fshelp_node_t node, grub_disk_addr_t fileblock) + struct grub_ext4_extent *ext; + int i; + grub_disk_addr_t ret; ++ grub_uint16_t nent; ++ const grub_uint16_t max_inline_ext = sizeof (inode->blocks) / sizeof (*ext) - 1; /* Minus 1 extent header. */ + + leaf = grub_ext4_find_leaf (data, (struct grub_ext4_extent_header *) inode->blocks.dir_blocks, fileblock); + if (! leaf) +@@ -482,7 +484,13 @@ grub_ext2_read_block (grub_fshelp_node_t node, grub_disk_addr_t fileblock) + } + + ext = (struct grub_ext4_extent *) (leaf + 1); +- for (i = 0; i < grub_le_to_cpu16 (leaf->entries); i++) ++ ++ nent = grub_le_to_cpu16 (leaf->entries); ++ ++ if (leaf->depth == 0) ++ nent = grub_min (nent, max_inline_ext); ++ ++ for (i = 0; i < nent; i++) + { + if (fileblock < grub_le_to_cpu32 (ext[i].block)) + break; diff --git a/0606-fs-ntfs-Fix-out-of-bounds-read.patch b/0606-fs-ntfs-Fix-out-of-bounds-read.patch new file mode 100644 index 0000000..3b50108 --- /dev/null +++ b/0606-fs-ntfs-Fix-out-of-bounds-read.patch @@ -0,0 +1,47 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Michael Chang +Date: Mon, 3 Jun 2024 12:12:06 +0800 +Subject: [PATCH] fs/ntfs: Fix out-of-bounds read + +When parsing NTFS file records the presence of the 0xFF marker indicates +the end of the attribute list. This value signifies that there are no +more attributes to process. + +However, when the end marker is missing due to corrupted metadata the +loop continues to read beyond the attribute list resulting in out-of-bounds +reads and potentially entering an infinite loop. + +This patch adds a check to provide a stop condition for the loop ensuring +it stops at the end of the attribute list or at the end of the Master File +Table. This guards against out-of-bounds reads and prevents infinite loops. + +Reported-by: Daniel Axtens +Signed-off-by: Michael Chang +Reviewed-by: Daniel Kiper +--- + grub-core/fs/ntfs.c | 5 ++++- + 1 file changed, 4 insertions(+), 1 deletion(-) + +diff --git a/grub-core/fs/ntfs.c b/grub-core/fs/ntfs.c +index d3fb5b48f..9c4d9be98 100644 +--- a/grub-core/fs/ntfs.c ++++ b/grub-core/fs/ntfs.c +@@ -139,6 +139,8 @@ free_attr (struct grub_ntfs_attr *at) + static grub_uint8_t * + find_attr (struct grub_ntfs_attr *at, grub_uint8_t attr) + { ++ grub_uint8_t *mft_end; ++ + if (at->flags & GRUB_NTFS_AF_ALST) + { + retry: +@@ -191,7 +193,8 @@ find_attr (struct grub_ntfs_attr *at, grub_uint8_t attr) + return NULL; + } + at->attr_cur = at->attr_nxt; +- while (*at->attr_cur != 0xFF) ++ mft_end = at->mft->buf + (at->mft->data->mft_size << GRUB_NTFS_BLK_SHR); ++ while (at->attr_cur < mft_end && *at->attr_cur != 0xFF) + { + at->attr_nxt += u16at (at->attr_cur, 4); + if (*at->attr_cur == GRUB_NTFS_AT_ATTRIBUTE_LIST) diff --git a/0607-fs-ntfs-Track-the-end-of-the-MFT-attribute-buffer.patch b/0607-fs-ntfs-Track-the-end-of-the-MFT-attribute-buffer.patch new file mode 100644 index 0000000..893bdfb --- /dev/null +++ b/0607-fs-ntfs-Track-the-end-of-the-MFT-attribute-buffer.patch @@ -0,0 +1,141 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: B Horn +Date: Tue, 7 Jan 2025 11:38:34 +0000 +Subject: [PATCH] fs/ntfs: Track the end of the MFT attribute buffer + +The end of the attribute buffer should be stored alongside the rest of +the attribute struct as right now it is not possible to implement bounds +checking when accessing attributes sequentially. + +This is done via: + - updating init_attr() to set at->end and check is is not initially out of bounds, + - implementing checks as init_attr() had its type change in its callers, + - updating the value of at->end when needed. + +Signed-off-by: B Horn +Reviewed-by: Daniel Kiper +--- + grub-core/fs/ntfs.c | 34 ++++++++++++++++++++++++++++------ + include/grub/ntfs.h | 1 + + 2 files changed, 29 insertions(+), 6 deletions(-) + +diff --git a/grub-core/fs/ntfs.c b/grub-core/fs/ntfs.c +index 9c4d9be98..699cdb3c0 100644 +--- a/grub-core/fs/ntfs.c ++++ b/grub-core/fs/ntfs.c +@@ -119,13 +119,20 @@ static grub_err_t read_data (struct grub_ntfs_attr *at, grub_uint8_t *pa, + grub_disk_read_hook_t read_hook, + void *read_hook_data); + +-static void ++static grub_err_t + init_attr (struct grub_ntfs_attr *at, struct grub_ntfs_file *mft) + { + at->mft = mft; + at->flags = (mft == &mft->data->mmft) ? GRUB_NTFS_AF_MMFT : 0; + at->attr_nxt = mft->buf + first_attr_off (mft->buf); ++ at->end = mft->buf + (mft->data->mft_size << GRUB_NTFS_BLK_SHR); ++ ++ if (at->attr_nxt > at->end) ++ return grub_error (GRUB_ERR_BAD_FS, "attributes start outside the MFT"); ++ + at->attr_end = at->emft_buf = at->edat_buf = at->sbuf = NULL; ++ ++ return GRUB_ERR_NONE; + } + + static void +@@ -239,6 +246,10 @@ find_attr (struct grub_ntfs_attr *at, grub_uint8_t attr) + pa_end = at->mft->buf + (at->mft->data->mft_size << GRUB_NTFS_BLK_SHR); + } + at->flags |= GRUB_NTFS_AF_ALST; ++ ++ /* From this point on pa_end is the end of the buffer */ ++ at->end = pa_end; ++ + while (at->attr_nxt < at->attr_end) + { + if ((*at->attr_nxt == attr) || (attr == 0)) +@@ -298,7 +309,9 @@ locate_attr (struct grub_ntfs_attr *at, struct grub_ntfs_file *mft, + { + grub_uint8_t *pa; + +- init_attr (at, mft); ++ if (init_attr (at, mft) != GRUB_ERR_NONE) ++ return NULL; ++ + pa = find_attr (at, attr); + if (pa == NULL) + return NULL; +@@ -314,7 +327,8 @@ locate_attr (struct grub_ntfs_attr *at, struct grub_ntfs_file *mft, + } + grub_errno = GRUB_ERR_NONE; + free_attr (at); +- init_attr (at, mft); ++ if (init_attr (at, mft) != GRUB_ERR_NONE) ++ return NULL; + pa = find_attr (at, attr); + } + return pa; +@@ -585,7 +599,7 @@ init_file (struct grub_ntfs_file *mft, grub_uint64_t mftno) + mft->attr.attr_end = 0; /* Don't jump to attribute list */ + } + else +- init_attr (&mft->attr, mft); ++ return init_attr (&mft->attr, mft); + + return 0; + } +@@ -807,7 +821,9 @@ grub_ntfs_iterate_dir (grub_fshelp_node_t dir, + bmp = NULL; + + at = &attr; +- init_attr (at, mft); ++ if (init_attr (at, mft) != GRUB_ERR_NONE) ++ return 0; ++ + while (1) + { + cur_pos = find_attr (at, GRUB_NTFS_AT_INDEX_ROOT); +@@ -838,7 +854,9 @@ grub_ntfs_iterate_dir (grub_fshelp_node_t dir, + bitmap = NULL; + bitmap_len = 0; + free_attr (at); ++ /* No need to check errors here, as it will already be fine */ + init_attr (at, mft); ++ + while ((cur_pos = find_attr (at, GRUB_NTFS_AT_BITMAP)) != NULL) + { + int ofs; +@@ -1203,6 +1221,7 @@ grub_ntfs_label (grub_device_t device, char **label) + struct grub_ntfs_data *data = 0; + struct grub_fshelp_node *mft = 0; + grub_uint8_t *pa; ++ grub_err_t err; + + grub_dl_ref (my_mod); + +@@ -1228,7 +1247,10 @@ grub_ntfs_label (grub_device_t device, char **label) + goto fail; + } + +- init_attr (&mft->attr, mft); ++ err = init_attr (&mft->attr, mft); ++ if (err != GRUB_ERR_NONE) ++ return err; ++ + pa = find_attr (&mft->attr, GRUB_NTFS_AT_VOLUME_NAME); + + if (pa >= mft->buf + (mft->data->mft_size << GRUB_NTFS_BLK_SHR)) +diff --git a/include/grub/ntfs.h b/include/grub/ntfs.h +index d1a6af696..ec1c4db38 100644 +--- a/include/grub/ntfs.h ++++ b/include/grub/ntfs.h +@@ -134,6 +134,7 @@ struct grub_ntfs_attr + grub_uint8_t *attr_cur, *attr_nxt, *attr_end; + grub_uint32_t save_pos; + grub_uint8_t *sbuf; ++ grub_uint8_t *end; + struct grub_ntfs_file *mft; + }; + diff --git a/0608-fs-ntfs-Use-a-helper-function-to-access-attributes.patch b/0608-fs-ntfs-Use-a-helper-function-to-access-attributes.patch new file mode 100644 index 0000000..16029ba --- /dev/null +++ b/0608-fs-ntfs-Use-a-helper-function-to-access-attributes.patch @@ -0,0 +1,183 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: B Horn +Date: Tue, 14 May 2024 12:39:56 +0100 +Subject: [PATCH] fs/ntfs: Use a helper function to access attributes + +Right now to access the next attribute the code reads the length of the +current attribute and adds that to the current pointer. This is error +prone as bounds checking needs to be performed all over the place. So, +implement a helper and ensure its used across find_attr() and read_attr(). + +This commit does *not* implement full bounds checking. It is just the +preparation work for this to be added into the helper. + +Signed-off-by: B Horn +Reviewed-by: Daniel Kiper +--- + grub-core/fs/ntfs.c | 69 +++++++++++++++++++++++++++++++++++++++++++---------- + include/grub/ntfs.h | 2 ++ + 2 files changed, 58 insertions(+), 13 deletions(-) + +diff --git a/grub-core/fs/ntfs.c b/grub-core/fs/ntfs.c +index 699cdb3c0..5b3189ab2 100644 +--- a/grub-core/fs/ntfs.c ++++ b/grub-core/fs/ntfs.c +@@ -70,6 +70,25 @@ res_attr_data_len (void *res_attr_ptr) + return u32at (res_attr_ptr, 0x10); + } + ++/* Return the next attribute if it exists, otherwise return NULL. */ ++static grub_uint8_t * ++next_attribute (grub_uint8_t *curr_attribute, void *end) ++{ ++ grub_uint8_t *next = curr_attribute; ++ ++ /* ++ * Need to verify we aren't exceeding the end of the buffer by reading the ++ * header for the current attribute ++ */ ++ if (curr_attribute + GRUB_NTFS_ATTRIBUTE_HEADER_SIZE >= (grub_uint8_t *) end) ++ return NULL; ++ ++ next += u16at (curr_attribute, 4); ++ ++ return next; ++} ++ ++ + grub_ntfscomp_func_t grub_ntfscomp_func; + + static grub_err_t +@@ -151,13 +170,13 @@ find_attr (struct grub_ntfs_attr *at, grub_uint8_t attr) + if (at->flags & GRUB_NTFS_AF_ALST) + { + retry: +- while (at->attr_nxt < at->attr_end) ++ while (at->attr_nxt) + { + at->attr_cur = at->attr_nxt; +- at->attr_nxt += u16at (at->attr_cur, 4); ++ at->attr_nxt = next_attribute (at->attr_cur, at->attr_end); + if ((*at->attr_cur == attr) || (attr == 0)) + { +- grub_uint8_t *new_pos; ++ grub_uint8_t *new_pos, *end; + + if (at->flags & GRUB_NTFS_AF_MMFT) + { +@@ -181,15 +200,36 @@ find_attr (struct grub_ntfs_attr *at, grub_uint8_t attr) + return NULL; + } + ++ /* ++ * Only time emft_bufs is defined is in this function, with this ++ * size. ++ */ ++ grub_size_t emft_buf_size = ++ at->mft->data->mft_size << GRUB_NTFS_BLK_SHR; ++ ++ /* ++ * Needs to be enough space for the successful case to even ++ * bother. ++ */ ++ if (first_attr_off (at->emft_buf) >= (emft_buf_size - 0x18 - 2)) ++ { ++ grub_error (GRUB_ERR_BAD_FS, ++ "can\'t find 0x%X in attribute list", ++ (unsigned char) *at->attr_cur); ++ return NULL; ++ } ++ + new_pos = &at->emft_buf[first_attr_off (at->emft_buf)]; +- while (*new_pos != 0xFF) ++ end = &at->emft_buf[emft_buf_size]; ++ ++ while (new_pos && *new_pos != 0xFF) + { + if ((*new_pos == *at->attr_cur) + && (u16at (new_pos, 0xE) == u16at (at->attr_cur, 0x18))) + { + return new_pos; + } +- new_pos += u16at (new_pos, 4); ++ new_pos = next_attribute (new_pos, end); + } + grub_error (GRUB_ERR_BAD_FS, + "can\'t find 0x%X in attribute list", +@@ -203,7 +243,7 @@ find_attr (struct grub_ntfs_attr *at, grub_uint8_t attr) + mft_end = at->mft->buf + (at->mft->data->mft_size << GRUB_NTFS_BLK_SHR); + while (at->attr_cur < mft_end && *at->attr_cur != 0xFF) + { +- at->attr_nxt += u16at (at->attr_cur, 4); ++ at->attr_nxt = next_attribute (at->attr_cur, at->end); + if (*at->attr_cur == GRUB_NTFS_AT_ATTRIBUTE_LIST) + at->attr_end = at->attr_cur; + if ((*at->attr_cur == attr) || (attr == 0)) +@@ -250,13 +290,14 @@ find_attr (struct grub_ntfs_attr *at, grub_uint8_t attr) + /* From this point on pa_end is the end of the buffer */ + at->end = pa_end; + +- while (at->attr_nxt < at->attr_end) ++ while (at->attr_nxt) + { + if ((*at->attr_nxt == attr) || (attr == 0)) + break; +- at->attr_nxt += u16at (at->attr_nxt, 4); ++ at->attr_nxt = next_attribute (at->attr_nxt, pa_end); + } +- if (at->attr_nxt >= at->attr_end) ++ ++ if (at->attr_nxt >= at->attr_end || at->attr_nxt == NULL) + return NULL; + + if ((at->flags & GRUB_NTFS_AF_MMFT) && (attr == GRUB_NTFS_AT_DATA)) +@@ -277,7 +318,8 @@ find_attr (struct grub_ntfs_attr *at, grub_uint8_t attr) + grub_cpu_to_le32 (at->mft->data->mft_start + + 1)); + pa = at->attr_nxt + u16at (pa, 4); +- while (pa < at->attr_end) ++ ++ while (pa) + { + if (*pa != attr) + break; +@@ -293,7 +335,7 @@ find_attr (struct grub_ntfs_attr *at, grub_uint8_t attr) + u32at (pa, 0x10) * (at->mft->data->mft_size << GRUB_NTFS_BLK_SHR), + at->mft->data->mft_size << GRUB_NTFS_BLK_SHR, 0, 0, 0)) + return NULL; +- pa += u16at (pa, 4); ++ pa = next_attribute (pa, pa_end); + } + at->attr_nxt = at->attr_cur; + at->flags &= ~GRUB_NTFS_AF_GPOS; +@@ -530,14 +572,15 @@ read_attr (struct grub_ntfs_attr *at, grub_uint8_t *dest, grub_disk_addr_t ofs, + else + vcn = ofs >> (at->mft->data->log_spc + GRUB_NTFS_BLK_SHR); + pa = at->attr_nxt + u16at (at->attr_nxt, 4); +- while (pa < at->attr_end) ++ ++ while (pa) + { + if (*pa != attr) + break; + if (u32at (pa, 8) > vcn) + break; + at->attr_nxt = pa; +- pa += u16at (pa, 4); ++ pa = next_attribute (pa, at->attr_end); + } + } + pp = find_attr (at, attr); +diff --git a/include/grub/ntfs.h b/include/grub/ntfs.h +index ec1c4db38..2c8078403 100644 +--- a/include/grub/ntfs.h ++++ b/include/grub/ntfs.h +@@ -89,6 +89,8 @@ enum + #define GRUB_NTFS_COM_SEC (GRUB_NTFS_COM_LEN >> GRUB_NTFS_BLK_SHR) + #define GRUB_NTFS_LOG_COM_SEC (GRUB_NTFS_COM_LOG_LEN - GRUB_NTFS_BLK_SHR) + ++#define GRUB_NTFS_ATTRIBUTE_HEADER_SIZE 16 ++ + enum + { + GRUB_NTFS_AF_ALST = 1, diff --git a/0610-fs-xfs-Fix-out-of-bounds-read.patch b/0610-fs-xfs-Fix-out-of-bounds-read.patch new file mode 100644 index 0000000..9e31a92 --- /dev/null +++ b/0610-fs-xfs-Fix-out-of-bounds-read.patch @@ -0,0 +1,42 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Michael Chang +Date: Fri, 31 May 2024 15:14:57 +0800 +Subject: [PATCH] fs/xfs: Fix out-of-bounds read + +The number of records in the root key array read from disk was not being +validated against the size of the root node. This could lead to an +out-of-bounds read. + +This patch adds a check to ensure that the number of records in the root +key array does not exceed the expected size of a root node read from +disk. If this check detects an out-of-bounds condition the operation is +aborted to prevent random errors due to metadata corruption. + +Reported-by: Daniel Axtens +Signed-off-by: Michael Chang +Reviewed-by: Daniel Kiper +--- + grub-core/fs/xfs.c | 10 ++++++++++ + 1 file changed, 10 insertions(+) + +diff --git a/grub-core/fs/xfs.c b/grub-core/fs/xfs.c +index 66e66dd58..f919bfd2a 100644 +--- a/grub-core/fs/xfs.c ++++ b/grub-core/fs/xfs.c +@@ -540,6 +540,16 @@ grub_xfs_read_block (grub_fshelp_node_t node, grub_disk_addr_t fileblock) + do + { + int i; ++ grub_addr_t keys_end, data_end; ++ if (grub_mul (sizeof (grub_uint64_t), nrec, &keys_end) || ++ grub_add ((grub_addr_t) keys, keys_end, &keys_end) || ++ grub_add ((grub_addr_t) node->data, node->data->data_size, &data_end) || ++ keys_end > data_end) ++ { ++ grub_error (GRUB_ERR_BAD_FS, "invalid number of XFS root keys"); ++ grub_free (leaf); ++ return 0; ++ } + + for (i = 0; i < nrec; i++) + { diff --git a/0611-fs-xfs-Ensuring-failing-to-mount-sets-a-grub_errno.patch b/0611-fs-xfs-Ensuring-failing-to-mount-sets-a-grub_errno.patch new file mode 100644 index 0000000..b67077d --- /dev/null +++ b/0611-fs-xfs-Ensuring-failing-to-mount-sets-a-grub_errno.patch @@ -0,0 +1,42 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: B Horn +Date: Sun, 12 May 2024 06:03:58 +0100 +Subject: [PATCH] fs/xfs: Ensuring failing to mount sets a grub_errno + +It was previously possible for grub_xfs_mount() to return NULL without +setting grub_errno if the XFS version was invalid. This resulted in it +being possible for grub_dl_unref() to be called twice allowing the XFS +module to be unloaded while there were still references to it. + +Fixing this problem in general by ensuring a grub_errno is set if the +fail label is reached. + +Reported-by: B Horn +Signed-off-by: B Horn +Reviewed-by: Daniel Kiper +--- + grub-core/fs/xfs.c | 4 +++- + 1 file changed, 3 insertions(+), 1 deletion(-) + +diff --git a/grub-core/fs/xfs.c b/grub-core/fs/xfs.c +index f919bfd2a..c9818a96a 100644 +--- a/grub-core/fs/xfs.c ++++ b/grub-core/fs/xfs.c +@@ -297,6 +297,8 @@ static int grub_xfs_sb_valid(struct grub_xfs_data *data) + } + return 1; + } ++ ++ grub_error (GRUB_ERR_BAD_FS, "unsupported XFS filesystem version"); + return 0; + } + +@@ -952,7 +954,7 @@ grub_xfs_mount (grub_disk_t disk) + return data; + fail: + +- if (grub_errno == GRUB_ERR_OUT_OF_RANGE) ++ if (grub_errno == GRUB_ERR_OUT_OF_RANGE || grub_errno == GRUB_ERR_NONE) + grub_error (GRUB_ERR_BAD_FS, "not an XFS filesystem"); + + grub_free (data); diff --git a/0612-kern-file-Ensure-file-data-is-set.patch b/0612-kern-file-Ensure-file-data-is-set.patch new file mode 100644 index 0000000..efd3b6b --- /dev/null +++ b/0612-kern-file-Ensure-file-data-is-set.patch @@ -0,0 +1,32 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: B Horn +Date: Sun, 12 May 2024 03:01:40 +0100 +Subject: [PATCH] kern/file: Ensure file->data is set + +This is to avoid a generic issue were some filesystems would not set +data and also not set a grub_errno. This meant it was possible for many +filesystems to grub_dl_unref() themselves multiple times resulting in +it being possible to unload the filesystems while there were still +references to them, e.g., via a loopback. + +Reported-by: B Horn +Signed-off-by: B Horn +Reviewed-by: Daniel Kiper +--- + grub-core/kern/file.c | 3 +++ + 1 file changed, 3 insertions(+) + +diff --git a/grub-core/kern/file.c b/grub-core/kern/file.c +index 5e1f29d0d..596f16ced 100644 +--- a/grub-core/kern/file.c ++++ b/grub-core/kern/file.c +@@ -121,6 +121,9 @@ grub_file_open (const char *name, enum grub_file_type type) + if ((file->fs->open) (file, file_name) != GRUB_ERR_NONE) + goto fail; + ++ if (file->data == NULL) ++ goto fail; ++ + file->name = grub_strdup (name); + grub_errno = GRUB_ERR_NONE; + diff --git a/0613-kern-file-Implement-filesystem-reference-counting.patch b/0613-kern-file-Implement-filesystem-reference-counting.patch new file mode 100644 index 0000000..0e04721 --- /dev/null +++ b/0613-kern-file-Implement-filesystem-reference-counting.patch @@ -0,0 +1,430 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: B Horn +Date: Sun, 12 May 2024 10:15:03 +0100 +Subject: [PATCH] kern/file: Implement filesystem reference counting + +The grub_file_open() and grub_file_close() should be the only places +that allow a reference to a filesystem to stay open. So, add grub_dl_t +to grub_fs_t and set this in the GRUB_MOD_INIT() for each filesystem to +avoid issues when filesystems forget to do it themselves or do not track +their own references, e.g. squash4. + +The fs_label(), fs_uuid(), fs_mtime() and fs_read() should all ref and +unref in the same function but it is essentially redundant in GRUB +single threaded model. + +Signed-off-by: B Horn +Reviewed-by: Daniel Kiper +--- + grub-core/fs/affs.c | 1 + + grub-core/fs/bfs.c | 1 + + grub-core/fs/btrfs.c | 1 + + grub-core/fs/cbfs.c | 1 + + grub-core/fs/cpio.c | 1 + + grub-core/fs/cpio_be.c | 1 + + grub-core/fs/ext2.c | 1 + + grub-core/fs/f2fs.c | 1 + + grub-core/fs/fat.c | 1 + + grub-core/fs/hfs.c | 1 + + grub-core/fs/hfsplus.c | 1 + + grub-core/fs/iso9660.c | 1 + + grub-core/fs/jfs.c | 1 + + grub-core/fs/minix.c | 1 + + grub-core/fs/newc.c | 1 + + grub-core/fs/nilfs2.c | 1 + + grub-core/fs/ntfs.c | 1 + + grub-core/fs/odc.c | 1 + + grub-core/fs/proc.c | 1 + + grub-core/fs/reiserfs.c | 1 + + grub-core/fs/romfs.c | 1 + + grub-core/fs/sfs.c | 1 + + grub-core/fs/squash4.c | 1 + + grub-core/fs/tar.c | 1 + + grub-core/fs/udf.c | 1 + + grub-core/fs/ufs.c | 1 + + grub-core/fs/xfs.c | 1 + + grub-core/fs/zfs/zfs.c | 1 + + grub-core/kern/file.c | 4 ++++ + include/grub/fs.h | 4 ++++ + 30 files changed, 36 insertions(+) + +diff --git a/grub-core/fs/affs.c b/grub-core/fs/affs.c +index e4615c743..6347ca368 100644 +--- a/grub-core/fs/affs.c ++++ b/grub-core/fs/affs.c +@@ -699,6 +699,7 @@ static struct grub_fs grub_affs_fs = + + GRUB_MOD_INIT(affs) + { ++ grub_affs_fs.mod = mod; + grub_fs_register (&grub_affs_fs); + my_mod = mod; + } +diff --git a/grub-core/fs/bfs.c b/grub-core/fs/bfs.c +index d2b490bce..6afdfc987 100644 +--- a/grub-core/fs/bfs.c ++++ b/grub-core/fs/bfs.c +@@ -1104,6 +1104,7 @@ GRUB_MOD_INIT (bfs) + { + COMPILE_TIME_ASSERT (1 << LOG_EXTENT_SIZE == + sizeof (struct grub_bfs_extent)); ++ grub_bfs_fs.mod = mod; + grub_fs_register (&grub_bfs_fs); + } + +diff --git a/grub-core/fs/btrfs.c b/grub-core/fs/btrfs.c +index 9da2952f7..65213549e 100644 +--- a/grub-core/fs/btrfs.c ++++ b/grub-core/fs/btrfs.c +@@ -2802,6 +2802,7 @@ subvol_get_env (struct grub_env_var *var __attribute__ ((unused)), + + GRUB_MOD_INIT (btrfs) + { ++ grub_btrfs_fs.mod = mod; + grub_fs_register (&grub_btrfs_fs); + cmd_info = grub_register_command("btrfs-info", grub_cmd_btrfs_info, + "DEVICE", +diff --git a/grub-core/fs/cbfs.c b/grub-core/fs/cbfs.c +index 0842701a6..395edcbbd 100644 +--- a/grub-core/fs/cbfs.c ++++ b/grub-core/fs/cbfs.c +@@ -390,6 +390,7 @@ GRUB_MOD_INIT (cbfs) + #if (defined (__i386__) || defined (__x86_64__)) && !defined (GRUB_UTIL) && !defined (GRUB_MACHINE_EMU) && !defined (GRUB_MACHINE_XEN) + init_cbfsdisk (); + #endif ++ grub_cbfs_fs.mod = mod; + grub_fs_register (&grub_cbfs_fs); + } + +diff --git a/grub-core/fs/cpio.c b/grub-core/fs/cpio.c +index dab5f9898..1799f7ff5 100644 +--- a/grub-core/fs/cpio.c ++++ b/grub-core/fs/cpio.c +@@ -52,6 +52,7 @@ read_number (const grub_uint16_t *arr, grub_size_t size) + + GRUB_MOD_INIT (cpio) + { ++ grub_cpio_fs.mod = mod; + grub_fs_register (&grub_cpio_fs); + } + +diff --git a/grub-core/fs/cpio_be.c b/grub-core/fs/cpio_be.c +index 846548892..7bed1b848 100644 +--- a/grub-core/fs/cpio_be.c ++++ b/grub-core/fs/cpio_be.c +@@ -52,6 +52,7 @@ read_number (const grub_uint16_t *arr, grub_size_t size) + + GRUB_MOD_INIT (cpio_be) + { ++ grub_cpio_fs.mod = mod; + grub_fs_register (&grub_cpio_fs); + } + +diff --git a/grub-core/fs/ext2.c b/grub-core/fs/ext2.c +index de5c268a9..dc62a6cfe 100644 +--- a/grub-core/fs/ext2.c ++++ b/grub-core/fs/ext2.c +@@ -1105,6 +1105,7 @@ static struct grub_fs grub_ext2_fs = + + GRUB_MOD_INIT(ext2) + { ++ grub_ext2_fs.mod = mod; + grub_fs_register (&grub_ext2_fs); + my_mod = mod; + } +diff --git a/grub-core/fs/f2fs.c b/grub-core/fs/f2fs.c +index 706e595d3..149f33695 100644 +--- a/grub-core/fs/f2fs.c ++++ b/grub-core/fs/f2fs.c +@@ -1339,6 +1339,7 @@ static struct grub_fs grub_f2fs_fs = { + + GRUB_MOD_INIT (f2fs) + { ++ grub_f2fs_fs.mod = mod; + grub_fs_register (&grub_f2fs_fs); + my_mod = mod; + } +diff --git a/grub-core/fs/fat.c b/grub-core/fs/fat.c +index 8d8dc35ce..72785a798 100644 +--- a/grub-core/fs/fat.c ++++ b/grub-core/fs/fat.c +@@ -1254,6 +1254,7 @@ GRUB_MOD_INIT(fat) + #endif + { + COMPILE_TIME_ASSERT (sizeof (struct grub_fat_dir_entry) == 32); ++ grub_fat_fs.mod = mod; + grub_fs_register (&grub_fat_fs); + my_mod = mod; + } +diff --git a/grub-core/fs/hfs.c b/grub-core/fs/hfs.c +index 44859fe43..66b9fca83 100644 +--- a/grub-core/fs/hfs.c ++++ b/grub-core/fs/hfs.c +@@ -1434,6 +1434,7 @@ static struct grub_fs grub_hfs_fs = + + GRUB_MOD_INIT(hfs) + { ++ grub_hfs_fs.mod = mod; + if (!grub_is_lockdown ()) + grub_fs_register (&grub_hfs_fs); + my_mod = mod; +diff --git a/grub-core/fs/hfsplus.c b/grub-core/fs/hfsplus.c +index 2edb1649d..a1805eed0 100644 +--- a/grub-core/fs/hfsplus.c ++++ b/grub-core/fs/hfsplus.c +@@ -1139,6 +1139,7 @@ static struct grub_fs grub_hfsplus_fs = + + GRUB_MOD_INIT(hfsplus) + { ++ grub_hfsplus_fs.mod = mod; + grub_fs_register (&grub_hfsplus_fs); + my_mod = mod; + } +diff --git a/grub-core/fs/iso9660.c b/grub-core/fs/iso9660.c +index cf17702e2..576505535 100644 +--- a/grub-core/fs/iso9660.c ++++ b/grub-core/fs/iso9660.c +@@ -1165,6 +1165,7 @@ static struct grub_fs grub_iso9660_fs = + + GRUB_MOD_INIT(iso9660) + { ++ grub_iso9660_fs.mod = mod; + grub_fs_register (&grub_iso9660_fs); + my_mod = mod; + } +diff --git a/grub-core/fs/jfs.c b/grub-core/fs/jfs.c +index 739b3c540..46941248b 100644 +--- a/grub-core/fs/jfs.c ++++ b/grub-core/fs/jfs.c +@@ -998,6 +998,7 @@ static struct grub_fs grub_jfs_fs = + + GRUB_MOD_INIT(jfs) + { ++ grub_jfs_fs.mod = mod; + grub_fs_register (&grub_jfs_fs); + my_mod = mod; + } +diff --git a/grub-core/fs/minix.c b/grub-core/fs/minix.c +index d451b3426..28571c49e 100644 +--- a/grub-core/fs/minix.c ++++ b/grub-core/fs/minix.c +@@ -716,6 +716,7 @@ GRUB_MOD_INIT(minix) + #endif + #endif + { ++ grub_minix_fs.mod = mod; + grub_fs_register (&grub_minix_fs); + my_mod = mod; + } +diff --git a/grub-core/fs/newc.c b/grub-core/fs/newc.c +index 4fb8b2e3d..43b7f8b64 100644 +--- a/grub-core/fs/newc.c ++++ b/grub-core/fs/newc.c +@@ -64,6 +64,7 @@ read_number (const char *str, grub_size_t size) + + GRUB_MOD_INIT (newc) + { ++ grub_cpio_fs.mod = mod; + grub_fs_register (&grub_cpio_fs); + } + +diff --git a/grub-core/fs/nilfs2.c b/grub-core/fs/nilfs2.c +index c4c4610be..3c1e4d1f6 100644 +--- a/grub-core/fs/nilfs2.c ++++ b/grub-core/fs/nilfs2.c +@@ -1231,6 +1231,7 @@ GRUB_MOD_INIT (nilfs2) + grub_nilfs2_dat_entry)); + COMPILE_TIME_ASSERT (1 << LOG_INODE_SIZE + == sizeof (struct grub_nilfs2_inode)); ++ grub_nilfs2_fs.mod = mod; + grub_fs_register (&grub_nilfs2_fs); + my_mod = mod; + } +diff --git a/grub-core/fs/ntfs.c b/grub-core/fs/ntfs.c +index 86222413c..9244e95dd 100644 +--- a/grub-core/fs/ntfs.c ++++ b/grub-core/fs/ntfs.c +@@ -1537,6 +1537,7 @@ static struct grub_fs grub_ntfs_fs = + + GRUB_MOD_INIT (ntfs) + { ++ grub_ntfs_fs.mod = mod; + grub_fs_register (&grub_ntfs_fs); + my_mod = mod; + } +diff --git a/grub-core/fs/odc.c b/grub-core/fs/odc.c +index 790000622..8e4e8aeac 100644 +--- a/grub-core/fs/odc.c ++++ b/grub-core/fs/odc.c +@@ -52,6 +52,7 @@ read_number (const char *str, grub_size_t size) + + GRUB_MOD_INIT (odc) + { ++ grub_cpio_fs.mod = mod; + grub_fs_register (&grub_cpio_fs); + } + +diff --git a/grub-core/fs/proc.c b/grub-core/fs/proc.c +index 31f3aa9a4..317118b81 100644 +--- a/grub-core/fs/proc.c ++++ b/grub-core/fs/proc.c +@@ -192,6 +192,7 @@ static struct grub_fs grub_procfs_fs = + + GRUB_MOD_INIT (procfs) + { ++ grub_procfs_fs.mod = mod; + grub_disk_dev_register (&grub_procfs_dev); + grub_fs_register (&grub_procfs_fs); + } +diff --git a/grub-core/fs/reiserfs.c b/grub-core/fs/reiserfs.c +index 9556c15ff..e65b81467 100644 +--- a/grub-core/fs/reiserfs.c ++++ b/grub-core/fs/reiserfs.c +@@ -1407,6 +1407,7 @@ static struct grub_fs grub_reiserfs_fs = + + GRUB_MOD_INIT(reiserfs) + { ++ grub_reiserfs_fs.mod = mod; + grub_fs_register (&grub_reiserfs_fs); + my_mod = mod; + } +diff --git a/grub-core/fs/romfs.c b/grub-core/fs/romfs.c +index 2e3544408..f282cff86 100644 +--- a/grub-core/fs/romfs.c ++++ b/grub-core/fs/romfs.c +@@ -475,6 +475,7 @@ static struct grub_fs grub_romfs_fs = + + GRUB_MOD_INIT(romfs) + { ++ grub_romfs_fs.mod = mod; + grub_fs_register (&grub_romfs_fs); + } + +diff --git a/grub-core/fs/sfs.c b/grub-core/fs/sfs.c +index 61d6c303c..c6b9fb49a 100644 +--- a/grub-core/fs/sfs.c ++++ b/grub-core/fs/sfs.c +@@ -779,6 +779,7 @@ static struct grub_fs grub_sfs_fs = + + GRUB_MOD_INIT(sfs) + { ++ grub_sfs_fs.mod = mod; + grub_fs_register (&grub_sfs_fs); + my_mod = mod; + } +diff --git a/grub-core/fs/squash4.c b/grub-core/fs/squash4.c +index f9bef38fc..1505832d5 100644 +--- a/grub-core/fs/squash4.c ++++ b/grub-core/fs/squash4.c +@@ -1032,6 +1032,7 @@ static struct grub_fs grub_squash_fs = + + GRUB_MOD_INIT(squash4) + { ++ grub_squash_fs.mod = mod; + grub_fs_register (&grub_squash_fs); + } + +diff --git a/grub-core/fs/tar.c b/grub-core/fs/tar.c +index 28baa5845..01738ec55 100644 +--- a/grub-core/fs/tar.c ++++ b/grub-core/fs/tar.c +@@ -354,6 +354,7 @@ static struct grub_fs grub_cpio_fs = { + + GRUB_MOD_INIT (tar) + { ++ grub_cpio_fs.mod = mod; + grub_fs_register (&grub_cpio_fs); + } + +diff --git a/grub-core/fs/udf.c b/grub-core/fs/udf.c +index 6670beb56..8db2b5686 100644 +--- a/grub-core/fs/udf.c ++++ b/grub-core/fs/udf.c +@@ -1382,6 +1382,7 @@ static struct grub_fs grub_udf_fs = { + + GRUB_MOD_INIT (udf) + { ++ grub_udf_fs.mod = mod; + grub_fs_register (&grub_udf_fs); + my_mod = mod; + } +diff --git a/grub-core/fs/ufs.c b/grub-core/fs/ufs.c +index 0c2004fd7..a2c63d646 100644 +--- a/grub-core/fs/ufs.c ++++ b/grub-core/fs/ufs.c +@@ -899,6 +899,7 @@ GRUB_MOD_INIT(ufs1) + #endif + #endif + { ++ grub_ufs_fs.mod = mod; + grub_fs_register (&grub_ufs_fs); + my_mod = mod; + } +diff --git a/grub-core/fs/xfs.c b/grub-core/fs/xfs.c +index c9818a96a..5b7643eb1 100644 +--- a/grub-core/fs/xfs.c ++++ b/grub-core/fs/xfs.c +@@ -1167,6 +1167,7 @@ static struct grub_fs grub_xfs_fs = + + GRUB_MOD_INIT(xfs) + { ++ grub_xfs_fs.mod = mod; + grub_fs_register (&grub_xfs_fs); + my_mod = mod; + } +diff --git a/grub-core/fs/zfs/zfs.c b/grub-core/fs/zfs/zfs.c +index 0d8c08eec..0c4b15f08 100644 +--- a/grub-core/fs/zfs/zfs.c ++++ b/grub-core/fs/zfs/zfs.c +@@ -4391,6 +4391,7 @@ static struct grub_fs grub_zfs_fs = { + GRUB_MOD_INIT (zfs) + { + COMPILE_TIME_ASSERT (sizeof (zap_leaf_chunk_t) == ZAP_LEAF_CHUNKSIZE); ++ grub_zfs_fs.mod = mod; + grub_fs_register (&grub_zfs_fs); + #ifndef GRUB_UTIL + my_mod = mod; +diff --git a/grub-core/kern/file.c b/grub-core/kern/file.c +index 596f16ced..7e64609d4 100644 +--- a/grub-core/kern/file.c ++++ b/grub-core/kern/file.c +@@ -25,6 +25,7 @@ + #include + #include + #include ++#include + + void (*EXPORT_VAR (grub_grubnet_fini)) (void); + +@@ -124,6 +125,9 @@ grub_file_open (const char *name, enum grub_file_type type) + if (file->data == NULL) + goto fail; + ++ if (file->fs->mod) ++ grub_dl_ref (file->fs->mod); ++ + file->name = grub_strdup (name); + grub_errno = GRUB_ERR_NONE; + +diff --git a/include/grub/fs.h b/include/grub/fs.h +index 5678c60c2..cb1040b50 100644 +--- a/include/grub/fs.h ++++ b/include/grub/fs.h +@@ -23,6 +23,7 @@ + #include + #include + #include ++#include + + #include + /* For embedding types. */ +@@ -57,6 +58,9 @@ struct grub_fs + /* My name. */ + const char *name; + ++ /* My module */ ++ grub_dl_t mod; ++ + /* Call HOOK with each file under DIR. */ + grub_err_t (*dir) (grub_device_t device, const char *path, + grub_fs_dir_hook_t hook, void *hook_data); diff --git a/0614-disk-loopback-Reference-tracking-for-the-loopback.patch b/0614-disk-loopback-Reference-tracking-for-the-loopback.patch new file mode 100644 index 0000000..8780233 --- /dev/null +++ b/0614-disk-loopback-Reference-tracking-for-the-loopback.patch @@ -0,0 +1,103 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: B Horn +Date: Sun, 12 May 2024 03:26:19 +0100 +Subject: [PATCH] disk/loopback: Reference tracking for the loopback + +It was possible to delete a loopback while there were still references +to it. This led to an exploitable use-after-free. + +Fixed by implementing a reference counting in the grub_loopback struct. + +Reported-by: B Horn +Signed-off-by: B Horn +Reviewed-by: Daniel Kiper +--- + grub-core/disk/loopback.c | 18 ++++++++++++++++++ + include/grub/err.h | 3 ++- + 2 files changed, 20 insertions(+), 1 deletion(-) + +diff --git a/grub-core/disk/loopback.c b/grub-core/disk/loopback.c +index 9406d931c..e7052a9cb 100644 +--- a/grub-core/disk/loopback.c ++++ b/grub-core/disk/loopback.c +@@ -24,6 +24,7 @@ + #include + #include + #include ++#include + + GRUB_MOD_LICENSE ("GPLv3+"); + +@@ -33,6 +34,7 @@ struct grub_loopback + grub_file_t file; + struct grub_loopback *next; + unsigned long id; ++ grub_uint64_t refcnt; + }; + + static struct grub_loopback *loopback_list; +@@ -63,6 +65,8 @@ delete_loopback (const char *name) + if (! dev) + return grub_error (GRUB_ERR_BAD_DEVICE, "device not found"); + ++ if (dev->refcnt > 0) ++ return grub_error (GRUB_ERR_STILL_REFERENCED, "device still referenced"); + /* Remove the device from the list. */ + *prev = dev->next; + +@@ -124,6 +128,7 @@ grub_cmd_loopback (grub_extcmd_context_t ctxt, int argc, char **args) + + newdev->file = file; + newdev->id = last_id++; ++ newdev->refcnt = 0; + + /* Add the new entry to the list. */ + newdev->next = loopback_list; +@@ -165,6 +170,9 @@ grub_loopback_open (const char *name, grub_disk_t disk) + if (! dev) + return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "can't open device"); + ++ if (grub_add (dev->refcnt, 1, &dev->refcnt)) ++ grub_fatal ("Reference count overflow"); ++ + /* Use the filesize for the disk size, round up to a complete sector. */ + if (dev->file->size != GRUB_FILE_SIZE_UNKNOWN) + disk->total_sectors = ((dev->file->size + GRUB_DISK_SECTOR_SIZE - 1) +@@ -182,6 +190,15 @@ grub_loopback_open (const char *name, grub_disk_t disk) + return 0; + } + ++static void ++grub_loopback_close (grub_disk_t disk) ++{ ++ struct grub_loopback *dev = disk->data; ++ ++ if (grub_sub (dev->refcnt, 1, &dev->refcnt)) ++ grub_fatal ("Reference count underflow"); ++} ++ + static grub_err_t + grub_loopback_read (grub_disk_t disk, grub_disk_addr_t sector, + grub_size_t size, char *buf) +@@ -224,6 +241,7 @@ static struct grub_disk_dev grub_loopback_dev = + .id = GRUB_DISK_DEVICE_LOOPBACK_ID, + .iterate = grub_loopback_iterate, + .open = grub_loopback_open, ++ .close = grub_loopback_close, + .read = grub_loopback_read, + .write = grub_loopback_write, + .next = 0 +diff --git a/include/grub/err.h b/include/grub/err.h +index 9b830757d..1b5335610 100644 +--- a/include/grub/err.h ++++ b/include/grub/err.h +@@ -71,7 +71,8 @@ typedef enum + GRUB_ERR_NET_PACKET_TOO_BIG, + GRUB_ERR_NET_NO_DOMAIN, + GRUB_ERR_EOF, +- GRUB_ERR_BAD_SIGNATURE ++ GRUB_ERR_BAD_SIGNATURE, ++ GRUB_ERR_STILL_REFERENCED + } + grub_err_t; + diff --git a/0615-kern-disk-Limit-recursion-depth.patch b/0615-kern-disk-Limit-recursion-depth.patch new file mode 100644 index 0000000..eb518e5 --- /dev/null +++ b/0615-kern-disk-Limit-recursion-depth.patch @@ -0,0 +1,120 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: B Horn +Date: Sun, 12 May 2024 04:09:24 +0100 +Subject: [PATCH] kern/disk: Limit recursion depth + +The grub_disk_read() may trigger other disk reads, e.g. via loopbacks. +This may lead to very deep recursion which can corrupt the heap. So, fix +the issue by limiting reads depth. + +Reported-by: B Horn +Signed-off-by: B Horn +Reviewed-by: Daniel Kiper +--- + grub-core/kern/disk.c | 27 ++++++++++++++++++++------- + include/grub/err.h | 3 ++- + 2 files changed, 22 insertions(+), 8 deletions(-) + +diff --git a/grub-core/kern/disk.c b/grub-core/kern/disk.c +index 7f58c5614..62d6a85cc 100644 +--- a/grub-core/kern/disk.c ++++ b/grub-core/kern/disk.c +@@ -28,6 +28,10 @@ + + #define GRUB_CACHE_TIMEOUT 2 + ++/* Disk reads may trigger other disk reads. So, limit recursion depth. */ ++#define MAX_READ_RECURSION_DEPTH 16 ++static unsigned int read_recursion_depth = 0; ++ + /* The last time the disk was used. */ + static grub_uint64_t grub_last_time = 0; + +@@ -417,6 +421,8 @@ grub_err_t + grub_disk_read (grub_disk_t disk, grub_disk_addr_t sector, + grub_off_t offset, grub_size_t size, void *buf) + { ++ grub_err_t err = GRUB_ERR_NONE; ++ + /* First of all, check if the region is within the disk. */ + if (grub_disk_adjust_range (disk, §or, &offset, size) != GRUB_ERR_NONE) + { +@@ -427,12 +433,17 @@ grub_disk_read (grub_disk_t disk, grub_disk_addr_t sector, + return grub_errno; + } + ++ if (++read_recursion_depth >= MAX_READ_RECURSION_DEPTH) ++ { ++ grub_error (GRUB_ERR_RECURSION_DEPTH, "grub_disk_read(): Maximum recursion depth exceeded"); ++ goto error; ++ } ++ + /* First read until first cache boundary. */ + if (offset || (sector & (GRUB_DISK_CACHE_SIZE - 1))) + { + grub_disk_addr_t start_sector; + grub_size_t pos; +- grub_err_t err; + grub_size_t len; + + start_sector = sector & ~((grub_disk_addr_t) GRUB_DISK_CACHE_SIZE - 1); +@@ -444,7 +455,7 @@ grub_disk_read (grub_disk_t disk, grub_disk_addr_t sector, + err = grub_disk_read_small (disk, start_sector, + offset + pos, len, buf); + if (err) +- return err; ++ goto error; + buf = (char *) buf + len; + size -= len; + offset += len; +@@ -457,7 +468,6 @@ grub_disk_read (grub_disk_t disk, grub_disk_addr_t sector, + { + char *data = NULL; + grub_disk_addr_t agglomerate; +- grub_err_t err; + + /* agglomerate read until we find a first cached entry. */ + for (agglomerate = 0; agglomerate +@@ -493,7 +503,7 @@ grub_disk_read (grub_disk_t disk, grub_disk_addr_t sector, + - disk->log_sector_size), + buf); + if (err) +- return err; ++ goto error; + + for (i = 0; i < agglomerate; i ++) + grub_disk_cache_store (disk->dev->id, disk->id, +@@ -527,13 +537,16 @@ grub_disk_read (grub_disk_t disk, grub_disk_addr_t sector, + /* And now read the last part. */ + if (size) + { +- grub_err_t err; + err = grub_disk_read_small (disk, sector, 0, size, buf); + if (err) +- return err; ++ goto error; + } + +- return grub_errno; ++ err = grub_errno; ++ ++ error: ++ read_recursion_depth--; ++ return err; + } + + grub_uint64_t +diff --git a/include/grub/err.h b/include/grub/err.h +index 1b5335610..670d40967 100644 +--- a/include/grub/err.h ++++ b/include/grub/err.h +@@ -72,7 +72,8 @@ typedef enum + GRUB_ERR_NET_NO_DOMAIN, + GRUB_ERR_EOF, + GRUB_ERR_BAD_SIGNATURE, +- GRUB_ERR_STILL_REFERENCED ++ GRUB_ERR_STILL_REFERENCED, ++ GRUB_ERR_RECURSION_DEPTH + } + grub_err_t; + diff --git a/0616-kern-partition-Limit-recursion-in-part_iterate.patch b/0616-kern-partition-Limit-recursion-in-part_iterate.patch new file mode 100644 index 0000000..60b0255 --- /dev/null +++ b/0616-kern-partition-Limit-recursion-in-part_iterate.patch @@ -0,0 +1,44 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: B Horn +Date: Sat, 16 Nov 2024 21:24:19 +0000 +Subject: [PATCH] kern/partition: Limit recursion in part_iterate() + +The part_iterate() is used by grub_partition_iterate() as a callback in +the partition iterate functions. However, part_iterate() may also call +the partition iterate functions which may lead to recursion. Fix potential +issue by limiting the recursion depth. + +Signed-off-by: B Horn +Reviewed-by: Daniel Kiper +--- + grub-core/kern/partition.c | 10 +++++++++- + 1 file changed, 9 insertions(+), 1 deletion(-) + +diff --git a/grub-core/kern/partition.c b/grub-core/kern/partition.c +index 3068c4dca..f3f125e75 100644 +--- a/grub-core/kern/partition.c ++++ b/grub-core/kern/partition.c +@@ -28,6 +28,9 @@ + + grub_partition_map_t grub_partition_map_list; + ++#define MAX_RECURSION_DEPTH 32 ++static unsigned int recursion_depth = 0; ++ + /* + * Checks that disk->partition contains part. This function assumes that the + * start of part is relative to the start of disk->partition. Returns 1 if +@@ -208,7 +211,12 @@ part_iterate (grub_disk_t dsk, const grub_partition_t partition, void *data) + FOR_PARTITION_MAPS(partmap) + { + grub_err_t err; +- err = partmap->iterate (dsk, part_iterate, ctx); ++ recursion_depth++; ++ if (recursion_depth <= MAX_RECURSION_DEPTH) ++ err = partmap->iterate (dsk, part_iterate, ctx); ++ else ++ err = grub_error (GRUB_ERR_RECURSION_DEPTH, "maximum recursion depth exceeded"); ++ recursion_depth--; + if (err) + grub_errno = GRUB_ERR_NONE; + if (ctx->ret) diff --git a/0617-script-execute-Limit-the-recursion-depth.patch b/0617-script-execute-Limit-the-recursion-depth.patch new file mode 100644 index 0000000..d4ab505 --- /dev/null +++ b/0617-script-execute-Limit-the-recursion-depth.patch @@ -0,0 +1,55 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: B Horn +Date: Thu, 18 Apr 2024 19:04:13 +0100 +Subject: [PATCH] script/execute: Limit the recursion depth + +If unbounded recursion is allowed it becomes possible to collide the +stack with the heap. As UEFI firmware often lacks guard pages this +becomes an exploitable issue as it is possible in some cases to do +a controlled overwrite of a section of this heap region with +arbitrary data. + +Reported-by: B Horn +Signed-off-by: B Horn +Reviewed-by: Daniel Kiper +--- + grub-core/script/execute.c | 14 ++++++++++++++ + 1 file changed, 14 insertions(+) + +diff --git a/grub-core/script/execute.c b/grub-core/script/execute.c +index 266d99ed3..ef9a01642 100644 +--- a/grub-core/script/execute.c ++++ b/grub-core/script/execute.c +@@ -36,10 +36,18 @@ + is sizeof (int) * 3, and one extra for a possible -ve sign. */ + #define ERRNO_DIGITS_MAX (sizeof (int) * 3 + 1) + ++/* ++ * A limit on recursion, to avoid colliding with the heap. UEFI defines a baseline ++ * stack size of 128 KiB. So, assuming at most 1-2 KiB per iteration this should ++ * keep us safe. ++ */ ++#define MAX_RECURSION_DEPTH 64 ++ + static unsigned long is_continue; + static unsigned long active_loops; + static unsigned long active_breaks; + static unsigned long function_return; ++static unsigned long recursion_depth; + + #define GRUB_SCRIPT_SCOPE_MALLOCED 1 + #define GRUB_SCRIPT_SCOPE_ARGS_MALLOCED 2 +@@ -850,7 +858,13 @@ grub_script_execute_cmd (struct grub_script_cmd *cmd) + if (cmd == 0) + return 0; + ++ recursion_depth++; ++ ++ if (recursion_depth >= MAX_RECURSION_DEPTH) ++ return grub_error (GRUB_ERR_RECURSION_DEPTH, N_("maximum recursion depth exceeded")); ++ + ret = cmd->exec (cmd); ++ recursion_depth--; + + grub_snprintf (errnobuf, sizeof (errnobuf), "%d", ret); + grub_env_set ("?", errnobuf); diff --git a/0618-net-Unregister-net_default_ip-and-net_default_mac-va.patch b/0618-net-Unregister-net_default_ip-and-net_default_mac-va.patch new file mode 100644 index 0000000..07ae790 --- /dev/null +++ b/0618-net-Unregister-net_default_ip-and-net_default_mac-va.patch @@ -0,0 +1,29 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: B Horn +Date: Thu, 28 Nov 2024 04:05:04 +0000 +Subject: [PATCH] net: Unregister net_default_ip and net_default_mac variables + hooks on unload + +The net module is a dependency of normal. So, it shouldn't be possible +to unload the net. Though unregister variables hooks as a precaution. +It also gets in line with unregistering the other net module hooks. + +Signed-off-by: B Horn +Reviewed-by: Daniel Kiper +--- + grub-core/net/net.c | 2 ++ + 1 file changed, 2 insertions(+) + +diff --git a/grub-core/net/net.c b/grub-core/net/net.c +index b9e2a4d10..c78c8694b 100644 +--- a/grub-core/net/net.c ++++ b/grub-core/net/net.c +@@ -2161,6 +2161,8 @@ GRUB_MOD_FINI(net) + + grub_register_variable_hook ("net_default_server", 0, 0); + grub_register_variable_hook ("pxe_default_server", 0, 0); ++ grub_register_variable_hook ("net_default_ip", 0, 0); ++ grub_register_variable_hook ("net_default_mac", 0, 0); + + grub_bootp_fini (); + grub_dns_fini (); diff --git a/0619-net-Remove-variables-hooks-when-interface-is-unregis.patch b/0619-net-Remove-variables-hooks-when-interface-is-unregis.patch new file mode 100644 index 0000000..c93a810 --- /dev/null +++ b/0619-net-Remove-variables-hooks-when-interface-is-unregis.patch @@ -0,0 +1,85 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: B Horn +Date: Fri, 1 Nov 2024 23:49:48 +0000 +Subject: [PATCH] net: Remove variables hooks when interface is unregisted + +The grub_net_network_level_interface_unregister(), previously +implemented in a header, did not remove the variables hooks that +were registered in grub_net_network_level_interface_register(). +Fix this by implementing the same logic used to register the +variables and move the function into the grub-core/net/net.c. + +Signed-off-by: B Horn +Reviewed-by: Daniel Kiper +--- + grub-core/net/net.c | 34 ++++++++++++++++++++++++++++++++++ + include/grub/net.h | 11 +---------- + 2 files changed, 35 insertions(+), 10 deletions(-) + +diff --git a/grub-core/net/net.c b/grub-core/net/net.c +index c78c8694b..69223164d 100644 +--- a/grub-core/net/net.c ++++ b/grub-core/net/net.c +@@ -1051,6 +1051,40 @@ grub_net_add_ipv6_local (struct grub_net_network_level_interface *inter, + return 0; + } + ++void ++grub_net_network_level_interface_unregister (struct grub_net_network_level_interface *inter) ++{ ++ char *name; ++ ++ { ++ char buf[GRUB_NET_MAX_STR_HWADDR_LEN]; ++ ++ grub_net_hwaddr_to_str (&inter->hwaddress, buf); ++ name = grub_xasprintf ("net_%s_mac", inter->name); ++ if (name != NULL) ++ grub_register_variable_hook (name, NULL, NULL); ++ grub_free (name); ++ } ++ ++ { ++ char buf[GRUB_NET_MAX_STR_ADDR_LEN]; ++ ++ grub_net_addr_to_str (&inter->address, buf); ++ name = grub_xasprintf ("net_%s_ip", inter->name); ++ if (name != NULL) ++ grub_register_variable_hook (name, NULL, NULL); ++ grub_free (name); ++ } ++ ++ inter->card->num_ifaces--; ++ *inter->prev = inter->next; ++ if (inter->next) ++ inter->next->prev = inter->prev; ++ inter->next = 0; ++ inter->prev = 0; ++} ++ ++ + grub_err_t + grub_net_add_ipv4_local (struct grub_net_network_level_interface *inter, + int mask) +diff --git a/include/grub/net.h b/include/grub/net.h +index 0d31f0066..4a815fa91 100644 +--- a/include/grub/net.h ++++ b/include/grub/net.h +@@ -607,16 +607,7 @@ void grub_bootp_fini (void); + void grub_dns_init (void); + void grub_dns_fini (void); + +-static inline void +-grub_net_network_level_interface_unregister (struct grub_net_network_level_interface *inter) +-{ +- inter->card->num_ifaces--; +- *inter->prev = inter->next; +- if (inter->next) +- inter->next->prev = inter->prev; +- inter->next = 0; +- inter->prev = 0; +-} ++void grub_net_network_level_interface_unregister (struct grub_net_network_level_interface *inter); + + void + grub_net_tcp_retransmit (void); diff --git a/0620-net-Fix-OOB-write-in-grub_net_search_config_file.patch b/0620-net-Fix-OOB-write-in-grub_net_search_config_file.patch new file mode 100644 index 0000000..b6afa35 --- /dev/null +++ b/0620-net-Fix-OOB-write-in-grub_net_search_config_file.patch @@ -0,0 +1,80 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: B Horn +Date: Fri, 15 Nov 2024 13:12:09 +0000 +Subject: [PATCH] net: Fix OOB write in grub_net_search_config_file() + +The function included a call to grub_strcpy() which copied data from an +environment variable to a buffer allocated in grub_cmd_normal(). The +grub_cmd_normal() didn't consider the length of the environment variable. +So, the copy operation could exceed the allocation and lead to an OOB +write. Fix the issue by replacing grub_strcpy() with grub_strlcpy() and +pass the underlying buffers size to the grub_net_search_config_file(). + +Fixes: CVE-2025-0624 + +Signed-off-by: B Horn +Reviewed-by: Daniel Kiper +--- + grub-core/net/net.c | 7 ++++--- + grub-core/normal/main.c | 2 +- + include/grub/net.h | 2 +- + 3 files changed, 6 insertions(+), 5 deletions(-) + +diff --git a/grub-core/net/net.c b/grub-core/net/net.c +index 69223164d..f0896979d 100644 +--- a/grub-core/net/net.c ++++ b/grub-core/net/net.c +@@ -1940,9 +1940,9 @@ grub_net_restore_hw (void) + } + + grub_err_t +-grub_net_search_configfile (char *config) ++grub_net_search_configfile (char *config, grub_size_t config_buf_len) + { +- grub_size_t config_len; ++ grub_size_t config_len, suffix_len; + char *suffix; + + auto int search_through (grub_size_t num_tries, grub_size_t slice_size); +@@ -1979,6 +1979,7 @@ grub_net_search_configfile (char *config) + config_len = grub_strlen (config); + config[config_len] = '-'; + suffix = config + config_len + 1; ++ suffix_len = config_buf_len - (config_len + 1); + + struct grub_net_network_level_interface *inf; + FOR_NET_NETWORK_LEVEL_INTERFACES (inf) +@@ -2004,7 +2005,7 @@ grub_net_search_configfile (char *config) + + if (client_uuid) + { +- grub_strcpy (suffix, client_uuid); ++ grub_strlcpy (suffix, client_uuid, suffix_len); + if (search_through (1, 0) == 0) return GRUB_ERR_NONE; + } + +diff --git a/grub-core/normal/main.c b/grub-core/normal/main.c +index 93f33c167..f5e9b54f7 100644 +--- a/grub-core/normal/main.c ++++ b/grub-core/normal/main.c +@@ -349,7 +349,7 @@ grub_try_normal_prefix (const char *prefix) + return err; + + grub_snprintf (config, config_len, "%s/grub.cfg", prefix); +- err = grub_net_search_configfile (config); ++ err = grub_net_search_configfile (config, config_len); + } + + if (err != GRUB_ERR_NONE) +diff --git a/include/grub/net.h b/include/grub/net.h +index 4a815fa91..8c8521944 100644 +--- a/include/grub/net.h ++++ b/include/grub/net.h +@@ -646,6 +646,6 @@ extern char *grub_net_default_server; + #define VLANTAG_IDENTIFIER 0x8100 + + grub_err_t +-grub_net_search_configfile (char *config); ++grub_net_search_configfile (char *config, grub_size_t config_buf_len); + + #endif /* ! GRUB_NET_HEADER */ diff --git a/0621-net-tftp-Fix-stack-buffer-overflow-in-tftp_open.patch b/0621-net-tftp-Fix-stack-buffer-overflow-in-tftp_open.patch new file mode 100644 index 0000000..8edb2ff --- /dev/null +++ b/0621-net-tftp-Fix-stack-buffer-overflow-in-tftp_open.patch @@ -0,0 +1,114 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: B Horn +Date: Thu, 18 Apr 2024 17:32:34 +0100 +Subject: [PATCH] net/tftp: Fix stack buffer overflow in tftp_open() + +An overly long filename can be passed to tftp_open() which would cause +grub_normalize_filename() to write out of bounds. + +Fixed by adding an extra argument to grub_normalize_filename() for the +space available, making it act closer to a strlcpy(). As several fixed +strings are strcpy()'d after into the same buffer, their total length is +checked to see if they exceed the remaining space in the buffer. If so, +return an error. + +On the occasion simplify code a bit by removing unneeded rrqlen zeroing. + +Reported-by: B Horn +Signed-off-by: B Horn +Reviewed-by: Daniel Kiper +--- + grub-core/net/tftp.c | 40 +++++++++++++++++++++++++--------------- + 1 file changed, 25 insertions(+), 15 deletions(-) + +diff --git a/grub-core/net/tftp.c b/grub-core/net/tftp.c +index 09e1511cc..9bc8a688d 100644 +--- a/grub-core/net/tftp.c ++++ b/grub-core/net/tftp.c +@@ -267,17 +267,19 @@ tftp_receive (grub_net_udp_socket_t sock __attribute__ ((unused)), + * forward slashes to a single forward slash. + */ + static void +-grub_normalize_filename (char *normalized, const char *filename) ++grub_normalize_filename (char *normalized, const char *filename, int c) + { + char *dest = normalized; + const char *src = filename; + +- while (*src != '\0') ++ while (*src != '\0' && c > 0) + { + if (src[0] == '/' && src[1] == '/') + src++; +- else +- *dest++ = *src++; ++ else { ++ c--; ++ *dest++ = *src++; ++ } + } + *dest = '\0'; + } +@@ -288,7 +290,7 @@ tftp_open (struct grub_file *file, const char *filename) + struct tftphdr *tftph; + char *rrq; + int i; +- int rrqlen; ++ int rrqlen, rrqsize; + int hdrlen; + grub_uint8_t open_data[1500]; + struct grub_net_buff nb; +@@ -316,35 +318,43 @@ tftp_open (struct grub_file *file, const char *filename) + + tftph = (struct tftphdr *) nb.data; + +- rrq = (char *) tftph->u.rrq; +- rrqlen = 0; +- + tftph->opcode = grub_cpu_to_be16_compile_time (TFTP_RRQ); + ++ rrq = (char *) tftph->u.rrq; ++ rrqsize = sizeof (tftph->u.rrq); ++ + /* Copy and normalize the filename to work-around issues on some tftp + servers when file names are being matched for remapping. */ +- grub_normalize_filename (rrq, filename); +- rrqlen += grub_strlen (rrq) + 1; ++ grub_normalize_filename (rrq, filename, rrqsize); ++ ++ rrqlen = grub_strlen (rrq) + 1; + rrq += grub_strlen (rrq) + 1; + +- grub_strcpy (rrq, "octet"); ++ /* Verify there is enough space for the remaining components. */ + rrqlen += grub_strlen ("octet") + 1; ++ rrqlen += grub_strlen ("blksize") + 1; ++ rrqlen += grub_strlen ("1024") + 1; ++ rrqlen += grub_strlen ("tsize") + 1; ++ rrqlen += grub_strlen ("0") + 1; ++ ++ if (rrqlen >= rrqsize) { ++ grub_free (data); ++ return grub_error (GRUB_ERR_BAD_FILENAME, N_("filename too long")); ++ } ++ ++ grub_strcpy (rrq, "octet"); + rrq += grub_strlen ("octet") + 1; + + grub_strcpy (rrq, "blksize"); +- rrqlen += grub_strlen ("blksize") + 1; + rrq += grub_strlen ("blksize") + 1; + + grub_strcpy (rrq, "1024"); +- rrqlen += grub_strlen ("1024") + 1; + rrq += grub_strlen ("1024") + 1; + + grub_strcpy (rrq, "tsize"); +- rrqlen += grub_strlen ("tsize") + 1; + rrq += grub_strlen ("tsize") + 1; + + grub_strcpy (rrq, "0"); +- rrqlen += grub_strlen ("0") + 1; + rrq += grub_strlen ("0") + 1; + hdrlen = sizeof (tftph->opcode) + rrqlen; + diff --git a/0622-video-readers-jpeg-Do-not-permit-duplicate-SOF0-mark.patch b/0622-video-readers-jpeg-Do-not-permit-duplicate-SOF0-mark.patch new file mode 100644 index 0000000..4c680f6 --- /dev/null +++ b/0622-video-readers-jpeg-Do-not-permit-duplicate-SOF0-mark.patch @@ -0,0 +1,33 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Daniel Axtens +Date: Fri, 8 Mar 2024 22:47:20 +1100 +Subject: [PATCH] video/readers/jpeg: Do not permit duplicate SOF0 markers in + JPEG + +Otherwise a subsequent header could change the height and width +allowing future OOB writes. + +Fixes: CVE-2024-45774 + +Reported-by: Nils Langius +Signed-off-by: Daniel Axtens +Reviewed-by: Daniel Kiper +--- + grub-core/video/readers/jpeg.c | 4 ++++ + 1 file changed, 4 insertions(+) + +diff --git a/grub-core/video/readers/jpeg.c b/grub-core/video/readers/jpeg.c +index 2da04094b..c7aaac362 100644 +--- a/grub-core/video/readers/jpeg.c ++++ b/grub-core/video/readers/jpeg.c +@@ -332,6 +332,10 @@ grub_jpeg_decode_sof (struct grub_jpeg_data *data) + if (grub_errno != GRUB_ERR_NONE) + return grub_errno; + ++ if (data->image_height != 0 || data->image_width != 0) ++ return grub_error (GRUB_ERR_BAD_FILE_TYPE, ++ "jpeg: cannot have duplicate SOF0 markers"); ++ + if (grub_jpeg_get_byte (data) != 8) + return grub_error (GRUB_ERR_BAD_FILE_TYPE, + "jpeg: only 8-bit precision is supported"); diff --git a/0623-kern-dl-Fix-for-an-integer-overflow-in-grub_dl_ref.patch b/0623-kern-dl-Fix-for-an-integer-overflow-in-grub_dl_ref.patch new file mode 100644 index 0000000..0b40185 --- /dev/null +++ b/0623-kern-dl-Fix-for-an-integer-overflow-in-grub_dl_ref.patch @@ -0,0 +1,138 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: B Horn +Date: Thu, 18 Apr 2024 15:59:26 +0100 +Subject: [PATCH] kern/dl: Fix for an integer overflow in grub_dl_ref() + +It was possible to overflow the value of mod->ref_count, a signed +integer, by repeatedly invoking insmod on an already loaded module. +This led to a use-after-free. As once ref_count was overflowed it became +possible to unload the module while there was still references to it. + +This resolves the issue by using grub_add() to check if the ref_count +will overflow and then stops further increments. Further changes were +also made to grub_dl_unref() to check for the underflow condition and +the reference count was changed to an unsigned 64-bit integer. + +Reported-by: B Horn +Signed-off-by: B Horn +Reviewed-by: Daniel Kiper +--- + grub-core/commands/minicmd.c | 2 +- + grub-core/kern/dl.c | 17 ++++++++++++----- + include/grub/dl.h | 8 ++++---- + util/misc.c | 4 ++-- + 4 files changed, 19 insertions(+), 12 deletions(-) + +diff --git a/grub-core/commands/minicmd.c b/grub-core/commands/minicmd.c +index 2bd3ac76f..2001043cf 100644 +--- a/grub-core/commands/minicmd.c ++++ b/grub-core/commands/minicmd.c +@@ -167,7 +167,7 @@ grub_mini_cmd_lsmod (struct grub_command *cmd __attribute__ ((unused)), + { + grub_dl_dep_t dep; + +- grub_printf ("%s\t%d\t\t", mod->name, mod->ref_count); ++ grub_printf ("%s\t%" PRIuGRUB_UINT64_T "\t\t", mod->name, mod->ref_count); + for (dep = mod->dep; dep; dep = dep->next) + { + if (dep != mod->dep) +diff --git a/grub-core/kern/dl.c b/grub-core/kern/dl.c +index 68d3177f5..edbb55d7d 100644 +--- a/grub-core/kern/dl.c ++++ b/grub-core/kern/dl.c +@@ -34,6 +34,7 @@ + #include + #include + #include ++#include + + /* Platforms where modules are in a readonly area of memory. */ + #if defined(GRUB_MACHINE_QEMU) +@@ -595,7 +596,7 @@ grub_dl_resolve_dependencies (grub_dl_t mod, Elf_Ehdr *e) + return GRUB_ERR_NONE; + } + +-int ++grub_uint64_t + grub_dl_ref (grub_dl_t mod) + { + grub_dl_dep_t dep; +@@ -606,10 +607,13 @@ grub_dl_ref (grub_dl_t mod) + for (dep = mod->dep; dep; dep = dep->next) + grub_dl_ref (dep->mod); + +- return ++mod->ref_count; ++ if (grub_add (mod->ref_count, 1, &mod->ref_count)) ++ grub_fatal ("Module reference count overflow"); ++ ++ return mod->ref_count; + } + +-int ++grub_uint64_t + grub_dl_unref (grub_dl_t mod) + { + grub_dl_dep_t dep; +@@ -620,10 +624,13 @@ grub_dl_unref (grub_dl_t mod) + for (dep = mod->dep; dep; dep = dep->next) + grub_dl_unref (dep->mod); + +- return --mod->ref_count; ++ if (grub_sub (mod->ref_count, 1, &mod->ref_count)) ++ grub_fatal ("Module reference count underflow"); ++ ++ return mod->ref_count; + } + +-int ++grub_uint64_t + grub_dl_ref_count (grub_dl_t mod) + { + if (mod == NULL) +diff --git a/include/grub/dl.h b/include/grub/dl.h +index 6f46b7e86..2e27ef596 100644 +--- a/include/grub/dl.h ++++ b/include/grub/dl.h +@@ -177,7 +177,7 @@ typedef struct grub_dl_dep *grub_dl_dep_t; + struct grub_dl + { + char *name; +- int ref_count; ++ grub_uint64_t ref_count; + int persistent; + grub_dl_dep_t dep; + grub_dl_segment_t segment; +@@ -207,9 +207,9 @@ grub_dl_t grub_dl_load_core (void *addr, grub_size_t size); + grub_dl_t EXPORT_FUNC(grub_dl_load_core_noinit) (void *addr, grub_size_t size); + int EXPORT_FUNC(grub_dl_unload) (grub_dl_t mod); + extern void grub_dl_unload_unneeded (void); +-extern int EXPORT_FUNC(grub_dl_ref) (grub_dl_t mod); +-extern int EXPORT_FUNC(grub_dl_unref) (grub_dl_t mod); +-extern int EXPORT_FUNC(grub_dl_ref_count) (grub_dl_t mod); ++extern grub_uint64_t EXPORT_FUNC(grub_dl_ref) (grub_dl_t mod); ++extern grub_uint64_t EXPORT_FUNC(grub_dl_unref) (grub_dl_t mod); ++extern grub_uint64_t EXPORT_FUNC(grub_dl_ref_count) (grub_dl_t mod); + + extern grub_dl_t EXPORT_VAR(grub_dl_head); + +diff --git a/util/misc.c b/util/misc.c +index d545212d9..0f928e5b4 100644 +--- a/util/misc.c ++++ b/util/misc.c +@@ -190,14 +190,14 @@ grub_xputs_real (const char *str) + + void (*grub_xputs) (const char *str) = grub_xputs_real; + +-int ++grub_uint64_t + grub_dl_ref (grub_dl_t mod) + { + (void) mod; + return 0; + } + +-int ++grub_uint64_t + grub_dl_unref (grub_dl_t mod) + { + (void) mod; diff --git a/0624-kern-dl-Check-for-the-SHF_INFO_LINK-flag-in-grub_dl_.patch b/0624-kern-dl-Check-for-the-SHF_INFO_LINK-flag-in-grub_dl_.patch new file mode 100644 index 0000000..9479ec8 --- /dev/null +++ b/0624-kern-dl-Check-for-the-SHF_INFO_LINK-flag-in-grub_dl_.patch @@ -0,0 +1,34 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: B Horn +Date: Thu, 7 Nov 2024 06:00:36 +0000 +Subject: [PATCH] kern/dl: Check for the SHF_INFO_LINK flag in + grub_dl_relocate_symbols() + +The grub_dl_relocate_symbols() iterates through the sections in +an ELF looking for relocation sections. According to the spec [1] +the SHF_INFO_LINK flag should be set if the sh_info field is meant +to be a section index. + +[1] https://refspecs.linuxbase.org/elf/gabi4+/ch4.sheader.html + +Reported-by: B Horn +Signed-off-by: B Horn +Reviewed-by: Daniel Kiper +--- + grub-core/kern/dl.c | 3 +++ + 1 file changed, 3 insertions(+) + +diff --git a/grub-core/kern/dl.c b/grub-core/kern/dl.c +index edbb55d7d..e93863196 100644 +--- a/grub-core/kern/dl.c ++++ b/grub-core/kern/dl.c +@@ -663,6 +663,9 @@ grub_dl_relocate_symbols (grub_dl_t mod, void *ehdr) + grub_dl_segment_t seg; + grub_err_t err; + ++ if (!(s->sh_flags & SHF_INFO_LINK)) ++ continue; ++ + seg = grub_dl_find_segment(mod, s->sh_info); + if (!seg) + continue; diff --git a/0625-commands-extcmd-Missing-check-for-failed-allocation.patch b/0625-commands-extcmd-Missing-check-for-failed-allocation.patch new file mode 100644 index 0000000..44e8c91 --- /dev/null +++ b/0625-commands-extcmd-Missing-check-for-failed-allocation.patch @@ -0,0 +1,34 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Lidong Chen +Date: Fri, 22 Nov 2024 06:27:55 +0000 +Subject: [PATCH] commands/extcmd: Missing check for failed allocation + +The grub_extcmd_dispatcher() calls grub_arg_list_alloc() to allocate +a grub_arg_list struct but it does not verify the allocation was successful. +In case of failed allocation the NULL state pointer can be accessed in +parse_option() through grub_arg_parse() which may lead to a security issue. + +Fixes: CVE-2024-45775 + +Reported-by: Nils Langius +Signed-off-by: Lidong Chen +Reviewed-by: Daniel Kiper +Reviewed-by: Alec Brown +--- + grub-core/commands/extcmd.c | 3 +++ + 1 file changed, 3 insertions(+) + +diff --git a/grub-core/commands/extcmd.c b/grub-core/commands/extcmd.c +index 90a5ca24a..c236be13a 100644 +--- a/grub-core/commands/extcmd.c ++++ b/grub-core/commands/extcmd.c +@@ -49,6 +49,9 @@ grub_extcmd_dispatcher (struct grub_command *cmd, int argc, char **args, + } + + state = grub_arg_list_alloc (ext, argc, args); ++ if (state == NULL) ++ return grub_errno; ++ + if (grub_arg_parse (ext, argc, args, state, &new_args, &new_argc)) + { + context.state = state; diff --git a/0626-commands-ls-Fix-NULL-dereference.patch b/0626-commands-ls-Fix-NULL-dereference.patch new file mode 100644 index 0000000..9cca39c --- /dev/null +++ b/0626-commands-ls-Fix-NULL-dereference.patch @@ -0,0 +1,32 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: B Horn +Date: Sun, 12 May 2024 11:08:23 +0100 +Subject: [PATCH] commands/ls: Fix NULL dereference + +The grub_strrchr() may return NULL when the dirname do not contain "/". +This can happen on broken filesystems. + +Reported-by: B Horn +Signed-off-by: B Horn +Reviewed-by: Daniel Kiper +--- + grub-core/commands/ls.c | 6 +++++- + 1 file changed, 5 insertions(+), 1 deletion(-) + +diff --git a/grub-core/commands/ls.c b/grub-core/commands/ls.c +index d4dcffd31..ac6936bc7 100644 +--- a/grub-core/commands/ls.c ++++ b/grub-core/commands/ls.c +@@ -241,7 +241,11 @@ grub_ls_list_files (char *dirname, int longlist, int all, int human) + + grub_file_close (file); + +- p = grub_strrchr (dirname, '/') + 1; ++ p = grub_strrchr (dirname, '/'); ++ if (p == NULL) ++ goto fail; ++ ++p; ++ + dirname = grub_strndup (dirname, p - dirname); + if (! dirname) + goto fail; diff --git a/0627-commands-pgp-Unregister-the-check_signatures-hooks-o.patch b/0627-commands-pgp-Unregister-the-check_signatures-hooks-o.patch new file mode 100644 index 0000000..9db341a --- /dev/null +++ b/0627-commands-pgp-Unregister-the-check_signatures-hooks-o.patch @@ -0,0 +1,31 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: B Horn +Date: Fri, 1 Nov 2024 19:24:29 +0000 +Subject: [PATCH] commands/pgp: Unregister the "check_signatures" hooks on + module unload + +If the hooks are not removed they can be called after the module has +been unloaded leading to an use-after-free. + +Fixes: CVE-2025-0622 + +Reported-by: B Horn +Signed-off-by: B Horn +Reviewed-by: Daniel Kiper +--- + grub-core/commands/pgp.c | 2 ++ + 1 file changed, 2 insertions(+) + +diff --git a/grub-core/commands/pgp.c b/grub-core/commands/pgp.c +index 55d354be0..15e981304 100644 +--- a/grub-core/commands/pgp.c ++++ b/grub-core/commands/pgp.c +@@ -982,6 +982,8 @@ GRUB_MOD_INIT(pgp) + + GRUB_MOD_FINI(pgp) + { ++ grub_register_variable_hook ("check_signatures", NULL, NULL); ++ grub_env_unset ("check_signatures"); + grub_verifier_unregister (&grub_pubkey_verifier); + grub_unregister_extcmd (cmd); + grub_unregister_extcmd (cmd_trust); diff --git a/0628-normal-Remove-variables-hooks-on-module-unload.patch b/0628-normal-Remove-variables-hooks-on-module-unload.patch new file mode 100644 index 0000000..c2b2336 --- /dev/null +++ b/0628-normal-Remove-variables-hooks-on-module-unload.patch @@ -0,0 +1,37 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: B Horn +Date: Fri, 1 Nov 2024 23:46:55 +0000 +Subject: [PATCH] normal: Remove variables hooks on module unload + +The normal module does not entirely cleanup after itself in +its GRUB_MOD_FINI() leaving a few variables hooks in place. +It is not possible to unload normal module now but fix the +issues for completeness. + +On the occasion replace 0s with NULLs for "pager" variable +hooks unregister. + +Fixes: CVE-2025-0622 + +Reported-by: B Horn +Signed-off-by: B Horn +Reviewed-by: Daniel Kiper +--- + grub-core/normal/main.c | 4 +++- + 1 file changed, 3 insertions(+), 1 deletion(-) + +diff --git a/grub-core/normal/main.c b/grub-core/normal/main.c +index f5e9b54f7..b61f01ae5 100644 +--- a/grub-core/normal/main.c ++++ b/grub-core/normal/main.c +@@ -670,7 +670,9 @@ GRUB_MOD_FINI(normal) + grub_xputs = grub_xputs_saved; + + grub_set_history (0); +- grub_register_variable_hook ("pager", 0, 0); ++ grub_register_variable_hook ("pager", NULL, NULL); ++ grub_register_variable_hook ("color_normal", NULL, NULL); ++ grub_register_variable_hook ("color_highlight", NULL, NULL); + grub_fs_autoload_hook = 0; + grub_unregister_command (cmd_clear); + } diff --git a/0629-gettext-Remove-variables-hooks-on-module-unload.patch b/0629-gettext-Remove-variables-hooks-on-module-unload.patch new file mode 100644 index 0000000..58583aa --- /dev/null +++ b/0629-gettext-Remove-variables-hooks-on-module-unload.patch @@ -0,0 +1,34 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: B Horn +Date: Fri, 1 Nov 2024 23:52:06 +0000 +Subject: [PATCH] gettext: Remove variables hooks on module unload + +The gettext module does not entirely cleanup after itself in +its GRUB_MOD_FINI() leaving a few variables hooks in place. +It is not possible to unload gettext module because normal +module depends on it. Though fix the issues for completeness. + +Fixes: CVE-2025-0622 + +Reported-by: B Horn +Signed-off-by: B Horn +Reviewed-by: Daniel Kiper +--- + grub-core/gettext/gettext.c | 4 ++++ + 1 file changed, 4 insertions(+) + +diff --git a/grub-core/gettext/gettext.c b/grub-core/gettext/gettext.c +index 84d520cd4..1344c7c81 100644 +--- a/grub-core/gettext/gettext.c ++++ b/grub-core/gettext/gettext.c +@@ -520,6 +520,10 @@ GRUB_MOD_INIT (gettext) + + GRUB_MOD_FINI (gettext) + { ++ grub_register_variable_hook ("locale_dir", NULL, NULL); ++ grub_register_variable_hook ("secondary_locale_dir", NULL, NULL); ++ grub_register_variable_hook ("lang", NULL, NULL); ++ + grub_gettext_delete_list (&main_context); + grub_gettext_delete_list (&secondary_context); + diff --git a/0630-gettext-Integer-overflow-leads-to-heap-OOB-write-or-.patch b/0630-gettext-Integer-overflow-leads-to-heap-OOB-write-or-.patch new file mode 100644 index 0000000..3218850 --- /dev/null +++ b/0630-gettext-Integer-overflow-leads-to-heap-OOB-write-or-.patch @@ -0,0 +1,35 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Lidong Chen +Date: Fri, 22 Nov 2024 06:27:56 +0000 +Subject: [PATCH] gettext: Integer overflow leads to heap OOB write or read + +Calculation of ctx->grub_gettext_msg_list size in grub_mofile_open() may +overflow leading to subsequent OOB write or read. This patch fixes the +issue by replacing grub_zalloc() and explicit multiplication with +grub_calloc() which does the same thing in safe manner. + +Fixes: CVE-2024-45776 + +Reported-by: Nils Langius +Signed-off-by: Lidong Chen +Reviewed-by: Daniel Kiper +Reviewed-by: Alec Brown +--- + grub-core/gettext/gettext.c | 4 ++-- + 1 file changed, 2 insertions(+), 2 deletions(-) + +diff --git a/grub-core/gettext/gettext.c b/grub-core/gettext/gettext.c +index 1344c7c81..cb304ebeb 100644 +--- a/grub-core/gettext/gettext.c ++++ b/grub-core/gettext/gettext.c +@@ -323,8 +323,8 @@ grub_mofile_open (struct grub_gettext_context *ctx, + for (ctx->grub_gettext_max_log = 0; ctx->grub_gettext_max >> ctx->grub_gettext_max_log; + ctx->grub_gettext_max_log++); + +- ctx->grub_gettext_msg_list = grub_zalloc (ctx->grub_gettext_max +- * sizeof (ctx->grub_gettext_msg_list[0])); ++ ctx->grub_gettext_msg_list = grub_calloc (ctx->grub_gettext_max, ++ sizeof (ctx->grub_gettext_msg_list[0])); + if (!ctx->grub_gettext_msg_list) + { + grub_file_close (fd); diff --git a/0631-gettext-Integer-overflow-leads-to-heap-OOB-write.patch b/0631-gettext-Integer-overflow-leads-to-heap-OOB-write.patch new file mode 100644 index 0000000..0cdad3c --- /dev/null +++ b/0631-gettext-Integer-overflow-leads-to-heap-OOB-write.patch @@ -0,0 +1,53 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Lidong Chen +Date: Fri, 22 Nov 2024 06:27:57 +0000 +Subject: [PATCH] gettext: Integer overflow leads to heap OOB write + +The size calculation of the translation buffer in +grub_gettext_getstr_from_position() may overflow +to 0 leading to heap OOB write. This patch fixes +the issue by using grub_add() and checking for +an overflow. + +Fixes: CVE-2024-45777 + +Reported-by: Nils Langius +Signed-off-by: Lidong Chen +Reviewed-by: Daniel Kiper +Reviewed-by: Alec Brown +--- + grub-core/gettext/gettext.c | 7 ++++++- + 1 file changed, 6 insertions(+), 1 deletion(-) + +diff --git a/grub-core/gettext/gettext.c b/grub-core/gettext/gettext.c +index cb304ebeb..9654bb3fd 100644 +--- a/grub-core/gettext/gettext.c ++++ b/grub-core/gettext/gettext.c +@@ -26,6 +26,7 @@ + #include + #include + #include ++#include + + GRUB_MOD_LICENSE ("GPLv3+"); + +@@ -99,6 +100,7 @@ grub_gettext_getstr_from_position (struct grub_gettext_context *ctx, + char *translation; + struct string_descriptor desc; + grub_err_t err; ++ grub_size_t alloc_sz; + + internal_position = (off + position * sizeof (desc)); + +@@ -109,7 +111,10 @@ grub_gettext_getstr_from_position (struct grub_gettext_context *ctx, + length = grub_cpu_to_le32 (desc.length); + offset = grub_cpu_to_le32 (desc.offset); + +- translation = grub_malloc (length + 1); ++ if (grub_add (length, 1, &alloc_sz)) ++ return NULL; ++ ++ translation = grub_malloc (alloc_sz); + if (!translation) + return NULL; + diff --git a/0632-commands-read-Fix-an-integer-overflow-when-supplying.patch b/0632-commands-read-Fix-an-integer-overflow-when-supplying.patch new file mode 100644 index 0000000..37e352a --- /dev/null +++ b/0632-commands-read-Fix-an-integer-overflow-when-supplying.patch @@ -0,0 +1,69 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Jonathan Bar Or +Date: Thu, 23 Jan 2025 19:17:05 +0100 +Subject: [PATCH] commands/read: Fix an integer overflow when supplying more + than 2^31 characters + +The grub_getline() function currently has a signed integer variable "i" +that can be overflown when user supplies more than 2^31 characters. +It results in a memory corruption of the allocated line buffer as well +as supplying large negative values to grub_realloc(). + +Fixes: CVE-2025-0690 + +Reported-by: Jonathan Bar Or +Signed-off-by: Jonathan Bar Or +Reviewed-by: Daniel Kiper +--- + grub-core/commands/read.c | 20 ++++++++++++++++---- + 1 file changed, 16 insertions(+), 4 deletions(-) + +diff --git a/grub-core/commands/read.c b/grub-core/commands/read.c +index fe3e88b15..7b6735b79 100644 +--- a/grub-core/commands/read.c ++++ b/grub-core/commands/read.c +@@ -25,19 +25,21 @@ + #include + #include + #include ++#include + + GRUB_MOD_LICENSE ("GPLv3+"); + + static char * + grub_getline (void) + { +- int i; ++ grub_size_t i; + char *line; + char *tmp; + char c; ++ grub_size_t alloc_size; + + i = 0; +- line = grub_malloc (1 + i + sizeof('\0')); ++ line = grub_malloc (1 + sizeof('\0')); + if (! line) + return NULL; + +@@ -50,8 +52,18 @@ grub_getline (void) + line[i] = c; + if (grub_isprint (c)) + grub_printf ("%c", c); +- i++; +- tmp = grub_realloc (line, 1 + i + sizeof('\0')); ++ if (grub_add (i, 1, &i)) ++ { ++ grub_error (GRUB_ERR_OUT_OF_RANGE, N_("overflow is detected")); ++ return NULL; ++ } ++ if (grub_add (i, 1 + sizeof('\0'), &alloc_size)) ++ { ++ grub_error (GRUB_ERR_OUT_OF_RANGE, N_("overflow is detected")); ++ return NULL; ++ } ++ tmp = grub_realloc (line, alloc_size); ++ + if (! tmp) + { + grub_free (line); diff --git a/0633-commands-test-Stack-overflow-due-to-unlimited-recurs.patch b/0633-commands-test-Stack-overflow-due-to-unlimited-recurs.patch new file mode 100644 index 0000000..4c932b9 --- /dev/null +++ b/0633-commands-test-Stack-overflow-due-to-unlimited-recurs.patch @@ -0,0 +1,83 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Lidong Chen +Date: Mon, 16 Dec 2024 20:22:41 +0000 +Subject: [PATCH] commands/test: Stack overflow due to unlimited recursion + depth + +The test_parse() evaluates test expression recursively. Due to lack of +recursion depth check a specially crafted expression may cause a stack +overflow. The recursion is only triggered by the parentheses usage and +it can be unlimited. However, sensible expressions are unlikely to +contain more than a few parentheses. So, this patch limits the recursion +depth to 100, which should be sufficient. + +Reported-by: Nils Langius +Signed-off-by: Lidong Chen +Reviewed-by: Daniel Kiper +--- + grub-core/commands/test.c | 21 ++++++++++++++++++--- + 1 file changed, 18 insertions(+), 3 deletions(-) + +diff --git a/grub-core/commands/test.c b/grub-core/commands/test.c +index 13c6ed953..0a6f51bf3 100644 +--- a/grub-core/commands/test.c ++++ b/grub-core/commands/test.c +@@ -29,6 +29,9 @@ + + GRUB_MOD_LICENSE ("GPLv3+"); + ++/* Set a limit on recursion to avoid stack overflow. */ ++#define MAX_TEST_RECURSION_DEPTH 100 ++ + /* A simple implementation for signed numbers. */ + static int + grub_strtosl (char *arg, char **end, int base) +@@ -150,7 +153,7 @@ get_fileinfo (char *path, struct test_parse_ctx *ctx) + + /* Parse a test expression starting from *argn. */ + static int +-test_parse (char **args, int *argn, int argc) ++test_parse (char **args, int *argn, int argc, int *depth) + { + struct test_parse_ctx ctx = { + .and = 1, +@@ -387,13 +390,24 @@ test_parse (char **args, int *argn, int argc) + if (grub_strcmp (args[*argn], ")") == 0) + { + (*argn)++; ++ if (*depth > 0) ++ (*depth)--; ++ + return ctx.or || ctx.and; + } + /* Recursively invoke if parenthesis. */ + if (grub_strcmp (args[*argn], "(") == 0) + { + (*argn)++; +- update_val (test_parse (args, argn, argc), &ctx); ++ ++ if (++(*depth) > MAX_TEST_RECURSION_DEPTH) ++ { ++ grub_error (GRUB_ERR_OUT_OF_RANGE, N_("max recursion depth exceeded")); ++ depth--; ++ return ctx.or || ctx.and; ++ } ++ ++ update_val (test_parse (args, argn, argc, depth), &ctx); + continue; + } + +@@ -428,11 +442,12 @@ grub_cmd_test (grub_command_t cmd __attribute__ ((unused)), + int argc, char **args) + { + int argn = 0; ++ int depth = 0; + + if (argc >= 1 && grub_strcmp (args[argc - 1], "]") == 0) + argc--; + +- return test_parse (args, &argn, argc) ? GRUB_ERR_NONE ++ return test_parse (args, &argn, argc, &depth) ? GRUB_ERR_NONE + : grub_error (GRUB_ERR_TEST_FAILURE, N_("false")); + } + diff --git a/0634-commands-minicmd-Block-the-dump-command-in-lockdown-.patch b/0634-commands-minicmd-Block-the-dump-command-in-lockdown-.patch new file mode 100644 index 0000000..a999146 --- /dev/null +++ b/0634-commands-minicmd-Block-the-dump-command-in-lockdown-.patch @@ -0,0 +1,33 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: B Horn +Date: Thu, 18 Apr 2024 20:29:39 +0100 +Subject: [PATCH] commands/minicmd: Block the dump command in lockdown mode + +The dump enables a user to read memory which should not be possible +in lockdown mode. + +Fixes: CVE-2025-1118 + +Reported-by: B Horn +Reported-by: Jonathan Bar Or +Signed-off-by: B Horn +Reviewed-by: Daniel Kiper +--- + grub-core/commands/minicmd.c | 4 ++-- + 1 file changed, 2 insertions(+), 2 deletions(-) + +diff --git a/grub-core/commands/minicmd.c b/grub-core/commands/minicmd.c +index 2001043cf..9efb7718c 100644 +--- a/grub-core/commands/minicmd.c ++++ b/grub-core/commands/minicmd.c +@@ -215,8 +215,8 @@ GRUB_MOD_INIT(minicmd) + grub_register_command ("help", grub_mini_cmd_help, + 0, N_("Show this message.")); + cmd_dump = +- grub_register_command ("dump", grub_mini_cmd_dump, +- N_("ADDR [SIZE]"), N_("Show memory contents.")); ++ grub_register_command_lockdown ("dump", grub_mini_cmd_dump, ++ N_("ADDR [SIZE]"), N_("Show memory contents.")); + cmd_rmmod = + grub_register_command ("rmmod", grub_mini_cmd_rmmod, + N_("MODULE"), N_("Remove a module.")); diff --git a/0635-commands-memrw-Disable-memory-reading-in-lockdown-mo.patch b/0635-commands-memrw-Disable-memory-reading-in-lockdown-mo.patch new file mode 100644 index 0000000..6e14744 --- /dev/null +++ b/0635-commands-memrw-Disable-memory-reading-in-lockdown-mo.patch @@ -0,0 +1,50 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: B Horn +Date: Thu, 18 Apr 2024 20:37:10 +0100 +Subject: [PATCH] commands/memrw: Disable memory reading in lockdown mode + +With the rest of module being blocked in lockdown mode it does not make +a lot of sense to leave memory reading enabled. This also goes in par +with disabling the dump command. + +Reported-by: B Horn +Signed-off-by: B Horn +Reviewed-by: Daniel Kiper +--- + grub-core/commands/memrw.c | 21 ++++++++++++--------- + 1 file changed, 12 insertions(+), 9 deletions(-) + +diff --git a/grub-core/commands/memrw.c b/grub-core/commands/memrw.c +index d401a6db0..3542683d1 100644 +--- a/grub-core/commands/memrw.c ++++ b/grub-core/commands/memrw.c +@@ -122,17 +122,20 @@ grub_cmd_write (grub_command_t cmd, int argc, char **argv) + GRUB_MOD_INIT(memrw) + { + cmd_read_byte = +- grub_register_extcmd ("read_byte", grub_cmd_read, 0, +- N_("ADDR"), N_("Read 8-bit value from ADDR."), +- options); ++ grub_register_extcmd_lockdown ("read_byte", grub_cmd_read, 0, ++ N_("ADDR"), ++ N_("Read 8-bit value from ADDR."), ++ options); + cmd_read_word = +- grub_register_extcmd ("read_word", grub_cmd_read, 0, +- N_("ADDR"), N_("Read 16-bit value from ADDR."), +- options); ++ grub_register_extcmd_lockdown ("read_word", grub_cmd_read, 0, ++ N_("ADDR"), ++ N_("Read 16-bit value from ADDR."), ++ options); + cmd_read_dword = +- grub_register_extcmd ("read_dword", grub_cmd_read, 0, +- N_("ADDR"), N_("Read 32-bit value from ADDR."), +- options); ++ grub_register_extcmd_lockdown ("read_dword", grub_cmd_read, 0, ++ N_("ADDR"), ++ N_("Read 32-bit value from ADDR."), ++ options); + cmd_write_byte = + grub_register_command_lockdown ("write_byte", grub_cmd_write, + N_("ADDR VALUE [MASK]"), diff --git a/0636-commands-hexdump-Disable-memory-reading-in-lockdown-.patch b/0636-commands-hexdump-Disable-memory-reading-in-lockdown-.patch new file mode 100644 index 0000000..ae523f8 --- /dev/null +++ b/0636-commands-hexdump-Disable-memory-reading-in-lockdown-.patch @@ -0,0 +1,37 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: B Horn +Date: Fri, 19 Apr 2024 22:31:45 +0100 +Subject: [PATCH] commands/hexdump: Disable memory reading in lockdown mode + +Reported-by: B Horn +Signed-off-by: B Horn +Reviewed-by: Daniel Kiper +--- + grub-core/commands/hexdump.c | 7 ++++++- + 1 file changed, 6 insertions(+), 1 deletion(-) + +diff --git a/grub-core/commands/hexdump.c b/grub-core/commands/hexdump.c +index eaa12465b..d6f61d98a 100644 +--- a/grub-core/commands/hexdump.c ++++ b/grub-core/commands/hexdump.c +@@ -24,6 +24,7 @@ + #include + #include + #include ++#include + + GRUB_MOD_LICENSE ("GPLv3+"); + +@@ -51,7 +52,11 @@ grub_cmd_hexdump (grub_extcmd_context_t ctxt, int argc, char **args) + length = (state[1].set) ? grub_strtoul (state[1].arg, 0, 0) : 256; + + if (!grub_strcmp (args[0], "(mem)")) +- hexdump (skip, (char *) (grub_addr_t) skip, length); ++ { ++ if (grub_is_lockdown() == GRUB_LOCKDOWN_ENABLED) ++ return grub_error (GRUB_ERR_ACCESS_DENIED, N_("memory reading is disabled in lockdown mode")); ++ hexdump (skip, (char *) (grub_addr_t) skip, length); ++ } + else if ((args[0][0] == '(') && (args[0][namelen - 1] == ')')) + { + grub_disk_t disk; diff --git a/0637-fs-bfs-Disable-under-lockdown.patch b/0637-fs-bfs-Disable-under-lockdown.patch new file mode 100644 index 0000000..ec83976 --- /dev/null +++ b/0637-fs-bfs-Disable-under-lockdown.patch @@ -0,0 +1,52 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Daniel Axtens +Date: Sat, 23 Mar 2024 15:59:43 +1100 +Subject: [PATCH] fs/bfs: Disable under lockdown + +The BFS is not fuzz-clean. Don't allow it to be loaded under lockdown. +This will also disable the AFS. + +Fixes: CVE-2024-45778 +Fixes: CVE-2024-45779 + +Reported-by: Nils Langius +Signed-off-by: Daniel Axtens +Reviewed-by: Daniel Kiper +--- + grub-core/fs/bfs.c | 11 ++++++++--- + 1 file changed, 8 insertions(+), 3 deletions(-) + +diff --git a/grub-core/fs/bfs.c b/grub-core/fs/bfs.c +index 6afdfc987..e70aaa448 100644 +--- a/grub-core/fs/bfs.c ++++ b/grub-core/fs/bfs.c +@@ -30,6 +30,7 @@ + #include + #include + #include ++#include + + GRUB_MOD_LICENSE ("GPLv3+"); + +@@ -1104,8 +1105,11 @@ GRUB_MOD_INIT (bfs) + { + COMPILE_TIME_ASSERT (1 << LOG_EXTENT_SIZE == + sizeof (struct grub_bfs_extent)); +- grub_bfs_fs.mod = mod; +- grub_fs_register (&grub_bfs_fs); ++ if (!grub_is_lockdown ()) ++ { ++ grub_bfs_fs.mod = mod; ++ grub_fs_register (&grub_bfs_fs); ++ } + } + + #ifdef MODE_AFS +@@ -1114,5 +1118,6 @@ GRUB_MOD_FINI (afs) + GRUB_MOD_FINI (bfs) + #endif + { +- grub_fs_unregister (&grub_bfs_fs); ++ if (!grub_is_lockdown ()) ++ grub_fs_unregister (&grub_bfs_fs); + } diff --git a/0638-fs-Disable-many-filesystems-under-lockdown.patch b/0638-fs-Disable-many-filesystems-under-lockdown.patch new file mode 100644 index 0000000..9b9ddb2 --- /dev/null +++ b/0638-fs-Disable-many-filesystems-under-lockdown.patch @@ -0,0 +1,391 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Daniel Axtens +Date: Sat, 23 Mar 2024 16:20:45 +1100 +Subject: [PATCH] fs: Disable many filesystems under lockdown + +The idea is to permit the following: btrfs, cpio, exfat, ext, f2fs, fat, +hfsplus, iso9660, squash4, tar, xfs and zfs. + +The JFS, ReiserFS, romfs, UDF and UFS security vulnerabilities were +reported by Jonathan Bar Or . + +Fixes: CVE-2025-0677 +Fixes: CVE-2025-0684 +Fixes: CVE-2025-0685 +Fixes: CVE-2025-0686 +Fixes: CVE-2025-0689 + +Suggested-by: Daniel Axtens +Signed-off-by: Daniel Axtens +Reviewed-by: Daniel Kiper +--- + grub-core/fs/affs.c | 11 ++++++++--- + grub-core/fs/cbfs.c | 11 ++++++++--- + grub-core/fs/jfs.c | 11 ++++++++--- + grub-core/fs/minix.c | 11 ++++++++--- + grub-core/fs/nilfs2.c | 11 ++++++++--- + grub-core/fs/ntfs.c | 11 ++++++++--- + grub-core/fs/reiserfs.c | 11 ++++++++--- + grub-core/fs/romfs.c | 11 ++++++++--- + grub-core/fs/sfs.c | 11 ++++++++--- + grub-core/fs/udf.c | 11 ++++++++--- + grub-core/fs/ufs.c | 11 ++++++++--- + 11 files changed, 88 insertions(+), 33 deletions(-) + +diff --git a/grub-core/fs/affs.c b/grub-core/fs/affs.c +index 6347ca368..2a850632e 100644 +--- a/grub-core/fs/affs.c ++++ b/grub-core/fs/affs.c +@@ -26,6 +26,7 @@ + #include + #include + #include ++#include + + GRUB_MOD_LICENSE ("GPLv3+"); + +@@ -699,12 +700,16 @@ static struct grub_fs grub_affs_fs = + + GRUB_MOD_INIT(affs) + { +- grub_affs_fs.mod = mod; +- grub_fs_register (&grub_affs_fs); ++ if (!grub_is_lockdown ()) ++ { ++ grub_affs_fs.mod = mod; ++ grub_fs_register (&grub_affs_fs); ++ } + my_mod = mod; + } + + GRUB_MOD_FINI(affs) + { +- grub_fs_unregister (&grub_affs_fs); ++ if (!grub_is_lockdown ()) ++ grub_fs_unregister (&grub_affs_fs); + } +diff --git a/grub-core/fs/cbfs.c b/grub-core/fs/cbfs.c +index 395edcbbd..c82980375 100644 +--- a/grub-core/fs/cbfs.c ++++ b/grub-core/fs/cbfs.c +@@ -26,6 +26,7 @@ + #include + #include + #include ++#include + + GRUB_MOD_LICENSE ("GPLv3+"); + +@@ -390,13 +391,17 @@ GRUB_MOD_INIT (cbfs) + #if (defined (__i386__) || defined (__x86_64__)) && !defined (GRUB_UTIL) && !defined (GRUB_MACHINE_EMU) && !defined (GRUB_MACHINE_XEN) + init_cbfsdisk (); + #endif +- grub_cbfs_fs.mod = mod; +- grub_fs_register (&grub_cbfs_fs); ++ if (!grub_is_lockdown ()) ++ { ++ grub_cbfs_fs.mod = mod; ++ grub_fs_register (&grub_cbfs_fs); ++ } + } + + GRUB_MOD_FINI (cbfs) + { +- grub_fs_unregister (&grub_cbfs_fs); ++ if (!grub_is_lockdown ()) ++ grub_fs_unregister (&grub_cbfs_fs); + #if (defined (__i386__) || defined (__x86_64__)) && !defined (GRUB_UTIL) && !defined (GRUB_MACHINE_EMU) && !defined (GRUB_MACHINE_XEN) + fini_cbfsdisk (); + #endif +diff --git a/grub-core/fs/jfs.c b/grub-core/fs/jfs.c +index 46941248b..e08771b44 100644 +--- a/grub-core/fs/jfs.c ++++ b/grub-core/fs/jfs.c +@@ -26,6 +26,7 @@ + #include + #include + #include ++#include + + GRUB_MOD_LICENSE ("GPLv3+"); + +@@ -998,12 +999,16 @@ static struct grub_fs grub_jfs_fs = + + GRUB_MOD_INIT(jfs) + { +- grub_jfs_fs.mod = mod; +- grub_fs_register (&grub_jfs_fs); ++ if (!grub_is_lockdown ()) ++ { ++ grub_jfs_fs.mod = mod; ++ grub_fs_register (&grub_jfs_fs); ++ } + my_mod = mod; + } + + GRUB_MOD_FINI(jfs) + { +- grub_fs_unregister (&grub_jfs_fs); ++ if (!grub_is_lockdown ()) ++ grub_fs_unregister (&grub_jfs_fs); + } +diff --git a/grub-core/fs/minix.c b/grub-core/fs/minix.c +index 28571c49e..38e658763 100644 +--- a/grub-core/fs/minix.c ++++ b/grub-core/fs/minix.c +@@ -25,6 +25,7 @@ + #include + #include + #include ++#include + + GRUB_MOD_LICENSE ("GPLv3+"); + +@@ -716,8 +717,11 @@ GRUB_MOD_INIT(minix) + #endif + #endif + { +- grub_minix_fs.mod = mod; +- grub_fs_register (&grub_minix_fs); ++ if (!grub_is_lockdown ()) ++ { ++ grub_minix_fs.mod = mod; ++ grub_fs_register (&grub_minix_fs); ++ } + my_mod = mod; + } + +@@ -739,5 +743,6 @@ GRUB_MOD_FINI(minix) + #endif + #endif + { +- grub_fs_unregister (&grub_minix_fs); ++ if (!grub_is_lockdown ()) ++ grub_fs_unregister (&grub_minix_fs); + } +diff --git a/grub-core/fs/nilfs2.c b/grub-core/fs/nilfs2.c +index 3c1e4d1f6..6d1e25fb6 100644 +--- a/grub-core/fs/nilfs2.c ++++ b/grub-core/fs/nilfs2.c +@@ -34,6 +34,7 @@ + #include + #include + #include ++#include + + GRUB_MOD_LICENSE ("GPLv3+"); + +@@ -1231,12 +1232,16 @@ GRUB_MOD_INIT (nilfs2) + grub_nilfs2_dat_entry)); + COMPILE_TIME_ASSERT (1 << LOG_INODE_SIZE + == sizeof (struct grub_nilfs2_inode)); +- grub_nilfs2_fs.mod = mod; +- grub_fs_register (&grub_nilfs2_fs); ++ if (!grub_is_lockdown ()) ++ { ++ grub_nilfs2_fs.mod = mod; ++ grub_fs_register (&grub_nilfs2_fs); ++ } + my_mod = mod; + } + + GRUB_MOD_FINI (nilfs2) + { +- grub_fs_unregister (&grub_nilfs2_fs); ++ if (!grub_is_lockdown ()) ++ grub_fs_unregister (&grub_nilfs2_fs); + } +diff --git a/grub-core/fs/ntfs.c b/grub-core/fs/ntfs.c +index 9244e95dd..1ad3a2715 100644 +--- a/grub-core/fs/ntfs.c ++++ b/grub-core/fs/ntfs.c +@@ -27,6 +27,7 @@ + #include + #include + #include ++#include + + GRUB_MOD_LICENSE ("GPLv3+"); + +@@ -1537,12 +1538,16 @@ static struct grub_fs grub_ntfs_fs = + + GRUB_MOD_INIT (ntfs) + { +- grub_ntfs_fs.mod = mod; +- grub_fs_register (&grub_ntfs_fs); ++ if (!grub_is_lockdown ()) ++ { ++ grub_ntfs_fs.mod = mod; ++ grub_fs_register (&grub_ntfs_fs); ++ } + my_mod = mod; + } + + GRUB_MOD_FINI (ntfs) + { +- grub_fs_unregister (&grub_ntfs_fs); ++ if (!grub_is_lockdown ()) ++ grub_fs_unregister (&grub_ntfs_fs); + } +diff --git a/grub-core/fs/reiserfs.c b/grub-core/fs/reiserfs.c +index e65b81467..72e724f4c 100644 +--- a/grub-core/fs/reiserfs.c ++++ b/grub-core/fs/reiserfs.c +@@ -39,6 +39,7 @@ + #include + #include + #include ++#include + + GRUB_MOD_LICENSE ("GPLv3+"); + +@@ -1407,12 +1408,16 @@ static struct grub_fs grub_reiserfs_fs = + + GRUB_MOD_INIT(reiserfs) + { +- grub_reiserfs_fs.mod = mod; +- grub_fs_register (&grub_reiserfs_fs); ++ if (!grub_is_lockdown ()) ++ { ++ grub_reiserfs_fs.mod = mod; ++ grub_fs_register (&grub_reiserfs_fs); ++ } + my_mod = mod; + } + + GRUB_MOD_FINI(reiserfs) + { +- grub_fs_unregister (&grub_reiserfs_fs); ++ if (!grub_is_lockdown ()) ++ grub_fs_unregister (&grub_reiserfs_fs); + } +diff --git a/grub-core/fs/romfs.c b/grub-core/fs/romfs.c +index f282cff86..d7817cdc5 100644 +--- a/grub-core/fs/romfs.c ++++ b/grub-core/fs/romfs.c +@@ -23,6 +23,7 @@ + #include + #include + #include ++#include + + GRUB_MOD_LICENSE ("GPLv3+"); + +@@ -475,11 +476,15 @@ static struct grub_fs grub_romfs_fs = + + GRUB_MOD_INIT(romfs) + { +- grub_romfs_fs.mod = mod; +- grub_fs_register (&grub_romfs_fs); ++ if (!grub_is_lockdown ()) ++ { ++ grub_romfs_fs.mod = mod; ++ grub_fs_register (&grub_romfs_fs); ++ } + } + + GRUB_MOD_FINI(romfs) + { +- grub_fs_unregister (&grub_romfs_fs); ++ if (!grub_is_lockdown ()) ++ grub_fs_unregister (&grub_romfs_fs); + } +diff --git a/grub-core/fs/sfs.c b/grub-core/fs/sfs.c +index c6b9fb49a..a272aea0a 100644 +--- a/grub-core/fs/sfs.c ++++ b/grub-core/fs/sfs.c +@@ -26,6 +26,7 @@ + #include + #include + #include ++#include + #include + + GRUB_MOD_LICENSE ("GPLv3+"); +@@ -779,12 +780,16 @@ static struct grub_fs grub_sfs_fs = + + GRUB_MOD_INIT(sfs) + { +- grub_sfs_fs.mod = mod; +- grub_fs_register (&grub_sfs_fs); ++ if (!grub_is_lockdown ()) ++ { ++ grub_sfs_fs.mod = mod; ++ grub_fs_register (&grub_sfs_fs); ++ } + my_mod = mod; + } + + GRUB_MOD_FINI(sfs) + { +- grub_fs_unregister (&grub_sfs_fs); ++ if (!grub_is_lockdown ()) ++ grub_fs_unregister (&grub_sfs_fs); + } +diff --git a/grub-core/fs/udf.c b/grub-core/fs/udf.c +index 8db2b5686..406260901 100644 +--- a/grub-core/fs/udf.c ++++ b/grub-core/fs/udf.c +@@ -27,6 +27,7 @@ + #include + #include + #include ++#include + #include + #include + +@@ -1382,12 +1383,16 @@ static struct grub_fs grub_udf_fs = { + + GRUB_MOD_INIT (udf) + { +- grub_udf_fs.mod = mod; +- grub_fs_register (&grub_udf_fs); ++ if (!grub_is_lockdown ()) ++ { ++ grub_udf_fs.mod = mod; ++ grub_fs_register (&grub_udf_fs); ++ } + my_mod = mod; + } + + GRUB_MOD_FINI (udf) + { +- grub_fs_unregister (&grub_udf_fs); ++ if (!grub_is_lockdown ()) ++ grub_fs_unregister (&grub_udf_fs); + } +diff --git a/grub-core/fs/ufs.c b/grub-core/fs/ufs.c +index a2c63d646..844a439f8 100644 +--- a/grub-core/fs/ufs.c ++++ b/grub-core/fs/ufs.c +@@ -25,6 +25,7 @@ + #include + #include + #include ++#include + + GRUB_MOD_LICENSE ("GPLv3+"); + +@@ -899,8 +900,11 @@ GRUB_MOD_INIT(ufs1) + #endif + #endif + { +- grub_ufs_fs.mod = mod; +- grub_fs_register (&grub_ufs_fs); ++ if (!grub_is_lockdown ()) ++ { ++ grub_ufs_fs.mod = mod; ++ grub_fs_register (&grub_ufs_fs); ++ } + my_mod = mod; + } + +@@ -914,6 +918,7 @@ GRUB_MOD_FINI(ufs1) + #endif + #endif + { +- grub_fs_unregister (&grub_ufs_fs); ++ if (!grub_is_lockdown ()) ++ grub_fs_unregister (&grub_ufs_fs); + } + diff --git a/0639-disk-Use-safe-math-macros-to-prevent-overflows.patch b/0639-disk-Use-safe-math-macros-to-prevent-overflows.patch new file mode 100644 index 0000000..269a445 --- /dev/null +++ b/0639-disk-Use-safe-math-macros-to-prevent-overflows.patch @@ -0,0 +1,367 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Alec Brown +Date: Wed, 22 Jan 2025 02:55:09 +0000 +Subject: [PATCH] disk: Use safe math macros to prevent overflows + +Replace direct arithmetic operations with macros from include/grub/safemath.h +to prevent potential overflow issues when calculating the memory sizes. + +Signed-off-by: Alec Brown +Reviewed-by: Daniel Kiper +--- + grub-core/disk/cryptodisk.c | 36 ++++++++++++++++++------ + grub-core/disk/diskfilter.c | 9 ++++-- + grub-core/disk/ieee1275/ofdisk.c | 60 ++++++++++++++++++++++++++++++++++------ + grub-core/disk/ldm.c | 37 +++++++++++++++++++++---- + grub-core/disk/memdisk.c | 7 ++++- + 5 files changed, 123 insertions(+), 26 deletions(-) + +diff --git a/grub-core/disk/cryptodisk.c b/grub-core/disk/cryptodisk.c +index 78a902515..871640685 100644 +--- a/grub-core/disk/cryptodisk.c ++++ b/grub-core/disk/cryptodisk.c +@@ -26,6 +26,7 @@ + #include + #include + #include ++#include + + #ifdef GRUB_UTIL + #include +@@ -1039,7 +1040,7 @@ static char * + luks_script_get (grub_size_t *sz) + { + grub_cryptodisk_t i; +- grub_size_t size = 0; ++ grub_size_t size = 0, mul; + char *ptr, *ret; + + *sz = 0; +@@ -1047,16 +1048,33 @@ luks_script_get (grub_size_t *sz) + for (i = cryptodisk_list; i != NULL; i = i->next) + if (grub_strcmp (i->modname, "luks") == 0) + { +- size += sizeof ("luks_mount "); +- size += grub_strlen (i->uuid); +- size += grub_strlen (i->cipher->cipher->name); +- size += 54; +- if (i->essiv_hash) +- size += grub_strlen (i->essiv_hash->name); +- size += i->keysize * 2; ++ if (grub_add (size, grub_strlen (i->modname), &size) || ++ grub_add (size, sizeof ("_mount") + 60, &size) || ++ grub_add (size, grub_strlen (i->uuid), &size) || ++ grub_add (size, grub_strlen (i->cipher->cipher->name), &size) || ++ grub_mul (i->keysize, 2, &mul) || ++ grub_add (size, mul, &size)) ++ { ++ grub_error (GRUB_ERR_OUT_OF_RANGE, "overflow detected while obtaining size of luks script"); ++ return 0; ++ } ++ if (i->essiv_hash) ++ { ++ if (grub_add (size, grub_strlen (i->essiv_hash->name), &size)) ++ { ++ grub_error (GRUB_ERR_OUT_OF_RANGE, "overflow detected while obtaining size of luks script"); ++ return 0; ++ } ++ } + } + +- ret = grub_malloc (size + 1); ++ if (grub_add (size, 1, &size)) ++ { ++ grub_error (GRUB_ERR_OUT_OF_RANGE, "overflow detected while obtaining size of luks script"); ++ return 0; ++ } ++ ++ ret = grub_malloc (size); + if (!ret) + return 0; + +diff --git a/grub-core/disk/diskfilter.c b/grub-core/disk/diskfilter.c +index 2be019269..a881aa2c9 100644 +--- a/grub-core/disk/diskfilter.c ++++ b/grub-core/disk/diskfilter.c +@@ -24,6 +24,7 @@ + #include + #include + #include ++#include + #ifdef GRUB_UTIL + #include + #include +@@ -1013,7 +1014,7 @@ grub_diskfilter_make_raid (grub_size_t uuidlen, char *uuid, int nmemb, + { + struct grub_diskfilter_vg *array; + int i; +- grub_size_t j; ++ grub_size_t j, sz; + grub_uint64_t totsize; + struct grub_diskfilter_pv *pv; + grub_err_t err; +@@ -1107,7 +1108,11 @@ grub_diskfilter_make_raid (grub_size_t uuidlen, char *uuid, int nmemb, + } + array->lvs->vg = array; + +- array->lvs->idname = grub_malloc (sizeof ("mduuid/") + 2 * uuidlen); ++ if (grub_mul (uuidlen, 2, &sz) || ++ grub_add (sz, sizeof ("mduuid/"), &sz)) ++ goto fail; ++ ++ array->lvs->idname = grub_malloc (sz); + if (!array->lvs->idname) + goto fail; + +diff --git a/grub-core/disk/ieee1275/ofdisk.c b/grub-core/disk/ieee1275/ofdisk.c +index 98325ca98..c1b07d087 100644 +--- a/grub-core/disk/ieee1275/ofdisk.c ++++ b/grub-core/disk/ieee1275/ofdisk.c +@@ -24,6 +24,7 @@ + #include + #include + #include ++#include + + static char *last_devpath; + static grub_ieee1275_ihandle_t last_ihandle; +@@ -80,6 +81,7 @@ ofdisk_hash_add_real (char *devpath) + struct ofdisk_hash_ent **head = &ofdisk_hash[ofdisk_hash_fn(devpath)]; + const char *iptr; + char *optr; ++ grub_size_t sz; + + p = grub_zalloc (sizeof (*p)); + if (!p) +@@ -87,8 +89,14 @@ ofdisk_hash_add_real (char *devpath) + + p->devpath = devpath; + +- p->grub_devpath = grub_malloc (sizeof ("ieee1275/") +- + 2 * grub_strlen (p->devpath)); ++ if (grub_mul (grub_strlen (p->devpath), 2, &sz) || ++ grub_add (sz, sizeof ("ieee1275/"), &sz)) ++ { ++ grub_error (GRUB_ERR_OUT_OF_RANGE, N_("overflow detected while obtaining size of device path")); ++ return NULL; ++ } ++ ++ p->grub_devpath = grub_malloc (sz); + + if (!p->grub_devpath) + { +@@ -98,7 +106,13 @@ ofdisk_hash_add_real (char *devpath) + + if (! grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_NO_PARTITION_0)) + { +- p->open_path = grub_malloc (grub_strlen (p->devpath) + 3); ++ if (grub_add (grub_strlen (p->devpath), 3, &sz)) ++ { ++ grub_error (GRUB_ERR_OUT_OF_RANGE, N_("overflow detected while obtaining size of an open path")); ++ return NULL; ++ } ++ ++ p->open_path = grub_malloc (sz); + if (!p->open_path) + { + grub_free (p->grub_devpath); +@@ -224,7 +238,7 @@ dev_iterate (const struct grub_ieee1275_devalias *alias) + args; + char *buf, *bufptr; + unsigned i; +- ++ grub_size_t sz; + + RETRY_IEEE1275_OFDISK_OPEN(alias->path, &ihandle) + if (! ihandle) +@@ -245,7 +259,14 @@ dev_iterate (const struct grub_ieee1275_devalias *alias) + return; + } + +- buf = grub_malloc (grub_strlen (alias->path) + 32); ++ if (grub_add (grub_strlen (alias->path), 32, &sz)) ++ { ++ grub_error (GRUB_ERR_OUT_OF_RANGE, "overflow detected while creating buffer for vscsi"); ++ grub_ieee1275_close (ihandle); ++ return; ++ } ++ ++ buf = grub_malloc (sz); + if (!buf) + return; + bufptr = grub_stpcpy (buf, alias->path); +@@ -289,9 +310,15 @@ dev_iterate (const struct grub_ieee1275_devalias *alias) + grub_uint64_t *table; + grub_uint16_t table_size; + grub_ieee1275_ihandle_t ihandle; ++ grub_size_t sz; + +- buf = grub_malloc (grub_strlen (alias->path) + +- sizeof ("/disk@7766554433221100")); ++ if (grub_add (grub_strlen (alias->path), sizeof ("/disk@7766554433221100"), &sz)) ++ { ++ grub_error (GRUB_ERR_OUT_OF_RANGE, "overflow detected while creating buffer for sas_ioa"); ++ return; ++ } ++ ++ buf = grub_malloc (sz); + if (!buf) + return; + bufptr = grub_stpcpy (buf, alias->path); +@@ -431,9 +458,17 @@ grub_ofdisk_iterate (grub_disk_dev_iterate_hook_t hook, void *hook_data, + static char * + compute_dev_path (const char *name) + { +- char *devpath = grub_malloc (grub_strlen (name) + 3); ++ char *devpath; + char *p, c; ++ grub_size_t sz; + ++ if (grub_add (grub_strlen (name), 3, &sz)) ++ { ++ grub_error (GRUB_ERR_OUT_OF_RANGE, N_("overflow detected while obtaining size of device path")); ++ return NULL; ++ } ++ ++ devpath = grub_malloc (sz); + if (!devpath) + return NULL; + +@@ -660,6 +695,7 @@ insert_bootpath (void) + char *bootpath; + grub_ssize_t bootpath_size; + char *type; ++ grub_size_t sz; + + if (grub_ieee1275_get_property_length (grub_ieee1275_chosen, "bootpath", + &bootpath_size) +@@ -670,7 +706,13 @@ insert_bootpath (void) + return; + } + +- bootpath = (char *) grub_malloc ((grub_size_t) bootpath_size + 64); ++ if (grub_add (bootpath_size, 64, &sz)) ++ { ++ grub_error (GRUB_ERR_OUT_OF_RANGE, N_("overflow detected while obtaining bootpath size")); ++ return; ++ } ++ ++ bootpath = (char *) grub_malloc (sz); + if (! bootpath) + { + grub_print_error (); +diff --git a/grub-core/disk/ldm.c b/grub-core/disk/ldm.c +index e82e9899f..973fbfdcb 100644 +--- a/grub-core/disk/ldm.c ++++ b/grub-core/disk/ldm.c +@@ -220,6 +220,7 @@ make_vg (grub_disk_t disk, + struct grub_ldm_vblk vblk[GRUB_DISK_SECTOR_SIZE + / sizeof (struct grub_ldm_vblk)]; + unsigned i; ++ grub_size_t sz; + err = grub_disk_read (disk, cursec, 0, + sizeof(vblk), &vblk); + if (err) +@@ -251,7 +252,13 @@ make_vg (grub_disk_t disk, + grub_free (pv); + goto fail2; + } +- pv->internal_id = grub_malloc (ptr[0] + 2); ++ if (grub_add (ptr[0], 2, &sz)) ++ { ++ grub_free (pv); ++ goto fail2; ++ } ++ ++ pv->internal_id = grub_malloc (sz); + if (!pv->internal_id) + { + grub_free (pv); +@@ -276,7 +283,15 @@ make_vg (grub_disk_t disk, + goto fail2; + } + pv->id.uuidlen = *ptr; +- pv->id.uuid = grub_malloc (pv->id.uuidlen + 1); ++ ++ if (grub_add (pv->id.uuidlen, 1, &sz)) ++ { ++ grub_free (pv->internal_id); ++ grub_free (pv); ++ goto fail2; ++ } ++ ++ pv->id.uuid = grub_malloc (sz); + grub_memcpy (pv->id.uuid, ptr + 1, pv->id.uuidlen); + pv->id.uuid[pv->id.uuidlen] = 0; + +@@ -343,7 +358,13 @@ make_vg (grub_disk_t disk, + grub_free (lv); + goto fail2; + } +- lv->internal_id = grub_malloc ((grub_size_t) ptr[0] + 2); ++ if (grub_add (ptr[0], 2, &sz)) ++ { ++ grub_free (lv->segments); ++ grub_free (lv); ++ goto fail2; ++ } ++ lv->internal_id = grub_malloc (sz); + if (!lv->internal_id) + { + grub_free (lv); +@@ -455,6 +476,7 @@ make_vg (grub_disk_t disk, + struct grub_ldm_vblk vblk[GRUB_DISK_SECTOR_SIZE + / sizeof (struct grub_ldm_vblk)]; + unsigned i; ++ grub_size_t sz; + err = grub_disk_read (disk, cursec, 0, + sizeof(vblk), &vblk); + if (err) +@@ -489,7 +511,13 @@ make_vg (grub_disk_t disk, + { + goto fail2; + } +- comp->internal_id = grub_malloc ((grub_size_t) ptr[0] + 2); ++ if (grub_add (ptr[0], 2, &sz)) ++ { ++ grub_free (comp); ++ goto fail2; ++ } ++ comp->internal_id = grub_malloc (sz); ++ + if (!comp->internal_id) + { + grub_free (comp); +@@ -639,7 +667,6 @@ make_vg (grub_disk_t disk, + if (lv->segments->node_alloc == lv->segments->node_count) + { + void *t; +- grub_size_t sz; + + if (grub_mul (lv->segments->node_alloc, 2, &lv->segments->node_alloc) || + grub_mul (lv->segments->node_alloc, sizeof (*lv->segments->nodes), &sz)) +diff --git a/grub-core/disk/memdisk.c b/grub-core/disk/memdisk.c +index e5ffc01bf..18863305e 100644 +--- a/grub-core/disk/memdisk.c ++++ b/grub-core/disk/memdisk.c +@@ -23,6 +23,7 @@ + #include + #include + #include ++#include + + GRUB_MOD_LICENSE ("GPLv3+"); + +@@ -96,7 +97,11 @@ GRUB_MOD_INIT(memdisk) + + grub_dprintf ("memdisk", "Found memdisk image at %p\n", memdisk_orig_addr); + +- memdisk_size = header->size - sizeof (struct grub_module_header); ++ if (grub_sub (header->size, sizeof (struct grub_module_header), &memdisk_size)) ++ { ++ grub_error (GRUB_ERR_OUT_OF_RANGE, "underflow detected while obtaining memdisk size"); ++ return; ++ } + memdisk_addr = grub_malloc (memdisk_size); + + grub_dprintf ("memdisk", "Copying memdisk image to dynamic memory\n"); diff --git a/0640-disk-Prevent-overflows-when-allocating-memory-for-ar.patch b/0640-disk-Prevent-overflows-when-allocating-memory-for-ar.patch new file mode 100644 index 0000000..9ff3a7b --- /dev/null +++ b/0640-disk-Prevent-overflows-when-allocating-memory-for-ar.patch @@ -0,0 +1,38 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Alec Brown +Date: Wed, 22 Jan 2025 02:55:10 +0000 +Subject: [PATCH] disk: Prevent overflows when allocating memory for arrays + +Use grub_calloc() when allocating memory for arrays to ensure proper +overflow checks are in place. + +Signed-off-by: Alec Brown +Reviewed-by: Daniel Kiper +--- + grub-core/disk/lvm.c | 6 ++---- + 1 file changed, 2 insertions(+), 4 deletions(-) + +diff --git a/grub-core/disk/lvm.c b/grub-core/disk/lvm.c +index 7e86bb7df..47a89e6d0 100644 +--- a/grub-core/disk/lvm.c ++++ b/grub-core/disk/lvm.c +@@ -634,8 +634,7 @@ error_parsing_metadata: + goto lvs_segment_fail; + } + +- seg->nodes = grub_zalloc (sizeof (seg->nodes[0]) +- * seg->node_count); ++ seg->nodes = grub_calloc (seg->node_count, sizeof (seg->nodes[0])); + + p = grub_strstr (p, "mirrors = ["); + if (p == NULL) +@@ -723,8 +722,7 @@ error_parsing_metadata: + } + } + +- seg->nodes = grub_zalloc (sizeof (seg->nodes[0]) +- * seg->node_count); ++ seg->nodes = grub_calloc (seg->node_count, sizeof (seg->nodes[0])); + + p = grub_strstr (p, "raids = ["); + if (p == NULL) diff --git a/0641-disk-Check-if-returned-pointer-for-allocated-memory-.patch b/0641-disk-Check-if-returned-pointer-for-allocated-memory-.patch new file mode 100644 index 0000000..9272cc3 --- /dev/null +++ b/0641-disk-Check-if-returned-pointer-for-allocated-memory-.patch @@ -0,0 +1,134 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Alec Brown +Date: Wed, 22 Jan 2025 02:55:11 +0000 +Subject: [PATCH] disk: Check if returned pointer for allocated memory is NULL + +When using grub_malloc(), grub_zalloc() or grub_calloc(), these functions can +fail if we are out of memory. After allocating memory we should check if these +functions returned NULL and handle this error if they did. + +On the occasion make a NULL check in ATA code more obvious. + +Signed-off-by: Alec Brown +Reviewed-by: Daniel Kiper +--- + grub-core/disk/ata.c | 4 ++-- + grub-core/disk/ldm.c | 6 ++++++ + grub-core/disk/lvm.c | 14 ++++++++++++++ + grub-core/disk/memdisk.c | 2 ++ + 4 files changed, 24 insertions(+), 2 deletions(-) + +diff --git a/grub-core/disk/ata.c b/grub-core/disk/ata.c +index 8ba4e5c50..65d24103e 100644 +--- a/grub-core/disk/ata.c ++++ b/grub-core/disk/ata.c +@@ -112,10 +112,10 @@ grub_ata_identify (struct grub_ata *dev) + return grub_atapi_identify (dev); + + info64 = grub_malloc (GRUB_DISK_SECTOR_SIZE); ++ if (info64 == NULL) ++ return grub_errno; + info32 = (grub_uint32_t *) info64; + info16 = (grub_uint16_t *) info64; +- if (! info16) +- return grub_errno; + + grub_memset (&parms, 0, sizeof (parms)); + parms.buffer = info16; +diff --git a/grub-core/disk/ldm.c b/grub-core/disk/ldm.c +index 973fbfdcb..1510fb35a 100644 +--- a/grub-core/disk/ldm.c ++++ b/grub-core/disk/ldm.c +@@ -292,6 +292,12 @@ make_vg (grub_disk_t disk, + } + + pv->id.uuid = grub_malloc (sz); ++ if (pv->id.uuid == NULL) ++ { ++ grub_free (pv->internal_id); ++ grub_free (pv); ++ goto fail2; ++ } + grub_memcpy (pv->id.uuid, ptr + 1, pv->id.uuidlen); + pv->id.uuid[pv->id.uuidlen] = 0; + +diff --git a/grub-core/disk/lvm.c b/grub-core/disk/lvm.c +index 47a89e6d0..4fce7f226 100644 +--- a/grub-core/disk/lvm.c ++++ b/grub-core/disk/lvm.c +@@ -332,6 +332,8 @@ error_parsing_metadata: + break; + + pv = grub_zalloc (sizeof (*pv)); ++ if (pv == NULL) ++ goto fail4; + q = p; + while (*q != ' ' && q < mda_end) + q++; +@@ -341,6 +343,8 @@ error_parsing_metadata: + + s = q - p; + pv->name = grub_malloc (s + 1); ++ if (pv->name == NULL) ++ goto pvs_fail_noname; + grub_memcpy (pv->name, p, s); + pv->name[s] = '\0'; + +@@ -413,6 +417,8 @@ error_parsing_metadata: + break; + + lv = grub_zalloc (sizeof (*lv)); ++ if (lv == NULL) ++ goto fail4; + + q = p; + while (*q != ' ' && q < mda_end) +@@ -508,6 +514,8 @@ error_parsing_metadata: + goto lvs_fail; + } + lv->segments = grub_calloc (lv->segment_count, sizeof (*seg)); ++ if (lv->segments == NULL) ++ goto lvs_fail; + seg = lv->segments; + + for (i = 0; i < lv->segment_count; i++) +@@ -575,6 +583,8 @@ error_parsing_metadata: + + seg->nodes = grub_calloc (seg->node_count, + sizeof (*stripe)); ++ if (seg->nodes == NULL) ++ goto lvs_segment_fail; + stripe = seg->nodes; + + p = grub_strstr (p, "stripes = ["); +@@ -635,6 +645,8 @@ error_parsing_metadata: + } + + seg->nodes = grub_calloc (seg->node_count, sizeof (seg->nodes[0])); ++ if (seg->nodes == NULL) ++ goto lvs_segment_fail; + + p = grub_strstr (p, "mirrors = ["); + if (p == NULL) +@@ -723,6 +735,8 @@ error_parsing_metadata: + } + + seg->nodes = grub_calloc (seg->node_count, sizeof (seg->nodes[0])); ++ if (seg->nodes == NULL) ++ goto lvs_segment_fail; + + p = grub_strstr (p, "raids = ["); + if (p == NULL) +diff --git a/grub-core/disk/memdisk.c b/grub-core/disk/memdisk.c +index 18863305e..63fc8f1a6 100644 +--- a/grub-core/disk/memdisk.c ++++ b/grub-core/disk/memdisk.c +@@ -103,6 +103,8 @@ GRUB_MOD_INIT(memdisk) + return; + } + memdisk_addr = grub_malloc (memdisk_size); ++ if (memdisk_addr == NULL) ++ return; + + grub_dprintf ("memdisk", "Copying memdisk image to dynamic memory\n"); + grub_memmove (memdisk_addr, memdisk_orig_addr, memdisk_size); diff --git a/0642-disk-ieee1275-ofdisk-Call-grub_ieee1275_close-when-g.patch b/0642-disk-ieee1275-ofdisk-Call-grub_ieee1275_close-when-g.patch new file mode 100644 index 0000000..fb6bd65 --- /dev/null +++ b/0642-disk-ieee1275-ofdisk-Call-grub_ieee1275_close-when-g.patch @@ -0,0 +1,31 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Alec Brown +Date: Wed, 22 Jan 2025 02:55:12 +0000 +Subject: [PATCH] disk/ieee1275/ofdisk: Call grub_ieee1275_close() when + grub_malloc() fails + +In the dev_iterate() function a handle is opened but isn't closed when +grub_malloc() returns NULL. We should fix this by closing it on error. + +Signed-off-by: Alec Brown +Reviewed-by: Daniel Kiper +--- + grub-core/disk/ieee1275/ofdisk.c | 5 ++++- + 1 file changed, 4 insertions(+), 1 deletion(-) + +diff --git a/grub-core/disk/ieee1275/ofdisk.c b/grub-core/disk/ieee1275/ofdisk.c +index c1b07d087..c50614f03 100644 +--- a/grub-core/disk/ieee1275/ofdisk.c ++++ b/grub-core/disk/ieee1275/ofdisk.c +@@ -268,7 +268,10 @@ dev_iterate (const struct grub_ieee1275_devalias *alias) + + buf = grub_malloc (sz); + if (!buf) +- return; ++ { ++ grub_ieee1275_close (ihandle); ++ return; ++ } + bufptr = grub_stpcpy (buf, alias->path); + + for (i = 0; i < args.nentries; i++) diff --git a/0643-fs-Use-safe-math-macros-to-prevent-overflows.patch b/0643-fs-Use-safe-math-macros-to-prevent-overflows.patch new file mode 100644 index 0000000..3c47b6c --- /dev/null +++ b/0643-fs-Use-safe-math-macros-to-prevent-overflows.patch @@ -0,0 +1,353 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Lidong Chen +Date: Tue, 21 Jan 2025 19:02:36 +0000 +Subject: [PATCH] fs: Use safe math macros to prevent overflows + +Replace direct arithmetic operations with macros from include/grub/safemath.h +to prevent potential overflow issues when calculating the memory sizes. + +Signed-off-by: Lidong Chen +Reviewed-by: Daniel Kiper +--- + grub-core/fs/archelp.c | 9 ++++++++- + grub-core/fs/btrfs.c | 34 ++++++++++++++++++++++++++++------ + grub-core/fs/cpio_common.c | 16 ++++++++++++++-- + grub-core/fs/f2fs.c | 17 +++++++++++++++-- + grub-core/fs/ntfscomp.c | 9 ++++++++- + grub-core/fs/squash4.c | 12 +++++++++--- + grub-core/fs/xfs.c | 17 +++++++++++++++-- + 7 files changed, 97 insertions(+), 17 deletions(-) + +diff --git a/grub-core/fs/archelp.c b/grub-core/fs/archelp.c +index 0cf544f6f..6491f74f9 100644 +--- a/grub-core/fs/archelp.c ++++ b/grub-core/fs/archelp.c +@@ -21,6 +21,7 @@ + #include + #include + #include ++#include + + GRUB_MOD_LICENSE ("GPLv3+"); + +@@ -68,6 +69,7 @@ handle_symlink (struct grub_archelp_data *data, + char *rest; + char *linktarget; + grub_size_t linktarget_len; ++ grub_size_t sz; + + *restart = 0; + +@@ -98,7 +100,12 @@ handle_symlink (struct grub_archelp_data *data, + if (linktarget[0] == '\0') + return GRUB_ERR_NONE; + linktarget_len = grub_strlen (linktarget); +- target = grub_malloc (linktarget_len + grub_strlen (*name) + 2); ++ ++ if (grub_add (linktarget_len, grub_strlen (*name), &sz) || ++ grub_add (sz, 2, &sz)) ++ return grub_error (GRUB_ERR_OUT_OF_RANGE, N_("link target length overflow")); ++ ++ target = grub_malloc (sz); + if (!target) + return grub_errno; + +diff --git a/grub-core/fs/btrfs.c b/grub-core/fs/btrfs.c +index 65213549e..646543f5d 100644 +--- a/grub-core/fs/btrfs.c ++++ b/grub-core/fs/btrfs.c +@@ -1500,6 +1500,7 @@ find_path (struct grub_btrfs_data *data, + char *origpath = NULL; + unsigned symlinks_max = 32; + const char *relpath = grub_env_get ("btrfs_relative_path"); ++ grub_size_t sz; + + follow_default = 0; + origpath = grub_strdup (path); +@@ -1626,9 +1627,15 @@ find_path (struct grub_btrfs_data *data, + struct grub_btrfs_dir_item *cdirel; + if (elemsize > allocated) + { +- allocated = 2 * elemsize; ++ if (grub_mul (2, elemsize, &allocated) || ++ grub_add (allocated, 1, &sz)) ++ { ++ grub_free (path_alloc); ++ grub_free (origpath); ++ return grub_error (GRUB_ERR_OUT_OF_RANGE, N_("directory item size overflow")); ++ } + grub_free (direl); +- direl = grub_malloc (allocated + 1); ++ direl = grub_malloc (sz); + if (!direl) + { + grub_free (path_alloc); +@@ -1692,8 +1699,16 @@ find_path (struct grub_btrfs_data *data, + grub_free (origpath); + return err; + } +- tmp = grub_malloc (grub_le_to_cpu64 (inode.size) +- + grub_strlen (path) + 1); ++ ++ if (grub_add (grub_le_to_cpu64 (inode.size), grub_strlen (path), &sz) || ++ grub_add (sz, 1, &sz)) ++ { ++ grub_free (direl); ++ grub_free (path_alloc); ++ grub_free (origpath); ++ return grub_error (GRUB_ERR_OUT_OF_RANGE, N_("buffer size overflow")); ++ } ++ tmp = grub_malloc (sz); + if (!tmp) + { + grub_free (direl); +@@ -1835,6 +1850,7 @@ grub_btrfs_dir (grub_device_t device, const char *path, + grub_uint8_t type; + char *new_path = NULL; + grub_size_t est_size = 0; ++ grub_size_t sz; + + if (!data) + return grub_errno; +@@ -1884,9 +1900,15 @@ grub_btrfs_dir (grub_device_t device, const char *path, + } + if (elemsize > allocated) + { +- allocated = 2 * elemsize; ++ if (grub_mul (2, elemsize, &allocated) || ++ grub_add (allocated, 1, &sz)) ++ { ++ grub_error (GRUB_ERR_OUT_OF_RANGE, N_("directory element size overflow")); ++ r = -grub_errno; ++ break; ++ } + grub_free (direl); +- direl = grub_malloc (allocated + 1); ++ direl = grub_malloc (sz); + if (!direl) + { + r = -grub_errno; +diff --git a/grub-core/fs/cpio_common.c b/grub-core/fs/cpio_common.c +index 50fea47d1..5760072c5 100644 +--- a/grub-core/fs/cpio_common.c ++++ b/grub-core/fs/cpio_common.c +@@ -24,6 +24,7 @@ + #include + #include + #include ++#include + + GRUB_MOD_LICENSE ("GPLv3+"); + +@@ -43,6 +44,7 @@ grub_cpio_find_file (struct grub_archelp_data *data, char **name, + struct head hd; + grub_size_t namesize; + grub_uint32_t modeval; ++ grub_size_t sz; + + data->hofs = data->next_hofs; + +@@ -71,7 +73,10 @@ grub_cpio_find_file (struct grub_archelp_data *data, char **name, + + *mode = modeval; + +- *name = grub_malloc (namesize + 1); ++ if (grub_add (namesize, 1, &sz)) ++ return grub_error (GRUB_ERR_OUT_OF_RANGE, N_("file name size overflow")); ++ ++ *name = grub_malloc (sz); + if (*name == NULL) + return grub_errno; + +@@ -101,10 +106,17 @@ grub_cpio_get_link_target (struct grub_archelp_data *data) + { + char *ret; + grub_err_t err; ++ grub_size_t sz; + + if (data->size == 0) + return grub_strdup (""); +- ret = grub_malloc (data->size + 1); ++ ++ if (grub_add (data->size, 1, &sz)) ++ { ++ grub_error (GRUB_ERR_OUT_OF_RANGE, N_("target data size overflow")); ++ return NULL; ++ } ++ ret = grub_malloc (sz); + if (!ret) + return NULL; + +diff --git a/grub-core/fs/f2fs.c b/grub-core/fs/f2fs.c +index 149f33695..0af62f1a1 100644 +--- a/grub-core/fs/f2fs.c ++++ b/grub-core/fs/f2fs.c +@@ -28,6 +28,7 @@ + #include + #include + #include ++#include + + GRUB_MOD_LICENSE ("GPLv3+"); + +@@ -953,6 +954,7 @@ grub_f2fs_read_symlink (grub_fshelp_node_t node) + char *symlink; + struct grub_fshelp_node *diro = node; + grub_uint64_t filesize; ++ grub_size_t sz; + + if (!diro->inode_read) + { +@@ -963,7 +965,12 @@ grub_f2fs_read_symlink (grub_fshelp_node_t node) + + filesize = grub_f2fs_file_size(&diro->inode.i); + +- symlink = grub_malloc (filesize + 1); ++ if (grub_add (filesize, 1, &sz)) ++ { ++ grub_error (GRUB_ERR_OUT_OF_RANGE, N_("symlink size overflow")); ++ return 0; ++ } ++ symlink = grub_malloc (sz); + if (!symlink) + return 0; + +@@ -992,6 +999,7 @@ grub_f2fs_check_dentries (struct grub_f2fs_dir_iter_ctx *ctx) + enum FILE_TYPE ftype; + int name_len; + int ret; ++ int sz; + + if (grub_f2fs_test_bit_le (i, ctx->bitmap) == 0) + { +@@ -1005,7 +1013,12 @@ grub_f2fs_check_dentries (struct grub_f2fs_dir_iter_ctx *ctx) + if (name_len >= F2FS_NAME_LEN) + return 0; + +- filename = grub_malloc (name_len + 1); ++ if (grub_add (name_len, 1, &sz)) ++ { ++ grub_error (GRUB_ERR_OUT_OF_RANGE, N_("directory entry name length overflow")); ++ return 0; ++ } ++ filename = grub_malloc (sz); + if (!filename) + return 0; + +diff --git a/grub-core/fs/ntfscomp.c b/grub-core/fs/ntfscomp.c +index 3cd97d337..4bf95c85d 100644 +--- a/grub-core/fs/ntfscomp.c ++++ b/grub-core/fs/ntfscomp.c +@@ -22,6 +22,7 @@ + #include + #include + #include ++#include + + GRUB_MOD_LICENSE ("GPLv3+"); + +@@ -310,6 +311,7 @@ ntfscomp (grub_uint8_t *dest, grub_disk_addr_t ofs, + { + grub_err_t ret; + grub_disk_addr_t vcn; ++ int log_sz; + + if (ctx->attr->sbuf) + { +@@ -349,7 +351,12 @@ ntfscomp (grub_uint8_t *dest, grub_disk_addr_t ofs, + } + + ctx->comp.comp_head = ctx->comp.comp_tail = 0; +- ctx->comp.cbuf = grub_malloc (1 << (ctx->comp.log_spc + GRUB_NTFS_BLK_SHR)); ++ if (grub_add (ctx->comp.log_spc, GRUB_NTFS_BLK_SHR, &log_sz)) ++ { ++ grub_error (GRUB_ERR_OUT_OF_RANGE, N_("compression buffer size overflow")); ++ return 0; ++ } ++ ctx->comp.cbuf = grub_malloc (1 << log_sz); + if (!ctx->comp.cbuf) + return 0; + +diff --git a/grub-core/fs/squash4.c b/grub-core/fs/squash4.c +index 1505832d5..4fe843935 100644 +--- a/grub-core/fs/squash4.c ++++ b/grub-core/fs/squash4.c +@@ -460,11 +460,11 @@ grub_squash_read_symlink (grub_fshelp_node_t node) + { + char *ret; + grub_err_t err; +- grub_size_t sz; ++ grub_uint32_t sz; + + if (grub_add (grub_le_to_cpu32 (node->ino.symlink.namelen), 1, &sz)) + { +- grub_error (GRUB_ERR_OUT_OF_RANGE, N_("overflow is detected")); ++ grub_error (GRUB_ERR_OUT_OF_RANGE, N_("symlink name length overflow")); + return NULL; + } + +@@ -577,6 +577,7 @@ grub_squash_iterate_dir (grub_fshelp_node_t dir, + struct grub_squash_dirent di; + struct grub_squash_inode ino; + grub_size_t sz; ++ grub_uint16_t nlen; + + err = read_chunk (dir->data, &di, sizeof (di), + grub_le_to_cpu64 (dir->data->sb.diroffset) +@@ -592,7 +593,12 @@ grub_squash_iterate_dir (grub_fshelp_node_t dir, + if (err) + return 0; + +- buf = grub_malloc (grub_le_to_cpu16 (di.namelen) + 2); ++ if (grub_add (grub_le_to_cpu16 (di.namelen), 2, &nlen)) ++ { ++ grub_error (GRUB_ERR_OUT_OF_RANGE, N_("name length overflow")); ++ return 0; ++ } ++ buf = grub_malloc (nlen); + if (!buf) + return 0; + err = read_chunk (dir->data, buf, +diff --git a/grub-core/fs/xfs.c b/grub-core/fs/xfs.c +index 5b7643eb1..b0d371ee1 100644 +--- a/grub-core/fs/xfs.c ++++ b/grub-core/fs/xfs.c +@@ -647,6 +647,7 @@ static char * + grub_xfs_read_symlink (grub_fshelp_node_t node) + { + grub_ssize_t size = grub_be_to_cpu64 (node->inode.size); ++ grub_size_t sz; + + if (size < 0) + { +@@ -668,7 +669,12 @@ grub_xfs_read_symlink (grub_fshelp_node_t node) + if (node->data->hascrc) + off = 56; + +- symlink = grub_malloc (size + 1); ++ if (grub_add (size, 1, &sz)) ++ { ++ grub_error (GRUB_ERR_OUT_OF_RANGE, N_("symlink size overflow")); ++ return 0; ++ } ++ symlink = grub_malloc (sz); + if (!symlink) + return 0; + +@@ -718,8 +724,15 @@ static int iterate_dir_call_hook (grub_uint64_t ino, const char *filename, + { + struct grub_fshelp_node *fdiro; + grub_err_t err; ++ grub_size_t sz; + +- fdiro = grub_malloc (grub_xfs_fshelp_size(ctx->diro->data) + 1); ++ if (grub_add (grub_xfs_fshelp_size(ctx->diro->data), 1, &sz)) ++ { ++ grub_error (GRUB_ERR_OUT_OF_RANGE, N_("directory data size overflow")); ++ grub_print_error (); ++ return 0; ++ } ++ fdiro = grub_malloc (sz); + if (!fdiro) + { + grub_print_error (); diff --git a/0644-fs-Prevent-overflows-when-allocating-memory-for-arra.patch b/0644-fs-Prevent-overflows-when-allocating-memory-for-arra.patch new file mode 100644 index 0000000..37b531b --- /dev/null +++ b/0644-fs-Prevent-overflows-when-allocating-memory-for-arra.patch @@ -0,0 +1,82 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Lidong Chen +Date: Tue, 21 Jan 2025 19:02:37 +0000 +Subject: [PATCH] fs: Prevent overflows when allocating memory for arrays + +Use grub_calloc() when allocating memory for arrays to ensure proper +overflow checks are in place. + +The HFS+ and squash4 security vulnerabilities were reported by +Jonathan Bar Or . + +Fixes: CVE-2025-0678 +Fixes: CVE-2025-1125 + +Signed-off-by: Lidong Chen +Reviewed-by: Daniel Kiper +--- + grub-core/fs/btrfs.c | 4 ++-- + grub-core/fs/hfspluscomp.c | 9 +++++++-- + grub-core/fs/squash4.c | 8 ++++---- + 3 files changed, 13 insertions(+), 8 deletions(-) + +diff --git a/grub-core/fs/btrfs.c b/grub-core/fs/btrfs.c +index 646543f5d..83336f606 100644 +--- a/grub-core/fs/btrfs.c ++++ b/grub-core/fs/btrfs.c +@@ -1015,8 +1015,8 @@ grub_btrfs_mount (grub_device_t dev) + } + + data->n_devices_allocated = 16; +- data->devices_attached = grub_malloc (sizeof (data->devices_attached[0]) +- * data->n_devices_allocated); ++ data->devices_attached = grub_calloc (data->n_devices_allocated, ++ sizeof (data->devices_attached[0])); + if (!data->devices_attached) + { + grub_free (data); +diff --git a/grub-core/fs/hfspluscomp.c b/grub-core/fs/hfspluscomp.c +index d76f3f137..4965ef19a 100644 +--- a/grub-core/fs/hfspluscomp.c ++++ b/grub-core/fs/hfspluscomp.c +@@ -244,14 +244,19 @@ hfsplus_open_compressed_real (struct grub_hfsplus_file *node) + return 0; + } + node->compress_index_size = grub_le_to_cpu32 (index_size); +- node->compress_index = grub_malloc (node->compress_index_size +- * sizeof (node->compress_index[0])); ++ node->compress_index = grub_calloc (node->compress_index_size, ++ sizeof (node->compress_index[0])); + if (!node->compress_index) + { + node->compressed = 0; + grub_free (attr_node); + return grub_errno; + } ++ ++ /* ++ * The node->compress_index_size * sizeof (node->compress_index[0]) is safe here ++ * due to relevant checks done in grub_calloc() above. ++ */ + if (grub_hfsplus_read_file (node, 0, 0, + 0x104 + sizeof (index_size), + node->compress_index_size +diff --git a/grub-core/fs/squash4.c b/grub-core/fs/squash4.c +index 4fe843935..b145bb9cd 100644 +--- a/grub-core/fs/squash4.c ++++ b/grub-core/fs/squash4.c +@@ -810,10 +810,10 @@ direct_read (struct grub_squash_data *data, + break; + } + total_blocks = ((total_size + data->blksz - 1) >> data->log2_blksz); +- ino->block_sizes = grub_malloc (total_blocks +- * sizeof (ino->block_sizes[0])); +- ino->cumulated_block_sizes = grub_malloc (total_blocks +- * sizeof (ino->cumulated_block_sizes[0])); ++ ino->block_sizes = grub_calloc (total_blocks, ++ sizeof (ino->block_sizes[0])); ++ ino->cumulated_block_sizes = grub_calloc (total_blocks, ++ sizeof (ino->cumulated_block_sizes[0])); + if (!ino->block_sizes || !ino->cumulated_block_sizes) + { + grub_free (ino->block_sizes); diff --git a/0645-fs-Prevent-overflows-when-assigning-returned-values-.patch b/0645-fs-Prevent-overflows-when-assigning-returned-values-.patch new file mode 100644 index 0000000..3c8d3e5 --- /dev/null +++ b/0645-fs-Prevent-overflows-when-assigning-returned-values-.patch @@ -0,0 +1,105 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Lidong Chen +Date: Tue, 21 Jan 2025 19:02:38 +0000 +Subject: [PATCH] fs: Prevent overflows when assigning returned values from + read_number() + +The direct assignment of the unsigned long long value returned by +read_number() can potentially lead to an overflow on a 32-bit systems. +The fix replaces the direct assignments with calls to grub_cast() +which detects the overflows and safely assigns the values if no +overflow is detected. + +Signed-off-by: Lidong Chen +Reviewed-by: Daniel Kiper +--- + grub-core/fs/cpio_common.c | 18 ++++++++++++++---- + grub-core/fs/tar.c | 23 ++++++++++++++++------- + 2 files changed, 30 insertions(+), 11 deletions(-) + +diff --git a/grub-core/fs/cpio_common.c b/grub-core/fs/cpio_common.c +index 5760072c5..bcb038025 100644 +--- a/grub-core/fs/cpio_common.c ++++ b/grub-core/fs/cpio_common.c +@@ -57,11 +57,21 @@ grub_cpio_find_file (struct grub_archelp_data *data, char **name, + #endif + ) + return grub_error (GRUB_ERR_BAD_FS, "invalid cpio archive"); +- data->size = read_number (hd.filesize, ARRAY_SIZE (hd.filesize)); ++ ++ if (grub_cast (read_number (hd.filesize, ARRAY_SIZE (hd.filesize)), &data->size)) ++ return grub_error (GRUB_ERR_BAD_FS, N_("data size overflow")); ++ + if (mtime) +- *mtime = read_number (hd.mtime, ARRAY_SIZE (hd.mtime)); +- modeval = read_number (hd.mode, ARRAY_SIZE (hd.mode)); +- namesize = read_number (hd.namesize, ARRAY_SIZE (hd.namesize)); ++ { ++ if (grub_cast (read_number (hd.mtime, ARRAY_SIZE (hd.mtime)), mtime)) ++ return grub_error (GRUB_ERR_BAD_FS, N_("mtime overflow")); ++ } ++ ++ if (grub_cast (read_number (hd.mode, ARRAY_SIZE (hd.mode)), &modeval)) ++ return grub_error (GRUB_ERR_BAD_FS, N_("mode overflow")); ++ ++ if (grub_cast (read_number (hd.namesize, ARRAY_SIZE (hd.namesize)), &namesize)) ++ return grub_error (GRUB_ERR_BAD_FS, N_("namesize overflow")); + + /* Don't allow negative numbers. */ + if (namesize >= 0x80000000) +diff --git a/grub-core/fs/tar.c b/grub-core/fs/tar.c +index 01738ec55..b8f121828 100644 +--- a/grub-core/fs/tar.c ++++ b/grub-core/fs/tar.c +@@ -99,9 +99,10 @@ grub_cpio_find_file (struct grub_archelp_data *data, char **name, + if (hd.typeflag == 'L') + { + grub_err_t err; +- grub_size_t namesize = read_number (hd.size, sizeof (hd.size)); ++ grub_size_t namesize; + +- if (grub_add (namesize, 1, &sz)) ++ if (grub_cast (read_number (hd.size, sizeof (hd.size)), &namesize) || ++ grub_add (namesize, 1, &sz)) + return grub_error (GRUB_ERR_BAD_FS, N_("name size overflow")); + + *name = grub_malloc (sz); +@@ -123,9 +124,10 @@ grub_cpio_find_file (struct grub_archelp_data *data, char **name, + if (hd.typeflag == 'K') + { + grub_err_t err; +- grub_size_t linksize = read_number (hd.size, sizeof (hd.size)); ++ grub_size_t linksize; + +- if (grub_add (linksize, 1, &sz)) ++ if (grub_cast (read_number (hd.size, sizeof (hd.size)), &linksize) || ++ grub_add (linksize, 1, &sz)) + return grub_error (GRUB_ERR_BAD_FS, N_("link size overflow")); + + if (data->linkname_alloc < sz) +@@ -174,15 +176,22 @@ grub_cpio_find_file (struct grub_archelp_data *data, char **name, + (*name)[extra_size + sizeof (hd.name)] = 0; + } + +- data->size = read_number (hd.size, sizeof (hd.size)); ++ if (grub_cast (read_number (hd.size, sizeof (hd.size)), &data->size)) ++ return grub_error (GRUB_ERR_BAD_FS, N_("data size overflow")); ++ + data->dofs = data->hofs + GRUB_DISK_SECTOR_SIZE; + data->next_hofs = data->dofs + ((data->size + GRUB_DISK_SECTOR_SIZE - 1) & + ~(GRUB_DISK_SECTOR_SIZE - 1)); + if (mtime) +- *mtime = read_number (hd.mtime, sizeof (hd.mtime)); ++ { ++ if (grub_cast (read_number (hd.mtime, sizeof (hd.mtime)), mtime)) ++ return grub_error (GRUB_ERR_BAD_FS, N_("mtime overflow")); ++ } + if (mode) + { +- *mode = read_number (hd.mode, sizeof (hd.mode)); ++ if (grub_cast (read_number (hd.mode, sizeof (hd.mode)), mode)) ++ return grub_error (GRUB_ERR_BAD_FS, N_("mode overflow")); ++ + switch (hd.typeflag) + { + /* Hardlink. */ diff --git a/0646-fs-zfs-Use-safe-math-macros-to-prevent-overflows.patch b/0646-fs-zfs-Use-safe-math-macros-to-prevent-overflows.patch new file mode 100644 index 0000000..a753c0d --- /dev/null +++ b/0646-fs-zfs-Use-safe-math-macros-to-prevent-overflows.patch @@ -0,0 +1,139 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Lidong Chen +Date: Wed, 22 Jan 2025 07:17:02 +0000 +Subject: [PATCH] fs/zfs: Use safe math macros to prevent overflows + +Replace direct arithmetic operations with macros from include/grub/safemath.h +to prevent potential overflow issues when calculating the memory sizes. + +Signed-off-by: Lidong Chen +Reviewed-by: Daniel Kiper +--- + grub-core/fs/zfs/zfs.c | 51 +++++++++++++++++++++++++++++++++++++++++++------- + 1 file changed, 44 insertions(+), 7 deletions(-) + +diff --git a/grub-core/fs/zfs/zfs.c b/grub-core/fs/zfs/zfs.c +index 0c4b15f08..4384d6aae 100644 +--- a/grub-core/fs/zfs/zfs.c ++++ b/grub-core/fs/zfs/zfs.c +@@ -2380,6 +2380,7 @@ fzap_iterate (dnode_end_t * zap_dnode, zap_phys_t * zap, + zap_dnode->endian) << DNODE_SHIFT); + grub_err_t err; + grub_zfs_endian_t endian; ++ grub_size_t sz; + + if (zap_verify (zap, zap_dnode->endian)) + return 0; +@@ -2441,8 +2442,14 @@ fzap_iterate (dnode_end_t * zap_dnode, zap_phys_t * zap, + if (le->le_type != ZAP_CHUNK_ENTRY) + continue; + +- buf = grub_malloc (grub_zfs_to_cpu16 (le->le_name_length, endian) +- * name_elem_length + 1); ++ if (grub_mul (grub_zfs_to_cpu16 (le->le_name_length, endian), name_elem_length, &sz) || ++ grub_add (sz, 1, &sz)) ++ { ++ grub_error (GRUB_ERR_OUT_OF_RANGE, N_("buffer size overflow")); ++ grub_free (l); ++ return grub_errno; ++ } ++ buf = grub_malloc (sz); + if (zap_leaf_array_get (l, endian, blksft, + grub_zfs_to_cpu16 (le->le_name_chunk, + endian), +@@ -2863,6 +2870,7 @@ dnode_get_path (struct subvolume *subvol, const char *path_in, dnode_end_t *dn, + && ((grub_zfs_to_cpu64(((znode_phys_t *) DN_BONUS (&dnode_path->dn.dn))->zp_mode, dnode_path->dn.endian) >> 12) & 0xf) == 0xa) + { + char *sym_value; ++ grub_size_t sz; + grub_size_t sym_sz; + int free_symval = 0; + char *oldpath = path, *oldpathbuf = path_buf; +@@ -2914,7 +2922,17 @@ dnode_get_path (struct subvolume *subvol, const char *path_in, dnode_end_t *dn, + break; + free_symval = 1; + } +- path = path_buf = grub_malloc (sym_sz + grub_strlen (oldpath) + 1); ++ if (grub_add (sym_sz, grub_strlen (oldpath), &sz) || ++ grub_add (sz, 1, &sz)) ++ { ++ grub_error (GRUB_ERR_OUT_OF_RANGE, N_("path buffer size overflow")); ++ grub_free (oldpathbuf); ++ if (free_symval) ++ grub_free (sym_value); ++ err = grub_errno; ++ break; ++ } ++ path = path_buf = grub_malloc (sz); + if (!path_buf) + { + grub_free (oldpathbuf); +@@ -2948,7 +2966,8 @@ dnode_get_path (struct subvolume *subvol, const char *path_in, dnode_end_t *dn, + { + void *sahdrp; + int hdrsize; +- ++ grub_size_t sz; ++ + if (dnode_path->dn.dn.dn_bonuslen != 0) + { + sahdrp = DN_BONUS (&dnode_path->dn.dn); +@@ -2981,7 +3000,15 @@ dnode_get_path (struct subvolume *subvol, const char *path_in, dnode_end_t *dn, + + SA_SIZE_OFFSET), + dnode_path->dn.endian); + char *oldpath = path, *oldpathbuf = path_buf; +- path = path_buf = grub_malloc (sym_sz + grub_strlen (oldpath) + 1); ++ if (grub_add (sym_sz, grub_strlen (oldpath), &sz) || ++ grub_add (sz, 1, &sz)) ++ { ++ grub_error (GRUB_ERR_OUT_OF_RANGE, N_("path buffer size overflow")); ++ grub_free (oldpathbuf); ++ err = grub_errno; ++ break; ++ } ++ path = path_buf = grub_malloc (sz); + if (!path_buf) + { + grub_free (oldpathbuf); +@@ -3550,6 +3577,7 @@ grub_zfs_nvlist_lookup_nvlist_array (const char *nvlist, const char *name, + unsigned i; + grub_size_t nelm; + int elemsize = 0; ++ int sz; + + found = nvlist_find_value (nvlist, name, DATA_TYPE_NVLIST_ARRAY, &nvpair, + &size, &nelm); +@@ -3584,7 +3612,12 @@ grub_zfs_nvlist_lookup_nvlist_array (const char *nvlist, const char *name, + return 0; + } + +- ret = grub_zalloc (elemsize + sizeof (grub_uint32_t)); ++ if (grub_add (elemsize, sizeof (grub_uint32_t), &sz)) ++ { ++ grub_error (GRUB_ERR_OUT_OF_RANGE, N_("elemsize overflow")); ++ return 0; ++ } ++ ret = grub_zalloc (sz); + if (!ret) + return 0; + grub_memcpy (ret, nvlist, sizeof (grub_uint32_t)); +@@ -4163,6 +4196,7 @@ iterate_zap_snap (const char *name, grub_uint64_t val, + struct grub_dirhook_info info; + char *name2; + int ret; ++ grub_size_t sz; + + dnode_end_t mdn; + +@@ -4183,7 +4217,10 @@ iterate_zap_snap (const char *name, grub_uint64_t val, + return 0; + } + +- name2 = grub_malloc (grub_strlen (name) + 2); ++ if (grub_add (grub_strlen (name), 2, &sz)) ++ return grub_error (GRUB_ERR_OUT_OF_RANGE, N_("name length overflow")); ++ ++ name2 = grub_malloc (sz); + name2[0] = '@'; + grub_memcpy (name2 + 1, name, grub_strlen (name) + 1); + ret = ctx->hook (name2, &info, ctx->hook_data); diff --git a/0647-fs-zfs-Prevent-overflows-when-allocating-memory-for-.patch b/0647-fs-zfs-Prevent-overflows-when-allocating-memory-for-.patch new file mode 100644 index 0000000..ff1758f --- /dev/null +++ b/0647-fs-zfs-Prevent-overflows-when-allocating-memory-for-.patch @@ -0,0 +1,42 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Lidong Chen +Date: Wed, 22 Jan 2025 07:17:03 +0000 +Subject: [PATCH] fs/zfs: Prevent overflows when allocating memory for arrays + +Use grub_calloc() when allocating memory for arrays to ensure proper +overflow checks are in place. + +Signed-off-by: Lidong Chen +Reviewed-by: Daniel Kiper +--- + grub-core/fs/zfs/zfs.c | 10 +++++----- + 1 file changed, 5 insertions(+), 5 deletions(-) + +diff --git a/grub-core/fs/zfs/zfs.c b/grub-core/fs/zfs/zfs.c +index 4384d6aae..23fe50211 100644 +--- a/grub-core/fs/zfs/zfs.c ++++ b/grub-core/fs/zfs/zfs.c +@@ -717,9 +717,9 @@ fill_vdev_info_real (struct grub_zfs_data *data, + if (!fill->children) + { + fill->n_children = nelm; +- +- fill->children = grub_zalloc (fill->n_children +- * sizeof (fill->children[0])); ++ ++ fill->children = grub_calloc (fill->n_children, ++ sizeof (fill->children[0])); + } + + for (i = 0; i < nelm; i++) +@@ -3693,8 +3693,8 @@ zfs_mount (grub_device_t dev) + #endif + + data->n_devices_allocated = 16; +- data->devices_attached = grub_malloc (sizeof (data->devices_attached[0]) +- * data->n_devices_allocated); ++ data->devices_attached = grub_calloc (data->n_devices_allocated, ++ sizeof (data->devices_attached[0])); + data->n_devices_attached = 0; + err = scan_disk (dev, data, 1, &inserted); + if (err) diff --git a/0648-fs-zfs-Check-if-returned-pointer-for-allocated-memor.patch b/0648-fs-zfs-Check-if-returned-pointer-for-allocated-memor.patch new file mode 100644 index 0000000..6ff2729 --- /dev/null +++ b/0648-fs-zfs-Check-if-returned-pointer-for-allocated-memor.patch @@ -0,0 +1,88 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Lidong Chen +Date: Wed, 22 Jan 2025 07:17:01 +0000 +Subject: [PATCH] fs/zfs: Check if returned pointer for allocated memory is + NULL + +When using grub_malloc() or grub_zalloc(), these functions can fail if +we are out of memory. After allocating memory we should check if these +functions returned NULL and handle this error if they did. + +Signed-off-by: Lidong Chen +Reviewed-by: Daniel Kiper +--- + grub-core/fs/zfs/zfs.c | 26 ++++++++++++++++++++++++++ + 1 file changed, 26 insertions(+) + +diff --git a/grub-core/fs/zfs/zfs.c b/grub-core/fs/zfs/zfs.c +index 23fe50211..434c3bd11 100644 +--- a/grub-core/fs/zfs/zfs.c ++++ b/grub-core/fs/zfs/zfs.c +@@ -609,6 +609,8 @@ zfs_fetch_nvlist (struct grub_zfs_device_desc *diskdesc, char **nvlist) + return grub_error (GRUB_ERR_BUG, "member drive unknown"); + + *nvlist = grub_malloc (VDEV_PHYS_SIZE); ++ if (!*nvlist) ++ return grub_errno; + + /* Read in the vdev name-value pair list (112K). */ + err = grub_disk_read (diskdesc->dev->disk, diskdesc->vdev_phys_sector, 0, +@@ -721,6 +723,11 @@ fill_vdev_info_real (struct grub_zfs_data *data, + fill->children = grub_calloc (fill->n_children, + sizeof (fill->children[0])); + } ++ if (!fill->children) ++ { ++ grub_free (type); ++ return grub_errno; ++ } + + for (i = 0; i < nelm; i++) + { +@@ -2450,6 +2457,11 @@ fzap_iterate (dnode_end_t * zap_dnode, zap_phys_t * zap, + return grub_errno; + } + buf = grub_malloc (sz); ++ if (!buf) ++ { ++ grub_free (l); ++ return grub_errno; ++ } + if (zap_leaf_array_get (l, endian, blksft, + grub_zfs_to_cpu16 (le->le_name_chunk, + endian), +@@ -2465,6 +2477,12 @@ fzap_iterate (dnode_end_t * zap_dnode, zap_phys_t * zap, + val_length = ((int) le->le_value_length + * (int) le->le_int_size); + val = grub_malloc (grub_zfs_to_cpu16 (val_length, endian)); ++ if (!val) ++ { ++ grub_free (l); ++ grub_free (buf); ++ return grub_errno; ++ } + if (zap_leaf_array_get (l, endian, blksft, + grub_zfs_to_cpu16 (le->le_value_chunk, + endian), +@@ -3695,6 +3713,11 @@ zfs_mount (grub_device_t dev) + data->n_devices_allocated = 16; + data->devices_attached = grub_calloc (data->n_devices_allocated, + sizeof (data->devices_attached[0])); ++ if (!data->devices_attached) ++ { ++ grub_free (data); ++ return NULL; ++ } + data->n_devices_attached = 0; + err = scan_disk (dev, data, 1, &inserted); + if (err) +@@ -4221,6 +4244,9 @@ iterate_zap_snap (const char *name, grub_uint64_t val, + return grub_error (GRUB_ERR_OUT_OF_RANGE, N_("name length overflow")); + + name2 = grub_malloc (sz); ++ if (!name2) ++ return grub_errno; ++ + name2[0] = '@'; + grub_memcpy (name2 + 1, name, grub_strlen (name) + 1); + ret = ctx->hook (name2, &info, ctx->hook_data); diff --git a/0649-fs-zfs-Add-missing-NULL-check-after-grub_strdup-call.patch b/0649-fs-zfs-Add-missing-NULL-check-after-grub_strdup-call.patch new file mode 100644 index 0000000..d5fb7a2 --- /dev/null +++ b/0649-fs-zfs-Add-missing-NULL-check-after-grub_strdup-call.patch @@ -0,0 +1,24 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Lidong Chen +Date: Wed, 22 Jan 2025 07:17:04 +0000 +Subject: [PATCH] fs/zfs: Add missing NULL check after grub_strdup() call + +Signed-off-by: Lidong Chen +Reviewed-by: Daniel Kiper +--- + grub-core/fs/zfs/zfs.c | 2 ++ + 1 file changed, 2 insertions(+) + +diff --git a/grub-core/fs/zfs/zfs.c b/grub-core/fs/zfs/zfs.c +index 434c3bd11..5254e75e1 100644 +--- a/grub-core/fs/zfs/zfs.c ++++ b/grub-core/fs/zfs/zfs.c +@@ -3293,6 +3293,8 @@ dnode_get_fullpath (const char *fullpath, struct subvolume *subvol, + filename = 0; + snapname = 0; + fsname = grub_strdup (fullpath); ++ if (!fsname) ++ return grub_errno; + } + else + { diff --git a/0650-net-Use-safe-math-macros-to-prevent-overflows.patch b/0650-net-Use-safe-math-macros-to-prevent-overflows.patch new file mode 100644 index 0000000..5436623 --- /dev/null +++ b/0650-net-Use-safe-math-macros-to-prevent-overflows.patch @@ -0,0 +1,239 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Lidong Chen +Date: Wed, 22 Jan 2025 18:04:42 +0000 +Subject: [PATCH] net: Use safe math macros to prevent overflows + +Replace direct arithmetic operations with macros from include/grub/safemath.h +to prevent potential overflow issues when calculating the memory sizes. + +Signed-off-by: Lidong Chen +Signed-off-by: Alec Brown +Reviewed-by: Daniel Kiper +--- + grub-core/net/bootp.c | 16 +++++++++++-- + grub-core/net/dns.c | 9 ++++++- + grub-core/net/drivers/ieee1275/ofnet.c | 20 ++++++++++++++-- + grub-core/net/net.c | 43 +++++++++++++++++++++++++++------- + 4 files changed, 75 insertions(+), 13 deletions(-) + +diff --git a/grub-core/net/bootp.c b/grub-core/net/bootp.c +index adf36fa4a..786340bf0 100644 +--- a/grub-core/net/bootp.c ++++ b/grub-core/net/bootp.c +@@ -28,6 +28,7 @@ + #include + #include + #include ++#include + + static int + dissect_url (const char *url, char **proto, char **host, char **path) +@@ -1190,6 +1191,7 @@ grub_cmd_dhcpopt (struct grub_command *cmd __attribute__ ((unused)), + int num; + grub_uint8_t *ptr; + grub_uint8_t taglength; ++ grub_uint8_t len; + + if (argc < 4) + return grub_error (GRUB_ERR_BAD_ARGUMENT, +@@ -1248,7 +1250,12 @@ grub_cmd_dhcpopt (struct grub_command *cmd __attribute__ ((unused)), + if (grub_strcmp (args[3], "string") == 0) + { + grub_err_t err = GRUB_ERR_NONE; +- char *val = grub_malloc (taglength + 1); ++ char *val; ++ ++ if (grub_add (taglength, 1, &len)) ++ return grub_error (GRUB_ERR_OUT_OF_RANGE, N_("tag length overflow")); ++ ++ val = grub_malloc (len); + if (!val) + return grub_errno; + grub_memcpy (val, ptr, taglength); +@@ -1281,7 +1288,12 @@ grub_cmd_dhcpopt (struct grub_command *cmd __attribute__ ((unused)), + if (grub_strcmp (args[3], "hex") == 0) + { + grub_err_t err = GRUB_ERR_NONE; +- char *val = grub_malloc (2 * taglength + 1); ++ char *val; ++ ++ if (grub_mul (taglength, 2, &len) || grub_add (len, 1, &len)) ++ return grub_error (GRUB_ERR_OUT_OF_RANGE, N_("tag length overflow")); ++ ++ val = grub_malloc (len); + int i; + if (!val) + return grub_errno; +diff --git a/grub-core/net/dns.c b/grub-core/net/dns.c +index 17961a9f1..64b48a31c 100644 +--- a/grub-core/net/dns.c ++++ b/grub-core/net/dns.c +@@ -224,10 +224,17 @@ get_name (const grub_uint8_t *name_at, const grub_uint8_t *head, + { + int length; + char *ret; ++ int len; + + if (!check_name_real (name_at, head, tail, NULL, &length, NULL)) + return NULL; +- ret = grub_malloc (length + 1); ++ ++ if (grub_add (length, 1, &len)) ++ { ++ grub_error (GRUB_ERR_OUT_OF_RANGE, N_("name length overflow")); ++ return NULL; ++ } ++ ret = grub_malloc (len); + if (!ret) + return NULL; + if (!check_name_real (name_at, head, tail, NULL, NULL, ret)) +diff --git a/grub-core/net/drivers/ieee1275/ofnet.c b/grub-core/net/drivers/ieee1275/ofnet.c +index ba50415f5..753d54e43 100644 +--- a/grub-core/net/drivers/ieee1275/ofnet.c ++++ b/grub-core/net/drivers/ieee1275/ofnet.c +@@ -22,6 +22,7 @@ + #include + #include + #include ++#include + + GRUB_MOD_LICENSE ("GPLv3+"); + +@@ -404,6 +405,7 @@ search_net_devices (struct grub_ieee1275_devalias *alias) + grub_uint8_t *pprop; + char *shortname; + char need_suffix = 1; ++ grub_size_t sz; + + if (grub_strcmp (alias->type, "network") != 0) + return 0; +@@ -461,9 +463,23 @@ search_net_devices (struct grub_ieee1275_devalias *alias) + } + + if (need_suffix) +- ofdata->path = grub_malloc (grub_strlen (alias->path) + sizeof (SUFFIX)); ++ { ++ if (grub_add (grub_strlen (alias->path), sizeof (SUFFIX), &sz)) ++ { ++ grub_error (GRUB_ERR_OUT_OF_RANGE, N_("overflow detected while obatining size of ofdata path")); ++ grub_print_error (); ++ return 0; ++ } ++ } + else +- ofdata->path = grub_malloc (grub_strlen (alias->path) + 1); ++ { ++ if (grub_add (grub_strlen (alias->path), 1, &sz)) ++ { ++ grub_error (GRUB_ERR_OUT_OF_RANGE, N_("overflow detected while obatining size of ofdata path")); ++ grub_print_error (); ++ return 0; ++ } ++ } + if (!ofdata->path) + { + grub_print_error (); +diff --git a/grub-core/net/net.c b/grub-core/net/net.c +index f0896979d..10ea4ae71 100644 +--- a/grub-core/net/net.c ++++ b/grub-core/net/net.c +@@ -32,6 +32,7 @@ + #include + #include + #include ++#include + #ifdef GRUB_MACHINE_EFI + #include + #endif +@@ -211,6 +212,7 @@ grub_net_ipv6_get_slaac (struct grub_net_card *card, + { + struct grub_net_slaac_mac_list *slaac; + char *ptr; ++ grub_size_t sz; + + for (slaac = card->slaac_list; slaac; slaac = slaac->next) + if (grub_net_hwaddr_cmp (&slaac->address, hwaddr) == 0) +@@ -220,9 +222,16 @@ grub_net_ipv6_get_slaac (struct grub_net_card *card, + if (!slaac) + return NULL; + +- slaac->name = grub_malloc (grub_strlen (card->name) +- + GRUB_NET_MAX_STR_HWADDR_LEN +- + sizeof (":slaac")); ++ if (grub_add (grub_strlen (card->name), ++ (GRUB_NET_MAX_STR_HWADDR_LEN + sizeof (":slaac")), &sz)) ++ { ++ grub_free (slaac); ++ grub_error (GRUB_ERR_OUT_OF_RANGE, ++ "overflow detected while obtaining size of slaac name"); ++ return NULL; ++ } ++ ++ slaac->name = grub_malloc (sz); + ptr = grub_stpcpy (slaac->name, card->name); + if (grub_net_hwaddr_cmp (&card->default_address, hwaddr) != 0) + { +@@ -293,6 +302,7 @@ grub_net_ipv6_get_link_local (struct grub_net_card *card, + char *name; + char *ptr; + grub_net_network_level_address_t addr; ++ grub_size_t sz; + + addr.type = GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV6; + addr.ipv6[0] = grub_cpu_to_be64_compile_time (0xfe80ULL << 48); +@@ -306,9 +316,14 @@ grub_net_ipv6_get_link_local (struct grub_net_card *card, + return inf; + } + +- name = grub_malloc (grub_strlen (card->name) +- + GRUB_NET_MAX_STR_HWADDR_LEN +- + sizeof (":link")); ++ if (grub_add (grub_strlen (card->name), ++ (GRUB_NET_MAX_STR_HWADDR_LEN + sizeof (":link")), &sz)) ++ { ++ grub_error (GRUB_ERR_OUT_OF_RANGE, ++ "overflow detected while obtaining size of link name"); ++ return NULL; ++ } ++ name = grub_malloc (sz); + if (!name) + return NULL; + +@@ -1462,8 +1477,14 @@ grub_net_open_real (const char *name) + if (grub_strchr (port_start + 1, ':')) + { + int iplen = grub_strlen (server); ++ grub_size_t sz; + /* bracket bare ipv6 addrs */ +- host = grub_malloc (iplen + 3); ++ if (grub_add (iplen, 3, &sz)) ++ { ++ grub_error (GRUB_ERR_OUT_OF_RANGE, N_("overflow detected while obtaining length of host")); ++ return NULL; ++ } ++ host = grub_malloc (sz); + if(!host) + { + return NULL; +@@ -1723,6 +1744,7 @@ grub_env_set_net_property (const char *intername, const char *suffix, + { + char *varname, *varvalue; + char *ptr; ++ grub_size_t sz; + + varname = grub_xasprintf ("net_%s_%s", intername, suffix); + if (!varname) +@@ -1730,7 +1752,12 @@ grub_env_set_net_property (const char *intername, const char *suffix, + for (ptr = varname; *ptr; ptr++) + if (*ptr == ':') + *ptr = '_'; +- varvalue = grub_malloc (len + 1); ++ if (grub_add (len, 1, &sz)) ++ { ++ grub_free (varname); ++ return grub_error (GRUB_ERR_OUT_OF_RANGE, "overflow detected while obtaining the size of an env variable"); ++ } ++ varvalue = grub_malloc (sz); + if (!varvalue) + { + grub_free (varname); diff --git a/0651-net-Prevent-overflows-when-allocating-memory-for-arr.patch b/0651-net-Prevent-overflows-when-allocating-memory-for-arr.patch new file mode 100644 index 0000000..6c6f4a6 --- /dev/null +++ b/0651-net-Prevent-overflows-when-allocating-memory-for-arr.patch @@ -0,0 +1,45 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Lidong Chen +Date: Wed, 22 Jan 2025 18:04:43 +0000 +Subject: [PATCH] net: Prevent overflows when allocating memory for arrays + +Use grub_calloc() when allocating memory for arrays to ensure proper +overflow checks are in place. + +Signed-off-by: Lidong Chen +Reviewed-by: Daniel Kiper +--- + grub-core/net/dns.c | 4 ++-- + grub-core/net/net.c | 4 ++-- + 2 files changed, 4 insertions(+), 4 deletions(-) + +diff --git a/grub-core/net/dns.c b/grub-core/net/dns.c +index 64b48a31c..aafd92902 100644 +--- a/grub-core/net/dns.c ++++ b/grub-core/net/dns.c +@@ -494,8 +494,8 @@ grub_net_dns_lookup (const char *name, + && grub_get_time_ms () < dns_cache[h].limit_time) + { + grub_dprintf ("dns", "retrieved from cache\n"); +- *addresses = grub_malloc (dns_cache[h].naddresses +- * sizeof ((*addresses)[0])); ++ *addresses = grub_calloc (dns_cache[h].naddresses, ++ sizeof ((*addresses)[0])); + if (!*addresses) + return grub_errno; + *naddresses = dns_cache[h].naddresses; +diff --git a/grub-core/net/net.c b/grub-core/net/net.c +index 10ea4ae71..06f4b1c9f 100644 +--- a/grub-core/net/net.c ++++ b/grub-core/net/net.c +@@ -91,8 +91,8 @@ grub_net_link_layer_add_address (struct grub_net_card *card, + /* Add sender to cache table. */ + if (card->link_layer_table == NULL) + { +- card->link_layer_table = grub_zalloc (LINK_LAYER_CACHE_SIZE +- * sizeof (card->link_layer_table[0])); ++ card->link_layer_table = grub_calloc (LINK_LAYER_CACHE_SIZE, ++ sizeof (card->link_layer_table[0])); + if (card->link_layer_table == NULL) + return; + } diff --git a/0652-net-Check-if-returned-pointer-for-allocated-memory-i.patch b/0652-net-Check-if-returned-pointer-for-allocated-memory-i.patch new file mode 100644 index 0000000..62d3f5a --- /dev/null +++ b/0652-net-Check-if-returned-pointer-for-allocated-memory-i.patch @@ -0,0 +1,31 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Alec Brown +Date: Wed, 22 Jan 2025 18:04:44 +0000 +Subject: [PATCH] net: Check if returned pointer for allocated memory is NULL + +When using grub_malloc(), the function can fail if we are out of memory. +After allocating memory we should check if this function returned NULL +and handle this error if it did. + +Signed-off-by: Alec Brown +Reviewed-by: Daniel Kiper +--- + grub-core/net/net.c | 5 +++++ + 1 file changed, 5 insertions(+) + +diff --git a/grub-core/net/net.c b/grub-core/net/net.c +index 06f4b1c9f..6fcbe6fd6 100644 +--- a/grub-core/net/net.c ++++ b/grub-core/net/net.c +@@ -232,6 +232,11 @@ grub_net_ipv6_get_slaac (struct grub_net_card *card, + } + + slaac->name = grub_malloc (sz); ++ if (slaac->name == NULL) ++ { ++ grub_free (slaac); ++ return NULL; ++ } + ptr = grub_stpcpy (slaac->name, card->name); + if (grub_net_hwaddr_cmp (&card->default_address, hwaddr) != 0) + { diff --git a/0653-fs-sfs-Check-if-allocated-memory-is-NULL.patch b/0653-fs-sfs-Check-if-allocated-memory-is-NULL.patch new file mode 100644 index 0000000..1c0756a --- /dev/null +++ b/0653-fs-sfs-Check-if-allocated-memory-is-NULL.patch @@ -0,0 +1,32 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Alec Brown +Date: Tue, 28 Jan 2025 05:15:50 +0000 +Subject: [PATCH] fs/sfs: Check if allocated memory is NULL + +When using grub_zalloc(), if we are out of memory, this function can fail. +After allocating memory, we should check if grub_zalloc() returns NULL. +If so, we should handle this error. + +Fixes: CID 473856 + +Signed-off-by: Alec Brown +Reviewed-by: Ross Philipson +Reviewed-by: Daniel Kiper +--- + grub-core/fs/sfs.c | 3 +++ + 1 file changed, 3 insertions(+) + +diff --git a/grub-core/fs/sfs.c b/grub-core/fs/sfs.c +index a272aea0a..36c5fd656 100644 +--- a/grub-core/fs/sfs.c ++++ b/grub-core/fs/sfs.c +@@ -429,6 +429,9 @@ grub_sfs_mount (grub_disk_t disk) + - 24 /* offsetof (struct grub_sfs_objc, objects) */ + - 25); /* offsetof (struct grub_sfs_obj, filename) */ + data->label = grub_zalloc (max_len + 1); ++ if (data->label == NULL) ++ goto fail; ++ + grub_strncpy (data->label, (char *) rootobjc->objects[0].filename, max_len); + + grub_free (rootobjc_data); diff --git a/0654-script-execute-Fix-potential-underflow-and-NULL-dere.patch b/0654-script-execute-Fix-potential-underflow-and-NULL-dere.patch new file mode 100644 index 0000000..1eb31c5 --- /dev/null +++ b/0654-script-execute-Fix-potential-underflow-and-NULL-dere.patch @@ -0,0 +1,32 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Lidong Chen +Date: Wed, 29 Jan 2025 06:48:37 +0000 +Subject: [PATCH] script/execute: Fix potential underflow and NULL dereference + +The result is initialized to 0 in grub_script_arglist_to_argv(). +If the for loop condition is not met both result.args and result.argc +remain 0 causing result.argc - 1 to underflow and/or result.args NULL +dereference. Fix the issues by adding relevant checks. + +Fixes: CID 473880 + +Signed-off-by: Lidong Chen +Reviewed-by: Daniel Kiper +--- + grub-core/script/execute.c | 3 +++ + 1 file changed, 3 insertions(+) + +diff --git a/grub-core/script/execute.c b/grub-core/script/execute.c +index ef9a01642..961e9722c 100644 +--- a/grub-core/script/execute.c ++++ b/grub-core/script/execute.c +@@ -794,6 +794,9 @@ cleanup: + } + } + ++ if (result.args == NULL || result.argc == 0) ++ goto fail; ++ + if (! result.args[result.argc - 1]) + result.argc--; + diff --git a/0655-osdep-unix-getroot-Fix-potential-underflow.patch b/0655-osdep-unix-getroot-Fix-potential-underflow.patch new file mode 100644 index 0000000..e9fddb1 --- /dev/null +++ b/0655-osdep-unix-getroot-Fix-potential-underflow.patch @@ -0,0 +1,35 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Lidong Chen +Date: Wed, 29 Jan 2025 06:48:38 +0000 +Subject: [PATCH] osdep/unix/getroot: Fix potential underflow + +The entry_len is initialized in grub_find_root_devices_from_mountinfo() +to 0 before the while loop iterates through /proc/self/mountinfo. If the +file is empty or contains only invalid entries entry_len remains +0 causing entry_len - 1 in the subsequent for loop initialization +to underflow. To prevent this add a check to ensure entry_len > 0 before +entering the for loop. + +Fixes: CID 473877 + +Signed-off-by: Lidong Chen +Reviewed-by: Daniel Kiper +Reviewed-by: Ross Philipson +--- + grub-core/osdep/linux/getroot.c | 3 +++ + 1 file changed, 3 insertions(+) + +diff --git a/grub-core/osdep/linux/getroot.c b/grub-core/osdep/linux/getroot.c +index e450f5f0a..27a4d7c81 100644 +--- a/grub-core/osdep/linux/getroot.c ++++ b/grub-core/osdep/linux/getroot.c +@@ -599,6 +599,9 @@ again: + } + } + ++ if (!entry_len) ++ goto out; ++ + /* Now scan visible mounts for the ones we're interested in. */ + for (i = entry_len - 1; i >= 0; i--) + { diff --git a/0656-misc-Ensure-consistent-overflow-error-messages.patch b/0656-misc-Ensure-consistent-overflow-error-messages.patch new file mode 100644 index 0000000..31722be --- /dev/null +++ b/0656-misc-Ensure-consistent-overflow-error-messages.patch @@ -0,0 +1,55 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Lidong Chen +Date: Tue, 21 Jan 2025 19:02:39 +0000 +Subject: [PATCH] misc: Ensure consistent overflow error messages + +Update the overflow error messages to make them consistent +across the GRUB code. + +Signed-off-by: Lidong Chen +Reviewed-by: Daniel Kiper +--- + grub-core/fs/ntfs.c | 2 +- + grub-core/fs/ntfscomp.c | 2 +- + grub-core/video/readers/png.c | 2 +- + 3 files changed, 3 insertions(+), 3 deletions(-) + +diff --git a/grub-core/fs/ntfs.c b/grub-core/fs/ntfs.c +index 1ad3a2715..f241c5237 100644 +--- a/grub-core/fs/ntfs.c ++++ b/grub-core/fs/ntfs.c +@@ -574,7 +574,7 @@ retry: + goto retry; + } + } +- return grub_error (GRUB_ERR_BAD_FS, "run list overflown"); ++ return grub_error (GRUB_ERR_BAD_FS, "run list overflow"); + } + ctx->curr_vcn = ctx->next_vcn; + ctx->next_vcn += read_run_data (run, c1, 0); /* length of current VCN */ +diff --git a/grub-core/fs/ntfscomp.c b/grub-core/fs/ntfscomp.c +index 4bf95c85d..88594702e 100644 +--- a/grub-core/fs/ntfscomp.c ++++ b/grub-core/fs/ntfscomp.c +@@ -30,7 +30,7 @@ static grub_err_t + decomp_nextvcn (struct grub_ntfs_comp *cc) + { + if (cc->comp_head >= cc->comp_tail) +- return grub_error (GRUB_ERR_BAD_FS, "compression block overflown"); ++ return grub_error (GRUB_ERR_BAD_FS, "compression block overflow"); + if (grub_disk_read + (cc->disk, + (cc->comp_table[cc->comp_head].next_lcn - +diff --git a/grub-core/video/readers/png.c b/grub-core/video/readers/png.c +index 7f2ba7849..3ff215dd6 100644 +--- a/grub-core/video/readers/png.c ++++ b/grub-core/video/readers/png.c +@@ -622,7 +622,7 @@ static grub_err_t + grub_png_output_byte (struct grub_png_data *data, grub_uint8_t n) + { + if (--data->raw_bytes < 0) +- return grub_error (GRUB_ERR_BAD_FILE_TYPE, "image size overflown"); ++ return grub_error (GRUB_ERR_BAD_FILE_TYPE, "image size overflow"); + + if (data->cur_column == 0) + { diff --git a/0657-bus-usb-ehci-Define-GRUB_EHCI_TOGGLE-as-grub_uint32_.patch b/0657-bus-usb-ehci-Define-GRUB_EHCI_TOGGLE-as-grub_uint32_.patch new file mode 100644 index 0000000..c919216 --- /dev/null +++ b/0657-bus-usb-ehci-Define-GRUB_EHCI_TOGGLE-as-grub_uint32_.patch @@ -0,0 +1,30 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Alec Brown +Date: Tue, 4 Feb 2025 15:11:10 +0000 +Subject: [PATCH] bus/usb/ehci: Define GRUB_EHCI_TOGGLE as grub_uint32_t + +The Coverity indicates that GRUB_EHCI_TOGGLE is an int that contains +a negative value and we are using it for the variable token which is +grub_uint32_t. To remedy this we can cast the definition to grub_uint32_t. + +Fixes: CID 473851 + +Signed-off-by: Alec Brown +Reviewed-by: Daniel Kiper +--- + grub-core/bus/usb/ehci.c | 2 +- + 1 file changed, 1 insertion(+), 1 deletion(-) + +diff --git a/grub-core/bus/usb/ehci.c b/grub-core/bus/usb/ehci.c +index d966fc210..d1e8a01ca 100644 +--- a/grub-core/bus/usb/ehci.c ++++ b/grub-core/bus/usb/ehci.c +@@ -218,7 +218,7 @@ enum + + #define GRUB_EHCI_TERMINATE (1<<0) + +-#define GRUB_EHCI_TOGGLE (1<<31) ++#define GRUB_EHCI_TOGGLE ((grub_uint32_t) 1<<31) + + enum + { diff --git a/0658-normal-menu-Use-safe-math-to-avoid-an-integer-overfl.patch b/0658-normal-menu-Use-safe-math-to-avoid-an-integer-overfl.patch new file mode 100644 index 0000000..fbe026b --- /dev/null +++ b/0658-normal-menu-Use-safe-math-to-avoid-an-integer-overfl.patch @@ -0,0 +1,41 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Alec Brown +Date: Tue, 4 Feb 2025 15:11:11 +0000 +Subject: [PATCH] normal/menu: Use safe math to avoid an integer overflow + +The Coverity indicates that the variable current_entry might overflow. +To prevent this use safe math when adding GRUB_MENU_PAGE_SIZE to current_entry. + +On the occasion fix limiting condition which was broken. + +Fixes: CID 473853 + +Signed-off-by: Alec Brown +Reviewed-by: Daniel Kiper +--- + grub-core/normal/menu.c | 5 ++--- + 1 file changed, 2 insertions(+), 3 deletions(-) + +diff --git a/grub-core/normal/menu.c b/grub-core/normal/menu.c +index 7e32c498a..ff2bebe82 100644 +--- a/grub-core/normal/menu.c ++++ b/grub-core/normal/menu.c +@@ -32,6 +32,7 @@ + #include + #include + #include ++#include + + /* Time to delay after displaying an error message about a default/fallback + entry failing to boot. */ +@@ -780,9 +781,7 @@ run_menu (grub_menu_t menu, int nested, int *auto_boot) + + case GRUB_TERM_CTRL | 'c': + case GRUB_TERM_KEY_NPAGE: +- if (current_entry + GRUB_MENU_PAGE_SIZE < menu->size) +- current_entry += GRUB_MENU_PAGE_SIZE; +- else ++ if (grub_add (current_entry, GRUB_MENU_PAGE_SIZE, ¤t_entry) || current_entry >= menu->size) + current_entry = menu->size - 1; + menu_set_chosen_entry (current_entry); + break; diff --git a/0659-kern-partition-Add-sanity-check-after-grub_strtoul-c.patch b/0659-kern-partition-Add-sanity-check-after-grub_strtoul-c.patch new file mode 100644 index 0000000..cb29426 --- /dev/null +++ b/0659-kern-partition-Add-sanity-check-after-grub_strtoul-c.patch @@ -0,0 +1,45 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Lidong Chen +Date: Thu, 6 Feb 2025 18:16:56 +0000 +Subject: [PATCH] kern/partition: Add sanity check after grub_strtoul() call + +The current code incorrectly assumes that both the input and the values +returned by grub_strtoul() are always valid which can lead to potential +errors. This fix ensures proper validation to prevent any unintended issues. + +Fixes: CID 473843 + +Signed-off-by: Lidong Chen +Reviewed-by: Daniel Kiper +--- + grub-core/kern/partition.c | 11 +++++++++-- + 1 file changed, 9 insertions(+), 2 deletions(-) + +diff --git a/grub-core/kern/partition.c b/grub-core/kern/partition.c +index f3f125e75..f95efe8ce 100644 +--- a/grub-core/kern/partition.c ++++ b/grub-core/kern/partition.c +@@ -125,14 +125,21 @@ grub_partition_probe (struct grub_disk *disk, const char *str) + for (ptr = str; *ptr;) + { + grub_partition_map_t partmap; +- int num; ++ unsigned long num; + const char *partname, *partname_end; + + partname = ptr; + while (*ptr && grub_isalpha (*ptr)) + ptr++; + partname_end = ptr; +- num = grub_strtoul (ptr, &ptr, 0) - 1; ++ num = grub_strtoul (ptr, &ptr, 0); ++ if (*ptr != '\0' || num == 0 || num > GRUB_INT_MAX) ++ { ++ grub_error (GRUB_ERR_BAD_NUMBER, N_("invalid partition number")); ++ return 0; ++ } ++ ++ num -= 1; + + curpart = 0; + /* Use the first partition map type found. */ diff --git a/0660-kern-misc-Add-sanity-check-after-grub_strtoul-call.patch b/0660-kern-misc-Add-sanity-check-after-grub_strtoul-call.patch new file mode 100644 index 0000000..9d1f69c --- /dev/null +++ b/0660-kern-misc-Add-sanity-check-after-grub_strtoul-call.patch @@ -0,0 +1,57 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Lidong Chen +Date: Thu, 6 Feb 2025 18:16:57 +0000 +Subject: [PATCH] kern/misc: Add sanity check after grub_strtoul() call + +When the format string, fmt0, includes a positional argument +grub_strtoul() or grub_strtoull() is called to extract the argument +position. However, the returned argument position isn't fully validated. +If the format is something like "%0$x" then these functions return +0 which leads to an underflow in the calculation of the args index, curn. +The fix is to add a check to ensure the extracted argument position is +greater than 0 before computing curn. Additionally, replace one +grub_strtoull() with grub_strtoul() and change curn type to make code +more correct. + +Fixes: CID 473841 + +Signed-off-by: Lidong Chen +Reviewed-by: Daniel Kiper +--- + grub-core/kern/misc.c | 9 +++++++-- + 1 file changed, 7 insertions(+), 2 deletions(-) + +diff --git a/grub-core/kern/misc.c b/grub-core/kern/misc.c +index b375e486a..8f211e4c7 100644 +--- a/grub-core/kern/misc.c ++++ b/grub-core/kern/misc.c +@@ -830,7 +830,7 @@ parse_printf_arg_fmt (const char *fmt0, struct printf_args *args, + while ((c = *fmt++) != 0) + { + int longfmt = 0; +- grub_size_t curn; ++ unsigned long curn; + const char *p; + + if (c != '%') +@@ -848,7 +848,10 @@ parse_printf_arg_fmt (const char *fmt0, struct printf_args *args, + + if (*fmt == '$') + { +- curn = grub_strtoull (p, 0, 10) - 1; ++ curn = grub_strtoul (p, 0, 10); ++ if (curn == 0) ++ continue; ++ curn--; + fmt++; + } + +@@ -1007,6 +1010,8 @@ grub_vsnprintf_real (char *str, grub_size_t max_len, const char *fmt0, + + if (*fmt == '$') + { ++ if (format1 == 0) ++ continue; + curn = format1 - 1; + fmt++; + format1 = 0; diff --git a/0661-loader-i386-linux-Cast-left-shift-to-grub_uint32_t.patch b/0661-loader-i386-linux-Cast-left-shift-to-grub_uint32_t.patch new file mode 100644 index 0000000..5d042cb --- /dev/null +++ b/0661-loader-i386-linux-Cast-left-shift-to-grub_uint32_t.patch @@ -0,0 +1,30 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Alec Brown +Date: Fri, 7 Feb 2025 01:47:57 +0000 +Subject: [PATCH] loader/i386/linux: Cast left shift to grub_uint32_t + +The Coverity complains that we might overflow into a negative value when +setting linux_params.kernel_alignment to (1 << align). We can remedy +this by casting it to grub_uint32_t. + +Fixes: CID 473876 + +Signed-off-by: Alec Brown +Reviewed-by: Daniel Kiper +--- + grub-core/loader/i386/linux.c | 2 +- + 1 file changed, 1 insertion(+), 1 deletion(-) + +diff --git a/grub-core/loader/i386/linux.c b/grub-core/loader/i386/linux.c +index c160ddb0e..b72b7d591 100644 +--- a/grub-core/loader/i386/linux.c ++++ b/grub-core/loader/i386/linux.c +@@ -837,7 +837,7 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)), + GRUB_MEM_ATTR_R|GRUB_MEM_ATTR_W, GRUB_MEM_ATTR_X); + + linux_params.code32_start = prot_mode_target + lh.code32_start - GRUB_LINUX_BZIMAGE_ADDR; +- linux_params.kernel_alignment = (1 << align); ++ linux_params.kernel_alignment = ((grub_uint32_t) 1 << align); + linux_params.ps_mouse = linux_params.padding10 = 0; + + len = sizeof (linux_params) - sizeof (lh); diff --git a/0662-loader-i386-bsd-Use-safe-math-to-avoid-underflow.patch b/0662-loader-i386-bsd-Use-safe-math-to-avoid-underflow.patch new file mode 100644 index 0000000..304f96c --- /dev/null +++ b/0662-loader-i386-bsd-Use-safe-math-to-avoid-underflow.patch @@ -0,0 +1,56 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Alec Brown +Date: Wed, 5 Feb 2025 22:04:08 +0000 +Subject: [PATCH] loader/i386/bsd: Use safe math to avoid underflow + +The operation kern_end - kern_start may underflow when we input it into +grub_relocator_alloc_chunk_addr() call. To avoid this we can use safe +math for this subtraction. + +Fixes: CID 73845 + +Signed-off-by: Alec Brown +Reviewed-by: Daniel Kiper +--- + grub-core/loader/i386/bsd.c | 14 ++++++++++---- + 1 file changed, 10 insertions(+), 4 deletions(-) + +diff --git a/grub-core/loader/i386/bsd.c b/grub-core/loader/i386/bsd.c +index b5ab848ee..ff1658503 100644 +--- a/grub-core/loader/i386/bsd.c ++++ b/grub-core/loader/i386/bsd.c +@@ -1341,6 +1341,7 @@ static grub_err_t + grub_bsd_load_elf (grub_elf_t elf, const char *filename) + { + grub_err_t err; ++ grub_size_t sz; + + kern_end = 0; + kern_start = ~0; +@@ -1371,8 +1372,11 @@ grub_bsd_load_elf (grub_elf_t elf, const char *filename) + + if (grub_errno) + return grub_errno; +- err = grub_relocator_alloc_chunk_addr (relocator, &ch, +- kern_start, kern_end - kern_start); ++ ++ if (grub_sub (kern_end, kern_start, &sz)) ++ return grub_error (GRUB_ERR_OUT_OF_RANGE, "underflow detected while determining size of kernel for relocator"); ++ ++ err = grub_relocator_alloc_chunk_addr (relocator, &ch, kern_start, sz); + if (err) + return err; + +@@ -1432,8 +1436,10 @@ grub_bsd_load_elf (grub_elf_t elf, const char *filename) + { + grub_relocator_chunk_t ch; + +- err = grub_relocator_alloc_chunk_addr (relocator, &ch, kern_start, +- kern_end - kern_start); ++ if (grub_sub (kern_end, kern_start, &sz)) ++ return grub_error (GRUB_ERR_OUT_OF_RANGE, "underflow detected while determining size of kernel for relocator"); ++ ++ err = grub_relocator_alloc_chunk_addr (relocator, &ch, kern_start, sz); + if (err) + return err; + kern_chunk_src = get_virtual_current_address (ch); diff --git a/0663-types-Make-bool-generally-available.patch b/0663-types-Make-bool-generally-available.patch new file mode 100644 index 0000000..a2bcd5f --- /dev/null +++ b/0663-types-Make-bool-generally-available.patch @@ -0,0 +1,78 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Robbie Harwood +Date: Fri, 4 Nov 2022 12:13:34 -0400 +Subject: [PATCH] types: Make bool generally available + +Add an include on stdbool.h, making the bool type generally available +within the GRUB without needing to add a file-specific include every +time it would be used. + +Signed-off-by: Robbie Harwood +Reviewed-by: Daniel Kiper +--- + grub-core/commands/parttool.c | 2 +- + grub-core/parttool/msdospart.c | 4 ++-- + include/grub/parttool.h | 2 +- + include/grub/types.h | 1 + + 4 files changed, 5 insertions(+), 4 deletions(-) + +diff --git a/grub-core/commands/parttool.c b/grub-core/commands/parttool.c +index 051e31320..ff45c65e6 100644 +--- a/grub-core/commands/parttool.c ++++ b/grub-core/commands/parttool.c +@@ -315,7 +315,7 @@ grub_cmd_parttool (grub_command_t cmd __attribute__ ((unused)), + switch (curarg->type) + { + case GRUB_PARTTOOL_ARG_BOOL: +- pargs[curarg - ptool->args].bool ++ pargs[curarg - ptool->args].b + = (args[j][grub_strlen (curarg->name)] != '-'); + break; + +diff --git a/grub-core/parttool/msdospart.c b/grub-core/parttool/msdospart.c +index dcbf74e3b..a92669e5a 100644 +--- a/grub-core/parttool/msdospart.c ++++ b/grub-core/parttool/msdospart.c +@@ -61,7 +61,7 @@ static grub_err_t grub_pcpart_boot (const grub_device_t dev, + return grub_errno; + } + +- if (args[0].set && args[0].bool) ++ if (args[0].set && args[0].b) + { + for (i = 0; i < 4; i++) + mbr.entries[i].flag = 0x0; +@@ -116,7 +116,7 @@ static grub_err_t grub_pcpart_type (const grub_device_t dev, + + if (args[1].set) + { +- if (args[1].bool) ++ if (args[1].b) + type |= GRUB_PC_PARTITION_TYPE_HIDDEN_FLAG; + else + type &= ~GRUB_PC_PARTITION_TYPE_HIDDEN_FLAG; +diff --git a/include/grub/parttool.h b/include/grub/parttool.h +index 4e8f8d5e5..4799a22c5 100644 +--- a/include/grub/parttool.h ++++ b/include/grub/parttool.h +@@ -32,7 +32,7 @@ struct grub_parttool_args + int set; + union + { +- int bool; ++ int b; + char *str; + }; + }; +diff --git a/include/grub/types.h b/include/grub/types.h +index f6a972397..7a66af768 100644 +--- a/include/grub/types.h ++++ b/include/grub/types.h +@@ -20,6 +20,7 @@ + #define GRUB_TYPES_HEADER 1 + + #include ++#include + #ifndef GRUB_UTIL + #include + #endif diff --git a/0664-Remove-exttra-bool-definitions.patch b/0664-Remove-exttra-bool-definitions.patch new file mode 100644 index 0000000..9b42d9c --- /dev/null +++ b/0664-Remove-exttra-bool-definitions.patch @@ -0,0 +1,45 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Leo Sandoval +Date: Sat, 15 Feb 2025 20:11:42 -0600 +Subject: [PATCH] Remove exttra bool definitions + +Previous commit 'types: Make bool generally available' make this type available +so no need to redefined it. + +Signed-off-by: Leo Sandoval +--- + grub-core/lib/posix_wrap/sys/types.h | 5 ----- + grub-core/lib/xzembed/xz.h | 4 ---- + 2 files changed, 9 deletions(-) + +diff --git a/grub-core/lib/posix_wrap/sys/types.h b/grub-core/lib/posix_wrap/sys/types.h +index f63412c8d..2cd36d081 100644 +--- a/grub-core/lib/posix_wrap/sys/types.h ++++ b/grub-core/lib/posix_wrap/sys/types.h +@@ -24,11 +24,6 @@ + #include + + typedef grub_ssize_t ssize_t; +-#ifndef GRUB_POSIX_BOOL_DEFINED +-typedef enum { false = 0, true = 1 } bool; +-#define GRUB_POSIX_BOOL_DEFINED 1 +-#endif +- + typedef grub_uint8_t uint8_t; + typedef grub_uint16_t uint16_t; + typedef grub_uint32_t uint32_t; +diff --git a/grub-core/lib/xzembed/xz.h b/grub-core/lib/xzembed/xz.h +index fe7158bb2..0265c4a6d 100644 +--- a/grub-core/lib/xzembed/xz.h ++++ b/grub-core/lib/xzembed/xz.h +@@ -29,10 +29,6 @@ + #include + #include + +-#ifndef GRUB_POSIX_BOOL_DEFINED +-typedef enum { false = 0, true = 1 } bool; +-#endif +- + /** + * enum xz_ret - Return codes + * @XZ_OK: Everything is OK so far. More input or more output diff --git a/0665-fs-xfs-Fix-issues-found-while-fuzzing-the-XFS-filesy.patch b/0665-fs-xfs-Fix-issues-found-while-fuzzing-the-XFS-filesy.patch new file mode 100644 index 0000000..31b0df9 --- /dev/null +++ b/0665-fs-xfs-Fix-issues-found-while-fuzzing-the-XFS-filesy.patch @@ -0,0 +1,106 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Darren Kenny +Date: Fri, 2 Jun 2023 18:08:44 +0000 +Subject: [PATCH] fs/xfs: Fix issues found while fuzzing the XFS filesystem + +While performing fuzz testing with XFS filesystem images with ASAN +enabled, several issues were found where the memory accesses are made +beyond the data that is allocated into the struct grub_xfs_data +structure's data field. + +The existing structure didn't store the size of the memory allocated into +the buffer in the data field and had no way to check it. To resolve these +issues, the data size is stored to enable checks into the data buffer. + +With these checks in place, the fuzzing corpus no longer cause any crashes. + +Signed-off-by: Darren Kenny +Signed-off-by: Robbie Harwood +Signed-off-by: Marta Lewandowska +Signed-off-by: Lidong Chen +Reviewed-by: Daniel Kiper +--- + grub-core/fs/xfs.c | 26 ++++++++++++++++++++++++++ + 1 file changed, 26 insertions(+) + +diff --git a/grub-core/fs/xfs.c b/grub-core/fs/xfs.c +index b0d371ee1..7076192c1 100644 +--- a/grub-core/fs/xfs.c ++++ b/grub-core/fs/xfs.c +@@ -221,6 +221,7 @@ struct grub_fshelp_node + + struct grub_xfs_data + { ++ grub_size_t data_size; + struct grub_xfs_sblock sblock; + grub_disk_t disk; + int pos; +@@ -592,8 +593,20 @@ grub_xfs_read_block (grub_fshelp_node_t node, grub_disk_addr_t fileblock) + } + else if (node->inode.format == XFS_INODE_FORMAT_EXT) + { ++ grub_addr_t exts_end = 0; ++ grub_addr_t data_end = 0; ++ + nrec = grub_be_to_cpu32 (node->inode.nextents); + exts = (struct grub_xfs_extent *) grub_xfs_inode_data(&node->inode); ++ ++ if (grub_mul (sizeof (struct grub_xfs_extent), nrec, &exts_end) || ++ grub_add ((grub_addr_t) node->data, exts_end, &exts_end) || ++ grub_add ((grub_addr_t) node->data, node->data->data_size, &data_end) || ++ exts_end > data_end) ++ { ++ grub_error (GRUB_ERR_BAD_FS, "invalid number of XFS extents"); ++ return 0; ++ } + } + else + { +@@ -796,6 +809,9 @@ grub_xfs_iterate_dir (grub_fshelp_node_t dir, + grub_uint8_t *inopos = grub_xfs_inline_de_inopos(dir->data, de); + grub_uint8_t c; + ++ if ((inopos + (smallino ? 4 : 8)) > (grub_uint8_t *) dir + grub_xfs_fshelp_size (dir->data)) ++ return grub_error (GRUB_ERR_BAD_FS, "not a correct XFS inode"); ++ + /* inopos might be unaligned. */ + if (smallino) + ino = (((grub_uint32_t) inopos[0]) << 24) +@@ -822,6 +838,10 @@ grub_xfs_iterate_dir (grub_fshelp_node_t dir, + de->name[de->len] = c; + + de = grub_xfs_inline_next_de(dir->data, head, de); ++ ++ if ((grub_uint8_t *) de >= (grub_uint8_t *) dir + grub_xfs_fshelp_size (dir->data)) ++ return grub_error (GRUB_ERR_BAD_FS, "invalid XFS directory entry"); ++ + } + break; + } +@@ -887,6 +907,9 @@ grub_xfs_iterate_dir (grub_fshelp_node_t dir, + } + + filename = (char *)(direntry + 1); ++ if (filename + direntry->len - 1 > (char *) tail) ++ return grub_error (GRUB_ERR_BAD_FS, "invalid XFS directory entry"); ++ + /* The byte after the filename is for the filetype, padding, or + tag, which is not used by GRUB. So it can be overwritten. */ + filename[direntry->len] = '\0'; +@@ -931,6 +954,8 @@ grub_xfs_mount (grub_disk_t disk) + if (!data) + return 0; + ++ data->data_size = sizeof (struct grub_xfs_data); ++ + grub_dprintf("xfs", "Reading sb\n"); + /* Read the superblock. */ + if (grub_disk_read (disk, 0, 0, +@@ -949,6 +974,7 @@ grub_xfs_mount (grub_disk_t disk) + if (! data) + goto fail; + ++ data->data_size = sz; + data->diropen.data = data; + data->diropen.ino = grub_be_to_cpu64(data->sblock.rootino); + data->diropen.inode_read = 1; diff --git a/0666-ieee1275-ofnet-Fix-grub_malloc-removed-after-added-s.patch b/0666-ieee1275-ofnet-Fix-grub_malloc-removed-after-added-s.patch new file mode 100644 index 0000000..92da026 --- /dev/null +++ b/0666-ieee1275-ofnet-Fix-grub_malloc-removed-after-added-s.patch @@ -0,0 +1,32 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Nicolas Frayer +Date: Wed, 19 Mar 2025 17:39:41 +0100 +Subject: [PATCH] ieee1275/ofnet: Fix grub_malloc() removed after added safe + math functions + +grub_malloc() has been inadvertently removed from the ieee1275/ofnet +code after it has been modified to use safe match function. + +Fixes: 4beeff8a (net: Use safe math macros to prevent overflows) + +Tested-by: Marta Lewandowska +Reviewed-by: Daniel Kiper +Signed-off-by: Nicolas Frayer +--- + grub-core/net/drivers/ieee1275/ofnet.c | 3 +++ + 1 file changed, 3 insertions(+) + +diff --git a/grub-core/net/drivers/ieee1275/ofnet.c b/grub-core/net/drivers/ieee1275/ofnet.c +index 3e1b9094e2ab..37bc82467f60 100644 +--- a/grub-core/net/drivers/ieee1275/ofnet.c ++++ b/grub-core/net/drivers/ieee1275/ofnet.c +@@ -463,6 +463,9 @@ search_net_devices (struct grub_ieee1275_devalias *alias) + return 0; + } + } ++ ++ ofdata->path = grub_malloc(sz); ++ + if (!ofdata->path) + { + grub_print_error (); diff --git a/1001-bls-make-list.patch b/1001-bls-make-list.patch deleted file mode 100644 index ac39162..0000000 --- a/1001-bls-make-list.patch +++ /dev/null @@ -1,119 +0,0 @@ -From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 -From: Zhongling He -Date: Tue, 29 Mar 2022 17:27:13 +0800 -Subject: [PATCH] Fix a bug in bls_make_list, blscfg - -We have encountered a bug while running LifseaOS aarch64 version without -initrd. This commit fixes this bug. It may have fixed some potential -bugs as well. - -There are 4 partitions in a LifseaOS disk image: -- The 1st partition is for BIOS bootloader -- The 2nd partition is for UEFI booting, which contains grub binary - compiled from this source code, grubaa64.efi -- The 3rd partition contains grub.cfg files and - loader/entries/ostree-1-LifseaOS.conf -- The 4th partition contains rootfs - -Since x86_64 supports both BIOS and UEFI booting and we employ BIOS -booting for x86_64 (w/o initrd) image, we don't put the grub2 binary -compiled into the 2nd partition. As a result, this bug was not observed. -However, aarch64 only supports UEFI booting. In other words, we are -using this grub2 to boot LifseaOS (w/o initrd). - -grubaa64.efi from the 2nd partition will read `/grub2/grub.cfg` in the -3rd partition. - -``` -... - set ignition_firstboot="ignition.firstboot -${ignition_network_kcmdline}" -fi - -blscfg -``` - -`blscfg` is a command to convert `loader/entries/ostree-1-LifseaOS.conf` -into a typical grub.cfg. However, here comes the bug. While booting, an -error message will appear. - -``` -error: ../../grub-core/loader/arm64/linux.c:292:filename expected. -Press any key to continue. -``` - -This is because the bug in `blscfg` unexpectedly add a line, `initrd`, -into the generated grub.cfg file. Grub2 will expect an initrd file path -after `initrd`. As a result, the error occurs. - -In grub-core/command/blscfg.c:676, if `initrds` is a non-NULL pointer, -then `src` will contain a `initrd` line. - -``` -static void create_entry (struct bls_entry *entry){ - ... - initrds = bls_make_list (entry, "initrd", NULL); // - ... - if (initrds){ - ... - tmp = grub_stpcpy(initrd, "initrd "); - ... - } - ... - src = grub_xasprintf ("load_video\n" - "set gfx_payload=keep\n" - "insmod gzio\n" - "linux %s%s%s%s\n" - "%s", - GRUB_BOOT_DEVICE, clinux, options ? " " : "", options ? options -: "", - initrd ? initrd : ""); -``` - -In grub-core/command/blscfg.c:562, `bls_make_list` will always return a -non-NULL pointer except for a `grub_malloc` error. - -``` -static char **bls_make_list (struct bls_entry *entry, const char *key, -int *num) -{ - ... - list = grub_malloc (sizeof (char *)); - if (!list) - return NULL; - list[0] = NULL; - ... - return list; -} -``` - -Therefore, `initrd` like will be appended into the auto-generated -grub.cfg if `grub_malloc` succeed, regardless of whether initrd is -stated in config file. Our bug fix, same as upstream, modifies the -behaviour of `bls_make_list`. `bls_make_list` will now return a non-NULL -pointer only if the required field actually exists. - ---- - grub-core/commands/blscfg.c | 6 ++++++ - 1 file changed, 6 insertions(+) - -diff --git a/grub-core/commands/blscfg.c b/grub-core/commands/blscfg.c -index 795a9f9..bf18270 100644 ---- a/grub-core/commands/blscfg.c -+++ b/grub-core/commands/blscfg.c -@@ -589,6 +589,12 @@ static char **bls_make_list (struct bls_entry *entry, const char *key, int *num) - list[nlist] = NULL; - } - -+ if (!nlist) -+ { -+ grub_free (list); -+ return NULL; -+ } -+ - if (num) - *num = nlist; - --- -2.27.0 - diff --git a/1002-Add-LoongArch64-support-and-update-interface-to-v40.patch b/1002-Add-LoongArch64-support-and-update-interface-to-v40.patch deleted file mode 100644 index 36750c1..0000000 --- a/1002-Add-LoongArch64-support-and-update-interface-to-v40.patch +++ /dev/null @@ -1,3444 +0,0 @@ -From 478205a88ea71b0c546987602cd879027819125e Mon Sep 17 00:00:00 2001 -From: yangqiming -Date: Thu, 7 Jul 2022 11:25:02 +0800 -Subject: [PATCH] Add LoongArch64 support and update interface to V40 - - -diff --git a/conf/Makefile.common b/conf/Makefile.common -index 521cdda..d9f058b 100644 ---- a/conf/Makefile.common -+++ b/conf/Makefile.common -@@ -20,6 +20,10 @@ endif - if COND_powerpc_ieee1275 - CFLAGS_PLATFORM += -mcpu=powerpc - endif -+if COND_loongarch64 -+ CFLAGS_PLATFORM += -fno-strict-aliasing -march=loongarch64 -mabi=lp64 -fno-plt -Wa,-mla-global-with-abs -mcmodel=large -+ CPPFLAGS_PLATFORM = -fno-strict-aliasing -march=loongarch64 -mabi=lp64 -fno-plt -Wa,-mla-global-with-abs -mcmodel=large -+endif - - # Other options - -diff --git a/configure.ac b/configure.ac -index f59a7b8..4715d17 100644 ---- a/configure.ac -+++ b/configure.ac -@@ -116,6 +116,10 @@ case "$target_cpu" in - i[[3456]]86) target_cpu=i386 ;; - amd64) target_cpu=x86_64 ;; - sparc) target_cpu=sparc64 ;; -+ loongarch64) -+ target_cpu=loongarch64 -+ machine_CPPFLAGS="$machine_CPPFLAGS -DGRUB_CPU_LOONGARCH64=1" -+ ;; - mipsel|mips64el) - target_cpu=mipsel - machine_CPPFLAGS="$machine_CPPFLAGS -DGRUB_CPU_MIPSEL=1" -@@ -148,6 +152,7 @@ if test "x$with_platform" = x; then - powerpc64-*) platform=ieee1275 ;; - powerpc64le-*) platform=ieee1275 ;; - sparc64-*) platform=ieee1275 ;; -+ loongarch64-*) platform=efi;; - mipsel-*) platform=loongson ;; - mips-*) platform=arc ;; - ia64-*) platform=efi ;; -@@ -196,6 +201,7 @@ case "$target_cpu"-"$platform" in - mipsel-yeeloong) platform=loongson ;; - mipsel-fuloong) platform=loongson ;; - mipsel-loongson) ;; -+ loongarch64-efi) ;; - arm-uboot) ;; - arm-coreboot) ;; - arm-efi) ;; -@@ -251,6 +257,7 @@ case "$platform" in - pc) machine_CPPFLAGS="$machine_CPPFLAGS -DGRUB_MACHINE_PCBIOS=1" ;; - emu) machine_CPPFLAGS="$machine_CPPFLAGS -DGRUB_MACHINE_EMU=1" ;; - loongson) machine_CPPFLAGS="$machine_CPPFLAGS -DGRUB_MACHINE_MIPS_LOONGSON=1" ;; -+ loongson64) machine_CPPFLAGS="$machine_CPPFLAGS -DGRUB_MACHINE_LOONARCH64=1" ;; - qemu_mips) machine_CPPFLAGS="$machine_CPPFLAGS -DGRUB_MACHINE_MIPS_QEMU_MIPS=1" ;; - arc) machine_CPPFLAGS="$machine_CPPFLAGS -DGRUB_MACHINE_ARC=1" ;; - esac -@@ -2098,6 +2105,8 @@ AM_CONDITIONAL([COND_mips_arc], [test "(" x$target_cpu = xmips -o x$target_cpu = - AM_CONDITIONAL([COND_sparc64_ieee1275], [test x$target_cpu = xsparc64 -a x$platform = xieee1275]) - AM_CONDITIONAL([COND_sparc64_emu], [test x$target_cpu = xsparc64 -a x$platform = xemu]) - AM_CONDITIONAL([COND_powerpc_ieee1275], [test x$target_cpu = xpowerpc -a x$platform = xieee1275]) -+AM_CONDITIONAL([COND_loongarch64_efi], [test x$target_cpu = xloongarch64 -a x$platform = xefi]) -+AM_CONDITIONAL([COND_loongarch64], [test x$target_cpu = xloongarch64]) - AM_CONDITIONAL([COND_mips], [test x$target_cpu = xmips -o x$target_cpu = xmipsel]) - AM_CONDITIONAL([COND_mipsel], [test x$target_cpu = xmipsel]) - AM_CONDITIONAL([COND_mipseb], [test x$target_cpu = xmips]) -diff --git a/gentpl.py b/gentpl.py -index d662c30..3afd642 100644 ---- a/gentpl.py -+++ b/gentpl.py -@@ -32,7 +32,7 @@ GRUB_PLATFORMS = [ "emu", "i386_pc", "i386_efi", "i386_qemu", "i386_coreboot", - "mips_loongson", "sparc64_ieee1275", - "powerpc_ieee1275", "mips_arc", "ia64_efi", - "mips_qemu_mips", "arm_uboot", "arm_efi", "arm64_efi", -- "arm_coreboot"] -+ "arm_coreboot", "loongarch64_efi"] - - GROUPS = {} - -@@ -44,13 +44,14 @@ GROUPS["x86_64"] = [ "x86_64_efi" ] - GROUPS["x86"] = GROUPS["i386"] + GROUPS["x86_64"] - GROUPS["mips"] = [ "mips_loongson", "mips_qemu_mips", "mips_arc" ] - GROUPS["sparc64"] = [ "sparc64_ieee1275" ] -+GROUPS["loongarch64"] = [ "loongarch64_efi" ] - GROUPS["powerpc"] = [ "powerpc_ieee1275" ] - GROUPS["arm"] = [ "arm_uboot", "arm_efi", "arm_coreboot" ] - GROUPS["arm64"] = [ "arm64_efi" ] - - # Groups based on firmware - GROUPS["pc"] = [ "i386_pc" ] --GROUPS["efi"] = [ "i386_efi", "x86_64_efi", "ia64_efi", "arm_efi", "arm64_efi" ] -+GROUPS["efi"] = [ "i386_efi", "x86_64_efi", "ia64_efi", "arm_efi", "arm64_efi", "loongarch64_efi"] - GROUPS["ieee1275"] = [ "i386_ieee1275", "sparc64_ieee1275", "powerpc_ieee1275" ] - GROUPS["uboot"] = [ "arm_uboot" ] - GROUPS["xen"] = [ "i386_xen", "x86_64_xen" ] -diff --git a/grub-core/Makefile.am b/grub-core/Makefile.am -index 308ad88..a976fad 100644 ---- a/grub-core/Makefile.am -+++ b/grub-core/Makefile.am -@@ -222,6 +222,14 @@ KERNEL_HEADER_FILES += $(top_builddir)/include/grub/machine/memory.h - KERNEL_HEADER_FILES += $(top_builddir)/include/grub/machine/kernel.h - endif - -+if COND_loongarch64_efi -+KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/efi/efi.h -+KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/efi/disk.h -+KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/acpi.h -+KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/fdt.h -+KERNEL_HEADER_FILES += $(top_builddir)/include/grub/machine/loongson.h -+endif -+ - if COND_powerpc_ieee1275 - KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/ieee1275/ieee1275.h - KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/terminfo.h -diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def -index ef06f8c..54f6bf0 100644 ---- a/grub-core/Makefile.core.def -+++ b/grub-core/Makefile.core.def -@@ -95,6 +95,9 @@ kernel = { - arm_coreboot_ldflags = '-Wl,-r,-d'; - arm_coreboot_stripflags = '--strip-unneeded -K start -R .note -R .comment -R .note.gnu.gold-version'; - -+ loongarch64_efi_ldflags = '-Wl,-r,-d'; -+ loongarch64_efi_stripflags = '--strip-unneeded -K start -R .note -R .comment -R .note.gnu.gold-version -R .eh_frame'; -+ - i386_pc_startup = kern/i386/pc/startup.S; - i386_efi_startup = kern/i386/efi/startup.S; - x86_64_efi_startup = kern/x86_64/efi/startup.S; -@@ -105,6 +108,7 @@ kernel = { - i386_coreboot_startup = kern/i386/coreboot/startup.S; - i386_multiboot_startup = kern/i386/coreboot/startup.S; - mips_startup = kern/mips/startup.S; -+ loongarch64_efi_startup = kern/loongarch64/efi/startup.S; - sparc64_ieee1275_startup = kern/sparc64/ieee1275/crt0.S; - powerpc_ieee1275_startup = kern/powerpc/ieee1275/startup.S; - arm_uboot_startup = kern/arm/startup.S; -@@ -295,6 +299,15 @@ kernel = { - extra_dist = video/sis315_init.c; - mips_loongson = commands/keylayouts.c; - -+ loongarch64 = kern/loongarch64/init.c; -+ loongarch64 = kern/loongarch64/dl.c; -+ loongarch64 = kern/loongarch64/cache.S; -+ loongarch64 = kern/generic/rtc_get_time_ms.c; -+ loongarch64 = kern/efi/fdt.c; -+ loongarch64 = lib/fdt.c; -+ loongarch64_efi = kern/loongarch64/efi/init.c; -+ loongarch64_efi = lib/loongarch64/efi/loongson.c; -+ - powerpc_ieee1275 = kern/powerpc/cache.S; - powerpc_ieee1275 = kern/powerpc/dl.c; - powerpc_ieee1275 = kern/powerpc/compiler-rt.S; -@@ -810,6 +823,7 @@ module = { - enable = sparc64_ieee1275; - enable = powerpc_ieee1275; - enable = mips_arc; -+ enable = loongarch64_efi; - enable = ia64_efi; - enable = arm_efi; - enable = arm64_efi; -@@ -896,6 +910,7 @@ module = { - i386_qemu = lib/i386/halt.c; - xen = lib/xen/halt.c; - efi = lib/efi/halt.c; -+ loongarch64_efi = commands/acpihalt.c; - ieee1275 = lib/ieee1275/halt.c; - emu = lib/emu/halt.c; - uboot = lib/dummy/halt.c; -@@ -911,6 +926,7 @@ module = { - mips_arc = lib/mips/arc/reboot.c; - mips_loongson = lib/mips/loongson/reboot.c; - mips_qemu_mips = lib/mips/qemu_mips/reboot.c; -+ loongarch64_efi = lib/loongson/reboot.c; - xen = lib/xen/reboot.c; - uboot = lib/uboot/reboot.c; - arm_coreboot = lib/dummy/reboot.c; -@@ -1608,6 +1624,8 @@ module = { - efi = lib/efi/relocator.c; - mips = lib/mips/relocator_asm.S; - mips = lib/mips/relocator.c; -+ loongarch64 = lib/loongarch64/relocator_asm.S; -+ loongarch64 = lib/loongarch64/relocator.c; - powerpc = lib/powerpc/relocator_asm.S; - powerpc = lib/powerpc/relocator.c; - xen = lib/xen/relocator.c; -@@ -1620,6 +1638,7 @@ module = { - extra_dist = kern/powerpc/cache_flush.S; - - enable = mips; -+ enable = loongarch64; - enable = powerpc; - enable = x86; - enable = xen; -@@ -1737,6 +1756,7 @@ module = { - xen = loader/i386/xen.c; - i386_pc = lib/i386/pc/vesa_modes_table.c; - mips = loader/mips/linux.c; -+ loongarch64 = loader/loongarch64/linux.c; - powerpc_ieee1275 = loader/powerpc/ieee1275/linux.c; - sparc64_ieee1275 = loader/sparc64/ieee1275/linux.c; - ia64_efi = loader/ia64/efi/linux.c; -@@ -1838,6 +1858,7 @@ module = { - enable = arm_efi; - enable = arm64_efi; - enable = mips; -+ enable = loongarch64_efi; - }; - - module = { -diff --git a/grub-core/kern/efi/mm.c b/grub-core/kern/efi/mm.c -index 8a89614..0b5f158 100644 ---- a/grub-core/kern/efi/mm.c -+++ b/grub-core/kern/efi/mm.c -@@ -122,7 +122,11 @@ grub_efi_allocate_pages_max (grub_efi_physical_address_t max, - grub_efi_boot_services_t *b; - grub_efi_physical_address_t address = max; - -+#ifdef GRUB_CPU_LOONGARCH64 -+ if (max > grub_efi_max_usable_address()) -+#else - if (max > GRUB_EFI_MAX_USABLE_ADDRESS) -+#endif - return 0; - - b = grub_efi_system_table->boot_services; -@@ -157,11 +161,19 @@ grub_efi_allocate_pages_real (grub_efi_physical_address_t address, - grub_efi_physical_address_t ret = address; - - /* Limit the memory access to less than 4GB for 32-bit platforms. */ -+#ifdef GRUB_CPU_LOONGARCH64 -+ if (address > grub_efi_max_usable_address()) -+ { -+ grub_error (GRUB_ERR_BAD_ARGUMENT, -+ N_("invalid memory address 0x%llx"), -+ address); -+#else - if (address > GRUB_EFI_MAX_USABLE_ADDRESS) - { - grub_error (GRUB_ERR_BAD_ARGUMENT, - N_("invalid memory address (0x%llx > 0x%llx)"), - address, GRUB_EFI_MAX_USABLE_ADDRESS); -+#endif - return NULL; - } - -@@ -177,7 +189,11 @@ grub_efi_allocate_pages_real (grub_efi_physical_address_t address, - { - /* Uggh, the address 0 was allocated... This is too annoying, - so reallocate another one. */ -+#ifdef GRUB_CPU_LOONGARCH64 -+ ret = grub_efi_max_usable_address(); -+#else - ret = address; -+#endif - status = efi_call_4 (b->allocate_pages, alloctype, memtype, pages, &ret); - grub_efi_free_pages (0, pages); - if (status != GRUB_EFI_SUCCESS) -@@ -195,9 +211,15 @@ grub_efi_allocate_pages_real (grub_efi_physical_address_t address, - void * - grub_efi_allocate_any_pages (grub_efi_uintn_t pages) - { -+#ifdef GRUB_CPU_LOONGARCH64 -+ return grub_efi_allocate_pages_real (grub_efi_max_usable_address(), -+ pages, GRUB_EFI_ALLOCATE_MAX_ADDRESS, -+ GRUB_EFI_LOADER_DATA); -+#else - return grub_efi_allocate_pages_real (GRUB_EFI_MAX_USABLE_ADDRESS, - pages, GRUB_EFI_ALLOCATE_MAX_ADDRESS, - GRUB_EFI_LOADER_DATA); -+#endif - } - - void * -@@ -471,7 +493,9 @@ filter_memory_map (grub_efi_memory_descriptor_t *memory_map, - desc = NEXT_MEMORY_DESCRIPTOR (desc, desc_size)) - { - if (desc->type == GRUB_EFI_CONVENTIONAL_MEMORY --#if 1 -+#ifdef GRUB_CPU_LOONGARCH64 -+ && desc->physical_start <= grub_efi_max_usable_address() -+#else - && desc->physical_start <= GRUB_EFI_MAX_ALLOCATION_ADDRESS - #endif - && desc->physical_start + PAGES_TO_BYTES (desc->num_pages) > 0x100000 -@@ -487,7 +511,14 @@ filter_memory_map (grub_efi_memory_descriptor_t *memory_map, - desc->physical_start = 0x100000; - } - --#if 1 -+#ifdef GRUB_CPU_LOONGARCH64 -+ if (BYTES_TO_PAGES (filtered_desc->physical_start) -+ + filtered_desc->num_pages -+ > BYTES_TO_PAGES_DOWN (grub_efi_max_usable_address())) -+ filtered_desc->num_pages -+ = (BYTES_TO_PAGES_DOWN (grub_efi_max_usable_address()) -+ - BYTES_TO_PAGES (filtered_desc->physical_start)); -+#else - if (BYTES_TO_PAGES (filtered_desc->physical_start) - + filtered_desc->num_pages - > BYTES_TO_PAGES_DOWN (GRUB_EFI_MAX_ALLOCATION_ADDRESS)) -@@ -765,7 +796,7 @@ grub_efi_mm_init (void) - 2 * BYTES_TO_PAGES (MEMORY_MAP_SIZE)); - } - --#if defined (__aarch64__) || defined (__arm__) -+#if defined (__aarch64__) || defined (__arm__) || defined (__loongarch__) - grub_err_t - grub_efi_get_ram_base(grub_addr_t *base_addr) - { -@@ -784,9 +815,15 @@ grub_efi_get_ram_base(grub_addr_t *base_addr) - if (ret < 1) - return GRUB_ERR_BUG; - -+#ifdef GRUB_CPU_LOONGARCH64 -+ for (desc = memory_map, *base_addr = grub_efi_max_usable_address(); -+ (grub_addr_t) desc < ((grub_addr_t) memory_map + memory_map_size); -+ desc = NEXT_MEMORY_DESCRIPTOR (desc, desc_size)) -+#else - for (desc = memory_map, *base_addr = GRUB_EFI_MAX_USABLE_ADDRESS; - (grub_addr_t) desc < ((grub_addr_t) memory_map + memory_map_size); - desc = NEXT_MEMORY_DESCRIPTOR (desc, desc_size)) -+#endif - { - if (desc->type == GRUB_EFI_CONVENTIONAL_MEMORY && - (desc->attribute & GRUB_EFI_MEMORY_WB)) -@@ -800,7 +837,11 @@ grub_efi_get_ram_base(grub_addr_t *base_addr) - } - } - -+#ifdef GRUB_CPU_LOONGARCH64 -+ if (*base_addr == grub_efi_max_usable_address()) -+#else - if (*base_addr == GRUB_EFI_MAX_USABLE_ADDRESS) -+#endif - grub_dprintf ("efi", "base_addr 0x%016lx is probably wrong.\n", *base_addr); - - grub_free(memory_map); -diff --git a/grub-core/kern/loongarch64/cache.S b/grub-core/kern/loongarch64/cache.S -new file mode 100644 -index 0000000..d291c67 ---- /dev/null -+++ b/grub-core/kern/loongarch64/cache.S -@@ -0,0 +1,26 @@ -+/* -+ * GRUB -- GRand Unified Bootloader -+ * Copyright (C) 2009,2017 Free Software Foundation, Inc. -+ * -+ * GRUB is free software: you can redistribute it and/or modify -+ * it under the terms of the GNU General Public License as published by -+ * the Free Software Foundation, either version 3 of the License, or -+ * (at your option) any later version. -+ * -+ * GRUB is distributed in the hope that it will be useful, -+ * but WITHOUT ANY WARRANTY; without even the implied warranty of -+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -+ * GNU General Public License for more details. -+ * -+ * You should have received a copy of the GNU General Public License -+ * along with GRUB. If not, see . -+ */ -+ -+#include -+ -+FUNCTION (grub_arch_sync_caches) -+ jr $ra -+ -+FUNCTION (grub_arch_sync_dma_caches) -+ jr $ra -+ -diff --git a/grub-core/kern/loongarch64/dl.c b/grub-core/kern/loongarch64/dl.c -new file mode 100644 -index 0000000..7d9e2a1 ---- /dev/null -+++ b/grub-core/kern/loongarch64/dl.c -@@ -0,0 +1,266 @@ -+/* loongarch64/dl.c - arch-dependent part of loadable module support */ -+/* -+ * GRUB -- GRand Unified Bootloader -+ * Copyright (C) 2002,2005,2007,2009,2017 Free Software Foundation, Inc. -+ * -+ * GRUB is free software: you can redistribute it and/or modify -+ * it under the terms of the GNU General Public License as published by -+ * the Free Software Foundation, either version 3 of the License, or -+ * (at your option) any later version. -+ * -+ * GRUB is distributed in the hope that it will be useful, -+ * but WITHOUT ANY WARRANTY; without even the implied warranty of -+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -+ * GNU General Public License for more details. -+ * -+ * You should have received a copy of the GNU General Public License -+ * along with GRUB. If not, see . -+ */ -+ -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+ -+/* Check if EHDR is a valid ELF header. */ -+grub_err_t -+grub_arch_dl_check_header (void *ehdr) -+{ -+ Elf_Ehdr *e = ehdr; -+ -+ /* Check the magic numbers. */ -+ if (e->e_ident[EI_CLASS] != ELFCLASS64 -+ || e->e_ident[EI_DATA] != ELFDATA2LSB -+ || e->e_machine != EM_LOONGARCH64) -+ return grub_error (GRUB_ERR_BAD_OS, N_("invalid arch-dependent ELF magic")); -+ -+ return GRUB_ERR_NONE; -+} -+ -+#pragma GCC diagnostic ignored "-Wcast-align" -+ -+grub_err_t -+grub_arch_dl_get_tramp_got_size (const void *ehdr __attribute__ ((unused)), -+ grub_size_t *tramp, grub_size_t *got) -+{ -+ *tramp = 0; -+ *got = 0; -+ return GRUB_ERR_NONE; -+} -+ -+/* Relocate symbols. */ -+grub_err_t -+grub_arch_dl_relocate_symbols (grub_dl_t mod, void *ehdr, -+ Elf_Shdr *s, grub_dl_segment_t seg) -+{ -+ Elf_Ehdr *e = ehdr; -+ Elf_Rel *rel, *max; -+ grub_uint64_t oprs[10240]={0}; -+ int opri=-1; -+ grub_uint32_t la_abs = 0; -+ -+ for (rel = (Elf_Rel *) ((char *) e + s->sh_offset), -+ max = (Elf_Rel *) ((char *) rel + s->sh_size); -+ rel < max; -+ rel = (Elf_Rel *) ((char *) rel + s->sh_entsize)) -+ { -+ grub_uint8_t *addr; -+ Elf_Sym *sym; -+ Elf_Addr r_info; -+ grub_uint64_t sym_value; -+ -+ if (seg->size < rel->r_offset) -+ return grub_error (GRUB_ERR_BAD_MODULE, -+ "reloc offset is out of the segment"); -+ -+ r_info = (grub_uint64_t) (rel->r_info); -+ -+ addr = (grub_uint8_t *) ((char*)seg->addr + rel->r_offset); -+ sym = (Elf_Sym *) ((char*)mod->symtab -+ + mod->symsize * ELF_R_SYM (r_info)); -+ sym_value = sym->st_value; -+ if (s->sh_type == SHT_RELA) -+ { -+ sym_value += ((Elf_Rela *) rel)->r_addend; -+ } -+ switch (ELF_R_TYPE (r_info)) -+ { -+ case R_LARCH_64: -+ { -+ *(grub_uint64_t *)addr=(grub_uint64_t)sym_value; -+ } -+ break; -+ case R_LARCH_MARK_LA: -+ { -+ la_abs=1; -+ } -+ break; -+ case R_LARCH_SOP_PUSH_PCREL: -+ { -+ opri++; -+ oprs[opri]=(grub_uint64_t)(sym_value-(grub_uint64_t)addr); -+ } -+ break; -+ case R_LARCH_SOP_PUSH_ABSOLUTE: -+ { -+ opri++; -+ oprs[opri]=(grub_uint64_t)sym_value; -+ } -+ break; -+ case R_LARCH_SOP_PUSH_PLT_PCREL: -+ { -+ opri++; -+ oprs[opri]=(grub_uint64_t)(sym_value-(grub_uint64_t)addr); -+ } -+ break; -+ case R_LARCH_SOP_SUB: -+ { -+ grub_uint64_t opr2=oprs[opri]; -+ opri--; -+ grub_uint64_t opr1=oprs[opri]; -+ opri--; -+ opri++; -+ oprs[opri]=opr1 - opr2; -+ } -+ break; -+ case R_LARCH_SOP_SL: -+ { -+ grub_uint64_t opr2=oprs[opri]; -+ opri--; -+ grub_uint64_t opr1=oprs[opri]; -+ opri--; -+ opri++; -+ oprs[opri]=opr1 << opr2; -+ } -+ break; -+ case R_LARCH_SOP_SR: -+ { -+ grub_uint64_t opr2=oprs[opri]; -+ opri--; -+ grub_uint64_t opr1=oprs[opri]; -+ opri--; -+ opri++; -+ oprs[opri]=opr1 >> opr2; -+ } -+ break; -+ case R_LARCH_SOP_ADD: -+ { -+ grub_uint64_t opr2=oprs[opri]; -+ opri--; -+ grub_uint64_t opr1=oprs[opri]; -+ opri--; -+ opri++; -+ oprs[opri]=opr1 + opr2; -+ } -+ break; -+ case R_LARCH_SOP_AND: -+ { -+ grub_uint64_t opr2=oprs[opri]; -+ opri--; -+ grub_uint64_t opr1=oprs[opri]; -+ opri--; -+ opri++; -+ oprs[opri]=opr1 & opr2; -+ } -+ break; -+ case R_LARCH_SOP_IF_ELSE: -+ { -+ grub_uint64_t opr3=oprs[opri]; -+ opri--; -+ grub_uint64_t opr2=oprs[opri]; -+ opri--; -+ grub_uint64_t opr1=oprs[opri]; -+ opri--; -+ if(opr1){ -+ opri++; -+ oprs[opri]=opr2; -+ } else { -+ opri++; -+ oprs[opri]=opr3; -+ } -+ } -+ break; -+ case R_LARCH_SOP_POP_32_S_10_5: -+ { -+ grub_uint64_t opr1 = oprs[opri]; -+ opri--; -+ *(grub_uint64_t *)addr=(*(grub_uint64_t *)addr) | ((opr1 & 0x1f) << 10); -+ } -+ break; -+ case R_LARCH_SOP_POP_32_U_10_12: -+ { -+ grub_uint64_t opr1 = oprs[opri]; -+ opri--; -+ *(grub_uint64_t *)addr=(*(grub_uint64_t *)addr) | ((opr1 & 0xfff) << 10); -+ } -+ break; -+ case R_LARCH_SOP_POP_32_S_10_12: -+ { -+ if(la_abs==1) -+ la_abs=0; -+ grub_uint64_t opr1 = oprs[opri]; -+ opri--; -+ *(grub_uint64_t *)addr= (*(grub_uint64_t *)addr) | ((opr1 & 0xfff) << 10); -+ } -+ break; -+ case R_LARCH_SOP_POP_32_S_10_16: -+ { -+ grub_uint64_t opr1 = oprs[opri]; -+ opri--; -+ *(grub_uint64_t *)addr= (*(grub_uint64_t *)addr) | ((opr1 & 0xffff) << 10); -+ } -+ break; -+ case R_LARCH_SOP_POP_32_S_10_16_S2: -+ { -+ grub_uint64_t opr1 = oprs[opri]; -+ opri--; -+ *(grub_uint64_t *)addr= (*(grub_uint64_t *)addr) | (((opr1 >> 2) & 0xffff) << 10); -+ } -+ break; -+ case R_LARCH_SOP_POP_32_S_5_20: -+ { -+ grub_uint64_t opr1 = oprs[opri]; -+ opri--; -+ *(grub_uint64_t *)addr= (*(grub_uint64_t *)addr) | ((opr1 & 0xfffff)<<5) ; -+ } -+ break; -+ case R_LARCH_SOP_POP_32_S_0_5_10_16_S2: -+ { -+ grub_uint64_t opr1 = oprs[opri]; -+ opri--; -+ *(grub_uint64_t *)addr=(*(grub_uint64_t *)addr) | (((opr1 >> 2) & 0xffff) << 10); -+ *(grub_uint64_t *)addr=(*(grub_uint64_t *)addr) | ((opr1 >> 18) & 0x1f); -+ } -+ break; -+ case R_LARCH_SOP_POP_32_S_0_10_10_16_S2: -+ { -+ grub_uint64_t opr1 = oprs[opri]; -+ opri--; -+ *(grub_uint64_t *)addr=(*(grub_uint64_t *)addr) | (((opr1 >> 2) & 0xffff) << 10); -+ *(grub_uint64_t *)addr=(*(grub_uint64_t *)addr) | ((opr1 >> 18) & 0x3ff); -+ } -+ break; -+ default: -+ { -+ return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET, -+ N_("relocation 0x%x is not implemented yet"), -+ ELF_R_TYPE (r_info)); -+ } -+ break; -+ } -+ } -+ -+ return GRUB_ERR_NONE; -+} -+ -+/* -+ * Tell the loader what our minimum section alignment is. -+ */ -+grub_size_t -+grub_arch_dl_min_alignment (void) -+{ -+ return 1; -+} -diff --git a/grub-core/kern/loongarch64/efi/init.c b/grub-core/kern/loongarch64/efi/init.c -new file mode 100644 -index 0000000..632c39a ---- /dev/null -+++ b/grub-core/kern/loongarch64/efi/init.c -@@ -0,0 +1,74 @@ -+/* init.c - initialize an arm-based EFI system */ -+/* -+ * GRUB -- GRand Unified Bootloader -+ * Copyright (C) 2013 Free Software Foundation, Inc. -+ * -+ * GRUB is free software: you can redistribute it and/or modify -+ * it under the terms of the GNU General Public License as published by -+ * the Free Software Foundation, either version 3 of the License, or -+ * (at your option) any later version. -+ * -+ * GRUB is distributed in the hope that it will be useful, -+ * but WITHOUT ANY WARRANTY; without even the implied warranty of -+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -+ * GNU General Public License for more details. -+ * -+ * You should have received a copy of the GNU General Public License -+ * along with GRUB. If not, see . -+ */ -+ -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+ -+static grub_uint64_t tmr; -+static grub_efi_event_t tmr_evt; -+ -+static grub_uint64_t -+grub_efi_get_time_ms (void) -+{ -+ return tmr; -+} -+ -+static void -+grub_loongson_increment_timer (grub_efi_event_t event __attribute__ ((unused)), -+ void *context __attribute__ ((unused))) -+{ -+ tmr += 10; -+} -+ -+void -+grub_machine_init (void) -+{ -+ grub_efi_boot_services_t *b; -+ -+ grub_efi_init (); -+ -+ b = grub_efi_system_table->boot_services; -+ efi_call_5 (b->create_event, GRUB_EFI_EVT_TIMER | GRUB_EFI_EVT_NOTIFY_SIGNAL, -+ GRUB_EFI_TPL_CALLBACK, grub_loongson_increment_timer, NULL, &tmr_evt); -+ efi_call_3 (b->set_timer, tmr_evt, GRUB_EFI_TIMER_PERIODIC, 100000); -+ -+ grub_install_get_time_ms (grub_efi_get_time_ms); -+} -+ -+void -+grub_machine_fini (int flags) -+{ -+ grub_efi_boot_services_t *b; -+ -+ if (!(flags & GRUB_LOADER_FLAG_NORETURN)) -+ return; -+ -+ b = grub_efi_system_table->boot_services; -+ -+ efi_call_3 (b->set_timer, tmr_evt, GRUB_EFI_TIMER_CANCEL, 0); -+ efi_call_1 (b->close_event, tmr_evt); -+ -+ grub_efi_fini (); -+} -diff --git a/grub-core/kern/loongarch64/efi/startup.S b/grub-core/kern/loongarch64/efi/startup.S -new file mode 100644 -index 0000000..1ffff08 ---- /dev/null -+++ b/grub-core/kern/loongarch64/efi/startup.S -@@ -0,0 +1,45 @@ -+/* -+ * GRUB -- GRand Unified Bootloader -+ * Copyright (C) 2013 Free Software Foundation, Inc. -+ * -+ * GRUB is free software: you can redistribute it and/or modify -+ * it under the terms of the GNU General Public License as published by -+ * the Free Software Foundation, either version 3 of the License, or -+ * (at your option) any later version. -+ * -+ * GRUB is distributed in the hope that it will be useful, -+ * but WITHOUT ANY WARRANTY; without even the implied warranty of -+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -+ * GNU General Public License for more details. -+ * -+ * You should have received a copy of the GNU General Public License -+ * along with GRUB. If not, see . -+ */ -+ -+#include -+ -+ .file "startup.S" -+ .text -+ .globl start, _start -+ .align 4 -+ -+FUNCTION(start) -+FUNCTION(_start) -+ /* -+ * EFI_SYSTEM_TABLE and EFI_HANDLE are passed in a1/a0. -+ */ -+ addi.d $sp, $sp, -16 -+ st.d $ra, $sp, 0 -+ -+ la $a2, grub_efi_image_handle -+ st.d $a0, $a2, 0 -+ la $a2, grub_efi_system_table -+ st.d $a1, $a2, 0 -+ -+ bl grub_main -+ -+1: -+ ld.d $ra, $sp, 0 -+ addi.d $sp, $sp, 16 -+ jr $ra -+ -diff --git a/grub-core/kern/loongarch64/init.c b/grub-core/kern/loongarch64/init.c -new file mode 100644 -index 0000000..b2de930 ---- /dev/null -+++ b/grub-core/kern/loongarch64/init.c -@@ -0,0 +1,47 @@ -+/* -+ * GRUB -- GRand Unified Bootloader -+ * Copyright (C) 2009,2017 Free Software Foundation, Inc. -+ * -+ * GRUB is free software: you can redistribute it and/or modify -+ * it under the terms of the GNU General Public License as published by -+ * the Free Software Foundation, either version 3 of the License, or -+ * (at your option) any later version. -+ * -+ * GRUB is distributed in the hope that it will be useful, -+ * but WITHOUT ANY WARRANTY; without even the implied warranty of -+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -+ * GNU General Public License for more details. -+ * -+ * You should have received a copy of the GNU General Public License -+ * along with GRUB. If not, see . -+ */ -+ -+#include -+#include -+#include -+#include -+ -+grub_uint32_t grub_arch_cpuclock; -+ -+/* FIXME: use interrupt to count high. */ -+grub_uint64_t -+grub_get_rtc (void) -+{ -+ static grub_uint32_t high = 0; -+ static grub_uint32_t last = 0; -+ grub_uint32_t low; -+ -+ asm volatile ("csrrd %0, " GRUB_CPU_LOONGARCH_COP0_TIMER_COUNT : "=r" (low)); -+ if (low < last) -+ high++; -+ last = low; -+ -+ return (((grub_uint64_t) high) << 32) | low; -+} -+ -+void -+grub_timer_init (grub_uint32_t cpuclock) -+{ -+ grub_arch_cpuclock = cpuclock; -+ grub_install_get_time_ms (grub_rtc_get_time_ms); -+} -diff --git a/grub-core/lib/loongarch64/efi/loongson.c b/grub-core/lib/loongarch64/efi/loongson.c -new file mode 100644 -index 0000000..eda1e1c ---- /dev/null -+++ b/grub-core/lib/loongarch64/efi/loongson.c -@@ -0,0 +1,119 @@ -+/* -+ * GRUB -- GRand Unified Bootloader -+ * Copyright (C) 2017 Free Software Foundation, Inc. -+ * -+ * GRUB is free software: you can redistribute it and/or modify -+ * it under the terms of the GNU General Public License as published by -+ * the Free Software Foundation, either version 3 of the License, or -+ * (at your option) any later version. -+ * -+ * GRUB is distributed in the hope that it will be useful, -+ * but WITHOUT ANY WARRANTY; without even the implied warranty of -+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -+ * GNU General Public License for more details. -+ * -+ * You should have received a copy of the GNU General Public License -+ * along with GRUB. If not, see . -+ */ -+ -+#include -+#include -+#include -+#include -+#include -+#include -+ -+unsigned long -+grub_efi_get_bpi_version (const char *str) -+{ -+ unsigned long version = GRUB_EFI_BPI_VER_NONE; -+ -+ version = grub_strtoul (str + 4, 0, 0); -+ -+ return version; -+} -+ -+void * -+grub_efi_loongson_get_boot_params (void) -+{ -+ static void * boot_params = NULL; -+ grub_efi_configuration_table_t *tables; -+ grub_efi_guid_t loongson_bpi_guid = GRUB_EFI_LOONGSON_BPI_TABLE_GUID; -+ unsigned int i; -+ -+ if (boot_params) -+ return boot_params; -+ -+ /* Look for Loongson boot params interface in UEFI config tables. */ -+ tables = grub_efi_system_table->configuration_table; -+ -+ for (i = 0; i < grub_efi_system_table->num_table_entries; i++) -+ if (grub_memcmp (&tables[i].vendor_guid, &loongson_bpi_guid, sizeof (loongson_bpi_guid)) == 0) -+ { -+ boot_params= tables[i].vendor_table; -+ grub_dprintf ("loongson", "found registered BPI @ %p\n", boot_params); -+ break; -+ } -+ return boot_params; -+} -+ -+grub_uint8_t -+grub_efi_loongson_calculatesum8 (const grub_uint8_t *buffer, grub_efi_uintn_t length) -+{ -+ grub_uint8_t sum; -+ grub_efi_uintn_t count; -+ -+ for (sum = 0, count = 0; count < length; count++) -+ { -+ sum = (grub_uint8_t) (sum + *(buffer + count)); -+ } -+ return sum; -+} -+ -+grub_uint8_t -+grub_efi_loongson_grub_calculatechecksum8 (const grub_uint8_t *buffer, grub_efi_uintn_t length) -+{ -+ grub_uint8_t checksum; -+ -+ checksum = grub_efi_loongson_calculatesum8(buffer, length); -+ -+ return (grub_uint8_t) (0x100 - checksum); -+} -+ -+ -+grub_uint32_t -+grub_efi_loongson_memmap_v1_sort(struct memmap_v1 array[], grub_uint32_t length, -+ mem_map_v1 * bpmem, grub_uint32_t index, grub_uint32_t memtype) -+{ -+ grub_uint64_t tempmemsize = 0; -+ grub_uint32_t j = 0; -+ grub_uint32_t t = 0; -+ -+ for(j = 0; j < length;) -+ { -+ tempmemsize = array[j].memsize; -+ for(t = j + 1; t < length; t++) -+ { -+ if(array[j].memstart + tempmemsize == array[t].memstart) -+ { -+ tempmemsize += array[t].memsize; -+ } -+ else -+ { -+ break; -+ } -+ } -+ bpmem->map[index].memtype = memtype; -+ bpmem->map[index].memstart = array[j].memstart; -+ bpmem->map[index].memsize = tempmemsize; -+ grub_dprintf("loongson", "v1 map[%d]:type %x, start 0x%lx, end 0x%lx\n", -+ index, -+ bpmem->map[index].memtype, -+ bpmem->map[index].memstart, -+ bpmem->map[index].memstart+ bpmem->map[index].memsize -+ ); -+ j = t; -+ index++; -+ } -+ return index; -+} -diff --git a/grub-core/lib/loongarch64/relocator.c b/grub-core/lib/loongarch64/relocator.c -new file mode 100644 -index 0000000..4b253ca ---- /dev/null -+++ b/grub-core/lib/loongarch64/relocator.c -@@ -0,0 +1,163 @@ -+/* -+ * GRUB -- GRand Unified Bootloader -+ * Copyright (C) 2017 Free Software Foundation, Inc. -+ * -+ * GRUB is free software: you can redistribute it and/or modify -+ * it under the terms of the GNU General Public License as published by -+ * the Free Software Foundation, either version 3 of the License, or -+ * (at your option) any later version. -+ * -+ * GRUB is distributed in the hope that it will be useful, -+ * but WITHOUT ANY WARRANTY; without even the implied warranty of -+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -+ * GNU General Public License for more details. -+ * -+ * You should have received a copy of the GNU General Public License -+ * along with GRUB. If not, see . -+ */ -+ -+#include -+#include -+ -+#include -+#include -+#include -+#include -+ -+#include -+#include -+ -+extern grub_uint8_t grub_relocator_forward_start; -+extern grub_uint8_t grub_relocator_forward_end; -+extern grub_uint8_t grub_relocator_backward_start; -+extern grub_uint8_t grub_relocator_backward_end; -+ -+#define REGW_SIZEOF (4 * sizeof (grub_uint32_t)) -+#define JUMP_SIZEOF (2 * sizeof (grub_uint32_t)) -+ -+#define RELOCATOR_SRC_SIZEOF(x) (&grub_relocator_##x##_end \ -+ - &grub_relocator_##x##_start) -+#define RELOCATOR_SIZEOF(x) (RELOCATOR_SRC_SIZEOF(x) \ -+ + REGW_SIZEOF * 3) -+grub_size_t grub_relocator_align = sizeof (grub_uint64_t); -+grub_size_t grub_relocator_forward_size; -+grub_size_t grub_relocator_backward_size; -+grub_size_t grub_relocator_jumper_size = JUMP_SIZEOF + REGW_SIZEOF; -+ -+void -+grub_cpu_relocator_init (void) -+{ -+ grub_relocator_forward_size = RELOCATOR_SIZEOF(forward); -+ grub_relocator_backward_size = RELOCATOR_SIZEOF(backward); -+} -+ -+static void -+write_reg (int regn, grub_uint64_t val, void **target) -+{ -+ grub_uint32_t lu12iw=0x14000000; -+ grub_uint32_t ori=0x03800000; -+ grub_uint32_t lu32id=0x16000000; -+ grub_uint32_t lu52id=0x03000000; -+ -+ *(grub_uint32_t *) *target = (lu12iw | (grub_uint32_t)((val & 0xfffff000)>>12<<5) | (grub_uint32_t)regn);; -+ *target = ((grub_uint32_t *) *target) + 1; -+ *(grub_uint32_t *) *target = (ori | (grub_uint32_t)((val & 0xfff)<<10) | (grub_uint32_t)(regn | regn<<5)); -+ *target = ((grub_uint32_t *) *target) + 1; -+ *(grub_uint32_t *) *target = (lu32id | (grub_uint32_t)((val & 0xfffff00000000)>>32<<5) | (grub_uint32_t)regn);; -+ *target = ((grub_uint32_t *) *target) + 1; -+ *(grub_uint32_t *) *target = (lu52id | (grub_uint32_t)((val & 0xfff0000000000000)>>52<<10) | (grub_uint32_t)(regn | regn<<5));; -+ *target = ((grub_uint32_t *) *target) + 1; -+} -+ -+static void -+write_jump (int regn, void **target) -+{ -+ grub_uint32_t jirl=0x4c000000; -+ -+ *(grub_uint32_t *) *target = (jirl | (grub_uint32_t)(regn<<5)); -+ *target = ((grub_uint32_t *) *target) + 1; -+} -+ -+void -+grub_cpu_relocator_jumper (void *rels, grub_addr_t addr) -+{ -+ write_reg (1, addr, &rels); -+ write_jump (1, &rels); -+} -+ -+void -+grub_cpu_relocator_backward (void *ptr0, void *src, void *dest, -+ grub_size_t size) -+{ -+ void *ptr = ptr0; -+ write_reg (8, (grub_uint64_t) src, &ptr); -+ write_reg (9, (grub_uint64_t) dest, &ptr); -+ write_reg (10, (grub_uint64_t) size, &ptr); -+ grub_memcpy (ptr, &grub_relocator_backward_start, -+ RELOCATOR_SRC_SIZEOF (backward)); -+} -+ -+void -+grub_cpu_relocator_forward (void *ptr0, void *src, void *dest, -+ grub_size_t size) -+{ -+ void *ptr = ptr0; -+ write_reg (8, (grub_uint64_t) src, &ptr); -+ write_reg (9, (grub_uint64_t) dest, &ptr); -+ write_reg (10, (grub_uint64_t) size, &ptr); -+ grub_memcpy (ptr, &grub_relocator_forward_start, -+ RELOCATOR_SRC_SIZEOF (forward)); -+} -+ -+grub_err_t -+grub_relocator64_boot (struct grub_relocator *rel, -+ struct grub_relocator64_state state) -+{ -+ grub_relocator_chunk_t ch; -+ void *ptr; -+ grub_err_t err; -+ void *relst; -+ grub_size_t relsize; -+ grub_size_t stateset_size = 31 * REGW_SIZEOF + JUMP_SIZEOF; -+ unsigned i; -+ grub_addr_t vtarget; -+ -+ err = grub_relocator_alloc_chunk_align (rel, &ch, 0, -+ (0xffffffff - stateset_size) -+ + 1, stateset_size, -+ grub_relocator_align, -+ GRUB_RELOCATOR_PREFERENCE_NONE, 0); -+ if (err) -+ return err; -+ -+ ptr = get_virtual_current_address (ch); -+ for (i = 1; i < 32; i++) -+ write_reg (i, state.gpr[i], &ptr); -+ write_jump (state.jumpreg, &ptr); -+ -+ vtarget = (grub_addr_t) grub_map_memory (get_physical_target_address (ch), -+ stateset_size); -+ -+ err = grub_relocator_prepare_relocs (rel, vtarget, &relst, &relsize); -+ if (err) -+ return err; -+ -+ grub_arch_sync_caches ((void *) relst, relsize); -+ -+ asm volatile ( -+ "ibar 0 \n"); -+ -+ grub_uint64_t val; -+ __asm__ __volatile__( -+ "li.w %0, 0x4\n\t" -+ "csrxchg $r0, %0, 0x0\n\t" -+ : "=r"(val) -+ : -+ : -+ ); -+ -+ ((void (*) (void)) relst) (); -+ -+ /* Not reached. */ -+ return GRUB_ERR_NONE; -+} -diff --git a/grub-core/lib/loongarch64/relocator_asm.S b/grub-core/lib/loongarch64/relocator_asm.S -new file mode 100644 -index 0000000..cf1724d ---- /dev/null -+++ b/grub-core/lib/loongarch64/relocator_asm.S -@@ -0,0 +1,51 @@ -+/* -+ * GRUB -- GRand Unified Bootloader -+ * Copyright (C) 2017 Free Software Foundation, Inc. -+ * -+ * GRUB is free software: you can redistribute it and/or modify -+ * it under the terms of the GNU General Public License as published by -+ * the Free Software Foundation, either version 3 of the License, or -+ * (at your option) any later version. -+ * -+ * GRUB is distributed in the hope that it will be useful, -+ * but WITHOUT ANY WARRANTY; without even the implied warranty of -+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -+ * GNU General Public License for more details. -+ * -+ * You should have received a copy of the GNU General Public License -+ * along with GRUB. If not, see . -+ */ -+ -+#include -+ -+ .p2align 4 /* force 16-byte alignment */ -+ -+VARIABLE (grub_relocator_forward_start) -+ -+copycont1: -+ ld.d $r11,$r8,0 -+ st.d $r11,$r9,0 -+ addi.d $r8, $r8, 8 -+ addi.d $r10, $r10, -8 -+ addi.d $r9, $r9, 8 -+ bne $r10, $r0, copycont1 -+ -+VARIABLE (grub_relocator_forward_end) -+ -+VARIABLE (grub_relocator_backward_start) -+ -+ add.d $r9, $r9, $r10 -+ add.d $r8, $r8, $r10 -+ /* Backward movsl is implicitly off-by-one. compensate that. */ -+ addi.d $r9, $r9, -8 -+ addi.d $r8, $r8, -8 -+copycont2: -+ ld.w $r11,$r8,0 -+ st.w $r11,$r9,0 -+ addi.d $r8, $r8, -8 -+ addi.d $r10, $r10, -8 -+ addi.d $r9, $r9, -8 -+ bne $r10, $r0, copycont2 -+ -+VARIABLE (grub_relocator_backward_end) -+ -diff --git a/grub-core/lib/loongarch64/setjmp.S b/grub-core/lib/loongarch64/setjmp.S -new file mode 100644 -index 0000000..47db814 ---- /dev/null -+++ b/grub-core/lib/loongarch64/setjmp.S -@@ -0,0 +1,74 @@ -+/* -+ * GRUB -- GRand Unified Bootloader -+ * Copyright (C) 2003,2007,2009 Free Software Foundation, Inc. -+ * -+ * GRUB is free software: you can redistribute it and/or modify -+ * it under the terms of the GNU General Public License as published by -+ * the Free Software Foundation, either version 3 of the License, or -+ * (at your option) any later version. -+ * -+ * GRUB is distributed in the hope that it will be useful, -+ * but WITHOUT ANY WARRANTY; without even the implied warranty of -+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -+ * GNU General Public License for more details. -+ * -+ * You should have received a copy of the GNU General Public License -+ * along with GRUB. If not, see . -+ */ -+ -+#include -+#include -+#include -+ -+ .file "setjmp.S" -+ -+GRUB_MOD_LICENSE "GPLv3+" -+ -+ .text -+ -+/* -+ * int grub_setjmp (grub_jmp_buf env) -+ */ -+FUNCTION(grub_setjmp) -+ GRUB_ASM_REG_S $s0, $a0,0 -+ GRUB_ASM_REG_S $s1, $a0,8 -+ GRUB_ASM_REG_S $s2, $a0,16 -+ GRUB_ASM_REG_S $s3, $a0,24 -+ GRUB_ASM_REG_S $s4, $a0,32 -+ GRUB_ASM_REG_S $s5, $a0,40 -+ GRUB_ASM_REG_S $s6, $a0,48 -+ GRUB_ASM_REG_S $s7, $a0,56 -+ GRUB_ASM_REG_S $s8, $a0,64 -+ GRUB_ASM_REG_S $fp, $a0,72 -+ GRUB_ASM_REG_S $sp, $a0,80 -+ GRUB_ASM_REG_S $ra, $a0,88 -+ move $v0, $zero -+ move $v1, $zero -+ jr $ra -+ nop -+/* -+ * int grub_longjmp (grub_jmp_buf env, int val) -+ */ -+FUNCTION(grub_longjmp) -+ GRUB_ASM_REG_L $s0, $a0,0 -+ GRUB_ASM_REG_L $s1, $a0,8 -+ GRUB_ASM_REG_L $s2, $a0,16 -+ GRUB_ASM_REG_L $s3, $a0,24 -+ GRUB_ASM_REG_L $s4, $a0,32 -+ GRUB_ASM_REG_L $s5, $a0,40 -+ GRUB_ASM_REG_L $s6, $a0,48 -+ GRUB_ASM_REG_L $s7, $a0,56 -+ GRUB_ASM_REG_L $s8, $a0,64 -+ GRUB_ASM_REG_L $fp, $a0,72 -+ GRUB_ASM_REG_L $sp, $a0,80 -+ GRUB_ASM_REG_L $ra, $a0,88 -+ addi.w $v0, $zero, 1 -+ /* -+ * replace: movn $v0, $a1, $a1 -+ */ -+ bnez $a1, .ZW0 -+ addi.d $v0, $a1, 0 -+.ZW0: -+ addi.d $v1,$zero,0 -+ jr $ra -+ nop -diff --git a/grub-core/lib/loongson/reboot.c b/grub-core/lib/loongson/reboot.c -new file mode 100644 -index 0000000..107787a ---- /dev/null -+++ b/grub-core/lib/loongson/reboot.c -@@ -0,0 +1,33 @@ -+/* -+ * GRUB -- GRand Unified Bootloader -+ * Copyright (C) 2011 Free Software Foundation, Inc. -+ * -+ * GRUB is free software: you can redistribute it and/or modify -+ * it under the terms of the GNU General Public License as published by -+ * the Free Software Foundation, either version 3 of the License, or -+ * (at your option) any later version. -+ * -+ * GRUB is distributed in the hope that it will be useful, -+ * but WITHOUT ANY WARRANTY; without even the implied warranty of -+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -+ * GNU General Public License for more details. -+ * -+ * You should have received a copy of the GNU General Public License -+ * along with GRUB. If not, see . -+ */ -+ -+#include -+#include -+#include -+#include -+#include -+#include -+ -+void -+grub_la_reboot (void) -+{ -+ grub_machine_fini (GRUB_LOADER_FLAG_NORETURN); -+ efi_call_4 (grub_efi_system_table->runtime_services->reset_system, -+ GRUB_EFI_RESET_COLD, GRUB_EFI_SUCCESS, 0, NULL); -+ for (;;) ; -+} -diff --git a/grub-core/lib/setjmp.S b/grub-core/lib/setjmp.S -index f6e4905..023dc90 100644 ---- a/grub-core/lib/setjmp.S -+++ b/grub-core/lib/setjmp.S -@@ -15,6 +15,8 @@ - #include "./arm/setjmp.S" - #elif defined(__aarch64__) - #include "./arm64/setjmp.S" -+#elif defined(__loongarch64) -+#include "./loongarch64/setjmp.S" - #else - #error "Unknown target cpu type" - #endif -diff --git a/grub-core/loader/efi/chainloader.c b/grub-core/loader/efi/chainloader.c -index b72e6bd..f4066b5 100644 ---- a/grub-core/loader/efi/chainloader.c -+++ b/grub-core/loader/efi/chainloader.c -@@ -343,6 +343,8 @@ static const grub_uint16_t machine_type __attribute__((__unused__)) = - GRUB_PE32_MACHINE_I386; - #elif defined(__ia64__) - GRUB_PE32_MACHINE_IA64; -+#elif defined(__loongarch64) -+ GRUB_PE32_MACHINE_LOONGARCH64; - #else - #error this architecture is not supported by grub2 - #endif -diff --git a/grub-core/loader/loongarch64/linux.c b/grub-core/loader/loongarch64/linux.c -new file mode 100644 -index 0000000..7594ca2 ---- /dev/null -+++ b/grub-core/loader/loongarch64/linux.c -@@ -0,0 +1,690 @@ -+/* linux.c - boot Linux */ -+/* -+ * GRUB -- GRand Unified Bootloader -+ * Copyright (C) 2003,2004,2005,2007,2009,2010,2017 Free Software Foundation, Inc. -+ * -+ * GRUB is free software: you can redistribute it and/or modify -+ * it under the terms of the GNU General Public License as published by -+ * the Free Software Foundation, either version 3 of the License, or -+ * (at your option) any later version. -+ * -+ * GRUB is distributed in the hope that it will be useful, -+ * but WITHOUT ANY WARRANTY; without even the implied warranty of -+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -+ * GNU General Public License for more details. -+ * -+ * You should have received a copy of the GNU General Public License -+ * along with GRUB. If not, see . -+ */ -+ -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+ -+GRUB_MOD_LICENSE ("GPLv3+"); -+ -+#pragma GCC diagnostic ignored "-Wcast-align" -+ -+typedef unsigned long size_t; -+ -+#define FDT_ADDR_CELLS_STRING "#address-cells" -+#define FDT_SIZE_CELLS_STRING "#size-cells" -+#define FDT_ADDR_SIZE_EXTRA ((2 * grub_fdt_prop_entry_size (sizeof(grub_uint32_t))) + \ -+ sizeof (FDT_ADDR_CELLS_STRING) + \ -+ sizeof (FDT_SIZE_CELLS_STRING)) -+ -+static grub_dl_t my_mod; -+static int loaded; -+static int initrd_loaded = 0; -+static grub_size_t linux_size; -+ -+static struct grub_relocator *relocator; -+static grub_addr_t target_addr, entry_addr; -+static int linux_argc; -+static grub_uint8_t *linux_args_addr; -+static grub_off_t rd_addr_arg_off, rd_size_arg_off, initrd_addr_arg_off; -+ -+static void *fdt; -+static int is_fdt_boot; -+static grub_addr_t initrd_start, initrd_end; -+static char *fdt_linux_args; -+ -+static grub_err_t -+allocate_fdt_and_exit_boot (void) -+{ -+ int node, retval; -+ grub_err_t err; -+ unsigned int size; -+ grub_efi_uintn_t mmap_size; -+ grub_efi_uintn_t desc_size; -+ grub_efi_uint32_t desc_version; -+ grub_efi_memory_descriptor_t *mmap_buf; -+ -+ size = GRUB_FDT_EMPTY_TREE_SZ + FDT_ADDR_SIZE_EXTRA + GRUB_EFI_LINUX_FDT_EXTRA_SPACE; -+ -+ fdt = grub_efi_allocate_any_pages (GRUB_EFI_BYTES_TO_PAGES (size)); -+ if (!fdt) -+ return GRUB_ERR_OUT_OF_MEMORY; -+ -+ grub_fdt_create_empty_tree (fdt, size); -+ grub_fdt_set_prop32 (fdt, 0, FDT_ADDR_CELLS_STRING, 2); -+ grub_fdt_set_prop32 (fdt, 0, FDT_SIZE_CELLS_STRING, 2); -+ -+ node = grub_fdt_find_subnode (fdt, 0, "chosen"); -+ if (node < 0) -+ node = grub_fdt_add_subnode (fdt, 0, "chosen"); -+ if (node < 1) -+ goto failure; -+ -+ grub_dprintf ("loongson", "command_line %s, len %ld\n", -+ fdt_linux_args, grub_strlen(fdt_linux_args) + 1); -+ if ((fdt_linux_args != NULL) && (grub_strlen(fdt_linux_args) > 0)) { -+ retval = grub_fdt_set_prop (fdt, node, "bootargs", fdt_linux_args, -+ grub_strlen(fdt_linux_args) + 1); -+ if (retval) -+ goto failure; -+ } -+ -+ /* Set initrd info */ -+ if (initrd_start && initrd_end > initrd_start) -+ { -+ grub_dprintf ("linux", "Initrd @ %p-%p\n", -+ (void *) initrd_start, (void *) initrd_end); -+ -+ retval = grub_fdt_set_prop64 (fdt, node, "linux,initrd-start", -+ initrd_start); -+ if (retval) -+ goto failure; -+ retval = grub_fdt_set_prop64 (fdt, node, "linux,initrd-end", -+ initrd_end); -+ if (retval) -+ goto failure; -+ } -+ -+ node = grub_fdt_find_subnode (fdt, 0, "chosen"); -+ retval = grub_fdt_set_prop64 (fdt, node, "linux,uefi-system-table", -+ (grub_uint64_t)grub_efi_system_table); -+ if (retval) -+ goto failure; -+ -+ mmap_size = grub_efi_find_mmap_size (); -+ if (! mmap_size) -+ return grub_errno; -+ mmap_buf = grub_efi_allocate_any_pages (GRUB_EFI_BYTES_TO_PAGES (mmap_size)); -+ if (! mmap_buf) -+ return grub_error (GRUB_ERR_IO, "cannot allocate memory map"); -+ err = grub_efi_finish_boot_services (&mmap_size, mmap_buf, NULL, -+ &desc_size, &desc_version); -+ if (err) -+ return err; -+ -+ if (!mmap_buf || !mmap_size || !desc_size) -+ return GRUB_ERR_BAD_ARGUMENT; -+ -+ retval = grub_fdt_set_prop64 (fdt, node, "linux,uefi-mmap-start", -+ (grub_uint64_t)mmap_buf); -+ if (retval) -+ goto failure; -+ -+ retval = grub_fdt_set_prop32 (fdt, node, "linux,uefi-mmap-size", -+ mmap_size); -+ if (retval) -+ goto failure; -+ -+ retval = grub_fdt_set_prop32 (fdt, node, "linux,uefi-mmap-desc-size", -+ desc_size); -+ if (retval) -+ goto failure; -+ -+ retval = grub_fdt_set_prop32 (fdt, node, "linux,uefi-mmap-desc-ver", -+ desc_version); -+ if (retval) -+ goto failure; -+ -+ return GRUB_ERR_NONE; -+ -+failure: -+ if (!fdt) { -+ return GRUB_ERR_BAD_OS; -+ } -+ grub_efi_free_pages ((grub_addr_t) fdt, -+ GRUB_EFI_BYTES_TO_PAGES (grub_fdt_get_totalsize (fdt))); -+ fdt = NULL; -+ return grub_error(GRUB_ERR_BAD_OS, "failed to install/update FDT"); -+} -+ -+static grub_err_t -+allocate_boot_params_and_exit_boot (void) -+{ -+ grub_efi_uintn_t mmap_size; -+ grub_efi_uintn_t desc_size; -+ grub_efi_uint32_t desc_version; -+ grub_efi_memory_descriptor_t *mmap_buf; -+ grub_efi_memory_descriptor_t * lsdesc = NULL; -+ grub_err_t err; -+ struct boot_params_interface * boot_params; -+ mem_map_v1 * mem_map_v1_table = NULL; -+ unsigned long bpi_version = 0; -+ grub_int8_t checksum = 0; -+ grub_uint32_t tmp_index = 0; -+ grub_uint32_t free_index = 0; -+ grub_uint32_t reserve_index = 0; -+ grub_uint32_t acpi_table_index = 0; -+ grub_uint32_t acpi_nvs_index = 0; -+ -+ struct memmap_v1 reserve_mem_v1[GRUB_EFI_LOONGSON_MMAP_MAX]; -+ struct memmap_v1 free_mem_v1[GRUB_EFI_LOONGSON_MMAP_MAX]; -+ struct memmap_v1 acpi_table_mem_v1[GRUB_EFI_LOONGSON_MMAP_MAX]; -+ struct memmap_v1 acpi_nvs_mem_v1[GRUB_EFI_LOONGSON_MMAP_MAX]; -+ -+ grub_memset(reserve_mem_v1, 0, sizeof(struct memmap_v1) * GRUB_EFI_LOONGSON_MMAP_MAX); -+ grub_memset(free_mem_v1, 0, sizeof(struct memmap_v1) * GRUB_EFI_LOONGSON_MMAP_MAX); -+ grub_memset(acpi_table_mem_v1, 0, sizeof(struct memmap_v1) * GRUB_EFI_LOONGSON_MMAP_MAX); -+ grub_memset(acpi_nvs_mem_v1, 0, sizeof(struct memmap_v1) * GRUB_EFI_LOONGSON_MMAP_MAX); -+ -+ boot_params = (struct boot_params_interface *)grub_efi_loongson_get_boot_params(); -+ -+ ext_list * listpointer = NULL; -+ /* Check extlist headers */ -+ listpointer = boot_params->extlist; -+ for( ;listpointer != NULL; listpointer = listpointer->next) -+ { -+ char *pl= (char *)&(listpointer->signature); -+ if(grub_strncmp(pl, "MEM", 3) == 0) -+ { -+ mem_map_v1_table = (mem_map_v1 *)listpointer; -+ break; -+ } -+ } -+ -+ mmap_size = grub_efi_find_mmap_size (); -+ if (! mmap_size) -+ return grub_errno; -+ mmap_buf = grub_efi_allocate_any_pages (GRUB_EFI_BYTES_TO_PAGES (mmap_size)); -+ if (! mmap_buf) -+ return grub_error (GRUB_ERR_IO, "cannot allocate memory map"); -+ err = grub_efi_finish_boot_services (&mmap_size, mmap_buf, NULL, -+ &desc_size, &desc_version); -+ if (err) -+ return err; -+ -+ if (!mmap_buf || !mmap_size || !desc_size) -+ return GRUB_ERR_BAD_ARGUMENT; -+ -+ char *p = (char *)&(boot_params->signature); -+ bpi_version = grub_efi_get_bpi_version(p); -+ grub_dprintf("loongson", "get bpi version:%ld\n", bpi_version); -+ -+ if (bpi_version <= GRUB_EFI_BPI_VER_V2) -+ { -+ /* -+ According to UEFI SPEC,mmap_buf is the accurate Memory Map array \ -+ now we can fill platform specific memory structure. -+ */ -+ for(lsdesc = mmap_buf; lsdesc < (grub_efi_memory_descriptor_t *)((char *)mmap_buf + mmap_size); -+ lsdesc = (grub_efi_memory_descriptor_t *)((char *)lsdesc + desc_size)) -+ { -+ grub_dprintf("loongson", "type:%d, phy_start:0x%lx, phy_end:0x%lx\n", lsdesc->type, -+ lsdesc->physical_start, lsdesc->physical_start + lsdesc->num_pages * GRUB_EFI_PAGE_SIZE); -+ -+ /* System RAM */ -+ if ((lsdesc->type != GRUB_EFI_ACPI_RECLAIM_MEMORY) && \ -+ (lsdesc->type != GRUB_EFI_ACPI_MEMORY_NVS) && \ -+ (lsdesc->type != GRUB_EFI_RUNTIME_SERVICES_DATA) && \ -+ (lsdesc->type != GRUB_EFI_RUNTIME_SERVICES_CODE) && \ -+ (lsdesc->type != GRUB_EFI_RESERVED_MEMORY_TYPE) && \ -+ (lsdesc->type != GRUB_EFI_PAL_CODE)) -+ { -+ free_mem_v1[free_index].memtype = GRUB_EFI_LOONGSON_SYSTEM_RAM; -+ free_mem_v1[free_index].memstart = (lsdesc->physical_start) & GRUB_EFI_MAX_PHY_ADDRESS; -+ free_mem_v1[free_index].memsize = lsdesc->num_pages * GRUB_EFI_PAGE_SIZE; -+ free_index++; -+ -+ /*ACPI*/ -+ } else if ((lsdesc->type == GRUB_EFI_ACPI_RECLAIM_MEMORY)){ -+ acpi_table_mem_v1[acpi_table_index].memtype = GRUB_EFI_LOONGSON_ACPI_TABLE; -+ acpi_table_mem_v1[acpi_table_index].memstart = (lsdesc->physical_start) & GRUB_EFI_MAX_PHY_ADDRESS; -+ acpi_table_mem_v1[acpi_table_index].memsize = lsdesc->num_pages * GRUB_EFI_PAGE_SIZE; -+ acpi_table_index++; -+ } else if ((lsdesc->type == GRUB_EFI_ACPI_MEMORY_NVS)){ -+ acpi_nvs_mem_v1[acpi_nvs_index].memtype = GRUB_EFI_LOONGSON_ACPI_NVS; -+ acpi_nvs_mem_v1[acpi_nvs_index].memstart = (lsdesc->physical_start) & GRUB_EFI_MAX_PHY_ADDRESS; -+ acpi_nvs_mem_v1[acpi_nvs_index].memsize = lsdesc->num_pages * GRUB_EFI_PAGE_SIZE; -+ acpi_nvs_index++; -+ -+ /* Reserve */ -+ } else { -+ reserve_mem_v1[reserve_index].memtype = GRUB_EFI_LOONGSON_MEMORY_RESERVED; -+ reserve_mem_v1[reserve_index].memstart = (lsdesc->physical_start) & GRUB_EFI_MAX_PHY_ADDRESS; -+ reserve_mem_v1[reserve_index].memsize = lsdesc->num_pages * GRUB_EFI_PAGE_SIZE; -+ reserve_index++; -+ } -+ } -+ -+ tmp_index = mem_map_v1_table->mapcount; -+ /*System RAM Sort*/ -+ tmp_index = grub_efi_loongson_memmap_v1_sort(free_mem_v1, free_index, mem_map_v1_table, -+ tmp_index, GRUB_EFI_LOONGSON_SYSTEM_RAM); -+ /*ACPI Sort*/ -+ tmp_index = grub_efi_loongson_memmap_v1_sort(acpi_table_mem_v1, acpi_table_index, -+ mem_map_v1_table, tmp_index, GRUB_EFI_LOONGSON_ACPI_TABLE); -+ tmp_index = grub_efi_loongson_memmap_v1_sort(acpi_nvs_mem_v1, acpi_nvs_index, -+ mem_map_v1_table, tmp_index, GRUB_EFI_LOONGSON_ACPI_NVS); -+ /*Reserve Sort*/ -+ { -+ grub_uint64_t loongarch_addr; -+ asm volatile ("csrrd %0, 0x181" : "=r" (loongarch_addr)); -+ if ((loongarch_addr & 0xff00000000000000) == 0x9000000000000000) -+ tmp_index = grub_efi_loongson_memmap_v1_sort(reserve_mem_v1, reserve_index, -+ mem_map_v1_table, tmp_index, -+ GRUB_EFI_LOONGSON_MEMORY_RESERVED); -+ else -+ tmp_index = grub_efi_loongson_memmap_v1_sort(reserve_mem_v1, reserve_index, -+ mem_map_v1_table, tmp_index, -+ GRUB_EFI_LOONGSON_MEMORY_RESERVED + 1); -+ } -+ mem_map_v1_table->mapcount = tmp_index; -+ mem_map_v1_table->header.checksum = 0; -+ -+ checksum = grub_efi_loongson_grub_calculatechecksum8((grub_uint8_t *)mem_map_v1_table, -+ mem_map_v1_table->header.length); -+ mem_map_v1_table->header.checksum = checksum; -+ } -+ -+ return GRUB_ERR_NONE; -+} -+ -+static grub_err_t -+grub_linux_boot (void) -+{ -+ struct grub_relocator64_state state; -+ -+ grub_memset (&state, 0, sizeof (state)); -+ -+ /* Boot the kernel. */ -+ state.gpr[1] = entry_addr; -+ grub_dprintf("loongson", "entry_addr is 0x%lx\n", state.gpr[1]); -+ -+ if (is_fdt_boot == 1) -+ { -+ if (allocate_fdt_and_exit_boot () != GRUB_ERR_NONE) -+ return grub_errno; -+ -+ state.gpr[4] = 1 << FLAGS_EFI_SUPPORT_BIT; -+ state.gpr[5] = (grub_uint64_t)fdt; -+ state.gpr[6] = 0; -+ } else { -+ state.gpr[4] = linux_argc; -+ state.gpr[5] = (grub_addr_t) linux_args_addr; -+ state.gpr[6] = (grub_uint64_t)grub_efi_loongson_get_boot_params(); -+ -+ if (allocate_boot_params_and_exit_boot () != GRUB_ERR_NONE) -+ return grub_errno; -+ } -+ grub_dprintf("loongson", "gpr[4]:%ld, gpr[5]:0x%lx, gpr[6]:0x%lx\n", -+ state.gpr[4], state.gpr[5], state.gpr[6]); -+ -+ state.jumpreg = 1; -+ grub_relocator64_boot (relocator, state); -+ -+ return GRUB_ERR_NONE; -+} -+ -+static grub_err_t -+grub_linux_unload (void) -+{ -+ grub_relocator_unload (relocator); -+ grub_dl_unref (my_mod); -+ loaded = 0; -+ -+ return GRUB_ERR_NONE; -+} -+ -+static grub_err_t -+grub_linux_load64 (grub_elf_t elf, const char *filename) -+{ -+ Elf64_Addr base; -+ grub_err_t err; -+ grub_uint8_t *playground; -+ grub_uint64_t addr; -+ int flag; -+ -+ /* Linux's entry point incorrectly contains a virtual address. */ -+ entry_addr = elf->ehdr.ehdr64.e_entry; -+ grub_dprintf("loongson", "entry address = 0x%lx\n", entry_addr); -+ -+ linux_size = grub_elf64_size (elf, &base, 0); -+ grub_dprintf("loongson", "base = 0x%lx\n", base); -+ -+ if (linux_size == 0) -+ return grub_errno; -+ target_addr = base; -+ linux_size = ALIGN_UP (base + linux_size - base, 8); -+ -+ asm volatile ("csrrd %0, 0x181" : "=r" (addr)); -+ if (addr & 0x1) { -+ flag = GRUB_ELF_LOAD_FLAGS_NONE; -+ } else { -+ flag = GRUB_ELF_LOAD_FLAGS_30BITS; -+ base &= ~ELF64_LOADMASK; -+ entry_addr &= ~ELF64_LOADMASK; -+ } -+ -+ relocator = grub_relocator_new (); -+ if (!relocator) -+ return grub_errno; -+ -+ { -+ grub_relocator_chunk_t ch; -+ err = grub_relocator_alloc_chunk_addr (relocator, &ch, -+ grub_vtop ((void *) target_addr), -+ linux_size); -+ if (err) -+ return err; -+ playground = get_virtual_current_address (ch); -+ } -+ -+ /* Now load the segments into the area we claimed. */ -+ return grub_elf64_load (elf, filename, playground - base, flag, 0, 0); -+} -+ -+static grub_err_t -+grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)), -+ int argc, char *argv[]) -+{ -+ grub_elf_t elf = 0; -+ grub_err_t err; -+ int args_size = 0; -+ -+ grub_dl_ref (my_mod); -+ -+ if (argc == 0) -+ { -+ return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected")); -+ } -+ -+ elf = grub_elf_open (argv[0], GRUB_FILE_TYPE_LINUX_KERNEL); -+ if (! elf) -+ return grub_errno; -+ -+ if (elf->ehdr.ehdr64.e_type != ET_EXEC) -+ { -+ grub_elf_close (elf); -+ return grub_error (GRUB_ERR_UNKNOWN_OS, -+ N_("this ELF file is not of the right type")); -+ } -+ -+ /* Release the previously used memory. */ -+ grub_loader_unset (); -+ loaded = 0; -+ -+ if (grub_elf_is_elf64 (elf)) -+ err = grub_linux_load64 (elf, argv[0]); -+ else -+ err = grub_error (GRUB_ERR_BAD_OS, N_("invalid arch-dependent ELF magic")); -+ -+ grub_elf_close (elf); -+ -+ if (err) -+ return err; -+ -+ if (grub_efi_loongson_get_boot_params() == NULL) { -+ grub_size_t cmdline_size; -+ -+ is_fdt_boot = 1; -+ cmdline_size = grub_loader_cmdline_size (argc, argv) + sizeof (LINUX_IMAGE); -+ fdt_linux_args = grub_malloc (cmdline_size); -+ if (!fdt_linux_args) -+ { -+ grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory")); -+ goto fail; -+ } -+ grub_memcpy (fdt_linux_args, LINUX_IMAGE, sizeof (LINUX_IMAGE)); -+ err = grub_create_loader_cmdline (argc, argv, -+ fdt_linux_args + sizeof (LINUX_IMAGE) - 1, -+ cmdline_size, -+ GRUB_VERIFY_KERNEL_CMDLINE); -+ if (err) -+ goto fail; -+ grub_dprintf("loongson", "fdt linux args:%s\n", -+ fdt_linux_args + sizeof (LINUX_IMAGE) - 1); -+ -+ } else { -+ int i; -+ grub_uint64_t *linux_argv; -+ char *linux_args; -+ -+ is_fdt_boot = 0; -+ /* For arguments. */ -+ linux_argc = argc; -+ /* Main arguments. */ -+ args_size = (linux_argc) * sizeof (grub_uint64_t); -+ /* Initrd address/size and initrd */ -+ args_size += 3 * sizeof (grub_uint64_t); -+ /* NULL terminator. */ -+ args_size += sizeof (grub_uint64_t); -+ /* First argument is always "a0". */ -+ args_size += ALIGN_UP (sizeof ("a0"), 4); -+ /* Normal arguments. */ -+ for (i = 1; i < argc; i++) -+ args_size += ALIGN_UP (grub_strlen (argv[i]) + 1, 4); -+ -+ /* rd arguments. */ -+ args_size += ALIGN_UP (sizeof ("rd_start=0xXXXXXXXXXXXXXXXX"), 4); -+ args_size += ALIGN_UP (sizeof ("rd_size=0xXXXXXXXXXXXXXXXX"), 4); -+ args_size += ALIGN_UP (sizeof ("initrd=0xXXXXXXXXXXXXXXXX,0xXXXXXXXXXXXXXXXX"), 4); -+ -+ args_size = ALIGN_UP (args_size, 8); -+ -+ linux_args_addr = grub_efi_allocate_any_pages (GRUB_EFI_BYTES_TO_PAGES (args_size)); -+ grub_dprintf ("linux", "linux args numpages: %lld\n", -+ (long long) GRUB_EFI_BYTES_TO_PAGES (args_size)); -+ if (!linux_args_addr) -+ { -+ grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory")); -+ goto fail; -+ } -+ linux_argv = (grub_uint64_t *) linux_args_addr; -+ linux_args = (char *) (linux_argv + (linux_argc + 1 + 3)); -+ -+ grub_memcpy (linux_args, "a0", sizeof ("a0")); -+ *linux_argv = (grub_uint64_t) (grub_addr_t) linux_args; -+ linux_argv++; -+ linux_args += ALIGN_UP (sizeof ("a0"), 4); -+ -+ for (i = 1; i < argc; i++) -+ { -+ grub_memcpy (linux_args, argv[i], grub_strlen (argv[i]) + 1); -+ *linux_argv = (grub_uint64_t) (grub_addr_t) linux_args; -+ linux_argv++; -+ linux_args += ALIGN_UP (grub_strlen (argv[i]) + 1, 4); -+ } -+ -+ /* Reserve space for rd arguments. */ -+ rd_addr_arg_off = (grub_uint8_t *) linux_args - linux_args_addr; -+ linux_args += ALIGN_UP (sizeof ("rd_start=0xXXXXXXXXXXXXXXXX"), 4); -+ *linux_argv = 0; -+ linux_argv++; -+ -+ rd_size_arg_off = (grub_uint8_t *) linux_args - linux_args_addr; -+ linux_args += ALIGN_UP (sizeof ("rd_size=0xXXXXXXXXXXXXXXXX"), 4); -+ *linux_argv = 0; -+ linux_argv++; -+ -+ /* Reserve space for initrd arguments. */ -+ initrd_addr_arg_off = (grub_uint8_t *) linux_args - linux_args_addr; -+ linux_args += ALIGN_UP (sizeof ("initrd=0xXXXXXXXXXXXXXXXX,0xXXXXXXXXXXXXXXXX"), 4); -+ *linux_argv = 0; -+ linux_argv++; -+ -+ *linux_argv = 0; -+ } -+ -+ if (grub_errno == GRUB_ERR_NONE) -+ { -+ grub_loader_set (grub_linux_boot, grub_linux_unload, 0); -+ loaded = 1; -+ } -+ -+ initrd_loaded = 0; -+ -+fail: -+ if (grub_errno != GRUB_ERR_NONE) -+ { -+ grub_dl_unref (my_mod); -+ loaded = 0; -+ } -+ -+ if (fdt_linux_args && !loaded) -+ grub_free (fdt_linux_args); -+ -+ if (linux_args_addr && !loaded) -+ grub_efi_free_pages ((grub_addr_t) linux_args_addr, -+ GRUB_EFI_BYTES_TO_PAGES (args_size)); -+ -+ return grub_errno; -+} -+ -+#define INITRD_MAX_ADDRESS_OFFSET (32ULL * 1024 * 1024 * 1024) -+ -+/* -+ * This function returns a pointer to a legally allocated initrd buffer, -+ * or NULL if unsuccessful -+ */ -+static void * -+allocate_initrd_mem (int initrd_pages) -+{ -+ grub_addr_t max_addr; -+ -+ if (grub_efi_get_ram_base (&max_addr) != GRUB_ERR_NONE) -+ return NULL; -+ -+ max_addr += INITRD_MAX_ADDRESS_OFFSET - 1; -+ -+ return grub_efi_allocate_pages_real (max_addr, initrd_pages, -+ GRUB_EFI_ALLOCATE_MAX_ADDRESS, -+ GRUB_EFI_LOADER_DATA); -+} -+ -+ -+static grub_err_t -+grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)), -+ int argc, char *argv[]) -+{ -+ grub_size_t initrd_size, initrd_pages; -+ grub_err_t err; -+ void *initrd_mem = NULL; -+ struct grub_linux_initrd_context initrd_ctx = { 0, 0, 0 }; -+ -+ if (argc == 0) -+ return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected")); -+ -+ if (!loaded) -+ return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("you need to load the kernel first")); -+ -+ if (initrd_loaded) -+ return grub_error (GRUB_ERR_BAD_ARGUMENT, "only one initrd command can be issued."); -+ -+ if (grub_initrd_init (argc, argv, &initrd_ctx)) -+ goto fail; -+ -+ initrd_size = grub_get_initrd_size (&initrd_ctx); -+ -+ if (is_fdt_boot == 1) //fdt -+ { -+ initrd_pages = (GRUB_EFI_BYTES_TO_PAGES (initrd_size)); -+ initrd_mem = allocate_initrd_mem (initrd_pages); -+ if (!initrd_mem) -+ { -+ grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory")); -+ goto fail; -+ } -+ } else { -+ grub_relocator_chunk_t ch; -+ err = grub_relocator_alloc_chunk_align (relocator, &ch, -+ 0, (0xffffffff - initrd_size) + 1, -+ initrd_size, 0x10000, -+ GRUB_RELOCATOR_PREFERENCE_HIGH, 0); -+ -+ if (err) -+ return err; -+ initrd_mem = get_virtual_current_address (ch); -+ } -+ -+ if (grub_initrd_load (&initrd_ctx, argv, initrd_mem)) -+ goto fail; -+ -+ initrd_start = (grub_addr_t) initrd_mem; -+ initrd_end = initrd_start + initrd_size; -+ grub_dprintf ("linux", "[addr=%p, size=0x%lx]\n", -+ (void *) initrd_start, initrd_size); -+ -+ if (is_fdt_boot == 0) //bpi -+ { -+ grub_snprintf ((char *) linux_args_addr + rd_addr_arg_off, -+ sizeof ("rd_start=0xXXXXXXXXXXXXXXXX"), -+ "rd_start=0x%lx", -+ (grub_uint64_t) initrd_mem); -+ ((grub_uint64_t *) linux_args_addr)[linux_argc] -+ = (grub_uint64_t) ((grub_addr_t) linux_args_addr + rd_addr_arg_off); -+ linux_argc++; -+ -+ grub_snprintf ((char *) linux_args_addr + rd_size_arg_off, -+ sizeof ("rd_size=0xXXXXXXXXXXXXXXXXX"), "rd_size=0x%lx", -+ (grub_uint64_t) initrd_size); -+ ((grub_uint64_t *) linux_args_addr)[linux_argc] -+ = (grub_uint64_t) ((grub_addr_t) linux_args_addr + rd_size_arg_off); -+ linux_argc++; -+ -+ grub_snprintf ((char *) linux_args_addr + initrd_addr_arg_off, -+ sizeof ("initrd=0xXXXXXXXXXXXXXXXX,0xXXXXXXXXXXXXXXXX"), -+ "initrd=0x%lx,0x%lx", -+ ((grub_uint64_t) initrd_mem & GRUB_EFI_MAX_PHY_ADDRESS), -+ (grub_uint64_t) initrd_size); -+ ((grub_uint64_t *) linux_args_addr)[linux_argc] -+ = (grub_uint64_t) ((grub_addr_t) linux_args_addr + initrd_addr_arg_off); -+ linux_argc++; -+ } -+ -+ initrd_loaded = 1; -+ -+ fail: -+ grub_initrd_close (&initrd_ctx); -+ if (is_fdt_boot == 1) -+ if (initrd_mem && !initrd_start) -+ grub_efi_free_pages ((grub_addr_t) initrd_mem, initrd_pages); -+ -+ return grub_errno; -+} -+ -+static grub_command_t cmd_linux, cmd_initrd; -+ -+GRUB_MOD_INIT(linux) -+{ -+ cmd_linux = grub_register_command ("linux", grub_cmd_linux, -+ 0, N_("Load Linux.")); -+ cmd_initrd = grub_register_command ("initrd", grub_cmd_initrd, -+ 0, N_("Load initrd.")); -+ my_mod = mod; -+} -+ -+GRUB_MOD_FINI(linux) -+{ -+ grub_unregister_command (cmd_linux); -+ grub_unregister_command (cmd_initrd); -+} -diff --git a/include/grub/dl.h b/include/grub/dl.h -index 6f46b7e..50e04ac 100644 ---- a/include/grub/dl.h -+++ b/include/grub/dl.h -@@ -360,7 +360,7 @@ grub_arch_dl_get_tramp_got_size (const void *ehdr, grub_size_t *tramp, - #define GRUB_ARCH_DL_GOT_ALIGN 4 - #endif - --#if defined (__aarch64__) || defined (__sparc__) -+#if defined (__aarch64__) || defined (__sparc__) || defined (__loongarch64) - #define GRUB_ARCH_DL_TRAMP_ALIGN 8 - #define GRUB_ARCH_DL_GOT_ALIGN 8 - #endif -diff --git a/include/grub/efi/api.h b/include/grub/efi/api.h -index 510a403..a57187f 100644 ---- a/include/grub/efi/api.h -+++ b/include/grub/efi/api.h -@@ -2117,7 +2117,8 @@ struct grub_efi_memory_attribute_protocol - typedef struct grub_efi_memory_attribute_protocol grub_efi_memory_attribute_protocol_t; - - #if (GRUB_TARGET_SIZEOF_VOID_P == 4) || defined (__ia64__) \ -- || defined (__aarch64__) || defined (__MINGW64__) || defined (__CYGWIN__) -+ || defined (__aarch64__) || defined (__MINGW64__) || defined (__CYGWIN__) \ -+ || defined (__loongarch64) - - #define efi_call_0(func) func() - #define efi_call_1(func, a) func(a) -diff --git a/include/grub/efi/efi.h b/include/grub/efi/efi.h -index 8ca8c38..fe1df9d 100644 ---- a/include/grub/efi/efi.h -+++ b/include/grub/efi/efi.h -@@ -138,7 +138,7 @@ extern void (*EXPORT_VAR(grub_efi_net_config)) (grub_efi_handle_t hnd, - extern grub_addr_t EXPORT_VAR(grub_stack_addr); - extern grub_size_t EXPORT_VAR(grub_stack_size); - --#if defined(__arm__) || defined(__aarch64__) -+#if defined(__arm__) || defined(__aarch64__) || defined (__loongarch__) - void *EXPORT_FUNC(grub_efi_get_firmware_fdt)(void); - grub_err_t EXPORT_FUNC(grub_efi_get_ram_base)(grub_addr_t *); - #include -diff --git a/include/grub/efi/pe32.h b/include/grub/efi/pe32.h -index 45c9f8b..f612a55 100644 ---- a/include/grub/efi/pe32.h -+++ b/include/grub/efi/pe32.h -@@ -89,6 +89,7 @@ struct grub_pe32_coff_header - }; - - #define GRUB_PE32_MACHINE_I386 0x14c -+#define GRUB_PE32_MACHINE_LOONGARCH64 0x6264 - #define GRUB_PE32_MACHINE_IA64 0x200 - #define GRUB_PE32_MACHINE_X86_64 0x8664 - #define GRUB_PE32_MACHINE_ARMTHUMB_MIXED 0x01c2 -@@ -355,6 +356,7 @@ struct grub_pe32_fixup_block - #define GRUB_PE32_REL_BASED_IA64_IMM64 9 - #define GRUB_PE32_REL_BASED_DIR64 10 - #define GRUB_PE32_REL_BASED_HIGH3ADJ 11 -+#define GRUB_PE32_REL_BASED_LOONGARCH64 8 - - struct grub_pe32_symbol - { -diff --git a/include/grub/elf.h b/include/grub/elf.h -index c8492f9..2c0b163 100644 ---- a/include/grub/elf.h -+++ b/include/grub/elf.h -@@ -247,6 +247,7 @@ typedef struct - #define EM_XTENSA 94 /* Tensilica Xtensa Architecture */ - #define EM_NUM 95 - #define EM_AARCH64 183 /* ARM 64-bit architecture */ -+#define EM_LOONGARCH64 258 /* LoongArch64 architecture */ - - /* If it is necessary to assign new unofficial EM_* values, please - pick large random numbers (0x8523, 0xa7f2, etc.) to minimize the -@@ -1448,6 +1449,60 @@ typedef struct - #define OHWA0_R4KEOP_CHECKED 0x00000001 - #define OHWA1_R4KEOP_CLEAN 0x00000002 - -+/* LOONGARCH64 relocs. */ -+#define R_LARCH_NONE 0 -+#define R_LARCH_32 1 -+#define R_LARCH_64 2 -+#define R_LARCH_RELATIVE 3 -+#define R_LARCH_COPY 4 -+#define R_LARCH_JUMP_SLOT 5 -+#define R_LARCH_TLS_DTPMOD32 6 -+#define R_LARCH_TLS_DTPMOD64 7 -+#define R_LARCH_TLS_DTPREL32 8 -+#define R_LARCH_TLS_DTPREL64 9 -+#define R_LARCH_TLS_TPREL32 10 -+#define R_LARCH_TLS_TPREL64 11 -+#define R_LARCH_IRELATIVE 12 -+#define R_LARCH_MARK_LA 20 -+#define R_LARCH_MARK_PCREL 21 -+#define R_LARCH_SOP_PUSH_PCREL 22 -+#define R_LARCH_SOP_PUSH_ABSOLUTE 23 -+#define R_LARCH_SOP_PUSH_DUP 24 -+#define R_LARCH_SOP_PUSH_GPREL 25 -+#define R_LARCH_SOP_PUSH_TLS_TPREL 26 -+#define R_LARCH_SOP_PUSH_TLS_GOT 27 -+#define R_LARCH_SOP_PUSH_TLS_GD 28 -+#define R_LARCH_SOP_PUSH_PLT_PCREL 29 -+#define R_LARCH_SOP_ASSERT 30 -+#define R_LARCH_SOP_NOT 31 -+#define R_LARCH_SOP_SUB 32 -+#define R_LARCH_SOP_SL 33 -+#define R_LARCH_SOP_SR 34 -+#define R_LARCH_SOP_ADD 35 -+#define R_LARCH_SOP_AND 36 -+#define R_LARCH_SOP_IF_ELSE 37 -+#define R_LARCH_SOP_POP_32_S_10_5 38 -+#define R_LARCH_SOP_POP_32_U_10_12 39 -+#define R_LARCH_SOP_POP_32_S_10_12 40 -+#define R_LARCH_SOP_POP_32_S_10_16 41 -+#define R_LARCH_SOP_POP_32_S_10_16_S2 42 -+#define R_LARCH_SOP_POP_32_S_5_20 43 -+#define R_LARCH_SOP_POP_32_S_0_5_10_16_S2 44 -+#define R_LARCH_SOP_POP_32_S_0_10_10_16_S2 45 -+#define R_LARCH_SOP_POP_32_U 46 -+#define R_LARCH_ADD8 47 -+#define R_LARCH_ADD16 48 -+#define R_LARCH_ADD24 49 -+#define R_LARCH_ADD32 50 -+#define R_LARCH_ADD64 51 -+#define R_LARCH_SUB8 52 -+#define R_LARCH_SUB16 53 -+#define R_LARCH_SUB24 54 -+#define R_LARCH_SUB32 55 -+#define R_LARCH_SUB64 56 -+#define R_LARCH_GNU_VTINHERIT 57 -+#define R_LARCH_GNU_VTENTRY 58 -+ - /* MIPS relocs. */ - - #define R_MIPS_NONE 0 /* No reloc */ -diff --git a/include/grub/fdt.h b/include/grub/fdt.h -index 6ee57e1..a0710ab 100644 ---- a/include/grub/fdt.h -+++ b/include/grub/fdt.h -@@ -19,11 +19,14 @@ - #ifndef GRUB_FDT_HEADER - #define GRUB_FDT_HEADER 1 - --#if defined(__arm__) || defined(__aarch64__) -+#if defined(__arm__) || defined(__aarch64__) || defined (__loongarch__) - - #include - #include - -+/* Space required when preparing the /chosen node after boot has been called. */ -+#define GRUB_EFI_LINUX_FDT_EXTRA_SPACE 0x400 -+ - #define FDT_MAGIC 0xD00DFEED - - typedef struct { -diff --git a/include/grub/loongarch64/asm.h b/include/grub/loongarch64/asm.h -new file mode 100644 -index 0000000..c3e77e9 ---- /dev/null -+++ b/include/grub/loongarch64/asm.h -@@ -0,0 +1,10 @@ -+#ifndef GRUB_LOONGARCH64_ASM_HEADER -+#define GRUB_LOONGARCH64_ASM_HEADER 1 -+ -+#define GRUB_ASM_T4 $a4 -+#define GRUB_ASM_T5 $a5 -+#define GRUB_ASM_SZREG 8 -+#define GRUB_ASM_REG_S st.d -+#define GRUB_ASM_REG_L ld.d -+ -+#endif -diff --git a/include/grub/loongarch64/efi/loongson.h b/include/grub/loongarch64/efi/loongson.h -new file mode 100644 -index 0000000..7a9ccb4 ---- /dev/null -+++ b/include/grub/loongarch64/efi/loongson.h -@@ -0,0 +1,113 @@ -+/* -+ * GRUB -- GRand Unified Bootloader -+ * Copyright (C) 2017 Free Software Foundation, Inc. -+ * -+ * GRUB is free software: you can redistribute it and/or modify -+ * it under the terms of the GNU General Public License as published by -+ * the Free Software Foundation, either version 3 of the License, or -+ * (at your option) any later version. -+ * -+ * GRUB is distributed in the hope that it will be useful, -+ * but WITHOUT ANY WARRANTY; without even the implied warranty of -+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -+ * GNU General Public License for more details. -+ * -+ * You should have received a copy of the GNU General Public License -+ * along with GRUB. If not, see . -+ */ -+ -+#ifndef GRUB_EFI_LOONGSON_HEADER -+#define GRUB_EFI_LOONGSON_HEADER 1 -+ -+#include -+ -+#include -+ -+#define GRUB_EFI_LOONGSON_BPI_TABLE_GUID \ -+ { 0x4660f721, 0x2ec5, 0x416a, \ -+ { 0x89, 0x9a, 0x43, 0x18, 0x02, 0x50, 0xa0, 0xc9 } \ -+ } -+ -+#define GRUB_EFI_MAX_PHY_ADDRESS 0xffffffffffffULL -+#define ELF32_LOADMASK (0xf0000000UL) -+#define ELF64_LOADMASK (0xf000000000000000ULL) -+#define FLAGS_EFI_SUPPORT_BIT 0 -+#define GRUB_EFI_LOONGSON_MMAP_MAX 128 -+ -+typedef enum -+ { -+ GRUB_EFI_LOONGSON_SYSTEM_RAM = 1, -+ GRUB_EFI_LOONGSON_MEMORY_RESERVED, -+ GRUB_EFI_LOONGSON_ACPI_TABLE, -+ GRUB_EFI_LOONGSON_ACPI_NVS, -+ GRUB_EFI_LOONGSON_MAX_MEMORY_TYPE -+ } -+grub_efi_loongson_memory_type; -+ -+typedef enum -+ { -+ GRUB_EFI_BPI_VER_NONE = 0, -+ GRUB_EFI_BPI_VER_V1 = 1000, -+ GRUB_EFI_BPI_VER_V2 = 1001, -+ } -+ grub_efi_loongson_bpi_version; -+ -+grub_uint8_t -+EXPORT_FUNC(grub_efi_loongson_calculatesum8) (const grub_uint8_t *Buffer, -+ grub_efi_uintn_t Length); -+ -+grub_uint8_t -+EXPORT_FUNC(grub_efi_loongson_grub_calculatechecksum8) (const grub_uint8_t *Buffer, -+ grub_efi_uintn_t Length); -+ -+unsigned long -+EXPORT_FUNC(grub_efi_get_bpi_version) (const char *str); -+ -+void * -+EXPORT_FUNC(grub_efi_loongson_get_boot_params) (void); -+ -+typedef struct _extention_list_hdr { -+ grub_uint64_t signature; -+ grub_uint32_t length; -+ grub_uint8_t revision; -+ grub_uint8_t checksum; -+ union { -+ struct _extention_list_hdr *next; -+ grub_uint64_t next_offset; -+ }; -+}GRUB_PACKED -+ext_list; -+ -+typedef struct boot_params_interface { -+ grub_uint64_t signature; //{'B', 'P', 'I', 'x', 'x', 'x', 'x', 'x'} -+ grub_efi_system_table_t *systemtable; -+ union { -+ ext_list *extlist; -+ grub_uint64_t extlist_offset; -+ }; -+ grub_uint64_t flags; -+}GRUB_PACKED -+boot_params_interface; -+ -+typedef struct { -+ ext_list header; //{MEM} -+ grub_uint8_t mapcount; -+ struct GRUB_PACKED memmap_v1 { -+ grub_uint32_t memtype; -+ grub_uint64_t memstart; -+ grub_uint64_t memsize; -+ } map[GRUB_EFI_LOONGSON_MMAP_MAX]; -+}GRUB_PACKED -+mem_map_v1; -+ -+typedef struct { -+ ext_list header; //{VBIOS} -+ grub_uint64_t vbiosaddr; -+}GRUB_PACKED -+vbios; -+ -+grub_uint32_t -+EXPORT_FUNC (grub_efi_loongson_memmap_v1_sort) (struct memmap_v1 array[], -+ grub_uint32_t length, mem_map_v1 * mem, -+ grub_uint32_t index, grub_uint32_t memtype); -+#endif /* ! GRUB_EFI_LOONGSON_HEADER */ -diff --git a/include/grub/loongarch64/efi/memory.h b/include/grub/loongarch64/efi/memory.h -new file mode 100644 -index 0000000..73641a1 ---- /dev/null -+++ b/include/grub/loongarch64/efi/memory.h -@@ -0,0 +1,12 @@ -+#ifndef GRUB_MEMORY_CPU_HEADER -+#include -+ -+static inline grub_uint64_t -+grub_efi_max_usable_address(void) -+{ -+ grub_uint64_t addr; -+ asm volatile ("csrrd %0, 0x181" : "=r" (addr)); -+ return addr |= 0xffffffffffUL; -+} -+ -+#endif /* ! GRUB_MEMORY_CPU_HEADER */ -diff --git a/include/grub/loongarch64/efi/time.h b/include/grub/loongarch64/efi/time.h -new file mode 100644 -index 0000000..e69de29 -diff --git a/include/grub/loongarch64/io.h b/include/grub/loongarch64/io.h -new file mode 100644 -index 0000000..5f34103 ---- /dev/null -+++ b/include/grub/loongarch64/io.h -@@ -0,0 +1,62 @@ -+/* -+ * GRUB -- GRand Unified Bootloader -+ * Copyright (C) 2009,2017 Free Software Foundation, Inc. -+ * -+ * GRUB is free software: you can redistribute it and/or modify -+ * it under the terms of the GNU General Public License as published by -+ * the Free Software Foundation, either version 3 of the License, or -+ * (at your option) any later version. -+ * -+ * GRUB is distributed in the hope that it will be useful, -+ * but WITHOUT ANY WARRANTY; without even the implied warranty of -+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -+ * GNU General Public License for more details. -+ * -+ * You should have received a copy of the GNU General Public License -+ * along with GRUB. If not, see . -+ */ -+ -+#ifndef GRUB_IO_H -+#define GRUB_IO_H 1 -+ -+#include -+ -+typedef grub_addr_t grub_port_t; -+ -+static __inline unsigned char -+grub_inb (grub_port_t port) -+{ -+ return *(volatile grub_uint8_t *) port; -+} -+ -+static __inline unsigned short int -+grub_inw (grub_port_t port) -+{ -+ return *(volatile grub_uint16_t *) port; -+} -+ -+static __inline unsigned int -+grub_inl (grub_port_t port) -+{ -+ return *(volatile grub_uint32_t *) port; -+} -+ -+static __inline void -+grub_outb (unsigned char value, grub_port_t port) -+{ -+ *(volatile grub_uint8_t *) port = value; -+} -+ -+static __inline void -+grub_outw (unsigned short int value, grub_port_t port) -+{ -+ *(volatile grub_uint16_t *) port = value; -+} -+ -+static __inline void -+grub_outl (unsigned int value, grub_port_t port) -+{ -+ *(volatile grub_uint32_t *) port = value; -+} -+ -+#endif /* _SYS_IO_H */ -diff --git a/include/grub/loongarch64/linux.h b/include/grub/loongarch64/linux.h -new file mode 100644 -index 0000000..3c6cf65 ---- /dev/null -+++ b/include/grub/loongarch64/linux.h -@@ -0,0 +1,74 @@ -+/* -+ * GRUB -- GRand Unified Bootloader -+ * Copyright (C) 2021 Free Software Foundation, Inc. -+ * -+ * GRUB is free software: you can redistribute it and/or modify -+ * it under the terms of the GNU General Public License as published by -+ * the Free Software Foundation, either version 3 of the License, or -+ * (at your option) any later version. -+ * -+ * GRUB is distributed in the hope that it will be useful, -+ * but WITHOUT ANY WARRANTY; without even the implied warranty of -+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -+ * GNU General Public License for more details. -+ * -+ * You should have received a copy of the GNU General Public License -+ * along with GRUB. If not, see . -+ */ -+ -+#ifndef GRUB_LOONGARCH64_LINUX_HEADER -+#define GRUB_LOONGARCH64_LINUX_HEADER 1 -+ -+#include -+ -+/* LoongArch linux kernel type */ -+#define GRUB_LOONGARCH_LINUX_BAD 0 -+#define GRUB_LOONGARCH_LINUX_ELF 1 -+#define GRUB_LOONGARCH_LINUX_EFI 2 -+ -+#define GRUB_LOONGSON3_BOOT_MEM_MAP_MAX 128 -+ -+#define GRUB_LINUX_LOONGARCH_MAGIC_SIGNATURE 0x4C6F6F6E67417263 /* 'LoongArc' */ -+#define GRUB_LINUX_LOONGARCH_MAGIC_SIGNATURE2 0x68 /* 'h' */ -+#define linux_arch_kernel_header linux_loongarch64_kernel_header -+ -+/* From linux/Documentation/loongarch/booting.txt -+ * -+ * 0-1: MZ -+ * 0x28: LoongArch\0 -+ * 0x3c: PE/COFF头偏移 -+ * 0x20e:内核版本号偏移-512 -+ * riscv的version字段在0x20偏移处,现在LoongArch没有使用,是0 -+ */ -+struct linux_loongarch64_kernel_header -+{ -+ grub_uint32_t code0; /* Executable code */ -+ grub_uint32_t code1; /* Executable code */ -+ grub_uint64_t text_offset; /* Image load offset */ -+ grub_uint64_t res0; /* reserved */ -+ grub_uint64_t res1; /* reserved */ -+ grub_uint64_t res2; /* reserved */ -+ grub_uint64_t magic; /* Magic number, little endian, "LoongArc" */ -+ grub_uint32_t magic1; /* Magic number, little endian, "h" */ -+ grub_uint64_t res3; /* reserved */ -+ grub_uint32_t hdr_offset; /* Offset of PE/COFF header */ -+}; -+ -+struct linux_loongarch64_kernel_params -+{ -+ grub_addr_t kernel_addr; /* kernel entry address */ -+ grub_size_t kernel_size; /* kernel size */ -+ grub_addr_t ramdisk_addr; /* initrd load address */ -+ grub_size_t ramdisk_size; /* initrd size */ -+ int linux_argc; -+ grub_addr_t linux_argv; -+ void* linux_args; -+}; -+ -+#include -+#include -+ -+#define ELF32_LOADMASK (0xf0000000UL) -+#define ELF64_LOADMASK (0xf000000000000000ULL) -+ -+#endif /* ! GRUB_LOONGARCH64_LINUX_HEADER */ -diff --git a/include/grub/loongarch64/loongarch64.h b/include/grub/loongarch64/loongarch64.h -new file mode 100644 -index 0000000..ea3be3d ---- /dev/null -+++ b/include/grub/loongarch64/loongarch64.h -@@ -0,0 +1,30 @@ -+/* -+ * GRUB -- GRand Unified Bootloader -+ * Copyright (C) 2010,2017 Free Software Foundation, Inc. -+ * -+ * GRUB is free software: you can redistribute it and/or modify -+ * it under the terms of the GNU General Public License as published by -+ * the Free Software Foundation, either version 3 of the License, or -+ * (at your option) any later version. -+ * -+ * GRUB is distributed in the hope that it will be useful, -+ * but WITHOUT ANY WARRANTY; without even the implied warranty of -+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -+ * GNU General Public License for more details. -+ * -+ * You should have received a copy of the GNU General Public License -+ * along with GRUB. If not, see . -+ */ -+ -+#ifndef GRUB_REGISTERS_CPU_HEADER -+#define GRUB_REGISTERS_CPU_HEADER 1 -+ -+#ifdef ASM_FILE -+#define GRUB_CPU_REGISTER_WRAP(x) x -+#else -+#define GRUB_CPU_REGISTER_WRAP(x) #x -+#endif -+ -+#define GRUB_CPU_LOONGARCH_COP0_TIMER_COUNT GRUB_CPU_REGISTER_WRAP(9) -+ -+#endif -diff --git a/include/grub/loongarch64/memory.h b/include/grub/loongarch64/memory.h -new file mode 100644 -index 0000000..8281dab ---- /dev/null -+++ b/include/grub/loongarch64/memory.h -@@ -0,0 +1,60 @@ -+/* -+ * GRUB -- GRand Unified Bootloader -+ * Copyright (C) 2017 Free Software Foundation, Inc. -+ * -+ * GRUB is free software: you can redistribute it and/or modify -+ * it under the terms of the GNU General Public License as published by -+ * the Free Software Foundation, either version 3 of the License, or -+ * (at your option) any later version. -+ * -+ * GRUB is distributed in the hope that it will be useful, -+ * but WITHOUT ANY WARRANTY; without even the implied warranty of -+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -+ * GNU General Public License for more details. -+ * -+ * You should have received a copy of the GNU General Public License -+ * along with GRUB. If not, see . -+ */ -+ -+#ifndef GRUB_MEMORY_CPU_HEADER -+#define GRUB_MEMORY_CPU_HEADER 1 -+ -+#ifndef ASM_FILE -+#include -+#include -+#include -+#endif -+ -+#ifndef ASM_FILE -+ -+typedef grub_addr_t grub_phys_addr_t; -+ -+static inline grub_phys_addr_t -+grub_vtop (void *a) -+{ -+ if (-1 == ((grub_int64_t) a >> 32)) -+ return ((grub_phys_addr_t) a) & 0x1fffffffUL; -+ return ((grub_phys_addr_t) a) & 0xffffffffffffUL; -+} -+ -+static inline void * -+grub_map_memory (grub_phys_addr_t a, -+ grub_size_t size __attribute__ ((unused))) -+{ -+ grub_uint64_t addr; -+ asm volatile ("csrrd %0, 0x181" : "=r" (addr)); -+ if (addr & 0x1) -+ return (void *) (a | (addr & 0xffffffffffffff00UL)); -+ else -+ return (void *) a; -+} -+ -+static inline void -+grub_unmap_memory (void *a __attribute__ ((unused)), -+ grub_size_t size __attribute__ ((unused))) -+{ -+} -+ -+#endif -+ -+#endif -diff --git a/include/grub/loongarch64/relocator.h b/include/grub/loongarch64/relocator.h -new file mode 100644 -index 0000000..8815314 ---- /dev/null -+++ b/include/grub/loongarch64/relocator.h -@@ -0,0 +1,38 @@ -+/* -+ * GRUB -- GRand Unified Bootloader -+ * Copyright (C) 2017 Free Software Foundation, Inc. -+ * -+ * GRUB is free software: you can redistribute it and/or modify -+ * it under the terms of the GNU General Public License as published by -+ * the Free Software Foundation, either version 3 of the License, or -+ * (at your option) any later version. -+ * -+ * GRUB is distributed in the hope that it will be useful, -+ * but WITHOUT ANY WARRANTY; without even the implied warranty of -+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -+ * GNU General Public License for more details. -+ * -+ * You should have received a copy of the GNU General Public License -+ * along with GRUB. If not, see . -+ */ -+ -+#ifndef GRUB_RELOCATOR_CPU_HEADER -+#define GRUB_RELOCATOR_CPU_HEADER 1 -+ -+#include -+#include -+#include -+ -+struct grub_relocator64_state -+{ -+ /* gpr[0] is ignored since it's hardwired to 0. */ -+ grub_uint64_t gpr[32]; -+ /* Register holding target $pc. */ -+ int jumpreg; -+}; -+ -+grub_err_t -+grub_relocator64_boot (struct grub_relocator *rel, -+ struct grub_relocator64_state state); -+ -+#endif /* ! GRUB_RELOCATOR_CPU_HEADER */ -diff --git a/include/grub/loongarch64/setjmp.h b/include/grub/loongarch64/setjmp.h -new file mode 100644 -index 0000000..d9a0776 ---- /dev/null -+++ b/include/grub/loongarch64/setjmp.h -@@ -0,0 +1,27 @@ -+/* -+ * GRUB -- GRand Unified Bootloader -+ * Copyright (C) 2002,2004,2006,2007,2009,2017 Free Software Foundation, Inc. -+ * -+ * GRUB is free software: you can redistribute it and/or modify -+ * it under the terms of the GNU General Public License as published by -+ * the Free Software Foundation, either version 3 of the License, or -+ * (at your option) any later version. -+ * -+ * GRUB is distributed in the hope that it will be useful, -+ * but WITHOUT ANY WARRANTY; without even the implied warranty of -+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -+ * GNU General Public License for more details. -+ * -+ * You should have received a copy of the GNU General Public License -+ * along with GRUB. If not, see . -+ */ -+ -+#ifndef GRUB_SETJMP_CPU_HEADER -+#define GRUB_SETJMP_CPU_HEADER 1 -+ -+typedef grub_uint64_t grub_jmp_buf[12]; -+ -+int grub_setjmp (grub_jmp_buf env) RETURNS_TWICE; -+void grub_longjmp (grub_jmp_buf env, int val) __attribute__ ((noreturn)); -+ -+#endif /* ! GRUB_SETJMP_CPU_HEADER */ -diff --git a/include/grub/loongarch64/time.h b/include/grub/loongarch64/time.h -new file mode 100644 -index 0000000..c9a7334 ---- /dev/null -+++ b/include/grub/loongarch64/time.h -@@ -0,0 +1,39 @@ -+/* -+ * GRUB -- GRand Unified Bootloader -+ * Copyright (C) 2003,2004,2005,2007,2017 Free Software Foundation, Inc. -+ * -+ * GRUB is free software: you can redistribute it and/or modify -+ * it under the terms of the GNU General Public License as published by -+ * the Free Software Foundation, either version 3 of the License, or -+ * (at your option) any later version. -+ * -+ * GRUB is distributed in the hope that it will be useful, -+ * but WITHOUT ANY WARRANTY; without even the implied warranty of -+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -+ * GNU General Public License for more details. -+ * -+ * You should have received a copy of the GNU General Public License -+ * along with GRUB. If not, see . -+ */ -+ -+#ifndef KERNEL_CPU_TIME_HEADER -+#define KERNEL_CPU_TIME_HEADER 1 -+ -+#ifndef GRUB_UTIL -+ -+#define GRUB_TICKS_PER_SECOND (grub_arch_cpuclock / 2) -+ -+void grub_timer_init (grub_uint32_t cpuclock); -+ -+/* Return the real time in ticks. */ -+grub_uint64_t grub_get_rtc (void); -+ -+extern grub_uint32_t grub_arch_cpuclock; -+#endif -+ -+static inline void -+grub_cpu_idle(void) -+{ -+} -+ -+#endif -diff --git a/include/grub/loongarch64/types.h b/include/grub/loongarch64/types.h -new file mode 100644 -index 0000000..5dc7f21 ---- /dev/null -+++ b/include/grub/loongarch64/types.h -@@ -0,0 +1,34 @@ -+/* -+ * GRUB -- GRand Unified Bootloader -+ * Copyright (C) 2002,2006,2007,2009,2017 Free Software Foundation, Inc. -+ * -+ * GRUB is free software: you can redistribute it and/or modify -+ * it under the terms of the GNU General Public License as published by -+ * the Free Software Foundation, either version 3 of the License, or -+ * (at your option) any later version. -+ * -+ * GRUB is distributed in the hope that it will be useful, -+ * but WITHOUT ANY WARRANTY; without even the implied warranty of -+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -+ * GNU General Public License for more details. -+ * -+ * You should have received a copy of the GNU General Public License -+ * along with GRUB. If not, see . -+ */ -+ -+#ifndef GRUB_TYPES_CPU_HEADER -+#define GRUB_TYPES_CPU_HEADER 1 -+ -+/* The size of void *. */ -+#define GRUB_TARGET_SIZEOF_VOID_P 8 -+ -+/* The size of long. */ -+#define GRUB_TARGET_SIZEOF_LONG 8 -+ -+#ifdef GRUB_CPU_LOONGARCH -+/* loongarch is little-endian. */ -+#undef GRUB_TARGET_WORDS_BIGENDIAN -+ -+#endif /* ! GRUB_TYPES_CPU_HEADER */ -+ -+#endif -diff --git a/include/grub/util/install.h b/include/grub/util/install.h -index dad1756..33feae9 100644 ---- a/include/grub/util/install.h -+++ b/include/grub/util/install.h -@@ -107,6 +107,7 @@ enum grub_install_plat - GRUB_INSTALL_PLATFORM_X86_64_XEN, - GRUB_INSTALL_PLATFORM_ARM64_EFI, - GRUB_INSTALL_PLATFORM_ARM_COREBOOT, -+ GRUB_INSTALL_PLATFORM_LOONGARCH64_EFI, - GRUB_INSTALL_PLATFORM_MAX - }; - -diff --git a/util/grub-install-common.c b/util/grub-install-common.c -index fde4ca7..6504637 100644 ---- a/util/grub-install-common.c -+++ b/util/grub-install-common.c -@@ -738,27 +738,28 @@ static struct - const char *platform; - } platforms[GRUB_INSTALL_PLATFORM_MAX] = - { -- [GRUB_INSTALL_PLATFORM_I386_PC] = { "i386", "pc" }, -- [GRUB_INSTALL_PLATFORM_I386_EFI] = { "i386", "efi" }, -- [GRUB_INSTALL_PLATFORM_I386_QEMU] = { "i386", "qemu" }, -- [GRUB_INSTALL_PLATFORM_I386_COREBOOT] = { "i386", "coreboot" }, -- [GRUB_INSTALL_PLATFORM_I386_MULTIBOOT] = { "i386", "multiboot" }, -- [GRUB_INSTALL_PLATFORM_I386_IEEE1275] = { "i386", "ieee1275" }, -- [GRUB_INSTALL_PLATFORM_X86_64_EFI] = { "x86_64", "efi" }, -- [GRUB_INSTALL_PLATFORM_I386_XEN] = { "i386", "xen" }, -- [GRUB_INSTALL_PLATFORM_X86_64_XEN] = { "x86_64", "xen" }, -- [GRUB_INSTALL_PLATFORM_MIPSEL_LOONGSON] = { "mipsel", "loongson" }, -- [GRUB_INSTALL_PLATFORM_MIPSEL_QEMU_MIPS] = { "mipsel", "qemu_mips" }, -- [GRUB_INSTALL_PLATFORM_MIPS_QEMU_MIPS] = { "mips", "qemu_mips" }, -- [GRUB_INSTALL_PLATFORM_MIPSEL_ARC] = { "mipsel", "arc" }, -- [GRUB_INSTALL_PLATFORM_MIPS_ARC] = { "mips", "arc" }, -- [GRUB_INSTALL_PLATFORM_SPARC64_IEEE1275] = { "sparc64", "ieee1275" }, -- [GRUB_INSTALL_PLATFORM_POWERPC_IEEE1275] = { "powerpc", "ieee1275" }, -- [GRUB_INSTALL_PLATFORM_IA64_EFI] = { "ia64", "efi" }, -- [GRUB_INSTALL_PLATFORM_ARM_EFI] = { "arm", "efi" }, -- [GRUB_INSTALL_PLATFORM_ARM64_EFI] = { "arm64", "efi" }, -- [GRUB_INSTALL_PLATFORM_ARM_UBOOT] = { "arm", "uboot" }, -- [GRUB_INSTALL_PLATFORM_ARM_COREBOOT] = { "arm", "coreboot" }, -+ [GRUB_INSTALL_PLATFORM_I386_PC] = { "i386", "pc" }, -+ [GRUB_INSTALL_PLATFORM_I386_EFI] = { "i386", "efi" }, -+ [GRUB_INSTALL_PLATFORM_I386_QEMU] = { "i386", "qemu" }, -+ [GRUB_INSTALL_PLATFORM_I386_COREBOOT] = { "i386", "coreboot" }, -+ [GRUB_INSTALL_PLATFORM_I386_MULTIBOOT] = { "i386", "multiboot" }, -+ [GRUB_INSTALL_PLATFORM_I386_IEEE1275] = { "i386", "ieee1275" }, -+ [GRUB_INSTALL_PLATFORM_X86_64_EFI] = { "x86_64", "efi" }, -+ [GRUB_INSTALL_PLATFORM_I386_XEN] = { "i386", "xen" }, -+ [GRUB_INSTALL_PLATFORM_X86_64_XEN] = { "x86_64", "xen" }, -+ [GRUB_INSTALL_PLATFORM_MIPSEL_LOONGSON] = { "mipsel", "loongson" }, -+ [GRUB_INSTALL_PLATFORM_MIPSEL_QEMU_MIPS] = { "mipsel", "qemu_mips" }, -+ [GRUB_INSTALL_PLATFORM_MIPS_QEMU_MIPS] = { "mips", "qemu_mips" }, -+ [GRUB_INSTALL_PLATFORM_MIPSEL_ARC] = { "mipsel", "arc" }, -+ [GRUB_INSTALL_PLATFORM_MIPS_ARC] = { "mips", "arc" }, -+ [GRUB_INSTALL_PLATFORM_SPARC64_IEEE1275] = { "sparc64", "ieee1275" }, -+ [GRUB_INSTALL_PLATFORM_POWERPC_IEEE1275] = { "powerpc", "ieee1275" }, -+ [GRUB_INSTALL_PLATFORM_IA64_EFI] = { "ia64", "efi" }, -+ [GRUB_INSTALL_PLATFORM_ARM_EFI] = { "arm", "efi" }, -+ [GRUB_INSTALL_PLATFORM_ARM64_EFI] = { "arm64", "efi" }, -+ [GRUB_INSTALL_PLATFORM_ARM_UBOOT] = { "arm", "uboot" }, -+ [GRUB_INSTALL_PLATFORM_ARM_COREBOOT] = { "arm", "coreboot" }, -+ [GRUB_INSTALL_PLATFORM_LOONGARCH64_EFI] = { "loongarch64", "efi" }, - }; - - char * -diff --git a/util/grub-install.c b/util/grub-install.c -index 65bb2f9..28b5d74 100644 ---- a/util/grub-install.c -+++ b/util/grub-install.c -@@ -322,6 +322,8 @@ get_default_platform (void) - return "arm-uboot"; - #elif defined (__aarch64__) - return "arm64-efi"; -+#elif defined (__loongarch64) -+ return "loongarch64-efi"; - #elif defined (__amd64__) || defined (__x86_64__) || defined (__i386__) - return grub_install_get_default_x86_platform (); - #else -@@ -477,6 +479,7 @@ have_bootdev (enum grub_install_plat pl) - case GRUB_INSTALL_PLATFORM_IA64_EFI: - case GRUB_INSTALL_PLATFORM_ARM_EFI: - case GRUB_INSTALL_PLATFORM_ARM64_EFI: -+ case GRUB_INSTALL_PLATFORM_LOONGARCH64_EFI: - case GRUB_INSTALL_PLATFORM_I386_IEEE1275: - case GRUB_INSTALL_PLATFORM_SPARC64_IEEE1275: - case GRUB_INSTALL_PLATFORM_POWERPC_IEEE1275: -@@ -895,6 +898,7 @@ main (int argc, char *argv[]) - case GRUB_INSTALL_PLATFORM_I386_EFI: - case GRUB_INSTALL_PLATFORM_IA64_EFI: - case GRUB_INSTALL_PLATFORM_X86_64_EFI: -+ case GRUB_INSTALL_PLATFORM_LOONGARCH64_EFI: - is_efi = 1; - grub_util_error (_("this utility cannot be used for EFI platforms" - " because it does not support UEFI Secure Boot")); -@@ -921,6 +925,7 @@ main (int argc, char *argv[]) - case GRUB_INSTALL_PLATFORM_ARM_EFI: - case GRUB_INSTALL_PLATFORM_ARM64_EFI: - case GRUB_INSTALL_PLATFORM_IA64_EFI: -+ case GRUB_INSTALL_PLATFORM_LOONGARCH64_EFI: - case GRUB_INSTALL_PLATFORM_I386_IEEE1275: - case GRUB_INSTALL_PLATFORM_SPARC64_IEEE1275: - case GRUB_INSTALL_PLATFORM_POWERPC_IEEE1275: -@@ -965,6 +970,7 @@ main (int argc, char *argv[]) - case GRUB_INSTALL_PLATFORM_ARM_EFI: - case GRUB_INSTALL_PLATFORM_ARM64_EFI: - case GRUB_INSTALL_PLATFORM_IA64_EFI: -+ case GRUB_INSTALL_PLATFORM_LOONGARCH64_EFI: - case GRUB_INSTALL_PLATFORM_I386_IEEE1275: - case GRUB_INSTALL_PLATFORM_ARM_UBOOT: - case GRUB_INSTALL_PLATFORM_I386_QEMU: -@@ -1110,6 +1116,9 @@ main (int argc, char *argv[]) - case GRUB_INSTALL_PLATFORM_ARM64_EFI: - efi_file = "BOOTAA64.EFI"; - break; -+ case GRUB_INSTALL_PLATFORM_LOONGARCH64_EFI: -+ efi_file = "BOOTLOONGARCH64.EFI"; -+ break; - default: - grub_util_error ("%s", _("You've found a bug")); - break; -@@ -1137,6 +1146,9 @@ main (int argc, char *argv[]) - case GRUB_INSTALL_PLATFORM_ARM64_EFI: - efi_file = "grubaa64.efi"; - break; -+ case GRUB_INSTALL_PLATFORM_LOONGARCH64_EFI: -+ efi_file = "grubloongarch64.efi"; -+ break; - default: - efi_file = "grub.efi"; - break; -@@ -1440,6 +1452,7 @@ main (int argc, char *argv[]) - case GRUB_INSTALL_PLATFORM_ARM_EFI: - case GRUB_INSTALL_PLATFORM_ARM64_EFI: - case GRUB_INSTALL_PLATFORM_IA64_EFI: -+ case GRUB_INSTALL_PLATFORM_LOONGARCH64_EFI: - g = grub_util_guess_efi_drive (*curdev); - break; - case GRUB_INSTALL_PLATFORM_SPARC64_IEEE1275: -@@ -1581,6 +1594,7 @@ main (int argc, char *argv[]) - case GRUB_INSTALL_PLATFORM_ARM_EFI: - case GRUB_INSTALL_PLATFORM_ARM64_EFI: - case GRUB_INSTALL_PLATFORM_IA64_EFI: -+ case GRUB_INSTALL_PLATFORM_LOONGARCH64_EFI: - core_name = "core.efi"; - snprintf (mkimage_target, sizeof (mkimage_target), - "%s-%s", -@@ -1683,6 +1697,7 @@ main (int argc, char *argv[]) - case GRUB_INSTALL_PLATFORM_ARM_EFI: - case GRUB_INSTALL_PLATFORM_ARM64_EFI: - case GRUB_INSTALL_PLATFORM_IA64_EFI: -+ case GRUB_INSTALL_PLATFORM_LOONGARCH64_EFI: - case GRUB_INSTALL_PLATFORM_MIPSEL_QEMU_MIPS: - case GRUB_INSTALL_PLATFORM_MIPS_QEMU_MIPS: - case GRUB_INSTALL_PLATFORM_I386_COREBOOT: -@@ -1917,6 +1932,7 @@ main (int argc, char *argv[]) - case GRUB_INSTALL_PLATFORM_ARM_EFI: - case GRUB_INSTALL_PLATFORM_ARM64_EFI: - case GRUB_INSTALL_PLATFORM_IA64_EFI: -+ case GRUB_INSTALL_PLATFORM_LOONGARCH64_EFI: - { - char *dst = grub_util_path_concat (2, efidir, efi_file); - grub_install_copy_file (imgfile, dst, 1); -diff --git a/util/grub-mkimagexx.c b/util/grub-mkimagexx.c -index 1bb5eb8..240e193 100644 ---- a/util/grub-mkimagexx.c -+++ b/util/grub-mkimagexx.c -@@ -783,6 +783,9 @@ SUFFIX (relocate_addrs) (Elf_Ehdr *e, struct section_metadata *smd, - struct grub_ia64_trampoline *tr = (void *) (pe_target + tramp_off); - grub_uint64_t *gpptr = (void *) (pe_target + got_off); - unsigned unmatched_adr_got_page = 0; -+ grub_uint64_t oprs[10240]= {0}; -+ int opri = -1; -+ grub_uint32_t la_abs = 0; - #define MASK19 ((1 << 19) - 1) - #else - grub_uint32_t *tr = (void *) (pe_target + tramp_off); -@@ -1122,6 +1125,173 @@ SUFFIX (relocate_addrs) (Elf_Ehdr *e, struct section_metadata *smd, - } - break; - } -+ case EM_LOONGARCH64: -+ { -+ sym_addr += addend; -+ switch (ELF_R_TYPE (info)) -+ { -+ case R_LARCH_64: -+ { -+ *target=(grub_uint64_t)sym_addr; -+ } -+ break; -+ case R_LARCH_MARK_LA: -+ { -+ la_abs=1; -+ } -+ break; -+ case R_LARCH_SOP_PUSH_PCREL: -+ { -+ opri++; -+ oprs[opri]=(grub_uint64_t)(sym_addr-(target_section_addr+offset+image_target->vaddr_offset)); -+ } -+ break; -+ case R_LARCH_SOP_PUSH_ABSOLUTE: -+ { -+ opri++; -+ oprs[opri]=(grub_uint64_t)sym_addr; -+ } -+ break; -+ case R_LARCH_SOP_PUSH_PLT_PCREL: -+ { -+ opri++; -+ oprs[opri]=(grub_uint64_t)(sym_addr-(target_section_addr+offset+image_target->vaddr_offset)); -+ } -+ break; -+ case R_LARCH_SOP_SUB: -+ { -+ grub_uint64_t opr2=oprs[opri]; -+ opri--; -+ grub_uint64_t opr1=oprs[opri]; -+ opri--; -+ opri++; -+ oprs[opri]=opr1 - opr2; -+ } -+ break; -+ case R_LARCH_SOP_SL: -+ { -+ grub_uint64_t opr2=oprs[opri]; -+ opri--; -+ grub_uint64_t opr1=oprs[opri]; -+ opri--; -+ opri++; -+ oprs[opri]=opr1 << opr2; -+ } -+ break; -+ case R_LARCH_SOP_SR: -+ { -+ grub_uint64_t opr2=oprs[opri]; -+ opri--; -+ grub_uint64_t opr1=oprs[opri]; -+ opri--; -+ opri++; -+ oprs[opri]=opr1 >> opr2; -+ } -+ break; -+ case R_LARCH_SOP_ADD: -+ { -+ grub_uint64_t opr2=oprs[opri]; -+ opri--; -+ grub_uint64_t opr1=oprs[opri]; -+ opri--; -+ opri++; -+ oprs[opri]=opr1 + opr2; -+ } -+ break; -+ case R_LARCH_SOP_AND: -+ { -+ grub_uint64_t opr2=oprs[opri]; -+ opri--; -+ grub_uint64_t opr1=oprs[opri]; -+ opri--; -+ opri++; -+ oprs[opri]=opr1 & opr2; -+ } -+ break; -+ case R_LARCH_SOP_IF_ELSE: -+ { -+ grub_uint64_t opr3=oprs[opri]; -+ opri--; -+ grub_uint64_t opr2=oprs[opri]; -+ opri--; -+ grub_uint64_t opr1=oprs[opri]; -+ opri--; -+ if(opr1){ -+ opri++; -+ oprs[opri]=opr2; -+ } else { -+ opri++; -+ oprs[opri]=opr3; -+ } -+ } -+ break; -+ case R_LARCH_SOP_POP_32_S_10_5: -+ { -+ grub_uint64_t opr1 = oprs[opri]; -+ opri--; -+ *target=(*target) | ((opr1 & 0x1f) << 10); -+ } -+ break; -+ case R_LARCH_SOP_POP_32_U_10_12: -+ { -+ grub_uint64_t opr1 = oprs[opri]; -+ opri--; -+ *target=(*target) | ((opr1 & 0xfff) << 10); -+ } -+ break; -+ case R_LARCH_SOP_POP_32_S_10_12: -+ { -+ if(la_abs==1) -+ la_abs=0; -+ grub_uint64_t opr1 = oprs[opri]; -+ opri--; -+ *target = (*target) | ((opr1 & 0xfff) << 10); -+ } -+ break; -+ case R_LARCH_SOP_POP_32_S_10_16: -+ { -+ grub_uint64_t opr1 = oprs[opri]; -+ opri--; -+ *target = (*target) | ((opr1 & 0xffff) << 10); -+ } -+ break; -+ case R_LARCH_SOP_POP_32_S_10_16_S2: -+ { -+ grub_uint64_t opr1 = oprs[opri]; -+ opri--; -+ *target = (*target) | (((opr1 >> 2) & 0xffff) << 10); -+ } -+ break; -+ case R_LARCH_SOP_POP_32_S_5_20: -+ { -+ grub_uint64_t opr1 = oprs[opri]; -+ opri--; -+ *target = (*target) | ((opr1 & 0xfffff)<<5) ; -+ } -+ break; -+ case R_LARCH_SOP_POP_32_S_0_5_10_16_S2: -+ { -+ grub_uint64_t opr1 = oprs[opri]; -+ opri--; -+ *target =(*target) | (((opr1 >> 2) & 0xffff) << 10); -+ *target =(*target) | ((opr1 >> 18) & 0x1f); -+ } -+ break; -+ case R_LARCH_SOP_POP_32_S_0_10_10_16_S2: -+ { -+ grub_uint64_t opr1 = oprs[opri]; -+ opri--; -+ *target =(*target) | (((opr1 >> 2) & 0xffff) << 10); -+ *target =(*target) | ((opr1 >> 18) & 0x3ff); -+ } -+ break; -+ default: -+ grub_util_error (_("relocation 0x%x is not implemented yet"), -+ (unsigned int) ELF_R_TYPE (info)); -+ break; -+ } -+ break; -+ } - #endif - #if defined(MKIMAGE_ELF32) - case EM_ARM: -@@ -1310,7 +1480,10 @@ add_fixup_entry (struct fixup_block_list **cblock, grub_uint16_t type, - - /* The spec does not mention the requirement of a Page RVA. - Here, align the address with a 4K boundary for safety. */ -- b->page_rva = (addr & ~(0x1000 - 1)); -+#ifdef GRUB_CPU_LOONGARCH64 -+ if (type) -+#endif -+ b->page_rva = (addr & ~(0x1000 - 1)); - b->block_size = sizeof (*b); - } - -@@ -1320,7 +1493,11 @@ add_fixup_entry (struct fixup_block_list **cblock, grub_uint16_t type, - - /* Add a new entry. */ - cur_index = ((b->block_size - sizeof (*b)) >> 1); -+#ifdef GRUB_CPU_LOONGARCH64 -+ entry = GRUB_PE32_FIXUP_ENTRY (type, type ? (addr - b->page_rva) : addr); -+#else - entry = GRUB_PE32_FIXUP_ENTRY (type, addr - b->page_rva); -+#endif - b->entries[cur_index] = grub_host_to_target16 (entry); - b->block_size += 2; - } -@@ -1366,6 +1543,10 @@ static void - translate_relocation_pe (struct translate_context *ctx, - Elf_Addr addr, - Elf_Addr info, -+#ifdef GRUB_CPU_LOONGARCH64 -+ Elf_Addr sym_addr, -+ Elf_Addr addend, -+#endif - const struct grub_install_image_target_desc *image_target) - { - /* Necessary to relocate only absolute addresses. */ -@@ -1477,6 +1658,133 @@ translate_relocation_pe (struct translate_context *ctx, - } - break; - break; -+ case EM_LOONGARCH64: -+ switch (ELF_R_TYPE (info)) -+ { -+ case R_LARCH_NONE: -+ break; -+ case R_LARCH_32: -+ break; -+ case R_LARCH_64: -+ { -+ ctx->current_address = add_fixup_entry ( -+ &ctx->lst, -+ GRUB_PE32_REL_BASED_DIR64, -+ addr, 0, ctx->current_address, -+ image_target); -+ } -+ break; -+ case R_LARCH_RELATIVE: -+ break; -+ case R_LARCH_COPY: -+ break; -+ case R_LARCH_JUMP_SLOT: -+ break; -+ case R_LARCH_TLS_DTPMOD32: -+ break; -+ case R_LARCH_TLS_DTPMOD64: -+ break; -+ case R_LARCH_TLS_DTPREL32: -+ break; -+ case R_LARCH_TLS_DTPREL64: -+ break; -+ case R_LARCH_TLS_TPREL32: -+ break; -+ case R_LARCH_TLS_TPREL64: -+ break; -+ case R_LARCH_IRELATIVE: -+ break; -+ case R_LARCH_MARK_LA: -+ { -+ ctx->current_address = add_fixup_entry ( -+ &ctx->lst, -+ GRUB_PE32_REL_BASED_LOONGARCH64, -+ addr, 0, ctx->current_address, -+ image_target); -+ } -+ break; -+ case R_LARCH_MARK_PCREL: -+ break; -+ case R_LARCH_SOP_PUSH_PCREL: -+ break; -+ case R_LARCH_SOP_PUSH_ABSOLUTE: -+ break; -+ case R_LARCH_SOP_PUSH_DUP: -+ break; -+ case R_LARCH_SOP_PUSH_GPREL: -+ break; -+ case R_LARCH_SOP_PUSH_TLS_TPREL: -+ break; -+ case R_LARCH_SOP_PUSH_TLS_GOT: -+ break; -+ case R_LARCH_SOP_PUSH_TLS_GD: -+ break; -+ case R_LARCH_SOP_PUSH_PLT_PCREL: -+ break; -+ case R_LARCH_SOP_ASSERT: -+ break; -+ case R_LARCH_SOP_NOT: -+ break; -+ case R_LARCH_SOP_SUB: -+ break; -+ case R_LARCH_SOP_SL: -+ break; -+ case R_LARCH_SOP_SR: -+ break; -+ case R_LARCH_SOP_ADD: -+ break; -+ case R_LARCH_SOP_AND: -+ break; -+ case R_LARCH_SOP_IF_ELSE: -+ break; -+ case R_LARCH_SOP_POP_32_S_10_5: -+ break; -+ case R_LARCH_SOP_POP_32_U_10_12: -+ break; -+ case R_LARCH_SOP_POP_32_S_10_12: -+ break; -+ case R_LARCH_SOP_POP_32_S_10_16: -+ break; -+ case R_LARCH_SOP_POP_32_S_10_16_S2: -+ break; -+ case R_LARCH_SOP_POP_32_S_5_20: -+ break; -+ case R_LARCH_SOP_POP_32_S_0_5_10_16_S2: -+ break; -+ case R_LARCH_SOP_POP_32_S_0_10_10_16_S2: -+ break; -+ case R_LARCH_SOP_POP_32_U: -+ break; -+ case R_LARCH_ADD8: -+ break; -+ case R_LARCH_ADD16: -+ break; -+ case R_LARCH_ADD24: -+ break; -+ case R_LARCH_ADD32: -+ break; -+ case R_LARCH_ADD64: -+ break; -+ case R_LARCH_SUB8: -+ break; -+ case R_LARCH_SUB16: -+ break; -+ case R_LARCH_SUB24: -+ break; -+ case R_LARCH_SUB32: -+ break; -+ case R_LARCH_SUB64: -+ break; -+ case R_LARCH_GNU_VTINHERIT: -+ break; -+ case R_LARCH_GNU_VTENTRY: -+ break; -+ default: -+ grub_util_error (_("relocation 0x%x is not implemented yet"), -+ (unsigned int) ELF_R_TYPE (info)); -+ break; -+ } -+ break; - #if defined(MKIMAGE_ELF32) - case EM_ARM: - switch (ELF_R_TYPE (info)) -@@ -1565,10 +1873,18 @@ static void - translate_relocation (struct translate_context *ctx, - Elf_Addr addr, - Elf_Addr info, -+#ifdef GRUB_CPU_LOONGARCH64 -+ Elf_Addr sym_addr, -+ Elf_Addr addend, -+#endif - const struct grub_install_image_target_desc *image_target) - { - if (image_target->id == IMAGE_EFI) -+#ifdef GRUB_CPU_LOONGARCH64 -+ translate_relocation_pe (ctx, addr, info, sym_addr, addend, image_target); -+#else - translate_relocation_pe (ctx, addr, info, image_target); -+#endif - else - translate_relocation_raw (ctx, addr, info, image_target); - } -@@ -1709,11 +2025,21 @@ make_reloc_section (Elf_Ehdr *e, struct grub_mkimage_layout *layout, - if ((grub_target_to_host32 (s->sh_type) == SHT_REL) || - (grub_target_to_host32 (s->sh_type) == SHT_RELA)) - { -+#ifdef GRUB_CPU_LOONGARCH64 -+ Elf_Rela *r; -+#else - Elf_Rel *r; -+#endif - Elf_Word rtab_size, r_size, num_rs; - Elf_Off rtab_offset; - Elf_Addr section_address; - Elf_Word j; -+#ifdef GRUB_CPU_LOONGARCH64 -+ Elf_Shdr *symtab_section; -+ -+ symtab_section = (Elf_Shdr *) ((char *) smd->sections -+ + (grub_target_to_host32 (s->sh_link) * smd->section_entsize)); -+#endif - - if (!SUFFIX (is_kept_reloc_section) (s, image_target, smd)) - { -@@ -1732,20 +2058,39 @@ make_reloc_section (Elf_Ehdr *e, struct grub_mkimage_layout *layout, - - section_address = smd->vaddrs[grub_le_to_cpu32 (s->sh_info)]; - -+#ifdef GRUB_CPU_LOONGARCH64 -+ for (j = 0, r = (Elf_Rela *) ((char *) e + rtab_offset); -+ j < num_rs; -+ j++, r = (Elf_Rela *) ((char *) r + r_size)) -+#else - for (j = 0, r = (Elf_Rel *) ((char *) e + rtab_offset); - j < num_rs; - j++, r = (Elf_Rel *) ((char *) r + r_size)) -+#endif - { - Elf_Addr info; - Elf_Addr offset; - Elf_Addr addr; -+#ifdef GRUB_CPU_LOONGARCH64 -+ Elf_Addr sym_addr; -+ Elf_Addr addend; -+#endif - - offset = grub_target_to_host (r->r_offset); - info = grub_target_to_host (r->r_info); -- -+#ifdef GRUB_CPU_LOONGARCH64 -+ sym_addr = SUFFIX (get_symbol_address) (e, symtab_section, -+ ELF_R_SYM (info), image_target); -+ addend = (s->sh_type == grub_target_to_host32 (SHT_RELA)) ? -+ grub_target_to_host (r->r_addend) : 0; -+#endif - addr = section_address + offset; - -+#ifdef GRUB_CPU_LOONGARCH64 -+ translate_relocation (&ctx, addr, info, sym_addr, addend, image_target); -+#else - translate_relocation (&ctx, addr, info, image_target); -+#endif - } - } - -diff --git a/util/grub-mknetdir.c b/util/grub-mknetdir.c -index ae31271..c998850 100644 ---- a/util/grub-mknetdir.c -+++ b/util/grub-mknetdir.c -@@ -112,7 +112,8 @@ static struct - [GRUB_INSTALL_PLATFORM_X86_64_EFI] = { "x86_64-efi", "efinet", ".efi" }, - [GRUB_INSTALL_PLATFORM_IA64_EFI] = { "ia64-efi", "efinet", ".efi" }, - [GRUB_INSTALL_PLATFORM_ARM_EFI] = { "arm-efi", "efinet", ".efi" }, -- [GRUB_INSTALL_PLATFORM_ARM64_EFI] = { "arm64-efi", "efinet", ".efi" } -+ [GRUB_INSTALL_PLATFORM_ARM64_EFI] = { "arm64-efi", "efinet", ".efi" }, -+ [GRUB_INSTALL_PLATFORM_LOONGARCH64_EFI] = { "loongarch64-efi", "efinet", ".efi" } - }; - - static void -diff --git a/util/grub-module-verifier.c b/util/grub-module-verifier.c -index 03ba1ab..4a9943a 100644 ---- a/util/grub-module-verifier.c -+++ b/util/grub-module-verifier.c -@@ -119,6 +119,52 @@ struct grub_module_verifier_arch archs[] = { - -1 - } - }, -+ { "loongarch64", 8, 0, EM_LOONGARCH64, GRUB_MODULE_VERIFY_SUPPORTS_REL | GRUB_MODULE_VERIFY_SUPPORTS_RELA, (int[]){ -+ R_LARCH_NONE, -+ R_LARCH_32, -+ R_LARCH_64, -+ R_LARCH_RELATIVE, -+ R_LARCH_COPY, -+ R_LARCH_JUMP_SLOT, -+ R_LARCH_TLS_DTPMOD32, -+ R_LARCH_TLS_DTPMOD64, -+ R_LARCH_TLS_DTPREL32, -+ R_LARCH_TLS_DTPREL64, -+ R_LARCH_TLS_TPREL32, -+ R_LARCH_TLS_TPREL64, -+ R_LARCH_IRELATIVE, -+ R_LARCH_MARK_LA, -+ R_LARCH_MARK_PCREL, -+ R_LARCH_SOP_PUSH_PCREL, -+ R_LARCH_SOP_PUSH_ABSOLUTE, -+ R_LARCH_SOP_PUSH_DUP, -+ R_LARCH_SOP_PUSH_GPREL, -+ R_LARCH_SOP_PUSH_TLS_TPREL, -+ R_LARCH_SOP_PUSH_TLS_GOT, -+ R_LARCH_SOP_PUSH_TLS_GD, -+ R_LARCH_SOP_PUSH_PLT_PCREL, -+ R_LARCH_SOP_ASSERT, -+ R_LARCH_SOP_NOT, -+ R_LARCH_SOP_SUB, -+ R_LARCH_SOP_SL, -+ R_LARCH_SOP_SR, -+ R_LARCH_SOP_ADD, -+ R_LARCH_SOP_AND, -+ R_LARCH_SOP_IF_ELSE, -+ R_LARCH_SOP_POP_32_S_10_5, -+ R_LARCH_SOP_POP_32_U_10_12, -+ R_LARCH_SOP_POP_32_S_10_12, -+ R_LARCH_SOP_POP_32_S_10_16, -+ R_LARCH_SOP_POP_32_S_10_16_S2, -+ R_LARCH_SOP_POP_32_S_5_20, -+ R_LARCH_SOP_POP_32_S_0_5_10_16_S2, -+ R_LARCH_SOP_POP_32_S_0_10_10_16_S2, -+ R_LARCH_SOP_POP_32_U, -+ -1 -+ }, (int[]){ -+ -1 -+ } -+ }, - }; - - struct platform_whitelist { -diff --git a/util/mkimage.c b/util/mkimage.c -index c770259..250ca81 100644 ---- a/util/mkimage.c -+++ b/util/mkimage.c -@@ -609,6 +609,22 @@ static const struct grub_install_image_target_desc image_targets[] = - .pe_target = GRUB_PE32_MACHINE_ARM64, - .elf_target = EM_AARCH64, - }, -+ { -+ .dirname = "loongarch64-efi", -+ .names = { "loongarch64-efi", NULL }, -+ .voidp_sizeof = 8, -+ .bigendian = 0, -+ .id = IMAGE_EFI, -+ .flags = PLATFORM_FLAGS_NONE, -+ .total_module_size = TARGET_NO_FIELD, -+ .decompressor_compressed_size = TARGET_NO_FIELD, -+ .decompressor_uncompressed_size = TARGET_NO_FIELD, -+ .decompressor_uncompressed_addr = TARGET_NO_FIELD, -+ .section_align = GRUB_PE32_SECTION_ALIGNMENT, -+ .vaddr_offset = EFI64_HEADER_SIZE, -+ .pe_target = GRUB_PE32_MACHINE_LOONGARCH64, -+ .elf_target = EM_LOONGARCH64, -+ }, - }; - - #include --- -2.1.0 - diff --git a/1003-cryptodisk-make-the-password-getter-and-additional-a.patch b/1003-cryptodisk-make-the-password-getter-and-additional-a.patch deleted file mode 100644 index bf6e451..0000000 --- a/1003-cryptodisk-make-the-password-getter-and-additional-a.patch +++ /dev/null @@ -1,173 +0,0 @@ -From 6503036b4adfcb078dc33b23a61969d914f7449b Mon Sep 17 00:00:00 2001 -From: James Bottomley -Date: Thu, 31 Dec 2020 09:36:16 -0800 -Subject: [PATCH 1/3] cryptodisk: make the password getter and additional - argument to recover_key - -cherry-picked from https://lists.gnu.org/archive/html/grub-devel/2020-12/msg00258.html. - -For AMD SEV environments, the grub boot password has to be retrieved -from a given memory location rather than prompted for. This means -that the standard password getter needs to be replaced with one that -gets the passphrase from the SEV area and uses that instead. Adding -the password getter as a passed in argument to recover_key() makes -this possible. - -Signed-off-by: James Bottomley ---- - grub-core/disk/cryptodisk.c | 2 +- - grub-core/disk/geli.c | 12 +++++++----- - grub-core/disk/luks.c | 12 +++++++----- - grub-core/lib/crypto.c | 4 ++++ - grub-core/osdep/unix/password.c | 4 ++++ - grub-core/osdep/windows/password.c | 4 ++++ - include/grub/cryptodisk.h | 6 +++++- - 7 files changed, 32 insertions(+), 12 deletions(-) - -diff --git a/grub-core/disk/cryptodisk.c b/grub-core/disk/cryptodisk.c -index 78a9025..133d36d 100644 ---- a/grub-core/disk/cryptodisk.c -+++ b/grub-core/disk/cryptodisk.c -@@ -839,7 +839,7 @@ grub_cryptodisk_scan_device_real (const char *name, grub_disk_t source) - if (!dev) - continue; - -- err = cr->recover_key (source, dev); -+ err = cr->recover_key (source, dev, grub_password_get); - if (err) - { - cryptodisk_close (dev); -diff --git a/grub-core/disk/geli.c b/grub-core/disk/geli.c -index e9d2329..ed43047 100644 ---- a/grub-core/disk/geli.c -+++ b/grub-core/disk/geli.c -@@ -398,7 +398,8 @@ configure_ciphers (grub_disk_t disk, const char *check_uuid, - } - - static grub_err_t --recover_key (grub_disk_t source, grub_cryptodisk_t dev) -+recover_key (grub_disk_t source, grub_cryptodisk_t dev, -+ grub_passwd_cb *password_get) - { - grub_size_t keysize; - grub_uint8_t digest[GRUB_CRYPTO_MAX_MDLEN]; -@@ -438,11 +439,12 @@ recover_key (grub_disk_t source, grub_cryptodisk_t dev) - tmp = NULL; - if (source->partition) - tmp = grub_partition_get_name (source->partition); -- grub_printf_ (N_("Enter passphrase for %s%s%s (%s): "), source->name, -- source->partition ? "," : "", tmp ? : "", -- dev->uuid); -+ if (password_get (NULL, 0)) -+ grub_printf_ (N_("Enter passphrase for %s%s%s (%s): "), source->name, -+ source->partition ? "," : "", tmp ? : "", -+ dev->uuid); - grub_free (tmp); -- if (!grub_password_get (passphrase, MAX_PASSPHRASE)) -+ if (!password_get (passphrase, MAX_PASSPHRASE)) - return grub_error (GRUB_ERR_BAD_ARGUMENT, "Passphrase not supplied"); - - /* Calculate the PBKDF2 of the user supplied passphrase. */ -diff --git a/grub-core/disk/luks.c b/grub-core/disk/luks.c -index 18b3a8b..037d20d 100644 ---- a/grub-core/disk/luks.c -+++ b/grub-core/disk/luks.c -@@ -309,7 +309,8 @@ configure_ciphers (grub_disk_t disk, const char *check_uuid, - - static grub_err_t - luks_recover_key (grub_disk_t source, -- grub_cryptodisk_t dev) -+ grub_cryptodisk_t dev, -+ grub_passwd_cb *password_get) - { - struct grub_luks_phdr header; - grub_size_t keysize; -@@ -344,11 +345,12 @@ luks_recover_key (grub_disk_t source, - tmp = NULL; - if (source->partition) - tmp = grub_partition_get_name (source->partition); -- grub_printf_ (N_("Enter passphrase for %s%s%s (%s): "), source->name, -- source->partition ? "," : "", tmp ? : "", -- dev->uuid); -+ if (password_get (NULL, 0)) -+ grub_printf_ (N_("Enter passphrase for %s%s%s (%s): "), source->name, -+ source->partition ? "," : "", tmp ? : "", -+ dev->uuid); - grub_free (tmp); -- if (!grub_password_get (passphrase, MAX_PASSPHRASE)) -+ if (!password_get (passphrase, MAX_PASSPHRASE)) - { - grub_free (split_key); - return grub_error (GRUB_ERR_BAD_ARGUMENT, "Passphrase not supplied"); -diff --git a/grub-core/lib/crypto.c b/grub-core/lib/crypto.c -index ff62fa3..07c4400 100644 ---- a/grub-core/lib/crypto.c -+++ b/grub-core/lib/crypto.c -@@ -460,6 +460,10 @@ grub_password_get (char buf[], unsigned buf_size) - unsigned cur_len = 0; - int key; - -+ if (!buf) -+ /* want prompt */ -+ return 1; -+ - while (1) - { - key = grub_getkey (); -diff --git a/grub-core/osdep/unix/password.c b/grub-core/osdep/unix/password.c -index 9996b24..365ac4b 100644 ---- a/grub-core/osdep/unix/password.c -+++ b/grub-core/osdep/unix/password.c -@@ -34,6 +34,10 @@ grub_password_get (char buf[], unsigned buf_size) - int tty_changed = 0; - char *ptr; - -+ if (!buf) -+ /* want prompt */ -+ return 1; -+ - grub_refresh (); - - /* Disable echoing. Based on glibc. */ -diff --git a/grub-core/osdep/windows/password.c b/grub-core/osdep/windows/password.c -index 1d3af0c..2a66156 100644 ---- a/grub-core/osdep/windows/password.c -+++ b/grub-core/osdep/windows/password.c -@@ -33,6 +33,10 @@ grub_password_get (char buf[], unsigned buf_size) - DWORD mode = 0; - char *ptr; - -+ if (!buf) -+ /* want prompt */ -+ return 1; -+ - grub_refresh (); - - GetConsoleMode (hStdin, &mode); -diff --git a/include/grub/cryptodisk.h b/include/grub/cryptodisk.h -index 32f564a..33c86ad 100644 ---- a/include/grub/cryptodisk.h -+++ b/include/grub/cryptodisk.h -@@ -101,6 +101,9 @@ struct grub_cryptodisk - }; - typedef struct grub_cryptodisk *grub_cryptodisk_t; - -+/* must match prototype for grub_password_get */ -+typedef int (grub_passwd_cb)(char buf[], unsigned buf_size); -+ - struct grub_cryptodisk_dev - { - struct grub_cryptodisk_dev *next; -@@ -108,7 +111,8 @@ struct grub_cryptodisk_dev - - grub_cryptodisk_t (*scan) (grub_disk_t disk, const char *check_uuid, - int boot_only); -- grub_err_t (*recover_key) (grub_disk_t disk, grub_cryptodisk_t dev); -+ grub_err_t (*recover_key) (grub_disk_t disk, grub_cryptodisk_t dev, -+ grub_passwd_cb *get_passwd); - }; - typedef struct grub_cryptodisk_dev *grub_cryptodisk_dev_t; - --- -2.43.5 - diff --git a/1004-cryptodisk-add-OS-provided-secret-support.patch b/1004-cryptodisk-add-OS-provided-secret-support.patch deleted file mode 100644 index 958a745..0000000 --- a/1004-cryptodisk-add-OS-provided-secret-support.patch +++ /dev/null @@ -1,206 +0,0 @@ -From a6d7988415f1ff7e3977b7e15aacaf1e7f854aac Mon Sep 17 00:00:00 2001 -From: James Bottomley -Date: Thu, 31 Dec 2020 09:36:17 -0800 -Subject: [PATCH 2/3] cryptodisk: add OS provided secret support - -cherry-picked from https://lists.gnu.org/archive/html/grub-devel/2020-12/msg00259.html. - -Make use of the new OS provided secrets API so that if the new '-s' -option is passed in we try to extract the secret from the API rather -than prompting for it. - -The primary consumer of this is AMD SEV, which has been programmed to -provide an injectable secret to the encrypted virtual machine. OVMF -provides the secret area and passes it into the EFI Configuration -Tables. The grub EFI layer pulls the secret out and primes the -secrets API with it. The upshot of all of this is that a SEV -protected VM can do an encrypted boot with a protected boot secret. - -Signed-off-by: James Bottomley ---- - grub-core/disk/cryptodisk.c | 77 +++++++++++++++++++++++++++++++++++-- - include/grub/cryptodisk.h | 14 +++++++ - 2 files changed, 87 insertions(+), 4 deletions(-) - -diff --git a/grub-core/disk/cryptodisk.c b/grub-core/disk/cryptodisk.c -index 133d36d..d2b1617 100644 ---- a/grub-core/disk/cryptodisk.c -+++ b/grub-core/disk/cryptodisk.c -@@ -41,6 +41,7 @@ static const struct grub_arg_option options[] = - /* TRANSLATORS: It's still restricted to cryptodisks only. */ - {"all", 'a', 0, N_("Mount all."), 0, 0}, - {"boot", 'b', 0, N_("Mount all volumes with `boot' flag set."), 0, 0}, -+ {"secret", 's', 0, N_("Get secret passphrase from named module and optional identifier"), 0, 0}, - {0, 0, 0, 0, 0, 0} - }; - -@@ -809,6 +810,10 @@ grub_util_cryptodisk_get_uuid (grub_disk_t disk) - - static int check_boot, have_it; - static char *search_uuid; -+static char *os_passwd; -+ -+/* variable to hold the list of secret providers */ -+static struct grub_secret_entry *secret_providers; - - static void - cryptodisk_close (grub_cryptodisk_t dev) -@@ -819,6 +824,21 @@ cryptodisk_close (grub_cryptodisk_t dev) - grub_free (dev); - } - -+static int -+os_password_get(char buf[], unsigned len) -+{ -+ if (!buf) -+ /* we're not interactive so no prompt */ -+ return 0; -+ -+ /* os_passwd should be null terminated, so just copy everything */ -+ grub_strncpy(buf, os_passwd, len); -+ /* and add a terminator just in case */ -+ buf[len - 1] = 0; -+ -+ return 1; -+} -+ - static grub_err_t - grub_cryptodisk_scan_device_real (const char *name, grub_disk_t source) - { -@@ -838,8 +858,17 @@ grub_cryptodisk_scan_device_real (const char *name, grub_disk_t source) - return grub_errno; - if (!dev) - continue; -- -- err = cr->recover_key (source, dev, grub_password_get); -+ -+ if (os_passwd) -+ { -+ err = cr->recover_key (source, dev, os_password_get); -+ if (err) -+ /* if the key doesn't work ignore the access denied error */ -+ grub_error_pop(); -+ } -+ else -+ err = cr->recover_key (source, dev, grub_password_get); -+ - if (err) - { - cryptodisk_close (dev); -@@ -855,6 +884,18 @@ grub_cryptodisk_scan_device_real (const char *name, grub_disk_t source) - return GRUB_ERR_NONE; - } - -+void -+grub_cryptodisk_add_secret_provider (struct grub_secret_entry *e) -+{ -+ grub_list_push(GRUB_AS_LIST_P (&secret_providers), GRUB_AS_LIST (e)); -+} -+ -+void -+grub_cryptodisk_remove_secret_provider (struct grub_secret_entry *e) -+{ -+ grub_list_remove (GRUB_AS_LIST (e)); -+} -+ - #ifdef GRUB_UTIL - #include - grub_err_t -@@ -931,7 +972,7 @@ grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int argc, char **args) - { - struct grub_arg_list *state = ctxt->state; - -- if (argc < 1 && !state[1].set && !state[2].set) -+ if (argc < 1 && !state[1].set && !state[2].set && !state[3].set) - return grub_error (GRUB_ERR_BAD_ARGUMENT, "device name required"); - - have_it = 0; -@@ -949,6 +990,7 @@ grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int argc, char **args) - - check_boot = state[2].set; - search_uuid = args[0]; -+ os_passwd = NULL; - grub_device_iterate (&grub_cryptodisk_scan_device, NULL); - search_uuid = NULL; - -@@ -959,11 +1001,37 @@ grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int argc, char **args) - else if (state[1].set || (argc == 0 && state[2].set)) - { - search_uuid = NULL; -+ os_passwd = NULL; - check_boot = state[2].set; - grub_device_iterate (&grub_cryptodisk_scan_device, NULL); - search_uuid = NULL; - return GRUB_ERR_NONE; - } -+ else if (state[3].set) -+ { -+ struct grub_secret_entry *se; -+ grub_err_t rc; -+ -+ if (argc < 1) -+ return grub_error (GRUB_ERR_BAD_ARGUMENT, "secret module must be specified"); -+#ifndef GRUB_UTIL -+ grub_dl_load (args[0]); -+#endif -+ se = grub_named_list_find (GRUB_AS_NAMED_LIST (secret_providers), args[0]); -+ if (se == NULL) -+ return grub_error (GRUB_ERR_INVALID_COMMAND, "No secret provider is found"); -+ -+ rc = se->get (args[1], &os_passwd); -+ if (rc) -+ return rc; -+ -+ search_uuid = NULL; -+ grub_device_iterate (&grub_cryptodisk_scan_device, NULL); -+ rc = se->put (args[1], have_it, &os_passwd); -+ os_passwd = NULL; -+ -+ return rc; -+ } - else - { - grub_err_t err; -@@ -974,6 +1042,7 @@ grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int argc, char **args) - grub_size_t len; - - search_uuid = NULL; -+ os_passwd = NULL; - check_boot = state[2].set; - diskname = args[0]; - len = grub_strlen (diskname); -@@ -1141,7 +1210,7 @@ GRUB_MOD_INIT (cryptodisk) - { - grub_disk_dev_register (&grub_cryptodisk_dev); - cmd = grub_register_extcmd ("cryptomount", grub_cmd_cryptomount, 0, -- N_("SOURCE|-u UUID|-a|-b"), -+ N_("SOURCE|-u UUID|-a|-b|-s MOD [ID]"), - N_("Mount a crypto device."), options); - grub_procfs_register ("luks_script", &luks_script); - } -diff --git a/include/grub/cryptodisk.h b/include/grub/cryptodisk.h -index 33c86ad..6a93c56 100644 ---- a/include/grub/cryptodisk.h -+++ b/include/grub/cryptodisk.h -@@ -160,4 +160,18 @@ grub_util_get_geli_uuid (const char *dev); - grub_cryptodisk_t grub_cryptodisk_get_by_uuid (const char *uuid); - grub_cryptodisk_t grub_cryptodisk_get_by_source_disk (grub_disk_t disk); - -+struct grub_secret_entry { -+ /* as named list */ -+ struct grub_secret_entry *next; -+ struct grub_secret_entry **prev; -+ const char *name; -+ -+ /* additional entries */ -+ grub_err_t (*get)(const char *arg, char **secret); -+ grub_err_t (*put)(const char *arg, int have_it, char **secret); -+}; -+ -+void grub_cryptodisk_add_secret_provider (struct grub_secret_entry *e); -+void grub_cryptodisk_remove_secret_provider (struct grub_secret_entry *e); -+ - #endif --- -2.43.5 - diff --git a/1005-efi-Add-API-for-retrieving-the-EFI-secret-for-crypto.patch b/1005-efi-Add-API-for-retrieving-the-EFI-secret-for-crypto.patch deleted file mode 100644 index cf51219..0000000 --- a/1005-efi-Add-API-for-retrieving-the-EFI-secret-for-crypto.patch +++ /dev/null @@ -1,217 +0,0 @@ -From 9711568e6dce36a6245fada1509e9477c0268e30 Mon Sep 17 00:00:00 2001 -From: James Bottomley -Date: Thu, 31 Dec 2020 09:36:18 -0800 -Subject: [PATCH 3/3] efi: Add API for retrieving the EFI secret for cryptodisk - -cherry-picked from https://lists.gnu.org/archive/html/grub-devel/2020-12/msg00260.html. - -This module is designed to provide an efisecret command which -interrogates the EFI configuration table to find the location of the -confidential computing secret and tries to register the secret with -the cryptodisk. - -The secret is stored in a boot allocated area, usually a page in size. -The layout of the secret injection area is a header - -|GRUB_EFI_SECRET_TABLE_HEADER_GUID|len| - -with entries of the form - -|guid|len|data| - -the guid corresponding to the disk encryption passphrase is -GRUB_EFI_DISKPASSWD_GUID and data must be a zero terminated string. -To get a high entropy string that doesn't need large numbers of -iterations, use a base64 encoding of 33 bytes of random data. - -Signed-off-by: James Bottomley ---- - grub-core/Makefile.core.def | 8 ++ - grub-core/disk/efi/efisecret.c | 129 +++++++++++++++++++++++++++++++++ - include/grub/efi/api.h | 15 ++++ - 3 files changed, 152 insertions(+) - create mode 100644 grub-core/disk/efi/efisecret.c - -diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def -index 0963e88..aded014 100644 ---- a/grub-core/Makefile.core.def -+++ b/grub-core/Makefile.core.def -@@ -774,6 +774,14 @@ module = { - enable = efi; - }; - -+module = { -+ name = efisecret; -+ -+ common = disk/efi/efisecret.c; -+ -+ enable = efi; -+}; -+ - module = { - name = lsefimmap; - -diff --git a/grub-core/disk/efi/efisecret.c b/grub-core/disk/efi/efisecret.c -new file mode 100644 -index 0000000..745eede ---- /dev/null -+++ b/grub-core/disk/efi/efisecret.c -@@ -0,0 +1,129 @@ -+#include -+#include -+#include -+#include -+#include -+#include -+ -+GRUB_MOD_LICENSE ("GPLv3+"); -+ -+static grub_efi_packed_guid_t secret_guid = GRUB_EFI_SECRET_TABLE_GUID; -+static grub_efi_packed_guid_t tableheader_guid = GRUB_EFI_SECRET_TABLE_HEADER_GUID; -+static grub_efi_packed_guid_t diskpasswd_guid = GRUB_EFI_DISKPASSWD_GUID; -+ -+/* -+ * EFI places the secret in the lower 4GB, so it uses a UINT32 -+ * for the pointer which we have to transform to the correct type -+ */ -+struct efi_secret { -+ grub_uint64_t base; -+ grub_uint64_t size; -+}; -+ -+struct secret_header { -+ grub_efi_packed_guid_t guid; -+ grub_uint32_t len; -+}; -+ -+struct secret_entry { -+ grub_efi_packed_guid_t guid; -+ grub_uint32_t len; -+ char data[0]; -+}; -+ -+static grub_err_t -+grub_efi_secret_put (const char *arg __attribute__((unused)), int have_it, -+ char **ptr) -+{ -+ struct secret_entry *e = (struct secret_entry *)(*ptr - (long)&((struct secret_entry *)0)->data); -+ -+ /* destroy the secret */ -+ grub_memset (e, 0, e->len); -+ *ptr = NULL; -+ -+ if (have_it) -+ return GRUB_ERR_NONE; -+ -+ return grub_error (GRUB_ERR_ACCESS_DENIED, "EFI secret failed to unlock any volumes"); -+} -+ -+static grub_err_t -+grub_efi_secret_find (struct efi_secret *s, char **secret_ptr) -+{ -+ int len; -+ struct secret_header *h; -+ struct secret_entry *e; -+ unsigned char *ptr = (unsigned char *)(unsigned long)s->base; -+ -+ /* the area must be big enough for a guid and a u32 length */ -+ if (s->size < sizeof (*h)) -+ return grub_error (GRUB_ERR_BAD_ARGUMENT, "EFI secret area is too small"); -+ -+ h = (struct secret_header *)ptr; -+ if (grub_memcmp(&h->guid, &tableheader_guid, sizeof (h->guid))) -+ return grub_error (GRUB_ERR_BAD_ARGUMENT, "EFI secret area does not start with correct guid\n"); -+ if (h->len < sizeof (*h)) -+ return grub_error (GRUB_ERR_BAD_ARGUMENT, "EFI secret area is too small\n"); -+ -+ len = h->len - sizeof (*h); -+ ptr += sizeof (*h); -+ -+ while (len >= (int)sizeof (*e)) { -+ e = (struct secret_entry *)ptr; -+ if (e->len < sizeof(*e) || e->len > (unsigned int)len) -+ return grub_error (GRUB_ERR_BAD_ARGUMENT, "EFI secret area is corrupt\n"); -+ -+ if (! grub_memcmp (&e->guid, &diskpasswd_guid, sizeof (e->guid))) { -+ int end = e->len - sizeof(*e); -+ -+ /* -+ * the passphrase must be a zero terminated string because the -+ * password routines call grub_strlen () to find its size -+ */ -+ if (e->data[end - 1] != '\0') -+ return grub_error (GRUB_ERR_BAD_ARGUMENT, "EFI secret area disk encryption password is not zero terminated\n"); -+ -+ *secret_ptr = e->data; -+ return GRUB_ERR_NONE; -+ } -+ ptr += e->len; -+ len -= e->len; -+ } -+ return grub_error (GRUB_ERR_BAD_ARGUMENT, "EFI secret aread does not contain disk decryption password\n"); -+} -+ -+static grub_err_t -+grub_efi_secret_get (const char *arg __attribute__((unused)), char **ptr) -+{ -+ unsigned int i; -+ -+ for (i = 0; i < grub_efi_system_table->num_table_entries; i++) -+ { -+ grub_efi_packed_guid_t *guid = -+ &grub_efi_system_table->configuration_table[i].vendor_guid; -+ -+ if (! grub_memcmp (guid, &secret_guid, sizeof (grub_efi_packed_guid_t))) { -+ struct efi_secret *s = -+ grub_efi_system_table->configuration_table[i].vendor_table; -+ -+ return grub_efi_secret_find(s, ptr); -+ } -+ } -+ return grub_error (GRUB_ERR_BAD_ARGUMENT, "No secret found in the EFI configuration table"); -+} -+ -+static struct grub_secret_entry secret = { -+ .name = "efisecret", -+ .get = grub_efi_secret_get, -+ .put = grub_efi_secret_put, -+}; -+ -+GRUB_MOD_INIT(efisecret) -+{ -+ grub_cryptodisk_add_secret_provider (&secret); -+} -+ -+GRUB_MOD_FINI(efisecret) -+{ -+ grub_cryptodisk_remove_secret_provider (&secret); -+} -diff --git a/include/grub/efi/api.h b/include/grub/efi/api.h -index a57187f..d5c805d 100644 ---- a/include/grub/efi/api.h -+++ b/include/grub/efi/api.h -@@ -299,6 +299,21 @@ - { 0x9a, 0x16, 0x00, 0x90, 0x27, 0x3f, 0xc1, 0x4d } \ - } - -+#define GRUB_EFI_SECRET_TABLE_GUID \ -+ { 0xadf956ad, 0xe98c, 0x484c, \ -+ { 0xae, 0x11, 0xb5, 0x1c, 0x7d, 0x33, 0x64, 0x47} \ -+ } -+ -+#define GRUB_EFI_SECRET_TABLE_HEADER_GUID \ -+ { 0x1e74f542, 0x71dd, 0x4d66, \ -+ { 0x96, 0x3e, 0xef, 0x42, 0x87, 0xff, 0x17, 0x3b } \ -+ } -+ -+#define GRUB_EFI_DISKPASSWD_GUID \ -+ { 0x736869e5, 0x84f0, 0x4973, \ -+ { 0x92, 0xec, 0x06, 0x87, 0x9c, 0xe3, 0xda, 0x0b } \ -+ } -+ - #define GRUB_EFI_ACPI_TABLE_GUID \ - { 0xeb9d2d30, 0x2d88, 0x11d3, \ - { 0x9a, 0x16, 0x0, 0x90, 0x27, 0x3f, 0xc1, 0x4d } \ --- -2.43.5 - diff --git a/1006-Update-patches-in-sync-with-loongarch64.patch b/1006-Update-patches-in-sync-with-loongarch64.patch deleted file mode 100644 index dc39a93..0000000 --- a/1006-Update-patches-in-sync-with-loongarch64.patch +++ /dev/null @@ -1,2815 +0,0 @@ -From 7118e2827e64c205e3b2241b85648f2948cab3a3 Mon Sep 17 00:00:00 2001 -From: Fedora Ninjas -Date: Sat, 22 Mar 2025 16:49:13 +0800 -Subject: [PATCH] Update patches in sync with loongarch64 - ---- - gentpl.py | 2 +- - grub-core/Makefile.am | 1 - - grub-core/Makefile.core.def | 11 +- - grub-core/kern/loongarch64/cache.S | 2 + - grub-core/kern/loongarch64/efi/init.c | 2 - - grub-core/kern/loongarch64/efi/startup.S | 4 +- - grub-core/kern/loongarch64/init.c | 47 -- - grub-core/lib/loongarch64/efi/loongson.c | 119 --- - grub-core/lib/loongarch64/relocator.c | 2 +- - grub-core/lib/loongarch64/setjmp.S | 80 +- - grub-core/loader/loongarch64/linux-efi.c | 143 ++++ - grub-core/loader/loongarch64/linux-elf.c | 910 +++++++++++++++++++++++ - grub-core/loader/loongarch64/linux.c | 731 +++++------------- - include/grub/efi/api.h | 20 + - include/grub/fdt.h | 2 +- - include/grub/loongarch64/asm.h | 10 - - include/grub/loongarch64/efi/loongson.h | 113 --- - include/grub/loongarch64/efi/memory.h | 7 +- - include/grub/loongarch64/efi/time.h | 0 - include/grub/loongarch64/linux.h | 163 +++- - include/grub/loongarch64/time.h | 12 - - 21 files changed, 1487 insertions(+), 894 deletions(-) - delete mode 100644 grub-core/kern/loongarch64/init.c - delete mode 100644 grub-core/lib/loongarch64/efi/loongson.c - create mode 100644 grub-core/loader/loongarch64/linux-efi.c - create mode 100644 grub-core/loader/loongarch64/linux-elf.c - delete mode 100644 include/grub/loongarch64/asm.h - delete mode 100644 include/grub/loongarch64/efi/loongson.h - delete mode 100644 include/grub/loongarch64/efi/time.h - -diff --git a/gentpl.py b/gentpl.py -index 3afd642..fa7f1ba 100644 ---- a/gentpl.py -+++ b/gentpl.py -@@ -78,7 +78,7 @@ GROUPS["terminfomodule"] = GRUB_PLATFORMS[:]; - for i in GROUPS["terminfoinkernel"]: GROUPS["terminfomodule"].remove(i) - - # Flattened Device Trees (FDT) --GROUPS["fdt"] = [ "arm64_efi", "arm_uboot", "arm_efi" ] -+GROUPS["fdt"] = [ "arm64_efi", "arm_uboot", "arm_efi", "loongarch64_efi" ] - - # Needs software helpers for division - # Must match GRUB_DIVISION_IN_SOFTWARE in misc.h -diff --git a/grub-core/Makefile.am b/grub-core/Makefile.am -index a976fad..ec771c5 100644 ---- a/grub-core/Makefile.am -+++ b/grub-core/Makefile.am -@@ -227,7 +227,6 @@ KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/efi/efi.h - KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/efi/disk.h - KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/acpi.h - KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/fdt.h --KERNEL_HEADER_FILES += $(top_builddir)/include/grub/machine/loongson.h - endif - - if COND_powerpc_ieee1275 -diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def -index aded014..b89e58e 100644 ---- a/grub-core/Makefile.core.def -+++ b/grub-core/Makefile.core.def -@@ -250,6 +250,9 @@ kernel = { - arm64_efi = kern/arm64/efi/init.c; - arm64_efi = kern/efi/fdt.c; - -+ loongarch64_efi = kern/loongarch64/efi/init.c; -+ loongarch64_efi = kern/efi/fdt.c; -+ - i386_pc = kern/i386/pc/init.c; - i386_pc = kern/i386/pc/mmap.c; - i386_pc = term/i386/pc/console.c; -@@ -300,14 +303,8 @@ kernel = { - extra_dist = video/sis315_init.c; - mips_loongson = commands/keylayouts.c; - -- loongarch64 = kern/loongarch64/init.c; - loongarch64 = kern/loongarch64/dl.c; - loongarch64 = kern/loongarch64/cache.S; -- loongarch64 = kern/generic/rtc_get_time_ms.c; -- loongarch64 = kern/efi/fdt.c; -- loongarch64 = lib/fdt.c; -- loongarch64_efi = kern/loongarch64/efi/init.c; -- loongarch64_efi = lib/loongarch64/efi/loongson.c; - - powerpc_ieee1275 = kern/powerpc/cache.S; - powerpc_ieee1275 = kern/powerpc/dl.c; -@@ -1767,6 +1764,8 @@ module = { - i386_pc = lib/i386/pc/vesa_modes_table.c; - mips = loader/mips/linux.c; - loongarch64 = loader/loongarch64/linux.c; -+ loongarch64 = loader/loongarch64/linux-elf.c; -+ loongarch64 = loader/loongarch64/linux-efi.c; - powerpc_ieee1275 = loader/powerpc/ieee1275/linux.c; - sparc64_ieee1275 = loader/sparc64/ieee1275/linux.c; - ia64_efi = loader/ia64/efi/linux.c; -diff --git a/grub-core/kern/loongarch64/cache.S b/grub-core/kern/loongarch64/cache.S -index d291c67..b268261 100644 ---- a/grub-core/kern/loongarch64/cache.S -+++ b/grub-core/kern/loongarch64/cache.S -@@ -19,6 +19,8 @@ - #include - - FUNCTION (grub_arch_sync_caches) -+ ibar 0 -+ dbar 0 - jr $ra - - FUNCTION (grub_arch_sync_dma_caches) -diff --git a/grub-core/kern/loongarch64/efi/init.c b/grub-core/kern/loongarch64/efi/init.c -index 632c39a..15c41aa 100644 ---- a/grub-core/kern/loongarch64/efi/init.c -+++ b/grub-core/kern/loongarch64/efi/init.c -@@ -1,4 +1,3 @@ --/* init.c - initialize an arm-based EFI system */ - /* - * GRUB -- GRand Unified Bootloader - * Copyright (C) 2013 Free Software Foundation, Inc. -@@ -24,7 +23,6 @@ - #include - #include - #include --#include - - static grub_uint64_t tmr; - static grub_efi_event_t tmr_evt; -diff --git a/grub-core/kern/loongarch64/efi/startup.S b/grub-core/kern/loongarch64/efi/startup.S -index 1ffff08..00ecd15 100644 ---- a/grub-core/kern/loongarch64/efi/startup.S -+++ b/grub-core/kern/loongarch64/efi/startup.S -@@ -19,8 +19,9 @@ - #include - - .file "startup.S" -- .text - .globl start, _start -+ .text -+ - .align 4 - - FUNCTION(start) -@@ -42,4 +43,3 @@ FUNCTION(_start) - ld.d $ra, $sp, 0 - addi.d $sp, $sp, 16 - jr $ra -- -diff --git a/grub-core/kern/loongarch64/init.c b/grub-core/kern/loongarch64/init.c -deleted file mode 100644 -index b2de930..0000000 ---- a/grub-core/kern/loongarch64/init.c -+++ /dev/null -@@ -1,47 +0,0 @@ --/* -- * GRUB -- GRand Unified Bootloader -- * Copyright (C) 2009,2017 Free Software Foundation, Inc. -- * -- * GRUB is free software: you can redistribute it and/or modify -- * it under the terms of the GNU General Public License as published by -- * the Free Software Foundation, either version 3 of the License, or -- * (at your option) any later version. -- * -- * GRUB is distributed in the hope that it will be useful, -- * but WITHOUT ANY WARRANTY; without even the implied warranty of -- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -- * GNU General Public License for more details. -- * -- * You should have received a copy of the GNU General Public License -- * along with GRUB. If not, see . -- */ -- --#include --#include --#include --#include -- --grub_uint32_t grub_arch_cpuclock; -- --/* FIXME: use interrupt to count high. */ --grub_uint64_t --grub_get_rtc (void) --{ -- static grub_uint32_t high = 0; -- static grub_uint32_t last = 0; -- grub_uint32_t low; -- -- asm volatile ("csrrd %0, " GRUB_CPU_LOONGARCH_COP0_TIMER_COUNT : "=r" (low)); -- if (low < last) -- high++; -- last = low; -- -- return (((grub_uint64_t) high) << 32) | low; --} -- --void --grub_timer_init (grub_uint32_t cpuclock) --{ -- grub_arch_cpuclock = cpuclock; -- grub_install_get_time_ms (grub_rtc_get_time_ms); --} -diff --git a/grub-core/lib/loongarch64/efi/loongson.c b/grub-core/lib/loongarch64/efi/loongson.c -deleted file mode 100644 -index eda1e1c..0000000 ---- a/grub-core/lib/loongarch64/efi/loongson.c -+++ /dev/null -@@ -1,119 +0,0 @@ --/* -- * GRUB -- GRand Unified Bootloader -- * Copyright (C) 2017 Free Software Foundation, Inc. -- * -- * GRUB is free software: you can redistribute it and/or modify -- * it under the terms of the GNU General Public License as published by -- * the Free Software Foundation, either version 3 of the License, or -- * (at your option) any later version. -- * -- * GRUB is distributed in the hope that it will be useful, -- * but WITHOUT ANY WARRANTY; without even the implied warranty of -- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -- * GNU General Public License for more details. -- * -- * You should have received a copy of the GNU General Public License -- * along with GRUB. If not, see . -- */ -- --#include --#include --#include --#include --#include --#include -- --unsigned long --grub_efi_get_bpi_version (const char *str) --{ -- unsigned long version = GRUB_EFI_BPI_VER_NONE; -- -- version = grub_strtoul (str + 4, 0, 0); -- -- return version; --} -- --void * --grub_efi_loongson_get_boot_params (void) --{ -- static void * boot_params = NULL; -- grub_efi_configuration_table_t *tables; -- grub_efi_guid_t loongson_bpi_guid = GRUB_EFI_LOONGSON_BPI_TABLE_GUID; -- unsigned int i; -- -- if (boot_params) -- return boot_params; -- -- /* Look for Loongson boot params interface in UEFI config tables. */ -- tables = grub_efi_system_table->configuration_table; -- -- for (i = 0; i < grub_efi_system_table->num_table_entries; i++) -- if (grub_memcmp (&tables[i].vendor_guid, &loongson_bpi_guid, sizeof (loongson_bpi_guid)) == 0) -- { -- boot_params= tables[i].vendor_table; -- grub_dprintf ("loongson", "found registered BPI @ %p\n", boot_params); -- break; -- } -- return boot_params; --} -- --grub_uint8_t --grub_efi_loongson_calculatesum8 (const grub_uint8_t *buffer, grub_efi_uintn_t length) --{ -- grub_uint8_t sum; -- grub_efi_uintn_t count; -- -- for (sum = 0, count = 0; count < length; count++) -- { -- sum = (grub_uint8_t) (sum + *(buffer + count)); -- } -- return sum; --} -- --grub_uint8_t --grub_efi_loongson_grub_calculatechecksum8 (const grub_uint8_t *buffer, grub_efi_uintn_t length) --{ -- grub_uint8_t checksum; -- -- checksum = grub_efi_loongson_calculatesum8(buffer, length); -- -- return (grub_uint8_t) (0x100 - checksum); --} -- -- --grub_uint32_t --grub_efi_loongson_memmap_v1_sort(struct memmap_v1 array[], grub_uint32_t length, -- mem_map_v1 * bpmem, grub_uint32_t index, grub_uint32_t memtype) --{ -- grub_uint64_t tempmemsize = 0; -- grub_uint32_t j = 0; -- grub_uint32_t t = 0; -- -- for(j = 0; j < length;) -- { -- tempmemsize = array[j].memsize; -- for(t = j + 1; t < length; t++) -- { -- if(array[j].memstart + tempmemsize == array[t].memstart) -- { -- tempmemsize += array[t].memsize; -- } -- else -- { -- break; -- } -- } -- bpmem->map[index].memtype = memtype; -- bpmem->map[index].memstart = array[j].memstart; -- bpmem->map[index].memsize = tempmemsize; -- grub_dprintf("loongson", "v1 map[%d]:type %x, start 0x%lx, end 0x%lx\n", -- index, -- bpmem->map[index].memtype, -- bpmem->map[index].memstart, -- bpmem->map[index].memstart+ bpmem->map[index].memsize -- ); -- j = t; -- index++; -- } -- return index; --} -diff --git a/grub-core/lib/loongarch64/relocator.c b/grub-core/lib/loongarch64/relocator.c -index 4b253ca..7217b68 100644 ---- a/grub-core/lib/loongarch64/relocator.c -+++ b/grub-core/lib/loongarch64/relocator.c -@@ -122,7 +122,7 @@ grub_relocator64_boot (struct grub_relocator *rel, - unsigned i; - grub_addr_t vtarget; - -- err = grub_relocator_alloc_chunk_align (rel, &ch, 0, -+ err = grub_relocator_alloc_chunk_align (rel, &ch, 0x3000000, - (0xffffffff - stateset_size) - + 1, stateset_size, - grub_relocator_align, -diff --git a/grub-core/lib/loongarch64/setjmp.S b/grub-core/lib/loongarch64/setjmp.S -index 47db814..bb09959 100644 ---- a/grub-core/lib/loongarch64/setjmp.S -+++ b/grub-core/lib/loongarch64/setjmp.S -@@ -1,6 +1,6 @@ - /* - * GRUB -- GRand Unified Bootloader -- * Copyright (C) 2003,2007,2009 Free Software Foundation, Inc. -+ * Copyright (C) 2021 Free Software Foundation, Inc. - * - * GRUB is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by -@@ -16,9 +16,7 @@ - * along with GRUB. If not, see . - */ - --#include - #include --#include - - .file "setjmp.S" - -@@ -27,48 +25,44 @@ GRUB_MOD_LICENSE "GPLv3+" - .text - - /* -- * int grub_setjmp (grub_jmp_buf env) -+ * int grub_setjmp (jmp_buf env) - */ - FUNCTION(grub_setjmp) -- GRUB_ASM_REG_S $s0, $a0,0 -- GRUB_ASM_REG_S $s1, $a0,8 -- GRUB_ASM_REG_S $s2, $a0,16 -- GRUB_ASM_REG_S $s3, $a0,24 -- GRUB_ASM_REG_S $s4, $a0,32 -- GRUB_ASM_REG_S $s5, $a0,40 -- GRUB_ASM_REG_S $s6, $a0,48 -- GRUB_ASM_REG_S $s7, $a0,56 -- GRUB_ASM_REG_S $s8, $a0,64 -- GRUB_ASM_REG_S $fp, $a0,72 -- GRUB_ASM_REG_S $sp, $a0,80 -- GRUB_ASM_REG_S $ra, $a0,88 -- move $v0, $zero -- move $v1, $zero -- jr $ra -- nop -+ st.d $s0, $a0, 0x0 -+ st.d $s1, $a0, 0x8 -+ st.d $s2, $a0, 0x10 -+ st.d $s3, $a0, 0x18 -+ st.d $s4, $a0, 0x20 -+ st.d $s5, $a0, 0x28 -+ st.d $s6, $a0, 0x30 -+ st.d $s7, $a0, 0x38 -+ st.d $s8, $a0, 0x40 -+ st.d $fp, $a0, 0x48 -+ st.d $sp, $a0, 0x50 -+ st.d $ra, $a0, 0x58 -+ -+ move $a0, $zero -+ jr $ra -+ - /* -- * int grub_longjmp (grub_jmp_buf env, int val) -+ * void grub_longjmp (jmp_buf env, int val) - */ - FUNCTION(grub_longjmp) -- GRUB_ASM_REG_L $s0, $a0,0 -- GRUB_ASM_REG_L $s1, $a0,8 -- GRUB_ASM_REG_L $s2, $a0,16 -- GRUB_ASM_REG_L $s3, $a0,24 -- GRUB_ASM_REG_L $s4, $a0,32 -- GRUB_ASM_REG_L $s5, $a0,40 -- GRUB_ASM_REG_L $s6, $a0,48 -- GRUB_ASM_REG_L $s7, $a0,56 -- GRUB_ASM_REG_L $s8, $a0,64 -- GRUB_ASM_REG_L $fp, $a0,72 -- GRUB_ASM_REG_L $sp, $a0,80 -- GRUB_ASM_REG_L $ra, $a0,88 -- addi.w $v0, $zero, 1 -- /* -- * replace: movn $v0, $a1, $a1 -- */ -- bnez $a1, .ZW0 -- addi.d $v0, $a1, 0 --.ZW0: -- addi.d $v1,$zero,0 -- jr $ra -- nop -+ ld.d $s0, $a0, 0x0 -+ ld.d $s1, $a0, 0x8 -+ ld.d $s2, $a0, 0x10 -+ ld.d $s3, $a0, 0x18 -+ ld.d $s4, $a0, 0x20 -+ ld.d $s5, $a0, 0x28 -+ ld.d $s6, $a0, 0x30 -+ ld.d $s7, $a0, 0x38 -+ ld.d $s8, $a0, 0x40 -+ ld.d $fp, $a0, 0x48 -+ ld.d $sp, $a0, 0x50 -+ ld.d $ra, $a0, 0x58 -+ -+ li.w $a0, 1 -+ beqz $a1, .L0 -+ move $a0, $a1 -+.L0: -+ jr $ra -diff --git a/grub-core/loader/loongarch64/linux-efi.c b/grub-core/loader/loongarch64/linux-efi.c -new file mode 100644 -index 0000000..6f5ff15 ---- /dev/null -+++ b/grub-core/loader/loongarch64/linux-efi.c -@@ -0,0 +1,143 @@ -+/* -+ * GRUB -- GRand Unified Bootloader -+ * Copyright (C) 2021 Free Software Foundation, Inc. -+ * -+ * GRUB is free software: you can redistribute it and/or modify -+ * it under the terms of the GNU General Public License as published by -+ * the Free Software Foundation, either version 3 of the License, or -+ * (at your option) any later version. -+ * -+ * GRUB is distributed in the hope that it will be useful, -+ * but WITHOUT ANY WARRANTY; without even the implied warranty of -+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -+ * GNU General Public License for more details. -+ * -+ * You should have received a copy of the GNU General Public License -+ * along with GRUB. If not, see . -+ */ -+#include -+#include -+#include -+#include -+#include -+#include -+ -+#define GRUB_EFI_PE_MAGIC 0x5A4D -+ -+grub_err_t -+finalize_efi_params_linux (struct linux_loongarch64_kernel_params *kernel_params) -+{ -+ int node, retval; -+ -+ void *fdt; -+ -+ fdt = grub_fdt_load (GRUB_EFI_LINUX_FDT_EXTRA_SPACE); -+ -+ if (!fdt) -+ goto failure; -+ -+ node = grub_fdt_find_subnode (fdt, 0, "chosen"); -+ if (node < 0) -+ node = grub_fdt_add_subnode (fdt, 0, "chosen"); -+ -+ if (node < 1) -+ goto failure; -+ -+ /* Set initrd info */ -+ if (kernel_params->ramdisk_addr && kernel_params->ramdisk_size) -+ { -+ grub_dprintf ("linux", "Initrd @ %p-%p\n", -+ (void *) kernel_params->ramdisk_addr, -+ (void *) (kernel_params->ramdisk_addr + kernel_params->ramdisk_size)); -+ -+ retval = grub_fdt_set_prop64 (fdt, node, "linux,initrd-start", -+ kernel_params->ramdisk_addr); -+ if (retval) -+ goto failure; -+ retval = grub_fdt_set_prop64 (fdt, node, "linux,initrd-end", -+ kernel_params->ramdisk_addr + kernel_params->ramdisk_size); -+ if (retval) -+ goto failure; -+ } -+ -+ if (grub_fdt_install() != GRUB_ERR_NONE) -+ goto failure; -+ -+ return GRUB_ERR_NONE; -+ -+failure: -+ grub_fdt_unload(); -+ return grub_error(GRUB_ERR_BAD_OS, "failed to install/update FDT"); -+} -+ -+grub_err_t -+grub_arch_efi_linux_check_image (struct linux_arch_kernel_header * lh) -+{ -+ if ((lh->code0 & 0xffff) == GRUB_EFI_PE_MAGIC) -+ return GRUB_ERR_NONE; -+ else -+ return 1; -+ -+ grub_dprintf ("linux", "UEFI stub kernel:\n"); -+ grub_dprintf ("linux", "PE/COFF header @ %08x\n", lh->hdr_offset); -+ -+ return GRUB_ERR_NONE; -+} -+ -+grub_err_t -+grub_arch_efi_linux_boot_image (grub_addr_t addr, grub_size_t size, char *args) -+{ -+ grub_efi_memory_mapped_device_path_t *mempath; -+ grub_efi_handle_t image_handle; -+ grub_efi_boot_services_t *b; -+ grub_efi_status_t status; -+ grub_efi_loaded_image_t *loaded_image; -+ int len; -+ -+ mempath = grub_malloc (2 * sizeof (grub_efi_memory_mapped_device_path_t)); -+ if (!mempath) -+ return grub_errno; -+ -+ mempath[0].header.type = GRUB_EFI_HARDWARE_DEVICE_PATH_TYPE; -+ mempath[0].header.subtype = GRUB_EFI_MEMORY_MAPPED_DEVICE_PATH_SUBTYPE; -+ mempath[0].header.length = grub_cpu_to_le16_compile_time (sizeof (*mempath)); -+ mempath[0].memory_type = GRUB_EFI_LOADER_DATA; -+ mempath[0].start_address = addr; -+ mempath[0].end_address = addr + size; -+ -+ mempath[1].header.type = GRUB_EFI_END_DEVICE_PATH_TYPE; -+ mempath[1].header.subtype = GRUB_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE; -+ mempath[1].header.length = sizeof (grub_efi_device_path_t); -+ -+ b = grub_efi_system_table->boot_services; -+ status = b->load_image (0, grub_efi_image_handle, -+ (grub_efi_device_path_t *) mempath, -+ (void *) addr, size, &image_handle); -+ if (status != GRUB_EFI_SUCCESS) -+ return grub_error (GRUB_ERR_BAD_OS, "cannot load image"); -+ -+ grub_dprintf ("linux", "linux command line: '%s'\n", args); -+ -+ /* Convert command line to UCS-2 */ -+ loaded_image = grub_efi_get_loaded_image (image_handle); -+ loaded_image->load_options_size = len = -+ (grub_strlen (args) + 1) * sizeof (grub_efi_char16_t); -+ loaded_image->load_options = -+ grub_efi_allocate_any_pages (GRUB_EFI_BYTES_TO_PAGES (loaded_image->load_options_size)); -+ if (!loaded_image->load_options) -+ return grub_errno; -+ -+ loaded_image->load_options_size = -+ 2 * grub_utf8_to_utf16 (loaded_image->load_options, len, -+ (grub_uint8_t *) args, len, NULL); -+ -+ grub_dprintf ("linux", "starting image %p\n", image_handle); -+ status = b->start_image (image_handle, 0, NULL); -+ -+ /* When successful, not reached */ -+ b->unload_image (image_handle); -+ grub_efi_free_pages ((grub_addr_t) loaded_image->load_options, -+ GRUB_EFI_BYTES_TO_PAGES (loaded_image->load_options_size)); -+ -+ return grub_errno; -+} -diff --git a/grub-core/loader/loongarch64/linux-elf.c b/grub-core/loader/loongarch64/linux-elf.c -new file mode 100644 -index 0000000..d54028e ---- /dev/null -+++ b/grub-core/loader/loongarch64/linux-elf.c -@@ -0,0 +1,910 @@ -+/* -+ * GRUB -- GRand Unified Bootloader -+ * Copyright (C) 2021 Free Software Foundation, Inc. -+ * -+ * GRUB is free software: you can redistribute it and/or modify -+ * it under the terms of the GNU General Public License as published by -+ * the Free Software Foundation, either version 3 of the License, or -+ * (at your option) any later version. -+ * -+ * GRUB is distributed in the hope that it will be useful, -+ * but WITHOUT ANY WARRANTY; without even the implied warranty of -+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -+ * GNU General Public License for more details. -+ * -+ * You should have received a copy of the GNU General Public License -+ * along with GRUB. If not, see . -+ */ -+ -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+ -+#define GRUB_EFI_MMAP_NR_SLACK_SLOTS 8 -+ -+#define GRUB_ADDRESS_TYPE_SYSRAM 1 -+#define GRUB_ADDRESS_TYPE_RESERVED 2 -+#define GRUB_ADDRESS_TYPE_ACPI 3 -+#define GRUB_ADDRESS_TYPE_NVS 4 -+#define GRUB_ADDRESS_TYPE_PMEM 5 -+ -+#define GRUB_EFI_LOONGSON_BPI_TABLE_GUID \ -+ { 0x4660f721, 0x2ec5, 0x416a, \ -+ { 0x89, 0x9a, 0x43, 0x18, 0x02, 0x50, 0xa0, 0xc9 } \ -+ } -+ -+#define GRUB_EFI_LARCH_SCREEN_INFO_GUID \ -+ { 0x07fd51a6, 0X9532, 0X926f, \ -+ { 0X51, 0Xdc, 0X6a, 0X63, 0X60, 0X2f, 0X84, 0Xb4 } \ -+ } -+ -+#define GRUB_EFI_SCREEN_INFO_GUID \ -+ { 0xe03fc20a, 0x85dc, 0x406e, \ -+ { 0xb9, 0x0e, 0x4a, 0xb5, 0x02, 0x37, 0x1d, 0x95 } \ -+ } -+ -+static struct grub_relocator *relocator; -+static grub_efi_guid_t compat_screen_info_guid = GRUB_EFI_LARCH_SCREEN_INFO_GUID; -+static grub_efi_guid_t screen_info_guid = GRUB_EFI_SCREEN_INFO_GUID; -+ -+void grub_linux_loongarch_elf_relocator_unload (void) -+{ -+ grub_relocator_unload (relocator); -+} -+ -+static void find_bits(unsigned long mask, grub_efi_uint8_t *pos, grub_efi_uint8_t *size) -+{ -+ grub_efi_uint8_t first, len; -+ -+ first = 0; -+ len = 0; -+ -+ if (mask) { -+ while (!(mask & 0x1)) { -+ mask = mask >> 1; -+ first++; -+ } -+ -+ while (mask & 0x1) { -+ mask = mask >> 1; -+ len++; -+ } -+ } -+ -+ *pos = first; -+ *size = len; -+} -+ -+static void -+setup_pixel_info(struct screen_info *si, grub_efi_uint32_t pixels_per_scan_line, -+ struct grub_efi_gop_pixel_bitmask pixel_info, int pixel_format) -+{ -+ if (pixel_format == GRUB_EFI_GOT_RGBA8) { -+ si->lfb_depth = 32; -+ si->lfb_linelength = pixels_per_scan_line * 4; -+ si->red_size = 8; -+ si->red_pos = 0; -+ si->green_size = 8; -+ si->green_pos = 8; -+ si->blue_size = 8; -+ si->blue_pos = 16; -+ si->rsvd_size = 8; -+ si->rsvd_pos = 24; -+ } else if (pixel_format == GRUB_EFI_GOT_BGRA8) { -+ si->lfb_depth = 32; -+ si->lfb_linelength = pixels_per_scan_line * 4; -+ si->red_size = 8; -+ si->red_pos = 16; -+ si->green_size = 8; -+ si->green_pos = 8; -+ si->blue_size = 8; -+ si->blue_pos = 0; -+ si->rsvd_size = 8; -+ si->rsvd_pos = 24; -+ } else if (pixel_format == GRUB_EFI_GOT_BITMASK) { -+ find_bits(pixel_info.r, &si->red_pos, &si->red_size); -+ find_bits(pixel_info.g, &si->green_pos, -+ &si->green_size); -+ find_bits(pixel_info.b, &si->blue_pos, &si->blue_size); -+ find_bits(pixel_info.a, &si->rsvd_pos, -+ &si->rsvd_size); -+ si->lfb_depth = si->red_size + si->green_size + -+ si->blue_size + si->rsvd_size; -+ si->lfb_linelength = (pixels_per_scan_line * si->lfb_depth) / 8; -+ } else { -+ si->lfb_depth = 4; -+ si->lfb_linelength = si->lfb_width / 2; -+ si->red_size = 0; -+ si->red_pos = 0; -+ si->green_size = 0; -+ si->green_pos = 0; -+ si->blue_size = 0; -+ si->blue_pos = 0; -+ si->rsvd_size = 0; -+ si->rsvd_pos = 0; -+ } -+} -+ -+static struct screen_info *alloc_screen_info(void) -+{ -+ grub_efi_status_t status; -+ grub_efi_boot_services_t *b; -+ struct screen_info *si; -+ -+ b = grub_efi_system_table->boot_services; -+ status = efi_call_3 (b->allocate_pool, GRUB_EFI_RUNTIME_SERVICES_DATA, -+ sizeof(*si), (void**)&si); -+ if (status != GRUB_EFI_SUCCESS) -+ return NULL; -+ -+ grub_memset ((void *)si, 0, sizeof(struct screen_info)); -+ -+ status = b->install_configuration_table (&compat_screen_info_guid, si); -+ if (status != GRUB_EFI_SUCCESS) -+ goto free_mem; -+ -+ status = b->install_configuration_table (&screen_info_guid, si); -+ if (status == GRUB_EFI_SUCCESS) { -+ return si; -+ } -+ -+free_table: -+ b->install_configuration_table (&compat_screen_info_guid, NULL); -+free_mem: -+ efi_call_1 (b->free_pool, si); -+ -+ return NULL; -+} -+ -+static struct screen_info *setup_screen_info(void) -+{ -+ grub_efi_boot_services_t *b; -+ grub_efi_handle_t gop_handle; -+ struct screen_info *si = NULL; -+ struct grub_efi_gop *gop, *first_gop; -+ grub_efi_handle_t *handles; -+ grub_efi_uintn_t num_handles, i; -+ grub_efi_guid_t graphics_output_guid = GRUB_EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID; -+ grub_efi_uint16_t width, height; -+ grub_efi_uint32_t ext_lfb_base, pixels_per_scan_line; -+ grub_efi_uint64_t fb_base; -+ struct grub_efi_gop_pixel_bitmask pixel_info; -+ grub_efi_gop_pixel_format_t pixel_format; -+ -+ si = alloc_screen_info(); -+ if (!si) -+ return NULL; -+ -+ handles = grub_efi_locate_handle (GRUB_EFI_BY_PROTOCOL, -+ &graphics_output_guid, NULL, &num_handles); -+ if (!handles || num_handles == 0) -+ goto free_screen_info; -+ -+ gop = NULL; -+ first_gop = NULL; -+ -+ for (i = 0; i < num_handles; i++) -+ { -+ struct grub_efi_gop_mode *mode; -+ struct grub_efi_gop_mode_info *info = NULL; -+ grub_efi_guid_t conout_proto = GRUB_EFI_CONSOLE_OUT_DEVICE_GUID; -+ void *dummy = NULL; -+ grub_efi_uint8_t conout_found = 0; -+ grub_efi_uint64_t current_fb_base; -+ -+ gop_handle = handles[i]; -+ gop = grub_efi_open_protocol (gop_handle, &graphics_output_guid, -+ GRUB_EFI_OPEN_PROTOCOL_GET_PROTOCOL); -+ -+ dummy = grub_efi_open_protocol (gop_handle, &conout_proto, -+ GRUB_EFI_OPEN_PROTOCOL_GET_PROTOCOL); -+ if (dummy != NULL) -+ conout_found = 1; -+ -+ mode = gop->mode; -+ info = mode->info; -+ current_fb_base = mode->fb_base; -+ -+ if ((!first_gop || conout_found) && -+ info->pixel_format != GRUB_EFI_GOT_BLT_ONLY) { -+ /* -+ * Systems that use the UEFI Console Splitter may -+ * provide multiple GOP devices, not all of which are -+ * backed by real hardware. The workaround is to search -+ * for a GOP implementing the ConOut protocol, and if -+ * one isn't found, to just fall back to the first GOP. -+ */ -+ width = info->width; -+ height = info->height; -+ pixel_format = info->pixel_format; -+ pixel_info = info->pixel_bitmask; -+ pixels_per_scan_line = info->pixels_per_scanline; -+ fb_base = current_fb_base; -+ -+ /* -+ * Once we've found a GOP supporting ConOut, -+ * don't bother looking any further. -+ */ -+ first_gop = gop; -+ if (conout_found) -+ break; -+ } -+ } -+ -+ /* Did we find any GOPs? */ -+ if (!first_gop) -+ goto free_screen_info; -+ -+ /* EFI framebuffer */ -+ si->orig_video_isVGA = GRUB_VIDEO_TYPE_EFI; -+ -+ si->lfb_width = width; -+ si->lfb_height = height; -+ si->lfb_base = fb_base; -+ grub_dprintf ("loongson", "Screen info fb base: 0x%"PRIxGRUB_UINT32_T"\n", si->lfb_base); -+ -+ ext_lfb_base = (grub_uint64_t)fb_base >> 32; -+ if (ext_lfb_base) { -+ si->capabilities |= GRUB_VIDEO_CAPABILITY_64BIT_BASE; -+ si->ext_lfb_base = ext_lfb_base; -+ } -+ si->pages = 1; -+ -+ setup_pixel_info(si, pixels_per_scan_line, pixel_info, pixel_format); -+ -+ si->lfb_size = si->lfb_linelength * si->lfb_height; -+ si->capabilities |= GRUB_VIDEO_CAPABILITY_SKIP_QUIRKS; -+ -+ return si; -+ -+free_screen_info: -+ b = grub_efi_system_table->boot_services; -+ b->install_configuration_table (&compat_screen_info_guid, NULL); -+ b->install_configuration_table (&screen_info_guid, NULL); -+ if (si) -+ efi_call_1 (b->free_pool, si); -+ -+ grub_dprintf ("loongson", "No screen info\n"); -+ return NULL; -+} -+ -+static grub_err_t -+allocate_memmap_and_exit_boot (struct linux_loongarch64_kernel_params *kernel_params) -+{ -+ grub_err_t err; -+ grub_efi_status_t status; -+ grub_efi_uintn_t mmap_size, desc_size, size; -+ grub_efi_uint32_t desc_version; -+ grub_efi_memory_descriptor_t *mmap_buf; -+ grub_efi_boot_services_t *b; -+ struct efi_boot_memmap *m, tmp; -+ struct efi_initrd *tbl = NULL; -+ grub_efi_guid_t boot_memmap_guid = GRUB_EFI_BOOT_MEMMAP_GUID; -+ grub_efi_guid_t initrd_media_guid = GRUB_EFI_INITRD_MEDIA_GUID; -+ -+ setup_screen_info(); -+ -+ b = grub_efi_system_table->boot_services; -+ -+ grub_dprintf ("loongson", "ramdisk_addr:0x%"PRIxGRUB_UINT64_T", \ -+ size:0x%"PRIxGRUB_UINT64_T"\n", -+ kernel_params->ramdisk_addr, -+ kernel_params->ramdisk_size); -+#if 0 -+ char string[64]; -+ -+ /* Set initrd info to cmdline*/ -+ if (kernel_params->ramdisk_addr && kernel_params->ramdisk_size) -+ { -+ grub_printf ( "Initrd @ %p-%p\n", -+ (void *) kernel_params->ramdisk_addr, -+ (void *) (kernel_params->ramdisk_addr + kernel_params->ramdisk_size)); -+ /* initrd */ -+ grub_snprintf (string, -+ sizeof (GRUB_INITRD_STRING), -+ "initrd=0x%lx,0x%lx", -+ ((grub_uint64_t) kernel_params->ramdisk_addr & 0xffffffff), -+ (grub_uint64_t) kernel_params->ramdisk_size); -+ *(char*) ((grub_addr_t) kernel_params->linux_args + kernel_params->ramdisk_args_len - 2) = ' '; -+ grub_memcpy ((char*) ((grub_addr_t) kernel_params->linux_args + kernel_params->ramdisk_args_len - 1), -+ string, sizeof (GRUB_INITRD_STRING)); -+ } -+#else -+ /* Set initrd info to system table*/ -+ if (kernel_params->ramdisk_addr && kernel_params->ramdisk_size) -+ { -+ tbl = grub_efi_allocate_any_pages (GRUB_EFI_BYTES_TO_PAGES (sizeof(struct efi_initrd))); -+ if (!tbl) -+ return grub_error (GRUB_ERR_IO, "cannot allocate tbl memory"); -+ tbl->base = kernel_params->ramdisk_addr; -+ tbl->size = kernel_params->ramdisk_size; -+ -+ status = b->install_configuration_table (&initrd_media_guid, tbl); -+ if (status != GRUB_EFI_SUCCESS) { -+ grub_error (GRUB_ERR_IO, "failed to install initrd media"); -+ goto free_tbl; -+ } -+ } -+#endif -+ -+ tmp.map_size = 0; -+ status = grub_efi_get_memory_map (&tmp.map_size, NULL, &tmp.map_key, -+ &tmp.desc_size, &tmp.desc_ver); -+ if (status != 0) { -+ grub_error (GRUB_ERR_IO, "cannot get memory map"); -+ goto uninstall_initrd_table; -+ } -+ size = tmp.map_size + tmp.desc_size * GRUB_EFI_MMAP_NR_SLACK_SLOTS; -+ m = grub_efi_allocate_any_pages (GRUB_EFI_BYTES_TO_PAGES (sizeof(*m) + size)); -+ if (!m) { -+ grub_error (GRUB_ERR_IO, "cannot allocate m memory"); -+ goto uninstall_initrd_table; -+ } -+ -+ status = b->install_configuration_table (&boot_memmap_guid, m); -+ if (status != GRUB_EFI_SUCCESS) { -+ grub_error (GRUB_ERR_IO, "failed to install boot memmap"); -+ goto free_m; -+ } -+ -+ m->buff_size = m->map_size = size; -+ if (grub_efi_get_memory_map (&m->map_size, m->map, -+ &m->map_key, &m->desc_size, -+ &m->desc_ver) <= 0) -+ { -+ grub_error (GRUB_ERR_IO, "cannot get EFI memory map"); -+ goto uninstall_mem_table; -+ } -+ -+ mmap_size = grub_efi_find_mmap_size (); -+ if (! mmap_size) -+ goto uninstall_mem_table; -+ -+ mmap_buf = grub_efi_allocate_any_pages (GRUB_EFI_BYTES_TO_PAGES (mmap_size)); -+ err = grub_efi_finish_boot_services (&mmap_size, mmap_buf, NULL, -+ &desc_size, &desc_version); -+ if (err) { -+ grub_error (GRUB_ERR_IO, "failed to finish boot services"); -+ goto free_map; -+ } -+ -+ return 0; -+ -+free_map: -+ if (mmap_buf) -+ grub_efi_free_pages ((grub_addr_t) mmap_buf, -+ GRUB_EFI_BYTES_TO_PAGES (mmap_size)); -+ -+uninstall_mem_table: -+ b->install_configuration_table (&boot_memmap_guid, NULL); -+ -+free_m: -+ if (m) -+ grub_efi_free_pages ((grub_addr_t) m, -+ GRUB_EFI_BYTES_TO_PAGES (sizeof(*m) + size)); -+ -+uninstall_initrd_table: -+ if (kernel_params->ramdisk_addr && kernel_params->ramdisk_size) -+ b->install_configuration_table (&initrd_media_guid, NULL); -+ -+free_tbl: -+ if (kernel_params->ramdisk_addr && kernel_params->ramdisk_size) { -+ if (tbl) -+ grub_efi_free_pages ((grub_addr_t) tbl, -+ GRUB_EFI_BYTES_TO_PAGES (sizeof(struct efi_initrd))); -+ } -+ -+ return grub_error(GRUB_ERR_BAD_OS, "failed to V40 boot"); -+} -+ -+static grub_err_t -+allocate_fdt_and_exit_boot (struct linux_loongarch64_kernel_params *kernel_params) -+{ -+ int node, retval; -+ grub_err_t err; -+ unsigned int size; -+ grub_efi_uintn_t mmap_size; -+ grub_efi_uintn_t desc_size; -+ grub_efi_uint32_t desc_version; -+ grub_efi_memory_descriptor_t *mmap_buf; -+ -+ size = GRUB_FDT_EMPTY_TREE_SZ + FDT_ADDR_SIZE_EXTRA + GRUB_EFI_LINUX_FDT_EXTRA_SPACE; -+ -+ kernel_params->fdt = grub_efi_allocate_any_pages (GRUB_EFI_BYTES_TO_PAGES (size)); -+ if (!kernel_params->fdt) -+ return GRUB_ERR_OUT_OF_MEMORY; -+ -+ grub_fdt_create_empty_tree (kernel_params->fdt, size); -+ grub_fdt_set_prop32 (kernel_params->fdt, 0, FDT_ADDR_CELLS_STRING, 2); -+ grub_fdt_set_prop32 (kernel_params->fdt, 0, FDT_SIZE_CELLS_STRING, 2); -+ -+ node = grub_fdt_find_subnode (kernel_params->fdt, 0, "chosen"); -+ if (node < 0) -+ node = grub_fdt_add_subnode (kernel_params->fdt, 0, "chosen"); -+ if (node < 1) -+ goto failure; -+ -+ grub_dprintf ("loongson", "command_line %s, len %ld\n", (char *)kernel_params->linux_args, -+ grub_strlen(kernel_params->linux_args) + 1); -+ if ((kernel_params->linux_args != NULL) && (grub_strlen(kernel_params->linux_args) > 0)) { -+ retval = grub_fdt_set_prop (kernel_params->fdt, node, "bootargs", kernel_params->linux_args, -+ grub_strlen(kernel_params->linux_args) + 1); -+ if (retval) -+ goto failure; -+ } -+ -+ /* Set initrd info */ -+ if (kernel_params->ramdisk_addr && kernel_params->ramdisk_size) -+ { -+ grub_dprintf ("linux", "Initrd @ %p-%p\n", -+ (void *) kernel_params->ramdisk_addr, -+ (void *) (kernel_params->ramdisk_addr + kernel_params->ramdisk_size)); -+ -+ retval = grub_fdt_set_prop64 (kernel_params->fdt, node, "linux,initrd-start", -+ kernel_params->ramdisk_addr); -+ if (retval) -+ goto failure; -+ retval = grub_fdt_set_prop64 (kernel_params->fdt, node, "linux,initrd-end", -+ (kernel_params->ramdisk_addr + kernel_params->ramdisk_size)); -+ if (retval) -+ goto failure; -+ } -+ -+ node = grub_fdt_find_subnode (kernel_params->fdt, 0, "chosen"); -+ retval = grub_fdt_set_prop64 (kernel_params->fdt, node, "linux,uefi-system-table", -+ (grub_uint64_t)grub_efi_system_table); -+ if (retval) -+ goto failure; -+ -+ mmap_size = grub_efi_find_mmap_size (); -+ if (! mmap_size) -+ return grub_errno; -+ mmap_buf = grub_efi_allocate_any_pages (GRUB_EFI_BYTES_TO_PAGES (mmap_size)); -+ if (! mmap_buf) -+ return grub_error (GRUB_ERR_IO, "cannot allocate memory map"); -+ err = grub_efi_finish_boot_services (&mmap_size, mmap_buf, NULL, -+ &desc_size, &desc_version); -+ if (err) -+ return err; -+ -+ if (!mmap_buf || !mmap_size || !desc_size) -+ return GRUB_ERR_BAD_ARGUMENT; -+ -+ retval = grub_fdt_set_prop64 (kernel_params->fdt, node, "linux,uefi-mmap-start", -+ (grub_uint64_t)mmap_buf); -+ if (retval) -+ goto failure; -+ -+ retval = grub_fdt_set_prop32 (kernel_params->fdt, node, "linux,uefi-mmap-size", -+ mmap_size); -+ if (retval) -+ goto failure; -+ -+ retval = grub_fdt_set_prop32 (kernel_params->fdt, node, "linux,uefi-mmap-desc-size", -+ desc_size); -+ if (retval) -+ goto failure; -+ -+ retval = grub_fdt_set_prop32 (kernel_params->fdt, node, "linux,uefi-mmap-desc-ver", -+ desc_version); -+ if (retval) -+ goto failure; -+ -+ return GRUB_ERR_NONE; -+ -+failure: -+ if (!kernel_params->fdt) { -+ return GRUB_ERR_BAD_OS; -+ } -+ grub_efi_free_pages ((grub_addr_t) kernel_params->fdt, -+ GRUB_EFI_BYTES_TO_PAGES (grub_fdt_get_totalsize (kernel_params->fdt))); -+ kernel_params->fdt = NULL; -+ return grub_error(GRUB_ERR_BAD_OS, "failed to install/update FDT"); -+} -+ -+static void -+grub_linux_loongarch_elf_make_argv (struct linux_loongarch64_kernel_params *kernel_params) -+{ -+ static void* linux_args_addr; -+ int size; -+ grub_uint64_t *linux_argv; -+ char *args, *p, *linux_args; -+ int i, argc; -+ grub_err_t err; -+ -+ argc = kernel_params->linux_argc; -+ args = kernel_params->linux_args; -+ -+ /* new size */ -+ p = args; -+ size = (argc + 3 + 1) * sizeof (grub_uint64_t); /* orig arguments */ -+ for (i = 0; i < argc; i++) -+ { -+ size += ALIGN_UP (grub_strlen (p) + 1, 4); -+ p += grub_strlen (p) + 1; -+ } -+ -+ if (kernel_params->ramdisk_addr && kernel_params->ramdisk_size) -+ { -+ size += ALIGN_UP (sizeof (GRUB_RD_START_STRING), 4) \ -+ + ALIGN_UP (sizeof (GRUB_RD_SIZE_STRING), 4) \ -+ + ALIGN_UP (sizeof (GRUB_INITRD_STRING), 4); -+ } -+ size = ALIGN_UP (size, 8); -+ -+ /* alloc memory */ -+ linux_args_addr = grub_linux_loongarch_alloc_virtual_mem_align (size, 8, &err); -+ -+ linux_argv = linux_args_addr; -+ linux_args = (char *)(linux_argv + (argc + 1 + 3)); -+ p = args; -+ for (i = 0; i < argc; i++) -+ { -+ grub_memcpy (linux_args, p, grub_strlen (p) + 1); -+ *linux_argv = (grub_uint64_t) (grub_addr_t) linux_args; -+ linux_argv++; -+ linux_args += ALIGN_UP (grub_strlen (p) + 1, 4); -+ p += grub_strlen (p) + 1; -+ } -+ -+ if (kernel_params->ramdisk_addr && kernel_params->ramdisk_size) -+ { -+ /* rd_start */ -+ grub_snprintf (linux_args, -+ sizeof (GRUB_RD_START_STRING), -+ "rd_start=0x%lx", -+ (grub_uint64_t) kernel_params->ramdisk_addr); -+ *linux_argv = (grub_uint64_t) (grub_addr_t) linux_args; -+ linux_argv++; -+ linux_args += ALIGN_UP (sizeof (GRUB_RD_START_STRING), 4); -+ kernel_params->linux_argc++; -+ -+ /* rd_size */ -+ grub_snprintf (linux_args, -+ sizeof (GRUB_RD_SIZE_STRING), -+ "rd_size=0x%lx", -+ (grub_uint64_t) kernel_params->ramdisk_size); -+ *linux_argv = (grub_uint64_t) (grub_addr_t) linux_args; -+ linux_argv++; -+ linux_args += ALIGN_UP (sizeof (GRUB_RD_SIZE_STRING), 4); -+ kernel_params->linux_argc++; -+ -+ /* initrd */ -+ grub_snprintf (linux_args, -+ sizeof (GRUB_INITRD_STRING), -+ "initrd=0x%lx,0x%lx", -+ ((grub_uint64_t) kernel_params->ramdisk_addr & 0xffffffff), -+ (grub_uint64_t) kernel_params->ramdisk_size); -+ *linux_argv = (grub_uint64_t) (grub_addr_t) linux_args; -+ linux_argv++; -+ linux_args += ALIGN_UP (sizeof (GRUB_INITRD_STRING), 4); -+ kernel_params->linux_argc++; -+ } -+ -+ /* Reserve space for initrd arguments. */ -+ *linux_argv = 0; -+ -+ grub_free (kernel_params->linux_args); -+ kernel_params->linux_argv = (grub_addr_t) linux_args_addr; -+} -+ -+grub_err_t -+grub_linux_loongarch_elf_linux_boot_image (struct linux_loongarch64_kernel_params -+ *kernel_params) -+{ -+ struct boot_params_interface *boot_params = NULL; -+ struct grub_relocator64_state state; -+ grub_err_t err; -+ -+ /* linux kernel type is ELF */ -+ grub_memset (&state, 0, sizeof (state)); -+ -+ state.jumpreg = 1; -+ state.gpr[1] = kernel_params->kernel_addr; /* ra */ -+ if (grub_linux_loongarch_elf_get_boot_params (&boot_params) == 0) -+ { -+ grub_dprintf ("loongson", "V40 boot\n"); -+ if (allocate_memmap_and_exit_boot(kernel_params) != GRUB_ERR_NONE) -+ return grub_errno; -+ state.gpr[4] = 1 << FLAGS_EFI_SUPPORT_BIT; /* a0 = flag */ -+ state.gpr[5] = (grub_uint64_t)kernel_params->linux_args; /* a1 = cmdline */ -+ state.gpr[6] = (grub_uint64_t)grub_efi_system_table; /* a2 = system_table */ -+ } else { -+ grub_dprintf ("loongson", "BPI boot\n"); -+ grub_linux_loongarch_elf_make_argv (kernel_params); -+ state.gpr[4] = kernel_params->linux_argc; /* a0 = argc */ -+ state.gpr[5] = kernel_params->linux_argv; /* a1 = args */ -+ state.gpr[6] = (grub_uint64_t) boot_params; /* a2 = envp */ -+ err = grub_linux_loongarch_elf_boot_params (boot_params); -+ if (err) -+ return err; -+ } -+ -+ /* Boot the ELF kernel */ -+ grub_relocator64_boot (relocator, state); -+ -+ return GRUB_ERR_NONE; -+} -+ -+void* -+grub_linux_loongarch_alloc_virtual_mem_addr (grub_addr_t addr, -+ grub_size_t size, -+ grub_err_t *err) -+{ -+ relocator = grub_relocator_new (); -+ if (!relocator) -+ return NULL; -+ -+ grub_relocator_chunk_t ch; -+ *err = grub_relocator_alloc_chunk_addr (relocator, &ch, -+ grub_vtop ((void *) addr), -+ size); -+ if (*err) -+ return NULL; -+ return get_virtual_current_address (ch); -+} -+ -+void* -+grub_linux_loongarch_alloc_virtual_mem_align (grub_size_t size, -+ grub_size_t align, -+ grub_err_t *err) -+{ -+ grub_relocator_chunk_t ch; -+ -+ *err = grub_relocator_alloc_chunk_align (relocator, &ch, -+ 0x3000000, (0xffffffff - size) + 1, -+ size, align, -+ GRUB_RELOCATOR_PREFERENCE_LOW, 0); -+ return get_virtual_current_address (ch); -+} -+ -+void* -+grub_linux_loongarch_alloc_initrd_mem_align (grub_size_t size, -+ grub_size_t align, -+ grub_err_t *err) -+{ -+ grub_relocator_chunk_t ch; -+ -+ /* Firstly try to allocate from memory higher than 256MB */ -+ *err = grub_relocator_alloc_chunk_align (relocator, &ch, -+ 0x10000000, (0xffffffff - size) + 1, size, align, -+ GRUB_RELOCATOR_PREFERENCE_LOW, 0); -+ if (*err != GRUB_ERR_NONE) -+ { -+ /* Failed, try to allocate in range 0 ~ 256MB */ -+ *err = grub_relocator_alloc_chunk_align (relocator, &ch, -+ 0, (0xfffffff - size) + 1, size, align, -+ GRUB_RELOCATOR_PREFERENCE_HIGH, 0); -+ } -+ return get_virtual_current_address (ch); -+} -+ -+int -+grub_linux_loongarch_elf_get_boot_params (struct boot_params_interface **boot_params) -+{ -+ grub_efi_configuration_table_t *tables; -+ grub_efi_guid_t bpi_guid = GRUB_EFI_LOONGSON_BPI_TABLE_GUID; -+ unsigned int i; -+ int found = 0; -+ -+ /* Look for Loongson BPI in UEFI config tables. */ -+ tables = grub_efi_system_table->configuration_table; -+ -+ for (i = 0; i < grub_efi_system_table->num_table_entries; i++) -+ if (grub_memcmp (&tables[i].vendor_guid, &bpi_guid, sizeof (bpi_guid)) == 0) -+ { -+ *boot_params = tables[i].vendor_table; -+ char *p = (char*) &((*boot_params)->signature); -+ if (grub_strncmp (p, "BPI", 3) == 0) -+ { -+ found = 1; -+ break; -+ } -+ } -+ return found; -+} -+ -+static grub_uint8_t -+grub_kernel_update_checksum (const grub_uint8_t *buffer, grub_efi_uintn_t length) -+{ -+ grub_uint8_t sum; -+ grub_efi_uintn_t count; -+ -+ for (sum = 0, count = 0; count < length; count++) -+ { -+ sum = (grub_uint8_t) (sum + *(buffer + count)); -+ } -+ -+ return (grub_uint8_t) (0x100 - sum); -+} -+ -+static grub_uint32_t -+grub_efi_loongarch64_memmap_sort (struct memmap array[], -+ grub_uint32_t length, -+ struct loongsonlist_mem_map* bpmem, -+ grub_uint32_t index, -+ grub_uint32_t memtype) -+{ -+ grub_uint64_t tempmemsize = 0; -+ grub_uint32_t j = 0; -+ grub_uint32_t t = 0; -+ -+ for(j = 0; j < length;) -+ { -+ tempmemsize = array[j].mem_size; -+ for(t = j + 1; t < length; t++) -+ { -+ if(array[j].mem_start + tempmemsize == array[t].mem_start) -+ { -+ tempmemsize += array[t].mem_size; -+ } -+ else -+ { -+ break; -+ } -+ } -+ bpmem->map[index].mem_type = memtype; -+ bpmem->map[index].mem_start = array[j].mem_start; -+ bpmem->map[index].mem_size = tempmemsize; -+ grub_dprintf ("loongson", "map[%d]:type %"PRIuGRUB_UINT32_T", start 0x%" -+ PRIxGRUB_UINT64_T", end 0x%"PRIxGRUB_UINT64_T"\n", -+ index, -+ bpmem->map[index].mem_type, -+ bpmem->map[index].mem_start, -+ bpmem->map[index].mem_start+ bpmem->map[index].mem_size -+ ); -+ j = t; -+ index++; -+ } -+ return index; -+} -+ -+grub_err_t -+grub_linux_loongarch_elf_boot_params (struct boot_params_interface *boot_params) -+{ -+ grub_int8_t checksum = 0; -+ grub_err_t err; -+ -+ struct loongsonlist_mem_map *loongson_mem_map = NULL; -+ struct _extention_list_hdr * listpointer = NULL; -+ grub_uint32_t tmp_index = 0; -+ grub_efi_memory_descriptor_t * lsdesc = NULL; -+ -+ grub_uint32_t free_index = 0; -+ grub_uint32_t reserve_index = 0; -+ grub_uint32_t acpi_table_index = 0; -+ grub_uint32_t acpi_nvs_index = 0; -+ -+ grub_efi_uintn_t mmap_size; -+ grub_efi_uintn_t desc_size; -+ grub_efi_memory_descriptor_t *mmap_buf; -+ -+ struct memmap reserve_mem[GRUB_LOONGSON3_BOOT_MEM_MAP_MAX]; -+ struct memmap free_mem[GRUB_LOONGSON3_BOOT_MEM_MAP_MAX]; -+ struct memmap acpi_table_mem[GRUB_LOONGSON3_BOOT_MEM_MAP_MAX]; -+ struct memmap acpi_nvs_mem[GRUB_LOONGSON3_BOOT_MEM_MAP_MAX]; -+ -+ grub_memset (reserve_mem, 0, sizeof(struct memmap) * GRUB_LOONGSON3_BOOT_MEM_MAP_MAX); -+ grub_memset (free_mem, 0, sizeof(struct memmap) * GRUB_LOONGSON3_BOOT_MEM_MAP_MAX); -+ grub_memset (acpi_table_mem, 0, sizeof(struct memmap) * GRUB_LOONGSON3_BOOT_MEM_MAP_MAX); -+ grub_memset (acpi_nvs_mem, 0, sizeof(struct memmap) * GRUB_LOONGSON3_BOOT_MEM_MAP_MAX); -+ -+ /* Check extlist headers */ -+ listpointer = boot_params->extlist; -+ for( ;listpointer != NULL; listpointer = listpointer->next) -+ { -+ char *pl= (char *)&(listpointer->signature); -+ if(grub_strncmp(pl, "MEM", 3) == 0) -+ { -+ loongson_mem_map = (struct loongsonlist_mem_map *)listpointer; -+ break; -+ } -+ } -+ -+ mmap_size = grub_efi_find_mmap_size (); -+ if (! mmap_size) -+ return grub_errno; -+ mmap_buf = grub_efi_allocate_any_pages (GRUB_EFI_BYTES_TO_PAGES (mmap_size)); -+ if (! mmap_buf) -+ return grub_error (GRUB_ERR_IO, "cannot allocate memory map"); -+ -+ err = grub_efi_finish_boot_services (&mmap_size, mmap_buf, NULL, -+ &desc_size, NULL); -+ if (err) -+ return err; -+ -+ if (!mmap_buf || !mmap_size || !desc_size) -+ return -1; -+ -+ /* -+ According to UEFI SPEC,mmap_buf is the accurate Memory Map array \ -+ now we can fill platform specific memory structure. -+ */ -+ for (lsdesc = mmap_buf; lsdesc < (grub_efi_memory_descriptor_t *)((char *)mmap_buf + mmap_size); -+ lsdesc = (grub_efi_memory_descriptor_t *)((char *)lsdesc + desc_size)) -+ { -+ /* System RAM */ -+ if((lsdesc->type != GRUB_EFI_ACPI_RECLAIM_MEMORY) && \ -+ (lsdesc->type != GRUB_EFI_ACPI_MEMORY_NVS) && \ -+ (lsdesc->type != GRUB_EFI_RUNTIME_SERVICES_DATA) && \ -+ (lsdesc->type != GRUB_EFI_RUNTIME_SERVICES_CODE) && \ -+ (lsdesc->type != GRUB_EFI_RESERVED_MEMORY_TYPE) && \ -+ (lsdesc->type != GRUB_EFI_PAL_CODE)) -+ { -+ free_mem[free_index].mem_type = GRUB_ADDRESS_TYPE_SYSRAM; -+ free_mem[free_index].mem_start = (lsdesc->physical_start) & GRUB_EFI_MAX_PHY_ADDRESS; -+ free_mem[free_index].mem_size = lsdesc->num_pages * GRUB_EFI_PAGE_SIZE; -+ free_index++; -+ -+ /*ACPI*/ -+ }else if((lsdesc->type == GRUB_EFI_ACPI_RECLAIM_MEMORY)){ -+ acpi_table_mem[acpi_table_index].mem_type = GRUB_ADDRESS_TYPE_ACPI; -+ acpi_table_mem[acpi_table_index].mem_start = (lsdesc->physical_start) & GRUB_EFI_MAX_PHY_ADDRESS; -+ acpi_table_mem[acpi_table_index].mem_size = lsdesc->num_pages * GRUB_EFI_PAGE_SIZE; -+ acpi_table_index++; -+ }else if((lsdesc->type == GRUB_EFI_ACPI_MEMORY_NVS)){ -+ acpi_nvs_mem[acpi_nvs_index].mem_type = GRUB_ADDRESS_TYPE_NVS; -+ acpi_nvs_mem[acpi_nvs_index].mem_start = (lsdesc->physical_start) & GRUB_EFI_MAX_PHY_ADDRESS; -+ acpi_nvs_mem[acpi_nvs_index].mem_size = lsdesc->num_pages * GRUB_EFI_PAGE_SIZE; -+ acpi_nvs_index++; -+ -+ /* Reserve */ -+ }else{ -+ reserve_mem[reserve_index].mem_type = GRUB_ADDRESS_TYPE_RESERVED; -+ reserve_mem[reserve_index].mem_start = (lsdesc->physical_start) & GRUB_EFI_MAX_PHY_ADDRESS; -+ reserve_mem[reserve_index].mem_size = lsdesc->num_pages * GRUB_EFI_PAGE_SIZE; -+ reserve_index++; -+ } -+ } -+ -+ tmp_index = loongson_mem_map->map_count; -+ /*System RAM Sort*/ -+ tmp_index = grub_efi_loongarch64_memmap_sort(free_mem, -+ free_index, -+ loongson_mem_map, -+ tmp_index, -+ GRUB_ADDRESS_TYPE_SYSRAM); -+ /*ACPI Sort*/ -+ tmp_index = grub_efi_loongarch64_memmap_sort(acpi_table_mem, -+ acpi_table_index, -+ loongson_mem_map, -+ tmp_index, -+ GRUB_ADDRESS_TYPE_ACPI); -+ tmp_index = grub_efi_loongarch64_memmap_sort(acpi_nvs_mem, -+ acpi_nvs_index, -+ loongson_mem_map, -+ tmp_index, -+ GRUB_ADDRESS_TYPE_NVS); -+ -+ /*Reserve Sort*/ -+ { -+ grub_uint64_t loongarch_addr; -+ asm volatile ("csrrd %0, 0x181" : "=r" (loongarch_addr)); -+ if ((loongarch_addr & 0xff00000000000000) == 0x9000000000000000) -+ tmp_index = grub_efi_loongarch64_memmap_sort(reserve_mem, -+ reserve_index, -+ loongson_mem_map, -+ tmp_index, -+ GRUB_ADDRESS_TYPE_RESERVED); -+ else -+ tmp_index = grub_efi_loongarch64_memmap_sort(reserve_mem, -+ reserve_index, -+ loongson_mem_map, -+ tmp_index, -+ GRUB_ADDRESS_TYPE_RESERVED + 1); -+ } -+ loongson_mem_map->map_count = tmp_index; -+ loongson_mem_map->header.checksum = 0; -+ -+ checksum = grub_kernel_update_checksum ((grub_uint8_t *) loongson_mem_map, -+ loongson_mem_map->header.length); -+ loongson_mem_map->header.checksum = checksum; -+ -+ return grub_errno; -+} -diff --git a/grub-core/loader/loongarch64/linux.c b/grub-core/loader/loongarch64/linux.c -index 7594ca2..8dae4fe 100644 ---- a/grub-core/loader/loongarch64/linux.c -+++ b/grub-core/loader/loongarch64/linux.c -@@ -1,7 +1,6 @@ --/* linux.c - boot Linux */ - /* - * GRUB -- GRand Unified Bootloader -- * Copyright (C) 2003,2004,2005,2007,2009,2010,2017 Free Software Foundation, Inc. -+ * Copyright (C) 2021 Free Software Foundation, Inc. - * - * GRUB is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by -@@ -17,346 +16,76 @@ - * along with GRUB. If not, see . - */ - --#include --#include --#include --#include - #include --#include --#include - #include - #include --#include --#include --#include --#include - #include - #include - #include --#include -+#include - #include - - GRUB_MOD_LICENSE ("GPLv3+"); - --#pragma GCC diagnostic ignored "-Wcast-align" -- --typedef unsigned long size_t; -+#define INITRD_MAX_ADDRESS_OFFSET (32ULL * 1024 * 1024 * 1024) - --#define FDT_ADDR_CELLS_STRING "#address-cells" --#define FDT_SIZE_CELLS_STRING "#size-cells" --#define FDT_ADDR_SIZE_EXTRA ((2 * grub_fdt_prop_entry_size (sizeof(grub_uint32_t))) + \ -- sizeof (FDT_ADDR_CELLS_STRING) + \ -- sizeof (FDT_SIZE_CELLS_STRING)) -+static struct linux_loongarch64_kernel_params kernel_params; - -+static grub_addr_t phys_addr; - static grub_dl_t my_mod; - static int loaded; --static int initrd_loaded = 0; --static grub_size_t linux_size; -- --static struct grub_relocator *relocator; --static grub_addr_t target_addr, entry_addr; --static int linux_argc; --static grub_uint8_t *linux_args_addr; --static grub_off_t rd_addr_arg_off, rd_size_arg_off, initrd_addr_arg_off; -- --static void *fdt; --static int is_fdt_boot; --static grub_addr_t initrd_start, initrd_end; --static char *fdt_linux_args; -+static int is_bpi_boot; -+static int grub_loongarch_linux_type = GRUB_LOONGARCH_LINUX_BAD; - - static grub_err_t --allocate_fdt_and_exit_boot (void) -+grub_linux_boot (void) - { -- int node, retval; -- grub_err_t err; -- unsigned int size; -- grub_efi_uintn_t mmap_size; -- grub_efi_uintn_t desc_size; -- grub_efi_uint32_t desc_version; -- grub_efi_memory_descriptor_t *mmap_buf; -- -- size = GRUB_FDT_EMPTY_TREE_SZ + FDT_ADDR_SIZE_EXTRA + GRUB_EFI_LINUX_FDT_EXTRA_SPACE; -- -- fdt = grub_efi_allocate_any_pages (GRUB_EFI_BYTES_TO_PAGES (size)); -- if (!fdt) -- return GRUB_ERR_OUT_OF_MEMORY; -- -- grub_fdt_create_empty_tree (fdt, size); -- grub_fdt_set_prop32 (fdt, 0, FDT_ADDR_CELLS_STRING, 2); -- grub_fdt_set_prop32 (fdt, 0, FDT_SIZE_CELLS_STRING, 2); -- -- node = grub_fdt_find_subnode (fdt, 0, "chosen"); -- if (node < 0) -- node = grub_fdt_add_subnode (fdt, 0, "chosen"); -- if (node < 1) -- goto failure; -- -- grub_dprintf ("loongson", "command_line %s, len %ld\n", -- fdt_linux_args, grub_strlen(fdt_linux_args) + 1); -- if ((fdt_linux_args != NULL) && (grub_strlen(fdt_linux_args) > 0)) { -- retval = grub_fdt_set_prop (fdt, node, "bootargs", fdt_linux_args, -- grub_strlen(fdt_linux_args) + 1); -- if (retval) -- goto failure; -- } - -- /* Set initrd info */ -- if (initrd_start && initrd_end > initrd_start) -- { -- grub_dprintf ("linux", "Initrd @ %p-%p\n", -- (void *) initrd_start, (void *) initrd_end); -- -- retval = grub_fdt_set_prop64 (fdt, node, "linux,initrd-start", -- initrd_start); -- if (retval) -- goto failure; -- retval = grub_fdt_set_prop64 (fdt, node, "linux,initrd-end", -- initrd_end); -- if (retval) -- goto failure; -+ if (grub_loongarch_linux_type == GRUB_LOONGARCH_LINUX_EFI) { -+ if (finalize_efi_params_linux (&kernel_params) != GRUB_ERR_NONE) -+ return grub_errno; -+ return (grub_arch_efi_linux_boot_image((grub_addr_t) kernel_params.kernel_addr, -+ kernel_params.kernel_size, -+ kernel_params.linux_args)); - } -- -- node = grub_fdt_find_subnode (fdt, 0, "chosen"); -- retval = grub_fdt_set_prop64 (fdt, node, "linux,uefi-system-table", -- (grub_uint64_t)grub_efi_system_table); -- if (retval) -- goto failure; -- -- mmap_size = grub_efi_find_mmap_size (); -- if (! mmap_size) -- return grub_errno; -- mmap_buf = grub_efi_allocate_any_pages (GRUB_EFI_BYTES_TO_PAGES (mmap_size)); -- if (! mmap_buf) -- return grub_error (GRUB_ERR_IO, "cannot allocate memory map"); -- err = grub_efi_finish_boot_services (&mmap_size, mmap_buf, NULL, -- &desc_size, &desc_version); -- if (err) -- return err; -- -- if (!mmap_buf || !mmap_size || !desc_size) -- return GRUB_ERR_BAD_ARGUMENT; -- -- retval = grub_fdt_set_prop64 (fdt, node, "linux,uefi-mmap-start", -- (grub_uint64_t)mmap_buf); -- if (retval) -- goto failure; -- -- retval = grub_fdt_set_prop32 (fdt, node, "linux,uefi-mmap-size", -- mmap_size); -- if (retval) -- goto failure; -- -- retval = grub_fdt_set_prop32 (fdt, node, "linux,uefi-mmap-desc-size", -- desc_size); -- if (retval) -- goto failure; -- -- retval = grub_fdt_set_prop32 (fdt, node, "linux,uefi-mmap-desc-ver", -- desc_version); -- if (retval) -- goto failure; -- -- return GRUB_ERR_NONE; -- --failure: -- if (!fdt) { -- return GRUB_ERR_BAD_OS; -+ if (grub_loongarch_linux_type == GRUB_LOONGARCH_LINUX_ELF) { -+ return grub_linux_loongarch_elf_linux_boot_image (&kernel_params); - } -- grub_efi_free_pages ((grub_addr_t) fdt, -- GRUB_EFI_BYTES_TO_PAGES (grub_fdt_get_totalsize (fdt))); -- fdt = NULL; -- return grub_error(GRUB_ERR_BAD_OS, "failed to install/update FDT"); --} -- --static grub_err_t --allocate_boot_params_and_exit_boot (void) --{ -- grub_efi_uintn_t mmap_size; -- grub_efi_uintn_t desc_size; -- grub_efi_uint32_t desc_version; -- grub_efi_memory_descriptor_t *mmap_buf; -- grub_efi_memory_descriptor_t * lsdesc = NULL; -- grub_err_t err; -- struct boot_params_interface * boot_params; -- mem_map_v1 * mem_map_v1_table = NULL; -- unsigned long bpi_version = 0; -- grub_int8_t checksum = 0; -- grub_uint32_t tmp_index = 0; -- grub_uint32_t free_index = 0; -- grub_uint32_t reserve_index = 0; -- grub_uint32_t acpi_table_index = 0; -- grub_uint32_t acpi_nvs_index = 0; -- -- struct memmap_v1 reserve_mem_v1[GRUB_EFI_LOONGSON_MMAP_MAX]; -- struct memmap_v1 free_mem_v1[GRUB_EFI_LOONGSON_MMAP_MAX]; -- struct memmap_v1 acpi_table_mem_v1[GRUB_EFI_LOONGSON_MMAP_MAX]; -- struct memmap_v1 acpi_nvs_mem_v1[GRUB_EFI_LOONGSON_MMAP_MAX]; -- -- grub_memset(reserve_mem_v1, 0, sizeof(struct memmap_v1) * GRUB_EFI_LOONGSON_MMAP_MAX); -- grub_memset(free_mem_v1, 0, sizeof(struct memmap_v1) * GRUB_EFI_LOONGSON_MMAP_MAX); -- grub_memset(acpi_table_mem_v1, 0, sizeof(struct memmap_v1) * GRUB_EFI_LOONGSON_MMAP_MAX); -- grub_memset(acpi_nvs_mem_v1, 0, sizeof(struct memmap_v1) * GRUB_EFI_LOONGSON_MMAP_MAX); -- -- boot_params = (struct boot_params_interface *)grub_efi_loongson_get_boot_params(); -- -- ext_list * listpointer = NULL; -- /* Check extlist headers */ -- listpointer = boot_params->extlist; -- for( ;listpointer != NULL; listpointer = listpointer->next) -- { -- char *pl= (char *)&(listpointer->signature); -- if(grub_strncmp(pl, "MEM", 3) == 0) -- { -- mem_map_v1_table = (mem_map_v1 *)listpointer; -- break; -- } -- } -- -- mmap_size = grub_efi_find_mmap_size (); -- if (! mmap_size) -- return grub_errno; -- mmap_buf = grub_efi_allocate_any_pages (GRUB_EFI_BYTES_TO_PAGES (mmap_size)); -- if (! mmap_buf) -- return grub_error (GRUB_ERR_IO, "cannot allocate memory map"); -- err = grub_efi_finish_boot_services (&mmap_size, mmap_buf, NULL, -- &desc_size, &desc_version); -- if (err) -- return err; -- -- if (!mmap_buf || !mmap_size || !desc_size) -- return GRUB_ERR_BAD_ARGUMENT; -- -- char *p = (char *)&(boot_params->signature); -- bpi_version = grub_efi_get_bpi_version(p); -- grub_dprintf("loongson", "get bpi version:%ld\n", bpi_version); -- -- if (bpi_version <= GRUB_EFI_BPI_VER_V2) -- { -- /* -- According to UEFI SPEC,mmap_buf is the accurate Memory Map array \ -- now we can fill platform specific memory structure. -- */ -- for(lsdesc = mmap_buf; lsdesc < (grub_efi_memory_descriptor_t *)((char *)mmap_buf + mmap_size); -- lsdesc = (grub_efi_memory_descriptor_t *)((char *)lsdesc + desc_size)) -- { -- grub_dprintf("loongson", "type:%d, phy_start:0x%lx, phy_end:0x%lx\n", lsdesc->type, -- lsdesc->physical_start, lsdesc->physical_start + lsdesc->num_pages * GRUB_EFI_PAGE_SIZE); -- -- /* System RAM */ -- if ((lsdesc->type != GRUB_EFI_ACPI_RECLAIM_MEMORY) && \ -- (lsdesc->type != GRUB_EFI_ACPI_MEMORY_NVS) && \ -- (lsdesc->type != GRUB_EFI_RUNTIME_SERVICES_DATA) && \ -- (lsdesc->type != GRUB_EFI_RUNTIME_SERVICES_CODE) && \ -- (lsdesc->type != GRUB_EFI_RESERVED_MEMORY_TYPE) && \ -- (lsdesc->type != GRUB_EFI_PAL_CODE)) -- { -- free_mem_v1[free_index].memtype = GRUB_EFI_LOONGSON_SYSTEM_RAM; -- free_mem_v1[free_index].memstart = (lsdesc->physical_start) & GRUB_EFI_MAX_PHY_ADDRESS; -- free_mem_v1[free_index].memsize = lsdesc->num_pages * GRUB_EFI_PAGE_SIZE; -- free_index++; -- -- /*ACPI*/ -- } else if ((lsdesc->type == GRUB_EFI_ACPI_RECLAIM_MEMORY)){ -- acpi_table_mem_v1[acpi_table_index].memtype = GRUB_EFI_LOONGSON_ACPI_TABLE; -- acpi_table_mem_v1[acpi_table_index].memstart = (lsdesc->physical_start) & GRUB_EFI_MAX_PHY_ADDRESS; -- acpi_table_mem_v1[acpi_table_index].memsize = lsdesc->num_pages * GRUB_EFI_PAGE_SIZE; -- acpi_table_index++; -- } else if ((lsdesc->type == GRUB_EFI_ACPI_MEMORY_NVS)){ -- acpi_nvs_mem_v1[acpi_nvs_index].memtype = GRUB_EFI_LOONGSON_ACPI_NVS; -- acpi_nvs_mem_v1[acpi_nvs_index].memstart = (lsdesc->physical_start) & GRUB_EFI_MAX_PHY_ADDRESS; -- acpi_nvs_mem_v1[acpi_nvs_index].memsize = lsdesc->num_pages * GRUB_EFI_PAGE_SIZE; -- acpi_nvs_index++; -- -- /* Reserve */ -- } else { -- reserve_mem_v1[reserve_index].memtype = GRUB_EFI_LOONGSON_MEMORY_RESERVED; -- reserve_mem_v1[reserve_index].memstart = (lsdesc->physical_start) & GRUB_EFI_MAX_PHY_ADDRESS; -- reserve_mem_v1[reserve_index].memsize = lsdesc->num_pages * GRUB_EFI_PAGE_SIZE; -- reserve_index++; -- } -- } -- -- tmp_index = mem_map_v1_table->mapcount; -- /*System RAM Sort*/ -- tmp_index = grub_efi_loongson_memmap_v1_sort(free_mem_v1, free_index, mem_map_v1_table, -- tmp_index, GRUB_EFI_LOONGSON_SYSTEM_RAM); -- /*ACPI Sort*/ -- tmp_index = grub_efi_loongson_memmap_v1_sort(acpi_table_mem_v1, acpi_table_index, -- mem_map_v1_table, tmp_index, GRUB_EFI_LOONGSON_ACPI_TABLE); -- tmp_index = grub_efi_loongson_memmap_v1_sort(acpi_nvs_mem_v1, acpi_nvs_index, -- mem_map_v1_table, tmp_index, GRUB_EFI_LOONGSON_ACPI_NVS); -- /*Reserve Sort*/ -- { -- grub_uint64_t loongarch_addr; -- asm volatile ("csrrd %0, 0x181" : "=r" (loongarch_addr)); -- if ((loongarch_addr & 0xff00000000000000) == 0x9000000000000000) -- tmp_index = grub_efi_loongson_memmap_v1_sort(reserve_mem_v1, reserve_index, -- mem_map_v1_table, tmp_index, -- GRUB_EFI_LOONGSON_MEMORY_RESERVED); -- else -- tmp_index = grub_efi_loongson_memmap_v1_sort(reserve_mem_v1, reserve_index, -- mem_map_v1_table, tmp_index, -- GRUB_EFI_LOONGSON_MEMORY_RESERVED + 1); -- } -- mem_map_v1_table->mapcount = tmp_index; -- mem_map_v1_table->header.checksum = 0; -- -- checksum = grub_efi_loongson_grub_calculatechecksum8((grub_uint8_t *)mem_map_v1_table, -- mem_map_v1_table->header.length); -- mem_map_v1_table->header.checksum = checksum; -- } - - return GRUB_ERR_NONE; - } - - static grub_err_t --grub_linux_boot (void) -+grub_linux_unload (void) - { -- struct grub_relocator64_state state; -- -- grub_memset (&state, 0, sizeof (state)); -- -- /* Boot the kernel. */ -- state.gpr[1] = entry_addr; -- grub_dprintf("loongson", "entry_addr is 0x%lx\n", state.gpr[1]); - -- if (is_fdt_boot == 1) -- { -- if (allocate_fdt_and_exit_boot () != GRUB_ERR_NONE) -- return grub_errno; -+ if (grub_loongarch_linux_type == GRUB_LOONGARCH_LINUX_EFI) { -+ if (kernel_params.ramdisk_addr) -+ grub_efi_free_pages ((grub_efi_physical_address_t) kernel_params.ramdisk_addr, -+ GRUB_EFI_BYTES_TO_PAGES (kernel_params.ramdisk_size)); -+ kernel_params.ramdisk_size = 0; - -- state.gpr[4] = 1 << FLAGS_EFI_SUPPORT_BIT; -- state.gpr[5] = (grub_uint64_t)fdt; -- state.gpr[6] = 0; -- } else { -- state.gpr[4] = linux_argc; -- state.gpr[5] = (grub_addr_t) linux_args_addr; -- state.gpr[6] = (grub_uint64_t)grub_efi_loongson_get_boot_params(); -- -- if (allocate_boot_params_and_exit_boot () != GRUB_ERR_NONE) -- return grub_errno; -+ if (kernel_params.kernel_addr) -+ grub_efi_free_pages ((grub_addr_t) kernel_params.kernel_addr, -+ GRUB_EFI_BYTES_TO_PAGES (kernel_params.kernel_size)); -+ kernel_params.kernel_addr = 0; - } -- grub_dprintf("loongson", "gpr[4]:%ld, gpr[5]:0x%lx, gpr[6]:0x%lx\n", -- state.gpr[4], state.gpr[5], state.gpr[6]); - -- state.jumpreg = 1; -- grub_relocator64_boot (relocator, state); -- -- return GRUB_ERR_NONE; --} -+ if (grub_loongarch_linux_type == GRUB_LOONGARCH_LINUX_ELF) { -+ grub_free (kernel_params.linux_args); -+ kernel_params.linux_args = 0; -+ grub_linux_loongarch_elf_relocator_unload (); -+ } - --static grub_err_t --grub_linux_unload (void) --{ -- grub_relocator_unload (relocator); - grub_dl_unref (my_mod); - loaded = 0; -+ grub_loongarch_linux_type = GRUB_LOONGARCH_LINUX_BAD; - - return GRUB_ERR_NONE; - } - --static grub_err_t --grub_linux_load64 (grub_elf_t elf, const char *filename) -+grub_err_t -+grub_linux_loongarch_elf_load_kernel (grub_elf_t elf, const char *filename) - { - Elf64_Addr base; - grub_err_t err; -@@ -365,16 +94,14 @@ grub_linux_load64 (grub_elf_t elf, const char *filename) - int flag; - - /* Linux's entry point incorrectly contains a virtual address. */ -- entry_addr = elf->ehdr.ehdr64.e_entry; -- grub_dprintf("loongson", "entry address = 0x%lx\n", entry_addr); -+ kernel_params.kernel_addr = elf->ehdr.ehdr64.e_entry; -+ kernel_params.kernel_size = grub_elf64_size (elf, &base, 0); - -- linux_size = grub_elf64_size (elf, &base, 0); -- grub_dprintf("loongson", "base = 0x%lx\n", base); -- -- if (linux_size == 0) -+ if (kernel_params.kernel_size == 0) - return grub_errno; -- target_addr = base; -- linux_size = ALIGN_UP (base + linux_size - base, 8); -+ -+ phys_addr = base; -+ kernel_params.kernel_size = ALIGN_UP (base + kernel_params.kernel_size - base, 8); - - asm volatile ("csrrd %0, 0x181" : "=r" (addr)); - if (addr & 0x1) { -@@ -382,157 +109,163 @@ grub_linux_load64 (grub_elf_t elf, const char *filename) - } else { - flag = GRUB_ELF_LOAD_FLAGS_30BITS; - base &= ~ELF64_LOADMASK; -- entry_addr &= ~ELF64_LOADMASK; -+ kernel_params.kernel_addr &= ~ELF64_LOADMASK; - } - -- relocator = grub_relocator_new (); -- if (!relocator) -- return grub_errno; -- -- { -- grub_relocator_chunk_t ch; -- err = grub_relocator_alloc_chunk_addr (relocator, &ch, -- grub_vtop ((void *) target_addr), -- linux_size); -- if (err) -- return err; -- playground = get_virtual_current_address (ch); -- } -+ playground = grub_linux_loongarch_alloc_virtual_mem_addr (phys_addr, -+ kernel_params.kernel_size, -+ &err); -+ if (playground == NULL) -+ return err; - - /* Now load the segments into the area we claimed. */ -- return grub_elf64_load (elf, filename, playground - base, flag, 0, 0); -+ return grub_elf64_load (elf, filename, playground - base, -+ flag, 0, 0); - } - - static grub_err_t - grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)), - int argc, char *argv[]) - { -- grub_elf_t elf = 0; -+ grub_file_t file = 0; -+ struct linux_arch_kernel_header lh; -+ struct boot_params_interface *boot_params = NULL; -+ grub_elf_t elf = NULL; - grub_err_t err; -- int args_size = 0; -+ grub_size_t cmdline_size; -+ int i; - - grub_dl_ref (my_mod); - -- if (argc == 0) -- { -- return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected")); -- } -- -- elf = grub_elf_open (argv[0], GRUB_FILE_TYPE_LINUX_KERNEL); -- if (! elf) -- return grub_errno; -+ /* Release the previously used memory. */ -+ grub_loader_unset (); - -- if (elf->ehdr.ehdr64.e_type != ET_EXEC) -+ if (argc == 0) - { -- grub_elf_close (elf); -- return grub_error (GRUB_ERR_UNKNOWN_OS, -- N_("this ELF file is not of the right type")); -+ grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected")); -+ goto fail; - } - -- /* Release the previously used memory. */ -- grub_loader_unset (); -- loaded = 0; -- -- if (grub_elf_is_elf64 (elf)) -- err = grub_linux_load64 (elf, argv[0]); -- else -- err = grub_error (GRUB_ERR_BAD_OS, N_("invalid arch-dependent ELF magic")); -+ file = grub_file_open (argv[0], GRUB_FILE_TYPE_LINUX_KERNEL); -+ if (!file) -+ goto fail; - -- grub_elf_close (elf); -+ kernel_params.kernel_size = grub_file_size (file); -+ grub_dprintf ("linux", "kernel file size: %" PRIuGRUB_SIZE "\n", -+ kernel_params.kernel_size); - -- if (err) -- return err; -+ if (grub_file_read (file, &lh, sizeof (lh)) < (long) sizeof (lh)) -+ return grub_errno; - -- if (grub_efi_loongson_get_boot_params() == NULL) { -- grub_size_t cmdline_size; -+ if (grub_arch_efi_linux_check_image (&lh) == GRUB_ERR_NONE) { -+ grub_loongarch_linux_type = GRUB_LOONGARCH_LINUX_EFI; -+ } - -- is_fdt_boot = 1; -- cmdline_size = grub_loader_cmdline_size (argc, argv) + sizeof (LINUX_IMAGE); -- fdt_linux_args = grub_malloc (cmdline_size); -- if (!fdt_linux_args) -+ if (grub_loongarch_linux_type != GRUB_LOONGARCH_LINUX_EFI) { -+ elf = grub_elf_file (file, argv[0]); -+ if (elf != NULL) - { -- grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory")); -- goto fail; -+ /* linux kernel type is ELF */ -+ grub_loongarch_linux_type = GRUB_LOONGARCH_LINUX_ELF; -+ if (elf->ehdr.ehdr64.e_type != ET_EXEC) -+ { -+ grub_error (GRUB_ERR_UNKNOWN_OS, -+ N_("this ELF file is not of the right type")); -+ goto fail; -+ } -+ if (elf->ehdr.ehdr64.e_machine != EM_LOONGARCH64) -+ { -+ grub_error (GRUB_ERR_BAD_OS, "invalid magic number"); -+ goto fail; -+ } -+ -+ if (grub_elf_is_elf64 (elf)) -+ { -+ err = grub_linux_loongarch_elf_load_kernel (elf, argv[0]); -+ if (err) -+ goto fail; -+ } else { -+ grub_error (GRUB_ERR_BAD_OS, N_("invalid arch-dependent ELF magic")); -+ goto fail; -+ } -+ grub_dprintf ("linux", "kernel @ %p\n", (void*) elf->ehdr.ehdr64.e_entry); - } -- grub_memcpy (fdt_linux_args, LINUX_IMAGE, sizeof (LINUX_IMAGE)); -- err = grub_create_loader_cmdline (argc, argv, -- fdt_linux_args + sizeof (LINUX_IMAGE) - 1, -- cmdline_size, -- GRUB_VERIFY_KERNEL_CMDLINE); -- if (err) -- goto fail; -- grub_dprintf("loongson", "fdt linux args:%s\n", -- fdt_linux_args + sizeof (LINUX_IMAGE) - 1); -- - } else { -- int i; -- grub_uint64_t *linux_argv; -- char *linux_args; -- -- is_fdt_boot = 0; -- /* For arguments. */ -- linux_argc = argc; -- /* Main arguments. */ -- args_size = (linux_argc) * sizeof (grub_uint64_t); -- /* Initrd address/size and initrd */ -- args_size += 3 * sizeof (grub_uint64_t); -- /* NULL terminator. */ -- args_size += sizeof (grub_uint64_t); -- /* First argument is always "a0". */ -- args_size += ALIGN_UP (sizeof ("a0"), 4); -- /* Normal arguments. */ -- for (i = 1; i < argc; i++) -- args_size += ALIGN_UP (grub_strlen (argv[i]) + 1, 4); -- -- /* rd arguments. */ -- args_size += ALIGN_UP (sizeof ("rd_start=0xXXXXXXXXXXXXXXXX"), 4); -- args_size += ALIGN_UP (sizeof ("rd_size=0xXXXXXXXXXXXXXXXX"), 4); -- args_size += ALIGN_UP (sizeof ("initrd=0xXXXXXXXXXXXXXXXX,0xXXXXXXXXXXXXXXXX"), 4); -- -- args_size = ALIGN_UP (args_size, 8); -- -- linux_args_addr = grub_efi_allocate_any_pages (GRUB_EFI_BYTES_TO_PAGES (args_size)); -- grub_dprintf ("linux", "linux args numpages: %lld\n", -- (long long) GRUB_EFI_BYTES_TO_PAGES (args_size)); -- if (!linux_args_addr) -- { -- grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory")); -- goto fail; -+ if (grub_file_seek (file, 0) == (grub_off_t) -1) -+ goto fail; -+ -+ if (grub_file_read (file, &lh, sizeof (lh)) < (grub_ssize_t) sizeof (lh)) -+ { -+ if (!grub_errno) -+ grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"), -+ argv[0]); -+ goto fail; -+ } -+ -+ if (grub_arch_efi_linux_check_image (&lh) != GRUB_ERR_NONE) -+ { -+ goto fail; -+ } -+ /* linux kernel type is EFI */ -+ grub_loongarch_linux_type = GRUB_LOONGARCH_LINUX_EFI; -+ kernel_params.kernel_addr = (grub_addr_t) grub_efi_allocate_any_pages ( -+ GRUB_EFI_BYTES_TO_PAGES (kernel_params.kernel_size)); -+ grub_dprintf ("linux", "kernel numpages: %" PRIuGRUB_SIZE "\n", -+ GRUB_EFI_BYTES_TO_PAGES (kernel_params.kernel_size)); -+ if (!kernel_params.kernel_addr) -+ { -+ grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory")); -+ goto fail; -+ } -+ -+ grub_file_seek (file, 0); -+ if (grub_file_read (file, (void*) kernel_params.kernel_addr, kernel_params.kernel_size) -+ < (grub_int64_t) kernel_params.kernel_size) -+ { -+ if (!grub_errno) -+ grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"), argv[0]); -+ goto fail; -+ } -+ -+ grub_dprintf ("linux", "kernel @ %p\n", (void*) kernel_params.kernel_addr); - } -- linux_argv = (grub_uint64_t *) linux_args_addr; -- linux_args = (char *) (linux_argv + (linux_argc + 1 + 3)); -- -- grub_memcpy (linux_args, "a0", sizeof ("a0")); -- *linux_argv = (grub_uint64_t) (grub_addr_t) linux_args; -- linux_argv++; -- linux_args += ALIGN_UP (sizeof ("a0"), 4); - -- for (i = 1; i < argc; i++) -+ cmdline_size = grub_loader_cmdline_size (argc, argv) + sizeof (LINUX_IMAGE) -+ + sizeof (GRUB_INITRD_STRING); -+ kernel_params.ramdisk_args_len = grub_loader_cmdline_size (argc, argv) + sizeof (LINUX_IMAGE); -+ kernel_params.linux_argc = argc; -+ kernel_params.linux_args = grub_malloc (cmdline_size); -+ if (!kernel_params.linux_args) - { -- grub_memcpy (linux_args, argv[i], grub_strlen (argv[i]) + 1); -- *linux_argv = (grub_uint64_t) (grub_addr_t) linux_args; -- linux_argv++; -- linux_args += ALIGN_UP (grub_strlen (argv[i]) + 1, 4); -+ grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory")); -+ goto fail; - } - -- /* Reserve space for rd arguments. */ -- rd_addr_arg_off = (grub_uint8_t *) linux_args - linux_args_addr; -- linux_args += ALIGN_UP (sizeof ("rd_start=0xXXXXXXXXXXXXXXXX"), 4); -- *linux_argv = 0; -- linux_argv++; -+ grub_memcpy (kernel_params.linux_args, LINUX_IMAGE, sizeof (LINUX_IMAGE)); - -- rd_size_arg_off = (grub_uint8_t *) linux_args - linux_args_addr; -- linux_args += ALIGN_UP (sizeof ("rd_size=0xXXXXXXXXXXXXXXXX"), 4); -- *linux_argv = 0; -- linux_argv++; -- -- /* Reserve space for initrd arguments. */ -- initrd_addr_arg_off = (grub_uint8_t *) linux_args - linux_args_addr; -- linux_args += ALIGN_UP (sizeof ("initrd=0xXXXXXXXXXXXXXXXX,0xXXXXXXXXXXXXXXXX"), 4); -- *linux_argv = 0; -- linux_argv++; -+ if (grub_linux_loongarch_elf_get_boot_params (&boot_params) == 1) -+ is_bpi_boot = 1; -+ else -+ is_bpi_boot = 0; - -- *linux_argv = 0; -+ if (is_bpi_boot == 0) -+ { -+ err = grub_create_loader_cmdline (argc, argv, -+ (char*) ((grub_addr_t) kernel_params.linux_args + sizeof (LINUX_IMAGE) - 1), -+ kernel_params.ramdisk_args_len, -+ GRUB_VERIFY_KERNEL_CMDLINE); -+ if (err) -+ goto fail; -+ } else { -+ /* save args from linux cmdline */ -+ char *p = kernel_params.linux_args; -+ -+ p += sizeof (LINUX_IMAGE) - 1; -+ for (i=0; i < argc; i++) -+ { -+ grub_memcpy (p, argv[i], grub_strlen(argv[i]) + 1); -+ p += grub_strlen(argv[i]) + 1; -+ } - } - - if (grub_errno == GRUB_ERR_NONE) -@@ -541,133 +274,81 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)), - loaded = 1; - } - -- initrd_loaded = 0; -- - fail: -+ if (elf != NULL) { -+ /* grub_elf_close will call grub_file_close() */ -+ grub_elf_close (elf); -+ } else { -+ if (file) -+ grub_file_close (file); -+ } -+ - if (grub_errno != GRUB_ERR_NONE) - { - grub_dl_unref (my_mod); - loaded = 0; - } - -- if (fdt_linux_args && !loaded) -- grub_free (fdt_linux_args); -+ if (kernel_params.linux_args && !loaded) -+ grub_free (kernel_params.linux_args); - -- if (linux_args_addr && !loaded) -- grub_efi_free_pages ((grub_addr_t) linux_args_addr, -- GRUB_EFI_BYTES_TO_PAGES (args_size)); -+ if (grub_loongarch_linux_type == GRUB_LOONGARCH_LINUX_EFI) { -+ if (kernel_params.kernel_addr && !loaded) -+ grub_efi_free_pages ((grub_addr_t) kernel_params.kernel_addr, -+ GRUB_EFI_BYTES_TO_PAGES (kernel_params.kernel_size)); -+ } - - return grub_errno; - } - --#define INITRD_MAX_ADDRESS_OFFSET (32ULL * 1024 * 1024 * 1024) -- --/* -- * This function returns a pointer to a legally allocated initrd buffer, -- * or NULL if unsuccessful -- */ --static void * --allocate_initrd_mem (int initrd_pages) --{ -- grub_addr_t max_addr; -- -- if (grub_efi_get_ram_base (&max_addr) != GRUB_ERR_NONE) -- return NULL; -- -- max_addr += INITRD_MAX_ADDRESS_OFFSET - 1; -- -- return grub_efi_allocate_pages_real (max_addr, initrd_pages, -- GRUB_EFI_ALLOCATE_MAX_ADDRESS, -- GRUB_EFI_LOADER_DATA); --} -- -- - static grub_err_t - grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)), - int argc, char *argv[]) - { -- grub_size_t initrd_size, initrd_pages; -- grub_err_t err; -- void *initrd_mem = NULL; - struct grub_linux_initrd_context initrd_ctx = { 0, 0, 0 }; -+ grub_size_t initrd_size; -+ void *initrd_mem = NULL; -+ grub_err_t err; - - if (argc == 0) -- return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected")); -+ { -+ grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected")); -+ goto fail; -+ } - - if (!loaded) -- return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("you need to load the kernel first")); -- -- if (initrd_loaded) -- return grub_error (GRUB_ERR_BAD_ARGUMENT, "only one initrd command can be issued."); -+ { -+ grub_error (GRUB_ERR_BAD_ARGUMENT, -+ N_("you need to load the kernel first")); -+ goto fail; -+ } - - if (grub_initrd_init (argc, argv, &initrd_ctx)) - goto fail; - - initrd_size = grub_get_initrd_size (&initrd_ctx); -+ grub_dprintf ("linux", "Loading initrd\n"); - -- if (is_fdt_boot == 1) //fdt -- { -- initrd_pages = (GRUB_EFI_BYTES_TO_PAGES (initrd_size)); -- initrd_mem = allocate_initrd_mem (initrd_pages); -- if (!initrd_mem) -+ initrd_mem = grub_linux_loongarch_alloc_initrd_mem_align (initrd_size, 0x10000, &err); -+ if (err) -+ goto fail; -+ -+ if (!initrd_mem) - { - grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory")); - goto fail; - } -- } else { -- grub_relocator_chunk_t ch; -- err = grub_relocator_alloc_chunk_align (relocator, &ch, -- 0, (0xffffffff - initrd_size) + 1, -- initrd_size, 0x10000, -- GRUB_RELOCATOR_PREFERENCE_HIGH, 0); -- -- if (err) -- return err; -- initrd_mem = get_virtual_current_address (ch); -- } - - if (grub_initrd_load (&initrd_ctx, argv, initrd_mem)) - goto fail; - -- initrd_start = (grub_addr_t) initrd_mem; -- initrd_end = initrd_start + initrd_size; -- grub_dprintf ("linux", "[addr=%p, size=0x%lx]\n", -- (void *) initrd_start, initrd_size); -- -- if (is_fdt_boot == 0) //bpi -- { -- grub_snprintf ((char *) linux_args_addr + rd_addr_arg_off, -- sizeof ("rd_start=0xXXXXXXXXXXXXXXXX"), -- "rd_start=0x%lx", -- (grub_uint64_t) initrd_mem); -- ((grub_uint64_t *) linux_args_addr)[linux_argc] -- = (grub_uint64_t) ((grub_addr_t) linux_args_addr + rd_addr_arg_off); -- linux_argc++; -- -- grub_snprintf ((char *) linux_args_addr + rd_size_arg_off, -- sizeof ("rd_size=0xXXXXXXXXXXXXXXXXX"), "rd_size=0x%lx", -- (grub_uint64_t) initrd_size); -- ((grub_uint64_t *) linux_args_addr)[linux_argc] -- = (grub_uint64_t) ((grub_addr_t) linux_args_addr + rd_size_arg_off); -- linux_argc++; -- -- grub_snprintf ((char *) linux_args_addr + initrd_addr_arg_off, -- sizeof ("initrd=0xXXXXXXXXXXXXXXXX,0xXXXXXXXXXXXXXXXX"), -- "initrd=0x%lx,0x%lx", -- ((grub_uint64_t) initrd_mem & GRUB_EFI_MAX_PHY_ADDRESS), -- (grub_uint64_t) initrd_size); -- ((grub_uint64_t *) linux_args_addr)[linux_argc] -- = (grub_uint64_t) ((grub_addr_t) linux_args_addr + initrd_addr_arg_off); -- linux_argc++; -- } -- -- initrd_loaded = 1; -- -- fail: -+ /* save ramdisk addr and size */ -+ kernel_params.ramdisk_addr = (grub_addr_t) initrd_mem; -+ kernel_params.ramdisk_size = initrd_size; -+ grub_dprintf ("linux", "ramdisk [addr=%p, size=0x%lx]\n", -+ (void *) initrd_mem, initrd_size); -+fail: - grub_initrd_close (&initrd_ctx); -- if (is_fdt_boot == 1) -- if (initrd_mem && !initrd_start) -- grub_efi_free_pages ((grub_addr_t) initrd_mem, initrd_pages); - - return grub_errno; - } -@@ -677,9 +358,9 @@ static grub_command_t cmd_linux, cmd_initrd; - GRUB_MOD_INIT(linux) - { - cmd_linux = grub_register_command ("linux", grub_cmd_linux, -- 0, N_("Load Linux.")); -+ N_("FILE [ARGS...]"), N_("Load Linux.")); - cmd_initrd = grub_register_command ("initrd", grub_cmd_initrd, -- 0, N_("Load initrd.")); -+ N_("FILE"), N_("Load initrd.")); - my_mod = mod; - } - -diff --git a/include/grub/efi/api.h b/include/grub/efi/api.h -index d5c805d..c34f9d1 100644 ---- a/include/grub/efi/api.h -+++ b/include/grub/efi/api.h -@@ -374,6 +374,26 @@ - { 0xa1, 0x92, 0xbf, 0x1d, 0x57, 0xd0, 0xb1, 0x89 } \ - } - -+#define GRUB_EFI_BOOT_MEMMAP_GUID \ -+ { 0x800f683f, 0xd08b, 0x423a, \ -+ { 0xa2, 0x93, 0x96, 0x5c, 0x3c, 0x6f, 0xe2, 0xb4 } \ -+ } -+ -+#define GRUB_EFI_INITRD_MEDIA_GUID \ -+ { 0x5568e427, 0x68fc, 0x4f3d, \ -+ { 0xac, 0x74, 0xca, 0x55, 0x52, 0x31, 0xcc, 0x68 } \ -+ } -+ -+#define GRUB_EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID \ -+ { 0x9042a9de, 0x23dc, 0x4a38, \ -+ { 0x96, 0xfb, 0x7a, 0xde, 0xd0, 0x80, 0x51, 0x6a } \ -+ } -+ -+#define GRUB_EFI_CONSOLE_OUT_DEVICE_GUID \ -+ { 0xd3b36f2c, 0xd551, 0x11d4, \ -+ { 0x9a, 0x46, 0x00, 0x90, 0x27, 0x3f, 0xc1, 0x4d } \ -+ } -+ - struct grub_efi_sal_system_table - { - grub_uint32_t signature; -diff --git a/include/grub/fdt.h b/include/grub/fdt.h -index a0710ab..2f4f270 100644 ---- a/include/grub/fdt.h -+++ b/include/grub/fdt.h -@@ -146,6 +146,6 @@ int EXPORT_FUNC(grub_fdt_set_prop) (void *fdt, unsigned int nodeoffset, const ch - grub_fdt_set_prop ((fdt), (nodeoffset), "reg", reg_64, 16); \ - }) - --#endif /* defined(__arm__) || defined(__aarch64__) */ -+#endif /* defined(__arm__) || defined(__aarch64__) || defined(__loongarch__) */ - - #endif /* ! GRUB_FDT_HEADER */ -diff --git a/include/grub/loongarch64/asm.h b/include/grub/loongarch64/asm.h -deleted file mode 100644 -index c3e77e9..0000000 ---- a/include/grub/loongarch64/asm.h -+++ /dev/null -@@ -1,10 +0,0 @@ --#ifndef GRUB_LOONGARCH64_ASM_HEADER --#define GRUB_LOONGARCH64_ASM_HEADER 1 -- --#define GRUB_ASM_T4 $a4 --#define GRUB_ASM_T5 $a5 --#define GRUB_ASM_SZREG 8 --#define GRUB_ASM_REG_S st.d --#define GRUB_ASM_REG_L ld.d -- --#endif -diff --git a/include/grub/loongarch64/efi/loongson.h b/include/grub/loongarch64/efi/loongson.h -deleted file mode 100644 -index 7a9ccb4..0000000 ---- a/include/grub/loongarch64/efi/loongson.h -+++ /dev/null -@@ -1,113 +0,0 @@ --/* -- * GRUB -- GRand Unified Bootloader -- * Copyright (C) 2017 Free Software Foundation, Inc. -- * -- * GRUB is free software: you can redistribute it and/or modify -- * it under the terms of the GNU General Public License as published by -- * the Free Software Foundation, either version 3 of the License, or -- * (at your option) any later version. -- * -- * GRUB is distributed in the hope that it will be useful, -- * but WITHOUT ANY WARRANTY; without even the implied warranty of -- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -- * GNU General Public License for more details. -- * -- * You should have received a copy of the GNU General Public License -- * along with GRUB. If not, see . -- */ -- --#ifndef GRUB_EFI_LOONGSON_HEADER --#define GRUB_EFI_LOONGSON_HEADER 1 -- --#include -- --#include -- --#define GRUB_EFI_LOONGSON_BPI_TABLE_GUID \ -- { 0x4660f721, 0x2ec5, 0x416a, \ -- { 0x89, 0x9a, 0x43, 0x18, 0x02, 0x50, 0xa0, 0xc9 } \ -- } -- --#define GRUB_EFI_MAX_PHY_ADDRESS 0xffffffffffffULL --#define ELF32_LOADMASK (0xf0000000UL) --#define ELF64_LOADMASK (0xf000000000000000ULL) --#define FLAGS_EFI_SUPPORT_BIT 0 --#define GRUB_EFI_LOONGSON_MMAP_MAX 128 -- --typedef enum -- { -- GRUB_EFI_LOONGSON_SYSTEM_RAM = 1, -- GRUB_EFI_LOONGSON_MEMORY_RESERVED, -- GRUB_EFI_LOONGSON_ACPI_TABLE, -- GRUB_EFI_LOONGSON_ACPI_NVS, -- GRUB_EFI_LOONGSON_MAX_MEMORY_TYPE -- } --grub_efi_loongson_memory_type; -- --typedef enum -- { -- GRUB_EFI_BPI_VER_NONE = 0, -- GRUB_EFI_BPI_VER_V1 = 1000, -- GRUB_EFI_BPI_VER_V2 = 1001, -- } -- grub_efi_loongson_bpi_version; -- --grub_uint8_t --EXPORT_FUNC(grub_efi_loongson_calculatesum8) (const grub_uint8_t *Buffer, -- grub_efi_uintn_t Length); -- --grub_uint8_t --EXPORT_FUNC(grub_efi_loongson_grub_calculatechecksum8) (const grub_uint8_t *Buffer, -- grub_efi_uintn_t Length); -- --unsigned long --EXPORT_FUNC(grub_efi_get_bpi_version) (const char *str); -- --void * --EXPORT_FUNC(grub_efi_loongson_get_boot_params) (void); -- --typedef struct _extention_list_hdr { -- grub_uint64_t signature; -- grub_uint32_t length; -- grub_uint8_t revision; -- grub_uint8_t checksum; -- union { -- struct _extention_list_hdr *next; -- grub_uint64_t next_offset; -- }; --}GRUB_PACKED --ext_list; -- --typedef struct boot_params_interface { -- grub_uint64_t signature; //{'B', 'P', 'I', 'x', 'x', 'x', 'x', 'x'} -- grub_efi_system_table_t *systemtable; -- union { -- ext_list *extlist; -- grub_uint64_t extlist_offset; -- }; -- grub_uint64_t flags; --}GRUB_PACKED --boot_params_interface; -- --typedef struct { -- ext_list header; //{MEM} -- grub_uint8_t mapcount; -- struct GRUB_PACKED memmap_v1 { -- grub_uint32_t memtype; -- grub_uint64_t memstart; -- grub_uint64_t memsize; -- } map[GRUB_EFI_LOONGSON_MMAP_MAX]; --}GRUB_PACKED --mem_map_v1; -- --typedef struct { -- ext_list header; //{VBIOS} -- grub_uint64_t vbiosaddr; --}GRUB_PACKED --vbios; -- --grub_uint32_t --EXPORT_FUNC (grub_efi_loongson_memmap_v1_sort) (struct memmap_v1 array[], -- grub_uint32_t length, mem_map_v1 * mem, -- grub_uint32_t index, grub_uint32_t memtype); --#endif /* ! GRUB_EFI_LOONGSON_HEADER */ -diff --git a/include/grub/loongarch64/efi/memory.h b/include/grub/loongarch64/efi/memory.h -index 73641a1..16b2e0b 100644 ---- a/include/grub/loongarch64/efi/memory.h -+++ b/include/grub/loongarch64/efi/memory.h -@@ -6,7 +6,12 @@ grub_efi_max_usable_address(void) - { - grub_uint64_t addr; - asm volatile ("csrrd %0, 0x181" : "=r" (addr)); -- return addr |= 0xffffffffffUL; -+ if (addr & 0x1) -+ addr |= 0xfffffffffffUL; -+ else -+ addr = 0xfffffffffffUL; -+ -+ return addr; - } - - #endif /* ! GRUB_MEMORY_CPU_HEADER */ -diff --git a/include/grub/loongarch64/efi/time.h b/include/grub/loongarch64/efi/time.h -deleted file mode 100644 -index e69de29..0000000 -diff --git a/include/grub/loongarch64/linux.h b/include/grub/loongarch64/linux.h -index 3c6cf65..4ac3c57 100644 ---- a/include/grub/loongarch64/linux.h -+++ b/include/grub/loongarch64/linux.h -@@ -28,8 +28,7 @@ - - #define GRUB_LOONGSON3_BOOT_MEM_MAP_MAX 128 - --#define GRUB_LINUX_LOONGARCH_MAGIC_SIGNATURE 0x4C6F6F6E67417263 /* 'LoongArc' */ --#define GRUB_LINUX_LOONGARCH_MAGIC_SIGNATURE2 0x68 /* 'h' */ -+#define GRUB_LINUX_LOONGARCH_MAGIC_SIGNATURE 0x6E6F73676E6F6F4C /* 'Loongson' */ - #define linux_arch_kernel_header linux_loongarch64_kernel_header - - /* From linux/Documentation/loongarch/booting.txt -@@ -48,27 +47,171 @@ struct linux_loongarch64_kernel_header - grub_uint64_t res0; /* reserved */ - grub_uint64_t res1; /* reserved */ - grub_uint64_t res2; /* reserved */ -- grub_uint64_t magic; /* Magic number, little endian, "LoongArc" */ -- grub_uint32_t magic1; /* Magic number, little endian, "h" */ -+ grub_uint64_t magic; /* Magic number, little endian, "Loongson" */ - grub_uint64_t res3; /* reserved */ -+ grub_uint32_t res4; /* reserved */ - grub_uint32_t hdr_offset; /* Offset of PE/COFF header */ - }; - - struct linux_loongarch64_kernel_params - { -- grub_addr_t kernel_addr; /* kernel entry address */ -- grub_size_t kernel_size; /* kernel size */ -- grub_addr_t ramdisk_addr; /* initrd load address */ -- grub_size_t ramdisk_size; /* initrd size */ -- int linux_argc; -- grub_addr_t linux_argv; -+ grub_addr_t kernel_addr; /* kernel entry address */ -+ grub_size_t kernel_size; /* kernel size */ -+ grub_addr_t ramdisk_addr; /* initrd load address */ -+ grub_size_t ramdisk_size; /* initrd size */ -+ int ramdisk_args_len; /* position of initrd in linux_args*/ -+ int linux_argc; /* cmdline parameters number*/ -+ grub_addr_t linux_argv; /* cmdline parameters address*/ - void* linux_args; -+ void* fdt; - }; - - #include - #include - -+#define GRUB_EFI_MAX_PHY_ADDRESS 0xffffffffffffULL - #define ELF32_LOADMASK (0xf0000000UL) - #define ELF64_LOADMASK (0xf000000000000000ULL) -+#define FLAGS_EFI_SUPPORT_BIT 0 -+ -+/*initrd info*/ -+#define GRUB_RD_START_STRING "rd_start=0xXXXXXXXXXXXXXXXX" -+#define GRUB_RD_SIZE_STRING "rd_size=0xXXXXXXXXXXXXXXXX" -+#define GRUB_INITRD_STRING "initrd=0xXXXXXXXXXXXXXXXX,0xXXXXXXXXXXXXXXXX" -+ -+#define FDT_ADDR_CELLS_STRING "#address-cells" -+#define FDT_SIZE_CELLS_STRING "#size-cells" -+#define FDT_ADDR_SIZE_EXTRA ((2 * grub_fdt_prop_entry_size (sizeof(grub_uint32_t))) + \ -+ sizeof (FDT_ADDR_CELLS_STRING) + \ -+ sizeof (FDT_SIZE_CELLS_STRING)) -+ -+struct efi_boot_memmap { -+ grub_efi_uintn_t map_size; -+ grub_efi_uintn_t desc_size; -+ grub_efi_uint32_t desc_ver; -+ grub_efi_uintn_t map_key; -+ grub_efi_uintn_t buff_size; -+ grub_efi_memory_descriptor_t map[]; -+}; -+ -+struct efi_initrd { -+ grub_efi_uintn_t base; -+ grub_efi_uintn_t size; -+}; -+ -+/* -+ * These are set up by the setup-routine at boot-time: -+ */ -+struct screen_info { -+ grub_efi_uint8_t orig_x; /* 0x00 */ -+ grub_efi_uint8_t orig_y; /* 0x01 */ -+ grub_efi_uint16_t ext_mem_k; /* 0x02 */ -+ grub_efi_uint16_t orig_video_page; /* 0x04 */ -+ grub_efi_uint8_t orig_video_mode; /* 0x06 */ -+ grub_efi_uint8_t orig_video_cols; /* 0x07 */ -+ grub_efi_uint8_t flags; /* 0x08 */ -+ grub_efi_uint8_t unused2; /* 0x09 */ -+ grub_efi_uint16_t orig_video_ega_bx;/* 0x0a */ -+ grub_efi_uint16_t unused3; /* 0x0c */ -+ grub_efi_uint8_t orig_video_lines; /* 0x0e */ -+ grub_efi_uint8_t orig_video_isVGA; /* 0x0f */ -+ grub_efi_uint16_t orig_video_points;/* 0x10 */ -+ -+ /* VESA graphic mode -- linear frame buffer */ -+ grub_efi_uint16_t lfb_width; /* 0x12 */ -+ grub_efi_uint16_t lfb_height; /* 0x14 */ -+ grub_efi_uint16_t lfb_depth; /* 0x16 */ -+ grub_efi_uint32_t lfb_base; /* 0x18 */ -+ grub_efi_uint32_t lfb_size; /* 0x1c */ -+ grub_efi_uint16_t cl_magic, cl_offset; /* 0x20 */ -+ grub_efi_uint16_t lfb_linelength; /* 0x24 */ -+ grub_efi_uint8_t red_size; /* 0x26 */ -+ grub_efi_uint8_t red_pos; /* 0x27 */ -+ grub_efi_uint8_t green_size; /* 0x28 */ -+ grub_efi_uint8_t green_pos; /* 0x29 */ -+ grub_efi_uint8_t blue_size; /* 0x2a */ -+ grub_efi_uint8_t blue_pos; /* 0x2b */ -+ grub_efi_uint8_t rsvd_size; /* 0x2c */ -+ grub_efi_uint8_t rsvd_pos; /* 0x2d */ -+ grub_efi_uint16_t vesapm_seg; /* 0x2e */ -+ grub_efi_uint16_t vesapm_off; /* 0x30 */ -+ grub_efi_uint16_t pages; /* 0x32 */ -+ grub_efi_uint16_t vesa_attributes; /* 0x34 */ -+ grub_efi_uint32_t capabilities; /* 0x36 */ -+ grub_efi_uint32_t ext_lfb_base; /* 0x3a */ -+ grub_efi_uint8_t _reserved[2]; /* 0x3e */ -+} __attribute__((packed)); -+ -+#define GRUB_VIDEO_TYPE_EFI 0x70 -+#define GRUB_VIDEO_CAPABILITY_SKIP_QUIRKS (1 << 0) -+#define GRUB_VIDEO_CAPABILITY_64BIT_BASE (1 << 1) /* Frame buffer base is 64-bit */ -+ -+ -+/* From arch/loongarch/include/asm/mach-loongson64/boot_param.h */ -+struct _extention_list_hdr { -+ grub_uint64_t signature; -+ grub_uint32_t length; -+ grub_uint8_t revision; -+ grub_uint8_t checksum; -+ union { -+ struct _extention_list_hdr *next; -+ grub_uint64_t next_offset; -+ }; -+ -+} GRUB_PACKED; -+ -+struct boot_params_interface { -+ grub_uint64_t signature; /* {"B", "P", "I", "0", "1", ... } */ -+ grub_efi_system_table_t *systemtable; -+ union { -+ struct _extention_list_hdr *extlist; -+ grub_uint64_t extlist_offset; -+ }; -+ grub_uint64_t flags; -+}GRUB_PACKED; -+ -+struct loongsonlist_mem_map { -+ struct _extention_list_hdr header; /* {"M", "E", "M"} */ -+ grub_uint8_t map_count; -+ struct memmap { -+ grub_uint32_t mem_type; -+ grub_uint64_t mem_start; -+ grub_uint64_t mem_size; -+ } GRUB_PACKED map[GRUB_LOONGSON3_BOOT_MEM_MAP_MAX]; -+}GRUB_PACKED; -+ -+grub_err_t -+finalize_efi_params_linux (struct linux_loongarch64_kernel_params *kernel_params); -+ -+grub_err_t -+grub_linux_loongarch_elf_linux_boot_image (struct linux_loongarch64_kernel_params -+ *kernel_params); -+ -+void* -+grub_linux_loongarch_alloc_virtual_mem_addr (grub_addr_t addr, -+ grub_size_t size, -+ grub_err_t *err); -+ -+void* -+grub_linux_loongarch_alloc_virtual_mem_align (grub_size_t size, -+ grub_size_t align, -+ grub_err_t *err); -+ -+void* -+grub_linux_loongarch_alloc_initrd_mem_align (grub_size_t size, -+ grub_size_t align, -+ grub_err_t *err); -+ -+void -+grub_linux_loongarch_elf_relocator_unload (void); -+ -+int -+grub_linux_loongarch_elf_get_boot_params (struct boot_params_interface **boot_params); -+ -+grub_err_t -+grub_linux_loongarch_elf_boot_params (struct boot_params_interface *boot_params); -+ -+grub_err_t -+grub_linux_loongarch_elf_load_kernel (grub_elf_t elf, const char *filename); - - #endif /* ! GRUB_LOONGARCH64_LINUX_HEADER */ -diff --git a/include/grub/loongarch64/time.h b/include/grub/loongarch64/time.h -index c9a7334..0a10fbd 100644 ---- a/include/grub/loongarch64/time.h -+++ b/include/grub/loongarch64/time.h -@@ -19,18 +19,6 @@ - #ifndef KERNEL_CPU_TIME_HEADER - #define KERNEL_CPU_TIME_HEADER 1 - --#ifndef GRUB_UTIL -- --#define GRUB_TICKS_PER_SECOND (grub_arch_cpuclock / 2) -- --void grub_timer_init (grub_uint32_t cpuclock); -- --/* Return the real time in ticks. */ --grub_uint64_t grub_get_rtc (void); -- --extern grub_uint32_t grub_arch_cpuclock; --#endif -- - static inline void - grub_cpu_idle(void) - { --- -2.43.5 - diff --git a/20-grub.install b/20-grub.install index 40a437c..0ecf1e2 100755 --- a/20-grub.install +++ b/20-grub.install @@ -62,7 +62,7 @@ case "$COMMAND" in "$KERNEL_DIR"/dtb do [[ -e "$i" ]] || continue - rm -rf "/boot/${i##*/}-${KERNEL_VERSION}" + rm -f "/boot/${i##*/}-${KERNEL_VERSION}" cp -aT "$i" "/boot/${i##*/}-${KERNEL_VERSION}" command -v restorecon &>/dev/null && \ restorecon -R "/boot/${i##*/}-${KERNEL_VERSION}" diff --git a/grub.macros b/grub.macros index 07a0edd..229a82a 100644 --- a/grub.macros +++ b/grub.macros @@ -92,7 +92,7 @@ %endif -%global efi_only aarch64 %{arm} loongarch64 +%global efi_only aarch64 %{arm} %global efi_arch x86_64 ia64 %{efi_only} %ifarch %{efi_arch} %global with_efi_arch 1 @@ -122,7 +122,7 @@ %global platform_modules " appendedsig " %endif -%ifarch aarch64 %{arm} loongarch64 +%ifarch aarch64 %{arm} %global platform_modules " " %endif @@ -187,14 +187,6 @@ )} %endif -%ifarch loongarch64 -%global with_emu_arch 0 -%global efiarch loongarch64 -%global target_cpu_name loongarch64 -%global grub_target_name loongarch64-efi -%global package_arch efi-loongarch64 -%endif - %global _target_platform %{target_cpu_name}-%{_vendor}-%{_target_os}%{?_gnu} %global _alt_target_platform %{alt_target_cpu_name}-%{_vendor}-%{_target_os}%{?_gnu} @@ -230,7 +222,7 @@ %ifarch x86_64 %global with_efi_common 1 -%global with_legacy_modules 1 +%global with_legacy_modules 0 %global with_legacy_common 0 %else %global with_efi_common 0 diff --git a/grub.patches b/grub.patches index 6ace98b..066ebb5 100644 --- a/grub.patches +++ b/grub.patches @@ -589,11 +589,77 @@ Patch0588: 0588-fs-ntfs-Fix-an-OOB-read-when-parsing-bitmaps-for-ind.patch Patch0589: 0589-fs-ntfs-Fix-an-OOB-read-when-parsing-a-volume-label.patch Patch0590: 0590-fs-ntfs-Make-code-more-readable.patch Patch0591: 0591-grub-mkconfig.in-turn-off-executable-owner-bit.patch -# Support loongarch64 -#Patch1000: 1000-loongarch64-add-support.patch -Patch1001: 1001-bls-make-list.patch -Patch1002: 1002-Add-LoongArch64-support-and-update-interface-to-v40.patch -Patch1003: 1003-cryptodisk-make-the-password-getter-and-additional-a.patch -Patch1004: 1004-cryptodisk-add-OS-provided-secret-support.patch -Patch1005: 1005-efi-Add-API-for-retrieving-the-EFI-secret-for-crypto.patch -Patch1006: 1006-Update-patches-in-sync-with-loongarch64.patch +Patch0592: 0592-misc-Implement-grub_strlcpy.patch +Patch0593: 0593-fs-ufs-Fix-a-heap-OOB-write.patch +Patch0594: 0594-fs-hfs-Fix-stack-OOB-write-with-grub_strcpy.patch +Patch0595: 0595-fs-tar-Initialize-name-in-grub_cpio_find_file.patch +Patch0596: 0596-fs-tar-Integer-overflow-leads-to-heap-OOB-write.patch +Patch0597: 0597-fs-f2fs-Set-a-grub_errno-if-mount-fails.patch +Patch0598: 0598-fs-hfsplus-Set-a-grub_errno-if-mount-fails.patch +Patch0599: 0599-fs-iso9660-Set-a-grub_errno-if-mount-fails.patch +Patch0600: 0600-fs-iso9660-Fix-invalid-free.patch +Patch0601: 0601-fs-jfs-Fix-OOB-read-in-jfs_getent.patch +Patch0602: 0602-fs-jfs-Fix-OOB-read-caused-by-invalid-dir-slot-index.patch +Patch0603: 0603-fs-jfs-Use-full-40-bits-offset-and-address-for-a-dat.patch +Patch0604: 0604-fs-jfs-Inconsistent-signed-unsigned-types-usage-in-r.patch +Patch0605: 0605-fs-ext2-Fix-out-of-bounds-read-for-inline-extents.patch +Patch0606: 0606-fs-ntfs-Fix-out-of-bounds-read.patch +Patch0607: 0607-fs-ntfs-Track-the-end-of-the-MFT-attribute-buffer.patch +Patch0608: 0608-fs-ntfs-Use-a-helper-function-to-access-attributes.patch +Patch0610: 0610-fs-xfs-Fix-out-of-bounds-read.patch +Patch0611: 0611-fs-xfs-Ensuring-failing-to-mount-sets-a-grub_errno.patch +Patch0612: 0612-kern-file-Ensure-file-data-is-set.patch +Patch0613: 0613-kern-file-Implement-filesystem-reference-counting.patch +Patch0614: 0614-disk-loopback-Reference-tracking-for-the-loopback.patch +Patch0615: 0615-kern-disk-Limit-recursion-depth.patch +Patch0616: 0616-kern-partition-Limit-recursion-in-part_iterate.patch +Patch0617: 0617-script-execute-Limit-the-recursion-depth.patch +Patch0618: 0618-net-Unregister-net_default_ip-and-net_default_mac-va.patch +Patch0619: 0619-net-Remove-variables-hooks-when-interface-is-unregis.patch +Patch0620: 0620-net-Fix-OOB-write-in-grub_net_search_config_file.patch +Patch0621: 0621-net-tftp-Fix-stack-buffer-overflow-in-tftp_open.patch +Patch0622: 0622-video-readers-jpeg-Do-not-permit-duplicate-SOF0-mark.patch +Patch0623: 0623-kern-dl-Fix-for-an-integer-overflow-in-grub_dl_ref.patch +Patch0624: 0624-kern-dl-Check-for-the-SHF_INFO_LINK-flag-in-grub_dl_.patch +Patch0625: 0625-commands-extcmd-Missing-check-for-failed-allocation.patch +Patch0626: 0626-commands-ls-Fix-NULL-dereference.patch +Patch0627: 0627-commands-pgp-Unregister-the-check_signatures-hooks-o.patch +Patch0628: 0628-normal-Remove-variables-hooks-on-module-unload.patch +Patch0629: 0629-gettext-Remove-variables-hooks-on-module-unload.patch +Patch0630: 0630-gettext-Integer-overflow-leads-to-heap-OOB-write-or-.patch +Patch0631: 0631-gettext-Integer-overflow-leads-to-heap-OOB-write.patch +Patch0632: 0632-commands-read-Fix-an-integer-overflow-when-supplying.patch +Patch0633: 0633-commands-test-Stack-overflow-due-to-unlimited-recurs.patch +Patch0634: 0634-commands-minicmd-Block-the-dump-command-in-lockdown-.patch +Patch0635: 0635-commands-memrw-Disable-memory-reading-in-lockdown-mo.patch +Patch0636: 0636-commands-hexdump-Disable-memory-reading-in-lockdown-.patch +Patch0637: 0637-fs-bfs-Disable-under-lockdown.patch +Patch0638: 0638-fs-Disable-many-filesystems-under-lockdown.patch +Patch0639: 0639-disk-Use-safe-math-macros-to-prevent-overflows.patch +Patch0640: 0640-disk-Prevent-overflows-when-allocating-memory-for-ar.patch +Patch0641: 0641-disk-Check-if-returned-pointer-for-allocated-memory-.patch +Patch0642: 0642-disk-ieee1275-ofdisk-Call-grub_ieee1275_close-when-g.patch +Patch0643: 0643-fs-Use-safe-math-macros-to-prevent-overflows.patch +Patch0644: 0644-fs-Prevent-overflows-when-allocating-memory-for-arra.patch +Patch0645: 0645-fs-Prevent-overflows-when-assigning-returned-values-.patch +Patch0646: 0646-fs-zfs-Use-safe-math-macros-to-prevent-overflows.patch +Patch0647: 0647-fs-zfs-Prevent-overflows-when-allocating-memory-for-.patch +Patch0648: 0648-fs-zfs-Check-if-returned-pointer-for-allocated-memor.patch +Patch0649: 0649-fs-zfs-Add-missing-NULL-check-after-grub_strdup-call.patch +Patch0650: 0650-net-Use-safe-math-macros-to-prevent-overflows.patch +Patch0651: 0651-net-Prevent-overflows-when-allocating-memory-for-arr.patch +Patch0652: 0652-net-Check-if-returned-pointer-for-allocated-memory-i.patch +Patch0653: 0653-fs-sfs-Check-if-allocated-memory-is-NULL.patch +Patch0654: 0654-script-execute-Fix-potential-underflow-and-NULL-dere.patch +Patch0655: 0655-osdep-unix-getroot-Fix-potential-underflow.patch +Patch0656: 0656-misc-Ensure-consistent-overflow-error-messages.patch +Patch0657: 0657-bus-usb-ehci-Define-GRUB_EHCI_TOGGLE-as-grub_uint32_.patch +Patch0658: 0658-normal-menu-Use-safe-math-to-avoid-an-integer-overfl.patch +Patch0659: 0659-kern-partition-Add-sanity-check-after-grub_strtoul-c.patch +Patch0660: 0660-kern-misc-Add-sanity-check-after-grub_strtoul-call.patch +Patch0661: 0661-loader-i386-linux-Cast-left-shift-to-grub_uint32_t.patch +Patch0662: 0662-loader-i386-bsd-Use-safe-math-to-avoid-underflow.patch +Patch0663: 0663-types-Make-bool-generally-available.patch +Patch0664: 0664-Remove-exttra-bool-definitions.patch +Patch0665: 0665-fs-xfs-Fix-issues-found-while-fuzzing-the-XFS-filesy.patch +Patch0666: 0666-ieee1275-ofnet-Fix-grub_malloc-removed-after-added-s.patch diff --git a/grub2.spec b/grub2.spec index f99f085..b6d9097 100644 --- a/grub2.spec +++ b/grub2.spec @@ -1,18 +1,13 @@ -%define anolis_release .0.3 %undefine _hardened_build %global tarversion 2.02 %undefine _missing_build_ids_terminate_build %global _configure_gnuconfig_hack 0 -%ifarch loongarch64 -%global _configure_gnuconfig_hack 1 -%endif - Name: grub2 Epoch: 1 Version: 2.02 -Release: 160%{anolis_release}%{?dist} +Release: 162%{?dist} Summary: Bootloader with support for Linux, Multiboot and more Group: System Environment/Base License: GPLv3+ @@ -119,7 +114,7 @@ Requires(post): dracut %{desc} This subpackage provides tools for support of all platforms. -%ifarch x86_64 loongarch64 +%ifarch x86_64 %package tools-efi Summary: Support tools for GRUB. Group: System Environment/Base @@ -189,11 +184,6 @@ git add grub-%{grublegacyarch}-%{tarversion} %endif git commit -m "After making subdirs" -%ifarch loongarch64 -%_update_config_sub -%_update_config_guess -%endif - %build %if 0%{with_efi_arch} %{expand:%do_primary_efi_build %%{grubefiarch} %%{grubefiname} %%{grubeficdname} %%{_target_platform} %%{efi_target_cflags} %%{efi_host_cflags} %{old_sb_ca} %{old_sb_cer} %{old_sb_key} %{sb_ca} %{sb_cer} %{sb_key}} @@ -235,11 +225,8 @@ rm -f $RPM_BUILD_ROOT%{_infodir}/dir ln -s %{name}-set-password ${RPM_BUILD_ROOT}/%{_sbindir}/%{name}-setpassword echo '.so man8/%{name}-set-password.8' > ${RPM_BUILD_ROOT}/%{_datadir}/man/man8/%{name}-setpassword.8 %ifnarch x86_64 -rm -vf ${RPM_BUILD_ROOT}/%{_sbindir}/%{name}-bios-setup -%endif - -%ifnarch x86_64 loongarch64 rm -vf ${RPM_BUILD_ROOT}/%{_bindir}/%{name}-render-label +rm -vf ${RPM_BUILD_ROOT}/%{_sbindir}/%{name}-bios-setup rm -vf ${RPM_BUILD_ROOT}/%{_sbindir}/%{name}-macbless %endif @@ -412,7 +399,7 @@ fi %{_datadir}/man/man1/%{name}-editenv* %{_datadir}/man/man1/%{name}-mkpasswd-* -%ifarch x86_64 loongarch64 +%ifarch x86_64 %files tools-efi %{_sbindir}/%{name}-macbless %{_bindir}/%{name}-render-label @@ -536,20 +523,16 @@ fi %endif %changelog -* Sat Mar 22 2025 Xue Liu - 2.02-160.0.3 -- Update for loongarch64 in sync with latest v2.04 - -* Thu Jan 02 2025 hanliyang - 2.02-160.0.2 -- Support use confidential computing provisioned secrets for disk decryption - -* Tue Dec 24 2024 Bo Ren - 2.02-160.0.1 -- Build pc-modules package on x86_64 (geliwei@openanolis.org) -- Add loongarch64 base support (zhangwenlong@loongson.cn)(chenguoqi@loongson.cn) -- Fix a bug in bls_make_list, blscfg. (zhonglingh@linux.alibaba.com) -- Delete LoongArch64 support old version due to compile error(yangqiming@loongson.cn) -- Add LoongArch64 support(yangqiming@loongson.cn) -- LoongArch64 support fdt and phy-addr BIOS(yangqiming@loongson.cn) -- Remove dtb dir with correct argument (Liwei Ge) +* Tue Mar 25 2025 Nicolas Frayer - 2.02-162 +- ieee1275/ofnet: Fix grub_malloc() removed after added safe +- Remove 'fs/ntfs: Implement attribute verification' patch +- Related: #RHEL-79837 + +* Tue Feb 18 2025 Leo Sandoval - 2.02-161 +- Add Several CVE fixes +- Resolves CVE-2024-45775 CVE-2025-0624 +- Resolves: #RHEL-75735 +- Resolves: #RHEL-79837 * Wed Nov 13 2024 Leo Sandoval - 2.02-160 - Remove BLS fake config in case of kernel removal -- Gitee From e0cae647ff227a9e53f6eb80c7d81e85390b17fc Mon Sep 17 00:00:00 2001 From: songmingliang Date: Tue, 17 May 2022 18:12:12 +0800 Subject: [PATCH 2/8] build: build pc-modules package on x86_64 --- grub.macros | 2 +- grub2.spec | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/grub.macros b/grub.macros index 229a82a..e55de53 100644 --- a/grub.macros +++ b/grub.macros @@ -222,7 +222,7 @@ %ifarch x86_64 %global with_efi_common 1 -%global with_legacy_modules 0 +%global with_legacy_modules 1 %global with_legacy_common 0 %else %global with_efi_common 0 diff --git a/grub2.spec b/grub2.spec index b6d9097..e017723 100644 --- a/grub2.spec +++ b/grub2.spec @@ -1,3 +1,4 @@ +%define anolis_release .0.1 %undefine _hardened_build %global tarversion 2.02 @@ -7,7 +8,7 @@ Name: grub2 Epoch: 1 Version: 2.02 -Release: 162%{?dist} +Release: 162%{anolis_release}%{?dist} Summary: Bootloader with support for Linux, Multiboot and more Group: System Environment/Base License: GPLv3+ @@ -523,6 +524,9 @@ fi %endif %changelog +* Fri Mar 28 2025 Bo Ren - 2.02-162.0.1 +- Build pc-modules package on x86_64 (geliwei@openanolis.org) + * Tue Mar 25 2025 Nicolas Frayer - 2.02-162 - ieee1275/ofnet: Fix grub_malloc() removed after added safe - Remove 'fs/ntfs: Implement attribute verification' patch -- Gitee From 6654d21047c4bc2c143ecbbbce7e224ab27d6dcb Mon Sep 17 00:00:00 2001 From: songmingliang Date: Tue, 17 May 2022 18:43:45 +0800 Subject: [PATCH 3/8] Add loongarch64 base support --- 1000-loongarch64-add-support.patch | 3968 ++++++++++++++++++++++++++++ grub.macros | 12 +- grub.patches | 2 + grub2.spec | 19 +- 4 files changed, 3996 insertions(+), 5 deletions(-) create mode 100644 1000-loongarch64-add-support.patch diff --git a/1000-loongarch64-add-support.patch b/1000-loongarch64-add-support.patch new file mode 100644 index 0000000..2d83c94 --- /dev/null +++ b/1000-loongarch64-add-support.patch @@ -0,0 +1,3968 @@ +From febee03c70471fd2ce6f0b4cc0f365c32c106ab3 Mon Sep 17 00:00:00 2001 +From: Fedora Ninjas +Date: Wed, 18 May 2022 23:50:45 +0800 +Subject: [PATCH] add loongarch64 support + +--- + conf/Makefile.common | 4 + + configure.ac | 9 + + gentpl.py | 5 +- + grub-core/Makefile.am | 7 + + grub-core/Makefile.core.def | 20 + + grub-core/kern/efi/mm.c | 29 +- + grub-core/kern/elfXX.c | 6 + + grub-core/kern/loongarch64/cache.S | 26 + + grub-core/kern/loongarch64/dl.c | 258 +++++++++ + grub-core/kern/loongarch64/efi/init.c | 76 +++ + grub-core/kern/loongarch64/efi/startup.S | 45 ++ + grub-core/kern/loongarch64/init.c | 47 ++ + grub-core/lib/loongarch64/efi/loongson.c | 505 ++++++++++++++++ + grub-core/lib/loongarch64/efi/loongson_asm.S | 58 ++ + grub-core/lib/loongarch64/relocator.c | 163 ++++++ + grub-core/lib/loongarch64/relocator_asm.S | 51 ++ + grub-core/lib/loongarch64/setjmp.S | 74 +++ + grub-core/lib/loongson/reboot.c | 33 ++ + grub-core/lib/setjmp.S | 2 + + grub-core/loader/efi/chainloader.c | 2 + + grub-core/loader/loongarch64/linux.c | 580 +++++++++++++++++++ + include/grub/dl.h | 2 +- + include/grub/efi/api.h | 3 +- + include/grub/efi/pe32.h | 2 + + include/grub/elf.h | 55 ++ + include/grub/loongarch64/asm.h | 10 + + include/grub/loongarch64/efi/boot.h | 0 + include/grub/loongarch64/efi/loader.h | 25 + + include/grub/loongarch64/efi/loongson.h | 290 ++++++++++ + include/grub/loongarch64/efi/memory.h | 14 + + include/grub/loongarch64/efi/time.h | 0 + include/grub/loongarch64/io.h | 62 ++ + include/grub/loongarch64/kernel.h | 24 + + include/grub/loongarch64/linux.h | 22 + + include/grub/loongarch64/loongarch64.h | 30 + + include/grub/loongarch64/memory.h | 57 ++ + include/grub/loongarch64/relocator.h | 38 ++ + include/grub/loongarch64/setjmp.h | 27 + + include/grub/loongarch64/time.h | 39 ++ + include/grub/loongarch64/types.h | 34 ++ + include/grub/util/install.h | 1 + + util/grub-install-common.c | 43 +- + util/grub-install.c | 16 + + util/grub-mkimagexx.c | 349 ++++++++++- + util/grub-mknetdir.c | 3 +- + util/grub-module-verifier.c | 46 ++ + util/mkimage.c | 16 + + 47 files changed, 3176 insertions(+), 32 deletions(-) + create mode 100644 grub-core/kern/loongarch64/cache.S + create mode 100644 grub-core/kern/loongarch64/dl.c + create mode 100644 grub-core/kern/loongarch64/efi/init.c + create mode 100644 grub-core/kern/loongarch64/efi/startup.S + create mode 100644 grub-core/kern/loongarch64/init.c + create mode 100644 grub-core/lib/loongarch64/efi/loongson.c + create mode 100644 grub-core/lib/loongarch64/efi/loongson_asm.S + create mode 100644 grub-core/lib/loongarch64/relocator.c + create mode 100644 grub-core/lib/loongarch64/relocator_asm.S + create mode 100644 grub-core/lib/loongarch64/setjmp.S + create mode 100644 grub-core/lib/loongson/reboot.c + create mode 100644 grub-core/loader/loongarch64/linux.c + create mode 100644 include/grub/loongarch64/asm.h + create mode 100644 include/grub/loongarch64/efi/boot.h + create mode 100644 include/grub/loongarch64/efi/loader.h + create mode 100644 include/grub/loongarch64/efi/loongson.h + create mode 100644 include/grub/loongarch64/efi/memory.h + create mode 100644 include/grub/loongarch64/efi/time.h + create mode 100644 include/grub/loongarch64/io.h + create mode 100644 include/grub/loongarch64/kernel.h + create mode 100644 include/grub/loongarch64/linux.h + create mode 100644 include/grub/loongarch64/loongarch64.h + create mode 100644 include/grub/loongarch64/memory.h + create mode 100644 include/grub/loongarch64/relocator.h + create mode 100644 include/grub/loongarch64/setjmp.h + create mode 100644 include/grub/loongarch64/time.h + create mode 100644 include/grub/loongarch64/types.h + +diff --git a/conf/Makefile.common b/conf/Makefile.common +index 521cdda..cc1d190 100644 +--- a/conf/Makefile.common ++++ b/conf/Makefile.common +@@ -20,6 +20,10 @@ endif + if COND_powerpc_ieee1275 + CFLAGS_PLATFORM += -mcpu=powerpc + endif ++if COND_loongarch64 ++ CFLAGS_PLATFORM += -fno-strict-aliasing -march=loongarch64 -mabi=lp64 -fno-plt -Wa,-mla-global-with-pcrel ++ CPPFLAGS_PLATFORM = -fno-strict-aliasing -march=loongarch64 -mabi=lp64 -fno-plt -Wa,-mla-global-with-pcrel ++endif + + # Other options + +diff --git a/configure.ac b/configure.ac +index f59a7b8..4715d17 100644 +--- a/configure.ac ++++ b/configure.ac +@@ -116,6 +116,10 @@ case "$target_cpu" in + i[[3456]]86) target_cpu=i386 ;; + amd64) target_cpu=x86_64 ;; + sparc) target_cpu=sparc64 ;; ++ loongarch64) ++ target_cpu=loongarch64 ++ machine_CPPFLAGS="$machine_CPPFLAGS -DGRUB_CPU_LOONGARCH64=1" ++ ;; + mipsel|mips64el) + target_cpu=mipsel + machine_CPPFLAGS="$machine_CPPFLAGS -DGRUB_CPU_MIPSEL=1" +@@ -148,6 +152,7 @@ if test "x$with_platform" = x; then + powerpc64-*) platform=ieee1275 ;; + powerpc64le-*) platform=ieee1275 ;; + sparc64-*) platform=ieee1275 ;; ++ loongarch64-*) platform=efi;; + mipsel-*) platform=loongson ;; + mips-*) platform=arc ;; + ia64-*) platform=efi ;; +@@ -196,6 +201,7 @@ case "$target_cpu"-"$platform" in + mipsel-yeeloong) platform=loongson ;; + mipsel-fuloong) platform=loongson ;; + mipsel-loongson) ;; ++ loongarch64-efi) ;; + arm-uboot) ;; + arm-coreboot) ;; + arm-efi) ;; +@@ -251,6 +257,7 @@ case "$platform" in + pc) machine_CPPFLAGS="$machine_CPPFLAGS -DGRUB_MACHINE_PCBIOS=1" ;; + emu) machine_CPPFLAGS="$machine_CPPFLAGS -DGRUB_MACHINE_EMU=1" ;; + loongson) machine_CPPFLAGS="$machine_CPPFLAGS -DGRUB_MACHINE_MIPS_LOONGSON=1" ;; ++ loongson64) machine_CPPFLAGS="$machine_CPPFLAGS -DGRUB_MACHINE_LOONARCH64=1" ;; + qemu_mips) machine_CPPFLAGS="$machine_CPPFLAGS -DGRUB_MACHINE_MIPS_QEMU_MIPS=1" ;; + arc) machine_CPPFLAGS="$machine_CPPFLAGS -DGRUB_MACHINE_ARC=1" ;; + esac +@@ -2098,6 +2105,8 @@ AM_CONDITIONAL([COND_mips_arc], [test "(" x$target_cpu = xmips -o x$target_cpu = + AM_CONDITIONAL([COND_sparc64_ieee1275], [test x$target_cpu = xsparc64 -a x$platform = xieee1275]) + AM_CONDITIONAL([COND_sparc64_emu], [test x$target_cpu = xsparc64 -a x$platform = xemu]) + AM_CONDITIONAL([COND_powerpc_ieee1275], [test x$target_cpu = xpowerpc -a x$platform = xieee1275]) ++AM_CONDITIONAL([COND_loongarch64_efi], [test x$target_cpu = xloongarch64 -a x$platform = xefi]) ++AM_CONDITIONAL([COND_loongarch64], [test x$target_cpu = xloongarch64]) + AM_CONDITIONAL([COND_mips], [test x$target_cpu = xmips -o x$target_cpu = xmipsel]) + AM_CONDITIONAL([COND_mipsel], [test x$target_cpu = xmipsel]) + AM_CONDITIONAL([COND_mipseb], [test x$target_cpu = xmips]) +diff --git a/gentpl.py b/gentpl.py +index d662c30..3afd642 100644 +--- a/gentpl.py ++++ b/gentpl.py +@@ -32,7 +32,7 @@ GRUB_PLATFORMS = [ "emu", "i386_pc", "i386_efi", "i386_qemu", "i386_coreboot", + "mips_loongson", "sparc64_ieee1275", + "powerpc_ieee1275", "mips_arc", "ia64_efi", + "mips_qemu_mips", "arm_uboot", "arm_efi", "arm64_efi", +- "arm_coreboot"] ++ "arm_coreboot", "loongarch64_efi"] + + GROUPS = {} + +@@ -44,13 +44,14 @@ GROUPS["x86_64"] = [ "x86_64_efi" ] + GROUPS["x86"] = GROUPS["i386"] + GROUPS["x86_64"] + GROUPS["mips"] = [ "mips_loongson", "mips_qemu_mips", "mips_arc" ] + GROUPS["sparc64"] = [ "sparc64_ieee1275" ] ++GROUPS["loongarch64"] = [ "loongarch64_efi" ] + GROUPS["powerpc"] = [ "powerpc_ieee1275" ] + GROUPS["arm"] = [ "arm_uboot", "arm_efi", "arm_coreboot" ] + GROUPS["arm64"] = [ "arm64_efi" ] + + # Groups based on firmware + GROUPS["pc"] = [ "i386_pc" ] +-GROUPS["efi"] = [ "i386_efi", "x86_64_efi", "ia64_efi", "arm_efi", "arm64_efi" ] ++GROUPS["efi"] = [ "i386_efi", "x86_64_efi", "ia64_efi", "arm_efi", "arm64_efi", "loongarch64_efi"] + GROUPS["ieee1275"] = [ "i386_ieee1275", "sparc64_ieee1275", "powerpc_ieee1275" ] + GROUPS["uboot"] = [ "arm_uboot" ] + GROUPS["xen"] = [ "i386_xen", "x86_64_xen" ] +diff --git a/grub-core/Makefile.am b/grub-core/Makefile.am +index 308ad88..24e2ac3 100644 +--- a/grub-core/Makefile.am ++++ b/grub-core/Makefile.am +@@ -222,6 +222,13 @@ KERNEL_HEADER_FILES += $(top_builddir)/include/grub/machine/memory.h + KERNEL_HEADER_FILES += $(top_builddir)/include/grub/machine/kernel.h + endif + ++if COND_loongarch64_efi ++KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/efi/efi.h ++KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/efi/disk.h ++KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/acpi.h ++KERNEL_HEADER_FILES += $(top_builddir)/include/grub/machine/loongson.h ++endif ++ + if COND_powerpc_ieee1275 + KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/ieee1275/ieee1275.h + KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/terminfo.h +diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def +index ef06f8c..74e66d3 100644 +--- a/grub-core/Makefile.core.def ++++ b/grub-core/Makefile.core.def +@@ -95,6 +95,9 @@ kernel = { + arm_coreboot_ldflags = '-Wl,-r,-d'; + arm_coreboot_stripflags = '--strip-unneeded -K start -R .note -R .comment -R .note.gnu.gold-version'; + ++ loongarch64_efi_ldflags = '-Wl,-r,-d'; ++ loongarch64_efi_stripflags = '--strip-unneeded -K start -R .note -R .comment -R .note.gnu.gold-version -R .eh_frame'; ++ + i386_pc_startup = kern/i386/pc/startup.S; + i386_efi_startup = kern/i386/efi/startup.S; + x86_64_efi_startup = kern/x86_64/efi/startup.S; +@@ -105,6 +108,7 @@ kernel = { + i386_coreboot_startup = kern/i386/coreboot/startup.S; + i386_multiboot_startup = kern/i386/coreboot/startup.S; + mips_startup = kern/mips/startup.S; ++ loongarch64_efi_startup = kern/loongarch64/efi/startup.S; + sparc64_ieee1275_startup = kern/sparc64/ieee1275/crt0.S; + powerpc_ieee1275_startup = kern/powerpc/ieee1275/startup.S; + arm_uboot_startup = kern/arm/startup.S; +@@ -295,6 +299,14 @@ kernel = { + extra_dist = video/sis315_init.c; + mips_loongson = commands/keylayouts.c; + ++ loongarch64 = kern/loongarch64/init.c; ++ loongarch64 = kern/loongarch64/dl.c; ++ loongarch64 = kern/loongarch64/cache.S; ++ loongarch64 = kern/generic/rtc_get_time_ms.c; ++ loongarch64_efi = kern/loongarch64/efi/init.c; ++ loongarch64_efi = lib/loongarch64/efi/loongson.c; ++ loongarch64_efi = lib/loongarch64/efi/loongson_asm.S; ++ + powerpc_ieee1275 = kern/powerpc/cache.S; + powerpc_ieee1275 = kern/powerpc/dl.c; + powerpc_ieee1275 = kern/powerpc/compiler-rt.S; +@@ -810,6 +822,7 @@ module = { + enable = sparc64_ieee1275; + enable = powerpc_ieee1275; + enable = mips_arc; ++ enable = loongarch64_efi; + enable = ia64_efi; + enable = arm_efi; + enable = arm64_efi; +@@ -896,6 +909,7 @@ module = { + i386_qemu = lib/i386/halt.c; + xen = lib/xen/halt.c; + efi = lib/efi/halt.c; ++ loongarch64_efi = commands/acpihalt.c; + ieee1275 = lib/ieee1275/halt.c; + emu = lib/emu/halt.c; + uboot = lib/dummy/halt.c; +@@ -911,6 +925,7 @@ module = { + mips_arc = lib/mips/arc/reboot.c; + mips_loongson = lib/mips/loongson/reboot.c; + mips_qemu_mips = lib/mips/qemu_mips/reboot.c; ++ loongarch64_efi = lib/loongson/reboot.c; + xen = lib/xen/reboot.c; + uboot = lib/uboot/reboot.c; + arm_coreboot = lib/dummy/reboot.c; +@@ -1608,6 +1623,8 @@ module = { + efi = lib/efi/relocator.c; + mips = lib/mips/relocator_asm.S; + mips = lib/mips/relocator.c; ++ loongarch64 = lib/loongarch64/relocator_asm.S; ++ loongarch64 = lib/loongarch64/relocator.c; + powerpc = lib/powerpc/relocator_asm.S; + powerpc = lib/powerpc/relocator.c; + xen = lib/xen/relocator.c; +@@ -1620,6 +1637,7 @@ module = { + extra_dist = kern/powerpc/cache_flush.S; + + enable = mips; ++ enable = loongarch64; + enable = powerpc; + enable = x86; + enable = xen; +@@ -1737,6 +1755,7 @@ module = { + xen = loader/i386/xen.c; + i386_pc = lib/i386/pc/vesa_modes_table.c; + mips = loader/mips/linux.c; ++ loongarch64 = loader/loongarch64/linux.c; + powerpc_ieee1275 = loader/powerpc/ieee1275/linux.c; + sparc64_ieee1275 = loader/sparc64/ieee1275/linux.c; + ia64_efi = loader/ia64/efi/linux.c; +@@ -1838,6 +1857,7 @@ module = { + enable = arm_efi; + enable = arm64_efi; + enable = mips; ++ enable = loongarch64_efi; + }; + + module = { +diff --git a/grub-core/kern/efi/mm.c b/grub-core/kern/efi/mm.c +index 9e76f23..fb087e3 100644 +--- a/grub-core/kern/efi/mm.c ++++ b/grub-core/kern/efi/mm.c +@@ -157,7 +157,11 @@ grub_efi_allocate_pages_real (grub_efi_physical_address_t address, + grub_efi_physical_address_t ret = address; + + /* Limit the memory access to less than 4GB for 32-bit platforms. */ ++#ifdef GRUB_CPU_LOONGARCH64 ++ if (address > grub_efi_max_usable_address()) ++#else + if (address > GRUB_EFI_MAX_USABLE_ADDRESS) ++#endif + { + grub_error (GRUB_ERR_BAD_ARGUMENT, + N_("invalid memory address (0x%llx > 0x%llx)"), +@@ -177,7 +181,11 @@ grub_efi_allocate_pages_real (grub_efi_physical_address_t address, + { + /* Uggh, the address 0 was allocated... This is too annoying, + so reallocate another one. */ ++#ifdef GRUB_CPU_LOONGARCH64 ++ ret = grub_efi_max_usable_address(); ++#else + ret = address; ++#endif + status = efi_call_4 (b->allocate_pages, alloctype, memtype, pages, &ret); + grub_efi_free_pages (0, pages); + if (status != GRUB_EFI_SUCCESS) +@@ -195,9 +203,14 @@ grub_efi_allocate_pages_real (grub_efi_physical_address_t address, + void * + grub_efi_allocate_any_pages (grub_efi_uintn_t pages) + { +- return grub_efi_allocate_pages_real (GRUB_EFI_MAX_USABLE_ADDRESS, ++#ifdef GRUB_CPU_LOONGARCH64 ++ return grub_efi_allocate_pages_real (grub_efi_max_usable_address(), + pages, GRUB_EFI_ALLOCATE_MAX_ADDRESS, + GRUB_EFI_LOADER_DATA); ++#else ++ return grub_efi_allocate_pages_real (GRUB_EFI_MAX_USABLE_ADDRESS, ++ pages, GRUB_EFI_ALLOCATE_MAX_ADDRESS, ++#endif GRUB_EFI_LOADER_DATA); + } + + void * +@@ -471,7 +484,9 @@ filter_memory_map (grub_efi_memory_descriptor_t *memory_map, + desc = NEXT_MEMORY_DESCRIPTOR (desc, desc_size)) + { + if (desc->type == GRUB_EFI_CONVENTIONAL_MEMORY +-#if 1 ++#ifdef GRUB_CPU_LOONGARCH64 ++ && desc->physical_start <= grub_efi_max_usable_address() ++#else + && desc->physical_start <= GRUB_EFI_MAX_ALLOCATION_ADDRESS + #endif + && desc->physical_start + PAGES_TO_BYTES (desc->num_pages) > 0x100000 +@@ -486,8 +501,14 @@ filter_memory_map (grub_efi_memory_descriptor_t *memory_map, + - desc->physical_start); + desc->physical_start = 0x100000; + } +- +-#if 1 ++#ifdef GRUB_CPU_LOONGARCH64 ++ if (BYTES_TO_PAGES (filtered_desc->physical_start) ++ + filtered_desc->num_pages ++ > BYTES_TO_PAGES_DOWN (grub_efi_max_usable_address())) ++ filtered_desc->num_pages ++ = (BYTES_TO_PAGES_DOWN (grub_efi_max_usable_address()) ++ - BYTES_TO_PAGES (filtered_desc->physical_start)); ++#else + if (BYTES_TO_PAGES (filtered_desc->physical_start) + + filtered_desc->num_pages + > BYTES_TO_PAGES_DOWN (GRUB_EFI_MAX_ALLOCATION_ADDRESS)) +diff --git a/grub-core/kern/elfXX.c b/grub-core/kern/elfXX.c +index 1859d18..8b1dce9 100644 +--- a/grub-core/kern/elfXX.c ++++ b/grub-core/kern/elfXX.c +@@ -134,6 +134,12 @@ grub_elfXX_load (grub_elf_t elf, const char *filename, + load_addr &= 0x3FFFFFFFFFFFFFFFULL; + break; + } ++#ifdef GRUB_CPU_LOONGARCH64 ++ grub_uint64_t addr; ++ asm volatile ("csrrd %0, 0x181" : "=r" (addr)); ++ if ((load_addr >> 48) != (addr >> 48)) ++ return grub_error (GRUB_ERR_BAD_OS, "bad address space"); ++#endif + load_addr += (grub_addr_t) load_offset; + + if (load_addr < load_base) +diff --git a/grub-core/kern/loongarch64/cache.S b/grub-core/kern/loongarch64/cache.S +new file mode 100644 +index 0000000..d291c67 +--- /dev/null ++++ b/grub-core/kern/loongarch64/cache.S +@@ -0,0 +1,26 @@ ++/* ++ * GRUB -- GRand Unified Bootloader ++ * Copyright (C) 2009,2017 Free Software Foundation, Inc. ++ * ++ * GRUB is free software: you can redistribute it and/or modify ++ * it under the terms of the GNU General Public License as published by ++ * the Free Software Foundation, either version 3 of the License, or ++ * (at your option) any later version. ++ * ++ * GRUB is distributed in the hope that it will be useful, ++ * but WITHOUT ANY WARRANTY; without even the implied warranty of ++ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ++ * GNU General Public License for more details. ++ * ++ * You should have received a copy of the GNU General Public License ++ * along with GRUB. If not, see . ++ */ ++ ++#include ++ ++FUNCTION (grub_arch_sync_caches) ++ jr $ra ++ ++FUNCTION (grub_arch_sync_dma_caches) ++ jr $ra ++ +diff --git a/grub-core/kern/loongarch64/dl.c b/grub-core/kern/loongarch64/dl.c +new file mode 100644 +index 0000000..a6fd387 +--- /dev/null ++++ b/grub-core/kern/loongarch64/dl.c +@@ -0,0 +1,258 @@ ++/* loongarch64/dl.c - arch-dependent part of loadable module support */ ++/* ++ * GRUB -- GRand Unified Bootloader ++ * Copyright (C) 2002,2005,2007,2009,2017 Free Software Foundation, Inc. ++ * ++ * GRUB is free software: you can redistribute it and/or modify ++ * it under the terms of the GNU General Public License as published by ++ * the Free Software Foundation, either version 3 of the License, or ++ * (at your option) any later version. ++ * ++ * GRUB is distributed in the hope that it will be useful, ++ * but WITHOUT ANY WARRANTY; without even the implied warranty of ++ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ++ * GNU General Public License for more details. ++ * ++ * You should have received a copy of the GNU General Public License ++ * along with GRUB. If not, see . ++ */ ++ ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++ ++/* Check if EHDR is a valid ELF header. */ ++grub_err_t ++grub_arch_dl_check_header (void *ehdr) ++{ ++ Elf_Ehdr *e = ehdr; ++ ++ /* Check the magic numbers. */ ++ if (e->e_ident[EI_CLASS] != ELFCLASS64 ++ || e->e_ident[EI_DATA] != ELFDATA2LSB ++ || e->e_machine != EM_LOONGARCH64) ++ return grub_error (GRUB_ERR_BAD_OS, N_("invalid arch-dependent ELF magic")); ++ ++ return GRUB_ERR_NONE; ++} ++ ++#pragma GCC diagnostic ignored "-Wcast-align" ++ ++grub_err_t ++grub_arch_dl_get_tramp_got_size (const void *ehdr __attribute__ ((unused)), ++ grub_size_t *tramp, grub_size_t *got) ++{ ++ *tramp = 0; ++ *got = 0; ++ return GRUB_ERR_NONE; ++} ++ ++/* Relocate symbols. */ ++grub_err_t ++grub_arch_dl_relocate_symbols (grub_dl_t mod, void *ehdr, ++ Elf_Shdr *s, grub_dl_segment_t seg) ++{ ++ Elf_Ehdr *e = ehdr; ++ Elf_Rel *rel, *max; ++ grub_uint64_t oprs[10240]={0}; ++ int opri=-1; ++ grub_uint32_t la_abs = 0; ++ ++ for (rel = (Elf_Rel *) ((char *) e + s->sh_offset), ++ max = (Elf_Rel *) ((char *) rel + s->sh_size); ++ rel < max; ++ rel = (Elf_Rel *) ((char *) rel + s->sh_entsize)) ++ { ++ grub_uint8_t *addr; ++ Elf_Sym *sym; ++ Elf_Addr r_info; ++ grub_uint64_t sym_value; ++ ++ if (seg->size < rel->r_offset) ++ return grub_error (GRUB_ERR_BAD_MODULE, ++ "reloc offset is out of the segment"); ++ ++ r_info = (grub_uint64_t) (rel->r_info); ++ ++ addr = (grub_uint8_t *) ((char*)seg->addr + rel->r_offset); ++ sym = (Elf_Sym *) ((char*)mod->symtab ++ + mod->symsize * ELF_R_SYM (r_info)); ++ sym_value = sym->st_value; ++ if (s->sh_type == SHT_RELA) ++ { ++ sym_value += ((Elf_Rela *) rel)->r_addend; ++ } ++ switch (ELF_R_TYPE (r_info)) ++ { ++ case R_LARCH_64: ++ { ++ *(grub_uint64_t *)addr=(grub_uint64_t)sym_value; ++ } ++ break; ++ case R_LARCH_MARK_LA: ++ { ++ la_abs=1; ++ } ++ break; ++ case R_LARCH_SOP_PUSH_PCREL: ++ { ++ opri++; ++ oprs[opri]=(grub_uint64_t)(sym_value-(grub_uint64_t)addr); ++ } ++ break; ++ case R_LARCH_SOP_PUSH_ABSOLUTE: ++ { ++ opri++; ++ oprs[opri]=(grub_uint64_t)sym_value; ++ } ++ break; ++ case R_LARCH_SOP_PUSH_PLT_PCREL: ++ { ++ opri++; ++ oprs[opri]=(grub_uint64_t)(sym_value-(grub_uint64_t)addr); ++ } ++ break; ++ case R_LARCH_SOP_SUB: ++ { ++ grub_uint64_t opr2=oprs[opri]; ++ opri--; ++ grub_uint64_t opr1=oprs[opri]; ++ opri--; ++ opri++; ++ oprs[opri]=opr1 - opr2; ++ } ++ break; ++ case R_LARCH_SOP_SL: ++ { ++ grub_uint64_t opr2=oprs[opri]; ++ opri--; ++ grub_uint64_t opr1=oprs[opri]; ++ opri--; ++ opri++; ++ oprs[opri]=opr1 << opr2; ++ } ++ break; ++ case R_LARCH_SOP_SR: ++ { ++ grub_uint64_t opr2=oprs[opri]; ++ opri--; ++ grub_uint64_t opr1=oprs[opri]; ++ opri--; ++ opri++; ++ oprs[opri]=opr1 >> opr2; ++ } ++ break; ++ case R_LARCH_SOP_ADD: ++ { ++ grub_uint64_t opr2=oprs[opri]; ++ opri--; ++ grub_uint64_t opr1=oprs[opri]; ++ opri--; ++ opri++; ++ oprs[opri]=opr1 + opr2; ++ } ++ break; ++ case R_LARCH_SOP_AND: ++ { ++ grub_uint64_t opr2=oprs[opri]; ++ opri--; ++ grub_uint64_t opr1=oprs[opri]; ++ opri--; ++ opri++; ++ oprs[opri]=opr1 & opr2; ++ } ++ break; ++ case R_LARCH_SOP_IF_ELSE: ++ { ++ grub_uint64_t opr3=oprs[opri]; ++ opri--; ++ grub_uint64_t opr2=oprs[opri]; ++ opri--; ++ grub_uint64_t opr1=oprs[opri]; ++ opri--; ++ if(opr1){ ++ opri++; ++ oprs[opri]=opr2; ++ } else { ++ opri++; ++ oprs[opri]=opr3; ++ } ++ } ++ break; ++ case R_LARCH_SOP_POP_32_S_10_5: ++ { ++ grub_uint64_t opr1 = oprs[opri]; ++ opri--; ++ *(grub_uint64_t *)addr=(*(grub_uint64_t *)addr) | ((opr1 & 0x1f) << 10); ++ } ++ break; ++ case R_LARCH_SOP_POP_32_U_10_12: ++ { ++ grub_uint64_t opr1 = oprs[opri]; ++ opri--; ++ *(grub_uint64_t *)addr=(*(grub_uint64_t *)addr) | ((opr1 & 0xfff) << 10); ++ } ++ break; ++ case R_LARCH_SOP_POP_32_S_10_12: ++ { ++ if(la_abs==1) ++ la_abs=0; ++ grub_uint64_t opr1 = oprs[opri]; ++ opri--; ++ *(grub_uint64_t *)addr= (*(grub_uint64_t *)addr) | ((opr1 & 0xfff) << 10); ++ } ++ break; ++ case R_LARCH_SOP_POP_32_S_10_16: ++ { ++ grub_uint64_t opr1 = oprs[opri]; ++ opri--; ++ *(grub_uint64_t *)addr= (*(grub_uint64_t *)addr) | ((opr1 & 0xffff) << 10); ++ } ++ break; ++ case R_LARCH_SOP_POP_32_S_10_16_S2: ++ { ++ grub_uint64_t opr1 = oprs[opri]; ++ opri--; ++ *(grub_uint64_t *)addr= (*(grub_uint64_t *)addr) | (((opr1 >> 2) & 0xffff) << 10); ++ } ++ break; ++ case R_LARCH_SOP_POP_32_S_5_20: ++ { ++ grub_uint64_t opr1 = oprs[opri]; ++ opri--; ++ *(grub_uint64_t *)addr= (*(grub_uint64_t *)addr) | ((opr1 & 0xfffff)<<5) ; ++ } ++ break; ++ case R_LARCH_SOP_POP_32_S_0_5_10_16_S2: ++ { ++ grub_uint64_t opr1 = oprs[opri]; ++ opri--; ++ *(grub_uint64_t *)addr=(*(grub_uint64_t *)addr) | (((opr1 >> 2) & 0xffff) << 10); ++ *(grub_uint64_t *)addr=(*(grub_uint64_t *)addr) | ((opr1 >> 18) & 0x1f); ++ } ++ break; ++ case R_LARCH_SOP_POP_32_S_0_10_10_16_S2: ++ { ++ grub_uint64_t opr1 = oprs[opri]; ++ opri--; ++ *(grub_uint64_t *)addr=(*(grub_uint64_t *)addr) | (((opr1 >> 2) & 0xffff) << 10); ++ *(grub_uint64_t *)addr=(*(grub_uint64_t *)addr) | ((opr1 >> 18) & 0x3ff); ++ } ++ break; ++ default: ++ { ++ return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET, ++ N_("relocation 0x%x is not implemented yet"), ++ ELF_R_TYPE (r_info)); ++ } ++ break; ++ } ++ } ++ ++ return GRUB_ERR_NONE; ++} ++ +diff --git a/grub-core/kern/loongarch64/efi/init.c b/grub-core/kern/loongarch64/efi/init.c +new file mode 100644 +index 0000000..b21d4f1 +--- /dev/null ++++ b/grub-core/kern/loongarch64/efi/init.c +@@ -0,0 +1,76 @@ ++/* init.c - initialize an arm-based EFI system */ ++/* ++ * GRUB -- GRand Unified Bootloader ++ * Copyright (C) 2013 Free Software Foundation, Inc. ++ * ++ * GRUB is free software: you can redistribute it and/or modify ++ * it under the terms of the GNU General Public License as published by ++ * the Free Software Foundation, either version 3 of the License, or ++ * (at your option) any later version. ++ * ++ * GRUB is distributed in the hope that it will be useful, ++ * but WITHOUT ANY WARRANTY; without even the implied warranty of ++ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ++ * GNU General Public License for more details. ++ * ++ * You should have received a copy of the GNU General Public License ++ * along with GRUB. If not, see . ++ */ ++ ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++ ++static grub_uint64_t tmr; ++static grub_efi_event_t tmr_evt; ++ ++static grub_uint64_t ++grub_efi_get_time_ms (void) ++{ ++ return tmr; ++} ++ ++static void ++grub_loongson_increment_timer (grub_efi_event_t event __attribute__ ((unused)), ++ void *context __attribute__ ((unused))) ++{ ++ tmr += 10; ++} ++ ++ ++ ++void ++grub_machine_init (void) ++{ ++ grub_efi_boot_services_t *b; ++ ++ grub_efi_init (); ++ ++ b = grub_efi_system_table->boot_services; ++ efi_call_5 (b->create_event, GRUB_EFI_EVT_TIMER | GRUB_EFI_EVT_NOTIFY_SIGNAL, ++ GRUB_EFI_TPL_CALLBACK, grub_loongson_increment_timer, NULL, &tmr_evt); ++ efi_call_3 (b->set_timer, tmr_evt, GRUB_EFI_TIMER_PERIODIC, 100000); ++ ++ grub_install_get_time_ms (grub_efi_get_time_ms); ++} ++ ++void ++grub_machine_fini (int flags) ++{ ++ grub_efi_boot_services_t *b; ++ ++ if (!(flags & GRUB_LOADER_FLAG_NORETURN)) ++ return; ++ ++ b = grub_efi_system_table->boot_services; ++ ++ efi_call_3 (b->set_timer, tmr_evt, GRUB_EFI_TIMER_CANCEL, 0); ++ efi_call_1 (b->close_event, tmr_evt); ++ ++ grub_efi_fini (); ++} +diff --git a/grub-core/kern/loongarch64/efi/startup.S b/grub-core/kern/loongarch64/efi/startup.S +new file mode 100644 +index 0000000..1ffff08 +--- /dev/null ++++ b/grub-core/kern/loongarch64/efi/startup.S +@@ -0,0 +1,45 @@ ++/* ++ * GRUB -- GRand Unified Bootloader ++ * Copyright (C) 2013 Free Software Foundation, Inc. ++ * ++ * GRUB is free software: you can redistribute it and/or modify ++ * it under the terms of the GNU General Public License as published by ++ * the Free Software Foundation, either version 3 of the License, or ++ * (at your option) any later version. ++ * ++ * GRUB is distributed in the hope that it will be useful, ++ * but WITHOUT ANY WARRANTY; without even the implied warranty of ++ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ++ * GNU General Public License for more details. ++ * ++ * You should have received a copy of the GNU General Public License ++ * along with GRUB. If not, see . ++ */ ++ ++#include ++ ++ .file "startup.S" ++ .text ++ .globl start, _start ++ .align 4 ++ ++FUNCTION(start) ++FUNCTION(_start) ++ /* ++ * EFI_SYSTEM_TABLE and EFI_HANDLE are passed in a1/a0. ++ */ ++ addi.d $sp, $sp, -16 ++ st.d $ra, $sp, 0 ++ ++ la $a2, grub_efi_image_handle ++ st.d $a0, $a2, 0 ++ la $a2, grub_efi_system_table ++ st.d $a1, $a2, 0 ++ ++ bl grub_main ++ ++1: ++ ld.d $ra, $sp, 0 ++ addi.d $sp, $sp, 16 ++ jr $ra ++ +diff --git a/grub-core/kern/loongarch64/init.c b/grub-core/kern/loongarch64/init.c +new file mode 100644 +index 0000000..b2de930 +--- /dev/null ++++ b/grub-core/kern/loongarch64/init.c +@@ -0,0 +1,47 @@ ++/* ++ * GRUB -- GRand Unified Bootloader ++ * Copyright (C) 2009,2017 Free Software Foundation, Inc. ++ * ++ * GRUB is free software: you can redistribute it and/or modify ++ * it under the terms of the GNU General Public License as published by ++ * the Free Software Foundation, either version 3 of the License, or ++ * (at your option) any later version. ++ * ++ * GRUB is distributed in the hope that it will be useful, ++ * but WITHOUT ANY WARRANTY; without even the implied warranty of ++ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ++ * GNU General Public License for more details. ++ * ++ * You should have received a copy of the GNU General Public License ++ * along with GRUB. If not, see . ++ */ ++ ++#include ++#include ++#include ++#include ++ ++grub_uint32_t grub_arch_cpuclock; ++ ++/* FIXME: use interrupt to count high. */ ++grub_uint64_t ++grub_get_rtc (void) ++{ ++ static grub_uint32_t high = 0; ++ static grub_uint32_t last = 0; ++ grub_uint32_t low; ++ ++ asm volatile ("csrrd %0, " GRUB_CPU_LOONGARCH_COP0_TIMER_COUNT : "=r" (low)); ++ if (low < last) ++ high++; ++ last = low; ++ ++ return (((grub_uint64_t) high) << 32) | low; ++} ++ ++void ++grub_timer_init (grub_uint32_t cpuclock) ++{ ++ grub_arch_cpuclock = cpuclock; ++ grub_install_get_time_ms (grub_rtc_get_time_ms); ++} +diff --git a/grub-core/lib/loongarch64/efi/loongson.c b/grub-core/lib/loongarch64/efi/loongson.c +new file mode 100644 +index 0000000..8b60d82 +--- /dev/null ++++ b/grub-core/lib/loongarch64/efi/loongson.c +@@ -0,0 +1,505 @@ ++/* ++ * GRUB -- GRand Unified Bootloader ++ * Copyright (C) 2017 Free Software Foundation, Inc. ++ * ++ * GRUB is free software: you can redistribute it and/or modify ++ * it under the terms of the GNU General Public License as published by ++ * the Free Software Foundation, either version 3 of the License, or ++ * (at your option) any later version. ++ * ++ * GRUB is distributed in the hope that it will be useful, ++ * but WITHOUT ANY WARRANTY; without even the implied warranty of ++ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ++ * GNU General Public License for more details. ++ * ++ * You should have received a copy of the GNU General Public License ++ * along with GRUB. If not, see . ++ */ ++ ++#include ++#include ++#include ++#include ++#include ++#include ++ ++#define loongson_params (&loongson_boot_params->boot_params.efi.smbios.lp) ++#define loongson_boot_params_size ALIGN_UP (sizeof (*loongson_boot_params), 8) ++#define loongson_reset_code_size (&grub_efi_loongson_reset_end - &grub_efi_loongson_reset_start) ++ ++extern grub_uint8_t grub_efi_loongson_reset_start; ++extern grub_uint8_t grub_efi_loongson_reset_end; ++ ++static struct ++{ ++ grub_efi_loongson_boot_params boot_params; ++ grub_efi_loongson_memory_map memory_map; ++ grub_efi_loongson_cpu_info cpu_info; ++ grub_efi_loongson_system_info system_info; ++ grub_efi_loongson_irq_src_routing_table irq_src_routing_table; ++ grub_efi_loongson_interface_info interface_info; ++ grub_efi_loongson_special_attribute special_attribute; ++ grub_efi_loongson_board_devices board_devices; ++} GRUB_PACKED ++* loongson_boot_params; ++ ++static void ++grub_efi_loongson_init_reset_system (void) ++{ ++ grub_efi_loongson_boot_params *boot_params; ++ grub_uint8_t *reset_code_addr = (grub_uint8_t *) loongson_boot_params + ++ loongson_boot_params_size; ++ ++ boot_params = &loongson_boot_params->boot_params; ++ grub_efi_loongson_reset_system_addr = ++ (grub_uint64_t) grub_efi_system_table->runtime_services->reset_system; ++ grub_memcpy (reset_code_addr, &grub_efi_loongson_reset_start, loongson_reset_code_size); ++ grub_arch_sync_caches (reset_code_addr, loongson_reset_code_size); ++ ++ boot_params->reset_system.reset_cold = (grub_uint64_t) reset_code_addr + ++ ((grub_uint64_t) &grub_efi_loongson_reset_cold - ++ (grub_uint64_t) &grub_efi_loongson_reset_start); ++ boot_params->reset_system.reset_warm = (grub_uint64_t) reset_code_addr + ++ ((grub_uint64_t) &grub_efi_loongson_reset_warm - ++ (grub_uint64_t) &grub_efi_loongson_reset_start); ++ boot_params->reset_system.shutdown = (grub_uint64_t) reset_code_addr + ++ ((grub_uint64_t) &grub_efi_loongson_reset_shutdown - ++ (grub_uint64_t) &grub_efi_loongson_reset_start); ++ boot_params->reset_system.do_suspend = (grub_uint64_t) reset_code_addr + ++ ((grub_uint64_t) &grub_efi_loongson_reset_suspend - ++ (grub_uint64_t) &grub_efi_loongson_reset_start); ++} ++ ++static void ++grub_efi_loongson_init_smbios (grub_efi_loongson_smbios_table *smbios_table) ++{ ++ grub_efi_loongson_smbios_table *dst = &loongson_boot_params->boot_params.efi.smbios; ++ ++ dst->vers = smbios_table->vers; ++ dst->vga_bios = smbios_table->vga_bios; ++} ++ ++static void ++grub_efi_loongson_init_cpu_info (grub_efi_loongson_smbios_table *smbios_table) ++{ ++ grub_efi_loongson_cpu_info *src = (void *) smbios_table->lp.cpu_offset; ++ grub_efi_loongson_cpu_info *dst = &loongson_boot_params->cpu_info; ++ ++ if (!src) ++ return; ++ ++ grub_memcpy (dst, src, sizeof (grub_efi_loongson_cpu_info)); ++ loongson_params->cpu_offset = (grub_uint64_t) dst - (grub_uint64_t) loongson_params; ++} ++ ++static void ++grub_efi_loongson_init_system_info (grub_efi_loongson_smbios_table *smbios_table) ++{ ++ grub_efi_loongson_system_info *src = (void *) smbios_table->lp.system_offset; ++ grub_efi_loongson_system_info *dst = &loongson_boot_params->system_info; ++ ++ if (!src) ++ return; ++ ++ grub_memcpy (dst, src, sizeof (grub_efi_loongson_system_info)); ++ loongson_params->system_offset = (grub_uint64_t) dst - (grub_uint64_t) loongson_params; ++} ++ ++static void ++grub_efi_loongson_init_irq_src_routing_table (grub_efi_loongson_smbios_table *smbios_table) ++{ ++ grub_efi_loongson_irq_src_routing_table *src = (void *) smbios_table->lp.irq_offset; ++ grub_efi_loongson_irq_src_routing_table *dst = &loongson_boot_params->irq_src_routing_table; ++ ++ if (!src) ++ return; ++ ++ grub_memcpy (dst, src, sizeof (grub_efi_loongson_irq_src_routing_table)); ++ loongson_params->irq_offset = (grub_uint64_t) dst - (grub_uint64_t) loongson_params; ++} ++ ++static void ++grub_efi_loongson_init_interface_info (grub_efi_loongson_smbios_table *smbios_table) ++{ ++ grub_efi_loongson_interface_info *src = (void *) smbios_table->lp.interface_offset; ++ grub_efi_loongson_interface_info *dst = &loongson_boot_params->interface_info; ++ ++ if (!src) ++ return; ++ ++ grub_memcpy (dst, src, sizeof (grub_efi_loongson_interface_info)); ++ loongson_params->interface_offset = (grub_uint64_t) dst - (grub_uint64_t) loongson_params; ++} ++ ++static void ++grub_efi_loongson_init_special_attribute (grub_efi_loongson_smbios_table *smbios_table) ++{ ++ grub_efi_loongson_special_attribute *src = (void *) smbios_table->lp.special_offset; ++ grub_efi_loongson_special_attribute *dst = &loongson_boot_params->special_attribute; ++ ++ if (!src) ++ return; ++ ++ grub_memcpy (dst, src, sizeof (grub_efi_loongson_special_attribute)); ++ loongson_params->special_offset = (grub_uint64_t) dst - (grub_uint64_t) loongson_params; ++} ++ ++static void ++grub_efi_loongson_init_board_devices (grub_efi_loongson_smbios_table *smbios_table) ++{ ++ grub_efi_loongson_board_devices *src = (void *) smbios_table->lp.boarddev_table_offset; ++ grub_efi_loongson_board_devices *dst = &loongson_boot_params->board_devices; ++ ++ if (!src) ++ return; ++ ++ grub_memcpy (dst, src, sizeof (grub_efi_loongson_board_devices)); ++ loongson_params->boarddev_table_offset = (grub_uint64_t) dst - (grub_uint64_t) loongson_params; ++} ++ ++#define ADD_MEMORY_DESCRIPTOR(desc, size) \ ++ ((grub_efi_memory_descriptor_t *) ((char *) (desc) + (size))) ++ ++static void ++grub_efi_loongson_init_memory_map (grub_efi_loongson_smbios_table *smbios_table, ++ grub_efi_memory_descriptor_t *mmap_buf, ++ grub_efi_uintn_t mmap_size, ++ grub_efi_uintn_t desc_size) ++{ ++ grub_efi_loongson_memory_map *src = (void *) smbios_table->lp.memory_offset; ++ grub_efi_loongson_memory_map *dst = &loongson_boot_params->memory_map; ++ grub_efi_memory_descriptor_t *mmap_end; ++ grub_efi_memory_descriptor_t *desc; ++ grub_efi_memory_descriptor_t *desc_next; ++ grub_efi_uint32_t mem_types_reserved[] = ++ { ++ 1, // GRUB_EFI_RESERVED_MEMORY_TYPE ++ 0, // GRUB_EFI_LOADER_CODE ++ 0, // GRUB_EFI_LOADER_DATA ++ 0, // GRUB_EFI_BOOT_SERVICES_CODE ++ 0, // GRUB_EFI_BOOT_SERVICES_DATA ++ 1, // GRUB_EFI_RUNTIME_SERVICES_CODE ++ 1, // GRUB_EFI_RUNTIME_SERVICES_DATA ++ 0, // GRUB_EFI_CONVENTIONAL_MEMORY ++ 1, // GRUB_EFI_UNUSABLE_MEMORY ++ 0, // GRUB_EFI_ACPI_RECLAIM_MEMORY ++ 0, // GRUB_EFI_ACPI_MEMORY_NVS ++ 1, // GRUB_EFI_MEMORY_MAPPED_IO ++ 1, // GRUB_EFI_MEMORY_MAPPED_IO_PORT_SPACE ++ 1, // GRUB_EFI_PAL_CODE ++ 1, // GRUB_EFI_PERSISTENT_MEMORY ++ }; ++ grub_uint32_t need_sort = 1; ++ ++ if (!src) ++ return; ++ ++ dst->vers = src->vers; ++ dst->nr_map = 0; ++ dst->mem_freq = src->mem_freq; ++ loongson_params->memory_offset = (grub_uint64_t) dst - (grub_uint64_t) loongson_params; ++ ++ if (!mmap_buf || !mmap_size || !desc_size) ++ return; ++ ++ mmap_end = ADD_MEMORY_DESCRIPTOR (mmap_buf, mmap_size); ++ ++ /* drop reserved */ ++ for (desc = mmap_buf, ++ desc_next = desc; ++ desc < mmap_end; ++ desc = ADD_MEMORY_DESCRIPTOR (desc, desc_size)) ++ { ++ desc->type = mem_types_reserved[desc->type]; ++ if (desc->type) ++ continue; ++ ++ if (desc != desc_next) ++ *desc_next = *desc; ++ desc_next = ADD_MEMORY_DESCRIPTOR (desc_next, desc_size); ++ } ++ mmap_end = desc_next; ++ ++ /* sort: low->high */ ++ while (need_sort) ++ { ++ need_sort = 0; ++ ++ for (desc = mmap_buf, ++ desc_next = ADD_MEMORY_DESCRIPTOR (desc, desc_size); ++ (desc < mmap_end) && (desc_next < mmap_end); ++ desc = desc_next, ++ desc_next = ADD_MEMORY_DESCRIPTOR (desc, desc_size)) ++ { ++ grub_efi_memory_descriptor_t tmp; ++ ++ if (desc->physical_start <= desc_next->physical_start) ++ continue; ++ ++ tmp = *desc; ++ *desc = *desc_next; ++ *desc_next = tmp; ++ need_sort = 1; ++ } ++ } ++ ++ /* combine continuous memory map */ ++ for (desc = mmap_buf, ++ desc_next = ADD_MEMORY_DESCRIPTOR (desc, desc_size); ++ desc_next < mmap_end; ++ desc_next = ADD_MEMORY_DESCRIPTOR (desc_next, desc_size)) ++ { ++ grub_efi_physical_address_t prev_end = desc->physical_start + (desc->num_pages << 12); ++ ++ if (prev_end == desc_next->physical_start) ++ { ++ desc->num_pages += desc_next->num_pages; ++ continue; ++ } ++ ++ desc = ADD_MEMORY_DESCRIPTOR (desc, desc_size); ++ grub_memcpy (desc, desc_next, desc_size); ++ } ++ mmap_end = ADD_MEMORY_DESCRIPTOR (desc, desc_size); ++ ++ /* write to loongson memory map */ ++ for (desc = mmap_buf; ++ desc < mmap_end; ++ desc = ADD_MEMORY_DESCRIPTOR (desc, desc_size)) ++ { ++ grub_efi_physical_address_t physical_start = grub_vtop ((void *) desc->physical_start); ++ grub_efi_physical_address_t physical_end = physical_start + (desc->num_pages << 12); ++ ++ physical_start = ALIGN_UP (physical_start, 0x100000); ++ physical_end = ALIGN_DOWN (physical_end, 0x100000); ++ ++ if (physical_start >= physical_end || (physical_end - physical_start) < 0x100000) ++ continue; ++ ++ dst->map[dst->nr_map].node_id = (desc->physical_start >> 44) & 0xf; ++ dst->map[dst->nr_map].mem_type = GRUB_EFI_LOONGSON_SYSTEM_RAM; ++ dst->map[dst->nr_map].mem_start = physical_start; ++ dst->map[dst->nr_map].mem_size = (physical_end - physical_start) >> 20; ++ ++ grub_dprintf ("loongson", "memory map %03u: 0x%016lx 0x%016lx @ %u\n", ++ dst->nr_map, physical_start, physical_end - physical_start, ++ dst->map[dst->nr_map].node_id); ++ ++ dst->nr_map ++; ++ } ++} ++ ++#define BYTES_TO_PAGES(bytes) (((bytes) + 0xfff) >> 12) ++#define SUB_MEMORY_DESCRIPTOR(desc, size) \ ++ ((grub_efi_memory_descriptor_t *) ((char *) (desc) - (size))) ++ ++void ++grub_efi_loongson_alloc_boot_params (void) ++{ ++ grub_efi_memory_descriptor_t *mmap_buf; ++ grub_efi_memory_descriptor_t *mmap_end; ++ grub_efi_memory_descriptor_t *desc; ++ grub_efi_uintn_t mmap_size; ++ grub_efi_uintn_t desc_size; ++ grub_efi_physical_address_t address; ++ grub_efi_allocate_type_t type; ++ grub_efi_uintn_t pages; ++ grub_efi_status_t status; ++ grub_efi_boot_services_t *b; ++ int mm_status; ++ ++ type = GRUB_EFI_ALLOCATE_ADDRESS; ++ pages = BYTES_TO_PAGES (loongson_boot_params_size + loongson_reset_code_size); ++ ++ mmap_size = (1 << 12); ++ mmap_buf = grub_malloc (mmap_size); ++ if (!mmap_buf) ++ grub_fatal ("out of memory!"); ++ ++ mm_status = grub_efi_get_memory_map (&mmap_size, mmap_buf, 0, &desc_size, 0); ++ if (mm_status == 0) ++ { ++ grub_free (mmap_buf); ++ mmap_size += desc_size * 32; ++ ++ mmap_buf = grub_malloc (mmap_size); ++ if (!mmap_buf) ++ grub_fatal ("out of memory!"); ++ ++ mm_status = grub_efi_get_memory_map (&mmap_size, mmap_buf, 0, &desc_size, 0); ++ } ++ ++ if (mm_status < 0) ++ grub_fatal ("cannot get memory map!"); ++ ++ mmap_end = ADD_MEMORY_DESCRIPTOR (mmap_buf, mmap_size); ++ ++ for (desc = SUB_MEMORY_DESCRIPTOR (mmap_end, desc_size); ++ desc >= mmap_buf; ++ desc = SUB_MEMORY_DESCRIPTOR (desc, desc_size)) ++ { ++ if (desc->type != GRUB_EFI_CONVENTIONAL_MEMORY) ++ continue; ++ if (desc->physical_start >= grub_efi_max_usable_address()) ++ continue; ++ if (desc->num_pages < pages) ++ continue; ++ ++ address = desc->physical_start; ++ break; ++ } ++ ++ grub_free (mmap_buf); ++ ++ b = grub_efi_system_table->boot_services; ++ status = efi_call_4 (b->allocate_pages, type, GRUB_EFI_RUNTIME_SERVICES_DATA, pages, &address); ++ if (status != GRUB_EFI_SUCCESS) ++ grub_fatal ("cannot allocate Loongson boot parameters!"); ++ ++ loongson_boot_params = (void *) ((grub_addr_t) address); ++} ++ ++void ++grub_efi_loongson_free_boot_params (void) ++{ ++ grub_efi_free_pages ((grub_addr_t) loongson_boot_params, ++ BYTES_TO_PAGES (loongson_boot_params_size + loongson_reset_code_size)); ++} ++ ++void * ++grub_efi_loongson_get_smbios_table (void) ++{ ++ static grub_efi_loongson_smbios_table *smbios_table; ++ grub_efi_loongson_boot_params *old_boot_params; ++ struct bootparamsinterface* boot_params; ++ void * tmp_boot_params = NULL; ++ char * p = NULL; ++ if(smbios_table) ++ return smbios_table; ++ ++ tmp_boot_params = grub_efi_loongson_get_boot_params(); ++ if(tmp_boot_params == NULL) ++ { ++ grub_dprintf("loongson", "tmp_boot_params is NULL\n"); ++ return tmp_boot_params; ++ } ++ ++ boot_params = (struct bootparamsinterface *)tmp_boot_params; ++ p = (char *)&(boot_params->signature); ++ if(grub_strncmp(p, "BPI", 3) == 0) ++ { ++ grub_dprintf("loongson", "find new bpi\n"); ++ return boot_params ? boot_params : 0; ++ } ++ else ++ { ++ old_boot_params = (grub_efi_loongson_boot_params *)tmp_boot_params; ++ /* ++ { ++ grub_dprintf("loongson", "smbios addr%llx\n", &old_boot_params->efi.smbios); ++ grub_dprintf("loongson", "smbios vers%d\n", (grub_uint16_t)(&old_boot_params->efi.smbios.vers)); ++ grub_dprintf("loongson", "smbios vga_bios%d\n", &old_boot_params->efi.smbios.vga_bios); ++ grub_dprintf("loongson", "lp memory offset %llx\n", &old_boot_params->efi.smbios.lp.memory_offset); ++ grub_dprintf("loongson", "lp cpu offset %llx\n", &old_boot_params->efi.smbios.lp.cpu_offset); ++ grub_dprintf("loongson", "lp system offset %llx\n", &old_boot_params->efi.smbios.lp.system_offset); ++ grub_dprintf("loongson", "lp irq offset %llx\n", &old_boot_params->efi.smbios.lp.irq_offset); ++ grub_dprintf("loongson", "lp interface offset %llx\n", &old_boot_params->efi.smbios.p.interface_offset); ++ grub_dprintf("loongson", "lp special offset %llx\n", &old_boot_params->efi.smbios.lp.special_offset); ++ grub_dprintf("loongson", "lp boarddev table offset %llx\n", &old_boot_params->efi.smbios.lp.boarddev_table_offset); ++ } ++ */ ++ return old_boot_params ? &old_boot_params->efi.smbios : 0; ++ } ++ ++} ++ ++int ++grub_efi_is_loongson (void) ++{ ++ return grub_efi_loongson_get_smbios_table () ? 1 : 0; ++} ++ ++void * ++grub_efi_loongson_get_boot_params (void) ++{ ++ static void * boot_params = NULL; ++ grub_efi_configuration_table_t *tables; ++ grub_efi_guid_t smbios_guid = GRUB_EFI_LOONGSON_SMBIOS_TABLE_GUID; ++ unsigned int i; ++ ++ if (boot_params) ++ return boot_params; ++ ++ /* Look for Loongson SMBIOS in UEFI config tables. */ ++ tables = grub_efi_system_table->configuration_table; ++ ++ for (i = 0; i < grub_efi_system_table->num_table_entries; i++) ++ if (grub_memcmp (&tables[i].vendor_guid, &smbios_guid, sizeof (smbios_guid)) == 0) ++ { ++ boot_params= tables[i].vendor_table; ++ grub_dprintf ("loongson", "found registered SMBIOS @ %p\n", boot_params); ++ break; ++ } ++ return boot_params; ++} ++ ++grub_uint8_t ++grub_efi_loongson_calculatesum8 (const grub_uint8_t *buffer, grub_efi_uintn_t length) ++{ ++ grub_uint8_t sum; ++ grub_efi_uintn_t count; ++ ++ for (sum = 0, count = 0; count < length; count++) ++ { ++ sum = (grub_uint8_t) (sum + *(buffer + count)); ++ } ++ return sum; ++} ++ ++grub_uint8_t ++grub_efi_loongson_grub_calculatechecksum8 (const grub_uint8_t *buffer, grub_efi_uintn_t length) ++{ ++ grub_uint8_t checksum; ++ ++ checksum = grub_efi_loongson_calculatesum8(buffer, length); ++ ++ return (grub_uint8_t) (0x100 - checksum); ++} ++ ++ ++grub_uint32_t ++grub_efi_loongson_memmap_sort(struct memmap array[], grub_uint32_t length, mem_map * bpmem, grub_uint32_t index, grub_uint32_t memtype) ++{ ++ grub_uint64_t tempmemsize = 0; ++ grub_uint32_t j = 0; ++ grub_uint32_t t = 0; ++ ++ for(j = 0; j < length;) ++ { ++ tempmemsize = array[j].memsize; ++ for(t = j + 1; t < length; t++) ++ { ++ if(array[j].memstart + tempmemsize == array[t].memstart) ++ { ++ tempmemsize += array[t].memsize; ++ } ++ else ++ { ++ break; ++ } ++ } ++ bpmem->map[index].memtype = memtype; ++ bpmem->map[index].memstart = array[j].memstart; ++ bpmem->map[index].memsize = tempmemsize; ++ grub_dprintf("loongson", "map[%d]:type %x, start 0x%llx, end 0x%llx\n", ++ index, ++ bpmem->map[index].memtype, ++ bpmem->map[index].memstart, ++ bpmem->map[index].memstart+ bpmem->map[index].memsize ++ ); ++ j = t; ++ index++; ++ } ++ return index; ++} ++ +diff --git a/grub-core/lib/loongarch64/efi/loongson_asm.S b/grub-core/lib/loongarch64/efi/loongson_asm.S +new file mode 100644 +index 0000000..4a04d34 +--- /dev/null ++++ b/grub-core/lib/loongarch64/efi/loongson_asm.S +@@ -0,0 +1,58 @@ ++/* ++ * GRUB -- GRand Unified Bootloader ++ * Copyright (C) 2017 Free Software Foundation, Inc. ++ * ++ * GRUB is free software: you can redistribute it and/or modify ++ * it under the terms of the GNU General Public License as published by ++ * the Free Software Foundation, either version 3 of the License, or ++ * (at your option) any later version. ++ * ++ * GRUB is distributed in the hope that it will be useful, ++ * but WITHOUT ANY WARRANTY; without even the implied warranty of ++ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ++ * GNU General Public License for more details. ++ * ++ * You should have received a copy of the GNU General Public License ++ * along with GRUB. If not, see . ++ */ ++ ++#include ++ ++ .file "loongson_asm.S" ++ .text ++ ++ .align 4 ++ ++VARIABLE (grub_efi_loongson_reset_start) ++ ++VARIABLE (grub_efi_loongson_reset_system_addr) ++ .dword 0 ++ ++reset_system: ++ bl 1f ++ move $a1, $zero ++1: ++ ld.d $t8, $ra, -16 ++ move $a2, $zero ++ jr $t8 ++ move $a3, $zero ++ ++FUNCTION(grub_efi_loongson_reset_cold) ++ b reset_system ++ li.w $a0, 0 ++ ++FUNCTION(grub_efi_loongson_reset_warm) ++ b reset_system ++ li.w $a0, 1 ++ ++FUNCTION(grub_efi_loongson_reset_shutdown) ++ b reset_system ++ li.w $a0, 2 ++ ++FUNCTION(grub_efi_loongson_reset_suspend) ++ b reset_system ++ li.w $a0, 3 ++ ++VARIABLE (grub_efi_loongson_reset_end) ++ ++ +diff --git a/grub-core/lib/loongarch64/relocator.c b/grub-core/lib/loongarch64/relocator.c +new file mode 100644 +index 0000000..f6c1b01 +--- /dev/null ++++ b/grub-core/lib/loongarch64/relocator.c +@@ -0,0 +1,163 @@ ++/* ++ * GRUB -- GRand Unified Bootloader ++ * Copyright (C) 2017 Free Software Foundation, Inc. ++ * ++ * GRUB is free software: you can redistribute it and/or modify ++ * it under the terms of the GNU General Public License as published by ++ * the Free Software Foundation, either version 3 of the License, or ++ * (at your option) any later version. ++ * ++ * GRUB is distributed in the hope that it will be useful, ++ * but WITHOUT ANY WARRANTY; without even the implied warranty of ++ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ++ * GNU General Public License for more details. ++ * ++ * You should have received a copy of the GNU General Public License ++ * along with GRUB. If not, see . ++ */ ++ ++#include ++#include ++ ++#include ++#include ++#include ++#include ++ ++#include ++#include ++ ++extern grub_uint8_t grub_relocator_forward_start; ++extern grub_uint8_t grub_relocator_forward_end; ++extern grub_uint8_t grub_relocator_backward_start; ++extern grub_uint8_t grub_relocator_backward_end; ++ ++#define REGW_SIZEOF (4 * sizeof (grub_uint32_t)) ++#define JUMP_SIZEOF (2 * sizeof (grub_uint32_t)) ++ ++#define RELOCATOR_SRC_SIZEOF(x) (&grub_relocator_##x##_end \ ++ - &grub_relocator_##x##_start) ++#define RELOCATOR_SIZEOF(x) (RELOCATOR_SRC_SIZEOF(x) \ ++ + REGW_SIZEOF * 3) ++grub_size_t grub_relocator_align = sizeof (grub_uint64_t); ++grub_size_t grub_relocator_forward_size; ++grub_size_t grub_relocator_backward_size; ++grub_size_t grub_relocator_jumper_size = JUMP_SIZEOF + REGW_SIZEOF; ++ ++void ++grub_cpu_relocator_init (void) ++{ ++ grub_relocator_forward_size = RELOCATOR_SIZEOF(forward); ++ grub_relocator_backward_size = RELOCATOR_SIZEOF(backward); ++} ++ ++static void ++write_reg (int regn, grub_uint64_t val, void **target) ++{ ++ grub_uint32_t lu12iw=0x14000000; ++ grub_uint32_t ori=0x03800000; ++ grub_uint32_t lu32id=0x16000000; ++ grub_uint32_t lu52id=0x03000000; ++ ++ *(grub_uint32_t *) *target = (lu12iw | (grub_uint32_t)((val & 0xfffff000)>>12<<5) | (grub_uint32_t)regn);; ++ *target = ((grub_uint32_t *) *target) + 1; ++ *(grub_uint32_t *) *target = (ori | (grub_uint32_t)((val & 0xfff)<<10) | (grub_uint32_t)(regn | regn<<5)); ++ *target = ((grub_uint32_t *) *target) + 1; ++ *(grub_uint32_t *) *target = (lu32id | (grub_uint32_t)((val & 0xfffff00000000)>>32<<5) | (grub_uint32_t)regn);; ++ *target = ((grub_uint32_t *) *target) + 1; ++ *(grub_uint32_t *) *target = (lu52id | (grub_uint32_t)((val & 0xfff0000000000000)>>52<<10) | (grub_uint32_t)(regn | regn<<5));; ++ *target = ((grub_uint32_t *) *target) + 1; ++} ++ ++static void ++write_jump (int regn, void **target) ++{ ++ grub_uint32_t andi=0x4c000000; ++ grub_uint32_t nop=0x03400000; ++ ++ *(grub_uint32_t *) *target = (andi | (grub_uint32_t)(regn<<5)); ++ *target = ((grub_uint32_t *) *target) + 1; ++ *(grub_uint32_t *) *target = nop; ++ *target = ((grub_uint32_t *) *target) + 1; ++} ++ ++void ++grub_cpu_relocator_jumper (void *rels, grub_addr_t addr) ++{ ++ write_reg (1, addr, &rels); ++ write_jump (1, &rels); ++} ++ ++void ++grub_cpu_relocator_backward (void *ptr0, void *src, void *dest, ++ grub_size_t size) ++{ ++ void *ptr = ptr0; ++ write_reg (8, (grub_uint64_t) src, &ptr); ++ write_reg (9, (grub_uint64_t) dest, &ptr); ++ write_reg (10, (grub_uint64_t) size, &ptr); ++ grub_memcpy (ptr, &grub_relocator_backward_start, ++ RELOCATOR_SRC_SIZEOF (backward)); ++} ++ ++void ++grub_cpu_relocator_forward (void *ptr0, void *src, void *dest, ++ grub_size_t size) ++{ ++ void *ptr = ptr0; ++ write_reg (8, (grub_uint64_t) src, &ptr); ++ write_reg (9, (grub_uint64_t) dest, &ptr); ++ write_reg (10, (grub_uint64_t) size, &ptr); ++ grub_memcpy (ptr, &grub_relocator_forward_start, ++ RELOCATOR_SRC_SIZEOF (forward)); ++} ++ ++grub_err_t ++grub_relocator64_boot (struct grub_relocator *rel, ++ struct grub_relocator64_state state) ++{ ++ grub_relocator_chunk_t ch; ++ void *ptr; ++ grub_err_t err; ++ void *relst; ++ grub_size_t relsize; ++ grub_size_t stateset_size = 31 * REGW_SIZEOF + JUMP_SIZEOF; ++ unsigned i; ++ grub_addr_t vtarget; ++ ++ err = grub_relocator_alloc_chunk_align (rel, &ch, 0, ++ (0xffffffff - stateset_size) ++ + 1, stateset_size, ++ grub_relocator_align, ++ GRUB_RELOCATOR_PREFERENCE_NONE, 0); ++ if (err) ++ return err; ++ ++ ptr = get_virtual_current_address (ch); ++ for (i = 1; i < 32; i++) ++ write_reg (i, state.gpr[i], &ptr); ++ write_jump (state.jumpreg, &ptr); ++ ++ vtarget = (grub_addr_t) grub_map_memory (get_physical_target_address (ch), ++ stateset_size); ++ ++ err = grub_relocator_prepare_relocs (rel, vtarget, &relst, &relsize); ++ if (err) ++ return err; ++ ++ grub_arch_sync_caches ((void *) relst, relsize); ++ ++ grub_uint64_t val; ++ __asm__ __volatile__( ++ "li.w %0, 0x4\n\t" ++ "csrxchg $r0, %0, 0x0\n\t" ++ : "=r"(val) ++ : ++ : ++ ); ++ ++ ((void (*) (void)) relst) (); ++ ++ /* Not reached. */ ++ return GRUB_ERR_NONE; ++} +diff --git a/grub-core/lib/loongarch64/relocator_asm.S b/grub-core/lib/loongarch64/relocator_asm.S +new file mode 100644 +index 0000000..cf1724d +--- /dev/null ++++ b/grub-core/lib/loongarch64/relocator_asm.S +@@ -0,0 +1,51 @@ ++/* ++ * GRUB -- GRand Unified Bootloader ++ * Copyright (C) 2017 Free Software Foundation, Inc. ++ * ++ * GRUB is free software: you can redistribute it and/or modify ++ * it under the terms of the GNU General Public License as published by ++ * the Free Software Foundation, either version 3 of the License, or ++ * (at your option) any later version. ++ * ++ * GRUB is distributed in the hope that it will be useful, ++ * but WITHOUT ANY WARRANTY; without even the implied warranty of ++ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ++ * GNU General Public License for more details. ++ * ++ * You should have received a copy of the GNU General Public License ++ * along with GRUB. If not, see . ++ */ ++ ++#include ++ ++ .p2align 4 /* force 16-byte alignment */ ++ ++VARIABLE (grub_relocator_forward_start) ++ ++copycont1: ++ ld.d $r11,$r8,0 ++ st.d $r11,$r9,0 ++ addi.d $r8, $r8, 8 ++ addi.d $r10, $r10, -8 ++ addi.d $r9, $r9, 8 ++ bne $r10, $r0, copycont1 ++ ++VARIABLE (grub_relocator_forward_end) ++ ++VARIABLE (grub_relocator_backward_start) ++ ++ add.d $r9, $r9, $r10 ++ add.d $r8, $r8, $r10 ++ /* Backward movsl is implicitly off-by-one. compensate that. */ ++ addi.d $r9, $r9, -8 ++ addi.d $r8, $r8, -8 ++copycont2: ++ ld.w $r11,$r8,0 ++ st.w $r11,$r9,0 ++ addi.d $r8, $r8, -8 ++ addi.d $r10, $r10, -8 ++ addi.d $r9, $r9, -8 ++ bne $r10, $r0, copycont2 ++ ++VARIABLE (grub_relocator_backward_end) ++ +diff --git a/grub-core/lib/loongarch64/setjmp.S b/grub-core/lib/loongarch64/setjmp.S +new file mode 100644 +index 0000000..47db814 +--- /dev/null ++++ b/grub-core/lib/loongarch64/setjmp.S +@@ -0,0 +1,74 @@ ++/* ++ * GRUB -- GRand Unified Bootloader ++ * Copyright (C) 2003,2007,2009 Free Software Foundation, Inc. ++ * ++ * GRUB is free software: you can redistribute it and/or modify ++ * it under the terms of the GNU General Public License as published by ++ * the Free Software Foundation, either version 3 of the License, or ++ * (at your option) any later version. ++ * ++ * GRUB is distributed in the hope that it will be useful, ++ * but WITHOUT ANY WARRANTY; without even the implied warranty of ++ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ++ * GNU General Public License for more details. ++ * ++ * You should have received a copy of the GNU General Public License ++ * along with GRUB. If not, see . ++ */ ++ ++#include ++#include ++#include ++ ++ .file "setjmp.S" ++ ++GRUB_MOD_LICENSE "GPLv3+" ++ ++ .text ++ ++/* ++ * int grub_setjmp (grub_jmp_buf env) ++ */ ++FUNCTION(grub_setjmp) ++ GRUB_ASM_REG_S $s0, $a0,0 ++ GRUB_ASM_REG_S $s1, $a0,8 ++ GRUB_ASM_REG_S $s2, $a0,16 ++ GRUB_ASM_REG_S $s3, $a0,24 ++ GRUB_ASM_REG_S $s4, $a0,32 ++ GRUB_ASM_REG_S $s5, $a0,40 ++ GRUB_ASM_REG_S $s6, $a0,48 ++ GRUB_ASM_REG_S $s7, $a0,56 ++ GRUB_ASM_REG_S $s8, $a0,64 ++ GRUB_ASM_REG_S $fp, $a0,72 ++ GRUB_ASM_REG_S $sp, $a0,80 ++ GRUB_ASM_REG_S $ra, $a0,88 ++ move $v0, $zero ++ move $v1, $zero ++ jr $ra ++ nop ++/* ++ * int grub_longjmp (grub_jmp_buf env, int val) ++ */ ++FUNCTION(grub_longjmp) ++ GRUB_ASM_REG_L $s0, $a0,0 ++ GRUB_ASM_REG_L $s1, $a0,8 ++ GRUB_ASM_REG_L $s2, $a0,16 ++ GRUB_ASM_REG_L $s3, $a0,24 ++ GRUB_ASM_REG_L $s4, $a0,32 ++ GRUB_ASM_REG_L $s5, $a0,40 ++ GRUB_ASM_REG_L $s6, $a0,48 ++ GRUB_ASM_REG_L $s7, $a0,56 ++ GRUB_ASM_REG_L $s8, $a0,64 ++ GRUB_ASM_REG_L $fp, $a0,72 ++ GRUB_ASM_REG_L $sp, $a0,80 ++ GRUB_ASM_REG_L $ra, $a0,88 ++ addi.w $v0, $zero, 1 ++ /* ++ * replace: movn $v0, $a1, $a1 ++ */ ++ bnez $a1, .ZW0 ++ addi.d $v0, $a1, 0 ++.ZW0: ++ addi.d $v1,$zero,0 ++ jr $ra ++ nop +diff --git a/grub-core/lib/loongson/reboot.c b/grub-core/lib/loongson/reboot.c +new file mode 100644 +index 0000000..107787a +--- /dev/null ++++ b/grub-core/lib/loongson/reboot.c +@@ -0,0 +1,33 @@ ++/* ++ * GRUB -- GRand Unified Bootloader ++ * Copyright (C) 2011 Free Software Foundation, Inc. ++ * ++ * GRUB is free software: you can redistribute it and/or modify ++ * it under the terms of the GNU General Public License as published by ++ * the Free Software Foundation, either version 3 of the License, or ++ * (at your option) any later version. ++ * ++ * GRUB is distributed in the hope that it will be useful, ++ * but WITHOUT ANY WARRANTY; without even the implied warranty of ++ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ++ * GNU General Public License for more details. ++ * ++ * You should have received a copy of the GNU General Public License ++ * along with GRUB. If not, see . ++ */ ++ ++#include ++#include ++#include ++#include ++#include ++#include ++ ++void ++grub_la_reboot (void) ++{ ++ grub_machine_fini (GRUB_LOADER_FLAG_NORETURN); ++ efi_call_4 (grub_efi_system_table->runtime_services->reset_system, ++ GRUB_EFI_RESET_COLD, GRUB_EFI_SUCCESS, 0, NULL); ++ for (;;) ; ++} +diff --git a/grub-core/lib/setjmp.S b/grub-core/lib/setjmp.S +index f6e4905..023dc90 100644 +--- a/grub-core/lib/setjmp.S ++++ b/grub-core/lib/setjmp.S +@@ -15,6 +15,8 @@ + #include "./arm/setjmp.S" + #elif defined(__aarch64__) + #include "./arm64/setjmp.S" ++#elif defined(__loongarch64) ++#include "./loongarch64/setjmp.S" + #else + #error "Unknown target cpu type" + #endif +diff --git a/grub-core/loader/efi/chainloader.c b/grub-core/loader/efi/chainloader.c +index 29663f7..c0ca17d 100644 +--- a/grub-core/loader/efi/chainloader.c ++++ b/grub-core/loader/efi/chainloader.c +@@ -332,6 +332,8 @@ static const grub_uint16_t machine_type __attribute__((__unused__)) = + GRUB_PE32_MACHINE_I386; + #elif defined(__ia64__) + GRUB_PE32_MACHINE_IA64; ++#elif defined(__loongarch64) ++ GRUB_PE32_MACHINE_LOONGARCH64; + #else + #error this architecture is not supported by grub2 + #endif +diff --git a/grub-core/loader/loongarch64/linux.c b/grub-core/loader/loongarch64/linux.c +new file mode 100644 +index 0000000..c769bb2 +--- /dev/null ++++ b/grub-core/loader/loongarch64/linux.c +@@ -0,0 +1,580 @@ ++/* linux.c - boot Linux */ ++/* ++ * GRUB -- GRand Unified Bootloader ++ * Copyright (C) 2003,2004,2005,2007,2009,2010,2017 Free Software Foundation, Inc. ++ * ++ * GRUB is free software: you can redistribute it and/or modify ++ * it under the terms of the GNU General Public License as published by ++ * the Free Software Foundation, either version 3 of the License, or ++ * (at your option) any later version. ++ * ++ * GRUB is distributed in the hope that it will be useful, ++ * but WITHOUT ANY WARRANTY; without even the implied warranty of ++ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ++ * GNU General Public License for more details. ++ * ++ * You should have received a copy of the GNU General Public License ++ * along with GRUB. If not, see . ++ */ ++ ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++ ++ ++GRUB_MOD_LICENSE ("GPLv3+"); ++ ++#pragma GCC diagnostic ignored "-Wcast-align" ++ ++typedef unsigned long size_t; ++ ++static grub_dl_t my_mod; ++ ++static int loaded; ++ ++static grub_uint32_t tmp_index = 0; ++static grub_size_t linux_size; ++ ++static struct grub_relocator *relocator; ++static grub_addr_t target_addr, entry_addr; ++static int linux_argc; ++static grub_uint8_t *linux_args_addr; ++static grub_off_t rd_addr_arg_off, rd_size_arg_off; ++static grub_off_t initrd_addr_arg_off; ++static int initrd_loaded = 0; ++ ++ ++static grub_uint32_t j = 0; ++static grub_uint32_t t = 0; ++grub_uint64_t tempMemsize = 0; ++grub_uint32_t free_index = 0; ++grub_uint32_t reserve_index = 0; ++grub_uint32_t acpi_table_index = 0; ++grub_uint32_t acpi_nvs_index = 0; ++ ++static inline grub_size_t ++page_align (grub_size_t size) ++{ ++ return (size + (1 << 12) - 1) & (~((1 << 12) - 1)); ++} ++ ++/* Find the optimal number of pages for the memory map. Is it better to ++ move this code to efi/mm.c? */ ++static grub_efi_uintn_t ++find_mmap_size (void) ++{ ++ static grub_efi_uintn_t mmap_size = 0; ++ ++ if (mmap_size != 0) ++ return mmap_size; ++ ++ mmap_size = (1 << 12); ++ while (1) ++ { ++ int ret; ++ grub_efi_memory_descriptor_t *mmap; ++ grub_efi_uintn_t desc_size; ++ ++ mmap = grub_malloc (mmap_size); ++ if (! mmap) ++ return 0; ++ ++ ret = grub_efi_get_memory_map (&mmap_size, mmap, 0, &desc_size, 0); ++ grub_free (mmap); ++ ++ if (ret < 0) ++ { ++ grub_error (GRUB_ERR_IO, "cannot get memory map"); ++ return 0; ++ } ++ else if (ret > 0) ++ break; ++ ++ mmap_size += (1 << 12); ++ } ++ ++ ++ /* Increase the size a bit for safety, because GRUB allocates more on ++ later, and EFI itself may allocate more. */ ++ mmap_size += (1 << 12); ++ ++ return page_align (mmap_size); ++} ++ ++static grub_err_t ++grub_linux_boot (void) ++{ ++ struct grub_relocator64_state state; ++ grub_int8_t checksum = 0; ++ grub_efi_memory_descriptor_t * lsdesc = NULL; ++ ++ grub_memset (&state, 0, sizeof (state)); ++ ++ /* Boot the kernel. */ ++ state.gpr[1] = entry_addr; ++ grub_dprintf("loongson", "entry_addr is %p\n", state.gpr[1]); ++ state.gpr[4] = linux_argc; ++ grub_dprintf("loongson", "linux_argc is %d\n", state.gpr[4]); ++ state.gpr[5] = (grub_addr_t) linux_args_addr; ++ grub_dprintf("loongson", "args_addr is %p\n", state.gpr[5]); ++ ++ if(grub_efi_is_loongson ()) ++ { ++ grub_efi_uintn_t mmap_size; ++ grub_efi_uintn_t desc_size; ++ grub_efi_memory_descriptor_t *mmap_buf; ++ grub_err_t err; ++ struct bootparamsinterface * boot_params; ++ void * tmp_boot_params = NULL; ++ grub_efi_uint8_t new_interface_flag = 0; ++ mem_map * new_interface_mem = NULL; ++ char *p = NULL; ++ ++ struct memmap reserve_mem[GRUB_EFI_LOONGSON_MMAP_MAX]; ++ struct memmap free_mem[GRUB_EFI_LOONGSON_MMAP_MAX]; ++ struct memmap acpi_table_mem[GRUB_EFI_LOONGSON_MMAP_MAX]; ++ struct memmap acpi_nvs_mem[GRUB_EFI_LOONGSON_MMAP_MAX]; ++ ++ grub_memset(reserve_mem, 0, sizeof(struct memmap) * GRUB_EFI_LOONGSON_MMAP_MAX); ++ grub_memset(free_mem, 0, sizeof(struct memmap) * GRUB_EFI_LOONGSON_MMAP_MAX); ++ grub_memset(acpi_table_mem, 0, sizeof(struct memmap) * GRUB_EFI_LOONGSON_MMAP_MAX); ++ grub_memset(acpi_nvs_mem, 0, sizeof(struct memmap) * GRUB_EFI_LOONGSON_MMAP_MAX); ++ ++ tmp_boot_params = grub_efi_loongson_get_boot_params(); ++ if(tmp_boot_params == NULL) ++ { ++ grub_printf("not find param\n"); ++ return -1; ++ } ++ ++ boot_params = (struct bootparamsinterface *)tmp_boot_params; ++ p = (char *)&(boot_params->signature); ++ if(grub_strncmp(p, "BPI", 3) == 0) ++ { ++ /* Check extlist headers */ ++ ext_list * listpointer = NULL; ++ listpointer = boot_params->extlist; ++ for( ;listpointer != NULL; listpointer = listpointer->next) ++ { ++ char *pl= (char *)&(listpointer->signature); ++ if(grub_strncmp(pl, "MEM", 3) == 0) ++ { ++ new_interface_mem = (mem_map *)listpointer; ++ } ++ } ++ ++ new_interface_flag = 1; ++ grub_dprintf("loongson", "get new parameter interface\n"); ++ }else{ ++ new_interface_flag = 0; ++ grub_dprintf("loongson", "get old parameter interface\n"); ++ } ++ state.gpr[6] = (grub_uint64_t)tmp_boot_params; ++ grub_dprintf("loongson", "boot_params is %p\n", state.gpr[6]); ++ ++ mmap_size = find_mmap_size (); ++ if (! mmap_size) ++ return grub_errno; ++ mmap_buf = grub_efi_allocate_any_pages (page_align (mmap_size) >> 12); ++ if (! mmap_buf) ++ return grub_error (GRUB_ERR_IO, "cannot allocate memory map"); ++ err = grub_efi_finish_boot_services (&mmap_size, mmap_buf, NULL, ++ &desc_size, NULL); ++ if (err) ++ return err; ++ ++ if(new_interface_flag) ++ { ++ if (!mmap_buf || !mmap_size || !desc_size) ++ return -1; ++ tmp_index = new_interface_mem -> mapcount; ++ ++ /* ++ According to UEFI SPEC,mmap_buf is the accurate Memory Map array \ ++ now we can fill platform specific memory structure. ++ */ ++ for(lsdesc = mmap_buf; lsdesc < (grub_efi_memory_descriptor_t *)((char *)mmap_buf + mmap_size); ++ lsdesc = (grub_efi_memory_descriptor_t *)((char *)lsdesc + desc_size)) ++ { ++ /* Recovery */ ++ if((lsdesc->type != GRUB_EFI_ACPI_RECLAIM_MEMORY) && \ ++ (lsdesc->type != GRUB_EFI_ACPI_MEMORY_NVS) && \ ++ (lsdesc->type != GRUB_EFI_RUNTIME_SERVICES_DATA) && \ ++ (lsdesc->type != GRUB_EFI_RUNTIME_SERVICES_CODE) && \ ++ (lsdesc->type != GRUB_EFI_RESERVED_MEMORY_TYPE) && \ ++ (lsdesc->type != GRUB_EFI_PAL_CODE)) ++ { ++ free_mem[free_index].memtype = GRUB_EFI_LOONGSON_SYSTEM_RAM; ++ free_mem[free_index].memstart = (lsdesc->physical_start) & 0xffffffffffff; ++ free_mem[free_index].memsize = lsdesc->num_pages * 4096; ++ free_index++; ++ ++ /*ACPI*/ ++ }else if((lsdesc->type == GRUB_EFI_ACPI_RECLAIM_MEMORY)){ ++ acpi_table_mem[acpi_table_index].memtype = GRUB_EFI_LOONGSON_ACPI_TABLE; ++ acpi_table_mem[acpi_table_index].memstart = (lsdesc->physical_start) & 0xffffffffffff; ++ acpi_table_mem[acpi_table_index].memsize = lsdesc->num_pages * 4096; ++ acpi_table_index++; ++ }else if((lsdesc->type == GRUB_EFI_ACPI_MEMORY_NVS)){ ++ acpi_nvs_mem[acpi_nvs_index].memtype = GRUB_EFI_LOONGSON_ACPI_NVS; ++ acpi_nvs_mem[acpi_nvs_index].memstart = (lsdesc->physical_start) & 0xffffffffffff; ++ acpi_nvs_mem[acpi_nvs_index].memsize = lsdesc->num_pages * 4096; ++ acpi_nvs_index++; ++ ++ /* Reserve */ ++ }else{ ++ reserve_mem[reserve_index].memtype = GRUB_EFI_LOONGSON_MEMORY_RESERVED; ++ reserve_mem[reserve_index].memstart = (lsdesc->physical_start) & 0xffffffffffff; ++ reserve_mem[reserve_index].memsize = lsdesc->num_pages * 4096; ++ reserve_index++; ++ } ++ } ++ ++ /* Recovery sort */ ++ for(j = 0; j < free_index;) ++ { ++ tempMemsize = free_mem[j].memsize; ++ for(t = j + 1; t < free_index; t++) ++ { ++ if((free_mem[j].memstart + tempMemsize == free_mem[t].memstart) && (free_mem[j].memtype == free_mem[t].memtype)) ++ { ++ tempMemsize += free_mem[t].memsize; ++ }else{ ++ break; ++ } ++ } ++ ++ new_interface_mem->map[tmp_index].memtype = GRUB_EFI_LOONGSON_SYSTEM_RAM; ++ new_interface_mem->map[tmp_index].memstart = free_mem[j].memstart; ++ new_interface_mem->map[tmp_index].memsize = tempMemsize; ++ grub_dprintf("loongson", "map[%d]:type %x, start 0x%llx, end 0x%llx\n", ++ tmp_index, ++ new_interface_mem->map[tmp_index].memtype, ++ new_interface_mem->map[tmp_index].memstart, ++ new_interface_mem->map[tmp_index].memstart+ new_interface_mem->map[tmp_index].memsize ++ ); ++ j = t; ++ tmp_index++; ++ } ++ /*ACPI Sort*/ ++ tmp_index = grub_efi_loongson_memmap_sort(acpi_table_mem, acpi_table_index, new_interface_mem, tmp_index, GRUB_EFI_LOONGSON_ACPI_TABLE); ++ tmp_index = grub_efi_loongson_memmap_sort(acpi_nvs_mem, acpi_nvs_index, new_interface_mem, tmp_index, GRUB_EFI_LOONGSON_ACPI_NVS); ++ /*Reserve Sort*/ ++ grub_uint64_t loongarch_addr; ++ asm volatile ("csrrd %0, 0x181" : "=r" (loongarch_addr)); ++ if((loongarch_addr & 0xff00000000000000) == 0x9000000000000000){ ++ tmp_index = grub_efi_loongson_memmap_sort(reserve_mem, reserve_index, new_interface_mem, tmp_index, GRUB_EFI_LOONGSON_MEMORY_RESERVED); ++ }else{ ++ tmp_index = grub_efi_loongson_memmap_sort(reserve_mem, reserve_index, new_interface_mem, tmp_index, GRUB_EFI_LOONGSON_MEMORY_RESERVED + 1); ++ } ++ ++ new_interface_mem->mapcount = tmp_index; ++ new_interface_mem->header.checksum = 0; ++ ++ checksum = grub_efi_loongson_grub_calculatechecksum8(new_interface_mem, new_interface_mem->header.length); ++ new_interface_mem->header.checksum = checksum; ++ } ++ } ++ ++ state.jumpreg = 1; ++ grub_relocator64_boot (relocator, state); ++ ++ return GRUB_ERR_NONE; ++} ++ ++static grub_err_t ++grub_linux_unload (void) ++{ ++ grub_relocator_unload (relocator); ++ grub_dl_unref (my_mod); ++ ++ loaded = 0; ++ ++ return GRUB_ERR_NONE; ++} ++ ++static grub_err_t ++grub_linux_load32 (grub_elf_t elf, const char *filename) ++{ ++ Elf32_Addr base; ++ grub_err_t err; ++ grub_uint8_t *playground; ++ ++ /* Linux's entry point incorrectly contains a virtual address. */ ++ entry_addr = elf->ehdr.ehdr32.e_entry; ++ ++ linux_size = grub_elf32_size (elf, &base, 0); ++ if (linux_size == 0) ++ return grub_errno; ++ target_addr = base; ++ linux_size = ALIGN_UP (base + linux_size - base, 8); ++ ++ relocator = grub_relocator_new (); ++ if (!relocator) ++ return grub_errno; ++ ++ { ++ grub_relocator_chunk_t ch; ++ err = grub_relocator_alloc_chunk_addr (relocator, &ch, ++ grub_vtop ((void *) target_addr), ++ linux_size); ++ if (err) ++ return err; ++ playground = get_virtual_current_address (ch); ++ } ++ ++ /* Now load the segments into the area we claimed. */ ++ return grub_elf32_load (elf, filename, playground - base, GRUB_ELF_LOAD_FLAGS_NONE, 0, 0); ++} ++ ++static grub_err_t ++grub_linux_load64 (grub_elf_t elf, const char *filename) ++{ ++ Elf64_Addr base; ++ grub_err_t err; ++ grub_uint8_t *playground; ++ ++ /* Linux's entry point incorrectly contains a virtual address. */ ++ entry_addr = elf->ehdr.ehdr64.e_entry; ++ grub_dprintf("loongson", "entry address = %p\n", entry_addr); ++ ++ linux_size = grub_elf64_size (elf, &base, 0); ++ grub_dprintf("loongson", "base = %p\n", base); ++ ++ if (linux_size == 0) ++ return grub_errno; ++ target_addr = base; ++ linux_size = ALIGN_UP (base + linux_size - base, 8); ++ ++ relocator = grub_relocator_new (); ++ // linux_size=0x322fa80; ++ if (!relocator) ++ return grub_errno; ++ ++ { ++ grub_relocator_chunk_t ch; ++ err = grub_relocator_alloc_chunk_addr (relocator, &ch, ++ grub_vtop ((void *) target_addr), ++ linux_size); ++ if (err) ++ return err; ++ playground = get_virtual_current_address (ch); ++ } ++ ++ /* Now load the segments into the area we claimed. */ ++ return grub_elf64_load (elf, filename, playground - base, GRUB_ELF_LOAD_FLAGS_NONE, 0, 0); ++} ++ ++static grub_err_t ++grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)), ++ int argc, char *argv[]) ++{ ++ grub_elf_t elf = 0; ++ int size; ++ int i; ++ grub_uint64_t *linux_argv; ++ char *linux_args; ++ grub_err_t err; ++ ++ if (argc == 0) ++ return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected")); ++ ++ elf = grub_elf_open (argv[0],GRUB_FILE_TYPE_LINUX_KERNEL); ++ if (! elf) ++ return grub_errno; ++ ++ if (elf->ehdr.ehdr32.e_type != ET_EXEC) ++ { ++ grub_elf_close (elf); ++ return grub_error (GRUB_ERR_UNKNOWN_OS, ++ N_("this ELF file is not of the right type")); ++ } ++ ++ /* Release the previously used memory. */ ++ grub_loader_unset (); ++ loaded = 0; ++ ++ /* For arguments. */ ++ linux_argc = argc; ++ /* Main arguments. */ ++ size = (linux_argc) * sizeof (grub_uint64_t); ++ /* Initrd address and size. */ ++ size += 3 * sizeof (grub_uint64_t); ++ /* NULL terminator. */ ++ size += sizeof (grub_uint64_t); ++ /* First argument is always "a0". */ ++ size += ALIGN_UP (sizeof ("a0"), 4); ++ /* Normal arguments. */ ++ for (i = 1; i < argc; i++) ++ size += ALIGN_UP (grub_strlen (argv[i]) + 1, 4); ++ ++ /* rd arguments. */ ++ size += ALIGN_UP (sizeof ("rd_start=0xXXXXXXXXXXXXXXXX"), 4); ++ size += ALIGN_UP (sizeof ("rd_size=0xXXXXXXXXXXXXXXXX"), 4); ++ size += ALIGN_UP (sizeof ("initrd=0xXXXXXXXXXXXXXXXX,0xXXXXXXXXXXXXXXXX"), 4); ++ ++ size = ALIGN_UP (size, 8); ++ ++ if (grub_elf_is_elf32 (elf)) ++ err = grub_linux_load32 (elf, argv[0]); ++ else ++ if (grub_elf_is_elf64 (elf)) ++ err = grub_linux_load64 (elf, argv[0]); ++ else ++ err = grub_error (GRUB_ERR_BAD_OS, N_("invalid arch-dependent ELF magic")); ++ ++ grub_elf_close (elf); ++ ++ if (err) ++ return err; ++ ++ { ++ grub_relocator_chunk_t ch; ++ err = grub_relocator_alloc_chunk_align (relocator, &ch, ++ 0, (0xffffffff - size) + 1, ++ size, 8, ++ GRUB_RELOCATOR_PREFERENCE_HIGH, 0); ++ if (err) ++ return err; ++ linux_args_addr = get_virtual_current_address (ch); ++ } ++ ++ linux_argv = (grub_uint64_t *) linux_args_addr; ++ linux_args = (char *) (linux_argv + (linux_argc + 1 + 3)); ++ ++ grub_memcpy (linux_args, "a0", sizeof ("a0")); ++ *linux_argv = (grub_uint64_t) (grub_addr_t) linux_args; ++ linux_argv++; ++ linux_args += ALIGN_UP (sizeof ("a0"), 4); ++ ++ for (i = 1; i < argc; i++) ++ { ++ grub_memcpy (linux_args, argv[i], grub_strlen (argv[i]) + 1); ++ *linux_argv = (grub_uint64_t) (grub_addr_t) linux_args; ++ linux_argv++; ++ linux_args += ALIGN_UP (grub_strlen (argv[i]) + 1, 4); ++ } ++ ++ /* Reserve space for rd arguments. */ ++ rd_addr_arg_off = (grub_uint8_t *) linux_args - linux_args_addr; ++ linux_args += ALIGN_UP (sizeof ("rd_start=0xXXXXXXXXXXXXXXXX"), 4); ++ *linux_argv = 0; ++ linux_argv++; ++ ++ rd_size_arg_off = (grub_uint8_t *) linux_args - linux_args_addr; ++ linux_args += ALIGN_UP (sizeof ("rd_size=0xXXXXXXXXXXXXXXXX"), 4); ++ *linux_argv = 0; ++ linux_argv++; ++ ++ /* Reserve space for initrd arguments. */ ++ initrd_addr_arg_off = (grub_uint8_t *) linux_args - linux_args_addr; ++ linux_args += ALIGN_UP (sizeof ("initrd=0xXXXXXXXXXXXXXXXX,0xXXXXXXXXXXXXXXXX"), 4); ++ *linux_argv = 0; ++ linux_argv++; ++ ++ *linux_argv = 0; ++ ++ grub_loader_set (grub_linux_boot, grub_linux_unload, 0); ++ initrd_loaded = 0; ++ loaded = 1; ++ grub_dl_ref (my_mod); ++ ++ return GRUB_ERR_NONE; ++} ++ ++static grub_err_t ++grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)), ++ int argc, char *argv[]) ++{ ++ grub_size_t size = 0; ++ void *initrd_dest; ++ grub_err_t err; ++ struct grub_linux_initrd_context initrd_ctx = { 0, 0, 0 }; ++ ++ if (argc == 0) ++ return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected")); ++ ++ if (!loaded) ++ return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("you need to load the kernel first")); ++ ++ if (initrd_loaded) ++ return grub_error (GRUB_ERR_BAD_ARGUMENT, "only one initrd command can be issued."); ++ ++ if (grub_initrd_init (argc, argv, &initrd_ctx)) ++ goto fail; ++ ++ size = grub_get_initrd_size (&initrd_ctx); ++ ++ { ++ grub_relocator_chunk_t ch; ++ err = grub_relocator_alloc_chunk_align (relocator, &ch, ++ 0, (0xffffffff - size) + 1, ++ size, 0x10000, ++ GRUB_RELOCATOR_PREFERENCE_HIGH, 0); ++ ++ if (err) ++ goto fail; ++ initrd_dest = get_virtual_current_address (ch); ++ } ++ ++ if (grub_initrd_load (&initrd_ctx, argv, initrd_dest)) ++ goto fail; ++ ++ grub_snprintf ((char *) linux_args_addr + rd_addr_arg_off, ++ sizeof ("rd_start=0xXXXXXXXXXXXXXXXX"), "rd_start=0x%lx", ++ (grub_uint64_t) initrd_dest); ++ ((grub_uint64_t *) linux_args_addr)[linux_argc] ++ = (grub_uint64_t) ((grub_addr_t) linux_args_addr + rd_addr_arg_off); ++ linux_argc++; ++ ++ grub_snprintf ((char *) linux_args_addr + rd_size_arg_off, ++ sizeof ("rd_size=0xXXXXXXXXXXXXXXXXX"), "rd_size=0x%lx", ++ (grub_uint64_t) size); ++ ((grub_uint64_t *) linux_args_addr)[linux_argc] ++ = (grub_uint64_t) ((grub_addr_t) linux_args_addr + rd_size_arg_off); ++ linux_argc++; ++ ++ ++ grub_snprintf ((char *) linux_args_addr + initrd_addr_arg_off, ++ sizeof ("initrd=0xXXXXXXXXXXXXXXXX,0xXXXXXXXXXXXXXXXX"), "initrd=0x%lx,0x%lx", ++ ((grub_uint64_t) initrd_dest & 0xffffffff), (grub_uint64_t) size); ++ ((grub_uint64_t *) linux_args_addr)[linux_argc] ++ = (grub_uint64_t) ((grub_addr_t) linux_args_addr + initrd_addr_arg_off); ++ linux_argc++; ++ ++ initrd_loaded = 1; ++ ++ fail: ++ grub_initrd_close (&initrd_ctx); ++ ++ return grub_errno; ++} ++ ++static grub_command_t cmd_linux, cmd_initrd; ++ ++GRUB_MOD_INIT(linux) ++{ ++ cmd_linux = grub_register_command ("linux", grub_cmd_linux, ++ 0, N_("Load Linux.")); ++ cmd_initrd = grub_register_command ("initrd", grub_cmd_initrd, ++ 0, N_("Load initrd.")); ++ my_mod = mod; ++} ++ ++GRUB_MOD_FINI(linux) ++{ ++ grub_unregister_command (cmd_linux); ++ grub_unregister_command (cmd_initrd); ++} +diff --git a/include/grub/dl.h b/include/grub/dl.h +index 6a3e251..c1b6dd9 100644 +--- a/include/grub/dl.h ++++ b/include/grub/dl.h +@@ -314,7 +314,7 @@ grub_arch_dl_get_tramp_got_size (const void *ehdr, grub_size_t *tramp, + #define GRUB_ARCH_DL_GOT_ALIGN 4 + #endif + +-#if defined (__aarch64__) || defined (__sparc__) ++#if defined (__aarch64__) || defined (__sparc__) || defined (__loongarch64) + #define GRUB_ARCH_DL_TRAMP_ALIGN 8 + #define GRUB_ARCH_DL_GOT_ALIGN 8 + #endif +diff --git a/include/grub/efi/api.h b/include/grub/efi/api.h +index 37e7b16..049326c 100644 +--- a/include/grub/efi/api.h ++++ b/include/grub/efi/api.h +@@ -2087,7 +2087,8 @@ struct grub_efi_rng_protocol + typedef struct grub_efi_rng_protocol grub_efi_rng_protocol_t; + + #if (GRUB_TARGET_SIZEOF_VOID_P == 4) || defined (__ia64__) \ +- || defined (__aarch64__) || defined (__MINGW64__) || defined (__CYGWIN__) ++ || defined (__aarch64__) || defined (__MINGW64__) || defined (__CYGWIN__) \ ++ || defined (__loongarch64) + + #define efi_call_0(func) func() + #define efi_call_1(func, a) func(a) +diff --git a/include/grub/efi/pe32.h b/include/grub/efi/pe32.h +index c03cc59..be88c54 100644 +--- a/include/grub/efi/pe32.h ++++ b/include/grub/efi/pe32.h +@@ -66,6 +66,7 @@ struct grub_pe32_coff_header + }; + + #define GRUB_PE32_MACHINE_I386 0x14c ++#define GRUB_PE32_MACHINE_LOONGARCH64 0x6264 + #define GRUB_PE32_MACHINE_IA64 0x200 + #define GRUB_PE32_MACHINE_X86_64 0x8664 + #define GRUB_PE32_MACHINE_ARMTHUMB_MIXED 0x01c2 +@@ -329,6 +330,7 @@ struct grub_pe32_fixup_block + #define GRUB_PE32_REL_BASED_IA64_IMM64 9 + #define GRUB_PE32_REL_BASED_DIR64 10 + #define GRUB_PE32_REL_BASED_HIGH3ADJ 11 ++#define GRUB_PE32_REL_BASED_LOONGARCH64 8 + + struct grub_pe32_symbol + { +diff --git a/include/grub/elf.h b/include/grub/elf.h +index c8492f9..2c0b163 100644 +--- a/include/grub/elf.h ++++ b/include/grub/elf.h +@@ -247,6 +247,7 @@ typedef struct + #define EM_XTENSA 94 /* Tensilica Xtensa Architecture */ + #define EM_NUM 95 + #define EM_AARCH64 183 /* ARM 64-bit architecture */ ++#define EM_LOONGARCH64 258 /* LoongArch64 architecture */ + + /* If it is necessary to assign new unofficial EM_* values, please + pick large random numbers (0x8523, 0xa7f2, etc.) to minimize the +@@ -1448,6 +1449,60 @@ typedef struct + #define OHWA0_R4KEOP_CHECKED 0x00000001 + #define OHWA1_R4KEOP_CLEAN 0x00000002 + ++/* LOONGARCH64 relocs. */ ++#define R_LARCH_NONE 0 ++#define R_LARCH_32 1 ++#define R_LARCH_64 2 ++#define R_LARCH_RELATIVE 3 ++#define R_LARCH_COPY 4 ++#define R_LARCH_JUMP_SLOT 5 ++#define R_LARCH_TLS_DTPMOD32 6 ++#define R_LARCH_TLS_DTPMOD64 7 ++#define R_LARCH_TLS_DTPREL32 8 ++#define R_LARCH_TLS_DTPREL64 9 ++#define R_LARCH_TLS_TPREL32 10 ++#define R_LARCH_TLS_TPREL64 11 ++#define R_LARCH_IRELATIVE 12 ++#define R_LARCH_MARK_LA 20 ++#define R_LARCH_MARK_PCREL 21 ++#define R_LARCH_SOP_PUSH_PCREL 22 ++#define R_LARCH_SOP_PUSH_ABSOLUTE 23 ++#define R_LARCH_SOP_PUSH_DUP 24 ++#define R_LARCH_SOP_PUSH_GPREL 25 ++#define R_LARCH_SOP_PUSH_TLS_TPREL 26 ++#define R_LARCH_SOP_PUSH_TLS_GOT 27 ++#define R_LARCH_SOP_PUSH_TLS_GD 28 ++#define R_LARCH_SOP_PUSH_PLT_PCREL 29 ++#define R_LARCH_SOP_ASSERT 30 ++#define R_LARCH_SOP_NOT 31 ++#define R_LARCH_SOP_SUB 32 ++#define R_LARCH_SOP_SL 33 ++#define R_LARCH_SOP_SR 34 ++#define R_LARCH_SOP_ADD 35 ++#define R_LARCH_SOP_AND 36 ++#define R_LARCH_SOP_IF_ELSE 37 ++#define R_LARCH_SOP_POP_32_S_10_5 38 ++#define R_LARCH_SOP_POP_32_U_10_12 39 ++#define R_LARCH_SOP_POP_32_S_10_12 40 ++#define R_LARCH_SOP_POP_32_S_10_16 41 ++#define R_LARCH_SOP_POP_32_S_10_16_S2 42 ++#define R_LARCH_SOP_POP_32_S_5_20 43 ++#define R_LARCH_SOP_POP_32_S_0_5_10_16_S2 44 ++#define R_LARCH_SOP_POP_32_S_0_10_10_16_S2 45 ++#define R_LARCH_SOP_POP_32_U 46 ++#define R_LARCH_ADD8 47 ++#define R_LARCH_ADD16 48 ++#define R_LARCH_ADD24 49 ++#define R_LARCH_ADD32 50 ++#define R_LARCH_ADD64 51 ++#define R_LARCH_SUB8 52 ++#define R_LARCH_SUB16 53 ++#define R_LARCH_SUB24 54 ++#define R_LARCH_SUB32 55 ++#define R_LARCH_SUB64 56 ++#define R_LARCH_GNU_VTINHERIT 57 ++#define R_LARCH_GNU_VTENTRY 58 ++ + /* MIPS relocs. */ + + #define R_MIPS_NONE 0 /* No reloc */ +diff --git a/include/grub/loongarch64/asm.h b/include/grub/loongarch64/asm.h +new file mode 100644 +index 0000000..c3e77e9 +--- /dev/null ++++ b/include/grub/loongarch64/asm.h +@@ -0,0 +1,10 @@ ++#ifndef GRUB_LOONGARCH64_ASM_HEADER ++#define GRUB_LOONGARCH64_ASM_HEADER 1 ++ ++#define GRUB_ASM_T4 $a4 ++#define GRUB_ASM_T5 $a5 ++#define GRUB_ASM_SZREG 8 ++#define GRUB_ASM_REG_S st.d ++#define GRUB_ASM_REG_L ld.d ++ ++#endif +diff --git a/include/grub/loongarch64/efi/boot.h b/include/grub/loongarch64/efi/boot.h +new file mode 100644 +index 0000000..e69de29 +diff --git a/include/grub/loongarch64/efi/loader.h b/include/grub/loongarch64/efi/loader.h +new file mode 100644 +index 0000000..71a0159 +--- /dev/null ++++ b/include/grub/loongarch64/efi/loader.h +@@ -0,0 +1,25 @@ ++/* ++ * GRUB -- GRand Unified Bootloader ++ * Copyright (C) 2002,2003,2004,2006,2007,2017 Free Software Foundation, Inc. ++ * ++ * GRUB is free software: you can redistribute it and/or modify ++ * it under the terms of the GNU General Public License as published by ++ * the Free Software Foundation, either version 3 of the License, or ++ * (at your option) any later version. ++ * ++ * GRUB is distributed in the hope that it will be useful, ++ * but WITHOUT ANY WARRANTY; without even the implied warranty of ++ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ++ * GNU General Public License for more details. ++ * ++ * You should have received a copy of the GNU General Public License ++ * along with GRUB. If not, see . ++ */ ++ ++#ifndef GRUB_LOADER_MACHINE_HEADER ++#define GRUB_LOADER_MACHINE_HEADER 1 ++ ++#include ++#include ++ ++#endif /* ! GRUB_LOADER_MACHINE_HEADER */ +diff --git a/include/grub/loongarch64/efi/loongson.h b/include/grub/loongarch64/efi/loongson.h +new file mode 100644 +index 0000000..fa32ef5 +--- /dev/null ++++ b/include/grub/loongarch64/efi/loongson.h +@@ -0,0 +1,290 @@ ++/* ++ * GRUB -- GRand Unified Bootloader ++ * Copyright (C) 2017 Free Software Foundation, Inc. ++ * ++ * GRUB is free software: you can redistribute it and/or modify ++ * it under the terms of the GNU General Public License as published by ++ * the Free Software Foundation, either version 3 of the License, or ++ * (at your option) any later version. ++ * ++ * GRUB is distributed in the hope that it will be useful, ++ * but WITHOUT ANY WARRANTY; without even the implied warranty of ++ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ++ * GNU General Public License for more details. ++ * ++ * You should have received a copy of the GNU General Public License ++ * along with GRUB. If not, see . ++ */ ++ ++#ifndef GRUB_EFI_LOONGSON_HEADER ++#define GRUB_EFI_LOONGSON_HEADER 1 ++ ++#include ++ ++#include ++ ++#define GRUB_EFI_LOONGSON_SMBIOS_TABLE_GUID \ ++ { 0x4660f721, 0x2ec5, 0x416a, \ ++ { 0x89, 0x9a, 0x43, 0x18, 0x02, 0x50, 0xa0, 0xc9 } \ ++ } ++ ++#define GRUB_EFI_LOONGSON_MMAP_MAX 128 ++typedef enum ++ { ++ GRUB_EFI_LOONGSON_SYSTEM_RAM = 1, ++ GRUB_EFI_LOONGSON_MEMORY_RESERVED, ++ GRUB_EFI_LOONGSON_ACPI_TABLE, ++ GRUB_EFI_LOONGSON_ACPI_NVS, ++ GRUB_EFI_LOONGSON_MAX_MEMORY_TYPE ++ } ++grub_efi_loongson_memory_type; ++ ++typedef struct ++{ ++ grub_uint16_t vers; /* version */ ++ grub_uint32_t nr_map; /* number of memory_maps */ ++ grub_uint32_t mem_freq; /* memory frequence */ ++ struct mem_map { ++ grub_uint32_t node_id; /* node_id which memory attached to */ ++ grub_uint32_t mem_type; /* system memory, pci memory, pci io, etc. */ ++ grub_uint64_t mem_start; /* memory map start address */ ++ grub_uint32_t mem_size; /* each memory_map size, not the total size */ ++ } map[GRUB_EFI_LOONGSON_MMAP_MAX]; ++} GRUB_PACKED ++grub_efi_loongson_memory_map; ++ ++/* ++ * Capability and feature descriptor structure for LOONGARCH CPU ++ */ ++typedef struct ++{ ++ grub_uint16_t vers; /* version */ ++ grub_uint32_t processor_id; /* PRID, e.g. 6305, 6306 */ ++ grub_uint32_t cputype; /* Loongson_3A/3B, etc. */ ++ grub_uint32_t total_node; /* num of total numa nodes */ ++ grub_uint16_t cpu_startup_core_id; /* Boot core id */ ++ grub_uint16_t reserved_cores_mask; ++ grub_uint32_t cpu_clock_freq; /* cpu_clock */ ++ grub_uint32_t nr_cpus; ++} GRUB_PACKED ++grub_efi_loongson_cpu_info; ++ ++#define GRUB_EFI_LOONGSON_MAX_UARTS 64 ++ ++typedef struct ++{ ++ grub_uint32_t iotype; /* see include/linux/serial_core.h */ ++ grub_uint32_t uartclk; ++ grub_uint32_t int_offset; ++ grub_uint64_t uart_base; ++} GRUB_PACKED ++grub_efi_loongson_uart_device; ++ ++#define GRUB_EFI_LOONGSON_MAX_SENSORS 64 ++ ++typedef struct ++{ ++ char name[32]; /* a formal name */ ++ char label[64]; /* a flexible description */ ++ grub_uint32_t type; /* SENSOR_* */ ++ grub_uint32_t id; /* instance id of a sensor-class */ ++ grub_uint32_t fan_policy; ++ grub_uint32_t fan_percent; /* only for constant speed policy */ ++ grub_uint64_t base_addr; /* base address of device registers */ ++} GRUB_PACKED ++grub_efi_loongson_sensor_device; ++ ++typedef struct ++{ ++ grub_uint16_t vers; /* version */ ++ grub_uint32_t ccnuma_smp; /* 0: no numa; 1: has numa */ ++ grub_uint32_t sing_double_channel; /* 1:single; 2:double */ ++ grub_uint32_t nr_uarts; ++ grub_efi_loongson_uart_device uarts[GRUB_EFI_LOONGSON_MAX_UARTS]; ++ grub_uint32_t nr_sensors; ++ grub_efi_loongson_sensor_device sensors[GRUB_EFI_LOONGSON_MAX_SENSORS]; ++ char has_ec; ++ char ec_name[32]; ++ grub_uint64_t ec_base_addr; ++ char has_tcm; ++ char tcm_name[32]; ++ grub_uint64_t tcm_base_addr; ++ grub_uint64_t workarounds; /* see workarounds.h */ ++} GRUB_PACKED ++grub_efi_loongson_system_info; ++ ++typedef struct ++{ ++ grub_uint16_t vers; ++ grub_uint16_t size; ++ grub_uint16_t rtr_bus; ++ grub_uint16_t rtr_devfn; ++ grub_uint32_t vendor; ++ grub_uint32_t device; ++ grub_uint32_t PIC_type; /* conform use HT or PCI to route to CPU-PIC */ ++ grub_uint64_t ht_int_bit; /* 3A: 1<<24; 3B: 1<<16 */ ++ grub_uint64_t ht_enable; /* irqs used in this PIC */ ++ grub_uint32_t node_id; /* node id: 0x0-0; 0x1-1; 0x10-2; 0x11-3 */ ++ grub_uint64_t pci_mem_start_addr; ++ grub_uint64_t pci_mem_end_addr; ++ grub_uint64_t pci_io_start_addr; ++ grub_uint64_t pci_io_end_addr; ++ grub_uint64_t pci_config_addr; ++ grub_uint32_t dma_mask_bits; ++} GRUB_PACKED ++grub_efi_loongson_irq_src_routing_table; ++ ++typedef struct ++{ ++ grub_uint16_t vers; /* version */ ++ grub_uint16_t size; ++ grub_uint8_t flag; ++ char description[64]; ++} GRUB_PACKED ++grub_efi_loongson_interface_info; ++ ++#define GRUB_EFI_LOONGSON_MAX_RESOURCE_NUMBER 128 ++ ++typedef struct ++{ ++ grub_uint64_t start; /* resource start address */ ++ grub_uint64_t end; /* resource end address */ ++ char name[64]; ++ grub_uint32_t flags; ++} ++grub_efi_loongson_resource; ++ ++/* arch specific additions */ ++typedef struct ++{ ++} ++grub_efi_loongson_archdev_data; ++ ++typedef struct ++{ ++ char name[64]; /* hold the device name */ ++ grub_uint32_t num_resources; /* number of device_resource */ ++ /* for each device's resource */ ++ grub_efi_loongson_resource resource[GRUB_EFI_LOONGSON_MAX_RESOURCE_NUMBER]; ++ /* arch specific additions */ ++ grub_efi_loongson_archdev_data archdata; ++} ++grub_efi_loongson_board_devices; ++ ++typedef struct ++{ ++ grub_uint16_t vers; /* version */ ++ char special_name[64]; /* special_atribute_name */ ++ grub_uint32_t loongson_special_type; /* type of special device */ ++ /* for each device's resource */ ++ grub_efi_loongson_resource resource[GRUB_EFI_LOONGSON_MAX_RESOURCE_NUMBER]; ++} ++grub_efi_loongson_special_attribute; ++ ++typedef struct ++{ ++ grub_uint64_t memory_offset; /* efi_loongson_memory_map struct offset */ ++ grub_uint64_t cpu_offset; /* efi_loongson_cpuinfo struct offset */ ++ grub_uint64_t system_offset; /* efi_loongson_system_info struct offset */ ++ grub_uint64_t irq_offset; /* efi_loongson_irq_src_routing_table struct offset */ ++ grub_uint64_t interface_offset; /* interface_info struct offset */ ++ grub_uint64_t special_offset; /* efi_loongson_special_attribute struct offset */ ++ grub_uint64_t boarddev_table_offset; /* efi_loongson_board_devices offset */ ++} ++grub_efi_loongson_params; ++ ++typedef struct ++{ ++ grub_uint16_t vers; /* version */ ++ grub_uint64_t vga_bios; /* vga_bios address */ ++ grub_efi_loongson_params lp; ++} ++grub_efi_loongson_smbios_table; ++ ++typedef struct ++{ ++ grub_uint64_t reset_cold; ++ grub_uint64_t reset_warm; ++ grub_uint64_t reset_type; ++ grub_uint64_t shutdown; ++ grub_uint64_t do_suspend; /* NULL if not support */ ++} ++grub_efi_loongson_reset_system; ++ ++typedef struct ++{ ++ grub_uint64_t mps; /* MPS table */ ++ grub_uint64_t acpi; /* ACPI table (IA64 ext 0.71) */ ++ grub_uint64_t acpi20; /* ACPI table (ACPI 2.0) */ ++ grub_efi_loongson_smbios_table smbios; /* SM BIOS table */ ++ grub_uint64_t sal_systab; /* SAL system table */ ++ grub_uint64_t boot_info; /* boot info table */ ++} ++grub_efi_loongson; ++ ++typedef struct ++{ ++ grub_efi_loongson efi; ++ grub_efi_loongson_reset_system reset_system; ++} ++grub_efi_loongson_boot_params; ++ ++extern grub_uint64_t grub_efi_loongson_reset_system_addr; ++ ++extern void grub_efi_loongson_reset_cold (void); ++extern void grub_efi_loongson_reset_warm (void); ++extern void grub_efi_loongson_reset_shutdown (void); ++extern void grub_efi_loongson_reset_suspend (void); ++ ++void grub_efi_loongson_alloc_boot_params (void); ++void grub_efi_loongson_free_boot_params (void); ++void * grub_efi_loongson_get_smbios_table (void); ++ ++int EXPORT_FUNC(grub_efi_is_loongson) (void); ++ ++grub_uint8_t ++EXPORT_FUNC(grub_efi_loongson_calculatesum8) (const grub_uint8_t *Buffer, grub_efi_uintn_t Length); ++ ++grub_uint8_t ++EXPORT_FUNC(grub_efi_loongson_grub_calculatechecksum8) (const grub_uint8_t *Buffer, grub_efi_uintn_t Length); ++ ++ ++void * ++EXPORT_FUNC(grub_efi_loongson_get_boot_params) (void); ++ ++typedef struct _extention_list_hdr{ ++ grub_uint64_t signature; ++ grub_uint32_t length; ++ grub_uint8_t revision; ++ grub_uint8_t checksum; ++ struct _extention_list_hdr *next; ++}GRUB_PACKED ++ext_list; ++ ++typedef struct bootparamsinterface { ++ grub_uint64_t signature; //{'B', 'P', 'I', '_', '0', '_', '1'} ++ grub_efi_system_table_t *systemtable; ++ ext_list *extlist; ++}GRUB_PACKED ++bootparamsinterface; ++ ++typedef struct { ++ ext_list header; // {'M', 'E', 'M'} ++ grub_uint8_t mapcount; ++ struct GRUB_PACKED memmap { ++ grub_uint32_t memtype; ++ grub_uint64_t memstart; ++ grub_uint64_t memsize; ++ } map[GRUB_EFI_LOONGSON_MMAP_MAX]; ++}GRUB_PACKED ++mem_map; ++ ++typedef struct { ++ ext_list header; // {VBIOS} ++ grub_uint64_t vbiosaddr; ++}GRUB_PACKED ++vbios; ++ ++grub_uint32_t ++EXPORT_FUNC (grub_efi_loongson_memmap_sort) (struct memmap array[], grub_uint32_t length, mem_map * bpmem, grub_uint32_t index, grub_uint32_t memtype); ++#endif /* ! GRUB_EFI_LOONGSON_HEADER */ +diff --git a/include/grub/loongarch64/efi/memory.h b/include/grub/loongarch64/efi/memory.h +new file mode 100644 +index 0000000..b8556c7 +--- /dev/null ++++ b/include/grub/loongarch64/efi/memory.h +@@ -0,0 +1,14 @@ ++#ifndef GRUB_MEMORY_CPU_HEADER ++#include ++ ++#define GRUB_EFI_MAX_USABLE_ADDRESS 0x9800000fffffffffUL ++#define GRUB_EFI_MAX_ALLOCATION_ADDRESS GRUB_EFI_MAX_USABLE_ADDRESS ++ ++static inline grub_uint64_t grub_efi_max_usable_address(void) ++{ ++ grub_uint64_t addr; ++ asm volatile ("csrrd %0, 0x181" : "=r" (addr)); ++ return addr |= 0xffffffffffUL; ++} ++ ++#endif /* ! GRUB_MEMORY_CPU_HEADER */ +diff --git a/include/grub/loongarch64/efi/time.h b/include/grub/loongarch64/efi/time.h +new file mode 100644 +index 0000000..e69de29 +diff --git a/include/grub/loongarch64/io.h b/include/grub/loongarch64/io.h +new file mode 100644 +index 0000000..5f34103 +--- /dev/null ++++ b/include/grub/loongarch64/io.h +@@ -0,0 +1,62 @@ ++/* ++ * GRUB -- GRand Unified Bootloader ++ * Copyright (C) 2009,2017 Free Software Foundation, Inc. ++ * ++ * GRUB is free software: you can redistribute it and/or modify ++ * it under the terms of the GNU General Public License as published by ++ * the Free Software Foundation, either version 3 of the License, or ++ * (at your option) any later version. ++ * ++ * GRUB is distributed in the hope that it will be useful, ++ * but WITHOUT ANY WARRANTY; without even the implied warranty of ++ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ++ * GNU General Public License for more details. ++ * ++ * You should have received a copy of the GNU General Public License ++ * along with GRUB. If not, see . ++ */ ++ ++#ifndef GRUB_IO_H ++#define GRUB_IO_H 1 ++ ++#include ++ ++typedef grub_addr_t grub_port_t; ++ ++static __inline unsigned char ++grub_inb (grub_port_t port) ++{ ++ return *(volatile grub_uint8_t *) port; ++} ++ ++static __inline unsigned short int ++grub_inw (grub_port_t port) ++{ ++ return *(volatile grub_uint16_t *) port; ++} ++ ++static __inline unsigned int ++grub_inl (grub_port_t port) ++{ ++ return *(volatile grub_uint32_t *) port; ++} ++ ++static __inline void ++grub_outb (unsigned char value, grub_port_t port) ++{ ++ *(volatile grub_uint8_t *) port = value; ++} ++ ++static __inline void ++grub_outw (unsigned short int value, grub_port_t port) ++{ ++ *(volatile grub_uint16_t *) port = value; ++} ++ ++static __inline void ++grub_outl (unsigned int value, grub_port_t port) ++{ ++ *(volatile grub_uint32_t *) port = value; ++} ++ ++#endif /* _SYS_IO_H */ +diff --git a/include/grub/loongarch64/kernel.h b/include/grub/loongarch64/kernel.h +new file mode 100644 +index 0000000..909d539 +--- /dev/null ++++ b/include/grub/loongarch64/kernel.h +@@ -0,0 +1,24 @@ ++/* ++ * GRUB -- GRand Unified Bootloader ++ * Copyright (C) 2005,2006,2007,2008,2009,2017 Free Software Foundation, Inc. ++ * ++ * GRUB is free software: you can redistribute it and/or modify ++ * it under the terms of the GNU General Public License as published by ++ * the Free Software Foundation, either version 3 of the License, or ++ * (at your option) any later version. ++ * ++ * GRUB is distributed in the hope that it will be useful, ++ * but WITHOUT ANY WARRANTY; without even the implied warranty of ++ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ++ * GNU General Public License for more details. ++ * ++ * You should have received a copy of the GNU General Public License ++ * along with GRUB. If not, see . ++ */ ++ ++#ifndef GRUB_KERNEL_CPU_HEADER ++#define GRUB_KERNEL_CPU_HEADER 1 ++ ++#include ++ ++#endif /* ! GRUB_KERNEL_MACHINE_HEADER */ +diff --git a/include/grub/loongarch64/linux.h b/include/grub/loongarch64/linux.h +new file mode 100644 +index 0000000..cbf8775 +--- /dev/null ++++ b/include/grub/loongarch64/linux.h +@@ -0,0 +1,22 @@ ++/* ++ * GRUB -- GRand Unified Bootloader ++ * Copyright (C) 2013 Free Software Foundation, Inc. ++ * ++ * GRUB is free software: you can redistribute it and/or modify ++ * it under the terms of the GNU General Public License as published by ++ * the Free Software Foundation, either version 3 of the License, or ++ * (at your option) any later version. ++ * ++ * GRUB is distributed in the hope that it will be useful, ++ * but WITHOUT ANY WARRANTY; without even the implied warranty of ++ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ++ * GNU General Public License for more details. ++ * ++ * You should have received a copy of the GNU General Public License ++ * along with GRUB. If not, see . ++ */ ++ ++#ifndef GRUB_LOONGARCH64_LINUX_HEADER ++#define GRUB_LOONGARCH64_LINUX_HEADER 1 ++ ++#endif /* ! GRUB_LOONGARCH64_LINUX_HEADER */ +diff --git a/include/grub/loongarch64/loongarch64.h b/include/grub/loongarch64/loongarch64.h +new file mode 100644 +index 0000000..ea3be3d +--- /dev/null ++++ b/include/grub/loongarch64/loongarch64.h +@@ -0,0 +1,30 @@ ++/* ++ * GRUB -- GRand Unified Bootloader ++ * Copyright (C) 2010,2017 Free Software Foundation, Inc. ++ * ++ * GRUB is free software: you can redistribute it and/or modify ++ * it under the terms of the GNU General Public License as published by ++ * the Free Software Foundation, either version 3 of the License, or ++ * (at your option) any later version. ++ * ++ * GRUB is distributed in the hope that it will be useful, ++ * but WITHOUT ANY WARRANTY; without even the implied warranty of ++ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ++ * GNU General Public License for more details. ++ * ++ * You should have received a copy of the GNU General Public License ++ * along with GRUB. If not, see . ++ */ ++ ++#ifndef GRUB_REGISTERS_CPU_HEADER ++#define GRUB_REGISTERS_CPU_HEADER 1 ++ ++#ifdef ASM_FILE ++#define GRUB_CPU_REGISTER_WRAP(x) x ++#else ++#define GRUB_CPU_REGISTER_WRAP(x) #x ++#endif ++ ++#define GRUB_CPU_LOONGARCH_COP0_TIMER_COUNT GRUB_CPU_REGISTER_WRAP(9) ++ ++#endif +diff --git a/include/grub/loongarch64/memory.h b/include/grub/loongarch64/memory.h +new file mode 100644 +index 0000000..03398b3 +--- /dev/null ++++ b/include/grub/loongarch64/memory.h +@@ -0,0 +1,57 @@ ++/* ++ * GRUB -- GRand Unified Bootloader ++ * Copyright (C) 2017 Free Software Foundation, Inc. ++ * ++ * GRUB is free software: you can redistribute it and/or modify ++ * it under the terms of the GNU General Public License as published by ++ * the Free Software Foundation, either version 3 of the License, or ++ * (at your option) any later version. ++ * ++ * GRUB is distributed in the hope that it will be useful, ++ * but WITHOUT ANY WARRANTY; without even the implied warranty of ++ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ++ * GNU General Public License for more details. ++ * ++ * You should have received a copy of the GNU General Public License ++ * along with GRUB. If not, see . ++ */ ++ ++#ifndef GRUB_MEMORY_CPU_HEADER ++#define GRUB_MEMORY_CPU_HEADER 1 ++ ++#ifndef ASM_FILE ++#include ++#include ++#include ++#endif ++ ++#ifndef ASM_FILE ++ ++typedef grub_addr_t grub_phys_addr_t; ++ ++static inline grub_phys_addr_t ++grub_vtop (void *a) ++{ ++ if (-1 == ((grub_int64_t) a >> 32)) ++ return ((grub_phys_addr_t) a) & 0x1fffffffUL; ++ return ((grub_phys_addr_t) a) & 0xffffffffffffUL; ++} ++ ++static inline void * ++grub_map_memory (grub_phys_addr_t a, grub_size_t size) ++{ ++ grub_uint64_t addr; ++ ++ asm volatile ("csrrd %0, 0x181" : "=r" (addr)); ++ return (void *) (a | (addr & 0xffffffffffffff00UL)); ++} ++ ++static inline void ++grub_unmap_memory (void *a __attribute__ ((unused)), ++ grub_size_t size __attribute__ ((unused))) ++{ ++} ++ ++#endif ++ ++#endif +diff --git a/include/grub/loongarch64/relocator.h b/include/grub/loongarch64/relocator.h +new file mode 100644 +index 0000000..8815314 +--- /dev/null ++++ b/include/grub/loongarch64/relocator.h +@@ -0,0 +1,38 @@ ++/* ++ * GRUB -- GRand Unified Bootloader ++ * Copyright (C) 2017 Free Software Foundation, Inc. ++ * ++ * GRUB is free software: you can redistribute it and/or modify ++ * it under the terms of the GNU General Public License as published by ++ * the Free Software Foundation, either version 3 of the License, or ++ * (at your option) any later version. ++ * ++ * GRUB is distributed in the hope that it will be useful, ++ * but WITHOUT ANY WARRANTY; without even the implied warranty of ++ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ++ * GNU General Public License for more details. ++ * ++ * You should have received a copy of the GNU General Public License ++ * along with GRUB. If not, see . ++ */ ++ ++#ifndef GRUB_RELOCATOR_CPU_HEADER ++#define GRUB_RELOCATOR_CPU_HEADER 1 ++ ++#include ++#include ++#include ++ ++struct grub_relocator64_state ++{ ++ /* gpr[0] is ignored since it's hardwired to 0. */ ++ grub_uint64_t gpr[32]; ++ /* Register holding target $pc. */ ++ int jumpreg; ++}; ++ ++grub_err_t ++grub_relocator64_boot (struct grub_relocator *rel, ++ struct grub_relocator64_state state); ++ ++#endif /* ! GRUB_RELOCATOR_CPU_HEADER */ +diff --git a/include/grub/loongarch64/setjmp.h b/include/grub/loongarch64/setjmp.h +new file mode 100644 +index 0000000..d9a0776 +--- /dev/null ++++ b/include/grub/loongarch64/setjmp.h +@@ -0,0 +1,27 @@ ++/* ++ * GRUB -- GRand Unified Bootloader ++ * Copyright (C) 2002,2004,2006,2007,2009,2017 Free Software Foundation, Inc. ++ * ++ * GRUB is free software: you can redistribute it and/or modify ++ * it under the terms of the GNU General Public License as published by ++ * the Free Software Foundation, either version 3 of the License, or ++ * (at your option) any later version. ++ * ++ * GRUB is distributed in the hope that it will be useful, ++ * but WITHOUT ANY WARRANTY; without even the implied warranty of ++ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ++ * GNU General Public License for more details. ++ * ++ * You should have received a copy of the GNU General Public License ++ * along with GRUB. If not, see . ++ */ ++ ++#ifndef GRUB_SETJMP_CPU_HEADER ++#define GRUB_SETJMP_CPU_HEADER 1 ++ ++typedef grub_uint64_t grub_jmp_buf[12]; ++ ++int grub_setjmp (grub_jmp_buf env) RETURNS_TWICE; ++void grub_longjmp (grub_jmp_buf env, int val) __attribute__ ((noreturn)); ++ ++#endif /* ! GRUB_SETJMP_CPU_HEADER */ +diff --git a/include/grub/loongarch64/time.h b/include/grub/loongarch64/time.h +new file mode 100644 +index 0000000..c9a7334 +--- /dev/null ++++ b/include/grub/loongarch64/time.h +@@ -0,0 +1,39 @@ ++/* ++ * GRUB -- GRand Unified Bootloader ++ * Copyright (C) 2003,2004,2005,2007,2017 Free Software Foundation, Inc. ++ * ++ * GRUB is free software: you can redistribute it and/or modify ++ * it under the terms of the GNU General Public License as published by ++ * the Free Software Foundation, either version 3 of the License, or ++ * (at your option) any later version. ++ * ++ * GRUB is distributed in the hope that it will be useful, ++ * but WITHOUT ANY WARRANTY; without even the implied warranty of ++ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ++ * GNU General Public License for more details. ++ * ++ * You should have received a copy of the GNU General Public License ++ * along with GRUB. If not, see . ++ */ ++ ++#ifndef KERNEL_CPU_TIME_HEADER ++#define KERNEL_CPU_TIME_HEADER 1 ++ ++#ifndef GRUB_UTIL ++ ++#define GRUB_TICKS_PER_SECOND (grub_arch_cpuclock / 2) ++ ++void grub_timer_init (grub_uint32_t cpuclock); ++ ++/* Return the real time in ticks. */ ++grub_uint64_t grub_get_rtc (void); ++ ++extern grub_uint32_t grub_arch_cpuclock; ++#endif ++ ++static inline void ++grub_cpu_idle(void) ++{ ++} ++ ++#endif +diff --git a/include/grub/loongarch64/types.h b/include/grub/loongarch64/types.h +new file mode 100644 +index 0000000..5dc7f21 +--- /dev/null ++++ b/include/grub/loongarch64/types.h +@@ -0,0 +1,34 @@ ++/* ++ * GRUB -- GRand Unified Bootloader ++ * Copyright (C) 2002,2006,2007,2009,2017 Free Software Foundation, Inc. ++ * ++ * GRUB is free software: you can redistribute it and/or modify ++ * it under the terms of the GNU General Public License as published by ++ * the Free Software Foundation, either version 3 of the License, or ++ * (at your option) any later version. ++ * ++ * GRUB is distributed in the hope that it will be useful, ++ * but WITHOUT ANY WARRANTY; without even the implied warranty of ++ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ++ * GNU General Public License for more details. ++ * ++ * You should have received a copy of the GNU General Public License ++ * along with GRUB. If not, see . ++ */ ++ ++#ifndef GRUB_TYPES_CPU_HEADER ++#define GRUB_TYPES_CPU_HEADER 1 ++ ++/* The size of void *. */ ++#define GRUB_TARGET_SIZEOF_VOID_P 8 ++ ++/* The size of long. */ ++#define GRUB_TARGET_SIZEOF_LONG 8 ++ ++#ifdef GRUB_CPU_LOONGARCH ++/* loongarch is little-endian. */ ++#undef GRUB_TARGET_WORDS_BIGENDIAN ++ ++#endif /* ! GRUB_TYPES_CPU_HEADER */ ++ ++#endif +diff --git a/include/grub/util/install.h b/include/grub/util/install.h +index dad1756..33feae9 100644 +--- a/include/grub/util/install.h ++++ b/include/grub/util/install.h +@@ -107,6 +107,7 @@ enum grub_install_plat + GRUB_INSTALL_PLATFORM_X86_64_XEN, + GRUB_INSTALL_PLATFORM_ARM64_EFI, + GRUB_INSTALL_PLATFORM_ARM_COREBOOT, ++ GRUB_INSTALL_PLATFORM_LOONGARCH64_EFI, + GRUB_INSTALL_PLATFORM_MAX + }; + +diff --git a/util/grub-install-common.c b/util/grub-install-common.c +index fde4ca7..6504637 100644 +--- a/util/grub-install-common.c ++++ b/util/grub-install-common.c +@@ -738,27 +738,28 @@ static struct + const char *platform; + } platforms[GRUB_INSTALL_PLATFORM_MAX] = + { +- [GRUB_INSTALL_PLATFORM_I386_PC] = { "i386", "pc" }, +- [GRUB_INSTALL_PLATFORM_I386_EFI] = { "i386", "efi" }, +- [GRUB_INSTALL_PLATFORM_I386_QEMU] = { "i386", "qemu" }, +- [GRUB_INSTALL_PLATFORM_I386_COREBOOT] = { "i386", "coreboot" }, +- [GRUB_INSTALL_PLATFORM_I386_MULTIBOOT] = { "i386", "multiboot" }, +- [GRUB_INSTALL_PLATFORM_I386_IEEE1275] = { "i386", "ieee1275" }, +- [GRUB_INSTALL_PLATFORM_X86_64_EFI] = { "x86_64", "efi" }, +- [GRUB_INSTALL_PLATFORM_I386_XEN] = { "i386", "xen" }, +- [GRUB_INSTALL_PLATFORM_X86_64_XEN] = { "x86_64", "xen" }, +- [GRUB_INSTALL_PLATFORM_MIPSEL_LOONGSON] = { "mipsel", "loongson" }, +- [GRUB_INSTALL_PLATFORM_MIPSEL_QEMU_MIPS] = { "mipsel", "qemu_mips" }, +- [GRUB_INSTALL_PLATFORM_MIPS_QEMU_MIPS] = { "mips", "qemu_mips" }, +- [GRUB_INSTALL_PLATFORM_MIPSEL_ARC] = { "mipsel", "arc" }, +- [GRUB_INSTALL_PLATFORM_MIPS_ARC] = { "mips", "arc" }, +- [GRUB_INSTALL_PLATFORM_SPARC64_IEEE1275] = { "sparc64", "ieee1275" }, +- [GRUB_INSTALL_PLATFORM_POWERPC_IEEE1275] = { "powerpc", "ieee1275" }, +- [GRUB_INSTALL_PLATFORM_IA64_EFI] = { "ia64", "efi" }, +- [GRUB_INSTALL_PLATFORM_ARM_EFI] = { "arm", "efi" }, +- [GRUB_INSTALL_PLATFORM_ARM64_EFI] = { "arm64", "efi" }, +- [GRUB_INSTALL_PLATFORM_ARM_UBOOT] = { "arm", "uboot" }, +- [GRUB_INSTALL_PLATFORM_ARM_COREBOOT] = { "arm", "coreboot" }, ++ [GRUB_INSTALL_PLATFORM_I386_PC] = { "i386", "pc" }, ++ [GRUB_INSTALL_PLATFORM_I386_EFI] = { "i386", "efi" }, ++ [GRUB_INSTALL_PLATFORM_I386_QEMU] = { "i386", "qemu" }, ++ [GRUB_INSTALL_PLATFORM_I386_COREBOOT] = { "i386", "coreboot" }, ++ [GRUB_INSTALL_PLATFORM_I386_MULTIBOOT] = { "i386", "multiboot" }, ++ [GRUB_INSTALL_PLATFORM_I386_IEEE1275] = { "i386", "ieee1275" }, ++ [GRUB_INSTALL_PLATFORM_X86_64_EFI] = { "x86_64", "efi" }, ++ [GRUB_INSTALL_PLATFORM_I386_XEN] = { "i386", "xen" }, ++ [GRUB_INSTALL_PLATFORM_X86_64_XEN] = { "x86_64", "xen" }, ++ [GRUB_INSTALL_PLATFORM_MIPSEL_LOONGSON] = { "mipsel", "loongson" }, ++ [GRUB_INSTALL_PLATFORM_MIPSEL_QEMU_MIPS] = { "mipsel", "qemu_mips" }, ++ [GRUB_INSTALL_PLATFORM_MIPS_QEMU_MIPS] = { "mips", "qemu_mips" }, ++ [GRUB_INSTALL_PLATFORM_MIPSEL_ARC] = { "mipsel", "arc" }, ++ [GRUB_INSTALL_PLATFORM_MIPS_ARC] = { "mips", "arc" }, ++ [GRUB_INSTALL_PLATFORM_SPARC64_IEEE1275] = { "sparc64", "ieee1275" }, ++ [GRUB_INSTALL_PLATFORM_POWERPC_IEEE1275] = { "powerpc", "ieee1275" }, ++ [GRUB_INSTALL_PLATFORM_IA64_EFI] = { "ia64", "efi" }, ++ [GRUB_INSTALL_PLATFORM_ARM_EFI] = { "arm", "efi" }, ++ [GRUB_INSTALL_PLATFORM_ARM64_EFI] = { "arm64", "efi" }, ++ [GRUB_INSTALL_PLATFORM_ARM_UBOOT] = { "arm", "uboot" }, ++ [GRUB_INSTALL_PLATFORM_ARM_COREBOOT] = { "arm", "coreboot" }, ++ [GRUB_INSTALL_PLATFORM_LOONGARCH64_EFI] = { "loongarch64", "efi" }, + }; + + char * +diff --git a/util/grub-install.c b/util/grub-install.c +index 65bb2f9..28b5d74 100644 +--- a/util/grub-install.c ++++ b/util/grub-install.c +@@ -322,6 +322,8 @@ get_default_platform (void) + return "arm-uboot"; + #elif defined (__aarch64__) + return "arm64-efi"; ++#elif defined (__loongarch64) ++ return "loongarch64-efi"; + #elif defined (__amd64__) || defined (__x86_64__) || defined (__i386__) + return grub_install_get_default_x86_platform (); + #else +@@ -477,6 +479,7 @@ have_bootdev (enum grub_install_plat pl) + case GRUB_INSTALL_PLATFORM_IA64_EFI: + case GRUB_INSTALL_PLATFORM_ARM_EFI: + case GRUB_INSTALL_PLATFORM_ARM64_EFI: ++ case GRUB_INSTALL_PLATFORM_LOONGARCH64_EFI: + case GRUB_INSTALL_PLATFORM_I386_IEEE1275: + case GRUB_INSTALL_PLATFORM_SPARC64_IEEE1275: + case GRUB_INSTALL_PLATFORM_POWERPC_IEEE1275: +@@ -895,6 +898,7 @@ main (int argc, char *argv[]) + case GRUB_INSTALL_PLATFORM_I386_EFI: + case GRUB_INSTALL_PLATFORM_IA64_EFI: + case GRUB_INSTALL_PLATFORM_X86_64_EFI: ++ case GRUB_INSTALL_PLATFORM_LOONGARCH64_EFI: + is_efi = 1; + grub_util_error (_("this utility cannot be used for EFI platforms" + " because it does not support UEFI Secure Boot")); +@@ -921,6 +925,7 @@ main (int argc, char *argv[]) + case GRUB_INSTALL_PLATFORM_ARM_EFI: + case GRUB_INSTALL_PLATFORM_ARM64_EFI: + case GRUB_INSTALL_PLATFORM_IA64_EFI: ++ case GRUB_INSTALL_PLATFORM_LOONGARCH64_EFI: + case GRUB_INSTALL_PLATFORM_I386_IEEE1275: + case GRUB_INSTALL_PLATFORM_SPARC64_IEEE1275: + case GRUB_INSTALL_PLATFORM_POWERPC_IEEE1275: +@@ -965,6 +970,7 @@ main (int argc, char *argv[]) + case GRUB_INSTALL_PLATFORM_ARM_EFI: + case GRUB_INSTALL_PLATFORM_ARM64_EFI: + case GRUB_INSTALL_PLATFORM_IA64_EFI: ++ case GRUB_INSTALL_PLATFORM_LOONGARCH64_EFI: + case GRUB_INSTALL_PLATFORM_I386_IEEE1275: + case GRUB_INSTALL_PLATFORM_ARM_UBOOT: + case GRUB_INSTALL_PLATFORM_I386_QEMU: +@@ -1110,6 +1116,9 @@ main (int argc, char *argv[]) + case GRUB_INSTALL_PLATFORM_ARM64_EFI: + efi_file = "BOOTAA64.EFI"; + break; ++ case GRUB_INSTALL_PLATFORM_LOONGARCH64_EFI: ++ efi_file = "BOOTLOONGARCH64.EFI"; ++ break; + default: + grub_util_error ("%s", _("You've found a bug")); + break; +@@ -1137,6 +1146,9 @@ main (int argc, char *argv[]) + case GRUB_INSTALL_PLATFORM_ARM64_EFI: + efi_file = "grubaa64.efi"; + break; ++ case GRUB_INSTALL_PLATFORM_LOONGARCH64_EFI: ++ efi_file = "grubloongarch64.efi"; ++ break; + default: + efi_file = "grub.efi"; + break; +@@ -1440,6 +1452,7 @@ main (int argc, char *argv[]) + case GRUB_INSTALL_PLATFORM_ARM_EFI: + case GRUB_INSTALL_PLATFORM_ARM64_EFI: + case GRUB_INSTALL_PLATFORM_IA64_EFI: ++ case GRUB_INSTALL_PLATFORM_LOONGARCH64_EFI: + g = grub_util_guess_efi_drive (*curdev); + break; + case GRUB_INSTALL_PLATFORM_SPARC64_IEEE1275: +@@ -1581,6 +1594,7 @@ main (int argc, char *argv[]) + case GRUB_INSTALL_PLATFORM_ARM_EFI: + case GRUB_INSTALL_PLATFORM_ARM64_EFI: + case GRUB_INSTALL_PLATFORM_IA64_EFI: ++ case GRUB_INSTALL_PLATFORM_LOONGARCH64_EFI: + core_name = "core.efi"; + snprintf (mkimage_target, sizeof (mkimage_target), + "%s-%s", +@@ -1683,6 +1697,7 @@ main (int argc, char *argv[]) + case GRUB_INSTALL_PLATFORM_ARM_EFI: + case GRUB_INSTALL_PLATFORM_ARM64_EFI: + case GRUB_INSTALL_PLATFORM_IA64_EFI: ++ case GRUB_INSTALL_PLATFORM_LOONGARCH64_EFI: + case GRUB_INSTALL_PLATFORM_MIPSEL_QEMU_MIPS: + case GRUB_INSTALL_PLATFORM_MIPS_QEMU_MIPS: + case GRUB_INSTALL_PLATFORM_I386_COREBOOT: +@@ -1917,6 +1932,7 @@ main (int argc, char *argv[]) + case GRUB_INSTALL_PLATFORM_ARM_EFI: + case GRUB_INSTALL_PLATFORM_ARM64_EFI: + case GRUB_INSTALL_PLATFORM_IA64_EFI: ++ case GRUB_INSTALL_PLATFORM_LOONGARCH64_EFI: + { + char *dst = grub_util_path_concat (2, efidir, efi_file); + grub_install_copy_file (imgfile, dst, 1); +diff --git a/util/grub-mkimagexx.c b/util/grub-mkimagexx.c +index 1bb5eb8..240e193 100644 +--- a/util/grub-mkimagexx.c ++++ b/util/grub-mkimagexx.c +@@ -783,6 +783,9 @@ SUFFIX (relocate_addrs) (Elf_Ehdr *e, struct section_metadata *smd, + struct grub_ia64_trampoline *tr = (void *) (pe_target + tramp_off); + grub_uint64_t *gpptr = (void *) (pe_target + got_off); + unsigned unmatched_adr_got_page = 0; ++ grub_uint64_t oprs[10240]= {0}; ++ int opri = -1; ++ grub_uint32_t la_abs = 0; + #define MASK19 ((1 << 19) - 1) + #else + grub_uint32_t *tr = (void *) (pe_target + tramp_off); +@@ -1122,6 +1125,173 @@ SUFFIX (relocate_addrs) (Elf_Ehdr *e, struct section_metadata *smd, + } + break; + } ++ case EM_LOONGARCH64: ++ { ++ sym_addr += addend; ++ switch (ELF_R_TYPE (info)) ++ { ++ case R_LARCH_64: ++ { ++ *target=(grub_uint64_t)sym_addr; ++ } ++ break; ++ case R_LARCH_MARK_LA: ++ { ++ la_abs=1; ++ } ++ break; ++ case R_LARCH_SOP_PUSH_PCREL: ++ { ++ opri++; ++ oprs[opri]=(grub_uint64_t)(sym_addr-(target_section_addr+offset+image_target->vaddr_offset)); ++ } ++ break; ++ case R_LARCH_SOP_PUSH_ABSOLUTE: ++ { ++ opri++; ++ oprs[opri]=(grub_uint64_t)sym_addr; ++ } ++ break; ++ case R_LARCH_SOP_PUSH_PLT_PCREL: ++ { ++ opri++; ++ oprs[opri]=(grub_uint64_t)(sym_addr-(target_section_addr+offset+image_target->vaddr_offset)); ++ } ++ break; ++ case R_LARCH_SOP_SUB: ++ { ++ grub_uint64_t opr2=oprs[opri]; ++ opri--; ++ grub_uint64_t opr1=oprs[opri]; ++ opri--; ++ opri++; ++ oprs[opri]=opr1 - opr2; ++ } ++ break; ++ case R_LARCH_SOP_SL: ++ { ++ grub_uint64_t opr2=oprs[opri]; ++ opri--; ++ grub_uint64_t opr1=oprs[opri]; ++ opri--; ++ opri++; ++ oprs[opri]=opr1 << opr2; ++ } ++ break; ++ case R_LARCH_SOP_SR: ++ { ++ grub_uint64_t opr2=oprs[opri]; ++ opri--; ++ grub_uint64_t opr1=oprs[opri]; ++ opri--; ++ opri++; ++ oprs[opri]=opr1 >> opr2; ++ } ++ break; ++ case R_LARCH_SOP_ADD: ++ { ++ grub_uint64_t opr2=oprs[opri]; ++ opri--; ++ grub_uint64_t opr1=oprs[opri]; ++ opri--; ++ opri++; ++ oprs[opri]=opr1 + opr2; ++ } ++ break; ++ case R_LARCH_SOP_AND: ++ { ++ grub_uint64_t opr2=oprs[opri]; ++ opri--; ++ grub_uint64_t opr1=oprs[opri]; ++ opri--; ++ opri++; ++ oprs[opri]=opr1 & opr2; ++ } ++ break; ++ case R_LARCH_SOP_IF_ELSE: ++ { ++ grub_uint64_t opr3=oprs[opri]; ++ opri--; ++ grub_uint64_t opr2=oprs[opri]; ++ opri--; ++ grub_uint64_t opr1=oprs[opri]; ++ opri--; ++ if(opr1){ ++ opri++; ++ oprs[opri]=opr2; ++ } else { ++ opri++; ++ oprs[opri]=opr3; ++ } ++ } ++ break; ++ case R_LARCH_SOP_POP_32_S_10_5: ++ { ++ grub_uint64_t opr1 = oprs[opri]; ++ opri--; ++ *target=(*target) | ((opr1 & 0x1f) << 10); ++ } ++ break; ++ case R_LARCH_SOP_POP_32_U_10_12: ++ { ++ grub_uint64_t opr1 = oprs[opri]; ++ opri--; ++ *target=(*target) | ((opr1 & 0xfff) << 10); ++ } ++ break; ++ case R_LARCH_SOP_POP_32_S_10_12: ++ { ++ if(la_abs==1) ++ la_abs=0; ++ grub_uint64_t opr1 = oprs[opri]; ++ opri--; ++ *target = (*target) | ((opr1 & 0xfff) << 10); ++ } ++ break; ++ case R_LARCH_SOP_POP_32_S_10_16: ++ { ++ grub_uint64_t opr1 = oprs[opri]; ++ opri--; ++ *target = (*target) | ((opr1 & 0xffff) << 10); ++ } ++ break; ++ case R_LARCH_SOP_POP_32_S_10_16_S2: ++ { ++ grub_uint64_t opr1 = oprs[opri]; ++ opri--; ++ *target = (*target) | (((opr1 >> 2) & 0xffff) << 10); ++ } ++ break; ++ case R_LARCH_SOP_POP_32_S_5_20: ++ { ++ grub_uint64_t opr1 = oprs[opri]; ++ opri--; ++ *target = (*target) | ((opr1 & 0xfffff)<<5) ; ++ } ++ break; ++ case R_LARCH_SOP_POP_32_S_0_5_10_16_S2: ++ { ++ grub_uint64_t opr1 = oprs[opri]; ++ opri--; ++ *target =(*target) | (((opr1 >> 2) & 0xffff) << 10); ++ *target =(*target) | ((opr1 >> 18) & 0x1f); ++ } ++ break; ++ case R_LARCH_SOP_POP_32_S_0_10_10_16_S2: ++ { ++ grub_uint64_t opr1 = oprs[opri]; ++ opri--; ++ *target =(*target) | (((opr1 >> 2) & 0xffff) << 10); ++ *target =(*target) | ((opr1 >> 18) & 0x3ff); ++ } ++ break; ++ default: ++ grub_util_error (_("relocation 0x%x is not implemented yet"), ++ (unsigned int) ELF_R_TYPE (info)); ++ break; ++ } ++ break; ++ } + #endif + #if defined(MKIMAGE_ELF32) + case EM_ARM: +@@ -1310,7 +1480,10 @@ add_fixup_entry (struct fixup_block_list **cblock, grub_uint16_t type, + + /* The spec does not mention the requirement of a Page RVA. + Here, align the address with a 4K boundary for safety. */ +- b->page_rva = (addr & ~(0x1000 - 1)); ++#ifdef GRUB_CPU_LOONGARCH64 ++ if (type) ++#endif ++ b->page_rva = (addr & ~(0x1000 - 1)); + b->block_size = sizeof (*b); + } + +@@ -1320,7 +1493,11 @@ add_fixup_entry (struct fixup_block_list **cblock, grub_uint16_t type, + + /* Add a new entry. */ + cur_index = ((b->block_size - sizeof (*b)) >> 1); ++#ifdef GRUB_CPU_LOONGARCH64 ++ entry = GRUB_PE32_FIXUP_ENTRY (type, type ? (addr - b->page_rva) : addr); ++#else + entry = GRUB_PE32_FIXUP_ENTRY (type, addr - b->page_rva); ++#endif + b->entries[cur_index] = grub_host_to_target16 (entry); + b->block_size += 2; + } +@@ -1366,6 +1543,10 @@ static void + translate_relocation_pe (struct translate_context *ctx, + Elf_Addr addr, + Elf_Addr info, ++#ifdef GRUB_CPU_LOONGARCH64 ++ Elf_Addr sym_addr, ++ Elf_Addr addend, ++#endif + const struct grub_install_image_target_desc *image_target) + { + /* Necessary to relocate only absolute addresses. */ +@@ -1477,6 +1658,133 @@ translate_relocation_pe (struct translate_context *ctx, + } + break; + break; ++ case EM_LOONGARCH64: ++ switch (ELF_R_TYPE (info)) ++ { ++ case R_LARCH_NONE: ++ break; ++ case R_LARCH_32: ++ break; ++ case R_LARCH_64: ++ { ++ ctx->current_address = add_fixup_entry ( ++ &ctx->lst, ++ GRUB_PE32_REL_BASED_DIR64, ++ addr, 0, ctx->current_address, ++ image_target); ++ } ++ break; ++ case R_LARCH_RELATIVE: ++ break; ++ case R_LARCH_COPY: ++ break; ++ case R_LARCH_JUMP_SLOT: ++ break; ++ case R_LARCH_TLS_DTPMOD32: ++ break; ++ case R_LARCH_TLS_DTPMOD64: ++ break; ++ case R_LARCH_TLS_DTPREL32: ++ break; ++ case R_LARCH_TLS_DTPREL64: ++ break; ++ case R_LARCH_TLS_TPREL32: ++ break; ++ case R_LARCH_TLS_TPREL64: ++ break; ++ case R_LARCH_IRELATIVE: ++ break; ++ case R_LARCH_MARK_LA: ++ { ++ ctx->current_address = add_fixup_entry ( ++ &ctx->lst, ++ GRUB_PE32_REL_BASED_LOONGARCH64, ++ addr, 0, ctx->current_address, ++ image_target); ++ } ++ break; ++ case R_LARCH_MARK_PCREL: ++ break; ++ case R_LARCH_SOP_PUSH_PCREL: ++ break; ++ case R_LARCH_SOP_PUSH_ABSOLUTE: ++ break; ++ case R_LARCH_SOP_PUSH_DUP: ++ break; ++ case R_LARCH_SOP_PUSH_GPREL: ++ break; ++ case R_LARCH_SOP_PUSH_TLS_TPREL: ++ break; ++ case R_LARCH_SOP_PUSH_TLS_GOT: ++ break; ++ case R_LARCH_SOP_PUSH_TLS_GD: ++ break; ++ case R_LARCH_SOP_PUSH_PLT_PCREL: ++ break; ++ case R_LARCH_SOP_ASSERT: ++ break; ++ case R_LARCH_SOP_NOT: ++ break; ++ case R_LARCH_SOP_SUB: ++ break; ++ case R_LARCH_SOP_SL: ++ break; ++ case R_LARCH_SOP_SR: ++ break; ++ case R_LARCH_SOP_ADD: ++ break; ++ case R_LARCH_SOP_AND: ++ break; ++ case R_LARCH_SOP_IF_ELSE: ++ break; ++ case R_LARCH_SOP_POP_32_S_10_5: ++ break; ++ case R_LARCH_SOP_POP_32_U_10_12: ++ break; ++ case R_LARCH_SOP_POP_32_S_10_12: ++ break; ++ case R_LARCH_SOP_POP_32_S_10_16: ++ break; ++ case R_LARCH_SOP_POP_32_S_10_16_S2: ++ break; ++ case R_LARCH_SOP_POP_32_S_5_20: ++ break; ++ case R_LARCH_SOP_POP_32_S_0_5_10_16_S2: ++ break; ++ case R_LARCH_SOP_POP_32_S_0_10_10_16_S2: ++ break; ++ case R_LARCH_SOP_POP_32_U: ++ break; ++ case R_LARCH_ADD8: ++ break; ++ case R_LARCH_ADD16: ++ break; ++ case R_LARCH_ADD24: ++ break; ++ case R_LARCH_ADD32: ++ break; ++ case R_LARCH_ADD64: ++ break; ++ case R_LARCH_SUB8: ++ break; ++ case R_LARCH_SUB16: ++ break; ++ case R_LARCH_SUB24: ++ break; ++ case R_LARCH_SUB32: ++ break; ++ case R_LARCH_SUB64: ++ break; ++ case R_LARCH_GNU_VTINHERIT: ++ break; ++ case R_LARCH_GNU_VTENTRY: ++ break; ++ default: ++ grub_util_error (_("relocation 0x%x is not implemented yet"), ++ (unsigned int) ELF_R_TYPE (info)); ++ break; ++ } ++ break; + #if defined(MKIMAGE_ELF32) + case EM_ARM: + switch (ELF_R_TYPE (info)) +@@ -1565,10 +1873,18 @@ static void + translate_relocation (struct translate_context *ctx, + Elf_Addr addr, + Elf_Addr info, ++#ifdef GRUB_CPU_LOONGARCH64 ++ Elf_Addr sym_addr, ++ Elf_Addr addend, ++#endif + const struct grub_install_image_target_desc *image_target) + { + if (image_target->id == IMAGE_EFI) ++#ifdef GRUB_CPU_LOONGARCH64 ++ translate_relocation_pe (ctx, addr, info, sym_addr, addend, image_target); ++#else + translate_relocation_pe (ctx, addr, info, image_target); ++#endif + else + translate_relocation_raw (ctx, addr, info, image_target); + } +@@ -1709,11 +2025,21 @@ make_reloc_section (Elf_Ehdr *e, struct grub_mkimage_layout *layout, + if ((grub_target_to_host32 (s->sh_type) == SHT_REL) || + (grub_target_to_host32 (s->sh_type) == SHT_RELA)) + { ++#ifdef GRUB_CPU_LOONGARCH64 ++ Elf_Rela *r; ++#else + Elf_Rel *r; ++#endif + Elf_Word rtab_size, r_size, num_rs; + Elf_Off rtab_offset; + Elf_Addr section_address; + Elf_Word j; ++#ifdef GRUB_CPU_LOONGARCH64 ++ Elf_Shdr *symtab_section; ++ ++ symtab_section = (Elf_Shdr *) ((char *) smd->sections ++ + (grub_target_to_host32 (s->sh_link) * smd->section_entsize)); ++#endif + + if (!SUFFIX (is_kept_reloc_section) (s, image_target, smd)) + { +@@ -1732,20 +2058,39 @@ make_reloc_section (Elf_Ehdr *e, struct grub_mkimage_layout *layout, + + section_address = smd->vaddrs[grub_le_to_cpu32 (s->sh_info)]; + ++#ifdef GRUB_CPU_LOONGARCH64 ++ for (j = 0, r = (Elf_Rela *) ((char *) e + rtab_offset); ++ j < num_rs; ++ j++, r = (Elf_Rela *) ((char *) r + r_size)) ++#else + for (j = 0, r = (Elf_Rel *) ((char *) e + rtab_offset); + j < num_rs; + j++, r = (Elf_Rel *) ((char *) r + r_size)) ++#endif + { + Elf_Addr info; + Elf_Addr offset; + Elf_Addr addr; ++#ifdef GRUB_CPU_LOONGARCH64 ++ Elf_Addr sym_addr; ++ Elf_Addr addend; ++#endif + + offset = grub_target_to_host (r->r_offset); + info = grub_target_to_host (r->r_info); +- ++#ifdef GRUB_CPU_LOONGARCH64 ++ sym_addr = SUFFIX (get_symbol_address) (e, symtab_section, ++ ELF_R_SYM (info), image_target); ++ addend = (s->sh_type == grub_target_to_host32 (SHT_RELA)) ? ++ grub_target_to_host (r->r_addend) : 0; ++#endif + addr = section_address + offset; + ++#ifdef GRUB_CPU_LOONGARCH64 ++ translate_relocation (&ctx, addr, info, sym_addr, addend, image_target); ++#else + translate_relocation (&ctx, addr, info, image_target); ++#endif + } + } + +diff --git a/util/grub-mknetdir.c b/util/grub-mknetdir.c +index ae31271..c998850 100644 +--- a/util/grub-mknetdir.c ++++ b/util/grub-mknetdir.c +@@ -112,7 +112,8 @@ static struct + [GRUB_INSTALL_PLATFORM_X86_64_EFI] = { "x86_64-efi", "efinet", ".efi" }, + [GRUB_INSTALL_PLATFORM_IA64_EFI] = { "ia64-efi", "efinet", ".efi" }, + [GRUB_INSTALL_PLATFORM_ARM_EFI] = { "arm-efi", "efinet", ".efi" }, +- [GRUB_INSTALL_PLATFORM_ARM64_EFI] = { "arm64-efi", "efinet", ".efi" } ++ [GRUB_INSTALL_PLATFORM_ARM64_EFI] = { "arm64-efi", "efinet", ".efi" }, ++ [GRUB_INSTALL_PLATFORM_LOONGARCH64_EFI] = { "loongarch64-efi", "efinet", ".efi" } + }; + + static void +diff --git a/util/grub-module-verifier.c b/util/grub-module-verifier.c +index 03ba1ab..4a9943a 100644 +--- a/util/grub-module-verifier.c ++++ b/util/grub-module-verifier.c +@@ -119,6 +119,52 @@ struct grub_module_verifier_arch archs[] = { + -1 + } + }, ++ { "loongarch64", 8, 0, EM_LOONGARCH64, GRUB_MODULE_VERIFY_SUPPORTS_REL | GRUB_MODULE_VERIFY_SUPPORTS_RELA, (int[]){ ++ R_LARCH_NONE, ++ R_LARCH_32, ++ R_LARCH_64, ++ R_LARCH_RELATIVE, ++ R_LARCH_COPY, ++ R_LARCH_JUMP_SLOT, ++ R_LARCH_TLS_DTPMOD32, ++ R_LARCH_TLS_DTPMOD64, ++ R_LARCH_TLS_DTPREL32, ++ R_LARCH_TLS_DTPREL64, ++ R_LARCH_TLS_TPREL32, ++ R_LARCH_TLS_TPREL64, ++ R_LARCH_IRELATIVE, ++ R_LARCH_MARK_LA, ++ R_LARCH_MARK_PCREL, ++ R_LARCH_SOP_PUSH_PCREL, ++ R_LARCH_SOP_PUSH_ABSOLUTE, ++ R_LARCH_SOP_PUSH_DUP, ++ R_LARCH_SOP_PUSH_GPREL, ++ R_LARCH_SOP_PUSH_TLS_TPREL, ++ R_LARCH_SOP_PUSH_TLS_GOT, ++ R_LARCH_SOP_PUSH_TLS_GD, ++ R_LARCH_SOP_PUSH_PLT_PCREL, ++ R_LARCH_SOP_ASSERT, ++ R_LARCH_SOP_NOT, ++ R_LARCH_SOP_SUB, ++ R_LARCH_SOP_SL, ++ R_LARCH_SOP_SR, ++ R_LARCH_SOP_ADD, ++ R_LARCH_SOP_AND, ++ R_LARCH_SOP_IF_ELSE, ++ R_LARCH_SOP_POP_32_S_10_5, ++ R_LARCH_SOP_POP_32_U_10_12, ++ R_LARCH_SOP_POP_32_S_10_12, ++ R_LARCH_SOP_POP_32_S_10_16, ++ R_LARCH_SOP_POP_32_S_10_16_S2, ++ R_LARCH_SOP_POP_32_S_5_20, ++ R_LARCH_SOP_POP_32_S_0_5_10_16_S2, ++ R_LARCH_SOP_POP_32_S_0_10_10_16_S2, ++ R_LARCH_SOP_POP_32_U, ++ -1 ++ }, (int[]){ ++ -1 ++ } ++ }, + }; + + struct platform_whitelist { +diff --git a/util/mkimage.c b/util/mkimage.c +index 16418e2..0d39c07 100644 +--- a/util/mkimage.c ++++ b/util/mkimage.c +@@ -609,6 +609,22 @@ static const struct grub_install_image_target_desc image_targets[] = + .pe_target = GRUB_PE32_MACHINE_ARM64, + .elf_target = EM_AARCH64, + }, ++ { ++ .dirname = "loongarch64-efi", ++ .names = { "loongarch64-efi", NULL }, ++ .voidp_sizeof = 8, ++ .bigendian = 0, ++ .id = IMAGE_EFI, ++ .flags = PLATFORM_FLAGS_NONE, ++ .total_module_size = TARGET_NO_FIELD, ++ .decompressor_compressed_size = TARGET_NO_FIELD, ++ .decompressor_uncompressed_size = TARGET_NO_FIELD, ++ .decompressor_uncompressed_addr = TARGET_NO_FIELD, ++ .section_align = GRUB_PE32_SECTION_ALIGNMENT, ++ .vaddr_offset = EFI64_HEADER_SIZE, ++ .pe_target = GRUB_PE32_MACHINE_LOONGARCH64, ++ .elf_target = EM_LOONGARCH64, ++ }, + }; + + #include +-- +2.27.0 + diff --git a/grub.macros b/grub.macros index e55de53..07a0edd 100644 --- a/grub.macros +++ b/grub.macros @@ -92,7 +92,7 @@ %endif -%global efi_only aarch64 %{arm} +%global efi_only aarch64 %{arm} loongarch64 %global efi_arch x86_64 ia64 %{efi_only} %ifarch %{efi_arch} %global with_efi_arch 1 @@ -122,7 +122,7 @@ %global platform_modules " appendedsig " %endif -%ifarch aarch64 %{arm} +%ifarch aarch64 %{arm} loongarch64 %global platform_modules " " %endif @@ -187,6 +187,14 @@ )} %endif +%ifarch loongarch64 +%global with_emu_arch 0 +%global efiarch loongarch64 +%global target_cpu_name loongarch64 +%global grub_target_name loongarch64-efi +%global package_arch efi-loongarch64 +%endif + %global _target_platform %{target_cpu_name}-%{_vendor}-%{_target_os}%{?_gnu} %global _alt_target_platform %{alt_target_cpu_name}-%{_vendor}-%{_target_os}%{?_gnu} diff --git a/grub.patches b/grub.patches index 066ebb5..31d08f1 100644 --- a/grub.patches +++ b/grub.patches @@ -663,3 +663,5 @@ Patch0663: 0663-types-Make-bool-generally-available.patch Patch0664: 0664-Remove-exttra-bool-definitions.patch Patch0665: 0665-fs-xfs-Fix-issues-found-while-fuzzing-the-XFS-filesy.patch Patch0666: 0666-ieee1275-ofnet-Fix-grub_malloc-removed-after-added-s.patch +# Support loongarch64 +#Patch1000: 1000-loongarch64-add-support.patch diff --git a/grub2.spec b/grub2.spec index e017723..067e917 100644 --- a/grub2.spec +++ b/grub2.spec @@ -5,6 +5,10 @@ %undefine _missing_build_ids_terminate_build %global _configure_gnuconfig_hack 0 +%ifarch loongarch64 +%global _configure_gnuconfig_hack 1 +%endif + Name: grub2 Epoch: 1 Version: 2.02 @@ -115,7 +119,7 @@ Requires(post): dracut %{desc} This subpackage provides tools for support of all platforms. -%ifarch x86_64 +%ifarch x86_64 loongarch64 %package tools-efi Summary: Support tools for GRUB. Group: System Environment/Base @@ -185,6 +189,11 @@ git add grub-%{grublegacyarch}-%{tarversion} %endif git commit -m "After making subdirs" +%ifarch loongarch64 +%_update_config_sub +%_update_config_guess +%endif + %build %if 0%{with_efi_arch} %{expand:%do_primary_efi_build %%{grubefiarch} %%{grubefiname} %%{grubeficdname} %%{_target_platform} %%{efi_target_cflags} %%{efi_host_cflags} %{old_sb_ca} %{old_sb_cer} %{old_sb_key} %{sb_ca} %{sb_cer} %{sb_key}} @@ -226,8 +235,11 @@ rm -f $RPM_BUILD_ROOT%{_infodir}/dir ln -s %{name}-set-password ${RPM_BUILD_ROOT}/%{_sbindir}/%{name}-setpassword echo '.so man8/%{name}-set-password.8' > ${RPM_BUILD_ROOT}/%{_datadir}/man/man8/%{name}-setpassword.8 %ifnarch x86_64 -rm -vf ${RPM_BUILD_ROOT}/%{_bindir}/%{name}-render-label rm -vf ${RPM_BUILD_ROOT}/%{_sbindir}/%{name}-bios-setup +%endif + +%ifnarch x86_64 loongarch64 +rm -vf ${RPM_BUILD_ROOT}/%{_bindir}/%{name}-render-label rm -vf ${RPM_BUILD_ROOT}/%{_sbindir}/%{name}-macbless %endif @@ -400,7 +412,7 @@ fi %{_datadir}/man/man1/%{name}-editenv* %{_datadir}/man/man1/%{name}-mkpasswd-* -%ifarch x86_64 +%ifarch x86_64 loongarch64 %files tools-efi %{_sbindir}/%{name}-macbless %{_bindir}/%{name}-render-label @@ -526,6 +538,7 @@ fi %changelog * Fri Mar 28 2025 Bo Ren - 2.02-162.0.1 - Build pc-modules package on x86_64 (geliwei@openanolis.org) +- Add loongarch64 base support (zhangwenlong@loongson.cn)(chenguoqi@loongson.cn) * Tue Mar 25 2025 Nicolas Frayer - 2.02-162 - ieee1275/ofnet: Fix grub_malloc() removed after added safe -- Gitee From 1e39ab2b14d8ecb891842cc55d368f205d1680a3 Mon Sep 17 00:00:00 2001 From: songmingliang Date: Thu, 19 May 2022 00:17:50 +0800 Subject: [PATCH 4/8] Fix a bug in bls_make_list, blscfg --- 1001-bls-make-list.patch | 119 +++++++++++++++++++++++++++++++++++++++ grub.patches | 1 + grub2.spec | 1 + 3 files changed, 121 insertions(+) create mode 100644 1001-bls-make-list.patch diff --git a/1001-bls-make-list.patch b/1001-bls-make-list.patch new file mode 100644 index 0000000..ac39162 --- /dev/null +++ b/1001-bls-make-list.patch @@ -0,0 +1,119 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Zhongling He +Date: Tue, 29 Mar 2022 17:27:13 +0800 +Subject: [PATCH] Fix a bug in bls_make_list, blscfg + +We have encountered a bug while running LifseaOS aarch64 version without +initrd. This commit fixes this bug. It may have fixed some potential +bugs as well. + +There are 4 partitions in a LifseaOS disk image: +- The 1st partition is for BIOS bootloader +- The 2nd partition is for UEFI booting, which contains grub binary + compiled from this source code, grubaa64.efi +- The 3rd partition contains grub.cfg files and + loader/entries/ostree-1-LifseaOS.conf +- The 4th partition contains rootfs + +Since x86_64 supports both BIOS and UEFI booting and we employ BIOS +booting for x86_64 (w/o initrd) image, we don't put the grub2 binary +compiled into the 2nd partition. As a result, this bug was not observed. +However, aarch64 only supports UEFI booting. In other words, we are +using this grub2 to boot LifseaOS (w/o initrd). + +grubaa64.efi from the 2nd partition will read `/grub2/grub.cfg` in the +3rd partition. + +``` +... + set ignition_firstboot="ignition.firstboot +${ignition_network_kcmdline}" +fi + +blscfg +``` + +`blscfg` is a command to convert `loader/entries/ostree-1-LifseaOS.conf` +into a typical grub.cfg. However, here comes the bug. While booting, an +error message will appear. + +``` +error: ../../grub-core/loader/arm64/linux.c:292:filename expected. +Press any key to continue. +``` + +This is because the bug in `blscfg` unexpectedly add a line, `initrd`, +into the generated grub.cfg file. Grub2 will expect an initrd file path +after `initrd`. As a result, the error occurs. + +In grub-core/command/blscfg.c:676, if `initrds` is a non-NULL pointer, +then `src` will contain a `initrd` line. + +``` +static void create_entry (struct bls_entry *entry){ + ... + initrds = bls_make_list (entry, "initrd", NULL); // + ... + if (initrds){ + ... + tmp = grub_stpcpy(initrd, "initrd "); + ... + } + ... + src = grub_xasprintf ("load_video\n" + "set gfx_payload=keep\n" + "insmod gzio\n" + "linux %s%s%s%s\n" + "%s", + GRUB_BOOT_DEVICE, clinux, options ? " " : "", options ? options +: "", + initrd ? initrd : ""); +``` + +In grub-core/command/blscfg.c:562, `bls_make_list` will always return a +non-NULL pointer except for a `grub_malloc` error. + +``` +static char **bls_make_list (struct bls_entry *entry, const char *key, +int *num) +{ + ... + list = grub_malloc (sizeof (char *)); + if (!list) + return NULL; + list[0] = NULL; + ... + return list; +} +``` + +Therefore, `initrd` like will be appended into the auto-generated +grub.cfg if `grub_malloc` succeed, regardless of whether initrd is +stated in config file. Our bug fix, same as upstream, modifies the +behaviour of `bls_make_list`. `bls_make_list` will now return a non-NULL +pointer only if the required field actually exists. + +--- + grub-core/commands/blscfg.c | 6 ++++++ + 1 file changed, 6 insertions(+) + +diff --git a/grub-core/commands/blscfg.c b/grub-core/commands/blscfg.c +index 795a9f9..bf18270 100644 +--- a/grub-core/commands/blscfg.c ++++ b/grub-core/commands/blscfg.c +@@ -589,6 +589,12 @@ static char **bls_make_list (struct bls_entry *entry, const char *key, int *num) + list[nlist] = NULL; + } + ++ if (!nlist) ++ { ++ grub_free (list); ++ return NULL; ++ } ++ + if (num) + *num = nlist; + +-- +2.27.0 + diff --git a/grub.patches b/grub.patches index 31d08f1..1ccb15d 100644 --- a/grub.patches +++ b/grub.patches @@ -665,3 +665,4 @@ Patch0665: 0665-fs-xfs-Fix-issues-found-while-fuzzing-the-XFS-filesy.patch Patch0666: 0666-ieee1275-ofnet-Fix-grub_malloc-removed-after-added-s.patch # Support loongarch64 #Patch1000: 1000-loongarch64-add-support.patch +Patch1001: 1001-bls-make-list.patch diff --git a/grub2.spec b/grub2.spec index 067e917..42d281d 100644 --- a/grub2.spec +++ b/grub2.spec @@ -539,6 +539,7 @@ fi * Fri Mar 28 2025 Bo Ren - 2.02-162.0.1 - Build pc-modules package on x86_64 (geliwei@openanolis.org) - Add loongarch64 base support (zhangwenlong@loongson.cn)(chenguoqi@loongson.cn) +- Fix a bug in bls_make_list, blscfg. (zhonglingh@linux.alibaba.com) * Tue Mar 25 2025 Nicolas Frayer - 2.02-162 - ieee1275/ofnet: Fix grub_malloc() removed after added safe -- Gitee From fd20f09514e0f315bf62c7a154744cc68937af35 Mon Sep 17 00:00:00 2001 From: yangqiming Date: Wed, 6 Jul 2022 23:45:53 -0400 Subject: [PATCH 5/8] Add LoongArch64 support and update interface to v40 --- ...-support-and-update-interface-to-v40.patch | 1968 ++++++----------- grub.patches | 1 + grub2.spec | 3 + 3 files changed, 726 insertions(+), 1246 deletions(-) rename 1000-loongarch64-add-support.patch => 1002-Add-LoongArch64-support-and-update-interface-to-v40.patch (64%) diff --git a/1000-loongarch64-add-support.patch b/1002-Add-LoongArch64-support-and-update-interface-to-v40.patch similarity index 64% rename from 1000-loongarch64-add-support.patch rename to 1002-Add-LoongArch64-support-and-update-interface-to-v40.patch index 2d83c94..36750c1 100644 --- a/1000-loongarch64-add-support.patch +++ b/1002-Add-LoongArch64-support-and-update-interface-to-v40.patch @@ -1,87 +1,11 @@ -From febee03c70471fd2ce6f0b4cc0f365c32c106ab3 Mon Sep 17 00:00:00 2001 -From: Fedora Ninjas -Date: Wed, 18 May 2022 23:50:45 +0800 -Subject: [PATCH] add loongarch64 support +From 478205a88ea71b0c546987602cd879027819125e Mon Sep 17 00:00:00 2001 +From: yangqiming +Date: Thu, 7 Jul 2022 11:25:02 +0800 +Subject: [PATCH] Add LoongArch64 support and update interface to V40 ---- - conf/Makefile.common | 4 + - configure.ac | 9 + - gentpl.py | 5 +- - grub-core/Makefile.am | 7 + - grub-core/Makefile.core.def | 20 + - grub-core/kern/efi/mm.c | 29 +- - grub-core/kern/elfXX.c | 6 + - grub-core/kern/loongarch64/cache.S | 26 + - grub-core/kern/loongarch64/dl.c | 258 +++++++++ - grub-core/kern/loongarch64/efi/init.c | 76 +++ - grub-core/kern/loongarch64/efi/startup.S | 45 ++ - grub-core/kern/loongarch64/init.c | 47 ++ - grub-core/lib/loongarch64/efi/loongson.c | 505 ++++++++++++++++ - grub-core/lib/loongarch64/efi/loongson_asm.S | 58 ++ - grub-core/lib/loongarch64/relocator.c | 163 ++++++ - grub-core/lib/loongarch64/relocator_asm.S | 51 ++ - grub-core/lib/loongarch64/setjmp.S | 74 +++ - grub-core/lib/loongson/reboot.c | 33 ++ - grub-core/lib/setjmp.S | 2 + - grub-core/loader/efi/chainloader.c | 2 + - grub-core/loader/loongarch64/linux.c | 580 +++++++++++++++++++ - include/grub/dl.h | 2 +- - include/grub/efi/api.h | 3 +- - include/grub/efi/pe32.h | 2 + - include/grub/elf.h | 55 ++ - include/grub/loongarch64/asm.h | 10 + - include/grub/loongarch64/efi/boot.h | 0 - include/grub/loongarch64/efi/loader.h | 25 + - include/grub/loongarch64/efi/loongson.h | 290 ++++++++++ - include/grub/loongarch64/efi/memory.h | 14 + - include/grub/loongarch64/efi/time.h | 0 - include/grub/loongarch64/io.h | 62 ++ - include/grub/loongarch64/kernel.h | 24 + - include/grub/loongarch64/linux.h | 22 + - include/grub/loongarch64/loongarch64.h | 30 + - include/grub/loongarch64/memory.h | 57 ++ - include/grub/loongarch64/relocator.h | 38 ++ - include/grub/loongarch64/setjmp.h | 27 + - include/grub/loongarch64/time.h | 39 ++ - include/grub/loongarch64/types.h | 34 ++ - include/grub/util/install.h | 1 + - util/grub-install-common.c | 43 +- - util/grub-install.c | 16 + - util/grub-mkimagexx.c | 349 ++++++++++- - util/grub-mknetdir.c | 3 +- - util/grub-module-verifier.c | 46 ++ - util/mkimage.c | 16 + - 47 files changed, 3176 insertions(+), 32 deletions(-) - create mode 100644 grub-core/kern/loongarch64/cache.S - create mode 100644 grub-core/kern/loongarch64/dl.c - create mode 100644 grub-core/kern/loongarch64/efi/init.c - create mode 100644 grub-core/kern/loongarch64/efi/startup.S - create mode 100644 grub-core/kern/loongarch64/init.c - create mode 100644 grub-core/lib/loongarch64/efi/loongson.c - create mode 100644 grub-core/lib/loongarch64/efi/loongson_asm.S - create mode 100644 grub-core/lib/loongarch64/relocator.c - create mode 100644 grub-core/lib/loongarch64/relocator_asm.S - create mode 100644 grub-core/lib/loongarch64/setjmp.S - create mode 100644 grub-core/lib/loongson/reboot.c - create mode 100644 grub-core/loader/loongarch64/linux.c - create mode 100644 include/grub/loongarch64/asm.h - create mode 100644 include/grub/loongarch64/efi/boot.h - create mode 100644 include/grub/loongarch64/efi/loader.h - create mode 100644 include/grub/loongarch64/efi/loongson.h - create mode 100644 include/grub/loongarch64/efi/memory.h - create mode 100644 include/grub/loongarch64/efi/time.h - create mode 100644 include/grub/loongarch64/io.h - create mode 100644 include/grub/loongarch64/kernel.h - create mode 100644 include/grub/loongarch64/linux.h - create mode 100644 include/grub/loongarch64/loongarch64.h - create mode 100644 include/grub/loongarch64/memory.h - create mode 100644 include/grub/loongarch64/relocator.h - create mode 100644 include/grub/loongarch64/setjmp.h - create mode 100644 include/grub/loongarch64/time.h - create mode 100644 include/grub/loongarch64/types.h diff --git a/conf/Makefile.common b/conf/Makefile.common -index 521cdda..cc1d190 100644 +index 521cdda..d9f058b 100644 --- a/conf/Makefile.common +++ b/conf/Makefile.common @@ -20,6 +20,10 @@ endif @@ -89,8 +13,8 @@ index 521cdda..cc1d190 100644 CFLAGS_PLATFORM += -mcpu=powerpc endif +if COND_loongarch64 -+ CFLAGS_PLATFORM += -fno-strict-aliasing -march=loongarch64 -mabi=lp64 -fno-plt -Wa,-mla-global-with-pcrel -+ CPPFLAGS_PLATFORM = -fno-strict-aliasing -march=loongarch64 -mabi=lp64 -fno-plt -Wa,-mla-global-with-pcrel ++ CFLAGS_PLATFORM += -fno-strict-aliasing -march=loongarch64 -mabi=lp64 -fno-plt -Wa,-mla-global-with-abs -mcmodel=large ++ CPPFLAGS_PLATFORM = -fno-strict-aliasing -march=loongarch64 -mabi=lp64 -fno-plt -Wa,-mla-global-with-abs -mcmodel=large +endif # Other options @@ -173,10 +97,10 @@ index d662c30..3afd642 100644 GROUPS["uboot"] = [ "arm_uboot" ] GROUPS["xen"] = [ "i386_xen", "x86_64_xen" ] diff --git a/grub-core/Makefile.am b/grub-core/Makefile.am -index 308ad88..24e2ac3 100644 +index 308ad88..a976fad 100644 --- a/grub-core/Makefile.am +++ b/grub-core/Makefile.am -@@ -222,6 +222,13 @@ KERNEL_HEADER_FILES += $(top_builddir)/include/grub/machine/memory.h +@@ -222,6 +222,14 @@ KERNEL_HEADER_FILES += $(top_builddir)/include/grub/machine/memory.h KERNEL_HEADER_FILES += $(top_builddir)/include/grub/machine/kernel.h endif @@ -184,6 +108,7 @@ index 308ad88..24e2ac3 100644 +KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/efi/efi.h +KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/efi/disk.h +KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/acpi.h ++KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/fdt.h +KERNEL_HEADER_FILES += $(top_builddir)/include/grub/machine/loongson.h +endif + @@ -191,7 +116,7 @@ index 308ad88..24e2ac3 100644 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/ieee1275/ieee1275.h KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/terminfo.h diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def -index ef06f8c..74e66d3 100644 +index ef06f8c..54f6bf0 100644 --- a/grub-core/Makefile.core.def +++ b/grub-core/Makefile.core.def @@ -95,6 +95,9 @@ kernel = { @@ -212,7 +137,7 @@ index ef06f8c..74e66d3 100644 sparc64_ieee1275_startup = kern/sparc64/ieee1275/crt0.S; powerpc_ieee1275_startup = kern/powerpc/ieee1275/startup.S; arm_uboot_startup = kern/arm/startup.S; -@@ -295,6 +299,14 @@ kernel = { +@@ -295,6 +299,15 @@ kernel = { extra_dist = video/sis315_init.c; mips_loongson = commands/keylayouts.c; @@ -220,14 +145,15 @@ index ef06f8c..74e66d3 100644 + loongarch64 = kern/loongarch64/dl.c; + loongarch64 = kern/loongarch64/cache.S; + loongarch64 = kern/generic/rtc_get_time_ms.c; ++ loongarch64 = kern/efi/fdt.c; ++ loongarch64 = lib/fdt.c; + loongarch64_efi = kern/loongarch64/efi/init.c; + loongarch64_efi = lib/loongarch64/efi/loongson.c; -+ loongarch64_efi = lib/loongarch64/efi/loongson_asm.S; + powerpc_ieee1275 = kern/powerpc/cache.S; powerpc_ieee1275 = kern/powerpc/dl.c; powerpc_ieee1275 = kern/powerpc/compiler-rt.S; -@@ -810,6 +822,7 @@ module = { +@@ -810,6 +823,7 @@ module = { enable = sparc64_ieee1275; enable = powerpc_ieee1275; enable = mips_arc; @@ -235,7 +161,7 @@ index ef06f8c..74e66d3 100644 enable = ia64_efi; enable = arm_efi; enable = arm64_efi; -@@ -896,6 +909,7 @@ module = { +@@ -896,6 +910,7 @@ module = { i386_qemu = lib/i386/halt.c; xen = lib/xen/halt.c; efi = lib/efi/halt.c; @@ -243,7 +169,7 @@ index ef06f8c..74e66d3 100644 ieee1275 = lib/ieee1275/halt.c; emu = lib/emu/halt.c; uboot = lib/dummy/halt.c; -@@ -911,6 +925,7 @@ module = { +@@ -911,6 +926,7 @@ module = { mips_arc = lib/mips/arc/reboot.c; mips_loongson = lib/mips/loongson/reboot.c; mips_qemu_mips = lib/mips/qemu_mips/reboot.c; @@ -251,7 +177,7 @@ index ef06f8c..74e66d3 100644 xen = lib/xen/reboot.c; uboot = lib/uboot/reboot.c; arm_coreboot = lib/dummy/reboot.c; -@@ -1608,6 +1623,8 @@ module = { +@@ -1608,6 +1624,8 @@ module = { efi = lib/efi/relocator.c; mips = lib/mips/relocator_asm.S; mips = lib/mips/relocator.c; @@ -260,7 +186,7 @@ index ef06f8c..74e66d3 100644 powerpc = lib/powerpc/relocator_asm.S; powerpc = lib/powerpc/relocator.c; xen = lib/xen/relocator.c; -@@ -1620,6 +1637,7 @@ module = { +@@ -1620,6 +1638,7 @@ module = { extra_dist = kern/powerpc/cache_flush.S; enable = mips; @@ -268,7 +194,7 @@ index ef06f8c..74e66d3 100644 enable = powerpc; enable = x86; enable = xen; -@@ -1737,6 +1755,7 @@ module = { +@@ -1737,6 +1756,7 @@ module = { xen = loader/i386/xen.c; i386_pc = lib/i386/pc/vesa_modes_table.c; mips = loader/mips/linux.c; @@ -276,7 +202,7 @@ index ef06f8c..74e66d3 100644 powerpc_ieee1275 = loader/powerpc/ieee1275/linux.c; sparc64_ieee1275 = loader/sparc64/ieee1275/linux.c; ia64_efi = loader/ia64/efi/linux.c; -@@ -1838,6 +1857,7 @@ module = { +@@ -1838,6 +1858,7 @@ module = { enable = arm_efi; enable = arm64_efi; enable = mips; @@ -285,22 +211,42 @@ index ef06f8c..74e66d3 100644 module = { diff --git a/grub-core/kern/efi/mm.c b/grub-core/kern/efi/mm.c -index 9e76f23..fb087e3 100644 +index 8a89614..0b5f158 100644 --- a/grub-core/kern/efi/mm.c +++ b/grub-core/kern/efi/mm.c -@@ -157,7 +157,11 @@ grub_efi_allocate_pages_real (grub_efi_physical_address_t address, +@@ -122,7 +122,11 @@ grub_efi_allocate_pages_max (grub_efi_physical_address_t max, + grub_efi_boot_services_t *b; + grub_efi_physical_address_t address = max; + ++#ifdef GRUB_CPU_LOONGARCH64 ++ if (max > grub_efi_max_usable_address()) ++#else + if (max > GRUB_EFI_MAX_USABLE_ADDRESS) ++#endif + return 0; + + b = grub_efi_system_table->boot_services; +@@ -157,11 +161,19 @@ grub_efi_allocate_pages_real (grub_efi_physical_address_t address, grub_efi_physical_address_t ret = address; /* Limit the memory access to less than 4GB for 32-bit platforms. */ +#ifdef GRUB_CPU_LOONGARCH64 + if (address > grub_efi_max_usable_address()) ++ { ++ grub_error (GRUB_ERR_BAD_ARGUMENT, ++ N_("invalid memory address 0x%llx"), ++ address); +#else if (address > GRUB_EFI_MAX_USABLE_ADDRESS) -+#endif { grub_error (GRUB_ERR_BAD_ARGUMENT, N_("invalid memory address (0x%llx > 0x%llx)"), -@@ -177,7 +181,11 @@ grub_efi_allocate_pages_real (grub_efi_physical_address_t address, + address, GRUB_EFI_MAX_USABLE_ADDRESS); ++#endif + return NULL; + } + +@@ -177,7 +189,11 @@ grub_efi_allocate_pages_real (grub_efi_physical_address_t address, { /* Uggh, the address 0 was allocated... This is too annoying, so reallocate another one. */ @@ -312,23 +258,23 @@ index 9e76f23..fb087e3 100644 status = efi_call_4 (b->allocate_pages, alloctype, memtype, pages, &ret); grub_efi_free_pages (0, pages); if (status != GRUB_EFI_SUCCESS) -@@ -195,9 +203,14 @@ grub_efi_allocate_pages_real (grub_efi_physical_address_t address, +@@ -195,9 +211,15 @@ grub_efi_allocate_pages_real (grub_efi_physical_address_t address, void * grub_efi_allocate_any_pages (grub_efi_uintn_t pages) { -- return grub_efi_allocate_pages_real (GRUB_EFI_MAX_USABLE_ADDRESS, +#ifdef GRUB_CPU_LOONGARCH64 + return grub_efi_allocate_pages_real (grub_efi_max_usable_address(), ++ pages, GRUB_EFI_ALLOCATE_MAX_ADDRESS, ++ GRUB_EFI_LOADER_DATA); ++#else + return grub_efi_allocate_pages_real (GRUB_EFI_MAX_USABLE_ADDRESS, pages, GRUB_EFI_ALLOCATE_MAX_ADDRESS, GRUB_EFI_LOADER_DATA); -+#else -+ return grub_efi_allocate_pages_real (GRUB_EFI_MAX_USABLE_ADDRESS, -+ pages, GRUB_EFI_ALLOCATE_MAX_ADDRESS, -+#endif GRUB_EFI_LOADER_DATA); ++#endif } void * -@@ -471,7 +484,9 @@ filter_memory_map (grub_efi_memory_descriptor_t *memory_map, +@@ -471,7 +493,9 @@ filter_memory_map (grub_efi_memory_descriptor_t *memory_map, desc = NEXT_MEMORY_DESCRIPTOR (desc, desc_size)) { if (desc->type == GRUB_EFI_CONVENTIONAL_MEMORY @@ -339,11 +285,10 @@ index 9e76f23..fb087e3 100644 && desc->physical_start <= GRUB_EFI_MAX_ALLOCATION_ADDRESS #endif && desc->physical_start + PAGES_TO_BYTES (desc->num_pages) > 0x100000 -@@ -486,8 +501,14 @@ filter_memory_map (grub_efi_memory_descriptor_t *memory_map, - - desc->physical_start); +@@ -487,7 +511,14 @@ filter_memory_map (grub_efi_memory_descriptor_t *memory_map, desc->physical_start = 0x100000; } -- + -#if 1 +#ifdef GRUB_CPU_LOONGARCH64 + if (BYTES_TO_PAGES (filtered_desc->physical_start) @@ -356,23 +301,43 @@ index 9e76f23..fb087e3 100644 if (BYTES_TO_PAGES (filtered_desc->physical_start) + filtered_desc->num_pages > BYTES_TO_PAGES_DOWN (GRUB_EFI_MAX_ALLOCATION_ADDRESS)) -diff --git a/grub-core/kern/elfXX.c b/grub-core/kern/elfXX.c -index 1859d18..8b1dce9 100644 ---- a/grub-core/kern/elfXX.c -+++ b/grub-core/kern/elfXX.c -@@ -134,6 +134,12 @@ grub_elfXX_load (grub_elf_t elf, const char *filename, - load_addr &= 0x3FFFFFFFFFFFFFFFULL; - break; - } +@@ -765,7 +796,7 @@ grub_efi_mm_init (void) + 2 * BYTES_TO_PAGES (MEMORY_MAP_SIZE)); + } + +-#if defined (__aarch64__) || defined (__arm__) ++#if defined (__aarch64__) || defined (__arm__) || defined (__loongarch__) + grub_err_t + grub_efi_get_ram_base(grub_addr_t *base_addr) + { +@@ -784,9 +815,15 @@ grub_efi_get_ram_base(grub_addr_t *base_addr) + if (ret < 1) + return GRUB_ERR_BUG; + ++#ifdef GRUB_CPU_LOONGARCH64 ++ for (desc = memory_map, *base_addr = grub_efi_max_usable_address(); ++ (grub_addr_t) desc < ((grub_addr_t) memory_map + memory_map_size); ++ desc = NEXT_MEMORY_DESCRIPTOR (desc, desc_size)) ++#else + for (desc = memory_map, *base_addr = GRUB_EFI_MAX_USABLE_ADDRESS; + (grub_addr_t) desc < ((grub_addr_t) memory_map + memory_map_size); + desc = NEXT_MEMORY_DESCRIPTOR (desc, desc_size)) ++#endif + { + if (desc->type == GRUB_EFI_CONVENTIONAL_MEMORY && + (desc->attribute & GRUB_EFI_MEMORY_WB)) +@@ -800,7 +837,11 @@ grub_efi_get_ram_base(grub_addr_t *base_addr) + } + } + +#ifdef GRUB_CPU_LOONGARCH64 -+ grub_uint64_t addr; -+ asm volatile ("csrrd %0, 0x181" : "=r" (addr)); -+ if ((load_addr >> 48) != (addr >> 48)) -+ return grub_error (GRUB_ERR_BAD_OS, "bad address space"); ++ if (*base_addr == grub_efi_max_usable_address()) ++#else + if (*base_addr == GRUB_EFI_MAX_USABLE_ADDRESS) +#endif - load_addr += (grub_addr_t) load_offset; + grub_dprintf ("efi", "base_addr 0x%016lx is probably wrong.\n", *base_addr); - if (load_addr < load_base) + grub_free(memory_map); diff --git a/grub-core/kern/loongarch64/cache.S b/grub-core/kern/loongarch64/cache.S new file mode 100644 index 0000000..d291c67 @@ -407,10 +372,10 @@ index 0000000..d291c67 + diff --git a/grub-core/kern/loongarch64/dl.c b/grub-core/kern/loongarch64/dl.c new file mode 100644 -index 0000000..a6fd387 +index 0000000..7d9e2a1 --- /dev/null +++ b/grub-core/kern/loongarch64/dl.c -@@ -0,0 +1,258 @@ +@@ -0,0 +1,266 @@ +/* loongarch64/dl.c - arch-dependent part of loadable module support */ +/* + * GRUB -- GRand Unified Bootloader @@ -669,12 +634,20 @@ index 0000000..a6fd387 + return GRUB_ERR_NONE; +} + ++/* ++ * Tell the loader what our minimum section alignment is. ++ */ ++grub_size_t ++grub_arch_dl_min_alignment (void) ++{ ++ return 1; ++} diff --git a/grub-core/kern/loongarch64/efi/init.c b/grub-core/kern/loongarch64/efi/init.c new file mode 100644 -index 0000000..b21d4f1 +index 0000000..632c39a --- /dev/null +++ b/grub-core/kern/loongarch64/efi/init.c -@@ -0,0 +1,76 @@ +@@ -0,0 +1,74 @@ +/* init.c - initialize an arm-based EFI system */ +/* + * GRUB -- GRand Unified Bootloader @@ -698,7 +671,7 @@ index 0000000..b21d4f1 +#include +#include +#include -+#include ++#include +#include +#include +#include @@ -719,15 +692,13 @@ index 0000000..b21d4f1 + tmr += 10; +} + -+ -+ +void +grub_machine_init (void) +{ + grub_efi_boot_services_t *b; + + grub_efi_init (); -+ ++ + b = grub_efi_system_table->boot_services; + efi_call_5 (b->create_event, GRUB_EFI_EVT_TIMER | GRUB_EFI_EVT_NOTIFY_SIGNAL, + GRUB_EFI_TPL_CALLBACK, grub_loongson_increment_timer, NULL, &tmr_evt); @@ -857,10 +828,10 @@ index 0000000..b2de930 +} diff --git a/grub-core/lib/loongarch64/efi/loongson.c b/grub-core/lib/loongarch64/efi/loongson.c new file mode 100644 -index 0000000..8b60d82 +index 0000000..eda1e1c --- /dev/null +++ b/grub-core/lib/loongarch64/efi/loongson.c -@@ -0,0 +1,505 @@ +@@ -0,0 +1,119 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2017 Free Software Foundation, Inc. @@ -886,400 +857,14 @@ index 0000000..8b60d82 +#include +#include + -+#define loongson_params (&loongson_boot_params->boot_params.efi.smbios.lp) -+#define loongson_boot_params_size ALIGN_UP (sizeof (*loongson_boot_params), 8) -+#define loongson_reset_code_size (&grub_efi_loongson_reset_end - &grub_efi_loongson_reset_start) -+ -+extern grub_uint8_t grub_efi_loongson_reset_start; -+extern grub_uint8_t grub_efi_loongson_reset_end; -+ -+static struct -+{ -+ grub_efi_loongson_boot_params boot_params; -+ grub_efi_loongson_memory_map memory_map; -+ grub_efi_loongson_cpu_info cpu_info; -+ grub_efi_loongson_system_info system_info; -+ grub_efi_loongson_irq_src_routing_table irq_src_routing_table; -+ grub_efi_loongson_interface_info interface_info; -+ grub_efi_loongson_special_attribute special_attribute; -+ grub_efi_loongson_board_devices board_devices; -+} GRUB_PACKED -+* loongson_boot_params; -+ -+static void -+grub_efi_loongson_init_reset_system (void) -+{ -+ grub_efi_loongson_boot_params *boot_params; -+ grub_uint8_t *reset_code_addr = (grub_uint8_t *) loongson_boot_params + -+ loongson_boot_params_size; -+ -+ boot_params = &loongson_boot_params->boot_params; -+ grub_efi_loongson_reset_system_addr = -+ (grub_uint64_t) grub_efi_system_table->runtime_services->reset_system; -+ grub_memcpy (reset_code_addr, &grub_efi_loongson_reset_start, loongson_reset_code_size); -+ grub_arch_sync_caches (reset_code_addr, loongson_reset_code_size); -+ -+ boot_params->reset_system.reset_cold = (grub_uint64_t) reset_code_addr + -+ ((grub_uint64_t) &grub_efi_loongson_reset_cold - -+ (grub_uint64_t) &grub_efi_loongson_reset_start); -+ boot_params->reset_system.reset_warm = (grub_uint64_t) reset_code_addr + -+ ((grub_uint64_t) &grub_efi_loongson_reset_warm - -+ (grub_uint64_t) &grub_efi_loongson_reset_start); -+ boot_params->reset_system.shutdown = (grub_uint64_t) reset_code_addr + -+ ((grub_uint64_t) &grub_efi_loongson_reset_shutdown - -+ (grub_uint64_t) &grub_efi_loongson_reset_start); -+ boot_params->reset_system.do_suspend = (grub_uint64_t) reset_code_addr + -+ ((grub_uint64_t) &grub_efi_loongson_reset_suspend - -+ (grub_uint64_t) &grub_efi_loongson_reset_start); -+} -+ -+static void -+grub_efi_loongson_init_smbios (grub_efi_loongson_smbios_table *smbios_table) -+{ -+ grub_efi_loongson_smbios_table *dst = &loongson_boot_params->boot_params.efi.smbios; -+ -+ dst->vers = smbios_table->vers; -+ dst->vga_bios = smbios_table->vga_bios; -+} -+ -+static void -+grub_efi_loongson_init_cpu_info (grub_efi_loongson_smbios_table *smbios_table) -+{ -+ grub_efi_loongson_cpu_info *src = (void *) smbios_table->lp.cpu_offset; -+ grub_efi_loongson_cpu_info *dst = &loongson_boot_params->cpu_info; -+ -+ if (!src) -+ return; -+ -+ grub_memcpy (dst, src, sizeof (grub_efi_loongson_cpu_info)); -+ loongson_params->cpu_offset = (grub_uint64_t) dst - (grub_uint64_t) loongson_params; -+} -+ -+static void -+grub_efi_loongson_init_system_info (grub_efi_loongson_smbios_table *smbios_table) -+{ -+ grub_efi_loongson_system_info *src = (void *) smbios_table->lp.system_offset; -+ grub_efi_loongson_system_info *dst = &loongson_boot_params->system_info; -+ -+ if (!src) -+ return; -+ -+ grub_memcpy (dst, src, sizeof (grub_efi_loongson_system_info)); -+ loongson_params->system_offset = (grub_uint64_t) dst - (grub_uint64_t) loongson_params; -+} -+ -+static void -+grub_efi_loongson_init_irq_src_routing_table (grub_efi_loongson_smbios_table *smbios_table) -+{ -+ grub_efi_loongson_irq_src_routing_table *src = (void *) smbios_table->lp.irq_offset; -+ grub_efi_loongson_irq_src_routing_table *dst = &loongson_boot_params->irq_src_routing_table; -+ -+ if (!src) -+ return; -+ -+ grub_memcpy (dst, src, sizeof (grub_efi_loongson_irq_src_routing_table)); -+ loongson_params->irq_offset = (grub_uint64_t) dst - (grub_uint64_t) loongson_params; -+} -+ -+static void -+grub_efi_loongson_init_interface_info (grub_efi_loongson_smbios_table *smbios_table) -+{ -+ grub_efi_loongson_interface_info *src = (void *) smbios_table->lp.interface_offset; -+ grub_efi_loongson_interface_info *dst = &loongson_boot_params->interface_info; -+ -+ if (!src) -+ return; -+ -+ grub_memcpy (dst, src, sizeof (grub_efi_loongson_interface_info)); -+ loongson_params->interface_offset = (grub_uint64_t) dst - (grub_uint64_t) loongson_params; -+} -+ -+static void -+grub_efi_loongson_init_special_attribute (grub_efi_loongson_smbios_table *smbios_table) -+{ -+ grub_efi_loongson_special_attribute *src = (void *) smbios_table->lp.special_offset; -+ grub_efi_loongson_special_attribute *dst = &loongson_boot_params->special_attribute; -+ -+ if (!src) -+ return; -+ -+ grub_memcpy (dst, src, sizeof (grub_efi_loongson_special_attribute)); -+ loongson_params->special_offset = (grub_uint64_t) dst - (grub_uint64_t) loongson_params; -+} -+ -+static void -+grub_efi_loongson_init_board_devices (grub_efi_loongson_smbios_table *smbios_table) -+{ -+ grub_efi_loongson_board_devices *src = (void *) smbios_table->lp.boarddev_table_offset; -+ grub_efi_loongson_board_devices *dst = &loongson_boot_params->board_devices; -+ -+ if (!src) -+ return; -+ -+ grub_memcpy (dst, src, sizeof (grub_efi_loongson_board_devices)); -+ loongson_params->boarddev_table_offset = (grub_uint64_t) dst - (grub_uint64_t) loongson_params; -+} -+ -+#define ADD_MEMORY_DESCRIPTOR(desc, size) \ -+ ((grub_efi_memory_descriptor_t *) ((char *) (desc) + (size))) -+ -+static void -+grub_efi_loongson_init_memory_map (grub_efi_loongson_smbios_table *smbios_table, -+ grub_efi_memory_descriptor_t *mmap_buf, -+ grub_efi_uintn_t mmap_size, -+ grub_efi_uintn_t desc_size) -+{ -+ grub_efi_loongson_memory_map *src = (void *) smbios_table->lp.memory_offset; -+ grub_efi_loongson_memory_map *dst = &loongson_boot_params->memory_map; -+ grub_efi_memory_descriptor_t *mmap_end; -+ grub_efi_memory_descriptor_t *desc; -+ grub_efi_memory_descriptor_t *desc_next; -+ grub_efi_uint32_t mem_types_reserved[] = -+ { -+ 1, // GRUB_EFI_RESERVED_MEMORY_TYPE -+ 0, // GRUB_EFI_LOADER_CODE -+ 0, // GRUB_EFI_LOADER_DATA -+ 0, // GRUB_EFI_BOOT_SERVICES_CODE -+ 0, // GRUB_EFI_BOOT_SERVICES_DATA -+ 1, // GRUB_EFI_RUNTIME_SERVICES_CODE -+ 1, // GRUB_EFI_RUNTIME_SERVICES_DATA -+ 0, // GRUB_EFI_CONVENTIONAL_MEMORY -+ 1, // GRUB_EFI_UNUSABLE_MEMORY -+ 0, // GRUB_EFI_ACPI_RECLAIM_MEMORY -+ 0, // GRUB_EFI_ACPI_MEMORY_NVS -+ 1, // GRUB_EFI_MEMORY_MAPPED_IO -+ 1, // GRUB_EFI_MEMORY_MAPPED_IO_PORT_SPACE -+ 1, // GRUB_EFI_PAL_CODE -+ 1, // GRUB_EFI_PERSISTENT_MEMORY -+ }; -+ grub_uint32_t need_sort = 1; -+ -+ if (!src) -+ return; -+ -+ dst->vers = src->vers; -+ dst->nr_map = 0; -+ dst->mem_freq = src->mem_freq; -+ loongson_params->memory_offset = (grub_uint64_t) dst - (grub_uint64_t) loongson_params; -+ -+ if (!mmap_buf || !mmap_size || !desc_size) -+ return; -+ -+ mmap_end = ADD_MEMORY_DESCRIPTOR (mmap_buf, mmap_size); -+ -+ /* drop reserved */ -+ for (desc = mmap_buf, -+ desc_next = desc; -+ desc < mmap_end; -+ desc = ADD_MEMORY_DESCRIPTOR (desc, desc_size)) -+ { -+ desc->type = mem_types_reserved[desc->type]; -+ if (desc->type) -+ continue; -+ -+ if (desc != desc_next) -+ *desc_next = *desc; -+ desc_next = ADD_MEMORY_DESCRIPTOR (desc_next, desc_size); -+ } -+ mmap_end = desc_next; -+ -+ /* sort: low->high */ -+ while (need_sort) -+ { -+ need_sort = 0; -+ -+ for (desc = mmap_buf, -+ desc_next = ADD_MEMORY_DESCRIPTOR (desc, desc_size); -+ (desc < mmap_end) && (desc_next < mmap_end); -+ desc = desc_next, -+ desc_next = ADD_MEMORY_DESCRIPTOR (desc, desc_size)) -+ { -+ grub_efi_memory_descriptor_t tmp; -+ -+ if (desc->physical_start <= desc_next->physical_start) -+ continue; -+ -+ tmp = *desc; -+ *desc = *desc_next; -+ *desc_next = tmp; -+ need_sort = 1; -+ } -+ } -+ -+ /* combine continuous memory map */ -+ for (desc = mmap_buf, -+ desc_next = ADD_MEMORY_DESCRIPTOR (desc, desc_size); -+ desc_next < mmap_end; -+ desc_next = ADD_MEMORY_DESCRIPTOR (desc_next, desc_size)) -+ { -+ grub_efi_physical_address_t prev_end = desc->physical_start + (desc->num_pages << 12); -+ -+ if (prev_end == desc_next->physical_start) -+ { -+ desc->num_pages += desc_next->num_pages; -+ continue; -+ } -+ -+ desc = ADD_MEMORY_DESCRIPTOR (desc, desc_size); -+ grub_memcpy (desc, desc_next, desc_size); -+ } -+ mmap_end = ADD_MEMORY_DESCRIPTOR (desc, desc_size); -+ -+ /* write to loongson memory map */ -+ for (desc = mmap_buf; -+ desc < mmap_end; -+ desc = ADD_MEMORY_DESCRIPTOR (desc, desc_size)) -+ { -+ grub_efi_physical_address_t physical_start = grub_vtop ((void *) desc->physical_start); -+ grub_efi_physical_address_t physical_end = physical_start + (desc->num_pages << 12); -+ -+ physical_start = ALIGN_UP (physical_start, 0x100000); -+ physical_end = ALIGN_DOWN (physical_end, 0x100000); -+ -+ if (physical_start >= physical_end || (physical_end - physical_start) < 0x100000) -+ continue; -+ -+ dst->map[dst->nr_map].node_id = (desc->physical_start >> 44) & 0xf; -+ dst->map[dst->nr_map].mem_type = GRUB_EFI_LOONGSON_SYSTEM_RAM; -+ dst->map[dst->nr_map].mem_start = physical_start; -+ dst->map[dst->nr_map].mem_size = (physical_end - physical_start) >> 20; -+ -+ grub_dprintf ("loongson", "memory map %03u: 0x%016lx 0x%016lx @ %u\n", -+ dst->nr_map, physical_start, physical_end - physical_start, -+ dst->map[dst->nr_map].node_id); -+ -+ dst->nr_map ++; -+ } -+} -+ -+#define BYTES_TO_PAGES(bytes) (((bytes) + 0xfff) >> 12) -+#define SUB_MEMORY_DESCRIPTOR(desc, size) \ -+ ((grub_efi_memory_descriptor_t *) ((char *) (desc) - (size))) -+ -+void -+grub_efi_loongson_alloc_boot_params (void) -+{ -+ grub_efi_memory_descriptor_t *mmap_buf; -+ grub_efi_memory_descriptor_t *mmap_end; -+ grub_efi_memory_descriptor_t *desc; -+ grub_efi_uintn_t mmap_size; -+ grub_efi_uintn_t desc_size; -+ grub_efi_physical_address_t address; -+ grub_efi_allocate_type_t type; -+ grub_efi_uintn_t pages; -+ grub_efi_status_t status; -+ grub_efi_boot_services_t *b; -+ int mm_status; -+ -+ type = GRUB_EFI_ALLOCATE_ADDRESS; -+ pages = BYTES_TO_PAGES (loongson_boot_params_size + loongson_reset_code_size); -+ -+ mmap_size = (1 << 12); -+ mmap_buf = grub_malloc (mmap_size); -+ if (!mmap_buf) -+ grub_fatal ("out of memory!"); -+ -+ mm_status = grub_efi_get_memory_map (&mmap_size, mmap_buf, 0, &desc_size, 0); -+ if (mm_status == 0) -+ { -+ grub_free (mmap_buf); -+ mmap_size += desc_size * 32; -+ -+ mmap_buf = grub_malloc (mmap_size); -+ if (!mmap_buf) -+ grub_fatal ("out of memory!"); -+ -+ mm_status = grub_efi_get_memory_map (&mmap_size, mmap_buf, 0, &desc_size, 0); -+ } -+ -+ if (mm_status < 0) -+ grub_fatal ("cannot get memory map!"); -+ -+ mmap_end = ADD_MEMORY_DESCRIPTOR (mmap_buf, mmap_size); -+ -+ for (desc = SUB_MEMORY_DESCRIPTOR (mmap_end, desc_size); -+ desc >= mmap_buf; -+ desc = SUB_MEMORY_DESCRIPTOR (desc, desc_size)) -+ { -+ if (desc->type != GRUB_EFI_CONVENTIONAL_MEMORY) -+ continue; -+ if (desc->physical_start >= grub_efi_max_usable_address()) -+ continue; -+ if (desc->num_pages < pages) -+ continue; -+ -+ address = desc->physical_start; -+ break; -+ } -+ -+ grub_free (mmap_buf); -+ -+ b = grub_efi_system_table->boot_services; -+ status = efi_call_4 (b->allocate_pages, type, GRUB_EFI_RUNTIME_SERVICES_DATA, pages, &address); -+ if (status != GRUB_EFI_SUCCESS) -+ grub_fatal ("cannot allocate Loongson boot parameters!"); -+ -+ loongson_boot_params = (void *) ((grub_addr_t) address); -+} -+ -+void -+grub_efi_loongson_free_boot_params (void) -+{ -+ grub_efi_free_pages ((grub_addr_t) loongson_boot_params, -+ BYTES_TO_PAGES (loongson_boot_params_size + loongson_reset_code_size)); -+} -+ -+void * -+grub_efi_loongson_get_smbios_table (void) ++unsigned long ++grub_efi_get_bpi_version (const char *str) +{ -+ static grub_efi_loongson_smbios_table *smbios_table; -+ grub_efi_loongson_boot_params *old_boot_params; -+ struct bootparamsinterface* boot_params; -+ void * tmp_boot_params = NULL; -+ char * p = NULL; -+ if(smbios_table) -+ return smbios_table; -+ -+ tmp_boot_params = grub_efi_loongson_get_boot_params(); -+ if(tmp_boot_params == NULL) -+ { -+ grub_dprintf("loongson", "tmp_boot_params is NULL\n"); -+ return tmp_boot_params; -+ } -+ -+ boot_params = (struct bootparamsinterface *)tmp_boot_params; -+ p = (char *)&(boot_params->signature); -+ if(grub_strncmp(p, "BPI", 3) == 0) -+ { -+ grub_dprintf("loongson", "find new bpi\n"); -+ return boot_params ? boot_params : 0; -+ } -+ else -+ { -+ old_boot_params = (grub_efi_loongson_boot_params *)tmp_boot_params; -+ /* -+ { -+ grub_dprintf("loongson", "smbios addr%llx\n", &old_boot_params->efi.smbios); -+ grub_dprintf("loongson", "smbios vers%d\n", (grub_uint16_t)(&old_boot_params->efi.smbios.vers)); -+ grub_dprintf("loongson", "smbios vga_bios%d\n", &old_boot_params->efi.smbios.vga_bios); -+ grub_dprintf("loongson", "lp memory offset %llx\n", &old_boot_params->efi.smbios.lp.memory_offset); -+ grub_dprintf("loongson", "lp cpu offset %llx\n", &old_boot_params->efi.smbios.lp.cpu_offset); -+ grub_dprintf("loongson", "lp system offset %llx\n", &old_boot_params->efi.smbios.lp.system_offset); -+ grub_dprintf("loongson", "lp irq offset %llx\n", &old_boot_params->efi.smbios.lp.irq_offset); -+ grub_dprintf("loongson", "lp interface offset %llx\n", &old_boot_params->efi.smbios.p.interface_offset); -+ grub_dprintf("loongson", "lp special offset %llx\n", &old_boot_params->efi.smbios.lp.special_offset); -+ grub_dprintf("loongson", "lp boarddev table offset %llx\n", &old_boot_params->efi.smbios.lp.boarddev_table_offset); -+ } -+ */ -+ return old_boot_params ? &old_boot_params->efi.smbios : 0; -+ } ++ unsigned long version = GRUB_EFI_BPI_VER_NONE; + -+} ++ version = grub_strtoul (str + 4, 0, 0); + -+int -+grub_efi_is_loongson (void) -+{ -+ return grub_efi_loongson_get_smbios_table () ? 1 : 0; ++ return version; +} + +void * @@ -1287,20 +872,20 @@ index 0000000..8b60d82 +{ + static void * boot_params = NULL; + grub_efi_configuration_table_t *tables; -+ grub_efi_guid_t smbios_guid = GRUB_EFI_LOONGSON_SMBIOS_TABLE_GUID; ++ grub_efi_guid_t loongson_bpi_guid = GRUB_EFI_LOONGSON_BPI_TABLE_GUID; + unsigned int i; + + if (boot_params) + return boot_params; + -+ /* Look for Loongson SMBIOS in UEFI config tables. */ ++ /* Look for Loongson boot params interface in UEFI config tables. */ + tables = grub_efi_system_table->configuration_table; + + for (i = 0; i < grub_efi_system_table->num_table_entries; i++) -+ if (grub_memcmp (&tables[i].vendor_guid, &smbios_guid, sizeof (smbios_guid)) == 0) ++ if (grub_memcmp (&tables[i].vendor_guid, &loongson_bpi_guid, sizeof (loongson_bpi_guid)) == 0) + { + boot_params= tables[i].vendor_table; -+ grub_dprintf ("loongson", "found registered SMBIOS @ %p\n", boot_params); ++ grub_dprintf ("loongson", "found registered BPI @ %p\n", boot_params); + break; + } + return boot_params; @@ -1330,8 +915,9 @@ index 0000000..8b60d82 +} + + -+grub_uint32_t -+grub_efi_loongson_memmap_sort(struct memmap array[], grub_uint32_t length, mem_map * bpmem, grub_uint32_t index, grub_uint32_t memtype) ++grub_uint32_t ++grub_efi_loongson_memmap_v1_sort(struct memmap_v1 array[], grub_uint32_t length, ++ mem_map_v1 * bpmem, grub_uint32_t index, grub_uint32_t memtype) +{ + grub_uint64_t tempmemsize = 0; + grub_uint32_t j = 0; @@ -1342,19 +928,19 @@ index 0000000..8b60d82 + tempmemsize = array[j].memsize; + for(t = j + 1; t < length; t++) + { -+ if(array[j].memstart + tempmemsize == array[t].memstart) ++ if(array[j].memstart + tempmemsize == array[t].memstart) + { + tempmemsize += array[t].memsize; + } + else -+ { ++ { + break; + } + } + bpmem->map[index].memtype = memtype; + bpmem->map[index].memstart = array[j].memstart; + bpmem->map[index].memsize = tempmemsize; -+ grub_dprintf("loongson", "map[%d]:type %x, start 0x%llx, end 0x%llx\n", ++ grub_dprintf("loongson", "v1 map[%d]:type %x, start 0x%lx, end 0x%lx\n", + index, + bpmem->map[index].memtype, + bpmem->map[index].memstart, @@ -1365,74 +951,9 @@ index 0000000..8b60d82 + } + return index; +} -+ -diff --git a/grub-core/lib/loongarch64/efi/loongson_asm.S b/grub-core/lib/loongarch64/efi/loongson_asm.S -new file mode 100644 -index 0000000..4a04d34 ---- /dev/null -+++ b/grub-core/lib/loongarch64/efi/loongson_asm.S -@@ -0,0 +1,58 @@ -+/* -+ * GRUB -- GRand Unified Bootloader -+ * Copyright (C) 2017 Free Software Foundation, Inc. -+ * -+ * GRUB is free software: you can redistribute it and/or modify -+ * it under the terms of the GNU General Public License as published by -+ * the Free Software Foundation, either version 3 of the License, or -+ * (at your option) any later version. -+ * -+ * GRUB is distributed in the hope that it will be useful, -+ * but WITHOUT ANY WARRANTY; without even the implied warranty of -+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -+ * GNU General Public License for more details. -+ * -+ * You should have received a copy of the GNU General Public License -+ * along with GRUB. If not, see . -+ */ -+ -+#include -+ -+ .file "loongson_asm.S" -+ .text -+ -+ .align 4 -+ -+VARIABLE (grub_efi_loongson_reset_start) -+ -+VARIABLE (grub_efi_loongson_reset_system_addr) -+ .dword 0 -+ -+reset_system: -+ bl 1f -+ move $a1, $zero -+1: -+ ld.d $t8, $ra, -16 -+ move $a2, $zero -+ jr $t8 -+ move $a3, $zero -+ -+FUNCTION(grub_efi_loongson_reset_cold) -+ b reset_system -+ li.w $a0, 0 -+ -+FUNCTION(grub_efi_loongson_reset_warm) -+ b reset_system -+ li.w $a0, 1 -+ -+FUNCTION(grub_efi_loongson_reset_shutdown) -+ b reset_system -+ li.w $a0, 2 -+ -+FUNCTION(grub_efi_loongson_reset_suspend) -+ b reset_system -+ li.w $a0, 3 -+ -+VARIABLE (grub_efi_loongson_reset_end) -+ -+ diff --git a/grub-core/lib/loongarch64/relocator.c b/grub-core/lib/loongarch64/relocator.c new file mode 100644 -index 0000000..f6c1b01 +index 0000000..4b253ca --- /dev/null +++ b/grub-core/lib/loongarch64/relocator.c @@ -0,0 +1,163 @@ @@ -1510,12 +1031,9 @@ index 0000000..f6c1b01 +static void +write_jump (int regn, void **target) +{ -+ grub_uint32_t andi=0x4c000000; -+ grub_uint32_t nop=0x03400000; ++ grub_uint32_t jirl=0x4c000000; + -+ *(grub_uint32_t *) *target = (andi | (grub_uint32_t)(regn<<5)); -+ *target = ((grub_uint32_t *) *target) + 1; -+ *(grub_uint32_t *) *target = nop; ++ *(grub_uint32_t *) *target = (jirl | (grub_uint32_t)(regn<<5)); + *target = ((grub_uint32_t *) *target) + 1; +} + @@ -1585,6 +1103,9 @@ index 0000000..f6c1b01 + + grub_arch_sync_caches ((void *) relst, relsize); + ++ asm volatile ( ++ "ibar 0 \n"); ++ + grub_uint64_t val; + __asm__ __volatile__( + "li.w %0, 0x4\n\t" @@ -1789,10 +1310,10 @@ index f6e4905..023dc90 100644 #error "Unknown target cpu type" #endif diff --git a/grub-core/loader/efi/chainloader.c b/grub-core/loader/efi/chainloader.c -index 29663f7..c0ca17d 100644 +index b72e6bd..f4066b5 100644 --- a/grub-core/loader/efi/chainloader.c +++ b/grub-core/loader/efi/chainloader.c -@@ -332,6 +332,8 @@ static const grub_uint16_t machine_type __attribute__((__unused__)) = +@@ -343,6 +343,8 @@ static const grub_uint16_t machine_type __attribute__((__unused__)) = GRUB_PE32_MACHINE_I386; #elif defined(__ia64__) GRUB_PE32_MACHINE_IA64; @@ -1803,10 +1324,10 @@ index 29663f7..c0ca17d 100644 #endif diff --git a/grub-core/loader/loongarch64/linux.c b/grub-core/loader/loongarch64/linux.c new file mode 100644 -index 0000000..c769bb2 +index 0000000..7594ca2 --- /dev/null +++ b/grub-core/loader/loongarch64/linux.c -@@ -0,0 +1,580 @@ +@@ -0,0 +1,690 @@ +/* linux.c - boot Linux */ +/* + * GRUB -- GRand Unified Bootloader @@ -1838,10 +1359,12 @@ index 0000000..c769bb2 +#include +#include +#include ++#include +#include +#include +#include -+ ++#include ++#include + +GRUB_MOD_LICENSE ("GPLv3+"); + @@ -1849,253 +1372,302 @@ index 0000000..c769bb2 + +typedef unsigned long size_t; + -+static grub_dl_t my_mod; ++#define FDT_ADDR_CELLS_STRING "#address-cells" ++#define FDT_SIZE_CELLS_STRING "#size-cells" ++#define FDT_ADDR_SIZE_EXTRA ((2 * grub_fdt_prop_entry_size (sizeof(grub_uint32_t))) + \ ++ sizeof (FDT_ADDR_CELLS_STRING) + \ ++ sizeof (FDT_SIZE_CELLS_STRING)) + ++static grub_dl_t my_mod; +static int loaded; -+ -+static grub_uint32_t tmp_index = 0; ++static int initrd_loaded = 0; +static grub_size_t linux_size; + +static struct grub_relocator *relocator; +static grub_addr_t target_addr, entry_addr; +static int linux_argc; +static grub_uint8_t *linux_args_addr; -+static grub_off_t rd_addr_arg_off, rd_size_arg_off; -+static grub_off_t initrd_addr_arg_off; -+static int initrd_loaded = 0; -+ ++static grub_off_t rd_addr_arg_off, rd_size_arg_off, initrd_addr_arg_off; + -+static grub_uint32_t j = 0; -+static grub_uint32_t t = 0; -+grub_uint64_t tempMemsize = 0; -+grub_uint32_t free_index = 0; -+grub_uint32_t reserve_index = 0; -+grub_uint32_t acpi_table_index = 0; -+grub_uint32_t acpi_nvs_index = 0; ++static void *fdt; ++static int is_fdt_boot; ++static grub_addr_t initrd_start, initrd_end; ++static char *fdt_linux_args; + -+static inline grub_size_t -+page_align (grub_size_t size) ++static grub_err_t ++allocate_fdt_and_exit_boot (void) +{ -+ return (size + (1 << 12) - 1) & (~((1 << 12) - 1)); -+} ++ int node, retval; ++ grub_err_t err; ++ unsigned int size; ++ grub_efi_uintn_t mmap_size; ++ grub_efi_uintn_t desc_size; ++ grub_efi_uint32_t desc_version; ++ grub_efi_memory_descriptor_t *mmap_buf; + -+/* Find the optimal number of pages for the memory map. Is it better to -+ move this code to efi/mm.c? */ -+static grub_efi_uintn_t -+find_mmap_size (void) -+{ -+ static grub_efi_uintn_t mmap_size = 0; ++ size = GRUB_FDT_EMPTY_TREE_SZ + FDT_ADDR_SIZE_EXTRA + GRUB_EFI_LINUX_FDT_EXTRA_SPACE; ++ ++ fdt = grub_efi_allocate_any_pages (GRUB_EFI_BYTES_TO_PAGES (size)); ++ if (!fdt) ++ return GRUB_ERR_OUT_OF_MEMORY; ++ ++ grub_fdt_create_empty_tree (fdt, size); ++ grub_fdt_set_prop32 (fdt, 0, FDT_ADDR_CELLS_STRING, 2); ++ grub_fdt_set_prop32 (fdt, 0, FDT_SIZE_CELLS_STRING, 2); ++ ++ node = grub_fdt_find_subnode (fdt, 0, "chosen"); ++ if (node < 0) ++ node = grub_fdt_add_subnode (fdt, 0, "chosen"); ++ if (node < 1) ++ goto failure; ++ ++ grub_dprintf ("loongson", "command_line %s, len %ld\n", ++ fdt_linux_args, grub_strlen(fdt_linux_args) + 1); ++ if ((fdt_linux_args != NULL) && (grub_strlen(fdt_linux_args) > 0)) { ++ retval = grub_fdt_set_prop (fdt, node, "bootargs", fdt_linux_args, ++ grub_strlen(fdt_linux_args) + 1); ++ if (retval) ++ goto failure; ++ } + -+ if (mmap_size != 0) -+ return mmap_size; ++ /* Set initrd info */ ++ if (initrd_start && initrd_end > initrd_start) ++ { ++ grub_dprintf ("linux", "Initrd @ %p-%p\n", ++ (void *) initrd_start, (void *) initrd_end); ++ ++ retval = grub_fdt_set_prop64 (fdt, node, "linux,initrd-start", ++ initrd_start); ++ if (retval) ++ goto failure; ++ retval = grub_fdt_set_prop64 (fdt, node, "linux,initrd-end", ++ initrd_end); ++ if (retval) ++ goto failure; ++ } + -+ mmap_size = (1 << 12); -+ while (1) -+ { -+ int ret; -+ grub_efi_memory_descriptor_t *mmap; -+ grub_efi_uintn_t desc_size; ++ node = grub_fdt_find_subnode (fdt, 0, "chosen"); ++ retval = grub_fdt_set_prop64 (fdt, node, "linux,uefi-system-table", ++ (grub_uint64_t)grub_efi_system_table); ++ if (retval) ++ goto failure; + -+ mmap = grub_malloc (mmap_size); -+ if (! mmap) -+ return 0; ++ mmap_size = grub_efi_find_mmap_size (); ++ if (! mmap_size) ++ return grub_errno; ++ mmap_buf = grub_efi_allocate_any_pages (GRUB_EFI_BYTES_TO_PAGES (mmap_size)); ++ if (! mmap_buf) ++ return grub_error (GRUB_ERR_IO, "cannot allocate memory map"); ++ err = grub_efi_finish_boot_services (&mmap_size, mmap_buf, NULL, ++ &desc_size, &desc_version); ++ if (err) ++ return err; + -+ ret = grub_efi_get_memory_map (&mmap_size, mmap, 0, &desc_size, 0); -+ grub_free (mmap); ++ if (!mmap_buf || !mmap_size || !desc_size) ++ return GRUB_ERR_BAD_ARGUMENT; + -+ if (ret < 0) -+ { -+ grub_error (GRUB_ERR_IO, "cannot get memory map"); -+ return 0; -+ } -+ else if (ret > 0) -+ break; ++ retval = grub_fdt_set_prop64 (fdt, node, "linux,uefi-mmap-start", ++ (grub_uint64_t)mmap_buf); ++ if (retval) ++ goto failure; + -+ mmap_size += (1 << 12); -+ } ++ retval = grub_fdt_set_prop32 (fdt, node, "linux,uefi-mmap-size", ++ mmap_size); ++ if (retval) ++ goto failure; ++ ++ retval = grub_fdt_set_prop32 (fdt, node, "linux,uefi-mmap-desc-size", ++ desc_size); ++ if (retval) ++ goto failure; + ++ retval = grub_fdt_set_prop32 (fdt, node, "linux,uefi-mmap-desc-ver", ++ desc_version); ++ if (retval) ++ goto failure; + -+ /* Increase the size a bit for safety, because GRUB allocates more on -+ later, and EFI itself may allocate more. */ -+ mmap_size += (1 << 12); ++ return GRUB_ERR_NONE; + -+ return page_align (mmap_size); ++failure: ++ if (!fdt) { ++ return GRUB_ERR_BAD_OS; ++ } ++ grub_efi_free_pages ((grub_addr_t) fdt, ++ GRUB_EFI_BYTES_TO_PAGES (grub_fdt_get_totalsize (fdt))); ++ fdt = NULL; ++ return grub_error(GRUB_ERR_BAD_OS, "failed to install/update FDT"); +} + +static grub_err_t -+grub_linux_boot (void) ++allocate_boot_params_and_exit_boot (void) +{ -+ struct grub_relocator64_state state; -+ grub_int8_t checksum = 0; ++ grub_efi_uintn_t mmap_size; ++ grub_efi_uintn_t desc_size; ++ grub_efi_uint32_t desc_version; ++ grub_efi_memory_descriptor_t *mmap_buf; + grub_efi_memory_descriptor_t * lsdesc = NULL; -+ -+ grub_memset (&state, 0, sizeof (state)); -+ -+ /* Boot the kernel. */ -+ state.gpr[1] = entry_addr; -+ grub_dprintf("loongson", "entry_addr is %p\n", state.gpr[1]); -+ state.gpr[4] = linux_argc; -+ grub_dprintf("loongson", "linux_argc is %d\n", state.gpr[4]); -+ state.gpr[5] = (grub_addr_t) linux_args_addr; -+ grub_dprintf("loongson", "args_addr is %p\n", state.gpr[5]); -+ -+ if(grub_efi_is_loongson ()) -+ { -+ grub_efi_uintn_t mmap_size; -+ grub_efi_uintn_t desc_size; -+ grub_efi_memory_descriptor_t *mmap_buf; -+ grub_err_t err; -+ struct bootparamsinterface * boot_params; -+ void * tmp_boot_params = NULL; -+ grub_efi_uint8_t new_interface_flag = 0; -+ mem_map * new_interface_mem = NULL; -+ char *p = NULL; -+ -+ struct memmap reserve_mem[GRUB_EFI_LOONGSON_MMAP_MAX]; -+ struct memmap free_mem[GRUB_EFI_LOONGSON_MMAP_MAX]; -+ struct memmap acpi_table_mem[GRUB_EFI_LOONGSON_MMAP_MAX]; -+ struct memmap acpi_nvs_mem[GRUB_EFI_LOONGSON_MMAP_MAX]; -+ -+ grub_memset(reserve_mem, 0, sizeof(struct memmap) * GRUB_EFI_LOONGSON_MMAP_MAX); -+ grub_memset(free_mem, 0, sizeof(struct memmap) * GRUB_EFI_LOONGSON_MMAP_MAX); -+ grub_memset(acpi_table_mem, 0, sizeof(struct memmap) * GRUB_EFI_LOONGSON_MMAP_MAX); -+ grub_memset(acpi_nvs_mem, 0, sizeof(struct memmap) * GRUB_EFI_LOONGSON_MMAP_MAX); -+ -+ tmp_boot_params = grub_efi_loongson_get_boot_params(); -+ if(tmp_boot_params == NULL) -+ { -+ grub_printf("not find param\n"); -+ return -1; -+ } -+ -+ boot_params = (struct bootparamsinterface *)tmp_boot_params; -+ p = (char *)&(boot_params->signature); -+ if(grub_strncmp(p, "BPI", 3) == 0) ++ grub_err_t err; ++ struct boot_params_interface * boot_params; ++ mem_map_v1 * mem_map_v1_table = NULL; ++ unsigned long bpi_version = 0; ++ grub_int8_t checksum = 0; ++ grub_uint32_t tmp_index = 0; ++ grub_uint32_t free_index = 0; ++ grub_uint32_t reserve_index = 0; ++ grub_uint32_t acpi_table_index = 0; ++ grub_uint32_t acpi_nvs_index = 0; ++ ++ struct memmap_v1 reserve_mem_v1[GRUB_EFI_LOONGSON_MMAP_MAX]; ++ struct memmap_v1 free_mem_v1[GRUB_EFI_LOONGSON_MMAP_MAX]; ++ struct memmap_v1 acpi_table_mem_v1[GRUB_EFI_LOONGSON_MMAP_MAX]; ++ struct memmap_v1 acpi_nvs_mem_v1[GRUB_EFI_LOONGSON_MMAP_MAX]; ++ ++ grub_memset(reserve_mem_v1, 0, sizeof(struct memmap_v1) * GRUB_EFI_LOONGSON_MMAP_MAX); ++ grub_memset(free_mem_v1, 0, sizeof(struct memmap_v1) * GRUB_EFI_LOONGSON_MMAP_MAX); ++ grub_memset(acpi_table_mem_v1, 0, sizeof(struct memmap_v1) * GRUB_EFI_LOONGSON_MMAP_MAX); ++ grub_memset(acpi_nvs_mem_v1, 0, sizeof(struct memmap_v1) * GRUB_EFI_LOONGSON_MMAP_MAX); ++ ++ boot_params = (struct boot_params_interface *)grub_efi_loongson_get_boot_params(); ++ ++ ext_list * listpointer = NULL; ++ /* Check extlist headers */ ++ listpointer = boot_params->extlist; ++ for( ;listpointer != NULL; listpointer = listpointer->next) + { -+ /* Check extlist headers */ -+ ext_list * listpointer = NULL; -+ listpointer = boot_params->extlist; -+ for( ;listpointer != NULL; listpointer = listpointer->next) ++ char *pl= (char *)&(listpointer->signature); ++ if(grub_strncmp(pl, "MEM", 3) == 0) + { -+ char *pl= (char *)&(listpointer->signature); -+ if(grub_strncmp(pl, "MEM", 3) == 0) -+ { -+ new_interface_mem = (mem_map *)listpointer; -+ } ++ mem_map_v1_table = (mem_map_v1 *)listpointer; ++ break; + } -+ -+ new_interface_flag = 1; -+ grub_dprintf("loongson", "get new parameter interface\n"); -+ }else{ -+ new_interface_flag = 0; -+ grub_dprintf("loongson", "get old parameter interface\n"); + } -+ state.gpr[6] = (grub_uint64_t)tmp_boot_params; -+ grub_dprintf("loongson", "boot_params is %p\n", state.gpr[6]); + -+ mmap_size = find_mmap_size (); -+ if (! mmap_size) -+ return grub_errno; -+ mmap_buf = grub_efi_allocate_any_pages (page_align (mmap_size) >> 12); -+ if (! mmap_buf) -+ return grub_error (GRUB_ERR_IO, "cannot allocate memory map"); -+ err = grub_efi_finish_boot_services (&mmap_size, mmap_buf, NULL, -+ &desc_size, NULL); -+ if (err) -+ return err; ++ mmap_size = grub_efi_find_mmap_size (); ++ if (! mmap_size) ++ return grub_errno; ++ mmap_buf = grub_efi_allocate_any_pages (GRUB_EFI_BYTES_TO_PAGES (mmap_size)); ++ if (! mmap_buf) ++ return grub_error (GRUB_ERR_IO, "cannot allocate memory map"); ++ err = grub_efi_finish_boot_services (&mmap_size, mmap_buf, NULL, ++ &desc_size, &desc_version); ++ if (err) ++ return err; + -+ if(new_interface_flag) -+ { -+ if (!mmap_buf || !mmap_size || !desc_size) -+ return -1; -+ tmp_index = new_interface_mem -> mapcount; ++ if (!mmap_buf || !mmap_size || !desc_size) ++ return GRUB_ERR_BAD_ARGUMENT; + ++ char *p = (char *)&(boot_params->signature); ++ bpi_version = grub_efi_get_bpi_version(p); ++ grub_dprintf("loongson", "get bpi version:%ld\n", bpi_version); ++ ++ if (bpi_version <= GRUB_EFI_BPI_VER_V2) ++ { + /* -+ According to UEFI SPEC,mmap_buf is the accurate Memory Map array \ -+ now we can fill platform specific memory structure. -+ */ ++ According to UEFI SPEC,mmap_buf is the accurate Memory Map array \ ++ now we can fill platform specific memory structure. ++ */ + for(lsdesc = mmap_buf; lsdesc < (grub_efi_memory_descriptor_t *)((char *)mmap_buf + mmap_size); -+ lsdesc = (grub_efi_memory_descriptor_t *)((char *)lsdesc + desc_size)) ++ lsdesc = (grub_efi_memory_descriptor_t *)((char *)lsdesc + desc_size)) + { -+ /* Recovery */ -+ if((lsdesc->type != GRUB_EFI_ACPI_RECLAIM_MEMORY) && \ -+ (lsdesc->type != GRUB_EFI_ACPI_MEMORY_NVS) && \ -+ (lsdesc->type != GRUB_EFI_RUNTIME_SERVICES_DATA) && \ -+ (lsdesc->type != GRUB_EFI_RUNTIME_SERVICES_CODE) && \ -+ (lsdesc->type != GRUB_EFI_RESERVED_MEMORY_TYPE) && \ -+ (lsdesc->type != GRUB_EFI_PAL_CODE)) ++ grub_dprintf("loongson", "type:%d, phy_start:0x%lx, phy_end:0x%lx\n", lsdesc->type, ++ lsdesc->physical_start, lsdesc->physical_start + lsdesc->num_pages * GRUB_EFI_PAGE_SIZE); ++ ++ /* System RAM */ ++ if ((lsdesc->type != GRUB_EFI_ACPI_RECLAIM_MEMORY) && \ ++ (lsdesc->type != GRUB_EFI_ACPI_MEMORY_NVS) && \ ++ (lsdesc->type != GRUB_EFI_RUNTIME_SERVICES_DATA) && \ ++ (lsdesc->type != GRUB_EFI_RUNTIME_SERVICES_CODE) && \ ++ (lsdesc->type != GRUB_EFI_RESERVED_MEMORY_TYPE) && \ ++ (lsdesc->type != GRUB_EFI_PAL_CODE)) + { -+ free_mem[free_index].memtype = GRUB_EFI_LOONGSON_SYSTEM_RAM; -+ free_mem[free_index].memstart = (lsdesc->physical_start) & 0xffffffffffff; -+ free_mem[free_index].memsize = lsdesc->num_pages * 4096; ++ free_mem_v1[free_index].memtype = GRUB_EFI_LOONGSON_SYSTEM_RAM; ++ free_mem_v1[free_index].memstart = (lsdesc->physical_start) & GRUB_EFI_MAX_PHY_ADDRESS; ++ free_mem_v1[free_index].memsize = lsdesc->num_pages * GRUB_EFI_PAGE_SIZE; + free_index++; + + /*ACPI*/ -+ }else if((lsdesc->type == GRUB_EFI_ACPI_RECLAIM_MEMORY)){ -+ acpi_table_mem[acpi_table_index].memtype = GRUB_EFI_LOONGSON_ACPI_TABLE; -+ acpi_table_mem[acpi_table_index].memstart = (lsdesc->physical_start) & 0xffffffffffff; -+ acpi_table_mem[acpi_table_index].memsize = lsdesc->num_pages * 4096; ++ } else if ((lsdesc->type == GRUB_EFI_ACPI_RECLAIM_MEMORY)){ ++ acpi_table_mem_v1[acpi_table_index].memtype = GRUB_EFI_LOONGSON_ACPI_TABLE; ++ acpi_table_mem_v1[acpi_table_index].memstart = (lsdesc->physical_start) & GRUB_EFI_MAX_PHY_ADDRESS; ++ acpi_table_mem_v1[acpi_table_index].memsize = lsdesc->num_pages * GRUB_EFI_PAGE_SIZE; + acpi_table_index++; -+ }else if((lsdesc->type == GRUB_EFI_ACPI_MEMORY_NVS)){ -+ acpi_nvs_mem[acpi_nvs_index].memtype = GRUB_EFI_LOONGSON_ACPI_NVS; -+ acpi_nvs_mem[acpi_nvs_index].memstart = (lsdesc->physical_start) & 0xffffffffffff; -+ acpi_nvs_mem[acpi_nvs_index].memsize = lsdesc->num_pages * 4096; ++ } else if ((lsdesc->type == GRUB_EFI_ACPI_MEMORY_NVS)){ ++ acpi_nvs_mem_v1[acpi_nvs_index].memtype = GRUB_EFI_LOONGSON_ACPI_NVS; ++ acpi_nvs_mem_v1[acpi_nvs_index].memstart = (lsdesc->physical_start) & GRUB_EFI_MAX_PHY_ADDRESS; ++ acpi_nvs_mem_v1[acpi_nvs_index].memsize = lsdesc->num_pages * GRUB_EFI_PAGE_SIZE; + acpi_nvs_index++; + + /* Reserve */ -+ }else{ -+ reserve_mem[reserve_index].memtype = GRUB_EFI_LOONGSON_MEMORY_RESERVED; -+ reserve_mem[reserve_index].memstart = (lsdesc->physical_start) & 0xffffffffffff; -+ reserve_mem[reserve_index].memsize = lsdesc->num_pages * 4096; ++ } else { ++ reserve_mem_v1[reserve_index].memtype = GRUB_EFI_LOONGSON_MEMORY_RESERVED; ++ reserve_mem_v1[reserve_index].memstart = (lsdesc->physical_start) & GRUB_EFI_MAX_PHY_ADDRESS; ++ reserve_mem_v1[reserve_index].memsize = lsdesc->num_pages * GRUB_EFI_PAGE_SIZE; + reserve_index++; + } + } + -+ /* Recovery sort */ -+ for(j = 0; j < free_index;) -+ { -+ tempMemsize = free_mem[j].memsize; -+ for(t = j + 1; t < free_index; t++) -+ { -+ if((free_mem[j].memstart + tempMemsize == free_mem[t].memstart) && (free_mem[j].memtype == free_mem[t].memtype)) -+ { -+ tempMemsize += free_mem[t].memsize; -+ }else{ -+ break; -+ } -+ } -+ -+ new_interface_mem->map[tmp_index].memtype = GRUB_EFI_LOONGSON_SYSTEM_RAM; -+ new_interface_mem->map[tmp_index].memstart = free_mem[j].memstart; -+ new_interface_mem->map[tmp_index].memsize = tempMemsize; -+ grub_dprintf("loongson", "map[%d]:type %x, start 0x%llx, end 0x%llx\n", -+ tmp_index, -+ new_interface_mem->map[tmp_index].memtype, -+ new_interface_mem->map[tmp_index].memstart, -+ new_interface_mem->map[tmp_index].memstart+ new_interface_mem->map[tmp_index].memsize -+ ); -+ j = t; -+ tmp_index++; -+ } ++ tmp_index = mem_map_v1_table->mapcount; ++ /*System RAM Sort*/ ++ tmp_index = grub_efi_loongson_memmap_v1_sort(free_mem_v1, free_index, mem_map_v1_table, ++ tmp_index, GRUB_EFI_LOONGSON_SYSTEM_RAM); + /*ACPI Sort*/ -+ tmp_index = grub_efi_loongson_memmap_sort(acpi_table_mem, acpi_table_index, new_interface_mem, tmp_index, GRUB_EFI_LOONGSON_ACPI_TABLE); -+ tmp_index = grub_efi_loongson_memmap_sort(acpi_nvs_mem, acpi_nvs_index, new_interface_mem, tmp_index, GRUB_EFI_LOONGSON_ACPI_NVS); ++ tmp_index = grub_efi_loongson_memmap_v1_sort(acpi_table_mem_v1, acpi_table_index, ++ mem_map_v1_table, tmp_index, GRUB_EFI_LOONGSON_ACPI_TABLE); ++ tmp_index = grub_efi_loongson_memmap_v1_sort(acpi_nvs_mem_v1, acpi_nvs_index, ++ mem_map_v1_table, tmp_index, GRUB_EFI_LOONGSON_ACPI_NVS); + /*Reserve Sort*/ -+ grub_uint64_t loongarch_addr; -+ asm volatile ("csrrd %0, 0x181" : "=r" (loongarch_addr)); -+ if((loongarch_addr & 0xff00000000000000) == 0x9000000000000000){ -+ tmp_index = grub_efi_loongson_memmap_sort(reserve_mem, reserve_index, new_interface_mem, tmp_index, GRUB_EFI_LOONGSON_MEMORY_RESERVED); -+ }else{ -+ tmp_index = grub_efi_loongson_memmap_sort(reserve_mem, reserve_index, new_interface_mem, tmp_index, GRUB_EFI_LOONGSON_MEMORY_RESERVED + 1); -+ } -+ -+ new_interface_mem->mapcount = tmp_index; -+ new_interface_mem->header.checksum = 0; ++ { ++ grub_uint64_t loongarch_addr; ++ asm volatile ("csrrd %0, 0x181" : "=r" (loongarch_addr)); ++ if ((loongarch_addr & 0xff00000000000000) == 0x9000000000000000) ++ tmp_index = grub_efi_loongson_memmap_v1_sort(reserve_mem_v1, reserve_index, ++ mem_map_v1_table, tmp_index, ++ GRUB_EFI_LOONGSON_MEMORY_RESERVED); ++ else ++ tmp_index = grub_efi_loongson_memmap_v1_sort(reserve_mem_v1, reserve_index, ++ mem_map_v1_table, tmp_index, ++ GRUB_EFI_LOONGSON_MEMORY_RESERVED + 1); ++ } ++ mem_map_v1_table->mapcount = tmp_index; ++ mem_map_v1_table->header.checksum = 0; + -+ checksum = grub_efi_loongson_grub_calculatechecksum8(new_interface_mem, new_interface_mem->header.length); -+ new_interface_mem->header.checksum = checksum; ++ checksum = grub_efi_loongson_grub_calculatechecksum8((grub_uint8_t *)mem_map_v1_table, ++ mem_map_v1_table->header.length); ++ mem_map_v1_table->header.checksum = checksum; + } ++ ++ return GRUB_ERR_NONE; ++} ++ ++static grub_err_t ++grub_linux_boot (void) ++{ ++ struct grub_relocator64_state state; ++ ++ grub_memset (&state, 0, sizeof (state)); ++ ++ /* Boot the kernel. */ ++ state.gpr[1] = entry_addr; ++ grub_dprintf("loongson", "entry_addr is 0x%lx\n", state.gpr[1]); ++ ++ if (is_fdt_boot == 1) ++ { ++ if (allocate_fdt_and_exit_boot () != GRUB_ERR_NONE) ++ return grub_errno; ++ ++ state.gpr[4] = 1 << FLAGS_EFI_SUPPORT_BIT; ++ state.gpr[5] = (grub_uint64_t)fdt; ++ state.gpr[6] = 0; ++ } else { ++ state.gpr[4] = linux_argc; ++ state.gpr[5] = (grub_addr_t) linux_args_addr; ++ state.gpr[6] = (grub_uint64_t)grub_efi_loongson_get_boot_params(); ++ ++ if (allocate_boot_params_and_exit_boot () != GRUB_ERR_NONE) ++ return grub_errno; + } ++ grub_dprintf("loongson", "gpr[4]:%ld, gpr[5]:0x%lx, gpr[6]:0x%lx\n", ++ state.gpr[4], state.gpr[5], state.gpr[6]); + + state.jumpreg = 1; + grub_relocator64_boot (relocator, state); @@ -2108,67 +1680,42 @@ index 0000000..c769bb2 +{ + grub_relocator_unload (relocator); + grub_dl_unref (my_mod); -+ + loaded = 0; + + return GRUB_ERR_NONE; +} + +static grub_err_t -+grub_linux_load32 (grub_elf_t elf, const char *filename) -+{ -+ Elf32_Addr base; -+ grub_err_t err; -+ grub_uint8_t *playground; -+ -+ /* Linux's entry point incorrectly contains a virtual address. */ -+ entry_addr = elf->ehdr.ehdr32.e_entry; -+ -+ linux_size = grub_elf32_size (elf, &base, 0); -+ if (linux_size == 0) -+ return grub_errno; -+ target_addr = base; -+ linux_size = ALIGN_UP (base + linux_size - base, 8); -+ -+ relocator = grub_relocator_new (); -+ if (!relocator) -+ return grub_errno; -+ -+ { -+ grub_relocator_chunk_t ch; -+ err = grub_relocator_alloc_chunk_addr (relocator, &ch, -+ grub_vtop ((void *) target_addr), -+ linux_size); -+ if (err) -+ return err; -+ playground = get_virtual_current_address (ch); -+ } -+ -+ /* Now load the segments into the area we claimed. */ -+ return grub_elf32_load (elf, filename, playground - base, GRUB_ELF_LOAD_FLAGS_NONE, 0, 0); -+} -+ -+static grub_err_t +grub_linux_load64 (grub_elf_t elf, const char *filename) +{ + Elf64_Addr base; + grub_err_t err; + grub_uint8_t *playground; ++ grub_uint64_t addr; ++ int flag; + + /* Linux's entry point incorrectly contains a virtual address. */ + entry_addr = elf->ehdr.ehdr64.e_entry; -+ grub_dprintf("loongson", "entry address = %p\n", entry_addr); ++ grub_dprintf("loongson", "entry address = 0x%lx\n", entry_addr); + + linux_size = grub_elf64_size (elf, &base, 0); -+ grub_dprintf("loongson", "base = %p\n", base); ++ grub_dprintf("loongson", "base = 0x%lx\n", base); + + if (linux_size == 0) + return grub_errno; + target_addr = base; + linux_size = ALIGN_UP (base + linux_size - base, 8); + ++ asm volatile ("csrrd %0, 0x181" : "=r" (addr)); ++ if (addr & 0x1) { ++ flag = GRUB_ELF_LOAD_FLAGS_NONE; ++ } else { ++ flag = GRUB_ELF_LOAD_FLAGS_30BITS; ++ base &= ~ELF64_LOADMASK; ++ entry_addr &= ~ELF64_LOADMASK; ++ } ++ + relocator = grub_relocator_new (); -+ // linux_size=0x322fa80; + if (!relocator) + return grub_errno; + @@ -2183,7 +1730,7 @@ index 0000000..c769bb2 + } + + /* Now load the segments into the area we claimed. */ -+ return grub_elf64_load (elf, filename, playground - base, GRUB_ELF_LOAD_FLAGS_NONE, 0, 0); ++ return grub_elf64_load (elf, filename, playground - base, flag, 0, 0); +} + +static grub_err_t @@ -2191,20 +1738,21 @@ index 0000000..c769bb2 + int argc, char *argv[]) +{ + grub_elf_t elf = 0; -+ int size; -+ int i; -+ grub_uint64_t *linux_argv; -+ char *linux_args; + grub_err_t err; ++ int args_size = 0; ++ ++ grub_dl_ref (my_mod); + + if (argc == 0) ++ { + return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected")); ++ } + -+ elf = grub_elf_open (argv[0],GRUB_FILE_TYPE_LINUX_KERNEL); ++ elf = grub_elf_open (argv[0], GRUB_FILE_TYPE_LINUX_KERNEL); + if (! elf) + return grub_errno; + -+ if (elf->ehdr.ehdr32.e_type != ET_EXEC) ++ if (elf->ehdr.ehdr64.e_type != ET_EXEC) + { + grub_elf_close (elf); + return grub_error (GRUB_ERR_UNKNOWN_OS, @@ -2215,30 +1763,6 @@ index 0000000..c769bb2 + grub_loader_unset (); + loaded = 0; + -+ /* For arguments. */ -+ linux_argc = argc; -+ /* Main arguments. */ -+ size = (linux_argc) * sizeof (grub_uint64_t); -+ /* Initrd address and size. */ -+ size += 3 * sizeof (grub_uint64_t); -+ /* NULL terminator. */ -+ size += sizeof (grub_uint64_t); -+ /* First argument is always "a0". */ -+ size += ALIGN_UP (sizeof ("a0"), 4); -+ /* Normal arguments. */ -+ for (i = 1; i < argc; i++) -+ size += ALIGN_UP (grub_strlen (argv[i]) + 1, 4); -+ -+ /* rd arguments. */ -+ size += ALIGN_UP (sizeof ("rd_start=0xXXXXXXXXXXXXXXXX"), 4); -+ size += ALIGN_UP (sizeof ("rd_size=0xXXXXXXXXXXXXXXXX"), 4); -+ size += ALIGN_UP (sizeof ("initrd=0xXXXXXXXXXXXXXXXX,0xXXXXXXXXXXXXXXXX"), 4); -+ -+ size = ALIGN_UP (size, 8); -+ -+ if (grub_elf_is_elf32 (elf)) -+ err = grub_linux_load32 (elf, argv[0]); -+ else + if (grub_elf_is_elf64 (elf)) + err = grub_linux_load64 (elf, argv[0]); + else @@ -2249,26 +1773,71 @@ index 0000000..c769bb2 + if (err) + return err; + -+ { -+ grub_relocator_chunk_t ch; -+ err = grub_relocator_alloc_chunk_align (relocator, &ch, -+ 0, (0xffffffff - size) + 1, -+ size, 8, -+ GRUB_RELOCATOR_PREFERENCE_HIGH, 0); -+ if (err) -+ return err; -+ linux_args_addr = get_virtual_current_address (ch); -+ } ++ if (grub_efi_loongson_get_boot_params() == NULL) { ++ grub_size_t cmdline_size; + -+ linux_argv = (grub_uint64_t *) linux_args_addr; -+ linux_args = (char *) (linux_argv + (linux_argc + 1 + 3)); ++ is_fdt_boot = 1; ++ cmdline_size = grub_loader_cmdline_size (argc, argv) + sizeof (LINUX_IMAGE); ++ fdt_linux_args = grub_malloc (cmdline_size); ++ if (!fdt_linux_args) ++ { ++ grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory")); ++ goto fail; ++ } ++ grub_memcpy (fdt_linux_args, LINUX_IMAGE, sizeof (LINUX_IMAGE)); ++ err = grub_create_loader_cmdline (argc, argv, ++ fdt_linux_args + sizeof (LINUX_IMAGE) - 1, ++ cmdline_size, ++ GRUB_VERIFY_KERNEL_CMDLINE); ++ if (err) ++ goto fail; ++ grub_dprintf("loongson", "fdt linux args:%s\n", ++ fdt_linux_args + sizeof (LINUX_IMAGE) - 1); ++ ++ } else { ++ int i; ++ grub_uint64_t *linux_argv; ++ char *linux_args; ++ ++ is_fdt_boot = 0; ++ /* For arguments. */ ++ linux_argc = argc; ++ /* Main arguments. */ ++ args_size = (linux_argc) * sizeof (grub_uint64_t); ++ /* Initrd address/size and initrd */ ++ args_size += 3 * sizeof (grub_uint64_t); ++ /* NULL terminator. */ ++ args_size += sizeof (grub_uint64_t); ++ /* First argument is always "a0". */ ++ args_size += ALIGN_UP (sizeof ("a0"), 4); ++ /* Normal arguments. */ ++ for (i = 1; i < argc; i++) ++ args_size += ALIGN_UP (grub_strlen (argv[i]) + 1, 4); ++ ++ /* rd arguments. */ ++ args_size += ALIGN_UP (sizeof ("rd_start=0xXXXXXXXXXXXXXXXX"), 4); ++ args_size += ALIGN_UP (sizeof ("rd_size=0xXXXXXXXXXXXXXXXX"), 4); ++ args_size += ALIGN_UP (sizeof ("initrd=0xXXXXXXXXXXXXXXXX,0xXXXXXXXXXXXXXXXX"), 4); ++ ++ args_size = ALIGN_UP (args_size, 8); ++ ++ linux_args_addr = grub_efi_allocate_any_pages (GRUB_EFI_BYTES_TO_PAGES (args_size)); ++ grub_dprintf ("linux", "linux args numpages: %lld\n", ++ (long long) GRUB_EFI_BYTES_TO_PAGES (args_size)); ++ if (!linux_args_addr) ++ { ++ grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory")); ++ goto fail; ++ } ++ linux_argv = (grub_uint64_t *) linux_args_addr; ++ linux_args = (char *) (linux_argv + (linux_argc + 1 + 3)); + -+ grub_memcpy (linux_args, "a0", sizeof ("a0")); -+ *linux_argv = (grub_uint64_t) (grub_addr_t) linux_args; -+ linux_argv++; -+ linux_args += ALIGN_UP (sizeof ("a0"), 4); ++ grub_memcpy (linux_args, "a0", sizeof ("a0")); ++ *linux_argv = (grub_uint64_t) (grub_addr_t) linux_args; ++ linux_argv++; ++ linux_args += ALIGN_UP (sizeof ("a0"), 4); + -+ for (i = 1; i < argc; i++) ++ for (i = 1; i < argc; i++) + { + grub_memcpy (linux_args, argv[i], grub_strlen (argv[i]) + 1); + *linux_argv = (grub_uint64_t) (grub_addr_t) linux_args; @@ -2276,40 +1845,80 @@ index 0000000..c769bb2 + linux_args += ALIGN_UP (grub_strlen (argv[i]) + 1, 4); + } + -+ /* Reserve space for rd arguments. */ -+ rd_addr_arg_off = (grub_uint8_t *) linux_args - linux_args_addr; -+ linux_args += ALIGN_UP (sizeof ("rd_start=0xXXXXXXXXXXXXXXXX"), 4); -+ *linux_argv = 0; -+ linux_argv++; ++ /* Reserve space for rd arguments. */ ++ rd_addr_arg_off = (grub_uint8_t *) linux_args - linux_args_addr; ++ linux_args += ALIGN_UP (sizeof ("rd_start=0xXXXXXXXXXXXXXXXX"), 4); ++ *linux_argv = 0; ++ linux_argv++; ++ ++ rd_size_arg_off = (grub_uint8_t *) linux_args - linux_args_addr; ++ linux_args += ALIGN_UP (sizeof ("rd_size=0xXXXXXXXXXXXXXXXX"), 4); ++ *linux_argv = 0; ++ linux_argv++; + -+ rd_size_arg_off = (grub_uint8_t *) linux_args - linux_args_addr; -+ linux_args += ALIGN_UP (sizeof ("rd_size=0xXXXXXXXXXXXXXXXX"), 4); -+ *linux_argv = 0; -+ linux_argv++; ++ /* Reserve space for initrd arguments. */ ++ initrd_addr_arg_off = (grub_uint8_t *) linux_args - linux_args_addr; ++ linux_args += ALIGN_UP (sizeof ("initrd=0xXXXXXXXXXXXXXXXX,0xXXXXXXXXXXXXXXXX"), 4); ++ *linux_argv = 0; ++ linux_argv++; + -+ /* Reserve space for initrd arguments. */ -+ initrd_addr_arg_off = (grub_uint8_t *) linux_args - linux_args_addr; -+ linux_args += ALIGN_UP (sizeof ("initrd=0xXXXXXXXXXXXXXXXX,0xXXXXXXXXXXXXXXXX"), 4); -+ *linux_argv = 0; -+ linux_argv++; ++ *linux_argv = 0; ++ } + -+ *linux_argv = 0; ++ if (grub_errno == GRUB_ERR_NONE) ++ { ++ grub_loader_set (grub_linux_boot, grub_linux_unload, 0); ++ loaded = 1; ++ } + -+ grub_loader_set (grub_linux_boot, grub_linux_unload, 0); + initrd_loaded = 0; -+ loaded = 1; -+ grub_dl_ref (my_mod); + -+ return GRUB_ERR_NONE; ++fail: ++ if (grub_errno != GRUB_ERR_NONE) ++ { ++ grub_dl_unref (my_mod); ++ loaded = 0; ++ } ++ ++ if (fdt_linux_args && !loaded) ++ grub_free (fdt_linux_args); ++ ++ if (linux_args_addr && !loaded) ++ grub_efi_free_pages ((grub_addr_t) linux_args_addr, ++ GRUB_EFI_BYTES_TO_PAGES (args_size)); ++ ++ return grub_errno; ++} ++ ++#define INITRD_MAX_ADDRESS_OFFSET (32ULL * 1024 * 1024 * 1024) ++ ++/* ++ * This function returns a pointer to a legally allocated initrd buffer, ++ * or NULL if unsuccessful ++ */ ++static void * ++allocate_initrd_mem (int initrd_pages) ++{ ++ grub_addr_t max_addr; ++ ++ if (grub_efi_get_ram_base (&max_addr) != GRUB_ERR_NONE) ++ return NULL; ++ ++ max_addr += INITRD_MAX_ADDRESS_OFFSET - 1; ++ ++ return grub_efi_allocate_pages_real (max_addr, initrd_pages, ++ GRUB_EFI_ALLOCATE_MAX_ADDRESS, ++ GRUB_EFI_LOADER_DATA); +} + ++ +static grub_err_t +grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)), + int argc, char *argv[]) +{ -+ grub_size_t size = 0; -+ void *initrd_dest; ++ grub_size_t initrd_size, initrd_pages; + grub_err_t err; ++ void *initrd_mem = NULL; + struct grub_linux_initrd_context initrd_ctx = { 0, 0, 0 }; + + if (argc == 0) @@ -2324,49 +1933,71 @@ index 0000000..c769bb2 + if (grub_initrd_init (argc, argv, &initrd_ctx)) + goto fail; + -+ size = grub_get_initrd_size (&initrd_ctx); ++ initrd_size = grub_get_initrd_size (&initrd_ctx); + ++ if (is_fdt_boot == 1) //fdt + { ++ initrd_pages = (GRUB_EFI_BYTES_TO_PAGES (initrd_size)); ++ initrd_mem = allocate_initrd_mem (initrd_pages); ++ if (!initrd_mem) ++ { ++ grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory")); ++ goto fail; ++ } ++ } else { + grub_relocator_chunk_t ch; + err = grub_relocator_alloc_chunk_align (relocator, &ch, -+ 0, (0xffffffff - size) + 1, -+ size, 0x10000, -+ GRUB_RELOCATOR_PREFERENCE_HIGH, 0); ++ 0, (0xffffffff - initrd_size) + 1, ++ initrd_size, 0x10000, ++ GRUB_RELOCATOR_PREFERENCE_HIGH, 0); + + if (err) -+ goto fail; -+ initrd_dest = get_virtual_current_address (ch); ++ return err; ++ initrd_mem = get_virtual_current_address (ch); + } + -+ if (grub_initrd_load (&initrd_ctx, argv, initrd_dest)) ++ if (grub_initrd_load (&initrd_ctx, argv, initrd_mem)) + goto fail; + -+ grub_snprintf ((char *) linux_args_addr + rd_addr_arg_off, -+ sizeof ("rd_start=0xXXXXXXXXXXXXXXXX"), "rd_start=0x%lx", -+ (grub_uint64_t) initrd_dest); -+ ((grub_uint64_t *) linux_args_addr)[linux_argc] -+ = (grub_uint64_t) ((grub_addr_t) linux_args_addr + rd_addr_arg_off); -+ linux_argc++; -+ -+ grub_snprintf ((char *) linux_args_addr + rd_size_arg_off, -+ sizeof ("rd_size=0xXXXXXXXXXXXXXXXXX"), "rd_size=0x%lx", -+ (grub_uint64_t) size); -+ ((grub_uint64_t *) linux_args_addr)[linux_argc] -+ = (grub_uint64_t) ((grub_addr_t) linux_args_addr + rd_size_arg_off); -+ linux_argc++; -+ ++ initrd_start = (grub_addr_t) initrd_mem; ++ initrd_end = initrd_start + initrd_size; ++ grub_dprintf ("linux", "[addr=%p, size=0x%lx]\n", ++ (void *) initrd_start, initrd_size); + -+ grub_snprintf ((char *) linux_args_addr + initrd_addr_arg_off, -+ sizeof ("initrd=0xXXXXXXXXXXXXXXXX,0xXXXXXXXXXXXXXXXX"), "initrd=0x%lx,0x%lx", -+ ((grub_uint64_t) initrd_dest & 0xffffffff), (grub_uint64_t) size); -+ ((grub_uint64_t *) linux_args_addr)[linux_argc] -+ = (grub_uint64_t) ((grub_addr_t) linux_args_addr + initrd_addr_arg_off); -+ linux_argc++; ++ if (is_fdt_boot == 0) //bpi ++ { ++ grub_snprintf ((char *) linux_args_addr + rd_addr_arg_off, ++ sizeof ("rd_start=0xXXXXXXXXXXXXXXXX"), ++ "rd_start=0x%lx", ++ (grub_uint64_t) initrd_mem); ++ ((grub_uint64_t *) linux_args_addr)[linux_argc] ++ = (grub_uint64_t) ((grub_addr_t) linux_args_addr + rd_addr_arg_off); ++ linux_argc++; ++ ++ grub_snprintf ((char *) linux_args_addr + rd_size_arg_off, ++ sizeof ("rd_size=0xXXXXXXXXXXXXXXXXX"), "rd_size=0x%lx", ++ (grub_uint64_t) initrd_size); ++ ((grub_uint64_t *) linux_args_addr)[linux_argc] ++ = (grub_uint64_t) ((grub_addr_t) linux_args_addr + rd_size_arg_off); ++ linux_argc++; ++ ++ grub_snprintf ((char *) linux_args_addr + initrd_addr_arg_off, ++ sizeof ("initrd=0xXXXXXXXXXXXXXXXX,0xXXXXXXXXXXXXXXXX"), ++ "initrd=0x%lx,0x%lx", ++ ((grub_uint64_t) initrd_mem & GRUB_EFI_MAX_PHY_ADDRESS), ++ (grub_uint64_t) initrd_size); ++ ((grub_uint64_t *) linux_args_addr)[linux_argc] ++ = (grub_uint64_t) ((grub_addr_t) linux_args_addr + initrd_addr_arg_off); ++ linux_argc++; ++ } + + initrd_loaded = 1; + + fail: + grub_initrd_close (&initrd_ctx); ++ if (is_fdt_boot == 1) ++ if (initrd_mem && !initrd_start) ++ grub_efi_free_pages ((grub_addr_t) initrd_mem, initrd_pages); + + return grub_errno; +} @@ -2388,10 +2019,10 @@ index 0000000..c769bb2 + grub_unregister_command (cmd_initrd); +} diff --git a/include/grub/dl.h b/include/grub/dl.h -index 6a3e251..c1b6dd9 100644 +index 6f46b7e..50e04ac 100644 --- a/include/grub/dl.h +++ b/include/grub/dl.h -@@ -314,7 +314,7 @@ grub_arch_dl_get_tramp_got_size (const void *ehdr, grub_size_t *tramp, +@@ -360,7 +360,7 @@ grub_arch_dl_get_tramp_got_size (const void *ehdr, grub_size_t *tramp, #define GRUB_ARCH_DL_GOT_ALIGN 4 #endif @@ -2401,11 +2032,11 @@ index 6a3e251..c1b6dd9 100644 #define GRUB_ARCH_DL_GOT_ALIGN 8 #endif diff --git a/include/grub/efi/api.h b/include/grub/efi/api.h -index 37e7b16..049326c 100644 +index 510a403..a57187f 100644 --- a/include/grub/efi/api.h +++ b/include/grub/efi/api.h -@@ -2087,7 +2087,8 @@ struct grub_efi_rng_protocol - typedef struct grub_efi_rng_protocol grub_efi_rng_protocol_t; +@@ -2117,7 +2117,8 @@ struct grub_efi_memory_attribute_protocol + typedef struct grub_efi_memory_attribute_protocol grub_efi_memory_attribute_protocol_t; #if (GRUB_TARGET_SIZEOF_VOID_P == 4) || defined (__ia64__) \ - || defined (__aarch64__) || defined (__MINGW64__) || defined (__CYGWIN__) @@ -2414,11 +2045,24 @@ index 37e7b16..049326c 100644 #define efi_call_0(func) func() #define efi_call_1(func, a) func(a) +diff --git a/include/grub/efi/efi.h b/include/grub/efi/efi.h +index 8ca8c38..fe1df9d 100644 +--- a/include/grub/efi/efi.h ++++ b/include/grub/efi/efi.h +@@ -138,7 +138,7 @@ extern void (*EXPORT_VAR(grub_efi_net_config)) (grub_efi_handle_t hnd, + extern grub_addr_t EXPORT_VAR(grub_stack_addr); + extern grub_size_t EXPORT_VAR(grub_stack_size); + +-#if defined(__arm__) || defined(__aarch64__) ++#if defined(__arm__) || defined(__aarch64__) || defined (__loongarch__) + void *EXPORT_FUNC(grub_efi_get_firmware_fdt)(void); + grub_err_t EXPORT_FUNC(grub_efi_get_ram_base)(grub_addr_t *); + #include diff --git a/include/grub/efi/pe32.h b/include/grub/efi/pe32.h -index c03cc59..be88c54 100644 +index 45c9f8b..f612a55 100644 --- a/include/grub/efi/pe32.h +++ b/include/grub/efi/pe32.h -@@ -66,6 +66,7 @@ struct grub_pe32_coff_header +@@ -89,6 +89,7 @@ struct grub_pe32_coff_header }; #define GRUB_PE32_MACHINE_I386 0x14c @@ -2426,7 +2070,7 @@ index c03cc59..be88c54 100644 #define GRUB_PE32_MACHINE_IA64 0x200 #define GRUB_PE32_MACHINE_X86_64 0x8664 #define GRUB_PE32_MACHINE_ARMTHUMB_MIXED 0x01c2 -@@ -329,6 +330,7 @@ struct grub_pe32_fixup_block +@@ -355,6 +356,7 @@ struct grub_pe32_fixup_block #define GRUB_PE32_REL_BASED_IA64_IMM64 9 #define GRUB_PE32_REL_BASED_DIR64 10 #define GRUB_PE32_REL_BASED_HIGH3ADJ 11 @@ -2507,6 +2151,26 @@ index c8492f9..2c0b163 100644 /* MIPS relocs. */ #define R_MIPS_NONE 0 /* No reloc */ +diff --git a/include/grub/fdt.h b/include/grub/fdt.h +index 6ee57e1..a0710ab 100644 +--- a/include/grub/fdt.h ++++ b/include/grub/fdt.h +@@ -19,11 +19,14 @@ + #ifndef GRUB_FDT_HEADER + #define GRUB_FDT_HEADER 1 + +-#if defined(__arm__) || defined(__aarch64__) ++#if defined(__arm__) || defined(__aarch64__) || defined (__loongarch__) + + #include + #include + ++/* Space required when preparing the /chosen node after boot has been called. */ ++#define GRUB_EFI_LINUX_FDT_EXTRA_SPACE 0x400 ++ + #define FDT_MAGIC 0xD00DFEED + + typedef struct { diff --git a/include/grub/loongarch64/asm.h b/include/grub/loongarch64/asm.h new file mode 100644 index 0000000..c3e77e9 @@ -2523,46 +2187,12 @@ index 0000000..c3e77e9 +#define GRUB_ASM_REG_L ld.d + +#endif -diff --git a/include/grub/loongarch64/efi/boot.h b/include/grub/loongarch64/efi/boot.h -new file mode 100644 -index 0000000..e69de29 -diff --git a/include/grub/loongarch64/efi/loader.h b/include/grub/loongarch64/efi/loader.h -new file mode 100644 -index 0000000..71a0159 ---- /dev/null -+++ b/include/grub/loongarch64/efi/loader.h -@@ -0,0 +1,25 @@ -+/* -+ * GRUB -- GRand Unified Bootloader -+ * Copyright (C) 2002,2003,2004,2006,2007,2017 Free Software Foundation, Inc. -+ * -+ * GRUB is free software: you can redistribute it and/or modify -+ * it under the terms of the GNU General Public License as published by -+ * the Free Software Foundation, either version 3 of the License, or -+ * (at your option) any later version. -+ * -+ * GRUB is distributed in the hope that it will be useful, -+ * but WITHOUT ANY WARRANTY; without even the implied warranty of -+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -+ * GNU General Public License for more details. -+ * -+ * You should have received a copy of the GNU General Public License -+ * along with GRUB. If not, see . -+ */ -+ -+#ifndef GRUB_LOADER_MACHINE_HEADER -+#define GRUB_LOADER_MACHINE_HEADER 1 -+ -+#include -+#include -+ -+#endif /* ! GRUB_LOADER_MACHINE_HEADER */ diff --git a/include/grub/loongarch64/efi/loongson.h b/include/grub/loongarch64/efi/loongson.h new file mode 100644 -index 0000000..fa32ef5 +index 0000000..7a9ccb4 --- /dev/null +++ b/include/grub/loongarch64/efi/loongson.h -@@ -0,0 +1,290 @@ +@@ -0,0 +1,113 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2017 Free Software Foundation, Inc. @@ -2588,12 +2218,17 @@ index 0000000..fa32ef5 + +#include + -+#define GRUB_EFI_LOONGSON_SMBIOS_TABLE_GUID \ ++#define GRUB_EFI_LOONGSON_BPI_TABLE_GUID \ + { 0x4660f721, 0x2ec5, 0x416a, \ + { 0x89, 0x9a, 0x43, 0x18, 0x02, 0x50, 0xa0, 0xc9 } \ + } + ++#define GRUB_EFI_MAX_PHY_ADDRESS 0xffffffffffffULL ++#define ELF32_LOADMASK (0xf0000000UL) ++#define ELF64_LOADMASK (0xf000000000000000ULL) ++#define FLAGS_EFI_SUPPORT_BIT 0 +#define GRUB_EFI_LOONGSON_MMAP_MAX 128 ++ +typedef enum + { + GRUB_EFI_LOONGSON_SYSTEM_RAM = 1, @@ -2604,272 +2239,88 @@ index 0000000..fa32ef5 + } +grub_efi_loongson_memory_type; + -+typedef struct -+{ -+ grub_uint16_t vers; /* version */ -+ grub_uint32_t nr_map; /* number of memory_maps */ -+ grub_uint32_t mem_freq; /* memory frequence */ -+ struct mem_map { -+ grub_uint32_t node_id; /* node_id which memory attached to */ -+ grub_uint32_t mem_type; /* system memory, pci memory, pci io, etc. */ -+ grub_uint64_t mem_start; /* memory map start address */ -+ grub_uint32_t mem_size; /* each memory_map size, not the total size */ -+ } map[GRUB_EFI_LOONGSON_MMAP_MAX]; -+} GRUB_PACKED -+grub_efi_loongson_memory_map; -+ -+/* -+ * Capability and feature descriptor structure for LOONGARCH CPU -+ */ -+typedef struct -+{ -+ grub_uint16_t vers; /* version */ -+ grub_uint32_t processor_id; /* PRID, e.g. 6305, 6306 */ -+ grub_uint32_t cputype; /* Loongson_3A/3B, etc. */ -+ grub_uint32_t total_node; /* num of total numa nodes */ -+ grub_uint16_t cpu_startup_core_id; /* Boot core id */ -+ grub_uint16_t reserved_cores_mask; -+ grub_uint32_t cpu_clock_freq; /* cpu_clock */ -+ grub_uint32_t nr_cpus; -+} GRUB_PACKED -+grub_efi_loongson_cpu_info; -+ -+#define GRUB_EFI_LOONGSON_MAX_UARTS 64 -+ -+typedef struct -+{ -+ grub_uint32_t iotype; /* see include/linux/serial_core.h */ -+ grub_uint32_t uartclk; -+ grub_uint32_t int_offset; -+ grub_uint64_t uart_base; -+} GRUB_PACKED -+grub_efi_loongson_uart_device; -+ -+#define GRUB_EFI_LOONGSON_MAX_SENSORS 64 -+ -+typedef struct -+{ -+ char name[32]; /* a formal name */ -+ char label[64]; /* a flexible description */ -+ grub_uint32_t type; /* SENSOR_* */ -+ grub_uint32_t id; /* instance id of a sensor-class */ -+ grub_uint32_t fan_policy; -+ grub_uint32_t fan_percent; /* only for constant speed policy */ -+ grub_uint64_t base_addr; /* base address of device registers */ -+} GRUB_PACKED -+grub_efi_loongson_sensor_device; -+ -+typedef struct -+{ -+ grub_uint16_t vers; /* version */ -+ grub_uint32_t ccnuma_smp; /* 0: no numa; 1: has numa */ -+ grub_uint32_t sing_double_channel; /* 1:single; 2:double */ -+ grub_uint32_t nr_uarts; -+ grub_efi_loongson_uart_device uarts[GRUB_EFI_LOONGSON_MAX_UARTS]; -+ grub_uint32_t nr_sensors; -+ grub_efi_loongson_sensor_device sensors[GRUB_EFI_LOONGSON_MAX_SENSORS]; -+ char has_ec; -+ char ec_name[32]; -+ grub_uint64_t ec_base_addr; -+ char has_tcm; -+ char tcm_name[32]; -+ grub_uint64_t tcm_base_addr; -+ grub_uint64_t workarounds; /* see workarounds.h */ -+} GRUB_PACKED -+grub_efi_loongson_system_info; -+ -+typedef struct -+{ -+ grub_uint16_t vers; -+ grub_uint16_t size; -+ grub_uint16_t rtr_bus; -+ grub_uint16_t rtr_devfn; -+ grub_uint32_t vendor; -+ grub_uint32_t device; -+ grub_uint32_t PIC_type; /* conform use HT or PCI to route to CPU-PIC */ -+ grub_uint64_t ht_int_bit; /* 3A: 1<<24; 3B: 1<<16 */ -+ grub_uint64_t ht_enable; /* irqs used in this PIC */ -+ grub_uint32_t node_id; /* node id: 0x0-0; 0x1-1; 0x10-2; 0x11-3 */ -+ grub_uint64_t pci_mem_start_addr; -+ grub_uint64_t pci_mem_end_addr; -+ grub_uint64_t pci_io_start_addr; -+ grub_uint64_t pci_io_end_addr; -+ grub_uint64_t pci_config_addr; -+ grub_uint32_t dma_mask_bits; -+} GRUB_PACKED -+grub_efi_loongson_irq_src_routing_table; -+ -+typedef struct -+{ -+ grub_uint16_t vers; /* version */ -+ grub_uint16_t size; -+ grub_uint8_t flag; -+ char description[64]; -+} GRUB_PACKED -+grub_efi_loongson_interface_info; -+ -+#define GRUB_EFI_LOONGSON_MAX_RESOURCE_NUMBER 128 -+ -+typedef struct -+{ -+ grub_uint64_t start; /* resource start address */ -+ grub_uint64_t end; /* resource end address */ -+ char name[64]; -+ grub_uint32_t flags; -+} -+grub_efi_loongson_resource; -+ -+/* arch specific additions */ -+typedef struct -+{ -+} -+grub_efi_loongson_archdev_data; -+ -+typedef struct -+{ -+ char name[64]; /* hold the device name */ -+ grub_uint32_t num_resources; /* number of device_resource */ -+ /* for each device's resource */ -+ grub_efi_loongson_resource resource[GRUB_EFI_LOONGSON_MAX_RESOURCE_NUMBER]; -+ /* arch specific additions */ -+ grub_efi_loongson_archdev_data archdata; -+} -+grub_efi_loongson_board_devices; -+ -+typedef struct -+{ -+ grub_uint16_t vers; /* version */ -+ char special_name[64]; /* special_atribute_name */ -+ grub_uint32_t loongson_special_type; /* type of special device */ -+ /* for each device's resource */ -+ grub_efi_loongson_resource resource[GRUB_EFI_LOONGSON_MAX_RESOURCE_NUMBER]; -+} -+grub_efi_loongson_special_attribute; -+ -+typedef struct -+{ -+ grub_uint64_t memory_offset; /* efi_loongson_memory_map struct offset */ -+ grub_uint64_t cpu_offset; /* efi_loongson_cpuinfo struct offset */ -+ grub_uint64_t system_offset; /* efi_loongson_system_info struct offset */ -+ grub_uint64_t irq_offset; /* efi_loongson_irq_src_routing_table struct offset */ -+ grub_uint64_t interface_offset; /* interface_info struct offset */ -+ grub_uint64_t special_offset; /* efi_loongson_special_attribute struct offset */ -+ grub_uint64_t boarddev_table_offset; /* efi_loongson_board_devices offset */ -+} -+grub_efi_loongson_params; -+ -+typedef struct -+{ -+ grub_uint16_t vers; /* version */ -+ grub_uint64_t vga_bios; /* vga_bios address */ -+ grub_efi_loongson_params lp; -+} -+grub_efi_loongson_smbios_table; -+ -+typedef struct -+{ -+ grub_uint64_t reset_cold; -+ grub_uint64_t reset_warm; -+ grub_uint64_t reset_type; -+ grub_uint64_t shutdown; -+ grub_uint64_t do_suspend; /* NULL if not support */ -+} -+grub_efi_loongson_reset_system; -+ -+typedef struct -+{ -+ grub_uint64_t mps; /* MPS table */ -+ grub_uint64_t acpi; /* ACPI table (IA64 ext 0.71) */ -+ grub_uint64_t acpi20; /* ACPI table (ACPI 2.0) */ -+ grub_efi_loongson_smbios_table smbios; /* SM BIOS table */ -+ grub_uint64_t sal_systab; /* SAL system table */ -+ grub_uint64_t boot_info; /* boot info table */ -+} -+grub_efi_loongson; -+ -+typedef struct -+{ -+ grub_efi_loongson efi; -+ grub_efi_loongson_reset_system reset_system; -+} -+grub_efi_loongson_boot_params; -+ -+extern grub_uint64_t grub_efi_loongson_reset_system_addr; -+ -+extern void grub_efi_loongson_reset_cold (void); -+extern void grub_efi_loongson_reset_warm (void); -+extern void grub_efi_loongson_reset_shutdown (void); -+extern void grub_efi_loongson_reset_suspend (void); -+ -+void grub_efi_loongson_alloc_boot_params (void); -+void grub_efi_loongson_free_boot_params (void); -+void * grub_efi_loongson_get_smbios_table (void); -+ -+int EXPORT_FUNC(grub_efi_is_loongson) (void); ++typedef enum ++ { ++ GRUB_EFI_BPI_VER_NONE = 0, ++ GRUB_EFI_BPI_VER_V1 = 1000, ++ GRUB_EFI_BPI_VER_V2 = 1001, ++ } ++ grub_efi_loongson_bpi_version; + +grub_uint8_t -+EXPORT_FUNC(grub_efi_loongson_calculatesum8) (const grub_uint8_t *Buffer, grub_efi_uintn_t Length); ++EXPORT_FUNC(grub_efi_loongson_calculatesum8) (const grub_uint8_t *Buffer, ++ grub_efi_uintn_t Length); + +grub_uint8_t -+EXPORT_FUNC(grub_efi_loongson_grub_calculatechecksum8) (const grub_uint8_t *Buffer, grub_efi_uintn_t Length); ++EXPORT_FUNC(grub_efi_loongson_grub_calculatechecksum8) (const grub_uint8_t *Buffer, ++ grub_efi_uintn_t Length); + ++unsigned long ++EXPORT_FUNC(grub_efi_get_bpi_version) (const char *str); + +void * +EXPORT_FUNC(grub_efi_loongson_get_boot_params) (void); + -+typedef struct _extention_list_hdr{ ++typedef struct _extention_list_hdr { + grub_uint64_t signature; + grub_uint32_t length; + grub_uint8_t revision; + grub_uint8_t checksum; -+ struct _extention_list_hdr *next; ++ union { ++ struct _extention_list_hdr *next; ++ grub_uint64_t next_offset; ++ }; +}GRUB_PACKED +ext_list; + -+typedef struct bootparamsinterface { -+ grub_uint64_t signature; //{'B', 'P', 'I', '_', '0', '_', '1'} ++typedef struct boot_params_interface { ++ grub_uint64_t signature; //{'B', 'P', 'I', 'x', 'x', 'x', 'x', 'x'} + grub_efi_system_table_t *systemtable; -+ ext_list *extlist; ++ union { ++ ext_list *extlist; ++ grub_uint64_t extlist_offset; ++ }; ++ grub_uint64_t flags; +}GRUB_PACKED -+bootparamsinterface; ++boot_params_interface; + +typedef struct { -+ ext_list header; // {'M', 'E', 'M'} ++ ext_list header; //{MEM} + grub_uint8_t mapcount; -+ struct GRUB_PACKED memmap { ++ struct GRUB_PACKED memmap_v1 { + grub_uint32_t memtype; + grub_uint64_t memstart; + grub_uint64_t memsize; + } map[GRUB_EFI_LOONGSON_MMAP_MAX]; +}GRUB_PACKED -+mem_map; ++mem_map_v1; + +typedef struct { -+ ext_list header; // {VBIOS} ++ ext_list header; //{VBIOS} + grub_uint64_t vbiosaddr; +}GRUB_PACKED +vbios; + -+grub_uint32_t -+EXPORT_FUNC (grub_efi_loongson_memmap_sort) (struct memmap array[], grub_uint32_t length, mem_map * bpmem, grub_uint32_t index, grub_uint32_t memtype); ++grub_uint32_t ++EXPORT_FUNC (grub_efi_loongson_memmap_v1_sort) (struct memmap_v1 array[], ++ grub_uint32_t length, mem_map_v1 * mem, ++ grub_uint32_t index, grub_uint32_t memtype); +#endif /* ! GRUB_EFI_LOONGSON_HEADER */ diff --git a/include/grub/loongarch64/efi/memory.h b/include/grub/loongarch64/efi/memory.h new file mode 100644 -index 0000000..b8556c7 +index 0000000..73641a1 --- /dev/null +++ b/include/grub/loongarch64/efi/memory.h -@@ -0,0 +1,14 @@ +@@ -0,0 +1,12 @@ +#ifndef GRUB_MEMORY_CPU_HEADER +#include + -+#define GRUB_EFI_MAX_USABLE_ADDRESS 0x9800000fffffffffUL -+#define GRUB_EFI_MAX_ALLOCATION_ADDRESS GRUB_EFI_MAX_USABLE_ADDRESS -+ -+static inline grub_uint64_t grub_efi_max_usable_address(void) ++static inline grub_uint64_t ++grub_efi_max_usable_address(void) +{ -+ grub_uint64_t addr; -+ asm volatile ("csrrd %0, 0x181" : "=r" (addr)); -+ return addr |= 0xffffffffffUL; ++ grub_uint64_t addr; ++ asm volatile ("csrrd %0, 0x181" : "=r" (addr)); ++ return addr |= 0xffffffffffUL; +} + +#endif /* ! GRUB_MEMORY_CPU_HEADER */ @@ -2944,15 +2395,15 @@ index 0000000..5f34103 +} + +#endif /* _SYS_IO_H */ -diff --git a/include/grub/loongarch64/kernel.h b/include/grub/loongarch64/kernel.h +diff --git a/include/grub/loongarch64/linux.h b/include/grub/loongarch64/linux.h new file mode 100644 -index 0000000..909d539 +index 0000000..3c6cf65 --- /dev/null -+++ b/include/grub/loongarch64/kernel.h -@@ -0,0 +1,24 @@ ++++ b/include/grub/loongarch64/linux.h +@@ -0,0 +1,74 @@ +/* + * GRUB -- GRand Unified Bootloader -+ * Copyright (C) 2005,2006,2007,2008,2009,2017 Free Software Foundation, Inc. ++ * Copyright (C) 2021 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by @@ -2968,38 +2419,60 @@ index 0000000..909d539 + * along with GRUB. If not, see . + */ + -+#ifndef GRUB_KERNEL_CPU_HEADER -+#define GRUB_KERNEL_CPU_HEADER 1 ++#ifndef GRUB_LOONGARCH64_LINUX_HEADER ++#define GRUB_LOONGARCH64_LINUX_HEADER 1 + -+#include ++#include + -+#endif /* ! GRUB_KERNEL_MACHINE_HEADER */ -diff --git a/include/grub/loongarch64/linux.h b/include/grub/loongarch64/linux.h -new file mode 100644 -index 0000000..cbf8775 ---- /dev/null -+++ b/include/grub/loongarch64/linux.h -@@ -0,0 +1,22 @@ -+/* -+ * GRUB -- GRand Unified Bootloader -+ * Copyright (C) 2013 Free Software Foundation, Inc. -+ * -+ * GRUB is free software: you can redistribute it and/or modify -+ * it under the terms of the GNU General Public License as published by -+ * the Free Software Foundation, either version 3 of the License, or -+ * (at your option) any later version. -+ * -+ * GRUB is distributed in the hope that it will be useful, -+ * but WITHOUT ANY WARRANTY; without even the implied warranty of -+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -+ * GNU General Public License for more details. ++/* LoongArch linux kernel type */ ++#define GRUB_LOONGARCH_LINUX_BAD 0 ++#define GRUB_LOONGARCH_LINUX_ELF 1 ++#define GRUB_LOONGARCH_LINUX_EFI 2 ++ ++#define GRUB_LOONGSON3_BOOT_MEM_MAP_MAX 128 ++ ++#define GRUB_LINUX_LOONGARCH_MAGIC_SIGNATURE 0x4C6F6F6E67417263 /* 'LoongArc' */ ++#define GRUB_LINUX_LOONGARCH_MAGIC_SIGNATURE2 0x68 /* 'h' */ ++#define linux_arch_kernel_header linux_loongarch64_kernel_header ++ ++/* From linux/Documentation/loongarch/booting.txt + * -+ * You should have received a copy of the GNU General Public License -+ * along with GRUB. If not, see . ++ * 0-1: MZ ++ * 0x28: LoongArch\0 ++ * 0x3c: PE/COFF头偏移 ++ * 0x20e:内核版本号偏移-512 ++ * riscv的version字段在0x20偏移处,现在LoongArch没有使用,是0 + */ ++struct linux_loongarch64_kernel_header ++{ ++ grub_uint32_t code0; /* Executable code */ ++ grub_uint32_t code1; /* Executable code */ ++ grub_uint64_t text_offset; /* Image load offset */ ++ grub_uint64_t res0; /* reserved */ ++ grub_uint64_t res1; /* reserved */ ++ grub_uint64_t res2; /* reserved */ ++ grub_uint64_t magic; /* Magic number, little endian, "LoongArc" */ ++ grub_uint32_t magic1; /* Magic number, little endian, "h" */ ++ grub_uint64_t res3; /* reserved */ ++ grub_uint32_t hdr_offset; /* Offset of PE/COFF header */ ++}; + -+#ifndef GRUB_LOONGARCH64_LINUX_HEADER -+#define GRUB_LOONGARCH64_LINUX_HEADER 1 ++struct linux_loongarch64_kernel_params ++{ ++ grub_addr_t kernel_addr; /* kernel entry address */ ++ grub_size_t kernel_size; /* kernel size */ ++ grub_addr_t ramdisk_addr; /* initrd load address */ ++ grub_size_t ramdisk_size; /* initrd size */ ++ int linux_argc; ++ grub_addr_t linux_argv; ++ void* linux_args; ++}; ++ ++#include ++#include ++ ++#define ELF32_LOADMASK (0xf0000000UL) ++#define ELF64_LOADMASK (0xf000000000000000ULL) + +#endif /* ! GRUB_LOONGARCH64_LINUX_HEADER */ diff --git a/include/grub/loongarch64/loongarch64.h b/include/grub/loongarch64/loongarch64.h @@ -3040,10 +2513,10 @@ index 0000000..ea3be3d +#endif diff --git a/include/grub/loongarch64/memory.h b/include/grub/loongarch64/memory.h new file mode 100644 -index 0000000..03398b3 +index 0000000..8281dab --- /dev/null +++ b/include/grub/loongarch64/memory.h -@@ -0,0 +1,57 @@ +@@ -0,0 +1,60 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2017 Free Software Foundation, Inc. @@ -3084,12 +2557,15 @@ index 0000000..03398b3 +} + +static inline void * -+grub_map_memory (grub_phys_addr_t a, grub_size_t size) ++grub_map_memory (grub_phys_addr_t a, ++ grub_size_t size __attribute__ ((unused))) +{ + grub_uint64_t addr; -+ + asm volatile ("csrrd %0, 0x181" : "=r" (addr)); -+ return (void *) (a | (addr & 0xffffffffffffff00UL)); ++ if (addr & 0x1) ++ return (void *) (a | (addr & 0xffffffffffffff00UL)); ++ else ++ return (void *) a; +} + +static inline void @@ -3937,7 +3413,7 @@ index 03ba1ab..4a9943a 100644 struct platform_whitelist { diff --git a/util/mkimage.c b/util/mkimage.c -index 16418e2..0d39c07 100644 +index c770259..250ca81 100644 --- a/util/mkimage.c +++ b/util/mkimage.c @@ -609,6 +609,22 @@ static const struct grub_install_image_target_desc image_targets[] = @@ -3964,5 +3440,5 @@ index 16418e2..0d39c07 100644 #include -- -2.27.0 +2.1.0 diff --git a/grub.patches b/grub.patches index 1ccb15d..a82a1a9 100644 --- a/grub.patches +++ b/grub.patches @@ -666,3 +666,4 @@ Patch0666: 0666-ieee1275-ofnet-Fix-grub_malloc-removed-after-added-s.patch # Support loongarch64 #Patch1000: 1000-loongarch64-add-support.patch Patch1001: 1001-bls-make-list.patch +Patch1002: 1002-Add-LoongArch64-support-and-update-interface-to-v40.patch diff --git a/grub2.spec b/grub2.spec index 42d281d..526688e 100644 --- a/grub2.spec +++ b/grub2.spec @@ -540,6 +540,9 @@ fi - Build pc-modules package on x86_64 (geliwei@openanolis.org) - Add loongarch64 base support (zhangwenlong@loongson.cn)(chenguoqi@loongson.cn) - Fix a bug in bls_make_list, blscfg. (zhonglingh@linux.alibaba.com) +- Delete LoongArch64 support old version due to compile error(yangqiming@loongson.cn) +- Add LoongArch64 support(yangqiming@loongson.cn) +- LoongArch64 support fdt and phy-addr BIOS(yangqiming@loongson.cn) * Tue Mar 25 2025 Nicolas Frayer - 2.02-162 - ieee1275/ofnet: Fix grub_malloc() removed after added safe -- Gitee From 3e5f85e8240a3a2275ff3201dbafba922d4a8a39 Mon Sep 17 00:00:00 2001 From: Liwei Ge Date: Thu, 18 Aug 2022 16:47:48 +0800 Subject: [PATCH 6/8] kernel-install: remove dtb dir with correct rm argument --- 20-grub.install | 2 +- grub2.spec | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/20-grub.install b/20-grub.install index 0ecf1e2..40a437c 100755 --- a/20-grub.install +++ b/20-grub.install @@ -62,7 +62,7 @@ case "$COMMAND" in "$KERNEL_DIR"/dtb do [[ -e "$i" ]] || continue - rm -f "/boot/${i##*/}-${KERNEL_VERSION}" + rm -rf "/boot/${i##*/}-${KERNEL_VERSION}" cp -aT "$i" "/boot/${i##*/}-${KERNEL_VERSION}" command -v restorecon &>/dev/null && \ restorecon -R "/boot/${i##*/}-${KERNEL_VERSION}" diff --git a/grub2.spec b/grub2.spec index 526688e..7dcf64a 100644 --- a/grub2.spec +++ b/grub2.spec @@ -543,6 +543,7 @@ fi - Delete LoongArch64 support old version due to compile error(yangqiming@loongson.cn) - Add LoongArch64 support(yangqiming@loongson.cn) - LoongArch64 support fdt and phy-addr BIOS(yangqiming@loongson.cn) +- Remove dtb dir with correct argument (Liwei Ge) * Tue Mar 25 2025 Nicolas Frayer - 2.02-162 - ieee1275/ofnet: Fix grub_malloc() removed after added safe -- Gitee From 0e02cb202289490392d48e02774a720bdf0f41ac Mon Sep 17 00:00:00 2001 From: hanliyang Date: Thu, 2 Jan 2025 09:36:13 +0000 Subject: [PATCH 7/8] [Feature]Hygon: Support use confidential computing provisioned secrets for disk decryption to #bug13129 The code of this functionality is cherry-picked from https://lists.gnu.org/archive/html/grub-devel/2020-12/msg00257.html. project: Signed-off-by: hanliyang --- ...the-password-getter-and-additional-a.patch | 173 ++++++++++++++ ...odisk-add-OS-provided-secret-support.patch | 206 +++++++++++++++++ ...retrieving-the-EFI-secret-for-crypto.patch | 217 ++++++++++++++++++ grub.patches | 3 + grub2.spec | 1 + 5 files changed, 600 insertions(+) create mode 100644 1003-cryptodisk-make-the-password-getter-and-additional-a.patch create mode 100644 1004-cryptodisk-add-OS-provided-secret-support.patch create mode 100644 1005-efi-Add-API-for-retrieving-the-EFI-secret-for-crypto.patch diff --git a/1003-cryptodisk-make-the-password-getter-and-additional-a.patch b/1003-cryptodisk-make-the-password-getter-and-additional-a.patch new file mode 100644 index 0000000..bf6e451 --- /dev/null +++ b/1003-cryptodisk-make-the-password-getter-and-additional-a.patch @@ -0,0 +1,173 @@ +From 6503036b4adfcb078dc33b23a61969d914f7449b Mon Sep 17 00:00:00 2001 +From: James Bottomley +Date: Thu, 31 Dec 2020 09:36:16 -0800 +Subject: [PATCH 1/3] cryptodisk: make the password getter and additional + argument to recover_key + +cherry-picked from https://lists.gnu.org/archive/html/grub-devel/2020-12/msg00258.html. + +For AMD SEV environments, the grub boot password has to be retrieved +from a given memory location rather than prompted for. This means +that the standard password getter needs to be replaced with one that +gets the passphrase from the SEV area and uses that instead. Adding +the password getter as a passed in argument to recover_key() makes +this possible. + +Signed-off-by: James Bottomley +--- + grub-core/disk/cryptodisk.c | 2 +- + grub-core/disk/geli.c | 12 +++++++----- + grub-core/disk/luks.c | 12 +++++++----- + grub-core/lib/crypto.c | 4 ++++ + grub-core/osdep/unix/password.c | 4 ++++ + grub-core/osdep/windows/password.c | 4 ++++ + include/grub/cryptodisk.h | 6 +++++- + 7 files changed, 32 insertions(+), 12 deletions(-) + +diff --git a/grub-core/disk/cryptodisk.c b/grub-core/disk/cryptodisk.c +index 78a9025..133d36d 100644 +--- a/grub-core/disk/cryptodisk.c ++++ b/grub-core/disk/cryptodisk.c +@@ -839,7 +839,7 @@ grub_cryptodisk_scan_device_real (const char *name, grub_disk_t source) + if (!dev) + continue; + +- err = cr->recover_key (source, dev); ++ err = cr->recover_key (source, dev, grub_password_get); + if (err) + { + cryptodisk_close (dev); +diff --git a/grub-core/disk/geli.c b/grub-core/disk/geli.c +index e9d2329..ed43047 100644 +--- a/grub-core/disk/geli.c ++++ b/grub-core/disk/geli.c +@@ -398,7 +398,8 @@ configure_ciphers (grub_disk_t disk, const char *check_uuid, + } + + static grub_err_t +-recover_key (grub_disk_t source, grub_cryptodisk_t dev) ++recover_key (grub_disk_t source, grub_cryptodisk_t dev, ++ grub_passwd_cb *password_get) + { + grub_size_t keysize; + grub_uint8_t digest[GRUB_CRYPTO_MAX_MDLEN]; +@@ -438,11 +439,12 @@ recover_key (grub_disk_t source, grub_cryptodisk_t dev) + tmp = NULL; + if (source->partition) + tmp = grub_partition_get_name (source->partition); +- grub_printf_ (N_("Enter passphrase for %s%s%s (%s): "), source->name, +- source->partition ? "," : "", tmp ? : "", +- dev->uuid); ++ if (password_get (NULL, 0)) ++ grub_printf_ (N_("Enter passphrase for %s%s%s (%s): "), source->name, ++ source->partition ? "," : "", tmp ? : "", ++ dev->uuid); + grub_free (tmp); +- if (!grub_password_get (passphrase, MAX_PASSPHRASE)) ++ if (!password_get (passphrase, MAX_PASSPHRASE)) + return grub_error (GRUB_ERR_BAD_ARGUMENT, "Passphrase not supplied"); + + /* Calculate the PBKDF2 of the user supplied passphrase. */ +diff --git a/grub-core/disk/luks.c b/grub-core/disk/luks.c +index 18b3a8b..037d20d 100644 +--- a/grub-core/disk/luks.c ++++ b/grub-core/disk/luks.c +@@ -309,7 +309,8 @@ configure_ciphers (grub_disk_t disk, const char *check_uuid, + + static grub_err_t + luks_recover_key (grub_disk_t source, +- grub_cryptodisk_t dev) ++ grub_cryptodisk_t dev, ++ grub_passwd_cb *password_get) + { + struct grub_luks_phdr header; + grub_size_t keysize; +@@ -344,11 +345,12 @@ luks_recover_key (grub_disk_t source, + tmp = NULL; + if (source->partition) + tmp = grub_partition_get_name (source->partition); +- grub_printf_ (N_("Enter passphrase for %s%s%s (%s): "), source->name, +- source->partition ? "," : "", tmp ? : "", +- dev->uuid); ++ if (password_get (NULL, 0)) ++ grub_printf_ (N_("Enter passphrase for %s%s%s (%s): "), source->name, ++ source->partition ? "," : "", tmp ? : "", ++ dev->uuid); + grub_free (tmp); +- if (!grub_password_get (passphrase, MAX_PASSPHRASE)) ++ if (!password_get (passphrase, MAX_PASSPHRASE)) + { + grub_free (split_key); + return grub_error (GRUB_ERR_BAD_ARGUMENT, "Passphrase not supplied"); +diff --git a/grub-core/lib/crypto.c b/grub-core/lib/crypto.c +index ff62fa3..07c4400 100644 +--- a/grub-core/lib/crypto.c ++++ b/grub-core/lib/crypto.c +@@ -460,6 +460,10 @@ grub_password_get (char buf[], unsigned buf_size) + unsigned cur_len = 0; + int key; + ++ if (!buf) ++ /* want prompt */ ++ return 1; ++ + while (1) + { + key = grub_getkey (); +diff --git a/grub-core/osdep/unix/password.c b/grub-core/osdep/unix/password.c +index 9996b24..365ac4b 100644 +--- a/grub-core/osdep/unix/password.c ++++ b/grub-core/osdep/unix/password.c +@@ -34,6 +34,10 @@ grub_password_get (char buf[], unsigned buf_size) + int tty_changed = 0; + char *ptr; + ++ if (!buf) ++ /* want prompt */ ++ return 1; ++ + grub_refresh (); + + /* Disable echoing. Based on glibc. */ +diff --git a/grub-core/osdep/windows/password.c b/grub-core/osdep/windows/password.c +index 1d3af0c..2a66156 100644 +--- a/grub-core/osdep/windows/password.c ++++ b/grub-core/osdep/windows/password.c +@@ -33,6 +33,10 @@ grub_password_get (char buf[], unsigned buf_size) + DWORD mode = 0; + char *ptr; + ++ if (!buf) ++ /* want prompt */ ++ return 1; ++ + grub_refresh (); + + GetConsoleMode (hStdin, &mode); +diff --git a/include/grub/cryptodisk.h b/include/grub/cryptodisk.h +index 32f564a..33c86ad 100644 +--- a/include/grub/cryptodisk.h ++++ b/include/grub/cryptodisk.h +@@ -101,6 +101,9 @@ struct grub_cryptodisk + }; + typedef struct grub_cryptodisk *grub_cryptodisk_t; + ++/* must match prototype for grub_password_get */ ++typedef int (grub_passwd_cb)(char buf[], unsigned buf_size); ++ + struct grub_cryptodisk_dev + { + struct grub_cryptodisk_dev *next; +@@ -108,7 +111,8 @@ struct grub_cryptodisk_dev + + grub_cryptodisk_t (*scan) (grub_disk_t disk, const char *check_uuid, + int boot_only); +- grub_err_t (*recover_key) (grub_disk_t disk, grub_cryptodisk_t dev); ++ grub_err_t (*recover_key) (grub_disk_t disk, grub_cryptodisk_t dev, ++ grub_passwd_cb *get_passwd); + }; + typedef struct grub_cryptodisk_dev *grub_cryptodisk_dev_t; + +-- +2.43.5 + diff --git a/1004-cryptodisk-add-OS-provided-secret-support.patch b/1004-cryptodisk-add-OS-provided-secret-support.patch new file mode 100644 index 0000000..958a745 --- /dev/null +++ b/1004-cryptodisk-add-OS-provided-secret-support.patch @@ -0,0 +1,206 @@ +From a6d7988415f1ff7e3977b7e15aacaf1e7f854aac Mon Sep 17 00:00:00 2001 +From: James Bottomley +Date: Thu, 31 Dec 2020 09:36:17 -0800 +Subject: [PATCH 2/3] cryptodisk: add OS provided secret support + +cherry-picked from https://lists.gnu.org/archive/html/grub-devel/2020-12/msg00259.html. + +Make use of the new OS provided secrets API so that if the new '-s' +option is passed in we try to extract the secret from the API rather +than prompting for it. + +The primary consumer of this is AMD SEV, which has been programmed to +provide an injectable secret to the encrypted virtual machine. OVMF +provides the secret area and passes it into the EFI Configuration +Tables. The grub EFI layer pulls the secret out and primes the +secrets API with it. The upshot of all of this is that a SEV +protected VM can do an encrypted boot with a protected boot secret. + +Signed-off-by: James Bottomley +--- + grub-core/disk/cryptodisk.c | 77 +++++++++++++++++++++++++++++++++++-- + include/grub/cryptodisk.h | 14 +++++++ + 2 files changed, 87 insertions(+), 4 deletions(-) + +diff --git a/grub-core/disk/cryptodisk.c b/grub-core/disk/cryptodisk.c +index 133d36d..d2b1617 100644 +--- a/grub-core/disk/cryptodisk.c ++++ b/grub-core/disk/cryptodisk.c +@@ -41,6 +41,7 @@ static const struct grub_arg_option options[] = + /* TRANSLATORS: It's still restricted to cryptodisks only. */ + {"all", 'a', 0, N_("Mount all."), 0, 0}, + {"boot", 'b', 0, N_("Mount all volumes with `boot' flag set."), 0, 0}, ++ {"secret", 's', 0, N_("Get secret passphrase from named module and optional identifier"), 0, 0}, + {0, 0, 0, 0, 0, 0} + }; + +@@ -809,6 +810,10 @@ grub_util_cryptodisk_get_uuid (grub_disk_t disk) + + static int check_boot, have_it; + static char *search_uuid; ++static char *os_passwd; ++ ++/* variable to hold the list of secret providers */ ++static struct grub_secret_entry *secret_providers; + + static void + cryptodisk_close (grub_cryptodisk_t dev) +@@ -819,6 +824,21 @@ cryptodisk_close (grub_cryptodisk_t dev) + grub_free (dev); + } + ++static int ++os_password_get(char buf[], unsigned len) ++{ ++ if (!buf) ++ /* we're not interactive so no prompt */ ++ return 0; ++ ++ /* os_passwd should be null terminated, so just copy everything */ ++ grub_strncpy(buf, os_passwd, len); ++ /* and add a terminator just in case */ ++ buf[len - 1] = 0; ++ ++ return 1; ++} ++ + static grub_err_t + grub_cryptodisk_scan_device_real (const char *name, grub_disk_t source) + { +@@ -838,8 +858,17 @@ grub_cryptodisk_scan_device_real (const char *name, grub_disk_t source) + return grub_errno; + if (!dev) + continue; +- +- err = cr->recover_key (source, dev, grub_password_get); ++ ++ if (os_passwd) ++ { ++ err = cr->recover_key (source, dev, os_password_get); ++ if (err) ++ /* if the key doesn't work ignore the access denied error */ ++ grub_error_pop(); ++ } ++ else ++ err = cr->recover_key (source, dev, grub_password_get); ++ + if (err) + { + cryptodisk_close (dev); +@@ -855,6 +884,18 @@ grub_cryptodisk_scan_device_real (const char *name, grub_disk_t source) + return GRUB_ERR_NONE; + } + ++void ++grub_cryptodisk_add_secret_provider (struct grub_secret_entry *e) ++{ ++ grub_list_push(GRUB_AS_LIST_P (&secret_providers), GRUB_AS_LIST (e)); ++} ++ ++void ++grub_cryptodisk_remove_secret_provider (struct grub_secret_entry *e) ++{ ++ grub_list_remove (GRUB_AS_LIST (e)); ++} ++ + #ifdef GRUB_UTIL + #include + grub_err_t +@@ -931,7 +972,7 @@ grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int argc, char **args) + { + struct grub_arg_list *state = ctxt->state; + +- if (argc < 1 && !state[1].set && !state[2].set) ++ if (argc < 1 && !state[1].set && !state[2].set && !state[3].set) + return grub_error (GRUB_ERR_BAD_ARGUMENT, "device name required"); + + have_it = 0; +@@ -949,6 +990,7 @@ grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int argc, char **args) + + check_boot = state[2].set; + search_uuid = args[0]; ++ os_passwd = NULL; + grub_device_iterate (&grub_cryptodisk_scan_device, NULL); + search_uuid = NULL; + +@@ -959,11 +1001,37 @@ grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int argc, char **args) + else if (state[1].set || (argc == 0 && state[2].set)) + { + search_uuid = NULL; ++ os_passwd = NULL; + check_boot = state[2].set; + grub_device_iterate (&grub_cryptodisk_scan_device, NULL); + search_uuid = NULL; + return GRUB_ERR_NONE; + } ++ else if (state[3].set) ++ { ++ struct grub_secret_entry *se; ++ grub_err_t rc; ++ ++ if (argc < 1) ++ return grub_error (GRUB_ERR_BAD_ARGUMENT, "secret module must be specified"); ++#ifndef GRUB_UTIL ++ grub_dl_load (args[0]); ++#endif ++ se = grub_named_list_find (GRUB_AS_NAMED_LIST (secret_providers), args[0]); ++ if (se == NULL) ++ return grub_error (GRUB_ERR_INVALID_COMMAND, "No secret provider is found"); ++ ++ rc = se->get (args[1], &os_passwd); ++ if (rc) ++ return rc; ++ ++ search_uuid = NULL; ++ grub_device_iterate (&grub_cryptodisk_scan_device, NULL); ++ rc = se->put (args[1], have_it, &os_passwd); ++ os_passwd = NULL; ++ ++ return rc; ++ } + else + { + grub_err_t err; +@@ -974,6 +1042,7 @@ grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int argc, char **args) + grub_size_t len; + + search_uuid = NULL; ++ os_passwd = NULL; + check_boot = state[2].set; + diskname = args[0]; + len = grub_strlen (diskname); +@@ -1141,7 +1210,7 @@ GRUB_MOD_INIT (cryptodisk) + { + grub_disk_dev_register (&grub_cryptodisk_dev); + cmd = grub_register_extcmd ("cryptomount", grub_cmd_cryptomount, 0, +- N_("SOURCE|-u UUID|-a|-b"), ++ N_("SOURCE|-u UUID|-a|-b|-s MOD [ID]"), + N_("Mount a crypto device."), options); + grub_procfs_register ("luks_script", &luks_script); + } +diff --git a/include/grub/cryptodisk.h b/include/grub/cryptodisk.h +index 33c86ad..6a93c56 100644 +--- a/include/grub/cryptodisk.h ++++ b/include/grub/cryptodisk.h +@@ -160,4 +160,18 @@ grub_util_get_geli_uuid (const char *dev); + grub_cryptodisk_t grub_cryptodisk_get_by_uuid (const char *uuid); + grub_cryptodisk_t grub_cryptodisk_get_by_source_disk (grub_disk_t disk); + ++struct grub_secret_entry { ++ /* as named list */ ++ struct grub_secret_entry *next; ++ struct grub_secret_entry **prev; ++ const char *name; ++ ++ /* additional entries */ ++ grub_err_t (*get)(const char *arg, char **secret); ++ grub_err_t (*put)(const char *arg, int have_it, char **secret); ++}; ++ ++void grub_cryptodisk_add_secret_provider (struct grub_secret_entry *e); ++void grub_cryptodisk_remove_secret_provider (struct grub_secret_entry *e); ++ + #endif +-- +2.43.5 + diff --git a/1005-efi-Add-API-for-retrieving-the-EFI-secret-for-crypto.patch b/1005-efi-Add-API-for-retrieving-the-EFI-secret-for-crypto.patch new file mode 100644 index 0000000..cf51219 --- /dev/null +++ b/1005-efi-Add-API-for-retrieving-the-EFI-secret-for-crypto.patch @@ -0,0 +1,217 @@ +From 9711568e6dce36a6245fada1509e9477c0268e30 Mon Sep 17 00:00:00 2001 +From: James Bottomley +Date: Thu, 31 Dec 2020 09:36:18 -0800 +Subject: [PATCH 3/3] efi: Add API for retrieving the EFI secret for cryptodisk + +cherry-picked from https://lists.gnu.org/archive/html/grub-devel/2020-12/msg00260.html. + +This module is designed to provide an efisecret command which +interrogates the EFI configuration table to find the location of the +confidential computing secret and tries to register the secret with +the cryptodisk. + +The secret is stored in a boot allocated area, usually a page in size. +The layout of the secret injection area is a header + +|GRUB_EFI_SECRET_TABLE_HEADER_GUID|len| + +with entries of the form + +|guid|len|data| + +the guid corresponding to the disk encryption passphrase is +GRUB_EFI_DISKPASSWD_GUID and data must be a zero terminated string. +To get a high entropy string that doesn't need large numbers of +iterations, use a base64 encoding of 33 bytes of random data. + +Signed-off-by: James Bottomley +--- + grub-core/Makefile.core.def | 8 ++ + grub-core/disk/efi/efisecret.c | 129 +++++++++++++++++++++++++++++++++ + include/grub/efi/api.h | 15 ++++ + 3 files changed, 152 insertions(+) + create mode 100644 grub-core/disk/efi/efisecret.c + +diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def +index 0963e88..aded014 100644 +--- a/grub-core/Makefile.core.def ++++ b/grub-core/Makefile.core.def +@@ -774,6 +774,14 @@ module = { + enable = efi; + }; + ++module = { ++ name = efisecret; ++ ++ common = disk/efi/efisecret.c; ++ ++ enable = efi; ++}; ++ + module = { + name = lsefimmap; + +diff --git a/grub-core/disk/efi/efisecret.c b/grub-core/disk/efi/efisecret.c +new file mode 100644 +index 0000000..745eede +--- /dev/null ++++ b/grub-core/disk/efi/efisecret.c +@@ -0,0 +1,129 @@ ++#include ++#include ++#include ++#include ++#include ++#include ++ ++GRUB_MOD_LICENSE ("GPLv3+"); ++ ++static grub_efi_packed_guid_t secret_guid = GRUB_EFI_SECRET_TABLE_GUID; ++static grub_efi_packed_guid_t tableheader_guid = GRUB_EFI_SECRET_TABLE_HEADER_GUID; ++static grub_efi_packed_guid_t diskpasswd_guid = GRUB_EFI_DISKPASSWD_GUID; ++ ++/* ++ * EFI places the secret in the lower 4GB, so it uses a UINT32 ++ * for the pointer which we have to transform to the correct type ++ */ ++struct efi_secret { ++ grub_uint64_t base; ++ grub_uint64_t size; ++}; ++ ++struct secret_header { ++ grub_efi_packed_guid_t guid; ++ grub_uint32_t len; ++}; ++ ++struct secret_entry { ++ grub_efi_packed_guid_t guid; ++ grub_uint32_t len; ++ char data[0]; ++}; ++ ++static grub_err_t ++grub_efi_secret_put (const char *arg __attribute__((unused)), int have_it, ++ char **ptr) ++{ ++ struct secret_entry *e = (struct secret_entry *)(*ptr - (long)&((struct secret_entry *)0)->data); ++ ++ /* destroy the secret */ ++ grub_memset (e, 0, e->len); ++ *ptr = NULL; ++ ++ if (have_it) ++ return GRUB_ERR_NONE; ++ ++ return grub_error (GRUB_ERR_ACCESS_DENIED, "EFI secret failed to unlock any volumes"); ++} ++ ++static grub_err_t ++grub_efi_secret_find (struct efi_secret *s, char **secret_ptr) ++{ ++ int len; ++ struct secret_header *h; ++ struct secret_entry *e; ++ unsigned char *ptr = (unsigned char *)(unsigned long)s->base; ++ ++ /* the area must be big enough for a guid and a u32 length */ ++ if (s->size < sizeof (*h)) ++ return grub_error (GRUB_ERR_BAD_ARGUMENT, "EFI secret area is too small"); ++ ++ h = (struct secret_header *)ptr; ++ if (grub_memcmp(&h->guid, &tableheader_guid, sizeof (h->guid))) ++ return grub_error (GRUB_ERR_BAD_ARGUMENT, "EFI secret area does not start with correct guid\n"); ++ if (h->len < sizeof (*h)) ++ return grub_error (GRUB_ERR_BAD_ARGUMENT, "EFI secret area is too small\n"); ++ ++ len = h->len - sizeof (*h); ++ ptr += sizeof (*h); ++ ++ while (len >= (int)sizeof (*e)) { ++ e = (struct secret_entry *)ptr; ++ if (e->len < sizeof(*e) || e->len > (unsigned int)len) ++ return grub_error (GRUB_ERR_BAD_ARGUMENT, "EFI secret area is corrupt\n"); ++ ++ if (! grub_memcmp (&e->guid, &diskpasswd_guid, sizeof (e->guid))) { ++ int end = e->len - sizeof(*e); ++ ++ /* ++ * the passphrase must be a zero terminated string because the ++ * password routines call grub_strlen () to find its size ++ */ ++ if (e->data[end - 1] != '\0') ++ return grub_error (GRUB_ERR_BAD_ARGUMENT, "EFI secret area disk encryption password is not zero terminated\n"); ++ ++ *secret_ptr = e->data; ++ return GRUB_ERR_NONE; ++ } ++ ptr += e->len; ++ len -= e->len; ++ } ++ return grub_error (GRUB_ERR_BAD_ARGUMENT, "EFI secret aread does not contain disk decryption password\n"); ++} ++ ++static grub_err_t ++grub_efi_secret_get (const char *arg __attribute__((unused)), char **ptr) ++{ ++ unsigned int i; ++ ++ for (i = 0; i < grub_efi_system_table->num_table_entries; i++) ++ { ++ grub_efi_packed_guid_t *guid = ++ &grub_efi_system_table->configuration_table[i].vendor_guid; ++ ++ if (! grub_memcmp (guid, &secret_guid, sizeof (grub_efi_packed_guid_t))) { ++ struct efi_secret *s = ++ grub_efi_system_table->configuration_table[i].vendor_table; ++ ++ return grub_efi_secret_find(s, ptr); ++ } ++ } ++ return grub_error (GRUB_ERR_BAD_ARGUMENT, "No secret found in the EFI configuration table"); ++} ++ ++static struct grub_secret_entry secret = { ++ .name = "efisecret", ++ .get = grub_efi_secret_get, ++ .put = grub_efi_secret_put, ++}; ++ ++GRUB_MOD_INIT(efisecret) ++{ ++ grub_cryptodisk_add_secret_provider (&secret); ++} ++ ++GRUB_MOD_FINI(efisecret) ++{ ++ grub_cryptodisk_remove_secret_provider (&secret); ++} +diff --git a/include/grub/efi/api.h b/include/grub/efi/api.h +index a57187f..d5c805d 100644 +--- a/include/grub/efi/api.h ++++ b/include/grub/efi/api.h +@@ -299,6 +299,21 @@ + { 0x9a, 0x16, 0x00, 0x90, 0x27, 0x3f, 0xc1, 0x4d } \ + } + ++#define GRUB_EFI_SECRET_TABLE_GUID \ ++ { 0xadf956ad, 0xe98c, 0x484c, \ ++ { 0xae, 0x11, 0xb5, 0x1c, 0x7d, 0x33, 0x64, 0x47} \ ++ } ++ ++#define GRUB_EFI_SECRET_TABLE_HEADER_GUID \ ++ { 0x1e74f542, 0x71dd, 0x4d66, \ ++ { 0x96, 0x3e, 0xef, 0x42, 0x87, 0xff, 0x17, 0x3b } \ ++ } ++ ++#define GRUB_EFI_DISKPASSWD_GUID \ ++ { 0x736869e5, 0x84f0, 0x4973, \ ++ { 0x92, 0xec, 0x06, 0x87, 0x9c, 0xe3, 0xda, 0x0b } \ ++ } ++ + #define GRUB_EFI_ACPI_TABLE_GUID \ + { 0xeb9d2d30, 0x2d88, 0x11d3, \ + { 0x9a, 0x16, 0x0, 0x90, 0x27, 0x3f, 0xc1, 0x4d } \ +-- +2.43.5 + diff --git a/grub.patches b/grub.patches index a82a1a9..62d80ca 100644 --- a/grub.patches +++ b/grub.patches @@ -667,3 +667,6 @@ Patch0666: 0666-ieee1275-ofnet-Fix-grub_malloc-removed-after-added-s.patch #Patch1000: 1000-loongarch64-add-support.patch Patch1001: 1001-bls-make-list.patch Patch1002: 1002-Add-LoongArch64-support-and-update-interface-to-v40.patch +Patch1003: 1003-cryptodisk-make-the-password-getter-and-additional-a.patch +Patch1004: 1004-cryptodisk-add-OS-provided-secret-support.patch +Patch1005: 1005-efi-Add-API-for-retrieving-the-EFI-secret-for-crypto.patch diff --git a/grub2.spec b/grub2.spec index 7dcf64a..ee6e08b 100644 --- a/grub2.spec +++ b/grub2.spec @@ -544,6 +544,7 @@ fi - Add LoongArch64 support(yangqiming@loongson.cn) - LoongArch64 support fdt and phy-addr BIOS(yangqiming@loongson.cn) - Remove dtb dir with correct argument (Liwei Ge) +- Support use confidential computing provisioned secrets for disk decryption(hanliyang@hygon.cn) * Tue Mar 25 2025 Nicolas Frayer - 2.02-162 - ieee1275/ofnet: Fix grub_malloc() removed after added safe -- Gitee From dc36ca3712f5a4e6fba8cdfd6fc0bf4296010126 Mon Sep 17 00:00:00 2001 From: Xue Liu Date: Sat, 22 Mar 2025 17:31:26 +0800 Subject: [PATCH 8/8] Update for loongarch64 in sync with latest v2.04 --- ...ate-patches-in-sync-with-loongarch64.patch | 2815 +++++++++++++++++ grub.patches | 1 + grub2.spec | 1 + 3 files changed, 2817 insertions(+) create mode 100644 1006-Update-patches-in-sync-with-loongarch64.patch diff --git a/1006-Update-patches-in-sync-with-loongarch64.patch b/1006-Update-patches-in-sync-with-loongarch64.patch new file mode 100644 index 0000000..dc39a93 --- /dev/null +++ b/1006-Update-patches-in-sync-with-loongarch64.patch @@ -0,0 +1,2815 @@ +From 7118e2827e64c205e3b2241b85648f2948cab3a3 Mon Sep 17 00:00:00 2001 +From: Fedora Ninjas +Date: Sat, 22 Mar 2025 16:49:13 +0800 +Subject: [PATCH] Update patches in sync with loongarch64 + +--- + gentpl.py | 2 +- + grub-core/Makefile.am | 1 - + grub-core/Makefile.core.def | 11 +- + grub-core/kern/loongarch64/cache.S | 2 + + grub-core/kern/loongarch64/efi/init.c | 2 - + grub-core/kern/loongarch64/efi/startup.S | 4 +- + grub-core/kern/loongarch64/init.c | 47 -- + grub-core/lib/loongarch64/efi/loongson.c | 119 --- + grub-core/lib/loongarch64/relocator.c | 2 +- + grub-core/lib/loongarch64/setjmp.S | 80 +- + grub-core/loader/loongarch64/linux-efi.c | 143 ++++ + grub-core/loader/loongarch64/linux-elf.c | 910 +++++++++++++++++++++++ + grub-core/loader/loongarch64/linux.c | 731 +++++------------- + include/grub/efi/api.h | 20 + + include/grub/fdt.h | 2 +- + include/grub/loongarch64/asm.h | 10 - + include/grub/loongarch64/efi/loongson.h | 113 --- + include/grub/loongarch64/efi/memory.h | 7 +- + include/grub/loongarch64/efi/time.h | 0 + include/grub/loongarch64/linux.h | 163 +++- + include/grub/loongarch64/time.h | 12 - + 21 files changed, 1487 insertions(+), 894 deletions(-) + delete mode 100644 grub-core/kern/loongarch64/init.c + delete mode 100644 grub-core/lib/loongarch64/efi/loongson.c + create mode 100644 grub-core/loader/loongarch64/linux-efi.c + create mode 100644 grub-core/loader/loongarch64/linux-elf.c + delete mode 100644 include/grub/loongarch64/asm.h + delete mode 100644 include/grub/loongarch64/efi/loongson.h + delete mode 100644 include/grub/loongarch64/efi/time.h + +diff --git a/gentpl.py b/gentpl.py +index 3afd642..fa7f1ba 100644 +--- a/gentpl.py ++++ b/gentpl.py +@@ -78,7 +78,7 @@ GROUPS["terminfomodule"] = GRUB_PLATFORMS[:]; + for i in GROUPS["terminfoinkernel"]: GROUPS["terminfomodule"].remove(i) + + # Flattened Device Trees (FDT) +-GROUPS["fdt"] = [ "arm64_efi", "arm_uboot", "arm_efi" ] ++GROUPS["fdt"] = [ "arm64_efi", "arm_uboot", "arm_efi", "loongarch64_efi" ] + + # Needs software helpers for division + # Must match GRUB_DIVISION_IN_SOFTWARE in misc.h +diff --git a/grub-core/Makefile.am b/grub-core/Makefile.am +index a976fad..ec771c5 100644 +--- a/grub-core/Makefile.am ++++ b/grub-core/Makefile.am +@@ -227,7 +227,6 @@ KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/efi/efi.h + KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/efi/disk.h + KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/acpi.h + KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/fdt.h +-KERNEL_HEADER_FILES += $(top_builddir)/include/grub/machine/loongson.h + endif + + if COND_powerpc_ieee1275 +diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def +index aded014..b89e58e 100644 +--- a/grub-core/Makefile.core.def ++++ b/grub-core/Makefile.core.def +@@ -250,6 +250,9 @@ kernel = { + arm64_efi = kern/arm64/efi/init.c; + arm64_efi = kern/efi/fdt.c; + ++ loongarch64_efi = kern/loongarch64/efi/init.c; ++ loongarch64_efi = kern/efi/fdt.c; ++ + i386_pc = kern/i386/pc/init.c; + i386_pc = kern/i386/pc/mmap.c; + i386_pc = term/i386/pc/console.c; +@@ -300,14 +303,8 @@ kernel = { + extra_dist = video/sis315_init.c; + mips_loongson = commands/keylayouts.c; + +- loongarch64 = kern/loongarch64/init.c; + loongarch64 = kern/loongarch64/dl.c; + loongarch64 = kern/loongarch64/cache.S; +- loongarch64 = kern/generic/rtc_get_time_ms.c; +- loongarch64 = kern/efi/fdt.c; +- loongarch64 = lib/fdt.c; +- loongarch64_efi = kern/loongarch64/efi/init.c; +- loongarch64_efi = lib/loongarch64/efi/loongson.c; + + powerpc_ieee1275 = kern/powerpc/cache.S; + powerpc_ieee1275 = kern/powerpc/dl.c; +@@ -1767,6 +1764,8 @@ module = { + i386_pc = lib/i386/pc/vesa_modes_table.c; + mips = loader/mips/linux.c; + loongarch64 = loader/loongarch64/linux.c; ++ loongarch64 = loader/loongarch64/linux-elf.c; ++ loongarch64 = loader/loongarch64/linux-efi.c; + powerpc_ieee1275 = loader/powerpc/ieee1275/linux.c; + sparc64_ieee1275 = loader/sparc64/ieee1275/linux.c; + ia64_efi = loader/ia64/efi/linux.c; +diff --git a/grub-core/kern/loongarch64/cache.S b/grub-core/kern/loongarch64/cache.S +index d291c67..b268261 100644 +--- a/grub-core/kern/loongarch64/cache.S ++++ b/grub-core/kern/loongarch64/cache.S +@@ -19,6 +19,8 @@ + #include + + FUNCTION (grub_arch_sync_caches) ++ ibar 0 ++ dbar 0 + jr $ra + + FUNCTION (grub_arch_sync_dma_caches) +diff --git a/grub-core/kern/loongarch64/efi/init.c b/grub-core/kern/loongarch64/efi/init.c +index 632c39a..15c41aa 100644 +--- a/grub-core/kern/loongarch64/efi/init.c ++++ b/grub-core/kern/loongarch64/efi/init.c +@@ -1,4 +1,3 @@ +-/* init.c - initialize an arm-based EFI system */ + /* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2013 Free Software Foundation, Inc. +@@ -24,7 +23,6 @@ + #include + #include + #include +-#include + + static grub_uint64_t tmr; + static grub_efi_event_t tmr_evt; +diff --git a/grub-core/kern/loongarch64/efi/startup.S b/grub-core/kern/loongarch64/efi/startup.S +index 1ffff08..00ecd15 100644 +--- a/grub-core/kern/loongarch64/efi/startup.S ++++ b/grub-core/kern/loongarch64/efi/startup.S +@@ -19,8 +19,9 @@ + #include + + .file "startup.S" +- .text + .globl start, _start ++ .text ++ + .align 4 + + FUNCTION(start) +@@ -42,4 +43,3 @@ FUNCTION(_start) + ld.d $ra, $sp, 0 + addi.d $sp, $sp, 16 + jr $ra +- +diff --git a/grub-core/kern/loongarch64/init.c b/grub-core/kern/loongarch64/init.c +deleted file mode 100644 +index b2de930..0000000 +--- a/grub-core/kern/loongarch64/init.c ++++ /dev/null +@@ -1,47 +0,0 @@ +-/* +- * GRUB -- GRand Unified Bootloader +- * Copyright (C) 2009,2017 Free Software Foundation, Inc. +- * +- * GRUB is free software: you can redistribute it and/or modify +- * it under the terms of the GNU General Public License as published by +- * the Free Software Foundation, either version 3 of the License, or +- * (at your option) any later version. +- * +- * GRUB is distributed in the hope that it will be useful, +- * but WITHOUT ANY WARRANTY; without even the implied warranty of +- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +- * GNU General Public License for more details. +- * +- * You should have received a copy of the GNU General Public License +- * along with GRUB. If not, see . +- */ +- +-#include +-#include +-#include +-#include +- +-grub_uint32_t grub_arch_cpuclock; +- +-/* FIXME: use interrupt to count high. */ +-grub_uint64_t +-grub_get_rtc (void) +-{ +- static grub_uint32_t high = 0; +- static grub_uint32_t last = 0; +- grub_uint32_t low; +- +- asm volatile ("csrrd %0, " GRUB_CPU_LOONGARCH_COP0_TIMER_COUNT : "=r" (low)); +- if (low < last) +- high++; +- last = low; +- +- return (((grub_uint64_t) high) << 32) | low; +-} +- +-void +-grub_timer_init (grub_uint32_t cpuclock) +-{ +- grub_arch_cpuclock = cpuclock; +- grub_install_get_time_ms (grub_rtc_get_time_ms); +-} +diff --git a/grub-core/lib/loongarch64/efi/loongson.c b/grub-core/lib/loongarch64/efi/loongson.c +deleted file mode 100644 +index eda1e1c..0000000 +--- a/grub-core/lib/loongarch64/efi/loongson.c ++++ /dev/null +@@ -1,119 +0,0 @@ +-/* +- * GRUB -- GRand Unified Bootloader +- * Copyright (C) 2017 Free Software Foundation, Inc. +- * +- * GRUB is free software: you can redistribute it and/or modify +- * it under the terms of the GNU General Public License as published by +- * the Free Software Foundation, either version 3 of the License, or +- * (at your option) any later version. +- * +- * GRUB is distributed in the hope that it will be useful, +- * but WITHOUT ANY WARRANTY; without even the implied warranty of +- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +- * GNU General Public License for more details. +- * +- * You should have received a copy of the GNU General Public License +- * along with GRUB. If not, see . +- */ +- +-#include +-#include +-#include +-#include +-#include +-#include +- +-unsigned long +-grub_efi_get_bpi_version (const char *str) +-{ +- unsigned long version = GRUB_EFI_BPI_VER_NONE; +- +- version = grub_strtoul (str + 4, 0, 0); +- +- return version; +-} +- +-void * +-grub_efi_loongson_get_boot_params (void) +-{ +- static void * boot_params = NULL; +- grub_efi_configuration_table_t *tables; +- grub_efi_guid_t loongson_bpi_guid = GRUB_EFI_LOONGSON_BPI_TABLE_GUID; +- unsigned int i; +- +- if (boot_params) +- return boot_params; +- +- /* Look for Loongson boot params interface in UEFI config tables. */ +- tables = grub_efi_system_table->configuration_table; +- +- for (i = 0; i < grub_efi_system_table->num_table_entries; i++) +- if (grub_memcmp (&tables[i].vendor_guid, &loongson_bpi_guid, sizeof (loongson_bpi_guid)) == 0) +- { +- boot_params= tables[i].vendor_table; +- grub_dprintf ("loongson", "found registered BPI @ %p\n", boot_params); +- break; +- } +- return boot_params; +-} +- +-grub_uint8_t +-grub_efi_loongson_calculatesum8 (const grub_uint8_t *buffer, grub_efi_uintn_t length) +-{ +- grub_uint8_t sum; +- grub_efi_uintn_t count; +- +- for (sum = 0, count = 0; count < length; count++) +- { +- sum = (grub_uint8_t) (sum + *(buffer + count)); +- } +- return sum; +-} +- +-grub_uint8_t +-grub_efi_loongson_grub_calculatechecksum8 (const grub_uint8_t *buffer, grub_efi_uintn_t length) +-{ +- grub_uint8_t checksum; +- +- checksum = grub_efi_loongson_calculatesum8(buffer, length); +- +- return (grub_uint8_t) (0x100 - checksum); +-} +- +- +-grub_uint32_t +-grub_efi_loongson_memmap_v1_sort(struct memmap_v1 array[], grub_uint32_t length, +- mem_map_v1 * bpmem, grub_uint32_t index, grub_uint32_t memtype) +-{ +- grub_uint64_t tempmemsize = 0; +- grub_uint32_t j = 0; +- grub_uint32_t t = 0; +- +- for(j = 0; j < length;) +- { +- tempmemsize = array[j].memsize; +- for(t = j + 1; t < length; t++) +- { +- if(array[j].memstart + tempmemsize == array[t].memstart) +- { +- tempmemsize += array[t].memsize; +- } +- else +- { +- break; +- } +- } +- bpmem->map[index].memtype = memtype; +- bpmem->map[index].memstart = array[j].memstart; +- bpmem->map[index].memsize = tempmemsize; +- grub_dprintf("loongson", "v1 map[%d]:type %x, start 0x%lx, end 0x%lx\n", +- index, +- bpmem->map[index].memtype, +- bpmem->map[index].memstart, +- bpmem->map[index].memstart+ bpmem->map[index].memsize +- ); +- j = t; +- index++; +- } +- return index; +-} +diff --git a/grub-core/lib/loongarch64/relocator.c b/grub-core/lib/loongarch64/relocator.c +index 4b253ca..7217b68 100644 +--- a/grub-core/lib/loongarch64/relocator.c ++++ b/grub-core/lib/loongarch64/relocator.c +@@ -122,7 +122,7 @@ grub_relocator64_boot (struct grub_relocator *rel, + unsigned i; + grub_addr_t vtarget; + +- err = grub_relocator_alloc_chunk_align (rel, &ch, 0, ++ err = grub_relocator_alloc_chunk_align (rel, &ch, 0x3000000, + (0xffffffff - stateset_size) + + 1, stateset_size, + grub_relocator_align, +diff --git a/grub-core/lib/loongarch64/setjmp.S b/grub-core/lib/loongarch64/setjmp.S +index 47db814..bb09959 100644 +--- a/grub-core/lib/loongarch64/setjmp.S ++++ b/grub-core/lib/loongarch64/setjmp.S +@@ -1,6 +1,6 @@ + /* + * GRUB -- GRand Unified Bootloader +- * Copyright (C) 2003,2007,2009 Free Software Foundation, Inc. ++ * Copyright (C) 2021 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by +@@ -16,9 +16,7 @@ + * along with GRUB. If not, see . + */ + +-#include + #include +-#include + + .file "setjmp.S" + +@@ -27,48 +25,44 @@ GRUB_MOD_LICENSE "GPLv3+" + .text + + /* +- * int grub_setjmp (grub_jmp_buf env) ++ * int grub_setjmp (jmp_buf env) + */ + FUNCTION(grub_setjmp) +- GRUB_ASM_REG_S $s0, $a0,0 +- GRUB_ASM_REG_S $s1, $a0,8 +- GRUB_ASM_REG_S $s2, $a0,16 +- GRUB_ASM_REG_S $s3, $a0,24 +- GRUB_ASM_REG_S $s4, $a0,32 +- GRUB_ASM_REG_S $s5, $a0,40 +- GRUB_ASM_REG_S $s6, $a0,48 +- GRUB_ASM_REG_S $s7, $a0,56 +- GRUB_ASM_REG_S $s8, $a0,64 +- GRUB_ASM_REG_S $fp, $a0,72 +- GRUB_ASM_REG_S $sp, $a0,80 +- GRUB_ASM_REG_S $ra, $a0,88 +- move $v0, $zero +- move $v1, $zero +- jr $ra +- nop ++ st.d $s0, $a0, 0x0 ++ st.d $s1, $a0, 0x8 ++ st.d $s2, $a0, 0x10 ++ st.d $s3, $a0, 0x18 ++ st.d $s4, $a0, 0x20 ++ st.d $s5, $a0, 0x28 ++ st.d $s6, $a0, 0x30 ++ st.d $s7, $a0, 0x38 ++ st.d $s8, $a0, 0x40 ++ st.d $fp, $a0, 0x48 ++ st.d $sp, $a0, 0x50 ++ st.d $ra, $a0, 0x58 ++ ++ move $a0, $zero ++ jr $ra ++ + /* +- * int grub_longjmp (grub_jmp_buf env, int val) ++ * void grub_longjmp (jmp_buf env, int val) + */ + FUNCTION(grub_longjmp) +- GRUB_ASM_REG_L $s0, $a0,0 +- GRUB_ASM_REG_L $s1, $a0,8 +- GRUB_ASM_REG_L $s2, $a0,16 +- GRUB_ASM_REG_L $s3, $a0,24 +- GRUB_ASM_REG_L $s4, $a0,32 +- GRUB_ASM_REG_L $s5, $a0,40 +- GRUB_ASM_REG_L $s6, $a0,48 +- GRUB_ASM_REG_L $s7, $a0,56 +- GRUB_ASM_REG_L $s8, $a0,64 +- GRUB_ASM_REG_L $fp, $a0,72 +- GRUB_ASM_REG_L $sp, $a0,80 +- GRUB_ASM_REG_L $ra, $a0,88 +- addi.w $v0, $zero, 1 +- /* +- * replace: movn $v0, $a1, $a1 +- */ +- bnez $a1, .ZW0 +- addi.d $v0, $a1, 0 +-.ZW0: +- addi.d $v1,$zero,0 +- jr $ra +- nop ++ ld.d $s0, $a0, 0x0 ++ ld.d $s1, $a0, 0x8 ++ ld.d $s2, $a0, 0x10 ++ ld.d $s3, $a0, 0x18 ++ ld.d $s4, $a0, 0x20 ++ ld.d $s5, $a0, 0x28 ++ ld.d $s6, $a0, 0x30 ++ ld.d $s7, $a0, 0x38 ++ ld.d $s8, $a0, 0x40 ++ ld.d $fp, $a0, 0x48 ++ ld.d $sp, $a0, 0x50 ++ ld.d $ra, $a0, 0x58 ++ ++ li.w $a0, 1 ++ beqz $a1, .L0 ++ move $a0, $a1 ++.L0: ++ jr $ra +diff --git a/grub-core/loader/loongarch64/linux-efi.c b/grub-core/loader/loongarch64/linux-efi.c +new file mode 100644 +index 0000000..6f5ff15 +--- /dev/null ++++ b/grub-core/loader/loongarch64/linux-efi.c +@@ -0,0 +1,143 @@ ++/* ++ * GRUB -- GRand Unified Bootloader ++ * Copyright (C) 2021 Free Software Foundation, Inc. ++ * ++ * GRUB is free software: you can redistribute it and/or modify ++ * it under the terms of the GNU General Public License as published by ++ * the Free Software Foundation, either version 3 of the License, or ++ * (at your option) any later version. ++ * ++ * GRUB is distributed in the hope that it will be useful, ++ * but WITHOUT ANY WARRANTY; without even the implied warranty of ++ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ++ * GNU General Public License for more details. ++ * ++ * You should have received a copy of the GNU General Public License ++ * along with GRUB. If not, see . ++ */ ++#include ++#include ++#include ++#include ++#include ++#include ++ ++#define GRUB_EFI_PE_MAGIC 0x5A4D ++ ++grub_err_t ++finalize_efi_params_linux (struct linux_loongarch64_kernel_params *kernel_params) ++{ ++ int node, retval; ++ ++ void *fdt; ++ ++ fdt = grub_fdt_load (GRUB_EFI_LINUX_FDT_EXTRA_SPACE); ++ ++ if (!fdt) ++ goto failure; ++ ++ node = grub_fdt_find_subnode (fdt, 0, "chosen"); ++ if (node < 0) ++ node = grub_fdt_add_subnode (fdt, 0, "chosen"); ++ ++ if (node < 1) ++ goto failure; ++ ++ /* Set initrd info */ ++ if (kernel_params->ramdisk_addr && kernel_params->ramdisk_size) ++ { ++ grub_dprintf ("linux", "Initrd @ %p-%p\n", ++ (void *) kernel_params->ramdisk_addr, ++ (void *) (kernel_params->ramdisk_addr + kernel_params->ramdisk_size)); ++ ++ retval = grub_fdt_set_prop64 (fdt, node, "linux,initrd-start", ++ kernel_params->ramdisk_addr); ++ if (retval) ++ goto failure; ++ retval = grub_fdt_set_prop64 (fdt, node, "linux,initrd-end", ++ kernel_params->ramdisk_addr + kernel_params->ramdisk_size); ++ if (retval) ++ goto failure; ++ } ++ ++ if (grub_fdt_install() != GRUB_ERR_NONE) ++ goto failure; ++ ++ return GRUB_ERR_NONE; ++ ++failure: ++ grub_fdt_unload(); ++ return grub_error(GRUB_ERR_BAD_OS, "failed to install/update FDT"); ++} ++ ++grub_err_t ++grub_arch_efi_linux_check_image (struct linux_arch_kernel_header * lh) ++{ ++ if ((lh->code0 & 0xffff) == GRUB_EFI_PE_MAGIC) ++ return GRUB_ERR_NONE; ++ else ++ return 1; ++ ++ grub_dprintf ("linux", "UEFI stub kernel:\n"); ++ grub_dprintf ("linux", "PE/COFF header @ %08x\n", lh->hdr_offset); ++ ++ return GRUB_ERR_NONE; ++} ++ ++grub_err_t ++grub_arch_efi_linux_boot_image (grub_addr_t addr, grub_size_t size, char *args) ++{ ++ grub_efi_memory_mapped_device_path_t *mempath; ++ grub_efi_handle_t image_handle; ++ grub_efi_boot_services_t *b; ++ grub_efi_status_t status; ++ grub_efi_loaded_image_t *loaded_image; ++ int len; ++ ++ mempath = grub_malloc (2 * sizeof (grub_efi_memory_mapped_device_path_t)); ++ if (!mempath) ++ return grub_errno; ++ ++ mempath[0].header.type = GRUB_EFI_HARDWARE_DEVICE_PATH_TYPE; ++ mempath[0].header.subtype = GRUB_EFI_MEMORY_MAPPED_DEVICE_PATH_SUBTYPE; ++ mempath[0].header.length = grub_cpu_to_le16_compile_time (sizeof (*mempath)); ++ mempath[0].memory_type = GRUB_EFI_LOADER_DATA; ++ mempath[0].start_address = addr; ++ mempath[0].end_address = addr + size; ++ ++ mempath[1].header.type = GRUB_EFI_END_DEVICE_PATH_TYPE; ++ mempath[1].header.subtype = GRUB_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE; ++ mempath[1].header.length = sizeof (grub_efi_device_path_t); ++ ++ b = grub_efi_system_table->boot_services; ++ status = b->load_image (0, grub_efi_image_handle, ++ (grub_efi_device_path_t *) mempath, ++ (void *) addr, size, &image_handle); ++ if (status != GRUB_EFI_SUCCESS) ++ return grub_error (GRUB_ERR_BAD_OS, "cannot load image"); ++ ++ grub_dprintf ("linux", "linux command line: '%s'\n", args); ++ ++ /* Convert command line to UCS-2 */ ++ loaded_image = grub_efi_get_loaded_image (image_handle); ++ loaded_image->load_options_size = len = ++ (grub_strlen (args) + 1) * sizeof (grub_efi_char16_t); ++ loaded_image->load_options = ++ grub_efi_allocate_any_pages (GRUB_EFI_BYTES_TO_PAGES (loaded_image->load_options_size)); ++ if (!loaded_image->load_options) ++ return grub_errno; ++ ++ loaded_image->load_options_size = ++ 2 * grub_utf8_to_utf16 (loaded_image->load_options, len, ++ (grub_uint8_t *) args, len, NULL); ++ ++ grub_dprintf ("linux", "starting image %p\n", image_handle); ++ status = b->start_image (image_handle, 0, NULL); ++ ++ /* When successful, not reached */ ++ b->unload_image (image_handle); ++ grub_efi_free_pages ((grub_addr_t) loaded_image->load_options, ++ GRUB_EFI_BYTES_TO_PAGES (loaded_image->load_options_size)); ++ ++ return grub_errno; ++} +diff --git a/grub-core/loader/loongarch64/linux-elf.c b/grub-core/loader/loongarch64/linux-elf.c +new file mode 100644 +index 0000000..d54028e +--- /dev/null ++++ b/grub-core/loader/loongarch64/linux-elf.c +@@ -0,0 +1,910 @@ ++/* ++ * GRUB -- GRand Unified Bootloader ++ * Copyright (C) 2021 Free Software Foundation, Inc. ++ * ++ * GRUB is free software: you can redistribute it and/or modify ++ * it under the terms of the GNU General Public License as published by ++ * the Free Software Foundation, either version 3 of the License, or ++ * (at your option) any later version. ++ * ++ * GRUB is distributed in the hope that it will be useful, ++ * but WITHOUT ANY WARRANTY; without even the implied warranty of ++ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ++ * GNU General Public License for more details. ++ * ++ * You should have received a copy of the GNU General Public License ++ * along with GRUB. If not, see . ++ */ ++ ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++ ++#define GRUB_EFI_MMAP_NR_SLACK_SLOTS 8 ++ ++#define GRUB_ADDRESS_TYPE_SYSRAM 1 ++#define GRUB_ADDRESS_TYPE_RESERVED 2 ++#define GRUB_ADDRESS_TYPE_ACPI 3 ++#define GRUB_ADDRESS_TYPE_NVS 4 ++#define GRUB_ADDRESS_TYPE_PMEM 5 ++ ++#define GRUB_EFI_LOONGSON_BPI_TABLE_GUID \ ++ { 0x4660f721, 0x2ec5, 0x416a, \ ++ { 0x89, 0x9a, 0x43, 0x18, 0x02, 0x50, 0xa0, 0xc9 } \ ++ } ++ ++#define GRUB_EFI_LARCH_SCREEN_INFO_GUID \ ++ { 0x07fd51a6, 0X9532, 0X926f, \ ++ { 0X51, 0Xdc, 0X6a, 0X63, 0X60, 0X2f, 0X84, 0Xb4 } \ ++ } ++ ++#define GRUB_EFI_SCREEN_INFO_GUID \ ++ { 0xe03fc20a, 0x85dc, 0x406e, \ ++ { 0xb9, 0x0e, 0x4a, 0xb5, 0x02, 0x37, 0x1d, 0x95 } \ ++ } ++ ++static struct grub_relocator *relocator; ++static grub_efi_guid_t compat_screen_info_guid = GRUB_EFI_LARCH_SCREEN_INFO_GUID; ++static grub_efi_guid_t screen_info_guid = GRUB_EFI_SCREEN_INFO_GUID; ++ ++void grub_linux_loongarch_elf_relocator_unload (void) ++{ ++ grub_relocator_unload (relocator); ++} ++ ++static void find_bits(unsigned long mask, grub_efi_uint8_t *pos, grub_efi_uint8_t *size) ++{ ++ grub_efi_uint8_t first, len; ++ ++ first = 0; ++ len = 0; ++ ++ if (mask) { ++ while (!(mask & 0x1)) { ++ mask = mask >> 1; ++ first++; ++ } ++ ++ while (mask & 0x1) { ++ mask = mask >> 1; ++ len++; ++ } ++ } ++ ++ *pos = first; ++ *size = len; ++} ++ ++static void ++setup_pixel_info(struct screen_info *si, grub_efi_uint32_t pixels_per_scan_line, ++ struct grub_efi_gop_pixel_bitmask pixel_info, int pixel_format) ++{ ++ if (pixel_format == GRUB_EFI_GOT_RGBA8) { ++ si->lfb_depth = 32; ++ si->lfb_linelength = pixels_per_scan_line * 4; ++ si->red_size = 8; ++ si->red_pos = 0; ++ si->green_size = 8; ++ si->green_pos = 8; ++ si->blue_size = 8; ++ si->blue_pos = 16; ++ si->rsvd_size = 8; ++ si->rsvd_pos = 24; ++ } else if (pixel_format == GRUB_EFI_GOT_BGRA8) { ++ si->lfb_depth = 32; ++ si->lfb_linelength = pixels_per_scan_line * 4; ++ si->red_size = 8; ++ si->red_pos = 16; ++ si->green_size = 8; ++ si->green_pos = 8; ++ si->blue_size = 8; ++ si->blue_pos = 0; ++ si->rsvd_size = 8; ++ si->rsvd_pos = 24; ++ } else if (pixel_format == GRUB_EFI_GOT_BITMASK) { ++ find_bits(pixel_info.r, &si->red_pos, &si->red_size); ++ find_bits(pixel_info.g, &si->green_pos, ++ &si->green_size); ++ find_bits(pixel_info.b, &si->blue_pos, &si->blue_size); ++ find_bits(pixel_info.a, &si->rsvd_pos, ++ &si->rsvd_size); ++ si->lfb_depth = si->red_size + si->green_size + ++ si->blue_size + si->rsvd_size; ++ si->lfb_linelength = (pixels_per_scan_line * si->lfb_depth) / 8; ++ } else { ++ si->lfb_depth = 4; ++ si->lfb_linelength = si->lfb_width / 2; ++ si->red_size = 0; ++ si->red_pos = 0; ++ si->green_size = 0; ++ si->green_pos = 0; ++ si->blue_size = 0; ++ si->blue_pos = 0; ++ si->rsvd_size = 0; ++ si->rsvd_pos = 0; ++ } ++} ++ ++static struct screen_info *alloc_screen_info(void) ++{ ++ grub_efi_status_t status; ++ grub_efi_boot_services_t *b; ++ struct screen_info *si; ++ ++ b = grub_efi_system_table->boot_services; ++ status = efi_call_3 (b->allocate_pool, GRUB_EFI_RUNTIME_SERVICES_DATA, ++ sizeof(*si), (void**)&si); ++ if (status != GRUB_EFI_SUCCESS) ++ return NULL; ++ ++ grub_memset ((void *)si, 0, sizeof(struct screen_info)); ++ ++ status = b->install_configuration_table (&compat_screen_info_guid, si); ++ if (status != GRUB_EFI_SUCCESS) ++ goto free_mem; ++ ++ status = b->install_configuration_table (&screen_info_guid, si); ++ if (status == GRUB_EFI_SUCCESS) { ++ return si; ++ } ++ ++free_table: ++ b->install_configuration_table (&compat_screen_info_guid, NULL); ++free_mem: ++ efi_call_1 (b->free_pool, si); ++ ++ return NULL; ++} ++ ++static struct screen_info *setup_screen_info(void) ++{ ++ grub_efi_boot_services_t *b; ++ grub_efi_handle_t gop_handle; ++ struct screen_info *si = NULL; ++ struct grub_efi_gop *gop, *first_gop; ++ grub_efi_handle_t *handles; ++ grub_efi_uintn_t num_handles, i; ++ grub_efi_guid_t graphics_output_guid = GRUB_EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID; ++ grub_efi_uint16_t width, height; ++ grub_efi_uint32_t ext_lfb_base, pixels_per_scan_line; ++ grub_efi_uint64_t fb_base; ++ struct grub_efi_gop_pixel_bitmask pixel_info; ++ grub_efi_gop_pixel_format_t pixel_format; ++ ++ si = alloc_screen_info(); ++ if (!si) ++ return NULL; ++ ++ handles = grub_efi_locate_handle (GRUB_EFI_BY_PROTOCOL, ++ &graphics_output_guid, NULL, &num_handles); ++ if (!handles || num_handles == 0) ++ goto free_screen_info; ++ ++ gop = NULL; ++ first_gop = NULL; ++ ++ for (i = 0; i < num_handles; i++) ++ { ++ struct grub_efi_gop_mode *mode; ++ struct grub_efi_gop_mode_info *info = NULL; ++ grub_efi_guid_t conout_proto = GRUB_EFI_CONSOLE_OUT_DEVICE_GUID; ++ void *dummy = NULL; ++ grub_efi_uint8_t conout_found = 0; ++ grub_efi_uint64_t current_fb_base; ++ ++ gop_handle = handles[i]; ++ gop = grub_efi_open_protocol (gop_handle, &graphics_output_guid, ++ GRUB_EFI_OPEN_PROTOCOL_GET_PROTOCOL); ++ ++ dummy = grub_efi_open_protocol (gop_handle, &conout_proto, ++ GRUB_EFI_OPEN_PROTOCOL_GET_PROTOCOL); ++ if (dummy != NULL) ++ conout_found = 1; ++ ++ mode = gop->mode; ++ info = mode->info; ++ current_fb_base = mode->fb_base; ++ ++ if ((!first_gop || conout_found) && ++ info->pixel_format != GRUB_EFI_GOT_BLT_ONLY) { ++ /* ++ * Systems that use the UEFI Console Splitter may ++ * provide multiple GOP devices, not all of which are ++ * backed by real hardware. The workaround is to search ++ * for a GOP implementing the ConOut protocol, and if ++ * one isn't found, to just fall back to the first GOP. ++ */ ++ width = info->width; ++ height = info->height; ++ pixel_format = info->pixel_format; ++ pixel_info = info->pixel_bitmask; ++ pixels_per_scan_line = info->pixels_per_scanline; ++ fb_base = current_fb_base; ++ ++ /* ++ * Once we've found a GOP supporting ConOut, ++ * don't bother looking any further. ++ */ ++ first_gop = gop; ++ if (conout_found) ++ break; ++ } ++ } ++ ++ /* Did we find any GOPs? */ ++ if (!first_gop) ++ goto free_screen_info; ++ ++ /* EFI framebuffer */ ++ si->orig_video_isVGA = GRUB_VIDEO_TYPE_EFI; ++ ++ si->lfb_width = width; ++ si->lfb_height = height; ++ si->lfb_base = fb_base; ++ grub_dprintf ("loongson", "Screen info fb base: 0x%"PRIxGRUB_UINT32_T"\n", si->lfb_base); ++ ++ ext_lfb_base = (grub_uint64_t)fb_base >> 32; ++ if (ext_lfb_base) { ++ si->capabilities |= GRUB_VIDEO_CAPABILITY_64BIT_BASE; ++ si->ext_lfb_base = ext_lfb_base; ++ } ++ si->pages = 1; ++ ++ setup_pixel_info(si, pixels_per_scan_line, pixel_info, pixel_format); ++ ++ si->lfb_size = si->lfb_linelength * si->lfb_height; ++ si->capabilities |= GRUB_VIDEO_CAPABILITY_SKIP_QUIRKS; ++ ++ return si; ++ ++free_screen_info: ++ b = grub_efi_system_table->boot_services; ++ b->install_configuration_table (&compat_screen_info_guid, NULL); ++ b->install_configuration_table (&screen_info_guid, NULL); ++ if (si) ++ efi_call_1 (b->free_pool, si); ++ ++ grub_dprintf ("loongson", "No screen info\n"); ++ return NULL; ++} ++ ++static grub_err_t ++allocate_memmap_and_exit_boot (struct linux_loongarch64_kernel_params *kernel_params) ++{ ++ grub_err_t err; ++ grub_efi_status_t status; ++ grub_efi_uintn_t mmap_size, desc_size, size; ++ grub_efi_uint32_t desc_version; ++ grub_efi_memory_descriptor_t *mmap_buf; ++ grub_efi_boot_services_t *b; ++ struct efi_boot_memmap *m, tmp; ++ struct efi_initrd *tbl = NULL; ++ grub_efi_guid_t boot_memmap_guid = GRUB_EFI_BOOT_MEMMAP_GUID; ++ grub_efi_guid_t initrd_media_guid = GRUB_EFI_INITRD_MEDIA_GUID; ++ ++ setup_screen_info(); ++ ++ b = grub_efi_system_table->boot_services; ++ ++ grub_dprintf ("loongson", "ramdisk_addr:0x%"PRIxGRUB_UINT64_T", \ ++ size:0x%"PRIxGRUB_UINT64_T"\n", ++ kernel_params->ramdisk_addr, ++ kernel_params->ramdisk_size); ++#if 0 ++ char string[64]; ++ ++ /* Set initrd info to cmdline*/ ++ if (kernel_params->ramdisk_addr && kernel_params->ramdisk_size) ++ { ++ grub_printf ( "Initrd @ %p-%p\n", ++ (void *) kernel_params->ramdisk_addr, ++ (void *) (kernel_params->ramdisk_addr + kernel_params->ramdisk_size)); ++ /* initrd */ ++ grub_snprintf (string, ++ sizeof (GRUB_INITRD_STRING), ++ "initrd=0x%lx,0x%lx", ++ ((grub_uint64_t) kernel_params->ramdisk_addr & 0xffffffff), ++ (grub_uint64_t) kernel_params->ramdisk_size); ++ *(char*) ((grub_addr_t) kernel_params->linux_args + kernel_params->ramdisk_args_len - 2) = ' '; ++ grub_memcpy ((char*) ((grub_addr_t) kernel_params->linux_args + kernel_params->ramdisk_args_len - 1), ++ string, sizeof (GRUB_INITRD_STRING)); ++ } ++#else ++ /* Set initrd info to system table*/ ++ if (kernel_params->ramdisk_addr && kernel_params->ramdisk_size) ++ { ++ tbl = grub_efi_allocate_any_pages (GRUB_EFI_BYTES_TO_PAGES (sizeof(struct efi_initrd))); ++ if (!tbl) ++ return grub_error (GRUB_ERR_IO, "cannot allocate tbl memory"); ++ tbl->base = kernel_params->ramdisk_addr; ++ tbl->size = kernel_params->ramdisk_size; ++ ++ status = b->install_configuration_table (&initrd_media_guid, tbl); ++ if (status != GRUB_EFI_SUCCESS) { ++ grub_error (GRUB_ERR_IO, "failed to install initrd media"); ++ goto free_tbl; ++ } ++ } ++#endif ++ ++ tmp.map_size = 0; ++ status = grub_efi_get_memory_map (&tmp.map_size, NULL, &tmp.map_key, ++ &tmp.desc_size, &tmp.desc_ver); ++ if (status != 0) { ++ grub_error (GRUB_ERR_IO, "cannot get memory map"); ++ goto uninstall_initrd_table; ++ } ++ size = tmp.map_size + tmp.desc_size * GRUB_EFI_MMAP_NR_SLACK_SLOTS; ++ m = grub_efi_allocate_any_pages (GRUB_EFI_BYTES_TO_PAGES (sizeof(*m) + size)); ++ if (!m) { ++ grub_error (GRUB_ERR_IO, "cannot allocate m memory"); ++ goto uninstall_initrd_table; ++ } ++ ++ status = b->install_configuration_table (&boot_memmap_guid, m); ++ if (status != GRUB_EFI_SUCCESS) { ++ grub_error (GRUB_ERR_IO, "failed to install boot memmap"); ++ goto free_m; ++ } ++ ++ m->buff_size = m->map_size = size; ++ if (grub_efi_get_memory_map (&m->map_size, m->map, ++ &m->map_key, &m->desc_size, ++ &m->desc_ver) <= 0) ++ { ++ grub_error (GRUB_ERR_IO, "cannot get EFI memory map"); ++ goto uninstall_mem_table; ++ } ++ ++ mmap_size = grub_efi_find_mmap_size (); ++ if (! mmap_size) ++ goto uninstall_mem_table; ++ ++ mmap_buf = grub_efi_allocate_any_pages (GRUB_EFI_BYTES_TO_PAGES (mmap_size)); ++ err = grub_efi_finish_boot_services (&mmap_size, mmap_buf, NULL, ++ &desc_size, &desc_version); ++ if (err) { ++ grub_error (GRUB_ERR_IO, "failed to finish boot services"); ++ goto free_map; ++ } ++ ++ return 0; ++ ++free_map: ++ if (mmap_buf) ++ grub_efi_free_pages ((grub_addr_t) mmap_buf, ++ GRUB_EFI_BYTES_TO_PAGES (mmap_size)); ++ ++uninstall_mem_table: ++ b->install_configuration_table (&boot_memmap_guid, NULL); ++ ++free_m: ++ if (m) ++ grub_efi_free_pages ((grub_addr_t) m, ++ GRUB_EFI_BYTES_TO_PAGES (sizeof(*m) + size)); ++ ++uninstall_initrd_table: ++ if (kernel_params->ramdisk_addr && kernel_params->ramdisk_size) ++ b->install_configuration_table (&initrd_media_guid, NULL); ++ ++free_tbl: ++ if (kernel_params->ramdisk_addr && kernel_params->ramdisk_size) { ++ if (tbl) ++ grub_efi_free_pages ((grub_addr_t) tbl, ++ GRUB_EFI_BYTES_TO_PAGES (sizeof(struct efi_initrd))); ++ } ++ ++ return grub_error(GRUB_ERR_BAD_OS, "failed to V40 boot"); ++} ++ ++static grub_err_t ++allocate_fdt_and_exit_boot (struct linux_loongarch64_kernel_params *kernel_params) ++{ ++ int node, retval; ++ grub_err_t err; ++ unsigned int size; ++ grub_efi_uintn_t mmap_size; ++ grub_efi_uintn_t desc_size; ++ grub_efi_uint32_t desc_version; ++ grub_efi_memory_descriptor_t *mmap_buf; ++ ++ size = GRUB_FDT_EMPTY_TREE_SZ + FDT_ADDR_SIZE_EXTRA + GRUB_EFI_LINUX_FDT_EXTRA_SPACE; ++ ++ kernel_params->fdt = grub_efi_allocate_any_pages (GRUB_EFI_BYTES_TO_PAGES (size)); ++ if (!kernel_params->fdt) ++ return GRUB_ERR_OUT_OF_MEMORY; ++ ++ grub_fdt_create_empty_tree (kernel_params->fdt, size); ++ grub_fdt_set_prop32 (kernel_params->fdt, 0, FDT_ADDR_CELLS_STRING, 2); ++ grub_fdt_set_prop32 (kernel_params->fdt, 0, FDT_SIZE_CELLS_STRING, 2); ++ ++ node = grub_fdt_find_subnode (kernel_params->fdt, 0, "chosen"); ++ if (node < 0) ++ node = grub_fdt_add_subnode (kernel_params->fdt, 0, "chosen"); ++ if (node < 1) ++ goto failure; ++ ++ grub_dprintf ("loongson", "command_line %s, len %ld\n", (char *)kernel_params->linux_args, ++ grub_strlen(kernel_params->linux_args) + 1); ++ if ((kernel_params->linux_args != NULL) && (grub_strlen(kernel_params->linux_args) > 0)) { ++ retval = grub_fdt_set_prop (kernel_params->fdt, node, "bootargs", kernel_params->linux_args, ++ grub_strlen(kernel_params->linux_args) + 1); ++ if (retval) ++ goto failure; ++ } ++ ++ /* Set initrd info */ ++ if (kernel_params->ramdisk_addr && kernel_params->ramdisk_size) ++ { ++ grub_dprintf ("linux", "Initrd @ %p-%p\n", ++ (void *) kernel_params->ramdisk_addr, ++ (void *) (kernel_params->ramdisk_addr + kernel_params->ramdisk_size)); ++ ++ retval = grub_fdt_set_prop64 (kernel_params->fdt, node, "linux,initrd-start", ++ kernel_params->ramdisk_addr); ++ if (retval) ++ goto failure; ++ retval = grub_fdt_set_prop64 (kernel_params->fdt, node, "linux,initrd-end", ++ (kernel_params->ramdisk_addr + kernel_params->ramdisk_size)); ++ if (retval) ++ goto failure; ++ } ++ ++ node = grub_fdt_find_subnode (kernel_params->fdt, 0, "chosen"); ++ retval = grub_fdt_set_prop64 (kernel_params->fdt, node, "linux,uefi-system-table", ++ (grub_uint64_t)grub_efi_system_table); ++ if (retval) ++ goto failure; ++ ++ mmap_size = grub_efi_find_mmap_size (); ++ if (! mmap_size) ++ return grub_errno; ++ mmap_buf = grub_efi_allocate_any_pages (GRUB_EFI_BYTES_TO_PAGES (mmap_size)); ++ if (! mmap_buf) ++ return grub_error (GRUB_ERR_IO, "cannot allocate memory map"); ++ err = grub_efi_finish_boot_services (&mmap_size, mmap_buf, NULL, ++ &desc_size, &desc_version); ++ if (err) ++ return err; ++ ++ if (!mmap_buf || !mmap_size || !desc_size) ++ return GRUB_ERR_BAD_ARGUMENT; ++ ++ retval = grub_fdt_set_prop64 (kernel_params->fdt, node, "linux,uefi-mmap-start", ++ (grub_uint64_t)mmap_buf); ++ if (retval) ++ goto failure; ++ ++ retval = grub_fdt_set_prop32 (kernel_params->fdt, node, "linux,uefi-mmap-size", ++ mmap_size); ++ if (retval) ++ goto failure; ++ ++ retval = grub_fdt_set_prop32 (kernel_params->fdt, node, "linux,uefi-mmap-desc-size", ++ desc_size); ++ if (retval) ++ goto failure; ++ ++ retval = grub_fdt_set_prop32 (kernel_params->fdt, node, "linux,uefi-mmap-desc-ver", ++ desc_version); ++ if (retval) ++ goto failure; ++ ++ return GRUB_ERR_NONE; ++ ++failure: ++ if (!kernel_params->fdt) { ++ return GRUB_ERR_BAD_OS; ++ } ++ grub_efi_free_pages ((grub_addr_t) kernel_params->fdt, ++ GRUB_EFI_BYTES_TO_PAGES (grub_fdt_get_totalsize (kernel_params->fdt))); ++ kernel_params->fdt = NULL; ++ return grub_error(GRUB_ERR_BAD_OS, "failed to install/update FDT"); ++} ++ ++static void ++grub_linux_loongarch_elf_make_argv (struct linux_loongarch64_kernel_params *kernel_params) ++{ ++ static void* linux_args_addr; ++ int size; ++ grub_uint64_t *linux_argv; ++ char *args, *p, *linux_args; ++ int i, argc; ++ grub_err_t err; ++ ++ argc = kernel_params->linux_argc; ++ args = kernel_params->linux_args; ++ ++ /* new size */ ++ p = args; ++ size = (argc + 3 + 1) * sizeof (grub_uint64_t); /* orig arguments */ ++ for (i = 0; i < argc; i++) ++ { ++ size += ALIGN_UP (grub_strlen (p) + 1, 4); ++ p += grub_strlen (p) + 1; ++ } ++ ++ if (kernel_params->ramdisk_addr && kernel_params->ramdisk_size) ++ { ++ size += ALIGN_UP (sizeof (GRUB_RD_START_STRING), 4) \ ++ + ALIGN_UP (sizeof (GRUB_RD_SIZE_STRING), 4) \ ++ + ALIGN_UP (sizeof (GRUB_INITRD_STRING), 4); ++ } ++ size = ALIGN_UP (size, 8); ++ ++ /* alloc memory */ ++ linux_args_addr = grub_linux_loongarch_alloc_virtual_mem_align (size, 8, &err); ++ ++ linux_argv = linux_args_addr; ++ linux_args = (char *)(linux_argv + (argc + 1 + 3)); ++ p = args; ++ for (i = 0; i < argc; i++) ++ { ++ grub_memcpy (linux_args, p, grub_strlen (p) + 1); ++ *linux_argv = (grub_uint64_t) (grub_addr_t) linux_args; ++ linux_argv++; ++ linux_args += ALIGN_UP (grub_strlen (p) + 1, 4); ++ p += grub_strlen (p) + 1; ++ } ++ ++ if (kernel_params->ramdisk_addr && kernel_params->ramdisk_size) ++ { ++ /* rd_start */ ++ grub_snprintf (linux_args, ++ sizeof (GRUB_RD_START_STRING), ++ "rd_start=0x%lx", ++ (grub_uint64_t) kernel_params->ramdisk_addr); ++ *linux_argv = (grub_uint64_t) (grub_addr_t) linux_args; ++ linux_argv++; ++ linux_args += ALIGN_UP (sizeof (GRUB_RD_START_STRING), 4); ++ kernel_params->linux_argc++; ++ ++ /* rd_size */ ++ grub_snprintf (linux_args, ++ sizeof (GRUB_RD_SIZE_STRING), ++ "rd_size=0x%lx", ++ (grub_uint64_t) kernel_params->ramdisk_size); ++ *linux_argv = (grub_uint64_t) (grub_addr_t) linux_args; ++ linux_argv++; ++ linux_args += ALIGN_UP (sizeof (GRUB_RD_SIZE_STRING), 4); ++ kernel_params->linux_argc++; ++ ++ /* initrd */ ++ grub_snprintf (linux_args, ++ sizeof (GRUB_INITRD_STRING), ++ "initrd=0x%lx,0x%lx", ++ ((grub_uint64_t) kernel_params->ramdisk_addr & 0xffffffff), ++ (grub_uint64_t) kernel_params->ramdisk_size); ++ *linux_argv = (grub_uint64_t) (grub_addr_t) linux_args; ++ linux_argv++; ++ linux_args += ALIGN_UP (sizeof (GRUB_INITRD_STRING), 4); ++ kernel_params->linux_argc++; ++ } ++ ++ /* Reserve space for initrd arguments. */ ++ *linux_argv = 0; ++ ++ grub_free (kernel_params->linux_args); ++ kernel_params->linux_argv = (grub_addr_t) linux_args_addr; ++} ++ ++grub_err_t ++grub_linux_loongarch_elf_linux_boot_image (struct linux_loongarch64_kernel_params ++ *kernel_params) ++{ ++ struct boot_params_interface *boot_params = NULL; ++ struct grub_relocator64_state state; ++ grub_err_t err; ++ ++ /* linux kernel type is ELF */ ++ grub_memset (&state, 0, sizeof (state)); ++ ++ state.jumpreg = 1; ++ state.gpr[1] = kernel_params->kernel_addr; /* ra */ ++ if (grub_linux_loongarch_elf_get_boot_params (&boot_params) == 0) ++ { ++ grub_dprintf ("loongson", "V40 boot\n"); ++ if (allocate_memmap_and_exit_boot(kernel_params) != GRUB_ERR_NONE) ++ return grub_errno; ++ state.gpr[4] = 1 << FLAGS_EFI_SUPPORT_BIT; /* a0 = flag */ ++ state.gpr[5] = (grub_uint64_t)kernel_params->linux_args; /* a1 = cmdline */ ++ state.gpr[6] = (grub_uint64_t)grub_efi_system_table; /* a2 = system_table */ ++ } else { ++ grub_dprintf ("loongson", "BPI boot\n"); ++ grub_linux_loongarch_elf_make_argv (kernel_params); ++ state.gpr[4] = kernel_params->linux_argc; /* a0 = argc */ ++ state.gpr[5] = kernel_params->linux_argv; /* a1 = args */ ++ state.gpr[6] = (grub_uint64_t) boot_params; /* a2 = envp */ ++ err = grub_linux_loongarch_elf_boot_params (boot_params); ++ if (err) ++ return err; ++ } ++ ++ /* Boot the ELF kernel */ ++ grub_relocator64_boot (relocator, state); ++ ++ return GRUB_ERR_NONE; ++} ++ ++void* ++grub_linux_loongarch_alloc_virtual_mem_addr (grub_addr_t addr, ++ grub_size_t size, ++ grub_err_t *err) ++{ ++ relocator = grub_relocator_new (); ++ if (!relocator) ++ return NULL; ++ ++ grub_relocator_chunk_t ch; ++ *err = grub_relocator_alloc_chunk_addr (relocator, &ch, ++ grub_vtop ((void *) addr), ++ size); ++ if (*err) ++ return NULL; ++ return get_virtual_current_address (ch); ++} ++ ++void* ++grub_linux_loongarch_alloc_virtual_mem_align (grub_size_t size, ++ grub_size_t align, ++ grub_err_t *err) ++{ ++ grub_relocator_chunk_t ch; ++ ++ *err = grub_relocator_alloc_chunk_align (relocator, &ch, ++ 0x3000000, (0xffffffff - size) + 1, ++ size, align, ++ GRUB_RELOCATOR_PREFERENCE_LOW, 0); ++ return get_virtual_current_address (ch); ++} ++ ++void* ++grub_linux_loongarch_alloc_initrd_mem_align (grub_size_t size, ++ grub_size_t align, ++ grub_err_t *err) ++{ ++ grub_relocator_chunk_t ch; ++ ++ /* Firstly try to allocate from memory higher than 256MB */ ++ *err = grub_relocator_alloc_chunk_align (relocator, &ch, ++ 0x10000000, (0xffffffff - size) + 1, size, align, ++ GRUB_RELOCATOR_PREFERENCE_LOW, 0); ++ if (*err != GRUB_ERR_NONE) ++ { ++ /* Failed, try to allocate in range 0 ~ 256MB */ ++ *err = grub_relocator_alloc_chunk_align (relocator, &ch, ++ 0, (0xfffffff - size) + 1, size, align, ++ GRUB_RELOCATOR_PREFERENCE_HIGH, 0); ++ } ++ return get_virtual_current_address (ch); ++} ++ ++int ++grub_linux_loongarch_elf_get_boot_params (struct boot_params_interface **boot_params) ++{ ++ grub_efi_configuration_table_t *tables; ++ grub_efi_guid_t bpi_guid = GRUB_EFI_LOONGSON_BPI_TABLE_GUID; ++ unsigned int i; ++ int found = 0; ++ ++ /* Look for Loongson BPI in UEFI config tables. */ ++ tables = grub_efi_system_table->configuration_table; ++ ++ for (i = 0; i < grub_efi_system_table->num_table_entries; i++) ++ if (grub_memcmp (&tables[i].vendor_guid, &bpi_guid, sizeof (bpi_guid)) == 0) ++ { ++ *boot_params = tables[i].vendor_table; ++ char *p = (char*) &((*boot_params)->signature); ++ if (grub_strncmp (p, "BPI", 3) == 0) ++ { ++ found = 1; ++ break; ++ } ++ } ++ return found; ++} ++ ++static grub_uint8_t ++grub_kernel_update_checksum (const grub_uint8_t *buffer, grub_efi_uintn_t length) ++{ ++ grub_uint8_t sum; ++ grub_efi_uintn_t count; ++ ++ for (sum = 0, count = 0; count < length; count++) ++ { ++ sum = (grub_uint8_t) (sum + *(buffer + count)); ++ } ++ ++ return (grub_uint8_t) (0x100 - sum); ++} ++ ++static grub_uint32_t ++grub_efi_loongarch64_memmap_sort (struct memmap array[], ++ grub_uint32_t length, ++ struct loongsonlist_mem_map* bpmem, ++ grub_uint32_t index, ++ grub_uint32_t memtype) ++{ ++ grub_uint64_t tempmemsize = 0; ++ grub_uint32_t j = 0; ++ grub_uint32_t t = 0; ++ ++ for(j = 0; j < length;) ++ { ++ tempmemsize = array[j].mem_size; ++ for(t = j + 1; t < length; t++) ++ { ++ if(array[j].mem_start + tempmemsize == array[t].mem_start) ++ { ++ tempmemsize += array[t].mem_size; ++ } ++ else ++ { ++ break; ++ } ++ } ++ bpmem->map[index].mem_type = memtype; ++ bpmem->map[index].mem_start = array[j].mem_start; ++ bpmem->map[index].mem_size = tempmemsize; ++ grub_dprintf ("loongson", "map[%d]:type %"PRIuGRUB_UINT32_T", start 0x%" ++ PRIxGRUB_UINT64_T", end 0x%"PRIxGRUB_UINT64_T"\n", ++ index, ++ bpmem->map[index].mem_type, ++ bpmem->map[index].mem_start, ++ bpmem->map[index].mem_start+ bpmem->map[index].mem_size ++ ); ++ j = t; ++ index++; ++ } ++ return index; ++} ++ ++grub_err_t ++grub_linux_loongarch_elf_boot_params (struct boot_params_interface *boot_params) ++{ ++ grub_int8_t checksum = 0; ++ grub_err_t err; ++ ++ struct loongsonlist_mem_map *loongson_mem_map = NULL; ++ struct _extention_list_hdr * listpointer = NULL; ++ grub_uint32_t tmp_index = 0; ++ grub_efi_memory_descriptor_t * lsdesc = NULL; ++ ++ grub_uint32_t free_index = 0; ++ grub_uint32_t reserve_index = 0; ++ grub_uint32_t acpi_table_index = 0; ++ grub_uint32_t acpi_nvs_index = 0; ++ ++ grub_efi_uintn_t mmap_size; ++ grub_efi_uintn_t desc_size; ++ grub_efi_memory_descriptor_t *mmap_buf; ++ ++ struct memmap reserve_mem[GRUB_LOONGSON3_BOOT_MEM_MAP_MAX]; ++ struct memmap free_mem[GRUB_LOONGSON3_BOOT_MEM_MAP_MAX]; ++ struct memmap acpi_table_mem[GRUB_LOONGSON3_BOOT_MEM_MAP_MAX]; ++ struct memmap acpi_nvs_mem[GRUB_LOONGSON3_BOOT_MEM_MAP_MAX]; ++ ++ grub_memset (reserve_mem, 0, sizeof(struct memmap) * GRUB_LOONGSON3_BOOT_MEM_MAP_MAX); ++ grub_memset (free_mem, 0, sizeof(struct memmap) * GRUB_LOONGSON3_BOOT_MEM_MAP_MAX); ++ grub_memset (acpi_table_mem, 0, sizeof(struct memmap) * GRUB_LOONGSON3_BOOT_MEM_MAP_MAX); ++ grub_memset (acpi_nvs_mem, 0, sizeof(struct memmap) * GRUB_LOONGSON3_BOOT_MEM_MAP_MAX); ++ ++ /* Check extlist headers */ ++ listpointer = boot_params->extlist; ++ for( ;listpointer != NULL; listpointer = listpointer->next) ++ { ++ char *pl= (char *)&(listpointer->signature); ++ if(grub_strncmp(pl, "MEM", 3) == 0) ++ { ++ loongson_mem_map = (struct loongsonlist_mem_map *)listpointer; ++ break; ++ } ++ } ++ ++ mmap_size = grub_efi_find_mmap_size (); ++ if (! mmap_size) ++ return grub_errno; ++ mmap_buf = grub_efi_allocate_any_pages (GRUB_EFI_BYTES_TO_PAGES (mmap_size)); ++ if (! mmap_buf) ++ return grub_error (GRUB_ERR_IO, "cannot allocate memory map"); ++ ++ err = grub_efi_finish_boot_services (&mmap_size, mmap_buf, NULL, ++ &desc_size, NULL); ++ if (err) ++ return err; ++ ++ if (!mmap_buf || !mmap_size || !desc_size) ++ return -1; ++ ++ /* ++ According to UEFI SPEC,mmap_buf is the accurate Memory Map array \ ++ now we can fill platform specific memory structure. ++ */ ++ for (lsdesc = mmap_buf; lsdesc < (grub_efi_memory_descriptor_t *)((char *)mmap_buf + mmap_size); ++ lsdesc = (grub_efi_memory_descriptor_t *)((char *)lsdesc + desc_size)) ++ { ++ /* System RAM */ ++ if((lsdesc->type != GRUB_EFI_ACPI_RECLAIM_MEMORY) && \ ++ (lsdesc->type != GRUB_EFI_ACPI_MEMORY_NVS) && \ ++ (lsdesc->type != GRUB_EFI_RUNTIME_SERVICES_DATA) && \ ++ (lsdesc->type != GRUB_EFI_RUNTIME_SERVICES_CODE) && \ ++ (lsdesc->type != GRUB_EFI_RESERVED_MEMORY_TYPE) && \ ++ (lsdesc->type != GRUB_EFI_PAL_CODE)) ++ { ++ free_mem[free_index].mem_type = GRUB_ADDRESS_TYPE_SYSRAM; ++ free_mem[free_index].mem_start = (lsdesc->physical_start) & GRUB_EFI_MAX_PHY_ADDRESS; ++ free_mem[free_index].mem_size = lsdesc->num_pages * GRUB_EFI_PAGE_SIZE; ++ free_index++; ++ ++ /*ACPI*/ ++ }else if((lsdesc->type == GRUB_EFI_ACPI_RECLAIM_MEMORY)){ ++ acpi_table_mem[acpi_table_index].mem_type = GRUB_ADDRESS_TYPE_ACPI; ++ acpi_table_mem[acpi_table_index].mem_start = (lsdesc->physical_start) & GRUB_EFI_MAX_PHY_ADDRESS; ++ acpi_table_mem[acpi_table_index].mem_size = lsdesc->num_pages * GRUB_EFI_PAGE_SIZE; ++ acpi_table_index++; ++ }else if((lsdesc->type == GRUB_EFI_ACPI_MEMORY_NVS)){ ++ acpi_nvs_mem[acpi_nvs_index].mem_type = GRUB_ADDRESS_TYPE_NVS; ++ acpi_nvs_mem[acpi_nvs_index].mem_start = (lsdesc->physical_start) & GRUB_EFI_MAX_PHY_ADDRESS; ++ acpi_nvs_mem[acpi_nvs_index].mem_size = lsdesc->num_pages * GRUB_EFI_PAGE_SIZE; ++ acpi_nvs_index++; ++ ++ /* Reserve */ ++ }else{ ++ reserve_mem[reserve_index].mem_type = GRUB_ADDRESS_TYPE_RESERVED; ++ reserve_mem[reserve_index].mem_start = (lsdesc->physical_start) & GRUB_EFI_MAX_PHY_ADDRESS; ++ reserve_mem[reserve_index].mem_size = lsdesc->num_pages * GRUB_EFI_PAGE_SIZE; ++ reserve_index++; ++ } ++ } ++ ++ tmp_index = loongson_mem_map->map_count; ++ /*System RAM Sort*/ ++ tmp_index = grub_efi_loongarch64_memmap_sort(free_mem, ++ free_index, ++ loongson_mem_map, ++ tmp_index, ++ GRUB_ADDRESS_TYPE_SYSRAM); ++ /*ACPI Sort*/ ++ tmp_index = grub_efi_loongarch64_memmap_sort(acpi_table_mem, ++ acpi_table_index, ++ loongson_mem_map, ++ tmp_index, ++ GRUB_ADDRESS_TYPE_ACPI); ++ tmp_index = grub_efi_loongarch64_memmap_sort(acpi_nvs_mem, ++ acpi_nvs_index, ++ loongson_mem_map, ++ tmp_index, ++ GRUB_ADDRESS_TYPE_NVS); ++ ++ /*Reserve Sort*/ ++ { ++ grub_uint64_t loongarch_addr; ++ asm volatile ("csrrd %0, 0x181" : "=r" (loongarch_addr)); ++ if ((loongarch_addr & 0xff00000000000000) == 0x9000000000000000) ++ tmp_index = grub_efi_loongarch64_memmap_sort(reserve_mem, ++ reserve_index, ++ loongson_mem_map, ++ tmp_index, ++ GRUB_ADDRESS_TYPE_RESERVED); ++ else ++ tmp_index = grub_efi_loongarch64_memmap_sort(reserve_mem, ++ reserve_index, ++ loongson_mem_map, ++ tmp_index, ++ GRUB_ADDRESS_TYPE_RESERVED + 1); ++ } ++ loongson_mem_map->map_count = tmp_index; ++ loongson_mem_map->header.checksum = 0; ++ ++ checksum = grub_kernel_update_checksum ((grub_uint8_t *) loongson_mem_map, ++ loongson_mem_map->header.length); ++ loongson_mem_map->header.checksum = checksum; ++ ++ return grub_errno; ++} +diff --git a/grub-core/loader/loongarch64/linux.c b/grub-core/loader/loongarch64/linux.c +index 7594ca2..8dae4fe 100644 +--- a/grub-core/loader/loongarch64/linux.c ++++ b/grub-core/loader/loongarch64/linux.c +@@ -1,7 +1,6 @@ +-/* linux.c - boot Linux */ + /* + * GRUB -- GRand Unified Bootloader +- * Copyright (C) 2003,2004,2005,2007,2009,2010,2017 Free Software Foundation, Inc. ++ * Copyright (C) 2021 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by +@@ -17,346 +16,76 @@ + * along with GRUB. If not, see . + */ + +-#include +-#include +-#include +-#include + #include +-#include +-#include + #include + #include +-#include +-#include +-#include +-#include + #include + #include + #include +-#include ++#include + #include + + GRUB_MOD_LICENSE ("GPLv3+"); + +-#pragma GCC diagnostic ignored "-Wcast-align" +- +-typedef unsigned long size_t; ++#define INITRD_MAX_ADDRESS_OFFSET (32ULL * 1024 * 1024 * 1024) + +-#define FDT_ADDR_CELLS_STRING "#address-cells" +-#define FDT_SIZE_CELLS_STRING "#size-cells" +-#define FDT_ADDR_SIZE_EXTRA ((2 * grub_fdt_prop_entry_size (sizeof(grub_uint32_t))) + \ +- sizeof (FDT_ADDR_CELLS_STRING) + \ +- sizeof (FDT_SIZE_CELLS_STRING)) ++static struct linux_loongarch64_kernel_params kernel_params; + ++static grub_addr_t phys_addr; + static grub_dl_t my_mod; + static int loaded; +-static int initrd_loaded = 0; +-static grub_size_t linux_size; +- +-static struct grub_relocator *relocator; +-static grub_addr_t target_addr, entry_addr; +-static int linux_argc; +-static grub_uint8_t *linux_args_addr; +-static grub_off_t rd_addr_arg_off, rd_size_arg_off, initrd_addr_arg_off; +- +-static void *fdt; +-static int is_fdt_boot; +-static grub_addr_t initrd_start, initrd_end; +-static char *fdt_linux_args; ++static int is_bpi_boot; ++static int grub_loongarch_linux_type = GRUB_LOONGARCH_LINUX_BAD; + + static grub_err_t +-allocate_fdt_and_exit_boot (void) ++grub_linux_boot (void) + { +- int node, retval; +- grub_err_t err; +- unsigned int size; +- grub_efi_uintn_t mmap_size; +- grub_efi_uintn_t desc_size; +- grub_efi_uint32_t desc_version; +- grub_efi_memory_descriptor_t *mmap_buf; +- +- size = GRUB_FDT_EMPTY_TREE_SZ + FDT_ADDR_SIZE_EXTRA + GRUB_EFI_LINUX_FDT_EXTRA_SPACE; +- +- fdt = grub_efi_allocate_any_pages (GRUB_EFI_BYTES_TO_PAGES (size)); +- if (!fdt) +- return GRUB_ERR_OUT_OF_MEMORY; +- +- grub_fdt_create_empty_tree (fdt, size); +- grub_fdt_set_prop32 (fdt, 0, FDT_ADDR_CELLS_STRING, 2); +- grub_fdt_set_prop32 (fdt, 0, FDT_SIZE_CELLS_STRING, 2); +- +- node = grub_fdt_find_subnode (fdt, 0, "chosen"); +- if (node < 0) +- node = grub_fdt_add_subnode (fdt, 0, "chosen"); +- if (node < 1) +- goto failure; +- +- grub_dprintf ("loongson", "command_line %s, len %ld\n", +- fdt_linux_args, grub_strlen(fdt_linux_args) + 1); +- if ((fdt_linux_args != NULL) && (grub_strlen(fdt_linux_args) > 0)) { +- retval = grub_fdt_set_prop (fdt, node, "bootargs", fdt_linux_args, +- grub_strlen(fdt_linux_args) + 1); +- if (retval) +- goto failure; +- } + +- /* Set initrd info */ +- if (initrd_start && initrd_end > initrd_start) +- { +- grub_dprintf ("linux", "Initrd @ %p-%p\n", +- (void *) initrd_start, (void *) initrd_end); +- +- retval = grub_fdt_set_prop64 (fdt, node, "linux,initrd-start", +- initrd_start); +- if (retval) +- goto failure; +- retval = grub_fdt_set_prop64 (fdt, node, "linux,initrd-end", +- initrd_end); +- if (retval) +- goto failure; ++ if (grub_loongarch_linux_type == GRUB_LOONGARCH_LINUX_EFI) { ++ if (finalize_efi_params_linux (&kernel_params) != GRUB_ERR_NONE) ++ return grub_errno; ++ return (grub_arch_efi_linux_boot_image((grub_addr_t) kernel_params.kernel_addr, ++ kernel_params.kernel_size, ++ kernel_params.linux_args)); + } +- +- node = grub_fdt_find_subnode (fdt, 0, "chosen"); +- retval = grub_fdt_set_prop64 (fdt, node, "linux,uefi-system-table", +- (grub_uint64_t)grub_efi_system_table); +- if (retval) +- goto failure; +- +- mmap_size = grub_efi_find_mmap_size (); +- if (! mmap_size) +- return grub_errno; +- mmap_buf = grub_efi_allocate_any_pages (GRUB_EFI_BYTES_TO_PAGES (mmap_size)); +- if (! mmap_buf) +- return grub_error (GRUB_ERR_IO, "cannot allocate memory map"); +- err = grub_efi_finish_boot_services (&mmap_size, mmap_buf, NULL, +- &desc_size, &desc_version); +- if (err) +- return err; +- +- if (!mmap_buf || !mmap_size || !desc_size) +- return GRUB_ERR_BAD_ARGUMENT; +- +- retval = grub_fdt_set_prop64 (fdt, node, "linux,uefi-mmap-start", +- (grub_uint64_t)mmap_buf); +- if (retval) +- goto failure; +- +- retval = grub_fdt_set_prop32 (fdt, node, "linux,uefi-mmap-size", +- mmap_size); +- if (retval) +- goto failure; +- +- retval = grub_fdt_set_prop32 (fdt, node, "linux,uefi-mmap-desc-size", +- desc_size); +- if (retval) +- goto failure; +- +- retval = grub_fdt_set_prop32 (fdt, node, "linux,uefi-mmap-desc-ver", +- desc_version); +- if (retval) +- goto failure; +- +- return GRUB_ERR_NONE; +- +-failure: +- if (!fdt) { +- return GRUB_ERR_BAD_OS; ++ if (grub_loongarch_linux_type == GRUB_LOONGARCH_LINUX_ELF) { ++ return grub_linux_loongarch_elf_linux_boot_image (&kernel_params); + } +- grub_efi_free_pages ((grub_addr_t) fdt, +- GRUB_EFI_BYTES_TO_PAGES (grub_fdt_get_totalsize (fdt))); +- fdt = NULL; +- return grub_error(GRUB_ERR_BAD_OS, "failed to install/update FDT"); +-} +- +-static grub_err_t +-allocate_boot_params_and_exit_boot (void) +-{ +- grub_efi_uintn_t mmap_size; +- grub_efi_uintn_t desc_size; +- grub_efi_uint32_t desc_version; +- grub_efi_memory_descriptor_t *mmap_buf; +- grub_efi_memory_descriptor_t * lsdesc = NULL; +- grub_err_t err; +- struct boot_params_interface * boot_params; +- mem_map_v1 * mem_map_v1_table = NULL; +- unsigned long bpi_version = 0; +- grub_int8_t checksum = 0; +- grub_uint32_t tmp_index = 0; +- grub_uint32_t free_index = 0; +- grub_uint32_t reserve_index = 0; +- grub_uint32_t acpi_table_index = 0; +- grub_uint32_t acpi_nvs_index = 0; +- +- struct memmap_v1 reserve_mem_v1[GRUB_EFI_LOONGSON_MMAP_MAX]; +- struct memmap_v1 free_mem_v1[GRUB_EFI_LOONGSON_MMAP_MAX]; +- struct memmap_v1 acpi_table_mem_v1[GRUB_EFI_LOONGSON_MMAP_MAX]; +- struct memmap_v1 acpi_nvs_mem_v1[GRUB_EFI_LOONGSON_MMAP_MAX]; +- +- grub_memset(reserve_mem_v1, 0, sizeof(struct memmap_v1) * GRUB_EFI_LOONGSON_MMAP_MAX); +- grub_memset(free_mem_v1, 0, sizeof(struct memmap_v1) * GRUB_EFI_LOONGSON_MMAP_MAX); +- grub_memset(acpi_table_mem_v1, 0, sizeof(struct memmap_v1) * GRUB_EFI_LOONGSON_MMAP_MAX); +- grub_memset(acpi_nvs_mem_v1, 0, sizeof(struct memmap_v1) * GRUB_EFI_LOONGSON_MMAP_MAX); +- +- boot_params = (struct boot_params_interface *)grub_efi_loongson_get_boot_params(); +- +- ext_list * listpointer = NULL; +- /* Check extlist headers */ +- listpointer = boot_params->extlist; +- for( ;listpointer != NULL; listpointer = listpointer->next) +- { +- char *pl= (char *)&(listpointer->signature); +- if(grub_strncmp(pl, "MEM", 3) == 0) +- { +- mem_map_v1_table = (mem_map_v1 *)listpointer; +- break; +- } +- } +- +- mmap_size = grub_efi_find_mmap_size (); +- if (! mmap_size) +- return grub_errno; +- mmap_buf = grub_efi_allocate_any_pages (GRUB_EFI_BYTES_TO_PAGES (mmap_size)); +- if (! mmap_buf) +- return grub_error (GRUB_ERR_IO, "cannot allocate memory map"); +- err = grub_efi_finish_boot_services (&mmap_size, mmap_buf, NULL, +- &desc_size, &desc_version); +- if (err) +- return err; +- +- if (!mmap_buf || !mmap_size || !desc_size) +- return GRUB_ERR_BAD_ARGUMENT; +- +- char *p = (char *)&(boot_params->signature); +- bpi_version = grub_efi_get_bpi_version(p); +- grub_dprintf("loongson", "get bpi version:%ld\n", bpi_version); +- +- if (bpi_version <= GRUB_EFI_BPI_VER_V2) +- { +- /* +- According to UEFI SPEC,mmap_buf is the accurate Memory Map array \ +- now we can fill platform specific memory structure. +- */ +- for(lsdesc = mmap_buf; lsdesc < (grub_efi_memory_descriptor_t *)((char *)mmap_buf + mmap_size); +- lsdesc = (grub_efi_memory_descriptor_t *)((char *)lsdesc + desc_size)) +- { +- grub_dprintf("loongson", "type:%d, phy_start:0x%lx, phy_end:0x%lx\n", lsdesc->type, +- lsdesc->physical_start, lsdesc->physical_start + lsdesc->num_pages * GRUB_EFI_PAGE_SIZE); +- +- /* System RAM */ +- if ((lsdesc->type != GRUB_EFI_ACPI_RECLAIM_MEMORY) && \ +- (lsdesc->type != GRUB_EFI_ACPI_MEMORY_NVS) && \ +- (lsdesc->type != GRUB_EFI_RUNTIME_SERVICES_DATA) && \ +- (lsdesc->type != GRUB_EFI_RUNTIME_SERVICES_CODE) && \ +- (lsdesc->type != GRUB_EFI_RESERVED_MEMORY_TYPE) && \ +- (lsdesc->type != GRUB_EFI_PAL_CODE)) +- { +- free_mem_v1[free_index].memtype = GRUB_EFI_LOONGSON_SYSTEM_RAM; +- free_mem_v1[free_index].memstart = (lsdesc->physical_start) & GRUB_EFI_MAX_PHY_ADDRESS; +- free_mem_v1[free_index].memsize = lsdesc->num_pages * GRUB_EFI_PAGE_SIZE; +- free_index++; +- +- /*ACPI*/ +- } else if ((lsdesc->type == GRUB_EFI_ACPI_RECLAIM_MEMORY)){ +- acpi_table_mem_v1[acpi_table_index].memtype = GRUB_EFI_LOONGSON_ACPI_TABLE; +- acpi_table_mem_v1[acpi_table_index].memstart = (lsdesc->physical_start) & GRUB_EFI_MAX_PHY_ADDRESS; +- acpi_table_mem_v1[acpi_table_index].memsize = lsdesc->num_pages * GRUB_EFI_PAGE_SIZE; +- acpi_table_index++; +- } else if ((lsdesc->type == GRUB_EFI_ACPI_MEMORY_NVS)){ +- acpi_nvs_mem_v1[acpi_nvs_index].memtype = GRUB_EFI_LOONGSON_ACPI_NVS; +- acpi_nvs_mem_v1[acpi_nvs_index].memstart = (lsdesc->physical_start) & GRUB_EFI_MAX_PHY_ADDRESS; +- acpi_nvs_mem_v1[acpi_nvs_index].memsize = lsdesc->num_pages * GRUB_EFI_PAGE_SIZE; +- acpi_nvs_index++; +- +- /* Reserve */ +- } else { +- reserve_mem_v1[reserve_index].memtype = GRUB_EFI_LOONGSON_MEMORY_RESERVED; +- reserve_mem_v1[reserve_index].memstart = (lsdesc->physical_start) & GRUB_EFI_MAX_PHY_ADDRESS; +- reserve_mem_v1[reserve_index].memsize = lsdesc->num_pages * GRUB_EFI_PAGE_SIZE; +- reserve_index++; +- } +- } +- +- tmp_index = mem_map_v1_table->mapcount; +- /*System RAM Sort*/ +- tmp_index = grub_efi_loongson_memmap_v1_sort(free_mem_v1, free_index, mem_map_v1_table, +- tmp_index, GRUB_EFI_LOONGSON_SYSTEM_RAM); +- /*ACPI Sort*/ +- tmp_index = grub_efi_loongson_memmap_v1_sort(acpi_table_mem_v1, acpi_table_index, +- mem_map_v1_table, tmp_index, GRUB_EFI_LOONGSON_ACPI_TABLE); +- tmp_index = grub_efi_loongson_memmap_v1_sort(acpi_nvs_mem_v1, acpi_nvs_index, +- mem_map_v1_table, tmp_index, GRUB_EFI_LOONGSON_ACPI_NVS); +- /*Reserve Sort*/ +- { +- grub_uint64_t loongarch_addr; +- asm volatile ("csrrd %0, 0x181" : "=r" (loongarch_addr)); +- if ((loongarch_addr & 0xff00000000000000) == 0x9000000000000000) +- tmp_index = grub_efi_loongson_memmap_v1_sort(reserve_mem_v1, reserve_index, +- mem_map_v1_table, tmp_index, +- GRUB_EFI_LOONGSON_MEMORY_RESERVED); +- else +- tmp_index = grub_efi_loongson_memmap_v1_sort(reserve_mem_v1, reserve_index, +- mem_map_v1_table, tmp_index, +- GRUB_EFI_LOONGSON_MEMORY_RESERVED + 1); +- } +- mem_map_v1_table->mapcount = tmp_index; +- mem_map_v1_table->header.checksum = 0; +- +- checksum = grub_efi_loongson_grub_calculatechecksum8((grub_uint8_t *)mem_map_v1_table, +- mem_map_v1_table->header.length); +- mem_map_v1_table->header.checksum = checksum; +- } + + return GRUB_ERR_NONE; + } + + static grub_err_t +-grub_linux_boot (void) ++grub_linux_unload (void) + { +- struct grub_relocator64_state state; +- +- grub_memset (&state, 0, sizeof (state)); +- +- /* Boot the kernel. */ +- state.gpr[1] = entry_addr; +- grub_dprintf("loongson", "entry_addr is 0x%lx\n", state.gpr[1]); + +- if (is_fdt_boot == 1) +- { +- if (allocate_fdt_and_exit_boot () != GRUB_ERR_NONE) +- return grub_errno; ++ if (grub_loongarch_linux_type == GRUB_LOONGARCH_LINUX_EFI) { ++ if (kernel_params.ramdisk_addr) ++ grub_efi_free_pages ((grub_efi_physical_address_t) kernel_params.ramdisk_addr, ++ GRUB_EFI_BYTES_TO_PAGES (kernel_params.ramdisk_size)); ++ kernel_params.ramdisk_size = 0; + +- state.gpr[4] = 1 << FLAGS_EFI_SUPPORT_BIT; +- state.gpr[5] = (grub_uint64_t)fdt; +- state.gpr[6] = 0; +- } else { +- state.gpr[4] = linux_argc; +- state.gpr[5] = (grub_addr_t) linux_args_addr; +- state.gpr[6] = (grub_uint64_t)grub_efi_loongson_get_boot_params(); +- +- if (allocate_boot_params_and_exit_boot () != GRUB_ERR_NONE) +- return grub_errno; ++ if (kernel_params.kernel_addr) ++ grub_efi_free_pages ((grub_addr_t) kernel_params.kernel_addr, ++ GRUB_EFI_BYTES_TO_PAGES (kernel_params.kernel_size)); ++ kernel_params.kernel_addr = 0; + } +- grub_dprintf("loongson", "gpr[4]:%ld, gpr[5]:0x%lx, gpr[6]:0x%lx\n", +- state.gpr[4], state.gpr[5], state.gpr[6]); + +- state.jumpreg = 1; +- grub_relocator64_boot (relocator, state); +- +- return GRUB_ERR_NONE; +-} ++ if (grub_loongarch_linux_type == GRUB_LOONGARCH_LINUX_ELF) { ++ grub_free (kernel_params.linux_args); ++ kernel_params.linux_args = 0; ++ grub_linux_loongarch_elf_relocator_unload (); ++ } + +-static grub_err_t +-grub_linux_unload (void) +-{ +- grub_relocator_unload (relocator); + grub_dl_unref (my_mod); + loaded = 0; ++ grub_loongarch_linux_type = GRUB_LOONGARCH_LINUX_BAD; + + return GRUB_ERR_NONE; + } + +-static grub_err_t +-grub_linux_load64 (grub_elf_t elf, const char *filename) ++grub_err_t ++grub_linux_loongarch_elf_load_kernel (grub_elf_t elf, const char *filename) + { + Elf64_Addr base; + grub_err_t err; +@@ -365,16 +94,14 @@ grub_linux_load64 (grub_elf_t elf, const char *filename) + int flag; + + /* Linux's entry point incorrectly contains a virtual address. */ +- entry_addr = elf->ehdr.ehdr64.e_entry; +- grub_dprintf("loongson", "entry address = 0x%lx\n", entry_addr); ++ kernel_params.kernel_addr = elf->ehdr.ehdr64.e_entry; ++ kernel_params.kernel_size = grub_elf64_size (elf, &base, 0); + +- linux_size = grub_elf64_size (elf, &base, 0); +- grub_dprintf("loongson", "base = 0x%lx\n", base); +- +- if (linux_size == 0) ++ if (kernel_params.kernel_size == 0) + return grub_errno; +- target_addr = base; +- linux_size = ALIGN_UP (base + linux_size - base, 8); ++ ++ phys_addr = base; ++ kernel_params.kernel_size = ALIGN_UP (base + kernel_params.kernel_size - base, 8); + + asm volatile ("csrrd %0, 0x181" : "=r" (addr)); + if (addr & 0x1) { +@@ -382,157 +109,163 @@ grub_linux_load64 (grub_elf_t elf, const char *filename) + } else { + flag = GRUB_ELF_LOAD_FLAGS_30BITS; + base &= ~ELF64_LOADMASK; +- entry_addr &= ~ELF64_LOADMASK; ++ kernel_params.kernel_addr &= ~ELF64_LOADMASK; + } + +- relocator = grub_relocator_new (); +- if (!relocator) +- return grub_errno; +- +- { +- grub_relocator_chunk_t ch; +- err = grub_relocator_alloc_chunk_addr (relocator, &ch, +- grub_vtop ((void *) target_addr), +- linux_size); +- if (err) +- return err; +- playground = get_virtual_current_address (ch); +- } ++ playground = grub_linux_loongarch_alloc_virtual_mem_addr (phys_addr, ++ kernel_params.kernel_size, ++ &err); ++ if (playground == NULL) ++ return err; + + /* Now load the segments into the area we claimed. */ +- return grub_elf64_load (elf, filename, playground - base, flag, 0, 0); ++ return grub_elf64_load (elf, filename, playground - base, ++ flag, 0, 0); + } + + static grub_err_t + grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)), + int argc, char *argv[]) + { +- grub_elf_t elf = 0; ++ grub_file_t file = 0; ++ struct linux_arch_kernel_header lh; ++ struct boot_params_interface *boot_params = NULL; ++ grub_elf_t elf = NULL; + grub_err_t err; +- int args_size = 0; ++ grub_size_t cmdline_size; ++ int i; + + grub_dl_ref (my_mod); + +- if (argc == 0) +- { +- return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected")); +- } +- +- elf = grub_elf_open (argv[0], GRUB_FILE_TYPE_LINUX_KERNEL); +- if (! elf) +- return grub_errno; ++ /* Release the previously used memory. */ ++ grub_loader_unset (); + +- if (elf->ehdr.ehdr64.e_type != ET_EXEC) ++ if (argc == 0) + { +- grub_elf_close (elf); +- return grub_error (GRUB_ERR_UNKNOWN_OS, +- N_("this ELF file is not of the right type")); ++ grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected")); ++ goto fail; + } + +- /* Release the previously used memory. */ +- grub_loader_unset (); +- loaded = 0; +- +- if (grub_elf_is_elf64 (elf)) +- err = grub_linux_load64 (elf, argv[0]); +- else +- err = grub_error (GRUB_ERR_BAD_OS, N_("invalid arch-dependent ELF magic")); ++ file = grub_file_open (argv[0], GRUB_FILE_TYPE_LINUX_KERNEL); ++ if (!file) ++ goto fail; + +- grub_elf_close (elf); ++ kernel_params.kernel_size = grub_file_size (file); ++ grub_dprintf ("linux", "kernel file size: %" PRIuGRUB_SIZE "\n", ++ kernel_params.kernel_size); + +- if (err) +- return err; ++ if (grub_file_read (file, &lh, sizeof (lh)) < (long) sizeof (lh)) ++ return grub_errno; + +- if (grub_efi_loongson_get_boot_params() == NULL) { +- grub_size_t cmdline_size; ++ if (grub_arch_efi_linux_check_image (&lh) == GRUB_ERR_NONE) { ++ grub_loongarch_linux_type = GRUB_LOONGARCH_LINUX_EFI; ++ } + +- is_fdt_boot = 1; +- cmdline_size = grub_loader_cmdline_size (argc, argv) + sizeof (LINUX_IMAGE); +- fdt_linux_args = grub_malloc (cmdline_size); +- if (!fdt_linux_args) ++ if (grub_loongarch_linux_type != GRUB_LOONGARCH_LINUX_EFI) { ++ elf = grub_elf_file (file, argv[0]); ++ if (elf != NULL) + { +- grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory")); +- goto fail; ++ /* linux kernel type is ELF */ ++ grub_loongarch_linux_type = GRUB_LOONGARCH_LINUX_ELF; ++ if (elf->ehdr.ehdr64.e_type != ET_EXEC) ++ { ++ grub_error (GRUB_ERR_UNKNOWN_OS, ++ N_("this ELF file is not of the right type")); ++ goto fail; ++ } ++ if (elf->ehdr.ehdr64.e_machine != EM_LOONGARCH64) ++ { ++ grub_error (GRUB_ERR_BAD_OS, "invalid magic number"); ++ goto fail; ++ } ++ ++ if (grub_elf_is_elf64 (elf)) ++ { ++ err = grub_linux_loongarch_elf_load_kernel (elf, argv[0]); ++ if (err) ++ goto fail; ++ } else { ++ grub_error (GRUB_ERR_BAD_OS, N_("invalid arch-dependent ELF magic")); ++ goto fail; ++ } ++ grub_dprintf ("linux", "kernel @ %p\n", (void*) elf->ehdr.ehdr64.e_entry); + } +- grub_memcpy (fdt_linux_args, LINUX_IMAGE, sizeof (LINUX_IMAGE)); +- err = grub_create_loader_cmdline (argc, argv, +- fdt_linux_args + sizeof (LINUX_IMAGE) - 1, +- cmdline_size, +- GRUB_VERIFY_KERNEL_CMDLINE); +- if (err) +- goto fail; +- grub_dprintf("loongson", "fdt linux args:%s\n", +- fdt_linux_args + sizeof (LINUX_IMAGE) - 1); +- + } else { +- int i; +- grub_uint64_t *linux_argv; +- char *linux_args; +- +- is_fdt_boot = 0; +- /* For arguments. */ +- linux_argc = argc; +- /* Main arguments. */ +- args_size = (linux_argc) * sizeof (grub_uint64_t); +- /* Initrd address/size and initrd */ +- args_size += 3 * sizeof (grub_uint64_t); +- /* NULL terminator. */ +- args_size += sizeof (grub_uint64_t); +- /* First argument is always "a0". */ +- args_size += ALIGN_UP (sizeof ("a0"), 4); +- /* Normal arguments. */ +- for (i = 1; i < argc; i++) +- args_size += ALIGN_UP (grub_strlen (argv[i]) + 1, 4); +- +- /* rd arguments. */ +- args_size += ALIGN_UP (sizeof ("rd_start=0xXXXXXXXXXXXXXXXX"), 4); +- args_size += ALIGN_UP (sizeof ("rd_size=0xXXXXXXXXXXXXXXXX"), 4); +- args_size += ALIGN_UP (sizeof ("initrd=0xXXXXXXXXXXXXXXXX,0xXXXXXXXXXXXXXXXX"), 4); +- +- args_size = ALIGN_UP (args_size, 8); +- +- linux_args_addr = grub_efi_allocate_any_pages (GRUB_EFI_BYTES_TO_PAGES (args_size)); +- grub_dprintf ("linux", "linux args numpages: %lld\n", +- (long long) GRUB_EFI_BYTES_TO_PAGES (args_size)); +- if (!linux_args_addr) +- { +- grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory")); +- goto fail; ++ if (grub_file_seek (file, 0) == (grub_off_t) -1) ++ goto fail; ++ ++ if (grub_file_read (file, &lh, sizeof (lh)) < (grub_ssize_t) sizeof (lh)) ++ { ++ if (!grub_errno) ++ grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"), ++ argv[0]); ++ goto fail; ++ } ++ ++ if (grub_arch_efi_linux_check_image (&lh) != GRUB_ERR_NONE) ++ { ++ goto fail; ++ } ++ /* linux kernel type is EFI */ ++ grub_loongarch_linux_type = GRUB_LOONGARCH_LINUX_EFI; ++ kernel_params.kernel_addr = (grub_addr_t) grub_efi_allocate_any_pages ( ++ GRUB_EFI_BYTES_TO_PAGES (kernel_params.kernel_size)); ++ grub_dprintf ("linux", "kernel numpages: %" PRIuGRUB_SIZE "\n", ++ GRUB_EFI_BYTES_TO_PAGES (kernel_params.kernel_size)); ++ if (!kernel_params.kernel_addr) ++ { ++ grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory")); ++ goto fail; ++ } ++ ++ grub_file_seek (file, 0); ++ if (grub_file_read (file, (void*) kernel_params.kernel_addr, kernel_params.kernel_size) ++ < (grub_int64_t) kernel_params.kernel_size) ++ { ++ if (!grub_errno) ++ grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"), argv[0]); ++ goto fail; ++ } ++ ++ grub_dprintf ("linux", "kernel @ %p\n", (void*) kernel_params.kernel_addr); + } +- linux_argv = (grub_uint64_t *) linux_args_addr; +- linux_args = (char *) (linux_argv + (linux_argc + 1 + 3)); +- +- grub_memcpy (linux_args, "a0", sizeof ("a0")); +- *linux_argv = (grub_uint64_t) (grub_addr_t) linux_args; +- linux_argv++; +- linux_args += ALIGN_UP (sizeof ("a0"), 4); + +- for (i = 1; i < argc; i++) ++ cmdline_size = grub_loader_cmdline_size (argc, argv) + sizeof (LINUX_IMAGE) ++ + sizeof (GRUB_INITRD_STRING); ++ kernel_params.ramdisk_args_len = grub_loader_cmdline_size (argc, argv) + sizeof (LINUX_IMAGE); ++ kernel_params.linux_argc = argc; ++ kernel_params.linux_args = grub_malloc (cmdline_size); ++ if (!kernel_params.linux_args) + { +- grub_memcpy (linux_args, argv[i], grub_strlen (argv[i]) + 1); +- *linux_argv = (grub_uint64_t) (grub_addr_t) linux_args; +- linux_argv++; +- linux_args += ALIGN_UP (grub_strlen (argv[i]) + 1, 4); ++ grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory")); ++ goto fail; + } + +- /* Reserve space for rd arguments. */ +- rd_addr_arg_off = (grub_uint8_t *) linux_args - linux_args_addr; +- linux_args += ALIGN_UP (sizeof ("rd_start=0xXXXXXXXXXXXXXXXX"), 4); +- *linux_argv = 0; +- linux_argv++; ++ grub_memcpy (kernel_params.linux_args, LINUX_IMAGE, sizeof (LINUX_IMAGE)); + +- rd_size_arg_off = (grub_uint8_t *) linux_args - linux_args_addr; +- linux_args += ALIGN_UP (sizeof ("rd_size=0xXXXXXXXXXXXXXXXX"), 4); +- *linux_argv = 0; +- linux_argv++; +- +- /* Reserve space for initrd arguments. */ +- initrd_addr_arg_off = (grub_uint8_t *) linux_args - linux_args_addr; +- linux_args += ALIGN_UP (sizeof ("initrd=0xXXXXXXXXXXXXXXXX,0xXXXXXXXXXXXXXXXX"), 4); +- *linux_argv = 0; +- linux_argv++; ++ if (grub_linux_loongarch_elf_get_boot_params (&boot_params) == 1) ++ is_bpi_boot = 1; ++ else ++ is_bpi_boot = 0; + +- *linux_argv = 0; ++ if (is_bpi_boot == 0) ++ { ++ err = grub_create_loader_cmdline (argc, argv, ++ (char*) ((grub_addr_t) kernel_params.linux_args + sizeof (LINUX_IMAGE) - 1), ++ kernel_params.ramdisk_args_len, ++ GRUB_VERIFY_KERNEL_CMDLINE); ++ if (err) ++ goto fail; ++ } else { ++ /* save args from linux cmdline */ ++ char *p = kernel_params.linux_args; ++ ++ p += sizeof (LINUX_IMAGE) - 1; ++ for (i=0; i < argc; i++) ++ { ++ grub_memcpy (p, argv[i], grub_strlen(argv[i]) + 1); ++ p += grub_strlen(argv[i]) + 1; ++ } + } + + if (grub_errno == GRUB_ERR_NONE) +@@ -541,133 +274,81 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)), + loaded = 1; + } + +- initrd_loaded = 0; +- + fail: ++ if (elf != NULL) { ++ /* grub_elf_close will call grub_file_close() */ ++ grub_elf_close (elf); ++ } else { ++ if (file) ++ grub_file_close (file); ++ } ++ + if (grub_errno != GRUB_ERR_NONE) + { + grub_dl_unref (my_mod); + loaded = 0; + } + +- if (fdt_linux_args && !loaded) +- grub_free (fdt_linux_args); ++ if (kernel_params.linux_args && !loaded) ++ grub_free (kernel_params.linux_args); + +- if (linux_args_addr && !loaded) +- grub_efi_free_pages ((grub_addr_t) linux_args_addr, +- GRUB_EFI_BYTES_TO_PAGES (args_size)); ++ if (grub_loongarch_linux_type == GRUB_LOONGARCH_LINUX_EFI) { ++ if (kernel_params.kernel_addr && !loaded) ++ grub_efi_free_pages ((grub_addr_t) kernel_params.kernel_addr, ++ GRUB_EFI_BYTES_TO_PAGES (kernel_params.kernel_size)); ++ } + + return grub_errno; + } + +-#define INITRD_MAX_ADDRESS_OFFSET (32ULL * 1024 * 1024 * 1024) +- +-/* +- * This function returns a pointer to a legally allocated initrd buffer, +- * or NULL if unsuccessful +- */ +-static void * +-allocate_initrd_mem (int initrd_pages) +-{ +- grub_addr_t max_addr; +- +- if (grub_efi_get_ram_base (&max_addr) != GRUB_ERR_NONE) +- return NULL; +- +- max_addr += INITRD_MAX_ADDRESS_OFFSET - 1; +- +- return grub_efi_allocate_pages_real (max_addr, initrd_pages, +- GRUB_EFI_ALLOCATE_MAX_ADDRESS, +- GRUB_EFI_LOADER_DATA); +-} +- +- + static grub_err_t + grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)), + int argc, char *argv[]) + { +- grub_size_t initrd_size, initrd_pages; +- grub_err_t err; +- void *initrd_mem = NULL; + struct grub_linux_initrd_context initrd_ctx = { 0, 0, 0 }; ++ grub_size_t initrd_size; ++ void *initrd_mem = NULL; ++ grub_err_t err; + + if (argc == 0) +- return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected")); ++ { ++ grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected")); ++ goto fail; ++ } + + if (!loaded) +- return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("you need to load the kernel first")); +- +- if (initrd_loaded) +- return grub_error (GRUB_ERR_BAD_ARGUMENT, "only one initrd command can be issued."); ++ { ++ grub_error (GRUB_ERR_BAD_ARGUMENT, ++ N_("you need to load the kernel first")); ++ goto fail; ++ } + + if (grub_initrd_init (argc, argv, &initrd_ctx)) + goto fail; + + initrd_size = grub_get_initrd_size (&initrd_ctx); ++ grub_dprintf ("linux", "Loading initrd\n"); + +- if (is_fdt_boot == 1) //fdt +- { +- initrd_pages = (GRUB_EFI_BYTES_TO_PAGES (initrd_size)); +- initrd_mem = allocate_initrd_mem (initrd_pages); +- if (!initrd_mem) ++ initrd_mem = grub_linux_loongarch_alloc_initrd_mem_align (initrd_size, 0x10000, &err); ++ if (err) ++ goto fail; ++ ++ if (!initrd_mem) + { + grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory")); + goto fail; + } +- } else { +- grub_relocator_chunk_t ch; +- err = grub_relocator_alloc_chunk_align (relocator, &ch, +- 0, (0xffffffff - initrd_size) + 1, +- initrd_size, 0x10000, +- GRUB_RELOCATOR_PREFERENCE_HIGH, 0); +- +- if (err) +- return err; +- initrd_mem = get_virtual_current_address (ch); +- } + + if (grub_initrd_load (&initrd_ctx, argv, initrd_mem)) + goto fail; + +- initrd_start = (grub_addr_t) initrd_mem; +- initrd_end = initrd_start + initrd_size; +- grub_dprintf ("linux", "[addr=%p, size=0x%lx]\n", +- (void *) initrd_start, initrd_size); +- +- if (is_fdt_boot == 0) //bpi +- { +- grub_snprintf ((char *) linux_args_addr + rd_addr_arg_off, +- sizeof ("rd_start=0xXXXXXXXXXXXXXXXX"), +- "rd_start=0x%lx", +- (grub_uint64_t) initrd_mem); +- ((grub_uint64_t *) linux_args_addr)[linux_argc] +- = (grub_uint64_t) ((grub_addr_t) linux_args_addr + rd_addr_arg_off); +- linux_argc++; +- +- grub_snprintf ((char *) linux_args_addr + rd_size_arg_off, +- sizeof ("rd_size=0xXXXXXXXXXXXXXXXXX"), "rd_size=0x%lx", +- (grub_uint64_t) initrd_size); +- ((grub_uint64_t *) linux_args_addr)[linux_argc] +- = (grub_uint64_t) ((grub_addr_t) linux_args_addr + rd_size_arg_off); +- linux_argc++; +- +- grub_snprintf ((char *) linux_args_addr + initrd_addr_arg_off, +- sizeof ("initrd=0xXXXXXXXXXXXXXXXX,0xXXXXXXXXXXXXXXXX"), +- "initrd=0x%lx,0x%lx", +- ((grub_uint64_t) initrd_mem & GRUB_EFI_MAX_PHY_ADDRESS), +- (grub_uint64_t) initrd_size); +- ((grub_uint64_t *) linux_args_addr)[linux_argc] +- = (grub_uint64_t) ((grub_addr_t) linux_args_addr + initrd_addr_arg_off); +- linux_argc++; +- } +- +- initrd_loaded = 1; +- +- fail: ++ /* save ramdisk addr and size */ ++ kernel_params.ramdisk_addr = (grub_addr_t) initrd_mem; ++ kernel_params.ramdisk_size = initrd_size; ++ grub_dprintf ("linux", "ramdisk [addr=%p, size=0x%lx]\n", ++ (void *) initrd_mem, initrd_size); ++fail: + grub_initrd_close (&initrd_ctx); +- if (is_fdt_boot == 1) +- if (initrd_mem && !initrd_start) +- grub_efi_free_pages ((grub_addr_t) initrd_mem, initrd_pages); + + return grub_errno; + } +@@ -677,9 +358,9 @@ static grub_command_t cmd_linux, cmd_initrd; + GRUB_MOD_INIT(linux) + { + cmd_linux = grub_register_command ("linux", grub_cmd_linux, +- 0, N_("Load Linux.")); ++ N_("FILE [ARGS...]"), N_("Load Linux.")); + cmd_initrd = grub_register_command ("initrd", grub_cmd_initrd, +- 0, N_("Load initrd.")); ++ N_("FILE"), N_("Load initrd.")); + my_mod = mod; + } + +diff --git a/include/grub/efi/api.h b/include/grub/efi/api.h +index d5c805d..c34f9d1 100644 +--- a/include/grub/efi/api.h ++++ b/include/grub/efi/api.h +@@ -374,6 +374,26 @@ + { 0xa1, 0x92, 0xbf, 0x1d, 0x57, 0xd0, 0xb1, 0x89 } \ + } + ++#define GRUB_EFI_BOOT_MEMMAP_GUID \ ++ { 0x800f683f, 0xd08b, 0x423a, \ ++ { 0xa2, 0x93, 0x96, 0x5c, 0x3c, 0x6f, 0xe2, 0xb4 } \ ++ } ++ ++#define GRUB_EFI_INITRD_MEDIA_GUID \ ++ { 0x5568e427, 0x68fc, 0x4f3d, \ ++ { 0xac, 0x74, 0xca, 0x55, 0x52, 0x31, 0xcc, 0x68 } \ ++ } ++ ++#define GRUB_EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID \ ++ { 0x9042a9de, 0x23dc, 0x4a38, \ ++ { 0x96, 0xfb, 0x7a, 0xde, 0xd0, 0x80, 0x51, 0x6a } \ ++ } ++ ++#define GRUB_EFI_CONSOLE_OUT_DEVICE_GUID \ ++ { 0xd3b36f2c, 0xd551, 0x11d4, \ ++ { 0x9a, 0x46, 0x00, 0x90, 0x27, 0x3f, 0xc1, 0x4d } \ ++ } ++ + struct grub_efi_sal_system_table + { + grub_uint32_t signature; +diff --git a/include/grub/fdt.h b/include/grub/fdt.h +index a0710ab..2f4f270 100644 +--- a/include/grub/fdt.h ++++ b/include/grub/fdt.h +@@ -146,6 +146,6 @@ int EXPORT_FUNC(grub_fdt_set_prop) (void *fdt, unsigned int nodeoffset, const ch + grub_fdt_set_prop ((fdt), (nodeoffset), "reg", reg_64, 16); \ + }) + +-#endif /* defined(__arm__) || defined(__aarch64__) */ ++#endif /* defined(__arm__) || defined(__aarch64__) || defined(__loongarch__) */ + + #endif /* ! GRUB_FDT_HEADER */ +diff --git a/include/grub/loongarch64/asm.h b/include/grub/loongarch64/asm.h +deleted file mode 100644 +index c3e77e9..0000000 +--- a/include/grub/loongarch64/asm.h ++++ /dev/null +@@ -1,10 +0,0 @@ +-#ifndef GRUB_LOONGARCH64_ASM_HEADER +-#define GRUB_LOONGARCH64_ASM_HEADER 1 +- +-#define GRUB_ASM_T4 $a4 +-#define GRUB_ASM_T5 $a5 +-#define GRUB_ASM_SZREG 8 +-#define GRUB_ASM_REG_S st.d +-#define GRUB_ASM_REG_L ld.d +- +-#endif +diff --git a/include/grub/loongarch64/efi/loongson.h b/include/grub/loongarch64/efi/loongson.h +deleted file mode 100644 +index 7a9ccb4..0000000 +--- a/include/grub/loongarch64/efi/loongson.h ++++ /dev/null +@@ -1,113 +0,0 @@ +-/* +- * GRUB -- GRand Unified Bootloader +- * Copyright (C) 2017 Free Software Foundation, Inc. +- * +- * GRUB is free software: you can redistribute it and/or modify +- * it under the terms of the GNU General Public License as published by +- * the Free Software Foundation, either version 3 of the License, or +- * (at your option) any later version. +- * +- * GRUB is distributed in the hope that it will be useful, +- * but WITHOUT ANY WARRANTY; without even the implied warranty of +- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +- * GNU General Public License for more details. +- * +- * You should have received a copy of the GNU General Public License +- * along with GRUB. If not, see . +- */ +- +-#ifndef GRUB_EFI_LOONGSON_HEADER +-#define GRUB_EFI_LOONGSON_HEADER 1 +- +-#include +- +-#include +- +-#define GRUB_EFI_LOONGSON_BPI_TABLE_GUID \ +- { 0x4660f721, 0x2ec5, 0x416a, \ +- { 0x89, 0x9a, 0x43, 0x18, 0x02, 0x50, 0xa0, 0xc9 } \ +- } +- +-#define GRUB_EFI_MAX_PHY_ADDRESS 0xffffffffffffULL +-#define ELF32_LOADMASK (0xf0000000UL) +-#define ELF64_LOADMASK (0xf000000000000000ULL) +-#define FLAGS_EFI_SUPPORT_BIT 0 +-#define GRUB_EFI_LOONGSON_MMAP_MAX 128 +- +-typedef enum +- { +- GRUB_EFI_LOONGSON_SYSTEM_RAM = 1, +- GRUB_EFI_LOONGSON_MEMORY_RESERVED, +- GRUB_EFI_LOONGSON_ACPI_TABLE, +- GRUB_EFI_LOONGSON_ACPI_NVS, +- GRUB_EFI_LOONGSON_MAX_MEMORY_TYPE +- } +-grub_efi_loongson_memory_type; +- +-typedef enum +- { +- GRUB_EFI_BPI_VER_NONE = 0, +- GRUB_EFI_BPI_VER_V1 = 1000, +- GRUB_EFI_BPI_VER_V2 = 1001, +- } +- grub_efi_loongson_bpi_version; +- +-grub_uint8_t +-EXPORT_FUNC(grub_efi_loongson_calculatesum8) (const grub_uint8_t *Buffer, +- grub_efi_uintn_t Length); +- +-grub_uint8_t +-EXPORT_FUNC(grub_efi_loongson_grub_calculatechecksum8) (const grub_uint8_t *Buffer, +- grub_efi_uintn_t Length); +- +-unsigned long +-EXPORT_FUNC(grub_efi_get_bpi_version) (const char *str); +- +-void * +-EXPORT_FUNC(grub_efi_loongson_get_boot_params) (void); +- +-typedef struct _extention_list_hdr { +- grub_uint64_t signature; +- grub_uint32_t length; +- grub_uint8_t revision; +- grub_uint8_t checksum; +- union { +- struct _extention_list_hdr *next; +- grub_uint64_t next_offset; +- }; +-}GRUB_PACKED +-ext_list; +- +-typedef struct boot_params_interface { +- grub_uint64_t signature; //{'B', 'P', 'I', 'x', 'x', 'x', 'x', 'x'} +- grub_efi_system_table_t *systemtable; +- union { +- ext_list *extlist; +- grub_uint64_t extlist_offset; +- }; +- grub_uint64_t flags; +-}GRUB_PACKED +-boot_params_interface; +- +-typedef struct { +- ext_list header; //{MEM} +- grub_uint8_t mapcount; +- struct GRUB_PACKED memmap_v1 { +- grub_uint32_t memtype; +- grub_uint64_t memstart; +- grub_uint64_t memsize; +- } map[GRUB_EFI_LOONGSON_MMAP_MAX]; +-}GRUB_PACKED +-mem_map_v1; +- +-typedef struct { +- ext_list header; //{VBIOS} +- grub_uint64_t vbiosaddr; +-}GRUB_PACKED +-vbios; +- +-grub_uint32_t +-EXPORT_FUNC (grub_efi_loongson_memmap_v1_sort) (struct memmap_v1 array[], +- grub_uint32_t length, mem_map_v1 * mem, +- grub_uint32_t index, grub_uint32_t memtype); +-#endif /* ! GRUB_EFI_LOONGSON_HEADER */ +diff --git a/include/grub/loongarch64/efi/memory.h b/include/grub/loongarch64/efi/memory.h +index 73641a1..16b2e0b 100644 +--- a/include/grub/loongarch64/efi/memory.h ++++ b/include/grub/loongarch64/efi/memory.h +@@ -6,7 +6,12 @@ grub_efi_max_usable_address(void) + { + grub_uint64_t addr; + asm volatile ("csrrd %0, 0x181" : "=r" (addr)); +- return addr |= 0xffffffffffUL; ++ if (addr & 0x1) ++ addr |= 0xfffffffffffUL; ++ else ++ addr = 0xfffffffffffUL; ++ ++ return addr; + } + + #endif /* ! GRUB_MEMORY_CPU_HEADER */ +diff --git a/include/grub/loongarch64/efi/time.h b/include/grub/loongarch64/efi/time.h +deleted file mode 100644 +index e69de29..0000000 +diff --git a/include/grub/loongarch64/linux.h b/include/grub/loongarch64/linux.h +index 3c6cf65..4ac3c57 100644 +--- a/include/grub/loongarch64/linux.h ++++ b/include/grub/loongarch64/linux.h +@@ -28,8 +28,7 @@ + + #define GRUB_LOONGSON3_BOOT_MEM_MAP_MAX 128 + +-#define GRUB_LINUX_LOONGARCH_MAGIC_SIGNATURE 0x4C6F6F6E67417263 /* 'LoongArc' */ +-#define GRUB_LINUX_LOONGARCH_MAGIC_SIGNATURE2 0x68 /* 'h' */ ++#define GRUB_LINUX_LOONGARCH_MAGIC_SIGNATURE 0x6E6F73676E6F6F4C /* 'Loongson' */ + #define linux_arch_kernel_header linux_loongarch64_kernel_header + + /* From linux/Documentation/loongarch/booting.txt +@@ -48,27 +47,171 @@ struct linux_loongarch64_kernel_header + grub_uint64_t res0; /* reserved */ + grub_uint64_t res1; /* reserved */ + grub_uint64_t res2; /* reserved */ +- grub_uint64_t magic; /* Magic number, little endian, "LoongArc" */ +- grub_uint32_t magic1; /* Magic number, little endian, "h" */ ++ grub_uint64_t magic; /* Magic number, little endian, "Loongson" */ + grub_uint64_t res3; /* reserved */ ++ grub_uint32_t res4; /* reserved */ + grub_uint32_t hdr_offset; /* Offset of PE/COFF header */ + }; + + struct linux_loongarch64_kernel_params + { +- grub_addr_t kernel_addr; /* kernel entry address */ +- grub_size_t kernel_size; /* kernel size */ +- grub_addr_t ramdisk_addr; /* initrd load address */ +- grub_size_t ramdisk_size; /* initrd size */ +- int linux_argc; +- grub_addr_t linux_argv; ++ grub_addr_t kernel_addr; /* kernel entry address */ ++ grub_size_t kernel_size; /* kernel size */ ++ grub_addr_t ramdisk_addr; /* initrd load address */ ++ grub_size_t ramdisk_size; /* initrd size */ ++ int ramdisk_args_len; /* position of initrd in linux_args*/ ++ int linux_argc; /* cmdline parameters number*/ ++ grub_addr_t linux_argv; /* cmdline parameters address*/ + void* linux_args; ++ void* fdt; + }; + + #include + #include + ++#define GRUB_EFI_MAX_PHY_ADDRESS 0xffffffffffffULL + #define ELF32_LOADMASK (0xf0000000UL) + #define ELF64_LOADMASK (0xf000000000000000ULL) ++#define FLAGS_EFI_SUPPORT_BIT 0 ++ ++/*initrd info*/ ++#define GRUB_RD_START_STRING "rd_start=0xXXXXXXXXXXXXXXXX" ++#define GRUB_RD_SIZE_STRING "rd_size=0xXXXXXXXXXXXXXXXX" ++#define GRUB_INITRD_STRING "initrd=0xXXXXXXXXXXXXXXXX,0xXXXXXXXXXXXXXXXX" ++ ++#define FDT_ADDR_CELLS_STRING "#address-cells" ++#define FDT_SIZE_CELLS_STRING "#size-cells" ++#define FDT_ADDR_SIZE_EXTRA ((2 * grub_fdt_prop_entry_size (sizeof(grub_uint32_t))) + \ ++ sizeof (FDT_ADDR_CELLS_STRING) + \ ++ sizeof (FDT_SIZE_CELLS_STRING)) ++ ++struct efi_boot_memmap { ++ grub_efi_uintn_t map_size; ++ grub_efi_uintn_t desc_size; ++ grub_efi_uint32_t desc_ver; ++ grub_efi_uintn_t map_key; ++ grub_efi_uintn_t buff_size; ++ grub_efi_memory_descriptor_t map[]; ++}; ++ ++struct efi_initrd { ++ grub_efi_uintn_t base; ++ grub_efi_uintn_t size; ++}; ++ ++/* ++ * These are set up by the setup-routine at boot-time: ++ */ ++struct screen_info { ++ grub_efi_uint8_t orig_x; /* 0x00 */ ++ grub_efi_uint8_t orig_y; /* 0x01 */ ++ grub_efi_uint16_t ext_mem_k; /* 0x02 */ ++ grub_efi_uint16_t orig_video_page; /* 0x04 */ ++ grub_efi_uint8_t orig_video_mode; /* 0x06 */ ++ grub_efi_uint8_t orig_video_cols; /* 0x07 */ ++ grub_efi_uint8_t flags; /* 0x08 */ ++ grub_efi_uint8_t unused2; /* 0x09 */ ++ grub_efi_uint16_t orig_video_ega_bx;/* 0x0a */ ++ grub_efi_uint16_t unused3; /* 0x0c */ ++ grub_efi_uint8_t orig_video_lines; /* 0x0e */ ++ grub_efi_uint8_t orig_video_isVGA; /* 0x0f */ ++ grub_efi_uint16_t orig_video_points;/* 0x10 */ ++ ++ /* VESA graphic mode -- linear frame buffer */ ++ grub_efi_uint16_t lfb_width; /* 0x12 */ ++ grub_efi_uint16_t lfb_height; /* 0x14 */ ++ grub_efi_uint16_t lfb_depth; /* 0x16 */ ++ grub_efi_uint32_t lfb_base; /* 0x18 */ ++ grub_efi_uint32_t lfb_size; /* 0x1c */ ++ grub_efi_uint16_t cl_magic, cl_offset; /* 0x20 */ ++ grub_efi_uint16_t lfb_linelength; /* 0x24 */ ++ grub_efi_uint8_t red_size; /* 0x26 */ ++ grub_efi_uint8_t red_pos; /* 0x27 */ ++ grub_efi_uint8_t green_size; /* 0x28 */ ++ grub_efi_uint8_t green_pos; /* 0x29 */ ++ grub_efi_uint8_t blue_size; /* 0x2a */ ++ grub_efi_uint8_t blue_pos; /* 0x2b */ ++ grub_efi_uint8_t rsvd_size; /* 0x2c */ ++ grub_efi_uint8_t rsvd_pos; /* 0x2d */ ++ grub_efi_uint16_t vesapm_seg; /* 0x2e */ ++ grub_efi_uint16_t vesapm_off; /* 0x30 */ ++ grub_efi_uint16_t pages; /* 0x32 */ ++ grub_efi_uint16_t vesa_attributes; /* 0x34 */ ++ grub_efi_uint32_t capabilities; /* 0x36 */ ++ grub_efi_uint32_t ext_lfb_base; /* 0x3a */ ++ grub_efi_uint8_t _reserved[2]; /* 0x3e */ ++} __attribute__((packed)); ++ ++#define GRUB_VIDEO_TYPE_EFI 0x70 ++#define GRUB_VIDEO_CAPABILITY_SKIP_QUIRKS (1 << 0) ++#define GRUB_VIDEO_CAPABILITY_64BIT_BASE (1 << 1) /* Frame buffer base is 64-bit */ ++ ++ ++/* From arch/loongarch/include/asm/mach-loongson64/boot_param.h */ ++struct _extention_list_hdr { ++ grub_uint64_t signature; ++ grub_uint32_t length; ++ grub_uint8_t revision; ++ grub_uint8_t checksum; ++ union { ++ struct _extention_list_hdr *next; ++ grub_uint64_t next_offset; ++ }; ++ ++} GRUB_PACKED; ++ ++struct boot_params_interface { ++ grub_uint64_t signature; /* {"B", "P", "I", "0", "1", ... } */ ++ grub_efi_system_table_t *systemtable; ++ union { ++ struct _extention_list_hdr *extlist; ++ grub_uint64_t extlist_offset; ++ }; ++ grub_uint64_t flags; ++}GRUB_PACKED; ++ ++struct loongsonlist_mem_map { ++ struct _extention_list_hdr header; /* {"M", "E", "M"} */ ++ grub_uint8_t map_count; ++ struct memmap { ++ grub_uint32_t mem_type; ++ grub_uint64_t mem_start; ++ grub_uint64_t mem_size; ++ } GRUB_PACKED map[GRUB_LOONGSON3_BOOT_MEM_MAP_MAX]; ++}GRUB_PACKED; ++ ++grub_err_t ++finalize_efi_params_linux (struct linux_loongarch64_kernel_params *kernel_params); ++ ++grub_err_t ++grub_linux_loongarch_elf_linux_boot_image (struct linux_loongarch64_kernel_params ++ *kernel_params); ++ ++void* ++grub_linux_loongarch_alloc_virtual_mem_addr (grub_addr_t addr, ++ grub_size_t size, ++ grub_err_t *err); ++ ++void* ++grub_linux_loongarch_alloc_virtual_mem_align (grub_size_t size, ++ grub_size_t align, ++ grub_err_t *err); ++ ++void* ++grub_linux_loongarch_alloc_initrd_mem_align (grub_size_t size, ++ grub_size_t align, ++ grub_err_t *err); ++ ++void ++grub_linux_loongarch_elf_relocator_unload (void); ++ ++int ++grub_linux_loongarch_elf_get_boot_params (struct boot_params_interface **boot_params); ++ ++grub_err_t ++grub_linux_loongarch_elf_boot_params (struct boot_params_interface *boot_params); ++ ++grub_err_t ++grub_linux_loongarch_elf_load_kernel (grub_elf_t elf, const char *filename); + + #endif /* ! GRUB_LOONGARCH64_LINUX_HEADER */ +diff --git a/include/grub/loongarch64/time.h b/include/grub/loongarch64/time.h +index c9a7334..0a10fbd 100644 +--- a/include/grub/loongarch64/time.h ++++ b/include/grub/loongarch64/time.h +@@ -19,18 +19,6 @@ + #ifndef KERNEL_CPU_TIME_HEADER + #define KERNEL_CPU_TIME_HEADER 1 + +-#ifndef GRUB_UTIL +- +-#define GRUB_TICKS_PER_SECOND (grub_arch_cpuclock / 2) +- +-void grub_timer_init (grub_uint32_t cpuclock); +- +-/* Return the real time in ticks. */ +-grub_uint64_t grub_get_rtc (void); +- +-extern grub_uint32_t grub_arch_cpuclock; +-#endif +- + static inline void + grub_cpu_idle(void) + { +-- +2.43.5 + diff --git a/grub.patches b/grub.patches index 62d80ca..798ccc8 100644 --- a/grub.patches +++ b/grub.patches @@ -670,3 +670,4 @@ Patch1002: 1002-Add-LoongArch64-support-and-update-interface-to-v40.patch Patch1003: 1003-cryptodisk-make-the-password-getter-and-additional-a.patch Patch1004: 1004-cryptodisk-add-OS-provided-secret-support.patch Patch1005: 1005-efi-Add-API-for-retrieving-the-EFI-secret-for-crypto.patch +Patch1006: 1006-Update-patches-in-sync-with-loongarch64.patch diff --git a/grub2.spec b/grub2.spec index ee6e08b..abb85ae 100644 --- a/grub2.spec +++ b/grub2.spec @@ -545,6 +545,7 @@ fi - LoongArch64 support fdt and phy-addr BIOS(yangqiming@loongson.cn) - Remove dtb dir with correct argument (Liwei Ge) - Support use confidential computing provisioned secrets for disk decryption(hanliyang@hygon.cn) +- Update for loongarch64 in sync with latest v2.04(liuxue@loongson.cn) * Tue Mar 25 2025 Nicolas Frayer - 2.02-162 - ieee1275/ofnet: Fix grub_malloc() removed after added safe -- Gitee