efi_net.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. EFI_ENTRY("%p, %p, %p, %p, %p, %p, %p", this, header_size,
  170. buffer_size, buffer, src_addr, dest_addr, protocol);
  171. efi_timer_check();
  172. if (!new_rx_packet)
  173. return EFI_EXIT(EFI_NOT_READY);
  174. if (*buffer_size < net_rx_packet_len) {
  175. /* Packet doesn't fit, try again with bigger buf */
  176. *buffer_size = net_rx_packet_len;
  177. return EFI_EXIT(EFI_BUFFER_TOO_SMALL);
  178. }
  179. memcpy(buffer, net_rx_packet, net_rx_packet_len);
  180. *buffer_size = net_rx_packet_len;
  181. new_rx_packet = false;
  182. return EFI_EXIT(EFI_SUCCESS);
  183. }
  184. void efi_net_set_dhcp_ack(void *pkt, int len)
  185. {
  186. int maxsize = sizeof(*dhcp_ack);
  187. if (!dhcp_ack)
  188. dhcp_ack = malloc(maxsize);
  189. memcpy(dhcp_ack, pkt, min(len, maxsize));
  190. }
  191. /*
  192. * Check if a new network packet has been received.
  193. *
  194. * This notification function is called in every timer cycle.
  195. *
  196. * @event the event for which this notification function is registered
  197. * @context event context - not used in this function
  198. */
  199. static void EFIAPI efi_network_timer_notify(struct efi_event *event,
  200. void *context)
  201. {
  202. EFI_ENTRY("%p, %p", event, context);
  203. if (!new_rx_packet) {
  204. push_packet = efi_net_push;
  205. eth_rx();
  206. push_packet = NULL;
  207. }
  208. EFI_EXIT(EFI_SUCCESS);
  209. }
  210. /* This gets called from do_bootefi_exec(). */
  211. int efi_net_register(void)
  212. {
  213. struct efi_net_obj *netobj;
  214. efi_status_t r;
  215. if (!eth_get_dev()) {
  216. /* No eth device active, don't expose any */
  217. return 0;
  218. }
  219. /* We only expose the "active" eth device, so one is enough */
  220. netobj = calloc(1, sizeof(*netobj));
  221. /* Fill in object data */
  222. netobj->parent.protocols[0].guid = &efi_net_guid;
  223. netobj->parent.protocols[0].protocol_interface = &netobj->net;
  224. netobj->parent.protocols[1].guid = &efi_guid_device_path;
  225. netobj->parent.protocols[1].protocol_interface =
  226. efi_dp_from_eth();
  227. netobj->parent.protocols[2].guid = &efi_pxe_guid;
  228. netobj->parent.protocols[2].protocol_interface = &netobj->pxe;
  229. netobj->parent.handle = &netobj->net;
  230. netobj->net.revision = EFI_SIMPLE_NETWORK_PROTOCOL_REVISION;
  231. netobj->net.start = efi_net_start;
  232. netobj->net.stop = efi_net_stop;
  233. netobj->net.initialize = efi_net_initialize;
  234. netobj->net.reset = efi_net_reset;
  235. netobj->net.shutdown = efi_net_shutdown;
  236. netobj->net.receive_filters = efi_net_receive_filters;
  237. netobj->net.station_address = efi_net_station_address;
  238. netobj->net.statistics = efi_net_statistics;
  239. netobj->net.mcastiptomac = efi_net_mcastiptomac;
  240. netobj->net.nvdata = efi_net_nvdata;
  241. netobj->net.get_status = efi_net_get_status;
  242. netobj->net.transmit = efi_net_transmit;
  243. netobj->net.receive = efi_net_receive;
  244. netobj->net.mode = &netobj->net_mode;
  245. netobj->net_mode.state = EFI_NETWORK_STARTED;
  246. memcpy(netobj->net_mode.current_address.mac_addr, eth_get_ethaddr(), 6);
  247. netobj->net_mode.hwaddr_size = ARP_HLEN;
  248. netobj->net_mode.max_packet_size = PKTSIZE;
  249. netobj->pxe.mode = &netobj->pxe_mode;
  250. if (dhcp_ack)
  251. netobj->pxe_mode.dhcp_ack = *dhcp_ack;
  252. /* Hook net up to the device list */
  253. list_add_tail(&netobj->parent.link, &efi_obj_list);
  254. /*
  255. * Create WaitForPacket event.
  256. */
  257. r = efi_create_event(EVT_NOTIFY_WAIT, TPL_CALLBACK,
  258. efi_network_timer_notify, NULL,
  259. &wait_for_packet);
  260. if (r != EFI_SUCCESS) {
  261. printf("ERROR: Failed to register network event\n");
  262. return r;
  263. }
  264. netobj->net.wait_for_packet = wait_for_packet;
  265. /*
  266. * Create a timer event.
  267. *
  268. * The notification function is used to check if a new network packet
  269. * has been received.
  270. */
  271. r = efi_create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_CALLBACK,
  272. efi_network_timer_notify, NULL,
  273. &network_timer_event);
  274. if (r != EFI_SUCCESS) {
  275. printf("ERROR: Failed to register network event\n");
  276. return r;
  277. }
  278. /* Network is time critical, create event in every timer cyle */
  279. r = efi_set_timer(network_timer_event, EFI_TIMER_PERIODIC, 0);
  280. if (r != EFI_SUCCESS) {
  281. printf("ERROR: Failed to set network timer\n");
  282. return r;
  283. }
  284. return 0;
  285. }