Home | History | Annotate | Download | only in LinuxLoader
      1 /** @file
      2 *
      3 *  Copyright (c) 2011-2015, ARM Limited. All rights reserved.
      4 *
      5 *  This program and the accompanying materials
      6 *  are licensed and made available under the terms and conditions of the BSD License
      7 *  which accompanies this distribution.  The full text of the license may be found at
      8 *  http://opensource.org/licenses/bsd-license.php
      9 *
     10 *  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
     11 *  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
     12 *
     13 **/
     14 
     15 #include "LinuxLoader.h"
     16 
     17 //
     18 // Internal variables
     19 //
     20 
     21 CONST EFI_GUID mLinuxLoaderHiiGuid = {
     22                          0xd5d16edc, 0x35c5, 0x4866,
     23                          {0xbd, 0xe5, 0x2b, 0x64, 0xa2, 0x26, 0x55, 0x6e}
     24                          };
     25 EFI_HANDLE mLinuxLoaderHiiHandle;
     26 
     27 STATIC CONST SHELL_PARAM_ITEM ParamList[] = {
     28   {L"-f", TypeValue},
     29   {L"-d", TypeValue},
     30   {L"-c", TypeValue},
     31   {L"-a", TypeValue},
     32   {NULL , TypeMax  }
     33   };
     34 
     35 /**
     36   Print a string given the "HII Id" of the format string and a list of
     37   arguments.
     38 
     39   @param[in] Language           The language of the string to retrieve. If
     40                                 this parameter is NULL, then the current
     41                                 platform language is used.
     42   @param[in] HiiFormatStringId  The format string Id for getting from Hii.
     43   @param[in] ...                The variable argument list.
     44 
     45   @retval EFI_SUCCESS           The printing was successful.
     46   @retval EFI_OUT_OF_RESOURCES  A memory allocation failed.
     47 
     48 **/
     49 EFI_STATUS
     50 PrintHii (
     51   IN CONST CHAR8          *Language OPTIONAL,
     52   IN CONST EFI_STRING_ID  HiiFormatStringId,
     53   ...
     54   )
     55 {
     56   VA_LIST  Marker;
     57   CHAR16   *HiiFormatString;
     58   CHAR16   Buffer[MAX_MSG_LEN];
     59 
     60   VA_START (Marker, HiiFormatStringId);
     61 
     62   HiiFormatString = HiiGetString (mLinuxLoaderHiiHandle, HiiFormatStringId, Language);
     63   if (HiiFormatString != NULL) {
     64     UnicodeVSPrint (Buffer, sizeof (Buffer), HiiFormatString, Marker);
     65     Print (L"%s", Buffer);
     66     FreePool (HiiFormatString);
     67   } else {
     68     ASSERT (FALSE);
     69     return EFI_OUT_OF_RESOURCES;
     70   }
     71 
     72   VA_END (Marker);
     73 
     74   return EFI_SUCCESS;
     75 }
     76 
     77 /**
     78   Print the help.
     79 
     80   @param[in]  Language  The language of the string to retrieve.  If this
     81                         parameter is NULL, then the current platform
     82                         language is used.
     83 **/
     84 VOID
     85 PrintHelp (
     86   IN CONST CHAR8  *Language OPTIONAL
     87   )
     88 {
     89   CHAR16  *Help;
     90   CHAR16  *Walker;
     91   CHAR16  *LineEnd;
     92 
     93   //
     94   // Print the help line by line as it is too big to be printed at once.
     95   //
     96 
     97   Help = HiiGetString (mLinuxLoaderHiiHandle, STRING_TOKEN (STR_HELP), Language);
     98   if (Help != NULL) {
     99     Walker = Help;
    100     while (*Walker != L'\0') {
    101       LineEnd = StrStr (Walker, L"\r\n");
    102       if (LineEnd != NULL) {
    103         *LineEnd = L'\0';
    104       }
    105       Print (L"%s\r\n", Walker);
    106       if (LineEnd == NULL) {
    107         break;
    108       }
    109       Walker = LineEnd + 2;
    110     }
    111     FreePool (Help);
    112   }
    113 
    114 }
    115 
    116 /**
    117   Process the Shell parameters in the case the application has been called
    118   from the EFI Shell.
    119 
    120   @param[out]  KernelPath        A pointer to the buffer where the path
    121                                  to the Linux kernel (EFI Shell file path
    122                                  or device path is stored. The address of
    123                                  the buffer is NULL in case of an error.
    124                                  Otherwise, the returned address is the
    125                                  address of a buffer allocated with
    126                                  a call to AllocatePool() that has to be
    127                                  freed by the caller.
    128   @param[out]  FdtPath           A pointer to the buffer where the path
    129                                  to the FDT (EFI Shell file path or
    130                                  device path) is stored. The address of
    131                                  the buffer is NULL in case of an error or
    132                                  if the path to the FDT is not defined.
    133                                  Otherwise, the returned address is the
    134                                  address of a buffer allocated with a call
    135                                  to AllocatePool() that has to be freed by
    136                                  the caller.
    137   @param[out]  InitrdPath        A pointer to the buffer where the path
    138                                  (EFI Shell file path or device path)
    139                                  to the RAM root file system is stored.
    140                                  The address of the buffer is NULL in case
    141                                  of an error or if the path to the RAM root
    142                                  file system is not defined. Otherwise, the
    143                                  returned address is the address of a
    144                                  buffer allocated with a call to
    145                                  AllocatePool() that has to be freed by
    146                                  the caller.
    147   @param[out]  LinuxCommandLine  A pointer to the buffer where the Linux
    148                                  kernel command line is stored. The address
    149                                  of the buffer is NULL in case of an error
    150                                  or if the Linux command line is not
    151                                  defined. Otherwise, the returned address
    152                                  is the address of a buffer allocated with
    153                                  a call to AllocatePool() that has to be
    154                                  freed by the caller.
    155   @param[out]  AtagMachineType   Value of the ARM Machine Type
    156 
    157   @retval  EFI_SUCCESS            The processing was successfull.
    158   @retval  EFI_ABORTED            The initialisation of the Shell Library failed.
    159   @retval  EFI_NOT_FOUND          Path to the Linux kernel not found.
    160   @retval  EFI_INVALID_PARAMETER  At least one parameter is not valid or there is a
    161                                   conflict between two parameters.
    162   @retval  EFI_OUT_OF_RESOURCES   A memory allocation failed.
    163 
    164 **/
    165 EFI_STATUS
    166 ProcessShellParameters (
    167   OUT  CHAR16   **KernelPath,
    168   OUT  CHAR16   **FdtPath,
    169   OUT  CHAR16   **InitrdPath,
    170   OUT  CHAR16   **LinuxCommandLine,
    171   OUT  UINTN    *AtagMachineType
    172   )
    173 {
    174   EFI_STATUS    Status;
    175   LIST_ENTRY    *CheckPackage;
    176   CHAR16        *ProblemParam;
    177   CONST CHAR16  *FlagValue;
    178   CONST CHAR16  *ParameterValue;
    179   UINTN         LinuxCommandLineLen;
    180 
    181 
    182   *KernelPath       = NULL;
    183   *FdtPath          = NULL;
    184   *InitrdPath       = NULL;
    185   *LinuxCommandLine = NULL;
    186   *AtagMachineType  = ARM_FDT_MACHINE_TYPE;
    187 
    188   //
    189   // Initialise the Shell Library as we are going to use it.
    190   // Assert that the return code is EFI_SUCCESS as it should.
    191   // To anticipate any change is the codes returned by
    192   // ShellInitialize(), leave in case of error.
    193   //
    194   Status = ShellInitialize ();
    195   if (EFI_ERROR (Status)) {
    196     ASSERT_EFI_ERROR (Status);
    197     return EFI_ABORTED;
    198   }
    199 
    200   Status = ShellCommandLineParse (ParamList, &CheckPackage, &ProblemParam, TRUE);
    201   if (EFI_ERROR (Status)) {
    202     if ((Status == EFI_VOLUME_CORRUPTED) &&
    203         (ProblemParam != NULL) ) {
    204       PrintHii (
    205         NULL, STRING_TOKEN (STR_SHELL_INVALID_PARAMETER), ProblemParam
    206         );
    207       FreePool (ProblemParam);
    208     } else {
    209       ASSERT (FALSE);
    210     }
    211     goto Error;
    212   }
    213 
    214   Status = EFI_INVALID_PARAMETER;
    215   if (ShellCommandLineGetCount (CheckPackage) != 2) {
    216     PrintHelp (NULL);
    217     goto Error;
    218   }
    219 
    220   Status = EFI_OUT_OF_RESOURCES;
    221 
    222   FlagValue = ShellCommandLineGetValue (CheckPackage, L"-a");
    223   if (FlagValue != NULL) {
    224     if (ShellCommandLineGetFlag (CheckPackage, L"-d")) {
    225       PrintHii (NULL, STRING_TOKEN (STR_ATAG_FDT_CONFLICT));
    226       goto Error;
    227     }
    228     *AtagMachineType = StrDecimalToUintn (FlagValue);
    229   }
    230 
    231   ParameterValue = ShellCommandLineGetRawValue (CheckPackage, 1);
    232   *KernelPath = AllocateCopyPool (StrSize (ParameterValue), ParameterValue);
    233   if (*KernelPath == NULL) {
    234     goto Error;
    235   }
    236 
    237   FlagValue = ShellCommandLineGetValue (CheckPackage, L"-d");
    238   if (FlagValue != NULL) {
    239     *FdtPath = AllocateCopyPool (StrSize (FlagValue), FlagValue);
    240     if (*FdtPath == NULL) {
    241       goto Error;
    242     }
    243   }
    244 
    245   FlagValue = ShellCommandLineGetValue (CheckPackage, L"-f");
    246   if (FlagValue != NULL) {
    247     *InitrdPath = AllocateCopyPool (StrSize (FlagValue), FlagValue);
    248     if (*InitrdPath == NULL) {
    249       goto Error;
    250     }
    251   }
    252 
    253   FlagValue = ShellCommandLineGetValue (CheckPackage, L"-c");
    254   if (FlagValue != NULL) {
    255     LinuxCommandLineLen = StrLen (FlagValue);
    256     if ((LinuxCommandLineLen != 0) &&
    257         (FlagValue[0] == L'"'    ) &&
    258         (FlagValue[LinuxCommandLineLen - 1] == L'"')) {
    259       FlagValue++;
    260       LinuxCommandLineLen -= 2;
    261     }
    262 
    263     *LinuxCommandLine = AllocateCopyPool (
    264                           (LinuxCommandLineLen + 1) * sizeof (CHAR16),
    265                           FlagValue
    266                           );
    267     if (*LinuxCommandLine == NULL) {
    268       goto Error;
    269     }
    270     (*LinuxCommandLine)[LinuxCommandLineLen] = L'\0';
    271   }
    272 
    273   Status = EFI_SUCCESS;
    274 
    275 Error:
    276   ShellCommandLineFreeVarList (CheckPackage);
    277 
    278   if (EFI_ERROR (Status)) {
    279     if (*KernelPath != NULL) {
    280       FreePool (*KernelPath);
    281       *KernelPath = NULL;
    282     }
    283     if (*FdtPath != NULL) {
    284       FreePool (*FdtPath);
    285       *FdtPath = NULL;
    286     }
    287     if (*InitrdPath != NULL) {
    288       FreePool (*InitrdPath);
    289       *InitrdPath = NULL;
    290     }
    291     if (*LinuxCommandLine != NULL) {
    292       FreePool (*LinuxCommandLine);
    293       *LinuxCommandLine = NULL;
    294     }
    295   }
    296 
    297   return Status;
    298 }
    299