]> git.proxmox.com Git - mirror_edk2.git/blame - ShellPkg/Library/UefiShellLevel2CommandsLib/Rm.c
ShellPkg/hexedit: Fix a read-after-free bug
[mirror_edk2.git] / ShellPkg / Library / UefiShellLevel2CommandsLib / Rm.c
CommitLineData
a405b86d 1/** @file\r
2 Main file for attrib shell level 2 function.\r
3\r
c011b6c9 4 (C) Copyright 2015 Hewlett-Packard Development Company, L.P.<BR>\r
e75390f0 5 Copyright (c) 2009 - 2015, Intel Corporation. All rights reserved.<BR>\r
a405b86d 6 This program and the accompanying materials\r
7 are licensed and made available under the terms and conditions of the BSD License\r
8 which accompanies this distribution. The full text of the license may be found at\r
9 http://opensource.org/licenses/bsd-license.php\r
10\r
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13\r
14**/\r
15\r
16#include "UefiShellLevel2CommandsLib.h"\r
17\r
18STATIC CONST SHELL_PARAM_ITEM ParamList[] = {\r
19 {L"-q", TypeFlag},\r
20 {NULL, TypeMax}\r
21 };\r
22\r
b54fd049 23/**\r
24 Determine if a directory has no files in it.\r
25\r
26 @param[in] FileHandle The EFI_HANDLE to the directory.\r
27 \r
28 @retval TRUE The directory has no files (or directories).\r
29 @retval FALSE The directory has at least 1 file or directory in it.\r
30**/\r
a405b86d 31BOOLEAN\r
a405b86d 32IsDirectoryEmpty (\r
33 IN EFI_HANDLE FileHandle\r
34 )\r
35{\r
a405b86d 36 EFI_FILE_INFO *FileInfo;\r
37 BOOLEAN NoFile;\r
38 BOOLEAN RetVal;\r
39\r
40 RetVal = TRUE;\r
41 NoFile = FALSE;\r
0d807dae 42 FileInfo = NULL;\r
a405b86d 43\r
e755a4ca 44 for (FileHandleFindFirstFile(FileHandle, &FileInfo)\r
a405b86d 45 ; !NoFile\r
e755a4ca 46 ; FileHandleFindNextFile(FileHandle, FileInfo, &NoFile)\r
a405b86d 47 ){\r
48 if (StrStr(FileInfo->FileName, L".") != FileInfo->FileName\r
49 &&StrStr(FileInfo->FileName, L"..") != FileInfo->FileName) {\r
50 RetVal = FALSE;\r
51 }\r
52 }\r
53 return (RetVal);\r
54}\r
55\r
b54fd049 56/**\r
57 Delete a node and all nodes under it (including sub directories).\r
58\r
59 @param[in] Node The node to start deleting with.\r
60 @param[in] Quiet TRUE to print no messages.\r
61\r
62 @retval SHELL_SUCCESS The operation was successful.\r
63 @retval SHELL_ACCESS_DENIED A file was read only.\r
64 @retval SHELL_ABORTED The abort message was received.\r
65 @retval SHELL_DEVICE_ERROR A device error occured reading this Node.\r
66**/\r
a405b86d 67SHELL_STATUS\r
a405b86d 68CascadeDelete(\r
69 IN EFI_SHELL_FILE_INFO *Node,\r
70 IN CONST BOOLEAN Quiet\r
71 )\r
72{\r
73 SHELL_STATUS ShellStatus;\r
74 EFI_SHELL_FILE_INFO *List;\r
75 EFI_SHELL_FILE_INFO *Node2;\r
76 EFI_STATUS Status;\r
77 SHELL_PROMPT_RESPONSE *Resp;\r
22feb630 78 CHAR16 *TempName;\r
81957f8d 79 UINTN NewSize;\r
a405b86d 80\r
81 Resp = NULL;\r
82 ShellStatus = SHELL_SUCCESS;\r
83 List = NULL;\r
84 Status = EFI_SUCCESS;\r
85\r
86 if ((Node->Info->Attribute & EFI_FILE_READ_ONLY) == EFI_FILE_READ_ONLY) {\r
099e8ff5 87 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_RM_LOG_DETELE_RO), gShellLevel2HiiHandle, L"rm", Node->FullName); \r
a405b86d 88 return (SHELL_ACCESS_DENIED);\r
89 }\r
90\r
91 if ((Node->Info->Attribute & EFI_FILE_DIRECTORY) == EFI_FILE_DIRECTORY) {\r
92 if (!IsDirectoryEmpty(Node->Handle)) {\r
93 if (!Quiet) {\r
94 Status = ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN(STR_RM_LOG_DELETE_CONF), gShellLevel2HiiHandle, Node->FullName);\r
95 Status = ShellPromptForResponse(ShellPromptResponseTypeYesNo, NULL, (VOID**)&Resp);\r
a405b86d 96 ASSERT(Resp != NULL);\r
97 if (EFI_ERROR(Status) || *Resp != ShellPromptResponseYes) {\r
98 SHELL_FREE_NON_NULL(Resp);\r
99 return (SHELL_ABORTED);\r
100 }\r
101 SHELL_FREE_NON_NULL(Resp);\r
102 }\r
103 //\r
104 // empty out the directory\r
105 //\r
106 Status = gEfiShellProtocol->FindFilesInDir(Node->Handle, &List);\r
107 if (EFI_ERROR(Status)) {\r
108 if (List!=NULL) {\r
109 gEfiShellProtocol->FreeFileList(&List);\r
110 }\r
111 return (SHELL_DEVICE_ERROR);\r
112 }\r
113 for (Node2 = (EFI_SHELL_FILE_INFO *)GetFirstNode(&List->Link)\r
114 ; !IsNull(&List->Link, &Node2->Link)\r
115 ; Node2 = (EFI_SHELL_FILE_INFO *)GetNextNode(&List->Link, &Node2->Link)\r
116 ){\r
117 //\r
118 // skip the directory traversing stuff...\r
119 //\r
120 if (StrCmp(Node2->FileName, L".") == 0 || StrCmp(Node2->FileName, L"..") == 0) {\r
121 continue;\r
122 }\r
123 Node2->Status = gEfiShellProtocol->OpenFileByName (Node2->FullName, &Node2->Handle, EFI_FILE_MODE_READ|EFI_FILE_MODE_WRITE);\r
22feb630
JC
124 if (EFI_ERROR(Node2->Status) && StrStr(Node2->FileName, L":") == NULL) {\r
125 //\r
126 // Update the node filename to have full path with file system identifier\r
127 //\r
81957f8d
JC
128 NewSize = StrSize(Node->FullName) + StrSize(Node2->FullName);\r
129 TempName = AllocateZeroPool(NewSize);\r
7461437c 130 if (TempName == NULL) {\r
73c5c619 131 ShellStatus = SHELL_OUT_OF_RESOURCES;\r
7461437c 132 } else {\r
e75390f0 133 StrCpyS(TempName, NewSize/sizeof(CHAR16), Node->FullName);\r
7461437c 134 TempName[StrStr(TempName, L":")+1-TempName] = CHAR_NULL;\r
e75390f0 135 StrCatS(TempName, NewSize/sizeof(CHAR16), Node2->FullName);\r
7461437c 136 FreePool((VOID*)Node2->FullName);\r
137 Node2->FullName = TempName;\r
138\r
139 //\r
140 // Now try again to open the file\r
141 //\r
142 Node2->Status = gEfiShellProtocol->OpenFileByName (Node2->FullName, &Node2->Handle, EFI_FILE_MODE_READ|EFI_FILE_MODE_WRITE);\r
143 }\r
144 }\r
145 if (!EFI_ERROR(Node2->Status)) {\r
146 ShellStatus = CascadeDelete(Node2, Quiet);\r
147 } else if (ShellStatus == SHELL_SUCCESS) {\r
75a68c69 148 ShellStatus = (SHELL_STATUS)(Node2->Status&(~0x80000000));\r
22feb630 149 }\r
a405b86d 150 if (ShellStatus != SHELL_SUCCESS) {\r
151 if (List!=NULL) {\r
152 gEfiShellProtocol->FreeFileList(&List);\r
153 }\r
154 return (ShellStatus);\r
155 }\r
156 }\r
157 if (List!=NULL) {\r
158 gEfiShellProtocol->FreeFileList(&List);\r
159 }\r
160 }\r
161 }\r
162\r
163 if (!(StrCmp(Node->FileName, L".") == 0 || StrCmp(Node->FileName, L"..") == 0)) {\r
164 //\r
165 // now delete the current node...\r
166 //\r
5a51ad8d
JC
167 if (!Quiet) {\r
168 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_RM_LOG_DELETE), gShellLevel2HiiHandle, Node->FullName);\r
169 }\r
a405b86d 170 Status = gEfiShellProtocol->DeleteFile(Node->Handle);\r
171 Node->Handle = NULL;\r
172 }\r
173\r
174 //\r
b54fd049 175 // We cant allow for the warning here! (Dont use EFI_ERROR Macro).\r
a405b86d 176 //\r
177 if (Status != EFI_SUCCESS){\r
178 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_RM_LOG_DELETE_ERR), gShellLevel2HiiHandle, Status);\r
179 return (SHELL_ACCESS_DENIED);\r
180 } else {\r
a737ea73
JC
181 if (!Quiet) {\r
182 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_RM_LOG_DELETE_COMP), gShellLevel2HiiHandle);\r
183 }\r
a405b86d 184 return (SHELL_SUCCESS);\r
185 }\r
186}\r
187\r
b54fd049 188/**\r
cceb4ebd 189 Determines if a Node is a valid delete target. Will prevent deleting the root directory.\r
b54fd049 190\r
191 @param[in] List RESERVED. Not used.\r
192 @param[in] Node The node to analyze.\r
193 @param[in] Package RESERVED. Not used.\r
194**/\r
a405b86d 195BOOLEAN\r
a405b86d 196IsValidDeleteTarget(\r
197 IN CONST EFI_SHELL_FILE_INFO *List,\r
198 IN CONST EFI_SHELL_FILE_INFO *Node,\r
199 IN CONST LIST_ENTRY *Package\r
200 )\r
201{\r
202 CONST CHAR16 *TempLocation;\r
eef1ed46 203 BOOLEAN RetVal;\r
204 CHAR16 *SearchString;\r
205 CHAR16 *Pattern;\r
a405b86d 206 UINTN Size;\r
207\r
eef1ed46 208 if (Node == NULL || Node->FullName == NULL) {\r
209 return (FALSE);\r
210 }\r
211\r
a405b86d 212 TempLocation = StrStr(Node->FullName, L":");\r
eef1ed46 213 if (StrLen(TempLocation) <= 2) {\r
a405b86d 214 //\r
215 // Deleting the root directory is invalid.\r
216 //\r
217 return (FALSE);\r
218 }\r
eef1ed46 219\r
a405b86d 220 TempLocation = ShellGetCurrentDir(NULL);\r
eef1ed46 221 if (TempLocation == NULL) {\r
222 //\r
223 // No working directory is specified so whatever is left is ok.\r
224 //\r
225 return (TRUE);\r
a405b86d 226 }\r
a405b86d 227\r
eef1ed46 228 Pattern = NULL;\r
229 SearchString = NULL;\r
230 Size = 0;\r
fbd2dfad
QS
231 Pattern = StrnCatGrow(&Pattern, &Size, TempLocation , 0);\r
232 Pattern = StrnCatGrow(&Pattern, &Size, L"\\" , 0);\r
233 Size = 0;\r
eef1ed46 234 SearchString = StrnCatGrow(&SearchString, &Size, Node->FullName, 0);\r
a32980dd 235 if (!EFI_ERROR(ShellIsDirectory(SearchString))) {\r
236 SearchString = StrnCatGrow(&SearchString, &Size, L"\\", 0);\r
237 SearchString = StrnCatGrow(&SearchString, &Size, L"*", 0);\r
238 }\r
eef1ed46 239\r
240 if (Pattern == NULL || SearchString == NULL) {\r
241 RetVal = FALSE;\r
242 } else {\r
243 RetVal = TRUE;\r
244 if (gUnicodeCollation->MetaiMatch(gUnicodeCollation, Pattern, SearchString)) {\r
245 RetVal = FALSE;\r
246 }\r
247 }\r
248\r
249 SHELL_FREE_NON_NULL(Pattern );\r
250 SHELL_FREE_NON_NULL(SearchString);\r
251\r
252 return (RetVal);\r
a405b86d 253}\r
254\r
255/**\r
256 Function for 'rm' command.\r
257\r
258 @param[in] ImageHandle Handle to the Image (NULL if Internal).\r
259 @param[in] SystemTable Pointer to the System Table (NULL if Internal).\r
260**/\r
261SHELL_STATUS\r
262EFIAPI\r
263ShellCommandRunRm (\r
264 IN EFI_HANDLE ImageHandle,\r
265 IN EFI_SYSTEM_TABLE *SystemTable\r
266 )\r
267{\r
268 EFI_STATUS Status;\r
269 LIST_ENTRY *Package;\r
270 CHAR16 *ProblemParam;\r
271 CONST CHAR16 *Param;\r
272 SHELL_STATUS ShellStatus;\r
273 UINTN ParamCount;\r
274 EFI_SHELL_FILE_INFO *FileList;\r
275 EFI_SHELL_FILE_INFO *Node;\r
276\r
277 ProblemParam = NULL;\r
278 ShellStatus = SHELL_SUCCESS;\r
279 ParamCount = 0;\r
280 FileList = NULL;\r
281\r
282 //\r
283 // initialize the shell lib (we must be in non-auto-init...)\r
284 //\r
285 Status = ShellInitialize();\r
286 ASSERT_EFI_ERROR(Status);\r
287\r
288 //\r
289 // parse the command line\r
290 //\r
291 Status = ShellCommandLineParse (ParamList, &Package, &ProblemParam, TRUE);\r
292 if (EFI_ERROR(Status)) {\r
293 if (Status == EFI_VOLUME_CORRUPTED && ProblemParam != NULL) {\r
099e8ff5 294 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellLevel2HiiHandle, L"rm", ProblemParam); \r
a405b86d 295 FreePool(ProblemParam);\r
296 ShellStatus = SHELL_INVALID_PARAMETER;\r
297 } else {\r
298 ASSERT(FALSE);\r
299 }\r
300 } else {\r
301 //\r
302 // check for "-?"\r
303 //\r
304 if (ShellCommandLineGetFlag(Package, L"-?")) {\r
305 ASSERT(FALSE);\r
306 }\r
307 if (ShellCommandLineGetRawValue(Package, 1) == NULL) {\r
308 //\r
309 // we insufficient parameters\r
310 //\r
099e8ff5 311 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellLevel2HiiHandle, L"rm"); \r
a405b86d 312 ShellStatus = SHELL_INVALID_PARAMETER;\r
313 } else {\r
314 //\r
315 // get a list with each file specified by parameters\r
316 // if parameter is a directory then add all the files below it to the list\r
317 //\r
318 for ( ParamCount = 1, Param = ShellCommandLineGetRawValue(Package, ParamCount)\r
319 ; Param != NULL\r
320 ; ParamCount++, Param = ShellCommandLineGetRawValue(Package, ParamCount)\r
321 ){\r
322 Status = ShellOpenFileMetaArg((CHAR16*)Param, EFI_FILE_MODE_WRITE|EFI_FILE_MODE_READ, &FileList);\r
323 if (EFI_ERROR(Status) || FileList == NULL || IsListEmpty(&FileList->Link)) {\r
099e8ff5 324 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_NF), gShellLevel2HiiHandle, L"rm", (CHAR16*)Param); \r
a405b86d 325 ShellStatus = SHELL_NOT_FOUND;\r
326 break;\r
327 }\r
328 }\r
329\r
330 if (ShellStatus == SHELL_SUCCESS){\r
331 //\r
332 // loop through the list and make sure we are not aborting...\r
333 //\r
334 for ( Node = (EFI_SHELL_FILE_INFO*)GetFirstNode(&FileList->Link)\r
335 ; !IsNull(&FileList->Link, &Node->Link) && !ShellGetExecutionBreakFlag()\r
336 ; Node = (EFI_SHELL_FILE_INFO*)GetNextNode(&FileList->Link, &Node->Link)\r
337 ){\r
338 //\r
339 // skip the directory traversing stuff...\r
340 //\r
341 if (StrCmp(Node->FileName, L".") == 0 || StrCmp(Node->FileName, L"..") == 0) {\r
342 continue;\r
343 }\r
344\r
345 //\r
346 // do the deleting of nodes\r
347 //\r
348 if (EFI_ERROR(Node->Status)){\r
349 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_RM_LOG_DELETE_ERR2), gShellLevel2HiiHandle, Node->Status);\r
350 ShellStatus = SHELL_ACCESS_DENIED;\r
351 break;\r
352 }\r
353 if (!IsValidDeleteTarget(FileList, Node, Package)) {\r
354 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_RM_LOG_DELETE_ERR3), gShellLevel2HiiHandle, Node->FullName);\r
355 ShellStatus = SHELL_INVALID_PARAMETER;\r
356 break;\r
357 }\r
358\r
359 ShellStatus = CascadeDelete(Node, ShellCommandLineGetFlag(Package, L"-q"));\r
360 }\r
361 }\r
362 //\r
363 // Free the fileList\r
364 //\r
365 if (FileList != NULL) {\r
366 Status = ShellCloseFileMetaArg(&FileList);\r
367 }\r
368 FileList = NULL;\r
369 }\r
370\r
371 //\r
372 // free the command line package\r
373 //\r
374 ShellCommandLineFreeVarList (Package);\r
375 }\r
376\r
377 return (ShellStatus);\r
378}\r
379\r