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 /* Device path to the network adapter */
30 struct efi_device_path_file_path dp[2];
31 /* PXE struct to transmit dhcp data */
33 struct efi_pxe_mode pxe_mode;
36 static efi_status_t EFIAPI efi_net_start(struct efi_simple_network *this)
38 EFI_ENTRY("%p", this);
40 return EFI_EXIT(EFI_SUCCESS);
43 static efi_status_t EFIAPI efi_net_stop(struct efi_simple_network *this)
45 EFI_ENTRY("%p", this);
47 return EFI_EXIT(EFI_SUCCESS);
50 static efi_status_t EFIAPI efi_net_initialize(struct efi_simple_network *this,
51 ulong extra_rx, ulong extra_tx)
53 EFI_ENTRY("%p, %lx, %lx", this, extra_rx, extra_tx);
57 return EFI_EXIT(EFI_SUCCESS);
60 static efi_status_t EFIAPI efi_net_reset(struct efi_simple_network *this,
61 int extended_verification)
63 EFI_ENTRY("%p, %x", this, extended_verification);
65 return EFI_EXIT(EFI_SUCCESS);
68 static efi_status_t EFIAPI efi_net_shutdown(struct efi_simple_network *this)
70 EFI_ENTRY("%p", this);
72 return EFI_EXIT(EFI_SUCCESS);
75 static efi_status_t EFIAPI efi_net_receive_filters(
76 struct efi_simple_network *this, u32 enable, u32 disable,
77 int reset_mcast_filter, ulong mcast_filter_count,
78 struct efi_mac_address *mcast_filter)
80 EFI_ENTRY("%p, %x, %x, %x, %lx, %p", this, enable, disable,
81 reset_mcast_filter, mcast_filter_count, mcast_filter);
85 return EFI_EXIT(EFI_SUCCESS);
88 static efi_status_t EFIAPI efi_net_station_address(
89 struct efi_simple_network *this, int reset,
90 struct efi_mac_address *new_mac)
92 EFI_ENTRY("%p, %x, %p", this, reset, new_mac);
94 return EFI_EXIT(EFI_INVALID_PARAMETER);
97 static efi_status_t EFIAPI efi_net_statistics(struct efi_simple_network *this,
98 int reset, ulong *stat_size,
101 EFI_ENTRY("%p, %x, %p, %p", this, reset, stat_size, stat_table);
103 return EFI_EXIT(EFI_INVALID_PARAMETER);
106 static efi_status_t EFIAPI efi_net_mcastiptomac(struct efi_simple_network *this,
108 struct efi_ip_address *ip,
109 struct efi_mac_address *mac)
111 EFI_ENTRY("%p, %x, %p, %p", this, ipv6, ip, mac);
113 return EFI_EXIT(EFI_INVALID_PARAMETER);
116 static efi_status_t EFIAPI efi_net_nvdata(struct efi_simple_network *this,
117 int read_write, ulong offset,
118 ulong buffer_size, char *buffer)
120 EFI_ENTRY("%p, %x, %lx, %lx, %p", this, read_write, offset, buffer_size,
123 return EFI_EXIT(EFI_INVALID_PARAMETER);
126 static efi_status_t EFIAPI efi_net_get_status(struct efi_simple_network *this,
127 u32 *int_status, void **txbuf)
129 EFI_ENTRY("%p, %p, %p", this, int_status, txbuf);
131 /* We send packets synchronously, so nothing is outstanding */
135 *txbuf = new_tx_packet;
137 new_tx_packet = NULL;
139 return EFI_EXIT(EFI_SUCCESS);
142 static efi_status_t EFIAPI efi_net_transmit(struct efi_simple_network *this,
143 ulong header_size, ulong buffer_size, void *buffer,
144 struct efi_mac_address *src_addr,
145 struct efi_mac_address *dest_addr, u16 *protocol)
147 EFI_ENTRY("%p, %lx, %lx, %p, %p, %p, %p", this, header_size,
148 buffer_size, buffer, src_addr, dest_addr, protocol);
151 /* We would need to create the header if header_size != 0 */
152 return EFI_EXIT(EFI_INVALID_PARAMETER);
155 net_send_packet(buffer, buffer_size);
156 new_tx_packet = buffer;
158 return EFI_EXIT(EFI_SUCCESS);
161 static void efi_net_push(void *pkt, int len)
163 new_rx_packet = true;
166 static efi_status_t EFIAPI efi_net_receive(struct efi_simple_network *this,
167 ulong *header_size, ulong *buffer_size, void *buffer,
168 struct efi_mac_address *src_addr,
169 struct efi_mac_address *dest_addr, u16 *protocol)
171 EFI_ENTRY("%p, %p, %p, %p, %p, %p, %p", this, header_size,
172 buffer_size, buffer, src_addr, dest_addr, protocol);
174 push_packet = efi_net_push;
179 return EFI_EXIT(EFI_NOT_READY);
181 if (*buffer_size < net_rx_packet_len) {
182 /* Packet doesn't fit, try again with bigger buf */
183 *buffer_size = net_rx_packet_len;
184 return EFI_EXIT(EFI_BUFFER_TOO_SMALL);
187 memcpy(buffer, net_rx_packet, net_rx_packet_len);
188 *buffer_size = net_rx_packet_len;
189 new_rx_packet = false;
191 return EFI_EXIT(EFI_SUCCESS);
194 static efi_status_t efi_net_open_dp(void *handle, efi_guid_t *protocol,
195 void **protocol_interface, void *agent_handle,
196 void *controller_handle, uint32_t attributes)
198 struct efi_simple_network *net = handle;
199 struct efi_net_obj *netobj = container_of(net, struct efi_net_obj, net);
201 *protocol_interface = netobj->dp;
206 static efi_status_t efi_net_open_pxe(void *handle, efi_guid_t *protocol,
207 void **protocol_interface, void *agent_handle,
208 void *controller_handle, uint32_t attributes)
210 struct efi_simple_network *net = handle;
211 struct efi_net_obj *netobj = container_of(net, struct efi_net_obj, net);
213 *protocol_interface = &netobj->pxe;
218 void efi_net_set_dhcp_ack(void *pkt, int len)
220 int maxsize = sizeof(*dhcp_ack);
223 dhcp_ack = malloc(maxsize);
225 memcpy(dhcp_ack, pkt, min(len, maxsize));
228 /* This gets called from do_bootefi_exec(). */
229 int efi_net_register(void **handle)
231 struct efi_net_obj *netobj;
232 struct efi_device_path_file_path dp_net = {
233 .dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE,
234 .dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH,
235 .dp.length = sizeof(dp_net),
236 .str = { 'N', 'e', 't' },
238 struct efi_device_path_file_path dp_end = {
239 .dp.type = DEVICE_PATH_TYPE_END,
240 .dp.sub_type = DEVICE_PATH_SUB_TYPE_END,
241 .dp.length = sizeof(dp_end),
244 if (!eth_get_dev()) {
245 /* No eth device active, don't expose any */
249 /* We only expose the "active" eth device, so one is enough */
250 netobj = calloc(1, sizeof(*netobj));
252 /* Fill in object data */
253 netobj->parent.protocols[0].guid = &efi_net_guid;
254 netobj->parent.protocols[0].open = efi_return_handle;
255 netobj->parent.protocols[1].guid = &efi_guid_device_path;
256 netobj->parent.protocols[1].open = efi_net_open_dp;
257 netobj->parent.protocols[2].guid = &efi_pxe_guid;
258 netobj->parent.protocols[2].open = efi_net_open_pxe;
259 netobj->parent.handle = &netobj->net;
260 netobj->net.start = efi_net_start;
261 netobj->net.stop = efi_net_stop;
262 netobj->net.initialize = efi_net_initialize;
263 netobj->net.reset = efi_net_reset;
264 netobj->net.shutdown = efi_net_shutdown;
265 netobj->net.receive_filters = efi_net_receive_filters;
266 netobj->net.station_address = efi_net_station_address;
267 netobj->net.statistics = efi_net_statistics;
268 netobj->net.mcastiptomac = efi_net_mcastiptomac;
269 netobj->net.nvdata = efi_net_nvdata;
270 netobj->net.get_status = efi_net_get_status;
271 netobj->net.transmit = efi_net_transmit;
272 netobj->net.receive = efi_net_receive;
273 netobj->net.mode = &netobj->net_mode;
274 netobj->net_mode.state = EFI_NETWORK_STARTED;
275 netobj->dp[0] = dp_net;
276 netobj->dp[1] = dp_end;
277 memcpy(netobj->net_mode.current_address.mac_addr, eth_get_ethaddr(), 6);
278 netobj->net_mode.max_packet_size = PKTSIZE;
280 netobj->pxe.mode = &netobj->pxe_mode;
282 netobj->pxe_mode.dhcp_ack = *dhcp_ack;
284 /* Hook net up to the device list */
285 list_add_tail(&netobj->parent.link, &efi_obj_list);
288 *handle = &netobj->net;