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