summaryrefslogtreecommitdiffstats
path: root/MdeModulePkg/Application/DumpDynPcd/DumpDynPcd.c
blob: b8571c455650dc0444295b9d0e4855ff296d01cb (plain)
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
/** @file
  A shell application to dump dynamic PCD settings.

  Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
  SPDX-License-Identifier: BSD-2-Clause-Patent

**/

#include <Uefi.h>
#include <PiDxe.h>
#include <Library/BaseLib.h>
#include <Library/DebugLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/UefiLib.h>

#include <Protocol/UnicodeCollation.h>
#include <Protocol/PiPcd.h>
#include <Protocol/Pcd.h>
#include <Protocol/PiPcdInfo.h>
#include <Protocol/PcdInfo.h>
#include <Protocol/ShellParameters.h>
#include <Protocol/Shell.h>

//
// String token ID of help message text.
// Shell supports to find help message in the resource section of an application image if
// .MAN file is not found. This global variable is added to make build tool recognizes
// that the help string is consumed by user and then build tool will add the string into
// the resource section. Thus the application can use '-?' option to show help message in
// Shell.
//
GLOBAL_REMOVE_IF_UNREFERENCED EFI_STRING_ID  mStrDumpDynPcdHelpTokenId = STRING_TOKEN (STR_DUMP_DYN_PCD_HELP_INFORMATION);

#define MAJOR_VERSION  1
#define MINOR_VERSION  0

static EFI_UNICODE_COLLATION_PROTOCOL  *mUnicodeCollation     = NULL;
static EFI_PCD_PROTOCOL                *mPiPcd                = NULL;
static PCD_PROTOCOL                    *mPcd                  = NULL;
static EFI_GET_PCD_INFO_PROTOCOL       *mPiPcdInfo            = NULL;
static GET_PCD_INFO_PROTOCOL           *mPcdInfo              = NULL;
static CHAR16                          *mTempPcdNameBuffer    = NULL;
static UINTN                           mTempPcdNameBufferSize = 0;

static CONST CHAR8  mHex[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };

static UINTN   Argc;
static CHAR16  **Argv;

/**

  This function parse application ARG.

  @return Status
**/
static
EFI_STATUS
GetArg (
  VOID
  )
{
  EFI_STATUS                     Status;
  EFI_SHELL_PARAMETERS_PROTOCOL  *ShellParameters;

  Status = gBS->HandleProtocol (
                  gImageHandle,
                  &gEfiShellParametersProtocolGuid,
                  (VOID **)&ShellParameters
                  );
  if (EFI_ERROR (Status)) {
    return Status;
  }

  Argc = ShellParameters->Argc;
  Argv = ShellParameters->Argv;
  return EFI_SUCCESS;
}

/**
   Display current version.
**/
static
VOID
ShowVersion (
  )
{
  Print (L"DumpDynPcd Version %d.%02d\n", MAJOR_VERSION, MINOR_VERSION);
}

/**
   Display Usage and Help information.
**/
static
VOID
ShowHelp (
  )
{
  Print (L"Dump dynamic[ex] PCD info.\n");
  Print (L"\n");
  Print (L"DumpDynPcd [PcdName]\n");
  Print (L"\n");
  Print (L"  PcdName    Specifies the name of PCD.\n");
  Print (L"             A literal[or partial] name or a pattern as specified in\n");
  Print (L"             the MetaiMatch() function of the EFI_UNICODE_COLLATION2_PROCOOL.\n");
  Print (L"             If it is absent, dump all PCDs' info.\n");
  Print (L"The PCD data is printed as hexadecimal dump.\n");
}

