diff mbox series

[v2,09/10] RISC-V: Add to build system

Message ID 20181114131138.43713-10-agraf@suse.de
State Superseded
Headers show
Series Add RISC-V support | expand

Commit Message

Alexander Graf Nov. 14, 2018, 1:11 p.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>

---
 configure.ac                 | 28 ++++++++++++++++++++++++++--
 gentpl.py                    |  9 ++++++---
 grub-core/Makefile.am        | 12 ++++++++++++
 grub-core/Makefile.core.def  | 29 +++++++++++++++++++++++++++++
 grub-core/commands/file.c    | 14 +++++++++++++-
 grub-core/kern/compiler-rt.c |  3 ++-
 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   |  6 ++++--
 include/grub/efi/api.h       |  3 ++-
 include/grub/efi/efi.h       |  2 +-
 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 ++++++++++++++++++++++++++++++++
 21 files changed, 191 insertions(+), 15 deletions(-)

-- 
2.12.3


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

Comments

Bin Meng Nov. 14, 2018, 3:12 p.m. UTC | #1
Hi Alex,

On Wed, Nov 14, 2018 at 9:11 PM Alexander Graf <agraf@suse.de> 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>

> ---

>  configure.ac                 | 28 ++++++++++++++++++++++++++--

>  gentpl.py                    |  9 ++++++---

>  grub-core/Makefile.am        | 12 ++++++++++++

>  grub-core/Makefile.core.def  | 29 +++++++++++++++++++++++++++++

>  grub-core/commands/file.c    | 14 +++++++++++++-

>  grub-core/kern/compiler-rt.c |  3 ++-

>  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   |  6 ++++--

>  include/grub/efi/api.h       |  3 ++-

>  include/grub/efi/efi.h       |  2 +-

>  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 ++++++++++++++++++++++++++++++++

>  21 files changed, 191 insertions(+), 15 deletions(-)

>


This patch currently does not handle the following configuration:

- use riscv32-* toolchain to build riscv64 target (-m64 required)
- use riscv64-* toolchain to build riscv32 target (-m32 required)

Can you please add the missing bits in configure.ac?

Regards,
Bin

_______________________________________________
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel
Andreas Schwab Nov. 14, 2018, 3:28 p.m. UTC | #2
On Nov 14 2018, Bin Meng <bmeng.cn@gmail.com> wrote:

> - use riscv32-* toolchain to build riscv64 target (-m64 required)

> - use riscv64-* toolchain to build riscv32 target (-m32 required)


riscv does not use -m64/-m32.

Andreas.

-- 
Andreas Schwab, SUSE Labs, schwab@suse.de
GPG Key fingerprint = 0196 BAD8 1CE9 1970 F4BE  1748 E4D4 88E3 0EEA B9D7
"And now for something completely different."

_______________________________________________
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel
Bin Meng Nov. 14, 2018, 3:34 p.m. UTC | #3
On Wed, Nov 14, 2018 at 11:28 PM Andreas Schwab <schwab@suse.de> wrote:
>

> On Nov 14 2018, Bin Meng <bmeng.cn@gmail.com> wrote:

>

> > - use riscv32-* toolchain to build riscv64 target (-m64 required)

> > - use riscv64-* toolchain to build riscv32 target (-m32 required)

>

> riscv does not use -m64/-m32.

>


Ah, yes! I was brain-dead. I actually wanted to say

- use riscv32-* toolchain to build riscv64 target (riscv32 cpu detected)
- use riscv64-* toolchain to build riscv32 target (riscv64 cpu detected)

and before I hit 'send', I changed it to -m32/-m64. Sorry for the wrong message.

Regards,
Bin

_______________________________________________
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel
Alexander Graf Nov. 14, 2018, 5:02 p.m. UTC | #4
On 11/14/2018 04:34 PM, Bin Meng wrote:
> On Wed, Nov 14, 2018 at 11:28 PM Andreas Schwab <schwab@suse.de> wrote:

>> On Nov 14 2018, Bin Meng <bmeng.cn@gmail.com> wrote:

