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