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