Skip to contentSkip to conversation

net: new command: wol - Wake on LAN

PATCH In review Lothar Felten 2 participants 3 messages Jun 22, 2018 – Jul 2, 2018 Networking

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 net: new command: wol - Wake on LAN

Add a new command 'wol': Wait for an incoming Wake-on-LAN packet or time out if no WoL packed is received.
If the WoL packet contains a password, it is saved in the environment variable 'wolpassword' using the etherwake format (dot or colon separated decimals).

Intended use case: a networked device should boot an alternate image. It's attached to a network on a client site, modifying the DHCP server configuration or setup of a tftp server is not allowed. After power on the device waits a few seconds for a WoL packet. If a packet is received, the device boots the alternate image. Otherwise it boots the default image.

This method is a simple way to interact with a system via network even if only the MAC address is known. Tools to send WoL packets are available on all common platforms.

Some ethernet drivers seem to pad the incoming packet. The additional padding bytes might be recognized as Wake-on-LAN password bytes.

By default enabled in pengwyn_defconfig.

---
Changes v1 -> v2:
- UDP support (suggested by Joe Hershberger)
- moved to separate command file cmd/wol.c
- style and formatting

 cmd/Kconfig               |  5 +++
 cmd/Makefile              |  1 +
 cmd/wol.c                 | 33 ++++++++++++++++
 configs/pengwyn_defconfig |  1 +
 include/net.h             |  3 +-
 net/Makefile              |  1 +
 net/net.c                 | 19 ++++++++++
 net/wol.c                 | 96 +++++++++++++++++++++++++++++++++++++++++++++++
 net/wol.h                 | 65 ++++++++++++++++++++++++++++++++
 9 files changed, 223 insertions(+), 1 deletion(-)
 create mode 100644 cmd/wol.c
 create mode 100644 net/wol.c
 create mode 100644 net/wol.h
cmd/Kconfig +5 -0
index 1eb55e5250..00c208843d 100644
@@ -1239,6 +1239,11 @@ config CMD_PXE
 	help
 	  Boot image via network using PXE protocol
 
+config CMD_WOL
+	bool "wol"
+	help
+	  Wait for wake-on-lan Magic Packet
+
 endif
 
 menu "Misc commands"
cmd/Makefile +1 -0
index e0088df33b..2875fd4b4b 100644
@@ -100,6 +100,7 @@ obj-$(CONFIG_CMD_PCI) += pci.o
 endif
 obj-y += pcmcia.o
 obj-$(CONFIG_CMD_PXE) += pxe.o
+obj-$(CONFIG_CMD_WOL) += wol.o
 obj-$(CONFIG_CMD_QFW) += qfw.o
 obj-$(CONFIG_CMD_READ) += read.o
 obj-$(CONFIG_CMD_REGINFO) += reginfo.o