/**
  Dump some hexadecimal data to the screen.

  @param[in] Indent     How many spaces to indent the output.
  @param[in] Offset     The offset of the printing.
  @param[in] DataSize   The size in bytes of UserData.
  @param[in] UserData   The data to print out.
**/
static
VOID
DumpHex (
  IN UINTN  Indent,
  IN UINTN  Offset,
  IN UINTN  DataSize,
  IN VOID   *UserData
  )
{
  UINT8  *Data;

  CHAR8  Val[50];

  CHAR8  Str[20];

  UINT8  TempByte;
  UINTN  Size;
  UINTN  Index;

  Data = UserData;
  while (DataSize != 0) {
    Size = 16;
    if (Size > DataSize) {
      Size = DataSize;
    }

    for (Index = 0; Index < Size; Index += 1) {
      TempByte           = Data[Index];
      Val[Index * 3 + 0] = mHex[TempByte >> 4];
      Val[Index * 3 + 1] = mHex[TempByte & 0xF];
      Val[Index * 3 + 2] = (CHAR8)((Index == 7) ? '-' : ' ');
      Str[Index]         = (CHAR8)((TempByte < ' ' || TempByte > 'z') ? '.' : TempByte);
    }

    Val[Index * 3] = 0;
    Str[Index]     = 0;
    Print (L"%*a%08X: %-48a *%a*\r\n", Indent, "", Offset, Val, Str);

    Data     += Size;
    Offset   += Size;
    DataSize -= Size;
  }
}

/**
  Safely append with automatic string resizing given length of Destination and
  desired length of copy from Source.

  append the first D characters of Source to the end of Destination, where D is
  the lesser of Count and the StrLen() of Source. If appending those D characters
  will fit within Destination (whose Size is given as CurrentSize) and
  still leave room for a NULL terminator, then those characters are appended,
  starting at the original terminating NULL of Destination, and a new terminating
  NULL is appended.

  If appending D characters onto Destination will result in a overflow of the size
  given in CurrentSize the string will be grown such that the copy can be performed
  and CurrentSize will be updated to the new size.

  If Source is NULL, there is nothing to append, just return the current buffer in
  Destination.

  if Destination is NULL, then ASSERT()
  if Destination's current length (including NULL terminator) is already more then
  CurrentSize, then ASSERT()

  @param[in, out] Destination   The String to append onto
  @param[in, out] CurrentSize   on call the number of bytes in Destination.  On
                                return possibly the new size (still in bytes).  if NULL
                                then allocate whatever is needed.
  @param[in]      Source        The String to append from

  @return Destination           return the resultant string.
**/
static
CHAR16 *
InternalStrnCatGrow (
  IN OUT CHAR16        **Destination,
  IN OUT UINTN         *CurrentSize,
  IN     CONST CHAR16  *Source
  )
{
  UINTN  DestinationStartSize;
  UINTN  NewSize;
  UINTN  SourceLen;

  SourceLen = StrLen (Source);

  //
  // ASSERTs
  //
  ASSERT (Destination != NULL);

  //
  // If there's nothing to do then just return Destination
  //
  if (Source == NULL) {
    return (*Destination);
  }

  //
  // allow for un-initialized pointers, based on size being 0
  //
  if ((CurrentSize != NULL) && (*CurrentSize == 0)) {
    *Destination = NULL;
  }

  //
  // allow for NULL pointers address as Destination
  //
  if (*Destination != NULL) {
    ASSERT (CurrentSize != 0);
    DestinationStartSize = StrSize (*Destination);
    ASSERT (DestinationStartSize <= *CurrentSize);
  } else {
    DestinationStartSize = 0;
  }

  //
  // Test and grow if required
  //
  if (CurrentSize != NULL) {
    NewSize = *CurrentSize;
    if (NewSize < DestinationStartSize + (SourceLen * sizeof (CHAR16))) {
      while (NewSize < (DestinationStartSize + (SourceLen*sizeof (CHAR16)))) {
        NewSize += 2 * SourceLen * sizeof (CHAR16);
      }

      *Destination = ReallocatePool (*CurrentSize, NewSize, *Destination);
      *CurrentSize = NewSize;
    }
  } else {
    NewSize      = (SourceLen + 1)*sizeof (CHAR16);
    *Destination = AllocateZeroPool (NewSize);
  }

  //
  // Now use standard StrnCat on a big enough buffer
  //
  if (*Destination == NULL) {
    return (NULL);
  }

  StrnCatS (*Destination, NewSize/sizeof (CHAR16), Source, SourceLen);
  return *Destination;
}

