]> git.proxmox.com Git - mirror_edk2.git/blob - ShellPkg/Library/UefiShellDebug1CommandsLib/Mm.c
ShellPkg: Initialize the local pointer to avoid potential suspicious dereference.
[mirror_edk2.git] / ShellPkg / Library / UefiShellDebug1CommandsLib / Mm.c
1 /** @file
2 Main file for Mm shell Debug1 function.
3
4 (C) Copyright 2015 Hewlett-Packard Development Company, L.P.<BR>
5 Copyright (c) 2005 - 2015, 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
10
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.
13
14 **/
15
16 #include "UefiShellDebug1CommandsLib.h"
17 #include <Library/ShellLib.h>
18 #include <Library/IoLib.h>
19 #include <Protocol/PciRootBridgeIo.h>
20 #include <Protocol/DeviceIo.h>
21
22 typedef enum {
23 ShellMmMemory,
24 ShellMmMemoryMappedIo,
25 ShellMmIo,
26 ShellMmPci,
27 ShellMmPciExpress
28 } SHELL_MM_ACCESS_TYPE;
29
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)
36 };
37
38 STATIC CONST SHELL_PARAM_ITEM ParamList[] = {
39 {L"-mmio", TypeFlag},
40 {L"-mem", TypeFlag},
41 {L"-io", TypeFlag},
42 {L"-pci", TypeFlag},
43 {L"-pcie", TypeFlag},
44 {L"-n", TypeFlag},
45 {L"-w", TypeValue},
46 {NULL, TypeMax}
47 };
48
49 CONST UINT64 mShellMmMaxNumber[] = {
50 0, MAX_UINT8, MAX_UINT16, 0, MAX_UINT32, 0, 0, 0, MAX_UINT64
51 };
52 CONST EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH mShellMmRootBridgeIoWidth[] = {
53 0, EfiPciWidthUint8, EfiPciWidthUint16, 0, EfiPciWidthUint32, 0, 0, 0, EfiPciWidthUint64
54 };
55 CONST EFI_CPU_IO_PROTOCOL_WIDTH mShellMmCpuIoWidth[] = {
56 0, EfiCpuIoWidthUint8, EfiCpuIoWidthUint16, 0, EfiCpuIoWidthUint32, 0, 0, 0, EfiCpuIoWidthUint64
57 };
58
59 /**
60 Extract the PCI segment, bus, device, function, register from
61 from a SHELL_MM_PCI or SHELL_MM_PCIE format of address..
62
63 @param[in] PciFormat Whether the address is of PCI format of PCIE format.
64 @param[in] Address SHELL_MM_PCI or SHELL_MM_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.
70 **/
71 VOID
72 EFIAPI
73 ShellMmDecodePciAddress (
74 IN BOOLEAN PciFormat,
75 IN UINT64 Address,
76 OUT UINT32 *Segment,
77 OUT UINT8 *Bus,
78 OUT UINT8 *Device, OPTIONAL
79 OUT UINT8 *Function, OPTIONAL
80 OUT UINT32 *Register OPTIONAL
81 )
82 {
83 if (PciFormat) {
84 //
85 // PCI Configuration Space.The address will have the format 0x000000ssbbddffrr,
86 // where ss = Segment, bb = Bus, dd = Device, ff = Function and rr = Register.
87 //
88 *Segment = (UINT32) (RShiftU64 (Address, 32) & 0xFF);
89 *Bus = (UINT8) (((UINT32) Address) >> 24);
90
91 if (Device != NULL) {
92 *Device = (UINT8) (((UINT32) Address) >> 16);
93 }
94 if (Function != NULL) {
95 *Function = (UINT8) (((UINT32) Address) >> 8);
96 }
97 if (Register != NULL) {
98 *Register = (UINT8) Address;
99 }
100 } else {
101 //
102 // PCI Express Configuration Space.The address will have the format 0x0000000ssbbddffrrr,
103 // where ss = Segment, bb = Bus, dd = Device, ff = Function and rrr = Register.
104 //
105 *Segment = (UINT32) (RShiftU64 (Address, 36) & 0xFF);
106 *Bus = (UINT8) RShiftU64 (Address, 28);
107 if (Device != NULL) {
108 *Device = (UINT8) (((UINT32) Address) >> 20);
109 }
110 if (Function != NULL) {
111 *Function = (UINT8) (((UINT32) Address) >> 12);
112 }
113 if (Register != NULL) {
114 *Register = (UINT32) (Address & 0xFFF);
115 }
116 }
117 }
118
119 /**
120 Read or write some data from or into the Address.
121
122 @param[in] AccessType Access type.
123 @param[in] PciRootBridgeIo PciRootBridgeIo instance.
124 @param[in] CpuIo CpuIo instance.
125 @param[in] Read TRUE for read, FALSE for write.
126 @param[in] Addresss The memory location to access.
127 @param[in] Size The size of Buffer in Width sized units.
128 @param[in, out] Buffer The buffer to read into or write from.
129 **/
130 VOID
131 ShellMmAccess (
132 IN SHELL_MM_ACCESS_TYPE AccessType,
133 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *PciRootBridgeIo,
134 IN EFI_CPU_IO2_PROTOCOL *CpuIo,
135 IN BOOLEAN Read,
136 IN UINT64 Address,
137 IN UINTN Size,
138 IN OUT VOID *Buffer
139 )
140 {
141 EFI_STATUS Status;
142 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_IO_MEM RootBridgeIoMem;
143 EFI_CPU_IO_PROTOCOL_IO_MEM CpuIoMem;
144 UINT32 Segment;
145 UINT8 Bus;
146 UINT8 Device;
147 UINT8 Function;
148 UINT32 Register;
149
150 if (AccessType == ShellMmMemory) {
151 if (Read) {
152 CopyMem (Buffer, (VOID *) (UINTN) Address, Size);
153 } else {
154 CopyMem ((VOID *) (UINTN) Address, Buffer, Size);
155 }
156 } else {
157 RootBridgeIoMem = NULL;
158 CpuIoMem = NULL;
159 switch (AccessType) {
160 case ShellMmPci:
161 case ShellMmPciExpress:
162 ASSERT (PciRootBridgeIo != NULL);
163 ShellMmDecodePciAddress ((BOOLEAN) (AccessType == ShellMmPci), Address, &Segment, &Bus, &Device, &Function, &Register);
164 if (Read) {
165 Status = PciRootBridgeIo->Pci.Read (
166 PciRootBridgeIo, mShellMmRootBridgeIoWidth[Size],
167 EFI_PCI_ADDRESS (Bus, Device, Function, Register),
168 1, Buffer
169 );
170 } else {
171 Status = PciRootBridgeIo->Pci.Write (
172 PciRootBridgeIo, mShellMmRootBridgeIoWidth[Size],
173 EFI_PCI_ADDRESS (Bus, Device, Function, Register),
174 1, Buffer
175 );
176 }
177 ASSERT_EFI_ERROR (Status);
178 return;
179
180 case ShellMmMemoryMappedIo:
181 if (PciRootBridgeIo != NULL) {
182 RootBridgeIoMem = Read ? PciRootBridgeIo->Mem.Read : PciRootBridgeIo->Mem.Write;
183 }
184 if (CpuIo != NULL) {
185 CpuIoMem = Read ? CpuIo->Mem.Read : CpuIo->Mem.Write;
186 }
187 break;
188
189 case ShellMmIo:
190 if (PciRootBridgeIo != NULL) {
191 RootBridgeIoMem = Read ? PciRootBridgeIo->Io.Read : PciRootBridgeIo->Io.Write;
192 }
193 if (CpuIo != NULL) {
194 CpuIoMem = Read ? CpuIo->Io.Read : CpuIo->Io.Write;
195 }
196 break;
197 default:
198 ASSERT (FALSE);
199 break;
200 }
201
202 Status = EFI_UNSUPPORTED;
203 if (RootBridgeIoMem != NULL) {
204 Status = RootBridgeIoMem (PciRootBridgeIo, mShellMmRootBridgeIoWidth[Size], Address, 1, Buffer);
205 }
206 if (EFI_ERROR (Status) && (CpuIoMem != NULL)) {
207 Status = CpuIoMem (CpuIo, mShellMmCpuIoWidth[Size], Address, 1, Buffer);
208 }
209
210 if (EFI_ERROR (Status)) {
211 if (AccessType == ShellMmIo) {
212 switch (Size) {
213 case 1:
214 if (Read) {
215 *(UINT8 *) Buffer = IoRead8 ((UINTN) Address);
216 } else {
217 IoWrite8 ((UINTN) Address, *(UINT8 *) Buffer);
218 }
219 break;
220 case 2:
221 if (Read) {
222 *(UINT16 *) Buffer = IoRead16 ((UINTN) Address);
223 } else {
224 IoWrite16 ((UINTN) Address, *(UINT16 *) Buffer);
225 }
226 break;
227 case 4:
228 if (Read) {
229 *(UINT32 *) Buffer = IoRead32 ((UINTN) Address);
230 } else {
231 IoWrite32 ((UINTN) Address, *(UINT32 *) Buffer);
232 }
233 break;
234 case 8:
235 if (Read) {
236 *(UINT64 *) Buffer = IoRead64 ((UINTN) Address);
237 } else {
238 IoWrite64 ((UINTN) Address, *(UINT64 *) Buffer);
239 }
240 break;
241 default:
242 ASSERT (FALSE);
243 break;
244 }
245 } else {
246 switch (Size) {
247 case 1:
248 if (Read) {
249 *(UINT8 *) Buffer = MmioRead8 ((UINTN) Address);
250 } else {
251 MmioWrite8 ((UINTN) Address, *(UINT8 *) Buffer);
252 }
253 break;
254 case 2:
255 if (Read) {
256 *(UINT16 *) Buffer = MmioRead16 ((UINTN) Address);
257 } else {
258 MmioWrite16 ((UINTN) Address, *(UINT16 *) Buffer);
259 }
260 break;
261 case 4:
262 if (Read) {
263 *(UINT32 *) Buffer = MmioRead32 ((UINTN) Address);
264 } else {
265 MmioWrite32 ((UINTN) Address, *(UINT32 *) Buffer);
266 }
267 break;
268 case 8:
269 if (Read) {
270 *(UINT64 *) Buffer = MmioRead64 ((UINTN) Address);
271 } else {
272 MmioWrite64 ((UINTN) Address, *(UINT64 *) Buffer);
273 }
274 break;
275 default:
276 ASSERT (FALSE);
277 break;
278 }
279 }
280 }
281 }
282 }
283
284 /**
285 Find the CpuIo instance and PciRootBridgeIo instance in the platform.
286 If there are multiple PciRootBridgeIo instances, the instance which manages
287 the Address is returned.
288
289 @param[in] AccessType Access type.
290 @param[in] Address Address to access.
291 @param[out] CpuIo Return the CpuIo instance.
292 @param[out] PciRootBridgeIo Return the proper PciRootBridgeIo instance.
293
294 @retval TRUE There are PciRootBridgeIo instances in the platform.
295 @retval FALSE There isn't PciRootBridgeIo instance in the platform.
296 **/
297 BOOLEAN
298 ShellMmLocateIoProtocol (
299 IN SHELL_MM_ACCESS_TYPE AccessType,
300 IN UINT64 Address,
301 OUT EFI_CPU_IO2_PROTOCOL **CpuIo,
302 OUT EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL **PciRootBridgeIo
303 )
304 {
305 EFI_STATUS Status;
306 UINTN Index;
307 UINTN HandleCount;
308 EFI_HANDLE *HandleBuffer;
309 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *Io;
310 UINT32 Segment;
311 UINT8 Bus;
312 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *Descriptors;
313
314 Status = gBS->LocateProtocol (&gEfiCpuIo2ProtocolGuid, NULL, (VOID **) CpuIo);
315 if (EFI_ERROR (Status)) {
316 *CpuIo = NULL;
317 }
318
319 *PciRootBridgeIo = NULL;
320 HandleBuffer = NULL;
321 Status = gBS->LocateHandleBuffer (
322 ByProtocol,
323 &gEfiPciRootBridgeIoProtocolGuid,
324 NULL,
325 &HandleCount,
326 &HandleBuffer
327 );
328 if (EFI_ERROR (Status) || (HandleCount == 0) || (HandleBuffer == NULL)) {
329 return FALSE;
330 }
331
332 if ((AccessType == ShellMmPci) || (AccessType == ShellMmPciExpress)) {
333 ShellMmDecodePciAddress ((BOOLEAN) (AccessType == ShellMmPci), Address, &Segment, &Bus, NULL, NULL, NULL);
334 }
335
336 //
337 // Find the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL of the specified segment & bus number
338 //
339 for (Index = 0; (Index < HandleCount) && (*PciRootBridgeIo == NULL); Index++) {
340 Status = gBS->HandleProtocol (
341 HandleBuffer[Index],
342 &gEfiPciRootBridgeIoProtocolGuid,
343 (VOID *) &Io
344 );
345 if (EFI_ERROR (Status)) {
346 continue;
347 }
348
349 if ((((AccessType == ShellMmPci) || (AccessType == ShellMmPciExpress)) && (Io->SegmentNumber == Segment)) ||
350 ((AccessType == ShellMmIo) || (AccessType == ShellMmMemoryMappedIo))
351 ) {
352 Status = Io->Configuration (Io, (VOID **) &Descriptors);
353 if (!EFI_ERROR (Status)) {
354 while (Descriptors->Desc != ACPI_END_TAG_DESCRIPTOR) {
355 //
356 // Compare the segment and bus range for PCI/PCIE access
357 //
358 if ((Descriptors->ResType == ACPI_ADDRESS_SPACE_TYPE_BUS) &&
359 ((AccessType == ShellMmPci) || (AccessType == ShellMmPciExpress)) &&
360 ((Bus >= Descriptors->AddrRangeMin) && (Bus <= Descriptors->AddrRangeMax))
361 ) {
362 *PciRootBridgeIo = Io;
363 break;
364
365 //
366 // Compare the address range for MMIO/IO access
367 //
368 } else if ((((Descriptors->ResType == ACPI_ADDRESS_SPACE_TYPE_IO) && (AccessType == ShellMmIo)) ||
369 ((Descriptors->ResType == ACPI_ADDRESS_SPACE_TYPE_MEM) && (AccessType == ShellMmMemoryMappedIo))
370 ) && ((Address >= Descriptors->AddrRangeMin) && (Address <= Descriptors->AddrRangeMax))
371 ) {
372 *PciRootBridgeIo = Io;
373 break;
374 }
375 Descriptors++;
376 }
377 }
378 }
379 }
380 if (HandleBuffer != NULL) {
381 FreePool (HandleBuffer);
382 }
383
384 return TRUE;
385 }
386
387 /**
388 Function for 'mm' command.
389
390 @param[in] ImageHandle Handle to the Image (NULL if Internal).
391 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
392 **/
393 SHELL_STATUS
394 EFIAPI
395 ShellCommandRunMm (
396 IN EFI_HANDLE ImageHandle,
397 IN EFI_SYSTEM_TABLE *SystemTable
398 )
399 {
400 EFI_STATUS Status;
401 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *PciRootBridgeIo;
402 EFI_CPU_IO2_PROTOCOL *CpuIo;
403 UINT64 Address;
404 UINT64 Value;
405 SHELL_MM_ACCESS_TYPE AccessType;
406 UINT64 Buffer;
407 UINTN Index;
408 UINTN Size;
409 BOOLEAN Complete;
410 CHAR16 *InputStr;
411 BOOLEAN Interactive;
412 LIST_ENTRY *Package;
413 CHAR16 *ProblemParam;
414 SHELL_STATUS ShellStatus;
415 CONST CHAR16 *Temp;
416 BOOLEAN HasPciRootBridgeIo;
417
418 Value = 0;
419 Address = 0;
420 ShellStatus = SHELL_SUCCESS;
421 InputStr = NULL;
422 Size = 1;
423 AccessType = ShellMmMemory;
424
425 //
426 // Parse arguments
427 //
428 Status = ShellCommandLineParse (ParamList, &Package, &ProblemParam, TRUE);
429 if (EFI_ERROR (Status)) {
430 if (Status == EFI_VOLUME_CORRUPTED && ProblemParam != NULL) {
431 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellDebug1HiiHandle, L"mm", ProblemParam);
432 FreePool (ProblemParam);
433 ShellStatus = SHELL_INVALID_PARAMETER;
434 goto Done;
435 } else {
436 ASSERT (FALSE);
437 }
438 } else {
439 if (ShellCommandLineGetCount (Package) < 2) {
440 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellDebug1HiiHandle, L"mm");
441 ShellStatus = SHELL_INVALID_PARAMETER;
442 goto Done;
443 } else if (ShellCommandLineGetCount (Package) > 3) {
444 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellDebug1HiiHandle, L"mm");
445 ShellStatus = SHELL_INVALID_PARAMETER;
446 goto Done;
447 } else if (ShellCommandLineGetFlag (Package, L"-w") && ShellCommandLineGetValue (Package, L"-w") == NULL) {
448 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_VALUE), gShellDebug1HiiHandle, L"mm", L"-w");
449 ShellStatus = SHELL_INVALID_PARAMETER;
450 goto Done;
451 } else {
452 if (ShellCommandLineGetFlag (Package, L"-mmio")) {
453 AccessType = ShellMmMemoryMappedIo;
454 if (ShellCommandLineGetFlag (Package, L"-mem")
455 || ShellCommandLineGetFlag (Package, L"-io")
456 || ShellCommandLineGetFlag (Package, L"-pci")
457 || ShellCommandLineGetFlag (Package, L"-pcie")
458 ) {
459 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellDebug1HiiHandle, L"mm");
460 ShellStatus = SHELL_INVALID_PARAMETER;
461 goto Done;
462 }
463 } else if (ShellCommandLineGetFlag (Package, L"-mem")) {
464 AccessType = ShellMmMemory;
465 if (ShellCommandLineGetFlag (Package, L"-io")
466 || ShellCommandLineGetFlag (Package, L"-pci")
467 || ShellCommandLineGetFlag (Package, L"-pcie")
468 ) {
469 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellDebug1HiiHandle, L"mm");
470 ShellStatus = SHELL_INVALID_PARAMETER;
471 goto Done;
472 }
473 } else if (ShellCommandLineGetFlag (Package, L"-io")) {
474 AccessType = ShellMmIo;
475 if (ShellCommandLineGetFlag (Package, L"-pci")
476 || ShellCommandLineGetFlag (Package, L"-pcie")
477 ) {
478 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellDebug1HiiHandle, L"mm");
479 ShellStatus = SHELL_INVALID_PARAMETER;
480 goto Done;
481 }
482 } else if (ShellCommandLineGetFlag (Package, L"-pci")) {
483 AccessType = ShellMmPci;
484 if (ShellCommandLineGetFlag (Package, L"-pcie")
485 ) {
486 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellDebug1HiiHandle, L"mm");
487 ShellStatus = SHELL_INVALID_PARAMETER;
488 goto Done;
489 }
490 } else if (ShellCommandLineGetFlag (Package, L"-pcie")) {
491 AccessType = ShellMmPciExpress;
492 }
493 }
494
495 //
496 // Non interactive for a script file or for the specific parameter
497 //
498 Interactive = TRUE;
499 if (gEfiShellProtocol->BatchIsActive () || ShellCommandLineGetFlag (Package, L"-n")) {
500 Interactive = FALSE;
501 }
502
503 Temp = ShellCommandLineGetValue (Package, L"-w");
504 if (Temp != NULL) {
505 Size = ShellStrToUintn (Temp);
506 }
507 if ((Size != 1) && (Size != 2) && (Size != 4) && (Size != 8)) {
508 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM_VAL), gShellDebug1HiiHandle, L"mm", Temp, L"-w");
509 ShellStatus = SHELL_INVALID_PARAMETER;
510 goto Done;
511 }
512
513 Temp = ShellCommandLineGetRawValue (Package, 1);
514 Status = ShellConvertStringToUint64 (Temp, &Address, TRUE, FALSE);
515 if (EFI_ERROR (Status)) {
516 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV), gShellDebug1HiiHandle, L"mm", Temp);
517 ShellStatus = SHELL_INVALID_PARAMETER;
518 goto Done;
519 }
520
521 if ((Address & (Size - 1)) != 0) {
522 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_MM_NOT_ALIGNED), gShellDebug1HiiHandle, L"mm", Address);
523 ShellStatus = SHELL_INVALID_PARAMETER;
524 goto Done;
525 }
526
527 if ((AccessType == ShellMmIo) && (Address + Size > MAX_UINT16 + 1)) {
528 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_MM_IO_ADDRESS_RANGE), gShellDebug1HiiHandle, L"mm");
529 ShellStatus = SHELL_INVALID_PARAMETER;
530 goto Done;
531 }
532
533 //
534 // locate IO protocol interface
535 //
536 HasPciRootBridgeIo = ShellMmLocateIoProtocol (AccessType, Address, &CpuIo, &PciRootBridgeIo);
537 if ((AccessType == ShellMmPci) || (AccessType == ShellMmPciExpress)) {
538 if (!HasPciRootBridgeIo) {
539 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_PCIRBIO_NF), gShellDebug1HiiHandle, L"mm");
540 ShellStatus = SHELL_NOT_FOUND;
541 goto Done;
542 }
543 if (PciRootBridgeIo == NULL) {
544 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_MM_PCIE_ADDRESS_RANGE), gShellDebug1HiiHandle, L"mm", Address);
545 ShellStatus = SHELL_INVALID_PARAMETER;
546 goto Done;
547 }
548 }
549
550 //
551 // Mode 1: Directly set a value
552 //
553 Temp = ShellCommandLineGetRawValue (Package, 2);
554 if (Temp != NULL) {
555 Status = ShellConvertStringToUint64 (Temp, &Value, TRUE, FALSE);
556 if (EFI_ERROR (Status)) {
557 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV), gShellDebug1HiiHandle, L"mm", Temp);
558 ShellStatus = SHELL_INVALID_PARAMETER;
559 goto Done;
560 }
561
562 if (Value > mShellMmMaxNumber[Size]) {
563 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV), gShellDebug1HiiHandle, L"mm", Temp);
564 ShellStatus = SHELL_INVALID_PARAMETER;
565 goto Done;
566 }
567
568 ShellMmAccess (AccessType, PciRootBridgeIo, CpuIo, FALSE, Address, Size, &Value);
569 goto Done;
570 }
571
572 //
573 // Mode 2: Directly show a value
574 //
575 if (!Interactive) {
576 if (!gEfiShellProtocol->BatchIsActive ()) {
577 ShellPrintHiiEx (-1, -1, NULL, mShellMmAccessTypeStr[AccessType], gShellDebug1HiiHandle);
578 }
579 ShellMmAccess (AccessType, PciRootBridgeIo, CpuIo, TRUE, Address, Size, &Buffer);
580
581 if (!gEfiShellProtocol->BatchIsActive ()) {
582 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_MM_ADDRESS), gShellDebug1HiiHandle, Address);
583 }
584 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_MM_BUF), gShellDebug1HiiHandle, Size * 2, Buffer & mShellMmMaxNumber[Size]);
585 ShellPrintEx (-1, -1, L"\r\n");
586 goto Done;
587 }
588
589 //
590 // Mode 3: Show or set values in interactive mode
591 //
592 Complete = FALSE;
593 do {
594 if ((AccessType == ShellMmIo) && (Address + Size > MAX_UINT16 + 1)) {
595 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_MM_ADDRESS_RANGE2), gShellDebug1HiiHandle, L"mm");
596 break;
597 }
598
599 ShellMmAccess (AccessType, PciRootBridgeIo, CpuIo, TRUE, Address, Size, &Buffer);
600 ShellPrintHiiEx (-1, -1, NULL, mShellMmAccessTypeStr[AccessType], gShellDebug1HiiHandle);
601 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_MM_ADDRESS), gShellDebug1HiiHandle, Address);
602 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_MM_BUF), gShellDebug1HiiHandle, Size * 2, Buffer & mShellMmMaxNumber[Size]);
603 ShellPrintEx (-1, -1, L" > ");
604 //
605 // wait user input to modify
606 //
607 if (InputStr != NULL) {
608 FreePool (InputStr);
609 InputStr = NULL;
610 }
611 ShellPromptForResponse (ShellPromptResponseTypeFreeform, NULL, (VOID**) &InputStr);
612
613 if (InputStr != NULL) {
614 //
615 // skip space characters
616 //
617 for (Index = 0; InputStr[Index] == ' '; Index++);
618 }
619
620 if ((InputStr != NULL) && (InputStr[Index] != CHAR_NULL)) {
621 if ((InputStr[Index] == '.') || (InputStr[Index] == 'q') || (InputStr[Index] == 'Q')) {
622 Complete = TRUE;
623 } else if (!EFI_ERROR (ShellConvertStringToUint64 (InputStr + Index, &Buffer, TRUE, TRUE)) &&
624 (Buffer <= mShellMmMaxNumber[Size])
625 ) {
626 ShellMmAccess (AccessType, PciRootBridgeIo, CpuIo, FALSE, Address, Size, &Buffer);
627 } else {
628 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_MM_ERROR), gShellDebug1HiiHandle, L"mm");
629 continue;
630 }
631 }
632
633 Address += Size;
634 ShellPrintEx (-1, -1, L"\r\n");
635 } while (!Complete);
636 }
637 ASSERT (ShellStatus == SHELL_SUCCESS);
638
639 Done:
640 if (InputStr != NULL) {
641 FreePool (InputStr);
642 }
643 if (Package != NULL) {
644 ShellCommandLineFreeVarList (Package);
645 }
646 return ShellStatus;
647 }