mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH 1/3] bootm: move ARM64 Linux image parsing to common directory
@ 2021-05-04 10:45 Ahmad Fatoum
  2021-05-04 10:45 ` [PATCH 2/3] RISC-V: asm: barebox-riscv-head: use load-offset of 0 Ahmad Fatoum
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Ahmad Fatoum @ 2021-05-04 10:45 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

Linux on RISC-V adopts the same structure as on ARM64 for both 32-
and 64-bit kernel images and it's likely future architectures will
as well. In preparation for adding RISC-V Linux boot support,
move the bulk of the code to a common location for reusability.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 arch/arm/lib64/armlinux.c | 74 +++------------------------------------
 common/Makefile           |  2 +-
 common/booti.c            | 66 ++++++++++++++++++++++++++++++++++
 include/bootm.h           |  2 ++
 4 files changed, 74 insertions(+), 70 deletions(-)
 create mode 100644 common/booti.c

diff --git a/arch/arm/lib64/armlinux.c b/arch/arm/lib64/armlinux.c
index a5f122edcd2a..0ba4d30b8e15 100644
--- a/arch/arm/lib64/armlinux.c
+++ b/arch/arm/lib64/armlinux.c
@@ -1,90 +1,26 @@
 // SPDX-License-Identifier: GPL-2.0-only
 // SPDX-FileCopyrightText: 2018 Sascha Hauer <s.hauer@pengutronix.de>
 
-#include <boot.h>
 #include <common.h>
-#include <environment.h>
-#include <image.h>
-#include <fs.h>
-#include <xfuncs.h>
-#include <malloc.h>
-#include <fcntl.h>
-#include <errno.h>
 #include <memory.h>
-#include <of.h>
 #include <init.h>
 #include <bootm.h>
-#include <linux/list.h>
-#include <asm/byteorder.h>
-#include <asm/setup.h>
-#include <asm/barebox-arm.h>
-#include <asm/armlinux.h>
-#include <asm/system.h>
 
 static int do_bootm_linux(struct image_data *data)
 {
-	const void *kernel_header =
-			data->os_fit ? data->fit_kernel : data->os_header;
 	void (*fn)(unsigned long dtb, unsigned long x1, unsigned long x2,
 		       unsigned long x3);
-	resource_size_t start, end;
-	unsigned long text_offset, image_size, devicetree, kernel;
-	unsigned long image_end;
-	int ret;
-	void *fdt;
-
-	text_offset = le64_to_cpup(kernel_header + 8);
-	image_size = le64_to_cpup(kernel_header+ 16);
-
-	ret = memory_bank_first_find_space(&start, &end);
-	if (ret)
-		goto out;
-
-	kernel = ALIGN(start, SZ_2M) + text_offset;
-
-	ret = bootm_load_os(data, kernel);
-	if (ret)
-		goto out;
-
-	image_end = PAGE_ALIGN(kernel + image_size);
-
-	if (bootm_has_initrd(data)) {
-		ret = bootm_load_initrd(data, image_end);
-		if (ret)
-			return ret;
-
-		image_end += resource_size(data->initrd_res);
-		image_end = PAGE_ALIGN(image_end);
-	}
+	phys_addr_t devicetree;
 
-	devicetree = image_end;
-
-	fdt = bootm_get_devicetree(data);
-	if (IS_ERR(fdt)) {
-		ret = PTR_ERR(fdt);
-		goto out;
-	}
-
-	ret = bootm_load_devicetree(data, fdt, devicetree);
-
-	free(fdt);
-
-	if (ret)
-		goto out;
-
-	printf("Loaded kernel to 0x%08lx, devicetree at 0x%08lx\n",
-	       kernel, devicetree);
+	fn = booti_load_image(data, &devicetree);
+	if (IS_ERR(fn))
+		return PTR_ERR(fn);
 
 	shutdown_barebox();
 
-	fn = (void *)kernel;
-
 	fn(devicetree, 0, 0, 0);
 
-	ret = -EINVAL;
-
-out:
-	return ret;
+	return -EINVAL;
 }
 
 static struct image_handler aarch64_linux_handler = {
diff --git a/common/Makefile b/common/Makefile
index c2c15817badf..382a4f661f67 100644
--- a/common/Makefile
+++ b/common/Makefile
@@ -21,7 +21,7 @@ obj-$(CONFIG_SYSTEM_PARTITIONS) += system-partitions.o
 obj-$(CONFIG_BINFMT)		+= binfmt.o
 obj-$(CONFIG_BLOCK)		+= block.o
 obj-$(CONFIG_BLSPEC)		+= blspec.o
-obj-$(CONFIG_BOOTM)		+= bootm.o
+obj-$(CONFIG_BOOTM)		+= bootm.o booti.o
 obj-$(CONFIG_CMD_LOADS)		+= s_record.o
 obj-$(CONFIG_CMD_MEMTEST)	+= memtest.o
 obj-$(CONFIG_COMMAND_SUPPORT)	+= command.o
diff --git a/common/booti.c b/common/booti.c
new file mode 100644
index 000000000000..a2d63d8c3188
--- /dev/null
+++ b/common/booti.c
@@ -0,0 +1,66 @@
+// SPDX-License-Identifier: GPL-2.0-only
+// SPDX-FileCopyrightText: 2018 Sascha Hauer <s.hauer@pengutronix.de>
+
+#include <common.h>
+#include <memory.h>
+#include <bootm.h>
+#include <linux/sizes.h>
+
+void *booti_load_image(struct image_data *data, phys_addr_t *oftree)
+{
+	const void *kernel_header =
+			data->os_fit ? data->fit_kernel : data->os_header;
+	resource_size_t start, end;
+	unsigned long text_offset, image_size, devicetree, kernel;
+	unsigned long image_end;
+	int ret;
+	void *fdt;
+
+	text_offset = le64_to_cpup(kernel_header + 8);
+	image_size = le64_to_cpup(kernel_header + 16);
+
+	ret = memory_bank_first_find_space(&start, &end);
+	if (ret)
+		return ERR_PTR(ret);
+
+	kernel = ALIGN(start, SZ_2M) + text_offset;
+
+	ret = bootm_load_os(data, kernel);
+	if (ret)
+		return ERR_PTR(ret);
+
+	image_end = PAGE_ALIGN(kernel + image_size);
+
+	if (oftree) {
+		if (bootm_has_initrd(data)) {
+			ret = bootm_load_initrd(data, image_end);
+			if (ret)
+				return ERR_PTR(ret);
+
+			image_end += resource_size(data->initrd_res);
+			image_end = PAGE_ALIGN(image_end);
+		}
+
+		devicetree = image_end;
+
+		fdt = bootm_get_devicetree(data);
+		if (IS_ERR(fdt))
+			return fdt;
+
+		ret = bootm_load_devicetree(data, fdt, devicetree);
+
+		free(fdt);
+
+		if (ret)
+			return ERR_PTR(ret);
+
+		*oftree = devicetree;
+	}
+
+	printf("Loaded kernel to 0x%08lx", kernel);
+	if (oftree)
+		printf(", devicetree at 0x%08lx", devicetree);
+	printf("\n");
+
+	return (void *)kernel;
+}
diff --git a/include/bootm.h b/include/bootm.h
index 51e9b3d71ab5..655c5152d97e 100644
--- a/include/bootm.h
+++ b/include/bootm.h
@@ -148,4 +148,6 @@ enum bootm_verify bootm_get_verify_mode(void);
 
 #define UIMAGE_SOME_ADDRESS (UIMAGE_INVALID_ADDRESS - 1)
 
+void *booti_load_image(struct image_data *data, phys_addr_t *oftree);
+
 #endif /* __BOOTM_H */
-- 
2.29.2


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 2/3] RISC-V: asm: barebox-riscv-head: use load-offset of 0
  2021-05-04 10:45 [PATCH 1/3] bootm: move ARM64 Linux image parsing to common directory Ahmad Fatoum
@ 2021-05-04 10:45 ` Ahmad Fatoum
  2021-05-04 10:45 ` [PATCH 3/3] RISC-V: add Linux kernel boot support Ahmad Fatoum
  2021-05-07  7:28 ` [PATCH 1/3] bootm: move ARM64 Linux image parsing to common directory Sascha Hauer
  2 siblings, 0 replies; 4+ messages in thread
