diff mbox series

[v6,10/11] RISC-V: Add to build system

Message ID 20190212103108.56963-11-agraf@suse.de
State Accepted
Commit f1957dc8a3347278a095bc8f44197662559a8ba3
Headers show
Series Add RISC-V support | expand

Commit Message

Alexander Graf Feb. 12, 2019, 10:31 a.m. UTC
This patch adds support for RISC-V to the grub build system. With this
patch, I can successfully build grub on RISC-V as a UEFI application.

Signed-off-by: Alexander Graf <agraf@suse.de>

Reviewed-by: Alistair Francis <alistair.francis@wdc.com>

Reviewed-by: Bin Meng <bmeng.cn@gmail.com>

Tested-by: Bin Meng <bmeng.cn@gmail.com>


---

v2 -> v3:

  - Fix riscv32 target
---
 configure.ac                 | 28 ++++++++++++++++++++++++++--
 gentpl.py                    | 11 +++++++----
 grub-core/Makefile.am        | 12 ++++++++++++
 grub-core/Makefile.core.def  | 29 +++++++++++++++++++++++++++++
 grub-core/commands/file.c    | 14 +++++++++++++-
 grub-core/kern/compiler-rt.c |  6 ++++--
 grub-core/kern/efi/mm.c      |  2 +-
 grub-core/kern/emu/cache.c   |  6 ++++++
 grub-core/kern/emu/cache_s.S |  1 +
 grub-core/kern/emu/lite.c    |  2 ++
 grub-core/lib/efi/halt.c     |  3 ++-
 grub-core/lib/setjmp.S       |  2 ++
 include/grub/compiler-rt.h   | 12 ++++++++----
 include/grub/efi/api.h       |  3 ++-
 include/grub/efi/efi.h       |  2 +-
 include/grub/misc.h          |  3 ++-
 include/grub/util/install.h  |  2 ++
 util/grub-install-common.c   |  2 ++
 util/grub-install.c          | 28 ++++++++++++++++++++++++++++
 util/grub-mknetdir.c         |  4 +++-
 util/grub-mkrescue.c         | 16 +++++++++++++++-
 util/mkimage.c               | 32 ++++++++++++++++++++++++++++++++
 22 files changed, 200 insertions(+), 20 deletions(-)

-- 
2.12.3


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

Comments

Daniel Kiper Feb. 18, 2019, 8:14 p.m. UTC | #1
On Tue, Feb 12, 2019 at 11:31:07AM +0100, Alexander Graf wrote:
> This patch adds support for RISC-V to the grub build system. With this

> patch, I can successfully build grub on RISC-V as a UEFI application.

>

> Signed-off-by: Alexander Graf <agraf@suse.de>

> Reviewed-by: Alistair Francis <alistair.francis@wdc.com>

> Reviewed-by: Bin Meng <bmeng.cn@gmail.com>

> Tested-by: Bin Meng <bmeng.cn@gmail.com>


Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>


But two nitpicks below...

[...]

> diff --git a/grub-core/kern/emu/cache.c b/grub-core/kern/emu/cache.c

> index 6f89e871a..113682cc4 100644

> --- a/grub-core/kern/emu/cache.c

> +++ b/grub-core/kern/emu/cache.c

> @@ -25,5 +25,11 @@ grub_arch_sync_caches (void *address, grub_size_t len)

>    return _flush_cache (address, len, 0);

>  }

>

> +#elif defined(__riscv)

> +void

> +grub_arch_sync_caches (void *address, grub_size_t len)

> +{

> +}

> +


Redundant empty line.

>  #endif

>

> diff --git a/grub-core/kern/emu/cache_s.S b/grub-core/kern/emu/cache_s.S

> index 76cf7560d..e27b865f9 100644

> --- a/grub-core/kern/emu/cache_s.S

> +++ b/grub-core/kern/emu/cache_s.S

> @@ -9,6 +9,7 @@

>  #elif defined(__powerpc__)

>  #include "../powerpc/cache.S"

>  #elif defined(__ia64__) || defined(__arm__) || defined(__aarch64__) || defined(__mips__)

> +#elif defined(__riscv)


Why not

#elif defined(__ia64__) || defined(__arm__) || defined(__aarch64__) || \
      defined(__mips__) || defined(__riscv)?

>  #else

>  #error "No target cpu type is defined"

>  #endif


Daniel

_______________________________________________
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel
Alexander Graf Feb. 19, 2019, 1:44 p.m. UTC | #2
On 02/18/2019 09:14 PM, Daniel Kiper wrote:
> On Tue, Feb 12, 2019 at 11:31:07AM +0100, Alexander Graf wrote:

>> This patch adds support for RISC-V to the grub build system. With this

>> patch, I can successfully build grub on RISC-V as a UEFI application.

>>

>> Signed-off-by: Alexander Graf <agraf@suse.de>

>> Reviewed-by: Alistair Francis <alistair.francis@wdc.com>

>> Reviewed-by: Bin Meng <bmeng.cn@gmail.com>

>> Tested-by: Bin Meng <bmeng.cn@gmail.com>

> Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>

>

> But two nitpicks below...

>

> [...]

>

>> diff --git a/grub-core/kern/emu/cache.c b/grub-core/kern/emu/cache.c

>> index 6f89e871a..113682cc4 100644

>> --- a/grub-core/kern/emu/cache.c

>> +++ b/grub-core/kern/emu/cache.c