/**
  Get PCD type string based on input PCD type.

  @param[in]    TokenSpace      PCD Token Space.
  @param[in]    PcdType         The input PCD type.

  @return       Pointer to PCD type string.
**/
static
CHAR16 *
GetPcdTypeString (
  IN CONST EFI_GUID  *TokenSpace,
  IN EFI_PCD_TYPE    PcdType
  )
{
  UINTN   BufLen;
  CHAR16  *RetString;

  BufLen    = 0;
  RetString = NULL;

  switch (PcdType) {
    case EFI_PCD_TYPE_8:
      InternalStrnCatGrow (&RetString, &BufLen, L"UINT8");
      break;
    case EFI_PCD_TYPE_16:
      InternalStrnCatGrow (&RetString, &BufLen, L"UINT16");
      break;
    case EFI_PCD_TYPE_32:
      InternalStrnCatGrow (&RetString, &BufLen, L"UINT32");
      break;
    case EFI_PCD_TYPE_64:
      InternalStrnCatGrow (&RetString, &BufLen, L"UINT64");
      break;
    case EFI_PCD_TYPE_BOOL:
      InternalStrnCatGrow (&RetString, &BufLen, L"BOOLEAN");
      break;
    case EFI_PCD_TYPE_PTR:
      InternalStrnCatGrow (&RetString, &BufLen, L"POINTER");
      break;
    default:
      InternalStrnCatGrow (&RetString, &BufLen, L"UNKNOWN");
      break;
  }

  if (TokenSpace == NULL) {
    InternalStrnCatGrow (&RetString, &BufLen, L":DYNAMIC");
  } else {
    InternalStrnCatGrow (&RetString, &BufLen, L":DYNAMICEX");
  }

  return RetString;
}

/**
  Dump PCD info.

  @param[in]    TokenSpace      PCD Token Space.
  @param[in]    TokenNumber     PCD Token Number.
  @param[in]    PcdInfo         Pointer to PCD info.
**/
static
VOID
DumpPcdInfo (
  IN CONST EFI_GUID  *TokenSpace,
  IN UINTN           TokenNumber,
  IN EFI_PCD_INFO    *PcdInfo
  )
{
  CHAR16   *RetString;
  UINT8    Uint8;
  UINT16   Uint16;
  UINT32   Uint32;
  UINT64   Uint64;
  BOOLEAN  Boolean;
  VOID     *PcdData;

  RetString = NULL;

  if (PcdInfo->PcdName != NULL) {
    Print (L"%a\n", PcdInfo->PcdName);
  } else {
    if (TokenSpace == NULL) {
      Print (L"Default Token Space\n");
    } else {
      Print (L"%g\n", TokenSpace);
    }
  }

  RetString = GetPcdTypeString (TokenSpace, PcdInfo->PcdType);

  switch (PcdInfo->PcdType) {
    case EFI_PCD_TYPE_8:
      if (TokenSpace == NULL) {
        Uint8 = mPcd->Get8 (TokenNumber);
      } else {
        Uint8 = mPiPcd->Get8 (TokenSpace, TokenNumber);
      }

      Print (L"  Token = 0x%08x - Type = %H%-17s%N - Size = 0x%x - Value = 0x%x\n", TokenNumber, RetString, PcdInfo->PcdSize, Uint8);
      break;
    case EFI_PCD_TYPE_16:
      if (TokenSpace == NULL) {
        Uint16 = mPcd->Get16 (TokenNumber);
      } else {
        Uint16 = mPiPcd->Get16 (TokenSpace, TokenNumber);
      }

      Print (L"  Token = 0x%08x - Type = %H%-17s%N - Size = 0x%x - Value = 0x%x\n", TokenNumber, RetString, PcdInfo->PcdSize, Uint16);
      break;
    case EFI_PCD_TYPE_32:
      if (TokenSpace == NULL) {
        Uint32 = mPcd->Get32 (TokenNumber);
      } else {
        Uint32 = mPiPcd->Get32 (TokenSpace, TokenNumber);
      }

      Print (L"  Token = 0x%08x - Type = %H%-17s%N - Size = 0x%x - Value = 0x%x\n", TokenNumber, RetString, PcdInfo->PcdSize, Uint32);
      break;
    case EFI_PCD_TYPE_64:
      if (TokenSpace == NULL) {
        Uint64 = mPcd->Get64 (TokenNumber);
      } else {
        Uint64 = mPiPcd->Get64 (TokenSpace, TokenNumber);
      }

      Print (L"  Token = 0x%08x - Type = %H%-17s%N - Size = 0x%x - Value = 0x%lx\n", TokenNumber, RetString, PcdInfo->PcdSize, Uint64);
      break;
    case EFI_PCD_TYPE_BOOL:
      if (TokenSpace == NULL) {
        Boolean = mPcd->GetBool (TokenNumber);
      } else {
        Boolean = mPiPcd->GetBool (TokenSpace, TokenNumber);
      }

      Print (L"  Token = 0x%08x - Type = %H%-17s%N - Size = 0x%x - Value = %a\n", TokenNumber, RetString, PcdInfo->PcdSize, Boolean ? "TRUE" : "FALSE");
      break;
    case EFI_PCD_TYPE_PTR:
      if (TokenSpace == NULL) {
        PcdData = mPcd->GetPtr (TokenNumber);
      } else {
        PcdData = mPiPcd->GetPtr (TokenSpace, TokenNumber);
      }

      Print (L"  Token = 0x%08x - Type = %H%-17s%N - Size = 0x%x\n", TokenNumber, RetString, PcdInfo->PcdSize);
      DumpHex (2, 0, PcdInfo->PcdSize, PcdData);
      break;
    default:
      return;
  }

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

  Print (L"\n");
}

