]> git.proxmox.com Git - mirror_edk2.git/blob - ShellPkg/Library/UefiShellDebug1CommandsLib/Dmem.c
ShellPkg/DMem: Handle memory allocation failure
[mirror_edk2.git] / ShellPkg / Library / UefiShellDebug1CommandsLib / Dmem.c
1 /** @file
2 Main file for Dmem shell Debug1 function.
3
4 Copyright (c) 2010 - 2016, Intel Corporation. All rights reserved.<BR>
5 (C) Copyright 2015 Hewlett-Packard Development Company, L.P.<BR>
6 (C) Copyright 2015 Hewlett Packard Enterprise Development LP<BR>
7 This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
11
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
14
15 **/
16
17 #include "UefiShellDebug1CommandsLib.h"
18 #include <Protocol/PciRootBridgeIo.h>
19 #include <Guid/Acpi.h>
20 #include <Guid/Mps.h>
21 #include <Guid/SmBios.h>
22 #include <Guid/SalSystemTable.h>
23
24 /**
25 Make a printable character.
26
27 If Char is printable then return it, otherwise return a question mark.
28
29 @param[in] Char The character to make printable.
30
31 @return A printable character representing Char.
32 **/
33 CHAR16
34 EFIAPI
35 MakePrintable(
36 IN CONST CHAR16 Char
37 )
38 {
39 if ((Char < 0x20 && Char > 0)||(Char > 126)) {
40 return (L'?');
41 }
42 return (Char);
43 }
44
45 /**
46 Display some Memory-Mapped-IO memory.
47
48 @param[in] Address The starting address to display.
49 @param[in] Size The length of memory to display.
50 **/
51 SHELL_STATUS
52 EFIAPI
53 DisplayMmioMemory(
54 IN CONST VOID *Address,
55 IN CONST UINTN Size
56 )
57 {
58 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *PciRbIo;
59 EFI_STATUS Status;
60 VOID *Buffer;
61 SHELL_STATUS ShellStatus;
62
63 ShellStatus = SHELL_SUCCESS;
64
65 Status = gBS->LocateProtocol(&gEfiPciRootBridgeIoProtocolGuid, NULL, (VOID**)&PciRbIo);
66 if (EFI_ERROR(Status)) {
67 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PCIRBIO_NF), gShellDebug1HiiHandle, L"dmem");
68 return (SHELL_NOT_FOUND);
69 }
70 Buffer = AllocateZeroPool(Size);
71 if (Buffer == NULL) {
72 return SHELL_OUT_OF_RESOURCES;
73 }
74
75 Status = PciRbIo->Mem.Read(PciRbIo, EfiPciWidthUint8, (UINT64)(UINTN)Address, Size, Buffer);
76 if (EFI_ERROR(Status)) {
77 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PCIRBIO_ER), gShellDebug1HiiHandle, L"dmem");
78 ShellStatus = SHELL_NOT_FOUND;
79 } else {
80 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_DMEM_MMIO_HEADER_ROW), gShellDebug1HiiHandle, (UINT64)(UINTN)Address, Size);
81 DumpHex(2, (UINTN)Address, Size, Buffer);
82 }
83
84 FreePool(Buffer);
85 return (ShellStatus);
86 }
87
88 STATIC CONST SHELL_PARAM_ITEM ParamList[] = {
89 {L"-mmio", TypeFlag},
90 {NULL, TypeMax}
91 };
92
93 /**
94 Function for 'dmem' command.
95
96 @param[in] ImageHandle Handle to the Image (NULL if Internal).
97 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
98 **/
99 SHELL_STATUS
100 EFIAPI
101 ShellCommandRunDmem (
102 IN EFI_HANDLE ImageHandle,
103 IN EFI_SYSTEM_TABLE *SystemTable
104 )
105 {
106 EFI_STATUS Status;
107 LIST_ENTRY *Package;
108 CHAR16 *ProblemParam;
109 SHELL_STATUS ShellStatus;
110 VOID *Address;
111 UINT64 Size;
112 CONST CHAR16 *Temp1;
113 UINT64 AcpiTableAddress;
114 UINT64 Acpi20TableAddress;
115 UINT64 SalTableAddress;
116 UINT64 SmbiosTableAddress;
117 UINT64 MpsTableAddress;
118 UINTN TableWalker;
119
120 ShellStatus = SHELL_SUCCESS;
121 Status = EFI_SUCCESS;
122 Address = NULL;
123 Size = 0;
124
125 //
126 // initialize the shell lib (we must be in non-auto-init...)
127 //
128 Status = ShellInitialize();
129 ASSERT_EFI_ERROR(Status);
130
131 Status = CommandInit();
132 ASSERT_EFI_ERROR(Status);
133
134 //
135 // parse the command line
136 //
137 Status = ShellCommandLineParse (ParamList, &Package, &ProblemParam, TRUE);
138 if (EFI_ERROR(Status)) {
139 if (Status == EFI_VOLUME_CORRUPTED && ProblemParam != NULL) {
140 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellDebug1HiiHandle, L"dmem", ProblemParam);
141 FreePool(ProblemParam);
142 ShellStatus = SHELL_INVALID_PARAMETER;
143 } else {
144 ASSERT(FALSE);
145 }
146 } else {
147 if (ShellCommandLineGetCount(Package) > 3) {
148 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellDebug1HiiHandle, L"dmem");
149 ShellStatus = SHELL_INVALID_PARAMETER;
150 } else {
151 Temp1 = ShellCommandLineGetRawValue(Package, 1);
152 if (Temp1 == NULL) {
153 Address = gST;
154 Size = 512;
155 } else {
156 if (!ShellIsHexOrDecimalNumber(Temp1, TRUE, FALSE) || EFI_ERROR(ShellConvertStringToUint64(Temp1, (UINT64*)&Address, TRUE, FALSE))) {
157 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV), gShellDebug1HiiHandle, L"dmem", Temp1);
158 ShellStatus = SHELL_INVALID_PARAMETER;
159 }
160 Temp1 = ShellCommandLineGetRawValue(Package, 2);
161 if (Temp1 == NULL) {
162 Size = 512;
163 } else {
164 if (!ShellIsHexOrDecimalNumber(Temp1, FALSE, FALSE) || EFI_ERROR(ShellConvertStringToUint64(Temp1, &Size, TRUE, FALSE))) {
165 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV), gShellDebug1HiiHandle, L"dmem", Temp1);
166 ShellStatus = SHELL_INVALID_PARAMETER;
167 }
168 }
169 }
170 }
171
172 if (ShellStatus == SHELL_SUCCESS) {
173 if (!ShellCommandLineGetFlag(Package, L"-mmio")) {
174 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_DMEM_HEADER_ROW), gShellDebug1HiiHandle, (UINT64)(UINTN)Address, Size);
175 DumpHex(2, (UINTN)Address, (UINTN)Size, Address);
176 if (Address == (VOID*)gST) {
177 Acpi20TableAddress = 0;
178 AcpiTableAddress = 0;
179 SalTableAddress = 0;
180 SmbiosTableAddress = 0;
181 MpsTableAddress = 0;
182 for (TableWalker = 0 ; TableWalker < gST->NumberOfTableEntries ; TableWalker++) {
183 if (CompareGuid(&gST->ConfigurationTable[TableWalker].VendorGuid, &gEfiAcpi20TableGuid)) {
184 Acpi20TableAddress = (UINT64)(UINTN)gST->ConfigurationTable[TableWalker].VendorTable;
185 continue;
186 }
187 if (CompareGuid(&gST->ConfigurationTable[TableWalker].VendorGuid, &gEfiAcpi10TableGuid)) {
188 AcpiTableAddress = (UINT64)(UINTN)gST->ConfigurationTable[TableWalker].VendorTable;
189 continue;
190 }
191 if (CompareGuid(&gST->ConfigurationTable[TableWalker].VendorGuid, &gEfiSalSystemTableGuid)) {
192 SalTableAddress = (UINT64)(UINTN)gST->ConfigurationTable[TableWalker].VendorTable;
193 continue;
194 }
195 if (CompareGuid(&gST->ConfigurationTable[TableWalker].VendorGuid, &gEfiSmbiosTableGuid)) {
196 SmbiosTableAddress = (UINT64)(UINTN)gST->ConfigurationTable[TableWalker].VendorTable;
197 continue;
198 }
199 if (CompareGuid (&gST->ConfigurationTable[TableWalker].VendorGuid, &gEfiSmbios3TableGuid)) {
200 SmbiosTableAddress = (UINT64) (UINTN) gST->ConfigurationTable[TableWalker].VendorTable;
201 continue;
202 }
203 if (CompareGuid(&gST->ConfigurationTable[TableWalker].VendorGuid, &gEfiMpsTableGuid)) {
204 MpsTableAddress = (UINT64)(UINTN)gST->ConfigurationTable[TableWalker].VendorTable;
205 continue;
206 }
207 }
208
209 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_DMEM_SYSTEM_TABLE), gShellDebug1HiiHandle,
210 (UINT64)(UINTN)Address,
211 gST->Hdr.HeaderSize,
212 gST->Hdr.Revision,
213 (UINT64)(UINTN)gST->ConIn,
214 (UINT64)(UINTN)gST->ConOut,
215 (UINT64)(UINTN)gST->StdErr,
216 (UINT64)(UINTN)gST->RuntimeServices,
217 (UINT64)(UINTN)gST->BootServices,
218 SalTableAddress,
219 AcpiTableAddress,
220 Acpi20TableAddress,
221 MpsTableAddress,
222 SmbiosTableAddress
223 );
224 }
225 } else {
226 ShellStatus = DisplayMmioMemory(Address, (UINTN)Size);
227 }
228 }
229
230
231 ShellCommandLineFreeVarList (Package);
232 }
233
234 return (ShellStatus);
235 }