File: MemMap.c

package info (click to toggle)
edk2 2025.11-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 338,556 kB
  • sloc: ansic: 2,166,376; asm: 270,725; perl: 235,301; python: 149,839; sh: 34,744; cpp: 23,311; makefile: 3,326; pascal: 1,602; xml: 806; lisp: 35; ruby: 16; sed: 6; tcl: 4
file content (423 lines) | stat: -rw-r--r-- 14,469 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/** @file
  Main file for Mode shell Debug1 function.

  (C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>
  (C) Copyright 2013-2015 Hewlett-Packard Development Company, L.P.<BR>
  Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved.<BR>
  SPDX-License-Identifier: BSD-2-Clause-Patent

**/

//
// Need full names for Standard-Format Output
//
STATIC CONST CHAR16  *FullNameEfiMemory[] = {
  [EfiReservedMemoryType]      = L"Reserved",
  [EfiLoaderCode]              = L"LoaderCode",
  [EfiLoaderData]              = L"LoaderData",
  [EfiBootServicesCode]        = L"BootServiceCode",
  [EfiBootServicesData]        = L"BootServiceData",
  [EfiRuntimeServicesCode]     = L"RuntimeCode",
  [EfiRuntimeServicesData]     = L"RuntimeData",
  [EfiConventionalMemory]      = L"Available",
  [EfiUnusableMemory]          = L"UnusableMemory",
  [EfiACPIReclaimMemory]       = L"ACPIReclaimMemory",
  [EfiACPIMemoryNVS]           = L"ACPIMemoryNVS",
  [EfiMemoryMappedIO]          = L"MemoryMappedIO",
  [EfiMemoryMappedIOPortSpace] = L"MemoryMappedIOPortSpace",
  [EfiPalCode]                 = L"PalCode",
  [EfiPersistentMemory]        = L"Persistent",
  [EfiUnacceptedMemoryType]    = L"Unaccepted",
};

//
// Need short names for some memory types
//
STATIC CONST CHAR16  *ShortNameEfiMemory[] = {
  [EfiBootServicesCode]        = L"BS_Code",
  [EfiBootServicesData]        = L"BS_Data",
  [EfiRuntimeServicesCode]     = L"RT_Code",
  [EfiRuntimeServicesData]     = L"RT_Data",
  [EfiUnusableMemory]          = L"Unusable",
  [EfiACPIReclaimMemory]       = L"ACPI_Recl",
  [EfiACPIMemoryNVS]           = L"ACPI_NVS",
  [EfiMemoryMappedIO]          = L"MMIO",
  [EfiMemoryMappedIOPortSpace] = L"MMIO_Port",
};

/** Memory Type information.

  Store some information about the actions to operate on different
  Memory Types.
*/
typedef struct {
  /// Whether the MemoryType should contribute to the total number of pages.
  BOOLEAN    ContributeTotalPages;

  /// Whether the MemoryType has a short name.
  BOOLEAN    HasShortName;
} MEMORY_TYPE_INFO;

STATIC CONST MEMORY_TYPE_INFO  MemoryPageArray[EfiMaxMemoryType] = {
  [EfiReservedMemoryType]      = { FALSE, FALSE, },
  [EfiLoaderCode]              = { TRUE,  FALSE, },
  [EfiLoaderData]              = { TRUE,  FALSE, },
  [EfiBootServicesCode]        = { TRUE,  TRUE,  },
  [EfiBootServicesData]        = { TRUE,  TRUE,  },
  [EfiRuntimeServicesCode]     = { TRUE,  TRUE,  },
  [EfiRuntimeServicesData]     = { TRUE,  TRUE,  },
  [EfiConventionalMemory]      = { TRUE,  FALSE, },
  [EfiUnusableMemory]          = { FALSE, TRUE,  },
  [EfiACPIReclaimMemory]       = { TRUE,  TRUE,  },
  [EfiACPIMemoryNVS]           = { TRUE,  TRUE,  },
  [EfiMemoryMappedIO]          = { FALSE, TRUE,  },
  [EfiMemoryMappedIOPortSpace] = { FALSE, TRUE,  },
  [EfiPalCode]                 = { TRUE,  FALSE, },
  [EfiPersistentMemory]        = { TRUE,  FALSE, },
  [EfiUnacceptedMemoryType]    = { TRUE,  FALSE, },
};

/** Memory Type Pages Information.

  Store the number of Pages/PagesSize for each MemoryType.
*/
typedef struct {
  /// Number of pages for this MemoryType.
  UINT64    Pages;

  /// Number of Pages * Page Size for this MemoryType.
  UINT64    PagesSize;
} MEMORY_TYPE_PAGES_INFO;

#include "UefiShellDebug1CommandsLib.h"

