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