efi_net.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. ulong header_size, ulong 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, %lx, %lx, %p, %p, %p, %p", this, header_size,
  127. buffer_size, buffer, src_addr, dest_addr, protocol);
  128. efi_timer_check();
  129. if (header_size) {
  130. /* We would need to create the header if header_size != 0 */
  131. return EFI_EXIT(EFI_INVALID_PARAMETER);
  132. }
  133. #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
  134. /* Ethernet packets always fit, just bounce */
  135. memcpy(efi_bounce_buffer, buffer, buffer_size);
  136. net_send_packet(efi_bounce_buffer, buffer_size);
  137. #else
  138. net_send_packet(buffer, buffer_size);
  139. #endif
  140. new_tx_packet = buffer;
  141. return EFI_EXIT(EFI_SUCCESS);
  142. }
  143. static void efi_net_push(void *pkt, int len)
  144. {
  145. new_rx_packet = true;
  146. wait_for_packet->is_signaled = true;
  147. }
  148. static efi_status_t EFIAPI efi_net_receive(struct efi_simple_network *this,
  149. ulong *header_size, ulong *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, %p, %p, %p, %p, %p, %p", this, header_size,
  154. buffer_size, buffer, src_addr, dest_addr, protocol);
  155. efi_timer_check();
  156. if (!new_rx_packet)
  157. return EFI_EXIT(EFI_NOT_READY);
  158. if (*buffer_size < net_rx_packet_len) {
  159. /* Packet doesn't fit, try again with bigger buf */
  160. *buffer_size = net_rx_packet_len;
  161. return EFI_EXIT(EFI_BUFFER_TOO_SMALL);
  162. }
  163. memcpy(buffer, net_rx_packet, net_rx_packet_len);
  164. *buffer_size = net_rx_packet_len;
  165. new_rx_packet = false;
  166. return EFI_EXIT(EFI_SUCCESS);
  167. }
  168. void efi_net_set_dhcp_ack(void *pkt, int len)
  169. {
  170. int maxsize = sizeof(*dhcp_ack);
  171. if (!dhcp_ack)
  172. dhcp_ack = malloc(maxsize);
  173. memcpy(dhcp_ack, pkt, min(len, maxsize));
  174. }
  175. /*
  176. * Check if a new network packet has been received.
  177. *
  178. * This notification function is called in every timer cycle.
  179. *
  180. * @event the event for which this notification function is registered
  181. * @context event context - not used in this function
  182. */
  183. static void EFIAPI efi_network_timer_notify(struct efi_event *event,
  184. void *context)
  185. {
  186. EFI_ENTRY("%p, %p", event, context);
  187. if (!new_rx_packet) {
  188. push_packet = efi_net_push;
  189. eth_rx();
  190. push_packet = NULL;
  191. }
  192. EFI_EXIT(EFI_SUCCESS);
  193. }
  194. /* This gets called from do_bootefi_exec(). */
  195. int efi_net_register(void)
  196. {
  197. struct efi_net_obj *netobj;
  198. efi_status_t r;
  199. if (!eth_get_dev()) {
  200. /* No eth device active, don't expose any */
  201. return 0;
  202. }
  203. /* We only expose the "active" eth device, so one is enough */
  204. netobj = calloc(1, sizeof(*netobj));
  205. /* Fill in object data */
  206. netobj->parent.protocols[0].guid = &efi_net_guid;
  207. netobj->parent.protocols[0].protocol_interface = &netobj->net;
  208. netobj->parent.protocols[1].guid = &efi_guid_device_path;
  209. netobj->parent.protocols[1].protocol_interface =
  210. efi_dp_from_eth();
  211. netobj->parent.protocols[2].guid = &efi_pxe_guid;
  212. netobj->parent.protocols[2].protocol_interface = &netobj->pxe;
  213. netobj->parent.handle = &netobj->net;
  214. netobj->net.revision = EFI_SIMPLE_NETWORK_PROTOCOL_REVISION;
  215. netobj->net.start = efi_net_start;
  216. netobj->net.stop = efi_net_stop;
  217. netobj->net.initialize = efi_net_initialize;
  218. netobj->net.reset = efi_net_reset;
  219. netobj->net.shutdown = efi_net_shutdown;
  220. netobj->net.receive_filters = efi_net_receive_filters;
  221. netobj->net.station_address = efi_net_station_address;
  222. netobj->net.statistics = efi_net_statistics;
  223. netobj->net.mcastiptomac = efi_net_mcastiptomac;
  224. netobj->net.nvdata = efi_net_nvdata;
  225. netobj->net.get_status = efi_net_get_status;
  226. netobj->net.transmit = efi_net_transmit;
  227. netobj->net.receive = efi_net_receive;
  228. netobj->net.mode = &netobj->net_mode;
  229. netobj->net_mode.state = EFI_NETWORK_STARTED;
  230. memcpy(netobj->net_mode.current_address.mac_addr, eth_get_ethaddr(), 6);
  231. netobj->net_mode.hwaddr_size = ARP_HLEN;
  232. netobj->net_mode.max_packet_size = PKTSIZE;
  233. netobj->pxe.mode = &netobj->pxe_mode;
  234. if (dhcp_ack)
  235. netobj->pxe_mode.dhcp_ack = *dhcp_ack;
  236. /* Hook net up to the device list */
  237. list_add_tail(&netobj->parent.link, &efi_obj_list);
  238. /*
  239. * Create WaitForPacket event.
  240. */
  241. r = efi_create_event(EVT_NOTIFY_WAIT, TPL_CALLBACK,
  242. efi_network_timer_notify, NULL,
  243. &wait_for_packet);
  244. if (r != EFI_SUCCESS) {
  245. printf("ERROR: Failed to register network event\n");
  246. return r;
  247. }
  248. netobj->net.wait_for_packet = wait_for_packet;
  249. /*
  250. * Create a timer event.
  251. *
  252. * The notification function is used to check if a new network packet
  253. * has been received.
  254. */
  255. r = efi_create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_CALLBACK,
  256. efi_network_timer_notify, NULL,
  257. &network_timer_event);
  258. if (r != EFI_SUCCESS) {
  259. printf("ERROR: Failed to register network event\n");
  260. return r;
  261. }
  262. /* Network is time critical, create event in every timer cyle */
  263. r = efi_set_timer(network_timer_event, EFI_TIMER_PERIODIC, 0);
  264. if (r != EFI_SUCCESS) {
  265. printf("ERROR: Failed to set network timer\n");
  266. return r;
  267. }
  268. return 0;
  269. }