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