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 36KB

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