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