[PATCH v5 1/9] PE: Add LoongArch definitions

2022-07-29 Thread Xiaotian Wu
Signed-off-by: Xiaotian Wu 
Signed-off-by: Zhou Yang 
---
 include/grub/efi/pe32.h | 36 
 1 file changed, 20 insertions(+), 16 deletions(-)

diff --git a/include/grub/efi/pe32.h b/include/grub/efi/pe32.h
index 0ed8781f0..de56edef6 100644
--- a/include/grub/efi/pe32.h
+++ b/include/grub/efi/pe32.h
@@ -77,6 +77,8 @@ struct grub_pe32_coff_header
 #define GRUB_PE32_MACHINE_X86_64   0x8664
 #define GRUB_PE32_MACHINE_ARMTHUMB_MIXED   0x01c2
 #define GRUB_PE32_MACHINE_ARM640xAA64
+#define GRUB_PE32_MACHINE_LOONGARCH32  0x6232
+#define GRUB_PE32_MACHINE_LOONGARCH64  0x6264
 #define GRUB_PE32_MACHINE_RISCV32  0x5032
 #define GRUB_PE32_MACHINE_RISCV64  0x5064
 
@@ -283,22 +285,24 @@ struct grub_pe32_fixup_block
 
 #define GRUB_PE32_FIXUP_ENTRY(type, offset)(((type) << 12) | (offset))
 
-#define GRUB_PE32_REL_BASED_ABSOLUTE   0
-#define GRUB_PE32_REL_BASED_HIGH   1
-#define GRUB_PE32_REL_BASED_LOW2
-#define GRUB_PE32_REL_BASED_HIGHLOW3
-#define GRUB_PE32_REL_BASED_HIGHADJ4
-#define GRUB_PE32_REL_BASED_MIPS_JMPADDR 5
-#define GRUB_PE32_REL_BASED_ARM_MOV32A  5
-#define GRUB_PE32_REL_BASED_RISCV_HI20 5
-#define GRUB_PE32_REL_BASED_SECTION6
-#define GRUB_PE32_REL_BASED_REL7
-#define GRUB_PE32_REL_BASED_ARM_MOV32T  7
-#define GRUB_PE32_REL_BASED_RISCV_LOW12I 7
-#define GRUB_PE32_REL_BASED_RISCV_LOW12S 8
-#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_ABSOLUTE   0
+#define GRUB_PE32_REL_BASED_HIGH   1
+#define GRUB_PE32_REL_BASED_LOW2
+#define GRUB_PE32_REL_BASED_HIGHLOW3
+#define GRUB_PE32_REL_BASED_HIGHADJ4
+#define GRUB_PE32_REL_BASED_MIPS_JMPADDR   5
+#define GRUB_PE32_REL_BASED_ARM_MOV32A 5
+#define GRUB_PE32_REL_BASED_RISCV_HI20 5
+#define GRUB_PE32_REL_BASED_SECTION6
+#define GRUB_PE32_REL_BASED_REL7
+#define GRUB_PE32_REL_BASED_ARM_MOV32T 7
+#define GRUB_PE32_REL_BASED_RISCV_LOW12I   7
+#define GRUB_PE32_REL_BASED_RISCV_LOW12S   8
+#define GRUB_PE32_REL_BASED_LOONGARCH32_MARK_LA8
+#define GRUB_PE32_REL_BASED_LOONGARCH64_MARK_LA8
+#define GRUB_PE32_REL_BASED_IA64_IMM64 9
+#define GRUB_PE32_REL_BASED_DIR64  10
+#define GRUB_PE32_REL_BASED_HIGH3ADJ   11
 
 struct grub_pe32_symbol
 {
-- 
2.35.1


___
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel


[PATCH v5 2/9] Add LoongArch definitions

2022-07-29 Thread Xiaotian Wu
Signed-off-by: Xiaotian Wu 
Signed-off-by: Zhou Yang 
---
 include/grub/elf.h | 23 +++
 1 file changed, 23 insertions(+)

diff --git a/include/grub/elf.h b/include/grub/elf.h
index c478933ee..1c8d4f5d5 100644
--- a/include/grub/elf.h
+++ b/include/grub/elf.h
@@ -248,6 +248,7 @@ typedef struct
 #define EM_NUM 95
 #define EM_AARCH64 183 /* ARM 64-bit architecture */
 #define EM_RISCV   243 /* RISC-V */
+#define EM_LOONGARCH   258 /* LoongArch */
 
 /* If it is necessary to assign new unofficial EM_* values, please
pick large random numbers (0x8523, 0xa7f2, etc.) to minimize the
@@ -2531,6 +2532,28 @@ typedef Elf32_Addr Elf32_Conflict;
 #define R_RISCV_SET32   56
 #define R_RISCV_32_PCREL57
 
+/* LoongArch relocations */
+#define R_LARCH_NONE 0
+#define R_LARCH_64   2
+#define R_LARCH_MARK_LA  20
+#define R_LARCH_SOP_PUSH_PCREL   22
+#define R_LARCH_SOP_PUSH_ABSOLUTE23
+#define R_LARCH_SOP_PUSH_PLT_PCREL   29
+#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_538
+#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_S242
+#define R_LARCH_SOP_POP_32_S_5_2043
+#define R_LARCH_SOP_POP_32_S_0_5_10_16_S2 44
+#define R_LARCH_SOP_POP_32_S_0_10_10_16_S245
+
 #ifdef GRUB_TARGET_WORDSIZE
 #if GRUB_TARGET_WORDSIZE == 32
 
-- 
2.35.1


___
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel


[PATCH v5 5/9] LoongArch: Add stubs for Linux loading commands

2022-07-29 Thread Xiaotian Wu
Signed-off-by: Xiaotian Wu 
---
 grub-core/loader/loongarch64/linux.c | 59 
 include/grub/loongarch64/linux.h | 31 +++
 2 files changed, 90 insertions(+)
 create mode 100644 grub-core/loader/loongarch64/linux.c
 create mode 100644 include/grub/loongarch64/linux.h

diff --git a/grub-core/loader/loongarch64/linux.c 
b/grub-core/loader/loongarch64/linux.c
new file mode 100644
index 0..d9d39a20c
--- /dev/null
+++ b/grub-core/loader/loongarch64/linux.c
@@ -0,0 +1,59 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2022 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 
+
+GRUB_MOD_LICENSE ("GPLv3+");
+
+static grub_err_t
+grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
+int argc __attribute__ ((unused)),
+char *argv[] __attribute__ ((unused)))
+{
+  grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET, N_("Linux not supported yet"));
+
+  return grub_errno;
+}
+
+static grub_err_t
+grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
+   int argc __attribute__ ((unused)),
+   char *argv[] __attribute__ ((unused)))
+{
+  grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET, N_("Linux not supported yet"));
+
+  return grub_errno;
+}
+
+static grub_command_t cmd_linux, cmd_initrd;
+
+GRUB_MOD_INIT (linux)
+{
+  cmd_linux = grub_register_command ("linux", grub_cmd_linux,
+N_("FILE [ARGS...]"), N_("Load Linux."));
+  cmd_initrd = grub_register_command ("initrd", grub_cmd_initrd,
+ N_("FILE"), N_("Load initrd."));
+}
+
+GRUB_MOD_FINI (linux)
+{
+  grub_unregister_command (cmd_linux);
+  grub_unregister_command (cmd_initrd);
+}
diff --git a/include/grub/loongarch64/linux.h b/include/grub/loongarch64/linux.h
new file mode 100644
index 0..76fe693f4
--- /dev/null
+++ b/include/grub/loongarch64/linux.h
@@ -0,0 +1,31 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2022 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
+
+struct linux_loongarch64_kernel_header
+{
+  /*
+   * TODO
+   */
+};
+
+#define linux_arch_kernel_header linux_loongarch64_kernel_header
+
+#endif /* ! GRUB_LOONGARCH64_LINUX_HEADER */
-- 
2.35.1


