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