1 // SPDX-License-Identifier: GPL-2.0+
3 * EFI application network access support
5 * Copyright (c) 2016 Alexander Graf
9 #include <efi_loader.h>
13 static const efi_guid_t efi_net_guid = EFI_SIMPLE_NETWORK_GUID;
14 static const efi_guid_t efi_pxe_guid = EFI_PXE_GUID;
15 static struct efi_pxe_packet *dhcp_ack;
16 static bool new_rx_packet;
17 static void *new_tx_packet;
19 * The notification function of this event is called in every timer cycle
20 * to check if a new network packet has been received.
22 static struct efi_event *network_timer_event;
24 * This event is signaled when a packet has been received.
26 static struct efi_event *wait_for_packet;
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 */
36 struct efi_pxe_mode pxe_mode;
39 static efi_status_t EFIAPI efi_net_start(struct efi_simple_network *this)
41 EFI_ENTRY("%p", this);
43 return EFI_EXIT(EFI_SUCCESS);
46 static efi_status_t EFIAPI efi_net_stop(struct efi_simple_network *this)
48 EFI_ENTRY("%p", this);
50 return EFI_EXIT(EFI_SUCCESS);
54 * Initialize network adapter and allocate transmit and receive buffers.
56 * This function implements the Initialize service of the
57 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
58 * (UEFI) specification for details.
60 * @this: pointer to the protocol instance
61 * @extra_rx: extra receive buffer to be allocated
62 * @extra_tx: extra transmit buffer to be allocated
63 * @return: status code
65 static efi_status_t EFIAPI efi_net_initialize(struct efi_simple_network *this,
66 ulong extra_rx, ulong extra_tx)
69 efi_status_t r = EFI_SUCCESS;
71 EFI_ENTRY("%p, %lx, %lx", this, extra_rx, extra_tx);
74 r = EFI_INVALID_PARAMETER;
78 /* Setup packet buffers */
80 /* Disable hardware and put it into the reset state */
82 /* Set current device according to environment variables */
84 /* Get hardware ready for send and receive operations */
95 static efi_status_t EFIAPI efi_net_reset(struct efi_simple_network *this,
96 int extended_verification)
98 EFI_ENTRY("%p, %x", this, extended_verification);
100 return EFI_EXIT(EFI_SUCCESS);
103 static efi_status_t EFIAPI efi_net_shutdown(struct efi_simple_network *this)
105 EFI_ENTRY("%p", this);
107 return EFI_EXIT(EFI_SUCCESS);
110 static efi_status_t EFIAPI efi_net_receive_filters(
111 struct efi_simple_network *this, u32 enable, u32 disable,
112 int reset_mcast_filter, ulong mcast_filter_count,
113 struct efi_mac_address *mcast_filter)
115 EFI_ENTRY("%p, %x, %x, %x, %lx, %p", this, enable, disable,
116 reset_mcast_filter, mcast_filter_count, mcast_filter);
118 return EFI_EXIT(EFI_UNSUPPORTED);
121 static efi_status_t EFIAPI efi_net_station_address(
122 struct efi_simple_network *this, int reset,
123 struct efi_mac_address *new_mac)
125 EFI_ENTRY("%p, %x, %p", this, reset, new_mac);
127 return EFI_EXIT(EFI_UNSUPPORTED);
130 static efi_status_t EFIAPI efi_net_statistics(struct efi_simple_network *this,
131 int reset, ulong *stat_size,
134 EFI_ENTRY("%p, %x, %p, %p", this, reset, stat_size, stat_table);
136 return EFI_EXIT(EFI_UNSUPPORTED);
139 static efi_status_t EFIAPI efi_net_mcastiptomac(struct efi_simple_network *this,
141 struct efi_ip_address *ip,
142 struct efi_mac_address *mac)
144 EFI_ENTRY("%p, %x, %p, %p", this, ipv6, ip, mac);
146 return EFI_EXIT(EFI_INVALID_PARAMETER);
149 static efi_status_t EFIAPI efi_net_nvdata(struct efi_simple_network *this,
150 int read_write, ulong offset,
151 ulong buffer_size, char *buffer)
153 EFI_ENTRY("%p, %x, %lx, %lx, %p", this, read_write, offset, buffer_size,
156 return EFI_EXIT(EFI_UNSUPPORTED);
159 static efi_status_t EFIAPI efi_net_get_status(struct efi_simple_network *this,
160 u32 *int_status, void **txbuf)
162 EFI_ENTRY("%p, %p, %p", this, int_status, txbuf);
167 /* We send packets synchronously, so nothing is outstanding */
168 *int_status = EFI_SIMPLE_NETWORK_TRANSMIT_INTERRUPT;
170 *int_status |= EFI_SIMPLE_NETWORK_RECEIVE_INTERRUPT;
173 *txbuf = new_tx_packet;
175 new_tx_packet = NULL;
177 return EFI_EXIT(EFI_SUCCESS);
180 static efi_status_t EFIAPI efi_net_transmit(struct efi_simple_network *this,
181 size_t header_size, size_t buffer_size, void *buffer,
182 struct efi_mac_address *src_addr,
183 struct efi_mac_address *dest_addr, u16 *protocol)
185 EFI_ENTRY("%p, %lu, %lu, %p, %p, %p, %p", this,
186 (unsigned long)header_size, (unsigned long)buffer_size,
187 buffer, src_addr, dest_addr, protocol);
192 /* We would need to create the header if header_size != 0 */
193 return EFI_EXIT(EFI_INVALID_PARAMETER);
196 #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
197 /* Ethernet packets always fit, just bounce */
198 memcpy(efi_bounce_buffer, buffer, buffer_size);
199 net_send_packet(efi_bounce_buffer, buffer_size);
201 net_send_packet(buffer, buffer_size);
204 new_tx_packet = buffer;
206 return EFI_EXIT(EFI_SUCCESS);
209 static void efi_net_push(void *pkt, int len)
211 new_rx_packet = true;
212 wait_for_packet->is_signaled = true;
216 * Receive a packet from a network interface.
218 * This function implements the Receive service of the Simple Network Protocol.
219 * See the UEFI spec for details.
221 * @this the instance of the Simple Network Protocol
222 * @header_size size of the media header
223 * @buffer_size size of the buffer to receive the packet
224 * @buffer buffer to receive the packet
225 * @src_addr source MAC address
226 * @dest_addr destination MAC address
228 * @return status code
230 static efi_status_t EFIAPI efi_net_receive(struct efi_simple_network *this,
231 size_t *header_size, size_t *buffer_size, void *buffer,
232 struct efi_mac_address *src_addr,
233 struct efi_mac_address *dest_addr, u16 *protocol)
235 struct ethernet_hdr *eth_hdr;
236 size_t hdr_size = sizeof(struct ethernet_hdr);
239 EFI_ENTRY("%p, %p, %p, %p, %p, %p, %p", this, header_size,
240 buffer_size, buffer, src_addr, dest_addr, protocol);
245 return EFI_EXIT(EFI_NOT_READY);
246 /* Check that we at least received an Ethernet header */
247 if (net_rx_packet_len < sizeof(struct ethernet_hdr)) {
248 new_rx_packet = false;
249 return EFI_EXIT(EFI_NOT_READY);
251 /* Fill export parameters */
252 eth_hdr = (struct ethernet_hdr *)net_rx_packet;
253 protlen = ntohs(eth_hdr->et_protlen);
254 if (protlen == 0x8100) {
256 protlen = ntohs(*(u16 *)&net_rx_packet[hdr_size - 2]);
259 *header_size = hdr_size;
261 memcpy(dest_addr, eth_hdr->et_dest, ARP_HLEN);
263 memcpy(src_addr, eth_hdr->et_src, ARP_HLEN);
266 if (*buffer_size < net_rx_packet_len) {
267 /* Packet doesn't fit, try again with bigger buf */
268 *buffer_size = net_rx_packet_len;
269 return EFI_EXIT(EFI_BUFFER_TOO_SMALL);
272 memcpy(buffer, net_rx_packet, net_rx_packet_len);
273 *buffer_size = net_rx_packet_len;
274 new_rx_packet = false;
276 return EFI_EXIT(EFI_SUCCESS);
279 void efi_net_set_dhcp_ack(void *pkt, int len)
281 int maxsize = sizeof(*dhcp_ack);
284 dhcp_ack = malloc(maxsize);
286 memcpy(dhcp_ack, pkt, min(len, maxsize));
290 * Check if a new network packet has been received.
292 * This notification function is called in every timer cycle.
294 * @event the event for which this notification function is registered
295 * @context event context - not used in this function
297 static void EFIAPI efi_network_timer_notify(struct efi_event *event,
300 EFI_ENTRY("%p, %p", event, context);
302 if (!new_rx_packet) {
303 push_packet = efi_net_push;
307 EFI_EXIT(EFI_SUCCESS);
310 /* This gets called from do_bootefi_exec(). */
311 efi_status_t efi_net_register(void)
313 struct efi_net_obj *netobj;
316 if (!eth_get_dev()) {
317 /* No eth device active, don't expose any */
321 /* We only expose the "active" eth device, so one is enough */
322 netobj = calloc(1, sizeof(*netobj));
324 printf("ERROR: Out of memory\n");
325 return EFI_OUT_OF_RESOURCES;
328 /* Hook net up to the device list */
329 efi_add_handle(&netobj->parent);
331 /* Fill in object data */
332 r = efi_add_protocol(netobj->parent.handle, &efi_net_guid,
334 if (r != EFI_SUCCESS)
335 goto failure_to_add_protocol;
336 r = efi_add_protocol(netobj->parent.handle, &efi_guid_device_path,
338 if (r != EFI_SUCCESS)
339 goto failure_to_add_protocol;
340 r = efi_add_protocol(netobj->parent.handle, &efi_pxe_guid,
342 if (r != EFI_SUCCESS)
343 goto failure_to_add_protocol;
344 netobj->net.revision = EFI_SIMPLE_NETWORK_PROTOCOL_REVISION;
345 netobj->net.start = efi_net_start;
346 netobj->net.stop = efi_net_stop;
347 netobj->net.initialize = efi_net_initialize;
348 netobj->net.reset = efi_net_reset;
349 netobj->net.shutdown = efi_net_shutdown;
350 netobj->net.receive_filters = efi_net_receive_filters;
351 netobj->net.station_address = efi_net_station_address;
352 netobj->net.statistics = efi_net_statistics;
353 netobj->net.mcastiptomac = efi_net_mcastiptomac;
354 netobj->net.nvdata = efi_net_nvdata;
355 netobj->net.get_status = efi_net_get_status;
356 netobj->net.transmit = efi_net_transmit;
357 netobj->net.receive = efi_net_receive;
358 netobj->net.mode = &netobj->net_mode;
359 netobj->net_mode.state = EFI_NETWORK_STARTED;
360 memcpy(netobj->net_mode.current_address.mac_addr, eth_get_ethaddr(), 6);
361 netobj->net_mode.hwaddr_size = ARP_HLEN;
362 netobj->net_mode.max_packet_size = PKTSIZE;
363 netobj->net_mode.if_type = ARP_ETHER;
365 netobj->pxe.mode = &netobj->pxe_mode;
367 netobj->pxe_mode.dhcp_ack = *dhcp_ack;
370 * Create WaitForPacket event.
372 r = efi_create_event(EVT_NOTIFY_WAIT, TPL_CALLBACK,
373 efi_network_timer_notify, NULL, NULL,
375 if (r != EFI_SUCCESS) {
376 printf("ERROR: Failed to register network event\n");
379 netobj->net.wait_for_packet = wait_for_packet;
381 * Create a timer event.
383 * The notification function is used to check if a new network packet
386 * iPXE is running at TPL_CALLBACK most of the time. Use a higher TPL.
388 r = efi_create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_NOTIFY,
389 efi_network_timer_notify, NULL, NULL,
390 &network_timer_event);
391 if (r != EFI_SUCCESS) {
392 printf("ERROR: Failed to register network event\n");
395 /* Network is time critical, create event in every timer cyle */
396 r = efi_set_timer(network_timer_event, EFI_TIMER_PERIODIC, 0);
397 if (r != EFI_SUCCESS) {
398 printf("ERROR: Failed to set network timer\n");
403 failure_to_add_protocol:
404 printf("ERROR: Failure to add protocol\n");