summaryrefslogtreecommitdiffstats
path: root/ArmPkg/Application/LinuxLoader/LinuxLoaderEfiApp.c
blob: 57a9cd38cf68270a3f1dbc1974bcf47a5782d610 (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
/** @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"

/**
  Extract the next item from the command line.

  The items are separated by spaces. Quotation marks (") are used for argument
  grouping and the escaping character is "^" as for the EFI Shell command lines.

  @param[in out]  CommandLine  Command line pointer.
  @param[out]     Item         Pointer to the allocated buffer where the
                               item is stored.

  @retval  EFI_SUCCESS           The token was found and extracted.
  @retval  EFI_NOT_FOUND         No item found.
  @retval  EFI_OUT_OF_RESOURCES  The memory allocation failed.

**/
STATIC
EFI_STATUS
ExtractNextItem (
  IN OUT CONST CHAR16  **CommandLine,
  OUT CHAR16           **Item
  )
{
  CONST CHAR16  *Walker;
  VOID          *Buffer;
  CHAR16        *WritePtr;
  BOOLEAN       InQuotedString;
  BOOLEAN       Interpret;

  for (Walker = *CommandLine; *Walker == L' '; Walker++) {
    ;
  }

  Buffer = AllocatePool (StrSize (Walker));
  if (Buffer == NULL) {
    return EFI_OUT_OF_RESOURCES;
  }

  for (WritePtr = Buffer, Interpret = TRUE, InQuotedString = FALSE;
       ((*Walker != L' ') || InQuotedString) && (*Walker != L'\0');
       Walker++
       ) {
    if (Interpret) {
      if (*Walker == L'^') {
        Interpret = FALSE;
        continue;
      }
      if (*Walker == L'"') {
        InQuotedString = !InQuotedString;
        continue;
      }
    } else {
      Interpret = TRUE;
    }
    *(WritePtr++) = *Walker;
  }

  if (WritePtr == Buffer) {
    FreePool (Buffer);
    return EFI_NOT_FOUND;
  }

  *WritePtr = L'\0';
  *CommandLine = Walker;
  *Item        = Buffer;

  return EFI_SUCCESS;
}

/**
  Check if an item of the command line is a flag or not.

  @param[in]  Item  Command line item.

  @retval  TRUE   The item is a flag.
  @retval  FALSE  The item is not a flag.

**/
STATIC
BOOLEAN
IsFlag (
  IN CONST CHAR16 *Item
  )
{
  return ((Item[0] == L'-') && (Item[2] == L'\0'));
}

/**
  Process the application command line.

  @param[out]  KernelTextDevicePath  A pointer to the buffer where the device
                                     path to the Linux kernel 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]  FdtTextDevicePath     A pointer to the buffer where the device
                                     path to the FDT is stored. The address of
                                     the buffer is NULL in case of an error or
                                     if the device 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]  InitrdTextDevicePath  A pointer to the buffer where the 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 device 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_NOT_FOUND          EFI_LOADED_IMAGE_PROTOCOL not found.
  @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
ProcessAppCommandLine (
  OUT  CHAR16   **KernelTextDevicePath,
  OUT  CHAR16   **FdtTextDevicePath,
  OUT  CHAR16   **InitrdTextDevicePath,
  OUT  CHAR16   **LinuxCommandLine,
  OUT  UINTN    *AtagMachineType
  )
{
  EFI_STATUS                 Status;
  EFI_STATUS                 Status2;
  EFI_LOADED_IMAGE_PROTOCOL  *LoadedImage;
  CONST CHAR16               *Walker;
  CHAR16                     *Item;
  CHAR16                     Flag;
  BOOLEAN                    HasAtagSupport;
  BOOLEAN                    HasFdtSupport;

  *KernelTextDevicePath = NULL;
  *FdtTextDevicePath    = NULL;
  *InitrdTextDevicePath = NULL;
  *LinuxCommandLine     = NULL;
  *AtagMachineType      = ARM_FDT_MACHINE_TYPE;

  HasAtagSupport        = FALSE;
  HasFdtSupport         = FALSE;

  Status = gBS->HandleProtocol (
                  gImageHandle,
                  &gEfiLoadedImageProtocolGuid,
                  (VOID**)&LoadedImage
                  );
  if (EFI_ERROR (Status)) {
    ASSERT_EFI_ERROR (Status);
    return Status;
  }

  Walker = (CHAR16*)LoadedImage->LoadOptions;
  if (Walker == NULL) {
    PrintHelp (NULL);
    return EFI_INVALID_PARAMETER;
  }

  //
  // Get the device path to the Linux kernel.
  //

  Status = ExtractNextItem (&Walker, &Item);
  if (!EFI_ERROR (Status)) {
    if (!IsFlag (Item)) {
      *KernelTextDevicePath = Item;
    } else {
      PrintHii (NULL, STRING_TOKEN (STR_MISSING_KERNEL_PATH));
      FreePool (Item);
      return EFI_NOT_FOUND;
    }
  } else {
    if (Status != EFI_NOT_FOUND) {
      return Status;
    }
    PrintHelp (NULL);
    return EFI_INVALID_PARAMETER;
  }

  Status = EFI_INVALID_PARAMETER;
  while (*Walker != L'\0') {
    Status2 = ExtractNextItem (&Walker, &Item);
    if (Status2 == EFI_NOT_FOUND) {
      break;
    }
    if (EFI_ERROR (Status2)) {
      Status = Status2;
      goto Error;
    }

    if (!IsFlag (Item)) {
      PrintHii (NULL, STRING_TOKEN (STR_INVALID_FLAG), Item[0], Item[1]);
      FreePool (Item);
      goto Error;
    }
    Flag = Item[1];
    FreePool (Item);

    Status2 = ExtractNextItem (&Walker, &Item);
    if (Status2 == EFI_NOT_FOUND) {
      PrintHii (NULL, STRING_TOKEN (STR_MISSING_VALUE), Flag);
      goto Error;
    }
    if (EFI_ERROR (Status2)) {
      Status = Status2;
      goto Error;
    }
    if (IsFlag (Item)) {
      PrintHii (NULL, STRING_TOKEN (STR_MISSING_VALUE), Flag);
      FreePool (Item);
      goto Error;
    }

    switch (Flag) {
    case  L'a':
      if (HasFdtSupport) {
        PrintHii (NULL, STRING_TOKEN (STR_ATAG_FDT_CONFLICT));
        goto Error;
      }
      *AtagMachineType = StrDecimalToUintn (Item);
      HasAtagSupport = TRUE;
      break;
    case L'd':
      *FdtTextDevicePath = Item;
      if (HasAtagSupport) {
        PrintHii (NULL, STRING_TOKEN (STR_ATAG_FDT_CONFLICT));
        goto Error;
      }
      HasFdtSupport = TRUE;
      break;

    case L'c':
      *LinuxCommandLine = Item;
      break;

    case L'f':
      *InitrdTextDevicePath = Item;
      break;

    default:
      PrintHii (NULL, STRING_TOKEN (STR_INVALID_FLAG), L'-', Flag);
      FreePool (Item);
      goto Error;
    }
  }

  Status = EFI_SUCCESS;

Error:
  if (EFI_ERROR (Status)) {
    if (*KernelTextDevicePath != NULL) {
      FreePool (*KernelTextDevicePath);
      *KernelTextDevicePath = NULL;
    }
    if (*FdtTextDevicePath != NULL) {
      FreePool (*FdtTextDevicePath);
      *FdtTextDevicePath = NULL;
    }
    if (*InitrdTextDevicePath != NULL) {
      FreePool (*InitrdTextDevicePath);
      *InitrdTextDevicePath = NULL;
    }
    if (*LinuxCommandLine != NULL) {
      FreePool (*LinuxCommandLine);
      *LinuxCommandLine = NULL;
    }
  }

  return Status;
}