]> git.proxmox.com Git - mirror_edk2.git/blame - FatPkg/FatPei/Mbr.c
FatPkg: Break down Part.c file.
[mirror_edk2.git] / FatPkg / FatPei / Mbr.c
CommitLineData
6aac772c
CC
1/** @file\r
2 Routines supporting partition discovery and\r
3 logical device reading\r
4\r
5Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.<BR>\r
6\r
7This program and the accompanying materials are licensed and made available\r
8under the terms and conditions of the BSD License which accompanies this\r
9distribution. The full text of the license may be found at\r
10http://opensource.org/licenses/bsd-license.php\r
11\r
12THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
13WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
14\r
15**/\r
16\r
17#include <IndustryStandard/Mbr.h>\r
18#include "FatLitePeim.h"\r
19\r
20/**\r
21 Test to see if the Mbr buffer is a valid MBR\r
22\r
23 @param[in] Mbr Parent Handle\r
24 @param[in] LastLba Last Lba address on the device.\r
25\r
26 @retval TRUE Mbr is a Valid MBR\r
27 @retval FALSE Mbr is not a Valid MBR\r
28\r
29**/\r
30BOOLEAN\r
31PartitionValidMbr (\r
32 IN MASTER_BOOT_RECORD *Mbr,\r
33 IN EFI_PEI_LBA LastLba\r
34 )\r
35{\r
36 UINT32 StartingLBA;\r
37 UINT32 EndingLBA;\r
38 UINT32 NewEndingLBA;\r
39 INTN Index1;\r
40 INTN Index2;\r
41 BOOLEAN MbrValid;\r
42\r
43 if (Mbr->Signature != MBR_SIGNATURE) {\r
44 return FALSE;\r
45 }\r
46 //\r
47 // The BPB also has this signature, so it can not be used alone.\r
48 //\r
49 MbrValid = FALSE;\r
50 for (Index1 = 0; Index1 < MAX_MBR_PARTITIONS; Index1++) {\r
51 if (Mbr->Partition[Index1].OSIndicator == 0x00 || UNPACK_UINT32 (Mbr->Partition[Index1].SizeInLBA) == 0) {\r
52 continue;\r
53 }\r
54\r
55 MbrValid = TRUE;\r
56 StartingLBA = UNPACK_UINT32 (Mbr->Partition[Index1].StartingLBA);\r
57 EndingLBA = StartingLBA + UNPACK_UINT32 (Mbr->Partition[Index1].SizeInLBA) - 1;\r
58 if (EndingLBA > LastLba) {\r
59 //\r
60 // Compatability Errata:\r
61 // Some systems try to hide drive space with thier INT 13h driver\r
62 // This does not hide space from the OS driver. This means the MBR\r
63 // that gets created from DOS is smaller than the MBR created from\r
64 // a real OS (NT & Win98). This leads to BlockIo->LastBlock being\r
65 // wrong on some systems FDISKed by the OS.\r
66 //\r
67 // return FALSE Because no block devices on a system are implemented\r
68 // with INT 13h\r
69 //\r
70 return FALSE;\r
71 }\r
72\r
73 for (Index2 = Index1 + 1; Index2 < MAX_MBR_PARTITIONS; Index2++) {\r
74 if (Mbr->Partition[Index2].OSIndicator == 0x00 || UNPACK_INT32 (Mbr->Partition[Index2].SizeInLBA) == 0) {\r
75 continue;\r
76 }\r
77\r
78 NewEndingLBA = UNPACK_UINT32 (Mbr->Partition[Index2].StartingLBA) + UNPACK_UINT32 (Mbr->Partition[Index2].SizeInLBA) - 1;\r
79 if (NewEndingLBA >= StartingLBA && UNPACK_UINT32 (Mbr->Partition[Index2].StartingLBA) <= EndingLBA) {\r
80 //\r
81 // This region overlaps with the Index1'th region\r
82 //\r
83 return FALSE;\r
84 }\r
85 }\r
86 }\r
87 //\r
88 // Non of the regions overlapped so MBR is O.K.\r
89 //\r
90 return MbrValid;\r
91}\r
92\r
93/**\r
94 This function finds Mbr partitions. Main algorithm\r
95 is ported from DXE partition driver.\r
96\r
97 @param[in] PrivateData The global memory map\r
98 @param[in] ParentBlockDevNo The parent block device\r
99\r
100 @retval TRUE New partitions are detected and logical block devices\r
101 are added to block device array\r
102 @retval FALSE No new partitions are added\r
103\r
104**/\r
105BOOLEAN\r
106FatFindMbrPartitions (\r
107 IN PEI_FAT_PRIVATE_DATA *PrivateData,\r
108 IN UINTN ParentBlockDevNo\r
109 )\r
110{\r
111 EFI_STATUS Status;\r
112 MASTER_BOOT_RECORD *Mbr;\r
113 UINTN Index;\r
114 BOOLEAN Found;\r
115 PEI_FAT_BLOCK_DEVICE *ParentBlockDev;\r
116 PEI_FAT_BLOCK_DEVICE *BlockDev;\r
117\r
118 if (ParentBlockDevNo > PEI_FAT_MAX_BLOCK_DEVICE - 1) {\r
119 return FALSE;\r
120 }\r
121\r
122 ParentBlockDev = &(PrivateData->BlockDevice[ParentBlockDevNo]);\r
123\r
124 if (ParentBlockDev->BlockSize > PEI_FAT_MAX_BLOCK_SIZE) {\r
125 DEBUG((DEBUG_ERROR, "Device BlockSize %x exceeds FAT_MAX_BLOCK_SIZE\n", ParentBlockDev->BlockSize));\r
126 return FALSE;\r
127 }\r
128\r
129 Found = FALSE;\r
130 Mbr = (MASTER_BOOT_RECORD *) PrivateData->BlockData;\r
131\r
132 Status = FatReadBlock (\r
133 PrivateData,\r
134 ParentBlockDevNo,\r
135 0,\r
136 ParentBlockDev->BlockSize,\r
137 Mbr\r
138 );\r
139\r
140 if (EFI_ERROR (Status) || !PartitionValidMbr (Mbr, ParentBlockDev->LastBlock)) {\r
141 goto Done;\r
142 }\r
143 //\r
144 // We have a valid mbr - add each partition\r
145 //\r
146 for (Index = 0; Index < MAX_MBR_PARTITIONS; Index++) {\r
147 if (Mbr->Partition[Index].OSIndicator == 0x00 || UNPACK_INT32 (Mbr->Partition[Index].SizeInLBA) == 0) {\r
148 //\r
149 // Don't use null MBR entries\r
150 //\r
151 continue;\r
152 }\r
153 //\r
154 // Register this partition\r
155 //\r
156 if (PrivateData->BlockDeviceCount < PEI_FAT_MAX_BLOCK_DEVICE) {\r
157\r
158 Found = TRUE;\r
159\r
160 BlockDev = &(PrivateData->BlockDevice[PrivateData->BlockDeviceCount]);\r
161\r
162 BlockDev->BlockSize = MBR_SIZE;\r
163 BlockDev->LastBlock = UNPACK_INT32 (Mbr->Partition[Index].SizeInLBA) - 1;\r
164 BlockDev->IoAlign = ParentBlockDev->IoAlign;\r
165 BlockDev->Logical = TRUE;\r
166 BlockDev->PartitionChecked = FALSE;\r
167 BlockDev->StartingPos = MultU64x32 (\r
168 UNPACK_INT32 (Mbr->Partition[Index].StartingLBA),\r
169 ParentBlockDev->BlockSize\r
170 );\r
171 BlockDev->ParentDevNo = ParentBlockDevNo;\r
172\r
173 PrivateData->BlockDeviceCount++;\r
174 }\r
175 }\r
176\r
177Done:\r
178\r
179 ParentBlockDev->PartitionChecked = TRUE;\r
180 return Found;\r
181}\r