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