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