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 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include "UefiShellDebug1CommandsLib.h"
17 #include <Library/ShellLib.h>
18 #include <Library/IoLib.h>
19 #include <Protocol/PciRootBridgeIo.h>
20 #include <Protocol/DeviceIo.h>
24 ShellMmMemoryMappedIo
,
28 } SHELL_MM_ACCESS_TYPE
;
30 CONST UINT16 mShellMmAccessTypeStr
[] = {
31 STRING_TOKEN (STR_MM_MEM
),
32 STRING_TOKEN (STR_MM_MMIO
),
33 STRING_TOKEN (STR_MM_IO
),
34 STRING_TOKEN (STR_MM_PCI
),
35 STRING_TOKEN (STR_MM_PCIE
)
38 STATIC CONST SHELL_PARAM_ITEM ParamList
[] = {
49 CONST UINT64 mShellMmMaxNumber
[] = {
50 0, MAX_UINT8
, MAX_UINT16
, 0, MAX_UINT32
, 0, 0, 0, MAX_UINT64
52 CONST EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH mShellMmRootBridgeIoWidth
[] = {
53 0, EfiPciWidthUint8
, EfiPciWidthUint16
, 0, EfiPciWidthUint32
, 0, 0, 0, EfiPciWidthUint64
55 CONST EFI_CPU_IO_PROTOCOL_WIDTH mShellMmCpuIoWidth
[] = {
56 0, EfiCpuIoWidthUint8
, EfiCpuIoWidthUint16
, 0, EfiCpuIoWidthUint32
, 0, 0, 0, EfiCpuIoWidthUint64
60 Extract the PCI segment, bus, device, function, register from
61 from a PCI or PCIE format of address..
63 @param[in] PciFormat Whether the address is of PCI format of PCIE format.
64 @param[in] Address PCI or PCIE address.
65 @param[out] Segment PCI segment number.
66 @param[out] Bus PCI bus number.
67 @param[out] Device PCI device number.
68 @param[out] Function PCI function number.
69 @param[out] Register PCI register offset.
72 ShellMmDecodePciAddress (
77 OUT UINT8
*Device
, OPTIONAL
78 OUT UINT8
*Function
, OPTIONAL
79 OUT UINT32
*Register OPTIONAL
84 // PCI Configuration Space.The address will have the format ssssbbddffrr,
85 // where ssss = Segment, bb = Bus, dd = Device, ff = Function and rr = Register.
87 *Segment
= (UINT32
) (RShiftU64 (Address
, 32) & 0xFFFF);
88 *Bus
= (UINT8
) (((UINT32
) Address
) >> 24);
91 *Device
= (UINT8
) (((UINT32
) Address
) >> 16);
93 if (Function
!= NULL
) {
94 *Function
= (UINT8
) (((UINT32
) Address
) >> 8);
96 if (Register
!= NULL
) {
97 *Register
= (UINT8
) Address
;
101 // PCI Express Configuration Space.The address will have the format ssssssbbddffrrr,
102 // where ssss = Segment, bb = Bus, dd = Device, ff = Function and rrr = Register.
104 *Segment
= (UINT32
) (RShiftU64 (Address
, 36) & 0xFFFF);
105 *Bus
= (UINT8
) RShiftU64 (Address
, 28);
106 if (Device
!= NULL
) {
107 *Device
= (UINT8
) (((UINT32
) Address
) >> 20);
109 if (Function
!= NULL
) {
110 *Function
= (UINT8
) (((UINT32
) Address
) >> 12);
112 if (Register
!= NULL
) {
113 *Register
= (UINT32
) (Address
& 0xFFF);
119 Read or write some data from or into the Address.
121 @param[in] AccessType Access type.
122 @param[in] PciRootBridgeIo PciRootBridgeIo instance.
123 @param[in] CpuIo CpuIo instance.
124 @param[in] Read TRUE for read, FALSE for write.
125 @param[in] Addresss The memory location to access.
126 @param[in] Size The size of Buffer in Width sized units.
127 @param[in, out] Buffer The buffer to read into or write from.
131 IN SHELL_MM_ACCESS_TYPE AccessType
,
132 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL
*PciRootBridgeIo
,
133 IN EFI_CPU_IO2_PROTOCOL
*CpuIo
,
141 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_IO_MEM RootBridgeIoMem
;
142 EFI_CPU_IO_PROTOCOL_IO_MEM CpuIoMem
;
149 if (AccessType
== ShellMmMemory
) {
151 CopyMem (Buffer
, (VOID
*) (UINTN
) Address
, Size
);
153 CopyMem ((VOID
*) (UINTN
) Address
, Buffer
, Size
);
156 RootBridgeIoMem
= NULL
;
158 switch (AccessType
) {
160 case ShellMmPciExpress
:
161 ASSERT (PciRootBridgeIo
!= NULL
);
162 ShellMmDecodePciAddress ((BOOLEAN
) (AccessType
== ShellMmPci
), Address
, &Segment
, &Bus
, &Device
, &Function
, &Register
);
164 Status
= PciRootBridgeIo
->Pci
.Read (
165 PciRootBridgeIo
, mShellMmRootBridgeIoWidth
[Size
],
166 EFI_PCI_ADDRESS (Bus
, Device
, Function
, Register
),
170 Status
= PciRootBridgeIo
->Pci
.Write (
171 PciRootBridgeIo
, mShellMmRootBridgeIoWidth
[Size
],
172 EFI_PCI_ADDRESS (Bus
, Device
, Function
, Register
),
176 ASSERT_EFI_ERROR (Status
);
179 case ShellMmMemoryMappedIo
:
180 if (PciRootBridgeIo
!= NULL
) {
181 RootBridgeIoMem
= Read
? PciRootBridgeIo
->Mem
.Read
: PciRootBridgeIo
->Mem
.Write
;
184 CpuIoMem
= Read
? CpuIo
->Mem
.Read
: CpuIo
->Mem
.Write
;
189 if (PciRootBridgeIo
!= NULL
) {
190 RootBridgeIoMem
= Read
? PciRootBridgeIo
->Io
.Read
: PciRootBridgeIo
->Io
.Write
;
193 CpuIoMem
= Read
? CpuIo
->Io
.Read
: CpuIo
->Io
.Write
;
201 Status
= EFI_UNSUPPORTED
;
202 if (RootBridgeIoMem
!= NULL
) {
203 Status
= RootBridgeIoMem (PciRootBridgeIo
, mShellMmRootBridgeIoWidth
[Size
], Address
, 1, Buffer
);
205 if (EFI_ERROR (Status
) && (CpuIoMem
!= NULL
)) {
206 Status
= CpuIoMem (CpuIo
, mShellMmCpuIoWidth
[Size
], Address
, 1, Buffer
);
209 if (EFI_ERROR (Status
)) {
210 if (AccessType
== ShellMmIo
) {
214 *(UINT8
*) Buffer
= IoRead8 ((UINTN
) Address
);
216 IoWrite8 ((UINTN
) Address
, *(UINT8
*) Buffer
);
221 *(UINT16
*) Buffer
= IoRead16 ((UINTN
) Address
);
223 IoWrite16 ((UINTN
) Address
, *(UINT16
*) Buffer
);
228 *(UINT32
*) Buffer
= IoRead32 ((UINTN
) Address
);
230 IoWrite32 ((UINTN
) Address
, *(UINT32
*) Buffer
);
235 *(UINT64
*) Buffer
= IoRead64 ((UINTN
) Address
);
237 IoWrite64 ((UINTN
) Address
, *(UINT64
*) Buffer
);
248 *(UINT8
*) Buffer
= MmioRead8 ((UINTN
) Address
);
250 MmioWrite8 ((UINTN
) Address
, *(UINT8
*) Buffer
);
255 *(UINT16
*) Buffer
= MmioRead16 ((UINTN
) Address
);
257 MmioWrite16 ((UINTN
) Address
, *(UINT16
*) Buffer
);
262 *(UINT32
*) Buffer
= MmioRead32 ((UINTN
) Address
);
264 MmioWrite32 ((UINTN
) Address
, *(UINT32
*) Buffer
);
269 *(UINT64
*) Buffer
= MmioRead64 ((UINTN
) Address
);
271 MmioWrite64 ((UINTN
) Address
, *(UINT64
*) Buffer
);
284 Find the CpuIo instance and PciRootBridgeIo instance in the platform.
285 If there are multiple PciRootBridgeIo instances, the instance which manages
286 the Address is returned.
288 @param[in] AccessType Access type.
289 @param[in] Address Address to access.
290 @param[out] CpuIo Return the CpuIo instance.
291 @param[out] PciRootBridgeIo Return the proper PciRootBridgeIo instance.
293 @retval TRUE There are PciRootBridgeIo instances in the platform.
294 @retval FALSE There isn't PciRootBridgeIo instance in the platform.
297 ShellMmLocateIoProtocol (
298 IN SHELL_MM_ACCESS_TYPE AccessType
,
300 OUT EFI_CPU_IO2_PROTOCOL
**CpuIo
,
301 OUT EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL
**PciRootBridgeIo
307 EFI_HANDLE
*HandleBuffer
;
308 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL
*Io
;
311 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*Descriptors
;
313 Status
= gBS
->LocateProtocol (&gEfiCpuIo2ProtocolGuid
, NULL
, (VOID
**) CpuIo
);
314 if (EFI_ERROR (Status
)) {
318 *PciRootBridgeIo
= NULL
;
320 Status
= gBS
->LocateHandleBuffer (
322 &gEfiPciRootBridgeIoProtocolGuid
,
327 if (EFI_ERROR (Status
) || (HandleCount
== 0) || (HandleBuffer
== NULL
)) {
333 if ((AccessType
== ShellMmPci
) || (AccessType
== ShellMmPciExpress
)) {
334 ShellMmDecodePciAddress ((BOOLEAN
) (AccessType
== ShellMmPci
), Address
, &Segment
, &Bus
, NULL
, NULL
, NULL
);
338 // Find the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL of the specified segment & bus number
340 for (Index
= 0; (Index
< HandleCount
) && (*PciRootBridgeIo
== NULL
); Index
++) {
341 Status
= gBS
->HandleProtocol (
343 &gEfiPciRootBridgeIoProtocolGuid
,
346 if (EFI_ERROR (Status
)) {
350 if ((((AccessType
== ShellMmPci
) || (AccessType
== ShellMmPciExpress
)) && (Io
->SegmentNumber
== Segment
)) ||
351 ((AccessType
== ShellMmIo
) || (AccessType
== ShellMmMemoryMappedIo
))
353 Status
= Io
->Configuration (Io
, (VOID
**) &Descriptors
);
354 if (!EFI_ERROR (Status
)) {
355 while (Descriptors
->Desc
!= ACPI_END_TAG_DESCRIPTOR
) {
357 // Compare the segment and bus range for PCI/PCIE access
359 if ((Descriptors
->ResType
== ACPI_ADDRESS_SPACE_TYPE_BUS
) &&
360 ((AccessType
== ShellMmPci
) || (AccessType
== ShellMmPciExpress
)) &&
361 ((Bus
>= Descriptors
->AddrRangeMin
) && (Bus
<= Descriptors
->AddrRangeMax
))
363 *PciRootBridgeIo
= Io
;
367 // Compare the address range for MMIO/IO access
369 } else if ((((Descriptors
->ResType
== ACPI_ADDRESS_SPACE_TYPE_IO
) && (AccessType
== ShellMmIo
)) ||
370 ((Descriptors
->ResType
== ACPI_ADDRESS_SPACE_TYPE_MEM
) && (AccessType
== ShellMmMemoryMappedIo
))
371 ) && ((Address
>= Descriptors
->AddrRangeMin
) && (Address
<= Descriptors
->AddrRangeMax
))
373 *PciRootBridgeIo
= Io
;
381 if (HandleBuffer
!= NULL
) {
382 FreePool (HandleBuffer
);
389 Function for 'mm' command.
391 @param[in] ImageHandle Handle to the Image (NULL if Internal).
392 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
397 IN EFI_HANDLE ImageHandle
,
398 IN EFI_SYSTEM_TABLE
*SystemTable
402 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL
*PciRootBridgeIo
;
403 EFI_CPU_IO2_PROTOCOL
*CpuIo
;
406 SHELL_MM_ACCESS_TYPE AccessType
;
414 CHAR16
*ProblemParam
;
415 SHELL_STATUS ShellStatus
;
417 BOOLEAN HasPciRootBridgeIo
;
421 ShellStatus
= SHELL_SUCCESS
;
424 AccessType
= ShellMmMemory
;
429 Status
= ShellCommandLineParse (ParamList
, &Package
, &ProblemParam
, TRUE
);
430 if (EFI_ERROR (Status
)) {
431 if (Status
== EFI_VOLUME_CORRUPTED
&& ProblemParam
!= NULL
) {
432 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM
), gShellDebug1HiiHandle
, L
"mm", ProblemParam
);
433 FreePool (ProblemParam
);
434 ShellStatus
= SHELL_INVALID_PARAMETER
;
440 if (ShellCommandLineGetCount (Package
) < 2) {
441 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_FEW
), gShellDebug1HiiHandle
, L
"mm");
442 ShellStatus
= SHELL_INVALID_PARAMETER
;
444 } else if (ShellCommandLineGetCount (Package
) > 3) {
445 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_MANY
), gShellDebug1HiiHandle
, L
"mm");
446 ShellStatus
= SHELL_INVALID_PARAMETER
;
448 } else if (ShellCommandLineGetFlag (Package
, L
"-w") && ShellCommandLineGetValue (Package
, L
"-w") == NULL
) {
449 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_NO_VALUE
), gShellDebug1HiiHandle
, L
"mm", L
"-w");
450 ShellStatus
= SHELL_INVALID_PARAMETER
;
453 if (ShellCommandLineGetFlag (Package
, L
"-mmio")) {
454 AccessType
= ShellMmMemoryMappedIo
;
455 if (ShellCommandLineGetFlag (Package
, L
"-mem")
456 || ShellCommandLineGetFlag (Package
, L
"-io")
457 || ShellCommandLineGetFlag (Package
, L
"-pci")
458 || ShellCommandLineGetFlag (Package
, L
"-pcie")
460 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_MANY
), gShellDebug1HiiHandle
, L
"mm");
461 ShellStatus
= SHELL_INVALID_PARAMETER
;
464 } else if (ShellCommandLineGetFlag (Package
, L
"-mem")) {
465 AccessType
= ShellMmMemory
;
466 if (ShellCommandLineGetFlag (Package
, L
"-io")
467 || ShellCommandLineGetFlag (Package
, L
"-pci")
468 || ShellCommandLineGetFlag (Package
, L
"-pcie")
470 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_MANY
), gShellDebug1HiiHandle
, L
"mm");
471 ShellStatus
= SHELL_INVALID_PARAMETER
;
474 } else if (ShellCommandLineGetFlag (Package
, L
"-io")) {
475 AccessType
= ShellMmIo
;
476 if (ShellCommandLineGetFlag (Package
, L
"-pci")
477 || ShellCommandLineGetFlag (Package
, L
"-pcie")
479 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_MANY
), gShellDebug1HiiHandle
, L
"mm");
480 ShellStatus
= SHELL_INVALID_PARAMETER
;
483 } else if (ShellCommandLineGetFlag (Package
, L
"-pci")) {
484 AccessType
= ShellMmPci
;
485 if (ShellCommandLineGetFlag (Package
, L
"-pcie")
487 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_MANY
), gShellDebug1HiiHandle
, L
"mm");
488 ShellStatus
= SHELL_INVALID_PARAMETER
;
491 } else if (ShellCommandLineGetFlag (Package
, L
"-pcie")) {
492 AccessType
= ShellMmPciExpress
;
497 // Non interactive for a script file or for the specific parameter
500 if (gEfiShellProtocol
->BatchIsActive () || ShellCommandLineGetFlag (Package
, L
"-n")) {
504 Temp
= ShellCommandLineGetValue (Package
, L
"-w");
506 Size
= ShellStrToUintn (Temp
);
508 if ((Size
!= 1) && (Size
!= 2) && (Size
!= 4) && (Size
!= 8)) {
509 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM_VAL
), gShellDebug1HiiHandle
, L
"mm", Temp
, L
"-w");
510 ShellStatus
= SHELL_INVALID_PARAMETER
;
514 Temp
= ShellCommandLineGetRawValue (Package
, 1);
515 Status
= ShellConvertStringToUint64 (Temp
, &Address
, TRUE
, FALSE
);
516 if (EFI_ERROR (Status
)) {
517 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_PARAM_INV
), gShellDebug1HiiHandle
, L
"mm", Temp
);
518 ShellStatus
= SHELL_INVALID_PARAMETER
;
522 if ((Address
& (Size
- 1)) != 0) {
523 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_MM_NOT_ALIGNED
), gShellDebug1HiiHandle
, L
"mm", Address
);
524 ShellStatus
= SHELL_INVALID_PARAMETER
;
528 if ((AccessType
== ShellMmIo
) && (Address
+ Size
> MAX_UINT16
+ 1)) {
529 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_MM_IO_ADDRESS_RANGE
), gShellDebug1HiiHandle
, L
"mm");
530 ShellStatus
= SHELL_INVALID_PARAMETER
;
535 // locate IO protocol interface
537 HasPciRootBridgeIo
= ShellMmLocateIoProtocol (AccessType
, Address
, &CpuIo
, &PciRootBridgeIo
);
538 if ((AccessType
== ShellMmPci
) || (AccessType
== ShellMmPciExpress
)) {
539 if (!HasPciRootBridgeIo
) {
540 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_PCIRBIO_NF
), gShellDebug1HiiHandle
, L
"mm");
541 ShellStatus
= SHELL_NOT_FOUND
;
544 if (PciRootBridgeIo
== NULL
) {
545 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_MM_PCIE_ADDRESS_RANGE
), gShellDebug1HiiHandle
, L
"mm", Address
);
546 ShellStatus
= SHELL_INVALID_PARAMETER
;
552 // Mode 1: Directly set a value
554 Temp
= ShellCommandLineGetRawValue (Package
, 2);
556 Status
= ShellConvertStringToUint64 (Temp
, &Value
, TRUE
, FALSE
);
557 if (EFI_ERROR (Status
)) {
558 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_PARAM_INV
), gShellDebug1HiiHandle
, L
"mm", Temp
);
559 ShellStatus
= SHELL_INVALID_PARAMETER
;
563 if (Value
> mShellMmMaxNumber
[Size
]) {
564 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_PARAM_INV
), gShellDebug1HiiHandle
, L
"mm", Temp
);
565 ShellStatus
= SHELL_INVALID_PARAMETER
;
569 ShellMmAccess (AccessType
, PciRootBridgeIo
, CpuIo
, FALSE
, Address
, Size
, &Value
);
574 // Mode 2: Directly show a value
577 if (!gEfiShellProtocol
->BatchIsActive ()) {
578 ShellPrintHiiEx (-1, -1, NULL
, mShellMmAccessTypeStr
[AccessType
], gShellDebug1HiiHandle
);
580 ShellMmAccess (AccessType
, PciRootBridgeIo
, CpuIo
, TRUE
, Address
, Size
, &Buffer
);
582 if (!gEfiShellProtocol
->BatchIsActive ()) {
583 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_MM_ADDRESS
), gShellDebug1HiiHandle
, Address
);
585 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_MM_BUF
), gShellDebug1HiiHandle
, Size
* 2, Buffer
& mShellMmMaxNumber
[Size
]);
586 ShellPrintEx (-1, -1, L
"\r\n");
591 // Mode 3: Show or set values in interactive mode
595 if ((AccessType
== ShellMmIo
) && (Address
+ Size
> MAX_UINT16
+ 1)) {
596 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_MM_ADDRESS_RANGE2
), gShellDebug1HiiHandle
, L
"mm");
600 ShellMmAccess (AccessType
, PciRootBridgeIo
, CpuIo
, TRUE
, Address
, Size
, &Buffer
);
601 ShellPrintHiiEx (-1, -1, NULL
, mShellMmAccessTypeStr
[AccessType
], gShellDebug1HiiHandle
);
602 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_MM_ADDRESS
), gShellDebug1HiiHandle
, Address
);
603 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_MM_BUF
), gShellDebug1HiiHandle
, Size
* 2, Buffer
& mShellMmMaxNumber
[Size
]);
604 ShellPrintEx (-1, -1, L
" > ");
606 // wait user input to modify
608 if (InputStr
!= NULL
) {
612 ShellPromptForResponse (ShellPromptResponseTypeFreeform
, NULL
, (VOID
**) &InputStr
);
614 if (InputStr
!= NULL
) {
616 // skip space characters
618 for (Index
= 0; InputStr
[Index
] == ' '; Index
++);
620 if (InputStr
[Index
] != CHAR_NULL
) {
621 if ((InputStr
[Index
] == '.') || (InputStr
[Index
] == 'q') || (InputStr
[Index
] == 'Q')) {
623 } else if (!EFI_ERROR (ShellConvertStringToUint64 (InputStr
+ Index
, &Buffer
, TRUE
, TRUE
)) &&
624 (Buffer
<= mShellMmMaxNumber
[Size
])
626 ShellMmAccess (AccessType
, PciRootBridgeIo
, CpuIo
, FALSE
, Address
, Size
, &Buffer
);
628 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_MM_ERROR
), gShellDebug1HiiHandle
, L
"mm");
635 ShellPrintEx (-1, -1, L
"\r\n");
638 ASSERT (ShellStatus
== SHELL_SUCCESS
);
641 if (InputStr
!= NULL
) {
644 if (Package
!= NULL
) {
645 ShellCommandLineFreeVarList (Package
);