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 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 smtp.lore.kernel.org (Postfix) with ESMTPS id 1D001C352A1 for ; Wed, 7 Dec 2022 09:00:41 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id B22FB8562C; Wed, 7 Dec 2022 09:55:59 +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="P5kC+b8X"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 17EC9855FC; Wed, 7 Dec 2022 09:55:43 +0100 (CET) Received: from mail-pj1-x102d.google.com (mail-pj1-x102d.google.com [IPv6:2607:f8b0:4864:20::102d]) (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 EF20B8557C for ; Wed, 7 Dec 2022 09:55:38 +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-pj1-x102d.google.com with SMTP id o12so16936484pjo.4 for ; Wed, 07 Dec 2022 00:55:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=hq8oUI6O1HwdBfb6jjmBIFN4xdzabjPe76CI3LkaQ5Q=; b=P5kC+b8XT2d+fmaPXYr/6T2dTEoA9Pekqz8nWZaLBnas2hZkPdzbTyldd8bMBbZpkG BeEuq5DpU5SOcr4t4+zdFVCLaEr4DMiCUnBEtCXl9d99A2EKOgv4gyAzK/efKjNhSb59 U0dY1THMfd6//ecOrk51oadu27tUlGrdWvdkQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=hq8oUI6O1HwdBfb6jjmBIFN4xdzabjPe76CI3LkaQ5Q=; b=zsQ1t/+xuXXFJiX37QkAB6HUWzbUg7jS49DWXVMXMXau2idu+n+mqsSujb76G7fnoN VdGeuKa/xW8r2xcTnZH27Y/gteGoaNgwR0cimzrlA5Lq78vN8GrAVxk6KDkC4i06+5OY zHWcE423tnzdDwDyXkTTFTtMAi9SCgQlJQeJDZyxhDIaLXiYvPOqXZtCNibe0qYGlCgT wXSs+s8wvBioa8HMOvDDLvjeMzc1fFx9uvTLP5xs80wJxGdQeOrnyOOwx9hHXHVs/fLx 9B4bnyTl6nLiylodk3cUtnQrZggLJsQvGjF7NiC/vf+sWUkRTvFp1gvFb4necwI2GJl+ 9ntg== X-Gm-Message-State: ANoB5pmZhY0ekMiY3FrQwQ6dO14fc14aBMgGplJEuzgpuZSQvidmc/0d G1zDptBQotCSgYqsfmoFmu/q1H+oP9SOJ0XX X-Google-Smtp-Source: AA0mqf6+DFxq/jtrueT9Os7LRtSEsvHJkisqUbfijPKm4mcDMR3HDsIuz/oMOYOGMDz447mEcz1+PA== X-Received: by 2002:a17:90b:33c8:b0:219:94b2:2004 with SMTP id lk8-20020a17090b33c800b0021994b22004mr23607894pjb.215.1670403337115; Wed, 07 Dec 2022 00:55:37 -0800 (PST) Received: from localhost.localdomain ([27.110.126.54]) by smtp.gmail.com with ESMTPSA id l16-20020a170903121000b0018157b415dbsm14145514plh.63.2022.12.07.00.55.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Dec 2022 00:55:36 -0800 (PST) From: Simon Glass To: U-Boot Mailing List Cc: U-Boot Custodians , Simon Glass , =?UTF-8?q?C=C3=A9dric=20Le=20Goater?= , Heinrich Schuchardt , Joe Hershberger , Ramon Fried , Samuel Mendoza-Jonas , Viacheslav Mitrofanov , "Ying-Chun Liu (PaulLiu)" Subject: [PATCH 30/71] net: Add a function to run dhcp Date: Wed, 7 Dec 2022 21:50:56 +1300 Message-Id: <20221207085137.445249-31-sjg@chromium.org> X-Mailer: git-send-email 2.39.0.rc0.267.gcb52ba06e7-goog In-Reply-To: <20221207085137.445249-1-sjg@chromium.org> References: <20221207085137.445249-1-sjg@chromium.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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.6 at phobos.denx.de X-Virus-Status: Clean At present this must be done by executing the command. Also it involves fiddling with the environment to determine the correct autoload behaviour. Ideally it should be possible to run network operations without even having the command line present (CONFIG_CMDLINE). For now, add a function to handle DHCP, so it can be called from a bootdev more easily. Signed-off-by: Simon Glass --- cmd/net.c | 35 +++++++++++++++++++++++++++++++++++ include/net.h | 15 +++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/cmd/net.c b/cmd/net.c index 0e9f200ca97..6b3774bfe7c 100644 --- a/cmd/net.c +++ b/cmd/net.c @@ -4,6 +4,8 @@ * Wolfgang Denk, DENX Software Engineering, wd@denx.de. */ +#define LOG_CATEGORY UCLASS_ETH + /* * Boot support */ @@ -13,6 +15,7 @@ #include #include #include +#include #include #include #include @@ -120,6 +123,38 @@ U_BOOT_CMD( "boot image via network using DHCP/TFTP protocol", "[loadAddress] [[hostIPaddr:]bootfilename]" ); + +int dhcp_run(ulong addr, const char *fname, bool autoload) +{ + char *dhcp_argv[] = {"dhcp", NULL, (char *)fname, NULL}; + struct cmd_tbl cmdtp = {}; /* dummy */ + char file_addr[17]; + int old_autoload; + int ret, result; + + log_debug("addr=%lx, fname=%s, autoload=%d\n", addr, fname, autoload); + old_autoload = env_get_yesno("autoload"); + ret = env_set("autoload", autoload ? "y" : "n"); + if (ret) + return log_msg_ret("en1", -EINVAL); + + if (autoload) { + sprintf(file_addr, "%lx", addr); + dhcp_argv[1] = file_addr; + } + + result = do_dhcp(&cmdtp, 0, !autoload ? 1 : fname ? 3 : 2, dhcp_argv); + + ret = env_set("autoload", old_autoload == -1 ? NULL : + old_autoload ? "y" : "n"); + if (ret) + return log_msg_ret("en2", -EINVAL); + + if (result) + return log_msg_ret("res", -ENOENT); + + return 0; +} #endif #if defined(CONFIG_CMD_NFS) diff --git a/include/net.h b/include/net.h index 749f20fa967..c296d06c266 100644 --- a/include/net.h +++ b/include/net.h @@ -65,6 +65,21 @@ struct in_addr { */ int do_tftpb(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]); +/** + * dhcp_run() - Run DHCP on the current ethernet device + * + * This sets the autoload variable, then puts it back to similar to its original + * state (y, n or unset). + * + * @addr: Address to load the file into (0 if @autoload is false) + * @fname: Filename of file to load (NULL if @autoload is false or to use the + * default filename) + * @autoload: true to load the file, false to just get the network IP + * @return 0 if OK, -EINVAL if the environment failed, -ENOENT if ant file was + * not found + */ +int dhcp_run(ulong addr, const char *fname, bool autoload); + /** * An incoming packet handler. * @param pkt pointer to the application packet -- 2.39.0.rc0.267.gcb52ba06e7-goog