___
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel


[PATCH v5 8/9] LoongArch: Add to build system

2022-07-29 Thread Xiaotian Wu
Signed-off-by: Xiaotian Wu 
Signed-off-by: Zhou Yang 
---
 Makefile.util.def   |  1 +
 configure.ac| 22 -
 gentpl.py   | 25 ++-
 grub-core/Makefile.am   |  6 +
 grub-core/Makefile.core.def | 16 
 include/grub/efi/api.h  |  2 +-
 include/grub/util/install.h |  1 +
 util/grub-install-common.c  | 49 +++--
 util/grub-install.c | 16 
 util/grub-mknetdir.c|  1 +
 util/grub-mkrescue.c|  8 ++
 util/mkimage.c  | 16 
 12 files changed, 125 insertions(+), 38 deletions(-)

diff --git a/Makefile.util.def b/Makefile.util.def
index d919c562c..765c6fea2 100644
--- a/Makefile.util.def
+++ b/Makefile.util.def
@@ -163,6 +163,7 @@ library = {
   common = grub-core/kern/ia64/dl_helper.c;
   common = grub-core/kern/arm/dl_helper.c;
   common = grub-core/kern/arm64/dl_helper.c;
+  common = grub-core/kern/loongarch64/dl_helper.c;
   common = grub-core/lib/minilzo/minilzo.c;
   common = grub-core/lib/xzembed/xz_dec_bcj.c;
   common = grub-core/lib/xzembed/xz_dec_lzma2.c;
diff --git a/configure.ac b/configure.ac
index 90f686f79..71f65a70d 100644
--- a/configure.ac
+++ b/configure.ac
@@ -119,6 +119,7 @@ case "$target_cpu" in
;;
   arm*)target_cpu=arm ;;
   aarch64*)target_cpu=arm64 ;;
+  loongarch64) target_cpu=loongarch64 ;;
   riscv32*)target_cpu=riscv32 ;;
   riscv64*)target_cpu=riscv64 ;;
 esac
@@ -144,6 +145,7 @@ if test "x$with_platform" = x; then
 ia64-*) platform=efi ;;
 arm-*) platform=uboot ;;
 arm64-*) platform=efi ;;
+loongarch64-*) platform=efi;;
 riscv32-*) platform=efi ;;
 riscv64-*) platform=efi ;;
 *)
@@ -194,6 +196,7 @@ case "$target_cpu"-"$platform" in
   arm-coreboot) ;;
   arm-efi) ;;
   arm64-efi) ;;
+  loongarch64-efi) ;;
   riscv32-efi) ;;
   riscv64-efi) ;;
   *-emu) ;;
@@ -855,6 +858,20 @@ if ( test "x$target_cpu" = xi386 || test "x$target_cpu" = 
xx86_64 ); then
   fi
 fi
 
+if test "x$target_cpu" = xloongarch64; then
+  AC_CACHE_CHECK([whether -Wa,-mla-global-with-abs works], 
[grub_cv_cc_mla_global_with_abs], [
+CFLAGS="$TARGET_CFLAGS -Wa,-mla-global-with-abs -Werror"
+AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]], [[]])],
+   [grub_cv_cc_mla_global_with_abs=yes],
+   [grub_cv_cc_mla_global_with_abs=no])
+  ])
+
+  if test "x$grub_cv_cc_mla_global_with_abs" = xyes; then
+TARGET_CFLAGS="$TARGET_CFLAGS -Wa,-mla-global-with-abs"
+TARGET_CCASFLAGS="$TARGET_CCASFLAGS -Wa,-mla-global-with-abs"
+  fi
+fi
+
 # GRUB doesn't use float or doubles at all. Yet some toolchains may decide
 # that floats are a good fit to run instead of what's written in the code.
 # Given that floating point unit is disabled (if present to begin with)
@@ -1216,7 +1233,8 @@ AC_SUBST(TARGET_LDFLAGS_OLDMAGIC)
 
 LDFLAGS="$TARGET_LDFLAGS"
 
