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