]> git.proxmox.com Git - mirror_edk2.git/blobdiff - ShellPkg/Library/UefiShellLevel2CommandsLib/TimeDate.c
ShellPkg: Fix timezone command
[mirror_edk2.git] / ShellPkg / Library / UefiShellLevel2CommandsLib / TimeDate.c
index 44e83ba254808cde35e0b10969f99c7932dfc191..25dc6ffb640f889a87b9b5d4a13e4134ee01ebff 100644 (file)
@@ -1,7 +1,8 @@
 /** @file\r
   Main file for time, timezone, and date shell level 2 and shell level 3 functions.\r
 \r
-  Copyright (c) 2009 - 2011, Intel Corporation. All rights reserved.<BR>\r
+  (C) Copyright 2012-2014, Hewlett-Packard Development Company, L.P.\r
+  Copyright (c) 2009 - 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
@@ -179,6 +180,7 @@ ShellCommandRunDate (
   EFI_TIME      TheTime;\r
   CHAR16        *ProblemParam;\r
   SHELL_STATUS  ShellStatus;\r
+  CONST CHAR16  *Param1;\r
 \r
   ShellStatus  = SHELL_SUCCESS;\r
   ProblemParam = NULL;\r
@@ -220,13 +222,22 @@ ShellCommandRunDate (
         // get the current date\r
         //\r
         Status = gRT->GetTime(&TheTime, NULL);\r
-        ASSERT_EFI_ERROR(Status);\r
+        if (EFI_ERROR(Status)) {\r
+          ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_UEFI_FUNC_WARN), gShellLevel2HiiHandle, L"gRT->GetTime", Status);\r
+          return (SHELL_DEVICE_ERROR);\r
+        }\r
 \r
         //\r
         // ShellPrintEx the date in SFO or regular format\r
         //\r
         if (ShellCommandLineGetFlag(Package, L"-sfo")) {\r
-          ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_DATE_SFO_FORMAT), gShellLevel2HiiHandle, TheTime.Month, TheTime.Day, TheTime.Year);\r
+          //\r
+          // Match UEFI Shell spec:\r
+          // ShellCommand,"date"\r
+          // Date,"DD","MM","YYYY"\r
+          //\r
+          ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_SFO_HEADER), gShellLevel2HiiHandle, L"date");\r
+          ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_DATE_SFO_FORMAT), gShellLevel2HiiHandle, TheTime.Day, TheTime.Month, TheTime.Year);\r
         } else {\r
           ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_DATE_FORMAT), gShellLevel2HiiHandle, TheTime.Month, TheTime.Day, TheTime.Year);\r
         }\r
@@ -238,9 +249,14 @@ ShellCommandRunDate (
           //\r
           // perform level 3 operation here.\r
           //\r
-          ShellStatus = CheckAndSetDate(ShellCommandLineGetRawValue(Package, 1));\r
+          Param1 = ShellCommandLineGetRawValue(Package, 1);\r
+          if (Param1 == NULL) {\r
+            ShellStatus = SHELL_INVALID_PARAMETER;\r
+          } else {\r
+            ShellStatus = CheckAndSetDate(Param1);\r
+          }\r
           if (ShellStatus != SHELL_SUCCESS) {\r
-            ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellLevel2HiiHandle, ShellCommandLineGetRawValue(Package, 1));\r
+            ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellLevel2HiiHandle, Param1);\r
             ShellStatus = SHELL_INVALID_PARAMETER;\r
           }\r
         }\r
@@ -306,7 +322,10 @@ CheckAndSetTime (
   }\r
 \r
   Status = gRT->GetTime(&TheTime, NULL);\r
-  ASSERT_EFI_ERROR(Status);\r
+  if (EFI_ERROR(Status)) {\r
+    ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_UEFI_FUNC_WARN), gShellLevel2HiiHandle, L"gRT->GetTime", Status);\r
+    return (SHELL_DEVICE_ERROR);\r
+  }\r
 \r
   if (TimeString != NULL) {\r
     TimeStringCopy = NULL;\r
@@ -326,21 +345,35 @@ CheckAndSetTime (
     Walker2          = Walker1!=NULL?StrStr(Walker1, L":"):NULL;\r
     if (Walker2 != NULL && *Walker2 == L':') {\r
       *Walker2 = CHAR_NULL;\r
+      TheTime.Second = (UINT8)0;\r
+    }\r
+    else if (Walker2 == NULL) {\r
+      TheTime.Second = (UINT8)0;\r
     }\r
     if (Walker1 != NULL && Walker1[0] != CHAR_NULL) {\r
       TheTime.Minute = (UINT8)ShellStrToUintn (Walker1);\r
       if (Walker2 != NULL) {\r
         Walker1 = Walker2 + 1;\r
-      }\r
-      if (Walker1 != NULL && Walker1[0] != CHAR_NULL) {\r
-        TheTime.Second = (UINT8)ShellStrToUintn (Walker1);\r
+        if (Walker1 != NULL && Walker1[0] != CHAR_NULL) {\r
+          TheTime.Second = (UINT8)ShellStrToUintn (Walker1);\r
+        }\r
       }\r
     }\r
     SHELL_FREE_NON_NULL(TimeStringCopy);\r
   }\r
 \r
 \r
-  if ((Tz >= -1440 && Tz <= 1440)||(Tz == 0x7FF)) {\r
+  if (Tz >= -1440 && Tz <= 1440) {\r
+    //\r
+    // EFI_TIME TimeZone is stored to meet the following calculation (see UEFI Spec):\r
+    // Localtime = UTC - TimeZone\r
+    // This means the sign must be changed for the user provided Tz.\r
+    // EX: User wants to set TimeZone to Pacific Standard Time, so runs\r
+    // time -tz -480 # set to UTC-08:00\r
+    // To meet the calculation, the sign must be changed.\r
+    //\r
+    TheTime.TimeZone = -Tz;\r
+  } else if (Tz == EFI_UNSPECIFIED_TIMEZONE) {\r
     TheTime.TimeZone = Tz;\r
   }\r
 \r
@@ -372,7 +405,6 @@ ShellCommandRunTime (
 {\r
   EFI_STATUS    Status;\r
   LIST_ENTRY    *Package;\r
-  CHAR16        *Message;\r
   EFI_TIME      TheTime;\r
   CHAR16        *ProblemParam;\r
   SHELL_STATUS  ShellStatus;\r
@@ -381,13 +413,11 @@ ShellCommandRunTime (
   CONST CHAR16  *TempLocation;\r
   UINTN         TzMinutes;\r
 \r
-  ShellStatus  = SHELL_SUCCESS;\r
-  ProblemParam = NULL;\r
-\r
   //\r
   // Initialize variables\r
   //\r
-  Message = NULL;\r
+  ShellStatus  = SHELL_SUCCESS;\r
+  ProblemParam = NULL;\r
 \r
   //\r
   // initialize the shell lib (we must be in non-auto-init...)\r
@@ -417,7 +447,11 @@ ShellCommandRunTime (
     // check for "-?"\r
     //\r
     Status = gRT->GetTime(&TheTime, NULL);\r
-    ASSERT_EFI_ERROR(Status);\r
+    if (EFI_ERROR(Status)) {\r
+      ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_UEFI_FUNC_WARN), gShellLevel2HiiHandle, L"gRT->GetTime", Status);\r
+      return (SHELL_DEVICE_ERROR);\r
+    }\r
+\r
     if (ShellCommandLineGetFlag(Package, L"-?")) {\r
       ASSERT(FALSE);\r
     } else if (ShellCommandLineGetRawValue(Package, 2) != NULL) {\r
@@ -439,40 +473,61 @@ ShellCommandRunTime (
           TzMinutes = (ABS(TheTime.TimeZone)) % 60;\r
         }\r
 \r
-        ShellPrintHiiEx (\r
-          -1,\r
-          -1,\r
-          NULL,\r
-          STRING_TOKEN (STR_TIME_FORMAT),\r
-          gShellLevel2HiiHandle,\r
-          TheTime.Hour,\r
-          TheTime.Minute,\r
-          TheTime.Second,\r
-          TheTime.TimeZone==EFI_UNSPECIFIED_TIMEZONE?L" ":(TheTime.TimeZone > 0?L"-":L"+"),\r
-          TheTime.TimeZone==EFI_UNSPECIFIED_TIMEZONE?0:(ABS(TheTime.TimeZone)) / 60,\r
-          TzMinutes\r
-         );\r
-         ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_CRLF), gShellLevel2HiiHandle);\r
+        if (TheTime.TimeZone != EFI_UNSPECIFIED_TIMEZONE) {\r
+          ShellPrintHiiEx (\r
+            -1,\r
+            -1,\r
+            NULL,\r
+            STRING_TOKEN (STR_TIME_FORMAT),\r
+            gShellLevel2HiiHandle,\r
+            TheTime.Hour,\r
+            TheTime.Minute,\r
+            TheTime.Second,\r
+            (TheTime.TimeZone > 0?L"-":L"+"),\r
+            ((ABS(TheTime.TimeZone)) / 60),\r
+            TzMinutes\r
+            );\r
+        } else {\r
+          ShellPrintHiiEx (\r
+            -1,\r
+            -1,\r
+            NULL,\r
+            STRING_TOKEN (STR_TIME_FORMAT_LOCAL),\r
+            gShellLevel2HiiHandle,\r
+            TheTime.Hour,\r
+            TheTime.Minute,\r
+            TheTime.Second\r
+            );\r
+        }\r
+        ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_CRLF), gShellLevel2HiiHandle);\r
       } else if (ShellCommandLineGetFlag(Package, L"-d") && ShellCommandLineGetValue(Package, L"-d") == NULL) {\r
         if (TheTime.TimeZone == EFI_UNSPECIFIED_TIMEZONE) {\r
-          TzMinutes = 0;\r
+          ShellPrintHiiEx (\r
+            -1,\r
+            -1,\r
+            NULL,\r
+            STRING_TOKEN (STR_TIME_FORMAT_LOCAL),\r
+            gShellLevel2HiiHandle,\r
+            TheTime.Hour,\r
+            TheTime.Minute,\r
+            TheTime.Second\r
+            );\r
         } else {\r
           TzMinutes = (ABS(TheTime.TimeZone)) % 60;\r
+          ShellPrintHiiEx (\r
+            -1,\r
+            -1,\r
+            NULL,\r
+            STRING_TOKEN (STR_TIME_FORMAT),\r
+            gShellLevel2HiiHandle,\r
+            TheTime.Hour,\r
+            TheTime.Minute,\r
+            TheTime.Second,\r
+            (TheTime.TimeZone > 0?L"-":L"+"),\r
+            ((ABS(TheTime.TimeZone)) / 60),\r
+            TzMinutes\r
+           );\r
         }\r
-\r
-        ShellPrintHiiEx (\r
-          -1,\r
-          -1,\r
-          NULL,\r
-          STRING_TOKEN (STR_TIME_FORMAT),\r
-          gShellLevel2HiiHandle,\r
-          TheTime.Hour,\r
-          TheTime.Minute,\r
-          TheTime.Second,\r
-          TheTime.TimeZone==EFI_UNSPECIFIED_TIMEZONE?L" ":(TheTime.TimeZone > 0?L"-":L"+"),\r
-          TheTime.TimeZone==EFI_UNSPECIFIED_TIMEZONE?0:(ABS(TheTime.TimeZone)) / 60,\r
-          TzMinutes\r
-         );\r
           switch (TheTime.Daylight) {\r
             case 0:\r
               ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_TIME_DST0), gShellLevel2HiiHandle);\r
@@ -498,10 +553,32 @@ ShellCommandRunTime (
           // perform level 3 operation here.\r
           //\r
           if ((TempLocation = ShellCommandLineGetValue(Package, L"-tz")) != NULL) {\r
-            if (TempLocation[0] == L'-') {\r
-              Tz = (INT16)(0 - ShellStrToUintn(++TempLocation));\r
+            if (StrniCmp (TempLocation, L"_local", StrLen (TempLocation)) == NULL) {\r
+              Tz = EFI_UNSPECIFIED_TIMEZONE;\r
+            } else if (TempLocation[0] == L'-') {\r
+\r
+              Tz = (INT16) ShellStrToUintn (++TempLocation);\r
+              //\r
+              // When the argument of "time [-tz tz]" is not numeric, ShellStrToUintn() returns "-1".\r
+              // Here we can detect the argument error by checking the return of ShellStrToUintn().\r
+              //\r
+              if (Tz == -1) {\r
+                Tz = 1441; //make it to be out of bounds value\r
+              } else {\r
+                Tz *= (-1); //sign convert\r
+              }\r
             } else {\r
-              Tz = (INT16)ShellStrToUintn(TempLocation);\r
+              if (TempLocation[0] == L'+') {\r
+                Tz = (INT16)ShellStrToUintn (++TempLocation);\r
+              } else {\r
+                Tz = (INT16)ShellStrToUintn (TempLocation);\r
+              }\r
+              //\r
+              // Detect the return of ShellStrToUintn() to make sure the argument is valid.\r
+              //\r
+              if (Tz == -1) {\r
+                Tz = 1441; //make it to be out of bounds value\r
+              }\r
             }\r
             if (!(Tz >= -1440 && Tz <= 1440) && Tz != EFI_UNSPECIFIED_TIMEZONE) {\r
               ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM_VAL), gShellLevel2HiiHandle, L"-tz");\r
@@ -516,6 +593,14 @@ ShellCommandRunTime (
           TempLocation = ShellCommandLineGetValue(Package, L"-d");\r
           if (TempLocation != NULL) {\r
             Daylight = (UINT8)ShellStrToUintn(TempLocation);\r
+            //\r
+            // The argument of "time [-d dl]" is unsigned, if the first character is '-',\r
+            // the argument is incorrect.  That's because ShellStrToUintn() will skip past\r
+            // any '-' sign and convert what's next, forgetting the sign is here.\r
+            //\r
+            if (TempLocation[0] == '-') {\r
+              Daylight = 0xff; //make it invalid = will not use\r
+            }\r
             if (Daylight != 0 && Daylight != 1 && Daylight != 3) {\r
               ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM_VAL), gShellLevel2HiiHandle, L"-d");\r
               ShellStatus = SHELL_INVALID_PARAMETER;\r
@@ -562,7 +647,7 @@ STATIC CONST SHELL_PARAM_ITEM TimeZoneParamList2[] = {
 STATIC CONST SHELL_PARAM_ITEM TimeZoneParamList3[] = {\r
   {L"-l", TypeFlag},\r
   {L"-f", TypeFlag},\r
-  {L"-s", TypeValue},\r
+  {L"-s", TypeTimeValue},\r
   {NULL, TypeMax}\r
   };\r
 \r
@@ -601,7 +686,8 @@ STATIC CONST SHELL_PARAM_ITEM TimeZoneParamList3[] = {
     {-660 , STRING_TOKEN (STR_TIMEZONE_P11)},\r
     {-720 , STRING_TOKEN (STR_TIMEZONE_P12)},\r
     {-780 , STRING_TOKEN (STR_TIMEZONE_P13)},\r
-    {-840 , STRING_TOKEN (STR_TIMEZONE_P14)}\r
+    {-840 , STRING_TOKEN (STR_TIMEZONE_P14)},\r
+    {EFI_UNSPECIFIED_TIMEZONE, STRING_TOKEN (STR_TIMEZONE_LOCAL)}\r
 };\r
 \r
 /**\r
@@ -631,15 +717,35 @@ CheckAndSetTimeZone (
     return (SHELL_INVALID_PARAMETER);\r
   }\r
 \r
+  if (StrniCmp (TimeZoneString, L"_local", StrLen (TimeZoneString)) == NULL) {\r
+    Status = gRT->GetTime (&TheTime, NULL);\r
+    if (EFI_ERROR (Status)) {\r
+      ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_UEFI_FUNC_WARN), gShellLevel2HiiHandle, L"gRT->GetTime", Status);\r
+      return (SHELL_DEVICE_ERROR);\r
+    }\r
+\r
+    TheTime.TimeZone = EFI_UNSPECIFIED_TIMEZONE;\r
+    Status = gRT->SetTime (&TheTime);\r
+    if (!EFI_ERROR(Status)){\r
+      return (SHELL_SUCCESS);\r
+    }\r
+    return (SHELL_INVALID_PARAMETER);\r
+  }\r
   if (TimeZoneString != NULL && !InternalIsTimeLikeString(TimeZoneString, L':', 1, 1, TRUE)) {\r
     return (SHELL_INVALID_PARAMETER);\r
   }\r
 \r
   Status = gRT->GetTime(&TheTime, NULL);\r
-  ASSERT_EFI_ERROR(Status);\r
+  if (EFI_ERROR(Status)) {\r
+    ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_UEFI_FUNC_WARN), gShellLevel2HiiHandle, L"gRT->GetTime", Status);\r
+    return (SHELL_DEVICE_ERROR);\r
+  }\r
 \r
   TimeZoneCopy = NULL;\r
   TimeZoneCopy = StrnCatGrow(&TimeZoneCopy, NULL, TimeZoneString, 0);\r
+  if (TimeZoneCopy == NULL) {\r
+    return (SHELL_OUT_OF_RESOURCES);\r
+  }\r
   Walker = TimeZoneCopy;\r
   Walker2 = StrStr(Walker, L":");\r
   if (Walker2 != NULL && *Walker2 == L':') {\r
@@ -648,7 +754,7 @@ CheckAndSetTimeZone (
   if (*Walker == L'-') {\r
     TheTime.TimeZone = (INT16)((ShellStrToUintn (++Walker)) * 60);\r
   } else {\r
-    TheTime.TimeZone = (INT16)((ShellStrToUintn (Walker)) * -60);\r
+    TheTime.TimeZone = (INT16)((INT16)(ShellStrToUintn (Walker)) * -60);\r
   }\r
   if (Walker2 != NULL) {\r
     Walker = Walker2 + 1;\r
@@ -720,10 +826,10 @@ ShellCommandRunTimeZone (
   // parse the command line\r
   //\r
   if (PcdGet8(PcdShellSupportLevel) == 2) {\r
-    Status = ShellCommandLineParse (TimeZoneParamList2, &Package, &ProblemParam, FALSE);\r
+    Status = ShellCommandLineParse (TimeZoneParamList2, &Package, &ProblemParam, TRUE);\r
   } else {\r
     ASSERT(PcdGet8(PcdShellSupportLevel) == 3);\r
-    Status = ShellCommandLineParseEx (TimeZoneParamList3, &Package, &ProblemParam, FALSE, TRUE);\r
+    Status = ShellCommandLineParseEx (TimeZoneParamList3, &Package, &ProblemParam, TRUE, TRUE);\r
   }\r
   if (EFI_ERROR(Status)) {\r
     if (Status == EFI_VOLUME_CORRUPTED && ProblemParam != NULL) {\r
@@ -777,7 +883,10 @@ ShellCommandRunTimeZone (
       // Get Current Time Zone Info\r
       //\r
       Status = gRT->GetTime(&TheTime, NULL);\r
-      ASSERT_EFI_ERROR(Status);\r
+      if (EFI_ERROR(Status)) {\r
+        ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_UEFI_FUNC_WARN), gShellLevel2HiiHandle, L"gRT->GetTime", Status);\r
+        return (SHELL_DEVICE_ERROR);\r
+      }\r
 \r
       if (TheTime.TimeZone != EFI_UNSPECIFIED_TIMEZONE) {\r
         Found = FALSE;\r
@@ -795,11 +904,7 @@ ShellCommandRunTimeZone (
               //\r
               // Print basic info only\r
               //\r
-              if (TheTime.TimeZone == EFI_UNSPECIFIED_TIMEZONE) {\r
-                TzMinutes = 0;\r
-              } else {\r
-                TzMinutes = (ABS(TheTime.TimeZone)) % 60;\r
-              }\r
+              TzMinutes = (ABS(TheTime.TimeZone)) % 60;\r
 \r
               ShellPrintHiiEx (\r
                 -1,\r
@@ -807,8 +912,8 @@ ShellCommandRunTimeZone (
                 NULL,\r
                 STRING_TOKEN(STR_TIMEZONE_SIMPLE),\r
                 gShellLevel2HiiHandle,\r
-                TheTime.TimeZone==EFI_UNSPECIFIED_TIMEZONE?0:(TheTime.TimeZone > 0?L"-":L"+"),\r
-                TheTime.TimeZone==EFI_UNSPECIFIED_TIMEZONE?0:(ABS(TheTime.TimeZone)) / 60,\r
+                (TheTime.TimeZone > 0?L"-":L"+"),\r
+                (ABS(TheTime.TimeZone)) / 60,\r
                 TzMinutes);\r
             }\r
             Found = TRUE;\r
@@ -819,28 +924,45 @@ ShellCommandRunTimeZone (
           //\r
           // Print basic info only\r
           //\r
-          if (TheTime.TimeZone == EFI_UNSPECIFIED_TIMEZONE) {\r
-            TzMinutes = 0;\r
-          } else {\r
-            TzMinutes = (ABS(TheTime.TimeZone)) % 60;\r
-          }\r
+          TzMinutes = (ABS(TheTime.TimeZone)) % 60;\r
+\r
           ShellPrintHiiEx (\r
             -1,\r
             -1,\r
             NULL,\r
             STRING_TOKEN(STR_TIMEZONE_SIMPLE),\r
             gShellLevel2HiiHandle,\r
-            TheTime.TimeZone==EFI_UNSPECIFIED_TIMEZONE?0:(TheTime.TimeZone > 0?L"-":L"+"),\r
-            TheTime.TimeZone==EFI_UNSPECIFIED_TIMEZONE?0:(ABS(TheTime.TimeZone)) / 60,\r
+            (TheTime.TimeZone > 0?L"-":L"+"),\r
+            (ABS(TheTime.TimeZone)) / 60,\r
             TzMinutes);\r
+\r
           if (ShellCommandLineGetFlag(Package, L"-f")) {\r
             ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN(STR_TIMEZONE_NI), gShellLevel2HiiHandle);\r
           }\r
         }\r
       } else {\r
         //\r
-        // TimeZone was EFI_UNSPECIFIED_TIMEZONE (unknown) from GetTime()\r
+        // TimeZone was EFI_UNSPECIFIED_TIMEZONE (local) from GetTime()\r
         //\r
+        if (ShellCommandLineGetFlag (Package, L"-f")) {\r
+          for ( LoopVar = 0\r
+              ; LoopVar < sizeof (TimeZoneList) / sizeof (TimeZoneList[0])\r
+              ; LoopVar++\r
+             ){\r
+            if (TheTime.TimeZone == TimeZoneList[LoopVar].TimeZone) {\r
+              //\r
+              //  Print all info about current time zone\r
+              //\r
+              ShellPrintHiiEx (-1, -1, NULL, TimeZoneList[LoopVar].StringId, gShellLevel2HiiHandle);\r
+              break;\r
+            } \r
+          }\r
+        } else {\r
+          //\r
+          // Print basic info only\r
+          //\r
+          ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_TIMEZONE_SIMPLE_LOCAL), gShellLevel2HiiHandle);\r
+        }\r
       }\r
     }\r
   }\r