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