1 /* basetypes.h
2 
3 Copyright (c) 2016, Nikolaj Schlej. All rights reserved.
4 This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution.  The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
8 
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11 
12 */
13 
14 #ifndef BASETYPES_H
15 #define BASETYPES_H
16 
17 #include <stdarg.h>
18 #include <stdint.h>
19 #include <stddef.h>
20 
21 typedef size_t USTATUS;
22 #define U_SUCCESS                         0
23 #define U_INVALID_PARAMETER               1
24 #define U_BUFFER_TOO_SMALL                2
25 #define U_OUT_OF_RESOURCES                3
26 #define U_OUT_OF_MEMORY                   4
27 #define U_FILE_OPEN                       5
28 #define U_FILE_READ                       6
29 #define U_FILE_WRITE                      7
30 #define U_ITEM_NOT_FOUND                  8
31 #define U_UNKNOWN_ITEM_TYPE               9
32 #define U_INVALID_FLASH_DESCRIPTOR        10
33 #define U_INVALID_REGION                  11
34 #define U_EMPTY_REGION                    12
35 #define U_BIOS_REGION_NOT_FOUND           13
36 #define U_VOLUMES_NOT_FOUND               14
37 #define U_INVALID_VOLUME                  15
38 #define U_VOLUME_REVISION_NOT_SUPPORTED   16
39 #define U_COMPLEX_BLOCK_MAP               17
40 #define U_UNKNOWN_FFS                     18
41 #define U_INVALID_FILE                    19
42 #define U_INVALID_SECTION                 20
43 #define U_UNKNOWN_SECTION                 21
44 #define U_STANDARD_COMPRESSION_FAILED     22
45 #define U_CUSTOMIZED_COMPRESSION_FAILED   23
46 #define U_STANDARD_DECOMPRESSION_FAILED   24
47 #define U_CUSTOMIZED_DECOMPRESSION_FAILED 25
48 #define U_GZIP_DECOMPRESSION_FAILED       26
49 #define U_UNKNOWN_COMPRESSION_TYPE        27
50 #define U_DEPEX_PARSE_FAILED              28
51 #define U_UNKNOWN_EXTRACT_MODE            29
52 #define U_UNKNOWN_REPLACE_MODE            30
53 #define U_UNKNOWN_IMAGE_TYPE              31
54 #define U_UNKNOWN_PE_OPTIONAL_HEADER_TYPE 32
55 #define U_UNKNOWN_RELOCATION_TYPE         33
56 #define U_DIR_ALREADY_EXIST               34
57 #define U_DIR_CREATE                      35
58 #define U_DIR_CHANGE                      36
59 #define U_TRUNCATED_IMAGE                 37
60 #define U_INVALID_CAPSULE                 38
61 #define U_STORES_NOT_FOUND                39
62 #define U_INVALID_IMAGE                   40
63 #define U_INVALID_RAW_AREA                41
64 #define U_INVALID_FIT                     42
65 #define U_INVALID_MICROCODE               43
66 #define U_INVALID_ACM                     44
67 #define U_INVALID_BG_KEY_MANIFEST         45
68 #define U_INVALID_BG_BOOT_POLICY          46
69 #define U_INVALID_TXT_CONF                47
70 #define U_ELEMENTS_NOT_FOUND              48
71 #define U_PEI_CORE_ENTRY_POINT_NOT_FOUND  49
72 #define U_INVALID_STORE_SIZE              50
73 #define U_UNKNOWN_COMPRESSION_ALGORITHM   51
74 #define U_NOTHING_TO_PATCH                52
75 #define U_UNKNOWN_PATCH_TYPE              53
76 #define U_PATCH_OFFSET_OUT_OF_BOUNDS      54
77 #define U_INVALID_SYMBOL                  55
78 
79 #define U_INVALID_MANIFEST                251
80 #define U_UNKNOWN_MANIFEST_HEADER_VERSION 252
81 #define U_INVALID_ME_PARTITION_TABLE      253
82 #define U_INVALID_ME_PARTITION            254
83 
84 #define U_NOT_IMPLEMENTED                 0xFF
85 
86 // EDK2 porting definitions
87 typedef uint8_t      BOOLEAN;
88 typedef int8_t       INT8;
89 typedef uint8_t      UINT8;
90 typedef int16_t      INT16;
91 typedef uint16_t     UINT16;
92 typedef int32_t      INT32;
93 typedef uint32_t     UINT32;
94 typedef int64_t      INT64;
95 typedef uint64_t     UINT64;
96 typedef char         CHAR8;
97 typedef uint16_t     CHAR16;
98 typedef size_t       UINTN;
99 typedef ptrdiff_t    INTN;
100 
101 #define CONST  const
102 #define VOID   void
103 #define STATIC static
104 
105 #ifndef INT32_MAX
106 #define INT32_MAX 0x7fffffff
107 #endif
108 
109 #ifndef TRUE
110 #define TRUE  ((BOOLEAN)(1==1))
111 #endif
112 
113 #ifndef FALSE
114 #define FALSE ((BOOLEAN)(0==1))
115 #endif
116 
117 #define IN
118 #define OUT
119 #define EFIAPI
120 #define EFI_STATUS UINTN
121 #define EFI_SUCCESS U_SUCCESS
122 #define EFI_INVALID_PARAMETER U_INVALID_PARAMETER
123 #define EFI_OUT_OF_RESOURCES U_OUT_OF_RESOURCES
124 #define EFI_BUFFER_TOO_SMALL U_BUFFER_TOO_SMALL
125 #define EFI_ERROR(X) (X)
126 
127 // Compression algorithms
128 #define COMPRESSION_ALGORITHM_UNKNOWN                0
129 #define COMPRESSION_ALGORITHM_NONE                   1
130 #define COMPRESSION_ALGORITHM_EFI11                  2
131 #define COMPRESSION_ALGORITHM_TIANO                  3
132 #define COMPRESSION_ALGORITHM_UNDECIDED              4
133 #define COMPRESSION_ALGORITHM_LZMA                   5
134 #define COMPRESSION_ALGORITHM_LZMA_INTEL_LEGACY      6
135 #define COMPRESSION_ALGORITHM_LZMAF86                7
136 #define COMPRESSION_ALGORITHM_GZIP                   8
137 
138 
139 // Item create modes
140 #define CREATE_MODE_APPEND    0
141 #define CREATE_MODE_PREPEND   1
142 #define CREATE_MODE_BEFORE    2
143 #define CREATE_MODE_AFTER     3
144 
145 // Item extract modes
146 #define EXTRACT_MODE_AS_IS                 0
147 #define EXTRACT_MODE_BODY                  1
148 #define EXTRACT_MODE_BODY_UNCOMPRESSED     2
149 
150 // Item replace modes
151 #define REPLACE_MODE_AS_IS    0
152 #define REPLACE_MODE_BODY     1
153 
154 // Item patch modes
155 #define PATCH_MODE_HEADER     0
156 #define PATCH_MODE_BODY       1
157 
158 // Patch types
159 #define PATCH_TYPE_OFFSET    'O'
160 #define PATCH_TYPE_PATTERN   'P'
161 
162 // Erase polarity types
163 #define ERASE_POLARITY_FALSE   0
164 #define ERASE_POLARITY_TRUE    1
165 #define ERASE_POLARITY_UNKNOWN 0xFF
166 
167 // Search modes
168 #define SEARCH_MODE_HEADER    1
169 #define SEARCH_MODE_BODY      2
170 #define SEARCH_MODE_ALL       3
171 
172 // EFI GUID
173 typedef struct EFI_GUID_ {
174     UINT32 Data1;
175     UINT16 Data2;
176     UINT16 Data3;
177     UINT8  Data4[8];
178 } EFI_GUID;
179 
180 // EFI Time
181 typedef struct EFI_TIME_ {
182     UINT16  Year;       // Year:       2000 - 20XX
183     UINT8   Month;      // Month:      1 - 12
184     UINT8   Day;        // Day:        1 - 31
185     UINT8   Hour;       // Hour:       0 - 23
186     UINT8   Minute;     // Minute:     0 - 59
187     UINT8   Second;     // Second:     0 - 59
188     UINT8   Reserved0;
189     UINT32  Nanosecond; // Nanosecond: 0 - 999,999,999
190     INT16   TimeZone;   // TimeZone:   -1440 to 1440 or UNSPECIFIED (0x07FF)
191     UINT8   Daylight;   // Daylight:   ADJUST_DAYLIGHT (1) or IN_DAYLIGHT (2)
192     UINT8   Reserved1;
193 } EFI_TIME;
194 
195 // Align to 4 or 8 bytes
196 #define ALIGN4(Value) (((Value)+3) & ~3)
197 #define ALIGN8(Value) (((Value)+7) & ~7)
198 
199 // Unused parameter declaration
200 #define U_UNUSED_PARAMETER(x) ((void)x)
201 
202 // Assert macro
203 #include <assert.h>
204 #define ASSERT(x) assert(x)
205 
206 // SHA256 hash size in bytes
207 #define SHA256_HASH_SIZE 0x20
208 
209 #endif // BASETYPES_H
210