From: Ahmad Fatoum @ 2021-05-04 10:45 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

Incoming RISC-V bootm implementation will use the same bootm handler for
booting both kernel and barebox. For this to work, the load offset in
the header needs to make sense. As non-generic DT barebox images have
enough knowledge about the platform to know where to place the stack,
they don't require a load offset, thus set it to zero.

Signed-off-by: Ahmad Fatoum <ahmad@a3f.at>
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 arch/riscv/include/asm/barebox-riscv-head.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/riscv/include/asm/barebox-riscv-head.h b/arch/riscv/include/asm/barebox-riscv-head.h
index f681ec8bcee6..a4c33472cd53 100644
--- a/arch/riscv/include/asm/barebox-riscv-head.h
+++ b/arch/riscv/include/asm/barebox-riscv-head.h
@@ -30,7 +30,7 @@
 
 #ifndef __barebox_riscv_head
 #define __barebox_riscv_head() \
-	__barebox_riscv_header("nop", 0x55555555FFFFFFFF, 0x0, "barebox", "RSCV")
+	__barebox_riscv_header("nop", 0x0, 0x0, "barebox", "RSCV")
 #endif
 
 #endif /* __ASM_RISCV_HEAD_H */
-- 
2.29.2


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 3/3] RISC-V: add Linux kernel boot support
  2021-05-04 10:45 [PATCH 1/3] bootm: move ARM64 Linux image parsing to common directory Ahmad Fatoum
  2021-05-04 10:45 ` [PATCH 2/3] RISC-V: asm: barebox-riscv-head: use load-offset of 0 Ahmad Fatoum
@ 2021-05-04 10:45 ` Ahmad Fatoum
  2021-05-07  7:28 ` [PATCH 1/3] bootm: move ARM64 Linux image parsing to common directory Sascha Hauer
  2 siblings, 0 replies; 4+ messages in thread
From: Ahmad Fatoum @ 2021-05-04 10:45 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

The Linux kernel RISC-V header has the same structure as on ARM64. The
barebox header for the architecture also follows the same structure.

Add the architecture-specific glue for barebox to be able to boot both
RISC-V Linux and barebox.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 arch/riscv/lib/Makefile |  1 +
 arch/riscv/lib/bootm.c  | 51 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 52 insertions(+)
 create mode 100644 arch/riscv/lib/bootm.c

diff --git a/arch/riscv/lib/Makefile b/arch/riscv/lib/Makefile
index 49750d576ae1..a399de7399cb 100644
--- a/arch/riscv/lib/Makefile
+++ b/arch/riscv/lib/Makefile
@@ -8,3 +8,4 @@ obj-$(CONFIG_HAS_ARCH_SJLJ) += setjmp.o longjmp.o
 obj-$(CONFIG_RISCV_OPTIMZED_STRING_FUNCTIONS) += memcpy.o memset.o memmove.o
 obj-$(CONFIG_RISCV_SBI) += sbi.o
 obj-$(CONFIG_CMD_RISCV_CPUINFO) += cpuinfo.o
+obj-$(CONFIG_BOOTM) += bootm.o
diff --git a/arch/riscv/lib/bootm.c b/arch/riscv/lib/bootm.c
new file mode 100644
index 000000000000..b3e41de4a890
--- /dev/null
+++ b/arch/riscv/lib/bootm.c
@@ -0,0 +1,51 @@
+// SPDX-License-Identifier: GPL-2.0-only
+// SPDX-FileCopyrightText: 2018 Sascha Hauer <s.hauer@pengutronix.de>
+
+#include <common.h>
+#include <bootm.h>
+
+static int do_bootm_linux(struct image_data *data)
+{
+	void (*fn)(unsigned long a0, unsigned long dtb, unsigned long a2);
+	phys_addr_t devicetree;
+
+	fn = booti_load_image(data, &devicetree);
+	if (IS_ERR(fn))
+		return PTR_ERR(fn);
+
+	shutdown_barebox();
+
+	fn(0, devicetree, 0);
+
+	return -EINVAL;
+}
+
+static struct image_handler riscv_linux_handler = {
+        .name = "RISC-V Linux image",
+        .bootm = do_bootm_linux,
+        .filetype = filetype_riscv_linux_image,
+};
+
+static struct image_handler riscv_fit_handler = {
+	.name = "FIT image",
+	.bootm = do_bootm_linux,
+	.filetype = filetype_oftree,
+};
+
+static struct image_handler riscv_barebox_handler = {
+        .name = "RISC-V barebox image",
+        .bootm = do_bootm_linux,
+        .filetype = filetype_riscv_barebox_image,
+};
+
+static int riscv_register_image_handler(void)
+{
+	register_image_handler(&riscv_linux_handler);
+	register_image_handler(&riscv_barebox_handler);
+
+	if (IS_ENABLED(CONFIG_FITIMAGE))
+		register_image_handler(&riscv_fit_handler);
+
+	return 0;
+}
+late_initcall(riscv_register_image_handler);
-- 
2.29.2


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH 1/3] bootm: move ARM64 Linux image parsing to common directory
  2021-05-04 10:45 [PATCH 1/3] bootm: move ARM64 Linux image parsing to common directory Ahmad Fatoum
  2021-05-04 10:45 ` [PATCH 2/3] RISC-V: asm: barebox-riscv-head: use load-offset of 0 Ahmad Fatoum
  2021-05-04 10:45 ` [PATCH 3/3] RISC-V: add Linux kernel boot support Ahmad Fatoum
@ 2021-05-07  7:28 ` Sascha Hauer
  2 siblings, 0 replies; 4+ messages in thread
