]> git.proxmox.com Git - mirror_edk2.git/blobdiff - ShellPkg/Library/UefiShellLevel2CommandsLib/TimeDate.c
ShellPkg: Code refine. Add error handling code to check pointer and remove redundant...
[mirror_edk2.git] / ShellPkg / Library / UefiShellLevel2CommandsLib / TimeDate.c
index 6fbb3e59a9738050efc957b3c9275ee118867414..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
@@ -230,7 +231,13 @@ ShellCommandRunDate (
         // 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
@@ -338,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
@@ -452,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
@@ -511,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
@@ -529,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
@@ -575,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
@@ -614,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
@@ -644,6 +717,20 @@ 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
@@ -667,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
@@ -817,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
@@ -829,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
@@ -841,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