]> git.proxmox.com Git - mirror_edk2.git/blobdiff - ShellPkg/Application/Shell/ShellParametersProtocol.c
ShellPkg: Add checking for memory allocation and pointer returns from functions.
[mirror_edk2.git] / ShellPkg / Application / Shell / ShellParametersProtocol.c
index b90a2c32bbce2d076b713e0375a5db2f35b3a1f6..55cf3c279eb74401d6d62ed64b3095094f80a42d 100644 (file)
@@ -26,9 +26,9 @@
   Temp Parameter must be large enough to hold the parameter before calling this\r
   function.\r
 \r
-  @param[in,out] Walker        pointer to string of command line.  Adjusted to\r
+  @param[in, out] Walker        pointer to string of command line.  Adjusted to\r
                                 reminaing command line on return\r
-  @param[in,out] TempParameter pointer to string of command line item extracted.\r
+  @param[in, out] TempParameter pointer to string of command line item extracted.\r
 \r
 **/\r
 VOID\r
@@ -69,9 +69,7 @@ GetNextParameter(
   if ((*Walker)[0] == L'\"') {\r
     NextDelim = NULL;\r
     for (TempLoc = *Walker + 1 ; TempLoc != NULL && *TempLoc != CHAR_NULL ; TempLoc++) {\r
-      if (*TempLoc == L'^' && *(TempLoc+1) == L'^') {\r
-        TempLoc++;\r
-      } else if (*TempLoc == L'^' && *(TempLoc+1) == L'\"') {\r
+      if (*TempLoc == L'^' && *(TempLoc+1) == L'\"') {\r
         TempLoc++;\r
       } else if (*TempLoc == L'\"') {\r
         NextDelim = TempLoc;\r
@@ -96,10 +94,7 @@ GetNextParameter(
       *Walker = NULL;\r
     }\r
     for (TempLoc = *TempParameter ; TempLoc != NULL && *TempLoc != CHAR_NULL ; TempLoc++) {\r
-      if ((*TempLoc == L'^' && *(TempLoc+1) == L'^')\r
-       || (*TempLoc == L'^' && *(TempLoc+1) == L'|')\r
-       || (*TempLoc == L'^' && *(TempLoc+1) == L'\"')\r
-      ){\r
+      if (*TempLoc == L'^' && *(TempLoc+1) == L'\"') {\r
         CopyMem(TempLoc, TempLoc+1, StrSize(TempLoc) - sizeof(TempLoc[0]));\r
       }\r
     }\r
@@ -123,9 +118,7 @@ GetNextParameter(
     for (NextDelim = *TempParameter ; NextDelim != NULL && *NextDelim != CHAR_NULL ; NextDelim++) {\r
       if (*NextDelim == L'^' && *(NextDelim+1) == L'^') {\r
         CopyMem(NextDelim, NextDelim+1, StrSize(NextDelim) - sizeof(NextDelim[0]));\r
-      }/* else if (*NextDelim == L'^') {\r
-        *NextDelim = L' ';\r
-      }*/\r
+      }\r
     }\r
     while ((*TempParameter)[StrLen(*TempParameter)-1] == L' ') {\r
       (*TempParameter)[StrLen(*TempParameter)-1] = CHAR_NULL;\r
@@ -144,9 +137,9 @@ GetNextParameter(
   parameters for inclusion in EFI_SHELL_PARAMETERS_PROTOCOL.  this supports space\r
   delimited and quote surrounded parameter definition.\r
 \r
-  @param[in] CommandLine        String of command line to parse\r
-  @param[in,out] Argv           pointer to array of strings; one for each parameter\r
-  @param[in,out] Argc           pointer to number of strings in Argv array\r
+  @param[in] CommandLine         String of command line to parse\r
+  @param[in, out] Argv           pointer to array of strings; one for each parameter\r
+  @param[in, out] Argc           pointer to number of strings in Argv array\r
 \r
   @return EFI_SUCCESS           the operation was sucessful\r
   @return EFI_OUT_OF_RESOURCES  a memory allocation failed.\r
@@ -222,9 +215,9 @@ ParseCommandLineToArgs(
   installs it on our handle and if there is an existing version of the protocol\r
   that one is cached for removal later.\r
 \r
-  @param[in,out] NewShellParameters on a successful return, a pointer to pointer\r
+  @param[in, out] NewShellParameters on a successful return, a pointer to pointer\r
                                      to the newly installed interface.\r
-  @param[in,out] RootShellInstance  on a successful return, pointer to boolean.\r
+  @param[in, out] RootShellInstance  on a successful return, pointer to boolean.\r
                                      TRUE if this is the root shell instance.\r
 \r
   @retval EFI_SUCCESS               the operation completed successfully.\r
@@ -456,7 +449,7 @@ IsUnicodeFile(
 \r
   All of the characters between quotes is replaced with spaces.\r
 \r
-  @param[in,out] TheString  A pointer to the string to update.\r
+  @param[in, out] TheString  A pointer to the string to update.\r
 **/\r
 VOID\r
 EFIAPI\r
@@ -477,6 +470,31 @@ StripQuotes (
   }\r
 }\r
 \r
+/**\r
+  Calcualte the 32-bit CRC in a EFI table using the service provided by the\r
+  gRuntime service.\r
+\r
+  @param  Hdr                    Pointer to an EFI standard header\r
+\r
+**/\r
+VOID\r
+CalculateEfiHdrCrc (\r
+  IN  OUT EFI_TABLE_HEADER    *Hdr\r
+  )\r
+{\r
+  UINT32 Crc;\r
+\r
+  Hdr->CRC32 = 0;\r
+\r
+  //\r
+  // If gBS->CalculateCrce32 () == CoreEfiNotAvailableYet () then\r
+  //  Crc will come back as zero if we set it to zero here\r
+  //\r
+  Crc = 0;\r
+  gBS->CalculateCrc32 ((UINT8 *)Hdr, Hdr->HeaderSize, &Crc);\r
+  Hdr->CRC32 = Crc;\r
+}\r
+\r
 /**\r
   Funcion will replace the current StdIn and StdOut in the ShellParameters protocol\r
   structure by parsing NewCommandLine.  The current values are returned to the\r
@@ -484,12 +502,12 @@ StripQuotes (
 \r
   This will also update the system table.\r
 \r
-  @param[in,out] ShellParameters        Pointer to parameter structure to modify.\r
-  @param[in] NewCommandLine             The new command line to parse and use.\r
-  @param[out] OldStdIn                  Pointer to old StdIn.\r
-  @param[out] OldStdOut                 Pointer to old StdOut.\r
-  @param[out] OldStdErr                 Pointer to old StdErr.\r
-  @param[out] SystemTableInfo           Pointer to old system table information.\r
+  @param[in, out] ShellParameters        Pointer to parameter structure to modify.\r
+  @param[in] NewCommandLine              The new command line to parse and use.\r
+  @param[out] OldStdIn                   Pointer to old StdIn.\r
+  @param[out] OldStdOut                  Pointer to old StdOut.\r
+  @param[out] OldStdErr                  Pointer to old StdErr.\r
+  @param[out] SystemTableInfo            Pointer to old system table information.\r
 \r
   @retval   EFI_SUCCESS                 Operation was sucessful, Argv and Argc are valid.\r
   @retval   EFI_OUT_OF_RESOURCES        A memory allocation failed.\r
@@ -559,6 +577,9 @@ UpdateStdInStdOutStdErr(
   }\r
 \r
   CommandLineCopy = StrnCatGrow(&CommandLineCopy, NULL, NewCommandLine, 0);\r
+  if (CommandLineCopy == NULL) {\r
+    return (EFI_OUT_OF_RESOURCES);\r
+  }\r
   Status          = EFI_SUCCESS;\r
   Split           = NULL;\r
   FirstLocation   = CommandLineCopy + StrLen(CommandLineCopy);\r
@@ -893,6 +914,9 @@ UpdateStdInStdOutStdErr(
       ||(StdErrFileName != NULL && !ErrUnicode && ErrAppend && (!EFI_ERROR(ShellFileExists(StdErrFileName)) && !EFI_ERROR(IsUnicodeFile(StdErrFileName))))\r
       ){\r
       Status = EFI_INVALID_PARAMETER;\r
+      ShellParameters->StdIn  = *OldStdIn;\r
+      ShellParameters->StdOut = *OldStdOut;\r
+      ShellParameters->StdErr = *OldStdErr;\r
     } else if (!EFI_ERROR(Status)){\r
       //\r
       // Open the Std<Whatever> and we should not have conflicts here...\r
@@ -1009,16 +1033,19 @@ UpdateStdInStdOutStdErr(
       //\r
       if (!EFI_ERROR(Status) && StdInVarName != NULL) {\r
         TempHandle = CreateFileInterfaceEnv(StdInVarName);\r
-        if (!InUnicode) {\r
-          TempHandle = CreateFileInterfaceFile(TempHandle, FALSE);\r
-        }\r
-        Size = 0;\r
-        ASSERT(TempHandle != NULL);\r
-        if (((EFI_FILE_PROTOCOL*)TempHandle)->Read(TempHandle, &Size, NULL) != EFI_BUFFER_TOO_SMALL) {\r
-          Status = EFI_INVALID_PARAMETER;\r
+        if (TempHandle == NULL) {\r
+          Status = EFI_OUT_OF_RESOURCES;\r
         } else {\r
-          ShellParameters->StdIn = TempHandle;\r
-          gST->ConIn = CreateSimpleTextInOnFile(TempHandle, &gST->ConsoleInHandle);\r
+          if (!InUnicode) {\r
+            TempHandle = CreateFileInterfaceFile(TempHandle, FALSE);\r
+          }\r
+          Size = 0;\r
+          if (((EFI_FILE_PROTOCOL*)TempHandle)->Read(TempHandle, &Size, NULL) != EFI_BUFFER_TOO_SMALL) {\r
+            Status = EFI_INVALID_PARAMETER;\r
+          } else {\r
+            ShellParameters->StdIn = TempHandle;\r
+            gST->ConIn = CreateSimpleTextInOnFile(TempHandle, &gST->ConsoleInHandle);\r
+          }\r
         }\r
       }\r
 \r
@@ -1043,6 +1070,8 @@ UpdateStdInStdOutStdErr(
   }\r
   FreePool(CommandLineCopy);\r
 \r
+  CalculateEfiHdrCrc(&gST->Hdr);\r
+\r
   if (gST->ConIn == NULL ||gST->ConOut == NULL) {\r
     return (EFI_OUT_OF_RESOURCES);\r
   }\r
@@ -1053,11 +1082,11 @@ UpdateStdInStdOutStdErr(
   Funcion will replace the current StdIn and StdOut in the ShellParameters protocol\r
   structure with StdIn and StdOut.  The current values are de-allocated.\r
 \r
-  @param[in,out] ShellParameters      Pointer to parameter structure to modify.\r
-  @param[in] OldStdIn                 Pointer to old StdIn.\r
-  @param[in] OldStdOut                Pointer to old StdOut.\r
-  @param[in] OldStdErr                Pointer to old StdErr.\r
-  @param[in] SystemTableInfo          Pointer to old system table information.\r
+  @param[in, out] ShellParameters      Pointer to parameter structure to modify.\r
+  @param[in] OldStdIn                  Pointer to old StdIn.\r
+  @param[in] OldStdOut                 Pointer to old StdOut.\r
+  @param[in] OldStdErr                 Pointer to old StdErr.\r
+  @param[in] SystemTableInfo           Pointer to old system table information.\r
 **/\r
 EFI_STATUS\r
 EFIAPI\r
@@ -1116,6 +1145,8 @@ RestoreStdInStdOutStdErr (
     gST->StandardErrorHandle  = SystemTableInfo->ConErrHandle;\r
   }\r
 \r
+  CalculateEfiHdrCrc(&gST->Hdr);\r
+\r
   return (EFI_SUCCESS);\r
 }\r
 /**\r
@@ -1125,10 +1156,10 @@ RestoreStdInStdOutStdErr (
 \r
   If OldArgv or OldArgc is NULL then that value is not returned.\r
 \r
-  @param[in,out] ShellParameters        Pointer to parameter structure to modify.\r
-  @param[in] NewCommandLine             The new command line to parse and use.\r
-  @param[out] OldArgv                   Pointer to old list of parameters.\r
-  @param[out] OldArgc                   Pointer to old number of items in Argv list.\r
+  @param[in, out] ShellParameters        Pointer to parameter structure to modify.\r
+  @param[in] NewCommandLine              The new command line to parse and use.\r
+  @param[out] OldArgv                    Pointer to old list of parameters.\r
+  @param[out] OldArgc                    Pointer to old number of items in Argv list.\r
 \r
   @retval   EFI_SUCCESS                 Operation was sucessful, Argv and Argc are valid.\r
   @retval   EFI_OUT_OF_RESOURCES        A memory allocation failed.\r
@@ -1159,9 +1190,9 @@ UpdateArgcArgv(
   structure with Argv and Argc.  The current values are de-allocated and the\r
   OldArgv must not be deallocated by the caller.\r
 \r
-  @param[in,out] ShellParameters       pointer to parameter structure to modify\r
-  @param[in] OldArgv                   pointer to old list of parameters\r
-  @param[in] OldArgc                   pointer to old number of items in Argv list\r
+  @param[in, out] ShellParameters       pointer to parameter structure to modify\r
+  @param[in] OldArgv                    pointer to old list of parameters\r
+  @param[in] OldArgc                    pointer to old number of items in Argv list\r
 **/\r
 VOID\r
 EFIAPI\r