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