2 Dir for EBL (Embedded Boot Loader)
4 Copyright (c) 2007, Intel Corporation. All rights reserved.<BR>
5 Portions copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
8 This program and the accompanying materials
9 are licensed and made available under the terms and conditions of the BSD License
10 which accompanies this distribution. The full text of the license may be found at
11 http://opensource.org/licenses/bsd-license.php
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
14 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 Module Name: CmdTemplate.c
18 Search/Replace Dir with the name of your new command
25 GLOBAL_REMOVE_IF_UNREFERENCED CHAR8
*gFvFileType
[] = {
42 Perform a dir on a device. The device must support Simple File System Protocol
46 Argv[1] - Device Name:path. Path is optional
47 Argv[2] - Optional filename to match on. A leading * means match substring
48 Argv[3] - Optional FV file type
50 dir fs1:\efi ; perform a dir on fs1: device in the efi directory
51 dir fs1:\efi *.efi; perform a dir on fs1: device in the efi directory but
52 only print out files that contain the string *.efi
53 dir fv1:\ ; perform a dir on fv1: device in the efi directory
54 NOTE: fv devices do not contain subdirs
55 dir fv1:\ * PEIM ; will match all files of type PEIM
57 @param Argc Number of command arguments in Argv
58 @param Argv Array of strings that represent the parsed command line.
59 Argv[0] is the command name
72 EFI_FILE_INFO
*DirInfo
;
75 CHAR16
*MatchSubString
;
76 EFI_STATUS GetNextFileStatus
;
78 EFI_FV_FILETYPE SearchType
;
80 EFI_FV_FILE_ATTRIBUTES Attributes
;
83 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
84 UINT32 AuthenticationStatus
;
87 EFI_FV_FILETYPE Index
;
90 CHAR16 UnicodeFileName
[MAX_CMD_LINE
];
105 File
= EfiOpen (Path
, EFI_FILE_MODE_READ
, 0);
110 if (File
->Type
== EfiOpenFirmwareVolume
) {
113 SearchType
= EFI_FV_FILETYPE_ALL
;
114 UnicodeFileName
[0] = '\0';
115 MatchSubString
= &UnicodeFileName
[0];
117 AsciiStrToUnicodeStr (Argv
[2], UnicodeFileName
);
118 if (UnicodeFileName
[0] == '*') {
119 // Handle *Name substring matching
120 MatchSubString
= &UnicodeFileName
[1];
123 // Handle file type matchs
125 // match a specific file type, always last argument
126 Length
= AsciiStrLen (Argv
[3]);
127 for (Index
= 1, BestMatchCount
= 0; Index
< sizeof (gFvFileType
)/sizeof (CHAR8
*); Index
++) {
128 if (AsciiStriCmp (gFvFileType
[Index
], Argv
[3]) == 0) {
134 if (AsciiStrniCmp (Argv
[3], gFvFileType
[Index
], Length
) == 0) {
135 // partial match, so keep looking to make sure there is only one partial match
141 if (BestMatchCount
> 1) {
142 SearchType
= EFI_FV_FILETYPE_ALL
;
153 GetNextFileStatus
= Fv
->GetNextFile (
161 if (!EFI_ERROR (GetNextFileStatus
)) {
163 // Calculate size of entire file
166 Status
= Fv
->ReadFile (
173 &AuthenticationStatus
175 if (!((Status
== EFI_BUFFER_TOO_SMALL
) || !EFI_ERROR (Status
))) {
176 // EFI_SUCCESS or EFI_BUFFER_TOO_SMALL mean size is valid
180 TypeStr
= (Type
<= EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE
) ? gFvFileType
[Type
] : "UNKNOWN";
182 // read the UI seciton to do a name match.
184 Status
= Fv
->ReadSection (
187 EFI_SECTION_USER_INTERFACE
,
191 &AuthenticationStatus
193 if (!EFI_ERROR (Status
)) {
194 if (StrStr (Section
, MatchSubString
) != NULL
) {
195 AsciiPrint ("%,9d %7a %g %s\n", Size
, TypeStr
, &NameGuid
, Section
);
196 if (EblAnyKeyToContinueQtoQuit (&CurrentRow
, FALSE
)) {
202 if (*MatchSubString
== '\0') {
203 AsciiPrint ("%,9d %7a %g\n", Size
, TypeStr
, &NameGuid
);
204 if (EblAnyKeyToContinueQtoQuit (&CurrentRow
, FALSE
)) {
210 } while (!EFI_ERROR (GetNextFileStatus
));
212 if (SearchType
== EFI_FV_FILETYPE_ALL
) {
213 AsciiPrint ("%,20d bytes in files %,d bytes free\n", TotalSize
, File
->FvSize
- File
->FvHeaderSize
- TotalSize
);
217 } else if ((File
->Type
== EfiOpenFileSystem
) || (File
->Type
== EfiOpenBlockIo
)) {
218 // Simple File System DIR
220 if (File
->FsFileInfo
== NULL
) {
224 if (!(File
->FsFileInfo
->Attribute
& EFI_FILE_DIRECTORY
)) {
228 // Handle *Name substring matching
229 MatchSubString
= NULL
;
230 UnicodeFileName
[0] = '\0';
232 AsciiStrToUnicodeStr (Argv
[2], UnicodeFileName
);
233 if (UnicodeFileName
[0] == '*') {
234 MatchSubString
= &UnicodeFileName
[1];
238 File
->FsFileHandle
->SetPosition (File
->FsFileHandle
, 0);
239 for (CurrentRow
= 0;;) {
240 // First read gets the size
243 Status
= File
->FsFileHandle
->Read (File
->FsFileHandle
, &ReadSize
, DirInfo
);
244 if (Status
== EFI_BUFFER_TOO_SMALL
) {
245 // Allocate the buffer for the real read
246 DirInfo
= AllocatePool (ReadSize
);
247 if (DirInfo
== NULL
) {
252 Status
= File
->FsFileHandle
->Read (File
->FsFileHandle
, &ReadSize
, DirInfo
);
253 if ((EFI_ERROR (Status
)) || (ReadSize
== 0)) {
260 if (MatchSubString
!= NULL
) {
261 if (StrStr (&DirInfo
->FileName
[0], MatchSubString
) == NULL
) {
262 // does not match *name argument, so skip
265 } else if (UnicodeFileName
[0] != '\0') {
266 // is not an exact match for name argument, so skip
267 if (StrCmp (&DirInfo
->FileName
[0], UnicodeFileName
) != 0) {
272 if (DirInfo
->Attribute
& EFI_FILE_DIRECTORY
) {
273 AsciiPrint (" <DIR> %s\n", &DirInfo
->FileName
[0]);
275 AsciiPrint ("%,14ld %s\n", DirInfo
->FileSize
, &DirInfo
->FileName
[0]);
278 if (EblAnyKeyToContinueQtoQuit (&CurrentRow
, FALSE
)) {
286 if (DirInfo
!= NULL
) {
297 Change the Current Working Directory
300 Argv[1] - Device Name:path. Path is optional
302 @param Argc Number of command arguments in Argv
303 @param Argv Array of strings that represent the parsed command line.
304 Argv[0] is the command name
319 return EfiSetCwd (Argv
[1]);
324 GLOBAL_REMOVE_IF_UNREFERENCED
const EBL_COMMAND_TABLE mCmdDirTemplate
[] =
328 " dirdev [*match]; directory listing of dirdev. opt match a substring",
334 " device - set the current working directory",
342 Initialize the commands in this in this file
345 EblInitializeDirCmd (
349 if (FeaturePcdGet (PcdEmbeddedDirCmd
)) {
350 EblAddCommands (mCmdDirTemplate
, sizeof (mCmdDirTemplate
)/sizeof (EBL_COMMAND_TABLE
));