summaryrefslogtreecommitdiff
path: root/ArmPkg/Application/LinuxLoader/LinuxLoaderShellApp.c
blob: 2245185394c846b6f05e464672cef461e9452b6c (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
/** @file
*
*  Copyright (c) 2011-2015, ARM Limited. All rights reserved.
*
*  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 "LinuxLoader.h"

//
// Internal variables
//

CONST EFI_GUID mLinuxLoaderHiiGuid = {
                         0xd5d16edc, 0x35c5, 0x4866,
                         {0xbd, 0xe5, 0x2b, 0x64, 0xa2, 0x26, 0x55, 0x6e}
                         };
EFI_HANDLE mLinuxLoaderHiiHandle;

STATIC CONST SHELL_PARAM_ITEM ParamList[] = {
  {L"-f", TypeValue},
  {L"-d", TypeValue},
  {L"-c", TypeValue},
  {L"-a", TypeValue},
  {NULL , TypeMax  }
  };

/**
  Print a string given the "HII Id" of the format string and a list of
  arguments.

  @param[in] Language           The language of the string to retrieve. If
                                this parameter is NULL, then the current
                                platform language is used.
  @param[in] HiiFormatStringId  The format string Id for getting from Hii.
  @param[in] ...                The variable argument list.

  @retval EFI_SUCCESS           The printing was successful.
  @retval EFI_OUT_OF_RESOURCES  A memory allocation failed.

**/
EFI_STATUS
PrintHii (
  IN CONST CHAR8          *Language OPTIONAL,
  IN CONST EFI_STRING_ID  HiiFormatStringId,
  ...
  )
{
  VA_LIST  Marker;
  CHAR16   *HiiFormatString;
  CHAR16   Buffer[MAX_MSG_LEN];

  VA_START (Marker, HiiFormatStringId);

  HiiFormatString = HiiGetString (mLinuxLoaderHiiHandle, HiiFormatStringId, Language);
  if (HiiFormatString != NULL) {
    UnicodeVSPrint (Buffer, sizeof (Buffer), HiiFormatString, Marker);
    Print (L"%s", Buffer);
    FreePool (HiiFormatString);
  } else {
    ASSERT (FALSE);
    return EFI_OUT_OF_RESOURCES;
  }

  VA_END (Marker);

  return EFI_SUCCESS;
}

/**
  Print the help.

  @param[in]  Language  The language of the string to retrieve.  If this
                        parameter is NULL, then the current platform
                        language is used.
**/
VOID
PrintHelp (
  IN CONST CHAR8  *Language OPTIONAL
  )
{
  CHAR16  *Help;
  CHAR16  *Walker;
  CHAR16  *LineEnd;

  //
  // Print the help line by line as it is too big to be printed at once.
  //

  Help = HiiGetString (mLinuxLoaderHiiHandle, STRING_TOKEN (STR_HELP), Language);
  if (Help != NULL) {
    Walker = Help;
    while (*Walker != L'\0') {
      LineEnd = StrStr (Walker, L"\r\n");
      if (LineEnd != NULL) {
        *LineEnd = L'\0';
      }
      Print (L"%s\r\n", Walker);
      if (LineEnd == NULL) {
        break;
      }
      Walker = LineEnd + 2;
    }
    FreePool (Help);
  }

}

/**
  Process the Shell parameters in the case the application has been called
  from the EFI Shell.

  @param[out]  KernelPath        A pointer to the buffer where the path
                                 to the Linux kernel (EFI Shell file path
                                 or device path is stored. The address of
                                 the buffer is NULL in case of an error.
                                 Otherwise, the returned address is the
                                 address of a buffer allocated with
                                 a call to AllocatePool() that has to be
                                 freed by the caller.
  @param[out]  FdtPath           A pointer to the buffer where the path
                                 to the FDT (EFI Shell file path or
                                 device path) is stored. The address of
                                 the buffer is NULL in case of an error or
                                 if the path to the FDT is not defined.
                                 Otherwise, the returned address is the
                                 address of a buffer allocated with a call
                                 to AllocatePool() that has to be freed by
                                 the caller.
  @param[out]  InitrdPath        A pointer to the buffer where the path
                                 (EFI Shell file path or device path)
                                 to the RAM root file system is stored.
                                 The address of the buffer is NULL in case
                                 of an error or if the path to the RAM root
                                 file system is not defined. Otherwise, the
                                 returned address is the address of a
                                 buffer allocated with a call to
                                 AllocatePool() that has to be freed by
                                 the caller.
  @param[out]  LinuxCommandLine  A pointer to the buffer where the Linux
                                 kernel command line is stored. The address
                                 of the buffer is NULL in case of an error
                                 or if the Linux command line is not
                                 defined. Otherwise, the returned address
                                 is the address of a buffer allocated with
                                 a call to AllocatePool() that has to be
                                 freed by the caller.
  @param[out]  AtagMachineType   Value of the ARM Machine Type

  @retval  EFI_SUCCESS            The processing was successfull.
  @retval  EFI_ABORTED            The initialisation of the Shell Library failed.
  @retval  EFI_NOT_FOUND          Path to the Linux kernel not found.
  @retval  EFI_INVALID_PARAMETER  At least one parameter is not valid or there is a
                                  conflict between two parameters.
  @retval  EFI_OUT_OF_RESOURCES   A memory allocation failed.

**/
EFI_STATUS
ProcessShellParameters (
  OUT  CHAR16   **KernelPath,
  OUT  CHAR16   **FdtPath,
  OUT  CHAR16   **InitrdPath,
  OUT  CHAR16   **LinuxCommandLine,
  OUT  UINTN    *AtagMachineType
  )
{
  EFI_STATUS    Status;
  LIST_ENTRY    *CheckPackage;
  CHAR16        *ProblemParam;
  CONST CHAR16  *FlagValue;
  CONST CHAR16  *ParameterValue;
  UINTN         LinuxCommandLineLen;


  *KernelPath       = NULL;
  *FdtPath          = NULL;
  *InitrdPath       = NULL;
  *LinuxCommandLine = NULL;
  *AtagMachineType  = ARM_FDT_MACHINE_TYPE;

  //
  // Initialise the Shell Library as we are going to use it.
  // Assert that the return code is EFI_SUCCESS as it should.
  // To anticipate any change is the codes returned by
  // ShellInitialize(), leave in case of error.
  //
  Status = ShellInitialize ();
  if (EFI_ERROR (Status)) {
    ASSERT_EFI_ERROR (Status);
    return EFI_ABORTED;
  }

  Status = ShellCommandLineParse (ParamList, &CheckPackage, &ProblemParam, TRUE);
  if (EFI_ERROR (Status)) {
    if ((Status == EFI_VOLUME_CORRUPTED) &&
        (ProblemParam != NULL) ) {
      PrintHii (
        NULL, STRING_TOKEN (STR_SHELL_INVALID_PARAMETER), ProblemParam
        );
      FreePool (ProblemParam);
    } else {
      ASSERT (FALSE);
    }
    goto Error;
  }

  Status = EFI_INVALID_PARAMETER;
  if (ShellCommandLineGetCount (CheckPackage) != 2) {
    PrintHelp (NULL);
    goto Error;
  }

  Status = EFI_OUT_OF_RESOURCES;

  FlagValue = ShellCommandLineGetValue (CheckPackage, L"-a");
  if (FlagValue != NULL) {
    if (ShellCommandLineGetFlag (CheckPackage, L"-d")) {
      PrintHii (NULL, STRING_TOKEN (STR_ATAG_FDT_CONFLICT));
      goto Error;
    }
    *AtagMachineType = StrDecimalToUintn (FlagValue);
  }

  ParameterValue = ShellCommandLineGetRawValue (CheckPackage, 1);
  *KernelPath = AllocateCopyPool (StrSize (ParameterValue), ParameterValue);
  if (*KernelPath == NULL) {
    goto Error;
  }

  FlagValue = ShellCommandLineGetValue (CheckPackage, L"-d");
  if (FlagValue != NULL) {
    *FdtPath = AllocateCopyPool (StrSize (FlagValue), FlagValue);
    if (*FdtPath == NULL) {
      goto Error;
    }
  }

  FlagValue = ShellCommandLineGetValue (CheckPackage, L"-f");
  if (FlagValue != NULL) {
    *InitrdPath = AllocateCopyPool (StrSize (FlagValue), FlagValue);
    if (*InitrdPath == NULL) {
      goto Error;
    }
  }

  FlagValue = ShellCommandLineGetValue (CheckPackage, L"-c");
  if (FlagValue != NULL) {
    LinuxCommandLineLen = StrLen (FlagValue);
    if ((LinuxCommandLineLen != 0) &&
        (FlagValue[0] == L'"'    ) &&
        (FlagValue[LinuxCommandLineLen - 1] == L'"')) {
      FlagValue++;
      LinuxCommandLineLen -= 2;
    }

    *LinuxCommandLine = AllocateCopyPool (
                          (LinuxCommandLineLen + 1) * sizeof (CHAR16),
                          FlagValue
                          );
    if (*LinuxCommandLine == NULL) {
      goto Error;
    }
    (*LinuxCommandLine)[LinuxCommandLineLen] = L'\0';
  }

  Status = EFI_SUCCESS;

Error:
  ShellCommandLineFreeVarList (CheckPackage);

  if (EFI_ERROR (Status)) {
    if (*KernelPath != NULL) {
      FreePool (*KernelPath);
      *KernelPath = NULL;
    }
    if (*FdtPath != NULL) {
      FreePool (*FdtPath);
      *FdtPath = NULL;
    }
    if (*InitrdPath != NULL) {
      FreePool (*InitrdPath);
      *InitrdPath = NULL;
    }
    if (*LinuxCommandLine != NULL) {
      FreePool (*LinuxCommandLine);
      *LinuxCommandLine = NULL;
    }
  }

  return Status;
}