sandbox-raw.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2015 National Instruments
  4. *
  5. * (C) Copyright 2015
  6. * Joe Hershberger <joe.hershberger@ni.com>
  7. */
  8. #include <log.h>
  9. #include <asm/eth-raw-os.h>
  10. #include <common.h>
  11. #include <dm.h>
  12. #include <env.h>
  13. #include <malloc.h>
  14. #include <net.h>
  15. DECLARE_GLOBAL_DATA_PTR;
  16. static int reply_arp;
  17. static struct in_addr arp_ip;
  18. static int sb_eth_raw_start(struct udevice *dev)
  19. {
  20. struct eth_sandbox_raw_priv *priv = dev_get_priv(dev);
  21. struct eth_pdata *pdata = dev_get_platdata(dev);
  22. int ret;
  23. debug("eth_sandbox_raw: Start\n");
  24. ret = sandbox_eth_raw_os_start(priv, pdata->enetaddr);
  25. if (priv->local) {
  26. env_set("ipaddr", "127.0.0.1");
  27. env_set("serverip", "127.0.0.1");
  28. net_ip = string_to_ip("127.0.0.1");
  29. net_server_ip = net_ip;
  30. }
  31. return ret;
  32. }
  33. static int sb_eth_raw_send(struct udevice *dev, void *packet, int length)
  34. {
  35. struct eth_sandbox_raw_priv *priv = dev_get_priv(dev);
  36. debug("eth_sandbox_raw: Send packet %d\n", length);
  37. if (priv->local) {
  38. struct ethernet_hdr *eth = packet;
  39. if (ntohs(eth->et_protlen) == PROT_ARP) {
  40. struct arp_hdr *arp = packet + ETHER_HDR_SIZE;
  41. /**
  42. * localhost works on a higher-level API in Linux than
  43. * ARP packets, so fake it
  44. */
  45. arp_ip = net_read_ip(&arp->ar_tpa);
  46. reply_arp = 1;
  47. return 0;
  48. }
  49. packet += ETHER_HDR_SIZE;
  50. length -= ETHER_HDR_SIZE;
  51. }
  52. return sandbox_eth_raw_os_send(packet, length, priv);
  53. }
  54. static int sb_eth_raw_recv(struct udevice *dev, int flags, uchar **packetp)
  55. {
  56. struct eth_pdata *pdata = dev_get_platdata(dev);
  57. struct eth_sandbox_raw_priv *priv = dev_get_priv(dev);
  58. int retval = 0;
  59. int length;
  60. if (reply_arp) {
  61. struct arp_hdr *arp = (void *)net_rx_packets[0] +
  62. ETHER_HDR_SIZE;
  63. /*
  64. * Fake an ARP response. The u-boot network stack is sending an
  65. * ARP request (to find the MAC address to address the actual
  66. * packet to) and requires an ARP response to continue. Since
  67. * this is the localhost interface, there is no Etherent level
  68. * traffic at all, so there is no way to send an ARP request or
  69. * to get a response. For this reason we fake the response to
  70. * make the u-boot network stack happy.
  71. */
  72. arp->ar_hrd = htons(ARP_ETHER);
  73. arp->ar_pro = htons(PROT_IP);
  74. arp->ar_hln = ARP_HLEN;
  75. arp->ar_pln = ARP_PLEN;
  76. arp->ar_op = htons(ARPOP_REPLY);
  77. /* Any non-zero MAC address will work */
  78. memset(&arp->ar_sha, 0x01, ARP_HLEN);
  79. /* Use whatever IP we were looking for (always 127.0.0.1?) */
  80. net_write_ip(&arp->ar_spa, arp_ip);
  81. memcpy(&arp->ar_tha, pdata->enetaddr, ARP_HLEN);
  82. net_write_ip(&arp->ar_tpa, net_ip);
  83. length = ARP_HDR_SIZE;
  84. } else {
  85. /* If local, the Ethernet header won't be included; skip it */
  86. uchar *pktptr = priv->local ?
  87. net_rx_packets[0] + ETHER_HDR_SIZE : net_rx_packets[0];
  88. retval = sandbox_eth_raw_os_recv(pktptr, &length, priv);
  89. }
  90. if (!retval && length) {
  91. if (priv->local) {
  92. struct ethernet_hdr *eth = (void *)net_rx_packets[0];
  93. /* Fill in enough of the missing Ethernet header */
  94. memcpy(eth->et_dest, pdata->enetaddr, ARP_HLEN);
  95. memset(eth->et_src, 0x01, ARP_HLEN);
  96. eth->et_protlen = htons(reply_arp ? PROT_ARP : PROT_IP);
  97. reply_arp = 0;
  98. length += ETHER_HDR_SIZE;
  99. }
  100. debug("eth_sandbox_raw: received packet %d\n",
  101. length);
  102. *packetp = net_rx_packets[0];
  103. return length;
  104. }
  105. return retval;
  106. }
  107. static void sb_eth_raw_stop(struct udevice *dev)
  108. {
  109. struct eth_sandbox_raw_priv *priv = dev_get_priv(dev);
  110. debug("eth_sandbox_raw: Stop\n");
  111. sandbox_eth_raw_os_stop(priv);
  112. }
  113. static int sb_eth_raw_read_rom_hwaddr(struct udevice *dev)
  114. {
  115. struct eth_pdata *pdata = dev_get_platdata(dev);
  116. net_random_ethaddr(pdata->enetaddr);
  117. return 0;
  118. }
  119. static const struct eth_ops sb_eth_raw_ops = {
  120. .start = sb_eth_raw_start,
  121. .send = sb_eth_raw_send,
  122. .recv = sb_eth_raw_recv,
  123. .stop = sb_eth_raw_stop,
  124. .read_rom_hwaddr = sb_eth_raw_read_rom_hwaddr,
  125. };
  126. static int sb_eth_raw_ofdata_to_platdata(struct udevice *dev)
  127. {
  128. struct eth_pdata *pdata = dev_get_platdata(dev);
  129. struct eth_sandbox_raw_priv *priv = dev_get_priv(dev);
  130. const char *ifname;
  131. int ret;
  132. pdata->iobase = dev_read_addr(dev);
  133. ifname = dev_read_string(dev, "host-raw-interface");
  134. if (ifname) {
  135. strncpy(priv->host_ifname, ifname, IFNAMSIZ);
  136. printf(": Using %s from DT\n", priv->host_ifname);
  137. }
  138. if (dev_read_u32(dev, "host-raw-interface-idx",
  139. &priv->host_ifindex) < 0) {
  140. priv->host_ifindex = 0;
  141. } else {
  142. ret = sandbox_eth_raw_os_idx_to_name(priv);
  143. if (ret < 0)
  144. return ret;
  145. printf(": Using interface index %d from DT (%s)\n",
  146. priv->host_ifindex, priv->host_ifname);
  147. }
  148. ret = sandbox_eth_raw_os_is_local(priv->host_ifname);
  149. if (ret < 0)
  150. return ret;
  151. priv->local = ret;
  152. return 0;
  153. }
  154. static const struct udevice_id sb_eth_raw_ids[] = {
  155. { .compatible = "sandbox,eth-raw" },
  156. { }
  157. };
  158. U_BOOT_DRIVER(eth_sandbox_raw) = {
  159. .name = "eth_sandbox_raw",
  160. .id = UCLASS_ETH,
  161. .of_match = sb_eth_raw_ids,
  162. .ofdata_to_platdata = sb_eth_raw_ofdata_to_platdata,
  163. .ops = &sb_eth_raw_ops,
  164. .priv_auto_alloc_size = sizeof(struct eth_sandbox_raw_priv),
  165. .platdata_auto_alloc_size = sizeof(struct eth_pdata),
  166. };