blob: d71c663068258be8ec909b8d81a4d8d5ba088815 [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
Heinrich Schuchardtdec88e42019-04-20 07:39:11 +020012static const efi_guid_t efi_net_guid = EFI_SIMPLE_NETWORK_PROTOCOL_GUID;
13static const efi_guid_t efi_pxe_guid = EFI_PXE_BASE_CODE_PROTOCOL_GUID;
Alexander Graf0efe1bc2016-05-06 21:01:01 +020014static 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
Heinrich Schuchardt41b05872018-12-01 00:16:33 +010046/*
47 * efi_net_start() - start the network interface
48 *
49 * This function implements the Start service of the
50 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
51 * (UEFI) specification for details.
52 *
53 * @this: pointer to the protocol instance
54 * Return: status code
55 */
Alexander Graf0efe1bc2016-05-06 21:01:01 +020056static efi_status_t EFIAPI efi_net_start(struct efi_simple_network *this)
57{
Heinrich Schuchardt41b05872018-12-01 00:16:33 +010058 efi_status_t ret = EFI_SUCCESS;
59
Alexander Graf0efe1bc2016-05-06 21:01:01 +020060 EFI_ENTRY("%p", this);
61
Heinrich Schuchardt41b05872018-12-01 00:16:33 +010062 /* Check parameters */
63 if (!this) {
64 ret = EFI_INVALID_PARAMETER;
65 goto out;
66 }
Alexander Graf0efe1bc2016-05-06 21:01:01 +020067
Heinrich Schuchardt41b05872018-12-01 00:16:33 +010068 if (this->mode->state != EFI_NETWORK_STOPPED)
69 ret = EFI_ALREADY_STARTED;
70 else
71 this->mode->state = EFI_NETWORK_STARTED;
72out:
73 return EFI_EXIT(ret);
Alexander Graf0efe1bc2016-05-06 21:01:01 +020074}
75
Heinrich Schuchardt0c5d2a32018-04-03 22:06:52 +020076/*
Heinrich Schuchardt41b05872018-12-01 00:16:33 +010077 * efi_net_stop() - stop the network interface
78 *
79 * This function implements the Stop service of the
80 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
81 * (UEFI) specification for details.
82 *
83 * @this: pointer to the protocol instance
84 * Return: status code
85 */
86static efi_status_t EFIAPI efi_net_stop(struct efi_simple_network *this)
87{
88 efi_status_t ret = EFI_SUCCESS;
89
90 EFI_ENTRY("%p", this);
91
92 /* Check parameters */
93 if (!this) {
94 ret = EFI_INVALID_PARAMETER;
95 goto out;
96 }
97
98 if (this->mode->state == EFI_NETWORK_STOPPED)
99 ret = EFI_NOT_STARTED;
100 else
101 this->mode->state = EFI_NETWORK_STOPPED;
102out:
103 return EFI_EXIT(ret);
104}
105
106/*
107 * efi_net_initialize() - initialize the network interface
Heinrich Schuchardt0c5d2a32018-04-03 22:06:52 +0200108 *
109 * This function implements the Initialize service of the
110 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
111 * (UEFI) specification for details.
112 *
113 * @this: pointer to the protocol instance
114 * @extra_rx: extra receive buffer to be allocated
115 * @extra_tx: extra transmit buffer to be allocated
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100116 * Return: status code
Heinrich Schuchardt0c5d2a32018-04-03 22:06:52 +0200117 */
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200118static efi_status_t EFIAPI efi_net_initialize(struct efi_simple_network *this,
119 ulong extra_rx, ulong extra_tx)
120{
Heinrich Schuchardt0c5d2a32018-04-03 22:06:52 +0200121 int ret;
122 efi_status_t r = EFI_SUCCESS;
123
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200124 EFI_ENTRY("%p, %lx, %lx", this, extra_rx, extra_tx);
125
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100126 /* Check parameters */
Heinrich Schuchardt0c5d2a32018-04-03 22:06:52 +0200127 if (!this) {
128 r = EFI_INVALID_PARAMETER;
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100129 goto out;
Heinrich Schuchardt0c5d2a32018-04-03 22:06:52 +0200130 }
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200131
Heinrich Schuchardt0c5d2a32018-04-03 22:06:52 +0200132 /* Setup packet buffers */
133 net_init();
134 /* Disable hardware and put it into the reset state */
135 eth_halt();
136 /* Set current device according to environment variables */
137 eth_set_current();
138 /* Get hardware ready for send and receive operations */
139 ret = eth_init();
140 if (ret < 0) {
141 eth_halt();
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100142 this->mode->state = EFI_NETWORK_STOPPED;
Heinrich Schuchardt0c5d2a32018-04-03 22:06:52 +0200143 r = EFI_DEVICE_ERROR;
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100144 goto out;
145 } else {
146 this->mode->state = EFI_NETWORK_INITIALIZED;
Heinrich Schuchardt0c5d2a32018-04-03 22:06:52 +0200147 }
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100148out:
Heinrich Schuchardt0c5d2a32018-04-03 22:06:52 +0200149 return EFI_EXIT(r);
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200150}
151
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100152/*
153 * efi_net_reset() - reinitialize the network interface
154 *
155 * This function implements the Reset service of the
156 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
157 * (UEFI) specification for details.
158 *
159 * @this: pointer to the protocol instance
160 * @extended_verification: execute exhaustive verification
161 * Return: status code
162 */
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200163static efi_status_t EFIAPI efi_net_reset(struct efi_simple_network *this,
164 int extended_verification)
165{
166 EFI_ENTRY("%p, %x", this, extended_verification);
167
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100168 return EFI_EXIT(EFI_CALL(efi_net_initialize(this, 0, 0)));
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200169}
170
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100171/*
172 * efi_net_shutdown() - shut down the network interface
173 *
174 * This function implements the Shutdown service of the
175 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
176 * (UEFI) specification for details.
177 *
178 * @this: pointer to the protocol instance
179 * Return: status code
180 */
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200181static efi_status_t EFIAPI efi_net_shutdown(struct efi_simple_network *this)
182{
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100183 efi_status_t ret = EFI_SUCCESS;
184
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200185 EFI_ENTRY("%p", this);
186
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100187 /* Check parameters */
188 if (!this) {
189 ret = EFI_INVALID_PARAMETER;
190 goto out;
191 }
192
193 eth_halt();
194 this->mode->state = EFI_NETWORK_STOPPED;
195
196out:
197 return EFI_EXIT(ret);
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200198}
199
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100200/*
201 * efi_net_receive_filters() - mange multicast receive filters
202 *
203 * This function implements the ReceiveFilters service of the
204 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
205 * (UEFI) specification for details.
206 *
207 * @this: pointer to the protocol instance
208 * @enable: bit mask of receive filters to enable
209 * @disable: bit mask of receive filters to disable
210 * @reset_mcast_filter: true resets contents of the filters
211 * @mcast_filter_count: number of hardware MAC addresses in the new filters list
212 * @mcast_filter: list of new filters
213 * Return: status code
214 */
215static efi_status_t EFIAPI efi_net_receive_filters
216 (struct efi_simple_network *this, u32 enable, u32 disable,
217 int reset_mcast_filter, ulong mcast_filter_count,
218 struct efi_mac_address *mcast_filter)
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200219{
220 EFI_ENTRY("%p, %x, %x, %x, %lx, %p", this, enable, disable,
221 reset_mcast_filter, mcast_filter_count, mcast_filter);
222
Heinrich Schuchardt61da6782017-10-05 16:35:59 +0200223 return EFI_EXIT(EFI_UNSUPPORTED);
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200224}
225
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100226/*
227 * efi_net_station_address() - set the hardware MAC address
228 *
229 * This function implements the StationAddress service of the
230 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
231 * (UEFI) specification for details.
232 *
233 * @this: pointer to the protocol instance
234 * @reset: if true reset the address to default
235 * @new_mac: new MAC address
236 * Return: status code
237 */
238static efi_status_t EFIAPI efi_net_station_address
239 (struct efi_simple_network *this, int reset,
240 struct efi_mac_address *new_mac)
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200241{
242 EFI_ENTRY("%p, %x, %p", this, reset, new_mac);
243
Heinrich Schuchardt61da6782017-10-05 16:35:59 +0200244 return EFI_EXIT(EFI_UNSUPPORTED);
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200245}
246
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100247/*
248 * efi_net_statistics() - reset or collect statistics of the network interface
249 *
250 * This function implements the Statistics service of the
251 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
252 * (UEFI) specification for details.
253 *
254 * @this: pointer to the protocol instance
255 * @reset: if true, the statistics are reset
256 * @stat_size: size of the statistics table
257 * @stat_table: table to receive the statistics
258 * Return: status code
259 */
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200260static efi_status_t EFIAPI efi_net_statistics(struct efi_simple_network *this,
261 int reset, ulong *stat_size,
262 void *stat_table)
263{
264 EFI_ENTRY("%p, %x, %p, %p", this, reset, stat_size, stat_table);
265
Heinrich Schuchardt61da6782017-10-05 16:35:59 +0200266 return EFI_EXIT(EFI_UNSUPPORTED);
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200267}
268
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100269/*
270 * efi_net_mcastiptomac() - translate multicast IP address to MAC address
271 *
272 * This function implements the Statistics service of the
273 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
274 * (UEFI) specification for details.
275 *
276 * @this: pointer to the protocol instance
277 * @ipv6: true if the IP address is an IPv6 address
278 * @ip: IP address
279 * @mac: MAC address
280 * Return: status code
281 */
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200282static efi_status_t EFIAPI efi_net_mcastiptomac(struct efi_simple_network *this,
283 int ipv6,
284 struct efi_ip_address *ip,
285 struct efi_mac_address *mac)
286{
287 EFI_ENTRY("%p, %x, %p, %p", this, ipv6, ip, mac);
288
289 return EFI_EXIT(EFI_INVALID_PARAMETER);
290}
291
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100292/**
293 * efi_net_nvdata() - read or write NVRAM
294 *
295 * This function implements the GetStatus service of the Simple Network
296 * Protocol. See the UEFI spec for details.
297 *
298 * @this: the instance of the Simple Network Protocol
299 * @readwrite: true for read, false for write
300 * @offset: offset in NVRAM
301 * @buffer_size: size of buffer
302 * @buffer: buffer
303 * Return: status code
304 */
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200305static efi_status_t EFIAPI efi_net_nvdata(struct efi_simple_network *this,
306 int read_write, ulong offset,
307 ulong buffer_size, char *buffer)
308{
309 EFI_ENTRY("%p, %x, %lx, %lx, %p", this, read_write, offset, buffer_size,
310 buffer);
311
Heinrich Schuchardt61da6782017-10-05 16:35:59 +0200312 return EFI_EXIT(EFI_UNSUPPORTED);
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200313}
314
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100315/**
316 * efi_net_get_status() - get interrupt status
317 *
318 * This function implements the GetStatus service of the Simple Network
319 * Protocol. See the UEFI spec for details.
320 *
321 * @this: the instance of the Simple Network Protocol
322 * @int_status: interface status
323 * @txbuf: transmission buffer
324 */
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200325static efi_status_t EFIAPI efi_net_get_status(struct efi_simple_network *this,
326 u32 *int_status, void **txbuf)
327{
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100328 efi_status_t ret = EFI_SUCCESS;
329
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200330 EFI_ENTRY("%p, %p, %p", this, int_status, txbuf);
331
Heinrich Schuchardt891b3d92017-10-05 16:36:02 +0200332 efi_timer_check();
333
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100334 /* Check parameters */
335 if (!this) {
336 ret = EFI_INVALID_PARAMETER;
337 goto out;
338 }
339
340 switch (this->mode->state) {
341 case EFI_NETWORK_STOPPED:
342 ret = EFI_NOT_STARTED;
343 goto out;
344 case EFI_NETWORK_STARTED:
345 ret = EFI_DEVICE_ERROR;
346 goto out;
347 default:
348 break;
349 }
350
Heinrich Schuchardt891b3d92017-10-05 16:36:02 +0200351 if (int_status) {
352 /* We send packets synchronously, so nothing is outstanding */
353 *int_status = EFI_SIMPLE_NETWORK_TRANSMIT_INTERRUPT;
354 if (new_rx_packet)
355 *int_status |= EFI_SIMPLE_NETWORK_RECEIVE_INTERRUPT;
356 }
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200357 if (txbuf)
358 *txbuf = new_tx_packet;
359
360 new_tx_packet = NULL;
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100361out:
362 return EFI_EXIT(ret);
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200363}
364
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100365/**
366 * efi_net_transmit() - transmit a packet
367 *
368 * This function implements the Transmit service of the Simple Network Protocol.
369 * See the UEFI spec for details.
370 *
371 * @this: the instance of the Simple Network Protocol
372 * @header_size: size of the media header
373 * @buffer_size: size of the buffer to receive the packet
374 * @buffer: buffer to receive the packet
375 * @src_addr: source hardware MAC address
376 * @dest_addr: destination hardware MAC address
377 * @protocol: type of header to build
378 * Return: status code
379 */
380static efi_status_t EFIAPI efi_net_transmit
381 (struct efi_simple_network *this, size_t header_size,
382 size_t buffer_size, void *buffer,
383 struct efi_mac_address *src_addr,
384 struct efi_mac_address *dest_addr, u16 *protocol)
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200385{
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100386 efi_status_t ret = EFI_SUCCESS;
387
Heinrich Schuchardt8db174d2017-10-05 16:36:03 +0200388 EFI_ENTRY("%p, %lu, %lu, %p, %p, %p, %p", this,
389 (unsigned long)header_size, (unsigned long)buffer_size,
390 buffer, src_addr, dest_addr, protocol);
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200391
Heinrich Schuchardta0549ef2017-10-05 16:36:00 +0200392 efi_timer_check();
393
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100394 /* Check parameters */
Heinrich Schuchardtce54fdc2019-05-15 23:27:43 +0200395 if (!this || !buffer) {
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100396 ret = EFI_INVALID_PARAMETER;
397 goto out;
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200398 }
399
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100400 /* We do not support jumbo packets */
401 if (buffer_size > PKTSIZE_ALIGN) {
402 ret = EFI_INVALID_PARAMETER;
403 goto out;
404 }
405
406 if (header_size) {
407 /*
408 * TODO: We would need to create the header
409 * if header_size != 0
410 */
Heinrich Schuchardtce54fdc2019-05-15 23:27:43 +0200411 ret = EFI_UNSUPPORTED;
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100412 goto out;
413 }
414
415 switch (this->mode->state) {
416 case EFI_NETWORK_STOPPED:
417 ret = EFI_NOT_STARTED;
418 goto out;
419 case EFI_NETWORK_STARTED:
420 ret = EFI_DEVICE_ERROR;
421 goto out;
422 default:
423 break;
424 }
425
426 /* Ethernet packets always fit, just bounce */
Heinrich Schuchardt622fe622018-12-01 00:16:32 +0100427 memcpy(transmit_buffer, buffer, buffer_size);
428 net_send_packet(transmit_buffer, buffer_size);
Alexander Graf712cd292016-09-06 14:26:27 +0200429
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200430 new_tx_packet = buffer;
431
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100432out:
433 return EFI_EXIT(ret);
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200434}
435
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100436/**
437 * efi_net_receive() - receive a packet from a network interface
Heinrich Schuchardt8db174d2017-10-05 16:36:03 +0200438 *
439 * This function implements the Receive service of the Simple Network Protocol.
440 * See the UEFI spec for details.
441 *
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100442 * @this: the instance of the Simple Network Protocol
443 * @header_size: size of the media header
444 * @buffer_size: size of the buffer to receive the packet
445 * @buffer: buffer to receive the packet
446 * @src_addr: source MAC address
447 * @dest_addr: destination MAC address
448 * @protocol: protocol
449 * Return: status code
Heinrich Schuchardt8db174d2017-10-05 16:36:03 +0200450 */
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100451static efi_status_t EFIAPI efi_net_receive
452 (struct efi_simple_network *this, size_t *header_size,
453 size_t *buffer_size, void *buffer,
454 struct efi_mac_address *src_addr,
455 struct efi_mac_address *dest_addr, u16 *protocol)
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200456{
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100457 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardt336d9df2017-10-05 16:36:04 +0200458 struct ethernet_hdr *eth_hdr;
459 size_t hdr_size = sizeof(struct ethernet_hdr);
460 u16 protlen;
461
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200462 EFI_ENTRY("%p, %p, %p, %p, %p, %p, %p", this, header_size,
463 buffer_size, buffer, src_addr, dest_addr, protocol);
464
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100465 /* Execute events */
Heinrich Schuchardta0549ef2017-10-05 16:36:00 +0200466 efi_timer_check();
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200467
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100468 /* Check parameters */
Heinrich Schuchardtce54fdc2019-05-15 23:27:43 +0200469 if (!this || !buffer || !buffer_size) {
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100470 ret = EFI_INVALID_PARAMETER;
471 goto out;
472 }
473
474 switch (this->mode->state) {
475 case EFI_NETWORK_STOPPED:
476 ret = EFI_NOT_STARTED;
477 goto out;
478 case EFI_NETWORK_STARTED:
479 ret = EFI_DEVICE_ERROR;
480 goto out;
481 default:
482 break;
483 }
484
485 if (!new_rx_packet) {
486 ret = EFI_NOT_READY;
487 goto out;
488 }
Heinrich Schuchardt336d9df2017-10-05 16:36:04 +0200489 /* Check that we at least received an Ethernet header */
490 if (net_rx_packet_len < sizeof(struct ethernet_hdr)) {
491 new_rx_packet = false;
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100492 ret = EFI_NOT_READY;
493 goto out;
Heinrich Schuchardt336d9df2017-10-05 16:36:04 +0200494 }
495 /* Fill export parameters */
496 eth_hdr = (struct ethernet_hdr *)net_rx_packet;
497 protlen = ntohs(eth_hdr->et_protlen);
498 if (protlen == 0x8100) {
499 hdr_size += 4;
500 protlen = ntohs(*(u16 *)&net_rx_packet[hdr_size - 2]);
501 }
502 if (header_size)
503 *header_size = hdr_size;
504 if (dest_addr)
505 memcpy(dest_addr, eth_hdr->et_dest, ARP_HLEN);
506 if (src_addr)
507 memcpy(src_addr, eth_hdr->et_src, ARP_HLEN);
508 if (protocol)
509 *protocol = protlen;
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200510 if (*buffer_size < net_rx_packet_len) {
Heinrich Schuchardte1fec152018-10-18 21:51:38 +0200511 /* Packet doesn't fit, try again with bigger buffer */
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200512 *buffer_size = net_rx_packet_len;
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100513 ret = EFI_BUFFER_TOO_SMALL;
514 goto out;
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200515 }
Heinrich Schuchardt336d9df2017-10-05 16:36:04 +0200516 /* Copy packet */
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200517 memcpy(buffer, net_rx_packet, net_rx_packet_len);
518 *buffer_size = net_rx_packet_len;
519 new_rx_packet = false;
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100520out:
521 return EFI_EXIT(ret);
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200522}
523
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100524/**
525 * efi_net_set_dhcp_ack() - take note of a selected DHCP IP address
526 *
527 * This function is called by dhcp_handler().
528 */
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200529void efi_net_set_dhcp_ack(void *pkt, int len)
530{
531 int maxsize = sizeof(*dhcp_ack);
532
533 if (!dhcp_ack)
534 dhcp_ack = malloc(maxsize);
535
536 memcpy(dhcp_ack, pkt, min(len, maxsize));
537}
538
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100539/**
540 * efi_net_push() - callback for received network packet
541 *
542 * This function is called when a network packet is received by eth_rx().
543 *
544 * @pkt: network packet
545 * @len: length
546 */
547static void efi_net_push(void *pkt, int len)
548{
549 new_rx_packet = true;
550 wait_for_packet->is_signaled = true;
551}
552
553/**
554 * efi_network_timer_notify() - check if a new network packet has been received
Heinrich Schuchardta0549ef2017-10-05 16:36:00 +0200555 *
556 * This notification function is called in every timer cycle.
557 *
558 * @event the event for which this notification function is registered
559 * @context event context - not used in this function
560 */
561static void EFIAPI efi_network_timer_notify(struct efi_event *event,
562 void *context)
563{
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100564 struct efi_simple_network *this = (struct efi_simple_network *)context;
565
Heinrich Schuchardta0549ef2017-10-05 16:36:00 +0200566 EFI_ENTRY("%p, %p", event, context);
567
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100568 /*
569 * Some network drivers do not support calling eth_rx() before
570 * initialization.
571 */
572 if (!this || this->mode->state != EFI_NETWORK_INITIALIZED)
573 goto out;
574
Heinrich Schuchardta0549ef2017-10-05 16:36:00 +0200575 if (!new_rx_packet) {
576 push_packet = efi_net_push;
577 eth_rx();
578 push_packet = NULL;
579 }
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100580out:
Heinrich Schuchardta0549ef2017-10-05 16:36:00 +0200581 EFI_EXIT(EFI_SUCCESS);
582}
583
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100584/**
585 * efi_net_register() - register the simple network protocol
586 *
587 * This gets called from do_bootefi_exec().
588 */
Heinrich Schuchardt075d4252018-03-03 15:28:56 +0100589efi_status_t efi_net_register(void)
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200590{
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100591 struct efi_net_obj *netobj = NULL;
Heinrich Schuchardta0549ef2017-10-05 16:36:00 +0200592 efi_status_t r;
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200593
594 if (!eth_get_dev()) {
Heinrich Schuchardte1fec152018-10-18 21:51:38 +0200595 /* No network device active, don't expose any */
Heinrich Schuchardt075d4252018-03-03 15:28:56 +0100596 return EFI_SUCCESS;
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200597 }
598
Heinrich Schuchardte1fec152018-10-18 21:51:38 +0200599 /* We only expose the "active" network device, so one is enough */
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200600 netobj = calloc(1, sizeof(*netobj));
Heinrich Schuchardt622fe622018-12-01 00:16:32 +0100601 if (!netobj)
602 goto out_of_resources;
603
604 /* Allocate an aligned transmit buffer */
605 transmit_buffer = calloc(1, PKTSIZE_ALIGN + PKTALIGN);
606 if (!transmit_buffer)
607 goto out_of_resources;
608 transmit_buffer = (void *)ALIGN((uintptr_t)transmit_buffer, PKTALIGN);
Heinrich Schuchardt84d14562017-11-26 14:05:13 +0100609
610 /* Hook net up to the device list */
Heinrich Schuchardtd39646a2018-09-26 05:27:56 +0200611 efi_add_handle(&netobj->header);
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200612
613 /* Fill in object data */
Heinrich Schuchardtd39646a2018-09-26 05:27:56 +0200614 r = efi_add_protocol(&netobj->header, &efi_net_guid,
Heinrich Schuchardt84d14562017-11-26 14:05:13 +0100615 &netobj->net);
616 if (r != EFI_SUCCESS)
Heinrich Schuchardt075d4252018-03-03 15:28:56 +0100617 goto failure_to_add_protocol;
Heinrich Schuchardtd39646a2018-09-26 05:27:56 +0200618 r = efi_add_protocol(&netobj->header, &efi_guid_device_path,
Heinrich Schuchardt84d14562017-11-26 14:05:13 +0100619 efi_dp_from_eth());
620 if (r != EFI_SUCCESS)
Heinrich Schuchardt075d4252018-03-03 15:28:56 +0100621 goto failure_to_add_protocol;
Heinrich Schuchardtd39646a2018-09-26 05:27:56 +0200622 r = efi_add_protocol(&netobj->header, &efi_pxe_guid,
Heinrich Schuchardt84d14562017-11-26 14:05:13 +0100623 &netobj->pxe);
624 if (r != EFI_SUCCESS)
Heinrich Schuchardt075d4252018-03-03 15:28:56 +0100625 goto failure_to_add_protocol;
Heinrich Schuchardtbdecf972017-10-05 16:35:57 +0200626 netobj->net.revision = EFI_SIMPLE_NETWORK_PROTOCOL_REVISION;
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200627 netobj->net.start = efi_net_start;
628 netobj->net.stop = efi_net_stop;
629 netobj->net.initialize = efi_net_initialize;
630 netobj->net.reset = efi_net_reset;
631 netobj->net.shutdown = efi_net_shutdown;
632 netobj->net.receive_filters = efi_net_receive_filters;
633 netobj->net.station_address = efi_net_station_address;
634 netobj->net.statistics = efi_net_statistics;
635 netobj->net.mcastiptomac = efi_net_mcastiptomac;
636 netobj->net.nvdata = efi_net_nvdata;
637 netobj->net.get_status = efi_net_get_status;
638 netobj->net.transmit = efi_net_transmit;
639 netobj->net.receive = efi_net_receive;
640 netobj->net.mode = &netobj->net_mode;
641 netobj->net_mode.state = EFI_NETWORK_STARTED;
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200642 memcpy(netobj->net_mode.current_address.mac_addr, eth_get_ethaddr(), 6);
Heinrich Schuchardt5d4a5ea2017-10-05 16:35:58 +0200643 netobj->net_mode.hwaddr_size = ARP_HLEN;
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200644 netobj->net_mode.max_packet_size = PKTSIZE;
Andrew Thomasf25ddca2018-06-21 16:21:01 -0700645 netobj->net_mode.if_type = ARP_ETHER;
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200646
647 netobj->pxe.mode = &netobj->pxe_mode;
648 if (dhcp_ack)
649 netobj->pxe_mode.dhcp_ack = *dhcp_ack;
650
Heinrich Schuchardta0549ef2017-10-05 16:36:00 +0200651 /*
Heinrich Schuchardte5c21602017-10-05 16:36:01 +0200652 * Create WaitForPacket event.
653 */
654 r = efi_create_event(EVT_NOTIFY_WAIT, TPL_CALLBACK,
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100655 efi_network_timer_notify, NULL, NULL,
Heinrich Schuchardte5c21602017-10-05 16:36:01 +0200656 &wait_for_packet);
657 if (r != EFI_SUCCESS) {
658 printf("ERROR: Failed to register network event\n");
659 return r;
660 }
661 netobj->net.wait_for_packet = wait_for_packet;
662 /*
Heinrich Schuchardta0549ef2017-10-05 16:36:00 +0200663 * Create a timer event.
664 *
665 * The notification function is used to check if a new network packet
666 * has been received.
Heinrich Schuchardtee3db4f2018-03-24 18:40:22 +0100667 *
668 * iPXE is running at TPL_CALLBACK most of the time. Use a higher TPL.
Heinrich Schuchardta0549ef2017-10-05 16:36:00 +0200669 */
Heinrich Schuchardtee3db4f2018-03-24 18:40:22 +0100670 r = efi_create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_NOTIFY,
Heinrich Schuchardt41b05872018-12-01 00:16:33 +0100671 efi_network_timer_notify, &netobj->net, NULL,
Heinrich Schuchardta0549ef2017-10-05 16:36:00 +0200672 &network_timer_event);
673 if (r != EFI_SUCCESS) {
674 printf("ERROR: Failed to register network event\n");
675 return r;
676 }
Heinrich Schuchardte1fec152018-10-18 21:51:38 +0200677 /* Network is time critical, create event in every timer cycle */
Heinrich Schuchardta0549ef2017-10-05 16:36:00 +0200678 r = efi_set_timer(network_timer_event, EFI_TIMER_PERIODIC, 0);
679 if (r != EFI_SUCCESS) {
680 printf("ERROR: Failed to set network timer\n");
681 return r;
682 }
683
Heinrich Schuchardt075d4252018-03-03 15:28:56 +0100684 return EFI_SUCCESS;
685failure_to_add_protocol:
686 printf("ERROR: Failure to add protocol\n");
687 return r;
Heinrich Schuchardt622fe622018-12-01 00:16:32 +0100688out_of_resources:
689 free(netobj);
690 /* free(transmit_buffer) not needed yet */
691 printf("ERROR: Out of memory\n");
692 return EFI_OUT_OF_RESOURCES;
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200693}