You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Base.h 30KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023
  1. /** @file
  2. Root include file for Mde Package Base type modules
  3. This is the include file for any module of type base. Base modules only use
  4. types defined via this include file and can be ported easily to any
  5. environment. There are a set of base libraries in the Mde Package that can
  6. be used to implement base modules.
  7. Copyright (c) 2006 - 2013, Intel Corporation. All rights reserved.<BR>
  8. Portions copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
  9. This program and the accompanying materials
  10. are licensed and made available under the terms and conditions of the BSD License
  11. which accompanies this distribution. The full text of the license may be found at
  12. http://opensource.org/licenses/bsd-license.php.
  13. THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
  14. WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
  15. **/
  16. #ifndef __BASE_H__
  17. #define __BASE_H__
  18. FILE_LICENCE ( BSD3 );
  19. //
  20. // Include processor specific binding
  21. //
  22. #include <ipxe/efi/ProcessorBind.h>
  23. /**
  24. Verifies the storage size of a given data type.
  25. This macro generates a divide by zero error or a zero size array declaration in
  26. the preprocessor if the size is incorrect. These are declared as "extern" so
  27. the space for these arrays will not be in the modules.
  28. @param TYPE The date type to determine the size of.
  29. @param Size The expected size for the TYPE.
  30. **/
  31. #define VERIFY_SIZE_OF(TYPE, Size) extern UINT8 _VerifySizeof##TYPE[(sizeof(TYPE) == (Size)) / (sizeof(TYPE) == (Size))]
  32. //
  33. // Verify that ProcessorBind.h produced UEFI Data Types that are compliant with
  34. // Section 2.3.1 of the UEFI 2.3 Specification.
  35. //
  36. VERIFY_SIZE_OF (BOOLEAN, 1);
  37. VERIFY_SIZE_OF (INT8, 1);
  38. VERIFY_SIZE_OF (UINT8, 1);
  39. VERIFY_SIZE_OF (INT16, 2);
  40. VERIFY_SIZE_OF (UINT16, 2);
  41. VERIFY_SIZE_OF (INT32, 4);
  42. VERIFY_SIZE_OF (UINT32, 4);
  43. VERIFY_SIZE_OF (INT64, 8);
  44. VERIFY_SIZE_OF (UINT64, 8);
  45. VERIFY_SIZE_OF (CHAR8, 1);
  46. VERIFY_SIZE_OF (CHAR16, 2);
  47. //
  48. // The Microsoft* C compiler can removed references to unreferenced data items
  49. // if the /OPT:REF linker option is used. We defined a macro as this is a
  50. // a non standard extension
  51. //
  52. #if defined(_MSC_EXTENSIONS) && !defined (MDE_CPU_EBC)
  53. ///
  54. /// Remove global variable from the linked image if there are no references to
  55. /// it after all compiler and linker optimizations have been performed.
  56. ///
  57. ///
  58. #define GLOBAL_REMOVE_IF_UNREFERENCED __declspec(selectany)
  59. #else
  60. ///
  61. /// Remove the global variable from the linked image if there are no references
  62. /// to it after all compiler and linker optimizations have been performed.
  63. ///
  64. ///
  65. #define GLOBAL_REMOVE_IF_UNREFERENCED
  66. #endif
  67. //
  68. // For symbol name in GNU assembly code, an extra "_" is necessary
  69. //
  70. #if defined(__GNUC__)
  71. ///
  72. /// Private worker functions for ASM_PFX()
  73. ///
  74. #define _CONCATENATE(a, b) __CONCATENATE(a, b)
  75. #define __CONCATENATE(a, b) a ## b
  76. ///
  77. /// The __USER_LABEL_PREFIX__ macro predefined by GNUC represents the prefix
  78. /// on symbols in assembly language.
  79. ///
  80. #define ASM_PFX(name) _CONCATENATE (__USER_LABEL_PREFIX__, name)
  81. #endif
  82. #if __APPLE__
  83. //
  84. // Apple extension that is used by the linker to optimize code size
  85. // with assembly functions. Put at the end of your .S files
  86. //
  87. #define ASM_FUNCTION_REMOVE_IF_UNREFERENCED .subsections_via_symbols
  88. #else
  89. #define ASM_FUNCTION_REMOVE_IF_UNREFERENCED
  90. #endif
  91. #ifdef __CC_ARM
  92. //
  93. // Older RVCT ARM compilers don't fully support #pragma pack and require __packed
  94. // as a prefix for the structure.
  95. //
  96. #define PACKED __packed
  97. #else
  98. #define PACKED
  99. #endif
  100. ///
  101. /// 128 bit buffer containing a unique identifier value.
  102. /// Unless otherwise specified, aligned on a 64 bit boundary.
  103. ///
  104. typedef struct {
  105. UINT32 Data1;
  106. UINT16 Data2;
  107. UINT16 Data3;
  108. UINT8 Data4[8];
  109. } GUID;
  110. //
  111. // 8-bytes unsigned value that represents a physical system address.
  112. //
  113. typedef UINT64 PHYSICAL_ADDRESS;
  114. ///
  115. /// LIST_ENTRY structure definition.
  116. ///
  117. typedef struct _LIST_ENTRY LIST_ENTRY;
  118. ///
  119. /// _LIST_ENTRY structure definition.
  120. ///
  121. struct _LIST_ENTRY {
  122. LIST_ENTRY *ForwardLink;
  123. LIST_ENTRY *BackLink;
  124. };
  125. //
  126. // Modifiers to abstract standard types to aid in debug of problems
  127. //
  128. ///
  129. /// Datum is read-only.
  130. ///
  131. #define CONST const
  132. ///
  133. /// Datum is scoped to the current file or function.
  134. ///
  135. #define STATIC static
  136. ///
  137. /// Undeclared type.
  138. ///
  139. #define VOID void
  140. //
  141. // Modifiers for Data Types used to self document code.
  142. // This concept is borrowed for UEFI specification.
  143. //
  144. ///
  145. /// Datum is passed to the function.
  146. ///
  147. #define IN
  148. ///
  149. /// Datum is returned from the function.
  150. ///
  151. #define OUT
  152. ///
  153. /// Passing the datum to the function is optional, and a NULL
  154. /// is passed if the value is not supplied.
  155. ///
  156. #define OPTIONAL
  157. //
  158. // UEFI specification claims 1 and 0. We are concerned about the
  159. // complier portability so we did it this way.
  160. //
  161. ///
  162. /// Boolean true value. UEFI Specification defines this value to be 1,
  163. /// but this form is more portable.
  164. ///
  165. #define TRUE ((BOOLEAN)(1==1))
  166. ///
  167. /// Boolean false value. UEFI Specification defines this value to be 0,
  168. /// but this form is more portable.
  169. ///
  170. #define FALSE ((BOOLEAN)(0==1))
  171. ///
  172. /// NULL pointer (VOID *)
  173. ///
  174. #define NULL ((VOID *) 0)
  175. ///
  176. /// Maximum values for common UEFI Data Types
  177. ///
  178. #define MAX_INT8 ((INT8)0x7F)
  179. #define MAX_UINT8 ((UINT8)0xFF)
  180. #define MAX_INT16 ((INT16)0x7FFF)
  181. #define MAX_UINT16 ((UINT16)0xFFFF)
  182. #define MAX_INT32 ((INT32)0x7FFFFFFF)
  183. #define MAX_UINT32 ((UINT32)0xFFFFFFFF)
  184. #define MAX_INT64 ((INT64)0x7FFFFFFFFFFFFFFFULL)
  185. #define MAX_UINT64 ((UINT64)0xFFFFFFFFFFFFFFFFULL)
  186. #define BIT0 0x00000001
  187. #define BIT1 0x00000002
  188. #define BIT2 0x00000004
  189. #define BIT3 0x00000008
  190. #define BIT4 0x00000010
  191. #define BIT5 0x00000020
  192. #define BIT6 0x00000040
  193. #define BIT7 0x00000080
  194. #define BIT8 0x00000100
  195. #define BIT9 0x00000200
  196. #define BIT10 0x00000400
  197. #define BIT11 0x00000800
  198. #define BIT12 0x00001000
  199. #define BIT13 0x00002000
  200. #define BIT14 0x00004000
  201. #define BIT15 0x00008000
  202. #define BIT16 0x00010000
  203. #define BIT17 0x00020000
  204. #define BIT18 0x00040000
  205. #define BIT19 0x00080000
  206. #define BIT20 0x00100000
  207. #define BIT21 0x00200000
  208. #define BIT22 0x00400000
  209. #define BIT23 0x00800000
  210. #define BIT24 0x01000000
  211. #define BIT25 0x02000000
  212. #define BIT26 0x04000000
  213. #define BIT27 0x08000000
  214. #define BIT28 0x10000000
  215. #define BIT29 0x20000000
  216. #define BIT30 0x40000000
  217. #define BIT31 0x80000000
  218. #define BIT32 0x0000000100000000ULL
  219. #define BIT33 0x0000000200000000ULL
  220. #define BIT34 0x0000000400000000ULL
  221. #define BIT35 0x0000000800000000ULL
  222. #define BIT36 0x0000001000000000ULL
  223. #define BIT37 0x0000002000000000ULL
  224. #define BIT38 0x0000004000000000ULL
  225. #define BIT39 0x0000008000000000ULL
  226. #define BIT40 0x0000010000000000ULL
  227. #define BIT41 0x0000020000000000ULL
  228. #define BIT42 0x0000040000000000ULL
  229. #define BIT43 0x0000080000000000ULL
  230. #define BIT44 0x0000100000000000ULL
  231. #define BIT45 0x0000200000000000ULL
  232. #define BIT46 0x0000400000000000ULL
  233. #define BIT47 0x0000800000000000ULL
  234. #define BIT48 0x0001000000000000ULL
  235. #define BIT49 0x0002000000000000ULL
  236. #define BIT50 0x0004000000000000ULL
  237. #define BIT51 0x0008000000000000ULL
  238. #define BIT52 0x0010000000000000ULL
  239. #define BIT53 0x0020000000000000ULL
  240. #define BIT54 0x0040000000000000ULL
  241. #define BIT55 0x0080000000000000ULL
  242. #define BIT56 0x0100000000000000ULL
  243. #define BIT57 0x0200000000000000ULL
  244. #define BIT58 0x0400000000000000ULL
  245. #define BIT59 0x0800000000000000ULL
  246. #define BIT60 0x1000000000000000ULL
  247. #define BIT61 0x2000000000000000ULL
  248. #define BIT62 0x4000000000000000ULL
  249. #define BIT63 0x8000000000000000ULL
  250. #define SIZE_1KB 0x00000400
  251. #define SIZE_2KB 0x00000800
  252. #define SIZE_4KB 0x00001000
  253. #define SIZE_8KB 0x00002000
  254. #define SIZE_16KB 0x00004000
  255. #define SIZE_32KB 0x00008000
  256. #define SIZE_64KB 0x00010000
  257. #define SIZE_128KB 0x00020000
  258. #define SIZE_256KB 0x00040000
  259. #define SIZE_512KB 0x00080000
  260. #define SIZE_1MB 0x00100000
  261. #define SIZE_2MB 0x00200000
  262. #define SIZE_4MB 0x00400000
  263. #define SIZE_8MB 0x00800000
  264. #define SIZE_16MB 0x01000000
  265. #define SIZE_32MB 0x02000000
  266. #define SIZE_64MB 0x04000000
  267. #define SIZE_128MB 0x08000000
  268. #define SIZE_256MB 0x10000000
  269. #define SIZE_512MB 0x20000000
  270. #define SIZE_1GB 0x40000000
  271. #define SIZE_2GB 0x80000000
  272. #define SIZE_4GB 0x0000000100000000ULL
  273. #define SIZE_8GB 0x0000000200000000ULL
  274. #define SIZE_16GB 0x0000000400000000ULL
  275. #define SIZE_32GB 0x0000000800000000ULL
  276. #define SIZE_64GB 0x0000001000000000ULL
  277. #define SIZE_128GB 0x0000002000000000ULL
  278. #define SIZE_256GB 0x0000004000000000ULL
  279. #define SIZE_512GB 0x0000008000000000ULL
  280. #define SIZE_1TB 0x0000010000000000ULL
  281. #define SIZE_2TB 0x0000020000000000ULL
  282. #define SIZE_4TB 0x0000040000000000ULL
  283. #define SIZE_8TB 0x0000080000000000ULL
  284. #define SIZE_16TB 0x0000100000000000ULL
  285. #define SIZE_32TB 0x0000200000000000ULL
  286. #define SIZE_64TB 0x0000400000000000ULL
  287. #define SIZE_128TB 0x0000800000000000ULL
  288. #define SIZE_256TB 0x0001000000000000ULL
  289. #define SIZE_512TB 0x0002000000000000ULL
  290. #define SIZE_1PB 0x0004000000000000ULL
  291. #define SIZE_2PB 0x0008000000000000ULL
  292. #define SIZE_4PB 0x0010000000000000ULL
  293. #define SIZE_8PB 0x0020000000000000ULL
  294. #define SIZE_16PB 0x0040000000000000ULL
  295. #define SIZE_32PB 0x0080000000000000ULL
  296. #define SIZE_64PB 0x0100000000000000ULL
  297. #define SIZE_128PB 0x0200000000000000ULL
  298. #define SIZE_256PB 0x0400000000000000ULL
  299. #define SIZE_512PB 0x0800000000000000ULL
  300. #define SIZE_1EB 0x1000000000000000ULL
  301. #define SIZE_2EB 0x2000000000000000ULL
  302. #define SIZE_4EB 0x4000000000000000ULL
  303. #define SIZE_8EB 0x8000000000000000ULL
  304. #define BASE_1KB 0x00000400
  305. #define BASE_2KB 0x00000800
  306. #define BASE_4KB 0x00001000
  307. #define BASE_8KB 0x00002000
  308. #define BASE_16KB 0x00004000
  309. #define BASE_32KB 0x00008000
  310. #define BASE_64KB 0x00010000
  311. #define BASE_128KB 0x00020000
  312. #define BASE_256KB 0x00040000
  313. #define BASE_512KB 0x00080000
  314. #define BASE_1MB 0x00100000
  315. #define BASE_2MB 0x00200000
  316. #define BASE_4MB 0x00400000
  317. #define BASE_8MB 0x00800000
  318. #define BASE_16MB 0x01000000
  319. #define BASE_32MB 0x02000000
  320. #define BASE_64MB 0x04000000
  321. #define BASE_128MB 0x08000000
  322. #define BASE_256MB 0x10000000
  323. #define BASE_512MB 0x20000000
  324. #define BASE_1GB 0x40000000
  325. #define BASE_2GB 0x80000000
  326. #define BASE_4GB 0x0000000100000000ULL
  327. #define BASE_8GB 0x0000000200000000ULL
  328. #define BASE_16GB 0x0000000400000000ULL
  329. #define BASE_32GB 0x0000000800000000ULL
  330. #define BASE_64GB 0x0000001000000000ULL
  331. #define BASE_128GB 0x0000002000000000ULL
  332. #define BASE_256GB 0x0000004000000000ULL
  333. #define BASE_512GB 0x0000008000000000ULL
  334. #define BASE_1TB 0x0000010000000000ULL
  335. #define BASE_2TB 0x0000020000000000ULL
  336. #define BASE_4TB 0x0000040000000000ULL
  337. #define BASE_8TB 0x0000080000000000ULL
  338. #define BASE_16TB 0x0000100000000000ULL
  339. #define BASE_32TB 0x0000200000000000ULL
  340. #define BASE_64TB 0x0000400000000000ULL
  341. #define BASE_128TB 0x0000800000000000ULL
  342. #define BASE_256TB 0x0001000000000000ULL
  343. #define BASE_512TB 0x0002000000000000ULL
  344. #define BASE_1PB 0x0004000000000000ULL
  345. #define BASE_2PB 0x0008000000000000ULL
  346. #define BASE_4PB 0x0010000000000000ULL
  347. #define BASE_8PB 0x0020000000000000ULL
  348. #define BASE_16PB 0x0040000000000000ULL
  349. #define BASE_32PB 0x0080000000000000ULL
  350. #define BASE_64PB 0x0100000000000000ULL
  351. #define BASE_128PB 0x0200000000000000ULL
  352. #define BASE_256PB 0x0400000000000000ULL
  353. #define BASE_512PB 0x0800000000000000ULL
  354. #define BASE_1EB 0x1000000000000000ULL
  355. #define BASE_2EB 0x2000000000000000ULL
  356. #define BASE_4EB 0x4000000000000000ULL
  357. #define BASE_8EB 0x8000000000000000ULL
  358. //
  359. // Support for variable length argument lists using the ANSI standard.
  360. //
  361. // Since we are using the ANSI standard we used the standard naming and
  362. // did not follow the coding convention
  363. //
  364. // VA_LIST - typedef for argument list.
  365. // VA_START (VA_LIST Marker, argument before the ...) - Init Marker for use.
  366. // VA_END (VA_LIST Marker) - Clear Marker
  367. // VA_ARG (VA_LIST Marker, var arg size) - Use Marker to get an argument from
  368. // the ... list. You must know the size and pass it in this macro.
  369. // VA_COPY (VA_LIST Dest, VA_LIST Start) - Initialize Dest as a copy of Start.
  370. //
  371. // example:
  372. //
  373. // UINTN
  374. // ExampleVarArg (
  375. // IN UINTN NumberOfArgs,
  376. // ...
  377. // )
  378. // {
  379. // VA_LIST Marker;
  380. // UINTN Index;
  381. // UINTN Result;
  382. //
  383. // //
  384. // // Initialize the Marker
  385. // //
  386. // VA_START (Marker, NumberOfArgs);
  387. // for (Index = 0, Result = 0; Index < NumberOfArgs; Index++) {
  388. // //
  389. // // The ... list is a series of UINTN values, so average them up.
  390. // //
  391. // Result += VA_ARG (Marker, UINTN);
  392. // }
  393. //
  394. // VA_END (Marker);
  395. // return Result
  396. // }
  397. //
  398. /**
  399. Return the size of argument that has been aligned to sizeof (UINTN).
  400. @param n The parameter size to be aligned.
  401. @return The aligned size.
  402. **/
  403. #define _INT_SIZE_OF(n) ((sizeof (n) + sizeof (UINTN) - 1) &~(sizeof (UINTN) - 1))
  404. #if defined(__CC_ARM)
  405. //
  406. // RVCT ARM variable argument list support.
  407. //
  408. ///
  409. /// Variable used to traverse the list of arguments. This type can vary by
  410. /// implementation and could be an array or structure.
  411. ///
  412. #ifdef __APCS_ADSABI
  413. typedef int *va_list[1];
  414. #define VA_LIST va_list
  415. #else
  416. typedef struct __va_list { void *__ap; } va_list;
  417. #define VA_LIST va_list
  418. #endif
  419. #define VA_START(Marker, Parameter) __va_start(Marker, Parameter)
  420. #define VA_ARG(Marker, TYPE) __va_arg(Marker, TYPE)
  421. #define VA_END(Marker) ((void)0)
  422. // For some ARM RVCT compilers, __va_copy is not defined
  423. #ifndef __va_copy
  424. #define __va_copy(dest, src) ((void)((dest) = (src)))
  425. #endif
  426. #define VA_COPY(Dest, Start) __va_copy (Dest, Start)
  427. #elif defined(__GNUC__) && !defined(NO_BUILTIN_VA_FUNCS)
  428. //
  429. // Use GCC built-in macros for variable argument lists.
  430. //
  431. ///
  432. /// Variable used to traverse the list of arguments. This type can vary by
  433. /// implementation and could be an array or structure.
  434. ///
  435. typedef __builtin_va_list VA_LIST;
  436. #define VA_START(Marker, Parameter) __builtin_va_start (Marker, Parameter)
  437. #define VA_ARG(Marker, TYPE) ((sizeof (TYPE) < sizeof (UINTN)) ? (TYPE)(__builtin_va_arg (Marker, UINTN)) : (TYPE)(__builtin_va_arg (Marker, TYPE)))
  438. #define VA_END(Marker) __builtin_va_end (Marker)
  439. #define VA_COPY(Dest, Start) __builtin_va_copy (Dest, Start)
  440. #else
  441. ///
  442. /// Variable used to traverse the list of arguments. This type can vary by
  443. /// implementation and could be an array or structure.
  444. ///
  445. typedef CHAR8 *VA_LIST;
  446. /**
  447. Retrieves a pointer to the beginning of a variable argument list, based on
  448. the name of the parameter that immediately precedes the variable argument list.
  449. This function initializes Marker to point to the beginning of the variable
  450. argument list that immediately follows Parameter. The method for computing the
  451. pointer to the next argument in the argument list is CPU-specific following the
  452. EFIAPI ABI.
  453. @param Marker The VA_LIST used to traverse the list of arguments.
  454. @param Parameter The name of the parameter that immediately precedes
  455. the variable argument list.
  456. @return A pointer to the beginning of a variable argument list.
  457. **/
  458. #define VA_START(Marker, Parameter) (Marker = (VA_LIST) ((UINTN) & (Parameter) + _INT_SIZE_OF (Parameter)))
  459. /**
  460. Returns an argument of a specified type from a variable argument list and updates
  461. the pointer to the variable argument list to point to the next argument.
  462. This function returns an argument of the type specified by TYPE from the beginning
  463. of the variable argument list specified by Marker. Marker is then updated to point
  464. to the next argument in the variable argument list. The method for computing the
  465. pointer to the next argument in the argument list is CPU-specific following the EFIAPI ABI.
  466. @param Marker VA_LIST used to traverse the list of arguments.
  467. @param TYPE The type of argument to retrieve from the beginning
  468. of the variable argument list.
  469. @return An argument of the type specified by TYPE.
  470. **/
  471. #define VA_ARG(Marker, TYPE) (*(TYPE *) ((Marker += _INT_SIZE_OF (TYPE)) - _INT_SIZE_OF (TYPE)))
  472. /**
  473. Terminates the use of a variable argument list.
  474. This function initializes Marker so it can no longer be used with VA_ARG().
  475. After this macro is used, the only way to access the variable argument list is
  476. by using VA_START() again.
  477. @param Marker VA_LIST used to traverse the list of arguments.
  478. **/
  479. #define VA_END(Marker) (Marker = (VA_LIST) 0)
  480. /**
  481. Initializes a VA_LIST as a copy of an existing VA_LIST.
  482. This macro initializes Dest as a copy of Start, as if the VA_START macro had been applied to Dest
  483. followed by the same sequence of uses of the VA_ARG macro as had previously been used to reach
  484. the present state of Start.
  485. @param Dest VA_LIST used to traverse the list of arguments.
  486. @param Start VA_LIST used to traverse the list of arguments.
  487. **/
  488. #define VA_COPY(Dest, Start) ((void)((Dest) = (Start)))
  489. #endif
  490. ///
  491. /// Pointer to the start of a variable argument list stored in a memory buffer. Same as UINT8 *.
  492. ///
  493. typedef UINTN *BASE_LIST;
  494. /**
  495. Returns the size of a data type in sizeof(UINTN) units rounded up to the nearest UINTN boundary.
  496. @param TYPE The date type to determine the size of.
  497. @return The size of TYPE in sizeof (UINTN) units rounded up to the nearest UINTN boundary.
  498. **/
  499. #define _BASE_INT_SIZE_OF(TYPE) ((sizeof (TYPE) + sizeof (UINTN) - 1) / sizeof (UINTN))
  500. /**
  501. Returns an argument of a specified type from a variable argument list and updates
  502. the pointer to the variable argument list to point to the next argument.
  503. This function returns an argument of the type specified by TYPE from the beginning
  504. of the variable argument list specified by Marker. Marker is then updated to point
  505. to the next argument in the variable argument list. The method for computing the
  506. pointer to the next argument in the argument list is CPU specific following the EFIAPI ABI.
  507. @param Marker The pointer to the beginning of a variable argument list.
  508. @param TYPE The type of argument to retrieve from the beginning
  509. of the variable argument list.
  510. @return An argument of the type specified by TYPE.
  511. **/
  512. #define BASE_ARG(Marker, TYPE) (*(TYPE *) ((Marker += _BASE_INT_SIZE_OF (TYPE)) - _BASE_INT_SIZE_OF (TYPE)))
  513. /**
  514. The macro that returns the byte offset of a field in a data structure.
  515. This function returns the offset, in bytes, of field specified by Field from the
  516. beginning of the data structure specified by TYPE. If TYPE does not contain Field,
  517. the module will not compile.
  518. @param TYPE The name of the data structure that contains the field specified by Field.
  519. @param Field The name of the field in the data structure.
  520. @return Offset, in bytes, of field.
  521. **/
  522. #ifdef __GNUC__
  523. #if __GNUC__ >= 4
  524. #define OFFSET_OF(TYPE, Field) ((UINTN) __builtin_offsetof(TYPE, Field))
  525. #endif
  526. #endif
  527. #ifndef OFFSET_OF
  528. #define OFFSET_OF(TYPE, Field) ((UINTN) &(((TYPE *)0)->Field))
  529. #endif
  530. /**
  531. Macro that returns a pointer to the data structure that contains a specified field of
  532. that data structure. This is a lightweight method to hide information by placing a
  533. public data structure inside a larger private data structure and using a pointer to
  534. the public data structure to retrieve a pointer to the private data structure.
  535. This function computes the offset, in bytes, of field specified by Field from the beginning
  536. of the data structure specified by TYPE. This offset is subtracted from Record, and is
  537. used to return a pointer to a data structure of the type specified by TYPE. If the data type
  538. specified by TYPE does not contain the field specified by Field, then the module will not compile.
  539. @param Record Pointer to the field specified by Field within a data structure of type TYPE.
  540. @param TYPE The name of the data structure type to return. This data structure must
  541. contain the field specified by Field.
  542. @param Field The name of the field in the data structure specified by TYPE to which Record points.
  543. @return A pointer to the structure from one of it's elements.
  544. **/
  545. #define BASE_CR(Record, TYPE, Field) ((TYPE *) ((CHAR8 *) (Record) - (CHAR8 *) &(((TYPE *) 0)->Field)))
  546. /**
  547. Rounds a value up to the next boundary using a specified alignment.
  548. This function rounds Value up to the next boundary using the specified Alignment.
  549. This aligned value is returned.
  550. @param Value The value to round up.
  551. @param Alignment The alignment boundary used to return the aligned value.
  552. @return A value up to the next boundary.
  553. **/
  554. #define ALIGN_VALUE(Value, Alignment) ((Value) + (((Alignment) - (Value)) & ((Alignment) - 1)))
  555. /**
  556. Adjust a pointer by adding the minimum offset required for it to be aligned on
  557. a specified alignment boundary.
  558. This function rounds the pointer specified by Pointer to the next alignment boundary
  559. specified by Alignment. The pointer to the aligned address is returned.
  560. @param Pointer The pointer to round up.
  561. @param Alignment The alignment boundary to use to return an aligned pointer.
  562. @return Pointer to the aligned address.
  563. **/
  564. #define ALIGN_POINTER(Pointer, Alignment) ((VOID *) (ALIGN_VALUE ((UINTN)(Pointer), (Alignment))))
  565. /**
  566. Rounds a value up to the next natural boundary for the current CPU.
  567. This is 4-bytes for 32-bit CPUs and 8-bytes for 64-bit CPUs.
  568. This function rounds the value specified by Value up to the next natural boundary for the
  569. current CPU. This rounded value is returned.
  570. @param Value The value to round up.
  571. @return Rounded value specified by Value.
  572. **/
  573. #define ALIGN_VARIABLE(Value) ALIGN_VALUE ((Value), sizeof (UINTN))
  574. /**
  575. Return the maximum of two operands.
  576. This macro returns the maximum of two operand specified by a and b.
  577. Both a and b must be the same numerical types, signed or unsigned.
  578. @param a The first operand with any numerical type.
  579. @param b The second operand. Can be any numerical type as long as is
  580. the same type as a.
  581. @return Maximum of two operands.
  582. **/
  583. #define MAX(a, b) \
  584. (((a) > (b)) ? (a) : (b))
  585. /**
  586. Return the minimum of two operands.
  587. This macro returns the minimal of two operand specified by a and b.
  588. Both a and b must be the same numerical types, signed or unsigned.
  589. @param a The first operand with any numerical type.
  590. @param b The second operand. It should be the same any numerical type with a.
  591. @return Minimum of two operands.
  592. **/
  593. #define MIN(a, b) \
  594. (((a) < (b)) ? (a) : (b))
  595. /**
  596. Return the absolute value of a signed operand.
  597. This macro returns the absolute value of the signed operand specified by a.
  598. @param a The signed operand.
  599. @return The absolute value of the signed operand.
  600. **/
  601. #define ABS(a) \
  602. (((a) < 0) ? (-(a)) : (a))
  603. //
  604. // Status codes common to all execution phases
  605. //
  606. typedef UINTN RETURN_STATUS;
  607. /**
  608. Produces a RETURN_STATUS code with the highest bit set.
  609. @param StatusCode The status code value to convert into a warning code.
  610. StatusCode must be in the range 0x00000000..0x7FFFFFFF.
  611. @return The value specified by StatusCode with the highest bit set.
  612. **/
  613. #define ENCODE_ERROR(StatusCode) ((RETURN_STATUS)(MAX_BIT | (StatusCode)))
  614. /**
  615. Produces a RETURN_STATUS code with the highest bit clear.
  616. @param StatusCode The status code value to convert into a warning code.
  617. StatusCode must be in the range 0x00000000..0x7FFFFFFF.
  618. @return The value specified by StatusCode with the highest bit clear.
  619. **/
  620. #define ENCODE_WARNING(StatusCode) ((RETURN_STATUS)(StatusCode))
  621. /**
  622. Returns TRUE if a specified RETURN_STATUS code is an error code.
  623. This function returns TRUE if StatusCode has the high bit set. Otherwise, FALSE is returned.
  624. @param StatusCode The status code value to evaluate.
  625. @retval TRUE The high bit of StatusCode is set.
  626. @retval FALSE The high bit of StatusCode is clear.
  627. **/
  628. #define RETURN_ERROR(StatusCode) (((INTN)(RETURN_STATUS)(StatusCode)) < 0)
  629. ///
  630. /// The operation completed successfully.
  631. ///
  632. #define RETURN_SUCCESS 0
  633. ///
  634. /// The image failed to load.
  635. ///
  636. #define RETURN_LOAD_ERROR ENCODE_ERROR (1)
  637. ///
  638. /// The parameter was incorrect.
  639. ///
  640. #define RETURN_INVALID_PARAMETER ENCODE_ERROR (2)
  641. ///
  642. /// The operation is not supported.
  643. ///
  644. #define RETURN_UNSUPPORTED ENCODE_ERROR (3)
  645. ///
  646. /// The buffer was not the proper size for the request.
  647. ///
  648. #define RETURN_BAD_BUFFER_SIZE ENCODE_ERROR (4)
  649. ///
  650. /// The buffer was not large enough to hold the requested data.
  651. /// The required buffer size is returned in the appropriate
  652. /// parameter when this error occurs.
  653. ///
  654. #define RETURN_BUFFER_TOO_SMALL ENCODE_ERROR (5)
  655. ///
  656. /// There is no data pending upon return.
  657. ///
  658. #define RETURN_NOT_READY ENCODE_ERROR (6)
  659. ///
  660. /// The physical device reported an error while attempting the
  661. /// operation.
  662. ///
  663. #define RETURN_DEVICE_ERROR ENCODE_ERROR (7)
  664. ///
  665. /// The device can not be written to.
  666. ///
  667. #define RETURN_WRITE_PROTECTED ENCODE_ERROR (8)
  668. ///
  669. /// The resource has run out.
  670. ///
  671. #define RETURN_OUT_OF_RESOURCES ENCODE_ERROR (9)
  672. ///
  673. /// An inconsistency was detected on the file system causing the
  674. /// operation to fail.
  675. ///
  676. #define RETURN_VOLUME_CORRUPTED ENCODE_ERROR (10)
  677. ///
  678. /// There is no more space on the file system.
  679. ///
  680. #define RETURN_VOLUME_FULL ENCODE_ERROR (11)
  681. ///
  682. /// The device does not contain any medium to perform the
  683. /// operation.
  684. ///
  685. #define RETURN_NO_MEDIA ENCODE_ERROR (12)
  686. ///
  687. /// The medium in the device has changed since the last
  688. /// access.
  689. ///
  690. #define RETURN_MEDIA_CHANGED ENCODE_ERROR (13)
  691. ///
  692. /// The item was not found.
  693. ///
  694. #define RETURN_NOT_FOUND ENCODE_ERROR (14)
  695. ///
  696. /// Access was denied.
  697. ///
  698. #define RETURN_ACCESS_DENIED ENCODE_ERROR (15)
  699. ///
  700. /// The server was not found or did not respond to the request.
  701. ///
  702. #define RETURN_NO_RESPONSE ENCODE_ERROR (16)
  703. ///
  704. /// A mapping to the device does not exist.
  705. ///
  706. #define RETURN_NO_MAPPING ENCODE_ERROR (17)
  707. ///
  708. /// A timeout time expired.
  709. ///
  710. #define RETURN_TIMEOUT ENCODE_ERROR (18)
  711. ///
  712. /// The protocol has not been started.
  713. ///
  714. #define RETURN_NOT_STARTED ENCODE_ERROR (19)
  715. ///
  716. /// The protocol has already been started.
  717. ///
  718. #define RETURN_ALREADY_STARTED ENCODE_ERROR (20)
  719. ///
  720. /// The operation was aborted.
  721. ///
  722. #define RETURN_ABORTED ENCODE_ERROR (21)
  723. ///
  724. /// An ICMP error occurred during the network operation.
  725. ///
  726. #define RETURN_ICMP_ERROR ENCODE_ERROR (22)
  727. ///
  728. /// A TFTP error occurred during the network operation.
  729. ///
  730. #define RETURN_TFTP_ERROR ENCODE_ERROR (23)
  731. ///
  732. /// A protocol error occurred during the network operation.
  733. ///
  734. #define RETURN_PROTOCOL_ERROR ENCODE_ERROR (24)
  735. ///
  736. /// A function encountered an internal version that was
  737. /// incompatible with a version requested by the caller.
  738. ///
  739. #define RETURN_INCOMPATIBLE_VERSION ENCODE_ERROR (25)
  740. ///
  741. /// The function was not performed due to a security violation.
  742. ///
  743. #define RETURN_SECURITY_VIOLATION ENCODE_ERROR (26)
  744. ///
  745. /// A CRC error was detected.
  746. ///
  747. #define RETURN_CRC_ERROR ENCODE_ERROR (27)
  748. ///
  749. /// The beginning or end of media was reached.
  750. ///
  751. #define RETURN_END_OF_MEDIA ENCODE_ERROR (28)
  752. ///
  753. /// The end of the file was reached.
  754. ///
  755. #define RETURN_END_OF_FILE ENCODE_ERROR (31)
  756. ///
  757. /// The language specified was invalid.
  758. ///
  759. #define RETURN_INVALID_LANGUAGE ENCODE_ERROR (32)
  760. ///
  761. /// The security status of the data is unknown or compromised
  762. /// and the data must be updated or replaced to restore a valid
  763. /// security status.
  764. ///
  765. #define RETURN_COMPROMISED_DATA ENCODE_ERROR (33)
  766. ///
  767. /// The string contained one or more characters that
  768. /// the device could not render and were skipped.
  769. ///
  770. #define RETURN_WARN_UNKNOWN_GLYPH ENCODE_WARNING (1)
  771. ///
  772. /// The handle was closed, but the file was not deleted.
  773. ///
  774. #define RETURN_WARN_DELETE_FAILURE ENCODE_WARNING (2)
  775. ///
  776. /// The handle was closed, but the data to the file was not
  777. /// flushed properly.
  778. ///
  779. #define RETURN_WARN_WRITE_FAILURE ENCODE_WARNING (3)
  780. ///
  781. /// The resulting buffer was too small, and the data was
  782. /// truncated to the buffer size.
  783. ///
  784. #define RETURN_WARN_BUFFER_TOO_SMALL ENCODE_WARNING (4)
  785. ///
  786. /// The data has not been updated within the timeframe set by
  787. /// local policy for this type of data.
  788. ///
  789. #define RETURN_WARN_STALE_DATA ENCODE_WARNING (5)
  790. /**
  791. Returns a 16-bit signature built from 2 ASCII characters.
  792. This macro returns a 16-bit value built from the two ASCII characters specified
  793. by A and B.
  794. @param A The first ASCII character.
  795. @param B The second ASCII character.
  796. @return A 16-bit value built from the two ASCII characters specified by A and B.
  797. **/
  798. #define SIGNATURE_16(A, B) ((A) | (B << 8))
  799. /**
  800. Returns a 32-bit signature built from 4 ASCII characters.
  801. This macro returns a 32-bit value built from the four ASCII characters specified
  802. by A, B, C, and D.
  803. @param A The first ASCII character.
  804. @param B The second ASCII character.
  805. @param C The third ASCII character.
  806. @param D The fourth ASCII character.
  807. @return A 32-bit value built from the two ASCII characters specified by A, B,
  808. C and D.
  809. **/
  810. #define SIGNATURE_32(A, B, C, D) (SIGNATURE_16 (A, B) | (SIGNATURE_16 (C, D) << 16))
  811. /**
  812. Returns a 64-bit signature built from 8 ASCII characters.
  813. This macro returns a 64-bit value built from the eight ASCII characters specified
  814. by A, B, C, D, E, F, G,and H.
  815. @param A The first ASCII character.
  816. @param B The second ASCII character.
  817. @param C The third ASCII character.
  818. @param D The fourth ASCII character.
  819. @param E The fifth ASCII character.
  820. @param F The sixth ASCII character.
  821. @param G The seventh ASCII character.
  822. @param H The eighth ASCII character.
  823. @return A 64-bit value built from the two ASCII characters specified by A, B,
  824. C, D, E, F, G and H.
  825. **/
  826. #define SIGNATURE_64(A, B, C, D, E, F, G, H) \
  827. (SIGNATURE_32 (A, B, C, D) | ((UINT64) (SIGNATURE_32 (E, F, G, H)) << 32))
  828. #endif