]> git.proxmox.com Git - mirror_edk2.git/blobdiff - ShellPkg/Library/UefiShellDebug1CommandsLib/DmpStore.c
Add the comment for function 'IsValidSplit' in 'Shell.c'. Add code to check whether...
[mirror_edk2.git] / ShellPkg / Library / UefiShellDebug1CommandsLib / DmpStore.c
index 0a586591beeb2a2a53a5f8d1bb7aa27ba584b00e..afeedb09591ec95aef652b954a8476ad9d4cce66 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Main file for DmpStore shell Debug1 function.\r
 \r
-  Copyright (c) 2005 - 2010, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2005 - 2014, Intel Corporation. All rights reserved.<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
 \r
 #include "UefiShellDebug1CommandsLib.h"\r
 \r
-STATIC CHAR16   *AttrType[] = {\r
-  L"invalid",   // 000\r
-  L"invalid",   // 001\r
-  L"BS",        // 010\r
-  L"NV+BS",     // 011\r
-  L"RT+BS",     // 100\r
-  L"NV+RT+BS",  // 101\r
-  L"RT+BS",     // 110\r
-  L"NV+RT+BS",  // 111\r
-};\r
+/**\r
+  Base on the input attribute value to return the attribute string.\r
 \r
+  @param[in]     Atts           The input attribute value\r
+\r
+  @retval The attribute string info.\r
+**/\r
+CHAR16 *\r
+EFIAPI\r
+GetAttrType (\r
+  IN CONST UINT32 Atts\r
+  )\r
+{\r
+  UINTN  BufLen;\r
+  CHAR16 *RetString;\r
+\r
+  BufLen      = 0;\r
+  RetString   = NULL;\r
\r
+  if ((Atts & EFI_VARIABLE_NON_VOLATILE) != 0) {\r
+    StrnCatGrow (&RetString, &BufLen, L"+NV", 0);\r
+  }\r
+  if ((Atts & EFI_VARIABLE_RUNTIME_ACCESS) != 0) {\r
+    StrnCatGrow (&RetString, &BufLen, L"+RS+BS", 0);\r
+  } else if ((Atts & EFI_VARIABLE_BOOTSERVICE_ACCESS) != 0) {\r
+    StrnCatGrow (&RetString, &BufLen, L"+BS", 0);\r
+  }\r
+  if ((Atts & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0) {\r
+    StrnCatGrow (&RetString, &BufLen, L"+HR", 0);\r
+  }\r
+  if ((Atts & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) != 0) {\r
+    StrnCatGrow (&RetString, &BufLen, L"+AW", 0);\r
+  }\r
+  if ((Atts & EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) != 0) {\r
+    StrnCatGrow (&RetString, &BufLen, L"+AT", 0);\r
+  }\r
+\r
+  if (RetString == NULL) {\r
+    RetString = StrnCatGrow(&RetString, &BufLen, L"Invalid", 0);\r
+  }\r
+\r
+  if ((RetString != NULL) && (RetString[0] == L'+')) {\r
+    CopyMem(RetString, RetString + 1, StrSize(RetString + 1));\r
+  }\r
+\r
+  return RetString;\r
+}\r
+\r
+/**\r
+  Recursive function to display or delete variables.\r
+\r
+  This function will call itself to create a stack-based list of allt he variables to process, \r
+  then fromt he last to the first, they will do either printing or deleting.\r
+\r
+  This is necessary since once a delete happens GetNextVariableName() will work.\r
+\r
+  @param[in] VariableName   The variable name of the EFI variable (or NULL).\r
+  @param[in] Guid           The GUID of the variable set (or NULL).\r
+  @param[in] Delete         TRUE to delete, FALSE otherwise.\r
+  @param[in] PrevName       The previous variable name from GetNextVariableName. L"" to start.\r
+  @param[in] FoundVarGuid   The previous GUID from GetNextVariableName. ignored at start.\r
+  @param[in] FoundOne       If a VariableName or Guid was specified and one was printed or\r
+                            deleted, then set this to TRUE, otherwise ignored.\r
+\r
+  @retval SHELL_SUCCESS           The operation was successful.\r
+  @retval SHELL_OUT_OF_RESOURCES  A memorty allocation failed.\r
+  @retval SHELL_ABORTED           The abort message was received.\r
+  @retval SHELL_DEVICE_ERROR      UEFI Variable Services returned an error.\r
+  @retval SHELL_NOT_FOUND         the Name/Guid pair could not be found.\r
+**/\r
 SHELL_STATUS\r
 EFIAPI\r
-ProcessVariables (\r
+CascadeProcessVariables (\r
   IN CONST CHAR16   *VariableName OPTIONAL,\r
   IN CONST EFI_GUID *Guid OPTIONAL,\r
-  IN BOOLEAN        Delete\r
+  IN BOOLEAN        Delete,\r
+  IN CONST CHAR16   * CONST PrevName,\r
+  IN EFI_GUID       FoundVarGuid,\r
+  IN BOOLEAN        *FoundOne\r
   )\r
 {\r
   EFI_STATUS                Status;\r
-  UINT64                    MaxStorSize;\r
-  UINT64                    RemStorSize;\r
-  UINT64                    MaxVarSize;\r
   CHAR16                    *FoundVarName;\r
-  UINTN                     Size;\r
-  EFI_GUID                  FoundVarGuid;\r
   UINT8                     *DataBuffer;\r
   UINTN                     DataSize;\r
   UINT32                    Atts;\r
   SHELL_STATUS              ShellStatus;\r
+  UINTN                     NameSize;\r
+  CHAR16                    *RetString;\r
 \r
-  ShellStatus   = SHELL_SUCCESS;\r
-  Size          = PcdGet16(PcdShellFileOperationSize);\r
-  FoundVarName  = AllocatePool(Size);\r
-\r
-  if (FoundVarName == NULL) {\r
-    return (SHELL_OUT_OF_RESOURCES);\r
+  if (ShellGetExecutionBreakFlag()) {\r
+    return (SHELL_ABORTED);\r
   }\r
-  FoundVarName[0] = CHAR_NULL;\r
 \r
-  Status = gRT->QueryVariableInfo(EFI_VARIABLE_BOOTSERVICE_ACCESS|EFI_VARIABLE_RUNTIME_ACCESS|EFI_VARIABLE_NON_VOLATILE, &MaxStorSize, &RemStorSize, &MaxVarSize);\r
-  ASSERT_EFI_ERROR(Status);\r
+  NameSize      = 0;\r
+  FoundVarName  = NULL;\r
 \r
-  DataSize = (UINTN)MaxVarSize;\r
-  DataBuffer = AllocatePool(DataSize);\r
-  if (DataBuffer == NULL) {\r
-    FreePool(FoundVarName);\r
-    return (SHELL_OUT_OF_RESOURCES);\r
+  if (PrevName!=NULL) {\r
+    StrnCatGrow(&FoundVarName, &NameSize, PrevName, 0);\r
+  } else {\r
+    FoundVarName = AllocateZeroPool(sizeof(CHAR16));\r
   }\r
 \r
-  for (;;){\r
-    if (ShellGetExecutionBreakFlag()) {\r
-      ShellStatus = SHELL_ABORTED;\r
-      break;\r
+  Status = gRT->GetNextVariableName (&NameSize, FoundVarName, &FoundVarGuid);\r
+  if (Status == EFI_BUFFER_TOO_SMALL) {\r
+    SHELL_FREE_NON_NULL(FoundVarName);\r
+    FoundVarName = AllocateZeroPool (NameSize);\r
+    if (PrevName != NULL) {\r
+      StrCpy(FoundVarName, PrevName);\r
     }\r
-    Size      = (UINTN)PcdGet16(PcdShellFileOperationSize);\r
-    DataSize  = (UINTN)MaxVarSize;\r
 \r
-    Status = gRT->GetNextVariableName(&Size, FoundVarName, &FoundVarGuid);\r
-    if (Status == EFI_NOT_FOUND) {\r
-      break;\r
-    }\r
-    ASSERT_EFI_ERROR(Status);\r
+    Status = gRT->GetNextVariableName (&NameSize, FoundVarName, &FoundVarGuid);\r
+  }\r
 \r
-    Status = gRT->GetVariable(FoundVarName, &FoundVarGuid, &Atts, &DataSize, DataBuffer);\r
-    ASSERT_EFI_ERROR(Status);\r
+  //\r
+  // No more is fine.\r
+  //\r
+  if (Status == EFI_NOT_FOUND) {\r
+    SHELL_FREE_NON_NULL(FoundVarName);\r
+    return (SHELL_SUCCESS);\r
+  } else if (EFI_ERROR(Status)) {\r
+    SHELL_FREE_NON_NULL(FoundVarName);\r
+    return (SHELL_DEVICE_ERROR);\r
+  }\r
 \r
-    //\r
-    // Check if it matches\r
-    //\r
-    if (VariableName != NULL) {\r
-      if (!gUnicodeCollation->MetaiMatch(gUnicodeCollation, FoundVarName, (CHAR16*)VariableName)) {\r
-        continue;\r
-      }\r
-    }\r
-    if (Guid != NULL) {\r
-      if (!CompareGuid(&FoundVarGuid, Guid)) {\r
-        continue;\r
-      }\r
-    }\r
+  //\r
+  // Recurse to the next iteration.  We know "our" variable's name.\r
+  //\r
+  ShellStatus = CascadeProcessVariables(VariableName, Guid, Delete, FoundVarName, FoundVarGuid, FoundOne);\r
 \r
+  //\r
+  // No matter what happened we process our own variable\r
+  // Only continue if Guid and VariableName are each either NULL or a match\r
+  //\r
+  if ( ( VariableName == NULL \r
+      || gUnicodeCollation->MetaiMatch(gUnicodeCollation, FoundVarName, (CHAR16*)VariableName) )\r
+     && ( Guid == NULL \r
+      || CompareGuid(&FoundVarGuid, Guid) )\r
+      ) {\r
+    DataSize      = 0;\r
+    DataBuffer    = NULL;\r
     //\r
     // do the print or delete\r
     //\r
+    *FoundOne = TRUE;\r
+    Status = gRT->GetVariable (FoundVarName, &FoundVarGuid, &Atts, &DataSize, DataBuffer);\r
+    if (Status == EFI_BUFFER_TOO_SMALL) {\r
+      SHELL_FREE_NON_NULL (DataBuffer);\r
+      DataBuffer = AllocatePool (DataSize);\r
+      if (DataBuffer == NULL) {\r
+        Status = EFI_OUT_OF_RESOURCES;\r
+      } else {\r
+        Status = gRT->GetVariable (FoundVarName, &FoundVarGuid, &Atts, &DataSize, DataBuffer);\r
+      }\r
+    }\r
     if (!Delete) {\r
-      ShellPrintHiiEx(\r
-        -1,\r
-        -1,\r
-        NULL,\r
-        STRING_TOKEN(STR_DMPSTORE_HEADER_LINE),\r
-        gShellDebug1HiiHandle,\r
-        AttrType[Atts & 7],\r
-        &FoundVarGuid,\r
-        FoundVarName,\r
-        DataSize);\r
-      DumpHex(2, 0, DataSize, DataBuffer);\r
+      //\r
+      // Last error check then print this variable out.\r
+      //\r
+      if (!EFI_ERROR(Status) && DataBuffer != NULL) {\r
+        RetString = GetAttrType(Atts);\r
+        ShellPrintHiiEx(\r
+          -1,\r
+          -1,\r
+          NULL,\r
+          STRING_TOKEN(STR_DMPSTORE_HEADER_LINE),\r
+          gShellDebug1HiiHandle,\r
+          RetString,\r
+          &FoundVarGuid,\r
+          FoundVarName,\r
+          DataSize);\r
+        DumpHex(2, 0, DataSize, DataBuffer);\r
+        SHELL_FREE_NON_NULL(RetString);\r
+      }\r
     } else {\r
+      //\r
+      // We only need name to delete it...\r
+      //\r
       ShellPrintHiiEx(\r
         -1,\r
         -1,\r
@@ -127,16 +208,69 @@ ProcessVariables (
         gShellDebug1HiiHandle,\r
         gRT->SetVariable(FoundVarName, &FoundVarGuid, Atts, 0, NULL));\r
     }\r
+    SHELL_FREE_NON_NULL(DataBuffer);\r
   }\r
 \r
-  if (FoundVarName != NULL) {\r
-    FreePool(FoundVarName);\r
-  }\r
-  if (DataBuffer != NULL) {\r
-    FreePool(DataBuffer);\r
+  SHELL_FREE_NON_NULL(FoundVarName);\r
+\r
+  if (Status == EFI_DEVICE_ERROR) {\r
+    ShellStatus = SHELL_DEVICE_ERROR;\r
+  } else if (Status == EFI_SECURITY_VIOLATION) {\r
+    ShellStatus = SHELL_SECURITY_VIOLATION;\r
+  } else if (EFI_ERROR(Status)) {\r
+    ShellStatus = SHELL_NOT_READY;\r
   }\r
 \r
-  return (SHELL_UNSUPPORTED);\r
+  return (ShellStatus);\r
+}\r
+\r
+/**\r
+  Function to display or delete variables.  This will set up and call into the recursive function.\r
+\r
+  @param[in] VariableName   The variable name of the EFI variable (or NULL).\r
+  @param[in] Guid           The GUID of the variable set (or NULL).\r
+  @param[in] Delete         TRUE to delete, FALSE otherwise.\r
+\r
+  @retval SHELL_SUCCESS           The operation was successful.\r
+  @retval SHELL_OUT_OF_RESOURCES  A memorty allocation failed.\r
+  @retval SHELL_ABORTED           The abort message was received.\r
+  @retval SHELL_DEVICE_ERROR      UEFI Variable Services returned an error.\r
+  @retval SHELL_NOT_FOUND         the Name/Guid pair could not be found.\r
+**/\r
+SHELL_STATUS\r
+EFIAPI\r
+ProcessVariables (\r
+  IN CONST CHAR16   *VariableName OPTIONAL,\r
+  IN CONST EFI_GUID *Guid OPTIONAL,\r
+  IN BOOLEAN        Delete\r
+  )\r
+{\r
+  SHELL_STATUS              ShellStatus;\r
+  BOOLEAN                   Found;\r
+  EFI_GUID                  FoundVarGuid;\r
+\r
+  Found         = FALSE;\r
+  ShellStatus   = SHELL_SUCCESS;\r
+  ZeroMem (&FoundVarGuid, sizeof(EFI_GUID));\r
+\r
+  ShellStatus = CascadeProcessVariables(VariableName, Guid, Delete, NULL, FoundVarGuid, &Found);\r
+\r
+  if (!Found) {\r
+    if (ShellStatus == SHELL_OUT_OF_RESOURCES) {\r
+      ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_OUT_MEM), gShellDebug1HiiHandle);\r
+      return (ShellStatus);\r
+    } else if (VariableName != NULL && Guid == NULL) {\r
+      ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_DMPSTORE_NO_VAR_FOUND_N), gShellDebug1HiiHandle, VariableName);\r
+    } else if (VariableName != NULL && Guid != NULL) {\r
+      ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_DMPSTORE_NO_VAR_FOUND_GN), gShellDebug1HiiHandle, Guid, VariableName);\r
+    } else if (VariableName == NULL && Guid == NULL) {\r
+      ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_DMPSTORE_NO_VAR_FOUND), gShellDebug1HiiHandle);\r
+    } else if (VariableName == NULL && Guid != NULL) {\r
+      ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_DMPSTORE_NO_VAR_FOUND_G), gShellDebug1HiiHandle, Guid);\r
+    } \r
+    return (SHELL_NOT_FOUND);\r
+  }\r
+  return (ShellStatus);\r
 }\r
 \r
 STATIC CONST SHELL_PARAM_ITEM ParamList[] = {\r
@@ -148,6 +282,12 @@ STATIC CONST SHELL_PARAM_ITEM ParamList[] = {
   {NULL, TypeMax}\r
   };\r
 \r
+/**\r
+  Function for 'dmpstore' command.\r
+\r
+  @param[in] ImageHandle  Handle to the Image (NULL if Internal).\r
+  @param[in] SystemTable  Pointer to the System Table (NULL if Internal).\r
+**/\r
 SHELL_STATUS\r
 EFIAPI\r
 ShellCommandRunDmpStore (\r
@@ -177,10 +317,7 @@ ShellCommandRunDmpStore (
       ASSERT(FALSE);\r
     }\r
   } else {\r
-    if (ShellCommandLineGetCount(Package) < 1) {\r
-      ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellDebug1HiiHandle);\r
-      ShellStatus = SHELL_INVALID_PARAMETER;\r
-    } else if (ShellCommandLineGetCount(Package) > 2) {\r
+    if (ShellCommandLineGetCount(Package) > 2) {\r
       ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellDebug1HiiHandle);\r
       ShellStatus = SHELL_INVALID_PARAMETER;\r
     } else if (ShellCommandLineGetFlag(Package, L"-all") && ShellCommandLineGetFlag(Package, L"-guid")) {\r
@@ -195,24 +332,23 @@ ShellCommandRunDmpStore (
         if (Temp != NULL) {\r
           Status = ConvertStringToGuid(Temp, &GuidData);\r
           if (EFI_ERROR(Status)) {\r
-            ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellDebug1HiiHandle, L"-guid");\r
+            ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellDebug1HiiHandle, Temp);\r
             ShellStatus = SHELL_INVALID_PARAMETER;\r
           }\r
           Guid = &GuidData;\r
         } else  {\r
           Guid = &gEfiGlobalVariableGuid;\r
         }\r
-        VariableName = ShellCommandLineGetRawValue(Package, 2);\r
+        VariableName = ShellCommandLineGetRawValue(Package, 1);\r
       } else {\r
         VariableName  = NULL;\r
         Guid          = NULL;\r
       }\r
       if (ShellStatus == SHELL_SUCCESS) {\r
         if (ShellCommandLineGetFlag(Package, L"-s") || ShellCommandLineGetFlag(Package, L"-l")) {\r
-          ///@todo fix this after Jordan makes lib...\r
-          ShellPrintEx(-1, -1, L"Not implemeneted yet (ASSERT follows).\r\n");\r
-          ShellStatus = SHELL_INVALID_PARAMETER;\r
-          ASSERT(FALSE);\r
+          ///@todo fix this after lib ready...\r
+          ShellPrintEx(-1, -1, L"Not implemeneted yet.\r\n");\r
+          ShellStatus = SHELL_UNSUPPORTED;\r
         } else {\r
           ShellStatus = ProcessVariables (VariableName, Guid, ShellCommandLineGetFlag(Package, L"-d"));\r
         }\r