xref: /DragonStub/inc/ia32/efibind.h (revision dcced03469f3ef51b581a7cfd54884e3198bbc33)
1530d68baSNigel Croxon /*++
2530d68baSNigel Croxon 
3530d68baSNigel Croxon Copyright (c) 1998  Intel Corporation
4530d68baSNigel Croxon 
5530d68baSNigel Croxon Module Name:
6530d68baSNigel Croxon 
7530d68baSNigel Croxon     efefind.h
8530d68baSNigel Croxon 
9530d68baSNigel Croxon Abstract:
10530d68baSNigel Croxon 
11530d68baSNigel Croxon     EFI to compile bindings
12530d68baSNigel Croxon 
13530d68baSNigel Croxon 
14530d68baSNigel Croxon 
15530d68baSNigel Croxon 
16530d68baSNigel Croxon Revision History
17530d68baSNigel Croxon 
18530d68baSNigel Croxon --*/
19530d68baSNigel Croxon 
20530d68baSNigel Croxon #ifndef __GNUC__
21530d68baSNigel Croxon #pragma pack()
22530d68baSNigel Croxon #endif
23530d68baSNigel Croxon 
24530d68baSNigel Croxon //
25530d68baSNigel Croxon // Basic int types of various widths
26530d68baSNigel Croxon //
27530d68baSNigel Croxon 
286685cd00SAlexander von Gluck IV #if !defined(__STDC_VERSION__) || (__STDC_VERSION__ < 199901L ) && !defined(__cplusplus)
29530d68baSNigel Croxon 
30530d68baSNigel Croxon     // No ANSI C 1999/2000 stdint.h integer width declarations
31530d68baSNigel Croxon 
32530d68baSNigel Croxon     #if defined(_MSC_EXTENSIONS)
33530d68baSNigel Croxon 
34530d68baSNigel Croxon         // Use Microsoft C compiler integer width declarations
35530d68baSNigel Croxon 
36530d68baSNigel Croxon         typedef unsigned __int64    uint64_t;
37530d68baSNigel Croxon         typedef __int64             int64_t;
38530d68baSNigel Croxon         typedef unsigned __int32    uint32_t;
39530d68baSNigel Croxon         typedef __int32             int32_t;
40530d68baSNigel Croxon         typedef unsigned short      uint16_t;
41530d68baSNigel Croxon         typedef short               int16_t;
42530d68baSNigel Croxon         typedef unsigned char       uint8_t;
43530d68baSNigel Croxon         typedef char                int8_t;
44530d68baSNigel Croxon     #elif defined(__GNUC__)
45530d68baSNigel Croxon         typedef int __attribute__((__mode__(__DI__)))           int64_t;
46530d68baSNigel Croxon         typedef unsigned int __attribute__((__mode__(__DI__)))  uint64_t;
47530d68baSNigel Croxon         typedef unsigned int        uint32_t;
48530d68baSNigel Croxon         typedef int                 int32_t;
49530d68baSNigel Croxon         typedef unsigned short      uint16_t;
50530d68baSNigel Croxon         typedef short               int16_t;
51530d68baSNigel Croxon         typedef unsigned char       uint8_t;
52530d68baSNigel Croxon         typedef signed char         int8_t;
53530d68baSNigel Croxon     #elif defined(UNIX_LP64)
54530d68baSNigel Croxon 
55530d68baSNigel Croxon         /*  Use LP64 programming model from C_FLAGS for integer width declarations */
56530d68baSNigel Croxon 
57530d68baSNigel Croxon        typedef unsigned long       uint64_t;
58530d68baSNigel Croxon        typedef long                int64_t;
59530d68baSNigel Croxon        typedef unsigned int        uint32_t;
60530d68baSNigel Croxon        typedef int                 int32_t;
61530d68baSNigel Croxon        typedef unsigned short      uint16_t;
62530d68baSNigel Croxon        typedef short               int16_t;
63530d68baSNigel Croxon        typedef unsigned char       uint8_t;
64530d68baSNigel Croxon        typedef char                int8_t;
65530d68baSNigel Croxon     #else
66530d68baSNigel Croxon 
67530d68baSNigel Croxon        /*  Assume P64 programming model from C_FLAGS for integer width declarations */
68530d68baSNigel Croxon 
69530d68baSNigel Croxon        typedef unsigned long long  uint64_t __attribute__((aligned (8)));
70530d68baSNigel Croxon        typedef long long           int64_t __attribute__((aligned (8)));
71530d68baSNigel Croxon        typedef unsigned int        uint32_t;
72530d68baSNigel Croxon        typedef int                 int32_t;
73530d68baSNigel Croxon        typedef unsigned short      uint16_t;
74530d68baSNigel Croxon        typedef short               int16_t;
75530d68baSNigel Croxon        typedef unsigned char       uint8_t;
76530d68baSNigel Croxon        typedef char                int8_t;
77530d68baSNigel Croxon     #endif
781a53d8f8SEsben Haabendal     typedef uint32_t            uintptr_t;
791a53d8f8SEsben Haabendal     typedef int32_t             intptr_t;
80530d68baSNigel Croxon #elif defined(__GNUC__)
81530d68baSNigel Croxon     #include <stdint.h>
82530d68baSNigel Croxon #endif
83530d68baSNigel Croxon 
84530d68baSNigel Croxon //
85530d68baSNigel Croxon // Basic EFI types of various widths
86530d68baSNigel Croxon //
87530d68baSNigel Croxon 
88189200d0SCallum Farmer #include <stddef.h>
89189200d0SCallum Farmer 
90189200d0SCallum Farmer typedef wchar_t CHAR16;
91189200d0SCallum Farmer #define WCHAR CHAR16
92189200d0SCallum Farmer 
93530d68baSNigel Croxon 
94530d68baSNigel Croxon typedef uint64_t   UINT64;
95530d68baSNigel Croxon typedef int64_t    INT64;
96530d68baSNigel Croxon 
97530d68baSNigel Croxon #ifndef _BASETSD_H_
98530d68baSNigel Croxon     typedef uint32_t   UINT32;
99530d68baSNigel Croxon     typedef int32_t    INT32;
100530d68baSNigel Croxon #endif
101530d68baSNigel Croxon 
102530d68baSNigel Croxon typedef uint16_t   UINT16;
103530d68baSNigel Croxon typedef int16_t    INT16;
10414899d89SPeter Jones 
105530d68baSNigel Croxon typedef uint8_t    UINT8;
10614899d89SPeter Jones typedef char       CHAR8;
107530d68baSNigel Croxon typedef int8_t     INT8;
10814899d89SPeter Jones 
109530d68baSNigel Croxon #undef VOID
110*056bdaa5SCallum Farmer typedef void       VOID;
111530d68baSNigel Croxon 
112530d68baSNigel Croxon 
113530d68baSNigel Croxon typedef int32_t    INTN;
114530d68baSNigel Croxon typedef uint32_t   UINTN;
115530d68baSNigel Croxon 
116530d68baSNigel Croxon #ifdef EFI_NT_EMULATOR
117530d68baSNigel Croxon     #define POST_CODE(_Data)
118530d68baSNigel Croxon #else
119530d68baSNigel Croxon     #ifdef EFI_DEBUG
120530d68baSNigel Croxon #define POST_CODE(_Data)    __asm mov eax,(_Data) __asm out 0x80,al
121530d68baSNigel Croxon     #else
122530d68baSNigel Croxon         #define POST_CODE(_Data)
123530d68baSNigel Croxon     #endif
124530d68baSNigel Croxon #endif
125530d68baSNigel Croxon 
126530d68baSNigel Croxon #define EFIERR(a)           (0x80000000 | a)
127530d68baSNigel Croxon #define EFI_ERROR_MASK      0x80000000
128530d68baSNigel Croxon #define EFIERR_OEM(a)       (0xc0000000 | a)
129530d68baSNigel Croxon 
130530d68baSNigel Croxon 
131530d68baSNigel Croxon #define BAD_POINTER         0xFBFBFBFB
132530d68baSNigel Croxon #define MAX_ADDRESS         0xFFFFFFFF
133530d68baSNigel Croxon 
134530d68baSNigel Croxon #ifdef EFI_NT_EMULATOR
135530d68baSNigel Croxon     #define BREAKPOINT()        __asm { int 3 }
136530d68baSNigel Croxon #else
137530d68baSNigel Croxon     #define BREAKPOINT()        while (TRUE);    // Make it hang on Bios[Dbg]32
138530d68baSNigel Croxon #endif
139530d68baSNigel Croxon 
140530d68baSNigel Croxon //
141530d68baSNigel Croxon // Pointers must be aligned to these address to function
142530d68baSNigel Croxon //
143530d68baSNigel Croxon 
144530d68baSNigel Croxon #define MIN_ALIGNMENT_SIZE  4
145530d68baSNigel Croxon 
146530d68baSNigel Croxon #define ALIGN_VARIABLE(Value ,Adjustment) \
147530d68baSNigel Croxon             (UINTN)Adjustment = 0; \
148530d68baSNigel Croxon             if((UINTN)Value % MIN_ALIGNMENT_SIZE) \
149530d68baSNigel Croxon                 (UINTN)Adjustment = MIN_ALIGNMENT_SIZE - ((UINTN)Value % MIN_ALIGNMENT_SIZE); \
150530d68baSNigel Croxon             Value = (UINTN)Value + (UINTN)Adjustment
151530d68baSNigel Croxon 
152530d68baSNigel Croxon 
153530d68baSNigel Croxon //
154530d68baSNigel Croxon // Define macros to build data structure signatures from characters.
155530d68baSNigel Croxon //
156530d68baSNigel Croxon 
157530d68baSNigel Croxon #define EFI_SIGNATURE_16(A,B)             ((A) | (B<<8))
158530d68baSNigel Croxon #define EFI_SIGNATURE_32(A,B,C,D)         (EFI_SIGNATURE_16(A,B)     | (EFI_SIGNATURE_16(C,D)     << 16))
159530d68baSNigel Croxon #define EFI_SIGNATURE_64(A,B,C,D,E,F,G,H) (EFI_SIGNATURE_32(A,B,C,D) | ((UINT64)(EFI_SIGNATURE_32(E,F,G,H)) << 32))
160530d68baSNigel Croxon //
161530d68baSNigel Croxon // To export & import functions in the EFI emulator environment
162530d68baSNigel Croxon //
163530d68baSNigel Croxon 
164530d68baSNigel Croxon #ifdef EFI_NT_EMULATOR
165530d68baSNigel Croxon     #define EXPORTAPI           __declspec( dllexport )
166530d68baSNigel Croxon #else
167530d68baSNigel Croxon     #define EXPORTAPI
168530d68baSNigel Croxon #endif
169530d68baSNigel Croxon 
170530d68baSNigel Croxon 
171530d68baSNigel Croxon //
172530d68baSNigel Croxon // EFIAPI - prototype calling convention for EFI function pointers
173530d68baSNigel Croxon // BOOTSERVICE - prototype for implementation of a boot service interface
174530d68baSNigel Croxon // RUNTIMESERVICE - prototype for implementation of a runtime service interface
175530d68baSNigel Croxon // RUNTIMEFUNCTION - prototype for implementation of a runtime function that is not a service
176530d68baSNigel Croxon // RUNTIME_CODE - pragma macro for declaring runtime code
177530d68baSNigel Croxon //
178530d68baSNigel Croxon 
179530d68baSNigel Croxon #ifndef EFIAPI                  // Forces EFI calling conventions reguardless of compiler options
180530d68baSNigel Croxon     #ifdef _MSC_EXTENSIONS
181530d68baSNigel Croxon         #define EFIAPI __cdecl  // Force C calling convention for Microsoft C compiler
182530d68baSNigel Croxon     #else
183530d68baSNigel Croxon         #define EFIAPI          // Substitute expresion to force C calling convention
184530d68baSNigel Croxon     #endif
185530d68baSNigel Croxon #endif
186530d68baSNigel Croxon 
187530d68baSNigel Croxon #define BOOTSERVICE
188530d68baSNigel Croxon //#define RUNTIMESERVICE(proto,a)    alloc_text("rtcode",a); proto a
189530d68baSNigel Croxon //#define RUNTIMEFUNCTION(proto,a)   alloc_text("rtcode",a); proto a
190530d68baSNigel Croxon #define RUNTIMESERVICE
191530d68baSNigel Croxon #define RUNTIMEFUNCTION
192530d68baSNigel Croxon 
193530d68baSNigel Croxon 
194530d68baSNigel Croxon #define RUNTIME_CODE(a)         alloc_text("rtcode", a)
195530d68baSNigel Croxon #define BEGIN_RUNTIME_DATA()    data_seg("rtdata")
196530d68baSNigel Croxon #define END_RUNTIME_DATA()      data_seg("")
197530d68baSNigel Croxon 
198530d68baSNigel Croxon #define VOLATILE    volatile
199530d68baSNigel Croxon 
200530d68baSNigel Croxon #define MEMORY_FENCE()
201530d68baSNigel Croxon 
202530d68baSNigel Croxon #ifdef EFI_NT_EMULATOR
203530d68baSNigel Croxon 
204530d68baSNigel Croxon //
205530d68baSNigel Croxon // To help ensure proper coding of integrated drivers, they are
206530d68baSNigel Croxon // compiled as DLLs.  In NT they require a dll init entry pointer.
207530d68baSNigel Croxon // The macro puts a stub entry point into the DLL so it will load.
208530d68baSNigel Croxon //
209530d68baSNigel Croxon 
210530d68baSNigel Croxon #define EFI_DRIVER_ENTRY_POINT(InitFunction)    \
211530d68baSNigel Croxon     UINTN                                       \
212530d68baSNigel Croxon     __stdcall                                   \
213530d68baSNigel Croxon     _DllMainCRTStartup (                        \
214530d68baSNigel Croxon         UINTN    Inst,                          \
215530d68baSNigel Croxon         UINTN    reason_for_call,               \
216530d68baSNigel Croxon         VOID    *rserved                        \
217530d68baSNigel Croxon         )                                       \
218530d68baSNigel Croxon     {                                           \
219530d68baSNigel Croxon         return 1;                               \
220530d68baSNigel Croxon     }                                           \
221530d68baSNigel Croxon                                                 \
222530d68baSNigel Croxon     int                                         \
223530d68baSNigel Croxon     EXPORTAPI                                   \
224530d68baSNigel Croxon     __cdecl                                     \
225530d68baSNigel Croxon     InitializeDriver (                          \
226530d68baSNigel Croxon         void *ImageHandle,                      \
227530d68baSNigel Croxon         void *SystemTable                       \
228530d68baSNigel Croxon         )                                       \
229530d68baSNigel Croxon     {                                           \
230530d68baSNigel Croxon         return InitFunction(ImageHandle, SystemTable);       \
231530d68baSNigel Croxon     }
232530d68baSNigel Croxon 
233530d68baSNigel Croxon 
234530d68baSNigel Croxon     #define LOAD_INTERNAL_DRIVER(_if, type, name, entry)      \
235530d68baSNigel Croxon         (_if)->LoadInternal(type, name, NULL)
236530d68baSNigel Croxon 
237530d68baSNigel Croxon #else // EFI_NT_EMULATOR
238530d68baSNigel Croxon 
239530d68baSNigel Croxon //
240530d68baSNigel Croxon // When build similiar to FW, then link everything together as
24109027207SNigel Croxon // one big module. For the MSVC toolchain, we simply tell the
24209027207SNigel Croxon // linker what our driver init function is using /ENTRY.
243530d68baSNigel Croxon //
24409027207SNigel Croxon #if defined(_MSC_EXTENSIONS)
24509027207SNigel Croxon     #define EFI_DRIVER_ENTRY_POINT(InitFunction) \
24609027207SNigel Croxon         __pragma(comment(linker, "/ENTRY:" # InitFunction))
24709027207SNigel Croxon #else
248530d68baSNigel Croxon     #define EFI_DRIVER_ENTRY_POINT(InitFunction)    \
249530d68baSNigel Croxon         UINTN                                       \
250530d68baSNigel Croxon         InitializeDriver (                          \
251530d68baSNigel Croxon             VOID    *ImageHandle,                   \
252530d68baSNigel Croxon             VOID    *SystemTable                    \
253530d68baSNigel Croxon             )                                       \
254530d68baSNigel Croxon         {                                           \
255530d68baSNigel Croxon             return InitFunction(ImageHandle,        \
256530d68baSNigel Croxon                     SystemTable);                   \
257530d68baSNigel Croxon         }                                           \
258530d68baSNigel Croxon                                                     \
259530d68baSNigel Croxon         EFI_STATUS efi_main(                        \
260530d68baSNigel Croxon             EFI_HANDLE image,                       \
261530d68baSNigel Croxon             EFI_SYSTEM_TABLE *systab                \
262530d68baSNigel Croxon             ) __attribute__((weak,                  \
263530d68baSNigel Croxon                     alias ("InitializeDriver")));
26409027207SNigel Croxon #endif
265530d68baSNigel Croxon 
266530d68baSNigel Croxon     #define LOAD_INTERNAL_DRIVER(_if, type, name, entry)    \
267530d68baSNigel Croxon             (_if)->LoadInternal(type, name, entry)
268530d68baSNigel Croxon 
269530d68baSNigel Croxon #endif // EFI_FW_NT
270530d68baSNigel Croxon 
271530d68baSNigel Croxon //
272530d68baSNigel Croxon // Some compilers don't support the forward reference construct:
273530d68baSNigel Croxon //  typedef struct XXXXX
274530d68baSNigel Croxon //
275530d68baSNigel Croxon // The following macro provide a workaround for such cases.
276530d68baSNigel Croxon //
277530d68baSNigel Croxon #ifdef NO_INTERFACE_DECL
278530d68baSNigel Croxon #define INTERFACE_DECL(x)
279530d68baSNigel Croxon #else
2800b5f181aSNigel Croxon #if defined(__GNUC__) || defined(_MSC_EXTENSIONS)
281530d68baSNigel Croxon #define INTERFACE_DECL(x) struct x
282530d68baSNigel Croxon #else
283530d68baSNigel Croxon #define INTERFACE_DECL(x) typedef struct x
284530d68baSNigel Croxon #endif
285530d68baSNigel Croxon #endif
286530d68baSNigel Croxon 
287530d68baSNigel Croxon /* No efi call wrapper for IA32 architecture */
288530d68baSNigel Croxon #define uefi_call_wrapper(func, va_num, ...)	func(__VA_ARGS__)
289530d68baSNigel Croxon #define EFI_FUNCTION
290530d68baSNigel Croxon 
291530d68baSNigel Croxon #ifdef _MSC_EXTENSIONS
292530d68baSNigel Croxon #pragma warning ( disable : 4731 )  // Suppress warnings about modification of EBP
293530d68baSNigel Croxon #endif
294530d68baSNigel Croxon 
295