]> git.proxmox.com Git - mirror_edk2.git/blame - StdLib/LibC/Uefi/Devices/Utility/Path.c
Add device abstraction code for the UEFI Console and UEFI Shell-based file systems.
[mirror_edk2.git] / StdLib / LibC / Uefi / Devices / Utility / Path.c
CommitLineData
53e1e5c6 1/** @file\r
2 Device Abstraction: Path manipulation utilities.\r
3\r
4 Copyright (c) 2011, Intel Corporation. All rights reserved.<BR>\r
5 This program and the accompanying materials are licensed and made available under\r
6 the terms and conditions of the BSD License that accompanies this distribution.\r
7 The full text of the license may be found at\r
8 http://opensource.org/licenses/bsd-license.php.\r
9\r
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12**/\r
13#include <Library/BaseLib.h>\r
14\r
15#include <LibConfig.h>\r
16\r
17#include <errno.h>\r
18#include <stdlib.h>\r
19#include <wchar.h>\r
20#include <wctype.h>\r
21#include <kfile.h>\r
22#include <Device/Device.h>\r
23#include <MainData.h>\r
24\r
25/** Identify the type of path pointed to by Path.\r
26\r
27 Paths are classified based upon their initial character sequences.\r
28 ^\\ Absolute Path\r
29 ^\. Relative Path\r
30 ^[^:\\]: Mapping Path\r
31 .* Relative Path\r
32\r
33 Mapping paths are broken into two parts at the ':'. The part to the left of the ':'\r
34 is the Map Name, pointed to by Path, and the part to the right of the ':' is pointed\r
35 to by NewPath.\r
36\r
37 If Path was not a Mapping Path, then NewPath is set to Path.\r
38\r
39 @param[in] Path Pointer to the path to be classified.\r
40 @param[out] NewPath Pointer to the path portion of a mapping path.\r
41 @param[out] Length Length of the Map Name portion of the path.\r
42\r
43 @retval PathAbsolute Path is an absolute path. NewPath points to the first '\'.\r
44 @retval PathRelative Path is a relative path. NewPath = Path.\r
45 @retval PathMapping Path is a mapping path. NewPath points to the character following ':'.\r
46 @retval PathError Path is NULL.\r
47**/\r
48PATH_CLASS\r
49EFIAPI\r
50ClassifyPath(\r
51 IN wchar_t * Path,\r
52 OUT wchar_t ** NewPath,\r
53 OUT int * const Length\r
54 )\r
55{\r
56 size_t MapLen;\r
57\r
58 if(Path == NULL) {\r
59 return PathError; // Bad parameter\r
60 }\r
61 if(NewPath != NULL) {\r
62 *NewPath = Path; // Setup default condition\r
63 }\r
64 if((*Path == L'\\') || (*Path == L'\0')) {\r
65 return PathAbsolute;\r
66 }\r
67 if(*Path == L'.') {\r
68 return PathRelative;\r
69 }\r
70 /* The easy stuff has been done, now see if this is a mapping path.\r
71 See if there is a ':' in Path that isn't the first character and is before\r
72 any '\\' characters.\r
73 */\r
74 MapLen = wcscspn(Path, L"\\:");\r
75 if(Length != NULL) {\r
76 *Length = (int)MapLen;\r
77 }\r
78 /* MapLen == 0 means that the first character is a ':'\r
79 == PathLen means that there are no '\\' or ':'\r
80 Otherwise, Path[MapLen] == ':' for a mapping path\r
81 or '\\' for a relative path.\r
82 */\r
83 if(MapLen == 0) {\r
84 return PathError;\r
85 }\r
86 if(Path[MapLen] == L':') {\r
87 if(NewPath != NULL) {\r
88 *NewPath = &Path[MapLen + 1]; // Point to character after then ':'. Might be '\0'.\r
89 }\r
90 return PathMapping;\r
91 }\r
92 return PathRelative;\r
93}\r
94\r
95/* Normalize a narrow-character path and produce a wide-character path\r
96 that has forward slashes replaced with backslashes.\r
97 Backslashes are directory separators in UEFI File Paths.\r
98\r
99 It is the caller's responsibility to eventually free() the returned buffer.\r
100\r
101 @param[in] path A pointer to the narrow-character path to be normalized.\r
102\r
103 @return A pointer to a buffer containing the normalized, wide-character, path.\r
104*/\r
105wchar_t *\r
106NormalizePath( const char *path)\r
107{\r
108 wchar_t *temp;\r
109 wchar_t *OldPath;\r
110 wchar_t *NewPath;\r
111 size_t Length;\r
112\r
113 OldPath = AsciiStrToUnicodeStr(path, gMD->UString);\r
114 Length = wcslen(OldPath) + 1;\r
115\r
116 NewPath = calloc(Length, sizeof(wchar_t));\r
117 if(NewPath != NULL) {\r
118 temp = NewPath;\r
119 for( ; *OldPath; ++OldPath) {\r
120 if(*OldPath == L'/') {\r
121 *temp = L'\\';\r
122 }\r
123 else {\r
124 *temp = *OldPath;\r
125 }\r
126 ++temp;\r
127 }\r
128 }\r
129 else {\r
130 errno = ENOMEM;\r
131 EFIerrno = RETURN_OUT_OF_RESOURCES;\r
132 }\r
133 return NewPath;\r
134}\r
135\r
136/** Process a wide character string representing a Mapping Path and extract the instance number.\r
137\r
138 The instance number is the sequence of decimal digits immediately to the left\r
139 of the ":" in the Map Name portion of a Mapping Path.\r
140\r
141 This function is called with a pointer to beginning of the Map Name.\r
142 Thus Path[Length] must be a ':' and Path[Length - 1] must be a decimal digit.\r
143 If either of these are not true, an instance value of 0 is returned.\r
144\r
145 If Path is NULL, an instance value of 0 is returned.\r
146\r
147 @param[in] Path Points to the beginning of a Mapping Path\r
148 @param[in] Length Number of valid characters to the left of the ':'\r
149\r
150 @return Returns either 0 or the value of the contiguous digits to the left of the ':'.\r
151**/\r
152int\r
153EFIAPI\r
154PathInstance(\r
155 const wchar_t *Path,\r
156 int Length\r
157 )\r
158{\r
159 wchar_t *temp;\r
160 int instance = 0;\r
161\r
162 if((Path != NULL) && (Path[Length] == L':') && (Length > 0)) {\r
163 for(temp = __UNCONST(&Path[Length - 1]); Length > 0; --Length) {\r
164 if(!iswdigit(*temp)) {\r
165 break;\r
166 }\r
167 --temp;\r
168 }\r
169 instance = (int)wcstol(temp+1, NULL, 10);\r
170 }\r
171 return instance;\r
172}\r
173\r
174/** Transform a relative path into an absolute path.\r
175\r
176 If Path is NULL, return NULL.\r
177 Otherwise, pre-pend the CWD to Path then process the resulting path to:\r
178 - Replace "/./" with "/"\r
179 - Replace "/<dirname>/../" with "/"\r
180 - Do not allow one to back up past the root, "/"\r
181\r
182 Also sets the Current Working Device to the Root Device.\r
183\r
184 Path must be a previously allocated buffer. PathAdjust will\r
185 allocate a new buffer to hold the results of the transformation\r
186 and free Path. A pointer to the newly allocated buffer is returned.\r
187\r
188 @param[in] Path A pointer to the path to be transformed. This buffer\r
189 will always be freed.\r
190\r
191 @return A pointer to a buffer containing the transformed path.\r
192**/\r
193wchar_t *\r
194EFIAPI\r
195PathAdjust(\r
196 wchar_t *Path\r
197 )\r
198{\r
199 wchar_t *NewPath;\r
200\r
201 NewPath = calloc(PATH_MAX, sizeof(wchar_t));\r
202 if(NewPath != NULL) {\r
203 wmemcpy(NewPath, Path, PATH_MAX);\r
204 }\r
205 else {\r
206 errno = ENOMEM;\r
207 }\r
208 free(Path);\r
209 return NewPath;\r
210}\r
211\r
212/** Replace the leading portion of Path with any aliases.\r
213\r
214 Aliases are read from /etc/fstab. If there is an associated device, the\r
215 Current Working Device is set to that device.\r
216\r
217 Path must be a previously allocated buffer. PathAlias will\r
218 allocate a new buffer to hold the results of the transformation\r
219 then free Path. A pointer to the newly allocated buffer is returned.\r
220\r
221 @param[in] Path A pointer to the original, unaliased, path. This\r
222 buffer is always freed.\r
223 @param[out] Node Filled in with a pointer to the Device Node describing\r
224 the device abstraction associated with this path.\r
225\r
226 @return A pointer to a buffer containing the aliased path.\r
227**/\r
228wchar_t *\r
229EFIAPI\r
230PathAlias(\r
231 wchar_t *Path,\r
232 DeviceNode **Node\r
233 )\r
234{\r
235 wchar_t *NewPath;\r
236\r
237 NewPath = calloc(PATH_MAX, sizeof(wchar_t));\r
238 if(NewPath != NULL) {\r
239 wmemcpy(NewPath, Path, PATH_MAX);\r
240 }\r
241 else {\r
242 errno = ENOMEM;\r
243 }\r
244 free(Path);\r
245 *Node = NULL;\r
246 return NewPath;\r
247}\r
248\r
249/** Parse a path producing the target device, device instance, and file path.\r
250\r
251 @param[in] path\r
252 @param[out] FullPath\r
253 @param[out] DevNode\r
254 @param[out] Which\r
255\r
256 @retval RETURN_SUCCESS The path was parsed successfully.\r
257 @retval RETURN_NOT_FOUND The path does not map to a valid device.\r
258 @retval RETURN_OUT_OF_RESOURCES Insufficient memory to calloc a MapName buffer.\r
259 The errno variable is set to ENOMEM.\r
260 @retval RETURN_INVALID_PARAMETER The path parameter is not valid.\r
261 The errno variable is set to EINVAL.\r
262**/\r
263RETURN_STATUS\r
264EFIAPI\r
265ParsePath(\r
266 IN const char *path,\r
267 OUT wchar_t **FullPath,\r
268 OUT DeviceNode **DevNode,\r
269 OUT int *Which\r
270 )\r
271{\r
272 int MapLen;\r
273 PATH_CLASS PathClass;\r
274 wchar_t *NewPath;\r
275 wchar_t *WPath = NULL;\r
276 wchar_t *MPath = NULL;\r
277 DeviceNode *Node = NULL;\r
278 RETURN_STATUS Status = RETURN_NOT_FOUND;\r
279 int Instance = 0;\r
280 BOOLEAN ReMapped;\r
281\r
282 ReMapped = FALSE;\r
283\r
284 // Convert name from MBCS to WCS and change '/' to '\\'\r
285 WPath = NormalizePath( path);\r
286 PathClass = ClassifyPath(WPath, &NewPath, &MapLen);\r
287\r
288reclassify:\r
289 switch(PathClass) {\r
290 case PathMapping:\r
291 if(!ReMapped) {\r
292 if((NewPath == NULL) || (*NewPath == L'\0')) { /* Nothing after the ':' */\r
293 PathClass = PathAbsolute;\r
294 }\r
295 else {\r
296 Instance = PathInstance(WPath, MapLen);\r
297 PathClass = ClassifyPath(NewPath, NULL, NULL);\r
298 }\r
299 ReMapped = TRUE;\r
300 if(WPath[MapLen] == L':') {\r
301 // Get the Map Name, including the trailing ':'. */\r
302 MPath = calloc(MapLen+2, sizeof(wchar_t));\r
303 if(MPath != NULL) {\r
304 wmemcpy(MPath, WPath, MapLen+2);\r
305 }\r
306 else {\r
307 errno = ENOMEM;\r
308 Status = RETURN_OUT_OF_RESOURCES;\r
309 break; // Exit the switch(PathClass) statement.\r
310 }\r
311 }\r
312 if(WPath != NewPath) {\r
313 /* Shift the RHS of the path down to the start of the buffer. */\r
314 wmemmove(WPath, NewPath, wcslen(NewPath)+1);\r
315 NewPath = WPath;\r
316 }\r
317 goto reclassify;\r
318 }\r
319 /* Fall through to PathError if Remapped.\r
320 This means that the path looked like "foo:bar:something".\r
321 */\r
322\r
323 case PathError:\r
324 errno = EINVAL;\r
325 Status = RETURN_INVALID_PARAMETER;\r
326 break;\r
327\r
328 case PathRelative:\r
329 /* Transform a relative path into an Absolute path.\r
330 Prepends CWD and handles ./ and ../ entries.\r
331 It is the caller's responsibility to free the space\r
332 allocated to WPath.\r
333 */\r
334 WPath = PathAdjust(NewPath); // WPath was malloc()ed by PathAdjust\r
335\r
336 case PathAbsolute:\r
337 /* Perform any path aliasing. For example: /dev/foo -> { node.foo, "" }\r
338 The current volume and directory are updated in the path as needed.\r
339 It is the caller's responsibility to free the space\r
340 allocated to WPath.\r
341 */\r
342 Status = RETURN_SUCCESS;\r
343 WPath = PathAlias(WPath, &Node); // PathAlias frees its argument and malloc()s a new one.\r
344 break;\r
345 }\r
346 if(!RETURN_ERROR(Status)) {\r
347 *FullPath = WPath;\r
348 *Which = Instance;\r
349\r
350 /* At this point, WPath is an absolute path,\r
351 MPath is either NULL or points to the Map Name,\r
352 and Instance is the instance number.\r
353 */\r
354 if(MPath == NULL) {\r
355 /* This is NOT a mapped path. */\r
356 if(Node == NULL) {\r
357 Node = daDefaultDevice;\r
358 }\r
359 if(Node != NULL) {\r
360 Status = RETURN_SUCCESS;\r
361 }\r
362 }\r
363 else {\r
364 /* This is a mapped path. */\r
365 Status = __DevSearch( MPath, NULL, &Node);\r
366 if(Status == RETURN_NOT_FOUND) {\r
367 Node = daDefaultDevice;\r
368\r
369 if(Node != NULL) {\r
370 Status = RETURN_SUCCESS;\r
371 }\r
372 }\r
373 }\r
374 if(DevNode != NULL) {\r
375 *DevNode = Node;\r
376 }\r
377 }\r
378 if(MPath != NULL) {\r
379 free(MPath); // We don't need this any more.\r
380 }\r
381 return Status;\r
382}\r