OpenCore  1.0.4
OpenCore Bootloader
Loading...
Searching...
No Matches
Info.c
Go to the documentation of this file.
1
9#include "NTFS.h"
10#include "Helper.h"
11
12STATIC
13EFI_STATUS
15 IN CONST EFI_FS *FileSystem,
16 IN OUT CHAR16 **Label
17 )
18{
19 EFI_STATUS Status;
20 NTFS_FILE *BaseMftRecord;
21 ATTR_HEADER_RES *Res;
22
23 ASSERT (FileSystem != NULL);
24 ASSERT (Label != NULL);
25
26 *Label = NULL;
27 BaseMftRecord = NULL;
28
29 Status = FsHelpFindFile (L"/$Volume", FileSystem->RootIndex, &BaseMftRecord, FSHELP_REG);
30 if (EFI_ERROR (Status)) {
31 DEBUG ((DEBUG_INFO, "NTFS: Could not find $Volume file.\n"));
32 return Status;
33 }
34
35 if (!BaseMftRecord->InodeRead) {
36 BaseMftRecord->FileRecord = AllocateZeroPool (FileSystem->FileRecordSize);
37 if (BaseMftRecord->FileRecord == NULL) {
38 DEBUG ((DEBUG_INFO, "NTFS: Failed to allocate buffer for FileRecord.\n"));
39 FreeFile (BaseMftRecord);
40 FreePool (BaseMftRecord);
41 return EFI_OUT_OF_RESOURCES;
42 }
43
44 Status = ReadMftRecord (BaseMftRecord->File, BaseMftRecord->FileRecord, BaseMftRecord->Inode);
45 if (EFI_ERROR (Status)) {
46 DEBUG ((DEBUG_INFO, "NTFS: Could not read Mft Record #%d.\n", BaseMftRecord->Inode));
47 FreeFile (BaseMftRecord);
48 FreePool (BaseMftRecord);
49 return Status;
50 }
51 }
52
53 Status = InitAttr (&BaseMftRecord->Attr, BaseMftRecord);
54 if (EFI_ERROR (Status)) {
55 DEBUG ((DEBUG_INFO, "NTFS: Could not initialize attribute.\n"));
56 FreeFile (BaseMftRecord);
57 FreePool (BaseMftRecord);
58 return Status;
59 }
60
61 Res = (ATTR_HEADER_RES *)FindAttr (&BaseMftRecord->Attr, AT_VOLUME_NAME);
62 if ((Res != NULL) && (Res->NonResFlag == 0) && (Res->InfoLength != 0)) {
63 //
64 // The Volume Name is not terminated with a Unicode NULL.
65 // Its name's length is the size of the attribute as stored in the header.
66 //
67 if (Res->InfoLength > (MAX_FILE_SIZE - sizeof (CHAR16))) {
68 DEBUG ((DEBUG_INFO, "NTFS: Volume Name is too huge.\n"));
69 FreeFile (BaseMftRecord);
70 FreePool (BaseMftRecord);
71 return EFI_VOLUME_CORRUPTED;
72 }
73
74 *Label = AllocateZeroPool (Res->InfoLength + sizeof (CHAR16));
75 if (*Label == NULL) {
76 DEBUG ((DEBUG_INFO, "NTFS: Failed to allocate buffer for *Label\n"));
77 FreeFile (BaseMftRecord);
78 FreePool (BaseMftRecord);
79 return EFI_OUT_OF_RESOURCES;
80 }
81
82 CopyMem (*Label, (UINT8 *)Res + Res->InfoOffset, Res->InfoLength);
83 }
84
85 FreeFile (BaseMftRecord);
86 FreePool (BaseMftRecord);
87
88 return EFI_SUCCESS;
89}
90
102EFI_STATUS
103EFIAPI
105 IN EFI_FILE_PROTOCOL *This,
106 IN EFI_GUID *Type,
107 IN OUT UINTN *Len,
108 OUT VOID *Data
109 )
110{
111 EFI_STATUS Status;
112 EFI_NTFS_FILE *File;
113 EFI_FILE_INFO *Info;
114 EFI_FILE_SYSTEM_INFO *FSInfo;
115 EFI_FILE_SYSTEM_VOLUME_LABEL *VLInfo;
116 CHAR16 *Label;
117 UINTN Length;
118
119 ASSERT (This != NULL);
120 ASSERT (Type != NULL);
121 ASSERT (Len != NULL);
122 ASSERT ((Data != NULL) || (*Len == 0));
123
124 File = (EFI_NTFS_FILE *)This;
125 Length = 0;
126
127 if (CompareMem (Type, &gEfiFileInfoGuid, sizeof (*Type)) == 0) {
128 //
129 // Fill in file information
130 //
131 Info = (EFI_FILE_INFO *)Data;
132
133 if (File->BaseName != NULL) {
134 Length = sizeof (EFI_FILE_INFO) + StrSize (File->BaseName);
135 } else if (File->Path != NULL) {
136 Length = sizeof (EFI_FILE_INFO) + StrSize (File->Path);
137 } else {
138 return EFI_VOLUME_CORRUPTED;
139 }
140
141 if (*Len < Length) {
142 *Len = Length;
143 return EFI_BUFFER_TOO_SMALL;
144 }
145
146 ZeroMem (Data, sizeof (EFI_FILE_INFO));
147
148 Info->Size = (UINT64)Length;
149 Info->Attribute = EFI_FILE_READ_ONLY;
150 NtfsToEfiTime (&Info->CreateTime, File->RootFile.CreationTime);
151 NtfsToEfiTime (&Info->LastAccessTime, File->RootFile.ReadTime);
152 NtfsToEfiTime (&Info->ModificationTime, File->RootFile.AlteredTime);
153
154 if (File->IsDir) {
155 Info->Attribute |= EFI_FILE_DIRECTORY;
156 } else {
157 Info->FileSize = File->RootFile.DataAttributeSize;
158 Info->PhysicalSize = File->RootFile.DataAttributeSize;
159 }
160
161 if (File->BaseName != NULL) {
162 CopyMem (Info->FileName, File->BaseName, StrSize (File->BaseName));
163 } else {
164 CopyMem (Info->FileName, File->Path, StrSize (File->Path));
165 }
166
167 *Len = Length;
168
169 return EFI_SUCCESS;
170 }
171
172 if (CompareMem (Type, &gEfiFileSystemInfoGuid, sizeof (*Type)) == 0) {
173 //
174 // Get file system information
175 //
176 FSInfo = (EFI_FILE_SYSTEM_INFO *)Data;
177
180 return EFI_BUFFER_TOO_SMALL;
181 }
182
183 FSInfo->ReadOnly = TRUE;
184 FSInfo->BlockSize = File->FileSystem->BlockIo->Media->BlockSize;
185 if (FSInfo->BlockSize == 0) {
186 DEBUG ((DEBUG_INFO, "NTFS: Corrected Media BlockSize\n"));
187 FSInfo->BlockSize = 512;
188 }
189
190 FSInfo->VolumeSize = (File->FileSystem->BlockIo->Media->LastBlock + 1U) * FSInfo->BlockSize;
191 //
192 // The device is Read Only
193 //
194 FSInfo->FreeSpace = 0;
195
196 Length = *Len - sizeof (EFI_FILE_SYSTEM_INFO);
197 Status = FileGetInfo (
198 This,
200 &Length,
201 FSInfo->VolumeLabel
202 );
203
204 FSInfo->Size = (UINT64)Length + sizeof (EFI_FILE_SYSTEM_INFO);
205 *Len = (UINTN)FSInfo->Size;
206
207 return Status;
208 }
209
210 if (CompareMem (Type, &gEfiFileSystemVolumeLabelInfoIdGuid, sizeof (*Type)) == 0) {
211 //
212 // Get the volume label
213 //
214 VLInfo = (EFI_FILE_SYSTEM_VOLUME_LABEL *)Data;
215
216 Status = GetLabel (File->FileSystem, &Label);
217 if (EFI_ERROR (Status)) {
218 DEBUG ((DEBUG_INFO, "NTFS: Could not read disk label - %r\n", Status));
219 return EFI_DEVICE_ERROR;
220 }
221
222 if (Label == NULL) {
223 DEBUG ((DEBUG_INFO, "NTFS: Volume has no label\n"));
224 *Len = 0;
225 } else {
226 if (*Len < StrSize (Label)) {
227 *Len = StrSize (Label);
228 return EFI_BUFFER_TOO_SMALL;
229 }
230
231 *Len = StrSize (Label);
232 CopyMem (VLInfo->VolumeLabel, Label, *Len);
233 }
234
235 return EFI_SUCCESS;
236 }
237
238 return EFI_UNSUPPORTED;
239}
240
241EFI_STATUS
242EFIAPI
244 IN EFI_FILE_PROTOCOL *This,
245 IN EFI_GUID *InformationType,
246 IN UINTN BufferSize,
247 IN VOID *Buffer
248 )
249{
250 return EFI_WRITE_PROTECTED;
251}
UINT64 Length
EFI_STATUS EFIAPI ReadMftRecord(IN EFI_NTFS_FILE *File, OUT UINT8 *Buffer, IN UINT64 RecordNumber)
Definition Data.c:148
VOID FreeFile(IN NTFS_FILE *File)
Definition Disc.c:810
EFI_STATUS InitAttr(OUT NTFS_ATTR *Attr, IN NTFS_FILE *File)
Definition Disc.c:313
UINT8 * FindAttr(IN NTFS_ATTR *Attr, IN UINT32 Type)
Definition Disc.c:400
#define MINIMUM_FS_INFO_LENGTH
Definition Driver.h:18
#define MAX_FILE_SIZE
Definition Driver.h:20
@ AT_VOLUME_NAME
Definition Driver.h:119
VOID NtfsToEfiTime(EFI_TIME *EfiTime, UINT64 NtfsTime)
Definition Index.c:928
@ FSHELP_REG
Definition Helper.h:30
EFI_STATUS FsHelpFindFile(IN CONST CHAR16 *Path, IN NTFS_FILE *RootNode, OUT NTFS_FILE **FoundNode, IN FSHELP_FILETYPE Type)
Definition Index.c:471
EFI_STATUS EFIAPI FileSetInfo(IN EFI_FILE_PROTOCOL *This, IN EFI_GUID *InformationType, IN UINTN BufferSize, IN VOID *Buffer)
Definition Info.c:243
EFI_STATUS EFIAPI FileGetInfo(IN EFI_FILE_PROTOCOL *This, IN EFI_GUID *Type, IN OUT UINTN *Len, OUT VOID *Data)
Definition Info.c:104
STATIC EFI_STATUS GetLabel(IN CONST EFI_FS *FileSystem, IN OUT CHAR16 **Label)
Definition Info.c:14
OC_TYPING_BUFFER_ENTRY Buffer[OC_TYPING_BUFFER_SIZE]
Definition OcTypingLib.h:42
INTN EFIAPI CompareMem(IN CONST VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
VOID *EFIAPI CopyMem(OUT VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
VOID *EFIAPI ZeroMem(OUT VOID *Buffer, IN UINTN Length)
EFI_GUID gEfiFileInfoGuid
EFI_GUID gEfiFileSystemInfoGuid
EFI_GUID gEfiFileSystemVolumeLabelInfoIdGuid
#define ASSERT(x)
Definition coder.h:55
#define Len
Definition deflate.h:82
EFI_BLOCK_IO_PROTOCOL * BlockIo
Definition Driver.h:642
NTFS_FILE RootFile
Definition Driver.h:634
BOOLEAN IsDir
Definition Driver.h:628
CHAR16 * Path
Definition Driver.h:630
CHAR16 * BaseName
Definition Driver.h:631
EFI_FS * FileSystem
Definition Driver.h:636
NTFS_ATTR Attr
Definition Driver.h:622
UINT8 * FileRecord
Definition Driver.h:615
UINT64 ReadTime
Definition Driver.h:619
UINT64 CreationTime
Definition Driver.h:617
UINT64 AlteredTime
Definition Driver.h:618
UINT64 Inode
Definition Driver.h:620
EFI_NTFS_FILE * File
Definition Driver.h:623
BOOLEAN InodeRead
Definition Driver.h:621
UINT64 DataAttributeSize
Definition Driver.h:616
UINT16 InfoOffset
Definition Driver.h:293
UINT32 InfoLength
Definition Driver.h:292
UINT8 NonResFlag
Definition Driver.h:287