OpenCore  1.0.4
OpenCore Bootloader
Loading...
Searching...
No Matches
GetVolumeLabel.c
Go to the documentation of this file.
1
15#include <Uefi.h>
16
17#include <Guid/FileInfo.h>
18#include <Guid/FileSystemVolumeLabelInfo.h>
19
20#include <Protocol/SimpleFileSystem.h>
21
22#include <Library/BaseLib.h>
23#include <Library/BaseMemoryLib.h>
24#include <Library/BaseOverflowLib.h>
25#include <Library/DebugLib.h>
26#include <Library/DevicePathLib.h>
27#include <Library/MemoryAllocationLib.h>
29#include <Library/OcFileLib.h>
30#include <Library/OcStringLib.h>
31#include <Library/UefiBootServicesTableLib.h>
32#include <Library/UefiLib.h>
33
34CHAR16 *
36 IN EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *FileSystem
37 )
38{
39 EFI_STATUS Status;
40
41 EFI_FILE_HANDLE Volume;
42 EFI_FILE_SYSTEM_VOLUME_LABEL *VolumeInfo;
43 UINTN VolumeLabelSize;
44
45 ASSERT (FileSystem != NULL);
46
47 Volume = NULL;
48 Status = FileSystem->OpenVolume (
49 FileSystem,
50 &Volume
51 );
52
53 if (EFI_ERROR (Status)) {
54 return NULL;
55 }
56
57 VolumeInfo = OcGetFileInfo (
58 Volume,
60 sizeof (EFI_FILE_SYSTEM_VOLUME_LABEL),
61 &VolumeLabelSize
62 );
63
64 Volume->Close (Volume);
65
67 OFFSET_OF (EFI_FILE_SYSTEM_VOLUME_LABEL, VolumeLabel) == 0,
68 "Expected EFI_FILE_SYSTEM_VOLUME_LABEL to represent CHAR16 string!"
69 );
70
71 if (VolumeInfo != NULL) {
72 if ((VolumeLabelSize >= sizeof (CHAR16)) && (VolumeInfo->VolumeLabel[0] != L'\0')) {
73 //
74 // The spec requires disk label to be NULL-terminated, but it
75 // was unclear whether the size should contain terminator or not.
76 // Some old HFS Plus drivers provide volume label size without
77 // terminating \0 (though they do append it). These drivers must
78 // not be used, but we try not to die when debugging is off.
79 //
80 if ( (VolumeLabelSize > OC_MAX_VOLUME_LABEL_SIZE * sizeof (CHAR16))
81 || (VolumeInfo->VolumeLabel[VolumeLabelSize / sizeof (CHAR16) - 1] != '\0'))
82 {
83 DEBUG ((DEBUG_INFO, "OCFS: Found unterminated or too long volume label!"));
84 FreePool (VolumeInfo);
85 return AllocateCopyPool (sizeof (L"INVALID"), L"INVALID");
86 } else {
87 UnicodeFilterString (VolumeInfo->VolumeLabel, TRUE);
88 return VolumeInfo->VolumeLabel;
89 }
90 }
91
92 FreePool (VolumeInfo);
93 }
94
95 return AllocateCopyPool (sizeof (L"NO NAME"), L"NO NAME");
96}
CHAR16 * OcGetVolumeLabel(IN EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *FileSystem)
STATIC_ASSERT(BYTES_PER_PIXEL==sizeof(UINT32), "Non 4-byte pixels are unsupported!")
VOID * OcGetFileInfo(IN EFI_FILE_PROTOCOL *File, IN EFI_GUID *InformationType, IN UINTN MinFileInfoSize, OUT UINTN *RealFileInfoSize OPTIONAL)
Definition GetFileInfo.c:33
#define OC_MAX_VOLUME_LABEL_SIZE
Definition OcFileLib.h:32
VOID UnicodeFilterString(IN OUT CHAR16 *String, IN BOOLEAN SingleLine)
EFI_GUID gEfiFileSystemVolumeLabelInfoIdGuid
#define ASSERT(x)
Definition coder.h:55