summaryrefslogtreecommitdiff
path: root/edk2/ShellPkg/Library/UefiShellDriver1CommandsLib/DevTree.c
blob: a66bdec06b20c5de3b4dd9b2e4b1f61190bec5f4 (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
/** @file
  Main file for DevTree shell Driver1 function.

  Copyright (c) 2010 - 2011, Intel Corporation. All rights reserved.<BR>
  This program and the accompanying materials
  are licensed and made available under the terms and conditions of the BSD License
  which accompanies this distribution.  The full text of the license may be found at
  http://opensource.org/licenses/bsd-license.php

  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.

**/

#include "UefiShellDriver1CommandsLib.h"

STATIC CONST SHELL_PARAM_ITEM ParamList[] = {
  {L"-d", TypeFlag},
  {L"-l", TypeValue},
  {NULL, TypeMax}
  };

/**
  Display a tree starting from this handle.

  @param[in] TheHandle      The handle to start with.
  @param[in] Lang           Optionally, a UEFI defined language code.
  @param[in] UseDevPaths    TRUE to display info from DevPath as identifiers.
                            FALSE will use component name protocol instead.
  @param[in] IndentCharCount   How many characters to indent (allows for recursion).
  @param[in] HiiString      The string from HII to use for output.

  @retval SHELL_SUCCESS     The operation was successful.
**/
SHELL_STATUS
EFIAPI
DoDevTreeForHandle(
  IN CONST EFI_HANDLE TheHandle,
  IN CONST CHAR8      *Lang OPTIONAL,
  IN CONST BOOLEAN    UseDevPaths,
  IN CONST UINTN      IndentCharCount,
  IN CONST CHAR16     *HiiString
  )
{
  SHELL_STATUS        ShellStatus;
  EFI_STATUS          Status;
  CHAR16              *FormatString;
  CHAR16              *Name;
  EFI_HANDLE          *ChildHandleBuffer;
  UINTN               ChildCount;
  UINTN               LoopVar;

  Status              = EFI_SUCCESS;
  ShellStatus         = SHELL_SUCCESS;
  Name                = NULL;
  ChildHandleBuffer   = NULL;
  ChildCount          = 0;

  ASSERT(TheHandle    != NULL);
  //
  // We want controller handles.  they will not have LoadedImage or DriverBinding (or others...)
  //
  Status = gBS->OpenProtocol (
                TheHandle,
                &gEfiDriverBindingProtocolGuid,
                NULL,
                NULL,
                NULL,
                EFI_OPEN_PROTOCOL_TEST_PROTOCOL
               );
  if (!EFI_ERROR (Status)) {
    return SHELL_SUCCESS;
  }

  Status = gBS->OpenProtocol (
                TheHandle,
                &gEfiLoadedImageProtocolGuid,
                NULL,
                NULL,
                NULL,
                EFI_OPEN_PROTOCOL_TEST_PROTOCOL
               );
  if (!EFI_ERROR (Status)) {
    return SHELL_SUCCESS;
  }

  //
  // If we are at the begining then we want root handles they have no parents and do have device path.
  //
  if (IndentCharCount == 0) {
    Status = gBS->OpenProtocol (
                  TheHandle,
                  &gEfiDevicePathProtocolGuid,
                  NULL,
                  NULL,
                  NULL,
                  EFI_OPEN_PROTOCOL_TEST_PROTOCOL
                 );
    if (EFI_ERROR (Status)) {
      return SHELL_SUCCESS;
    }
  }

  FormatString        = AllocateZeroPool(StrSize(HiiString) + (10)*sizeof(FormatString[0]));

  ASSERT(HiiString    != NULL);
  ASSERT(FormatString != NULL);

  //
  // we generate the format string on the fly so that we can control the
  // number of space characters that the first (empty) string has.  this
  // handles the indenting.
  //

  UnicodeSPrint(FormatString, StrSize(HiiString) + (10)*sizeof(FormatString[0]), L"%%%ds %s", IndentCharCount, HiiString);
  gEfiShellProtocol->GetDeviceName((EFI_HANDLE)TheHandle, !UseDevPaths?EFI_DEVICE_NAME_USE_COMPONENT_NAME|EFI_DEVICE_NAME_USE_DEVICE_PATH:EFI_DEVICE_NAME_USE_DEVICE_PATH, (CHAR8*)Lang, &Name);
  //
  // print out the information for ourselves
  //
  ShellPrintEx(
    -1,
    -1,
    FormatString,
    L"",
    ConvertHandleToHandleIndex(TheHandle),
    Name==NULL?L"Unknown":Name);

  FreePool(FormatString);
  if (Name != NULL) {
    FreePool(Name);
  }

  //
  // recurse on each child handle with IndentCharCount + 2
  //
  ParseHandleDatabaseForChildControllers(TheHandle, &ChildCount, &ChildHandleBuffer);
  for (LoopVar = 0 ; LoopVar < ChildCount && ShellStatus == SHELL_SUCCESS; LoopVar++){
    ShellStatus = DoDevTreeForHandle(ChildHandleBuffer[LoopVar], Lang, UseDevPaths, IndentCharCount+2, HiiString);
  }

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

  return (ShellStatus);
}

/**
  Function for 'devtree' 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
ShellCommandRunDevTree (
  IN EFI_HANDLE        ImageHandle,
  IN EFI_SYSTEM_TABLE  *SystemTable
  )
{
  EFI_STATUS          Status;
  LIST_ENTRY          *Package;
  CHAR16              *ProblemParam;
  SHELL_STATUS        ShellStatus;
  CHAR8               *Language;
  CONST CHAR16        *Lang;
  CHAR16              *HiiString;
  UINTN               LoopVar;
  EFI_HANDLE          TheHandle;
  BOOLEAN             FlagD;
  UINT64              Intermediate;

  ShellStatus         = SHELL_SUCCESS;
  Status              = EFI_SUCCESS;
  Language                = NULL;

  //
  // 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 (ParamList, &Package, &ProblemParam, TRUE);
  if (EFI_ERROR(Status)) {
    if (Status == EFI_VOLUME_CORRUPTED && ProblemParam != NULL) {
      ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellDriver1HiiHandle, ProblemParam);
      FreePool(ProblemParam);
      ShellStatus = SHELL_INVALID_PARAMETER;
    } else {
      ASSERT(FALSE);
    }
  } else {
    if (ShellCommandLineGetCount(Package) > 2) {
      ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellDriver1HiiHandle);
      ShellCommandLineFreeVarList (Package);
      return (SHELL_INVALID_PARAMETER);
    }
    Lang = ShellCommandLineGetValue(Package, L"-l");
    if (Lang != NULL) {
      Language = AllocateZeroPool(StrSize(Lang));
      AsciiSPrint(Language, StrSize(Lang), "%S", Lang);
    } else if (!ShellCommandLineGetFlag(Package, L"-l")){
      ASSERT(Language == NULL);
//      Language = AllocateZeroPool(10);
//      AsciiSPrint(Language, 10, "en-us");
    } else {
      ASSERT(Language == NULL);
      ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_VALUE), gShellDriver1HiiHandle, L"-l");
      ShellCommandLineFreeVarList (Package);
      return (SHELL_INVALID_PARAMETER);
    }
    FlagD = ShellCommandLineGetFlag(Package, L"-d");

    Lang = ShellCommandLineGetRawValue(Package, 1);
    HiiString = HiiGetString(gShellDriver1HiiHandle, STRING_TOKEN (STR_DEV_TREE_OUTPUT), Language);

    if (Lang == NULL) {
      for (LoopVar = 1 ; ; LoopVar++){
        TheHandle = ConvertHandleIndexToHandle(LoopVar);
        if (TheHandle == NULL){
          break;
        }
        ShellStatus = DoDevTreeForHandle(TheHandle, Language, FlagD, 0, HiiString);
      }
    } else {
      Status = ShellConvertStringToUint64(Lang, &Intermediate, TRUE, FALSE);
      if (EFI_ERROR(Status) || ConvertHandleIndexToHandle((UINTN)Intermediate) == NULL) {
        ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_INV_HANDLE), gShellDriver1HiiHandle, Lang);
        ShellStatus = SHELL_INVALID_PARAMETER;
      } else {
        ShellStatus = DoDevTreeForHandle(ConvertHandleIndexToHandle((UINTN)Intermediate), Language, FlagD, 0, HiiString);
      }
    }

    if (HiiString != NULL) {
      FreePool(HiiString);
    }
    SHELL_FREE_NON_NULL(Language);
    ShellCommandLineFreeVarList (Package);
  }

  return (ShellStatus);
}