>>

>>> - use riscv32-* toolchain to build riscv64 target (-m64 required)

>>> - use riscv64-* toolchain to build riscv32 target (-m32 required)

>> riscv does not use -m64/-m32.

>>

> Ah, yes! I was brain-dead. I actually wanted to say

>

> - use riscv32-* toolchain to build riscv64 target (riscv32 cpu detected)

> - use riscv64-* toolchain to build riscv32 target (riscv64 cpu detected)

>

> and before I hit 'send', I changed it to -m32/-m64. Sorry for the wrong message.


So what would you expect instead?


Alex


_______________________________________________
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel
Bin Meng Nov. 15, 2018, 1:48 a.m. UTC | #5
Hi Alex,

On Thu, Nov 15, 2018 at 1:02 AM Alexander Graf <agraf@suse.de> wrote:
>

> On 11/14/2018 04:34 PM, Bin Meng wrote:

> > On Wed, Nov 14, 2018 at 11:28 PM Andreas Schwab <schwab@suse.de> wrote:

> >> On Nov 14 2018, Bin Meng <bmeng.cn@gmail.com> wrote:

> >>

> >>> - use riscv32-* toolchain to build riscv64 target (-m64 required)

> >>> - use riscv64-* toolchain to build riscv32 target (-m32 required)

> >> riscv does not use -m64/-m32.

> >>

> > Ah, yes! I was brain-dead. I actually wanted to say

> >

> > - use riscv32-* toolchain to build riscv64 target (riscv32 cpu detected)

> > - use riscv64-* toolchain to build riscv32 target (riscv64 cpu detected)

> >

> > and before I hit 'send', I changed it to -m32/-m64. Sorry for the wrong message.

>

> So what would you expect instead?

>


I was expecting using the riscv32-* toolchain we can still build grub
for riscv64 (or using riscv64-* toolchain to build riscv32), i.e. the
similar way to what current U-Boot build for targets like x86, riscv.

Regards,
Bin

_______________________________________________
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel
Alexander Graf Nov. 15, 2018, 10:01 a.m. UTC | #6
On 15.11.18 02:48, Bin Meng wrote:
> Hi Alex,

> 

> On Thu, Nov 15, 2018 at 1:02 AM Alexander Graf <agraf@suse.de> wrote:

>>

>> On 11/14/2018 04:34 PM, Bin Meng wrote:

>>> On Wed, Nov 14, 2018 at 11:28 PM Andreas Schwab <schwab@suse.de> wrote:

>>>> On Nov 14 2018, Bin Meng <bmeng.cn@gmail.com> wrote:

>>>>

>>>>> - use riscv32-* toolchain to build riscv64 target (-m64 required)

>>>>> - use riscv64-* toolchain to build riscv32 target (-m32 required)

>>>> riscv does not use -m64/-m32.

>>>>

>>> Ah, yes! I was brain-dead. I actually wanted to say

>>>

>>> - use riscv32-* toolchain to build riscv64 target (riscv32 cpu detected)

>>> - use riscv64-* toolchain to build riscv32 target (riscv64 cpu detected)

>>>

>>> and before I hit 'send', I changed it to -m32/-m64. Sorry for the wrong message.

>>

>> So what would you expect instead?

>>

> 

> I was expecting using the riscv32-* toolchain we can still build grub

> for riscv64 (or using riscv64-* toolchain to build riscv32), i.e. the

> similar way to what current U-Boot build for targets like x86, riscv.


Can you please post a patch on top of my series for that? I'm not sure
how that would work :).


Alex

_______________________________________________
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel
Bin Meng Nov. 16, 2018, 3:03 a.m. UTC | #7
Hi Alex,

On Thu, Nov 15, 2018 at 6:02 PM Alexander Graf <agraf@suse.de> wrote:
>

>

>

> On 15.11.18 02:48, Bin Meng wrote:

> > Hi Alex,

> >

> > On Thu, Nov 15, 2018 at 1:02 AM Alexander Graf <agraf@suse.de> wrote:

> >>

