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