/** @file1Root include file for Mde Package Base type modules23This is the include file for any module of type base. Base modules only use4types defined via this include file and can be ported easily to any5environment. There are a set of base libraries in the Mde Package that can6be used to implement base modules.78Copyright (c) 2006 - 2021, Intel Corporation. All rights reserved.<BR>9Portions copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>10SPDX-License-Identifier: BSD-2-Clause-Patent1112**/1314#ifndef __BASE_H__15#define __BASE_H__1617//18// Include processor specific binding19//20#include <ProcessorBind.h>2122#if defined (_MSC_EXTENSIONS)23//24// Disable warning when last field of data structure is a zero sized array.25//26#pragma warning ( disable : 4200 )27#endif2829//30// The Microsoft* C compiler can removed references to unreferenced data items31// if the /OPT:REF linker option is used. We defined a macro as this is a32// a non standard extension33//34#if defined (_MSC_VER) && _MSC_VER < 1800 && !defined (MDE_CPU_EBC)35///36/// Remove global variable from the linked image if there are no references to37/// it after all compiler and linker optimizations have been performed.38///39///40#define GLOBAL_REMOVE_IF_UNREFERENCED __declspec(selectany)41#else42///43/// Remove the global variable from the linked image if there are no references44/// to it after all compiler and linker optimizations have been performed.45///46///47#define GLOBAL_REMOVE_IF_UNREFERENCED48#endif4950//51// Should be used in combination with NORETURN to avoid 'noreturn' returns52// warnings.53//54#ifndef UNREACHABLE55#ifdef __GNUC__56///57/// Signal compilers and analyzers that this call is not reachable. It is58/// up to the compiler to remove any code past that point.59///60#define UNREACHABLE() __builtin_unreachable ()61#elif defined (__has_builtin) && defined (__has_feature)62#if __has_builtin (__builtin_unreachable)63///64/// Signal compilers and analyzers that this call is not reachable. It is65/// up to the compiler to remove any code past that point.66///67#define UNREACHABLE() __builtin_unreachable ()68#endif69#endif7071#ifndef UNREACHABLE72///73/// Signal compilers and analyzers that this call is not reachable. It is74/// up to the compiler to remove any code past that point.75///76#define UNREACHABLE()77#endif78#endif7980//81// Signaling compilers and analyzers that a certain function cannot return may82// remove all following code and thus lead to better optimization and less83// false positives.84//85#ifndef NORETURN86#if defined (__GNUC__) || defined (__clang__)87///88/// Signal compilers and analyzers that the function cannot return.89/// It is up to the compiler to remove any code past a call to functions90/// flagged with this attribute.91///92#define NORETURN __attribute__((noreturn))93#elif defined (_MSC_EXTENSIONS) && !defined (MDE_CPU_EBC)94///95/// Signal compilers and analyzers that the function cannot return.96/// It is up to the compiler to remove any code past a call to functions97/// flagged with this attribute.98///99#define NORETURN __declspec(noreturn)100#else101///102/// Signal compilers and analyzers that the function cannot return.103/// It is up to the compiler to remove any code past a call to functions104/// flagged with this attribute.105///106#define NORETURN107#endif108#endif109110//111// Should be used in combination with ANALYZER_NORETURN to avoid 'noreturn'112// returns warnings.113//114#ifndef ANALYZER_UNREACHABLE115#ifdef __clang_analyzer__116#if __has_builtin (__builtin_unreachable)117///118/// Signal the analyzer that this call is not reachable.119/// This excludes compilers.120///121#define ANALYZER_UNREACHABLE() __builtin_unreachable ()122#endif123#endif124125#ifndef ANALYZER_UNREACHABLE126///127/// Signal the analyzer that this call is not reachable.128/// This excludes compilers.129///130#define ANALYZER_UNREACHABLE()131#endif132#endif133134//135// Static Analyzers may issue errors about potential NULL-dereferences when136// dereferencing a pointer, that has been checked before, outside of a137// NULL-check. This may lead to false positives, such as when using ASSERT()138// for verification.139//140#ifndef ANALYZER_NORETURN141#ifdef __has_feature142#if __has_feature (attribute_analyzer_noreturn)143///144/// Signal analyzers that the function cannot return.145/// This excludes compilers.146///147#define ANALYZER_NORETURN __attribute__((analyzer_noreturn))148#endif149#endif150151#ifndef ANALYZER_NORETURN152///153/// Signal the analyzer that the function cannot return.154/// This excludes compilers.155///156#define ANALYZER_NORETURN157#endif158#endif159160///161/// Tell the code optimizer that the function will return twice.162/// This prevents wrong optimizations which can cause bugs.163///164#ifndef RETURNS_TWICE165#if defined (__GNUC__) || defined (__clang__)166///167/// Tell the code optimizer that the function will return twice.168/// This prevents wrong optimizations which can cause bugs.169///170#define RETURNS_TWICE __attribute__((returns_twice))171#else172///173/// Tell the code optimizer that the function will return twice.174/// This prevents wrong optimizations which can cause bugs.175///176#define RETURNS_TWICE177#endif178#endif179180//181// For symbol name in assembly code, an extra "_" is sometimes necessary182//183184///185/// Private worker functions for ASM_PFX()186///187#define _CONCATENATE(a, b) __CONCATENATE(a, b)188#define __CONCATENATE(a, b) a ## b189190///191/// The __USER_LABEL_PREFIX__ macro predefined by GNUC represents the prefix192/// on symbols in assembly language.193///194#define ASM_PFX(name) _CONCATENATE (__USER_LABEL_PREFIX__, name)195196#ifdef __APPLE__197//198// Apple extension that is used by the linker to optimize code size199// with assembly functions. Put at the end of your .S files200//201#define ASM_FUNCTION_REMOVE_IF_UNREFERENCED .subsections_via_symbols202#else203#define ASM_FUNCTION_REMOVE_IF_UNREFERENCED204#endif205206#define PACKED207208///209/// 128 bit buffer containing a unique identifier value.210/// Unless otherwise specified, aligned on a 64 bit boundary.211///212typedef struct {213UINT32 Data1;214UINT16 Data2;215UINT16 Data3;216UINT8 Data4[8];217} GUID;218219///220/// 4-byte buffer. An IPv4 internet protocol address.221///222typedef struct {223UINT8 Addr[4];224} IPv4_ADDRESS;225226///227/// 16-byte buffer. An IPv6 internet protocol address.228///229typedef struct {230UINT8 Addr[16];231} IPv6_ADDRESS;232233//234// 8-bytes unsigned value that represents a physical system address.235//236typedef UINT64 PHYSICAL_ADDRESS;237238///239/// LIST_ENTRY structure definition.240///241typedef struct _LIST_ENTRY LIST_ENTRY;242243///244/// _LIST_ENTRY structure definition.245///246struct _LIST_ENTRY {247LIST_ENTRY *ForwardLink;248LIST_ENTRY *BackLink;249};250251//252// Modifiers to abstract standard types to aid in debug of problems253//254255///256/// Datum is read-only.257///258#define CONST const259260///261/// Datum is scoped to the current file or function.262///263#define STATIC static264265///266/// Undeclared type.267///268#define VOID void269270//271// Modifiers for Data Types used to self document code.272// This concept is borrowed for UEFI specification.273//274275///276/// Datum is passed to the function.277///278#define IN279280///281/// Datum is returned from the function.282///283#define OUT284285///286/// Passing the datum to the function is optional, and a NULL287/// is passed if the value is not supplied.288///289#define OPTIONAL290291//292// UEFI specification claims 1 and 0. We are concerned about the293// compiler portability so we did it this way.294//295296///297/// Boolean true value. UEFI Specification defines this value to be 1,298/// but this form is more portable.299///300#define TRUE ((BOOLEAN)(1==1))301302///303/// Boolean false value. UEFI Specification defines this value to be 0,304/// but this form is more portable.305///306#define FALSE ((BOOLEAN)(0==1))307308///309/// NULL pointer (VOID *)310///311#if defined (__cplusplus)312#if defined (_MSC_EXTENSIONS)313#define NULL nullptr314#else315#define NULL __null316#endif317#else318#define NULL ((VOID *) 0)319#endif320321//322// Null character323//324#define CHAR_NULL 0x0000325326///327/// Maximum values for common UEFI Data Types328///329#define MAX_INT8 ((INT8)0x7F)330#define MAX_UINT8 ((UINT8)0xFF)331#define MAX_INT16 ((INT16)0x7FFF)332#define MAX_UINT16 ((UINT16)0xFFFF)333#define MAX_INT32 ((INT32)0x7FFFFFFF)334#define MAX_UINT32 ((UINT32)0xFFFFFFFF)335#define MAX_INT64 ((INT64)0x7FFFFFFFFFFFFFFFULL)336#define MAX_UINT64 ((UINT64)0xFFFFFFFFFFFFFFFFULL)337338///339/// Minimum values for the signed UEFI Data Types340///341#define MIN_INT8 (((INT8) -127) - 1)342#define MIN_INT16 (((INT16) -32767) - 1)343#define MIN_INT32 (((INT32) -2147483647) - 1)344#define MIN_INT64 (((INT64) -9223372036854775807LL) - 1)345346#define BIT0 0x00000001347#define BIT1 0x00000002348#define BIT2 0x00000004349#define BIT3 0x00000008350#define BIT4 0x00000010351#define BIT5 0x00000020352#define BIT6 0x00000040353#define BIT7 0x00000080354#define BIT8 0x00000100355#define BIT9 0x00000200356#define BIT10 0x00000400357#define BIT11 0x00000800358#define BIT12 0x00001000359#define BIT13 0x00002000360#define BIT14 0x00004000361#define BIT15 0x00008000362#define BIT16 0x00010000363#define BIT17 0x00020000364#define BIT18 0x00040000365#define BIT19 0x00080000366#define BIT20 0x00100000367#define BIT21 0x00200000368#define BIT22 0x00400000369#define BIT23 0x00800000370#define BIT24 0x01000000371#define BIT25 0x02000000372#define BIT26 0x04000000373#define BIT27 0x08000000374#define BIT28 0x10000000375#define BIT29 0x20000000376#define BIT30 0x40000000377#define BIT31 0x80000000378#define BIT32 0x0000000100000000ULL379#define BIT33 0x0000000200000000ULL380#define BIT34 0x0000000400000000ULL381#define BIT35 0x0000000800000000ULL382#define BIT36 0x0000001000000000ULL383#define BIT37 0x0000002000000000ULL384#define BIT38 0x0000004000000000ULL385#define BIT39 0x0000008000000000ULL386#define BIT40 0x0000010000000000ULL387#define BIT41 0x0000020000000000ULL388#define BIT42 0x0000040000000000ULL389#define BIT43 0x0000080000000000ULL390#define BIT44 0x0000100000000000ULL391#define BIT45 0x0000200000000000ULL392#define BIT46 0x0000400000000000ULL393#define BIT47 0x0000800000000000ULL394#define BIT48 0x0001000000000000ULL395#define BIT49 0x0002000000000000ULL396#define BIT50 0x0004000000000000ULL397#define BIT51 0x0008000000000000ULL398#define BIT52 0x0010000000000000ULL399#define BIT53 0x0020000000000000ULL400#define BIT54 0x0040000000000000ULL401#define BIT55 0x0080000000000000ULL402#define BIT56 0x0100000000000000ULL403#define BIT57 0x0200000000000000ULL404#define BIT58 0x0400000000000000ULL405#define BIT59 0x0800000000000000ULL406#define BIT60 0x1000000000000000ULL407#define BIT61 0x2000000000000000ULL408#define BIT62 0x4000000000000000ULL409#define BIT63 0x8000000000000000ULL410411#define SIZE_1KB 0x00000400412#define SIZE_2KB 0x00000800413#define SIZE_4KB 0x00001000414#define SIZE_8KB 0x00002000415#define SIZE_16KB 0x00004000416#define SIZE_32KB 0x00008000417#define SIZE_64KB 0x00010000418#define SIZE_128KB 0x00020000419#define SIZE_256KB 0x00040000420#define SIZE_512KB 0x00080000421#define SIZE_1MB 0x00100000422#define SIZE_2MB 0x00200000423#define SIZE_4MB 0x00400000424#define SIZE_8MB 0x00800000425#define SIZE_16MB 0x01000000426#define SIZE_32MB 0x02000000427#define SIZE_64MB 0x04000000428#define SIZE_128MB 0x08000000429#define SIZE_256MB 0x10000000430#define SIZE_512MB 0x20000000431#define SIZE_1GB 0x40000000432#define SIZE_2GB 0x80000000433#define SIZE_4GB 0x0000000100000000ULL434#define SIZE_8GB 0x0000000200000000ULL435#define SIZE_16GB 0x0000000400000000ULL436#define SIZE_32GB 0x0000000800000000ULL437#define SIZE_64GB 0x0000001000000000ULL438#define SIZE_128GB 0x0000002000000000ULL439#define SIZE_256GB 0x0000004000000000ULL440#define SIZE_512GB 0x0000008000000000ULL441#define SIZE_1TB 0x0000010000000000ULL442#define SIZE_2TB 0x0000020000000000ULL443#define SIZE_4TB 0x0000040000000000ULL444#define SIZE_8TB 0x0000080000000000ULL445#define SIZE_16TB 0x0000100000000000ULL446#define SIZE_32TB 0x0000200000000000ULL447#define SIZE_64TB 0x0000400000000000ULL448#define SIZE_128TB 0x0000800000000000ULL449#define SIZE_256TB 0x0001000000000000ULL450#define SIZE_512TB 0x0002000000000000ULL451#define SIZE_1PB 0x0004000000000000ULL452#define SIZE_2PB 0x0008000000000000ULL453#define SIZE_4PB 0x0010000000000000ULL454#define SIZE_8PB 0x0020000000000000ULL455#define SIZE_16PB 0x0040000000000000ULL456#define SIZE_32PB 0x0080000000000000ULL457#define SIZE_64PB 0x0100000000000000ULL458#define SIZE_128PB 0x0200000000000000ULL459#define SIZE_256PB 0x0400000000000000ULL460#define SIZE_512PB 0x0800000000000000ULL461#define SIZE_1EB 0x1000000000000000ULL462#define SIZE_2EB 0x2000000000000000ULL463#define SIZE_4EB 0x4000000000000000ULL464#define SIZE_8EB 0x8000000000000000ULL465466#define BASE_1KB 0x00000400467#define BASE_2KB 0x00000800468#define BASE_4KB 0x00001000469#define BASE_8KB 0x00002000470#define BASE_16KB 0x00004000471#define BASE_32KB 0x00008000472#define BASE_64KB 0x00010000473#define BASE_128KB 0x00020000474#define BASE_256KB 0x00040000475#define BASE_512KB 0x00080000476#define BASE_1MB 0x00100000477#define BASE_2MB 0x00200000478#define BASE_4MB 0x00400000479#define BASE_8MB 0x00800000480#define BASE_16MB 0x01000000481#define BASE_32MB 0x02000000482#define BASE_64MB 0x04000000483#define BASE_128MB 0x08000000484#define BASE_256MB 0x10000000485#define BASE_512MB 0x20000000486#define BASE_1GB 0x40000000487#define BASE_2GB 0x80000000488#define BASE_4GB 0x0000000100000000ULL489#define BASE_8GB 0x0000000200000000ULL490#define BASE_16GB 0x0000000400000000ULL491#define BASE_32GB 0x0000000800000000ULL492#define BASE_64GB 0x0000001000000000ULL493#define BASE_128GB 0x0000002000000000ULL494#define BASE_256GB 0x0000004000000000ULL495#define BASE_512GB 0x0000008000000000ULL496#define BASE_1TB 0x0000010000000000ULL497#define BASE_2TB 0x0000020000000000ULL498#define BASE_4TB 0x0000040000000000ULL499#define BASE_8TB 0x0000080000000000ULL500#define BASE_16TB 0x0000100000000000ULL501#define BASE_32TB 0x0000200000000000ULL502#define BASE_64TB 0x0000400000000000ULL503#define BASE_128TB 0x0000800000000000ULL504#define BASE_256TB 0x0001000000000000ULL505#define BASE_512TB 0x0002000000000000ULL506#define BASE_1PB 0x0004000000000000ULL507#define BASE_2PB 0x0008000000000000ULL508#define BASE_4PB 0x0010000000000000ULL509#define BASE_8PB 0x0020000000000000ULL510#define BASE_16PB 0x0040000000000000ULL511#define BASE_32PB 0x0080000000000000ULL512#define BASE_64PB 0x0100000000000000ULL513#define BASE_128PB 0x0200000000000000ULL514#define BASE_256PB 0x0400000000000000ULL515#define BASE_512PB 0x0800000000000000ULL516#define BASE_1EB 0x1000000000000000ULL517#define BASE_2EB 0x2000000000000000ULL518#define BASE_4EB 0x4000000000000000ULL519#define BASE_8EB 0x8000000000000000ULL520521//522// Support for variable argument lists in freestanding edk2 modules.523//524// For modules that use the ISO C library interfaces for variable525// argument lists, refer to "StdLib/Include/stdarg.h".526//527// VA_LIST - typedef for argument list.528// VA_START (VA_LIST Marker, argument before the ...) - Init Marker for use.529// VA_END (VA_LIST Marker) - Clear Marker530// VA_ARG (VA_LIST Marker, var arg type) - Use Marker to get an argument from531// the ... list. You must know the type and pass it in this macro. Type532// must be compatible with the type of the actual next argument (as promoted533// according to the default argument promotions.)534// VA_COPY (VA_LIST Dest, VA_LIST Start) - Initialize Dest as a copy of Start.535//536// Example:537//538// UINTN539// EFIAPI540// ExampleVarArg (541// IN UINTN NumberOfArgs,542// ...543// )544// {545// VA_LIST Marker;546// UINTN Index;547// UINTN Result;548//549// //550// // Initialize the Marker551// //552// VA_START (Marker, NumberOfArgs);553// for (Index = 0, Result = 0; Index < NumberOfArgs; Index++) {554// //555// // The ... list is a series of UINTN values, so sum them up.556// //557// Result += VA_ARG (Marker, UINTN);558// }559//560// VA_END (Marker);561// return Result;562// }563//564// Notes:565// - Functions that call VA_START() / VA_END() must have a variable566// argument list and must be declared EFIAPI.567// - Functions that call VA_COPY() / VA_END() must be declared EFIAPI.568// - Functions that only use VA_LIST and VA_ARG() need not be EFIAPI.569//570571/**572Return the size of argument that has been aligned to sizeof (UINTN).573574@param n The parameter size to be aligned.575576@return The aligned size.577**/578#define _INT_SIZE_OF(n) ((sizeof (n) + sizeof (UINTN) - 1) &~(sizeof (UINTN) - 1))579580#if defined (_M_ARM) || defined (_M_ARM64)581//582// MSFT ARM variable argument list support.583//584585typedef char *VA_LIST;586587#define VA_START(Marker, Parameter) __va_start (&Marker, &Parameter, _INT_SIZE_OF (Parameter), __alignof(Parameter), &Parameter)588#define VA_ARG(Marker, TYPE) (*(TYPE *) ((Marker += _INT_SIZE_OF (TYPE) + ((-(INTN)Marker) & (sizeof(TYPE) - 1))) - _INT_SIZE_OF (TYPE)))589#define VA_END(Marker) (Marker = (VA_LIST) 0)590#define VA_COPY(Dest, Start) ((void)((Dest) = (Start)))591592#elif defined (__GNUC__) || defined (__clang__)593594#if defined (MDE_CPU_X64) && !defined (NO_MSABI_VA_FUNCS)595//596// X64 only. Use MS ABI version of GCC built-in macros for variable argument lists.597//598///599/// Both GCC and LLVM 3.8 for X64 support new variable argument intrinsics for Microsoft ABI600///601602///603/// Variable used to traverse the list of arguments. This type can vary by604/// implementation and could be an array or structure.605///606typedef __builtin_ms_va_list VA_LIST;607608#define VA_START(Marker, Parameter) __builtin_ms_va_start (Marker, Parameter)609610#define VA_ARG(Marker, TYPE) ((sizeof (TYPE) < sizeof (UINTN)) ? (TYPE)(__builtin_va_arg (Marker, UINTN)) : (TYPE)(__builtin_va_arg (Marker, TYPE)))611612#define VA_END(Marker) __builtin_ms_va_end (Marker)613614#define VA_COPY(Dest, Start) __builtin_ms_va_copy (Dest, Start)615616#else617//618// Use GCC built-in macros for variable argument lists.619//620621///622/// Variable used to traverse the list of arguments. This type can vary by623/// implementation and could be an array or structure.624///625typedef __builtin_va_list VA_LIST;626627#define VA_START(Marker, Parameter) __builtin_va_start (Marker, Parameter)628629#define VA_ARG(Marker, TYPE) ((sizeof (TYPE) < sizeof (UINTN)) ? (TYPE)(__builtin_va_arg (Marker, UINTN)) : (TYPE)(__builtin_va_arg (Marker, TYPE)))630631#define VA_END(Marker) __builtin_va_end (Marker)632633#define VA_COPY(Dest, Start) __builtin_va_copy (Dest, Start)634635#endif636637#else638///639/// Variable used to traverse the list of arguments. This type can vary by640/// implementation and could be an array or structure.641///642typedef CHAR8 *VA_LIST;643644/**645Retrieves a pointer to the beginning of a variable argument list, based on646the name of the parameter that immediately precedes the variable argument list.647648This function initializes Marker to point to the beginning of the variable649argument list that immediately follows Parameter. The method for computing the650pointer to the next argument in the argument list is CPU-specific following the651EFIAPI ABI.652653@param Marker The VA_LIST used to traverse the list of arguments.654@param Parameter The name of the parameter that immediately precedes655the variable argument list.656657@return A pointer to the beginning of a variable argument list.658659**/660#define VA_START(Marker, Parameter) (Marker = (VA_LIST) ((UINTN) & (Parameter) + _INT_SIZE_OF (Parameter)))661662/**663Returns an argument of a specified type from a variable argument list and updates664the pointer to the variable argument list to point to the next argument.665666This function returns an argument of the type specified by TYPE from the beginning667of the variable argument list specified by Marker. Marker is then updated to point668to the next argument in the variable argument list. The method for computing the669pointer to the next argument in the argument list is CPU-specific following the EFIAPI ABI.670671@param Marker VA_LIST used to traverse the list of arguments.672@param TYPE The type of argument to retrieve from the beginning673of the variable argument list.674675@return An argument of the type specified by TYPE.676677**/678#define VA_ARG(Marker, TYPE) (*(TYPE *) ((Marker += _INT_SIZE_OF (TYPE)) - _INT_SIZE_OF (TYPE)))679680/**681Terminates the use of a variable argument list.682683This function initializes Marker so it can no longer be used with VA_ARG().684After this macro is used, the only way to access the variable argument list is685by using VA_START() again.686687@param Marker VA_LIST used to traverse the list of arguments.688689**/690#define VA_END(Marker) (Marker = (VA_LIST) 0)691692/**693Initializes a VA_LIST as a copy of an existing VA_LIST.694695This macro initializes Dest as a copy of Start, as if the VA_START macro had been applied to Dest696followed by the same sequence of uses of the VA_ARG macro as had previously been used to reach697the present state of Start.698699@param Dest VA_LIST used to traverse the list of arguments.700@param Start VA_LIST used to traverse the list of arguments.701702**/703#define VA_COPY(Dest, Start) ((void)((Dest) = (Start)))704705#endif706707///708/// Pointer to the start of a variable argument list stored in a memory buffer. Same as UINT8 *.709///710typedef UINTN *BASE_LIST;711712/**713Returns the size of a data type in sizeof(UINTN) units rounded up to the nearest UINTN boundary.714715@param TYPE The date type to determine the size of.716717@return The size of TYPE in sizeof (UINTN) units rounded up to the nearest UINTN boundary.718**/719#define _BASE_INT_SIZE_OF(TYPE) ((sizeof (TYPE) + sizeof (UINTN) - 1) / sizeof (UINTN))720721/**722Returns an argument of a specified type from a variable argument list and updates723the pointer to the variable argument list to point to the next argument.724725This function returns an argument of the type specified by TYPE from the beginning726of the variable argument list specified by Marker. Marker is then updated to point727to the next argument in the variable argument list. The method for computing the728pointer to the next argument in the argument list is CPU specific following the EFIAPI ABI.729730@param Marker The pointer to the beginning of a variable argument list.731@param TYPE The type of argument to retrieve from the beginning732of the variable argument list.733734@return An argument of the type specified by TYPE.735736**/737#define BASE_ARG(Marker, TYPE) (*(TYPE *) ((Marker += _BASE_INT_SIZE_OF (TYPE)) - _BASE_INT_SIZE_OF (TYPE)))738739/**740The macro that returns the byte offset of a field in a data structure.741742This function returns the offset, in bytes, of field specified by Field from the743beginning of the data structure specified by TYPE. If TYPE does not contain Field,744the module will not compile.745746@param TYPE The name of the data structure that contains the field specified by Field.747@param Field The name of the field in the data structure.748749@return Offset, in bytes, of field.750751**/752#if (defined (__GNUC__) && __GNUC__ >= 4) || defined (__clang__)753#define OFFSET_OF(TYPE, Field) ((UINTN) __builtin_offsetof(TYPE, Field))754#endif755756#ifndef OFFSET_OF757#define OFFSET_OF(TYPE, Field) ((UINTN) &(((TYPE *)0)->Field))758#endif759760/**761Returns the alignment requirement of a type.762763@param TYPE The name of the type to retrieve the alignment requirement of.764765@return Alignment requirement, in Bytes, of TYPE.766**/767#if defined (__cplusplus)768//769// Standard C++ operator.770//771#define ALIGNOF(TYPE) alignof (TYPE)772#elif defined (__GNUC__) || defined (__clang__) || (defined (_MSC_VER) && _MSC_VER >= 1900)773//774// All supported versions of GCC and Clang, as well as MSVC 2015 and later,775// support the standard operator _Alignof.776//777#define ALIGNOF(TYPE) _Alignof (TYPE)778#elif defined (_MSC_EXTENSIONS)779//780// Earlier versions of MSVC, at least MSVC 2008 and later, support the vendor781// extension __alignof.782//783#define ALIGNOF(TYPE) __alignof (TYPE)784#else785//786// For compilers that do not support inbuilt alignof operators, use OFFSET_OF.787// CHAR8 is known to have both a size and an alignment requirement of 1 Byte.788// As such, A must be located exactly at the offset equal to its alignment789// requirement.790//791#define ALIGNOF(TYPE) OFFSET_OF (struct { CHAR8 C; TYPE A; }, A)792#endif793794/**795Portable definition for compile time assertions.796Equivalent to C11 static_assert macro from assert.h.797798@param Expression Boolean expression.799@param Message Raised compiler diagnostic message when expression is false.800801**/802#if defined (__cplusplus)803#define STATIC_ASSERT static_assert804#elif defined (__GNUC__) || defined (__clang__)805#define STATIC_ASSERT _Static_assert806#elif defined (_MSC_EXTENSIONS)807#define STATIC_ASSERT static_assert808#endif809810//811// Verify that ProcessorBind.h produced UEFI Data Types that are compliant with812// Section 2.3.1 of the UEFI 2.3 Specification.813//814815STATIC_ASSERT (sizeof (BOOLEAN) == 1, "sizeof (BOOLEAN) does not meet UEFI Specification Data Type requirements");816STATIC_ASSERT (sizeof (INT8) == 1, "sizeof (INT8) does not meet UEFI Specification Data Type requirements");817STATIC_ASSERT (sizeof (UINT8) == 1, "sizeof (UINT8) does not meet UEFI Specification Data Type requirements");818STATIC_ASSERT (sizeof (INT16) == 2, "sizeof (INT16) does not meet UEFI Specification Data Type requirements");819STATIC_ASSERT (sizeof (UINT16) == 2, "sizeof (UINT16) does not meet UEFI Specification Data Type requirements");820STATIC_ASSERT (sizeof (INT32) == 4, "sizeof (INT32) does not meet UEFI Specification Data Type requirements");821STATIC_ASSERT (sizeof (UINT32) == 4, "sizeof (UINT32) does not meet UEFI Specification Data Type requirements");822STATIC_ASSERT (sizeof (INT64) == 8, "sizeof (INT64) does not meet UEFI Specification Data Type requirements");823STATIC_ASSERT (sizeof (UINT64) == 8, "sizeof (UINT64) does not meet UEFI Specification Data Type requirements");824STATIC_ASSERT (sizeof (CHAR8) == 1, "sizeof (CHAR8) does not meet UEFI Specification Data Type requirements");825STATIC_ASSERT (sizeof (CHAR16) == 2, "sizeof (CHAR16) does not meet UEFI Specification Data Type requirements");826/*827* FreeBSD uses these headers in userland wher the following two assertions828* fail, but it also takes lengths to never use either of these constructs. The829* boot loader, however, uses them and needs these assertionst o be correct.830*/831#ifdef _STANDALONE832STATIC_ASSERT (sizeof (L'A') == 2, "sizeof (L'A') does not meet UEFI Specification Data Type requirements");833STATIC_ASSERT (sizeof (L"A") == 4, "sizeof (L\"A\") does not meet UEFI Specification Data Type requirements");834#endif835836STATIC_ASSERT (ALIGNOF (BOOLEAN) == sizeof (BOOLEAN), "Alignment of BOOLEAN does not meet UEFI Specification Data Type requirements");837STATIC_ASSERT (ALIGNOF (INT8) == sizeof (INT8), "Alignment of INT8 does not meet UEFI Specification Data Type requirements");838STATIC_ASSERT (ALIGNOF (UINT8) == sizeof (UINT8), "Alignment of INT16 does not meet UEFI Specification Data Type requirements");839STATIC_ASSERT (ALIGNOF (INT16) == sizeof (INT16), "Alignment of INT16 does not meet UEFI Specification Data Type requirements");840STATIC_ASSERT (ALIGNOF (UINT16) == sizeof (UINT16), "Alignment of UINT16 does not meet UEFI Specification Data Type requirements");841STATIC_ASSERT (ALIGNOF (INT32) == sizeof (INT32), "Alignment of INT32 does not meet UEFI Specification Data Type requirements");842STATIC_ASSERT (ALIGNOF (UINT32) == sizeof (UINT32), "Alignment of UINT32 does not meet UEFI Specification Data Type requirements");843STATIC_ASSERT (ALIGNOF (INT64) == sizeof (INT64), "Alignment of INT64 does not meet UEFI Specification Data Type requirements");844STATIC_ASSERT (ALIGNOF (UINT64) == sizeof (UINT64), "Alignment of UINT64 does not meet UEFI Specification Data Type requirements");845STATIC_ASSERT (ALIGNOF (CHAR8) == sizeof (CHAR8), "Alignment of CHAR8 does not meet UEFI Specification Data Type requirements");846STATIC_ASSERT (ALIGNOF (CHAR16) == sizeof (CHAR16), "Alignment of CHAR16 does not meet UEFI Specification Data Type requirements");847STATIC_ASSERT (ALIGNOF (INTN) == sizeof (INTN), "Alignment of INTN does not meet UEFI Specification Data Type requirements");848STATIC_ASSERT (ALIGNOF (UINTN) == sizeof (UINTN), "Alignment of UINTN does not meet UEFI Specification Data Type requirements");849STATIC_ASSERT (ALIGNOF (VOID *) == sizeof (VOID *), "Alignment of VOID * does not meet UEFI Specification Data Type requirements");850851//852// The following three enum types are used to verify that the compiler853// configuration for enum types is compliant with Section 2.3.1 of the854// UEFI 2.3.1 Errata C Specification. These enum types and enum values855// are not intended to be used. A prefix of '__' is used avoid856// conflicts with other types.857//858typedef enum {859__VerifyUint8EnumValue = 0xff860} __VERIFY_UINT8_ENUM_SIZE;861862typedef enum {863__VerifyUint16EnumValue = 0xffff864} __VERIFY_UINT16_ENUM_SIZE;865866typedef enum {867__VerifyInt32EnumValue = 0x7fffffff868} __VERIFY_INT32_ENUM_SIZE;869870STATIC_ASSERT (sizeof (__VERIFY_UINT8_ENUM_SIZE) == 4, "Size of enum does not meet UEFI Specification Data Type requirements");871STATIC_ASSERT (sizeof (__VERIFY_UINT16_ENUM_SIZE) == 4, "Size of enum does not meet UEFI Specification Data Type requirements");872STATIC_ASSERT (sizeof (__VERIFY_INT32_ENUM_SIZE) == 4, "Size of enum does not meet UEFI Specification Data Type requirements");873874STATIC_ASSERT (ALIGNOF (__VERIFY_UINT8_ENUM_SIZE) == sizeof (__VERIFY_UINT8_ENUM_SIZE), "Alignment of enum does not meet UEFI Specification Data Type requirements");875STATIC_ASSERT (ALIGNOF (__VERIFY_UINT16_ENUM_SIZE) == sizeof (__VERIFY_UINT16_ENUM_SIZE), "Alignment of enum does not meet UEFI Specification Data Type requirements");876STATIC_ASSERT (ALIGNOF (__VERIFY_INT32_ENUM_SIZE) == sizeof (__VERIFY_INT32_ENUM_SIZE), "Alignment of enum does not meet UEFI Specification Data Type requirements");877878/**879Macro that returns a pointer to the data structure that contains a specified field of880that data structure. This is a lightweight method to hide information by placing a881public data structure inside a larger private data structure and using a pointer to882the public data structure to retrieve a pointer to the private data structure.883884This function computes the offset, in bytes, of field specified by Field from the beginning885of the data structure specified by TYPE. This offset is subtracted from Record, and is886used to return a pointer to a data structure of the type specified by TYPE. If the data type887specified by TYPE does not contain the field specified by Field, then the module will not compile.888889@param Record Pointer to the field specified by Field within a data structure of type TYPE.890@param TYPE The name of the data structure type to return. This data structure must891contain the field specified by Field.892@param Field The name of the field in the data structure specified by TYPE to which Record points.893894@return A pointer to the structure from one of it's elements.895896**/897#define BASE_CR(Record, TYPE, Field) ((TYPE *) (VOID *) ((CHAR8 *) (Record) - OFFSET_OF (TYPE, Field)))898899/**900Checks whether a value is a power of two.901902@param Value The value to check.903904@retval TRUE Value is a power of two.905@retval FALSE Value is not a power of two.906**/907#define IS_POW2(Value) ((Value) != 0U && ((Value) & ((Value) - 1U)) == 0U)908909/**910Checks whether a value is aligned by a specified alignment.911912@param Value The value to check.913@param Alignment The alignment boundary used to check against.914915@retval TRUE Value is aligned by Alignment.916@retval FALSE Value is not aligned by Alignment.917**/918#define IS_ALIGNED(Value, Alignment) (((Value) & ((Alignment) - 1U)) == 0U)919920/**921Checks whether a pointer or address is aligned by a specified alignment.922923@param Address The pointer or address to check.924@param Alignment The alignment boundary used to check against.925926@retval TRUE Address is aligned by Alignment.927@retval FALSE Address is not aligned by Alignment.928**/929#define ADDRESS_IS_ALIGNED(Address, Alignment) IS_ALIGNED ((UINTN) (Address), Alignment)930931/**932Determines the addend to add to a value to round it up to the next boundary of933a specified alignment.934935@param Value The value to round up.936@param Alignment The alignment boundary used to return the addend.937938@return Addend to round Value up to alignment boundary Alignment.939**/940#define ALIGN_VALUE_ADDEND(Value, Alignment) (((Alignment) - (Value)) & ((Alignment) - 1U))941942/**943Rounds a value up to the next boundary using a specified alignment.944945This function rounds Value up to the next boundary using the specified Alignment.946This aligned value is returned.947948@param Value The value to round up.949@param Alignment The alignment boundary used to return the aligned value.950951@return A value up to the next boundary.952953**/954#define ALIGN_VALUE(Value, Alignment) ((Value) + ALIGN_VALUE_ADDEND (Value, Alignment))955956/**957Adjust a pointer by adding the minimum offset required for it to be aligned on958a specified alignment boundary.959960This function rounds the pointer specified by Pointer to the next alignment boundary961specified by Alignment. The pointer to the aligned address is returned.962963@param Pointer The pointer to round up.964@param Alignment The alignment boundary to use to return an aligned pointer.965966@return Pointer to the aligned address.967968**/969#define ALIGN_POINTER(Pointer, Alignment) ((VOID *) (ALIGN_VALUE ((UINTN)(Pointer), (Alignment))))970971/**972Rounds a value up to the next natural boundary for the current CPU.973This is 4-bytes for 32-bit CPUs and 8-bytes for 64-bit CPUs.974975This function rounds the value specified by Value up to the next natural boundary for the976current CPU. This rounded value is returned.977978@param Value The value to round up.979980@return Rounded value specified by Value.981982**/983#define ALIGN_VARIABLE(Value) ALIGN_VALUE ((Value), sizeof (UINTN))984985/**986Return the maximum of two operands.987988This macro returns the maximum of two operand specified by a and b.989Both a and b must be the same numerical types, signed or unsigned.990991@param a The first operand with any numerical type.992@param b The second operand. Can be any numerical type as long as is993the same type as a.994995@return Maximum of two operands.996997**/998#define MAX(a, b) \999(((a) > (b)) ? (a) : (b))10001001/**1002Return the minimum of two operands.10031004This macro returns the minimal of two operand specified by a and b.1005Both a and b must be the same numerical types, signed or unsigned.10061007@param a The first operand with any numerical type.1008@param b The second operand. It should be the same any numerical type with a.10091010@return Minimum of two operands.10111012**/1013#define MIN(a, b) \1014(((a) < (b)) ? (a) : (b))10151016/**1017Return the absolute value of a signed operand.10181019This macro returns the absolute value of the signed operand specified by a.10201021@param a The signed operand.10221023@return The absolute value of the signed operand.10241025**/1026#define ABS(a) \1027(((a) < 0) ? (-(a)) : (a))10281029//1030// Status codes common to all execution phases1031//1032typedef UINTN RETURN_STATUS;10331034/**1035Produces a RETURN_STATUS code with the highest bit set.10361037@param StatusCode The status code value to convert into a warning code.1038StatusCode must be in the range 0x00000000..0x7FFFFFFF.10391040@return The value specified by StatusCode with the highest bit set.10411042**/1043#define ENCODE_ERROR(StatusCode) ((RETURN_STATUS)(MAX_BIT | (StatusCode)))10441045/**1046Produces a RETURN_STATUS code with the highest bit clear.10471048@param StatusCode The status code value to convert into a warning code.1049StatusCode must be in the range 0x00000000..0x7FFFFFFF.10501051@return The value specified by StatusCode with the highest bit clear.10521053**/1054#define ENCODE_WARNING(StatusCode) ((RETURN_STATUS)(StatusCode))10551056/**1057Returns TRUE if a specified RETURN_STATUS code is an error code.10581059This function returns TRUE if StatusCode has the high bit set. Otherwise, FALSE is returned.10601061@param StatusCode The status code value to evaluate.10621063@retval TRUE The high bit of StatusCode is set.1064@retval FALSE The high bit of StatusCode is clear.10651066**/1067#define RETURN_ERROR(StatusCode) (((RETURN_STATUS)(StatusCode)) >= MAX_BIT)10681069///1070/// The operation completed successfully.1071///1072#define RETURN_SUCCESS (RETURN_STATUS)(0)10731074///1075/// The image failed to load.1076///1077#define RETURN_LOAD_ERROR ENCODE_ERROR (1)10781079///1080/// The parameter was incorrect.1081///1082#define RETURN_INVALID_PARAMETER ENCODE_ERROR (2)10831084///1085/// The operation is not supported.1086///1087#define RETURN_UNSUPPORTED ENCODE_ERROR (3)10881089///1090/// The buffer was not the proper size for the request.1091///1092#define RETURN_BAD_BUFFER_SIZE ENCODE_ERROR (4)10931094///1095/// The buffer was not large enough to hold the requested data.1096/// The required buffer size is returned in the appropriate1097/// parameter when this error occurs.1098///1099#define RETURN_BUFFER_TOO_SMALL ENCODE_ERROR (5)11001101///1102/// There is no data pending upon return.1103///1104#define RETURN_NOT_READY ENCODE_ERROR (6)11051106///1107/// The physical device reported an error while attempting the1108/// operation.1109///1110#define RETURN_DEVICE_ERROR ENCODE_ERROR (7)11111112///1113/// The device can not be written to.1114///1115#define RETURN_WRITE_PROTECTED ENCODE_ERROR (8)11161117///1118/// The resource has run out.1119///1120#define RETURN_OUT_OF_RESOURCES ENCODE_ERROR (9)11211122///1123/// An inconsistency was detected on the file system causing the1124/// operation to fail.1125///1126#define RETURN_VOLUME_CORRUPTED ENCODE_ERROR (10)11271128///1129/// There is no more space on the file system.1130///1131#define RETURN_VOLUME_FULL ENCODE_ERROR (11)11321133///1134/// The device does not contain any medium to perform the1135/// operation.1136///1137#define RETURN_NO_MEDIA ENCODE_ERROR (12)11381139///1140/// The medium in the device has changed since the last1141/// access.1142///1143#define RETURN_MEDIA_CHANGED ENCODE_ERROR (13)11441145///1146/// The item was not found.1147///1148#define RETURN_NOT_FOUND ENCODE_ERROR (14)11491150///1151/// Access was denied.1152///1153#define RETURN_ACCESS_DENIED ENCODE_ERROR (15)11541155///1156/// The server was not found or did not respond to the request.1157///1158#define RETURN_NO_RESPONSE ENCODE_ERROR (16)11591160///1161/// A mapping to the device does not exist.1162///1163#define RETURN_NO_MAPPING ENCODE_ERROR (17)11641165///1166/// A timeout time expired.1167///1168#define RETURN_TIMEOUT ENCODE_ERROR (18)11691170///1171/// The protocol has not been started.1172///1173#define RETURN_NOT_STARTED ENCODE_ERROR (19)11741175///1176/// The protocol has already been started.1177///1178#define RETURN_ALREADY_STARTED ENCODE_ERROR (20)11791180///1181/// The operation was aborted.1182///1183#define RETURN_ABORTED ENCODE_ERROR (21)11841185///1186/// An ICMP error occurred during the network operation.1187///1188#define RETURN_ICMP_ERROR ENCODE_ERROR (22)11891190///1191/// A TFTP error occurred during the network operation.1192///1193#define RETURN_TFTP_ERROR ENCODE_ERROR (23)11941195///1196/// A protocol error occurred during the network operation.1197///1198#define RETURN_PROTOCOL_ERROR ENCODE_ERROR (24)11991200///1201/// A function encountered an internal version that was1202/// incompatible with a version requested by the caller.1203///1204#define RETURN_INCOMPATIBLE_VERSION ENCODE_ERROR (25)12051206///1207/// The function was not performed due to a security violation.1208///1209#define RETURN_SECURITY_VIOLATION ENCODE_ERROR (26)12101211///1212/// A CRC error was detected.1213///1214#define RETURN_CRC_ERROR ENCODE_ERROR (27)12151216///1217/// The beginning or end of media was reached.1218///1219#define RETURN_END_OF_MEDIA ENCODE_ERROR (28)12201221///1222/// The end of the file was reached.1223///1224#define RETURN_END_OF_FILE ENCODE_ERROR (31)12251226///1227/// The language specified was invalid.1228///1229#define RETURN_INVALID_LANGUAGE ENCODE_ERROR (32)12301231///1232/// The security status of the data is unknown or compromised1233/// and the data must be updated or replaced to restore a valid1234/// security status.1235///1236#define RETURN_COMPROMISED_DATA ENCODE_ERROR (33)12371238///1239/// There is an address conflict address allocation.1240///1241#define RETURN_IP_ADDRESS_CONFLICT ENCODE_ERROR (34)12421243///1244/// A HTTP error occurred during the network operation.1245///1246#define RETURN_HTTP_ERROR ENCODE_ERROR (35)12471248///1249/// The string contained one or more characters that1250/// the device could not render and were skipped.1251///1252#define RETURN_WARN_UNKNOWN_GLYPH ENCODE_WARNING (1)12531254///1255/// The handle was closed, but the file was not deleted.1256///1257#define RETURN_WARN_DELETE_FAILURE ENCODE_WARNING (2)12581259///1260/// The handle was closed, but the data to the file was not1261/// flushed properly.1262///1263#define RETURN_WARN_WRITE_FAILURE ENCODE_WARNING (3)12641265///1266/// The resulting buffer was too small, and the data was1267/// truncated to the buffer size.1268///1269#define RETURN_WARN_BUFFER_TOO_SMALL ENCODE_WARNING (4)12701271///1272/// The data has not been updated within the timeframe set by1273/// local policy for this type of data.1274///1275#define RETURN_WARN_STALE_DATA ENCODE_WARNING (5)12761277///1278/// The resulting buffer contains UEFI-compliant file system.1279///1280#define RETURN_WARN_FILE_SYSTEM ENCODE_WARNING (6)12811282///1283/// The operation will be processed across a system reset.1284///1285#define RETURN_WARN_RESET_REQUIRED ENCODE_WARNING (7)12861287/**1288Returns a 16-bit signature built from 2 ASCII characters.12891290This macro returns a 16-bit value built from the two ASCII characters specified1291by A and B.12921293@param A The first ASCII character.1294@param B The second ASCII character.12951296@return A 16-bit value built from the two ASCII characters specified by A and B.12971298**/1299#define SIGNATURE_16(A, B) ((A) | (B << 8))13001301/**1302Returns a 32-bit signature built from 4 ASCII characters.13031304This macro returns a 32-bit value built from the four ASCII characters specified1305by A, B, C, and D.13061307@param A The first ASCII character.1308@param B The second ASCII character.1309@param C The third ASCII character.1310@param D The fourth ASCII character.13111312@return A 32-bit value built from the two ASCII characters specified by A, B,1313C and D.13141315**/1316#define SIGNATURE_32(A, B, C, D) (SIGNATURE_16 (A, B) | (SIGNATURE_16 (C, D) << 16))13171318/**1319Returns a 64-bit signature built from 8 ASCII characters.13201321This macro returns a 64-bit value built from the eight ASCII characters specified1322by A, B, C, D, E, F, G,and H.13231324@param A The first ASCII character.1325@param B The second ASCII character.1326@param C The third ASCII character.1327@param D The fourth ASCII character.1328@param E The fifth ASCII character.1329@param F The sixth ASCII character.1330@param G The seventh ASCII character.1331@param H The eighth ASCII character.13321333@return A 64-bit value built from the two ASCII characters specified by A, B,1334C, D, E, F, G and H.13351336**/1337#define SIGNATURE_64(A, B, C, D, E, F, G, H) \1338(SIGNATURE_32 (A, B, C, D) | ((UINT64) (SIGNATURE_32 (E, F, G, H)) << 32))13391340#if defined (_MSC_EXTENSIONS) && !defined (__INTEL_COMPILER) && !defined (MDE_CPU_EBC)1341void *1342_ReturnAddress (1343void1344);13451346#pragma intrinsic(_ReturnAddress)13471348/**1349Get the return address of the calling function.13501351Based on intrinsic function _ReturnAddress that provides the address of1352the instruction in the calling function that will be executed after1353control returns to the caller.13541355@param L Return Level.13561357@return The return address of the calling function or 0 if L != 0.13581359**/1360#define RETURN_ADDRESS(L) ((L == 0) ? _ReturnAddress() : (VOID *) 0)1361#elif defined (__GNUC__) || defined (__clang__)13621363/**1364Get the return address of the calling function.13651366Based on built-in Function __builtin_return_address that returns1367the return address of the current function, or of one of its callers.13681369@param L Return Level.13701371@return The return address of the calling function.13721373**/1374#define RETURN_ADDRESS(L) __builtin_return_address (L)1375#else13761377/**1378Get the return address of the calling function.13791380@param L Return Level.13811382@return 0 as compilers don't support this feature.13831384**/1385#define RETURN_ADDRESS(L) ((VOID *) 0)1386#endif13871388/**1389Return the number of elements in an array.13901391@param Array An object of array type. Array is only used as an argument to1392the sizeof operator, therefore Array is never evaluated. The1393caller is responsible for ensuring that Array's type is not1394incomplete; that is, Array must have known constant size.13951396@return The number of elements in Array. The result has type UINTN.13971398**/1399#define ARRAY_SIZE(Array) (sizeof (Array) / sizeof ((Array)[0]))14001401#endif140214031404