/**
  Show one or all PCDs' info.

  @param[in]  InputPcdName       Pointer to PCD name to show. If NULL, show all PCDs' info.

  @retval EFI_SUCCESS            Command completed successfully.
  @retval EFI_OUT_OF_RESOURCES   Not enough resources were available to run the command.
  @retval EFI_ABORTED            Aborted by user.
  @retval EFI_NOT_FOUND          The specified PCD is not found.
**/
static
EFI_STATUS
ProcessPcd (
  IN CHAR16  *InputPcdName
  )
{
  EFI_STATUS    Status;
  EFI_GUID      *TokenSpace;
  UINTN         TokenNumber;
  EFI_PCD_INFO  PcdInfo;
  BOOLEAN       Found;
  UINTN         PcdNameSize;

  PcdInfo.PcdName = NULL;
  PcdInfo.PcdSize = 0;
  PcdInfo.PcdType = 0xFF;
  Found           = FALSE;

  Print (L"Current system SKU ID: 0x%x\n\n", mPiPcdInfo->GetSku ());

  TokenSpace = NULL;
  do {
    TokenNumber = 0;
    do {
      Status = mPiPcd->GetNextToken (TokenSpace, &TokenNumber);
      if (!EFI_ERROR (Status) && (TokenNumber != 0)) {
        if (TokenSpace == NULL) {
          //
          // PCD in default Token Space.
          //
          mPcdInfo->GetInfo (TokenNumber, &PcdInfo);
        } else {
          mPiPcdInfo->GetInfo (TokenSpace, TokenNumber, &PcdInfo);
        }

        if (InputPcdName != NULL) {
          if (PcdInfo.PcdName == NULL) {
            continue;
          }

          PcdNameSize = AsciiStrSize (PcdInfo.PcdName) * sizeof (CHAR16);
          if (mTempPcdNameBuffer == NULL) {
            mTempPcdNameBufferSize = PcdNameSize;
            mTempPcdNameBuffer     = AllocatePool (mTempPcdNameBufferSize);
          } else if (mTempPcdNameBufferSize < PcdNameSize) {
            mTempPcdNameBuffer     = ReallocatePool (mTempPcdNameBufferSize, PcdNameSize, mTempPcdNameBuffer);
            mTempPcdNameBufferSize = PcdNameSize;
          }

          if (mTempPcdNameBuffer == NULL) {
            return EFI_OUT_OF_RESOURCES;
          }

          AsciiStrToUnicodeStrS (PcdInfo.PcdName, mTempPcdNameBuffer, mTempPcdNameBufferSize / sizeof (CHAR16));
          //
          // Compare the input PCD name with the PCD name in PCD database.
          //
          if ((StrStr (mTempPcdNameBuffer, InputPcdName) != NULL) ||
              ((mUnicodeCollation != NULL) && mUnicodeCollation->MetaiMatch (mUnicodeCollation, mTempPcdNameBuffer, InputPcdName)))
          {
            //
            // Found matched PCD.
            //
            DumpPcdInfo (TokenSpace, TokenNumber, &PcdInfo);
            Found = TRUE;
          }
        } else {
          DumpPcdInfo (TokenSpace, TokenNumber, &PcdInfo);
        }
      }
    } while (!EFI_ERROR (Status) && TokenNumber != 0);

    Status = mPiPcd->GetNextTokenSpace ((CONST EFI_GUID **)&TokenSpace);
  } while (!EFI_ERROR (Status) && TokenSpace != NULL);

  if ((InputPcdName != NULL) && !Found) {
    //
    // The specified PCD is not found, print error.
    //
    Print (L"%EError. %NNo matching PCD found: %s.\n", InputPcdName);
    return EFI_NOT_FOUND;
  }

  return EFI_SUCCESS;
}

