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(__STDC_VERSION__) || (__STDC_VERSION__ < 199901L ) 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 31 #else 32 #include <stdint.h> 33 #endif 34 35 /* 36 * This prevents GCC from emitting GOT based relocations, and use R_ARM_REL32 37 * relative relocations instead, which are more suitable for static binaries. 38 */ 39 #ifdef __GNUC__ 40 #pragma GCC visibility push (hidden) 41 #endif 42 43 // 44 // Basic EFI types of various widths 45 // 46 47 #ifndef __WCHAR_TYPE__ 48 # define __WCHAR_TYPE__ short 49 #endif 50 51 typedef uint64_t UINT64; 52 typedef int64_t INT64; 53 54 typedef uint32_t UINT32; 55 typedef int32_t INT32; 56 57 typedef uint16_t UINT16; 58 typedef int16_t INT16; 59 typedef uint8_t UINT8; 60 typedef int8_t INT8; 61 typedef __WCHAR_TYPE__ WCHAR; 62 63 #undef VOID 64 #define VOID void 65 66 typedef int32_t INTN; 67 typedef uint32_t UINTN; 68 69 #define EFIERR(a) (0x80000000 | a) 70 #define EFI_ERROR_MASK 0x80000000 71 #define EFIERR_OEM(a) (0xc0000000 | a) 72 73 #define BAD_POINTER 0xFBFBFBFB 74 #define MAX_ADDRESS 0xFFFFFFFF 75 76 #define BREAKPOINT() while (TRUE); 77 78 // 79 // Pointers must be aligned to these address to function 80 // 81 82 #define MIN_ALIGNMENT_SIZE 4 83 84 #define ALIGN_VARIABLE(Value ,Adjustment) \ 85 (UINTN)Adjustment = 0; \ 86 if((UINTN)Value % MIN_ALIGNMENT_SIZE) \ 87 (UINTN)Adjustment = MIN_ALIGNMENT_SIZE - ((UINTN)Value % MIN_ALIGNMENT_SIZE); \ 88 Value = (UINTN)Value + (UINTN)Adjustment 89 90 91 // 92 // Define macros to build data structure signatures from characters. 93 // 94 95 #define EFI_SIGNATURE_16(A,B) ((A) | (B<<8)) 96 #define EFI_SIGNATURE_32(A,B,C,D) (EFI_SIGNATURE_16(A,B) | (EFI_SIGNATURE_16(C,D) << 16)) 97 #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)) 98 99 // 100 // EFIAPI - prototype calling convention for EFI function pointers 101 // BOOTSERVICE - prototype for implementation of a boot service interface 102 // RUNTIMESERVICE - prototype for implementation of a runtime service interface 103 // RUNTIMEFUNCTION - prototype for implementation of a runtime function that is not a service 104 // RUNTIME_CODE - pragma macro for declaring runtime code 105 // 106 107 #ifndef EFIAPI // Forces EFI calling conventions reguardless of compiler options 108 #define EFIAPI // Substitute expresion to force C calling convention 109 #endif 110 111 #define BOOTSERVICE 112 #define RUNTIMESERVICE 113 #define RUNTIMEFUNCTION 114 115 116 #define RUNTIME_CODE(a) alloc_text("rtcode", a) 117 #define BEGIN_RUNTIME_DATA() data_seg("rtdata") 118 #define END_RUNTIME_DATA() data_seg("") 119 120 #define VOLATILE volatile 121 122 #define MEMORY_FENCE __sync_synchronize 123 124 // 125 // When build similiar to FW, then link everything together as 126 // one big module. For the MSVC toolchain, we simply tell the 127 // linker what our driver init function is using /ENTRY. 128 // 129 #if defined(_MSC_EXTENSIONS) 130 #define EFI_DRIVER_ENTRY_POINT(InitFunction) \ 131 __pragma(comment(linker, "/ENTRY:" # InitFunction)) 132 #else 133 #define EFI_DRIVER_ENTRY_POINT(InitFunction) \ 134 UINTN \ 135 InitializeDriver ( \ 136 VOID *ImageHandle, \ 137 VOID *SystemTable \ 138 ) \ 139 { \ 140 return InitFunction(ImageHandle, \ 141 SystemTable); \ 142 } \ 143 \ 144 EFI_STATUS efi_main( \ 145 EFI_HANDLE image, \ 146 EFI_SYSTEM_TABLE *systab \ 147 ) __attribute__((weak, \ 148 alias ("InitializeDriver"))); 149 #endif 150 151 #define LOAD_INTERNAL_DRIVER(_if, type, name, entry) \ 152 (_if)->LoadInternal(type, name, entry) 153 154 155 // 156 // Some compilers don't support the forward reference construct: 157 // typedef struct XXXXX 158 // 159 // The following macro provide a workaround for such cases. 160 161 #define INTERFACE_DECL(x) struct x 162 163 #define uefi_call_wrapper(func, va_num, ...) func(__VA_ARGS__) 164 #define EFI_FUNCTION 165 166 static inline UINT64 DivU64x32(UINT64 Dividend, UINTN Divisor, UINTN *Remainder) 167 { 168 /* 169 * GCC turns a division into a multiplication and shift with precalculated 170 * constants if the divisor is constant and the dividend fits into a 32 bit 171 * variable. Otherwise, it will turn this into calls into the 32-bit div 172 * library functions. 173 */ 174 if (Remainder) 175 *Remainder = Dividend % Divisor; 176 Dividend = Dividend / Divisor; 177 return Dividend; 178 } 179