ShellPkg/memmap: Refine code
[mirror_edk2.git] / ShellPkg / Library / UefiShellDebug1CommandsLib / MemMap.c
1 /** @file
2 Main file for Mode shell Debug1 function.
3
4 (C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>
5 (C) Copyright 2013-2015 Hewlett-Packard Development Company, L.P.<BR>
6 Copyright (c) 2010 - 2017, Intel Corporation. All rights reserved.<BR>
7 This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which acModeanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
11
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
14
15 **/
16
17 //
18 // Need full names for Standard-Format Output
19 //
20 STATIC CONST CHAR16 NameEfiReservedMemoryType[] = L"Reserved";
21 STATIC CONST CHAR16 NameEfiLoaderCode[] = L"LoaderCode";
22 STATIC CONST CHAR16 NameEfiLoaderData[] = L"LoaderData";
23 STATIC CONST CHAR16 NameEfiBootServicesCode[] = L"BootServiceCode";
24 STATIC CONST CHAR16 NameEfiBootServicesData[] = L"BootServiceData";
25 STATIC CONST CHAR16 NameEfiRuntimeServicesCode[] = L"RuntimeCode";
26 STATIC CONST CHAR16 NameEfiRuntimeServicesData[] = L"RuntimeData";
27 STATIC CONST CHAR16 NameEfiConventionalMemory[] = L"Available";
28 STATIC CONST CHAR16 NameEfiPersistentMemory[] = L"Persistent";
29 STATIC CONST CHAR16 NameEfiUnusableMemory[] = L"UnusableMemory";
30 STATIC CONST CHAR16 NameEfiACPIReclaimMemory[] = L"ACPIReclaimMemory";
31 STATIC CONST CHAR16 NameEfiACPIMemoryNVS[] = L"ACPIMemoryNVS";
32 STATIC CONST CHAR16 NameEfiMemoryMappedIO[] = L"MemoryMappedIO";
33 STATIC CONST CHAR16 NameEfiMemoryMappedIOPortSpace[] = L"MemoryMappedIOPortSpace";
34 STATIC CONST CHAR16 NameEfiPalCode[] = L"PalCode";
35
36 //
37 // Need short names for some memory types
38 //
39 STATIC CONST CHAR16 NameEfiBootServicesCodeShort[] = L"BS_Code";
40 STATIC CONST CHAR16 NameEfiBootServicesDataShort[] = L"BS_Data";
41 STATIC CONST CHAR16 NameEfiRuntimeServicesCodeShort[] = L"RT_Code";
42 STATIC CONST CHAR16 NameEfiRuntimeServicesDataShort[] = L"RT_Data";
43 STATIC CONST CHAR16 NameEfiUnusableMemoryShort[] = L"Unusable";
44 STATIC CONST CHAR16 NameEfiACPIReclaimMemoryShort[] = L"ACPI_Recl";
45 STATIC CONST CHAR16 NameEfiACPIMemoryNVSShort[] = L"ACPI_NVS";
46 STATIC CONST CHAR16 NameEfiMemoryMappedIOShort[] = L"MMIO";
47 STATIC CONST CHAR16 NameEfiMemoryMappedIOPortSpaceShort[] = L"MMIO_Port";
48
49 #include "UefiShellDebug1CommandsLib.h"
50
51 /**
52 Function for 'memmap' command.
53
54 @param[in] ImageHandle Handle to the Image (NULL if Internal).
55 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
56 **/
57 SHELL_STATUS
58 EFIAPI
59 ShellCommandRunMemMap (
60 IN EFI_HANDLE ImageHandle,
61 IN EFI_SYSTEM_TABLE *SystemTable
62 )
63 {
64 EFI_STATUS Status;
65 LIST_ENTRY *Package;
66 CHAR16 *ProblemParam;
67 SHELL_STATUS ShellStatus;
68 UINTN Size;
69 EFI_MEMORY_DESCRIPTOR *Descriptors;
70 UINTN MapKey;
71 UINTN ItemSize;
72 UINT32 Version;
73 EFI_MEMORY_DESCRIPTOR *Walker;
74 UINT64 ReservedPages;
75 UINT64 LoadCodePages;
76 UINT64 LoadDataPages;
77 UINT64 BSCodePages;
78 UINT64 BSDataPages;
79 UINT64 RTDataPages;
80 UINT64 RTCodePages;
81 UINT64 AvailPages;
82 UINT64 TotalPages;
83 UINT64 ReservedPagesSize;
84 UINT64 LoadCodePagesSize;
85 UINT64 LoadDataPagesSize;
86 UINT64 BSCodePagesSize;
87 UINT64 BSDataPagesSize;
88 UINT64 RTDataPagesSize;
89 UINT64 RTCodePagesSize;
90 UINT64 AvailPagesSize;
91 UINT64 TotalPagesSize;
92 UINT64 AcpiReclaimPages;
93 UINT64 AcpiNvsPages;
94 UINT64 MmioSpacePages;
95 UINT64 AcpiReclaimPagesSize;
96 UINT64 AcpiNvsPagesSize;
97 UINT64 MmioSpacePagesSize;
98 UINT64 MmioPortPages;
99 UINT64 MmioPortPagesSize;
100 UINT64 UnusableMemoryPages;
101 UINT64 UnusableMemoryPagesSize;
102 UINT64 PalCodePages;
103 UINT64 PalCodePagesSize;
104 UINT64 PersistentPages;
105 UINT64 PersistentPagesSize;
106 BOOLEAN Sfo;
107
108 AcpiReclaimPages = 0;
109 AcpiNvsPages = 0;
110 MmioSpacePages = 0;
111 TotalPages = 0;
112 ReservedPages = 0;
113 LoadCodePages = 0;
114 LoadDataPages = 0;
115 BSCodePages = 0;
116 BSDataPages = 0;
117 RTDataPages = 0;
118 RTCodePages = 0;
119 AvailPages = 0;
120 MmioPortPages = 0;
121 UnusableMemoryPages = 0;
122 PalCodePages = 0;
123 PersistentPages = 0;
124 Size = 0;
125 Descriptors = NULL;
126 ShellStatus = SHELL_SUCCESS;
127 Status = EFI_SUCCESS;
128
129 //
130 // initialize the shell lib (we must be in non-auto-init...)
131 //
132 Status = ShellInitialize();
133 ASSERT_EFI_ERROR(Status);
134
135 Status = CommandInit();
136 ASSERT_EFI_ERROR(Status);
137
138 //
139 // parse the command line
140 //
141 Status = ShellCommandLineParse (SfoParamList, &Package, &ProblemParam, TRUE);
142 if (EFI_ERROR(Status)) {
143 if (Status == EFI_VOLUME_CORRUPTED && ProblemParam != NULL) {
144 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellDebug1HiiHandle, L"memmap", ProblemParam);
145 FreePool(ProblemParam);
146 ShellStatus = SHELL_INVALID_PARAMETER;
147 } else {
148 ASSERT(FALSE);
149 }
150 } else {
151 if (ShellCommandLineGetCount(Package) > 1) {
152 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellDebug1HiiHandle, L"memmap");
153 ShellStatus = SHELL_INVALID_PARAMETER;
154 } else {
155 Status = gBS->GetMemoryMap(&Size, Descriptors, &MapKey, &ItemSize, &Version);
156 if (Status == EFI_BUFFER_TOO_SMALL){
157 Size += SIZE_1KB;
158 Descriptors = AllocateZeroPool(Size);
159 Status = gBS->GetMemoryMap(&Size, Descriptors, &MapKey, &ItemSize, &Version);
160 }
161 if (EFI_ERROR(Status)) {
162 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_MEMMAP_GET_FAILED), gShellDebug1HiiHandle, L"memmap");
163 ShellStatus = SHELL_ACCESS_DENIED;
164 } else {
165 ASSERT(Version == EFI_MEMORY_DESCRIPTOR_VERSION);
166
167 Sfo = ShellCommandLineGetFlag(Package, L"-sfo");
168 if (!Sfo) {
169 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_MEMMAP_LIST_HEAD), gShellDebug1HiiHandle);
170 } else {
171 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_SFO_HEADER), gShellDebug1HiiHandle, L"memmap");
172 }
173
174 for ( Walker = Descriptors
175 ; (Walker < (EFI_MEMORY_DESCRIPTOR *) ((UINT8*)Descriptors + Size)) && (Walker != NULL)
176 ; Walker = (EFI_MEMORY_DESCRIPTOR *) ((UINT8 *)Walker + ItemSize)
177 ){
178 switch (Walker->Type) {
179 case EfiReservedMemoryType:
180 ShellPrintHiiEx(-1, -1, NULL, (EFI_STRING_ID)(!Sfo?STRING_TOKEN (STR_MEMMAP_LIST_ITEM):STRING_TOKEN (STR_MEMMAP_LIST_ITEM_SFO)), gShellDebug1HiiHandle, NameEfiReservedMemoryType, Walker->PhysicalStart, Walker->PhysicalStart+MultU64x64(SIZE_4KB,Walker->NumberOfPages)-1, Walker->NumberOfPages, Walker->Attribute);
181 ReservedPages += Walker->NumberOfPages;
182 break;
183 case EfiLoaderCode:
184 ShellPrintHiiEx(-1, -1, NULL, (EFI_STRING_ID)(!Sfo?STRING_TOKEN (STR_MEMMAP_LIST_ITEM):STRING_TOKEN (STR_MEMMAP_LIST_ITEM_SFO)), gShellDebug1HiiHandle, NameEfiLoaderCode, Walker->PhysicalStart, Walker->PhysicalStart+MultU64x64(SIZE_4KB,Walker->NumberOfPages)-1, Walker->NumberOfPages, Walker->Attribute);
185 LoadCodePages += Walker->NumberOfPages;
186 TotalPages += Walker->NumberOfPages;
187 break;
188 case EfiLoaderData:
189 ShellPrintHiiEx(-1, -1, NULL, (EFI_STRING_ID)(!Sfo?STRING_TOKEN (STR_MEMMAP_LIST_ITEM):STRING_TOKEN (STR_MEMMAP_LIST_ITEM_SFO)), gShellDebug1HiiHandle, NameEfiLoaderData, Walker->PhysicalStart, Walker->PhysicalStart+MultU64x64(SIZE_4KB,Walker->NumberOfPages)-1, Walker->NumberOfPages, Walker->Attribute);
190 LoadDataPages += Walker->NumberOfPages;
191 TotalPages += Walker->NumberOfPages;
192 break;
193 case EfiBootServicesCode:
194 ShellPrintHiiEx(-1, -1, NULL, (EFI_STRING_ID)(!Sfo?STRING_TOKEN (STR_MEMMAP_LIST_ITEM):STRING_TOKEN (STR_MEMMAP_LIST_ITEM_SFO)), gShellDebug1HiiHandle, !Sfo?NameEfiBootServicesCodeShort:NameEfiBootServicesCode, Walker->PhysicalStart, Walker->PhysicalStart+MultU64x64(SIZE_4KB,Walker->NumberOfPages)-1, Walker->NumberOfPages, Walker->Attribute);
195 BSCodePages += Walker->NumberOfPages;
196 TotalPages += Walker->NumberOfPages;
197 break;
198 case EfiBootServicesData:
199 ShellPrintHiiEx(-1, -1, NULL, (EFI_STRING_ID)(!Sfo?STRING_TOKEN (STR_MEMMAP_LIST_ITEM):STRING_TOKEN (STR_MEMMAP_LIST_ITEM_SFO)), gShellDebug1HiiHandle, !Sfo?NameEfiBootServicesDataShort:NameEfiBootServicesData, Walker->PhysicalStart, Walker->PhysicalStart+MultU64x64(SIZE_4KB,Walker->NumberOfPages)-1, Walker->NumberOfPages, Walker->Attribute);
200 BSDataPages += Walker->NumberOfPages;
201 TotalPages += Walker->NumberOfPages;
202 break;
203 case EfiRuntimeServicesCode:
204 ShellPrintHiiEx(-1, -1, NULL, (EFI_STRING_ID)(!Sfo?STRING_TOKEN (STR_MEMMAP_LIST_ITEM):STRING_TOKEN (STR_MEMMAP_LIST_ITEM_SFO)), gShellDebug1HiiHandle, !Sfo?NameEfiRuntimeServicesCodeShort:NameEfiRuntimeServicesCode, Walker->PhysicalStart, Walker->PhysicalStart+MultU64x64(SIZE_4KB,Walker->NumberOfPages)-1, Walker->NumberOfPages, Walker->Attribute);
205 RTCodePages += Walker->NumberOfPages;
206 TotalPages += Walker->NumberOfPages;
207 break;
208 case EfiRuntimeServicesData:
209 ShellPrintHiiEx(-1, -1, NULL, (EFI_STRING_ID)(!Sfo?STRING_TOKEN (STR_MEMMAP_LIST_ITEM):STRING_TOKEN (STR_MEMMAP_LIST_ITEM_SFO)), gShellDebug1HiiHandle, !Sfo?NameEfiRuntimeServicesDataShort:NameEfiRuntimeServicesData, Walker->PhysicalStart, Walker->PhysicalStart+MultU64x64(SIZE_4KB,Walker->NumberOfPages)-1, Walker->NumberOfPages, Walker->Attribute);
210 RTDataPages += Walker->NumberOfPages;
211 TotalPages += Walker->NumberOfPages;
212 break;
213 case EfiConventionalMemory:
214 ShellPrintHiiEx(-1, -1, NULL, (EFI_STRING_ID)(!Sfo?STRING_TOKEN (STR_MEMMAP_LIST_ITEM):STRING_TOKEN (STR_MEMMAP_LIST_ITEM_SFO)), gShellDebug1HiiHandle, NameEfiConventionalMemory, Walker->PhysicalStart, Walker->PhysicalStart+MultU64x64(SIZE_4KB,Walker->NumberOfPages)-1, Walker->NumberOfPages, Walker->Attribute);
215 AvailPages += Walker->NumberOfPages;
216 TotalPages += Walker->NumberOfPages;
217 break;
218 case EfiPersistentMemory:
219 ShellPrintHiiEx(-1, -1, NULL, (EFI_STRING_ID)(!Sfo?STRING_TOKEN (STR_MEMMAP_LIST_ITEM):STRING_TOKEN (STR_MEMMAP_LIST_ITEM_SFO)), gShellDebug1HiiHandle, NameEfiPersistentMemory, Walker->PhysicalStart, Walker->PhysicalStart+MultU64x64(SIZE_4KB,Walker->NumberOfPages)-1, Walker->NumberOfPages, Walker->Attribute);
220 PersistentPages += Walker->NumberOfPages;
221 TotalPages += Walker->NumberOfPages;
222 break;
223 case EfiUnusableMemory:
224 ShellPrintHiiEx(-1, -1, NULL, (EFI_STRING_ID)(!Sfo?STRING_TOKEN (STR_MEMMAP_LIST_ITEM):STRING_TOKEN (STR_MEMMAP_LIST_ITEM_SFO)), gShellDebug1HiiHandle, !Sfo?NameEfiUnusableMemoryShort:NameEfiUnusableMemory, Walker->PhysicalStart, Walker->PhysicalStart+MultU64x64(SIZE_4KB,Walker->NumberOfPages)-1, Walker->NumberOfPages, Walker->Attribute);
225 UnusableMemoryPages += Walker->NumberOfPages;
226 break;
227 case EfiACPIReclaimMemory:
228 ShellPrintHiiEx(-1, -1, NULL, (EFI_STRING_ID)(!Sfo?STRING_TOKEN (STR_MEMMAP_LIST_ITEM):STRING_TOKEN (STR_MEMMAP_LIST_ITEM_SFO)), gShellDebug1HiiHandle, !Sfo?NameEfiACPIReclaimMemoryShort:NameEfiACPIReclaimMemory, Walker->PhysicalStart, Walker->PhysicalStart+MultU64x64(SIZE_4KB,Walker->NumberOfPages)-1, Walker->NumberOfPages, Walker->Attribute);
229 TotalPages += Walker->NumberOfPages;
230 AcpiReclaimPages += Walker->NumberOfPages;
231 break;
232 case EfiACPIMemoryNVS:
233 ShellPrintHiiEx(-1, -1, NULL, (EFI_STRING_ID)(!Sfo?STRING_TOKEN (STR_MEMMAP_LIST_ITEM):STRING_TOKEN (STR_MEMMAP_LIST_ITEM_SFO)), gShellDebug1HiiHandle, !Sfo?NameEfiACPIMemoryNVSShort:NameEfiACPIMemoryNVS, Walker->PhysicalStart, Walker->PhysicalStart+MultU64x64(SIZE_4KB,Walker->NumberOfPages)-1, Walker->NumberOfPages, Walker->Attribute);
234 TotalPages += Walker->NumberOfPages;
235 AcpiNvsPages += Walker->NumberOfPages;
236 break;
237 case EfiMemoryMappedIO:
238 ShellPrintHiiEx(-1, -1, NULL, (EFI_STRING_ID)(!Sfo?STRING_TOKEN (STR_MEMMAP_LIST_ITEM):STRING_TOKEN (STR_MEMMAP_LIST_ITEM_SFO)), gShellDebug1HiiHandle, !Sfo?NameEfiMemoryMappedIOShort:NameEfiMemoryMappedIO, Walker->PhysicalStart, Walker->PhysicalStart+MultU64x64(SIZE_4KB,Walker->NumberOfPages)-1, Walker->NumberOfPages, Walker->Attribute);
239 MmioSpacePages += Walker->NumberOfPages;
240 break;
241 case EfiMemoryMappedIOPortSpace:
242 ShellPrintHiiEx(-1, -1, NULL, (EFI_STRING_ID)(!Sfo?STRING_TOKEN (STR_MEMMAP_LIST_ITEM):STRING_TOKEN (STR_MEMMAP_LIST_ITEM_SFO)), gShellDebug1HiiHandle, !Sfo?NameEfiMemoryMappedIOPortSpaceShort:NameEfiMemoryMappedIOPortSpace, Walker->PhysicalStart, Walker->PhysicalStart+MultU64x64(SIZE_4KB,Walker->NumberOfPages)-1, Walker->NumberOfPages, Walker->Attribute);
243 MmioPortPages += Walker->NumberOfPages;
244 break;
245 case EfiPalCode:
246 ShellPrintHiiEx(-1, -1, NULL, (EFI_STRING_ID)(!Sfo?STRING_TOKEN (STR_MEMMAP_LIST_ITEM):STRING_TOKEN (STR_MEMMAP_LIST_ITEM_SFO)), gShellDebug1HiiHandle, NameEfiPalCode, Walker->PhysicalStart, Walker->PhysicalStart+MultU64x64(SIZE_4KB,Walker->NumberOfPages)-1, Walker->NumberOfPages, Walker->Attribute);
247 TotalPages += Walker->NumberOfPages;
248 PalCodePages += Walker->NumberOfPages;
249 break;
250 default:
251 ASSERT(FALSE);
252 }
253 }
254 //
255 // print the summary
256 //
257 ReservedPagesSize = MultU64x64(SIZE_4KB,ReservedPages);
258 LoadCodePagesSize = MultU64x64(SIZE_4KB,LoadCodePages);
259 LoadDataPagesSize = MultU64x64(SIZE_4KB,LoadDataPages);
260 BSCodePagesSize = MultU64x64(SIZE_4KB,BSCodePages);
261 BSDataPagesSize = MultU64x64(SIZE_4KB,BSDataPages);
262 RTDataPagesSize = MultU64x64(SIZE_4KB,RTDataPages);
263 RTCodePagesSize = MultU64x64(SIZE_4KB,RTCodePages);
264 AvailPagesSize = MultU64x64(SIZE_4KB,AvailPages);
265 TotalPagesSize = MultU64x64(SIZE_4KB,TotalPages);
266 AcpiReclaimPagesSize = MultU64x64(SIZE_4KB,AcpiReclaimPages);
267 AcpiNvsPagesSize = MultU64x64(SIZE_4KB,AcpiNvsPages);
268 MmioSpacePagesSize = MultU64x64(SIZE_4KB,MmioSpacePages);
269 MmioPortPagesSize = MultU64x64(SIZE_4KB,MmioPortPages);
270 PalCodePagesSize = MultU64x64(SIZE_4KB,PalCodePages);
271 PersistentPagesSize = MultU64x64(SIZE_4KB,PersistentPages);
272 UnusableMemoryPagesSize = MultU64x64(SIZE_4KB,UnusableMemoryPages);
273 if (!Sfo) {
274 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_MEMMAP_LIST_SUMM), gShellDebug1HiiHandle,
275 ReservedPages, ReservedPagesSize,
276 LoadCodePages, LoadCodePagesSize,
277 LoadDataPages, LoadDataPagesSize,
278 BSCodePages, BSCodePagesSize,
279 BSDataPages, BSDataPagesSize,
280 RTCodePages, RTCodePagesSize,
281 RTDataPages, RTDataPagesSize,
282 AcpiReclaimPages, AcpiReclaimPagesSize,
283 AcpiNvsPages, AcpiNvsPagesSize,
284 MmioSpacePages, MmioSpacePagesSize,
285 MmioPortPages, MmioPortPagesSize,
286 PalCodePages, PalCodePagesSize,
287 AvailPages, AvailPagesSize,
288 PersistentPages, PersistentPagesSize,
289 DivU64x32(MultU64x64(SIZE_4KB,TotalPages), SIZE_1MB), TotalPagesSize
290 );
291 } else {
292 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_MEMMAP_LIST_SUMM_SFO), gShellDebug1HiiHandle,
293 TotalPagesSize,
294 ReservedPagesSize,
295 BSCodePagesSize,
296 BSDataPagesSize,
297 RTCodePagesSize,
298 RTDataPagesSize,
299 LoadCodePagesSize,
300 LoadDataPagesSize,
301 AvailPagesSize,
302 MmioSpacePagesSize,
303 MmioPortPagesSize,
304 UnusableMemoryPagesSize,
305 AcpiReclaimPagesSize,
306 AcpiNvsPagesSize,
307 PalCodePagesSize,
308 PersistentPagesSize
309 );
310 }
311 }
312 }
313 ShellCommandLineFreeVarList (Package);
314 }
315
316 if (Descriptors != NULL) {
317 FreePool(Descriptors);
318 }
319
320 return (ShellStatus);
321 }
322