1 /** @file
2   Emulator Thunk to abstract OS services from pure EFI code
3 
4   Copyright (c) 2008 - 2011, Apple Inc. All rights reserved.<BR>
5 
6   SPDX-License-Identifier: BSD-2-Clause-Patent
7 
8 **/
9 
10 #ifndef __EMU_THUNK_PROTOCOL_H__
11 #define __EMU_THUNK_PROTOCOL_H__
12 
13 #define EMU_THUNK_PROTOCOL_GUID  \
14  { 0x5CF32E0B, 0x8EDF, 0x2E44, { 0x9C, 0xDA, 0x93, 0x20, 0x5E, 0x99, 0xEC, 0x1C } }
15 
16 // neded for things like EFI_TIME_CAPABILITIES
17 #include <Uefi.h>
18 
19 #include <Library/PeCoffExtraActionLib.h>
20 
21 #include <Protocol/EmuIoThunk.h>
22 #include <Protocol/DevicePath.h>
23 
24 
25 typedef struct {
26   VENDOR_DEVICE_PATH  VendorDevicePath;
27   UINT32              Instance;
28 } EMU_VENDOR_DEVICE_PATH_NODE;
29 
30 typedef struct {
31   EMU_VENDOR_DEVICE_PATH_NODE Vendor;
32   EFI_DEVICE_PATH_PROTOCOL    EndDevicePath;
33 } EMU_THUNK_DEVICE_PATH;
34 
35 
36 
37 typedef struct _EMU_THUNK_PROTOCOL  EMU_THUNK_PROTOCOL;
38 
39 
40 
41 typedef
42 UINTN
43 (EFIAPI *EMU_WRITE_STD_ERROR) (
44   IN UINT8     *Buffer,
45   IN UINTN     NumberOfBytes
46   );
47 
48 typedef
49 EFI_STATUS
50 (EFIAPI *EMU_CONFIG_STD_IN) (
51   VOID
52   );
53 
54 typedef
55 UINTN
56 (EFIAPI *EMU_WRITE_STD_OUT) (
57   IN UINT8     *Buffer,
58   IN UINTN     NumberOfBytes
59   );
60 
61 typedef
62 UINTN
63 (EFIAPI *EMU_READ_STD_IN) (
64   OUT UINT8     *Buffer,
65   IN  UINTN     NumberOfBytes
66   );
67 
68 typedef
69 BOOLEAN
70 (EFIAPI *EMU_POLL_STD_IN) (
71   VOID
72   );
73 
74 
75 typedef
76 VOID *
77 (EFIAPI *EMU_OS_MALLOC) (
78   IN  UINTN Size
79   );
80 
81 typedef
82 VOID *
83 (EFIAPI *EMU_OS_VMALLOC) (
84   IN  UINTN Size
85   );
86 
87 typedef
88 BOOLEAN
89 (EFIAPI *EMU_OS_FREE) (
90   IN  VOID *Ptr
91   );
92 
93 
94 typedef
95 EFI_STATUS
96 (EFIAPI *EMU_PE_COFF_GET_ENTRY_POINT) (
97   IN     VOID  *Pe32Data,
98   IN OUT VOID  **EntryPoint
99   );
100 
101 typedef
102 VOID
103 (EFIAPI *EMU_PE_COFF_RELOCATE_EXTRA_ACTION) (
104   IN OUT PE_COFF_LOADER_IMAGE_CONTEXT  *ImageContext
105   );
106 
107 typedef
108 VOID
109 (EFIAPI *EMU_PE_COFF_UNLOAD_EXTRA_ACTION) (
110   IN OUT PE_COFF_LOADER_IMAGE_CONTEXT  *ImageContext
111   );
112 
113 typedef
114 VOID
115 (EFIAPI *EMU_ENABLE_INERRUPTS) (
116   VOID
117   );
118 
119 typedef
120 VOID
121 (EFIAPI *EMU_DISABLE_INERRUPTS) (
122   VOID
123   );
124 
125 typedef
126 UINT64
127 (EFIAPI *EMU_QUERY_PERFORMANCE_FREQENCY) (
128   VOID
129   );
130 
131 typedef
132 UINT64
133 (EFIAPI *EMU_QUERY_PERFORMANCE_COUNTER) (
134   VOID
135   );
136 
137 typedef
138 VOID
139 (EFIAPI *EMU_SLEEP) (
140   IN  UINT64    Milliseconds
141   );
142 
143 typedef
144 VOID
145 (EFIAPI *EMU_CPU_SLEEP) (
146   VOID
147   );
148 
149 typedef
150 VOID
151 (EFIAPI *EMU_EXIT) (
152   IN  UINTN    Status
153   );
154 
155 typedef
156 VOID
157 (EFIAPI *EMU_GET_TIME) (
158   OUT  EFI_TIME               *Time,
159   OUT EFI_TIME_CAPABILITIES   *Capabilities OPTIONAL
160   );
161 
162 typedef
163 VOID
164 (EFIAPI *EMU_SET_TIME) (
165   IN   EFI_TIME               *Time
166   );
167 
168 
169 typedef
170 VOID
171 (EFIAPI EMU_SET_TIMER_CALLBACK) (
172   IN  UINT64  DeltaMs
173   );
174 
175 typedef
176 VOID
177 (EFIAPI *EMU_SET_TIMER) (
178   IN  UINT64                  PeriodMs,
179   IN  EMU_SET_TIMER_CALLBACK  CallBack
180   );
181 
182 
183 
184 /**
185   Enumerates the current set of protocol instances that abstract OS services from EFI.
186 
187   A given protocol can have multiple instances. Usually a protocol is configured via a
188   single PCD string. The data associated for each instance is seperated via a ! in the string.
189   EMU_IO_THUNK_PROTOCOL_CLOSE.ConfigString will contain the information in the PCD string up to the next !.
190   Thus each instance has a unique ConfigString.
191 
192   @param  EmuBusDriver          TRUE means only return protocol instances that need to be produced
193                                 by the EmuBusDriver. FALSE means return all possible protocols
194   @param  Instance              On input the protocol to search for, or NULL to start a search
195                                 of all the supported protocol instances.
196   @param  NextProtocol          On output it represents the next value to be passed into Protocol.
197   @param  Interface             A pointer to the EMU_IO_THUNK_PROTOCOL_CLOSE interface.
198 
199   @retval EFI_SUCCESS           The function completed successfully.
200   @retval EFI_NOT_FOUND         The next protocol instance was not found.
201   @retval EFI_INVALID_PARAMETER Instance is NULL.
202 
203 **/
204 typedef
205 EFI_STATUS
206 (EFIAPI *EMU_GET_NEXT_PROTOCOL) (
207   IN  BOOLEAN                 EmuBusDriver,
208   OUT EMU_IO_THUNK_PROTOCOL   **Instance  OPTIONAL
209   );
210 
211 
212 struct _EMU_THUNK_PROTOCOL {
213   // Used for early debug printing
214   EMU_WRITE_STD_ERROR               WriteStdErr;
215   EMU_CONFIG_STD_IN                 ConfigStdIn;
216   EMU_WRITE_STD_OUT                 WriteStdOut;
217   EMU_READ_STD_IN                   ReadStdIn;
218   EMU_POLL_STD_IN                   PollStdIn;
219 
220   //
221   // Map OS malloc/free so we can use OS based guard malloc
222   //
223   EMU_OS_MALLOC                     Malloc;
224   EMU_OS_VMALLOC                    Valloc;
225   EMU_OS_FREE                       Free;
226 
227 
228   ///
229   /// PE/COFF loader hooks to get symbols loaded
230   ///
231   EMU_PE_COFF_GET_ENTRY_POINT       PeCoffGetEntryPoint;
232   EMU_PE_COFF_RELOCATE_EXTRA_ACTION PeCoffRelocateImageExtraAction;
233   EMU_PE_COFF_UNLOAD_EXTRA_ACTION   PeCoffUnloadImageExtraAction;
234 
235   ///
236   /// DXE Architecture Protocol Services
237   ///
238   EMU_ENABLE_INERRUPTS              EnableInterrupt;
239   EMU_DISABLE_INERRUPTS             DisableInterrupt;
240   EMU_QUERY_PERFORMANCE_FREQENCY    QueryPerformanceFrequency;
241   EMU_QUERY_PERFORMANCE_COUNTER     QueryPerformanceCounter;
242 
243   EMU_SLEEP                         Sleep;
244   EMU_CPU_SLEEP                     CpuSleep;
245   EMU_EXIT                          Exit;
246   EMU_GET_TIME                      GetTime;
247   EMU_SET_TIME                      SetTime;
248   EMU_SET_TIMER                     SetTimer;
249 
250   ///
251   /// Generic System Services
252   ///
253   EMU_GET_NEXT_PROTOCOL             GetNextProtocol;
254 };
255 
256 extern EFI_GUID gEmuThunkProtocolGuid;
257 
258 #endif
259