All of lore.kernel.org
 help / color / mirror / Atom feed
From: Simon Glass <sjg@chromium.org>
To: U-Boot Mailing List <u-boot@lists.denx.de>
Cc: Dennis Gilmore <dennis@ausil.us>,
	Ilias Apalodimas <ilias.apalodimas@linaro.org>,
	Lukas Auer <lukas.auer@aisec.fraunhofer.de>,
	Tom Rini <trini@konsulko.com>,
	Heinrich Schuchardt <xypron.glpk@gmx.de>,
	Michal Simek <michal.simek@xilinx.com>,
	Daniel Schwierzeck <daniel.schwierzeck@gmail.com>,
	Steffen Jaeckel <jaeckel-floss@eyet-services.de>,
	Simon Glass <sjg@chromium.org>
Subject: [PATCH v4 23/33] bootstd: Add an implementation of distro PXE boot
Date: Sun,  6 Mar 2022 05:50:06 -0700	[thread overview]
Message-ID: <20220306125016.3133737-15-sjg@chromium.org> (raw)
In-Reply-To: <20220306125016.3133737-1-sjg@chromium.org>

Add a bootmeth driver which handles distro boot from a network device, so
we can boot a bootflow using this commonly used mechanism.

In effect, this provides the same functionality as the 'pxe' command
and shares the same code. But the interface into it is via a bootmeth.

For now this requires the 'pxe' command be enabled. Future work may tidy
this up so that it can be used without CONFIG_CMDLINE being enabled.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

(no changes since v3)

Changes in v3:
- Add a log category
- Use a short name when BOOTSTD_FULL is not enabled

 boot/Kconfig        |  10 +++
 boot/Makefile       |   1 +
 boot/bootmeth_pxe.c | 186 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 197 insertions(+)
 create mode 100644 boot/bootmeth_pxe.c

diff --git a/boot/Kconfig b/boot/Kconfig
index 5f9fca4ffa..15b5e2e99b 100644
--- a/boot/Kconfig
+++ b/boot/Kconfig
@@ -339,6 +339,16 @@ config BOOTMETH_DISTRO
 
 	  This provides a way to try out standard boot on an existing boot flow.
 
+config BOOTMETH_DISTRO_PXE
+	bool "Bootdev support for distro boot over network"
+	depends on CMD_PXE && CMD_NET && DM_ETH
+	default y
+	help
+	  Enables support for distro boot using bootdevs. This makes the
+	  bootdevs look for a 'extlinux/extlinux.conf' on the tftp server.
+
+	  This provides a way to try out standard boot on an existing boot flow.
+
 endif
 
 config LEGACY_IMAGE_FORMAT
diff --git a/boot/Makefile b/boot/Makefile
index d8f5c340b8..170fcac8ec 100644
--- a/boot/Makefile
+++ b/boot/Makefile
@@ -29,6 +29,7 @@ obj-$(CONFIG_$(SPL_TPL_)BOOTSTD) += bootmeth-uclass.o
 obj-$(CONFIG_$(SPL_TPL_)BOOTSTD) += bootstd-uclass.o
 
 obj-$(CONFIG_$(SPL_TPL_)BOOTMETH_DISTRO) += bootmeth_distro.o
+obj-$(CONFIG_$(SPL_TPL_)BOOTMETH_DISTRO_PXE) += bootmeth_pxe.o
 
 obj-$(CONFIG_$(SPL_TPL_)OF_LIBFDT) += image-fdt.o
 obj-$(CONFIG_$(SPL_TPL_)FIT_SIGNATURE) += fdt_region.o
