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>
6 (C) Copyright 2015 Hewlett Packard Enterprise Development LP<BR>
9 This program and the accompanying materials
10 are licensed and made available under the terms and conditions of the BSD License
11 which accompanies this distribution. The full text of the license may be found at
12 http://opensource.org/licenses/bsd-license.php
14 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
15 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 Module Name: CmdTemplate.c
19 Search/Replace Dir with the name of your new command
26 GLOBAL_REMOVE_IF_UNREFERENCED CHAR8
*gFvFileType
[] = {
43 Perform a dir on a device. The device must support Simple File System Protocol
47 Argv[1] - Device Name:path. Path is optional
48 Argv[2] - Optional filename to match on. A leading * means match substring
49 Argv[3] - Optional FV file type
51 dir fs1:\efi ; perform a dir on fs1: device in the efi directory
52 dir fs1:\efi *.efi; perform a dir on fs1: device in the efi directory but
53 only print out files that contain the string *.efi
54 dir fv1:\ ; perform a dir on fv1: device in the efi directory
55 NOTE: fv devices do not contain subdirs
56 dir fv1:\ * PEIM ; will match all files of type PEIM
58 @param Argc Number of command arguments in Argv
59 @param Argv Array of strings that represent the parsed command line.
60 Argv[0] is the command name
74 EFI_FILE_INFO
*DirInfo
;
77 CHAR16
*MatchSubString
;
78 EFI_STATUS GetNextFileStatus
;
80 EFI_FV_FILETYPE SearchType
;
82 EFI_FV_FILE_ATTRIBUTES Attributes
;
85 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
86 UINT32 AuthenticationStatus
;
89 EFI_FV_FILETYPE Index
;
92 CHAR16 UnicodeFileName
[MAX_CMD_LINE
];
107 File
= EfiOpen (Path
, EFI_FILE_MODE_READ
, 0);
112 if (File
->Type
== EfiOpenFirmwareVolume
) {
115 SearchType
= EFI_FV_FILETYPE_ALL
;
116 UnicodeFileName
[0] = '\0';
117 MatchSubString
= &UnicodeFileName
[0];
119 AsciiStrToUnicodeStr (Argv
[2], UnicodeFileName
);
120 if (UnicodeFileName
[0] == '*') {
121 // Handle *Name substring matching
122 MatchSubString
= &UnicodeFileName
[1];
125 // Handle file type matchs
127 // match a specific file type, always last argument
128 Length
= AsciiStrLen (Argv
[3]);
129 for (Index
= 1, BestMatchCount
= 0; Index
< sizeof (gFvFileType
)/sizeof (CHAR8
*); Index
++) {
130 if (AsciiStriCmp (gFvFileType
[Index
], Argv
[3]) == 0) {
136 if (AsciiStrniCmp (Argv
[3], gFvFileType
[Index
], Length
) == 0) {
137 // partial match, so keep looking to make sure there is only one partial match
143 if (BestMatchCount
> 1) {
144 SearchType
= EFI_FV_FILETYPE_ALL
;
155 GetNextFileStatus
= Fv
->GetNextFile (
163 if (!EFI_ERROR (GetNextFileStatus
)) {
165 // Calculate size of entire file
168 Status
= Fv
->ReadFile (
175 &AuthenticationStatus
177 if (!((Status
== EFI_BUFFER_TOO_SMALL
) || !EFI_ERROR (Status
))) {
178 // EFI_SUCCESS or EFI_BUFFER_TOO_SMALL mean size is valid
182 TypeStr
= (Type
<= EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE
) ? gFvFileType
[Type
] : "UNKNOWN";
184 // read the UI seciton to do a name match.
186 Status
= Fv
->ReadSection (
189 EFI_SECTION_USER_INTERFACE
,
193 &AuthenticationStatus
195 if (!EFI_ERROR (Status
)) {
196 if (StrStr (Section
, MatchSubString
) != NULL
) {
197 AsciiPrint ("%,9d %7a %g %s\n", Size
, TypeStr
, &NameGuid
, Section
);
198 if (EblAnyKeyToContinueQtoQuit (&CurrentRow
, FALSE
)) {
204 if (*MatchSubString
== '\0') {
205 AsciiPrint ("%,9d %7a %g\n", Size
, TypeStr
, &NameGuid
);
206 if (EblAnyKeyToContinueQtoQuit (&CurrentRow
, FALSE
)) {
212 } while (!EFI_ERROR (GetNextFileStatus
));
214 if (SearchType
== EFI_FV_FILETYPE_ALL
) {
215 AsciiPrint ("%,20d bytes in files %,d bytes free\n", TotalSize
, File
->FvSize
- File
->FvHeaderSize
- TotalSize
);
219 } else if ((File
->Type
== EfiOpenFileSystem
) || (File
->Type
== EfiOpenBlockIo
)) {
220 // Simple File System DIR
222 if (File
->FsFileInfo
== NULL
) {
226 if (!(File
->FsFileInfo
->Attribute
& EFI_FILE_DIRECTORY
)) {
230 // Handle *Name substring matching
231 MatchSubString
= NULL
;
232 UnicodeFileName
[0] = '\0';
234 AsciiStrToUnicodeStr (Argv
[2], UnicodeFileName
);
235 if (UnicodeFileName
[0] == '*') {
236 MatchSubString
= &UnicodeFileName
[1];
240 File
->FsFileHandle
->SetPosition (File
->FsFileHandle
, 0);
241 for (CurrentRow
= 0;;) {
242 // First read gets the size
245 Status
= File
->FsFileHandle
->Read (File
->FsFileHandle
, &ReadSize
, DirInfo
);
246 if (Status
== EFI_BUFFER_TOO_SMALL
) {
247 // Allocate the buffer for the real read
248 DirInfo
= AllocatePool (ReadSize
);
249 if (DirInfo
== NULL
) {
254 Status
= File
->FsFileHandle
->Read (File
->FsFileHandle
, &ReadSize
, DirInfo
);
255 if ((EFI_ERROR (Status
)) || (ReadSize
== 0)) {
262 if (MatchSubString
!= NULL
) {
263 if (StrStr (&DirInfo
->FileName
[0], MatchSubString
) == NULL
) {
264 // does not match *name argument, so skip
267 } else if (UnicodeFileName
[0] != '\0') {
268 // is not an exact match for name argument, so skip
269 if (StrCmp (&DirInfo
->FileName
[0], UnicodeFileName
) != 0) {
274 if (DirInfo
->Attribute
& EFI_FILE_DIRECTORY
) {
275 AsciiPrint (" <DIR> %s\n", &DirInfo
->FileName
[0]);
277 AsciiPrint ("%,14ld %s\n", DirInfo
->FileSize
, &DirInfo
->FileName
[0]);
280 if (EblAnyKeyToContinueQtoQuit (&CurrentRow
, FALSE
)) {
288 if (DirInfo
!= NULL
) {
299 Change the Current Working Directory
302 Argv[1] - Device Name:path. Path is optional
304 @param Argc Number of command arguments in Argv
305 @param Argv Array of strings that represent the parsed command line.
306 Argv[0] is the command name
322 return EfiSetCwd (Argv
[1]);
327 GLOBAL_REMOVE_IF_UNREFERENCED
const EBL_COMMAND_TABLE mCmdDirTemplate
[] =
331 " dirdev [*match]; directory listing of dirdev. opt match a substring",
337 " device - set the current working directory",
345 Initialize the commands in this in this file
348 EblInitializeDirCmd (
352 if (FeaturePcdGet (PcdEmbeddedDirCmd
)) {
353 EblAddCommands (mCmdDirTemplate
, sizeof (mCmdDirTemplate
)/sizeof (EBL_COMMAND_TABLE
));