]> git.proxmox.com Git - mirror_edk2.git/blob - UnixPkg/UnixSnpDxe/UnixSnp.c
Fix issue with fixing tabs.
[mirror_edk2.git] / UnixPkg / UnixSnpDxe / UnixSnp.c
1 /** @file
2
3 Copyright (c) 2010, Apple, Inc. All rights reserved.<BR>
4
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 Module Name:
14
15 UnixSnp.c
16
17 Abstract:
18
19 -**/
20
21 #include <Library/PrintLib.h>
22
23 #include "UnixSnp.h"
24
25 EFI_DRIVER_BINDING_PROTOCOL gUnixSnpDriverBinding =
26 {
27 UnixSnpDriverBindingSupported,
28 UnixSnpDriverBindingStart,
29 UnixSnpDriverBindingStop,
30 0xA,
31 NULL,
32 NULL
33 };
34
35 /**
36 Changes the state of a network interface from "stopped" to "started".
37
38 @param This Protocol instance pointer.
39
40 @retval EFI_SUCCESS Always succeeds.
41
42 **/
43 EFI_STATUS
44 EFIAPI
45 UnixSnpStart(
46 IN EFI_SIMPLE_NETWORK_PROTOCOL* This
47 );
48
49 /**
50 Changes the state of a network interface from "started" to "stopped".
51
52 @param This Protocol instance pointer.
53
54 @retval EFI_SUCCESS Always succeeds.
55
56 **/
57 EFI_STATUS
58 EFIAPI
59 UnixSnpStop(
60 IN EFI_SIMPLE_NETWORK_PROTOCOL* This
61 );
62
63 /**
64 Resets a network adapter and allocates the transmit and receive buffers
65 required by the network interface; optionally, also requests allocation
66 of additional transmit and receive buffers.
67
68 @param This Protocol instance pointer.
69 @param ExtraRxBufferSize The size, in bytes, of the extra receive buffer space
70 that the driver should allocate for the network interface.
71 Some network interfaces will not be able to use the extra
72 buffer, and the caller will not know if it is actually
73 being used.
74 @param ExtraTxBufferSize The size, in bytes, of the extra transmit buffer space
75 that the driver should allocate for the network interface.
76 Some network interfaces will not be able to use the extra
77 buffer, and the caller will not know if it is actually
78 being used.
79
80 @retval EFI_SUCCESS Always succeeds.
81
82 **/
83 EFI_STATUS
84 EFIAPI
85 UnixSnpInitialize(
86 IN EFI_SIMPLE_NETWORK_PROTOCOL* This,
87 IN UINTN ExtraRxBufferSize OPTIONAL,
88 IN UINTN ExtraTxBufferSize OPTIONAL
89 );
90
91 /**
92 Resets a network adapter and re-initializes it with the parameters that were
93 provided in the previous call to Initialize().
94
95 @param This Protocol instance pointer.
96 @param ExtendedVerification Indicates that the driver may perform a more
97 exhaustive verification operation of the device
98 during reset.
99
100 @retval EFI_SUCCESS Always succeeds.
101
102 **/
103 EFI_STATUS
104 EFIAPI
105 UnixSnpReset(
106 IN EFI_SIMPLE_NETWORK_PROTOCOL* This,
107 IN BOOLEAN ExtendedVerification
108 );
109
110 /**
111 Resets a network adapter and leaves it in a state that is safe for
112 another driver to initialize.
113
114 @param This Protocol instance pointer.
115
116 @retval EFI_SUCCESS Always succeeds.
117
118 **/
119 EFI_STATUS
120 EFIAPI
121 UnixSnpShutdown(
122 IN EFI_SIMPLE_NETWORK_PROTOCOL* This
123 );
124
125 /**
126 Manages the multicast receive filters of a network interface.
127
128 @param This Protocol instance pointer.
129 @param EnableBits A bit mask of receive filters to enable on the network interface.
130 @param DisableBits A bit mask of receive filters to disable on the network interface.
131 @param ResetMcastFilter Set to TRUE to reset the contents of the multicast receive
132 filters on the network interface to their default values.
133 @param McastFilterCount Number of multicast HW MAC addresses in the new
134 MCastFilter list. This value must be less than or equal to
135 the MCastFilterCnt field of EFI_SIMPLE_NETWORK_MODE. This
136 field is optional if ResetMCastFilter is TRUE.
137 @param McastFilter A pointer to a list of new multicast receive filter HW MAC
138 addresses. This list will replace any existing multicast
139 HW MAC address list. This field is optional if
140 ResetMCastFilter is TRUE.
141
142 @retval EFI_SUCCESS The multicast receive filter list was updated.
143 @retval EFI_DEVICE_ERROR The command could not be sent to the network interface.
144
145 **/
146 EFI_STATUS
147 EFIAPI
148 UnixSnpReceiveFilters(
149 IN EFI_SIMPLE_NETWORK_PROTOCOL* This,
150 IN UINT32 EnableBits,
151 IN UINT32 DisableBits,
152 IN BOOLEAN ResetMcastFilter,
153 IN UINTN McastFilterCount OPTIONAL,
154 IN EFI_MAC_ADDRESS* McastFilter OPTIONAL
155 );
156
157 /**
158 Modifies or resets the current station address, if supported.
159
160 @param This Protocol instance pointer.
161 @param Reset Flag used to reset the station address to the network interfaces
162 permanent address.
163 @param NewMacAddr New station address to be used for the network interface.
164
165 @retval EFI_UNSUPPORTED Not supported yet.
166
167 **/
168 EFI_STATUS
169 EFIAPI
170 UnixSnpStationAddress(
171 IN EFI_SIMPLE_NETWORK_PROTOCOL* This,
172 IN BOOLEAN Reset,
173 IN EFI_MAC_ADDRESS* NewMacAddr OPTIONAL
174 );
175
176 /**
177 Resets or collects the statistics on a network interface.
178
179 @param This Protocol instance pointer.
180 @param Reset Set to TRUE to reset the statistics for the network interface.
181 @param StatisticsSize On input the size, in bytes, of StatisticsTable. On
182 output the size, in bytes, of the resulting table of
183 statistics.
184 @param StatisticsTable A pointer to the EFI_NETWORK_STATISTICS structure that
185 contains the statistics.
186
187 @retval EFI_SUCCESS The statistics were collected from the network interface.
188 @retval EFI_NOT_STARTED The network interface has not been started.
189 @retval EFI_BUFFER_TOO_SMALL The Statistics buffer was too small. The current buffer
190 size needed to hold the statistics is returned in
191 StatisticsSize.
192 @retval EFI_UNSUPPORTED Not supported yet.
193
194 **/
195 EFI_STATUS
196 EFIAPI
197 UnixSnpStatistics(
198 IN EFI_SIMPLE_NETWORK_PROTOCOL* This,
199 IN BOOLEAN Reset,
200 IN OUT UINTN* StatisticsSize OPTIONAL,
201 OUT EFI_NETWORK_STATISTICS* StatisticsTable OPTIONAL
202 );
203
204 /**
205 Converts a multicast IP address to a multicast HW MAC address.
206
207 @param This Protocol instance pointer.
208 @param Ipv6 Set to TRUE if the multicast IP address is IPv6 [RFC 2460]. Set
209 to FALSE if the multicast IP address is IPv4 [RFC 791].
210 @param Ip The multicast IP address that is to be converted to a multicast
211 HW MAC address.
212 @param Mac The multicast HW MAC address that is to be generated from IP.
213
214 @retval EFI_SUCCESS The multicast IP address was mapped to the multicast
215 HW MAC address.
216 @retval EFI_NOT_STARTED The network interface has not been started.
217 @retval EFI_BUFFER_TOO_SMALL The Statistics buffer was too small. The current buffer
218 size needed to hold the statistics is returned in
219 StatisticsSize.
220 @retval EFI_UNSUPPORTED Not supported yet.
221
222 **/
223 EFI_STATUS
224 EFIAPI
225 UnixSnpMcastIptoMac(
226 IN EFI_SIMPLE_NETWORK_PROTOCOL* This,
227 IN BOOLEAN Ipv6,
228 IN EFI_IP_ADDRESS* Ip,
229 OUT EFI_MAC_ADDRESS* Mac
230 );
231
232 /**
233 Performs read and write operations on the NVRAM device attached to a
234 network interface.
235
236 @param This Protocol instance pointer.
237 @param ReadOrWrite TRUE for read operations, FALSE for write operations.
238 @param Offset Byte offset in the NVRAM device at which to start the read or
239 write operation. This must be a multiple of NvRamAccessSize and
240 less than NvRamSize.
241 @param BufferSize The number of bytes to read or write from the NVRAM device.
242 This must also be a multiple of NvramAccessSize.
243 @param Buffer A pointer to the data buffer.
244
245 @retval EFI_UNSUPPORTED Not supported yet.
246
247 **/
248 EFI_STATUS
249 EFIAPI
250 UnixSnpNvdata(
251 IN EFI_SIMPLE_NETWORK_PROTOCOL* This,
252 IN BOOLEAN ReadOrWrite,
253 IN UINTN Offset,
254 IN UINTN BufferSize,
255 IN OUT VOID* Buffer
256 );
257
258 /**
259 Reads the current interrupt status and recycled transmit buffer status from
260 a network interface.
261
262 @param This Protocol instance pointer.
263 @param InterruptStatus A pointer to the bit mask of the currently active interrupts
264 If this is NULL, the interrupt status will not be read from
265 the device. If this is not NULL, the interrupt status will
266 be read from the device. When the interrupt status is read,
267 it will also be cleared. Clearing the transmit interrupt
268 does not empty the recycled transmit buffer array.
269 @param TxBuffer Recycled transmit buffer address. The network interface will
270 not transmit if its internal recycled transmit buffer array
271 is full. Reading the transmit buffer does not clear the
272 transmit interrupt. If this is NULL, then the transmit buffer
273 status will not be read. If there are no transmit buffers to
274 recycle and TxBuf is not NULL, * TxBuf will be set to NULL.
275
276 @retval EFI_SUCCESS Always succeeds.
277
278 **/
279 EFI_STATUS
280 EFIAPI
281 UnixSnpGetStatus(
282 IN EFI_SIMPLE_NETWORK_PROTOCOL* This,
283 OUT UINT32* InterruptStatus,
284 OUT VOID** TxBuffer
285 );
286
287 /**
288 Places a packet in the transmit queue of a network interface.
289
290 @param This Protocol instance pointer.
291 @param HeaderSize The size, in bytes, of the media header to be filled in by
292 the Transmit() function. If HeaderSize is non-zero, then it
293 must be equal to This->Mode->MediaHeaderSize and the DestAddr
294 and Protocol parameters must not be NULL.
295 @param BufferSize The size, in bytes, of the entire packet (media header and
296 data) to be transmitted through the network interface.
297 @param Buffer A pointer to the packet (media header followed by data) to be
298 transmitted. This parameter cannot be NULL. If HeaderSize is zero,
299 then the media header in Buffer must already be filled in by the
300 caller. If HeaderSize is non-zero, then the media header will be
301 filled in by the Transmit() function.
302 @param SrcAddr The source HW MAC address. If HeaderSize is zero, then this parameter
303 is ignored. If HeaderSize is non-zero and SrcAddr is NULL, then
304 This->Mode->CurrentAddress is used for the source HW MAC address.
305 @param DestAddr The destination HW MAC address. If HeaderSize is zero, then this
306 parameter is ignored.
307 @param Protocol The type of header to build. If HeaderSize is zero, then this
308 parameter is ignored. See RFC 1700, section "Ether Types", for
309 examples.
310
311 @retval EFI_SUCCESS The packet was placed on the transmit queue.
312 @retval EFI_DEVICE_ERROR The command could not be sent to the network interface.
313 @retval EFI_ACCESS_DENIED Error acquire global lock for operation.
314
315 **/
316 EFI_STATUS
317 EFIAPI
318 UnixSnpTransmit(
319 IN EFI_SIMPLE_NETWORK_PROTOCOL* This,
320 IN UINTN HeaderSize,
321 IN UINTN BufferSize,
322 IN VOID* Buffer,
323 IN EFI_MAC_ADDRESS* SrcAddr OPTIONAL,
324 IN EFI_MAC_ADDRESS* DestAddr OPTIONAL,
325 IN UINT16* Protocol OPTIONAL
326 );
327
328 /**
329 Receives a packet from a network interface.
330
331 @param This Protocol instance pointer.
332 @param HeaderSize The size, in bytes, of the media header received on the network
333 interface. If this parameter is NULL, then the media header size
334 will not be returned.
335 @param BuffSize On entry, the size, in bytes, of Buffer. On exit, the size, in
336 bytes, of the packet that was received on the network interface.
337 @param Buffer A pointer to the data buffer to receive both the media header and
338 the data.
339 @param SourceAddr The source HW MAC address. If this parameter is NULL, the
340 HW MAC source address will not be extracted from the media
341 header.
342 @param DestinationAddr The destination HW MAC address. If this parameter is NULL,
343 the HW MAC destination address will not be extracted from the
344 media header.
345 @param Protocol The media header type. If this parameter is NULL, then the
346 protocol will not be extracted from the media header. See
347 RFC 1700 section "Ether Types" for examples.
348
349 @retval EFI_SUCCESS The received data was stored in Buffer, and BufferSize has
350 been updated to the number of bytes received.
351 @retval EFI_NOT_READY The network interface is too busy to accept this transmit
352 request.
353 @retval EFI_BUFFER_TOO_SMALL The BufferSize parameter is too small.
354 @retval EFI_DEVICE_ERROR The command could not be sent to the network interface.
355 @retval EFI_ACCESS_DENIED Error acquire global lock for operation.
356
357 **/
358 EFI_STATUS
359 EFIAPI
360 UnixSnpReceive(
361 IN EFI_SIMPLE_NETWORK_PROTOCOL* This,
362 OUT UINTN* HeaderSize OPTIONAL,
363 IN OUT UINTN* BuffSize,
364 OUT VOID* Buffer,
365 OUT EFI_MAC_ADDRESS* SourceAddr OPTIONAL,
366 OUT EFI_MAC_ADDRESS* DestinationAddr OPTIONAL,
367 OUT UINT16* Protocol OPTIONAL
368 );
369
370 VOID
371 EFIAPI
372 UnixSnpWaitForPacketNotify(
373 IN EFI_EVENT Event,
374 IN VOID* Private
375 );
376
377 //
378 // Strange, but there doesn't appear to be any structure for the Ethernet header in edk2...
379 //
380
381 typedef struct
382 {
383 UINT8 DstAddr[ NET_ETHER_ADDR_LEN ];
384 UINT8 SrcAddr[ NET_ETHER_ADDR_LEN ];
385 UINT16 Type;
386 } EthernetHeader;
387
388 UNIX_SNP_PRIVATE_DATA gUnixSnpPrivateTemplate =
389 {
390 UNIX_SNP_PRIVATE_DATA_SIGNATURE, // Signature
391 NULL, // UnixThunk
392 NULL, // DeviceHandle
393 NULL, // DevicePath
394 { 0 }, // MacAddress
395 NULL, // InterfaceName
396 0, // ReadBufferSize
397 NULL, // ReadBuffer
398 NULL, // CurrentReadPointer
399 NULL, // EndReadPointer
400 0, // BpfFd
401 { // Snp
402 EFI_SIMPLE_NETWORK_PROTOCOL_REVISION, // Revision
403 UnixSnpStart, // Start
404 UnixSnpStop, // Stop
405 UnixSnpInitialize, // Initialize
406 UnixSnpReset, // Reset
407 UnixSnpShutdown, // Shutdown
408 UnixSnpReceiveFilters, // ReceiveFilters
409 UnixSnpStationAddress, // StationAddress
410 UnixSnpStatistics, // Statistics
411 UnixSnpMcastIptoMac, // MCastIpToMac
412 UnixSnpNvdata, // NvData
413 UnixSnpGetStatus, // GetStatus
414 UnixSnpTransmit, // Transmit
415 UnixSnpReceive, // Receive
416 NULL, // WaitForPacket
417 NULL // Mode
418 },
419 { // Mode
420 EfiSimpleNetworkStopped, // State
421 NET_ETHER_ADDR_LEN, // HwAddressSize
422 NET_ETHER_HEADER_SIZE, // MediaHeaderSize
423 1500, // MaxPacketSize
424 0, // NvRamSize
425 0, // NvRamAccessSize
426 0, // ReceiveFilterMask
427 0, // ReceiveFilterSetting
428 MAX_MCAST_FILTER_CNT, // MaxMCastFilterCount
429 0, // MCastFilterCount
430 {
431 0
432 }, // MCastFilter
433 {
434 0
435 }, // CurrentAddress
436 {
437 0
438 }, // BroadcastAddress
439 {
440 0
441 }, // PermanentAddress
442 NET_IFTYPE_ETHERNET, // IfType
443 FALSE, // MacAddressChangeable
444 FALSE, // MultipleTxSupported
445 FALSE, // MediaPresentSupported
446 TRUE // MediaPresent
447 }
448 };
449
450 STATIC EFI_STATUS
451 GetInterfaceMacAddr(
452 IN UNIX_SNP_PRIVATE_DATA* Private,
453 IN EFI_UNIX_IO_PROTOCOL* UnixIo
454 )
455 {
456 struct ifaddrs* IfAddrs;
457 struct ifaddrs* If;
458 struct sockaddr_dl* IfSdl;
459 EFI_STATUS Status;
460 INTN Result;
461
462 Result = UnixIo->UnixThunk->GetIfAddrs( &IfAddrs );
463 if ( Result != 0 )
464 {
465 return( EFI_UNSUPPORTED );
466 }
467
468 //
469 // Convert the interface name to ASCII so we can find it.
470 //
471 Private->InterfaceName = AllocateZeroPool( StrLen( UnixIo->EnvString ) );
472
473 if ( !Private->InterfaceName )
474 {
475 Status = EFI_OUT_OF_RESOURCES;
476 goto Exit;
477 }
478
479 UnicodeStrToAsciiStr( UnixIo->EnvString, Private->InterfaceName );
480
481 If = IfAddrs;
482
483 while ( If != NULL )
484 {
485 IfSdl = ( struct sockaddr_dl * ) If->ifa_addr;
486
487 if ( IfSdl->sdl_family == AF_LINK )
488 {
489 if ( !AsciiStrCmp( Private->InterfaceName, If->ifa_name ) )
490 {
491 CopyMem( &Private->MacAddress, LLADDR( IfSdl ), NET_ETHER_ADDR_LEN );
492
493 Status = EFI_SUCCESS;
494 break;
495 }
496 }
497
498 If = If->ifa_next;
499 }
500
501 Exit:
502 ( VOID ) UnixIo->UnixThunk->FreeIfAddrs( IfAddrs );
503
504 return( Status );
505 }
506
507
508 STATIC EFI_STATUS
509 OpenBpfFileDescriptor(
510 IN UNIX_SNP_PRIVATE_DATA* Private,
511 OUT INTN* Fd
512 )
513 {
514 CHAR8 BfpDeviceName[ 256 ];
515 INTN Index;
516 EFI_STATUS Status = EFI_OUT_OF_RESOURCES;
517 INTN Result;
518
519 //
520 // Open a Berkeley Packet Filter device. This must be done as root, so this is probably
521 // the place which is most likely to fail...
522 //
523 for ( Index = 0; TRUE; Index++ )
524 {
525 AsciiSPrint( BfpDeviceName, sizeof( BfpDeviceName ), "/dev/bpf%d", Index );
526
527 *Fd = Private->UnixThunk->Open( BfpDeviceName, O_RDWR, 0 );
528
529 if ( *Fd >= 0 )
530 {
531 Status = EFI_SUCCESS;
532 break;
533 }
534
535 Result = Private->UnixThunk->GetErrno();
536 if ( Result == EACCES )
537 {
538 DEBUG( ( EFI_D_ERROR, "Permissions on '%a' are incorrect. Fix with 'sudo chmod 666 %a'.\n",
539 BfpDeviceName, BfpDeviceName ) );
540 }
541 if ( Result != EBUSY )
542 {
543 break;
544 }
545 }
546
547 return( Status );
548 }
549
550
551 /**
552 Test to see if this driver supports ControllerHandle. This service
553 is called by the EFI boot service ConnectController(). In
554 order to make drivers as small as possible, there are a few calling
555 restrictions for this service. ConnectController() must
556 follow these calling restrictions. If any other agent wishes to call
557 Supported() it must also follow these calling restrictions.
558
559 @param This Protocol instance pointer.
560 @param ControllerHandle Handle of device to test
561 @param RemainingDevicePath Optional parameter use to pick a specific child
562 device to start.
563
564 @retval EFI_SUCCESS This driver supports this device
565 @retval EFI_UNSUPPORTED This driver does not support this device
566
567 **/
568 EFI_STATUS
569 EFIAPI
570 UnixSnpDriverBindingSupported(
571 IN EFI_DRIVER_BINDING_PROTOCOL* This,
572 IN EFI_HANDLE ControllerHandle,
573 IN EFI_DEVICE_PATH_PROTOCOL* RemainingDevicePath OPTIONAL
574 )
575 {
576 EFI_STATUS Status;
577 EFI_UNIX_IO_PROTOCOL* UnixIo;
578
579 //
580 // Open the I/O abstraction needed to perform the supported test.
581 //
582 Status = gBS->OpenProtocol(
583 ControllerHandle,
584 &gEfiUnixIoProtocolGuid,
585 ( VOID ** ) &UnixIo,
586 This->DriverBindingHandle,
587 ControllerHandle,
588 EFI_OPEN_PROTOCOL_BY_DRIVER
589 );
590
591 if ( EFI_ERROR( Status ) )
592 {
593 return( Status );
594 }
595
596 //
597 // Validate GUID
598 //
599 Status = EFI_UNSUPPORTED;
600 if ( CompareGuid( UnixIo->TypeGuid, &gEfiUnixNetworkGuid ) )
601 {
602 Status = EFI_SUCCESS;
603 }
604
605 //
606 // Close the I/O abstraction used to perform the supported test.
607 //
608 gBS->CloseProtocol(
609 ControllerHandle,
610 &gEfiUnixIoProtocolGuid,
611 This->DriverBindingHandle,
612 ControllerHandle
613 );
614
615 return( Status );
616 }
617
618
619 /**
620 Start this driver on ControllerHandle. This service is called by the
621 EFI boot service ConnectController(). In order to make
622 drivers as small as possible, there are a few calling restrictions for
623 this service. ConnectController() must follow these
624 calling restrictions. If any other agent wishes to call Start() it
625 must also follow these calling restrictions.
626
627 @param This Protocol instance pointer.
628 @param ControllerHandle Handle of device to bind driver to
629 @param RemainingDevicePath Optional parameter use to pick a specific child
630 device to start.
631
632 @retval EFI_SUCCESS Always succeeds.
633
634 **/
635 EFI_STATUS
636 EFIAPI
637 UnixSnpDriverBindingStart(
638 IN EFI_DRIVER_BINDING_PROTOCOL* This,
639 IN EFI_HANDLE ControllerHandle,
640 IN EFI_DEVICE_PATH_PROTOCOL* RemainingDevicePath OPTIONAL
641 )
642 {
643 MAC_ADDR_DEVICE_PATH Node;
644 EFI_DEVICE_PATH_PROTOCOL* ParentDevicePath = NULL;
645 EFI_UNIX_IO_PROTOCOL* UnixIo;
646 UNIX_SNP_PRIVATE_DATA* Private = NULL;
647 EFI_STATUS Status;
648 BOOLEAN CreateDevice;
649
650 //
651 // Grab the protocols we need.
652 //
653 Status = gBS->OpenProtocol(
654 ControllerHandle,
655 &gEfiDevicePathProtocolGuid,
656 ( VOID ** ) &ParentDevicePath,
657 This->DriverBindingHandle,
658 ControllerHandle,
659 EFI_OPEN_PROTOCOL_BY_DRIVER
660 );
661 if ( EFI_ERROR( Status ) )
662 {
663 goto ErrorExit;
664 }
665
666 //
667 // Open the I/O abstraction needed to perform the supported test.
668 //
669 Status = gBS->OpenProtocol(
670 ControllerHandle,
671 &gEfiUnixIoProtocolGuid,
672 ( VOID ** ) &UnixIo,
673 This->DriverBindingHandle,
674 ControllerHandle,
675 EFI_OPEN_PROTOCOL_BY_DRIVER
676 );
677 if ( EFI_ERROR( Status ) )
678 {
679 goto ErrorExit;
680 }
681
682 //
683 // Validate GUID
684 //
685 if ( !CompareGuid( UnixIo->TypeGuid, &gEfiUnixNetworkGuid ) )
686 {
687 Status = EFI_UNSUPPORTED;
688 goto ErrorExit;
689 }
690
691 CreateDevice = TRUE;
692 if ( ( RemainingDevicePath != NULL ) && IsDevicePathEnd( RemainingDevicePath ) )
693 {
694 CreateDevice = FALSE;
695 }
696
697 if ( CreateDevice )
698 {
699 //
700 // Allocate the private data.
701 //
702 Private = AllocateCopyPool( sizeof( UNIX_SNP_PRIVATE_DATA ), &gUnixSnpPrivateTemplate );
703 if ( Private == NULL )
704 {
705 Status = EFI_OUT_OF_RESOURCES;
706 goto ErrorExit;
707 }
708
709 Status = GetInterfaceMacAddr( Private, UnixIo );
710 if ( EFI_ERROR( Status ) )
711 {
712 goto ErrorExit;
713 }
714
715 Private->UnixThunk = UnixIo->UnixThunk;
716
717 Private->Snp.Mode = &Private->Mode;
718
719 //
720 // Set the broadcast address.
721 //
722 SetMem( &Private->Mode.BroadcastAddress, sizeof( EFI_MAC_ADDRESS ), 0xFF );
723
724 CopyMem( &Private->Mode.CurrentAddress, &Private->MacAddress, sizeof( EFI_MAC_ADDRESS ) );
725 CopyMem( &Private->Mode.PermanentAddress, &Private->MacAddress, sizeof( EFI_MAC_ADDRESS ) );
726
727 //
728 // Since the fake SNP is based on a real NIC, to avoid conflict with the host NIC
729 // network stack, we use a different MAC address.
730 // So just change the last byte of the MAC address for the real NIC.
731 //
732 Private->Mode.CurrentAddress.Addr[ NET_ETHER_ADDR_LEN - 1 ]++;
733
734 //
735 // Build the device path by appending the MAC node to the ParentDevicePath
736 // from the UnixIo handle.
737 //
738 ZeroMem( &Node, sizeof( MAC_ADDR_DEVICE_PATH ) );
739
740 Node.Header.Type = MESSAGING_DEVICE_PATH;
741 Node.Header.SubType = MSG_MAC_ADDR_DP;
742 Node.IfType = Private->Mode.IfType;
743
744 SetDevicePathNodeLength( ( EFI_DEVICE_PATH_PROTOCOL * ) &Node, sizeof( MAC_ADDR_DEVICE_PATH ) );
745
746 CopyMem( &Node.MacAddress, &Private->Mode.CurrentAddress, sizeof( EFI_MAC_ADDRESS ) );
747
748 //
749 // Build the device path by appending the MAC node to the ParentDevicePath from the UnixIo handle.
750 //
751 Private->DevicePath = AppendDevicePathNode( ParentDevicePath, ( EFI_DEVICE_PATH_PROTOCOL * ) &Node );
752 if ( Private->DevicePath == NULL )
753 {
754 Status = EFI_OUT_OF_RESOURCES;
755 goto ErrorExit;
756 }
757
758 Status = gBS->InstallMultipleProtocolInterfaces(
759 &Private->DeviceHandle,
760 &gEfiSimpleNetworkProtocolGuid,
761 &Private->Snp,
762 &gEfiDevicePathProtocolGuid,
763 Private->DevicePath,
764 NULL
765 );
766 if ( EFI_ERROR( Status ) )
767 {
768 goto ErrorExit;
769 }
770
771 Status = gBS->OpenProtocol(
772 ControllerHandle,
773 &gEfiUnixIoProtocolGuid,
774 ( VOID ** ) &UnixIo,
775 This->DriverBindingHandle,
776 Private->DeviceHandle,
777 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
778 );
779 if ( EFI_ERROR( Status ) )
780 {
781 goto ErrorExit;
782 }
783 }
784 return( Status );
785
786 ErrorExit:
787 if ( Private->InterfaceName != NULL )
788 {
789 FreePool( Private->InterfaceName );
790 Private->InterfaceName = NULL;
791 }
792 if ( Private != NULL )
793 {
794 FreePool( Private );
795 }
796 if ( ParentDevicePath != NULL )
797 {
798 gBS->CloseProtocol(
799 ControllerHandle,
800 &gEfiDevicePathProtocolGuid,
801 This->DriverBindingHandle,
802 ControllerHandle
803 );
804 }
805
806 return( Status );
807 }
808
809 /**
810 Stop this driver on ControllerHandle. This service is called by the
811 EFI boot service DisconnectController(). In order to
812 make drivers as small as possible, there are a few calling
813 restrictions for this service. DisconnectController()
814 must follow these calling restrictions. If any other agent wishes
815 to call Stop() it must also follow these calling restrictions.
816
817 @param This Protocol instance pointer.
818 @param ControllerHandle Handle of device to stop driver on
819 @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
820 children is zero stop the entire bus driver.
821 @param ChildHandleBuffer List of Child Handles to Stop.
822
823 @retval EFI_SUCCESS Always succeeds.
824
825 **/
826 EFI_STATUS
827 EFIAPI
828 UnixSnpDriverBindingStop(
829 IN EFI_DRIVER_BINDING_PROTOCOL* This,
830 IN EFI_HANDLE ControllerHandle,
831 IN UINTN NumberOfChildren,
832 IN EFI_HANDLE* ChildHandleBuffer
833 )
834 {
835 UNIX_SNP_PRIVATE_DATA* Private = NULL;
836 EFI_SIMPLE_NETWORK_PROTOCOL* Snp;
837 EFI_STATUS Status;
838
839 //
840 // Get our context back.
841 //
842 Status = gBS->OpenProtocol(
843 ControllerHandle,
844 &gEfiSimpleNetworkProtocolGuid,
845 ( VOID ** ) &Snp,
846 This->DriverBindingHandle,
847 ControllerHandle,
848 EFI_OPEN_PROTOCOL_GET_PROTOCOL
849 );
850 if ( EFI_ERROR( Status ) )
851 {
852 return( EFI_UNSUPPORTED );
853 }
854
855 Private = UNIX_SNP_PRIVATE_DATA_FROM_SNP_THIS( Snp );
856
857 Status = gBS->CloseProtocol(
858 ControllerHandle,
859 &gEfiUnixIoProtocolGuid,
860 This->DriverBindingHandle,
861 Private->DeviceHandle
862 );
863
864 Status = gBS->UninstallMultipleProtocolInterfaces(
865 Private->DeviceHandle,
866 &gEfiSimpleNetworkProtocolGuid,
867 &Private->Snp,
868 &gEfiDevicePathProtocolGuid,
869 Private->DevicePath,
870 NULL
871 );
872
873 FreePool( Private->InterfaceName );
874 FreePool( Private->DevicePath );
875 FreePool( Private );
876
877 return( EFI_SUCCESS );
878 }
879
880
881 /**
882 Changes the state of a network interface from "stopped" to "started".
883
884 @param This Protocol instance pointer.
885
886 @retval EFI_SUCCESS Always succeeds.
887
888 **/
889 EFI_STATUS
890 EFIAPI
891 UnixSnpStart(
892 IN EFI_SIMPLE_NETWORK_PROTOCOL* This
893 )
894 {
895 STATIC struct bpf_insn FilterInstructionTemplate[] =
896 {
897 // Load 4 bytes from the destination MAC address.
898 BPF_STMT( BPF_LD + BPF_W + BPF_ABS, OFFSET_OF( EthernetHeader, DstAddr[ 0 ] ) ),
899
900 // Compare to first 4 bytes of fake MAC address.
901 BPF_JUMP( BPF_JMP + BPF_JEQ + BPF_K, 0x12345678, 0, 3 ),
902
903 // Load remaining 2 bytes from the destination MAC address.
904 BPF_STMT( BPF_LD + BPF_H + BPF_ABS, OFFSET_OF( EthernetHeader, DstAddr[ 4 ] ) ),
905
906 // Compare to remaining 2 bytes of fake MAC address.
907 BPF_JUMP( BPF_JMP + BPF_JEQ + BPF_K, 0x9ABC, 5, 0 ),
908
909 // Load 4 bytes from the destination MAC address.
910 BPF_STMT( BPF_LD + BPF_W + BPF_ABS, OFFSET_OF( EthernetHeader, DstAddr[ 0 ] ) ),
911
912 // Compare to first 4 bytes of broadcast MAC address.
913 BPF_JUMP( BPF_JMP + BPF_JEQ + BPF_K, 0xFFFFFFFF, 0, 2 ),
914
915 // Load remaining 2 bytes from the destination MAC address.
916 BPF_STMT( BPF_LD + BPF_H + BPF_ABS, OFFSET_OF( EthernetHeader, DstAddr[ 4 ] ) ),
917
918 // Compare to remaining 2 bytes of broadcast MAC address.
919 BPF_JUMP( BPF_JMP + BPF_JEQ + BPF_K, 0xFFFF, 1, 0 ),
920
921 // Reject packet.
922 BPF_STMT( BPF_RET + BPF_K, 0 ),
923
924 // Receive entire packet.
925 BPF_STMT( BPF_RET + BPF_K, -1 )
926 };
927 struct ifreq BoundIf;
928 struct bpf_program BpfProgram;
929 struct bpf_insn* FilterProgram;
930 UNIX_SNP_PRIVATE_DATA* Private;
931 EFI_STATUS Status;
932 UINT32 Temp32;
933 INTN Fd;
934 INTN Result;
935 INTN Value;
936 UINT16 Temp16;
937
938 Private = UNIX_SNP_PRIVATE_DATA_FROM_SNP_THIS( This );
939
940 switch ( Private->Snp.Mode->State )
941 {
942 case EfiSimpleNetworkStopped:
943 break;
944
945 case EfiSimpleNetworkStarted:
946 case EfiSimpleNetworkInitialized:
947 return( EFI_ALREADY_STARTED );
948 break;
949
950 default:
951 return( EFI_DEVICE_ERROR );
952 break;
953 }
954
955 if ( Private->BpfFd == 0 )
956 {
957 Status = OpenBpfFileDescriptor( Private, &Fd );
958
959 if ( EFI_ERROR( Status ) )
960 {
961 goto ErrorExit;
962 }
963
964 Private->BpfFd = Fd;
965
966 //
967 // Associate our interface with this BPF file descriptor.
968 //
969 AsciiStrCpy( BoundIf.ifr_name, Private->InterfaceName );
970 Result = Private->UnixThunk->IoCtl( Private->BpfFd, BIOCSETIF, &BoundIf );
971
972 if ( Result < 0 )
973 {
974 goto DeviceErrorExit;
975 }
976
977 //
978 // Enable immediate mode and find out the buffer size.
979 //
980 Value = 1;
981 Result = Private->UnixThunk->IoCtl( Private->BpfFd, BIOCIMMEDIATE, &Value );
982
983 if ( Result < 0 )
984 {
985 goto DeviceErrorExit;
986 }
987
988 //
989 // Enable non-blocking I/O.
990 //
991
992 Value = Private->UnixThunk->Fcntl( Private->BpfFd, F_GETFL, 0 );
993
994 if ( Value == -1 )
995 {
996 goto DeviceErrorExit;
997 }
998
999 Value |= O_NONBLOCK;
1000
1001 Result = Private->UnixThunk->Fcntl( Private->BpfFd, F_SETFL, (void *) Value );
1002
1003 if ( Result == -1 )
1004 {
1005 goto DeviceErrorExit;
1006 }
1007
1008 //
1009 // Disable "header complete" flag. This means the supplied source MAC address is
1010 // what goes on the wire.
1011 //
1012 Value = 1;
1013 Result = Private->UnixThunk->IoCtl( Private->BpfFd, BIOCSHDRCMPLT, &Value );
1014
1015 if ( Result < 0 )
1016 {
1017 goto DeviceErrorExit;
1018 }
1019
1020 Result = Private->UnixThunk->IoCtl( Private->BpfFd, BIOCGBLEN, &Value );
1021
1022 if ( Result < 0 )
1023 {
1024 goto DeviceErrorExit;
1025 }
1026
1027 //
1028 // Allocate read buffer.
1029 //
1030 Private->ReadBufferSize = Value;
1031 Private->ReadBuffer = AllocateZeroPool( Private->ReadBufferSize );
1032 if ( Private->ReadBuffer == NULL )
1033 {
1034 Status = EFI_OUT_OF_RESOURCES;
1035 goto ErrorExit;
1036 }
1037
1038 Private->CurrentReadPointer = Private->EndReadPointer = Private->ReadBuffer;
1039
1040 //
1041 // Install our packet filter: successful reads should only produce broadcast or unitcast
1042 // packets directed to our fake MAC address.
1043 //
1044 FilterProgram = AllocateCopyPool( sizeof( FilterInstructionTemplate ), &FilterInstructionTemplate );
1045 if ( FilterProgram == NULL )
1046 {
1047 goto ErrorExit;
1048 }
1049
1050 //
1051 // Insert out fake MAC address into the filter. The data has to be host endian.
1052 //
1053 CopyMem( &Temp32, &Private->Mode.CurrentAddress.Addr[ 0 ], sizeof( UINT32 ) );
1054 FilterProgram[ 1 ].k = NTOHL( Temp32 );
1055 CopyMem( &Temp16, &Private->Mode.CurrentAddress.Addr[ 4 ], sizeof( UINT16 ) );
1056 FilterProgram[ 3 ].k = NTOHS( Temp16 );
1057
1058 BpfProgram.bf_len = sizeof( FilterInstructionTemplate ) / sizeof( struct bpf_insn );
1059 BpfProgram.bf_insns = FilterProgram;
1060
1061 Result = Private->UnixThunk->IoCtl( Private->BpfFd, BIOCSETF, &BpfProgram );
1062
1063 if ( Result < 0 )
1064 {
1065 goto DeviceErrorExit;
1066 }
1067
1068 FreePool( FilterProgram );
1069
1070 //
1071 // Enable promiscuous mode.
1072 //
1073
1074 Result = Private->UnixThunk->IoCtl( Private->BpfFd, BIOCPROMISC, 0 );
1075
1076 if ( Result < 0 )
1077 {
1078 goto DeviceErrorExit;
1079 }
1080
1081
1082 Private->Snp.Mode->State = EfiSimpleNetworkStarted;
1083 }
1084
1085 return( Status );
1086
1087 DeviceErrorExit:
1088 Status = EFI_DEVICE_ERROR;
1089 ErrorExit:
1090 if ( Private->ReadBuffer != NULL )
1091 {
1092 FreePool( Private->ReadBuffer );
1093 Private->ReadBuffer = NULL;
1094 }
1095 return( Status );
1096 }
1097
1098
1099 /**
1100 Changes the state of a network interface from "started" to "stopped".
1101
1102 @param This Protocol instance pointer.
1103
1104 @retval EFI_SUCCESS Always succeeds.
1105
1106 **/
1107 EFI_STATUS
1108 EFIAPI
1109 UnixSnpStop(
1110 IN EFI_SIMPLE_NETWORK_PROTOCOL* This
1111 )
1112 {
1113 UNIX_SNP_PRIVATE_DATA* Private = EFI_SUCCESS;
1114 EFI_STATUS Status;
1115
1116 Private = UNIX_SNP_PRIVATE_DATA_FROM_SNP_THIS( This );
1117
1118 switch ( Private->Snp.Mode->State )
1119 {
1120 case EfiSimpleNetworkStarted:
1121 break;
1122
1123 case EfiSimpleNetworkStopped:
1124 return( EFI_NOT_STARTED );
1125 break;
1126
1127 default:
1128 return( EFI_DEVICE_ERROR );
1129 break;
1130 }
1131
1132 if ( Private->BpfFd != 0 )
1133 {
1134 Private->UnixThunk->Close( Private->BpfFd );
1135 Private->BpfFd = 0;
1136 }
1137
1138 if ( Private->ReadBuffer != NULL )
1139 {
1140 FreePool( Private->ReadBuffer );
1141 Private->CurrentReadPointer = Private->EndReadPointer = Private->ReadBuffer = NULL;
1142 }
1143
1144 Private->Snp.Mode->State = EfiSimpleNetworkStopped;
1145
1146 return( Status );
1147 }
1148
1149
1150 /**
1151 Resets a network adapter and allocates the transmit and receive buffers
1152 required by the network interface; optionally, also requests allocation
1153 of additional transmit and receive buffers.
1154
1155 @param This Protocol instance pointer.
1156 @param ExtraRxBufferSize The size, in bytes, of the extra receive buffer space
1157 that the driver should allocate for the network interface.
1158 Some network interfaces will not be able to use the extra
1159 buffer, and the caller will not know if it is actually
1160 being used.
1161 @param ExtraTxBufferSize The size, in bytes, of the extra transmit buffer space
1162 that the driver should allocate for the network interface.
1163 Some network interfaces will not be able to use the extra
1164 buffer, and the caller will not know if it is actually
1165 being used.
1166
1167 @retval EFI_SUCCESS Always succeeds.
1168
1169 **/
1170 EFI_STATUS
1171 EFIAPI
1172 UnixSnpInitialize(
1173 IN EFI_SIMPLE_NETWORK_PROTOCOL* This,
1174 IN UINTN ExtraRxBufferSize OPTIONAL,
1175 IN UINTN ExtraTxBufferSize OPTIONAL
1176 )
1177 {
1178 UNIX_SNP_PRIVATE_DATA* Private = EFI_SUCCESS;
1179 EFI_STATUS Status;
1180
1181 Private = UNIX_SNP_PRIVATE_DATA_FROM_SNP_THIS( This );
1182
1183 switch ( Private->Snp.Mode->State )
1184 {
1185 case EfiSimpleNetworkStarted:
1186 break;
1187
1188 case EfiSimpleNetworkStopped:
1189 return( EFI_NOT_STARTED );
1190 break;
1191
1192 default:
1193 return( EFI_DEVICE_ERROR );
1194 break;
1195 }
1196
1197 #if 0
1198 Status = gBS->CreateEvent(
1199 EVT_NOTIFY_WAIT,
1200 TPL_NOTIFY,
1201 UnixSnpWaitForPacketNotify,
1202 Private,
1203 &Private->Snp.WaitForPacket
1204 );
1205 #endif
1206
1207 if ( !EFI_ERROR( Status ) )
1208 {
1209 Private->Mode.MCastFilterCount = 0;
1210 Private->Mode.ReceiveFilterSetting = 0;
1211 ZeroMem( Private->Mode.MCastFilter, sizeof( Private->Mode.MCastFilter ) );
1212
1213 Private->Snp.Mode->State = EfiSimpleNetworkInitialized;
1214 }
1215
1216 return( Status );
1217 }
1218
1219 /**
1220 Resets a network adapter and re-initializes it with the parameters that were
1221 provided in the previous call to Initialize().
1222
1223 @param This Protocol instance pointer.
1224 @param ExtendedVerification Indicates that the driver may perform a more
1225 exhaustive verification operation of the device
1226 during reset.
1227
1228 @retval EFI_SUCCESS Always succeeds.
1229
1230 **/
1231 EFI_STATUS
1232 EFIAPI
1233 UnixSnpReset(
1234 IN EFI_SIMPLE_NETWORK_PROTOCOL* This,
1235 IN BOOLEAN ExtendedVerification
1236 )
1237 {
1238 UNIX_SNP_PRIVATE_DATA* Private;
1239 EFI_STATUS Success = EFI_SUCCESS;
1240
1241 Private = UNIX_SNP_PRIVATE_DATA_FROM_SNP_THIS( This );
1242
1243 switch ( Private->Snp.Mode->State )
1244 {
1245 case EfiSimpleNetworkInitialized:
1246 break;
1247
1248 case EfiSimpleNetworkStopped:
1249 return( EFI_NOT_STARTED );
1250 break;
1251
1252 default:
1253 return( EFI_DEVICE_ERROR );
1254 break;
1255 }
1256
1257 return( Success );
1258 }
1259
1260 /**
1261 Resets a network adapter and leaves it in a state that is safe for
1262 another driver to initialize.
1263
1264 @param This Protocol instance pointer.
1265
1266 @retval EFI_SUCCESS Always succeeds.
1267
1268 **/
1269 EFI_STATUS
1270 EFIAPI
1271 UnixSnpShutdown(
1272 IN EFI_SIMPLE_NETWORK_PROTOCOL* This
1273 )
1274 {
1275 UNIX_SNP_PRIVATE_DATA* Private;
1276 EFI_STATUS Success = EFI_SUCCESS;
1277
1278 Private = UNIX_SNP_PRIVATE_DATA_FROM_SNP_THIS( This );
1279
1280 switch ( Private->Snp.Mode->State )
1281 {
1282 case EfiSimpleNetworkInitialized:
1283 break;
1284
1285 case EfiSimpleNetworkStopped:
1286 return( EFI_NOT_STARTED );
1287 break;
1288
1289 default:
1290 return( EFI_DEVICE_ERROR );
1291 break;
1292 }
1293
1294 Private->Snp.Mode->State = EfiSimpleNetworkStarted;
1295
1296 Private->Mode.ReceiveFilterSetting = 0;
1297 Private->Mode.MCastFilterCount = 0;
1298 ZeroMem( Private->Mode.MCastFilter, sizeof( Private->Mode.MCastFilter ) );
1299
1300 if ( Private->Snp.WaitForPacket != NULL )
1301 {
1302 gBS->CloseEvent( Private->Snp.WaitForPacket );
1303 Private->Snp.WaitForPacket = NULL;
1304 }
1305
1306 if ( Private->BpfFd != 0 )
1307 {
1308 Private->UnixThunk->Close( Private->BpfFd );
1309 Private->BpfFd = 0;
1310 }
1311
1312 if ( Private->ReadBuffer != NULL )
1313 {
1314 FreePool( Private->ReadBuffer );
1315 Private->CurrentReadPointer = Private->EndReadPointer = Private->ReadBuffer = NULL;
1316 }
1317
1318 return( Success );
1319 }
1320
1321 /**
1322 Manages the multicast receive filters of a network interface.
1323
1324 @param This Protocol instance pointer.
1325 @param EnableBits A bit mask of receive filters to enable on the network interface.
1326 @param DisableBits A bit mask of receive filters to disable on the network interface.
1327 @param ResetMcastFilter Set to TRUE to reset the contents of the multicast receive
1328 filters on the network interface to their default values.
1329 @param McastFilterCount Number of multicast HW MAC addresses in the new
1330 MCastFilter list. This value must be less than or equal to
1331 the MCastFilterCnt field of EFI_SIMPLE_NETWORK_MODE. This
1332 field is optional if ResetMCastFilter is TRUE.
1333 @param McastFilter A pointer to a list of new multicast receive filter HW MAC
1334 addresses. This list will replace any existing multicast
1335 HW MAC address list. This field is optional if
1336 ResetMCastFilter is TRUE.
1337
1338 @retval EFI_SUCCESS The multicast receive filter list was updated.
1339 @retval EFI_DEVICE_ERROR The command could not be sent to the network interface.
1340
1341 **/
1342 EFI_STATUS
1343 EFIAPI
1344 UnixSnpReceiveFilters(
1345 IN EFI_SIMPLE_NETWORK_PROTOCOL* This,
1346 IN UINT32 EnableBits,
1347 IN UINT32 DisableBits,
1348 IN BOOLEAN ResetMcastFilter,
1349 IN UINTN McastFilterCount OPTIONAL,
1350 IN EFI_MAC_ADDRESS* McastFilter OPTIONAL
1351 )
1352 {
1353 UNIX_SNP_PRIVATE_DATA* Private;
1354
1355 Private = UNIX_SNP_PRIVATE_DATA_FROM_SNP_THIS( This );
1356
1357 // ReturnValue = GlobalData->NtNetUtilityTable.SetReceiveFilter (
1358 // Instance->InterfaceInfo.InterfaceIndex,
1359 // EnableBits,
1360 // McastFilterCount,
1361 // McastFilter
1362 // );
1363
1364 // For now, just succeed...
1365 return( EFI_SUCCESS );
1366 }
1367
1368 /**
1369 Modifies or resets the current station address, if supported.
1370
1371 @param This Protocol instance pointer.
1372 @param Reset Flag used to reset the station address to the network interfaces
1373 permanent address.
1374 @param NewMacAddr New station address to be used for the network interface.
1375
1376 @retval EFI_UNSUPPORTED Not supported yet.
1377
1378 **/
1379 EFI_STATUS
1380 EFIAPI
1381 UnixSnpStationAddress(
1382 IN EFI_SIMPLE_NETWORK_PROTOCOL* This,
1383 IN BOOLEAN Reset,
1384 IN EFI_MAC_ADDRESS* NewMacAddr OPTIONAL
1385 )
1386 {
1387 return( EFI_UNSUPPORTED );
1388 }
1389
1390 /**
1391 Resets or collects the statistics on a network interface.
1392
1393 @param This Protocol instance pointer.
1394 @param Reset Set to TRUE to reset the statistics for the network interface.
1395 @param StatisticsSize On input the size, in bytes, of StatisticsTable. On
1396 output the size, in bytes, of the resulting table of
1397 statistics.
1398 @param StatisticsTable A pointer to the EFI_NETWORK_STATISTICS structure that
1399 contains the statistics.
1400
1401 @retval EFI_SUCCESS The statistics were collected from the network interface.
1402 @retval EFI_NOT_STARTED The network interface has not been started.
1403 @retval EFI_BUFFER_TOO_SMALL The Statistics buffer was too small. The current buffer
1404 size needed to hold the statistics is returned in
1405 StatisticsSize.
1406 @retval EFI_UNSUPPORTED Not supported yet.
1407
1408 **/
1409 EFI_STATUS
1410 EFIAPI
1411 UnixSnpStatistics(
1412 IN EFI_SIMPLE_NETWORK_PROTOCOL* This,
1413 IN BOOLEAN Reset,
1414 IN OUT UINTN* StatisticsSize OPTIONAL,
1415 OUT EFI_NETWORK_STATISTICS* StatisticsTable OPTIONAL
1416 )
1417 {
1418 return( EFI_UNSUPPORTED );
1419 }
1420
1421 /**
1422 Converts a multicast IP address to a multicast HW MAC address.
1423
1424 @param This Protocol instance pointer.
1425 @param Ipv6 Set to TRUE if the multicast IP address is IPv6 [RFC 2460]. Set
1426 to FALSE if the multicast IP address is IPv4 [RFC 791].
1427 @param Ip The multicast IP address that is to be converted to a multicast
1428 HW MAC address.
1429 @param Mac The multicast HW MAC address that is to be generated from IP.
1430
1431 @retval EFI_SUCCESS The multicast IP address was mapped to the multicast
1432 HW MAC address.
1433 @retval EFI_NOT_STARTED The network interface has not been started.
1434 @retval EFI_BUFFER_TOO_SMALL The Statistics buffer was too small. The current buffer
1435 size needed to hold the statistics is returned in
1436 StatisticsSize.
1437 @retval EFI_UNSUPPORTED Not supported yet.
1438
1439 **/
1440 EFI_STATUS
1441 EFIAPI
1442 UnixSnpMcastIptoMac(
1443 IN EFI_SIMPLE_NETWORK_PROTOCOL* This,
1444 IN BOOLEAN Ipv6,
1445 IN EFI_IP_ADDRESS* Ip,
1446 OUT EFI_MAC_ADDRESS* Mac
1447 )
1448 {
1449 return( EFI_UNSUPPORTED );
1450 }
1451
1452
1453 /**
1454 Performs read and write operations on the NVRAM device attached to a
1455 network interface.
1456
1457 @param This Protocol instance pointer.
1458 @param ReadOrWrite TRUE for read operations, FALSE for write operations.
1459 @param Offset Byte offset in the NVRAM device at which to start the read or
1460 write operation. This must be a multiple of NvRamAccessSize and
1461 less than NvRamSize.
1462 @param BufferSize The number of bytes to read or write from the NVRAM device.
1463 This must also be a multiple of NvramAccessSize.
1464 @param Buffer A pointer to the data buffer.
1465
1466 @retval EFI_UNSUPPORTED Not supported yet.
1467
1468 **/
1469 EFI_STATUS
1470 EFIAPI
1471 UnixSnpNvdata(
1472 IN EFI_SIMPLE_NETWORK_PROTOCOL* This,
1473 IN BOOLEAN ReadOrWrite,
1474 IN UINTN Offset,
1475 IN UINTN BufferSize,
1476 IN OUT VOID* Buffer
1477 )
1478 {
1479 return( EFI_UNSUPPORTED );
1480 }
1481
1482
1483 /**
1484 Reads the current interrupt status and recycled transmit buffer status from
1485 a network interface.
1486
1487 @param This Protocol instance pointer.
1488 @param InterruptStatus A pointer to the bit mask of the currently active interrupts
1489 If this is NULL, the interrupt status will not be read from
1490 the device. If this is not NULL, the interrupt status will
1491 be read from the device. When the interrupt status is read,
1492 it will also be cleared. Clearing the transmit interrupt
1493 does not empty the recycled transmit buffer array.
1494 @param TxBuffer Recycled transmit buffer address. The network interface will
1495 not transmit if its internal recycled transmit buffer array
1496 is full. Reading the transmit buffer does not clear the
1497 transmit interrupt. If this is NULL, then the transmit buffer
1498 status will not be read. If there are no transmit buffers to
1499 recycle and TxBuf is not NULL, * TxBuf will be set to NULL.
1500
1501 @retval EFI_SUCCESS Always succeeds.
1502
1503 **/
1504 EFI_STATUS
1505 EFIAPI
1506 UnixSnpGetStatus(
1507 IN EFI_SIMPLE_NETWORK_PROTOCOL* This,
1508 OUT UINT32* InterruptStatus,
1509 OUT VOID** TxBuffer
1510 )
1511 {
1512 if ( TxBuffer != NULL )
1513 {
1514 *( ( UINT8 ** ) TxBuffer ) = ( UINT8 * ) 1;
1515 }
1516
1517 if ( InterruptStatus != NULL )
1518 {
1519 *InterruptStatus = EFI_SIMPLE_NETWORK_TRANSMIT_INTERRUPT;
1520 }
1521
1522 return( EFI_SUCCESS );
1523 }
1524
1525
1526 /**
1527 Places a packet in the transmit queue of a network interface.
1528
1529 @param This Protocol instance pointer.
1530 @param HeaderSize The size, in bytes, of the media header to be filled in by
1531 the Transmit() function. If HeaderSize is non-zero, then it
1532 must be equal to This->Mode->MediaHeaderSize and the DestAddr
1533 and Protocol parameters must not be NULL.
1534 @param BufferSize The size, in bytes, of the entire packet (media header and
1535 data) to be transmitted through the network interface.
1536 @param Buffer A pointer to the packet (media header followed by data) to be
1537 transmitted. This parameter cannot be NULL. If HeaderSize is zero,
1538 then the media header in Buffer must already be filled in by the
1539 caller. If HeaderSize is non-zero, then the media header will be
1540 filled in by the Transmit() function.
1541 @param SrcAddr The source HW MAC address. If HeaderSize is zero, then this parameter
1542 is ignored. If HeaderSize is non-zero and SrcAddr is NULL, then
1543 This->Mode->CurrentAddress is used for the source HW MAC address.
1544 @param DestAddr The destination HW MAC address. If HeaderSize is zero, then this
1545 parameter is ignored.
1546 @param Protocol The type of header to build. If HeaderSize is zero, then this
1547 parameter is ignored. See RFC 1700, section "Ether Types", for
1548 examples.
1549
1550 @retval EFI_SUCCESS The packet was placed on the transmit queue.
1551 @retval EFI_DEVICE_ERROR The command could not be sent to the network interface.
1552 @retval EFI_INVALID_PARAMETER One or more of the parameters has an unsupported value.
1553 @retval EFI_NOT_STARTED The network interface has not been started.
1554
1555 **/
1556 EFI_STATUS
1557 EFIAPI
1558 UnixSnpTransmit(
1559 IN EFI_SIMPLE_NETWORK_PROTOCOL* This,
1560 IN UINTN HeaderSize,
1561 IN UINTN BufferSize,
1562 IN VOID* Buffer,
1563 IN EFI_MAC_ADDRESS* SrcAddr OPTIONAL,
1564 IN EFI_MAC_ADDRESS* DestAddr OPTIONAL,
1565 IN UINT16* Protocol OPTIONAL
1566 )
1567 {
1568 UNIX_SNP_PRIVATE_DATA* Private;
1569 EthernetHeader* EnetHeader;
1570 INTN Result;
1571
1572 Private = UNIX_SNP_PRIVATE_DATA_FROM_SNP_THIS( This );
1573
1574 if ( This->Mode->State < EfiSimpleNetworkStarted )
1575 {
1576 return( EFI_NOT_STARTED );
1577 }
1578
1579 if ( HeaderSize != 0 )
1580 {
1581 if ( ( DestAddr == NULL ) || ( Protocol == NULL ) || ( HeaderSize != This->Mode->MediaHeaderSize ) )
1582 {
1583 return( EFI_INVALID_PARAMETER );
1584 }
1585
1586 if ( SrcAddr == NULL )
1587 {
1588 SrcAddr = &This->Mode->CurrentAddress;
1589 }
1590
1591 EnetHeader = ( EthernetHeader * ) Buffer;
1592
1593 CopyMem( EnetHeader->DstAddr, DestAddr, NET_ETHER_ADDR_LEN );
1594 CopyMem( EnetHeader->SrcAddr, SrcAddr, NET_ETHER_ADDR_LEN );
1595
1596 EnetHeader->Type = HTONS( *Protocol );
1597 }
1598
1599 Result = Private->UnixThunk->Write( Private->BpfFd, Buffer, BufferSize );
1600
1601 if ( Result < 0 )
1602 {
1603 return( EFI_DEVICE_ERROR );
1604 }
1605 else
1606 {
1607 return( EFI_SUCCESS );
1608 }
1609 }
1610
1611 /**
1612 Receives a packet from a network interface.
1613
1614 @param This Protocol instance pointer.
1615 @param HeaderSize The size, in bytes, of the media header received on the network
1616 interface. If this parameter is NULL, then the media header size
1617 will not be returned.
1618 @param BuffSize On entry, the size, in bytes, of Buffer. On exit, the size, in
1619 bytes, of the packet that was received on the network interface.
1620 @param Buffer A pointer to the data buffer to receive both the media header and
1621 the data.
1622 @param SourceAddr The source HW MAC address. If this parameter is NULL, the
1623 HW MAC source address will not be extracted from the media
1624 header.
1625 @param DestinationAddr The destination HW MAC address. If this parameter is NULL,
1626 the HW MAC destination address will not be extracted from the
1627 media header.
1628 @param Protocol The media header type. If this parameter is NULL, then the
1629 protocol will not be extracted from the media header. See
1630 RFC 1700 section "Ether Types" for examples.
1631
1632 @retval EFI_SUCCESS The received data was stored in Buffer, and BufferSize has
1633 been updated to the number of bytes received.
1634 @retval EFI_NOT_READY The network interface is too busy to accept this transmit
1635 request.
1636 @retval EFI_NOT_STARTED The network interface has not been started.
1637 @retval EFI_BUFFER_TOO_SMALL The BufferSize parameter is too small.
1638 @retval EFI_DEVICE_ERROR The command could not be sent to the network interface.
1639
1640 **/
1641 EFI_STATUS
1642 EFIAPI
1643 UnixSnpReceive(
1644 IN EFI_SIMPLE_NETWORK_PROTOCOL* This,
1645 OUT UINTN* HeaderSize OPTIONAL,
1646 IN OUT UINTN* BuffSize,
1647 OUT VOID* Buffer,
1648 OUT EFI_MAC_ADDRESS* SourceAddr OPTIONAL,
1649 OUT EFI_MAC_ADDRESS* DestinationAddr OPTIONAL,
1650 OUT UINT16* Protocol OPTIONAL
1651 )
1652 {
1653 UNIX_SNP_PRIVATE_DATA* Private;
1654 struct bpf_hdr* BpfHeader;
1655 EthernetHeader* EnetHeader;
1656 EFI_STATUS Status = EFI_SUCCESS;
1657 INTN Result;
1658
1659 if ( This->Mode->State < EfiSimpleNetworkStarted )
1660 {
1661 return( EFI_NOT_STARTED );
1662 }
1663
1664 Private = UNIX_SNP_PRIVATE_DATA_FROM_SNP_THIS( This );
1665
1666 //
1667 // Do we have any remaining packets from the previous read?
1668 //
1669 if ( Private->CurrentReadPointer >= Private->EndReadPointer )
1670 {
1671 Result = Private->UnixThunk->Read( Private->BpfFd, Private->ReadBuffer, Private->ReadBufferSize );
1672
1673 if ( Result < 0 )
1674 {
1675 Result = Private->UnixThunk->GetErrno();
1676
1677 //
1678 // EAGAIN means that there's no I/O outstanding against this file descriptor.
1679 //
1680 if ( Result == EAGAIN )
1681 {
1682 return( EFI_NOT_READY );
1683 }
1684 else
1685 {
1686 return( EFI_DEVICE_ERROR );
1687 }
1688 }
1689
1690 if ( Result == 0 )
1691 {
1692 return( EFI_NOT_READY );
1693 }
1694
1695 Private->CurrentReadPointer = Private->ReadBuffer;
1696 Private->EndReadPointer = Private->CurrentReadPointer + Result;
1697 }
1698
1699 BpfHeader = Private->CurrentReadPointer;
1700 EnetHeader = Private->CurrentReadPointer + BpfHeader->bh_hdrlen;
1701
1702 if ( BpfHeader->bh_caplen > *BuffSize )
1703 {
1704 *BuffSize = BpfHeader->bh_caplen;
1705 return( EFI_BUFFER_TOO_SMALL );
1706 }
1707
1708 CopyMem( Buffer, EnetHeader, BpfHeader->bh_caplen );
1709 *BuffSize = BpfHeader->bh_caplen;
1710
1711 if ( HeaderSize != NULL )
1712 {
1713 *HeaderSize = sizeof( EthernetHeader );
1714 }
1715
1716 if ( DestinationAddr != NULL )
1717 {
1718 ZeroMem( DestinationAddr, sizeof( EFI_MAC_ADDRESS ) );
1719 CopyMem( DestinationAddr, EnetHeader->DstAddr, NET_ETHER_ADDR_LEN );
1720 }
1721
1722 if ( SourceAddr != NULL )
1723 {
1724 ZeroMem( SourceAddr, sizeof( EFI_MAC_ADDRESS ) );
1725 CopyMem( SourceAddr, EnetHeader->SrcAddr, NET_ETHER_ADDR_LEN );
1726 }
1727
1728 if ( Protocol != NULL )
1729 {
1730 *Protocol = NTOHS( EnetHeader->Type );
1731 }
1732
1733 Private->CurrentReadPointer += BPF_WORDALIGN( BpfHeader->bh_hdrlen + BpfHeader->bh_caplen );
1734
1735 return( Status );
1736 }
1737
1738
1739 VOID
1740 EFIAPI
1741 UnixSnpWaitForPacketNotify(
1742 IN EFI_EVENT Event,
1743 IN VOID* Context
1744 )
1745 {
1746 UNIX_SNP_PRIVATE_DATA* Private;
1747
1748 Private = UNIX_SNP_PRIVATE_DATA_FROM_SNP_THIS( Context );
1749
1750 if ( Private->Snp.Mode->State < EfiSimpleNetworkStarted )
1751 {
1752 return;
1753 }
1754 }
1755
1756
1757 /**
1758 This is the declaration of an EFI image entry point. This entry point is
1759 the same for UEFI Applications, UEFI OS Loaders, and UEFI Drivers including
1760 both device drivers and bus drivers.
1761
1762 @param ImageHandle The firmware allocated handle for the UEFI image.
1763 @param SystemTable A pointer to the EFI System Table.
1764
1765 @retval EFI_SUCCESS The operation completed successfully.
1766 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
1767
1768 **/
1769 EFI_STATUS
1770 InitializeUnixSnpDriver(
1771 IN EFI_HANDLE ImageHandle,
1772 IN EFI_SYSTEM_TABLE* SystemTable
1773 )
1774 {
1775 EFI_STATUS Status;
1776
1777 //
1778 // Install the Driver Protocols
1779 //
1780
1781 Status = EfiLibInstallDriverBindingComponentName2(
1782 ImageHandle,
1783 SystemTable,
1784 &gUnixSnpDriverBinding,
1785 ImageHandle,
1786 &gUnixSnpDriverComponentName,
1787 &gUnixSnpDriverComponentName2
1788 );
1789
1790 return( Status );
1791 }