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