]>
Commit | Line | Data |
---|---|---|
0d18f5db CC |
1 | /** @file\r |
2 | Routines supporting partition discovery and\r | |
3 | logical device reading\r | |
4 | \r | |
5 | Copyright (c) 2019 Intel Corporation. All rights reserved.<BR>\r | |
6 | \r | |
7 | This program and the accompanying materials are licensed and made available\r | |
8 | under the terms and conditions of the BSD License which accompanies this\r | |
9 | distribution. The full text of the license may be found at\r | |
10 | http://opensource.org/licenses/bsd-license.php\r | |
11 | \r | |
12 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
13 | WITHOUT 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 <Uefi/UefiGpt.h>\r | |
19 | #include <Library/BaseLib.h>\r | |
20 | #include "FatLitePeim.h"\r | |
21 | \r | |
22 | //\r | |
23 | // Assumption: 'a' and 'blocksize' are all UINT32 or UINT64.\r | |
24 | // If 'a' and 'blocksize' are not the same type, should use DivU64xU32 to calculate.\r | |
25 | //\r | |
26 | #define EFI_SIZE_TO_BLOCKS(a, blocksize) (((a) / (blocksize)) + (((a) % (blocksize)) ? 1 : 0))\r | |
27 | \r | |
28 | //\r | |
29 | // GPT Partition Entry Status\r | |
30 | //\r | |
31 | typedef struct {\r | |
32 | BOOLEAN OutOfRange;\r | |
33 | BOOLEAN Overlap;\r | |
34 | BOOLEAN OsSpecific;\r | |
35 | } EFI_PARTITION_ENTRY_STATUS;\r | |
36 | \r | |
37 | /**\r | |
38 | Check if the CRC field in the Partition table header is valid.\r | |
39 | \r | |
40 | @param[in] PartHeader Partition table header structure\r | |
41 | \r | |
42 | @retval TRUE the CRC is valid\r | |
43 | @retval FALSE the CRC is invalid\r | |
44 | \r | |
45 | **/\r | |
46 | BOOLEAN\r | |
47 | PartitionCheckGptHeaderCRC (\r | |
48 | IN EFI_PARTITION_TABLE_HEADER *PartHeader\r | |
49 | )\r | |
50 | {\r | |
51 | UINT32 GptHdrCrc;\r | |
52 | UINT32 Crc;\r | |
53 | \r | |
54 | GptHdrCrc = PartHeader->Header.CRC32;\r | |
55 | \r | |
56 | //\r | |
57 | // Set CRC field to zero when doing calcuation\r | |
58 | //\r | |
59 | PartHeader->Header.CRC32 = 0;\r | |
60 | \r | |
61 | Crc = CalculateCrc32 (PartHeader, PartHeader->Header.HeaderSize);\r | |
62 | \r | |
63 | //\r | |
64 | // Restore Header CRC\r | |
65 | //\r | |
66 | PartHeader->Header.CRC32 = GptHdrCrc;\r | |
67 | \r | |
68 | return (GptHdrCrc == Crc);\r | |
69 | }\r | |
70 | \r | |
71 | \r | |
72 | /**\r | |
73 | Check if the CRC field in the Partition table header is valid\r | |
74 | for Partition entry array.\r | |
75 | \r | |
76 | @param[in] PartHeader Partition table header structure\r | |
77 | @param[in] PartEntry The partition entry array\r | |
78 | \r | |
79 | @retval TRUE the CRC is valid\r | |
80 | @retval FALSE the CRC is invalid\r | |
81 | \r | |
82 | **/\r | |
83 | BOOLEAN\r | |
84 | PartitionCheckGptEntryArrayCRC (\r | |
85 | IN EFI_PARTITION_TABLE_HEADER *PartHeader,\r | |
86 | IN EFI_PARTITION_ENTRY *PartEntry\r | |
87 | )\r | |
88 | {\r | |
89 | UINT32 Crc;\r | |
90 | UINTN Size;\r | |
91 | \r | |
92 | Size = (UINTN)MultU64x32(PartHeader->NumberOfPartitionEntries, PartHeader->SizeOfPartitionEntry);\r | |
93 | Crc = CalculateCrc32 (PartEntry, Size);\r | |
94 | \r | |
95 | return (BOOLEAN) (PartHeader->PartitionEntryArrayCRC32 == Crc);\r | |
96 | }\r | |
97 | \r | |
98 | /**\r | |
99 | The function is used for valid GPT table. Both for Primary and Backup GPT header.\r | |
100 | \r | |
101 | @param[in] PrivateData The global memory map\r | |
102 | @param[in] ParentBlockDevNo The parent block device\r | |
103 | @param[in] IsPrimaryHeader Indicate to which header will be checked.\r | |
104 | @param[in] PartHdr Stores the partition table that is read\r | |
105 | \r | |
106 | @retval TRUE The partition table is valid\r | |
107 | @retval FALSE The partition table is not valid\r | |
108 | \r | |
109 | **/\r | |
110 | BOOLEAN\r | |
111 | PartitionCheckGptHeader (\r | |
112 | IN PEI_FAT_PRIVATE_DATA *PrivateData,\r | |
113 | IN UINTN ParentBlockDevNo,\r | |
114 | IN BOOLEAN IsPrimaryHeader,\r | |
115 | IN EFI_PARTITION_TABLE_HEADER *PartHdr\r | |
116 | )\r | |
117 | {\r | |
118 | PEI_FAT_BLOCK_DEVICE *ParentBlockDev;\r | |
119 | EFI_PEI_LBA Lba;\r | |
120 | EFI_PEI_LBA AlternateLba;\r | |
121 | EFI_PEI_LBA EntryArrayLastLba;\r | |
122 | \r | |
123 | UINT64 PartitionEntryArraySize;\r | |
124 | UINT64 PartitionEntryBlockNumb;\r | |
125 | UINT32 EntryArraySizeRemainder;\r | |
126 | \r | |
127 | ParentBlockDev = &(PrivateData->BlockDevice[ParentBlockDevNo]);\r | |
128 | \r | |
129 | if (IsPrimaryHeader) {\r | |
130 | Lba = PRIMARY_PART_HEADER_LBA;\r | |
131 | AlternateLba = ParentBlockDev->LastBlock;\r | |
132 | } else {\r | |
133 | Lba = ParentBlockDev->LastBlock;\r | |
134 | AlternateLba = PRIMARY_PART_HEADER_LBA;\r | |
135 | }\r | |
136 | \r | |
137 | if ( (PartHdr->Header.Signature != EFI_PTAB_HEADER_ID) ||\r | |
138 | (PartHdr->Header.Revision != 0x00010000) ||\r | |
139 | (PartHdr->Header.HeaderSize < 92) ||\r | |
140 | (PartHdr->Header.HeaderSize > ParentBlockDev->BlockSize) ||\r | |
141 | (!PartitionCheckGptHeaderCRC (PartHdr)) ||\r | |
142 | (PartHdr->Header.Reserved != 0)\r | |
143 | ) {\r | |
144 | DEBUG ((DEBUG_ERROR, "Invalid efi partition table header\n"));\r | |
145 | return FALSE;\r | |
146 | }\r | |
147 | \r | |
148 | //\r | |
149 | // | Block0 | Block1 |Block2 ~ FirstUsableLBA - 1|FirstUsableLBA, ... ,LastUsableLBA|LastUsableLBA+1 ~ LastBlock-1| LastBlock |\r | |
150 | // |Protective MBR|Primary Header|Entry Array(At Least 16384)| Partition | Entry Array(At Least 16384) |BackUp Header|\r | |
151 | //\r | |
152 | // 1. Protective MBR is fixed at Block 0.\r | |
153 | // 2. Primary Header is fixed at Block 1.\r | |
154 | // 3. Backup Header is fixed at LastBlock.\r | |
155 | // 4. Must be remain 128*128 bytes for primary entry array.\r | |
156 | // 5. Must be remain 128*128 bytes for backup entry array.\r | |
157 | // 6. SizeOfPartitionEntry must be equals to 128 * 2^n.\r | |
158 | //\r | |
159 | if ( (PartHdr->MyLBA != Lba) ||\r | |
160 | (PartHdr->AlternateLBA != AlternateLba) ||\r | |
161 | (PartHdr->FirstUsableLBA < 2 + EFI_SIZE_TO_BLOCKS (EFI_GPT_PART_ENTRY_MIN_SIZE, ParentBlockDev->BlockSize)) ||\r | |
162 | (PartHdr->LastUsableLBA > ParentBlockDev->LastBlock - 1 - EFI_SIZE_TO_BLOCKS (EFI_GPT_PART_ENTRY_MIN_SIZE, ParentBlockDev->BlockSize)) ||\r | |
163 | (PartHdr->FirstUsableLBA > PartHdr->LastUsableLBA) ||\r | |
164 | (PartHdr->PartitionEntryLBA < 2) ||\r | |
165 | (PartHdr->PartitionEntryLBA > ParentBlockDev->LastBlock - 1) ||\r | |
166 | (PartHdr->PartitionEntryLBA >= PartHdr->FirstUsableLBA && PartHdr->PartitionEntryLBA <= PartHdr->LastUsableLBA) ||\r | |
167 | (PartHdr->SizeOfPartitionEntry%128 != 0) ||\r | |
168 | (PartHdr->SizeOfPartitionEntry != sizeof (EFI_PARTITION_ENTRY))\r | |
169 | ) {\r | |
170 | DEBUG ((DEBUG_ERROR, "Invalid efi partition table header\n"));\r | |
171 | return FALSE;\r | |
172 | }\r | |
173 | \r | |
174 | //\r | |
175 | // Ensure the NumberOfPartitionEntries * SizeOfPartitionEntry doesn't overflow.\r | |
176 | //\r | |
177 | if (PartHdr->NumberOfPartitionEntries > DivU64x32 (MAX_UINTN, PartHdr->SizeOfPartitionEntry)) {\r | |
178 | DEBUG ((DEBUG_ERROR, "Memory overflow in GPT Entry Array\n"));\r | |
179 | return FALSE;\r | |
180 | }\r | |
181 | \r | |
182 | PartitionEntryArraySize = MultU64x32 (PartHdr->NumberOfPartitionEntries, PartHdr->SizeOfPartitionEntry);\r | |
183 | EntryArraySizeRemainder = 0;\r | |
184 | PartitionEntryBlockNumb = DivU64x32Remainder (PartitionEntryArraySize, ParentBlockDev->BlockSize, &EntryArraySizeRemainder);\r | |
185 | if (EntryArraySizeRemainder != 0) {\r | |
186 | PartitionEntryBlockNumb++;\r | |
187 | }\r | |
188 | \r | |
189 | if (IsPrimaryHeader) {\r | |
190 | EntryArrayLastLba = PartHdr->FirstUsableLBA;\r | |
191 | } else {\r | |
192 | EntryArrayLastLba = ParentBlockDev->LastBlock;\r | |
193 | }\r | |
194 | \r | |
195 | //\r | |
196 | // Make sure partition entry array not overlaps with partition area or the LastBlock.\r | |
197 | //\r | |
198 | if (PartHdr->PartitionEntryLBA + PartitionEntryBlockNumb > EntryArrayLastLba) {\r | |
199 | DEBUG ((DEBUG_ERROR, "GPT Partition Entry Array Error!\n"));\r | |
200 | DEBUG ((DEBUG_ERROR, "PartitionEntryArraySize = %lu.\n", PartitionEntryArraySize));\r | |
201 | DEBUG ((DEBUG_ERROR, "PartitionEntryLBA = %lu.\n", PartHdr->PartitionEntryLBA));\r | |
202 | DEBUG ((DEBUG_ERROR, "PartitionEntryBlockNumb = %lu.\n", PartitionEntryBlockNumb));\r | |
203 | DEBUG ((DEBUG_ERROR, "EntryArrayLastLba = %lu.\n", EntryArrayLastLba));\r | |
204 | return FALSE;\r | |
205 | }\r | |
206 | \r | |
207 | return TRUE;\r | |
208 | }\r | |
209 | \r | |
210 | /**\r | |
211 | This function is used to verify each partition in block device.\r | |
212 | \r | |
213 | @param[in] PrivateData The global memory map\r | |
214 | @param[in] ParentBlockDevNo The parent block device\r | |
215 | @param[in] PartHdr Stores the partition table that is read\r | |
216 | \r | |
217 | @retval TRUE The partition is valid\r | |
218 | @retval FALSE The partition is not valid\r | |
219 | \r | |
220 | **/\r | |
221 | BOOLEAN\r | |
222 | PartitionCheckGptEntryArray (\r | |
223 | IN PEI_FAT_PRIVATE_DATA *PrivateData,\r | |
224 | IN UINTN ParentBlockDevNo,\r | |
225 | IN EFI_PARTITION_TABLE_HEADER *PartHdr\r | |
226 | )\r | |
227 | {\r | |
228 | EFI_STATUS Status;\r | |
229 | PEI_FAT_BLOCK_DEVICE *ParentBlockDev;\r | |
230 | PEI_FAT_BLOCK_DEVICE *BlockDevPtr;\r | |
231 | \r | |
232 | UINT64 PartitionEntryArraySize;\r | |
233 | UINT64 PartitionEntryBlockNumb;\r | |
234 | UINT32 EntryArraySizeRemainder;\r | |
235 | \r | |
236 | EFI_PARTITION_ENTRY *PartitionEntryBuffer;\r | |
237 | EFI_PARTITION_ENTRY_STATUS *PartitionEntryStatus;\r | |
238 | \r | |
239 | BOOLEAN Found;\r | |
240 | EFI_LBA StartingLBA;\r | |
241 | EFI_LBA EndingLBA;\r | |
242 | UINTN Index;\r | |
243 | UINTN Index1;\r | |
244 | UINTN Index2;\r | |
245 | EFI_PARTITION_ENTRY *Entry;\r | |
246 | \r | |
aad4e2ec CC |
247 | PartitionEntryBuffer = NULL;\r |
248 | PartitionEntryStatus = NULL;\r | |
249 | \r | |
0d18f5db CC |
250 | ParentBlockDev = &(PrivateData->BlockDevice[ParentBlockDevNo]);\r |
251 | Found = FALSE;\r | |
252 | \r | |
253 | PartitionEntryArraySize = MultU64x32 (PartHdr->NumberOfPartitionEntries, PartHdr->SizeOfPartitionEntry);\r | |
254 | EntryArraySizeRemainder = 0;\r | |
255 | PartitionEntryBlockNumb = DivU64x32Remainder (PartitionEntryArraySize, ParentBlockDev->BlockSize, &EntryArraySizeRemainder);\r | |
256 | if (EntryArraySizeRemainder != 0) {\r | |
257 | PartitionEntryBlockNumb++;\r | |
258 | }\r | |
259 | PartitionEntryArraySize = MultU64x32 (PartitionEntryBlockNumb, ParentBlockDev->BlockSize);\r | |
260 | \r | |
261 | PartitionEntryBuffer = (EFI_PARTITION_ENTRY *) AllocatePages (EFI_SIZE_TO_PAGES ((UINTN)PartitionEntryArraySize));\r | |
262 | if (PartitionEntryBuffer == NULL) {\r | |
263 | DEBUG ((DEBUG_ERROR, "Allocate memory error!\n"));\r | |
264 | goto EXIT;\r | |
265 | }\r | |
266 | \r | |
267 | PartitionEntryStatus = (EFI_PARTITION_ENTRY_STATUS *) AllocatePages (EFI_SIZE_TO_PAGES (PartHdr->NumberOfPartitionEntries * sizeof (EFI_PARTITION_ENTRY_STATUS)));\r | |
268 | if (PartitionEntryStatus == NULL) {\r | |
269 | DEBUG ((DEBUG_ERROR, "Allocate memory error!\n"));\r | |
270 | goto EXIT;\r | |
271 | }\r | |
272 | ZeroMem (PartitionEntryStatus, PartHdr->NumberOfPartitionEntries * sizeof (EFI_PARTITION_ENTRY_STATUS));\r | |
273 | \r | |
274 | Status = FatReadBlock (\r | |
275 | PrivateData,\r | |
276 | ParentBlockDevNo,\r | |
277 | PartHdr->PartitionEntryLBA,\r | |
278 | (UINTN)PartitionEntryArraySize,\r | |
279 | PartitionEntryBuffer\r | |
280 | );\r | |
281 | if (EFI_ERROR (Status)) {\r | |
282 | DEBUG ((DEBUG_ERROR, "Read partition entry array error!\n"));\r | |
283 | goto EXIT;\r | |
284 | }\r | |
285 | \r | |
286 | if (!PartitionCheckGptEntryArrayCRC (PartHdr, PartitionEntryBuffer)) {\r | |
287 | DEBUG ((DEBUG_ERROR, "Partition entries CRC check fail\n"));\r | |
288 | goto EXIT;\r | |
289 | }\r | |
290 | \r | |
291 | for (Index1 = 0; Index1 < PartHdr->NumberOfPartitionEntries; Index1++) {\r | |
292 | Entry = (EFI_PARTITION_ENTRY *) ((UINT8 *) PartitionEntryBuffer + Index1 * PartHdr->SizeOfPartitionEntry);\r | |
293 | if (CompareGuid (&Entry->PartitionTypeGUID, &gEfiPartTypeUnusedGuid)) {\r | |
294 | continue;\r | |
295 | }\r | |
296 | \r | |
297 | StartingLBA = Entry->StartingLBA;\r | |
298 | EndingLBA = Entry->EndingLBA;\r | |
299 | if (StartingLBA > EndingLBA ||\r | |
300 | StartingLBA < PartHdr->FirstUsableLBA ||\r | |
301 | StartingLBA > PartHdr->LastUsableLBA ||\r | |
302 | EndingLBA < PartHdr->FirstUsableLBA ||\r | |
303 | EndingLBA > PartHdr->LastUsableLBA\r | |
304 | ) {\r | |
305 | PartitionEntryStatus[Index1].OutOfRange = TRUE;\r | |
306 | continue;\r | |
307 | }\r | |
308 | \r | |
309 | if ((Entry->Attributes & BIT1) != 0) {\r | |
310 | //\r | |
311 | // If Bit 1 is set, this indicate that this is an OS specific GUID partition.\r | |
312 | //\r | |
313 | PartitionEntryStatus[Index1].OsSpecific = TRUE;\r | |
314 | }\r | |
315 | \r | |
316 | for (Index2 = Index1 + 1; Index2 < PartHdr->NumberOfPartitionEntries; Index2++) {\r | |
317 | Entry = (EFI_PARTITION_ENTRY *) ((UINT8 *) PartitionEntryBuffer + Index2 * PartHdr->SizeOfPartitionEntry);\r | |
318 | if (CompareGuid (&Entry->PartitionTypeGUID, &gEfiPartTypeUnusedGuid)) {\r | |
319 | continue;\r | |
320 | }\r | |
321 | \r | |
322 | if (Entry->EndingLBA >= StartingLBA && Entry->StartingLBA <= EndingLBA) {\r | |
323 | //\r | |
324 | // This region overlaps with the Index1'th region\r | |
325 | //\r | |
326 | PartitionEntryStatus[Index1].Overlap = TRUE;\r | |
327 | PartitionEntryStatus[Index2].Overlap = TRUE;\r | |
328 | continue;\r | |
329 | }\r | |
330 | }\r | |
331 | }\r | |
332 | \r | |
333 | for (Index = 0; Index < PartHdr->NumberOfPartitionEntries; Index++) {\r | |
334 | if (CompareGuid (&PartitionEntryBuffer[Index].PartitionTypeGUID, &gEfiPartTypeUnusedGuid)||\r | |
335 | PartitionEntryStatus[Index].OutOfRange ||\r | |
336 | PartitionEntryStatus[Index].Overlap ||\r | |
337 | PartitionEntryStatus[Index].OsSpecific) {\r | |
338 | //\r | |
339 | // Don't use null EFI Partition Entries, Invalid Partition Entries or OS specific\r | |
340 | // partition Entries\r | |
341 | //\r | |
342 | continue;\r | |
343 | }\r | |
344 | \r | |
345 | if (PrivateData->BlockDeviceCount >= PEI_FAT_MAX_BLOCK_DEVICE) {\r | |
346 | break;\r | |
347 | }\r | |
348 | \r | |
349 | Found = TRUE;\r | |
350 | BlockDevPtr = &(PrivateData->BlockDevice[PrivateData->BlockDeviceCount]);\r | |
351 | \r | |
352 | BlockDevPtr->BlockSize = ParentBlockDev->BlockSize;\r | |
353 | BlockDevPtr->LastBlock = PartitionEntryBuffer[Index].EndingLBA;\r | |
354 | BlockDevPtr->IoAlign = ParentBlockDev->IoAlign;\r | |
355 | BlockDevPtr->Logical = TRUE;\r | |
356 | BlockDevPtr->PartitionChecked = FALSE;\r | |
357 | BlockDevPtr->StartingPos = MultU64x32 (\r | |
358 | PartitionEntryBuffer[Index].StartingLBA,\r | |
359 | ParentBlockDev->BlockSize\r | |
360 | );\r | |
361 | BlockDevPtr->ParentDevNo = ParentBlockDevNo;\r | |
362 | \r | |
363 | PrivateData->BlockDeviceCount++;\r | |
364 | \r | |
365 | DEBUG ((DEBUG_INFO, "Find GPT Partition [0x%lx", PartitionEntryBuffer[Index].StartingLBA, BlockDevPtr->LastBlock));\r | |
366 | DEBUG ((DEBUG_INFO, ", 0x%lx]\n", BlockDevPtr->LastBlock));\r | |
367 | DEBUG ((DEBUG_INFO, " BlockSize %x\n", BlockDevPtr->BlockSize));\r | |
368 | }\r | |
369 | \r | |
370 | EXIT:\r | |
371 | if (PartitionEntryBuffer != NULL) {\r | |
372 | FreePages (PartitionEntryBuffer, EFI_SIZE_TO_PAGES ((UINTN)PartitionEntryArraySize));\r | |
373 | }\r | |
374 | \r | |
375 | if (PartitionEntryStatus != NULL) {\r | |
376 | FreePages (PartitionEntryStatus, EFI_SIZE_TO_PAGES (PartHdr->NumberOfPartitionEntries * sizeof (EFI_PARTITION_ENTRY_STATUS)));\r | |
377 | }\r | |
378 | \r | |
379 | return Found;\r | |
380 | }\r | |
381 | \r | |
382 | /**\r | |
383 | The function is used to check GPT structure, include GPT header and GPT entry array.\r | |
384 | \r | |
385 | 1. Check GPT header.\r | |
386 | 2. Check partition entry array.\r | |
387 | 3. Check each partitions.\r | |
388 | \r | |
389 | @param[in] PrivateData The global memory map\r | |
390 | @param[in] ParentBlockDevNo The parent block device\r | |
391 | @param[in] IsPrimary Indicate primary or backup to be check\r | |
392 | \r | |
393 | @retval TRUE Primary or backup GPT structure is valid.\r | |
394 | @retval FALSE Both primary and backup are invalid.\r | |
395 | \r | |
396 | **/\r | |
397 | BOOLEAN\r | |
398 | PartitionCheckGptStructure (\r | |
399 | IN PEI_FAT_PRIVATE_DATA *PrivateData,\r | |
400 | IN UINTN ParentBlockDevNo,\r | |
401 | IN BOOLEAN IsPrimary\r | |
402 | )\r | |
403 | {\r | |
404 | EFI_STATUS Status;\r | |
405 | PEI_FAT_BLOCK_DEVICE *ParentBlockDev;\r | |
406 | EFI_PARTITION_TABLE_HEADER *PartHdr;\r | |
407 | EFI_PEI_LBA GptHeaderLBA;\r | |
408 | \r | |
409 | ParentBlockDev = &(PrivateData->BlockDevice[ParentBlockDevNo]);\r | |
410 | PartHdr = (EFI_PARTITION_TABLE_HEADER *) PrivateData->BlockData;\r | |
411 | \r | |
412 | if (IsPrimary) {\r | |
413 | GptHeaderLBA = PRIMARY_PART_HEADER_LBA;\r | |
414 | } else {\r | |
415 | GptHeaderLBA = ParentBlockDev->LastBlock;\r | |
416 | }\r | |
417 | \r | |
418 | Status = FatReadBlock (\r | |
419 | PrivateData,\r | |
420 | ParentBlockDevNo,\r | |
421 | GptHeaderLBA,\r | |
422 | ParentBlockDev->BlockSize,\r | |
423 | PartHdr\r | |
424 | );\r | |
425 | if (EFI_ERROR (Status)) {\r | |
426 | return FALSE;\r | |
427 | }\r | |
428 | \r | |
429 | if (!PartitionCheckGptHeader (PrivateData, ParentBlockDevNo, IsPrimary, PartHdr)) {\r | |
430 | return FALSE;\r | |
431 | }\r | |
432 | \r | |
433 | if (!PartitionCheckGptEntryArray (PrivateData, ParentBlockDevNo, PartHdr)) {\r | |
434 | return FALSE;\r | |
435 | }\r | |
436 | \r | |
437 | return TRUE;\r | |
438 | }\r | |
439 | \r | |
440 | /**\r | |
441 | This function is used to check protective MBR structure before checking GPT.\r | |
442 | \r | |
443 | @param[in] PrivateData The global memory map\r | |
444 | @param[in] ParentBlockDevNo The parent block device\r | |
445 | \r | |
446 | @retval TRUE Valid protective MBR\r | |
447 | @retval FALSE Invalid MBR\r | |
448 | **/\r | |
449 | BOOLEAN\r | |
450 | PartitionCheckProtectiveMbr (\r | |
451 | IN PEI_FAT_PRIVATE_DATA *PrivateData,\r | |
452 | IN UINTN ParentBlockDevNo\r | |
453 | )\r | |
454 | {\r | |
455 | EFI_STATUS Status;\r | |
456 | MASTER_BOOT_RECORD *ProtectiveMbr;\r | |
457 | MBR_PARTITION_RECORD *MbrPartition;\r | |
458 | PEI_FAT_BLOCK_DEVICE *ParentBlockDev;\r | |
459 | UINTN Index;\r | |
460 | \r | |
461 | ProtectiveMbr = (MASTER_BOOT_RECORD *) PrivateData->BlockData;\r | |
462 | ParentBlockDev = &(PrivateData->BlockDevice[ParentBlockDevNo]);\r | |
463 | \r | |
464 | //\r | |
465 | // Read Protective MBR\r | |
466 | //\r | |
467 | Status = FatReadBlock (\r | |
468 | PrivateData,\r | |
469 | ParentBlockDevNo,\r | |
470 | 0,\r | |
471 | ParentBlockDev->BlockSize,\r | |
472 | ProtectiveMbr\r | |
473 | );\r | |
474 | if (EFI_ERROR (Status)) {\r | |
475 | DEBUG ((DEBUG_ERROR, "GPT Error When Read Protective Mbr From Partition!\n"));\r | |
476 | return FALSE;\r | |
477 | }\r | |
478 | \r | |
479 | if (ProtectiveMbr->Signature != MBR_SIGNATURE) {\r | |
480 | DEBUG ((DEBUG_ERROR, "Protective Mbr Signature is invalid!\n"));\r | |
481 | return FALSE;\r | |
482 | }\r | |
483 | \r | |
484 | //\r | |
485 | // The partition define in UEFI Spec Table 17.\r | |
486 | // Boot Code, Unique MBR Disk Signature, Unknown.\r | |
487 | // These parts will not be used by UEFI, so we skip to check them.\r | |
488 | //\r | |
489 | for (Index = 0; Index < MAX_MBR_PARTITIONS; Index++) {\r | |
490 | MbrPartition = (MBR_PARTITION_RECORD *)&ProtectiveMbr->Partition[Index];\r | |
491 | if (MbrPartition->BootIndicator == 0x00 &&\r | |
492 | MbrPartition->StartSector == 0x02 &&\r | |
493 | MbrPartition->OSIndicator == PMBR_GPT_PARTITION &&\r | |
494 | UNPACK_UINT32 (MbrPartition->StartingLBA) == 1\r | |
495 | ) {\r | |
496 | return TRUE;\r | |
497 | }\r | |
498 | }\r | |
499 | \r | |
500 | DEBUG ((DEBUG_ERROR, "Protective Mbr, All Partition Entry Are Empty!\n"));\r | |
501 | return FALSE;\r | |
502 | }\r | |
503 | \r | |
504 | /**\r | |
505 | This function is used for finding GPT partition on block device.\r | |
506 | As follow UEFI spec we should check protective MBR first and then\r | |
507 | try to check both primary/backup GPT structures.\r | |
508 | \r | |
509 | @param[in] PrivateData The global memory map\r | |
510 | @param[in] ParentBlockDevNo The parent block device\r | |
511 | \r | |
512 | @retval TRUE New partitions are detected and logical block devices\r | |
513 | are added to block device array\r | |
514 | @retval FALSE No new partitions are added\r | |
515 | \r | |
516 | **/\r | |
517 | BOOLEAN\r | |
518 | FatFindGptPartitions (\r | |
519 | IN PEI_FAT_PRIVATE_DATA *PrivateData,\r | |
520 | IN UINTN ParentBlockDevNo\r | |
521 | )\r | |
522 | {\r | |
523 | BOOLEAN Found;\r | |
524 | PEI_FAT_BLOCK_DEVICE *ParentBlockDev;\r | |
525 | \r | |
526 | if (ParentBlockDevNo > PEI_FAT_MAX_BLOCK_DEVICE - 1) {\r | |
527 | return FALSE;\r | |
528 | }\r | |
529 | \r | |
530 | ParentBlockDev = &(PrivateData->BlockDevice[ParentBlockDevNo]);\r | |
531 | if (ParentBlockDev->BlockSize > PEI_FAT_MAX_BLOCK_SIZE) {\r | |
532 | DEBUG ((DEBUG_ERROR, "Device BlockSize %x exceed FAT_MAX_BLOCK_SIZE\n", ParentBlockDev->BlockSize));\r | |
533 | return FALSE;\r | |
534 | }\r | |
535 | \r | |
536 | if (!PartitionCheckProtectiveMbr (PrivateData, ParentBlockDevNo)) {\r | |
537 | return FALSE;\r | |
538 | }\r | |
539 | \r | |
540 | Found = PartitionCheckGptStructure (PrivateData, ParentBlockDevNo, TRUE);\r | |
541 | if (!Found) {\r | |
542 | DEBUG ((DEBUG_ERROR, "Primary GPT Header Error, Try to Check Backup GPT Header!\n"));\r | |
543 | Found = PartitionCheckGptStructure (PrivateData, ParentBlockDevNo, FALSE);\r | |
544 | }\r | |
545 | \r | |
546 | if (Found) {\r | |
547 | ParentBlockDev->PartitionChecked = TRUE;\r | |
548 | }\r | |
549 | \r | |
550 | return Found;\r | |
551 | }\r |