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