efi_net.c 11 KB

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