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