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