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 #if defined(__GNUC__) && !__STDC_HOSTED__ 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 #ifndef __CHAR16_TYPE__ 53 # define __CHAR16_TYPE__ unsigned short 54 #endif 55 56 typedef uint64_t UINT64; 57 typedef int64_t INT64; 58 59 typedef uint32_t UINT32; 60 typedef int32_t INT32; 61 62 typedef uint16_t UINT16; 63 typedef __CHAR16_TYPE__ CHAR16; 64 typedef int16_t INT16; 65 66 typedef uint8_t UINT8; 67 typedef char CHAR8; 68 typedef int8_t INT8; 69 70 typedef __WCHAR_TYPE__ WCHAR; 71 72 #undef VOID 73 #define VOID void 74 75 typedef int32_t INTN; 76 typedef uint32_t UINTN; 77 78 #define EFIERR(a) (0x80000000 | a) 79 #define EFI_ERROR_MASK 0x80000000 80 #define EFIERR_OEM(a) (0xc0000000 | a) 81 82 #define BAD_POINTER 0xFBFBFBFB 83 #define MAX_ADDRESS 0xFFFFFFFF 84 85 #define BREAKPOINT() while (TRUE); 86 87 // 88 // Pointers must be aligned to these address to function 89 // 90 91 #define MIN_ALIGNMENT_SIZE 4 92 93 #define ALIGN_VARIABLE(Value ,Adjustment) \ 94 (UINTN)Adjustment = 0; \ 95 if((UINTN)Value % MIN_ALIGNMENT_SIZE) \ 96 (UINTN)Adjustment = MIN_ALIGNMENT_SIZE - ((UINTN)Value % MIN_ALIGNMENT_SIZE); \ 97 Value = (UINTN)Value + (UINTN)Adjustment 98 99 100 // 101 // Define macros to build data structure signatures from characters. 102 // 103 104 #define EFI_SIGNATURE_16(A,B) ((A) | (B<<8)) 105 #define EFI_SIGNATURE_32(A,B,C,D) (EFI_SIGNATURE_16(A,B) | (EFI_SIGNATURE_16(C,D) << 16)) 106 #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)) 107 108 // 109 // EFIAPI - prototype calling convention for EFI function pointers 110 // BOOTSERVICE - prototype for implementation of a boot service interface 111 // RUNTIMESERVICE - prototype for implementation of a runtime service interface 112 // RUNTIMEFUNCTION - prototype for implementation of a runtime function that is not a service 113 // RUNTIME_CODE - pragma macro for declaring runtime code 114 // 115 116 #ifndef EFIAPI // Forces EFI calling conventions reguardless of compiler options 117 #define EFIAPI // Substitute expresion to force C calling convention 118 #endif 119 120 #define BOOTSERVICE 121 #define RUNTIMESERVICE 122 #define RUNTIMEFUNCTION 123 124 125 #define RUNTIME_CODE(a) alloc_text("rtcode", a) 126 #define BEGIN_RUNTIME_DATA() data_seg("rtdata") 127 #define END_RUNTIME_DATA() data_seg("") 128 129 #define VOLATILE volatile 130 131 #define MEMORY_FENCE __sync_synchronize 132 133 // 134 // When build similiar to FW, then link everything together as 135 // one big module. For the MSVC toolchain, we simply tell the 136 // linker what our driver init function is using /ENTRY. 137 // 138 #if defined(_MSC_EXTENSIONS) 139 #define EFI_DRIVER_ENTRY_POINT(InitFunction) \ 140 __pragma(comment(linker, "/ENTRY:" # InitFunction)) 141 #else 142 #define EFI_DRIVER_ENTRY_POINT(InitFunction) \ 143 UINTN \ 144 InitializeDriver ( \ 145 VOID *ImageHandle, \ 146 VOID *SystemTable \ 147 ) \ 148 { \ 149 return InitFunction(ImageHandle, \ 150 SystemTable); \ 151 } \ 152 \ 153 EFI_STATUS efi_main( \ 154 EFI_HANDLE image, \ 155 EFI_SYSTEM_TABLE *systab \ 156 ) __attribute__((weak, \ 157 alias ("InitializeDriver"))); 158 #endif 159 160 #define LOAD_INTERNAL_DRIVER(_if, type, name, entry) \ 161 (_if)->LoadInternal(type, name, entry) 162 163 164 // 165 // Some compilers don't support the forward reference construct: 166 // typedef struct XXXXX 167 // 168 // The following macro provide a workaround for such cases. 169 170 #define INTERFACE_DECL(x) struct x 171 172 #define uefi_call_wrapper(func, va_num, ...) func(__VA_ARGS__) 173 #define EFI_FUNCTION 174