sandbox-raw.c 5.0 KB

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