2 * EFI application network access support
4 * Copyright (c) 2016 Alexander Graf
6 * SPDX-License-Identifier: GPL-2.0+
10 #include <efi_loader.h>
15 DECLARE_GLOBAL_DATA_PTR;
17 static const efi_guid_t efi_net_guid = EFI_SIMPLE_NETWORK_GUID;
18 static const efi_guid_t efi_pxe_guid = EFI_PXE_GUID;
19 static struct efi_pxe_packet *dhcp_ack;
20 static bool new_rx_packet;
21 static void *new_tx_packet;
24 /* Generic EFI object parent class data */
25 struct efi_object parent;
26 /* EFI Interface callback struct for network */
27 struct efi_simple_network net;
28 struct efi_simple_network_mode net_mode;
29 /* PXE struct to transmit dhcp data */
31 struct efi_pxe_mode pxe_mode;
34 static efi_status_t EFIAPI efi_net_start(struct efi_simple_network *this)
36 EFI_ENTRY("%p", this);
38 return EFI_EXIT(EFI_SUCCESS);
41 static efi_status_t EFIAPI efi_net_stop(struct efi_simple_network *this)
43 EFI_ENTRY("%p", this);
45 return EFI_EXIT(EFI_SUCCESS);
48 static efi_status_t EFIAPI efi_net_initialize(struct efi_simple_network *this,
49 ulong extra_rx, ulong extra_tx)
51 EFI_ENTRY("%p, %lx, %lx", this, extra_rx, extra_tx);
55 return EFI_EXIT(EFI_SUCCESS);
58 static efi_status_t EFIAPI efi_net_reset(struct efi_simple_network *this,
59 int extended_verification)
61 EFI_ENTRY("%p, %x", this, extended_verification);
63 return EFI_EXIT(EFI_SUCCESS);
66 static efi_status_t EFIAPI efi_net_shutdown(struct efi_simple_network *this)
68 EFI_ENTRY("%p", this);
70 return EFI_EXIT(EFI_SUCCESS);
73 static efi_status_t EFIAPI efi_net_receive_filters(
74 struct efi_simple_network *this, u32 enable, u32 disable,
75 int reset_mcast_filter, ulong mcast_filter_count,
76 struct efi_mac_address *mcast_filter)
78 EFI_ENTRY("%p, %x, %x, %x, %lx, %p", this, enable, disable,
79 reset_mcast_filter, mcast_filter_count, mcast_filter);
81 return EFI_EXIT(EFI_UNSUPPORTED);
84 static efi_status_t EFIAPI efi_net_station_address(
85 struct efi_simple_network *this, int reset,
86 struct efi_mac_address *new_mac)
88 EFI_ENTRY("%p, %x, %p", this, reset, new_mac);
90 return EFI_EXIT(EFI_UNSUPPORTED);
93 static efi_status_t EFIAPI efi_net_statistics(struct efi_simple_network *this,
94 int reset, ulong *stat_size,
97 EFI_ENTRY("%p, %x, %p, %p", this, reset, stat_size, stat_table);
99 return EFI_EXIT(EFI_UNSUPPORTED);
102 static efi_status_t EFIAPI efi_net_mcastiptomac(struct efi_simple_network *this,
104 struct efi_ip_address *ip,
105 struct efi_mac_address *mac)
107 EFI_ENTRY("%p, %x, %p, %p", this, ipv6, ip, mac);
109 return EFI_EXIT(EFI_INVALID_PARAMETER);
112 static efi_status_t EFIAPI efi_net_nvdata(struct efi_simple_network *this,
113 int read_write, ulong offset,
114 ulong buffer_size, char *buffer)
116 EFI_ENTRY("%p, %x, %lx, %lx, %p", this, read_write, offset, buffer_size,
119 return EFI_EXIT(EFI_UNSUPPORTED);
122 static efi_status_t EFIAPI efi_net_get_status(struct efi_simple_network *this,
123 u32 *int_status, void **txbuf)
125 EFI_ENTRY("%p, %p, %p", this, int_status, txbuf);
127 /* We send packets synchronously, so nothing is outstanding */
131 *txbuf = new_tx_packet;
133 new_tx_packet = NULL;
135 return EFI_EXIT(EFI_SUCCESS);
138 static efi_status_t EFIAPI efi_net_transmit(struct efi_simple_network *this,
139 ulong header_size, ulong buffer_size, void *buffer,
140 struct efi_mac_address *src_addr,
141 struct efi_mac_address *dest_addr, u16 *protocol)
143 EFI_ENTRY("%p, %lx, %lx, %p, %p, %p, %p", this, header_size,
144 buffer_size, buffer, src_addr, dest_addr, protocol);
147 /* We would need to create the header if header_size != 0 */
148 return EFI_EXIT(EFI_INVALID_PARAMETER);
151 #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
152 /* Ethernet packets always fit, just bounce */
153 memcpy(efi_bounce_buffer, buffer, buffer_size);
154 net_send_packet(efi_bounce_buffer, buffer_size);
156 net_send_packet(buffer, buffer_size);
159 new_tx_packet = buffer;
161 return EFI_EXIT(EFI_SUCCESS);
164 static void efi_net_push(void *pkt, int len)
166 new_rx_packet = true;
169 static efi_status_t EFIAPI efi_net_receive(struct efi_simple_network *this,
170 ulong *header_size, ulong *buffer_size, void *buffer,
171 struct efi_mac_address *src_addr,
172 struct efi_mac_address *dest_addr, u16 *protocol)
174 EFI_ENTRY("%p, %p, %p, %p, %p, %p, %p", this, header_size,
175 buffer_size, buffer, src_addr, dest_addr, protocol);
177 push_packet = efi_net_push;
182 return EFI_EXIT(EFI_NOT_READY);
184 if (*buffer_size < net_rx_packet_len) {
185 /* Packet doesn't fit, try again with bigger buf */
186 *buffer_size = net_rx_packet_len;
187 return EFI_EXIT(EFI_BUFFER_TOO_SMALL);
190 memcpy(buffer, net_rx_packet, net_rx_packet_len);
191 *buffer_size = net_rx_packet_len;
192 new_rx_packet = false;
194 return EFI_EXIT(EFI_SUCCESS);
197 void efi_net_set_dhcp_ack(void *pkt, int len)
199 int maxsize = sizeof(*dhcp_ack);
202 dhcp_ack = malloc(maxsize);
204 memcpy(dhcp_ack, pkt, min(len, maxsize));
207 /* This gets called from do_bootefi_exec(). */
208 int efi_net_register(void)
210 struct efi_net_obj *netobj;
212 if (!eth_get_dev()) {
213 /* No eth device active, don't expose any */
217 /* We only expose the "active" eth device, so one is enough */
218 netobj = calloc(1, sizeof(*netobj));
220 /* Fill in object data */
221 netobj->parent.protocols[0].guid = &efi_net_guid;
222 netobj->parent.protocols[0].protocol_interface = &netobj->net;
223 netobj->parent.protocols[1].guid = &efi_guid_device_path;
224 netobj->parent.protocols[1].protocol_interface =
226 netobj->parent.protocols[2].guid = &efi_pxe_guid;
227 netobj->parent.protocols[2].protocol_interface = &netobj->pxe;
228 netobj->parent.handle = &netobj->net;
229 netobj->net.revision = EFI_SIMPLE_NETWORK_PROTOCOL_REVISION;
230 netobj->net.start = efi_net_start;
231 netobj->net.stop = efi_net_stop;
232 netobj->net.initialize = efi_net_initialize;
233 netobj->net.reset = efi_net_reset;
234 netobj->net.shutdown = efi_net_shutdown;
235 netobj->net.receive_filters = efi_net_receive_filters;
236 netobj->net.station_address = efi_net_station_address;
237 netobj->net.statistics = efi_net_statistics;
238 netobj->net.mcastiptomac = efi_net_mcastiptomac;
239 netobj->net.nvdata = efi_net_nvdata;
240 netobj->net.get_status = efi_net_get_status;
241 netobj->net.transmit = efi_net_transmit;
242 netobj->net.receive = efi_net_receive;
243 netobj->net.mode = &netobj->net_mode;
244 netobj->net_mode.state = EFI_NETWORK_STARTED;
245 memcpy(netobj->net_mode.current_address.mac_addr, eth_get_ethaddr(), 6);
246 netobj->net_mode.hwaddr_size = ARP_HLEN;
247 netobj->net_mode.max_packet_size = PKTSIZE;
249 netobj->pxe.mode = &netobj->pxe_mode;
251 netobj->pxe_mode.dhcp_ack = *dhcp_ack;
253 /* Hook net up to the device list */
254 list_add_tail(&netobj->parent.link, &efi_obj_list);