]> Git Repo - u-boot.git/blame - lib/efi_loader/efi_net.c
Fix incorrect return code of boot option update
[u-boot.git] / lib / efi_loader / efi_net.c
CommitLineData
f739fcd8 1// SPDX-License-Identifier: GPL-2.0+
0efe1bcf 2/*
72a8f168
HS
3 * Simple network protocol
4 * PXE base code protocol
0efe1bcf 5 *
72a8f168
HS
6 * Copyright (c) 2016 Alexander Graf
7 *
8 * The simple network protocol has the following statuses and services
9 * to move between them:
10 *
11 * Start(): EfiSimpleNetworkStopped -> EfiSimpleNetworkStarted
12 * Initialize(): EfiSimpleNetworkStarted -> EfiSimpleNetworkInitialized
13 * Shutdown(): EfiSimpleNetworkInitialized -> EfiSimpleNetworkStarted
14 * Stop(): EfiSimpleNetworkStarted -> EfiSimpleNetworkStopped
15 * Reset(): EfiSimpleNetworkInitialized -> EfiSimpleNetworkInitialized
0efe1bcf
AG
16 */
17
18#include <common.h>
19#include <efi_loader.h>
0efe1bcf 20#include <malloc.h>
90526e9f 21#include <net.h>
0efe1bcf 22
dec88e41 23static const efi_guid_t efi_net_guid = EFI_SIMPLE_NETWORK_PROTOCOL_GUID;
a6d37098
HS
24static const efi_guid_t efi_pxe_base_code_protocol_guid =
25 EFI_PXE_BASE_CODE_PROTOCOL_GUID;
0efe1bcf 26static struct efi_pxe_packet *dhcp_ack;
0efe1bcf 27static void *new_tx_packet;
622fe621 28static void *transmit_buffer;
42f804fb
PW
29static uchar **receive_buffer;
30static size_t *receive_lengths;
31static int rx_packet_idx;
32static int rx_packet_num;
1702055e 33static struct efi_net_obj *netobj;
622fe621 34
a0549ef6
HS
35/*
36 * The notification function of this event is called in every timer cycle
37 * to check if a new network packet has been received.
38 */
39static struct efi_event *network_timer_event;
e5c21603
HS
40/*
41 * This event is signaled when a packet has been received.
42 */
43static struct efi_event *wait_for_packet;
0efe1bcf 44
d39646a3
HS
45/**
46 * struct efi_net_obj - EFI object representing a network interface
47 *
48 * @header: EFI object header
49 * @net: simple network protocol interface
50 * @net_mode: status of the network interface
51 * @pxe: PXE base code protocol interface
52 * @pxe_mode: status of the PXE base code protocol
53 */
0efe1bcf 54struct efi_net_obj {
d39646a3 55 struct efi_object header;
0efe1bcf
AG
56 struct efi_simple_network net;
57 struct efi_simple_network_mode net_mode;
a6d37098 58 struct efi_pxe_base_code_protocol pxe;
0efe1bcf
AG
59 struct efi_pxe_mode pxe_mode;
60};
61
41b05879
HS
62/*
63 * efi_net_start() - start the network interface
64 *
65 * This function implements the Start service of the
66 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
67 * (UEFI) specification for details.
68 *
69 * @this: pointer to the protocol instance
70 * Return: status code
71 */
0efe1bcf
AG
72static efi_status_t EFIAPI efi_net_start(struct efi_simple_network *this)
73{
41b05879
HS
74 efi_status_t ret = EFI_SUCCESS;
75
0efe1bcf
AG
76 EFI_ENTRY("%p", this);
77
41b05879
HS
78 /* Check parameters */
79 if (!this) {
80 ret = EFI_INVALID_PARAMETER;
81 goto out;
82 }
83
7f6d874d 84 if (this->mode->state != EFI_NETWORK_STOPPED) {
41b05879 85 ret = EFI_ALREADY_STARTED;
7f6d874d
HS
86 } else {
87 this->int_status = 0;
88 wait_for_packet->is_signaled = false;
41b05879 89 this->mode->state = EFI_NETWORK_STARTED;
7f6d874d 90 }
41b05879
HS
91out:
92 return EFI_EXIT(ret);
0efe1bcf
AG
93}
94
41b05879
HS
95/*
96 * efi_net_stop() - stop the network interface
97 *
98 * This function implements the Stop service of the
99 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
100 * (UEFI) specification for details.
101 *
102 * @this: pointer to the protocol instance
103 * Return: status code
104 */
0efe1bcf
AG
105static efi_status_t EFIAPI efi_net_stop(struct efi_simple_network *this)
106{
41b05879
HS
107 efi_status_t ret = EFI_SUCCESS;
108
0efe1bcf
AG
109 EFI_ENTRY("%p", this);
110
41b05879
HS
111 /* Check parameters */
112 if (!this) {
113 ret = EFI_INVALID_PARAMETER;
114 goto out;
115 }
116
72a8f168 117 if (this->mode->state == EFI_NETWORK_STOPPED) {
41b05879 118 ret = EFI_NOT_STARTED;
72a8f168
HS
119 } else {
120 /* Disable hardware and put it into the reset state */
121 eth_halt();
42f804fb
PW
122 /* Clear cache of packets */
123 rx_packet_num = 0;
41b05879 124 this->mode->state = EFI_NETWORK_STOPPED;
72a8f168 125 }
41b05879
HS
126out:
127 return EFI_EXIT(ret);
0efe1bcf
AG
128}
129
0c5d2a3d 130/*
41b05879 131 * efi_net_initialize() - initialize the network interface
0c5d2a3d
HS
132 *
133 * This function implements the Initialize service of the
134 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
135 * (UEFI) specification for details.
136 *
137 * @this: pointer to the protocol instance
138 * @extra_rx: extra receive buffer to be allocated
139 * @extra_tx: extra transmit buffer to be allocated
41b05879 140 * Return: status code
0c5d2a3d 141 */
0efe1bcf
AG
142static efi_status_t EFIAPI efi_net_initialize(struct efi_simple_network *this,
143 ulong extra_rx, ulong extra_tx)
144{
0c5d2a3d
HS
145 int ret;
146 efi_status_t r = EFI_SUCCESS;
0efe1bcf 147
0c5d2a3d 148 EFI_ENTRY("%p, %lx, %lx", this, extra_rx, extra_tx);
0efe1bcf 149
41b05879 150 /* Check parameters */
0c5d2a3d
HS
151 if (!this) {
152 r = EFI_INVALID_PARAMETER;
41b05879 153 goto out;
0c5d2a3d
HS
154 }
155
72a8f168
HS
156 switch (this->mode->state) {
157 case EFI_NETWORK_INITIALIZED:
158 case EFI_NETWORK_STARTED:
159 break;
160 default:
161 r = EFI_NOT_STARTED;
162 goto out;
163 }
164
0c5d2a3d
HS
165 /* Setup packet buffers */
166 net_init();
167 /* Disable hardware and put it into the reset state */
168 eth_halt();
42f804fb
PW
169 /* Clear cache of packets */
170 rx_packet_num = 0;
0c5d2a3d
HS
171 /* Set current device according to environment variables */
172 eth_set_current();
173 /* Get hardware ready for send and receive operations */
174 ret = eth_init();
175 if (ret < 0) {
176 eth_halt();
41b05879 177 this->mode->state = EFI_NETWORK_STOPPED;
0c5d2a3d 178 r = EFI_DEVICE_ERROR;
41b05879
HS
179 goto out;
180 } else {
7f6d874d
HS
181 this->int_status = 0;
182 wait_for_packet->is_signaled = false;
41b05879 183 this->mode->state = EFI_NETWORK_INITIALIZED;
0c5d2a3d 184 }
41b05879 185out:
0c5d2a3d 186 return EFI_EXIT(r);
0efe1bcf
AG
187}
188
41b05879
HS
189/*
190 * efi_net_reset() - reinitialize the network interface
191 *
192 * This function implements the Reset service of the
193 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
194 * (UEFI) specification for details.
195 *
196 * @this: pointer to the protocol instance
197 * @extended_verification: execute exhaustive verification
198 * Return: status code
199 */
0efe1bcf
AG
200static efi_status_t EFIAPI efi_net_reset(struct efi_simple_network *this,
201 int extended_verification)
202{
72a8f168
HS
203 efi_status_t ret;
204
0efe1bcf
AG
205 EFI_ENTRY("%p, %x", this, extended_verification);
206
72a8f168
HS
207 /* Check parameters */
208 if (!this) {
209 ret = EFI_INVALID_PARAMETER;
210 goto out;
211 }
212
213 switch (this->mode->state) {
214 case EFI_NETWORK_INITIALIZED:
215 break;
216 case EFI_NETWORK_STOPPED:
217 ret = EFI_NOT_STARTED;
218 goto out;
219 default:
220 ret = EFI_DEVICE_ERROR;
221 goto out;
222 }
223
224 this->mode->state = EFI_NETWORK_STARTED;
225 ret = EFI_CALL(efi_net_initialize(this, 0, 0));
226out:
227 return EFI_EXIT(ret);
0efe1bcf
AG
228}
229
41b05879
HS
230/*
231 * efi_net_shutdown() - shut down the network interface
232 *
233 * This function implements the Shutdown service of the
234 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
235 * (UEFI) specification for details.
236 *
237 * @this: pointer to the protocol instance
238 * Return: status code
239 */
0efe1bcf
AG
240static efi_status_t EFIAPI efi_net_shutdown(struct efi_simple_network *this)
241{
41b05879
HS
242 efi_status_t ret = EFI_SUCCESS;
243
0efe1bcf
AG
244 EFI_ENTRY("%p", this);
245
41b05879
HS
246 /* Check parameters */
247 if (!this) {
248 ret = EFI_INVALID_PARAMETER;
249 goto out;
250 }
251
72a8f168
HS
252 switch (this->mode->state) {
253 case EFI_NETWORK_INITIALIZED:
254 break;
255 case EFI_NETWORK_STOPPED:
256 ret = EFI_NOT_STARTED;
257 goto out;
258 default:
259 ret = EFI_DEVICE_ERROR;
260 goto out;
261 }
262
41b05879 263 eth_halt();
7f6d874d
HS
264 this->int_status = 0;
265 wait_for_packet->is_signaled = false;
72a8f168 266 this->mode->state = EFI_NETWORK_STARTED;
41b05879
HS
267
268out:
269 return EFI_EXIT(ret);
0efe1bcf
AG
270}
271
41b05879
HS
272/*
273 * efi_net_receive_filters() - mange multicast receive filters
274 *
275 * This function implements the ReceiveFilters service of the
276 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
277 * (UEFI) specification for details.
278 *
279 * @this: pointer to the protocol instance
280 * @enable: bit mask of receive filters to enable
281 * @disable: bit mask of receive filters to disable
282 * @reset_mcast_filter: true resets contents of the filters
283 * @mcast_filter_count: number of hardware MAC addresses in the new filters list
284 * @mcast_filter: list of new filters
285 * Return: status code
286 */
287static efi_status_t EFIAPI efi_net_receive_filters
288 (struct efi_simple_network *this, u32 enable, u32 disable,
289 int reset_mcast_filter, ulong mcast_filter_count,
290 struct efi_mac_address *mcast_filter)
0efe1bcf
AG
291{
292 EFI_ENTRY("%p, %x, %x, %x, %lx, %p", this, enable, disable,
293 reset_mcast_filter, mcast_filter_count, mcast_filter);
294
61da678c 295 return EFI_EXIT(EFI_UNSUPPORTED);
0efe1bcf
AG
296}
297
41b05879
HS
298/*
299 * efi_net_station_address() - set the hardware MAC address
300 *
301 * This function implements the StationAddress service of the
302 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
303 * (UEFI) specification for details.
304 *
305 * @this: pointer to the protocol instance
306 * @reset: if true reset the address to default
307 * @new_mac: new MAC address
308 * Return: status code
309 */
310static efi_status_t EFIAPI efi_net_station_address
311 (struct efi_simple_network *this, int reset,
312 struct efi_mac_address *new_mac)
0efe1bcf
AG
313{
314 EFI_ENTRY("%p, %x, %p", this, reset, new_mac);
315
61da678c 316 return EFI_EXIT(EFI_UNSUPPORTED);
0efe1bcf
AG
317}
318
41b05879
HS
319/*
320 * efi_net_statistics() - reset or collect statistics of the network interface
321 *
322 * This function implements the Statistics service of the
323 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
324 * (UEFI) specification for details.
325 *
326 * @this: pointer to the protocol instance
327 * @reset: if true, the statistics are reset
328 * @stat_size: size of the statistics table
329 * @stat_table: table to receive the statistics
330 * Return: status code
331 */
0efe1bcf
AG
332static efi_status_t EFIAPI efi_net_statistics(struct efi_simple_network *this,
333 int reset, ulong *stat_size,
334 void *stat_table)
335{
336 EFI_ENTRY("%p, %x, %p, %p", this, reset, stat_size, stat_table);
337
61da678c 338 return EFI_EXIT(EFI_UNSUPPORTED);
0efe1bcf
AG
339}
340
41b05879
HS
341/*
342 * efi_net_mcastiptomac() - translate multicast IP address to MAC address
343 *
5b4746fd 344 * This function implements the MCastIPtoMAC service of the
41b05879
HS
345 * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
346 * (UEFI) specification for details.
347 *
348 * @this: pointer to the protocol instance
349 * @ipv6: true if the IP address is an IPv6 address
350 * @ip: IP address
351 * @mac: MAC address
352 * Return: status code
353 */
0efe1bcf
AG
354static efi_status_t EFIAPI efi_net_mcastiptomac(struct efi_simple_network *this,
355 int ipv6,
356 struct efi_ip_address *ip,
357 struct efi_mac_address *mac)
358{
5b4746fd
HS
359 efi_status_t ret = EFI_SUCCESS;
360
0efe1bcf
AG
361 EFI_ENTRY("%p, %x, %p, %p", this, ipv6, ip, mac);
362
5b4746fd
HS
363 if (!this || !ip || !mac) {
364 ret = EFI_INVALID_PARAMETER;
365 goto out;
366 }
367
368 if (ipv6) {
369 ret = EFI_UNSUPPORTED;
370 goto out;
371 }
372
373 /* Multi-cast addresses are in the range 224.0.0.0 - 239.255.255.255 */
374 if ((ip->ip_addr[0] & 0xf0) != 0xe0) {
375 ret = EFI_INVALID_PARAMETER;
376 goto out;
377 };
378
379 switch (this->mode->state) {
380 case EFI_NETWORK_INITIALIZED:
381 case EFI_NETWORK_STARTED:
382 break;
383 default:
384 ret = EFI_NOT_STARTED;
385 goto out;
386 }
387
388 memset(mac, 0, sizeof(struct efi_mac_address));
389
390 /*
391 * Copy lower 23 bits of IPv4 multi-cast address
392 * RFC 1112, RFC 7042 2.1.1.
393 */
394 mac->mac_addr[0] = 0x01;
395 mac->mac_addr[1] = 0x00;
396 mac->mac_addr[2] = 0x5E;
397 mac->mac_addr[3] = ip->ip_addr[1] & 0x7F;
398 mac->mac_addr[4] = ip->ip_addr[2];
399 mac->mac_addr[5] = ip->ip_addr[3];
400out:
401 return EFI_EXIT(ret);
0efe1bcf
AG
402}
403
41b05879
HS
404/**
405 * efi_net_nvdata() - read or write NVRAM
406 *
407 * This function implements the GetStatus service of the Simple Network
408 * Protocol. See the UEFI spec for details.
409 *
410 * @this: the instance of the Simple Network Protocol
fe1a81c1 411 * @read_write: true for read, false for write
41b05879
HS
412 * @offset: offset in NVRAM
413 * @buffer_size: size of buffer
414 * @buffer: buffer
415 * Return: status code
416 */
0efe1bcf
AG
417static efi_status_t EFIAPI efi_net_nvdata(struct efi_simple_network *this,
418 int read_write, ulong offset,
419 ulong buffer_size, char *buffer)
420{
421 EFI_ENTRY("%p, %x, %lx, %lx, %p", this, read_write, offset, buffer_size,
422 buffer);
423
61da678c 424 return EFI_EXIT(EFI_UNSUPPORTED);
0efe1bcf
AG
425}
426
41b05879
HS
427/**
428 * efi_net_get_status() - get interrupt status
429 *
430 * This function implements the GetStatus service of the Simple Network
431 * Protocol. See the UEFI spec for details.
432 *
433 * @this: the instance of the Simple Network Protocol
434 * @int_status: interface status
435 * @txbuf: transmission buffer
436 */
0efe1bcf
AG
437static efi_status_t EFIAPI efi_net_get_status(struct efi_simple_network *this,
438 u32 *int_status, void **txbuf)
439{
41b05879
HS
440 efi_status_t ret = EFI_SUCCESS;
441
0efe1bcf
AG
442 EFI_ENTRY("%p, %p, %p", this, int_status, txbuf);
443
891b3d90
HS
444 efi_timer_check();
445
41b05879
HS
446 /* Check parameters */
447 if (!this) {
448 ret = EFI_INVALID_PARAMETER;
449 goto out;
450 }
451
452 switch (this->mode->state) {
453 case EFI_NETWORK_STOPPED:
454 ret = EFI_NOT_STARTED;
455 goto out;
456 case EFI_NETWORK_STARTED:
457 ret = EFI_DEVICE_ERROR;
458 goto out;
459 default:
460 break;
461 }
462
891b3d90 463 if (int_status) {
7f6d874d
HS
464 *int_status = this->int_status;
465 this->int_status = 0;
891b3d90 466 }
0efe1bcf
AG
467 if (txbuf)
468 *txbuf = new_tx_packet;
469
470 new_tx_packet = NULL;
41b05879
HS
471out:
472 return EFI_EXIT(ret);
0efe1bcf
AG
473}
474
41b05879
HS
475/**
476 * efi_net_transmit() - transmit a packet
477 *
478 * This function implements the Transmit service of the Simple Network Protocol.
479 * See the UEFI spec for details.
480 *
481 * @this: the instance of the Simple Network Protocol
482 * @header_size: size of the media header
483 * @buffer_size: size of the buffer to receive the packet
484 * @buffer: buffer to receive the packet
485 * @src_addr: source hardware MAC address
486 * @dest_addr: destination hardware MAC address
487 * @protocol: type of header to build
488 * Return: status code
489 */
490static efi_status_t EFIAPI efi_net_transmit
491 (struct efi_simple_network *this, size_t header_size,
492 size_t buffer_size, void *buffer,
493 struct efi_mac_address *src_addr,
494 struct efi_mac_address *dest_addr, u16 *protocol)
0efe1bcf 495{
41b05879
HS
496 efi_status_t ret = EFI_SUCCESS;
497
8db174d6
HS
498 EFI_ENTRY("%p, %lu, %lu, %p, %p, %p, %p", this,
499 (unsigned long)header_size, (unsigned long)buffer_size,
500 buffer, src_addr, dest_addr, protocol);
0efe1bcf 501
a0549ef6
HS
502 efi_timer_check();
503
41b05879 504 /* Check parameters */
ce54fdc4 505 if (!this || !buffer) {
41b05879
HS
506 ret = EFI_INVALID_PARAMETER;
507 goto out;
508 }
509
510 /* We do not support jumbo packets */
511 if (buffer_size > PKTSIZE_ALIGN) {
512 ret = EFI_INVALID_PARAMETER;
513 goto out;
514 }
515
5947b49b
HS
516 /* At least the IP header has to fit into the buffer */
517 if (buffer_size < this->mode->media_header_size) {
518 ret = EFI_BUFFER_TOO_SMALL;
41b05879
HS
519 goto out;
520 }
521
5947b49b
HS
522 /*
523 * TODO:
524 * Support VLANs. Use net_set_ether() for copying the header. Use a
525 * U_BOOT_ENV_CALLBACK to update the media header size.
526 */
527 if (header_size) {
528 struct ethernet_hdr *header = buffer;
529
530 if (!dest_addr || !protocol ||
531 header_size != this->mode->media_header_size) {
532 ret = EFI_INVALID_PARAMETER;
533 goto out;
534 }
535 if (!src_addr)
536 src_addr = &this->mode->current_address;
537
538 memcpy(header->et_dest, dest_addr, ARP_HLEN);
539 memcpy(header->et_src, src_addr, ARP_HLEN);
540 header->et_protlen = htons(*protocol);
541 }
542
41b05879
HS
543 switch (this->mode->state) {
544 case EFI_NETWORK_STOPPED:
545 ret = EFI_NOT_STARTED;
546 goto out;
547 case EFI_NETWORK_STARTED:
548 ret = EFI_DEVICE_ERROR;
549 goto out;
550 default:
551 break;
0efe1bcf
AG
552 }
553
41b05879 554 /* Ethernet packets always fit, just bounce */
622fe621
HS
555 memcpy(transmit_buffer, buffer, buffer_size);
556 net_send_packet(transmit_buffer, buffer_size);
712cd298 557
0efe1bcf 558 new_tx_packet = buffer;
7f6d874d 559 this->int_status |= EFI_SIMPLE_NETWORK_TRANSMIT_INTERRUPT;
41b05879
HS
560out:
561 return EFI_EXIT(ret);
0efe1bcf
AG
562}
563
41b05879
HS
564/**
565 * efi_net_receive() - receive a packet from a network interface
8db174d6
HS
566 *
567 * This function implements the Receive service of the Simple Network Protocol.
568 * See the UEFI spec for details.
569 *
41b05879
HS
570 * @this: the instance of the Simple Network Protocol
571 * @header_size: size of the media header
572 * @buffer_size: size of the buffer to receive the packet
573 * @buffer: buffer to receive the packet
574 * @src_addr: source MAC address
575 * @dest_addr: destination MAC address
576 * @protocol: protocol
577 * Return: status code
8db174d6 578 */
41b05879
HS
579static efi_status_t EFIAPI efi_net_receive
580 (struct efi_simple_network *this, size_t *header_size,
581 size_t *buffer_size, void *buffer,
582 struct efi_mac_address *src_addr,
583 struct efi_mac_address *dest_addr, u16 *protocol)
0efe1bcf 584{
41b05879 585 efi_status_t ret = EFI_SUCCESS;
336d9dfc
HS
586 struct ethernet_hdr *eth_hdr;
587 size_t hdr_size = sizeof(struct ethernet_hdr);
588 u16 protlen;
589
0efe1bcf
AG
590 EFI_ENTRY("%p, %p, %p, %p, %p, %p, %p", this, header_size,
591 buffer_size, buffer, src_addr, dest_addr, protocol);
592
41b05879 593 /* Execute events */
a0549ef6 594 efi_timer_check();
0efe1bcf 595
41b05879 596 /* Check parameters */
ce54fdc4 597 if (!this || !buffer || !buffer_size) {
41b05879
HS
598 ret = EFI_INVALID_PARAMETER;
599 goto out;
600 }
601
602 switch (this->mode->state) {
603 case EFI_NETWORK_STOPPED:
604 ret = EFI_NOT_STARTED;
605 goto out;
606 case EFI_NETWORK_STARTED:
607 ret = EFI_DEVICE_ERROR;
608 goto out;
609 default:
610 break;
611 }
612
42f804fb 613 if (!rx_packet_num) {
41b05879
HS
614 ret = EFI_NOT_READY;
615 goto out;
616 }
336d9dfc 617 /* Fill export parameters */
42f804fb 618 eth_hdr = (struct ethernet_hdr *)receive_buffer[rx_packet_idx];
336d9dfc
HS
619 protlen = ntohs(eth_hdr->et_protlen);
620 if (protlen == 0x8100) {
621 hdr_size += 4;
42f804fb 622 protlen = ntohs(*(u16 *)&receive_buffer[rx_packet_idx][hdr_size - 2]);
336d9dfc
HS
623 }
624 if (header_size)
625 *header_size = hdr_size;
626 if (dest_addr)
627 memcpy(dest_addr, eth_hdr->et_dest, ARP_HLEN);
628 if (src_addr)
629 memcpy(src_addr, eth_hdr->et_src, ARP_HLEN);
630 if (protocol)
631 *protocol = protlen;
42f804fb 632 if (*buffer_size < receive_lengths[rx_packet_idx]) {
e1fec152 633 /* Packet doesn't fit, try again with bigger buffer */
42f804fb 634 *buffer_size = receive_lengths[rx_packet_idx];
41b05879
HS
635 ret = EFI_BUFFER_TOO_SMALL;
636 goto out;
0efe1bcf 637 }
336d9dfc 638 /* Copy packet */
42f804fb
PW
639 memcpy(buffer, receive_buffer[rx_packet_idx],
640 receive_lengths[rx_packet_idx]);
641 *buffer_size = receive_lengths[rx_packet_idx];
642 rx_packet_idx = (rx_packet_idx + 1) % ETH_PACKETS_BATCH_RECV;
643 rx_packet_num--;
644 if (rx_packet_num)
645 wait_for_packet->is_signaled = true;
646 else
647 this->int_status &= ~EFI_SIMPLE_NETWORK_RECEIVE_INTERRUPT;
41b05879
HS
648out:
649 return EFI_EXIT(ret);
0efe1bcf
AG
650}
651
41b05879
HS
652/**
653 * efi_net_set_dhcp_ack() - take note of a selected DHCP IP address
654 *
655 * This function is called by dhcp_handler().
fe1a81c1
HS
656 *
657 * @pkt: packet received by dhcp_handler()
658 * @len: length of the packet received
41b05879 659 */
0efe1bcf
AG
660void efi_net_set_dhcp_ack(void *pkt, int len)
661{
662 int maxsize = sizeof(*dhcp_ack);
663
1702055e 664 if (!dhcp_ack) {
0efe1bcf 665 dhcp_ack = malloc(maxsize);
1702055e
HS
666 if (!dhcp_ack)
667 return;
668 }
669 memset(dhcp_ack, 0, maxsize);
0efe1bcf 670 memcpy(dhcp_ack, pkt, min(len, maxsize));
1702055e
HS
671
672 if (netobj)
673 netobj->pxe_mode.dhcp_ack = *dhcp_ack;
0efe1bcf
AG
674}
675
41b05879
HS
676/**
677 * efi_net_push() - callback for received network packet
678 *
679 * This function is called when a network packet is received by eth_rx().
680 *
681 * @pkt: network packet
682 * @len: length
683 */
684static void efi_net_push(void *pkt, int len)
685{
42f804fb
PW
686 int rx_packet_next;
687
688 /* Check that we at least received an Ethernet header */
689 if (len < sizeof(struct ethernet_hdr))
690 return;
691
692 /* Check that the buffer won't overflow */
693 if (len > PKTSIZE_ALIGN)
694 return;
695
696 /* Can't store more than pre-alloced buffer */
697 if (rx_packet_num >= ETH_PACKETS_BATCH_RECV)
698 return;
699
700 rx_packet_next = (rx_packet_idx + rx_packet_num) %
701 ETH_PACKETS_BATCH_RECV;
702 memcpy(receive_buffer[rx_packet_next], pkt, len);
703 receive_lengths[rx_packet_next] = len;
704
705 rx_packet_num++;
41b05879
HS
706}
707
708/**
709 * efi_network_timer_notify() - check if a new network packet has been received
a0549ef6
HS
710 *
711 * This notification function is called in every timer cycle.
712 *
fe1a81c1
HS
713 * @event: the event for which this notification function is registered
714 * @context: event context - not used in this function
a0549ef6
HS
715 */
716static void EFIAPI efi_network_timer_notify(struct efi_event *event,
717 void *context)
718{
41b05879
HS
719 struct efi_simple_network *this = (struct efi_simple_network *)context;
720
a0549ef6
HS
721 EFI_ENTRY("%p, %p", event, context);
722
41b05879
HS
723 /*
724 * Some network drivers do not support calling eth_rx() before
725 * initialization.
726 */
727 if (!this || this->mode->state != EFI_NETWORK_INITIALIZED)
728 goto out;
729
42f804fb 730 if (!rx_packet_num) {
a0549ef6
HS
731 push_packet = efi_net_push;
732 eth_rx();
733 push_packet = NULL;
42f804fb
PW
734 if (rx_packet_num) {
735 this->int_status |=
736 EFI_SIMPLE_NETWORK_RECEIVE_INTERRUPT;
737 wait_for_packet->is_signaled = true;
7f6d874d 738 }
a0549ef6 739 }
41b05879 740out:
a0549ef6
HS
741 EFI_EXIT(EFI_SUCCESS);
742}
743
a6d37098
HS
744static efi_status_t EFIAPI efi_pxe_base_code_start(
745 struct efi_pxe_base_code_protocol *this,
746 u8 use_ipv6)
747{
748 return EFI_UNSUPPORTED;
749}
750
751static efi_status_t EFIAPI efi_pxe_base_code_stop(
752 struct efi_pxe_base_code_protocol *this)
753{
754 return EFI_UNSUPPORTED;
755}
756
757static efi_status_t EFIAPI efi_pxe_base_code_dhcp(
758 struct efi_pxe_base_code_protocol *this,
759 u8 sort_offers)
760{
761 return EFI_UNSUPPORTED;
762}
763
764static efi_status_t EFIAPI efi_pxe_base_code_discover(
765 struct efi_pxe_base_code_protocol *this,
766 u16 type, u16 *layer, u8 bis,
767 struct efi_pxe_base_code_discover_info *info)
768{
769 return EFI_UNSUPPORTED;
770}
771
772static efi_status_t EFIAPI efi_pxe_base_code_mtftp(
773 struct efi_pxe_base_code_protocol *this,
774 u32 operation, void *buffer_ptr,
775 u8 overwrite, efi_uintn_t *buffer_size,
776 struct efi_ip_address server_ip, char *filename,
777 struct efi_pxe_base_code_mtftp_info *info,
778 u8 dont_use_buffer)
779{
780 return EFI_UNSUPPORTED;
781}
782
783static efi_status_t EFIAPI efi_pxe_base_code_udp_write(
784 struct efi_pxe_base_code_protocol *this,
785 u16 op_flags, struct efi_ip_address *dest_ip,
786 u16 *dest_port,
787 struct efi_ip_address *gateway_ip,
788 struct efi_ip_address *src_ip, u16 *src_port,
789 efi_uintn_t *header_size, void *header_ptr,
790 efi_uintn_t *buffer_size, void *buffer_ptr)
791{
792 return EFI_UNSUPPORTED;
793}
794
795static efi_status_t EFIAPI efi_pxe_base_code_udp_read(
796 struct efi_pxe_base_code_protocol *this,
797 u16 op_flags, struct efi_ip_address *dest_ip,
798 u16 *dest_port, struct efi_ip_address *src_ip,
799 u16 *src_port, efi_uintn_t *header_size,
800 void *header_ptr, efi_uintn_t *buffer_size,
801 void *buffer_ptr)
802{
803 return EFI_UNSUPPORTED;
804}
805
806static efi_status_t EFIAPI efi_pxe_base_code_set_ip_filter(
807 struct efi_pxe_base_code_protocol *this,
808 struct efi_pxe_base_code_filter *new_filter)
809{
810 return EFI_UNSUPPORTED;
811}
812
813static efi_status_t EFIAPI efi_pxe_base_code_arp(
814 struct efi_pxe_base_code_protocol *this,
815 struct efi_ip_address *ip_addr,
816 struct efi_mac_address *mac_addr)
817{
818 return EFI_UNSUPPORTED;
819}
820
821static efi_status_t EFIAPI efi_pxe_base_code_set_parameters(
822 struct efi_pxe_base_code_protocol *this,
823 u8 *new_auto_arp, u8 *new_send_guid,
824 u8 *new_ttl, u8 *new_tos,
825 u8 *new_make_callback)
826{
827 return EFI_UNSUPPORTED;
828}
829
830static efi_status_t EFIAPI efi_pxe_base_code_set_station_ip(
831 struct efi_pxe_base_code_protocol *this,
832 struct efi_ip_address *new_station_ip,
833 struct efi_ip_address *new_subnet_mask)
834{
835 return EFI_UNSUPPORTED;
836}
837
838static efi_status_t EFIAPI efi_pxe_base_code_set_packets(
839 struct efi_pxe_base_code_protocol *this,
840 u8 *new_dhcp_discover_valid,
841 u8 *new_dhcp_ack_received,
842 u8 *new_proxy_offer_received,
843 u8 *new_pxe_discover_valid,
844 u8 *new_pxe_reply_received,
845 u8 *new_pxe_bis_reply_received,
846 EFI_PXE_BASE_CODE_PACKET *new_dchp_discover,
847 EFI_PXE_BASE_CODE_PACKET *new_dhcp_acc,
848 EFI_PXE_BASE_CODE_PACKET *new_proxy_offer,
849 EFI_PXE_BASE_CODE_PACKET *new_pxe_discover,
850 EFI_PXE_BASE_CODE_PACKET *new_pxe_reply,
851 EFI_PXE_BASE_CODE_PACKET *new_pxe_bis_reply)
852{
853 return EFI_UNSUPPORTED;
854}
855
41b05879
HS
856/**
857 * efi_net_register() - register the simple network protocol
858 *
859 * This gets called from do_bootefi_exec().
860 */
075d425d 861efi_status_t efi_net_register(void)
0efe1bcf 862{
a0549ef6 863 efi_status_t r;
42f804fb 864 int i;
0efe1bcf
AG
865
866 if (!eth_get_dev()) {
e1fec152 867 /* No network device active, don't expose any */
075d425d 868 return EFI_SUCCESS;
0efe1bcf
AG
869 }
870
e1fec152 871 /* We only expose the "active" network device, so one is enough */
0efe1bcf 872 netobj = calloc(1, sizeof(*netobj));
622fe621
HS
873 if (!netobj)
874 goto out_of_resources;
875
876 /* Allocate an aligned transmit buffer */
877 transmit_buffer = calloc(1, PKTSIZE_ALIGN + PKTALIGN);
878 if (!transmit_buffer)
879 goto out_of_resources;
880 transmit_buffer = (void *)ALIGN((uintptr_t)transmit_buffer, PKTALIGN);
84d14568 881
42f804fb
PW
882 /* Allocate a number of receive buffers */
883 receive_buffer = calloc(ETH_PACKETS_BATCH_RECV,
884 sizeof(*receive_buffer));
885 if (!receive_buffer)
886 goto out_of_resources;
887 for (i = 0; i < ETH_PACKETS_BATCH_RECV; i++) {
888 receive_buffer[i] = malloc(PKTSIZE_ALIGN);
889 if (!receive_buffer[i])
890 goto out_of_resources;
891 }
892 receive_lengths = calloc(ETH_PACKETS_BATCH_RECV,
893 sizeof(*receive_lengths));
894 if (!receive_lengths)
895 goto out_of_resources;
896
84d14568 897 /* Hook net up to the device list */
d39646a3 898 efi_add_handle(&netobj->header);
0efe1bcf
AG
899
900 /* Fill in object data */
d39646a3 901 r = efi_add_protocol(&netobj->header, &efi_net_guid,
84d14568
HS
902 &netobj->net);
903 if (r != EFI_SUCCESS)
075d425d 904 goto failure_to_add_protocol;
d39646a3 905 r = efi_add_protocol(&netobj->header, &efi_guid_device_path,
84d14568
HS
906 efi_dp_from_eth());
907 if (r != EFI_SUCCESS)
075d425d 908 goto failure_to_add_protocol;
a6d37098 909 r = efi_add_protocol(&netobj->header, &efi_pxe_base_code_protocol_guid,
84d14568
HS
910 &netobj->pxe);
911 if (r != EFI_SUCCESS)
075d425d 912 goto failure_to_add_protocol;
bdecf974 913 netobj->net.revision = EFI_SIMPLE_NETWORK_PROTOCOL_REVISION;
0efe1bcf
AG
914 netobj->net.start = efi_net_start;
915 netobj->net.stop = efi_net_stop;
916 netobj->net.initialize = efi_net_initialize;
917 netobj->net.reset = efi_net_reset;
918 netobj->net.shutdown = efi_net_shutdown;
919 netobj->net.receive_filters = efi_net_receive_filters;
920 netobj->net.station_address = efi_net_station_address;
921 netobj->net.statistics = efi_net_statistics;
922 netobj->net.mcastiptomac = efi_net_mcastiptomac;
923 netobj->net.nvdata = efi_net_nvdata;
924 netobj->net.get_status = efi_net_get_status;
925 netobj->net.transmit = efi_net_transmit;
926 netobj->net.receive = efi_net_receive;
927 netobj->net.mode = &netobj->net_mode;
72a8f168 928 netobj->net_mode.state = EFI_NETWORK_STOPPED;
0efe1bcf 929 memcpy(netobj->net_mode.current_address.mac_addr, eth_get_ethaddr(), 6);
5d4a5ea9 930 netobj->net_mode.hwaddr_size = ARP_HLEN;
5947b49b 931 netobj->net_mode.media_header_size = ETHER_HDR_SIZE;
0efe1bcf 932 netobj->net_mode.max_packet_size = PKTSIZE;
f25ddca1 933 netobj->net_mode.if_type = ARP_ETHER;
0efe1bcf 934
a6d37098
HS
935 netobj->pxe.revision = EFI_PXE_BASE_CODE_PROTOCOL_REVISION;
936 netobj->pxe.start = efi_pxe_base_code_start;
937 netobj->pxe.stop = efi_pxe_base_code_stop;
938 netobj->pxe.dhcp = efi_pxe_base_code_dhcp;
939 netobj->pxe.discover = efi_pxe_base_code_discover;
940 netobj->pxe.mtftp = efi_pxe_base_code_mtftp;
941 netobj->pxe.udp_write = efi_pxe_base_code_udp_write;
942 netobj->pxe.udp_read = efi_pxe_base_code_udp_read;
943 netobj->pxe.set_ip_filter = efi_pxe_base_code_set_ip_filter;
944 netobj->pxe.arp = efi_pxe_base_code_arp;
945 netobj->pxe.set_parameters = efi_pxe_base_code_set_parameters;
946 netobj->pxe.set_station_ip = efi_pxe_base_code_set_station_ip;
947 netobj->pxe.set_packets = efi_pxe_base_code_set_packets;
0efe1bcf
AG
948 netobj->pxe.mode = &netobj->pxe_mode;
949 if (dhcp_ack)
950 netobj->pxe_mode.dhcp_ack = *dhcp_ack;
951
e5c21603
HS
952 /*
953 * Create WaitForPacket event.
954 */
955 r = efi_create_event(EVT_NOTIFY_WAIT, TPL_CALLBACK,
b095f3c8 956 efi_network_timer_notify, NULL, NULL,
e5c21603
HS
957 &wait_for_packet);
958 if (r != EFI_SUCCESS) {
959 printf("ERROR: Failed to register network event\n");
960 return r;
961 }
962 netobj->net.wait_for_packet = wait_for_packet;
a0549ef6
HS
963 /*
964 * Create a timer event.
965 *
966 * The notification function is used to check if a new network packet
967 * has been received.
ee3db4fc
HS
968 *
969 * iPXE is running at TPL_CALLBACK most of the time. Use a higher TPL.
a0549ef6 970 */
ee3db4fc 971 r = efi_create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_NOTIFY,
41b05879 972 efi_network_timer_notify, &netobj->net, NULL,
a0549ef6
HS
973 &network_timer_event);
974 if (r != EFI_SUCCESS) {
975 printf("ERROR: Failed to register network event\n");
976 return r;
977 }
e1fec152 978 /* Network is time critical, create event in every timer cycle */
a0549ef6
HS
979 r = efi_set_timer(network_timer_event, EFI_TIMER_PERIODIC, 0);
980 if (r != EFI_SUCCESS) {
981 printf("ERROR: Failed to set network timer\n");
982 return r;
983 }
984
075d425d
HS
985 return EFI_SUCCESS;
986failure_to_add_protocol:
987 printf("ERROR: Failure to add protocol\n");
988 return r;
622fe621
HS
989out_of_resources:
990 free(netobj);
1702055e 991 netobj = NULL;
42f804fb
PW
992 free(transmit_buffer);
993 if (receive_buffer)
994 for (i = 0; i < ETH_PACKETS_BATCH_RECV; i++)
995 free(receive_buffer[i]);
996 free(receive_buffer);
997 free(receive_lengths);
622fe621
HS
998 printf("ERROR: Out of memory\n");
999 return EFI_OUT_OF_RESOURCES;
0efe1bcf 1000}
This page took 0.356714 seconds and 4 git commands to generate.