2 * Copyright (c) 2015 National Instruments
5 * Joe Hershberger <joe.hershberger@ni.com>
7 * SPDX-License-Identifier: GPL-2.0
15 DECLARE_GLOBAL_DATA_PTR;
18 * struct eth_sandbox_priv - memory for sandbox mock driver
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
25 struct eth_sandbox_priv {
26 uchar fake_host_hwaddr[ARP_HLEN];
27 IPaddr_t fake_host_ipaddr;
28 uchar *recv_packet_buffer;
29 int recv_packet_length;
32 static int sb_eth_start(struct udevice *dev)
34 struct eth_sandbox_priv *priv = dev_get_priv(dev);
36 debug("eth_sandbox: Start\n");
38 fdtdec_get_byte_array(gd->fdt_blob, dev->of_offset, "fake-host-hwaddr",
39 priv->fake_host_hwaddr, ARP_HLEN);
40 priv->recv_packet_buffer = net_rx_packets[0];
44 static int sb_eth_send(struct udevice *dev, void *packet, int length)
46 struct eth_sandbox_priv *priv = dev_get_priv(dev);
47 struct ethernet_hdr *eth = packet;
49 debug("eth_sandbox: Send packet %d\n", length);
51 if (ntohs(eth->et_protlen) == PROT_ARP) {
52 struct arp_hdr *arp = packet + ETHER_HDR_SIZE;
54 if (ntohs(arp->ar_op) == ARPOP_REQUEST) {
55 struct ethernet_hdr *eth_recv;
56 struct arp_hdr *arp_recv;
58 /* store this as the assumed IP of the fake host */
59 priv->fake_host_ipaddr = NetReadIP(&arp->ar_tpa);
60 /* Formulate a fake response */
61 eth_recv = (void *)priv->recv_packet_buffer;
62 memcpy(eth_recv->et_dest, eth->et_src, ARP_HLEN);
63 memcpy(eth_recv->et_src, priv->fake_host_hwaddr,
65 eth_recv->et_protlen = htons(PROT_ARP);
67 arp_recv = (void *)priv->recv_packet_buffer +
69 arp_recv->ar_hrd = htons(ARP_ETHER);
70 arp_recv->ar_pro = htons(PROT_IP);
71 arp_recv->ar_hln = ARP_HLEN;
72 arp_recv->ar_pln = ARP_PLEN;
73 arp_recv->ar_op = htons(ARPOP_REPLY);
74 memcpy(&arp_recv->ar_sha, priv->fake_host_hwaddr,
76 NetWriteIP(&arp_recv->ar_spa, priv->fake_host_ipaddr);
77 memcpy(&arp_recv->ar_tha, &arp->ar_sha, ARP_HLEN);
78 NetCopyIP(&arp_recv->ar_tpa, &arp->ar_spa);
80 priv->recv_packet_length = ETHER_HDR_SIZE +
83 } else if (ntohs(eth->et_protlen) == PROT_IP) {
84 struct ip_udp_hdr *ip = packet + ETHER_HDR_SIZE;
86 if (ip->ip_p == IPPROTO_ICMP) {
87 struct icmp_hdr *icmp = (struct icmp_hdr *)&ip->udp_src;
89 if (icmp->type == ICMP_ECHO_REQUEST) {
90 struct ethernet_hdr *eth_recv;
91 struct ip_udp_hdr *ipr;
92 struct icmp_hdr *icmpr;
94 /* reply to the ping */
95 memcpy(priv->recv_packet_buffer, packet,
97 eth_recv = (void *)priv->recv_packet_buffer;
98 ipr = (void *)priv->recv_packet_buffer +
100 icmpr = (struct icmp_hdr *)&ipr->udp_src;
101 memcpy(eth_recv->et_dest, eth->et_src,
103 memcpy(eth_recv->et_src, priv->fake_host_hwaddr,
107 NetCopyIP((void *)&ipr->ip_dst, &ip->ip_src);
108 NetWriteIP((void *)&ipr->ip_src,
109 priv->fake_host_ipaddr);
110 ipr->ip_sum = compute_ip_checksum(ipr,
113 icmpr->type = ICMP_ECHO_REPLY;
115 icmpr->checksum = compute_ip_checksum(icmpr,
118 priv->recv_packet_length = length;
126 static int sb_eth_recv(struct udevice *dev, uchar **packetp)
128 struct eth_sandbox_priv *priv = dev_get_priv(dev);
130 if (priv->recv_packet_length) {
131 int lcl_recv_packet_length = priv->recv_packet_length;
133 debug("eth_sandbox: received packet %d\n",
134 priv->recv_packet_length);
135 priv->recv_packet_length = 0;
136 *packetp = priv->recv_packet_buffer;
137 return lcl_recv_packet_length;
142 static void sb_eth_stop(struct udevice *dev)
144 debug("eth_sandbox: Stop\n");
147 static int sb_eth_write_hwaddr(struct udevice *dev)
149 struct eth_pdata *pdata = dev_get_platdata(dev);
151 debug("eth_sandbox %s: Write HW ADDR - %pM\n", dev->name,
156 static const struct eth_ops sb_eth_ops = {
157 .start = sb_eth_start,
161 .write_hwaddr = sb_eth_write_hwaddr,
164 static int sb_eth_remove(struct udevice *dev)
169 static int sb_eth_ofdata_to_platdata(struct udevice *dev)
171 struct eth_pdata *pdata = dev_get_platdata(dev);
173 pdata->iobase = dev_get_addr(dev);
177 static const struct udevice_id sb_eth_ids[] = {
178 { .compatible = "sandbox,eth" },
182 U_BOOT_DRIVER(eth_sandbox) = {
183 .name = "eth_sandbox",
185 .of_match = sb_eth_ids,
186 .ofdata_to_platdata = sb_eth_ofdata_to_platdata,
187 .remove = sb_eth_remove,
189 .priv_auto_alloc_size = sizeof(struct eth_sandbox_priv),
190 .platdata_auto_alloc_size = sizeof(struct eth_pdata),