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