]> git.proxmox.com Git - mirror_edk2.git/blob - ArmPlatformPkg/FileSystem/BootMonFs/BootMonFsDir.c
bf91bf0e1a86f0ef3980ee49210e97dd06199a49
[mirror_edk2.git] / ArmPlatformPkg / FileSystem / BootMonFs / BootMonFsDir.c
1 /** @file
2 *
3 * Copyright (c) 2012-2014, ARM Limited. All rights reserved.
4 *
5 * This program and the accompanying materials
6 * are licensed and made available under the terms and conditions of the BSD License
7 * which accompanies this distribution. The full text of the license may be found at
8 * http://opensource.org/licenses/bsd-license.php
9 *
10 * THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12 *
13 **/
14
15 #include "BootMonFsInternal.h"
16
17 EFIAPI
18 EFI_STATUS
19 OpenBootMonFsOpenVolume (
20 IN EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *This,
21 OUT EFI_FILE_PROTOCOL **Root
22 )
23 {
24 BOOTMON_FS_INSTANCE *Instance;
25
26 Instance = BOOTMON_FS_FROM_FS_THIS (This);
27 if (Instance == NULL) {
28 return EFI_DEVICE_ERROR;
29 }
30
31 *Root = &Instance->RootFile->File;
32
33 return EFI_SUCCESS;
34 }
35
36 UINT32
37 BootMonFsGetImageLength (
38 IN BOOTMON_FS_FILE *File
39 )
40 {
41 UINT32 Index;
42 UINT32 FileSize;
43 LIST_ENTRY *RegionToFlushLink;
44 BOOTMON_FS_FILE_REGION *Region;
45
46 FileSize = 0;
47
48 // Look at all Flash areas to determine file size
49 for (Index = 0; Index < HW_IMAGE_DESCRIPTION_REGION_MAX; Index++) {
50 FileSize += File->HwDescription.Region[Index].Size;
51 }
52
53 // Add the regions that have not been flushed yet
54 for (RegionToFlushLink = GetFirstNode (&File->RegionToFlushLink);
55 !IsNull (&File->RegionToFlushLink, RegionToFlushLink);
56 RegionToFlushLink = GetNextNode (&File->RegionToFlushLink, RegionToFlushLink)
57 )
58 {
59 Region = (BOOTMON_FS_FILE_REGION*)RegionToFlushLink;
60 if (Region->Offset + Region->Size > FileSize) {
61 FileSize += Region->Offset + Region->Size;
62 }
63 }
64
65 return FileSize;
66 }
67
68 UINTN
69 BootMonFsGetPhysicalSize (
70 IN BOOTMON_FS_FILE* File
71 )
72 {
73 // Return 0 for files that haven't yet been flushed to media
74 if (File->HwDescription.RegionCount == 0) {
75 return 0;
76 }
77
78 return ((File->HwDescription.BlockEnd - File->HwDescription.BlockStart) + 1 )
79 * File->Instance->Media->BlockSize;
80 }
81
82 EFIAPI
83 EFI_STATUS
84 BootMonFsSetDirPosition (
85 IN EFI_FILE_PROTOCOL *This,
86 IN UINT64 Position
87 )
88 {
89 BOOTMON_FS_FILE *File;
90
91 File = BOOTMON_FS_FILE_FROM_FILE_THIS (This);
92 if (File == NULL) {
93 return EFI_INVALID_PARAMETER;
94 }
95
96 // UEFI Spec section 12.5:
97 // "The seek request for nonzero is not valid on open directories."
98 if (Position != 0) {
99 return EFI_UNSUPPORTED;
100 }
101 File->Position = Position;
102
103 return EFI_SUCCESS;
104 }
105
106 EFI_STATUS
107 BootMonFsOpenDirectory (
108 OUT EFI_FILE_PROTOCOL **NewHandle,
109 IN CHAR16 *FileName,
110 IN BOOTMON_FS_INSTANCE *Volume
111 )
112 {
113 ASSERT(0);
114
115 return EFI_UNSUPPORTED;
116 }
117 EFI_STATUS
118 GetFileSystemVolumeLabelInfo (
119 IN BOOTMON_FS_INSTANCE *Instance,
120 IN OUT UINTN *BufferSize,
121 OUT VOID *Buffer
122 )
123 {
124 UINTN Size;
125 EFI_FILE_SYSTEM_VOLUME_LABEL *Label;
126 EFI_STATUS Status;
127
128 Label = Buffer;
129
130 // Value returned by StrSize includes null terminator.
131 Size = SIZE_OF_EFI_FILE_SYSTEM_VOLUME_LABEL
132 + StrSize (Instance->FsInfo.VolumeLabel);
133
134 if (*BufferSize >= Size) {
135 CopyMem (&Label->VolumeLabel, &Instance->FsInfo.VolumeLabel, Size);
136 Status = EFI_SUCCESS;
137 } else {
138 Status = EFI_BUFFER_TOO_SMALL;
139 }
140 *BufferSize = Size;
141 return Status;
142 }
143
144 // Helper function that calculates a rough "free space" by:
145 // - Taking the media size
146 // - Subtracting the sum of all file sizes
147 // - Subtracting the block size times the number of files
148 // (To account for the blocks containing the HW_IMAGE_INFO
149 STATIC
150 UINT64
151 ComputeFreeSpace (
152 IN BOOTMON_FS_INSTANCE *Instance
153 )
154 {
155 LIST_ENTRY *FileLink;
156 UINT64 FileSizeSum;
157 UINT64 MediaSize;
158 UINTN NumFiles;
159 EFI_BLOCK_IO_MEDIA *Media;
160 BOOTMON_FS_FILE *File;
161
162 Media = Instance->BlockIo->Media;
163 MediaSize = Media->BlockSize * (Media->LastBlock + 1);
164
165 NumFiles = 0;
166 FileSizeSum = 0;
167 for (FileLink = GetFirstNode (&Instance->RootFile->Link);
168 !IsNull (&Instance->RootFile->Link, FileLink);
169 FileLink = GetNextNode (&Instance->RootFile->Link, FileLink)
170 )
171 {
172 File = BOOTMON_FS_FILE_FROM_LINK_THIS (FileLink);
173 FileSizeSum += BootMonFsGetImageLength (File);
174
175 NumFiles++;
176 }
177
178 return MediaSize - (FileSizeSum + (Media->BlockSize + NumFiles));
179 }
180
181 EFI_STATUS
182 GetFilesystemInfo (
183 IN BOOTMON_FS_INSTANCE *Instance,
184 IN OUT UINTN *BufferSize,
185 OUT VOID *Buffer
186 )
187 {
188 EFI_STATUS Status;
189
190 if (*BufferSize >= Instance->FsInfo.Size) {
191 Instance->FsInfo.FreeSpace = ComputeFreeSpace (Instance);
192 CopyMem (Buffer, &Instance->FsInfo, Instance->FsInfo.Size);
193 Status = EFI_SUCCESS;
194 } else {
195 Status = EFI_BUFFER_TOO_SMALL;
196 }
197
198 *BufferSize = Instance->FsInfo.Size;
199 return Status;
200 }
201
202 EFI_STATUS
203 GetFileInfo (
204 IN BOOTMON_FS_INSTANCE *Instance,
205 IN BOOTMON_FS_FILE *File,
206 IN OUT UINTN *BufferSize,
207 OUT VOID *Buffer
208 )
209 {
210 EFI_FILE_INFO *Info;
211 UINTN ResultSize;
212 UINTN NameSize;
213 UINTN Index;
214
215 if (File == Instance->RootFile) {
216 NameSize = 0;
217 ResultSize = SIZE_OF_EFI_FILE_INFO + sizeof (CHAR16);
218 } else {
219 NameSize = AsciiStrLen (File->HwDescription.Footer.Filename) + 1;
220 ResultSize = SIZE_OF_EFI_FILE_INFO + (NameSize * sizeof (CHAR16));
221 }
222
223 if (*BufferSize < ResultSize) {
224 *BufferSize = ResultSize;
225 return EFI_BUFFER_TOO_SMALL;
226 }
227
228 Info = Buffer;
229
230 // Zero out the structure
231 ZeroMem (Info, ResultSize);
232
233 // Fill in the structure
234 Info->Size = ResultSize;
235
236 if (File == Instance->RootFile) {
237 Info->Attribute = EFI_FILE_READ_ONLY | EFI_FILE_DIRECTORY;
238 Info->FileName[0] = L'\0';
239 } else {
240 Info->FileSize = BootMonFsGetImageLength (File);
241 Info->PhysicalSize = BootMonFsGetPhysicalSize (File);
242
243 for (Index = 0; Index < NameSize; Index++) {
244 Info->FileName[Index] = File->HwDescription.Footer.Filename[Index];
245 }
246 }
247
248 *BufferSize = ResultSize;
249
250 return EFI_SUCCESS;
251 }
252
253 STATIC
254 EFI_STATUS
255 SetFileName (
256 IN BOOTMON_FS_FILE *File,
257 IN CHAR16 *FileNameUnicode
258 )
259 {
260 CHAR8 *FileNameAscii;
261 UINT16 SavedChar;
262 UINTN FileNameSize;
263 BOOTMON_FS_FILE *SameFile;
264 EFI_STATUS Status;
265
266 // EFI Shell inserts '\' in front of the filename that must be stripped
267 if (FileNameUnicode[0] == L'\\') {
268 FileNameUnicode++;
269 }
270 //
271 // Convert Unicode into Ascii
272 //
273 SavedChar = L'\0';
274 FileNameSize = StrLen (FileNameUnicode) + 1;
275 FileNameAscii = AllocatePool (FileNameSize * sizeof (CHAR8));
276 if (FileNameAscii == NULL) {
277 return EFI_OUT_OF_RESOURCES;
278 }
279 // If Unicode string is too long then truncate it.
280 if (FileNameSize > MAX_NAME_LENGTH) {
281 SavedChar = FileNameUnicode[MAX_NAME_LENGTH - 1];
282 FileNameUnicode[MAX_NAME_LENGTH - 1] = L'\0';
283 }
284 UnicodeStrToAsciiStr (FileNameUnicode, FileNameAscii);
285 // If the unicode string was truncated then restore its original content.
286 if (SavedChar != L'\0') {
287 FileNameUnicode[MAX_NAME_LENGTH - 1] = SavedChar;
288 }
289
290 // If we're changing the file name
291 if (AsciiStrCmp (FileNameAscii, File->HwDescription.Footer.Filename) == 0) {
292 // No change to filename.
293 Status = EFI_SUCCESS;
294 } else if (!(File->OpenMode & EFI_FILE_MODE_WRITE)) {
295 // You can only change the filename if you open the file for write.
296 Status = EFI_ACCESS_DENIED;
297 } else if (BootMonGetFileFromAsciiFileName (
298 File->Instance,
299 File->HwDescription.Footer.Filename,
300 &SameFile) != EFI_NOT_FOUND) {
301 // A file with that name already exists.
302 Status = EFI_ACCESS_DENIED;
303 } else {
304 // OK, change the filename.
305 AsciiStrCpy (FileNameAscii, File->HwDescription.Footer.Filename);
306 Status = EFI_SUCCESS;
307 }
308
309 FreePool (FileNameAscii);
310 return Status;
311 }
312
313 // Set the file's size (NB "size", not "physical size"). If the change amounts
314 // to an increase, simply do a write followed by a flush.
315 // (This is a helper function for SetFileInfo.)
316 STATIC
317 EFI_STATUS
318 SetFileSize (
319 IN BOOTMON_FS_INSTANCE *Instance,
320 IN BOOTMON_FS_FILE *BootMonFsFile,
321 IN UINTN NewSize
322 )
323 {
324 UINT64 StoredPosition;
325 EFI_STATUS Status;
326 EFI_FILE_PROTOCOL *File;
327 CHAR8 Buffer;
328 UINTN BufferSize;
329 UINT32 OldSize;
330
331 OldSize = BootMonFsFile->HwDescription.Region[0].Size;
332
333 if (OldSize == NewSize) {
334 return EFI_SUCCESS;
335 }
336
337 Buffer = 0;
338 BufferSize = sizeof (Buffer);
339
340 File = &BootMonFsFile->File;
341
342 if (!(BootMonFsFile->OpenMode & EFI_FILE_MODE_WRITE)) {
343 return EFI_ACCESS_DENIED;
344 }
345
346 if (NewSize <= OldSize) {
347 OldSize = NewSize;
348 } else {
349 // Increasing a file's size is potentially complicated as it may require
350 // moving the image description on media. The simplest way to do it is to
351 // seek past the end of the file (which is valid in UEFI) and perform a
352 // Write.
353
354 // Save position
355 Status = File->GetPosition (File, &StoredPosition);
356 if (EFI_ERROR (Status)) {
357 return Status;
358 }
359
360 Status = File->SetPosition (File, NewSize - 1);
361 if (EFI_ERROR (Status)) {
362 return Status;
363 }
364 Status = File->Write (File, &BufferSize, &Buffer);
365 if (EFI_ERROR (Status)) {
366 return Status;
367 }
368
369 // Restore saved position
370 Status = File->SetPosition (File, NewSize - 1);
371 if (EFI_ERROR (Status)) {
372 return Status;
373 }
374
375 Status = File->Flush (File);
376 if (EFI_ERROR (Status)) {
377 return Status;
378 }
379 }
380 return EFI_SUCCESS;
381 }
382
383 EFI_STATUS
384 SetFileInfo (
385 IN BOOTMON_FS_INSTANCE *Instance,
386 IN BOOTMON_FS_FILE *File,
387 IN UINTN BufferSize,
388 IN EFI_FILE_INFO *Info
389 )
390 {
391 EFI_STATUS Status;
392 EFI_BLOCK_IO_PROTOCOL *BlockIo;
393 UINT8 *DataBuffer;
394 UINTN BlockSize;
395
396 Status = EFI_SUCCESS;
397 BlockIo = Instance->BlockIo;
398
399 // Note that a call to this function on a file opened read-only is only
400 // invalid if it actually changes fields, so we don't immediately fail if the
401 // OpenMode is wrong.
402 // Also note that the only fields supported are filename and size, others are
403 // ignored.
404
405 if (File != Instance->RootFile) {
406 if (!(File->OpenMode & EFI_FILE_MODE_WRITE)) {
407 return EFI_ACCESS_DENIED;
408 }
409
410 Status = SetFileName (File, Info->FileName);
411 if (EFI_ERROR (Status)) {
412 return Status;
413 }
414
415 // Update file size
416 Status = SetFileSize (Instance, File, Info->FileSize);
417 if (EFI_ERROR (Status)) {
418 return Status;
419 }
420
421 //
422 // Update the last block
423 //
424 BlockSize = BlockIo->Media->BlockSize;
425 DataBuffer = AllocatePool (BlockSize);
426 if (DataBuffer == NULL) {
427 return EFI_OUT_OF_RESOURCES;
428 }
429 Status = BlockIo->ReadBlocks (BlockIo, Instance->Media->MediaId,
430 File->HwDescription.BlockEnd, BlockSize, DataBuffer);
431 if (EFI_ERROR (Status)) {
432 FreePool (DataBuffer);
433 return Status;
434 }
435 CopyMem (DataBuffer + BlockSize - sizeof (File->HwDescription), &File->HwDescription, sizeof (File->HwDescription));
436 Status = BlockIo->WriteBlocks (BlockIo, Instance->Media->MediaId,
437 File->HwDescription.BlockEnd, BlockSize, DataBuffer);
438 FreePool (DataBuffer);
439 }
440 return Status;
441 }
442
443 EFIAPI
444 EFI_STATUS
445 BootMonFsGetInfo (
446 IN EFI_FILE_PROTOCOL *This,
447 IN EFI_GUID *InformationType,
448 IN OUT UINTN *BufferSize,
449 OUT VOID *Buffer
450 )
451 {
452 EFI_STATUS Status;
453 BOOTMON_FS_FILE *File;
454 BOOTMON_FS_INSTANCE *Instance;
455
456 File = BOOTMON_FS_FILE_FROM_FILE_THIS (This);
457 if (File == NULL) {
458 return EFI_DEVICE_ERROR;
459 }
460
461 Instance = File->Instance;
462
463 // If the instance has not been initialized yet then do it ...
464 if (!Instance->Initialized) {
465 Status = BootMonFsInitialize (Instance);
466 } else {
467 Status = EFI_SUCCESS;
468 }
469
470 if (!EFI_ERROR (Status)) {
471 if (CompareGuid (InformationType, &gEfiFileSystemVolumeLabelInfoIdGuid)
472 != 0) {
473 Status = GetFileSystemVolumeLabelInfo (Instance, BufferSize, Buffer);
474 } else if (CompareGuid (InformationType, &gEfiFileSystemInfoGuid) != 0) {
475 Status = GetFilesystemInfo (Instance, BufferSize, Buffer);
476 } else if (CompareGuid (InformationType, &gEfiFileInfoGuid) != 0) {
477 Status = GetFileInfo (Instance, File, BufferSize, Buffer);
478 } else {
479 Status = EFI_UNSUPPORTED;
480 }
481 }
482
483 return Status;
484 }
485
486 EFIAPI
487 EFI_STATUS
488 BootMonFsSetInfo (
489 IN EFI_FILE_PROTOCOL *This,
490 IN EFI_GUID *InformationType,
491 IN UINTN BufferSize,
492 IN VOID *Buffer
493 )
494 {
495 EFI_STATUS Status;
496 BOOTMON_FS_FILE *File;
497 BOOTMON_FS_INSTANCE *Instance;
498
499 File = BOOTMON_FS_FILE_FROM_FILE_THIS (This);
500 if (File == NULL) {
501 return EFI_DEVICE_ERROR;
502 }
503
504 Instance = File->Instance;
505
506 if (CompareGuid (InformationType, &gEfiFileInfoGuid) != 0) {
507 Status = SetFileInfo (Instance, File, BufferSize, (EFI_FILE_INFO *) Buffer);
508 } else {
509 // The only writable field in the other two information types
510 // (i.e. EFI_FILE_SYSTEM_INFO and EFI_FILE_SYSTEM_VOLUME_LABEL) is the
511 // filesystem volume label. This can be retrieved with GetInfo, but it is
512 // hard-coded into this driver, not stored on media.
513 Status = EFI_UNSUPPORTED;
514 }
515
516 return Status;
517 }
518
519 EFIAPI
520 EFI_STATUS
521 BootMonFsReadDirectory (
522 IN EFI_FILE_PROTOCOL *This,
523 IN OUT UINTN *BufferSize,
524 OUT VOID *Buffer
525 )
526 {
527 BOOTMON_FS_INSTANCE *Instance;
528 BOOTMON_FS_FILE *RootFile;
529 BOOTMON_FS_FILE *File;
530 EFI_FILE_INFO *Info;
531 UINTN NameSize;
532 UINTN ResultSize;
533 EFI_STATUS Status;
534 UINTN Index;
535
536 RootFile = BOOTMON_FS_FILE_FROM_FILE_THIS (This);
537 if (RootFile == NULL) {
538 return EFI_INVALID_PARAMETER;
539 }
540
541 Instance = RootFile->Instance;
542 Status = BootMonGetFileFromPosition (Instance, RootFile->Position, &File);
543 if (EFI_ERROR (Status)) {
544 // No more file
545 *BufferSize = 0;
546 return EFI_SUCCESS;
547 }
548
549 NameSize = AsciiStrLen (File->HwDescription.Footer.Filename) + 1;
550 ResultSize = SIZE_OF_EFI_FILE_INFO + (NameSize * sizeof (CHAR16));
551 if (*BufferSize < ResultSize) {
552 *BufferSize = ResultSize;
553 return EFI_BUFFER_TOO_SMALL;
554 }
555
556 // Zero out the structure
557 Info = Buffer;
558 ZeroMem (Info, ResultSize);
559
560 // Fill in the structure
561 Info->Size = ResultSize;
562 Info->FileSize = BootMonFsGetImageLength (File);
563 Info->PhysicalSize = BootMonFsGetPhysicalSize (File);
564 for (Index = 0; Index < NameSize; Index++) {
565 Info->FileName[Index] = File->HwDescription.Footer.Filename[Index];
566 }
567
568 *BufferSize = ResultSize;
569 RootFile->Position++;
570
571 return EFI_SUCCESS;
572 }
573
574 EFIAPI
575 EFI_STATUS
576 BootMonFsFlushDirectory (
577 IN EFI_FILE_PROTOCOL *This
578 )
579 {
580 BOOTMON_FS_FILE *RootFile;
581 LIST_ENTRY *ListFiles;
582 LIST_ENTRY *Link;
583 BOOTMON_FS_FILE *File;
584
585 RootFile = BOOTMON_FS_FILE_FROM_FILE_THIS (This);
586 if (RootFile == NULL) {
587 return EFI_INVALID_PARAMETER;
588 }
589
590 ListFiles = &RootFile->Link;
591
592 if (IsListEmpty (ListFiles)) {
593 return EFI_SUCCESS;
594 }
595
596 //
597 // Flush all the files that need to be flushed
598 //
599
600 // Go through all the list of files to flush them
601 for (Link = GetFirstNode (ListFiles);
602 !IsNull (ListFiles, Link);
603 Link = GetNextNode (ListFiles, Link)
604 )
605 {
606 File = BOOTMON_FS_FILE_FROM_LINK_THIS (Link);
607 File->File.Flush (&File->File);
608 }
609
610 return EFI_SUCCESS;
611 }