efi_net.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. /*
  20. * The notification function of this event is called in every timer cycle
  21. * to check if a new network packet has been received.
  22. */
  23. static struct efi_event *network_timer_event;
  24. /*
  25. * This event is signaled when a packet has been received.
  26. */
  27. static struct efi_event *wait_for_packet;
  28. struct efi_net_obj {
  29. /* Generic EFI object parent class data */
  30. struct efi_object parent;
  31. /* EFI Interface callback struct for network */
  32. struct efi_simple_network net;
  33. struct efi_simple_network_mode net_mode;
  34. /* PXE struct to transmit dhcp data */
  35. struct efi_pxe pxe;
  36. struct efi_pxe_mode pxe_mode;
  37. };
  38. static efi_status_t EFIAPI efi_net_start(struct efi_simple_network *this)
  39. {
  40. EFI_ENTRY("%p", this);
  41. return EFI_EXIT(EFI_SUCCESS);
  42. }
  43. static efi_status_t EFIAPI efi_net_stop(struct efi_simple_network *this)
  44. {
  45. EFI_ENTRY("%p", this);
  46. return EFI_EXIT(EFI_SUCCESS);
  47. }
  48. static efi_status_t EFIAPI efi_net_initialize(struct efi_simple_network *this,
  49. ulong extra_rx, ulong extra_tx)
  50. {
  51. EFI_ENTRY("%p, %lx, %lx", this, extra_rx, extra_tx);
  52. eth_init();
  53. return EFI_EXIT(EFI_SUCCESS);
  54. }
  55. static efi_status_t EFIAPI efi_net_reset(struct efi_simple_network *this,
  56. int extended_verification)
  57. {
  58. EFI_ENTRY("%p, %x", this, extended_verification);
  59. return EFI_EXIT(EFI_SUCCESS);
  60. }
  61. static efi_status_t EFIAPI efi_net_shutdown(struct efi_simple_network *this)
  62. {
  63. EFI_ENTRY("%p", this);
  64. return EFI_EXIT(EFI_SUCCESS);
  65. }
  66. static efi_status_t EFIAPI efi_net_receive_filters(
  67. struct efi_simple_network *this, u32 enable, u32 disable,
  68. int reset_mcast_filter, ulong mcast_filter_count,
  69. struct efi_mac_address *mcast_filter)
  70. {
  71. EFI_ENTRY("%p, %x, %x, %x, %lx, %p", this, enable, disable,
  72. reset_mcast_filter, mcast_filter_count, mcast_filter);
  73. return EFI_EXIT(EFI_UNSUPPORTED);
  74. }
  75. static efi_status_t EFIAPI efi_net_station_address(
  76. struct efi_simple_network *this, int reset,
  77. struct efi_mac_address *new_mac)
  78. {
  79. EFI_ENTRY("%p, %x, %p", this, reset, new_mac);
  80. return EFI_EXIT(EFI_UNSUPPORTED);
  81. }
  82. static efi_status_t EFIAPI efi_net_statistics(struct efi_simple_network *this,
  83. int reset, ulong *stat_size,
  84. void *stat_table)
  85. {
  86. EFI_ENTRY("%p, %x, %p, %p", this, reset, stat_size, stat_table);
  87. return EFI_EXIT(EFI_UNSUPPORTED);
  88. }
  89. static efi_status_t EFIAPI efi_net_mcastiptomac(struct efi_simple_network *this,
  90. int ipv6,
  91. struct efi_ip_address *ip,
  92. struct efi_mac_address *mac)
  93. {
  94. EFI_ENTRY("%p, %x, %p, %p", this, ipv6, ip, mac);
  95. return EFI_EXIT(EFI_INVALID_PARAMETER);
  96. }
  97. static efi_status_t EFIAPI efi_net_nvdata(struct efi_simple_network *this,
  98. int read_write, ulong offset,
  99. ulong buffer_size, char *buffer)
  100. {
  101. EFI_ENTRY("%p, %x, %lx, %lx, %p", this, read_write, offset, buffer_size,
  102. buffer);
  103. return EFI_EXIT(EFI_UNSUPPORTED);
  104. }
  105. static efi_status_t EFIAPI efi_net_get_status(struct efi_simple_network *this,
  106. u32 *int_status, void **txbuf)
  107. {
  108. EFI_ENTRY("%p, %p, %p", this, int_status, txbuf);
  109. efi_timer_check();
  110. if (int_status) {
  111. /* We send packets synchronously, so nothing is outstanding */
  112. *int_status = EFI_SIMPLE_NETWORK_TRANSMIT_INTERRUPT;
  113. if (new_rx_packet)
  114. *int_status |= EFI_SIMPLE_NETWORK_RECEIVE_INTERRUPT;
  115. }
  116. if (txbuf)
  117. *txbuf = new_tx_packet;
  118. new_tx_packet = NULL;
  119. return EFI_EXIT(EFI_SUCCESS);
  120. }
  121. static efi_status_t EFIAPI efi_net_transmit(struct efi_simple_network *this,
  122. size_t header_size, size_t buffer_size, void *buffer,
  123. struct efi_mac_address *src_addr,
  124. struct efi_mac_address *dest_addr, u16 *protocol)
  125. {
  126. EFI_ENTRY("%p, %lu, %lu, %p, %p, %p, %p", this,
  127. (unsigned long)header_size, (unsigned long)buffer_size,
  128. buffer, src_addr, dest_addr, protocol);
  129. efi_timer_check();
  130. if (header_size) {
  131. /* We would need to create the header if header_size != 0 */
  132. return EFI_EXIT(EFI_INVALID_PARAMETER);
  133. }
  134. #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
  135. /* Ethernet packets always fit, just bounce */
  136. memcpy(efi_bounce_buffer, buffer, buffer_size);
  137. net_send_packet(efi_bounce_buffer, buffer_size);
  138. #else
  139. net_send_packet(buffer, buffer_size);
  140. #endif
  141. new_tx_packet = buffer;
  142. return EFI_EXIT(EFI_SUCCESS);
  143. }
  144. static void efi_net_push(void *pkt, int len)
  145. {
  146. new_rx_packet = true;
  147. wait_for_packet->is_signaled = true;
  148. }
  149. /*
  150. * Receive a packet from a network interface.
  151. *
  152. * This function implements the Receive service of the Simple Network Protocol.
  153. * See the UEFI spec for details.
  154. *
  155. * @this the instance of the Simple Network Protocol
  156. * @header_size size of the media header
  157. * @buffer_size size of the buffer to receive the packet
  158. * @buffer buffer to receive the packet
  159. * @src_addr source MAC address
  160. * @dest_addr destination MAC address
  161. * @protocol protocol
  162. * @return status code
  163. */
  164. static efi_status_t EFIAPI efi_net_receive(struct efi_simple_network *this,
  165. size_t *header_size, size_t *buffer_size, void *buffer,
  166. struct efi_mac_address *src_addr,
  167. struct efi_mac_address *dest_addr, u16 *protocol)
  168. {
  169. struct ethernet_hdr *eth_hdr;
  170. size_t hdr_size = sizeof(struct ethernet_hdr);
  171. u16 protlen;
  172. EFI_ENTRY("%p, %p, %p, %p, %p, %p, %p", this, header_size,
  173. buffer_size, buffer, src_addr, dest_addr, protocol);
  174. efi_timer_check();
  175. if (!new_rx_packet)
  176. return EFI_EXIT(EFI_NOT_READY);
  177. /* Check that we at least received an Ethernet header */
  178. if (net_rx_packet_len < sizeof(struct ethernet_hdr)) {
  179. new_rx_packet = false;
  180. return EFI_EXIT(EFI_NOT_READY);
  181. }
  182. /* Fill export parameters */
  183. eth_hdr = (struct ethernet_hdr *)net_rx_packet;
  184. protlen = ntohs(eth_hdr->et_protlen);
  185. if (protlen == 0x8100) {
  186. hdr_size += 4;
  187. protlen = ntohs(*(u16 *)&net_rx_packet[hdr_size - 2]);
  188. }
  189. if (header_size)
  190. *header_size = hdr_size;
  191. if (dest_addr)
  192. memcpy(dest_addr, eth_hdr->et_dest, ARP_HLEN);
  193. if (src_addr)
  194. memcpy(src_addr, eth_hdr->et_src, ARP_HLEN);
  195. if (protocol)
  196. *protocol = protlen;
  197. if (*buffer_size < net_rx_packet_len) {
  198. /* Packet doesn't fit, try again with bigger buf */
  199. *buffer_size = net_rx_packet_len;
  200. return EFI_EXIT(EFI_BUFFER_TOO_SMALL);
  201. }
  202. /* Copy packet */
  203. memcpy(buffer, net_rx_packet, net_rx_packet_len);
  204. *buffer_size = net_rx_packet_len;
  205. new_rx_packet = false;
  206. return EFI_EXIT(EFI_SUCCESS);
  207. }
  208. void efi_net_set_dhcp_ack(void *pkt, int len)
  209. {
  210. int maxsize = sizeof(*dhcp_ack);
  211. if (!dhcp_ack)
  212. dhcp_ack = malloc(maxsize);
  213. memcpy(dhcp_ack, pkt, min(len, maxsize));
  214. }
  215. /*
  216. * Check if a new network packet has been received.
  217. *
  218. * This notification function is called in every timer cycle.
  219. *
  220. * @event the event for which this notification function is registered
  221. * @context event context - not used in this function
  222. */
  223. static void EFIAPI efi_network_timer_notify(struct efi_event *event,
  224. void *context)
  225. {
  226. EFI_ENTRY("%p, %p", event, context);
  227. if (!new_rx_packet) {
  228. push_packet = efi_net_push;
  229. eth_rx();
  230. push_packet = NULL;
  231. }
  232. EFI_EXIT(EFI_SUCCESS);
  233. }
  234. /* This gets called from do_bootefi_exec(). */
  235. efi_status_t efi_net_register(void)
  236. {
  237. struct efi_net_obj *netobj;
  238. efi_status_t r;
  239. if (!eth_get_dev()) {
  240. /* No eth device active, don't expose any */
  241. return EFI_SUCCESS;
  242. }
  243. /* We only expose the "active" eth device, so one is enough */
  244. netobj = calloc(1, sizeof(*netobj));
  245. if (!netobj) {
  246. printf("ERROR: Out of memory\n");
  247. return EFI_OUT_OF_RESOURCES;
  248. }
  249. /* Hook net up to the device list */
  250. efi_add_handle(&netobj->parent);
  251. /* Fill in object data */
  252. r = efi_add_protocol(netobj->parent.handle, &efi_net_guid,
  253. &netobj->net);
  254. if (r != EFI_SUCCESS)
  255. goto failure_to_add_protocol;
  256. r = efi_add_protocol(netobj->parent.handle, &efi_guid_device_path,
  257. efi_dp_from_eth());
  258. if (r != EFI_SUCCESS)
  259. goto failure_to_add_protocol;
  260. r = efi_add_protocol(netobj->parent.handle, &efi_pxe_guid,
  261. &netobj->pxe);
  262. if (r != EFI_SUCCESS)
  263. goto failure_to_add_protocol;
  264. netobj->net.revision = EFI_SIMPLE_NETWORK_PROTOCOL_REVISION;
  265. netobj->net.start = efi_net_start;
  266. netobj->net.stop = efi_net_stop;
  267. netobj->net.initialize = efi_net_initialize;
  268. netobj->net.reset = efi_net_reset;
  269. netobj->net.shutdown = efi_net_shutdown;
  270. netobj->net.receive_filters = efi_net_receive_filters;
  271. netobj->net.station_address = efi_net_station_address;
  272. netobj->net.statistics = efi_net_statistics;
  273. netobj->net.mcastiptomac = efi_net_mcastiptomac;
  274. netobj->net.nvdata = efi_net_nvdata;
  275. netobj->net.get_status = efi_net_get_status;
  276. netobj->net.transmit = efi_net_transmit;
  277. netobj->net.receive = efi_net_receive;
  278. netobj->net.mode = &netobj->net_mode;
  279. netobj->net_mode.state = EFI_NETWORK_STARTED;
  280. memcpy(netobj->net_mode.current_address.mac_addr, eth_get_ethaddr(), 6);
  281. netobj->net_mode.hwaddr_size = ARP_HLEN;
  282. netobj->net_mode.max_packet_size = PKTSIZE;
  283. netobj->pxe.mode = &netobj->pxe_mode;
  284. if (dhcp_ack)
  285. netobj->pxe_mode.dhcp_ack = *dhcp_ack;
  286. /*
  287. * Create WaitForPacket event.
  288. */
  289. r = efi_create_event(EVT_NOTIFY_WAIT, TPL_CALLBACK,
  290. efi_network_timer_notify, NULL, NULL,
  291. &wait_for_packet);
  292. if (r != EFI_SUCCESS) {
  293. printf("ERROR: Failed to register network event\n");
  294. return r;
  295. }
  296. netobj->net.wait_for_packet = wait_for_packet;
  297. /*
  298. * Create a timer event.
  299. *
  300. * The notification function is used to check if a new network packet
  301. * has been received.
  302. */
  303. r = efi_create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_CALLBACK,
  304. efi_network_timer_notify, NULL, NULL,
  305. &network_timer_event);
  306. if (r != EFI_SUCCESS) {
  307. printf("ERROR: Failed to register network event\n");
  308. return r;
  309. }
  310. /* Network is time critical, create event in every timer cyle */
  311. r = efi_set_timer(network_timer_event, EFI_TIMER_PERIODIC, 0);
  312. if (r != EFI_SUCCESS) {
  313. printf("ERROR: Failed to set network timer\n");
  314. return r;
  315. }
  316. return EFI_SUCCESS;
  317. failure_to_add_protocol:
  318. printf("ERROR: Failure to add protocol\n");
  319. return r;
  320. }