Dmem.c 7.5 KB

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