]> git.proxmox.com Git - mirror_edk2.git/blob - ShellPkg/Library/UefiShellLevel2CommandsLib/TimeDate.c
ShellPkg: Fixed build error 'variable set but not used'
[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 Copyright (c) 2009 - 2011, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 #include "UefiShellLevel2CommandsLib.h"
16
17 /**
18 Determine if String is a valid representation for a time or date.
19
20 @param[in] String The pointer to the string to test.
21 @param[in] Char The delimeter character.
22 @param[in] Min The minimum value allowed.
23 @param[in] Max The maximum value allowed.
24 @param[in] MinusOk Whether negative numbers are permitted.
25
26 @retval TRUE String is a valid representation.
27 @retval FALSE String is invalid.
28 **/
29 BOOLEAN
30 EFIAPI
31 InternalIsTimeLikeString (
32 IN CONST CHAR16 *String,
33 IN CONST CHAR16 Char,
34 IN CONST UINTN Min,
35 IN CONST UINTN Max,
36 IN CONST BOOLEAN MinusOk
37 )
38 {
39 UINTN Count;
40 Count = 0;
41
42 if (MinusOk) {
43 //
44 // A single minus is ok.
45 //
46 if (*String == L'-') {
47 String++;
48 }
49 }
50
51 //
52 // the first char must be numeric.
53 //
54 if (!ShellIsDecimalDigitCharacter(*String)) {
55 return (FALSE);
56 }
57 //
58 // loop through the characters and use the lib function
59 //
60 for ( ; String != NULL && *String != CHAR_NULL ; String++){
61 if (*String == Char) {
62 Count++;
63 if (Count > Max) {
64 return (FALSE);
65 }
66 continue;
67 }
68 if (!ShellIsDecimalDigitCharacter(*String)) {
69 return (FALSE);
70 }
71 }
72 if (Count < Min) {
73 return (FALSE);
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 EFIAPI
90 CheckAndSetDate (
91 IN CONST CHAR16 *DateString
92 )
93 {
94 EFI_TIME TheTime;
95 EFI_STATUS Status;
96 CHAR16 *DateStringCopy;
97 CHAR16 *Walker;
98 CHAR16 *Walker1;
99
100 if (!InternalIsTimeLikeString(DateString, L'/', 2, 2, FALSE)) {
101 return (SHELL_INVALID_PARAMETER);
102 }
103
104 Status = gRT->GetTime(&TheTime, NULL);
105 if (EFI_ERROR(Status)) {
106 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_UEFI_FUNC_WARN), gShellLevel2HiiHandle, L"gRT->GetTime", Status);
107 return (SHELL_DEVICE_ERROR);
108 }
109
110 DateStringCopy = NULL;
111 DateStringCopy = StrnCatGrow(&DateStringCopy, NULL, DateString, 0);
112 if (DateStringCopy == NULL) {
113 return (SHELL_OUT_OF_RESOURCES);
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 Walker1 = Walker!=NULL?StrStr(Walker, L"/"):NULL;
131 if (Walker1 != NULL && *Walker1 == L'/') {
132 *Walker1 = CHAR_NULL;
133 }
134 if (Walker != NULL && Walker[0] != CHAR_NULL) {
135 TheTime.Day = (UINT8)ShellStrToUintn (Walker);
136 if (Walker1 != NULL) {
137 Walker = Walker1 + 1;
138 }
139 Walker1 = Walker!=NULL?StrStr(Walker, L"/"):NULL;
140 if (Walker1 != NULL && *Walker1 == L'/') {
141 *Walker1 = CHAR_NULL;
142 }
143 if (Walker != NULL && Walker[0] != CHAR_NULL) {
144 TheTime.Year = (UINT16)ShellStrToUintn (Walker);
145 }
146 }
147
148 if (TheTime.Year < 100) {
149 if (TheTime.Year >= 98) {
150 TheTime.Year = (UINT16)(1900 + TheTime.Year);
151 } else {
152 TheTime.Year = (UINT16)(2000 + TheTime.Year);
153 }
154 }
155
156 Status = gRT->SetTime(&TheTime);
157
158 if (!EFI_ERROR(Status)){
159 return (SHELL_SUCCESS);
160 }
161 return (SHELL_INVALID_PARAMETER);
162 }
163
164 /**
165 Function for 'date' command.
166
167 @param[in] ImageHandle Handle to the Image (NULL if Internal).
168 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
169 **/
170 SHELL_STATUS
171 EFIAPI
172 ShellCommandRunDate (
173 IN EFI_HANDLE ImageHandle,
174 IN EFI_SYSTEM_TABLE *SystemTable
175 )
176 {
177 EFI_STATUS Status;
178 LIST_ENTRY *Package;
179 EFI_TIME TheTime;
180 CHAR16 *ProblemParam;
181 SHELL_STATUS ShellStatus;
182 CONST CHAR16 *Param1;
183
184 ShellStatus = SHELL_SUCCESS;
185 ProblemParam = NULL;
186
187 //
188 // initialize the shell lib (we must be in non-auto-init...)
189 //
190 Status = ShellInitialize();
191 ASSERT_EFI_ERROR(Status);
192
193 //
194 // parse the command line
195 //
196 Status = ShellCommandLineParse (SfoParamList, &Package, &ProblemParam, TRUE);
197 if (EFI_ERROR(Status)) {
198 if (Status == EFI_VOLUME_CORRUPTED && ProblemParam != NULL) {
199 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellLevel2HiiHandle, ProblemParam);
200 FreePool(ProblemParam);
201 ShellStatus = SHELL_INVALID_PARAMETER;
202 } else {
203 ASSERT(FALSE);
204 }
205 } else {
206 //
207 // check for "-?"
208 //
209 if (ShellCommandLineGetFlag(Package, L"-?")) {
210 ASSERT(FALSE);
211 } else if (ShellCommandLineGetRawValue(Package, 2) != NULL) {
212 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellLevel2HiiHandle);
213 ShellStatus = SHELL_INVALID_PARAMETER;
214 } else {
215 //
216 // If there are 0 value parameters, then print the current date
217 // else If there are any value paramerers, then print error
218 //
219 if (ShellCommandLineGetRawValue(Package, 1) == NULL) {
220 //
221 // get the current date
222 //
223 Status = gRT->GetTime(&TheTime, NULL);
224 ASSERT_EFI_ERROR(Status);
225
226 //
227 // ShellPrintEx the date in SFO or regular format
228 //
229 if (ShellCommandLineGetFlag(Package, L"-sfo")) {
230 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_DATE_SFO_FORMAT), gShellLevel2HiiHandle, TheTime.Month, TheTime.Day, TheTime.Year);
231 } else {
232 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_DATE_FORMAT), gShellLevel2HiiHandle, TheTime.Month, TheTime.Day, TheTime.Year);
233 }
234 } else {
235 if (PcdGet8(PcdShellSupportLevel) == 2) {
236 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellLevel2HiiHandle);
237 ShellStatus = SHELL_INVALID_PARAMETER;
238 } else {
239 //
240 // perform level 3 operation here.
241 //
242 Param1 = ShellCommandLineGetRawValue(Package, 1);
243 if (Param1 == NULL) {
244 ShellStatus = SHELL_INVALID_PARAMETER;
245 } else {
246 ShellStatus = CheckAndSetDate(Param1);
247 }
248 if (ShellStatus != SHELL_SUCCESS) {
249 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellLevel2HiiHandle, Param1);
250 ShellStatus = SHELL_INVALID_PARAMETER;
251 }
252 }
253 }
254 }
255 }
256 //
257 // free the command line package
258 //
259 ShellCommandLineFreeVarList (Package);
260
261 //
262 // return the status
263 //
264 return (ShellStatus);
265 }
266
267 //
268 // Note "-tz" is invalid for this (non-interactive) version of 'time'.
269 //
270 STATIC CONST SHELL_PARAM_ITEM TimeParamList2[] = {
271 {L"-d", TypeValue},
272 {NULL, TypeMax}
273 };
274
275 STATIC CONST SHELL_PARAM_ITEM TimeParamList3[] = {
276 {L"-d", TypeValue},
277 {L"-tz", TypeValue},
278 {NULL, TypeMax}
279 };
280
281 /**
282 Verify that the TimeString is valid and if so set that as the current
283 time.
284
285 @param[in] TimeString The pointer to a string representation of the time.
286 @param[in] Tz The value to set for TimeZone.
287 @param[in] Daylight The value to set for Daylight.
288
289 @retval SHELL_INVALID_PARAMETER TimeString was NULL.
290 @retval SHELL_INVALID_PARAMETER TimeString was mis-formatted.
291 @retval SHELL_SUCCESS The operation was successful.
292 **/
293 SHELL_STATUS
294 EFIAPI
295 CheckAndSetTime (
296 IN CONST CHAR16 *TimeString,
297 IN CONST INT16 Tz,
298 IN CONST UINT8 Daylight
299 )
300 {
301 EFI_TIME TheTime;
302 EFI_STATUS Status;
303 CHAR16 *TimeStringCopy;
304 CHAR16 *Walker1;
305 CHAR16 *Walker2;
306
307 if (TimeString != NULL && !InternalIsTimeLikeString(TimeString, L':', 1, 2, FALSE)) {
308 return (SHELL_INVALID_PARAMETER);
309 }
310 if (Daylight != 0xFF &&((Daylight & (EFI_TIME_IN_DAYLIGHT|EFI_TIME_ADJUST_DAYLIGHT)) != Daylight)) {
311 return (SHELL_INVALID_PARAMETER);
312 }
313
314 Status = gRT->GetTime(&TheTime, NULL);
315 ASSERT_EFI_ERROR(Status);
316
317 if (TimeString != NULL) {
318 TimeStringCopy = NULL;
319 TimeStringCopy = StrnCatGrow(&TimeStringCopy, NULL, TimeString, 0);
320 Walker1 = TimeStringCopy;
321 TheTime.Hour = 0xFF;
322 TheTime.Minute = 0xFF;
323
324 Walker2 = Walker1!=NULL?StrStr(Walker1, L":"):NULL;
325 if (Walker2 != NULL && *Walker2 == L':') {
326 *Walker2 = CHAR_NULL;
327 }
328 TheTime.Hour = (UINT8)ShellStrToUintn (Walker1);
329 if (Walker2 != NULL) {
330 Walker1 = Walker2 + 1;
331 }
332 Walker2 = Walker1!=NULL?StrStr(Walker1, L":"):NULL;
333 if (Walker2 != NULL && *Walker2 == L':') {
334 *Walker2 = CHAR_NULL;
335 }
336 if (Walker1 != NULL && Walker1[0] != CHAR_NULL) {
337 TheTime.Minute = (UINT8)ShellStrToUintn (Walker1);
338 if (Walker2 != NULL) {
339 Walker1 = Walker2 + 1;
340 }
341 if (Walker1 != NULL && Walker1[0] != CHAR_NULL) {
342 TheTime.Second = (UINT8)ShellStrToUintn (Walker1);
343 }
344 }
345 SHELL_FREE_NON_NULL(TimeStringCopy);
346 }
347
348
349 if ((Tz >= -1440 && Tz <= 1440)||(Tz == 0x7FF)) {
350 TheTime.TimeZone = Tz;
351 }
352
353 if (Daylight != 0xFF) {
354 TheTime.Daylight = Daylight;
355 }
356
357 Status = gRT->SetTime(&TheTime);
358
359 if (!EFI_ERROR(Status)){
360 return (SHELL_SUCCESS);
361 }
362
363 return (SHELL_INVALID_PARAMETER);
364 }
365
366 /**
367 Function for 'time' command.
368
369 @param[in] ImageHandle Handle to the Image (NULL if Internal).
370 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
371 **/
372 SHELL_STATUS
373 EFIAPI
374 ShellCommandRunTime (
375 IN EFI_HANDLE ImageHandle,
376 IN EFI_SYSTEM_TABLE *SystemTable
377 )
378 {
379 EFI_STATUS Status;
380 LIST_ENTRY *Package;
381 EFI_TIME TheTime;
382 CHAR16 *ProblemParam;
383 SHELL_STATUS ShellStatus;
384 INT16 Tz;
385 UINT8 Daylight;
386 CONST CHAR16 *TempLocation;
387 UINTN TzMinutes;
388
389 //
390 // Initialize variables
391 //
392 ShellStatus = SHELL_SUCCESS;
393 ProblemParam = NULL;
394
395 //
396 // initialize the shell lib (we must be in non-auto-init...)
397 //
398 Status = ShellInitialize();
399 ASSERT_EFI_ERROR(Status);
400
401 //
402 // parse the command line
403 //
404 if (PcdGet8(PcdShellSupportLevel) == 2) {
405 Status = ShellCommandLineParseEx (TimeParamList2, &Package, &ProblemParam, TRUE, TRUE);
406 } else {
407 ASSERT(PcdGet8(PcdShellSupportLevel) == 3);
408 Status = ShellCommandLineParseEx (TimeParamList3, &Package, &ProblemParam, TRUE, TRUE);
409 }
410 if (EFI_ERROR(Status)) {
411 if (Status == EFI_VOLUME_CORRUPTED && ProblemParam != NULL) {
412 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellLevel2HiiHandle, ProblemParam);
413 FreePool(ProblemParam);
414 ShellStatus = SHELL_INVALID_PARAMETER;
415 } else {
416 ASSERT(FALSE);
417 }
418 } else {
419 //
420 // check for "-?"
421 //
422 Status = gRT->GetTime(&TheTime, NULL);
423 ASSERT_EFI_ERROR(Status);
424 if (ShellCommandLineGetFlag(Package, L"-?")) {
425 ASSERT(FALSE);
426 } else if (ShellCommandLineGetRawValue(Package, 2) != NULL) {
427 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellLevel2HiiHandle);
428 ShellStatus = SHELL_INVALID_PARAMETER;
429 } else {
430 //
431 // If there are no parameters, then print the current time
432 //
433 if (ShellCommandLineGetRawValue(Package, 1) == NULL
434 && !ShellCommandLineGetFlag(Package, L"-d")
435 && !ShellCommandLineGetFlag(Package, L"-tz")) {
436 //
437 // ShellPrintEx the current time
438 //
439 if (TheTime.TimeZone == EFI_UNSPECIFIED_TIMEZONE) {
440 TzMinutes = 0;
441 } else {
442 TzMinutes = (ABS(TheTime.TimeZone)) % 60;
443 }
444
445 ShellPrintHiiEx (
446 -1,
447 -1,
448 NULL,
449 STRING_TOKEN (STR_TIME_FORMAT),
450 gShellLevel2HiiHandle,
451 TheTime.Hour,
452 TheTime.Minute,
453 TheTime.Second,
454 TheTime.TimeZone==EFI_UNSPECIFIED_TIMEZONE?L" ":(TheTime.TimeZone > 0?L"-":L"+"),
455 TheTime.TimeZone==EFI_UNSPECIFIED_TIMEZONE?0:(ABS(TheTime.TimeZone)) / 60,
456 TzMinutes
457 );
458 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_CRLF), gShellLevel2HiiHandle);
459 } else if (ShellCommandLineGetFlag(Package, L"-d") && ShellCommandLineGetValue(Package, L"-d") == NULL) {
460 if (TheTime.TimeZone == EFI_UNSPECIFIED_TIMEZONE) {
461 TzMinutes = 0;
462 } else {
463 TzMinutes = (ABS(TheTime.TimeZone)) % 60;
464 }
465
466 ShellPrintHiiEx (
467 -1,
468 -1,
469 NULL,
470 STRING_TOKEN (STR_TIME_FORMAT),
471 gShellLevel2HiiHandle,
472 TheTime.Hour,
473 TheTime.Minute,
474 TheTime.Second,
475 TheTime.TimeZone==EFI_UNSPECIFIED_TIMEZONE?L" ":(TheTime.TimeZone > 0?L"-":L"+"),
476 TheTime.TimeZone==EFI_UNSPECIFIED_TIMEZONE?0:(ABS(TheTime.TimeZone)) / 60,
477 TzMinutes
478 );
479 switch (TheTime.Daylight) {
480 case 0:
481 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_TIME_DST0), gShellLevel2HiiHandle);
482 break;
483 case EFI_TIME_ADJUST_DAYLIGHT:
484 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_TIME_DST1), gShellLevel2HiiHandle);
485 break;
486 case EFI_TIME_IN_DAYLIGHT:
487 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_TIME_DST2), gShellLevel2HiiHandle);
488 break;
489 case EFI_TIME_IN_DAYLIGHT|EFI_TIME_ADJUST_DAYLIGHT:
490 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_TIME_DST3), gShellLevel2HiiHandle);
491 break;
492 default:
493 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_UEFI_FUNC_ERROR), gShellLevel2HiiHandle, L"gRT->GetTime", L"TheTime.Daylight", TheTime.Daylight);
494 }
495 } else {
496 if (PcdGet8(PcdShellSupportLevel) == 2) {
497 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellLevel2HiiHandle);
498 ShellStatus = SHELL_INVALID_PARAMETER;
499 } else {
500 //
501 // perform level 3 operation here.
502 //
503 if ((TempLocation = ShellCommandLineGetValue(Package, L"-tz")) != NULL) {
504 if (TempLocation[0] == L'-') {
505 Tz = (INT16)(0 - ShellStrToUintn(++TempLocation));
506 } else {
507 Tz = (INT16)ShellStrToUintn(TempLocation);
508 }
509 if (!(Tz >= -1440 && Tz <= 1440) && Tz != EFI_UNSPECIFIED_TIMEZONE) {
510 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM_VAL), gShellLevel2HiiHandle, L"-tz");
511 ShellStatus = SHELL_INVALID_PARAMETER;
512 }
513 } else {
514 //
515 // intentionally out of bounds value will prevent changing it...
516 //
517 Tz = 1441;
518 }
519 TempLocation = ShellCommandLineGetValue(Package, L"-d");
520 if (TempLocation != NULL) {
521 Daylight = (UINT8)ShellStrToUintn(TempLocation);
522 if (Daylight != 0 && Daylight != 1 && Daylight != 3) {
523 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM_VAL), gShellLevel2HiiHandle, L"-d");
524 ShellStatus = SHELL_INVALID_PARAMETER;
525 }
526 } else {
527 //
528 // invalid = will not use
529 //
530 Daylight = 0xFF;
531 }
532 if (ShellStatus == SHELL_SUCCESS) {
533 ShellStatus = CheckAndSetTime(ShellCommandLineGetRawValue(Package, 1), Tz, Daylight);
534 if (ShellStatus != SHELL_SUCCESS) {
535 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellLevel2HiiHandle, ShellCommandLineGetRawValue(Package, 1));
536 ShellStatus = SHELL_INVALID_PARAMETER;
537 }
538 }
539 }
540 }
541 }
542 }
543
544 //
545 // free the command line package
546 //
547 ShellCommandLineFreeVarList (Package);
548
549 //
550 // return the status
551 //
552 return (ShellStatus);
553 }
554
555 typedef struct {
556 INT16 TimeZone;
557 EFI_STRING_ID StringId;
558 } TIME_ZONE_ITEM;
559
560 STATIC CONST SHELL_PARAM_ITEM TimeZoneParamList2[] = {
561 {L"-l", TypeFlag},
562 {L"-f", TypeFlag},
563 {NULL, TypeMax}
564 };
565 STATIC CONST SHELL_PARAM_ITEM TimeZoneParamList3[] = {
566 {L"-l", TypeFlag},
567 {L"-f", TypeFlag},
568 {L"-s", TypeValue},
569 {NULL, TypeMax}
570 };
571
572 STATIC CONST TIME_ZONE_ITEM TimeZoneList[] = {
573 {720, STRING_TOKEN (STR_TIMEZONE_M12)},
574 {660, STRING_TOKEN (STR_TIMEZONE_M11)},
575 {600, STRING_TOKEN (STR_TIMEZONE_M10)},
576 {540, STRING_TOKEN (STR_TIMEZONE_M9)},
577 {480, STRING_TOKEN (STR_TIMEZONE_M8)},
578 {420, STRING_TOKEN (STR_TIMEZONE_M7)},
579 {360, STRING_TOKEN (STR_TIMEZONE_M6)},
580 {300, STRING_TOKEN (STR_TIMEZONE_M5)},
581 {270, STRING_TOKEN (STR_TIMEZONE_M430)},
582 {240, STRING_TOKEN (STR_TIMEZONE_M4)},
583 {210, STRING_TOKEN (STR_TIMEZONE_M330)},
584 {180, STRING_TOKEN (STR_TIMEZONE_M3)},
585 {120, STRING_TOKEN (STR_TIMEZONE_M2)},
586 {60 , STRING_TOKEN (STR_TIMEZONE_M1)},
587 {0 , STRING_TOKEN (STR_TIMEZONE_0)},
588 {-60 , STRING_TOKEN (STR_TIMEZONE_P1)},
589 {-120 , STRING_TOKEN (STR_TIMEZONE_P2)},
590 {-180 , STRING_TOKEN (STR_TIMEZONE_P3)},
591 {-210 , STRING_TOKEN (STR_TIMEZONE_P330)},
592 {-240 , STRING_TOKEN (STR_TIMEZONE_P4)},
593 {-270 , STRING_TOKEN (STR_TIMEZONE_P430)},
594 {-300 , STRING_TOKEN (STR_TIMEZONE_P5)},
595 {-330 , STRING_TOKEN (STR_TIMEZONE_P530)},
596 {-345 , STRING_TOKEN (STR_TIMEZONE_P545)},
597 {-360 , STRING_TOKEN (STR_TIMEZONE_P6)},
598 {-390 , STRING_TOKEN (STR_TIMEZONE_P630)},
599 {-420 , STRING_TOKEN (STR_TIMEZONE_P7)},
600 {-480 , STRING_TOKEN (STR_TIMEZONE_P8)},
601 {-540 , STRING_TOKEN (STR_TIMEZONE_P9)},
602 {-570 , STRING_TOKEN (STR_TIMEZONE_P930)},
603 {-600 , STRING_TOKEN (STR_TIMEZONE_P10)},
604 {-660 , STRING_TOKEN (STR_TIMEZONE_P11)},
605 {-720 , STRING_TOKEN (STR_TIMEZONE_P12)},
606 {-780 , STRING_TOKEN (STR_TIMEZONE_P13)},
607 {-840 , STRING_TOKEN (STR_TIMEZONE_P14)}
608 };
609
610 /**
611 Verify that the TimeZoneString is valid and if so set that as the current
612 timezone.
613
614 @param[in] TimeZoneString The pointer to a string representation of the timezone.
615
616 @retval SHELL_INVALID_PARAMETER TimeZoneString was NULL.
617 @retval SHELL_INVALID_PARAMETER TimeZoneString was mis-formatted.
618 @retval SHELL_SUCCESS The operation was successful.
619 **/
620 SHELL_STATUS
621 EFIAPI
622 CheckAndSetTimeZone (
623 IN CONST CHAR16 *TimeZoneString
624 )
625 {
626 EFI_TIME TheTime;
627 EFI_STATUS Status;
628 CHAR16 *TimeZoneCopy;
629 CHAR16 *Walker;
630 CHAR16 *Walker2;
631 UINTN LoopVar;
632
633 if (TimeZoneString == NULL) {
634 return (SHELL_INVALID_PARAMETER);
635 }
636
637 if (TimeZoneString != NULL && !InternalIsTimeLikeString(TimeZoneString, L':', 1, 1, TRUE)) {
638 return (SHELL_INVALID_PARAMETER);
639 }
640
641 Status = gRT->GetTime(&TheTime, NULL);
642 if (EFI_ERROR(Status)) {
643 return (SHELL_DEVICE_ERROR);
644 }
645
646 TimeZoneCopy = NULL;
647 TimeZoneCopy = StrnCatGrow(&TimeZoneCopy, NULL, TimeZoneString, 0);
648 if (TimeZoneCopy == NULL) {
649 return (SHELL_OUT_OF_RESOURCES);
650 }
651 Walker = TimeZoneCopy;
652 Walker2 = StrStr(Walker, L":");
653 if (Walker2 != NULL && *Walker2 == L':') {
654 *Walker2 = CHAR_NULL;
655 }
656 if (*Walker == L'-') {
657 TheTime.TimeZone = (INT16)((ShellStrToUintn (++Walker)) * 60);
658 } else {
659 TheTime.TimeZone = (INT16)((ShellStrToUintn (Walker)) * -60);
660 }
661 if (Walker2 != NULL) {
662 Walker = Walker2 + 1;
663 }
664 if (Walker != NULL && Walker[0] != CHAR_NULL) {
665 if (TheTime.TimeZone < 0) {
666 TheTime.TimeZone = (INT16)(TheTime.TimeZone - (UINT8)ShellStrToUintn (Walker));
667 } else {
668 TheTime.TimeZone = (INT16)(TheTime.TimeZone + (UINT8)ShellStrToUintn (Walker));
669 }
670 }
671
672 Status = EFI_INVALID_PARAMETER;
673
674 for ( LoopVar = 0
675 ; LoopVar < sizeof(TimeZoneList) / sizeof(TimeZoneList[0])
676 ; LoopVar++
677 ){
678 if (TheTime.TimeZone == TimeZoneList[LoopVar].TimeZone) {
679 Status = gRT->SetTime(&TheTime);
680 break;
681 }
682 }
683
684 FreePool(TimeZoneCopy);
685
686 if (!EFI_ERROR(Status)){
687 return (SHELL_SUCCESS);
688 }
689 return (SHELL_INVALID_PARAMETER);
690 }
691
692
693 /**
694 Function for 'timezone' command.
695
696 @param[in] ImageHandle Handle to the Image (NULL if Internal).
697 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
698 **/
699 SHELL_STATUS
700 EFIAPI
701 ShellCommandRunTimeZone (
702 IN EFI_HANDLE ImageHandle,
703 IN EFI_SYSTEM_TABLE *SystemTable
704 )
705 {
706 //
707 // non interactive
708 //
709 EFI_STATUS Status;
710 LIST_ENTRY *Package;
711 CHAR16 *ProblemParam;
712 SHELL_STATUS ShellStatus;
713 UINT8 LoopVar;
714 EFI_TIME TheTime;
715 BOOLEAN Found;
716 UINTN TzMinutes;
717
718 ShellStatus = SHELL_SUCCESS;
719 ProblemParam = NULL;
720
721 //
722 // initialize the shell lib (we must be in non-auto-init...)
723 //
724 Status = ShellInitialize();
725 ASSERT_EFI_ERROR(Status);
726
727 //
728 // parse the command line
729 //
730 if (PcdGet8(PcdShellSupportLevel) == 2) {
731 Status = ShellCommandLineParse (TimeZoneParamList2, &Package, &ProblemParam, TRUE);
732 } else {
733 ASSERT(PcdGet8(PcdShellSupportLevel) == 3);
734 Status = ShellCommandLineParseEx (TimeZoneParamList3, &Package, &ProblemParam, TRUE, TRUE);
735 }
736 if (EFI_ERROR(Status)) {
737 if (Status == EFI_VOLUME_CORRUPTED && ProblemParam != NULL) {
738 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellLevel2HiiHandle, ProblemParam);
739 FreePool(ProblemParam);
740 ShellStatus = SHELL_INVALID_PARAMETER;
741 } else {
742 ASSERT(FALSE);
743 }
744 } else {
745 //
746 // check for "-?"
747 //
748 if (ShellCommandLineGetCount(Package) > 1) {
749 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellLevel2HiiHandle);
750 ShellStatus = SHELL_INVALID_PARAMETER;
751 } else if (ShellCommandLineGetFlag(Package, L"-?")) {
752 ASSERT(FALSE);
753 } else if (ShellCommandLineGetFlag(Package, L"-s")) {
754 if ((ShellCommandLineGetFlag(Package, L"-l")) || (ShellCommandLineGetFlag(Package, L"-f"))) {
755 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellLevel2HiiHandle, L"-l or -f");
756 ShellStatus = SHELL_INVALID_PARAMETER;
757 } else {
758 ASSERT(PcdGet8(PcdShellSupportLevel) == 3);
759 if (ShellCommandLineGetValue(Package, L"-s") == NULL) {
760 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_VALUE), gShellLevel2HiiHandle, L"-s");
761 ShellStatus = SHELL_INVALID_PARAMETER;
762 } else {
763 //
764 // Set the time zone
765 //
766 ShellStatus = CheckAndSetTimeZone(ShellCommandLineGetValue(Package, L"-s"));
767 if (ShellStatus != SHELL_SUCCESS) {
768 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellLevel2HiiHandle, ShellCommandLineGetValue(Package, L"-s"));
769 ShellStatus = SHELL_INVALID_PARAMETER;
770 }
771 }
772 }
773 } else if (ShellCommandLineGetFlag(Package, L"-l")) {
774 //
775 // Print a list of all time zones
776 //
777 for ( LoopVar = 0
778 ; LoopVar < sizeof(TimeZoneList) / sizeof(TimeZoneList[0])
779 ; LoopVar++
780 ){
781 ShellPrintHiiEx (-1, -1, NULL, TimeZoneList[LoopVar].StringId, gShellLevel2HiiHandle);
782 }
783 } else {
784 //
785 // Get Current Time Zone Info
786 //
787 Status = gRT->GetTime(&TheTime, NULL);
788 ASSERT_EFI_ERROR(Status);
789
790 if (TheTime.TimeZone != EFI_UNSPECIFIED_TIMEZONE) {
791 Found = FALSE;
792 for ( LoopVar = 0
793 ; LoopVar < sizeof(TimeZoneList) / sizeof(TimeZoneList[0])
794 ; LoopVar++
795 ){
796 if (TheTime.TimeZone == TimeZoneList[LoopVar].TimeZone) {
797 if (ShellCommandLineGetFlag(Package, L"-f")) {
798 //
799 // Print all info about current time zone
800 //
801 ShellPrintHiiEx (-1, -1, NULL, TimeZoneList[LoopVar].StringId, gShellLevel2HiiHandle);
802 } else {
803 //
804 // Print basic info only
805 //
806 if (TheTime.TimeZone == EFI_UNSPECIFIED_TIMEZONE) {
807 TzMinutes = 0;
808 } else {
809 TzMinutes = (ABS(TheTime.TimeZone)) % 60;
810 }
811
812 ShellPrintHiiEx (
813 -1,
814 -1,
815 NULL,
816 STRING_TOKEN(STR_TIMEZONE_SIMPLE),
817 gShellLevel2HiiHandle,
818 TheTime.TimeZone==EFI_UNSPECIFIED_TIMEZONE?0:(TheTime.TimeZone > 0?L"-":L"+"),
819 TheTime.TimeZone==EFI_UNSPECIFIED_TIMEZONE?0:(ABS(TheTime.TimeZone)) / 60,
820 TzMinutes);
821 }
822 Found = TRUE;
823 break;
824 }
825 }
826 if (!Found) {
827 //
828 // Print basic info only
829 //
830 if (TheTime.TimeZone == EFI_UNSPECIFIED_TIMEZONE) {
831 TzMinutes = 0;
832 } else {
833 TzMinutes = (ABS(TheTime.TimeZone)) % 60;
834 }
835 ShellPrintHiiEx (
836 -1,
837 -1,
838 NULL,
839 STRING_TOKEN(STR_TIMEZONE_SIMPLE),
840 gShellLevel2HiiHandle,
841 TheTime.TimeZone==EFI_UNSPECIFIED_TIMEZONE?0:(TheTime.TimeZone > 0?L"-":L"+"),
842 TheTime.TimeZone==EFI_UNSPECIFIED_TIMEZONE?0:(ABS(TheTime.TimeZone)) / 60,
843 TzMinutes);
844 if (ShellCommandLineGetFlag(Package, L"-f")) {
845 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN(STR_TIMEZONE_NI), gShellLevel2HiiHandle);
846 }
847 }
848 } else {
849 //
850 // TimeZone was EFI_UNSPECIFIED_TIMEZONE (unknown) from GetTime()
851 //
852 }
853 }
854 }
855
856 //
857 // free the command line package
858 //
859 ShellCommandLineFreeVarList (Package);
860
861 return (ShellStatus);
862 }