BaseTools: Add DevicePath support for PCD values
[mirror_edk2.git] / BaseTools / Source / C / Common / CommonLib.h
1 /** @file
2 Common library assistance routines.
3
4 Copyright (c) 2004 - 2017, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 #ifndef _EFI_COMMON_LIB_H
16 #define _EFI_COMMON_LIB_H
17
18 #include <Common/UefiBaseTypes.h>
19 #include <Common/BuildVersion.h>
20 #include <assert.h>
21 #define PRINTED_GUID_BUFFER_SIZE 37 // including null-termination
22
23 #define MAX_LONG_FILE_PATH 500
24
25 #define MAX_UINTN MAX_ADDRESS
26 #define MAX_UINT64 ((UINT64)0xFFFFFFFFFFFFFFFFULL)
27 #define MAX_UINT16 ((UINT16)0xFFFF)
28 #define MAX_UINT8 ((UINT8)0xFF)
29 #define ARRAY_SIZE(Array) (sizeof (Array) / sizeof ((Array)[0]))
30 #define ASCII_RSIZE_MAX 1000000
31 #ifndef RSIZE_MAX
32 #define RSIZE_MAX 1000000
33 #endif
34
35 #define IS_COMMA(a) ((a) == L',')
36 #define IS_HYPHEN(a) ((a) == L'-')
37 #define IS_DOT(a) ((a) == L'.')
38 #define IS_LEFT_PARENTH(a) ((a) == L'(')
39 #define IS_RIGHT_PARENTH(a) ((a) == L')')
40 #define IS_SLASH(a) ((a) == L'/')
41 #define IS_NULL(a) ((a) == L'\0')
42
43 #define ASSERT(x) assert(x)
44
45 #ifdef __cplusplus
46 extern "C" {
47 #endif
48
49 //
50 // Function declarations
51 //
52 VOID
53 PeiZeroMem (
54 IN VOID *Buffer,
55 IN UINTN Size
56 )
57 ;
58
59 VOID
60 PeiCopyMem (
61 IN VOID *Destination,
62 IN VOID *Source,
63 IN UINTN Length
64 )
65 ;
66
67 VOID
68 ZeroMem (
69 IN VOID *Buffer,
70 IN UINTN Size
71 )
72 ;
73
74 VOID
75 CopyMem (
76 IN VOID *Destination,
77 IN VOID *Source,
78 IN UINTN Length
79 )
80 ;
81
82 INTN
83 CompareGuid (
84 IN EFI_GUID *Guid1,
85 IN EFI_GUID *Guid2
86 )
87 ;
88
89 EFI_STATUS
90 GetFileImage (
91 IN CHAR8 *InputFileName,
92 OUT CHAR8 **InputFileImage,
93 OUT UINT32 *BytesRead
94 )
95 ;
96
97 EFI_STATUS
98 PutFileImage (
99 IN CHAR8 *OutputFileName,
100 IN CHAR8 *OutputFileImage,
101 IN UINT32 BytesToWrite
102 )
103 ;
104 /*++
105
106 Routine Description:
107
108 This function opens a file and writes OutputFileImage into the file.
109
110 Arguments:
111
112 OutputFileName The name of the file to write.
113 OutputFileImage A pointer to the memory buffer.
114 BytesToWrite The size of the memory buffer.
115
116 Returns:
117
118 EFI_SUCCESS The function completed successfully.
119 EFI_INVALID_PARAMETER One of the input parameters was invalid.
120 EFI_ABORTED An error occurred.
121 EFI_OUT_OF_RESOURCES No resource to complete operations.
122
123 **/
124
125 UINT8
126 CalculateChecksum8 (
127 IN UINT8 *Buffer,
128 IN UINTN Size
129 )
130 ;
131
132 UINT8
133 CalculateSum8 (
134 IN UINT8 *Buffer,
135 IN UINTN Size
136 )
137 ;
138
139 UINT16
140 CalculateChecksum16 (
141 IN UINT16 *Buffer,
142 IN UINTN Size
143 )
144 ;
145
146 UINT16
147 CalculateSum16 (
148 IN UINT16 *Buffer,
149 IN UINTN Size
150 )
151 ;
152
153 EFI_STATUS
154 PrintGuid (
155 IN EFI_GUID *Guid
156 )
157 ;
158
159 #define PRINTED_GUID_BUFFER_SIZE 37 // including null-termination
160 EFI_STATUS
161 PrintGuidToBuffer (
162 IN EFI_GUID *Guid,
163 IN OUT UINT8 *Buffer,
164 IN UINT32 BufferLen,
165 IN BOOLEAN Uppercase
166 )
167 ;
168
169 CHAR8 *
170 LongFilePath (
171 IN CHAR8 *FileName
172 );
173
174 UINTN
175 StrLen (
176 CONST CHAR16 *String
177 );
178
179 VOID *
180 AllocateCopyPool (
181 UINTN AllocationSize,
182 CONST VOID *Buffer
183 );
184
185 INTN
186 StrnCmp (
187 CONST CHAR16 *FirstString,
188 CONST CHAR16 *SecondString,
189 UINTN Length
190 );
191
192 RETURN_STATUS
193 StrToGuid (
194 CONST CHAR16 *String,
195 EFI_GUID *Guid
196 );
197
198 RETURN_STATUS
199 StrHexToBytes (
200 CONST CHAR16 *String,
201 UINTN Length,
202 UINT8 *Buffer,
203 UINTN MaxBufferSize
204 );
205
206 UINTN
207 InternalHexCharToUintn (
208 CHAR16 Char
209 );
210
211 VOID *
212 InternalAllocateCopyPool (
213 UINTN AllocationSize,
214 CONST VOID *Buffer
215 );
216
217 BOOLEAN
218 InternalIsDecimalDigitCharacter (
219 CHAR16 Char
220 );
221
222 UINT32
223 SwapBytes32 (
224 UINT32 Value
225 );
226
227 UINT16
228 SwapBytes16 (
229 UINT16 Value
230 );
231
232 EFI_GUID *
233 CopyGuid (
234 EFI_GUID *DestinationGuid,
235 CONST EFI_GUID *SourceGuid
236 );
237
238 UINT64
239 WriteUnaligned64 (
240 UINT64 *Buffer,
241 UINT64 Value
242 );
243
244 UINT64
245 ReadUnaligned64 (
246 CONST UINT64 *Buffer
247 );
248
249 UINTN
250 StrSize (
251 CONST CHAR16 *String
252 );
253
254 UINTN
255 StrHexToUintn (
256 CONST CHAR16 *String
257 );
258
259 UINTN
260 StrDecimalToUintn (
261 CONST CHAR16 *String
262 );
263
264 UINT64
265 StrHexToUint64 (
266 CONST CHAR16 *String
267 );
268
269 UINT64
270 StrDecimalToUint64 (
271 CONST CHAR16 *String
272 );
273
274 RETURN_STATUS
275 StrHexToUint64S (
276 CONST CHAR16 *String,
277 CHAR16 **EndPointer,
278 UINT64 *Data
279 );
280
281 RETURN_STATUS
282 StrHexToUintnS (
283 CONST CHAR16 *String,
284 CHAR16 **EndPointer, OPTIONAL
285 UINTN *Data
286 );
287
288 RETURN_STATUS
289 StrDecimalToUint64S (
290 CONST CHAR16 *String,
291 CHAR16 **EndPointer, OPTIONAL
292 UINT64 *Data
293 );
294
295 RETURN_STATUS
296 StrDecimalToUintnS (
297 CONST CHAR16 *String,
298 CHAR16 **EndPointer, OPTIONAL
299 UINTN *Data
300 );
301
302 VOID *
303 ReallocatePool (
304 UINTN OldSize,
305 UINTN NewSize,
306 VOID *OldBuffer OPTIONAL
307 );
308
309 VOID *
310 InternalReallocatePool (
311 UINTN OldSize,
312 UINTN NewSize,
313 VOID *OldBuffer OPTIONAL
314 );
315
316 VOID *
317 InternalAllocateZeroPool (
318 UINTN AllocationSize
319 ) ;
320
321 VOID *
322 InternalAllocatePool (
323 UINTN AllocationSize
324 );
325
326 UINTN
327 StrnLenS (
328 CONST CHAR16 *String,
329 UINTN MaxSize
330 );
331
332 CHAR16
333 InternalCharToUpper (
334 CHAR16 Char
335 );
336
337 INTN
338 StrCmp (
339 CONST CHAR16 *FirstString,
340 CONST CHAR16 *SecondString
341 );
342
343 UINT64
344 SwapBytes64 (
345 UINT64 Value
346 );
347
348 UINT64
349 InternalMathSwapBytes64 (
350 UINT64 Operand
351 );
352
353 RETURN_STATUS
354 StrToIpv4Address (
355 CONST CHAR16 *String,
356 CHAR16 **EndPointer,
357 EFI_IPv4_ADDRESS *Address,
358 UINT8 *PrefixLength
359 );
360
361 RETURN_STATUS
362 StrToIpv6Address (
363 CONST CHAR16 *String,
364 CHAR16 **EndPointer,
365 EFI_IPv6_ADDRESS *Address,
366 UINT8 *PrefixLength
367 );
368
369 RETURN_STATUS
370 StrCpyS (
371 CHAR16 *Destination,
372 UINTN DestMax,
373 CONST CHAR16 *Source
374 );
375
376 RETURN_STATUS
377 UnicodeStrToAsciiStrS (
378 CONST CHAR16 *Source,
379 CHAR8 *Destination,
380 UINTN DestMax
381 );
382 VOID *
383 AllocatePool (
384 UINTN AllocationSize
385 );
386
387 UINT16
388 WriteUnaligned16 (
389 UINT16 *Buffer,
390 UINT16 Value
391 );
392
393 UINT16
394 ReadUnaligned16 (
395 CONST UINT16 *Buffer
396 );
397
398 VOID *
399 AllocateZeroPool (
400 UINTN AllocationSize
401 );
402
403 BOOLEAN
404 InternalIsHexaDecimalDigitCharacter (
405 CHAR16 Char
406 );
407
408 BOOLEAN
409 InternalSafeStringIsOverlap (
410 IN VOID *Base1,
411 IN UINTN Size1,
412 IN VOID *Base2,
413 IN UINTN Size2
414 );
415
416 BOOLEAN
417 InternalSafeStringNoStrOverlap (
418 IN CHAR16 *Str1,
419 IN UINTN Size1,
420 IN CHAR16 *Str2,
421 IN UINTN Size2
422 );
423
424 BOOLEAN
425 IsHexStr (
426 CHAR16 *Str
427 );
428
429 UINTN
430 Strtoi (
431 CHAR16 *Str
432 );
433
434 VOID
435 Strtoi64 (
436 CHAR16 *Str,
437 UINT64 *Data
438 );
439
440 VOID
441 StrToAscii (
442 CHAR16 *Str,
443 CHAR8 **AsciiStr
444 );
445
446 CHAR16 *
447 SplitStr (
448 CHAR16 **List,
449 CHAR16 Separator
450 );
451
452 /*++
453
454 Routine Description:
455 Convert FileName to the long file path, which can support larger than 260 length.
456
457 Arguments:
458 FileName - FileName.
459
460 Returns:
461 LongFilePath A pointer to the converted long file path.
462
463 --*/
464
465 #ifdef __cplusplus
466 }
467 #endif
468
469 #ifdef __GNUC__
470 #include <stdio.h>
471 #include <sys/stat.h>
472 #define stricmp strcasecmp
473 #define _stricmp strcasecmp
474 #define strnicmp strncasecmp
475 #define strcmpi strcasecmp
476 size_t _filelength(int fd);
477 #ifndef __CYGWIN__
478 char *strlwr(char *s);
479 #endif
480 #endif
481
482 //
483 // On windows, mkdir only has one parameter.
484 // On unix, it has two parameters
485 //
486 #if defined(__GNUC__)
487 #define mkdir(dir, perm) mkdir(dir, perm)
488 #else
489 #define mkdir(dir, perm) mkdir(dir)
490 #endif
491
492 #endif