efibind.h revision 1.3
11.3Sjmcneill/*	$NetBSD: efibind.h,v 1.3 2021/09/30 19:02:47 jmcneill Exp $	*/
21.1Sjmcneill
31.1Sjmcneill/*
41.1Sjmcneill * Copright (C) 2014 - 2015 Linaro Ltd.
51.1Sjmcneill * Author: Ard Biesheuvel <ard.biesheuvel@linaro.org>
61.1Sjmcneill *
71.1Sjmcneill * Redistribution and use in source and binary forms, with or without
81.1Sjmcneill * modification, are permitted provided that the following conditions
91.1Sjmcneill * are met:
101.1Sjmcneill * 1. Redistributions of source code must retain the above copyright
111.1Sjmcneill *    notice and this list of conditions, without modification.
121.1Sjmcneill * 2. The name of the author may not be used to endorse or promote products
131.1Sjmcneill *    derived from this software without specific prior written permission.
141.1Sjmcneill *
151.1Sjmcneill * Alternatively, this software may be distributed under the terms of the
161.1Sjmcneill * GNU General Public License as published by the Free Software Foundation;
171.1Sjmcneill * either version 2 of the License, or (at your option) any later version.
181.1Sjmcneill */
191.1Sjmcneill
201.3Sjmcneill#if !defined(_MSC_VER) && (!defined(__STDC_VERSION__) || (__STDC_VERSION__ < 199901L )) && !defined(__cplusplus)
211.1Sjmcneill
221.1Sjmcneill// ANSI C 1999/2000 stdint.h integer width declarations
231.1Sjmcneill
241.1Sjmcneilltypedef unsigned long long  uint64_t;
251.1Sjmcneilltypedef long long           int64_t;
261.1Sjmcneilltypedef unsigned int        uint32_t;
271.1Sjmcneilltypedef int                 int32_t;
281.1Sjmcneilltypedef unsigned short      uint16_t;
291.1Sjmcneilltypedef short               int16_t;
301.1Sjmcneilltypedef unsigned char       uint8_t;
311.1Sjmcneilltypedef signed char         int8_t;   // unqualified 'char' is unsigned on ARM
321.3Sjmcneilltypedef uint32_t            uintptr_t;
331.3Sjmcneilltypedef int32_t             intptr_t;
341.1Sjmcneill
351.2Sjmcneill#elif defined(__NetBSD__)
361.2Sjmcneill#include <sys/stdint.h>
371.1Sjmcneill#else
381.1Sjmcneill#include <stdint.h>
391.1Sjmcneill#endif
401.1Sjmcneill
411.1Sjmcneill/*
421.1Sjmcneill * This prevents GCC from emitting GOT based relocations, and use R_ARM_REL32
431.1Sjmcneill * relative relocations instead, which are more suitable for static binaries.
441.1Sjmcneill */
451.3Sjmcneill#if defined(__GNUC__) && !__STDC_HOSTED__
461.1Sjmcneill#pragma GCC visibility push (hidden)
471.1Sjmcneill#endif
481.1Sjmcneill
491.1Sjmcneill//
501.1Sjmcneill// Basic EFI types of various widths
511.1Sjmcneill//
521.1Sjmcneill
531.1Sjmcneill#ifndef __WCHAR_TYPE__
541.1Sjmcneill# define __WCHAR_TYPE__ short
551.1Sjmcneill#endif
561.1Sjmcneill
571.1Sjmcneilltypedef uint64_t   UINT64;
581.1Sjmcneilltypedef int64_t    INT64;
591.1Sjmcneill
601.1Sjmcneilltypedef uint32_t   UINT32;
611.1Sjmcneilltypedef int32_t    INT32;
621.1Sjmcneill
631.1Sjmcneilltypedef uint16_t   UINT16;
641.1Sjmcneilltypedef int16_t    INT16;
651.1Sjmcneilltypedef uint8_t    UINT8;
661.1Sjmcneilltypedef int8_t     INT8;
671.1Sjmcneilltypedef __WCHAR_TYPE__ WCHAR;
681.1Sjmcneill
691.1Sjmcneill#undef VOID
701.1Sjmcneill#define VOID    void
711.1Sjmcneill
721.1Sjmcneilltypedef int32_t    INTN;
731.1Sjmcneilltypedef uint32_t   UINTN;
741.1Sjmcneill
751.1Sjmcneill#define EFIERR(a)           (0x80000000 | a)
761.1Sjmcneill#define EFI_ERROR_MASK      0x80000000
771.1Sjmcneill#define EFIERR_OEM(a)       (0xc0000000 | a)
781.1Sjmcneill
791.1Sjmcneill#define BAD_POINTER         0xFBFBFBFB
801.1Sjmcneill#define MAX_ADDRESS         0xFFFFFFFF
811.1Sjmcneill
821.1Sjmcneill#define BREAKPOINT()        while (TRUE);
831.1Sjmcneill
841.1Sjmcneill//
851.1Sjmcneill// Pointers must be aligned to these address to function
861.1Sjmcneill//
871.1Sjmcneill
881.1Sjmcneill#define MIN_ALIGNMENT_SIZE  4
891.1Sjmcneill
901.1Sjmcneill#define ALIGN_VARIABLE(Value ,Adjustment) \
911.1Sjmcneill            (UINTN)Adjustment = 0; \
921.1Sjmcneill            if((UINTN)Value % MIN_ALIGNMENT_SIZE) \
931.1Sjmcneill                (UINTN)Adjustment = MIN_ALIGNMENT_SIZE - ((UINTN)Value % MIN_ALIGNMENT_SIZE); \
941.1Sjmcneill            Value = (UINTN)Value + (UINTN)Adjustment
951.1Sjmcneill
961.1Sjmcneill
971.1Sjmcneill//
981.1Sjmcneill// Define macros to build data structure signatures from characters.
991.1Sjmcneill//
1001.1Sjmcneill
1011.1Sjmcneill#define EFI_SIGNATURE_16(A,B)             ((A) | (B<<8))
1021.1Sjmcneill#define EFI_SIGNATURE_32(A,B,C,D)         (EFI_SIGNATURE_16(A,B)     | (EFI_SIGNATURE_16(C,D)     << 16))
1031.1Sjmcneill#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))
1041.1Sjmcneill
1051.1Sjmcneill//
1061.1Sjmcneill// EFIAPI - prototype calling convention for EFI function pointers
1071.1Sjmcneill// BOOTSERVICE - prototype for implementation of a boot service interface
1081.1Sjmcneill// RUNTIMESERVICE - prototype for implementation of a runtime service interface
1091.1Sjmcneill// RUNTIMEFUNCTION - prototype for implementation of a runtime function that is not a service
1101.1Sjmcneill// RUNTIME_CODE - pragma macro for declaring runtime code
1111.1Sjmcneill//
1121.1Sjmcneill
1131.1Sjmcneill#ifndef EFIAPI          // Forces EFI calling conventions reguardless of compiler options
1141.1Sjmcneill#define EFIAPI          // Substitute expresion to force C calling convention
1151.1Sjmcneill#endif
1161.1Sjmcneill
1171.1Sjmcneill#define BOOTSERVICE
1181.1Sjmcneill#define RUNTIMESERVICE
1191.1Sjmcneill#define RUNTIMEFUNCTION
1201.1Sjmcneill
1211.1Sjmcneill
1221.1Sjmcneill#define RUNTIME_CODE(a)         alloc_text("rtcode", a)
1231.1Sjmcneill#define BEGIN_RUNTIME_DATA()    data_seg("rtdata")
1241.1Sjmcneill#define END_RUNTIME_DATA()      data_seg("")
1251.1Sjmcneill
1261.1Sjmcneill#define VOLATILE                volatile
1271.1Sjmcneill
1281.1Sjmcneill#define MEMORY_FENCE            __sync_synchronize
1291.1Sjmcneill
1301.1Sjmcneill//
1311.1Sjmcneill// When build similiar to FW, then link everything together as
1321.1Sjmcneill// one big module. For the MSVC toolchain, we simply tell the
1331.1Sjmcneill// linker what our driver init function is using /ENTRY.
1341.1Sjmcneill//
1351.1Sjmcneill#if defined(_MSC_EXTENSIONS)
1361.1Sjmcneill#define EFI_DRIVER_ENTRY_POINT(InitFunction) \
1371.1Sjmcneill    __pragma(comment(linker, "/ENTRY:" # InitFunction))
1381.1Sjmcneill#else
1391.1Sjmcneill#define EFI_DRIVER_ENTRY_POINT(InitFunction)    \
1401.1Sjmcneill    UINTN                                       \
1411.1Sjmcneill    InitializeDriver (                          \
1421.1Sjmcneill        VOID    *ImageHandle,                   \
1431.1Sjmcneill        VOID    *SystemTable                    \
1441.1Sjmcneill        )                                       \
1451.1Sjmcneill    {                                           \
1461.1Sjmcneill        return InitFunction(ImageHandle,        \
1471.1Sjmcneill                SystemTable);                   \
1481.1Sjmcneill    }                                           \
1491.1Sjmcneill                                                \
1501.1Sjmcneill    EFI_STATUS efi_main(                        \
1511.1Sjmcneill        EFI_HANDLE image,                       \
1521.1Sjmcneill        EFI_SYSTEM_TABLE *systab                \
1531.1Sjmcneill        ) __attribute__((weak,                  \
1541.1Sjmcneill                alias ("InitializeDriver")));
1551.1Sjmcneill#endif
1561.1Sjmcneill
1571.1Sjmcneill#define LOAD_INTERNAL_DRIVER(_if, type, name, entry)    \
1581.1Sjmcneill        (_if)->LoadInternal(type, name, entry)
1591.1Sjmcneill
1601.1Sjmcneill
1611.1Sjmcneill//
1621.1Sjmcneill// Some compilers don't support the forward reference construct:
1631.1Sjmcneill//  typedef struct XXXXX
1641.1Sjmcneill//
1651.1Sjmcneill// The following macro provide a workaround for such cases.
1661.1Sjmcneill
1671.1Sjmcneill#define INTERFACE_DECL(x) struct x
1681.1Sjmcneill
1691.1Sjmcneill#define uefi_call_wrapper(func, va_num, ...) func(__VA_ARGS__)
1701.1Sjmcneill#define EFI_FUNCTION
171