>> @@ -25,5 +25,11 @@ grub_arch_sync_caches (void *address, grub_size_t len)

>>     return _flush_cache (address, len, 0);

>>   }

>>

>> +#elif defined(__riscv)

>> +void

>> +grub_arch_sync_caches (void *address, grub_size_t len)

>> +{

>> +}

>> +

> Redundant empty line.

>

>>   #endif

>>

>> diff --git a/grub-core/kern/emu/cache_s.S b/grub-core/kern/emu/cache_s.S

>> index 76cf7560d..e27b865f9 100644

>> --- a/grub-core/kern/emu/cache_s.S

>> +++ b/grub-core/kern/emu/cache_s.S

>> @@ -9,6 +9,7 @@

>>   #elif defined(__powerpc__)

>>   #include "../powerpc/cache.S"

>>   #elif defined(__ia64__) || defined(__arm__) || defined(__aarch64__) || defined(__mips__)

>> +#elif defined(__riscv)

> Why not

>

> #elif defined(__ia64__) || defined(__arm__) || defined(__aarch64__) || \

>        defined(__mips__) || defined(__riscv)?


Mostly because I dislike multiline preprocessor macros :).


Alex


_______________________________________________
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel
Daniel Kiper Feb. 20, 2019, 8:36 p.m. UTC | #3
On Tue, Feb 19, 2019 at 02:44:06PM +0100, Alexander Graf wrote:
> On 02/18/2019 09:14 PM, Daniel Kiper wrote:

> > On Tue, Feb 12, 2019 at 11:31:07AM +0100, Alexander Graf wrote:

> > > This patch adds support for RISC-V to the grub build system. With this

> > > patch, I can successfully build grub on RISC-V as a UEFI application.

> > >

> > > Signed-off-by: Alexander Graf <agraf@suse.de>

> > > Reviewed-by: Alistair Francis <alistair.francis@wdc.com>

> > > Reviewed-by: Bin Meng <bmeng.cn@gmail.com>

> > > Tested-by: Bin Meng <bmeng.cn@gmail.com>

> > Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>

> >

> > But two nitpicks below...

> >

> > [...]

> >

> > > diff --git a/grub-core/kern/emu/cache.c b/grub-core/kern/emu/cache.c

> > > index 6f89e871a..113682cc4 100644

> > > --- a/grub-core/kern/emu/cache.c

> > > +++ b/grub-core/kern/emu/cache.c

> > > @@ -25,5 +25,11 @@ grub_arch_sync_caches (void *address, grub_size_t len)

> > >     return _flush_cache (address, len, 0);

> > >   }

> > >

> > > +#elif defined(__riscv)

> > > +void

> > > +grub_arch_sync_caches (void *address, grub_size_t len)

> > > +{

> > > +}

> > > +

> > Redundant empty line.

> >

> > >   #endif

> > >

> > > diff --git a/grub-core/kern/emu/cache_s.S b/grub-core/kern/emu/cache_s.S

> > > index 76cf7560d..e27b865f9 100644

> > > --- a/grub-core/kern/emu/cache_s.S

> > > +++ b/grub-core/kern/emu/cache_s.S

> > > @@ -9,6 +9,7 @@

> > >   #elif defined(__powerpc__)

> > >   #include "../powerpc/cache.S"

> > >   #elif defined(__ia64__) || defined(__arm__) || defined(__aarch64__) || defined(__mips__)

> > > +#elif defined(__riscv)

> > Why not

> >

> > #elif defined(__ia64__) || defined(__arm__) || defined(__aarch64__) || \

> >        defined(__mips__) || defined(__riscv)?

>

> Mostly because I dislike multiline preprocessor macros :).


I hope that you will not object if I replace it with my version... :-)))

Daniel

_______________________________________________
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel
diff mbox series

Patch

diff --git a/configure.ac b/configure.ac
index 81a19afd5..e36195266 100644
--- a/configure.ac
+++ b/configure.ac
@@ -104,6 +104,12 @@  case "$target_cpu" in
   aarch64*)
 		target_cpu=arm64
 		;;
+  riscv32*)
+		target_cpu=riscv32
+		;;
+  riscv64*)
+		target_cpu=riscv64
+		;;
 esac
 
 # Specify the platform (such as firmware).
@@ -127,6 +133,8 @@  if test "x$with_platform" = x; then
     ia64-*) platform=efi ;;
     arm-*) platform=uboot ;;
     arm64-*) platform=efi ;;
+    riscv32-*) platform=efi ;;
+    riscv64-*) platform=efi ;;
     *)
       AC_MSG_WARN([unsupported CPU: "$target_cpu" - only building utilities])
       platform=none
@@ -175,6 +183,8 @@  case "$target_cpu"-"$platform" in
   arm-coreboot) ;;
   arm-efi) ;;
   arm64-efi) ;;
+  riscv32-efi) ;;
+  riscv64-efi) ;;
   *-emu) ;;
   *-none) ;;
   *) AC_MSG_ERROR([platform "$platform" is not supported for target CPU "$target_cpu"]) ;;
@@ -828,6 +838,16 @@  if test x"$platform" != xemu ; then
        AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]], [[]])],
 		         [grub_cv_target_cc_soft_float="-mgeneral-regs-only"], [])
     fi
