ubootnet.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2013 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <grub/net/netbuff.h>
  19. #include <grub/uboot/disk.h>
  20. #include <grub/uboot/uboot.h>
  21. #include <grub/uboot/api_public.h>
  22. #include <grub/dl.h>
  23. #include <grub/net.h>
  24. #include <grub/time.h>
  25. #include <grub/i18n.h>
  26. GRUB_MOD_LICENSE ("GPLv3+");
  27. static grub_err_t
  28. card_open (struct grub_net_card *dev)
  29. {
  30. int status;
  31. status = grub_uboot_dev_open (dev->data);
  32. if (status)
  33. return grub_error (GRUB_ERR_IO, "Couldn't open network card.");
  34. return GRUB_ERR_NONE;
  35. }
  36. static void
  37. card_close (struct grub_net_card *dev)
  38. {
  39. grub_uboot_dev_close (dev->data);
  40. }
  41. static grub_err_t
  42. send_card_buffer (struct grub_net_card *dev, struct grub_net_buff *pack)
  43. {
  44. int status;
  45. grub_size_t len;
  46. len = (pack->tail - pack->data);
  47. if (len > dev->mtu)
  48. len = dev->mtu;
  49. grub_memcpy (dev->txbuf, pack->data, len);
  50. status = grub_uboot_dev_send (dev->data, dev->txbuf, len);
  51. if (status)
  52. return grub_error (GRUB_ERR_IO, N_("couldn't send network packet"));
  53. return GRUB_ERR_NONE;
  54. }
  55. static struct grub_net_buff *
  56. get_card_packet (struct grub_net_card *dev)
  57. {
  58. int rc;
  59. grub_uint64_t start_time;
  60. struct grub_net_buff *nb;
  61. int actual;
  62. nb = grub_netbuff_alloc (dev->mtu + 64 + 2);
  63. if (!nb)
  64. return NULL;
  65. /* Reserve 2 bytes so that 2 + 14/18 bytes of ethernet header is divisible
  66. by 4. So that IP header is aligned on 4 bytes. */
  67. grub_netbuff_reserve (nb, 2);
  68. start_time = grub_get_time_ms ();
  69. do
  70. {
  71. rc = grub_uboot_dev_recv (dev->data, nb->data, dev->mtu + 64, &actual);
  72. grub_dprintf ("net", "rc=%d, actual=%d, time=%lld\n", rc, actual,
  73. grub_get_time_ms () - start_time);
  74. }
  75. while ((actual <= 0 || rc < 0) && (grub_get_time_ms () - start_time < 200));
  76. if (actual > 0)
  77. {
  78. grub_netbuff_put (nb, actual);
  79. return nb;
  80. }
  81. grub_netbuff_free (nb);
  82. return NULL;
  83. }
  84. static struct grub_net_card_driver ubootnet =
  85. {
  86. .name = "ubnet",
  87. .open = card_open,
  88. .close = card_close,
  89. .send = send_card_buffer,
  90. .recv = get_card_packet
  91. };
  92. GRUB_MOD_INIT (ubootnet)
  93. {
  94. int devcount, i;
  95. int nfound = 0;
  96. devcount = grub_uboot_dev_enum ();
  97. for (i = 0; i < devcount; i++)
  98. {
  99. struct device_info *devinfo = grub_uboot_dev_get (i);
  100. struct grub_net_card *card;
  101. if (!(devinfo->type & DEV_TYP_NET))
  102. continue;
  103. card = grub_zalloc (sizeof (struct grub_net_card));
  104. if (!card)
  105. {
  106. grub_print_error ();
  107. return;
  108. }
  109. /* FIXME: Any way to check this? */
  110. card->mtu = 1500;
  111. grub_memcpy (&(card->default_address.mac), &devinfo->di_net.hwaddr, 6);
  112. card->default_address.type = GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET;
  113. card->txbufsize = ALIGN_UP (card->mtu, 64) + 256;
  114. card->txbuf = grub_zalloc (card->txbufsize);
  115. if (!card->txbuf)
  116. {
  117. grub_free (card);
  118. grub_print_error ();
  119. continue;
  120. }
  121. card->data = devinfo;
  122. card->flags = 0;
  123. card->name = grub_xasprintf ("ubnet_%d", ++nfound);
  124. card->idle_poll_delay_ms = 10;
  125. card->driver = &ubootnet;
  126. grub_net_card_register (card);
  127. }
  128. }
  129. GRUB_MOD_FINI (ubootnet)
  130. {
  131. struct grub_net_card *card, *next;
  132. FOR_NET_CARDS_SAFE (card, next)
  133. if (card->driver && grub_strcmp (card->driver->name, "ubnet") == 0)
  134. grub_net_card_unregister (card);
  135. }