+// SPDX-License-Identifier: GPL-2.0
/*
* Copied from Linux Monitor (LiMon) - Networking.
*
* Copyright 2000 Roland Borde
* Copyright 2000 Paolo Scaffardi
* Copyright 2000-2002 Wolfgang Denk, wd@denx.de
- * SPDX-License-Identifier: GPL-2.0
*/
/*
* - own IP address
* We want: - network time
* Next step: none
+ *
+ * WOL:
+ *
+ * Prerequisites: - own ethernet address
+ * We want: - magic packet or timeout
+ * Next step: none
*/
#include <environment.h>
#include <errno.h>
#include <net.h>
+#include <net/fastboot.h>
#include <net/tftp.h>
-#if defined(CONFIG_STATUS_LED)
+#if defined(CONFIG_LED_STATUS)
#include <miiphy.h>
#include <status_led.h>
#endif
#if defined(CONFIG_CMD_SNTP)
#include "sntp.h"
#endif
-
-DECLARE_GLOBAL_DATA_PTR;
+#if defined(CONFIG_CMD_WOL)
+#include "wol.h"
+#endif
/** BOOTP EXTENTIONS **/
struct in_addr net_dns_server2;
#endif
-#ifdef CONFIG_MCAST_TFTP /* Multicast TFTP */
-struct in_addr net_mcast_addr;
-#endif
-
/** END OF BOOTP EXTENTIONS **/
/* Our ethernet address */
/* Ethernet bcast address */
const u8 net_bcast_ethaddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
const u8 net_null_ethaddr[6];
-#ifdef CONFIG_API
+#if defined(CONFIG_API) || defined(CONFIG_EFI_LOADER)
void (*push_packet)(void *, int len) = 0;
#endif
/* Network loop state */
/* Boot File name */
char net_boot_file_name[1024];
+/* Indicates whether the file name was specified on the command line */
+bool net_boot_file_name_explicit;
/* The actual transferred size of the bootfile (in bytes) */
u32 net_boot_file_size;
/* Boot file size in blocks as reported by the DHCP server */
/**********************************************************************/
-static int on_bootfile(const char *name, const char *value, enum env_op op,
- int flags)
-{
- if (flags & H_PROGRAMMATIC)
- return 0;
-
- switch (op) {
- case env_op_create:
- case env_op_overwrite:
- copy_filename(net_boot_file_name, value,
- sizeof(net_boot_file_name));
- break;
- default:
- break;
- }
-
- return 0;
-}
-U_BOOT_ENV_CALLBACK(bootfile, on_bootfile);
-
static int on_ipaddr(const char *name, const char *value, enum env_op op,
int flags)
{
void net_auto_load(void)
{
#if defined(CONFIG_CMD_NFS)
- const char *s = getenv("autoload");
+ const char *s = env_get("autoload");
if (s != NULL && strcmp(s, "NFS") == 0) {
+ if (net_check_prereq(NFS)) {
+/* We aren't expecting to get a serverip, so just accept the assigned IP */
+#ifdef CONFIG_BOOTP_SERVERIP
+ net_set_state(NETLOOP_SUCCESS);
+#else
+ printf("Cannot autoload with NFS\n");
+ net_set_state(NETLOOP_FAIL);
+#endif
+ return;
+ }
/*
* Use NFS to load the bootfile.
*/
return;
}
#endif
- if (getenv_yesno("autoload") == 0) {
+ if (env_get_yesno("autoload") == 0) {
/*
* Just use BOOTP/RARP to configure system;
* Do not use TFTP to load the bootfile.
net_set_state(NETLOOP_SUCCESS);
return;
}
+ if (net_check_prereq(TFTPGET)) {
+/* We aren't expecting to get a serverip, so just accept the assigned IP */
+#ifdef CONFIG_BOOTP_SERVERIP
+ net_set_state(NETLOOP_SUCCESS);
+#else
+ printf("Cannot autoload with TFTPGET\n");
+ net_set_state(NETLOOP_FAIL);
+#endif
+ return;
+ }
tftp_start(TFTPGET);
}
int net_loop(enum proto_t protocol)
{
int ret = -EINVAL;
+ enum net_loop_state prev_net_state = net_state;
net_restarted = 0;
net_dev_exists = 0;
case 1:
/* network not configured */
eth_halt();
+ net_set_state(prev_net_state);
return -ENODEV;
case 2:
tftp_start_server();
break;
#endif
+#ifdef CONFIG_UDP_FUNCTION_FASTBOOT
+ case FASTBOOT:
+ fastboot_start_server();
+ break;
+#endif
#if defined(CONFIG_CMD_DHCP)
case DHCP:
bootp_reset();
cdp_start();
break;
#endif
-#if defined(CONFIG_NETCONSOLE) && !(CONFIG_SPL_BUILD)
+#if defined(CONFIG_NETCONSOLE) && !defined(CONFIG_SPL_BUILD)
case NETCONS:
nc_start();
break;
case LINKLOCAL:
link_local_start();
break;
+#endif
+#if defined(CONFIG_CMD_WOL)
+ case WOL:
+ wol_start();
+ break;
#endif
default:
break;
#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
#if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \
- defined(CONFIG_STATUS_LED) && \
- defined(STATUS_LED_RED)
+ defined(CONFIG_LED_STATUS) && \
+ defined(CONFIG_LED_STATUS_RED)
/*
* Echo the inverted link state to the fault LED.
*/
if (miiphy_link(eth_get_dev()->name, CONFIG_SYS_FAULT_MII_ADDR))
- status_led_set(STATUS_LED_RED, STATUS_LED_OFF);
+ status_led_set(CONFIG_LED_STATUS_RED, CONFIG_LED_STATUS_OFF);
else
- status_led_set(STATUS_LED_RED, STATUS_LED_ON);
+ status_led_set(CONFIG_LED_STATUS_RED, CONFIG_LED_STATUS_ON);
#endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
#endif /* CONFIG_MII, ... */
#ifdef CONFIG_USB_KEYBOARD
#ifdef CONFIG_SHOW_ACTIVITY
show_activity(1);
#endif
+ if (arp_timeout_check() > 0)
+ time_start = get_timer(0);
+
/*
* Check the ethernet for a new packet. The ethernet
* receive routine will process it.
goto done;
}
- if (arp_timeout_check() > 0) {
- time_start = get_timer(0);
- }
-
/*
* Check for a timeout, and run the timeout handler
* if we have one.
#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
#if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \
- defined(CONFIG_STATUS_LED) && \
- defined(STATUS_LED_RED)
+ defined(CONFIG_LED_STATUS) && \
+ defined(CONFIG_LED_STATUS_RED)
/*
* Echo the inverted link state to the fault LED.
*/
if (miiphy_link(eth_get_dev()->name,
CONFIG_SYS_FAULT_MII_ADDR))
- status_led_set(STATUS_LED_RED, STATUS_LED_OFF);
+ status_led_set(CONFIG_LED_STATUS_RED,
+ CONFIG_LED_STATUS_OFF);
else
- status_led_set(STATUS_LED_RED, STATUS_LED_ON);
+ status_led_set(CONFIG_LED_STATUS_RED,
+ CONFIG_LED_STATUS_ON);
#endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
#endif /* CONFIG_MII, ... */
debug_cond(DEBUG_INT_STATE, "--- net_loop timeout\n");
if (net_boot_file_size > 0) {
printf("Bytes transferred = %d (%x hex)\n",
net_boot_file_size, net_boot_file_size);
- setenv_hex("filesize", net_boot_file_size);
- setenv_hex("fileaddr", load_addr);
+ env_set_hex("filesize", net_boot_file_size);
+ env_set_hex("fileaddr", load_addr);
}
if (protocol != NETCONS)
eth_halt();
/* Invalidate the last protocol */
eth_set_last_protocol(BOOTP);
debug_cond(DEBUG_INT_STATE, "--- net_loop Fail!\n");
+ ret = -ENONET;
goto done;
case NETLOOP_CONTINUE:
net_set_udp_handler(NULL);
net_set_icmp_handler(NULL);
#endif
+ net_set_state(prev_net_state);
return ret;
}
unsigned long retrycnt = 0;
int ret;
- nretry = getenv("netretry");
+ nretry = env_get("netretry");
if (nretry) {
if (!strcmp(nretry, "yes"))
retry_forever = 1;
retry_forever = 0;
}
- if ((!retry_forever) && (net_try_count >= retrycnt)) {
+ if ((!retry_forever) && (net_try_count > retrycnt)) {
eth_halt();
net_set_state(NETLOOP_FAIL);
/*
}
}
+uchar *net_get_async_tx_pkt_buf(void)
+{
+ if (arp_is_waiting())
+ return arp_tx_packet; /* If we are waiting, we already sent */
+ else
+ return net_tx_packet;
+}
+
int net_send_udp_packet(uchar *ether, struct in_addr dest, int dport, int sport,
int payload_len)
+{
+ return net_send_ip_packet(ether, dest, dport, sport, payload_len,
+ IPPROTO_UDP, 0, 0, 0);
+}
+
+int net_send_ip_packet(uchar *ether, struct in_addr dest, int dport, int sport,
+ int payload_len, int proto, u8 action, u32 tcp_seq_num,
+ u32 tcp_ack_num)
{
uchar *pkt;
int eth_hdr_size;
pkt = (uchar *)net_tx_packet;
eth_hdr_size = net_set_ether(pkt, ether, PROT_IP);
- pkt += eth_hdr_size;
- net_set_udp_header(pkt, dest, dport, sport, payload_len);
- pkt_hdr_size = eth_hdr_size + IP_UDP_HDR_SIZE;
+
+ switch (proto) {
+ case IPPROTO_UDP:
+ net_set_udp_header(pkt + eth_hdr_size, dest, dport, sport,
+ payload_len);
+ pkt_hdr_size = eth_hdr_size + IP_UDP_HDR_SIZE;
+ break;
+ default:
+ return -EINVAL;
+ }
/* if MAC address was not discovered yet, do an ARP request */
if (memcmp(ether, net_null_ethaddr, 6) == 0) {
#ifndef CONFIG_NET_MAXDEFRAG
#define CONFIG_NET_MAXDEFRAG 16384
#endif
-/*
- * MAXDEFRAG, above, is chosen in the config file and is real data
- * so we need to add the NFS overhead, which is more than TFTP.
- * To use sizeof in the internal unnamed structures, we need a real
- * instance (can't do "sizeof(struct rpc_t.u.reply))", unfortunately).
- * The compiler doesn't complain nor allocates the actual structure
- */
-static struct rpc_t rpc_specimen;
-#define IP_PKTSIZE (CONFIG_NET_MAXDEFRAG + sizeof(rpc_specimen.u.reply))
+#define IP_PKTSIZE (CONFIG_NET_MAXDEFRAG)
#define IP_MAXUDP (IP_PKTSIZE - IP_HDR_SIZE)
if (len < ETHER_HDR_SIZE)
return;
-#ifdef CONFIG_API
+#if defined(CONFIG_API) || defined(CONFIG_EFI_LOADER)
if (push_packet) {
(*push_packet)(in_packet, len);
return;
dst_ip = net_read_ip(&ip->ip_dst);
if (net_ip.s_addr && dst_ip.s_addr != net_ip.s_addr &&
dst_ip.s_addr != 0xFFFFFFFF) {
-#ifdef CONFIG_MCAST_TFTP
- if (net_mcast_addr != dst_ip)
-#endif
return;
}
/* Read source IP address for later use */
}
#endif
-#if defined(CONFIG_NETCONSOLE) && !(CONFIG_SPL_BUILD)
+#if defined(CONFIG_NETCONSOLE) && !defined(CONFIG_SPL_BUILD)
nc_input_packet((uchar *)ip + IP_UDP_HDR_SIZE,
src_ip,
ntohs(ip->udp_dst),
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
}
}
/* Fall through */
case TFTPGET:
case TFTPPUT:
- if (net_server_ip.s_addr == 0) {
+ if (net_server_ip.s_addr == 0 && !is_serverip_in_cmd()) {
puts("*** ERROR: `serverip' not set\n");
return 1;
}
/* Fall through */
case NETCONS:
+ case FASTBOOT:
case TFTPSRV:
if (net_ip.s_addr == 0) {
puts("*** ERROR: `ipaddr' not set\n");
}
}
-void net_set_ip_header(uchar *pkt, struct in_addr dest, struct in_addr source)
+void net_set_ip_header(uchar *pkt, struct in_addr dest, struct in_addr source,
+ u16 pkt_len, u8 proto)
{
struct ip_udp_hdr *ip = (struct ip_udp_hdr *)pkt;
/* IP_HDR_SIZE / 4 (not including UDP) */
ip->ip_hl_v = 0x45;
ip->ip_tos = 0;
- ip->ip_len = htons(IP_HDR_SIZE);
+ ip->ip_len = htons(pkt_len);
+ ip->ip_p = proto;
ip->ip_id = htons(net_ip_id++);
ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */
ip->ip_ttl = 255;
net_copy_ip((void *)&ip->ip_src, &source);
/* already in network byte order */
net_copy_ip((void *)&ip->ip_dst, &dest);
+
+ ip->ip_sum = compute_ip_checksum(ip, IP_HDR_SIZE);
}
void net_set_udp_header(uchar *pkt, struct in_addr dest, int dport, int sport,
if (len & 1)
pkt[IP_UDP_HDR_SIZE + len] = 0;
- net_set_ip_header(pkt, dest, net_ip);
- ip->ip_len = htons(IP_UDP_HDR_SIZE + len);
- ip->ip_p = IPPROTO_UDP;
- ip->ip_sum = compute_ip_checksum(ip, IP_HDR_SIZE);
+ net_set_ip_header(pkt, dest, net_ip, IP_UDP_HDR_SIZE + len,
+ IPPROTO_UDP);
ip->udp_src = htons(sport);
ip->udp_dst = htons(dport);
void copy_filename(char *dst, const char *src, int size)
{
- if (*src && (*src == '"')) {
+ if (src && *src && (*src == '"')) {
++src;
--size;
}
- while ((--size > 0) && *src && (*src != '"'))
+ while ((--size > 0) && src && *src && (*src != '"'))
*dst++ = *src++;
*dst = '\0';
}
+int is_serverip_in_cmd(void)
+{
+ return !!strchr(net_boot_file_name, ':');
+}
+
+int net_parse_bootfile(struct in_addr *ipaddr, char *filename, int max_len)
+{
+ char *colon;
+
+ if (net_boot_file_name[0] == '\0')
+ return 0;
+
+ colon = strchr(net_boot_file_name, ':');
+ if (colon) {
+ if (ipaddr)
+ *ipaddr = string_to_ip(net_boot_file_name);
+ strncpy(filename, colon + 1, max_len);
+ } else {
+ strncpy(filename, net_boot_file_name, max_len);
+ }
+ filename[max_len - 1] = '\0';
+
+ return 1;
+}
+
#if defined(CONFIG_CMD_NFS) || \
defined(CONFIG_CMD_SNTP) || \
defined(CONFIG_CMD_DNS)
return htons(id);
}
-ushort getenv_vlan(char *var)
+ushort env_get_vlan(char *var)
{
- return string_to_vlan(getenv(var));
+ return string_to_vlan(env_get(var));
}