• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /** @file
2 
3 Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.<BR>
4 This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution.  The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
8 
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11 
12 Module Name:
13 
14   SnpNt32.c
15 
16 Abstract:
17 
18 -**/
19 
20 #include "SnpNt32.h"
21 
22 EFI_DRIVER_BINDING_PROTOCOL gSnpNt32DriverBinding = {
23   SnpNt32DriverBindingSupported,
24   SnpNt32DriverBindingStart,
25   SnpNt32DriverBindingStop,
26   0xa,
27   NULL,
28   NULL
29 };
30 
31 SNPNT32_GLOBAL_DATA         gSnpNt32GlobalData = {
32   SNP_NT32_DRIVER_SIGNATURE,  //  Signature
33   {
34     NULL,
35     NULL
36   },                          //  InstanceList
37   NULL,                       //  WinNtThunk
38   NULL,                       //  NetworkLibraryHandle
39   {
40     0
41   },                          //  NtNetUtilityTable
42   {
43     0,
44     0,
45     EfiLockUninitialized
46   },                          //  Lock
47   NULL,                       //  RecycledTxBuf
48   0,                          //  RecycledTxBufCount
49   32,                         //  MaxRecycledTxBuf
50   //
51   //  Private functions
52   //
53   SnpNt32InitializeGlobalData,            //  InitializeGlobalData
54   SnpNt32InitializeInstanceData,          //  InitializeInstanceData
55   SnpNt32CloseInstance                    //  CloseInstance
56 };
57 
58 /**
59   Changes the state of a network interface from "stopped" to "started".
60 
61   @param  This Protocol instance pointer.
62 
63   @retval EFI_SUCCESS           Always succeeds.
64 
65 **/
66 EFI_STATUS
67 EFIAPI
68 SnpNt32Start (
69   IN EFI_SIMPLE_NETWORK_PROTOCOL *This
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 SnpNt32Stop (
83   IN EFI_SIMPLE_NETWORK_PROTOCOL *This
84   );
85 
86 /**
87   Resets a network adapter and allocates the transmit and receive buffers
88   required by the network interface; optionally, also requests allocation
89   of additional transmit and receive buffers.
90 
91   @param  This              Protocol instance pointer.
92   @param  ExtraRxBufferSize The size, in bytes, of the extra receive buffer space
93                             that the driver should allocate for the network interface.
94                             Some network interfaces will not be able to use the extra
95                             buffer, and the caller will not know if it is actually
96                             being used.
97   @param  ExtraTxBufferSize The size, in bytes, of the extra transmit buffer space
98                             that the driver should allocate for the network interface.
99                             Some network interfaces will not be able to use the extra
100                             buffer, and the caller will not know if it is actually
101                             being used.
102 
103   @retval EFI_SUCCESS           Always succeeds.
104 
105 **/
106 EFI_STATUS
107 EFIAPI
108 SnpNt32Initialize (
109   IN EFI_SIMPLE_NETWORK_PROTOCOL *This,
110   IN UINTN                       ExtraRxBufferSize OPTIONAL,
111   IN UINTN                       ExtraTxBufferSize OPTIONAL
112   );
113 
114 /**
115   Resets a network adapter and re-initializes it with the parameters that were
116   provided in the previous call to Initialize().
117 
118   @param  This                 Protocol instance pointer.
119   @param  ExtendedVerification Indicates that the driver may perform a more
120                                exhaustive verification operation of the device
121                                during reset.
122 
123   @retval EFI_SUCCESS           Always succeeds.
124 
125 **/
126 EFI_STATUS
127 EFIAPI
128 SnpNt32Reset (
129   IN EFI_SIMPLE_NETWORK_PROTOCOL  *This,
130   IN BOOLEAN                      ExtendedVerification
131   );
132 
133 /**
134   Resets a network adapter and leaves it in a state that is safe for
135   another driver to initialize.
136 
137   @param  This Protocol instance pointer.
138 
139   @retval EFI_SUCCESS           Always succeeds.
140 
141 **/
142 EFI_STATUS
143 EFIAPI
144 SnpNt32Shutdown (
145   IN EFI_SIMPLE_NETWORK_PROTOCOL *This
146   );
147 
148 /**
149   Manages the multicast receive filters of a network interface.
150 
151   @param  This               Protocol instance pointer.
152   @param  EnableBits         A bit mask of receive filters to enable on the network interface.
153   @param  DisableBits        A bit mask of receive filters to disable on the network interface.
154   @param  ResetMcastFilter   Set to TRUE to reset the contents of the multicast receive
155                              filters on the network interface to their default values.
156   @param  McastFilterCount   Number of multicast HW MAC addresses in the new
157                              MCastFilter list. This value must be less than or equal to
158                              the MCastFilterCnt field of EFI_SIMPLE_NETWORK_MODE. This
159                              field is optional if ResetMCastFilter is TRUE.
160   @param  McastFilter        A pointer to a list of new multicast receive filter HW MAC
161                              addresses. This list will replace any existing multicast
162                              HW MAC address list. This field is optional if
163                              ResetMCastFilter is TRUE.
164 
165   @retval EFI_SUCCESS           The multicast receive filter list was updated.
166   @retval EFI_DEVICE_ERROR      The command could not be sent to the network interface.
167 
168 **/
169 EFI_STATUS
170 EFIAPI
171 SnpNt32ReceiveFilters (
172   IN EFI_SIMPLE_NETWORK_PROTOCOL *This,
173   IN UINT32                      EnableBits,
174   IN UINT32                      DisableBits,
175   IN BOOLEAN                     ResetMcastFilter,
176   IN UINTN                       McastFilterCount OPTIONAL,
177   IN EFI_MAC_ADDRESS             *McastFilter     OPTIONAL
178   );
179 
180 /**
181   Modifies or resets the current station address, if supported.
182 
183   @param  This         Protocol instance pointer.
184   @param  Reset        Flag used to reset the station address to the network interfaces
185                        permanent address.
186   @param  NewMacAddr   New station address to be used for the network interface.
187 
188   @retval EFI_UNSUPPORTED       Not supported yet.
189 
190 **/
191 EFI_STATUS
192 EFIAPI
193 SnpNt32StationAddress (
194   IN EFI_SIMPLE_NETWORK_PROTOCOL *This,
195   IN BOOLEAN                     Reset,
196   IN EFI_MAC_ADDRESS             *NewMacAddr OPTIONAL
197   );
198 
199 /**
200   Resets or collects the statistics on a network interface.
201 
202   @param  This            Protocol instance pointer.
203   @param  Reset           Set to TRUE to reset the statistics for the network interface.
204   @param  StatisticsSize  On input the size, in bytes, of StatisticsTable. On
205                           output the size, in bytes, of the resulting table of
206                           statistics.
207   @param  StatisticsTable A pointer to the EFI_NETWORK_STATISTICS structure that
208                           contains the statistics.
209 
210   @retval EFI_SUCCESS           The statistics were collected from the network interface.
211   @retval EFI_NOT_STARTED       The network interface has not been started.
212   @retval EFI_BUFFER_TOO_SMALL  The Statistics buffer was too small. The current buffer
213                                 size needed to hold the statistics is returned in
214                                 StatisticsSize.
215   @retval EFI_UNSUPPORTED       Not supported yet.
216 
217 **/
218 EFI_STATUS
219 EFIAPI
220 SnpNt32Statistics (
221   IN EFI_SIMPLE_NETWORK_PROTOCOL  * This,
222   IN BOOLEAN                      Reset,
223   IN OUT UINTN                    *StatisticsSize OPTIONAL,
224   OUT EFI_NETWORK_STATISTICS      *StatisticsTable OPTIONAL
225   );
226 
227 /**
228   Converts a multicast IP address to a multicast HW MAC address.
229 
230   @param  This  Protocol instance pointer.
231   @param  Ipv6  Set to TRUE if the multicast IP address is IPv6 [RFC 2460]. Set
232                 to FALSE if the multicast IP address is IPv4 [RFC 791].
233   @param  Ip    The multicast IP address that is to be converted to a multicast
234                 HW MAC address.
235   @param  Mac   The multicast HW MAC address that is to be generated from IP.
236 
237   @retval EFI_SUCCESS           The multicast IP address was mapped to the multicast
238                                 HW MAC address.
239   @retval EFI_NOT_STARTED       The network interface has not been started.
240   @retval EFI_BUFFER_TOO_SMALL  The Statistics buffer was too small. The current buffer
241                                 size needed to hold the statistics is returned in
242                                 StatisticsSize.
243   @retval EFI_UNSUPPORTED       Not supported yet.
244 
245 **/
246 EFI_STATUS
247 EFIAPI
248 SnpNt32McastIptoMac (
249   IN EFI_SIMPLE_NETWORK_PROTOCOL *This,
250   IN BOOLEAN                     Ipv6,
251   IN EFI_IP_ADDRESS              *Ip,
252   OUT EFI_MAC_ADDRESS            *Mac
253   );
254 
255 /**
256   Performs read and write operations on the NVRAM device attached to a
257   network interface.
258 
259   @param  This         Protocol instance pointer.
260   @param  ReadOrWrite  TRUE for read operations, FALSE for write operations.
261   @param  Offset       Byte offset in the NVRAM device at which to start the read or
262                        write operation. This must be a multiple of NvRamAccessSize and
263                        less than NvRamSize.
264   @param  BufferSize   The number of bytes to read or write from the NVRAM device.
265                        This must also be a multiple of NvramAccessSize.
266   @param  Buffer       A pointer to the data buffer.
267 
268   @retval EFI_UNSUPPORTED       Not supported yet.
269 
270 **/
271 EFI_STATUS
272 EFIAPI
273 SnpNt32Nvdata (
274   IN EFI_SIMPLE_NETWORK_PROTOCOL *This,
275   IN BOOLEAN                     ReadOrWrite,
276   IN UINTN                       Offset,
277   IN UINTN                       BufferSize,
278   IN OUT VOID                    *Buffer
279   );
280 
281 /**
282   Reads the current interrupt status and recycled transmit buffer status from
283   a network interface.
284 
285   @param  This            Protocol instance pointer.
286   @param  InterruptStatus A pointer to the bit mask of the currently active interrupts
287                           If this is NULL, the interrupt status will not be read from
288                           the device. If this is not NULL, the interrupt status will
289                           be read from the device. When the  interrupt status is read,
290                           it will also be cleared. Clearing the transmit  interrupt
291                           does not empty the recycled transmit buffer array.
292   @param  TxBuffer        Recycled transmit buffer address. The network interface will
293                           not transmit if its internal recycled transmit buffer array
294                           is full. Reading the transmit buffer does not clear the
295                           transmit interrupt. If this is NULL, then the transmit buffer
296                           status will not be read. If there are no transmit buffers to
297                           recycle and TxBuf is not NULL, * TxBuf will be set to NULL.
298 
299   @retval EFI_SUCCESS           Always succeeds.
300 
301 **/
302 EFI_STATUS
303 EFIAPI
304 SnpNt32GetStatus (
305   IN EFI_SIMPLE_NETWORK_PROTOCOL *This,
306   OUT UINT32                     *InterruptStatus,
307   OUT VOID                       **TxBuffer
308   );
309 
310 /**
311   Places a packet in the transmit queue of a network interface.
312 
313   @param  This       Protocol instance pointer.
314   @param  HeaderSize The size, in bytes, of the media header to be filled in by
315                      the Transmit() function. If HeaderSize is non-zero, then it
316                      must be equal to This->Mode->MediaHeaderSize and the DestAddr
317                      and Protocol parameters must not be NULL.
318   @param  BufferSize The size, in bytes, of the entire packet (media header and
319                      data) to be transmitted through the network interface.
320   @param  Buffer     A pointer to the packet (media header followed by data) to be
321                      transmitted. This parameter cannot be NULL. If HeaderSize is zero,
322                      then the media header in Buffer must already be filled in by the
323                      caller. If HeaderSize is non-zero, then the media header will be
324                      filled in by the Transmit() function.
325   @param  SrcAddr    The source HW MAC address. If HeaderSize is zero, then this parameter
326                      is ignored. If HeaderSize is non-zero and SrcAddr is NULL, then
327                      This->Mode->CurrentAddress is used for the source HW MAC address.
328   @param  DestAddr   The destination HW MAC address. If HeaderSize is zero, then this
329                      parameter is ignored.
330   @param  Protocol   The type of header to build. If HeaderSize is zero, then this
331                      parameter is ignored. See RFC 1700, section "Ether Types", for
332                      examples.
333 
334   @retval EFI_SUCCESS           The packet was placed on the transmit queue.
335   @retval EFI_DEVICE_ERROR      The command could not be sent to the network interface.
336   @retval EFI_ACCESS_DENIED     Error acquire global lock for operation.
337 
338 **/
339 EFI_STATUS
340 EFIAPI
341 SnpNt32Transmit (
342   IN EFI_SIMPLE_NETWORK_PROTOCOL *This,
343   IN UINTN                       HeaderSize,
344   IN UINTN                       BufferSize,
345   IN VOID                        *Buffer,
346   IN EFI_MAC_ADDRESS             *SrcAddr OPTIONAL,
347   IN EFI_MAC_ADDRESS             *DestAddr OPTIONAL,
348   IN UINT16                      *Protocol OPTIONAL
349   );
350 
351 /**
352   Receives a packet from a network interface.
353 
354   @param  This             Protocol instance pointer.
355   @param  HeaderSize       The size, in bytes, of the media header received on the network
356                            interface. If this parameter is NULL, then the media header size
357                            will not be returned.
358   @param  BuffSize         On entry, the size, in bytes, of Buffer. On exit, the size, in
359                            bytes, of the packet that was received on the network interface.
360   @param  Buffer           A pointer to the data buffer to receive both the media header and
361                            the data.
362   @param  SourceAddr       The source HW MAC address. If this parameter is NULL, the
363                            HW MAC source address will not be extracted from the media
364                            header.
365   @param  DestinationAddr  The destination HW MAC address. If this parameter is NULL,
366                            the HW MAC destination address will not be extracted from the
367                            media header.
368   @param  Protocol         The media header type. If this parameter is NULL, then the
369                            protocol will not be extracted from the media header. See
370                            RFC 1700 section "Ether Types" for examples.
371 
372   @retval  EFI_SUCCESS           The received data was stored in Buffer, and BufferSize has
373                                  been updated to the number of bytes received.
374   @retval  EFI_NOT_READY         The network interface is too busy to accept this transmit
375                                  request.
376   @retval  EFI_BUFFER_TOO_SMALL  The BufferSize parameter is too small.
377   @retval  EFI_DEVICE_ERROR      The command could not be sent to the network interface.
378   @retval  EFI_ACCESS_DENIED     Error acquire global lock for operation.
379 
380 **/
381 EFI_STATUS
382 EFIAPI
383 SnpNt32Receive (
384   IN EFI_SIMPLE_NETWORK_PROTOCOL *This,
385   OUT UINTN                      *HeaderSize,
386   IN OUT UINTN                   *BuffSize,
387   OUT VOID                       *Buffer,
388   OUT EFI_MAC_ADDRESS            *SourceAddr,
389   OUT EFI_MAC_ADDRESS            *DestinationAddr,
390   OUT UINT16                     *Protocol
391   );
392 
393 SNPNT32_INSTANCE_DATA gSnpNt32InstanceTemplate = {
394   SNP_NT32_INSTANCE_SIGNATURE,            //  Signature
395   {
396     NULL,
397     NULL
398   },                                      //  Entry
399   NULL,                                   //  GlobalData
400   NULL,                                   //  DeviceHandle
401   NULL,                                   //  DevicePath
402   {                                       //  Snp
403     EFI_SIMPLE_NETWORK_PROTOCOL_REVISION, //  Revision
404     SnpNt32Start,                         //  Start
405     SnpNt32Stop,                          //  Stop
406     SnpNt32Initialize,                    //  Initialize
407     SnpNt32Reset,                         //  Reset
408     SnpNt32Shutdown,                      //  Shutdown
409     SnpNt32ReceiveFilters,                //  ReceiveFilters
410     SnpNt32StationAddress,                //  StationAddress
411     SnpNt32Statistics,                    //  Statistics
412     SnpNt32McastIptoMac,                  //  MCastIpToMac
413     SnpNt32Nvdata,                        //  NvData
414     SnpNt32GetStatus,                     //  GetStatus
415     SnpNt32Transmit,                      //  Transmit
416     SnpNt32Receive,                       //  Receive
417     NULL,                                 //  WaitForPacket
418     NULL                                  //  Mode
419   },
420   {                                       //  Mode
421     EfiSimpleNetworkInitialized,          //  State
422     NET_ETHER_ADDR_LEN,                   //  HwAddressSize
423     NET_ETHER_HEADER_SIZE,                //  MediaHeaderSize
424     1500,                                 //  MaxPacketSize
425     0,                                    //  NvRamSize
426     0,                                    //  NvRamAccessSize
427     0,                                    //  ReceiveFilterMask
428     0,                                    //  ReceiveFilterSetting
429     MAX_MCAST_FILTER_CNT,                 //  MaxMCastFilterCount
430     0,                                    //  MCastFilterCount
431     {
432       0
433     },                                    //  MCastFilter
434     {
435       0
436     },                                    //  CurrentAddress
437     {
438       0
439     },                                    //  BroadcastAddress
440     {
441       0
442     },                                    //  PermanentAddress
443     NET_IFTYPE_ETHERNET,                  //  IfType
444     FALSE,                                //  MacAddressChangeable
445     FALSE,                                //  MultipleTxSupported
446     TRUE,                                 //  MediaPresentSupported
447     TRUE                                  //  MediaPresent
448   },
449   {
450     0
451   }                                       //  InterfaceInfo
452 };
453 
454 /**
455   Test to see if this driver supports ControllerHandle. This service
456   is called by the EFI boot service ConnectController(). In
457   order to make drivers as small as possible, there are a few calling
458   restrictions for this service. ConnectController() must
459   follow these calling restrictions. If any other agent wishes to call
460   Supported() it must also follow these calling restrictions.
461 
462   @param  This                Protocol instance pointer.
463   @param  ControllerHandle    Handle of device to test
464   @param  RemainingDevicePath Optional parameter use to pick a specific child
465                               device to start.
466 
467   @retval EFI_SUCCESS         This driver supports this device
468   @retval EFI_UNSUPPORTED     This driver does not support this device
469 
470 **/
471 EFI_STATUS
472 EFIAPI
SnpNt32DriverBindingSupported(IN EFI_DRIVER_BINDING_PROTOCOL * This,IN EFI_HANDLE ControllerHandle,IN EFI_DEVICE_PATH_PROTOCOL * RemainingDevicePath OPTIONAL)473 SnpNt32DriverBindingSupported (
474   IN EFI_DRIVER_BINDING_PROTOCOL  * This,
475   IN EFI_HANDLE                   ControllerHandle,
476   IN EFI_DEVICE_PATH_PROTOCOL     * RemainingDevicePath OPTIONAL
477   )
478 {
479 
480   SNPNT32_GLOBAL_DATA   *GlobalData;
481   LIST_ENTRY            *Entry;
482   SNPNT32_INSTANCE_DATA *Instance;
483 
484   GlobalData = &gSnpNt32GlobalData;
485 
486   NET_LIST_FOR_EACH (Entry, &GlobalData->InstanceList) {
487 
488     Instance = NET_LIST_USER_STRUCT_S (Entry, SNPNT32_INSTANCE_DATA, Entry, SNP_NT32_INSTANCE_SIGNATURE);
489 
490     if (Instance->DeviceHandle == ControllerHandle) {
491       return EFI_SUCCESS;
492     }
493 
494   }
495 
496   return EFI_UNSUPPORTED;
497 }
498 
499 
500 /**
501   Start this driver on ControllerHandle. This service is called by the
502   EFI boot service ConnectController(). In order to make
503   drivers as small as possible, there are a few calling restrictions for
504   this service. ConnectController() must follow these
505   calling restrictions. If any other agent wishes to call Start() it
506   must also follow these calling restrictions.
507 
508   @param  This                 Protocol instance pointer.
509   @param  ControllerHandle     Handle of device to bind driver to
510   @param  RemainingDevicePath  Optional parameter use to pick a specific child
511                                device to start.
512 
513   @retval EFI_SUCCESS          Always succeeds.
514 
515 **/
516 EFI_STATUS
517 EFIAPI
SnpNt32DriverBindingStart(IN EFI_DRIVER_BINDING_PROTOCOL * This,IN EFI_HANDLE ControllerHandle,IN EFI_DEVICE_PATH_PROTOCOL * RemainingDevicePath OPTIONAL)518 SnpNt32DriverBindingStart (
519   IN EFI_DRIVER_BINDING_PROTOCOL  * This,
520   IN EFI_HANDLE                   ControllerHandle,
521   IN EFI_DEVICE_PATH_PROTOCOL     * RemainingDevicePath OPTIONAL
522   )
523 {
524   return EFI_SUCCESS;
525 }
526 
527 /**
528   Stop this driver on ControllerHandle. This service is called by the
529   EFI boot service DisconnectController(). In order to
530   make drivers as small as possible, there are a few calling
531   restrictions for this service. DisconnectController()
532   must follow these calling restrictions. If any other agent wishes
533   to call Stop() it must also follow these calling restrictions.
534 
535   @param  This              Protocol instance pointer.
536   @param  ControllerHandle  Handle of device to stop driver on
537   @param  NumberOfChildren  Number of Handles in ChildHandleBuffer. If number of
538                             children is zero stop the entire bus driver.
539   @param  ChildHandleBuffer List of Child Handles to Stop.
540 
541   @retval EFI_SUCCESS       Always succeeds.
542 
543 **/
544 EFI_STATUS
545 EFIAPI
SnpNt32DriverBindingStop(IN EFI_DRIVER_BINDING_PROTOCOL * This,IN EFI_HANDLE ControllerHandle,IN UINTN NumberOfChildren,IN EFI_HANDLE * ChildHandleBuffer)546 SnpNt32DriverBindingStop (
547   IN  EFI_DRIVER_BINDING_PROTOCOL  *This,
548   IN  EFI_HANDLE                   ControllerHandle,
549   IN  UINTN                        NumberOfChildren,
550   IN  EFI_HANDLE                   *ChildHandleBuffer
551   )
552 {
553   return EFI_SUCCESS;
554 }
555 
556 
557 /**
558   Changes the state of a network interface from "stopped" to "started".
559 
560   @param  This Protocol instance pointer.
561 
562   @retval EFI_SUCCESS           Always succeeds.
563 
564 **/
565 EFI_STATUS
566 EFIAPI
SnpNt32Start(IN EFI_SIMPLE_NETWORK_PROTOCOL * This)567 SnpNt32Start (
568   IN EFI_SIMPLE_NETWORK_PROTOCOL *This
569   )
570 {
571   return EFI_SUCCESS;
572 }
573 
574 
575 /**
576   Changes the state of a network interface from "started" to "stopped".
577 
578   @param  This Protocol instance pointer.
579 
580   @retval EFI_SUCCESS           Always succeeds.
581 
582 **/
583 EFI_STATUS
584 EFIAPI
SnpNt32Stop(IN EFI_SIMPLE_NETWORK_PROTOCOL * This)585 SnpNt32Stop (
586   IN EFI_SIMPLE_NETWORK_PROTOCOL *This
587   )
588 {
589   return EFI_SUCCESS;
590 }
591 
592 /**
593   Resets a network adapter and allocates the transmit and receive buffers
594   required by the network interface; optionally, also requests allocation
595   of additional transmit and receive buffers.
596 
597   @param  This              Protocol instance pointer.
598   @param  ExtraRxBufferSize The size, in bytes, of the extra receive buffer space
599                             that the driver should allocate for the network interface.
600                             Some network interfaces will not be able to use the extra
601                             buffer, and the caller will not know if it is actually
602                             being used.
603   @param  ExtraTxBufferSize The size, in bytes, of the extra transmit buffer space
604                             that the driver should allocate for the network interface.
605                             Some network interfaces will not be able to use the extra
606                             buffer, and the caller will not know if it is actually
607                             being used.
608 
609   @retval EFI_SUCCESS           Always succeeds.
610 
611 **/
612 EFI_STATUS
613 EFIAPI
SnpNt32Initialize(IN EFI_SIMPLE_NETWORK_PROTOCOL * This,IN UINTN ExtraRxBufferSize OPTIONAL,IN UINTN ExtraTxBufferSize OPTIONAL)614 SnpNt32Initialize (
615   IN EFI_SIMPLE_NETWORK_PROTOCOL *This,
616   IN UINTN                       ExtraRxBufferSize OPTIONAL,
617   IN UINTN                       ExtraTxBufferSize OPTIONAL
618   )
619 {
620   return EFI_SUCCESS;
621 }
622 
623 /**
624   Resets a network adapter and re-initializes it with the parameters that were
625   provided in the previous call to Initialize().
626 
627   @param  This                 Protocol instance pointer.
628   @param  ExtendedVerification Indicates that the driver may perform a more
629                                exhaustive verification operation of the device
630                                during reset.
631 
632   @retval EFI_SUCCESS           Always succeeds.
633 
634 **/
635 EFI_STATUS
636 EFIAPI
SnpNt32Reset(IN EFI_SIMPLE_NETWORK_PROTOCOL * This,IN BOOLEAN ExtendedVerification)637 SnpNt32Reset (
638   IN EFI_SIMPLE_NETWORK_PROTOCOL  *This,
639   IN BOOLEAN                      ExtendedVerification
640   )
641 {
642   return EFI_SUCCESS;
643 }
644 
645 /**
646   Resets a network adapter and leaves it in a state that is safe for
647   another driver to initialize.
648 
649   @param  This Protocol instance pointer.
650 
651   @retval EFI_SUCCESS           Always succeeds.
652 
653 **/
654 EFI_STATUS
655 EFIAPI
SnpNt32Shutdown(IN EFI_SIMPLE_NETWORK_PROTOCOL * This)656 SnpNt32Shutdown (
657   IN EFI_SIMPLE_NETWORK_PROTOCOL *This
658   )
659 {
660   return EFI_SUCCESS;
661 }
662 
663 /**
664   Manages the multicast receive filters of a network interface.
665 
666   @param  This               Protocol instance pointer.
667   @param  EnableBits         A bit mask of receive filters to enable on the network interface.
668   @param  DisableBits        A bit mask of receive filters to disable on the network interface.
669   @param  ResetMcastFilter   Set to TRUE to reset the contents of the multicast receive
670                              filters on the network interface to their default values.
671   @param  McastFilterCount   Number of multicast HW MAC addresses in the new
672                              MCastFilter list. This value must be less than or equal to
673                              the MCastFilterCnt field of EFI_SIMPLE_NETWORK_MODE. This
674                              field is optional if ResetMCastFilter is TRUE.
675   @param  McastFilter        A pointer to a list of new multicast receive filter HW MAC
676                              addresses. This list will replace any existing multicast
677                              HW MAC address list. This field is optional if
678                              ResetMCastFilter is TRUE.
679 
680   @retval EFI_SUCCESS           The multicast receive filter list was updated.
681   @retval EFI_DEVICE_ERROR      The command could not be sent to the network interface.
682 
683 **/
684 EFI_STATUS
685 EFIAPI
SnpNt32ReceiveFilters(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)686 SnpNt32ReceiveFilters (
687   IN EFI_SIMPLE_NETWORK_PROTOCOL *This,
688   IN UINT32                      EnableBits,
689   IN UINT32                      DisableBits,
690   IN BOOLEAN                     ResetMcastFilter,
691   IN UINTN                       McastFilterCount OPTIONAL,
692   IN EFI_MAC_ADDRESS             *McastFilter     OPTIONAL
693   )
694 {
695   SNPNT32_INSTANCE_DATA *Instance;
696   SNPNT32_GLOBAL_DATA   *GlobalData;
697   INT32                 ReturnValue;
698 
699   Instance    = SNP_NT32_INSTANCE_DATA_FROM_SNP_THIS (This);
700 
701   GlobalData  = Instance->GlobalData;
702 
703   if (EFI_ERROR (EfiAcquireLockOrFail (&GlobalData->Lock))) {
704     return EFI_ACCESS_DENIED;
705   }
706 
707   ReturnValue = GlobalData->NtNetUtilityTable.SetReceiveFilter (
708                                                 Instance->InterfaceInfo.InterfaceIndex,
709                                                 EnableBits,
710                                                 (UINT32)McastFilterCount,
711                                                 McastFilter
712                                                 );
713 
714   EfiReleaseLock (&GlobalData->Lock);
715 
716   if (ReturnValue <= 0) {
717     return EFI_DEVICE_ERROR;
718   }
719 
720   return EFI_SUCCESS;
721 }
722 
723 /**
724   Modifies or resets the current station address, if supported.
725 
726   @param  This         Protocol instance pointer.
727   @param  Reset        Flag used to reset the station address to the network interfaces
728                        permanent address.
729   @param  NewMacAddr   New station address to be used for the network interface.
730 
731   @retval EFI_UNSUPPORTED       Not supported yet.
732 
733 **/
734 EFI_STATUS
735 EFIAPI
SnpNt32StationAddress(IN EFI_SIMPLE_NETWORK_PROTOCOL * This,IN BOOLEAN Reset,IN EFI_MAC_ADDRESS * NewMacAddr OPTIONAL)736 SnpNt32StationAddress (
737   IN EFI_SIMPLE_NETWORK_PROTOCOL *This,
738   IN BOOLEAN                     Reset,
739   IN EFI_MAC_ADDRESS             *NewMacAddr OPTIONAL
740   )
741 {
742   return EFI_UNSUPPORTED;
743 }
744 
745 /**
746   Resets or collects the statistics on a network interface.
747 
748   @param  This            Protocol instance pointer.
749   @param  Reset           Set to TRUE to reset the statistics for the network interface.
750   @param  StatisticsSize  On input the size, in bytes, of StatisticsTable. On
751                           output the size, in bytes, of the resulting table of
752                           statistics.
753   @param  StatisticsTable A pointer to the EFI_NETWORK_STATISTICS structure that
754                           contains the statistics.
755 
756   @retval EFI_SUCCESS           The statistics were collected from the network interface.
757   @retval EFI_NOT_STARTED       The network interface has not been started.
758   @retval EFI_BUFFER_TOO_SMALL  The Statistics buffer was too small. The current buffer
759                                 size needed to hold the statistics is returned in
760                                 StatisticsSize.
761   @retval EFI_UNSUPPORTED       Not supported yet.
762 
763 **/
764 EFI_STATUS
765 EFIAPI
SnpNt32Statistics(IN EFI_SIMPLE_NETWORK_PROTOCOL * This,IN BOOLEAN Reset,IN OUT UINTN * StatisticsSize OPTIONAL,OUT EFI_NETWORK_STATISTICS * StatisticsTable OPTIONAL)766 SnpNt32Statistics (
767   IN EFI_SIMPLE_NETWORK_PROTOCOL  * This,
768   IN BOOLEAN                      Reset,
769   IN OUT UINTN                    *StatisticsSize OPTIONAL,
770   OUT EFI_NETWORK_STATISTICS      *StatisticsTable OPTIONAL
771   )
772 {
773   return EFI_UNSUPPORTED;
774 }
775 
776 /**
777   Converts a multicast IP address to a multicast HW MAC address.
778 
779   @param  This Protocol instance pointer.
780   @param  Ipv6 Set to TRUE if the multicast IP address is IPv6 [RFC 2460]. Set
781                to FALSE if the multicast IP address is IPv4 [RFC 791].
782   @param  Ip   The multicast IP address that is to be converted to a multicast
783                HW MAC address.
784   @param  Mac  The multicast HW MAC address that is to be generated from IP.
785 
786   @retval EFI_SUCCESS           The multicast IP address was mapped to the multicast
787                                 HW MAC address.
788   @retval EFI_NOT_STARTED       The network interface has not been started.
789   @retval EFI_BUFFER_TOO_SMALL  The Statistics buffer was too small. The current buffer
790                                 size needed to hold the statistics is returned in
791                                 StatisticsSize.
792   @retval EFI_UNSUPPORTED       Not supported yet.
793 
794 **/
795 EFI_STATUS
796 EFIAPI
SnpNt32McastIptoMac(IN EFI_SIMPLE_NETWORK_PROTOCOL * This,IN BOOLEAN Ipv6,IN EFI_IP_ADDRESS * Ip,OUT EFI_MAC_ADDRESS * Mac)797 SnpNt32McastIptoMac (
798   IN EFI_SIMPLE_NETWORK_PROTOCOL *This,
799   IN BOOLEAN                     Ipv6,
800   IN EFI_IP_ADDRESS              *Ip,
801   OUT EFI_MAC_ADDRESS            *Mac
802   )
803 {
804   return EFI_UNSUPPORTED;
805 }
806 
807 
808 /**
809   Performs read and write operations on the NVRAM device attached to a
810   network interface.
811 
812   @param  This         Protocol instance pointer.
813   @param  ReadOrWrite  TRUE for read operations, FALSE for write operations.
814   @param  Offset       Byte offset in the NVRAM device at which to start the read or
815                        write operation. This must be a multiple of NvRamAccessSize and
816                        less than NvRamSize.
817   @param  BufferSize   The number of bytes to read or write from the NVRAM device.
818                        This must also be a multiple of NvramAccessSize.
819   @param  Buffer       A pointer to the data buffer.
820 
821   @retval EFI_UNSUPPORTED       Not supported yet.
822 
823 **/
824 EFI_STATUS
825 EFIAPI
SnpNt32Nvdata(IN EFI_SIMPLE_NETWORK_PROTOCOL * This,IN BOOLEAN ReadOrWrite,IN UINTN Offset,IN UINTN BufferSize,IN OUT VOID * Buffer)826 SnpNt32Nvdata (
827   IN EFI_SIMPLE_NETWORK_PROTOCOL *This,
828   IN BOOLEAN                     ReadOrWrite,
829   IN UINTN                       Offset,
830   IN UINTN                       BufferSize,
831   IN OUT VOID                    *Buffer
832   )
833 {
834   return EFI_UNSUPPORTED;
835 }
836 
837 
838 /**
839   Reads the current interrupt status and recycled transmit buffer status from
840   a network interface.
841 
842   @param  This            Protocol instance pointer.
843   @param  InterruptStatus A pointer to the bit mask of the currently active interrupts
844                           If this is NULL, the interrupt status will not be read from
845                           the device. If this is not NULL, the interrupt status will
846                           be read from the device. When the  interrupt status is read,
847                           it will also be cleared. Clearing the transmit  interrupt
848                           does not empty the recycled transmit buffer array.
849   @param  TxBuffer        Recycled transmit buffer address. The network interface will
850                           not transmit if its internal recycled transmit buffer array
851                           is full. Reading the transmit buffer does not clear the
852                           transmit interrupt. If this is NULL, then the transmit buffer
853                           status will not be read. If there are no transmit buffers to
854                           recycle and TxBuf is not NULL, * TxBuf will be set to NULL.
855 
856   @retval EFI_SUCCESS           Always succeeds.
857 
858 **/
859 EFI_STATUS
860 EFIAPI
SnpNt32GetStatus(IN EFI_SIMPLE_NETWORK_PROTOCOL * This,OUT UINT32 * InterruptStatus,OUT VOID ** TxBuffer)861 SnpNt32GetStatus (
862   IN EFI_SIMPLE_NETWORK_PROTOCOL *This,
863   OUT UINT32                     *InterruptStatus,
864   OUT VOID                       **TxBuffer
865   )
866 {
867   SNPNT32_INSTANCE_DATA *Instance;
868   SNPNT32_GLOBAL_DATA   *GlobalData;
869 
870   Instance    = SNP_NT32_INSTANCE_DATA_FROM_SNP_THIS (This);
871 
872   GlobalData  = Instance->GlobalData;
873 
874   if (TxBuffer != NULL) {
875     if (GlobalData->RecycledTxBufCount != 0) {
876       GlobalData->RecycledTxBufCount --;
877       *((UINT8 **) TxBuffer)    = (UINT8 *) (UINTN)GlobalData->RecycledTxBuf[GlobalData->RecycledTxBufCount];
878     } else {
879       *((UINT8 **) TxBuffer)    = NULL;
880     }
881   }
882 
883   if (InterruptStatus != NULL) {
884     *InterruptStatus = EFI_SIMPLE_NETWORK_TRANSMIT_INTERRUPT;
885   }
886 
887   return EFI_SUCCESS;
888 }
889 
890 
891 /**
892   Places a packet in the transmit queue of a network interface.
893 
894   @param  This       Protocol instance pointer.
895   @param  HeaderSize The size, in bytes, of the media header to be filled in by
896                      the Transmit() function. If HeaderSize is non-zero, then it
897                      must be equal to This->Mode->MediaHeaderSize and the DestAddr
898                      and Protocol parameters must not be NULL.
899   @param  BufferSize The size, in bytes, of the entire packet (media header and
900                      data) to be transmitted through the network interface.
901   @param  Buffer     A pointer to the packet (media header followed by data) to be
902                      transmitted. This parameter cannot be NULL. If HeaderSize is zero,
903                      then the media header in Buffer must already be filled in by the
904                      caller. If HeaderSize is non-zero, then the media header will be
905                      filled in by the Transmit() function.
906   @param  SrcAddr    The source HW MAC address. If HeaderSize is zero, then this parameter
907                      is ignored. If HeaderSize is non-zero and SrcAddr is NULL, then
908                      This->Mode->CurrentAddress is used for the source HW MAC address.
909   @param  DestAddr   The destination HW MAC address. If HeaderSize is zero, then this
910                      parameter is ignored.
911   @param  Protocol   The type of header to build. If HeaderSize is zero, then this
912                      parameter is ignored. See RFC 1700, section "Ether Types", for
913                      examples.
914 
915   @retval EFI_SUCCESS           The packet was placed on the transmit queue.
916   @retval EFI_DEVICE_ERROR      The command could not be sent to the network interface.
917   @retval EFI_ACCESS_DENIED     Error acquire global lock for operation.
918 
919 **/
920 EFI_STATUS
921 EFIAPI
SnpNt32Transmit(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)922 SnpNt32Transmit (
923   IN EFI_SIMPLE_NETWORK_PROTOCOL *This,
924   IN UINTN                       HeaderSize,
925   IN UINTN                       BufferSize,
926   IN VOID                        *Buffer,
927   IN EFI_MAC_ADDRESS             *SrcAddr OPTIONAL,
928   IN EFI_MAC_ADDRESS             *DestAddr OPTIONAL,
929   IN UINT16                      *Protocol OPTIONAL
930   )
931 {
932   SNPNT32_INSTANCE_DATA *Instance;
933   SNPNT32_GLOBAL_DATA   *GlobalData;
934   INT32                 ReturnValue;
935   UINT64                *Tmp;
936 
937   Instance    = SNP_NT32_INSTANCE_DATA_FROM_SNP_THIS (This);
938 
939   GlobalData  = Instance->GlobalData;
940 
941   if ((HeaderSize != 0) && (SrcAddr == NULL)) {
942     SrcAddr = &Instance->Mode.CurrentAddress;
943   }
944 
945   if (EFI_ERROR (EfiAcquireLockOrFail (&GlobalData->Lock))) {
946     return EFI_ACCESS_DENIED;
947   }
948 
949   ReturnValue = GlobalData->NtNetUtilityTable.Transmit (
950                                                 Instance->InterfaceInfo.InterfaceIndex,
951                                                 (UINT32)HeaderSize,
952                                                 (UINT32)BufferSize,
953                                                 Buffer,
954                                                 SrcAddr,
955                                                 DestAddr,
956                                                 Protocol
957                                                 );
958 
959   EfiReleaseLock (&GlobalData->Lock);
960 
961   if (ReturnValue < 0) {
962     return EFI_DEVICE_ERROR;
963   } else {
964     if ((GlobalData->MaxRecycledTxBuf + SNP_TX_BUFFER_INCREASEMENT) >= SNP_MAX_TX_BUFFER_NUM) {
965       return EFI_NOT_READY;
966     }
967 
968     if (GlobalData->RecycledTxBufCount < GlobalData->MaxRecycledTxBuf) {
969       GlobalData->RecycledTxBuf[GlobalData->RecycledTxBufCount] = (UINT64) Buffer;
970       GlobalData->RecycledTxBufCount ++;
971     } else {
972       Tmp = AllocatePool (sizeof (UINT64) * (GlobalData->MaxRecycledTxBuf + SNP_TX_BUFFER_INCREASEMENT));
973       if (Tmp == NULL) {
974         return EFI_DEVICE_ERROR;
975       }
976       CopyMem (Tmp, GlobalData->RecycledTxBuf, sizeof (UINT64) * GlobalData->RecycledTxBufCount);
977       FreePool (GlobalData->RecycledTxBuf);
978       GlobalData->RecycledTxBuf    =  Tmp;
979       GlobalData->MaxRecycledTxBuf += SNP_TX_BUFFER_INCREASEMENT;
980     }
981   }
982 
983   return EFI_SUCCESS;
984 }
985 
986 /**
987   Receives a packet from a network interface.
988 
989   @param  This             Protocol instance pointer.
990   @param  HeaderSize       The size, in bytes, of the media header received on the network
991                            interface. If this parameter is NULL, then the media header size
992                            will not be returned.
993   @param  BuffSize         On entry, the size, in bytes, of Buffer. On exit, the size, in
994                            bytes, of the packet that was received on the network interface.
995   @param  Buffer           A pointer to the data buffer to receive both the media header and
996                            the data.
997   @param  SourceAddr       The source HW MAC address. If this parameter is NULL, the
998                            HW MAC source address will not be extracted from the media
999                            header.
1000   @param  DestinationAddr  The destination HW MAC address. If this parameter is NULL,
1001                            the HW MAC destination address will not be extracted from the
1002                            media header.
1003   @param  Protocol         The media header type. If this parameter is NULL, then the
1004                            protocol will not be extracted from the media header. See
1005                            RFC 1700 section "Ether Types" for examples.
1006 
1007   @retval  EFI_SUCCESS           The received data was stored in Buffer, and BufferSize has
1008                                  been updated to the number of bytes received.
1009   @retval  EFI_NOT_READY         The network interface is too busy to accept this transmit
1010                                  request.
1011   @retval  EFI_BUFFER_TOO_SMALL  The BufferSize parameter is too small.
1012   @retval  EFI_DEVICE_ERROR      The command could not be sent to the network interface.
1013   @retval  EFI_ACCESS_DENIED     Error acquire global lock for operation.
1014 
1015 **/
1016 EFI_STATUS
1017 EFIAPI
SnpNt32Receive(IN EFI_SIMPLE_NETWORK_PROTOCOL * This,OUT UINTN * HeaderSize,IN OUT UINTN * BuffSize,OUT VOID * Buffer,OUT EFI_MAC_ADDRESS * SourceAddr,OUT EFI_MAC_ADDRESS * DestinationAddr,OUT UINT16 * Protocol)1018 SnpNt32Receive (
1019   IN EFI_SIMPLE_NETWORK_PROTOCOL *This,
1020   OUT UINTN                      *HeaderSize,
1021   IN OUT UINTN                   *BuffSize,
1022   OUT VOID                       *Buffer,
1023   OUT EFI_MAC_ADDRESS            *SourceAddr,
1024   OUT EFI_MAC_ADDRESS            *DestinationAddr,
1025   OUT UINT16                     *Protocol
1026   )
1027 {
1028   SNPNT32_INSTANCE_DATA *Instance;
1029   SNPNT32_GLOBAL_DATA   *GlobalData;
1030   INT32                 ReturnValue;
1031   UINTN                 BufSize;
1032 
1033   BufSize     = *BuffSize;
1034 
1035   Instance    = SNP_NT32_INSTANCE_DATA_FROM_SNP_THIS (This);
1036 
1037   GlobalData  = Instance->GlobalData;
1038 
1039   ASSERT (GlobalData->NtNetUtilityTable.Receive != NULL);
1040 
1041   if (EFI_ERROR (EfiAcquireLockOrFail (&GlobalData->Lock))) {
1042     return EFI_ACCESS_DENIED;
1043   }
1044 
1045   ReturnValue = GlobalData->NtNetUtilityTable.Receive (
1046                                                 Instance->InterfaceInfo.InterfaceIndex,
1047                                                 BuffSize,
1048                                                 Buffer
1049                                                 );
1050 
1051   EfiReleaseLock (&GlobalData->Lock);
1052 
1053   if (ReturnValue < 0) {
1054     if (ReturnValue == -100) {
1055       return EFI_BUFFER_TOO_SMALL;
1056     }
1057 
1058     return EFI_DEVICE_ERROR;
1059   } else if (ReturnValue == 0) {
1060     return EFI_NOT_READY;
1061   }
1062 
1063   if (HeaderSize != NULL) {
1064     *HeaderSize = 14;
1065   }
1066 
1067   if (SourceAddr != NULL) {
1068     ZeroMem (SourceAddr, sizeof (EFI_MAC_ADDRESS));
1069     CopyMem (SourceAddr, ((UINT8 *) Buffer) + 6, 6);
1070   }
1071 
1072   if (DestinationAddr != NULL) {
1073     ZeroMem (DestinationAddr, sizeof (EFI_MAC_ADDRESS));
1074     CopyMem (DestinationAddr, ((UINT8 *) Buffer), 6);
1075   }
1076 
1077   if (Protocol != NULL) {
1078     *Protocol = NTOHS (*((UINT16 *) (((UINT8 *) Buffer) + 12)));
1079   }
1080 
1081   return (*BuffSize <= BufSize) ? EFI_SUCCESS : EFI_BUFFER_TOO_SMALL;
1082 }
1083 
1084 /**
1085   Initialize the driver's global data.
1086 
1087   @param  This                  Pointer to the global context data.
1088 
1089   @retval EFI_SUCCESS           The global data is initialized.
1090   @retval EFI_NOT_FOUND         The required DLL is not found.
1091   @retval EFI_DEVICE_ERROR      Error initialize network utility library.
1092   @retval EFI_OUT_OF_RESOURCES  Out of resource.
1093   @retval other                 Other errors.
1094 
1095 **/
1096 EFI_STATUS
SnpNt32InitializeGlobalData(IN OUT SNPNT32_GLOBAL_DATA * This)1097 SnpNt32InitializeGlobalData (
1098   IN OUT SNPNT32_GLOBAL_DATA *This
1099   )
1100 {
1101   EFI_STATUS            Status;
1102   CHAR16                *DllFileNameU;
1103   UINT32                Index;
1104   INT32                 ReturnValue;
1105   BOOLEAN               NetUtilityLibInitDone;
1106   NT_NET_INTERFACE_INFO NetInterfaceInfoBuffer[MAX_INTERFACE_INFO_NUMBER];
1107   SNPNT32_INSTANCE_DATA *Instance;
1108   LIST_ENTRY            *Entry;
1109   UINT32                InterfaceCount;
1110 
1111   ASSERT (This != NULL);
1112 
1113   NetUtilityLibInitDone = FALSE;
1114   InterfaceCount        = MAX_INTERFACE_INFO_NUMBER;
1115 
1116   InitializeListHead (&This->InstanceList);
1117   EfiInitializeLock (&This->Lock, TPL_CALLBACK);
1118 
1119   This->RecycledTxBuf = AllocatePool (sizeof (UINT64) * This->MaxRecycledTxBuf);
1120   if (This->RecycledTxBuf == NULL) {
1121     return EFI_OUT_OF_RESOURCES;
1122   }
1123 
1124   //
1125   //  Get the WinNT thunk
1126   //
1127   Status = gBS->LocateProtocol (&gEfiWinNtThunkProtocolGuid, NULL, (VOID **)&This->WinNtThunk);
1128 
1129   if (EFI_ERROR (Status)) {
1130     return Status;
1131   }
1132 
1133   ASSERT (This->WinNtThunk != NULL);
1134 
1135   DllFileNameU = NETWORK_LIBRARY_NAME_U;
1136 
1137   //
1138   //  Load network utility library
1139   //
1140   This->NetworkLibraryHandle = This->WinNtThunk->LoadLibraryEx (DllFileNameU, NULL, 0);
1141 
1142   if (NULL == This->NetworkLibraryHandle) {
1143     return EFI_NOT_FOUND;
1144   }
1145 
1146   This->NtNetUtilityTable.Initialize = (NT_NET_INITIALIZE) This->WinNtThunk->GetProcAddress (
1147                                                                                This->NetworkLibraryHandle,
1148                                                                                NETWORK_LIBRARY_INITIALIZE
1149                                                                                );
1150 
1151   if (NULL == This->NtNetUtilityTable.Initialize) {
1152     Status = EFI_NOT_FOUND;
1153     goto ErrorReturn;
1154   }
1155 
1156   This->NtNetUtilityTable.Finalize = (NT_NET_FINALIZE) This->WinNtThunk->GetProcAddress (
1157                                                                            This->NetworkLibraryHandle,
1158                                                                            NETWORK_LIBRARY_FINALIZE
1159                                                                            );
1160 
1161   if (NULL == This->NtNetUtilityTable.Finalize) {
1162     Status = EFI_NOT_FOUND;
1163     goto ErrorReturn;
1164   }
1165 
1166   This->NtNetUtilityTable.SetReceiveFilter = (NT_NET_SET_RECEIVE_FILTER) This->WinNtThunk->GetProcAddress (
1167                                                                                              This->NetworkLibraryHandle,
1168                                                                                              NETWORK_LIBRARY_SET_RCV_FILTER
1169                                                                                              );
1170 
1171   if (NULL == This->NtNetUtilityTable.SetReceiveFilter) {
1172     Status = EFI_NOT_FOUND;
1173     goto ErrorReturn;
1174   }
1175 
1176   This->NtNetUtilityTable.Receive = (NT_NET_RECEIVE) This->WinNtThunk->GetProcAddress (
1177                                                                          This->NetworkLibraryHandle,
1178                                                                          NETWORK_LIBRARY_RECEIVE
1179                                                                          );
1180 
1181   if (NULL == This->NtNetUtilityTable.Receive) {
1182     Status = EFI_NOT_FOUND;
1183     goto ErrorReturn;
1184   }
1185 
1186   This->NtNetUtilityTable.Transmit = (NT_NET_TRANSMIT) This->WinNtThunk->GetProcAddress (
1187                                                                            This->NetworkLibraryHandle,
1188                                                                            NETWORK_LIBRARY_TRANSMIT
1189                                                                            );
1190 
1191   if (NULL == This->NtNetUtilityTable.Transmit) {
1192     Status = EFI_NOT_FOUND;
1193     goto ErrorReturn;
1194   }
1195   //
1196   //  Initialize the network utility library
1197   //  And enumerate the interfaces in NT32 host
1198   //
1199   ReturnValue = This->NtNetUtilityTable.Initialize (&InterfaceCount, &NetInterfaceInfoBuffer[0]);
1200   if (ReturnValue <= 0) {
1201     Status = EFI_DEVICE_ERROR;
1202     goto ErrorReturn;
1203   }
1204 
1205   NetUtilityLibInitDone = TRUE;
1206 
1207   if (InterfaceCount == 0) {
1208     Status = EFI_NOT_FOUND;
1209     goto ErrorReturn;
1210   }
1211   //
1212   //  Create fake SNP instances
1213   //
1214   for (Index = 0; Index < InterfaceCount; Index++) {
1215 
1216     Instance = AllocatePool (sizeof (SNPNT32_INSTANCE_DATA));
1217 
1218     if (NULL == Instance) {
1219       Status = EFI_OUT_OF_RESOURCES;
1220       goto ErrorReturn;
1221     }
1222     //
1223     //  Copy the content from a template
1224     //
1225     CopyMem (Instance, &gSnpNt32InstanceTemplate, sizeof (SNPNT32_INSTANCE_DATA));
1226 
1227     //
1228     //  Set the interface information.
1229     //
1230     CopyMem (&Instance->InterfaceInfo, &NetInterfaceInfoBuffer[Index], sizeof(Instance->InterfaceInfo));
1231     //
1232     //  Initialize this instance
1233     //
1234     Status = This->InitializeInstanceData (This, Instance);
1235     if (EFI_ERROR (Status)) {
1236 
1237       gBS->FreePool (Instance);
1238       goto ErrorReturn;
1239     }
1240     //
1241     //  Insert this instance into the instance list
1242     //
1243     InsertTailList (&This->InstanceList, &Instance->Entry);
1244   }
1245 
1246   return EFI_SUCCESS;
1247 
1248 ErrorReturn:
1249 
1250   while (!IsListEmpty (&This->InstanceList)) {
1251 
1252     Entry     = This->InstanceList.ForwardLink;
1253 
1254     Instance  = NET_LIST_USER_STRUCT_S (Entry, SNPNT32_INSTANCE_DATA, Entry, SNP_NT32_INSTANCE_SIGNATURE);
1255 
1256     RemoveEntryList (Entry);
1257 
1258     This->CloseInstance (This, Instance);
1259     gBS->FreePool (Instance);
1260   }
1261 
1262   if (NetUtilityLibInitDone) {
1263 
1264     ASSERT (This->WinNtThunk != NULL);
1265 
1266     if (This->NtNetUtilityTable.Finalize != NULL) {
1267       This->NtNetUtilityTable.Finalize ();
1268       This->NtNetUtilityTable.Finalize = NULL;
1269     }
1270   }
1271 
1272   return Status;
1273 }
1274 
1275 
1276 /**
1277   Initialize the snpnt32 driver instance.
1278 
1279   @param  This                  Pointer to the SnpNt32 global data.
1280   @param  Instance              Pointer to the instance context data.
1281 
1282   @retval EFI_SUCCESS           The driver instance is initialized.
1283   @retval other                 Initialization errors.
1284 
1285 **/
1286 EFI_STATUS
SnpNt32InitializeInstanceData(IN SNPNT32_GLOBAL_DATA * This,IN OUT SNPNT32_INSTANCE_DATA * Instance)1287 SnpNt32InitializeInstanceData (
1288   IN SNPNT32_GLOBAL_DATA        *This,
1289   IN OUT SNPNT32_INSTANCE_DATA  *Instance
1290   )
1291 {
1292   EFI_STATUS    Status;
1293   EFI_DEV_PATH  EndNode;
1294   EFI_DEV_PATH  Node;
1295 
1296   Instance->GlobalData  = This;
1297   Instance->Snp.Mode    = &Instance->Mode;
1298   //
1299   //  Set broadcast address
1300   //
1301   SetMem (&Instance->Mode.BroadcastAddress, sizeof (EFI_MAC_ADDRESS), 0xFF);
1302 
1303   //
1304   //  Copy Current/PermanentAddress MAC address
1305   //
1306   CopyMem (&Instance->Mode.CurrentAddress, &Instance->InterfaceInfo.MacAddr, sizeof(Instance->Mode.CurrentAddress));
1307   CopyMem (&Instance->Mode.PermanentAddress, &Instance->InterfaceInfo.MacAddr, sizeof(Instance->Mode.PermanentAddress));
1308 
1309   //
1310   //  Since the fake SNP is based on a real NIC, to avoid conflict with the host
1311   //  NIC network stack, we use a different MAC address.
1312   //  So just change the last byte of the MAC address for the real NIC.
1313   //
1314   Instance->Mode.CurrentAddress.Addr[NET_ETHER_ADDR_LEN - 1]++;
1315 
1316   //
1317   //  Create a fake device path for the instance
1318   //
1319   ZeroMem (&Node, sizeof (Node));
1320 
1321   Node.DevPath.Type     = MESSAGING_DEVICE_PATH;
1322   Node.DevPath.SubType  = MSG_MAC_ADDR_DP;
1323   SetDevicePathNodeLength (&Node.DevPath, sizeof (MAC_ADDR_DEVICE_PATH));
1324 
1325   CopyMem (
1326     &Node.MacAddr.MacAddress,
1327     &Instance->Mode.CurrentAddress,
1328     NET_ETHER_ADDR_LEN
1329     );
1330 
1331   Node.MacAddr.IfType = Instance->Mode.IfType;
1332 
1333   SetDevicePathEndNode (&EndNode.DevPath);
1334 
1335   Instance->DevicePath = AppendDevicePathNode (
1336                            &EndNode.DevPath,
1337                            &Node.DevPath
1338                            );
1339 
1340   //
1341   //  Create a fake device handle for the fake SNP
1342   //
1343   Status = gBS->InstallMultipleProtocolInterfaces (
1344                   &Instance->DeviceHandle,
1345                   &gEfiSimpleNetworkProtocolGuid,
1346                   &Instance->Snp,
1347                   &gEfiDevicePathProtocolGuid,
1348                   Instance->DevicePath,
1349                   NULL
1350                   );
1351   return Status;
1352 }
1353 
1354 
1355 /**
1356   Close the SnpNt32 driver instance.
1357 
1358   @param  This                  Pointer to the SnpNt32 global data.
1359   @param  Instance              Pointer to the instance context data.
1360 
1361   @retval EFI_SUCCESS           The instance is closed.
1362 
1363 **/
1364 EFI_STATUS
SnpNt32CloseInstance(IN SNPNT32_GLOBAL_DATA * This,IN OUT SNPNT32_INSTANCE_DATA * Instance)1365 SnpNt32CloseInstance (
1366   IN SNPNT32_GLOBAL_DATA        *This,
1367   IN OUT SNPNT32_INSTANCE_DATA  *Instance
1368   )
1369 {
1370   ASSERT (This != NULL);
1371   ASSERT (Instance != NULL);
1372 
1373   gBS->UninstallMultipleProtocolInterfaces (
1374          Instance->DeviceHandle,
1375          &gEfiSimpleNetworkProtocolGuid,
1376          &Instance->Snp,
1377          &gEfiDevicePathProtocolGuid,
1378          Instance->DevicePath,
1379          NULL
1380          );
1381 
1382   if (Instance->DevicePath != NULL) {
1383     gBS->FreePool (Instance->DevicePath);
1384   }
1385 
1386   return EFI_SUCCESS;
1387 }
1388 
1389 /**
1390   Unloads an image.
1391 
1392   @param  ImageHandle           Handle that identifies the image to be unloaded.
1393 
1394   @retval EFI_SUCCESS           The image has been unloaded.
1395   @return Exit code from the image's unload handler
1396 
1397 **/
1398 EFI_STATUS
1399 EFIAPI
SnpNt32Unload(IN EFI_HANDLE ImageHandle)1400 SnpNt32Unload (
1401   IN EFI_HANDLE  ImageHandle
1402   )
1403 {
1404   EFI_STATUS            Status;
1405   SNPNT32_GLOBAL_DATA   *This;
1406   LIST_ENTRY            *Entry;
1407   SNPNT32_INSTANCE_DATA *Instance;
1408 
1409   This    = &gSnpNt32GlobalData;
1410 
1411   Status  = NetLibDefaultUnload (ImageHandle);
1412 
1413   if (EFI_ERROR (Status)) {
1414     return Status;
1415   }
1416 
1417   while (!IsListEmpty (&This->InstanceList)) {
1418     //
1419     //  Walkthrough the interfaces and remove all the SNP instance
1420     //
1421     Entry     = This->InstanceList.ForwardLink;
1422 
1423     Instance  = NET_LIST_USER_STRUCT_S (Entry, SNPNT32_INSTANCE_DATA, Entry, SNP_NT32_INSTANCE_SIGNATURE);
1424 
1425     RemoveEntryList (Entry);
1426 
1427     This->CloseInstance (This, Instance);
1428     gBS->FreePool (Instance);
1429   }
1430 
1431   if (This->NtNetUtilityTable.Finalize != NULL) {
1432     This->NtNetUtilityTable.Finalize ();
1433   }
1434 
1435   This->WinNtThunk->FreeLibrary (This->NetworkLibraryHandle);
1436 
1437   return EFI_SUCCESS;
1438 }
1439 
1440 /**
1441   This is the declaration of an EFI image entry point. This entry point is
1442   the same for UEFI Applications, UEFI OS Loaders, and UEFI Drivers including
1443   both device drivers and bus drivers.
1444 
1445   @param  ImageHandle           The firmware allocated handle for the UEFI image.
1446   @param  SystemTable           A pointer to the EFI System Table.
1447 
1448   @retval EFI_SUCCESS           The operation completed successfully.
1449   @retval EFI_OUT_OF_RESOURCES  The request could not be completed due to a lack of resources.
1450 
1451 **/
1452 EFI_STATUS
InitializeSnpNt32Driver(IN EFI_HANDLE ImageHandle,IN EFI_SYSTEM_TABLE * SystemTable)1453 InitializeSnpNt32Driver (
1454   IN EFI_HANDLE        ImageHandle,
1455   IN EFI_SYSTEM_TABLE  *SystemTable
1456   )
1457 {
1458 
1459   EFI_STATUS  Status;
1460 
1461   //
1462   // Install the Driver Protocols
1463   //
1464 
1465   Status = EfiLibInstallDriverBindingComponentName2 (
1466              ImageHandle,
1467              SystemTable,
1468              &gSnpNt32DriverBinding,
1469              ImageHandle,
1470              &gSnpNt32DriverComponentName,
1471              &gSnpNt32DriverComponentName2
1472              );
1473   if (EFI_ERROR (Status)) {
1474     return Status;
1475   }
1476 
1477   //
1478   //  Initialize the global data
1479   //
1480   Status = SnpNt32InitializeGlobalData (&gSnpNt32GlobalData);
1481   if (EFI_ERROR (Status)) {
1482     SnpNt32Unload (ImageHandle);
1483   }
1484 
1485   return Status;
1486 }
1487