efibind.h 13 KB

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