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       uint64_t;
251.1Sjmcneilltypedef 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 uint64_t            uintptr_t;
331.3Sjmcneilltypedef int64_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// Basic EFI types of various widths
431.1Sjmcneill//
441.1Sjmcneill
451.1Sjmcneill#ifndef __WCHAR_TYPE__
461.1Sjmcneill# define __WCHAR_TYPE__ short
471.1Sjmcneill#endif
481.1Sjmcneill
491.1Sjmcneilltypedef uint64_t   UINT64;
501.1Sjmcneilltypedef int64_t    INT64;
511.1Sjmcneill
521.1Sjmcneilltypedef uint32_t   UINT32;
531.1Sjmcneilltypedef int32_t    INT32;
541.1Sjmcneill
551.1Sjmcneilltypedef uint16_t   UINT16;
561.1Sjmcneilltypedef int16_t    INT16;
571.1Sjmcneilltypedef uint8_t    UINT8;
581.1Sjmcneilltypedef int8_t     INT8;
591.1Sjmcneilltypedef __WCHAR_TYPE__ WCHAR;
601.1Sjmcneill
611.1Sjmcneill#undef VOID
621.1Sjmcneill#define VOID    void
631.1Sjmcneill
641.1Sjmcneilltypedef int64_t    INTN;
651.1Sjmcneilltypedef uint64_t   UINTN;
661.1Sjmcneill
671.1Sjmcneill#define EFIERR(a)           (0x8000000000000000 | a)
681.1Sjmcneill#define EFI_ERROR_MASK      0x8000000000000000
691.1Sjmcneill#define EFIERR_OEM(a)       (0xc000000000000000 | a)
701.1Sjmcneill
711.1Sjmcneill#define BAD_POINTER         0xFBFBFBFBFBFBFBFB
721.1Sjmcneill#define MAX_ADDRESS         0xFFFFFFFFFFFFFFFF
731.1Sjmcneill
741.1Sjmcneill#define BREAKPOINT()        while (TRUE);    // Make it hang on Bios[Dbg]32
751.1Sjmcneill
761.1Sjmcneill//
771.1Sjmcneill// Pointers must be aligned to these address to function
781.1Sjmcneill//
791.1Sjmcneill
801.1Sjmcneill#define MIN_ALIGNMENT_SIZE  8
811.1Sjmcneill
821.1Sjmcneill#define ALIGN_VARIABLE(Value ,Adjustment) \
831.1Sjmcneill            (UINTN)Adjustment = 0; \
841.1Sjmcneill            if((UINTN)Value % MIN_ALIGNMENT_SIZE) \
851.1Sjmcneill                (UINTN)Adjustment = MIN_ALIGNMENT_SIZE - ((UINTN)Value % MIN_ALIGNMENT_SIZE); \
861.1Sjmcneill            Value = (UINTN)Value + (UINTN)Adjustment
871.1Sjmcneill
881.1Sjmcneill
891.1Sjmcneill//
901.1Sjmcneill// Define macros to build data structure signatures from characters.
911.1Sjmcneill//
921.1Sjmcneill
931.1Sjmcneill#define EFI_SIGNATURE_16(A,B)             ((A) | (B<<8))
941.1Sjmcneill#define EFI_SIGNATURE_32(A,B,C,D)         (EFI_SIGNATURE_16(A,B)     | (EFI_SIGNATURE_16(C,D)     << 16))
951.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))
961.1Sjmcneill
971.1Sjmcneill//
981.1Sjmcneill// EFIAPI - prototype calling convention for EFI function pointers
991.1Sjmcneill// BOOTSERVICE - prototype for implementation of a boot service interface
1001.1Sjmcneill// RUNTIMESERVICE - prototype for implementation of a runtime service interface
1011.1Sjmcneill// RUNTIMEFUNCTION - prototype for implementation of a runtime function that is not a service
1021.1Sjmcneill// RUNTIME_CODE - pragma macro for declaring runtime code
1031.1Sjmcneill//
1041.1Sjmcneill
1051.1Sjmcneill#ifndef EFIAPI          // Forces EFI calling conventions reguardless of compiler options
1061.1Sjmcneill#define EFIAPI          // Substitute expresion to force C calling convention
1071.1Sjmcneill#endif
1081.1Sjmcneill
1091.1Sjmcneill#define BOOTSERVICE
1101.1Sjmcneill#define RUNTIMESERVICE
1111.1Sjmcneill#define RUNTIMEFUNCTION
1121.1Sjmcneill
1131.1Sjmcneill
1141.1Sjmcneill#define RUNTIME_CODE(a)         alloc_text("rtcode", a)
1151.1Sjmcneill#define BEGIN_RUNTIME_DATA()    data_seg("rtdata")
1161.1Sjmcneill#define END_RUNTIME_DATA()      data_seg("")
1171.1Sjmcneill
1181.1Sjmcneill#define VOLATILE                volatile
1191.1Sjmcneill
1201.1Sjmcneill#define MEMORY_FENCE            __sync_synchronize
1211.1Sjmcneill
1221.1Sjmcneill//
1231.1Sjmcneill// When build similiar to FW, then link everything together as
1241.1Sjmcneill// one big module. For the MSVC toolchain, we simply tell the
1251.1Sjmcneill// linker what our driver init function is using /ENTRY.
1261.1Sjmcneill//
1271.1Sjmcneill#if defined(_MSC_EXTENSIONS)
1281.1Sjmcneill#define EFI_DRIVER_ENTRY_POINT(InitFunction) \
1291.1Sjmcneill    __pragma(comment(linker, "/ENTRY:" # InitFunction))
1301.1Sjmcneill#else
1311.1Sjmcneill#define EFI_DRIVER_ENTRY_POINT(InitFunction)    \
1321.1Sjmcneill    UINTN                                       \
1331.1Sjmcneill    InitializeDriver (                          \
1341.1Sjmcneill        VOID    *ImageHandle,                   \
1351.1Sjmcneill        VOID    *SystemTable                    \
1361.1Sjmcneill        )                                       \
1371.1Sjmcneill    {                                           \
1381.1Sjmcneill        return InitFunction(ImageHandle,        \
1391.1Sjmcneill                SystemTable);                   \
1401.1Sjmcneill    }                                           \
1411.1Sjmcneill                                                \
1421.1Sjmcneill    EFI_STATUS efi_main(                        \
1431.1Sjmcneill        EFI_HANDLE image,                       \
1441.1Sjmcneill        EFI_SYSTEM_TABLE *systab                \
1451.1Sjmcneill        ) __attribute__((weak,                  \
1461.1Sjmcneill                alias ("InitializeDriver")));
1471.1Sjmcneill#endif
1481.1Sjmcneill
1491.1Sjmcneill#define LOAD_INTERNAL_DRIVER(_if, type, name, entry)    \
1501.1Sjmcneill        (_if)->LoadInternal(type, name, entry)
1511.1Sjmcneill
1521.1Sjmcneill
1531.1Sjmcneill//
1541.1Sjmcneill// Some compilers don't support the forward reference construct:
1551.1Sjmcneill//  typedef struct XXXXX
1561.1Sjmcneill//
1571.1Sjmcneill// The following macro provide a workaround for such cases.
1581.1Sjmcneill
1591.1Sjmcneill#define INTERFACE_DECL(x) struct x
1601.1Sjmcneill
1611.1Sjmcneill#define uefi_call_wrapper(func, va_num, ...) func(__VA_ARGS__)
1621.1Sjmcneill#define EFI_FUNCTION
163