efi_net.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * EFI application network access support
  3. *
  4. * Copyright (c) 2016 Alexander Graf
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. #include <common.h>
  9. #include <efi_loader.h>
  10. #include <inttypes.h>
  11. #include <lcd.h>
  12. #include <malloc.h>
  13. DECLARE_GLOBAL_DATA_PTR;
  14. static const efi_guid_t efi_net_guid = EFI_SIMPLE_NETWORK_GUID;
  15. static const efi_guid_t efi_pxe_guid = EFI_PXE_GUID;
  16. static struct efi_pxe_packet *dhcp_ack;
  17. static bool new_rx_packet;
  18. static void *new_tx_packet;
  19. struct efi_net_obj {
  20. /* Generic EFI object parent class data */
  21. struct efi_object parent;
  22. /* EFI Interface callback struct for network */
  23. struct efi_simple_network net;
  24. struct efi_simple_network_mode net_mode;
  25. /* PXE struct to transmit dhcp data */
  26. struct efi_pxe pxe;
  27. struct efi_pxe_mode pxe_mode;
  28. };
  29. static efi_status_t EFIAPI efi_net_start(struct efi_simple_network *this)
  30. {
  31. EFI_ENTRY("%p", this);
  32. return EFI_EXIT(EFI_SUCCESS);
  33. }
  34. static efi_status_t EFIAPI efi_net_stop(struct efi_simple_network *this)
  35. {
  36. EFI_ENTRY("%p", this);
  37. return EFI_EXIT(EFI_SUCCESS);
  38. }
  39. static efi_status_t EFIAPI efi_net_initialize(struct efi_simple_network *this,
  40. ulong extra_rx, ulong extra_tx)
  41. {
  42. EFI_ENTRY("%p, %lx, %lx", this, extra_rx, extra_tx);
  43. eth_init();
  44. return EFI_EXIT(EFI_SUCCESS);
  45. }
  46. static efi_status_t EFIAPI efi_net_reset(struct efi_simple_network *this,
  47. int extended_verification)
  48. {
  49. EFI_ENTRY("%p, %x", this, extended_verification);
  50. return EFI_EXIT(EFI_SUCCESS);
  51. }
  52. static efi_status_t EFIAPI efi_net_shutdown(struct efi_simple_network *this)
  53. {
  54. EFI_ENTRY("%p", this);
  55. return EFI_EXIT(EFI_SUCCESS);
  56. }
  57. static efi_status_t EFIAPI efi_net_receive_filters(
  58. struct efi_simple_network *this, u32 enable, u32 disable,
  59. int reset_mcast_filter, ulong mcast_filter_count,
  60. struct efi_mac_address *mcast_filter)
  61. {
  62. EFI_ENTRY("%p, %x, %x, %x, %lx, %p", this, enable, disable,
  63. reset_mcast_filter, mcast_filter_count, mcast_filter);
  64. return EFI_EXIT(EFI_UNSUPPORTED);
  65. }
  66. static efi_status_t EFIAPI efi_net_station_address(
  67. struct efi_simple_network *this, int reset,
  68. struct efi_mac_address *new_mac)
  69. {
  70. EFI_ENTRY("%p, %x, %p", this, reset, new_mac);
  71. return EFI_EXIT(EFI_UNSUPPORTED);
  72. }
  73. static efi_status_t EFIAPI efi_net_statistics(struct efi_simple_network *this,
  74. int reset, ulong *stat_size,
  75. void *stat_table)
  76. {
  77. EFI_ENTRY("%p, %x, %p, %p", this, reset, stat_size, stat_table);
  78. return EFI_EXIT(EFI_UNSUPPORTED);
  79. }
  80. static efi_status_t EFIAPI efi_net_mcastiptomac(struct efi_simple_network *this,
  81. int ipv6,
  82. struct efi_ip_address *ip,
  83. struct efi_mac_address *mac)
  84. {
  85. EFI_ENTRY("%p, %x, %p, %p", this, ipv6, ip, mac);
  86. return EFI_EXIT(EFI_INVALID_PARAMETER);
  87. }
  88. static efi_status_t EFIAPI efi_net_nvdata(struct efi_simple_network *this,
  89. int read_write, ulong offset,
  90. ulong buffer_size, char *buffer)
  91. {
  92. EFI_ENTRY("%p, %x, %lx, %lx, %p", this, read_write, offset, buffer_size,
  93. buffer);
  94. return EFI_EXIT(EFI_UNSUPPORTED);
  95. }
  96. static efi_status_t EFIAPI efi_net_get_status(struct efi_simple_network *this,
  97. u32 *int_status, void **txbuf)
  98. {
  99. EFI_ENTRY("%p, %p, %p", this, int_status, txbuf);
  100. /* We send packets synchronously, so nothing is outstanding */
  101. if (int_status)
  102. *int_status = 0;
  103. if (txbuf)
  104. *txbuf = new_tx_packet;
  105. new_tx_packet = NULL;
  106. return EFI_EXIT(EFI_SUCCESS);
  107. }
  108. static efi_status_t EFIAPI efi_net_transmit(struct efi_simple_network *this,
  109. ulong header_size, ulong buffer_size, void *buffer,
  110. struct efi_mac_address *src_addr,
  111. struct efi_mac_address *dest_addr, u16 *protocol)
  112. {
  113. EFI_ENTRY("%p, %lx, %lx, %p, %p, %p, %p", this, header_size,
  114. buffer_size, buffer, src_addr, dest_addr, protocol);
  115. if (header_size) {
  116. /* We would need to create the header if header_size != 0 */
  117. return EFI_EXIT(EFI_INVALID_PARAMETER);
  118. }
  119. #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
  120. /* Ethernet packets always fit, just bounce */
  121. memcpy(efi_bounce_buffer, buffer, buffer_size);
  122. net_send_packet(efi_bounce_buffer, buffer_size);
  123. #else
  124. net_send_packet(buffer, buffer_size);
  125. #endif
  126. new_tx_packet = buffer;
  127. return EFI_EXIT(EFI_SUCCESS);
  128. }
  129. static void efi_net_push(void *pkt, int len)
  130. {
  131. new_rx_packet = true;
  132. }
  133. static efi_status_t EFIAPI efi_net_receive(struct efi_simple_network *this,
  134. ulong *header_size, ulong *buffer_size, void *buffer,
  135. struct efi_mac_address *src_addr,
  136. struct efi_mac_address *dest_addr, u16 *protocol)
  137. {
  138. EFI_ENTRY("%p, %p, %p, %p, %p, %p, %p", this, header_size,
  139. buffer_size, buffer, src_addr, dest_addr, protocol);
  140. push_packet = efi_net_push;
  141. eth_rx();
  142. push_packet = NULL;
  143. if (!new_rx_packet)
  144. return EFI_EXIT(EFI_NOT_READY);
  145. if (*buffer_size < net_rx_packet_len) {
  146. /* Packet doesn't fit, try again with bigger buf */
  147. *buffer_size = net_rx_packet_len;
  148. return EFI_EXIT(EFI_BUFFER_TOO_SMALL);
  149. }
  150. memcpy(buffer, net_rx_packet, net_rx_packet_len);
  151. *buffer_size = net_rx_packet_len;
  152. new_rx_packet = false;
  153. return EFI_EXIT(EFI_SUCCESS);
  154. }
  155. void efi_net_set_dhcp_ack(void *pkt, int len)
  156. {
  157. int maxsize = sizeof(*dhcp_ack);
  158. if (!dhcp_ack)
  159. dhcp_ack = malloc(maxsize);
  160. memcpy(dhcp_ack, pkt, min(len, maxsize));
  161. }
  162. /* This gets called from do_bootefi_exec(). */
  163. int efi_net_register(void)
  164. {
  165. struct efi_net_obj *netobj;
  166. if (!eth_get_dev()) {
  167. /* No eth device active, don't expose any */
  168. return 0;
  169. }
  170. /* We only expose the "active" eth device, so one is enough */
  171. netobj = calloc(1, sizeof(*netobj));
  172. /* Fill in object data */
  173. netobj->parent.protocols[0].guid = &efi_net_guid;
  174. netobj->parent.protocols[0].protocol_interface = &netobj->net;
  175. netobj->parent.protocols[1].guid = &efi_guid_device_path;
  176. netobj->parent.protocols[1].protocol_interface =
  177. efi_dp_from_eth();
  178. netobj->parent.protocols[2].guid = &efi_pxe_guid;
  179. netobj->parent.protocols[2].protocol_interface = &netobj->pxe;
  180. netobj->parent.handle = &netobj->net;
  181. netobj->net.revision = EFI_SIMPLE_NETWORK_PROTOCOL_REVISION;
  182. netobj->net.start = efi_net_start;
  183. netobj->net.stop = efi_net_stop;
  184. netobj->net.initialize = efi_net_initialize;
  185. netobj->net.reset = efi_net_reset;
  186. netobj->net.shutdown = efi_net_shutdown;
  187. netobj->net.receive_filters = efi_net_receive_filters;
  188. netobj->net.station_address = efi_net_station_address;
  189. netobj->net.statistics = efi_net_statistics;
  190. netobj->net.mcastiptomac = efi_net_mcastiptomac;
  191. netobj->net.nvdata = efi_net_nvdata;
  192. netobj->net.get_status = efi_net_get_status;
  193. netobj->net.transmit = efi_net_transmit;
  194. netobj->net.receive = efi_net_receive;
  195. netobj->net.mode = &netobj->net_mode;
  196. netobj->net_mode.state = EFI_NETWORK_STARTED;
  197. memcpy(netobj->net_mode.current_address.mac_addr, eth_get_ethaddr(), 6);
  198. netobj->net_mode.hwaddr_size = ARP_HLEN;
  199. netobj->net_mode.max_packet_size = PKTSIZE;
  200. netobj->pxe.mode = &netobj->pxe_mode;
  201. if (dhcp_ack)
  202. netobj->pxe_mode.dhcp_ack = *dhcp_ack;
  203. /* Hook net up to the device list */
  204. list_add_tail(&netobj->parent.link, &efi_obj_list);
  205. return 0;
  206. }