]> git.proxmox.com Git - mirror_edk2.git/blob - EmbeddedPkg/Ebl/Dir.c
ff1b557ed18a20e2d954e54dda9714f3b9adbd1e
[mirror_edk2.git] / EmbeddedPkg / Ebl / Dir.c
1 /** @file
2 Dir for EBL (Embedded Boot Loader)
3
4 Copyright (c) 2007, Intel Corporation<BR>
5 Portions copyright (c) 2008-2009, Apple Inc. All rights reserved.
6
7
8 All rights reserved. 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
12
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.
15
16 Module Name: CmdTemplate.c
17
18 Search/Replace Dir with the name of your new command
19
20 **/
21
22 #include "Ebl.h"
23
24
25 GLOBAL_REMOVE_IF_UNREFERENCED CHAR8 *gFvFileType[] = {
26 "All",
27 "Raw",
28 "Freeform",
29 "SEC",
30 "PeiCore",
31 "DxeCore",
32 "PEIM",
33 "Driver",
34 "Combo Driver",
35 "Application",
36 "NULL",
37 "FV"
38 };
39
40
41 /**
42 Perform a dir on a device. The device must support Simple File System Protocol
43 or the FV protocol.
44
45 Argv[0] - "dir"
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
49
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 contian subdirs
55 dir fv1:\ * PEIM ; will match all files of type PEIM
56
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 comamnd name
60
61 @return EFI_SUCCESS
62
63 **/
64 EFI_STATUS
65 EblDirCmd (
66 IN UINTN Argc,
67 IN CHAR8 **Argv
68 )
69 {
70 EFI_STATUS Status;
71 EFI_OPEN_FILE *File;
72 EFI_FILE_INFO *DirInfo;
73 UINTN ReadSize;
74 UINTN CurrentRow;
75 CHAR16 *MatchSubString;
76 EFI_STATUS GetNextFileStatus;
77 UINTN Key;
78 EFI_FV_FILETYPE SearchType;
79 EFI_FV_FILETYPE Type;
80 EFI_FV_FILE_ATTRIBUTES Attributes;
81 UINTN Size;
82 EFI_GUID NameGuid;
83 EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;
84 UINT32 AuthenticationStatus;
85 VOID *Section;
86 UINTN SectionSize;
87 EFI_FV_FILETYPE Index;
88 UINTN Length;
89 UINTN BestMatchCount;
90 CHAR16 UnicodeFileName[MAX_CMD_LINE];
91 CHAR8 *Path;
92
93
94 if (Argc <= 1) {
95 Path = EfiGetCwd ();
96 if (Path == NULL) {
97 return EFI_SUCCESS;
98 }
99 } else {
100 Path = Argv[1];
101 }
102
103 File = EfiOpen (Path, EFI_FILE_MODE_READ, 0);
104 if (File == NULL) {
105 return EFI_SUCCESS;
106 }
107
108 if (File->Type == EfiOpenFirmwareVolume) {
109 // FV Dir
110
111 SearchType = EFI_FV_FILETYPE_ALL;
112 UnicodeFileName[0] = '\0';
113 MatchSubString = &UnicodeFileName[0];
114 if (Argc > 2) {
115 AsciiStrToUnicodeStr (Argv[2], UnicodeFileName);
116 if (UnicodeFileName[0] == '*') {
117 // Handle *Name substring matching
118 MatchSubString = &UnicodeFileName[1];
119 }
120
121 // Handle file type matchs
122 if (Argc > 3) {
123 // match a specific file type, always last argument
124 Length = AsciiStrLen (Argv[3]);
125 for (Index = 1, BestMatchCount = 0; Index < sizeof (gFvFileType)/sizeof (CHAR8 *); Index++) {
126 if (AsciiStriCmp (gFvFileType[Index], Argv[3]) == 0) {
127 // exact match
128 SearchType = Index;
129 break;
130 }
131
132 if (AsciiStrniCmp (Argv[3], gFvFileType[Index], Length) == 0) {
133 // partial match, so keep looking to make sure there is only one partial match
134 BestMatchCount++;
135 SearchType = Index;
136 }
137 }
138
139 if (BestMatchCount > 1) {
140 SearchType = EFI_FV_FILETYPE_ALL;
141 }
142 }
143 }
144
145 Fv = File->Fv;
146 Key = 0;
147 CurrentRow = 0;
148 do {
149 Type = SearchType;
150 GetNextFileStatus = Fv->GetNextFile (
151 Fv,
152 &Key,
153 &Type,
154 &NameGuid,
155 &Attributes,
156 &Size
157 );
158 if (!EFI_ERROR (GetNextFileStatus)) {
159 // Calculate size of entire file
160 Section = NULL;
161 Size = 0;
162 Status = Fv->ReadFile (
163 Fv,
164 &NameGuid,
165 Section,
166 &Size,
167 &Type,
168 &Attributes,
169 &AuthenticationStatus
170 );
171 if (!((Status == EFI_BUFFER_TOO_SMALL) || !EFI_ERROR (Status))) {
172 // EFI_SUCCESS or EFI_BUFFER_TOO_SMALL mean size is valid
173 Size = 0;
174 }
175
176 // read the UI seciton to do a name match.
177 Section = NULL;
178 Status = Fv->ReadSection (
179 Fv,
180 &NameGuid,
181 EFI_SECTION_USER_INTERFACE,
182 0,
183 &Section,
184 &SectionSize,
185 &AuthenticationStatus
186 );
187 if (!EFI_ERROR (Status)) {
188 if (StrStr (Section, MatchSubString) != NULL) {
189 AsciiPrint (" %g %s %a %,d\n", &NameGuid, Section, gFvFileType[Type], Size);
190 if (EblAnyKeyToContinueQtoQuit (&CurrentRow, FALSE)) {
191 break;
192 }
193 }
194 FreePool (Section);
195 } else {
196 if (*MatchSubString == '\0') {
197 AsciiPrint (" %g %a %,d\n", &NameGuid, gFvFileType[Type], Size);
198 if (EblAnyKeyToContinueQtoQuit (&CurrentRow, FALSE)) {
199 break;
200 }
201 }
202 }
203 }
204 } while (!EFI_ERROR (GetNextFileStatus));
205
206 } else if ((File->Type == EfiOpenFileSystem) || (File->Type == EfiOpenBlockIo)) {
207 // Simple File System DIR
208
209 if (File->FsFileInfo == NULL) {
210 return EFI_SUCCESS;
211 }
212
213 if (!(File->FsFileInfo->Attribute & EFI_FILE_DIRECTORY)) {
214 return EFI_SUCCESS;
215 }
216
217 // Handle *Name substring matching
218 MatchSubString = NULL;
219 UnicodeFileName[0] = '\0';
220 if (Argc > 2) {
221 AsciiStrToUnicodeStr (Argv[2], UnicodeFileName);
222 if (UnicodeFileName[0] == '*') {
223 MatchSubString = &UnicodeFileName[1];
224 }
225 }
226
227 File->FsFileHandle->SetPosition (File->FsFileHandle, 0);
228 for (CurrentRow = 0;;) {
229 // First read gets the size
230 DirInfo = NULL;
231 ReadSize = 0;
232 Status = File->FsFileHandle->Read (File->FsFileHandle, &ReadSize, DirInfo);
233 if (Status == EFI_BUFFER_TOO_SMALL) {
234 // Allocate the buffer for the real read
235 DirInfo = AllocatePool (ReadSize);
236 if (DirInfo == NULL) {
237 goto Done;
238 }
239
240 // Read the data
241 Status = File->FsFileHandle->Read (File->FsFileHandle, &ReadSize, DirInfo);
242 if ((EFI_ERROR (Status)) || (ReadSize == 0)) {
243 break;
244 }
245 } else {
246 break;
247 }
248
249 if (MatchSubString != NULL) {
250 if (StrStr (&DirInfo->FileName[0], MatchSubString) == NULL) {
251 // does not match *name argument, so skip
252 continue;
253 }
254 } else if (UnicodeFileName[0] != '\0') {
255 // is not an exact match for name argument, so skip
256 if (StrCmp (&DirInfo->FileName[0], UnicodeFileName) != 0) {
257 continue;
258 }
259 }
260
261 if (DirInfo->Attribute & EFI_FILE_DIRECTORY) {
262 AsciiPrint (" <DIR> %s\n", &DirInfo->FileName[0]);
263 } else {
264 AsciiPrint ("%,14ld %s\n", DirInfo->FileSize, &DirInfo->FileName[0]);
265 }
266
267 if (EblAnyKeyToContinueQtoQuit (&CurrentRow, FALSE)) {
268 break;
269 }
270
271 FreePool (DirInfo);
272 }
273
274 Done:
275 if (DirInfo != NULL) {
276 FreePool (DirInfo);
277 }
278 }
279
280 EfiClose (File);
281
282 return EFI_SUCCESS;
283 }
284
285 /**
286 Change the Current Working Directory
287
288 Argv[0] - "cd"
289 Argv[1] - Device Name:path. Path is optional
290
291 @param Argc Number of command arguments in Argv
292 @param Argv Array of strings that represent the parsed command line.
293 Argv[0] is the comamnd name
294
295 @return EFI_SUCCESS
296
297 **/
298 EFI_STATUS
299 EblCdCmd (
300 IN UINTN Argc,
301 IN CHAR8 **Argv
302 )
303 {
304 if (Argc <= 1) {
305 return EFI_SUCCESS;
306 }
307
308 return EfiSetCwd (Argv[1]);
309 }
310
311
312
313 GLOBAL_REMOVE_IF_UNREFERENCED const EBL_COMMAND_TABLE mCmdDirTemplate[] =
314 {
315 {
316 "dir",
317 " dirdev [*match]; directory listing of dirdev. opt match a substring",
318 NULL,
319 EblDirCmd
320 },
321 {
322 "cd",
323 " device - set the current working directory",
324 NULL,
325 EblCdCmd
326 }
327 };
328
329
330 /**
331 Initialize the commands in this in this file
332 **/
333 VOID
334 EblInitializeDirCmd (
335 VOID
336 )
337 {
338 if (FeaturePcdGet (PcdEmbeddedDirCmd)) {
339 EblAddCommands (mCmdDirTemplate, sizeof (mCmdDirTemplate)/sizeof (EBL_COMMAND_TABLE));
340 }
341 }
342