]> git.proxmox.com Git - mirror_edk2.git/blobdiff - ShellPkg/Application/Shell/FileHandleWrappers.c
ShellPkg: Standardized HP Copyright Message String
[mirror_edk2.git] / ShellPkg / Application / Shell / FileHandleWrappers.c
index 38fdb8d848957b9067bbef535221b5c383b2a5dd..984cd36957af6107b4209f231e8d521a696f5f4e 100644 (file)
@@ -2,7 +2,8 @@
   EFI_FILE_PROTOCOL wrappers for other items (Like Environment Variables,\r
   StdIn, StdOut, StdErr, etc...).\r
 \r
-  Copyright (c) 2009 - 2011, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>\r
+  (C) Copyright 2013 Hewlett-Packard Development Company, L.P.<BR>\r
   This program and the accompanying materials\r
   are licensed and made available under the terms and conditions of the BSD License\r
   which accompanies this distribution.  The full text of the license may be found at\r
@@ -95,10 +96,10 @@ FileInterfaceNopSetPosition(
 /**\r
   File style interface for console (GetInfo).\r
   \r
-  @param[in] This             Ignored.\r
-  @param[in] InformationType  Ignored.\r
-  @param[in,out] BufferSize   Ignored.\r
-  @param[out] Buffer          Ignored.\r
+  @param[in] This              Ignored.\r
+  @param[in] InformationType   Ignored.\r
+  @param[in, out] BufferSize   Ignored.\r
+  @param[out] Buffer           Ignored.\r
   \r
   @retval EFI_UNSUPPORTED\r
 **/\r
@@ -141,9 +142,9 @@ FileInterfaceNopSetInfo(
 \r
   Writes data to the screen.\r
   \r
-  @param[in] This             The pointer to the EFI_FILE_PROTOCOL object.\r
-  @param[in,out] BufferSize   Size in bytes of Buffer.\r
-  @param[in] Buffer           The pointer to the buffer to write.\r
+  @param[in] This              The pointer to the EFI_FILE_PROTOCOL object.\r
+  @param[in, out] BufferSize   Size in bytes of Buffer.\r
+  @param[in] Buffer            The pointer to the buffer to write.\r
   \r
   @retval EFI_UNSUPPORTED No output console is supported.\r
   @return A return value from gST->ConOut->OutputString.\r
@@ -166,9 +167,9 @@ FileInterfaceStdOutWrite(
 /**\r
   File style interface for StdIn (Write).\r
   \r
-  @param[in] This           Ignored.\r
-  @param[in,out] BufferSize Ignored.\r
-  @param[in] Buffer         Ignored.\r
+  @param[in] This            Ignored.\r
+  @param[in, out] BufferSize Ignored.\r
+  @param[in] Buffer          Ignored.\r
   \r
   @retval EFI_UNSUPPORTED\r
 **/\r
@@ -188,9 +189,9 @@ FileInterfaceStdInWrite(
 \r
   Writes error to the error output.\r
   \r
-  @param[in] This             The pointer to the EFI_FILE_PROTOCOL object.\r
-  @param[in,out] BufferSize   Size in bytes of Buffer.\r
-  @param[in] Buffer           The pointer to the buffer to write.\r
+  @param[in] This              The pointer to the EFI_FILE_PROTOCOL object.\r
+  @param[in, out] BufferSize   Size in bytes of Buffer.\r
+  @param[in] Buffer            The pointer to the buffer to write.\r
   \r
   @return A return value from gST->StdErr->OutputString.\r
 **/\r
@@ -208,9 +209,9 @@ FileInterfaceStdErrWrite(
 /**\r
   File style interface for console StdOut (Read).\r
   \r
-  @param[in] This             Ignored.\r
-  @param[in,out] BufferSize   Ignored.\r
-  @param[out] Buffer          Ignored.\r
+  @param[in] This              Ignored.\r
+  @param[in, out] BufferSize   Ignored.\r
+  @param[out] Buffer           Ignored.\r
   \r
   @retval EFI_UNSUPPORTED\r
 **/\r
@@ -228,9 +229,9 @@ FileInterfaceStdOutRead(
 /**\r
   File style interface for console StdErr (Read).\r
   \r
-  @param[in] This             Ignored.\r
-  @param[in,out] BufferSize   Ignored.\r
-  @param[out] Buffer          Ignored.\r
+  @param[in] This              Ignored.\r
+  @param[in, out] BufferSize   Ignored.\r
+  @param[out] Buffer           Ignored.\r
   \r
   @retval EFI_UNSUPPORTED Always.\r
 **/\r
@@ -248,9 +249,9 @@ FileInterfaceStdErrRead(
 /**\r
   File style interface for NUL file (Read).\r
   \r
-  @param[in] This             Ignored.\r
-  @param[in,out] BufferSize   Ignored.\r
-  @param[out] Buffer          Ignored.\r
+  @param[in] This              Ignored.\r
+  @param[in, out] BufferSize   Poiner to 0 upon return.\r
+  @param[out] Buffer           Ignored.\r
   \r
   @retval EFI_SUCCESS Always.\r
 **/\r
@@ -262,15 +263,16 @@ FileInterfaceNulRead(
   OUT     VOID              *Buffer\r
   )\r
 {\r
+  *BufferSize = 0;\r
   return (EFI_SUCCESS);\r
 }\r
 \r
 /**\r
   File style interface for NUL file (Write).\r
   \r
-  @param[in] This             Ignored.\r
-  @param[in,out] BufferSize   Ignored.\r
-  @param[in] Buffer           Ignored.\r
+  @param[in] This              Ignored.\r
+  @param[in, out] BufferSize   Ignored.\r
+  @param[in] Buffer            Ignored.\r
   \r
   @retval EFI_SUCCESS\r
 **/\r
@@ -415,7 +417,7 @@ FileInterfaceStdInRead(
     gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);\r
     Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);\r
     if (EFI_ERROR (Status)) {\r
-      continue;\r
+      break;\r
     }\r
 \r
     //\r
@@ -507,21 +509,20 @@ FileInterfaceStdInRead(
         if (StrStr(CurrentString + TabPos, L":") == NULL) {\r
           Cwd = ShellInfoObject.NewEfiShellProtocol->GetCurDir(NULL);\r
           if (Cwd != NULL) {\r
-            StrCpy(TabStr, Cwd);\r
+            StrnCpy(TabStr, Cwd, (*BufferSize)/sizeof(CHAR16) - 1);\r
             if (TabStr[StrLen(TabStr)-1] == L'\\' && *(CurrentString + TabPos) == L'\\' ) {\r
               TabStr[StrLen(TabStr)-1] = CHAR_NULL;\r
             }\r
             StrnCat(TabStr, CurrentString + TabPos, (StringLen - TabPos) * sizeof (CHAR16));\r
           } else {\r
-            StrCpy(TabStr, L"");\r
+            *TabStr = CHAR_NULL;\r
             StrnCat(TabStr, CurrentString + TabPos, (StringLen - TabPos) * sizeof (CHAR16));\r
           }\r
         } else {\r
-          StrCpy(TabStr, CurrentString + TabPos);\r
+          StrnCpy(TabStr, CurrentString + TabPos, (*BufferSize)/sizeof(CHAR16) - 1);\r
         }\r
-        StrCat(TabStr, L"*");\r
+        StrnCat(TabStr, L"*", (*BufferSize)/sizeof(CHAR16) - 1 - StrLen(TabStr));\r
         FoundFileList = NULL;\r
-//        TabStr = CleanPath(TabStr);\r
         Status  = ShellInfoObject.NewEfiShellProtocol->FindFiles(TabStr, &FoundFileList);\r
         for ( TempStr = CurrentString\r
             ; *TempStr == L' '\r
@@ -847,7 +848,7 @@ FileInterfaceStdInRead(
   //\r
   ASSERT(FoundFileList == NULL);\r
 \r
-  return EFI_SUCCESS;\r
+  return Status;\r
 }\r
 \r
 //\r
@@ -949,8 +950,48 @@ FileInterfaceEnvClose(
   IN EFI_FILE_PROTOCOL *This\r
   )\r
 {\r
+  VOID*       NewBuffer;\r
+  UINTN       NewSize;\r
+  EFI_STATUS  Status;\r
+\r
+  //\r
+  // Most if not all UEFI commands will have an '\r\n' at the end of any output. \r
+  // Since the output was redirected to a variable, it does not make sense to \r
+  // keep this.  So, before closing, strip the trailing '\r\n' from the variable\r
+  // if it exists.\r
+  //\r
+  NewBuffer   = NULL;\r
+  NewSize     = 0;\r
+\r
+  Status = SHELL_GET_ENVIRONMENT_VARIABLE(((EFI_FILE_PROTOCOL_ENVIRONMENT*)This)->Name, &NewSize, NewBuffer);\r
+  if (Status == EFI_BUFFER_TOO_SMALL) {\r
+    NewBuffer = AllocateZeroPool(NewSize + sizeof(CHAR16));\r
+    if (NewBuffer == NULL) {\r
+      return EFI_OUT_OF_RESOURCES;\r
+    }  \r
+    Status = SHELL_GET_ENVIRONMENT_VARIABLE(((EFI_FILE_PROTOCOL_ENVIRONMENT*)This)->Name, &NewSize, NewBuffer);\r
+  }\r
+  \r
+  if (!EFI_ERROR(Status) && NewBuffer != NULL) {\r
+    \r
+    if (StrSize(NewBuffer) > 6)\r
+    {\r
+      if ((((CHAR16*)NewBuffer)[(StrSize(NewBuffer)/2) - 2] == CHAR_LINEFEED) \r
+           && (((CHAR16*)NewBuffer)[(StrSize(NewBuffer)/2) - 3] == CHAR_CARRIAGE_RETURN)) {\r
+        ((CHAR16*)NewBuffer)[(StrSize(NewBuffer)/2) - 3] = CHAR_NULL;   \r
+      }\r
+    \r
+      if (IsVolatileEnv(((EFI_FILE_PROTOCOL_ENVIRONMENT*)This)->Name)) {\r
+        Status = SHELL_SET_ENVIRONMENT_VARIABLE_V(((EFI_FILE_PROTOCOL_ENVIRONMENT*)This)->Name, StrSize(NewBuffer), NewBuffer);\r
+      } else {\r
+        Status = SHELL_SET_ENVIRONMENT_VARIABLE_NV(((EFI_FILE_PROTOCOL_ENVIRONMENT*)This)->Name, StrSize(NewBuffer), NewBuffer);\r
+      }\r
+    }\r
+  } \r
+  \r
+  SHELL_FREE_NON_NULL(NewBuffer);\r
   FreePool((EFI_FILE_PROTOCOL_ENVIRONMENT*)This);\r
-  return (EFI_SUCCESS);\r
+  return (Status);\r
 }\r
 \r
 /**\r
@@ -973,9 +1014,9 @@ FileInterfaceEnvDelete(
 /**\r
   File style interface for Environment Variable (Read).\r
   \r
-  @param[in] This             The pointer to the EFI_FILE_PROTOCOL object.\r
-  @param[in,out] BufferSize   Size in bytes of Buffer.\r
-  @param[out] Buffer          The pointer to the buffer to fill.\r
+  @param[in] This              The pointer to the EFI_FILE_PROTOCOL object.\r
+  @param[in, out] BufferSize   Size in bytes of Buffer.\r
+  @param[out] Buffer           The pointer to the buffer to fill.\r
   \r
   @retval EFI_SUCCESS   The data was read.\r
 **/\r
@@ -996,9 +1037,9 @@ FileInterfaceEnvRead(
 /**\r
   File style interface for Volatile Environment Variable (Write).\r
   \r
-  @param[in] This             The pointer to the EFI_FILE_PROTOCOL object.\r
-  @param[in,out] BufferSize   Size in bytes of Buffer.\r
-  @param[in] Buffer           The pointer to the buffer to write.\r
+  @param[in] This              The pointer to the EFI_FILE_PROTOCOL object.\r
+  @param[in, out] BufferSize   Size in bytes of Buffer.\r
+  @param[in] Buffer            The pointer to the buffer to write.\r
   \r
   @retval EFI_SUCCESS   The data was read.\r
 **/\r
@@ -1043,9 +1084,9 @@ FileInterfaceEnvVolWrite(
 /**\r
   File style interface for Non Volatile Environment Variable (Write).\r
   \r
-  @param[in] This             The pointer to the EFI_FILE_PROTOCOL object.\r
-  @param[in,out] BufferSize   Size in bytes of Buffer.\r
-  @param[in] Buffer           The pointer to the buffer to write.\r
+  @param[in] This              The pointer to the EFI_FILE_PROTOCOL object.\r
+  @param[in, out] BufferSize   Size in bytes of Buffer.\r
+  @param[in] Buffer            The pointer to the buffer to write.\r
   \r
   @retval EFI_SUCCESS   The data was read.\r
 **/\r
@@ -1099,6 +1140,7 @@ CreateFileInterfaceEnv(
   )\r
 {\r
   EFI_FILE_PROTOCOL_ENVIRONMENT  *EnvFileInterface;\r
+  UINTN                          EnvNameSize;\r
 \r
   if (EnvName == NULL) {\r
     return (NULL);\r
@@ -1107,7 +1149,8 @@ CreateFileInterfaceEnv(
   //\r
   // Get some memory\r
   //\r
-  EnvFileInterface = AllocateZeroPool(sizeof(EFI_FILE_PROTOCOL_ENVIRONMENT)+StrSize(EnvName));\r
+  EnvNameSize = StrSize(EnvName);\r
+  EnvFileInterface = AllocateZeroPool(sizeof(EFI_FILE_PROTOCOL_ENVIRONMENT)+EnvNameSize);\r
   if (EnvFileInterface == NULL){\r
     return (NULL);\r
   }\r
@@ -1125,8 +1168,8 @@ CreateFileInterfaceEnv(
   EnvFileInterface->Flush       = FileInterfaceNopGeneric;\r
   EnvFileInterface->Delete      = FileInterfaceEnvDelete;\r
   EnvFileInterface->Read        = FileInterfaceEnvRead;\r
-\r
-  StrCpy(EnvFileInterface->Name, EnvName);\r
+  \r
+  CopyMem(EnvFileInterface->Name, EnvName, EnvNameSize);\r
 \r
   //\r
   // Assign the different members for Volatile and Non-Volatile variables\r
@@ -1143,8 +1186,8 @@ CreateFileInterfaceEnv(
   Move the cursor position one character backward.\r
 \r
   @param[in] LineLength       Length of a line. Get it by calling QueryMode\r
-  @param[in,out] Column      Current column of the cursor position\r
-  @param[in,out] Row         Current row of the cursor position\r
+  @param[in, out] Column      Current column of the cursor position\r
+  @param[in, out] Row         Current row of the cursor position\r
 **/\r
 VOID\r
 EFIAPI\r
@@ -1173,8 +1216,8 @@ MoveCursorBackward (
 \r
   @param[in] LineLength       Length of a line.\r
   @param[in] TotalRow         Total row of a screen\r
-  @param[in,out] Column      Current column of the cursor position\r
-  @param[in,out] Row         Current row of the cursor position\r
+  @param[in, out] Column      Current column of the cursor position\r
+  @param[in, out] Row         Current row of the cursor position\r
 **/\r
 VOID\r
 EFIAPI\r
@@ -1322,11 +1365,12 @@ FileInterfaceMemGetPosition(
 /**\r
   File style interface for Mem (Write).\r
   \r
-  @param[in] This             The pointer to the EFI_FILE_PROTOCOL object.\r
-  @param[in,out] BufferSize   Size in bytes of Buffer.\r
-  @param[in] Buffer           The pointer to the buffer to write.\r
+  @param[in] This              The pointer to the EFI_FILE_PROTOCOL object.\r
+  @param[in, out] BufferSize   Size in bytes of Buffer.\r
+  @param[in] Buffer            The pointer to the buffer to write.\r
   \r
-  @retval EFI_SUCCESS   The data was written.\r
+  @retval EFI_OUT_OF_RESOURCES The operation failed due to lack of resources.\r
+  @retval EFI_SUCCESS          The data was written.\r
 **/\r
 EFI_STATUS\r
 EFIAPI\r
@@ -1353,6 +1397,9 @@ FileInterfaceMemWrite(
     // Ascii\r
     //\r
     AsciiBuffer = AllocateZeroPool(*BufferSize);\r
+    if (AsciiBuffer == NULL) {\r
+      return (EFI_OUT_OF_RESOURCES);\r
+    }\r
     AsciiSPrint(AsciiBuffer, *BufferSize, "%S", Buffer);\r
     if ((UINTN)(((EFI_FILE_PROTOCOL_MEM*)This)->Position + AsciiStrSize(AsciiBuffer)) > (UINTN)(((EFI_FILE_PROTOCOL_MEM*)This)->BufferSize)) {\r
       ((EFI_FILE_PROTOCOL_MEM*)This)->Buffer = ReallocatePool((UINTN)(((EFI_FILE_PROTOCOL_MEM*)This)->BufferSize), (UINTN)(((EFI_FILE_PROTOCOL_MEM*)This)->BufferSize) + AsciiStrSize(AsciiBuffer) + 10, ((EFI_FILE_PROTOCOL_MEM*)This)->Buffer);\r
@@ -1368,9 +1415,9 @@ FileInterfaceMemWrite(
 /**\r
   File style interface for Mem (Read).\r
   \r
-  @param[in] This             The pointer to the EFI_FILE_PROTOCOL object.\r
-  @param[in,out] BufferSize   Size in bytes of Buffer.\r
-  @param[in] Buffer           The pointer to the buffer to fill.\r
+  @param[in] This              The pointer to the EFI_FILE_PROTOCOL object.\r
+  @param[in, out] BufferSize   Size in bytes of Buffer.\r
+  @param[in] Buffer            The pointer to the buffer to fill.\r
   \r
   @retval EFI_SUCCESS   The data was read.\r
 **/\r
@@ -1547,7 +1594,7 @@ FileInterfaceFileGetInfo(
 /**\r
   Set information about a file\r
 \r
-  @param  File            Protocol instance pointer.\r
+  @param  This            Protocol instance pointer.\r
   @param  InformationType Type of information in Buffer.\r
   @param  BufferSize      Size of buffer.\r
   @param  Buffer          The data to write.\r
@@ -1717,9 +1764,9 @@ FileInterfaceFileClose(
   If the file was opened with ASCII mode the data will be processed through \r
   AsciiSPrint before writing.\r
   \r
-  @param[in] This             The pointer to the EFI_FILE_PROTOCOL object.\r
-  @param[in,out] BufferSize   Size in bytes of Buffer.\r
-  @param[in] Buffer           The pointer to the buffer to write.\r
+  @param[in] This              The pointer to the EFI_FILE_PROTOCOL object.\r
+  @param[in, out] BufferSize   Size in bytes of Buffer.\r
+  @param[in] Buffer            The pointer to the buffer to write.\r
   \r
   @retval EFI_SUCCESS   The data was written.\r
 **/\r