summaryrefslogtreecommitdiff
path: root/Include/AmiLib.h
blob: 82ce5973e397dac7afbfa1850a460f7b86478719 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
//**********************************************************************
//**********************************************************************
//**                                                                  **
//**        (C)Copyright 1985-2010, American Megatrends, Inc.         **
//**                                                                  **
//**                       All Rights Reserved.                       **
//**                                                                  **
//**      5555 Oakbrook Parkway, Suite 200, Norcross, GA 30093        **
//**                                                                  **
//**                       Phone: (770)-246-8600                      **
//**                                                                  **
//**********************************************************************
//**********************************************************************

//**********************************************************************
// $Header: /Alaska/BIN/Core/Include/AmiLib.h 45    6/12/12 3:22p Oleksiyy $
//
// $Revision: 45 $
//
// $Date: 6/12/12 3:22p $
//**********************************************************************
// Revision History
// ----------------
// $Log: /Alaska/BIN/Core/Include/AmiLib.h $
// 
// 45    6/12/12 3:22p Oleksiyy
// TAG]  		EIP90322
// [Category]  	Improvement
// [Description] 	Extern declaradion of gAmiGlobalVariableGuid moved to
// AmiLib.h.
// [Files]  		AmiLib.h, Misc.c, EfiLib.c, AcpiCore.c and S3Resume.c
// 
// 44    5/22/12 4:13p Oleksiyy
// [TAG]  		EIP90322
// [Category]  	Improvement
// [Description]  	Defenition of AMI Global Variable Guid is added.
// [Files]  		AmiLib.h
// 
// 43    10/17/11 2:37p Yakovlevs
// [TAG]  		EIP71694 
// [Category]  	Bug Fix
// [Symptom]  	Option ROM is corrupted when copied from device on Rosecity
// Core 4.6.5.1.
// [RootCause]  	MemCopy routine was updated to use 64 bit access but PCI
// BAR
// Copy cannot handle this.
// 
// [Solution]  	Introduced MemCpy32 routine to handle fast copy 4 byte at
// a time.
// 
// 
// [Files]  		MemCpy32.asm; AmiLib.h
// 
// 42    3/09/11 5:37p Artems
// PI 1.1 clean-up, removed duplicated and unused definitions
// 
// 41    2/05/11 1:55p Artems
// Added Intel's style LinkedList support
// 
// 40    11/10/10 5:07p Felixp
// Enhancement: MMIO_ORX, MMIO_ANDX, MMIO_ANDORX macros are added(EIP
// 46932).
// 
// 39    8/20/10 12:39p Felixp
// LZMA compression support
// 
// 38    11/05/09 3:51p Oleksiyy
// EIP 27821: Support for 64 bit operations in IoRead and IoWrite added. 
// To use this functions AmiLib.h,  IA32CLib.c, x64AsmLib.asm files should
// be updated also.
// 
// 37    7/30/09 4:43p Vyacheslava
// Fixed comments.
// 
// 36    7/28/09 4:54p Vyacheslava
// Minor bug fix. EIP#24453: Synopsis: CPU exception when printing long
// debug messages.
// 
// 35    5/21/09 6:06p Felixp
// Bit definitions (BITXX macros) are enclosed in #ifdef's to avoid
// potential double definitions.
// 
// 34    11/20/08 3:00p Felixp
// ReallocatePeImage function is upadted to properly handle virtual
// address fixups of the runtime images.
// 
// 33    5/07/08 12:21p Felixp
// Performance API added
// 
// 32    4/17/08 3:17p Markw
// Added Read/Write Cr3 and Identity map x64 page tables.
// 
// 31    3/19/08 2:24p Markw
// Add declaration GetPowerOfTwo64.
// 
// 30    9/05/07 12:21p Felixp
// GetDebugDirectory routine declarations removed (it's a private routine
// used only by PELoader).
// 
// 29    8/02/07 1:29a Felixp
// Always define GetName (used to be only when EFI_DEBUG is defined). This
// is needed to support selective debugging.
// 
// 28    7/17/07 4:57p Yakovlevs
// 
// 27    3/08/07 2:55a Felixp
// AMI specific status code definitions moved to AmiStatusCodes.h
// 
// 26    10/27/06 10:17a Felixp
// Status codes for exit boot services and set  virtual address map added
// 
// 25    10/13/06 2:53p Felixp
// AMI_STATUS_CODE_BEEP_CLASS added
// 
// 24    10/12/06 9:36a Felixp
// SectionSize parameter of FindPeSection changed from UINT32 to UINTN
// 
// 23    8/24/06 9:09a Felixp
//  - x64 support
//  - memory mapped I/O macros added
// 
// 22    6/28/06 1:01p Felixp
// 
// 21    5/21/06 11:15p Felixp
// PEI_RECOVERY_FAILED error code added
// 
// 20    5/19/06 6:19p Felixp
// Strcpy, Wcscpy changed to return pointer to the end of the string
// 
// 19    4/30/06 9:40p Felixp
// 
// 18    4/03/06 5:04p Felixp
// Headers updated in accordance with new PCI and Super I/O infrastructure
// 
// 17    3/13/06 1:15a Felixp
// 
// 16    6/07/05 7:21p Felixp
// additional macroses to work with AMI Status Code
// 
// 15    5/31/05 10:23a Felixp
// 
// 14    5/27/05 5:28p Felixp
// 
// 12    4/02/05 2:23p Felixp
// Swprintf function added
// 
// 11    4/01/05 5:51p Felixp
// CONVERT_TO_WSTRING macros added
// 
// 10    4/01/05 3:50p Felixp
// CONVERT_TO_STRING macros added
// 
// 9     3/25/05 5:25p Felixp
// Wcstol funciton added
// 
// 8     3/23/05 4:27p Markw
// 
// 7     3/23/05 4:13p Felixp
// 
// 6     3/23/05 4:12p Felixp
// 
// 5     3/21/05 11:46a Felixp
// Bit definitions moved from AmiDxeLib.h to AmiLib.h
// 
// 4     3/07/05 6:10p Felixp
// FindPeSection added
// 
// 3     2/01/05 2:27a Felixp
// 
// 2     2/01/05 2:00a Felixp
// I/O routine declarations moved from AmiDxeLib.h to AmiLib.h
// 
// 1     1/28/05 12:44p Felixp
// 
// 5     1/18/05 3:21p Felixp
// PrintDebugMessage renamed to Trace
// 
// 2     1/14/05 1:14p Felixp
// FindNextHobByGuid added
// 
// 1     12/23/04 9:41a Felixp
// 
// 27    12/15/04 9:06p Felixp
// LegacyBoot application added
// Minor build process improvements
// 
// 26    12/15/04 3:51p Markw
// Added Strstr.
// 
// 25    11/24/04 9:36a Felixp
// int64 suppot added to Itoa and Itow
// 
// 24    11/19/04 1:33a Felixp
// GetName debug routine added
// 
// 23    11/18/04 11:55a Markw
// 
// 22    11/18/04 11:51a Felixp
// FindNextHobByType declaration added
// 
// 21    11/10/04 5:17p Felixp
// 1. Definitions for StdC functions added
// 2. Definition for NameAndEntryStr added
// 
// 20    10/22/04 7:35p Felixp
// 
// 19    10/14/04 12:04p Felixp
// 
// 18    8/22/04 11:57p Felixp
// 
// 17    8/18/04 3:16p Felixp
// 
// 16    7/13/04 10:42a Felixp
// 
// 15    4/11/04 2:49p Felixp
// 
// 14    4/10/04 1:07p Felixp
// 
// 13    4/08/04 6:54p Yakovlevs
// 
// 12    4/04/04 2:58p Felixp
// 
// 11    3/30/04 11:16a Felixp
// 
// 10    3/30/04 2:48a Felixp
// 
// 9     3/29/04 6:06p Felixp
// 
// 7     3/29/04 5:05p Felixp
// 
// 6     3/29/04 4:36p Felixp
// 
// 5     3/28/04 2:11p Felixp
// 1. PE Loader and some other commonly used code moved to the Library
// 2. Warnings fixed (from now on warning will be treated as error)
// 
// 4     3/26/04 6:04p Markw
// Replaced printf with sprintf.
// 
// 3     3/26/04 3:47p Markw
// Added headers for stdlib.c.
// 
// 2     3/25/04 2:12p Markw
// Added Shr64.
// 
// 1     3/23/04 5:52p Felixp
// 
// 11    3/20/04 10:46a Felixp
// 
// 10    3/19/04 10:30a Markw
// Added GetEfiConfigurationTable.
// 
// 9     3/17/04 12:01p Markw
// Changed size_t UINTN.
// 
// 8     3/08/04 5:44p Markw
//      
// 
// 7     3/05/04 12:46p Markw
// Added Div64
// 
// 6     3/01/04 2:00p Markw
// removed FILE typedef
// 
// 5     2/26/04 3:23p Felixp
// 
// 4     2/10/04 12:54p Markw
// Fixed return type on IoWrite16 and added definition memcmp and guidcmp
// 
// 3     2/05/04 3:45p Markw
// Fixed parameter type error on IoWrite16.
// 
// 2     1/28/04 12:11p Robert
// 
// 1     1/22/04 3:15p Markw
// 
//**********************************************************************
#ifndef __AMI_LIBRARY__H__
#define __AMI_LIBRARY__H__
#ifdef __cplusplus
extern "C" {
#endif

#include <EFI.h>
#include <AmiStatusCodes.h>

//****************************************************************************/
//		TYPE DECLARATIONS
//****************************************************************************/
typedef CHAR8   *va_list;

// PE Loader                                                                */
#pragma pack(push)
#pragma pack(2)

typedef struct _FILE_HEADER { // File Header
	UINT16 Machine;
	UINT16 NumberOfSections;
	UINT32 Dummy[3];
	UINT16 OptionalHeaderSize;
	UINT16 Characteristics;
} FILE_HEADER;

typedef struct _DIRECTORY_ENTRY{ // Directory entry
	UINT32 VirtualAddress;
	UINT32 Size;
} DIRECTORY_ENTRY;

typedef struct _OPTIONAL_HEADER { // Optional header
	UINT16 Magic;
	UINT8 MajorLinkerVersion;
	UINT8 MinorLinkerVersion;
	UINT32 SizeOfCode;
	UINT32 SizeOfInitializedData;
	UINT32 SizeOfUninitializedData;
	UINT32 AddressOfEntryPoint;
	UINT32 BaseOfCode;
#if !defined(EFI64) && !defined(EFIx64)
	UINT32 BaseOfData;
#endif
	UINTN  ImageBase;
	UINT32 SectionAlignment;
	UINT32 FileAlignment;
	UINT16 MajorOperatingSystemVersion;
	UINT16 MinorOperatingSystemVersion;
	UINT16 MajorImageVersion;
	UINT16 MinorImageVersion;
	UINT16 MajorSubsystemVersion;
	UINT16 MinorSubsystemVersion;
	UINT32 Reserved;
	UINT32 SizeOfImage;
	UINT32 SizeOfHeaders;
	UINT32 CheckSum;
	UINT16 Subsystem;
	UINT16 Characteristics;
	UINTN SizeOfStackReserve;
	UINTN SizeOfStackCommit;
	UINTN SizeOfHeapReserve;
	UINTN SizeOfHeapCommit;
	UINT32 LoaderFlags;
	UINT32 NumberOfRvaAndSizes;
	DIRECTORY_ENTRY Export, Import, Resource, Exception,
				 	Certificate, Reallocations, Debug;
} OPTIONAL_HEADER;

typedef struct _DEBUG_DIRECTORY{
	UINT32 Characteristics;	//A reserved field intended to be used for flags, set to zero for now.
	UINT32 TimeDateStamp; //Time and date the debug data was created.
	UINT16 MajorVersion; //Major version number of the debug data format.
	UINT16 MinorVersion; //Minor version number of the debug data format.
	UINT32 Type; //Format of debugging information: this field enables support of multiple debuggers. See Section 6.1.2, Debug Type, for more information.
	UINT32 SizeOfData; //Size of the debug data (not including the debug directory itself).
	//TODO: Is it UINT32 for IPF ?????
	UINT32 AddressOfRawData; //Address of the debug data when loaded, relative to the image base.
	UINT32 PointerToRawData; //File pointer to the debug data.
} DEBUG_DIRECTORY;
#pragma pack(pop)

typedef enum {
	CpuIoWidthUint8,
	CpuIoWidthUint16,
	CpuIoWidthUint32,
	CpuIoWidthUint64,
	CpuIoWidthFifoUint8,
	CpuIoWidthFifoUint16,
	CpuIoWidthFifoUint32,
	CpuIoWidthFifoUint64,
	CpuIoWidthFillUint8,
	CpuIoWidthFillUint16,
	CpuIoWidthFillUint32,
	CpuIoWidthFillUint64
} CPU_IO_WIDTH;

//Decompression routine types
typedef EFI_STATUS (*GET_INFO) (
	IN VOID *Source, IN UINT32 SourceSize,
	OUT UINT32 *DestinationSize, OUT UINT32 *ScratchSize
);

typedef EFI_STATUS (*DECOMPRESS) (
	IN VOID* Source, IN UINT32 SourceSize,
	IN OUT VOID* Destination, IN UINT32 DestinationSize,
	IN OUT VOID* Scratch, IN UINT32 ScratchSize
);
// Variable Services
#define AMI_GLOBAL_VARIABLE_GUID \
    {0x1368881,0xc4ad,0x4b1d,0xb6,0x31,0xd5,0x7a,0x8e,0xc8,0xdb,0x6b}

extern EFI_GUID gAmiGlobalVariableGuid;
//****************************************************************************/
//		MACOROS DECLARATIONS
//****************************************************************************/
//Some BIT values definition for the sake of convenience
#ifndef BIT0
#define 	BIT0                0x01
#endif
#ifndef BIT1
#define 	BIT1                0x02
#endif
#ifndef BIT2
#define 	BIT2                0x04
#endif
#ifndef BIT3
#define 	BIT3                0x08
#endif
#ifndef BIT4
#define 	BIT4                0x10
#endif
#ifndef BIT5
#define 	BIT5                0x20
#endif
#ifndef BIT6
#define 	BIT6                0x40
#endif
#ifndef BIT7
#define 	BIT7                0x80
#endif
#ifndef BIT8
#define 	BIT8               0x100
#endif
#ifndef BIT9
#define 	BIT9    0x200
#endif
#ifndef BIT00
#define 	BIT00	0x00000001
#endif
#ifndef BIT01
#define 	BIT01	0x00000002
#endif
#ifndef BIT02
#define 	BIT02	0x00000004
#endif
#ifndef BIT03
#define 	BIT03	0x00000008
#endif
#ifndef BIT04
#define 	BIT04	0x00000010
#endif
#ifndef BIT05
#define 	BIT05	0x00000020
#endif
#ifndef BIT06
#define 	BIT06	0x00000040
#endif
#ifndef BIT07
#define 	BIT07	0x00000080
#endif
#ifndef BIT08
#define 	BIT08	0x00000100
#endif
#ifndef BIT09
#define 	BIT09	0x00000200
#endif
#ifndef BIT10
#define 	BIT10	0x00000400
#endif
#ifndef BIT11
#define 	BIT11	0x00000800
#endif
#ifndef BIT12
#define 	BIT12	0x00001000
#endif
#ifndef BIT13
#define 	BIT13	0x00002000
#endif
#ifndef BIT14
#define 	BIT14	0x00004000
#endif
#ifndef BIT15
#define 	BIT15	0x00008000
#endif
#ifndef BIT16
#define 	BIT16	0x00010000
#endif
#ifndef BIT17
#define 	BIT17	0x00020000
#endif
#ifndef BIT18
#define 	BIT18	0x00040000
#endif
#ifndef BIT19
#define 	BIT19	0x00080000
#endif
#ifndef BIT20
#define 	BIT20	0x00100000
#endif
#ifndef BIT21
#define 	BIT21	0x00200000
#endif
#ifndef BIT22
#define 	BIT22	0x00400000
#endif
#ifndef BIT23
#define 	BIT23	0x00800000
#endif
#ifndef BIT24
#define 	BIT24	0x01000000
#endif
#ifndef BIT25
#define 	BIT25	0x02000000
#endif
#ifndef BIT26
#define 	BIT26	0x04000000
#endif
#ifndef BIT27
#define 	BIT27	0x08000000
#endif
#ifndef BIT28
#define 	BIT28	0x10000000
#endif
#ifndef BIT29
#define 	BIT29	0x20000000
#endif
#ifndef BIT30
#define 	BIT30	0x40000000
#endif
#ifndef BIT31
#define 	BIT31	0x80000000
#endif
//=============================================================================
// This is all ANSI Standard macros pulled from stdarg.h
// Support for Variable arguments in strings
#ifndef _INTSIZEOF
#define _INTSIZEOF(n)( (sizeof(n) + sizeof(UINTN) - 1) & ~(sizeof(UINTN) - 1) )
#endif 
// Also support coding convention rules for var arg macros
#ifndef va_start
#define va_start(ap,v)  ( ap = (va_list)&(v) + _INTSIZEOF(v) )
#endif
#define va_arg(ap,t)    ( *(t *)((ap += _INTSIZEOF(t)) - _INTSIZEOF(t)) )
#define va_end(ap)      ( ap = (va_list)0 )
// end variable argument support
//=============================================================================
#define ___INTERNAL_CONVERT_TO_STRING___(a) #a
#define CONVERT_TO_STRING(a) ___INTERNAL_CONVERT_TO_STRING___(a)
#define ___INTERNAL_CONVERT_TO_WSTRING___(a) L#a
#define CONVERT_TO_WSTRING(a) ___INTERNAL_CONVERT_TO_WSTRING___(a)

#define guidcmp(g1,g2) MemCmp(g1,g2,sizeof(EFI_GUID))

// The EFI memory allocation functions work in units of EFI_PAGEs that are
// 4K. This should in no way be confused with the page size of the processor.
// An EFI_PAGE is just the quanta of memory in EFI.
#define EFI_PAGE_MASK   (EFI_PAGE_SIZE - 1)
#define EFI_PAGE_SHIFT  12

#define EFI_SIZE_TO_PAGES(s)  \
    ( (((UINTN)(s)) >> EFI_PAGE_SHIFT) + ((((UINTN)(s)) & EFI_PAGE_MASK) ? 1 : 0) )

#define EFI_PAGES_TO_SIZE(p)   ( ((UINTN)(p)) << EFI_PAGE_SHIFT)
//FIELD_OFFSET - returns the byte offset to a field within a structure
#define EFI_FIELD_OFFSET(TYPE,Field) ((UINTN)(&(((TYPE *) 0)->Field)))

//returns the field size within a structure
#define EFI_FIELD_SIZE(TYPE,Field) (sizeof((((TYPE *) 0)->Field)))
#define OUTTER(pField, Field, OutterType) ((OutterType*)((UINT8*)(pField)-EFI_FIELD_OFFSET(OutterType,Field)))

#define Itoa(value,string,radix) ItoaEx((INT32)value,string,radix,FALSE)
#define Itow(value,string,radix) ItowEx((INT32)value,string,radix,FALSE)
#define I64toa(value,string,radix) ItoaEx(value,string,radix,TRUE)
#define I64tow(value,string,radix) ItowEx(value,string,radix,TRUE)

#define Strtol(nptr,endptr,base) StrtolEx(nptr,endptr,base,1)
#define Wcstol(nptr,endptr,base) StrtolEx((char*)nptr,(char**)endptr,base,2)

//Debug Message Categories
#define TRACE_NEVER 0 // never displayed
#define TRACE_ALWAYS -1 // always displayed
#define TRACE_PEICORE 1
#define TRACE_DXEIPL 2
#define TRACE_PEI_CHIPSET 4
#define TRACE_PEI_PLATFORM 8
#define TRACE_PEI_CPU 0x10
#define TRACE_DXE_CORE 0x20
#define TRACE_DXE_CHIPSET 0x40
#define TRACE_DXE_PLATFORM 0x80
#define TRACE_DXE_CPU 0x100
#define TRACE_CSM 0x200
#define TRACE_USB 0x400

#define EFI_DEADLOOP() { volatile UINTN __DeadLoopVar__ = 1; while (__DeadLoopVar__); }

#define MMIO_READ8(Address) (*(volatile UINT8*)(Address))
#define MMIO_WRITE8(Address,Value) (*(volatile UINT8*)(Address)=(Value))
#define MMIO_READ16(Address) (*(volatile UINT16*)(Address))
#define MMIO_WRITE16(Address,Value) (*(volatile UINT16*)(Address)=(Value))
#define MMIO_READ32(Address) (*(volatile UINT32*)(Address))
#define MMIO_WRITE32(Address,Value) (*(volatile UINT32*)(Address)=(Value))
#define MMIO_READ64(Address) (*(volatile UINT64*)(Address))
#define MMIO_WRITE64(Address,Value) (*(volatile UINT64*)(Address)=(Value))

#define MMIO_OR8(Address, Value)  (*(volatile UINT8*)(Address) |= (Value))
#define MMIO_OR16(Address, Value) (*(volatile UINT16*)(Address) |= (Value))
#define MMIO_OR32(Address, Value) (*(volatile UINT32*)(Address) |= (Value))
#define MMIO_OR64(Address, Value) (*(volatile UINT64*)(Address) |= (Value))

#define MMIO_AND8(Address, Value)  (*(volatile UINT8*)(Address) &= (Value))
#define MMIO_AND16(Address, Value) (*(volatile UINT16*)(Address) &= (Value))
#define MMIO_AND32(Address, Value) (*(volatile UINT32*)(Address) &= (Value))
#define MMIO_AND64(Address, Value) (*(volatile UINT64*)(Address) &= (Value))

#define MMIO_ANDOR8(Address, AndData, OrData)   (*(volatile UINT8*)(Address))  = (*(volatile UINT8*)(Address)  & (AndData) | (OrData))
#define MMIO_ANDOR16(Address, AndData, OrData)  (*(volatile UINT16*)(Address)) = (*(volatile UINT16*)(Address) & (AndData) | (OrData))
#define MMIO_ANDOR32(Address, AndData, OrData)  (*(volatile UINT32*)(Address)) = (*(volatile UINT32*)(Address) & (AndData) | (OrData))
#define MMIO_ANDOR64(Address, AndData, OrData)  (*(volatile UINT64*)(Address)) = (*(volatile UINT32*)(Address) & (AndData) | (OrData))

#define DXE_TOK L"DXE"
#define SHELL_TOK L"SHELL"
#define PEI_TOK L"PEI"
#define BDS_TOK L"BDS"
#define DRIVERBINDING_START_TOK L"DriverBinding:Start"
#define DRIVERBINDING_SUPPORT_TOK L"DriverBinding:Support"
#define START_IMAGE_TOK L"StartImage"
#define LOAD_IMAGE_TOK L"LoadImage"
#define DXEIPL_TOK L"DXEIPL"
#define PCI_TOK L"PCI"
#define CSM_TOK L"CSM"
#define NVRAM_TOK L"NVRAM"
#define S3RESUME_TOK L"S3RESUME"

//****************************************************************************/
//		VARIABLE DECLARATIONS
//****************************************************************************/

//****************************************************************************/
//		FUNCTION DECLARATIONS
//****************************************************************************/
UINTN Sprintf_va_list( char *buffer, char *format, va_list arg );
UINTN Sprintf(char *Buffer, char *Format, ...);
UINTN Sprintf_s(char *Buffer, UINTN BufferSize, char *Format, ...);
UINTN Sprintf_s_va_list(char *Buffer, UINTN BufferSize, char *Format, va_list Marker);
UINTN Swprintf_va_list(CHAR16 *buffer, CHAR16 *format, va_list arg);
UINTN Swprintf(CHAR16 *Buffer, CHAR16 *Format, ...);
UINTN Swprintf_s(CHAR16 *Buffer, UINTN BufferSize, CHAR16 *Format, ...);
UINTN Swprintf_s_va_list(CHAR16 *Buffer, UINTN BufferSize, CHAR16 *Format, va_list Marker);
int Atoi( char *string );
char * ItoaEx(INT64 value, char *string,int radix, BOOLEAN is_int64);
CHAR16 * ItowEx(INT64 value, CHAR16 *string,int radix, BOOLEAN is_int64);
long StrtolEx(char *nptr,char **endptr,int base, int increment);
int Strcmp(char *string1, char *string2);
int Wcscmp(CHAR16 *string1, CHAR16 *string2);
UINTN Strlen(char *);
UINTN Wcslen(CHAR16 *);
char* Strcpy(char *,char *);
CHAR16* Wcscpy(CHAR16 *,CHAR16 *);
char *Strstr(const char *,const char *);
char * Strupr(char *str);
CHAR16 * Wcsupr(CHAR16 *str);

/*
int tolower(int);
int toupper(int);
char * strupr(char *str);
char *strcpy(char *,char *);
char *strcat(char *,char *);
CHAR16 *wcscat(CHAR16 *,CHAR16 *);
*/

VOID MemCpy(VOID* pDestination, VOID* pSource, UINTN Length);
VOID MemCpy32(VOID* pDestination, VOID* pSource, UINTN Length);
void MemSet(VOID* pBuffer, UINTN Size, UINT8 Value);
INTN MemCmp(VOID* pDestination, VOID* pSource, UINTN Length);

UINT64 Shr64( IN UINT64 Value, IN UINT8 Shift );
UINT64 Shl64( IN UINT64 Value, IN UINT8 Shift );

UINT64 Div64 (
	IN UINT64	Dividend,
	IN UINTN	Divisor,	//Can only be 31 bits for IA-32
  	OUT UINTN	*Remainder OPTIONAL
);

UINT64 Mul64(
	IN UINT64   Value64,
	IN UINTN	Value32
);

UINT64 GetPowerOfTwo64(
  IN  UINT64  Input
);


// PE Loader
BOOLEAN ReallocatePeImage(UINT8 *pcurrent, UINT8 *pnew, UINT8 *prealloc);
BOOLEAN IsPeImageValid(UINT8* pstart);
UINT32 GetImageSize(VOID *p);
OPTIONAL_HEADER* GetPeOptionalHeader(VOID *p);
VOID* LoadPeImage(UINT8* pold, UINT8* pnew);
VOID* LoadPeImageEx(UINT8* pold, UINT8* pnew, UINT8 *prealloc);
VOID* FindPeSection(UINT8* pstart, UINT64 name, UINTN* pSize);
BOOLEAN GetName(UINT8 *p, CHAR8 *sName);

//Decompression
// Standard UEFI Decompression
EFI_STATUS GetInfo (
	IN VOID *Source, IN UINT32 SourceSize,
	OUT UINT32 *DestinationSize, OUT UINT32 *ScratchSize
);

EFI_STATUS Decompress (
	IN VOID* Source, IN UINT32 SourceSize,
	IN OUT VOID* Destination, IN UINT32 DestinationSize,
	IN OUT VOID* Scratch, IN UINT32 ScratchSize
);

// LZMA Decompression
EFI_STATUS LzmaGetInfo (
	IN VOID *Source, IN UINT32 SourceSize,
	OUT UINT32 *DestinationSize, OUT UINT32 *ScratchSize
);

EFI_STATUS LzmaDecompress (
	IN VOID* Source, IN UINT32 SourceSize,
	IN OUT VOID* Destination, IN UINT32 DestinationSize,
	IN OUT VOID* Scratch, IN UINT32 ScratchSize
);

//Defined in Tokens.c
BOOLEAN GetDecompressInterface(
    UINT8 CompressionType, GET_INFO *GetInfoPtr, DECOMPRESS *DecompressPtr
);

// DEBUG
VOID checkpoint(UINT8 c);
UINT64 GetCpuTimer();
VOID PrepareStatusCodeString(VOID* pBuffer, UINTN Size, CHAR8 *sFormat, va_list ArgList);

//HOB
EFI_STATUS FindNextHobByType(IN UINT16   Type,IN OUT VOID **Hob);
EFI_STATUS FindNextHobByGuid(IN EFI_GUID *Guid, IN OUT VOID **Hob);

UINT8 IoRead8(UINT16 Port);
VOID IoWrite8(UINT16 Port, UINT8 Value);
UINT16 IoRead16(UINT16 Port);
VOID IoWrite16(UINT16 Port, UINT16 Value);
UINT32 IoRead32(UINT16 Port);
VOID IoWrite32(UINT16 Port, UINT32 Value);
UINT64 IoRead64(UINT16 Port);
VOID IoWrite64(UINT16 Port, UINT64 Value);

EFI_STATUS IoRead(
	IN CPU_IO_WIDTH	Width,
	IN UINT64		Address,
	IN UINTN		Count,
	IN VOID			*Buffer
);

EFI_STATUS IoWrite(
	IN CPU_IO_WIDTH	Width,
	IN UINT64		Address,
	IN UINTN		Count,
	OUT VOID		*Buffer
);

EFI_STATUS IoReadWrite(
	IN CPU_IO_WIDTH	Width,
	IN UINT64		Address,
	IN VOID			*DataValue,
	IN VOID			*DataMask
);

EFI_STATUS MemRead(
	IN CPU_IO_WIDTH	Width,
	IN UINT64		Address,
	IN UINTN		Count,
	IN VOID			*Buffer
);

EFI_STATUS MemWrite(
	IN CPU_IO_WIDTH	Width,
	IN UINT64		Address,
	IN UINTN		Count,
	OUT VOID		*Buffer
);

EFI_STATUS MemReadWrite(
	IN CPU_IO_WIDTH	Width,
	IN UINT64		Address,
	IN VOID			*DataValue,
	IN VOID			*DataMask
);

typedef	struct _IO_DATA{
	UINT16 			Addr;
	CPU_IO_WIDTH	Width;	
	UINT32 			Data;
} IO_DATA;

//IO Init table execution function
EFI_STATUS ExecScript(IO_DATA *ScriptData, UINTN EntryCount);


UINTN ReadCr3(VOID);
VOID WriteCr3(UINTN Value);

UINT32 GetPageTableNumPages(
    IN UINT8 NumberMemoryBits
);

VOID FillPageTable(
    IN UINT8  NumberMemoryBits,
    IN VOID   *PageTable
);

//Linked List

//
// Prevent multiple includes in the same source file
//

#ifndef EFI_LIST_ENTRY_DEFINED
#define EFI_LIST_ENTRY_DEFINED
typedef struct _EFI_LIST_ENTRY {
  struct _EFI_LIST_ENTRY  *ForwardLink;
  struct _EFI_LIST_ENTRY  *BackLink;
} EFI_LIST_ENTRY;
//*** AMI PORTING BEGIN ***//
#endif
//*** AMI PORTING END *****//

typedef EFI_LIST_ENTRY EFI_LIST;      
typedef EFI_LIST_ENTRY EFI_LIST_NODE;

#define INITIALIZE_LIST_HEAD_VARIABLE(ListHead)  {&ListHead, &ListHead}

VOID
InitializeListHead (
  EFI_LIST_ENTRY       *List
  )
/*++

Routine Description:

  Initialize the head of the List.  The caller must allocate the memory 
  for the EFI_LIST. This function must be called before the other linked
  list macros can be used.
    
Arguments:

  List - Pointer to list head to initialize
   
Returns:

  None.

--*/
;

BOOLEAN
IsListEmpty (
  EFI_LIST_ENTRY  *List
  )
/*++

Routine Description:

  Return TRUE is the list contains zero nodes. Otherzise return FALSE.
  The list must have been initialized with InitializeListHead () before using 
  this function.
    
Arguments:

  List - Pointer to list head to test

   
Returns:

  Return TRUE is the list contains zero nodes. Otherzise return FALSE.

--*/
;

VOID
RemoveEntryList (
  EFI_LIST_ENTRY  *Entry
  )
/*++

Routine Description:

  Remove Node from the doubly linked list. It is the caller's responsibility
  to free any memory used by the entry if needed. The list must have been 
  initialized with InitializeListHead () before using this function.
    
Arguments:

  Entry - Element to remove from the list.
   
Returns:
  
  None

--*/
;

VOID
InsertTailList (
  EFI_LIST_ENTRY  *ListHead,
  EFI_LIST_ENTRY  *Entry
  )
/*++

Routine Description:

  Insert a Node into the end of a doubly linked list. The list must have 
  been initialized with InitializeListHead () before using this function.
    
Arguments:

  ListHead - Head of doubly linked list

  Entry    - Element to insert at the end of the list.
   
Returns:
  
  None

--*/
;

VOID
InsertHeadList (
  EFI_LIST_ENTRY  *ListHead,
  EFI_LIST_ENTRY  *Entry
  )
/*++

Routine Description:

  Insert a Node into the start of a doubly linked list. The list must have 
  been initialized with InitializeListHead () before using this function.
    
Arguments:

  ListHead - Head of doubly linked list

  Entry    - Element to insert to beginning of list
   
Returns:
  
  None

--*/
;

VOID
SwapListEntries (
  EFI_LIST_ENTRY  *Entry1,
  EFI_LIST_ENTRY  *Entry2
  )
/*++

Routine Description:

  Swap the location of the two elements of a doubly linked list. Node2 
  is placed in front of Node1. The list must have been initialized with 
  InitializeListHead () before using this function.
    
Arguments:

  Entry1 - Element in the doubly linked list in front of Node2. 

  Entry2 - Element in the doubly linked list behind Node1.
   
Returns:
  
  None

--*/
;

EFI_LIST_ENTRY *
GetFirstNode (
  EFI_LIST_ENTRY  *List 
  )
/*++

Routine Description:

  Return the first node pointed to by the list head.  The list must 
  have been initialized with InitializeListHead () before using this 
  function and must contain data.
    
Arguments:

  List - The head of the doubly linked list.
   
Returns:
  
  Pointer to the first node, if the list contains nodes.  The list will
  return a null value--that is, the value of List--when the list is empty.
    See the description of IsNull for more information.


--*/
;

EFI_LIST_ENTRY *
GetNextNode (
  EFI_LIST_ENTRY  *List,
  EFI_LIST_ENTRY  *Node
  )
/*++

Routine Description:

  Returns the node following Node in the list. The list must 
  have been initialized with InitializeListHead () before using this 
  function and must contain data.
    
Arguments:

  List - The head of the list.  MUST NOT be the literal value NULL.
  Node - The node in the list.  This value MUST NOT be the literal value NULL.
    See the description of IsNull for more information.
   
Returns:
  
  Pointer to the next node, if one exists.  Otherwise, returns a null value,
  which is actually a pointer to List.
    See the description of IsNull for more information.

--*/
;

BOOLEAN 
IsNull ( 
  EFI_LIST_ENTRY  *List,
  EFI_LIST_ENTRY  *Node 
  )
/*++

Routine Description:

  Determines whether the given node is null.  Note that Node is null
  when its value is equal to the value of List.  It is an error for
  Node to be the literal value NULL [(VOID*)0x0].

Arguments:

  List - The head of the list.  MUST NOT be the literal value NULL.
  Node - The node to test.  MUST NOT be the literal value NULL.  See
    the description above.
   
Returns:
  
  Returns true if the node is null.

--*/
;

BOOLEAN 
IsNodeAtEnd ( 
  EFI_LIST_ENTRY  *List, 
  EFI_LIST_ENTRY  *Node 
  )
/*++

Routine Description:

  Determines whether the given node is at the end of the list.  Used
  to walk the list.  The list must have been initialized with 
  InitializeListHead () before using this function and must contain 
  data.

Arguments:

  List - The head of the list.  MUST NOT be the literal value NULL.
  Node - The node to test.  MUST NOT be the literal value NULL.
    See the description of IsNull for more information.
   
Returns:
  
  Returns true if the list is the tail.

--*/
;

/****** DO NOT WRITE BELOW THIS LINE *******/
#ifdef __cplusplus
}
#endif
#endif

//**********************************************************************
//**********************************************************************
//**                                                                  **
//**        (C)Copyright 1985-2010, American Megatrends, Inc.         **
//**                                                                  **
//**                       All Rights Reserved.                       **
//**                                                                  **
//**      5555 Oakbrook Parkway, Suite 200, Norcross, GA 30093        **
//**                                                                  **
//**                       Phone: (770)-246-8600                      **
//**                                                                  **
//**********************************************************************
//**********************************************************************