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