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