1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2015 National Instruments
9 #include <asm/eth-raw-os.h>
15 DECLARE_GLOBAL_DATA_PTR;
18 static struct in_addr arp_ip;
20 static int sb_eth_raw_start(struct udevice *dev)
22 struct eth_sandbox_raw_priv *priv = dev_get_priv(dev);
23 struct eth_pdata *pdata = dev_get_platdata(dev);
26 debug("eth_sandbox_raw: Start\n");
28 ret = sandbox_eth_raw_os_start(priv, pdata->enetaddr);
30 env_set("ipaddr", "127.0.0.1");
31 env_set("serverip", "127.0.0.1");
36 static int sb_eth_raw_send(struct udevice *dev, void *packet, int length)
38 struct eth_sandbox_raw_priv *priv = dev_get_priv(dev);
40 debug("eth_sandbox_raw: Send packet %d\n", length);
43 struct ethernet_hdr *eth = packet;
45 if (ntohs(eth->et_protlen) == PROT_ARP) {
46 struct arp_hdr *arp = packet + ETHER_HDR_SIZE;
49 * localhost works on a higher-level API in Linux than
50 * ARP packets, so fake it
52 arp_ip = net_read_ip(&arp->ar_tpa);
56 packet += ETHER_HDR_SIZE;
57 length -= ETHER_HDR_SIZE;
59 return sandbox_eth_raw_os_send(packet, length, priv);
62 static int sb_eth_raw_recv(struct udevice *dev, int flags, uchar **packetp)
64 struct eth_pdata *pdata = dev_get_platdata(dev);
65 struct eth_sandbox_raw_priv *priv = dev_get_priv(dev);
70 struct arp_hdr *arp = (void *)net_rx_packets[0] +
74 * Fake an ARP response. The u-boot network stack is sending an
75 * ARP request (to find the MAC address to address the actual
76 * packet to) and requires an ARP response to continue. Since
77 * this is the localhost interface, there is no Etherent level
78 * traffic at all, so there is no way to send an ARP request or
79 * to get a response. For this reason we fake the response to
80 * make the u-boot network stack happy.
82 arp->ar_hrd = htons(ARP_ETHER);
83 arp->ar_pro = htons(PROT_IP);
84 arp->ar_hln = ARP_HLEN;
85 arp->ar_pln = ARP_PLEN;
86 arp->ar_op = htons(ARPOP_REPLY);
87 /* Any non-zero MAC address will work */
88 memset(&arp->ar_sha, 0x01, ARP_HLEN);
89 /* Use whatever IP we were looking for (always 127.0.0.1?) */
90 net_write_ip(&arp->ar_spa, arp_ip);
91 memcpy(&arp->ar_tha, pdata->enetaddr, ARP_HLEN);
92 net_write_ip(&arp->ar_tpa, net_ip);
93 length = ARP_HDR_SIZE;
95 /* If local, the Ethernet header won't be included; skip it */
96 uchar *pktptr = priv->local ?
97 net_rx_packets[0] + ETHER_HDR_SIZE : net_rx_packets[0];
99 retval = sandbox_eth_raw_os_recv(pktptr, &length, priv);
102 if (!retval && length) {
104 struct ethernet_hdr *eth = (void *)net_rx_packets[0];
106 /* Fill in enough of the missing Ethernet header */
107 memcpy(eth->et_dest, pdata->enetaddr, ARP_HLEN);
108 memset(eth->et_src, 0x01, ARP_HLEN);
109 eth->et_protlen = htons(reply_arp ? PROT_ARP : PROT_IP);
111 length += ETHER_HDR_SIZE;
114 debug("eth_sandbox_raw: received packet %d\n",
116 *packetp = net_rx_packets[0];
122 static void sb_eth_raw_stop(struct udevice *dev)
124 struct eth_sandbox_raw_priv *priv = dev_get_priv(dev);
126 debug("eth_sandbox_raw: Stop\n");
128 sandbox_eth_raw_os_stop(priv);
131 static const struct eth_ops sb_eth_raw_ops = {
132 .start = sb_eth_raw_start,
133 .send = sb_eth_raw_send,
134 .recv = sb_eth_raw_recv,
135 .stop = sb_eth_raw_stop,
138 static int sb_eth_raw_ofdata_to_platdata(struct udevice *dev)
140 struct eth_pdata *pdata = dev_get_platdata(dev);
141 struct eth_sandbox_raw_priv *priv = dev_get_priv(dev);
144 pdata->iobase = dev_read_addr(dev);
146 ifname = dev_read_string(dev, "host-raw-interface");
148 strncpy(priv->host_ifname, ifname, IFNAMSIZ);
149 printf(": Using %s from DT\n", priv->host_ifname);
150 if (strcmp(ifname, "lo") == 0)
157 static const struct udevice_id sb_eth_raw_ids[] = {
158 { .compatible = "sandbox,eth-raw" },
162 U_BOOT_DRIVER(eth_sandbox_raw) = {
163 .name = "eth_sandbox_raw",
165 .of_match = sb_eth_raw_ids,
166 .ofdata_to_platdata = sb_eth_raw_ofdata_to_platdata,
167 .ops = &sb_eth_raw_ops,
168 .priv_auto_alloc_size = sizeof(struct eth_sandbox_raw_priv),
169 .platdata_auto_alloc_size = sizeof(struct eth_pdata),