From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3C506C433EF for ; Sat, 23 Oct 2021 23:30:48 +0000 (UTC) Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id BEC9660C4D for ; Sat, 23 Oct 2021 23:30:47 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org BEC9660C4D Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=chromium.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=lists.denx.de Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 7BE8983537; Sun, 24 Oct 2021 01:30:18 +0200 (CEST) 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="TJRGYM/N"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 7482F8355F; Sun, 24 Oct 2021 01:28:03 +0200 (CEST) Received: from mail-oi1-x233.google.com (mail-oi1-x233.google.com [IPv6:2607:f8b0:4864:20::233]) (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 7F623834F1 for ; Sun, 24 Oct 2021 01:27:19 +0200 (CEST) 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-oi1-x233.google.com with SMTP id r6so10051175oiw.2 for ; Sat, 23 Oct 2021 16:27:19 -0700 (PDT) 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=VdKyQVocqCqa3MHurt9kZ0Kn8o5nNa4cWxLQ4nIiQQI=; b=TJRGYM/Nfg8LzTlWLcnfdY9fziKKkCDoq1lOlU1uaGwhAlKx0fDGKRuOQ7vy+YM6YN mkGYk+ieojMmPP9nuls1gBdgJEACrTEOlbPwUMKt2RKN4/DDys+mQoHdaxE1pVQeH2UX F3/ds6n3F+NkkLdsXsQGnRdCr1FXFqui9lo3U= 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=VdKyQVocqCqa3MHurt9kZ0Kn8o5nNa4cWxLQ4nIiQQI=; b=QuHC8c7b1++qkE2V1V2E1E3SeksLJXXwkFkga0fcgrYKwPm/EoNXo1+FNGl+Zs9NB9 nkdO+ZITWyqIKxCJJi1Kzfikipfw0UG62+oMm53nVrn8zvkE79MHIlhOLbeqpkoKrNYQ VevvZamWxcj0TnPo3thupa/dOla8BpgvBZUp9eszs0z6atS1rYy1EZZJI9n50Yv5oBJf CBHXyT3yjRGX0EsbX4/IYbdLTQTbn/Gt6Z7lhmskBo5+NQtAkAc4SdOd9Kyu/7vWVSGz tNw/lcoOxp7MFeoy+YEtySF7kfY2MLd6LuNV1K/H1unuHDrMFoKbGClgmGmw+EeQqMyM 3VvQ== X-Gm-Message-State: AOAM53206+ziUyumrrqhLQkSjBhO0tzuGLqX85Do4/MMp34N4/dP5kmJ 0nUIslTpYegLmSbRZpangNBMFKtG71peGw== X-Google-Smtp-Source: ABdhPJzpSNZ/kXqfQISiZytYAfw9yce54+MysWqRNDcjRDjyi62hmqtyNgPFgOBnQN0kNRImptlgHw== X-Received: by 2002:a05:6808:1aa9:: with SMTP id bm41mr6122656oib.55.1635031638015; Sat, 23 Oct 2021 16:27:18 -0700 (PDT) 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 l24sm2253885oop.4.2021.10.23.16.27.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 23 Oct 2021 16:27:17 -0700 (PDT) From: Simon Glass To: U-Boot Mailing List Cc: Michal Simek , Heinrich Schuchardt , Tom Rini , Ilias Apalodimas , Daniel Schwierzeck , Steffen Jaeckel , =?UTF-8?q?Marek=20Beh=C3=BAn?= , Lukas Auer , Dennis Gilmore , Simon Glass Subject: [PATCH v2 30/41] bootstd: Add an implementation of distro PXE boot Date: Sat, 23 Oct 2021 17:26:24 -0600 Message-Id: <20211023232635.9195-21-sjg@chromium.org> X-Mailer: git-send-email 2.33.0.1079.g6e70778dc9-goog In-Reply-To: <20211023232635.9195-1-sjg@chromium.org> References: <20211023232635.9195-1-sjg@chromium.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 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 --- (no changes since v1) boot/Kconfig | 10 +++ boot/Makefile | 1 + boot/bootmeth_pxe.c | 183 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 194 insertions(+) create mode 100644 boot/bootmeth_pxe.c diff --git a/boot/Kconfig b/boot/Kconfig index c5c8be70da9..7e948f95e92 100644 --- a/boot/Kconfig +++ b/boot/Kconfig @@ -308,6 +308,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..6c92ce39265 --- /dev/null +++ b/boot/bootmeth_pxe.c @@ -0,0 +1,183 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Bootmethod for distro boot using PXE (network boot) + * + * Copyright 2021 Google LLC + * Written by Simon Glass + */ + +#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 = "PXE boot from a network device"; + + 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.33.0.1079.g6e70778dc9-goog