1
2
3
4
5
6
7
8
9#include <log.h>
10#include <asm/eth-raw-os.h>
11#include <common.h>
12#include <dm.h>
13#include <env.h>
14#include <malloc.h>
15#include <net.h>
16#include <asm/global_data.h>
17
18DECLARE_GLOBAL_DATA_PTR;
19
20static int reply_arp;
21static struct in_addr arp_ip;
22
23static int sb_eth_raw_start(struct udevice *dev)
24{
25 struct eth_sandbox_raw_priv *priv = dev_get_priv(dev);
26 struct eth_pdata *pdata = dev_get_plat(dev);
27 int ret;
28
29 debug("eth_sandbox_raw: Start\n");
30
31 ret = sandbox_eth_raw_os_start(priv, pdata->enetaddr);
32 if (priv->local) {
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;
37 }
38 return ret;
39}
40
41static int sb_eth_raw_send(struct udevice *dev, void *packet, int length)
42{
43 struct eth_sandbox_raw_priv *priv = dev_get_priv(dev);
44
45 debug("eth_sandbox_raw: Send packet %d\n", length);
46
47 if (priv->local) {
48 struct ethernet_hdr *eth = packet;
49
50 if (ntohs(eth->et_protlen) == PROT_ARP) {
51 struct arp_hdr *arp = packet + ETHER_HDR_SIZE;
52
53
54
55
56
57 arp_ip = net_read_ip(&arp->ar_tpa);
58 reply_arp = 1;
59 return 0;
60 }
61 packet += ETHER_HDR_SIZE;
62 length -= ETHER_HDR_SIZE;
63 }
64 return sandbox_eth_raw_os_send(packet, length, priv);
65}
66
67static int sb_eth_raw_recv(struct udevice *dev, int flags, uchar **packetp)
68{
69 struct eth_pdata *pdata = dev_get_plat(dev);
70 struct eth_sandbox_raw_priv *priv = dev_get_priv(dev);
71 int retval = 0;
72 int length;
73
74 if (reply_arp) {
75 struct arp_hdr *arp = (void *)net_rx_packets[0] +
76 ETHER_HDR_SIZE;
77
78
79
80
81
82
83
84
85
86
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
93 memset(&arp->ar_sha, 0x01, ARP_HLEN);
94
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;
99 } else {
100
101 uchar *pktptr = priv->local ?
102 net_rx_packets[0] + ETHER_HDR_SIZE : net_rx_packets[0];
103
104 retval = sandbox_eth_raw_os_recv(pktptr, &length, priv);
105 }
106
107 if (!retval && length) {
108 if (priv->local) {
109 struct ethernet_hdr *eth = (void *)net_rx_packets[0];
110
111
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);
115 reply_arp = 0;
116 length += ETHER_HDR_SIZE;
117 }
118
119 debug("eth_sandbox_raw: received packet %d\n",
120 length);
121 *packetp = net_rx_packets[0];
122 return length;
123 }
124 return retval;
125}
126
127static void sb_eth_raw_stop(struct udevice *dev)
128{
129 struct eth_sandbox_raw_priv *priv = dev_get_priv(dev);
130
131 debug("eth_sandbox_raw: Stop\n");
132
133 sandbox_eth_raw_os_stop(priv);
134}
135
136static int sb_eth_raw_read_rom_hwaddr(struct udevice *dev)
137{
138 struct eth_pdata *pdata = dev_get_plat(dev);
139
140 net_random_ethaddr(pdata->enetaddr);
141
142 return 0;
143}
144
145static 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,
151};
152
153static int sb_eth_raw_of_to_plat(struct udevice *dev)
154{
155 struct eth_pdata *pdata = dev_get_plat(dev);
156 struct eth_sandbox_raw_priv *priv = dev_get_priv(dev);
157 const char *ifname;
158 int ret;
159
160 pdata->iobase = dev_read_addr(dev);
161
162 ifname = dev_read_string(dev, "host-raw-interface");
163 if (ifname) {
164 strncpy(priv->host_ifname, ifname, IFNAMSIZ);
165 printf(": Using %s from DT\n", priv->host_ifname);
166 }
167 if (dev_read_u32(dev, "host-raw-interface-idx",
168 &priv->host_ifindex) < 0) {
169 priv->host_ifindex = 0;
170 } else {
171 ret = sandbox_eth_raw_os_idx_to_name(priv);
172 if (ret < 0)
173 return ret;
174 printf(": Using interface index %d from DT (%s)\n",
175 priv->host_ifindex, priv->host_ifname);
176 }
177
178 ret = sandbox_eth_raw_os_is_local(priv->host_ifname);
179 if (ret < 0)
180 return ret;
181 priv->local = ret;
182
183 return 0;
184}
185
186static const struct udevice_id sb_eth_raw_ids[] = {
187 { .compatible = "sandbox,eth-raw" },
188 { }
189};
190
191U_BOOT_DRIVER(eth_sandbox_raw) = {
192 .name = "eth_sandbox_raw",
193 .id = UCLASS_ETH,
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),
199};
200