]> git.proxmox.com Git - mirror_edk2.git/blame - ShellPkg/Library/UefiShellLevel2CommandsLib/Cp.c
ArmPlatformPkg/Bds: Fixed potential overflow in EditHIInputStr()
[mirror_edk2.git] / ShellPkg / Library / UefiShellLevel2CommandsLib / Cp.c
CommitLineData
a405b86d 1/** @file\r
2 Main file for cp shell level 2 function.\r
3\r
54c9a68d 4 Copyright (c) 2009 - 2013, Intel Corporation. All rights reserved.<BR>\r
a405b86d 5 This program and the accompanying materials\r
6 are licensed and made available under the terms and conditions of the BSD License\r
7 which accompanies this distribution. 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**/\r
14\r
15#include "UefiShellLevel2CommandsLib.h"\r
f06be00e 16#include <Guid/FileSystemInfo.h>\r
17#include <Guid/FileSystemVolumeLabelInfo.h>\r
a405b86d 18\r
b54fd049 19/**\r
20 Function to take a list of files to copy and a destination location and do\r
21 the verification and copying of those files to that location. This function\r
22 will report any errors to the user and halt.\r
23\r
24 @param[in] FileList A LIST_ENTRY* based list of files to move.\r
25 @param[in] DestDir The destination location.\r
26 @param[in] SilentMode TRUE to eliminate screen output.\r
27 @param[in] RecursiveMode TRUE to copy directories.\r
28 @param[in] Resp The response to the overwrite query (if always).\r
29\r
30 @retval SHELL_SUCCESS the files were all moved.\r
31 @retval SHELL_INVALID_PARAMETER a parameter was invalid\r
32 @retval SHELL_SECURITY_VIOLATION a security violation ocurred\r
33 @retval SHELL_WRITE_PROTECTED the destination was write protected\r
34 @retval SHELL_OUT_OF_RESOURCES a memory allocation failed\r
35**/\r
a405b86d 36SHELL_STATUS\r
37EFIAPI\r
38ValidateAndCopyFiles(\r
39 IN CONST EFI_SHELL_FILE_INFO *FileList,\r
40 IN CONST CHAR16 *DestDir,\r
41 IN BOOLEAN SilentMode,\r
42 IN BOOLEAN RecursiveMode,\r
43 IN VOID **Resp\r
44 );\r
45\r
46/**\r
47 Function to Copy one file to another location\r
48\r
49 If the destination exists the user will be prompted and the result put into *resp\r
50\r
51 @param[in] Source pointer to source file name\r
52 @param[in] Dest pointer to destination file name\r
53 @param[out] Resp pointer to response from question. Pass back on looped calling\r
54 @param[in] SilentMode whether to run in quiet mode or not\r
55\r
56 @retval SHELL_SUCCESS The source file was copied to the destination\r
57**/\r
58SHELL_STATUS\r
59EFIAPI\r
60CopySingleFile(\r
61 IN CONST CHAR16 *Source,\r
62 IN CONST CHAR16 *Dest,\r
63 OUT VOID **Resp,\r
64 IN BOOLEAN SilentMode\r
65 )\r
66{\r
f06be00e 67 VOID *Response;\r
68 UINTN ReadSize;\r
69 SHELL_FILE_HANDLE SourceHandle;\r
70 SHELL_FILE_HANDLE DestHandle;\r
71 EFI_STATUS Status;\r
72 VOID *Buffer;\r
73 CHAR16 *TempName;\r
74 UINTN Size;\r
75 EFI_SHELL_FILE_INFO *List;\r
76 SHELL_STATUS ShellStatus;\r
77 UINT64 SourceFileSize;\r
78 UINT64 DestFileSize;\r
79 EFI_FILE_PROTOCOL *DestVolumeFP;\r
80 EFI_FILE_SYSTEM_INFO *DestVolumeInfo;\r
81 UINTN DestVolumeInfoSize;\r
a405b86d 82\r
83 ASSERT(Resp != NULL);\r
84\r
f06be00e 85 SourceHandle = NULL;\r
86 DestHandle = NULL;\r
87 Response = *Resp;\r
88 List = NULL;\r
89 DestVolumeInfo = NULL;\r
e755a4ca 90 ShellStatus = SHELL_SUCCESS;\r
a405b86d 91\r
92 ReadSize = PcdGet16(PcdShellFileOperationSize);\r
93 // Why bother copying a file to itself\r
94 if (StrCmp(Source, Dest) == 0) {\r
95 return (SHELL_SUCCESS);\r
96 }\r
97\r
98 //\r
99 // Open destination file without create\r
100 //\r
101 Status = ShellOpenFileByName(Dest, &DestHandle, EFI_FILE_MODE_READ|EFI_FILE_MODE_WRITE, 0);\r
102\r
103 //\r
104 // close file\r
105 //\r
106 if (DestHandle != NULL) {\r
107 ShellCloseFile(&DestHandle);\r
108 DestHandle = NULL;\r
109 }\r
110\r
111 //\r
112 // if the destination file existed check response and possibly prompt user\r
113 //\r
114 if (!EFI_ERROR(Status)) {\r
115 if (Response == NULL && !SilentMode) {\r
b54fd049 116 Status = ShellPromptForResponseHii(ShellPromptResponseTypeYesNoAllCancel, STRING_TOKEN (STR_GEN_DEST_EXIST_OVR), gShellLevel2HiiHandle, &Response);\r
a405b86d 117 }\r
118 //\r
119 // possibly return based on response\r
120 //\r
121 if (!SilentMode) {\r
122 switch (*(SHELL_PROMPT_RESPONSE*)Response) {\r
123 case ShellPromptResponseNo:\r
124 //\r
125 // return success here so we dont stop the process\r
126 //\r
127 return (SHELL_SUCCESS);\r
128 case ShellPromptResponseCancel:\r
129 *Resp = Response;\r
130 //\r
131 // indicate to stop everything\r
132 //\r
133 return (SHELL_ABORTED);\r
134 case ShellPromptResponseAll:\r
135 *Resp = Response;\r
136 case ShellPromptResponseYes:\r
137 break;\r
e9723321 138 default:\r
139 return SHELL_ABORTED;\r
a405b86d 140 }\r
141 }\r
142 }\r
143\r
144 if (ShellIsDirectory(Source) == EFI_SUCCESS) {\r
145 Status = ShellCreateDirectory(Dest, &DestHandle);\r
146 if (EFI_ERROR(Status)) {\r
147 return (SHELL_ACCESS_DENIED);\r
148 }\r
149\r
150 //\r
151 // Now copy all the files under the directory...\r
152 //\r
153 TempName = NULL;\r
f06be00e 154 Size = 0;\r
a405b86d 155 StrnCatGrow(&TempName, &Size, Source, 0);\r
156 StrnCatGrow(&TempName, &Size, L"\\*", 0);\r
7dd05623 157 if (TempName != NULL) {\r
158 ShellOpenFileMetaArg((CHAR16*)TempName, EFI_FILE_MODE_READ, &List);\r
159 *TempName = CHAR_NULL;\r
160 StrnCatGrow(&TempName, &Size, Dest, 0);\r
161 StrnCatGrow(&TempName, &Size, L"\\", 0);\r
162 ShellStatus = ValidateAndCopyFiles(List, TempName, SilentMode, TRUE, Resp);\r
163 ShellCloseFileMetaArg(&List);\r
164 SHELL_FREE_NON_NULL(TempName);\r
165 Size = 0;\r
166 }\r
a405b86d 167 } else {\r
f06be00e 168 //\r
169 // open file with create enabled\r
170 //\r
171 Status = ShellOpenFileByName(Dest, &DestHandle, EFI_FILE_MODE_READ|EFI_FILE_MODE_WRITE|EFI_FILE_MODE_CREATE, 0);\r
172 if (EFI_ERROR(Status)) {\r
173 return (SHELL_ACCESS_DENIED);\r
174 }\r
a405b86d 175\r
f06be00e 176 //\r
177 // open source file\r
178 //\r
179 Status = ShellOpenFileByName(Source, &SourceHandle, EFI_FILE_MODE_READ, 0);\r
a405b86d 180 ASSERT_EFI_ERROR(Status);\r
f06be00e 181\r
182 //\r
183 //get file size of source file and freespace available on destination volume\r
184 //\r
185 ShellGetFileSize(SourceHandle, &SourceFileSize);\r
186 ShellGetFileSize(DestHandle, &DestFileSize);\r
187\r
188 //\r
189 //if the destination file already exists then it will be replaced, meaning the sourcefile effectively needs less storage space\r
190 //\r
191 if(DestFileSize < SourceFileSize){\r
192 SourceFileSize -= DestFileSize;\r
193 } else {\r
194 SourceFileSize = 0;\r
195 }\r
196\r
197 //\r
198 //get the system volume info to check the free space\r
199 //\r
200 DestVolumeFP = ConvertShellHandleToEfiFileProtocol(DestHandle);\r
201 DestVolumeInfo = NULL;\r
202 DestVolumeInfoSize = 0;\r
203 Status = DestVolumeFP->GetInfo(\r
204 DestVolumeFP,\r
205 &gEfiFileSystemInfoGuid,\r
206 &DestVolumeInfoSize,\r
207 DestVolumeInfo\r
208 );\r
209\r
210 if (Status == EFI_BUFFER_TOO_SMALL) {\r
211 DestVolumeInfo = AllocateZeroPool(DestVolumeInfoSize);\r
212 Status = DestVolumeFP->GetInfo(\r
213 DestVolumeFP,\r
214 &gEfiFileSystemInfoGuid,\r
215 &DestVolumeInfoSize,\r
216 DestVolumeInfo\r
217 );\r
218 }\r
219\r
220 //\r
221 //check if enough space available on destination drive to complete copy\r
222 //\r
0290177a 223 if (DestVolumeInfo!= NULL && (DestVolumeInfo->FreeSpace < SourceFileSize)) {\r
f06be00e 224 //\r
225 //not enough space on destination directory to copy file\r
226 //\r
227 SHELL_FREE_NON_NULL(DestVolumeInfo);\r
228 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_CPY_FAIL), gShellLevel2HiiHandle);\r
229 return(SHELL_VOLUME_FULL);\r
230 } else {\r
231 //\r
232 // copy data between files\r
233 //\r
234 Buffer = AllocateZeroPool(ReadSize);\r
235 ASSERT(Buffer != NULL);\r
236 while (ReadSize == PcdGet16(PcdShellFileOperationSize) && !EFI_ERROR(Status)) {\r
237 Status = ShellReadFile(SourceHandle, &ReadSize, Buffer);\r
238 Status = ShellWriteFile(DestHandle, &ReadSize, Buffer);\r
239 }\r
240 }\r
241 SHELL_FREE_NON_NULL(DestVolumeInfo);\r
a405b86d 242 }\r
a405b86d 243\r
244 //\r
245 // close files\r
246 //\r
247 if (DestHandle != NULL) {\r
248 ShellCloseFile(&DestHandle);\r
249 DestHandle = NULL;\r
250 }\r
251 if (SourceHandle != NULL) {\r
252 ShellCloseFile(&SourceHandle);\r
253 SourceHandle = NULL;\r
254 }\r
255\r
256 //\r
257 // return\r
258 //\r
e755a4ca 259 return ShellStatus;\r
a405b86d 260}\r
261\r
262/**\r
263 function to take a list of files to copy and a destination location and do\r
264 the verification and copying of those files to that location. This function\r
265 will report any errors to the user and halt.\r
266\r
267 The key is to have this function called ONLY once. this allows for the parameter\r
268 verification to happen correctly.\r
269\r
b54fd049 270 @param[in] FileList A LIST_ENTRY* based list of files to move.\r
271 @param[in] DestDir The destination location.\r
272 @param[in] SilentMode TRUE to eliminate screen output.\r
273 @param[in] RecursiveMode TRUE to copy directories.\r
274 @param[in] Resp The response to the overwrite query (if always).\r
a405b86d 275\r
276 @retval SHELL_SUCCESS the files were all moved.\r
277 @retval SHELL_INVALID_PARAMETER a parameter was invalid\r
278 @retval SHELL_SECURITY_VIOLATION a security violation ocurred\r
279 @retval SHELL_WRITE_PROTECTED the destination was write protected\r
280 @retval SHELL_OUT_OF_RESOURCES a memory allocation failed\r
281**/\r
282SHELL_STATUS\r
283EFIAPI\r
284ValidateAndCopyFiles(\r
285 IN CONST EFI_SHELL_FILE_INFO *FileList,\r
286 IN CONST CHAR16 *DestDir,\r
287 IN BOOLEAN SilentMode,\r
288 IN BOOLEAN RecursiveMode,\r
289 IN VOID **Resp\r
290 )\r
291{\r
292 CHAR16 *HiiOutput;\r
293 CHAR16 *HiiResultOk;\r
294 CONST EFI_SHELL_FILE_INFO *Node;\r
295 SHELL_STATUS ShellStatus;\r
296 CHAR16 *DestPath;\r
297 VOID *Response;\r
298 UINTN PathLen;\r
299 CONST CHAR16 *Cwd;\r
300 CONST CHAR16 *TempLocation;\r
301 UINTN NewSize;\r
302\r
303 if (Resp == NULL) {\r
304 Response = NULL;\r
305 } else {\r
306 Response = *Resp;\r
307 }\r
308\r
309 DestPath = NULL;\r
310 ShellStatus = SHELL_SUCCESS;\r
311 PathLen = 0;\r
312 Cwd = ShellGetCurrentDir(NULL);\r
313\r
314 ASSERT(FileList != NULL);\r
315 ASSERT(DestDir != NULL);\r
316\r
f1518f69 317 //\r
318 // We already verified that this was present.\r
319 //\r
320 ASSERT(Cwd != NULL);\r
321\r
a405b86d 322 //\r
323 // If we are trying to copy multiple files... make sure we got a directory for the target...\r
324 //\r
325 if (EFI_ERROR(ShellIsDirectory(DestDir)) && FileList->Link.ForwardLink != FileList->Link.BackLink) {\r
326 //\r
327 // Error for destination not a directory\r
328 //\r
329 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NOT_DIR), gShellLevel2HiiHandle, DestDir);\r
330 return (SHELL_INVALID_PARAMETER);\r
331 }\r
332 for (Node = (EFI_SHELL_FILE_INFO *)GetFirstNode(&FileList->Link)\r
333 ; !IsNull(&FileList->Link, &Node->Link)\r
334 ; Node = (EFI_SHELL_FILE_INFO *)GetNextNode(&FileList->Link, &Node->Link)\r
f06be00e 335 ){\r
a405b86d 336 //\r
337 // skip the directory traversing stuff...\r
338 //\r
339 if (StrCmp(Node->FileName, L".") == 0 || StrCmp(Node->FileName, L"..") == 0) {\r
340 continue;\r
341 }\r
342\r
343 NewSize = StrSize(DestDir);\r
b54fd049 344 NewSize += StrSize(Node->FullName);\r
a405b86d 345 NewSize += StrSize(Cwd);\r
346 if (NewSize > PathLen) {\r
347 PathLen = NewSize;\r
348 }\r
349\r
350 //\r
351 // Make sure got -r if required\r
352 //\r
353 if (!RecursiveMode && !EFI_ERROR(ShellIsDirectory(Node->FullName))) {\r
354 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CP_DIR_REQ), gShellLevel2HiiHandle);\r
355 return (SHELL_INVALID_PARAMETER);\r
356 }\r
357\r
358 //\r
359 // make sure got dest as dir if needed\r
360 //\r
361 if (!EFI_ERROR(ShellIsDirectory(Node->FullName)) && EFI_ERROR(ShellIsDirectory(DestDir))) {\r
362 //\r
363 // Error for destination not a directory\r
364 //\r
365 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NOT_DIR), gShellLevel2HiiHandle, DestDir);\r
366 return (SHELL_INVALID_PARAMETER);\r
367 }\r
368 }\r
369\r
370 HiiOutput = HiiGetString (gShellLevel2HiiHandle, STRING_TOKEN (STR_CP_OUTPUT), NULL);\r
371 HiiResultOk = HiiGetString (gShellLevel2HiiHandle, STRING_TOKEN (STR_GEN_RES_OK), NULL);\r
b54fd049 372 DestPath = AllocateZeroPool(PathLen);\r
a405b86d 373\r
3e082d58 374 if (DestPath == NULL || HiiOutput == NULL || HiiResultOk == NULL) {\r
9ea69f8a 375 SHELL_FREE_NON_NULL(DestPath);\r
376 SHELL_FREE_NON_NULL(HiiOutput);\r
377 SHELL_FREE_NON_NULL(HiiResultOk);\r
378 return (SHELL_OUT_OF_RESOURCES);\r
379 }\r
380\r
a405b86d 381 //\r
382 // Go through the list of files to copy...\r
383 //\r
384 for (Node = (EFI_SHELL_FILE_INFO *)GetFirstNode(&FileList->Link)\r
385 ; !IsNull(&FileList->Link, &Node->Link)\r
386 ; Node = (EFI_SHELL_FILE_INFO *)GetNextNode(&FileList->Link, &Node->Link)\r
f06be00e 387 ){\r
a405b86d 388 if (ShellGetExecutionBreakFlag()) {\r
389 break;\r
390 }\r
391 ASSERT(Node->FileName != NULL);\r
392 ASSERT(Node->FullName != NULL);\r
393\r
394 //\r
395 // skip the directory traversing stuff...\r
396 //\r
397 if (StrCmp(Node->FileName, L".") == 0 || StrCmp(Node->FileName, L"..") == 0) {\r
398 continue;\r
399 }\r
400\r
401 if (FileList->Link.ForwardLink == FileList->Link.BackLink // 1 item\r
402 && EFI_ERROR(ShellIsDirectory(DestDir)) // not an existing directory\r
f06be00e 403 ) {\r
b54fd049 404 if (StrStr(DestDir, L":") == NULL) {\r
405 //\r
406 // simple copy of a single file\r
407 //\r
408 StrCpy(DestPath, Cwd);\r
409 if (DestPath[StrLen(DestPath)-1] != L'\\' && DestDir[0] != L'\\') {\r
410 StrCat(DestPath, L"\\");\r
411 } else if (DestPath[StrLen(DestPath)-1] == L'\\' && DestDir[0] == L'\\') {\r
412 ((CHAR16*)DestPath)[StrLen(DestPath)-1] = CHAR_NULL;\r
413 }\r
414 StrCat(DestPath, DestDir);\r
415 } else {\r
416 StrCpy(DestPath, DestDir);\r
a405b86d 417 }\r
a405b86d 418 } else {\r
419 //\r
420 // we have multiple files or a directory in the DestDir\r
421 //\r
1fc3749d 422 \r
423 //\r
424 // Check for leading slash\r
425 //\r
426 if (DestDir[0] == L'\\') {\r
f06be00e 427 //\r
428 // Copy to the root of CWD\r
429 //\r
1fc3749d 430 StrCpy(DestPath, Cwd);\r
431 while (PathRemoveLastItem(DestPath));\r
432 StrCat(DestPath, DestDir+1);\r
433 StrCat(DestPath, Node->FileName);\r
434 } else if (StrStr(DestDir, L":") == NULL) {\r
a405b86d 435 StrCpy(DestPath, Cwd);\r
436 if (DestPath[StrLen(DestPath)-1] != L'\\' && DestDir[0] != L'\\') {\r
437 StrCat(DestPath, L"\\");\r
438 } else if (DestPath[StrLen(DestPath)-1] == L'\\' && DestDir[0] == L'\\') {\r
439 ((CHAR16*)DestPath)[StrLen(DestPath)-1] = CHAR_NULL;\r
440 }\r
441 StrCat(DestPath, DestDir);\r
442 if (DestDir[StrLen(DestDir)-1] != L'\\' && Node->FileName[0] != L'\\') {\r
443 StrCat(DestPath, L"\\");\r
444 } else if (DestDir[StrLen(DestDir)-1] == L'\\' && Node->FileName[0] == L'\\') {\r
445 ((CHAR16*)DestPath)[StrLen(DestPath)-1] = CHAR_NULL;\r
446 }\r
447 StrCat(DestPath, Node->FileName);\r
448\r
449 } else {\r
450 StrCpy(DestPath, DestDir);\r
451 if (DestDir[StrLen(DestDir)-1] != L'\\' && Node->FileName[0] != L'\\') {\r
452 StrCat(DestPath, L"\\");\r
453 } else if (DestDir[StrLen(DestDir)-1] == L'\\' && Node->FileName[0] == L'\\') {\r
454 ((CHAR16*)DestDir)[StrLen(DestDir)-1] = CHAR_NULL;\r
455 }\r
456 StrCat(DestPath, Node->FileName);\r
457 }\r
458 }\r
459\r
460 //\r
461 // Make sure the path exists\r
462 //\r
463 if (EFI_ERROR(VerifyIntermediateDirectories(DestPath))) {\r
464 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CP_DIR_WNF), gShellLevel2HiiHandle);\r
465 ShellStatus = SHELL_DEVICE_ERROR;\r
466 break;\r
467 }\r
468\r
469 if ( !EFI_ERROR(ShellIsDirectory(Node->FullName))\r
470 && !EFI_ERROR(ShellIsDirectory(DestPath))\r
471 && StrniCmp(Node->FullName, DestPath, StrLen(DestPath)) == NULL\r
f06be00e 472 ){\r
a405b86d 473 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CP_SD_PARENT), gShellLevel2HiiHandle);\r
474 ShellStatus = SHELL_INVALID_PARAMETER;\r
475 break;\r
476 }\r
477 if (StringNoCaseCompare(&Node->FullName, &DestPath) == 0) {\r
478 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CP_SD_SAME), gShellLevel2HiiHandle);\r
479 ShellStatus = SHELL_INVALID_PARAMETER;\r
480 break;\r
481 }\r
482\r
483 if ((TempLocation = StrniCmp(Node->FullName, DestPath, StrLen(Node->FullName))) == 0\r
484 && (DestPath[StrLen(Node->FullName)] == CHAR_NULL || DestPath[StrLen(Node->FullName)] == L'\\')\r
f06be00e 485 ) {\r
a405b86d 486 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CP_SD_SAME), gShellLevel2HiiHandle);\r
487 ShellStatus = SHELL_INVALID_PARAMETER;\r
488 break;\r
489 }\r
490\r
ab94587a 491 PathCleanUpDirectories(DestPath);\r
a405b86d 492\r
493 ShellPrintEx(-1, -1, HiiOutput, Node->FullName, DestPath);\r
494\r
495 //\r
496 // copy single file...\r
497 //\r
498 ShellStatus = CopySingleFile(Node->FullName, DestPath, &Response, SilentMode);\r
499 if (ShellStatus != SHELL_SUCCESS) {\r
500 break;\r
501 }\r
502 }\r
503 if (ShellStatus == SHELL_SUCCESS && Resp == NULL) {\r
504 ShellPrintEx(-1, -1, L"%s", HiiResultOk);\r
505 }\r
506\r
507 SHELL_FREE_NON_NULL(DestPath);\r
508 SHELL_FREE_NON_NULL(HiiOutput);\r
509 SHELL_FREE_NON_NULL(HiiResultOk);\r
590c3cb1 510 if (Resp == NULL) {\r
a405b86d 511 SHELL_FREE_NON_NULL(Response);\r
512 }\r
513\r
514 return (ShellStatus);\r
f06be00e 515\r
a405b86d 516}\r
517\r
b54fd049 518/**\r
519 Validate and if successful copy all the files from the list into \r
520 destination directory.\r
521\r
522 @param[in] FileList The list of files to copy.\r
523 @param[in] DestDir The directory to copy files to.\r
524 @param[in] SilentMode TRUE to eliminate screen output.\r
525 @param[in] RecursiveMode TRUE to copy directories.\r
526\r
527 @retval SHELL_INVALID_PARAMETER A parameter was invalid.\r
528 @retval SHELL_SUCCESS The operation was successful.\r
529**/\r
a405b86d 530SHELL_STATUS\r
531EFIAPI\r
532ProcessValidateAndCopyFiles(\r
533 IN EFI_SHELL_FILE_INFO *FileList,\r
534 IN CONST CHAR16 *DestDir,\r
535 IN BOOLEAN SilentMode,\r
536 IN BOOLEAN RecursiveMode\r
537 )\r
538{\r
539 SHELL_STATUS ShellStatus;\r
540 EFI_SHELL_FILE_INFO *List;\r
a405b86d 541 EFI_FILE_INFO *FileInfo;\r
542\r
543 List = NULL;\r
544\r
e755a4ca 545 ShellOpenFileMetaArg((CHAR16*)DestDir, EFI_FILE_MODE_READ, &List);\r
a405b86d 546 if (List != NULL && List->Link.ForwardLink != List->Link.BackLink) {\r
547 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_MARG_ERROR), gShellLevel2HiiHandle, DestDir);\r
548 ShellStatus = SHELL_INVALID_PARAMETER;\r
549 ShellCloseFileMetaArg(&List);\r
550 } else if (List != NULL) {\r
a405b86d 551 ASSERT(((EFI_SHELL_FILE_INFO *)List->Link.ForwardLink) != NULL);\r
552 ASSERT(((EFI_SHELL_FILE_INFO *)List->Link.ForwardLink)->FullName != NULL);\r
553 FileInfo = NULL;\r
554 FileInfo = gEfiShellProtocol->GetFileInfo(((EFI_SHELL_FILE_INFO *)List->Link.ForwardLink)->Handle);\r
555 ASSERT(FileInfo != NULL);\r
556 if ((FileInfo->Attribute & EFI_FILE_READ_ONLY) == 0) {\r
557 ShellStatus = ValidateAndCopyFiles(FileList, ((EFI_SHELL_FILE_INFO *)List->Link.ForwardLink)->FullName, SilentMode, RecursiveMode, NULL);\r
558 } else {\r
559 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CP_DEST_ERROR), gShellLevel2HiiHandle);\r
560 ShellStatus = SHELL_ACCESS_DENIED;\r
561 }\r
562 SHELL_FREE_NON_NULL(FileInfo);\r
563 ShellCloseFileMetaArg(&List);\r
564 } else {\r
565 ShellStatus = ValidateAndCopyFiles(FileList, DestDir, SilentMode, RecursiveMode, NULL);\r
566 }\r
567\r
568 return (ShellStatus);\r
569}\r
570\r
571STATIC CONST SHELL_PARAM_ITEM ParamList[] = {\r
572 {L"-r", TypeFlag},\r
573 {L"-q", TypeFlag},\r
574 {NULL, TypeMax}\r
575 };\r
576\r
577/**\r
578 Function for 'cp' command.\r
579\r
580 @param[in] ImageHandle Handle to the Image (NULL if Internal).\r
581 @param[in] SystemTable Pointer to the System Table (NULL if Internal).\r
582**/\r
583SHELL_STATUS\r
584EFIAPI\r
585ShellCommandRunCp (\r
586 IN EFI_HANDLE ImageHandle,\r
587 IN EFI_SYSTEM_TABLE *SystemTable\r
588 )\r
589{\r
590 EFI_STATUS Status;\r
591 LIST_ENTRY *Package;\r
592 CHAR16 *ProblemParam;\r
593 SHELL_STATUS ShellStatus;\r
594 UINTN ParamCount;\r
595 UINTN LoopCounter;\r
596 EFI_SHELL_FILE_INFO *FileList;\r
597 BOOLEAN SilentMode;\r
598 BOOLEAN RecursiveMode;\r
599 CONST CHAR16 *Cwd;\r
600\r
601 ProblemParam = NULL;\r
602 ShellStatus = SHELL_SUCCESS;\r
603 ParamCount = 0;\r
604 FileList = NULL;\r
605\r
606 //\r
607 // initialize the shell lib (we must be in non-auto-init...)\r
608 //\r
609 Status = ShellInitialize();\r
610 ASSERT_EFI_ERROR(Status);\r
611\r
612 Status = CommandInit();\r
613 ASSERT_EFI_ERROR(Status);\r
614\r
615 //\r
616 // parse the command line\r
617 //\r
618 Status = ShellCommandLineParse (ParamList, &Package, &ProblemParam, TRUE);\r
619 if (EFI_ERROR(Status)) {\r
620 if (Status == EFI_VOLUME_CORRUPTED && ProblemParam != NULL) {\r
621 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellLevel2HiiHandle, ProblemParam);\r
622 FreePool(ProblemParam);\r
623 ShellStatus = SHELL_INVALID_PARAMETER;\r
624 } else {\r
625 ASSERT(FALSE);\r
626 }\r
627 } else {\r
628 //\r
629 // check for "-?"\r
630 //\r
631 if (ShellCommandLineGetFlag(Package, L"-?")) {\r
632 ASSERT(FALSE);\r
633 }\r
634\r
635 //\r
636 // Initialize SilentMode and RecursiveMode\r
637 //\r
638 if (gEfiShellProtocol->BatchIsActive()) {\r
639 SilentMode = TRUE;\r
640 } else {\r
641 SilentMode = ShellCommandLineGetFlag(Package, L"-q");\r
642 }\r
643 RecursiveMode = ShellCommandLineGetFlag(Package, L"-r");\r
644\r
645 switch (ParamCount = ShellCommandLineGetCount(Package)) {\r
646 case 0:\r
647 case 1:\r
648 //\r
649 // we have insufficient parameters\r
650 //\r
651 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellLevel2HiiHandle);\r
652 ShellStatus = SHELL_INVALID_PARAMETER;\r
653 break;\r
654 case 2:\r
655 //\r
656 // must have valid CWD for single parameter...\r
657 //\r
658 Cwd = ShellGetCurrentDir(NULL);\r
659 if (Cwd == NULL){\r
660 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_CWD), gShellLevel2HiiHandle);\r
661 ShellStatus = SHELL_INVALID_PARAMETER;\r
662 } else {\r
663 Status = ShellOpenFileMetaArg((CHAR16*)ShellCommandLineGetRawValue(Package, 1), EFI_FILE_MODE_WRITE|EFI_FILE_MODE_READ, &FileList);\r
664 if (FileList == NULL || IsListEmpty(&FileList->Link) || EFI_ERROR(Status)) {\r
665 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_NF), gShellLevel2HiiHandle, ShellCommandLineGetRawValue(Package, 1));\r
666 ShellStatus = SHELL_NOT_FOUND;\r
667 } else {\r
668 ShellStatus = ProcessValidateAndCopyFiles(FileList, Cwd, SilentMode, RecursiveMode);\r
669 }\r
670 }\r
671\r
672 break;\r
673 default:\r
674 //\r
675 // Make a big list of all the files...\r
676 //\r
677 for (ParamCount--, LoopCounter = 1 ; LoopCounter < ParamCount && ShellStatus == SHELL_SUCCESS ; LoopCounter++) {\r
678 if (ShellGetExecutionBreakFlag()) {\r
679 break;\r
680 }\r
681 Status = ShellOpenFileMetaArg((CHAR16*)ShellCommandLineGetRawValue(Package, LoopCounter), EFI_FILE_MODE_WRITE|EFI_FILE_MODE_READ, &FileList);\r
682 if (EFI_ERROR(Status) || FileList == NULL || IsListEmpty(&FileList->Link)) {\r
b2bf9735 683 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_NF), gShellLevel2HiiHandle, ShellCommandLineGetRawValue(Package, LoopCounter));\r
a405b86d 684 ShellStatus = SHELL_NOT_FOUND;\r
685 }\r
686 }\r
b2bf9735 687 if (ShellStatus != SHELL_SUCCESS) {\r
a405b86d 688 Status = ShellCloseFileMetaArg(&FileList);\r
b2bf9735 689 } else {\r
690 //\r
691 // now copy them all...\r
692 //\r
693 if (FileList != NULL && !IsListEmpty(&FileList->Link)) {\r
ab94587a 694 ShellStatus = ProcessValidateAndCopyFiles(FileList, PathCleanUpDirectories((CHAR16*)ShellCommandLineGetRawValue(Package, ParamCount)), SilentMode, RecursiveMode);\r
b2bf9735 695 Status = ShellCloseFileMetaArg(&FileList);\r
696 if (EFI_ERROR(Status) && ShellStatus == SHELL_SUCCESS) {\r
697 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_ERR_FILE), gShellLevel2HiiHandle, ShellCommandLineGetRawValue(Package, ParamCount), ShellStatus|MAX_BIT);\r
698 ShellStatus = SHELL_ACCESS_DENIED;\r
699 }\r
a405b86d 700 }\r
701 }\r
a405b86d 702 break;\r
703 } // switch on parameter count\r
704\r
705 if (FileList != NULL) {\r
706 ShellCloseFileMetaArg(&FileList);\r
707 }\r
708\r
709 //\r
710 // free the command line package\r
711 //\r
712 ShellCommandLineFreeVarList (Package);\r
713 }\r
714\r
715 if (ShellGetExecutionBreakFlag()) {\r
716 return (SHELL_ABORTED);\r
717 }\r
718\r
719 return (ShellStatus);\r
720}\r
721\r