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