typedef struct {
  UINT32        Type;
  UINT64        NumberOfPages;
  LIST_ENTRY    Link;
} MEMORY_LENGTH_ENTRY;

/**
  Add the length of the specified type to List.

  @param List          A list to hold all pairs of <Type, NumberOfPages>.
  @param Type          Memory type.
  @param NumberOfPages Number of pages.
**/
VOID
AddMemoryLength (
  LIST_ENTRY  *List,
  UINT32      Type,
  UINT64      NumberOfPages
  )
{
  MEMORY_LENGTH_ENTRY  *Entry;
  MEMORY_LENGTH_ENTRY  *NewEntry;
  LIST_ENTRY           *Link;

  Entry = NULL;
  for (Link = GetFirstNode (List); !IsNull (List, Link); Link = GetNextNode (List, Link)) {
    Entry = BASE_CR (Link, MEMORY_LENGTH_ENTRY, Link);
    if (Entry->Type >= Type) {
      break;
    }
  }

  if ((Entry != NULL) && (Entry->Type == Type)) {
    //
    // The Entry is the one we look for.
    //
    NewEntry = Entry;
  } else {
    //
    // The search operation breaks due to:
    // 1. Type of every entry < Type --> Insert to tail
    // 2. Type of an entry > Type --> Insert to previous of this entry
    //
    NewEntry = AllocatePool (sizeof (*NewEntry));
    if (NewEntry == NULL) {
      return;
    }

    NewEntry->Type          = Type;
    NewEntry->NumberOfPages = 0;
    InsertTailList (Link, &NewEntry->Link);
  }

  NewEntry->NumberOfPages += NumberOfPages;
}

