TianoCore EDK2 master
Loading...
Searching...
No Matches
Mbr.c
Go to the documentation of this file.
1
12#include "FatLitePeim.h"
13
24BOOLEAN
27 IN EFI_PEI_LBA LastLba
28 )
29{
30 UINT32 StartingLBA;
31 UINT32 EndingLBA;
32 UINT32 NewEndingLBA;
33 INTN Index1;
34 INTN Index2;
35 BOOLEAN MbrValid;
36
37 if (Mbr->Signature != MBR_SIGNATURE) {
38 return FALSE;
39 }
40
41 //
42 // The BPB also has this signature, so it can not be used alone.
43 //
44 MbrValid = FALSE;
45 for (Index1 = 0; Index1 < MAX_MBR_PARTITIONS; Index1++) {
46 if ((Mbr->Partition[Index1].OSIndicator == 0x00) || (UNPACK_UINT32 (Mbr->Partition[Index1].SizeInLBA) == 0)) {
47 continue;
48 }
49
50 MbrValid = TRUE;
51 StartingLBA = UNPACK_UINT32 (Mbr->Partition[Index1].StartingLBA);
52 EndingLBA = StartingLBA + UNPACK_UINT32 (Mbr->Partition[Index1].SizeInLBA) - 1;
53 if (EndingLBA > LastLba) {
54 //
55 // Compatibility Errata:
56 // Some systems try to hide drive space with their INT 13h driver
57 // This does not hide space from the OS driver. This means the MBR
58 // that gets created from DOS is smaller than the MBR created from
59 // a real OS (NT & Win98). This leads to BlockIo->LastBlock being
60 // wrong on some systems FDISKed by the OS.
61 //
62 // return FALSE Because no block devices on a system are implemented
63 // with INT 13h
64 //
65 return FALSE;
66 }
67
68 for (Index2 = Index1 + 1; Index2 < MAX_MBR_PARTITIONS; Index2++) {
69 if ((Mbr->Partition[Index2].OSIndicator == 0x00) || (UNPACK_INT32 (Mbr->Partition[Index2].SizeInLBA) == 0)) {
70 continue;
71 }
72
73 NewEndingLBA = UNPACK_UINT32 (Mbr->Partition[Index2].StartingLBA) + UNPACK_UINT32 (Mbr->Partition[Index2].SizeInLBA) - 1;
74 if ((NewEndingLBA >= StartingLBA) && (UNPACK_UINT32 (Mbr->Partition[Index2].StartingLBA) <= EndingLBA)) {
75 //
76 // This region overlaps with the Index1'th region
77 //
78 return FALSE;
79 }
80 }
81 }
82
83 //
84 // Non of the regions overlapped so MBR is O.K.
85 //
86 return MbrValid;
87}
88
101BOOLEAN
103 IN PEI_FAT_PRIVATE_DATA *PrivateData,
104 IN UINTN ParentBlockDevNo
105 )
106{
107 EFI_STATUS Status;
109 UINTN Index;
110 BOOLEAN Found;
111 PEI_FAT_BLOCK_DEVICE *ParentBlockDev;
112 PEI_FAT_BLOCK_DEVICE *BlockDev;
113
114 if (ParentBlockDevNo > PEI_FAT_MAX_BLOCK_DEVICE - 1) {
115 return FALSE;
116 }
117
118 ParentBlockDev = &(PrivateData->BlockDevice[ParentBlockDevNo]);
119
120 if (ParentBlockDev->BlockSize > PEI_FAT_MAX_BLOCK_SIZE) {
121 DEBUG ((DEBUG_ERROR, "Device BlockSize %x exceeds FAT_MAX_BLOCK_SIZE\n", ParentBlockDev->BlockSize));
122 return FALSE;
123 }
124
125 Found = FALSE;
126 Mbr = (MASTER_BOOT_RECORD *)PrivateData->BlockData;
127
128 Status = FatReadBlock (
129 PrivateData,
130 ParentBlockDevNo,
131 0,
132 ParentBlockDev->BlockSize,
133 Mbr
134 );
135
136 if (EFI_ERROR (Status) || !PartitionValidMbr (Mbr, ParentBlockDev->LastBlock)) {
137 goto Done;
138 }
139
140 //
141 // We have a valid mbr - add each partition
142 //
143 for (Index = 0; Index < MAX_MBR_PARTITIONS; Index++) {
144 if ((Mbr->Partition[Index].OSIndicator == 0x00) || (UNPACK_INT32 (Mbr->Partition[Index].SizeInLBA) == 0)) {
145 //
146 // Don't use null MBR entries
147 //
148 continue;
149 }
150
151 //
152 // Register this partition
153 //
154 if (PrivateData->BlockDeviceCount < PEI_FAT_MAX_BLOCK_DEVICE) {
155 Found = TRUE;
156
157 BlockDev = &(PrivateData->BlockDevice[PrivateData->BlockDeviceCount]);
158
159 BlockDev->BlockSize = MBR_SIZE;
160 BlockDev->LastBlock = UNPACK_INT32 (Mbr->Partition[Index].SizeInLBA) - 1;
161 BlockDev->IoAlign = ParentBlockDev->IoAlign;
162 BlockDev->Logical = TRUE;
163 BlockDev->PartitionChecked = FALSE;
164 BlockDev->StartingPos = MultU64x32 (
165 UNPACK_INT32 (Mbr->Partition[Index].StartingLBA),
166 ParentBlockDev->BlockSize
167 );
168 BlockDev->ParentDevNo = ParentBlockDevNo;
169
170 PrivateData->BlockDeviceCount++;
171 }
172 }
173
174Done:
175
176 ParentBlockDev->PartitionChecked = TRUE;
177 return Found;
178}
UINT64 UINTN
INT64 INTN
UINT64 EFIAPI MultU64x32(IN UINT64 Multiplicand, IN UINT32 Multiplier)
Definition: MultU64x32.c:27
EFI_STATUS FatReadBlock(IN PEI_FAT_PRIVATE_DATA *PrivateData, IN UINTN BlockDeviceNo, IN EFI_PEI_LBA Lba, IN UINTN BufferSize, OUT VOID *Buffer)
Definition: FatLiteLib.c:54
BOOLEAN PartitionValidMbr(IN MASTER_BOOT_RECORD *Mbr, IN EFI_PEI_LBA LastLba)
Definition: Mbr.c:25
BOOLEAN FatFindMbrPartitions(IN PEI_FAT_PRIVATE_DATA *PrivateData, IN UINTN ParentBlockDevNo)
Definition: Mbr.c:102
#define TRUE
Definition: Base.h:301
#define FALSE
Definition: Base.h:307
#define IN
Definition: Base.h:279
#define DEBUG(Expression)
Definition: DebugLib.h:434
UINT64 EFI_PEI_LBA
Definition: BlockIo.h:41
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29