2019-05-06 18:13:44

by Atish Patra

[permalink] [raw]
Subject: [U-Boot] [v4 PATCH] RISCV: image: Add booti support

This patch adds booti support for RISC-V Linux kernel. The existing
bootm method will also continue to work as it is.

It depends on the following kernel patch which adds the header to the
flat Image. Gzip compressed Image (Image.gz) support is not enabled with
this patch.

https://patchwork.kernel.org/patch/10925543/

Tested on HiFive Unleashed and QEMU.

Signed-off-by: Atish Patra <[email protected]>
Reviewed-by: Tom Rini <[email protected]>
Tested-by: Karsten Merker <[email protected]>
---
Changes from v3->v4
1. Rebased on top of master to avoid git am errors.

Changes from v2->v3
1. Updated the image header structure as per kernel patch.
2. Removed Image.gz support as it will be added as separate RFC patch.
---
arch/riscv/lib/Makefile | 1 +
arch/riscv/lib/image.c | 55 +++++++++++++++++++++++++++++++++++++++++
cmd/Kconfig | 2 +-
cmd/booti.c | 8 ++++--
4 files changed, 63 insertions(+), 3 deletions(-)
create mode 100644 arch/riscv/lib/image.c

diff --git a/arch/riscv/lib/Makefile b/arch/riscv/lib/Makefile
index 1c332db436a9..6ae6ebbeafda 100644
--- a/arch/riscv/lib/Makefile
+++ b/arch/riscv/lib/Makefile
@@ -7,6 +7,7 @@
# Rick Chen, Andes Technology Corporation <[email protected]>

obj-$(CONFIG_CMD_BOOTM) += bootm.o
+obj-$(CONFIG_CMD_BOOTI) += bootm.o image.o
obj-$(CONFIG_CMD_GO) += boot.o
obj-y += cache.o
obj-$(CONFIG_RISCV_RDTIME) += rdtime.o
diff --git a/arch/riscv/lib/image.c b/arch/riscv/lib/image.c
new file mode 100644
index 000000000000..d063beb7dfbe
--- /dev/null
+++ b/arch/riscv/lib/image.c
@@ -0,0 +1,55 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (C) 2019 Western Digital Corporation or its affiliates.
+ * Authors:
+ * Atish Patra <[email protected]>
+ * Based on arm/lib/image.c
+ */
+
+#include <common.h>
+#include <mapmem.h>
+#include <errno.h>
+#include <linux/sizes.h>
+#include <linux/stddef.h>
+
+DECLARE_GLOBAL_DATA_PTR;
+
+/* ASCII version of "RISCV" defined in Linux kernel */
+#define LINUX_RISCV_IMAGE_MAGIC 0x5643534952
+
+struct linux_image_h {
+ uint32_t code0; /* Executable code */
+ uint32_t code1; /* Executable code */
+ uint64_t text_offset; /* Image load offset */
+ uint64_t image_size; /* Effective Image size */
+ uint64_t res1; /* reserved */
+ uint64_t res2; /* reserved */
+ uint64_t res3; /* reserved */
+ uint64_t magic; /* Magic number */
+ uint32_t res4; /* reserved */
+ uint32_t res5; /* reserved */
+};
+
+int booti_setup(ulong image, ulong *relocated_addr, ulong *size,
+ bool force_reloc)
+{
+ struct linux_image_h *lhdr;
+
+ lhdr = (struct linux_image_h *)map_sysmem(image, 0);
+
+ if (lhdr->magic != LINUX_RISCV_IMAGE_MAGIC) {
+ puts("Bad Linux RISCV Image magic!\n");
+ return -EINVAL;
+ }
+
+ if (lhdr->image_size == 0) {
+ puts("Image lacks image_size field, error!\n");
+ return -EINVAL;
+ }
+ *size = lhdr->image_size;
+ *relocated_addr = gd->ram_base + lhdr->text_offset;
+
+ unmap_sysmem(lhdr);
+
+ return 0;
+}
diff --git a/cmd/Kconfig b/cmd/Kconfig
index 069e0ea7300b..4e11e0f404c8 100644
--- a/cmd/Kconfig
+++ b/cmd/Kconfig
@@ -223,7 +223,7 @@ config CMD_BOOTZ

