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