+    if test "x$target_cpu" = xriscv32; then
+       CFLAGS="$TARGET_CFLAGS -march=rv32imac -mabi=ilp32 -Werror"
+       AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]], [[]])],
+		         [grub_cv_target_cc_soft_float="-march=rv32imac -mabi=ilp32"], [])
+    fi
+    if test "x$target_cpu" = xriscv64; then
+       CFLAGS="$TARGET_CFLAGS -march=rv64imac -mabi=lp64 -Werror"
+       AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]], [[]])],
+		         [grub_cv_target_cc_soft_float="-march=rv64imac -mabi=lp64"], [])
+    fi
     if test "x$target_cpu" = xia64; then
        CFLAGS="$TARGET_CFLAGS -mno-inline-float-divide -mno-inline-sqrt -Werror"
        AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]], [[]])],
@@ -1143,7 +1163,7 @@  AC_SUBST(TARGET_LDFLAGS_OLDMAGIC)
 
 LDFLAGS="$TARGET_LDFLAGS"
 
-if test "$target_cpu" = x86_64 || test "$target_cpu" = sparc64 ; then
+if test "$target_cpu" = x86_64 || test "$target_cpu" = sparc64 || test "$target_cpu" = riscv64 ; 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"
@@ -1153,7 +1173,7 @@  if test "$target_cpu" = x86_64 || test "$target_cpu" = sparc64 ; then
   ])
   if test "x$grub_cv_cc_mcmodel" = xyes; then
     TARGET_CFLAGS="$TARGET_CFLAGS -mcmodel=large"
-  elif test "$target_cpu" = sparc64; then
+  elif test "$target_cpu" = sparc64 || test "$target_cpu" = riscv64; then
     TARGET_CFLAGS="$TARGET_CFLAGS -mcmodel=medany"
   fi
 fi
