efibind.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * Copright (C) 2014 - 2015 Linaro Ltd.
  3. * Author: Ard Biesheuvel <ard.biesheuvel@linaro.org>
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice and this list of conditions, without modification.
  10. * 2. The name of the author may not be used to endorse or promote products
  11. * derived from this software without specific prior written permission.
  12. *
  13. * Alternatively, this software may be distributed under the terms of the
  14. * GNU General Public License as published by the Free Software Foundation;
  15. * either version 2 of the License, or (at your option) any later version.
  16. */
  17. #if !defined(__STDC_VERSION__) || (__STDC_VERSION__ < 199901L )
  18. // ANSI C 1999/2000 stdint.h integer width declarations
  19. typedef unsigned long long uint64_t;
  20. typedef long long int64_t;
  21. typedef unsigned int uint32_t;
  22. typedef int int32_t;
  23. typedef unsigned short uint16_t;
  24. typedef short int16_t;
  25. typedef unsigned char uint8_t;
  26. typedef signed char int8_t; // unqualified 'char' is unsigned on ARM
  27. #else
  28. #include <stdint.h>
  29. #endif
  30. /*
  31. * This prevents GCC from emitting GOT based relocations, and use R_ARM_REL32
  32. * relative relocations instead, which are more suitable for static binaries.
  33. */
  34. #pragma GCC visibility push (hidden)
  35. //
  36. // Basic EFI types of various widths
  37. //
  38. #ifndef __WCHAR_TYPE__
  39. # define __WCHAR_TYPE__ short
  40. #endif
  41. typedef uint64_t UINT64;
  42. typedef int64_t INT64;
  43. typedef uint32_t UINT32;
  44. typedef int32_t INT32;
  45. typedef uint16_t UINT16;
  46. typedef int16_t INT16;
  47. typedef uint8_t UINT8;
  48. typedef int8_t INT8;
  49. typedef __WCHAR_TYPE__ WCHAR;
  50. #undef VOID
  51. #define VOID void
  52. typedef int32_t INTN;
  53. typedef uint32_t UINTN;
  54. #define EFIERR(a) (0x80000000 | a)
  55. #define EFI_ERROR_MASK 0x80000000
  56. #define EFIERR_OEM(a) (0xc0000000 | a)
  57. #define BAD_POINTER 0xFBFBFBFB
  58. #define MAX_ADDRESS 0xFFFFFFFF
  59. #define BREAKPOINT() while (TRUE);
  60. //
  61. // Pointers must be aligned to these address to function
  62. //
  63. #define MIN_ALIGNMENT_SIZE 4
  64. #define ALIGN_VARIABLE(Value ,Adjustment) \
  65. (UINTN)Adjustment = 0; \
  66. if((UINTN)Value % MIN_ALIGNMENT_SIZE) \
  67. (UINTN)Adjustment = MIN_ALIGNMENT_SIZE - ((UINTN)Value % MIN_ALIGNMENT_SIZE); \
  68. Value = (UINTN)Value + (UINTN)Adjustment
  69. //
  70. // Define macros to build data structure signatures from characters.
  71. //
  72. #define EFI_SIGNATURE_16(A,B) ((A) | (B<<8))
  73. #define EFI_SIGNATURE_32(A,B,C,D) (EFI_SIGNATURE_16(A,B) | (EFI_SIGNATURE_16(C,D) << 16))
  74. #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))
  75. //
  76. // EFIAPI - prototype calling convention for EFI function pointers
  77. // BOOTSERVICE - prototype for implementation of a boot service interface
  78. // RUNTIMESERVICE - prototype for implementation of a runtime service interface
  79. // RUNTIMEFUNCTION - prototype for implementation of a runtime function that is not a service
  80. // RUNTIME_CODE - pragma macro for declaring runtime code
  81. //
  82. #ifndef EFIAPI // Forces EFI calling conventions reguardless of compiler options
  83. #define EFIAPI // Substitute expresion to force C calling convention
  84. #endif
  85. #define BOOTSERVICE
  86. #define RUNTIMESERVICE
  87. #define RUNTIMEFUNCTION
  88. #define RUNTIME_CODE(a) alloc_text("rtcode", a)
  89. #define BEGIN_RUNTIME_DATA() data_seg("rtdata")
  90. #define END_RUNTIME_DATA() data_seg("")
  91. #define VOLATILE volatile
  92. #define MEMORY_FENCE __sync_synchronize
  93. //
  94. // When build similiar to FW, then link everything together as
  95. // one big module.
  96. //
  97. #define EFI_DRIVER_ENTRY_POINT(InitFunction) \
  98. UINTN \
  99. InitializeDriver ( \
  100. VOID *ImageHandle, \
  101. VOID *SystemTable \
  102. ) \
  103. { \
  104. return InitFunction(ImageHandle, \
  105. SystemTable); \
  106. } \
  107. \
  108. EFI_STATUS efi_main( \
  109. EFI_HANDLE image, \
  110. EFI_SYSTEM_TABLE *systab \
  111. ) __attribute__((weak, \
  112. alias ("InitializeDriver")));
  113. #define LOAD_INTERNAL_DRIVER(_if, type, name, entry) \
  114. (_if)->LoadInternal(type, name, entry)
  115. //
  116. // Some compilers don't support the forward reference construct:
  117. // typedef struct XXXXX
  118. //
  119. // The following macro provide a workaround for such cases.
  120. #define INTERFACE_DECL(x) struct x
  121. #define uefi_call_wrapper(func, va_num, ...) func(__VA_ARGS__)
  122. #define EFI_FUNCTION
  123. static inline UINT64 DivU64x32(UINT64 Dividend, UINTN Divisor, UINTN *Remainder)
  124. {
  125. /*
  126. * GCC turns a division into a multiplication and shift with precalculated
  127. * constants if the divisor is constant and the dividend fits into a 32 bit
  128. * variable. Otherwise, it will turn this into calls into the 32-bit div
  129. * library functions.
  130. */
  131. if (Remainder)
  132. *Remainder = Dividend % Divisor;
  133. Dividend = Dividend / Divisor;
  134. return Dividend;
  135. }