-if test "$target_cpu" = x86_64 || test "$target_cpu" = sparc64 || test 
"$target_cpu" = riscv64 ; then
+if test "$target_cpu" = x86_64 || test "$target_cpu" = sparc64 || test 
"$target_cpu" = riscv64 \
+  || test "$target_cpu" = loongarch64 ; then
   # Use large model to support 4G memory
   AC_CACHE_CHECK([whether option -mcmodel=large works], grub_cv_cc_mcmodel, [
 CFLAGS="$TARGET_CFLAGS -mcmodel=large"
@@ -1996,6 +2014,8 @@ AM_CONDITIONAL([COND_i386_coreboot], [test x$target_cpu = 
xi386 -a x$platform =
 AM_CONDITIONAL([COND_i386_multiboot], [test x$target_cpu = xi386 -a x$platform 
= xmultiboot])
 AM_CONDITIONAL([COND_i386_xen], [test x$target_cpu = xi386 -a x$platform = 
xxen])
 AM_CONDITIONAL([COND_i386_xen_pvh], [test x$target_cpu = xi386 -a x$platform = 
xxen_pvh])
+AM_CONDITIONAL([COND_loongarch64], [test x$target_cpu = xloongarch64])
+AM_CONDITIONAL([COND_loongarch64_efi], [test x$target_cpu = xloongarch64 -a 
x$platform = xefi])
 AM_CONDITIONAL([COND_mips], [test x$target_cpu = xmips -o x$target_cpu = 
xmipsel])
 AM_CONDITIONAL([COND_mips_arc], [test "(" x$target_cpu = xmips -o x$target_cpu 
= xmipsel ")"  -a x$platform = xarc])
 AM_CONDITIONAL([COND_mips_loongson], [test x$target_cpu = xmipsel -a 
x$platform = xloongson])
diff --git a/gentpl.py b/gentpl.py
index 9f51e4fb6..fa53e17e8 100644
--- a/gentpl.py
+++ b/gentpl.py
@@ -32,27 +32,28 @@ 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", "riscv32_efi", "riscv64_efi" ]
+   "arm_coreboot", "loongarch64_efi", "riscv32_efi", 
"riscv64_efi" ]
 
 GROUPS = {}
 
 GROUPS["common"]   = GRUB_PLATFORMS[:]
 
 # Groups based on CPU
-GROUPS["i386"] = [ "i386_pc", "i386_efi", "i386_qemu", "i386_coreboot", 
"i386_multiboot", "i386_ieee1275" ]
-GROUPS["x86_64"]   = [ "x86_64_efi" ]
-GROUPS["x86"]   

[PATCH v5 7/9] LoongArch: Add auxiliary files

2022-07-29 Thread Xiaotian Wu
Signed-off-by: Xiaotian Wu 
Signed-off-by: Zhou Yang 
---
 grub-core/kern/efi/mm.c  |   3 +-
 grub-core/kern/loongarch64/cache.c   |  39 +
 grub-core/kern/loongarch64/cache_flush.S |  33 +++
 grub-core/kern/loongarch64/efi/init.c|  77 
 grub-core/lib/efi/halt.c |   2 +-
 include/grub/efi/efi.h   |   2 +-
 include/grub/loongarch64/efi/memory.h|  24 +
 include/grub/loongarch64/reloc.h | 107 +++
 include/grub/loongarch64/time.h  |  28 ++
 include/grub/loongarch64/types.h |  34 +++
 10 files changed, 346 insertions(+), 3 deletions(-)
 create mode 100644 grub-core/kern/loongarch64/cache.c
 create mode 100644 grub-core/kern/loongarch64/cache_flush.S
 create mode 100644 grub-core/kern/loongarch64/efi/init.c
 create mode 100644 include/grub/loongarch64/efi/memory.h
 create mode 100644 include/grub/loongarch64/reloc.h
 create mode 100644 include/grub/loongarch64/time.h
 create mode 100644 include/grub/loongarch64/types.h

diff --git a/grub-core/kern/efi/mm.c b/grub-core/kern/efi/mm.c
index d290c9a76..5a486558e 100644
--- a/grub-core/kern/efi/mm.c
+++ b/grub-core/kern/efi/mm.c
@@ -654,7 +654,8 @@ grub_efi_mm_init (void)
   grub_mm_add_region_fn = grub_efi_mm_add_regions;
 }
 
-#if defined (__aarch64__) || defined (__arm__) || defined (__riscv)
+#if defined (__aarch64__) || defined (__arm__) || defined (__riscv) || \
+  defined (__loongarch__)
 grub_err_t
 grub_efi_get_ram_base(grub_addr_t *base_addr)
 {
diff --git a/grub-core/kern/loongarch64/cache.c 
b/grub-core/kern/loongarch64/cache.c
new file mode 100644
index 0..43d314df9
--- /dev/null
+++ b/grub-core/kern/loongarch64/cache.c
@@ -0,0 +1,39 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2022 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 
+
+/* Prototypes for asm functions. */
+void grub_arch_clean_dcache_range (void);
+void grub_arch_invalidate_icache_range (void);
+
+void
+grub_arch_sync_caches (void *address __attribute__((unused)),
+  grub_size_t len __attribute__((unused)))
+{
+  grub_arch_clean_dcache_range ();
+  grub_arch_invalidate_icache_range ();
+}
+
+void
+grub_arch_sync_dma_caches (volatile void *address __attribute__((unused)),
+  grub_size_t len __attribute__((unused)))
+{
+  /* DMA non-coherent devices not supported yet */
+}
diff --git a/grub-core/kern/loongarch64/cache_flush.S 
b/grub-core/kern/loongarch64/cache_flush.S
new file mode 100644
index 0..43b97d822
--- /dev/null
+++ b/grub-core/kern/loongarch64/cache_flush.S
@@ -0,0 +1,33 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2022 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   "cache_flush.S"
+   .text
+/*
+ * No further work to do because cache consistency is maintained by hardware on
+ * LoongArch.
+ */
+FUNCTION(grub_arch_clean_dcache_range)
+   dbar 0
+   jr $ra
+
+FUNCTION(grub_arch_invalidate_icache_range)
+   ibar 0
+   jr $ra
diff --git a/grub-core/kern/loongarch64/efi/init.c 
b/grub-core/kern/loongarch64/efi/init.c
new file mode 100644
index 0..8cbeafaba
--- /dev/null
+++ b/grub-core/kern/loongarch64/efi/init.c
@@ -0,0 +1,77 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2022 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 hop

[PATCH v5 4/9] LoongArch: Add early startup code

2022-07-29 Thread Xiaotian Wu
Signed-off-by: Xiaotian Wu 
Signed-off-by: Zhou Yang 
---
 grub-core/kern/loongarch64/efi/startup.S | 34 
 1 file changed, 34 insertions(+)
 create mode 100644 grub-core/kern/loongarch64/efi/startup.S

diff --git a/grub-core/kern/loongarch64/efi/startup.S 
b/grub-core/kern/loongarch64/efi/startup.S
new file mode 100644
index 0..fc8123f8c
--- /dev/null
+++ b/grub-core/kern/loongarch64/efi/startup.S
@@ -0,0 +1,34 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2022 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
+
+FUNCTION(_start)
+   /*
+*  EFI_SYSTEM_TABLE and EFI_HANDLE are passed in $a1/$a0.
+*/
+
+   la  $a2, EXT_C(grub_efi_image_handle)
+   st.d$a0, $a2, 0
+   la  $a2, EXT_C(grub_efi_system_table)
+   st.d$a1, $a2, 0
+
+   b   EXT_C(grub_main)
-- 
2.35.1


___
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel


[PATCH v5 6/9] LoongArch: Add awareness for LoongArch relocations

2022-07-29 Thread Xiaotian Wu
Signed-off-by: Xiaotian Wu 
Signed-off-by: Zhou Yang 
---
 grub-core/kern/dl.c|   9 +-
 grub-core/kern/loongarch64/dl.c| 102 +
 grub-core/kern/loongarch64/dl_helper.c | 198 +
 include/grub/dl.h  |   1 +
 util/grub-mkimagexx.c  |  79 ++
 util/grub-module-verifier.c|  26 
 6 files changed, 412 insertions(+), 3 deletions(-)
 create mode 100644 grub-core/kern/loongarch64/dl.c
 create mode 100644 grub-core/kern/loongarch64/dl_helper.c

diff --git a/grub-core/kern/dl.c b/grub-core/kern/dl.c
index e447fd0fa..0bf40caa6 100644
--- a/grub-core/kern/dl.c
+++ b/grub-core/kern/dl.c
@@ -225,7 +225,8 @@ grub_dl_load_segments (grub_dl_t mod, const Elf_Ehdr *e)
   unsigned i;
   const Elf_Shdr *s;
   grub_size_t tsize = 0, talign = 1;
-#if !defined (__i386__) && !defined (__x86_64__) && !defined(__riscv)
+#if !defined (__i386__) && !defined (__x86_64__) && !defined(__riscv) && \
+  !defined (__loongarch__)
   grub_size_t tramp;
   grub_size_t got;
   grub_err_t err;
@@ -241,7 +242,8 @@ grub_dl_load_segments (grub_dl_t mod, const Elf_Ehdr *e)
talign = s->sh_addralign;
 }
 
-#if !defined (__i386__) && !defined (__x86_64__) && !defined(__riscv)
+#if !defined (__i386__) && !defined (__x86_64__) && !defined(__riscv) && \
+  !defined (__loongarch__)
   err = grub_arch_dl_get_tramp_got_size (e, &tramp, &got);
   if (err)
 return err;
@@ -304,7 +306,8 @@ grub_dl_load_segments (grub_dl_t mod, const Elf_Ehdr *e)
  mod->segment = seg;
}
 }
