]>
Commit | Line | Data |
---|---|---|
5d73d92f | 1 | /** @file\r |
2 | Main file for NULL named library for debug1 profile shell command functions.\r | |
3 | \r | |
ba0014b9 | 4 | Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved.<BR>\r |
56ba3746 | 5 | SPDX-License-Identifier: BSD-2-Clause-Patent\r |
5d73d92f | 6 | \r |
7 | **/\r | |
8 | \r | |
9 | #include "UefiShellDebug1CommandsLib.h"\r | |
fba9b6ba | 10 | #include <Library/BcfgCommandLib.h>\r |
5d73d92f | 11 | \r |
12 | STATIC CONST CHAR16 mFileName[] = L"Debug1Commands";\r | |
13 | EFI_HANDLE gShellDebug1HiiHandle = NULL;\r | |
5d73d92f | 14 | \r |
3737ac2b | 15 | /**\r |
16 | Gets the debug file name. This will be used if HII is not working.\r | |
17 | \r | |
18 | @retval NULL No file is available.\r | |
19 | @return The NULL-terminated filename to get help from.\r | |
20 | **/\r | |
5d73d92f | 21 | CONST CHAR16*\r |
22 | EFIAPI\r | |
23 | ShellCommandGetManFileNameDebug1 (\r | |
24 | VOID\r | |
25 | )\r | |
26 | {\r | |
27 | return (mFileName);\r | |
28 | }\r | |
29 | \r | |
30 | /**\r | |
31 | Constructor for the Shell Debug1 Commands library.\r | |
32 | \r | |
33 | @param ImageHandle the image handle of the process\r | |
34 | @param SystemTable the EFI System Table pointer\r | |
35 | \r | |
36 | @retval EFI_SUCCESS the shell command handlers were installed sucessfully\r | |
37 | @retval EFI_UNSUPPORTED the shell level required was not found.\r | |
38 | **/\r | |
39 | EFI_STATUS\r | |
40 | EFIAPI\r | |
41 | UefiShellDebug1CommandsLibConstructor (\r | |
42 | IN EFI_HANDLE ImageHandle,\r | |
43 | IN EFI_SYSTEM_TABLE *SystemTable\r | |
44 | )\r | |
45 | {\r | |
46 | //\r | |
47 | // check our bit of the profiles mask\r | |
48 | //\r | |
49 | if ((PcdGet8(PcdShellProfileMask) & BIT1) == 0) {\r | |
82571fb5 | 50 | return (EFI_SUCCESS);\r |
5d73d92f | 51 | }\r |
52 | \r | |
53 | //\r | |
54 | // install the HII stuff.\r | |
55 | //\r | |
56 | gShellDebug1HiiHandle = HiiAddPackages (&gShellDebug1HiiGuid, gImageHandle, UefiShellDebug1CommandsLibStrings, NULL);\r | |
57 | if (gShellDebug1HiiHandle == NULL) {\r | |
58 | return (EFI_DEVICE_ERROR);\r | |
59 | }\r | |
60 | \r | |
61 | //\r | |
62 | // install our shell command handlers that are always installed\r | |
63 | //\r | |
3737ac2b | 64 | ShellCommandRegisterCommandName(L"setsize", ShellCommandRunSetSize , ShellCommandGetManFileNameDebug1, 0, L"Debug1", TRUE, gShellDebug1HiiHandle, STRING_TOKEN(STR_GET_HELP_SETSIZE) );\r |
5d73d92f | 65 | ShellCommandRegisterCommandName(L"comp", ShellCommandRunComp , ShellCommandGetManFileNameDebug1, 0, L"Debug1", TRUE, gShellDebug1HiiHandle, STRING_TOKEN(STR_GET_HELP_COMP) );\r |
66 | ShellCommandRegisterCommandName(L"mode", ShellCommandRunMode , ShellCommandGetManFileNameDebug1, 0, L"Debug1", TRUE, gShellDebug1HiiHandle, STRING_TOKEN(STR_GET_HELP_MODE) );\r | |
67 | ShellCommandRegisterCommandName(L"memmap", ShellCommandRunMemMap , ShellCommandGetManFileNameDebug1, 0, L"Debug1", TRUE, gShellDebug1HiiHandle, STRING_TOKEN(STR_GET_HELP_MEMMAP) );\r | |
68 | ShellCommandRegisterCommandName(L"eficompress", ShellCommandRunEfiCompress , ShellCommandGetManFileNameDebug1, 0, L"Debug1", TRUE, gShellDebug1HiiHandle, STRING_TOKEN(STR_GET_HELP_EFICOMPRESS) );\r | |
69 | ShellCommandRegisterCommandName(L"efidecompress", ShellCommandRunEfiDecompress , ShellCommandGetManFileNameDebug1, 0, L"Debug1", TRUE, gShellDebug1HiiHandle, STRING_TOKEN(STR_GET_HELP_EFIDCOMPRESS) );\r | |
70 | ShellCommandRegisterCommandName(L"dmem", ShellCommandRunDmem , ShellCommandGetManFileNameDebug1, 0, L"Debug1", TRUE, gShellDebug1HiiHandle, STRING_TOKEN(STR_GET_HELP_DMEM) );\r | |
3737ac2b | 71 | ShellCommandRegisterCommandName(L"loadpcirom", ShellCommandRunLoadPciRom , ShellCommandGetManFileNameDebug1, 0, L"Debug1", TRUE, gShellDebug1HiiHandle, STRING_TOKEN(STR_GET_HELP_LOAD_PCI_ROM) );\r |
5d73d92f | 72 | ShellCommandRegisterCommandName(L"mm", ShellCommandRunMm , ShellCommandGetManFileNameDebug1, 0, L"Debug1", TRUE, gShellDebug1HiiHandle, STRING_TOKEN(STR_GET_HELP_MM) );\r |
3737ac2b | 73 | ShellCommandRegisterCommandName(L"setvar", ShellCommandRunSetVar , ShellCommandGetManFileNameDebug1, 0, L"Debug1", TRUE, gShellDebug1HiiHandle, STRING_TOKEN(STR_GET_HELP_SETVAR) );\r |
74 | ShellCommandRegisterCommandName(L"sermode", ShellCommandRunSerMode , ShellCommandGetManFileNameDebug1, 0, L"Debug1", TRUE, gShellDebug1HiiHandle, STRING_TOKEN(STR_GET_HELP_SERMODE) );\r | |
75 | ShellCommandRegisterCommandName(L"pci", ShellCommandRunPci , ShellCommandGetManFileNameDebug1, 0, L"Debug1", TRUE, gShellDebug1HiiHandle, STRING_TOKEN(STR_GET_HELP_PCI) );\r | |
5d73d92f | 76 | ShellCommandRegisterCommandName(L"smbiosview", ShellCommandRunSmbiosView , ShellCommandGetManFileNameDebug1, 0, L"Debug1", TRUE, gShellDebug1HiiHandle, STRING_TOKEN(STR_GET_HELP_SMBIOSVIEW) );\r |
77 | ShellCommandRegisterCommandName(L"dmpstore", ShellCommandRunDmpStore , ShellCommandGetManFileNameDebug1, 0, L"Debug1", TRUE, gShellDebug1HiiHandle, STRING_TOKEN(STR_GET_HELP_DMPSTORE) );\r | |
78 | ShellCommandRegisterCommandName(L"dblk", ShellCommandRunDblk , ShellCommandGetManFileNameDebug1, 0, L"Debug1", TRUE, gShellDebug1HiiHandle, STRING_TOKEN(STR_GET_HELP_DBLK) );\r | |
3737ac2b | 79 | ShellCommandRegisterCommandName(L"edit", ShellCommandRunEdit , ShellCommandGetManFileNameDebug1, 0, L"Debug1", TRUE, gShellDebug1HiiHandle, STRING_TOKEN(STR_GET_HELP_EDIT) );\r |
80 | ShellCommandRegisterCommandName(L"hexedit", ShellCommandRunHexEdit , ShellCommandGetManFileNameDebug1, 0, L"Debug1", TRUE, gShellDebug1HiiHandle, STRING_TOKEN(STR_GET_HELP_HEXEDIT) );\r | |
5d73d92f | 81 | \r |
5d73d92f | 82 | ShellCommandRegisterAlias(L"dmem", L"mem");\r |
83 | \r | |
fba9b6ba JC |
84 | BcfgLibraryRegisterBcfgCommand(ImageHandle, SystemTable, L"Debug1");\r |
85 | \r | |
5d73d92f | 86 | return (EFI_SUCCESS);\r |
87 | }\r | |
88 | \r | |
89 | /**\r | |
90 | Destructor for the library. free any resources.\r | |
3737ac2b | 91 | \r |
92 | @param ImageHandle The image handle of the process.\r | |
93 | @param SystemTable The EFI System Table pointer.\r | |
5d73d92f | 94 | **/\r |
95 | EFI_STATUS\r | |
96 | EFIAPI\r | |
97 | UefiShellDebug1CommandsLibDestructor (\r | |
98 | IN EFI_HANDLE ImageHandle,\r | |
99 | IN EFI_SYSTEM_TABLE *SystemTable\r | |
100 | )\r | |
101 | {\r | |
102 | if (gShellDebug1HiiHandle != NULL) {\r | |
103 | HiiRemovePackages(gShellDebug1HiiHandle);\r | |
104 | }\r | |
fba9b6ba JC |
105 | \r |
106 | BcfgLibraryUnregisterBcfgCommand(ImageHandle, SystemTable);\r | |
5d73d92f | 107 | return (EFI_SUCCESS);\r |
108 | }\r | |
109 | \r | |
5d73d92f | 110 | \r |
111 | /**\r | |
112 | Function returns a system configuration table that is stored in the\r | |
113 | EFI System Table based on the provided GUID.\r | |
114 | \r | |
4ff7e37b ED |
115 | @param[in] TableGuid A pointer to the table's GUID type.\r |
116 | @param[in, out] Table On exit, a pointer to a system configuration table.\r | |
5d73d92f | 117 | \r |
118 | @retval EFI_SUCCESS A configuration table matching TableGuid was found.\r | |
119 | @retval EFI_NOT_FOUND A configuration table matching TableGuid was not found.\r | |
120 | **/\r | |
121 | EFI_STATUS\r | |
5d73d92f | 122 | GetSystemConfigurationTable (\r |
123 | IN EFI_GUID *TableGuid,\r | |
124 | IN OUT VOID **Table\r | |
125 | )\r | |
126 | {\r | |
127 | UINTN Index;\r | |
128 | ASSERT (Table != NULL);\r | |
129 | \r | |
130 | for (Index = 0; Index < gST->NumberOfTableEntries; Index++) {\r | |
3737ac2b | 131 | if (CompareGuid (TableGuid, &(gST->ConfigurationTable[Index].VendorGuid))) {\r |
5d73d92f | 132 | *Table = gST->ConfigurationTable[Index].VendorTable;\r |
133 | return EFI_SUCCESS;\r | |
134 | }\r | |
135 | }\r | |
136 | \r | |
137 | return EFI_NOT_FOUND;\r | |
138 | }\r | |
139 | \r | |
3737ac2b | 140 | /**\r |
141 | Clear the line at the specified Row.\r | |
ba0014b9 | 142 | \r |
3737ac2b | 143 | @param[in] Row The row number to be cleared ( start from 1 )\r |
144 | @param[in] LastCol The last printable column.\r | |
145 | @param[in] LastRow The last printable row.\r | |
146 | **/\r | |
147 | VOID\r | |
3737ac2b | 148 | EditorClearLine (\r |
149 | IN UINTN Row,\r | |
150 | IN UINTN LastCol,\r | |
151 | IN UINTN LastRow\r | |
152 | )\r | |
153 | {\r | |
1a421c2c | 154 | UINTN Col;\r |
3737ac2b | 155 | CHAR16 Line[200];\r |
156 | \r | |
157 | if (Row == 0) {\r | |
158 | Row = 1;\r | |
159 | }\r | |
160 | \r | |
161 | //\r | |
162 | // prepare a blank line\r | |
1a421c2c | 163 | // If max column is larger, split to multiple prints.\r |
3737ac2b | 164 | //\r |
1a421c2c RN |
165 | SetMem16 (Line, sizeof (Line), L' ');\r |
166 | Line[ARRAY_SIZE (Line) - 1] = CHAR_NULL;\r | |
167 | \r | |
168 | for (Col = 1; Col <= LastCol; Col += ARRAY_SIZE (Line) - 1) {\r | |
169 | if (Col + ARRAY_SIZE (Line) - 1 > LastCol) {\r | |
170 | if (Row == LastRow) {\r | |
171 | //\r | |
172 | // if CHAR_NULL is still at position LastCol, it will cause first line error\r | |
173 | //\r | |
452676ff | 174 | Line[(LastCol - 1) % (ARRAY_SIZE (Line) - 1)] = CHAR_NULL;\r |
1a421c2c RN |
175 | } else {\r |
176 | Line[LastCol % (ARRAY_SIZE (Line) - 1)] = CHAR_NULL;\r | |
177 | }\r | |
178 | }\r | |
ba0014b9 | 179 | \r |
3737ac2b | 180 | //\r |
1a421c2c | 181 | // print out the blank line\r |
3737ac2b | 182 | //\r |
1a421c2c | 183 | ShellPrintEx ((INT32) Col - 1, (INT32) Row - 1, Line);\r |
3737ac2b | 184 | }\r |
3737ac2b | 185 | }\r |
186 | \r | |
187 | /**\r | |
188 | Determine if the character is valid for a filename.\r | |
189 | \r | |
190 | @param[in] Ch The character to test.\r | |
191 | \r | |
192 | @retval TRUE The character is valid.\r | |
193 | @retval FALSE The character is not valid.\r | |
194 | **/\r | |
195 | BOOLEAN\r | |
3737ac2b | 196 | IsValidFileNameChar (\r |
197 | IN CONST CHAR16 Ch\r | |
198 | )\r | |
199 | {\r | |
200 | //\r | |
201 | // See if there are any illegal characters within the name\r | |
202 | //\r | |
d16efcae | 203 | if (Ch < 0x20 || Ch == L'\"' || Ch == L'*' || Ch == L'/' || Ch == L'<' || Ch == L'>' || Ch == L'?' || Ch == L'|') {\r |
3737ac2b | 204 | return FALSE;\r |
205 | }\r | |
206 | \r | |
207 | return TRUE;\r | |
208 | }\r | |
209 | \r | |
210 | /**\r | |
211 | Check if file name has illegal characters.\r | |
ba0014b9 | 212 | \r |
3737ac2b | 213 | @param Name The filename to check.\r |
214 | \r | |
215 | @retval TRUE The filename is ok.\r | |
216 | @retval FALSE The filename is not ok.\r | |
217 | **/\r | |
218 | BOOLEAN\r | |
3737ac2b | 219 | IsValidFileName (\r |
220 | IN CONST CHAR16 *Name\r | |
221 | )\r | |
222 | {\r | |
223 | \r | |
224 | UINTN Index;\r | |
225 | UINTN Len;\r | |
226 | \r | |
227 | //\r | |
228 | // check the length of Name\r | |
229 | //\r | |
230 | for (Len = 0, Index = StrLen (Name) - 1; Index + 1 != 0; Index--, Len++) {\r | |
231 | if (Name[Index] == '\\' || Name[Index] == ':') {\r | |
232 | break;\r | |
233 | }\r | |
234 | }\r | |
235 | \r | |
236 | if (Len == 0 || Len > 255) {\r | |
237 | return FALSE;\r | |
238 | }\r | |
239 | //\r | |
240 | // check whether any char in Name not appears in valid file name char\r | |
241 | //\r | |
242 | for (Index = 0; Index < StrLen (Name); Index++) {\r | |
243 | if (!IsValidFileNameChar (Name[Index])) {\r | |
244 | return FALSE;\r | |
245 | }\r | |
5d73d92f | 246 | }\r |
3737ac2b | 247 | \r |
248 | return TRUE;\r | |
249 | }\r | |
250 | \r | |
251 | /**\r | |
252 | Find a filename that is valid (not taken) with the given extension.\r | |
253 | \r | |
254 | @param[in] Extension The file extension.\r | |
255 | \r | |
256 | @retval NULL Something went wrong.\r | |
257 | @return the valid filename.\r | |
258 | **/\r | |
259 | CHAR16 *\r | |
3737ac2b | 260 | EditGetDefaultFileName (\r |
261 | IN CONST CHAR16 *Extension\r | |
262 | )\r | |
263 | {\r | |
264 | EFI_STATUS Status;\r | |
265 | UINTN Suffix;\r | |
3737ac2b | 266 | CHAR16 *FileNameTmp;\r |
267 | \r | |
268 | Suffix = 0;\r | |
3737ac2b | 269 | \r |
270 | do {\r | |
271 | FileNameTmp = CatSPrint (NULL, L"NewFile%d.%s", Suffix, Extension);\r | |
272 | \r | |
273 | //\r | |
274 | // after that filename changed to path\r | |
275 | //\r | |
276 | Status = ShellFileExists (FileNameTmp);\r | |
277 | \r | |
278 | if (Status == EFI_NOT_FOUND) {\r | |
279 | return FileNameTmp;\r | |
280 | }\r | |
281 | \r | |
282 | FreePool (FileNameTmp);\r | |
283 | FileNameTmp = NULL;\r | |
284 | Suffix++;\r | |
285 | } while (Suffix != 0);\r | |
286 | \r | |
287 | FreePool (FileNameTmp);\r | |
288 | return NULL;\r | |
5d73d92f | 289 | }\r |
290 | \r | |
3737ac2b | 291 | /**\r |
ba0014b9 | 292 | Read a file into an allocated buffer. The buffer is the responsibility\r |
3737ac2b | 293 | of the caller to free.\r |
294 | \r | |
295 | @param[in] FileName The filename of the file to open.\r | |
ba0014b9 LG |
296 | @param[out] Buffer Upon successful return, the pointer to the\r |
297 | address of the allocated buffer.\r | |
3737ac2b | 298 | @param[out] BufferSize If not NULL, then the pointer to the size\r |
299 | of the allocated buffer.\r | |
300 | @param[out] ReadOnly Upon successful return TRUE if the file is\r | |
301 | read only. FALSE otherwise.\r | |
302 | \r | |
ba0014b9 | 303 | @retval EFI_NOT_FOUND The filename did not represent a file in the\r |
3737ac2b | 304 | file system.\r |
305 | @retval EFI_SUCCESS The file was read into the buffer.\r | |
306 | @retval EFI_OUT_OF_RESOURCES A memory allocation failed.\r | |
307 | @retval EFI_LOAD_ERROR The file read operation failed.\r | |
308 | @retval EFI_INVALID_PARAMETER A parameter was invalid.\r | |
309 | @retval EFI_INVALID_PARAMETER FileName was NULL.\r | |
310 | @retval EFI_INVALID_PARAMETER FileName was a directory.\r | |
311 | **/\r | |
312 | EFI_STATUS\r | |
3737ac2b | 313 | ReadFileIntoBuffer (\r |
314 | IN CONST CHAR16 *FileName,\r | |
315 | OUT VOID **Buffer,\r | |
316 | OUT UINTN *BufferSize OPTIONAL,\r | |
317 | OUT BOOLEAN *ReadOnly\r | |
318 | )\r | |
319 | {\r | |
320 | VOID *InternalBuffer;\r | |
321 | UINTN FileSize;\r | |
322 | SHELL_FILE_HANDLE FileHandle;\r | |
323 | BOOLEAN CreateFile;\r | |
324 | EFI_STATUS Status;\r | |
325 | EFI_FILE_INFO *Info;\r | |
326 | \r | |
327 | InternalBuffer = NULL;\r | |
328 | FileSize = 0;\r | |
329 | FileHandle = NULL;\r | |
330 | CreateFile = FALSE;\r | |
331 | Status = EFI_SUCCESS;\r | |
332 | Info = NULL;\r | |
333 | \r | |
334 | if (FileName == NULL || Buffer == NULL || ReadOnly == NULL) {\r | |
335 | return (EFI_INVALID_PARAMETER);\r | |
336 | }\r | |
337 | \r | |
338 | //\r | |
339 | // try to open the file\r | |
340 | //\r | |
341 | Status = ShellOpenFileByName (FileName, &FileHandle, EFI_FILE_MODE_READ, 0);\r | |
342 | \r | |
343 | if (!EFI_ERROR(Status)) {\r | |
344 | ASSERT(CreateFile == FALSE);\r | |
345 | if (FileHandle == NULL) {\r | |
346 | return EFI_LOAD_ERROR;\r | |
347 | }\r | |
348 | \r | |
349 | Info = ShellGetFileInfo(FileHandle);\r | |
ba0014b9 | 350 | \r |
3737ac2b | 351 | if (Info->Attribute & EFI_FILE_DIRECTORY) {\r |
352 | FreePool (Info);\r | |
353 | return EFI_INVALID_PARAMETER;\r | |
354 | }\r | |
355 | \r | |
356 | if (Info->Attribute & EFI_FILE_READ_ONLY) {\r | |
357 | *ReadOnly = TRUE;\r | |
358 | } else {\r | |
359 | *ReadOnly = FALSE;\r | |
360 | }\r | |
361 | //\r | |
362 | // get file size\r | |
363 | //\r | |
364 | FileSize = (UINTN) Info->FileSize;\r | |
365 | \r | |
366 | FreePool (Info);\r | |
367 | } else if (Status == EFI_NOT_FOUND) {\r | |
368 | //\r | |
369 | // file not exists. add create and try again\r | |
370 | //\r | |
371 | Status = ShellOpenFileByName (FileName, &FileHandle, EFI_FILE_MODE_READ|EFI_FILE_MODE_WRITE|EFI_FILE_MODE_CREATE, 0);\r | |
372 | if (EFI_ERROR (Status)) {\r | |
373 | return Status;\r | |
374 | } else {\r | |
375 | //\r | |
376 | // it worked. now delete it and move on with the name (now validated)\r | |
377 | //\r | |
378 | Status = ShellDeleteFile (&FileHandle);\r | |
379 | if (Status == EFI_WARN_DELETE_FAILURE) {\r | |
380 | Status = EFI_ACCESS_DENIED;\r | |
381 | }\r | |
382 | if (EFI_ERROR (Status)) {\r | |
383 | return Status;\r | |
384 | }\r | |
385 | }\r | |
386 | //\r | |
387 | // file doesn't exist, so set CreateFile to TRUE and can't be read-only\r | |
388 | //\r | |
389 | CreateFile = TRUE;\r | |
390 | *ReadOnly = FALSE;\r | |
391 | }\r | |
392 | \r | |
393 | //\r | |
394 | // the file exists\r | |
395 | //\r | |
396 | if (!CreateFile) {\r | |
397 | //\r | |
398 | // allocate buffer to read file\r | |
399 | //\r | |
400 | InternalBuffer = AllocateZeroPool (FileSize);\r | |
401 | if (InternalBuffer == NULL) {\r | |
402 | return EFI_OUT_OF_RESOURCES;\r | |
403 | }\r | |
404 | //\r | |
405 | // read file into InternalBuffer\r | |
406 | //\r | |
407 | Status = ShellReadFile (FileHandle, &FileSize, InternalBuffer);\r | |
408 | ShellCloseFile(&FileHandle);\r | |
409 | FileHandle = NULL;\r | |
410 | if (EFI_ERROR (Status)) {\r | |
411 | SHELL_FREE_NON_NULL (InternalBuffer);\r | |
412 | return EFI_LOAD_ERROR;\r | |
413 | }\r | |
414 | }\r | |
415 | *Buffer = InternalBuffer;\r | |
416 | if (BufferSize != NULL) {\r | |
417 | *BufferSize = FileSize;\r | |
418 | }\r | |
419 | return (EFI_SUCCESS);\r | |
420 | \r | |
421 | }\r |