1*b1364609SPete Batard /* 2*b1364609SPete Batard * Copright (C) 2014 - 2015 Linaro Ltd. 3*b1364609SPete Batard * Author: Ard Biesheuvel <ard.biesheuvel@linaro.org> 4*b1364609SPete Batard * 5*b1364609SPete Batard * Redistribution and use in source and binary forms, with or without 6*b1364609SPete Batard * modification, are permitted provided that the following conditions 7*b1364609SPete Batard * are met: 8*b1364609SPete Batard * 1. Redistributions of source code must retain the above copyright 9*b1364609SPete Batard * notice and this list of conditions, without modification. 10*b1364609SPete Batard * 2. The name of the author may not be used to endorse or promote products 11*b1364609SPete Batard * derived from this software without specific prior written permission. 12*b1364609SPete Batard * 13*b1364609SPete Batard * Alternatively, this software may be distributed under the terms of the 14*b1364609SPete Batard * GNU General Public License as published by the Free Software Foundation; 15*b1364609SPete Batard * either version 2 of the License, or (at your option) any later version. 16*b1364609SPete Batard */ 17b95f138fSHeinrich Schuchardt 18b95f138fSHeinrich Schuchardt #include <stdint.h> 19b95f138fSHeinrich Schuchardt 20*b1364609SPete Batard // 21*b1364609SPete Batard // Basic EFI types of various widths 22*b1364609SPete Batard // 23b95f138fSHeinrich Schuchardt 24*b1364609SPete Batard 25*b1364609SPete Batard 26*b1364609SPete Batard typedef uint64_t UINT64; 27*b1364609SPete Batard typedef int64_t INT64; 28*b1364609SPete Batard typedef uint32_t UINT32; 29*b1364609SPete Batard typedef int32_t INT32; 30*b1364609SPete Batard typedef uint16_t UINT16; 31*b1364609SPete Batard typedef int16_t INT16; 32*b1364609SPete Batard typedef uint8_t UINT8; 33*b1364609SPete Batard typedef int8_t INT8; 34*b1364609SPete Batard #ifndef __WCHAR_TYPE__ 35*b1364609SPete Batard #define __WCHAR_TYPE__ short 36*b1364609SPete Batard #endif 37*b1364609SPete Batard typedef __WCHAR_TYPE__ WCHAR; 38*b1364609SPete Batard #ifndef BOOLEAN 39b95f138fSHeinrich Schuchardt typedef uint8_t BOOLEAN; 40*b1364609SPete Batard #endif 41*b1364609SPete Batard #undef VOID 42*b1364609SPete Batard #define VOID void 43b95f138fSHeinrich Schuchardt typedef int64_t INTN; 44b95f138fSHeinrich Schuchardt typedef uint64_t UINTN; 45*b1364609SPete Batard 46*b1364609SPete Batard #define EFI_ERROR_MASK 0x8000000000000000 47*b1364609SPete Batard #define EFIERR(a) (EFI_ERROR_MASK | a) 48*b1364609SPete Batard #define EFIERR_OEM(a) (0xc000000000000000 | a) 49*b1364609SPete Batard 50*b1364609SPete Batard #define BAD_POINTER 0xFBFBFBFBFBFBFBFB 51*b1364609SPete Batard #define MAX_ADDRESS 0xFFFFFFFFFFFFFFFF 52b95f138fSHeinrich Schuchardt 53b95f138fSHeinrich Schuchardt #define BREAKPOINT() while(1); 54b95f138fSHeinrich Schuchardt 55*b1364609SPete Batard // 56*b1364609SPete Batard // Pointers must be aligned to these address to function 57*b1364609SPete Batard // 58*b1364609SPete Batard #define MIN_ALIGNMENT_SIZE 8 59*b1364609SPete Batard 60*b1364609SPete Batard #define ALIGN_VARIABLE(Value, Adjustment) \ 61*b1364609SPete Batard (UINTN)Adjustment = 0; \ 62*b1364609SPete Batard if((UINTN)Value % MIN_ALIGNMENT_SIZE) \ 63*b1364609SPete Batard (UINTN)Adjustment = MIN_ALIGNMENT_SIZE - ((UINTN)Value % MIN_ALIGNMENT_SIZE); \ 64*b1364609SPete Batard Value = (UINTN)Value + (UINTN)Adjustment 65*b1364609SPete Batard 66*b1364609SPete Batard // 67*b1364609SPete Batard // Define macros to build data structure signatures from characters. 68*b1364609SPete Batard // 69b95f138fSHeinrich Schuchardt #define EFI_SIGNATURE_16(A,B) ((A) | (B<<8)) 70b95f138fSHeinrich Schuchardt #define EFI_SIGNATURE_32(A,B,C,D) (EFI_SIGNATURE_16(A,B) | (EFI_SIGNATURE_16(C,D) << 16)) 71b95f138fSHeinrich Schuchardt #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)) 72b5f98d40SHeinrich Schuchardt 73*b1364609SPete Batard // 74*b1364609SPete Batard // EFIAPI - prototype calling convention for EFI function pointers 75*b1364609SPete Batard // BOOTSERVICE - prototype for implementation of a boot service interface 76*b1364609SPete Batard // RUNTIMESERVICE - prototype for implementation of a runtime service interface 77*b1364609SPete Batard // RUNTIMEFUNCTION - prototype for implementation of a runtime function that is not a service 78*b1364609SPete Batard // RUNTIME_CODE - pragma macro for declaring runtime code 79*b1364609SPete Batard // 80*b1364609SPete Batard #ifndef EFIAPI // Forces EFI calling conventions reguardless of compiler options 81*b1364609SPete Batard #define EFIAPI // Substitute expresion to force C calling convention 82*b1364609SPete Batard #endif 83*b1364609SPete Batard #define BOOTSERVICE 84*b1364609SPete Batard #define RUNTIMESERVICE 85*b1364609SPete Batard #define RUNTIMEFUNCTION 86*b1364609SPete Batard #define RUNTIME_CODE(a) alloc_text("rtcode", a) 87*b1364609SPete Batard #define BEGIN_RUNTIME_DATA() data_seg("rtdata") 88*b1364609SPete Batard #define END_RUNTIME_DATA() data_seg("") 89*b1364609SPete Batard 90*b1364609SPete Batard #define VOLATILE volatile 91*b1364609SPete Batard #define MEMORY_FENCE __sync_synchronize 92*b1364609SPete Batard 93*b1364609SPete Batard // 94*b1364609SPete Batard // When build similiar to FW, then link everything together as 95*b1364609SPete Batard // one big module. For the MSVC toolchain, we simply tell the 96*b1364609SPete Batard // linker what our driver init function is using /ENTRY. 97*b1364609SPete Batard // 98*b1364609SPete Batard #if defined(_MSC_EXTENSIONS) 99*b1364609SPete Batard #define EFI_DRIVER_ENTRY_POINT(InitFunction) \ 100*b1364609SPete Batard __pragma(comment(linker, "/ENTRY:" # InitFunction)) 101*b1364609SPete Batard #else 102*b1364609SPete Batard #define EFI_DRIVER_ENTRY_POINT(InitFunction) \ 103*b1364609SPete Batard UINTN \ 104*b1364609SPete Batard InitializeDriver ( \ 105*b1364609SPete Batard VOID *ImageHandle, \ 106*b1364609SPete Batard VOID *SystemTable \ 107*b1364609SPete Batard ) \ 108*b1364609SPete Batard { \ 109*b1364609SPete Batard return InitFunction(ImageHandle, \ 110*b1364609SPete Batard SystemTable); \ 111*b1364609SPete Batard } \ 112*b1364609SPete Batard \ 113*b1364609SPete Batard EFI_STATUS efi_main( \ 114*b1364609SPete Batard EFI_HANDLE image, \ 115*b1364609SPete Batard EFI_SYSTEM_TABLE *systab \ 116*b1364609SPete Batard ) __attribute__((weak, \ 117*b1364609SPete Batard alias ("InitializeDriver"))); 118*b1364609SPete Batard #endif 119*b1364609SPete Batard 120*b1364609SPete Batard #define LOAD_INTERNAL_DRIVER(_if, type, name, entry) \ 121*b1364609SPete Batard (_if)->LoadInternal(type, name, entry) 122*b1364609SPete Batard 123*b1364609SPete Batard // 124*b1364609SPete Batard // Some compilers don't support the forward reference construct: 125*b1364609SPete Batard // typedef struct XXXXX 126*b1364609SPete Batard // 127*b1364609SPete Batard // The following macro provide a workaround for such cases. 128*b1364609SPete Batard #define INTERFACE_DECL(x) struct x 129*b1364609SPete Batard 130*b1364609SPete Batard #define uefi_call_wrapper(func, va_num, ...) func(__VA_ARGS__) 131b5f98d40SHeinrich Schuchardt #define EFI_FUNCTION 132