-#if !defined (__i386__) && !defined (__x86_64__) && !defined(__riscv)
+#if !defined (__i386__) && !defined (__x86_64__) && !defined(__riscv) && \
+  !defined (__loongarch__)
   ptr = (char *) ALIGN_UP ((grub_addr_t) ptr, GRUB_ARCH_DL_TRAMP_ALIGN);
   mod->tramp = ptr;
   mod->trampptr = ptr;
diff --git a/grub-core/kern/loongarch64/dl.c b/grub-core/kern/loongarch64/dl.c
new file mode 100644
index 0..3a6aa91cd
--- /dev/null
+++ b/grub-core/kern/loongarch64/dl.c
@@ -0,0 +1,102 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2022 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 
+
+/* 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_LOONGARCH)
+return grub_error (GRUB_ERR_BAD_OS, N_("invalid arch-dependent ELF 
magic"));
+
+  return GRUB_ERR_NONE;
+}
+
+#pragma GCC diagnostic ignored "-Wcast-align"
+
+/*
+ * Unified function for both REL and RELA.
+ */
+grub_err_t
+grub_arch_dl_relocate_symbols (grub_dl_t mod, void *ehdr,
+  Elf_Shdr *s, grub_dl_segment_t seg)
+{
+  Elf_Rel *rel, *max;
+  struct grub_loongarch64_stack stack;
+  grub_loongarch64_stack_init (&stack);
+
+  for (rel = (Elf_Rel *) ((char *) ehdr + s->sh_offset),
+max = (Elf_Rel *) ((char *) rel + s->sh_size);
+   rel < max;
+   rel = (Elf_Rel *) ((char *) rel + s->sh_entsize))
+{
+  Elf_Sym *sym;
+  grub_uint64_t *place;
+  grub_uint64_t sym_addr;
+
+  if (rel->r_offset >= seg->size)
+   return grub_error (GRUB_ERR_BAD_MODULE,
+  "reloc offset is outside the segment");
+
+  sym = (Elf_Sym *) ((char*)mod->symtab
++ mod->symsize * ELF_R_SYM (rel->r_info));
+
+  sym_addr = sym->st_value;
+  if (s->sh_type == SHT_RELA)
+   sym_addr += ((Elf_Rela *) rel)->r_addend;
+
+  place = (grub_uint64_t *) ((grub_addr_t)seg->addr + rel->r_offset);
+
+  switch (ELF_R_TYPE (rel->r_info))
+   {
+   case R_LARCH_64:
+ *place = sym_addr;
+ break;
+   case R_LARCH_MARK_LA:
+ break;
+   case R_LARCH_SOP_PUSH_PCREL:
+   case R_LARCH_SOP_PUSH_PLT_PCREL:
+ grub_loongarch64_sop_push (&stack, sym_addr - (grub_uint64_t)place);
+ break;
+   GRUB_LOONGARCH64_RELOCATION (&stack, place, sym_addr)
+   default:
+ {
+   char rel_info[17]; /* log16(2^64) = 16, plus NUL.  */
+
+   grub_snprintf (rel_info, sizeof (rel_info) - 1, "%" 
PRIxGRUB

[PATCH v5 3/9] LoongArch: Add setjmp implementation

2022-07-29 Thread Xiaotian Wu
Signed-off-by: Xiaotian Wu 
Signed-off-by: Zhou Yang 
Signed-off-by: Sun Haiyong 
---
 grub-core/lib/loongarch64/setjmp.S | 69 ++
 grub-core/lib/setjmp.S |  2 +
 include/grub/loongarch64/setjmp.h  | 27 
 3 files changed, 98 insertions(+)
 create mode 100644 grub-core/lib/loongarch64/setjmp.S
 create mode 100644 include/grub/loongarch64/setjmp.h

diff --git a/grub-core/lib/loongarch64/setjmp.S 
b/grub-core/lib/loongarch64/setjmp.S
new file mode 100644
index 0..41d58f569
--- /dev/null
+++ b/grub-core/lib/loongarch64/setjmp.S
@@ -0,0 +1,69 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2022 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   "setjmp.S"
+
+GRUB_MOD_LICENSE "GPLv3+"
+
+   .text
+
+/*
+ * int grub_setjmp (jmp_buf env)
+ */
+FUNCTION(grub_setjmp)
+   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
+
+/*
+ * void grub_longjmp (jmp_buf env, int val)
+ */
+FUNCTION(grub_longjmp)
+   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
+
+   /* Return 1 if passed 0, otherwise returns the value in place. */
+   li.w $a0, 1
+   beqz $a1, 1f
+   move $a0, $a1
+1:
+   jr   $ra
diff --git a/grub-core/lib/setjmp.S b/grub-core/lib/setjmp.S
index 9c8721088..cba1d546d 100644
--- a/grub-core/lib/setjmp.S
+++ b/grub-core/lib/setjmp.S
@@ -19,6 +19,8 @@
 #include "./arm/setjmp.S"
 #elif defined(__aarch64__)
 #include "./arm64/setjmp.S"
+#elif defined(__loongarch64)
+#include "./loongarch64/setjmp.S"
 #elif defined(__riscv)
 #include "./riscv/setjmp.S"
 #else
diff --git a/include/grub/loongarch64/setjmp.h 
b/include/grub/loongarch64/setjmp.h
new file mode 100644
index 0..cb3e17763
--- /dev/null
+++ b/include/grub/loongarch64/setjmp.h
@@ -0,0 +1,27 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2022 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 */
-- 
2.35.1


___
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel


[PATCH v5 0/9] Add support for LoongArch

2022-07-29 Thread Xiaotian Wu
LoongArch is a new Loongson 3A5000 CPU instruction set, you can read
documents[1] or visit the development community[2] to get more information.

[1]: https://loongson.github.io/LoongArch-Documentation/README-EN.html
[2]: https://github.com/loongson

This patch series will add the basic support for LoongArch architecture, it can
compile on LoongArch and display the GRUB UI.

The full LoongArch patch has two parts, this is the first part.

The second part needs to wait for the LoadFile2 patch to be merged upstream 
first.
See: https://github.com/loongarch64/grub/tree/dev/patchwork/efi

Please review the patches, thank you.

v1->v2:
- rebase on the last commit.
- fix some errors.
- change the year to 2022 for the new files.

v2->v3:
- based on the last commit.
- add a comment to setjmp.S
- complete LoongArch support for grub-mkrescue

v3->v4:
- based on the last commit.

v4->v5:
- Fix make distcheck failed.
- Add tests support.
- based on the last commit.

Here is the test results:

=
   GRUB 2.11: ./test-suite.log
=

# TOTAL: 82
# PASS:  52
# SKIP:  8
# XFAIL: 0
# FAIL:  2
# XPASS: 0
# ERROR: 20

The full test log is here: 
https://github.com/loongarch64/grub/files/9216733/test-suite.log

Xiaotian Wu (9):
  PE: Add LoongArch definitions
  Add LoongArch definitions
  LoongArch: Add setjmp implementation
  LoongArch: Add early startup code
  LoongArch: Add stubs for Linux loading commands
  LoongArch: Add awareness for LoongArch relocations
  LoongArch: Add auxiliary files
  LoongArch: Add to build system
  tests: add support for LoongArch

 Makefile.util.def|   1 +
 configure.ac |  22 ++-
 gentpl.py|  25 +--
 grub-core/Makefile.am|   6 +
 grub-core/Makefile.core.def  |  16 ++
 grub-core/kern/dl.c  |   9 +-
 grub-core/kern/efi/mm.c  |   3 +-
 grub-core/kern/loongarch64/cache.c   |  39 +
 grub-core/kern/loongarch64/cache_flush.S |  33 
 grub-core/kern/loongarch64/dl.c  | 102 
 grub-core/kern/loongarch64/dl_helper.c   | 198 +++
 grub-core/kern/loongarch64/efi/init.c|  77 +
 grub-core/kern/loongarch64/efi/startup.S |  34 
 grub-core/lib/efi/halt.c |   2 +-
 grub-core/lib/loongarch64/setjmp.S   |  69 
 grub-core/lib/setjmp.S   |   2 +
 grub-core/loader/loongarch64/linux.c |  59 +++
 include/grub/dl.h|   1 +
 include/grub/efi/api.h   |   2 +-
 include/grub/efi/efi.h   |   2 +-
 include/grub/efi/pe32.h  |  36 +++--
 include/grub/elf.h   |  23 +++
 include/grub/loongarch64/efi/memory.h|  24 +++
 include/grub/loongarch64/linux.h |  31 
 include/grub/loongarch64/reloc.h | 107 
 include/grub/loongarch64/setjmp.h|  27 
 include/grub/loongarch64/time.h  |  28 
 include/grub/loongarch64/types.h |  34 
 include/grub/util/install.h  |   1 +
 tests/ahci_test.in   |   2 +-
 tests/ehci_test.in   |   2 +-
 tests/ohci_test.in   |   2 +-
 tests/partmap_test.in|   2 +-
 tests/pata_test.in   |   2 +-
 tests/uhci_test.in   |   2 +-
 tests/util/grub-shell.in |  16 ++
 util/grub-install-common.c   |  49 +++---
 util/grub-install.c  |  16 ++
 util/grub-mkimagexx.c|  79 +
 util/grub-mknetdir.c |   1 +
 util/grub-mkrescue.c |   8 +
 util/grub-module-verifier.c  |  26 +++
 util/mkimage.c   |  16 ++
 43 files changed, 1170 insertions(+), 66 deletions(-)
 create mode 100644 grub-core/kern/loongarch64/cache.c
 create mode 100644 grub-core/kern/loongarch64/cache_flush.S
 create mode 100644 grub-core/kern/loongarch64/dl.c
 create mode 100644 grub-core/kern/loongarch64/dl_helper.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/lib/loongarch64/setjmp.S
 create mode 100644 grub-core/loader/loongarch64/linux.c
 create mode 100644 include/grub/loongarch64/efi/memory.h
 create mode 100644 include/grub/loongarch64/linux.h
 create mode 100644 include/grub/loongarch64/reloc.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

-- 
2.35.1


___
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel


[PATCH v5 9/9] tests: add support for LoongArch

2022-07-29 Thread Xiaotian Wu
Signed-off-by: Xiaotian Wu 
---
 tests/ahci_test.in   |  2 +-
 tests/ehci_test.in   |  2 +-
 tests/ohci_test.in   |  2 +-
 tests/partmap_test.in|  2 +-
 tests/pata_test.in   |  2 +-
 tests/uhci_test.in   |  2 +-
 tests/util/grub-shell.in | 16 
 7 files changed, 22 insertions(+), 6 deletions(-)

diff --git a/tests/ahci_test.in b/tests/ahci_test.in
index 6d2e61d4e..70646a24e 100644
--- a/tests/ahci_test.in
+++ b/tests/ahci_test.in
@@ -30,7 +30,7 @@ case "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" in
 mips*-arc | mips*-qemu_mips)
exit 77;;
 # FIXME: No native drivers are available for those
-powerpc-ieee1275 | sparc64-ieee1275 | arm*-efi)
+powerpc-ieee1275 | sparc64-ieee1275 | arm*-efi | loongarch64-efi)
exit 77;;
 esac
 
diff --git a/tests/ehci_test.in b/tests/ehci_test.in
index df671b4b6..bf823a5de 100644
--- a/tests/ehci_test.in
+++ b/tests/ehci_test.in
@@ -30,7 +30,7 @@ case "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" in
 mips*-arc | mips*-qemu_mips)
exit 77;;
 # FIXME: No native drivers are available for those
-powerpc-ieee1275 | sparc64-ieee1275 | arm*-efi)
+powerpc-ieee1275 | sparc64-ieee1275 | arm*-efi | loongarch64-efi)
exit 77;;
 esac
 
