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);
83 return EFI_EXIT(EFI_SUCCESS);
86 static efi_status_t EFIAPI efi_net_station_address(
87 struct efi_simple_network *this, int reset,
88 struct efi_mac_address *new_mac)
90 EFI_ENTRY("%p, %x, %p", this, reset, new_mac);
92 return EFI_EXIT(EFI_INVALID_PARAMETER);
95 static efi_status_t EFIAPI efi_net_statistics(struct efi_simple_network *this,
96 int reset, ulong *stat_size,
99 EFI_ENTRY("%p, %x, %p, %p", this, reset, stat_size, stat_table);
101 return EFI_EXIT(EFI_INVALID_PARAMETER);
104 static efi_status_t EFIAPI efi_net_mcastiptomac(struct efi_simple_network *this,
106 struct efi_ip_address *ip,
107 struct efi_mac_address *mac)
109 EFI_ENTRY("%p, %x, %p, %p", this, ipv6, ip, mac);
111 return EFI_EXIT(EFI_INVALID_PARAMETER);
114 static efi_status_t EFIAPI efi_net_nvdata(struct efi_simple_network *this,
115 int read_write, ulong offset,
116 ulong buffer_size, char *buffer)
118 EFI_ENTRY("%p, %x, %lx, %lx, %p", this, read_write, offset, buffer_size,
121 return EFI_EXIT(EFI_INVALID_PARAMETER);
124 static efi_status_t EFIAPI efi_net_get_status(struct efi_simple_network *this,
125 u32 *int_status, void **txbuf)
127 EFI_ENTRY("%p, %p, %p", this, int_status, txbuf);
129 /* We send packets synchronously, so nothing is outstanding */
133 *txbuf = new_tx_packet;
135 new_tx_packet = NULL;
137 return EFI_EXIT(EFI_SUCCESS);
140 static efi_status_t EFIAPI efi_net_transmit(struct efi_simple_network *this,
141 ulong header_size, ulong buffer_size, void *buffer,
142 struct efi_mac_address *src_addr,
143 struct efi_mac_address *dest_addr, u16 *protocol)
145 EFI_ENTRY("%p, %lx, %lx, %p, %p, %p, %p", this, header_size,
146 buffer_size, buffer, src_addr, dest_addr, protocol);
149 /* We would need to create the header if header_size != 0 */
150 return EFI_EXIT(EFI_INVALID_PARAMETER);
153 #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
154 /* Ethernet packets always fit, just bounce */
155 memcpy(efi_bounce_buffer, buffer, buffer_size);
156 net_send_packet(efi_bounce_buffer, buffer_size);
158 net_send_packet(buffer, buffer_size);
161 new_tx_packet = buffer;
163 return EFI_EXIT(EFI_SUCCESS);
166 static void efi_net_push(void *pkt, int len)
168 new_rx_packet = true;
171 static efi_status_t EFIAPI efi_net_receive(struct efi_simple_network *this,
172 ulong *header_size, ulong *buffer_size, void *buffer,
173 struct efi_mac_address *src_addr,
174 struct efi_mac_address *dest_addr, u16 *protocol)
176 EFI_ENTRY("%p, %p, %p, %p, %p, %p, %p", this, header_size,
177 buffer_size, buffer, src_addr, dest_addr, protocol);
179 push_packet = efi_net_push;
184 return EFI_EXIT(EFI_NOT_READY);
186 if (*buffer_size < net_rx_packet_len) {
187 /* Packet doesn't fit, try again with bigger buf */
188 *buffer_size = net_rx_packet_len;
189 return EFI_EXIT(EFI_BUFFER_TOO_SMALL);
192 memcpy(buffer, net_rx_packet, net_rx_packet_len);
193 *buffer_size = net_rx_packet_len;
194 new_rx_packet = false;
196 return EFI_EXIT(EFI_SUCCESS);
199 void efi_net_set_dhcp_ack(void *pkt, int len)
201 int maxsize = sizeof(*dhcp_ack);
204 dhcp_ack = malloc(maxsize);
206 memcpy(dhcp_ack, pkt, min(len, maxsize));
209 /* This gets called from do_bootefi_exec(). */
210 int efi_net_register(void)
212 struct efi_net_obj *netobj;
214 if (!eth_get_dev()) {
215 /* No eth device active, don't expose any */
219 /* We only expose the "active" eth device, so one is enough */
220 netobj = calloc(1, sizeof(*netobj));
222 /* Fill in object data */
223 netobj->parent.protocols[0].guid = &efi_net_guid;
224 netobj->parent.protocols[0].protocol_interface = &netobj->net;
225 netobj->parent.protocols[1].guid = &efi_guid_device_path;
226 netobj->parent.protocols[1].protocol_interface =
228 netobj->parent.protocols[2].guid = &efi_pxe_guid;
229 netobj->parent.protocols[2].protocol_interface = &netobj->pxe;
230 netobj->parent.handle = &netobj->net;
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.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);