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