diff --git a/tests/ohci_test.in b/tests/ohci_test.in
index 741ad881f..a40d3bc0a 100644
--- a/tests/ohci_test.in
+++ b/tests/ohci_test.in
@@ -30,7 +30,7 @@ case "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" in
 mips*-arc | mips*-qemu_mips)
exit 77;;
 # FIXME: No native drivers are available for those
-powerpc-ieee1275 | sparc64-ieee1275 | arm*-efi)
+powerpc-ieee1275 | sparc64-ieee1275 | arm*-efi | loongarch64-efi)
exit 77;;
 esac
 
diff --git a/tests/partmap_test.in b/tests/partmap_test.in
index 4138e88fe..6db59ce38 100644
--- a/tests/partmap_test.in
+++ b/tests/partmap_test.in
@@ -90,7 +90,7 @@ case "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" in
 mipsel-arc)
disk=arc/scsi0/disk0/rdisk0
;;
-arm*-efi)
+arm*-efi|loongarch64-efi)
disk=hd2
;;
 *)
diff --git a/tests/pata_test.in b/tests/pata_test.in
index 31144a8fd..4d0e7d573 100644
--- a/tests/pata_test.in
+++ b/tests/pata_test.in
@@ -33,7 +33,7 @@ case "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" in
 i386-efi)
exit 77;;
 # FIXME: No native drivers are available for those
-powerpc-ieee1275 | sparc64-ieee1275 | arm*-efi)
+powerpc-ieee1275 | sparc64-ieee1275 | arm*-efi | loongarch64-efi)
exit 77;;
 i386-ieee1275)
disk=hdb
diff --git a/tests/uhci_test.in b/tests/uhci_test.in
index 5aa5eb726..de199a281 100644
--- a/tests/uhci_test.in
+++ b/tests/uhci_test.in
@@ -30,7 +30,7 @@ case "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" in
 mips*-arc | mips*-qemu_mips)
exit 77;;
 # FIXME: No native drivers are available for those
-powerpc-ieee1275 | sparc64-ieee1275 | arm*-efi)
+powerpc-ieee1275 | sparc64-ieee1275 | arm*-efi | loongarch64-efi)
exit 77;;
 esac
 
