]> git.proxmox.com Git - mirror_edk2.git/blame - FatPkg/EnhancedFatDxe/Open.c
Fix a typo when checking the 16-bit alignment of Unicode String.
[mirror_edk2.git] / FatPkg / EnhancedFatDxe / Open.c
CommitLineData
b9ec9330
QH
1/*++\r
2\r
3Copyright (c) 2005 - 2007, Intel Corporation\r
4All rights reserved. This program and the accompanying materials are licensed and made available\r
5under the terms and conditions of the BSD License which accompanies this\r
6distribution. The full text of the license may be found at\r
7http://opensource.org/licenses/bsd-license.php\r
8\r
9THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
10WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
11\r
12\r
13Module Name:\r
14\r
15 open.c\r
16\r
17Abstract:\r
18\r
19 Routines dealing with file open\r
20\r
21Revision History\r
22\r
23--*/\r
24\r
25#include "Fat.h"\r
26\r
27EFI_STATUS\r
28FatAllocateIFile (\r
29 IN FAT_OFILE *OFile,\r
30 OUT FAT_IFILE **PtrIFile\r
31 )\r
32/*++\r
33\r
34Routine Description:\r
35\r
36 Create an Open instance for the existing OFile.\r
37 The IFile of the newly opened file is passed out.\r
38\r
39Arguments:\r
40\r
41 OFile - The file that serves as a starting reference point.\r
42 PtrIFile - The newly generated IFile instance.\r
43\r
44Returns:\r
45\r
46 EFI_OUT_OF_RESOURCES - Can not allocate the memory for the IFile\r
47 EFI_SUCCESS - Create the new IFile for the OFile successfully\r
48\r
49--*/\r
50{\r
51 FAT_IFILE *IFile;\r
52\r
53 ASSERT_VOLUME_LOCKED (OFile->Volume);\r
54\r
55 //\r
56 // Allocate a new open instance\r
57 //\r
58 IFile = AllocateZeroPool (sizeof (FAT_IFILE));\r
59 if (IFile == NULL) {\r
60 return EFI_OUT_OF_RESOURCES;\r
61 }\r
62\r
63 IFile->Signature = FAT_IFILE_SIGNATURE;\r
64\r
65 CopyMem (&(IFile->Handle), &FatFileInterface, sizeof (EFI_FILE));\r
66\r
67 IFile->OFile = OFile;\r
68 InsertTailList (&OFile->Opens, &IFile->Link);\r
69\r
70 *PtrIFile = IFile;\r
71 return EFI_SUCCESS;\r
72}\r
73\r
74EFI_STATUS\r
75FatOFileOpen (\r
76 IN FAT_OFILE *OFile,\r
77 OUT FAT_IFILE **NewIFile,\r
78 IN CHAR16 *FileName,\r
79 IN UINT64 OpenMode,\r
80 IN UINT8 Attributes\r
81 )\r
82/*++\r
83\r
84Routine Description:\r
85\r
86 Open a file for a file name relative to an existing OFile.\r
87 The IFile of the newly opened file is passed out.\r
88\r
89Arguments:\r
90\r
91 OFile - The file that serves as a starting reference point.\r
92 NewIFile - The newly generated IFile instance.\r
93 FileName - The file name relative to the OFile.\r
94 OpenMode - Open mode.\r
95 Attributes - Attributes to set if the file is created.\r
96\r
97Returns:\r
98\r
99 EFI_SUCCESS - Open the file successfully.\r
100 EFI_INVALID_PARAMETER - The open mode is conflict with the attributes\r
101 or the file name is not valid.\r
102 EFI_NOT_FOUND - Conficts between dir intention and attribute.\r
103 EFI_WRITE_PROTECTED - Can't open for write if the volume is read only.\r
104 EFI_ACCESS_DENIED - If the file's attribute is read only, and the\r
105 open is for read-write fail it.\r
106 EFI_OUT_OF_RESOURCES - Can not allocate the memory.\r
107\r
108--*/\r
109{\r
110 FAT_VOLUME *Volume;\r
111 EFI_STATUS Status;\r
112 CHAR16 NewFileName[EFI_PATH_STRING_LENGTH];\r
113 FAT_DIRENT *DirEnt;\r
114 UINT8 FileAttributes;\r
115 BOOLEAN WriteMode;\r
116\r
117 Volume = OFile->Volume;\r
118 ASSERT_VOLUME_LOCKED (Volume);\r
119 WriteMode = (BOOLEAN) (OpenMode & EFI_FILE_MODE_WRITE);\r
120 if (Volume->ReadOnly && WriteMode) {\r
121 return EFI_WRITE_PROTECTED;\r
122 }\r
123 //\r
124 // Verify the source file handle isn't in an error state\r
125 //\r
126 Status = OFile->Error;\r
127 if (EFI_ERROR (Status)) {\r
128 return Status;\r
129 }\r
130 //\r
131 // Get new OFile for the file\r
132 //\r
133 Status = FatLocateOFile (&OFile, FileName, Attributes, NewFileName);\r
134 if (EFI_ERROR (Status)) {\r
135 return Status;\r
136 }\r
137\r
138 if (*NewFileName != 0) {\r
139 //\r
140 // If there's a remaining part of the name, then we had\r
141 // better be creating the file in the directory\r
142 //\r
143 if ((OpenMode & EFI_FILE_MODE_CREATE) == 0) {\r
144 return EFI_NOT_FOUND;\r
145 }\r
146\r
147 Status = FatCreateDirEnt (OFile, NewFileName, Attributes, &DirEnt);\r
148 if (EFI_ERROR (Status)) {\r
149 return Status;\r
150 }\r
151\r
152 Status = FatOpenDirEnt (OFile, DirEnt);\r
153 if (EFI_ERROR (Status)) {\r
154 return Status;\r
155 }\r
156\r
157 OFile = DirEnt->OFile;\r
158 if (OFile->ODir != NULL) {\r
159 //\r
160 // If we just created a directory, we need to create "." and ".."\r
161 //\r
162 Status = FatCreateDotDirEnts (OFile);\r
163 if (EFI_ERROR (Status)) {\r
164 return Status;\r
165 }\r
166 }\r
167 }\r
168 //\r
169 // If the file's attribute is read only, and the open is for\r
170 // read-write, then the access is denied.\r
171 //\r
172 FileAttributes = OFile->DirEnt->Entry.Attributes;\r
173 if ((FileAttributes & EFI_FILE_READ_ONLY) != 0 && (FileAttributes & FAT_ATTRIBUTE_DIRECTORY) == 0 && WriteMode) {\r
174 return EFI_ACCESS_DENIED;\r
175 }\r
176 //\r
177 // Create an open instance of the OFile\r
178 //\r
179 Status = FatAllocateIFile (OFile, NewIFile);\r
180 if (EFI_ERROR (Status)) {\r
181 return Status;\r
182 }\r
183\r
184 (*NewIFile)->ReadOnly = (BOOLEAN)!WriteMode;\r
185\r
186 DEBUG ((EFI_D_INFO, "FSOpen: Open '%S' %r\n", FileName, Status));\r
187 return FatOFileFlush (OFile);\r
188}\r
189\r
190EFI_STATUS\r
191EFIAPI\r
192FatOpen (\r
193 IN EFI_FILE *FHand,\r
194 OUT EFI_FILE **NewHandle,\r
195 IN CHAR16 *FileName,\r
196 IN UINT64 OpenMode,\r
197 IN UINT64 Attributes\r
198 )\r
199/*++\r
200Routine Description:\r
201\r
202 Implements Open() of Simple File System Protocol.\r
203\r
204Arguments:\r
205\r
206 FHand - File handle of the file serves as a starting reference point.\r
207 NewHandle - Handle of the file that is newly opened.\r
208 FileName - File name relative to FHand.\r
209 OpenMode - Open mode.\r
210 Attributes - Attributes to set if the file is created.\r
211\r
212Returns:\r
213\r
214 EFI_INVALID_PARAMETER - The FileName is NULL or the file string is empty.\r
215 The OpenMode is not supported.\r
216 The Attributes is not the valid attributes.\r
217 EFI_OUT_OF_RESOURCES - Can not allocate the memory for file string.\r
218 EFI_SUCCESS - Open the file successfully.\r
219 Others - The status of open file.\r
220\r
221--*/\r
222{\r
223 FAT_IFILE *IFile;\r
224 FAT_IFILE *NewIFile;\r
225 FAT_OFILE *OFile;\r
226 EFI_STATUS Status;\r
227\r
228 //\r
229 // Perform some parameter checking\r
230 //\r
231 if (FileName == NULL) {\r
232 return EFI_INVALID_PARAMETER;\r
233 }\r
234 //\r
235 // Check for a valid mode\r
236 //\r
237 switch (OpenMode) {\r
238 case EFI_FILE_MODE_READ:\r
239 case EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE:\r
240 case EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE:\r
241 break;\r
242\r
243 default:\r
244 return EFI_INVALID_PARAMETER;\r
245 }\r
246 //\r
247 // Check for valid attributes\r
248 //\r
249 if (Attributes & (~EFI_FILE_VALID_ATTR)) {\r
250 return EFI_INVALID_PARAMETER;\r
251 }\r
252 //\r
253 // Can't open for create and apply the read only attribute\r
254 //\r
255 if ((OpenMode & EFI_FILE_MODE_CREATE) && (Attributes & EFI_FILE_READ_ONLY)) {\r
256 return EFI_INVALID_PARAMETER;\r
257 }\r
258\r
259 IFile = IFILE_FROM_FHAND (FHand);\r
260 OFile = IFile->OFile;\r
261\r
262 //\r
263 // Lock\r
264 //\r
265 FatAcquireLock ();\r
266\r
267 //\r
268 // Open the file\r
269 //\r
270 Status = FatOFileOpen (OFile, &NewIFile, FileName, OpenMode, (UINT8) Attributes);\r
271\r
272 //\r
273 // If the file was opened, return the handle to the caller\r
274 //\r
275 if (!EFI_ERROR (Status)) {\r
276 *NewHandle = &NewIFile->Handle;\r
277 }\r
278 //\r
279 // Unlock\r
280 //\r
281 Status = FatCleanupVolume (OFile->Volume, NULL, Status);\r
282 FatReleaseLock ();\r
283\r
284 return Status;\r
285}\r