cmd/wol.c new file +33 -0
new file mode 100644
index 0000000000..91b3929025
@@ -0,0 +1,33 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * (C) Copyright 2018
+ * Lothar Felte, lothar.felten at gmail.com
+ */
+
+/*
+ * Wake-on-LAN support
+ */
+#include <common.h>
+#include <command.h>
+#include <net.h>
+
+#if defined(CONFIG_CMD_WOL)
+void wol_set_timeout(ulong);
+
+int do_wol(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+{
+	/* Validate arguments */
+	if (argc < 2)
+		return CMD_RET_USAGE;
+	wol_set_timeout(simple_strtol(argv[1], NULL, 10) * 1000);
+	if (net_loop(WOL) < 0)
+		return CMD_RET_FAILURE;
+	return CMD_RET_SUCCESS;
+}
+
+U_BOOT_CMD(
+	wol,	2,	1,	do_wol,
+	"wait for an incoming wake-on-lan packet",
+	"Timeout"
+);
+#endif
configs/pengwyn_defconfig +1 -0
index 0ee8e6e1b9..76b5715a99 100644
@@ -37,6 +37,7 @@ CONFIG_CMD_MMC=y
 CONFIG_CMD_NAND=y
 CONFIG_CMD_SPI=y
 CONFIG_CMD_USB=y
+CONFIG_CMD_WOL=y
 # CONFIG_CMD_SETEXPR is not set
 CONFIG_CMD_EXT4_WRITE=y
 CONFIG_CMD_MTDPARTS=y
include/net.h +2 -1
index 5760685556..9c7199aca9 100644
@@ -344,6 +344,7 @@ struct vlan_ethernet_hdr {
 
 #define PROT_IP		0x0800		/* IP protocol			*/
 #define PROT_ARP	0x0806		/* IP ARP protocol		*/
+#define PROT_WOL	0x0842		/* ether-wake WoL protocol	*/
 #define PROT_RARP	0x8035		/* IP ARP protocol		*/
 #define PROT_VLAN	0x8100		/* IEEE 802.1q protocol		*/
 #define PROT_IPV6	0x86dd		/* IPv6 over bluebook		*/
@@ -535,7 +536,7 @@ extern int		net_restart_wrap;	/* Tried all network devices */
 
 enum proto_t {
 	BOOTP, RARP, ARP, TFTPGET, DHCP, PING, DNS, NFS, CDP, NETCONS, SNTP,
-	TFTPSRV, TFTPPUT, LINKLOCAL, FASTBOOT
+	TFTPSRV, TFTPPUT, LINKLOCAL, FASTBOOT, WOL
 };
 
 extern char	net_boot_file_name[1024];/* Boot File name */
net/Makefile +1 -0
index 07466879f5..c3699d309d 100644
@@ -23,6 +23,7 @@ obj-$(CONFIG_CMD_PING) += ping.o
 obj-$(CONFIG_CMD_RARP) += rarp.o
 obj-$(CONFIG_CMD_SNTP) += sntp.o
 obj-$(CONFIG_CMD_TFTPBOOT) += tftp.o
+obj-$(CONFIG_CMD_WOL)  += wol.o
 obj-$(CONFIG_UDP_FUNCTION_FASTBOOT)  += fastboot.o
 
 # Disable this warning as it is triggered by:
net/net.c +19 -0
index b4563a4cab..084269e31e 100644
@@ -78,6 +78,12 @@
  *			- own IP address
  *	We want:	- network time
  *	Next step:	none
+ *
+ * WOL:
+ *
+ *	Prerequisites:	- own ethernet address
+ *	We want:	- magic packet or timeout
+ *	Next step:	none
  */
 
 
@@ -108,6 +114,9 @@
 #if defined(CONFIG_CMD_SNTP)
 #include "sntp.h"
 #endif
+#if defined(CONFIG_CMD_WOL)
+#include "wol.h"
+#endif
 
 /** BOOTP EXTENTIONS **/
 
@@ -514,6 +523,11 @@ restart:
 		case LINKLOCAL:
 			link_local_start();
 			break;
+#endif
+#if defined(CONFIG_CMD_WOL)
+		case WOL:
+			wol_start();
+			break;
 #endif
 		default:
 			break;
@@ -1281,6 +1295,11 @@ void net_process_received_packet(uchar *in_packet, int len)
 				      ntohs(ip->udp_src),
 				      ntohs(ip->udp_len) - UDP_HDR_SIZE);
 		break;
+#ifdef CONFIG_CMD_WOL
+	case PROT_WOL:
+		wol_receive(ip, len);
+		break;
+#endif
 	}
 }
 
net/wol.c new file +96 -0

96 lines not on this page · raw message

net/wol.h new file +65 -0

65 lines not on this page · raw message

Signature · 1 line
2.14.1
#1 lore raw

PATCH v2 1/1 net: new command: wol - Wake on LAN

Hi Lothar,

Quoted 17 lines · Lothar Felten

Add a new command 'wol': Wait for an incoming Wake-on-LAN packet or time out if no WoL packed is received.
If the WoL packet contains a password, it is saved in the environment variable 'wolpassword' using the etherwake format (dot or colon separated decimals).

Intended use case: a networked device should boot an alternate image. It's attached to a network on a client site, modifying the DHCP server configuration or setup of a tftp server is not allowed. After power on the device waits a few seconds for a WoL packet. If a packet is received, the device boots the alternate image. Otherwise it boots the default image.

This method is a simple way to interact with a system via network even if only the MAC address is known. Tools to send WoL packets are available on all common platforms.

Quoted 6 lines

Some ethernet drivers seem to pad the incoming packet. The additional padding bytes might be recognized as Wake-on-LAN password bytes.

By default enabled in pengwyn_defconfig.

Looks great!