sandbox-raw.c 4.3 KB

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