efi_net.c 6.7 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. /* XXX Do we care? */
  65. return EFI_EXIT(EFI_SUCCESS);
  66. }
  67. static efi_status_t EFIAPI efi_net_station_address(
  68. struct efi_simple_network *this, int reset,
  69. struct efi_mac_address *new_mac)
  70. {
  71. EFI_ENTRY("%p, %x, %p", this, reset, new_mac);
  72. return EFI_EXIT(EFI_INVALID_PARAMETER);
  73. }
  74. static efi_status_t EFIAPI efi_net_statistics(struct efi_simple_network *this,
  75. int reset, ulong *stat_size,
  76. void *stat_table)
  77. {
  78. EFI_ENTRY("%p, %x, %p, %p", this, reset, stat_size, stat_table);
  79. return EFI_EXIT(EFI_INVALID_PARAMETER);
  80. }
  81. static efi_status_t EFIAPI efi_net_mcastiptomac(struct efi_simple_network *this,
  82. int ipv6,
  83. struct efi_ip_address *ip,
  84. struct efi_mac_address *mac)
  85. {
  86. EFI_ENTRY("%p, %x, %p, %p", this, ipv6, ip, mac);
  87. return EFI_EXIT(EFI_INVALID_PARAMETER);
  88. }
  89. static efi_status_t EFIAPI efi_net_nvdata(struct efi_simple_network *this,
  90. int read_write, ulong offset,
  91. ulong buffer_size, char *buffer)
  92. {
  93. EFI_ENTRY("%p, %x, %lx, %lx, %p", this, read_write, offset, buffer_size,
  94. buffer);
  95. return EFI_EXIT(EFI_INVALID_PARAMETER);
  96. }
  97. static efi_status_t EFIAPI efi_net_get_status(struct efi_simple_network *this,
  98. u32 *int_status, void **txbuf)
  99. {
  100. EFI_ENTRY("%p, %p, %p", this, int_status, txbuf);
  101. /* We send packets synchronously, so nothing is outstanding */
  102. if (int_status)
  103. *int_status = 0;
  104. if (txbuf)
  105. *txbuf = new_tx_packet;
  106. new_tx_packet = NULL;
  107. return EFI_EXIT(EFI_SUCCESS);
  108. }
  109. static efi_status_t EFIAPI efi_net_transmit(struct efi_simple_network *this,
  110. ulong header_size, ulong buffer_size, void *buffer,
  111. struct efi_mac_address *src_addr,
  112. struct efi_mac_address *dest_addr, u16 *protocol)
  113. {
  114. EFI_ENTRY("%p, %lx, %lx, %p, %p, %p, %p", this, header_size,
  115. buffer_size, buffer, src_addr, dest_addr, protocol);
  116. if (header_size) {
  117. /* We would need to create the header if header_size != 0 */
  118. return EFI_EXIT(EFI_INVALID_PARAMETER);
  119. }
  120. #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
  121. /* Ethernet packets always fit, just bounce */
  122. memcpy(efi_bounce_buffer, buffer, buffer_size);
  123. net_send_packet(efi_bounce_buffer, buffer_size);
  124. #else
  125. net_send_packet(buffer, buffer_size);
  126. #endif
  127. new_tx_packet = buffer;
  128. return EFI_EXIT(EFI_SUCCESS);
  129. }
  130. static void efi_net_push(void *pkt, int len)
  131. {
  132. new_rx_packet = true;
  133. }
  134. static efi_status_t EFIAPI efi_net_receive(struct efi_simple_network *this,
  135. ulong *header_size, ulong *buffer_size, void *buffer,
  136. struct efi_mac_address *src_addr,
  137. struct efi_mac_address *dest_addr, u16 *protocol)
  138. {
  139. EFI_ENTRY("%p, %p, %p, %p, %p, %p, %p", this, header_size,
  140. buffer_size, buffer, src_addr, dest_addr, protocol);
  141. push_packet = efi_net_push;
  142. eth_rx();
  143. push_packet = NULL;
  144. if (!new_rx_packet)
  145. return EFI_EXIT(EFI_NOT_READY);
  146. if (*buffer_size < net_rx_packet_len) {
  147. /* Packet doesn't fit, try again with bigger buf */
  148. *buffer_size = net_rx_packet_len;
  149. return EFI_EXIT(EFI_BUFFER_TOO_SMALL);
  150. }
  151. memcpy(buffer, net_rx_packet, net_rx_packet_len);
  152. *buffer_size = net_rx_packet_len;
  153. new_rx_packet = false;
  154. return EFI_EXIT(EFI_SUCCESS);
  155. }
  156. void efi_net_set_dhcp_ack(void *pkt, int len)
  157. {
  158. int maxsize = sizeof(*dhcp_ack);
  159. if (!dhcp_ack)
  160. dhcp_ack = malloc(maxsize);
  161. memcpy(dhcp_ack, pkt, min(len, maxsize));
  162. }
  163. /* This gets called from do_bootefi_exec(). */
  164. int efi_net_register(void)
  165. {
  166. struct efi_net_obj *netobj;
  167. if (!eth_get_dev()) {
  168. /* No eth device active, don't expose any */
  169. return 0;
  170. }
  171. /* We only expose the "active" eth device, so one is enough */
  172. netobj = calloc(1, sizeof(*netobj));
  173. /* Fill in object data */
  174. netobj->parent.protocols[0].guid = &efi_net_guid;
  175. netobj->parent.protocols[0].protocol_interface = &netobj->net;
  176. netobj->parent.protocols[1].guid = &efi_guid_device_path;
  177. netobj->parent.protocols[1].protocol_interface =
  178. efi_dp_from_eth();
  179. netobj->parent.protocols[2].guid = &efi_pxe_guid;
  180. netobj->parent.protocols[2].protocol_interface = &netobj->pxe;
  181. netobj->parent.handle = &netobj->net;
  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.max_packet_size = PKTSIZE;
  199. netobj->pxe.mode = &netobj->pxe_mode;
  200. if (dhcp_ack)
  201. netobj->pxe_mode.dhcp_ack = *dhcp_ack;
  202. /* Hook net up to the device list */
  203. list_add_tail(&netobj->parent.link, &efi_obj_list);
  204. return 0;
  205. }