efibind.h revision 1.2
11.2Sjmcneill/*	$NetBSD: efibind.h,v 1.2 2021/09/30 19:09:10 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.2Sjmcneill#if defined(__NetBSD__)
211.2Sjmcneill#include <sys/stdint.h>
221.2Sjmcneill#else
231.1Sjmcneill#include <stdint.h>
241.2Sjmcneill#endif
251.1Sjmcneill
261.1Sjmcneill//
271.1Sjmcneill// Basic EFI types of various widths
281.1Sjmcneill//
291.1Sjmcneill
301.1Sjmcneill
311.1Sjmcneill
321.1Sjmcneilltypedef uint64_t                UINT64;
331.1Sjmcneilltypedef int64_t                 INT64;
341.1Sjmcneilltypedef uint32_t                UINT32;
351.1Sjmcneilltypedef int32_t                 INT32;
361.1Sjmcneilltypedef uint16_t                UINT16;
371.1Sjmcneilltypedef int16_t                 INT16;
381.1Sjmcneilltypedef uint8_t                 UINT8;
391.1Sjmcneilltypedef int8_t                  INT8;
401.1Sjmcneill#ifndef __WCHAR_TYPE__
411.1Sjmcneill#define __WCHAR_TYPE__          short
421.1Sjmcneill#endif
431.1Sjmcneilltypedef __WCHAR_TYPE__          WCHAR;
441.1Sjmcneill#ifndef BOOLEAN
451.1Sjmcneilltypedef uint8_t                 BOOLEAN;
461.1Sjmcneill#endif
471.1Sjmcneill#undef VOID
481.1Sjmcneill#define VOID                    void
491.1Sjmcneilltypedef int64_t                 INTN;
501.1Sjmcneilltypedef uint64_t                UINTN;
511.1Sjmcneill
521.1Sjmcneill#define EFI_ERROR_MASK          0x8000000000000000
531.1Sjmcneill#define EFIERR(a)               (EFI_ERROR_MASK | a)
541.1Sjmcneill#define EFIERR_OEM(a)           (0xc000000000000000 | a)
551.1Sjmcneill
561.1Sjmcneill#define BAD_POINTER             0xFBFBFBFBFBFBFBFB
571.1Sjmcneill#define MAX_ADDRESS             0xFFFFFFFFFFFFFFFF
581.1Sjmcneill
591.1Sjmcneill#define BREAKPOINT()            while(1);
601.1Sjmcneill
611.1Sjmcneill//
621.1Sjmcneill// Pointers must be aligned to these address to function
631.1Sjmcneill//
641.1Sjmcneill#define MIN_ALIGNMENT_SIZE      8
651.1Sjmcneill
661.1Sjmcneill#define ALIGN_VARIABLE(Value, Adjustment) \
671.1Sjmcneill   (UINTN)Adjustment = 0; \
681.1Sjmcneill   if((UINTN)Value % MIN_ALIGNMENT_SIZE) \
691.1Sjmcneill       (UINTN)Adjustment = MIN_ALIGNMENT_SIZE - ((UINTN)Value % MIN_ALIGNMENT_SIZE); \
701.1Sjmcneill   Value = (UINTN)Value + (UINTN)Adjustment
711.1Sjmcneill
721.1Sjmcneill//
731.1Sjmcneill// Define macros to build data structure signatures from characters.
741.1Sjmcneill//
751.1Sjmcneill#define EFI_SIGNATURE_16(A,B)             ((A) | (B<<8))
761.1Sjmcneill#define EFI_SIGNATURE_32(A,B,C,D)         (EFI_SIGNATURE_16(A,B)     | (EFI_SIGNATURE_16(C,D)     << 16))
771.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))
781.1Sjmcneill
791.1Sjmcneill//
801.1Sjmcneill// EFIAPI - prototype calling convention for EFI function pointers
811.1Sjmcneill// BOOTSERVICE - prototype for implementation of a boot service interface
821.1Sjmcneill// RUNTIMESERVICE - prototype for implementation of a runtime service interface
831.1Sjmcneill// RUNTIMEFUNCTION - prototype for implementation of a runtime function that is not a service
841.1Sjmcneill// RUNTIME_CODE - pragma macro for declaring runtime code
851.1Sjmcneill//
861.1Sjmcneill#ifndef EFIAPI                  // Forces EFI calling conventions reguardless of compiler options
871.1Sjmcneill#define EFIAPI                  // Substitute expresion to force C calling convention
881.1Sjmcneill#endif
891.1Sjmcneill#define BOOTSERVICE
901.1Sjmcneill#define RUNTIMESERVICE
911.1Sjmcneill#define RUNTIMEFUNCTION
921.1Sjmcneill#define RUNTIME_CODE(a)         alloc_text("rtcode", a)
931.1Sjmcneill#define BEGIN_RUNTIME_DATA()    data_seg("rtdata")
941.1Sjmcneill#define END_RUNTIME_DATA()      data_seg("")
951.1Sjmcneill
961.1Sjmcneill#define VOLATILE                volatile
971.1Sjmcneill#define MEMORY_FENCE            __sync_synchronize
981.1Sjmcneill
991.1Sjmcneill//
1001.1Sjmcneill// When build similiar to FW, then link everything together as
1011.1Sjmcneill// one big module. For the MSVC toolchain, we simply tell the
1021.1Sjmcneill// linker what our driver init function is using /ENTRY.
1031.1Sjmcneill//
1041.1Sjmcneill#if defined(_MSC_EXTENSIONS)
1051.1Sjmcneill#define EFI_DRIVER_ENTRY_POINT(InitFunction) \
1061.1Sjmcneill    __pragma(comment(linker, "/ENTRY:" # InitFunction))
1071.1Sjmcneill#else
1081.1Sjmcneill#define EFI_DRIVER_ENTRY_POINT(InitFunction)    \
1091.1Sjmcneill    UINTN                                       \
1101.1Sjmcneill    InitializeDriver (                          \
1111.1Sjmcneill        VOID    *ImageHandle,                   \
1121.1Sjmcneill        VOID    *SystemTable                    \
1131.1Sjmcneill        )                                       \
1141.1Sjmcneill    {                                           \
1151.1Sjmcneill        return InitFunction(ImageHandle,        \
1161.1Sjmcneill                SystemTable);                   \
1171.1Sjmcneill    }                                           \
1181.1Sjmcneill                                                \
1191.1Sjmcneill    EFI_STATUS efi_main(                        \
1201.1Sjmcneill        EFI_HANDLE image,                       \
1211.1Sjmcneill        EFI_SYSTEM_TABLE *systab                \
1221.1Sjmcneill        ) __attribute__((weak,                  \
1231.1Sjmcneill                alias ("InitializeDriver")));
1241.1Sjmcneill#endif
1251.1Sjmcneill
1261.1Sjmcneill#define LOAD_INTERNAL_DRIVER(_if, type, name, entry) \
1271.1Sjmcneill   (_if)->LoadInternal(type, name, entry)
1281.1Sjmcneill
1291.1Sjmcneill//
1301.1Sjmcneill// Some compilers don't support the forward reference construct:
1311.1Sjmcneill//  typedef struct XXXXX
1321.1Sjmcneill//
1331.1Sjmcneill// The following macro provide a workaround for such cases.
1341.1Sjmcneill#define INTERFACE_DECL(x)       struct x
1351.1Sjmcneill
1361.1Sjmcneill#define uefi_call_wrapper(func, va_num, ...) func(__VA_ARGS__)
1371.1Sjmcneill#define EFI_FUNCTION
138