blob: b34712bd06f1f490111d36ddf2435e24747ea26e [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Joe Hershberger3ea143a2015-03-22 17:09:13 -05002/*
3 * Copyright (c) 2015 National Instruments
4 *
5 * (C) Copyright 2015
6 * Joe Hershberger <joe.hershberger@ni.com>
Joe Hershberger3ea143a2015-03-22 17:09:13 -05007 */
8
9#include <common.h>
10#include <dm.h>
11#include <malloc.h>
12#include <net.h>
Joe Hershberger6f2707c2015-04-21 13:57:19 -050013#include <asm/test.h>
Joe Hershberger3ea143a2015-03-22 17:09:13 -050014
15DECLARE_GLOBAL_DATA_PTR;
16
Joe Hershbergerd87a4572015-03-22 17:09:14 -050017/**
18 * struct eth_sandbox_priv - memory for sandbox mock driver
19 *
20 * fake_host_hwaddr: MAC address of mocked machine
21 * fake_host_ipaddr: IP address of mocked machine
22 * recv_packet_buffer: buffer of the packet returned as received
23 * recv_packet_length: length of the packet returned as received
24 */
25struct eth_sandbox_priv {
26 uchar fake_host_hwaddr[ARP_HLEN];
Joe Hershberger049a95a2015-04-08 01:41:01 -050027 struct in_addr fake_host_ipaddr;
Joe Hershbergerd87a4572015-03-22 17:09:14 -050028 uchar *recv_packet_buffer;
29 int recv_packet_length;
30};
31
Joe Hershberger2eede1f2015-03-22 17:09:19 -050032static bool disabled[8] = {false};
Joe Hershberger6f2707c2015-04-21 13:57:19 -050033static bool skip_timeout;
Joe Hershberger2eede1f2015-03-22 17:09:19 -050034
35/*
36 * sandbox_eth_disable_response()
37 *
38 * index - The alias index (also DM seq number)
39 * disable - If non-zero, ignore sent packets and don't send mock response
40 */
41void sandbox_eth_disable_response(int index, bool disable)
42{
43 disabled[index] = disable;
44}
45
Joe Hershberger6f2707c2015-04-21 13:57:19 -050046/*
47 * sandbox_eth_skip_timeout()
48 *
49 * When the first packet read is attempted, fast-forward time
50 */
51void sandbox_eth_skip_timeout(void)
52{
53 skip_timeout = true;
54}
55
Joe Hershberger3ea143a2015-03-22 17:09:13 -050056static int sb_eth_start(struct udevice *dev)
57{
Joe Hershbergerd87a4572015-03-22 17:09:14 -050058 struct eth_sandbox_priv *priv = dev_get_priv(dev);
59
Joe Hershberger3ea143a2015-03-22 17:09:13 -050060 debug("eth_sandbox: Start\n");
61
Simon Glasse160f7d2017-01-17 16:52:55 -070062 fdtdec_get_byte_array(gd->fdt_blob, dev_of_offset(dev),
63 "fake-host-hwaddr", priv->fake_host_hwaddr,
64 ARP_HLEN);
Joe Hershbergerd87a4572015-03-22 17:09:14 -050065 priv->recv_packet_buffer = net_rx_packets[0];
Joe Hershberger3ea143a2015-03-22 17:09:13 -050066 return 0;
67}
68
69static int sb_eth_send(struct udevice *dev, void *packet, int length)
70{
Joe Hershbergerd87a4572015-03-22 17:09:14 -050071 struct eth_sandbox_priv *priv = dev_get_priv(dev);
72 struct ethernet_hdr *eth = packet;
73
Joe Hershberger3ea143a2015-03-22 17:09:13 -050074 debug("eth_sandbox: Send packet %d\n", length);
75
Joe Hershberger2eede1f2015-03-22 17:09:19 -050076 if (dev->seq >= 0 && dev->seq < ARRAY_SIZE(disabled) &&
77 disabled[dev->seq])
78 return 0;
79
Joe Hershbergerd87a4572015-03-22 17:09:14 -050080 if (ntohs(eth->et_protlen) == PROT_ARP) {
81 struct arp_hdr *arp = packet + ETHER_HDR_SIZE;
82
83 if (ntohs(arp->ar_op) == ARPOP_REQUEST) {
84 struct ethernet_hdr *eth_recv;
85 struct arp_hdr *arp_recv;
86
87 /* store this as the assumed IP of the fake host */
Joe Hershberger049a95a2015-04-08 01:41:01 -050088 priv->fake_host_ipaddr = net_read_ip(&arp->ar_tpa);
Joe Hershbergerd87a4572015-03-22 17:09:14 -050089 /* Formulate a fake response */
90 eth_recv = (void *)priv->recv_packet_buffer;
91 memcpy(eth_recv->et_dest, eth->et_src, ARP_HLEN);
92 memcpy(eth_recv->et_src, priv->fake_host_hwaddr,
93 ARP_HLEN);
94 eth_recv->et_protlen = htons(PROT_ARP);
95
96 arp_recv = (void *)priv->recv_packet_buffer +
97 ETHER_HDR_SIZE;
98 arp_recv->ar_hrd = htons(ARP_ETHER);
99 arp_recv->ar_pro = htons(PROT_IP);
100 arp_recv->ar_hln = ARP_HLEN;
101 arp_recv->ar_pln = ARP_PLEN;
102 arp_recv->ar_op = htons(ARPOP_REPLY);
103 memcpy(&arp_recv->ar_sha, priv->fake_host_hwaddr,
104 ARP_HLEN);
Joe Hershberger049a95a2015-04-08 01:41:01 -0500105 net_write_ip(&arp_recv->ar_spa, priv->fake_host_ipaddr);
Joe Hershbergerd87a4572015-03-22 17:09:14 -0500106 memcpy(&arp_recv->ar_tha, &arp->ar_sha, ARP_HLEN);
Joe Hershberger049a95a2015-04-08 01:41:01 -0500107 net_copy_ip(&arp_recv->ar_tpa, &arp->ar_spa);
Joe Hershbergerd87a4572015-03-22 17:09:14 -0500108
109 priv->recv_packet_length = ETHER_HDR_SIZE +
110 ARP_HDR_SIZE;
111 }
112 } else if (ntohs(eth->et_protlen) == PROT_IP) {
113 struct ip_udp_hdr *ip = packet + ETHER_HDR_SIZE;
114
115 if (ip->ip_p == IPPROTO_ICMP) {
116 struct icmp_hdr *icmp = (struct icmp_hdr *)&ip->udp_src;
117
118 if (icmp->type == ICMP_ECHO_REQUEST) {
119 struct ethernet_hdr *eth_recv;
120 struct ip_udp_hdr *ipr;
121 struct icmp_hdr *icmpr;
122
123 /* reply to the ping */
124 memcpy(priv->recv_packet_buffer, packet,
125 length);
126 eth_recv = (void *)priv->recv_packet_buffer;
127 ipr = (void *)priv->recv_packet_buffer +
128 ETHER_HDR_SIZE;
129 icmpr = (struct icmp_hdr *)&ipr->udp_src;
130 memcpy(eth_recv->et_dest, eth->et_src,
131 ARP_HLEN);
132 memcpy(eth_recv->et_src, priv->fake_host_hwaddr,
133 ARP_HLEN);
134 ipr->ip_sum = 0;
135 ipr->ip_off = 0;
Joe Hershberger049a95a2015-04-08 01:41:01 -0500136 net_copy_ip((void *)&ipr->ip_dst, &ip->ip_src);
137 net_write_ip((void *)&ipr->ip_src,
138 priv->fake_host_ipaddr);
Joe Hershbergerd87a4572015-03-22 17:09:14 -0500139 ipr->ip_sum = compute_ip_checksum(ipr,
140 IP_HDR_SIZE);
141
142 icmpr->type = ICMP_ECHO_REPLY;
143 icmpr->checksum = 0;
144 icmpr->checksum = compute_ip_checksum(icmpr,
145 ICMP_HDR_SIZE);
146
147 priv->recv_packet_length = length;
148 }
149 }
150 }
151
Joe Hershberger3ea143a2015-03-22 17:09:13 -0500152 return 0;
153}
154
Simon Glassa1ca92e2015-07-06 16:47:49 -0600155static int sb_eth_recv(struct udevice *dev, int flags, uchar **packetp)
Joe Hershberger3ea143a2015-03-22 17:09:13 -0500156{
Joe Hershbergerd87a4572015-03-22 17:09:14 -0500157 struct eth_sandbox_priv *priv = dev_get_priv(dev);
158
Joe Hershberger6f2707c2015-04-21 13:57:19 -0500159 if (skip_timeout) {
Joe Hershbergerc5a75332015-12-21 16:31:35 -0600160 sandbox_timer_add_offset(11000UL);
Joe Hershberger6f2707c2015-04-21 13:57:19 -0500161 skip_timeout = false;
162 }
163
Joe Hershbergerd87a4572015-03-22 17:09:14 -0500164 if (priv->recv_packet_length) {
165 int lcl_recv_packet_length = priv->recv_packet_length;
166
167 debug("eth_sandbox: received packet %d\n",
168 priv->recv_packet_length);
169 priv->recv_packet_length = 0;
170 *packetp = priv->recv_packet_buffer;
171 return lcl_recv_packet_length;
172 }
Joe Hershberger3ea143a2015-03-22 17:09:13 -0500173 return 0;
174}
175
176static void sb_eth_stop(struct udevice *dev)
177{
178 debug("eth_sandbox: Stop\n");
179}
180
181static int sb_eth_write_hwaddr(struct udevice *dev)
182{
183 struct eth_pdata *pdata = dev_get_platdata(dev);
184
185 debug("eth_sandbox %s: Write HW ADDR - %pM\n", dev->name,
186 pdata->enetaddr);
187 return 0;
188}
189
190static const struct eth_ops sb_eth_ops = {
191 .start = sb_eth_start,
192 .send = sb_eth_send,
193 .recv = sb_eth_recv,
194 .stop = sb_eth_stop,
195 .write_hwaddr = sb_eth_write_hwaddr,
196};
197
198static int sb_eth_remove(struct udevice *dev)
199{
200 return 0;
201}
202
203static int sb_eth_ofdata_to_platdata(struct udevice *dev)
204{
205 struct eth_pdata *pdata = dev_get_platdata(dev);
206
Simon Glassa821c4a2017-05-17 17:18:05 -0600207 pdata->iobase = devfdt_get_addr(dev);
Joe Hershberger3ea143a2015-03-22 17:09:13 -0500208 return 0;
209}
210
211static const struct udevice_id sb_eth_ids[] = {
212 { .compatible = "sandbox,eth" },
213 { }
214};
215
216U_BOOT_DRIVER(eth_sandbox) = {
217 .name = "eth_sandbox",
218 .id = UCLASS_ETH,
219 .of_match = sb_eth_ids,
220 .ofdata_to_platdata = sb_eth_ofdata_to_platdata,
221 .remove = sb_eth_remove,
222 .ops = &sb_eth_ops,
Joe Hershbergerd87a4572015-03-22 17:09:14 -0500223 .priv_auto_alloc_size = sizeof(struct eth_sandbox_priv),
Joe Hershberger3ea143a2015-03-22 17:09:13 -0500224 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
225};