Skip to contentSkip to conversation

net: lwip: bring up Ethernet links and load network boot images

PATCH In review James Hilliard 2 participants 21 messages Nov 26 – Dec 2

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

cover net: lwip: bring up Ethernet links and load network boot images

Boards booting over Ethernet need a working link and an address before they can fetch an image. Add IPv4 link-local addressing and reverse ARP, honor the configured VLAN, and let boot scripts obtain the Ethernet address used to reach the boot server. CDP can supply the VLAN settings; on NC-SI ports, bring up the sideband channel before starting an IP client. Both management protocols receive their frames before ordinary VLAN filtering.

For NFS boot, use a reserved source port, restart a timed-out download according to netretry, and honor autostart after a nonempty download. SPL uses BOOTP or DHCP followed by TFTP and loads the image using the received file length.

---
James Hilliard (19):
env: invalidate caches after bulk imports
net: lwip: support the IPv4 linklocal command
net: lwip: apply the configured Ethernet VLAN
net: lwip: export the Ethernet address used to reach serverip
net: cdp: decode advertisements from the complete Ethernet frame
net: cdp: calculate checksums from network-order bytes
net: lwip: discover VLAN settings with CDP
net: lwip: bind NFS requests to a reserved UDP port
net: lwip: honor autostart after an NFS download
net: lwip: restart NFS after an RPC timeout
net: lwip: support network boot with reverse ARP
boot: pxe: allow boot without the DHCP command
spl: use the known file length for raw U-Boot images
spl: fit: read the exact size of an embedded FIT
spl: net: bound reads to the downloaded file
spl: net: load a boot image with lwIP
net: ncsi: check the selected interface before probing
net: ncsi: track the outstanding management command
net: lwip: configure NC-SI before starting network clients

 board/sunxi/board.c          |   1 +
 boot/bootmeth_pxe.c          |   8 +-
 cmd/Kconfig                  |   8 +-
 cmd/lwip/Makefile            |   2 +
 cmd/lwip/linklocal.c         |  71 +++++++++
 cmd/lwip/ncsi.c              |  23 +++
 cmd/lwip/ping.c              |   2 +-
 cmd/nvedit.c                 |   7 +-
 common/spl/Kconfig           |  10 +-
 common/spl/spl.c             |  49 +++---
 common/spl/spl_fit.c         |   9 +-
 common/spl/spl_nand.c        |   3 +-
 common/spl/spl_net.c         |  23 ++-
 drivers/net/phy/Kconfig      |   2 +-
 drivers/net/phy/ncsi.c       | 302 +++++++++++++++++++++--------------
 env/common.c                 |  25 ++-
 include/net-lwip.h           |   2 +
 include/net/ncsi.h           |   5 +-
 include/spl.h                |  20 ++-
 include/spl_load.h           |   2 +-
 lib/Makefile                 |   2 +-
 lib/lwip/u-boot/lwip-hooks.h |  10 ++
 lib/lwip/u-boot/lwipopts.h   |  19 +--
 net/Kconfig                  |   8 +-
 net/Makefile                 |   4 +-
 net/cdp.c                    | 311 ++++++++++++++----------------------
 net/cdp.h                    |  17 +-
 net/lwip/Makefile            |   7 +
 net/lwip/cdp.c               |  98 ++++++++++++
 net/lwip/dhcp.c              |  71 ++++-----
 net/lwip/net-lwip.c          | 188 ++++++++++++++++++++--
 net/lwip/nfs.c               |  77 ++++++---
 net/lwip/rarp.c              | 181 +++++++++++++++++++++
 net/lwip/wol.c               |   7 +
 net/net.c                    |  79 ++++-----
 net/nfs-common.c             |   5 +
 net/nfs-common.h             |   1 +
 test/dm/Makefile             |   9 ++
 test/dm/eth.c                |  81 ++++++++++
 test/dm/lwip.c               | 233 +++++++++++++++++++++++++++
 test/dm/lwip_cdp.c           | 117 ++++++++++++++
 test/dm/lwip_dhcp.c          |  23 ++-
 test/dm/lwip_ipv6.c          | 116 +++++++++++++-
 test/dm/lwip_rarp.c          | 139 ++++++++++++++++
 test/dm/lwip_test.h          |  42 +++++
 test/dm/ncsi.c               | 369 +++++++++++++++++++++++++++++++++++++++++++
 test/image/spl_load.c        | 113 +++++++++++++
 test/image/spl_load_net.c    |  43 ++++-
 48 files changed, 2444 insertions(+), 500 deletions(-)

---
base-commit: d778970a189b977c14f93961d7009f404d895b5b
base-tree: https://git.u-boot-project.org/u-boot/custodians/u-boot-net.git for-next
change-id: 20261123-submit-lwip-network-boot-v1-80f73f7fa642

Best regards,

Signature · 1 line
James Hilliard <[email protected]>
#1 lore raw

PATCH 1/19 env: invalidate caches after bulk imports

After himport_r() replaces environment entries, env_get_id() must change so cached consumers reread them. Otherwise eth_set_current() can retain an ethact pointer from before the import, leaving network device selection based on a value that no longer matches the environment.

Advance the ID after bulk imports and default resets, including imports that fail after partially updating the environment. This makes consumers refresh against the resulting environment.

---
 board/sunxi/board.c |  1 +
 cmd/nvedit.c        |  7 +++++--
 env/common.c        | 25 +++++++++++++++++--------
 3 files changed, 23 insertions(+), 10 deletions(-)
board/sunxi/board.c +1 -0
index 3d1afec7c66..fb55dc7eb8b 100644
@@ -742,6 +742,7 @@ static void parse_spl_header(const uint32_t spl_addr)
 		 */
 		himport_r(&env_htab, (char *)(uintptr_t)spl->fel_script_address,
 			  spl->fel_uEnv_length, '\n', H_NOCLEAR, 0, 0, NULL);
+		env_inc_id();
 		return;
 	}
 	/* otherwise assume .scr format (mkimage-type script) */
