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