xref: /freebsd/sys/contrib/edk2/Include/Library/BaseLib.h (revision 580fcf642ea2d5a1e60f24947d1c2e97007e44e9)
1 /** @file
2   Provides string functions, linked list functions, math functions, synchronization
3   functions, file path functions, and CPU architecture-specific functions.
4 
5 Copyright (c) 2006 - 2021, Intel Corporation. All rights reserved.<BR>
6 Portions copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
7 Copyright (c) Microsoft Corporation.<BR>
8 Portions Copyright (c) 2020, Hewlett Packard Enterprise Development LP. All rights reserved.<BR>
9 Portions Copyright (c) 2022, Loongson Technology Corporation Limited. All rights reserved.<BR>
10 Copyright (c) 2023 - 2024, Arm Limited. All rights reserved.<BR>
11 
12 SPDX-License-Identifier: BSD-2-Clause-Patent
13 
14 **/
15 
16 #ifndef __BASE_LIB__
17 #define __BASE_LIB__
18 
19 //
20 // Definitions for architecture-specific types
21 //
22 #if   defined (MDE_CPU_IA32)
23 ///
24 /// The IA-32 architecture context buffer used by SetJump() and LongJump().
25 ///
26 typedef struct {
27   UINT32    Ebx;
28   UINT32    Esi;
29   UINT32    Edi;
30   UINT32    Ebp;
31   UINT32    Esp;
32   UINT32    Eip;
33   UINT32    Ssp;
34 } BASE_LIBRARY_JUMP_BUFFER;
35 
36 #define BASE_LIBRARY_JUMP_BUFFER_ALIGNMENT  4
37 
38 #endif // defined (MDE_CPU_IA32)
39 
40 #if defined (MDE_CPU_X64)
41 ///
42 /// The x64 architecture context buffer used by SetJump() and LongJump().
43 ///
44 typedef struct {
45   UINT64    Rbx;
46   UINT64    Rsp;
47   UINT64    Rbp;
48   UINT64    Rdi;
49   UINT64    Rsi;
50   UINT64    R12;
51   UINT64    R13;
52   UINT64    R14;
53   UINT64    R15;
54   UINT64    Rip;
55   UINT64    MxCsr;
56   UINT8     XmmBuffer[160];                         ///< XMM6-XMM15.
57   UINT64    Ssp;
58 } BASE_LIBRARY_JUMP_BUFFER;
59 
60 #define BASE_LIBRARY_JUMP_BUFFER_ALIGNMENT  8
61 
62 #endif // defined (MDE_CPU_X64)
63 
64 #if defined (MDE_CPU_EBC)
65 ///
66 /// The EBC context buffer used by SetJump() and LongJump().
67 ///
68 typedef struct {
69   UINT64    R0;
70   UINT64    R1;
71   UINT64    R2;
72   UINT64    R3;
73   UINT64    IP;
74 } BASE_LIBRARY_JUMP_BUFFER;
75 
76 #define BASE_LIBRARY_JUMP_BUFFER_ALIGNMENT  8
77 
78 #endif // defined (MDE_CPU_EBC)
79 
80 #if defined (MDE_CPU_ARM)
81 
82 typedef struct {
83   UINT32    R3;  ///< A copy of R13.
84   UINT32    R4;
85   UINT32    R5;
86   UINT32    R6;
87   UINT32    R7;
88   UINT32    R8;
89   UINT32    R9;
90   UINT32    R10;
91   UINT32    R11;
92   UINT32    R12;
93   UINT32    R14;
94 } BASE_LIBRARY_JUMP_BUFFER;
95 
96 #define BASE_LIBRARY_JUMP_BUFFER_ALIGNMENT  4
97 
98 #endif // defined (MDE_CPU_ARM)
99 
100 #if defined (MDE_CPU_AARCH64)
101 typedef struct {
102   // GP regs
103   UINT64    X19;
104   UINT64    X20;
105   UINT64    X21;
106   UINT64    X22;
107   UINT64    X23;
108   UINT64    X24;
109   UINT64    X25;
110   UINT64    X26;
111   UINT64    X27;
112   UINT64    X28;
113   UINT64    FP;
114   UINT64    LR;
115   UINT64    IP0;
116 
117   // FP regs
118   UINT64    D8;
119   UINT64    D9;
120   UINT64    D10;
121   UINT64    D11;
122   UINT64    D12;
123   UINT64    D13;
124   UINT64    D14;
125   UINT64    D15;
126 } BASE_LIBRARY_JUMP_BUFFER;
127 
128 #define BASE_LIBRARY_JUMP_BUFFER_ALIGNMENT  8
129 
130 /**
131   Reads the current value of CNTPCT_EL0 register.
132 
133   Reads and returns the current value of CNTPCT_EL0.
134   This function is only available on AARCH64.
135 
136   @return The current value of CNTPCT_EL0
137 **/
138 UINT64
139 EFIAPI
140 ArmReadCntPctReg (
141   VOID
142   );
143 
144 //
145 // Bit shifts for the ID_AA64ISAR0_EL1 register.
146 //
147 #define ARM_ID_AA64ISAR0_EL1_AES_SHIFT     (4U)
148 #define ARM_ID_AA64ISAR0_EL1_SHA1_SHIFT    (8U)
149 #define ARM_ID_AA64ISAR0_EL1_SHA2_SHIFT    (12U)
150 #define ARM_ID_AA64ISAR0_EL1_CRC32_SHIFT   (16U)
151 #define ARM_ID_AA64ISAR0_EL1_ATOMIC_SHIFT  (20U)
152 #define ARM_ID_AA64ISAR0_EL1_RDM_SHIFT     (28U)
153 #define ARM_ID_AA64ISAR0_EL1_SHA3_SHIFT    (32U)
154 #define ARM_ID_AA64ISAR0_EL1_SM3_SHIFT     (36U)
155 #define ARM_ID_AA64ISAR0_EL1_SM4_SHIFT     (40U)
156 #define ARM_ID_AA64ISAR0_EL1_DP_SHIFT      (44U)
157 #define ARM_ID_AA64ISAR0_EL1_FHM_SHIFT     (48U)
158 #define ARM_ID_AA64ISAR0_EL1_TS_SHIFT      (52U)
159 #define ARM_ID_AA64ISAR0_EL1_TLB_SHIFT     (56U)
160 #define ARM_ID_AA64ISAR0_EL1_RNDR_SHIFT    (60U)
161 
162 //
163 // Bit masks for the ID_AA64ISAR0_EL1 fields.
164 //
165 #define ARM_ID_AA64ISAR0_EL1_AES_MASK     (0xFU)
166 #define ARM_ID_AA64ISAR0_EL1_SHA1_MASK    (0xFU)
167 #define ARM_ID_AA64ISAR0_EL1_SHA2_MASK    (0xFU)
168 #define ARM_ID_AA64ISAR0_EL1_CRC32_MASK   (0xFU)
169 #define ARM_ID_AA64ISAR0_EL1_ATOMIC_MASK  (0xFU)
170 #define ARM_ID_AA64ISAR0_EL1_RDM_MASK     (0xFU)
171 #define ARM_ID_AA64ISAR0_EL1_SHA3_MASK    (0xFU)
172 #define ARM_ID_AA64ISAR0_EL1_SM3_MASK     (0xFU)
173 #define ARM_ID_AA64ISAR0_EL1_SM4_MASK     (0xFU)
174 #define ARM_ID_AA64ISAR0_EL1_DP_MASK      (0xFU)
175 #define ARM_ID_AA64ISAR0_EL1_FHM_MASK     (0xFU)
176 #define ARM_ID_AA64ISAR0_EL1_TS_MASK      (0xFU)
177 #define ARM_ID_AA64ISAR0_EL1_TLB_MASK     (0xFU)
178 #define ARM_ID_AA64ISAR0_EL1_RNDR_MASK    (0xFU)
179 
180 //
181 // Bit masks for the ID_AA64ISAR0_EL1 field values.
182 //
183 #define ARM_ID_AA64ISAR0_EL1_AES_FEAT_AES_MASK        (0x1U)
184 #define ARM_ID_AA64ISAR0_EL1_AES_FEAT_PMULL_MASK      (0x2U)
185 #define ARM_ID_AA64ISAR0_EL1_SHA1_FEAT_SHA1_MASK      (0x1U)
186 #define ARM_ID_AA64ISAR0_EL1_SHA2_FEAT_SHA256_MASK    (0x1U)
187 #define ARM_ID_AA64ISAR0_EL1_SHA2_FEAT_SHA512_MASK    (0x2U)
188 #define ARM_ID_AA64ISAR0_EL1_CRC32_HAVE_CRC32_MASK    (0x1U)
189 #define ARM_ID_AA64ISAR0_EL1_ATOMIC_FEAT_LSE_MASK     (0x2U)
190 #define ARM_ID_AA64ISAR0_EL1_RDM_FEAT_RDM_MASK        (0x1U)
191 #define ARM_ID_AA64ISAR0_EL1_SHA3_FEAT_SHA3_MASK      (0x1U)
192 #define ARM_ID_AA64ISAR0_EL1_SM3_FEAT_SM3_MASK        (0x1U)
193 #define ARM_ID_AA64ISAR0_EL1_SM4_FEAT_SM4_MASK        (0x1U)
194 #define ARM_ID_AA64ISAR0_EL1_DP_FEAT_DOTPROD_MASK     (0x1U)
195 #define ARM_ID_AA64ISAR0_EL1_FHM_FEAT_FHM_MASK        (0x1U)
196 #define ARM_ID_AA64ISAR0_EL1_TS_FEAT_FLAGM_MASK       (0x1U)
197 #define ARM_ID_AA64ISAR0_EL1_TS_FEAT_FLAGM2_MASK      (0x2U)
198 #define ARM_ID_AA64ISAR0_EL1_TLB_FEAT_TLBIOS_MASK     (0x1U)
199 #define ARM_ID_AA64ISAR0_EL1_TLB_FEAT_TLBIRANGE_MASK  (0x2U)
200 #define ARM_ID_AA64ISAR0_EL1_RNDR_FEAT_RNG_MASK       (0x1U)
201 
202 /**
203   Reads the current value of ID_AA64ISAR0_EL1 register.
204 
205   Reads and returns the current value of ID_AA64ISAR0_EL1.
206   This function is only available on AARCH64.
207 
208   @return The current value of ID_AA64ISAR0_EL1
209 **/
210 UINT64
211 EFIAPI
212 ArmReadIdAA64Isar0Reg (
213   VOID
214   );
215 
216 #endif // defined (MDE_CPU_AARCH64)
217 
218 #if defined (MDE_CPU_RISCV64)
219 ///
220 /// The RISC-V architecture context buffer used by SetJump() and LongJump().
221 ///
222 typedef struct {
223   UINT64    RA;
224   UINT64    S0;
225   UINT64    S1;
226   UINT64    S2;
227   UINT64    S3;
228   UINT64    S4;
229   UINT64    S5;
230   UINT64    S6;
231   UINT64    S7;
232   UINT64    S8;
233   UINT64    S9;
234   UINT64    S10;
235   UINT64    S11;
236   UINT64    SP;
237 } BASE_LIBRARY_JUMP_BUFFER;
238 
239 #define BASE_LIBRARY_JUMP_BUFFER_ALIGNMENT  8
240 
241 VOID
242 RiscVSetSupervisorScratch (
243   IN UINT64
244   );
245 
246 UINT64
247 RiscVGetSupervisorScratch (
248   VOID
249   );
250 
251 VOID
252 RiscVSetSupervisorStvec (
253   IN UINT64
254   );
255 
256 UINT64
257 RiscVGetSupervisorStvec (
258   VOID
259   );
260 
261 UINT64
262 RiscVGetSupervisorTrapCause (
263   VOID
264   );
265 
266 VOID
267 RiscVSetSupervisorAddressTranslationRegister (
268   IN UINT64
269   );
270 
271 UINT64
272 RiscVGetSupervisorAddressTranslationRegister (
273   VOID
274   );
275 
276 UINT64
277 RiscVReadTimer (
278   VOID
279   );
280 
281 VOID
282 RiscVSetSupervisorTimeCompareRegister (
283   IN UINT64
284   );
285 
286 VOID
287 RiscVEnableTimerInterrupt (
288   VOID
289   );
290 
291 VOID
292 RiscVDisableTimerInterrupt (
293   VOID
294   );
295 
296 VOID
297 RiscVClearPendingTimerInterrupt (
298   VOID
299   );
300 
301 /**
302   RISC-V invalidate instruction cache.
303 
304 **/
305 VOID
306 EFIAPI
307 RiscVInvalidateInstCacheFenceAsm (
308   VOID
309   );
310 
311 /**
312   RISC-V invalidate data cache.
313 
314 **/
315 VOID
316 EFIAPI
317 RiscVInvalidateDataCacheFenceAsm (
318   VOID
319   );
320 
321 /**
322   RISC-V flush cache block. Atomically perform a clean operation
323   followed by an invalidate operation
324 
325 **/
326 VOID
327 EFIAPI
328 RiscVCpuCacheFlushCmoAsm (
329   IN UINTN
330   );
331 
332 /**
333 Perform a write transfer to another cache or to memory if the
334 data in the copy of the cache block have been modified by a store
335 operation
336 
337 **/
338 VOID
339 EFIAPI
340 RiscVCpuCacheCleanCmoAsm (
341   IN UINTN
342   );
343 
344 /**
345 Deallocate the copy of the cache block
346 
347 **/
348 VOID
349 EFIAPI
350 RiscVCpuCacheInvalCmoAsm (
351   IN UINTN
352   );
353 
354 #endif // defined (MDE_CPU_RISCV64)
355 
356 #if defined (MDE_CPU_LOONGARCH64)
357 ///
358 /// The LoongArch architecture context buffer used by SetJump() and LongJump()
359 ///
360 typedef struct {
361   UINT64    S0;
362   UINT64    S1;
363   UINT64    S2;
364   UINT64    S3;
365   UINT64    S4;
366   UINT64    S5;
367   UINT64    S6;
368   UINT64    S7;
369   UINT64    S8;
370   UINT64    SP;
371   UINT64    FP;
372   UINT64    RA;
373 } BASE_LIBRARY_JUMP_BUFFER;
374 
375 #define BASE_LIBRARY_JUMP_BUFFER_ALIGNMENT  8
376 
377 /*
378  * Set the exception base address for LoongArch.
379  *
380  * @param  ExceptionBaseAddress   The exception base address, must be aligned greater than or qeual to 4K .
381  */
382 VOID
383 SetExceptionBaseAddress (
384   IN UINT64
385   );
386 
387 /*
388  * Set the TlbRebase address for LoongArch.
389  *
390  * @param  TlbRebaseAddress   The TlbRebase address, must be aligned greater than or qeual to 4K .
391  */
392 VOID
393 SetTlbRebaseAddress (
394   IN UINT64
395   );
396 
397 /**
398   Enables local CPU interrupts.
399 
400   @param  Needs to enable local interrupt bit.
401 **/
402 VOID
403 EnableLocalInterrupts (
404   IN UINT16
405   );
406 
407 /**
408   Disables local CPU interrupts.
409 
410   @param  Needs to disable local interrupt bit.
411 **/
412 VOID
413 DisableLocalInterrupts (
414   IN UINT16
415   );
416 
417 /**
418   Read CPUCFG register.
419 
420   @param  Index  Specifies the register number of the CPUCFG to read the data.
421   @param  Data   A pointer to the variable used to store the CPUCFG register value.
422 **/
423 VOID
424 AsmCpucfg (
425   IN  UINT32  Index,
426   OUT UINT32  *Data
427   );
428 
429 /**
430   Gets the timer count value.
431 
432   @param[] VOID
433   @retval  timer count value.
434 
435 **/
436 UINTN
437 AsmReadStableCounter (
438   VOID
439   );
440 
441 /**
442   CSR read operation.
443 
444   @param[in]  Select   CSR read instruction select values.
445 
446   @return     The return value of csrrd instruction, return -1 means no CSR instruction
447               is found.
448 **/
449 UINTN
450 CsrRead (
451   IN UINT16  Select
452   );
453 
454 /**
455   CSR write operation.
456 
457   @param[in]  Select   CSR write instruction select values.
458   @param[in]  Value    The csrwr will write the value.
459 
460   @return     The return value of csrwr instruction, that is, store the old value of
461               the register, return -1 means no CSR instruction is found.
462 **/
463 UINTN
464 CsrWrite (
465   IN UINT16  Select,
466   IN UINTN   Value
467   );
468 
469 /**
470   CSR exchange operation.
471 
472   @param[in]  Select   CSR exchange instruction select values.
473   @param[in]  Value    The csrxchg will write the value.
474   @param[in]  Mask     The csrxchg mask value.
475 
476   @return     The return value of csrxchg instruction, that is, store the old value of
477               the register, return -1 means no CSR instruction is found.
478 **/
479 UINTN
480 CsrXChg (
481   IN UINT16  Select,
482   IN UINTN   Value,
483   IN UINTN   Mask
484   );
485 
486 /**
487   IO CSR read byte operation.
488 
489   @param[in]  Select   IO CSR read instruction select values.
490 
491   @return     The return value of iocsrrd.b instruction.
492 
493 **/
494 UINT8
495 IoCsrRead8 (
496   IN UINTN  Select
497   );
498 
499 /**
500   IO CSR read half word operation.
501 
502   @param[in]  Select   IO CSR read instruction select values.
503 
504   @return     The return value of iocsrrd.h instruction.
505 
506 **/
507 UINT16
508 IoCsrRead16 (
509   IN UINTN  Select
510   );
511 
512 /**
513   IO CSR read word operation.
514 
515   @param[in]  Select   IO CSR read instruction select values.
516 
517   @return     The return value of iocsrrd.w instruction.
518 
519 **/
520 UINT32
521 IoCsrRead32 (
522   IN UINTN  Select
523   );
524 
525 /**
526   IO CSR read double word operation. Only for LoongArch64.
527 
528   @param[in]  Select   IO CSR read instruction select values.
529 
530   @return     The return value of iocsrrd.d instruction.
531 
532 **/
533 UINT64
534 IoCsrRead64 (
535   IN UINTN  Select
536   );
537 
538 /**
539   IO CSR write byte operation.
540 
541   @param[in]  Select   IO CSR write instruction select values.
542   @param[in]  Value    The iocsrwr.b will write the value.
543 
544   @return     VOID.
545 
546 **/
547 VOID
548 IoCsrWrite8 (
549   IN UINTN  Select,
550   IN UINT8  Value
551   );
552 
553 /**
554   IO CSR write half word operation.
555 
556   @param[in]  Select   IO CSR write instruction select values.
557   @param[in]  Value    The iocsrwr.h will write the value.
558 
559   @return     VOID.
560 
561 **/
562 VOID
563 IoCsrWrite16 (
564   IN UINTN   Select,
565   IN UINT16  Value
566   );
567 
568 /**
569   IO CSR write word operation.
570 
571   @param[in]  Select   IO CSR write instruction select values.
572   @param[in]  Value    The iocsrwr.w will write the value.
573 
574   @return     VOID.
575 
576 **/
577 VOID
578 IoCsrWrite32 (
579   IN UINTN   Select,
580   IN UINT32  Value
581   );
582 
583 /**
584   IO CSR write double word operation. Only for LoongArch64.
585 
586   @param[in]  Select   IO CSR write instruction select values.
587   @param[in]  Value    The iocsrwr.d will write the value.
588 
589   @return     VOID.
590 
591 **/
592 VOID
593 IoCsrWrite64 (
594   IN UINTN   Select,
595   IN UINT64  Value
596   );
597 
598 #endif // defined (MDE_CPU_LOONGARCH64)
599 
600 //
601 // String Services
602 //
603 
604 /**
605   Returns the length of a Null-terminated Unicode string.
606 
607   This function is similar as strlen_s defined in C11.
608 
609   If String is not aligned on a 16-bit boundary, then ASSERT().
610 
611   @param  String   A pointer to a Null-terminated Unicode string.
612   @param  MaxSize  The maximum number of Destination Unicode
613                    char, including terminating null char.
614 
615   @retval 0        If String is NULL.
616   @retval MaxSize  If there is no null character in the first MaxSize characters of String.
617   @return The number of characters that percede the terminating null character.
618 
619 **/
620 UINTN
621 EFIAPI
622 StrnLenS (
623   IN CONST CHAR16  *String,
624   IN UINTN         MaxSize
625   );
626 
627 /**
628   Returns the size of a Null-terminated Unicode string in bytes, including the
629   Null terminator.
630 
631   This function returns the size of the Null-terminated Unicode string
632   specified by String in bytes, including the Null terminator.
633 
634   If String is not aligned on a 16-bit boundary, then ASSERT().
635 
636   @param  String   A pointer to a Null-terminated Unicode string.
637   @param  MaxSize  The maximum number of Destination Unicode
638                    char, including the Null terminator.
639 
640   @retval 0  If String is NULL.
641   @retval (sizeof (CHAR16) * (MaxSize + 1))
642              If there is no Null terminator in the first MaxSize characters of
643              String.
644   @return The size of the Null-terminated Unicode string in bytes, including
645           the Null terminator.
646 
647 **/
648 UINTN
649 EFIAPI
650 StrnSizeS (
651   IN CONST CHAR16  *String,
652   IN UINTN         MaxSize
653   );
654 
655 /**
656   Copies the string pointed to by Source (including the terminating null char)
657   to the array pointed to by Destination.
658 
659   This function is similar as strcpy_s defined in C11.
660 
661   If Destination is not aligned on a 16-bit boundary, then ASSERT().
662   If Source is not aligned on a 16-bit boundary, then ASSERT().
663 
664   If an error is returned, then the Destination is unmodified.
665 
666   @param  Destination              A pointer to a Null-terminated Unicode string.
667   @param  DestMax                  The maximum number of Destination Unicode
668                                    char, including terminating null char.
669   @param  Source                   A pointer to a Null-terminated Unicode string.
670 
671   @retval RETURN_SUCCESS           String is copied.
672   @retval RETURN_BUFFER_TOO_SMALL  If DestMax is NOT greater than StrLen(Source).
673   @retval RETURN_INVALID_PARAMETER If Destination is NULL.
674                                    If Source is NULL.
675                                    If PcdMaximumUnicodeStringLength is not zero,
676                                     and DestMax is greater than
677                                     PcdMaximumUnicodeStringLength.
678                                    If DestMax is 0.
679   @retval RETURN_ACCESS_DENIED     If Source and Destination overlap.
680 **/
681 RETURN_STATUS
682 EFIAPI
683 StrCpyS (
684   OUT CHAR16        *Destination,
685   IN  UINTN         DestMax,
686   IN  CONST CHAR16  *Source
687   );
688 
689 /**
690   Copies not more than Length successive char from the string pointed to by
691   Source to the array pointed to by Destination. If no null char is copied from
692   Source, then Destination[Length] is always set to null.
693 
694   This function is similar as strncpy_s defined in C11.
695 
696   If Length > 0 and Destination is not aligned on a 16-bit boundary, then ASSERT().
697   If Length > 0 and Source is not aligned on a 16-bit boundary, then ASSERT().
698 
699   If an error is returned, then the Destination is unmodified.
700 
701   @param  Destination              A pointer to a Null-terminated Unicode string.
702   @param  DestMax                  The maximum number of Destination Unicode
703                                    char, including terminating null char.
704   @param  Source                   A pointer to a Null-terminated Unicode string.
705   @param  Length                   The maximum number of Unicode characters to copy.
706 
707   @retval RETURN_SUCCESS           String is copied.
708   @retval RETURN_BUFFER_TOO_SMALL  If DestMax is NOT greater than
709                                    MIN(StrLen(Source), Length).
710   @retval RETURN_INVALID_PARAMETER If Destination is NULL.
711                                    If Source is NULL.
712                                    If PcdMaximumUnicodeStringLength is not zero,
713                                     and DestMax is greater than
714                                     PcdMaximumUnicodeStringLength.
715                                    If DestMax is 0.
716   @retval RETURN_ACCESS_DENIED     If Source and Destination overlap.
717 **/
718 RETURN_STATUS
719 EFIAPI
720 StrnCpyS (
721   OUT CHAR16        *Destination,
722   IN  UINTN         DestMax,
723   IN  CONST CHAR16  *Source,
724   IN  UINTN         Length
725   );
726 
727 /**
728   Appends a copy of the string pointed to by Source (including the terminating
729   null char) to the end of the string pointed to by Destination.
730 
731   This function is similar as strcat_s defined in C11.
732 
733   If Destination is not aligned on a 16-bit boundary, then ASSERT().
734   If Source is not aligned on a 16-bit boundary, then ASSERT().
735 
736   If an error is returned, then the Destination is unmodified.
737 
738   @param  Destination              A pointer to a Null-terminated Unicode string.
739   @param  DestMax                  The maximum number of Destination Unicode
740                                    char, including terminating null char.
741   @param  Source                   A pointer to a Null-terminated Unicode string.
742 
743   @retval RETURN_SUCCESS           String is appended.
744   @retval RETURN_BAD_BUFFER_SIZE   If DestMax is NOT greater than
745                                    StrLen(Destination).
746   @retval RETURN_BUFFER_TOO_SMALL  If (DestMax - StrLen(Destination)) is NOT
747                                    greater than StrLen(Source).
748   @retval RETURN_INVALID_PARAMETER If Destination is NULL.
749                                    If Source is NULL.
750                                    If PcdMaximumUnicodeStringLength is not zero,
751                                     and DestMax is greater than
752                                     PcdMaximumUnicodeStringLength.
753                                    If DestMax is 0.
754   @retval RETURN_ACCESS_DENIED     If Source and Destination overlap.
755 **/
756 RETURN_STATUS
757 EFIAPI
758 StrCatS (
759   IN OUT CHAR16        *Destination,
760   IN     UINTN         DestMax,
761   IN     CONST CHAR16  *Source
762   );
763 
764 /**
765   Appends not more than Length successive char from the string pointed to by
766   Source to the end of the string pointed to by Destination. If no null char is
767   copied from Source, then Destination[StrLen(Destination) + Length] is always
768   set to null.
769 
770   This function is similar as strncat_s defined in C11.
771 
772   If Destination is not aligned on a 16-bit boundary, then ASSERT().
773   If Source is not aligned on a 16-bit boundary, then ASSERT().
774 
775   If an error is returned, then the Destination is unmodified.
776 
777   @param  Destination              A pointer to a Null-terminated Unicode string.
778   @param  DestMax                  The maximum number of Destination Unicode
779                                    char, including terminating null char.
780   @param  Source                   A pointer to a Null-terminated Unicode string.
781   @param  Length                   The maximum number of Unicode characters to copy.
782 
783   @retval RETURN_SUCCESS           String is appended.
784   @retval RETURN_BAD_BUFFER_SIZE   If DestMax is NOT greater than
785                                    StrLen(Destination).
786   @retval RETURN_BUFFER_TOO_SMALL  If (DestMax - StrLen(Destination)) is NOT
787                                    greater than MIN(StrLen(Source), Length).
788   @retval RETURN_INVALID_PARAMETER If Destination is NULL.
789                                    If Source is NULL.
790                                    If PcdMaximumUnicodeStringLength is not zero,
791                                     and DestMax is greater than
792                                     PcdMaximumUnicodeStringLength.
793                                    If DestMax is 0.
794   @retval RETURN_ACCESS_DENIED     If Source and Destination overlap.
795 **/
796 RETURN_STATUS
797 EFIAPI
798 StrnCatS (
799   IN OUT CHAR16        *Destination,
800   IN     UINTN         DestMax,
801   IN     CONST CHAR16  *Source,
802   IN     UINTN         Length
803   );
804 
805 /**
806   Convert a Null-terminated Unicode decimal string to a value of type UINTN.
807 
808   This function outputs a value of type UINTN by interpreting the contents of
809   the Unicode string specified by String as a decimal number. The format of the
810   input Unicode string String is:
811 
812                   [spaces] [decimal digits].
813 
814   The valid decimal digit character is in the range [0-9]. The function will
815   ignore the pad space, which includes spaces or tab characters, before
816   [decimal digits]. The running zero in the beginning of [decimal digits] will
817   be ignored. Then, the function stops at the first character that is a not a
818   valid decimal character or a Null-terminator, whichever one comes first.
819 
820   If String is not aligned in a 16-bit boundary, then ASSERT().
821 
822   If String has no valid decimal digits in the above format, then 0 is stored
823   at the location pointed to by Data.
824   If the number represented by String exceeds the range defined by UINTN, then
825   MAX_UINTN is stored at the location pointed to by Data.
826 
827   If EndPointer is not NULL, a pointer to the character that stopped the scan
828   is stored at the location pointed to by EndPointer. If String has no valid
829   decimal digits right after the optional pad spaces, the value of String is
830   stored at the location pointed to by EndPointer.
831 
832   @param  String                   Pointer to a Null-terminated Unicode string.
833   @param  EndPointer               Pointer to character that stops scan.
834   @param  Data                     Pointer to the converted value.
835 
836   @retval RETURN_SUCCESS           Value is translated from String.
837   @retval RETURN_INVALID_PARAMETER If String is NULL.
838                                    If Data is NULL.
839                                    If PcdMaximumUnicodeStringLength is not
840                                    zero, and String contains more than
841                                    PcdMaximumUnicodeStringLength Unicode
842                                    characters, not including the
843                                    Null-terminator.
844   @retval RETURN_UNSUPPORTED       If the number represented by String exceeds
845                                    the range defined by UINTN.
846 
847 **/
848 RETURN_STATUS
849 EFIAPI
850 StrDecimalToUintnS (
851   IN  CONST CHAR16  *String,
852   OUT       CHAR16  **EndPointer   OPTIONAL,
853   OUT       UINTN   *Data
854   );
855 
856 /**
857   Convert a Null-terminated Unicode decimal string to a value of type UINT64.
858 
859   This function outputs a value of type UINT64 by interpreting the contents of
860   the Unicode string specified by String as a decimal number. The format of the
861   input Unicode string String is:
862 
863                   [spaces] [decimal digits].
864 
865   The valid decimal digit character is in the range [0-9]. The function will
866   ignore the pad space, which includes spaces or tab characters, before
867   [decimal digits]. The running zero in the beginning of [decimal digits] will
868   be ignored. Then, the function stops at the first character that is a not a
869   valid decimal character or a Null-terminator, whichever one comes first.
870 
871   If String is not aligned in a 16-bit boundary, then ASSERT().
872 
873   If String has no valid decimal digits in the above format, then 0 is stored
874   at the location pointed to by Data.
875   If the number represented by String exceeds the range defined by UINT64, then
876   MAX_UINT64 is stored at the location pointed to by Data.
877 
878   If EndPointer is not NULL, a pointer to the character that stopped the scan
879   is stored at the location pointed to by EndPointer. If String has no valid
880   decimal digits right after the optional pad spaces, the value of String is
881   stored at the location pointed to by EndPointer.
882 
883   @param  String                   Pointer to a Null-terminated Unicode string.
884   @param  EndPointer               Pointer to character that stops scan.
885   @param  Data                     Pointer to the converted value.
886 
887   @retval RETURN_SUCCESS           Value is translated from String.
888   @retval RETURN_INVALID_PARAMETER If String is NULL.
889                                    If Data is NULL.
890                                    If PcdMaximumUnicodeStringLength is not
891                                    zero, and String contains more than
892                                    PcdMaximumUnicodeStringLength Unicode
893                                    characters, not including the
894                                    Null-terminator.
895   @retval RETURN_UNSUPPORTED       If the number represented by String exceeds
896                                    the range defined by UINT64.
897 
898 **/
899 RETURN_STATUS
900 EFIAPI
901 StrDecimalToUint64S (
902   IN  CONST CHAR16  *String,
903   OUT       CHAR16  **EndPointer   OPTIONAL,
904   OUT       UINT64  *Data
905   );
906 
907 /**
908   Convert a Null-terminated Unicode hexadecimal string to a value of type
909   UINTN.
910 
911   This function outputs a value of type UINTN by interpreting the contents of
912   the Unicode string specified by String as a hexadecimal number. The format of
913   the input Unicode string String is:
914 
915                   [spaces][zeros][x][hexadecimal digits].
916 
917   The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].
918   The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix.
919   If "x" appears in the input string, it must be prefixed with at least one 0.
920   The function will ignore the pad space, which includes spaces or tab
921   characters, before [zeros], [x] or [hexadecimal digit]. The running zero
922   before [x] or [hexadecimal digit] will be ignored. Then, the decoding starts
923   after [x] or the first valid hexadecimal digit. Then, the function stops at
924   the first character that is a not a valid hexadecimal character or NULL,
925   whichever one comes first.
926 
927   If String is not aligned in a 16-bit boundary, then ASSERT().
928 
929   If String has no valid hexadecimal digits in the above format, then 0 is
930   stored at the location pointed to by Data.
931   If the number represented by String exceeds the range defined by UINTN, then
932   MAX_UINTN is stored at the location pointed to by Data.
933 
934   If EndPointer is not NULL, a pointer to the character that stopped the scan
935   is stored at the location pointed to by EndPointer. If String has no valid
936   hexadecimal digits right after the optional pad spaces, the value of String
937   is stored at the location pointed to by EndPointer.
938 
939   @param  String                   Pointer to a Null-terminated Unicode string.
940   @param  EndPointer               Pointer to character that stops scan.
941   @param  Data                     Pointer to the converted value.
942 
943   @retval RETURN_SUCCESS           Value is translated from String.
944   @retval RETURN_INVALID_PARAMETER If String is NULL.
945                                    If Data is NULL.
946                                    If PcdMaximumUnicodeStringLength is not
947                                    zero, and String contains more than
948                                    PcdMaximumUnicodeStringLength Unicode
949                                    characters, not including the
950                                    Null-terminator.
951   @retval RETURN_UNSUPPORTED       If the number represented by String exceeds
952                                    the range defined by UINTN.
953 
954 **/
955 RETURN_STATUS
956 EFIAPI
957 StrHexToUintnS (
958   IN  CONST CHAR16  *String,
959   OUT       CHAR16  **EndPointer   OPTIONAL,
960   OUT       UINTN   *Data
961   );
962 
963 /**
964   Convert a Null-terminated Unicode hexadecimal string to a value of type
965   UINT64.
966 
967   This function outputs a value of type UINT64 by interpreting the contents of
968   the Unicode string specified by String as a hexadecimal number. The format of
969   the input Unicode string String is:
970 
971                   [spaces][zeros][x][hexadecimal digits].
972 
973   The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].
974   The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix.
975   If "x" appears in the input string, it must be prefixed with at least one 0.
976   The function will ignore the pad space, which includes spaces or tab
977   characters, before [zeros], [x] or [hexadecimal digit]. The running zero
978   before [x] or [hexadecimal digit] will be ignored. Then, the decoding starts
979   after [x] or the first valid hexadecimal digit. Then, the function stops at
980   the first character that is a not a valid hexadecimal character or NULL,
981   whichever one comes first.
982 
983   If String is not aligned in a 16-bit boundary, then ASSERT().
984 
985   If String has no valid hexadecimal digits in the above format, then 0 is
986   stored at the location pointed to by Data.
987   If the number represented by String exceeds the range defined by UINT64, then
988   MAX_UINT64 is stored at the location pointed to by Data.
989 
990   If EndPointer is not NULL, a pointer to the character that stopped the scan
991   is stored at the location pointed to by EndPointer. If String has no valid
992   hexadecimal digits right after the optional pad spaces, the value of String
993   is stored at the location pointed to by EndPointer.
994 
995   @param  String                   Pointer to a Null-terminated Unicode string.
996   @param  EndPointer               Pointer to character that stops scan.
997   @param  Data                     Pointer to the converted value.
998 
999   @retval RETURN_SUCCESS           Value is translated from String.
1000   @retval RETURN_INVALID_PARAMETER If String is NULL.
1001                                    If Data is NULL.
1002                                    If PcdMaximumUnicodeStringLength is not
1003                                    zero, and String contains more than
1004                                    PcdMaximumUnicodeStringLength Unicode
1005                                    characters, not including the
1006                                    Null-terminator.
1007   @retval RETURN_UNSUPPORTED       If the number represented by String exceeds
1008                                    the range defined by UINT64.
1009 
1010 **/
1011 RETURN_STATUS
1012 EFIAPI
1013 StrHexToUint64S (
1014   IN  CONST CHAR16  *String,
1015   OUT       CHAR16  **EndPointer   OPTIONAL,
1016   OUT       UINT64  *Data
1017   );
1018 
1019 /**
1020   Returns the length of a Null-terminated Ascii string.
1021 
1022   This function is similar as strlen_s defined in C11.
1023 
1024   @param  String   A pointer to a Null-terminated Ascii string.
1025   @param  MaxSize  The maximum number of Destination Ascii
1026                    char, including terminating null char.
1027 
1028   @retval 0        If String is NULL.
1029   @retval MaxSize  If there is no null character in the first MaxSize characters of String.
1030   @return The number of characters that percede the terminating null character.
1031 
1032 **/
1033 UINTN
1034 EFIAPI
1035 AsciiStrnLenS (
1036   IN CONST CHAR8  *String,
1037   IN UINTN        MaxSize
1038   );
1039 
1040 /**
1041   Returns the size of a Null-terminated Ascii string in bytes, including the
1042   Null terminator.
1043 
1044   This function returns the size of the Null-terminated Ascii string specified
1045   by String in bytes, including the Null terminator.
1046 
1047   @param  String   A pointer to a Null-terminated Ascii string.
1048   @param  MaxSize  The maximum number of Destination Ascii
1049                    char, including the Null terminator.
1050 
1051   @retval 0  If String is NULL.
1052   @retval (sizeof (CHAR8) * (MaxSize + 1))
1053              If there is no Null terminator in the first MaxSize characters of
1054              String.
1055   @return The size of the Null-terminated Ascii string in bytes, including the
1056           Null terminator.
1057 
1058 **/
1059 UINTN
1060 EFIAPI
1061 AsciiStrnSizeS (
1062   IN CONST CHAR8  *String,
1063   IN UINTN        MaxSize
1064   );
1065 
1066 /**
1067   Copies the string pointed to by Source (including the terminating null char)
1068   to the array pointed to by Destination.
1069 
1070   This function is similar as strcpy_s defined in C11.
1071 
1072   If an error is returned, then the Destination is unmodified.
1073 
1074   @param  Destination              A pointer to a Null-terminated Ascii string.
1075   @param  DestMax                  The maximum number of Destination Ascii
1076                                    char, including terminating null char.
1077   @param  Source                   A pointer to a Null-terminated Ascii string.
1078 
1079   @retval RETURN_SUCCESS           String is copied.
1080   @retval RETURN_BUFFER_TOO_SMALL  If DestMax is NOT greater than StrLen(Source).
1081   @retval RETURN_INVALID_PARAMETER If Destination is NULL.
1082                                    If Source is NULL.
1083                                    If PcdMaximumAsciiStringLength is not zero,
1084                                     and DestMax is greater than
1085                                     PcdMaximumAsciiStringLength.
1086                                    If DestMax is 0.
1087   @retval RETURN_ACCESS_DENIED     If Source and Destination overlap.
1088 **/
1089 RETURN_STATUS
1090 EFIAPI
1091 AsciiStrCpyS (
1092   OUT CHAR8        *Destination,
1093   IN  UINTN        DestMax,
1094   IN  CONST CHAR8  *Source
1095   );
1096 
1097 /**
1098   Copies not more than Length successive char from the string pointed to by
1099   Source to the array pointed to by Destination. If no null char is copied from
1100   Source, then Destination[Length] is always set to null.
1101 
1102   This function is similar as strncpy_s defined in C11.
1103 
1104   If an error is returned, then the Destination is unmodified.
1105 
1106   @param  Destination              A pointer to a Null-terminated Ascii string.
1107   @param  DestMax                  The maximum number of Destination Ascii
1108                                    char, including terminating null char.
1109   @param  Source                   A pointer to a Null-terminated Ascii string.
1110   @param  Length                   The maximum number of Ascii characters to copy.
1111 
1112   @retval RETURN_SUCCESS           String is copied.
1113   @retval RETURN_BUFFER_TOO_SMALL  If DestMax is NOT greater than
1114                                    MIN(StrLen(Source), Length).
1115   @retval RETURN_INVALID_PARAMETER If Destination is NULL.
1116                                    If Source is NULL.
1117                                    If PcdMaximumAsciiStringLength is not zero,
1118                                     and DestMax is greater than
1119                                     PcdMaximumAsciiStringLength.
1120                                    If DestMax is 0.
1121   @retval RETURN_ACCESS_DENIED     If Source and Destination overlap.
1122 **/
1123 RETURN_STATUS
1124 EFIAPI
1125 AsciiStrnCpyS (
1126   OUT CHAR8        *Destination,
1127   IN  UINTN        DestMax,
1128   IN  CONST CHAR8  *Source,
1129   IN  UINTN        Length
1130   );
1131 
1132 /**
1133   Appends a copy of the string pointed to by Source (including the terminating
1134   null char) to the end of the string pointed to by Destination.
1135 
1136   This function is similar as strcat_s defined in C11.
1137 
1138   If an error is returned, then the Destination is unmodified.
1139 
1140   @param  Destination              A pointer to a Null-terminated Ascii string.
1141   @param  DestMax                  The maximum number of Destination Ascii
1142                                    char, including terminating null char.
1143   @param  Source                   A pointer to a Null-terminated Ascii string.
1144 
1145   @retval RETURN_SUCCESS           String is appended.
1146   @retval RETURN_BAD_BUFFER_SIZE   If DestMax is NOT greater than
1147                                    StrLen(Destination).
1148   @retval RETURN_BUFFER_TOO_SMALL  If (DestMax - StrLen(Destination)) is NOT
1149                                    greater than StrLen(Source).
1150   @retval RETURN_INVALID_PARAMETER If Destination is NULL.
1151                                    If Source is NULL.
1152                                    If PcdMaximumAsciiStringLength is not zero,
1153                                     and DestMax is greater than
1154                                     PcdMaximumAsciiStringLength.
1155                                    If DestMax is 0.
1156   @retval RETURN_ACCESS_DENIED     If Source and Destination overlap.
1157 **/
1158 RETURN_STATUS
1159 EFIAPI
1160 AsciiStrCatS (
1161   IN OUT CHAR8        *Destination,
1162   IN     UINTN        DestMax,
1163   IN     CONST CHAR8  *Source
1164   );
1165 
1166 /**
1167   Appends not more than Length successive char from the string pointed to by
1168   Source to the end of the string pointed to by Destination. If no null char is
1169   copied from Source, then Destination[StrLen(Destination) + Length] is always
1170   set to null.
1171 
1172   This function is similar as strncat_s defined in C11.
1173 
1174   If an error is returned, then the Destination is unmodified.
1175 
1176   @param  Destination              A pointer to a Null-terminated Ascii string.
1177   @param  DestMax                  The maximum number of Destination Ascii
1178                                    char, including terminating null char.
1179   @param  Source                   A pointer to a Null-terminated Ascii string.
1180   @param  Length                   The maximum number of Ascii characters to copy.
1181 
1182   @retval RETURN_SUCCESS           String is appended.
1183   @retval RETURN_BAD_BUFFER_SIZE   If DestMax is NOT greater than
1184                                    StrLen(Destination).
1185   @retval RETURN_BUFFER_TOO_SMALL  If (DestMax - StrLen(Destination)) is NOT
1186                                    greater than MIN(StrLen(Source), Length).
1187   @retval RETURN_INVALID_PARAMETER If Destination is NULL.
1188                                    If Source is NULL.
1189                                    If PcdMaximumAsciiStringLength is not zero,
1190                                     and DestMax is greater than
1191                                     PcdMaximumAsciiStringLength.
1192                                    If DestMax is 0.
1193   @retval RETURN_ACCESS_DENIED     If Source and Destination overlap.
1194 **/
1195 RETURN_STATUS
1196 EFIAPI
1197 AsciiStrnCatS (
1198   IN OUT CHAR8        *Destination,
1199   IN     UINTN        DestMax,
1200   IN     CONST CHAR8  *Source,
1201   IN     UINTN        Length
1202   );
1203 
1204 /**
1205   Convert a Null-terminated Ascii decimal string to a value of type UINTN.
1206 
1207   This function outputs a value of type UINTN by interpreting the contents of
1208   the Ascii string specified by String as a decimal number. The format of the
1209   input Ascii string String is:
1210 
1211                   [spaces] [decimal digits].
1212 
1213   The valid decimal digit character is in the range [0-9]. The function will
1214   ignore the pad space, which includes spaces or tab characters, before
1215   [decimal digits]. The running zero in the beginning of [decimal digits] will
1216   be ignored. Then, the function stops at the first character that is a not a
1217   valid decimal character or a Null-terminator, whichever one comes first.
1218 
1219   If String has no valid decimal digits in the above format, then 0 is stored
1220   at the location pointed to by Data.
1221   If the number represented by String exceeds the range defined by UINTN, then
1222   MAX_UINTN is stored at the location pointed to by Data.
1223 
1224   If EndPointer is not NULL, a pointer to the character that stopped the scan
1225   is stored at the location pointed to by EndPointer. If String has no valid
1226   decimal digits right after the optional pad spaces, the value of String is
1227   stored at the location pointed to by EndPointer.
1228 
1229   @param  String                   Pointer to a Null-terminated Ascii string.
1230   @param  EndPointer               Pointer to character that stops scan.
1231   @param  Data                     Pointer to the converted value.
1232 
1233   @retval RETURN_SUCCESS           Value is translated from String.
1234   @retval RETURN_INVALID_PARAMETER If String is NULL.
1235                                    If Data is NULL.
1236                                    If PcdMaximumAsciiStringLength is not zero,
1237                                    and String contains more than
1238                                    PcdMaximumAsciiStringLength Ascii
1239                                    characters, not including the
1240                                    Null-terminator.
1241   @retval RETURN_UNSUPPORTED       If the number represented by String exceeds
1242                                    the range defined by UINTN.
1243 
1244 **/
1245 RETURN_STATUS
1246 EFIAPI
1247 AsciiStrDecimalToUintnS (
1248   IN  CONST CHAR8  *String,
1249   OUT       CHAR8  **EndPointer   OPTIONAL,
1250   OUT       UINTN  *Data
1251   );
1252 
1253 /**
1254   Convert a Null-terminated Ascii decimal string to a value of type UINT64.
1255 
1256   This function outputs a value of type UINT64 by interpreting the contents of
1257   the Ascii string specified by String as a decimal number. The format of the
1258   input Ascii string String is:
1259 
1260                   [spaces] [decimal digits].
1261 
1262   The valid decimal digit character is in the range [0-9]. The function will
1263   ignore the pad space, which includes spaces or tab characters, before
1264   [decimal digits]. The running zero in the beginning of [decimal digits] will
1265   be ignored. Then, the function stops at the first character that is a not a
1266   valid decimal character or a Null-terminator, whichever one comes first.
1267 
1268   If String has no valid decimal digits in the above format, then 0 is stored
1269   at the location pointed to by Data.
1270   If the number represented by String exceeds the range defined by UINT64, then
1271   MAX_UINT64 is stored at the location pointed to by Data.
1272 
1273   If EndPointer is not NULL, a pointer to the character that stopped the scan
1274   is stored at the location pointed to by EndPointer. If String has no valid
1275   decimal digits right after the optional pad spaces, the value of String is
1276   stored at the location pointed to by EndPointer.
1277 
1278   @param  String                   Pointer to a Null-terminated Ascii string.
1279   @param  EndPointer               Pointer to character that stops scan.
1280   @param  Data                     Pointer to the converted value.
1281 
1282   @retval RETURN_SUCCESS           Value is translated from String.
1283   @retval RETURN_INVALID_PARAMETER If String is NULL.
1284                                    If Data is NULL.
1285                                    If PcdMaximumAsciiStringLength is not zero,
1286                                    and String contains more than
1287                                    PcdMaximumAsciiStringLength Ascii
1288                                    characters, not including the
1289                                    Null-terminator.
1290   @retval RETURN_UNSUPPORTED       If the number represented by String exceeds
1291                                    the range defined by UINT64.
1292 
1293 **/
1294 RETURN_STATUS
1295 EFIAPI
1296 AsciiStrDecimalToUint64S (
1297   IN  CONST CHAR8   *String,
1298   OUT       CHAR8   **EndPointer   OPTIONAL,
1299   OUT       UINT64  *Data
1300   );
1301 
1302 /**
1303   Convert a Null-terminated Ascii hexadecimal string to a value of type UINTN.
1304 
1305   This function outputs a value of type UINTN by interpreting the contents of
1306   the Ascii string specified by String as a hexadecimal number. The format of
1307   the input Ascii string String is:
1308 
1309                   [spaces][zeros][x][hexadecimal digits].
1310 
1311   The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].
1312   The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix. If
1313   "x" appears in the input string, it must be prefixed with at least one 0. The
1314   function will ignore the pad space, which includes spaces or tab characters,
1315   before [zeros], [x] or [hexadecimal digits]. The running zero before [x] or
1316   [hexadecimal digits] will be ignored. Then, the decoding starts after [x] or
1317   the first valid hexadecimal digit. Then, the function stops at the first
1318   character that is a not a valid hexadecimal character or Null-terminator,
1319   whichever on comes first.
1320 
1321   If String has no valid hexadecimal digits in the above format, then 0 is
1322   stored at the location pointed to by Data.
1323   If the number represented by String exceeds the range defined by UINTN, then
1324   MAX_UINTN is stored at the location pointed to by Data.
1325 
1326   If EndPointer is not NULL, a pointer to the character that stopped the scan
1327   is stored at the location pointed to by EndPointer. If String has no valid
1328   hexadecimal digits right after the optional pad spaces, the value of String
1329   is stored at the location pointed to by EndPointer.
1330 
1331   @param  String                   Pointer to a Null-terminated Ascii string.
1332   @param  EndPointer               Pointer to character that stops scan.
1333   @param  Data                     Pointer to the converted value.
1334 
1335   @retval RETURN_SUCCESS           Value is translated from String.
1336   @retval RETURN_INVALID_PARAMETER If String is NULL.
1337                                    If Data is NULL.
1338                                    If PcdMaximumAsciiStringLength is not zero,
1339                                    and String contains more than
1340                                    PcdMaximumAsciiStringLength Ascii
1341                                    characters, not including the
1342                                    Null-terminator.
1343   @retval RETURN_UNSUPPORTED       If the number represented by String exceeds
1344                                    the range defined by UINTN.
1345 
1346 **/
1347 RETURN_STATUS
1348 EFIAPI
1349 AsciiStrHexToUintnS (
1350   IN  CONST CHAR8  *String,
1351   OUT       CHAR8  **EndPointer   OPTIONAL,
1352   OUT       UINTN  *Data
1353   );
1354 
1355 /**
1356   Convert a Null-terminated Ascii hexadecimal string to a value of type UINT64.
1357 
1358   This function outputs a value of type UINT64 by interpreting the contents of
1359   the Ascii string specified by String as a hexadecimal number. The format of
1360   the input Ascii string String is:
1361 
1362                   [spaces][zeros][x][hexadecimal digits].
1363 
1364   The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].
1365   The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix. If
1366   "x" appears in the input string, it must be prefixed with at least one 0. The
1367   function will ignore the pad space, which includes spaces or tab characters,
1368   before [zeros], [x] or [hexadecimal digits]. The running zero before [x] or
1369   [hexadecimal digits] will be ignored. Then, the decoding starts after [x] or
1370   the first valid hexadecimal digit. Then, the function stops at the first
1371   character that is a not a valid hexadecimal character or Null-terminator,
1372   whichever on comes first.
1373 
1374   If String has no valid hexadecimal digits in the above format, then 0 is
1375   stored at the location pointed to by Data.
1376   If the number represented by String exceeds the range defined by UINT64, then
1377   MAX_UINT64 is stored at the location pointed to by Data.
1378 
1379   If EndPointer is not NULL, a pointer to the character that stopped the scan
1380   is stored at the location pointed to by EndPointer. If String has no valid
1381   hexadecimal digits right after the optional pad spaces, the value of String
1382   is stored at the location pointed to by EndPointer.
1383 
1384   @param  String                   Pointer to a Null-terminated Ascii string.
1385   @param  EndPointer               Pointer to character that stops scan.
1386   @param  Data                     Pointer to the converted value.
1387 
1388   @retval RETURN_SUCCESS           Value is translated from String.
1389   @retval RETURN_INVALID_PARAMETER If String is NULL.
1390                                    If Data is NULL.
1391                                    If PcdMaximumAsciiStringLength is not zero,
1392                                    and String contains more than
1393                                    PcdMaximumAsciiStringLength Ascii
1394                                    characters, not including the
1395                                    Null-terminator.
1396   @retval RETURN_UNSUPPORTED       If the number represented by String exceeds
1397                                    the range defined by UINT64.
1398 
1399 **/
1400 RETURN_STATUS
1401 EFIAPI
1402 AsciiStrHexToUint64S (
1403   IN  CONST CHAR8   *String,
1404   OUT       CHAR8   **EndPointer   OPTIONAL,
1405   OUT       UINT64  *Data
1406   );
1407 
1408 /**
1409   Returns the length of a Null-terminated Unicode string.
1410 
1411   This function returns the number of Unicode characters in the Null-terminated
1412   Unicode string specified by String.
1413 
1414   If String is NULL, then ASSERT().
1415   If String is not aligned on a 16-bit boundary, then ASSERT().
1416   If PcdMaximumUnicodeStringLength is not zero, and String contains more than
1417   PcdMaximumUnicodeStringLength Unicode characters not including the
1418   Null-terminator, then ASSERT().
1419 
1420   @param  String  Pointer to a Null-terminated Unicode string.
1421 
1422   @return The length of String.
1423 
1424 **/
1425 UINTN
1426 EFIAPI
1427 StrLen (
1428   IN      CONST CHAR16  *String
1429   );
1430 
1431 /**
1432   Returns the size of a Null-terminated Unicode string in bytes, including the
1433   Null terminator.
1434 
1435   This function returns the size, in bytes, of the Null-terminated Unicode string
1436   specified by String.
1437 
1438   If String is NULL, then ASSERT().
1439   If String is not aligned on a 16-bit boundary, then ASSERT().
1440   If PcdMaximumUnicodeStringLength is not zero, and String contains more than
1441   PcdMaximumUnicodeStringLength Unicode characters not including the
1442   Null-terminator, then ASSERT().
1443 
1444   @param  String  The pointer to a Null-terminated Unicode string.
1445 
1446   @return The size of String.
1447 
1448 **/
1449 UINTN
1450 EFIAPI
1451 StrSize (
1452   IN      CONST CHAR16  *String
1453   );
1454 
1455 /**
1456   Compares two Null-terminated Unicode strings, and returns the difference
1457   between the first mismatched Unicode characters.
1458 
1459   This function compares the Null-terminated Unicode string FirstString to the
1460   Null-terminated Unicode string SecondString. If FirstString is identical to
1461   SecondString, then 0 is returned. Otherwise, the value returned is the first
1462   mismatched Unicode character in SecondString subtracted from the first
1463   mismatched Unicode character in FirstString.
1464 
1465   If FirstString is NULL, then ASSERT().
1466   If FirstString is not aligned on a 16-bit boundary, then ASSERT().
1467   If SecondString is NULL, then ASSERT().
1468   If SecondString is not aligned on a 16-bit boundary, then ASSERT().
1469   If PcdMaximumUnicodeStringLength is not zero, and FirstString contains more
1470   than PcdMaximumUnicodeStringLength Unicode characters not including the
1471   Null-terminator, then ASSERT().
1472   If PcdMaximumUnicodeStringLength is not zero, and SecondString contains more
1473   than PcdMaximumUnicodeStringLength Unicode characters, not including the
1474   Null-terminator, then ASSERT().
1475 
1476   @param  FirstString   The pointer to a Null-terminated Unicode string.
1477   @param  SecondString  The pointer to a Null-terminated Unicode string.
1478 
1479   @retval 0      FirstString is identical to SecondString.
1480   @return others FirstString is not identical to SecondString.
1481 
1482 **/
1483 INTN
1484 EFIAPI
1485 StrCmp (
1486   IN      CONST CHAR16  *FirstString,
1487   IN      CONST CHAR16  *SecondString
1488   );
1489 
1490 /**
1491   Compares up to a specified length the contents of two Null-terminated Unicode strings,
1492   and returns the difference between the first mismatched Unicode characters.
1493 
1494   This function compares the Null-terminated Unicode string FirstString to the
1495   Null-terminated Unicode string SecondString. At most, Length Unicode
1496   characters will be compared. If Length is 0, then 0 is returned. If
1497   FirstString is identical to SecondString, then 0 is returned. Otherwise, the
1498   value returned is the first mismatched Unicode character in SecondString
1499   subtracted from the first mismatched Unicode character in FirstString.
1500 
1501   If Length > 0 and FirstString is NULL, then ASSERT().
1502   If Length > 0 and FirstString is not aligned on a 16-bit boundary, then ASSERT().
1503   If Length > 0 and SecondString is NULL, then ASSERT().
1504   If Length > 0 and SecondString is not aligned on a 16-bit boundary, then ASSERT().
1505   If PcdMaximumUnicodeStringLength is not zero, and Length is greater than
1506   PcdMaximumUnicodeStringLength, then ASSERT().
1507   If PcdMaximumUnicodeStringLength is not zero, and FirstString contains more than
1508   PcdMaximumUnicodeStringLength Unicode characters, not including the Null-terminator,
1509   then ASSERT().
1510   If PcdMaximumUnicodeStringLength is not zero, and SecondString contains more than
1511   PcdMaximumUnicodeStringLength Unicode characters, not including the Null-terminator,
1512   then ASSERT().
1513 
1514   @param  FirstString   The pointer to a Null-terminated Unicode string.
1515   @param  SecondString  The pointer to a Null-terminated Unicode string.
1516   @param  Length        The maximum number of Unicode characters to compare.
1517 
1518   @retval 0      FirstString is identical to SecondString.
1519   @return others FirstString is not identical to SecondString.
1520 
1521 **/
1522 INTN
1523 EFIAPI
1524 StrnCmp (
1525   IN      CONST CHAR16  *FirstString,
1526   IN      CONST CHAR16  *SecondString,
1527   IN      UINTN         Length
1528   );
1529 
1530 /**
1531   Returns the first occurrence of a Null-terminated Unicode sub-string
1532   in a Null-terminated Unicode string.
1533 
1534   This function scans the contents of the Null-terminated Unicode string
1535   specified by String and returns the first occurrence of SearchString.
1536   If SearchString is not found in String, then NULL is returned.  If
1537   the length of SearchString is zero, then String is returned.
1538 
1539   If String is NULL, then ASSERT().
1540   If String is not aligned on a 16-bit boundary, then ASSERT().
1541   If SearchString is NULL, then ASSERT().
1542   If SearchString is not aligned on a 16-bit boundary, then ASSERT().
1543 
1544   If PcdMaximumUnicodeStringLength is not zero, and SearchString
1545   or String contains more than PcdMaximumUnicodeStringLength Unicode
1546   characters, not including the Null-terminator, then ASSERT().
1547 
1548   @param  String          The pointer to a Null-terminated Unicode string.
1549   @param  SearchString    The pointer to a Null-terminated Unicode string to search for.
1550 
1551   @retval NULL            If the SearchString does not appear in String.
1552   @return others          If there is a match.
1553 
1554 **/
1555 CHAR16 *
1556 EFIAPI
1557 StrStr (
1558   IN      CONST CHAR16  *String,
1559   IN      CONST CHAR16  *SearchString
1560   );
1561 
1562 /**
1563   Convert a Null-terminated Unicode decimal string to a value of
1564   type UINTN.
1565 
1566   This function returns a value of type UINTN by interpreting the contents
1567   of the Unicode string specified by String as a decimal number. The format
1568   of the input Unicode string String is:
1569 
1570                   [spaces] [decimal digits].
1571 
1572   The valid decimal digit character is in the range [0-9]. The
1573   function will ignore the pad space, which includes spaces or
1574   tab characters, before [decimal digits]. The running zero in the
1575   beginning of [decimal digits] will be ignored. Then, the function
1576   stops at the first character that is a not a valid decimal character
1577   or a Null-terminator, whichever one comes first.
1578 
1579   If String is NULL, then ASSERT().
1580   If String is not aligned in a 16-bit boundary, then ASSERT().
1581   If String has only pad spaces, then 0 is returned.
1582   If String has no pad spaces or valid decimal digits,
1583   then 0 is returned.
1584   If the number represented by String overflows according
1585   to the range defined by UINTN, then MAX_UINTN is returned.
1586 
1587   If PcdMaximumUnicodeStringLength is not zero, and String contains
1588   more than PcdMaximumUnicodeStringLength Unicode characters not including
1589   the Null-terminator, then ASSERT().
1590 
1591   @param  String      The pointer to a Null-terminated Unicode string.
1592 
1593   @retval Value translated from String.
1594 
1595 **/
1596 UINTN
1597 EFIAPI
1598 StrDecimalToUintn (
1599   IN      CONST CHAR16  *String
1600   );
1601 
1602 /**
1603   Convert a Null-terminated Unicode decimal string to a value of
1604   type UINT64.
1605 
1606   This function returns a value of type UINT64 by interpreting the contents
1607   of the Unicode string specified by String as a decimal number. The format
1608   of the input Unicode string String is:
1609 
1610                   [spaces] [decimal digits].
1611 
1612   The valid decimal digit character is in the range [0-9]. The
1613   function will ignore the pad space, which includes spaces or
1614   tab characters, before [decimal digits]. The running zero in the
1615   beginning of [decimal digits] will be ignored. Then, the function
1616   stops at the first character that is a not a valid decimal character
1617   or a Null-terminator, whichever one comes first.
1618 
1619   If String is NULL, then ASSERT().
1620   If String is not aligned in a 16-bit boundary, then ASSERT().
1621   If String has only pad spaces, then 0 is returned.
1622   If String has no pad spaces or valid decimal digits,
1623   then 0 is returned.
1624   If the number represented by String overflows according
1625   to the range defined by UINT64, then MAX_UINT64 is returned.
1626 
1627   If PcdMaximumUnicodeStringLength is not zero, and String contains
1628   more than PcdMaximumUnicodeStringLength Unicode characters not including
1629   the Null-terminator, then ASSERT().
1630 
1631   @param  String          The pointer to a Null-terminated Unicode string.
1632 
1633   @retval Value translated from String.
1634 
1635 **/
1636 UINT64
1637 EFIAPI
1638 StrDecimalToUint64 (
1639   IN      CONST CHAR16  *String
1640   );
1641 
1642 /**
1643   Convert a Null-terminated Unicode hexadecimal string to a value of type UINTN.
1644 
1645   This function returns a value of type UINTN by interpreting the contents
1646   of the Unicode string specified by String as a hexadecimal number.
1647   The format of the input Unicode string String is:
1648 
1649                   [spaces][zeros][x][hexadecimal digits].
1650 
1651   The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].
1652   The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix.
1653   If "x" appears in the input string, it must be prefixed with at least one 0.
1654   The function will ignore the pad space, which includes spaces or tab characters,
1655   before [zeros], [x] or [hexadecimal digit]. The running zero before [x] or
1656   [hexadecimal digit] will be ignored. Then, the decoding starts after [x] or the
1657   first valid hexadecimal digit. Then, the function stops at the first character
1658   that is a not a valid hexadecimal character or NULL, whichever one comes first.
1659 
1660   If String is NULL, then ASSERT().
1661   If String is not aligned in a 16-bit boundary, then ASSERT().
1662   If String has only pad spaces, then zero is returned.
1663   If String has no leading pad spaces, leading zeros or valid hexadecimal digits,
1664   then zero is returned.
1665   If the number represented by String overflows according to the range defined by
1666   UINTN, then MAX_UINTN is returned.
1667 
1668   If PcdMaximumUnicodeStringLength is not zero, and String contains more than
1669   PcdMaximumUnicodeStringLength Unicode characters not including the Null-terminator,
1670   then ASSERT().
1671 
1672   @param  String          The pointer to a Null-terminated Unicode string.
1673 
1674   @retval Value translated from String.
1675 
1676 **/
1677 UINTN
1678 EFIAPI
1679 StrHexToUintn (
1680   IN      CONST CHAR16  *String
1681   );
1682 
1683 /**
1684   Convert a Null-terminated Unicode hexadecimal string to a value of type UINT64.
1685 
1686   This function returns a value of type UINT64 by interpreting the contents
1687   of the Unicode string specified by String as a hexadecimal number.
1688   The format of the input Unicode string String is
1689 
1690                   [spaces][zeros][x][hexadecimal digits].
1691 
1692   The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].
1693   The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix.
1694   If "x" appears in the input string, it must be prefixed with at least one 0.
1695   The function will ignore the pad space, which includes spaces or tab characters,
1696   before [zeros], [x] or [hexadecimal digit]. The running zero before [x] or
1697   [hexadecimal digit] will be ignored. Then, the decoding starts after [x] or the
1698   first valid hexadecimal digit. Then, the function stops at the first character that is
1699   a not a valid hexadecimal character or NULL, whichever one comes first.
1700 
1701   If String is NULL, then ASSERT().
1702   If String is not aligned in a 16-bit boundary, then ASSERT().
1703   If String has only pad spaces, then zero is returned.
1704   If String has no leading pad spaces, leading zeros or valid hexadecimal digits,
1705   then zero is returned.
1706   If the number represented by String overflows according to the range defined by
1707   UINT64, then MAX_UINT64 is returned.
1708 
1709   If PcdMaximumUnicodeStringLength is not zero, and String contains more than
1710   PcdMaximumUnicodeStringLength Unicode characters not including the Null-terminator,
1711   then ASSERT().
1712 
1713   @param  String          The pointer to a Null-terminated Unicode string.
1714 
1715   @retval Value translated from String.
1716 
1717 **/
1718 UINT64
1719 EFIAPI
1720 StrHexToUint64 (
1721   IN      CONST CHAR16  *String
1722   );
1723 
1724 /**
1725   Convert a Null-terminated Unicode string to IPv6 address and prefix length.
1726 
1727   This function outputs a value of type IPv6_ADDRESS and may output a value
1728   of type UINT8 by interpreting the contents of the Unicode string specified
1729   by String. The format of the input Unicode string String is as follows:
1730 
1731                   X:X:X:X:X:X:X:X[/P]
1732 
1733   X contains one to four hexadecimal digit characters in the range [0-9], [a-f] and
1734   [A-F]. X is converted to a value of type UINT16, whose low byte is stored in low
1735   memory address and high byte is stored in high memory address. P contains decimal
1736   digit characters in the range [0-9]. The running zero in the beginning of P will
1737   be ignored. /P is optional.
1738 
1739   When /P is not in the String, the function stops at the first character that is
1740   not a valid hexadecimal digit character after eight X's are converted.
1741 
1742   When /P is in the String, the function stops at the first character that is not
1743   a valid decimal digit character after P is converted.
1744 
1745   "::" can be used to compress one or more groups of X when X contains only 0.
1746   The "::" can only appear once in the String.
1747 
1748   If String is not aligned in a 16-bit boundary, then ASSERT().
1749 
1750   If EndPointer is not NULL and Address is translated from String, a pointer
1751   to the character that stopped the scan is stored at the location pointed to
1752   by EndPointer.
1753 
1754   @param  String                   Pointer to a Null-terminated Unicode string.
1755   @param  EndPointer               Pointer to character that stops scan.
1756   @param  Address                  Pointer to the converted IPv6 address.
1757   @param  PrefixLength             Pointer to the converted IPv6 address prefix
1758                                    length. MAX_UINT8 is returned when /P is
1759                                    not in the String.
1760 
1761   @retval RETURN_SUCCESS           Address is translated from String.
1762   @retval RETURN_INVALID_PARAMETER If String is NULL.
1763                                    If Data is NULL.
1764   @retval RETURN_UNSUPPORTED       If X contains more than four hexadecimal
1765                                     digit characters.
1766                                    If String contains "::" and number of X
1767                                     is not less than 8.
1768                                    If P starts with character that is not a
1769                                     valid decimal digit character.
1770                                    If the decimal number converted from P
1771                                     exceeds 128.
1772 
1773 **/
1774 RETURN_STATUS
1775 EFIAPI
1776 StrToIpv6Address (
1777   IN  CONST CHAR16  *String,
1778   OUT CHAR16        **EndPointer  OPTIONAL,
1779   OUT IPv6_ADDRESS  *Address,
1780   OUT UINT8         *PrefixLength OPTIONAL
1781   );
1782 
1783 /**
1784   Convert a Null-terminated Unicode string to IPv4 address and prefix length.
1785 
1786   This function outputs a value of type IPv4_ADDRESS and may output a value
1787   of type UINT8 by interpreting the contents of the Unicode string specified
1788   by String. The format of the input Unicode string String is as follows:
1789 
1790                   D.D.D.D[/P]
1791 
1792   D and P are decimal digit characters in the range [0-9]. The running zero in
1793   the beginning of D and P will be ignored. /P is optional.
1794 
1795   When /P is not in the String, the function stops at the first character that is
1796   not a valid decimal digit character after four D's are converted.
1797 
1798   When /P is in the String, the function stops at the first character that is not
1799   a valid decimal digit character after P is converted.
1800 
1801   If String is not aligned in a 16-bit boundary, then ASSERT().
1802 
1803   If EndPointer is not NULL and Address is translated from String, a pointer
1804   to the character that stopped the scan is stored at the location pointed to
1805   by EndPointer.
1806 
1807   @param  String                   Pointer to a Null-terminated Unicode string.
1808   @param  EndPointer               Pointer to character that stops scan.
1809   @param  Address                  Pointer to the converted IPv4 address.
1810   @param  PrefixLength             Pointer to the converted IPv4 address prefix
1811                                    length. MAX_UINT8 is returned when /P is
1812                                    not in the String.
1813 
1814   @retval RETURN_SUCCESS           Address is translated from String.
1815   @retval RETURN_INVALID_PARAMETER If String is NULL.
1816                                    If Data is NULL.
1817   @retval RETURN_UNSUPPORTED       If String is not in the correct format.
1818                                    If any decimal number converted from D
1819                                     exceeds 255.
1820                                    If the decimal number converted from P
1821                                     exceeds 32.
1822 
1823 **/
1824 RETURN_STATUS
1825 EFIAPI
1826 StrToIpv4Address (
1827   IN  CONST CHAR16  *String,
1828   OUT CHAR16        **EndPointer  OPTIONAL,
1829   OUT IPv4_ADDRESS  *Address,
1830   OUT UINT8         *PrefixLength OPTIONAL
1831   );
1832 
1833 #define GUID_STRING_LENGTH  36
1834 
1835 /**
1836   Convert a Null-terminated Unicode GUID string to a value of type
1837   EFI_GUID.
1838 
1839   This function outputs a GUID value by interpreting the contents of
1840   the Unicode string specified by String. The format of the input
1841   Unicode string String consists of 36 characters, as follows:
1842 
1843                   aabbccdd-eeff-gghh-iijj-kkllmmnnoopp
1844 
1845   The pairs aa - pp are two characters in the range [0-9], [a-f] and
1846   [A-F], with each pair representing a single byte hexadecimal value.
1847 
1848   The mapping between String and the EFI_GUID structure is as follows:
1849                   aa          Data1[24:31]
1850                   bb          Data1[16:23]
1851                   cc          Data1[8:15]
1852                   dd          Data1[0:7]
1853                   ee          Data2[8:15]
1854                   ff          Data2[0:7]
1855                   gg          Data3[8:15]
1856                   hh          Data3[0:7]
1857                   ii          Data4[0:7]
1858                   jj          Data4[8:15]
1859                   kk          Data4[16:23]
1860                   ll          Data4[24:31]
1861                   mm          Data4[32:39]
1862                   nn          Data4[40:47]
1863                   oo          Data4[48:55]
1864                   pp          Data4[56:63]
1865 
1866   If String is not aligned in a 16-bit boundary, then ASSERT().
1867 
1868   @param  String                   Pointer to a Null-terminated Unicode string.
1869   @param  Guid                     Pointer to the converted GUID.
1870 
1871   @retval RETURN_SUCCESS           Guid is translated from String.
1872   @retval RETURN_INVALID_PARAMETER If String is NULL.
1873                                    If Data is NULL.
1874   @retval RETURN_UNSUPPORTED       If String is not as the above format.
1875 
1876 **/
1877 RETURN_STATUS
1878 EFIAPI
1879 StrToGuid (
1880   IN  CONST CHAR16  *String,
1881   OUT GUID          *Guid
1882   );
1883 
1884 /**
1885   Convert a Null-terminated Unicode hexadecimal string to a byte array.
1886 
1887   This function outputs a byte array by interpreting the contents of
1888   the Unicode string specified by String in hexadecimal format. The format of
1889   the input Unicode string String is:
1890 
1891                   [XX]*
1892 
1893   X is a hexadecimal digit character in the range [0-9], [a-f] and [A-F].
1894   The function decodes every two hexadecimal digit characters as one byte. The
1895   decoding stops after Length of characters and outputs Buffer containing
1896   (Length / 2) bytes.
1897 
1898   If String is not aligned in a 16-bit boundary, then ASSERT().
1899 
1900   @param  String                   Pointer to a Null-terminated Unicode string.
1901   @param  Length                   The number of Unicode characters to decode.
1902   @param  Buffer                   Pointer to the converted bytes array.
1903   @param  MaxBufferSize            The maximum size of Buffer.
1904 
1905   @retval RETURN_SUCCESS           Buffer is translated from String.
1906   @retval RETURN_INVALID_PARAMETER If String is NULL.
1907                                    If Data is NULL.
1908                                    If Length is not multiple of 2.
1909                                    If PcdMaximumUnicodeStringLength is not zero,
1910                                     and Length is greater than
1911                                     PcdMaximumUnicodeStringLength.
1912   @retval RETURN_UNSUPPORTED       If Length of characters from String contain
1913                                     a character that is not valid hexadecimal
1914                                     digit characters, or a Null-terminator.
1915   @retval RETURN_BUFFER_TOO_SMALL  If MaxBufferSize is less than (Length / 2).
1916 **/
1917 RETURN_STATUS
1918 EFIAPI
1919 StrHexToBytes (
1920   IN  CONST CHAR16  *String,
1921   IN  UINTN         Length,
1922   OUT UINT8         *Buffer,
1923   IN  UINTN         MaxBufferSize
1924   );
1925 
1926 /**
1927   Convert a Null-terminated Unicode string to a Null-terminated
1928   ASCII string.
1929 
1930   This function is similar to AsciiStrCpyS.
1931 
1932   This function converts the content of the Unicode string Source
1933   to the ASCII string Destination by copying the lower 8 bits of
1934   each Unicode character. The function terminates the ASCII string
1935   Destination by appending a Null-terminator character at the end.
1936 
1937   The caller is responsible to make sure Destination points to a buffer with size
1938   equal or greater than ((StrLen (Source) + 1) * sizeof (CHAR8)) in bytes.
1939 
1940   If any Unicode characters in Source contain non-zero value in
1941   the upper 8 bits, then ASSERT().
1942 
1943   If Source is not aligned on a 16-bit boundary, then ASSERT().
1944 
1945   If an error is returned, then the Destination is unmodified.
1946 
1947   @param  Source        The pointer to a Null-terminated Unicode string.
1948   @param  Destination   The pointer to a Null-terminated ASCII string.
1949   @param  DestMax       The maximum number of Destination Ascii
1950                         char, including terminating null char.
1951 
1952   @retval RETURN_SUCCESS           String is converted.
1953   @retval RETURN_BUFFER_TOO_SMALL  If DestMax is NOT greater than StrLen(Source).
1954   @retval RETURN_INVALID_PARAMETER If Destination is NULL.
1955                                    If Source is NULL.
1956                                    If PcdMaximumAsciiStringLength is not zero,
1957                                     and DestMax is greater than
1958                                     PcdMaximumAsciiStringLength.
1959                                    If PcdMaximumUnicodeStringLength is not zero,
1960                                     and DestMax is greater than
1961                                     PcdMaximumUnicodeStringLength.
1962                                    If DestMax is 0.
1963   @retval RETURN_ACCESS_DENIED     If Source and Destination overlap.
1964 
1965 **/
1966 RETURN_STATUS
1967 EFIAPI
1968 UnicodeStrToAsciiStrS (
1969   IN      CONST CHAR16  *Source,
1970   OUT     CHAR8         *Destination,
1971   IN      UINTN         DestMax
1972   );
1973 
1974 /**
1975   Convert not more than Length successive characters from a Null-terminated
1976   Unicode string to a Null-terminated Ascii string. If no null char is copied
1977   from Source, then Destination[Length] is always set to null.
1978 
1979   This function converts not more than Length successive characters from the
1980   Unicode string Source to the Ascii string Destination by copying the lower 8
1981   bits of each Unicode character. The function terminates the Ascii string
1982   Destination by appending a Null-terminator character at the end.
1983 
1984   The caller is responsible to make sure Destination points to a buffer with size
1985   equal or greater than ((StrLen (Source) + 1) * sizeof (CHAR8)) in bytes.
1986 
1987   If any Unicode characters in Source contain non-zero value in the upper 8
1988   bits, then ASSERT().
1989   If Source is not aligned on a 16-bit boundary, then ASSERT().
1990 
1991   If an error is returned, then the Destination is unmodified.
1992 
1993   @param  Source             The pointer to a Null-terminated Unicode string.
1994   @param  Length             The maximum number of Unicode characters to
1995                              convert.
1996   @param  Destination        The pointer to a Null-terminated Ascii string.
1997   @param  DestMax            The maximum number of Destination Ascii
1998                              char, including terminating null char.
1999   @param  DestinationLength  The number of Unicode characters converted.
2000 
2001   @retval RETURN_SUCCESS            String is converted.
2002   @retval RETURN_INVALID_PARAMETER  If Destination is NULL.
2003                                     If Source is NULL.
2004                                     If DestinationLength is NULL.
2005                                     If PcdMaximumAsciiStringLength is not zero,
2006                                     and Length or DestMax is greater than
2007                                     PcdMaximumAsciiStringLength.
2008                                     If PcdMaximumUnicodeStringLength is not
2009                                     zero, and Length or DestMax is greater than
2010                                     PcdMaximumUnicodeStringLength.
2011                                     If DestMax is 0.
2012   @retval RETURN_BUFFER_TOO_SMALL   If DestMax is NOT greater than
2013                                     MIN(StrLen(Source), Length).
2014   @retval RETURN_ACCESS_DENIED      If Source and Destination overlap.
2015 
2016 **/
2017 RETURN_STATUS
2018 EFIAPI
2019 UnicodeStrnToAsciiStrS (
2020   IN      CONST CHAR16  *Source,
2021   IN      UINTN         Length,
2022   OUT     CHAR8         *Destination,
2023   IN      UINTN         DestMax,
2024   OUT     UINTN         *DestinationLength
2025   );
2026 
2027 /**
2028   Returns the length of a Null-terminated ASCII string.
2029 
2030   This function returns the number of ASCII characters in the Null-terminated
2031   ASCII string specified by String.
2032 
2033   If Length > 0 and Destination is NULL, then ASSERT().
2034   If Length > 0 and Source is NULL, then ASSERT().
2035   If PcdMaximumAsciiStringLength is not zero and String contains more than
2036   PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
2037   then ASSERT().
2038 
2039   @param  String  The pointer to a Null-terminated ASCII string.
2040 
2041   @return The length of String.
2042 
2043 **/
2044 UINTN
2045 EFIAPI
2046 AsciiStrLen (
2047   IN      CONST CHAR8  *String
2048   );
2049 
2050 /**
2051   Returns the size of a Null-terminated ASCII string in bytes, including the
2052   Null terminator.
2053 
2054   This function returns the size, in bytes, of the Null-terminated ASCII string
2055   specified by String.
2056 
2057   If String is NULL, then ASSERT().
2058   If PcdMaximumAsciiStringLength is not zero and String contains more than
2059   PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
2060   then ASSERT().
2061 
2062   @param  String  The pointer to a Null-terminated ASCII string.
2063 
2064   @return The size of String.
2065 
2066 **/
2067 UINTN
2068 EFIAPI
2069 AsciiStrSize (
2070   IN      CONST CHAR8  *String
2071   );
2072 
2073 /**
2074   Compares two Null-terminated ASCII strings, and returns the difference
2075   between the first mismatched ASCII characters.
2076 
2077   This function compares the Null-terminated ASCII string FirstString to the
2078   Null-terminated ASCII string SecondString. If FirstString is identical to
2079   SecondString, then 0 is returned. Otherwise, the value returned is the first
2080   mismatched ASCII character in SecondString subtracted from the first
2081   mismatched ASCII character in FirstString.
2082 
2083   If FirstString is NULL, then ASSERT().
2084   If SecondString is NULL, then ASSERT().
2085   If PcdMaximumAsciiStringLength is not zero and FirstString contains more than
2086   PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
2087   then ASSERT().
2088   If PcdMaximumAsciiStringLength is not zero and SecondString contains more
2089   than PcdMaximumAsciiStringLength ASCII characters not including the
2090   Null-terminator, then ASSERT().
2091 
2092   @param  FirstString   The pointer to a Null-terminated ASCII string.
2093   @param  SecondString  The pointer to a Null-terminated ASCII string.
2094 
2095   @retval ==0      FirstString is identical to SecondString.
2096   @retval !=0      FirstString is not identical to SecondString.
2097 
2098 **/
2099 INTN
2100 EFIAPI
2101 AsciiStrCmp (
2102   IN      CONST CHAR8  *FirstString,
2103   IN      CONST CHAR8  *SecondString
2104   );
2105 
2106 /**
2107   Performs a case insensitive comparison of two Null-terminated ASCII strings,
2108   and returns the difference between the first mismatched ASCII characters.
2109 
2110   This function performs a case insensitive comparison of the Null-terminated
2111   ASCII string FirstString to the Null-terminated ASCII string SecondString. If
2112   FirstString is identical to SecondString, then 0 is returned. Otherwise, the
2113   value returned is the first mismatched lower case ASCII character in
2114   SecondString subtracted from the first mismatched lower case ASCII character
2115   in FirstString.
2116 
2117   If FirstString is NULL, then ASSERT().
2118   If SecondString is NULL, then ASSERT().
2119   If PcdMaximumAsciiStringLength is not zero and FirstString contains more than
2120   PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
2121   then ASSERT().
2122   If PcdMaximumAsciiStringLength is not zero and SecondString contains more
2123   than PcdMaximumAsciiStringLength ASCII characters not including the
2124   Null-terminator, then ASSERT().
2125 
2126   @param  FirstString   The pointer to a Null-terminated ASCII string.
2127   @param  SecondString  The pointer to a Null-terminated ASCII string.
2128 
2129   @retval ==0    FirstString is identical to SecondString using case insensitive
2130                  comparisons.
2131   @retval !=0    FirstString is not identical to SecondString using case
2132                  insensitive comparisons.
2133 
2134 **/
2135 INTN
2136 EFIAPI
2137 AsciiStriCmp (
2138   IN      CONST CHAR8  *FirstString,
2139   IN      CONST CHAR8  *SecondString
2140   );
2141 
2142 /**
2143   Compares two Null-terminated ASCII strings with maximum lengths, and returns
2144   the difference between the first mismatched ASCII characters.
2145 
2146   This function compares the Null-terminated ASCII string FirstString to the
2147   Null-terminated ASCII  string SecondString. At most, Length ASCII characters
2148   will be compared. If Length is 0, then 0 is returned. If FirstString is
2149   identical to SecondString, then 0 is returned. Otherwise, the value returned
2150   is the first mismatched ASCII character in SecondString subtracted from the
2151   first mismatched ASCII character in FirstString.
2152 
2153   If Length > 0 and FirstString is NULL, then ASSERT().
2154   If Length > 0 and SecondString is NULL, then ASSERT().
2155   If PcdMaximumAsciiStringLength is not zero, and Length is greater than
2156   PcdMaximumAsciiStringLength, then ASSERT().
2157   If PcdMaximumAsciiStringLength is not zero, and FirstString contains more than
2158   PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,
2159   then ASSERT().
2160   If PcdMaximumAsciiStringLength is not zero, and SecondString contains more than
2161   PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,
2162   then ASSERT().
2163 
2164   @param  FirstString   The pointer to a Null-terminated ASCII string.
2165   @param  SecondString  The pointer to a Null-terminated ASCII string.
2166   @param  Length        The maximum number of ASCII characters for compare.
2167 
2168   @retval ==0       FirstString is identical to SecondString.
2169   @retval !=0       FirstString is not identical to SecondString.
2170 
2171 **/
2172 INTN
2173 EFIAPI
2174 AsciiStrnCmp (
2175   IN      CONST CHAR8  *FirstString,
2176   IN      CONST CHAR8  *SecondString,
2177   IN      UINTN        Length
2178   );
2179 
2180 /**
2181   Returns the first occurrence of a Null-terminated ASCII sub-string
2182   in a Null-terminated ASCII string.
2183 
2184   This function scans the contents of the ASCII string specified by String
2185   and returns the first occurrence of SearchString. If SearchString is not
2186   found in String, then NULL is returned. If the length of SearchString is zero,
2187   then String is returned.
2188 
2189   If String is NULL, then ASSERT().
2190   If SearchString is NULL, then ASSERT().
2191 
2192   If PcdMaximumAsciiStringLength is not zero, and SearchString or
2193   String contains more than PcdMaximumAsciiStringLength Unicode characters
2194   not including the Null-terminator, then ASSERT().
2195 
2196   @param  String          The pointer to a Null-terminated ASCII string.
2197   @param  SearchString    The pointer to a Null-terminated ASCII string to search for.
2198 
2199   @retval NULL            If the SearchString does not appear in String.
2200   @retval others          If there is a match return the first occurrence of SearchingString.
2201                           If the length of SearchString is zero,return String.
2202 
2203 **/
2204 CHAR8 *
2205 EFIAPI
2206 AsciiStrStr (
2207   IN      CONST CHAR8  *String,
2208   IN      CONST CHAR8  *SearchString
2209   );
2210 
2211 /**
2212   Convert a Null-terminated ASCII decimal string to a value of type
2213   UINTN.
2214 
2215   This function returns a value of type UINTN by interpreting the contents
2216   of the ASCII string String as a decimal number. The format of the input
2217   ASCII string String is:
2218 
2219                     [spaces] [decimal digits].
2220 
2221   The valid decimal digit character is in the range [0-9]. The function will
2222   ignore the pad space, which includes spaces or tab characters, before the digits.
2223   The running zero in the beginning of [decimal digits] will be ignored. Then, the
2224   function stops at the first character that is a not a valid decimal character or
2225   Null-terminator, whichever on comes first.
2226 
2227   If String has only pad spaces, then 0 is returned.
2228   If String has no pad spaces or valid decimal digits, then 0 is returned.
2229   If the number represented by String overflows according to the range defined by
2230   UINTN, then MAX_UINTN is returned.
2231   If String is NULL, then ASSERT().
2232   If PcdMaximumAsciiStringLength is not zero, and String contains more than
2233   PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
2234   then ASSERT().
2235 
2236   @param  String          The pointer to a Null-terminated ASCII string.
2237 
2238   @retval The value translated from String.
2239 
2240 **/
2241 UINTN
2242 EFIAPI
2243 AsciiStrDecimalToUintn (
2244   IN      CONST CHAR8  *String
2245   );
2246 
2247 /**
2248   Convert a Null-terminated ASCII decimal string to a value of type
2249   UINT64.
2250 
2251   This function returns a value of type UINT64 by interpreting the contents
2252   of the ASCII string String as a decimal number. The format of the input
2253   ASCII string String is:
2254 
2255                     [spaces] [decimal digits].
2256 
2257   The valid decimal digit character is in the range [0-9]. The function will
2258   ignore the pad space, which includes spaces or tab characters, before the digits.
2259   The running zero in the beginning of [decimal digits] will be ignored. Then, the
2260   function stops at the first character that is a not a valid decimal character or
2261   Null-terminator, whichever on comes first.
2262 
2263   If String has only pad spaces, then 0 is returned.
2264   If String has no pad spaces or valid decimal digits, then 0 is returned.
2265   If the number represented by String overflows according to the range defined by
2266   UINT64, then MAX_UINT64 is returned.
2267   If String is NULL, then ASSERT().
2268   If PcdMaximumAsciiStringLength is not zero, and String contains more than
2269   PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
2270   then ASSERT().
2271 
2272   @param  String          The pointer to a Null-terminated ASCII string.
2273 
2274   @retval Value translated from String.
2275 
2276 **/
2277 UINT64
2278 EFIAPI
2279 AsciiStrDecimalToUint64 (
2280   IN      CONST CHAR8  *String
2281   );
2282 
2283 /**
2284   Convert a Null-terminated ASCII hexadecimal string to a value of type UINTN.
2285 
2286   This function returns a value of type UINTN by interpreting the contents of
2287   the ASCII string String as a hexadecimal number. The format of the input ASCII
2288   string String is:
2289 
2290                   [spaces][zeros][x][hexadecimal digits].
2291 
2292   The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].
2293   The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix. If "x"
2294   appears in the input string, it must be prefixed with at least one 0. The function
2295   will ignore the pad space, which includes spaces or tab characters, before [zeros],
2296   [x] or [hexadecimal digits]. The running zero before [x] or [hexadecimal digits]
2297   will be ignored. Then, the decoding starts after [x] or the first valid hexadecimal
2298   digit. Then, the function stops at the first character that is a not a valid
2299   hexadecimal character or Null-terminator, whichever on comes first.
2300 
2301   If String has only pad spaces, then 0 is returned.
2302   If String has no leading pad spaces, leading zeros or valid hexadecimal digits, then
2303   0 is returned.
2304 
2305   If the number represented by String overflows according to the range defined by UINTN,
2306   then MAX_UINTN is returned.
2307   If String is NULL, then ASSERT().
2308   If PcdMaximumAsciiStringLength is not zero,
2309   and String contains more than PcdMaximumAsciiStringLength ASCII characters not including
2310   the Null-terminator, then ASSERT().
2311 
2312   @param  String          The pointer to a Null-terminated ASCII string.
2313 
2314   @retval Value translated from String.
2315 
2316 **/
2317 UINTN
2318 EFIAPI
2319 AsciiStrHexToUintn (
2320   IN      CONST CHAR8  *String
2321   );
2322 
2323 /**
2324   Convert a Null-terminated ASCII hexadecimal string to a value of type UINT64.
2325 
2326   This function returns a value of type UINT64 by interpreting the contents of
2327   the ASCII string String as a hexadecimal number. The format of the input ASCII
2328   string String is:
2329 
2330                   [spaces][zeros][x][hexadecimal digits].
2331 
2332   The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].
2333   The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix. If "x"
2334   appears in the input string, it must be prefixed with at least one 0. The function
2335   will ignore the pad space, which includes spaces or tab characters, before [zeros],
2336   [x] or [hexadecimal digits]. The running zero before [x] or [hexadecimal digits]
2337   will be ignored. Then, the decoding starts after [x] or the first valid hexadecimal
2338   digit. Then, the function stops at the first character that is a not a valid
2339   hexadecimal character or Null-terminator, whichever on comes first.
2340 
2341   If String has only pad spaces, then 0 is returned.
2342   If String has no leading pad spaces, leading zeros or valid hexadecimal digits, then
2343   0 is returned.
2344 
2345   If the number represented by String overflows according to the range defined by UINT64,
2346   then MAX_UINT64 is returned.
2347   If String is NULL, then ASSERT().
2348   If PcdMaximumAsciiStringLength is not zero,
2349   and String contains more than PcdMaximumAsciiStringLength ASCII characters not including
2350   the Null-terminator, then ASSERT().
2351 
2352   @param  String          The pointer to a Null-terminated ASCII string.
2353 
2354   @retval Value translated from String.
2355 
2356 **/
2357 UINT64
2358 EFIAPI
2359 AsciiStrHexToUint64 (
2360   IN      CONST CHAR8  *String
2361   );
2362 
2363 /**
2364   Convert a Null-terminated ASCII string to IPv6 address and prefix length.
2365 
2366   This function outputs a value of type IPv6_ADDRESS and may output a value
2367   of type UINT8 by interpreting the contents of the ASCII string specified
2368   by String. The format of the input ASCII string String is as follows:
2369 
2370                   X:X:X:X:X:X:X:X[/P]
2371 
2372   X contains one to four hexadecimal digit characters in the range [0-9], [a-f] and
2373   [A-F]. X is converted to a value of type UINT16, whose low byte is stored in low
2374   memory address and high byte is stored in high memory address. P contains decimal
2375   digit characters in the range [0-9]. The running zero in the beginning of P will
2376   be ignored. /P is optional.
2377 
2378   When /P is not in the String, the function stops at the first character that is
2379   not a valid hexadecimal digit character after eight X's are converted.
2380 
2381   When /P is in the String, the function stops at the first character that is not
2382   a valid decimal digit character after P is converted.
2383 
2384   "::" can be used to compress one or more groups of X when X contains only 0.
2385   The "::" can only appear once in the String.
2386 
2387   If EndPointer is not NULL and Address is translated from String, a pointer
2388   to the character that stopped the scan is stored at the location pointed to
2389   by EndPointer.
2390 
2391   @param  String                   Pointer to a Null-terminated ASCII string.
2392   @param  EndPointer               Pointer to character that stops scan.
2393   @param  Address                  Pointer to the converted IPv6 address.
2394   @param  PrefixLength             Pointer to the converted IPv6 address prefix
2395                                    length. MAX_UINT8 is returned when /P is
2396                                    not in the String.
2397 
2398   @retval RETURN_SUCCESS           Address is translated from String.
2399   @retval RETURN_INVALID_PARAMETER If String is NULL.
2400                                    If Data is NULL.
2401   @retval RETURN_UNSUPPORTED       If X contains more than four hexadecimal
2402                                     digit characters.
2403                                    If String contains "::" and number of X
2404                                     is not less than 8.
2405                                    If P starts with character that is not a
2406                                     valid decimal digit character.
2407                                    If the decimal number converted from P
2408                                     exceeds 128.
2409 
2410 **/
2411 RETURN_STATUS
2412 EFIAPI
2413 AsciiStrToIpv6Address (
2414   IN  CONST CHAR8   *String,
2415   OUT CHAR8         **EndPointer  OPTIONAL,
2416   OUT IPv6_ADDRESS  *Address,
2417   OUT UINT8         *PrefixLength OPTIONAL
2418   );
2419 
2420 /**
2421   Convert a Null-terminated ASCII string to IPv4 address and prefix length.
2422 
2423   This function outputs a value of type IPv4_ADDRESS and may output a value
2424   of type UINT8 by interpreting the contents of the ASCII string specified
2425   by String. The format of the input ASCII string String is as follows:
2426 
2427                   D.D.D.D[/P]
2428 
2429   D and P are decimal digit characters in the range [0-9]. The running zero in
2430   the beginning of D and P will be ignored. /P is optional.
2431 
2432   When /P is not in the String, the function stops at the first character that is
2433   not a valid decimal digit character after four D's are converted.
2434 
2435   When /P is in the String, the function stops at the first character that is not
2436   a valid decimal digit character after P is converted.
2437 
2438   If EndPointer is not NULL and Address is translated from String, a pointer
2439   to the character that stopped the scan is stored at the location pointed to
2440   by EndPointer.
2441 
2442   @param  String                   Pointer to a Null-terminated ASCII string.
2443   @param  EndPointer               Pointer to character that stops scan.
2444   @param  Address                  Pointer to the converted IPv4 address.
2445   @param  PrefixLength             Pointer to the converted IPv4 address prefix
2446                                    length. MAX_UINT8 is returned when /P is
2447                                    not in the String.
2448 
2449   @retval RETURN_SUCCESS           Address is translated from String.
2450   @retval RETURN_INVALID_PARAMETER If String is NULL.
2451                                    If Data is NULL.
2452   @retval RETURN_UNSUPPORTED       If String is not in the correct format.
2453                                    If any decimal number converted from D
2454                                     exceeds 255.
2455                                    If the decimal number converted from P
2456                                     exceeds 32.
2457 
2458 **/
2459 RETURN_STATUS
2460 EFIAPI
2461 AsciiStrToIpv4Address (
2462   IN  CONST CHAR8   *String,
2463   OUT CHAR8         **EndPointer  OPTIONAL,
2464   OUT IPv4_ADDRESS  *Address,
2465   OUT UINT8         *PrefixLength OPTIONAL
2466   );
2467 
2468 /**
2469   Convert a Null-terminated ASCII GUID string to a value of type
2470   EFI_GUID.
2471 
2472   This function outputs a GUID value by interpreting the contents of
2473   the ASCII string specified by String. The format of the input
2474   ASCII string String consists of 36 characters, as follows:
2475 
2476                   aabbccdd-eeff-gghh-iijj-kkllmmnnoopp
2477 
2478   The pairs aa - pp are two characters in the range [0-9], [a-f] and
2479   [A-F], with each pair representing a single byte hexadecimal value.
2480 
2481   The mapping between String and the EFI_GUID structure is as follows:
2482                   aa          Data1[24:31]
2483                   bb          Data1[16:23]
2484                   cc          Data1[8:15]
2485                   dd          Data1[0:7]
2486                   ee          Data2[8:15]
2487                   ff          Data2[0:7]
2488                   gg          Data3[8:15]
2489                   hh          Data3[0:7]
2490                   ii          Data4[0:7]
2491                   jj          Data4[8:15]
2492                   kk          Data4[16:23]
2493                   ll          Data4[24:31]
2494                   mm          Data4[32:39]
2495                   nn          Data4[40:47]
2496                   oo          Data4[48:55]
2497                   pp          Data4[56:63]
2498 
2499   @param  String                   Pointer to a Null-terminated ASCII string.
2500   @param  Guid                     Pointer to the converted GUID.
2501 
2502   @retval RETURN_SUCCESS           Guid is translated from String.
2503   @retval RETURN_INVALID_PARAMETER If String is NULL.
2504                                    If Data is NULL.
2505   @retval RETURN_UNSUPPORTED       If String is not as the above format.
2506 
2507 **/
2508 RETURN_STATUS
2509 EFIAPI
2510 AsciiStrToGuid (
2511   IN  CONST CHAR8  *String,
2512   OUT GUID         *Guid
2513   );
2514 
2515 /**
2516   Convert a Null-terminated ASCII hexadecimal string to a byte array.
2517 
2518   This function outputs a byte array by interpreting the contents of
2519   the ASCII string specified by String in hexadecimal format. The format of
2520   the input ASCII string String is:
2521 
2522                   [XX]*
2523 
2524   X is a hexadecimal digit character in the range [0-9], [a-f] and [A-F].
2525   The function decodes every two hexadecimal digit characters as one byte. The
2526   decoding stops after Length of characters and outputs Buffer containing
2527   (Length / 2) bytes.
2528 
2529   @param  String                   Pointer to a Null-terminated ASCII string.
2530   @param  Length                   The number of ASCII characters to decode.
2531   @param  Buffer                   Pointer to the converted bytes array.
2532   @param  MaxBufferSize            The maximum size of Buffer.
2533 
2534   @retval RETURN_SUCCESS           Buffer is translated from String.
2535   @retval RETURN_INVALID_PARAMETER If String is NULL.
2536                                    If Data is NULL.
2537                                    If Length is not multiple of 2.
2538                                    If PcdMaximumAsciiStringLength is not zero,
2539                                     and Length is greater than
2540                                     PcdMaximumAsciiStringLength.
2541   @retval RETURN_UNSUPPORTED       If Length of characters from String contain
2542                                     a character that is not valid hexadecimal
2543                                     digit characters, or a Null-terminator.
2544   @retval RETURN_BUFFER_TOO_SMALL  If MaxBufferSize is less than (Length / 2).
2545 **/
2546 RETURN_STATUS
2547 EFIAPI
2548 AsciiStrHexToBytes (
2549   IN  CONST CHAR8  *String,
2550   IN  UINTN        Length,
2551   OUT UINT8        *Buffer,
2552   IN  UINTN        MaxBufferSize
2553   );
2554 
2555 /**
2556   Convert one Null-terminated ASCII string to a Null-terminated
2557   Unicode string.
2558 
2559   This function is similar to StrCpyS.
2560 
2561   This function converts the contents of the ASCII string Source to the Unicode
2562   string Destination. The function terminates the Unicode string Destination by
2563   appending a Null-terminator character at the end.
2564 
2565   The caller is responsible to make sure Destination points to a buffer with size
2566   equal or greater than ((AsciiStrLen (Source) + 1) * sizeof (CHAR16)) in bytes.
2567 
2568   If Destination is not aligned on a 16-bit boundary, then ASSERT().
2569 
2570   If an error is returned, then the Destination is unmodified.
2571 
2572   @param  Source        The pointer to a Null-terminated ASCII string.
2573   @param  Destination   The pointer to a Null-terminated Unicode string.
2574   @param  DestMax       The maximum number of Destination Unicode
2575                         char, including terminating null char.
2576 
2577   @retval RETURN_SUCCESS           String is converted.
2578   @retval RETURN_BUFFER_TOO_SMALL  If DestMax is NOT greater than StrLen(Source).
2579   @retval RETURN_INVALID_PARAMETER If Destination is NULL.
2580                                    If Source is NULL.
2581                                    If PcdMaximumUnicodeStringLength is not zero,
2582                                     and DestMax is greater than
2583                                     PcdMaximumUnicodeStringLength.
2584                                    If PcdMaximumAsciiStringLength is not zero,
2585                                     and DestMax is greater than
2586                                     PcdMaximumAsciiStringLength.
2587                                    If DestMax is 0.
2588   @retval RETURN_ACCESS_DENIED     If Source and Destination overlap.
2589 
2590 **/
2591 RETURN_STATUS
2592 EFIAPI
2593 AsciiStrToUnicodeStrS (
2594   IN      CONST CHAR8  *Source,
2595   OUT     CHAR16       *Destination,
2596   IN      UINTN        DestMax
2597   );
2598 
2599 /**
2600   Convert not more than Length successive characters from a Null-terminated
2601   Ascii string to a Null-terminated Unicode string. If no null char is copied
2602   from Source, then Destination[Length] is always set to null.
2603 
2604   This function converts not more than Length successive characters from the
2605   Ascii string Source to the Unicode string Destination. The function
2606   terminates the Unicode string Destination by appending a Null-terminator
2607   character at the end.
2608 
2609   The caller is responsible to make sure Destination points to a buffer with
2610   size not smaller than
2611   ((MIN(AsciiStrLen(Source), Length) + 1) * sizeof (CHAR8)) in bytes.
2612 
2613   If Destination is not aligned on a 16-bit boundary, then ASSERT().
2614 
2615   If an error is returned, then Destination and DestinationLength are
2616   unmodified.
2617 
2618   @param  Source             The pointer to a Null-terminated Ascii string.
2619   @param  Length             The maximum number of Ascii characters to convert.
2620   @param  Destination        The pointer to a Null-terminated Unicode string.
2621   @param  DestMax            The maximum number of Destination Unicode char,
2622                              including terminating null char.
2623   @param  DestinationLength  The number of Ascii characters converted.
2624 
2625   @retval RETURN_SUCCESS            String is converted.
2626   @retval RETURN_INVALID_PARAMETER  If Destination is NULL.
2627                                     If Source is NULL.
2628                                     If DestinationLength is NULL.
2629                                     If PcdMaximumUnicodeStringLength is not
2630                                     zero, and Length or DestMax is greater than
2631                                     PcdMaximumUnicodeStringLength.
2632                                     If PcdMaximumAsciiStringLength is not zero,
2633                                     and Length or DestMax is greater than
2634                                     PcdMaximumAsciiStringLength.
2635                                     If DestMax is 0.
2636   @retval RETURN_BUFFER_TOO_SMALL   If DestMax is NOT greater than
2637                                     MIN(AsciiStrLen(Source), Length).
2638   @retval RETURN_ACCESS_DENIED      If Source and Destination overlap.
2639 
2640 **/
2641 RETURN_STATUS
2642 EFIAPI
2643 AsciiStrnToUnicodeStrS (
2644   IN      CONST CHAR8  *Source,
2645   IN      UINTN        Length,
2646   OUT     CHAR16       *Destination,
2647   IN      UINTN        DestMax,
2648   OUT     UINTN        *DestinationLength
2649   );
2650 
2651 /**
2652   Convert a Unicode character to upper case only if
2653   it maps to a valid small-case ASCII character.
2654 
2655   This internal function only deal with Unicode character
2656   which maps to a valid small-case ASCII character, i.e.
2657   L'a' to L'z'. For other Unicode character, the input character
2658   is returned directly.
2659 
2660   @param  Char  The character to convert.
2661 
2662   @retval LowerCharacter   If the Char is with range L'a' to L'z'.
2663   @retval Unchanged        Otherwise.
2664 
2665 **/
2666 CHAR16
2667 EFIAPI
2668 CharToUpper (
2669   IN      CHAR16  Char
2670   );
2671 
2672 /**
2673   Converts a lowercase Ascii character to upper one.
2674 
2675   If Chr is lowercase Ascii character, then converts it to upper one.
2676 
2677   If Value >= 0xA0, then ASSERT().
2678   If (Value & 0x0F) >= 0x0A, then ASSERT().
2679 
2680   @param  Chr   one Ascii character
2681 
2682   @return The uppercase value of Ascii character
2683 
2684 **/
2685 CHAR8
2686 EFIAPI
2687 AsciiCharToUpper (
2688   IN      CHAR8  Chr
2689   );
2690 
2691 /**
2692   Convert binary data to a Base64 encoded ascii string based on RFC4648.
2693 
2694   Produce a Null-terminated Ascii string in the output buffer specified by Destination and DestinationSize.
2695   The Ascii string is produced by converting the data string specified by Source and SourceLength.
2696 
2697   @param Source           Input UINT8 data
2698   @param SourceLength     Number of UINT8 bytes of data
2699   @param Destination      Pointer to output string buffer
2700   @param DestinationSize  Size of ascii buffer. Set to 0 to get the size needed.
2701                           Caller is responsible for passing in buffer of DestinationSize
2702 
2703   @retval RETURN_SUCCESS             When ascii buffer is filled in.
2704   @retval RETURN_INVALID_PARAMETER   If Source is NULL or DestinationSize is NULL.
2705   @retval RETURN_INVALID_PARAMETER   If SourceLength or DestinationSize is bigger than (MAX_ADDRESS - (UINTN)Destination).
2706   @retval RETURN_BUFFER_TOO_SMALL    If SourceLength is 0 and DestinationSize is <1.
2707   @retval RETURN_BUFFER_TOO_SMALL    If Destination is NULL or DestinationSize is smaller than required buffersize.
2708 
2709 **/
2710 RETURN_STATUS
2711 EFIAPI
2712 Base64Encode (
2713   IN  CONST UINT8  *Source,
2714   IN        UINTN  SourceLength,
2715   OUT       CHAR8  *Destination  OPTIONAL,
2716   IN OUT    UINTN  *DestinationSize
2717   );
2718 
2719 /**
2720   Decode Base64 ASCII encoded data to 8-bit binary representation, based on
2721   RFC4648.
2722 
2723   Decoding occurs according to "Table 1: The Base 64 Alphabet" in RFC4648.
2724 
2725   Whitespace is ignored at all positions:
2726   - 0x09 ('\t') horizontal tab
2727   - 0x0A ('\n') new line
2728   - 0x0B ('\v') vertical tab
2729   - 0x0C ('\f') form feed
2730   - 0x0D ('\r') carriage return
2731   - 0x20 (' ')  space
2732 
2733   The minimum amount of required padding (with ASCII 0x3D, '=') is tolerated
2734   and enforced at the end of the Base64 ASCII encoded data, and only there.
2735 
2736   Other characters outside of the encoding alphabet cause the function to
2737   reject the Base64 ASCII encoded data.
2738 
2739   @param[in] Source               Array of CHAR8 elements containing the Base64
2740                                   ASCII encoding. May be NULL if SourceSize is
2741                                   zero.
2742 
2743   @param[in] SourceSize           Number of CHAR8 elements in Source.
2744 
2745   @param[out] Destination         Array of UINT8 elements receiving the decoded
2746                                   8-bit binary representation. Allocated by the
2747                                   caller. May be NULL if DestinationSize is
2748                                   zero on input. If NULL, decoding is
2749                                   performed, but the 8-bit binary
2750                                   representation is not stored. If non-NULL and
2751                                   the function returns an error, the contents
2752                                   of Destination are indeterminate.
2753 
2754   @param[in,out] DestinationSize  On input, the number of UINT8 elements that
2755                                   the caller allocated for Destination. On
2756                                   output, if the function returns
2757                                   RETURN_SUCCESS or RETURN_BUFFER_TOO_SMALL,
2758                                   the number of UINT8 elements that are
2759                                   required for decoding the Base64 ASCII
2760                                   representation. If the function returns a
2761                                   value different from both RETURN_SUCCESS and
2762                                   RETURN_BUFFER_TOO_SMALL, then DestinationSize
2763                                   is indeterminate on output.
2764 
2765   @retval RETURN_SUCCESS            SourceSize CHAR8 elements at Source have
2766                                     been decoded to on-output DestinationSize
2767                                     UINT8 elements at Destination. Note that
2768                                     RETURN_SUCCESS covers the case when
2769                                     DestinationSize is zero on input, and
2770                                     Source decodes to zero bytes (due to
2771                                     containing at most ignored whitespace).
2772 
2773   @retval RETURN_BUFFER_TOO_SMALL   The input value of DestinationSize is not
2774                                     large enough for decoding SourceSize CHAR8
2775                                     elements at Source. The required number of
2776                                     UINT8 elements has been stored to
2777                                     DestinationSize.
2778 
2779   @retval RETURN_INVALID_PARAMETER  DestinationSize is NULL.
2780 
2781   @retval RETURN_INVALID_PARAMETER  Source is NULL, but SourceSize is not zero.
2782 
2783   @retval RETURN_INVALID_PARAMETER  Destination is NULL, but DestinationSize is
2784                                     not zero on input.
2785 
2786   @retval RETURN_INVALID_PARAMETER  Source is non-NULL, and (Source +
2787                                     SourceSize) would wrap around MAX_ADDRESS.
2788 
2789   @retval RETURN_INVALID_PARAMETER  Destination is non-NULL, and (Destination +
2790                                     DestinationSize) would wrap around
2791                                     MAX_ADDRESS, as specified on input.
2792 
2793   @retval RETURN_INVALID_PARAMETER  None of Source and Destination are NULL,
2794                                     and CHAR8[SourceSize] at Source overlaps
2795                                     UINT8[DestinationSize] at Destination, as
2796                                     specified on input.
2797 
2798   @retval RETURN_INVALID_PARAMETER  Invalid CHAR8 element encountered in
2799                                     Source.
2800 **/
2801 RETURN_STATUS
2802 EFIAPI
2803 Base64Decode (
2804   IN     CONST CHAR8  *Source          OPTIONAL,
2805   IN     UINTN        SourceSize,
2806   OUT    UINT8        *Destination     OPTIONAL,
2807   IN OUT UINTN        *DestinationSize
2808   );
2809 
2810 /**
2811   Converts an 8-bit value to an 8-bit BCD value.
2812 
2813   Converts the 8-bit value specified by Value to BCD. The BCD value is
2814   returned.
2815 
2816   If Value >= 100, then ASSERT().
2817 
2818   @param  Value The 8-bit value to convert to BCD. Range 0..99.
2819 
2820   @return The BCD value.
2821 
2822 **/
2823 UINT8
2824 EFIAPI
2825 DecimalToBcd8 (
2826   IN      UINT8  Value
2827   );
2828 
2829 /**
2830   Converts an 8-bit BCD value to an 8-bit value.
2831 
2832   Converts the 8-bit BCD value specified by Value to an 8-bit value. The 8-bit
2833   value is returned.
2834 
2835   If Value >= 0xA0, then ASSERT().
2836   If (Value & 0x0F) >= 0x0A, then ASSERT().
2837 
2838   @param  Value The 8-bit BCD value to convert to an 8-bit value.
2839 
2840   @return The 8-bit value is returned.
2841 
2842 **/
2843 UINT8
2844 EFIAPI
2845 BcdToDecimal8 (
2846   IN      UINT8  Value
2847   );
2848 
2849 //
2850 //  File Path Manipulation Functions
2851 //
2852 
2853 /**
2854   Removes the last directory or file entry in a path.
2855 
2856   @param[in, out] Path    The pointer to the path to modify.
2857 
2858   @retval FALSE     Nothing was found to remove.
2859   @retval TRUE      A directory or file was removed.
2860 **/
2861 BOOLEAN
2862 EFIAPI
2863 PathRemoveLastItem (
2864   IN OUT CHAR16  *Path
2865   );
2866 
2867 /**
2868   Function to clean up paths.
2869     - Single periods in the path are removed.
2870     - Double periods in the path are removed along with a single parent directory.
2871     - Forward slashes L'/' are converted to backward slashes L'\'.
2872 
2873   This will be done inline and the existing buffer may be larger than required
2874   upon completion.
2875 
2876   @param[in] Path       The pointer to the string containing the path.
2877 
2878   @return       Returns Path, otherwise returns NULL to indicate that an error has occurred.
2879 **/
2880 CHAR16 *
2881 EFIAPI
2882 PathCleanUpDirectories (
2883   IN CHAR16  *Path
2884   );
2885 
2886 //
2887 // Linked List Functions and Macros
2888 //
2889 
2890 /**
2891   Initializes the head node of a doubly linked list that is declared as a
2892   global variable in a module.
2893 
2894   Initializes the forward and backward links of a new linked list. After
2895   initializing a linked list with this macro, the other linked list functions
2896   may be used to add and remove nodes from the linked list. This macro results
2897   in smaller executables by initializing the linked list in the data section,
2898   instead if calling the InitializeListHead() function to perform the
2899   equivalent operation.
2900 
2901   @param  ListHead  The head note of a list to initialize.
2902 
2903 **/
2904 #define INITIALIZE_LIST_HEAD_VARIABLE(ListHead)  {&(ListHead), &(ListHead)}
2905 
2906 /**
2907   Iterates over each node in a doubly linked list using each node's forward link.
2908 
2909   @param  Entry     A pointer to a list node used as a loop cursor during iteration
2910   @param  ListHead  The head node of the doubly linked list
2911 
2912 **/
2913 #define BASE_LIST_FOR_EACH(Entry, ListHead)    \
2914   for(Entry = (ListHead)->ForwardLink; Entry != (ListHead); Entry = Entry->ForwardLink)
2915 
2916 /**
2917   Iterates over each node in a doubly linked list using each node's forward link
2918   with safety against node removal.
2919 
2920   This macro uses NextEntry to temporarily store the next list node so the node
2921   pointed to by Entry may be deleted in the current loop iteration step and
2922   iteration can continue from the node pointed to by NextEntry.
2923 
2924   @param  Entry     A pointer to a list node used as a loop cursor during iteration
2925   @param  NextEntry A pointer to a list node used to temporarily store the next node
2926   @param  ListHead  The head node of the doubly linked list
2927 
2928 **/
2929 #define BASE_LIST_FOR_EACH_SAFE(Entry, NextEntry, ListHead)            \
2930   for(Entry = (ListHead)->ForwardLink, NextEntry = Entry->ForwardLink;\
2931       Entry != (ListHead); Entry = NextEntry, NextEntry = Entry->ForwardLink)
2932 
2933 /**
2934   Checks whether FirstEntry and SecondEntry are part of the same doubly-linked
2935   list.
2936 
2937   If FirstEntry is NULL, then ASSERT().
2938   If FirstEntry->ForwardLink is NULL, then ASSERT().
2939   If FirstEntry->BackLink is NULL, then ASSERT().
2940   If SecondEntry is NULL, then ASSERT();
2941   If PcdMaximumLinkedListLength is not zero, and List contains more than
2942   PcdMaximumLinkedListLength nodes, then ASSERT().
2943 
2944   @param  FirstEntry   A pointer to a node in a linked list.
2945   @param  SecondEntry  A pointer to the node to locate.
2946 
2947   @retval TRUE   SecondEntry is in the same doubly-linked list as FirstEntry.
2948   @retval FALSE  SecondEntry isn't in the same doubly-linked list as FirstEntry,
2949                  or FirstEntry is invalid.
2950 
2951 **/
2952 BOOLEAN
2953 EFIAPI
2954 IsNodeInList (
2955   IN      CONST LIST_ENTRY  *FirstEntry,
2956   IN      CONST LIST_ENTRY  *SecondEntry
2957   );
2958 
2959 /**
2960   Initializes the head node of a doubly linked list, and returns the pointer to
2961   the head node of the doubly linked list.
2962 
2963   Initializes the forward and backward links of a new linked list. After
2964   initializing a linked list with this function, the other linked list
2965   functions may be used to add and remove nodes from the linked list. It is up
2966   to the caller of this function to allocate the memory for ListHead.
2967 
2968   If ListHead is NULL, then ASSERT().
2969 
2970   @param  ListHead  A pointer to the head node of a new doubly linked list.
2971 
2972   @return ListHead
2973 
2974 **/
2975 LIST_ENTRY *
2976 EFIAPI
2977 InitializeListHead (
2978   IN OUT  LIST_ENTRY  *ListHead
2979   );
2980 
2981 /**
2982   Adds a node to the beginning of a doubly linked list, and returns the pointer
2983   to the head node of the doubly linked list.
2984 
2985   Adds the node Entry at the beginning of the doubly linked list denoted by
2986   ListHead, and returns ListHead.
2987 
2988   If ListHead is NULL, then ASSERT().
2989   If Entry is NULL, then ASSERT().
2990   If ListHead was not initialized with INTIALIZE_LIST_HEAD_VARIABLE() or
2991   InitializeListHead(), then ASSERT().
2992   If PcdMaximumLinkedListLength is not zero, and prior to insertion the number
2993   of nodes in ListHead, including the ListHead node, is greater than or
2994   equal to PcdMaximumLinkedListLength, then ASSERT().
2995 
2996   @param  ListHead  A pointer to the head node of a doubly linked list.
2997   @param  Entry     A pointer to a node that is to be inserted at the beginning
2998                     of a doubly linked list.
2999 
3000   @return ListHead
3001 
3002 **/
3003 LIST_ENTRY *
3004 EFIAPI
3005 InsertHeadList (
3006   IN OUT  LIST_ENTRY  *ListHead,
3007   IN OUT  LIST_ENTRY  *Entry
3008   );
3009 
3010 /**
3011   Adds a node to the end of a doubly linked list, and returns the pointer to
3012   the head node of the doubly linked list.
3013 
3014   Adds the node Entry to the end of the doubly linked list denoted by ListHead,
3015   and returns ListHead.
3016 
3017   If ListHead is NULL, then ASSERT().
3018   If Entry is NULL, then ASSERT().
3019   If ListHead was not initialized with INTIALIZE_LIST_HEAD_VARIABLE() or
3020   InitializeListHead(), then ASSERT().
3021   If PcdMaximumLinkedListLength is not zero, and prior to insertion the number
3022   of nodes in ListHead, including the ListHead node, is greater than or
3023   equal to PcdMaximumLinkedListLength, then ASSERT().
3024 
3025   @param  ListHead  A pointer to the head node of a doubly linked list.
3026   @param  Entry     A pointer to a node that is to be added at the end of the
3027                     doubly linked list.
3028 
3029   @return ListHead
3030 
3031 **/
3032 LIST_ENTRY *
3033 EFIAPI
3034 InsertTailList (
3035   IN OUT  LIST_ENTRY  *ListHead,
3036   IN OUT  LIST_ENTRY  *Entry
3037   );
3038 
3039 /**
3040   Retrieves the first node of a doubly linked list.
3041 
3042   Returns the first node of a doubly linked list.  List must have been
3043   initialized with INTIALIZE_LIST_HEAD_VARIABLE() or InitializeListHead().
3044   If List is empty, then List is returned.
3045 
3046   If List is NULL, then ASSERT().
3047   If List was not initialized with INTIALIZE_LIST_HEAD_VARIABLE() or
3048   InitializeListHead(), then ASSERT().
3049   If PcdMaximumLinkedListLength is not zero, and the number of nodes
3050   in List, including the List node, is greater than or equal to
3051   PcdMaximumLinkedListLength, then ASSERT().
3052 
3053   @param  List  A pointer to the head node of a doubly linked list.
3054 
3055   @return The first node of a doubly linked list.
3056   @retval List  The list is empty.
3057 
3058 **/
3059 LIST_ENTRY *
3060 EFIAPI
3061 GetFirstNode (
3062   IN      CONST LIST_ENTRY  *List
3063   );
3064 
3065 /**
3066   Retrieves the next node of a doubly linked list.
3067 
3068   Returns the node of a doubly linked list that follows Node.
3069   List must have been initialized with INTIALIZE_LIST_HEAD_VARIABLE()
3070   or InitializeListHead().  If List is empty, then List is returned.
3071 
3072   If List is NULL, then ASSERT().
3073   If Node is NULL, then ASSERT().
3074   If List was not initialized with INTIALIZE_LIST_HEAD_VARIABLE() or
3075   InitializeListHead(), then ASSERT().
3076   If PcdMaximumLinkedListLength is not zero, and List contains more than
3077   PcdMaximumLinkedListLength nodes, then ASSERT().
3078   If PcdVerifyNodeInList is TRUE and Node is not a node in List, then ASSERT().
3079 
3080   @param  List  A pointer to the head node of a doubly linked list.
3081   @param  Node  A pointer to a node in the doubly linked list.
3082 
3083   @return The pointer to the next node if one exists. Otherwise List is returned.
3084 
3085 **/
3086 LIST_ENTRY *
3087 EFIAPI
3088 GetNextNode (
3089   IN      CONST LIST_ENTRY  *List,
3090   IN      CONST LIST_ENTRY  *Node
3091   );
3092 
3093 /**
3094   Retrieves the previous node of a doubly linked list.
3095 
3096   Returns the node of a doubly linked list that precedes Node.
3097   List must have been initialized with INTIALIZE_LIST_HEAD_VARIABLE()
3098   or InitializeListHead().  If List is empty, then List is returned.
3099 
3100   If List is NULL, then ASSERT().
3101   If Node is NULL, then ASSERT().
3102   If List was not initialized with INTIALIZE_LIST_HEAD_VARIABLE() or
3103   InitializeListHead(), then ASSERT().
3104   If PcdMaximumLinkedListLength is not zero, and List contains more than
3105   PcdMaximumLinkedListLength nodes, then ASSERT().
3106   If PcdVerifyNodeInList is TRUE and Node is not a node in List, then ASSERT().
3107 
3108   @param  List  A pointer to the head node of a doubly linked list.
3109   @param  Node  A pointer to a node in the doubly linked list.
3110 
3111   @return The pointer to the previous node if one exists. Otherwise List is returned.
3112 
3113 **/
3114 LIST_ENTRY *
3115 EFIAPI
3116 GetPreviousNode (
3117   IN      CONST LIST_ENTRY  *List,
3118   IN      CONST LIST_ENTRY  *Node
3119   );
3120 
3121 /**
3122   Checks to see if a doubly linked list is empty or not.
3123 
3124   Checks to see if the doubly linked list is empty. If the linked list contains
3125   zero nodes, this function returns TRUE. Otherwise, it returns FALSE.
3126 
3127   If ListHead is NULL, then ASSERT().
3128   If ListHead was not initialized with INTIALIZE_LIST_HEAD_VARIABLE() or
3129   InitializeListHead(), then ASSERT().
3130   If PcdMaximumLinkedListLength is not zero, and the number of nodes
3131   in List, including the List node, is greater than or equal to
3132   PcdMaximumLinkedListLength, then ASSERT().
3133 
3134   @param  ListHead  A pointer to the head node of a doubly linked list.
3135 
3136   @retval TRUE  The linked list is empty.
3137   @retval FALSE The linked list is not empty.
3138 
3139 **/
3140 BOOLEAN
3141 EFIAPI
3142 IsListEmpty (
3143   IN      CONST LIST_ENTRY  *ListHead
3144   );
3145 
3146 /**
3147   Determines if a node in a doubly linked list is the head node of a the same
3148   doubly linked list.  This function is typically used to terminate a loop that
3149   traverses all the nodes in a doubly linked list starting with the head node.
3150 
3151   Returns TRUE if Node is equal to List.  Returns FALSE if Node is one of the
3152   nodes in the doubly linked list specified by List.  List must have been
3153   initialized with INTIALIZE_LIST_HEAD_VARIABLE() or InitializeListHead().
3154 
3155   If List is NULL, then ASSERT().
3156   If Node is NULL, then ASSERT().
3157   If List was not initialized with INTIALIZE_LIST_HEAD_VARIABLE() or InitializeListHead(),
3158   then ASSERT().
3159   If PcdMaximumLinkedListLength is not zero, and the number of nodes
3160   in List, including the List node, is greater than or equal to
3161   PcdMaximumLinkedListLength, then ASSERT().
3162   If PcdVerifyNodeInList is TRUE and Node is not a node in List the and Node is not equal
3163   to List, then ASSERT().
3164 
3165   @param  List  A pointer to the head node of a doubly linked list.
3166   @param  Node  A pointer to a node in the doubly linked list.
3167 
3168   @retval TRUE  Node is the head of the doubly-linked list pointed by List.
3169   @retval FALSE Node is not the head of the doubly-linked list pointed by List.
3170 
3171 **/
3172 BOOLEAN
3173 EFIAPI
3174 IsNull (
3175   IN      CONST LIST_ENTRY  *List,
3176   IN      CONST LIST_ENTRY  *Node
3177   );
3178 
3179 /**
3180   Determines if a node the last node in a doubly linked list.
3181 
3182   Returns TRUE if Node is the last node in the doubly linked list specified by
3183   List. Otherwise, FALSE is returned. List must have been initialized with
3184   INTIALIZE_LIST_HEAD_VARIABLE() or InitializeListHead().
3185 
3186   If List is NULL, then ASSERT().
3187   If Node is NULL, then ASSERT().
3188   If List was not initialized with INTIALIZE_LIST_HEAD_VARIABLE() or
3189   InitializeListHead(), then ASSERT().
3190   If PcdMaximumLinkedListLength is not zero, and the number of nodes
3191   in List, including the List node, is greater than or equal to
3192   PcdMaximumLinkedListLength, then ASSERT().
3193   If PcdVerifyNodeInList is TRUE and Node is not a node in List, then ASSERT().
3194 
3195   @param  List  A pointer to the head node of a doubly linked list.
3196   @param  Node  A pointer to a node in the doubly linked list.
3197 
3198   @retval TRUE  Node is the last node in the linked list.
3199   @retval FALSE Node is not the last node in the linked list.
3200 
3201 **/
3202 BOOLEAN
3203 EFIAPI
3204 IsNodeAtEnd (
3205   IN      CONST LIST_ENTRY  *List,
3206   IN      CONST LIST_ENTRY  *Node
3207   );
3208 
3209 /**
3210   Swaps the location of two nodes in a doubly linked list, and returns the
3211   first node after the swap.
3212 
3213   If FirstEntry is identical to SecondEntry, then SecondEntry is returned.
3214   Otherwise, the location of the FirstEntry node is swapped with the location
3215   of the SecondEntry node in a doubly linked list. SecondEntry must be in the
3216   same double linked list as FirstEntry and that double linked list must have
3217   been initialized with INTIALIZE_LIST_HEAD_VARIABLE() or InitializeListHead().
3218   SecondEntry is returned after the nodes are swapped.
3219 
3220   If FirstEntry is NULL, then ASSERT().
3221   If SecondEntry is NULL, then ASSERT().
3222   If PcdVerifyNodeInList is TRUE and SecondEntry and FirstEntry are not in the
3223   same linked list, then ASSERT().
3224   If PcdMaximumLinkedListLength is not zero, and the number of nodes in the
3225   linked list containing the FirstEntry and SecondEntry nodes, including
3226   the FirstEntry and SecondEntry nodes, is greater than or equal to
3227   PcdMaximumLinkedListLength, then ASSERT().
3228 
3229   @param  FirstEntry  A pointer to a node in a linked list.
3230   @param  SecondEntry A pointer to another node in the same linked list.
3231 
3232   @return SecondEntry.
3233 
3234 **/
3235 LIST_ENTRY *
3236 EFIAPI
3237 SwapListEntries (
3238   IN OUT  LIST_ENTRY  *FirstEntry,
3239   IN OUT  LIST_ENTRY  *SecondEntry
3240   );
3241 
3242 /**
3243   Removes a node from a doubly linked list, and returns the node that follows
3244   the removed node.
3245 
3246   Removes the node Entry from a doubly linked list. It is up to the caller of
3247   this function to release the memory used by this node if that is required. On
3248   exit, the node following Entry in the doubly linked list is returned. If
3249   Entry is the only node in the linked list, then the head node of the linked
3250   list is returned.
3251 
3252   If Entry is NULL, then ASSERT().
3253   If Entry is the head node of an empty list, then ASSERT().
3254   If PcdMaximumLinkedListLength is not zero, and the number of nodes in the
3255   linked list containing Entry, including the Entry node, is greater than
3256   or equal to PcdMaximumLinkedListLength, then ASSERT().
3257 
3258   @param  Entry A pointer to a node in a linked list.
3259 
3260   @return Entry.
3261 
3262 **/
3263 LIST_ENTRY *
3264 EFIAPI
3265 RemoveEntryList (
3266   IN      CONST LIST_ENTRY  *Entry
3267   );
3268 
3269 //
3270 // Math Services
3271 //
3272 
3273 /**
3274   Prototype for comparison function for any two element types.
3275 
3276   @param[in] Buffer1                  The pointer to first buffer.
3277   @param[in] Buffer2                  The pointer to second buffer.
3278 
3279   @retval 0                           Buffer1 equal to Buffer2.
3280   @return <0                          Buffer1 is less than Buffer2.
3281   @return >0                          Buffer1 is greater than Buffer2.
3282 **/
3283 typedef
3284 INTN
3285 (EFIAPI *BASE_SORT_COMPARE)(
3286   IN CONST VOID                 *Buffer1,
3287   IN CONST VOID                 *Buffer2
3288   );
3289 
3290 /**
3291   This function is identical to perform QuickSort,
3292   except that is uses the pre-allocated buffer so the in place sorting does not need to
3293   allocate and free buffers constantly.
3294 
3295   Each element must be equal sized.
3296 
3297   if BufferToSort is NULL, then ASSERT.
3298   if CompareFunction is NULL, then ASSERT.
3299   if BufferOneElement is NULL, then ASSERT.
3300   if ElementSize is < 1, then ASSERT.
3301 
3302   if Count is < 2 then perform no action.
3303 
3304   @param[in, out] BufferToSort   on call a Buffer of (possibly sorted) elements
3305                                  on return a buffer of sorted elements
3306   @param[in] Count               the number of elements in the buffer to sort
3307   @param[in] ElementSize         Size of an element in bytes
3308   @param[in] CompareFunction     The function to call to perform the comparison
3309                                  of any 2 elements
3310   @param[out] BufferOneElement   Caller provided buffer whose size equals to ElementSize.
3311                                  It's used by QuickSort() for swapping in sorting.
3312 **/
3313 VOID
3314 EFIAPI
3315 QuickSort (
3316   IN OUT VOID                 *BufferToSort,
3317   IN CONST UINTN              Count,
3318   IN CONST UINTN              ElementSize,
3319   IN       BASE_SORT_COMPARE  CompareFunction,
3320   OUT VOID                    *BufferOneElement
3321   );
3322 
3323 /**
3324   Shifts a 64-bit integer left between 0 and 63 bits. The low bits are filled
3325   with zeros. The shifted value is returned.
3326 
3327   This function shifts the 64-bit value Operand to the left by Count bits. The
3328   low Count bits are set to zero. The shifted value is returned.
3329 
3330   If Count is greater than 63, then ASSERT().
3331 
3332   @param  Operand The 64-bit operand to shift left.
3333   @param  Count   The number of bits to shift left.
3334 
3335   @return Operand << Count.
3336 
3337 **/
3338 UINT64
3339 EFIAPI
3340 LShiftU64 (
3341   IN      UINT64  Operand,
3342   IN      UINTN   Count
3343   );
3344 
3345 /**
3346   Shifts a 64-bit integer right between 0 and 63 bits. This high bits are
3347   filled with zeros. The shifted value is returned.
3348 
3349   This function shifts the 64-bit value Operand to the right by Count bits. The
3350   high Count bits are set to zero. The shifted value is returned.
3351 
3352   If Count is greater than 63, then ASSERT().
3353 
3354   @param  Operand The 64-bit operand to shift right.
3355   @param  Count   The number of bits to shift right.
3356 
3357   @return Operand >> Count
3358 
3359 **/
3360 UINT64
3361 EFIAPI
3362 RShiftU64 (
3363   IN      UINT64  Operand,
3364   IN      UINTN   Count
3365   );
3366 
3367 /**
3368   Shifts a 64-bit integer right between 0 and 63 bits. The high bits are filled
3369   with original integer's bit 63. The shifted value is returned.
3370 
3371   This function shifts the 64-bit value Operand to the right by Count bits. The
3372   high Count bits are set to bit 63 of Operand.  The shifted value is returned.
3373 
3374   If Count is greater than 63, then ASSERT().
3375 
3376   @param  Operand The 64-bit operand to shift right.
3377   @param  Count   The number of bits to shift right.
3378 
3379   @return Operand >> Count
3380 
3381 **/
3382 UINT64
3383 EFIAPI
3384 ARShiftU64 (
3385   IN      UINT64  Operand,
3386   IN      UINTN   Count
3387   );
3388 
3389 /**
3390   Rotates a 32-bit integer left between 0 and 31 bits, filling the low bits
3391   with the high bits that were rotated.
3392 
3393   This function rotates the 32-bit value Operand to the left by Count bits. The
3394   low Count bits are fill with the high Count bits of Operand. The rotated
3395   value is returned.
3396 
3397   If Count is greater than 31, then ASSERT().
3398 
3399   @param  Operand The 32-bit operand to rotate left.
3400   @param  Count   The number of bits to rotate left.
3401 
3402   @return Operand << Count
3403 
3404 **/
3405 UINT32
3406 EFIAPI
3407 LRotU32 (
3408   IN      UINT32  Operand,
3409   IN      UINTN   Count
3410   );
3411 
3412 /**
3413   Rotates a 32-bit integer right between 0 and 31 bits, filling the high bits
3414   with the low bits that were rotated.
3415 
3416   This function rotates the 32-bit value Operand to the right by Count bits.
3417   The high Count bits are fill with the low Count bits of Operand. The rotated
3418   value is returned.
3419 
3420   If Count is greater than 31, then ASSERT().
3421 
3422   @param  Operand The 32-bit operand to rotate right.
3423   @param  Count   The number of bits to rotate right.
3424 
3425   @return Operand >> Count
3426 
3427 **/
3428 UINT32
3429 EFIAPI
3430 RRotU32 (
3431   IN      UINT32  Operand,
3432   IN      UINTN   Count
3433   );
3434 
3435 /**
3436   Rotates a 64-bit integer left between 0 and 63 bits, filling the low bits
3437   with the high bits that were rotated.
3438 
3439   This function rotates the 64-bit value Operand to the left by Count bits. The
3440   low Count bits are fill with the high Count bits of Operand. The rotated
3441   value is returned.
3442 
3443   If Count is greater than 63, then ASSERT().
3444 
3445   @param  Operand The 64-bit operand to rotate left.
3446   @param  Count   The number of bits to rotate left.
3447 
3448   @return Operand << Count
3449 
3450 **/
3451 UINT64
3452 EFIAPI
3453 LRotU64 (
3454   IN      UINT64  Operand,
3455   IN      UINTN   Count
3456   );
3457 
3458 /**
3459   Rotates a 64-bit integer right between 0 and 63 bits, filling the high bits
3460   with the high low bits that were rotated.
3461 
3462   This function rotates the 64-bit value Operand to the right by Count bits.
3463   The high Count bits are fill with the low Count bits of Operand. The rotated
3464   value is returned.
3465 
3466   If Count is greater than 63, then ASSERT().
3467 
3468   @param  Operand The 64-bit operand to rotate right.
3469   @param  Count   The number of bits to rotate right.
3470 
3471   @return Operand >> Count
3472 
3473 **/
3474 UINT64
3475 EFIAPI
3476 RRotU64 (
3477   IN      UINT64  Operand,
3478   IN      UINTN   Count
3479   );
3480 
3481 /**
3482   Returns the bit position of the lowest bit set in a 32-bit value.
3483 
3484   This function computes the bit position of the lowest bit set in the 32-bit
3485   value specified by Operand. If Operand is zero, then -1 is returned.
3486   Otherwise, a value between 0 and 31 is returned.
3487 
3488   @param  Operand The 32-bit operand to evaluate.
3489 
3490   @retval 0..31  The lowest bit set in Operand was found.
3491   @retval -1    Operand is zero.
3492 
3493 **/
3494 INTN
3495 EFIAPI
3496 LowBitSet32 (
3497   IN      UINT32  Operand
3498   );
3499 
3500 /**
3501   Returns the bit position of the lowest bit set in a 64-bit value.
3502 
3503   This function computes the bit position of the lowest bit set in the 64-bit
3504   value specified by Operand. If Operand is zero, then -1 is returned.
3505   Otherwise, a value between 0 and 63 is returned.
3506 
3507   @param  Operand The 64-bit operand to evaluate.
3508 
3509   @retval 0..63  The lowest bit set in Operand was found.
3510   @retval -1    Operand is zero.
3511 
3512 
3513 **/
3514 INTN
3515 EFIAPI
3516 LowBitSet64 (
3517   IN      UINT64  Operand
3518   );
3519 
3520 /**
3521   Returns the bit position of the highest bit set in a 32-bit value. Equivalent
3522   to log2(x).
3523 
3524   This function computes the bit position of the highest bit set in the 32-bit
3525   value specified by Operand. If Operand is zero, then -1 is returned.
3526   Otherwise, a value between 0 and 31 is returned.
3527 
3528   @param  Operand The 32-bit operand to evaluate.
3529 
3530   @retval 0..31  Position of the highest bit set in Operand if found.
3531   @retval -1    Operand is zero.
3532 
3533 **/
3534 INTN
3535 EFIAPI
3536 HighBitSet32 (
3537   IN      UINT32  Operand
3538   );
3539 
3540 /**
3541   Returns the bit position of the highest bit set in a 64-bit value. Equivalent
3542   to log2(x).
3543 
3544   This function computes the bit position of the highest bit set in the 64-bit
3545   value specified by Operand. If Operand is zero, then -1 is returned.
3546   Otherwise, a value between 0 and 63 is returned.
3547 
3548   @param  Operand The 64-bit operand to evaluate.
3549 
3550   @retval 0..63   Position of the highest bit set in Operand if found.
3551   @retval -1     Operand is zero.
3552 
3553 **/
3554 INTN
3555 EFIAPI
3556 HighBitSet64 (
3557   IN      UINT64  Operand
3558   );
3559 
3560 /**
3561   Returns the value of the highest bit set in a 32-bit value. Equivalent to
3562   1 << log2(x).
3563 
3564   This function computes the value of the highest bit set in the 32-bit value
3565   specified by Operand. If Operand is zero, then zero is returned.
3566 
3567   @param  Operand The 32-bit operand to evaluate.
3568 
3569   @return 1 << HighBitSet32(Operand)
3570   @retval 0 Operand is zero.
3571 
3572 **/
3573 UINT32
3574 EFIAPI
3575 GetPowerOfTwo32 (
3576   IN      UINT32  Operand
3577   );
3578 
3579 /**
3580   Returns the value of the highest bit set in a 64-bit value. Equivalent to
3581   1 << log2(x).
3582 
3583   This function computes the value of the highest bit set in the 64-bit value
3584   specified by Operand. If Operand is zero, then zero is returned.
3585 
3586   @param  Operand The 64-bit operand to evaluate.
3587 
3588   @return 1 << HighBitSet64(Operand)
3589   @retval 0 Operand is zero.
3590 
3591 **/
3592 UINT64
3593 EFIAPI
3594 GetPowerOfTwo64 (
3595   IN      UINT64  Operand
3596   );
3597 
3598 /**
3599   Switches the endianness of a 16-bit integer.
3600 
3601   This function swaps the bytes in a 16-bit unsigned value to switch the value
3602   from little endian to big endian or vice versa. The byte swapped value is
3603   returned.
3604 
3605   @param  Value A 16-bit unsigned value.
3606 
3607   @return The byte swapped Value.
3608 
3609 **/
3610 UINT16
3611 EFIAPI
3612 SwapBytes16 (
3613   IN      UINT16  Value
3614   );
3615 
3616 /**
3617   Switches the endianness of a 32-bit integer.
3618 
3619   This function swaps the bytes in a 32-bit unsigned value to switch the value
3620   from little endian to big endian or vice versa. The byte swapped value is
3621   returned.
3622 
3623   @param  Value A 32-bit unsigned value.
3624 
3625   @return The byte swapped Value.
3626 
3627 **/
3628 UINT32
3629 EFIAPI
3630 SwapBytes32 (
3631   IN      UINT32  Value
3632   );
3633 
3634 /**
3635   Switches the endianness of a 64-bit integer.
3636 
3637   This function swaps the bytes in a 64-bit unsigned value to switch the value
3638   from little endian to big endian or vice versa. The byte swapped value is
3639   returned.
3640 
3641   @param  Value A 64-bit unsigned value.
3642 
3643   @return The byte swapped Value.
3644 
3645 **/
3646 UINT64
3647 EFIAPI
3648 SwapBytes64 (
3649   IN      UINT64  Value
3650   );
3651 
3652 /**
3653   Multiples a 64-bit unsigned integer by a 32-bit unsigned integer and
3654   generates a 64-bit unsigned result.
3655 
3656   This function multiples the 64-bit unsigned value Multiplicand by the 32-bit
3657   unsigned value Multiplier and generates a 64-bit unsigned result. This 64-
3658   bit unsigned result is returned.
3659 
3660   @param  Multiplicand  A 64-bit unsigned value.
3661   @param  Multiplier    A 32-bit unsigned value.
3662 
3663   @return Multiplicand * Multiplier
3664 
3665 **/
3666 UINT64
3667 EFIAPI
3668 MultU64x32 (
3669   IN      UINT64  Multiplicand,
3670   IN      UINT32  Multiplier
3671   );
3672 
3673 /**
3674   Multiples a 64-bit unsigned integer by a 64-bit unsigned integer and
3675   generates a 64-bit unsigned result.
3676 
3677   This function multiples the 64-bit unsigned value Multiplicand by the 64-bit
3678   unsigned value Multiplier and generates a 64-bit unsigned result. This 64-
3679   bit unsigned result is returned.
3680 
3681   @param  Multiplicand  A 64-bit unsigned value.
3682   @param  Multiplier    A 64-bit unsigned value.
3683 
3684   @return Multiplicand * Multiplier.
3685 
3686 **/
3687 UINT64
3688 EFIAPI
3689 MultU64x64 (
3690   IN      UINT64  Multiplicand,
3691   IN      UINT64  Multiplier
3692   );
3693 
3694 /**
3695   Multiples a 64-bit signed integer by a 64-bit signed integer and generates a
3696   64-bit signed result.
3697 
3698   This function multiples the 64-bit signed value Multiplicand by the 64-bit
3699   signed value Multiplier and generates a 64-bit signed result. This 64-bit
3700   signed result is returned.
3701 
3702   @param  Multiplicand  A 64-bit signed value.
3703   @param  Multiplier    A 64-bit signed value.
3704 
3705   @return Multiplicand * Multiplier
3706 
3707 **/
3708 INT64
3709 EFIAPI
3710 MultS64x64 (
3711   IN      INT64  Multiplicand,
3712   IN      INT64  Multiplier
3713   );
3714 
3715 /**
3716   Divides a 64-bit unsigned integer by a 32-bit unsigned integer and generates
3717   a 64-bit unsigned result.
3718 
3719   This function divides the 64-bit unsigned value Dividend by the 32-bit
3720   unsigned value Divisor and generates a 64-bit unsigned quotient. This
3721   function returns the 64-bit unsigned quotient.
3722 
3723   If Divisor is 0, then ASSERT().
3724 
3725   @param  Dividend  A 64-bit unsigned value.
3726   @param  Divisor   A 32-bit unsigned value.
3727 
3728   @return Dividend / Divisor.
3729 
3730 **/
3731 UINT64
3732 EFIAPI
3733 DivU64x32 (
3734   IN      UINT64  Dividend,
3735   IN      UINT32  Divisor
3736   );
3737 
3738 /**
3739   Divides a 64-bit unsigned integer by a 32-bit unsigned integer and generates
3740   a 32-bit unsigned remainder.
3741 
3742   This function divides the 64-bit unsigned value Dividend by the 32-bit
3743   unsigned value Divisor and generates a 32-bit remainder. This function
3744   returns the 32-bit unsigned remainder.
3745 
3746   If Divisor is 0, then ASSERT().
3747 
3748   @param  Dividend  A 64-bit unsigned value.
3749   @param  Divisor   A 32-bit unsigned value.
3750 
3751   @return Dividend % Divisor.
3752 
3753 **/
3754 UINT32
3755 EFIAPI
3756 ModU64x32 (
3757   IN      UINT64  Dividend,
3758   IN      UINT32  Divisor
3759   );
3760 
3761 /**
3762   Divides a 64-bit unsigned integer by a 32-bit unsigned integer and generates
3763   a 64-bit unsigned result and an optional 32-bit unsigned remainder.
3764 
3765   This function divides the 64-bit unsigned value Dividend by the 32-bit
3766   unsigned value Divisor and generates a 64-bit unsigned quotient. If Remainder
3767   is not NULL, then the 32-bit unsigned remainder is returned in Remainder.
3768   This function returns the 64-bit unsigned quotient.
3769 
3770   If Divisor is 0, then ASSERT().
3771 
3772   @param  Dividend  A 64-bit unsigned value.
3773   @param  Divisor   A 32-bit unsigned value.
3774   @param  Remainder A pointer to a 32-bit unsigned value. This parameter is
3775                     optional and may be NULL.
3776 
3777   @return Dividend / Divisor.
3778 
3779 **/
3780 UINT64
3781 EFIAPI
3782 DivU64x32Remainder (
3783   IN      UINT64  Dividend,
3784   IN      UINT32  Divisor,
3785   OUT     UINT32  *Remainder  OPTIONAL
3786   );
3787 
3788 /**
3789   Divides a 64-bit unsigned integer by a 64-bit unsigned integer and generates
3790   a 64-bit unsigned result and an optional 64-bit unsigned remainder.
3791 
3792   This function divides the 64-bit unsigned value Dividend by the 64-bit
3793   unsigned value Divisor and generates a 64-bit unsigned quotient. If Remainder
3794   is not NULL, then the 64-bit unsigned remainder is returned in Remainder.
3795   This function returns the 64-bit unsigned quotient.
3796 
3797   If Divisor is 0, then ASSERT().
3798 
3799   @param  Dividend  A 64-bit unsigned value.
3800   @param  Divisor   A 64-bit unsigned value.
3801   @param  Remainder A pointer to a 64-bit unsigned value. This parameter is
3802                     optional and may be NULL.
3803 
3804   @return Dividend / Divisor.
3805 
3806 **/
3807 UINT64
3808 EFIAPI
3809 DivU64x64Remainder (
3810   IN      UINT64  Dividend,
3811   IN      UINT64  Divisor,
3812   OUT     UINT64  *Remainder  OPTIONAL
3813   );
3814 
3815 /**
3816   Divides a 64-bit signed integer by a 64-bit signed integer and generates a
3817   64-bit signed result and a optional 64-bit signed remainder.
3818 
3819   This function divides the 64-bit signed value Dividend by the 64-bit signed
3820   value Divisor and generates a 64-bit signed quotient. If Remainder is not
3821   NULL, then the 64-bit signed remainder is returned in Remainder. This
3822   function returns the 64-bit signed quotient.
3823 
3824   It is the caller's responsibility to not call this function with a Divisor of 0.
3825   If Divisor is 0, then the quotient and remainder should be assumed to be
3826   the largest negative integer.
3827 
3828   If Divisor is 0, then ASSERT().
3829 
3830   @param  Dividend  A 64-bit signed value.
3831   @param  Divisor   A 64-bit signed value.
3832   @param  Remainder A pointer to a 64-bit signed value. This parameter is
3833                     optional and may be NULL.
3834 
3835   @return Dividend / Divisor.
3836 
3837 **/
3838 INT64
3839 EFIAPI
3840 DivS64x64Remainder (
3841   IN      INT64  Dividend,
3842   IN      INT64  Divisor,
3843   OUT     INT64  *Remainder  OPTIONAL
3844   );
3845 
3846 /**
3847   Reads a 16-bit value from memory that may be unaligned.
3848 
3849   This function returns the 16-bit value pointed to by Buffer. The function
3850   guarantees that the read operation does not produce an alignment fault.
3851 
3852   If the Buffer is NULL, then ASSERT().
3853 
3854   @param  Buffer  The pointer to a 16-bit value that may be unaligned.
3855 
3856   @return The 16-bit value read from Buffer.
3857 
3858 **/
3859 UINT16
3860 EFIAPI
3861 ReadUnaligned16 (
3862   IN CONST UINT16  *Buffer
3863   );
3864 
3865 /**
3866   Writes a 16-bit value to memory that may be unaligned.
3867 
3868   This function writes the 16-bit value specified by Value to Buffer. Value is
3869   returned. The function guarantees that the write operation does not produce
3870   an alignment fault.
3871 
3872   If the Buffer is NULL, then ASSERT().
3873 
3874   @param  Buffer  The pointer to a 16-bit value that may be unaligned.
3875   @param  Value   16-bit value to write to Buffer.
3876 
3877   @return The 16-bit value to write to Buffer.
3878 
3879 **/
3880 UINT16
3881 EFIAPI
3882 WriteUnaligned16 (
3883   OUT UINT16  *Buffer,
3884   IN  UINT16  Value
3885   );
3886 
3887 /**
3888   Reads a 24-bit value from memory that may be unaligned.
3889 
3890   This function returns the 24-bit value pointed to by Buffer. The function
3891   guarantees that the read operation does not produce an alignment fault.
3892 
3893   If the Buffer is NULL, then ASSERT().
3894 
3895   @param  Buffer  The pointer to a 24-bit value that may be unaligned.
3896 
3897   @return The 24-bit value read from Buffer.
3898 
3899 **/
3900 UINT32
3901 EFIAPI
3902 ReadUnaligned24 (
3903   IN CONST UINT32  *Buffer
3904   );
3905 
3906 /**
3907   Writes a 24-bit value to memory that may be unaligned.
3908 
3909   This function writes the 24-bit value specified by Value to Buffer. Value is
3910   returned. The function guarantees that the write operation does not produce
3911   an alignment fault.
3912 
3913   If the Buffer is NULL, then ASSERT().
3914 
3915   @param  Buffer  The pointer to a 24-bit value that may be unaligned.
3916   @param  Value   24-bit value to write to Buffer.
3917 
3918   @return The 24-bit value to write to Buffer.
3919 
3920 **/
3921 UINT32
3922 EFIAPI
3923 WriteUnaligned24 (
3924   OUT UINT32  *Buffer,
3925   IN  UINT32  Value
3926   );
3927 
3928 /**
3929   Reads a 32-bit value from memory that may be unaligned.
3930 
3931   This function returns the 32-bit value pointed to by Buffer. The function
3932   guarantees that the read operation does not produce an alignment fault.
3933 
3934   If the Buffer is NULL, then ASSERT().
3935 
3936   @param  Buffer  The pointer to a 32-bit value that may be unaligned.
3937 
3938   @return The 32-bit value read from Buffer.
3939 
3940 **/
3941 UINT32
3942 EFIAPI
3943 ReadUnaligned32 (
3944   IN CONST UINT32  *Buffer
3945   );
3946 
3947 /**
3948   Writes a 32-bit value to memory that may be unaligned.
3949 
3950   This function writes the 32-bit value specified by Value to Buffer. Value is
3951   returned. The function guarantees that the write operation does not produce
3952   an alignment fault.
3953 
3954   If the Buffer is NULL, then ASSERT().
3955 
3956   @param  Buffer  The pointer to a 32-bit value that may be unaligned.
3957   @param  Value   32-bit value to write to Buffer.
3958 
3959   @return The 32-bit value to write to Buffer.
3960 
3961 **/
3962 UINT32
3963 EFIAPI
3964 WriteUnaligned32 (
3965   OUT UINT32  *Buffer,
3966   IN  UINT32  Value
3967   );
3968 
3969 /**
3970   Reads a 64-bit value from memory that may be unaligned.
3971 
3972   This function returns the 64-bit value pointed to by Buffer. The function
3973   guarantees that the read operation does not produce an alignment fault.
3974 
3975   If the Buffer is NULL, then ASSERT().
3976 
3977   @param  Buffer  The pointer to a 64-bit value that may be unaligned.
3978 
3979   @return The 64-bit value read from Buffer.
3980 
3981 **/
3982 UINT64
3983 EFIAPI
3984 ReadUnaligned64 (
3985   IN CONST UINT64  *Buffer
3986   );
3987 
3988 /**
3989   Writes a 64-bit value to memory that may be unaligned.
3990 
3991   This function writes the 64-bit value specified by Value to Buffer. Value is
3992   returned. The function guarantees that the write operation does not produce
3993   an alignment fault.
3994 
3995   If the Buffer is NULL, then ASSERT().
3996 
3997   @param  Buffer  The pointer to a 64-bit value that may be unaligned.
3998   @param  Value   64-bit value to write to Buffer.
3999 
4000   @return The 64-bit value to write to Buffer.
4001 
4002 **/
4003 UINT64
4004 EFIAPI
4005 WriteUnaligned64 (
4006   OUT UINT64  *Buffer,
4007   IN  UINT64  Value
4008   );
4009 
4010 //
4011 // Bit Field Functions
4012 //
4013 
4014 /**
4015   Returns a bit field from an 8-bit value.
4016 
4017   Returns the bitfield specified by the StartBit and the EndBit from Operand.
4018 
4019   If 8-bit operations are not supported, then ASSERT().
4020   If StartBit is greater than 7, then ASSERT().
4021   If EndBit is greater than 7, then ASSERT().
4022   If EndBit is less than StartBit, then ASSERT().
4023 
4024   @param  Operand   Operand on which to perform the bitfield operation.
4025   @param  StartBit  The ordinal of the least significant bit in the bit field.
4026                     Range 0..7.
4027   @param  EndBit    The ordinal of the most significant bit in the bit field.
4028                     Range 0..7.
4029 
4030   @return The bit field read.
4031 
4032 **/
4033 UINT8
4034 EFIAPI
4035 BitFieldRead8 (
4036   IN      UINT8  Operand,
4037   IN      UINTN  StartBit,
4038   IN      UINTN  EndBit
4039   );
4040 
4041 /**
4042   Writes a bit field to an 8-bit value, and returns the result.
4043 
4044   Writes Value to the bit field specified by the StartBit and the EndBit in
4045   Operand. All other bits in Operand are preserved. The new 8-bit value is
4046   returned.
4047 
4048   If 8-bit operations are not supported, then ASSERT().
4049   If StartBit is greater than 7, then ASSERT().
4050   If EndBit is greater than 7, then ASSERT().
4051   If EndBit is less than StartBit, then ASSERT().
4052   If Value is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
4053 
4054   @param  Operand   Operand on which to perform the bitfield operation.
4055   @param  StartBit  The ordinal of the least significant bit in the bit field.
4056                     Range 0..7.
4057   @param  EndBit    The ordinal of the most significant bit in the bit field.
4058                     Range 0..7.
4059   @param  Value     New value of the bit field.
4060 
4061   @return The new 8-bit value.
4062 
4063 **/
4064 UINT8
4065 EFIAPI
4066 BitFieldWrite8 (
4067   IN      UINT8  Operand,
4068   IN      UINTN  StartBit,
4069   IN      UINTN  EndBit,
4070   IN      UINT8  Value
4071   );
4072 
4073 /**
4074   Reads a bit field from an 8-bit value, performs a bitwise OR, and returns the
4075   result.
4076 
4077   Performs a bitwise OR between the bit field specified by StartBit
4078   and EndBit in Operand and the value specified by OrData. All other bits in
4079   Operand are preserved. The new 8-bit value is returned.
4080 
4081   If 8-bit operations are not supported, then ASSERT().
4082   If StartBit is greater than 7, then ASSERT().
4083   If EndBit is greater than 7, then ASSERT().
4084   If EndBit is less than StartBit, then ASSERT().
4085   If OrData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
4086 
4087   @param  Operand   Operand on which to perform the bitfield operation.
4088   @param  StartBit  The ordinal of the least significant bit in the bit field.
4089                     Range 0..7.
4090   @param  EndBit    The ordinal of the most significant bit in the bit field.
4091                     Range 0..7.
4092   @param  OrData    The value to OR with the read value from the value
4093 
4094   @return The new 8-bit value.
4095 
4096 **/
4097 UINT8
4098 EFIAPI
4099 BitFieldOr8 (
4100   IN      UINT8  Operand,
4101   IN      UINTN  StartBit,
4102   IN      UINTN  EndBit,
4103   IN      UINT8  OrData
4104   );
4105 
4106 /**
4107   Reads a bit field from an 8-bit value, performs a bitwise AND, and returns
4108   the result.
4109 
4110   Performs a bitwise AND between the bit field specified by StartBit and EndBit
4111   in Operand and the value specified by AndData. All other bits in Operand are
4112   preserved. The new 8-bit value is returned.
4113 
4114   If 8-bit operations are not supported, then ASSERT().
4115   If StartBit is greater than 7, then ASSERT().
4116   If EndBit is greater than 7, then ASSERT().
4117   If EndBit is less than StartBit, then ASSERT().
4118   If AndData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
4119 
4120   @param  Operand   Operand on which to perform the bitfield operation.
4121   @param  StartBit  The ordinal of the least significant bit in the bit field.
4122                     Range 0..7.
4123   @param  EndBit    The ordinal of the most significant bit in the bit field.
4124                     Range 0..7.
4125   @param  AndData   The value to AND with the read value from the value.
4126 
4127   @return The new 8-bit value.
4128 
4129 **/
4130 UINT8
4131 EFIAPI
4132 BitFieldAnd8 (
4133   IN      UINT8  Operand,
4134   IN      UINTN  StartBit,
4135   IN      UINTN  EndBit,
4136   IN      UINT8  AndData
4137   );
4138 
4139 /**
4140   Reads a bit field from an 8-bit value, performs a bitwise AND followed by a
4141   bitwise OR, and returns the result.
4142 
4143   Performs a bitwise AND between the bit field specified by StartBit and EndBit
4144   in Operand and the value specified by AndData, followed by a bitwise
4145   OR with value specified by OrData. All other bits in Operand are
4146   preserved. The new 8-bit value is returned.
4147 
4148   If 8-bit operations are not supported, then ASSERT().
4149   If StartBit is greater than 7, then ASSERT().
4150   If EndBit is greater than 7, then ASSERT().
4151   If EndBit is less than StartBit, then ASSERT().
4152   If AndData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
4153   If OrData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
4154 
4155   @param  Operand   Operand on which to perform the bitfield operation.
4156   @param  StartBit  The ordinal of the least significant bit in the bit field.
4157                     Range 0..7.
4158   @param  EndBit    The ordinal of the most significant bit in the bit field.
4159                     Range 0..7.
4160   @param  AndData   The value to AND with the read value from the value.
4161   @param  OrData    The value to OR with the result of the AND operation.
4162 
4163   @return The new 8-bit value.
4164 
4165 **/
4166 UINT8
4167 EFIAPI
4168 BitFieldAndThenOr8 (
4169   IN      UINT8  Operand,
4170   IN      UINTN  StartBit,
4171   IN      UINTN  EndBit,
4172   IN      UINT8  AndData,
4173   IN      UINT8  OrData
4174   );
4175 
4176 /**
4177   Returns a bit field from a 16-bit value.
4178 
4179   Returns the bitfield specified by the StartBit and the EndBit from Operand.
4180 
4181   If 16-bit operations are not supported, then ASSERT().
4182   If StartBit is greater than 15, then ASSERT().
4183   If EndBit is greater than 15, then ASSERT().
4184   If EndBit is less than StartBit, then ASSERT().
4185 
4186   @param  Operand   Operand on which to perform the bitfield operation.
4187   @param  StartBit  The ordinal of the least significant bit in the bit field.
4188                     Range 0..15.
4189   @param  EndBit    The ordinal of the most significant bit in the bit field.
4190                     Range 0..15.
4191 
4192   @return The bit field read.
4193 
4194 **/
4195 UINT16
4196 EFIAPI
4197 BitFieldRead16 (
4198   IN      UINT16  Operand,
4199   IN      UINTN   StartBit,
4200   IN      UINTN   EndBit
4201   );
4202 
4203 /**
4204   Writes a bit field to a 16-bit value, and returns the result.
4205 
4206   Writes Value to the bit field specified by the StartBit and the EndBit in
4207   Operand. All other bits in Operand are preserved. The new 16-bit value is
4208   returned.
4209 
4210   If 16-bit operations are not supported, then ASSERT().
4211   If StartBit is greater than 15, then ASSERT().
4212   If EndBit is greater than 15, then ASSERT().
4213   If EndBit is less than StartBit, then ASSERT().
4214   If Value is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
4215 
4216   @param  Operand   Operand on which to perform the bitfield operation.
4217   @param  StartBit  The ordinal of the least significant bit in the bit field.
4218                     Range 0..15.
4219   @param  EndBit    The ordinal of the most significant bit in the bit field.
4220                     Range 0..15.
4221   @param  Value     New value of the bit field.
4222 
4223   @return The new 16-bit value.
4224 
4225 **/
4226 UINT16
4227 EFIAPI
4228 BitFieldWrite16 (
4229   IN      UINT16  Operand,
4230   IN      UINTN   StartBit,
4231   IN      UINTN   EndBit,
4232   IN      UINT16  Value
4233   );
4234 
4235 /**
4236   Reads a bit field from a 16-bit value, performs a bitwise OR, and returns the
4237   result.
4238 
4239   Performs a bitwise OR between the bit field specified by StartBit
4240   and EndBit in Operand and the value specified by OrData. All other bits in
4241   Operand are preserved. The new 16-bit value is returned.
4242 
4243   If 16-bit operations are not supported, then ASSERT().
4244   If StartBit is greater than 15, then ASSERT().
4245   If EndBit is greater than 15, then ASSERT().
4246   If EndBit is less than StartBit, then ASSERT().
4247   If OrData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
4248 
4249   @param  Operand   Operand on which to perform the bitfield operation.
4250   @param  StartBit  The ordinal of the least significant bit in the bit field.
4251                     Range 0..15.
4252   @param  EndBit    The ordinal of the most significant bit in the bit field.
4253                     Range 0..15.
4254   @param  OrData    The value to OR with the read value from the value
4255 
4256   @return The new 16-bit value.
4257 
4258 **/
4259 UINT16
4260 EFIAPI
4261 BitFieldOr16 (
4262   IN      UINT16  Operand,
4263   IN      UINTN   StartBit,
4264   IN      UINTN   EndBit,
4265   IN      UINT16  OrData
4266   );
4267 
4268 /**
4269   Reads a bit field from a 16-bit value, performs a bitwise AND, and returns
4270   the result.
4271 
4272   Performs a bitwise AND between the bit field specified by StartBit and EndBit
4273   in Operand and the value specified by AndData. All other bits in Operand are
4274   preserved. The new 16-bit value is returned.
4275 
4276   If 16-bit operations are not supported, then ASSERT().
4277   If StartBit is greater than 15, then ASSERT().
4278   If EndBit is greater than 15, then ASSERT().
4279   If EndBit is less than StartBit, then ASSERT().
4280   If AndData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
4281 
4282   @param  Operand   Operand on which to perform the bitfield operation.
4283   @param  StartBit  The ordinal of the least significant bit in the bit field.
4284                     Range 0..15.
4285   @param  EndBit    The ordinal of the most significant bit in the bit field.
4286                     Range 0..15.
4287   @param  AndData   The value to AND with the read value from the value
4288 
4289   @return The new 16-bit value.
4290 
4291 **/
4292 UINT16
4293 EFIAPI
4294 BitFieldAnd16 (
4295   IN      UINT16  Operand,
4296   IN      UINTN   StartBit,
4297   IN      UINTN   EndBit,
4298   IN      UINT16  AndData
4299   );
4300 
4301 /**
4302   Reads a bit field from a 16-bit value, performs a bitwise AND followed by a
4303   bitwise OR, and returns the result.
4304 
4305   Performs a bitwise AND between the bit field specified by StartBit and EndBit
4306   in Operand and the value specified by AndData, followed by a bitwise
4307   OR with value specified by OrData. All other bits in Operand are
4308   preserved. The new 16-bit value is returned.
4309 
4310   If 16-bit operations are not supported, then ASSERT().
4311   If StartBit is greater than 15, then ASSERT().
4312   If EndBit is greater than 15, then ASSERT().
4313   If EndBit is less than StartBit, then ASSERT().
4314   If AndData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
4315   If OrData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
4316 
4317   @param  Operand   Operand on which to perform the bitfield operation.
4318   @param  StartBit  The ordinal of the least significant bit in the bit field.
4319                     Range 0..15.
4320   @param  EndBit    The ordinal of the most significant bit in the bit field.
4321                     Range 0..15.
4322   @param  AndData   The value to AND with the read value from the value.
4323   @param  OrData    The value to OR with the result of the AND operation.
4324 
4325   @return The new 16-bit value.
4326 
4327 **/
4328 UINT16
4329 EFIAPI
4330 BitFieldAndThenOr16 (
4331   IN      UINT16  Operand,
4332   IN      UINTN   StartBit,
4333   IN      UINTN   EndBit,
4334   IN      UINT16  AndData,
4335   IN      UINT16  OrData
4336   );
4337 
4338 /**
4339   Returns a bit field from a 32-bit value.
4340 
4341   Returns the bitfield specified by the StartBit and the EndBit from Operand.
4342 
4343   If 32-bit operations are not supported, then ASSERT().
4344   If StartBit is greater than 31, then ASSERT().
4345   If EndBit is greater than 31, then ASSERT().
4346   If EndBit is less than StartBit, then ASSERT().
4347 
4348   @param  Operand   Operand on which to perform the bitfield operation.
4349   @param  StartBit  The ordinal of the least significant bit in the bit field.
4350                     Range 0..31.
4351   @param  EndBit    The ordinal of the most significant bit in the bit field.
4352                     Range 0..31.
4353 
4354   @return The bit field read.
4355 
4356 **/
4357 UINT32
4358 EFIAPI
4359 BitFieldRead32 (
4360   IN      UINT32  Operand,
4361   IN      UINTN   StartBit,
4362   IN      UINTN   EndBit
4363   );
4364 
4365 /**
4366   Writes a bit field to a 32-bit value, and returns the result.
4367 
4368   Writes Value to the bit field specified by the StartBit and the EndBit in
4369   Operand. All other bits in Operand are preserved. The new 32-bit value is
4370   returned.
4371 
4372   If 32-bit operations are not supported, then ASSERT().
4373   If StartBit is greater than 31, then ASSERT().
4374   If EndBit is greater than 31, then ASSERT().
4375   If EndBit is less than StartBit, then ASSERT().
4376   If Value is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
4377 
4378   @param  Operand   Operand on which to perform the bitfield operation.
4379   @param  StartBit  The ordinal of the least significant bit in the bit field.
4380                     Range 0..31.
4381   @param  EndBit    The ordinal of the most significant bit in the bit field.
4382                     Range 0..31.
4383   @param  Value     New value of the bit field.
4384 
4385   @return The new 32-bit value.
4386 
4387 **/
4388 UINT32
4389 EFIAPI
4390 BitFieldWrite32 (
4391   IN      UINT32  Operand,
4392   IN      UINTN   StartBit,
4393   IN      UINTN   EndBit,
4394   IN      UINT32  Value
4395   );
4396 
4397 /**
4398   Reads a bit field from a 32-bit value, performs a bitwise OR, and returns the
4399   result.
4400 
4401   Performs a bitwise OR between the bit field specified by StartBit
4402   and EndBit in Operand and the value specified by OrData. All other bits in
4403   Operand are preserved. The new 32-bit value is returned.
4404 
4405   If 32-bit operations are not supported, then ASSERT().
4406   If StartBit is greater than 31, then ASSERT().
4407   If EndBit is greater than 31, then ASSERT().
4408   If EndBit is less than StartBit, then ASSERT().
4409   If OrData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
4410 
4411   @param  Operand   Operand on which to perform the bitfield operation.
4412   @param  StartBit  The ordinal of the least significant bit in the bit field.
4413                     Range 0..31.
4414   @param  EndBit    The ordinal of the most significant bit in the bit field.
4415                     Range 0..31.
4416   @param  OrData    The value to OR with the read value from the value.
4417 
4418   @return The new 32-bit value.
4419 
4420 **/
4421 UINT32
4422 EFIAPI
4423 BitFieldOr32 (
4424   IN      UINT32  Operand,
4425   IN      UINTN   StartBit,
4426   IN      UINTN   EndBit,
4427   IN      UINT32  OrData
4428   );
4429 
4430 /**
4431   Reads a bit field from a 32-bit value, performs a bitwise AND, and returns
4432   the result.
4433 
4434   Performs a bitwise AND between the bit field specified by StartBit and EndBit
4435   in Operand and the value specified by AndData. All other bits in Operand are
4436   preserved. The new 32-bit value is returned.
4437 
4438   If 32-bit operations are not supported, then ASSERT().
4439   If StartBit is greater than 31, then ASSERT().
4440   If EndBit is greater than 31, then ASSERT().
4441   If EndBit is less than StartBit, then ASSERT().
4442   If AndData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
4443 
4444   @param  Operand   Operand on which to perform the bitfield operation.
4445   @param  StartBit  The ordinal of the least significant bit in the bit field.
4446                     Range 0..31.
4447   @param  EndBit    The ordinal of the most significant bit in the bit field.
4448                     Range 0..31.
4449   @param  AndData   The value to AND with the read value from the value
4450 
4451   @return The new 32-bit value.
4452 
4453 **/
4454 UINT32
4455 EFIAPI
4456 BitFieldAnd32 (
4457   IN      UINT32  Operand,
4458   IN      UINTN   StartBit,
4459   IN      UINTN   EndBit,
4460   IN      UINT32  AndData
4461   );
4462 
4463 /**
4464   Reads a bit field from a 32-bit value, performs a bitwise AND followed by a
4465   bitwise OR, and returns the result.
4466 
4467   Performs a bitwise AND between the bit field specified by StartBit and EndBit
4468   in Operand and the value specified by AndData, followed by a bitwise
4469   OR with value specified by OrData. All other bits in Operand are
4470   preserved. The new 32-bit value is returned.
4471 
4472   If 32-bit operations are not supported, then ASSERT().
4473   If StartBit is greater than 31, then ASSERT().
4474   If EndBit is greater than 31, then ASSERT().
4475   If EndBit is less than StartBit, then ASSERT().
4476   If AndData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
4477   If OrData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
4478 
4479   @param  Operand   Operand on which to perform the bitfield operation.
4480   @param  StartBit  The ordinal of the least significant bit in the bit field.
4481                     Range 0..31.
4482   @param  EndBit    The ordinal of the most significant bit in the bit field.
4483                     Range 0..31.
4484   @param  AndData   The value to AND with the read value from the value.
4485   @param  OrData    The value to OR with the result of the AND operation.
4486 
4487   @return The new 32-bit value.
4488 
4489 **/
4490 UINT32
4491 EFIAPI
4492 BitFieldAndThenOr32 (
4493   IN      UINT32  Operand,
4494   IN      UINTN   StartBit,
4495   IN      UINTN   EndBit,
4496   IN      UINT32  AndData,
4497   IN      UINT32  OrData
4498   );
4499 
4500 /**
4501   Returns a bit field from a 64-bit value.
4502 
4503   Returns the bitfield specified by the StartBit and the EndBit from Operand.
4504 
4505   If 64-bit operations are not supported, then ASSERT().
4506   If StartBit is greater than 63, then ASSERT().
4507   If EndBit is greater than 63, then ASSERT().
4508   If EndBit is less than StartBit, then ASSERT().
4509 
4510   @param  Operand   Operand on which to perform the bitfield operation.
4511   @param  StartBit  The ordinal of the least significant bit in the bit field.
4512                     Range 0..63.
4513   @param  EndBit    The ordinal of the most significant bit in the bit field.
4514                     Range 0..63.
4515 
4516   @return The bit field read.
4517 
4518 **/
4519 UINT64
4520 EFIAPI
4521 BitFieldRead64 (
4522   IN      UINT64  Operand,
4523   IN      UINTN   StartBit,
4524   IN      UINTN   EndBit
4525   );
4526 
4527 /**
4528   Writes a bit field to a 64-bit value, and returns the result.
4529 
4530   Writes Value to the bit field specified by the StartBit and the EndBit in
4531   Operand. All other bits in Operand are preserved. The new 64-bit value is
4532   returned.
4533 
4534   If 64-bit operations are not supported, then ASSERT().
4535   If StartBit is greater than 63, then ASSERT().
4536   If EndBit is greater than 63, then ASSERT().
4537   If EndBit is less than StartBit, then ASSERT().
4538   If Value is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
4539 
4540   @param  Operand   Operand on which to perform the bitfield operation.
4541   @param  StartBit  The ordinal of the least significant bit in the bit field.
4542                     Range 0..63.
4543   @param  EndBit    The ordinal of the most significant bit in the bit field.
4544                     Range 0..63.
4545   @param  Value     New value of the bit field.
4546 
4547   @return The new 64-bit value.
4548 
4549 **/
4550 UINT64
4551 EFIAPI
4552 BitFieldWrite64 (
4553   IN      UINT64  Operand,
4554   IN      UINTN   StartBit,
4555   IN      UINTN   EndBit,
4556   IN      UINT64  Value
4557   );
4558 
4559 /**
4560   Reads a bit field from a 64-bit value, performs a bitwise OR, and returns the
4561   result.
4562 
4563   Performs a bitwise OR between the bit field specified by StartBit
4564   and EndBit in Operand and the value specified by OrData. All other bits in
4565   Operand are preserved. The new 64-bit value is returned.
4566 
4567   If 64-bit operations are not supported, then ASSERT().
4568   If StartBit is greater than 63, then ASSERT().
4569   If EndBit is greater than 63, then ASSERT().
4570   If EndBit is less than StartBit, then ASSERT().
4571   If OrData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
4572 
4573   @param  Operand   Operand on which to perform the bitfield operation.
4574   @param  StartBit  The ordinal of the least significant bit in the bit field.
4575                     Range 0..63.
4576   @param  EndBit    The ordinal of the most significant bit in the bit field.
4577                     Range 0..63.
4578   @param  OrData    The value to OR with the read value from the value
4579 
4580   @return The new 64-bit value.
4581 
4582 **/
4583 UINT64
4584 EFIAPI
4585 BitFieldOr64 (
4586   IN      UINT64  Operand,
4587   IN      UINTN   StartBit,
4588   IN      UINTN   EndBit,
4589   IN      UINT64  OrData
4590   );
4591 
4592 /**
4593   Reads a bit field from a 64-bit value, performs a bitwise AND, and returns
4594   the result.
4595 
4596   Performs a bitwise AND between the bit field specified by StartBit and EndBit
4597   in Operand and the value specified by AndData. All other bits in Operand are
4598   preserved. The new 64-bit value is returned.
4599 
4600   If 64-bit operations are not supported, then ASSERT().
4601   If StartBit is greater than 63, then ASSERT().
4602   If EndBit is greater than 63, then ASSERT().
4603   If EndBit is less than StartBit, then ASSERT().
4604   If AndData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
4605 
4606   @param  Operand   Operand on which to perform the bitfield operation.
4607   @param  StartBit  The ordinal of the least significant bit in the bit field.
4608                     Range 0..63.
4609   @param  EndBit    The ordinal of the most significant bit in the bit field.
4610                     Range 0..63.
4611   @param  AndData   The value to AND with the read value from the value
4612 
4613   @return The new 64-bit value.
4614 
4615 **/
4616 UINT64
4617 EFIAPI
4618 BitFieldAnd64 (
4619   IN      UINT64  Operand,
4620   IN      UINTN   StartBit,
4621   IN      UINTN   EndBit,
4622   IN      UINT64  AndData
4623   );
4624 
4625 /**
4626   Reads a bit field from a 64-bit value, performs a bitwise AND followed by a
4627   bitwise OR, and returns the result.
4628 
4629   Performs a bitwise AND between the bit field specified by StartBit and EndBit
4630   in Operand and the value specified by AndData, followed by a bitwise
4631   OR with value specified by OrData. All other bits in Operand are
4632   preserved. The new 64-bit value is returned.
4633 
4634   If 64-bit operations are not supported, then ASSERT().
4635   If StartBit is greater than 63, then ASSERT().
4636   If EndBit is greater than 63, then ASSERT().
4637   If EndBit is less than StartBit, then ASSERT().
4638   If AndData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
4639   If OrData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
4640 
4641   @param  Operand   Operand on which to perform the bitfield operation.
4642   @param  StartBit  The ordinal of the least significant bit in the bit field.
4643                     Range 0..63.
4644   @param  EndBit    The ordinal of the most significant bit in the bit field.
4645                     Range 0..63.
4646   @param  AndData   The value to AND with the read value from the value.
4647   @param  OrData    The value to OR with the result of the AND operation.
4648 
4649   @return The new 64-bit value.
4650 
4651 **/
4652 UINT64
4653 EFIAPI
4654 BitFieldAndThenOr64 (
4655   IN      UINT64  Operand,
4656   IN      UINTN   StartBit,
4657   IN      UINTN   EndBit,
4658   IN      UINT64  AndData,
4659   IN      UINT64  OrData
4660   );
4661 
4662 /**
4663   Reads a bit field from a 32-bit value, counts and returns
4664   the number of set bits.
4665 
4666   Counts the number of set bits in the  bit field specified by
4667   StartBit and EndBit in Operand. The count is returned.
4668 
4669   If StartBit is greater than 31, then ASSERT().
4670   If EndBit is greater than 31, then ASSERT().
4671   If EndBit is less than StartBit, then ASSERT().
4672 
4673   @param  Operand   Operand on which to perform the bitfield operation.
4674   @param  StartBit  The ordinal of the least significant bit in the bit field.
4675                     Range 0..31.
4676   @param  EndBit    The ordinal of the most significant bit in the bit field.
4677                     Range 0..31.
4678 
4679   @return The number of bits set between StartBit and EndBit.
4680 
4681 **/
4682 UINT8
4683 EFIAPI
4684 BitFieldCountOnes32 (
4685   IN       UINT32  Operand,
4686   IN       UINTN   StartBit,
4687   IN       UINTN   EndBit
4688   );
4689 
4690 /**
4691    Reads a bit field from a 64-bit value, counts and returns
4692    the number of set bits.
4693 
4694    Counts the number of set bits in the  bit field specified by
4695    StartBit and EndBit in Operand. The count is returned.
4696 
4697    If StartBit is greater than 63, then ASSERT().
4698    If EndBit is greater than 63, then ASSERT().
4699    If EndBit is less than StartBit, then ASSERT().
4700 
4701    @param  Operand   Operand on which to perform the bitfield operation.
4702    @param  StartBit  The ordinal of the least significant bit in the bit field.
4703    Range 0..63.
4704    @param  EndBit    The ordinal of the most significant bit in the bit field.
4705    Range 0..63.
4706 
4707    @return The number of bits set between StartBit and EndBit.
4708 
4709 **/
4710 UINT8
4711 EFIAPI
4712 BitFieldCountOnes64 (
4713   IN       UINT64  Operand,
4714   IN       UINTN   StartBit,
4715   IN       UINTN   EndBit
4716   );
4717 
4718 //
4719 // Base Library Checksum Functions
4720 //
4721 
4722 /**
4723   Returns the sum of all elements in a buffer in unit of UINT8.
4724   During calculation, the carry bits are dropped.
4725 
4726   This function calculates the sum of all elements in a buffer
4727   in unit of UINT8. The carry bits in result of addition are dropped.
4728   The result is returned as UINT8. If Length is Zero, then Zero is
4729   returned.
4730 
4731   If Buffer is NULL, then ASSERT().
4732   If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
4733 
4734   @param  Buffer      The pointer to the buffer to carry out the sum operation.
4735   @param  Length      The size, in bytes, of Buffer.
4736 
4737   @return Sum         The sum of Buffer with carry bits dropped during additions.
4738 
4739 **/
4740 UINT8
4741 EFIAPI
4742 CalculateSum8 (
4743   IN      CONST UINT8  *Buffer,
4744   IN      UINTN        Length
4745   );
4746 
4747 /**
4748   Returns the two's complement checksum of all elements in a buffer
4749   of 8-bit values.
4750 
4751   This function first calculates the sum of the 8-bit values in the
4752   buffer specified by Buffer and Length.  The carry bits in the result
4753   of addition are dropped. Then, the two's complement of the sum is
4754   returned.  If Length is 0, then 0 is returned.
4755 
4756   If Buffer is NULL, then ASSERT().
4757   If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
4758 
4759   @param  Buffer      The pointer to the buffer to carry out the checksum operation.
4760   @param  Length      The size, in bytes, of Buffer.
4761 
4762   @return Checksum    The two's complement checksum of Buffer.
4763 
4764 **/
4765 UINT8
4766 EFIAPI
4767 CalculateCheckSum8 (
4768   IN      CONST UINT8  *Buffer,
4769   IN      UINTN        Length
4770   );
4771 
4772 /**
4773   Returns the sum of all elements in a buffer of 16-bit values.  During
4774   calculation, the carry bits are dropped.
4775 
4776   This function calculates the sum of the 16-bit values in the buffer
4777   specified by Buffer and Length. The carry bits in result of addition are dropped.
4778   The 16-bit result is returned.  If Length is 0, then 0 is returned.
4779 
4780   If Buffer is NULL, then ASSERT().
4781   If Buffer is not aligned on a 16-bit boundary, then ASSERT().
4782   If Length is not aligned on a 16-bit boundary, then ASSERT().
4783   If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
4784 
4785   @param  Buffer      The pointer to the buffer to carry out the sum operation.
4786   @param  Length      The size, in bytes, of Buffer.
4787 
4788   @return Sum         The sum of Buffer with carry bits dropped during additions.
4789 
4790 **/
4791 UINT16
4792 EFIAPI
4793 CalculateSum16 (
4794   IN      CONST UINT16  *Buffer,
4795   IN      UINTN         Length
4796   );
4797 
4798 /**
4799   Returns the two's complement checksum of all elements in a buffer of
4800   16-bit values.
4801 
4802   This function first calculates the sum of the 16-bit values in the buffer
4803   specified by Buffer and Length.  The carry bits in the result of addition
4804   are dropped. Then, the two's complement of the sum is returned.  If Length
4805   is 0, then 0 is returned.
4806 
4807   If Buffer is NULL, then ASSERT().
4808   If Buffer is not aligned on a 16-bit boundary, then ASSERT().
4809   If Length is not aligned on a 16-bit boundary, then ASSERT().
4810   If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
4811 
4812   @param  Buffer      The pointer to the buffer to carry out the checksum operation.
4813   @param  Length      The size, in bytes, of Buffer.
4814 
4815   @return Checksum    The two's complement checksum of Buffer.
4816 
4817 **/
4818 UINT16
4819 EFIAPI
4820 CalculateCheckSum16 (
4821   IN      CONST UINT16  *Buffer,
4822   IN      UINTN         Length
4823   );
4824 
4825 /**
4826   Returns the sum of all elements in a buffer of 32-bit values. During
4827   calculation, the carry bits are dropped.
4828 
4829   This function calculates the sum of the 32-bit values in the buffer
4830   specified by Buffer and Length. The carry bits in result of addition are dropped.
4831   The 32-bit result is returned. If Length is 0, then 0 is returned.
4832 
4833   If Buffer is NULL, then ASSERT().
4834   If Buffer is not aligned on a 32-bit boundary, then ASSERT().
4835   If Length is not aligned on a 32-bit boundary, then ASSERT().
4836   If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
4837 
4838   @param  Buffer      The pointer to the buffer to carry out the sum operation.
4839   @param  Length      The size, in bytes, of Buffer.
4840 
4841   @return Sum         The sum of Buffer with carry bits dropped during additions.
4842 
4843 **/
4844 UINT32
4845 EFIAPI
4846 CalculateSum32 (
4847   IN      CONST UINT32  *Buffer,
4848   IN      UINTN         Length
4849   );
4850 
4851 /**
4852   Returns the two's complement checksum of all elements in a buffer of
4853   32-bit values.
4854 
4855   This function first calculates the sum of the 32-bit values in the buffer
4856   specified by Buffer and Length.  The carry bits in the result of addition
4857   are dropped. Then, the two's complement of the sum is returned.  If Length
4858   is 0, then 0 is returned.
4859 
4860   If Buffer is NULL, then ASSERT().
4861   If Buffer is not aligned on a 32-bit boundary, then ASSERT().
4862   If Length is not aligned on a 32-bit boundary, then ASSERT().
4863   If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
4864 
4865   @param  Buffer      The pointer to the buffer to carry out the checksum operation.
4866   @param  Length      The size, in bytes, of Buffer.
4867 
4868   @return Checksum    The two's complement checksum of Buffer.
4869 
4870 **/
4871 UINT32
4872 EFIAPI
4873 CalculateCheckSum32 (
4874   IN      CONST UINT32  *Buffer,
4875   IN      UINTN         Length
4876   );
4877 
4878 /**
4879   Returns the sum of all elements in a buffer of 64-bit values.  During
4880   calculation, the carry bits are dropped.
4881 
4882   This function calculates the sum of the 64-bit values in the buffer
4883   specified by Buffer and Length. The carry bits in result of addition are dropped.
4884   The 64-bit result is returned.  If Length is 0, then 0 is returned.
4885 
4886   If Buffer is NULL, then ASSERT().
4887   If Buffer is not aligned on a 64-bit boundary, then ASSERT().
4888   If Length is not aligned on a 64-bit boundary, then ASSERT().
4889   If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
4890 
4891   @param  Buffer      The pointer to the buffer to carry out the sum operation.
4892   @param  Length      The size, in bytes, of Buffer.
4893 
4894   @return Sum         The sum of Buffer with carry bits dropped during additions.
4895 
4896 **/
4897 UINT64
4898 EFIAPI
4899 CalculateSum64 (
4900   IN      CONST UINT64  *Buffer,
4901   IN      UINTN         Length
4902   );
4903 
4904 /**
4905   Returns the two's complement checksum of all elements in a buffer of
4906   64-bit values.
4907 
4908   This function first calculates the sum of the 64-bit values in the buffer
4909   specified by Buffer and Length.  The carry bits in the result of addition
4910   are dropped. Then, the two's complement of the sum is returned.  If Length
4911   is 0, then 0 is returned.
4912 
4913   If Buffer is NULL, then ASSERT().
4914   If Buffer is not aligned on a 64-bit boundary, then ASSERT().
4915   If Length is not aligned on a 64-bit boundary, then ASSERT().
4916   If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
4917 
4918   @param  Buffer      The pointer to the buffer to carry out the checksum operation.
4919   @param  Length      The size, in bytes, of Buffer.
4920 
4921   @return Checksum    The two's complement checksum of Buffer.
4922 
4923 **/
4924 UINT64
4925 EFIAPI
4926 CalculateCheckSum64 (
4927   IN      CONST UINT64  *Buffer,
4928   IN      UINTN         Length
4929   );
4930 
4931 /**
4932   Computes and returns a 32-bit CRC for a data buffer.
4933   CRC32 value bases on ITU-T V.42.
4934 
4935   If Buffer is NULL, then ASSERT().
4936   If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
4937 
4938   @param[in]  Buffer       A pointer to the buffer on which the 32-bit CRC is to be computed.
4939   @param[in]  Length       The number of bytes in the buffer Data.
4940 
4941   @retval Crc32            The 32-bit CRC was computed for the data buffer.
4942 
4943 **/
4944 UINT32
4945 EFIAPI
4946 CalculateCrc32 (
4947   IN  VOID   *Buffer,
4948   IN  UINTN  Length
4949   );
4950 
4951 /**
4952    Calculates the CRC16-ANSI checksum of the given buffer.
4953 
4954    @param[in]      Buffer        Pointer to the buffer.
4955    @param[in]      Length        Length of the buffer, in bytes.
4956    @param[in]      InitialValue  Initial value of the CRC.
4957 
4958    @return The CRC16-ANSI checksum.
4959 **/
4960 UINT16
4961 EFIAPI
4962 CalculateCrc16Ansi (
4963   IN  CONST VOID  *Buffer,
4964   IN  UINTN       Length,
4965   IN  UINT16      InitialValue
4966   );
4967 
4968 //
4969 // Initial value for the CRC16-ANSI algorithm, when no prior checksum has been calculated.
4970 //
4971 #define CRC16ANSI_INIT  0xffff
4972 
4973 /**
4974    Calculates the CRC32c checksum of the given buffer.
4975 
4976    @param[in]      Buffer        Pointer to the buffer.
4977    @param[in]      Length        Length of the buffer, in bytes.
4978    @param[in]      InitialValue  Initial value of the CRC.
4979 
4980    @return The CRC32c checksum.
4981 **/
4982 UINT32
4983 EFIAPI
4984 CalculateCrc32c (
4985   IN CONST VOID  *Buffer,
4986   IN UINTN       Length,
4987   IN UINT32      InitialValue
4988   );
4989 
4990 /**
4991   Calculates the CRC16-CCITT-FALSE checksum of the given buffer.
4992 
4993   @param[in]      Buffer        Pointer to the buffer.
4994   @param[in]      Length        Length of the buffer, in bytes.
4995   @param[in]      InitialValue  Initial value of the CRC.
4996 
4997   @return The CRC16-CCITT-FALSE checksum.
4998 **/
4999 UINT16
5000 EFIAPI
5001 CalculateCrc16CcittF (
5002   IN CONST VOID  *Buffer,
5003   IN UINTN       Length,
5004   IN UINT16      InitialValue
5005   );
5006 
5007 //
5008 // Base Library CPU Functions
5009 //
5010 
5011 /**
5012   Function entry point used when a stack switch is requested with SwitchStack()
5013 
5014   @param  Context1        Context1 parameter passed into SwitchStack().
5015   @param  Context2        Context2 parameter passed into SwitchStack().
5016 **/
5017 typedef
5018 VOID
5019 (EFIAPI *SWITCH_STACK_ENTRY_POINT)(
5020   IN      VOID                      *Context1   OPTIONAL,
5021   IN      VOID                      *Context2   OPTIONAL
5022   );
5023 
5024 /**
5025   Used to serialize load and store operations.
5026 
5027   All loads and stores that proceed calls to this function are guaranteed to be
5028   globally visible when this function returns.
5029 
5030 **/
5031 VOID
5032 EFIAPI
5033 MemoryFence (
5034   VOID
5035   );
5036 
5037 /**
5038   Saves the current CPU context that can be restored with a call to LongJump()
5039   and returns 0.
5040 
5041   Saves the current CPU context in the buffer specified by JumpBuffer and
5042   returns 0. The initial call to SetJump() must always return 0. Subsequent
5043   calls to LongJump() cause a non-zero value to be returned by SetJump().
5044 
5045   If JumpBuffer is NULL, then ASSERT().
5046   For Itanium processors, if JumpBuffer is not aligned on a 16-byte boundary, then ASSERT().
5047 
5048   NOTE: The structure BASE_LIBRARY_JUMP_BUFFER is CPU architecture specific.
5049   The same structure must never be used for more than one CPU architecture context.
5050   For example, a BASE_LIBRARY_JUMP_BUFFER allocated by an IA-32 module must never be used from an x64 module.
5051   SetJump()/LongJump() is not currently supported for the EBC processor type.
5052 
5053   @param  JumpBuffer  A pointer to CPU context buffer.
5054 
5055   @retval 0 Indicates a return from SetJump().
5056 
5057 **/
5058 RETURNS_TWICE
5059 UINTN
5060 EFIAPI
5061 SetJump (
5062   OUT     BASE_LIBRARY_JUMP_BUFFER  *JumpBuffer
5063   );
5064 
5065 /**
5066   Restores the CPU context that was saved with SetJump().
5067 
5068   Restores the CPU context from the buffer specified by JumpBuffer. This
5069   function never returns to the caller. Instead is resumes execution based on
5070   the state of JumpBuffer.
5071 
5072   If JumpBuffer is NULL, then ASSERT().
5073   For Itanium processors, if JumpBuffer is not aligned on a 16-byte boundary, then ASSERT().
5074   If Value is 0, then ASSERT().
5075 
5076   @param  JumpBuffer  A pointer to CPU context buffer.
5077   @param  Value       The value to return when the SetJump() context is
5078                       restored and must be non-zero.
5079 
5080 **/
5081 VOID
5082 EFIAPI
5083 LongJump (
5084   IN      BASE_LIBRARY_JUMP_BUFFER  *JumpBuffer,
5085   IN      UINTN                     Value
5086   );
5087 
5088 /**
5089   Enables CPU interrupts.
5090 
5091 **/
5092 VOID
5093 EFIAPI
5094 EnableInterrupts (
5095   VOID
5096   );
5097 
5098 /**
5099   Disables CPU interrupts.
5100 
5101 **/
5102 VOID
5103 EFIAPI
5104 DisableInterrupts (
5105   VOID
5106   );
5107 
5108 /**
5109   Disables CPU interrupts and returns the interrupt state prior to the disable
5110   operation.
5111 
5112   @retval TRUE  CPU interrupts were enabled on entry to this call.
5113   @retval FALSE CPU interrupts were disabled on entry to this call.
5114 
5115 **/
5116 BOOLEAN
5117 EFIAPI
5118 SaveAndDisableInterrupts (
5119   VOID
5120   );
5121 
5122 /**
5123   Enables CPU interrupts for the smallest window required to capture any
5124   pending interrupts.
5125 
5126 **/
5127 VOID
5128 EFIAPI
5129 EnableDisableInterrupts (
5130   VOID
5131   );
5132 
5133 /**
5134   Retrieves the current CPU interrupt state.
5135 
5136   Returns TRUE if interrupts are currently enabled. Otherwise
5137   returns FALSE.
5138 
5139   @retval TRUE  CPU interrupts are enabled.
5140   @retval FALSE CPU interrupts are disabled.
5141 
5142 **/
5143 BOOLEAN
5144 EFIAPI
5145 GetInterruptState (
5146   VOID
5147   );
5148 
5149 /**
5150   Set the current CPU interrupt state.
5151 
5152   Sets the current CPU interrupt state to the state specified by
5153   InterruptState. If InterruptState is TRUE, then interrupts are enabled. If
5154   InterruptState is FALSE, then interrupts are disabled. InterruptState is
5155   returned.
5156 
5157   @param  InterruptState  TRUE if interrupts should enabled. FALSE if
5158                           interrupts should be disabled.
5159 
5160   @return InterruptState
5161 
5162 **/
5163 BOOLEAN
5164 EFIAPI
5165 SetInterruptState (
5166   IN      BOOLEAN  InterruptState
5167   );
5168 
5169 /**
5170   Requests CPU to pause for a short period of time.
5171 
5172   Requests CPU to pause for a short period of time. Typically used in MP
5173   systems to prevent memory starvation while waiting for a spin lock.
5174 
5175 **/
5176 VOID
5177 EFIAPI
5178 CpuPause (
5179   VOID
5180   );
5181 
5182 /**
5183   Transfers control to a function starting with a new stack.
5184 
5185   Transfers control to the function specified by EntryPoint using the
5186   new stack specified by NewStack and passing in the parameters specified
5187   by Context1 and Context2.  Context1 and Context2 are optional and may
5188   be NULL.  The function EntryPoint must never return.  This function
5189   supports a variable number of arguments following the NewStack parameter.
5190   These additional arguments are ignored on IA-32, x64, and EBC architectures.
5191   Itanium processors expect one additional parameter of type VOID * that specifies
5192   the new backing store pointer.
5193 
5194   If EntryPoint is NULL, then ASSERT().
5195   If NewStack is NULL, then ASSERT().
5196 
5197   @param  EntryPoint  A pointer to function to call with the new stack.
5198   @param  Context1    A pointer to the context to pass into the EntryPoint
5199                       function.
5200   @param  Context2    A pointer to the context to pass into the EntryPoint
5201                       function.
5202   @param  NewStack    A pointer to the new stack to use for the EntryPoint
5203                       function.
5204   @param  ...         This variable argument list is ignored for IA-32, x64, and
5205                       EBC architectures.  For Itanium processors, this variable
5206                       argument list is expected to contain a single parameter of
5207                       type VOID * that specifies the new backing store pointer.
5208 
5209 
5210 **/
5211 VOID
5212 EFIAPI
5213 SwitchStack (
5214   IN      SWITCH_STACK_ENTRY_POINT  EntryPoint,
5215   IN      VOID                      *Context1   OPTIONAL,
5216   IN      VOID                      *Context2   OPTIONAL,
5217   IN      VOID                      *NewStack,
5218   ...
5219   );
5220 
5221 /**
5222   Generates a breakpoint on the CPU.
5223 
5224   Generates a breakpoint on the CPU. The breakpoint must be implemented such
5225   that code can resume normal execution after the breakpoint.
5226 
5227 **/
5228 VOID
5229 EFIAPI
5230 CpuBreakpoint (
5231   VOID
5232   );
5233 
5234 /**
5235   Executes an infinite loop.
5236 
5237   Forces the CPU to execute an infinite loop. A debugger may be used to skip
5238   past the loop and the code that follows the loop must execute properly. This
5239   implies that the infinite loop must not cause the code that follow it to be
5240   optimized away.
5241 
5242 **/
5243 VOID
5244 EFIAPI
5245 CpuDeadLoop (
5246   VOID
5247   );
5248 
5249 /**
5250   Uses as a barrier to stop speculative execution.
5251 
5252   Ensures that no later instruction will execute speculatively, until all prior
5253   instructions have completed.
5254 
5255 **/
5256 VOID
5257 EFIAPI
5258 SpeculationBarrier (
5259   VOID
5260   );
5261 
5262 /**
5263   The TDCALL instruction causes a VM exit to the Intel TDX module.  It is
5264   used to call guest-side Intel TDX functions, either local or a TD exit
5265   to the host VMM, as selected by Leaf.
5266 
5267   @param[in]      Leaf        Leaf number of TDCALL instruction
5268   @param[in]      Arg1        Arg1
5269   @param[in]      Arg2        Arg2
5270   @param[in]      Arg3        Arg3
5271   @param[in,out]  Results  Returned result of the Leaf function
5272 
5273   @return 0               A successful call
5274   @return Other           See individual leaf functions
5275 **/
5276 UINTN
5277 EFIAPI
5278 TdCall (
5279   IN UINT64    Leaf,
5280   IN UINT64    Arg1,
5281   IN UINT64    Arg2,
5282   IN UINT64    Arg3,
5283   IN OUT VOID  *Results
5284   );
5285 
5286 /**
5287   TDVMALL is a leaf function 0 for TDCALL. It helps invoke services from the
5288   host VMM to pass/receive information.
5289 
5290   @param[in]     Leaf        Number of sub-functions
5291   @param[in]     Arg1        Arg1
5292   @param[in]     Arg2        Arg2
5293   @param[in]     Arg3        Arg3
5294   @param[in]     Arg4        Arg4
5295   @param[in,out] Results     Returned result of the sub-function
5296 
5297   @return 0               A successful call
5298   @return Other           See individual sub-functions
5299 
5300 **/
5301 UINTN
5302 EFIAPI
5303 TdVmCall (
5304   IN UINT64    Leaf,
5305   IN UINT64    Arg1,
5306   IN UINT64    Arg2,
5307   IN UINT64    Arg3,
5308   IN UINT64    Arg4,
5309   IN OUT VOID  *Results
5310   );
5311 
5312 /**
5313   Probe if TD is enabled.
5314 
5315   @return TRUE    TD is enabled.
5316   @return FALSE   TD is not enabled.
5317 **/
5318 BOOLEAN
5319 EFIAPI
5320 TdIsEnabled (
5321   VOID
5322   );
5323 
5324 #if defined (MDE_CPU_X64)
5325 //
5326 // The page size for the PVALIDATE instruction
5327 //
5328 typedef enum {
5329   PvalidatePageSize4K = 0,
5330   PvalidatePageSize2MB,
5331 } PVALIDATE_PAGE_SIZE;
5332 
5333 //
5334 // PVALIDATE Return Code.
5335 //
5336 #define PVALIDATE_RET_SUCCESS        0
5337 #define PVALIDATE_RET_FAIL_INPUT     1
5338 #define PVALIDATE_RET_SIZE_MISMATCH  6
5339 
5340 //
5341 // The PVALIDATE instruction did not make any changes to the RMP entry.
5342 //
5343 #define PVALIDATE_RET_NO_RMPUPDATE  255
5344 
5345 /**
5346  Execute a PVALIDATE instruction to validate or to rescinds validation of a guest
5347  page's RMP entry.
5348 
5349  The instruction is available only when CPUID Fn8000_001F_EAX[SNP]=1.
5350 
5351  The function is available on X64.
5352 
5353  @param[in]    PageSize         The page size to use.
5354  @param[in]    Validate         If TRUE, validate the guest virtual address
5355                                 otherwise invalidate the guest virtual address.
5356  @param[in]    Address          The guest virtual address.
5357 
5358  @retval PVALIDATE_RET_SUCCESS        The PVALIDATE instruction succeeded, and
5359                                       updated the RMP entry.
5360  @retval PVALIDATE_RET_NO_RMPUPDATE   The PVALIDATE instruction succeeded, but
5361                                       did not update the RMP entry.
5362  @return                              Failure code from the PVALIDATE
5363                                       instruction.
5364 **/
5365 UINT32
5366 EFIAPI
5367 AsmPvalidate (
5368   IN   PVALIDATE_PAGE_SIZE  PageSize,
5369   IN   BOOLEAN              Validate,
5370   IN   PHYSICAL_ADDRESS     Address
5371   );
5372 
5373 //
5374 // RDX settings for RMPADJUST
5375 //
5376 #define RMPADJUST_VMPL_MAX               3
5377 #define RMPADJUST_VMPL_MASK              0xFF
5378 #define RMPADJUST_VMPL_SHIFT             0
5379 #define RMPADJUST_PERMISSION_MASK_MASK   0xFF
5380 #define RMPADJUST_PERMISSION_MASK_SHIFT  8
5381 #define RMPADJUST_VMSA_PAGE_BIT          BIT16
5382 
5383 /**
5384   Adjusts the permissions of an SEV-SNP guest page.
5385 
5386   Executes a RMPADJUST instruction with the register state specified by Rax,
5387   Rcx, and Rdx. Returns Eax. This function is only available on X64.
5388 
5389   The instruction is available only when CPUID Fn8000_001F_EAX[SNP]=1.
5390 
5391   @param[in]  Rax   The value to load into RAX before executing the RMPADJUST
5392                     instruction.
5393   @param[in]  Rcx   The value to load into RCX before executing the RMPADJUST
5394                     instruction.
5395   @param[in]  Rdx   The value to load into RDX before executing the RMPADJUST
5396                     instruction.
5397 
5398   @return     Eax
5399 **/
5400 UINT32
5401 EFIAPI
5402 AsmRmpAdjust (
5403   IN      UINT64  Rax,
5404   IN      UINT64  Rcx,
5405   IN      UINT64  Rdx
5406   );
5407 
5408 #endif
5409 
5410 #if defined (MDE_CPU_IA32) || defined (MDE_CPU_X64)
5411 ///
5412 /// IA32 and x64 Specific Functions.
5413 /// Byte packed structure for 16-bit Real Mode EFLAGS.
5414 ///
5415 typedef union {
5416   struct {
5417     UINT32    CF         : 1; ///< Carry Flag.
5418     UINT32    Reserved_0 : 1; ///< Reserved.
5419     UINT32    PF         : 1; ///< Parity Flag.
5420     UINT32    Reserved_1 : 1; ///< Reserved.
5421     UINT32    AF         : 1; ///< Auxiliary Carry Flag.
5422     UINT32    Reserved_2 : 1; ///< Reserved.
5423     UINT32    ZF         : 1; ///< Zero Flag.
5424     UINT32    SF         : 1; ///< Sign Flag.
5425     UINT32    TF         : 1; ///< Trap Flag.
5426     UINT32    IF         : 1; ///< Interrupt Enable Flag.
5427     UINT32    DF         : 1; ///< Direction Flag.
5428     UINT32    OF         : 1; ///< Overflow Flag.
5429     UINT32    IOPL       : 2; ///< I/O Privilege Level.
5430     UINT32    NT         : 1; ///< Nested Task.
5431     UINT32    Reserved_3 : 1; ///< Reserved.
5432   } Bits;
5433   UINT16    Uint16;
5434 } IA32_FLAGS16;
5435 
5436 ///
5437 /// Byte packed structure for EFLAGS/RFLAGS.
5438 /// 32-bits on IA-32.
5439 /// 64-bits on x64.  The upper 32-bits on x64 are reserved.
5440 ///
5441 typedef union {
5442   struct {
5443     UINT32    CF         : 1;  ///< Carry Flag.
5444     UINT32    Reserved_0 : 1;  ///< Reserved.
5445     UINT32    PF         : 1;  ///< Parity Flag.
5446     UINT32    Reserved_1 : 1;  ///< Reserved.
5447     UINT32    AF         : 1;  ///< Auxiliary Carry Flag.
5448     UINT32    Reserved_2 : 1;  ///< Reserved.
5449     UINT32    ZF         : 1;  ///< Zero Flag.
5450     UINT32    SF         : 1;  ///< Sign Flag.
5451     UINT32    TF         : 1;  ///< Trap Flag.
5452     UINT32    IF         : 1;  ///< Interrupt Enable Flag.
5453     UINT32    DF         : 1;  ///< Direction Flag.
5454     UINT32    OF         : 1;  ///< Overflow Flag.
5455     UINT32    IOPL       : 2;  ///< I/O Privilege Level.
5456     UINT32    NT         : 1;  ///< Nested Task.
5457     UINT32    Reserved_3 : 1;  ///< Reserved.
5458     UINT32    RF         : 1;  ///< Resume Flag.
5459     UINT32    VM         : 1;  ///< Virtual 8086 Mode.
5460     UINT32    AC         : 1;  ///< Alignment Check.
5461     UINT32    VIF        : 1;  ///< Virtual Interrupt Flag.
5462     UINT32    VIP        : 1;  ///< Virtual Interrupt Pending.
5463     UINT32    ID         : 1;  ///< ID Flag.
5464     UINT32    Reserved_4 : 10; ///< Reserved.
5465   } Bits;
5466   UINTN    UintN;
5467 } IA32_EFLAGS32;
5468 
5469 ///
5470 /// Byte packed structure for Control Register 0 (CR0).
5471 /// 32-bits on IA-32.
5472 /// 64-bits on x64.  The upper 32-bits on x64 are reserved.
5473 ///
5474 typedef union {
5475   struct {
5476     UINT32    PE         : 1;  ///< Protection Enable.
5477     UINT32    MP         : 1;  ///< Monitor Coprocessor.
5478     UINT32    EM         : 1;  ///< Emulation.
5479     UINT32    TS         : 1;  ///< Task Switched.
5480     UINT32    ET         : 1;  ///< Extension Type.
5481     UINT32    NE         : 1;  ///< Numeric Error.
5482     UINT32    Reserved_0 : 10; ///< Reserved.
5483     UINT32    WP         : 1;  ///< Write Protect.
5484     UINT32    Reserved_1 : 1;  ///< Reserved.
5485     UINT32    AM         : 1;  ///< Alignment Mask.
5486     UINT32    Reserved_2 : 10; ///< Reserved.
5487     UINT32    NW         : 1;  ///< Mot Write-through.
5488     UINT32    CD         : 1;  ///< Cache Disable.
5489     UINT32    PG         : 1;  ///< Paging.
5490   } Bits;
5491   UINTN    UintN;
5492 } IA32_CR0;
5493 
5494 ///
5495 /// Byte packed structure for Control Register 4 (CR4).
5496 /// 32-bits on IA-32.
5497 /// 64-bits on x64.  The upper 32-bits on x64 are reserved.
5498 ///
5499 typedef union {
5500   struct {
5501     UINT32    VME        : 1; ///< Virtual-8086 Mode Extensions.
5502     UINT32    PVI        : 1; ///< Protected-Mode Virtual Interrupts.
5503     UINT32    TSD        : 1; ///< Time Stamp Disable.
5504     UINT32    DE         : 1; ///< Debugging Extensions.
5505     UINT32    PSE        : 1; ///< Page Size Extensions.
5506     UINT32    PAE        : 1; ///< Physical Address Extension.
5507     UINT32    MCE        : 1; ///< Machine Check Enable.
5508     UINT32    PGE        : 1; ///< Page Global Enable.
5509     UINT32    PCE        : 1; ///< Performance Monitoring Counter
5510                               ///< Enable.
5511     UINT32    OSFXSR     : 1; ///< Operating System Support for
5512                               ///< FXSAVE and FXRSTOR instructions
5513     UINT32    OSXMMEXCPT : 1; ///< Operating System Support for
5514                               ///< Unmasked SIMD Floating Point
5515                               ///< Exceptions.
5516     UINT32    UMIP       : 1; ///< User-Mode Instruction Prevention.
5517     UINT32    LA57       : 1; ///< Linear Address 57bit.
5518     UINT32    VMXE       : 1; ///< VMX Enable.
5519     UINT32    SMXE       : 1; ///< SMX Enable.
5520     UINT32    Reserved_3 : 1; ///< Reserved.
5521     UINT32    FSGSBASE   : 1; ///< FSGSBASE Enable.
5522     UINT32    PCIDE      : 1; ///< PCID Enable.
5523     UINT32    OSXSAVE    : 1; ///< XSAVE and Processor Extended States Enable.
5524     UINT32    Reserved_4 : 1; ///< Reserved.
5525     UINT32    SMEP       : 1; ///< SMEP Enable.
5526     UINT32    SMAP       : 1; ///< SMAP Enable.
5527     UINT32    PKE        : 1; ///< Protection-Key Enable.
5528     UINT32    Reserved_5 : 9; ///< Reserved.
5529   } Bits;
5530   UINTN    UintN;
5531 } IA32_CR4;
5532 
5533 ///
5534 /// Byte packed structure for a segment descriptor in a GDT/LDT.
5535 ///
5536 typedef union {
5537   struct {
5538     UINT32    LimitLow  : 16;
5539     UINT32    BaseLow   : 16;
5540     UINT32    BaseMid   : 8;
5541     UINT32    Type      : 4;
5542     UINT32    S         : 1;
5543     UINT32    DPL       : 2;
5544     UINT32    P         : 1;
5545     UINT32    LimitHigh : 4;
5546     UINT32    AVL       : 1;
5547     UINT32    L         : 1;
5548     UINT32    DB        : 1;
5549     UINT32    G         : 1;
5550     UINT32    BaseHigh  : 8;
5551   } Bits;
5552   UINT64    Uint64;
5553 } IA32_SEGMENT_DESCRIPTOR;
5554 
5555 ///
5556 /// Byte packed structure for an IDTR, GDTR, LDTR descriptor.
5557 ///
5558   #pragma pack (1)
5559 typedef struct {
5560   UINT16    Limit;
5561   UINTN     Base;
5562 } IA32_DESCRIPTOR;
5563   #pragma pack ()
5564 
5565 #define IA32_IDT_GATE_TYPE_TASK          0x85
5566 #define IA32_IDT_GATE_TYPE_INTERRUPT_16  0x86
5567 #define IA32_IDT_GATE_TYPE_TRAP_16       0x87
5568 #define IA32_IDT_GATE_TYPE_INTERRUPT_32  0x8E
5569 #define IA32_IDT_GATE_TYPE_TRAP_32       0x8F
5570 
5571 #define IA32_GDT_TYPE_TSS   0x9
5572 #define IA32_GDT_ALIGNMENT  8
5573 
5574   #if defined (MDE_CPU_IA32)
5575 ///
5576 /// Byte packed structure for an IA-32 Interrupt Gate Descriptor.
5577 ///
5578 typedef union {
5579   struct {
5580     UINT32    OffsetLow  : 16; ///< Offset bits 15..0.
5581     UINT32    Selector   : 16; ///< Selector.
5582     UINT32    Reserved_0 : 8;  ///< Reserved.
5583     UINT32    GateType   : 8;  ///< Gate Type.  See #defines above.
5584     UINT32    OffsetHigh : 16; ///< Offset bits 31..16.
5585   } Bits;
5586   UINT64    Uint64;
5587 } IA32_IDT_GATE_DESCRIPTOR;
5588 
5589     #pragma pack (1)
5590 //
5591 // IA32 Task-State Segment Definition
5592 //
5593 typedef struct {
5594   UINT16    PreviousTaskLink;
5595   UINT16    Reserved_2;
5596   UINT32    ESP0;
5597   UINT16    SS0;
5598   UINT16    Reserved_10;
5599   UINT32    ESP1;
5600   UINT16    SS1;
5601   UINT16    Reserved_18;
5602   UINT32    ESP2;
5603   UINT16    SS2;
5604   UINT16    Reserved_26;
5605   UINT32    CR3;
5606   UINT32    EIP;
5607   UINT32    EFLAGS;
5608   UINT32    EAX;
5609   UINT32    ECX;
5610   UINT32    EDX;
5611   UINT32    EBX;
5612   UINT32    ESP;
5613   UINT32    EBP;
5614   UINT32    ESI;
5615   UINT32    EDI;
5616   UINT16    ES;
5617   UINT16    Reserved_74;
5618   UINT16    CS;
5619   UINT16    Reserved_78;
5620   UINT16    SS;
5621   UINT16    Reserved_82;
5622   UINT16    DS;
5623   UINT16    Reserved_86;
5624   UINT16    FS;
5625   UINT16    Reserved_90;
5626   UINT16    GS;
5627   UINT16    Reserved_94;
5628   UINT16    LDTSegmentSelector;
5629   UINT16    Reserved_98;
5630   UINT16    T;
5631   UINT16    IOMapBaseAddress;
5632 } IA32_TASK_STATE_SEGMENT;
5633 
5634 typedef union {
5635   struct {
5636     UINT32    LimitLow    : 16; ///< Segment Limit 15..00
5637     UINT32    BaseLow     : 16; ///< Base Address  15..00
5638     UINT32    BaseMid     : 8;  ///< Base Address  23..16
5639     UINT32    Type        : 4;  ///< Type (1 0 B 1)
5640     UINT32    Reserved_43 : 1;  ///< 0
5641     UINT32    DPL         : 2;  ///< Descriptor Privilege Level
5642     UINT32    P           : 1;  ///< Segment Present
5643     UINT32    LimitHigh   : 4;  ///< Segment Limit 19..16
5644     UINT32    AVL         : 1;  ///< Available for use by system software
5645     UINT32    Reserved_52 : 2;  ///< 0 0
5646     UINT32    G           : 1;  ///< Granularity
5647     UINT32    BaseHigh    : 8;  ///< Base Address 31..24
5648   } Bits;
5649   UINT64    Uint64;
5650 } IA32_TSS_DESCRIPTOR;
5651     #pragma pack ()
5652 
5653   #endif // defined (MDE_CPU_IA32)
5654 
5655   #if defined (MDE_CPU_X64)
5656 ///
5657 /// Byte packed structure for an x64 Interrupt Gate Descriptor.
5658 ///
5659 typedef union {
5660   struct {
5661     UINT32    OffsetLow   : 16; ///< Offset bits 15..0.
5662     UINT32    Selector    : 16; ///< Selector.
5663     UINT32    Reserved_0  : 8;  ///< Reserved.
5664     UINT32    GateType    : 8;  ///< Gate Type.  See #defines above.
5665     UINT32    OffsetHigh  : 16; ///< Offset bits 31..16.
5666     UINT32    OffsetUpper : 32; ///< Offset bits 63..32.
5667     UINT32    Reserved_1  : 32; ///< Reserved.
5668   } Bits;
5669   struct {
5670     UINT64    Uint64;
5671     UINT64    Uint64_1;
5672   } Uint128;
5673 } IA32_IDT_GATE_DESCRIPTOR;
5674 
5675     #pragma pack (1)
5676 //
5677 // IA32 Task-State Segment Definition
5678 //
5679 typedef struct {
5680   UINT32    Reserved_0;
5681   UINT64    RSP0;
5682   UINT64    RSP1;
5683   UINT64    RSP2;
5684   UINT64    Reserved_28;
5685   UINT64    IST[7];
5686   UINT64    Reserved_92;
5687   UINT16    Reserved_100;
5688   UINT16    IOMapBaseAddress;
5689 } IA32_TASK_STATE_SEGMENT;
5690 
5691 typedef union {
5692   struct {
5693     UINT32    LimitLow    : 16; ///< Segment Limit 15..00
5694     UINT32    BaseLow     : 16; ///< Base Address  15..00
5695     UINT32    BaseMidl    : 8;  ///< Base Address  23..16
5696     UINT32    Type        : 4;  ///< Type (1 0 B 1)
5697     UINT32    Reserved_43 : 1;  ///< 0
5698     UINT32    DPL         : 2;  ///< Descriptor Privilege Level
5699     UINT32    P           : 1;  ///< Segment Present
5700     UINT32    LimitHigh   : 4;  ///< Segment Limit 19..16
5701     UINT32    AVL         : 1;  ///< Available for use by system software
5702     UINT32    Reserved_52 : 2;  ///< 0 0
5703     UINT32    G           : 1;  ///< Granularity
5704     UINT32    BaseMidh    : 8;  ///< Base Address  31..24
5705     UINT32    BaseHigh    : 32; ///< Base Address  63..32
5706     UINT32    Reserved_96 : 32; ///< Reserved
5707   } Bits;
5708   struct {
5709     UINT64    Uint64;
5710     UINT64    Uint64_1;
5711   } Uint128;
5712 } IA32_TSS_DESCRIPTOR;
5713     #pragma pack ()
5714 
5715   #endif // defined (MDE_CPU_X64)
5716 
5717 ///
5718 /// Byte packed structure for an FP/SSE/SSE2 context.
5719 ///
5720 typedef struct {
5721   UINT8    Buffer[512];
5722 } IA32_FX_BUFFER;
5723 
5724 ///
5725 /// Structures for the 16-bit real mode thunks.
5726 ///
5727 typedef struct {
5728   UINT32    Reserved1;
5729   UINT32    Reserved2;
5730   UINT32    Reserved3;
5731   UINT32    Reserved4;
5732   UINT8     BL;
5733   UINT8     BH;
5734   UINT16    Reserved5;
5735   UINT8     DL;
5736   UINT8     DH;
5737   UINT16    Reserved6;
5738   UINT8     CL;
5739   UINT8     CH;
5740   UINT16    Reserved7;
5741   UINT8     AL;
5742   UINT8     AH;
5743   UINT16    Reserved8;
5744 } IA32_BYTE_REGS;
5745 
5746 typedef struct {
5747   UINT16    DI;
5748   UINT16    Reserved1;
5749   UINT16    SI;
5750   UINT16    Reserved2;
5751   UINT16    BP;
5752   UINT16    Reserved3;
5753   UINT16    SP;
5754   UINT16    Reserved4;
5755   UINT16    BX;
5756   UINT16    Reserved5;
5757   UINT16    DX;
5758   UINT16    Reserved6;
5759   UINT16    CX;
5760   UINT16    Reserved7;
5761   UINT16    AX;
5762   UINT16    Reserved8;
5763 } IA32_WORD_REGS;
5764 
5765 typedef struct {
5766   UINT32           EDI;
5767   UINT32           ESI;
5768   UINT32           EBP;
5769   UINT32           ESP;
5770   UINT32           EBX;
5771   UINT32           EDX;
5772   UINT32           ECX;
5773   UINT32           EAX;
5774   UINT16           DS;
5775   UINT16           ES;
5776   UINT16           FS;
5777   UINT16           GS;
5778   IA32_EFLAGS32    EFLAGS;
5779   UINT32           Eip;
5780   UINT16           CS;
5781   UINT16           SS;
5782 } IA32_DWORD_REGS;
5783 
5784 typedef union {
5785   IA32_DWORD_REGS    E;
5786   IA32_WORD_REGS     X;
5787   IA32_BYTE_REGS     H;
5788 } IA32_REGISTER_SET;
5789 
5790 ///
5791 /// Byte packed structure for an 16-bit real mode thunks.
5792 ///
5793 typedef struct {
5794   IA32_REGISTER_SET    *RealModeState;
5795   VOID                 *RealModeBuffer;
5796   UINT32               RealModeBufferSize;
5797   UINT32               ThunkAttributes;
5798 } THUNK_CONTEXT;
5799 
5800 #define THUNK_ATTRIBUTE_BIG_REAL_MODE              0x00000001
5801 #define THUNK_ATTRIBUTE_DISABLE_A20_MASK_INT_15    0x00000002
5802 #define THUNK_ATTRIBUTE_DISABLE_A20_MASK_KBD_CTRL  0x00000004
5803 
5804 ///
5805 /// Type definition for representing labels in NASM source code that allow for
5806 /// the patching of immediate operands of IA32 and X64 instructions.
5807 ///
5808 /// While the type is technically defined as a function type (note: not a
5809 /// pointer-to-function type), such labels in NASM source code never stand for
5810 /// actual functions, and identifiers declared with this function type should
5811 /// never be called. This is also why the EFIAPI calling convention specifier
5812 /// is missing from the typedef, and why the typedef does not follow the usual
5813 /// edk2 coding style for function (or pointer-to-function) typedefs. The VOID
5814 /// return type and the VOID argument list are merely artifacts.
5815 ///
5816 typedef VOID (X86_ASSEMBLY_PATCH_LABEL) (
5817   VOID
5818   );
5819 
5820 /**
5821   Retrieves CPUID information.
5822 
5823   Executes the CPUID instruction with EAX set to the value specified by Index.
5824   This function always returns Index.
5825   If Eax is not NULL, then the value of EAX after CPUID is returned in Eax.
5826   If Ebx is not NULL, then the value of EBX after CPUID is returned in Ebx.
5827   If Ecx is not NULL, then the value of ECX after CPUID is returned in Ecx.
5828   If Edx is not NULL, then the value of EDX after CPUID is returned in Edx.
5829   This function is only available on IA-32 and x64.
5830 
5831   @param  Index The 32-bit value to load into EAX prior to invoking the CPUID
5832                 instruction.
5833   @param  Eax   The pointer to the 32-bit EAX value returned by the CPUID
5834                 instruction. This is an optional parameter that may be NULL.
5835   @param  Ebx   The pointer to the 32-bit EBX value returned by the CPUID
5836                 instruction. This is an optional parameter that may be NULL.
5837   @param  Ecx   The pointer to the 32-bit ECX value returned by the CPUID
5838                 instruction. This is an optional parameter that may be NULL.
5839   @param  Edx   The pointer to the 32-bit EDX value returned by the CPUID
5840                 instruction. This is an optional parameter that may be NULL.
5841 
5842   @return Index.
5843 
5844 **/
5845 UINT32
5846 EFIAPI
5847 AsmCpuid (
5848   IN      UINT32  Index,
5849   OUT     UINT32  *Eax   OPTIONAL,
5850   OUT     UINT32  *Ebx   OPTIONAL,
5851   OUT     UINT32  *Ecx   OPTIONAL,
5852   OUT     UINT32  *Edx   OPTIONAL
5853   );
5854 
5855 /**
5856   Retrieves CPUID information using an extended leaf identifier.
5857 
5858   Executes the CPUID instruction with EAX set to the value specified by Index
5859   and ECX set to the value specified by SubIndex. This function always returns
5860   Index. This function is only available on IA-32 and x64.
5861 
5862   If Eax is not NULL, then the value of EAX after CPUID is returned in Eax.
5863   If Ebx is not NULL, then the value of EBX after CPUID is returned in Ebx.
5864   If Ecx is not NULL, then the value of ECX after CPUID is returned in Ecx.
5865   If Edx is not NULL, then the value of EDX after CPUID is returned in Edx.
5866 
5867   @param  Index     The 32-bit value to load into EAX prior to invoking the
5868                     CPUID instruction.
5869   @param  SubIndex  The 32-bit value to load into ECX prior to invoking the
5870                     CPUID instruction.
5871   @param  Eax       The pointer to the 32-bit EAX value returned by the CPUID
5872                     instruction. This is an optional parameter that may be
5873                     NULL.
5874   @param  Ebx       The pointer to the 32-bit EBX value returned by the CPUID
5875                     instruction. This is an optional parameter that may be
5876                     NULL.
5877   @param  Ecx       The pointer to the 32-bit ECX value returned by the CPUID
5878                     instruction. This is an optional parameter that may be
5879                     NULL.
5880   @param  Edx       The pointer to the 32-bit EDX value returned by the CPUID
5881                     instruction. This is an optional parameter that may be
5882                     NULL.
5883 
5884   @return Index.
5885 
5886 **/
5887 UINT32
5888 EFIAPI
5889 AsmCpuidEx (
5890   IN      UINT32  Index,
5891   IN      UINT32  SubIndex,
5892   OUT     UINT32  *Eax   OPTIONAL,
5893   OUT     UINT32  *Ebx   OPTIONAL,
5894   OUT     UINT32  *Ecx   OPTIONAL,
5895   OUT     UINT32  *Edx   OPTIONAL
5896   );
5897 
5898 /**
5899   Set CD bit and clear NW bit of CR0 followed by a WBINVD.
5900 
5901   Disables the caches by setting the CD bit of CR0 to 1, clearing the NW bit of CR0 to 0,
5902   and executing a WBINVD instruction.  This function is only available on IA-32 and x64.
5903 
5904 **/
5905 VOID
5906 EFIAPI
5907 AsmDisableCache (
5908   VOID
5909   );
5910 
5911 /**
5912   Perform a WBINVD and clear both the CD and NW bits of CR0.
5913 
5914   Enables the caches by executing a WBINVD instruction and then clear both the CD and NW
5915   bits of CR0 to 0.  This function is only available on IA-32 and x64.
5916 
5917 **/
5918 VOID
5919 EFIAPI
5920 AsmEnableCache (
5921   VOID
5922   );
5923 
5924 /**
5925   Returns the lower 32-bits of a Machine Specific Register(MSR).
5926 
5927   Reads and returns the lower 32-bits of the MSR specified by Index.
5928   No parameter checking is performed on Index, and some Index values may cause
5929   CPU exceptions. The caller must either guarantee that Index is valid, or the
5930   caller must set up exception handlers to catch the exceptions. This function
5931   is only available on IA-32 and x64.
5932 
5933   @param  Index The 32-bit MSR index to read.
5934 
5935   @return The lower 32 bits of the MSR identified by Index.
5936 
5937 **/
5938 UINT32
5939 EFIAPI
5940 AsmReadMsr32 (
5941   IN      UINT32  Index
5942   );
5943 
5944 /**
5945   Writes a 32-bit value to a Machine Specific Register(MSR), and returns the value.
5946   The upper 32-bits of the MSR are set to zero.
5947 
5948   Writes the 32-bit value specified by Value to the MSR specified by Index. The
5949   upper 32-bits of the MSR write are set to zero. The 32-bit value written to
5950   the MSR is returned. No parameter checking is performed on Index or Value,
5951   and some of these may cause CPU exceptions. The caller must either guarantee
5952   that Index and Value are valid, or the caller must establish proper exception
5953   handlers. This function is only available on IA-32 and x64.
5954 
5955   @param  Index The 32-bit MSR index to write.
5956   @param  Value The 32-bit value to write to the MSR.
5957 
5958   @return Value
5959 
5960 **/
5961 UINT32
5962 EFIAPI
5963 AsmWriteMsr32 (
5964   IN      UINT32  Index,
5965   IN      UINT32  Value
5966   );
5967 
5968 /**
5969   Reads a 64-bit MSR, performs a bitwise OR on the lower 32-bits, and
5970   writes the result back to the 64-bit MSR.
5971 
5972   Reads the 64-bit MSR specified by Index, performs a bitwise OR
5973   between the lower 32-bits of the read result and the value specified by
5974   OrData, and writes the result to the 64-bit MSR specified by Index. The lower
5975   32-bits of the value written to the MSR is returned. No parameter checking is
5976   performed on Index or OrData, and some of these may cause CPU exceptions. The
5977   caller must either guarantee that Index and OrData are valid, or the caller
5978   must establish proper exception handlers. This function is only available on
5979   IA-32 and x64.
5980 
5981   @param  Index   The 32-bit MSR index to write.
5982   @param  OrData  The value to OR with the read value from the MSR.
5983 
5984   @return The lower 32-bit value written to the MSR.
5985 
5986 **/
5987 UINT32
5988 EFIAPI
5989 AsmMsrOr32 (
5990   IN      UINT32  Index,
5991   IN      UINT32  OrData
5992   );
5993 
5994 /**
5995   Reads a 64-bit MSR, performs a bitwise AND on the lower 32-bits, and writes
5996   the result back to the 64-bit MSR.
5997 
5998   Reads the 64-bit MSR specified by Index, performs a bitwise AND between the
5999   lower 32-bits of the read result and the value specified by AndData, and
6000   writes the result to the 64-bit MSR specified by Index. The lower 32-bits of
6001   the value written to the MSR is returned. No parameter checking is performed
6002   on Index or AndData, and some of these may cause CPU exceptions. The caller
6003   must either guarantee that Index and AndData are valid, or the caller must
6004   establish proper exception handlers. This function is only available on IA-32
6005   and x64.
6006 
6007   @param  Index   The 32-bit MSR index to write.
6008   @param  AndData The value to AND with the read value from the MSR.
6009 
6010   @return The lower 32-bit value written to the MSR.
6011 
6012 **/
6013 UINT32
6014 EFIAPI
6015 AsmMsrAnd32 (
6016   IN      UINT32  Index,
6017   IN      UINT32  AndData
6018   );
6019 
6020 /**
6021   Reads a 64-bit MSR, performs a bitwise AND followed by a bitwise OR
6022   on the lower 32-bits, and writes the result back to the 64-bit MSR.
6023 
6024   Reads the 64-bit MSR specified by Index, performs a bitwise AND between the
6025   lower 32-bits of the read result and the value specified by AndData
6026   preserving the upper 32-bits, performs a bitwise OR between the
6027   result of the AND operation and the value specified by OrData, and writes the
6028   result to the 64-bit MSR specified by Address. The lower 32-bits of the value
6029   written to the MSR is returned. No parameter checking is performed on Index,
6030   AndData, or OrData, and some of these may cause CPU exceptions. The caller
6031   must either guarantee that Index, AndData, and OrData are valid, or the
6032   caller must establish proper exception handlers. This function is only
6033   available on IA-32 and x64.
6034 
6035   @param  Index   The 32-bit MSR index to write.
6036   @param  AndData The value to AND with the read value from the MSR.
6037   @param  OrData  The value to OR with the result of the AND operation.
6038 
6039   @return The lower 32-bit value written to the MSR.
6040 
6041 **/
6042 UINT32
6043 EFIAPI
6044 AsmMsrAndThenOr32 (
6045   IN      UINT32  Index,
6046   IN      UINT32  AndData,
6047   IN      UINT32  OrData
6048   );
6049 
6050 /**
6051   Reads a bit field of an MSR.
6052 
6053   Reads the bit field in the lower 32-bits of a 64-bit MSR. The bit field is
6054   specified by the StartBit and the EndBit. The value of the bit field is
6055   returned. The caller must either guarantee that Index is valid, or the caller
6056   must set up exception handlers to catch the exceptions. This function is only
6057   available on IA-32 and x64.
6058 
6059   If StartBit is greater than 31, then ASSERT().
6060   If EndBit is greater than 31, then ASSERT().
6061   If EndBit is less than StartBit, then ASSERT().
6062 
6063   @param  Index     The 32-bit MSR index to read.
6064   @param  StartBit  The ordinal of the least significant bit in the bit field.
6065                     Range 0..31.
6066   @param  EndBit    The ordinal of the most significant bit in the bit field.
6067                     Range 0..31.
6068 
6069   @return The bit field read from the MSR.
6070 
6071 **/
6072 UINT32
6073 EFIAPI
6074 AsmMsrBitFieldRead32 (
6075   IN      UINT32  Index,
6076   IN      UINTN   StartBit,
6077   IN      UINTN   EndBit
6078   );
6079 
6080 /**
6081   Writes a bit field to an MSR.
6082 
6083   Writes Value to a bit field in the lower 32-bits of a 64-bit MSR. The bit
6084   field is specified by the StartBit and the EndBit. All other bits in the
6085   destination MSR are preserved. The lower 32-bits of the MSR written is
6086   returned. The caller must either guarantee that Index and the data written
6087   is valid, or the caller must set up exception handlers to catch the exceptions.
6088   This function is only available on IA-32 and x64.
6089 
6090   If StartBit is greater than 31, then ASSERT().
6091   If EndBit is greater than 31, then ASSERT().
6092   If EndBit is less than StartBit, then ASSERT().
6093   If Value is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
6094 
6095   @param  Index     The 32-bit MSR index to write.
6096   @param  StartBit  The ordinal of the least significant bit in the bit field.
6097                     Range 0..31.
6098   @param  EndBit    The ordinal of the most significant bit in the bit field.
6099                     Range 0..31.
6100   @param  Value     New value of the bit field.
6101 
6102   @return The lower 32-bit of the value written to the MSR.
6103 
6104 **/
6105 UINT32
6106 EFIAPI
6107 AsmMsrBitFieldWrite32 (
6108   IN      UINT32  Index,
6109   IN      UINTN   StartBit,
6110   IN      UINTN   EndBit,
6111   IN      UINT32  Value
6112   );
6113 
6114 /**
6115   Reads a bit field in a 64-bit MSR, performs a bitwise OR, and writes the
6116   result back to the bit field in the 64-bit MSR.
6117 
6118   Reads the 64-bit MSR specified by Index, performs a bitwise OR
6119   between the read result and the value specified by OrData, and writes the
6120   result to the 64-bit MSR specified by Index. The lower 32-bits of the value
6121   written to the MSR are returned. Extra left bits in OrData are stripped. The
6122   caller must either guarantee that Index and the data written is valid, or
6123   the caller must set up exception handlers to catch the exceptions. This
6124   function is only available on IA-32 and x64.
6125 
6126   If StartBit is greater than 31, then ASSERT().
6127   If EndBit is greater than 31, then ASSERT().
6128   If EndBit is less than StartBit, then ASSERT().
6129   If OrData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
6130 
6131   @param  Index     The 32-bit MSR index to write.
6132   @param  StartBit  The ordinal of the least significant bit in the bit field.
6133                     Range 0..31.
6134   @param  EndBit    The ordinal of the most significant bit in the bit field.
6135                     Range 0..31.
6136   @param  OrData    The value to OR with the read value from the MSR.
6137 
6138   @return The lower 32-bit of the value written to the MSR.
6139 
6140 **/
6141 UINT32
6142 EFIAPI
6143 AsmMsrBitFieldOr32 (
6144   IN      UINT32  Index,
6145   IN      UINTN   StartBit,
6146   IN      UINTN   EndBit,
6147   IN      UINT32  OrData
6148   );
6149 
6150 /**
6151   Reads a bit field in a 64-bit MSR, performs a bitwise AND, and writes the
6152   result back to the bit field in the 64-bit MSR.
6153 
6154   Reads the 64-bit MSR specified by Index, performs a bitwise AND between the
6155   read result and the value specified by AndData, and writes the result to the
6156   64-bit MSR specified by Index. The lower 32-bits of the value written to the
6157   MSR are returned. Extra left bits in AndData are stripped. The caller must
6158   either guarantee that Index and the data written is valid, or the caller must
6159   set up exception handlers to catch the exceptions. This function is only
6160   available on IA-32 and x64.
6161 
6162   If StartBit is greater than 31, then ASSERT().
6163   If EndBit is greater than 31, then ASSERT().
6164   If EndBit is less than StartBit, then ASSERT().
6165   If AndData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
6166 
6167   @param  Index     The 32-bit MSR index to write.
6168   @param  StartBit  The ordinal of the least significant bit in the bit field.
6169                     Range 0..31.
6170   @param  EndBit    The ordinal of the most significant bit in the bit field.
6171                     Range 0..31.
6172   @param  AndData   The value to AND with the read value from the MSR.
6173 
6174   @return The lower 32-bit of the value written to the MSR.
6175 
6176 **/
6177 UINT32
6178 EFIAPI
6179 AsmMsrBitFieldAnd32 (
6180   IN      UINT32  Index,
6181   IN      UINTN   StartBit,
6182   IN      UINTN   EndBit,
6183   IN      UINT32  AndData
6184   );
6185 
6186 /**
6187   Reads a bit field in a 64-bit MSR, performs a bitwise AND followed by a
6188   bitwise OR, and writes the result back to the bit field in the
6189   64-bit MSR.
6190 
6191   Reads the 64-bit MSR specified by Index, performs a bitwise AND followed by a
6192   bitwise OR between the read result and the value specified by
6193   AndData, and writes the result to the 64-bit MSR specified by Index. The
6194   lower 32-bits of the value written to the MSR are returned. Extra left bits
6195   in both AndData and OrData are stripped. The caller must either guarantee
6196   that Index and the data written is valid, or the caller must set up exception
6197   handlers to catch the exceptions. This function is only available on IA-32
6198   and x64.
6199 
6200   If StartBit is greater than 31, then ASSERT().
6201   If EndBit is greater than 31, then ASSERT().
6202   If EndBit is less than StartBit, then ASSERT().
6203   If AndData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
6204   If OrData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
6205 
6206   @param  Index     The 32-bit MSR index to write.
6207   @param  StartBit  The ordinal of the least significant bit in the bit field.
6208                     Range 0..31.
6209   @param  EndBit    The ordinal of the most significant bit in the bit field.
6210                     Range 0..31.
6211   @param  AndData   The value to AND with the read value from the MSR.
6212   @param  OrData    The value to OR with the result of the AND operation.
6213 
6214   @return The lower 32-bit of the value written to the MSR.
6215 
6216 **/
6217 UINT32
6218 EFIAPI
6219 AsmMsrBitFieldAndThenOr32 (
6220   IN      UINT32  Index,
6221   IN      UINTN   StartBit,
6222   IN      UINTN   EndBit,
6223   IN      UINT32  AndData,
6224   IN      UINT32  OrData
6225   );
6226 
6227 /**
6228   Returns a 64-bit Machine Specific Register(MSR).
6229 
6230   Reads and returns the 64-bit MSR specified by Index. No parameter checking is
6231   performed on Index, and some Index values may cause CPU exceptions. The
6232   caller must either guarantee that Index is valid, or the caller must set up
6233   exception handlers to catch the exceptions. This function is only available
6234   on IA-32 and x64.
6235 
6236   @param  Index The 32-bit MSR index to read.
6237 
6238   @return The value of the MSR identified by Index.
6239 
6240 **/
6241 UINT64
6242 EFIAPI
6243 AsmReadMsr64 (
6244   IN      UINT32  Index
6245   );
6246 
6247 /**
6248   Writes a 64-bit value to a Machine Specific Register(MSR), and returns the
6249   value.
6250 
6251   Writes the 64-bit value specified by Value to the MSR specified by Index. The
6252   64-bit value written to the MSR is returned. No parameter checking is
6253   performed on Index or Value, and some of these may cause CPU exceptions. The
6254   caller must either guarantee that Index and Value are valid, or the caller
6255   must establish proper exception handlers. This function is only available on
6256   IA-32 and x64.
6257 
6258   @param  Index The 32-bit MSR index to write.
6259   @param  Value The 64-bit value to write to the MSR.
6260 
6261   @return Value
6262 
6263 **/
6264 UINT64
6265 EFIAPI
6266 AsmWriteMsr64 (
6267   IN      UINT32  Index,
6268   IN      UINT64  Value
6269   );
6270 
6271 /**
6272   Reads a 64-bit MSR, performs a bitwise OR, and writes the result
6273   back to the 64-bit MSR.
6274 
6275   Reads the 64-bit MSR specified by Index, performs a bitwise OR
6276   between the read result and the value specified by OrData, and writes the
6277   result to the 64-bit MSR specified by Index. The value written to the MSR is
6278   returned. No parameter checking is performed on Index or OrData, and some of
6279   these may cause CPU exceptions. The caller must either guarantee that Index
6280   and OrData are valid, or the caller must establish proper exception handlers.
6281   This function is only available on IA-32 and x64.
6282 
6283   @param  Index   The 32-bit MSR index to write.
6284   @param  OrData  The value to OR with the read value from the MSR.
6285 
6286   @return The value written back to the MSR.
6287 
6288 **/
6289 UINT64
6290 EFIAPI
6291 AsmMsrOr64 (
6292   IN      UINT32  Index,
6293   IN      UINT64  OrData
6294   );
6295 
6296 /**
6297   Reads a 64-bit MSR, performs a bitwise AND, and writes the result back to the
6298   64-bit MSR.
6299 
6300   Reads the 64-bit MSR specified by Index, performs a bitwise AND between the
6301   read result and the value specified by OrData, and writes the result to the
6302   64-bit MSR specified by Index. The value written to the MSR is returned. No
6303   parameter checking is performed on Index or OrData, and some of these may
6304   cause CPU exceptions. The caller must either guarantee that Index and OrData
6305   are valid, or the caller must establish proper exception handlers. This
6306   function is only available on IA-32 and x64.
6307 
6308   @param  Index   The 32-bit MSR index to write.
6309   @param  AndData The value to AND with the read value from the MSR.
6310 
6311   @return The value written back to the MSR.
6312 
6313 **/
6314 UINT64
6315 EFIAPI
6316 AsmMsrAnd64 (
6317   IN      UINT32  Index,
6318   IN      UINT64  AndData
6319   );
6320 
6321 /**
6322   Reads a 64-bit MSR, performs a bitwise AND followed by a bitwise
6323   OR, and writes the result back to the 64-bit MSR.
6324 
6325   Reads the 64-bit MSR specified by Index, performs a bitwise AND between read
6326   result and the value specified by AndData, performs a bitwise OR
6327   between the result of the AND operation and the value specified by OrData,
6328   and writes the result to the 64-bit MSR specified by Index. The value written
6329   to the MSR is returned. No parameter checking is performed on Index, AndData,
6330   or OrData, and some of these may cause CPU exceptions. The caller must either
6331   guarantee that Index, AndData, and OrData are valid, or the caller must
6332   establish proper exception handlers. This function is only available on IA-32
6333   and x64.
6334 
6335   @param  Index   The 32-bit MSR index to write.
6336   @param  AndData The value to AND with the read value from the MSR.
6337   @param  OrData  The value to OR with the result of the AND operation.
6338 
6339   @return The value written back to the MSR.
6340 
6341 **/
6342 UINT64
6343 EFIAPI
6344 AsmMsrAndThenOr64 (
6345   IN      UINT32  Index,
6346   IN      UINT64  AndData,
6347   IN      UINT64  OrData
6348   );
6349 
6350 /**
6351   Reads a bit field of an MSR.
6352 
6353   Reads the bit field in the 64-bit MSR. The bit field is specified by the
6354   StartBit and the EndBit. The value of the bit field is returned. The caller
6355   must either guarantee that Index is valid, or the caller must set up
6356   exception handlers to catch the exceptions. This function is only available
6357   on IA-32 and x64.
6358 
6359   If StartBit is greater than 63, then ASSERT().
6360   If EndBit is greater than 63, then ASSERT().
6361   If EndBit is less than StartBit, then ASSERT().
6362 
6363   @param  Index     The 32-bit MSR index to read.
6364   @param  StartBit  The ordinal of the least significant bit in the bit field.
6365                     Range 0..63.
6366   @param  EndBit    The ordinal of the most significant bit in the bit field.
6367                     Range 0..63.
6368 
6369   @return The value read from the MSR.
6370 
6371 **/
6372 UINT64
6373 EFIAPI
6374 AsmMsrBitFieldRead64 (
6375   IN      UINT32  Index,
6376   IN      UINTN   StartBit,
6377   IN      UINTN   EndBit
6378   );
6379 
6380 /**
6381   Writes a bit field to an MSR.
6382 
6383   Writes Value to a bit field in a 64-bit MSR. The bit field is specified by
6384   the StartBit and the EndBit. All other bits in the destination MSR are
6385   preserved. The MSR written is returned. The caller must either guarantee
6386   that Index and the data written is valid, or the caller must set up exception
6387   handlers to catch the exceptions. This function is only available on IA-32 and x64.
6388 
6389   If StartBit is greater than 63, then ASSERT().
6390   If EndBit is greater than 63, then ASSERT().
6391   If EndBit is less than StartBit, then ASSERT().
6392   If Value is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
6393 
6394   @param  Index     The 32-bit MSR index to write.
6395   @param  StartBit  The ordinal of the least significant bit in the bit field.
6396                     Range 0..63.
6397   @param  EndBit    The ordinal of the most significant bit in the bit field.
6398                     Range 0..63.
6399   @param  Value     New value of the bit field.
6400 
6401   @return The value written back to the MSR.
6402 
6403 **/
6404 UINT64
6405 EFIAPI
6406 AsmMsrBitFieldWrite64 (
6407   IN      UINT32  Index,
6408   IN      UINTN   StartBit,
6409   IN      UINTN   EndBit,
6410   IN      UINT64  Value
6411   );
6412 
6413 /**
6414   Reads a bit field in a 64-bit MSR, performs a bitwise OR, and
6415   writes the result back to the bit field in the 64-bit MSR.
6416 
6417   Reads the 64-bit MSR specified by Index, performs a bitwise OR
6418   between the read result and the value specified by OrData, and writes the
6419   result to the 64-bit MSR specified by Index. The value written to the MSR is
6420   returned. Extra left bits in OrData are stripped. The caller must either
6421   guarantee that Index and the data written is valid, or the caller must set up
6422   exception handlers to catch the exceptions. This function is only available
6423   on IA-32 and x64.
6424 
6425   If StartBit is greater than 63, then ASSERT().
6426   If EndBit is greater than 63, then ASSERT().
6427   If EndBit is less than StartBit, then ASSERT().
6428   If OrData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
6429 
6430   @param  Index     The 32-bit MSR index to write.
6431   @param  StartBit  The ordinal of the least significant bit in the bit field.
6432                     Range 0..63.
6433   @param  EndBit    The ordinal of the most significant bit in the bit field.
6434                     Range 0..63.
6435   @param  OrData    The value to OR with the read value from the bit field.
6436 
6437   @return The value written back to the MSR.
6438 
6439 **/
6440 UINT64
6441 EFIAPI
6442 AsmMsrBitFieldOr64 (
6443   IN      UINT32  Index,
6444   IN      UINTN   StartBit,
6445   IN      UINTN   EndBit,
6446   IN      UINT64  OrData
6447   );
6448 
6449 /**
6450   Reads a bit field in a 64-bit MSR, performs a bitwise AND, and writes the
6451   result back to the bit field in the 64-bit MSR.
6452 
6453   Reads the 64-bit MSR specified by Index, performs a bitwise AND between the
6454   read result and the value specified by AndData, and writes the result to the
6455   64-bit MSR specified by Index. The value written to the MSR is returned.
6456   Extra left bits in AndData are stripped. The caller must either guarantee
6457   that Index and the data written is valid, or the caller must set up exception
6458   handlers to catch the exceptions. This function is only available on IA-32
6459   and x64.
6460 
6461   If StartBit is greater than 63, then ASSERT().
6462   If EndBit is greater than 63, then ASSERT().
6463   If EndBit is less than StartBit, then ASSERT().
6464   If AndData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
6465 
6466   @param  Index     The 32-bit MSR index to write.
6467   @param  StartBit  The ordinal of the least significant bit in the bit field.
6468                     Range 0..63.
6469   @param  EndBit    The ordinal of the most significant bit in the bit field.
6470                     Range 0..63.
6471   @param  AndData   The value to AND with the read value from the bit field.
6472 
6473   @return The value written back to the MSR.
6474 
6475 **/
6476 UINT64
6477 EFIAPI
6478 AsmMsrBitFieldAnd64 (
6479   IN      UINT32  Index,
6480   IN      UINTN   StartBit,
6481   IN      UINTN   EndBit,
6482   IN      UINT64  AndData
6483   );
6484 
6485 /**
6486   Reads a bit field in a 64-bit MSR, performs a bitwise AND followed by a
6487   bitwise OR, and writes the result back to the bit field in the
6488   64-bit MSR.
6489 
6490   Reads the 64-bit MSR specified by Index, performs a bitwise AND followed by
6491   a bitwise OR between the read result and the value specified by
6492   AndData, and writes the result to the 64-bit MSR specified by Index. The
6493   value written to the MSR is returned. Extra left bits in both AndData and
6494   OrData are stripped. The caller must either guarantee that Index and the data
6495   written is valid, or the caller must set up exception handlers to catch the
6496   exceptions. This function is only available on IA-32 and x64.
6497 
6498   If StartBit is greater than 63, then ASSERT().
6499   If EndBit is greater than 63, then ASSERT().
6500   If EndBit is less than StartBit, then ASSERT().
6501   If AndData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
6502   If OrData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
6503 
6504   @param  Index     The 32-bit MSR index to write.
6505   @param  StartBit  The ordinal of the least significant bit in the bit field.
6506                     Range 0..63.
6507   @param  EndBit    The ordinal of the most significant bit in the bit field.
6508                     Range 0..63.
6509   @param  AndData   The value to AND with the read value from the bit field.
6510   @param  OrData    The value to OR with the result of the AND operation.
6511 
6512   @return The value written back to the MSR.
6513 
6514 **/
6515 UINT64
6516 EFIAPI
6517 AsmMsrBitFieldAndThenOr64 (
6518   IN      UINT32  Index,
6519   IN      UINTN   StartBit,
6520   IN      UINTN   EndBit,
6521   IN      UINT64  AndData,
6522   IN      UINT64  OrData
6523   );
6524 
6525 /**
6526   Reads the current value of the EFLAGS register.
6527 
6528   Reads and returns the current value of the EFLAGS register. This function is
6529   only available on IA-32 and x64. This returns a 32-bit value on IA-32 and a
6530   64-bit value on x64.
6531 
6532   @return EFLAGS on IA-32 or RFLAGS on x64.
6533 
6534 **/
6535 UINTN
6536 EFIAPI
6537 AsmReadEflags (
6538   VOID
6539   );
6540 
6541 /**
6542   Reads the current value of the Control Register 0 (CR0).
6543 
6544   Reads and returns the current value of CR0. This function is only available
6545   on IA-32 and x64. This returns a 32-bit value on IA-32 and a 64-bit value on
6546   x64.
6547 
6548   @return The value of the Control Register 0 (CR0).
6549 
6550 **/
6551 UINTN
6552 EFIAPI
6553 AsmReadCr0 (
6554   VOID
6555   );
6556 
6557 /**
6558   Reads the current value of the Control Register 2 (CR2).
6559 
6560   Reads and returns the current value of CR2. This function is only available
6561   on IA-32 and x64. This returns a 32-bit value on IA-32 and a 64-bit value on
6562   x64.
6563 
6564   @return The value of the Control Register 2 (CR2).
6565 
6566 **/
6567 UINTN
6568 EFIAPI
6569 AsmReadCr2 (
6570   VOID
6571   );
6572 
6573 /**
6574   Reads the current value of the Control Register 3 (CR3).
6575 
6576   Reads and returns the current value of CR3. This function is only available
6577   on IA-32 and x64. This returns a 32-bit value on IA-32 and a 64-bit value on
6578   x64.
6579 
6580   @return The value of the Control Register 3 (CR3).
6581 
6582 **/
6583 UINTN
6584 EFIAPI
6585 AsmReadCr3 (
6586   VOID
6587   );
6588 
6589 /**
6590   Reads the current value of the Control Register 4 (CR4).
6591 
6592   Reads and returns the current value of CR4. This function is only available
6593   on IA-32 and x64. This returns a 32-bit value on IA-32 and a 64-bit value on
6594   x64.
6595 
6596   @return The value of the Control Register 4 (CR4).
6597 
6598 **/
6599 UINTN
6600 EFIAPI
6601 AsmReadCr4 (
6602   VOID
6603   );
6604 
6605 /**
6606   Writes a value to Control Register 0 (CR0).
6607 
6608   Writes and returns a new value to CR0. This function is only available on
6609   IA-32 and x64. This writes a 32-bit value on IA-32 and a 64-bit value on x64.
6610 
6611   @param  Cr0 The value to write to CR0.
6612 
6613   @return The value written to CR0.
6614 
6615 **/
6616 UINTN
6617 EFIAPI
6618 AsmWriteCr0 (
6619   UINTN  Cr0
6620   );
6621 
6622 /**
6623   Writes a value to Control Register 2 (CR2).
6624 
6625   Writes and returns a new value to CR2. This function is only available on
6626   IA-32 and x64. This writes a 32-bit value on IA-32 and a 64-bit value on x64.
6627 
6628   @param  Cr2 The value to write to CR2.
6629 
6630   @return The value written to CR2.
6631 
6632 **/
6633 UINTN
6634 EFIAPI
6635 AsmWriteCr2 (
6636   UINTN  Cr2
6637   );
6638 
6639 /**
6640   Writes a value to Control Register 3 (CR3).
6641 
6642   Writes and returns a new value to CR3. This function is only available on
6643   IA-32 and x64. This writes a 32-bit value on IA-32 and a 64-bit value on x64.
6644 
6645   @param  Cr3 The value to write to CR3.
6646 
6647   @return The value written to CR3.
6648 
6649 **/
6650 UINTN
6651 EFIAPI
6652 AsmWriteCr3 (
6653   UINTN  Cr3
6654   );
6655 
6656 /**
6657   Writes a value to Control Register 4 (CR4).
6658 
6659   Writes and returns a new value to CR4. This function is only available on
6660   IA-32 and x64. This writes a 32-bit value on IA-32 and a 64-bit value on x64.
6661 
6662   @param  Cr4 The value to write to CR4.
6663 
6664   @return The value written to CR4.
6665 
6666 **/
6667 UINTN
6668 EFIAPI
6669 AsmWriteCr4 (
6670   UINTN  Cr4
6671   );
6672 
6673 /**
6674   Reads the current value of Debug Register 0 (DR0).
6675 
6676   Reads and returns the current value of DR0. This function is only available
6677   on IA-32 and x64. This returns a 32-bit value on IA-32 and a 64-bit value on
6678   x64.
6679 
6680   @return The value of Debug Register 0 (DR0).
6681 
6682 **/
6683 UINTN
6684 EFIAPI
6685 AsmReadDr0 (
6686   VOID
6687   );
6688 
6689 /**
6690   Reads the current value of Debug Register 1 (DR1).
6691 
6692   Reads and returns the current value of DR1. This function is only available
6693   on IA-32 and x64. This returns a 32-bit value on IA-32 and a 64-bit value on
6694   x64.
6695 
6696   @return The value of Debug Register 1 (DR1).
6697 
6698 **/
6699 UINTN
6700 EFIAPI
6701 AsmReadDr1 (
6702   VOID
6703   );
6704 
6705 /**
6706   Reads the current value of Debug Register 2 (DR2).
6707 
6708   Reads and returns the current value of DR2. This function is only available
6709   on IA-32 and x64. This returns a 32-bit value on IA-32 and a 64-bit value on
6710   x64.
6711 
6712   @return The value of Debug Register 2 (DR2).
6713 
6714 **/
6715 UINTN
6716 EFIAPI
6717 AsmReadDr2 (
6718   VOID
6719   );
6720 
6721 /**
6722   Reads the current value of Debug Register 3 (DR3).
6723 
6724   Reads and returns the current value of DR3. This function is only available
6725   on IA-32 and x64. This returns a 32-bit value on IA-32 and a 64-bit value on
6726   x64.
6727 
6728   @return The value of Debug Register 3 (DR3).
6729 
6730 **/
6731 UINTN
6732 EFIAPI
6733 AsmReadDr3 (
6734   VOID
6735   );
6736 
6737 /**
6738   Reads the current value of Debug Register 4 (DR4).
6739 
6740   Reads and returns the current value of DR4. This function is only available
6741   on IA-32 and x64. This returns a 32-bit value on IA-32 and a 64-bit value on
6742   x64.
6743 
6744   @return The value of Debug Register 4 (DR4).
6745 
6746 **/
6747 UINTN
6748 EFIAPI
6749 AsmReadDr4 (
6750   VOID
6751   );
6752 
6753 /**
6754   Reads the current value of Debug Register 5 (DR5).
6755 
6756   Reads and returns the current value of DR5. This function is only available
6757   on IA-32 and x64. This returns a 32-bit value on IA-32 and a 64-bit value on
6758   x64.
6759 
6760   @return The value of Debug Register 5 (DR5).
6761 
6762 **/
6763 UINTN
6764 EFIAPI
6765 AsmReadDr5 (
6766   VOID
6767   );
6768 
6769 /**
6770   Reads the current value of Debug Register 6 (DR6).
6771 
6772   Reads and returns the current value of DR6. This function is only available
6773   on IA-32 and x64. This returns a 32-bit value on IA-32 and a 64-bit value on
6774   x64.
6775 
6776   @return The value of Debug Register 6 (DR6).
6777 
6778 **/
6779 UINTN
6780 EFIAPI
6781 AsmReadDr6 (
6782   VOID
6783   );
6784 
6785 /**
6786   Reads the current value of Debug Register 7 (DR7).
6787 
6788   Reads and returns the current value of DR7. This function is only available
6789   on IA-32 and x64. This returns a 32-bit value on IA-32 and a 64-bit value on
6790   x64.
6791 
6792   @return The value of Debug Register 7 (DR7).
6793 
6794 **/
6795 UINTN
6796 EFIAPI
6797 AsmReadDr7 (
6798   VOID
6799   );
6800 
6801 /**
6802   Writes a value to Debug Register 0 (DR0).
6803 
6804   Writes and returns a new value to DR0. This function is only available on
6805   IA-32 and x64. This writes a 32-bit value on IA-32 and a 64-bit value on x64.
6806 
6807   @param  Dr0 The value to write to Dr0.
6808 
6809   @return The value written to Debug Register 0 (DR0).
6810 
6811 **/
6812 UINTN
6813 EFIAPI
6814 AsmWriteDr0 (
6815   UINTN  Dr0
6816   );
6817 
6818 /**
6819   Writes a value to Debug Register 1 (DR1).
6820 
6821   Writes and returns a new value to DR1. This function is only available on
6822   IA-32 and x64. This writes a 32-bit value on IA-32 and a 64-bit value on x64.
6823 
6824   @param  Dr1 The value to write to Dr1.
6825 
6826   @return The value written to Debug Register 1 (DR1).
6827 
6828 **/
6829 UINTN
6830 EFIAPI
6831 AsmWriteDr1 (
6832   UINTN  Dr1
6833   );
6834 
6835 /**
6836   Writes a value to Debug Register 2 (DR2).
6837 
6838   Writes and returns a new value to DR2. This function is only available on
6839   IA-32 and x64. This writes a 32-bit value on IA-32 and a 64-bit value on x64.
6840 
6841   @param  Dr2 The value to write to Dr2.
6842 
6843   @return The value written to Debug Register 2 (DR2).
6844 
6845 **/
6846 UINTN
6847 EFIAPI
6848 AsmWriteDr2 (
6849   UINTN  Dr2
6850   );
6851 
6852 /**
6853   Writes a value to Debug Register 3 (DR3).
6854 
6855   Writes and returns a new value to DR3. This function is only available on
6856   IA-32 and x64. This writes a 32-bit value on IA-32 and a 64-bit value on x64.
6857 
6858   @param  Dr3 The value to write to Dr3.
6859 
6860   @return The value written to Debug Register 3 (DR3).
6861 
6862 **/
6863 UINTN
6864 EFIAPI
6865 AsmWriteDr3 (
6866   UINTN  Dr3
6867   );
6868 
6869 /**
6870   Writes a value to Debug Register 4 (DR4).
6871 
6872   Writes and returns a new value to DR4. This function is only available on
6873   IA-32 and x64. This writes a 32-bit value on IA-32 and a 64-bit value on x64.
6874 
6875   @param  Dr4 The value to write to Dr4.
6876 
6877   @return The value written to Debug Register 4 (DR4).
6878 
6879 **/
6880 UINTN
6881 EFIAPI
6882 AsmWriteDr4 (
6883   UINTN  Dr4
6884   );
6885 
6886 /**
6887   Writes a value to Debug Register 5 (DR5).
6888 
6889   Writes and returns a new value to DR5. This function is only available on
6890   IA-32 and x64. This writes a 32-bit value on IA-32 and a 64-bit value on x64.
6891 
6892   @param  Dr5 The value to write to Dr5.
6893 
6894   @return The value written to Debug Register 5 (DR5).
6895 
6896 **/
6897 UINTN
6898 EFIAPI
6899 AsmWriteDr5 (
6900   UINTN  Dr5
6901   );
6902 
6903 /**
6904   Writes a value to Debug Register 6 (DR6).
6905 
6906   Writes and returns a new value to DR6. This function is only available on
6907   IA-32 and x64. This writes a 32-bit value on IA-32 and a 64-bit value on x64.
6908 
6909   @param  Dr6 The value to write to Dr6.
6910 
6911   @return The value written to Debug Register 6 (DR6).
6912 
6913 **/
6914 UINTN
6915 EFIAPI
6916 AsmWriteDr6 (
6917   UINTN  Dr6
6918   );
6919 
6920 /**
6921   Writes a value to Debug Register 7 (DR7).
6922 
6923   Writes and returns a new value to DR7. This function is only available on
6924   IA-32 and x64. This writes a 32-bit value on IA-32 and a 64-bit value on x64.
6925 
6926   @param  Dr7 The value to write to Dr7.
6927 
6928   @return The value written to Debug Register 7 (DR7).
6929 
6930 **/
6931 UINTN
6932 EFIAPI
6933 AsmWriteDr7 (
6934   UINTN  Dr7
6935   );
6936 
6937 /**
6938   Reads the current value of Code Segment Register (CS).
6939 
6940   Reads and returns the current value of CS. This function is only available on
6941   IA-32 and x64.
6942 
6943   @return The current value of CS.
6944 
6945 **/
6946 UINT16
6947 EFIAPI
6948 AsmReadCs (
6949   VOID
6950   );
6951 
6952 /**
6953   Reads the current value of Data Segment Register (DS).
6954 
6955   Reads and returns the current value of DS. This function is only available on
6956   IA-32 and x64.
6957 
6958   @return The current value of DS.
6959 
6960 **/
6961 UINT16
6962 EFIAPI
6963 AsmReadDs (
6964   VOID
6965   );
6966 
6967 /**
6968   Reads the current value of Extra Segment Register (ES).
6969 
6970   Reads and returns the current value of ES. This function is only available on
6971   IA-32 and x64.
6972 
6973   @return The current value of ES.
6974 
6975 **/
6976 UINT16
6977 EFIAPI
6978 AsmReadEs (
6979   VOID
6980   );
6981 
6982 /**
6983   Reads the current value of FS Data Segment Register (FS).
6984 
6985   Reads and returns the current value of FS. This function is only available on
6986   IA-32 and x64.
6987 
6988   @return The current value of FS.
6989 
6990 **/
6991 UINT16
6992 EFIAPI
6993 AsmReadFs (
6994   VOID
6995   );
6996 
6997 /**
6998   Reads the current value of GS Data Segment Register (GS).
6999 
7000   Reads and returns the current value of GS. This function is only available on
7001   IA-32 and x64.
7002 
7003   @return The current value of GS.
7004 
7005 **/
7006 UINT16
7007 EFIAPI
7008 AsmReadGs (
7009   VOID
7010   );
7011 
7012 /**
7013   Reads the current value of Stack Segment Register (SS).
7014 
7015   Reads and returns the current value of SS. This function is only available on
7016   IA-32 and x64.
7017 
7018   @return The current value of SS.
7019 
7020 **/
7021 UINT16
7022 EFIAPI
7023 AsmReadSs (
7024   VOID
7025   );
7026 
7027 /**
7028   Reads the current value of Task Register (TR).
7029 
7030   Reads and returns the current value of TR. This function is only available on
7031   IA-32 and x64.
7032 
7033   @return The current value of TR.
7034 
7035 **/
7036 UINT16
7037 EFIAPI
7038 AsmReadTr (
7039   VOID
7040   );
7041 
7042 /**
7043   Reads the current Global Descriptor Table Register(GDTR) descriptor.
7044 
7045   Reads and returns the current GDTR descriptor and returns it in Gdtr. This
7046   function is only available on IA-32 and x64.
7047 
7048   If Gdtr is NULL, then ASSERT().
7049 
7050   @param  Gdtr  The pointer to a GDTR descriptor.
7051 
7052 **/
7053 VOID
7054 EFIAPI
7055 AsmReadGdtr (
7056   OUT     IA32_DESCRIPTOR  *Gdtr
7057   );
7058 
7059 /**
7060   Writes the current Global Descriptor Table Register (GDTR) descriptor.
7061 
7062   Writes and the current GDTR descriptor specified by Gdtr. This function is
7063   only available on IA-32 and x64.
7064 
7065   If Gdtr is NULL, then ASSERT().
7066 
7067   @param  Gdtr  The pointer to a GDTR descriptor.
7068 
7069 **/
7070 VOID
7071 EFIAPI
7072 AsmWriteGdtr (
7073   IN      CONST IA32_DESCRIPTOR  *Gdtr
7074   );
7075 
7076 /**
7077   Reads the current Interrupt Descriptor Table Register(IDTR) descriptor.
7078 
7079   Reads and returns the current IDTR descriptor and returns it in Idtr. This
7080   function is only available on IA-32 and x64.
7081 
7082   If Idtr is NULL, then ASSERT().
7083 
7084   @param  Idtr  The pointer to a IDTR descriptor.
7085 
7086 **/
7087 VOID
7088 EFIAPI
7089 AsmReadIdtr (
7090   OUT     IA32_DESCRIPTOR  *Idtr
7091   );
7092 
7093 /**
7094   Writes the current Interrupt Descriptor Table Register(IDTR) descriptor.
7095 
7096   Writes the current IDTR descriptor and returns it in Idtr. This function is
7097   only available on IA-32 and x64.
7098 
7099   If Idtr is NULL, then ASSERT().
7100 
7101   @param  Idtr  The pointer to a IDTR descriptor.
7102 
7103 **/
7104 VOID
7105 EFIAPI
7106 AsmWriteIdtr (
7107   IN      CONST IA32_DESCRIPTOR  *Idtr
7108   );
7109 
7110 /**
7111   Reads the current Local Descriptor Table Register(LDTR) selector.
7112 
7113   Reads and returns the current 16-bit LDTR descriptor value. This function is
7114   only available on IA-32 and x64.
7115 
7116   @return The current selector of LDT.
7117 
7118 **/
7119 UINT16
7120 EFIAPI
7121 AsmReadLdtr (
7122   VOID
7123   );
7124 
7125 /**
7126   Writes the current Local Descriptor Table Register (LDTR) selector.
7127 
7128   Writes and the current LDTR descriptor specified by Ldtr. This function is
7129   only available on IA-32 and x64.
7130 
7131   @param  Ldtr  16-bit LDTR selector value.
7132 
7133 **/
7134 VOID
7135 EFIAPI
7136 AsmWriteLdtr (
7137   IN      UINT16  Ldtr
7138   );
7139 
7140 /**
7141   Save the current floating point/SSE/SSE2 context to a buffer.
7142 
7143   Saves the current floating point/SSE/SSE2 state to the buffer specified by
7144   Buffer. Buffer must be aligned on a 16-byte boundary. This function is only
7145   available on IA-32 and x64.
7146 
7147   If Buffer is NULL, then ASSERT().
7148   If Buffer is not aligned on a 16-byte boundary, then ASSERT().
7149 
7150   @param  Buffer  The pointer to a buffer to save the floating point/SSE/SSE2 context.
7151 
7152 **/
7153 VOID
7154 EFIAPI
7155 AsmFxSave (
7156   OUT     IA32_FX_BUFFER  *Buffer
7157   );
7158 
7159 /**
7160   Restores the current floating point/SSE/SSE2 context from a buffer.
7161 
7162   Restores the current floating point/SSE/SSE2 state from the buffer specified
7163   by Buffer. Buffer must be aligned on a 16-byte boundary. This function is
7164   only available on IA-32 and x64.
7165 
7166   If Buffer is NULL, then ASSERT().
7167   If Buffer is not aligned on a 16-byte boundary, then ASSERT().
7168   If Buffer was not saved with AsmFxSave(), then ASSERT().
7169 
7170   @param  Buffer  The pointer to a buffer to save the floating point/SSE/SSE2 context.
7171 
7172 **/
7173 VOID
7174 EFIAPI
7175 AsmFxRestore (
7176   IN      CONST IA32_FX_BUFFER  *Buffer
7177   );
7178 
7179 /**
7180   Reads the current value of 64-bit MMX Register #0 (MM0).
7181 
7182   Reads and returns the current value of MM0. This function is only available
7183   on IA-32 and x64.
7184 
7185   @return The current value of MM0.
7186 
7187 **/
7188 UINT64
7189 EFIAPI
7190 AsmReadMm0 (
7191   VOID
7192   );
7193 
7194 /**
7195   Reads the current value of 64-bit MMX Register #1 (MM1).
7196 
7197   Reads and returns the current value of MM1. This function is only available
7198   on IA-32 and x64.
7199 
7200   @return The current value of MM1.
7201 
7202 **/
7203 UINT64
7204 EFIAPI
7205 AsmReadMm1 (
7206   VOID
7207   );
7208 
7209 /**
7210   Reads the current value of 64-bit MMX Register #2 (MM2).
7211 
7212   Reads and returns the current value of MM2. This function is only available
7213   on IA-32 and x64.
7214 
7215   @return The current value of MM2.
7216 
7217 **/
7218 UINT64
7219 EFIAPI
7220 AsmReadMm2 (
7221   VOID
7222   );
7223 
7224 /**
7225   Reads the current value of 64-bit MMX Register #3 (MM3).
7226 
7227   Reads and returns the current value of MM3. This function is only available
7228   on IA-32 and x64.
7229 
7230   @return The current value of MM3.
7231 
7232 **/
7233 UINT64
7234 EFIAPI
7235 AsmReadMm3 (
7236   VOID
7237   );
7238 
7239 /**
7240   Reads the current value of 64-bit MMX Register #4 (MM4).
7241 
7242   Reads and returns the current value of MM4. This function is only available
7243   on IA-32 and x64.
7244 
7245   @return The current value of MM4.
7246 
7247 **/
7248 UINT64
7249 EFIAPI
7250 AsmReadMm4 (
7251   VOID
7252   );
7253 
7254 /**
7255   Reads the current value of 64-bit MMX Register #5 (MM5).
7256 
7257   Reads and returns the current value of MM5. This function is only available
7258   on IA-32 and x64.
7259 
7260   @return The current value of MM5.
7261 
7262 **/
7263 UINT64
7264 EFIAPI
7265 AsmReadMm5 (
7266   VOID
7267   );
7268 
7269 /**
7270   Reads the current value of 64-bit MMX Register #6 (MM6).
7271 
7272   Reads and returns the current value of MM6. This function is only available
7273   on IA-32 and x64.
7274 
7275   @return The current value of MM6.
7276 
7277 **/
7278 UINT64
7279 EFIAPI
7280 AsmReadMm6 (
7281   VOID
7282   );
7283 
7284 /**
7285   Reads the current value of 64-bit MMX Register #7 (MM7).
7286 
7287   Reads and returns the current value of MM7. This function is only available
7288   on IA-32 and x64.
7289 
7290   @return The current value of MM7.
7291 
7292 **/
7293 UINT64
7294 EFIAPI
7295 AsmReadMm7 (
7296   VOID
7297   );
7298 
7299 /**
7300   Writes the current value of 64-bit MMX Register #0 (MM0).
7301 
7302   Writes the current value of MM0. This function is only available on IA32 and
7303   x64.
7304 
7305   @param  Value The 64-bit value to write to MM0.
7306 
7307 **/
7308 VOID
7309 EFIAPI
7310 AsmWriteMm0 (
7311   IN      UINT64  Value
7312   );
7313 
7314 /**
7315   Writes the current value of 64-bit MMX Register #1 (MM1).
7316 
7317   Writes the current value of MM1. This function is only available on IA32 and
7318   x64.
7319 
7320   @param  Value The 64-bit value to write to MM1.
7321 
7322 **/
7323 VOID
7324 EFIAPI
7325 AsmWriteMm1 (
7326   IN      UINT64  Value
7327   );
7328 
7329 /**
7330   Writes the current value of 64-bit MMX Register #2 (MM2).
7331 
7332   Writes the current value of MM2. This function is only available on IA32 and
7333   x64.
7334 
7335   @param  Value The 64-bit value to write to MM2.
7336 
7337 **/
7338 VOID
7339 EFIAPI
7340 AsmWriteMm2 (
7341   IN      UINT64  Value
7342   );
7343 
7344 /**
7345   Writes the current value of 64-bit MMX Register #3 (MM3).
7346 
7347   Writes the current value of MM3. This function is only available on IA32 and
7348   x64.
7349 
7350   @param  Value The 64-bit value to write to MM3.
7351 
7352 **/
7353 VOID
7354 EFIAPI
7355 AsmWriteMm3 (
7356   IN      UINT64  Value
7357   );
7358 
7359 /**
7360   Writes the current value of 64-bit MMX Register #4 (MM4).
7361 
7362   Writes the current value of MM4. This function is only available on IA32 and
7363   x64.
7364 
7365   @param  Value The 64-bit value to write to MM4.
7366 
7367 **/
7368 VOID
7369 EFIAPI
7370 AsmWriteMm4 (
7371   IN      UINT64  Value
7372   );
7373 
7374 /**
7375   Writes the current value of 64-bit MMX Register #5 (MM5).
7376 
7377   Writes the current value of MM5. This function is only available on IA32 and
7378   x64.
7379 
7380   @param  Value The 64-bit value to write to MM5.
7381 
7382 **/
7383 VOID
7384 EFIAPI
7385 AsmWriteMm5 (
7386   IN      UINT64  Value
7387   );
7388 
7389 /**
7390   Writes the current value of 64-bit MMX Register #6 (MM6).
7391 
7392   Writes the current value of MM6. This function is only available on IA32 and
7393   x64.
7394 
7395   @param  Value The 64-bit value to write to MM6.
7396 
7397 **/
7398 VOID
7399 EFIAPI
7400 AsmWriteMm6 (
7401   IN      UINT64  Value
7402   );
7403 
7404 /**
7405   Writes the current value of 64-bit MMX Register #7 (MM7).
7406 
7407   Writes the current value of MM7. This function is only available on IA32 and
7408   x64.
7409 
7410   @param  Value The 64-bit value to write to MM7.
7411 
7412 **/
7413 VOID
7414 EFIAPI
7415 AsmWriteMm7 (
7416   IN      UINT64  Value
7417   );
7418 
7419 /**
7420   Reads the current value of Time Stamp Counter (TSC).
7421 
7422   Reads and returns the current value of TSC. This function is only available
7423   on IA-32 and x64.
7424 
7425   @return The current value of TSC
7426 
7427 **/
7428 UINT64
7429 EFIAPI
7430 AsmReadTsc (
7431   VOID
7432   );
7433 
7434 /**
7435   Reads the current value of a Performance Counter (PMC).
7436 
7437   Reads and returns the current value of performance counter specified by
7438   Index. This function is only available on IA-32 and x64.
7439 
7440   @param  Index The 32-bit Performance Counter index to read.
7441 
7442   @return The value of the PMC specified by Index.
7443 
7444 **/
7445 UINT64
7446 EFIAPI
7447 AsmReadPmc (
7448   IN      UINT32  Index
7449   );
7450 
7451 /**
7452   Sets up a monitor buffer that is used by AsmMwait().
7453 
7454   Executes a MONITOR instruction with the register state specified by Eax, Ecx
7455   and Edx. Returns Eax. This function is only available on IA-32 and x64.
7456 
7457   @param  Eax The value to load into EAX or RAX before executing the MONITOR
7458               instruction.
7459   @param  Ecx The value to load into ECX or RCX before executing the MONITOR
7460               instruction.
7461   @param  Edx The value to load into EDX or RDX before executing the MONITOR
7462               instruction.
7463 
7464   @return Eax
7465 
7466 **/
7467 UINTN
7468 EFIAPI
7469 AsmMonitor (
7470   IN      UINTN  Eax,
7471   IN      UINTN  Ecx,
7472   IN      UINTN  Edx
7473   );
7474 
7475 /**
7476   Executes an MWAIT instruction.
7477 
7478   Executes an MWAIT instruction with the register state specified by Eax and
7479   Ecx. Returns Eax. This function is only available on IA-32 and x64.
7480 
7481   @param  Eax The value to load into EAX or RAX before executing the MONITOR
7482               instruction.
7483   @param  Ecx The value to load into ECX or RCX before executing the MONITOR
7484               instruction.
7485 
7486   @return Eax
7487 
7488 **/
7489 UINTN
7490 EFIAPI
7491 AsmMwait (
7492   IN      UINTN  Eax,
7493   IN      UINTN  Ecx
7494   );
7495 
7496 /**
7497   Executes a WBINVD instruction.
7498 
7499   Executes a WBINVD instruction. This function is only available on IA-32 and
7500   x64.
7501 
7502 **/
7503 VOID
7504 EFIAPI
7505 AsmWbinvd (
7506   VOID
7507   );
7508 
7509 /**
7510   Executes a INVD instruction.
7511 
7512   Executes a INVD instruction. This function is only available on IA-32 and
7513   x64.
7514 
7515 **/
7516 VOID
7517 EFIAPI
7518 AsmInvd (
7519   VOID
7520   );
7521 
7522 /**
7523   Flushes a cache line from all the instruction and data caches within the
7524   coherency domain of the CPU.
7525 
7526   Flushed the cache line specified by LinearAddress, and returns LinearAddress.
7527   This function is only available on IA-32 and x64.
7528 
7529   @param  LinearAddress The address of the cache line to flush. If the CPU is
7530                         in a physical addressing mode, then LinearAddress is a
7531                         physical address. If the CPU is in a virtual
7532                         addressing mode, then LinearAddress is a virtual
7533                         address.
7534 
7535   @return LinearAddress.
7536 **/
7537 VOID *
7538 EFIAPI
7539 AsmFlushCacheLine (
7540   IN      VOID  *LinearAddress
7541   );
7542 
7543 /**
7544   Enables the 32-bit paging mode on the CPU.
7545 
7546   Enables the 32-bit paging mode on the CPU. CR0, CR3, CR4, and the page tables
7547   must be properly initialized prior to calling this service. This function
7548   assumes the current execution mode is 32-bit protected mode. This function is
7549   only available on IA-32. After the 32-bit paging mode is enabled, control is
7550   transferred to the function specified by EntryPoint using the new stack
7551   specified by NewStack and passing in the parameters specified by Context1 and
7552   Context2. Context1 and Context2 are optional and may be NULL. The function
7553   EntryPoint must never return.
7554 
7555   If the current execution mode is not 32-bit protected mode, then ASSERT().
7556   If EntryPoint is NULL, then ASSERT().
7557   If NewStack is NULL, then ASSERT().
7558 
7559   There are a number of constraints that must be followed before calling this
7560   function:
7561   1)  Interrupts must be disabled.
7562   2)  The caller must be in 32-bit protected mode with flat descriptors. This
7563       means all descriptors must have a base of 0 and a limit of 4GB.
7564   3)  CR0 and CR4 must be compatible with 32-bit protected mode with flat
7565       descriptors.
7566   4)  CR3 must point to valid page tables that will be used once the transition
7567       is complete, and those page tables must guarantee that the pages for this
7568       function and the stack are identity mapped.
7569 
7570   @param  EntryPoint  A pointer to function to call with the new stack after
7571                       paging is enabled.
7572   @param  Context1    A pointer to the context to pass into the EntryPoint
7573                       function as the first parameter after paging is enabled.
7574   @param  Context2    A pointer to the context to pass into the EntryPoint
7575                       function as the second parameter after paging is enabled.
7576   @param  NewStack    A pointer to the new stack to use for the EntryPoint
7577                       function after paging is enabled.
7578 
7579 **/
7580 VOID
7581 EFIAPI
7582 AsmEnablePaging32 (
7583   IN      SWITCH_STACK_ENTRY_POINT  EntryPoint,
7584   IN      VOID                      *Context1   OPTIONAL,
7585   IN      VOID                      *Context2   OPTIONAL,
7586   IN      VOID                      *NewStack
7587   );
7588 
7589 /**
7590   Disables the 32-bit paging mode on the CPU.
7591 
7592   Disables the 32-bit paging mode on the CPU and returns to 32-bit protected
7593   mode. This function assumes the current execution mode is 32-paged protected
7594   mode. This function is only available on IA-32. After the 32-bit paging mode
7595   is disabled, control is transferred to the function specified by EntryPoint
7596   using the new stack specified by NewStack and passing in the parameters
7597   specified by Context1 and Context2. Context1 and Context2 are optional and
7598   may be NULL. The function EntryPoint must never return.
7599 
7600   If the current execution mode is not 32-bit paged mode, then ASSERT().
7601   If EntryPoint is NULL, then ASSERT().
7602   If NewStack is NULL, then ASSERT().
7603 
7604   There are a number of constraints that must be followed before calling this
7605   function:
7606   1)  Interrupts must be disabled.
7607   2)  The caller must be in 32-bit paged mode.
7608   3)  CR0, CR3, and CR4 must be compatible with 32-bit paged mode.
7609   4)  CR3 must point to valid page tables that guarantee that the pages for
7610       this function and the stack are identity mapped.
7611 
7612   @param  EntryPoint  A pointer to function to call with the new stack after
7613                       paging is disabled.
7614   @param  Context1    A pointer to the context to pass into the EntryPoint
7615                       function as the first parameter after paging is disabled.
7616   @param  Context2    A pointer to the context to pass into the EntryPoint
7617                       function as the second parameter after paging is
7618                       disabled.
7619   @param  NewStack    A pointer to the new stack to use for the EntryPoint
7620                       function after paging is disabled.
7621 
7622 **/
7623 VOID
7624 EFIAPI
7625 AsmDisablePaging32 (
7626   IN      SWITCH_STACK_ENTRY_POINT  EntryPoint,
7627   IN      VOID                      *Context1   OPTIONAL,
7628   IN      VOID                      *Context2   OPTIONAL,
7629   IN      VOID                      *NewStack
7630   );
7631 
7632 /**
7633   Enables the 64-bit paging mode on the CPU.
7634 
7635   Enables the 64-bit paging mode on the CPU. CR0, CR3, CR4, and the page tables
7636   must be properly initialized prior to calling this service. This function
7637   assumes the current execution mode is 32-bit protected mode with flat
7638   descriptors. This function is only available on IA-32. After the 64-bit
7639   paging mode is enabled, control is transferred to the function specified by
7640   EntryPoint using the new stack specified by NewStack and passing in the
7641   parameters specified by Context1 and Context2. Context1 and Context2 are
7642   optional and may be 0. The function EntryPoint must never return.
7643 
7644   If the current execution mode is not 32-bit protected mode with flat
7645   descriptors, then ASSERT().
7646   If EntryPoint is 0, then ASSERT().
7647   If NewStack is 0, then ASSERT().
7648 
7649   @param  Cs          The 16-bit selector to load in the CS before EntryPoint
7650                       is called. The descriptor in the GDT that this selector
7651                       references must be setup for long mode.
7652   @param  EntryPoint  The 64-bit virtual address of the function to call with
7653                       the new stack after paging is enabled.
7654   @param  Context1    The 64-bit virtual address of the context to pass into
7655                       the EntryPoint function as the first parameter after
7656                       paging is enabled.
7657   @param  Context2    The 64-bit virtual address of the context to pass into
7658                       the EntryPoint function as the second parameter after
7659                       paging is enabled.
7660   @param  NewStack    The 64-bit virtual address of the new stack to use for
7661                       the EntryPoint function after paging is enabled.
7662 
7663 **/
7664 VOID
7665 EFIAPI
7666 AsmEnablePaging64 (
7667   IN      UINT16  Cs,
7668   IN      UINT64  EntryPoint,
7669   IN      UINT64  Context1   OPTIONAL,
7670   IN      UINT64  Context2   OPTIONAL,
7671   IN      UINT64  NewStack
7672   );
7673 
7674 /**
7675   Disables the 64-bit paging mode on the CPU.
7676 
7677   Disables the 64-bit paging mode on the CPU and returns to 32-bit protected
7678   mode. This function assumes the current execution mode is 64-paging mode.
7679   This function is only available on x64. After the 64-bit paging mode is
7680   disabled, control is transferred to the function specified by EntryPoint
7681   using the new stack specified by NewStack and passing in the parameters
7682   specified by Context1 and Context2. Context1 and Context2 are optional and
7683   may be 0. The function EntryPoint must never return.
7684 
7685   If the current execution mode is not 64-bit paged mode, then ASSERT().
7686   If EntryPoint is 0, then ASSERT().
7687   If NewStack is 0, then ASSERT().
7688 
7689   @param  Cs          The 16-bit selector to load in the CS before EntryPoint
7690                       is called. The descriptor in the GDT that this selector
7691                       references must be setup for 32-bit protected mode.
7692   @param  EntryPoint  The 64-bit virtual address of the function to call with
7693                       the new stack after paging is disabled.
7694   @param  Context1    The 64-bit virtual address of the context to pass into
7695                       the EntryPoint function as the first parameter after
7696                       paging is disabled.
7697   @param  Context2    The 64-bit virtual address of the context to pass into
7698                       the EntryPoint function as the second parameter after
7699                       paging is disabled.
7700   @param  NewStack    The 64-bit virtual address of the new stack to use for
7701                       the EntryPoint function after paging is disabled.
7702 
7703 **/
7704 VOID
7705 EFIAPI
7706 AsmDisablePaging64 (
7707   IN      UINT16  Cs,
7708   IN      UINT32  EntryPoint,
7709   IN      UINT32  Context1   OPTIONAL,
7710   IN      UINT32  Context2   OPTIONAL,
7711   IN      UINT32  NewStack
7712   );
7713 
7714 //
7715 // 16-bit thunking services
7716 //
7717 
7718 /**
7719   Retrieves the properties for 16-bit thunk functions.
7720 
7721   Computes the size of the buffer and stack below 1MB required to use the
7722   AsmPrepareThunk16(), AsmThunk16() and AsmPrepareAndThunk16() functions. This
7723   buffer size is returned in RealModeBufferSize, and the stack size is returned
7724   in ExtraStackSize. If parameters are passed to the 16-bit real mode code,
7725   then the actual minimum stack size is ExtraStackSize plus the maximum number
7726   of bytes that need to be passed to the 16-bit real mode code.
7727 
7728   If RealModeBufferSize is NULL, then ASSERT().
7729   If ExtraStackSize is NULL, then ASSERT().
7730 
7731   @param  RealModeBufferSize  A pointer to the size of the buffer below 1MB
7732                               required to use the 16-bit thunk functions.
7733   @param  ExtraStackSize      A pointer to the extra size of stack below 1MB
7734                               that the 16-bit thunk functions require for
7735                               temporary storage in the transition to and from
7736                               16-bit real mode.
7737 
7738 **/
7739 VOID
7740 EFIAPI
7741 AsmGetThunk16Properties (
7742   OUT     UINT32  *RealModeBufferSize,
7743   OUT     UINT32  *ExtraStackSize
7744   );
7745 
7746 /**
7747   Prepares all structures a code required to use AsmThunk16().
7748 
7749   Prepares all structures and code required to use AsmThunk16().
7750 
7751   This interface is limited to be used in either physical mode or virtual modes with paging enabled where the
7752   virtual to physical mappings for ThunkContext.RealModeBuffer is mapped 1:1.
7753 
7754   If ThunkContext is NULL, then ASSERT().
7755 
7756   @param  ThunkContext  A pointer to the context structure that describes the
7757                         16-bit real mode code to call.
7758 
7759 **/
7760 VOID
7761 EFIAPI
7762 AsmPrepareThunk16 (
7763   IN OUT  THUNK_CONTEXT  *ThunkContext
7764   );
7765 
7766 /**
7767   Transfers control to a 16-bit real mode entry point and returns the results.
7768 
7769   Transfers control to a 16-bit real mode entry point and returns the results.
7770   AsmPrepareThunk16() must be called with ThunkContext before this function is used.
7771   This function must be called with interrupts disabled.
7772 
7773   The register state from the RealModeState field of ThunkContext is restored just prior
7774   to calling the 16-bit real mode entry point.  This includes the EFLAGS field of RealModeState,
7775   which is used to set the interrupt state when a 16-bit real mode entry point is called.
7776   Control is transferred to the 16-bit real mode entry point specified by the CS and Eip fields of RealModeState.
7777   The stack is initialized to the SS and ESP fields of RealModeState.  Any parameters passed to
7778   the 16-bit real mode code must be populated by the caller at SS:ESP prior to calling this function.
7779   The 16-bit real mode entry point is invoked with a 16-bit CALL FAR instruction,
7780   so when accessing stack contents, the 16-bit real mode code must account for the 16-bit segment
7781   and 16-bit offset of the return address that were pushed onto the stack. The 16-bit real mode entry
7782   point must exit with a RETF instruction. The register state is captured into RealModeState immediately
7783   after the RETF instruction is executed.
7784 
7785   If EFLAGS specifies interrupts enabled, or any of the 16-bit real mode code enables interrupts,
7786   or any of the 16-bit real mode code makes a SW interrupt, then the caller is responsible for making sure
7787   the IDT at address 0 is initialized to handle any HW or SW interrupts that may occur while in 16-bit real mode.
7788 
7789   If EFLAGS specifies interrupts enabled, or any of the 16-bit real mode code enables interrupts,
7790   then the caller is responsible for making sure the 8259 PIC is in a state compatible with 16-bit real mode.
7791   This includes the base vectors, the interrupt masks, and the edge/level trigger mode.
7792 
7793   If THUNK_ATTRIBUTE_BIG_REAL_MODE is set in the ThunkAttributes field of ThunkContext, then the user code
7794   is invoked in big real mode.  Otherwise, the user code is invoked in 16-bit real mode with 64KB segment limits.
7795 
7796   If neither THUNK_ATTRIBUTE_DISABLE_A20_MASK_INT_15 nor THUNK_ATTRIBUTE_DISABLE_A20_MASK_KBD_CTRL are set in
7797   ThunkAttributes, then it is assumed that the user code did not enable the A20 mask, and no attempt is made to
7798   disable the A20 mask.
7799 
7800   If THUNK_ATTRIBUTE_DISABLE_A20_MASK_INT_15 is set and THUNK_ATTRIBUTE_DISABLE_A20_MASK_KBD_CTRL is clear in
7801   ThunkAttributes, then attempt to use the INT 15 service to disable the A20 mask.  If this INT 15 call fails,
7802   then attempt to disable the A20 mask by directly accessing the 8042 keyboard controller I/O ports.
7803 
7804   If THUNK_ATTRIBUTE_DISABLE_A20_MASK_INT_15 is clear and THUNK_ATTRIBUTE_DISABLE_A20_MASK_KBD_CTRL is set in
7805   ThunkAttributes, then attempt to disable the A20 mask by directly accessing the 8042 keyboard controller I/O ports.
7806 
7807   If ThunkContext is NULL, then ASSERT().
7808   If AsmPrepareThunk16() was not previously called with ThunkContext, then ASSERT().
7809   If both THUNK_ATTRIBUTE_DISABLE_A20_MASK_INT_15 and THUNK_ATTRIBUTE_DISABLE_A20_MASK_KBD_CTRL are set in
7810   ThunkAttributes, then ASSERT().
7811 
7812   This interface is limited to be used in either physical mode or virtual modes with paging enabled where the
7813   virtual to physical mappings for ThunkContext.RealModeBuffer are mapped 1:1.
7814 
7815   @param  ThunkContext  A pointer to the context structure that describes the
7816                         16-bit real mode code to call.
7817 
7818 **/
7819 VOID
7820 EFIAPI
7821 AsmThunk16 (
7822   IN OUT  THUNK_CONTEXT  *ThunkContext
7823   );
7824 
7825 /**
7826   Prepares all structures and code for a 16-bit real mode thunk, transfers
7827   control to a 16-bit real mode entry point, and returns the results.
7828 
7829   Prepares all structures and code for a 16-bit real mode thunk, transfers
7830   control to a 16-bit real mode entry point, and returns the results. If the
7831   caller only need to perform a single 16-bit real mode thunk, then this
7832   service should be used. If the caller intends to make more than one 16-bit
7833   real mode thunk, then it is more efficient if AsmPrepareThunk16() is called
7834   once and AsmThunk16() can be called for each 16-bit real mode thunk.
7835 
7836   This interface is limited to be used in either physical mode or virtual modes with paging enabled where the
7837   virtual to physical mappings for ThunkContext.RealModeBuffer is mapped 1:1.
7838 
7839   See AsmPrepareThunk16() and AsmThunk16() for the detailed description and ASSERT() conditions.
7840 
7841   @param  ThunkContext  A pointer to the context structure that describes the
7842                         16-bit real mode code to call.
7843 
7844 **/
7845 VOID
7846 EFIAPI
7847 AsmPrepareAndThunk16 (
7848   IN OUT  THUNK_CONTEXT  *ThunkContext
7849   );
7850 
7851 /**
7852   Generates a 16-bit random number through RDRAND instruction.
7853 
7854   if Rand is NULL, then ASSERT().
7855 
7856   @param[out]  Rand     Buffer pointer to store the random result.
7857 
7858   @retval TRUE          RDRAND call was successful.
7859   @retval FALSE         Failed attempts to call RDRAND.
7860 
7861  **/
7862 BOOLEAN
7863 EFIAPI
7864 AsmRdRand16 (
7865   OUT     UINT16  *Rand
7866   );
7867 
7868 /**
7869   Generates a 32-bit random number through RDRAND instruction.
7870 
7871   if Rand is NULL, then ASSERT().
7872 
7873   @param[out]  Rand     Buffer pointer to store the random result.
7874 
7875   @retval TRUE          RDRAND call was successful.
7876   @retval FALSE         Failed attempts to call RDRAND.
7877 
7878 **/
7879 BOOLEAN
7880 EFIAPI
7881 AsmRdRand32 (
7882   OUT     UINT32  *Rand
7883   );
7884 
7885 /**
7886   Generates a 64-bit random number through RDRAND instruction.
7887 
7888   if Rand is NULL, then ASSERT().
7889 
7890   @param[out]  Rand     Buffer pointer to store the random result.
7891 
7892   @retval TRUE          RDRAND call was successful.
7893   @retval FALSE         Failed attempts to call RDRAND.
7894 
7895 **/
7896 BOOLEAN
7897 EFIAPI
7898 AsmRdRand64  (
7899   OUT     UINT64  *Rand
7900   );
7901 
7902 /**
7903   Load given selector into TR register.
7904 
7905   @param[in] Selector     Task segment selector
7906 **/
7907 VOID
7908 EFIAPI
7909 AsmWriteTr (
7910   IN UINT16  Selector
7911   );
7912 
7913 /**
7914   Performs a serializing operation on all load-from-memory instructions that
7915   were issued prior the AsmLfence function.
7916 
7917   Executes a LFENCE instruction. This function is only available on IA-32 and x64.
7918 
7919 **/
7920 VOID
7921 EFIAPI
7922 AsmLfence (
7923   VOID
7924   );
7925 
7926 /**
7927   Executes a XGETBV instruction
7928 
7929   Executes a XGETBV instruction. This function is only available on IA-32 and
7930   x64.
7931 
7932   @param[in] Index        Extended control register index
7933 
7934   @return                 The current value of the extended control register
7935 **/
7936 UINT64
7937 EFIAPI
7938 AsmXGetBv (
7939   IN UINT32  Index
7940   );
7941 
7942 /**
7943   Executes a XSETBV instruction to write a 64-bit value to a Extended Control
7944   Register(XCR), and returns the value.
7945 
7946   Writes the 64-bit value specified by Value to the XCR specified by Index. The
7947   64-bit value written to the XCR is returned. No parameter checking is
7948   performed on Index or Value, and some of these may cause CPU exceptions. The
7949   caller must either guarantee that Index and Value are valid, or the caller
7950   must establish proper exception handlers. This function is only available on
7951   IA-32 and x64.
7952 
7953   @param  Index The 32-bit XCR index to write.
7954   @param  Value The 64-bit value to write to the XCR.
7955 
7956   @return Value
7957 
7958 **/
7959 UINT64
7960 EFIAPI
7961 AsmXSetBv (
7962   IN UINT32  Index,
7963   IN UINT64  Value
7964   );
7965 
7966 /**
7967   Executes a VMGEXIT instruction (VMMCALL with a REP prefix)
7968 
7969   Executes a VMGEXIT instruction. This function is only available on IA-32 and
7970   x64.
7971 
7972 **/
7973 VOID
7974 EFIAPI
7975 AsmVmgExit (
7976   VOID
7977   );
7978 
7979 ///
7980 /// The structure used to supply and return data to and from the SVSM.
7981 ///
7982 typedef struct {
7983   VOID      *Caa;
7984   UINT64    RaxIn;
7985   UINT64    RcxIn;
7986   UINT64    RdxIn;
7987   UINT64    R8In;
7988   UINT64    R9In;
7989   UINT64    RaxOut;
7990   UINT64    RcxOut;
7991   UINT64    RdxOut;
7992   UINT64    R8Out;
7993   UINT64    R9Out;
7994   UINT8     *CallPending;
7995 } SVSM_CALL_DATA;
7996 
7997 /**
7998   Executes a VMGEXIT instruction (VMMCALL with a REP prefix) with arguments
7999   and return code
8000 
8001   Executes a VMGEXIT instruction placing the specified arguments in the
8002   corresponding registers before invocation. Upon return an XCHG is done to
8003   atomically clear and retrieve the SVSM call pending value. The returned RAX
8004   register value becomes the function return code. This function is intended
8005   for use with an SVSM. This function is only available on IA-32 and x64.
8006 
8007   @param[in,out]  SvsmCallPending  Pointer to the location of the SVSM call data
8008 
8009   @return                          Value of the RAX register on return
8010 
8011 **/
8012 UINT32
8013 EFIAPI
8014 AsmVmgExitSvsm (
8015   IN OUT SVSM_CALL_DATA  *SvsmCallData
8016   );
8017 
8018 /**
8019   Patch the immediate operand of an IA32 or X64 instruction such that the byte,
8020   word, dword or qword operand is encoded at the end of the instruction's
8021   binary representation.
8022 
8023   This function should be used to update object code that was compiled with
8024   NASM from assembly source code. Example:
8025 
8026   NASM source code:
8027 
8028         mov     eax, strict dword 0 ; the imm32 zero operand will be patched
8029     ASM_PFX(gPatchCr3):
8030         mov     cr3, eax
8031 
8032   C source code:
8033 
8034     X86_ASSEMBLY_PATCH_LABEL gPatchCr3;
8035     PatchInstructionX86 (gPatchCr3, AsmReadCr3 (), 4);
8036 
8037   @param[out] InstructionEnd  Pointer right past the instruction to patch. The
8038                               immediate operand to patch is expected to
8039                               comprise the trailing bytes of the instruction.
8040                               If InstructionEnd is closer to address 0 than
8041                               ValueSize permits, then ASSERT().
8042 
8043   @param[in] PatchValue       The constant to write to the immediate operand.
8044                               The caller is responsible for ensuring that
8045                               PatchValue can be represented in the byte, word,
8046                               dword or qword operand (as indicated through
8047                               ValueSize); otherwise ASSERT().
8048 
8049   @param[in] ValueSize        The size of the operand in bytes; must be 1, 2,
8050                               4, or 8. ASSERT() otherwise.
8051 **/
8052 VOID
8053 EFIAPI
8054 PatchInstructionX86 (
8055   OUT X86_ASSEMBLY_PATCH_LABEL  *InstructionEnd,
8056   IN  UINT64                    PatchValue,
8057   IN  UINTN                     ValueSize
8058   );
8059 
8060 #endif // defined (MDE_CPU_IA32) || defined (MDE_CPU_X64)
8061 #endif // !defined (__BASE_LIB__)
8062