diff --git a/boot/bootmeth_pxe.c b/boot/bootmeth_pxe.c
new file mode 100644
index 0000000000..f1e2b4c776
--- /dev/null
+++ b/boot/bootmeth_pxe.c
@@ -0,0 +1,186 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Bootmethod for distro boot using PXE (network boot)
+ *
+ * Copyright 2021 Google LLC
+ * Written by Simon Glass <sjg@chromium.org>
+ */
+
+#define LOG_CATEGORY UCLASS_BOOTSTD
+
+#include <common.h>
+#include <bootdev.h>
+#include <bootflow.h>
+#include <bootmeth.h>
+#include <command.h>
+#include <distro.h>
+#include <dm.h>
+#include <fs.h>
+#include <log.h>
+#include <malloc.h>
+#include <mapmem.h>
+#include <mmc.h>
+#include <net.h>
+#include <pxe_utils.h>
+
+static int disto_pxe_getfile(struct pxe_context *ctx, const char *file_path,
+			     char *file_addr, ulong *sizep)
+{
+	struct distro_info *info = ctx->userdata;
+	ulong addr;
+	int ret;
+
+	addr = simple_strtoul(file_addr, NULL, 16);
+	ret = bootmeth_read_file(info->dev, info->bflow, file_path, addr,
+				 sizep);
+	if (ret)
+		return log_msg_ret("read", ret);
+
+	return 0;
+}
+
+static int distro_pxe_check(struct udevice *dev, struct bootflow_iter *iter)
+{
+	int ret;
+
+	/* This only works on network devices */
+	ret = bootflow_iter_uses_network(iter);
+	if (ret)
+		return log_msg_ret("net", ret);
+
+	return 0;
+}
+
+static int distro_pxe_read_bootflow(struct udevice *dev, struct bootflow *bflow)
+{
+	const char *addr_str;
+	char fname[200];
+	char *bootdir;
+	ulong addr;
+	ulong size;
+	char *buf;
+	int ret;
+
+	addr_str = env_get("pxefile_addr_r");
+	if (!addr_str)
+		return log_msg_ret("pxeb", -EPERM);
+	addr = simple_strtoul(addr_str, NULL, 16);
+
+	log_debug("calling pxe_get()\n");
+	ret = pxe_get(addr, &bootdir, &size);
+	log_debug("pxe_get() returned %d\n", ret);
+	if (ret)
+		return log_msg_ret("pxeb", ret);
+	bflow->size = size;
+
+	/* Use the directory of the dhcp bootdir as our subdir, if provided */
+	if (bootdir) {
+		const char *last_slash;
+		int path_len;
+
+		last_slash = strrchr(bootdir, '/');
+		if (last_slash) {
+			path_len = (last_slash - bootdir) + 1;
+			bflow->subdir = malloc(path_len + 1);
+			memcpy(bflow->subdir, bootdir, path_len);
+			bflow->subdir[path_len] = '\0';
+		}
+	}
+	snprintf(fname, sizeof(fname), "%s%s",
+		 bflow->subdir ? bflow->subdir : "", DISTRO_FNAME);
+
+	bflow->fname = strdup(fname);
+	if (!bflow->fname)
+		return log_msg_ret("name", -ENOMEM);
+
+	bflow->state = BOOTFLOWST_READY;
+
+	/* Allocate the buffer, including the \0 byte added by get_pxe_file() */
+	buf = malloc(size + 1);
+	if (!buf)
+		return log_msg_ret("buf", -ENOMEM);
+	memcpy(buf, map_sysmem(addr, 0), size + 1);
+	bflow->buf = buf;
+
+	return 0;
+}
+
+static int distro_pxe_read_file(struct udevice *dev, struct bootflow *bflow,
+				const char *file_path, ulong addr, ulong *sizep)
+{
+	char *tftp_argv[] = {"tftp", NULL, NULL, NULL};
+	struct pxe_context *ctx = dev_get_priv(dev);
+	char file_addr[17];
+	ulong size;
+	int ret;
+
+	sprintf(file_addr, "%lx", addr);
+	tftp_argv[1] = file_addr;
+	tftp_argv[2] = (void *)file_path;
+
+	if (do_tftpb(ctx->cmdtp, 0, 3, tftp_argv))
+		return -ENOENT;
+	ret = pxe_get_file_size(&size);
+	if (ret)
+		return log_msg_ret("tftp", ret);
+	if (size > *sizep)
+		return log_msg_ret("spc", -ENOSPC);
+	*sizep = size;
+
+	return 0;
+}
+
+static int distro_pxe_boot(struct udevice *dev, struct bootflow *bflow)
+{
+	struct pxe_context *ctx = dev_get_priv(dev);
+	struct cmd_tbl cmdtp = {};	/* dummy */
+	struct distro_info info;
+	ulong addr;
+	int ret;
+
+	addr = map_to_sysmem(bflow->buf);
+	info.dev = dev;
+	info.bflow = bflow;
+	info.cmdtp = &cmdtp;
+	ret = pxe_setup_ctx(ctx, &cmdtp, disto_pxe_getfile, &info, false,
+			    bflow->subdir);
+	if (ret)
+		return log_msg_ret("ctx", -EINVAL);
+
+	ret = pxe_process(ctx, addr, false);
+	if (ret)
+		return log_msg_ret("bread", -EINVAL);
+
+	return 0;
+}
+
+static int distro_bootmeth_pxe_bind(struct udevice *dev)
+{
+	struct bootmeth_uc_plat *plat = dev_get_uclass_plat(dev);
+
+	plat->desc = IS_ENABLED(CONFIG_BOOTSTD_FULL) ?
+		"PXE boot from a network device" : "PXE";
+
+	return 0;
+}
+
+static struct bootmeth_ops distro_bootmeth_pxe_ops = {
+	.check		= distro_pxe_check,
+	.read_bootflow	= distro_pxe_read_bootflow,
+	.read_file	= distro_pxe_read_file,
+	.boot		= distro_pxe_boot,
+};
+
+static const struct udevice_id distro_bootmeth_pxe_ids[] = {
+	{ .compatible = "u-boot,distro-pxe" },
+	{ }
+};
+
+U_BOOT_DRIVER(bootmeth_pxe) = {
+	.name		= "bootmeth_pxe",
+	.id		= UCLASS_BOOTMETH,
+	.of_match	= distro_bootmeth_pxe_ids,
+	.ops		= &distro_bootmeth_pxe_ops,
+	.bind		= distro_bootmeth_pxe_bind,
+	.priv_auto	= sizeof(struct pxe_context),
+};
-- 
2.35.1.616.g0bdcbb4464-goog


  parent reply	other threads:[~2022-03-06 13:00 UTC|newest]

