efibind.h 13 KB

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