From: Sascha Hauer @ 2021-05-07  7:28 UTC (permalink / raw)
  To: Ahmad Fatoum; +Cc: barebox

On Tue, May 04, 2021 at 12:45:11PM +0200, Ahmad Fatoum wrote:
> Linux on RISC-V adopts the same structure as on ARM64 for both 32-
> and 64-bit kernel images and it's likely future architectures will
> as well. In preparation for adding RISC-V Linux boot support,
> move the bulk of the code to a common location for reusability.
> 
> Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
> ---
>  arch/arm/lib64/armlinux.c | 74 +++------------------------------------
>  common/Makefile           |  2 +-
>  common/booti.c            | 66 ++++++++++++++++++++++++++++++++++
>  include/bootm.h           |  2 ++
>  4 files changed, 74 insertions(+), 70 deletions(-)
>  create mode 100644 common/booti.c

Applied, thanks

Sascha

-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2021-05-07  7:29 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-05-04 10:45 [PATCH 1/3] bootm: move ARM64 Linux image parsing to common directory Ahmad Fatoum
2021-05-04 10:45 ` [PATCH 2/3] RISC-V: asm: barebox-riscv-head: use load-offset of 0 Ahmad Fatoum
2021-05-04 10:45 ` [PATCH 3/3] RISC-V: add Linux kernel boot support Ahmad Fatoum
2021-05-07  7:28 ` [PATCH 1/3] bootm: move ARM64 Linux image parsing to common directory Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox