2 Main file for ls shell level 2 function.
4 Copyright (c) 2009 - 2012, 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
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.
15 #include "UefiShellLevel2CommandsLib.h"
16 #include <Guid/FileSystemInfo.h>
19 print out the list of files and directories from the LS command
21 @param[in] Rec TRUE to automatically recurse into each found directory
22 FALSE to only list the specified directory.
23 @param[in] Attribs List of required Attribute for display.
24 If 0 then all non-system and non-hidden files will be printed.
25 @param[in] Sfo TRUE to use Standard Format Output, FALSE otherwise
26 @param[in] Path String with starting path.
27 @param[in] First TRUE for the original and FALSE for any recursion spawned instances.
28 @param[in] Count The count of bits enabled in Attribs.
29 @param[in] TimeZone The current time zone offset.
31 @retval SHELL_SUCCESS the printing was sucessful.
37 IN CONST UINT64 Attribs
,
39 IN CONST CHAR16
*Path
,
40 IN CONST BOOLEAN First
,
42 IN CONST INT16 TimeZone
46 EFI_SHELL_FILE_INFO
*ListHead
;
47 EFI_SHELL_FILE_INFO
*Node
;
48 SHELL_STATUS ShellStatus
;
52 CHAR16
*DirectoryName
;
54 EFI_FILE_SYSTEM_INFO
*SysInfo
;
56 SHELL_FILE_HANDLE ShellFileHandle
;
57 CHAR16
*CorrectedPath
;
58 EFI_FILE_PROTOCOL
*EfiFpHandle
;
64 ShellStatus
= SHELL_SUCCESS
;
68 CorrectedPath
= StrnCatGrow(&CorrectedPath
, NULL
, Path
, 0);
69 if (CorrectedPath
== NULL
) {
70 return (SHELL_OUT_OF_RESOURCES
);
73 PathCleanUpDirectories(CorrectedPath
);
75 Status
= ShellOpenFileMetaArg((CHAR16
*)CorrectedPath
, EFI_FILE_MODE_READ
, &ListHead
);
76 if (EFI_ERROR(Status
)) {
77 SHELL_FREE_NON_NULL(CorrectedPath
);
78 if(Status
== EFI_NOT_FOUND
){
79 return (SHELL_NOT_FOUND
);
81 return (SHELL_DEVICE_ERROR
);
83 if (ListHead
== NULL
|| IsListEmpty(&ListHead
->Link
)) {
84 SHELL_FREE_NON_NULL(CorrectedPath
);
86 // On the first one only we expect to find something...
87 // do we find the . and .. directories otherwise?
90 return (SHELL_NOT_FOUND
);
92 return (SHELL_SUCCESS
);
97 // Get the first valid handle (directories)
99 for ( Node
= (EFI_SHELL_FILE_INFO
*)GetFirstNode(&ListHead
->Link
)
100 ; !IsNull(&ListHead
->Link
, &Node
->Link
) && Node
->Handle
== NULL
101 ; Node
= (EFI_SHELL_FILE_INFO
*)GetNextNode(&ListHead
->Link
, &Node
->Link
)
104 if (Node
->Handle
== NULL
) {
105 DirectoryName
= GetFullyQualifiedPath(((EFI_SHELL_FILE_INFO
*)GetFirstNode(&ListHead
->Link
))->FullName
);
108 // We need to open something up to get system information
110 Status
= gEfiShellProtocol
->OpenFileByName(
115 ASSERT_EFI_ERROR(Status
);
116 FreePool(DirectoryName
);
119 // Get the Volume Info from ShellFileHandle
123 EfiFpHandle
= ConvertShellHandleToEfiFileProtocol(ShellFileHandle
);
124 Status
= EfiFpHandle
->GetInfo(
126 &gEfiFileSystemInfoGuid
,
130 if (Status
== EFI_BUFFER_TOO_SMALL
) {
131 SysInfo
= AllocateZeroPool(SysInfoSize
);
132 Status
= EfiFpHandle
->GetInfo(
134 &gEfiFileSystemInfoGuid
,
139 ASSERT_EFI_ERROR(Status
);
141 gEfiShellProtocol
->CloseFile(ShellFileHandle
);
144 // Get the Volume Info from Node->Handle
148 EfiFpHandle
= ConvertShellHandleToEfiFileProtocol(Node
->Handle
);
149 Status
= EfiFpHandle
->GetInfo(
151 &gEfiFileSystemInfoGuid
,
155 if (Status
== EFI_BUFFER_TOO_SMALL
) {
156 SysInfo
= AllocateZeroPool(SysInfoSize
);
157 Status
= EfiFpHandle
->GetInfo(
159 &gEfiFileSystemInfoGuid
,
164 ASSERT_EFI_ERROR(Status
);
171 STRING_TOKEN (STR_GEN_SFO_HEADER
),
172 gShellLevel2HiiHandle
,
175 // print VolumeInfo table
177 ASSERT(SysInfo
!= NULL
);
180 gST
->ConOut
->Mode
->CursorRow
,
182 STRING_TOKEN (STR_LS_SFO_VOLINFO
),
183 gShellLevel2HiiHandle
,
184 SysInfo
->VolumeLabel
,
186 SysInfo
->ReadOnly
?L
"TRUE":L
"FALSE",
190 if (SysInfo
!= NULL
) {
197 // get directory name from path...
199 DirectoryName
= GetFullyQualifiedPath(CorrectedPath
);
206 gST
->ConOut
->Mode
->CursorRow
,
208 STRING_TOKEN (STR_LS_HEADER_LINE1
),
209 gShellLevel2HiiHandle
,
212 FreePool(DirectoryName
);
214 for ( Node
= (EFI_SHELL_FILE_INFO
*)GetFirstNode(&ListHead
->Link
)
215 ; !IsNull(&ListHead
->Link
, &Node
->Link
)
216 ; Node
= (EFI_SHELL_FILE_INFO
*)GetNextNode(&ListHead
->Link
, &Node
->Link
)
218 ASSERT(Node
!= NULL
);
219 if (LongestPath
< StrSize(Node
->FullName
)) {
220 LongestPath
= StrSize(Node
->FullName
);
222 ASSERT(Node
->Info
!= NULL
);
223 ASSERT((Node
->Info
->Attribute
& EFI_FILE_VALID_ATTR
) == Node
->Info
->Attribute
);
226 // NOT system & NOT hidden
228 if ( (Node
->Info
->Attribute
& EFI_FILE_SYSTEM
)
229 || (Node
->Info
->Attribute
& EFI_FILE_HIDDEN
)
233 } else if (Attribs
!= EFI_FILE_VALID_ATTR
) {
236 // the bit must match
238 if ( (Node
->Info
->Attribute
& Attribs
) != Attribs
) {
243 // exact match on all bits
245 if ( (Node
->Info
->Attribute
|EFI_FILE_ARCHIVE
) != (Attribs
|EFI_FILE_ARCHIVE
)) {
253 // Print the FileInfo Table
257 gST
->ConOut
->Mode
->CursorRow
,
259 STRING_TOKEN (STR_LS_SFO_FILEINFO
),
260 gShellLevel2HiiHandle
,
262 Node
->Info
->FileSize
,
263 Node
->Info
->PhysicalSize
,
264 (Node
->Info
->Attribute
& EFI_FILE_ARCHIVE
) != 0?L
"a":L
"",
265 (Node
->Info
->Attribute
& EFI_FILE_DIRECTORY
) != 0?L
"d":L
"",
266 (Node
->Info
->Attribute
& EFI_FILE_HIDDEN
) != 0?L
"h":L
"",
267 (Node
->Info
->Attribute
& EFI_FILE_READ_ONLY
) != 0?L
"r":L
"",
268 (Node
->Info
->Attribute
& EFI_FILE_SYSTEM
) != 0?L
"s":L
"",
269 Node
->Info
->CreateTime
.Hour
,
270 Node
->Info
->CreateTime
.Minute
,
271 Node
->Info
->CreateTime
.Second
,
272 Node
->Info
->CreateTime
.Day
,
273 Node
->Info
->CreateTime
.Month
,
274 Node
->Info
->CreateTime
.Year
,
275 Node
->Info
->LastAccessTime
.Hour
,
276 Node
->Info
->LastAccessTime
.Minute
,
277 Node
->Info
->LastAccessTime
.Second
,
278 Node
->Info
->LastAccessTime
.Day
,
279 Node
->Info
->LastAccessTime
.Month
,
280 Node
->Info
->LastAccessTime
.Year
,
281 Node
->Info
->ModificationTime
.Hour
,
282 Node
->Info
->ModificationTime
.Minute
,
283 Node
->Info
->ModificationTime
.Second
,
284 Node
->Info
->ModificationTime
.Day
,
285 Node
->Info
->ModificationTime
.Month
,
286 Node
->Info
->ModificationTime
.Year
290 // print this one out...
291 // first print the universal start, next print the type specific name format, last print the CRLF
297 STRING_TOKEN (STR_LS_LINE_START_ALL
),
298 gShellLevel2HiiHandle
,
299 &Node
->Info
->ModificationTime
,
300 (Node
->Info
->Attribute
& EFI_FILE_DIRECTORY
) != 0?L
"<DIR>":L
"",
301 (Node
->Info
->Attribute
& EFI_FILE_READ_ONLY
) != 0?L
'r':L
' ',
304 if (Node
->Info
->Attribute
& EFI_FILE_DIRECTORY
) {
310 STRING_TOKEN (STR_LS_LINE_END_DIR
),
311 gShellLevel2HiiHandle
,
316 FileSize
+= Node
->Info
->FileSize
;
317 if ( (gUnicodeCollation
->StriColl(gUnicodeCollation
, (CHAR16
*)L
".nsh", (CHAR16
*)&(Node
->FileName
[StrLen (Node
->FileName
) - 4])) == 0)
318 || (gUnicodeCollation
->StriColl(gUnicodeCollation
, (CHAR16
*)L
".efi", (CHAR16
*)&(Node
->FileName
[StrLen (Node
->FileName
) - 4])) == 0)
324 STRING_TOKEN (STR_LS_LINE_END_EXE
),
325 gShellLevel2HiiHandle
,
333 STRING_TOKEN (STR_LS_LINE_END_FILE
),
334 gShellLevel2HiiHandle
,
350 STRING_TOKEN (STR_LS_FOOTER_LINE
),
351 gShellLevel2HiiHandle
,
359 DirectoryName
= AllocateZeroPool(LongestPath
+ 2*sizeof(CHAR16
));
360 if (DirectoryName
== NULL
) {
361 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_NO_MEM
), gShellLevel2HiiHandle
);
362 ShellStatus
= SHELL_OUT_OF_RESOURCES
;
364 for ( Node
= (EFI_SHELL_FILE_INFO
*)GetFirstNode(&ListHead
->Link
)
365 ; !IsNull(&ListHead
->Link
, &Node
->Link
) && ShellStatus
== SHELL_SUCCESS
366 ; Node
= (EFI_SHELL_FILE_INFO
*)GetNextNode(&ListHead
->Link
, &Node
->Link
)
368 if (ShellGetExecutionBreakFlag ()) {
369 ShellStatus
= SHELL_ABORTED
;
374 // recurse on any directory except the traversing ones...
376 if (((Node
->Info
->Attribute
& EFI_FILE_DIRECTORY
) == EFI_FILE_DIRECTORY
)
377 && StrCmp(Node
->FileName
, L
".") != 0
378 && StrCmp(Node
->FileName
, L
"..") != 0
380 StrCpy(DirectoryName
, Node
->FullName
);
381 StrCat(DirectoryName
, L
"\\*");
382 ShellStatus
= PrintLsOutput(
392 FreePool(DirectoryName
);
396 FreePool(CorrectedPath
);
397 ShellCloseFileMetaArg(&ListHead
);
399 return (ShellStatus
);
402 STATIC CONST SHELL_PARAM_ITEM LsParamList
[] = {
410 Function for 'ls' command.
412 @param[in] ImageHandle Handle to the Image (NULL if Internal).
413 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
418 IN EFI_HANDLE ImageHandle
,
419 IN EFI_SYSTEM_TABLE
*SystemTable
424 CHAR16
*ProblemParam
;
425 CONST CHAR16
*Attribs
;
426 SHELL_STATUS ShellStatus
;
427 UINT64 RequiredAttributes
;
428 CONST CHAR16
*PathName
;
429 CONST CHAR16
*CurDir
;
440 ShellStatus
= SHELL_SUCCESS
;
441 RequiredAttributes
= 0;
447 // initialize the shell lib (we must be in non-auto-init...)
449 Status
= ShellInitialize();
450 ASSERT_EFI_ERROR(Status
);
453 // Fix local copies of the protocol pointers
455 Status
= CommandInit();
456 ASSERT_EFI_ERROR(Status
);
459 // parse the command line
461 Status
= ShellCommandLineParse (LsParamList
, &Package
, &ProblemParam
, TRUE
);
462 if (EFI_ERROR(Status
)) {
463 if (Status
== EFI_VOLUME_CORRUPTED
&& ProblemParam
!= NULL
) {
464 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM
), gShellLevel2HiiHandle
, ProblemParam
);
465 FreePool(ProblemParam
);
466 ShellStatus
= SHELL_INVALID_PARAMETER
;
474 if (ShellCommandLineGetFlag(Package
, L
"-?")) {
478 if (ShellCommandLineGetCount(Package
) > 2) {
479 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_MANY
), gShellLevel2HiiHandle
);
480 ShellStatus
= SHELL_INVALID_PARAMETER
;
485 if (ShellCommandLineGetFlag(Package
, L
"-a")) {
486 for ( Attribs
= ShellCommandLineGetValue(Package
, L
"-a")
487 ; Attribs
!= NULL
&& *Attribs
!= CHAR_NULL
&& ShellStatus
== SHELL_SUCCESS
493 RequiredAttributes
|= EFI_FILE_ARCHIVE
;
498 RequiredAttributes
|= EFI_FILE_SYSTEM
;
503 RequiredAttributes
|= EFI_FILE_HIDDEN
;
508 RequiredAttributes
|= EFI_FILE_READ_ONLY
;
513 RequiredAttributes
|= EFI_FILE_DIRECTORY
;
517 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_ATTRIBUTE
), gShellLevel2HiiHandle
, ShellCommandLineGetValue(Package
, L
"-a"));
518 ShellStatus
= SHELL_INVALID_PARAMETER
;
523 // if nothing is specified all are specified
525 if (RequiredAttributes
== 0) {
526 RequiredAttributes
= EFI_FILE_VALID_ATTR
;
529 if (ShellStatus
== SHELL_SUCCESS
) {
530 PathName
= ShellCommandLineGetRawValue(Package
, 1);
531 if (PathName
== NULL
) {
532 CurDir
= gEfiShellProtocol
->GetCurDir(NULL
);
533 if (CurDir
== NULL
) {
534 ShellStatus
= SHELL_NOT_FOUND
;
535 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_NO_CWD
), gShellLevel2HiiHandle
);
538 if (PathName
!= NULL
) {
539 if (StrStr(PathName
, L
":") == NULL
&& gEfiShellProtocol
->GetCurDir(NULL
) == NULL
) {
540 ShellStatus
= SHELL_NOT_FOUND
;
541 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_NO_CWD
), gShellLevel2HiiHandle
);
543 ASSERT((FullPath
== NULL
&& Size
== 0) || (FullPath
!= NULL
));
544 StrnCatGrow(&FullPath
, &Size
, PathName
, 0);
545 if (ShellIsDirectory(PathName
) == EFI_SUCCESS
) {
546 StrnCatGrow(&FullPath
, &Size
, L
"\\*", 0);
550 ASSERT(FullPath
== NULL
);
551 StrnCatGrow(&FullPath
, NULL
, L
"*", 0);
553 Status
= gRT
->GetTime(&TheTime
, NULL
);
554 if (EFI_ERROR(Status
)) {
555 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_UEFI_FUNC_WARN
), gShellLevel2HiiHandle
, L
"gRT->GetTime", Status
);
556 TheTime
.TimeZone
= EFI_UNSPECIFIED_TIMEZONE
;
559 SfoMode
= ShellCommandLineGetFlag(Package
, L
"-sfo");
560 if (ShellStatus
== SHELL_SUCCESS
) {
561 ShellStatus
= PrintLsOutput(
562 ShellCommandLineGetFlag(Package
, L
"-r"),
568 (INT16
)(TheTime
.TimeZone
==EFI_UNSPECIFIED_TIMEZONE
?0:TheTime
.TimeZone
)
570 if (ShellStatus
== SHELL_NOT_FOUND
) {
571 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_NO_FILES
), gShellLevel2HiiHandle
);
572 } else if (ShellStatus
== SHELL_INVALID_PARAMETER
) {
573 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PARAM_INV
), gShellLevel2HiiHandle
);
574 } else if (ShellStatus
== SHELL_ABORTED
) {
578 } else if (ShellStatus
!= SHELL_SUCCESS
) {
579 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PARAM_INV
), gShellLevel2HiiHandle
);
586 if (FullPath
!= NULL
) {
590 // free the command line package
592 ShellCommandLineFreeVarList (Package
);
594 return (ShellStatus
);