efi_net.c 11 KB

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