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
;
23 * The notification function of this event is called in every timer cycle
24 * to check if a new network packet has been received.
26 static struct efi_event
*network_timer_event
;
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
);
53 static efi_status_t EFIAPI
efi_net_initialize(struct efi_simple_network
*this,
54 ulong extra_rx
, ulong extra_tx
)
56 EFI_ENTRY("%p, %lx, %lx", this, extra_rx
, extra_tx
);
60 return EFI_EXIT(EFI_SUCCESS
);
63 static efi_status_t EFIAPI
efi_net_reset(struct efi_simple_network
*this,
64 int extended_verification
)
66 EFI_ENTRY("%p, %x", this, extended_verification
);
68 return EFI_EXIT(EFI_SUCCESS
);
71 static efi_status_t EFIAPI
efi_net_shutdown(struct efi_simple_network
*this)
73 EFI_ENTRY("%p", this);
75 return EFI_EXIT(EFI_SUCCESS
);
78 static efi_status_t EFIAPI
efi_net_receive_filters(
79 struct efi_simple_network
*this, u32 enable
, u32 disable
,
80 int reset_mcast_filter
, ulong mcast_filter_count
,
81 struct efi_mac_address
*mcast_filter
)
83 EFI_ENTRY("%p, %x, %x, %x, %lx, %p", this, enable
, disable
,
84 reset_mcast_filter
, mcast_filter_count
, mcast_filter
);
86 return EFI_EXIT(EFI_UNSUPPORTED
);
89 static efi_status_t EFIAPI
efi_net_station_address(
90 struct efi_simple_network
*this, int reset
,
91 struct efi_mac_address
*new_mac
)
93 EFI_ENTRY("%p, %x, %p", this, reset
, new_mac
);
95 return EFI_EXIT(EFI_UNSUPPORTED
);
98 static efi_status_t EFIAPI
efi_net_statistics(struct efi_simple_network
*this,
99 int reset
, ulong
*stat_size
,
102 EFI_ENTRY("%p, %x, %p, %p", this, reset
, stat_size
, stat_table
);
104 return EFI_EXIT(EFI_UNSUPPORTED
);
107 static efi_status_t EFIAPI
efi_net_mcastiptomac(struct efi_simple_network
*this,
109 struct efi_ip_address
*ip
,
110 struct efi_mac_address
*mac
)
112 EFI_ENTRY("%p, %x, %p, %p", this, ipv6
, ip
, mac
);
114 return EFI_EXIT(EFI_INVALID_PARAMETER
);
117 static efi_status_t EFIAPI
efi_net_nvdata(struct efi_simple_network
*this,
118 int read_write
, ulong offset
,
119 ulong buffer_size
, char *buffer
)
121 EFI_ENTRY("%p, %x, %lx, %lx, %p", this, read_write
, offset
, buffer_size
,
124 return EFI_EXIT(EFI_UNSUPPORTED
);
127 static efi_status_t EFIAPI
efi_net_get_status(struct efi_simple_network
*this,
128 u32
*int_status
, void **txbuf
)
130 EFI_ENTRY("%p, %p, %p", this, int_status
, txbuf
);
132 /* We send packets synchronously, so nothing is outstanding */
136 *txbuf
= new_tx_packet
;
138 new_tx_packet
= NULL
;
140 return EFI_EXIT(EFI_SUCCESS
);
143 static efi_status_t EFIAPI
efi_net_transmit(struct efi_simple_network
*this,
144 ulong header_size
, ulong buffer_size
, void *buffer
,
145 struct efi_mac_address
*src_addr
,
146 struct efi_mac_address
*dest_addr
, u16
*protocol
)
148 EFI_ENTRY("%p, %lx, %lx, %p, %p, %p, %p", this, header_size
,
149 buffer_size
, buffer
, src_addr
, dest_addr
, protocol
);
154 /* We would need to create the header if header_size != 0 */
155 return EFI_EXIT(EFI_INVALID_PARAMETER
);
158 #ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
159 /* Ethernet packets always fit, just bounce */
160 memcpy(efi_bounce_buffer
, buffer
, buffer_size
);
161 net_send_packet(efi_bounce_buffer
, buffer_size
);
163 net_send_packet(buffer
, buffer_size
);
166 new_tx_packet
= buffer
;
168 return EFI_EXIT(EFI_SUCCESS
);
171 static void efi_net_push(void *pkt
, int len
)
173 new_rx_packet
= true;
176 static efi_status_t EFIAPI
efi_net_receive(struct efi_simple_network
*this,
177 ulong
*header_size
, ulong
*buffer_size
, void *buffer
,
178 struct efi_mac_address
*src_addr
,
179 struct efi_mac_address
*dest_addr
, u16
*protocol
)
181 EFI_ENTRY("%p, %p, %p, %p, %p, %p, %p", this, header_size
,
182 buffer_size
, buffer
, src_addr
, dest_addr
, protocol
);
187 return EFI_EXIT(EFI_NOT_READY
);
189 if (*buffer_size
< net_rx_packet_len
) {
190 /* Packet doesn't fit, try again with bigger buf */
191 *buffer_size
= net_rx_packet_len
;
192 return EFI_EXIT(EFI_BUFFER_TOO_SMALL
);
195 memcpy(buffer
, net_rx_packet
, net_rx_packet_len
);
196 *buffer_size
= net_rx_packet_len
;
197 new_rx_packet
= false;
199 return EFI_EXIT(EFI_SUCCESS
);
202 void efi_net_set_dhcp_ack(void *pkt
, int len
)
204 int maxsize
= sizeof(*dhcp_ack
);
207 dhcp_ack
= malloc(maxsize
);
209 memcpy(dhcp_ack
, pkt
, min(len
, maxsize
));
213 * Check if a new network packet has been received.
215 * This notification function is called in every timer cycle.
217 * @event the event for which this notification function is registered
218 * @context event context - not used in this function
220 static void EFIAPI
efi_network_timer_notify(struct efi_event
*event
,
223 EFI_ENTRY("%p, %p", event
, context
);
225 if (!new_rx_packet
) {
226 push_packet
= efi_net_push
;
230 EFI_EXIT(EFI_SUCCESS
);
233 /* This gets called from do_bootefi_exec(). */
234 int efi_net_register(void)
236 struct efi_net_obj
*netobj
;
239 if (!eth_get_dev()) {
240 /* No eth device active, don't expose any */
244 /* We only expose the "active" eth device, so one is enough */
245 netobj
= calloc(1, sizeof(*netobj
));
247 /* Fill in object data */
248 netobj
->parent
.protocols
[0].guid
= &efi_net_guid
;
249 netobj
->parent
.protocols
[0].protocol_interface
= &netobj
->net
;
250 netobj
->parent
.protocols
[1].guid
= &efi_guid_device_path
;
251 netobj
->parent
.protocols
[1].protocol_interface
=
253 netobj
->parent
.protocols
[2].guid
= &efi_pxe_guid
;
254 netobj
->parent
.protocols
[2].protocol_interface
= &netobj
->pxe
;
255 netobj
->parent
.handle
= &netobj
->net
;
256 netobj
->net
.revision
= EFI_SIMPLE_NETWORK_PROTOCOL_REVISION
;
257 netobj
->net
.start
= efi_net_start
;
258 netobj
->net
.stop
= efi_net_stop
;
259 netobj
->net
.initialize
= efi_net_initialize
;
260 netobj
->net
.reset
= efi_net_reset
;
261 netobj
->net
.shutdown
= efi_net_shutdown
;
262 netobj
->net
.receive_filters
= efi_net_receive_filters
;
263 netobj
->net
.station_address
= efi_net_station_address
;
264 netobj
->net
.statistics
= efi_net_statistics
;
265 netobj
->net
.mcastiptomac
= efi_net_mcastiptomac
;
266 netobj
->net
.nvdata
= efi_net_nvdata
;
267 netobj
->net
.get_status
= efi_net_get_status
;
268 netobj
->net
.transmit
= efi_net_transmit
;
269 netobj
->net
.receive
= efi_net_receive
;
270 netobj
->net
.mode
= &netobj
->net_mode
;
271 netobj
->net_mode
.state
= EFI_NETWORK_STARTED
;
272 memcpy(netobj
->net_mode
.current_address
.mac_addr
, eth_get_ethaddr(), 6);
273 netobj
->net_mode
.hwaddr_size
= ARP_HLEN
;
274 netobj
->net_mode
.max_packet_size
= PKTSIZE
;
276 netobj
->pxe
.mode
= &netobj
->pxe_mode
;
278 netobj
->pxe_mode
.dhcp_ack
= *dhcp_ack
;
280 /* Hook net up to the device list */
281 list_add_tail(&netobj
->parent
.link
, &efi_obj_list
);
284 * Create a timer event.
286 * The notification function is used to check if a new network packet
289 r
= efi_create_event(EVT_TIMER
| EVT_NOTIFY_SIGNAL
, TPL_CALLBACK
,
290 efi_network_timer_notify
, NULL
,
291 &network_timer_event
);
292 if (r
!= EFI_SUCCESS
) {
293 printf("ERROR: Failed to register network event\n");
296 /* Network is time critical, create event in every timer cyle */
297 r
= efi_set_timer(network_timer_event
, EFI_TIMER_PERIODIC
, 0);
298 if (r
!= EFI_SUCCESS
) {
299 printf("ERROR: Failed to set network timer\n");