blob: 68ed9e9520c97494c2d08ddf685fdf871a2e974f [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 Schuchardt622fe622018-12-01 00:16:32 +010017static void *transmit_buffer;
18
Heinrich Schuchardta0549ef2017-10-05 16:36:00 +020019/*
20 * The notification function of this event is called in every timer cycle
21 * to check if a new network packet has been received.
22 */
23static struct efi_event *network_timer_event;
Heinrich Schuchardte5c21602017-10-05 16:36:01 +020024/*
25 * This event is signaled when a packet has been received.
26 */
27static struct efi_event *wait_for_packet;
Alexander Graf0efe1bc2016-05-06 21:01:01 +020028
Heinrich Schuchardtd39646a2018-09-26 05:27:56 +020029/**
30 * struct efi_net_obj - EFI object representing a network interface
31 *
32 * @header: EFI object header
33 * @net: simple network protocol interface
34 * @net_mode: status of the network interface
35 * @pxe: PXE base code protocol interface
36 * @pxe_mode: status of the PXE base code protocol
37 */
Alexander Graf0efe1bc2016-05-06 21:01:01 +020038struct efi_net_obj {
Heinrich Schuchardtd39646a2018-09-26 05:27:56 +020039 struct efi_object header;
Alexander Graf0efe1bc2016-05-06 21:01:01 +020040 struct efi_simple_network net;
41 struct efi_simple_network_mode net_mode;
Alexander Graf0efe1bc2016-05-06 21:01:01 +020042 struct efi_pxe pxe;
43 struct efi_pxe_mode pxe_mode;
44};
45
46static efi_status_t EFIAPI efi_net_start(struct efi_simple_network *this)
47{
48 EFI_ENTRY("%p", this);
49
50 return EFI_EXIT(EFI_SUCCESS);
51}
52
53static efi_status_t EFIAPI efi_net_stop(struct efi_simple_network *this)
54{
55 EFI_ENTRY("%p", this);
56
57 return EFI_EXIT(EFI_SUCCESS);
58}
59
Heinrich Schuchardt0c5d2a32018-04-03 22:06:52 +020060/*
61 * Initialize network adapter and allocate transmit and receive buffers.
62 *
63 * This function implements the Initialize service of the
64 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
65 * (UEFI) specification for details.
66 *
67 * @this: pointer to the protocol instance
68 * @extra_rx: extra receive buffer to be allocated
69 * @extra_tx: extra transmit buffer to be allocated
70 * @return: status code
71 */
Alexander Graf0efe1bc2016-05-06 21:01:01 +020072static efi_status_t EFIAPI efi_net_initialize(struct efi_simple_network *this,
73 ulong extra_rx, ulong extra_tx)
74{
Heinrich Schuchardt0c5d2a32018-04-03 22:06:52 +020075 int ret;
76 efi_status_t r = EFI_SUCCESS;
77
Alexander Graf0efe1bc2016-05-06 21:01:01 +020078 EFI_ENTRY("%p, %lx, %lx", this, extra_rx, extra_tx);
79
Heinrich Schuchardt0c5d2a32018-04-03 22:06:52 +020080 if (!this) {
81 r = EFI_INVALID_PARAMETER;
82 goto error;
83 }
Alexander Graf0efe1bc2016-05-06 21:01:01 +020084
Heinrich Schuchardt0c5d2a32018-04-03 22:06:52 +020085 /* Setup packet buffers */
86 net_init();
87 /* Disable hardware and put it into the reset state */
88 eth_halt();
89 /* Set current device according to environment variables */
90 eth_set_current();
91 /* Get hardware ready for send and receive operations */
92 ret = eth_init();
93 if (ret < 0) {
94 eth_halt();
95 r = EFI_DEVICE_ERROR;
96 }
97
98error:
99 return EFI_EXIT(r);
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200100}
101
102static efi_status_t EFIAPI efi_net_reset(struct efi_simple_network *this,
103 int extended_verification)
104{
105 EFI_ENTRY("%p, %x", this, extended_verification);
106
107 return EFI_EXIT(EFI_SUCCESS);
108}
109
110static efi_status_t EFIAPI efi_net_shutdown(struct efi_simple_network *this)
111{
112 EFI_ENTRY("%p", this);
113
114 return EFI_EXIT(EFI_SUCCESS);
115}
116
117static efi_status_t EFIAPI efi_net_receive_filters(
118 struct efi_simple_network *this, u32 enable, u32 disable,
119 int reset_mcast_filter, ulong mcast_filter_count,
120 struct efi_mac_address *mcast_filter)
121{
122 EFI_ENTRY("%p, %x, %x, %x, %lx, %p", this, enable, disable,
123 reset_mcast_filter, mcast_filter_count, mcast_filter);
124
Heinrich Schuchardt61da6782017-10-05 16:35:59 +0200125 return EFI_EXIT(EFI_UNSUPPORTED);
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200126}
127
128static efi_status_t EFIAPI efi_net_station_address(
129 struct efi_simple_network *this, int reset,
130 struct efi_mac_address *new_mac)
131{
132 EFI_ENTRY("%p, %x, %p", this, reset, new_mac);
133
Heinrich Schuchardt61da6782017-10-05 16:35:59 +0200134 return EFI_EXIT(EFI_UNSUPPORTED);
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200135}
136
137static efi_status_t EFIAPI efi_net_statistics(struct efi_simple_network *this,
138 int reset, ulong *stat_size,
139 void *stat_table)
140{
141 EFI_ENTRY("%p, %x, %p, %p", this, reset, stat_size, stat_table);
142
Heinrich Schuchardt61da6782017-10-05 16:35:59 +0200143 return EFI_EXIT(EFI_UNSUPPORTED);
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200144}
145
146static efi_status_t EFIAPI efi_net_mcastiptomac(struct efi_simple_network *this,
147 int ipv6,
148 struct efi_ip_address *ip,
149 struct efi_mac_address *mac)
150{
151 EFI_ENTRY("%p, %x, %p, %p", this, ipv6, ip, mac);
152
153 return EFI_EXIT(EFI_INVALID_PARAMETER);
154}
155
156static efi_status_t EFIAPI efi_net_nvdata(struct efi_simple_network *this,
157 int read_write, ulong offset,
158 ulong buffer_size, char *buffer)
159{
160 EFI_ENTRY("%p, %x, %lx, %lx, %p", this, read_write, offset, buffer_size,
161 buffer);
162
Heinrich Schuchardt61da6782017-10-05 16:35:59 +0200163 return EFI_EXIT(EFI_UNSUPPORTED);
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200164}
165
166static efi_status_t EFIAPI efi_net_get_status(struct efi_simple_network *this,
167 u32 *int_status, void **txbuf)
168{
169 EFI_ENTRY("%p, %p, %p", this, int_status, txbuf);
170
Heinrich Schuchardt891b3d92017-10-05 16:36:02 +0200171 efi_timer_check();
172
173 if (int_status) {
174 /* We send packets synchronously, so nothing is outstanding */
175 *int_status = EFI_SIMPLE_NETWORK_TRANSMIT_INTERRUPT;
176 if (new_rx_packet)
177 *int_status |= EFI_SIMPLE_NETWORK_RECEIVE_INTERRUPT;
178 }
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200179 if (txbuf)
180 *txbuf = new_tx_packet;
181
182 new_tx_packet = NULL;
183
184 return EFI_EXIT(EFI_SUCCESS);
185}
186
187static efi_status_t EFIAPI efi_net_transmit(struct efi_simple_network *this,
Heinrich Schuchardt8db174d2017-10-05 16:36:03 +0200188 size_t header_size, size_t buffer_size, void *buffer,
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200189 struct efi_mac_address *src_addr,
190 struct efi_mac_address *dest_addr, u16 *protocol)
191{
Heinrich Schuchardt8db174d2017-10-05 16:36:03 +0200192 EFI_ENTRY("%p, %lu, %lu, %p, %p, %p, %p", this,
193 (unsigned long)header_size, (unsigned long)buffer_size,
194 buffer, src_addr, dest_addr, protocol);
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200195
Heinrich Schuchardta0549ef2017-10-05 16:36:00 +0200196 efi_timer_check();
197
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200198 if (header_size) {
199 /* We would need to create the header if header_size != 0 */
200 return EFI_EXIT(EFI_INVALID_PARAMETER);
201 }
202
Heinrich Schuchardt622fe622018-12-01 00:16:32 +0100203 /* Ensure that the packet fits into the buffer */
204 if (buffer_size > PKTSIZE_ALIGN)
205 return EFI_EXIT(EFI_INVALID_PARAMETER);
206 memcpy(transmit_buffer, buffer, buffer_size);
207 net_send_packet(transmit_buffer, buffer_size);
Alexander Graf712cd292016-09-06 14:26:27 +0200208
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) {
Heinrich Schuchardte1fec152018-10-18 21:51:38 +0200272 /* Packet doesn't fit, try again with bigger buffer */
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200273 *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()) {
Heinrich Schuchardte1fec152018-10-18 21:51:38 +0200322 /* No network 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
Heinrich Schuchardte1fec152018-10-18 21:51:38 +0200326 /* We only expose the "active" network device, so one is enough */
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200327 netobj = calloc(1, sizeof(*netobj));
Heinrich Schuchardt622fe622018-12-01 00:16:32 +0100328 if (!netobj)
329 goto out_of_resources;
330
331 /* Allocate an aligned transmit buffer */
332 transmit_buffer = calloc(1, PKTSIZE_ALIGN + PKTALIGN);
333 if (!transmit_buffer)
334 goto out_of_resources;
335 transmit_buffer = (void *)ALIGN((uintptr_t)transmit_buffer, PKTALIGN);
Heinrich Schuchardt84d14562017-11-26 14:05:13 +0100336
337 /* Hook net up to the device list */
Heinrich Schuchardtd39646a2018-09-26 05:27:56 +0200338 efi_add_handle(&netobj->header);
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200339
340 /* Fill in object data */
Heinrich Schuchardtd39646a2018-09-26 05:27:56 +0200341 r = efi_add_protocol(&netobj->header, &efi_net_guid,
Heinrich Schuchardt84d14562017-11-26 14:05:13 +0100342 &netobj->net);
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_guid_device_path,
Heinrich Schuchardt84d14562017-11-26 14:05:13 +0100346 efi_dp_from_eth());
347 if (r != EFI_SUCCESS)
Heinrich Schuchardt075d4252018-03-03 15:28:56 +0100348 goto failure_to_add_protocol;
Heinrich Schuchardtd39646a2018-09-26 05:27:56 +0200349 r = efi_add_protocol(&netobj->header, &efi_pxe_guid,
Heinrich Schuchardt84d14562017-11-26 14:05:13 +0100350 &netobj->pxe);
351 if (r != EFI_SUCCESS)
Heinrich Schuchardt075d4252018-03-03 15:28:56 +0100352 goto failure_to_add_protocol;
Heinrich Schuchardtbdecf972017-10-05 16:35:57 +0200353 netobj->net.revision = EFI_SIMPLE_NETWORK_PROTOCOL_REVISION;
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200354 netobj->net.start = efi_net_start;
355 netobj->net.stop = efi_net_stop;
356 netobj->net.initialize = efi_net_initialize;
357 netobj->net.reset = efi_net_reset;
358 netobj->net.shutdown = efi_net_shutdown;
359 netobj->net.receive_filters = efi_net_receive_filters;
360 netobj->net.station_address = efi_net_station_address;
361 netobj->net.statistics = efi_net_statistics;
362 netobj->net.mcastiptomac = efi_net_mcastiptomac;
363 netobj->net.nvdata = efi_net_nvdata;
364 netobj->net.get_status = efi_net_get_status;
365 netobj->net.transmit = efi_net_transmit;
366 netobj->net.receive = efi_net_receive;
367 netobj->net.mode = &netobj->net_mode;
368 netobj->net_mode.state = EFI_NETWORK_STARTED;
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200369 memcpy(netobj->net_mode.current_address.mac_addr, eth_get_ethaddr(), 6);
Heinrich Schuchardt5d4a5ea2017-10-05 16:35:58 +0200370 netobj->net_mode.hwaddr_size = ARP_HLEN;
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200371 netobj->net_mode.max_packet_size = PKTSIZE;
Andrew Thomasf25ddca2018-06-21 16:21:01 -0700372 netobj->net_mode.if_type = ARP_ETHER;
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200373
374 netobj->pxe.mode = &netobj->pxe_mode;
375 if (dhcp_ack)
376 netobj->pxe_mode.dhcp_ack = *dhcp_ack;
377
Heinrich Schuchardta0549ef2017-10-05 16:36:00 +0200378 /*
Heinrich Schuchardte5c21602017-10-05 16:36:01 +0200379 * Create WaitForPacket event.
380 */
381 r = efi_create_event(EVT_NOTIFY_WAIT, TPL_CALLBACK,
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100382 efi_network_timer_notify, NULL, NULL,
Heinrich Schuchardte5c21602017-10-05 16:36:01 +0200383 &wait_for_packet);
384 if (r != EFI_SUCCESS) {
385 printf("ERROR: Failed to register network event\n");
386 return r;
387 }
388 netobj->net.wait_for_packet = wait_for_packet;
389 /*
Heinrich Schuchardta0549ef2017-10-05 16:36:00 +0200390 * Create a timer event.
391 *
392 * The notification function is used to check if a new network packet
393 * has been received.
Heinrich Schuchardtee3db4f2018-03-24 18:40:22 +0100394 *
395 * iPXE is running at TPL_CALLBACK most of the time. Use a higher TPL.
Heinrich Schuchardta0549ef2017-10-05 16:36:00 +0200396 */
Heinrich Schuchardtee3db4f2018-03-24 18:40:22 +0100397 r = efi_create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_NOTIFY,
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100398 efi_network_timer_notify, NULL, NULL,
Heinrich Schuchardta0549ef2017-10-05 16:36:00 +0200399 &network_timer_event);
400 if (r != EFI_SUCCESS) {
401 printf("ERROR: Failed to register network event\n");
402 return r;
403 }
Heinrich Schuchardte1fec152018-10-18 21:51:38 +0200404 /* Network is time critical, create event in every timer cycle */
Heinrich Schuchardta0549ef2017-10-05 16:36:00 +0200405 r = efi_set_timer(network_timer_event, EFI_TIMER_PERIODIC, 0);
406 if (r != EFI_SUCCESS) {
407 printf("ERROR: Failed to set network timer\n");
408 return r;
409 }
410
Heinrich Schuchardt075d4252018-03-03 15:28:56 +0100411 return EFI_SUCCESS;
412failure_to_add_protocol:
413 printf("ERROR: Failure to add protocol\n");
414 return r;
Heinrich Schuchardt622fe622018-12-01 00:16:32 +0100415out_of_resources:
416 free(netobj);
417 /* free(transmit_buffer) not needed yet */
418 printf("ERROR: Out of memory\n");
419 return EFI_OUT_OF_RESOURCES;
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200420}