blob: 950b6ed4b2bbd2fbf520e28787a33cf3f38d88ba [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
Heinrich Schuchardtd3f1ff22019-08-31 09:56:30 +020069 if (this->mode->state != EFI_NETWORK_STOPPED) {
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +010070 ret = EFI_ALREADY_STARTED;
Heinrich Schuchardtd3f1ff22019-08-31 09:56:30 +020071 } else {
72 this->int_status = 0;
73 wait_for_packet->is_signaled = false;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +010074 this->mode->state = EFI_NETWORK_STARTED;
Heinrich Schuchardtd3f1ff22019-08-31 09:56:30 +020075 }
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +010076out:
77 return EFI_EXIT(ret);
Alexander Graf94c4b992016-05-06 21:01:01 +020078}
79
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +010080/*
81 * efi_net_stop() - stop the network interface
82 *
83 * This function implements the Stop service of the
84 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
85 * (UEFI) specification for details.
86 *
87 * @this: pointer to the protocol instance
88 * Return: status code
89 */
Alexander Graf94c4b992016-05-06 21:01:01 +020090static efi_status_t EFIAPI efi_net_stop(struct efi_simple_network *this)
91{
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +010092 efi_status_t ret = EFI_SUCCESS;
93
Alexander Graf94c4b992016-05-06 21:01:01 +020094 EFI_ENTRY("%p", this);
95
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +010096 /* Check parameters */
97 if (!this) {
98 ret = EFI_INVALID_PARAMETER;
99 goto out;
100 }
101
102 if (this->mode->state == EFI_NETWORK_STOPPED)
103 ret = EFI_NOT_STARTED;
104 else
105 this->mode->state = EFI_NETWORK_STOPPED;
106out:
107 return EFI_EXIT(ret);
Alexander Graf94c4b992016-05-06 21:01:01 +0200108}
109
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200110/*
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100111 * efi_net_initialize() - initialize the network interface
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200112 *
113 * This function implements the Initialize service of the
114 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
115 * (UEFI) specification for details.
116 *
117 * @this: pointer to the protocol instance
118 * @extra_rx: extra receive buffer to be allocated
119 * @extra_tx: extra transmit buffer to be allocated
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100120 * Return: status code
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200121 */
Alexander Graf94c4b992016-05-06 21:01:01 +0200122static efi_status_t EFIAPI efi_net_initialize(struct efi_simple_network *this,
123 ulong extra_rx, ulong extra_tx)
124{
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200125 int ret;
126 efi_status_t r = EFI_SUCCESS;
127
Alexander Graf94c4b992016-05-06 21:01:01 +0200128 EFI_ENTRY("%p, %lx, %lx", this, extra_rx, extra_tx);
129
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100130 /* Check parameters */
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200131 if (!this) {
132 r = EFI_INVALID_PARAMETER;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100133 goto out;
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200134 }
135
136 /* Setup packet buffers */
137 net_init();
138 /* Disable hardware and put it into the reset state */
139 eth_halt();
140 /* Set current device according to environment variables */
141 eth_set_current();
142 /* Get hardware ready for send and receive operations */
143 ret = eth_init();
144 if (ret < 0) {
145 eth_halt();
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100146 this->mode->state = EFI_NETWORK_STOPPED;
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200147 r = EFI_DEVICE_ERROR;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100148 goto out;
149 } else {
Heinrich Schuchardtd3f1ff22019-08-31 09:56:30 +0200150 this->int_status = 0;
151 wait_for_packet->is_signaled = false;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100152 this->mode->state = EFI_NETWORK_INITIALIZED;
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200153 }
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100154out:
Heinrich Schuchardtad4d67c2018-04-03 22:06:52 +0200155 return EFI_EXIT(r);
Alexander Graf94c4b992016-05-06 21:01:01 +0200156}
157
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100158/*
159 * efi_net_reset() - reinitialize the network interface
160 *
161 * This function implements the Reset service of the
162 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
163 * (UEFI) specification for details.
164 *
165 * @this: pointer to the protocol instance
166 * @extended_verification: execute exhaustive verification
167 * Return: status code
168 */
Alexander Graf94c4b992016-05-06 21:01:01 +0200169static efi_status_t EFIAPI efi_net_reset(struct efi_simple_network *this,
170 int extended_verification)
171{
172 EFI_ENTRY("%p, %x", this, extended_verification);
173
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100174 return EFI_EXIT(EFI_CALL(efi_net_initialize(this, 0, 0)));
Alexander Graf94c4b992016-05-06 21:01:01 +0200175}
176
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100177/*
178 * efi_net_shutdown() - shut down the network interface
179 *
180 * This function implements the Shutdown service of the
181 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
182 * (UEFI) specification for details.
183 *
184 * @this: pointer to the protocol instance
185 * Return: status code
186 */
Alexander Graf94c4b992016-05-06 21:01:01 +0200187static efi_status_t EFIAPI efi_net_shutdown(struct efi_simple_network *this)
188{
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100189 efi_status_t ret = EFI_SUCCESS;
190
Alexander Graf94c4b992016-05-06 21:01:01 +0200191 EFI_ENTRY("%p", this);
192
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100193 /* Check parameters */
194 if (!this) {
195 ret = EFI_INVALID_PARAMETER;
196 goto out;
197 }
198
199 eth_halt();
Heinrich Schuchardtd3f1ff22019-08-31 09:56:30 +0200200 this->int_status = 0;
201 wait_for_packet->is_signaled = false;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100202 this->mode->state = EFI_NETWORK_STOPPED;
203
204out:
205 return EFI_EXIT(ret);
Alexander Graf94c4b992016-05-06 21:01:01 +0200206}
207
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100208/*
209 * efi_net_receive_filters() - mange multicast receive filters
210 *
211 * This function implements the ReceiveFilters service of the
212 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
213 * (UEFI) specification for details.
214 *
215 * @this: pointer to the protocol instance
216 * @enable: bit mask of receive filters to enable
217 * @disable: bit mask of receive filters to disable
218 * @reset_mcast_filter: true resets contents of the filters
219 * @mcast_filter_count: number of hardware MAC addresses in the new filters list
220 * @mcast_filter: list of new filters
221 * Return: status code
222 */
223static efi_status_t EFIAPI efi_net_receive_filters
224 (struct efi_simple_network *this, u32 enable, u32 disable,
225 int reset_mcast_filter, ulong mcast_filter_count,
226 struct efi_mac_address *mcast_filter)
Alexander Graf94c4b992016-05-06 21:01:01 +0200227{
228 EFI_ENTRY("%p, %x, %x, %x, %lx, %p", this, enable, disable,
229 reset_mcast_filter, mcast_filter_count, mcast_filter);
230
Heinrich Schuchardta50b5c62017-10-05 16:35:59 +0200231 return EFI_EXIT(EFI_UNSUPPORTED);
Alexander Graf94c4b992016-05-06 21:01:01 +0200232}
233
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100234/*
235 * efi_net_station_address() - set the hardware MAC address
236 *
237 * This function implements the StationAddress service of the
238 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
239 * (UEFI) specification for details.
240 *
241 * @this: pointer to the protocol instance
242 * @reset: if true reset the address to default
243 * @new_mac: new MAC address
244 * Return: status code
245 */
246static efi_status_t EFIAPI efi_net_station_address
247 (struct efi_simple_network *this, int reset,
248 struct efi_mac_address *new_mac)
Alexander Graf94c4b992016-05-06 21:01:01 +0200249{
250 EFI_ENTRY("%p, %x, %p", this, reset, new_mac);
251
Heinrich Schuchardta50b5c62017-10-05 16:35:59 +0200252 return EFI_EXIT(EFI_UNSUPPORTED);
Alexander Graf94c4b992016-05-06 21:01:01 +0200253}
254
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100255/*
256 * efi_net_statistics() - reset or collect statistics of the network interface
257 *
258 * This function implements the Statistics service of the
259 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
260 * (UEFI) specification for details.
261 *
262 * @this: pointer to the protocol instance
263 * @reset: if true, the statistics are reset
264 * @stat_size: size of the statistics table
265 * @stat_table: table to receive the statistics
266 * Return: status code
267 */
Alexander Graf94c4b992016-05-06 21:01:01 +0200268static efi_status_t EFIAPI efi_net_statistics(struct efi_simple_network *this,
269 int reset, ulong *stat_size,
270 void *stat_table)
271{
272 EFI_ENTRY("%p, %x, %p, %p", this, reset, stat_size, stat_table);
273
Heinrich Schuchardta50b5c62017-10-05 16:35:59 +0200274 return EFI_EXIT(EFI_UNSUPPORTED);
Alexander Graf94c4b992016-05-06 21:01:01 +0200275}
276
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100277/*
278 * efi_net_mcastiptomac() - translate multicast IP address to MAC address
279 *
280 * This function implements the Statistics service of the
281 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
282 * (UEFI) specification for details.
283 *
284 * @this: pointer to the protocol instance
285 * @ipv6: true if the IP address is an IPv6 address
286 * @ip: IP address
287 * @mac: MAC address
288 * Return: status code
289 */
Alexander Graf94c4b992016-05-06 21:01:01 +0200290static efi_status_t EFIAPI efi_net_mcastiptomac(struct efi_simple_network *this,
291 int ipv6,
292 struct efi_ip_address *ip,
293 struct efi_mac_address *mac)
294{
295 EFI_ENTRY("%p, %x, %p, %p", this, ipv6, ip, mac);
296
297 return EFI_EXIT(EFI_INVALID_PARAMETER);
298}
299
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100300/**
301 * efi_net_nvdata() - read or write NVRAM
302 *
303 * This function implements the GetStatus service of the Simple Network
304 * Protocol. See the UEFI spec for details.
305 *
306 * @this: the instance of the Simple Network Protocol
307 * @readwrite: true for read, false for write
308 * @offset: offset in NVRAM
309 * @buffer_size: size of buffer
310 * @buffer: buffer
311 * Return: status code
312 */
Alexander Graf94c4b992016-05-06 21:01:01 +0200313static efi_status_t EFIAPI efi_net_nvdata(struct efi_simple_network *this,
314 int read_write, ulong offset,
315 ulong buffer_size, char *buffer)
316{
317 EFI_ENTRY("%p, %x, %lx, %lx, %p", this, read_write, offset, buffer_size,
318 buffer);
319
Heinrich Schuchardta50b5c62017-10-05 16:35:59 +0200320 return EFI_EXIT(EFI_UNSUPPORTED);
Alexander Graf94c4b992016-05-06 21:01:01 +0200321}
322
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100323/**
324 * efi_net_get_status() - get interrupt status
325 *
326 * This function implements the GetStatus service of the Simple Network
327 * Protocol. See the UEFI spec for details.
328 *
329 * @this: the instance of the Simple Network Protocol
330 * @int_status: interface status
331 * @txbuf: transmission buffer
332 */
Alexander Graf94c4b992016-05-06 21:01:01 +0200333static efi_status_t EFIAPI efi_net_get_status(struct efi_simple_network *this,
334 u32 *int_status, void **txbuf)
335{
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100336 efi_status_t ret = EFI_SUCCESS;
337
Alexander Graf94c4b992016-05-06 21:01:01 +0200338 EFI_ENTRY("%p, %p, %p", this, int_status, txbuf);
339
Heinrich Schuchardte371c5f2017-10-05 16:36:02 +0200340 efi_timer_check();
341
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100342 /* Check parameters */
343 if (!this) {
344 ret = EFI_INVALID_PARAMETER;
345 goto out;
346 }
347
348 switch (this->mode->state) {
349 case EFI_NETWORK_STOPPED:
350 ret = EFI_NOT_STARTED;
351 goto out;
352 case EFI_NETWORK_STARTED:
353 ret = EFI_DEVICE_ERROR;
354 goto out;
355 default:
356 break;
357 }
358
Heinrich Schuchardte371c5f2017-10-05 16:36:02 +0200359 if (int_status) {
Heinrich Schuchardtd3f1ff22019-08-31 09:56:30 +0200360 *int_status = this->int_status;
361 this->int_status = 0;
Heinrich Schuchardte371c5f2017-10-05 16:36:02 +0200362 }
Alexander Graf94c4b992016-05-06 21:01:01 +0200363 if (txbuf)
364 *txbuf = new_tx_packet;
365
366 new_tx_packet = NULL;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100367out:
368 return EFI_EXIT(ret);
Alexander Graf94c4b992016-05-06 21:01:01 +0200369}
370
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100371/**
372 * efi_net_transmit() - transmit a packet
373 *
374 * This function implements the Transmit service of the Simple Network Protocol.
375 * See the UEFI spec for details.
376 *
377 * @this: the instance of the Simple Network Protocol
378 * @header_size: size of the media header
379 * @buffer_size: size of the buffer to receive the packet
380 * @buffer: buffer to receive the packet
381 * @src_addr: source hardware MAC address
382 * @dest_addr: destination hardware MAC address
383 * @protocol: type of header to build
384 * Return: status code
385 */
386static efi_status_t EFIAPI efi_net_transmit
387 (struct efi_simple_network *this, size_t header_size,
388 size_t buffer_size, void *buffer,
389 struct efi_mac_address *src_addr,
390 struct efi_mac_address *dest_addr, u16 *protocol)
Alexander Graf94c4b992016-05-06 21:01:01 +0200391{
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100392 efi_status_t ret = EFI_SUCCESS;
393
Heinrich Schuchardtf66b2e32017-10-05 16:36:03 +0200394 EFI_ENTRY("%p, %lu, %lu, %p, %p, %p, %p", this,
395 (unsigned long)header_size, (unsigned long)buffer_size,
396 buffer, src_addr, dest_addr, protocol);
Alexander Graf94c4b992016-05-06 21:01:01 +0200397
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200398 efi_timer_check();
399
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100400 /* Check parameters */
Heinrich Schuchardtf286c2c2019-05-15 23:27:43 +0200401 if (!this || !buffer) {
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100402 ret = EFI_INVALID_PARAMETER;
403 goto out;
404 }
405
406 /* We do not support jumbo packets */
407 if (buffer_size > PKTSIZE_ALIGN) {
408 ret = EFI_INVALID_PARAMETER;
409 goto out;
410 }
411
Heinrich Schuchardt0218a8f2019-08-31 10:55:29 +0200412 /* At least the IP header has to fit into the buffer */
413 if (buffer_size < this->mode->media_header_size) {
414 ret = EFI_BUFFER_TOO_SMALL;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100415 goto out;
Alexander Graf94c4b992016-05-06 21:01:01 +0200416 }
417
Heinrich Schuchardt0218a8f2019-08-31 10:55:29 +0200418 /*
419 * TODO:
420 * Support VLANs. Use net_set_ether() for copying the header. Use a
421 * U_BOOT_ENV_CALLBACK to update the media header size.
422 */
423 if (header_size) {
424 struct ethernet_hdr *header = buffer;
425
426 if (!dest_addr || !protocol ||
427 header_size != this->mode->media_header_size) {
428 ret = EFI_INVALID_PARAMETER;
429 goto out;
430 }
431 if (!src_addr)
432 src_addr = &this->mode->current_address;
433
434 memcpy(header->et_dest, dest_addr, ARP_HLEN);
435 memcpy(header->et_src, src_addr, ARP_HLEN);
436 header->et_protlen = htons(*protocol);
437 }
438
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100439 switch (this->mode->state) {
440 case EFI_NETWORK_STOPPED:
441 ret = EFI_NOT_STARTED;
442 goto out;
443 case EFI_NETWORK_STARTED:
444 ret = EFI_DEVICE_ERROR;
445 goto out;
446 default:
447 break;
448 }
449
450 /* Ethernet packets always fit, just bounce */
Heinrich Schuchardteb07c282018-12-01 00:16:32 +0100451 memcpy(transmit_buffer, buffer, buffer_size);
452 net_send_packet(transmit_buffer, buffer_size);
Alexander Graf0b12b862016-09-06 14:26:27 +0200453
Alexander Graf94c4b992016-05-06 21:01:01 +0200454 new_tx_packet = buffer;
Heinrich Schuchardtd3f1ff22019-08-31 09:56:30 +0200455 this->int_status |= EFI_SIMPLE_NETWORK_TRANSMIT_INTERRUPT;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100456out:
457 return EFI_EXIT(ret);
Alexander Graf94c4b992016-05-06 21:01:01 +0200458}
459
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100460/**
461 * efi_net_receive() - receive a packet from a network interface
Heinrich Schuchardtf66b2e32017-10-05 16:36:03 +0200462 *
463 * This function implements the Receive service of the Simple Network Protocol.
464 * See the UEFI spec for details.
465 *
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100466 * @this: the instance of the Simple Network Protocol
467 * @header_size: size of the media header
468 * @buffer_size: size of the buffer to receive the packet
469 * @buffer: buffer to receive the packet
470 * @src_addr: source MAC address
471 * @dest_addr: destination MAC address
472 * @protocol: protocol
473 * Return: status code
Heinrich Schuchardtf66b2e32017-10-05 16:36:03 +0200474 */
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100475static efi_status_t EFIAPI efi_net_receive
476 (struct efi_simple_network *this, size_t *header_size,
477 size_t *buffer_size, void *buffer,
478 struct efi_mac_address *src_addr,
479 struct efi_mac_address *dest_addr, u16 *protocol)
Alexander Graf94c4b992016-05-06 21:01:01 +0200480{
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100481 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardtaa0d1972017-10-05 16:36:04 +0200482 struct ethernet_hdr *eth_hdr;
483 size_t hdr_size = sizeof(struct ethernet_hdr);
484 u16 protlen;
485
Alexander Graf94c4b992016-05-06 21:01:01 +0200486 EFI_ENTRY("%p, %p, %p, %p, %p, %p, %p", this, header_size,
487 buffer_size, buffer, src_addr, dest_addr, protocol);
488
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100489 /* Execute events */
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200490 efi_timer_check();
Alexander Graf94c4b992016-05-06 21:01:01 +0200491
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100492 /* Check parameters */
Heinrich Schuchardtf286c2c2019-05-15 23:27:43 +0200493 if (!this || !buffer || !buffer_size) {
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100494 ret = EFI_INVALID_PARAMETER;
495 goto out;
496 }
497
498 switch (this->mode->state) {
499 case EFI_NETWORK_STOPPED:
500 ret = EFI_NOT_STARTED;
501 goto out;
502 case EFI_NETWORK_STARTED:
503 ret = EFI_DEVICE_ERROR;
504 goto out;
505 default:
506 break;
507 }
508
509 if (!new_rx_packet) {
510 ret = EFI_NOT_READY;
511 goto out;
512 }
Heinrich Schuchardtaa0d1972017-10-05 16:36:04 +0200513 /* Fill export parameters */
514 eth_hdr = (struct ethernet_hdr *)net_rx_packet;
515 protlen = ntohs(eth_hdr->et_protlen);
516 if (protlen == 0x8100) {
517 hdr_size += 4;
518 protlen = ntohs(*(u16 *)&net_rx_packet[hdr_size - 2]);
519 }
520 if (header_size)
521 *header_size = hdr_size;
522 if (dest_addr)
523 memcpy(dest_addr, eth_hdr->et_dest, ARP_HLEN);
524 if (src_addr)
525 memcpy(src_addr, eth_hdr->et_src, ARP_HLEN);
526 if (protocol)
527 *protocol = protlen;
Alexander Graf94c4b992016-05-06 21:01:01 +0200528 if (*buffer_size < net_rx_packet_len) {
Heinrich Schuchardt5e96f422018-10-18 21:51:38 +0200529 /* Packet doesn't fit, try again with bigger buffer */
Alexander Graf94c4b992016-05-06 21:01:01 +0200530 *buffer_size = net_rx_packet_len;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100531 ret = EFI_BUFFER_TOO_SMALL;
532 goto out;
Alexander Graf94c4b992016-05-06 21:01:01 +0200533 }
Heinrich Schuchardtaa0d1972017-10-05 16:36:04 +0200534 /* Copy packet */
Alexander Graf94c4b992016-05-06 21:01:01 +0200535 memcpy(buffer, net_rx_packet, net_rx_packet_len);
536 *buffer_size = net_rx_packet_len;
Heinrich Schuchardtd3f1ff22019-08-31 09:56:30 +0200537 new_rx_packet = 0;
538 this->int_status &= ~EFI_SIMPLE_NETWORK_RECEIVE_INTERRUPT;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100539out:
540 return EFI_EXIT(ret);
Alexander Graf94c4b992016-05-06 21:01:01 +0200541}
542
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100543/**
544 * efi_net_set_dhcp_ack() - take note of a selected DHCP IP address
545 *
546 * This function is called by dhcp_handler().
547 */
Alexander Graf94c4b992016-05-06 21:01:01 +0200548void efi_net_set_dhcp_ack(void *pkt, int len)
549{
550 int maxsize = sizeof(*dhcp_ack);
551
552 if (!dhcp_ack)
553 dhcp_ack = malloc(maxsize);
554
555 memcpy(dhcp_ack, pkt, min(len, maxsize));
556}
557
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100558/**
559 * efi_net_push() - callback for received network packet
560 *
561 * This function is called when a network packet is received by eth_rx().
562 *
563 * @pkt: network packet
564 * @len: length
565 */
566static void efi_net_push(void *pkt, int len)
567{
568 new_rx_packet = true;
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100569}
570
571/**
572 * efi_network_timer_notify() - check if a new network packet has been received
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200573 *
574 * This notification function is called in every timer cycle.
575 *
576 * @event the event for which this notification function is registered
577 * @context event context - not used in this function
578 */
579static void EFIAPI efi_network_timer_notify(struct efi_event *event,
580 void *context)
581{
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100582 struct efi_simple_network *this = (struct efi_simple_network *)context;
583
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200584 EFI_ENTRY("%p, %p", event, context);
585
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100586 /*
587 * Some network drivers do not support calling eth_rx() before
588 * initialization.
589 */
590 if (!this || this->mode->state != EFI_NETWORK_INITIALIZED)
591 goto out;
592
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200593 if (!new_rx_packet) {
594 push_packet = efi_net_push;
595 eth_rx();
596 push_packet = NULL;
Heinrich Schuchardtd3f1ff22019-08-31 09:56:30 +0200597 if (new_rx_packet) {
598 /* Check that we at least received an Ethernet header */
599 if (net_rx_packet_len >=
600 sizeof(struct ethernet_hdr)) {
601 this->int_status |=
602 EFI_SIMPLE_NETWORK_RECEIVE_INTERRUPT;
603 wait_for_packet->is_signaled = true;
604 } else {
605 new_rx_packet = 0;
606 }
607 }
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200608 }
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100609out:
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200610 EFI_EXIT(EFI_SUCCESS);
611}
612
Heinrich Schuchardt3127e7e2019-08-06 08:13:33 +0200613static efi_status_t EFIAPI efi_pxe_base_code_start(
614 struct efi_pxe_base_code_protocol *this,
615 u8 use_ipv6)
616{
617 return EFI_UNSUPPORTED;
618}
619
620static efi_status_t EFIAPI efi_pxe_base_code_stop(
621 struct efi_pxe_base_code_protocol *this)
622{
623 return EFI_UNSUPPORTED;
624}
625
626static efi_status_t EFIAPI efi_pxe_base_code_dhcp(
627 struct efi_pxe_base_code_protocol *this,
628 u8 sort_offers)
629{
630 return EFI_UNSUPPORTED;
631}
632
633static efi_status_t EFIAPI efi_pxe_base_code_discover(
634 struct efi_pxe_base_code_protocol *this,
635 u16 type, u16 *layer, u8 bis,
636 struct efi_pxe_base_code_discover_info *info)
637{
638 return EFI_UNSUPPORTED;
639}
640
641static efi_status_t EFIAPI efi_pxe_base_code_mtftp(
642 struct efi_pxe_base_code_protocol *this,
643 u32 operation, void *buffer_ptr,
644 u8 overwrite, efi_uintn_t *buffer_size,
645 struct efi_ip_address server_ip, char *filename,
646 struct efi_pxe_base_code_mtftp_info *info,
647 u8 dont_use_buffer)
648{
649 return EFI_UNSUPPORTED;
650}
651
652static efi_status_t EFIAPI efi_pxe_base_code_udp_write(
653 struct efi_pxe_base_code_protocol *this,
654 u16 op_flags, struct efi_ip_address *dest_ip,
655 u16 *dest_port,
656 struct efi_ip_address *gateway_ip,
657 struct efi_ip_address *src_ip, u16 *src_port,
658 efi_uintn_t *header_size, void *header_ptr,
659 efi_uintn_t *buffer_size, void *buffer_ptr)
660{
661 return EFI_UNSUPPORTED;
662}
663
664static efi_status_t EFIAPI efi_pxe_base_code_udp_read(
665 struct efi_pxe_base_code_protocol *this,
666 u16 op_flags, struct efi_ip_address *dest_ip,
667 u16 *dest_port, struct efi_ip_address *src_ip,
668 u16 *src_port, efi_uintn_t *header_size,
669 void *header_ptr, efi_uintn_t *buffer_size,
670 void *buffer_ptr)
671{
672 return EFI_UNSUPPORTED;
673}
674
675static efi_status_t EFIAPI efi_pxe_base_code_set_ip_filter(
676 struct efi_pxe_base_code_protocol *this,
677 struct efi_pxe_base_code_filter *new_filter)
678{
679 return EFI_UNSUPPORTED;
680}
681
682static efi_status_t EFIAPI efi_pxe_base_code_arp(
683 struct efi_pxe_base_code_protocol *this,
684 struct efi_ip_address *ip_addr,
685 struct efi_mac_address *mac_addr)
686{
687 return EFI_UNSUPPORTED;
688}
689
690static efi_status_t EFIAPI efi_pxe_base_code_set_parameters(
691 struct efi_pxe_base_code_protocol *this,
692 u8 *new_auto_arp, u8 *new_send_guid,
693 u8 *new_ttl, u8 *new_tos,
694 u8 *new_make_callback)
695{
696 return EFI_UNSUPPORTED;
697}
698
699static efi_status_t EFIAPI efi_pxe_base_code_set_station_ip(
700 struct efi_pxe_base_code_protocol *this,
701 struct efi_ip_address *new_station_ip,
702 struct efi_ip_address *new_subnet_mask)
703{
704 return EFI_UNSUPPORTED;
705}
706
707static efi_status_t EFIAPI efi_pxe_base_code_set_packets(
708 struct efi_pxe_base_code_protocol *this,
709 u8 *new_dhcp_discover_valid,
710 u8 *new_dhcp_ack_received,
711 u8 *new_proxy_offer_received,
712 u8 *new_pxe_discover_valid,
713 u8 *new_pxe_reply_received,
714 u8 *new_pxe_bis_reply_received,
715 EFI_PXE_BASE_CODE_PACKET *new_dchp_discover,
716 EFI_PXE_BASE_CODE_PACKET *new_dhcp_acc,
717 EFI_PXE_BASE_CODE_PACKET *new_proxy_offer,
718 EFI_PXE_BASE_CODE_PACKET *new_pxe_discover,
719 EFI_PXE_BASE_CODE_PACKET *new_pxe_reply,
720 EFI_PXE_BASE_CODE_PACKET *new_pxe_bis_reply)
721{
722 return EFI_UNSUPPORTED;
723}
724
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100725/**
726 * efi_net_register() - register the simple network protocol
727 *
728 * This gets called from do_bootefi_exec().
729 */
Heinrich Schuchardt6f5c73f2018-03-03 15:28:56 +0100730efi_status_t efi_net_register(void)
Alexander Graf94c4b992016-05-06 21:01:01 +0200731{
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100732 struct efi_net_obj *netobj = NULL;
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200733 efi_status_t r;
Alexander Graf94c4b992016-05-06 21:01:01 +0200734
735 if (!eth_get_dev()) {
Heinrich Schuchardt5e96f422018-10-18 21:51:38 +0200736 /* No network device active, don't expose any */
Heinrich Schuchardt6f5c73f2018-03-03 15:28:56 +0100737 return EFI_SUCCESS;
Alexander Graf94c4b992016-05-06 21:01:01 +0200738 }
739
Heinrich Schuchardt5e96f422018-10-18 21:51:38 +0200740 /* We only expose the "active" network device, so one is enough */
Alexander Graf94c4b992016-05-06 21:01:01 +0200741 netobj = calloc(1, sizeof(*netobj));
Heinrich Schuchardteb07c282018-12-01 00:16:32 +0100742 if (!netobj)
743 goto out_of_resources;
744
745 /* Allocate an aligned transmit buffer */
746 transmit_buffer = calloc(1, PKTSIZE_ALIGN + PKTALIGN);
747 if (!transmit_buffer)
748 goto out_of_resources;
749 transmit_buffer = (void *)ALIGN((uintptr_t)transmit_buffer, PKTALIGN);
Heinrich Schuchardt23b45b62017-11-26 14:05:13 +0100750
751 /* Hook net up to the device list */
Heinrich Schuchardt72928722018-09-26 05:27:56 +0200752 efi_add_handle(&netobj->header);
Alexander Graf94c4b992016-05-06 21:01:01 +0200753
754 /* Fill in object data */
Heinrich Schuchardt72928722018-09-26 05:27:56 +0200755 r = efi_add_protocol(&netobj->header, &efi_net_guid,
Heinrich Schuchardt23b45b62017-11-26 14:05:13 +0100756 &netobj->net);
757 if (r != EFI_SUCCESS)
Heinrich Schuchardt6f5c73f2018-03-03 15:28:56 +0100758 goto failure_to_add_protocol;
Heinrich Schuchardt72928722018-09-26 05:27:56 +0200759 r = efi_add_protocol(&netobj->header, &efi_guid_device_path,
Heinrich Schuchardt23b45b62017-11-26 14:05:13 +0100760 efi_dp_from_eth());
761 if (r != EFI_SUCCESS)
Heinrich Schuchardt6f5c73f2018-03-03 15:28:56 +0100762 goto failure_to_add_protocol;
Heinrich Schuchardt3127e7e2019-08-06 08:13:33 +0200763 r = efi_add_protocol(&netobj->header, &efi_pxe_base_code_protocol_guid,
Heinrich Schuchardt23b45b62017-11-26 14:05:13 +0100764 &netobj->pxe);
765 if (r != EFI_SUCCESS)
Heinrich Schuchardt6f5c73f2018-03-03 15:28:56 +0100766 goto failure_to_add_protocol;
Heinrich Schuchardtd54396a2017-10-05 16:35:57 +0200767 netobj->net.revision = EFI_SIMPLE_NETWORK_PROTOCOL_REVISION;
Alexander Graf94c4b992016-05-06 21:01:01 +0200768 netobj->net.start = efi_net_start;
769 netobj->net.stop = efi_net_stop;
770 netobj->net.initialize = efi_net_initialize;
771 netobj->net.reset = efi_net_reset;
772 netobj->net.shutdown = efi_net_shutdown;
773 netobj->net.receive_filters = efi_net_receive_filters;
774 netobj->net.station_address = efi_net_station_address;
775 netobj->net.statistics = efi_net_statistics;
776 netobj->net.mcastiptomac = efi_net_mcastiptomac;
777 netobj->net.nvdata = efi_net_nvdata;
778 netobj->net.get_status = efi_net_get_status;
779 netobj->net.transmit = efi_net_transmit;
780 netobj->net.receive = efi_net_receive;
781 netobj->net.mode = &netobj->net_mode;
782 netobj->net_mode.state = EFI_NETWORK_STARTED;
Alexander Graf94c4b992016-05-06 21:01:01 +0200783 memcpy(netobj->net_mode.current_address.mac_addr, eth_get_ethaddr(), 6);
Heinrich Schuchardtd1cc6cb2017-10-05 16:35:58 +0200784 netobj->net_mode.hwaddr_size = ARP_HLEN;
Heinrich Schuchardt0218a8f2019-08-31 10:55:29 +0200785 netobj->net_mode.media_header_size = ETHER_HDR_SIZE;
Alexander Graf94c4b992016-05-06 21:01:01 +0200786 netobj->net_mode.max_packet_size = PKTSIZE;
Andrew Thomas27df0a72018-06-21 16:21:01 -0700787 netobj->net_mode.if_type = ARP_ETHER;
Alexander Graf94c4b992016-05-06 21:01:01 +0200788
Heinrich Schuchardt3127e7e2019-08-06 08:13:33 +0200789 netobj->pxe.revision = EFI_PXE_BASE_CODE_PROTOCOL_REVISION;
790 netobj->pxe.start = efi_pxe_base_code_start;
791 netobj->pxe.stop = efi_pxe_base_code_stop;
792 netobj->pxe.dhcp = efi_pxe_base_code_dhcp;
793 netobj->pxe.discover = efi_pxe_base_code_discover;
794 netobj->pxe.mtftp = efi_pxe_base_code_mtftp;
795 netobj->pxe.udp_write = efi_pxe_base_code_udp_write;
796 netobj->pxe.udp_read = efi_pxe_base_code_udp_read;
797 netobj->pxe.set_ip_filter = efi_pxe_base_code_set_ip_filter;
798 netobj->pxe.arp = efi_pxe_base_code_arp;
799 netobj->pxe.set_parameters = efi_pxe_base_code_set_parameters;
800 netobj->pxe.set_station_ip = efi_pxe_base_code_set_station_ip;
801 netobj->pxe.set_packets = efi_pxe_base_code_set_packets;
Alexander Graf94c4b992016-05-06 21:01:01 +0200802 netobj->pxe.mode = &netobj->pxe_mode;
803 if (dhcp_ack)
804 netobj->pxe_mode.dhcp_ack = *dhcp_ack;
805
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200806 /*
Heinrich Schuchardt0c153c22017-10-05 16:36:01 +0200807 * Create WaitForPacket event.
808 */
809 r = efi_create_event(EVT_NOTIFY_WAIT, TPL_CALLBACK,
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100810 efi_network_timer_notify, NULL, NULL,
Heinrich Schuchardt0c153c22017-10-05 16:36:01 +0200811 &wait_for_packet);
812 if (r != EFI_SUCCESS) {
813 printf("ERROR: Failed to register network event\n");
814 return r;
815 }
816 netobj->net.wait_for_packet = wait_for_packet;
817 /*
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200818 * Create a timer event.
819 *
820 * The notification function is used to check if a new network packet
821 * has been received.
Heinrich Schuchardt86df48c2018-03-24 18:40:22 +0100822 *
823 * iPXE is running at TPL_CALLBACK most of the time. Use a higher TPL.
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200824 */
Heinrich Schuchardt86df48c2018-03-24 18:40:22 +0100825 r = efi_create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_NOTIFY,
Heinrich Schuchardtb6f20362018-12-01 00:16:33 +0100826 efi_network_timer_notify, &netobj->net, NULL,
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200827 &network_timer_event);
828 if (r != EFI_SUCCESS) {
829 printf("ERROR: Failed to register network event\n");
830 return r;
831 }
Heinrich Schuchardt5e96f422018-10-18 21:51:38 +0200832 /* Network is time critical, create event in every timer cycle */
Heinrich Schuchardt3d7fc692017-10-05 16:36:00 +0200833 r = efi_set_timer(network_timer_event, EFI_TIMER_PERIODIC, 0);
834 if (r != EFI_SUCCESS) {
835 printf("ERROR: Failed to set network timer\n");
836 return r;
837 }
838
Heinrich Schuchardt6f5c73f2018-03-03 15:28:56 +0100839 return EFI_SUCCESS;
840failure_to_add_protocol:
841 printf("ERROR: Failure to add protocol\n");
842 return r;
Heinrich Schuchardteb07c282018-12-01 00:16:32 +0100843out_of_resources:
844 free(netobj);
845 /* free(transmit_buffer) not needed yet */
846 printf("ERROR: Out of memory\n");
847 return EFI_OUT_OF_RESOURCES;
Alexander Graf94c4b992016-05-06 21:01:01 +0200848}