]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/Disk/UdfDxe/FileSystemOperations.c
MdeModulePkg/UdfDxe: Remove negative comparison of unsigned number
[mirror_edk2.git] / MdeModulePkg / Universal / Disk / UdfDxe / FileSystemOperations.c
CommitLineData
99c9b949
PA
1/** @file\r
2 Handle on-disk format and volume structures in UDF/ECMA-167 file systems.\r
3\r
4 Copyright (C) 2014-2017 Paulo Alcantara <pcacjr@zytor.com>\r
5\r
6 This program and the accompanying materials are licensed and made available\r
7 under the terms and conditions of the BSD License which accompanies this\r
8 distribution. The full text of the license may be found at\r
9 http://opensource.org/licenses/bsd-license.php\r
10\r
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT\r
12 WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13**/\r
14\r
15#include "Udf.h"\r
16\r
17EFI_STATUS\r
18FindAnchorVolumeDescriptorPointer (\r
19 IN EFI_BLOCK_IO_PROTOCOL *BlockIo,\r
20 IN EFI_DISK_IO_PROTOCOL *DiskIo,\r
21 OUT UDF_ANCHOR_VOLUME_DESCRIPTOR_POINTER *AnchorPoint\r
22 )\r
23{\r
24 EFI_STATUS Status;\r
b434f753
BD
25 UINT32 BlockSize;\r
26 EFI_LBA EndLBA;\r
27 EFI_LBA DescriptorLBAs[4];\r
99c9b949
PA
28 UINTN Index;\r
29\r
b434f753
BD
30 BlockSize = BlockIo->Media->BlockSize;\r
31 EndLBA = BlockIo->Media->LastBlock;\r
32 DescriptorLBAs[0] = 256;\r
33 DescriptorLBAs[1] = EndLBA - 256;\r
34 DescriptorLBAs[2] = EndLBA;\r
35 DescriptorLBAs[3] = 512;\r
36\r
99c9b949
PA
37 for (Index = 0; Index < ARRAY_SIZE (DescriptorLBAs); Index++) {\r
38 Status = DiskIo->ReadDisk (\r
39 DiskIo,\r
40 BlockIo->Media->MediaId,\r
41 MultU64x32 (DescriptorLBAs[Index], BlockSize),\r
42 sizeof (UDF_ANCHOR_VOLUME_DESCRIPTOR_POINTER),\r
43 (VOID *)AnchorPoint\r
44 );\r
45 if (EFI_ERROR (Status)) {\r
46 return Status;\r
47 }\r
48 //\r
49 // Check if read LBA has a valid AVDP descriptor.\r
50 //\r
51 if (IS_AVDP (AnchorPoint)) {\r
52 return EFI_SUCCESS;\r
53 }\r
54 }\r
55 //\r
56 // No AVDP found.\r
57 //\r
58 return EFI_VOLUME_CORRUPTED;\r
59}\r
60\r
61EFI_STATUS\r
62StartMainVolumeDescriptorSequence (\r
63 IN EFI_BLOCK_IO_PROTOCOL *BlockIo,\r
64 IN EFI_DISK_IO_PROTOCOL *DiskIo,\r
65 IN UDF_ANCHOR_VOLUME_DESCRIPTOR_POINTER *AnchorPoint,\r
66 OUT UDF_VOLUME_INFO *Volume\r
67 )\r
68{\r
69 EFI_STATUS Status;\r
70 UINT32 BlockSize;\r
71 UDF_EXTENT_AD *ExtentAd;\r
72 UINT64 StartingLsn;\r
73 UINT64 EndingLsn;\r
74 VOID *Buffer;\r
75 UDF_LOGICAL_VOLUME_DESCRIPTOR *LogicalVolDesc;\r
76 UDF_PARTITION_DESCRIPTOR *PartitionDesc;\r
77 UINTN Index;\r
78 UINT32 LogicalBlockSize;\r
79\r
80 //\r
81 // We've already found an ADVP on the volume. It contains the extent\r
82 // (MainVolumeDescriptorSequenceExtent) where the Main Volume Descriptor\r
83 // Sequence starts. Therefore, we'll look for Logical Volume Descriptors and\r
84 // Partitions Descriptors and save them in memory, accordingly.\r
85 //\r
86 // Note also that each descriptor will be aligned on a block size (BlockSize)\r
87 // boundary, so we need to read one block at a time.\r
88 //\r
89 BlockSize = BlockIo->Media->BlockSize;\r
90 ExtentAd = &AnchorPoint->MainVolumeDescriptorSequenceExtent;\r
91 StartingLsn = (UINT64)ExtentAd->ExtentLocation;\r
92 EndingLsn = StartingLsn + DivU64x32 (\r
93 (UINT64)ExtentAd->ExtentLength,\r
94 BlockSize\r
95 );\r
96\r
97 Volume->LogicalVolDescs =\r
98 (UDF_LOGICAL_VOLUME_DESCRIPTOR **)AllocateZeroPool (ExtentAd->ExtentLength);\r
99 if (Volume->LogicalVolDescs == NULL) {\r
100 return EFI_OUT_OF_RESOURCES;\r
101 }\r
102\r
103 Volume->PartitionDescs =\r
104 (UDF_PARTITION_DESCRIPTOR **)AllocateZeroPool (ExtentAd->ExtentLength);\r
105 if (Volume->PartitionDescs == NULL) {\r
106 Status = EFI_OUT_OF_RESOURCES;\r
107 goto Error_Alloc_Pds;\r
108 }\r
109\r
110 Buffer = AllocateZeroPool (BlockSize);\r
111 if (Buffer == NULL) {\r
112 Status = EFI_OUT_OF_RESOURCES;\r
113 goto Error_Alloc_Buf;\r
114 }\r
115\r
116 Volume->LogicalVolDescsNo = 0;\r
117 Volume->PartitionDescsNo = 0;\r
118\r
119 while (StartingLsn <= EndingLsn) {\r
120 Status = DiskIo->ReadDisk (\r
121 DiskIo,\r
122 BlockIo->Media->MediaId,\r
123 MultU64x32 (StartingLsn, BlockSize),\r
124 BlockSize,\r
125 Buffer\r
126 );\r
127 if (EFI_ERROR (Status)) {\r
128 goto Error_Read_Disk_Blk;\r
129 }\r
130\r
131 if (IS_TD (Buffer)) {\r
132 //\r
133 // Found a Terminating Descriptor. Stop the sequence then.\r
134 //\r
135 break;\r
136 }\r
137\r
138 if (IS_LVD (Buffer)) {\r
139 //\r
140 // Found a Logical Volume Descriptor.\r
141 //\r
142 LogicalVolDesc =\r
143 (UDF_LOGICAL_VOLUME_DESCRIPTOR *)\r
144 AllocateZeroPool (sizeof (UDF_LOGICAL_VOLUME_DESCRIPTOR));\r
145 if (LogicalVolDesc == NULL) {\r
146 Status = EFI_OUT_OF_RESOURCES;\r
147 goto Error_Alloc_Lvd;\r
148 }\r
149\r
150 CopyMem ((VOID *)LogicalVolDesc, Buffer,\r
151 sizeof (UDF_LOGICAL_VOLUME_DESCRIPTOR));\r
152 Volume->LogicalVolDescs[Volume->LogicalVolDescsNo++] = LogicalVolDesc;\r
153 } else if (IS_PD (Buffer)) {\r
154 //\r
155 // Found a Partition Descriptor.\r
156 //\r
157 PartitionDesc =\r
158 (UDF_PARTITION_DESCRIPTOR *)\r
159 AllocateZeroPool (sizeof (UDF_PARTITION_DESCRIPTOR));\r
160 if (PartitionDesc == NULL) {\r
161 Status = EFI_OUT_OF_RESOURCES;\r
162 goto Error_Alloc_Pd;\r
163 }\r
164\r
165 CopyMem ((VOID *)PartitionDesc, Buffer,\r
166 sizeof (UDF_PARTITION_DESCRIPTOR));\r
167 Volume->PartitionDescs[Volume->PartitionDescsNo++] = PartitionDesc;\r
168 }\r
169\r
170 StartingLsn++;\r
171 }\r
172\r
173 //\r
174 // When an UDF volume (revision 2.00 or higher) contains a File Entry rather\r
175 // than an Extended File Entry (which is not recommended as per spec), we need\r
176 // to make sure the size of a FE will be _at least_ 2048\r
177 // (UDF_LOGICAL_SECTOR_SIZE) bytes long to keep backward compatibility.\r
178 //\r
179 LogicalBlockSize = LV_BLOCK_SIZE (Volume, UDF_DEFAULT_LV_NUM);\r
180 if (LogicalBlockSize >= UDF_LOGICAL_SECTOR_SIZE) {\r
181 Volume->FileEntrySize = LogicalBlockSize;\r
182 } else {\r
183 Volume->FileEntrySize = UDF_LOGICAL_SECTOR_SIZE;\r
184 }\r
185\r
186 FreePool (Buffer);\r
187\r
188 return EFI_SUCCESS;\r
189\r
190Error_Alloc_Pd:\r
191Error_Alloc_Lvd:\r
192 for (Index = 0; Index < Volume->PartitionDescsNo; Index++) {\r
193 FreePool ((VOID *)Volume->PartitionDescs[Index]);\r
194 }\r
195\r
196 for (Index = 0; Index < Volume->LogicalVolDescsNo; Index++) {\r
197 FreePool ((VOID *)Volume->LogicalVolDescs[Index]);\r
198 }\r
199\r
200Error_Read_Disk_Blk:\r
201 FreePool (Buffer);\r
202\r
203Error_Alloc_Buf:\r
204 FreePool ((VOID *)Volume->PartitionDescs);\r
205 Volume->PartitionDescs = NULL;\r
206\r
207Error_Alloc_Pds:\r
208 FreePool ((VOID *)Volume->LogicalVolDescs);\r
209 Volume->LogicalVolDescs = NULL;\r
210\r
211 return Status;\r
212}\r
213\r
214//\r
215// Return a Partition Descriptor given a Long Allocation Descriptor. This is\r
216// necessary to calculate the right extent (LongAd) offset which is added up\r
217// with partition's starting location.\r
218//\r
219UDF_PARTITION_DESCRIPTOR *\r
220GetPdFromLongAd (\r
221 IN UDF_VOLUME_INFO *Volume,\r
222 IN UDF_LONG_ALLOCATION_DESCRIPTOR *LongAd\r
223 )\r
224{\r
225 UDF_LOGICAL_VOLUME_DESCRIPTOR *LogicalVolDesc;\r
226 UINTN Index;\r
227 UDF_PARTITION_DESCRIPTOR *PartitionDesc;\r
228 UINT16 PartitionNum;\r
229\r
230 LogicalVolDesc = Volume->LogicalVolDescs[UDF_DEFAULT_LV_NUM];\r
231\r
232 switch (LV_UDF_REVISION (LogicalVolDesc)) {\r
233 case 0x0102:\r
234 //\r
235 // As per UDF 1.02 specification:\r
236 //\r
237 // There shall be exactly one prevailing Logical Volume Descriptor recorded\r
238 // per Volume Set. The Partition Maps field shall contain only Type 1\r
239 // Partition Maps.\r
240 //\r
241 PartitionNum = *(UINT16 *)((UINTN)&LogicalVolDesc->PartitionMaps[4]);\r
242 break;\r
243 case 0x0150:\r
244 //\r
245 // Ensure Type 1 Partition map. Other types aren't supported in this\r
246 // implementation.\r
247 //\r
248 if (LogicalVolDesc->PartitionMaps[0] != 1 ||\r
249 LogicalVolDesc->PartitionMaps[1] != 6) {\r
250 return NULL;\r
251 }\r
252 PartitionNum = *(UINT16 *)((UINTN)&LogicalVolDesc->PartitionMaps[4]);\r
253 break;\r
254 case 0x0260:\r
255 //\r
256 // Fall through.\r
257 //\r
258 default:\r
259 PartitionNum = LongAd->ExtentLocation.PartitionReferenceNumber;\r
260 break;\r
261 }\r
262\r
263 for (Index = 0; Index < Volume->PartitionDescsNo; Index++) {\r
264 PartitionDesc = Volume->PartitionDescs[Index];\r
265 if (PartitionDesc->PartitionNumber == PartitionNum) {\r
266 return PartitionDesc;\r
267 }\r
268 }\r
269\r
270 return NULL;\r
271}\r
272\r
273//\r
274// Return logical sector number of a given Long Allocation Descriptor.\r
275//\r
276UINT64\r
277GetLongAdLsn (\r
278 IN UDF_VOLUME_INFO *Volume,\r
279 IN UDF_LONG_ALLOCATION_DESCRIPTOR *LongAd\r
280 )\r
281{\r
282 UDF_PARTITION_DESCRIPTOR *PartitionDesc;\r
283\r
284 PartitionDesc = GetPdFromLongAd (Volume, LongAd);\r
285 ASSERT (PartitionDesc != NULL);\r
286\r
287 return (UINT64)PartitionDesc->PartitionStartingLocation +\r
288 LongAd->ExtentLocation.LogicalBlockNumber;\r
289}\r
290\r
291//\r
292// Return logical sector number of a given Short Allocation Descriptor.\r
293//\r
294UINT64\r
295GetShortAdLsn (\r
296 IN UDF_PARTITION_DESCRIPTOR *PartitionDesc,\r
297 IN UDF_SHORT_ALLOCATION_DESCRIPTOR *ShortAd\r
298 )\r
299{\r
300 return (UINT64)PartitionDesc->PartitionStartingLocation +\r
301 ShortAd->ExtentPosition;\r
302}\r
303\r
304//\r
305// Find File Set Descriptor of a given Logical Volume Descriptor.\r
306//\r
307// The found FSD will contain the extent (LogicalVolumeContentsUse) where our\r
308// root directory is.\r
309//\r
310EFI_STATUS\r
311FindFileSetDescriptor (\r
312 IN EFI_BLOCK_IO_PROTOCOL *BlockIo,\r
313 IN EFI_DISK_IO_PROTOCOL *DiskIo,\r
314 IN UDF_VOLUME_INFO *Volume,\r
315 IN UINTN LogicalVolDescNum,\r
316 OUT UDF_FILE_SET_DESCRIPTOR *FileSetDesc\r
317 )\r
318{\r
319 EFI_STATUS Status;\r
320 UINT64 Lsn;\r
321 UDF_LOGICAL_VOLUME_DESCRIPTOR *LogicalVolDesc;\r
322\r
323 LogicalVolDesc = Volume->LogicalVolDescs[LogicalVolDescNum];\r
324 Lsn = GetLongAdLsn (Volume, &LogicalVolDesc->LogicalVolumeContentsUse);\r
325\r
326 //\r
327 // Read extent (Long Ad).\r
328 //\r
329 Status = DiskIo->ReadDisk (\r
330 DiskIo,\r
331 BlockIo->Media->MediaId,\r
332 MultU64x32 (Lsn, LogicalVolDesc->LogicalBlockSize),\r
333 sizeof (UDF_FILE_SET_DESCRIPTOR),\r
334 (VOID *)FileSetDesc\r
335 );\r
336 if (EFI_ERROR (Status)) {\r
337 return Status;\r
338 }\r
339\r
340 //\r
341 // Check if the read extent contains a valid FSD's tag identifier.\r
342 //\r
343 if (!IS_FSD (FileSetDesc)) {\r
344 return EFI_VOLUME_CORRUPTED;\r
345 }\r
346\r
347 return EFI_SUCCESS;\r
348}\r
349\r
350//\r
351// Get all File Set Descriptors for each Logical Volume Descriptor.\r
352//\r
353EFI_STATUS\r
354GetFileSetDescriptors (\r
355 IN EFI_BLOCK_IO_PROTOCOL *BlockIo,\r
356 IN EFI_DISK_IO_PROTOCOL *DiskIo,\r
357 IN OUT UDF_VOLUME_INFO *Volume\r
358 )\r
359{\r
360 EFI_STATUS Status;\r
361 UINTN Index;\r
362 UDF_FILE_SET_DESCRIPTOR *FileSetDesc;\r
363 UINTN Count;\r
364\r
365 Volume->FileSetDescs =\r
366 (UDF_FILE_SET_DESCRIPTOR **)AllocateZeroPool (\r
367 Volume->LogicalVolDescsNo * sizeof (UDF_FILE_SET_DESCRIPTOR));\r
368 if (Volume->FileSetDescs == NULL) {\r
369 return EFI_OUT_OF_RESOURCES;\r
370 }\r
371\r
372 for (Index = 0; Index < Volume->LogicalVolDescsNo; Index++) {\r
373 FileSetDesc = AllocateZeroPool (sizeof (UDF_FILE_SET_DESCRIPTOR));\r
374 if (FileSetDesc == NULL) {\r
375 Status = EFI_OUT_OF_RESOURCES;\r
376 goto Error_Alloc_Fsd;\r
377 }\r
378\r
379 //\r
380 // Find a FSD for this LVD.\r
381 //\r
382 Status = FindFileSetDescriptor (\r
383 BlockIo,\r
384 DiskIo,\r
385 Volume,\r
386 Index,\r
387 FileSetDesc\r
388 );\r
389 if (EFI_ERROR (Status)) {\r
390 goto Error_Find_Fsd;\r
391 }\r
392\r
393 //\r
394 // Got one. Save it.\r
395 //\r
396 Volume->FileSetDescs[Index] = FileSetDesc;\r
397 }\r
398\r
399 Volume->FileSetDescsNo = Volume->LogicalVolDescsNo;\r
400 return EFI_SUCCESS;\r
401\r
402Error_Find_Fsd:\r
403 Count = Index + 1;\r
404 for (Index = 0; Index < Count; Index++) {\r
405 FreePool ((VOID *)Volume->FileSetDescs[Index]);\r
406 }\r
407\r
408 FreePool ((VOID *)Volume->FileSetDescs);\r
409 Volume->FileSetDescs = NULL;\r
410\r
411Error_Alloc_Fsd:\r
412 return Status;\r
413}\r
414\r
415//\r
416// Read Volume and File Structure on an UDF file system.\r
417//\r
418EFI_STATUS\r
419ReadVolumeFileStructure (\r
420 IN EFI_BLOCK_IO_PROTOCOL *BlockIo,\r
421 IN EFI_DISK_IO_PROTOCOL *DiskIo,\r
422 OUT UDF_VOLUME_INFO *Volume\r
423 )\r
424{\r
425 EFI_STATUS Status;\r
426 UDF_ANCHOR_VOLUME_DESCRIPTOR_POINTER AnchorPoint;\r
427\r
428 //\r
429 // Find an AVDP.\r
430 //\r
431 Status = FindAnchorVolumeDescriptorPointer (\r
432 BlockIo,\r
433 DiskIo,\r
434 &AnchorPoint\r
435 );\r
436 if (EFI_ERROR (Status)) {\r
437 return Status;\r
438 }\r
439\r
440 //\r
441 // AVDP has been found. Start MVDS.\r
442 //\r
443 Status = StartMainVolumeDescriptorSequence (\r
444 BlockIo,\r
445 DiskIo,\r
446 &AnchorPoint,\r
447 Volume\r
448 );\r
449 if (EFI_ERROR (Status)) {\r
450 return Status;\r
451 }\r
452\r
453 return Status;\r
454}\r
455\r
456//\r
457// Calculate length of a given File Identifier Descriptor.\r
458//\r
459UINT64\r
460GetFidDescriptorLength (\r
461 IN UDF_FILE_IDENTIFIER_DESCRIPTOR *FileIdentifierDesc\r
462 )\r
463{\r
464 return (UINT64)(\r
465 (INTN)((OFFSET_OF (UDF_FILE_IDENTIFIER_DESCRIPTOR, Data[0]) + 3 +\r
466 FileIdentifierDesc->LengthOfFileIdentifier +\r
467 FileIdentifierDesc->LengthOfImplementationUse) >> 2) << 2\r
468 );\r
469}\r
470\r
471//\r
472// Duplicate a given File Identifier Descriptor.\r
473//\r
474VOID\r
475DuplicateFid (\r
476 IN UDF_FILE_IDENTIFIER_DESCRIPTOR *FileIdentifierDesc,\r
477 OUT UDF_FILE_IDENTIFIER_DESCRIPTOR **NewFileIdentifierDesc\r
478 )\r
479{\r
480 *NewFileIdentifierDesc =\r
481 (UDF_FILE_IDENTIFIER_DESCRIPTOR *)AllocateCopyPool (\r
0b4c8f00 482 (UINTN) GetFidDescriptorLength (FileIdentifierDesc), FileIdentifierDesc);\r
99c9b949
PA
483}\r
484\r
485//\r
486// Duplicate either a given File Entry or a given Extended File Entry.\r
487//\r
488VOID\r
489DuplicateFe (\r
490 IN EFI_BLOCK_IO_PROTOCOL *BlockIo,\r
491 IN UDF_VOLUME_INFO *Volume,\r
492 IN VOID *FileEntry,\r
493 OUT VOID **NewFileEntry\r
494 )\r
495{\r
496 *NewFileEntry = AllocateCopyPool (Volume->FileEntrySize, FileEntry);\r
497}\r
498\r
499//\r
500// Get raw data + length of a given File Entry or Extended File Entry.\r
501//\r
502// The file's recorded data can contain either real file content (inline) or\r
503// a sequence of extents (or Allocation Descriptors) which tells where file's\r
504// content is stored in.\r
505//\r
506// NOTE: The FE/EFE can be thought it was an inode.\r
507//\r
508VOID\r
509GetFileEntryData (\r
510 IN VOID *FileEntryData,\r
511 OUT VOID **Data,\r
512 OUT UINT64 *Length\r
513 )\r
514{\r
515 UDF_EXTENDED_FILE_ENTRY *ExtendedFileEntry;\r
516 UDF_FILE_ENTRY *FileEntry;\r
517\r
518 if (IS_EFE (FileEntryData)) {\r
519 ExtendedFileEntry = (UDF_EXTENDED_FILE_ENTRY *)FileEntryData;\r
520\r
521 *Length = ExtendedFileEntry->InformationLength;\r
522 *Data = (VOID *)((UINT8 *)ExtendedFileEntry->Data +\r
523 ExtendedFileEntry->LengthOfExtendedAttributes);\r
524 } else if (IS_FE (FileEntryData)) {\r
525 FileEntry = (UDF_FILE_ENTRY *)FileEntryData;\r
526\r
527 *Length = FileEntry->InformationLength;\r
528 *Data = (VOID *)((UINT8 *)FileEntry->Data +\r
529 FileEntry->LengthOfExtendedAttributes);\r
530 }\r
531}\r
532\r
533//\r
534// Get Allocation Descriptors' data information from a given FE/EFE.\r
535//\r
536VOID\r
537GetAdsInformation (\r
538 IN VOID *FileEntryData,\r
539 OUT VOID **AdsData,\r
540 OUT UINT64 *Length\r
541 )\r
542{\r
543 UDF_EXTENDED_FILE_ENTRY *ExtendedFileEntry;\r
544 UDF_FILE_ENTRY *FileEntry;\r
545\r
546 if (IS_EFE (FileEntryData)) {\r
547 ExtendedFileEntry = (UDF_EXTENDED_FILE_ENTRY *)FileEntryData;\r
548\r
549 *Length = ExtendedFileEntry->LengthOfAllocationDescriptors;\r
550 *AdsData = (VOID *)((UINT8 *)ExtendedFileEntry->Data +\r
551 ExtendedFileEntry->LengthOfExtendedAttributes);\r
552 } else if (IS_FE (FileEntryData)) {\r
553 FileEntry = (UDF_FILE_ENTRY *)FileEntryData;\r
554\r
555 *Length = FileEntry->LengthOfAllocationDescriptors;\r
556 *AdsData = (VOID *)((UINT8 *)FileEntry->Data +\r
557 FileEntry->LengthOfExtendedAttributes);\r
558 }\r
559}\r
560\r
561//\r
562// Read next Long Allocation Descriptor from a given file's data.\r
563//\r
564EFI_STATUS\r
565GetLongAdFromAds (\r
566 IN VOID *Data,\r
567 IN OUT UINT64 *Offset,\r
568 IN UINT64 Length,\r
569 OUT UDF_LONG_ALLOCATION_DESCRIPTOR **FoundLongAd\r
570 )\r
571{\r
572 UDF_LONG_ALLOCATION_DESCRIPTOR *LongAd;\r
573 UDF_EXTENT_FLAGS ExtentFlags;\r
574\r
575 for (;;) {\r
576 if (*Offset >= Length) {\r
577 //\r
578 // No more Long Allocation Descriptors.\r
579 //\r
580 return EFI_DEVICE_ERROR;\r
581 }\r
582\r
583 LongAd =\r
584 (UDF_LONG_ALLOCATION_DESCRIPTOR *)((UINT8 *)Data + *Offset);\r
585\r
586 //\r
587 // If it's either an indirect AD (Extended Alllocation Descriptor) or an\r
588 // allocated AD, then return it.\r
589 //\r
590 ExtentFlags = GET_EXTENT_FLAGS (LONG_ADS_SEQUENCE, LongAd);\r
591 if (ExtentFlags == EXTENT_IS_NEXT_EXTENT ||\r
592 ExtentFlags == EXTENT_RECORDED_AND_ALLOCATED) {\r
593 break;\r
594 }\r
595\r
596 //\r
597 // This AD is either not recorded but allocated, or not recorded and not\r
598 // allocated. Skip it.\r
599 //\r
600 *Offset += AD_LENGTH (LONG_ADS_SEQUENCE);\r
601 }\r
602\r
603 *FoundLongAd = LongAd;\r
604\r
605 return EFI_SUCCESS;\r
606}\r
607\r
608//\r
609// Read next Short Allocation Descriptor from a given file's data.\r
610//\r
611EFI_STATUS\r
612GetShortAdFromAds (\r
613 IN VOID *Data,\r
614 IN OUT UINT64 *Offset,\r
615 IN UINT64 Length,\r
616 OUT UDF_SHORT_ALLOCATION_DESCRIPTOR **FoundShortAd\r
617 )\r
618{\r
619 UDF_SHORT_ALLOCATION_DESCRIPTOR *ShortAd;\r
620 UDF_EXTENT_FLAGS ExtentFlags;\r
621\r
622 for (;;) {\r
623 if (*Offset >= Length) {\r
624 //\r
625 // No more Short Allocation Descriptors.\r
626 //\r
627 return EFI_DEVICE_ERROR;\r
628 }\r
629\r
630 ShortAd =\r
631 (UDF_SHORT_ALLOCATION_DESCRIPTOR *)((UINT8 *)Data + *Offset);\r
632\r
633 //\r
634 // If it's either an indirect AD (Extended Alllocation Descriptor) or an\r
635 // allocated AD, then return it.\r
636 //\r
637 ExtentFlags = GET_EXTENT_FLAGS (SHORT_ADS_SEQUENCE, ShortAd);\r
638 if (ExtentFlags == EXTENT_IS_NEXT_EXTENT ||\r
639 ExtentFlags == EXTENT_RECORDED_AND_ALLOCATED) {\r
640 break;\r
641 }\r
642\r
643 //\r
644 // This AD is either not recorded but allocated, or not recorded and not\r
645 // allocated. Skip it.\r
646 //\r
647 *Offset += AD_LENGTH (SHORT_ADS_SEQUENCE);\r
648 }\r
649\r
650 *FoundShortAd = ShortAd;\r
651\r
652 return EFI_SUCCESS;\r
653}\r
654\r
655//\r
656// Get either a Short Allocation Descriptor or a Long Allocation Descriptor from\r
657// file's data.\r
658//\r
659EFI_STATUS\r
660GetAllocationDescriptor (\r
661 IN UDF_FE_RECORDING_FLAGS RecordingFlags,\r
662 IN VOID *Data,\r
663 IN OUT UINT64 *Offset,\r
664 IN UINT64 Length,\r
665 OUT VOID **FoundAd\r
666 )\r
667{\r
668 if (RecordingFlags == LONG_ADS_SEQUENCE) {\r
669 return GetLongAdFromAds (\r
670 Data,\r
671 Offset,\r
672 Length,\r
673 (UDF_LONG_ALLOCATION_DESCRIPTOR **)FoundAd\r
674 );\r
675 } else if (RecordingFlags == SHORT_ADS_SEQUENCE) {\r
676 return GetShortAdFromAds (\r
677 Data,\r
678 Offset,\r
679 Length,\r
680 (UDF_SHORT_ALLOCATION_DESCRIPTOR **)FoundAd\r
681 );\r
682 }\r
683\r
684 return EFI_DEVICE_ERROR;\r
685}\r
686\r
687//\r
688// Return logical sector number of either Short or Long Allocation Descriptor.\r
689//\r
690UINT64\r
691GetAllocationDescriptorLsn (\r
692 IN UDF_FE_RECORDING_FLAGS RecordingFlags,\r
693 IN UDF_VOLUME_INFO *Volume,\r
694 IN UDF_LONG_ALLOCATION_DESCRIPTOR *ParentIcb,\r
695 IN VOID *Ad\r
696 )\r
697{\r
698 if (RecordingFlags == LONG_ADS_SEQUENCE) {\r
699 return GetLongAdLsn (Volume, (UDF_LONG_ALLOCATION_DESCRIPTOR *)Ad);\r
700 } else if (RecordingFlags == SHORT_ADS_SEQUENCE) {\r
701 return GetShortAdLsn (\r
702 GetPdFromLongAd (Volume, ParentIcb),\r
703 (UDF_SHORT_ALLOCATION_DESCRIPTOR *)Ad\r
704 );\r
705 }\r
706\r
707 return 0;\r
708}\r
709\r
710//\r
711// Return offset + length of a given indirect Allocation Descriptor (AED).\r
712//\r
713EFI_STATUS\r
714GetAedAdsOffset (\r
715 IN EFI_BLOCK_IO_PROTOCOL *BlockIo,\r
716 IN EFI_DISK_IO_PROTOCOL *DiskIo,\r
717 IN UDF_VOLUME_INFO *Volume,\r
718 IN UDF_LONG_ALLOCATION_DESCRIPTOR *ParentIcb,\r
719 IN UDF_FE_RECORDING_FLAGS RecordingFlags,\r
720 IN VOID *Ad,\r
721 OUT UINT64 *Offset,\r
722 OUT UINT64 *Length\r
723 )\r
724{\r
725 EFI_STATUS Status;\r
726 UINT32 ExtentLength;\r
727 UINT64 Lsn;\r
728 VOID *Data;\r
729 UINT32 LogicalBlockSize;\r
730 UDF_ALLOCATION_EXTENT_DESCRIPTOR *AllocExtDesc;\r
731\r
732 ExtentLength = GET_EXTENT_LENGTH (RecordingFlags, Ad);\r
733 Lsn = GetAllocationDescriptorLsn (RecordingFlags,\r
734 Volume,\r
735 ParentIcb,\r
736 Ad);\r
737\r
738 Data = AllocatePool (ExtentLength);\r
739 if (Data == NULL) {\r
740 return EFI_OUT_OF_RESOURCES;\r
741 }\r
742\r
743 LogicalBlockSize = LV_BLOCK_SIZE (Volume, UDF_DEFAULT_LV_NUM);\r
744\r
745 //\r
746 // Read extent.\r
747 //\r
748 Status = DiskIo->ReadDisk (\r
749 DiskIo,\r
750 BlockIo->Media->MediaId,\r
751 MultU64x32 (Lsn, LogicalBlockSize),\r
752 ExtentLength,\r
753 Data\r
754 );\r
755 if (EFI_ERROR (Status)) {\r
756 goto Exit;\r
757 }\r
758\r
759 //\r
760 // Check if read extent contains a valid tag identifier for AED.\r
761 //\r
762 AllocExtDesc = (UDF_ALLOCATION_EXTENT_DESCRIPTOR *)Data;\r
763 if (!IS_AED (AllocExtDesc)) {\r
764 Status = EFI_VOLUME_CORRUPTED;\r
765 goto Exit;\r
766 }\r
767\r
768 //\r
769 // Get AED's block offset and its length.\r
770 //\r
771 *Offset = MultU64x32 (Lsn, LogicalBlockSize) +\r
772 sizeof (UDF_ALLOCATION_EXTENT_DESCRIPTOR);\r
773 *Length = AllocExtDesc->LengthOfAllocationDescriptors;\r
774\r
775Exit:\r
776 FreePool (Data);\r
777\r
778 return Status;\r
779}\r
780\r
781//\r
782// Read Allocation Extent Descriptor into memory.\r
783//\r
784EFI_STATUS\r
785GetAedAdsData (\r
786 IN EFI_BLOCK_IO_PROTOCOL *BlockIo,\r
787 IN EFI_DISK_IO_PROTOCOL *DiskIo,\r
788 IN UDF_VOLUME_INFO *Volume,\r
789 IN UDF_LONG_ALLOCATION_DESCRIPTOR *ParentIcb,\r
790 IN UDF_FE_RECORDING_FLAGS RecordingFlags,\r
791 IN VOID *Ad,\r
792 OUT VOID **Data,\r
793 OUT UINT64 *Length\r
794 )\r
795{\r
796 EFI_STATUS Status;\r
797 UINT64 Offset;\r
798\r
799 //\r
800 // Get AED's offset + length.\r
801 //\r
802 Status = GetAedAdsOffset (\r
803 BlockIo,\r
804 DiskIo,\r
805 Volume,\r
806 ParentIcb,\r
807 RecordingFlags,\r
808 Ad,\r
809 &Offset,\r
810 Length\r
811 );\r
812 if (EFI_ERROR (Status)) {\r
813 return Status;\r
814 }\r
815\r
816 //\r
817 // Allocate buffer to read in AED's data.\r
818 //\r
0b4c8f00 819 *Data = AllocatePool ((UINTN) (*Length));\r
99c9b949
PA
820 if (*Data == NULL) {\r
821 return EFI_OUT_OF_RESOURCES;\r
822 }\r
823\r
824 return DiskIo->ReadDisk (\r
825 DiskIo,\r
826 BlockIo->Media->MediaId,\r
827 Offset,\r
0b4c8f00 828 (UINTN) (*Length),\r
99c9b949
PA
829 *Data\r
830 );\r
831}\r
832\r
833//\r
834// Function used to serialise reads of Allocation Descriptors.\r
835//\r
836EFI_STATUS\r
837GrowUpBufferToNextAd (\r
838 IN UDF_FE_RECORDING_FLAGS RecordingFlags,\r
839 IN VOID *Ad,\r
840 IN OUT VOID **Buffer,\r
841 IN UINT64 Length\r
842 )\r
843{\r
844 UINT32 ExtentLength;\r
845\r
846 ExtentLength = GET_EXTENT_LENGTH (RecordingFlags, Ad);\r
847\r
848 if (*Buffer == NULL) {\r
849 *Buffer = AllocatePool (ExtentLength);\r
850 if (*Buffer == NULL) {\r
851 return EFI_OUT_OF_RESOURCES;\r
852 }\r
853 } else {\r
0b4c8f00 854 *Buffer = ReallocatePool ((UINTN) Length, (UINTN) (Length + ExtentLength), *Buffer);\r
99c9b949
PA
855 if (*Buffer == NULL) {\r
856 return EFI_OUT_OF_RESOURCES;\r
857 }\r
858 }\r
859\r
860 return EFI_SUCCESS;\r
861}\r
862\r
863//\r
864// Read data or size of either a File Entry or an Extended File Entry.\r
865//\r
866EFI_STATUS\r
867ReadFile (\r
868 IN EFI_BLOCK_IO_PROTOCOL *BlockIo,\r
869 IN EFI_DISK_IO_PROTOCOL *DiskIo,\r
870 IN UDF_VOLUME_INFO *Volume,\r
871 IN UDF_LONG_ALLOCATION_DESCRIPTOR *ParentIcb,\r
872 IN VOID *FileEntryData,\r
873 IN OUT UDF_READ_FILE_INFO *ReadFileInfo\r
874 )\r
875{\r
876 EFI_STATUS Status;\r
877 UINT32 LogicalBlockSize;\r
878 VOID *Data;\r
879 UINT64 Length;\r
880 VOID *Ad;\r
881 UINT64 AdOffset;\r
882 UINT64 Lsn;\r
883 BOOLEAN DoFreeAed;\r
884 UINT64 FilePosition;\r
885 UINT64 Offset;\r
886 UINT64 DataOffset;\r
887 UINT64 BytesLeft;\r
888 UINT64 DataLength;\r
889 BOOLEAN FinishedSeeking;\r
890 UINT32 ExtentLength;\r
891 UDF_FE_RECORDING_FLAGS RecordingFlags;\r
892\r
893 LogicalBlockSize = LV_BLOCK_SIZE (Volume, UDF_DEFAULT_LV_NUM);\r
894 DoFreeAed = FALSE;\r
895\r
896 switch (ReadFileInfo->Flags) {\r
897 case READ_FILE_GET_FILESIZE:\r
898 case READ_FILE_ALLOCATE_AND_READ:\r
899 //\r
900 // Initialise ReadFileInfo structure for either getting file size, or\r
901 // reading file's recorded data.\r
902 //\r
903 ReadFileInfo->ReadLength = 0;\r
904 ReadFileInfo->FileData = NULL;\r
905 break;\r
906 case READ_FILE_SEEK_AND_READ:\r
907 //\r
908 // About to seek a file and/or read its data.\r
909 //\r
910 Length = ReadFileInfo->FileSize - ReadFileInfo->FilePosition;\r
911 if (ReadFileInfo->FileDataSize > Length) {\r
912 //\r
913 // About to read beyond the EOF -- truncate it.\r
914 //\r
915 ReadFileInfo->FileDataSize = Length;\r
916 }\r
917\r
918 //\r
919 // Initialise data to start seeking and/or reading a file.\r
920 //\r
921 BytesLeft = ReadFileInfo->FileDataSize;\r
922 DataOffset = 0;\r
923 FilePosition = 0;\r
924 FinishedSeeking = FALSE;\r
925\r
926 break;\r
927 }\r
928\r
929 RecordingFlags = GET_FE_RECORDING_FLAGS (FileEntryData);\r
930 switch (RecordingFlags) {\r
931 case INLINE_DATA:\r
932 //\r
933 // There are no extents for this FE/EFE. All data is inline.\r
934 //\r
935 GetFileEntryData (FileEntryData, &Data, &Length);\r
936\r
937 if (ReadFileInfo->Flags == READ_FILE_GET_FILESIZE) {\r
938 ReadFileInfo->ReadLength = Length;\r
939 } else if (ReadFileInfo->Flags == READ_FILE_ALLOCATE_AND_READ) {\r
940 //\r
941 // Allocate buffer for starting read data.\r
942 //\r
0b4c8f00 943 ReadFileInfo->FileData = AllocatePool ((UINTN) Length);\r
99c9b949
PA
944 if (ReadFileInfo->FileData == NULL) {\r
945 return EFI_OUT_OF_RESOURCES;\r
946 }\r
947\r
948 //\r
949 // Read all inline data into ReadFileInfo->FileData\r
950 //\r
0b4c8f00 951 CopyMem (ReadFileInfo->FileData, Data, (UINTN) Length);\r
99c9b949
PA
952 ReadFileInfo->ReadLength = Length;\r
953 } else if (ReadFileInfo->Flags == READ_FILE_SEEK_AND_READ) {\r
954 //\r
955 // If FilePosition is non-zero, seek file to FilePosition, read\r
956 // FileDataSize bytes and then updates FilePosition.\r
957 //\r
958 CopyMem (\r
959 ReadFileInfo->FileData,\r
960 (VOID *)((UINT8 *)Data + ReadFileInfo->FilePosition),\r
0b4c8f00 961 (UINTN) ReadFileInfo->FileDataSize\r
99c9b949
PA
962 );\r
963\r
964 ReadFileInfo->FilePosition += ReadFileInfo->FileDataSize;\r
a3240c63
LE
965 } else {\r
966 ASSERT (FALSE);\r
967 return EFI_INVALID_PARAMETER;\r
99c9b949
PA
968 }\r
969\r
131fd40f 970 Status = EFI_SUCCESS;\r
99c9b949 971 break;\r
131fd40f 972\r
99c9b949
PA
973 case LONG_ADS_SEQUENCE:\r
974 case SHORT_ADS_SEQUENCE:\r
975 //\r
976 // This FE/EFE contains a run of Allocation Descriptors. Get data + size\r
977 // for start reading them out.\r
978 //\r
979 GetAdsInformation (FileEntryData, &Data, &Length);\r
980 AdOffset = 0;\r
981\r
982 for (;;) {\r
983 //\r
984 // Read AD.\r
985 //\r
986 Status = GetAllocationDescriptor (\r
987 RecordingFlags,\r
988 Data,\r
989 &AdOffset,\r
990 Length,\r
991 &Ad\r
992 );\r
993 if (Status == EFI_DEVICE_ERROR) {\r
994 Status = EFI_SUCCESS;\r
995 goto Done;\r
996 }\r
997\r
998 //\r
999 // Check if AD is an indirect AD. If so, read Allocation Extent\r
1000 // Descriptor and its extents (ADs).\r
1001 //\r
1002 if (GET_EXTENT_FLAGS (RecordingFlags, Ad) == EXTENT_IS_NEXT_EXTENT) {\r
1003 if (!DoFreeAed) {\r
1004 DoFreeAed = TRUE;\r
1005 } else {\r
1006 FreePool (Data);\r
1007 }\r
1008\r
1009 Status = GetAedAdsData (\r
1010 BlockIo,\r
1011 DiskIo,\r
1012 Volume,\r
1013 ParentIcb,\r
1014 RecordingFlags,\r
1015 Ad,\r
1016 &Data,\r
1017 &Length\r
1018 );\r
1019 if (EFI_ERROR (Status)) {\r
1020 goto Error_Get_Aed;\r
1021 }\r
1022\r
1023 AdOffset = 0;\r
1024 continue;\r
1025 }\r
1026\r
1027 ExtentLength = GET_EXTENT_LENGTH (RecordingFlags, Ad);\r
1028\r
1029 Lsn = GetAllocationDescriptorLsn (RecordingFlags,\r
1030 Volume,\r
1031 ParentIcb,\r
1032 Ad);\r
1033\r
1034 switch (ReadFileInfo->Flags) {\r
1035 case READ_FILE_GET_FILESIZE:\r
1036 ReadFileInfo->ReadLength += ExtentLength;\r
1037 break;\r
1038 case READ_FILE_ALLOCATE_AND_READ:\r
1039 //\r
1040 // Increase FileData (if necessary) to read next extent.\r
1041 //\r
1042 Status = GrowUpBufferToNextAd (\r
1043 RecordingFlags,\r
1044 Ad,\r
1045 &ReadFileInfo->FileData,\r
1046 ReadFileInfo->ReadLength\r
1047 );\r
1048 if (EFI_ERROR (Status)) {\r
1049 goto Error_Alloc_Buffer_To_Next_Ad;\r
1050 }\r
1051\r
1052 //\r
1053 // Read extent's data into FileData.\r
1054 //\r
1055 Status = DiskIo->ReadDisk (\r
1056 DiskIo,\r
1057 BlockIo->Media->MediaId,\r
1058 MultU64x32 (Lsn, LogicalBlockSize),\r
1059 ExtentLength,\r
1060 (VOID *)((UINT8 *)ReadFileInfo->FileData +\r
1061 ReadFileInfo->ReadLength)\r
1062 );\r
1063 if (EFI_ERROR (Status)) {\r
1064 goto Error_Read_Disk_Blk;\r
1065 }\r
1066\r
1067 ReadFileInfo->ReadLength += ExtentLength;\r
1068 break;\r
1069 case READ_FILE_SEEK_AND_READ:\r
1070 //\r
1071 // Seek file first before reading in its data.\r
1072 //\r
1073 if (FinishedSeeking) {\r
1074 Offset = 0;\r
1075 goto Skip_File_Seek;\r
1076 }\r
1077\r
1078 if (FilePosition + ExtentLength < ReadFileInfo->FilePosition) {\r
1079 FilePosition += ExtentLength;\r
1080 goto Skip_Ad;\r
1081 }\r
1082\r
1083 if (FilePosition + ExtentLength > ReadFileInfo->FilePosition) {\r
1084 Offset = ReadFileInfo->FilePosition - FilePosition;\r
99c9b949
PA
1085 } else {\r
1086 Offset = 0;\r
1087 }\r
1088\r
1089 //\r
1090 // Done with seeking file. Start reading its data.\r
1091 //\r
1092 FinishedSeeking = TRUE;\r
1093\r
1094 Skip_File_Seek:\r
1095 //\r
1096 // Make sure we don't read more data than really wanted.\r
1097 //\r
1098 if (ExtentLength - Offset > BytesLeft) {\r
1099 DataLength = BytesLeft;\r
1100 } else {\r
1101 DataLength = ExtentLength - Offset;\r
1102 }\r
1103\r
1104 //\r
1105 // Read extent's data into FileData.\r
1106 //\r
1107 Status = DiskIo->ReadDisk (\r
1108 DiskIo,\r
1109 BlockIo->Media->MediaId,\r
1110 Offset + MultU64x32 (Lsn, LogicalBlockSize),\r
0b4c8f00 1111 (UINTN) DataLength,\r
99c9b949
PA
1112 (VOID *)((UINT8 *)ReadFileInfo->FileData +\r
1113 DataOffset)\r
1114 );\r
1115 if (EFI_ERROR (Status)) {\r
1116 goto Error_Read_Disk_Blk;\r
1117 }\r
1118\r
1119 //\r
1120 // Update current file's position.\r
1121 //\r
1122 DataOffset += DataLength;\r
1123 ReadFileInfo->FilePosition += DataLength;\r
1124\r
1125 BytesLeft -= DataLength;\r
1126 if (BytesLeft == 0) {\r
1127 //\r
1128 // There is no more file data to read.\r
1129 //\r
1130 Status = EFI_SUCCESS;\r
1131 goto Done;\r
1132 }\r
1133\r
1134 break;\r
1135 }\r
1136\r
1137 Skip_Ad:\r
1138 //\r
1139 // Point to the next AD (extent).\r
1140 //\r
1141 AdOffset += AD_LENGTH (RecordingFlags);\r
1142 }\r
1143\r
1144 break;\r
1145 case EXTENDED_ADS_SEQUENCE:\r
1146 // FIXME: Not supported. Got no volume with it, yet.\r
1147 ASSERT (FALSE);\r
1148 Status = EFI_UNSUPPORTED;\r
1149 break;\r
1150 }\r
1151\r
1152Done:\r
1153 if (DoFreeAed) {\r
1154 FreePool (Data);\r
1155 }\r
1156\r
1157 return Status;\r
1158\r
1159Error_Read_Disk_Blk:\r
1160Error_Alloc_Buffer_To_Next_Ad:\r
1161 if (ReadFileInfo->Flags != READ_FILE_SEEK_AND_READ) {\r
1162 FreePool (ReadFileInfo->FileData);\r
1163 }\r
1164\r
1165 if (DoFreeAed) {\r
1166 FreePool (Data);\r
1167 }\r
1168\r
1169Error_Get_Aed:\r
1170 return Status;\r
1171}\r
1172\r
1173//\r
1174// Find a file by its filename from a given Parent file.\r
1175//\r
1176EFI_STATUS\r
1177InternalFindFile (\r
1178 IN EFI_BLOCK_IO_PROTOCOL *BlockIo,\r
1179 IN EFI_DISK_IO_PROTOCOL *DiskIo,\r
1180 IN UDF_VOLUME_INFO *Volume,\r
1181 IN CHAR16 *FileName,\r
1182 IN UDF_FILE_INFO *Parent,\r
1183 IN UDF_LONG_ALLOCATION_DESCRIPTOR *Icb,\r
1184 OUT UDF_FILE_INFO *File\r
1185 )\r
1186{\r
1187 EFI_STATUS Status;\r
1188 UDF_FILE_IDENTIFIER_DESCRIPTOR *FileIdentifierDesc;\r
1189 UDF_READ_DIRECTORY_INFO ReadDirInfo;\r
1190 BOOLEAN Found;\r
1191 CHAR16 FoundFileName[UDF_FILENAME_LENGTH];\r
1192 VOID *CompareFileEntry;\r
1193\r
1194 //\r
1195 // Check if parent file is really directory.\r
1196 //\r
1197 if (!IS_FE_DIRECTORY (Parent->FileEntry)) {\r
1198 return EFI_NOT_FOUND;\r
1199 }\r
1200\r
1201 //\r
1202 // If FileName is current file or working directory, just duplicate Parent's\r
1203 // FE/EFE and FID descriptors.\r
1204 //\r
1205 if (StrCmp (FileName, L".") == 0) {\r
1206 DuplicateFe (BlockIo, Volume, Parent->FileEntry, &File->FileEntry);\r
1207 DuplicateFid (Parent->FileIdentifierDesc, &File->FileIdentifierDesc);\r
1208\r
1209 return EFI_SUCCESS;\r
1210 }\r
1211\r
1212 //\r
1213 // Start directory listing.\r
1214 //\r
1215 ZeroMem ((VOID *)&ReadDirInfo, sizeof (UDF_READ_DIRECTORY_INFO));\r
1216 Found = FALSE;\r
1217\r
1218 for (;;) {\r
1219 Status = ReadDirectoryEntry (\r
1220 BlockIo,\r
1221 DiskIo,\r
1222 Volume,\r
1223 Parent->FileIdentifierDesc ?\r
1224 &Parent->FileIdentifierDesc->Icb :\r
1225 Icb,\r
1226 Parent->FileEntry,\r
1227 &ReadDirInfo,\r
1228 &FileIdentifierDesc\r
1229 );\r
1230 if (EFI_ERROR (Status)) {\r
1231 if (Status == EFI_DEVICE_ERROR) {\r
1232 Status = EFI_NOT_FOUND;\r
1233 }\r
1234\r
1235 break;\r
1236 }\r
1237\r
1238 if (IS_FID_PARENT_FILE (FileIdentifierDesc)) {\r
1239 //\r
1240 // This FID contains the location (FE/EFE) of the parent directory of this\r
1241 // directory (Parent), and if FileName is either ".." or "\\", then it's\r
1242 // the expected FID.\r
1243 //\r
1244 if (StrCmp (FileName, L"..") == 0 || StrCmp (FileName, L"\\") == 0) {\r
1245 Found = TRUE;\r
1246 break;\r
1247 }\r
1248 } else {\r
1249 Status = GetFileNameFromFid (FileIdentifierDesc, FoundFileName);\r
1250 if (EFI_ERROR (Status)) {\r
1251 break;\r
1252 }\r
1253\r
1254 if (StrCmp (FileName, FoundFileName) == 0) {\r
1255 //\r
1256 // FID has been found. Prepare to find its respective FE/EFE.\r
1257 //\r
1258 Found = TRUE;\r
1259 break;\r
1260 }\r
1261 }\r
1262\r
1263 FreePool ((VOID *)FileIdentifierDesc);\r
1264 }\r
1265\r
1266 if (ReadDirInfo.DirectoryData != NULL) {\r
1267 //\r
1268 // Free all allocated resources for the directory listing.\r
1269 //\r
1270 FreePool (ReadDirInfo.DirectoryData);\r
1271 }\r
1272\r
1273 if (Found) {\r
1274 Status = EFI_SUCCESS;\r
1275\r
1276 File->FileIdentifierDesc = FileIdentifierDesc;\r
1277\r
1278 //\r
1279 // If the requested file is root directory, then the FE/EFE was already\r
1280 // retrieved in UdfOpenVolume() function, thus no need to find it again.\r
1281 //\r
1282 // Otherwise, find FE/EFE from the respective FID.\r
1283 //\r
1284 if (StrCmp (FileName, L"\\") != 0) {\r
1285 Status = FindFileEntry (\r
1286 BlockIo,\r
1287 DiskIo,\r
1288 Volume,\r
1289 &FileIdentifierDesc->Icb,\r
1290 &CompareFileEntry\r
1291 );\r
1292 if (EFI_ERROR (Status)) {\r
1293 goto Error_Find_Fe;\r
1294 }\r
1295\r
1296 //\r
1297 // Make sure that both Parent's FE/EFE and found FE/EFE are not equal.\r
1298 //\r
1299 if (CompareMem ((VOID *)Parent->FileEntry, (VOID *)CompareFileEntry,\r
1300 Volume->FileEntrySize) != 0) {\r
1301 File->FileEntry = CompareFileEntry;\r
1302 } else {\r
1303 FreePool ((VOID *)FileIdentifierDesc);\r
1304 FreePool ((VOID *)CompareFileEntry);\r
1305 Status = EFI_NOT_FOUND;\r
1306 }\r
1307 }\r
1308 }\r
1309\r
1310 return Status;\r
1311\r
1312Error_Find_Fe:\r
1313 FreePool ((VOID *)FileIdentifierDesc);\r
1314\r
1315 return Status;\r
1316}\r
1317\r
1318/**\r
1319 Read volume information on a medium which contains a valid UDF file system.\r
1320\r
1321 @param[in] BlockIo BlockIo interface.\r
1322 @param[in] DiskIo DiskIo interface.\r
1323 @param[out] Volume UDF volume information structure.\r
1324\r
1325 @retval EFI_SUCCESS Volume information read.\r
1326 @retval EFI_NO_MEDIA The device has no media.\r
1327 @retval EFI_DEVICE_ERROR The device reported an error.\r
1328 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
1329 @retval EFI_OUT_OF_RESOURCES The volume was not read due to lack of resources.\r
1330\r
1331**/\r
1332EFI_STATUS\r
1333ReadUdfVolumeInformation (\r
1334 IN EFI_BLOCK_IO_PROTOCOL *BlockIo,\r
1335 IN EFI_DISK_IO_PROTOCOL *DiskIo,\r
1336 OUT UDF_VOLUME_INFO *Volume\r
1337 )\r
1338{\r
1339 EFI_STATUS Status;\r
1340\r
1341 Status = ReadVolumeFileStructure (\r
1342 BlockIo,\r
1343 DiskIo,\r
1344 Volume\r
1345 );\r
1346 if (EFI_ERROR (Status)) {\r
1347 return Status;\r
1348 }\r
1349\r
1350 Status = GetFileSetDescriptors (\r
1351 BlockIo,\r
1352 DiskIo,\r
1353 Volume\r
1354 );\r
1355 if (EFI_ERROR (Status)) {\r
1356 CleanupVolumeInformation (Volume);\r
1357 }\r
1358\r
1359 return Status;\r
1360}\r
1361\r
1362/**\r
1363 Find the root directory on an UDF volume.\r
1364\r
1365 @param[in] BlockIo BlockIo interface.\r
1366 @param[in] DiskIo DiskIo interface.\r
1367 @param[in] Volume UDF volume information structure.\r
1368 @param[out] File Root directory file.\r
1369\r
1370 @retval EFI_SUCCESS Root directory found.\r
1371 @retval EFI_NO_MEDIA The device has no media.\r
1372 @retval EFI_DEVICE_ERROR The device reported an error.\r
1373 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
1374 @retval EFI_OUT_OF_RESOURCES The root directory was not found due to lack of\r
1375 resources.\r
1376\r
1377**/\r
1378EFI_STATUS\r
1379FindRootDirectory (\r
1380 IN EFI_BLOCK_IO_PROTOCOL *BlockIo,\r
1381 IN EFI_DISK_IO_PROTOCOL *DiskIo,\r
1382 IN UDF_VOLUME_INFO *Volume,\r
1383 OUT UDF_FILE_INFO *File\r
1384 )\r
1385{\r
1386 EFI_STATUS Status;\r
1387 UDF_FILE_INFO Parent;\r
1388\r
1389 Status = FindFileEntry (\r
1390 BlockIo,\r
1391 DiskIo,\r
1392 Volume,\r
1393 &Volume->FileSetDescs[0]->RootDirectoryIcb,\r
1394 &File->FileEntry\r
1395 );\r
1396 if (EFI_ERROR (Status)) {\r
1397 return Status;\r
1398 }\r
1399\r
1400 Parent.FileEntry = File->FileEntry;\r
1401 Parent.FileIdentifierDesc = NULL;\r
1402\r
1403 Status = FindFile (\r
1404 BlockIo,\r
1405 DiskIo,\r
1406 Volume,\r
1407 L"\\",\r
1408 NULL,\r
1409 &Parent,\r
1410 &Volume->FileSetDescs[0]->RootDirectoryIcb,\r
1411 File\r
1412 );\r
1413 if (EFI_ERROR (Status)) {\r
1414 FreePool (File->FileEntry);\r
1415 }\r
1416\r
1417 return Status;\r
1418}\r
1419\r
1420/**\r
1421 Find either a File Entry or a Extended File Entry from a given ICB.\r
1422\r
1423 @param[in] BlockIo BlockIo interface.\r
1424 @param[in] DiskIo DiskIo interface.\r
1425 @param[in] Volume UDF volume information structure.\r
1426 @param[in] Icb ICB of the FID.\r
1427 @param[out] FileEntry File Entry or Extended File Entry.\r
1428\r
1429 @retval EFI_SUCCESS File Entry or Extended File Entry found.\r
1430 @retval EFI_NO_MEDIA The device has no media.\r
1431 @retval EFI_DEVICE_ERROR The device reported an error.\r
1432 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
1433 @retval EFI_OUT_OF_RESOURCES The FE/EFE entry was not found due to lack of\r
1434 resources.\r
1435\r
1436**/\r
1437EFI_STATUS\r
1438FindFileEntry (\r
1439 IN EFI_BLOCK_IO_PROTOCOL *BlockIo,\r
1440 IN EFI_DISK_IO_PROTOCOL *DiskIo,\r
1441 IN UDF_VOLUME_INFO *Volume,\r
1442 IN UDF_LONG_ALLOCATION_DESCRIPTOR *Icb,\r
1443 OUT VOID **FileEntry\r
1444 )\r
1445{\r
1446 EFI_STATUS Status;\r
1447 UINT64 Lsn;\r
1448 UINT32 LogicalBlockSize;\r
1449\r
1450 Lsn = GetLongAdLsn (Volume, Icb);\r
1451 LogicalBlockSize = LV_BLOCK_SIZE (Volume, UDF_DEFAULT_LV_NUM);\r
1452\r
1453 *FileEntry = AllocateZeroPool (Volume->FileEntrySize);\r
1454 if (*FileEntry == NULL) {\r
1455 return EFI_OUT_OF_RESOURCES;\r
1456 }\r
1457\r
1458 //\r
1459 // Read extent.\r
1460 //\r
1461 Status = DiskIo->ReadDisk (\r
1462 DiskIo,\r
1463 BlockIo->Media->MediaId,\r
1464 MultU64x32 (Lsn, LogicalBlockSize),\r
1465 Volume->FileEntrySize,\r
1466 *FileEntry\r
1467 );\r
1468 if (EFI_ERROR (Status)) {\r
1469 goto Error_Read_Disk_Blk;\r
1470 }\r
1471\r
1472 //\r
1473 // Check if the read extent contains a valid Tag Identifier for the expected\r
1474 // FE/EFE.\r
1475 //\r
1476 if (!IS_FE (*FileEntry) && !IS_EFE (*FileEntry)) {\r
1477 Status = EFI_VOLUME_CORRUPTED;\r
1478 goto Error_Invalid_Fe;\r
1479 }\r
1480\r
1481 return EFI_SUCCESS;\r
1482\r
1483Error_Invalid_Fe:\r
1484Error_Read_Disk_Blk:\r
1485 FreePool (*FileEntry);\r
1486\r
1487 return Status;\r
1488}\r
1489\r
1490/**\r
1491 Find a file given its absolute path on an UDF volume.\r
1492\r
1493 @param[in] BlockIo BlockIo interface.\r
1494 @param[in] DiskIo DiskIo interface.\r
1495 @param[in] Volume UDF volume information structure.\r
1496 @param[in] FilePath File's absolute path.\r
1497 @param[in] Root Root directory file.\r
1498 @param[in] Parent Parent directory file.\r
1499 @param[out] File Found file.\r
1500\r
1501 @retval EFI_SUCCESS @p FilePath was found.\r
1502 @retval EFI_NO_MEDIA The device has no media.\r
1503 @retval EFI_DEVICE_ERROR The device reported an error.\r
1504 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
1505 @retval EFI_OUT_OF_RESOURCES The @p FilePath file was not found due to lack of\r
1506 resources.\r
1507\r
1508**/\r
1509EFI_STATUS\r
1510FindFile (\r
1511 IN EFI_BLOCK_IO_PROTOCOL *BlockIo,\r
1512 IN EFI_DISK_IO_PROTOCOL *DiskIo,\r
1513 IN UDF_VOLUME_INFO *Volume,\r
1514 IN CHAR16 *FilePath,\r
1515 IN UDF_FILE_INFO *Root,\r
1516 IN UDF_FILE_INFO *Parent,\r
1517 IN UDF_LONG_ALLOCATION_DESCRIPTOR *Icb,\r
1518 OUT UDF_FILE_INFO *File\r
1519 )\r
1520{\r
1521 EFI_STATUS Status;\r
1522 CHAR16 FileName[UDF_FILENAME_LENGTH];\r
1523 CHAR16 *FileNamePointer;\r
1524 UDF_FILE_INFO PreviousFile;\r
1525 VOID *FileEntry;\r
1526\r
1527 Status = EFI_NOT_FOUND;\r
1528\r
1529 CopyMem ((VOID *)&PreviousFile, (VOID *)Parent, sizeof (UDF_FILE_INFO));\r
1530 while (*FilePath != L'\0') {\r
1531 FileNamePointer = FileName;\r
1532 while (*FilePath != L'\0' && *FilePath != L'\\') {\r
1533 *FileNamePointer++ = *FilePath++;\r
1534 }\r
1535\r
1536 *FileNamePointer = L'\0';\r
1537 if (FileName[0] == L'\0') {\r
1538 //\r
1539 // Open root directory.\r
1540 //\r
1541 if (Root == NULL) {\r
1542 //\r
1543 // There is no file found for the root directory yet. So, find only its\r
1544 // FID by now.\r
1545 //\r
1546 // See UdfOpenVolume() function.\r
1547 //\r
1548 Status = InternalFindFile (BlockIo,\r
1549 DiskIo,\r
1550 Volume,\r
1551 L"\\",\r
1552 &PreviousFile,\r
1553 Icb,\r
1554 File);\r
1555 } else {\r
1556 //\r
1557 // We've already a file pointer (Root) for the root directory. Duplicate\r
1558 // its FE/EFE and FID descriptors.\r
1559 //\r
1560 DuplicateFe (BlockIo, Volume, Root->FileEntry, &File->FileEntry);\r
1561 DuplicateFid (Root->FileIdentifierDesc, &File->FileIdentifierDesc);\r
1562 Status = EFI_SUCCESS;\r
1563 }\r
1564 } else {\r
1565 //\r
1566 // No root directory. Find filename from the current directory.\r
1567 //\r
1568 Status = InternalFindFile (BlockIo,\r
1569 DiskIo,\r
1570 Volume,\r
1571 FileName,\r
1572 &PreviousFile,\r
1573 Icb,\r
1574 File);\r
1575 }\r
1576\r
1577 if (EFI_ERROR (Status)) {\r
1578 return Status;\r
1579 }\r
1580\r
1581 //\r
1582 // If the found file is a symlink, then find its respective FE/EFE and\r
1583 // FID descriptors.\r
1584 //\r
1585 if (IS_FE_SYMLINK (File->FileEntry)) {\r
1586 FreePool ((VOID *)File->FileIdentifierDesc);\r
1587\r
1588 FileEntry = File->FileEntry;\r
1589\r
1590 Status = ResolveSymlink (BlockIo,\r
1591 DiskIo,\r
1592 Volume,\r
1593 &PreviousFile,\r
1594 FileEntry,\r
1595 File);\r
1596\r
1597 FreePool (FileEntry);\r
1598\r
1599 if (EFI_ERROR (Status)) {\r
1600 return Status;\r
1601 }\r
1602 }\r
1603\r
1604 if (CompareMem ((VOID *)&PreviousFile, (VOID *)Parent,\r
1605 sizeof (UDF_FILE_INFO)) != 0) {\r
1606 CleanupFileInformation (&PreviousFile);\r
1607 }\r
1608\r
1609 CopyMem ((VOID *)&PreviousFile, (VOID *)File, sizeof (UDF_FILE_INFO));\r
1610 if (*FilePath != L'\0' && *FilePath == L'\\') {\r
1611 FilePath++;\r
1612 }\r
1613 }\r
1614\r
1615 return Status;\r
1616}\r
1617\r
1618/**\r
1619 Read a directory entry at a time on an UDF volume.\r
1620\r
1621 @param[in] BlockIo BlockIo interface.\r
1622 @param[in] DiskIo DiskIo interface.\r
1623 @param[in] Volume UDF volume information structure.\r
1624 @param[in] ParentIcb ICB of the parent file.\r
1625 @param[in] FileEntryData FE/EFE of the parent file.\r
1626 @param[in out] ReadDirInfo Next read directory listing structure\r
1627 information.\r
1628 @param[out] FoundFid File Identifier Descriptor pointer.\r
1629\r
1630 @retval EFI_SUCCESS Directory entry read.\r
1631 @retval EFI_UNSUPPORTED Extended Allocation Descriptors not supported.\r
1632 @retval EFI_NO_MEDIA The device has no media.\r
1633 @retval EFI_DEVICE_ERROR The device reported an error.\r
1634 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
1635 @retval EFI_OUT_OF_RESOURCES The directory entry was not read due to lack of\r
1636 resources.\r
1637\r
1638**/\r
1639EFI_STATUS\r
1640ReadDirectoryEntry (\r
1641 IN EFI_BLOCK_IO_PROTOCOL *BlockIo,\r
1642 IN EFI_DISK_IO_PROTOCOL *DiskIo,\r
1643 IN UDF_VOLUME_INFO *Volume,\r
1644 IN UDF_LONG_ALLOCATION_DESCRIPTOR *ParentIcb,\r
1645 IN VOID *FileEntryData,\r
1646 IN OUT UDF_READ_DIRECTORY_INFO *ReadDirInfo,\r
1647 OUT UDF_FILE_IDENTIFIER_DESCRIPTOR **FoundFid\r
1648 )\r
1649{\r
1650 EFI_STATUS Status;\r
1651 UDF_READ_FILE_INFO ReadFileInfo;\r
1652 UDF_FILE_IDENTIFIER_DESCRIPTOR *FileIdentifierDesc;\r
1653\r
1654 if (ReadDirInfo->DirectoryData == NULL) {\r
1655 //\r
1656 // The directory's recorded data has not been read yet. So let's cache it\r
1657 // into memory and the next calls won't need to read it again.\r
1658 //\r
1659 ReadFileInfo.Flags = READ_FILE_ALLOCATE_AND_READ;\r
1660\r
1661 Status = ReadFile (\r
1662 BlockIo,\r
1663 DiskIo,\r
1664 Volume,\r
1665 ParentIcb,\r
1666 FileEntryData,\r
1667 &ReadFileInfo\r
1668 );\r
1669 if (EFI_ERROR (Status)) {\r
1670 return Status;\r
1671 }\r
1672\r
1673 //\r
1674 // Fill in ReadDirInfo structure with the read directory's data information.\r
1675 //\r
1676 ReadDirInfo->DirectoryData = ReadFileInfo.FileData;\r
1677 ReadDirInfo->DirectoryLength = ReadFileInfo.ReadLength;\r
1678 }\r
1679\r
1680 do {\r
1681 if (ReadDirInfo->FidOffset >= ReadDirInfo->DirectoryLength) {\r
1682 //\r
1683 // There are no longer FIDs for this directory. By returning\r
1684 // EFI_DEVICE_ERROR to the callee will indicate end of directory\r
1685 // listening.\r
1686 //\r
1687 return EFI_DEVICE_ERROR;\r
1688 }\r
1689\r
1690 //\r
1691 // Get FID for this entry.\r
1692 //\r
1693 FileIdentifierDesc = GET_FID_FROM_ADS (ReadDirInfo->DirectoryData,\r
1694 ReadDirInfo->FidOffset);\r
1695 //\r
1696 // Update FidOffset to point to next FID.\r
1697 //\r
1698 ReadDirInfo->FidOffset += GetFidDescriptorLength (FileIdentifierDesc);\r
1699 } while (IS_FID_DELETED_FILE (FileIdentifierDesc));\r
1700\r
1701 DuplicateFid (FileIdentifierDesc, FoundFid);\r
1702\r
1703 return EFI_SUCCESS;\r
1704}\r
1705\r
1706/**\r
1707 Get a filename (encoded in OSTA-compressed format) from a File Identifier\r
1708 Descriptor on an UDF volume.\r
1709\r
1710 @param[in] FileIdentifierDesc File Identifier Descriptor pointer.\r
1711 @param[out] FileName Decoded filename.\r
1712\r
1713 @retval EFI_SUCCESS Filename decoded and read.\r
1714 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
1715**/\r
1716EFI_STATUS\r
1717GetFileNameFromFid (\r
1718 IN UDF_FILE_IDENTIFIER_DESCRIPTOR *FileIdentifierDesc,\r
1719 OUT CHAR16 *FileName\r
1720 )\r
1721{\r
1722 UINT8 *OstaCompressed;\r
1723 UINT8 CompressionId;\r
1724 UINT8 Length;\r
1725 UINTN Index;\r
1726\r
1727 OstaCompressed =\r
1728 (UINT8 *)(\r
1729 (UINT8 *)FileIdentifierDesc->Data +\r
1730 FileIdentifierDesc->LengthOfImplementationUse\r
1731 );\r
1732\r
1733 CompressionId = OstaCompressed[0];\r
1734 if (!IS_VALID_COMPRESSION_ID (CompressionId)) {\r
1735 return EFI_VOLUME_CORRUPTED;\r
1736 }\r
1737\r
1738 //\r
1739 // Decode filename.\r
1740 //\r
1741 Length = FileIdentifierDesc->LengthOfFileIdentifier;\r
1742 for (Index = 1; Index < Length; Index++) {\r
1743 if (CompressionId == 16) {\r
1744 *FileName = OstaCompressed[Index++] << 8;\r
1745 } else {\r
1746 *FileName = 0;\r
1747 }\r
1748\r
1749 if (Index < Length) {\r
1750 *FileName |= OstaCompressed[Index];\r
1751 }\r
1752\r
1753 FileName++;\r
1754 }\r
1755\r
1756 *FileName = L'\0';\r
1757\r
1758 return EFI_SUCCESS;\r
1759}\r
1760\r
1761/**\r
1762 Resolve a symlink file on an UDF volume.\r
1763\r
1764 @param[in] BlockIo BlockIo interface.\r
1765 @param[in] DiskIo DiskIo interface.\r
1766 @param[in] Volume UDF volume information structure.\r
1767 @param[in] Parent Parent file.\r
1768 @param[in] FileEntryData FE/EFE structure pointer.\r
1769 @param[out] File Resolved file.\r
1770\r
1771 @retval EFI_SUCCESS Symlink file resolved.\r
1772 @retval EFI_UNSUPPORTED Extended Allocation Descriptors not supported.\r
1773 @retval EFI_NO_MEDIA The device has no media.\r
1774 @retval EFI_DEVICE_ERROR The device reported an error.\r
1775 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
1776 @retval EFI_OUT_OF_RESOURCES The symlink file was not resolved due to lack of\r
1777 resources.\r
1778\r
1779**/\r
1780EFI_STATUS\r
1781ResolveSymlink (\r
1782 IN EFI_BLOCK_IO_PROTOCOL *BlockIo,\r
1783 IN EFI_DISK_IO_PROTOCOL *DiskIo,\r
1784 IN UDF_VOLUME_INFO *Volume,\r
1785 IN UDF_FILE_INFO *Parent,\r
1786 IN VOID *FileEntryData,\r
1787 OUT UDF_FILE_INFO *File\r
1788 )\r
1789{\r
1790 EFI_STATUS Status;\r
1791 UDF_READ_FILE_INFO ReadFileInfo;\r
1792 UINT8 *Data;\r
1793 UINT64 Length;\r
1794 UINT8 *EndData;\r
1795 UDF_PATH_COMPONENT *PathComp;\r
1796 UINT8 PathCompLength;\r
1797 CHAR16 FileName[UDF_FILENAME_LENGTH];\r
1798 CHAR16 *C;\r
1799 UINTN Index;\r
1800 UINT8 CompressionId;\r
1801 UDF_FILE_INFO PreviousFile;\r
1802\r
1803 //\r
1804 // Symlink files on UDF volumes do not contain so much data other than\r
1805 // Path Components which resolves to real filenames, so it's OK to read in\r
1806 // all its data here -- usually the data will be inline with the FE/EFE for\r
1807 // lower filenames.\r
1808 //\r
1809 ReadFileInfo.Flags = READ_FILE_ALLOCATE_AND_READ;\r
1810\r
1811 Status = ReadFile (\r
1812 BlockIo,\r
1813 DiskIo,\r
1814 Volume,\r
1815 &Parent->FileIdentifierDesc->Icb,\r
1816 FileEntryData,\r
1817 &ReadFileInfo\r
1818 );\r
1819 if (EFI_ERROR (Status)) {\r
1820 return Status;\r
1821 }\r
1822\r
1823 Length = ReadFileInfo.ReadLength;\r
1824\r
1825 Data = (UINT8 *)ReadFileInfo.FileData;\r
1826 EndData = Data + Length;\r
1827\r
1828 CopyMem ((VOID *)&PreviousFile, (VOID *)Parent, sizeof (UDF_FILE_INFO));\r
1829\r
1830 for (;;) {\r
1831 PathComp = (UDF_PATH_COMPONENT *)Data;\r
1832\r
1833 PathCompLength = PathComp->LengthOfComponentIdentifier;\r
1834\r
1835 switch (PathComp->ComponentType) {\r
1836 case 1:\r
1837 //\r
1838 // This Path Component specifies the root directory hierarchy subject to\r
1839 // agreement between the originator and recipient of the medium. Skip it.\r
1840 //\r
1841 // Fall through.\r
1842 //\r
1843 case 2:\r
1844 //\r
1845 // "\\." of the current directory. Read next Path Component.\r
1846 //\r
1847 goto Next_Path_Component;\r
1848 case 3:\r
1849 //\r
1850 // ".." (parent directory). Go to it.\r
1851 //\r
1852 CopyMem ((VOID *)FileName, L"..", 6);\r
1853 break;\r
1854 case 4:\r
1855 //\r
1856 // "." (current file). Duplicate both FE/EFE and FID of this file.\r
1857 //\r
1858 DuplicateFe (BlockIo, Volume, PreviousFile.FileEntry, &File->FileEntry);\r
1859 DuplicateFid (PreviousFile.FileIdentifierDesc,\r
1860 &File->FileIdentifierDesc);\r
1861 goto Next_Path_Component;\r
1862 case 5:\r
1863 //\r
1864 // This Path Component identifies an object, either a file or a\r
1865 // directory or an alias.\r
1866 //\r
1867 // Decode it from the compressed data in ComponentIdentifier and find\r
1868 // respective path.\r
1869 //\r
1870 CompressionId = PathComp->ComponentIdentifier[0];\r
1871 if (!IS_VALID_COMPRESSION_ID (CompressionId)) {\r
1872 return EFI_VOLUME_CORRUPTED;\r
1873 }\r
1874\r
1875 C = FileName;\r
1876 for (Index = 1; Index < PathCompLength; Index++) {\r
1877 if (CompressionId == 16) {\r
1878 *C = *(UINT8 *)((UINT8 *)PathComp->ComponentIdentifier +\r
1879 Index) << 8;\r
1880 Index++;\r
1881 } else {\r
1882 *C = 0;\r
1883 }\r
1884\r
1885 if (Index < Length) {\r
1886 *C |= *(UINT8 *)((UINT8 *)PathComp->ComponentIdentifier + Index);\r
1887 }\r
1888\r
1889 C++;\r
1890 }\r
1891\r
1892 *C = L'\0';\r
1893 break;\r
1894 }\r
1895\r
1896 //\r
1897 // Find file from the read filename in symlink's file data.\r
1898 //\r
1899 Status = InternalFindFile (\r
1900 BlockIo,\r
1901 DiskIo,\r
1902 Volume,\r
1903 FileName,\r
1904 &PreviousFile,\r
1905 NULL,\r
1906 File\r
1907 );\r
1908 if (EFI_ERROR (Status)) {\r
1909 goto Error_Find_File;\r
1910 }\r
1911\r
1912 Next_Path_Component:\r
1913 Data += sizeof (UDF_PATH_COMPONENT) + PathCompLength;\r
1914 if (Data >= EndData) {\r
1915 break;\r
1916 }\r
1917\r
1918 if (CompareMem ((VOID *)&PreviousFile, (VOID *)Parent,\r
1919 sizeof (UDF_FILE_INFO)) != 0) {\r
1920 CleanupFileInformation (&PreviousFile);\r
1921 }\r
1922\r
1923 CopyMem ((VOID *)&PreviousFile, (VOID *)File, sizeof (UDF_FILE_INFO));\r
1924 }\r
1925\r
1926 //\r
1927 // Unmap the symlink file.\r
1928 //\r
1929 FreePool (ReadFileInfo.FileData);\r
1930\r
1931 return EFI_SUCCESS;\r
1932\r
1933Error_Find_File:\r
1934 if (CompareMem ((VOID *)&PreviousFile, (VOID *)Parent,\r
1935 sizeof (UDF_FILE_INFO)) != 0) {\r
1936 CleanupFileInformation (&PreviousFile);\r
1937 }\r
1938\r
1939 FreePool (ReadFileInfo.FileData);\r
1940\r
1941 return Status;\r
1942}\r
1943\r
1944/**\r
1945 Clean up in-memory UDF volume information.\r
1946\r
1947 @param[in] Volume Volume information pointer.\r
1948\r
1949**/\r
1950VOID\r
1951CleanupVolumeInformation (\r
1952 IN UDF_VOLUME_INFO *Volume\r
1953 )\r
1954{\r
1955 UINTN Index;\r
1956\r
1957 if (Volume->LogicalVolDescs != NULL) {\r
1958 for (Index = 0; Index < Volume->LogicalVolDescsNo; Index++) {\r
1959 FreePool ((VOID *)Volume->LogicalVolDescs[Index]);\r
1960 }\r
1961 FreePool ((VOID *)Volume->LogicalVolDescs);\r
1962 }\r
1963\r
1964 if (Volume->PartitionDescs != NULL) {\r
1965 for (Index = 0; Index < Volume->PartitionDescsNo; Index++) {\r
1966 FreePool ((VOID *)Volume->PartitionDescs[Index]);\r
1967 }\r
1968 FreePool ((VOID *)Volume->PartitionDescs);\r
1969 }\r
1970\r
1971 if (Volume->FileSetDescs != NULL) {\r
1972 for (Index = 0; Index < Volume->FileSetDescsNo; Index++) {\r
1973 FreePool ((VOID *)Volume->FileSetDescs[Index]);\r
1974 }\r
1975 FreePool ((VOID *)Volume->FileSetDescs);\r
1976 }\r
1977\r
1978 ZeroMem ((VOID *)Volume, sizeof (UDF_VOLUME_INFO));\r
1979}\r
1980\r
1981/**\r
1982 Clean up in-memory UDF file information.\r
1983\r
1984 @param[in] File File information pointer.\r
1985\r
1986**/\r
1987VOID\r
1988CleanupFileInformation (\r
1989 IN UDF_FILE_INFO *File\r
1990 )\r
1991{\r
1992 if (File->FileEntry != NULL) {\r
1993 FreePool (File->FileEntry);\r
1994 }\r
1995 if (File->FileIdentifierDesc != NULL) {\r
1996 FreePool ((VOID *)File->FileIdentifierDesc);\r
1997 }\r
1998\r
1999 ZeroMem ((VOID *)File, sizeof (UDF_FILE_INFO));\r
2000}\r
2001\r
2002/**\r
2003 Find a file from its absolute path on an UDF volume.\r
2004\r
2005 @param[in] BlockIo BlockIo interface.\r
2006 @param[in] DiskIo DiskIo interface.\r
2007 @param[in] Volume UDF volume information structure.\r
2008 @param[in] File File information structure.\r
2009 @param[out] Size Size of the file.\r
2010\r
2011 @retval EFI_SUCCESS File size calculated and set in @p Size.\r
2012 @retval EFI_UNSUPPORTED Extended Allocation Descriptors not supported.\r
2013 @retval EFI_NO_MEDIA The device has no media.\r
2014 @retval EFI_DEVICE_ERROR The device reported an error.\r
2015 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
2016 @retval EFI_OUT_OF_RESOURCES The file size was not calculated due to lack of\r
2017 resources.\r
2018\r
2019**/\r
2020EFI_STATUS\r
2021GetFileSize (\r
2022 IN EFI_BLOCK_IO_PROTOCOL *BlockIo,\r
2023 IN EFI_DISK_IO_PROTOCOL *DiskIo,\r
2024 IN UDF_VOLUME_INFO *Volume,\r
2025 IN UDF_FILE_INFO *File,\r
2026 OUT UINT64 *Size\r
2027 )\r
2028{\r
2029 EFI_STATUS Status;\r
2030 UDF_READ_FILE_INFO ReadFileInfo;\r
2031\r
2032 ReadFileInfo.Flags = READ_FILE_GET_FILESIZE;\r
2033\r
2034 Status = ReadFile (\r
2035 BlockIo,\r
2036 DiskIo,\r
2037 Volume,\r
2038 &File->FileIdentifierDesc->Icb,\r
2039 File->FileEntry,\r
2040 &ReadFileInfo\r
2041 );\r
2042 if (EFI_ERROR (Status)) {\r
2043 return Status;\r
2044 }\r
2045\r
2046 *Size = ReadFileInfo.ReadLength;\r
2047\r
2048 return EFI_SUCCESS;\r
2049}\r
2050\r
2051/**\r
2052 Set information about a file on an UDF volume.\r
2053\r
2054 @param[in] File File pointer.\r
2055 @param[in] FileSize Size of the file.\r
2056 @param[in] FileName Filename of the file.\r
2057 @param[in out] BufferSize Size of the returned file infomation.\r
2058 @param[out] Buffer Data of the returned file information.\r
2059\r
2060 @retval EFI_SUCCESS File information set.\r
2061 @retval EFI_NO_MEDIA The device has no media.\r
2062 @retval EFI_DEVICE_ERROR The device reported an error.\r
2063 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
2064 @retval EFI_OUT_OF_RESOURCES The file information was not set due to lack of\r
2065 resources.\r
2066\r
2067**/\r
2068EFI_STATUS\r
2069SetFileInfo (\r
2070 IN UDF_FILE_INFO *File,\r
2071 IN UINT64 FileSize,\r
2072 IN CHAR16 *FileName,\r
2073 IN OUT UINTN *BufferSize,\r
2074 OUT VOID *Buffer\r
2075 )\r
2076{\r
2077 UINTN FileInfoLength;\r
2078 EFI_FILE_INFO *FileInfo;\r
2079 UDF_FILE_ENTRY *FileEntry;\r
2080 UDF_EXTENDED_FILE_ENTRY *ExtendedFileEntry;\r
2081\r
2082 //\r
2083 // Calculate the needed size for the EFI_FILE_INFO structure.\r
2084 //\r
2085 FileInfoLength = sizeof (EFI_FILE_INFO) + (FileName ?\r
2086 StrSize (FileName) :\r
2087 sizeof (CHAR16));\r
2088 if (*BufferSize < FileInfoLength) {\r
2089 //\r
2090 // The given Buffer has no size enough for EFI_FILE_INFO structure.\r
2091 //\r
2092 *BufferSize = FileInfoLength;\r
2093 return EFI_BUFFER_TOO_SMALL;\r
2094 }\r
2095\r
2096 //\r
2097 // Buffer now contains room enough to store EFI_FILE_INFO structure.\r
2098 // Now, fill it in with all necessary information about the file.\r
2099 //\r
2100 FileInfo = (EFI_FILE_INFO *)Buffer;\r
2101 FileInfo->Size = FileInfoLength;\r
2102 FileInfo->Attribute &= ~EFI_FILE_VALID_ATTR;\r
2103 FileInfo->Attribute |= EFI_FILE_READ_ONLY;\r
2104\r
2105 if (IS_FID_DIRECTORY_FILE (File->FileIdentifierDesc)) {\r
2106 FileInfo->Attribute |= EFI_FILE_DIRECTORY;\r
2107 } else if (IS_FID_NORMAL_FILE (File->FileIdentifierDesc)) {\r
2108 FileInfo->Attribute |= EFI_FILE_ARCHIVE;\r
2109 }\r
2110\r
2111 if (IS_FID_HIDDEN_FILE (File->FileIdentifierDesc)) {\r
2112 FileInfo->Attribute |= EFI_FILE_HIDDEN;\r
2113 }\r
2114\r
2115 if (IS_FE (File->FileEntry)) {\r
2116 FileEntry = (UDF_FILE_ENTRY *)File->FileEntry;\r
2117\r
2118 //\r
2119 // Check if FE has the system attribute set.\r
2120 //\r
2121 if (FileEntry->IcbTag.Flags & (1 << 10)) {\r
2122 FileInfo->Attribute |= EFI_FILE_SYSTEM;\r
2123 }\r
2124\r
2125 FileInfo->FileSize = FileSize;\r
2126 FileInfo->PhysicalSize = FileSize;\r
2127\r
2128 FileInfo->CreateTime.Year = FileEntry->AccessTime.Year;\r
2129 FileInfo->CreateTime.Month = FileEntry->AccessTime.Month;\r
2130 FileInfo->CreateTime.Day = FileEntry->AccessTime.Day;\r
2131 FileInfo->CreateTime.Hour = FileEntry->AccessTime.Hour;\r
2132 FileInfo->CreateTime.Minute = FileEntry->AccessTime.Second;\r
2133 FileInfo->CreateTime.Second = FileEntry->AccessTime.Second;\r
2134 FileInfo->CreateTime.Nanosecond =\r
2135 FileEntry->AccessTime.HundredsOfMicroseconds;\r
2136\r
2137 FileInfo->LastAccessTime.Year =\r
2138 FileEntry->AccessTime.Year;\r
2139 FileInfo->LastAccessTime.Month =\r
2140 FileEntry->AccessTime.Month;\r
2141 FileInfo->LastAccessTime.Day =\r
2142 FileEntry->AccessTime.Day;\r
2143 FileInfo->LastAccessTime.Hour =\r
2144 FileEntry->AccessTime.Hour;\r
2145 FileInfo->LastAccessTime.Minute =\r
2146 FileEntry->AccessTime.Minute;\r
2147 FileInfo->LastAccessTime.Second =\r
2148 FileEntry->AccessTime.Second;\r
2149 FileInfo->LastAccessTime.Nanosecond =\r
2150 FileEntry->AccessTime.HundredsOfMicroseconds;\r
2151 } else if (IS_EFE (File->FileEntry)) {\r
2152 ExtendedFileEntry = (UDF_EXTENDED_FILE_ENTRY *)File->FileEntry;\r
2153\r
2154 //\r
2155 // Check if EFE has the system attribute set.\r
2156 //\r
2157 if (ExtendedFileEntry->IcbTag.Flags & (1 << 10)) {\r
2158 FileInfo->Attribute |= EFI_FILE_SYSTEM;\r
2159 }\r
2160\r
2161 FileInfo->FileSize = FileSize;\r
2162 FileInfo->PhysicalSize = FileSize;\r
2163\r
2164 FileInfo->CreateTime.Year = ExtendedFileEntry->CreationTime.Year;\r
2165 FileInfo->CreateTime.Month = ExtendedFileEntry->CreationTime.Month;\r
2166 FileInfo->CreateTime.Day = ExtendedFileEntry->CreationTime.Day;\r
2167 FileInfo->CreateTime.Hour = ExtendedFileEntry->CreationTime.Hour;\r
2168 FileInfo->CreateTime.Minute = ExtendedFileEntry->CreationTime.Second;\r
2169 FileInfo->CreateTime.Second = ExtendedFileEntry->CreationTime.Second;\r
2170 FileInfo->CreateTime.Nanosecond =\r
2171 ExtendedFileEntry->AccessTime.HundredsOfMicroseconds;\r
2172\r
2173 FileInfo->LastAccessTime.Year =\r
2174 ExtendedFileEntry->AccessTime.Year;\r
2175 FileInfo->LastAccessTime.Month =\r
2176 ExtendedFileEntry->AccessTime.Month;\r
2177 FileInfo->LastAccessTime.Day =\r
2178 ExtendedFileEntry->AccessTime.Day;\r
2179 FileInfo->LastAccessTime.Hour =\r
2180 ExtendedFileEntry->AccessTime.Hour;\r
2181 FileInfo->LastAccessTime.Minute =\r
2182 ExtendedFileEntry->AccessTime.Minute;\r
2183 FileInfo->LastAccessTime.Second =\r
2184 ExtendedFileEntry->AccessTime.Second;\r
2185 FileInfo->LastAccessTime.Nanosecond =\r
2186 ExtendedFileEntry->AccessTime.HundredsOfMicroseconds;\r
2187 }\r
2188\r
2189 FileInfo->CreateTime.TimeZone = EFI_UNSPECIFIED_TIMEZONE;\r
2190 FileInfo->CreateTime.Daylight = EFI_TIME_ADJUST_DAYLIGHT;\r
2191 FileInfo->LastAccessTime.TimeZone = EFI_UNSPECIFIED_TIMEZONE;\r
2192 FileInfo->LastAccessTime.Daylight = EFI_TIME_ADJUST_DAYLIGHT;\r
2193\r
2194 CopyMem ((VOID *)&FileInfo->ModificationTime,\r
2195 (VOID *)&FileInfo->LastAccessTime,\r
2196 sizeof (EFI_TIME));\r
2197\r
2198 if (FileName != NULL) {\r
2199 StrCpyS (FileInfo->FileName, StrLen (FileName) + 1, FileName);\r
2200 } else {\r
2201 FileInfo->FileName[0] = '\0';\r
2202 }\r
2203\r
2204 *BufferSize = FileInfoLength;\r
2205\r
2206 return EFI_SUCCESS;\r
2207}\r
2208\r
2209/**\r
2210 Get volume and free space size information of an UDF volume.\r
2211\r
2212 @param[in] BlockIo BlockIo interface.\r
2213 @param[in] DiskIo DiskIo interface.\r
2214 @param[in] Volume UDF volume information structure.\r
2215 @param[out] VolumeSize Volume size.\r
2216 @param[out] FreeSpaceSize Free space size.\r
2217\r
2218 @retval EFI_SUCCESS Volume and free space size calculated.\r
2219 @retval EFI_NO_MEDIA The device has no media.\r
2220 @retval EFI_DEVICE_ERROR The device reported an error.\r
2221 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
2222 @retval EFI_OUT_OF_RESOURCES The volume and free space size were not\r
2223 calculated due to lack of resources.\r
2224\r
2225**/\r
2226EFI_STATUS\r
2227GetVolumeSize (\r
2228 IN EFI_BLOCK_IO_PROTOCOL *BlockIo,\r
2229 IN EFI_DISK_IO_PROTOCOL *DiskIo,\r
2230 IN UDF_VOLUME_INFO *Volume,\r
2231 OUT UINT64 *VolumeSize,\r
2232 OUT UINT64 *FreeSpaceSize\r
2233 )\r
2234{\r
2235 UDF_EXTENT_AD ExtentAd;\r
2236 UINT32 LogicalBlockSize;\r
2237 UINT64 Lsn;\r
2238 EFI_STATUS Status;\r
2239 UDF_LOGICAL_VOLUME_INTEGRITY *LogicalVolInt;\r
2240 UINTN Index;\r
2241 UINTN Length;\r
2242 UINT32 LsnsNo;\r
2243\r
2244 *VolumeSize = 0;\r
2245 *FreeSpaceSize = 0;\r
2246\r
2247 for (Index = 0; Index < Volume->LogicalVolDescsNo; Index++) {\r
2248 CopyMem ((VOID *)&ExtentAd,\r
2249 (VOID *)&Volume->LogicalVolDescs[Index]->IntegritySequenceExtent,\r
2250 sizeof (UDF_EXTENT_AD));\r
2251 if (ExtentAd.ExtentLength == 0) {\r
2252 continue;\r
2253 }\r
2254\r
2255 LogicalBlockSize = LV_BLOCK_SIZE (Volume, Index);\r
2256\r
2257 Read_Next_Sequence:\r
2258 LogicalVolInt = (UDF_LOGICAL_VOLUME_INTEGRITY *)\r
2259 AllocatePool (ExtentAd.ExtentLength);\r
2260 if (LogicalVolInt == NULL) {\r
2261 return EFI_OUT_OF_RESOURCES;\r
2262 }\r
2263\r
2264 Lsn = (UINT64)ExtentAd.ExtentLocation;\r
2265\r
2266 Status = DiskIo->ReadDisk (\r
2267 DiskIo,\r
2268 BlockIo->Media->MediaId,\r
2269 MultU64x32 (Lsn, LogicalBlockSize),\r
2270 ExtentAd.ExtentLength,\r
2271 (VOID *)LogicalVolInt\r
2272 );\r
2273 if (EFI_ERROR (Status)) {\r
2274 FreePool ((VOID *)LogicalVolInt);\r
2275 return Status;\r
2276 }\r
2277\r
2278 if (!IS_LVID (LogicalVolInt)) {\r
2279 FreePool ((VOID *)LogicalVolInt);\r
2280 return EFI_VOLUME_CORRUPTED;\r
2281 }\r
2282\r
2283 Length = LogicalVolInt->NumberOfPartitions;\r
2284 for (Index = 0; Index < Length; Index += sizeof (UINT32)) {\r
2285 LsnsNo = *(UINT32 *)((UINT8 *)LogicalVolInt->Data + Index);\r
2286 if (LsnsNo == 0xFFFFFFFFUL) {\r
2287 //\r
2288 // Size not specified.\r
2289 //\r
2290 continue;\r
2291 }\r
2292\r
2293 *FreeSpaceSize += MultU64x32 ((UINT64)LsnsNo, LogicalBlockSize);\r
2294 }\r
2295\r
2296 Length = (LogicalVolInt->NumberOfPartitions * sizeof (UINT32)) << 1;\r
2297 for (; Index < Length; Index += sizeof (UINT32)) {\r
2298 LsnsNo = *(UINT32 *)((UINT8 *)LogicalVolInt->Data + Index);\r
2299 if (LsnsNo == 0xFFFFFFFFUL) {\r
2300 //\r
2301 // Size not specified.\r
2302 //\r
2303 continue;\r
2304 }\r
2305\r
2306 *VolumeSize += MultU64x32 ((UINT64)LsnsNo, LogicalBlockSize);\r
2307 }\r
2308\r
2309 CopyMem ((VOID *)&ExtentAd,(VOID *)&LogicalVolInt->NextIntegrityExtent,\r
2310 sizeof (UDF_EXTENT_AD));\r
2311 if (ExtentAd.ExtentLength > 0) {\r
2312 FreePool ((VOID *)LogicalVolInt);\r
2313 goto Read_Next_Sequence;\r
2314 }\r
2315\r
2316 FreePool ((VOID *)LogicalVolInt);\r
2317 }\r
2318\r
2319 return EFI_SUCCESS;\r
2320}\r
2321\r
2322/**\r
2323 Seek a file and read its data into memory on an UDF volume.\r
2324\r
2325 @param[in] BlockIo BlockIo interface.\r
2326 @param[in] DiskIo DiskIo interface.\r
2327 @param[in] Volume UDF volume information structure.\r
2328 @param[in] File File information structure.\r
2329 @param[in] FileSize Size of the file.\r
2330 @param[in out] FilePosition File position.\r
2331 @param[in out] Buffer File data.\r
2332 @param[in out] BufferSize Read size.\r
2333\r
2334 @retval EFI_SUCCESS File seeked and read.\r
2335 @retval EFI_UNSUPPORTED Extended Allocation Descriptors not supported.\r
2336 @retval EFI_NO_MEDIA The device has no media.\r
2337 @retval EFI_DEVICE_ERROR The device reported an error.\r
2338 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
2339 @retval EFI_OUT_OF_RESOURCES The file's recorded data was not read due to lack\r
2340 of resources.\r
2341\r
2342**/\r
2343EFI_STATUS\r
2344ReadFileData (\r
2345 IN EFI_BLOCK_IO_PROTOCOL *BlockIo,\r
2346 IN EFI_DISK_IO_PROTOCOL *DiskIo,\r
2347 IN UDF_VOLUME_INFO *Volume,\r
2348 IN UDF_FILE_INFO *File,\r
2349 IN UINT64 FileSize,\r
2350 IN OUT UINT64 *FilePosition,\r
2351 IN OUT VOID *Buffer,\r
2352 IN OUT UINT64 *BufferSize\r
2353 )\r
2354{\r
2355 EFI_STATUS Status;\r
2356 UDF_READ_FILE_INFO ReadFileInfo;\r
2357\r
2358 ReadFileInfo.Flags = READ_FILE_SEEK_AND_READ;\r
2359 ReadFileInfo.FilePosition = *FilePosition;\r
2360 ReadFileInfo.FileData = Buffer;\r
2361 ReadFileInfo.FileDataSize = *BufferSize;\r
2362 ReadFileInfo.FileSize = FileSize;\r
2363\r
2364 Status = ReadFile (\r
2365 BlockIo,\r
2366 DiskIo,\r
2367 Volume,\r
2368 &File->FileIdentifierDesc->Icb,\r
2369 File->FileEntry,\r
2370 &ReadFileInfo\r
2371 );\r
2372 if (EFI_ERROR (Status)) {\r
2373 return Status;\r
2374 }\r
2375\r
2376 *BufferSize = ReadFileInfo.FileDataSize;\r
2377 *FilePosition = ReadFileInfo.FilePosition;\r
2378\r
2379 return EFI_SUCCESS;\r
2380}\r
2381\r
2382/**\r
2383 Check if ControllerHandle supports an UDF file system.\r
2384\r
2385 @param[in] This Protocol instance pointer.\r
2386 @param[in] ControllerHandle Handle of device to test.\r
2387\r
2388 @retval EFI_SUCCESS UDF file system found.\r
2389 @retval EFI_UNSUPPORTED UDF file system not found.\r
2390\r
2391**/\r
2392EFI_STATUS\r
2393SupportUdfFileSystem (\r
2394 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
2395 IN EFI_HANDLE ControllerHandle\r
2396 )\r
2397{\r
2398 EFI_STATUS Status;\r
2399 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
2400 EFI_DEVICE_PATH_PROTOCOL *DevicePathNode;\r
2401 EFI_DEVICE_PATH_PROTOCOL *LastDevicePathNode;\r
2402 EFI_GUID *VendorDefinedGuid;\r
2403 EFI_GUID UdfDevPathGuid = EFI_UDF_DEVICE_PATH_GUID;\r
2404\r
2405 //\r
2406 // Open Device Path protocol on ControllerHandle\r
2407 //\r
2408 Status = gBS->OpenProtocol (\r
2409 ControllerHandle,\r
2410 &gEfiDevicePathProtocolGuid,\r
2411 (VOID **)&DevicePath,\r
2412 This->DriverBindingHandle,\r
2413 ControllerHandle,\r
2414 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
2415 );\r
2416 if (EFI_ERROR (Status)) {\r
2417 return EFI_UNSUPPORTED;\r
2418 }\r
2419\r
2420 Status = EFI_UNSUPPORTED;\r
2421\r
2422 //\r
2423 // Get last Device Path node\r
2424 //\r
2425 LastDevicePathNode = NULL;\r
2426 DevicePathNode = DevicePath;\r
2427 while (!IsDevicePathEnd (DevicePathNode)) {\r
2428 LastDevicePathNode = DevicePathNode;\r
2429 DevicePathNode = NextDevicePathNode (DevicePathNode);\r
2430 }\r
2431 //\r
2432 // Check if last Device Path node contains a Vendor-Defined Media Device Path\r
2433 // of an UDF file system.\r
2434 //\r
2435 if (LastDevicePathNode != NULL &&\r
2436 DevicePathType (LastDevicePathNode) == MEDIA_DEVICE_PATH &&\r
2437 DevicePathSubType (LastDevicePathNode) == MEDIA_VENDOR_DP) {\r
2438 VendorDefinedGuid = (EFI_GUID *)((UINTN)LastDevicePathNode +\r
2439 OFFSET_OF (VENDOR_DEVICE_PATH, Guid));\r
2440 if (CompareGuid (VendorDefinedGuid, &UdfDevPathGuid)) {\r
2441 Status = EFI_SUCCESS;\r
2442 }\r
2443 }\r
2444\r
2445 //\r
2446 // Close Device Path protocol on ControllerHandle\r
2447 //\r
2448 gBS->CloseProtocol (\r
2449 ControllerHandle,\r
2450 &gEfiDevicePathProtocolGuid,\r
2451 This->DriverBindingHandle,\r
2452 ControllerHandle\r
2453 );\r
2454\r
2455 return Status;\r
2456}\r