Thread overview: 56+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-06 12:49 [PATCH v4 00/33] Initial implementation of standard boot Simon Glass
2022-03-06 12:49 ` [PATCH v4 01/33] lib: Move string tests to the string module Simon Glass
2022-03-06 12:49 ` [PATCH v4 02/33] test: Add tests for trailing_strtol() Simon Glass
2022-03-06 12:49 ` [PATCH v4 03/33] lib: Correct comment formatting to avoid sphinx problems Simon Glass
2022-03-06 12:49 ` [PATCH v4 04/33] lib: Fix a few bugs in trailing_strtoln() Simon Glass
2022-03-06 12:49 ` [PATCH v4 05/33] lib: Add a way to find the postiion of a trailing number Simon Glass
2022-03-06 12:49 ` [PATCH v4 06/33] dm: core: Rename and fix uclass_get_by_name_len() Simon Glass
2022-03-06 12:49 ` [PATCH v4 07/33] dm: core: Allow finding a uclass device by partial name Simon Glass
2022-03-06 12:49 ` [PATCH v4 08/33] test: fastboot: Avoid using mmc1 Simon Glass
2022-03-06 12:49 ` [PATCH v4 09/33] test: dm: Restart USB before assuming it is stopped Simon Glass
2022-03-06 12:49 ` [PATCH v4 10/33] dm: blk: Add a function to return the device type Simon Glass
2022-03-06 12:49 ` [PATCH v4 11/33] fs: Add a function to set the filesystem type Simon Glass
2022-03-06 12:49 ` [PATCH v4 12/33] bootstd: Add the concept of a bootflow Simon Glass
2022-03-06 12:49 ` [PATCH v4 13/33] bootstd: Add the bootstd uclass and core implementation Simon Glass
2022-03-12 14:35   ` Ilias Apalodimas
2022-03-12 17:58     ` Simon Glass
2022-03-06 12:49 ` [PATCH v4 14/33] bootstd: Add the bootdev uclass Simon Glass
2022-03-06 12:49 ` [PATCH v4 15/33] bootstd: Add the bootmeth uclass and helpers Simon Glass
2022-03-06 12:49 ` [PATCH v4 16/33] bootstd: Add support for bootflows Simon Glass
2022-03-06 12:50 ` [PATCH v4 17/33] bootstd: Add a bootdev command Simon Glass
2022-03-06 12:50 ` [PATCH v4 19/33] bootstd: Add a bootmeth command Simon Glass
2022-03-06 12:50 ` [PATCH v4 21/33] bootstd: mmc: Add a bootdev driver Simon Glass
2022-03-06 12:50 ` [PATCH v4 22/33] bootstd: ethernet: " Simon Glass
2022-03-06 12:50 ` Simon Glass [this message]
2022-03-06 12:50 ` [PATCH v4 24/33] bootstd: Add an implementation of EFI boot Simon Glass
2022-03-06 12:50 ` [PATCH v4 25/33] bootstd: Add a system bootdev for strange boot methods Simon Glass
2022-03-06 12:50 ` [PATCH v4 27/33] bootstd: Add a sandbox bootmeth driver Simon Glass
2022-03-06 12:50 ` [PATCH v4 28/33] bootstd: sandbox: Add a hostfs bootdev Simon Glass
2022-03-06 12:50 ` [PATCH v4 30/33] bootstd: usb: Add a bootdev driver Simon Glass
2022-03-06 12:50 ` [PATCH v4 31/33] bootstd: Add tests for bootstd including all uclasses Simon Glass
2022-03-06 12:50 ` [PATCH v4 32/33] bootstd: Add setup for the bootflow tests Simon Glass
2022-03-06 12:50 ` [PATCH v4 33/33] bootstd: doc: Add documentation Simon Glass
2022-03-13 15:47 ` [PATCH v4 00/33] Initial implementation of standard boot Mark Kettenis
2022-03-13 22:23   ` Simon Glass
2022-03-23 14:05 ` Tom Rini
2022-03-23 18:45   ` Simon Glass
2022-03-23 19:21     ` Michael Nazzareno Trimarchi
2022-03-23 19:30       ` Tom Rini
2022-03-23 19:57         ` Michael Nazzareno Trimarchi
2022-03-23 20:07           ` Tom Rini
2022-03-25 14:36             ` Michael Nazzareno Trimarchi
2022-03-25 14:50               ` Tom Rini
2022-03-26 19:56                 ` Simon Glass
2022-03-26 19:58                   ` Tom Rini
2022-03-26 20:51                     ` Simon Glass
2022-03-27 11:00                       ` Michael Nazzareno Trimarchi
2022-03-29  1:36                       ` Tom Rini
2022-03-23 23:15       ` Mark Kettenis
2022-03-24  2:18         ` Simon Glass
2022-03-23 20:09     ` Tom Rini
2022-03-24  2:13       ` Simon Glass
2022-03-24 14:46         ` Tom Rini
2022-03-24 18:33           ` Simon Glass
2022-03-24 19:27             ` Tom Rini
2022-03-24 20:04               ` Simon Glass
2022-03-24 21:16                 ` Tom Rini

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220306125016.3133737-15-sjg@chromium.org \
    --to=sjg@chromium.org \
    --cc=daniel.schwierzeck@gmail.com \
    --cc=dennis@ausil.us \
    --cc=ilias.apalodimas@linaro.org \
    --cc=jaeckel-floss@eyet-services.de \
    --cc=lukas.auer@aisec.fraunhofer.de \
    --cc=michal.simek@xilinx.com \
    --cc=trini@konsulko.com \
    --cc=u-boot@lists.denx.de \
    --cc=xypron.glpk@gmx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.