1/*	$NetBSD: efibind.h,v 1.5 2021/09/30 19:02:47 jmcneill Exp $	*/
2
3/*++
4
5Copyright (c) 1998  Intel Corporation
6
7Module Name:
8
9    efefind.h
10
11Abstract:
12
13    EFI to compile bindings
14
15
16
17
18Revision History
19
20--*/
21
22#ifndef __GNUC__
23#pragma pack()
24#endif
25
26//
27// Basic int types of various widths
28//
29
30#if !defined(__STDC_VERSION__) || (__STDC_VERSION__ < 199901L ) && !defined(__cplusplus)
31
32    // No ANSI C 1999/2000 stdint.h integer width declarations
33
34    #if defined(_MSC_EXTENSIONS)
35
36        // Use Microsoft C compiler integer width declarations
37
38        typedef unsigned __int64    uint64_t;
39        typedef __int64             int64_t;
40        typedef unsigned __int32    uint32_t;
41        typedef __int32             int32_t;
42        typedef unsigned short      uint16_t;
43        typedef short               int16_t;
44        typedef unsigned char       uint8_t;
45        typedef char                int8_t;
46    #elif defined(__GNUC__)
47        typedef int __attribute__((__mode__(__DI__)))           int64_t;
48        typedef unsigned int __attribute__((__mode__(__DI__)))  uint64_t;
49        typedef unsigned int        uint32_t;
50        typedef int                 int32_t;
51        typedef unsigned short      uint16_t;
52        typedef short               int16_t;
53        typedef unsigned char       uint8_t;
54        typedef signed char         int8_t;
55    #elif defined(UNIX_LP64)
56
57        /*  Use LP64 programming model from C_FLAGS for integer width declarations */
58
59       typedef unsigned long       uint64_t;
60       typedef long                int64_t;
61       typedef unsigned int        uint32_t;
62       typedef int                 int32_t;
63       typedef unsigned short      uint16_t;
64       typedef short               int16_t;
65       typedef unsigned char       uint8_t;
66       typedef char                int8_t;
67    #else
68
69       /*  Assume P64 programming model from C_FLAGS for integer width declarations */
70
71       typedef unsigned long long  uint64_t __attribute__((aligned (8)));
72       typedef long long           int64_t __attribute__((aligned (8)));
73       typedef unsigned int        uint32_t;
74       typedef int                 int32_t;
75       typedef unsigned short      uint16_t;
76       typedef short               int16_t;
77       typedef unsigned char       uint8_t;
78       typedef char                int8_t;
79    #endif
80    typedef uint32_t            uintptr_t;
81    typedef int32_t             intptr_t;
82#elif defined(__NetBSD__)
83    #include <sys/stdint.h>
84#elif defined(__GNUC__)
85    #include <stdint.h>
86#endif
87
88//
89// Basic EFI types of various widths
90//
91
92#ifndef __WCHAR_TYPE__
93# define __WCHAR_TYPE__ short
94#endif
95
96#ifndef __ACTYPES_H__
97typedef uint64_t   UINT64;
98typedef int64_t    INT64;
99
100#ifndef _BASETSD_H_
101    typedef uint32_t   UINT32;
102    typedef int32_t    INT32;
103#endif
104
105typedef uint16_t   UINT16;
106typedef int16_t    INT16;
107typedef uint8_t    UINT8;
108typedef int8_t     INT8;
109#endif /* __ACTYPES_H__ */
110typedef __WCHAR_TYPE__ WCHAR;
111
112#undef VOID
113#define VOID    void
114
115
116typedef int32_t    INTN;
117typedef uint32_t   UINTN;
118
119#ifdef EFI_NT_EMULATOR
120    #define POST_CODE(_Data)
121#else
122    #ifdef EFI_DEBUG
123#define POST_CODE(_Data)    __asm mov eax,(_Data) __asm out 0x80,al
124    #else
125        #define POST_CODE(_Data)
126    #endif
127#endif
128
129#define EFIERR(a)           (0x80000000 | a)
130#define EFI_ERROR_MASK      0x80000000
131#define EFIERR_OEM(a)       (0xc0000000 | a)
132
133
134#define BAD_POINTER         0xFBFBFBFB
135#define MAX_ADDRESS         0xFFFFFFFF
136
137#ifdef EFI_NT_EMULATOR
138    #define BREAKPOINT()        __asm { int 3 }
139#else
140    #define BREAKPOINT()        while (TRUE);    // Make it hang on Bios[Dbg]32
141#endif
142
143//
144// Pointers must be aligned to these address to function
145//
146
147#define MIN_ALIGNMENT_SIZE  4
148
149#define ALIGN_VARIABLE(Value ,Adjustment) \
150            (UINTN)Adjustment = 0; \
151            if((UINTN)Value % MIN_ALIGNMENT_SIZE) \
152                (UINTN)Adjustment = MIN_ALIGNMENT_SIZE - ((UINTN)Value % MIN_ALIGNMENT_SIZE); \
153            Value = (UINTN)Value + (UINTN)Adjustment
154
155
156//
157// Define macros to build data structure signatures from characters.
158//
159
160#define EFI_SIGNATURE_16(A,B)             ((A) | (B<<8))
161#define EFI_SIGNATURE_32(A,B,C,D)         (EFI_SIGNATURE_16(A,B)     | (EFI_SIGNATURE_16(C,D)     << 16))
162#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))
163//
164// To export & import functions in the EFI emulator environment
165//
166
167#ifdef EFI_NT_EMULATOR
168    #define EXPORTAPI           __declspec( dllexport )
169#else
170    #define EXPORTAPI
171#endif
172
173
174//
175// EFIAPI - prototype calling convention for EFI function pointers
176// BOOTSERVICE - prototype for implementation of a boot service interface
177// RUNTIMESERVICE - prototype for implementation of a runtime service interface
178// RUNTIMEFUNCTION - prototype for implementation of a runtime function that is not a service
179// RUNTIME_CODE - pragma macro for declaring runtime code
180//
181
182#ifndef EFIAPI                  // Forces EFI calling conventions reguardless of compiler options
183    #ifdef _MSC_EXTENSIONS
184        #define EFIAPI __cdecl  // Force C calling convention for Microsoft C compiler
185    #else
186        #define EFIAPI          // Substitute expresion to force C calling convention
187    #endif
188#endif
189
190#define BOOTSERVICE
191//#define RUNTIMESERVICE(proto,a)    alloc_text("rtcode",a); proto a
192//#define RUNTIMEFUNCTION(proto,a)   alloc_text("rtcode",a); proto a
193#define RUNTIMESERVICE
194#define RUNTIMEFUNCTION
195
196
197#define RUNTIME_CODE(a)         alloc_text("rtcode", a)
198#define BEGIN_RUNTIME_DATA()    data_seg("rtdata")
199#define END_RUNTIME_DATA()      data_seg("")
200
201#define VOLATILE    volatile
202
203#define MEMORY_FENCE()
204
205#ifdef EFI_NT_EMULATOR
206
207//
208// To help ensure proper coding of integrated drivers, they are
209// compiled as DLLs.  In NT they require a dll init entry pointer.
210// The macro puts a stub entry point into the DLL so it will load.
211//
212
213#define EFI_DRIVER_ENTRY_POINT(InitFunction)    \
214    UINTN                                       \
215    __stdcall                                   \
216    _DllMainCRTStartup (                        \
217        UINTN    Inst,                          \
218        UINTN    reason_for_call,               \
219        VOID    *rserved                        \
220        )                                       \
221    {                                           \
222        return 1;                               \
223    }                                           \
224                                                \
225    int                                         \
226    EXPORTAPI                                   \
227    __cdecl                                     \
228    InitializeDriver (                          \
229        void *ImageHandle,                      \
230        void *SystemTable                       \
231        )                                       \
232    {                                           \
233        return InitFunction(ImageHandle, SystemTable);       \
234    }
235
236
237    #define LOAD_INTERNAL_DRIVER(_if, type, name, entry)      \
238        (_if)->LoadInternal(type, name, NULL)
239
240#else // EFI_NT_EMULATOR
241
242//
243// When build similiar to FW, then link everything together as
244// one big module. For the MSVC toolchain, we simply tell the
245// linker what our driver init function is using /ENTRY.
246//
247#if defined(_MSC_EXTENSIONS)
248    #define EFI_DRIVER_ENTRY_POINT(InitFunction) \
249        __pragma(comment(linker, "/ENTRY:" # InitFunction))
250#else
251    #define EFI_DRIVER_ENTRY_POINT(InitFunction)    \
252        UINTN                                       \
253        InitializeDriver (                          \
254            VOID    *ImageHandle,                   \
255            VOID    *SystemTable                    \
256            )                                       \
257        {                                           \
258            return InitFunction(ImageHandle,        \
259                    SystemTable);                   \
260        }                                           \
261                                                    \
262        EFI_STATUS efi_main(                        \
263            EFI_HANDLE image,                       \
264            EFI_SYSTEM_TABLE *systab                \
265            ) __attribute__((weak,                  \
266                    alias ("InitializeDriver")));
267#endif
268
269    #define LOAD_INTERNAL_DRIVER(_if, type, name, entry)    \
270            (_if)->LoadInternal(type, name, entry)
271
272#endif // EFI_FW_NT
273
274//
275// Some compilers don't support the forward reference construct:
276//  typedef struct XXXXX
277//
278// The following macro provide a workaround for such cases.
279//
280#ifdef NO_INTERFACE_DECL
281#define INTERFACE_DECL(x)
282#else
283#if defined(__GNUC__) || defined(_MSC_EXTENSIONS)
284#define INTERFACE_DECL(x) struct x
285#else
286#define INTERFACE_DECL(x) typedef struct x
287#endif
288#endif
289
290/* No efi call wrapper for IA32 architecture */
291#define uefi_call_wrapper(func, va_num, ...)	func(__VA_ARGS__)
292#define EFI_FUNCTION
293
294#ifdef _MSC_EXTENSIONS
295#pragma warning ( disable : 4731 )  // Suppress warnings about modification of EBP
296#endif
297
298