/** Parse Memory Descriptors

@param[in]  Descriptors   Array of pointers to Memory Descriptors.
@param[in]  Size          Size of the Descriptors array.
@param[in]  ItemSize      Size of a Memory Descriptor.
@param[in]  Sfo           Whether 'Standard Format Output' should be used.
**/
STATIC
SHELL_STATUS
ParseMemoryDescriptors (
  IN  EFI_MEMORY_DESCRIPTOR  *Descriptors,
  IN  UINTN                  Size,
  IN  UINTN                  ItemSize,
  IN  BOOLEAN                Sfo
  )
{
  EFI_MEMORY_DESCRIPTOR   *Walker;
  UINT64                  TotalPages;
  UINT64                  TotalPagesSize;
  MEMORY_TYPE_PAGES_INFO  MemoryPageCount[EfiMaxMemoryType];
  CONST CHAR16            *MemoryTypeName;
  UINTN                   Index;
  LIST_ENTRY              MemoryList;
  LIST_ENTRY              *Link;
  MEMORY_LENGTH_ENTRY     *Entry;

  InitializeListHead (&MemoryList);

  TotalPages = 0;

  // Reset the MemoryPageCount array.
  ZeroMem (MemoryPageCount, sizeof (MemoryPageCount));

  for (Walker = Descriptors
       ; (Walker < (EFI_MEMORY_DESCRIPTOR *)((UINT8 *)Descriptors + Size)) && (Walker != NULL)
       ; Walker = (EFI_MEMORY_DESCRIPTOR *)((UINT8 *)Walker + ItemSize)
       )
  {
    if (Walker->Type < EfiMaxMemoryType) {
      MemoryPageCount[Walker->Type].Pages += Walker->NumberOfPages;

      if (MemoryPageArray[Walker->Type].ContributeTotalPages) {
        TotalPages += Walker->NumberOfPages;
      }

      if (!Sfo && MemoryPageArray[Walker->Type].HasShortName) {
        MemoryTypeName = ShortNameEfiMemory[Walker->Type];
      } else {
        MemoryTypeName = FullNameEfiMemory[Walker->Type];
      }

      ShellPrintHiiDefaultEx (
        (EFI_STRING_ID)(!Sfo ? STRING_TOKEN (STR_MEMMAP_LIST_ITEM) : STRING_TOKEN (STR_MEMMAP_LIST_ITEM_SFO)),
        gShellDebug1HiiHandle,
        MemoryTypeName,
        Walker->PhysicalStart,
        Walker->PhysicalStart+MultU64x64 (SIZE_4KB, Walker->NumberOfPages)-1,
        Walker->NumberOfPages,
        Walker->Attribute
        );
    } else {
      //
      // Shell Spec defines the SFO format.
      // Do not print the OEM/OS memory usage in the SFO format, to avoid conflict with Shell Spec.
      //
      if (!Sfo) {
        ShellPrintHiiDefaultEx (
          STRING_TOKEN (STR_MEMMAP_LIST_ITEM_OTHER),
          gShellDebug1HiiHandle,
          Walker->Type,
          Walker->PhysicalStart,
          Walker->PhysicalStart + MultU64x64 (SIZE_4KB, Walker->NumberOfPages) - 1,
          Walker->NumberOfPages,
          Walker->Attribute
          );
      }

      TotalPages += Walker->NumberOfPages;
      AddMemoryLength (&MemoryList, Walker->Type, Walker->NumberOfPages);
    }
  }

  TotalPagesSize = MultU64x64 (SIZE_4KB, TotalPages);
  for (Index = EfiReservedMemoryType; Index < EfiMaxMemoryType; Index++) {
    MemoryPageCount[Index].PagesSize = MultU64x64 (SIZE_4KB, MemoryPageCount[Index].Pages);
  }

  //
  // print the summary
  //
  if (!Sfo) {
    ShellPrintHiiDefaultEx (
      STRING_TOKEN (STR_MEMMAP_LIST_SUMM),
      gShellDebug1HiiHandle,
      MemoryPageCount[EfiReservedMemoryType].Pages,
      MemoryPageCount[EfiReservedMemoryType].PagesSize,
      MemoryPageCount[EfiLoaderCode].Pages,
      MemoryPageCount[EfiLoaderCode].PagesSize,
      MemoryPageCount[EfiLoaderData].Pages,
      MemoryPageCount[EfiLoaderData].PagesSize,
      MemoryPageCount[EfiBootServicesCode].Pages,
      MemoryPageCount[EfiBootServicesCode].PagesSize,
      MemoryPageCount[EfiBootServicesData].Pages,
      MemoryPageCount[EfiBootServicesData].PagesSize,
      MemoryPageCount[EfiRuntimeServicesCode].Pages,
      MemoryPageCount[EfiRuntimeServicesCode].PagesSize,
      MemoryPageCount[EfiRuntimeServicesData].Pages,
      MemoryPageCount[EfiRuntimeServicesData].PagesSize,
      MemoryPageCount[EfiACPIReclaimMemory].Pages,
      MemoryPageCount[EfiACPIReclaimMemory].PagesSize,
      MemoryPageCount[EfiACPIMemoryNVS].Pages,
      MemoryPageCount[EfiACPIMemoryNVS].PagesSize,
      MemoryPageCount[EfiMemoryMappedIO].Pages,
      MemoryPageCount[EfiMemoryMappedIO].PagesSize,
      MemoryPageCount[EfiMemoryMappedIOPortSpace].Pages,
      MemoryPageCount[EfiMemoryMappedIOPortSpace].PagesSize,
      MemoryPageCount[EfiPalCode].Pages,
      MemoryPageCount[EfiPalCode].PagesSize,
      MemoryPageCount[EfiUnacceptedMemoryType].Pages,
      MemoryPageCount[EfiUnacceptedMemoryType].PagesSize,
      MemoryPageCount[EfiConventionalMemory].Pages,
      MemoryPageCount[EfiConventionalMemory].PagesSize,
      MemoryPageCount[EfiPersistentMemory].Pages,
      MemoryPageCount[EfiPersistentMemory].PagesSize
      );

    //
    // Print out the total memory usage for OEM/OS types in the order of type.
    //
    for (Link = GetFirstNode (&MemoryList); !IsNull (&MemoryList, Link); Link = GetNextNode (&MemoryList, Link)) {
      Entry = BASE_CR (Link, MEMORY_LENGTH_ENTRY, Link);
      ShellPrintHiiDefaultEx (
        STRING_TOKEN (STR_MEMMAP_LIST_SUMM_OTHER),
        gShellDebug1HiiHandle,
        Entry->Type,
        Entry->NumberOfPages,
        MultU64x64 (SIZE_4KB, Entry->NumberOfPages)
        );
    }

    ShellPrintHiiDefaultEx (
      STRING_TOKEN (STR_MEMMAP_LIST_SUMM2),
      gShellDebug1HiiHandle,
      DivU64x32 (MultU64x64 (SIZE_4KB, TotalPages), SIZE_1MB),
      TotalPagesSize
      );
  } else {
    ShellPrintHiiDefaultEx (
      STRING_TOKEN (STR_MEMMAP_LIST_SUMM_SFO),
      gShellDebug1HiiHandle,
      TotalPagesSize,
      MemoryPageCount[EfiReservedMemoryType].PagesSize,
      MemoryPageCount[EfiBootServicesCode].PagesSize,
      MemoryPageCount[EfiBootServicesData].PagesSize,
      MemoryPageCount[EfiRuntimeServicesCode].PagesSize,
      MemoryPageCount[EfiRuntimeServicesData].PagesSize,
      MemoryPageCount[EfiLoaderCode].PagesSize,
      MemoryPageCount[EfiLoaderData].PagesSize,
      MemoryPageCount[EfiConventionalMemory].PagesSize,
      MemoryPageCount[EfiMemoryMappedIO].PagesSize,
      MemoryPageCount[EfiMemoryMappedIOPortSpace].PagesSize,
      MemoryPageCount[EfiUnusableMemory].PagesSize,
      MemoryPageCount[EfiACPIReclaimMemory].PagesSize,
      MemoryPageCount[EfiACPIMemoryNVS].PagesSize,
      MemoryPageCount[EfiPalCode].PagesSize,
      MemoryPageCount[EfiUnacceptedMemoryType].PagesSize,
      MemoryPageCount[EfiPersistentMemory].PagesSize
      );
  }

  //
  // Free the memory list.
  //
  for (Link = GetFirstNode (&MemoryList); !IsNull (&MemoryList, Link); ) {
    Link = RemoveEntryList (Link);
  }

  return SHELL_SUCCESS;
}

