blob: cb69a95d97f83b070e4277ec26188329e52559e8 [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>
14
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];
27 IPaddr_t fake_host_ipaddr;
28 uchar *recv_packet_buffer;
29 int recv_packet_length;
30};
31
Joe Hershberger3ea143a2015-03-22 17:09:13 -050032static int sb_eth_start(struct udevice *dev)
33{
Joe Hershbergerd87a4572015-03-22 17:09:14 -050034 struct eth_sandbox_priv *priv = dev_get_priv(dev);
35
Joe Hershberger3ea143a2015-03-22 17:09:13 -050036 debug("eth_sandbox: Start\n");
37
Joe Hershbergerd87a4572015-03-22 17:09:14 -050038 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];
Joe Hershberger3ea143a2015-03-22 17:09:13 -050041 return 0;
42}
43
44static int sb_eth_send(struct udevice *dev, void *packet, int length)
45{
Joe Hershbergerd87a4572015-03-22 17:09:14 -050046 struct eth_sandbox_priv *priv = dev_get_priv(dev);
47 struct ethernet_hdr *eth = packet;
48
Joe Hershberger3ea143a2015-03-22 17:09:13 -050049 debug("eth_sandbox: Send packet %d\n", length);
50
Joe Hershbergerd87a4572015-03-22 17:09:14 -050051 if (ntohs(eth->et_protlen) == PROT_ARP) {
52 struct arp_hdr *arp = packet + ETHER_HDR_SIZE;
53
54 if (ntohs(arp->ar_op) == ARPOP_REQUEST) {
55 struct ethernet_hdr *eth_recv;
56 struct arp_hdr *arp_recv;
57
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,
64 ARP_HLEN);
65 eth_recv->et_protlen = htons(PROT_ARP);
66
67 arp_recv = (void *)priv->recv_packet_buffer +
68 ETHER_HDR_SIZE;
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,
75 ARP_HLEN);
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);
79
80 priv->recv_packet_length = ETHER_HDR_SIZE +
81 ARP_HDR_SIZE;
82 }
83 } else if (ntohs(eth->et_protlen) == PROT_IP) {
84 struct ip_udp_hdr *ip = packet + ETHER_HDR_SIZE;
85
86 if (ip->ip_p == IPPROTO_ICMP) {
87 struct icmp_hdr *icmp = (struct icmp_hdr *)&ip->udp_src;
88
89 if (icmp->type == ICMP_ECHO_REQUEST) {
90 struct ethernet_hdr *eth_recv;
91 struct ip_udp_hdr *ipr;
92 struct icmp_hdr *icmpr;
93
94 /* reply to the ping */
95 memcpy(priv->recv_packet_buffer, packet,
96 length);
97 eth_recv = (void *)priv->recv_packet_buffer;
98 ipr = (void *)priv->recv_packet_buffer +
99 ETHER_HDR_SIZE;
100 icmpr = (struct icmp_hdr *)&ipr->udp_src;
101 memcpy(eth_recv->et_dest, eth->et_src,
102 ARP_HLEN);
103 memcpy(eth_recv->et_src, priv->fake_host_hwaddr,
104 ARP_HLEN);
105 ipr->ip_sum = 0;
106 ipr->ip_off = 0;
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,
111 IP_HDR_SIZE);
112
113 icmpr->type = ICMP_ECHO_REPLY;
114 icmpr->checksum = 0;
115 icmpr->checksum = compute_ip_checksum(icmpr,
116 ICMP_HDR_SIZE);
117
118 priv->recv_packet_length = length;
119 }
120 }
121 }
122
Joe Hershberger3ea143a2015-03-22 17:09:13 -0500123 return 0;
124}
125
126static int sb_eth_recv(struct udevice *dev, uchar **packetp)
127{
Joe Hershbergerd87a4572015-03-22 17:09:14 -0500128 struct eth_sandbox_priv *priv = dev_get_priv(dev);
129
130 if (priv->recv_packet_length) {
131 int lcl_recv_packet_length = priv->recv_packet_length;
132
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;
138 }
Joe Hershberger3ea143a2015-03-22 17:09:13 -0500139 return 0;
140}
141
142static void sb_eth_stop(struct udevice *dev)
143{
144 debug("eth_sandbox: Stop\n");
145}
146
147static int sb_eth_write_hwaddr(struct udevice *dev)
148{
149 struct eth_pdata *pdata = dev_get_platdata(dev);
150
151 debug("eth_sandbox %s: Write HW ADDR - %pM\n", dev->name,
152 pdata->enetaddr);
153 return 0;
154}
155
156static const struct eth_ops sb_eth_ops = {
157 .start = sb_eth_start,
158 .send = sb_eth_send,
159 .recv = sb_eth_recv,
160 .stop = sb_eth_stop,
161 .write_hwaddr = sb_eth_write_hwaddr,
162};
163
164static int sb_eth_remove(struct udevice *dev)
165{
166 return 0;
167}
168
169static int sb_eth_ofdata_to_platdata(struct udevice *dev)
170{
171 struct eth_pdata *pdata = dev_get_platdata(dev);
172
173 pdata->iobase = dev_get_addr(dev);
174 return 0;
175}
176
177static const struct udevice_id sb_eth_ids[] = {
178 { .compatible = "sandbox,eth" },
179 { }
180};
181
182U_BOOT_DRIVER(eth_sandbox) = {
183 .name = "eth_sandbox",
184 .id = UCLASS_ETH,
185 .of_match = sb_eth_ids,
186 .ofdata_to_platdata = sb_eth_ofdata_to_platdata,
187 .remove = sb_eth_remove,
188 .ops = &sb_eth_ops,
Joe Hershbergerd87a4572015-03-22 17:09:14 -0500189 .priv_auto_alloc_size = sizeof(struct eth_sandbox_priv),
Joe Hershberger3ea143a2015-03-22 17:09:13 -0500190 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
191};