blob: c4f35cd50d334c3b4ebb3a6aecd74aa57000fcef [file] [log] [blame]
Tom Rinif739fcd2018-05-07 17:02:21 -04001// SPDX-License-Identifier: GPL-2.0+
Alexander Graf0efe1bc2016-05-06 21:01:01 +02002/*
3 * EFI application network access support
4 *
5 * Copyright (c) 2016 Alexander Graf
Alexander Graf0efe1bc2016-05-06 21:01:01 +02006 */
7
8#include <common.h>
9#include <efi_loader.h>
Alexander Graf0efe1bc2016-05-06 21:01:01 +020010#include <malloc.h>
11
Alexander Graf0efe1bc2016-05-06 21:01:01 +020012static const efi_guid_t efi_net_guid = EFI_SIMPLE_NETWORK_GUID;
13static const efi_guid_t efi_pxe_guid = EFI_PXE_GUID;
14static struct efi_pxe_packet *dhcp_ack;
15static bool new_rx_packet;
16static void *new_tx_packet;
Heinrich Schuchardta0549ef2017-10-05 16:36:00 +020017/*
18 * The notification function of this event is called in every timer cycle
19 * to check if a new network packet has been received.
20 */
21static struct efi_event *network_timer_event;
Heinrich Schuchardte5c21602017-10-05 16:36:01 +020022/*
23 * This event is signaled when a packet has been received.
24 */
25static struct efi_event *wait_for_packet;
Alexander Graf0efe1bc2016-05-06 21:01:01 +020026
Heinrich Schuchardtd39646a2018-09-26 05:27:56 +020027/**
28 * struct efi_net_obj - EFI object representing a network interface
29 *
30 * @header: EFI object header
31 * @net: simple network protocol interface
32 * @net_mode: status of the network interface
33 * @pxe: PXE base code protocol interface
34 * @pxe_mode: status of the PXE base code protocol
35 */
Alexander Graf0efe1bc2016-05-06 21:01:01 +020036struct efi_net_obj {
Heinrich Schuchardtd39646a2018-09-26 05:27:56 +020037 struct efi_object header;
Alexander Graf0efe1bc2016-05-06 21:01:01 +020038 struct efi_simple_network net;
39 struct efi_simple_network_mode net_mode;
Alexander Graf0efe1bc2016-05-06 21:01:01 +020040 struct efi_pxe pxe;
41 struct efi_pxe_mode pxe_mode;
42};
43
44static efi_status_t EFIAPI efi_net_start(struct efi_simple_network *this)
45{
46 EFI_ENTRY("%p", this);
47
48 return EFI_EXIT(EFI_SUCCESS);
49}
50
51static efi_status_t EFIAPI efi_net_stop(struct efi_simple_network *this)
52{
53 EFI_ENTRY("%p", this);
54
55 return EFI_EXIT(EFI_SUCCESS);
56}
57
Heinrich Schuchardt0c5d2a32018-04-03 22:06:52 +020058/*
59 * Initialize network adapter and allocate transmit and receive buffers.
60 *
61 * This function implements the Initialize service of the
62 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
63 * (UEFI) specification for details.
64 *
65 * @this: pointer to the protocol instance
66 * @extra_rx: extra receive buffer to be allocated
67 * @extra_tx: extra transmit buffer to be allocated
68 * @return: status code
69 */
Alexander Graf0efe1bc2016-05-06 21:01:01 +020070static efi_status_t EFIAPI efi_net_initialize(struct efi_simple_network *this,
71 ulong extra_rx, ulong extra_tx)
72{
Heinrich Schuchardt0c5d2a32018-04-03 22:06:52 +020073 int ret;
74 efi_status_t r = EFI_SUCCESS;
75
Alexander Graf0efe1bc2016-05-06 21:01:01 +020076 EFI_ENTRY("%p, %lx, %lx", this, extra_rx, extra_tx);
77
Heinrich Schuchardt0c5d2a32018-04-03 22:06:52 +020078 if (!this) {
79 r = EFI_INVALID_PARAMETER;
80 goto error;
81 }
Alexander Graf0efe1bc2016-05-06 21:01:01 +020082
Heinrich Schuchardt0c5d2a32018-04-03 22:06:52 +020083 /* Setup packet buffers */
84 net_init();
85 /* Disable hardware and put it into the reset state */
86 eth_halt();
87 /* Set current device according to environment variables */
88 eth_set_current();
89 /* Get hardware ready for send and receive operations */
90 ret = eth_init();
91 if (ret < 0) {
92 eth_halt();
93 r = EFI_DEVICE_ERROR;
94 }
95
96error:
97 return EFI_EXIT(r);
Alexander Graf0efe1bc2016-05-06 21:01:01 +020098}
99
100static efi_status_t EFIAPI efi_net_reset(struct efi_simple_network *this,
101 int extended_verification)
102{
103 EFI_ENTRY("%p, %x", this, extended_verification);
104
105 return EFI_EXIT(EFI_SUCCESS);
106}
107
108static efi_status_t EFIAPI efi_net_shutdown(struct efi_simple_network *this)
109{
110 EFI_ENTRY("%p", this);
111
112 return EFI_EXIT(EFI_SUCCESS);
113}
114
115static efi_status_t EFIAPI efi_net_receive_filters(
116 struct efi_simple_network *this, u32 enable, u32 disable,
117 int reset_mcast_filter, ulong mcast_filter_count,
118 struct efi_mac_address *mcast_filter)
119{
120 EFI_ENTRY("%p, %x, %x, %x, %lx, %p", this, enable, disable,
121 reset_mcast_filter, mcast_filter_count, mcast_filter);
122
Heinrich Schuchardt61da6782017-10-05 16:35:59 +0200123 return EFI_EXIT(EFI_UNSUPPORTED);
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200124}
125
126static efi_status_t EFIAPI efi_net_station_address(
127 struct efi_simple_network *this, int reset,
128 struct efi_mac_address *new_mac)
129{
130 EFI_ENTRY("%p, %x, %p", this, reset, new_mac);
131
Heinrich Schuchardt61da6782017-10-05 16:35:59 +0200132 return EFI_EXIT(EFI_UNSUPPORTED);
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200133}
134
135static efi_status_t EFIAPI efi_net_statistics(struct efi_simple_network *this,
136 int reset, ulong *stat_size,
137 void *stat_table)
138{
139 EFI_ENTRY("%p, %x, %p, %p", this, reset, stat_size, stat_table);
140
Heinrich Schuchardt61da6782017-10-05 16:35:59 +0200141 return EFI_EXIT(EFI_UNSUPPORTED);
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200142}
143
144static efi_status_t EFIAPI efi_net_mcastiptomac(struct efi_simple_network *this,
145 int ipv6,
146 struct efi_ip_address *ip,
147 struct efi_mac_address *mac)
148{
149 EFI_ENTRY("%p, %x, %p, %p", this, ipv6, ip, mac);
150
151 return EFI_EXIT(EFI_INVALID_PARAMETER);
152}
153
154static efi_status_t EFIAPI efi_net_nvdata(struct efi_simple_network *this,
155 int read_write, ulong offset,
156 ulong buffer_size, char *buffer)
157{
158 EFI_ENTRY("%p, %x, %lx, %lx, %p", this, read_write, offset, buffer_size,
159 buffer);
160
Heinrich Schuchardt61da6782017-10-05 16:35:59 +0200161 return EFI_EXIT(EFI_UNSUPPORTED);
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200162}
163
164static efi_status_t EFIAPI efi_net_get_status(struct efi_simple_network *this,
165 u32 *int_status, void **txbuf)
166{
167 EFI_ENTRY("%p, %p, %p", this, int_status, txbuf);
168
Heinrich Schuchardt891b3d92017-10-05 16:36:02 +0200169 efi_timer_check();
170
171 if (int_status) {
172 /* We send packets synchronously, so nothing is outstanding */
173 *int_status = EFI_SIMPLE_NETWORK_TRANSMIT_INTERRUPT;
174 if (new_rx_packet)
175 *int_status |= EFI_SIMPLE_NETWORK_RECEIVE_INTERRUPT;
176 }
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200177 if (txbuf)
178 *txbuf = new_tx_packet;
179
180 new_tx_packet = NULL;
181
182 return EFI_EXIT(EFI_SUCCESS);
183}
184
185static efi_status_t EFIAPI efi_net_transmit(struct efi_simple_network *this,
Heinrich Schuchardt8db174d2017-10-05 16:36:03 +0200186 size_t header_size, size_t buffer_size, void *buffer,
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200187 struct efi_mac_address *src_addr,
188 struct efi_mac_address *dest_addr, u16 *protocol)
189{
Heinrich Schuchardt8db174d2017-10-05 16:36:03 +0200190 EFI_ENTRY("%p, %lu, %lu, %p, %p, %p, %p", this,
191 (unsigned long)header_size, (unsigned long)buffer_size,
192 buffer, src_addr, dest_addr, protocol);
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200193
Heinrich Schuchardta0549ef2017-10-05 16:36:00 +0200194 efi_timer_check();
195
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200196 if (header_size) {
197 /* We would need to create the header if header_size != 0 */
198 return EFI_EXIT(EFI_INVALID_PARAMETER);
199 }
200
Alexander Graf712cd292016-09-06 14:26:27 +0200201#ifdef CONFIG_EFI_LOADER_BOUNCE_BUFFER
202 /* Ethernet packets always fit, just bounce */
203 memcpy(efi_bounce_buffer, buffer, buffer_size);
204 net_send_packet(efi_bounce_buffer, buffer_size);
205#else
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200206 net_send_packet(buffer, buffer_size);
Alexander Graf712cd292016-09-06 14:26:27 +0200207#endif
208
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200209 new_tx_packet = buffer;
210
211 return EFI_EXIT(EFI_SUCCESS);
212}
213
214static void efi_net_push(void *pkt, int len)
215{
216 new_rx_packet = true;
Heinrich Schuchardte5c21602017-10-05 16:36:01 +0200217 wait_for_packet->is_signaled = true;
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200218}
219
Heinrich Schuchardt8db174d2017-10-05 16:36:03 +0200220/*
221 * Receive a packet from a network interface.
222 *
223 * This function implements the Receive service of the Simple Network Protocol.
224 * See the UEFI spec for details.
225 *
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
232 * @protocol protocol
233 * @return status code
234 */
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200235static efi_status_t EFIAPI efi_net_receive(struct efi_simple_network *this,
Heinrich Schuchardt8db174d2017-10-05 16:36:03 +0200236 size_t *header_size, size_t *buffer_size, void *buffer,
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200237 struct efi_mac_address *src_addr,
238 struct efi_mac_address *dest_addr, u16 *protocol)
239{
Heinrich Schuchardt336d9df2017-10-05 16:36:04 +0200240 struct ethernet_hdr *eth_hdr;
241 size_t hdr_size = sizeof(struct ethernet_hdr);
242 u16 protlen;
243
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200244 EFI_ENTRY("%p, %p, %p, %p, %p, %p, %p", this, header_size,
245 buffer_size, buffer, src_addr, dest_addr, protocol);
246
Heinrich Schuchardta0549ef2017-10-05 16:36:00 +0200247 efi_timer_check();
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200248
249 if (!new_rx_packet)
250 return EFI_EXIT(EFI_NOT_READY);
Heinrich Schuchardt336d9df2017-10-05 16:36:04 +0200251 /* 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);
255 }
256 /* Fill export parameters */
257 eth_hdr = (struct ethernet_hdr *)net_rx_packet;
258 protlen = ntohs(eth_hdr->et_protlen);
259 if (protlen == 0x8100) {
260 hdr_size += 4;
261 protlen = ntohs(*(u16 *)&net_rx_packet[hdr_size - 2]);
262 }
263 if (header_size)
264 *header_size = hdr_size;
265 if (dest_addr)
266 memcpy(dest_addr, eth_hdr->et_dest, ARP_HLEN);
267 if (src_addr)
268 memcpy(src_addr, eth_hdr->et_src, ARP_HLEN);
269 if (protocol)
270 *protocol = protlen;
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200271 if (*buffer_size < net_rx_packet_len) {
272 /* Packet doesn't fit, try again with bigger buf */
273 *buffer_size = net_rx_packet_len;
274 return EFI_EXIT(EFI_BUFFER_TOO_SMALL);
275 }
Heinrich Schuchardt336d9df2017-10-05 16:36:04 +0200276 /* Copy packet */
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200277 memcpy(buffer, net_rx_packet, net_rx_packet_len);
278 *buffer_size = net_rx_packet_len;
279 new_rx_packet = false;
280
281 return EFI_EXIT(EFI_SUCCESS);
282}
283
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200284void efi_net_set_dhcp_ack(void *pkt, int len)
285{
286 int maxsize = sizeof(*dhcp_ack);
287
288 if (!dhcp_ack)
289 dhcp_ack = malloc(maxsize);
290
291 memcpy(dhcp_ack, pkt, min(len, maxsize));
292}
293
Heinrich Schuchardta0549ef2017-10-05 16:36:00 +0200294/*
295 * Check if a new network packet has been received.
296 *
297 * This notification function is called in every timer cycle.
298 *
299 * @event the event for which this notification function is registered
300 * @context event context - not used in this function
301 */
302static void EFIAPI efi_network_timer_notify(struct efi_event *event,
303 void *context)
304{
305 EFI_ENTRY("%p, %p", event, context);
306
307 if (!new_rx_packet) {
308 push_packet = efi_net_push;
309 eth_rx();
310 push_packet = NULL;
311 }
312 EFI_EXIT(EFI_SUCCESS);
313}
314
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200315/* This gets called from do_bootefi_exec(). */
Heinrich Schuchardt075d4252018-03-03 15:28:56 +0100316efi_status_t efi_net_register(void)
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200317{
318 struct efi_net_obj *netobj;
Heinrich Schuchardta0549ef2017-10-05 16:36:00 +0200319 efi_status_t r;
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200320
321 if (!eth_get_dev()) {
322 /* No eth device active, don't expose any */
Heinrich Schuchardt075d4252018-03-03 15:28:56 +0100323 return EFI_SUCCESS;
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200324 }
325
326 /* We only expose the "active" eth device, so one is enough */
327 netobj = calloc(1, sizeof(*netobj));
Heinrich Schuchardt075d4252018-03-03 15:28:56 +0100328 if (!netobj) {
329 printf("ERROR: Out of memory\n");
330 return EFI_OUT_OF_RESOURCES;
331 }
Heinrich Schuchardt84d14562017-11-26 14:05:13 +0100332
333 /* Hook net up to the device list */
Heinrich Schuchardtd39646a2018-09-26 05:27:56 +0200334 efi_add_handle(&netobj->header);
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200335
336 /* Fill in object data */
Heinrich Schuchardtd39646a2018-09-26 05:27:56 +0200337 r = efi_add_protocol(&netobj->header, &efi_net_guid,
Heinrich Schuchardt84d14562017-11-26 14:05:13 +0100338 &netobj->net);
339 if (r != EFI_SUCCESS)
Heinrich Schuchardt075d4252018-03-03 15:28:56 +0100340 goto failure_to_add_protocol;
Heinrich Schuchardtd39646a2018-09-26 05:27:56 +0200341 r = efi_add_protocol(&netobj->header, &efi_guid_device_path,
Heinrich Schuchardt84d14562017-11-26 14:05:13 +0100342 efi_dp_from_eth());
343 if (r != EFI_SUCCESS)
Heinrich Schuchardt075d4252018-03-03 15:28:56 +0100344 goto failure_to_add_protocol;
Heinrich Schuchardtd39646a2018-09-26 05:27:56 +0200345 r = efi_add_protocol(&netobj->header, &efi_pxe_guid,
Heinrich Schuchardt84d14562017-11-26 14:05:13 +0100346 &netobj->pxe);
347 if (r != EFI_SUCCESS)
Heinrich Schuchardt075d4252018-03-03 15:28:56 +0100348 goto failure_to_add_protocol;
Heinrich Schuchardtbdecf972017-10-05 16:35:57 +0200349 netobj->net.revision = EFI_SIMPLE_NETWORK_PROTOCOL_REVISION;
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200350 netobj->net.start = efi_net_start;
351 netobj->net.stop = efi_net_stop;
352 netobj->net.initialize = efi_net_initialize;
353 netobj->net.reset = efi_net_reset;
354 netobj->net.shutdown = efi_net_shutdown;
355 netobj->net.receive_filters = efi_net_receive_filters;
356 netobj->net.station_address = efi_net_station_address;
357 netobj->net.statistics = efi_net_statistics;
358 netobj->net.mcastiptomac = efi_net_mcastiptomac;
359 netobj->net.nvdata = efi_net_nvdata;
360 netobj->net.get_status = efi_net_get_status;
361 netobj->net.transmit = efi_net_transmit;
362 netobj->net.receive = efi_net_receive;
363 netobj->net.mode = &netobj->net_mode;
364 netobj->net_mode.state = EFI_NETWORK_STARTED;
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200365 memcpy(netobj->net_mode.current_address.mac_addr, eth_get_ethaddr(), 6);
Heinrich Schuchardt5d4a5ea2017-10-05 16:35:58 +0200366 netobj->net_mode.hwaddr_size = ARP_HLEN;
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200367 netobj->net_mode.max_packet_size = PKTSIZE;
Andrew Thomasf25ddca2018-06-21 16:21:01 -0700368 netobj->net_mode.if_type = ARP_ETHER;
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200369
370 netobj->pxe.mode = &netobj->pxe_mode;
371 if (dhcp_ack)
372 netobj->pxe_mode.dhcp_ack = *dhcp_ack;
373
Heinrich Schuchardta0549ef2017-10-05 16:36:00 +0200374 /*
Heinrich Schuchardte5c21602017-10-05 16:36:01 +0200375 * Create WaitForPacket event.
376 */
377 r = efi_create_event(EVT_NOTIFY_WAIT, TPL_CALLBACK,
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100378 efi_network_timer_notify, NULL, NULL,
Heinrich Schuchardte5c21602017-10-05 16:36:01 +0200379 &wait_for_packet);
380 if (r != EFI_SUCCESS) {
381 printf("ERROR: Failed to register network event\n");
382 return r;
383 }
384 netobj->net.wait_for_packet = wait_for_packet;
385 /*
Heinrich Schuchardta0549ef2017-10-05 16:36:00 +0200386 * Create a timer event.
387 *
388 * The notification function is used to check if a new network packet
389 * has been received.
Heinrich Schuchardtee3db4f2018-03-24 18:40:22 +0100390 *
391 * iPXE is running at TPL_CALLBACK most of the time. Use a higher TPL.
Heinrich Schuchardta0549ef2017-10-05 16:36:00 +0200392 */
Heinrich Schuchardtee3db4f2018-03-24 18:40:22 +0100393 r = efi_create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_NOTIFY,
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100394 efi_network_timer_notify, NULL, NULL,
Heinrich Schuchardta0549ef2017-10-05 16:36:00 +0200395 &network_timer_event);
396 if (r != EFI_SUCCESS) {
397 printf("ERROR: Failed to register network event\n");
398 return r;
399 }
400 /* Network is time critical, create event in every timer cyle */
401 r = efi_set_timer(network_timer_event, EFI_TIMER_PERIODIC, 0);
402 if (r != EFI_SUCCESS) {
403 printf("ERROR: Failed to set network timer\n");
404 return r;
405 }
406
Heinrich Schuchardt075d4252018-03-03 15:28:56 +0100407 return EFI_SUCCESS;
408failure_to_add_protocol:
409 printf("ERROR: Failure to add protocol\n");
410 return r;
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200411}