/**
  Function for 'memmap' command.

  @param[in] ImageHandle  Handle to the Image (NULL if Internal).
  @param[in] SystemTable  Pointer to the System Table (NULL if Internal).
**/
SHELL_STATUS
EFIAPI
ShellCommandRunMemMap (
  IN EFI_HANDLE        ImageHandle,
  IN EFI_SYSTEM_TABLE  *SystemTable
  )
{
  EFI_STATUS             Status;
  LIST_ENTRY             *Package;
  CHAR16                 *ProblemParam;
  SHELL_STATUS           ShellStatus;
  UINTN                  Size;
  EFI_MEMORY_DESCRIPTOR  *Descriptors;
  UINTN                  MapKey;
  UINTN                  ItemSize;
  UINT32                 Version;
  BOOLEAN                Sfo;

  Size        = 0;
  Descriptors = NULL;
  ShellStatus = SHELL_SUCCESS;
  Status      = EFI_SUCCESS;

  //
  // initialize the shell lib (we must be in non-auto-init...)
  //
  Status = ShellInitialize ();
  ASSERT_EFI_ERROR (Status);

  Status = CommandInit ();
  ASSERT_EFI_ERROR (Status);

  //
  // parse the command line
  //
  Status = ShellCommandLineParse (SfoParamList, &Package, &ProblemParam, TRUE);
  if (EFI_ERROR (Status)) {
    if ((Status == EFI_VOLUME_CORRUPTED) && (ProblemParam != NULL)) {
      ShellPrintHiiDefaultEx (STRING_TOKEN (STR_GEN_PROBLEM), gShellDebug1HiiHandle, L"memmap", ProblemParam);
      FreePool (ProblemParam);
      ShellStatus = SHELL_INVALID_PARAMETER;
    } else {
      ASSERT (FALSE);
    }
  } else {
    if (ShellCommandLineGetCount (Package) > 1) {
      ShellPrintHiiDefaultEx (STRING_TOKEN (STR_GEN_TOO_MANY), gShellDebug1HiiHandle, L"memmap");
      ShellStatus = SHELL_INVALID_PARAMETER;
    } else {
      Status = gBS->GetMemoryMap (&Size, Descriptors, &MapKey, &ItemSize, &Version);
      if (Status == EFI_BUFFER_TOO_SMALL) {
        Size       += SIZE_1KB;
        Descriptors = AllocateZeroPool (Size);
        if (Descriptors == NULL) {
          ShellPrintHiiDefaultEx (STRING_TOKEN (STR_GEN_OUT_MEM), gShellDebug1HiiHandle, L"memmap");
          ShellCommandLineFreeVarList (Package);
          return SHELL_OUT_OF_RESOURCES;
        }

        Status = gBS->GetMemoryMap (&Size, Descriptors, &MapKey, &ItemSize, &Version);
      }

      if (EFI_ERROR (Status)) {
        ShellPrintHiiDefaultEx (STRING_TOKEN (STR_MEMMAP_GET_FAILED), gShellDebug1HiiHandle, L"memmap");
        ShellStatus = SHELL_ACCESS_DENIED;
      } else {
        ASSERT (Version == EFI_MEMORY_DESCRIPTOR_VERSION);

        Sfo = ShellCommandLineGetFlag (Package, L"-sfo");
        if (!Sfo) {
          ShellPrintHiiDefaultEx (STRING_TOKEN (STR_MEMMAP_LIST_HEAD), gShellDebug1HiiHandle);
        } else {
          ShellPrintHiiDefaultEx (STRING_TOKEN (STR_GEN_SFO_HEADER), gShellDebug1HiiHandle, L"memmap");
        }

        ParseMemoryDescriptors (Descriptors, Size, ItemSize, Sfo);
      }
    }

    ShellCommandLineFreeVarList (Package);
  }

  if (Descriptors != NULL) {
    FreePool (Descriptors);
  }

  return (ShellStatus);
}