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