efibind.h 13 KB

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