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