summaryrefslogtreecommitdiff
path: root/Core/EM/Recovery/RecoveryCsp.h
blob: 846037124824622dc2963f2082cf1173fbd9d921 (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
//*************************************************************************
//*************************************************************************
//**                                                                     **
//**        (C)Copyright 1985-2008, American Megatrends, Inc.            **
//**                                                                     **
//**                       All Rights Reserved.                          **
//**                                                                     **
//**      5555 Oakbrook Parkway, Suite 200, Norcross, GA 30093           **
//**                                                                     **
//**                       Phone: (770)-246-8600                         **
//**                                                                     **
//*************************************************************************
//*************************************************************************

//*************************************************************************
// $Header: /Alaska/BIN/Core/Modules/Recovery/RecoveryCsp.h 5     8/09/12 11:12a Pats $
//
// $Revision: 5 $
//
// $Date: 8/09/12 11:12a $
//*************************************************************************
// Revision History
// ----------------
// $Log: /Alaska/BIN/Core/Modules/Recovery/RecoveryCsp.h $
// 
// 5     8/09/12 11:12a Pats
// [TAG] - EIP 80780
// [Category] - Function Request
// [Description] - Support for recovery from media formatted with the
// EXT(x) file system.
// [Files] Recovery.c, Recovery.mak, Recovery.sdl, RecoveryCsp.h (bin),
// and FsRecovery.c (source)
// 
// 4     1/25/12 2:47p Pats
// [TAG] - EIP 26909
// [Category] - Function Request
// [Description] - Support for recovery from media formatted with the NTFS
// file system. This of necessity includes detection of GPT partitions,
// which may contain any file system.
// [Files] Recovery.c, Recovery.mak, Recovery.sdl, RecoveryCsp.h (bin),
// and FsRecovery.c (source)
// 
// 3     5/13/11 4:44p Artems
// Replaced GetFileName infrastructure with GetRecoveryFileInfo for
// clarification
// 
// 2     2/05/11 2:46p Artems
// EIP 39463: Added support for secure update - GetFileName eLink
// 
// 1     6/11/10 11:45a Artems
// Definitions of layout for FAT and ISO9660 structures
// 
//*************************************************************************
//<AMI_FHDR_START>
//
// Name:  RecoveryCsp.h
//
//  Description:
//  File system recovery data types definition
//
//<AMI_FHDR_END>
//*************************************************************************
#ifndef __RECOVERY_CSP__H__
#define __RECOVERY_CSP__H__
#ifdef __cplusplus
extern "C" {
#endif

#include <PPI/DeviceRecoveryBlockIo.h>

#define FAT12 0
#define FAT16 1
#define FAT32 2
#define NTFS  3
#define EXTx  4

//
// Some NTFS definitions
//
#define     FILE_RECORD_SIZE            1024 // Size of an NTFS file record
#define     MAXIMUM_RECORD_NUMBER       0x0000FFFFFFFFFFFF // NTFS max record no.
#define     FR_ATTRIBUTE_NAME           0x00000030
#define     FR_ATTRIBUTE_VOLUME_NAME    0x00000060
#define     FR_ATTRIBUTE_DATA           0x00000080
#define     FR_ATTRIBUTE_INDEX_ROOT     0x00000090
#define     FR_ATTRIBUTE_INDEX_ALLOC    0x000000A0
#define     MINIMUM_ENTRY_SIZE          0x58

#pragma pack(push, 1)

typedef struct
{
    CHAR8  FileName[11];
    UINT8  Unused[3];       //Attr, NTRes, CrtTimeTenth;
    UINT16 Unused1[3];       //CrtTime, CrtDate, LstAccDate;
    UINT16 FirstClusterHi;
    UINT16 Unused2[2];       //WrtTime, WrtDate;
    UINT16 FirstClusterLo;
    UINT32 FileSize;
} DIR_ENTRY;

typedef struct
{
    UINT8  jmp[3];               //0
    CHAR8  OEMName[8];           //3
    UINT16 BytsPerSec;           //11
    UINT8  SecPerClus;           //13
    UINT16 RsvdSecCnt;           //14
    UINT8  NumFATs;              //16
    UINT16 RootEntCnt;           //17
    UINT16 TotSec16;             //19
    UINT8  Media;                //21
    UINT16 FATSz16;              //22
    UINT16 SecPerTrk;            //24
    UINT16 NumHeads;             //26
    UINT32 HiddSec;              //28
    UINT32 TotSec32;             //32
    union
    {
        struct
        {
            UINT8 Padding[510 - 36];
        } Fat16;
        struct
        {
            UINT32 FATSz32;     //36
            UINT8  Padding1[4];  //40
            UINT32 RootClus;    //44
            UINT8  Padding2[510 - 48];
        } Fat32;
        struct
        {
            UINT32 FATSz32u;    //36
            UINT64 TotSec64;    //40
            UINT64 MFTClus;     //48
            UINT8  Padding3[510 - 56];
        } Ntfs;
    } Fat;
    UINT16 Signature;
} BOOT_SECTOR;

typedef struct _RC_VOL_INFO
{
    UINT32                        BlockSize;
    EFI_PEI_RECOVERY_BLOCK_IO_PPI *BlkIo;
    UINTN                         Device;
    UINT8                         FatType;
    UINT8                         *FatPointer;
    DIR_ENTRY                     *RootPointer;
    UINT64                        PartitionOffset;
    UINT64                        FatOffset;
    UINT64                        RootOffset;
    UINT64                        DataOffset;
    UINT32                        BytesPerCluster;
} RC_VOL_INFO;

typedef struct
{
    UINT8  Length, ExtAttrLength;   //1,2
    UINT32 ExtentOffset, Unused1;   //3..10
    UINT32 DataLength, Unused2;     //11..18
    UINT8  DataTime[7];             //19..25
    UINT8  Flags;                   //26
    UINT8  Unused3[32 - 26];        //27..32
    UINT8  LengthOfFileId;          //33
} DIR_RECORD;

typedef struct
{
    UINT8      Type; //1
    CHAR8      StandardId[5]; //2..6
    UINT8      Version; //7
    UINT8      Unused1[156 - 7]; //8..156
    DIR_RECORD RootDir; //157..190
    UINT8      Padding;
    CHAR8      Unused2[2048 - 190]; //191..2048
} PRIMARY_VOLUME_DESCRIPTOR;

typedef struct
{
    UINT8  BootRecordIndicator;
    CHAR8  ISO9660Identifier[0x5];
    UINT8  DescriptorVersion;
    CHAR8  BootSystemIdentifier[0x20];
    CHAR8  Unused0[0x20];
    UINT32 BootCatalogFirstSector;
    UINT8  Unused1[0x7B6];
} BOOT_RECORD_VOLUME_DESCRIPTOR;

typedef struct
{
    UINT8  HeaderID;
    UINT8  PlatformID;
    UINT8  Reserved0[0x2];
    CHAR8  ManufacturerID[0x18];
    UINT16 Checksum;
    UINT16 Key;
} VALIDATION_ENTRY;

typedef struct
{
    UINT8  BootIndicator;
    UINT8  BootMediaType;
    UINT16 LoadSegment;
    UINT8  SystemType;
    UINT8  Unused0;
    UINT16 SectorCount;
    UINT32 LoadRBA;
    UINT8  Unused1[0x14];
} INITIAL_DEFAULT_ENTRY;

typedef struct _MBR_PARTITION
{
    UINT8  BootIndicator;
    UINT8  StartHead;
    UINT8  StartSector;
    UINT8  StartTrack;
    UINT8  OSType;
    UINT8  EndHead;
    UINT8  EndSector;
    UINT8  EndTrack;
    UINT32 StartingLba;
    UINT32 SizeInLba;
} MBR_PARTITION;

typedef struct _MASTER_BOOT_RECORD
{
    UINT8         BootCode[440];
    UINT32        UniqueMbrSig;
    UINT16        Unknown;
    MBR_PARTITION PartRec[4];
    UINT16        Sig;
} MASTER_BOOT_RECORD;

//
// GUID Partition structures
//
typedef struct _GUID_PARTITION
{
    UINT8  TypeGuid[16];
    UINT8  UniqueGuid[16];
    UINT64 FirstLba;
    UINT64 LastLba;
    UINT64 Flags;
    CHAR16 Name[36];
} GUID_PARTITION;

typedef struct _GUID_BOOT_RECORD
{
    GUID_PARTITION GuidPart[4];
} GUID_BOOT_RECORD;

typedef struct _GUID_TABLE_HEADER
{
    UINT8  Signature[8];
    UINT32 Revision;
    UINT32 HeaderSize;
    UINT32 HeaderCRC;
    UINT32 Reserved;
    UINT64 CurrentLBA;
    UINT64 BackupLBA;
    UINT64 FirstLBA;
    UINT64 LastLBA;
    UINT8  DiskGuid[16];
    UINT64 StartLBA;
    UINT32 EntryCount;
    UINT32 EntrySize;
    UINT32 ArraySize;
} GUID_TABLE_HEADER;

//
// NTFS Structures
//
//<AMI_SHDR_START>
//----------------------------------------------------------------------------
// Name:        MFT_FILE_RECORD
//
// Description: Structure of a Master File Table file record.
//
// Referrals:
//
//----------------------------------------------------------------------------
//<AMI_SHDR_END>

typedef struct _MFT_FILE_RECORD
{
    CHAR8 FR_Name[4];               // 0x00 - Always "FILE"
    UINT16 FR_UpdateSeqOffset;      // 0x04
    UINT16 FR_UpdateSeqSize;        // 0x06
    UINT64 FR_LogSeqNumber;         // 0x08
    UINT16 FR_SequenceNumber;       // 0x10
    UINT16 FR_HardLinkCount;        // 0x12
    UINT16 FR_AttributeOffset;      // 0x14 - Offset to Attributes
    UINT16 FR_Flags;                // 0x16 - File Record Flags
                                    //        0x01 - Record is in use
                                    //        0x02 - Record is a directory
    UINT32 FR_RealSize;             // 0x18 - Real size of record
    UINT32 FR_AllocatedSize;        // 0x1C - Allocated size of record
    UINT64 FR_BaseRecordNumber;     // 0x20 - Zero if this is a base record
} MFT_FILE_RECORD;


//<AMI_SHDR_START>
//----------------------------------------------------------------------------
// Name:        FR_ATTR_HEADER_RES
//
// Description: Structure of a File Record resident attribute header.
//
// Referrals:
//
// Note(s):     The "Name" in this structure is not a file name, but the
//              name of the attribute. Not used in this driver.
//
//----------------------------------------------------------------------------
//<AMI_SHDR_END>

typedef struct _FR_ATTR_HEADER_RES
{
    UINT32 AHR_Type;                // 0x00 - Attribute Type
                                    //        0x30 - File Name
                                    //        0x80 - Data
    UINT32 AHR_Length;              // 0x04 - Attribute Length
    UINT8 AHR_NonResFlag;           // 0x08 - Non-Resident Flag
    UINT8 AHR_NameLength;           // 0x09 - Name Length (if named)
    UINT16 AHR_NameOffset;          // 0x0A - Offset to Name (if named)
    UINT16 AHR_Flags;               // 0x0C - Attribute Hdr. Flags 
                                    //        0x0001 - Compressed
                                    //        0x4000 - Encrypted
                                    //        0x8000 - Sparse
    UINT16 AHR_AttributeId;         // 0x0E
    UINT32 AHR_InfoLength;          // 0x10 - Attribute Information Length
    UINT16 AHR_InfoOffset;          // 0x14 - Offset to this Attribute's 
                                    //        Information
    UINT16 AHR_IndexFlag;           // 0x16
} FR_ATTR_HEADER_RES;


//<AMI_SHDR_START>
//----------------------------------------------------------------------------
// Name:        FR_ATTR_HEADER_NONRES
//
// Description: Structure of a File Record non-resident attribute header.
//
// Referrals:
//
//----------------------------------------------------------------------------
//<AMI_SHDR_END>

typedef struct _FR_ATTR_HEADER_NONRES
{
    UINT32 AHNR_Type;               // 0x00 - Attribute Type
                                    //        0x30 - File Name
                                    //        0x80 - Data
    UINT32 AHNR_Length;             // 0x04 - Attribute Length
    UINT8 AHNR_NonResFlag;          // 0x08 - Non-Resident Flag
    UINT8 AHNR_NameLength;          // 0x09 - Name Length (if named)
    UINT16 AHNR_NameOffset;         // 0x0A - Offset to Name (if named)
    UINT16 AHNR_Flags;              // 0x0C - Flags
                                    //        0x0001 - Compressed
                                    //        0x4000 - Encrypted
                                    //        0x8000 - Sparse
    UINT16 AHNR_AttributeId;        // 0x0E
    UINT64 AHNR_StartingVCN;        // 0x10 - Staeting Virtual Cluster Number
    UINT64 AHNR_LastVCN;            // 0x18 - Last Virtual Cluster Number
    UINT16 AHNR_RunOffset;          // 0x20 - Offset to data runs
                                    //        (Usually 0x40 for a 
                                    //        Data attribute)
} FR_ATTR_HEADER_NONRES;


//<AMI_SHDR_START>
//----------------------------------------------------------------------------
// Name:        FR_NAME_ATTRIBUTE (0x30)
//
// Description: Structure of a Master File Table Name Attribute.
//
// Referrals:
//
// Notes:       Time stamps in NTFS are the number of 100ns units since
//              January 1, 1601.
//              This attribute is preceeded by a resident attribute header.
//----------------------------------------------------------------------------
//<AMI_SHDR_END>

typedef struct _FR_NAME_ATTRIBUTE
{
    UINT64 NA_PDReference;          // 0x00 - Parent Directory Reference 
    UINT64 NA_CreationTime;         // 0x08 - File Creation Time
    UINT64 NA_AlteredTime;          // 0x10 - File Altered Time
    UINT64 NA_MFTChangedTime;       // 0x18 - MFT Record Changed Time
    UINT64 NA_ReadTime;             // 0x20 - File Read Time
    UINT64 NA_AllocatedSize;        // 0x28 - Allocated Size of File
    UINT64 NA_RealSize;             // 0x30 - Real size of File
    UINT32 NA_Flags;                // 0x38 - File Flags
                                    //        Note: These flags correspond to 
                                    //        DOS flags up to 0x20 (Archive).
                                    //        If other flags are set, reject
                                    //        the file.
    UINT32 NA_Reparse;              // 0x3C
    UINT8 NA_NameLength;            // 0x40 - Length of file name in Words
    UINT8 NA_NameType;              // 0x41 - File Name type
                                    //        0x00 - POSIX
                                    //        0x01 - Unicode
                                    //        0x02 - DOS
                                    //        0x03 - Both
    CHAR16 NA_Name[1];              // 0x42 - File Name (unicode)
} FR_NAME_ATTRIBUTE;

//<AMI_SHDR_START>
//----------------------------------------------------------------------------
// Name:        FR_DATA_ATTRIBUTE (0x80)
//
// Description: Structure of a Master File Table Data Attribute.
//
// Referrals:
//
// Notes:       This attribute can be preceeded by a resident or non-resident
//              attribute header. If resident, the data will be the actual
//              file data. If non-resident, the data will be a run list
//
//----------------------------------------------------------------------------
//<AMI_SHDR_END>

typedef struct _FR_DATA_ATTRIBUTE
{
    UINT8 DA_Data[1];   // 0x00 - Run List or data
} FR_DATA_ATTRIBUTE;

//<AMI_SHDR_START>
//----------------------------------------------------------------------------
// Name:        FR_INDEX_ROOT_ATTRIBUTE (0x90)
//
// Description: Structure of a Master File Table Index Root Attribute.
//
// Referrals:
//
// Notes:       A small index will have it's entries inside this attribute.
//              This attribute is preceeded by a resident attribute header.
//
//----------------------------------------------------------------------------
//<AMI_SHDR_END>
typedef struct _FR_INDEX_ROOT_ATTRIBUTE
{
    UINT32 IRA_Type;
    UINT32 IRA_Collation;
    UINT32 IRA_Size;
    UINT8 IRA_ClustersPerIndexRec;
    UINT8 Padding1[3];
    UINT32 IRA_Offset;      // 0x10 - Offset to Index entries (if small index)
                            // The above offset is from here, 
                            // not from beginning.
    UINT32 IRA_TotalSize;
    UINT32 IRA_AllocSize;
    UINT8 IRA_Flags;        // 0x1C - 0 = small index, 1 = large index
    UINT8 Padding2[3];
} FR_INDEX_ROOT_ATTRIBUTE;

//<AMI_SHDR_START>
//----------------------------------------------------------------------------
// Name:        FR_INDEX_ALLOC_ATTRIBUTE (0xA0)
//
// Description: Structure of a Master File Table Index Allocation Attribute.
//
// Referrals:
//
// Notes:       A large index will be in a separate index file, and the 
//              run list in this attribute will indicate where it is.
//              This attribute is preceeded by a non-resident attribute header
//
//----------------------------------------------------------------------------
//<AMI_SHDR_END>
typedef struct _FR_INDEX_ALLOC_ATTRIBUTE
{
    UINT8 IAA_RunList[1];   // 0x00 - Run list of index record
} FR_INDEX_ALLOC_ATTRIBUTE;

//<AMI_SHDR_START>
//----------------------------------------------------------------------------
// Name:        INDEX_RECORD
//
// Description: Structure of an Index record.
//
// Referrals:
//
// Notes:       In NTFS, the Index is equivalent to a directory in FAT. A large
//              index will be contained in an Index Record, while a small index
//              will be contained within the Master File Table File Record for
//              the index, in the Index Root Attribute. Index Records are 
//              files, the way FAT directories are files, but not every 
//              "directory" will have a file. See the structure INDEX_ENTRY 
//              for the actual "directory entry" format.
//
//----------------------------------------------------------------------------
//<AMI_SHDR_END>
typedef struct _INDEX_RECORD
{
    CHAR8 INDR_Name[4];              // 0x00 - Always "INDX"
    UINT16 INDR_UpSeqOff;            // 0x04 - Offset to update sequence
    UINT16 INDR_UpSeqSize;           // 0x06 - Size of update sequence
    UINT64 INDR_LogSeqNo;            // 0x08 - Log sequence number
    UINT64 INDR_VCN;                 // 0x10 - Virtual Cluster Number
    UINT32 INDR_IndxEntryOff;        // 0x18 - Offset to Index Entries
                                     //        (from here, not from start)
    UINT32 INDR_IndxEntrySize;       // 0x1C - Size of Index Entries
    UINT32 INDR_IndxEntryAlloc;      // 0x20 - Allocated size of Entries
    UINT8 INDR_LeafNode;             // 0x24 - 1 if not leaf node
    UINT8 INDR_Padding[3];           // 0x25 - Padding
    UINT16 INDR_UpdateSequence;      // 0x28 - Update Sequence
    UINT16 INDR_SeqenceArray[1];     // 0x2A - Sequence Array
} INDEX_RECORD;

//<AMI_SHDR_START>
//----------------------------------------------------------------------------
// Name:        INDEX_ENTRY
//
// Description: Structure of an Index record entry.
//
// Referrals:
//
// Notes:       In NTFS, the index is the equivalent of a directory in FAT,
//              so the index entry is the equivalent of a directory entry.
//              The lower 4 bytes of the MFT reference is the number of the
//              file's File Record in the MFT. The lower 4 bytes of the MFT
//              parent reference is the number of the File Record for the
//              Index that this entry is in.
//----------------------------------------------------------------------------
//<AMI_SHDR_END>
typedef struct _INDEX_ENTRY
{
    UINT64 INDE_MFTRecord;      // 0x00 - MFT reference of this entry's file
    UINT16 INDE_EntrySize;      // 0x08 - Size of this entry
    UINT16 INDE_NameOffset;     // 0x0a - Offset to file name
    UINT16 INDE_IndexFlags;     // 0x0C - Index Flags
    UINT16 INDE_Padding;
    UINT64 INDE_MFTParentRec;   // 0x10 - MFT refernece of this entry's parent
    UINT64 INDE_CreationTime;   // 0x18 - File Creation Time
    UINT64 INDE_AlteredTime;    // 0x20 - File Altered Time
    UINT64 INDE_MFTChangedTime; // 0x28 - MFT Record Changed Time
    UINT64 INDE_ReadTime;       // 0x30 - File Read Time
    UINT64 INDE_AllocSize;      // 0x38 - Allocated File Size
    UINT64 INDE_RealSize;       // 0x40 - Real File Size
    UINT64 INDE_FileFlags;      // 0x48 - File Flags (like DOS flags + more)
    UINT8 INDE_NameLength;      // 0x50 - Length of File name
    UINT8 INDE_NameType;        // 0x51 - File Name type
                                //        0x00 - POSIX
                                //        0x01 - Unicode
                                //        0x02 - DOS
                                //        0x03 - Both
    CHAR16 INDE_Name[1];        // 0x52 - File Name (Unicode, not terminated)
} INDEX_ENTRY;

//
// EXTx STRUCTURES
//
//<AMI_SHDR_START>
//----------------------------------------------------------------------------
// Name:        DIR_ENTRY_EXT
//
// Description: Structure of a EXT directory entry
//
// Referrals:
//
// Notes:       The directory entry length must be a multiple of 4 bytes
//              Padding will be added after the name if necessary.
//              Note that the directory does not contain any file info
//              other than the name. That info is in the inode for the file.
//
//----------------------------------------------------------------------------
//<AMI_SHDR_END>
typedef struct _DIR_ENTRY_EXT {
    UINT32 DIR_Inode;               // 0x00 - Inode no. of file
    UINT16 DIR_EntryLength;         // 0x04 - Length of dir. entry
    UINT8  DIR_NameLength;          // 0x06 - Length of name
    UINT8  DIR_FileType;            // 0x07 - Type of file
                                    //        1 = regular file, 2 = directory
    CHAR8  DIR_Name[1];             // 0x08 - File name starts here
} DIR_ENTRY_EXT;


//<AMI_SHDR_START>
//----------------------------------------------------------------------------
// Name:        VOLUME_SB
//
// Description: Structure of the Volume SuperBlock for EXT volumes.
//
// Referrals:
//
// Notes:       The Superblock is equivalent to the BPB of a FAT or NTFS
//              volume, containing information about the volume. However,
//              it does not contain any boot code.
//
//----------------------------------------------------------------------------
//<AMI_SHDR_END>

typedef struct  _VOLUME_SB {
    UINT32 SB_TotalInodes;      // 0x00 - Total Inodes
    UINT32 SB_TotalBlocks;      // 0x04 - Total size in blocks
    UINT32 SB_ReservedBlocks;   // 0x08 - Reserved blocks
    UINT32 SB_FreeBlocks;       // 0x0C - Free blocks
    UINT32 SB_FreeInodes;       // 0x10 - Free inodes
    UINT32 SB_FirstBlock;       // 0x14 - First data block (always 1 or 0)
    UINT32 SB_BlockSize;        // 0x18 - Block size shift value
                                //        The actual block size is calculated
                                //        as 1024 << SB_BlockSize
    UINT32 SB_FragSize;         // 0x0C - Fragment size
    UINT32 SB_BlocksPerGroup;   // 0x20 - Blocks per group
    UINT32 SB_FragsPerGroup;    // 0x24 - Fragments per group
    UINT32 SB_InodesPerGroup;   // 0x28 - Inodes per group
    UINT32 SB_LastMountTime;    // 0x2C - Time of last mount
    UINT32 SB_LastWriteTime;    // 0x30 - Time of last write
    UINT16 SB_MountCount;       // 0x34 - Count of mounts since verification
    UINT16 SB_MaxMountCount;    // 0x36 - Max mounts allowed before check
    UINT16 SB_Magic;            // 0x38 - Magic number (0xEF53)
    UINT16 SB_StatusFlag;       // 0x3A - 1 = clean, 2 = error
    UINT16 SB_ErrorDo;          // 0x3C - What to do if error
    UINT16 SB_MinorRev;         // 0x3E - Minor revision level
    UINT32 SB_LastCheckTime;    // 0x40
    UINT32 SB_CheckInterval;    // 0x44
    UINT32 SB_CreatorOS;        // 0x48 - OS that created the volume (0=Linux)
    UINT32 SB_Revision;         // 0x4C
    UINT16 SB_DefaultUID;       // 0x50 - Default User ID
    UINT16 SB_DefaultGID;       // 0x52 - Default Group ID
    UINT32 SB_FirstInode;       // 0x54 - First non-reserved inode
    UINT16 SB_InodeStrucSize;   // 0x58 - Inode structure size
    UINT16 SB_BlockGroupNo;     // 0x5A - Block Group Number
    UINT32 SB_CompFeatMap;      // 0x5C - Compatible feature bitmap
    UINT32 SB_IncompFeatMap;    // 0x60 - Incompatible feature bitmap
    UINT32 SB_ROCompFeatMap;    // 0x64 - Read-only comp. feature bitmap
    EFI_GUID SB_UUID;           // 0x68 - Volume Unique ID
    CHAR8  SB_VolumeName[16];   // 0x78 - Volume name
    CHAR8  SB_LastPath[64];     // 0x88 - Last mount path (normally not used)
    UINT32 SB_AlgoBitmap;       // 0xC8 - Compression algorithm bitmap
    UINT8  SB_PreAllocBlocks;   // 0xCC - Blocks to pre-allocate for new file
    UINT8  SB_PreAllocBlocksDir;// 0xCD - Blocks to pre-allocate for new directory
    UINT8  SB_Padding[512-0xCE];// 0xCE - Padding to 1024 starts here
                                //        We're loading this into an MBR buffer,
                                //        so we only pad to 512.
} VOLUME_SB;


//<AMI_SHDR_START>
//----------------------------------------------------------------------------
// Name:        VOLUME_BGDT
//
// Description: Structure of the Volume Block Group Descriptor Table.
//
// Referrals:
//
// Notes:       In EXT, volumes are arranged as groups of blocks. The size
//              of each group is defined in SB_BlocksPerGroup in the
//              Superblock. The descriptor table gives info about the group.
//
//----------------------------------------------------------------------------
//<AMI_SHDR_END>

typedef struct  _VOLUME_BGDT {
    UINT32 BGDT_BlockBitmapBlk; // 0x00 - Block bitmap block number
    UINT32 BGDT_InodeBitmapBlk; // 0x04 - Inode bitmap block number
    UINT32 BGDT_InodeTableBlk;  // 0x08 - Inode table block number
    UINT16 BGDT_FreeBlocks;     // 0x0C - Free blocks in the group
    UINT16 BGDT_FreeInodes;     // 0x0E - Free inodes in the group
    UINT16 BGDT_UsedDirs;       // 0x10 - Inodes used for directories in the group
    UINT16 BGDT_Padding;        // 0x12 - Padding
    UINT8  BGDT_Reserved[12];   // 0x14 - Reserved
} VOLUME_BGDT;


//<AMI_SHDR_START>
//----------------------------------------------------------------------------
// Name:        VOLUME_IT
//
// Description: Structure of an Inode table
//
// Referrals:
//
// Notes:       The inode table table contains all the information about each
//              file EXCEPT the file's name, which is in the directory.
//              The directory contains the file's name and inode number.
//              The inodes are numbered sequentially in the table, and the
//              table occupies consecutive blocks.
//
//              EXT2, EXT3 File allocation method:
//              The block map of the file in IT_Blocks works as follows:
//              The first 12 entries are direct block numbers of the file.
//              The 13th entry is the number of a block containing block
//              numbers of the file (single-indirect blocks).
//              The 14th entry is the number of a block containing block
//              numbers of blocks containing file block numbers (double-
//              indirect blocks).
//              Similarly, the 15th entry is triple-indirect blocks.
//              Therefore, the maximum file size with 1024-byte blocks
//              would be 12+256+65536+16777216 1024-byte blocks.
//
//              EXT4 File allocation method:
//              Instead of a block map, EXT4 has a header followed by up
//              to 4 extents of contiguous blocks. If the entire file is
//              contiguous, there will be only one extent. Each extent
//              consists of a 32-bit logical block no. (relative to the
//              start of the file), a 16-bit  block count, and a 48-bit
//              physical block number, expressed as a 16-bit high value
//              and a 32-bit low value. If a file requires more than 4
//              extents, the value of EH_Depth in the header will be
//              non-zero, and the first extent will point to a block of
//              extents indtead of the file.
//
//              The file dates in VOLUME_IT are the number of seconds since
//              January 1, 1970.
//
//----------------------------------------------------------------------------
//<AMI_SHDR_END>

typedef struct _EXT_HDR {
    UINT16 EH_Magic;            // 0x00 - Magic number (0xF30A)
    UINT16 EH_Extents;          // 0x02 - Number of extents
    UINT16 EH_Max;              // 0x04 - Maximum no. of extents
    UINT16 EH_Depth;            // 0x06 - Depth of tree (0 normally)
    UINT32 EH_Generation;       // 0x08 - Generation ID (currently 0)
} EXT_HDR;
typedef struct _EXT_EXT {
    UINT32 EE_LogBlk;          // 0x00 - Logical block no.
    UINT16 EE_Length;          // 0x04 - Length of extent in blocks
    UINT16 EE_BlockHi;         // 0x06 - Uppper 16 bits of block no.
    UINT32 EE_BlockLo;         // 0x08 - Lower 32 bits of block no.
} EXT_EXT;

typedef struct _VOLUME_IT {
    UINT16 IT_Type;             // 0x00 - File type and access rights
                                //        0x8??? = regular file
                                //        0x4??? = directory
    UINT16 IT_UID;              // 0x02 - User ID
    UINT32 IT_SizeLo;           // 0x04 - File size (low 32 bits)
    UINT32 IT_ATime;            // 0x08 - Last access time
    UINT32 IT_CTime;            // 0x0C - File creation time
    UINT32 IT_MTime;            // 0x10 - File modified time
    UINT32 IT_DTime;            // 0x14 - File deleted time
    UINT16 IT_GID;              // 0x18 - Group ID
    UINT16 IT_Links;            // 0x1A - Links to this file (usually 1)
    UINT32 IT_ListSize;         // 0x1C - Block list size in 512-byte units.
    UINT32 IT_Flags;            // 0x20 - File flags
    UINT32 IT_OSD1;             // 0x24 - OS Dependant value no. 1
    union
    {
        struct
        {
        UINT32 Blocks[15];      // 0x28 - Blocks occupied by file (see notes)
        } Ext2;
        struct
        {
        EXT_HDR Header;         // 0x28 - Extent header
        EXT_EXT Extent[4];      // 0x34 - Extents of file (see notes)
        } Ext4;
    } Alloc;
    UINT32 IT_Version;          // 0x64 - File version
    UINT32 IT_FileACL;          // 0x68 - File Access Control List
    UINT32 IT_SizeHi;           // 0x6C - File size (high 32 bits)
    UINT32 IT_Fragment;         // 0x70 - Not used in Linux
    UINT32 IT_OSD2[3];          // 0x74 - OS Dependant value no. 2
} VOLUME_IT;

//<AMI_SHDR_START>
//----------------------------------------------------------------------------
// Name:        MEMORY_BLOCK
//
// Description: Structure of a memory boock used to hold various basic
//              structures.
//
// Referrals:
//
// Notes:       In order to make a smaller compiled binary, this memory block
//              is allocated, and used to hold structures that were formerly
//              reserved statically.
//
//----------------------------------------------------------------------------
//<AMI_SHDR_END>

typedef struct _MEMORY_BLOCK {
    MASTER_BOOT_RECORD  Mbr;            // Master boot record (all file systems)
    BOOT_SECTOR         Bs;             // Boot sector (FAT and NTFS)
    UINT8               MFTRunList[256];// Master File Table run list (NTFS)
    UINT8               RootRunList[128];// Root dir run list (NTFS)
    UINT8               ResidentIndex[256];// Stores resident index (NTFS)
    VOLUME_SB           Sb;             // Superblock (EXT(n))
    VOLUME_IT           RootInode;      // Root directory inode (EXT(n))
    VOLUME_IT           FileInode;      // File inode (EXT(n))
} MEMORY_BLOCK;

#pragma pack(pop)

//**************** eLink override definitions *******************************
//This functions may be overriden by eLinks

VOID GetFileListFromPrimaryVolume(
    IN  DIR_RECORD          *Root,
    IN  UINT32              RootSize,
    OUT UINTN               *NumberOfFiles,
    OUT DIR_RECORD          **Buffer
);

VOID GetFileListFromFatVolume(
    IN  DIR_ENTRY            *Root,
    IN  UINT32               RootEntries,
    OUT UINTN                *NumberOfFiles,
    OUT DIR_ENTRY            **Buffer
);

VOID GetFileListFromNtfsVolume(
    IN  UINT8               *Root,
    IN  UINT32              RootSize,
    OUT UINTN               *NumberOfFiles,
    OUT INDEX_ENTRY         **Buffer
);

VOID GetFileListFromExtVolume(
    IN  UINT8               *Root,
    IN  UINT32              RootSize,
    OUT UINTN               *NumberOfFiles,
    OUT DIR_ENTRY_EXT       **Buffer
);

BOOLEAN IsValidFile(
    IN VOID  *FileData,
    IN UINTN FileSize
);

EFI_STATUS GetRecoveryFileInfo(
    IN EFI_PEI_SERVICES **PeiServices,
    IN OUT VOID         **pCapsuleName,
    IN OUT UINTN        *pCapsuleSize,
    OUT    BOOLEAN      *ExtendedVerification
);

/****** DO NOT WRITE BELOW THIS LINE *******/
#ifdef __cplusplus
}
#endif
#endif
//*************************************************************************
//*************************************************************************
//**                                                                     **
//**        (C)Copyright 1985-2008, American Megatrends, Inc.            **
//**                                                                     **
//**                       All Rights Reserved.                          **
//**                                                                     **
//**      5555 Oakbrook Parkway, Suite 200, Norcross, GA 30093           **
//**                                                                     **
//**                       Phone: (770)-246-8600                         **
//**                                                                     **
//*************************************************************************
//*************************************************************************