From patchwork Wed Jan 19 01:43:05 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 1581611 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.a=rsa-sha256 header.s=google header.b=eOuZJtS0; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Received: from phobos.denx.de (phobos.denx.de [IPv6:2a01:238:438b:c500:173d:9f52:ddab:ee01]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4JdpRC6rDMz9sPC for ; Wed, 19 Jan 2022 12:47:03 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 8BA4083876; Wed, 19 Jan 2022 02:45:08 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="eOuZJtS0"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 9F2C183178; Wed, 19 Jan 2022 02:44:21 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-il1-x12d.google.com (mail-il1-x12d.google.com [IPv6:2607:f8b0:4864:20::12d]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 7A84A83810 for ; Wed, 19 Jan 2022 02:44:01 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=sjg@chromium.org Received: by mail-il1-x12d.google.com with SMTP id v17so867742ilg.4 for ; Tue, 18 Jan 2022 17:44:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=lloZJ7gmOC/re0qxBCkpNOwYk1w9gikjziH4ZfsE9PM=; b=eOuZJtS0DIBsviLNNRUfheJbACYFutmy7GinT70Ggzn3vhunuK1h0zs0bFog6Ot84K 40m2q+g63zacAsFdOdj1HSCpN/nXBa7rEp4R9Y9JXaaL+6dJfV6VjBBHHjVKe0an9+hr Xm9wJmYH/mWfoAk9uuaU812856qqH2haGNhvM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=lloZJ7gmOC/re0qxBCkpNOwYk1w9gikjziH4ZfsE9PM=; b=SGeiNNqxBlVrSENoE07iH6UFALFr9uQ9skm86QGBL7WXGp1C/I+N/EcK/qD7DL6o3x HfX+I93cu/s7ig09xVG88n2//JHqNkpfkaX68/FrEb2HO3VX/nsUV9q6Q9KSzo0B79ol 4WAir0l8kpKKYjvhTcukoMn7uBdEyqIWmM02znkeDPB7FG5xyM0YIoMU432tDvCUPufX a9G2vUPnPrELdU9AYW8lw8K5B8GXZ7M+KIEOe1dReg2HL6iCXc0ue0rItipxIfQCv7ZJ tQJk3NZaxPVEUPeHgZe5INGC8Pw6Y7iTvj04hLKk1aOb/Ju62wik/NT/lq81WUABmlIK TjoA== X-Gm-Message-State: AOAM531ifU0RRVChVsZPofFyfQd9wWFaVuaz5O5IW5r+X34fdW7bkXjt Osj04GTYsq+2TgtTiJohGasVCuayIUceZA== X-Google-Smtp-Source: ABdhPJys3AvNpSMAuNdhtJnUNqNpiFcBBWgrBKcz6QzE4hNZuJM1h8MlN1PS85poR3N+tQzRpnX7Lg== X-Received: by 2002:a05:6e02:1908:: with SMTP id w8mr8851590ilu.50.1642556639987; Tue, 18 Jan 2022 17:43:59 -0800 (PST) Received: from kiwi.bld.corp.google.com (c-67-190-101-114.hsd1.co.comcast.net. [67.190.101.114]) by smtp.gmail.com with ESMTPSA id j18sm10546314ila.86.2022.01.18.17.43.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 18 Jan 2022 17:43:59 -0800 (PST) From: Simon Glass To: U-Boot Mailing List Cc: Ilias Apalodimas , Daniel Schwierzeck , Dennis Gilmore , Steffen Jaeckel , Lukas Auer , Michal Simek , Tom Rini , Heinrich Schuchardt , Simon Glass Subject: [PATCH v3 21/31] bootstd: Add an implementation of distro PXE boot Date: Tue, 18 Jan 2022 18:43:05 -0700 Message-Id: <20220119014315.1938157-13-sjg@chromium.org> X-Mailer: git-send-email 2.34.1.703.g22d0c6ccf7-goog In-Reply-To: <20220119014315.1938157-1-sjg@chromium.org> References: <20220119014315.1938157-1-sjg@chromium.org> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.2 at phobos.denx.de X-Virus-Status: Clean 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 --- 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 45d38a226cb..61df705141d 100644 --- a/boot/Kconfig +++ b/boot/Kconfig @@ -328,6 +328,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 d8f5c340b85..170fcac8ec4 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 00000000000..f1e2b4c7762 --- /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 + */ + +#define LOG_CATEGORY UCLASS_BOOTSTD + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +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), +};