[PATCH v2 08/13] bootmeth: share the pxelinux getfile helper

Alexey Charkov <[email protected]>
Newsgroups org.u-boot-project.lists.u-boot
Message-ID <[email protected]>
bootmeth_extlinux.c and bootmeth_pxe.c carry byte-identical copies of the
pxe_getfile_func that reads a file through the bootmeth uclass, and the
Boot Loader Specification bootmeth added later needs the same thing.

Move the function into a small translation unit of its own, so that all
consumers can call reuse it without introducing PXE dependency into
bootstd or bootstd dependency into PXE.

Bootmeths which need it select the new hidden BOOTMETH_PXE_COMMON symbol.

Signed-off-by: Alexey Charkov <[email protected]>
---
 boot/Kconfig               |  8 ++++++++
 boot/Makefile              |  1 +
 boot/bootmeth_extlinux.c   | 20 --------------------
 boot/bootmeth_pxe.c        | 22 +---------------------
 boot/bootmeth_pxe_common.c | 34 ++++++++++++++++++++++++++++++++++
 include/extlinux.h         | 23 +++++++++++++++++++++++
 6 files changed, 67 insertions(+), 41 deletions(-)

diff --git a/boot/Kconfig b/boot/Kconfig
index c67dc0ba4932..add78a0b965a 100644
--- a/boot/Kconfig
+++ b/boot/Kconfig
@@ -622,9 +622,16 @@ config BOOTMETH_CROS
 
 	  Note that only x86 devices are supported at present.
 
+config BOOTMETH_PXE_COMMON
+	bool
+	help
+	  Helpers shared by the bootmeths that hand their config files to the
+	  pxelinux parser. Selected by those bootmeths, not user-visible.
+
 config BOOTMETH_EXTLINUX
 	bool "Bootdev support for extlinux boot"
 	select PXE_UTILS
+	select BOOTMETH_PXE_COMMON
 	default y
 	help
 	  Enables support for extlinux boot using bootdevs. This makes the
@@ -640,6 +647,7 @@ config BOOTMETH_EXTLINUX
 config BOOTMETH_EXTLINUX_PXE
 	bool "Bootdev support for extlinux boot over network"
 	depends on CMD_PXE && CMD_NET && DM_ETH
+	select BOOTMETH_PXE_COMMON
 	default y
 	help
 	  Enables support for extlinux boot using bootdevs. This makes the
diff --git a/boot/Makefile b/boot/Makefile
index 7fb56e7ef379..a0f6cabcdfcc 100644
--- a/boot/Makefile
+++ b/boot/Makefile
@@ -28,6 +28,7 @@ obj-$(CONFIG_$(PHASE_)BOOTSTD) += bootstd-uclass.o
 obj-$(CONFIG_$(PHASE_)BOOTSTD_MENU) += bootflow_menu.o
 obj-$(CONFIG_$(PHASE_)BOOTSTD_PROG) += prog_boot.o
 
+obj-$(CONFIG_$(PHASE_)BOOTMETH_PXE_COMMON) += bootmeth_pxe_common.o
 obj-$(CONFIG_$(PHASE_)BOOTMETH_EXTLINUX) += bootmeth_extlinux.o
 obj-$(CONFIG_$(PHASE_)BOOTMETH_EXTLINUX_PXE) += bootmeth_pxe.o
 obj-$(CONFIG_$(PHASE_)BOOTMETH_EFILOADER) += bootmeth_efi.o
diff --git a/boot/bootmeth_extlinux.c b/boot/bootmeth_extlinux.c
index 921d721a27b2..bce6b4995717 100644
--- a/boot/bootmeth_extlinux.c
+++ b/boot/bootmeth_extlinux.c
@@ -68,26 +68,6 @@ static int extlinux_get_state_desc(struct udevice *dev, char *buf, int maxsize)
 	return 0;
 }
 
