1 // SPDX-License-Identifier: GPL-2.0+
3 * EFI application network access support
5 * Copyright (c) 2016 Alexander Graf
9 #include <efi_loader.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 static void *transmit_buffer;
20 * The notification function of this event is called in every timer cycle
21 * to check if a new network packet has been received.
23 static struct efi_event *network_timer_event;
25 * This event is signaled when a packet has been received.
27 static struct efi_event *wait_for_packet;
30 * struct efi_net_obj - EFI object representing a network interface
32 * @header: EFI object header
33 * @net: simple network protocol interface
34 * @net_mode: status of the network interface
35 * @pxe: PXE base code protocol interface
36 * @pxe_mode: status of the PXE base code protocol
39 struct efi_object header;
40 struct efi_simple_network net;
41 struct efi_simple_network_mode net_mode;
43 struct efi_pxe_mode pxe_mode;
46 static efi_status_t EFIAPI efi_net_start(struct efi_simple_network *this)
48 EFI_ENTRY("%p", this);
50 return EFI_EXIT(EFI_SUCCESS);
53 static efi_status_t EFIAPI efi_net_stop(struct efi_simple_network *this)
55 EFI_ENTRY("%p", this);
57 return EFI_EXIT(EFI_SUCCESS);
61 * Initialize network adapter and allocate transmit and receive buffers.
63 * This function implements the Initialize service of the
64 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
65 * (UEFI) specification for details.
67 * @this: pointer to the protocol instance
68 * @extra_rx: extra receive buffer to be allocated
69 * @extra_tx: extra transmit buffer to be allocated
70 * @return: status code
72 static efi_status_t EFIAPI efi_net_initialize(struct efi_simple_network *this,
73 ulong extra_rx, ulong extra_tx)
76 efi_status_t r = EFI_SUCCESS;
78 EFI_ENTRY("%p, %lx, %lx", this, extra_rx, extra_tx);
81 r = EFI_INVALID_PARAMETER;
85 /* Setup packet buffers */
87 /* Disable hardware and put it into the reset state */
89 /* Set current device according to environment variables */
91 /* Get hardware ready for send and receive operations */
102 static efi_status_t EFIAPI efi_net_reset(struct efi_simple_network *this,
103 int extended_verification)
105 EFI_ENTRY("%p, %x", this, extended_verification);
107 return EFI_EXIT(EFI_SUCCESS);
110 static efi_status_t EFIAPI efi_net_shutdown(struct efi_simple_network *this)
112 EFI_ENTRY("%p", this);
114 return EFI_EXIT(EFI_SUCCESS);
117 static efi_status_t EFIAPI efi_net_receive_filters(
118 struct efi_simple_network *this, u32 enable, u32 disable,
119 int reset_mcast_filter, ulong mcast_filter_count,
120 struct efi_mac_address *mcast_filter)
122 EFI_ENTRY("%p, %x, %x, %x, %lx, %p", this, enable, disable,
123 reset_mcast_filter, mcast_filter_count, mcast_filter);
125 return EFI_EXIT(EFI_UNSUPPORTED);
128 static efi_status_t EFIAPI efi_net_station_address(
129 struct efi_simple_network *this, int reset,
130 struct efi_mac_address *new_mac)
132 EFI_ENTRY("%p, %x, %p", this, reset, new_mac);
134 return EFI_EXIT(EFI_UNSUPPORTED);
137 static efi_status_t EFIAPI efi_net_statistics(struct efi_simple_network *this,
138 int reset, ulong *stat_size,
141 EFI_ENTRY("%p, %x, %p, %p", this, reset, stat_size, stat_table);
143 return EFI_EXIT(EFI_UNSUPPORTED);
146 static efi_status_t EFIAPI efi_net_mcastiptomac(struct efi_simple_network *this,
148 struct efi_ip_address *ip,
149 struct efi_mac_address *mac)
151 EFI_ENTRY("%p, %x, %p, %p", this, ipv6, ip, mac);
153 return EFI_EXIT(EFI_INVALID_PARAMETER);
156 static efi_status_t EFIAPI efi_net_nvdata(struct efi_simple_network *this,
157 int read_write, ulong offset,
158 ulong buffer_size, char *buffer)
160 EFI_ENTRY("%p, %x, %lx, %lx, %p", this, read_write, offset, buffer_size,
163 return EFI_EXIT(EFI_UNSUPPORTED);
166 static efi_status_t EFIAPI efi_net_get_status(struct efi_simple_network *this,
167 u32 *int_status, void **txbuf)
169 EFI_ENTRY("%p, %p, %p", this, int_status, txbuf);
174 /* We send packets synchronously, so nothing is outstanding */
175 *int_status = EFI_SIMPLE_NETWORK_TRANSMIT_INTERRUPT;
177 *int_status |= EFI_SIMPLE_NETWORK_RECEIVE_INTERRUPT;
180 *txbuf = new_tx_packet;
182 new_tx_packet = NULL;
184 return EFI_EXIT(EFI_SUCCESS);
187 static efi_status_t EFIAPI efi_net_transmit(struct efi_simple_network *this,
188 size_t header_size, size_t buffer_size, void *buffer,
189 struct efi_mac_address *src_addr,
190 struct efi_mac_address *dest_addr, u16 *protocol)
192 EFI_ENTRY("%p, %lu, %lu, %p, %p, %p, %p", this,
193 (unsigned long)header_size, (unsigned long)buffer_size,
194 buffer, src_addr, dest_addr, protocol);
199 /* We would need to create the header if header_size != 0 */
200 return EFI_EXIT(EFI_INVALID_PARAMETER);
203 /* Ensure that the packet fits into the buffer */
204 if (buffer_size > PKTSIZE_ALIGN)
205 return EFI_EXIT(EFI_INVALID_PARAMETER);
206 memcpy(transmit_buffer, buffer, buffer_size);
207 net_send_packet(transmit_buffer, buffer_size);
209 new_tx_packet = buffer;
211 return EFI_EXIT(EFI_SUCCESS);
214 static void efi_net_push(void *pkt, int len)
216 new_rx_packet = true;
217 wait_for_packet->is_signaled = true;
221 * Receive a packet from a network interface.
223 * This function implements the Receive service of the Simple Network Protocol.
224 * See the UEFI spec for details.
226 * @this the instance of the Simple Network Protocol
227 * @header_size size of the media header
228 * @buffer_size size of the buffer to receive the packet
229 * @buffer buffer to receive the packet
230 * @src_addr source MAC address
231 * @dest_addr destination MAC address
233 * @return status code
235 static efi_status_t EFIAPI efi_net_receive(struct efi_simple_network *this,
236 size_t *header_size, size_t *buffer_size, void *buffer,
237 struct efi_mac_address *src_addr,
238 struct efi_mac_address *dest_addr, u16 *protocol)
240 struct ethernet_hdr *eth_hdr;
241 size_t hdr_size = sizeof(struct ethernet_hdr);
244 EFI_ENTRY("%p, %p, %p, %p, %p, %p, %p", this, header_size,
245 buffer_size, buffer, src_addr, dest_addr, protocol);
250 return EFI_EXIT(EFI_NOT_READY);
251 /* Check that we at least received an Ethernet header */
252 if (net_rx_packet_len < sizeof(struct ethernet_hdr)) {
253 new_rx_packet = false;
254 return EFI_EXIT(EFI_NOT_READY);
256 /* Fill export parameters */
257 eth_hdr = (struct ethernet_hdr *)net_rx_packet;
258 protlen = ntohs(eth_hdr->et_protlen);
259 if (protlen == 0x8100) {
261 protlen = ntohs(*(u16 *)&net_rx_packet[hdr_size - 2]);
264 *header_size = hdr_size;
266 memcpy(dest_addr, eth_hdr->et_dest, ARP_HLEN);
268 memcpy(src_addr, eth_hdr->et_src, ARP_HLEN);
271 if (*buffer_size < net_rx_packet_len) {
272 /* Packet doesn't fit, try again with bigger buffer */
273 *buffer_size = net_rx_packet_len;
274 return EFI_EXIT(EFI_BUFFER_TOO_SMALL);
277 memcpy(buffer, net_rx_packet, net_rx_packet_len);
278 *buffer_size = net_rx_packet_len;
279 new_rx_packet = false;
281 return EFI_EXIT(EFI_SUCCESS);
284 void efi_net_set_dhcp_ack(void *pkt, int len)
286 int maxsize = sizeof(*dhcp_ack);
289 dhcp_ack = malloc(maxsize);
291 memcpy(dhcp_ack, pkt, min(len, maxsize));
295 * Check if a new network packet has been received.
297 * This notification function is called in every timer cycle.
299 * @event the event for which this notification function is registered
300 * @context event context - not used in this function
302 static void EFIAPI efi_network_timer_notify(struct efi_event *event,
305 EFI_ENTRY("%p, %p", event, context);
307 if (!new_rx_packet) {
308 push_packet = efi_net_push;
312 EFI_EXIT(EFI_SUCCESS);
315 /* This gets called from do_bootefi_exec(). */
316 efi_status_t efi_net_register(void)
318 struct efi_net_obj *netobj;
321 if (!eth_get_dev()) {
322 /* No network device active, don't expose any */
326 /* We only expose the "active" network device, so one is enough */
327 netobj = calloc(1, sizeof(*netobj));
329 goto out_of_resources;
331 /* Allocate an aligned transmit buffer */
332 transmit_buffer = calloc(1, PKTSIZE_ALIGN + PKTALIGN);
333 if (!transmit_buffer)
334 goto out_of_resources;
335 transmit_buffer = (void *)ALIGN((uintptr_t)transmit_buffer, PKTALIGN);
337 /* Hook net up to the device list */
338 efi_add_handle(&netobj->header);
340 /* Fill in object data */
341 r = efi_add_protocol(&netobj->header, &efi_net_guid,
343 if (r != EFI_SUCCESS)
344 goto failure_to_add_protocol;
345 r = efi_add_protocol(&netobj->header, &efi_guid_device_path,
347 if (r != EFI_SUCCESS)
348 goto failure_to_add_protocol;
349 r = efi_add_protocol(&netobj->header, &efi_pxe_guid,
351 if (r != EFI_SUCCESS)
352 goto failure_to_add_protocol;
353 netobj->net.revision = EFI_SIMPLE_NETWORK_PROTOCOL_REVISION;
354 netobj->net.start = efi_net_start;
355 netobj->net.stop = efi_net_stop;
356 netobj->net.initialize = efi_net_initialize;
357 netobj->net.reset = efi_net_reset;
358 netobj->net.shutdown = efi_net_shutdown;
359 netobj->net.receive_filters = efi_net_receive_filters;
360 netobj->net.station_address = efi_net_station_address;
361 netobj->net.statistics = efi_net_statistics;
362 netobj->net.mcastiptomac = efi_net_mcastiptomac;
363 netobj->net.nvdata = efi_net_nvdata;
364 netobj->net.get_status = efi_net_get_status;
365 netobj->net.transmit = efi_net_transmit;
366 netobj->net.receive = efi_net_receive;
367 netobj->net.mode = &netobj->net_mode;
368 netobj->net_mode.state = EFI_NETWORK_STARTED;
369 memcpy(netobj->net_mode.current_address.mac_addr, eth_get_ethaddr(), 6);
370 netobj->net_mode.hwaddr_size = ARP_HLEN;
371 netobj->net_mode.max_packet_size = PKTSIZE;
372 netobj->net_mode.if_type = ARP_ETHER;
374 netobj->pxe.mode = &netobj->pxe_mode;
376 netobj->pxe_mode.dhcp_ack = *dhcp_ack;
379 * Create WaitForPacket event.
381 r = efi_create_event(EVT_NOTIFY_WAIT, TPL_CALLBACK,
382 efi_network_timer_notify, NULL, NULL,
384 if (r != EFI_SUCCESS) {
385 printf("ERROR: Failed to register network event\n");
388 netobj->net.wait_for_packet = wait_for_packet;
390 * Create a timer event.
392 * The notification function is used to check if a new network packet
395 * iPXE is running at TPL_CALLBACK most of the time. Use a higher TPL.
397 r = efi_create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_NOTIFY,
398 efi_network_timer_notify, NULL, NULL,
399 &network_timer_event);
400 if (r != EFI_SUCCESS) {
401 printf("ERROR: Failed to register network event\n");
404 /* Network is time critical, create event in every timer cycle */
405 r = efi_set_timer(network_timer_event, EFI_TIMER_PERIODIC, 0);
406 if (r != EFI_SUCCESS) {
407 printf("ERROR: Failed to set network timer\n");
412 failure_to_add_protocol:
413 printf("ERROR: Failure to add protocol\n");
417 /* free(transmit_buffer) not needed yet */
418 printf("ERROR: Out of memory\n");
419 return EFI_OUT_OF_RESOURCES;