1 /**
2 	Based on Base.h file, original notice:
3 
4 	Root include file for Mde Package Base type modules
5 
6 	This is the include file for any module of type base. Base modules only use
7 	types defined via this include file and can be ported easily to any
8 	environment. There are a set of base libraries in the Mde Package that can
9 	be used to implement base modules.
10 
11 	Copyright (c) 2006 - 2015, Intel Corporation. All rights reserved.
12 	This program and the accompanying materials
13 	are licensed and made available under the terms and conditions of the BSD License
14 	which accompanies this distribution.  The full text of the license may be found at
15 	http://opensource.org/licenses/bsd-license.php
16 
17 	THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
18 	WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
19 **/
20 module uefi.base;
21 
22 public import uefi.bind;
23 
24 public:
25 extern (C):
26 
27 /// 128 bit buffer containing a unique identifier value.
28 /// Unless otherwise specified, aligned on a 64 bit boundary.
29 struct GUID
30 {
31     UINT32 Data1;
32     UINT16 Data2;
33     UINT16 Data3;
34     UINT8[8] Data4;
35 }
36 
37 /// 8-bytes unsigned value that represents a physical system address.
38 alias PHYSICAL_ADDRESS = UINT64;
39 
40 /// LIST_ENTRY structure definition.
41 struct LIST_ENTRY
42 {
43     LIST_ENTRY* ForwardLink;
44     LIST_ENTRY* BackwardLink;
45 }
46 
47 /// Boolean true value.
48 enum BOOLEAN TRUE = 1;
49 
50 /// Boolean false value.
51 enum BOOLEAN FALSE = 0;
52 
53 /// 
54 enum : UINT32
55 {
56     BIT0 = 0x00000001,
57     BIT1 = 0x00000002,
58     BIT2 = 0x00000004,
59     BIT3 = 0x00000008,
60     BIT4 = 0x00000010,
61     BIT5 = 0x00000020,
62     BIT6 = 0x00000040,
63     BIT7 = 0x00000080,
64     BIT8 = 0x00000100,
65     BIT9 = 0x00000200,
66     BIT10 = 0x00000400,
67     BIT11 = 0x00000800,
68     BIT12 = 0x00001000,
69     BIT13 = 0x00002000,
70     BIT14 = 0x00004000,
71     BIT15 = 0x00008000,
72     BIT16 = 0x00010000,
73     BIT17 = 0x00020000,
74     BIT18 = 0x00040000,
75     BIT19 = 0x00080000,
76     BIT20 = 0x00100000,
77     BIT21 = 0x00200000,
78     BIT22 = 0x00400000,
79     BIT23 = 0x00800000,
80     BIT24 = 0x01000000,
81     BIT25 = 0x02000000,
82     BIT26 = 0x04000000,
83     BIT27 = 0x08000000,
84     BIT28 = 0x10000000,
85     BIT29 = 0x20000000,
86     BIT30 = 0x40000000,
87     BIT31 = 0x80000000,
88 
89     SIZE_1KB = 0x00000400,
90     SIZE_2KB = 0x00000800,
91     SIZE_4KB = 0x00001000,
92     SIZE_8KB = 0x00002000,
93     SIZE_16KB = 0x00004000,
94     SIZE_32KB = 0x00008000,
95     SIZE_64KB = 0x00010000,
96     SIZE_128KB = 0x00020000,
97     SIZE_256KB = 0x00040000,
98     SIZE_512KB = 0x00080000,
99     SIZE_1MB = 0x00100000,
100     SIZE_2MB = 0x00200000,
101     SIZE_4MB = 0x00400000,
102     SIZE_8MB = 0x00800000,
103     SIZE_16MB = 0x01000000,
104     SIZE_32MB = 0x02000000,
105     SIZE_64MB = 0x04000000,
106     SIZE_128MB = 0x08000000,
107     SIZE_256MB = 0x10000000,
108     SIZE_512MB = 0x20000000,
109     SIZE_1GB = 0x40000000,
110     SIZE_2GB = 0x80000000,
111 
112     BASE_1KB = 0x00000400,
113     BASE_2KB = 0x00000800,
114     BASE_4KB = 0x00001000,
115     BASE_8KB = 0x00002000,
116     BASE_16KB = 0x00004000,
117     BASE_32KB = 0x00008000,
118     BASE_64KB = 0x00010000,
119     BASE_128KB = 0x00020000,
120     BASE_256KB = 0x00040000,
121     BASE_512KB = 0x00080000,
122     BASE_1MB = 0x00100000,
123     BASE_2MB = 0x00200000,
124     BASE_4MB = 0x00400000,
125     BASE_8MB = 0x00800000,
126     BASE_16MB = 0x01000000,
127     BASE_32MB = 0x02000000,
128     BASE_64MB = 0x04000000,
129     BASE_128MB = 0x08000000,
130     BASE_256MB = 0x10000000,
131     BASE_512MB = 0x20000000,
132     BASE_1GB = 0x40000000,
133     BASE_2GB = 0x80000000
134 }
135 /// ditto
136 enum : UINT64
137 {
138     BIT32 = 0x0000000100000000UL,
139     BIT33 = 0x0000000200000000UL,
140     BIT34 = 0x0000000400000000UL,
141     BIT35 = 0x0000000800000000UL,
142     BIT36 = 0x0000001000000000UL,
143     BIT37 = 0x0000002000000000UL,
144     BIT38 = 0x0000004000000000UL,
145     BIT39 = 0x0000008000000000UL,
146     BIT40 = 0x0000010000000000UL,
147     BIT41 = 0x0000020000000000UL,
148     BIT42 = 0x0000040000000000UL,
149     BIT43 = 0x0000080000000000UL,
150     BIT44 = 0x0000100000000000UL,
151     BIT45 = 0x0000200000000000UL,
152     BIT46 = 0x0000400000000000UL,
153     BIT47 = 0x0000800000000000UL,
154     BIT48 = 0x0001000000000000UL,
155     BIT49 = 0x0002000000000000UL,
156     BIT50 = 0x0004000000000000UL,
157     BIT51 = 0x0008000000000000UL,
158     BIT52 = 0x0010000000000000UL,
159     BIT53 = 0x0020000000000000UL,
160     BIT54 = 0x0040000000000000UL,
161     BIT55 = 0x0080000000000000UL,
162     BIT56 = 0x0100000000000000UL,
163     BIT57 = 0x0200000000000000UL,
164     BIT58 = 0x0400000000000000UL,
165     BIT59 = 0x0800000000000000UL,
166     BIT60 = 0x1000000000000000UL,
167     BIT61 = 0x2000000000000000UL,
168     BIT62 = 0x4000000000000000UL,
169     BIT63 = 0x8000000000000000UL,
170 
171     SIZE_4GB = 0x0000000100000000UL,
172     SIZE_8GB = 0x0000000200000000UL,
173     SIZE_16GB = 0x0000000400000000UL,
174     SIZE_32GB = 0x0000000800000000UL,
175     SIZE_64GB = 0x0000001000000000UL,
176     SIZE_128GB = 0x0000002000000000UL,
177     SIZE_256GB = 0x0000004000000000UL,
178     SIZE_512GB = 0x0000008000000000UL,
179     SIZE_1TB = 0x0000010000000000UL,
180     SIZE_2TB = 0x0000020000000000UL,
181     SIZE_4TB = 0x0000040000000000UL,
182     SIZE_8TB = 0x0000080000000000UL,
183     SIZE_16TB = 0x0000100000000000UL,
184     SIZE_32TB = 0x0000200000000000UL,
185     SIZE_64TB = 0x0000400000000000UL,
186     SIZE_128TB = 0x0000800000000000UL,
187     SIZE_256TB = 0x0001000000000000UL,
188     SIZE_512TB = 0x0002000000000000UL,
189     SIZE_1PB = 0x0004000000000000UL,
190     SIZE_2PB = 0x0008000000000000UL,
191     SIZE_4PB = 0x0010000000000000UL,
192     SIZE_8PB = 0x0020000000000000UL,
193     SIZE_16PB = 0x0040000000000000UL,
194     SIZE_32PB = 0x0080000000000000UL,
195     SIZE_64PB = 0x0100000000000000UL,
196     SIZE_128PB = 0x0200000000000000UL,
197     SIZE_256PB = 0x0400000000000000UL,
198     SIZE_512PB = 0x0800000000000000UL,
199     SIZE_1EB = 0x1000000000000000UL,
200     SIZE_2EB = 0x2000000000000000UL,
201     SIZE_4EB = 0x4000000000000000UL,
202     SIZE_8EB = 0x8000000000000000UL,
203 
204     BASE_4GB = 0x0000000100000000UL,
205     BASE_8GB = 0x0000000200000000UL,
206     BASE_16GB = 0x0000000400000000UL,
207     BASE_32GB = 0x0000000800000000UL,
208     BASE_64GB = 0x0000001000000000UL,
209     BASE_128GB = 0x0000002000000000UL,
210     BASE_256GB = 0x0000004000000000UL,
211     BASE_512GB = 0x0000008000000000UL,
212     BASE_1TB = 0x0000010000000000UL,
213     BASE_2TB = 0x0000020000000000UL,
214     BASE_4TB = 0x0000040000000000UL,
215     BASE_8TB = 0x0000080000000000UL,
216     BASE_16TB = 0x0000100000000000UL,
217     BASE_32TB = 0x0000200000000000UL,
218     BASE_64TB = 0x0000400000000000UL,
219     BASE_128TB = 0x0000800000000000UL,
220     BASE_256TB = 0x0001000000000000UL,
221     BASE_512TB = 0x0002000000000000UL,
222     BASE_1PB = 0x0004000000000000UL,
223     BASE_2PB = 0x0008000000000000UL,
224     BASE_4PB = 0x0010000000000000UL,
225     BASE_8PB = 0x0020000000000000UL,
226     BASE_16PB = 0x0040000000000000UL,
227     BASE_32PB = 0x0080000000000000UL,
228     BASE_64PB = 0x0100000000000000UL,
229     BASE_128PB = 0x0200000000000000UL,
230     BASE_256PB = 0x0400000000000000UL,
231     BASE_512PB = 0x0800000000000000UL,
232     BASE_1EB = 0x1000000000000000UL,
233     BASE_2EB = 0x2000000000000000UL,
234     BASE_4EB = 0x4000000000000000UL,
235     BASE_8EB = 0x8000000000000000UL
236 }
237 
238 /// Variable used to traverse the list of arguments. This type can vary by
239 /// implementation and could be an array or structure.
240 alias VA_LIST = ubyte*;
241 
242 /// Pointer to the start of a variable argument list stored in a memory buffer. Same as UINT8 *.
243 alias BASE_LIST = UINTN*;
244 
245 /**
246   Rounds a value up to the next boundary using a specified alignment.
247 
248   This function rounds Value up to the next boundary using the specified Alignment.
249   This aligned value is returned.
250 
251   Params:
252      Value     = The value to round up.
253      Alignment = The alignment boundary used to return the aligned value.
254 
255   Returns:  A value up to the next boundary.
256 
257 **/
258 auto ALIGN_VALUE(T)(T Value, T Alignment)
259 {
260     return ((Value) + (((Alignment) - (Value)) & ((Alignment) - 1)));
261 }
262 
263 /**
264   Adjust a pointer by adding the minimum offset required for it to be aligned on
265   a specified alignment boundary.
266 
267   This function rounds the pointer specified by Pointer to the next alignment boundary
268   specified by Alignment. The pointer to the aligned address is returned.
269 
270   Params:
271      Pointer    The pointer to round up.
272      Alignment  The alignment boundary to use to return an aligned pointer.
273 
274   Returns:  Pointer to the aligned address.
275 
276 **/
277 T* ALIGN_POINTER(T)(T* Pointer, INTN Alignment)
278 {
279     return (cast(VOID*)(ALIGN_VALUE(cast(UINTN)(Pointer), (Alignment))));
280 }
281 
282 /**
283   Rounds a value up to the next natural boundary for the current CPU.
284   This is 4-bytes for 32-bit CPUs and 8-bytes for 64-bit CPUs.
285 
286   This function rounds the value specified by Value up to the next natural boundary for the
287   current CPU. This rounded value is returned.
288 
289   @param   Value      The value to round up.
290 
291   @return  Rounded value specified by Value.
292 
293 **/
294 auto ALIGN_VARIABLE(T)(T Value)
295 {
296     return ALIGN_VALUE((Value), UINTN.sizeof);
297 }
298 
299 /**
300   Return the maximum of two operands.
301 
302   Params:
303      a     =   The first operand with any numerical type.
304      b     =   The second operand.
305 
306   Returns:  Maximum of two operands.
307 
308 **/
309 auto MAX(T, U)(T A, U B)
310 {
311     return (A > B) ? A : B;
312 }
313 
314 /**
315   Return the minimum of two operands.
316 
317   Params:
318      a     =   The first operand with any numerical type.
319      b     =   The second operand.
320 
321   Returns:  Minimum of two operands.
322 
323 **/
324 auto MIN(T, U)(T A, U B)
325 {
326     return (A < B) ? A : B;
327 }
328 
329 /**
330   Return the absolute value of a signed operand.
331 
332   Params:
333      a     =   The signed operand.
334 
335   Returns:  The absolute value of the signed operand.
336 
337 **/
338 auto ABS(T)(T A)
339 {
340     return (A < 0) ? (-A) : (A);
341 }
342 
343 /// Status codes common to all execution phases
344 alias RETURN_STATUS = UINTN;
345 
346 /**
347   Returns true if a specified RETURN_STATUS code is an error code.
348 
349   This function returns TRUE if StatusCode has the high bit set.  Otherwise, false is returned.
350 
351   Params:
352     StatusCode  =  The status code value to evaluate.
353 
354   Returns: true if         The high bit of StatusCode is set.
355    false if        The high bit of StatusCode is clear.
356 
357 **/
358 bool RETURN_ERROR(T)(T StatusCode)
359 {
360     return cast(INTN)(cast(RETURN_STATUS)(StatusCode)) < 0;
361 }
362 
363 /**
364   Produces a RETURN_STATUS code with the highest bit set.
365 
366   Params:
367     StatusCode  =  The status code value to convert into a warning code.
368                    StatusCode must be in the range 0x00000000..0x7FFFFFFF.
369 
370   Returns: The value specified by StatusCode with the highest bit set.
371 
372 **/
373 template ENCODE_ERROR(UINTN StatusCode)
374 {
375     enum RETURN_STATUS ENCODE_ERROR = StatusCode | MAX_BIT;
376 }
377 
378 /**
379   Produces a RETURN_STATUS code with the highest bit clear.
380 
381   Params:
382     StatusCode  =  The status code value to convert into a warning code.
383                    StatusCode must be in the range 0x00000000..0x7FFFFFFF.
384 
385   Returns: The value specified by StatusCode with the highest bit clear.
386 
387 **/
388 template ENCODE_WARNING(UINTN StatusCode)
389 {
390     enum RETURN_STATUS ENCODE_WARNING = StatusCode | MAX_BIT;
391 }
392 
393 /// Return status codes
394 enum : RETURN_STATUS
395 {
396 
397     ///
398     /// The operation completed successfully.
399     ///
400     RETURN_SUCCESS = 0,
401     ///
402     /// The image failed to load.
403     ///
404     RETURN_LOAD_ERROR = ENCODE_ERROR!(1),
405 
406     ///
407     /// The parameter was incorrect.
408     ///
409     RETURN_INVALID_PARAMETER = ENCODE_ERROR!(2),
410 
411     ///
412     /// The operation is not supported.
413     ///
414     RETURN_UNSUPPORTED = ENCODE_ERROR!(3),
415 
416     ///
417     /// The buffer was not the proper size for the request.
418     ///
419     RETURN_BAD_BUFFER_SIZE = ENCODE_ERROR!(4),
420 
421     ///
422     /// The buffer was not large enough to hold the requested data.
423     /// The required buffer size is returned in the appropriate
424     /// parameter when this error occurs.
425     ///
426     RETURN_BUFFER_TOO_SMALL = ENCODE_ERROR!(5),
427 
428     ///
429     /// There is no data pending upon return.
430     ///
431     RETURN_NOT_READY = ENCODE_ERROR!(6),
432 
433     ///
434     /// The physical device reported an error while attempting the
435     /// operation.
436     ///
437     RETURN_DEVICE_ERROR = ENCODE_ERROR!(7),
438 
439     ///
440     /// The device can not be written to.
441     ///
442     RETURN_WRITE_PROTECTED = ENCODE_ERROR!(8),
443 
444     ///
445     /// The resource has run out.
446     ///
447     RETURN_OUT_OF_RESOURCES = ENCODE_ERROR!(9),
448 
449     ///
450     /// An inconsistency was detected on the file system causing the
451     /// operation to fail.
452     ///
453     RETURN_VOLUME_CORRUPTED = ENCODE_ERROR!(10),
454 
455     ///
456     /// There is no more space on the file system.
457     ///
458     RETURN_VOLUME_FULL = ENCODE_ERROR!(11),
459 
460     ///
461     /// The device does not contain any medium to perform the
462     /// operation.
463     ///
464     RETURN_NO_MEDIA = ENCODE_ERROR!(12),
465 
466     ///
467     /// The medium in the device has changed since the last
468     /// access.
469     ///
470     RETURN_MEDIA_CHANGED = ENCODE_ERROR!(13),
471 
472     ///
473     /// The item was not found.
474     ///
475     RETURN_NOT_FOUND = ENCODE_ERROR!(14),
476 
477     ///
478     /// Access was denied.
479     ///
480     RETURN_ACCESS_DENIED = ENCODE_ERROR!(15),
481 
482     ///
483     /// The server was not found or did not respond to the request.
484     ///
485     RETURN_NO_RESPONSE = ENCODE_ERROR!(16),
486 
487     ///
488     /// A mapping to the device does not exist.
489     ///
490     RETURN_NO_MAPPING = ENCODE_ERROR!(17),
491 
492     ///
493     /// A timeout time expired.
494     ///
495     RETURN_TIMEOUT = ENCODE_ERROR!(18),
496 
497     ///
498     /// The protocol has not been started.
499     ///
500     RETURN_NOT_STARTED = ENCODE_ERROR!(19),
501 
502     ///
503     /// The protocol has already been started.
504     ///
505     RETURN_ALREADY_STARTED = ENCODE_ERROR!(20),
506 
507     ///
508     /// The operation was aborted.
509     ///
510     RETURN_ABORTED = ENCODE_ERROR!(21),
511 
512     ///
513     /// An ICMP error occurred during the network operation.
514     ///
515     RETURN_ICMP_ERROR = ENCODE_ERROR!(22),
516 
517     ///
518     /// A TFTP error occurred during the network operation.
519     ///
520     RETURN_TFTP_ERROR = ENCODE_ERROR!(23),
521 
522     ///
523     /// A protocol error occurred during the network operation.
524     ///
525     RETURN_PROTOCOL_ERROR = ENCODE_ERROR!(24),
526 
527     ///
528     /// A function encountered an internal version that was
529     /// incompatible with a version requested by the caller.
530     ///
531     RETURN_INCOMPATIBLE_VERSION = ENCODE_ERROR!(25),
532 
533     ///
534     /// The function was not performed due to a security violation.
535     ///
536     RETURN_SECURITY_VIOLATION = ENCODE_ERROR!(26),
537 
538     ///
539     /// A CRC error was detected.
540     ///
541     RETURN_CRC_ERROR = ENCODE_ERROR!(27),
542 
543     ///
544     /// The beginning or end of media was reached.
545     ///
546     RETURN_END_OF_MEDIA = ENCODE_ERROR!(28),
547 
548     ///
549     /// The end of the file was reached.
550     ///
551     RETURN_END_OF_FILE = ENCODE_ERROR!(31),
552 
553     ///
554     /// The language specified was invalid.
555     ///
556     RETURN_INVALID_LANGUAGE = ENCODE_ERROR!(32),
557 
558     ///
559     /// The security status of the data is unknown or compromised
560     /// and the data must be updated or replaced to restore a valid
561     /// security status.
562     ///
563     RETURN_COMPROMISED_DATA = ENCODE_ERROR!(33),
564 
565     ///
566     /// The string contained one or more characters that
567     /// the device could not render and were skipped.
568     ///
569     RETURN_WARN_UNKNOWN_GLYPH = ENCODE_WARNING!(1),
570 
571     ///
572     /// The handle was closed, but the file was not deleted.
573     ///
574     RETURN_WARN_DELETE_FAILURE = ENCODE_WARNING!(2),
575 
576     ///
577     /// The handle was closed, but the data to the file was not
578     /// flushed properly.
579     ///
580     RETURN_WARN_WRITE_FAILURE = ENCODE_WARNING!(3),
581 
582     ///
583     /// The resulting buffer was too small, and the data was
584     /// truncated to the buffer size.
585     ///
586     RETURN_WARN_BUFFER_TOO_SMALL = ENCODE_WARNING!(4),
587 
588     ///
589     /// The data has not been updated within the timeframe set by
590     /// local policy for this type of data.
591     ///
592     RETURN_WARN_STALE_DATA = ENCODE_WARNING!(5),
593 
594 }
595 
596 /**
597   Returns a 16-bit signature built from 2 ASCII characters.
598 
599   This macro returns a 16-bit value built from the two ASCII characters specified
600   by A and B.
601 
602   Params:
603     A =   The first ASCII character.
604     B =   The second ASCII character.
605 
606   Returns: A 16-bit value built from the two ASCII characters specified by A and B.
607 
608 **/
609 auto SIGNATURE_16(T)(T A, T B)
610 {
611     return ((A) | (B << 8));
612 }
613 
614 /**
615   Returns a 32-bit signature built from 2 ASCII characters.
616 
617   This macro returns a 32-bit value built from the four ASCII characters specified
618   by A, B, C, and D.
619 
620   Params:
621     A =   The first ASCII character.
622     B =   The second ASCII character.
623 	C =   The third ASCII character.
624 	D =   The fourth ASCII character.
625 
626   Returns: A 32-bit value built from the two ASCII characters specified by A, B, C and D.
627 
628 **/
629 auto SIGNATURE_32(T)(T A, T B, T C, T D)
630 {
631     return (SIGNATURE_16(A, B) | (SIGNATURE_16(C, D) << 16));
632 }
633 
634 /**
635   Returns a 64-bit signature built from 8 ASCII characters.
636 
637   This macro returns a 64-bit value built from the eight ASCII characters specified
638   by A, B, C, D, E, F, G,and H.
639 
640   Params:
641     A  =  The first ASCII character.
642     B  =  The second ASCII character.
643     C  =  The third ASCII character.
644     D  =  The fourth ASCII character.
645     E  =  The fifth ASCII character.
646     F  =  The sixth ASCII character.
647     G  =  The seventh ASCII character.
648     H  =  The eighth ASCII character.
649 
650   Returns: A 64-bit value built from the two ASCII characters specified by A, B,
651           C, D, E, F, G and H.
652 
653 **/
654 auto SIGNATURE_64(T)(T A, T B, T C, T D, T E, T F, T G, T H)
655 {
656     return (SIGNATURE_32(A, B, C, D) | (cast(UINT64)(SIGNATURE_32(E, F, G, H)) << 32));
657 }