2 * Copyright (c) 2015 National Instruments
5 * Joe Hershberger <joe.hershberger@ni.com>
7 * SPDX-License-Identifier: GPL-2.0
16 DECLARE_GLOBAL_DATA_PTR;
19 * struct eth_sandbox_priv - memory for sandbox mock driver
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
26 struct eth_sandbox_priv {
27 uchar fake_host_hwaddr[ARP_HLEN];
28 struct in_addr fake_host_ipaddr;
29 uchar *recv_packet_buffer;
30 int recv_packet_length;
33 static bool disabled[8] = {false};
34 static bool skip_timeout;
37 * sandbox_eth_disable_response()
39 * index - The alias index (also DM seq number)
40 * disable - If non-zero, ignore sent packets and don't send mock response
42 void sandbox_eth_disable_response(int index, bool disable)
44 disabled[index] = disable;
48 * sandbox_eth_skip_timeout()
50 * When the first packet read is attempted, fast-forward time
52 void sandbox_eth_skip_timeout(void)
57 static int sb_eth_start(struct udevice *dev)
59 struct eth_sandbox_priv *priv = dev_get_priv(dev);
61 debug("eth_sandbox: Start\n");
63 fdtdec_get_byte_array(gd->fdt_blob, dev_of_offset(dev),
64 "fake-host-hwaddr", priv->fake_host_hwaddr,
66 priv->recv_packet_buffer = net_rx_packets[0];
70 static int sb_eth_send(struct udevice *dev, void *packet, int length)
72 struct eth_sandbox_priv *priv = dev_get_priv(dev);
73 struct ethernet_hdr *eth = packet;
75 debug("eth_sandbox: Send packet %d\n", length);
77 if (dev->seq >= 0 && dev->seq < ARRAY_SIZE(disabled) &&
81 if (ntohs(eth->et_protlen) == PROT_ARP) {
82 struct arp_hdr *arp = packet + ETHER_HDR_SIZE;
84 if (ntohs(arp->ar_op) == ARPOP_REQUEST) {
85 struct ethernet_hdr *eth_recv;
86 struct arp_hdr *arp_recv;
88 /* store this as the assumed IP of the fake host */
89 priv->fake_host_ipaddr = net_read_ip(&arp->ar_tpa);
90 /* 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,
95 eth_recv->et_protlen = htons(PROT_ARP);
97 arp_recv = (void *)priv->recv_packet_buffer +
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,
106 net_write_ip(&arp_recv->ar_spa, priv->fake_host_ipaddr);
107 memcpy(&arp_recv->ar_tha, &arp->ar_sha, ARP_HLEN);
108 net_copy_ip(&arp_recv->ar_tpa, &arp->ar_spa);
110 priv->recv_packet_length = ETHER_HDR_SIZE +
113 } else if (ntohs(eth->et_protlen) == PROT_IP) {
114 struct ip_udp_hdr *ip = packet + ETHER_HDR_SIZE;
116 if (ip->ip_p == IPPROTO_ICMP) {
117 struct icmp_hdr *icmp = (struct icmp_hdr *)&ip->udp_src;
119 if (icmp->type == ICMP_ECHO_REQUEST) {
120 struct ethernet_hdr *eth_recv;
121 struct ip_udp_hdr *ipr;
122 struct icmp_hdr *icmpr;
124 /* reply to the ping */
125 memcpy(priv->recv_packet_buffer, packet,
127 eth_recv = (void *)priv->recv_packet_buffer;
128 ipr = (void *)priv->recv_packet_buffer +
130 icmpr = (struct icmp_hdr *)&ipr->udp_src;
131 memcpy(eth_recv->et_dest, eth->et_src,
133 memcpy(eth_recv->et_src, priv->fake_host_hwaddr,
137 net_copy_ip((void *)&ipr->ip_dst, &ip->ip_src);
138 net_write_ip((void *)&ipr->ip_src,
139 priv->fake_host_ipaddr);
140 ipr->ip_sum = compute_ip_checksum(ipr,
143 icmpr->type = ICMP_ECHO_REPLY;
145 icmpr->checksum = compute_ip_checksum(icmpr,
148 priv->recv_packet_length = length;
156 static int sb_eth_recv(struct udevice *dev, int flags, uchar **packetp)
158 struct eth_sandbox_priv *priv = dev_get_priv(dev);
161 sandbox_timer_add_offset(11000UL);
162 skip_timeout = false;
165 if (priv->recv_packet_length) {
166 int lcl_recv_packet_length = priv->recv_packet_length;
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;
177 static void sb_eth_stop(struct udevice *dev)
179 debug("eth_sandbox: Stop\n");
182 static int sb_eth_write_hwaddr(struct udevice *dev)
184 struct eth_pdata *pdata = dev_get_platdata(dev);
186 debug("eth_sandbox %s: Write HW ADDR - %pM\n", dev->name,
191 static const struct eth_ops sb_eth_ops = {
192 .start = sb_eth_start,
196 .write_hwaddr = sb_eth_write_hwaddr,
199 static int sb_eth_remove(struct udevice *dev)
204 static int sb_eth_ofdata_to_platdata(struct udevice *dev)
206 struct eth_pdata *pdata = dev_get_platdata(dev);
208 pdata->iobase = dev_get_addr(dev);
212 static const struct udevice_id sb_eth_ids[] = {
213 { .compatible = "sandbox,eth" },
217 U_BOOT_DRIVER(eth_sandbox) = {
218 .name = "eth_sandbox",
220 .of_match = sb_eth_ids,
221 .ofdata_to_platdata = sb_eth_ofdata_to_platdata,
222 .remove = sb_eth_remove,
224 .priv_auto_alloc_size = sizeof(struct eth_sandbox_priv),
225 .platdata_auto_alloc_size = sizeof(struct eth_pdata),