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