efibind.h revision 1.4
11.3Sjmcneill/*	$NetBSD: efibind.h,v 1.4 2021/09/30 19:02:47 jmcneill Exp $	*/
21.1Sjakllsch
31.1Sjakllsch/*++
41.1Sjakllsch
51.1SjakllschCopyright (c) 1998  Intel Corporation
61.1Sjakllsch
71.1SjakllschModule Name:
81.1Sjakllsch
91.1Sjakllsch    efefind.h
101.1Sjakllsch
111.1SjakllschAbstract:
121.1Sjakllsch
131.1Sjakllsch    EFI to compile bindings
141.1Sjakllsch
151.1Sjakllsch
161.1Sjakllsch
171.1Sjakllsch
181.1SjakllschRevision History
191.1Sjakllsch
201.1Sjakllsch--*/
211.1Sjakllsch
221.1Sjakllsch#pragma pack()
231.1Sjakllsch
241.1Sjakllsch
251.1Sjakllsch//
261.1Sjakllsch// Basic int types of various widths
271.1Sjakllsch//
281.1Sjakllsch
291.4Sjmcneill#if !defined(__STDC_VERSION__) || (__STDC_VERSION__ < 199901L ) && !defined(__cplusplus)
301.1Sjakllsch
311.1Sjakllsch    // No ANSI C 1999/2000 stdint.h integer width declarations
321.1Sjakllsch
331.1Sjakllsch    #ifdef _MSC_EXTENSIONS
341.1Sjakllsch        // Use Microsoft C compiler integer width declarations
351.1Sjakllsch
361.1Sjakllsch        typedef unsigned __int64    uint64_t;
371.1Sjakllsch        typedef __int64             int64_t;
381.1Sjakllsch        typedef unsigned __int32    uint32_t;
391.1Sjakllsch        typedef __int32             int32_t;
401.1Sjakllsch        typedef unsigned __int16    uint16_t;
411.1Sjakllsch        typedef __int16             int16_t;
421.1Sjakllsch        typedef unsigned __int8     uint8_t;
431.1Sjakllsch        typedef __int8              int8_t;
441.1Sjakllsch    #elif defined(UNIX_LP64)
451.1Sjakllsch        // Use LP64 programming model from C_FLAGS for integer width declarations
461.1Sjakllsch
471.1Sjakllsch        typedef unsigned long       uint64_t;
481.1Sjakllsch        typedef long                int64_t;
491.1Sjakllsch        typedef unsigned int        uint32_t;
501.1Sjakllsch        typedef int                 int32_t;
511.1Sjakllsch        typedef unsigned short      uint16_t;
521.1Sjakllsch        typedef short               int16_t;
531.1Sjakllsch        typedef unsigned char       uint8_t;
541.1Sjakllsch        typedef char                int8_t;
551.1Sjakllsch    #else
561.1Sjakllsch        // Assume P64 programming model from C_FLAGS for integer width declarations
571.1Sjakllsch
581.1Sjakllsch        typedef unsigned long long  uint64_t;
591.1Sjakllsch        typedef long long           int64_t;
601.1Sjakllsch        typedef unsigned int        uint32_t;
611.1Sjakllsch        typedef int                 int32_t;
621.1Sjakllsch        typedef unsigned short      uint16_t;
631.1Sjakllsch        typedef short               int16_t;
641.1Sjakllsch        typedef unsigned char       uint8_t;
651.1Sjakllsch        typedef char                int8_t;
661.1Sjakllsch    #endif
671.4Sjmcneill    typedef uint64_t            uintptr_t;
681.4Sjmcneill    typedef int64_t             intptr_t;
691.2Sjakllsch#elif defined(__NetBSD__)
701.2Sjakllsch    #include <sys/stdint.h>
711.1Sjakllsch#elif defined(__GNUC__)
721.1Sjakllsch    #include <stdint.h>
731.1Sjakllsch#endif
741.1Sjakllsch
751.1Sjakllsch//
761.1Sjakllsch// Basic EFI types of various widths
771.1Sjakllsch//
781.1Sjakllsch#ifndef __WCHAR_TYPE__
791.1Sjakllsch# define __WCHAR_TYPE__	short
801.1Sjakllsch#endif
811.1Sjakllsch
821.1Sjakllsch
831.1Sjakllschtypedef uint64_t   UINT64;
841.1Sjakllschtypedef int64_t    INT64;
851.1Sjakllschtypedef uint32_t   UINT32;
861.1Sjakllschtypedef int32_t    INT32;
871.1Sjakllschtypedef uint16_t   UINT16;
881.1Sjakllschtypedef int16_t    INT16;
891.1Sjakllschtypedef uint8_t    UINT8;
901.1Sjakllschtypedef int8_t     INT8;
911.1Sjakllschtypedef __WCHAR_TYPE__ WCHAR;
921.1Sjakllsch
931.1Sjakllsch
941.1Sjakllsch#undef VOID
951.1Sjakllsch#define VOID    void
961.1Sjakllsch
971.1Sjakllsch
981.1Sjakllschtypedef int64_t    INTN;
991.1Sjakllschtypedef uint64_t   UINTN;
1001.1Sjakllsch
1011.1Sjakllsch//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1021.1Sjakllsch// BugBug: Code to debug
1031.1Sjakllsch//
1041.1Sjakllsch#define BIT63   0x8000000000000000
1051.1Sjakllsch
1061.1Sjakllsch#define PLATFORM_IOBASE_ADDRESS   (0xffffc000000 | BIT63)
1071.1Sjakllsch#define PORT_TO_MEMD(_Port) (PLATFORM_IOBASE_ADDRESS | ( ( ( (_Port) & 0xfffc) << 10 ) | ( (_Port) & 0x0fff) ) )
1081.1Sjakllsch
1091.1Sjakllsch//
1101.1Sjakllsch// Macro's with casts make this much easier to use and read.
1111.1Sjakllsch//
1121.1Sjakllsch#define PORT_TO_MEM8D(_Port)  (*(UINT8  *)(PORT_TO_MEMD(_Port)))
1131.1Sjakllsch#define POST_CODE(_Data)  (PORT_TO_MEM8D(0x80) = (_Data))
1141.1Sjakllsch//
1151.1Sjakllsch// BugBug: End Debug Code!!!
1161.1Sjakllsch//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1171.1Sjakllsch
1181.1Sjakllsch#define EFIERR(a)           (0x8000000000000000 | a)
1191.1Sjakllsch#define EFI_ERROR_MASK      0x8000000000000000
1201.1Sjakllsch#define EFIERR_OEM(a)       (0xc000000000000000 | a)
1211.1Sjakllsch
1221.1Sjakllsch#define BAD_POINTER         0xFBFBFBFBFBFBFBFB
1231.1Sjakllsch#define MAX_ADDRESS         0xFFFFFFFFFFFFFFFF
1241.1Sjakllsch
1251.1Sjakllsch#define BREAKPOINT()        while (TRUE)
1261.1Sjakllsch
1271.1Sjakllsch//
1281.1Sjakllsch// Pointers must be aligned to these address to function
1291.1Sjakllsch//  you will get an alignment fault if this value is less than 8
1301.1Sjakllsch//
1311.1Sjakllsch#define MIN_ALIGNMENT_SIZE  8
1321.1Sjakllsch
1331.1Sjakllsch#define ALIGN_VARIABLE(Value , Adjustment) \
1341.1Sjakllsch            (UINTN) Adjustment = 0; \
1351.1Sjakllsch            if((UINTN)Value % MIN_ALIGNMENT_SIZE) \
1361.1Sjakllsch                (UINTN)Adjustment = MIN_ALIGNMENT_SIZE - ((UINTN)Value % MIN_ALIGNMENT_SIZE); \
1371.1Sjakllsch            Value = (UINTN)Value + (UINTN)Adjustment
1381.1Sjakllsch
1391.1Sjakllsch//
1401.1Sjakllsch// Define macros to create data structure signatures.
1411.1Sjakllsch//
1421.1Sjakllsch
1431.1Sjakllsch#define EFI_SIGNATURE_16(A,B)             ((A) | (B<<8))
1441.1Sjakllsch#define EFI_SIGNATURE_32(A,B,C,D)         (EFI_SIGNATURE_16(A,B)     | (EFI_SIGNATURE_16(C,D)     << 16))
1451.1Sjakllsch#define EFI_SIGNATURE_64(A,B,C,D,E,F,G,H) (EFI_SIGNATURE_32(A,B,C,D) | ((UINT64)(EFI_SIGNATURE_32(E,F,G,H)) << 32))
1461.1Sjakllsch//
1471.1Sjakllsch// To export & import functions in the EFI emulator environment
1481.1Sjakllsch//
1491.1Sjakllsch
1501.1Sjakllsch    #define EXPORTAPI
1511.1Sjakllsch
1521.1Sjakllsch//
1531.1Sjakllsch// EFIAPI - prototype calling convention for EFI function pointers
1541.1Sjakllsch// BOOTSERVICE - prototype for implementation of a boot service interface
1551.1Sjakllsch// RUNTIMESERVICE - prototype for implementation of a runtime service interface
1561.1Sjakllsch// RUNTIMEFUNCTION - prototype for implementation of a runtime function that is not a service
1571.1Sjakllsch// RUNTIME_CODE - pragma macro for declaring runtime code
1581.1Sjakllsch//
1591.1Sjakllsch
1601.1Sjakllsch#ifndef EFIAPI                  // Forces EFI calling conventions reguardless of compiler options
1611.1Sjakllsch    #ifdef _MSC_EXTENSIONS
1621.1Sjakllsch        #define EFIAPI __cdecl  // Force C calling convention for Microsoft C compiler
1631.1Sjakllsch    #else
1641.1Sjakllsch        #define EFIAPI          // Substitute expresion to force C calling convention
1651.1Sjakllsch    #endif
1661.1Sjakllsch#endif
1671.1Sjakllsch
1681.1Sjakllsch#define BOOTSERVICE
1691.1Sjakllsch#define RUNTIMESERVICE
1701.1Sjakllsch#define RUNTIMEFUNCTION
1711.1Sjakllsch
1721.1Sjakllsch#define RUNTIME_CODE(a)         alloc_text("rtcode", a)
1731.1Sjakllsch#define BEGIN_RUNTIME_DATA()    data_seg("rtdata")
1741.1Sjakllsch#define END_RUNTIME_DATA()      data_seg("")
1751.1Sjakllsch
1761.1Sjakllsch#define VOLATILE    volatile
1771.1Sjakllsch
1781.1Sjakllsch//
1791.1Sjakllsch// BugBug: Need to find out if this is portable accross compliers.
1801.1Sjakllsch//
1811.1Sjakllsch#ifdef __GNUC__
1821.1Sjakllsch#define MEMORY_FENCE()    __asm__ __volatile__ ("mf.a" ::: "memory")
1831.1Sjakllsch#else
1841.1Sjakllschvoid __mf (void);
1851.1Sjakllsch#pragma intrinsic (__mf)
1861.1Sjakllsch#define MEMORY_FENCE()    __mf()
1871.1Sjakllsch#endif
1881.3Sjmcneill
1891.1Sjakllsch//
1901.1Sjakllsch// When build similiar to FW, then link everything together as
1911.3Sjmcneill// one big module. For the MSVC toolchain, we simply tell the
1921.3Sjmcneill// linker what our driver init function is using /ENTRY.
1931.1Sjakllsch//
1941.3Sjmcneill#if defined(_MSC_EXTENSIONS)
1951.3Sjmcneill    #define EFI_DRIVER_ENTRY_POINT(InitFunction) \
1961.3Sjmcneill        __pragma(comment(linker, "/ENTRY:" # InitFunction))
1971.3Sjmcneill#else
1981.3Sjmcneill    #define EFI_DRIVER_ENTRY_POINT(InitFunction)    \
1991.3Sjmcneill        UINTN                                       \
2001.3Sjmcneill        InitializeDriver (                          \
2011.3Sjmcneill            VOID    *ImageHandle,                   \
2021.3Sjmcneill            VOID    *SystemTable                    \
2031.3Sjmcneill            )                                       \
2041.3Sjmcneill        {                                           \
2051.3Sjmcneill            return InitFunction(ImageHandle,        \
2061.3Sjmcneill                    SystemTable);                   \
2071.3Sjmcneill        }                                           \
2081.3Sjmcneill                                                    \
2091.3Sjmcneill        EFI_STATUS efi_main(                        \
2101.3Sjmcneill            EFI_HANDLE image,                       \
2111.3Sjmcneill            EFI_SYSTEM_TABLE *systab                \
2121.3Sjmcneill            ) __attribute__((weak,                  \
2131.3Sjmcneill                    alias ("InitializeDriver")));
2141.3Sjmcneill#endif
2151.1Sjakllsch
2161.1Sjakllsch#define LOAD_INTERNAL_DRIVER(_if, type, name, entry)    \
2171.1Sjakllsch        (_if)->LoadInternal(type, name, entry)
2181.1Sjakllsch
2191.1Sjakllsch//
2201.1Sjakllsch// Some compilers don't support the forward reference construct:
2211.1Sjakllsch//  typedef struct XXXXX
2221.1Sjakllsch//
2231.1Sjakllsch// The following macro provide a workaround for such cases.
2241.1Sjakllsch//
2251.1Sjakllsch#ifdef NO_INTERFACE_DECL
2261.1Sjakllsch#define INTERFACE_DECL(x)
2271.1Sjakllsch#else
2281.3Sjmcneill#if defined(__GNUC__) || defined(_MSC_EXTENSIONS)
2291.1Sjakllsch#define INTERFACE_DECL(x) struct x
2301.1Sjakllsch#else
2311.1Sjakllsch#define INTERFACE_DECL(x) typedef struct x
2321.1Sjakllsch#endif
2331.1Sjakllsch#endif
2341.1Sjakllsch
2351.1Sjakllsch/* No efi call wrapper for IA32 architecture */
2361.1Sjakllsch#define uefi_call_wrapper(func, va_num, ...)	func(__VA_ARGS__)
2371.1Sjakllsch#define EFI_FUNCTION
238