]> git.proxmox.com Git - mirror_edk2.git/blob - ShellPkg/Library/UefiShellLevel2CommandsLib/TimeDate.c
ShellPkg: Apply uncrustify changes
[mirror_edk2.git] / ShellPkg / Library / UefiShellLevel2CommandsLib / TimeDate.c
1 /** @file
2 Main file for time, timezone, and date shell level 2 and shell level 3 functions.
3
4 (C) Copyright 2012-2015 Hewlett-Packard Development Company, L.P.<BR>
5 Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
7
8 **/
9
10 #include "UefiShellLevel2CommandsLib.h"
11
12 /**
13 Determine if String is a valid representation for a time or date.
14
15 @param[in] String The pointer to the string to test.
16 @param[in] Char The delimeter character.
17 @param[in] Min The minimum value allowed.
18 @param[in] Max The maximum value allowed.
19 @param[in] MinusOk Whether negative numbers are permitted.
20
21 @retval TRUE String is a valid representation.
22 @retval FALSE String is invalid.
23 **/
24 BOOLEAN
25 InternalIsTimeLikeString (
26 IN CONST CHAR16 *String,
27 IN CONST CHAR16 Char,
28 IN CONST UINTN Min,
29 IN CONST UINTN Max,
30 IN CONST BOOLEAN MinusOk
31 )
32 {
33 UINTN Count;
34
35 Count = 0;
36
37 if (MinusOk) {
38 //
39 // A single minus is ok.
40 //
41 if (*String == L'-') {
42 String++;
43 }
44 }
45
46 //
47 // the first char must be numeric.
48 //
49 if (!ShellIsDecimalDigitCharacter (*String)) {
50 return (FALSE);
51 }
52
53 //
54 // loop through the characters and use the lib function
55 //
56 for ( ; String != NULL && *String != CHAR_NULL; String++) {
57 if (*String == Char) {
58 Count++;
59 if (Count > Max) {
60 return (FALSE);
61 }
62
63 continue;
64 }
65
66 if (!ShellIsDecimalDigitCharacter (*String)) {
67 return (FALSE);
68 }
69 }
70
71 if (Count < Min) {
72 return (FALSE);
73 }
74
75 return (TRUE);
76 }
77
78 /**
79 Verify that the DateString is valid and if so set that as the current
80 date.
81
82 @param[in] DateString The pointer to a string representation of the date.
83
84 @retval SHELL_INVALID_PARAMETER DateString was NULL.
85 @retval SHELL_INVALID_PARAMETER DateString was mis-formatted.
86 @retval SHELL_SUCCESS The operation was successful.
87 **/
88 SHELL_STATUS
89 CheckAndSetDate (
90 IN CONST CHAR16 *DateString
91 )
92 {
93 EFI_TIME TheTime;
94 EFI_STATUS Status;
95 CHAR16 *DateStringCopy;
96 CHAR16 *Walker;
97 CHAR16 *Walker1;
98
99 if (!InternalIsTimeLikeString (DateString, L'/', 2, 2, FALSE)) {
100 return (SHELL_INVALID_PARAMETER);
101 }
102
103 Status = gRT->GetTime (&TheTime, NULL);
104 if (EFI_ERROR (Status)) {
105 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_UEFI_FUNC_WARN), gShellLevel2HiiHandle, L"date", L"gRT->GetTime", Status);
106 return (SHELL_DEVICE_ERROR);
107 }
108
109 DateStringCopy = NULL;
110 DateStringCopy = StrnCatGrow (&DateStringCopy, NULL, DateString, 0);
111 if (DateStringCopy == NULL) {
112 return (SHELL_OUT_OF_RESOURCES);
113 }
114
115 Walker = DateStringCopy;
116
117 TheTime.Month = 0xFF;
118 TheTime.Day = 0xFF;
119 TheTime.Year = 0xFFFF;
120
121 Walker1 = StrStr (Walker, L"/");
122 if ((Walker1 != NULL) && (*Walker1 == L'/')) {
123 *Walker1 = CHAR_NULL;
124 }
125
126 TheTime.Month = (UINT8)ShellStrToUintn (Walker);
127 if (Walker1 != NULL) {
128 Walker = Walker1 + 1;
129 }
130
131 Walker1 = Walker != NULL ? StrStr (Walker, L"/") : NULL;
132 if ((Walker1 != NULL) && (*Walker1 == L'/')) {
133 *Walker1 = CHAR_NULL;
134 }
135
136 if ((Walker != NULL) && (Walker[0] != CHAR_NULL)) {
137 TheTime.Day = (UINT8)ShellStrToUintn (Walker);
138 if (Walker1 != NULL) {
139 Walker = Walker1 + 1;
140 }
141
142 Walker1 = Walker != NULL ? StrStr (Walker, L"/") : NULL;
143 if ((Walker1 != NULL) && (*Walker1 == L'/')) {
144 *Walker1 = CHAR_NULL;
145 }
146
147 if ((Walker != NULL) && (Walker[0] != CHAR_NULL)) {
148 TheTime.Year = (UINT16)ShellStrToUintn (Walker);
149 }
150 }
151
152 if (TheTime.Year < 100) {
153 if (TheTime.Year >= 98) {
154 TheTime.Year = (UINT16)(1900 + TheTime.Year);
155 } else {
156 TheTime.Year = (UINT16)(2000 + TheTime.Year);
157 }
158 }
159
160 Status = gRT->SetTime (&TheTime);
161
162 if (!EFI_ERROR (Status)) {
163 return (SHELL_SUCCESS);
164 }
165
166 return (SHELL_INVALID_PARAMETER);
167 }
168
169 /**
170 Function for 'date' command.
171
172 @param[in] ImageHandle Handle to the Image (NULL if Internal).
173 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
174 **/
175 SHELL_STATUS
176 EFIAPI
177 ShellCommandRunDate (
178 IN EFI_HANDLE ImageHandle,
179 IN EFI_SYSTEM_TABLE *SystemTable
180 )
181 {
182 EFI_STATUS Status;
183 LIST_ENTRY *Package;
184 EFI_TIME TheTime;
185 CHAR16 *ProblemParam;
186 SHELL_STATUS ShellStatus;
187 CONST CHAR16 *Param1;
188
189 ShellStatus = SHELL_SUCCESS;
190 ProblemParam = NULL;
191
192 //
193 // initialize the shell lib (we must be in non-auto-init...)
194 //
195 Status = ShellInitialize ();
196 ASSERT_EFI_ERROR (Status);
197
198 //
199 // parse the command line
200 //
201 Status = ShellCommandLineParse (SfoParamList, &Package, &ProblemParam, TRUE);
202 if (EFI_ERROR (Status)) {
203 if ((Status == EFI_VOLUME_CORRUPTED) && (ProblemParam != NULL)) {
204 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellLevel2HiiHandle, L"date", ProblemParam);
205 FreePool (ProblemParam);
206 ShellStatus = SHELL_INVALID_PARAMETER;
207 } else {
208 ASSERT (FALSE);
209 }
210 } else {
211 //
212 // check for "-?"
213 //
214 if (ShellCommandLineGetFlag (Package, L"-?")) {
215 ASSERT (FALSE);
216 } else if (ShellCommandLineGetRawValue (Package, 2) != NULL) {
217 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellLevel2HiiHandle, L"date");
218 ShellStatus = SHELL_INVALID_PARAMETER;
219 } else {
220 //
221 // If there are 0 value parameters, then print the current date
222 // else If there are any value paramerers, then print error
223 //
224 if (ShellCommandLineGetRawValue (Package, 1) == NULL) {
225 //
226 // get the current date
227 //
228 Status = gRT->GetTime (&TheTime, NULL);
229 if (EFI_ERROR (Status)) {
230 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_UEFI_FUNC_WARN), gShellLevel2HiiHandle, L"date", L"gRT->GetTime", Status);
231 return (SHELL_DEVICE_ERROR);
232 }
233
234 //
235 // ShellPrintEx the date in SFO or regular format
236 //
237 if (ShellCommandLineGetFlag (Package, L"-sfo")) {
238 //
239 // Match UEFI Shell spec:
240 // ShellCommand,"date"
241 // Date,"DD","MM","YYYY"
242 //
243 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_SFO_HEADER), gShellLevel2HiiHandle, L"date");
244 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_DATE_SFO_FORMAT), gShellLevel2HiiHandle, TheTime.Day, TheTime.Month, TheTime.Year);
245 } else {
246 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_DATE_FORMAT), gShellLevel2HiiHandle, TheTime.Month, TheTime.Day, TheTime.Year);
247 }
248 } else {
249 if (PcdGet8 (PcdShellSupportLevel) == 2) {
250 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellLevel2HiiHandle, L"date");
251 ShellStatus = SHELL_INVALID_PARAMETER;
252 } else {
253 //
254 // perform level 3 operation here.
255 //
256 Param1 = ShellCommandLineGetRawValue (Package, 1);
257 if (Param1 == NULL) {
258 ShellStatus = SHELL_INVALID_PARAMETER;
259 } else {
260 ShellStatus = CheckAndSetDate (Param1);
261 }
262
263 if (ShellStatus != SHELL_SUCCESS) {
264 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV), gShellLevel2HiiHandle, L"date", Param1);
265 ShellStatus = SHELL_INVALID_PARAMETER;
266 }
267 }
268 }
269 }
270 }
271
272 //
273 // free the command line package
274 //
275 ShellCommandLineFreeVarList (Package);
276
277 //
278 // return the status
279 //
280 return (ShellStatus);
281 }
282
283 //
284 // Note "-tz" is invalid for this (non-interactive) version of 'time'.
285 //
286 STATIC CONST SHELL_PARAM_ITEM TimeParamList2[] = {
287 { L"-d", TypeValue },
288 { NULL, TypeMax }
289 };
290
291 STATIC CONST SHELL_PARAM_ITEM TimeParamList3[] = {
292 { L"-d", TypeValue },
293 { L"-tz", TypeValue },
294 { NULL, TypeMax }
295 };
296
297 /**
298 Verify that the TimeString is valid and if so set that as the current
299 time.
300
301 @param[in] TimeString The pointer to a string representation of the time.
302 @param[in] Tz The value to set for TimeZone.
303 @param[in] Daylight The value to set for Daylight.
304
305 @retval SHELL_INVALID_PARAMETER TimeString was NULL.
306 @retval SHELL_INVALID_PARAMETER TimeString was mis-formatted.
307 @retval SHELL_SUCCESS The operation was successful.
308 **/
309 SHELL_STATUS
310 CheckAndSetTime (
311 IN CONST CHAR16 *TimeString,
312 IN CONST INT16 Tz,
313 IN CONST UINT8 Daylight
314 )
315 {
316 EFI_TIME TheTime;
317 EFI_STATUS Status;
318 CHAR16 *TimeStringCopy;
319 CHAR16 *Walker1;
320 CHAR16 *Walker2;
321
322 if ((TimeString != NULL) && !InternalIsTimeLikeString (TimeString, L':', 1, 2, FALSE)) {
323 return (SHELL_INVALID_PARAMETER);
324 }
325
326 if ((Daylight != 0xFF) && ((Daylight & (EFI_TIME_IN_DAYLIGHT|EFI_TIME_ADJUST_DAYLIGHT)) != Daylight)) {
327 return (SHELL_INVALID_PARAMETER);
328 }
329
330 Status = gRT->GetTime (&TheTime, NULL);
331 if (EFI_ERROR (Status)) {
332 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_UEFI_FUNC_WARN), gShellLevel2HiiHandle, L"time", L"gRT->GetTime", Status);
333 return (SHELL_DEVICE_ERROR);
334 }
335
336 if (TimeString != NULL) {
337 TimeStringCopy = NULL;
338 TimeStringCopy = StrnCatGrow (&TimeStringCopy, NULL, TimeString, 0);
339 Walker1 = TimeStringCopy;
340 TheTime.Hour = 0xFF;
341 TheTime.Minute = 0xFF;
342
343 Walker2 = Walker1 != NULL ? StrStr (Walker1, L":") : NULL;
344 if ((Walker2 != NULL) && (*Walker2 == L':')) {
345 *Walker2 = CHAR_NULL;
346 }
347
348 TheTime.Hour = (UINT8)ShellStrToUintn (Walker1);
349 if (Walker2 != NULL) {
350 Walker1 = Walker2 + 1;
351 }
352
353 Walker2 = Walker1 != NULL ? StrStr (Walker1, L":") : NULL;
354 if ((Walker2 != NULL) && (*Walker2 == L':')) {
355 *Walker2 = CHAR_NULL;
356 TheTime.Second = (UINT8)0;
357 } else if (Walker2 == NULL) {
358 TheTime.Second = (UINT8)0;
359 }
360
361 if ((Walker1 != NULL) && (Walker1[0] != CHAR_NULL)) {
362 TheTime.Minute = (UINT8)ShellStrToUintn (Walker1);
363 if (Walker2 != NULL) {
364 Walker1 = Walker2 + 1;
365 if ((Walker1 != NULL) && (Walker1[0] != CHAR_NULL)) {
366 TheTime.Second = (UINT8)ShellStrToUintn (Walker1);
367 }
368 }
369 }
370
371 SHELL_FREE_NON_NULL (TimeStringCopy);
372 }
373
374 if ((Tz >= -1440) && (Tz <= 1440)) {
375 //
376 // EFI_TIME TimeZone is stored to meet the following calculation (see UEFI Spec):
377 // Localtime = UTC - TimeZone
378 // This means the sign must be changed for the user provided Tz.
379 // EX: User wants to set TimeZone to Pacific Standard Time, so runs
380 // time -tz -480 # set to UTC-08:00
381 // To meet the calculation, the sign must be changed.
382 //
383 TheTime.TimeZone = -Tz;
384 } else if (Tz == EFI_UNSPECIFIED_TIMEZONE) {
385 TheTime.TimeZone = Tz;
386 }
387
388 if (Daylight != 0xFF) {
389 TheTime.Daylight = Daylight;
390 }
391
392 Status = gRT->SetTime (&TheTime);
393
394 if (!EFI_ERROR (Status)) {
395 return (SHELL_SUCCESS);
396 }
397
398 return (SHELL_INVALID_PARAMETER);
399 }
400
401 /**
402 Function for 'time' command.
403
404 @param[in] ImageHandle Handle to the Image (NULL if Internal).
405 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
406 **/
407 SHELL_STATUS
408 EFIAPI
409 ShellCommandRunTime (
410 IN EFI_HANDLE ImageHandle,
411 IN EFI_SYSTEM_TABLE *SystemTable
412 )
413 {
414 EFI_STATUS Status;
415 LIST_ENTRY *Package;
416 EFI_TIME TheTime;
417 CHAR16 *ProblemParam;
418 SHELL_STATUS ShellStatus;
419 INT16 Tz;
420 UINT8 Daylight;
421 CONST CHAR16 *TempLocation;
422 UINTN TzMinutes;
423
424 //
425 // Initialize variables
426 //
427 ShellStatus = SHELL_SUCCESS;
428 ProblemParam = NULL;
429
430 //
431 // initialize the shell lib (we must be in non-auto-init...)
432 //
433 Status = ShellInitialize ();
434 ASSERT_EFI_ERROR (Status);
435
436 //
437 // parse the command line
438 //
439 if (PcdGet8 (PcdShellSupportLevel) == 2) {
440 Status = ShellCommandLineParseEx (TimeParamList2, &Package, &ProblemParam, TRUE, TRUE);
441 } else {
442 ASSERT (PcdGet8 (PcdShellSupportLevel) == 3);
443 Status = ShellCommandLineParseEx (TimeParamList3, &Package, &ProblemParam, TRUE, TRUE);
444 }
445
446 if (EFI_ERROR (Status)) {
447 if ((Status == EFI_VOLUME_CORRUPTED) && (ProblemParam != NULL)) {
448 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellLevel2HiiHandle, L"time", ProblemParam);
449 FreePool (ProblemParam);
450 ShellStatus = SHELL_INVALID_PARAMETER;
451 } else {
452 ASSERT (FALSE);
453 }
454 } else {
455 //
456 // check for "-?"
457 //
458 Status = gRT->GetTime (&TheTime, NULL);
459 if (EFI_ERROR (Status)) {
460 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_UEFI_FUNC_WARN), gShellLevel2HiiHandle, L"time", L"gRT->GetTime", Status);
461 return (SHELL_DEVICE_ERROR);
462 }
463
464 if (ShellCommandLineGetFlag (Package, L"-?")) {
465 ASSERT (FALSE);
466 } else if (ShellCommandLineGetRawValue (Package, 2) != NULL) {
467 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellLevel2HiiHandle, L"time");
468 ShellStatus = SHELL_INVALID_PARAMETER;
469 } else {
470 //
471 // If there are no parameters, then print the current time
472 //
473 if ( (ShellCommandLineGetRawValue (Package, 1) == NULL)
474 && !ShellCommandLineGetFlag (Package, L"-d")
475 && !ShellCommandLineGetFlag (Package, L"-tz"))
476 {
477 //
478 // ShellPrintEx the current time
479 //
480 if (TheTime.TimeZone == EFI_UNSPECIFIED_TIMEZONE) {
481 TzMinutes = 0;
482 } else {
483 TzMinutes = (ABS (TheTime.TimeZone)) % 60;
484 }
485
486 if (TheTime.TimeZone != EFI_UNSPECIFIED_TIMEZONE) {
487 ShellPrintHiiEx (
488 -1,
489 -1,
490 NULL,
491 STRING_TOKEN (STR_TIME_FORMAT),
492 gShellLevel2HiiHandle,
493 TheTime.Hour,
494 TheTime.Minute,
495 TheTime.Second,
496 (TheTime.TimeZone > 0 ? L"-" : L"+"),
497 ((ABS (TheTime.TimeZone)) / 60),
498 TzMinutes
499 );
500 } else {
501 ShellPrintHiiEx (
502 -1,
503 -1,
504 NULL,
505 STRING_TOKEN (STR_TIME_FORMAT_LOCAL),
506 gShellLevel2HiiHandle,
507 TheTime.Hour,
508 TheTime.Minute,
509 TheTime.Second
510 );
511 }
512
513 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_CRLF), gShellLevel2HiiHandle);
514 } else if (ShellCommandLineGetFlag (Package, L"-d") && (ShellCommandLineGetValue (Package, L"-d") == NULL)) {
515 if (TheTime.TimeZone == EFI_UNSPECIFIED_TIMEZONE) {
516 ShellPrintHiiEx (
517 -1,
518 -1,
519 NULL,
520 STRING_TOKEN (STR_TIME_FORMAT_LOCAL),
521 gShellLevel2HiiHandle,
522 TheTime.Hour,
523 TheTime.Minute,
524 TheTime.Second
525 );
526 } else {
527 TzMinutes = (ABS (TheTime.TimeZone)) % 60;
528 ShellPrintHiiEx (
529 -1,
530 -1,
531 NULL,
532 STRING_TOKEN (STR_TIME_FORMAT),
533 gShellLevel2HiiHandle,
534 TheTime.Hour,
535 TheTime.Minute,
536 TheTime.Second,
537 (TheTime.TimeZone > 0 ? L"-" : L"+"),
538 ((ABS (TheTime.TimeZone)) / 60),
539 TzMinutes
540 );
541 }
542
543 switch (TheTime.Daylight) {
544 case 0:
545 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_TIME_DST0), gShellLevel2HiiHandle);
546 break;
547 case EFI_TIME_ADJUST_DAYLIGHT:
548 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_TIME_DST1), gShellLevel2HiiHandle);
549 break;
550 case EFI_TIME_IN_DAYLIGHT:
551 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_TIME_DST2), gShellLevel2HiiHandle);
552 break;
553 case EFI_TIME_IN_DAYLIGHT|EFI_TIME_ADJUST_DAYLIGHT:
554 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_TIME_DST3), gShellLevel2HiiHandle);
555 break;
556 default:
557 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_UEFI_FUNC_ERROR), gShellLevel2HiiHandle, L"time", L"gRT->GetTime", L"TheTime.Daylight", TheTime.Daylight);
558 }
559 } else {
560 if (PcdGet8 (PcdShellSupportLevel) == 2) {
561 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellLevel2HiiHandle, L"time");
562 ShellStatus = SHELL_INVALID_PARAMETER;
563 } else {
564 //
565 // perform level 3 operation here.
566 //
567 if ((TempLocation = ShellCommandLineGetValue (Package, L"-tz")) != NULL) {
568 if (gUnicodeCollation->StriColl (gUnicodeCollation, (CHAR16 *)TempLocation, L"_local") == 0) {
569 Tz = EFI_UNSPECIFIED_TIMEZONE;
570 } else if (TempLocation[0] == L'-') {
571 Tz = (INT16)ShellStrToUintn (++TempLocation);
572 //
573 // When the argument of "time [-tz tz]" is not numeric, ShellStrToUintn() returns "-1".
574 // Here we can detect the argument error by checking the return of ShellStrToUintn().
575 //
576 if (Tz == -1) {
577 Tz = 1441; // make it to be out of bounds value
578 } else {
579 Tz *= (-1); // sign convert
580 }
581 } else {
582 if (TempLocation[0] == L'+') {
583 Tz = (INT16)ShellStrToUintn (++TempLocation);
584 } else {
585 Tz = (INT16)ShellStrToUintn (TempLocation);
586 }
587
588 //
589 // Detect the return of ShellStrToUintn() to make sure the argument is valid.
590 //
591 if (Tz == -1) {
592 Tz = 1441; // make it to be out of bounds value
593 }
594 }
595
596 if (!((Tz >= -1440) && (Tz <= 1440)) && (Tz != EFI_UNSPECIFIED_TIMEZONE)) {
597 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM_VAL), gShellLevel2HiiHandle, L"time", TempLocation, L"-tz");
598 ShellStatus = SHELL_INVALID_PARAMETER;
599 }
600 } else {
601 //
602 // intentionally out of bounds value will prevent changing it...
603 //
604 Tz = 1441;
605 }
606
607 TempLocation = ShellCommandLineGetValue (Package, L"-d");
608 if (TempLocation != NULL) {
609 Daylight = (UINT8)ShellStrToUintn (TempLocation);
610 //
611 // The argument of "time [-d dl]" is unsigned, if the first character is '-',
612 // the argument is incorrect. That's because ShellStrToUintn() will skip past
613 // any '-' sign and convert what's next, forgetting the sign is here.
614 //
615 if (TempLocation[0] == '-') {
616 Daylight = 0xff; // make it invalid = will not use
617 }
618
619 if ((Daylight != 0) && (Daylight != 1) && (Daylight != 3)) {
620 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM_VAL), gShellLevel2HiiHandle, L"time", TempLocation, L"-d");
621 ShellStatus = SHELL_INVALID_PARAMETER;
622 }
623 } else {
624 //
625 // invalid = will not use
626 //
627 Daylight = 0xFF;
628 }
629
630 if (ShellStatus == SHELL_SUCCESS) {
631 ShellStatus = CheckAndSetTime (ShellCommandLineGetRawValue (Package, 1), Tz, Daylight);
632 if (ShellStatus != SHELL_SUCCESS) {
633 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV), gShellLevel2HiiHandle, L"time", ShellCommandLineGetRawValue (Package, 1));
634 ShellStatus = SHELL_INVALID_PARAMETER;
635 }
636 }
637 }
638 }
639 }
640 }
641
642 //
643 // free the command line package
644 //
645 ShellCommandLineFreeVarList (Package);
646
647 //
648 // return the status
649 //
650 return (ShellStatus);
651 }
652
653 typedef struct {
654 INT16 TimeZone;
655 EFI_STRING_ID StringId;
656 } TIME_ZONE_ITEM;
657
658 STATIC CONST SHELL_PARAM_ITEM TimeZoneParamList2[] = {
659 { L"-l", TypeFlag },
660 { L"-f", TypeFlag },
661 { NULL, TypeMax }
662 };
663 STATIC CONST SHELL_PARAM_ITEM TimeZoneParamList3[] = {
664 { L"-l", TypeFlag },
665 { L"-f", TypeFlag },
666 { L"-s", TypeTimeValue },
667 { NULL, TypeMax }
668 };
669
670 STATIC CONST TIME_ZONE_ITEM TimeZoneList[] = {
671 { 720, STRING_TOKEN (STR_TIMEZONE_M12) },
672 { 660, STRING_TOKEN (STR_TIMEZONE_M11) },
673 { 600, STRING_TOKEN (STR_TIMEZONE_M10) },
674 { 540, STRING_TOKEN (STR_TIMEZONE_M9) },
675 { 480, STRING_TOKEN (STR_TIMEZONE_M8) },
676 { 420, STRING_TOKEN (STR_TIMEZONE_M7) },
677 { 360, STRING_TOKEN (STR_TIMEZONE_M6) },
678 { 300, STRING_TOKEN (STR_TIMEZONE_M5) },
679 { 270, STRING_TOKEN (STR_TIMEZONE_M430) },
680 { 240, STRING_TOKEN (STR_TIMEZONE_M4) },
681 { 210, STRING_TOKEN (STR_TIMEZONE_M330) },
682 { 180, STRING_TOKEN (STR_TIMEZONE_M3) },
683 { 120, STRING_TOKEN (STR_TIMEZONE_M2) },
684 { 60, STRING_TOKEN (STR_TIMEZONE_M1) },
685 { 0, STRING_TOKEN (STR_TIMEZONE_0) },
686 { -60, STRING_TOKEN (STR_TIMEZONE_P1) },
687 { -120, STRING_TOKEN (STR_TIMEZONE_P2) },
688 { -180, STRING_TOKEN (STR_TIMEZONE_P3) },
689 { -210, STRING_TOKEN (STR_TIMEZONE_P330) },
690 { -240, STRING_TOKEN (STR_TIMEZONE_P4) },
691 { -270, STRING_TOKEN (STR_TIMEZONE_P430) },
692 { -300, STRING_TOKEN (STR_TIMEZONE_P5) },
693 { -330, STRING_TOKEN (STR_TIMEZONE_P530) },
694 { -345, STRING_TOKEN (STR_TIMEZONE_P545) },
695 { -360, STRING_TOKEN (STR_TIMEZONE_P6) },
696 { -390, STRING_TOKEN (STR_TIMEZONE_P630) },
697 { -420, STRING_TOKEN (STR_TIMEZONE_P7) },
698 { -480, STRING_TOKEN (STR_TIMEZONE_P8) },
699 { -540, STRING_TOKEN (STR_TIMEZONE_P9) },
700 { -570, STRING_TOKEN (STR_TIMEZONE_P930) },
701 { -600, STRING_TOKEN (STR_TIMEZONE_P10) },
702 { -660, STRING_TOKEN (STR_TIMEZONE_P11) },
703 { -720, STRING_TOKEN (STR_TIMEZONE_P12) },
704 { -780, STRING_TOKEN (STR_TIMEZONE_P13) },
705 { -840, STRING_TOKEN (STR_TIMEZONE_P14) },
706 { EFI_UNSPECIFIED_TIMEZONE, STRING_TOKEN (STR_TIMEZONE_LOCAL) }
707 };
708
709 /**
710 Verify that the TimeZoneString is valid and if so set that as the current
711 timezone.
712
713 @param[in] TimeZoneString The pointer to a string representation of the timezone.
714
715 @retval SHELL_INVALID_PARAMETER TimeZoneString was NULL.
716 @retval SHELL_INVALID_PARAMETER TimeZoneString was mis-formatted.
717 @retval SHELL_SUCCESS The operation was successful.
718 **/
719 SHELL_STATUS
720 CheckAndSetTimeZone (
721 IN CONST CHAR16 *TimeZoneString
722 )
723 {
724 EFI_TIME TheTime;
725 EFI_STATUS Status;
726 CHAR16 *TimeZoneCopy;
727 CHAR16 *Walker;
728 CHAR16 *Walker2;
729 UINTN LoopVar;
730
731 if (TimeZoneString == NULL) {
732 return (SHELL_INVALID_PARAMETER);
733 }
734
735 if (gUnicodeCollation->StriColl (gUnicodeCollation, (CHAR16 *)TimeZoneString, L"_local") == 0) {
736 Status = gRT->GetTime (&TheTime, NULL);
737 if (EFI_ERROR (Status)) {
738 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_UEFI_FUNC_WARN), gShellLevel2HiiHandle, L"gRT->GetTime", Status);
739 return (SHELL_DEVICE_ERROR);
740 }
741
742 TheTime.TimeZone = EFI_UNSPECIFIED_TIMEZONE;
743 Status = gRT->SetTime (&TheTime);
744 if (!EFI_ERROR (Status)) {
745 return (SHELL_SUCCESS);
746 }
747
748 return (SHELL_INVALID_PARAMETER);
749 }
750
751 if ((TimeZoneString != NULL) && !InternalIsTimeLikeString (TimeZoneString, L':', 1, 1, TRUE)) {
752 return (SHELL_INVALID_PARAMETER);
753 }
754
755 Status = gRT->GetTime (&TheTime, NULL);
756 if (EFI_ERROR (Status)) {
757 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_UEFI_FUNC_WARN), gShellLevel2HiiHandle, L"timezone", L"gRT->GetTime", Status);
758 return (SHELL_DEVICE_ERROR);
759 }
760
761 TimeZoneCopy = NULL;
762 TimeZoneCopy = StrnCatGrow (&TimeZoneCopy, NULL, TimeZoneString, 0);
763 if (TimeZoneCopy == NULL) {
764 return (SHELL_OUT_OF_RESOURCES);
765 }
766
767 Walker = TimeZoneCopy;
768 Walker2 = StrStr (Walker, L":");
769 if ((Walker2 != NULL) && (*Walker2 == L':')) {
770 *Walker2 = CHAR_NULL;
771 }
772
773 if (*Walker == L'-') {
774 TheTime.TimeZone = (INT16)((ShellStrToUintn (++Walker)) * 60);
775 } else {
776 TheTime.TimeZone = (INT16)((INT16)(ShellStrToUintn (Walker)) * -60);
777 }
778
779 if (Walker2 != NULL) {
780 Walker = Walker2 + 1;
781 }
782
783 if ((Walker != NULL) && (Walker[0] != CHAR_NULL)) {
784 if (TheTime.TimeZone < 0) {
785 TheTime.TimeZone = (INT16)(TheTime.TimeZone - (UINT8)ShellStrToUintn (Walker));
786 } else {
787 TheTime.TimeZone = (INT16)(TheTime.TimeZone + (UINT8)ShellStrToUintn (Walker));
788 }
789 }
790
791 Status = EFI_INVALID_PARAMETER;
792
793 for ( LoopVar = 0
794 ; LoopVar < sizeof (TimeZoneList) / sizeof (TimeZoneList[0])
795 ; LoopVar++
796 )
797 {
798 if (TheTime.TimeZone == TimeZoneList[LoopVar].TimeZone) {
799 Status = gRT->SetTime (&TheTime);
800 break;
801 }
802 }
803
804 FreePool (TimeZoneCopy);
805
806 if (!EFI_ERROR (Status)) {
807 return (SHELL_SUCCESS);
808 }
809
810 return (SHELL_INVALID_PARAMETER);
811 }
812
813 /**
814 Function for 'timezone' command.
815
816 @param[in] ImageHandle Handle to the Image (NULL if Internal).
817 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
818 **/
819 SHELL_STATUS
820 EFIAPI
821 ShellCommandRunTimeZone (
822 IN EFI_HANDLE ImageHandle,
823 IN EFI_SYSTEM_TABLE *SystemTable
824 )
825 {
826 //
827 // non interactive
828 //
829 EFI_STATUS Status;
830 LIST_ENTRY *Package;
831 CHAR16 *ProblemParam;
832 SHELL_STATUS ShellStatus;
833 UINT8 LoopVar;
834 EFI_TIME TheTime;
835 BOOLEAN Found;
836 UINTN TzMinutes;
837
838 ShellStatus = SHELL_SUCCESS;
839 ProblemParam = NULL;
840
841 //
842 // initialize the shell lib (we must be in non-auto-init...)
843 //
844 Status = ShellInitialize ();
845 ASSERT_EFI_ERROR (Status);
846
847 //
848 // parse the command line
849 //
850 if (PcdGet8 (PcdShellSupportLevel) == 2) {
851 Status = ShellCommandLineParse (TimeZoneParamList2, &Package, &ProblemParam, TRUE);
852 } else {
853 ASSERT (PcdGet8 (PcdShellSupportLevel) == 3);
854 Status = ShellCommandLineParseEx (TimeZoneParamList3, &Package, &ProblemParam, TRUE, TRUE);
855 }
856
857 if (EFI_ERROR (Status)) {
858 if ((Status == EFI_VOLUME_CORRUPTED) && (ProblemParam != NULL)) {
859 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellLevel2HiiHandle, L"timezone", ProblemParam);
860 FreePool (ProblemParam);
861 ShellStatus = SHELL_INVALID_PARAMETER;
862 } else {
863 ASSERT (FALSE);
864 }
865 } else {
866 //
867 // check for "-?"
868 //
869 if (ShellCommandLineGetCount (Package) > 1) {
870 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellLevel2HiiHandle, L"timezone");
871 ShellStatus = SHELL_INVALID_PARAMETER;
872 } else if (ShellCommandLineGetFlag (Package, L"-?")) {
873 ASSERT (FALSE);
874 } else if (ShellCommandLineGetFlag (Package, L"-s")) {
875 if ((ShellCommandLineGetFlag (Package, L"-l")) || (ShellCommandLineGetFlag (Package, L"-f"))) {
876 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV), gShellLevel2HiiHandle, L"timezone", L"-l or -f");
877 ShellStatus = SHELL_INVALID_PARAMETER;
878 } else {
879 ASSERT (PcdGet8 (PcdShellSupportLevel) == 3);
880 if (ShellCommandLineGetValue (Package, L"-s") == NULL) {
881 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_VALUE), gShellLevel2HiiHandle, L"timezone", L"-s");
882 ShellStatus = SHELL_INVALID_PARAMETER;
883 } else {
884 //
885 // Set the time zone
886 //
887 ShellStatus = CheckAndSetTimeZone (ShellCommandLineGetValue (Package, L"-s"));
888 if (ShellStatus != SHELL_SUCCESS) {
889 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV), gShellLevel2HiiHandle, L"timezone", ShellCommandLineGetValue (Package, L"-s"));
890 ShellStatus = SHELL_INVALID_PARAMETER;
891 }
892 }
893 }
894 } else if (ShellCommandLineGetFlag (Package, L"-l")) {
895 //
896 // Print a list of all time zones
897 //
898 for ( LoopVar = 0
899 ; LoopVar < sizeof (TimeZoneList) / sizeof (TimeZoneList[0])
900 ; LoopVar++
901 )
902 {
903 ShellPrintHiiEx (-1, -1, NULL, TimeZoneList[LoopVar].StringId, gShellLevel2HiiHandle);
904 }
905 } else {
906 //
907 // Get Current Time Zone Info
908 //
909 Status = gRT->GetTime (&TheTime, NULL);
910 if (EFI_ERROR (Status)) {
911 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_UEFI_FUNC_WARN), gShellLevel2HiiHandle, L"timezone", L"gRT->GetTime", Status);
912 return (SHELL_DEVICE_ERROR);
913 }
914
915 if (TheTime.TimeZone != EFI_UNSPECIFIED_TIMEZONE) {
916 Found = FALSE;
917 for ( LoopVar = 0
918 ; LoopVar < sizeof (TimeZoneList) / sizeof (TimeZoneList[0])
919 ; LoopVar++
920 )
921 {
922 if (TheTime.TimeZone == TimeZoneList[LoopVar].TimeZone) {
923 if (ShellCommandLineGetFlag (Package, L"-f")) {
924 //
925 // Print all info about current time zone
926 //
927 ShellPrintHiiEx (-1, -1, NULL, TimeZoneList[LoopVar].StringId, gShellLevel2HiiHandle);
928 } else {
929 //
930 // Print basic info only
931 //
932 TzMinutes = (ABS (TheTime.TimeZone)) % 60;
933
934 ShellPrintHiiEx (
935 -1,
936 -1,
937 NULL,
938 STRING_TOKEN (STR_TIMEZONE_SIMPLE),
939 gShellLevel2HiiHandle,
940 (TheTime.TimeZone > 0 ? L"-" : L"+"),
941 (ABS (TheTime.TimeZone)) / 60,
942 TzMinutes
943 );
944 }
945
946 Found = TRUE;
947 break;
948 }
949 }
950
951 if (!Found) {
952 //
953 // Print basic info only
954 //
955 TzMinutes = (ABS (TheTime.TimeZone)) % 60;
956
957 ShellPrintHiiEx (
958 -1,
959 -1,
960 NULL,
961 STRING_TOKEN (STR_TIMEZONE_SIMPLE),
962 gShellLevel2HiiHandle,
963 (TheTime.TimeZone > 0 ? L"-" : L"+"),
964 (ABS (TheTime.TimeZone)) / 60,
965 TzMinutes
966 );
967
968 if (ShellCommandLineGetFlag (Package, L"-f")) {
969 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_TIMEZONE_NI), gShellLevel2HiiHandle);
970 }
971 }
972 } else {
973 //
974 // TimeZone was EFI_UNSPECIFIED_TIMEZONE (local) from GetTime()
975 //
976 if (ShellCommandLineGetFlag (Package, L"-f")) {
977 for ( LoopVar = 0
978 ; LoopVar < ARRAY_SIZE (TimeZoneList)
979 ; LoopVar++
980 )
981 {
982 if (TheTime.TimeZone == TimeZoneList[LoopVar].TimeZone) {
983 //
984 // Print all info about current time zone
985 //
986 ShellPrintHiiEx (-1, -1, NULL, TimeZoneList[LoopVar].StringId, gShellLevel2HiiHandle);
987 break;
988 }
989 }
990 } else {
991 //
992 // Print basic info only
993 //
994 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_TIMEZONE_SIMPLE_LOCAL), gShellLevel2HiiHandle);
995 }
996 }
997 }
998 }
999
1000 //
1001 // free the command line package
1002 //
1003 ShellCommandLineFreeVarList (Package);
1004
1005 return (ShellStatus);
1006 }