blob: 79d0ae68bfd5281ff2d0c985053de8e9d71925d5 [file] [log] [blame]
Joe Hershberger3ea143a2015-03-22 17:09:13 -05001/*
2 * Copyright (c) 2015 National Instruments
3 *
4 * (C) Copyright 2015
5 * Joe Hershberger <joe.hershberger@ni.com>
6 *
7 * SPDX-License-Identifier: GPL-2.0
8 */
9
10#include <common.h>
11#include <dm.h>
12#include <malloc.h>
13#include <net.h>
Joe Hershberger6f2707c2015-04-21 13:57:19 -050014#include <asm/test.h>
Joe Hershberger3ea143a2015-03-22 17:09:13 -050015
16DECLARE_GLOBAL_DATA_PTR;
17
Joe Hershbergerd87a4572015-03-22 17:09:14 -050018/**
19 * struct eth_sandbox_priv - memory for sandbox mock driver
20 *
21 * fake_host_hwaddr: MAC address of mocked machine
22 * fake_host_ipaddr: IP address of mocked machine
23 * recv_packet_buffer: buffer of the packet returned as received
24 * recv_packet_length: length of the packet returned as received
25 */
26struct eth_sandbox_priv {
27 uchar fake_host_hwaddr[ARP_HLEN];
Joe Hershberger049a95a2015-04-08 01:41:01 -050028 struct in_addr fake_host_ipaddr;
Joe Hershbergerd87a4572015-03-22 17:09:14 -050029 uchar *recv_packet_buffer;
30 int recv_packet_length;
31};
32
Joe Hershberger2eede1f2015-03-22 17:09:19 -050033static bool disabled[8] = {false};
Joe Hershberger6f2707c2015-04-21 13:57:19 -050034static bool skip_timeout;
Joe Hershberger2eede1f2015-03-22 17:09:19 -050035
36/*
37 * sandbox_eth_disable_response()
38 *
39 * index - The alias index (also DM seq number)
40 * disable - If non-zero, ignore sent packets and don't send mock response
41 */
42void sandbox_eth_disable_response(int index, bool disable)
43{
44 disabled[index] = disable;
45}
46
Joe Hershberger6f2707c2015-04-21 13:57:19 -050047/*
48 * sandbox_eth_skip_timeout()
49 *
50 * When the first packet read is attempted, fast-forward time
51 */
52void sandbox_eth_skip_timeout(void)
53{
54 skip_timeout = true;
55}
56
Joe Hershberger3ea143a2015-03-22 17:09:13 -050057static int sb_eth_start(struct udevice *dev)
58{
Joe Hershbergerd87a4572015-03-22 17:09:14 -050059 struct eth_sandbox_priv *priv = dev_get_priv(dev);
60
Joe Hershberger3ea143a2015-03-22 17:09:13 -050061 debug("eth_sandbox: Start\n");
62
Simon Glasse160f7d2017-01-17 16:52:55 -070063 fdtdec_get_byte_array(gd->fdt_blob, dev_of_offset(dev),
64 "fake-host-hwaddr", priv->fake_host_hwaddr,
65 ARP_HLEN);
Joe Hershbergerd87a4572015-03-22 17:09:14 -050066 priv->recv_packet_buffer = net_rx_packets[0];
Joe Hershberger3ea143a2015-03-22 17:09:13 -050067 return 0;
68}
69
70static int sb_eth_send(struct udevice *dev, void *packet, int length)
71{
Joe Hershbergerd87a4572015-03-22 17:09:14 -050072 struct eth_sandbox_priv *priv = dev_get_priv(dev);
73 struct ethernet_hdr *eth = packet;
74
Joe Hershberger3ea143a2015-03-22 17:09:13 -050075 debug("eth_sandbox: Send packet %d\n", length);
76
Joe Hershberger2eede1f2015-03-22 17:09:19 -050077 if (dev->seq >= 0 && dev->seq < ARRAY_SIZE(disabled) &&
78 disabled[dev->seq])
79 return 0;
80
Joe Hershbergerd87a4572015-03-22 17:09:14 -050081 if (ntohs(eth->et_protlen) == PROT_ARP) {
82 struct arp_hdr *arp = packet + ETHER_HDR_SIZE;
83
84 if (ntohs(arp->ar_op) == ARPOP_REQUEST) {
85 struct ethernet_hdr *eth_recv;
86 struct arp_hdr *arp_recv;
87
88 /* store this as the assumed IP of the fake host */
Joe Hershberger049a95a2015-04-08 01:41:01 -050089 priv->fake_host_ipaddr = net_read_ip(&arp->ar_tpa);
Joe Hershbergerd87a4572015-03-22 17:09:14 -050090 /* Formulate a fake response */
91 eth_recv = (void *)priv->recv_packet_buffer;
92 memcpy(eth_recv->et_dest, eth->et_src, ARP_HLEN);
93 memcpy(eth_recv->et_src, priv->fake_host_hwaddr,
94 ARP_HLEN);
95 eth_recv->et_protlen = htons(PROT_ARP);
96
97 arp_recv = (void *)priv->recv_packet_buffer +
98 ETHER_HDR_SIZE;
99 arp_recv->ar_hrd = htons(ARP_ETHER);
100 arp_recv->ar_pro = htons(PROT_IP);
101 arp_recv->ar_hln = ARP_HLEN;
102 arp_recv->ar_pln = ARP_PLEN;
103 arp_recv->ar_op = htons(ARPOP_REPLY);
104 memcpy(&arp_recv->ar_sha, priv->fake_host_hwaddr,
105 ARP_HLEN);
Joe Hershberger049a95a2015-04-08 01:41:01 -0500106 net_write_ip(&arp_recv->ar_spa, priv->fake_host_ipaddr);
Joe Hershbergerd87a4572015-03-22 17:09:14 -0500107 memcpy(&arp_recv->ar_tha, &arp->ar_sha, ARP_HLEN);
Joe Hershberger049a95a2015-04-08 01:41:01 -0500108 net_copy_ip(&arp_recv->ar_tpa, &arp->ar_spa);
Joe Hershbergerd87a4572015-03-22 17:09:14 -0500109
110 priv->recv_packet_length = ETHER_HDR_SIZE +
111 ARP_HDR_SIZE;
112 }
113 } else if (ntohs(eth->et_protlen) == PROT_IP) {
114 struct ip_udp_hdr *ip = packet + ETHER_HDR_SIZE;
115
116 if (ip->ip_p == IPPROTO_ICMP) {
117 struct icmp_hdr *icmp = (struct icmp_hdr *)&ip->udp_src;
118
119 if (icmp->type == ICMP_ECHO_REQUEST) {
120 struct ethernet_hdr *eth_recv;
121 struct ip_udp_hdr *ipr;
122 struct icmp_hdr *icmpr;
123
124 /* reply to the ping */
125 memcpy(priv->recv_packet_buffer, packet,
126 length);
127 eth_recv = (void *)priv->recv_packet_buffer;
128 ipr = (void *)priv->recv_packet_buffer +
129 ETHER_HDR_SIZE;
130 icmpr = (struct icmp_hdr *)&ipr->udp_src;
131 memcpy(eth_recv->et_dest, eth->et_src,
132 ARP_HLEN);
133 memcpy(eth_recv->et_src, priv->fake_host_hwaddr,
134 ARP_HLEN);
135 ipr->ip_sum = 0;
136 ipr->ip_off = 0;
Joe Hershberger049a95a2015-04-08 01:41:01 -0500137 net_copy_ip((void *)&ipr->ip_dst, &ip->ip_src);
138 net_write_ip((void *)&ipr->ip_src,
139 priv->fake_host_ipaddr);
Joe Hershbergerd87a4572015-03-22 17:09:14 -0500140 ipr->ip_sum = compute_ip_checksum(ipr,
141 IP_HDR_SIZE);
142
143 icmpr->type = ICMP_ECHO_REPLY;
144 icmpr->checksum = 0;
145 icmpr->checksum = compute_ip_checksum(icmpr,
146 ICMP_HDR_SIZE);
147
148 priv->recv_packet_length = length;
149 }
150 }
151 }
152
Joe Hershberger3ea143a2015-03-22 17:09:13 -0500153 return 0;
154}
155
Simon Glassa1ca92e2015-07-06 16:47:49 -0600156static int sb_eth_recv(struct udevice *dev, int flags, uchar **packetp)
Joe Hershberger3ea143a2015-03-22 17:09:13 -0500157{
Joe Hershbergerd87a4572015-03-22 17:09:14 -0500158 struct eth_sandbox_priv *priv = dev_get_priv(dev);
159
Joe Hershberger6f2707c2015-04-21 13:57:19 -0500160 if (skip_timeout) {
Joe Hershbergerc5a75332015-12-21 16:31:35 -0600161 sandbox_timer_add_offset(11000UL);
Joe Hershberger6f2707c2015-04-21 13:57:19 -0500162 skip_timeout = false;
163 }
164
Joe Hershbergerd87a4572015-03-22 17:09:14 -0500165 if (priv->recv_packet_length) {
166 int lcl_recv_packet_length = priv->recv_packet_length;
167
168 debug("eth_sandbox: received packet %d\n",
169 priv->recv_packet_length);
170 priv->recv_packet_length = 0;
171 *packetp = priv->recv_packet_buffer;
172 return lcl_recv_packet_length;
173 }
Joe Hershberger3ea143a2015-03-22 17:09:13 -0500174 return 0;
175}
176
177static void sb_eth_stop(struct udevice *dev)
178{
179 debug("eth_sandbox: Stop\n");
180}
181
182static int sb_eth_write_hwaddr(struct udevice *dev)
183{
184 struct eth_pdata *pdata = dev_get_platdata(dev);
185
186 debug("eth_sandbox %s: Write HW ADDR - %pM\n", dev->name,
187 pdata->enetaddr);
188 return 0;
189}
190
191static const struct eth_ops sb_eth_ops = {
192 .start = sb_eth_start,
193 .send = sb_eth_send,
194 .recv = sb_eth_recv,
195 .stop = sb_eth_stop,
196 .write_hwaddr = sb_eth_write_hwaddr,
197};
198
199static int sb_eth_remove(struct udevice *dev)
200{
201 return 0;
202}
203
204static int sb_eth_ofdata_to_platdata(struct udevice *dev)
205{
206 struct eth_pdata *pdata = dev_get_platdata(dev);
207
208 pdata->iobase = dev_get_addr(dev);
209 return 0;
210}
211
212static const struct udevice_id sb_eth_ids[] = {
213 { .compatible = "sandbox,eth" },
214 { }
215};
216
217U_BOOT_DRIVER(eth_sandbox) = {
218 .name = "eth_sandbox",
219 .id = UCLASS_ETH,
220 .of_match = sb_eth_ids,
221 .ofdata_to_platdata = sb_eth_ofdata_to_platdata,
222 .remove = sb_eth_remove,
223 .ops = &sb_eth_ops,
Joe Hershbergerd87a4572015-03-22 17:09:14 -0500224 .priv_auto_alloc_size = sizeof(struct eth_sandbox_priv),
Joe Hershberger3ea143a2015-03-22 17:09:13 -0500225 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
226};