config CMD_BOOTI
bool "booti"
- depends on ARM64
+ depends on ARM64 || RISCV
default y
help
Boot an AArch64 Linux Kernel image from memory.
diff --git a/cmd/booti.c b/cmd/booti.c
index 04353b68eccc..5e902993865b 100644
--- a/cmd/booti.c
+++ b/cmd/booti.c
@@ -77,7 +77,11 @@ int do_booti(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
bootm_disable_interrupts();

images.os.os = IH_OS_LINUX;
+#ifdef CONFIG_RISCV_SMODE
+ images.os.arch = IH_ARCH_RISCV;
+#elif CONFIG_ARM64
images.os.arch = IH_ARCH_ARM64;
+#endif
ret = do_bootm_states(cmdtp, flag, argc, argv,
#ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
BOOTM_STATE_RAMDISK |
@@ -92,7 +96,7 @@ int do_booti(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
#ifdef CONFIG_SYS_LONGHELP
static char booti_help_text[] =
"[addr [initrd[:size]] [fdt]]\n"
- " - boot arm64 Linux Image stored in memory\n"
+ " - boot arm64/riscv Linux Image stored in memory\n"
"\tThe argument 'initrd' is optional and specifies the address\n"
"\tof an initrd in memory. The optional parameter ':size' allows\n"
"\tspecifying the size of a RAW initrd.\n"
@@ -107,5 +111,5 @@ static char booti_help_text[] =

U_BOOT_CMD(
booti, CONFIG_SYS_MAXARGS, 1, do_booti,
- "boot arm64 Linux Image image from memory", booti_help_text
+ "boot arm64/riscv Linux Image image from memory", booti_help_text
);
--
2.21.0


2019-05-06 20:08:34

by Heinrich Schuchardt

[permalink] [raw]
Subject: Re: [U-Boot] [v4 PATCH] RISCV: image: Add booti support

On 5/6/19 8:11 PM, Atish Patra wrote:
> This patch adds booti support for RISC-V Linux kernel. The existing
> bootm method will also continue to work as it is.
>
> It depends on the following kernel patch which adds the header to the
> flat Image. Gzip compressed Image (Image.gz) support is not enabled with
> this patch.
>
> https://patchwork.kernel.org/patch/10925543/
>
> Tested on HiFive Unleashed and QEMU.
>
> Signed-off-by: Atish Patra <[email protected]>
> Reviewed-by: Tom Rini <[email protected]>
> Tested-by: Karsten Merker <[email protected]>
> ---
> Changes from v3->v4
> 1. Rebased on top of master to avoid git am errors.
>
> Changes from v2->v3
> 1. Updated the image header structure as per kernel patch.
> 2. Removed Image.gz support as it will be added as separate RFC patch.
> ---
> arch/riscv/lib/Makefile | 1 +
> arch/riscv/lib/image.c | 55 +++++++++++++++++++++++++++++++++++++++++
> cmd/Kconfig | 2 +-
> cmd/booti.c | 8 ++++--
> 4 files changed, 63 insertions(+), 3 deletions(-)
> create mode 100644 arch/riscv/lib/image.c
>
> diff --git a/arch/riscv/lib/Makefile b/arch/riscv/lib/Makefile
> index 1c332db436a9..6ae6ebbeafda 100644
> --- a/arch/riscv/lib/Makefile
> +++ b/arch/riscv/lib/Makefile
> @@ -7,6 +7,7 @@
> # Rick Chen, Andes Technology Corporation <[email protected]>
>
> obj-$(CONFIG_CMD_BOOTM) += bootm.o
> +obj-$(CONFIG_CMD_BOOTI) += bootm.o image.o
> obj-$(CONFIG_CMD_GO) += boot.o
> obj-y += cache.o
> obj-$(CONFIG_RISCV_RDTIME) += rdtime.o
> diff --git a/arch/riscv/lib/image.c b/arch/riscv/lib/image.c
> new file mode 100644
> index 000000000000..d063beb7dfbe
> --- /dev/null
> +++ b/arch/riscv/lib/image.c
> @@ -0,0 +1,55 @@
> +// SPDX-License-Identifier: GPL-2.0+
> +/*
> + * Copyright (C) 2019 Western Digital Corporation or its affiliates.
> + * Authors:
> + * Atish Patra <[email protected]>
> + * Based on arm/lib/image.c
> + */
> +
> +#include <common.h>
> +#include <mapmem.h>
> +#include <errno.h>
> +#include <linux/sizes.h>
> +#include <linux/stddef.h>
> +
> +DECLARE_GLOBAL_DATA_PTR;
> +
> +/* ASCII version of "RISCV" defined in Linux kernel */
> +#define LINUX_RISCV_IMAGE_MAGIC 0x5643534952
> +
> +struct linux_image_h {
> + uint32_t code0; /* Executable code */
> + uint32_t code1; /* Executable code */
> + uint64_t text_offset; /* Image load offset */
> + uint64_t image_size; /* Effective Image size */
> + uint64_t res1; /* reserved */
> + uint64_t res2; /* reserved */
> + uint64_t res3; /* reserved */
> + uint64_t magic; /* Magic number */
> + uint32_t res4; /* reserved */
> + uint32_t res5; /* reserved */
> +};
> +
> +int booti_setup(ulong image, ulong *relocated_addr, ulong *size,
> + bool force_reloc)
> +{
> + struct linux_image_h *lhdr;
> +
> + lhdr = (struct linux_image_h *)map_sysmem(image, 0);
> +
> + if (lhdr->magic != LINUX_RISCV_IMAGE_MAGIC) {
> + puts("Bad Linux RISCV Image magic!\n");
> + return -EINVAL;
> + }
> +
> + if (lhdr->image_size == 0) {
> + puts("Image lacks image_size field, error!\n");
> + return -EINVAL;
> + }
> + *size = lhdr->image_size;
> + *relocated_addr = gd->ram_base + lhdr->text_offset;
> +
> + unmap_sysmem(lhdr);
> +
> + return 0;
> +}
> diff --git a/cmd/Kconfig b/cmd/Kconfig
> index 069e0ea7300b..4e11e0f404c8 100644
> --- a/cmd/Kconfig
> +++ b/cmd/Kconfig
> @@ -223,7 +223,7 @@ config CMD_BOOTZ
>
> config CMD_BOOTI
> bool "booti"
> - depends on ARM64
> + depends on ARM64 || RISCV
> default y
> help
> Boot an AArch64 Linux Kernel image from memory.
> diff --git a/cmd/booti.c b/cmd/booti.c
> index 04353b68eccc..5e902993865b 100644
> --- a/cmd/booti.c
> +++ b/cmd/booti.c
> @@ -77,7 +77,11 @@ int do_booti(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
> bootm_disable_interrupts();
>
> images.os.os = IH_OS_LINUX;
> +#ifdef CONFIG_RISCV_SMODE
> + images.os.arch = IH_ARCH_RISCV;
> +#elif CONFIG_ARM64
> images.os.arch = IH_ARCH_ARM64;
> +#endif
> ret = do_bootm_states(cmdtp, flag, argc, argv,
> #ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
> BOOTM_STATE_RAMDISK |
> @@ -92,7 +96,7 @@ int do_booti(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
> #ifdef CONFIG_SYS_LONGHELP
> static char booti_help_text[] =
> "[addr [initrd[:size]] [fdt]]\n"
> - " - boot arm64 Linux Image stored in memory\n"
> + " - boot arm64/riscv Linux Image stored in memory\n"

Why would you repeat the short description? Just remove this line.


> "\tThe argument 'initrd' is optional and specifies the address\n"
> "\tof an initrd in memory. The optional parameter ':size' allows\n"
> "\tspecifying the size of a RAW initrd.\n"
> @@ -107,5 +111,5 @@ static char booti_help_text[] =
>
> U_BOOT_CMD(
> booti, CONFIG_SYS_MAXARGS, 1, do_booti,
> - "boot arm64 Linux Image image from memory", booti_help_text
> + "boot arm64/riscv Linux Image image from memory", booti_help_text

%s/Image image/image/

"arm64/riscv" is distracting. If I am on RISC-V I cannot boot an ARM64
image here. Remove the reference to the architecture, please.

Best regards

Heinrich

> );
>

2019-05-06 20:42:51

by Karsten Merker

[permalink] [raw]
Subject: Re: [U-Boot] [v4 PATCH] RISCV: image: Add booti support

On Mon, May 06, 2019 at 10:06:39PM +0200, Heinrich Schuchardt wrote:
> On 5/6/19 8:11 PM, Atish Patra wrote:
> > This patch adds booti support for RISC-V Linux kernel. The existing
> > bootm method will also continue to work as it is.
[...]
> > + "boot arm64/riscv Linux Image image from memory", booti_help_text
>
> %s/Image image/image/
>
> "arm64/riscv" is distracting. If I am on RISC-V I cannot boot an ARM64
> image here. Remove the reference to the architecture, please.

Hello,

I'm not sure about the last point - ISTR (please correct me if my
memory betrays me here) that an arm64 U-Boot can in principle be
used to boot either an arm64 or an armv7 kernel, but the commands
are different in those cases (booti for an arm64 "Image" format
kernel and bootz for an armv7 "zImage" format kernel), so having
the information which kernel format is supported by the
respective commands appears useful to me. If the arm64 kernel
image format would have a distinctive name (like "zImage" on
armv7 or "bzImage" on x86) that would be less problematic, but
with the confusion potential of "boot a Linux Image" (as in the
arm64/riscv-specific "Image" format) vs "boot a Linux image" (as
in generally some form of kernel image), I think explicitly
mentioning the supported architectures makes sense.

Regards,
Karsten
--
Ich widerspreche hiermit ausdrücklich der Nutzung sowie der
Weitergabe meiner personenbezogenen Daten für Zwecke der Werbung
sowie der Markt- oder Meinungsforschung.

2019-05-06 21:13:54

by Heinrich Schuchardt

[permalink] [raw]
Subject: Re: [U-Boot] [v4 PATCH] RISCV: image: Add booti support

On 5/6/19 10:39 PM, Karsten Merker wrote:
> On Mon, May 06, 2019 at 10:06:39PM +0200, Heinrich Schuchardt wrote:
>> On 5/6/19 8:11 PM, Atish Patra wrote:
>>> This patch adds booti support for RISC-V Linux kernel. The existing
>>> bootm method will also continue to work as it is.
> [...]
>>> + "boot arm64/riscv Linux Image image from memory", booti_help_text
>>
>> %s/Image image/image/
>>
>> "arm64/riscv" is distracting. If I am on RISC-V I cannot boot an ARM64
>> image here. Remove the reference to the architecture, please.
>
> Hello,
>
> I'm not sure about the last point - ISTR (please correct me if my
> memory betrays me here) that an arm64 U-Boot can in principle be
> used to boot either an arm64 or an armv7 kernel, but the commands
> are different in those cases (booti for an arm64 "Image" format
> kernel and bootz for an armv7 "zImage" format kernel), so having
> the information which kernel format is supported by the
> respective commands appears useful to me. If the arm64 kernel
> image format would have a distinctive name (like "zImage" on
> armv7 or "bzImage" on x86) that would be less problematic, but
> with the confusion potential of "boot a Linux Image" (as in the
> arm64/riscv-specific "Image" format) vs "boot a Linux image" (as
> in generally some form of kernel image), I think explicitly
> mentioning the supported architectures makes sense.

In this case you have to ensure that only the *supported* architectures
are mentioned. RISC-V is not supported on ARM64.

Best regards

Heinrich

2019-05-06 21:31:00

by Tom Rini

[permalink] [raw]
Subject: Re: [U-Boot] [v4 PATCH] RISCV: image: Add booti support

On Mon, May 06, 2019 at 11:10:57PM +0200, Heinrich Schuchardt wrote:
> On 5/6/19 10:39 PM, Karsten Merker wrote:
> >On Mon, May 06, 2019 at 10:06:39PM +0200, Heinrich Schuchardt wrote:
> >>On 5/6/19 8:11 PM, Atish Patra wrote:
> >>>This patch adds booti support for RISC-V Linux kernel. The existing
> >>>bootm method will also continue to work as it is.
> >[...]
> >>>+ "boot arm64/riscv Linux Image image from memory", booti_help_text
> >>
> >>%s/Image image/image/
> >>
> >>"arm64/riscv" is distracting. If I am on RISC-V I cannot boot an ARM64
> >>image here. Remove the reference to the architecture, please.
> >
> >Hello,
> >
> >I'm not sure about the last point - ISTR (please correct me if my
> >memory betrays me here) that an arm64 U-Boot can in principle be
> >used to boot either an arm64 or an armv7 kernel, but the commands
> >are different in those cases (booti for an arm64 "Image" format
> >kernel and bootz for an armv7 "zImage" format kernel), so having
> >the information which kernel format is supported by the
> >respective commands appears useful to me. If the arm64 kernel
> >image format would have a distinctive name (like "zImage" on
> >armv7 or "bzImage" on x86) that would be less problematic, but
> >with the confusion potential of "boot a Linux Image" (as in the
> >arm64/riscv-specific "Image" format) vs "boot a Linux image" (as
> >in generally some form of kernel image), I think explicitly
> >mentioning the supported architectures makes sense.
>
> In this case you have to ensure that only the *supported* architectures
> are mentioned. RISC-V is not supported on ARM64.

The help should be re-worded to be both architecture agnostic and clear
that it is for the Linux Kernel 'Image' format images.

--
Tom


Attachments:
(No filename) (1.74 kB)
signature.asc (836.00 B)
Download all attachments

2019-05-07 00:51:29

by Atish Patra

[permalink] [raw]
Subject: Re: [U-Boot] [v4 PATCH] RISCV: image: Add booti support

On 5/6/19 2:27 PM, Tom Rini wrote:
> On Mon, May 06, 2019 at 11:10:57PM +0200, Heinrich Schuchardt wrote:
>> On 5/6/19 10:39 PM, Karsten Merker wrote:
>>> On Mon, May 06, 2019 at 10:06:39PM +0200, Heinrich Schuchardt wrote:
>>>> On 5/6/19 8:11 PM, Atish Patra wrote:
>>>>> This patch adds booti support for RISC-V Linux kernel. The existing
>>>>> bootm method will also continue to work as it is.
>>> [...]
>>>>> + "boot arm64/riscv Linux Image image from memory", booti_help_text
>>>>
>>>> %s/Image image/image/
>>>>
>>>> "arm64/riscv" is distracting. If I am on RISC-V I cannot boot an ARM64
>>>> image here. Remove the reference to the architecture, please.
>>>
>>> Hello,
>>>
>>> I'm not sure about the last point - ISTR (please correct me if my
>>> memory betrays me here) that an arm64 U-Boot can in principle be
>>> used to boot either an arm64 or an armv7 kernel, but the commands
>>> are different in those cases (booti for an arm64 "Image" format
>>> kernel and bootz for an armv7 "zImage" format kernel), so having
>>> the information which kernel format is supported by the
>>> respective commands appears useful to me. If the arm64 kernel
>>> image format would have a distinctive name (like "zImage" on
>>> armv7 or "bzImage" on x86) that would be less problematic, but
>>> with the confusion potential of "boot a Linux Image" (as in the
>>> arm64/riscv-specific "Image" format) vs "boot a Linux image" (as
>>> in generally some form of kernel image), I think explicitly
>>> mentioning the supported architectures makes sense.
>>
>> In this case you have to ensure that only the *supported* architectures
>> are mentioned. RISC-V is not supported on ARM64.
>
> The help should be re-worded to be both architecture agnostic and clear
> that it is for the Linux Kernel 'Image' format images.
>
Done.

Regards,
Atish