diff --git a/tests/util/grub-shell.in b/tests/util/grub-shell.in
index 4828afb7c..b82c8243f 100644
--- a/tests/util/grub-shell.in
+++ b/tests/util/grub-shell.in
@@ -194,6 +194,15 @@ case "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" 
in
disk="device virtio-blk-device,drive=hd1 -drive if=none,id=hd1,file="
serial_port=efi0
;;
+loongarch64-efi)
+   qemu=qemu-system-loongarch64
+   boot=hd
+   console=console
+   trim=1
+   qemuopts="-machine virt -cpu la464 -smp 2 -nographic -bios 
/usr/share/qemu/edk2-loongarch64-code.fd $qemuopts"
+   disk="device virtio-blk-pci,drive=hd1 -drive if=none,id=hd1,file="
+   serial_port=
+   ;;
 *)
boot=hd
qemu=qemu-system-i386
@@ -395,6 +404,8 @@ fi
 if [ x$boot = xhd ]; then
 if [ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = arm64-efi ] 
|| [ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = arm-efi ]; then
device="device virtio-blk-device,drive=hd0 -drive if=none,id=hd0,file="
+elif [ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = 
loongarch64-efi ]; then
+   device="device virtio-blk-pci,drive=hd0 -drive if=none,id=hd0,file="
 elif [ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = mips-arc ]; 
then
device="hdb "
 else
@@ -405,6 +416,8 @@ fi
 if [ x$boot = xcd ]; then
 if [ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = arm64-efi ] 
