1333bf05dSNigel Croxon /* 2333bf05dSNigel Croxon * Copright (C) 2014 - 2015 Linaro Ltd. 3333bf05dSNigel Croxon * Author: Ard Biesheuvel <ard.biesheuvel@linaro.org> 4333bf05dSNigel Croxon * 5333bf05dSNigel Croxon * Redistribution and use in source and binary forms, with or without 6333bf05dSNigel Croxon * modification, are permitted provided that the following conditions 7333bf05dSNigel Croxon * are met: 8333bf05dSNigel Croxon * 1. Redistributions of source code must retain the above copyright 9333bf05dSNigel Croxon * notice and this list of conditions, without modification. 10333bf05dSNigel Croxon * 2. The name of the author may not be used to endorse or promote products 11333bf05dSNigel Croxon * derived from this software without specific prior written permission. 12333bf05dSNigel Croxon * 13333bf05dSNigel Croxon * Alternatively, this software may be distributed under the terms of the 14333bf05dSNigel Croxon * GNU General Public License as published by the Free Software Foundation; 15333bf05dSNigel Croxon * either version 2 of the License, or (at your option) any later version. 16333bf05dSNigel Croxon */ 17530d68baSNigel Croxon 186685cd00SAlexander von Gluck IV #if !defined(_MSC_VER) && (!defined(__STDC_VERSION__) || (__STDC_VERSION__ < 199901L )) && !defined(__cplusplus) 19333bf05dSNigel Croxon 20333bf05dSNigel Croxon // ANSI C 1999/2000 stdint.h integer width declarations 21333bf05dSNigel Croxon 22333bf05dSNigel Croxon typedef unsigned long long uint64_t; 23333bf05dSNigel Croxon typedef long long int64_t; 24333bf05dSNigel Croxon typedef unsigned int uint32_t; 25333bf05dSNigel Croxon typedef int int32_t; 26333bf05dSNigel Croxon typedef unsigned short uint16_t; 27333bf05dSNigel Croxon typedef short int16_t; 28333bf05dSNigel Croxon typedef unsigned char uint8_t; 29333bf05dSNigel Croxon typedef signed char int8_t; // unqualified 'char' is unsigned on ARM 301a53d8f8SEsben Haabendal typedef uint32_t uintptr_t; 311a53d8f8SEsben Haabendal typedef int32_t intptr_t; 32333bf05dSNigel Croxon 33333bf05dSNigel Croxon #else 34530d68baSNigel Croxon #include <stdint.h> 35333bf05dSNigel Croxon #endif 36530d68baSNigel Croxon 37530d68baSNigel Croxon /* 38530d68baSNigel Croxon * This prevents GCC from emitting GOT based relocations, and use R_ARM_REL32 39530d68baSNigel Croxon * relative relocations instead, which are more suitable for static binaries. 40530d68baSNigel Croxon */ 41*fa506bd2SArd Biesheuvel #if defined(__GNUC__) && !__STDC_HOSTED__ 42530d68baSNigel Croxon #pragma GCC visibility push (hidden) 4358e6955bSNigel Croxon #endif 44530d68baSNigel Croxon 45530d68baSNigel Croxon // 46530d68baSNigel Croxon // Basic EFI types of various widths 47530d68baSNigel Croxon // 48530d68baSNigel Croxon 49530d68baSNigel Croxon #ifndef __WCHAR_TYPE__ 50530d68baSNigel Croxon # define __WCHAR_TYPE__ short 51530d68baSNigel Croxon #endif 52530d68baSNigel Croxon 53530d68baSNigel Croxon typedef uint64_t UINT64; 54530d68baSNigel Croxon typedef int64_t INT64; 55530d68baSNigel Croxon 56530d68baSNigel Croxon typedef uint32_t UINT32; 57530d68baSNigel Croxon typedef int32_t INT32; 58530d68baSNigel Croxon 59530d68baSNigel Croxon typedef uint16_t UINT16; 60530d68baSNigel Croxon typedef int16_t INT16; 61530d68baSNigel Croxon typedef uint8_t UINT8; 62530d68baSNigel Croxon typedef int8_t INT8; 63530d68baSNigel Croxon typedef __WCHAR_TYPE__ WCHAR; 64530d68baSNigel Croxon 65530d68baSNigel Croxon #undef VOID 66530d68baSNigel Croxon #define VOID void 67530d68baSNigel Croxon 68530d68baSNigel Croxon typedef int32_t INTN; 69530d68baSNigel Croxon typedef uint32_t UINTN; 70530d68baSNigel Croxon 71530d68baSNigel Croxon #define EFIERR(a) (0x80000000 | a) 72530d68baSNigel Croxon #define EFI_ERROR_MASK 0x80000000 73530d68baSNigel Croxon #define EFIERR_OEM(a) (0xc0000000 | a) 74530d68baSNigel Croxon 75530d68baSNigel Croxon #define BAD_POINTER 0xFBFBFBFB 76530d68baSNigel Croxon #define MAX_ADDRESS 0xFFFFFFFF 77530d68baSNigel Croxon 78530d68baSNigel Croxon #define BREAKPOINT() while (TRUE); 79530d68baSNigel Croxon 80530d68baSNigel Croxon // 81530d68baSNigel Croxon // Pointers must be aligned to these address to function 82530d68baSNigel Croxon // 83530d68baSNigel Croxon 84530d68baSNigel Croxon #define MIN_ALIGNMENT_SIZE 4 85530d68baSNigel Croxon 86530d68baSNigel Croxon #define ALIGN_VARIABLE(Value ,Adjustment) \ 87530d68baSNigel Croxon (UINTN)Adjustment = 0; \ 88530d68baSNigel Croxon if((UINTN)Value % MIN_ALIGNMENT_SIZE) \ 89530d68baSNigel Croxon (UINTN)Adjustment = MIN_ALIGNMENT_SIZE - ((UINTN)Value % MIN_ALIGNMENT_SIZE); \ 90530d68baSNigel Croxon Value = (UINTN)Value + (UINTN)Adjustment 91530d68baSNigel Croxon 92530d68baSNigel Croxon 93530d68baSNigel Croxon // 94530d68baSNigel Croxon // Define macros to build data structure signatures from characters. 95530d68baSNigel Croxon // 96530d68baSNigel Croxon 97530d68baSNigel Croxon #define EFI_SIGNATURE_16(A,B) ((A) | (B<<8)) 98530d68baSNigel Croxon #define EFI_SIGNATURE_32(A,B,C,D) (EFI_SIGNATURE_16(A,B) | (EFI_SIGNATURE_16(C,D) << 16)) 99530d68baSNigel Croxon #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)) 100530d68baSNigel Croxon 101530d68baSNigel Croxon // 102530d68baSNigel Croxon // EFIAPI - prototype calling convention for EFI function pointers 103530d68baSNigel Croxon // BOOTSERVICE - prototype for implementation of a boot service interface 104530d68baSNigel Croxon // RUNTIMESERVICE - prototype for implementation of a runtime service interface 105530d68baSNigel Croxon // RUNTIMEFUNCTION - prototype for implementation of a runtime function that is not a service 106530d68baSNigel Croxon // RUNTIME_CODE - pragma macro for declaring runtime code 107530d68baSNigel Croxon // 108530d68baSNigel Croxon 109530d68baSNigel Croxon #ifndef EFIAPI // Forces EFI calling conventions reguardless of compiler options 110530d68baSNigel Croxon #define EFIAPI // Substitute expresion to force C calling convention 111530d68baSNigel Croxon #endif 112530d68baSNigel Croxon 113530d68baSNigel Croxon #define BOOTSERVICE 114530d68baSNigel Croxon #define RUNTIMESERVICE 115530d68baSNigel Croxon #define RUNTIMEFUNCTION 116530d68baSNigel Croxon 117530d68baSNigel Croxon 118530d68baSNigel Croxon #define RUNTIME_CODE(a) alloc_text("rtcode", a) 119530d68baSNigel Croxon #define BEGIN_RUNTIME_DATA() data_seg("rtdata") 120530d68baSNigel Croxon #define END_RUNTIME_DATA() data_seg("") 121530d68baSNigel Croxon 122530d68baSNigel Croxon #define VOLATILE volatile 123530d68baSNigel Croxon 124530d68baSNigel Croxon #define MEMORY_FENCE __sync_synchronize 125530d68baSNigel Croxon 126530d68baSNigel Croxon // 127530d68baSNigel Croxon // When build similiar to FW, then link everything together as 1286605c16fSNigel Croxon // one big module. For the MSVC toolchain, we simply tell the 1296605c16fSNigel Croxon // linker what our driver init function is using /ENTRY. 130530d68baSNigel Croxon // 1316605c16fSNigel Croxon #if defined(_MSC_EXTENSIONS) 1326605c16fSNigel Croxon #define EFI_DRIVER_ENTRY_POINT(InitFunction) \ 1336605c16fSNigel Croxon __pragma(comment(linker, "/ENTRY:" # InitFunction)) 1346605c16fSNigel Croxon #else 135530d68baSNigel Croxon #define EFI_DRIVER_ENTRY_POINT(InitFunction) \ 136530d68baSNigel Croxon UINTN \ 137530d68baSNigel Croxon InitializeDriver ( \ 138530d68baSNigel Croxon VOID *ImageHandle, \ 139530d68baSNigel Croxon VOID *SystemTable \ 140530d68baSNigel Croxon ) \ 141530d68baSNigel Croxon { \ 142530d68baSNigel Croxon return InitFunction(ImageHandle, \ 143530d68baSNigel Croxon SystemTable); \ 144530d68baSNigel Croxon } \ 145530d68baSNigel Croxon \ 146530d68baSNigel Croxon EFI_STATUS efi_main( \ 147530d68baSNigel Croxon EFI_HANDLE image, \ 148530d68baSNigel Croxon EFI_SYSTEM_TABLE *systab \ 149530d68baSNigel Croxon ) __attribute__((weak, \ 150530d68baSNigel Croxon alias ("InitializeDriver"))); 1516605c16fSNigel Croxon #endif 152530d68baSNigel Croxon 153530d68baSNigel Croxon #define LOAD_INTERNAL_DRIVER(_if, type, name, entry) \ 154530d68baSNigel Croxon (_if)->LoadInternal(type, name, entry) 155530d68baSNigel Croxon 156530d68baSNigel Croxon 157530d68baSNigel Croxon // 158530d68baSNigel Croxon // Some compilers don't support the forward reference construct: 159530d68baSNigel Croxon // typedef struct XXXXX 160530d68baSNigel Croxon // 161530d68baSNigel Croxon // The following macro provide a workaround for such cases. 162530d68baSNigel Croxon 163530d68baSNigel Croxon #define INTERFACE_DECL(x) struct x 164530d68baSNigel Croxon 165530d68baSNigel Croxon #define uefi_call_wrapper(func, va_num, ...) func(__VA_ARGS__) 166530d68baSNigel Croxon #define EFI_FUNCTION 167