sandbox-raw.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  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. #include <asm/eth-raw-os.h>
  10. #include <common.h>
  11. #include <dm.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. const char *interface;
  22. debug("eth_sandbox_raw: Start\n");
  23. interface = fdt_getprop(gd->fdt_blob, dev->of_offset,
  24. "host-raw-interface", NULL);
  25. if (interface == NULL)
  26. return -EINVAL;
  27. if (strcmp(interface, "lo") == 0) {
  28. priv->local = 1;
  29. setenv("ipaddr", "127.0.0.1");
  30. setenv("serverip", "127.0.0.1");
  31. }
  32. return sandbox_eth_raw_os_start(interface, pdata->enetaddr, priv);
  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_platdata(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 const struct eth_ops sb_eth_raw_ops = {
  115. .start = sb_eth_raw_start,
  116. .send = sb_eth_raw_send,
  117. .recv = sb_eth_raw_recv,
  118. .stop = sb_eth_raw_stop,
  119. };
  120. static int sb_eth_raw_ofdata_to_platdata(struct udevice *dev)
  121. {
  122. struct eth_pdata *pdata = dev_get_platdata(dev);
  123. pdata->iobase = dev_get_addr(dev);
  124. return 0;
  125. }
  126. static const struct udevice_id sb_eth_raw_ids[] = {
  127. { .compatible = "sandbox,eth-raw" },
  128. { }
  129. };
  130. U_BOOT_DRIVER(eth_sandbox_raw) = {
  131. .name = "eth_sandbox_raw",
  132. .id = UCLASS_ETH,
  133. .of_match = sb_eth_raw_ids,
  134. .ofdata_to_platdata = sb_eth_raw_ofdata_to_platdata,
  135. .ops = &sb_eth_raw_ops,
  136. .priv_auto_alloc_size = sizeof(struct eth_sandbox_raw_priv),
  137. .platdata_auto_alloc_size = sizeof(struct eth_pdata),
  138. };