|| [ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = arm-efi ]; then
device="device virtio-blk-device,drive=cd0 -drive 
if=none,id=cd0,media=cdrom,file="
+elif [ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = 
loongarch64-efi ]; then
+   device="device virtio-blk-pci,drive=cd0 -drive 
if=none,id=cd0,media=cdrom,file="
 elif [ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = 
powerpc-ieee1275 ] && [ 

Re: [PATCH v5 2/9] Add LoongArch definitions

2022-07-29 Thread WANG Xuerui

On 2022/7/29 15:11, Xiaotian Wu wrote:

Signed-off-by: Xiaotian Wu 
Signed-off-by: Zhou Yang 
---
  include/grub/elf.h | 23 +++
  1 file changed, 23 insertions(+)

diff --git a/include/grub/elf.h b/include/grub/elf.h
index c478933ee..1c8d4f5d5 100644
--- a/include/grub/elf.h
+++ b/include/grub/elf.h
@@ -248,6 +248,7 @@ typedef struct
  #define EM_NUM95
  #define EM_AARCH64183 /* ARM 64-bit architecture */
  #define EM_RISCV  243 /* RISC-V */
+#define EM_LOONGARCH   258 /* LoongArch */
  
  /* If it is necessary to assign new unofficial EM_* values, please

 pick large random numbers (0x8523, 0xa7f2, etc.) to minimize the
@@ -2531,6 +2532,28 @@ typedef Elf32_Addr Elf32_Conflict;
  #define R_RISCV_SET32   56
  #define R_RISCV_32_PCREL57
  
+/* LoongArch relocations */

+#define R_LARCH_NONE 0
+#define R_LARCH_64   2
+#define R_LARCH_MARK_LA  20
+#define R_LARCH_SOP_PUSH_PCREL   22
+#define R_LARCH_SOP_PUSH_ABSOLUTE23
+#define R_LARCH_SOP_PUSH_PLT_PCREL   29
+#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_538
+#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_S242
+#define R_LARCH_SOP_POP_32_S_5_2043
+#define R_LARCH_SOP_POP_32_S_0_5_10_16_S2 44
+#define R_LARCH_SOP_POP_32_S_0_10_10_16_S245
+


Welp. This almost certainly needs some additional work, now that the 
new-style relocs [1][2] are upstreamed, and bound to be generated with 
the combo of binutils 2.40 trunk and gcc 13.0 trunk. So we'll need to 
support both flavors in order to stay compatible with both older and 
newer toolchains.


Do you think this part could be finished relatively quickly so as to not 
miss the next grub release?


[1]: 
https://sourceware.org/git/?p=binutils-gdb.git;a=commitdiff;h=6d13722a97cee3fd397e116bde3bcedbb1e220be

[2]: https://github.com/loongson/LoongArch-Documentation/pull/57


  #ifdef GRUB_TARGET_WORDSIZE
  #if GRUB_TARGET_WORDSIZE == 32
  


___
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel


[PATCH v3 0/3] Cryptomount detached headers

2022-07-29 Thread brutser--- via Grub-devel

testing detached header failed:



1. built grub payload with following modules: ahci usb_keyboard part_msdos 
part_gpt at_keyboard cbfs cryptodisk luks2 lvm gcry_rijndael gcry_sha1 
gcry_sha256 gcry_sha512

2. encrypt a partition: cryptsetup luksFormat --type luks2 -q -h sha512 -s 512 
--pbkdf pbkdf2 --header /path/to/header --luks2-metadata-size=16k 
--luks2-keyslots-size=512k /dev/sda1

(where --luks2-metadata-size=16k --luks2-keyslots-size=512k is optional, this 
is just to minimize header size, but I also tested without).

3. from the grub cmd, i try to decrypt this partition using: cryptomount -H 
/path/to/header (ahci0,msdos1)



4. I also tried luks1 encryption with detached header.



whatever I try, I always get the same error:

"no cryptodisk module can handle this device"



Is this feature not 100% implemented yet, I saw people already verifying the 
patches and would expect this to be working, so if yes, this seems like a bug.



___
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel


Re: [PATCH v3 0/3] Cryptomount detached headers

2022-07-29 Thread Glenn Washburn
On Fri, 29 Jul 2022 20:56:18 +0200 (CEST)
brut...@perso.be wrote:

> 
> testing detached header failed:
> 
> 
> 
> 1. built grub payload with following modules: ahci usb_keyboard part_msdos 
> part_gpt at_keyboard cbfs cryptodisk luks2 lvm gcry_rijndael gcry_sha1 
> gcry_sha256 gcry_sha512
> 
> 2. encrypt a partition: cryptsetup luksFormat --type luks2 -q -h sha512 -s 
> 512 --pbkdf pbkdf2 --header /path/to/header --luks2-metadata-size=16k 
> --luks2-keyslots-size=512k /dev/sda1
> 
> (where --luks2-metadata-size=16k --luks2-keyslots-size=512k is optional, this 
> is just to minimize header size, but I also tested without).
> 
> 3. from the grub cmd, i try to decrypt this partition using: cryptomount -H 
> /path/to/header (ahci0,msdos1)
> 
> 
> 
> 4. I also tried luks1 encryption with detached header.
> 
> 
> 
> whatever I try, I always get the same error:
> 
> "no cryptodisk module can handle this device"
> 
> 
> 
> Is this feature not 100% implemented yet, I saw people already verifying the 
> patches and would expect this to be working, so if yes, this seems like a bug.

This feature should be working in all cases, and if not there may be a
bug. I responded to your off-list email before seeing this one. I'll
repeat what I said there and let's continue this discussion on the list.

I see nothing obviously wrong with what you're doing, given the
information above. To further debug this, would you be able to send a
log of the serial output when the GRUB envvar debug is set to "all"
while running the cryptomount command? If so, please send compressed in
a reply to this email on the list.

If you can't because of hardware issues, would you be able to replicate
this in QEMU and grab the serial output from there? If you can boot the
system via other means, you should be able to use the raw disks (the
one with the LUKS volume and the other with the filesystem containing
the header file).

Glenn


___
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel


Re: [PATCH v3 0/3] Cryptomount detached headers

2022-07-29 Thread brutser--- via Grub-devel
Hi Glenn, 



To explain in more detail how I run my tests, because the whole picture can 
give you a better understanding as to why it fails with me:

1. As grub payload is used for coreboot, I first build coreboot for the system 
(default build, nothing special).

2. To build grub:

git clone https://git.savannah.gnu.org/git/grub.git

./bootstrap

./autogen.sh

./configure --with-platform=coreboot --disable-werror

make



3. Change modules in Makefile to match the ones I wrote earlier.

4. make default_payload.elf



5. Installation debian (expert install)

6. Encrypt partition

cryptsetup luksFormat --type luks2 -q -h sha512 -s 512 --pbkdf pbkdf2 --header 
/path/to/header --luks2-metadata-size=16k --luks2-keyslots-size=512k /dev/sda1

or LUKS1:

cryptsetup luksFormat --cipher aes-xts-plain64 --hash=sha256 --key-size=512 
--header /path/to/header --type luks1 /dev/sda1

7. Create necessary logical volumes and start installation debian

8. add crypttab, copy the header and keyfiles to target system.



This exact same setup works fine with grub 2.04 and john lane's patches: 
https://grub.johnlane.ie/ (obviously only LUKS1 support).



I will try to debug, not really experience with that, but will try to figure it 
out.



Van: Glenn Washburn 
Aan: brut...@perso.be
Onderwerp: Re: [PATCH v3 0/3] Cryptomount detached headers
Datum: 29/07/2022 21:27:48 Europe/Paris
Cc: grub-devel@gnu.org;
   dki...@net-space.pl;
   p...@pks.im

On Fri, 29 Jul 2022 20:56:18 +0200 (CEST)
brut...@perso.be wrote:

> 
> testing detached header failed:
> 
> 
> 
> 1. built grub payload with following modules: ahci usb_keyboard part_msdos 
> part_gpt at_keyboard cbfs cryptodisk luks2 lvm gcry_rijndael gcry_sha1 
> gcry_sha256 gcry_sha512
> 
> 2. encrypt a partition: cryptsetup luksFormat --type luks2 -q -h sha512 -s 
> 512 --pbkdf pbkdf2 --header /path/to/header --luks2-metadata-size=16k 
> --luks2-keyslots-size=512k /dev/sda1
> 
> (where --luks2-metadata-size=16k --luks2-keyslots-size=512k is optional, this 
> is just to minimize header size, but I also tested without).
> 
> 3. from the grub cmd, i try to decrypt this partition using: cryptomount -H 
> /path/to/header (ahci0,msdos1)
> 
> 
> 
> 4. I also tried luks1 encryption with detached header.
> 
> 
> 
> whatever I try, I always get the same error:
> 
> "no cryptodisk module can handle this device"
> 
> 
> 
> Is this feature not 100% implemented yet, I saw people already verifying the 
> patches and would expect this to be working, so if yes, this seems like a bug.

This feature should be working in all cases, and if not there may be a
bug. I responded to your off-list email before seeing this one. I'll
repeat what I said there and let's continue this discussion on the list.

I see nothing obviously wrong with what you're doing, given the
information above. To further debug this, would you be able to send a
log of the serial output when the GRUB envvar debug is set to "all"
while running the cryptomount command? If so, please send compressed in
a reply to this email on the list.

If you can't because of hardware issues, would you be able to replicate
this in QEMU and grab the serial output from there? If you can boot the
system via other means, you should be able to use the raw disks (the
one with the LUKS volume and the other with the filesystem containing
the header file).

Glenn


___
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel

___
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel


Re: [PATCH v5 0/9] Add support for LoongArch

2022-07-29 Thread Glenn Washburn
On Fri, 29 Jul 2022 15:11:52 +0800
Xiaotian Wu  wrote:

> LoongArch is a new Loongson 3A5000 CPU instruction set, you can read
> documents[1] or visit the development community[2] to get more information.
> 
> [1]: https://loongson.github.io/LoongArch-Documentation/README-EN.html
> [2]: https://github.com/loongson
> 
> This patch series will add the basic support for LoongArch architecture, it 
> can
> compile on LoongArch and display the GRUB UI.
> 
> The full LoongArch patch has two parts, this is the first part.
> 
> The second part needs to wait for the LoadFile2 patch to be merged upstream 
> first.
> See: https://github.com/loongarch64/grub/tree/dev/patchwork/efi
> 
> Please review the patches, thank you.
> 
> v1->v2:
> - rebase on the last commit.
> - fix some errors.
> - change the year to 2022 for the new files.
> 
> v2->v3:
> - based on the last commit.
> - add a comment to setjmp.S
> - complete LoongArch support for grub-mkrescue
> 
> v3->v4:
> - based on the last commit.
> 
> v4->v5:
> - Fix make distcheck failed.
> - Add tests support.
> - based on the last commit.
> 
> Here is the test results:
> 
> =
>GRUB 2.11: ./test-suite.log
> =
> 
> # TOTAL: 82
> # PASS:  52
> # SKIP:  8
> # XFAIL: 0
> # FAIL:  2
> # XPASS: 0
> # ERROR: 20
> 
> The full test log is here: 
> https://github.com/loongarch64/grub/files/9216733/test-suite.log

Thanks for doing this testing. What are the values passed for the
configure options --build, --host, and --target?

Also, would you be able to run these tests again on a system with as
many of the make check test prerequisites installed? They are listed in
the INSTALL file in the repository root. This would allow the testing
of various GRUB filesystem code which is not happening now (the reason
many tests fail with ERROR).

There are two actual failure in the provided log. One is the functional
test, which are not expected to be working. The other is the partmap
test, which should be working. Its not clear exactly which sub-test of
the partmap test is failing (due to output not being very verbose). If
you add 'set -x' to the top of partmap_test and rerun the tests, I can
get a clearer picture of what's going on.

Glenn

> 
> Xiaotian Wu (9):
>   PE: Add LoongArch definitions
>   Add LoongArch definitions
>   LoongArch: Add setjmp implementation
>   LoongArch: Add early startup code
>   LoongArch: Add stubs for Linux loading commands
>   LoongArch: Add awareness for LoongArch relocations
>   LoongArch: Add auxiliary files
>   LoongArch: Add to build system
>   tests: add support for LoongArch
> 
>  Makefile.util.def|   1 +
>  configure.ac |  22 ++-
>  gentpl.py|  25 +--
>  grub-core/Makefile.am|   6 +
>  grub-core/Makefile.core.def  |  16 ++
>  grub-core/kern/dl.c  |   9 +-
>  grub-core/kern/efi/mm.c  |   3 +-
>  grub-core/kern/loongarch64/cache.c   |  39 +
>  grub-core/kern/loongarch64/cache_flush.S |  33 
>  grub-core/kern/loongarch64/dl.c  | 102 
>  grub-core/kern/loongarch64/dl_helper.c   | 198 +++
>  grub-core/kern/loongarch64/efi/init.c|  77 +
>  grub-core/kern/loongarch64/efi/startup.S |  34 
>  grub-core/lib/efi/halt.c |   2 +-
>  grub-core/lib/loongarch64/setjmp.S   |  69 
>  grub-core/lib/setjmp.S   |   2 +
>  grub-core/loader/loongarch64/linux.c |  59 +++
>  include/grub/dl.h|   1 +
>  include/grub/efi/api.h   |   2 +-
>  include/grub/efi/efi.h   |   2 +-
>  include/grub/efi/pe32.h  |  36 +++--
>  include/grub/elf.h   |  23 +++
>  include/grub/loongarch64/efi/memory.h|  24 +++
>  include/grub/loongarch64/linux.h |  31 
>  include/grub/loongarch64/reloc.h | 107 
>  include/grub/loongarch64/setjmp.h|  27 
>  include/grub/loongarch64/time.h  |  28 
>  include/grub/loongarch64/types.h |  34 
>  include/grub/util/install.h  |   1 +
>  tests/ahci_test.in   |   2 +-
>  tests/ehci_test.in   |   2 +-
>  tests/ohci_test.in   |   2 +-
>  tests/partmap_test.in|   2 +-
>  tests/pata_test.in   |   2 +-
>  tests/uhci_test.in   |   2 +-
>  tests/util/grub-shell.in |  16 ++
>  util/grub-install-common.c   |  49 +++---
>  util/grub-install.c  |  16 ++
>  util/grub-mkimagexx.c|  79 +
>  util/grub-mknetdir.c |   1 +
>  util/grub-mkrescue.c |   8 +
>  util/grub-module-verifier.c  |  26 +++
>  util/mkimage.c

Re: Re: [PATCH v5 0/9] Add support for LoongArch

2022-07-29 Thread 武校田
Thanks for your reply.

> Thanks for doing this testing. What are the values passed for the
> configure options --build, --host, and --target?

I compiled it on my local machine, using the Archlinux 2022.03 LoongArch
version, the iso and repository are here: 
https://mirrors.wsyu.edu.cn/loongarch/2022.03/

> Also, would you be able to run these tests again on a system with as
> many of the make check test prerequisites installed? They are listed in
> the INSTALL file in the repository root. This would allow the testing
> of various GRUB filesystem code which is not happening now (the reason
> many tests fail with ERROR).

I will check the dependencies required by grub-fs-tester, thanks.

> There are two actual failure in the provided log. One is the functional
> test, which are not expected to be working. The other is the partmap
> test, which should be working. Its not clear exactly which sub-test of
> the partmap test is failing (due to output not being very verbose). If
> you add 'set -x' to the top of partmap_test and rerun the tests, I can
> get a clearer picture of what's going on.
> 
> Glenn
> 

The details of this partmap_test are as follows:

+ echo 'Checking MSDOS partition types...'
Checking MSDOS partition types...
+ create_disk_image /tmp/tmp.N6QvM6XPXY 64
+ name=/tmp/tmp.N6QvM6XPXY
+ size=64
+ rm -f /tmp/tmp.N6QvM6XPXY
+ dd if=/dev/zero of=/tmp/tmp.N6QvM6XPXY bs=512 count=1 seek=131071 
status=noxfer
记录了1+0 的读入
记录了1+0 的写出
+ parted -a none -s /tmp/tmp.N6QvM6XPXY mklabel msdos
+ list_parts part_msdos /tmp/tmp.N6QvM6XPXY /tmp/tmp.dOULt2UJMN
+ mod=part_msdos
+ shift
+ imgfile=/tmp/tmp.N6QvM6XPXY
+ shift
+ outfile=/tmp/tmp.dOULt2UJMN
+ shift
+ ./grub-shell --disk=/tmp/tmp.N6QvM6XPXY --modules=part_msdos
+ echo ls
WARNING: Image format was not specified for '/tmp/tmp.N6QvM6XPXY' and probing 
guessed raw.
 Automatically detecting the format is dangerous for raw images, write 
operations on block 0 will be restricted.
 Specify the 'raw' format explicitly to remove the restrictions.
WARNING: Image format was not specified for '/tmp/tmp.mQzmZs2ROc' and probing 
guessed raw.
 Automatically detecting the format is dangerous for raw images, write 
operations on block 0 will be restricted.
 Specify the 'raw' format explicitly to remove the restrictions.
+ cat /tmp/tmp.dOULt2UJMN
+ tr -d '\n\r'
(hd0) (hd1) (hd1,gpt3) (hd1,gpt2,msdos1) (hd1,gpt2) (hd1,gpt1) + echo

+ check_output /tmp/tmp.dOULt2UJMN hd2
+ outfile=/tmp/tmp.dOULt2UJMN
+ shift
+ for dsk in $@
+ grep '(hd2)' /tmp/tmp.dOULt2UJMN
++ cat /tmp/tmp.dOULt2UJMN
+ echo '(hd2): disk/partiton not found in: (hd0) (hd1) (hd1,gpt3) 
(hd1,gpt2,msdos1) (hd1,gpt2) (hd1,gpt1) '
(hd2): disk/partiton not found in: (hd0) (hd1) (hd1,gpt3) (hd1,gpt2,msdos1) 
(hd1,gpt2) (hd1,gpt1) 
+ exit 1
FAIL partmap_test (exit status: 1)

It looks like it can't find '(hd2)' in the output.

--
Best Regards
Xiaotian Wu


本邮件及其附件含有龙芯中科的商业秘密信息,仅限于发送给上面地址中列出的个人或群组。禁止任何其他人以任何形式使用(包括但不限于全部或部分地泄露、复制或散发)本邮件及其附件中的信息。如果您错收本邮件,请您立即电话或邮件通知发件人并删除本邮件。
 
This email and its attachments contain confidential information from Loongson 
Technology , which is intended only for the person or entity whose address is 
listed above. Any use of the information contained herein in any way 
(including, but not limited to, total or partial disclosure, reproduction or 
dissemination) by persons other than the intended recipient(s) is prohibited. 
If you receive this email in error, please notify the sender by phone or email 
immediately and delete it. 
___
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel


Re: [PATCH v3 0/3] Cryptomount detached headers

2022-07-29 Thread Maxim Fomin
--- Original Message ---

On Friday, July 29th, 2022 at 6:56 PM, brutser--- via Grub-devel 
 wrote:

> testing detached header failed:
>
> 1. built grub payload with following modules: ahci usb_keyboard part_msdos 
> part_gpt at_keyboard cbfs cryptodisk luks2 lvm gcry_rijndael gcry_sha1 
> gcry_sha256 gcry_sha512
> 2. encrypt a partition: cryptsetup luksFormat --type luks2 -q -h sha512 -s 
> 512 --pbkdf pbkdf2 --header /path/to/header --luks2-metadata-size=16k 
> --luks2-keyslots-size=512k /dev/sda1
> (where --luks2-metadata-size=16k --luks2-keyslots-size=512k is optional, this 
> is just to minimize header size, but I also tested without).
> 3. from the grub cmd, i try to decrypt this partition using: cryptomount -H 
> /path/to/header (ahci0,msdos1)
>
> 4. I also tried luks1 encryption with detached header.
>
> whatever I try, I always get the same error:
> "no cryptodisk module can handle this device"
>
> Is this feature not 100% implemented yet, I saw people already verifying the 
> patches and would expect this to be working, so if yes, this seems like a bug.

This error message sounds like luks (or luks2) module was not loaded. Did you 
load it before running cryptomount command?

Best regards,
Maxim Fomin​___
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel