]> git.proxmox.com Git - mirror_edk2.git/blob - InOsEmuPkg/EmuSnpDxe/EmuSnpDxe.c
a83fde8c9cf3e1358646dc8cd07cc04068c58d04
[mirror_edk2.git] / InOsEmuPkg / EmuSnpDxe / EmuSnpDxe.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 EmuSnp.c
16
17 Abstract:
18
19 -**/
20
21 #include "EmuSnpDxe.h"
22
23
24
25 EFI_SIMPLE_NETWORK_PROTOCOL gEmuSnpTemplate = {
26 EFI_SIMPLE_NETWORK_PROTOCOL_REVISION,
27 EmuSnpStart,
28 EmuSnpStop,
29 EmuSnpInitialize,
30 EmuSnpReset,
31 EmuSnpShutdown,
32 EmuSnpReceiveFilters,
33 EmuSnpStationAddress,
34 EmuSnpStatistics,
35 EmuSnpMcastIptoMac,
36 EmuSnpNvdata,
37 EmuSnpGetStatus,
38 EmuSnpTransmit,
39 EmuSnpReceive,
40 NULL, // WaitForPacket
41 NULL // Mode
42 };
43
44
45
46
47 /**
48 Changes the state of a network interface from "stopped" to "started".
49
50 @param This Protocol instance pointer.
51
52 @retval EFI_SUCCESS Always succeeds.
53
54 **/
55 EFI_STATUS
56 EFIAPI
57 EmuSnpStart(
58 IN EFI_SIMPLE_NETWORK_PROTOCOL *This
59 )
60 {
61 EFI_STATUS Status;
62 EMU_SNP_PRIVATE_DATA *Private;
63
64 Private = EMU_SNP_PRIVATE_DATA_FROM_SNP_THIS (This);
65
66 Status = Private->Io->Start (Private->Io);
67 return Status;
68 }
69
70
71 /**
72 Changes the state of a network interface from "started" to "stopped".
73
74 @param This Protocol instance pointer.
75
76 @retval EFI_SUCCESS Always succeeds.
77
78 **/
79 EFI_STATUS
80 EFIAPI
81 EmuSnpStop (
82 IN EFI_SIMPLE_NETWORK_PROTOCOL *This
83 )
84 {
85 EFI_STATUS Status;
86 EMU_SNP_PRIVATE_DATA *Private;
87
88 Private = EMU_SNP_PRIVATE_DATA_FROM_SNP_THIS (This);
89
90 Status = Private->Io->Stop (Private->Io);
91 return Status;
92 }
93
94
95 /**
96 Resets a network adapter and allocates the transmit and receive buffers
97 required by the network interface; optionally, also requests allocation
98 of additional transmit and receive buffers.
99
100 @param This Protocol instance pointer.
101 @param ExtraRxBufferSize The size, in bytes, of the extra receive buffer space
102 that the driver should allocate for the network interface.
103 Some network interfaces will not be able to use the extra
104 buffer, and the caller will not know if it is actually
105 being used.
106 @param ExtraTxBufferSize The size, in bytes, of the extra transmit buffer space
107 that the driver should allocate for the network interface.
108 Some network interfaces will not be able to use the extra
109 buffer, and the caller will not know if it is actually
110 being used.
111
112 @retval EFI_SUCCESS Always succeeds.
113
114 **/
115 EFI_STATUS
116 EFIAPI
117 EmuSnpInitialize (
118 IN EFI_SIMPLE_NETWORK_PROTOCOL *This,
119 IN UINTN ExtraRxBufferSize OPTIONAL,
120 IN UINTN ExtraTxBufferSize OPTIONAL
121 )
122 {
123 EFI_STATUS Status;
124 EMU_SNP_PRIVATE_DATA *Private;
125
126 Private = EMU_SNP_PRIVATE_DATA_FROM_SNP_THIS (This);
127
128 Status = Private->Io->Initialize (Private->Io, ExtraRxBufferSize, ExtraTxBufferSize);
129 return Status;
130 }
131
132 /**
133 Resets a network adapter and re-initializes it with the parameters that were
134 provided in the previous call to Initialize().
135
136 @param This Protocol instance pointer.
137 @param ExtendedVerification Indicates that the driver may perform a more
138 exhaustive verification operation of the device
139 during reset.
140
141 @retval EFI_SUCCESS Always succeeds.
142
143 **/
144 EFI_STATUS
145 EFIAPI
146 EmuSnpReset (
147 IN EFI_SIMPLE_NETWORK_PROTOCOL *This,
148 IN BOOLEAN ExtendedVerification
149 )
150 {
151 EFI_STATUS Status;
152 EMU_SNP_PRIVATE_DATA *Private;
153
154 Private = EMU_SNP_PRIVATE_DATA_FROM_SNP_THIS (This);
155
156 Status = Private->Io->Reset (Private->Io, ExtendedVerification);
157 return Status;
158 }
159
160 /**
161 Resets a network adapter and leaves it in a state that is safe for
162 another driver to initialize.
163
164 @param This Protocol instance pointer.
165
166 @retval EFI_SUCCESS Always succeeds.
167
168 **/
169 EFI_STATUS
170 EFIAPI
171 EmuSnpShutdown (
172 IN EFI_SIMPLE_NETWORK_PROTOCOL *This
173 )
174 {
175 EFI_STATUS Status;
176 EMU_SNP_PRIVATE_DATA *Private;
177
178 Private = EMU_SNP_PRIVATE_DATA_FROM_SNP_THIS (This);
179
180 Status = Private->Io->Shutdown (Private->Io);
181 return Status;
182 }
183
184 /**
185 Manages the multicast receive filters of a network interface.
186
187 @param This Protocol instance pointer.
188 @param EnableBits A bit mask of receive filters to enable on the network interface.
189 @param DisableBits A bit mask of receive filters to disable on the network interface.
190 @param ResetMcastFilter Set to TRUE to reset the contents of the multicast receive
191 filters on the network interface to their default values.
192 @param McastFilterCount Number of multicast HW MAC addresses in the new
193 MCastFilter list. This value must be less than or equal to
194 the MCastFilterCnt field of EFI_SIMPLE_NETWORK_MODE. This
195 field is optional if ResetMCastFilter is TRUE.
196 @param McastFilter A pointer to a list of new multicast receive filter HW MAC
197 addresses. This list will replace any existing multicast
198 HW MAC address list. This field is optional if
199 ResetMCastFilter is TRUE.
200
201 @retval EFI_SUCCESS The multicast receive filter list was updated.
202 @retval EFI_DEVICE_ERROR The command could not be sent to the network interface.
203
204 **/
205 EFI_STATUS
206 EFIAPI
207 EmuSnpReceiveFilters (
208 IN EFI_SIMPLE_NETWORK_PROTOCOL *This,
209 IN UINT32 EnableBits,
210 IN UINT32 DisableBits,
211 IN BOOLEAN ResetMcastFilter,
212 IN UINTN McastFilterCount OPTIONAL,
213 IN EFI_MAC_ADDRESS *McastFilter OPTIONAL
214 )
215 {
216 EFI_STATUS Status;
217 EMU_SNP_PRIVATE_DATA *Private;
218
219 Private = EMU_SNP_PRIVATE_DATA_FROM_SNP_THIS (This);
220
221 Status = Private->Io->ReceiveFilters (
222 Private->Io,
223 EnableBits,
224 DisableBits,
225 ResetMcastFilter,
226 McastFilterCount,
227 McastFilter
228 );
229 return Status;
230 }
231
232 /**
233 Modifies or resets the current station address, if supported.
234
235 @param This Protocol instance pointer.
236 @param Reset Flag used to reset the station address to the network interfaces
237 permanent address.
238 @param NewMacAddr New station address to be used for the network interface.
239
240 @retval EFI_UNSUPPORTED Not supported yet.
241
242 **/
243 EFI_STATUS
244 EFIAPI
245 EmuSnpStationAddress (
246 IN EFI_SIMPLE_NETWORK_PROTOCOL *This,
247 IN BOOLEAN Reset,
248 IN EFI_MAC_ADDRESS *NewMacAddr OPTIONAL
249 )
250 {
251 EFI_STATUS Status;
252 EMU_SNP_PRIVATE_DATA *Private;
253
254 Private = EMU_SNP_PRIVATE_DATA_FROM_SNP_THIS (This);
255
256 Status = Private->Io->StationAddress (Private->Io, Reset, NewMacAddr);
257 return Status;
258 }
259
260 /**
261 Resets or collects the statistics on a network interface.
262
263 @param This Protocol instance pointer.
264 @param Reset Set to TRUE to reset the statistics for the network interface.
265 @param StatisticsSize On input the size, in bytes, of StatisticsTable. On
266 output the size, in bytes, of the resulting table of
267 statistics.
268 @param StatisticsTable A pointer to the EFI_NETWORK_STATISTICS structure that
269 contains the statistics.
270
271 @retval EFI_SUCCESS The statistics were collected from the network interface.
272 @retval EFI_NOT_STARTED The network interface has not been started.
273 @retval EFI_BUFFER_TOO_SMALL The Statistics buffer was too small. The current buffer
274 size needed to hold the statistics is returned in
275 StatisticsSize.
276 @retval EFI_UNSUPPORTED Not supported yet.
277
278 **/
279 EFI_STATUS
280 EFIAPI
281 EmuSnpStatistics (
282 IN EFI_SIMPLE_NETWORK_PROTOCOL *This,
283 IN BOOLEAN Reset,
284 IN OUT UINTN *StatisticsSize OPTIONAL,
285 OUT EFI_NETWORK_STATISTICS *StatisticsTable OPTIONAL
286 )
287 {
288 EFI_STATUS Status;
289 EMU_SNP_PRIVATE_DATA *Private;
290
291 Private = EMU_SNP_PRIVATE_DATA_FROM_SNP_THIS (This);
292
293 Status = Private->Io->Statistics (Private->Io, Reset, StatisticsSize, StatisticsTable);
294 return Status;
295 }
296
297 /**
298 Converts a multicast IP address to a multicast HW MAC address.
299
300 @param This Protocol instance pointer.
301 @param Ipv6 Set to TRUE if the multicast IP address is IPv6 [RFC 2460]. Set
302 to FALSE if the multicast IP address is IPv4 [RFC 791].
303 @param Ip The multicast IP address that is to be converted to a multicast
304 HW MAC address.
305 @param Mac The multicast HW MAC address that is to be generated from IP.
306
307 @retval EFI_SUCCESS The multicast IP address was mapped to the multicast
308 HW MAC address.
309 @retval EFI_NOT_STARTED The network interface has not been started.
310 @retval EFI_BUFFER_TOO_SMALL The Statistics buffer was too small. The current buffer
311 size needed to hold the statistics is returned in
312 StatisticsSize.
313 @retval EFI_UNSUPPORTED Not supported yet.
314
315 **/
316 EFI_STATUS
317 EFIAPI
318 EmuSnpMcastIptoMac (
319 IN EFI_SIMPLE_NETWORK_PROTOCOL *This,
320 IN BOOLEAN Ipv6,
321 IN EFI_IP_ADDRESS *Ip,
322 OUT EFI_MAC_ADDRESS *Mac
323 )
324 {
325 EFI_STATUS Status;
326 EMU_SNP_PRIVATE_DATA *Private;
327
328 Private = EMU_SNP_PRIVATE_DATA_FROM_SNP_THIS (This);
329
330 Status = Private->Io->MCastIpToMac (Private->Io, Ipv6, Ip, Mac);
331 return Status;
332 }
333
334
335 /**
336 Performs read and write operations on the NVRAM device attached to a
337 network interface.
338
339 @param This Protocol instance pointer.
340 @param ReadOrWrite TRUE for read operations, FALSE for write operations.
341 @param Offset Byte offset in the NVRAM device at which to start the read or
342 write operation. This must be a multiple of NvRamAccessSize and
343 less than NvRamSize.
344 @param BufferSize The number of bytes to read or write from the NVRAM device.
345 This must also be a multiple of NvramAccessSize.
346 @param Buffer A pointer to the data buffer.
347
348 @retval EFI_UNSUPPORTED Not supported yet.
349
350 **/
351 EFI_STATUS
352 EFIAPI
353 EmuSnpNvdata (
354 IN EFI_SIMPLE_NETWORK_PROTOCOL *This,
355 IN BOOLEAN ReadOrWrite,
356 IN UINTN Offset,
357 IN UINTN BufferSize,
358 IN OUT VOID *Buffer
359 )
360 {
361 EFI_STATUS Status;
362 EMU_SNP_PRIVATE_DATA *Private;
363
364 Private = EMU_SNP_PRIVATE_DATA_FROM_SNP_THIS (This);
365
366 Status = Private->Io->NvData (Private->Io, ReadOrWrite, Offset, BufferSize, Buffer);
367 return Status;
368 }
369
370
371 /**
372 Reads the current interrupt status and recycled transmit buffer status from
373 a network interface.
374
375 @param This Protocol instance pointer.
376 @param InterruptStatus A pointer to the bit mask of the currently active interrupts
377 If this is NULL, the interrupt status will not be read from
378 the device. If this is not NULL, the interrupt status will
379 be read from the device. When the interrupt status is read,
380 it will also be cleared. Clearing the transmit interrupt
381 does not empty the recycled transmit buffer array.
382 @param TxBuffer Recycled transmit buffer address. The network interface will
383 not transmit if its internal recycled transmit buffer array
384 is full. Reading the transmit buffer does not clear the
385 transmit interrupt. If this is NULL, then the transmit buffer
386 status will not be read. If there are no transmit buffers to
387 recycle and TxBuf is not NULL, * TxBuf will be set to NULL.
388
389 @retval EFI_SUCCESS Always succeeds.
390
391 **/
392 EFI_STATUS
393 EFIAPI
394 EmuSnpGetStatus (
395 IN EFI_SIMPLE_NETWORK_PROTOCOL *This,
396 OUT UINT32 *InterruptStatus,
397 OUT VOID **TxBuffer
398 )
399 {
400 EFI_STATUS Status;
401 EMU_SNP_PRIVATE_DATA *Private;
402
403 Private = EMU_SNP_PRIVATE_DATA_FROM_SNP_THIS (This);
404
405 Status = Private->Io->GetStatus (Private->Io, InterruptStatus, TxBuffer);
406 return Status;
407 }
408
409
410 /**
411 Places a packet in the transmit queue of a network interface.
412
413 @param This Protocol instance pointer.
414 @param HeaderSize The size, in bytes, of the media header to be filled in by
415 the Transmit() function. If HeaderSize is non-zero, then it
416 must be equal to This->Mode->MediaHeaderSize and the DestAddr
417 and Protocol parameters must not be NULL.
418 @param BufferSize The size, in bytes, of the entire packet (media header and
419 data) to be transmitted through the network interface.
420 @param Buffer A pointer to the packet (media header followed by data) to be
421 transmitted. This parameter cannot be NULL. If HeaderSize is zero,
422 then the media header in Buffer must already be filled in by the
423 caller. If HeaderSize is non-zero, then the media header will be
424 filled in by the Transmit() function.
425 @param SrcAddr The source HW MAC address. If HeaderSize is zero, then this parameter
426 is ignored. If HeaderSize is non-zero and SrcAddr is NULL, then
427 This->Mode->CurrentAddress is used for the source HW MAC address.
428 @param DestAddr The destination HW MAC address. If HeaderSize is zero, then this
429 parameter is ignored.
430 @param Protocol The type of header to build. If HeaderSize is zero, then this
431 parameter is ignored. See RFC 1700, section "Ether Types", for
432 examples.
433
434 @retval EFI_SUCCESS The packet was placed on the transmit queue.
435 @retval EFI_DEVICE_ERROR The command could not be sent to the network interface.
436 @retval EFI_INVALID_PARAMETER One or more of the parameters has an unsupported value.
437 @retval EFI_NOT_STARTED The network interface has not been started.
438
439 **/
440 EFI_STATUS
441 EFIAPI
442 EmuSnpTransmit (
443 IN EFI_SIMPLE_NETWORK_PROTOCOL *This,
444 IN UINTN HeaderSize,
445 IN UINTN BufferSize,
446 IN VOID* Buffer,
447 IN EFI_MAC_ADDRESS *SrcAddr OPTIONAL,
448 IN EFI_MAC_ADDRESS *DestAddr OPTIONAL,
449 IN UINT16 *Protocol OPTIONAL
450 )
451 {
452 EFI_STATUS Status;
453 EMU_SNP_PRIVATE_DATA *Private;
454
455 Private = EMU_SNP_PRIVATE_DATA_FROM_SNP_THIS (This);
456
457 Status = Private->Io->Transmit (
458 Private->Io,
459 HeaderSize,
460 BufferSize,
461 Buffer,
462 SrcAddr,
463 DestAddr,
464 Protocol
465 );
466 return Status;
467 }
468
469 /**
470 Receives a packet from a network interface.
471
472 @param This Protocol instance pointer.
473 @param HeaderSize The size, in bytes, of the media header received on the network
474 interface. If this parameter is NULL, then the media header size
475 will not be returned.
476 @param BuffSize On entry, the size, in bytes, of Buffer. On exit, the size, in
477 bytes, of the packet that was received on the network interface.
478 @param Buffer A pointer to the data buffer to receive both the media header and
479 the data.
480 @param SourceAddr The source HW MAC address. If this parameter is NULL, the
481 HW MAC source address will not be extracted from the media
482 header.
483 @param DestinationAddr The destination HW MAC address. If this parameter is NULL,
484 the HW MAC destination address will not be extracted from the
485 media header.
486 @param Protocol The media header type. If this parameter is NULL, then the
487 protocol will not be extracted from the media header. See
488 RFC 1700 section "Ether Types" for examples.
489
490 @retval EFI_SUCCESS The received data was stored in Buffer, and BufferSize has
491 been updated to the number of bytes received.
492 @retval EFI_NOT_READY The network interface is too busy to accept this transmit
493 request.
494 @retval EFI_NOT_STARTED The network interface has not been started.
495 @retval EFI_BUFFER_TOO_SMALL The BufferSize parameter is too small.
496 @retval EFI_DEVICE_ERROR The command could not be sent to the network interface.
497
498 **/
499 EFI_STATUS
500 EFIAPI
501 EmuSnpReceive (
502 IN EFI_SIMPLE_NETWORK_PROTOCOL *This,
503 OUT UINTN *HeaderSize OPTIONAL,
504 IN OUT UINTN *BuffSize,
505 OUT VOID *Buffer,
506 OUT EFI_MAC_ADDRESS *SourceAddr OPTIONAL,
507 OUT EFI_MAC_ADDRESS *DestinationAddr OPTIONAL,
508 OUT UINT16 *Protocol OPTIONAL
509 )
510 {
511 EFI_STATUS Status;
512 EMU_SNP_PRIVATE_DATA *Private;
513
514 Private = EMU_SNP_PRIVATE_DATA_FROM_SNP_THIS (This);
515
516 Status = Private->Io->Receive (
517 Private->Io,
518 HeaderSize,
519 BuffSize,
520 Buffer,
521 SourceAddr,
522 DestinationAddr,
523 Protocol
524 );
525 return Status;
526 }
527
528
529
530 /**
531 Test to see if this driver supports ControllerHandle. This service
532 is called by the EFI boot service ConnectController(). In
533 order to make drivers as small as possible, there are a few calling
534 restrictions for this service. ConnectController() must
535 follow these calling restrictions. If any other agent wishes to call
536 Supported() it must also follow these calling restrictions.
537
538 @param This Protocol instance pointer.
539 @param ControllerHandle Handle of device to test
540 @param RemainingDevicePath Optional parameter use to pick a specific child
541 device to start.
542
543 @retval EFI_SUCCESS This driver supports this device
544 @retval EFI_UNSUPPORTED This driver does not support this device
545
546 **/
547 EFI_STATUS
548 EFIAPI
549 EmuSnpDriverBindingSupported (
550 IN EFI_DRIVER_BINDING_PROTOCOL *This,
551 IN EFI_HANDLE ControllerHandle,
552 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL
553 )
554 {
555 EFI_STATUS Status;
556 EMU_IO_THUNK_PROTOCOL *EmuIoThunk;
557 MAC_ADDR_DEVICE_PATH *Node;
558 EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath;
559
560 if (RemainingDevicePath != NULL) {
561 if (!IsDevicePathEnd (RemainingDevicePath)) {
562 Node = (MAC_ADDR_DEVICE_PATH *)RemainingDevicePath;
563 if (Node->Header.Type != MESSAGING_DEVICE_PATH ||
564 Node->Header.SubType != MSG_MAC_ADDR_DP) {
565 // If the remaining device path does not match we don't support the request
566 return EFI_UNSUPPORTED;
567 }
568 }
569 }
570
571
572 //
573 // Open the IO Abstraction(s) needed to perform the supported test
574 //
575 Status = gBS->OpenProtocol (
576 ControllerHandle,
577 &gEmuIoThunkProtocolGuid,
578 (VOID **)&EmuIoThunk,
579 This->DriverBindingHandle,
580 ControllerHandle,
581 EFI_OPEN_PROTOCOL_BY_DRIVER
582 );
583 if (EFI_ERROR (Status)) {
584 return Status;
585 }
586
587 //
588 // Make sure GUID is for a File System handle.
589 //
590 Status = EFI_UNSUPPORTED;
591 if (CompareGuid (EmuIoThunk->Protocol, &gEmuSnpProtocolGuid)) {
592 Status = EFI_SUCCESS;
593 }
594
595 //
596 // Close the I/O Abstraction(s) used to perform the supported test
597 //
598 gBS->CloseProtocol (
599 ControllerHandle,
600 &gEmuIoThunkProtocolGuid,
601 This->DriverBindingHandle,
602 ControllerHandle
603 );
604
605
606 //
607 // Open the EFI Device Path protocol needed to perform the supported test
608 //
609 Status = gBS->OpenProtocol (
610 ControllerHandle,
611 &gEfiDevicePathProtocolGuid,
612 (VOID **) &ParentDevicePath,
613 This->DriverBindingHandle,
614 ControllerHandle,
615 EFI_OPEN_PROTOCOL_BY_DRIVER
616 );
617 if (Status == EFI_ALREADY_STARTED) {
618 return EFI_SUCCESS;
619 }
620
621 if (EFI_ERROR (Status)) {
622 return Status;
623 }
624
625 //
626 // Close protocol, don't use device path protocol in the Support() function
627 //
628 gBS->CloseProtocol (
629 ControllerHandle,
630 &gEfiDevicePathProtocolGuid,
631 This->DriverBindingHandle,
632 ControllerHandle
633 );
634
635 return Status;
636 }
637
638
639 /**
640 Start this driver on ControllerHandle. This service is called by the
641 EFI boot service ConnectController(). In order to make
642 drivers as small as possible, there are a few calling restrictions for
643 this service. ConnectController() must follow these
644 calling restrictions. If any other agent wishes to call Start() it
645 must also follow these calling restrictions.
646
647 @param This Protocol instance pointer.
648 @param ControllerHandle Handle of device to bind driver to
649 @param RemainingDevicePath Optional parameter use to pick a specific child
650 device to start.
651
652 @retval EFI_SUCCESS Always succeeds.
653
654 **/
655 EFI_STATUS
656 EFIAPI
657 EmuSnpDriverBindingStart (
658 IN EFI_DRIVER_BINDING_PROTOCOL *This,
659 IN EFI_HANDLE ControllerHandle,
660 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL
661 )
662 {
663 EFI_STATUS Status;
664 EMU_IO_THUNK_PROTOCOL *EmuIoThunk;
665 EMU_SNP_PRIVATE_DATA *Private;
666 MAC_ADDR_DEVICE_PATH Node;
667 EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath;
668
669 //
670 // Grab the protocols we need
671 //
672
673 Status = gBS->OpenProtocol(
674 ControllerHandle,
675 &gEfiDevicePathProtocolGuid,
676 ( VOID ** ) &ParentDevicePath,
677 This->DriverBindingHandle,
678 ControllerHandle,
679 EFI_OPEN_PROTOCOL_BY_DRIVER
680 );
681 if (EFI_ERROR (Status) && Status) {
682 return Status;
683 }
684
685 Status = gBS->OpenProtocol (
686 ControllerHandle,
687 &gEmuIoThunkProtocolGuid,
688 (VOID **)&EmuIoThunk,
689 This->DriverBindingHandle,
690 ControllerHandle,
691 EFI_OPEN_PROTOCOL_BY_DRIVER
692 );
693 if (EFI_ERROR (Status)) {
694 return Status;
695 }
696
697 if (!CompareGuid (EmuIoThunk->Protocol, &gEmuSnpProtocolGuid)) {
698 return EFI_UNSUPPORTED;
699 }
700
701 Status = EmuIoThunk->Open (EmuIoThunk);
702 if (EFI_ERROR (Status)) {
703 goto Done;
704 }
705
706 //
707 // Allocate the private data.
708 //
709 Private = AllocateZeroPool (sizeof (EMU_SNP_PRIVATE_DATA));
710 if (Private == NULL) {
711 Status = EFI_OUT_OF_RESOURCES;
712 goto Done;
713 }
714
715 CopyMem (&Private->Snp, &gEmuSnpTemplate, sizeof (EFI_SIMPLE_NETWORK_PROTOCOL));
716
717 Private->Signature = EMU_SNP_PRIVATE_DATA_SIGNATURE;
718 Private->IoThunk = EmuIoThunk;
719 Private->Io = EmuIoThunk->Interface;
720 Private->EfiHandle = ControllerHandle;
721 Private->DeviceHandle = NULL;
722 Private->Snp.Mode = &Private->Mode;
723 Private->ControllerNameTable = NULL;
724
725
726 Status = Private->Io->CreateMapping (Private->Io, &Private->Mode);
727 if (EFI_ERROR (Status)) {
728 goto Done;
729 }
730
731 //
732 // Build the device path by appending the MAC node to the ParentDevicePath
733 // from the EmuIo handle.
734 //
735 ZeroMem (&Node, sizeof (MAC_ADDR_DEVICE_PATH));
736
737 Node.Header.Type = MESSAGING_DEVICE_PATH;
738 Node.Header.SubType = MSG_MAC_ADDR_DP;
739 Node.IfType = Private->Mode.IfType;
740
741 SetDevicePathNodeLength ((EFI_DEVICE_PATH_PROTOCOL * )&Node, sizeof (MAC_ADDR_DEVICE_PATH));
742
743 CopyMem (&Node.MacAddress, &Private->Mode.CurrentAddress, sizeof (EFI_MAC_ADDRESS));
744
745 //
746 // Build the device path by appending the MAC node to the ParentDevicePath from the EmuIo handle.
747 //
748 Private->DevicePath = AppendDevicePathNode (ParentDevicePath, (EFI_DEVICE_PATH_PROTOCOL *)&Node);
749 if ( Private->DevicePath == NULL ) {
750 Status = EFI_OUT_OF_RESOURCES;
751 goto Done;
752 }
753
754 AddUnicodeString2 (
755 "eng",
756 gEmuSnpDriverComponentName.SupportedLanguages,
757 &Private->ControllerNameTable,
758 EmuIoThunk->ConfigString,
759 TRUE
760 );
761
762 AddUnicodeString2 (
763 "en",
764 gEmuSnpDriverComponentName2.SupportedLanguages,
765 &Private->ControllerNameTable,
766 EmuIoThunk->ConfigString,
767 FALSE
768 );
769
770 //
771 // Create Child Handle
772 //
773 Status = gBS->InstallMultipleProtocolInterfaces(
774 &Private->DeviceHandle,
775 &gEfiSimpleNetworkProtocolGuid, &Private->Snp,
776 &gEfiDevicePathProtocolGuid, Private->DevicePath,
777 NULL
778 );
779 if (EFI_ERROR (Status)) {
780 goto Done;
781 }
782
783 //
784 // Open For Child Device
785 //
786 Status = gBS->OpenProtocol (
787 ControllerHandle,
788 &gEmuIoThunkProtocolGuid,
789 (VOID **)&EmuIoThunk,
790 This->DriverBindingHandle,
791 Private->DeviceHandle,
792 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
793 );
794
795 Done:
796 if (EFI_ERROR (Status)) {
797 if (Private != NULL) {
798 FreePool (Private);
799 }
800 if (ParentDevicePath != NULL) {
801 gBS->CloseProtocol(
802 ControllerHandle,
803 &gEfiDevicePathProtocolGuid,
804 This->DriverBindingHandle,
805 ControllerHandle
806 );
807 }
808 }
809
810 return Status;
811 }
812
813 /**
814 Stop this driver on ControllerHandle. This service is called by the
815 EFI boot service DisconnectController(). In order to
816 make drivers as small as possible, there are a few calling
817 restrictions for this service. DisconnectController()
818 must follow these calling restrictions. If any other agent wishes
819 to call Stop() it must also follow these calling restrictions.
820
821 @param This Protocol instance pointer.
822 @param ControllerHandle Handle of device to stop driver on
823 @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
824 children is zero stop the entire bus driver.
825 @param ChildHandleBuffer List of Child Handles to Stop.
826
827 @retval EFI_SUCCESS Always succeeds.
828
829 **/
830 EFI_STATUS
831 EFIAPI
832 EmuSnpDriverBindingStop (
833 IN EFI_DRIVER_BINDING_PROTOCOL *This,
834 IN EFI_HANDLE ControllerHandle,
835 IN UINTN NumberOfChildren,
836 IN EFI_HANDLE *ChildHandleBuffer
837 )
838 {
839 EFI_STATUS Status;
840 EMU_SNP_PRIVATE_DATA *Private = NULL;
841 EFI_SIMPLE_NETWORK_PROTOCOL *Snp;
842 EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath;
843
844 //
845 // Complete all outstanding transactions to Controller.
846 // Don't allow any new transaction to Controller to be started.
847 //
848 if (NumberOfChildren == 0) {
849 //
850 // Close the bus driver
851 //
852 Status = gBS->CloseProtocol (
853 ControllerHandle,
854 &gEmuIoThunkProtocolGuid,
855 This->DriverBindingHandle,
856 ControllerHandle
857 );
858
859 Status = gBS->CloseProtocol (
860 ControllerHandle,
861 &gEfiDevicePathProtocolGuid,
862 This->DriverBindingHandle,
863 ControllerHandle
864 );
865 return Status;
866 }
867
868 ASSERT (NumberOfChildren == 1);
869
870
871 //
872 // Get our context back.
873 //
874 Status = gBS->OpenProtocol(
875 ChildHandleBuffer[0],
876 &gEfiSimpleNetworkProtocolGuid,
877 ( VOID ** ) &Snp,
878 This->DriverBindingHandle,
879 ControllerHandle,
880 EFI_OPEN_PROTOCOL_GET_PROTOCOL
881 );
882 if (EFI_ERROR (Status)) {
883 return EFI_DEVICE_ERROR;
884 }
885
886 Private = EMU_SNP_PRIVATE_DATA_FROM_SNP_THIS (Snp);
887 Status = Private->IoThunk->Close (Private->IoThunk);
888
889 Status = gBS->CloseProtocol(
890 ChildHandleBuffer[0],
891 &gEmuIoThunkProtocolGuid,
892 This->DriverBindingHandle,
893 Private->DeviceHandle
894 );
895
896 Status = gBS->UninstallMultipleProtocolInterfaces(
897 ChildHandleBuffer[0],
898 &gEfiSimpleNetworkProtocolGuid, &Private->Snp,
899 &gEfiDevicePathProtocolGuid, Private->DevicePath,
900 NULL
901 );
902
903 FreePool (Private->DevicePath);
904 FreeUnicodeStringTable (Private->ControllerNameTable);
905 FreePool (Private);
906
907 return EFI_SUCCESS;
908 }
909
910
911 EFI_DRIVER_BINDING_PROTOCOL gEmuSnpDriverBinding = {
912 EmuSnpDriverBindingSupported,
913 EmuSnpDriverBindingStart,
914 EmuSnpDriverBindingStop,
915 0xA,
916 NULL,
917 NULL
918 };
919
920
921
922 /**
923 This is the declaration of an EFI image entry point. This entry point is
924 the same for UEFI Applications, UEFI OS Loaders, and UEFI Drivers including
925 both device drivers and bus drivers.
926
927 @param ImageHandle The firmware allocated handle for the UEFI image.
928 @param SystemTable A pointer to the EFI System Table.
929
930 @retval EFI_SUCCESS The operation completed successfully.
931 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
932
933 **/
934 EFI_STATUS
935 EFIAPI
936 InitializeEmuSnpDriver (
937 IN EFI_HANDLE ImageHandle,
938 IN EFI_SYSTEM_TABLE *SystemTable
939 )
940 {
941 EFI_STATUS Status;
942
943 //
944 // Install the Driver Protocols
945 //
946
947 Status = EfiLibInstallDriverBindingComponentName2(
948 ImageHandle,
949 SystemTable,
950 &gEmuSnpDriverBinding,
951 ImageHandle,
952 &gEmuSnpDriverComponentName,
953 &gEmuSnpDriverComponentName2
954 );
955
956 return Status;
957 }