cmd/nvedit.c +5 -2
index 636bddee1be..22db0c38ff1 100644
@@ -775,6 +775,7 @@ static int do_env_import(struct cmd_tbl *cmdtp, int flag,
 	int	del = 0;
 	int	crlf_is_lf = 0;
 	int	wl = 0;
+	int ret;
 	size_t	size;
 
 	cmd = *argv;
@@ -869,8 +870,10 @@ static int do_env_import(struct cmd_tbl *cmdtp, int flag,
 		ptr = (char *)ep->data;
 	}
 
-	if (!himport_r(&env_htab, ptr, size, sep, del ? 0 : H_NOCLEAR,
-		       crlf_is_lf, wl ? argc - 2 : 0, wl ? &argv[2] : NULL)) {
+	ret = himport_r(&env_htab, ptr, size, sep, del ? 0 : H_NOCLEAR,
+			crlf_is_lf, wl ? argc - 2 : 0, wl ? &argv[2] : NULL);
+	env_inc_id();
+	if (!ret) {
 		pr_err("## Error: Environment import failed: errno = %d\n",
 		       errno);
 		return 1;
env/common.c +17 -8
index b2adbe93dbe..f217d67032b 100644
@@ -387,6 +387,8 @@ static int env_update_fdt_addr_from_bloblist(void)
 
 void env_set_default(const char *s, int flags)
 {
+	int ret;
+
 	if (s) {
 		if ((flags & H_INTERACTIVE) == 0) {
 			printf("*** Warning - %s, "
@@ -399,9 +401,10 @@ void env_set_default(const char *s, int flags)
 	}
 
 	flags |= H_DEFAULT;
-	if (himport_r(&env_htab, default_environment,
-			sizeof(default_environment), '\0', flags, 0,
-			0, NULL) == 0) {
+	ret = himport_r(&env_htab, default_environment,
+			sizeof(default_environment), '\0', flags, 0, 0, NULL);
+	env_inc_id();
+	if (!ret) {
 		pr_err("## Error: Environment import failed: errno = %d\n",
 		       errno);
 		return;
@@ -418,6 +421,8 @@ void env_set_default(const char *s, int flags)
 /* [re]set individual variables to their value in the default environment */
 int env_set_default_vars(int nvars, char * const vars[], int flags)
 {
+	int ret;
+
 	/*
 	 * Special use-case: import from default environment
 	 * (and use \0 as a separator)
@@ -431,9 +436,10 @@ int env_set_default_vars(int nvars, char * const vars[], int flags)
 		flags |= H_NOCLEAR;
 
 	flags |= H_DEFAULT;
-	return himport_r(&env_htab, default_environment,
-				sizeof(default_environment), '\0',
-				flags, 0, nvars, vars);
+	ret = himport_r(&env_htab, default_environment,
+			sizeof(default_environment), '\0', flags, 0, nvars, vars);
+	env_inc_id();
+	return ret;
 }
 
 /*
@@ -443,6 +449,7 @@ int env_set_default_vars(int nvars, char * const vars[], int flags)
 int env_import(const char *buf, int check, int flags)
 {
 	env_t *ep = (env_t *)buf;
+	int ret;
 
 	if (check) {
 		uint32_t crc;
@@ -455,8 +462,10 @@ int env_import(const char *buf, int check, int flags)
 		}
 	}
 
-	if (himport_r(&env_htab, (char *)ep->data, ENV_SIZE, '\0', flags, 0,
-			0, NULL)) {
+	ret = himport_r(&env_htab, (char *)ep->data, ENV_SIZE, '\0', flags, 0,
+			0, NULL);
+	env_inc_id();
+	if (ret) {
 		gd->flags |= GD_FLG_ENV_READY;
 
 		/* This has to be done after GD_FLG_ENV_READY is set */
Signature · 1 line
2.53.0
#2 lore raw

PATCH 2/19 net: lwip: support the IPv4 linklocal command

In reply to James Hilliard

Allow boards without a DHCP server to acquire an IPv4 link-local address. Use the lwIP AutoIP client to probe the saved address and choose another one if a peer already owns it. Export the resulting address and subnet on the selected interface, and detach the client before releasing the shared runtime.

---
 cmd/Kconfig                |  4 +--
 cmd/lwip/Makefile          |  1 +
 cmd/lwip/linklocal.c       | 71 ++++++++++++++++++++++++++++++++++++++++++++++
 lib/lwip/u-boot/lwipopts.h |  2 +-
 test/dm/lwip.c             | 48 +++++++++++++++++++++++++++++++
 test/dm/lwip_test.h        | 42 +++++++++++++++++++++++++++
 6 files changed, 165 insertions(+), 3 deletions(-)
cmd/Kconfig +2 -2
index b44ec644b87..5c31fb00013 100644
@@ -2200,6 +2200,8 @@ config CMD_CDP
 	  and to retrieve configuration data including the VLAN id using the
 	  proprietary Cisco Discovery Protocol (CDP).
 
+endif  # if NET_LEGACY
+
 config CMD_LINK_LOCAL
 	bool "linklocal"
 	depends on (LIB_RAND || LIB_HW_RAND)
@@ -2214,8 +2216,6 @@ config CMD_NCSI
 	  Normally this happens automatically before other network
 	  operations.
 
-endif  # if NET_LEGACY
-
 config IPV6_ROUTER_DISCOVERY
 	bool "Do IPv6 router discovery"
 	depends on IPV6
cmd/lwip/Makefile +1 -0
index 606c3693d2b..90ad4d21e6f 100644
@@ -1,6 +1,7 @@
 obj-$(CONFIG_CMD_BOOTP) += bootp.o
 obj-$(CONFIG_CMD_DHCP) += dhcp.o
 obj-$(CONFIG_CMD_DNS) += dns.o
+obj-$(CONFIG_CMD_LINK_LOCAL) += linklocal.o
 obj-$(CONFIG_CMD_NFS) += nfs.o
 ifneq ($(filter y,$(CONFIG_CMD_PING) $(CONFIG_CMD_PING6)),)
 obj-y += ping.o
cmd/lwip/linklocal.c new file +71 -0

71 lines not on this page · raw message

lib/lwip/u-boot/lwipopts.h +1 -1
index 2c07bdd545c..b1f9d9cdb62 100644
@@ -205,7 +205,7 @@
 	UBOOT_DHCP_TIMEOFFSET UBOOT_DHCP_PXE_CONFIG 67
 #endif /* defined(CONFIG_CMD_BOOTP) || defined(CONFIG_CMD_DHCP) */
 
-#define LWIP_AUTOIP                     0
+#define LWIP_AUTOIP                     CONFIG_IS_ENABLED(CMD_LINK_LOCAL)
 
 #define LWIP_SNMP                       0
 
test/dm/lwip.c +48 -0

60 lines not on this page · raw message

test/dm/lwip_test.h new file +42 -0

42 lines not on this page · raw message

Signature · 1 line
2.53.0
#3 lore raw

PATCH 3/19 net: lwip: apply the configured Ethernet VLAN

In reply to James Hilliard

Tag outgoing frames with vlan, and accept untagged frames on an active VLAN only when nvlan identifies it as the native VLAN. Apply the receive policy before handing packets to either lwIP or the raw WoL listener, while leaving packet capture on the wire side of the filter.

Keep the VLAN policy fixed while clients share an interface. A VLAN change could otherwise reuse neighbor entries learned on the previous link. Reject attachment or refresh until the last client detaches and a new interface is created.

Use lwIP Ethernet encapsulation for both IPv4 and IPv6 and reserve headroom for the larger header. Exercise VLAN transmission and receive policy changes in sandbox.

---
 lib/lwip/u-boot/lwip-hooks.h |   3 +
 lib/lwip/u-boot/lwipopts.h   |   3 +-
 net/lwip/net-lwip.c          |  75 ++++++++++++++++++++-
 net/lwip/wol.c               |   7 ++
 test/dm/lwip.c               | 152 +++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 237 insertions(+), 3 deletions(-)
lib/lwip/u-boot/lwip-hooks.h +3 -0
index c4bf364ba3c..f665e80e869 100644
@@ -4,6 +4,9 @@
 #ifndef __UBOOT_LWIP_HOOKS_H
 #define __UBOOT_LWIP_HOOKS_H
 
+int net_lwip_vlan_id(void);
+#define LWIP_HOOK_VLAN_SET(...) net_lwip_vlan_id()
+
 #ifdef CONFIG_CMD_DHCP
 #include "lwip-dhcp.h"
 #endif
lib/lwip/u-boot/lwipopts.h +2 -1
index b1f9d9cdb62..ab3b39a198f 100644
@@ -244,7 +244,8 @@
 
 #define LWIP_LISTEN_BACKLOG             0
 
-#define PBUF_LINK_HLEN                  14
+#define ETHARP_SUPPORT_VLAN             1
+#define PBUF_LINK_HLEN                  18
 #define PBUF_POOL_BUFSIZE               LWIP_MEM_ALIGN_SIZE(TCP_MSS + 40 + PBUF_LINK_HLEN)
 
 #define LWIP_HAVE_LOOPIF                0
net/lwip/net-lwip.c +73 -2

143 lines not on this page · raw message

net/lwip/wol.c +7 -0
index 579f9752d50..0b0885ce4a2 100644
@@ -71,6 +71,13 @@ void net_lwip_wol_receive(struct netif *netif, const u8 *packet, int len)
 	protocol = get_unaligned_be16(packet + 2 * ARP_HLEN);
 	packet += ETHER_HDR_SIZE;
 	len -= ETHER_HDR_SIZE;
+	if (protocol == PROT_VLAN) {
+		if (len < VLAN_HLEN)
+			return;
+		protocol = get_unaligned_be16(packet + 2);
+		packet += VLAN_HLEN;
+		len -= VLAN_HLEN;
+	}
 
 	switch (protocol) {
 	case PROT_WOL:
test/dm/lwip.c +152 -0

164 lines not on this page · raw message

Signature · 1 line
2.53.0
#4 lore raw

PATCH 4/19 net: lwip: export the Ethernet address used to reach serverip

In reply to James Hilliard

Network boot scripts can pass serveraddr to Linux, for instance when configuring netconsole. Populate it from the first successful unicast IPv4 transmission to the configured server. This records the next-hop MAC that lwIP actually resolved and avoids recording unrelated ARP replies on a busy network.

---
 net/Kconfig         |  8 +++-----
 net/lwip/net-lwip.c | 40 ++++++++++++++++++++++++++++++++++++++++
 test/dm/lwip.c      | 33 +++++++++++++++++++++++++++++++++
 3 files changed, 76 insertions(+), 5 deletions(-)
net/Kconfig +3 -5
index 6d6672b9662..9b191118060 100644
@@ -138,16 +138,14 @@ config SERVERIP_FROM_PROXYDHCP_DELAY_MS
 	  does not fail an otherwise successful lease.
 	  The legacy client waits after receiving the DHCP offer.
 
-if NET_LEGACY
-
 config KEEP_SERVERADDR
 	bool "Write the server's MAC address to 'serveraddr'"
 	default y if SANDBOX
 	help
 	  Keeps the server's MAC address, in the env 'serveraddr'
-	  for passing to bootargs (like Linux's netconsole option). If this is
-	  enabled, when an ARP reply is received, the server's IP address is
-	  written there.
+	  for passing to bootargs (like Linux's netconsole option).
+
+if NET_LEGACY
 
 config UDP_CHECKSUM
 	bool "Check the UDP checksum"
net/lwip/net-lwip.c +40 -0
index d7a38b9f72f..8b55c9646ef 100644
@@ -36,6 +36,44 @@
 /* xx:xx:xx:xx:xx:xx\0 */
 #define MAC_ADDR_STRLEN 18
 
+static void net_lwip_keep_serveraddr(const u8 *frame, unsigned int len)
+{
+	static u8 saved[ETH_ALEN];
+	static struct in_addr server;
+	static int last_id = -1;
+	unsigned int hlen = ETHER_HDR_SIZE;
+	char addr[MAC_ADDR_STRLEN];
+	u16 type;
+	int id = env_get_id();
+
+	if (len < hlen || !is_valid_ethaddr(frame))
+		return;
+	if (last_id != id) {
+		server = string_to_ip(env_get("serverip"));
+		eth_env_get_enetaddr("serveraddr", saved);
+		last_id = id;
+	}
+	if (!server.s_addr || !memcmp(frame, saved, ETH_ALEN))
+		return;
+
+	type = get_unaligned_be16(frame + hlen - 2);
+	if (type == PROT_VLAN) {
+		hlen += VLAN_HLEN;
+		if (len < hlen)
+			return;
+		type = get_unaligned_be16(frame + hlen - 2);
+	}
+	if (type != PROT_IP || len < hlen + IP_HLEN ||
+	    memcmp(frame + hlen + offsetof(struct ip_hdr, dest),
+		   &server.s_addr, sizeof(server.s_addr)))
+		return;
+
+	/* This is the link address selected by ARP, including via a gateway. */
+	sprintf(addr, "%pM", frame);
+	if (!env_set("serveraddr", addr))
+		memcpy(saved, frame, sizeof(saved));
+}
+
 #if defined(CONFIG_API) || defined(CONFIG_EFI_LOADER)
 void (*push_packet)(void *, int len) = 0;
 #endif
@@ -150,6 +188,8 @@ static err_t net_lwip_tx(struct netif *netif, struct pbuf *p)
 	}
 
 	err = eth_get_ops(udev)->send(udev, pp, plen);
+	if (err >= 0 && CONFIG_IS_ENABLED(KEEP_SERVERADDR))
+		net_lwip_keep_serveraddr(pp, plen);
 	if (err >= 0 && CONFIG_IS_ENABLED(CMD_PCAP))
 		pcap_post(pp, plen, true);
 
test/dm/lwip.c +33 -0
index ba5e6ff1b96..a350e80ab67 100644
@@ -118,6 +118,39 @@ static int dm_test_lwip_decimal(struct unit_test_state *uts)
 }
 DM_TEST(dm_test_lwip_decimal, 0);
 
+#if CONFIG_IS_ENABLED(KEEP_SERVERADDR) && CONFIG_IS_ENABLED(CMD_PING)
+static int lwip_serveraddr_check(struct unit_test_state *uts)
+{
+	struct eth_sandbox_priv *priv;
+	struct udevice *dev;
+	u8 mac[ETH_ALEN];
+
+	ut_assertok(env_set("ethact", "eth@10002000"));
+	ut_assertok(env_set("ipaddr", "1.1.2.1"));
+	ut_assertok(env_set("netmask", "255.255.255.0"));
+	ut_assertok(env_set("serverip", "1.1.2.2"));
+	ut_assertok(env_set("serveraddr", "02:00:00:00:00:33"));
+	ut_assertok(run_command("ping 1.1.2.7", 0));
+	ut_asserteq_str("02:00:00:00:00:33", env_get("serveraddr"));
+	ut_assertok(run_command("ping 1.1.2.2", 0));
+	ut_assertok(uclass_get_device_by_seq(UCLASS_ETH, 0, &dev));
+	priv = dev_get_priv(dev);
+	ut_assert(eth_env_get_enetaddr("serveraddr", mac));
+	ut_asserteq_mem(priv->fake_host_hwaddr, mac, ETH_ALEN);
+	return 0;
+}
+
+static int dm_test_lwip_serveraddr(struct unit_test_state *uts)
+{
+	const char *const vars[] = { "ethact", "ipaddr", "netmask", "serverip",
+				     "serveraddr" };
+
+	return lwip_test_with_env(uts, vars, ARRAY_SIZE(vars), lwip_serveraddr_check);
+}
+
+DM_TEST(dm_test_lwip_serveraddr, UTF_SCAN_FDT);
+#endif
+
 #if CONFIG_IS_ENABLED(PROT_DNS_LWIP)
 struct lwip_test_dns_ctx {
 	unsigned int callbacks;
Signature · 1 line
2.53.0
#5 lore raw

PATCH 5/19 net: cdp: decode advertisements from the complete Ethernet frame

In reply to James Hilliard

An ordinary CDP advertisement uses an IEEE 802.3 length field followed by an LLC/SNAP header. The legacy Ethernet dispatcher consumes that header before calling the CDP parser, which expects to see it. On a short packet Ethernet padding can also be mistaken for more CDP data.

Pass the original frame to the parser and limit the checksum and TLVs to the declared payload length. Read potentially unaligned fields explicitly and publish VLANs only after parsing a complete advertisement. Zero-pad outgoing triggers to the Ethernet minimum without counting the padding in the 802.3 length or CDP checksum.

---
 net/cdp.c | 246 ++++++++++++++++++++++++++------------------------------------
 net/cdp.h |  15 ++--
 net/net.c |  22 ++----
 3 files changed, 118 insertions(+), 165 deletions(-)
net/cdp.c +103 -143
index 300b3d5c409..bcea5887bc2 100644
@@ -9,6 +9,7 @@
  */
 
 #include <net.h>
+#include <asm/unaligned.h>
 
 #include "cdp.h"
 
@@ -29,14 +30,6 @@ const u8 net_cdp_ethaddr[6] = { 0x01, 0x00, 0x0c, 0xcc, 0xcc, 0xcc };
 #define CDP_SYSOBJECT_TLV		0x0015
 #define CDP_MANAGEMENT_ADDRESS_TLV	0x0016
 
-#define CDP_TIMEOUT			250UL	/* one packet every 250ms */
-
-static int cdp_seq;
-static int cdp_ok;
-
-ushort cdp_native_vlan;
-ushort cdp_appliance_vlan;
-
 static const uchar cdp_snap_hdr[8] = {
 	0xAA, 0xAA, 0x03, 0x00, 0x00, 0x0C, 0x20, 0x00 };
 
@@ -98,7 +91,7 @@ static ushort cdp_compute_csum(const uchar *buff, ushort len)
 	return csum;
 }
 
-static int cdp_send_trigger(void)
+int cdp_build_trigger(uchar *frame, const u8 *src)
 {
 	uchar *pkt;
 	ushort *s;
@@ -108,17 +101,17 @@ static int cdp_send_trigger(void)
 	ushort chksum;
 #if	defined(CONFIG_CDP_DEVICE_ID) || defined(CONFIG_CDP_PORT_ID)   || \
 	defined(CONFIG_CDP_VERSION)   || defined(CONFIG_CDP_PLATFORM)
-	char buf[32];
+	char buf[32] = {};
 #endif
 
-	pkt = net_tx_packet;
+	pkt = frame;
 	et = (struct ethernet_hdr *)pkt;
 
 	/* NOTE: trigger sent not on any VLAN */
 
 	/* form ethernet header */
 	memcpy(et->et_dest, net_cdp_ethaddr, 6);
-	memcpy(et->et_src, net_ethaddr, 6);
+	memcpy(et->et_src, src, 6);
 
 	pkt += ETHER_HDR_SIZE;
 
@@ -138,7 +131,7 @@ static int cdp_send_trigger(void)
 #ifdef CONFIG_CDP_DEVICE_ID
 	*s++ = htons(CDP_DEVICE_ID_TLV);
 	*s++ = htons(CONFIG_CDP_DEVICE_ID);
-	sprintf(buf, CONFIG_CDP_DEVICE_ID_PREFIX "%pm", net_ethaddr);
+	snprintf(buf, sizeof(buf), CONFIG_CDP_DEVICE_ID_PREFIX "%pm", src);
 	memcpy((uchar *)s, buf, 16);
 	s += 16 / 2;
 #endif
@@ -146,7 +139,7 @@ static int cdp_send_trigger(void)
 #ifdef CONFIG_CDP_PORT_ID
 	*s++ = htons(CDP_PORT_ID_TLV);
 	memset(buf, 0, sizeof(buf));
-	sprintf(buf, CONFIG_CDP_PORT_ID, eth_get_dev_index());
+	snprintf(buf, sizeof(buf), CONFIG_CDP_PORT_ID, eth_get_dev_index());
 	len = strlen(buf);
 	if (len & 1)	/* make it even */
 		len++;
@@ -158,14 +151,14 @@ static int cdp_send_trigger(void)
 #ifdef CONFIG_CDP_CAPABILITIES
 	*s++ = htons(CDP_CAPABILITIES_TLV);
 	*s++ = htons(8);
-	*(ulong *)s = htonl(CONFIG_CDP_CAPABILITIES);
+	put_unaligned_be32(CONFIG_CDP_CAPABILITIES, s);
 	s += 2;
 #endif
 
 #ifdef CONFIG_CDP_VERSION
 	*s++ = htons(CDP_VERSION_TLV);
 	memset(buf, 0, sizeof(buf));
-	strcpy(buf, CONFIG_CDP_VERSION);
+	strlcpy(buf, CONFIG_CDP_VERSION, sizeof(buf));
 	len = strlen(buf);
 	if (len & 1)	/* make it even */
 		len++;
@@ -177,7 +170,7 @@ static int cdp_send_trigger(void)
 #ifdef CONFIG_CDP_PLATFORM
 	*s++ = htons(CDP_PLATFORM_TLV);
 	memset(buf, 0, sizeof(buf));
-	strcpy(buf, CONFIG_CDP_PLATFORM);
+	strlcpy(buf, CONFIG_CDP_PLATFORM, sizeof(buf));
 	len = strlen(buf);
 	if (len & 1)	/* make it even */
 		len++;
@@ -189,7 +182,7 @@ static int cdp_send_trigger(void)
 #ifdef CONFIG_CDP_TRIGGER
 	*s++ = htons(CDP_TRIGGER_TLV);
 	*s++ = htons(8);
-	*(ulong *)s = htonl(CONFIG_CDP_TRIGGER);
+	put_unaligned_be32(CONFIG_CDP_TRIGGER, s);
 	s += 2;
 #endif
 
@@ -200,165 +193,132 @@ static int cdp_send_trigger(void)
 #endif
 
 	/* length of ethernet packet */
-	len = (uchar *)s - ((uchar *)net_tx_packet + ETHER_HDR_SIZE);
+	len = (uchar *)s - (frame + ETHER_HDR_SIZE);
 	et->et_protlen = htons(len);
 
 	len = ETHER_HDR_SIZE + sizeof(cdp_snap_hdr);
-	chksum = cdp_compute_csum((uchar *)net_tx_packet + len,
-				  (uchar *)s - (net_tx_packet + len));
+	chksum = cdp_compute_csum(frame + len, (uchar *)s - (frame + len));
 	if (chksum == 0)
 		chksum = 0xFFFF;
 	*cp = htons(chksum);
 
-	net_send_packet(net_tx_packet, (uchar *)s - net_tx_packet);
-	return 0;
-}
-
-static void cdp_timeout_handler(void)
-{
-	cdp_seq++;
-
-	if (cdp_seq < 3) {
-		net_set_timeout_handler(CDP_TIMEOUT, cdp_timeout_handler);
-		cdp_send_trigger();
-		return;
+	len = (uchar *)s - frame;
+	if (len < ETH_ZLEN) {
+		memset(frame + len, 0, ETH_ZLEN - len);
+		return ETH_ZLEN;
 	}
-
-	/* if not OK try again */
-	if (!cdp_ok)
-		net_start_again();
-	else
-		net_set_state(NETLOOP_SUCCESS);
+	return len;
 }
 
-void cdp_receive(const uchar *pkt, unsigned len)
+bool cdp_parse_frame(const u8 *frame, unsigned int length, struct cdp_vlans *result)
 {
-	const uchar *t;
-	const ushort *ss;
-	ushort type, tlen;
-	ushort vlan, nvlan;
-
-	/* minimum size? */
-	if (len < sizeof(cdp_snap_hdr) + 4)
-		goto pkt_short;
-
-	/* check for valid CDP SNAP header */
-	if (memcmp(pkt, cdp_snap_hdr, sizeof(cdp_snap_hdr)) != 0)
-		return;
-
+	struct cdp_vlans vlans = { .native = -1, .appliance = -1 };
+	unsigned int hlen = ETHER_HDR_SIZE, len;
+	const u8 *pkt;
+
+	if (length < hlen || memcmp(frame, net_cdp_ethaddr, ETH_ALEN))
+		return false;
+	len = get_unaligned_be16(frame + hlen - 2);
+	if (len == PROT_VLAN) {
+		hlen += VLAN_HLEN;
+		if (length < hlen)
+			return false;
+		len = get_unaligned_be16(frame + hlen - 2);
+	}
+	/* Use the 802.3 length field: Ethernet padding isn't part of CDP. */
+	if (len > ETH_DATA_LEN || len > length - hlen || len < sizeof(cdp_snap_hdr) + 4)
+		return false;
+	pkt = frame + hlen;
+	if (memcmp(pkt, cdp_snap_hdr, sizeof(cdp_snap_hdr)))
+		return false;
 	pkt += sizeof(cdp_snap_hdr);
 	len -= sizeof(cdp_snap_hdr);
-
-	/* Version of CDP protocol must be >= 2 and TTL != 0 */
-	if (pkt[0] < 0x02 || pkt[1] == 0)
-		return;
-
-	/*
-	 * if version is greater than 0x02 maybe we'll have a problem;
-	 * output a warning
-	 */
-	if (pkt[0] != 0x02)
-		printf("**WARNING: CDP packet received with a protocol version "
-				"%d > 2\n", pkt[0] & 0xff);
-
-	if (cdp_compute_csum(pkt, len) != 0)
-		return;
+	if (pkt[0] < 2 || !pkt[1] || cdp_compute_csum(pkt, len))
+		return false;
 
 	pkt += 4;
 	len -= 4;
+	while (len) {
+		unsigned int tlv, size, type;
+		const u8 *value;
 
-	vlan = htons(-1);
-	nvlan = htons(-1);
-	while (len > 0) {
 		if (len < 4)
-			goto pkt_short;
-
-		ss = (const ushort *)pkt;
-		type = ntohs(ss[0]);
-		tlen = ntohs(ss[1]);
-		/*
-		 * tlen includes the 4-byte TLV header, so it must be at
-		 * least 4.  Without this check a crafted tlen < 4 makes the
-		 * "tlen -= 4" below underflow (tlen is a ushort), and a tlen
-		 * of 0 also fails to advance pkt/len, hanging the loop.
-		 */
-		if (tlen < 4 || tlen > len)
-			goto pkt_short;
-
-		pkt += tlen;
-		len -= tlen;
-
-		ss += 2;	/* point ss to the data of the TLV */
-		tlen -= 4;
-
-		switch (type) {
-		case CDP_DEVICE_ID_TLV:
-			break;
-		case CDP_ADDRESS_TLV:
-			break;
-		case CDP_PORT_ID_TLV:
-			break;
-		case CDP_CAPABILITIES_TLV:
-			break;
-		case CDP_VERSION_TLV:
-			break;
-		case CDP_PLATFORM_TLV:
-			break;
-		case CDP_NATIVE_VLAN_TLV:
-			nvlan = *ss;
-			break;
-		case CDP_APPLIANCE_VLAN_TLV:
-			t = (const uchar *)ss;
-			while (tlen > 0) {
-				if (tlen < 3)
-					goto pkt_short;
-
-				ss = (const ushort *)(t + 1);
-
+			return false;
+		type = get_unaligned_be16(pkt);
+		tlv = get_unaligned_be16(pkt + 2);
+		if (tlv < 4 || tlv > len)
+			return false;
+		value = pkt + 4;
+		size = tlv - 4;
+		if (type == CDP_NATIVE_VLAN_TLV) {
+			if (size != 2)
+				return false;
+			vlans.native = get_unaligned_be16(value);
+		} else if (type == CDP_APPLIANCE_VLAN_TLV) {
+			if (size % 3)
+				return false;
+			while (size) {
 #ifdef CONFIG_CDP_APPLIANCE_VLAN_TYPE
-				if (t[0] == CONFIG_CDP_APPLIANCE_VLAN_TYPE)
-					vlan = *ss;
-#else
-				/* XXX will this work; dunno */
-				vlan = ntohs(*ss);
+				if (value[0] == CONFIG_CDP_APPLIANCE_VLAN_TYPE)
 #endif
-				t += 3; tlen -= 3;
+					vlans.appliance = get_unaligned_be16(value + 1);
+				value += 3;
+				size -= 3;
 			}
-			break;
-		case CDP_TRIGGER_TLV:
-			break;
-		case CDP_POWER_CONSUMPTION_TLV:
-			break;
-		case CDP_SYSNAME_TLV:
-			break;
-		case CDP_SYSOBJECT_TLV:
-			break;
-		case CDP_MANAGEMENT_ADDRESS_TLV:
-			break;
 		}
+		pkt += tlv;
+		len -= tlv;
 	}
+	if (vlans.native > VLAN_VID_MASK || vlans.appliance > VLAN_VID_MASK)
+		return false;
+	*result = vlans;
+	return true;
+}
 
-	cdp_appliance_vlan = vlan;
-	cdp_native_vlan = nvlan;
+#ifdef CONFIG_NET_LEGACY
+static int cdp_seq;
+static bool cdp_ok;
+ushort cdp_native_vlan;
+ushort cdp_appliance_vlan;
 
-	cdp_ok = 1;
-	return;
+static void cdp_send_trigger(void)
+{
+	int len = cdp_build_trigger(net_tx_packet, net_ethaddr);
 
-pkt_short:
-	printf("** CDP packet is too short\n");
-	return;
+	net_send_packet(net_tx_packet, len);
+}
+
+static void cdp_timeout_handler(void)
+{
+	if (++cdp_seq < 3) {
+		net_set_timeout_handler(CDP_TIMEOUT, cdp_timeout_handler);
+		cdp_send_trigger();
+	} else if (!cdp_ok) {
+		net_start_again();
+	} else {
+		net_set_state(NETLOOP_SUCCESS);
+	}
+}
+
+void cdp_receive(const uchar *frame, unsigned int len)
+{
+	struct cdp_vlans vlans;
+
+	if (!cdp_parse_frame(frame, len, &vlans))
+		return;
+	cdp_native_vlan = htons(vlans.native);
+	cdp_appliance_vlan = htons(vlans.appliance);
+	cdp_ok = true;
 }
 
 void cdp_start(void)
 {
 	printf("Using %s device\n", eth_get_name());
 	cdp_seq = 0;
-	cdp_ok = 0;
-
+	cdp_ok = false;
 	cdp_native_vlan = htons(-1);
 	cdp_appliance_vlan = htons(-1);
-
 	net_set_timeout_handler(CDP_TIMEOUT, cdp_timeout_handler);
-
 	cdp_send_trigger();
 }
+#endif
net/cdp.h +10 -5
index 606fabba957..e53d1f81e4a 100644
@@ -8,14 +8,19 @@
  *	Copyright 2000-2002 Wolfgang Denk, [email protected]
  */
 
-#if defined(CONFIG_CMD_CDP)
-
 #ifndef __CDP_H__
 #define __CDP_H__
 
+#define CDP_TIMEOUT 250 /* milliseconds */
+struct cdp_vlans {
+	int native;
+	int appliance;
+};
+
+extern const u8 net_cdp_ethaddr[6];
+int cdp_build_trigger(uchar *frame, const u8 *src);
+bool cdp_parse_frame(const u8 *frame, unsigned int len, struct cdp_vlans *result);
 void cdp_start(void);
-/* Process a received CDP packet */
-void cdp_receive(const uchar *pkt, unsigned len);
+void cdp_receive(const uchar *frame, unsigned int len);
 
 #endif /* __CDP_H__ */
-#endif
net/net.c +5 -17
index f46cfee7129..0fa6178023b 100644
@@ -1180,9 +1180,6 @@ void net_process_received_packet(uchar *in_packet, int len)
 	struct in_addr dst_ip;
 	struct in_addr src_ip;
 	int eth_proto;
-#if defined(CONFIG_CMD_CDP)
-	int iscdp;
-#endif
 	ushort cti = 0, vlanid = VLAN_NONE, myvlanid, mynvlanid;
 
 	debug_cond(DEBUG_NET_PKT, "packet received\n");
@@ -1209,8 +1206,10 @@ void net_process_received_packet(uchar *in_packet, int len)
 #endif
 
 #if defined(CONFIG_CMD_CDP)
-	/* keep track if packet is CDP */
-	iscdp = is_cdp_packet(et->et_dest);
+	if (is_cdp_packet(et->et_dest)) {
+		cdp_receive(in_packet, len);
+		return;
+	}
 #endif
 
 	myvlanid = ntohs(net_our_vlan);
@@ -1248,11 +1247,7 @@ void net_process_received_packet(uchar *in_packet, int len)
 			return;
 
 		/* if no VLAN active */
-		if ((ntohs(net_our_vlan) & VLAN_IDMASK) == VLAN_NONE
-#if defined(CONFIG_CMD_CDP)
-				&& iscdp == 0
-#endif
-				)
+		if ((ntohs(net_our_vlan) & VLAN_IDMASK) == VLAN_NONE)
 			return;
 
 		cti = ntohs(vet->vet_tag);
@@ -1265,13 +1260,6 @@ void net_process_received_packet(uchar *in_packet, int len)
 
 	debug_cond(DEBUG_NET_PKT, "Receive from protocol 0x%x\n", eth_proto);
 
-#if defined(CONFIG_CMD_CDP)
-	if (iscdp) {
-		cdp_receive((uchar *)ip, len);
-		return;
-	}
-#endif
-
 	if ((myvlanid & VLAN_IDMASK) != VLAN_NONE) {
 		if (vlanid == VLAN_NONE)
 			vlanid = (mynvlanid & VLAN_IDMASK);
Signature · 1 line
2.53.0
#6 lore raw

PATCH 6/19 net: cdp: calculate checksums from network-order bytes

In reply to James Hilliard

Switches can send CDP advertisements with an odd payload length. The final octet contributes to the low byte of the checksum word, even though complete words use network byte order. Summing native-endian words gives the wrong result for these packets on little-endian systems.

Read the words in network order and apply CDP's one's-complement correction when the final octet has its high bit set. Use the same calculation for received advertisements and outgoing triggers.

---
 net/cdp.c | 65 +++++++++++++++------------------------------------------------
 1 file changed, 15 insertions(+), 50 deletions(-)
net/cdp.c +15 -50
index bcea5887bc2..57c3080d121 100644
@@ -35,60 +35,25 @@ static const uchar cdp_snap_hdr[8] = {
 
 static ushort cdp_compute_csum(const uchar *buff, ushort len)
 {
-	ushort csum;
-	int     odd;
-	ulong   result = 0;
-	ushort  leftover;
-	ushort *p;
-
-	if (len > 0) {
-		odd = 1 & (ulong)buff;
-		if (odd) {
-			result = *buff << 8;
-			len--;
-			buff++;
-		}
-		while (len > 1) {
-			p = (ushort *)buff;
-			result += *p++;
-			buff = (uchar *)p;
-			if (result & 0x80000000)
-				result = (result & 0xFFFF) + (result >> 16);
-			len -= 2;
-		}
-		if (len) {
-			leftover = (signed short)(*(const signed char *)buff);
-			/*
-			 * CISCO SUCKS big time! (and blows too):
-			 * CDP uses the IP checksum algorithm with a twist;
-			 * for the last byte it *sign* extends and sums.
-			 */
-			result = (result & 0xffff0000) |
-				 ((result + leftover) & 0x0000ffff);
-		}
-		while (result >> 16)
-			result = (result & 0xFFFF) + (result >> 16);
+	ulong result = 0;
 
-		if (odd)
-			result = ((result >> 8) & 0xff) |
-				 ((result & 0xff) << 8);
+	while (len > 1) {
+		result += get_unaligned_be16(buff);
+		buff += 2;
+		len -= 2;
 	}
+	if (len) {
+		u16 last = *buff;
 
-	/* add up 16-bit and 17-bit words for 17+c bits */
-	result = (result & 0xffff) + (result >> 16);
-	/* add up 16-bit and 2-bit for 16+c bit */
-	result = (result & 0xffff) + (result >> 16);
-	/* add up carry.. */
-	result = (result & 0xffff) + (result >> 16);
-
-	/* negate */
-	csum = ~(ushort)result;
-
-	/* run time endian detection */
-	if (csum != htons(csum))	/* little endian */
-		csum = htons(csum);
+		/* CDP treats an odd final octet as a signed one's-complement word. */
+		if (last & 0x80)
+			last = 0xff00 | (last - 1);
+		result += last;
+	}
+	while (result >> 16)
+		result = (result & 0xffff) + (result >> 16);
 
-	return csum;
+	return ~(ushort)result;
 }
 
 int cdp_build_trigger(uchar *frame, const u8 *src)
Signature · 1 line
2.53.0
#7 lore raw

PATCH 7/19 net: lwip: discover VLAN settings with CDP

In reply to James Hilliard

A switch can advertise the VLAN to use for network boot through CDP. Send the discovery trigger untagged and listen on the CDP multicast address before applying the advertised VLAN settings. On timeout, use the common interface retry policy.

When an advertisement specifies no VLAN, delete the corresponding vlan or nvlan variable. Both variables take decimal values, and leaving the old value in place would reuse a VLAN the switch no longer advertises.

Check tagged and untagged replies, no-VLAN handling, link padding and unaligned VLAN TLVs in sandbox.

---
 cmd/Kconfig         |   4 --
 net/Makefile        |   2 +-
 net/cdp.h           |   2 +
 net/lwip/Makefile   |   1 +
 net/lwip/cdp.c      |  98 +++++++++++++++++++++++++++++++++++++++++++
 net/lwip/net-lwip.c |   3 ++
 test/dm/Makefile    |   3 ++
 test/dm/lwip_cdp.c  | 117 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 8 files changed, 225 insertions(+), 5 deletions(-)
cmd/Kconfig +0 -4
index 5c31fb00013..0fde969ad98 100644
@@ -2191,8 +2191,6 @@ config CMD_PING6
 	help
 	  Send ICMPv6 ECHO_REQUEST to network host
 
-if NET_LEGACY
-
 config CMD_CDP
 	bool "cdp"
 	help
@@ -2200,8 +2198,6 @@ config CMD_CDP
 	  and to retrieve configuration data including the VLAN id using the
 	  proprietary Cisco Discovery Protocol (CDP).
 
-endif  # if NET_LEGACY
-
 config CMD_LINK_LOCAL
 	bool "linklocal"
 	depends on (LIB_RAND || LIB_HW_RAND)
net/Makefile +1 -1
index 5a04852a4de..fb06ccdb38d 100644
@@ -9,7 +9,6 @@ ifeq ($(CONFIG_NET_LEGACY),y)
 
 obj-$(CONFIG_NET_LEGACY)      += arp.o
 obj-$(CONFIG_CMD_BOOTP) += bootp.o
-obj-$(CONFIG_CMD_CDP)  += cdp.o
 obj-$(CONFIG_DNS)  += dns.o
 obj-$(CONFIG_CMD_LINK_LOCAL) += link_local.o
 obj-$(CONFIG_IPV6)     += ndisc.o
@@ -38,6 +37,7 @@ CFLAGS_eth_common.o += -Wno-format-extra-args
 endif
 
 ifeq ($(CONFIG_NET),y)
+obj-$(CONFIG_CMD_CDP) += cdp.o
 obj-$(CONFIG_DM_DSA)   += dsa-uclass.o
 obj-$(CONFIG_$(PHASE_)DM_ETH) += eth-uclass.o
 obj-$(CONFIG_$(PHASE_)BOOTDEV_ETH) += eth_bootdev.o
net/cdp.h +2 -0
index e53d1f81e4a..1e274390422 100644
@@ -20,6 +20,8 @@ struct cdp_vlans {
 extern const u8 net_cdp_ethaddr[6];
 int cdp_build_trigger(uchar *frame, const u8 *src);
 bool cdp_parse_frame(const u8 *frame, unsigned int len, struct cdp_vlans *result);
+struct netif;
+void net_lwip_cdp_receive(struct netif *netif, const u8 *frame, unsigned int len);
 void cdp_start(void);
 void cdp_receive(const uchar *frame, unsigned int len);
 
net/lwip/Makefile +1 -0
index d72c5237410..946abac8afd 100644
@@ -5,6 +5,7 @@ ifneq ($(filter y,$(CONFIG_CMD_BOOTP) $(CONFIG_CMD_DHCP)),)
 obj-y += dhcp.o
 endif
 obj-$(CONFIG_DNS) += dns.o
+obj-$(CONFIG_$(PHASE_)CMD_CDP) += cdp.o
 obj-$(CONFIG_CMD_DHCP6) += dhcp6.o
 obj-$(CONFIG_PROT_ICMP_LWIP) += icmp_unreach.o
 obj-$(CONFIG_CMD_NFS) += nfs.o
net/lwip/cdp.c new file +98 -0

98 lines not on this page · raw message

net/lwip/net-lwip.c +3 -0
index 8b55c9646ef..3a9a5a42716 100644
@@ -32,6 +32,7 @@
 #include <timer.h>
 #include <time.h>
 #include <u-boot/schedule.h>
+#include "../cdp.h"
 
 /* xx:xx:xx:xx:xx:xx\0 */
 #define MAC_ADDR_STRLEN 18
@@ -1110,6 +1111,8 @@ static int net_lwip_rx(struct udevice *udev, struct netif *netif)
 		flags = 0;
 
 		if (len > 0) {
+			if (CONFIG_IS_ENABLED(CMD_CDP))
+				net_lwip_cdp_receive(netif, packet, len);
 			if (CONFIG_IS_ENABLED(CMD_PCAP))
 				pcap_post(packet, len, false);
 
test/dm/Makefile +3 -0
index cba32cc3dd3..6289a873a4e 100644
@@ -64,6 +64,9 @@ obj-y += lwip_dhcp_options.o
 obj-y += lwip_dhcp_proxy.o
 obj-y += lwip_dhcp_policy.o
 endif
+ifeq ($(CONFIG_NET_LWIP)$(CONFIG_CMD_CDP),yy)
+obj-y += lwip_cdp.o
+endif
 ifeq ($(CONFIG_NET_LWIP)$(CONFIG_CMD_BOOTP),yy)
 obj-y += lwip_bootp.o
 endif
test/dm/lwip_cdp.c new file +117 -0

117 lines not on this page · raw message

Signature · 1 line
2.53.0
#8 lore raw

PATCH 8/19 net: lwip: bind NFS requests to a reserved UDP port

In reply to James Hilliard

Linux NFS exports normally reject AUTH_SYS requests from unprivileged source ports. The lwIP client copies a port number initialized only by the legacy stack, leaving lwIP to choose an ephemeral port on first use.

Bind a free reserved port before sending the first RPC and have the sandbox server enforce the same restriction.

---
 net/lwip/nfs.c      | 17 +++++++++++++++--
 test/dm/lwip_ipv6.c |  3 +++
 2 files changed, 18 insertions(+), 2 deletions(-)
net/lwip/nfs.c +15 -2
index a389aa6412f..6b00c6ab3fd 100644
@@ -75,11 +75,24 @@ exitfree:
 
 static int nfs_udp_init(struct nfs_ctx *ctx)
 {
-	ctx->pcb = udp_new_ip_type(IP_GET_TYPE(&ctx->nfs_server));
+	u8 type = IP_GET_TYPE(&ctx->nfs_server);
+	err_t err;
+
+	ctx->pcb = udp_new_ip_type(type);
 	if (!ctx->pcb)
 		return -ENOMEM;
 
-	ctx->pcb->local_port = nfs_our_port;
+	/* NFS servers normally require AUTH_SYS requests from a reserved port. */
+	for (int port = 1023; port >= 512; port--) {
+		err = udp_bind(ctx->pcb, IP46_ADDR_ANY(type), port);
+		if (err != ERR_USE)
+			break;
+	}
+	if (err) {
+		udp_remove(ctx->pcb);
+		ctx->pcb = NULL;
+		return -EADDRINUSE;
+	}
 	udp_recv(ctx->pcb, nfs_recv, ctx);
 
 	return 0;
test/dm/lwip_ipv6.c +3 -0
index bd68ac0e356..193f4ee0b5d 100644
@@ -661,6 +661,9 @@ static int ip6_test_rpc_reply(struct udevice *dev, struct ip6_hdr *ip)
 	const char *uaddr;
 	int ret;
 
+	/* A normal NFS export requires a reserved client port. */
+	if (ntohs(udp->src) >= 1024)
+		return -EACCES;
 	memcpy(reply, query, 4);
 	reply[1] = htonl(1);
 	switch (++test->rpc_requests) {
Signature · 1 line
2.53.0
#9 lore raw

PATCH 9/19 net: lwip: honor autostart after an NFS download

In reply to James Hilliard

With autostart enabled, the nfs command leaves a downloaded image in memory instead of booting it. Use the common boot helper after releasing the network and only for a successful, nonempty download. Publish the size and address even for an empty file, so scripts cannot mistake an earlier image for the latest download.

Use the current bootfile when no filename is supplied. Let DHCP pass its boot server directly to the download operation; the initiating command handles autostart.

---
 include/net-lwip.h  |  1 +
 net/lwip/dhcp.c     | 43 ++++++++++++-------------------------------
 net/lwip/nfs.c      | 28 ++++++++++++++++++++--------
 test/dm/lwip_ipv6.c | 29 ++++++++++++++++++++++++++++-
 4 files changed, 61 insertions(+), 40 deletions(-)
include/net-lwip.h +1 -0
index 2f66732fd85..8b64f32b913 100644
@@ -118,6 +118,7 @@ bool wget_validate_uri(char *uri);
 int do_bootp(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
 int do_dns(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
 int do_nfs(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
+int net_lwip_nfs_download(int argc, char *const argv[], const char *bootserver);
 int do_tftpput(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
 int do_tftpsrv(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
 int do_wget(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[]);
net/lwip/dhcp.c +12 -31

56 lines not on this page · raw message

net/lwip/nfs.c +20 -8
index 6b00c6ab3fd..3ecd259717a 100644
@@ -123,6 +123,7 @@ static int nfs_loop(struct net_lwip_ctx *net, ulong addr, char *fname,
 	ip_addr_set(&sess_ctx.nfs_server, &srvip);
 	nfs_use_rpcbind = IP_IS_V6(&srvip);
 
+	net_boot_file_size = 0;
 	nfs_download_state = NETLOOP_FAIL;
 	net_set_state(NETLOOP_FAIL);
 
@@ -182,13 +183,12 @@ static int nfs_loop(struct net_lwip_ctx *net, ulong addr, char *fname,
 
 	if (net_state == NETLOOP_SUCCESS) {
 		ret = 0;
-		if (net_boot_file_size > 0) {
+		if (net_boot_file_size)
 			printf("Bytes transferred = %u (%x hex)\n",
 			       net_boot_file_size, net_boot_file_size);
-			env_set_hex("filesize", net_boot_file_size);
-			env_set_hex("fileaddr", image_load_addr);
-		}
-
+		if (env_set_hex("filesize", net_boot_file_size) ||
+		    env_set_hex("fileaddr", image_load_addr))
+			ret = -EIO;
 	} else {
 		debug("%s: NFS loop failed\n", __func__);
 		ret = -1;
@@ -197,14 +197,14 @@ static int nfs_loop(struct net_lwip_ctx *net, ulong addr, char *fname,
 	return ret;
 }
 
-int do_nfs(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
+int net_lwip_nfs_download(int argc, char *const argv[], const char *bootserver)
 {
 	struct net_lwip_ctx net = {};
 	int ret = CMD_RET_SUCCESS;
 	const char *arg = NULL;
 	char *arg_copy = NULL;
 	char *fname = NULL;
-	char *server_ip = NULL;
+	const char *server_ip = NULL;
 	char *end;
 	ip_addr_t srvip;
 	ulong laddr;
@@ -248,7 +248,7 @@ int do_nfs(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
 	}
 
 	if (!arg)
-		arg = *net_boot_file_name ? net_boot_file_name : env_get("bootfile");
+		arg = env_get("bootfile");
 
 	if (arg && *arg) {
 		arg_copy = strdup(arg);
@@ -281,6 +281,8 @@ int do_nfs(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
 
 	if (!server_ip || !*server_ip)
 		server_ip = env_get("nfsserverip");
+	if (!server_ip || !*server_ip)
+		server_ip = bootserver;
 	if (!server_ip || !*server_ip)
 		server_ip = env_get(ipv6 ? "serverip6" : "serverip");
 	if (!server_ip || !*server_ip) {
@@ -315,3 +317,13 @@ out:
 	free(arg_copy);
 	return ret;
 }
+
+int do_nfs(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
+{
+	int ret = net_lwip_nfs_download(argc, argv, NULL);
+
+	if (ret || !net_boot_file_size)
+		return ret;
+
+	return bootm_maybe_autostart(cmdtp, argv[0]);
+}
test/dm/lwip_ipv6.c +28 -1
index 193f4ee0b5d..5eb48adb92c 100644
@@ -105,6 +105,7 @@ struct ip6_test {
 	bool advertise;
 	bool cancel;
 	bool fragment;
+	bool empty_nfs;
 };
 
 static u16 ip6_test_checksum(struct ip6_hdr *ip, void *data, unsigned int len)
@@ -688,6 +689,8 @@ static int ip6_test_rpc_reply(struct udevice *dev, struct ip6_hdr *ip)
 		reply[7] = htonl(32);
 		break;
 	case 5:
+		if (test->empty_nfs)
+			break;
 		reply[8] = htonl(9);
 		reply[9] = htonl(1);
 		reply[10] = htonl(9);
@@ -1448,6 +1451,22 @@ static int ip6_test_apps(struct unit_test_state *uts, struct ip6_test *test)
 	ut_assertok(run_command("nfs 2100000 [fe80::2]:/ipv6.bin", 0));
 	ut_asserteq_mem("ipv6-nfs!", data, 9);
 	ut_asserteq(7, test->rpc_requests);
+	if (IS_ENABLED(CONFIG_CMD_BOOTM)) {
+		ut_assertok(env_set("autostart", "yes"));
+		test->rpc_requests = 0;
+		/* The download succeeds but its contents are not a boot image. */
+		ut_asserteq(CMD_RET_FAILURE,
+			    run_command("nfs 2100000 [fe80::2]:/ipv6.bin", 0));
+		ut_asserteq(7, test->rpc_requests);
+	}
+	test->rpc_requests = 0;
+	test->empty_nfs = true;
+	ut_assertok(env_set("bootfile", "[fe80::2]:/empty"));
+	ut_assertok(run_command("nfs 2200000", 0));
+	ut_asserteq(6, test->rpc_requests);
+	ut_asserteq(0, net_boot_file_size);
+	ut_asserteq_str("0", env_get("filesize"));
+	ut_asserteq_str("2200000", env_get("fileaddr"));
 #endif
 #if CONFIG_IS_ENABLED(NETCONSOLE)
 	{
@@ -1477,7 +1496,15 @@ static int ip6_test_apps(struct unit_test_state *uts, struct ip6_test *test)
 
 static int dm_test_lwip_ip6_apps(struct unit_test_state *uts)
 {
-	return ip6_test_run(uts, ip6_test_apps);
+	char saved[sizeof(net_boot_file_name)];
+	int ret;
+
+	memcpy(saved, net_boot_file_name, sizeof(saved));
+	/* A previous DHCP lease must not override the current bootfile. */
+	strcpy(net_boot_file_name, "[invalid]:/old");
+	ret = ip6_test_run(uts, ip6_test_apps);
+	memcpy(net_boot_file_name, saved, sizeof(saved));
+	return ret;
 }
 DM_TEST(dm_test_lwip_ip6_apps, UTF_SCAN_FDT);
 
Signature · 1 line
2.53.0
#10 lore raw

PATCH 10/19 net: lwip: restart NFS after an RPC timeout

In reply to James Hilliard

When an NFS server stops answering, lwIP exhausts the RPC retries and returns to the prompt even if netretry allows trying another interface. Restart the network only after this timeout, once the old UDP callback has been removed. Re-resolve the server on the selected interface and start a fresh transfer so the previous attempt cannot inflate its size.

A timeout can occur after the first attempt mounts the export. Reset mount ownership at the start of each attempt so a mount failure on a replacement interface does not send UMOUNTALL using stale state.

---
 net/lwip/nfs.c      | 32 ++++++++++++++------
 net/nfs-common.c    |  5 ++++
 net/nfs-common.h    |  1 +
 test/dm/lwip_ipv6.c | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 113 insertions(+), 9 deletions(-)
net/lwip/nfs.c +23 -9
index 3ecd259717a..1f6f84fd24f 100644
@@ -117,9 +117,6 @@ static int nfs_loop(struct net_lwip_ctx *net, ulong addr, char *fname,
 {
 	int ret;
 
-	ret = net_lwip_prepare_addr(net, &srvip);
-	if (ret)
-		return ret;
 	ip_addr_set(&sess_ctx.nfs_server, &srvip);
 	nfs_use_rpcbind = IP_IS_V6(&srvip);
 
@@ -150,6 +147,8 @@ static int nfs_loop(struct net_lwip_ctx *net, ulong addr, char *fname,
 	image_load_addr = addr;
 
 	nfs_timeout_count = 0;
+	choosen_nfs_version = NFS_V3;
+	nfs_reset_mount_state();
 	nfs_state = STATE_PRCLOOKUP_PROG_MOUNT_REQ;
 
 	ret = nfs_udp_init(&sess_ctx);
@@ -173,8 +172,10 @@ static int nfs_loop(struct net_lwip_ctx *net, ulong addr, char *fname,
 			break;
 		}
 
-		if (nfs_timeout_check())
+		if (nfs_timeout_check()) {
+			ret = -EAGAIN;
 			break;
+		}
 	} while (true);
 	debug("%s: Loop exit at %lu\n", __func__, get_timer(0));
 
@@ -189,7 +190,7 @@ static int nfs_loop(struct net_lwip_ctx *net, ulong addr, char *fname,
 		if (env_set_hex("filesize", net_boot_file_size) ||
 		    env_set_hex("fileaddr", image_load_addr))
 			ret = -EIO;
-	} else {
+	} else if (!ret) {
 		debug("%s: NFS loop failed\n", __func__);
 		ret = -1;
 	}
@@ -308,10 +309,23 @@ int net_lwip_nfs_download(int argc, char *const argv[], const char *bootserver)
 		goto out;
 	}
 
-	if (net_lwip_resolve(&net, server_ip, &srvip,
-			     ipv6 ? IPADDR_TYPE_V6 : IPADDR_TYPE_ANY) ||
-	    nfs_loop(&net, laddr, fname, srvip) < 0)
-		ret = CMD_RET_FAILURE;
+	net_try_count = 1;
+	net_restart_wrap = 0;
+	do {
+		ret = net_lwip_resolve(&net, server_ip, &srvip,
+				       ipv6 ? IPADDR_TYPE_V6 : IPADDR_TYPE_ANY);
+		if (ret)
+			break;
+		ret = net_lwip_prepare_addr(&net, &srvip);
+		if (ret)
+			break;
+		ret = nfs_loop(&net, laddr, fname, srvip);
+		if (ret != -EAGAIN)
+			break;
+		puts("\nRetry count exceeded; starting again\n");
+		ret = net_lwip_restart(&net);
+	} while (!ret);
+	ret = ret ? CMD_RET_FAILURE : CMD_RET_SUCCESS;
 out:
 	net_lwip_stop(&net);
 	free(arg_copy);
net/nfs-common.c +5 -0
index a82b3b795a0..06216cc3bcc 100644
@@ -72,6 +72,11 @@ const ulong nfs_timeout = CONFIG_NFS_TIMEOUT;
 
 enum nfs_version choosen_nfs_version = NFS_V3;
 
+void nfs_reset_mount_state(void)
+{
+	fs_mounted = 0;
+}
+
 static inline int store_block(uchar *src, unsigned int offset, unsigned int len)
 {
 	ulong newsize = offset + len;
net/nfs-common.h +1 -0
index b48e035c6a8..b61ba833eea 100644
@@ -111,6 +111,7 @@ void rpc_req_common(int rpc_prog, int rpc_proc, uint32_t *data, int datalen,
 		    uchar *txbuff, int *pktlen, int *sport);
 void nfs_send(void);
 void nfs_pkt_recv(uchar *pkt, unsigned int len);
+void nfs_reset_mount_state(void);
 
 extern enum nfs_version choosen_nfs_version;
 
test/dm/lwip_ipv6.c +84 -0

114 lines not on this page · raw message

Signature · 1 line
2.53.0
#11 lore raw

PATCH 11/19 net: lwip: support network boot with reverse ARP

In reply to James Hilliard

Acquire an IPv4 address for the current Ethernet interface from a RARP reply addressed to its MAC. Publish the address and boot server, then hand the same interface to TFTP. Preserve a configured server, honor autoload and autostart, and use the shared runtime retry policy.

Use the Ethernet output and unknown-protocol input paths for RARP so normal IP traffic continues to reach other clients. Update the primary IPv4 address used by PXE and EFI as well as ipaddr. Exercise acquisition and a TFTP download to the configured server in sandbox.

---
 cmd/Kconfig                  |   4 +-
 lib/lwip/u-boot/lwip-hooks.h |   7 ++
 net/lwip/Makefile            |   1 +
 net/lwip/rarp.c              | 181 +++++++++++++++++++++++++++++++++++++++++++
 test/dm/Makefile             |   3 +
 test/dm/lwip_rarp.c          | 139 +++++++++++++++++++++++++++++++++
 6 files changed, 333 insertions(+), 2 deletions(-)
cmd/Kconfig +2 -2
index 0fde969ad98..4fb866f3cf6 100644
@@ -2167,13 +2167,13 @@ config BOOTP_RANDOM_XID
 	  Selecting this will allow for a random transaction ID to get
 	  selected for each BOOTP/DHCPv4 exchange.
 
+endif
+
 config CMD_RARP
 	bool "rarpboot"
 	help
 	  Boot image via network using RARP/TFTP protocol
 
-endif
-
 config SYS_DISABLE_AUTOLOAD
 	bool "Disable automatically loading files over the network"
 	depends on CMD_BOOTP || CMD_DHCP || CMD_NFS || CMD_RARP
lib/lwip/u-boot/lwip-hooks.h +7 -0
index f665e80e869..396a0606b44 100644
@@ -11,6 +11,13 @@ int net_lwip_vlan_id(void);
 #include "lwip-dhcp.h"
 #endif
 
+#if CONFIG_IS_ENABLED(CMD_RARP)
+struct netif;
+struct pbuf;
+int net_lwip_rarp_receive(struct pbuf *p, struct netif *netif);
+#define LWIP_HOOK_UNKNOWN_ETH_PROTOCOL net_lwip_rarp_receive
+#endif
+
 #if LWIP_IPV6
 #include <lwip/ip6_addr.h>
 
net/lwip/Makefile +1 -0
index 946abac8afd..ac17ebe87f2 100644
@@ -9,6 +9,7 @@ obj-$(CONFIG_$(PHASE_)CMD_CDP) += cdp.o
 obj-$(CONFIG_CMD_DHCP6) += dhcp6.o
 obj-$(CONFIG_PROT_ICMP_LWIP) += icmp_unreach.o
 obj-$(CONFIG_CMD_NFS) += nfs.o
+obj-$(CONFIG_$(PHASE_)CMD_RARP) += rarp.o
 obj-$(CONFIG_CMD_TFTPBOOT) += tftp.o
 obj-$(CONFIG_WGET) += wget.o
 obj-$(CONFIG_CMD_WOL) += wol.o
net/lwip/rarp.c new file +181 -0

181 lines not on this page · raw message

test/dm/Makefile +3 -0
index 6289a873a4e..5448be2267a 100644
@@ -70,6 +70,9 @@ endif
 ifeq ($(CONFIG_NET_LWIP)$(CONFIG_CMD_BOOTP),yy)
 obj-y += lwip_bootp.o
 endif
+ifeq ($(CONFIG_NET_LWIP)$(CONFIG_CMD_RARP),yy)
+obj-y += lwip_rarp.o
+endif
 ifeq ($(CONFIG_NET_LWIP)$(CONFIG_CMD_TFTPBOOT),yy)
 obj-y += lwip_tftp.o
 obj-y += lwip_net_loop.o
test/dm/lwip_rarp.c new file +139 -0

139 lines not on this page · raw message

Signature · 1 line
2.53.0
#12 lore raw

PATCH 12/19 boot: pxe: allow boot without the DHCP command

In reply to James Hilliard

Enabling the PXE boot method on a BOOTP-only build prevents U-Boot from linking: dhcp_run() is only built with CMD_DHCP. PXE can already fetch its configuration over TFTP when the addresses are set.

Only acquire an address through DHCP when the command is enabled. Otherwise use the current network settings.

Fixes 1915fdcc2126 ("boot: call dhcp_run in pxe bootmethod")
---
 boot/bootmeth_pxe.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
boot/bootmeth_pxe.c +5 -3
index 27f254b9f93..f6e95b03fad 100644
@@ -54,9 +54,11 @@ static int extlinux_pxe_read_bootflow(struct udevice *dev,
 		return log_msg_ret("pxeb", -EPERM);
 	addr = simple_strtoul(addr_str, NULL, 16);
 
-	ret = dhcp_run(addr, NULL, false);
-	if (ret)
-		return log_msg_ret("dhc", ret);
+	if (IS_ENABLED(CONFIG_CMD_DHCP)) {
+		ret = dhcp_run(addr, NULL, false);
+		if (ret)
+			return log_msg_ret("dhc", ret);
+	}
 
 	log_debug("calling pxe_get()\n");
 	ret = pxe_get(addr, &bootdir, &size, false);
Signature · 1 line
2.53.0
#13 lore raw

PATCH 13/19 spl: use the known file length for raw U-Boot images

In reply to James Hilliard

When a file loader supplies a headerless u-boot.bin, SPL may request more bytes than the file contains and reject a complete image. Without a size from binman it uses CONFIG_SYS_MONITOR_LEN or a 200 KiB guess. The guess can also truncate a larger file.

Pass the available byte count to the raw-image fallback. Use the exact file length within the configured monitor reservation and report an error if the image does not fit. When binman supplies the expected length, keep that length and reject shorter files. Loaders without a known file size retain the existing size selection.

---
 common/spl/spl.c      | 49 +++++++++++++++++++++++++++----------------
 common/spl/spl_nand.c |  3 +--
 include/spl.h         | 20 +++++++++++++++++-
 include/spl_load.h    |  2 +-
 test/image/spl_load.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 109 insertions(+), 22 deletions(-)
common/spl/spl.c +31 -18
index 6c9a1a1cbcc..10c495f8766 100644
@@ -259,16 +259,10 @@ __weak struct legacy_img_hdr *spl_get_load_buffer(ssize_t offset, size_t size)
 	return map_sysmem(CONFIG_TEXT_BASE + offset, 0);
 }
 
-void spl_set_header_raw_uboot(struct spl_image_info *spl_image)
+int spl_set_header_raw_uboot(struct spl_image_info *spl_image, size_t image_size)
 {
 	ulong u_boot_pos = spl_get_image_pos();
-
-#if CONFIG_SYS_MONITOR_LEN != 0
-	spl_image->size = CONFIG_SYS_MONITOR_LEN;
-#else
-	/* Unknown U-Boot size, let's assume it will not be more than 200 KB */
-	spl_image->size = 200 * 1024;
-#endif
+	ulong expected = 0;
 
 	/*
 	 * Binman error cases: address of the end of the previous region or the
@@ -279,7 +273,7 @@ void spl_set_header_raw_uboot(struct spl_image_info *spl_image)
 		/* Binman does not support separated entry addresses */
 		spl_image->entry_point = spl_get_image_text_base();
 		spl_image->load_addr = spl_get_image_text_base();
-		spl_image->size = spl_get_image_size();
+		expected = spl_get_image_size();
 		log_debug("Next load addr %lx\n", spl_image->load_addr);
 	} else {
 		spl_image->entry_point = CONFIG_SYS_UBOOT_START;
@@ -287,10 +281,24 @@ void spl_set_header_raw_uboot(struct spl_image_info *spl_image)
 		log_debug("Default load addr %lx (u_boot_pos=%lx)\n",
 			  spl_image->load_addr, u_boot_pos);
 	}
+	if (expected && expected != BINMAN_SYM_MISSING) {
+		if (expected > U32_MAX)
+			return -EFBIG;
+		if (image_size && image_size < expected)
+			return -EIO;
+		spl_image->size = expected;
+	} else {
+		/* A file has an exact length; raw storage may have only a bound. */
+		if (image_size > U32_MAX ||
+		    (CONFIG_SYS_MONITOR_LEN && image_size > CONFIG_SYS_MONITOR_LEN))
+			return -EFBIG;
+		spl_image->size = image_size ?: (CONFIG_SYS_MONITOR_LEN ?: 200 * 1024);
+	}
 	spl_image->os = IH_OS_U_BOOT;
 	spl_image->name = xpl_name(xpl_next_phase());
 	log_debug("Next phase: %s at %lx size %lx\n", spl_image->name,
 		  spl_image->load_addr, (ulong)spl_image->size);
+	return 0;
 }
 
 __weak int spl_parse_board_header(struct spl_image_info *spl_image,
@@ -308,9 +316,10 @@ __weak int spl_parse_legacy_header(struct spl_image_info *spl_image,
 	return -EINVAL;
 }
 
-int spl_parse_image_header(struct spl_image_info *spl_image,
-			   const struct spl_boot_device *bootdev,
-			   const struct legacy_img_hdr *header)
+int spl_parse_image_header_size(struct spl_image_info *spl_image,
+				const struct spl_boot_device *bootdev,
+				const struct legacy_img_hdr *header,
+				size_t image_size)
 {
 	int ret;
 
@@ -380,14 +389,18 @@ int spl_parse_image_header(struct spl_image_info *spl_image,
 		/* Signature not found - assume u-boot.bin */
 		debug("mkimage signature not found - ih_magic = %x\n",
 		      header->ih_magic);
-		spl_set_header_raw_uboot(spl_image);
-	} else {
-		/* RAW image not supported, proceed to other boot methods. */
-		debug("Raw boot image support not enabled, proceeding to other boot methods\n");
-		return -EINVAL;
+		return spl_set_header_raw_uboot(spl_image, image_size);
 	}
 
-	return 0;
+	debug("Raw boot image support not enabled, proceeding to other boot methods\n");
+	return -EINVAL;
+}
+
+int spl_parse_image_header(struct spl_image_info *spl_image,
+			   const struct spl_boot_device *bootdev,
+			   const struct legacy_img_hdr *header)
+{
+	return spl_parse_image_header_size(spl_image, bootdev, header, 0);
 }
 
 #if SPL_LOAD_USERS > 1
common/spl/spl_nand.c +1 -2
index f449b31f594..397fb26c10b 100644
@@ -35,10 +35,9 @@ static int spl_nand_load_image(struct spl_image_info *spl_image,
 			    CFG_SYS_NAND_U_BOOT_SIZE,
 			    map_sysmem(CFG_SYS_NAND_U_BOOT_DST,
 				       CFG_SYS_NAND_U_BOOT_SIZE));
-	spl_set_header_raw_uboot(spl_image);
 	nand_deselect();
 
-	return 0;
+	return spl_set_header_raw_uboot(spl_image, 0);
 }
 #else
 
include/spl.h +19 -1
index 390d18fd9d5..2fd99b64a5c 100644
@@ -713,8 +713,10 @@ unsigned long spl_mmc_get_uboot_raw_sector(struct mmc *mmc,
  * CONFIG_TEXT_BASE.
  *
  * @spl_image: Image description to set up
+ * @image_size: Available bytes in the image, or zero if unknown
+ * Return: 0 on success or a negative error if the image cannot be loaded
  */
-void spl_set_header_raw_uboot(struct spl_image_info *spl_image);
+int spl_set_header_raw_uboot(struct spl_image_info *spl_image, size_t image_size);
 
 /**
  * spl_parse_image_header() - parse the image header and set up info
@@ -734,6 +736,22 @@ int spl_parse_image_header(struct spl_image_info *spl_image,
 			   const struct spl_boot_device *bootdev,
 			   const struct legacy_img_hdr *header);
 
+/**
+ * spl_parse_image_header_size() - parse a header with a known image size
+ *
+ * @spl_image: Image description to set up
+ * @bootdev: Boot device providing the image
+ * @header: Image header to parse
+ * @image_size: Available bytes in the image, or zero if unknown
+ *
+ * Return: As for spl_parse_image_header(). A raw image uses its file size
+ * when binman does not specify the size of the next phase.
+ */
+int spl_parse_image_header_size(struct spl_image_info *spl_image,
+				const struct spl_boot_device *bootdev,
+				const struct legacy_img_hdr *header,
+				size_t image_size);
+
 void spl_board_prepare_for_linux(void);
 
 /**
include/spl_load.h +1 -1
index 31ed5651f7b..6265255f23e 100644
@@ -74,7 +74,7 @@ static inline int _spl_load(struct spl_image_info *spl_image,
 		return spl_load_legacy_lzma(spl_image, info, offset);
 	}
 
-	ret = spl_parse_image_header(spl_image, bootdev, header);
+	ret = spl_parse_image_header_size(spl_image, bootdev, header, size);
 	if (ret)
 		return ret;
 
test/image/spl_load.c +57 -0

75 lines not on this page · raw message

Signature · 1 line
2.53.0
#14 lore raw

PATCH 14/19 spl: fit: read the exact size of an embedded FIT

In reply to James Hilliard

A FIT header with totalsize zero makes spl_simple_fit_read() request no bytes, and the zero-byte read passes its length check. A load-only caller that skips FIT parsing can then report success for an empty image. Reject zero before alignment and before issuing this read.

A byte-addressed loader can also reject a complete FIT when its strings block ends between word boundaries and the file has no trailing padding. Only the start of external image data needs four-byte alignment.

Read the FIT at its declared length, preserving additional space a board requires for authentication and transport alignment. Reject short reads before parsing. Test zero size, an unpadded file, and a short read.

---
 common/spl/spl_fit.c  |  9 +++++++--
 test/image/spl_load.c | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 63 insertions(+), 2 deletions(-)
common/spl/spl_fit.c +7 -2
index 64e5f95546f..9182fa5c686 100644
@@ -16,6 +16,7 @@
 #include <sysinfo.h>
 #include <asm/global_data.h>
 #include <asm/io.h>
+#include <linux/err.h>
 #include <linux/libfdt.h>
 #include <linux/printk.h>
 
@@ -797,7 +798,11 @@ static int spl_simple_fit_read(struct spl_fit_info *ctx,
 	 * start. This is the base for the data-offset properties in each
 	 * image.
 	 */
-	size = ALIGN(fdt_totalsize(fit_header), 4);
+	size = fdt_totalsize(fit_header);
+	if (!size) {
+		log_debug("Invalid FIT size: %lu\n", size);
+		return -EINVAL;
+	}
 	size = board_spl_fit_size_align(size);
 	ctx->ext_data_offset = ALIGN(size, 4);
 
@@ -824,7 +829,7 @@ static int spl_simple_fit_read(struct spl_fit_info *ctx,
 	}
 
 	count = info->read(info, offset, aligned_size, buf);
-	if (!count) {
+	if (IS_ERR_VALUE(count) || count < size) {
 		/*
 		 * FIT could not be read. This means we should free the
 		 * memory allocated by board_spl_fit_buffer_addr().
test/image/spl_load.c +56 -0

62 lines not on this page · raw message

Signature · 1 line
2.53.0
#15 lore raw

PATCH 15/19 spl: net: bound reads to the downloaded file

In reply to James Hilliard

When booting over Ethernet, SPL knows how many bytes it downloaded but passes an unknown length to the image loader. A read past the end then copies whatever follows the image in RAM instead of reporting a short read.

Pass the file length to spl_load() and reject reads outside that range. Use memmove() because the load destination can overlap the downloaded image, and unmap the source after copying.

---
 common/spl/spl_net.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
common/spl/spl_net.c +8 -2
index 2be7b73ed35..1cdb7027ef2 100644
@@ -19,9 +19,15 @@
 static ulong spl_net_load_read(struct spl_load_info *load, ulong sector,
 			       ulong count, void *buf)
 {
+	void *src;
+
 	debug("%s: sector %lx, count %lx, buf %lx\n",
 	      __func__, sector, count, (ulong)buf);
-	memcpy(buf, map_sysmem(image_load_addr + sector, count), count);
+	if (sector > net_boot_file_size || count > net_boot_file_size - sector)
+		return 0;
+	src = map_sysmem(image_load_addr + sector, count);
+	memmove(buf, src, count);
+	unmap_sysmem(src);
 	return count;
 }
 
@@ -48,7 +54,7 @@ static int spl_net_load_image(struct spl_image_info *spl_image,
 	}
 
 	spl_load_init(&load, spl_net_load_read, NULL, 1);
-	return spl_load(spl_image, bootdev, &load, 0, 0);
+	return spl_load(spl_image, bootdev, &load, net_boot_file_size, 0);
 }
 #endif
 
Signature · 1 line
2.53.0
#16 lore raw

PATCH 16/19 spl: net: load a boot image with lwIP

In reply to James Hilliard

Let Ethernet SPL boot use the same IPv4 address acquisition and TFTP transfer as U-Boot proper. The loader needs a nonempty file in RAM:
leave the decision to boot it to SPL, regardless of the interactive autoload and autostart settings.

Make the in-memory environment writable if the backend exposes only the built-in defaults. DHCP and BOOTP need to publish the filename and addresses for TFTP. Use the SPL vendor-class identifier and require a reusable malloc pool for packet buffers.

---
 common/spl/Kconfig         | 10 ++++++++--
 common/spl/spl_net.c       | 13 ++++++++++++-
 include/net-lwip.h         |  1 +
 lib/Makefile               |  2 +-
 lib/lwip/u-boot/lwipopts.h | 14 +++++++-------
 net/Makefile               |  2 +-
 net/lwip/Makefile          |  5 +++++
 net/lwip/dhcp.c            | 28 ++++++++++++++++++++++------
 net/lwip/net-lwip.c        |  2 +-
 test/dm/lwip_dhcp.c        | 23 ++++++++++++++++++++---
 test/image/spl_load_net.c  | 43 +++++++++++++++++++++++++++++++++++--------
 11 files changed, 113 insertions(+), 30 deletions(-)
common/spl/Kconfig +8 -2
index 4930ecb0861..19b5d9f30f8 100644
@@ -1177,7 +1177,10 @@ config SPL_DM_SPI_FLASH
 
 config SPL_NET
 	bool "Support networking"
-	depends on NET_LEGACY
+	depends on NET_LEGACY || NET_LWIP
+	depends on !NET_LWIP || (SPL_DM && SPL_LIBGENERIC_SUPPORT && SPL_ENV_SUPPORT)
+	depends on !NET_LWIP || (SPL_SYS_MALLOC && !SPL_SYS_MALLOC_SIMPLE)
+	depends on !NET_LWIP || (CMD_TFTPBOOT && (CMD_BOOTP || CMD_DHCP))
 	select SPL_USE_TINY_PRINTF_POINTER_SUPPORT if SPL_USE_TINY_PRINTF
 	help
 	  Enable support for network devices (such as Ethernet) in SPL.
@@ -1187,7 +1190,10 @@ config SPL_NET
 	  SPL_ETH.
 
 config SPL_NET_LEGACY
-	def_bool y if SPL_NET
+	def_bool SPL_NET && NET_LEGACY
+
+config SPL_NET_LWIP
+	def_bool SPL_NET && NET_LWIP
 
 config SPL_NET_VCI_STRING
 	string "BOOTP Vendor Class Identifier string sent by SPL"
common/spl/spl_net.c +12 -1
index 1cdb7027ef2..27c7170133b 100644
@@ -13,8 +13,11 @@
 #include <spl.h>
 #include <spl_load.h>
 #include <net.h>
+#include <asm/global_data.h>
 #include <linux/libfdt.h>
 
+DECLARE_GLOBAL_DATA_PTR;
+
 #if defined(CONFIG_SPL_ETH) || defined(CONFIG_SPL_USB_ETHER)
 static ulong spl_net_load_read(struct spl_load_info *load, ulong sector,
 			       ulong count, void *buf)
@@ -39,7 +42,11 @@ static int spl_net_load_image(struct spl_image_info *spl_image,
 
 	env_init();
 	env_relocate();
-	env_set("autoload", "yes");
+	/* The nowhere backend leaves SPL's default environment read-only. */
+	if (CONFIG_IS_ENABLED(NET_LWIP) && !(gd->flags & GD_FLG_ENV_READY))
+		env_set_default(NULL, 0);
+	if (!CONFIG_IS_ENABLED(NET_LWIP))
+		env_set("autoload", "yes");
 	rv = eth_initialize();
 	if (rv == 0) {
 		printf("No Ethernet devices found\n");
@@ -47,7 +54,11 @@ static int spl_net_load_image(struct spl_image_info *spl_image,
 	}
 	if (bootdev->boot_device_name)
 		env_set("ethact", bootdev->boot_device_name);
+#if CONFIG_IS_ENABLED(NET_LWIP)
+	rv = net_lwip_load_bootfile();
+#else
 	rv = net_loop(BOOTP);
+#endif
 	if (rv < 0) {
 		printf("Problem booting with BOOTP\n");
 		return rv;
include/net-lwip.h +1 -0
index 8b64f32b913..65d6ab4af12 100644
@@ -106,6 +106,7 @@ void net_lwip_dhcp6_poll(void);
 int net_lwip_ip6_prepare(struct net_lwip_ctx *ctx, ip_addr_t *dest);
 #endif
 int net_lwip_tftp_download(struct net_lwip_ctx *net, int argc, char *const argv[]);
+int net_lwip_load_bootfile(void);
 
 /**
  * wget_validate_uri() - varidate the uri
lib/Makefile +1 -1
index 925cb619807..9ce701075dc 100644
@@ -104,7 +104,7 @@ obj-$(CONFIG_$(PHASE_)OF_REAL) += fdtdec_common.o fdtdec.o
 
 obj-$(CONFIG_$(PHASE_)MBEDTLS_LIB) += mbedtls/
 
-obj-$(CONFIG_NET_LWIP) += lwip/
+obj-$(CONFIG_$(PHASE_)NET_LWIP) += lwip/
 
 ifdef CONFIG_XPL_BUILD
 obj-$(CONFIG_SPL_YMODEM_SUPPORT) += crc16-ccitt.o
lib/lwip/u-boot/lwipopts.h +7 -7
index ab3b39a198f..5ce6b166f73 100644
@@ -60,9 +60,9 @@
 #define NO_SYS                          1
 
 #define LWIP_IPV4			1
-#define LWIP_IPV6			IS_ENABLED(CONFIG_IPV6)
-#define LWIP_IPV6_SEND_ROUTER_SOLICIT	IS_ENABLED(CONFIG_IPV6_ROUTER_DISCOVERY)
-#define LWIP_IPV6_AUTOCONFIG		IS_ENABLED(CONFIG_IPV6_ROUTER_DISCOVERY)
+#define LWIP_IPV6			CONFIG_IS_ENABLED(IPV6)
+#define LWIP_IPV6_SEND_ROUTER_SOLICIT	CONFIG_IS_ENABLED(IPV6_ROUTER_DISCOVERY)
+#define LWIP_IPV6_AUTOCONFIG		CONFIG_IS_ENABLED(IPV6_ROUTER_DISCOVERY)
 #define LWIP_ND6_RDNSS_MAX_DNS_SERVERS	(IS_ENABLED(CONFIG_IPV6_ROUTER_DISCOVERY) && \
 					 IS_ENABLED(CONFIG_DNS) ? DNS_MAX_SERVERS : 0)
 /* A 64-bit reassembly pointer does not fit in the fragment header. */
@@ -113,13 +113,13 @@
 
 #define IP_DEFAULT_TTL                  255
 
-#if defined(CONFIG_PROT_ICMP_LWIP)
+#if CONFIG_IS_ENABLED(PROT_ICMP_LWIP)
 #define LWIP_ICMP                       1
 #else
 #define LWIP_ICMP                       0
 #endif
 
-#if defined(CONFIG_PROT_RAW_LWIP)
+#if CONFIG_IS_ENABLED(PROT_RAW_LWIP)
 #define LWIP_RAW                        1
 #else
 #define LWIP_RAW			0
@@ -211,7 +211,7 @@
 
 #define LWIP_IGMP                       0
 
-#if defined(CONFIG_PROT_DNS_LWIP)
+#if CONFIG_IS_ENABLED(PROT_DNS_LWIP)
 #define LWIP_DNS                        1
 #define DNS_TABLE_SIZE                  1
 #else
@@ -229,7 +229,7 @@
  * CONFIG_PROT_TCP_LWIP is not defined
  */
 #define TCP_MSS                         1460
-#if defined(CONFIG_PROT_TCP_LWIP)
+#if CONFIG_IS_ENABLED(PROT_TCP_LWIP)
 #define LWIP_TCP                        1
 #define TCP_WND                         CONFIG_LWIP_TCP_WND
 #define LWIP_WND_SCALE                  1
net/Makefile +1 -1
index fb06ccdb38d..929b9ae9874 100644
@@ -49,5 +49,5 @@ obj-$(CONFIG_CMD_WOL) += wol-common.o
 obj-y += net-common.o
 endif
 
-obj-$(CONFIG_NET_LWIP) += lwip/
+obj-$(CONFIG_$(PHASE_)NET_LWIP) += lwip/
 obj-$(CONFIG_NET_9P) += 9p/
net/lwip/Makefile +5 -0
index ac17ebe87f2..539092190e4 100644
@@ -1,9 +1,13 @@
 ccflags-y += -I$(srctree)/lib/lwip/lwip/src/include -I$(srctree)/lib/lwip/u-boot
 
 obj-$(CONFIG_$(PHASE_)DM_ETH) += net-lwip.o
+ifdef CONFIG_XPL_BUILD
+obj-$(CONFIG_$(PHASE_)NET_LWIP) += dhcp.o tftp.o
+else
 ifneq ($(filter y,$(CONFIG_CMD_BOOTP) $(CONFIG_CMD_DHCP)),)
 obj-y += dhcp.o
 endif
+
 obj-$(CONFIG_DNS) += dns.o
 obj-$(CONFIG_$(PHASE_)CMD_CDP) += cdp.o
 obj-$(CONFIG_CMD_DHCP6) += dhcp6.o
@@ -19,3 +23,4 @@ $(obj)/builtin_cacert.c: $(CONFIG_WGET_BUILTIN_CACERT_PATH:"%"=%) FORCE
 	$(call if_changed,bin2c,builtin_cacert)
 obj-y += builtin_cacert.o
 endif
+endif # !CONFIG_XPL_BUILD
net/lwip/dhcp.c +22 -6
index b756199541b..a4b1d236f67 100644
@@ -931,7 +931,7 @@ out:
 }
 
 static int bootp_run(struct cmd_tbl *cmdtp, int flag, int argc,
-		     char *const argv[], bool bootp)
+		     char *const argv[], bool bootp, bool download_only)
 {
 	struct net_lwip_ctx net = {};
 	struct dhcp_options *options;
@@ -964,7 +964,14 @@ static int bootp_run(struct cmd_tbl *cmdtp, int flag, int argc,
 		      sizeof(net_boot_file_name));
 
 	hostname = IS_ENABLED(CONFIG_BOOTP_SEND_HOSTNAME) ? env_get("hostname") : NULL;
-	vendor = env_get("bootp_vci") ?: CONFIG_BOOTP_VCI_STRING;
+	vendor = env_get("bootp_vci");
+	if (!vendor) {
+#ifdef CONFIG_SPL_BUILD
+		vendor = CONFIG_SPL_NET_VCI_STRING;
+#else
+		vendor = CONFIG_BOOTP_VCI_STRING;
+#endif
+	}
 	if ((hostname && strlen(hostname) > 255) || strlen(vendor) > 255) {
 		log_err("BOOTP/DHCP hostname/vendor class exceeds 255 bytes\n");
 		return CMD_RET_FAILURE;
@@ -1014,8 +1021,8 @@ static int bootp_run(struct cmd_tbl *cmdtp, int flag, int argc,
 		acquisition_timeout = ret == -ETIMEDOUT;
 		if (!ret) {
 			autoload = env_get("autoload");
-			if ((autoload && !strcmp(autoload, "NFS")) ||
-			    env_get_yesno("autoload") == 0)
+			if (!download_only && ((autoload && !strcmp(autoload, "NFS")) ||
+					       env_get_yesno("autoload") == 0))
 				break;
 			if (!IS_ENABLED(CONFIG_CMD_TFTPBOOT)) {
 				log_err("Cannot autoload with TFTP: command is disabled\n");
@@ -1056,6 +1063,8 @@ static int bootp_run(struct cmd_tbl *cmdtp, int flag, int argc,
 
 	if (ret)
 		return CMD_RET_FAILURE;
+	if (IS_ENABLED(CONFIG_SPL_BUILD) || download_only)
+		return downloaded && net_boot_file_size ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
 	if (!downloaded) {
 		autoload = env_get("autoload");
 		if (!autoload || strcmp(autoload, "NFS"))
@@ -1073,10 +1082,17 @@ static int bootp_run(struct cmd_tbl *cmdtp, int flag, int argc,
 
 int do_dhcp(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
 {
-	return bootp_run(cmdtp, flag, argc, argv, false);
+	return bootp_run(cmdtp, flag, argc, argv, false, IS_ENABLED(CONFIG_SPL_BUILD));
 }
 
 int do_bootp(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
 {
-	return bootp_run(cmdtp, flag, argc, argv, true);
+	return bootp_run(cmdtp, flag, argc, argv, true, IS_ENABLED(CONFIG_SPL_BUILD));
+}
+
+int net_lwip_load_bootfile(void)
+{
+	char *const argv[] = { "bootp", NULL };
+
+	return bootp_run(NULL, 0, 1, argv, !IS_ENABLED(CONFIG_CMD_DHCP), true) ? -EIO : 0;
 }
net/lwip/net-lwip.c +1 -1
index 3a9a5a42716..075d5b1716f 100644
@@ -1126,7 +1126,7 @@ static int net_lwip_rx(struct udevice *udev, struct netif *netif)
 			if (!net_lwip_accept_vlan(packet, len))
 				goto release;
 
-			if (IS_ENABLED(CONFIG_CMD_WOL))
+			if (CONFIG_IS_ENABLED(CMD_WOL))
 				net_lwip_wol_receive(netif, packet, len);
 
 			pbuf = alloc_pbuf_and_copy(packet, len);
test/dm/lwip_dhcp.c +20 -3
index d7e1360d2de..f6af57a3647 100644
@@ -19,6 +19,7 @@
 struct dhcp_handoff_test {
 	const char *offer;
 	bool fail;
+	bool empty;
 	bool timeout;
 	bool dhcp_alive;
 	bool rx_burst;
@@ -123,6 +124,7 @@ static int dhcp_handoff_tx(struct udevice *dev, void *packet, unsigned int len)
 	struct dhcp_handoff_test *test = priv->priv;
 	struct ethernet_hdr *eth = packet, *reply_eth;
 	struct ip_udp_hdr *ip, *reply_ip;
+	unsigned int tftp_len = test->empty ? 4 : 6;
 	u8 *data;
 
 	if (test->rx_burst && netif_dhcp_data(netif_default) &&
@@ -167,14 +169,14 @@ static int dhcp_handoff_tx(struct udevice *dev, void *packet, unsigned int len)
 	reply_ip = (void *)(reply_eth + 1);
 	memset(reply_ip, 0, IP_UDP_HDR_SIZE);
 	reply_ip->ip_hl_v = 0x45;
-	reply_ip->ip_len = htons(IP_UDP_HDR_SIZE + 6);
+	reply_ip->ip_len = htons(IP_UDP_HDR_SIZE + tftp_len);
 	reply_ip->ip_ttl = 64;
 	reply_ip->ip_p = IPPROTO_UDP;
 	reply_ip->ip_src = ip->ip_dst;
 	reply_ip->ip_dst = ip->ip_src;
 	reply_ip->udp_src = htons(1234);
 	reply_ip->udp_dst = ip->udp_src;
-	reply_ip->udp_len = htons(UDP_HDR_SIZE + 6);
+	reply_ip->udp_len = htons(UDP_HDR_SIZE + tftp_len);
 	reply_ip->ip_sum = compute_ip_checksum(reply_ip, IP_HDR_SIZE);
 	data = (void *)(reply_ip + 1);
 	put_unaligned_be16(test->fail ? 5 : 3, data);
@@ -187,7 +189,7 @@ static int dhcp_handoff_tx(struct udevice *dev, void *packet, unsigned int len)
 	data[4] = 'x';
 	data[5] = 0;
 	priv->recv_packet_length[priv->recv_packets++] =
-		ETHER_HDR_SIZE + IP_UDP_HDR_SIZE + 6;
+		ETHER_HDR_SIZE + IP_UDP_HDR_SIZE + tftp_len;
 
 	return 0;
 }
@@ -289,6 +291,21 @@ static int dhcp_handoff_check(struct unit_test_state *uts,
 	ut_assertok(env_set("nfsserverip", NULL));
 	ut_assertok(env_set("tftpdstp", NULL));
 	ut_assertok(env_set("tftpsrcp", NULL));
+	if (IS_ENABLED(CONFIG_CMD_TFTPBOOT)) {
+		*test = (struct dhcp_handoff_test){ .offer = "u-boot.img" };
+		ut_assertok(env_set("autoload", "NFS"));
+		ut_assertok(env_set("autostart", "yes"));
+		ut_assertok(net_lwip_load_bootfile());
+		ut_asserteq(1, test->tftp);
+		ut_asserteq(0, test->nfs);
+		ut_asserteq(2, net_boot_file_size);
+		ut_asserteq_str("u-boot.img", test->filename);
+		ut_assertnull(netif_default);
+		test->empty = true;
+		ut_asserteq(-EIO, net_lwip_load_bootfile());
+		ut_asserteq(0, net_boot_file_size);
+		ut_assertok(env_set("autostart", "no"));
+	}
 	ut_assertok(dhcp_autoload_retries(uts, test));
 	for (i = 0; i < 4; i++) {
 		*test = (struct dhcp_handoff_test){ .offer = "offered.bin" };
test/image/spl_load_net.c +35 -8

78 lines not on this page · raw message

Signature · 1 line
2.53.0
#17 lore raw

PATCH 17/19 net: ncsi: check the selected interface before probing

In reply to James Hilliard

With multiple Ethernet interfaces, net_loop() decides whether to probe NC-SI before it selects the device named by ethact. It can start discovery on an ordinary port, or skip discovery on a sideband port. Its recursive call also starts a second Ethernet session and clears the ping destination before the original command can use it.

Select the protocol after Ethernet startup and repeat the choice when a retry rotates to another interface. Run discovery in the same loop:
on success, start the requested protocol on the configured interface; on failure, use the normal retry and error paths. Reject an explicit NC-SI command on a regular port or without an active PHY context.

---
 drivers/net/phy/ncsi.c |  6 ++++
 net/net.c              | 55 ++++++++++++++++++++++------------
 test/dm/eth.c          | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 123 insertions(+), 19 deletions(-)
drivers/net/phy/ncsi.c +6 -0
index a1de438ffff..7857412248e 100644
@@ -793,6 +793,12 @@ void ncsi_probe_packages(void)
 	struct ncsi_package *package;
 	unsigned int np, nc;
 
+	if (!ncsi_priv) {
+		printf("NCSI: no active interface\n");
+		net_set_state(NETLOOP_FAIL);
+		return;
+	}
+
 	switch (ncsi_priv->state) {
 	case NCSI_PROBE_PACKAGE_SP:
 		if (ncsi_priv->current_package == NCSI_PACKAGE_MAX)
net/net.c +36 -19

98 lines not on this page · raw message

test/dm/eth.c +81 -0

87 lines not on this page · raw message

Signature · 1 line
2.53.0
#18 lore raw

PATCH 18/19 net: ncsi: track the outstanding management command

In reply to James Hilliard

If the controller stops replying while a channel is being enabled, NC-SI can report success and let an IP client use an unconfigured channel. The timeout drops the last pending request, which the state machine mistakes for successful completion.

Record each command's ID, type and channel and advance only on its matching reply. Wrap from 0xff to 1 because zero is reserved for asynchronous events. Check the received length before reading the response payload or checksum. Report timeouts and send or allocation failures. Send management frames from a DMA-aligned buffer, as required by MACs that pass the packet buffer directly to hardware.

Select the active PHY at startup and release its discovery state at shutdown. Keep the package and channel selected by discovery through configuration. If link status succeeds but a later capability query times out, discovery advances but leaves the earlier has_link bit set. Rescanning those flags can select the timed-out channel instead of the channel whose probe completed.

Exercise discovery with a sandbox controller that checks command IDs and buffer alignment. Verify that a ping does not transmit IP traffic until the channel has been enabled.

---
 drivers/net/phy/ncsi.c | 243 ++++++++++++++++++++++++++-----------------------
 include/net/ncsi.h     |   3 +-
 net/net.c              |   2 +-
 test/dm/Makefile       |   3 +
 test/dm/ncsi.c         | 141 ++++++++++++++++++++++++++++
 5 files changed, 273 insertions(+), 119 deletions(-)
drivers/net/phy/ncsi.c +127 -116
index 7857412248e..5cc183b88f8 100644
@@ -7,6 +7,7 @@
 
 #include <log.h>
 #include <malloc.h>
+#include <memalign.h>
 #include <phy.h>
 #include <net.h>
 #include <net/ncsi.h>
@@ -76,8 +77,10 @@ struct ncsi {
 	} state;
 
 	unsigned int	pending_requests;
-	unsigned int	requests[256];
-	unsigned int	last_request;
+	u8		last_request;
+	u8		expected_type;
+	u8		expected_channel;
+	int		result;
 
 	unsigned int	current_package;
 	unsigned int	current_channel;
@@ -86,7 +89,7 @@ struct ncsi {
 	struct ncsi_package	*packages;
 };
 
-struct ncsi *ncsi_priv;
+static struct ncsi *ncsi_priv;
 
 bool ncsi_active(void)
 {
@@ -98,13 +101,37 @@ bool ncsi_active(void)
 	np = ncsi_priv->current_package;
 	nc = ncsi_priv->current_channel;
 
-	if (ncsi_priv->state != NCSI_CONFIG)
+	if (ncsi_priv->state != NCSI_CONFIG || ncsi_priv->result)
 		return false;
 
-	return np < NCSI_PACKAGE_MAX && nc < NCSI_CHANNEL_MAX &&
+	return np < ncsi_priv->n_packages && nc < ncsi_priv->packages[np].n_channels &&
 		ncsi_priv->packages[np].channels[nc].has_link;
 }
 
+static void ncsi_complete(int result)
+{
+	ncsi_priv->result = result;
+	ncsi_priv->pending_requests = 0;
+	net_set_timeout_handler(0, NULL);
+	net_set_state(result ? NETLOOP_FAIL : NETLOOP_SUCCESS);
+}
+
+static void ncsi_reset_probe(void)
+{
+	unsigned int i;
+
+	for (i = 0; i < ncsi_priv->n_packages; i++)
+		free(ncsi_priv->packages[i].channels);
+	free(ncsi_priv->packages);
+	ncsi_priv->packages = NULL;
+	ncsi_priv->n_packages = 0;
+	ncsi_priv->current_package = NCSI_PACKAGE_MAX;
+	ncsi_priv->current_channel = NCSI_CHANNEL_MAX;
+	ncsi_priv->pending_requests = 0;
+	ncsi_priv->result = -EINPROGRESS;
+	ncsi_priv->state = NCSI_PROBE_PACKAGE_SP;
+}
+
 static unsigned int cmd_payload(int cmd)
 {
 	switch (cmd) {
@@ -183,8 +210,7 @@ static u32 ncsi_calculate_checksum(unsigned char *data, int len)
 static int ncsi_validate_rsp(struct ncsi_rsp_pkt *pkt, int payload)
 {
 	struct ncsi_rsp_pkt_hdr *hdr = &pkt->rsp;
-	u32 checksum, c_offset;
-	__be32 pchecksum;
+	u32 checksum, c_offset, pchecksum;
 
 	if (hdr->common.revision != 1) {
 		printf("NCSI: 0x%02x response has unsupported revision 0x%x\n",
@@ -202,7 +228,7 @@ static int ncsi_validate_rsp(struct ncsi_rsp_pkt *pkt, int payload)
 
 	if (ntohs(hdr->common.length) != payload) {
 		printf("NCSI: 0x%02x response has incorrect length %d\n",
-		       hdr->common.type, hdr->common.length);
+		       hdr->common.type, ntohs(hdr->common.length));
 		return -1;
 	}
 
@@ -341,7 +367,7 @@ static void ncsi_rsp_gls(struct ncsi_rsp_pkt *pkt)
 	}
 
 	ncsi_priv->packages[np].channels[nc].has_link =
-					!!(get_unaligned_be32(&gls->status));
+					!!(get_unaligned_be32(&gls->status) & BIT(0));
 
 	if (ncsi_priv->state == NCSI_PROBE_CHANNEL)
 		ncsi_send_command(np, nc, NCSI_PKT_CMD_GVI, NULL, 0, true);
@@ -351,6 +377,7 @@ static void ncsi_rsp_cis(struct ncsi_rsp_pkt *pkt)
 {
 	struct ncsi_rsp_pkt_hdr *rsp = (struct ncsi_rsp_pkt_hdr *)pkt;
 	struct ncsi_package *package;
+	struct ncsi_channel *channels;
 	unsigned int np, nc;
 
 	np = NCSI_PACKAGE_INDEX(rsp->common.channel);
@@ -363,28 +390,22 @@ static void ncsi_rsp_cis(struct ncsi_rsp_pkt *pkt)
 
 	package = &ncsi_priv->packages[np];
 
-	if (nc < package->n_channels) {
-		/*
-		 * This is fine in general but in the current design we
-		 * don't send CIS commands to known channels.
-		 */
-		debug("NCSI: Duplicate channel 0x%02x\n", nc);
-		return;
-	}
-
-	package->channels = realloc(package->channels,
-				    sizeof(struct ncsi_channel) *
-				    (package->n_channels + 1));
-	if (!package->channels) {
-		printf("NCSI: Could not allocate memory for new channel\n");
-		return;
+	if (nc >= package->n_channels) {
+		channels = realloc(package->channels, sizeof(*channels) * (nc + 1));
+		if (!channels) {
+			ncsi_complete(-ENOMEM);
+			return;
+		}
+		memset(channels + package->n_channels, 0,
+		       (nc + 1 - package->n_channels) * sizeof(*channels));
+		package->channels = channels;
+		package->n_channels = nc + 1;
 	}
 
 	debug("NCSI: New channel 0x%02x\n", nc);
 
 	package->channels[nc].id = nc;
 	package->channels[nc].has_link = false;
-	package->n_channels++;
 
 	ncsi_send_gls(np, nc);
 }
@@ -404,6 +425,7 @@ static void ncsi_rsp_dp(struct ncsi_rsp_pkt *pkt)
 static void ncsi_rsp_sp(struct ncsi_rsp_pkt *pkt)
 {
 	struct ncsi_rsp_pkt_hdr *rsp = (struct ncsi_rsp_pkt_hdr *)pkt;
+	struct ncsi_package *packages;
 	unsigned int np;
 
 	np = NCSI_PACKAGE_INDEX(rsp->common.channel);
@@ -416,18 +438,16 @@ static void ncsi_rsp_sp(struct ncsi_rsp_pkt *pkt)
 
 	debug("NCSI: adding new package %d\n", np);
 
-	ncsi_priv->packages = realloc(ncsi_priv->packages,
-				      sizeof(struct ncsi_package) *
-				      (ncsi_priv->n_packages + 1));
-	if (!ncsi_priv->packages) {
-		printf("NCSI: could not allocate memory for new package\n");
+	packages = realloc(ncsi_priv->packages, sizeof(*packages) * (np + 1));
+	if (!packages) {
+		ncsi_complete(-ENOMEM);
 		return;
 	}
-
+	memset(packages + ncsi_priv->n_packages, 0,
+	       (np + 1 - ncsi_priv->n_packages) * sizeof(*packages));
+	ncsi_priv->packages = packages;
 	ncsi_priv->packages[np].id = np;
-	ncsi_priv->packages[np].n_channels = 0;
-	ncsi_priv->packages[np].channels = NULL;
-	ncsi_priv->n_packages++;
+	ncsi_priv->n_packages = np + 1;
 }
 
 static void ncsi_update_state(struct ncsi_rsp_pkt_hdr *nh)
@@ -452,7 +472,7 @@ static void ncsi_update_state(struct ncsi_rsp_pkt_hdr *nh)
 		} else {
 			if (!ncsi_priv->n_packages) {
 				printf("NCSI: no packages found\n");
-				net_set_state(NETLOOP_FAIL);
+				ncsi_complete(-ENODEV);
 				return;
 			}
 			printf("NCSI: probing channels\n");
@@ -466,9 +486,9 @@ static void ncsi_update_state(struct ncsi_rsp_pkt_hdr *nh)
 			ncsi_priv->state = NCSI_PROBE_CHANNEL;
 			return ncsi_probe_packages();
 		}
-		printf("NCSI: failed to select package 0x%0x2 or timeout\n",
+		printf("NCSI: failed to select package 0x%02x or timeout\n",
 		       ncsi_priv->current_package);
-		net_set_state(NETLOOP_FAIL);
+		ncsi_complete(-ENODEV);
 		break;
 	case NCSI_PROBE_CHANNEL:
 		// TODO only does package 0 for now
@@ -477,7 +497,8 @@ static void ncsi_update_state(struct ncsi_rsp_pkt_hdr *nh)
 			nc = ncsi_priv->current_channel;
 
 			/* Configure first channel that has link */
-			if (ncsi_priv->packages[np].channels[nc].has_link) {
+			if (nc < ncsi_priv->packages[np].n_channels &&
+			    ncsi_priv->packages[np].channels[nc].has_link && !timeout) {
 				ncsi_priv->state = NCSI_CONFIG;
 			} else if (ncsi_priv->current_channel + 1 <
 				   NCSI_CHANNEL_MAX) {
@@ -485,24 +506,24 @@ static void ncsi_update_state(struct ncsi_rsp_pkt_hdr *nh)
 			} else {
 				// XXX As above only package 0
 				printf("NCSI: no channel found with link\n");
-				net_set_state(NETLOOP_FAIL);
+				ncsi_complete(-ENETDOWN);
 				return;
 			}
 			return ncsi_probe_packages();
 		}
 		break;
 	case NCSI_CONFIG:
-		if (ncsi_priv->pending_requests == 0) {
-			printf("NCSI: configuration done!\n");
-			net_set_state(NETLOOP_SUCCESS);
-		} else if (timeout) {
+		if (timeout) {
 			printf("NCSI: timeout during configure\n");
-			net_set_state(NETLOOP_FAIL);
+			ncsi_complete(-ETIMEDOUT);
+		} else if (ncsi_priv->pending_requests == 0) {
+			printf("NCSI: configuration done!\n");
+			ncsi_complete(0);
 		}
 		break;
 	default:
 		printf("NCSI: something went very wrong, nevermind\n");
-		net_set_state(NETLOOP_FAIL);
+		ncsi_complete(-EINVAL);
 		break;
 	}
 }
@@ -523,11 +544,15 @@ static int ncsi_send_command(unsigned int np, unsigned int nc, unsigned int cmd,
 	int eth_hdr_size;
 	u32 checksum;
 	uchar *pkt, *start;
-	int final_len;
+	int final_len, ret;
 
-	pkt = calloc(1, PKTSIZE_ALIGN + PKTALIGN);
-	if (!pkt)
+	pkt = malloc_cache_aligned(PKTSIZE_ALIGN);
+	if (!pkt) {
+		if (wait)
+			ncsi_complete(-ENOMEM);
 		return -ENOMEM;
+	}
+	memset(pkt, 0, PKTSIZE_ALIGN);
 	start = pkt;
 
 	eth_hdr_size = net_set_ether(pkt, net_bcast_ethaddr, PROT_NCSI);
@@ -537,8 +562,10 @@ static int ncsi_send_command(unsigned int np, unsigned int nc, unsigned int cmd,
 	hdr = (struct ncsi_pkt_hdr *)pkt;
 	hdr->mc_id = 0;
 	hdr->revision = NCSI_PKT_REVISION;
-	hdr->id = ++ncsi_priv->last_request;
-	ncsi_priv->requests[ncsi_priv->last_request] = 1;
+	/* Instance ID zero is reserved for asynchronous events. */
+	if (!++ncsi_priv->last_request)
+		ncsi_priv->last_request = 1;
+	hdr->id = ncsi_priv->last_request;
 	hdr->type = cmd;
 	hdr->channel = NCSI_TO_CHANNEL(np, nc);
 	hdr->length = htons(len);
@@ -553,6 +580,8 @@ static int ncsi_send_command(unsigned int np, unsigned int nc, unsigned int cmd,
 	put_unaligned_be32(checksum, pchecksum);
 
 	if (wait) {
+		ncsi_priv->expected_type = cmd + 0x80;
+		ncsi_priv->expected_channel = hdr->channel;
 		net_set_timeout_handler(1000UL, ncsi_timeout_handler);
 		ncsi_priv->pending_requests++;
 	}
@@ -562,17 +591,18 @@ static int ncsi_send_command(unsigned int np, unsigned int nc, unsigned int cmd,
 	/* frame header, packet header, payload, checksum */
 	final_len = eth_hdr_size + sizeof(struct ncsi_cmd_pkt_hdr) + len + 4;
 
-	net_send_packet(start, final_len);
+	ret = eth_send(start, final_len);
 	free(start);
-	return 0;
+	if (ret < 0 && wait)
+		ncsi_complete(ret);
+	return ret < 0 ? ret : 0;
 }
 
-static void ncsi_handle_aen(struct ip_udp_hdr *ip, unsigned int len)
+static void ncsi_handle_aen(void *data, unsigned int len)
 {
-	struct ncsi_aen_pkt_hdr *hdr = (struct ncsi_aen_pkt_hdr *)ip;
-	int payload, i;
-	__be32 pchecksum;
-	u32 checksum;
+	struct ncsi_aen_pkt_hdr *hdr = data;
+	int payload;
+	u32 pchecksum, checksum;
 
 	switch (hdr->type) {
 	case NCSI_PKT_AEN_LSC:
@@ -599,9 +629,9 @@ static void ncsi_handle_aen(struct ip_udp_hdr *ip, unsigned int len)
 		return;
 	}
 
-	if (ntohs(hdr->common.length) != payload) {
+	if (ntohs(hdr->common.length) != payload || len < sizeof(*hdr) + payload) {
 		printf("NCSI: 0x%02x response has incorrect length %d\n",
-		       hdr->common.type, hdr->common.length);
+		       hdr->common.type, ntohs(hdr->common.length));
 		return;
 	}
 
@@ -617,39 +647,32 @@ static void ncsi_handle_aen(struct ip_udp_hdr *ip, unsigned int len)
 	}
 
 	/* Link or configuration lost - just redo the discovery process */
-	ncsi_priv->state = NCSI_PROBE_PACKAGE_SP;
-	for (i = 0; i < ncsi_priv->n_packages; i++) {
-		free(ncsi_priv->packages[i].channels);
-		ncsi_priv->packages[i].channels = NULL;
-	}
-	free(ncsi_priv->packages);
-	ncsi_priv->packages = NULL;
-	ncsi_priv->n_packages = 0;
-
-	ncsi_priv->current_package = NCSI_PACKAGE_MAX;
-	ncsi_priv->current_channel = NCSI_CHANNEL_MAX;
-
+	ncsi_reset_probe();
 	ncsi_probe_packages();
 }
 
-void ncsi_receive(struct ethernet_hdr *et, struct ip_udp_hdr *ip,
-		  unsigned int len)
+void ncsi_receive(void *data, unsigned int len)
 {
-	struct ncsi_rsp_pkt *pkt = (struct ncsi_rsp_pkt *)ip;
+	struct ncsi_rsp_pkt *pkt = data;
 	struct ncsi_rsp_pkt_hdr *nh = (struct ncsi_rsp_pkt_hdr *)&pkt->rsp;
 	void (*handler)(struct ncsi_rsp_pkt *pkt) = NULL;
 	unsigned short payload;
 
-	if (ncsi_priv->pending_requests)
-		ncsi_priv->pending_requests--;
+	if (!ncsi_priv)
+		return;
 
 	if (len < sizeof(struct ncsi_rsp_pkt_hdr)) {
 		printf("NCSI: undersized packet: %u bytes\n", len);
-		goto out;
+		return;
 	}
 
 	if (nh->common.type == NCSI_PKT_AEN)
-		return ncsi_handle_aen(ip, len);
+		return ncsi_handle_aen(data, len);
+
+	if (!ncsi_priv->pending_requests || nh->common.id != ncsi_priv->last_request ||
+	    nh->common.type != ncsi_priv->expected_type ||
+	    nh->common.channel != ncsi_priv->expected_channel)
+		return;
 
 	switch (nh->common.type) {
 	case NCSI_PKT_RSP_SP:
@@ -699,19 +722,20 @@ void ncsi_receive(struct ethernet_hdr *et, struct ip_udp_hdr *ip,
 	default:
 		printf("NCSI: unsupported packet type 0x%02x\n",
 		       nh->common.type);
-		goto out;
+		return;
 	}
 
-	if (ncsi_validate_rsp(pkt, payload) != 0) {
+	if (len < sizeof(*nh) + payload || ncsi_validate_rsp(pkt, payload)) {
 		printf("NCSI: discarding invalid packet of type 0x%02x\n",
 		       nh->common.type);
-		goto out;
+		return;
 	}
 
+	ncsi_priv->pending_requests--;
 	if (handler)
 		handler(pkt);
-out:
-	ncsi_update_state(nh);
+	if (ncsi_priv->result == -EINPROGRESS)
+		ncsi_update_state(nh);
 }
 
 static void ncsi_send_sp(unsigned int np)
@@ -773,6 +797,7 @@ static void ncsi_send_sma(unsigned int np, unsigned int nc)
 	addr = eth_get_ethaddr();
 	if (!addr) {
 		printf("NCSI: no MAC address configured\n");
+		ncsi_complete(-EADDRNOTAVAIL);
 		return;
 	}
 
@@ -790,9 +815,6 @@ static void ncsi_send_sma(unsigned int np, unsigned int nc)
 
 void ncsi_probe_packages(void)
 {
-	struct ncsi_package *package;
-	unsigned int np, nc;
-
 	if (!ncsi_priv) {
 		printf("NCSI: no active interface\n");
 		net_set_state(NETLOOP_FAIL);
@@ -820,24 +842,8 @@ void ncsi_probe_packages(void)
 			      ncsi_priv->current_channel);
 		break;
 	case NCSI_CONFIG:
-		for (np = 0; np < ncsi_priv->n_packages; np++) {
-			package = &ncsi_priv->packages[np];
-			for (nc = 0; nc < package->n_channels; nc++)
-				if (package->channels[nc].has_link)
-					break;
-			if (nc < package->n_channels)
-				break;
-		}
-		if (np == ncsi_priv->n_packages) {
-			printf("NCSI: no link available\n");
-			return;
-		}
-
-		printf("NCSI: configuring channel %d\n", nc);
-		ncsi_priv->current_package = np;
-		ncsi_priv->current_channel = nc;
-		/* Kicks off rest of configure chain */
-		ncsi_send_sma(np, nc);
+		printf("NCSI: configuring channel %u\n", ncsi_priv->current_channel);
+		ncsi_send_sma(ncsi_priv->current_package, ncsi_priv->current_channel);
 		break;
 	default:
 		printf("NCSI: unknown state 0x%x\n", ncsi_priv->state);
@@ -853,8 +859,6 @@ int ncsi_probe(struct phy_device *phydev)
 		memset(phydev->priv, 0, sizeof(struct ncsi));
 	}
 
-	ncsi_priv = phydev->priv;
-
 	return 0;
 }
 
@@ -866,27 +870,34 @@ int ncsi_startup(struct phy_device *phydev)
 	/* Normal phy reset is N/A */
 	phydev->flags |= PHY_FLAG_BROKEN_RESET;
 
-	/* Set initial probe state */
-	ncsi_priv->state = NCSI_PROBE_PACKAGE_SP;
-
-	/* No active package/channel yet */
-	ncsi_priv->current_package = NCSI_PACKAGE_MAX;
-	ncsi_priv->current_channel = NCSI_CHANNEL_MAX;
-
 	/* Pretend link works so the MAC driver sets final bits up */
 	phydev->link = true;
 
 	/* Set ncsi_priv so we can use it when called from net_loop() */
 	ncsi_priv = phydev->priv;
+	ncsi_reset_probe();
 
 	return 0;
 }
 
 int ncsi_shutdown(struct phy_device *phydev)
 {
-	printf("NCSI: Disabling package %d\n", ncsi_priv->current_package);
-	ncsi_send_dp(ncsi_priv->current_package);
-	return 0;
+	int ret = 0;
+
+	if (!ncsi_priv || ncsi_priv != phydev->priv)
+		return 0;
+#if CONFIG_IS_ENABLED(NET_LEGACY)
+	if (ncsi_priv->pending_requests)
+		net_set_timeout_handler(0, NULL);
+#endif
+	if (ncsi_priv->current_package < ncsi_priv->n_packages) {
+		printf("NCSI: Disabling package %d\n", ncsi_priv->current_package);
+		ret = ncsi_send_command(ncsi_priv->current_package, NCSI_RESERVED_CHANNEL,
+					NCSI_PKT_CMD_DP, NULL, 0, false);
+	}
+	ncsi_reset_probe();
+	ncsi_priv = NULL;
+	return ret;
 }
 
 U_BOOT_PHY_DRIVER(ncsi) = {
include/net/ncsi.h +1 -2
index 7aec457562d..42405992da3 100644
@@ -8,6 +8,5 @@
 #include <phy.h>
 
 bool ncsi_active(void);
-void ncsi_receive(struct ethernet_hdr *et, struct ip_udp_hdr *ip,
-		  unsigned int len);
+void ncsi_receive(void *data, unsigned int len);
 void ncsi_probe_packages(void);
net/net.c +1 -1
index a249a674236..30ce04893be 100644
@@ -1450,7 +1450,7 @@ void net_process_received_packet(uchar *in_packet, int len)
 #endif
 #ifdef CONFIG_PHY_NCSI
 	case PROT_NCSI:
-		ncsi_receive(et, ip, len);
+		ncsi_receive(ip, len);
 		break;
 #endif
 	}
test/dm/Makefile +3 -0
index 5448be2267a..c926ec3958f 100644
@@ -73,6 +73,9 @@ endif
 ifeq ($(CONFIG_NET_LWIP)$(CONFIG_CMD_RARP),yy)
 obj-y += lwip_rarp.o
 endif
+ifeq ($(CONFIG_CMD_PING),y)
+obj-$(CONFIG_PHY_NCSI) += ncsi.o
+endif
 ifeq ($(CONFIG_NET_LWIP)$(CONFIG_CMD_TFTPBOOT),yy)
 obj-y += lwip_tftp.o
 obj-y += lwip_net_loop.o
test/dm/ncsi.c new file +141 -0

141 lines not on this page · raw message

Signature · 1 line
2.53.0
#19 lore raw

PATCH 19/19 net: lwip: configure NC-SI before starting network clients

In reply to James Hilliard

On an NC-SI port, the management controller must enable a channel before IP traffic can reach the network. Use the shared NC-SI state machine to configure the channel before bringing up the lwIP interface. Dispatch management frames separately from IP and track their timeouts independently. The ncsi command can also bring up the channel without an IP transfer.

A Configuration Required event can start discovery again while a client still holds the interface. Check the channel before attaching another client or changing its addressing. Reject reentrant attachment during discovery and leave existing clients and their addresses alone if discovery fails.

Do not let a retry stop or rotate an interface another client is using. If Ctrl-C interrupts initial discovery, return from ping without trying the next interface.

Exercise capability-query timeouts, request-ID rollover, failed channel enable, shared-client recovery, callback reentry and cancellation with interface rotation enabled.

---
 cmd/lwip/Makefile       |   1 +
 cmd/lwip/ncsi.c         |  23 +++++
 cmd/lwip/ping.c         |   2 +-
 drivers/net/phy/Kconfig |   2 +-
 drivers/net/phy/ncsi.c  |  55 ++++++++++++
 include/net/ncsi.h      |   2 +
 net/lwip/net-lwip.c     |  70 ++++++++++++---
 test/dm/Makefile        |   2 +-
 test/dm/ncsi.c          | 230 +++++++++++++++++++++++++++++++++++++++++++++++-
 9 files changed, 372 insertions(+), 15 deletions(-)
cmd/lwip/Makefile +1 -0
index 90ad4d21e6f..43a6b29e687 100644
@@ -2,6 +2,7 @@ obj-$(CONFIG_CMD_BOOTP) += bootp.o
 obj-$(CONFIG_CMD_DHCP) += dhcp.o
 obj-$(CONFIG_CMD_DNS) += dns.o
 obj-$(CONFIG_CMD_LINK_LOCAL) += linklocal.o
+obj-$(CONFIG_CMD_NCSI) += ncsi.o
 obj-$(CONFIG_CMD_NFS) += nfs.o
 ifneq ($(filter y,$(CONFIG_CMD_PING) $(CONFIG_CMD_PING6)),)
 obj-y += ping.o
cmd/lwip/ncsi.c new file +23 -0
new file mode 100644
index 00000000000..3b6b257977d
@@ -0,0 +1,23 @@
+// SPDX-License-Identifier: GPL-2.0+
+
+#include <command.h>
+#include <net.h>
+#include <net/ncsi.h>
+
+static int do_ncsi(struct cmd_tbl *cmdtp, int flag, int argc,
+		   char *const argv[])
+{
+	struct net_lwip_ctx ctx = {};
+	int ret;
+
+	ret = net_lwip_start(&ctx, NET_LWIP_ADDR_ENV_FLEXIBLE);
+	if (ret)
+		return CMD_RET_FAILURE;
+
+	ret = !phy_interface_is_ncsi() || !ncsi_active();
+	net_lwip_stop(&ctx);
+
+	return ret ? CMD_RET_FAILURE : CMD_RET_SUCCESS;
+}
+
+U_BOOT_CMD(ncsi, 1, 1, do_ncsi, "configure the NC-SI interface", "");
cmd/lwip/ping.c +1 -1
index 31c9e0ad4d6..f50765cbf0f 100644
@@ -183,7 +183,7 @@ static int ping_execute(const char *name, u8 type)
 				    NET_LWIP_ADDR_ENV_STRICT);
 		if (!ret)
 			break;
-	} while (!net_start_again());
+	} while (ret != -EINTR && !net_start_again());
 	if (ret)
 		return CMD_RET_FAILURE;
 
drivers/net/phy/Kconfig +1 -1
index 02aeb1db977..ed5c52039a6 100644
@@ -389,7 +389,7 @@ config PHY_FIXED
 
 config PHY_NCSI
 	bool "NC-SI based PHY"
-	depends on NET_LEGACY
+	depends on NET
 
 endif #PHYLIB
 
drivers/net/phy/ncsi.c +55 -0

105 lines not on this page · raw message

include/net/ncsi.h +2 -0
index 42405992da3..619c08a4e18 100644
@@ -10,3 +10,5 @@
 bool ncsi_active(void);
 void ncsi_receive(void *data, unsigned int len);
 void ncsi_probe_packages(void);
+void ncsi_poll_timeout(void);
+int ncsi_lwip_configure(void);
net/lwip/net-lwip.c +59 -11

138 lines not on this page · raw message

test/dm/Makefile +1 -1
index c926ec3958f..a869371f305 100644
@@ -73,7 +73,7 @@ endif
 ifeq ($(CONFIG_NET_LWIP)$(CONFIG_CMD_RARP),yy)
 obj-y += lwip_rarp.o
 endif
-ifeq ($(CONFIG_CMD_PING),y)
+ifneq ($(filter y,$(CONFIG_NET_LWIP) $(CONFIG_CMD_PING)),)
 obj-$(CONFIG_PHY_NCSI) += ncsi.o
 endif
 ifeq ($(CONFIG_NET_LWIP)$(CONFIG_CMD_TFTPBOOT),yy)
test/dm/ncsi.c +229 -1

287 lines not on this page · raw message

Signature · 1 line
2.53.0
#20 lore raw
In reply to James Hilliard
Quoted 85 lines

Boards booting over Ethernet need a working link and an address before they can fetch an image. Add IPv4 link-local addressing and reverse ARP, honor the configured VLAN, and let boot scripts obtain the Ethernet address used to reach the boot server. CDP can supply the VLAN settings; on NC-SI ports, bring up the sideband channel before starting an IP client. Both management protocols receive their frames before ordinary VLAN filtering.

For NFS boot, use a reserved source port, restart a timed-out download according to netretry, and honor autostart after a nonempty download. SPL uses BOOTP or DHCP followed by TFTP and loads the image using the received file length.

---
James Hilliard (19):
env: invalidate caches after bulk imports
net: lwip: support the IPv4 linklocal command
net: lwip: apply the configured Ethernet VLAN
net: lwip: export the Ethernet address used to reach serverip
net: cdp: decode advertisements from the complete Ethernet frame
net: cdp: calculate checksums from network-order bytes
net: lwip: discover VLAN settings with CDP
net: lwip: bind NFS requests to a reserved UDP port
net: lwip: honor autostart after an NFS download
net: lwip: restart NFS after an RPC timeout
net: lwip: support network boot with reverse ARP
boot: pxe: allow boot without the DHCP command
spl: use the known file length for raw U-Boot images
spl: fit: read the exact size of an embedded FIT
spl: net: bound reads to the downloaded file
spl: net: load a boot image with lwIP
net: ncsi: check the selected interface before probing
net: ncsi: track the outstanding management command
net: lwip: configure NC-SI before starting network clients

 board/sunxi/board.c          |   1 +
 boot/bootmeth_pxe.c          |   8 +-
 cmd/Kconfig                  |   8 +-
 cmd/lwip/Makefile            |   2 +
 cmd/lwip/linklocal.c         |  71 +++++++++
 cmd/lwip/ncsi.c              |  23 +++
 cmd/lwip/ping.c              |   2 +-
 cmd/nvedit.c                 |   7 +-
 common/spl/Kconfig           |  10 +-
 common/spl/spl.c             |  49 +++---
 common/spl/spl_fit.c         |   9 +-
 common/spl/spl_nand.c        |   3 +-
 common/spl/spl_net.c         |  23 ++-
 drivers/net/phy/Kconfig      |   2 +-
 drivers/net/phy/ncsi.c       | 302 +++++++++++++++++++++--------------
 env/common.c                 |  25 ++-
 include/net-lwip.h           |   2 +
 include/net/ncsi.h           |   5 +-
 include/spl.h                |  20 ++-
 include/spl_load.h           |   2 +-
 lib/Makefile                 |   2 +-
 lib/lwip/u-boot/lwip-hooks.h |  10 ++
 lib/lwip/u-boot/lwipopts.h   |  19 +--
 net/Kconfig                  |   8 +-
 net/Makefile                 |   4 +-
 net/cdp.c                    | 311 ++++++++++++++----------------------
 net/cdp.h                    |  17 +-
 net/lwip/Makefile            |   7 +
 net/lwip/cdp.c               |  98 ++++++++++++
 net/lwip/dhcp.c              |  71 ++++-----
 net/lwip/net-lwip.c          | 188 ++++++++++++++++++++--
 net/lwip/nfs.c               |  77 ++++++---
 net/lwip/rarp.c              | 181 +++++++++++++++++++++
 net/lwip/wol.c               |   7 +
 net/net.c                    |  79 ++++-----
 net/nfs-common.c             |   5 +
 net/nfs-common.h             |   1 +
 test/dm/Makefile             |   9 ++
 test/dm/eth.c                |  81 ++++++++++
 test/dm/lwip.c               | 233 +++++++++++++++++++++++++++
 test/dm/lwip_cdp.c           | 117 ++++++++++++++
 test/dm/lwip_dhcp.c          |  23 ++-
 test/dm/lwip_ipv6.c          | 116 +++++++++++++-
 test/dm/lwip_rarp.c          | 139 ++++++++++++++++
 test/dm/lwip_test.h          |  42 +++++
 test/dm/ncsi.c               | 369 +++++++++++++++++++++++++++++++++++++++++++
 test/image/spl_load.c        | 113 +++++++++++++
 test/image/spl_load_net.c    |  43 ++++-
 48 files changed, 2444 insertions(+), 500 deletions(-)
Quoted 8 lines

---
base-commit: d778970a189b977c14f93961d7009f404d895b5b
base-tree: https://git.u-boot-project.org/u-boot/custodians/u-boot-net.git for-next
change-id: 20261123-submit-lwip-network-boot-v1-80f73f7fa642

Best regards,

Signature · 1 line
James Hilliard <[email protected]>

Applied to u-boot-net branch for-next [1] with local modifications, thanks!

[1] https://git.u-boot-project.org/u-boot/custodians/u-boot-net/-/commits/for-next

[1/19] env: invalidate caches after bulk imports

      commit: 28fdbaca10aea91f21384afdbf255a730eb95b49

[2/19] net: lwip: support the IPv4 linklocal command

      commit: a8fa1eafd30168a2bad0ed291ec78ef841fe242c
      Note: Applied with local modifications.
      [jf: add linklocal_tick() for 100x faster simulated time]

[3/19] net: lwip: apply the configured Ethernet VLAN

      commit: d56488eac9f57e776eb08df14d48bba4a460b95d

[4/19] net: lwip: export the Ethernet address used to reach serverip

      commit: 724ec9be6c38fd477a9d8ba557fb04eb366fb9e9

[5/19] net: cdp: decode advertisements from the complete Ethernet frame

      commit: 59cf892d054d06cfce65df893bae0008a3fed462

[6/19] net: cdp: calculate checksums from network-order bytes

      commit: c7d5fc75773dc0d1cc068c67381685bf58599be0

[7/19] net: lwip: discover VLAN settings with CDP
commit: 036c8faec682e116bf3c24c2ebdd5c3e34bd0587
Note: Applied with local modifications.
[jf: make lwIP VLAN support optional and disable it on rcar3_salvator-x to keep its image below the size limit]

[8/19] net: lwip: bind NFS requests to a reserved UDP port

      commit: 3dac8d7499e22aa1b79e262c689622b845332469

[9/19] net: lwip: honor autostart after an NFS download

      commit: 05c4044398e862b39620f7c91d6b04c2243196ef

[10/19] net: lwip: restart NFS after an RPC timeout

      commit: 427cbd1487916ac8f4dc0b6e89d5d55d501f2f1f

[11/19] net: lwip: support network boot with reverse ARP

      commit: d31c3c497b0d318d0da32d200437ddec87269dd7

[12/19] boot: pxe: allow boot without the DHCP command

      commit: 9968294b38487b5a166e6c9fc2802a755b5a368c

[13/19] spl: use the known file length for raw U-Boot images
commit: f0c3269d06bd536dcf9ba851f395893148fd0363
Note: Applied with local modifications.
[jf: fix two forgotten spl_set_header_raw_uboot() callers]

[14/19] spl: fit: read the exact size of an embedded FIT
commit: 7b4a770337813f52b73c940b4c46a15886114bfe
Note: Applied with local modifications. [jf: fix trivial conflict]

[15/19] spl: net: bound reads to the downloaded file

      commit: 3192c708c6f742297f995669b8ee4911ec72b4c6

[16/19] spl: net: load a boot image with lwIP

      commit: 86a9c0af38656dfdc0cf7a6bf9d8e95ea8e4938a

[17/19] net: ncsi: check the selected interface before probing

      commit: 2df0ec64c3bd5e1ff116aee3cd67f79053adfffe

[18/19] net: ncsi: track the outstanding management command

      commit: 28da3645b3aee11782416819bc1ac2d72ac1e7b8

[19/19] net: lwip: configure NC-SI before starting network clients

      commit: 2531adc12c3d549bed5ebbec17cc51bb0412aa73

Best regards,

Signature · 1 line
Jerome Forissier <[email protected]>