efi_net.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. /* Device path to the network adapter */
  26. struct efi_device_path_mac_addr dp_mac;
  27. struct efi_device_path_file_path dp_end;
  28. /* PXE struct to transmit dhcp data */
  29. struct efi_pxe pxe;
  30. struct efi_pxe_mode pxe_mode;
  31. };
  32. static efi_status_t EFIAPI efi_net_start(struct efi_simple_network *this)
  33. {
  34. EFI_ENTRY("%p", this);
  35. return EFI_EXIT(EFI_SUCCESS);
  36. }
  37. static efi_status_t EFIAPI efi_net_stop(struct efi_simple_network *this)
  38. {
  39. EFI_ENTRY("%p", this);
  40. return EFI_EXIT(EFI_SUCCESS);
  41. }
  42. static efi_status_t EFIAPI efi_net_initialize(struct efi_simple_network *this,
  43. ulong extra_rx, ulong extra_tx)
  44. {
  45. EFI_ENTRY("%p, %lx, %lx", this, extra_rx, extra_tx);
  46. eth_init();
  47. return EFI_EXIT(EFI_SUCCESS);
  48. }
  49. static efi_status_t EFIAPI efi_net_reset(struct efi_simple_network *this,
  50. int extended_verification)
  51. {
  52. EFI_ENTRY("%p, %x", this, extended_verification);
  53. return EFI_EXIT(EFI_SUCCESS);
  54. }
  55. static efi_status_t EFIAPI efi_net_shutdown(struct efi_simple_network *this)
  56. {
  57. EFI_ENTRY("%p", this);
  58. return EFI_EXIT(EFI_SUCCESS);
  59. }
  60. static efi_status_t EFIAPI efi_net_receive_filters(
  61. struct efi_simple_network *this, u32 enable, u32 disable,
  62. int reset_mcast_filter, ulong mcast_filter_count,
  63. struct efi_mac_address *mcast_filter)
  64. {
  65. EFI_ENTRY("%p, %x, %x, %x, %lx, %p", this, enable, disable,
  66. reset_mcast_filter, mcast_filter_count, mcast_filter);
  67. /* XXX Do we care? */
  68. return EFI_EXIT(EFI_SUCCESS);
  69. }
  70. static efi_status_t EFIAPI efi_net_station_address(
  71. struct efi_simple_network *this, int reset,
  72. struct efi_mac_address *new_mac)
  73. {
  74. EFI_ENTRY("%p, %x, %p", this, reset, new_mac);
  75. return EFI_EXIT(EFI_INVALID_PARAMETER);
  76. }
  77. static efi_status_t EFIAPI efi_net_statistics(struct efi_simple_network *this,
  78. int reset, ulong *stat_size,
  79. void *stat_table)
  80. {
  81. EFI_ENTRY("%p, %x, %p, %p", this, reset, stat_size, stat_table);
  82. return EFI_EXIT(EFI_INVALID_PARAMETER);
  83. }
  84. static efi_status_t EFIAPI efi_net_mcastiptomac(struct efi_simple_network *this,
  85. int ipv6,
  86. struct efi_ip_address *ip,
  87. struct efi_mac_address *mac)
  88. {
  89. EFI_ENTRY("%p, %x, %p, %p", this, ipv6, ip, mac);
  90. return EFI_EXIT(EFI_INVALID_PARAMETER);
  91. }
  92. static efi_status_t EFIAPI efi_net_nvdata(struct efi_simple_network *this,
  93. int read_write, ulong offset,
  94. ulong buffer_size, char *buffer)
  95. {
  96. EFI_ENTRY("%p, %x, %lx, %lx, %p", this, read_write, offset, buffer_size,
  97. buffer);
  98. return EFI_EXIT(EFI_INVALID_PARAMETER);
  99. }
  100. static efi_status_t EFIAPI efi_net_get_status(struct efi_simple_network *this,
  101. u32 *int_status, void **txbuf)
  102. {
  103. EFI_ENTRY("%p, %p, %p", this, int_status, txbuf);
  104. /* We send packets synchronously, so nothing is outstanding */
  105. if (int_status)
  106. *int_status = 0;
  107. if (txbuf)
  108. *txbuf = new_tx_packet;
  109. new_tx_packet = NULL;
  110. return EFI_EXIT(EFI_SUCCESS);
  111. }
  112. static efi_status_t EFIAPI efi_net_transmit(struct efi_simple_network *this,
  113. ulong header_size, ulong buffer_size, void *buffer,
  114. struct efi_mac_address *src_addr,
  115. struct efi_mac_address *dest_addr, u16 *protocol)
  116. {
  117. EFI_ENTRY("%p, %lx, %lx, %p, %p, %p, %p", this, header_size,
  118. buffer_size, buffer, src_addr, dest_addr, protocol);
  119. if (header_size) {
  120. /* We would need to create the header if header_size != 0 */
  121. return EFI_EXIT(EFI_INVALID_PARAMETER);
  122. }
  123. #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
  124. /* Ethernet packets always fit, just bounce */
  125. memcpy(efi_bounce_buffer, buffer, buffer_size);
  126. net_send_packet(efi_bounce_buffer, buffer_size);
  127. #else
  128. net_send_packet(buffer, buffer_size);
  129. #endif
  130. new_tx_packet = buffer;
  131. return EFI_EXIT(EFI_SUCCESS);
  132. }
  133. static void efi_net_push(void *pkt, int len)
  134. {
  135. new_rx_packet = true;
  136. }
  137. static efi_status_t EFIAPI efi_net_receive(struct efi_simple_network *this,
  138. ulong *header_size, ulong *buffer_size, void *buffer,
  139. struct efi_mac_address *src_addr,
  140. struct efi_mac_address *dest_addr, u16 *protocol)
  141. {
  142. EFI_ENTRY("%p, %p, %p, %p, %p, %p, %p", this, header_size,
  143. buffer_size, buffer, src_addr, dest_addr, protocol);
  144. push_packet = efi_net_push;
  145. eth_rx();
  146. push_packet = NULL;
  147. if (!new_rx_packet)
  148. return EFI_EXIT(EFI_NOT_READY);
  149. if (*buffer_size < net_rx_packet_len) {
  150. /* Packet doesn't fit, try again with bigger buf */
  151. *buffer_size = net_rx_packet_len;
  152. return EFI_EXIT(EFI_BUFFER_TOO_SMALL);
  153. }
  154. memcpy(buffer, net_rx_packet, net_rx_packet_len);
  155. *buffer_size = net_rx_packet_len;
  156. new_rx_packet = false;
  157. return EFI_EXIT(EFI_SUCCESS);
  158. }
  159. static efi_status_t EFIAPI efi_net_open_dp(void *handle, efi_guid_t *protocol,
  160. void **protocol_interface, void *agent_handle,
  161. void *controller_handle, uint32_t attributes)
  162. {
  163. struct efi_simple_network *net = handle;
  164. struct efi_net_obj *netobj = container_of(net, struct efi_net_obj, net);
  165. *protocol_interface = &netobj->dp_mac;
  166. return EFI_SUCCESS;
  167. }
  168. static efi_status_t EFIAPI efi_net_open_pxe(void *handle, efi_guid_t *protocol,
  169. void **protocol_interface, void *agent_handle,
  170. void *controller_handle, uint32_t attributes)
  171. {
  172. struct efi_simple_network *net = handle;
  173. struct efi_net_obj *netobj = container_of(net, struct efi_net_obj, net);
  174. *protocol_interface = &netobj->pxe;
  175. return EFI_SUCCESS;
  176. }
  177. void efi_net_set_dhcp_ack(void *pkt, int len)
  178. {
  179. int maxsize = sizeof(*dhcp_ack);
  180. if (!dhcp_ack)
  181. dhcp_ack = malloc(maxsize);
  182. memcpy(dhcp_ack, pkt, min(len, maxsize));
  183. }
  184. /* This gets called from do_bootefi_exec(). */
  185. int efi_net_register(void **handle)
  186. {
  187. struct efi_net_obj *netobj;
  188. struct efi_device_path_mac_addr dp_net = {
  189. .dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE,
  190. .dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR,
  191. .dp.length = sizeof(dp_net),
  192. };
  193. struct efi_device_path_file_path dp_end = {
  194. .dp.type = DEVICE_PATH_TYPE_END,
  195. .dp.sub_type = DEVICE_PATH_SUB_TYPE_END,
  196. .dp.length = sizeof(dp_end),
  197. };
  198. if (!eth_get_dev()) {
  199. /* No eth device active, don't expose any */
  200. return 0;
  201. }
  202. /* We only expose the "active" eth device, so one is enough */
  203. netobj = calloc(1, sizeof(*netobj));
  204. /* Fill in object data */
  205. netobj->parent.protocols[0].guid = &efi_net_guid;
  206. netobj->parent.protocols[0].open = efi_return_handle;
  207. netobj->parent.protocols[1].guid = &efi_guid_device_path;
  208. netobj->parent.protocols[1].open = efi_net_open_dp;
  209. netobj->parent.protocols[2].guid = &efi_pxe_guid;
  210. netobj->parent.protocols[2].open = efi_net_open_pxe;
  211. netobj->parent.handle = &netobj->net;
  212. netobj->net.start = efi_net_start;
  213. netobj->net.stop = efi_net_stop;
  214. netobj->net.initialize = efi_net_initialize;
  215. netobj->net.reset = efi_net_reset;
  216. netobj->net.shutdown = efi_net_shutdown;
  217. netobj->net.receive_filters = efi_net_receive_filters;
  218. netobj->net.station_address = efi_net_station_address;
  219. netobj->net.statistics = efi_net_statistics;
  220. netobj->net.mcastiptomac = efi_net_mcastiptomac;
  221. netobj->net.nvdata = efi_net_nvdata;
  222. netobj->net.get_status = efi_net_get_status;
  223. netobj->net.transmit = efi_net_transmit;
  224. netobj->net.receive = efi_net_receive;
  225. netobj->net.mode = &netobj->net_mode;
  226. netobj->net_mode.state = EFI_NETWORK_STARTED;
  227. netobj->dp_mac = dp_net;
  228. netobj->dp_end = dp_end;
  229. memcpy(netobj->dp_mac.mac.addr, eth_get_ethaddr(), 6);
  230. memcpy(netobj->net_mode.current_address.mac_addr, eth_get_ethaddr(), 6);
  231. netobj->net_mode.max_packet_size = PKTSIZE;
  232. netobj->pxe.mode = &netobj->pxe_mode;
  233. if (dhcp_ack)
  234. netobj->pxe_mode.dhcp_ack = *dhcp_ack;
  235. /* Hook net up to the device list */
  236. list_add_tail(&netobj->parent.link, &efi_obj_list);
  237. if (handle)
  238. *handle = &netobj->net;
  239. return 0;
  240. }