/**
  Main entrypoint for DumpDynPcd shell application.

  @param[in]  ImageHandle     The image handle.
  @param[in]  SystemTable     The system table.

  @retval EFI_SUCCESS            Command completed successfully.
  @retval EFI_INVALID_PARAMETER  Command usage error.
  @retval EFI_OUT_OF_RESOURCES   Not enough resources were available to run the command.
  @retval EFI_ABORTED            Aborted by user.
  @retval EFI_NOT_FOUND          The specified PCD is not found.
  @retval Others                 Error status returned from gBS->LocateProtocol.
**/
EFI_STATUS
EFIAPI
DumpDynPcdMain (
  IN EFI_HANDLE        ImageHandle,
  IN EFI_SYSTEM_TABLE  *SystemTable
  )
{
  EFI_STATUS  Status;
  CHAR16      *InputPcdName;

  InputPcdName = NULL;

  Status = gBS->LocateProtocol (&gEfiUnicodeCollation2ProtocolGuid, NULL, (VOID **)&mUnicodeCollation);
  if (EFI_ERROR (Status)) {
    mUnicodeCollation = NULL;
  }

  Status = gBS->LocateProtocol (&gEfiPcdProtocolGuid, NULL, (VOID **)&mPiPcd);
  if (EFI_ERROR (Status)) {
    Print (L"DumpDynPcd: %EError. %NPI PCD protocol is not present.\n");
    return Status;
  }

  Status = gBS->LocateProtocol (&gEfiGetPcdInfoProtocolGuid, NULL, (VOID **)&mPiPcdInfo);
  if (EFI_ERROR (Status)) {
    Print (L"DumpDynPcd: %EError. %NPI PCD info protocol is not present.\n");
    return Status;
  }

  Status = gBS->LocateProtocol (&gPcdProtocolGuid, NULL, (VOID **)&mPcd);
  if (EFI_ERROR (Status)) {
    Print (L"DumpDynPcd: %EError. %NPCD protocol is not present.\n");
    return Status;
  }

  Status = gBS->LocateProtocol (&gGetPcdInfoProtocolGuid, NULL, (VOID **)&mPcdInfo);
  if (EFI_ERROR (Status)) {
    Print (L"DumpDynPcd: %EError. %NPCD info protocol is not present.\n");
    return Status;
  }

  //
  // get the command line arguments
  //
  Status = GetArg ();
  if (EFI_ERROR (Status)) {
    Print (L"DumpDynPcd: %EError. %NThe input parameters are not recognized.\n");
    Status = EFI_INVALID_PARAMETER;
    return Status;
  }

  if (Argc > 2) {
    Print (L"DumpDynPcd: %EError. %NToo many arguments specified.\n");
    Status = EFI_INVALID_PARAMETER;
    return Status;
  }

  if (Argc == 1) {
    Status = ProcessPcd (InputPcdName);
    goto Done;
  }

  if ((StrCmp (Argv[1], L"-?") == 0) || (StrCmp (Argv[1], L"-h") == 0) || (StrCmp (Argv[1], L"-H") == 0)) {
    ShowHelp ();
    goto Done;
  } else {
    if ((StrCmp (Argv[1], L"-v") == 0) || (StrCmp (Argv[1], L"-V") == 0)) {
      ShowVersion ();
      goto Done;
    } else {
      if (StrStr (Argv[1], L"-") != NULL) {
        Print (L"DumpDynPcd: %EError. %NThe argument '%B%s%N' is invalid.\n", Argv[1]);
        goto Done;
      }
    }
  }

  InputPcdName = Argv[1];
  Status       = ProcessPcd (InputPcdName);

Done:

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

  return Status;
}