]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/Disk/UdfDxe/FileSystemOperations.c
MdeModulePkg/UdfDxe: don't return unset Status if INLINE_DATA req succeeds
[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
1085 if (Offset < 0) {\r
1086 Offset = -(Offset);\r
1087 }\r
1088 } else {\r
1089 Offset = 0;\r
1090 }\r
1091\r
1092 //\r
1093 // Done with seeking file. Start reading its data.\r
1094 //\r
1095 FinishedSeeking = TRUE;\r
1096\r
1097 Skip_File_Seek:\r
1098 //\r
1099 // Make sure we don't read more data than really wanted.\r
1100 //\r
1101 if (ExtentLength - Offset > BytesLeft) {\r
1102 DataLength = BytesLeft;\r
1103 } else {\r
1104 DataLength = ExtentLength - Offset;\r
1105 }\r
1106\r
1107 //\r
1108 // Read extent's data into FileData.\r
1109 //\r
1110 Status = DiskIo->ReadDisk (\r
1111 DiskIo,\r
1112 BlockIo->Media->MediaId,\r
1113 Offset + MultU64x32 (Lsn, LogicalBlockSize),\r
0b4c8f00 1114 (UINTN) DataLength,\r
99c9b949
PA
1115 (VOID *)((UINT8 *)ReadFileInfo->FileData +\r
1116 DataOffset)\r
1117 );\r
1118 if (EFI_ERROR (Status)) {\r
1119 goto Error_Read_Disk_Blk;\r
1120 }\r
1121\r
1122 //\r
1123 // Update current file's position.\r
1124 //\r
1125 DataOffset += DataLength;\r
1126 ReadFileInfo->FilePosition += DataLength;\r
1127\r
1128 BytesLeft -= DataLength;\r
1129 if (BytesLeft == 0) {\r
1130 //\r
1131 // There is no more file data to read.\r
1132 //\r
1133 Status = EFI_SUCCESS;\r
1134 goto Done;\r
1135 }\r
1136\r
1137 break;\r
1138 }\r
1139\r
1140 Skip_Ad:\r
1141 //\r
1142 // Point to the next AD (extent).\r
1143 //\r
1144 AdOffset += AD_LENGTH (RecordingFlags);\r
1145 }\r
1146\r
1147 break;\r
1148 case EXTENDED_ADS_SEQUENCE:\r
1149 // FIXME: Not supported. Got no volume with it, yet.\r
1150 ASSERT (FALSE);\r
1151 Status = EFI_UNSUPPORTED;\r
1152 break;\r
1153 }\r
1154\r
1155Done:\r
1156 if (DoFreeAed) {\r
1157 FreePool (Data);\r
1158 }\r
1159\r
1160 return Status;\r
1161\r
1162Error_Read_Disk_Blk:\r
1163Error_Alloc_Buffer_To_Next_Ad:\r
1164 if (ReadFileInfo->Flags != READ_FILE_SEEK_AND_READ) {\r
1165 FreePool (ReadFileInfo->FileData);\r
1166 }\r
1167\r
1168 if (DoFreeAed) {\r
1169 FreePool (Data);\r
1170 }\r
1171\r
1172Error_Get_Aed:\r
1173 return Status;\r
1174}\r
1175\r
1176//\r
1177// Find a file by its filename from a given Parent file.\r
1178//\r
1179EFI_STATUS\r
1180InternalFindFile (\r
1181 IN EFI_BLOCK_IO_PROTOCOL *BlockIo,\r
1182 IN EFI_DISK_IO_PROTOCOL *DiskIo,\r
1183 IN UDF_VOLUME_INFO *Volume,\r
1184 IN CHAR16 *FileName,\r
1185 IN UDF_FILE_INFO *Parent,\r
1186 IN UDF_LONG_ALLOCATION_DESCRIPTOR *Icb,\r
1187 OUT UDF_FILE_INFO *File\r
1188 )\r
1189{\r
1190 EFI_STATUS Status;\r
1191 UDF_FILE_IDENTIFIER_DESCRIPTOR *FileIdentifierDesc;\r
1192 UDF_READ_DIRECTORY_INFO ReadDirInfo;\r
1193 BOOLEAN Found;\r
1194 CHAR16 FoundFileName[UDF_FILENAME_LENGTH];\r
1195 VOID *CompareFileEntry;\r
1196\r
1197 //\r
1198 // Check if parent file is really directory.\r
1199 //\r
1200 if (!IS_FE_DIRECTORY (Parent->FileEntry)) {\r
1201 return EFI_NOT_FOUND;\r
1202 }\r
1203\r
1204 //\r
1205 // If FileName is current file or working directory, just duplicate Parent's\r
1206 // FE/EFE and FID descriptors.\r
1207 //\r
1208 if (StrCmp (FileName, L".") == 0) {\r
1209 DuplicateFe (BlockIo, Volume, Parent->FileEntry, &File->FileEntry);\r
1210 DuplicateFid (Parent->FileIdentifierDesc, &File->FileIdentifierDesc);\r
1211\r
1212 return EFI_SUCCESS;\r
1213 }\r
1214\r
1215 //\r
1216 // Start directory listing.\r
1217 //\r
1218 ZeroMem ((VOID *)&ReadDirInfo, sizeof (UDF_READ_DIRECTORY_INFO));\r
1219 Found = FALSE;\r
1220\r
1221 for (;;) {\r
1222 Status = ReadDirectoryEntry (\r
1223 BlockIo,\r
1224 DiskIo,\r
1225 Volume,\r
1226 Parent->FileIdentifierDesc ?\r
1227 &Parent->FileIdentifierDesc->Icb :\r
1228 Icb,\r
1229 Parent->FileEntry,\r
1230 &ReadDirInfo,\r
1231 &FileIdentifierDesc\r
1232 );\r
1233 if (EFI_ERROR (Status)) {\r
1234 if (Status == EFI_DEVICE_ERROR) {\r
1235 Status = EFI_NOT_FOUND;\r
1236 }\r
1237\r
1238 break;\r
1239 }\r
1240\r
1241 if (IS_FID_PARENT_FILE (FileIdentifierDesc)) {\r
1242 //\r
1243 // This FID contains the location (FE/EFE) of the parent directory of this\r
1244 // directory (Parent), and if FileName is either ".." or "\\", then it's\r
1245 // the expected FID.\r
1246 //\r
1247 if (StrCmp (FileName, L"..") == 0 || StrCmp (FileName, L"\\") == 0) {\r
1248 Found = TRUE;\r
1249 break;\r
1250 }\r
1251 } else {\r
1252 Status = GetFileNameFromFid (FileIdentifierDesc, FoundFileName);\r
1253 if (EFI_ERROR (Status)) {\r
1254 break;\r
1255 }\r
1256\r
1257 if (StrCmp (FileName, FoundFileName) == 0) {\r
1258 //\r
1259 // FID has been found. Prepare to find its respective FE/EFE.\r
1260 //\r
1261 Found = TRUE;\r
1262 break;\r
1263 }\r
1264 }\r
1265\r
1266 FreePool ((VOID *)FileIdentifierDesc);\r
1267 }\r
1268\r
1269 if (ReadDirInfo.DirectoryData != NULL) {\r
1270 //\r
1271 // Free all allocated resources for the directory listing.\r
1272 //\r
1273 FreePool (ReadDirInfo.DirectoryData);\r
1274 }\r
1275\r
1276 if (Found) {\r
1277 Status = EFI_SUCCESS;\r
1278\r
1279 File->FileIdentifierDesc = FileIdentifierDesc;\r
1280\r
1281 //\r
1282 // If the requested file is root directory, then the FE/EFE was already\r
1283 // retrieved in UdfOpenVolume() function, thus no need to find it again.\r
1284 //\r
1285 // Otherwise, find FE/EFE from the respective FID.\r
1286 //\r
1287 if (StrCmp (FileName, L"\\") != 0) {\r
1288 Status = FindFileEntry (\r
1289 BlockIo,\r
1290 DiskIo,\r
1291 Volume,\r
1292 &FileIdentifierDesc->Icb,\r
1293 &CompareFileEntry\r
1294 );\r
1295 if (EFI_ERROR (Status)) {\r
1296 goto Error_Find_Fe;\r
1297 }\r
1298\r
1299 //\r
1300 // Make sure that both Parent's FE/EFE and found FE/EFE are not equal.\r
1301 //\r
1302 if (CompareMem ((VOID *)Parent->FileEntry, (VOID *)CompareFileEntry,\r
1303 Volume->FileEntrySize) != 0) {\r
1304 File->FileEntry = CompareFileEntry;\r
1305 } else {\r
1306 FreePool ((VOID *)FileIdentifierDesc);\r
1307 FreePool ((VOID *)CompareFileEntry);\r
1308 Status = EFI_NOT_FOUND;\r
1309 }\r
1310 }\r
1311 }\r
1312\r
1313 return Status;\r
1314\r
1315Error_Find_Fe:\r
1316 FreePool ((VOID *)FileIdentifierDesc);\r
1317\r
1318 return Status;\r
1319}\r
1320\r
1321/**\r
1322 Read volume information on a medium which contains a valid UDF file system.\r
1323\r
1324 @param[in] BlockIo BlockIo interface.\r
1325 @param[in] DiskIo DiskIo interface.\r
1326 @param[out] Volume UDF volume information structure.\r
1327\r
1328 @retval EFI_SUCCESS Volume information read.\r
1329 @retval EFI_NO_MEDIA The device has no media.\r
1330 @retval EFI_DEVICE_ERROR The device reported an error.\r
1331 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
1332 @retval EFI_OUT_OF_RESOURCES The volume was not read due to lack of resources.\r
1333\r
1334**/\r
1335EFI_STATUS\r
1336ReadUdfVolumeInformation (\r
1337 IN EFI_BLOCK_IO_PROTOCOL *BlockIo,\r
1338 IN EFI_DISK_IO_PROTOCOL *DiskIo,\r
1339 OUT UDF_VOLUME_INFO *Volume\r
1340 )\r
1341{\r
1342 EFI_STATUS Status;\r
1343\r
1344 Status = ReadVolumeFileStructure (\r
1345 BlockIo,\r
1346 DiskIo,\r
1347 Volume\r
1348 );\r
1349 if (EFI_ERROR (Status)) {\r
1350 return Status;\r
1351 }\r
1352\r
1353 Status = GetFileSetDescriptors (\r
1354 BlockIo,\r
1355 DiskIo,\r
1356 Volume\r
1357 );\r
1358 if (EFI_ERROR (Status)) {\r
1359 CleanupVolumeInformation (Volume);\r
1360 }\r
1361\r
1362 return Status;\r
1363}\r
1364\r
1365/**\r
1366 Find the root directory on an UDF volume.\r
1367\r
1368 @param[in] BlockIo BlockIo interface.\r
1369 @param[in] DiskIo DiskIo interface.\r
1370 @param[in] Volume UDF volume information structure.\r
1371 @param[out] File Root directory file.\r
1372\r
1373 @retval EFI_SUCCESS Root directory found.\r
1374 @retval EFI_NO_MEDIA The device has no media.\r
1375 @retval EFI_DEVICE_ERROR The device reported an error.\r
1376 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
1377 @retval EFI_OUT_OF_RESOURCES The root directory was not found due to lack of\r
1378 resources.\r
1379\r
1380**/\r
1381EFI_STATUS\r
1382FindRootDirectory (\r
1383 IN EFI_BLOCK_IO_PROTOCOL *BlockIo,\r
1384 IN EFI_DISK_IO_PROTOCOL *DiskIo,\r
1385 IN UDF_VOLUME_INFO *Volume,\r
1386 OUT UDF_FILE_INFO *File\r
1387 )\r
1388{\r
1389 EFI_STATUS Status;\r
1390 UDF_FILE_INFO Parent;\r
1391\r
1392 Status = FindFileEntry (\r
1393 BlockIo,\r
1394 DiskIo,\r
1395 Volume,\r
1396 &Volume->FileSetDescs[0]->RootDirectoryIcb,\r
1397 &File->FileEntry\r
1398 );\r
1399 if (EFI_ERROR (Status)) {\r
1400 return Status;\r
1401 }\r
1402\r
1403 Parent.FileEntry = File->FileEntry;\r
1404 Parent.FileIdentifierDesc = NULL;\r
1405\r
1406 Status = FindFile (\r
1407 BlockIo,\r
1408 DiskIo,\r
1409 Volume,\r
1410 L"\\",\r
1411 NULL,\r
1412 &Parent,\r
1413 &Volume->FileSetDescs[0]->RootDirectoryIcb,\r
1414 File\r
1415 );\r
1416 if (EFI_ERROR (Status)) {\r
1417 FreePool (File->FileEntry);\r
1418 }\r
1419\r
1420 return Status;\r
1421}\r
1422\r
1423/**\r
1424 Find either a File Entry or a Extended File Entry from a given ICB.\r
1425\r
1426 @param[in] BlockIo BlockIo interface.\r
1427 @param[in] DiskIo DiskIo interface.\r
1428 @param[in] Volume UDF volume information structure.\r
1429 @param[in] Icb ICB of the FID.\r
1430 @param[out] FileEntry File Entry or Extended File Entry.\r
1431\r
1432 @retval EFI_SUCCESS File Entry or Extended File Entry found.\r
1433 @retval EFI_NO_MEDIA The device has no media.\r
1434 @retval EFI_DEVICE_ERROR The device reported an error.\r
1435 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
1436 @retval EFI_OUT_OF_RESOURCES The FE/EFE entry was not found due to lack of\r
1437 resources.\r
1438\r
1439**/\r
1440EFI_STATUS\r
1441FindFileEntry (\r
1442 IN EFI_BLOCK_IO_PROTOCOL *BlockIo,\r
1443 IN EFI_DISK_IO_PROTOCOL *DiskIo,\r
1444 IN UDF_VOLUME_INFO *Volume,\r
1445 IN UDF_LONG_ALLOCATION_DESCRIPTOR *Icb,\r
1446 OUT VOID **FileEntry\r
1447 )\r
1448{\r
1449 EFI_STATUS Status;\r
1450 UINT64 Lsn;\r
1451 UINT32 LogicalBlockSize;\r
1452\r
1453 Lsn = GetLongAdLsn (Volume, Icb);\r
1454 LogicalBlockSize = LV_BLOCK_SIZE (Volume, UDF_DEFAULT_LV_NUM);\r
1455\r
1456 *FileEntry = AllocateZeroPool (Volume->FileEntrySize);\r
1457 if (*FileEntry == NULL) {\r
1458 return EFI_OUT_OF_RESOURCES;\r
1459 }\r
1460\r
1461 //\r
1462 // Read extent.\r
1463 //\r
1464 Status = DiskIo->ReadDisk (\r
1465 DiskIo,\r
1466 BlockIo->Media->MediaId,\r
1467 MultU64x32 (Lsn, LogicalBlockSize),\r
1468 Volume->FileEntrySize,\r
1469 *FileEntry\r
1470 );\r
1471 if (EFI_ERROR (Status)) {\r
1472 goto Error_Read_Disk_Blk;\r
1473 }\r
1474\r
1475 //\r
1476 // Check if the read extent contains a valid Tag Identifier for the expected\r
1477 // FE/EFE.\r
1478 //\r
1479 if (!IS_FE (*FileEntry) && !IS_EFE (*FileEntry)) {\r
1480 Status = EFI_VOLUME_CORRUPTED;\r
1481 goto Error_Invalid_Fe;\r
1482 }\r
1483\r
1484 return EFI_SUCCESS;\r
1485\r
1486Error_Invalid_Fe:\r
1487Error_Read_Disk_Blk:\r
1488 FreePool (*FileEntry);\r
1489\r
1490 return Status;\r
1491}\r
1492\r
1493/**\r
1494 Find a file given its absolute path on an UDF volume.\r
1495\r
1496 @param[in] BlockIo BlockIo interface.\r
1497 @param[in] DiskIo DiskIo interface.\r
1498 @param[in] Volume UDF volume information structure.\r
1499 @param[in] FilePath File's absolute path.\r
1500 @param[in] Root Root directory file.\r
1501 @param[in] Parent Parent directory file.\r
1502 @param[out] File Found file.\r
1503\r
1504 @retval EFI_SUCCESS @p FilePath was found.\r
1505 @retval EFI_NO_MEDIA The device has no media.\r
1506 @retval EFI_DEVICE_ERROR The device reported an error.\r
1507 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
1508 @retval EFI_OUT_OF_RESOURCES The @p FilePath file was not found due to lack of\r
1509 resources.\r
1510\r
1511**/\r
1512EFI_STATUS\r
1513FindFile (\r
1514 IN EFI_BLOCK_IO_PROTOCOL *BlockIo,\r
1515 IN EFI_DISK_IO_PROTOCOL *DiskIo,\r
1516 IN UDF_VOLUME_INFO *Volume,\r
1517 IN CHAR16 *FilePath,\r
1518 IN UDF_FILE_INFO *Root,\r
1519 IN UDF_FILE_INFO *Parent,\r
1520 IN UDF_LONG_ALLOCATION_DESCRIPTOR *Icb,\r
1521 OUT UDF_FILE_INFO *File\r
1522 )\r
1523{\r
1524 EFI_STATUS Status;\r
1525 CHAR16 FileName[UDF_FILENAME_LENGTH];\r
1526 CHAR16 *FileNamePointer;\r
1527 UDF_FILE_INFO PreviousFile;\r
1528 VOID *FileEntry;\r
1529\r
1530 Status = EFI_NOT_FOUND;\r
1531\r
1532 CopyMem ((VOID *)&PreviousFile, (VOID *)Parent, sizeof (UDF_FILE_INFO));\r
1533 while (*FilePath != L'\0') {\r
1534 FileNamePointer = FileName;\r
1535 while (*FilePath != L'\0' && *FilePath != L'\\') {\r
1536 *FileNamePointer++ = *FilePath++;\r
1537 }\r
1538\r
1539 *FileNamePointer = L'\0';\r
1540 if (FileName[0] == L'\0') {\r
1541 //\r
1542 // Open root directory.\r
1543 //\r
1544 if (Root == NULL) {\r
1545 //\r
1546 // There is no file found for the root directory yet. So, find only its\r
1547 // FID by now.\r
1548 //\r
1549 // See UdfOpenVolume() function.\r
1550 //\r
1551 Status = InternalFindFile (BlockIo,\r
1552 DiskIo,\r
1553 Volume,\r
1554 L"\\",\r
1555 &PreviousFile,\r
1556 Icb,\r
1557 File);\r
1558 } else {\r
1559 //\r
1560 // We've already a file pointer (Root) for the root directory. Duplicate\r
1561 // its FE/EFE and FID descriptors.\r
1562 //\r
1563 DuplicateFe (BlockIo, Volume, Root->FileEntry, &File->FileEntry);\r
1564 DuplicateFid (Root->FileIdentifierDesc, &File->FileIdentifierDesc);\r
1565 Status = EFI_SUCCESS;\r
1566 }\r
1567 } else {\r
1568 //\r
1569 // No root directory. Find filename from the current directory.\r
1570 //\r
1571 Status = InternalFindFile (BlockIo,\r
1572 DiskIo,\r
1573 Volume,\r
1574 FileName,\r
1575 &PreviousFile,\r
1576 Icb,\r
1577 File);\r
1578 }\r
1579\r
1580 if (EFI_ERROR (Status)) {\r
1581 return Status;\r
1582 }\r
1583\r
1584 //\r
1585 // If the found file is a symlink, then find its respective FE/EFE and\r
1586 // FID descriptors.\r
1587 //\r
1588 if (IS_FE_SYMLINK (File->FileEntry)) {\r
1589 FreePool ((VOID *)File->FileIdentifierDesc);\r
1590\r
1591 FileEntry = File->FileEntry;\r
1592\r
1593 Status = ResolveSymlink (BlockIo,\r
1594 DiskIo,\r
1595 Volume,\r
1596 &PreviousFile,\r
1597 FileEntry,\r
1598 File);\r
1599\r
1600 FreePool (FileEntry);\r
1601\r
1602 if (EFI_ERROR (Status)) {\r
1603 return Status;\r
1604 }\r
1605 }\r
1606\r
1607 if (CompareMem ((VOID *)&PreviousFile, (VOID *)Parent,\r
1608 sizeof (UDF_FILE_INFO)) != 0) {\r
1609 CleanupFileInformation (&PreviousFile);\r
1610 }\r
1611\r
1612 CopyMem ((VOID *)&PreviousFile, (VOID *)File, sizeof (UDF_FILE_INFO));\r
1613 if (*FilePath != L'\0' && *FilePath == L'\\') {\r
1614 FilePath++;\r
1615 }\r
1616 }\r
1617\r
1618 return Status;\r
1619}\r
1620\r
1621/**\r
1622 Read a directory entry at a time on an UDF volume.\r
1623\r
1624 @param[in] BlockIo BlockIo interface.\r
1625 @param[in] DiskIo DiskIo interface.\r
1626 @param[in] Volume UDF volume information structure.\r
1627 @param[in] ParentIcb ICB of the parent file.\r
1628 @param[in] FileEntryData FE/EFE of the parent file.\r
1629 @param[in out] ReadDirInfo Next read directory listing structure\r
1630 information.\r
1631 @param[out] FoundFid File Identifier Descriptor pointer.\r
1632\r
1633 @retval EFI_SUCCESS Directory entry read.\r
1634 @retval EFI_UNSUPPORTED Extended Allocation Descriptors not supported.\r
1635 @retval EFI_NO_MEDIA The device has no media.\r
1636 @retval EFI_DEVICE_ERROR The device reported an error.\r
1637 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
1638 @retval EFI_OUT_OF_RESOURCES The directory entry was not read due to lack of\r
1639 resources.\r
1640\r
1641**/\r
1642EFI_STATUS\r
1643ReadDirectoryEntry (\r
1644 IN EFI_BLOCK_IO_PROTOCOL *BlockIo,\r
1645 IN EFI_DISK_IO_PROTOCOL *DiskIo,\r
1646 IN UDF_VOLUME_INFO *Volume,\r
1647 IN UDF_LONG_ALLOCATION_DESCRIPTOR *ParentIcb,\r
1648 IN VOID *FileEntryData,\r
1649 IN OUT UDF_READ_DIRECTORY_INFO *ReadDirInfo,\r
1650 OUT UDF_FILE_IDENTIFIER_DESCRIPTOR **FoundFid\r
1651 )\r
1652{\r
1653 EFI_STATUS Status;\r
1654 UDF_READ_FILE_INFO ReadFileInfo;\r
1655 UDF_FILE_IDENTIFIER_DESCRIPTOR *FileIdentifierDesc;\r
1656\r
1657 if (ReadDirInfo->DirectoryData == NULL) {\r
1658 //\r
1659 // The directory's recorded data has not been read yet. So let's cache it\r
1660 // into memory and the next calls won't need to read it again.\r
1661 //\r
1662 ReadFileInfo.Flags = READ_FILE_ALLOCATE_AND_READ;\r
1663\r
1664 Status = ReadFile (\r
1665 BlockIo,\r
1666 DiskIo,\r
1667 Volume,\r
1668 ParentIcb,\r
1669 FileEntryData,\r
1670 &ReadFileInfo\r
1671 );\r
1672 if (EFI_ERROR (Status)) {\r
1673 return Status;\r
1674 }\r
1675\r
1676 //\r
1677 // Fill in ReadDirInfo structure with the read directory's data information.\r
1678 //\r
1679 ReadDirInfo->DirectoryData = ReadFileInfo.FileData;\r
1680 ReadDirInfo->DirectoryLength = ReadFileInfo.ReadLength;\r
1681 }\r
1682\r
1683 do {\r
1684 if (ReadDirInfo->FidOffset >= ReadDirInfo->DirectoryLength) {\r
1685 //\r
1686 // There are no longer FIDs for this directory. By returning\r
1687 // EFI_DEVICE_ERROR to the callee will indicate end of directory\r
1688 // listening.\r
1689 //\r
1690 return EFI_DEVICE_ERROR;\r
1691 }\r
1692\r
1693 //\r
1694 // Get FID for this entry.\r
1695 //\r
1696 FileIdentifierDesc = GET_FID_FROM_ADS (ReadDirInfo->DirectoryData,\r
1697 ReadDirInfo->FidOffset);\r
1698 //\r
1699 // Update FidOffset to point to next FID.\r
1700 //\r
1701 ReadDirInfo->FidOffset += GetFidDescriptorLength (FileIdentifierDesc);\r
1702 } while (IS_FID_DELETED_FILE (FileIdentifierDesc));\r
1703\r
1704 DuplicateFid (FileIdentifierDesc, FoundFid);\r
1705\r
1706 return EFI_SUCCESS;\r
1707}\r
1708\r
1709/**\r
1710 Get a filename (encoded in OSTA-compressed format) from a File Identifier\r
1711 Descriptor on an UDF volume.\r
1712\r
1713 @param[in] FileIdentifierDesc File Identifier Descriptor pointer.\r
1714 @param[out] FileName Decoded filename.\r
1715\r
1716 @retval EFI_SUCCESS Filename decoded and read.\r
1717 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
1718**/\r
1719EFI_STATUS\r
1720GetFileNameFromFid (\r
1721 IN UDF_FILE_IDENTIFIER_DESCRIPTOR *FileIdentifierDesc,\r
1722 OUT CHAR16 *FileName\r
1723 )\r
1724{\r
1725 UINT8 *OstaCompressed;\r
1726 UINT8 CompressionId;\r
1727 UINT8 Length;\r
1728 UINTN Index;\r
1729\r
1730 OstaCompressed =\r
1731 (UINT8 *)(\r
1732 (UINT8 *)FileIdentifierDesc->Data +\r
1733 FileIdentifierDesc->LengthOfImplementationUse\r
1734 );\r
1735\r
1736 CompressionId = OstaCompressed[0];\r
1737 if (!IS_VALID_COMPRESSION_ID (CompressionId)) {\r
1738 return EFI_VOLUME_CORRUPTED;\r
1739 }\r
1740\r
1741 //\r
1742 // Decode filename.\r
1743 //\r
1744 Length = FileIdentifierDesc->LengthOfFileIdentifier;\r
1745 for (Index = 1; Index < Length; Index++) {\r
1746 if (CompressionId == 16) {\r
1747 *FileName = OstaCompressed[Index++] << 8;\r
1748 } else {\r
1749 *FileName = 0;\r
1750 }\r
1751\r
1752 if (Index < Length) {\r
1753 *FileName |= OstaCompressed[Index];\r
1754 }\r
1755\r
1756 FileName++;\r
1757 }\r
1758\r
1759 *FileName = L'\0';\r
1760\r
1761 return EFI_SUCCESS;\r
1762}\r
1763\r
1764/**\r
1765 Resolve a symlink file on an UDF volume.\r
1766\r
1767 @param[in] BlockIo BlockIo interface.\r
1768 @param[in] DiskIo DiskIo interface.\r
1769 @param[in] Volume UDF volume information structure.\r
1770 @param[in] Parent Parent file.\r
1771 @param[in] FileEntryData FE/EFE structure pointer.\r
1772 @param[out] File Resolved file.\r
1773\r
1774 @retval EFI_SUCCESS Symlink file resolved.\r
1775 @retval EFI_UNSUPPORTED Extended Allocation Descriptors not supported.\r
1776 @retval EFI_NO_MEDIA The device has no media.\r
1777 @retval EFI_DEVICE_ERROR The device reported an error.\r
1778 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
1779 @retval EFI_OUT_OF_RESOURCES The symlink file was not resolved due to lack of\r
1780 resources.\r
1781\r
1782**/\r
1783EFI_STATUS\r
1784ResolveSymlink (\r
1785 IN EFI_BLOCK_IO_PROTOCOL *BlockIo,\r
1786 IN EFI_DISK_IO_PROTOCOL *DiskIo,\r
1787 IN UDF_VOLUME_INFO *Volume,\r
1788 IN UDF_FILE_INFO *Parent,\r
1789 IN VOID *FileEntryData,\r
1790 OUT UDF_FILE_INFO *File\r
1791 )\r
1792{\r
1793 EFI_STATUS Status;\r
1794 UDF_READ_FILE_INFO ReadFileInfo;\r
1795 UINT8 *Data;\r
1796 UINT64 Length;\r
1797 UINT8 *EndData;\r
1798 UDF_PATH_COMPONENT *PathComp;\r
1799 UINT8 PathCompLength;\r
1800 CHAR16 FileName[UDF_FILENAME_LENGTH];\r
1801 CHAR16 *C;\r
1802 UINTN Index;\r
1803 UINT8 CompressionId;\r
1804 UDF_FILE_INFO PreviousFile;\r
1805\r
1806 //\r
1807 // Symlink files on UDF volumes do not contain so much data other than\r
1808 // Path Components which resolves to real filenames, so it's OK to read in\r
1809 // all its data here -- usually the data will be inline with the FE/EFE for\r
1810 // lower filenames.\r
1811 //\r
1812 ReadFileInfo.Flags = READ_FILE_ALLOCATE_AND_READ;\r
1813\r
1814 Status = ReadFile (\r
1815 BlockIo,\r
1816 DiskIo,\r
1817 Volume,\r
1818 &Parent->FileIdentifierDesc->Icb,\r
1819 FileEntryData,\r
1820 &ReadFileInfo\r
1821 );\r
1822 if (EFI_ERROR (Status)) {\r
1823 return Status;\r
1824 }\r
1825\r
1826 Length = ReadFileInfo.ReadLength;\r
1827\r
1828 Data = (UINT8 *)ReadFileInfo.FileData;\r
1829 EndData = Data + Length;\r
1830\r
1831 CopyMem ((VOID *)&PreviousFile, (VOID *)Parent, sizeof (UDF_FILE_INFO));\r
1832\r
1833 for (;;) {\r
1834 PathComp = (UDF_PATH_COMPONENT *)Data;\r
1835\r
1836 PathCompLength = PathComp->LengthOfComponentIdentifier;\r
1837\r
1838 switch (PathComp->ComponentType) {\r
1839 case 1:\r
1840 //\r
1841 // This Path Component specifies the root directory hierarchy subject to\r
1842 // agreement between the originator and recipient of the medium. Skip it.\r
1843 //\r
1844 // Fall through.\r
1845 //\r
1846 case 2:\r
1847 //\r
1848 // "\\." of the current directory. Read next Path Component.\r
1849 //\r
1850 goto Next_Path_Component;\r
1851 case 3:\r
1852 //\r
1853 // ".." (parent directory). Go to it.\r
1854 //\r
1855 CopyMem ((VOID *)FileName, L"..", 6);\r
1856 break;\r
1857 case 4:\r
1858 //\r
1859 // "." (current file). Duplicate both FE/EFE and FID of this file.\r
1860 //\r
1861 DuplicateFe (BlockIo, Volume, PreviousFile.FileEntry, &File->FileEntry);\r
1862 DuplicateFid (PreviousFile.FileIdentifierDesc,\r
1863 &File->FileIdentifierDesc);\r
1864 goto Next_Path_Component;\r
1865 case 5:\r
1866 //\r
1867 // This Path Component identifies an object, either a file or a\r
1868 // directory or an alias.\r
1869 //\r
1870 // Decode it from the compressed data in ComponentIdentifier and find\r
1871 // respective path.\r
1872 //\r
1873 CompressionId = PathComp->ComponentIdentifier[0];\r
1874 if (!IS_VALID_COMPRESSION_ID (CompressionId)) {\r
1875 return EFI_VOLUME_CORRUPTED;\r
1876 }\r
1877\r
1878 C = FileName;\r
1879 for (Index = 1; Index < PathCompLength; Index++) {\r
1880 if (CompressionId == 16) {\r
1881 *C = *(UINT8 *)((UINT8 *)PathComp->ComponentIdentifier +\r
1882 Index) << 8;\r
1883 Index++;\r
1884 } else {\r
1885 *C = 0;\r
1886 }\r
1887\r
1888 if (Index < Length) {\r
1889 *C |= *(UINT8 *)((UINT8 *)PathComp->ComponentIdentifier + Index);\r
1890 }\r
1891\r
1892 C++;\r
1893 }\r
1894\r
1895 *C = L'\0';\r
1896 break;\r
1897 }\r
1898\r
1899 //\r
1900 // Find file from the read filename in symlink's file data.\r
1901 //\r
1902 Status = InternalFindFile (\r
1903 BlockIo,\r
1904 DiskIo,\r
1905 Volume,\r
1906 FileName,\r
1907 &PreviousFile,\r
1908 NULL,\r
1909 File\r
1910 );\r
1911 if (EFI_ERROR (Status)) {\r
1912 goto Error_Find_File;\r
1913 }\r
1914\r
1915 Next_Path_Component:\r
1916 Data += sizeof (UDF_PATH_COMPONENT) + PathCompLength;\r
1917 if (Data >= EndData) {\r
1918 break;\r
1919 }\r
1920\r
1921 if (CompareMem ((VOID *)&PreviousFile, (VOID *)Parent,\r
1922 sizeof (UDF_FILE_INFO)) != 0) {\r
1923 CleanupFileInformation (&PreviousFile);\r
1924 }\r
1925\r
1926 CopyMem ((VOID *)&PreviousFile, (VOID *)File, sizeof (UDF_FILE_INFO));\r
1927 }\r
1928\r
1929 //\r
1930 // Unmap the symlink file.\r
1931 //\r
1932 FreePool (ReadFileInfo.FileData);\r
1933\r
1934 return EFI_SUCCESS;\r
1935\r
1936Error_Find_File:\r
1937 if (CompareMem ((VOID *)&PreviousFile, (VOID *)Parent,\r
1938 sizeof (UDF_FILE_INFO)) != 0) {\r
1939 CleanupFileInformation (&PreviousFile);\r
1940 }\r
1941\r
1942 FreePool (ReadFileInfo.FileData);\r
1943\r
1944 return Status;\r
1945}\r
1946\r
1947/**\r
1948 Clean up in-memory UDF volume information.\r
1949\r
1950 @param[in] Volume Volume information pointer.\r
1951\r
1952**/\r
1953VOID\r
1954CleanupVolumeInformation (\r
1955 IN UDF_VOLUME_INFO *Volume\r
1956 )\r
1957{\r
1958 UINTN Index;\r
1959\r
1960 if (Volume->LogicalVolDescs != NULL) {\r
1961 for (Index = 0; Index < Volume->LogicalVolDescsNo; Index++) {\r
1962 FreePool ((VOID *)Volume->LogicalVolDescs[Index]);\r
1963 }\r
1964 FreePool ((VOID *)Volume->LogicalVolDescs);\r
1965 }\r
1966\r
1967 if (Volume->PartitionDescs != NULL) {\r
1968 for (Index = 0; Index < Volume->PartitionDescsNo; Index++) {\r
1969 FreePool ((VOID *)Volume->PartitionDescs[Index]);\r
1970 }\r
1971 FreePool ((VOID *)Volume->PartitionDescs);\r
1972 }\r
1973\r
1974 if (Volume->FileSetDescs != NULL) {\r
1975 for (Index = 0; Index < Volume->FileSetDescsNo; Index++) {\r
1976 FreePool ((VOID *)Volume->FileSetDescs[Index]);\r
1977 }\r
1978 FreePool ((VOID *)Volume->FileSetDescs);\r
1979 }\r
1980\r
1981 ZeroMem ((VOID *)Volume, sizeof (UDF_VOLUME_INFO));\r
1982}\r
1983\r
1984/**\r
1985 Clean up in-memory UDF file information.\r
1986\r
1987 @param[in] File File information pointer.\r
1988\r
1989**/\r
1990VOID\r
1991CleanupFileInformation (\r
1992 IN UDF_FILE_INFO *File\r
1993 )\r
1994{\r
1995 if (File->FileEntry != NULL) {\r
1996 FreePool (File->FileEntry);\r
1997 }\r
1998 if (File->FileIdentifierDesc != NULL) {\r
1999 FreePool ((VOID *)File->FileIdentifierDesc);\r
2000 }\r
2001\r
2002 ZeroMem ((VOID *)File, sizeof (UDF_FILE_INFO));\r
2003}\r
2004\r
2005/**\r
2006 Find a file from its absolute path on an UDF volume.\r
2007\r
2008 @param[in] BlockIo BlockIo interface.\r
2009 @param[in] DiskIo DiskIo interface.\r
2010 @param[in] Volume UDF volume information structure.\r
2011 @param[in] File File information structure.\r
2012 @param[out] Size Size of the file.\r
2013\r
2014 @retval EFI_SUCCESS File size calculated and set in @p Size.\r
2015 @retval EFI_UNSUPPORTED Extended Allocation Descriptors not supported.\r
2016 @retval EFI_NO_MEDIA The device has no media.\r
2017 @retval EFI_DEVICE_ERROR The device reported an error.\r
2018 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
2019 @retval EFI_OUT_OF_RESOURCES The file size was not calculated due to lack of\r
2020 resources.\r
2021\r
2022**/\r
2023EFI_STATUS\r
2024GetFileSize (\r
2025 IN EFI_BLOCK_IO_PROTOCOL *BlockIo,\r
2026 IN EFI_DISK_IO_PROTOCOL *DiskIo,\r
2027 IN UDF_VOLUME_INFO *Volume,\r
2028 IN UDF_FILE_INFO *File,\r
2029 OUT UINT64 *Size\r
2030 )\r
2031{\r
2032 EFI_STATUS Status;\r
2033 UDF_READ_FILE_INFO ReadFileInfo;\r
2034\r
2035 ReadFileInfo.Flags = READ_FILE_GET_FILESIZE;\r
2036\r
2037 Status = ReadFile (\r
2038 BlockIo,\r
2039 DiskIo,\r
2040 Volume,\r
2041 &File->FileIdentifierDesc->Icb,\r
2042 File->FileEntry,\r
2043 &ReadFileInfo\r
2044 );\r
2045 if (EFI_ERROR (Status)) {\r
2046 return Status;\r
2047 }\r
2048\r
2049 *Size = ReadFileInfo.ReadLength;\r
2050\r
2051 return EFI_SUCCESS;\r
2052}\r
2053\r
2054/**\r
2055 Set information about a file on an UDF volume.\r
2056\r
2057 @param[in] File File pointer.\r
2058 @param[in] FileSize Size of the file.\r
2059 @param[in] FileName Filename of the file.\r
2060 @param[in out] BufferSize Size of the returned file infomation.\r
2061 @param[out] Buffer Data of the returned file information.\r
2062\r
2063 @retval EFI_SUCCESS File information set.\r
2064 @retval EFI_NO_MEDIA The device has no media.\r
2065 @retval EFI_DEVICE_ERROR The device reported an error.\r
2066 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
2067 @retval EFI_OUT_OF_RESOURCES The file information was not set due to lack of\r
2068 resources.\r
2069\r
2070**/\r
2071EFI_STATUS\r
2072SetFileInfo (\r
2073 IN UDF_FILE_INFO *File,\r
2074 IN UINT64 FileSize,\r
2075 IN CHAR16 *FileName,\r
2076 IN OUT UINTN *BufferSize,\r
2077 OUT VOID *Buffer\r
2078 )\r
2079{\r
2080 UINTN FileInfoLength;\r
2081 EFI_FILE_INFO *FileInfo;\r
2082 UDF_FILE_ENTRY *FileEntry;\r
2083 UDF_EXTENDED_FILE_ENTRY *ExtendedFileEntry;\r
2084\r
2085 //\r
2086 // Calculate the needed size for the EFI_FILE_INFO structure.\r
2087 //\r
2088 FileInfoLength = sizeof (EFI_FILE_INFO) + (FileName ?\r
2089 StrSize (FileName) :\r
2090 sizeof (CHAR16));\r
2091 if (*BufferSize < FileInfoLength) {\r
2092 //\r
2093 // The given Buffer has no size enough for EFI_FILE_INFO structure.\r
2094 //\r
2095 *BufferSize = FileInfoLength;\r
2096 return EFI_BUFFER_TOO_SMALL;\r
2097 }\r
2098\r
2099 //\r
2100 // Buffer now contains room enough to store EFI_FILE_INFO structure.\r
2101 // Now, fill it in with all necessary information about the file.\r
2102 //\r
2103 FileInfo = (EFI_FILE_INFO *)Buffer;\r
2104 FileInfo->Size = FileInfoLength;\r
2105 FileInfo->Attribute &= ~EFI_FILE_VALID_ATTR;\r
2106 FileInfo->Attribute |= EFI_FILE_READ_ONLY;\r
2107\r
2108 if (IS_FID_DIRECTORY_FILE (File->FileIdentifierDesc)) {\r
2109 FileInfo->Attribute |= EFI_FILE_DIRECTORY;\r
2110 } else if (IS_FID_NORMAL_FILE (File->FileIdentifierDesc)) {\r
2111 FileInfo->Attribute |= EFI_FILE_ARCHIVE;\r
2112 }\r
2113\r
2114 if (IS_FID_HIDDEN_FILE (File->FileIdentifierDesc)) {\r
2115 FileInfo->Attribute |= EFI_FILE_HIDDEN;\r
2116 }\r
2117\r
2118 if (IS_FE (File->FileEntry)) {\r
2119 FileEntry = (UDF_FILE_ENTRY *)File->FileEntry;\r
2120\r
2121 //\r
2122 // Check if FE has the system attribute set.\r
2123 //\r
2124 if (FileEntry->IcbTag.Flags & (1 << 10)) {\r
2125 FileInfo->Attribute |= EFI_FILE_SYSTEM;\r
2126 }\r
2127\r
2128 FileInfo->FileSize = FileSize;\r
2129 FileInfo->PhysicalSize = FileSize;\r
2130\r
2131 FileInfo->CreateTime.Year = FileEntry->AccessTime.Year;\r
2132 FileInfo->CreateTime.Month = FileEntry->AccessTime.Month;\r
2133 FileInfo->CreateTime.Day = FileEntry->AccessTime.Day;\r
2134 FileInfo->CreateTime.Hour = FileEntry->AccessTime.Hour;\r
2135 FileInfo->CreateTime.Minute = FileEntry->AccessTime.Second;\r
2136 FileInfo->CreateTime.Second = FileEntry->AccessTime.Second;\r
2137 FileInfo->CreateTime.Nanosecond =\r
2138 FileEntry->AccessTime.HundredsOfMicroseconds;\r
2139\r
2140 FileInfo->LastAccessTime.Year =\r
2141 FileEntry->AccessTime.Year;\r
2142 FileInfo->LastAccessTime.Month =\r
2143 FileEntry->AccessTime.Month;\r
2144 FileInfo->LastAccessTime.Day =\r
2145 FileEntry->AccessTime.Day;\r
2146 FileInfo->LastAccessTime.Hour =\r
2147 FileEntry->AccessTime.Hour;\r
2148 FileInfo->LastAccessTime.Minute =\r
2149 FileEntry->AccessTime.Minute;\r
2150 FileInfo->LastAccessTime.Second =\r
2151 FileEntry->AccessTime.Second;\r
2152 FileInfo->LastAccessTime.Nanosecond =\r
2153 FileEntry->AccessTime.HundredsOfMicroseconds;\r
2154 } else if (IS_EFE (File->FileEntry)) {\r
2155 ExtendedFileEntry = (UDF_EXTENDED_FILE_ENTRY *)File->FileEntry;\r
2156\r
2157 //\r
2158 // Check if EFE has the system attribute set.\r
2159 //\r
2160 if (ExtendedFileEntry->IcbTag.Flags & (1 << 10)) {\r
2161 FileInfo->Attribute |= EFI_FILE_SYSTEM;\r
2162 }\r
2163\r
2164 FileInfo->FileSize = FileSize;\r
2165 FileInfo->PhysicalSize = FileSize;\r
2166\r
2167 FileInfo->CreateTime.Year = ExtendedFileEntry->CreationTime.Year;\r
2168 FileInfo->CreateTime.Month = ExtendedFileEntry->CreationTime.Month;\r
2169 FileInfo->CreateTime.Day = ExtendedFileEntry->CreationTime.Day;\r
2170 FileInfo->CreateTime.Hour = ExtendedFileEntry->CreationTime.Hour;\r
2171 FileInfo->CreateTime.Minute = ExtendedFileEntry->CreationTime.Second;\r
2172 FileInfo->CreateTime.Second = ExtendedFileEntry->CreationTime.Second;\r
2173 FileInfo->CreateTime.Nanosecond =\r
2174 ExtendedFileEntry->AccessTime.HundredsOfMicroseconds;\r
2175\r
2176 FileInfo->LastAccessTime.Year =\r
2177 ExtendedFileEntry->AccessTime.Year;\r
2178 FileInfo->LastAccessTime.Month =\r
2179 ExtendedFileEntry->AccessTime.Month;\r
2180 FileInfo->LastAccessTime.Day =\r
2181 ExtendedFileEntry->AccessTime.Day;\r
2182 FileInfo->LastAccessTime.Hour =\r
2183 ExtendedFileEntry->AccessTime.Hour;\r
2184 FileInfo->LastAccessTime.Minute =\r
2185 ExtendedFileEntry->AccessTime.Minute;\r
2186 FileInfo->LastAccessTime.Second =\r
2187 ExtendedFileEntry->AccessTime.Second;\r
2188 FileInfo->LastAccessTime.Nanosecond =\r
2189 ExtendedFileEntry->AccessTime.HundredsOfMicroseconds;\r
2190 }\r
2191\r
2192 FileInfo->CreateTime.TimeZone = EFI_UNSPECIFIED_TIMEZONE;\r
2193 FileInfo->CreateTime.Daylight = EFI_TIME_ADJUST_DAYLIGHT;\r
2194 FileInfo->LastAccessTime.TimeZone = EFI_UNSPECIFIED_TIMEZONE;\r
2195 FileInfo->LastAccessTime.Daylight = EFI_TIME_ADJUST_DAYLIGHT;\r
2196\r
2197 CopyMem ((VOID *)&FileInfo->ModificationTime,\r
2198 (VOID *)&FileInfo->LastAccessTime,\r
2199 sizeof (EFI_TIME));\r
2200\r
2201 if (FileName != NULL) {\r
2202 StrCpyS (FileInfo->FileName, StrLen (FileName) + 1, FileName);\r
2203 } else {\r
2204 FileInfo->FileName[0] = '\0';\r
2205 }\r
2206\r
2207 *BufferSize = FileInfoLength;\r
2208\r
2209 return EFI_SUCCESS;\r
2210}\r
2211\r
2212/**\r
2213 Get volume and free space size information of an UDF volume.\r
2214\r
2215 @param[in] BlockIo BlockIo interface.\r
2216 @param[in] DiskIo DiskIo interface.\r
2217 @param[in] Volume UDF volume information structure.\r
2218 @param[out] VolumeSize Volume size.\r
2219 @param[out] FreeSpaceSize Free space size.\r
2220\r
2221 @retval EFI_SUCCESS Volume and free space size calculated.\r
2222 @retval EFI_NO_MEDIA The device has no media.\r
2223 @retval EFI_DEVICE_ERROR The device reported an error.\r
2224 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
2225 @retval EFI_OUT_OF_RESOURCES The volume and free space size were not\r
2226 calculated due to lack of resources.\r
2227\r
2228**/\r
2229EFI_STATUS\r
2230GetVolumeSize (\r
2231 IN EFI_BLOCK_IO_PROTOCOL *BlockIo,\r
2232 IN EFI_DISK_IO_PROTOCOL *DiskIo,\r
2233 IN UDF_VOLUME_INFO *Volume,\r
2234 OUT UINT64 *VolumeSize,\r
2235 OUT UINT64 *FreeSpaceSize\r
2236 )\r
2237{\r
2238 UDF_EXTENT_AD ExtentAd;\r
2239 UINT32 LogicalBlockSize;\r
2240 UINT64 Lsn;\r
2241 EFI_STATUS Status;\r
2242 UDF_LOGICAL_VOLUME_INTEGRITY *LogicalVolInt;\r
2243 UINTN Index;\r
2244 UINTN Length;\r
2245 UINT32 LsnsNo;\r
2246\r
2247 *VolumeSize = 0;\r
2248 *FreeSpaceSize = 0;\r
2249\r
2250 for (Index = 0; Index < Volume->LogicalVolDescsNo; Index++) {\r
2251 CopyMem ((VOID *)&ExtentAd,\r
2252 (VOID *)&Volume->LogicalVolDescs[Index]->IntegritySequenceExtent,\r
2253 sizeof (UDF_EXTENT_AD));\r
2254 if (ExtentAd.ExtentLength == 0) {\r
2255 continue;\r
2256 }\r
2257\r
2258 LogicalBlockSize = LV_BLOCK_SIZE (Volume, Index);\r
2259\r
2260 Read_Next_Sequence:\r
2261 LogicalVolInt = (UDF_LOGICAL_VOLUME_INTEGRITY *)\r
2262 AllocatePool (ExtentAd.ExtentLength);\r
2263 if (LogicalVolInt == NULL) {\r
2264 return EFI_OUT_OF_RESOURCES;\r
2265 }\r
2266\r
2267 Lsn = (UINT64)ExtentAd.ExtentLocation;\r
2268\r
2269 Status = DiskIo->ReadDisk (\r
2270 DiskIo,\r
2271 BlockIo->Media->MediaId,\r
2272 MultU64x32 (Lsn, LogicalBlockSize),\r
2273 ExtentAd.ExtentLength,\r
2274 (VOID *)LogicalVolInt\r
2275 );\r
2276 if (EFI_ERROR (Status)) {\r
2277 FreePool ((VOID *)LogicalVolInt);\r
2278 return Status;\r
2279 }\r
2280\r
2281 if (!IS_LVID (LogicalVolInt)) {\r
2282 FreePool ((VOID *)LogicalVolInt);\r
2283 return EFI_VOLUME_CORRUPTED;\r
2284 }\r
2285\r
2286 Length = LogicalVolInt->NumberOfPartitions;\r
2287 for (Index = 0; Index < Length; Index += sizeof (UINT32)) {\r
2288 LsnsNo = *(UINT32 *)((UINT8 *)LogicalVolInt->Data + Index);\r
2289 if (LsnsNo == 0xFFFFFFFFUL) {\r
2290 //\r
2291 // Size not specified.\r
2292 //\r
2293 continue;\r
2294 }\r
2295\r
2296 *FreeSpaceSize += MultU64x32 ((UINT64)LsnsNo, LogicalBlockSize);\r
2297 }\r
2298\r
2299 Length = (LogicalVolInt->NumberOfPartitions * sizeof (UINT32)) << 1;\r
2300 for (; Index < Length; Index += sizeof (UINT32)) {\r
2301 LsnsNo = *(UINT32 *)((UINT8 *)LogicalVolInt->Data + Index);\r
2302 if (LsnsNo == 0xFFFFFFFFUL) {\r
2303 //\r
2304 // Size not specified.\r
2305 //\r
2306 continue;\r
2307 }\r
2308\r
2309 *VolumeSize += MultU64x32 ((UINT64)LsnsNo, LogicalBlockSize);\r
2310 }\r
2311\r
2312 CopyMem ((VOID *)&ExtentAd,(VOID *)&LogicalVolInt->NextIntegrityExtent,\r
2313 sizeof (UDF_EXTENT_AD));\r
2314 if (ExtentAd.ExtentLength > 0) {\r
2315 FreePool ((VOID *)LogicalVolInt);\r
2316 goto Read_Next_Sequence;\r
2317 }\r
2318\r
2319 FreePool ((VOID *)LogicalVolInt);\r
2320 }\r
2321\r
2322 return EFI_SUCCESS;\r
2323}\r
2324\r
2325/**\r
2326 Seek a file and read its data into memory on an UDF volume.\r
2327\r
2328 @param[in] BlockIo BlockIo interface.\r
2329 @param[in] DiskIo DiskIo interface.\r
2330 @param[in] Volume UDF volume information structure.\r
2331 @param[in] File File information structure.\r
2332 @param[in] FileSize Size of the file.\r
2333 @param[in out] FilePosition File position.\r
2334 @param[in out] Buffer File data.\r
2335 @param[in out] BufferSize Read size.\r
2336\r
2337 @retval EFI_SUCCESS File seeked and read.\r
2338 @retval EFI_UNSUPPORTED Extended Allocation Descriptors not supported.\r
2339 @retval EFI_NO_MEDIA The device has no media.\r
2340 @retval EFI_DEVICE_ERROR The device reported an error.\r
2341 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
2342 @retval EFI_OUT_OF_RESOURCES The file's recorded data was not read due to lack\r
2343 of resources.\r
2344\r
2345**/\r
2346EFI_STATUS\r
2347ReadFileData (\r
2348 IN EFI_BLOCK_IO_PROTOCOL *BlockIo,\r
2349 IN EFI_DISK_IO_PROTOCOL *DiskIo,\r
2350 IN UDF_VOLUME_INFO *Volume,\r
2351 IN UDF_FILE_INFO *File,\r
2352 IN UINT64 FileSize,\r
2353 IN OUT UINT64 *FilePosition,\r
2354 IN OUT VOID *Buffer,\r
2355 IN OUT UINT64 *BufferSize\r
2356 )\r
2357{\r
2358 EFI_STATUS Status;\r
2359 UDF_READ_FILE_INFO ReadFileInfo;\r
2360\r
2361 ReadFileInfo.Flags = READ_FILE_SEEK_AND_READ;\r
2362 ReadFileInfo.FilePosition = *FilePosition;\r
2363 ReadFileInfo.FileData = Buffer;\r
2364 ReadFileInfo.FileDataSize = *BufferSize;\r
2365 ReadFileInfo.FileSize = FileSize;\r
2366\r
2367 Status = ReadFile (\r
2368 BlockIo,\r
2369 DiskIo,\r
2370 Volume,\r
2371 &File->FileIdentifierDesc->Icb,\r
2372 File->FileEntry,\r
2373 &ReadFileInfo\r
2374 );\r
2375 if (EFI_ERROR (Status)) {\r
2376 return Status;\r
2377 }\r
2378\r
2379 *BufferSize = ReadFileInfo.FileDataSize;\r
2380 *FilePosition = ReadFileInfo.FilePosition;\r
2381\r
2382 return EFI_SUCCESS;\r
2383}\r
2384\r
2385/**\r
2386 Check if ControllerHandle supports an UDF file system.\r
2387\r
2388 @param[in] This Protocol instance pointer.\r
2389 @param[in] ControllerHandle Handle of device to test.\r
2390\r
2391 @retval EFI_SUCCESS UDF file system found.\r
2392 @retval EFI_UNSUPPORTED UDF file system not found.\r
2393\r
2394**/\r
2395EFI_STATUS\r
2396SupportUdfFileSystem (\r
2397 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
2398 IN EFI_HANDLE ControllerHandle\r
2399 )\r
2400{\r
2401 EFI_STATUS Status;\r
2402 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
2403 EFI_DEVICE_PATH_PROTOCOL *DevicePathNode;\r
2404 EFI_DEVICE_PATH_PROTOCOL *LastDevicePathNode;\r
2405 EFI_GUID *VendorDefinedGuid;\r
2406 EFI_GUID UdfDevPathGuid = EFI_UDF_DEVICE_PATH_GUID;\r
2407\r
2408 //\r
2409 // Open Device Path protocol on ControllerHandle\r
2410 //\r
2411 Status = gBS->OpenProtocol (\r
2412 ControllerHandle,\r
2413 &gEfiDevicePathProtocolGuid,\r
2414 (VOID **)&DevicePath,\r
2415 This->DriverBindingHandle,\r
2416 ControllerHandle,\r
2417 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
2418 );\r
2419 if (EFI_ERROR (Status)) {\r
2420 return EFI_UNSUPPORTED;\r
2421 }\r
2422\r
2423 Status = EFI_UNSUPPORTED;\r
2424\r
2425 //\r
2426 // Get last Device Path node\r
2427 //\r
2428 LastDevicePathNode = NULL;\r
2429 DevicePathNode = DevicePath;\r
2430 while (!IsDevicePathEnd (DevicePathNode)) {\r
2431 LastDevicePathNode = DevicePathNode;\r
2432 DevicePathNode = NextDevicePathNode (DevicePathNode);\r
2433 }\r
2434 //\r
2435 // Check if last Device Path node contains a Vendor-Defined Media Device Path\r
2436 // of an UDF file system.\r
2437 //\r
2438 if (LastDevicePathNode != NULL &&\r
2439 DevicePathType (LastDevicePathNode) == MEDIA_DEVICE_PATH &&\r
2440 DevicePathSubType (LastDevicePathNode) == MEDIA_VENDOR_DP) {\r
2441 VendorDefinedGuid = (EFI_GUID *)((UINTN)LastDevicePathNode +\r
2442 OFFSET_OF (VENDOR_DEVICE_PATH, Guid));\r
2443 if (CompareGuid (VendorDefinedGuid, &UdfDevPathGuid)) {\r
2444 Status = EFI_SUCCESS;\r
2445 }\r
2446 }\r
2447\r
2448 //\r
2449 // Close Device Path protocol on ControllerHandle\r
2450 //\r
2451 gBS->CloseProtocol (\r
2452 ControllerHandle,\r
2453 &gEfiDevicePathProtocolGuid,\r
2454 This->DriverBindingHandle,\r
2455 ControllerHandle\r
2456 );\r
2457\r
2458 return Status;\r
2459}\r