blob: 825e064f9aa83f99dd051c72ae836d21aa11ee9d [file] [log] [blame]
Tom Rini70df9d62018-05-07 17:02:21 -04001// SPDX-License-Identifier: GPL-2.0+
Alexander Graf94c4b992016-05-06 21:01:01 +02002/*
3 * EFI application network access support
4 *
5 * Copyright (c) 2016 Alexander Graf
Alexander Graf94c4b992016-05-06 21:01:01 +02006 */
7
8#include <common.h>
9#include <efi_loader.h>
Alexander Graf94c4b992016-05-06 21:01:01 +020010#include <malloc.h>
11
Heinrich Schuchardt788ad412019-04-20 07:39:11 +020012static const efi_guid_t efi_net_guid = EFI_SIMPLE_NETWORK_PROTOCOL_GUID;
Heinrich Schuchardt3127e7e2019-08-06 08:13:33 +020013static const efi_guid_t efi_pxe_base_code_protocol_guid =
14 EFI_PXE_BASE_CODE_PROTOCOL_GUID;
Alexander Graf94c4b992016-05-06 21:01:01 +020015static struct efi_pxe_packet *dhcp_ack;
16static bool new_rx_packet;
17static void *new_tx_packet;
Heinrich Schuchardteb07c282018-12-01 00:16:32 +010018static void *transmit_buffer;
19
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +020020/*
21 * The notification function of this event is called in every timer cycle
22 * to check if a new network packet has been received.
23 */
24static struct efi_event *network_timer_event;
Heinrich Schuchardt0c153c22017-10-05 16:36:01 +020025/*
26 * This event is signaled when a packet has been received.
27 */
28static struct efi_event *wait_for_packet;
Alexander Graf94c4b992016-05-06 21:01:01 +020029
Heinrich Schuchardt72928722018-09-26 05:27:56 +020030/**
31 * struct efi_net_obj - EFI object representing a network interface
32 *
33 * @header: EFI object header
34 * @net: simple network protocol interface
35 * @net_mode: status of the network interface
36 * @pxe: PXE base code protocol interface
37 * @pxe_mode: status of the PXE base code protocol
38 */
Alexander Graf94c4b992016-05-06 21:01:01 +020039struct efi_net_obj {
Heinrich Schuchardt72928722018-09-26 05:27:56 +020040 struct efi_object header;
Alexander Graf94c4b992016-05-06 21:01:01 +020041 struct efi_simple_network net;
42 struct efi_simple_network_mode net_mode;
Heinrich Schuchardt3127e7e2019-08-06 08:13:33 +020043 struct efi_pxe_base_code_protocol pxe;
Alexander Graf94c4b992016-05-06 21:01:01 +020044 struct efi_pxe_mode pxe_mode;
45};
46
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +010047/*
48 * efi_net_start() - start the network interface
49 *
50 * This function implements the Start service of the
51 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
52 * (UEFI) specification for details.
53 *
54 * @this: pointer to the protocol instance
55 * Return: status code
56 */
Alexander Graf94c4b992016-05-06 21:01:01 +020057static efi_status_t EFIAPI efi_net_start(struct efi_simple_network *this)
58{
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +010059 efi_status_t ret = EFI_SUCCESS;
60
Alexander Graf94c4b992016-05-06 21:01:01 +020061 EFI_ENTRY("%p", this);
62
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +010063 /* Check parameters */
64 if (!this) {
65 ret = EFI_INVALID_PARAMETER;
66 goto out;
67 }
68
69 if (this->mode->state != EFI_NETWORK_STOPPED)
70 ret = EFI_ALREADY_STARTED;
71 else
72 this->mode->state = EFI_NETWORK_STARTED;
73out:
74 return EFI_EXIT(ret);
Alexander Graf94c4b992016-05-06 21:01:01 +020075}
76
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +010077/*
78 * efi_net_stop() - stop the network interface
79 *
80 * This function implements the Stop service of the
81 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
82 * (UEFI) specification for details.
83 *
84 * @this: pointer to the protocol instance
85 * Return: status code
86 */
Alexander Graf94c4b992016-05-06 21:01:01 +020087static efi_status_t EFIAPI efi_net_stop(struct efi_simple_network *this)
88{
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +010089 efi_status_t ret = EFI_SUCCESS;
90
Alexander Graf94c4b992016-05-06 21:01:01 +020091 EFI_ENTRY("%p", this);
92
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +010093 /* Check parameters */
94 if (!this) {
95 ret = EFI_INVALID_PARAMETER;
96 goto out;
97 }
98
99 if (this->mode->state == EFI_NETWORK_STOPPED)
100 ret = EFI_NOT_STARTED;
101 else
102 this->mode->state = EFI_NETWORK_STOPPED;
103out:
104 return EFI_EXIT(ret);
Alexander Graf94c4b992016-05-06 21:01:01 +0200105}
106
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200107/*
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100108 * efi_net_initialize() - initialize the network interface
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200109 *
110 * This function implements the Initialize service of the
111 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
112 * (UEFI) specification for details.
113 *
114 * @this: pointer to the protocol instance
115 * @extra_rx: extra receive buffer to be allocated
116 * @extra_tx: extra transmit buffer to be allocated
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100117 * Return: status code
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200118 */
Alexander Graf94c4b992016-05-06 21:01:01 +0200119static efi_status_t EFIAPI efi_net_initialize(struct efi_simple_network *this,
120 ulong extra_rx, ulong extra_tx)
121{
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200122 int ret;
123 efi_status_t r = EFI_SUCCESS;
124
Alexander Graf94c4b992016-05-06 21:01:01 +0200125 EFI_ENTRY("%p, %lx, %lx", this, extra_rx, extra_tx);
126
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100127 /* Check parameters */
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200128 if (!this) {
129 r = EFI_INVALID_PARAMETER;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100130 goto out;
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200131 }
132
133 /* Setup packet buffers */
134 net_init();
135 /* Disable hardware and put it into the reset state */
136 eth_halt();
137 /* Set current device according to environment variables */
138 eth_set_current();
139 /* Get hardware ready for send and receive operations */
140 ret = eth_init();
141 if (ret < 0) {
142 eth_halt();
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100143 this->mode->state = EFI_NETWORK_STOPPED;
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200144 r = EFI_DEVICE_ERROR;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100145 goto out;
146 } else {
147 this->mode->state = EFI_NETWORK_INITIALIZED;
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200148 }
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100149out:
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200150 return EFI_EXIT(r);
Alexander Graf94c4b992016-05-06 21:01:01 +0200151}
152
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100153/*
154 * efi_net_reset() - reinitialize the network interface
155 *
156 * This function implements the Reset service of the
157 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
158 * (UEFI) specification for details.
159 *
160 * @this: pointer to the protocol instance
161 * @extended_verification: execute exhaustive verification
162 * Return: status code
163 */
Alexander Graf94c4b992016-05-06 21:01:01 +0200164static efi_status_t EFIAPI efi_net_reset(struct efi_simple_network *this,
165 int extended_verification)
166{
167 EFI_ENTRY("%p, %x", this, extended_verification);
168
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100169 return EFI_EXIT(EFI_CALL(efi_net_initialize(this, 0, 0)));
Alexander Graf94c4b992016-05-06 21:01:01 +0200170}
171
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100172/*
173 * efi_net_shutdown() - shut down the network interface
174 *
175 * This function implements the Shutdown service of the
176 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
177 * (UEFI) specification for details.
178 *
179 * @this: pointer to the protocol instance
180 * Return: status code
181 */
Alexander Graf94c4b992016-05-06 21:01:01 +0200182static efi_status_t EFIAPI efi_net_shutdown(struct efi_simple_network *this)
183{
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100184 efi_status_t ret = EFI_SUCCESS;
185
Alexander Graf94c4b992016-05-06 21:01:01 +0200186 EFI_ENTRY("%p", this);
187
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100188 /* Check parameters */
189 if (!this) {
190 ret = EFI_INVALID_PARAMETER;
191 goto out;
192 }
193
194 eth_halt();
195 this->mode->state = EFI_NETWORK_STOPPED;
196
197out:
198 return EFI_EXIT(ret);
Alexander Graf94c4b992016-05-06 21:01:01 +0200199}
200
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100201/*
202 * efi_net_receive_filters() - mange multicast receive filters
203 *
204 * This function implements the ReceiveFilters service of the
205 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
206 * (UEFI) specification for details.
207 *
208 * @this: pointer to the protocol instance
209 * @enable: bit mask of receive filters to enable
210 * @disable: bit mask of receive filters to disable
211 * @reset_mcast_filter: true resets contents of the filters
212 * @mcast_filter_count: number of hardware MAC addresses in the new filters list
213 * @mcast_filter: list of new filters
214 * Return: status code
215 */
216static efi_status_t EFIAPI efi_net_receive_filters
217 (struct efi_simple_network *this, u32 enable, u32 disable,
218 int reset_mcast_filter, ulong mcast_filter_count,
219 struct efi_mac_address *mcast_filter)
Alexander Graf94c4b992016-05-06 21:01:01 +0200220{
221 EFI_ENTRY("%p, %x, %x, %x, %lx, %p", this, enable, disable,
222 reset_mcast_filter, mcast_filter_count, mcast_filter);
223
Heinrich Schuchardta50b5c62017-10-05 16:35:59 +0200224 return EFI_EXIT(EFI_UNSUPPORTED);
Alexander Graf94c4b992016-05-06 21:01:01 +0200225}
226
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100227/*
228 * efi_net_station_address() - set the hardware MAC address
229 *
230 * This function implements the StationAddress service of the
231 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
232 * (UEFI) specification for details.
233 *
234 * @this: pointer to the protocol instance
235 * @reset: if true reset the address to default
236 * @new_mac: new MAC address
237 * Return: status code
238 */
239static efi_status_t EFIAPI efi_net_station_address
240 (struct efi_simple_network *this, int reset,
241 struct efi_mac_address *new_mac)
Alexander Graf94c4b992016-05-06 21:01:01 +0200242{
243 EFI_ENTRY("%p, %x, %p", this, reset, new_mac);
244
Heinrich Schuchardta50b5c62017-10-05 16:35:59 +0200245 return EFI_EXIT(EFI_UNSUPPORTED);
Alexander Graf94c4b992016-05-06 21:01:01 +0200246}
247
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100248/*
249 * efi_net_statistics() - reset or collect statistics of the network interface
250 *
251 * This function implements the Statistics service of the
252 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
253 * (UEFI) specification for details.
254 *
255 * @this: pointer to the protocol instance
256 * @reset: if true, the statistics are reset
257 * @stat_size: size of the statistics table
258 * @stat_table: table to receive the statistics
259 * Return: status code
260 */
Alexander Graf94c4b992016-05-06 21:01:01 +0200261static efi_status_t EFIAPI efi_net_statistics(struct efi_simple_network *this,
262 int reset, ulong *stat_size,
263 void *stat_table)
264{
265 EFI_ENTRY("%p, %x, %p, %p", this, reset, stat_size, stat_table);
266
Heinrich Schuchardta50b5c62017-10-05 16:35:59 +0200267 return EFI_EXIT(EFI_UNSUPPORTED);
Alexander Graf94c4b992016-05-06 21:01:01 +0200268}
269
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100270/*
271 * efi_net_mcastiptomac() - translate multicast IP address to MAC address
272 *
273 * This function implements the Statistics service of the
274 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
275 * (UEFI) specification for details.
276 *
277 * @this: pointer to the protocol instance
278 * @ipv6: true if the IP address is an IPv6 address
279 * @ip: IP address
280 * @mac: MAC address
281 * Return: status code
282 */
Alexander Graf94c4b992016-05-06 21:01:01 +0200283static efi_status_t EFIAPI efi_net_mcastiptomac(struct efi_simple_network *this,
284 int ipv6,
285 struct efi_ip_address *ip,
286 struct efi_mac_address *mac)
287{
288 EFI_ENTRY("%p, %x, %p, %p", this, ipv6, ip, mac);
289
290 return EFI_EXIT(EFI_INVALID_PARAMETER);
291}
292
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100293/**
294 * efi_net_nvdata() - read or write NVRAM
295 *
296 * This function implements the GetStatus service of the Simple Network
297 * Protocol. See the UEFI spec for details.
298 *
299 * @this: the instance of the Simple Network Protocol
300 * @readwrite: true for read, false for write
301 * @offset: offset in NVRAM
302 * @buffer_size: size of buffer
303 * @buffer: buffer
304 * Return: status code
305 */
Alexander Graf94c4b992016-05-06 21:01:01 +0200306static efi_status_t EFIAPI efi_net_nvdata(struct efi_simple_network *this,
307 int read_write, ulong offset,
308 ulong buffer_size, char *buffer)
309{
310 EFI_ENTRY("%p, %x, %lx, %lx, %p", this, read_write, offset, buffer_size,
311 buffer);
312
Heinrich Schuchardta50b5c62017-10-05 16:35:59 +0200313 return EFI_EXIT(EFI_UNSUPPORTED);
Alexander Graf94c4b992016-05-06 21:01:01 +0200314}
315
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100316/**
317 * efi_net_get_status() - get interrupt status
318 *
319 * This function implements the GetStatus service of the Simple Network
320 * Protocol. See the UEFI spec for details.
321 *
322 * @this: the instance of the Simple Network Protocol
323 * @int_status: interface status
324 * @txbuf: transmission buffer
325 */
Alexander Graf94c4b992016-05-06 21:01:01 +0200326static efi_status_t EFIAPI efi_net_get_status(struct efi_simple_network *this,
327 u32 *int_status, void **txbuf)
328{
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100329 efi_status_t ret = EFI_SUCCESS;
330
Alexander Graf94c4b992016-05-06 21:01:01 +0200331 EFI_ENTRY("%p, %p, %p", this, int_status, txbuf);
332
Heinrich Schuchardte371c5f2017-10-05 16:36:02 +0200333 efi_timer_check();
334
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100335 /* Check parameters */
336 if (!this) {
337 ret = EFI_INVALID_PARAMETER;
338 goto out;
339 }
340
341 switch (this->mode->state) {
342 case EFI_NETWORK_STOPPED:
343 ret = EFI_NOT_STARTED;
344 goto out;
345 case EFI_NETWORK_STARTED:
346 ret = EFI_DEVICE_ERROR;
347 goto out;
348 default:
349 break;
350 }
351
Heinrich Schuchardte371c5f2017-10-05 16:36:02 +0200352 if (int_status) {
353 /* We send packets synchronously, so nothing is outstanding */
354 *int_status = EFI_SIMPLE_NETWORK_TRANSMIT_INTERRUPT;
355 if (new_rx_packet)
356 *int_status |= EFI_SIMPLE_NETWORK_RECEIVE_INTERRUPT;
357 }
Alexander Graf94c4b992016-05-06 21:01:01 +0200358 if (txbuf)
359 *txbuf = new_tx_packet;
360
361 new_tx_packet = NULL;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100362out:
363 return EFI_EXIT(ret);
Alexander Graf94c4b992016-05-06 21:01:01 +0200364}
365
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100366/**
367 * efi_net_transmit() - transmit a packet
368 *
369 * This function implements the Transmit service of the Simple Network Protocol.
370 * See the UEFI spec for details.
371 *
372 * @this: the instance of the Simple Network Protocol
373 * @header_size: size of the media header
374 * @buffer_size: size of the buffer to receive the packet
375 * @buffer: buffer to receive the packet
376 * @src_addr: source hardware MAC address
377 * @dest_addr: destination hardware MAC address
378 * @protocol: type of header to build
379 * Return: status code
380 */
381static efi_status_t EFIAPI efi_net_transmit
382 (struct efi_simple_network *this, size_t header_size,
383 size_t buffer_size, void *buffer,
384 struct efi_mac_address *src_addr,
385 struct efi_mac_address *dest_addr, u16 *protocol)
Alexander Graf94c4b992016-05-06 21:01:01 +0200386{
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100387 efi_status_t ret = EFI_SUCCESS;
388
Heinrich Schuchardtf66b2e32017-10-05 16:36:03 +0200389 EFI_ENTRY("%p, %lu, %lu, %p, %p, %p, %p", this,
390 (unsigned long)header_size, (unsigned long)buffer_size,
391 buffer, src_addr, dest_addr, protocol);
Alexander Graf94c4b992016-05-06 21:01:01 +0200392
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200393 efi_timer_check();
394
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100395 /* Check parameters */
Heinrich Schuchardtf286c2c2019-05-15 23:27:43 +0200396 if (!this || !buffer) {
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100397 ret = EFI_INVALID_PARAMETER;
398 goto out;
399 }
400
401 /* We do not support jumbo packets */
402 if (buffer_size > PKTSIZE_ALIGN) {
403 ret = EFI_INVALID_PARAMETER;
404 goto out;
405 }
406
Alexander Graf94c4b992016-05-06 21:01:01 +0200407 if (header_size) {
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100408 /*
409 * TODO: We would need to create the header
410 * if header_size != 0
411 */
Heinrich Schuchardtf286c2c2019-05-15 23:27:43 +0200412 ret = EFI_UNSUPPORTED;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100413 goto out;
Alexander Graf94c4b992016-05-06 21:01:01 +0200414 }
415
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100416 switch (this->mode->state) {
417 case EFI_NETWORK_STOPPED:
418 ret = EFI_NOT_STARTED;
419 goto out;
420 case EFI_NETWORK_STARTED:
421 ret = EFI_DEVICE_ERROR;
422 goto out;
423 default:
424 break;
425 }
426
427 /* Ethernet packets always fit, just bounce */
Heinrich Schuchardteb07c282018-12-01 00:16:32 +0100428 memcpy(transmit_buffer, buffer, buffer_size);
429 net_send_packet(transmit_buffer, buffer_size);
Alexander Graf0b12b862016-09-06 14:26:27 +0200430
Alexander Graf94c4b992016-05-06 21:01:01 +0200431 new_tx_packet = buffer;
432
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100433out:
434 return EFI_EXIT(ret);
Alexander Graf94c4b992016-05-06 21:01:01 +0200435}
436
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100437/**
438 * efi_net_receive() - receive a packet from a network interface
Heinrich Schuchardtf66b2e32017-10-05 16:36:03 +0200439 *
440 * This function implements the Receive service of the Simple Network Protocol.
441 * See the UEFI spec for details.
442 *
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100443 * @this: the instance of the Simple Network Protocol
444 * @header_size: size of the media header
445 * @buffer_size: size of the buffer to receive the packet
446 * @buffer: buffer to receive the packet
447 * @src_addr: source MAC address
448 * @dest_addr: destination MAC address
449 * @protocol: protocol
450 * Return: status code
Heinrich Schuchardtf66b2e32017-10-05 16:36:03 +0200451 */
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100452static efi_status_t EFIAPI efi_net_receive
453 (struct efi_simple_network *this, size_t *header_size,
454 size_t *buffer_size, void *buffer,
455 struct efi_mac_address *src_addr,
456 struct efi_mac_address *dest_addr, u16 *protocol)
Alexander Graf94c4b992016-05-06 21:01:01 +0200457{
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100458 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardtaa0d1972017-10-05 16:36:04 +0200459 struct ethernet_hdr *eth_hdr;
460 size_t hdr_size = sizeof(struct ethernet_hdr);
461 u16 protlen;
462
Alexander Graf94c4b992016-05-06 21:01:01 +0200463 EFI_ENTRY("%p, %p, %p, %p, %p, %p, %p", this, header_size,
464 buffer_size, buffer, src_addr, dest_addr, protocol);
465
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100466 /* Execute events */
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200467 efi_timer_check();
Alexander Graf94c4b992016-05-06 21:01:01 +0200468
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100469 /* Check parameters */
Heinrich Schuchardtf286c2c2019-05-15 23:27:43 +0200470 if (!this || !buffer || !buffer_size) {
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100471 ret = EFI_INVALID_PARAMETER;
472 goto out;
473 }
474
475 switch (this->mode->state) {
476 case EFI_NETWORK_STOPPED:
477 ret = EFI_NOT_STARTED;
478 goto out;
479 case EFI_NETWORK_STARTED:
480 ret = EFI_DEVICE_ERROR;
481 goto out;
482 default:
483 break;
484 }
485
486 if (!new_rx_packet) {
487 ret = EFI_NOT_READY;
488 goto out;
489 }
Heinrich Schuchardtaa0d1972017-10-05 16:36:04 +0200490 /* Check that we at least received an Ethernet header */
491 if (net_rx_packet_len < sizeof(struct ethernet_hdr)) {
492 new_rx_packet = false;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100493 ret = EFI_NOT_READY;
494 goto out;
Heinrich Schuchardtaa0d1972017-10-05 16:36:04 +0200495 }
496 /* Fill export parameters */
497 eth_hdr = (struct ethernet_hdr *)net_rx_packet;
498 protlen = ntohs(eth_hdr->et_protlen);
499 if (protlen == 0x8100) {
500 hdr_size += 4;
501 protlen = ntohs(*(u16 *)&net_rx_packet[hdr_size - 2]);
502 }
503 if (header_size)
504 *header_size = hdr_size;
505 if (dest_addr)
506 memcpy(dest_addr, eth_hdr->et_dest, ARP_HLEN);
507 if (src_addr)
508 memcpy(src_addr, eth_hdr->et_src, ARP_HLEN);
509 if (protocol)
510 *protocol = protlen;
Alexander Graf94c4b992016-05-06 21:01:01 +0200511 if (*buffer_size < net_rx_packet_len) {
Heinrich Schuchardt5e96f422018-10-18 21:51:38 +0200512 /* Packet doesn't fit, try again with bigger buffer */
Alexander Graf94c4b992016-05-06 21:01:01 +0200513 *buffer_size = net_rx_packet_len;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100514 ret = EFI_BUFFER_TOO_SMALL;
515 goto out;
Alexander Graf94c4b992016-05-06 21:01:01 +0200516 }
Heinrich Schuchardtaa0d1972017-10-05 16:36:04 +0200517 /* Copy packet */
Alexander Graf94c4b992016-05-06 21:01:01 +0200518 memcpy(buffer, net_rx_packet, net_rx_packet_len);
519 *buffer_size = net_rx_packet_len;
520 new_rx_packet = false;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100521out:
522 return EFI_EXIT(ret);
Alexander Graf94c4b992016-05-06 21:01:01 +0200523}
524
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100525/**
526 * efi_net_set_dhcp_ack() - take note of a selected DHCP IP address
527 *
528 * This function is called by dhcp_handler().
529 */
Alexander Graf94c4b992016-05-06 21:01:01 +0200530void efi_net_set_dhcp_ack(void *pkt, int len)
531{
532 int maxsize = sizeof(*dhcp_ack);
533
534 if (!dhcp_ack)
535 dhcp_ack = malloc(maxsize);
536
537 memcpy(dhcp_ack, pkt, min(len, maxsize));
538}
539
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100540/**
541 * efi_net_push() - callback for received network packet
542 *
543 * This function is called when a network packet is received by eth_rx().
544 *
545 * @pkt: network packet
546 * @len: length
547 */
548static void efi_net_push(void *pkt, int len)
549{
550 new_rx_packet = true;
551 wait_for_packet->is_signaled = true;
552}
553
554/**
555 * efi_network_timer_notify() - check if a new network packet has been received
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200556 *
557 * This notification function is called in every timer cycle.
558 *
559 * @event the event for which this notification function is registered
560 * @context event context - not used in this function
561 */
562static void EFIAPI efi_network_timer_notify(struct efi_event *event,
563 void *context)
564{
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100565 struct efi_simple_network *this = (struct efi_simple_network *)context;
566
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200567 EFI_ENTRY("%p, %p", event, context);
568
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100569 /*
570 * Some network drivers do not support calling eth_rx() before
571 * initialization.
572 */
573 if (!this || this->mode->state != EFI_NETWORK_INITIALIZED)
574 goto out;
575
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200576 if (!new_rx_packet) {
577 push_packet = efi_net_push;
578 eth_rx();
579 push_packet = NULL;
580 }
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100581out:
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200582 EFI_EXIT(EFI_SUCCESS);
583}
584
Heinrich Schuchardt3127e7e2019-08-06 08:13:33 +0200585static efi_status_t EFIAPI efi_pxe_base_code_start(
586 struct efi_pxe_base_code_protocol *this,
587 u8 use_ipv6)
588{
589 return EFI_UNSUPPORTED;
590}
591
592static efi_status_t EFIAPI efi_pxe_base_code_stop(
593 struct efi_pxe_base_code_protocol *this)
594{
595 return EFI_UNSUPPORTED;
596}
597
598static efi_status_t EFIAPI efi_pxe_base_code_dhcp(
599 struct efi_pxe_base_code_protocol *this,
600 u8 sort_offers)
601{
602 return EFI_UNSUPPORTED;
603}
604
605static efi_status_t EFIAPI efi_pxe_base_code_discover(
606 struct efi_pxe_base_code_protocol *this,
607 u16 type, u16 *layer, u8 bis,
608 struct efi_pxe_base_code_discover_info *info)
609{
610 return EFI_UNSUPPORTED;
611}
612
613static efi_status_t EFIAPI efi_pxe_base_code_mtftp(
614 struct efi_pxe_base_code_protocol *this,
615 u32 operation, void *buffer_ptr,
616 u8 overwrite, efi_uintn_t *buffer_size,
617 struct efi_ip_address server_ip, char *filename,
618 struct efi_pxe_base_code_mtftp_info *info,
619 u8 dont_use_buffer)
620{
621 return EFI_UNSUPPORTED;
622}
623
624static efi_status_t EFIAPI efi_pxe_base_code_udp_write(
625 struct efi_pxe_base_code_protocol *this,
626 u16 op_flags, struct efi_ip_address *dest_ip,
627 u16 *dest_port,
628 struct efi_ip_address *gateway_ip,
629 struct efi_ip_address *src_ip, u16 *src_port,
630 efi_uintn_t *header_size, void *header_ptr,
631 efi_uintn_t *buffer_size, void *buffer_ptr)
632{
633 return EFI_UNSUPPORTED;
634}
635
636static efi_status_t EFIAPI efi_pxe_base_code_udp_read(
637 struct efi_pxe_base_code_protocol *this,
638 u16 op_flags, struct efi_ip_address *dest_ip,
639 u16 *dest_port, struct efi_ip_address *src_ip,
640 u16 *src_port, efi_uintn_t *header_size,
641 void *header_ptr, efi_uintn_t *buffer_size,
642 void *buffer_ptr)
643{
644 return EFI_UNSUPPORTED;
645}
646
647static efi_status_t EFIAPI efi_pxe_base_code_set_ip_filter(
648 struct efi_pxe_base_code_protocol *this,
649 struct efi_pxe_base_code_filter *new_filter)
650{
651 return EFI_UNSUPPORTED;
652}
653
654static efi_status_t EFIAPI efi_pxe_base_code_arp(
655 struct efi_pxe_base_code_protocol *this,
656 struct efi_ip_address *ip_addr,
657 struct efi_mac_address *mac_addr)
658{
659 return EFI_UNSUPPORTED;
660}
661
662static efi_status_t EFIAPI efi_pxe_base_code_set_parameters(
663 struct efi_pxe_base_code_protocol *this,
664 u8 *new_auto_arp, u8 *new_send_guid,
665 u8 *new_ttl, u8 *new_tos,
666 u8 *new_make_callback)
667{
668 return EFI_UNSUPPORTED;
669}
670
671static efi_status_t EFIAPI efi_pxe_base_code_set_station_ip(
672 struct efi_pxe_base_code_protocol *this,
673 struct efi_ip_address *new_station_ip,
674 struct efi_ip_address *new_subnet_mask)
675{
676 return EFI_UNSUPPORTED;
677}
678
679static efi_status_t EFIAPI efi_pxe_base_code_set_packets(
680 struct efi_pxe_base_code_protocol *this,
681 u8 *new_dhcp_discover_valid,
682 u8 *new_dhcp_ack_received,
683 u8 *new_proxy_offer_received,
684 u8 *new_pxe_discover_valid,
685 u8 *new_pxe_reply_received,
686 u8 *new_pxe_bis_reply_received,
687 EFI_PXE_BASE_CODE_PACKET *new_dchp_discover,
688 EFI_PXE_BASE_CODE_PACKET *new_dhcp_acc,
689 EFI_PXE_BASE_CODE_PACKET *new_proxy_offer,
690 EFI_PXE_BASE_CODE_PACKET *new_pxe_discover,
691 EFI_PXE_BASE_CODE_PACKET *new_pxe_reply,
692 EFI_PXE_BASE_CODE_PACKET *new_pxe_bis_reply)
693{
694 return EFI_UNSUPPORTED;
695}
696
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100697/**
698 * efi_net_register() - register the simple network protocol
699 *
700 * This gets called from do_bootefi_exec().
701 */
Heinrich Schuchardt6f5c73f2018-03-03 15:28:56 +0100702efi_status_t efi_net_register(void)
Alexander Graf94c4b992016-05-06 21:01:01 +0200703{
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100704 struct efi_net_obj *netobj = NULL;
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200705 efi_status_t r;
Alexander Graf94c4b992016-05-06 21:01:01 +0200706
707 if (!eth_get_dev()) {
Heinrich Schuchardt5e96f422018-10-18 21:51:38 +0200708 /* No network device active, don't expose any */
Heinrich Schuchardt6f5c73f2018-03-03 15:28:56 +0100709 return EFI_SUCCESS;
Alexander Graf94c4b992016-05-06 21:01:01 +0200710 }
711
Heinrich Schuchardt5e96f422018-10-18 21:51:38 +0200712 /* We only expose the "active" network device, so one is enough */
Alexander Graf94c4b992016-05-06 21:01:01 +0200713 netobj = calloc(1, sizeof(*netobj));
Heinrich Schuchardteb07c282018-12-01 00:16:32 +0100714 if (!netobj)
715 goto out_of_resources;
716
717 /* Allocate an aligned transmit buffer */
718 transmit_buffer = calloc(1, PKTSIZE_ALIGN + PKTALIGN);
719 if (!transmit_buffer)
720 goto out_of_resources;
721 transmit_buffer = (void *)ALIGN((uintptr_t)transmit_buffer, PKTALIGN);
Heinrich Schuchardt23b45b62017-11-26 14:05:13 +0100722
723 /* Hook net up to the device list */
Heinrich Schuchardt72928722018-09-26 05:27:56 +0200724 efi_add_handle(&netobj->header);
Alexander Graf94c4b992016-05-06 21:01:01 +0200725
726 /* Fill in object data */
Heinrich Schuchardt72928722018-09-26 05:27:56 +0200727 r = efi_add_protocol(&netobj->header, &efi_net_guid,
Heinrich Schuchardt23b45b62017-11-26 14:05:13 +0100728 &netobj->net);
729 if (r != EFI_SUCCESS)
Heinrich Schuchardt6f5c73f2018-03-03 15:28:56 +0100730 goto failure_to_add_protocol;
Heinrich Schuchardt72928722018-09-26 05:27:56 +0200731 r = efi_add_protocol(&netobj->header, &efi_guid_device_path,
Heinrich Schuchardt23b45b62017-11-26 14:05:13 +0100732 efi_dp_from_eth());
733 if (r != EFI_SUCCESS)
Heinrich Schuchardt6f5c73f2018-03-03 15:28:56 +0100734 goto failure_to_add_protocol;
Heinrich Schuchardt3127e7e2019-08-06 08:13:33 +0200735 r = efi_add_protocol(&netobj->header, &efi_pxe_base_code_protocol_guid,
Heinrich Schuchardt23b45b62017-11-26 14:05:13 +0100736 &netobj->pxe);
737 if (r != EFI_SUCCESS)
Heinrich Schuchardt6f5c73f2018-03-03 15:28:56 +0100738 goto failure_to_add_protocol;
Heinrich Schuchardtd54396a2017-10-05 16:35:57 +0200739 netobj->net.revision = EFI_SIMPLE_NETWORK_PROTOCOL_REVISION;
Alexander Graf94c4b992016-05-06 21:01:01 +0200740 netobj->net.start = efi_net_start;
741 netobj->net.stop = efi_net_stop;
742 netobj->net.initialize = efi_net_initialize;
743 netobj->net.reset = efi_net_reset;
744 netobj->net.shutdown = efi_net_shutdown;
745 netobj->net.receive_filters = efi_net_receive_filters;
746 netobj->net.station_address = efi_net_station_address;
747 netobj->net.statistics = efi_net_statistics;
748 netobj->net.mcastiptomac = efi_net_mcastiptomac;
749 netobj->net.nvdata = efi_net_nvdata;
750 netobj->net.get_status = efi_net_get_status;
751 netobj->net.transmit = efi_net_transmit;
752 netobj->net.receive = efi_net_receive;
753 netobj->net.mode = &netobj->net_mode;
754 netobj->net_mode.state = EFI_NETWORK_STARTED;
Alexander Graf94c4b992016-05-06 21:01:01 +0200755 memcpy(netobj->net_mode.current_address.mac_addr, eth_get_ethaddr(), 6);
Heinrich Schuchardtd1cc6cb2017-10-05 16:35:58 +0200756 netobj->net_mode.hwaddr_size = ARP_HLEN;
Alexander Graf94c4b992016-05-06 21:01:01 +0200757 netobj->net_mode.max_packet_size = PKTSIZE;
Andrew Thomas27df0a72018-06-21 16:21:01 -0700758 netobj->net_mode.if_type = ARP_ETHER;
Alexander Graf94c4b992016-05-06 21:01:01 +0200759
Heinrich Schuchardt3127e7e2019-08-06 08:13:33 +0200760 netobj->pxe.revision = EFI_PXE_BASE_CODE_PROTOCOL_REVISION;
761 netobj->pxe.start = efi_pxe_base_code_start;
762 netobj->pxe.stop = efi_pxe_base_code_stop;
763 netobj->pxe.dhcp = efi_pxe_base_code_dhcp;
764 netobj->pxe.discover = efi_pxe_base_code_discover;
765 netobj->pxe.mtftp = efi_pxe_base_code_mtftp;
766 netobj->pxe.udp_write = efi_pxe_base_code_udp_write;
767 netobj->pxe.udp_read = efi_pxe_base_code_udp_read;
768 netobj->pxe.set_ip_filter = efi_pxe_base_code_set_ip_filter;
769 netobj->pxe.arp = efi_pxe_base_code_arp;
770 netobj->pxe.set_parameters = efi_pxe_base_code_set_parameters;
771 netobj->pxe.set_station_ip = efi_pxe_base_code_set_station_ip;
772 netobj->pxe.set_packets = efi_pxe_base_code_set_packets;
Alexander Graf94c4b992016-05-06 21:01:01 +0200773 netobj->pxe.mode = &netobj->pxe_mode;
774 if (dhcp_ack)
775 netobj->pxe_mode.dhcp_ack = *dhcp_ack;
776
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200777 /*
Heinrich Schuchardt0c153c22017-10-05 16:36:01 +0200778 * Create WaitForPacket event.
779 */
780 r = efi_create_event(EVT_NOTIFY_WAIT, TPL_CALLBACK,
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100781 efi_network_timer_notify, NULL, NULL,
Heinrich Schuchardt0c153c22017-10-05 16:36:01 +0200782 &wait_for_packet);
783 if (r != EFI_SUCCESS) {
784 printf("ERROR: Failed to register network event\n");
785 return r;
786 }
787 netobj->net.wait_for_packet = wait_for_packet;
788 /*
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200789 * Create a timer event.
790 *
791 * The notification function is used to check if a new network packet
792 * has been received.
Heinrich Schuchardt86df48c2018-03-24 18:40:22 +0100793 *
794 * iPXE is running at TPL_CALLBACK most of the time. Use a higher TPL.
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200795 */
Heinrich Schuchardt86df48c2018-03-24 18:40:22 +0100796 r = efi_create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_NOTIFY,
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100797 efi_network_timer_notify, &netobj->net, NULL,
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200798 &network_timer_event);
799 if (r != EFI_SUCCESS) {
800 printf("ERROR: Failed to register network event\n");
801 return r;
802 }
Heinrich Schuchardt5e96f422018-10-18 21:51:38 +0200803 /* Network is time critical, create event in every timer cycle */
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200804 r = efi_set_timer(network_timer_event, EFI_TIMER_PERIODIC, 0);
805 if (r != EFI_SUCCESS) {
806 printf("ERROR: Failed to set network timer\n");
807 return r;
808 }
809
Heinrich Schuchardt6f5c73f2018-03-03 15:28:56 +0100810 return EFI_SUCCESS;
811failure_to_add_protocol:
812 printf("ERROR: Failure to add protocol\n");
813 return r;
Heinrich Schuchardteb07c282018-12-01 00:16:32 +0100814out_of_resources:
815 free(netobj);
816 /* free(transmit_buffer) not needed yet */
817 printf("ERROR: Out of memory\n");
818 return EFI_OUT_OF_RESOURCES;
Alexander Graf94c4b992016-05-06 21:01:01 +0200819}