-static int extlinux_getfile(struct pxe_context *ctx, const char *file_path,
-			    char *file_addr, enum bootflow_img_t type,
-			    ulong *sizep)
-{
-	struct extlinux_info *info = ctx->userdata;
-	ulong addr;
-	int ret;
-
-	addr = simple_strtoul(file_addr, NULL, 16);
-
-	/* Allow up to 1GB */
-	*sizep = 1 << 30;
-	ret = bootmeth_read_file(info->dev, info->bflow, file_path, addr,
-				 type, sizep);
-	if (ret)
-		return log_msg_ret("read", ret);
-
-	return 0;
-}
-
 static int extlinux_check(struct udevice *dev, struct bootflow_iter *iter)
 {
 	int ret;
diff --git a/boot/bootmeth_pxe.c b/boot/bootmeth_pxe.c
index faa8d729b151..7e999730e6ac 100644
--- a/boot/bootmeth_pxe.c
+++ b/boot/bootmeth_pxe.c
@@ -23,26 +23,6 @@
 #include <net.h>
 #include <pxe_utils.h>
 
-static int extlinux_pxe_getfile(struct pxe_context *ctx, const char *file_path,
-				char *file_addr, enum bootflow_img_t type,
-				ulong *sizep)
-{
-	struct extlinux_info *info = ctx->userdata;
-	ulong addr;
-	int ret;
-
-	addr = simple_strtoul(file_addr, NULL, 16);
-
-	/* Allow up to 1GB */
-	*sizep = 1 << 30;
-	ret = bootmeth_read_file(info->dev, info->bflow, file_path, addr,
-				 type, sizep);
-	if (ret)
-		return log_msg_ret("read", ret);
-
-	return 0;
-}
-
 static int extlinux_pxe_check(struct udevice *dev, struct bootflow_iter *iter)
 {
 	int ret;
@@ -158,7 +138,7 @@ static int extlinux_pxe_boot(struct udevice *dev, struct bootflow *bflow)
 	info.dev = dev;
 	info.bflow = bflow;
 	info.cmdtp = &cmdtp;
-	ret = pxe_setup_ctx(ctx, &cmdtp, extlinux_pxe_getfile, &info, false,
+	ret = pxe_setup_ctx(ctx, &cmdtp, extlinux_getfile, &info, false,
 			    bflow->subdir, false, false);
 	if (ret)
 		return log_msg_ret("ctx", -EINVAL);
diff --git a/boot/bootmeth_pxe_common.c b/boot/bootmeth_pxe_common.c
new file mode 100644
index 000000000000..d6800f0dc349
--- /dev/null
+++ b/boot/bootmeth_pxe_common.c
@@ -0,0 +1,34 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Helpers shared by the bootmeths that boot via the pxelinux parser
+ *
+ * Copyright 2021 Google LLC
+ * Written by Simon Glass <[email protected]>
+ */
+
+#define LOG_CATEGORY UCLASS_BOOTSTD
+
+#include <bootflow.h>
+#include <bootmeth.h>
+#include <extlinux.h>
+#include <pxe_utils.h>
+#include <vsprintf.h>
+
+int extlinux_getfile(struct pxe_context *ctx, const char *file_path,
+		     char *file_addr, enum bootflow_img_t type, ulong *sizep)
+{
+	struct extlinux_info *info = ctx->userdata;
+	ulong addr;
+	int ret;
+
+	addr = simple_strtoul(file_addr, NULL, 16);
+
+	/* Allow up to 1GB */
+	*sizep = 1 << 30;
+	ret = bootmeth_read_file(info->dev, info->bflow, file_path, addr,
+				 type, sizep);
+	if (ret)
+		return log_msg_ret("read", ret);
+
+	return 0;
+}
diff --git a/include/extlinux.h b/include/extlinux.h
index 721ba46371cc..54e054501445 100644
--- a/include/extlinux.h
+++ b/include/extlinux.h
@@ -7,6 +7,11 @@
 #ifndef __extlinux_h
 #define __extlinux_h
 
+#include <bootflow.h>
+#include <linux/types.h>
+
+struct pxe_context;
+
 #define EXTLINUX_FNAME	"extlinux/extlinux.conf"
 
 /**
@@ -14,6 +19,7 @@
  *
  * @dev: bootmethod device being used to boot
  * @bflow: bootflow being booted
+ * @cmdtp: command table entry to run the boot command with, or NULL
  */
 struct extlinux_info {
 	struct udevice *dev;
@@ -21,4 +27,21 @@ struct extlinux_info {
 	struct cmd_tbl *cmdtp;
 };
 
+/**
+ * extlinux_getfile() - Read a file named by a pxelinux-style config
+ *
+ * This is the pxe_getfile_func used by every bootmeth that hands its files
+ * to the pxelinux parser. It reads the file through the bootmeth uclass, so
+ * @ctx->userdata must point at a struct extlinux_info.
+ *
+ * @ctx: PXE context
+ * @file_path: Path of the file to read
+ * @file_addr: Address to load the file to, as a hex string
+ * @type: Image type used to record the loaded file in the bootflow
+ * @sizep: Returns the file size in bytes
+ * Return: 0 if OK, -ve on error
+ */
+int extlinux_getfile(struct pxe_context *ctx, const char *file_path,
+		     char *file_addr, enum bootflow_img_t type, ulong *sizep);
+
 #endif

-- 
2.54.0
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.