@@ -1916,6 +1936,10 @@  AM_CONDITIONAL([COND_arm_coreboot], [test x$target_cpu = xarm -a x$platform = xc
 AM_CONDITIONAL([COND_arm_efi], [test x$target_cpu = xarm -a x$platform = xefi])
 AM_CONDITIONAL([COND_arm64], [test x$target_cpu = xarm64 ])
 AM_CONDITIONAL([COND_arm64_efi], [test x$target_cpu = xarm64 -a x$platform = xefi])
+AM_CONDITIONAL([COND_riscv32], [test x$target_cpu = xriscv32 ])
+AM_CONDITIONAL([COND_riscv64], [test x$target_cpu = xriscv64 ])
+AM_CONDITIONAL([COND_riscv32_efi], [test x$target_cpu = xriscv32 -a x$platform = xefi])
+AM_CONDITIONAL([COND_riscv64_efi], [test x$target_cpu = xriscv64 -a x$platform = xefi])
 
 AM_CONDITIONAL([COND_HOST_HURD], [test x$host_kernel = xhurd])
 AM_CONDITIONAL([COND_HOST_LINUX], [test x$host_kernel = xlinux])
diff --git a/gentpl.py b/gentpl.py
index e8439484a..387588c05 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", "riscv32_efi", "riscv64_efi" ]
 
 GROUPS = {}
 
@@ -47,9 +47,12 @@  GROUPS["sparc64"]  = [ "sparc64_ieee1275" ]
 GROUPS["powerpc"]  = [ "powerpc_ieee1275" ]
 GROUPS["arm"]      = [ "arm_uboot", "arm_efi", "arm_coreboot" ]
 GROUPS["arm64"]    = [ "arm64_efi" ]
+GROUPS["riscv32"]  = [ "riscv32_efi" ]
+GROUPS["riscv64"]  = [ "riscv64_efi" ]
 
 # Groups based on firmware
-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",
+		   "riscv32_efi", "riscv64_efi" ]
 GROUPS["ieee1275"]   = [ "i386_ieee1275", "sparc64_ieee1275", "powerpc_ieee1275" ]
 GROUPS["uboot"] = [ "arm_uboot" ]
 GROUPS["xen"]  = [ "i386_xen", "x86_64_xen" ]
@@ -76,11 +79,11 @@  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", "riscv32_efi", "riscv64_efi" ]
 
 # Needs software helpers for division
 # Must match GRUB_DIVISION_IN_SOFTWARE in misc.h
-GROUPS["softdiv"] = GROUPS["arm"] + ["ia64_efi"]
+GROUPS["softdiv"] = GROUPS["arm"] + ["ia64_efi"] + GROUPS["riscv32"]
 GROUPS["no_softdiv"]   = GRUB_PLATFORMS[:]
 for i in GROUPS["softdiv"]: GROUPS["no_softdiv"].remove(i)
 
diff --git a/grub-core/Makefile.am b/grub-core/Makefile.am
index df8dbe2fb..3ea8e7ff4 100644
--- a/grub-core/Makefile.am
+++ b/grub-core/Makefile.am
@@ -284,6 +284,18 @@  KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/efi/disk.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/acpi.h
 endif
 
+if COND_riscv32_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
+endif
+
+if COND_riscv64_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
+endif
+
 if COND_emu
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/datetime.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/emu/misc.h
diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def
index e16fb06ba..2346bd291 100644
--- a/grub-core/Makefile.core.def
+++ b/grub-core/Makefile.core.def
@@ -65,6 +65,12 @@  kernel = {
   arm64_efi_ldflags          = '-Wl,-r,-d';
   arm64_efi_stripflags       = '--strip-unneeded -K start -R .note -R .comment -R .note.gnu.gold-version -R .eh_frame';
 
+  riscv32_efi_ldflags      = '-Wl,-r,-d';
+  riscv32_efi_stripflags   = '--strip-unneeded -K start -R .note -R .comment -R .note.gnu.gold-version -R .eh_frame';
+
+  riscv64_efi_ldflags      = '-Wl,-r,-d';
+  riscv64_efi_stripflags   = '--strip-unneeded -K start -R .note -R .comment -R .note.gnu.gold-version -R .eh_frame';
+
   i386_pc_ldflags          = '$(TARGET_IMG_LDFLAGS)';
   i386_pc_ldflags          = '$(TARGET_IMG_BASE_LDOPT),0x9000';
   i386_qemu_ldflags        = '$(TARGET_IMG_LDFLAGS)';
@@ -114,6 +120,8 @@  kernel = {
   arm_coreboot_startup = kern/arm/startup.S;
   arm_efi_startup = kern/arm/efi/startup.S;
   arm64_efi_startup = kern/arm64/efi/startup.S;
+  riscv32_efi_startup = kern/riscv/efi/startup.S;
+  riscv64_efi_startup = kern/riscv/efi/startup.S;
 
   common = kern/command.c;
   common = kern/corecmd.c;
@@ -246,6 +254,12 @@  kernel = {
   arm64_efi = kern/arm64/efi/init.c;
   arm64_efi = kern/efi/fdt.c;
 
+  riscv32_efi = kern/riscv/efi/init.c;
+  riscv32_efi = kern/efi/fdt.c;
+
+  riscv64_efi = kern/riscv/efi/init.c;
+  riscv64_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;
@@ -317,6 +331,14 @@  kernel = {
   arm64 = kern/arm64/dl.c;
   arm64 = kern/arm64/dl_helper.c;
 
+  riscv32 = kern/riscv/cache.c;
+  riscv32 = kern/riscv/cache_flush.S;
+  riscv32 = kern/riscv/dl.c;
+
+  riscv64 = kern/riscv/cache.c;
+  riscv64 = kern/riscv/cache_flush.S;
+  riscv64 = kern/riscv/dl.c;
+
   emu = disk/host.c;
   emu = kern/emu/cache_s.S;
   emu = kern/emu/hostdisk.c;
@@ -792,6 +814,8 @@  module = {
   enable = arm64_efi;
   enable = arm_uboot;
   enable = arm_coreboot;
+  enable = riscv32_efi;
+  enable = riscv64_efi;
 };
 
 module = {
@@ -1649,6 +1673,7 @@  module = {
   extra_dist = lib/ia64/longjmp.S;
   extra_dist = lib/arm/setjmp.S;
   extra_dist = lib/arm64/setjmp.S;
+  extra_dist = lib/riscv/setjmp.S;
 };
 
 module = {
@@ -1750,6 +1775,8 @@  module = {
   arm_efi = loader/arm64/linux.c;
   arm_uboot = loader/arm/linux.c;
   arm64 = loader/arm64/linux.c;
+  riscv32 = loader/riscv/linux.c;
+  riscv64 = loader/riscv/linux.c;
   common = loader/linux.c;
   common = lib/cmdline.c;
   enable = noemu;
@@ -1844,6 +1871,8 @@  module = {
   enable = ia64_efi;
   enable = arm_efi;
   enable = arm64_efi;
+  enable = riscv32_efi;
+  enable = riscv64_efi;
   enable = mips;
 };
 
diff --git a/grub-core/commands/file.c b/grub-core/commands/file.c
index 4f81aa1f9..2574e6685 100644
--- a/grub-core/commands/file.c
+++ b/grub-core/commands/file.c
@@ -90,6 +90,10 @@  static const struct grub_arg_option options[] = {
    N_("Check if FILE is ARM64 EFI file"), 0, 0},
   {"is-arm-efi", 0, 0,
    N_("Check if FILE is ARM EFI file"), 0, 0},
+  {"is-riscv32-efi", 0, 0,
+   N_("Check if FILE is RISC-V 32bit EFI file"), 0, 0},
+  {"is-riscv64-efi", 0, 0,
+   N_("Check if FILE is RISC-V 64bit EFI file"), 0, 0},
   {"is-hibernated-hiberfil", 0, 0,
    N_("Check if FILE is hiberfil.sys in hibernated state"), 0, 0},
   {"is-x86_64-xnu", 0, 0,
@@ -130,6 +134,7 @@  enum
   IS_IA_EFI,
   IS_ARM64_EFI,
   IS_ARM_EFI,
+  IS_RISCV_EFI,
   IS_HIBERNATED,
   IS_XNU64,
   IS_XNU32,
@@ -571,6 +576,7 @@  grub_cmd_file (grub_extcmd_context_t ctxt, int argc, char **args)
     case IS_IA_EFI:
     case IS_ARM64_EFI:
     case IS_ARM_EFI:
+    case IS_RISCV_EFI:
       {
 	char signature[4];
 	grub_uint32_t pe_offset;
@@ -616,7 +622,13 @@  grub_cmd_file (grub_extcmd_context_t ctxt, int argc, char **args)
 	    && coff_head.machine !=
 	    grub_cpu_to_le16_compile_time (GRUB_PE32_MACHINE_ARMTHUMB_MIXED))
 	  break;
-	if (type == IS_IA_EFI || type == IS_64_EFI || type == IS_ARM64_EFI)
+	if (type == IS_RISCV_EFI
+	    && coff_head.machine !=
+	    grub_cpu_to_le16_compile_time (GRUB_PE32_MACHINE_RISCV64))
+          /* TODO: Determine bitness dynamically */
+	  break;
+	if (type == IS_IA_EFI || type == IS_64_EFI || type == IS_ARM64_EFI ||
+	    type == IS_RISCV_EFI)
 	  {
 	    struct grub_pe64_optional_header o64;
 	    if (grub_file_read (file, &o64, sizeof (o64)) != sizeof (o64))
diff --git a/grub-core/kern/compiler-rt.c b/grub-core/kern/compiler-rt.c
index 404902119..aaed3f25d 100644
--- a/grub-core/kern/compiler-rt.c
+++ b/grub-core/kern/compiler-rt.c
@@ -237,7 +237,8 @@  union component64
   };
 };
 
-#if defined (__powerpc__) || defined (__arm__) || defined(__mips__)
+#if defined (__powerpc__) || defined (__arm__) || defined(__mips__) || \
+    (defined(__riscv) && (__riscv_xlen == 32))
 
 /* Based on libgcc2.c from gcc suite.  */
 grub_uint64_t
@@ -343,7 +344,8 @@  __ucmpdi2 (grub_uint64_t a, grub_uint64_t b)
 
 #endif
 
-#if defined (__powerpc__) || defined(__mips__) || defined(__sparc__) || defined(__arm__)
+#if defined (__powerpc__) || defined(__mips__) || defined(__sparc__) || \
+    defined(__arm__) || defined(__riscv)
 
 /* Based on libgcc2.c from gcc suite.  */
 grub_uint32_t
diff --git a/grub-core/kern/efi/mm.c b/grub-core/kern/efi/mm.c
index 42ad7c570..a29af9fa5 100644
--- a/grub-core/kern/efi/mm.c
+++ b/grub-core/kern/efi/mm.c
@@ -634,7 +634,7 @@  grub_efi_mm_init (void)
 		       2 * BYTES_TO_PAGES (MEMORY_MAP_SIZE));
 }
 
-#if defined (__aarch64__) || defined (__arm__)
+#if defined (__aarch64__) || defined (__arm__) || defined (__riscv)
 grub_err_t
 grub_efi_get_ram_base(grub_addr_t *base_addr)
 {
diff --git a/grub-core/kern/emu/cache.c b/grub-core/kern/emu/cache.c
index 6f89e871a..113682cc4 100644
--- a/grub-core/kern/emu/cache.c
+++ b/grub-core/kern/emu/cache.c
@@ -25,5 +25,11 @@  grub_arch_sync_caches (void *address, grub_size_t len)
   return _flush_cache (address, len, 0);
 }
 
+#elif defined(__riscv)
+void
+grub_arch_sync_caches (void *address, grub_size_t len)
+{
+}
+
 #endif
 
diff --git a/grub-core/kern/emu/cache_s.S b/grub-core/kern/emu/cache_s.S
index 76cf7560d..e27b865f9 100644
--- a/grub-core/kern/emu/cache_s.S
+++ b/grub-core/kern/emu/cache_s.S
@@ -9,6 +9,7 @@ 
 #elif defined(__powerpc__)
 #include "../powerpc/cache.S"
 #elif defined(__ia64__) || defined(__arm__) || defined(__aarch64__) || defined(__mips__)
+#elif defined(__riscv)
 #else
 #error "No target cpu type is defined"
 #endif
diff --git a/grub-core/kern/emu/lite.c b/grub-core/kern/emu/lite.c
index b2fc93d7f..b327d4e41 100644
--- a/grub-core/kern/emu/lite.c
+++ b/grub-core/kern/emu/lite.c
@@ -24,6 +24,8 @@ 
 #elif defined(__aarch64__)
 #include "../arm64/dl_helper.c"
 #include "../arm64/dl.c"
+#elif defined(__riscv)
+#include "../riscv/dl.c"
 #else
 #error "No target cpu type is defined"
 #endif
diff --git a/grub-core/lib/efi/halt.c b/grub-core/lib/efi/halt.c
index e9441c844..5859f0498 100644
--- a/grub-core/lib/efi/halt.c
+++ b/grub-core/lib/efi/halt.c
@@ -29,7 +29,8 @@  void
 grub_halt (void)
 {
   grub_machine_fini (GRUB_LOADER_FLAG_NORETURN);
-#if !defined(__ia64__) && !defined(__arm__) && !defined(__aarch64__)
+#if !defined(__ia64__) && !defined(__arm__) && !defined(__aarch64__) && \
+    !defined(__riscv)
   grub_acpi_halt ();
 #endif
   efi_call_4 (grub_efi_system_table->runtime_services->reset_system,
diff --git a/grub-core/lib/setjmp.S b/grub-core/lib/setjmp.S
index f6e4905e2..aa297ab0a 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(__riscv)
+#include "./riscv/setjmp.S"
 #else
 #error "Unknown target cpu type"
 #endif
diff --git a/include/grub/compiler-rt.h b/include/grub/compiler-rt.h
index 2cc69e239..e6a25c6ea 100644
--- a/include/grub/compiler-rt.h
+++ b/include/grub/compiler-rt.h
@@ -53,13 +53,15 @@  EXPORT_FUNC (__umoddi3) (grub_uint64_t a, grub_uint64_t b);
 
 #endif
 
-#if defined (__sparc__) || defined (__powerpc__) || defined (__mips__) || defined (__arm__)
+#if defined (__sparc__) || defined (__powerpc__) || defined (__mips__) || \
+    defined (__arm__) || defined(__riscv)
 unsigned
 EXPORT_FUNC (__ctzdi2) (grub_uint64_t x);
 #define NEED_CTZDI2 1
 #endif
 
-#if defined (__mips__) || defined (__arm__)
+#if defined (__mips__) || defined (__arm__) || \
+    (defined(__riscv) && (__riscv_xlen == 32))
 unsigned
 EXPORT_FUNC (__ctzsi2) (grub_uint32_t x);
 #define NEED_CTZSI2 1
@@ -157,7 +159,8 @@  void EXPORT_FUNC (_savegpr_31) (void);
 
 #endif
 
-#if defined (__powerpc__) || defined(__mips__) || defined (__arm__)
+#if defined (__powerpc__) || defined(__mips__) || defined (__arm__) || \
+    (defined(__riscv) && (__riscv_xlen == 32))
 
 int
 EXPORT_FUNC(__ucmpdi2) (grub_uint64_t a, grub_uint64_t b);
@@ -172,7 +175,8 @@  grub_uint64_t
 EXPORT_FUNC (__lshrdi3) (grub_uint64_t u, int b);
 #endif
 
-#if defined (__powerpc__) || defined(__mips__) || defined(__sparc__) || defined (__arm__)
+#if defined (__powerpc__) || defined(__mips__) || defined(__sparc__) || \
+    defined (__arm__) || defined(__riscv)
 grub_uint32_t
 EXPORT_FUNC(__bswapsi2) (grub_uint32_t u);
 
diff --git a/include/grub/efi/api.h b/include/grub/efi/api.h
index c7c9f0e1d..addcbfa8f 100644
--- a/include/grub/efi/api.h
+++ b/include/grub/efi/api.h
@@ -1686,7 +1686,8 @@  struct grub_efi_block_io
 typedef struct grub_efi_block_io grub_efi_block_io_t;
 
 #if (GRUB_TARGET_SIZEOF_VOID_P == 4) || defined (__ia64__) \
-  || defined (__aarch64__) || defined (__MINGW64__) || defined (__CYGWIN__)
+  || defined (__aarch64__) || defined (__MINGW64__) || defined (__CYGWIN__) \
+  || defined(__riscv)
 
 #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 ec44aef7e..e90e00dc4 100644
--- a/include/grub/efi/efi.h
+++ b/include/grub/efi/efi.h
@@ -90,7 +90,7 @@  extern void (*EXPORT_VAR(grub_efi_net_config)) (grub_efi_handle_t hnd,
 						char **device,
 						char **path);
 
-#if defined(__arm__) || defined(__aarch64__)
+#if defined(__arm__) || defined(__aarch64__) || defined(__riscv)
 void *EXPORT_FUNC(grub_efi_get_firmware_fdt)(void);
 grub_err_t EXPORT_FUNC(grub_efi_get_ram_base)(grub_addr_t *);
 #include <grub/cpu/linux.h>
diff --git a/include/grub/misc.h b/include/grub/misc.h
index 372f009e8..ee48eb7a7 100644
--- a/include/grub/misc.h
+++ b/include/grub/misc.h
@@ -340,7 +340,8 @@  grub_uint64_t EXPORT_FUNC(grub_divmod64) (grub_uint64_t n,
 					  grub_uint64_t *r);
 
 /* Must match softdiv group in gentpl.py.  */
-#if !defined(GRUB_MACHINE_EMU) && (defined(__arm__) || defined(__ia64__))
+#if !defined(GRUB_MACHINE_EMU) && (defined(__arm__) || defined(__ia64__) || \
+    (defined(__riscv) && (__riscv_xlen == 32)))
 #define GRUB_DIVISION_IN_SOFTWARE 1
 #else
 #define GRUB_DIVISION_IN_SOFTWARE 0
diff --git a/include/grub/util/install.h b/include/grub/util/install.h
index af2bf65d7..b1a00154f 100644
--- a/include/grub/util/install.h
+++ b/include/grub/util/install.h
@@ -103,6 +103,8 @@  enum grub_install_plat
     GRUB_INSTALL_PLATFORM_I386_XEN_PVH,
     GRUB_INSTALL_PLATFORM_ARM64_EFI,
     GRUB_INSTALL_PLATFORM_ARM_COREBOOT,
+    GRUB_INSTALL_PLATFORM_RISCV32_EFI,
+    GRUB_INSTALL_PLATFORM_RISCV64_EFI,
     GRUB_INSTALL_PLATFORM_MAX
   };
 
diff --git a/util/grub-install-common.c b/util/grub-install-common.c
index 1b1cb43b4..0a7d68b9b 100644
--- a/util/grub-install-common.c
+++ b/util/grub-install-common.c
@@ -729,6 +729,8 @@  static struct
     [GRUB_INSTALL_PLATFORM_ARM64_EFI] =        { "arm64",   "efi"       },
     [GRUB_INSTALL_PLATFORM_ARM_UBOOT] =        { "arm",     "uboot"     },
     [GRUB_INSTALL_PLATFORM_ARM_COREBOOT] =     { "arm",     "coreboot"  },
+    [GRUB_INSTALL_PLATFORM_RISCV32_EFI] =      { "riscv32", "efi"       },
+    [GRUB_INSTALL_PLATFORM_RISCV64_EFI] =      { "riscv64", "efi"       },
   }; 
 
 char *
diff --git a/util/grub-install.c b/util/grub-install.c
index 4a0a66168..c6c3da331 100644
--- a/util/grub-install.c
+++ b/util/grub-install.c
@@ -477,6 +477,8 @@  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_RISCV32_EFI:
+    case GRUB_INSTALL_PLATFORM_RISCV64_EFI:
     case GRUB_INSTALL_PLATFORM_I386_IEEE1275:
     case GRUB_INSTALL_PLATFORM_SPARC64_IEEE1275:
     case GRUB_INSTALL_PLATFORM_POWERPC_IEEE1275:
@@ -900,6 +902,8 @@  main (int argc, char *argv[])
     case GRUB_INSTALL_PLATFORM_X86_64_EFI:
     case GRUB_INSTALL_PLATFORM_ARM_EFI:
     case GRUB_INSTALL_PLATFORM_ARM64_EFI:
+    case GRUB_INSTALL_PLATFORM_RISCV32_EFI:
+    case GRUB_INSTALL_PLATFORM_RISCV64_EFI:
     case GRUB_INSTALL_PLATFORM_IA64_EFI:
     case GRUB_INSTALL_PLATFORM_I386_IEEE1275:
     case GRUB_INSTALL_PLATFORM_SPARC64_IEEE1275:
@@ -945,6 +949,8 @@  main (int argc, char *argv[])
     case GRUB_INSTALL_PLATFORM_X86_64_EFI:
     case GRUB_INSTALL_PLATFORM_ARM_EFI:
     case GRUB_INSTALL_PLATFORM_ARM64_EFI:
+    case GRUB_INSTALL_PLATFORM_RISCV32_EFI:
+    case GRUB_INSTALL_PLATFORM_RISCV64_EFI:
     case GRUB_INSTALL_PLATFORM_IA64_EFI:
     case GRUB_INSTALL_PLATFORM_I386_IEEE1275:
     case GRUB_INSTALL_PLATFORM_ARM_UBOOT:
@@ -998,6 +1004,8 @@  main (int argc, char *argv[])
     case GRUB_INSTALL_PLATFORM_X86_64_EFI:
     case GRUB_INSTALL_PLATFORM_ARM_EFI:
     case GRUB_INSTALL_PLATFORM_ARM64_EFI:
+    case GRUB_INSTALL_PLATFORM_RISCV32_EFI:
+    case GRUB_INSTALL_PLATFORM_RISCV64_EFI:
     case GRUB_INSTALL_PLATFORM_IA64_EFI:
       is_efi = 1;
       break;
@@ -1111,6 +1119,12 @@  main (int argc, char *argv[])
 	    case GRUB_INSTALL_PLATFORM_ARM64_EFI:
 	      efi_file = "BOOTAA64.EFI";
 	      break;
+	    case GRUB_INSTALL_PLATFORM_RISCV32_EFI:
+	      efi_file = "BOOTRISCV32.EFI";
+	      break;
+	    case GRUB_INSTALL_PLATFORM_RISCV64_EFI:
+	      efi_file = "BOOTRISCV64.EFI";
+	      break;
 	    default:
 	      grub_util_error ("%s", _("You've found a bug"));
 	      break;
@@ -1138,6 +1152,12 @@  main (int argc, char *argv[])
 	    case GRUB_INSTALL_PLATFORM_ARM64_EFI:
 	      efi_file = "grubaa64.efi";
 	      break;
+	    case GRUB_INSTALL_PLATFORM_RISCV32_EFI:
+	      efi_file = "grubriscv32.efi";
+	      break;
+	    case GRUB_INSTALL_PLATFORM_RISCV64_EFI:
+	      efi_file = "grubriscv64.efi";
+	      break;
 	    default:
 	      efi_file = "grub.efi";
 	      break;
@@ -1440,6 +1460,8 @@  main (int argc, char *argv[])
 		  case GRUB_INSTALL_PLATFORM_X86_64_EFI:
 		  case GRUB_INSTALL_PLATFORM_ARM_EFI:
 		  case GRUB_INSTALL_PLATFORM_ARM64_EFI:
+		  case GRUB_INSTALL_PLATFORM_RISCV32_EFI:
+		  case GRUB_INSTALL_PLATFORM_RISCV64_EFI:
 		  case GRUB_INSTALL_PLATFORM_IA64_EFI:
 		    g = grub_util_guess_efi_drive (*curdev);
 		    break;
@@ -1533,6 +1555,8 @@  main (int argc, char *argv[])
     case GRUB_INSTALL_PLATFORM_X86_64_EFI:
     case GRUB_INSTALL_PLATFORM_ARM_EFI:
     case GRUB_INSTALL_PLATFORM_ARM64_EFI:
+    case GRUB_INSTALL_PLATFORM_RISCV32_EFI:
+    case GRUB_INSTALL_PLATFORM_RISCV64_EFI:
     case GRUB_INSTALL_PLATFORM_IA64_EFI:
       core_name = "core.efi";
       snprintf (mkimage_target, sizeof (mkimage_target),
@@ -1636,6 +1660,8 @@  main (int argc, char *argv[])
       break;
     case GRUB_INSTALL_PLATFORM_ARM_EFI:
     case GRUB_INSTALL_PLATFORM_ARM64_EFI:
+    case GRUB_INSTALL_PLATFORM_RISCV32_EFI:
+    case GRUB_INSTALL_PLATFORM_RISCV64_EFI:
     case GRUB_INSTALL_PLATFORM_IA64_EFI:
     case GRUB_INSTALL_PLATFORM_MIPSEL_QEMU_MIPS:
     case GRUB_INSTALL_PLATFORM_MIPS_QEMU_MIPS:
@@ -1871,6 +1897,8 @@  main (int argc, char *argv[])
       /* FALLTHROUGH */
     case GRUB_INSTALL_PLATFORM_ARM_EFI:
     case GRUB_INSTALL_PLATFORM_ARM64_EFI:
+    case GRUB_INSTALL_PLATFORM_RISCV32_EFI:
+    case GRUB_INSTALL_PLATFORM_RISCV64_EFI:
     case GRUB_INSTALL_PLATFORM_IA64_EFI:
       {
 	char *dst = grub_util_path_concat (2, efidir, efi_file);
diff --git a/util/grub-mknetdir.c b/util/grub-mknetdir.c
index 82073d5cc..602574d52 100644
--- a/util/grub-mknetdir.c
+++ b/util/grub-mknetdir.c
@@ -107,7 +107,9 @@  static const 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_RISCV32_EFI] = { "riscv32-efi", "efinet", ".efi" },
+    [GRUB_INSTALL_PLATFORM_RISCV64_EFI] = { "riscv64-efi", "efinet", ".efi" },
   };
 
 static void
diff --git a/util/grub-mkrescue.c b/util/grub-mkrescue.c
index 9545945d8..21e5ce4e4 100644
--- a/util/grub-mkrescue.c
+++ b/util/grub-mkrescue.c
@@ -538,6 +538,8 @@  main (int argc, char *argv[])
 	  || source_dirs[GRUB_INSTALL_PLATFORM_IA64_EFI]
 	  || source_dirs[GRUB_INSTALL_PLATFORM_ARM_EFI]
 	  || source_dirs[GRUB_INSTALL_PLATFORM_ARM64_EFI]
+	  || source_dirs[GRUB_INSTALL_PLATFORM_RISCV32_EFI]
+	  || source_dirs[GRUB_INSTALL_PLATFORM_RISCV64_EFI]
 	  || source_dirs[GRUB_INSTALL_PLATFORM_X86_64_EFI])
 	system_area = SYS_AREA_COMMON;
       else if (source_dirs[GRUB_INSTALL_PLATFORM_SPARC64_IEEE1275])
@@ -735,7 +737,9 @@  main (int argc, char *argv[])
       || source_dirs[GRUB_INSTALL_PLATFORM_X86_64_EFI]
       || source_dirs[GRUB_INSTALL_PLATFORM_IA64_EFI]
       || source_dirs[GRUB_INSTALL_PLATFORM_ARM_EFI]
-      || source_dirs[GRUB_INSTALL_PLATFORM_ARM64_EFI])
+      || source_dirs[GRUB_INSTALL_PLATFORM_ARM64_EFI]
+      || source_dirs[GRUB_INSTALL_PLATFORM_RISCV32_EFI]
+      || source_dirs[GRUB_INSTALL_PLATFORM_RISCV64_EFI])
     {
       char *efidir = grub_util_make_temporary_dir ();
       char *efidir_efi = grub_util_path_concat (2, efidir, "efi");
@@ -770,6 +774,16 @@  main (int argc, char *argv[])
 			     imgname);
       free (imgname);
 
+      imgname = grub_util_path_concat (2, efidir_efi_boot, "bootriscv32.efi");
+      make_image_fwdisk_abs (GRUB_INSTALL_PLATFORM_RISCV32_EFI, "riscv32-efi",
+			     imgname);
+      free (imgname);
+
+      imgname = grub_util_path_concat (2, efidir_efi_boot, "bootriscv64.efi");
+      make_image_fwdisk_abs (GRUB_INSTALL_PLATFORM_RISCV64_EFI, "riscv64-efi",
+			     imgname);
+      free (imgname);
+
       if (source_dirs[GRUB_INSTALL_PLATFORM_I386_EFI])
 	{
 	  imgname = grub_util_path_concat (2, efidir_efi_boot, "boot.efi");
diff --git a/util/mkimage.c b/util/mkimage.c
index cf1a47c94..37d6249f1 100644
--- a/util/mkimage.c
+++ b/util/mkimage.c
@@ -622,6 +622,38 @@  static const struct grub_install_image_target_desc image_targets[] =
       .pe_target = GRUB_PE32_MACHINE_ARM64,
       .elf_target = EM_AARCH64,
     },
+    {
+      .dirname = "riscv32-efi",
+      .names = { "riscv32-efi", NULL },
+      .voidp_sizeof = 4,
+      .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 = EFI32_HEADER_SIZE,
+      .pe_target = GRUB_PE32_MACHINE_RISCV32,
+      .elf_target = EM_RISCV,
+    },
+    {
+      .dirname = "riscv64-efi",
+      .names = { "riscv64-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_RISCV64,
+      .elf_target = EM_RISCV,
+    },
   };
 
 #include <grub/lib/LzmaEnc.h>