2 Main file for Mm shell Debug1 function.
4 (C) Copyright 2015 Hewlett-Packard Development Company, L.P.<BR>
5 Copyright (c) 2005 - 2017, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
10 #include "UefiShellDebug1CommandsLib.h"
11 #include <Library/ShellLib.h>
12 #include <Library/IoLib.h>
13 #include <Protocol/PciRootBridgeIo.h>
14 #include <Protocol/DeviceIo.h>
18 ShellMmMemoryMappedIo
,
22 } SHELL_MM_ACCESS_TYPE
;
24 CONST UINT16 mShellMmAccessTypeStr
[] = {
25 STRING_TOKEN (STR_MM_MEM
),
26 STRING_TOKEN (STR_MM_MMIO
),
27 STRING_TOKEN (STR_MM_IO
),
28 STRING_TOKEN (STR_MM_PCI
),
29 STRING_TOKEN (STR_MM_PCIE
)
32 STATIC CONST SHELL_PARAM_ITEM ParamList
[] = {
43 CONST UINT64 mShellMmMaxNumber
[] = {
44 0, MAX_UINT8
, MAX_UINT16
, 0, MAX_UINT32
, 0, 0, 0, MAX_UINT64
46 CONST EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH mShellMmRootBridgeIoWidth
[] = {
47 0, EfiPciWidthUint8
, EfiPciWidthUint16
, 0, EfiPciWidthUint32
, 0, 0, 0, EfiPciWidthUint64
49 CONST EFI_CPU_IO_PROTOCOL_WIDTH mShellMmCpuIoWidth
[] = {
50 0, EfiCpuIoWidthUint8
, EfiCpuIoWidthUint16
, 0, EfiCpuIoWidthUint32
, 0, 0, 0, EfiCpuIoWidthUint64
54 Extract the PCI segment, bus, device, function, register from
55 from a PCI or PCIE format of address..
57 @param[in] PciFormat Whether the address is of PCI format of PCIE format.
58 @param[in] Address PCI or PCIE address.
59 @param[out] Segment PCI segment number.
60 @param[out] Bus PCI bus number.
61 @param[out] Device PCI device number.
62 @param[out] Function PCI function number.
63 @param[out] Register PCI register offset.
66 ShellMmDecodePciAddress (
71 OUT UINT8
*Device
, OPTIONAL
72 OUT UINT8
*Function
, OPTIONAL
73 OUT UINT32
*Register OPTIONAL
78 // PCI Configuration Space.The address will have the format ssssbbddffrr,
79 // where ssss = Segment, bb = Bus, dd = Device, ff = Function and rr = Register.
81 *Segment
= (UINT32
) (RShiftU64 (Address
, 32) & 0xFFFF);
82 *Bus
= (UINT8
) (((UINT32
) Address
) >> 24);
85 *Device
= (UINT8
) (((UINT32
) Address
) >> 16);
87 if (Function
!= NULL
) {
88 *Function
= (UINT8
) (((UINT32
) Address
) >> 8);
90 if (Register
!= NULL
) {
91 *Register
= (UINT8
) Address
;
95 // PCI Express Configuration Space.The address will have the format ssssssbbddffrrr,
96 // where ssss = Segment, bb = Bus, dd = Device, ff = Function and rrr = Register.
98 *Segment
= (UINT32
) (RShiftU64 (Address
, 36) & 0xFFFF);
99 *Bus
= (UINT8
) RShiftU64 (Address
, 28);
100 if (Device
!= NULL
) {
101 *Device
= (UINT8
) (((UINT32
) Address
) >> 20);
103 if (Function
!= NULL
) {
104 *Function
= (UINT8
) (((UINT32
) Address
) >> 12);
106 if (Register
!= NULL
) {
107 *Register
= (UINT32
) (Address
& 0xFFF);
113 Read or write some data from or into the Address.
115 @param[in] AccessType Access type.
116 @param[in] PciRootBridgeIo PciRootBridgeIo instance.
117 @param[in] CpuIo CpuIo instance.
118 @param[in] Read TRUE for read, FALSE for write.
119 @param[in] Addresss The memory location to access.
120 @param[in] Size The size of Buffer in Width sized units.
121 @param[in, out] Buffer The buffer to read into or write from.
125 IN SHELL_MM_ACCESS_TYPE AccessType
,
126 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL
*PciRootBridgeIo
,
127 IN EFI_CPU_IO2_PROTOCOL
*CpuIo
,
135 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_IO_MEM RootBridgeIoMem
;
136 EFI_CPU_IO_PROTOCOL_IO_MEM CpuIoMem
;
143 if (AccessType
== ShellMmMemory
) {
145 CopyMem (Buffer
, (VOID
*) (UINTN
) Address
, Size
);
147 CopyMem ((VOID
*) (UINTN
) Address
, Buffer
, Size
);
150 RootBridgeIoMem
= NULL
;
152 switch (AccessType
) {
154 case ShellMmPciExpress
:
155 ASSERT (PciRootBridgeIo
!= NULL
);
156 ShellMmDecodePciAddress ((BOOLEAN
) (AccessType
== ShellMmPci
), Address
, &Segment
, &Bus
, &Device
, &Function
, &Register
);
158 Status
= PciRootBridgeIo
->Pci
.Read (
159 PciRootBridgeIo
, mShellMmRootBridgeIoWidth
[Size
],
160 EFI_PCI_ADDRESS (Bus
, Device
, Function
, Register
),
164 Status
= PciRootBridgeIo
->Pci
.Write (
165 PciRootBridgeIo
, mShellMmRootBridgeIoWidth
[Size
],
166 EFI_PCI_ADDRESS (Bus
, Device
, Function
, Register
),
170 ASSERT_EFI_ERROR (Status
);
173 case ShellMmMemoryMappedIo
:
174 if (PciRootBridgeIo
!= NULL
) {
175 RootBridgeIoMem
= Read
? PciRootBridgeIo
->Mem
.Read
: PciRootBridgeIo
->Mem
.Write
;
178 CpuIoMem
= Read
? CpuIo
->Mem
.Read
: CpuIo
->Mem
.Write
;
183 if (PciRootBridgeIo
!= NULL
) {
184 RootBridgeIoMem
= Read
? PciRootBridgeIo
->Io
.Read
: PciRootBridgeIo
->Io
.Write
;
187 CpuIoMem
= Read
? CpuIo
->Io
.Read
: CpuIo
->Io
.Write
;
195 Status
= EFI_UNSUPPORTED
;
196 if (RootBridgeIoMem
!= NULL
) {
197 Status
= RootBridgeIoMem (PciRootBridgeIo
, mShellMmRootBridgeIoWidth
[Size
], Address
, 1, Buffer
);
199 if (EFI_ERROR (Status
) && (CpuIoMem
!= NULL
)) {
200 Status
= CpuIoMem (CpuIo
, mShellMmCpuIoWidth
[Size
], Address
, 1, Buffer
);
203 if (EFI_ERROR (Status
)) {
204 if (AccessType
== ShellMmIo
) {
208 *(UINT8
*) Buffer
= IoRead8 ((UINTN
) Address
);
210 IoWrite8 ((UINTN
) Address
, *(UINT8
*) Buffer
);
215 *(UINT16
*) Buffer
= IoRead16 ((UINTN
) Address
);
217 IoWrite16 ((UINTN
) Address
, *(UINT16
*) Buffer
);
222 *(UINT32
*) Buffer
= IoRead32 ((UINTN
) Address
);
224 IoWrite32 ((UINTN
) Address
, *(UINT32
*) Buffer
);
229 *(UINT64
*) Buffer
= IoRead64 ((UINTN
) Address
);
231 IoWrite64 ((UINTN
) Address
, *(UINT64
*) Buffer
);
242 *(UINT8
*) Buffer
= MmioRead8 ((UINTN
) Address
);
244 MmioWrite8 ((UINTN
) Address
, *(UINT8
*) Buffer
);
249 *(UINT16
*) Buffer
= MmioRead16 ((UINTN
) Address
);
251 MmioWrite16 ((UINTN
) Address
, *(UINT16
*) Buffer
);
256 *(UINT32
*) Buffer
= MmioRead32 ((UINTN
) Address
);
258 MmioWrite32 ((UINTN
) Address
, *(UINT32
*) Buffer
);
263 *(UINT64
*) Buffer
= MmioRead64 ((UINTN
) Address
);
265 MmioWrite64 ((UINTN
) Address
, *(UINT64
*) Buffer
);
278 Find the CpuIo instance and PciRootBridgeIo instance in the platform.
279 If there are multiple PciRootBridgeIo instances, the instance which manages
280 the Address is returned.
282 @param[in] AccessType Access type.
283 @param[in] Address Address to access.
284 @param[out] CpuIo Return the CpuIo instance.
285 @param[out] PciRootBridgeIo Return the proper PciRootBridgeIo instance.
287 @retval TRUE There are PciRootBridgeIo instances in the platform.
288 @retval FALSE There isn't PciRootBridgeIo instance in the platform.
291 ShellMmLocateIoProtocol (
292 IN SHELL_MM_ACCESS_TYPE AccessType
,
294 OUT EFI_CPU_IO2_PROTOCOL
**CpuIo
,
295 OUT EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL
**PciRootBridgeIo
301 EFI_HANDLE
*HandleBuffer
;
302 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL
*Io
;
305 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*Descriptors
;
307 Status
= gBS
->LocateProtocol (&gEfiCpuIo2ProtocolGuid
, NULL
, (VOID
**) CpuIo
);
308 if (EFI_ERROR (Status
)) {
312 *PciRootBridgeIo
= NULL
;
314 Status
= gBS
->LocateHandleBuffer (
316 &gEfiPciRootBridgeIoProtocolGuid
,
321 if (EFI_ERROR (Status
) || (HandleCount
== 0) || (HandleBuffer
== NULL
)) {
327 if ((AccessType
== ShellMmPci
) || (AccessType
== ShellMmPciExpress
)) {
328 ShellMmDecodePciAddress ((BOOLEAN
) (AccessType
== ShellMmPci
), Address
, &Segment
, &Bus
, NULL
, NULL
, NULL
);
332 // Find the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL of the specified segment & bus number
334 for (Index
= 0; (Index
< HandleCount
) && (*PciRootBridgeIo
== NULL
); Index
++) {
335 Status
= gBS
->HandleProtocol (
337 &gEfiPciRootBridgeIoProtocolGuid
,
340 if (EFI_ERROR (Status
)) {
344 if ((((AccessType
== ShellMmPci
) || (AccessType
== ShellMmPciExpress
)) && (Io
->SegmentNumber
== Segment
)) ||
345 ((AccessType
== ShellMmIo
) || (AccessType
== ShellMmMemoryMappedIo
))
347 Status
= Io
->Configuration (Io
, (VOID
**) &Descriptors
);
348 if (!EFI_ERROR (Status
)) {
349 while (Descriptors
->Desc
!= ACPI_END_TAG_DESCRIPTOR
) {
351 // Compare the segment and bus range for PCI/PCIE access
353 if ((Descriptors
->ResType
== ACPI_ADDRESS_SPACE_TYPE_BUS
) &&
354 ((AccessType
== ShellMmPci
) || (AccessType
== ShellMmPciExpress
)) &&
355 ((Bus
>= Descriptors
->AddrRangeMin
) && (Bus
<= Descriptors
->AddrRangeMax
))
357 *PciRootBridgeIo
= Io
;
361 // Compare the address range for MMIO/IO access
363 } else if ((((Descriptors
->ResType
== ACPI_ADDRESS_SPACE_TYPE_IO
) && (AccessType
== ShellMmIo
)) ||
364 ((Descriptors
->ResType
== ACPI_ADDRESS_SPACE_TYPE_MEM
) && (AccessType
== ShellMmMemoryMappedIo
))
365 ) && ((Address
>= Descriptors
->AddrRangeMin
) && (Address
<= Descriptors
->AddrRangeMax
))
367 *PciRootBridgeIo
= Io
;
375 if (HandleBuffer
!= NULL
) {
376 FreePool (HandleBuffer
);
383 Function for 'mm' command.
385 @param[in] ImageHandle Handle to the Image (NULL if Internal).
386 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
391 IN EFI_HANDLE ImageHandle
,
392 IN EFI_SYSTEM_TABLE
*SystemTable
396 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL
*PciRootBridgeIo
;
397 EFI_CPU_IO2_PROTOCOL
*CpuIo
;
400 SHELL_MM_ACCESS_TYPE AccessType
;
408 CHAR16
*ProblemParam
;
409 SHELL_STATUS ShellStatus
;
411 BOOLEAN HasPciRootBridgeIo
;
415 ShellStatus
= SHELL_SUCCESS
;
418 AccessType
= ShellMmMemory
;
423 Status
= ShellCommandLineParse (ParamList
, &Package
, &ProblemParam
, TRUE
);
424 if (EFI_ERROR (Status
)) {
425 if (Status
== EFI_VOLUME_CORRUPTED
&& ProblemParam
!= NULL
) {
426 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM
), gShellDebug1HiiHandle
, L
"mm", ProblemParam
);
427 FreePool (ProblemParam
);
428 ShellStatus
= SHELL_INVALID_PARAMETER
;
434 if (ShellCommandLineGetCount (Package
) < 2) {
435 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_FEW
), gShellDebug1HiiHandle
, L
"mm");
436 ShellStatus
= SHELL_INVALID_PARAMETER
;
438 } else if (ShellCommandLineGetCount (Package
) > 3) {
439 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_MANY
), gShellDebug1HiiHandle
, L
"mm");
440 ShellStatus
= SHELL_INVALID_PARAMETER
;
442 } else if (ShellCommandLineGetFlag (Package
, L
"-w") && ShellCommandLineGetValue (Package
, L
"-w") == NULL
) {
443 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_NO_VALUE
), gShellDebug1HiiHandle
, L
"mm", L
"-w");
444 ShellStatus
= SHELL_INVALID_PARAMETER
;
447 if (ShellCommandLineGetFlag (Package
, L
"-mmio")) {
448 AccessType
= ShellMmMemoryMappedIo
;
449 if (ShellCommandLineGetFlag (Package
, L
"-mem")
450 || ShellCommandLineGetFlag (Package
, L
"-io")
451 || ShellCommandLineGetFlag (Package
, L
"-pci")
452 || ShellCommandLineGetFlag (Package
, L
"-pcie")
454 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_MANY
), gShellDebug1HiiHandle
, L
"mm");
455 ShellStatus
= SHELL_INVALID_PARAMETER
;
458 } else if (ShellCommandLineGetFlag (Package
, L
"-mem")) {
459 AccessType
= ShellMmMemory
;
460 if (ShellCommandLineGetFlag (Package
, L
"-io")
461 || ShellCommandLineGetFlag (Package
, L
"-pci")
462 || ShellCommandLineGetFlag (Package
, L
"-pcie")
464 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_MANY
), gShellDebug1HiiHandle
, L
"mm");
465 ShellStatus
= SHELL_INVALID_PARAMETER
;
468 } else if (ShellCommandLineGetFlag (Package
, L
"-io")) {
469 AccessType
= ShellMmIo
;
470 if (ShellCommandLineGetFlag (Package
, L
"-pci")
471 || ShellCommandLineGetFlag (Package
, L
"-pcie")
473 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_MANY
), gShellDebug1HiiHandle
, L
"mm");
474 ShellStatus
= SHELL_INVALID_PARAMETER
;
477 } else if (ShellCommandLineGetFlag (Package
, L
"-pci")) {
478 AccessType
= ShellMmPci
;
479 if (ShellCommandLineGetFlag (Package
, L
"-pcie")
481 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_MANY
), gShellDebug1HiiHandle
, L
"mm");
482 ShellStatus
= SHELL_INVALID_PARAMETER
;
485 } else if (ShellCommandLineGetFlag (Package
, L
"-pcie")) {
486 AccessType
= ShellMmPciExpress
;
491 // Non interactive for a script file or for the specific parameter
494 if (gEfiShellProtocol
->BatchIsActive () || ShellCommandLineGetFlag (Package
, L
"-n")) {
498 Temp
= ShellCommandLineGetValue (Package
, L
"-w");
500 Size
= ShellStrToUintn (Temp
);
502 if ((Size
!= 1) && (Size
!= 2) && (Size
!= 4) && (Size
!= 8)) {
503 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM_VAL
), gShellDebug1HiiHandle
, L
"mm", Temp
, L
"-w");
504 ShellStatus
= SHELL_INVALID_PARAMETER
;
508 Temp
= ShellCommandLineGetRawValue (Package
, 1);
509 Status
= ShellConvertStringToUint64 (Temp
, &Address
, TRUE
, FALSE
);
510 if (EFI_ERROR (Status
)) {
511 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_PARAM_INV
), gShellDebug1HiiHandle
, L
"mm", Temp
);
512 ShellStatus
= SHELL_INVALID_PARAMETER
;
516 if ((Address
& (Size
- 1)) != 0) {
517 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_MM_NOT_ALIGNED
), gShellDebug1HiiHandle
, L
"mm", Address
);
518 ShellStatus
= SHELL_INVALID_PARAMETER
;
523 // locate IO protocol interface
525 HasPciRootBridgeIo
= ShellMmLocateIoProtocol (AccessType
, Address
, &CpuIo
, &PciRootBridgeIo
);
526 if ((AccessType
== ShellMmPci
) || (AccessType
== ShellMmPciExpress
)) {
527 if (!HasPciRootBridgeIo
) {
528 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_PCIRBIO_NF
), gShellDebug1HiiHandle
, L
"mm");
529 ShellStatus
= SHELL_NOT_FOUND
;
532 if (PciRootBridgeIo
== NULL
) {
533 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_MM_PCIE_ADDRESS_RANGE
), gShellDebug1HiiHandle
, L
"mm", Address
);
534 ShellStatus
= SHELL_INVALID_PARAMETER
;
540 // Mode 1: Directly set a value
542 Temp
= ShellCommandLineGetRawValue (Package
, 2);
544 Status
= ShellConvertStringToUint64 (Temp
, &Value
, TRUE
, FALSE
);
545 if (EFI_ERROR (Status
)) {
546 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_PARAM_INV
), gShellDebug1HiiHandle
, L
"mm", Temp
);
547 ShellStatus
= SHELL_INVALID_PARAMETER
;
551 if (Value
> mShellMmMaxNumber
[Size
]) {
552 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_PARAM_INV
), gShellDebug1HiiHandle
, L
"mm", Temp
);
553 ShellStatus
= SHELL_INVALID_PARAMETER
;
557 ShellMmAccess (AccessType
, PciRootBridgeIo
, CpuIo
, FALSE
, Address
, Size
, &Value
);
562 // Mode 2: Directly show a value
565 if (!gEfiShellProtocol
->BatchIsActive ()) {
566 ShellPrintHiiEx (-1, -1, NULL
, mShellMmAccessTypeStr
[AccessType
], gShellDebug1HiiHandle
);
568 ShellMmAccess (AccessType
, PciRootBridgeIo
, CpuIo
, TRUE
, Address
, Size
, &Buffer
);
570 if (!gEfiShellProtocol
->BatchIsActive ()) {
571 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_MM_ADDRESS
), gShellDebug1HiiHandle
, Address
);
573 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_MM_BUF
), gShellDebug1HiiHandle
, Size
* 2, Buffer
& mShellMmMaxNumber
[Size
]);
574 ShellPrintEx (-1, -1, L
"\r\n");
579 // Mode 3: Show or set values in interactive mode
583 ShellMmAccess (AccessType
, PciRootBridgeIo
, CpuIo
, TRUE
, Address
, Size
, &Buffer
);
584 ShellPrintHiiEx (-1, -1, NULL
, mShellMmAccessTypeStr
[AccessType
], gShellDebug1HiiHandle
);
585 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_MM_ADDRESS
), gShellDebug1HiiHandle
, Address
);
586 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_MM_BUF
), gShellDebug1HiiHandle
, Size
* 2, Buffer
& mShellMmMaxNumber
[Size
]);
587 ShellPrintEx (-1, -1, L
" > ");
589 // wait user input to modify
591 if (InputStr
!= NULL
) {
595 ShellPromptForResponse (ShellPromptResponseTypeFreeform
, NULL
, (VOID
**) &InputStr
);
597 if (InputStr
!= NULL
) {
599 // skip space characters
601 for (Index
= 0; InputStr
[Index
] == ' '; Index
++);
603 if (InputStr
[Index
] != CHAR_NULL
) {
604 if ((InputStr
[Index
] == '.') || (InputStr
[Index
] == 'q') || (InputStr
[Index
] == 'Q')) {
606 } else if (!EFI_ERROR (ShellConvertStringToUint64 (InputStr
+ Index
, &Buffer
, TRUE
, TRUE
)) &&
607 (Buffer
<= mShellMmMaxNumber
[Size
])
609 ShellMmAccess (AccessType
, PciRootBridgeIo
, CpuIo
, FALSE
, Address
, Size
, &Buffer
);
611 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_MM_ERROR
), gShellDebug1HiiHandle
, L
"mm");
618 ShellPrintEx (-1, -1, L
"\r\n");
621 ASSERT (ShellStatus
== SHELL_SUCCESS
);
624 if (InputStr
!= NULL
) {
627 if (Package
!= NULL
) {
628 ShellCommandLineFreeVarList (Package
);