1/*	$NetBSD: efibind.h,v 1.3 2021/09/30 19:02:47 jmcneill Exp $	*/
2
3/*
4 * Copright (C) 2014 - 2015 Linaro Ltd.
5 * Author: Ard Biesheuvel <ard.biesheuvel@linaro.org>
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice and this list of conditions, without modification.
12 * 2. The name of the author may not be used to endorse or promote products
13 *    derived from this software without specific prior written permission.
14 *
15 * Alternatively, this software may be distributed under the terms of the
16 * GNU General Public License as published by the Free Software Foundation;
17 * either version 2 of the License, or (at your option) any later version.
18 */
19
20#if !defined(_MSC_VER) && (!defined(__STDC_VERSION__) || (__STDC_VERSION__ < 199901L )) && !defined(__cplusplus)
21
22// ANSI C 1999/2000 stdint.h integer width declarations
23
24typedef unsigned long long  uint64_t;
25typedef long long           int64_t;
26typedef unsigned int        uint32_t;
27typedef int                 int32_t;
28typedef unsigned short      uint16_t;
29typedef short               int16_t;
30typedef unsigned char       uint8_t;
31typedef signed char         int8_t;   // unqualified 'char' is unsigned on ARM
32typedef uint32_t            uintptr_t;
33typedef int32_t             intptr_t;
34
35#elif defined(__NetBSD__)
36#include <sys/stdint.h>
37#else
38#include <stdint.h>
39#endif
40
41/*
42 * This prevents GCC from emitting GOT based relocations, and use R_ARM_REL32
43 * relative relocations instead, which are more suitable for static binaries.
44 */
45#if defined(__GNUC__) && !__STDC_HOSTED__
46#pragma GCC visibility push (hidden)
47#endif
48
49//
50// Basic EFI types of various widths
51//
52
53#ifndef __WCHAR_TYPE__
54# define __WCHAR_TYPE__ short
55#endif
56
57typedef uint64_t   UINT64;
58typedef int64_t    INT64;
59
60typedef uint32_t   UINT32;
61typedef int32_t    INT32;
62
63typedef uint16_t   UINT16;
64typedef int16_t    INT16;
65typedef uint8_t    UINT8;
66typedef int8_t     INT8;
67typedef __WCHAR_TYPE__ WCHAR;
68
69#undef VOID
70#define VOID    void
71
72typedef int32_t    INTN;
73typedef uint32_t   UINTN;
74
75#define EFIERR(a)           (0x80000000 | a)
76#define EFI_ERROR_MASK      0x80000000
77#define EFIERR_OEM(a)       (0xc0000000 | a)
78
79#define BAD_POINTER         0xFBFBFBFB
80#define MAX_ADDRESS         0xFFFFFFFF
81
82#define BREAKPOINT()        while (TRUE);
83
84//
85// Pointers must be aligned to these address to function
86//
87
88#define MIN_ALIGNMENT_SIZE  4
89
90#define ALIGN_VARIABLE(Value ,Adjustment) \
91            (UINTN)Adjustment = 0; \
92            if((UINTN)Value % MIN_ALIGNMENT_SIZE) \
93                (UINTN)Adjustment = MIN_ALIGNMENT_SIZE - ((UINTN)Value % MIN_ALIGNMENT_SIZE); \
94            Value = (UINTN)Value + (UINTN)Adjustment
95
96
97//
98// Define macros to build data structure signatures from characters.
99//
100
101#define EFI_SIGNATURE_16(A,B)             ((A) | (B<<8))
102#define EFI_SIGNATURE_32(A,B,C,D)         (EFI_SIGNATURE_16(A,B)     | (EFI_SIGNATURE_16(C,D)     << 16))
103#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))
104
105//
106// EFIAPI - prototype calling convention for EFI function pointers
107// BOOTSERVICE - prototype for implementation of a boot service interface
108// RUNTIMESERVICE - prototype for implementation of a runtime service interface
109// RUNTIMEFUNCTION - prototype for implementation of a runtime function that is not a service
110// RUNTIME_CODE - pragma macro for declaring runtime code
111//
112
113#ifndef EFIAPI          // Forces EFI calling conventions reguardless of compiler options
114#define EFIAPI          // Substitute expresion to force C calling convention
115#endif
116
117#define BOOTSERVICE
118#define RUNTIMESERVICE
119#define RUNTIMEFUNCTION
120
121
122#define RUNTIME_CODE(a)         alloc_text("rtcode", a)
123#define BEGIN_RUNTIME_DATA()    data_seg("rtdata")
124#define END_RUNTIME_DATA()      data_seg("")
125
126#define VOLATILE                volatile
127
128#define MEMORY_FENCE            __sync_synchronize
129
130//
131// When build similiar to FW, then link everything together as
132// one big module. For the MSVC toolchain, we simply tell the
133// linker what our driver init function is using /ENTRY.
134//
135#if defined(_MSC_EXTENSIONS)
136#define EFI_DRIVER_ENTRY_POINT(InitFunction) \
137    __pragma(comment(linker, "/ENTRY:" # InitFunction))
138#else
139#define EFI_DRIVER_ENTRY_POINT(InitFunction)    \
140    UINTN                                       \
141    InitializeDriver (                          \
142        VOID    *ImageHandle,                   \
143        VOID    *SystemTable                    \
144        )                                       \
145    {                                           \
146        return InitFunction(ImageHandle,        \
147                SystemTable);                   \
148    }                                           \
149                                                \
150    EFI_STATUS efi_main(                        \
151        EFI_HANDLE image,                       \
152        EFI_SYSTEM_TABLE *systab                \
153        ) __attribute__((weak,                  \
154                alias ("InitializeDriver")));
155#endif
156
157#define LOAD_INTERNAL_DRIVER(_if, type, name, entry)    \
158        (_if)->LoadInternal(type, name, entry)
159
160
161//
162// Some compilers don't support the forward reference construct:
163//  typedef struct XXXXX
164//
165// The following macro provide a workaround for such cases.
166
167#define INTERFACE_DECL(x) struct x
168
169#define uefi_call_wrapper(func, va_num, ...) func(__VA_ARGS__)
170#define EFI_FUNCTION
171