efi_net.c 7.9 KB

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