VirtualBox

source: vbox/trunk/src/VBox/Devices/EFI/FirmwareNew/ShellPkg/Library/UefiShellDebug1CommandsLib/UefiShellDebug1CommandsLib.c

Last change on this file was 108794, checked in by vboxsync, 4 weeks ago

Devices/EFI/FirmwareNew: Merge edk2-stable202502 from the vendor branch and make it build for the important platforms, bugref:4643

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette