sandbox-raw.c 5.0 KB

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