summaryrefslogtreecommitdiffstats
path: root/ShellPkg/Application/Shell/ShellManParser.c
blob: 5c823cd7f5843e11808a6ab06dd6fc8e02328da4 (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
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
/** @file
  Provides interface to shell MAN file parser.

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

**/

#include "Shell.h"

#define SHELL_MAN_HII_GUID \
{ \
  0xf62ccd0c, 0x2449, 0x453c, { 0x8a, 0xcb, 0x8c, 0xc5, 0x7c, 0xf0, 0x2a, 0x97 } \
}

EFI_HII_HANDLE  mShellManHiiHandle    = NULL;
EFI_HANDLE      mShellManDriverHandle = NULL;

SHELL_MAN_HII_VENDOR_DEVICE_PATH  mShellManHiiDevicePath = {
  {
    {
      HARDWARE_DEVICE_PATH,
      HW_VENDOR_DP,
      {
        (UINT8)(sizeof (VENDOR_DEVICE_PATH)),
        (UINT8)((sizeof (VENDOR_DEVICE_PATH)) >> 8)
      }
    },
    SHELL_MAN_HII_GUID
  },
  {
    END_DEVICE_PATH_TYPE,
    END_ENTIRE_DEVICE_PATH_SUBTYPE,
    {
      (UINT8)(END_DEVICE_PATH_LENGTH),
      (UINT8)((END_DEVICE_PATH_LENGTH) >> 8)
    }
  }
};

/**
  Verifies that the filename has .EFI on the end.

  allocates a new buffer and copies the name (appending .EFI if necessary).
  Caller to free the buffer.

  @param[in] NameString            original name string

  @return                          the new filename with .efi as the extension.
**/
CHAR16 *
GetExecuatableFileName (
  IN CONST CHAR16  *NameString
  )
{
  CHAR16  *Buffer;
  CHAR16  *SuffixStr;

  if (NameString == NULL) {
    return (NULL);
  }

  //
  // Fix the file name
  //
  if (StrnCmp (NameString+StrLen (NameString)-StrLen (L".efi"), L".efi", StrLen (L".efi")) == 0) {
    Buffer = AllocateCopyPool (StrSize (NameString), NameString);
  } else if (StrnCmp (NameString+StrLen (NameString)-StrLen (L".man"), L".man", StrLen (L".man")) == 0) {
    Buffer = AllocateCopyPool (StrSize (NameString), NameString);
    if (Buffer != NULL) {
      SuffixStr = Buffer+StrLen (Buffer)-StrLen (L".man");
      StrnCpyS (SuffixStr, StrSize (L".man")/sizeof (CHAR16), L".efi", StrLen (L".efi"));
    }
  } else {
    Buffer = AllocateZeroPool (StrSize (NameString) + StrLen (L".efi")*sizeof (CHAR16));
    if (Buffer != NULL) {
      StrnCpyS (
        Buffer,
        (StrSize (NameString) + StrLen (L".efi")*sizeof (CHAR16))/sizeof (CHAR16),
        NameString,
        StrLen (NameString)
        );
      StrnCatS (
        Buffer,
        (StrSize (NameString) + StrLen (L".efi")*sizeof (CHAR16))/sizeof (CHAR16),
        L".efi",
        StrLen (L".efi")
        );
    }
  }

  return (Buffer);
}

/**
  Verifies that the filename has .MAN on the end.

  allocates a new buffer and copies the name (appending .MAN if necessary)

  ASSERT if ManFileName is NULL

  @param[in] ManFileName            original filename

  @return the new filename with .man as the extension.
**/
CHAR16 *
GetManFileName (
  IN CONST CHAR16  *ManFileName
  )
{
  CHAR16  *Buffer;

  if (ManFileName == NULL) {
    return (NULL);
  }

  //
  // Fix the file name
  //
  if (StrnCmp (ManFileName+StrLen (ManFileName)-4, L".man", 4) == 0) {
    Buffer = AllocateCopyPool (StrSize (ManFileName), ManFileName);
  } else {
    Buffer = AllocateZeroPool (StrSize (ManFileName) + 4*sizeof (CHAR16));
    if (Buffer != NULL) {
      StrnCpyS (
        Buffer,
        (StrSize (ManFileName) + 4*sizeof (CHAR16))/sizeof (CHAR16),
        ManFileName,
        StrLen (ManFileName)
        );
      StrnCatS (
        Buffer,
        (StrSize (ManFileName) + 4*sizeof (CHAR16))/sizeof (CHAR16),
        L".man",
        4
        );
    }
  }

  return (Buffer);
}

/**
  Search the path environment variable for possible locations and test for
  which one contains a man file with the name specified.  If a valid file is found
  stop searching and return the (opened) SHELL_FILE_HANDLE for that file.

  @param[in] FileName           Name of the file to find and open.
  @param[out] Handle            Pointer to the handle of the found file.  The
                                value of this is undefined for return values
                                except EFI_SUCCESS.

  @retval EFI_SUCCESS           The file was found.  Handle is a valid SHELL_FILE_HANDLE
  @retval EFI_INVALID_PARAMETER A parameter had an invalid value.
  @retval EFI_NOT_FOUND         The file was not found.
**/
EFI_STATUS
SearchPathForFile (
  IN CONST CHAR16        *FileName,
  OUT SHELL_FILE_HANDLE  *Handle
  )
{
  CHAR16      *FullFileName;
  EFI_STATUS  Status;

  if (  (FileName     == NULL)
     || (Handle       == NULL)
     || (StrLen (FileName) == 0)
        )
  {
    return (EFI_INVALID_PARAMETER);
  }

  FullFileName = ShellFindFilePath (FileName);
  if (FullFileName == NULL) {
    return (EFI_NOT_FOUND);
  }

  //
  // now open that file
  //
  Status = EfiShellOpenFileByName (FullFileName, Handle, EFI_FILE_MODE_READ);
  FreePool (FullFileName);

  return (Status);
}

/**
  Parses through the MAN file specified by SHELL_FILE_HANDLE and returns the
  detailed help for any sub section specified in the comma separated list of
  sections provided.  If the end of the file or a .TH section is found then
  return.

  Upon a successful return the caller is responsible to free the memory in *HelpText

  @param[in] Handle             FileHandle to read from
  @param[in] Sections           name of command's sub sections to find
  @param[out] HelpText          pointer to pointer to string where text goes.
  @param[out] HelpSize          pointer to size of allocated HelpText (may be updated)
  @param[in] Ascii              TRUE if the file is ASCII, FALSE otherwise.

  @retval EFI_OUT_OF_RESOURCES  a memory allocation failed.
  @retval EFI_SUCCESS           the section was found and its description stored in
                                an allocated buffer.
**/
EFI_STATUS
ManFileFindSections (
  IN SHELL_FILE_HANDLE  Handle,
  IN CONST CHAR16       *Sections,
  OUT CHAR16            **HelpText,
  OUT UINTN             *HelpSize,
  IN BOOLEAN            Ascii
  )
{
  EFI_STATUS  Status;
  CHAR16      *ReadLine;
  UINTN       Size;
  BOOLEAN     CurrentlyReading;
  CHAR16      *SectionName;
  UINTN       SectionLen;
  BOOLEAN     Found;

  if (  (Handle     == NULL)
     || (HelpText   == NULL)
     || (HelpSize   == NULL)
        )
  {
    return (EFI_INVALID_PARAMETER);
  }

  Status           = EFI_SUCCESS;
  CurrentlyReading = FALSE;
  Size             = 1024;
  Found            = FALSE;

  ReadLine = AllocateZeroPool (Size);
  if (ReadLine == NULL) {
    return (EFI_OUT_OF_RESOURCES);
  }

  for ( ; !ShellFileHandleEof (Handle); Size = 1024) {
    Status = ShellFileHandleReadLine (Handle, ReadLine, &Size, TRUE, &Ascii);
    if (ReadLine[0] == L'#') {
      //
      // Skip comment lines
      //
      continue;
    }

    //
    // ignore too small of buffer...
    //
    if (Status == EFI_BUFFER_TOO_SMALL) {
      Status = EFI_SUCCESS;
    }

    if (EFI_ERROR (Status)) {
      break;
    } else if (StrnCmp (ReadLine, L".TH", 3) == 0) {
      //
      // we hit the end of this commands section so stop.
      //
      break;
    } else if (StrnCmp (ReadLine, L".SH", 3) == 0) {
      if (Sections == NULL) {
        CurrentlyReading = TRUE;
        continue;
      }

      //
      // we found a section
      //
      if (CurrentlyReading) {
        CurrentlyReading = FALSE;
      }

      //
      // is this a section we want to read in?
      //
      for ( SectionName = ReadLine + 3
            ; *SectionName == L' '
            ; SectionName++)
      {
      }

      SectionLen  = StrLen (SectionName);
      SectionName = StrStr (Sections, SectionName);
      if (SectionName == NULL) {
        continue;
      }

      if ((*(SectionName + SectionLen) == CHAR_NULL) || (*(SectionName + SectionLen) == L',')) {
        CurrentlyReading = TRUE;
      }
    } else if (CurrentlyReading) {
      Found = TRUE;
      //
      // copy and save the current line.
      //
      ASSERT ((*HelpText == NULL && *HelpSize == 0) || (*HelpText != NULL));
      StrnCatGrow (HelpText, HelpSize, ReadLine, 0);
      StrnCatGrow (HelpText, HelpSize, L"\r\n", 0);
    }
  }

  FreePool (ReadLine);
  if (!Found && !EFI_ERROR (Status)) {
    return (EFI_NOT_FOUND);
  }

  return (Status);
}

/**
  Parses a line from a MAN file to see if it is the Title Header. If it is, then
  if the "Brief Description" is desired, allocate a buffer for it and return a
  copy. Upon a successful return the caller is responsible to free the memory in
  *BriefDesc

  Uses a simple state machine that allows "unlimited" whitespace before and after the
  ".TH", compares Command and the MAN file command name without respect to case, and
  allows "unlimited" whitespace and '0' and '1' characters before the Short Description.
  The PCRE regex describing this functionality is: ^\s*\.TH\s+(\S)\s[\s01]*(.*)$
  where group 1 is the Command Name and group 2 is the Short Description.

  @param[in] Command             name of command whose MAN file we think Line came from
  @param[in] Line                Pointer to a line from the MAN file
  @param[out] BriefDesc          pointer to pointer to string where description goes.
  @param[out] BriefSize          pointer to size of allocated BriefDesc
  @param[out] Found              TRUE if the Title Header was found and it belongs to Command

  @retval TRUE   Line contained the Title Header
  @retval FALSE  Line did not contain the Title Header
**/
BOOLEAN
IsTitleHeader (
  IN CONST CHAR16  *Command,
  IN CHAR16        *Line,
  OUT CHAR16       **BriefDesc OPTIONAL,
  OUT UINTN        *BriefSize OPTIONAL,
  OUT BOOLEAN      *Found
  )
{
  // The states of a simple state machine used to recognize a title header line
  // and to extract the Short Description, if desired.
  typedef enum {
    LookForThMacro, LookForCommandName, CompareCommands, GetBriefDescription, Final
  } STATEVALUES;

  STATEVALUES  State;
  UINTN        CommandIndex; // Indexes Command as we compare its chars to the MAN file.
  BOOLEAN      ReturnValue;  // TRUE if this the Title Header line of *some* MAN file.
  BOOLEAN      ReturnFound;  // TRUE if this the Title Header line of *the desired* MAN file.

  ReturnValue  = FALSE;
  ReturnFound  = FALSE;
  CommandIndex = 0;
  State        = LookForThMacro;

  do {
    if (*Line == L'\0') {
      break;
    }

    switch (State) {
      // Handle "^\s*.TH\s"
      // Go to state LookForCommandName if the title header macro is present; otherwise,
      // eat white space. If we see something other than white space, this is not a
      // title header line.
      case LookForThMacro:
        if ((StrnCmp (L".TH ", Line, 4) == 0) || (StrnCmp (L".TH\t", Line, 4) == 0)) {
          Line += 4;
          State = LookForCommandName;
        } else if ((*Line == L' ') || (*Line == L'\t')) {
          Line++;
        } else {
          State = Final;
        }

        break;

      // Handle "\s*"
      // Eat any "extra" whitespace after the title header macro (we have already seen
      // at least one white space character). Go to state CompareCommands when a
      // non-white space is seen.
      case LookForCommandName:
        if ((*Line == L' ') || (*Line == L'\t')) {
          Line++;
        } else {
          ReturnValue = TRUE;  // This is *some* command's title header line.
          State       = CompareCommands;
          // Do not increment Line; it points to the first character of the command
          // name on the title header line.
        }

        break;

      // Handle "(\S)\s"
      // Compare Command to the title header command name, ignoring case. When we
      // reach the end of the command (i.e. we see white space), the next state
      // depends on whether the caller wants a copy of the Brief Description.
      case CompareCommands:
        if ((*Line == L' ') || (*Line == L'\t')) {
          ReturnFound = TRUE;  // This is the desired command's title header line.
          State       = (BriefDesc == NULL) ? Final : GetBriefDescription;
        } else if (CharToUpper (*Line) != CharToUpper (*(Command + CommandIndex++))) {
          State = Final;
        }

        Line++;
        break;

      // Handle "[\s01]*(.*)$"
      // Skip whitespace, '0', and '1' characters, if any, prior to the brief description.
      // Return the description to the caller.
      case GetBriefDescription:
        if ((*Line != L' ') && (*Line != L'\t') && (*Line != L'0') && (*Line != L'1')) {
          *BriefSize = StrSize (Line);
          *BriefDesc = AllocateZeroPool (*BriefSize);
          if (*BriefDesc != NULL) {
            StrCpyS (*BriefDesc, (*BriefSize)/sizeof (CHAR16), Line);
          }

          State = Final;
        }

        Line++;
        break;

      default:
        break;
    }
  } while (State < Final);

  *Found = ReturnFound;
  return ReturnValue;
}

/**
  Parses through the MAN file specified by SHELL_FILE_HANDLE and returns the
  "Brief Description" for the .TH section as specified by Command.  If the
  command section is not found return EFI_NOT_FOUND.

  Upon a successful return the caller is responsible to free the memory in *BriefDesc

  @param[in] Handle              FileHandle to read from
  @param[in] Command             name of command's section to find as entered on the
                                 command line (may be a relative or absolute path or
                                 be in any case: upper, lower, or mixed in numerous ways!).
  @param[out] BriefDesc          pointer to pointer to string where description goes.
  @param[out] BriefSize          pointer to size of allocated BriefDesc
  @param[in, out] Ascii          TRUE if the file is ASCII, FALSE otherwise, will be
                                 set if the file handle is at the 0 position.

  @retval EFI_OUT_OF_RESOURCES  a memory allocation failed.
  @retval EFI_SUCCESS           the section was found and its description stored in
                                an allocated buffer if requested.
**/
EFI_STATUS
ManFileFindTitleSection (
  IN SHELL_FILE_HANDLE  Handle,
  IN CONST CHAR16       *Command,
  OUT CHAR16            **BriefDesc OPTIONAL,
  OUT UINTN             *BriefSize OPTIONAL,
  IN OUT BOOLEAN        *Ascii
  )
{
  EFI_STATUS  Status;
  CHAR16      *ReadLine;
  UINTN       Size;
  BOOLEAN     Found;
  UINTN       Start;

  if (  (Handle     == NULL)
     || (Command    == NULL)
     || ((BriefDesc != NULL) && (BriefSize == NULL))
        )
  {
    return (EFI_INVALID_PARAMETER);
  }

  Status = EFI_SUCCESS;
  Size   = 1024;
  Found  = FALSE;

  ReadLine = AllocateZeroPool (Size);
  if (ReadLine == NULL) {
    return (EFI_OUT_OF_RESOURCES);
  }

  //
  // Do not pass any leading path information that may be present to IsTitleHeader().
  //
  Start = StrLen (Command);
  while (  (Start != 0)
        && (*(Command + Start - 1) != L'\\')
        && (*(Command + Start - 1) != L'/')
        && (*(Command + Start - 1) != L':'))
  {
    --Start;
  }

  for ( ; !ShellFileHandleEof (Handle); Size = 1024) {
    Status = ShellFileHandleReadLine (Handle, ReadLine, &Size, TRUE, Ascii);
    //
    // ignore too small of buffer...
    //
    if (EFI_ERROR (Status) && (Status != EFI_BUFFER_TOO_SMALL)) {
      break;
    }

    Status = EFI_NOT_FOUND;
    if (IsTitleHeader (Command+Start, ReadLine, BriefDesc, BriefSize, &Found)) {
      Status = Found ? EFI_SUCCESS : EFI_NOT_FOUND;
      break;
    }
  }

  FreePool (ReadLine);
  return (Status);
}

/**
  This function returns the help information for the specified command. The help text
  will be parsed from a UEFI Shell manual page. (see UEFI Shell 2.0 Appendix B)

  If Sections is specified, then each section name listed will be compared in a casesensitive
  manner, to the section names described in Appendix B. If the section exists,
  it will be appended to the returned help text. If the section does not exist, no
  information will be returned. If Sections is NULL, then all help text information
  available will be returned.

  if BriefDesc is NULL, then the breif description will not be saved separately,
  but placed first in the main HelpText.

  @param[in] ManFileName        Points to the NULL-terminated UEFI Shell MAN file name.
  @param[in] Command            Points to the NULL-terminated UEFI Shell command name.
  @param[in] Sections           Points to the NULL-terminated comma-delimited
                                section names to return. If NULL, then all
                                sections will be returned.
  @param[out] BriefDesc         On return, points to a callee-allocated buffer
                                containing brief description text.
  @param[out] HelpText          On return, points to a callee-allocated buffer
                                containing all specified help text.

  @retval EFI_SUCCESS           The help text was returned.
  @retval EFI_OUT_OF_RESOURCES  The necessary buffer could not be allocated to hold the
                                returned help text.
  @retval EFI_INVALID_PARAMETER HelpText is NULL.
  @retval EFI_INVALID_PARAMETER ManFileName is invalid.
  @retval EFI_NOT_FOUND         There is no help text available for Command.
**/
EFI_STATUS
ProcessManFile (
  IN CONST CHAR16  *ManFileName,
  IN CONST CHAR16  *Command,
  IN CONST CHAR16  *Sections OPTIONAL,
  OUT CHAR16       **BriefDesc OPTIONAL,
  OUT CHAR16       **HelpText
  )
{
  CHAR16                       *TempString;
  SHELL_FILE_HANDLE            FileHandle;
  EFI_HANDLE                   CmdFileImgHandle;
  EFI_STATUS                   Status;
  UINTN                        HelpSize;
  UINTN                        BriefSize;
  UINTN                        StringIdWalker;
  BOOLEAN                      Ascii;
  CHAR16                       *CmdFileName;
  CHAR16                       *CmdFilePathName;
  EFI_DEVICE_PATH_PROTOCOL     *FileDevPath;
  EFI_DEVICE_PATH_PROTOCOL     *DevPath;
  EFI_HII_PACKAGE_LIST_HEADER  *PackageListHeader;

  if (  (ManFileName == NULL)
     || (Command     == NULL)
     || (HelpText    == NULL)
        )
  {
    return (EFI_INVALID_PARAMETER);
  }

  HelpSize          = 0;
  BriefSize         = 0;
  StringIdWalker    = 0;
  TempString        = NULL;
  Ascii             = FALSE;
  CmdFileName       = NULL;
  CmdFilePathName   = NULL;
  CmdFileImgHandle  = NULL;
  PackageListHeader = NULL;
  FileDevPath       = NULL;
  DevPath           = NULL;

  //
  // See if it's in HII first
  //
  TempString = ShellCommandGetCommandHelp (Command);
  if (TempString != NULL) {
    FileHandle = ConvertEfiFileProtocolToShellHandle (CreateFileInterfaceMem (TRUE), NULL);
    HelpSize   = StrLen (TempString) * sizeof (CHAR16);
    ShellWriteFile (FileHandle, &HelpSize, TempString);
    ShellSetFilePosition (FileHandle, 0);
    HelpSize  = 0;
    BriefSize = 0;
    Status    = ManFileFindTitleSection (FileHandle, Command, BriefDesc, &BriefSize, &Ascii);
    if (!EFI_ERROR (Status) && (HelpText != NULL)) {
      Status = ManFileFindSections (FileHandle, Sections, HelpText, &HelpSize, Ascii);
    }

    ShellCloseFile (&FileHandle);
  } else {
    //
    // If the image is a external app, check .MAN file first.
    //
    FileHandle = NULL;
    TempString = GetManFileName (ManFileName);
    if (TempString == NULL) {
      return (EFI_INVALID_PARAMETER);
    }

    Status = SearchPathForFile (TempString, &FileHandle);
    if (EFI_ERROR (Status)) {
      FileDevPath = FileDevicePath (NULL, TempString);
      DevPath     = AppendDevicePath (ShellInfoObject.ImageDevPath, FileDevPath);
      Status      = InternalOpenFileDevicePath (DevPath, &FileHandle, EFI_FILE_MODE_READ, 0);
      SHELL_FREE_NON_NULL (FileDevPath);
      SHELL_FREE_NON_NULL (DevPath);
    }

    if (!EFI_ERROR (Status)) {
      HelpSize  = 0;
      BriefSize = 0;
      Status    = ManFileFindTitleSection (FileHandle, Command, BriefDesc, &BriefSize, &Ascii);
      if (!EFI_ERROR (Status) && (HelpText != NULL)) {
        Status = ManFileFindSections (FileHandle, Sections, HelpText, &HelpSize, Ascii);
      }

      ShellInfoObject.NewEfiShellProtocol->CloseFile (FileHandle);
      if (!EFI_ERROR (Status)) {
        //
        // Get help text from .MAN file success.
        //
        goto Done;
      }
    }

    //
    // Load the app image to check  EFI_HII_PACKAGE_LIST_PROTOCOL.
    //
    CmdFileName = GetExecuatableFileName (TempString);
    if (CmdFileName == NULL) {
      Status = EFI_OUT_OF_RESOURCES;
      goto Done;
    }

    //
    // If the file in CWD then use the file name, else use the full
    // path name.
    //
    CmdFilePathName = ShellFindFilePath (CmdFileName);
    if (CmdFilePathName == NULL) {
      Status = EFI_NOT_FOUND;
      goto Done;
    }

    DevPath = ShellInfoObject.NewEfiShellProtocol->GetDevicePathFromFilePath (CmdFilePathName);
    Status  = gBS->LoadImage (FALSE, gImageHandle, DevPath, NULL, 0, &CmdFileImgHandle);
    if (EFI_ERROR (Status)) {
      //
      // With EFI_SECURITY_VIOLATION retval, the Image was loaded and an ImageHandle was created
      // with a valid EFI_LOADED_IMAGE_PROTOCOL, but the image can not be started right now.
      // If the caller doesn't have the option to defer the execution of an image, we should
      // unload image for the EFI_SECURITY_VIOLATION to avoid the resource leak.
      //
      if (Status == EFI_SECURITY_VIOLATION) {
        gBS->UnloadImage (CmdFileImgHandle);
      }

      *HelpText = NULL;
      goto Done;
    }

    Status = gBS->OpenProtocol (
                    CmdFileImgHandle,
                    &gEfiHiiPackageListProtocolGuid,
                    (VOID **)&PackageListHeader,
                    gImageHandle,
                    NULL,
                    EFI_OPEN_PROTOCOL_GET_PROTOCOL
                    );
    if (EFI_ERROR (Status)) {
      *HelpText = NULL;
      goto Done;
    }

    //
    // If get package list on image handle, install it on HiiDatabase.
    //
    Status = gBS->InstallProtocolInterface (
                    &mShellManDriverHandle,
                    &gEfiDevicePathProtocolGuid,
                    EFI_NATIVE_INTERFACE,
                    &mShellManHiiDevicePath
                    );
    if (EFI_ERROR (Status)) {
      goto Done;
    }

    Status = gHiiDatabase->NewPackageList (
                             gHiiDatabase,
                             PackageListHeader,
                             mShellManDriverHandle,
                             &mShellManHiiHandle
                             );
    if (EFI_ERROR (Status)) {
      goto Done;
    }

    StringIdWalker = 1;
    do {
      SHELL_FREE_NON_NULL (TempString);
      if (BriefDesc != NULL) {
        SHELL_FREE_NON_NULL (*BriefDesc);
      }

      TempString = HiiGetString (mShellManHiiHandle, (EFI_STRING_ID)StringIdWalker, NULL);
      if (TempString == NULL) {
        Status = EFI_NOT_FOUND;
        goto Done;
      }

      FileHandle = ConvertEfiFileProtocolToShellHandle (CreateFileInterfaceMem (TRUE), NULL);
      HelpSize   = StrLen (TempString) * sizeof (CHAR16);
      ShellWriteFile (FileHandle, &HelpSize, TempString);
      ShellSetFilePosition (FileHandle, 0);
      HelpSize  = 0;
      BriefSize = 0;
      Status    = ManFileFindTitleSection (FileHandle, Command, BriefDesc, &BriefSize, &Ascii);
      if (!EFI_ERROR (Status) && (HelpText != NULL)) {
        Status = ManFileFindSections (FileHandle, Sections, HelpText, &HelpSize, Ascii);
      }

      ShellCloseFile (&FileHandle);
      if (!EFI_ERROR (Status)) {
        //
        // Found what we need and return
        //
        goto Done;
      }

      StringIdWalker += 1;
    } while (StringIdWalker < 0xFFFF && TempString != NULL);
  }

Done:
  if (mShellManDriverHandle != NULL) {
    gBS->UninstallProtocolInterface (
           mShellManDriverHandle,
           &gEfiDevicePathProtocolGuid,
           &mShellManHiiDevicePath
           );
    mShellManDriverHandle = NULL;
  }

  if (mShellManHiiHandle != NULL) {
    HiiRemovePackages (mShellManHiiHandle);
    mShellManHiiHandle = NULL;
  }

  if (CmdFileImgHandle != NULL) {
    Status = gBS->UnloadImage (CmdFileImgHandle);
  }

  SHELL_FREE_NON_NULL (TempString);
  SHELL_FREE_NON_NULL (CmdFileName);
  SHELL_FREE_NON_NULL (CmdFilePathName);
  SHELL_FREE_NON_NULL (FileDevPath);
  SHELL_FREE_NON_NULL (DevPath);

  return (Status);
}