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