1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2015 National Instruments
10 #include <asm/eth-raw-os.h>
16 #include <asm/global_data.h>
18 DECLARE_GLOBAL_DATA_PTR;
21 static struct in_addr arp_ip;
23 static int sb_eth_raw_start(struct udevice *dev)
25 struct eth_sandbox_raw_priv *priv = dev_get_priv(dev);
26 struct eth_pdata *pdata = dev_get_plat(dev);
29 debug("eth_sandbox_raw: Start\n");
31 ret = sandbox_eth_raw_os_start(priv, pdata->enetaddr);
33 env_set("ipaddr", "127.0.0.1");
34 env_set("serverip", "127.0.0.1");
35 net_ip = string_to_ip("127.0.0.1");
36 net_server_ip = net_ip;
41 static int sb_eth_raw_send(struct udevice *dev, void *packet, int length)
43 struct eth_sandbox_raw_priv *priv = dev_get_priv(dev);
45 debug("eth_sandbox_raw: Send packet %d\n", length);
48 struct ethernet_hdr *eth = packet;
50 if (ntohs(eth->et_protlen) == PROT_ARP) {
51 struct arp_hdr *arp = packet + ETHER_HDR_SIZE;
54 * localhost works on a higher-level API in Linux than
55 * ARP packets, so fake it
57 arp_ip = net_read_ip(&arp->ar_tpa);
61 packet += ETHER_HDR_SIZE;
62 length -= ETHER_HDR_SIZE;
64 return sandbox_eth_raw_os_send(packet, length, priv);
67 static int sb_eth_raw_recv(struct udevice *dev, int flags, uchar **packetp)
69 struct eth_pdata *pdata = dev_get_plat(dev);
70 struct eth_sandbox_raw_priv *priv = dev_get_priv(dev);
75 struct arp_hdr *arp = (void *)net_rx_packets[0] +
79 * Fake an ARP response. The u-boot network stack is sending an
80 * ARP request (to find the MAC address to address the actual
81 * packet to) and requires an ARP response to continue. Since
82 * this is the localhost interface, there is no Etherent level
83 * traffic at all, so there is no way to send an ARP request or
84 * to get a response. For this reason we fake the response to
85 * make the u-boot network stack happy.
87 arp->ar_hrd = htons(ARP_ETHER);
88 arp->ar_pro = htons(PROT_IP);
89 arp->ar_hln = ARP_HLEN;
90 arp->ar_pln = ARP_PLEN;
91 arp->ar_op = htons(ARPOP_REPLY);
92 /* Any non-zero MAC address will work */
93 memset(&arp->ar_sha, 0x01, ARP_HLEN);
94 /* Use whatever IP we were looking for (always 127.0.0.1?) */
95 net_write_ip(&arp->ar_spa, arp_ip);
96 memcpy(&arp->ar_tha, pdata->enetaddr, ARP_HLEN);
97 net_write_ip(&arp->ar_tpa, net_ip);
98 length = ARP_HDR_SIZE;
100 /* If local, the Ethernet header won't be included; skip it */
101 uchar *pktptr = priv->local ?
102 net_rx_packets[0] + ETHER_HDR_SIZE : net_rx_packets[0];
104 retval = sandbox_eth_raw_os_recv(pktptr, &length, priv);
107 if (!retval && length) {
109 struct ethernet_hdr *eth = (void *)net_rx_packets[0];
111 /* Fill in enough of the missing Ethernet header */
112 memcpy(eth->et_dest, pdata->enetaddr, ARP_HLEN);
113 memset(eth->et_src, 0x01, ARP_HLEN);
114 eth->et_protlen = htons(reply_arp ? PROT_ARP : PROT_IP);
116 length += ETHER_HDR_SIZE;
119 debug("eth_sandbox_raw: received packet %d\n",
121 *packetp = net_rx_packets[0];
127 static void sb_eth_raw_stop(struct udevice *dev)
129 struct eth_sandbox_raw_priv *priv = dev_get_priv(dev);
131 debug("eth_sandbox_raw: Stop\n");
133 sandbox_eth_raw_os_stop(priv);
136 static int sb_eth_raw_read_rom_hwaddr(struct udevice *dev)
138 struct eth_pdata *pdata = dev_get_plat(dev);
140 net_random_ethaddr(pdata->enetaddr);
145 static const struct eth_ops sb_eth_raw_ops = {
146 .start = sb_eth_raw_start,
147 .send = sb_eth_raw_send,
148 .recv = sb_eth_raw_recv,
149 .stop = sb_eth_raw_stop,
150 .read_rom_hwaddr = sb_eth_raw_read_rom_hwaddr,
153 static int sb_eth_raw_of_to_plat(struct udevice *dev)
155 struct eth_pdata *pdata = dev_get_plat(dev);
156 struct eth_sandbox_raw_priv *priv = dev_get_priv(dev);
160 pdata->iobase = dev_read_addr(dev);
162 ifname = dev_read_string(dev, "host-raw-interface");
164 strncpy(priv->host_ifname, ifname, IFNAMSIZ);
165 printf(": Using %s from DT\n", priv->host_ifname);
167 if (dev_read_u32(dev, "host-raw-interface-idx",
168 &priv->host_ifindex) < 0) {
169 priv->host_ifindex = 0;
171 ret = sandbox_eth_raw_os_idx_to_name(priv);
174 printf(": Using interface index %d from DT (%s)\n",
175 priv->host_ifindex, priv->host_ifname);
178 ret = sandbox_eth_raw_os_is_local(priv->host_ifname);
186 static const struct udevice_id sb_eth_raw_ids[] = {
187 { .compatible = "sandbox,eth-raw" },
191 U_BOOT_DRIVER(eth_sandbox_raw) = {
192 .name = "eth_sandbox_raw",
194 .of_match = sb_eth_raw_ids,
195 .of_to_plat = sb_eth_raw_of_to_plat,
196 .ops = &sb_eth_raw_ops,
197 .priv_auto = sizeof(struct eth_sandbox_raw_priv),
198 .plat_auto = sizeof(struct eth_pdata),