1 /*++ @file
2 
3 Copyright (c) 2006 - 2008, Intel Corporation. All rights reserved.<BR>
4 Portions copyright (c) 2011, Apple Inc. All rights reserved.
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution.  The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9 
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12 
13 **/
14 
15 #include <PiDxe.h>
16 
17 #include <Library/DebugLib.h>
18 #include <Library/HobLib.h>
19 #include <Library/EmuThunkLib.h>
20 #include <Library/BaseMemoryLib.h>
21 
22 EMU_THUNK_PROTOCOL   *gEmuThunk = NULL;
23 
24 
25 /**
26   The constructor function caches the pointer of EMU Thunk protocol.
27 
28   @param  ImageHandle   The firmware allocated handle for the EFI image.
29   @param  SystemTable   A pointer to the EFI System Table.
30 
31   @retval EFI_SUCCESS   The constructor always returns EFI_SUCCESS.
32 
33 **/
34 EFI_STATUS
35 EFIAPI
DxeEmuLibConstructor(IN EFI_HANDLE ImageHandle,IN EFI_SYSTEM_TABLE * SystemTable)36 DxeEmuLibConstructor (
37   IN EFI_HANDLE        ImageHandle,
38   IN EFI_SYSTEM_TABLE  *SystemTable
39   )
40 {
41   EFI_HOB_GUID_TYPE        *GuidHob;
42 
43   GuidHob = GetFirstGuidHob (&gEmuThunkProtocolGuid);
44   ASSERT (GuidHob != NULL);
45 
46   gEmuThunk = (EMU_THUNK_PROTOCOL *)(*(UINTN *)(GET_GUID_HOB_DATA (GuidHob)));
47   ASSERT (gEmuThunk != NULL);
48 
49   return EFI_SUCCESS;
50 }
51 
52 
53 /**
54   Serach the EMU IO Thunk database for a matching EMU IO Thunk
55   Protocol instance.
56 
57   @param  Protocol   Protocol to search for.
58   @param  Instance   Instance of protocol to search for.
59 
60   @retval NULL       Protocol and Instance not found.
61   @retval other      EMU IO Thunk protocol that matched.
62 
63 **/
64 EMU_IO_THUNK_PROTOCOL *
65 EFIAPI
GetIoThunkInstance(IN EFI_GUID * Protocol,IN UINTN Instance)66 GetIoThunkInstance (
67   IN  EFI_GUID  *Protocol,
68   IN  UINTN     Instance
69   )
70 {
71   EFI_STATUS              Status;
72   EMU_IO_THUNK_PROTOCOL   *EmuIoThunk;
73 
74   for (Status = EFI_SUCCESS, EmuIoThunk = NULL; !EFI_ERROR (Status); ) {
75     Status = gEmuThunk->GetNextProtocol (FALSE, &EmuIoThunk);
76     if (EFI_ERROR (Status)) {
77       break;
78     }
79 
80     if (EmuIoThunk->Instance == Instance) {
81       if (CompareGuid (EmuIoThunk->Protocol, Protocol)) {
82         return EmuIoThunk;
83       }
84     }
85   }
86 
87   return NULL;
88 }