13f234f5bSNigel Croxon /*
23f234f5bSNigel Croxon * Copright (C) 2014 - 2015 Linaro Ltd.
33f234f5bSNigel Croxon * Author: Ard Biesheuvel <ard.biesheuvel@linaro.org>
43f234f5bSNigel Croxon * Copright (C) 2017 Lemote Co.
53f234f5bSNigel Croxon * Author: Heiher <r@hev.cc>
63f234f5bSNigel Croxon *
73f234f5bSNigel Croxon * Redistribution and use in source and binary forms, with or without
83f234f5bSNigel Croxon * modification, are permitted provided that the following conditions
93f234f5bSNigel Croxon * are met:
103f234f5bSNigel Croxon * 1. Redistributions of source code must retain the above copyright
113f234f5bSNigel Croxon * notice and this list of conditions, without modification.
123f234f5bSNigel Croxon * 2. The name of the author may not be used to endorse or promote products
133f234f5bSNigel Croxon * derived from this software without specific prior written permission.
143f234f5bSNigel Croxon *
153f234f5bSNigel Croxon * Alternatively, this software may be distributed under the terms of the
163f234f5bSNigel Croxon * GNU General Public License as published by the Free Software Foundation;
173f234f5bSNigel Croxon * either version 2 of the License, or (at your option) any later version.
183f234f5bSNigel Croxon */
193f234f5bSNigel Croxon
206685cd00SAlexander von Gluck IV #if !defined(__STDC_VERSION__) || (__STDC_VERSION__ < 199901L ) && !defined(__cplusplus)
213f234f5bSNigel Croxon
223f234f5bSNigel Croxon // ANSI C 1999/2000 stdint.h integer width declarations
233f234f5bSNigel Croxon
243f234f5bSNigel Croxon typedef unsigned long uint64_t;
253f234f5bSNigel Croxon typedef long int64_t;
263f234f5bSNigel Croxon typedef unsigned int uint32_t;
273f234f5bSNigel Croxon typedef int int32_t;
283f234f5bSNigel Croxon typedef unsigned short uint16_t;
293f234f5bSNigel Croxon typedef short int16_t;
303f234f5bSNigel Croxon typedef unsigned char uint8_t;
313f234f5bSNigel Croxon typedef signed char int8_t; // unqualified 'char' is unsigned on ARM
321a53d8f8SEsben Haabendal typedef uint64_t uintptr_t;
331a53d8f8SEsben Haabendal typedef int64_t intptr_t;
343f234f5bSNigel Croxon
353f234f5bSNigel Croxon #else
363f234f5bSNigel Croxon #include <stdint.h>
373f234f5bSNigel Croxon #endif
383f234f5bSNigel Croxon
393f234f5bSNigel Croxon //
403f234f5bSNigel Croxon // Basic EFI types of various widths
413f234f5bSNigel Croxon //
423f234f5bSNigel Croxon
43189200d0SCallum Farmer #include <stddef.h>
44189200d0SCallum Farmer
45189200d0SCallum Farmer typedef wchar_t CHAR16;
46189200d0SCallum Farmer #define WCHAR CHAR16
473f234f5bSNigel Croxon
483f234f5bSNigel Croxon typedef uint64_t UINT64;
493f234f5bSNigel Croxon typedef int64_t INT64;
503f234f5bSNigel Croxon
513f234f5bSNigel Croxon typedef uint32_t UINT32;
523f234f5bSNigel Croxon typedef int32_t INT32;
533f234f5bSNigel Croxon
543f234f5bSNigel Croxon typedef uint16_t UINT16;
553f234f5bSNigel Croxon typedef int16_t INT16;
5614899d89SPeter Jones
573f234f5bSNigel Croxon typedef uint8_t UINT8;
5814899d89SPeter Jones typedef char CHAR8;
593f234f5bSNigel Croxon typedef int8_t INT8;
6014899d89SPeter Jones
613f234f5bSNigel Croxon #undef VOID
62*056bdaa5SCallum Farmer typedef void VOID;
633f234f5bSNigel Croxon
643f234f5bSNigel Croxon typedef int64_t INTN;
653f234f5bSNigel Croxon typedef uint64_t UINTN;
663f234f5bSNigel Croxon
673f234f5bSNigel Croxon #define EFIERR(a) (0x8000000000000000 | a)
683f234f5bSNigel Croxon #define EFI_ERROR_MASK 0x8000000000000000
693f234f5bSNigel Croxon #define EFIERR_OEM(a) (0xc000000000000000 | a)
703f234f5bSNigel Croxon
713f234f5bSNigel Croxon #define BAD_POINTER 0xFBFBFBFBFBFBFBFB
723f234f5bSNigel Croxon #define MAX_ADDRESS 0xFFFFFFFFFFFFFFFF
733f234f5bSNigel Croxon
743f234f5bSNigel Croxon #define BREAKPOINT() while (TRUE); // Make it hang on Bios[Dbg]32
753f234f5bSNigel Croxon
763f234f5bSNigel Croxon //
773f234f5bSNigel Croxon // Pointers must be aligned to these address to function
783f234f5bSNigel Croxon //
793f234f5bSNigel Croxon
803f234f5bSNigel Croxon #define MIN_ALIGNMENT_SIZE 8
813f234f5bSNigel Croxon
823f234f5bSNigel Croxon #define ALIGN_VARIABLE(Value ,Adjustment) \
833f234f5bSNigel Croxon (UINTN)Adjustment = 0; \
843f234f5bSNigel Croxon if((UINTN)Value % MIN_ALIGNMENT_SIZE) \
853f234f5bSNigel Croxon (UINTN)Adjustment = MIN_ALIGNMENT_SIZE - ((UINTN)Value % MIN_ALIGNMENT_SIZE); \
863f234f5bSNigel Croxon Value = (UINTN)Value + (UINTN)Adjustment
873f234f5bSNigel Croxon
883f234f5bSNigel Croxon
893f234f5bSNigel Croxon //
903f234f5bSNigel Croxon // Define macros to build data structure signatures from characters.
913f234f5bSNigel Croxon //
923f234f5bSNigel Croxon
933f234f5bSNigel Croxon #define EFI_SIGNATURE_16(A,B) ((A) | (B<<8))
943f234f5bSNigel Croxon #define EFI_SIGNATURE_32(A,B,C,D) (EFI_SIGNATURE_16(A,B) | (EFI_SIGNATURE_16(C,D) << 16))
953f234f5bSNigel 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))
963f234f5bSNigel Croxon
973f234f5bSNigel Croxon //
983f234f5bSNigel Croxon // EFIAPI - prototype calling convention for EFI function pointers
993f234f5bSNigel Croxon // BOOTSERVICE - prototype for implementation of a boot service interface
1003f234f5bSNigel Croxon // RUNTIMESERVICE - prototype for implementation of a runtime service interface
1013f234f5bSNigel Croxon // RUNTIMEFUNCTION - prototype for implementation of a runtime function that is not a service
1023f234f5bSNigel Croxon // RUNTIME_CODE - pragma macro for declaring runtime code
1033f234f5bSNigel Croxon //
1043f234f5bSNigel Croxon
1053f234f5bSNigel Croxon #ifndef EFIAPI // Forces EFI calling conventions reguardless of compiler options
1063f234f5bSNigel Croxon #define EFIAPI // Substitute expresion to force C calling convention
1073f234f5bSNigel Croxon #endif
1083f234f5bSNigel Croxon
1093f234f5bSNigel Croxon #define BOOTSERVICE
1103f234f5bSNigel Croxon #define RUNTIMESERVICE
1113f234f5bSNigel Croxon #define RUNTIMEFUNCTION
1123f234f5bSNigel Croxon
1133f234f5bSNigel Croxon
1143f234f5bSNigel Croxon #define RUNTIME_CODE(a) alloc_text("rtcode", a)
1153f234f5bSNigel Croxon #define BEGIN_RUNTIME_DATA() data_seg("rtdata")
1163f234f5bSNigel Croxon #define END_RUNTIME_DATA() data_seg("")
1173f234f5bSNigel Croxon
1183f234f5bSNigel Croxon #define VOLATILE volatile
1193f234f5bSNigel Croxon
1203f234f5bSNigel Croxon #define MEMORY_FENCE __sync_synchronize
1213f234f5bSNigel Croxon
1223f234f5bSNigel Croxon //
1233f234f5bSNigel Croxon // When build similiar to FW, then link everything together as
1243f234f5bSNigel Croxon // one big module.
1253f234f5bSNigel Croxon //
1263f234f5bSNigel Croxon
1273f234f5bSNigel Croxon #define EFI_DRIVER_ENTRY_POINT(InitFunction) \
1283f234f5bSNigel Croxon UINTN \
1293f234f5bSNigel Croxon InitializeDriver ( \
1303f234f5bSNigel Croxon VOID *ImageHandle, \
1313f234f5bSNigel Croxon VOID *SystemTable \
1323f234f5bSNigel Croxon ) \
1333f234f5bSNigel Croxon { \
1343f234f5bSNigel Croxon return InitFunction(ImageHandle, \
1353f234f5bSNigel Croxon SystemTable); \
1363f234f5bSNigel Croxon } \
1373f234f5bSNigel Croxon \
1383f234f5bSNigel Croxon EFI_STATUS efi_main( \
1393f234f5bSNigel Croxon EFI_HANDLE image, \
1403f234f5bSNigel Croxon EFI_SYSTEM_TABLE *systab \
1413f234f5bSNigel Croxon ) __attribute__((weak, \
1423f234f5bSNigel Croxon alias ("InitializeDriver")));
1433f234f5bSNigel Croxon
1443f234f5bSNigel Croxon #define LOAD_INTERNAL_DRIVER(_if, type, name, entry) \
1453f234f5bSNigel Croxon (_if)->LoadInternal(type, name, entry)
1463f234f5bSNigel Croxon
1473f234f5bSNigel Croxon
1483f234f5bSNigel Croxon //
1493f234f5bSNigel Croxon // Some compilers don't support the forward reference construct:
1503f234f5bSNigel Croxon // typedef struct XXXXX
1513f234f5bSNigel Croxon //
1523f234f5bSNigel Croxon // The following macro provide a workaround for such cases.
1533f234f5bSNigel Croxon
1543f234f5bSNigel Croxon #define INTERFACE_DECL(x) struct x
1553f234f5bSNigel Croxon
1563f234f5bSNigel Croxon #define uefi_call_wrapper(func, va_num, ...) func(__VA_ARGS__)
1573f234f5bSNigel Croxon #define EFI_FUNCTION
1583f234f5bSNigel Croxon
swap_uint64(UINT64 v)1593f234f5bSNigel Croxon static inline UINT64 swap_uint64 (UINT64 v)
1603f234f5bSNigel Croxon {
1613f234f5bSNigel Croxon asm volatile (
1623f234f5bSNigel Croxon "dsbh %[v], %[v] \n\t"
1633f234f5bSNigel Croxon "dshd %[v], %[v] \n\t"
1643f234f5bSNigel Croxon :[v]"+r"(v)
1653f234f5bSNigel Croxon );
1663f234f5bSNigel Croxon
1673f234f5bSNigel Croxon return v;
1683f234f5bSNigel Croxon }
169