> >> On 11/14/2018 04:34 PM, Bin Meng wrote:

> >>> On Wed, Nov 14, 2018 at 11:28 PM Andreas Schwab <schwab@suse.de> wrote:

> >>>> On Nov 14 2018, Bin Meng <bmeng.cn@gmail.com> wrote:

> >>>>

> >>>>> - use riscv32-* toolchain to build riscv64 target (-m64 required)

> >>>>> - use riscv64-* toolchain to build riscv32 target (-m32 required)

> >>>> riscv does not use -m64/-m32.

> >>>>

> >>> Ah, yes! I was brain-dead. I actually wanted to say

> >>>

> >>> - use riscv32-* toolchain to build riscv64 target (riscv32 cpu detected)

> >>> - use riscv64-* toolchain to build riscv32 target (riscv64 cpu detected)

> >>>

> >>> and before I hit 'send', I changed it to -m32/-m64. Sorry for the wrong message.

> >>

> >> So what would you expect instead?

> >>

> >

> > I was expecting using the riscv32-* toolchain we can still build grub

> > for riscv64 (or using riscv64-* toolchain to build riscv32), i.e. the

> > similar way to what current U-Boot build for targets like x86, riscv.

>

> Can you please post a patch on top of my series for that? I'm not sure

> how that would work :).


I have looked at the autoconf, and it seems that such configuration
cannot be done without adding a new configure option for grub to
override the $target_cpu which was automatically guessed from the
toolchain triples by autoconf.

Guess we have to leave such "limitation" :)

Regards,
Bin

_______________________________________________
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 5e63c4af3..2cc2ffb2c 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
@@ -174,6 +182,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"]) ;;
@@ -826,6 +836,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([[]], [[]])],
@@ -1141,7 +1161,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"
@@ -1151,7 +1171,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
@@ -1913,6 +1933,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 da67965a4..6a34a30b3 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,7 +79,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", "riscv32_efi", "riscv64_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 f4ff62b76..30e849c5e 100644
--- a/grub-core/Makefile.am
+++ b/grub-core/Makefile.am
@@ -266,6 +266,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 6e2cc8444..11b1d56a9 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)';
@@ -111,6 +117,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;
@@ -234,6 +242,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;
@@ -305,6 +319,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;
@@ -780,6 +802,8 @@  module = {
   enable = arm64_efi;
   enable = arm_uboot;
   enable = arm_coreboot;
+  enable = riscv32_efi;
+  enable = riscv64_efi;
 };
 
 module = {
@@ -1611,6 +1635,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 = {
@@ -1707,6 +1732,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;
@@ -1798,6 +1825,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 5cfcb3907..0c0c3629f 100644
--- a/grub-core/kern/compiler-rt.c
+++ b/grub-core/kern/compiler-rt.c
@@ -343,7 +343,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 dc73649a5..d4f2c125f 100644
--- a/include/grub/compiler-rt.h
+++ b/include/grub/compiler-rt.h
@@ -53,7 +53,8 @@  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
@@ -165,7 +166,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/util/install.h b/include/grub/util/install.h
index 0dba8b67f..15186e8ea 100644
--- a/include/grub/util/install.h
+++ b/include/grub/util/install.h
@@ -102,6 +102,8 @@  enum grub_install_plat
     GRUB_INSTALL_PLATFORM_X86_64_XEN,
     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 0a2e24a79..d0cc93725 100644
--- a/util/grub-install-common.c
+++ b/util/grub-install-common.c
@@ -728,6 +728,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 78d0138cb..6649b2c14 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:
@@ -899,6 +901,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:
@@ -943,6 +947,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:
@@ -995,6 +1001,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;
@@ -1108,6 +1116,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;
@@ -1135,6 +1149,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;
@@ -1437,6 +1457,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;
@@ -1529,6 +1551,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),
@@ -1631,6 +1655,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:
@@ -1865,6 +1891,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 e22d82afa..b5368ffd2 100644
--- a/util/mkimage.c
+++ b/util/mkimage.c
@@ -609,6 +609,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>