efibind.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /*++
  2. Copyright (c) 1998 Intel Corporation
  3. Module Name:
  4. efefind.h
  5. Abstract:
  6. EFI to compile bindings
  7. Revision History
  8. --*/
  9. #ifndef X86_64_EFI_BIND
  10. #define X86_64_EFI_BIND
  11. #ifndef __GNUC__
  12. #pragma pack()
  13. #endif
  14. #if defined(_MSC_VER)
  15. #define HAVE_USE_MS_ABI 1
  16. #elif defined(GNU_EFI_USE_MS_ABI)
  17. #if (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)))||(defined(__clang__) && (__clang_major__ > 3 || (__clang_major__ == 3 && __clang_minor__ >= 2)))
  18. #define HAVE_USE_MS_ABI 1
  19. #else
  20. #error Compiler is too old for GNU_EFI_USE_MS_ABI
  21. #endif
  22. #endif
  23. //
  24. // Basic int types of various widths
  25. //
  26. #if !defined(__STDC_VERSION__) || (__STDC_VERSION__ < 199901L ) && !defined(__cplusplus)
  27. // No ANSI C 1999/2000 stdint.h integer width declarations
  28. #if defined(_MSC_EXTENSIONS)
  29. // Use Microsoft C compiler integer width declarations
  30. typedef unsigned __int64 uint64_t;
  31. typedef __int64 int64_t;
  32. typedef unsigned __int32 uint32_t;
  33. typedef __int32 int32_t;
  34. typedef unsigned short uint16_t;
  35. typedef short int16_t;
  36. typedef unsigned char uint8_t;
  37. typedef char int8_t;
  38. #elif defined(__GNUC__)
  39. typedef int __attribute__((__mode__(__DI__))) int64_t;
  40. typedef unsigned int __attribute__((__mode__(__DI__))) uint64_t;
  41. typedef unsigned int uint32_t;
  42. typedef int int32_t;
  43. typedef unsigned short uint16_t;
  44. typedef short int16_t;
  45. typedef unsigned char uint8_t;
  46. typedef signed char int8_t;
  47. #elif defined(UNIX_LP64)
  48. /* Use LP64 programming model from C_FLAGS for integer width declarations */
  49. typedef unsigned long uint64_t;
  50. typedef long int64_t;
  51. typedef unsigned int uint32_t;
  52. typedef int int32_t;
  53. typedef unsigned short uint16_t;
  54. typedef short int16_t;
  55. typedef unsigned char uint8_t;
  56. typedef char int8_t;
  57. #else
  58. /* Assume P64 programming model from C_FLAGS for integer width declarations */
  59. typedef unsigned long long uint64_t __attribute__((aligned (8)));
  60. typedef long long int64_t __attribute__((aligned (8)));
  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. #endif
  68. typedef uint64_t uintptr_t;
  69. typedef int64_t intptr_t;
  70. #else
  71. #include <stdint.h>
  72. #endif
  73. //
  74. // Basic EFI types of various widths
  75. //
  76. #ifndef __WCHAR_TYPE__
  77. # define __WCHAR_TYPE__ short
  78. #endif
  79. typedef uint64_t UINT64;
  80. typedef int64_t INT64;
  81. #ifndef _BASETSD_H_
  82. typedef uint32_t UINT32;
  83. typedef int32_t INT32;
  84. #endif
  85. typedef uint16_t UINT16;
  86. typedef __CHAR16_TYPE__ CHAR16;
  87. typedef int16_t INT16;
  88. typedef uint8_t UINT8;
  89. typedef char CHAR8;
  90. typedef int8_t INT8;
  91. typedef __WCHAR_TYPE__ WCHAR;
  92. #undef VOID
  93. #define VOID void
  94. typedef int64_t INTN;
  95. typedef uint64_t UINTN;
  96. #ifdef EFI_NT_EMULATOR
  97. #define POST_CODE(_Data)
  98. #else
  99. #ifdef EFI_DEBUG
  100. #define POST_CODE(_Data) __asm mov eax,(_Data) __asm out 0x80,al
  101. #else
  102. #define POST_CODE(_Data)
  103. #endif
  104. #endif
  105. #define EFIERR(a) (0x8000000000000000 | a)
  106. #define EFI_ERROR_MASK 0x8000000000000000
  107. #define EFIERR_OEM(a) (0xc000000000000000 | a)
  108. #define BAD_POINTER 0xFBFBFBFBFBFBFBFB
  109. #define MAX_ADDRESS 0xFFFFFFFFFFFFFFFF
  110. #ifdef EFI_NT_EMULATOR
  111. #define BREAKPOINT() __asm { int 3 }
  112. #else
  113. #define BREAKPOINT() while (TRUE); // Make it hang on Bios[Dbg]32
  114. #endif
  115. //
  116. // Pointers must be aligned to these address to function
  117. //
  118. #define MIN_ALIGNMENT_SIZE 4
  119. #define ALIGN_VARIABLE(Value ,Adjustment) \
  120. (UINTN)Adjustment = 0; \
  121. if((UINTN)Value % MIN_ALIGNMENT_SIZE) \
  122. (UINTN)Adjustment = MIN_ALIGNMENT_SIZE - ((UINTN)Value % MIN_ALIGNMENT_SIZE); \
  123. Value = (UINTN)Value + (UINTN)Adjustment
  124. //
  125. // Define macros to build data structure signatures from characters.
  126. //
  127. #define EFI_SIGNATURE_16(A,B) ((A) | (B<<8))
  128. #define EFI_SIGNATURE_32(A,B,C,D) (EFI_SIGNATURE_16(A,B) | (EFI_SIGNATURE_16(C,D) << 16))
  129. #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))
  130. //
  131. // To export & import functions in the EFI emulator environment
  132. //
  133. #ifdef EFI_NT_EMULATOR
  134. #define EXPORTAPI __declspec( dllexport )
  135. #else
  136. #define EXPORTAPI
  137. #endif
  138. //
  139. // EFIAPI - prototype calling convention for EFI function pointers
  140. // BOOTSERVICE - prototype for implementation of a boot service interface
  141. // RUNTIMESERVICE - prototype for implementation of a runtime service interface
  142. // RUNTIMEFUNCTION - prototype for implementation of a runtime function that is not a service
  143. // RUNTIME_CODE - pragma macro for declaring runtime code
  144. //
  145. #ifndef EFIAPI // Forces EFI calling conventions reguardless of compiler options
  146. #ifdef _MSC_EXTENSIONS
  147. #define EFIAPI __cdecl // Force C calling convention for Microsoft C compiler
  148. #elif defined(HAVE_USE_MS_ABI)
  149. // Force amd64/ms calling conventions.
  150. #define EFIAPI __attribute__((ms_abi))
  151. #else
  152. #define EFIAPI // Substitute expresion to force C calling convention
  153. #endif
  154. #endif
  155. #define BOOTSERVICE
  156. //#define RUNTIMESERVICE(proto,a) alloc_text("rtcode",a); proto a
  157. //#define RUNTIMEFUNCTION(proto,a) alloc_text("rtcode",a); proto a
  158. #define RUNTIMESERVICE
  159. #define RUNTIMEFUNCTION
  160. #define RUNTIME_CODE(a) alloc_text("rtcode", a)
  161. #define BEGIN_RUNTIME_DATA() data_seg("rtdata")
  162. #define END_RUNTIME_DATA() data_seg("")
  163. #define VOLATILE volatile
  164. #define MEMORY_FENCE()
  165. #ifdef EFI_NT_EMULATOR
  166. //
  167. // To help ensure proper coding of integrated drivers, they are
  168. // compiled as DLLs. In NT they require a dll init entry pointer.
  169. // The macro puts a stub entry point into the DLL so it will load.
  170. //
  171. #define EFI_DRIVER_ENTRY_POINT(InitFunction) \
  172. UINTN \
  173. __stdcall \
  174. _DllMainCRTStartup ( \
  175. UINTN Inst, \
  176. UINTN reason_for_call, \
  177. VOID *rserved \
  178. ) \
  179. { \
  180. return 1; \
  181. } \
  182. \
  183. int \
  184. EXPORTAPI \
  185. __cdecl \
  186. InitializeDriver ( \
  187. void *ImageHandle, \
  188. void *SystemTable \
  189. ) \
  190. { \
  191. return InitFunction(ImageHandle, SystemTable); \
  192. }
  193. #define LOAD_INTERNAL_DRIVER(_if, type, name, entry) \
  194. (_if)->LoadInternal(type, name, NULL)
  195. #else // EFI_NT_EMULATOR
  196. //
  197. // When build similiar to FW, then link everything together as
  198. // one big module. For the MSVC toolchain, we simply tell the
  199. // linker what our driver init function is using /ENTRY.
  200. //
  201. #if defined(_MSC_EXTENSIONS)
  202. #define EFI_DRIVER_ENTRY_POINT(InitFunction) \
  203. __pragma(comment(linker, "/ENTRY:" # InitFunction))
  204. #else
  205. #define EFI_DRIVER_ENTRY_POINT(InitFunction) \
  206. UINTN \
  207. InitializeDriver ( \
  208. VOID *ImageHandle, \
  209. VOID *SystemTable \
  210. ) \
  211. { \
  212. return InitFunction(ImageHandle, \
  213. SystemTable); \
  214. } \
  215. \
  216. EFI_STATUS efi_main( \
  217. EFI_HANDLE image, \
  218. EFI_SYSTEM_TABLE *systab \
  219. ) __attribute__((weak, \
  220. alias ("InitializeDriver")));
  221. #endif
  222. #define LOAD_INTERNAL_DRIVER(_if, type, name, entry) \
  223. (_if)->LoadInternal(type, name, entry)
  224. #endif // EFI_NT_EMULATOR
  225. //
  226. // Some compilers don't support the forward reference construct:
  227. // typedef struct XXXXX
  228. //
  229. // The following macro provide a workaround for such cases.
  230. //
  231. #ifdef NO_INTERFACE_DECL
  232. #define INTERFACE_DECL(x)
  233. #else
  234. #if defined(__GNUC__) || defined(_MSC_EXTENSIONS)
  235. #define INTERFACE_DECL(x) struct x
  236. #else
  237. #define INTERFACE_DECL(x) typedef struct x
  238. #endif
  239. #endif
  240. /* for x86_64, EFI_FUNCTION_WRAPPER must be defined */
  241. #if defined(HAVE_USE_MS_ABI)
  242. #define uefi_call_wrapper(func, va_num, ...) func(__VA_ARGS__)
  243. #else
  244. /*
  245. Credits for macro-magic:
  246. https://groups.google.com/forum/?fromgroups#!topic/comp.std.c/d-6Mj5Lko_s
  247. http://efesx.com/2010/08/31/overloading-macros/
  248. */
  249. #define __VA_NARG__(...) \
  250. __VA_NARG_(_0, ## __VA_ARGS__, __RSEQ_N())
  251. #define __VA_NARG_(...) \
  252. __VA_ARG_N(__VA_ARGS__)
  253. #define __VA_ARG_N( \
  254. _0,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,N,...) N
  255. #define __RSEQ_N() \
  256. 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0
  257. #define __VA_ARG_NSUFFIX__(prefix,...) \
  258. __VA_ARG_NSUFFIX_N(prefix, __VA_NARG__(__VA_ARGS__))
  259. #define __VA_ARG_NSUFFIX_N(prefix,nargs) \
  260. __VA_ARG_NSUFFIX_N_(prefix, nargs)
  261. #define __VA_ARG_NSUFFIX_N_(prefix,nargs) \
  262. prefix ## nargs
  263. /* Prototypes of EFI cdecl -> stdcall trampolines */
  264. UINT64 efi_call0(void *func);
  265. UINT64 efi_call1(void *func, UINT64 arg1);
  266. UINT64 efi_call2(void *func, UINT64 arg1, UINT64 arg2);
  267. UINT64 efi_call3(void *func, UINT64 arg1, UINT64 arg2, UINT64 arg3);
  268. UINT64 efi_call4(void *func, UINT64 arg1, UINT64 arg2, UINT64 arg3,
  269. UINT64 arg4);
  270. UINT64 efi_call5(void *func, UINT64 arg1, UINT64 arg2, UINT64 arg3,
  271. UINT64 arg4, UINT64 arg5);
  272. UINT64 efi_call6(void *func, UINT64 arg1, UINT64 arg2, UINT64 arg3,
  273. UINT64 arg4, UINT64 arg5, UINT64 arg6);
  274. UINT64 efi_call7(void *func, UINT64 arg1, UINT64 arg2, UINT64 arg3,
  275. UINT64 arg4, UINT64 arg5, UINT64 arg6, UINT64 arg7);
  276. UINT64 efi_call8(void *func, UINT64 arg1, UINT64 arg2, UINT64 arg3,
  277. UINT64 arg4, UINT64 arg5, UINT64 arg6, UINT64 arg7,
  278. UINT64 arg8);
  279. UINT64 efi_call9(void *func, UINT64 arg1, UINT64 arg2, UINT64 arg3,
  280. UINT64 arg4, UINT64 arg5, UINT64 arg6, UINT64 arg7,
  281. UINT64 arg8, UINT64 arg9);
  282. UINT64 efi_call10(void *func, UINT64 arg1, UINT64 arg2, UINT64 arg3,
  283. UINT64 arg4, UINT64 arg5, UINT64 arg6, UINT64 arg7,
  284. UINT64 arg8, UINT64 arg9, UINT64 arg10);
  285. /* Front-ends to efi_callX to avoid compiler warnings */
  286. #define _cast64_efi_call0(f) \
  287. efi_call0(f)
  288. #define _cast64_efi_call1(f,a1) \
  289. efi_call1(f, (UINT64)(a1))
  290. #define _cast64_efi_call2(f,a1,a2) \
  291. efi_call2(f, (UINT64)(a1), (UINT64)(a2))
  292. #define _cast64_efi_call3(f,a1,a2,a3) \
  293. efi_call3(f, (UINT64)(a1), (UINT64)(a2), (UINT64)(a3))
  294. #define _cast64_efi_call4(f,a1,a2,a3,a4) \
  295. efi_call4(f, (UINT64)(a1), (UINT64)(a2), (UINT64)(a3), (UINT64)(a4))
  296. #define _cast64_efi_call5(f,a1,a2,a3,a4,a5) \
  297. efi_call5(f, (UINT64)(a1), (UINT64)(a2), (UINT64)(a3), (UINT64)(a4), \
  298. (UINT64)(a5))
  299. #define _cast64_efi_call6(f,a1,a2,a3,a4,a5,a6) \
  300. efi_call6(f, (UINT64)(a1), (UINT64)(a2), (UINT64)(a3), (UINT64)(a4), \
  301. (UINT64)(a5), (UINT64)(a6))
  302. #define _cast64_efi_call7(f,a1,a2,a3,a4,a5,a6,a7) \
  303. efi_call7(f, (UINT64)(a1), (UINT64)(a2), (UINT64)(a3), (UINT64)(a4), \
  304. (UINT64)(a5), (UINT64)(a6), (UINT64)(a7))
  305. #define _cast64_efi_call8(f,a1,a2,a3,a4,a5,a6,a7,a8) \
  306. efi_call8(f, (UINT64)(a1), (UINT64)(a2), (UINT64)(a3), (UINT64)(a4), \
  307. (UINT64)(a5), (UINT64)(a6), (UINT64)(a7), (UINT64)(a8))
  308. #define _cast64_efi_call9(f,a1,a2,a3,a4,a5,a6,a7,a8,a9) \
  309. efi_call9(f, (UINT64)(a1), (UINT64)(a2), (UINT64)(a3), (UINT64)(a4), \
  310. (UINT64)(a5), (UINT64)(a6), (UINT64)(a7), (UINT64)(a8), \
  311. (UINT64)(a9))
  312. #define _cast64_efi_call10(f,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10) \
  313. efi_call10(f, (UINT64)(a1), (UINT64)(a2), (UINT64)(a3), (UINT64)(a4), \
  314. (UINT64)(a5), (UINT64)(a6), (UINT64)(a7), (UINT64)(a8), \
  315. (UINT64)(a9), (UINT64)(a10))
  316. /* main wrapper (va_num ignored) */
  317. #define uefi_call_wrapper(func,va_num,...) \
  318. __VA_ARG_NSUFFIX__(_cast64_efi_call, __VA_ARGS__) (func , ##__VA_ARGS__)
  319. #endif
  320. #if defined(HAVE_USE_MS_ABI) && !defined(_MSC_EXTENSIONS)
  321. #define EFI_FUNCTION __attribute__((ms_abi))
  322. #else
  323. #define EFI_FUNCTION
  324. #endif
  325. #ifdef _MSC_EXTENSIONS
  326. #pragma warning ( disable : 4731 ) // Suppress warnings about modification of EBP
  327. #endif
  328. #endif