forked from acidanthera/OpenCorePkg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOpenFile.c
238 lines (211 loc) · 6.91 KB
/
OpenFile.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/** @file
The UEFI Library provides functions and macros that simplify the development of
UEFI Drivers and UEFI Applications. These functions and macros help manage EFI
events, build simple locks utilizing EFI Task Priority Levels (TPLs), install
EFI Driver Model related protocols, manage Unicode string tables for UEFI Drivers,
and print messages on the console output and standard error devices.
Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution. The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php.
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
**/
#include <Uefi.h>
#include <Library/BaseLib.h>
#include <Library/DebugLib.h>
#include <Library/DevicePathLib.h>
#include <Library/OcFileLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Protocol/DevicePath.h>
#include <Protocol/SimpleFileSystem.h>
EFI_STATUS
OcSafeFileOpen (
IN CONST EFI_FILE_PROTOCOL *Directory,
OUT EFI_FILE_PROTOCOL **NewHandle,
IN CONST CHAR16 *FileName,
IN CONST UINT64 OpenMode,
IN CONST UINT64 Attributes
)
{
EFI_STATUS Status;
UINTN Length;
DEBUG_CODE_BEGIN ();
ASSERT (FileName != NULL);
ASSERT (NewHandle != NULL);
Length = StrLen (FileName);
if ((Length > 0) && (FileName[Length - 1] == L'\\')) {
DEBUG ((DEBUG_INFO, "OCFS: Filename %s has trailing slash\n", FileName));
}
DEBUG_CODE_END ();
*NewHandle = NULL;
Status = Directory->Open (
(EFI_FILE_PROTOCOL *)Directory,
NewHandle,
(CHAR16 *)FileName,
OpenMode,
Attributes
);
//
// Some boards like ASUS ROG RAMPAGE VI EXTREME may have malfunctioning FS
// drivers that report write protection violation errors for read-only
// operations but otherwise function as expected.
//
// REF: https://github.com/acidanthera/bugtracker/issues/1242
//
if ( (Status == EFI_WRITE_PROTECTED)
&& (OpenMode == EFI_FILE_MODE_READ)
&& (Attributes == 0)
&& (*NewHandle != NULL))
{
DEBUG ((
DEBUG_VERBOSE,
"OCFS: Avoid invalid WP error for Filename %s\n",
FileName
));
Status = EFI_SUCCESS;
}
return Status;
}
EFI_STATUS
EFIAPI
OcOpenFileByRemainingDevicePath (
IN EFI_HANDLE FileSystemHandle,
IN CONST EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath,
OUT EFI_FILE_PROTOCOL **File,
IN UINT64 OpenMode,
IN UINT64 Attributes
)
{
EFI_STATUS Status;
EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *FileSystem;
EFI_FILE_PROTOCOL *LastFile;
CONST EFI_DEVICE_PATH_PROTOCOL *FilePathNode;
CHAR16 *AlignedPathName;
CHAR16 *PathName;
UINTN PathLength;
EFI_FILE_PROTOCOL *NextFile;
ASSERT (FileSystemHandle != NULL);
ASSERT (RemainingDevicePath != NULL);
ASSERT (File != NULL);
Status = gBS->OpenProtocol (
FileSystemHandle,
&gEfiSimpleFileSystemProtocolGuid,
(VOID **)&FileSystem,
gImageHandle,
NULL,
EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
if (EFI_ERROR (Status)) {
return Status;
}
//
// Open the root directory of the filesystem. After this operation succeeds,
// we have to release LastFile on error.
//
Status = FileSystem->OpenVolume (FileSystem, &LastFile);
if (EFI_ERROR (Status)) {
return Status;
}
//
// Traverse the device path nodes relative to the filesystem.
//
FilePathNode = RemainingDevicePath;
while (!IsDevicePathEnd (FilePathNode)) {
if ((DevicePathType (FilePathNode) != MEDIA_DEVICE_PATH) ||
(DevicePathSubType (FilePathNode) != MEDIA_FILEPATH_DP))
{
Status = EFI_INVALID_PARAMETER;
goto CloseLastFile;
}
//
// FilePathNode->PathName may be unaligned, and the UEFI specification
// requires pointers that are passed to protocol member functions to be
// aligned. Create an aligned copy of the pathname to match that
// and to apply the hack below.
//
AlignedPathName = AllocateCopyPool (
(DevicePathNodeLength (FilePathNode) -
SIZE_OF_FILEPATH_DEVICE_PATH),
((CONST FILEPATH_DEVICE_PATH *)FilePathNode)->PathName
);
if (AlignedPathName == NULL) {
Status = EFI_OUT_OF_RESOURCES;
goto CloseLastFile;
}
//
// This is a compatibility hack for firmware types that do not support
// opening filepaths (directories) with a trailing slash.
// More details in a852f85986c1fe23fc3a429605e3c560ea800c54 OpenCorePkg commit.
//
PathLength = StrLen (AlignedPathName);
if ((PathLength > 0) && (AlignedPathName[PathLength - 1] == '\\')) {
AlignedPathName[PathLength - 1] = '\0';
}
PathName = AlignedPathName;
//
// Open or create the file corresponding to the next pathname fragment.
//
Status = OcSafeFileOpen (
LastFile,
&NextFile,
PathName,
OpenMode,
Attributes
);
FreePool (AlignedPathName);
if (EFI_ERROR (Status)) {
goto CloseLastFile;
}
//
// Advance to the next device path node.
//
LastFile->Close (LastFile);
LastFile = NextFile;
FilePathNode = NextDevicePathNode (FilePathNode);
}
*File = LastFile;
return EFI_SUCCESS;
CloseLastFile:
LastFile->Close (LastFile);
//
// We are on the error path; we must have set an error Status for returning
// to the caller.
//
ASSERT (EFI_ERROR (Status));
return Status;
}
EFI_STATUS
EFIAPI
OcOpenFileByDevicePath (
IN OUT EFI_DEVICE_PATH_PROTOCOL **FilePath,
OUT EFI_FILE_PROTOCOL **File,
IN UINT64 OpenMode,
IN UINT64 Attributes
)
{
EFI_STATUS Status;
EFI_HANDLE FileSystemHandle;
ASSERT (File != NULL);
ASSERT (FilePath != NULL);
//
// Look up the filesystem.
//
Status = gBS->LocateDevicePath (
&gEfiSimpleFileSystemProtocolGuid,
FilePath,
&FileSystemHandle
);
if (EFI_ERROR (Status)) {
return Status;
}
return OcOpenFileByRemainingDevicePath (
FileSystemHandle,
*FilePath,
File,
OpenMode,
Attributes
);
}