]> git.proxmox.com Git - mirror_edk2.git/blob - ShellPkg/Library/UefiShellLevel2CommandsLib/Cp.c
ShellPkg: Fixed build error 'variable set but not used'
[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 ShellStatus = SHELL_SUCCESS;
91
92 ReadSize = PcdGet16(PcdShellFileOperationSize);
93 // Why bother copying a file to itself
94 if (StrCmp(Source, Dest) == 0) {
95 return (SHELL_SUCCESS);
96 }
97
98 //
99 // Open destination file without create
100 //
101 Status = ShellOpenFileByName(Dest, &DestHandle, EFI_FILE_MODE_READ|EFI_FILE_MODE_WRITE, 0);
102
103 //
104 // close file
105 //
106 if (DestHandle != NULL) {
107 ShellCloseFile(&DestHandle);
108 DestHandle = NULL;
109 }
110
111 //
112 // if the destination file existed check response and possibly prompt user
113 //
114 if (!EFI_ERROR(Status)) {
115 if (Response == NULL && !SilentMode) {
116 Status = ShellPromptForResponseHii(ShellPromptResponseTypeYesNoAllCancel, STRING_TOKEN (STR_GEN_DEST_EXIST_OVR), gShellLevel2HiiHandle, &Response);
117 }
118 //
119 // possibly return based on response
120 //
121 if (!SilentMode) {
122 switch (*(SHELL_PROMPT_RESPONSE*)Response) {
123 case ShellPromptResponseNo:
124 //
125 // return success here so we dont stop the process
126 //
127 return (SHELL_SUCCESS);
128 case ShellPromptResponseCancel:
129 *Resp = Response;
130 //
131 // indicate to stop everything
132 //
133 return (SHELL_ABORTED);
134 case ShellPromptResponseAll:
135 *Resp = Response;
136 case ShellPromptResponseYes:
137 break;
138 default:
139 return SHELL_ABORTED;
140 }
141 }
142 }
143
144 if (ShellIsDirectory(Source) == EFI_SUCCESS) {
145 Status = ShellCreateDirectory(Dest, &DestHandle);
146 if (EFI_ERROR(Status)) {
147 return (SHELL_ACCESS_DENIED);
148 }
149
150 //
151 // Now copy all the files under the directory...
152 //
153 TempName = NULL;
154 Size = 0;
155 StrnCatGrow(&TempName, &Size, Source, 0);
156 StrnCatGrow(&TempName, &Size, L"\\*", 0);
157 if (TempName != NULL) {
158 ShellOpenFileMetaArg((CHAR16*)TempName, EFI_FILE_MODE_READ, &List);
159 *TempName = CHAR_NULL;
160 StrnCatGrow(&TempName, &Size, Dest, 0);
161 StrnCatGrow(&TempName, &Size, L"\\", 0);
162 ShellStatus = ValidateAndCopyFiles(List, TempName, SilentMode, TRUE, Resp);
163 ShellCloseFileMetaArg(&List);
164 SHELL_FREE_NON_NULL(TempName);
165 Size = 0;
166 }
167 } else {
168 //
169 // open file with create enabled
170 //
171 Status = ShellOpenFileByName(Dest, &DestHandle, EFI_FILE_MODE_READ|EFI_FILE_MODE_WRITE|EFI_FILE_MODE_CREATE, 0);
172 if (EFI_ERROR(Status)) {
173 return (SHELL_ACCESS_DENIED);
174 }
175
176 //
177 // open source file
178 //
179 Status = ShellOpenFileByName(Source, &SourceHandle, EFI_FILE_MODE_READ, 0);
180 ASSERT_EFI_ERROR(Status);
181
182 //
183 //get file size of source file and freespace available on destination volume
184 //
185 ShellGetFileSize(SourceHandle, &SourceFileSize);
186 ShellGetFileSize(DestHandle, &DestFileSize);
187
188 //
189 //if the destination file already exists then it will be replaced, meaning the sourcefile effectively needs less storage space
190 //
191 if(DestFileSize < SourceFileSize){
192 SourceFileSize -= DestFileSize;
193 } else {
194 SourceFileSize = 0;
195 }
196
197 //
198 //get the system volume info to check the free space
199 //
200 DestVolumeFP = ConvertShellHandleToEfiFileProtocol(DestHandle);
201 DestVolumeInfo = NULL;
202 DestVolumeInfoSize = 0;
203 Status = DestVolumeFP->GetInfo(
204 DestVolumeFP,
205 &gEfiFileSystemInfoGuid,
206 &DestVolumeInfoSize,
207 DestVolumeInfo
208 );
209
210 if (Status == EFI_BUFFER_TOO_SMALL) {
211 DestVolumeInfo = AllocateZeroPool(DestVolumeInfoSize);
212 Status = DestVolumeFP->GetInfo(
213 DestVolumeFP,
214 &gEfiFileSystemInfoGuid,
215 &DestVolumeInfoSize,
216 DestVolumeInfo
217 );
218 }
219
220 //
221 //check if enough space available on destination drive to complete copy
222 //
223 if (DestVolumeInfo!= NULL && (DestVolumeInfo->FreeSpace < SourceFileSize)) {
224 //
225 //not enough space on destination directory to copy file
226 //
227 SHELL_FREE_NON_NULL(DestVolumeInfo);
228 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_CPY_FAIL), gShellLevel2HiiHandle);
229 return(SHELL_VOLUME_FULL);
230 } else {
231 //
232 // copy data between files
233 //
234 Buffer = AllocateZeroPool(ReadSize);
235 ASSERT(Buffer != NULL);
236 while (ReadSize == PcdGet16(PcdShellFileOperationSize) && !EFI_ERROR(Status)) {
237 Status = ShellReadFile(SourceHandle, &ReadSize, Buffer);
238 Status = ShellWriteFile(DestHandle, &ReadSize, Buffer);
239 }
240 }
241 SHELL_FREE_NON_NULL(DestVolumeInfo);
242 }
243
244 //
245 // close files
246 //
247 if (DestHandle != NULL) {
248 ShellCloseFile(&DestHandle);
249 DestHandle = NULL;
250 }
251 if (SourceHandle != NULL) {
252 ShellCloseFile(&SourceHandle);
253 SourceHandle = NULL;
254 }
255
256 //
257 // return
258 //
259 return ShellStatus;
260 }
261
262 /**
263 function to take a list of files to copy and a destination location and do
264 the verification and copying of those files to that location. This function
265 will report any errors to the user and halt.
266
267 The key is to have this function called ONLY once. this allows for the parameter
268 verification to happen correctly.
269
270 @param[in] FileList A LIST_ENTRY* based list of files to move.
271 @param[in] DestDir The destination location.
272 @param[in] SilentMode TRUE to eliminate screen output.
273 @param[in] RecursiveMode TRUE to copy directories.
274 @param[in] Resp The response to the overwrite query (if always).
275
276 @retval SHELL_SUCCESS the files were all moved.
277 @retval SHELL_INVALID_PARAMETER a parameter was invalid
278 @retval SHELL_SECURITY_VIOLATION a security violation ocurred
279 @retval SHELL_WRITE_PROTECTED the destination was write protected
280 @retval SHELL_OUT_OF_RESOURCES a memory allocation failed
281 **/
282 SHELL_STATUS
283 EFIAPI
284 ValidateAndCopyFiles(
285 IN CONST EFI_SHELL_FILE_INFO *FileList,
286 IN CONST CHAR16 *DestDir,
287 IN BOOLEAN SilentMode,
288 IN BOOLEAN RecursiveMode,
289 IN VOID **Resp
290 )
291 {
292 CHAR16 *HiiOutput;
293 CHAR16 *HiiResultOk;
294 CONST EFI_SHELL_FILE_INFO *Node;
295 SHELL_STATUS ShellStatus;
296 CHAR16 *DestPath;
297 VOID *Response;
298 UINTN PathLen;
299 CONST CHAR16 *Cwd;
300 CONST CHAR16 *TempLocation;
301 UINTN NewSize;
302
303 if (Resp == NULL) {
304 Response = NULL;
305 } else {
306 Response = *Resp;
307 }
308
309 DestPath = NULL;
310 ShellStatus = SHELL_SUCCESS;
311 PathLen = 0;
312 Cwd = ShellGetCurrentDir(NULL);
313
314 ASSERT(FileList != NULL);
315 ASSERT(DestDir != NULL);
316
317 //
318 // We already verified that this was present.
319 //
320 ASSERT(Cwd != NULL);
321
322 //
323 // If we are trying to copy multiple files... make sure we got a directory for the target...
324 //
325 if (EFI_ERROR(ShellIsDirectory(DestDir)) && FileList->Link.ForwardLink != FileList->Link.BackLink) {
326 //
327 // Error for destination not a directory
328 //
329 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NOT_DIR), gShellLevel2HiiHandle, DestDir);
330 return (SHELL_INVALID_PARAMETER);
331 }
332 for (Node = (EFI_SHELL_FILE_INFO *)GetFirstNode(&FileList->Link)
333 ; !IsNull(&FileList->Link, &Node->Link)
334 ; Node = (EFI_SHELL_FILE_INFO *)GetNextNode(&FileList->Link, &Node->Link)
335 ){
336 //
337 // skip the directory traversing stuff...
338 //
339 if (StrCmp(Node->FileName, L".") == 0 || StrCmp(Node->FileName, L"..") == 0) {
340 continue;
341 }
342
343 NewSize = StrSize(DestDir);
344 NewSize += StrSize(Node->FullName);
345 NewSize += StrSize(Cwd);
346 if (NewSize > PathLen) {
347 PathLen = NewSize;
348 }
349
350 //
351 // Make sure got -r if required
352 //
353 if (!RecursiveMode && !EFI_ERROR(ShellIsDirectory(Node->FullName))) {
354 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CP_DIR_REQ), gShellLevel2HiiHandle);
355 return (SHELL_INVALID_PARAMETER);
356 }
357
358 //
359 // make sure got dest as dir if needed
360 //
361 if (!EFI_ERROR(ShellIsDirectory(Node->FullName)) && EFI_ERROR(ShellIsDirectory(DestDir))) {
362 //
363 // Error for destination not a directory
364 //
365 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NOT_DIR), gShellLevel2HiiHandle, DestDir);
366 return (SHELL_INVALID_PARAMETER);
367 }
368 }
369
370 HiiOutput = HiiGetString (gShellLevel2HiiHandle, STRING_TOKEN (STR_CP_OUTPUT), NULL);
371 HiiResultOk = HiiGetString (gShellLevel2HiiHandle, STRING_TOKEN (STR_GEN_RES_OK), NULL);
372 DestPath = AllocateZeroPool(PathLen);
373
374 if (DestPath == NULL || HiiOutput == NULL || HiiResultOk == NULL) {
375 SHELL_FREE_NON_NULL(DestPath);
376 SHELL_FREE_NON_NULL(HiiOutput);
377 SHELL_FREE_NON_NULL(HiiResultOk);
378 return (SHELL_OUT_OF_RESOURCES);
379 }
380
381 //
382 // Go through the list of files to copy...
383 //
384 for (Node = (EFI_SHELL_FILE_INFO *)GetFirstNode(&FileList->Link)
385 ; !IsNull(&FileList->Link, &Node->Link)
386 ; Node = (EFI_SHELL_FILE_INFO *)GetNextNode(&FileList->Link, &Node->Link)
387 ){
388 if (ShellGetExecutionBreakFlag()) {
389 break;
390 }
391 ASSERT(Node->FileName != NULL);
392 ASSERT(Node->FullName != NULL);
393
394 //
395 // skip the directory traversing stuff...
396 //
397 if (StrCmp(Node->FileName, L".") == 0 || StrCmp(Node->FileName, L"..") == 0) {
398 continue;
399 }
400
401 if (FileList->Link.ForwardLink == FileList->Link.BackLink // 1 item
402 && EFI_ERROR(ShellIsDirectory(DestDir)) // not an existing directory
403 ) {
404 if (StrStr(DestDir, L":") == NULL) {
405 //
406 // simple copy of a single file
407 //
408 StrCpy(DestPath, Cwd);
409 if (DestPath[StrLen(DestPath)-1] != L'\\' && DestDir[0] != L'\\') {
410 StrCat(DestPath, L"\\");
411 } else if (DestPath[StrLen(DestPath)-1] == L'\\' && DestDir[0] == L'\\') {
412 ((CHAR16*)DestPath)[StrLen(DestPath)-1] = CHAR_NULL;
413 }
414 StrCat(DestPath, DestDir);
415 } else {
416 StrCpy(DestPath, DestDir);
417 }
418 } else {
419 //
420 // we have multiple files or a directory in the DestDir
421 //
422
423 //
424 // Check for leading slash
425 //
426 if (DestDir[0] == L'\\') {
427 //
428 // Copy to the root of CWD
429 //
430 StrCpy(DestPath, Cwd);
431 while (PathRemoveLastItem(DestPath));
432 StrCat(DestPath, DestDir+1);
433 StrCat(DestPath, Node->FileName);
434 } else if (StrStr(DestDir, L":") == NULL) {
435 StrCpy(DestPath, Cwd);
436 if (DestPath[StrLen(DestPath)-1] != L'\\' && DestDir[0] != L'\\') {
437 StrCat(DestPath, L"\\");
438 } else if (DestPath[StrLen(DestPath)-1] == L'\\' && DestDir[0] == L'\\') {
439 ((CHAR16*)DestPath)[StrLen(DestPath)-1] = CHAR_NULL;
440 }
441 StrCat(DestPath, DestDir);
442 if (DestDir[StrLen(DestDir)-1] != L'\\' && Node->FileName[0] != L'\\') {
443 StrCat(DestPath, L"\\");
444 } else if (DestDir[StrLen(DestDir)-1] == L'\\' && Node->FileName[0] == L'\\') {
445 ((CHAR16*)DestPath)[StrLen(DestPath)-1] = CHAR_NULL;
446 }
447 StrCat(DestPath, Node->FileName);
448
449 } else {
450 StrCpy(DestPath, DestDir);
451 if (DestDir[StrLen(DestDir)-1] != L'\\' && Node->FileName[0] != L'\\') {
452 StrCat(DestPath, L"\\");
453 } else if (DestDir[StrLen(DestDir)-1] == L'\\' && Node->FileName[0] == L'\\') {
454 ((CHAR16*)DestDir)[StrLen(DestDir)-1] = CHAR_NULL;
455 }
456 StrCat(DestPath, Node->FileName);
457 }
458 }
459
460 //
461 // Make sure the path exists
462 //
463 if (EFI_ERROR(VerifyIntermediateDirectories(DestPath))) {
464 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CP_DIR_WNF), gShellLevel2HiiHandle);
465 ShellStatus = SHELL_DEVICE_ERROR;
466 break;
467 }
468
469 if ( !EFI_ERROR(ShellIsDirectory(Node->FullName))
470 && !EFI_ERROR(ShellIsDirectory(DestPath))
471 && StrniCmp(Node->FullName, DestPath, StrLen(DestPath)) == NULL
472 ){
473 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CP_SD_PARENT), gShellLevel2HiiHandle);
474 ShellStatus = SHELL_INVALID_PARAMETER;
475 break;
476 }
477 if (StringNoCaseCompare(&Node->FullName, &DestPath) == 0) {
478 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CP_SD_SAME), gShellLevel2HiiHandle);
479 ShellStatus = SHELL_INVALID_PARAMETER;
480 break;
481 }
482
483 if ((TempLocation = StrniCmp(Node->FullName, DestPath, StrLen(Node->FullName))) == 0
484 && (DestPath[StrLen(Node->FullName)] == CHAR_NULL || DestPath[StrLen(Node->FullName)] == L'\\')
485 ) {
486 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CP_SD_SAME), gShellLevel2HiiHandle);
487 ShellStatus = SHELL_INVALID_PARAMETER;
488 break;
489 }
490
491 PathCleanUpDirectories(DestPath);
492
493 ShellPrintEx(-1, -1, HiiOutput, Node->FullName, DestPath);
494
495 //
496 // copy single file...
497 //
498 ShellStatus = CopySingleFile(Node->FullName, DestPath, &Response, SilentMode);
499 if (ShellStatus != SHELL_SUCCESS) {
500 break;
501 }
502 }
503 if (ShellStatus == SHELL_SUCCESS && Resp == NULL) {
504 ShellPrintEx(-1, -1, L"%s", HiiResultOk);
505 }
506
507 SHELL_FREE_NON_NULL(DestPath);
508 SHELL_FREE_NON_NULL(HiiOutput);
509 SHELL_FREE_NON_NULL(HiiResultOk);
510 if (Resp == NULL) {
511 SHELL_FREE_NON_NULL(Response);
512 }
513
514 return (ShellStatus);
515
516 }
517
518 /**
519 Validate and if successful copy all the files from the list into
520 destination directory.
521
522 @param[in] FileList The list of files to copy.
523 @param[in] DestDir The directory to copy files to.
524 @param[in] SilentMode TRUE to eliminate screen output.
525 @param[in] RecursiveMode TRUE to copy directories.
526
527 @retval SHELL_INVALID_PARAMETER A parameter was invalid.
528 @retval SHELL_SUCCESS The operation was successful.
529 **/
530 SHELL_STATUS
531 EFIAPI
532 ProcessValidateAndCopyFiles(
533 IN EFI_SHELL_FILE_INFO *FileList,
534 IN CONST CHAR16 *DestDir,
535 IN BOOLEAN SilentMode,
536 IN BOOLEAN RecursiveMode
537 )
538 {
539 SHELL_STATUS ShellStatus;
540 EFI_SHELL_FILE_INFO *List;
541 EFI_FILE_INFO *FileInfo;
542
543 List = NULL;
544
545 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