]>
Commit | Line | Data |
---|---|---|
3863585b WD |
1 | /* |
2 | * (C) Copyright 2001 | |
3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. | |
4 | * | |
5 | * See file CREDITS for list of people who contributed to this | |
6 | * project. | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or | |
9 | * modify it under the terms of the GNU General Public License as | |
10 | * published by the Free Software Foundation; either version 2 of | |
11 | * the License, or (at your option) any later version. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program; if not, write to the Free Software | |
20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, | |
21 | * MA 02111-1307 USA | |
22 | */ | |
23 | ||
24 | /* | |
25 | * RTC, Date & Time support: get and set date & time | |
26 | */ | |
27 | #include <common.h> | |
28 | #include <command.h> | |
29 | #include <rtc.h> | |
30 | ||
31 | #if (CONFIG_COMMANDS & CFG_CMD_DATE) | |
32 | ||
33 | const char *weekdays[] = { | |
34 | "Sun", "Mon", "Tues", "Wednes", "Thurs", "Fri", "Satur", | |
35 | }; | |
36 | ||
3e38691e WD |
37 | #define RELOC(a) ((typeof(a))((unsigned long)(a) + gd->reloc_off)) |
38 | ||
3863585b WD |
39 | int mk_date (char *, struct rtc_time *); |
40 | ||
41 | int do_date (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) | |
42 | { | |
3e38691e | 43 | DECLARE_GLOBAL_DATA_PTR; |
3863585b WD |
44 | struct rtc_time tm; |
45 | int rcode = 0; | |
46 | ||
47 | switch (argc) { | |
48 | case 2: /* set date & time */ | |
49 | if (strcmp(argv[1],"reset") == 0) { | |
50 | printf ("Reset RTC...\n"); | |
51 | rtc_reset (); | |
52 | } else { | |
53 | /* initialize tm with current time */ | |
54 | rtc_get (&tm); | |
55 | /* insert new date & time */ | |
56 | if (mk_date (argv[1], &tm) != 0) { | |
57 | printf ("## Bad date format\n"); | |
58 | return 1; | |
59 | } | |
60 | /* and write to RTC */ | |
61 | rtc_set (&tm); | |
62 | } | |
63 | /* FALL TROUGH */ | |
64 | case 1: /* get date & time */ | |
65 | rtc_get (&tm); | |
66 | ||
67 | printf ("Date: %4d-%02d-%02d (%sday) Time: %2d:%02d:%02d\n", | |
68 | tm.tm_year, tm.tm_mon, tm.tm_mday, | |
69 | (tm.tm_wday<0 || tm.tm_wday>6) ? | |
3e38691e | 70 | "unknown " : RELOC(weekdays[tm.tm_wday]), |
3863585b WD |
71 | tm.tm_hour, tm.tm_min, tm.tm_sec); |
72 | ||
73 | return 0; | |
74 | default: | |
75 | printf ("Usage:\n%s\n", cmdtp->usage); | |
76 | rcode = 1; | |
77 | } | |
78 | return rcode; | |
79 | } | |
80 | ||
81 | /* | |
82 | * simple conversion of two-digit string with error checking | |
83 | */ | |
84 | static int cnvrt2 (char *str, int *valp) | |
85 | { | |
86 | int val; | |
87 | ||
88 | if ((*str < '0') || (*str > '9')) | |
89 | return (-1); | |
90 | ||
91 | val = *str - '0'; | |
92 | ||
93 | ++str; | |
94 | ||
95 | if ((*str < '0') || (*str > '9')) | |
96 | return (-1); | |
97 | ||
98 | *valp = 10 * val + (*str - '0'); | |
99 | ||
100 | return (0); | |
101 | } | |
102 | ||
103 | /* | |
104 | * Convert date string: MMDDhhmm[[CC]YY][.ss] | |
105 | * | |
106 | * Some basic checking for valid values is done, but this will not catch | |
107 | * all possible error conditions. | |
108 | */ | |
109 | int mk_date (char *datestr, struct rtc_time *tmp) | |
110 | { | |
111 | int len, val; | |
112 | char *ptr; | |
113 | ||
114 | ptr = strchr (datestr,'.'); | |
115 | len = strlen (datestr); | |
116 | ||
117 | /* Set seconds */ | |
118 | if (ptr) { | |
119 | int sec; | |
120 | ||
121 | *ptr++ = '\0'; | |
122 | if ((len - (ptr - datestr)) != 2) | |
123 | return (-1); | |
124 | ||
125 | len = strlen (datestr); | |
126 | ||
127 | if (cnvrt2 (ptr, &sec)) | |
128 | return (-1); | |
129 | ||
130 | tmp->tm_sec = sec; | |
131 | } else { | |
132 | tmp->tm_sec = 0; | |
133 | } | |
134 | ||
135 | if (len == 12) { /* MMDDhhmmCCYY */ | |
136 | int year, century; | |
137 | ||
138 | if (cnvrt2 (datestr+ 8, ¢ury) || | |
139 | cnvrt2 (datestr+10, &year) ) { | |
140 | return (-1); | |
141 | } | |
142 | tmp->tm_year = 100 * century + year; | |
143 | } else if (len == 10) { /* MMDDhhmmYY */ | |
144 | int year, century; | |
145 | ||
146 | century = tmp->tm_year / 100; | |
147 | if (cnvrt2 (datestr+ 8, &year)) | |
148 | return (-1); | |
149 | tmp->tm_year = 100 * century + year; | |
150 | } | |
151 | ||
152 | switch (len) { | |
153 | case 8: /* MMDDhhmm */ | |
154 | /* fall thru */ | |
155 | case 10: /* MMDDhhmmYY */ | |
156 | /* fall thru */ | |
157 | case 12: /* MMDDhhmmCCYY */ | |
158 | if (cnvrt2 (datestr+0, &val) || | |
159 | val > 12) { | |
160 | break; | |
161 | } | |
162 | tmp->tm_mon = val; | |
163 | if (cnvrt2 (datestr+2, &val) || | |
164 | val > ((tmp->tm_mon==2) ? 29 : 31)) { | |
165 | break; | |
166 | } | |
167 | tmp->tm_mday = val; | |
168 | ||
169 | if (cnvrt2 (datestr+4, &val) || | |
170 | val > 23) { | |
171 | break; | |
172 | } | |
173 | tmp->tm_hour = val; | |
174 | ||
175 | if (cnvrt2 (datestr+6, &val) || | |
176 | val > 59) { | |
177 | break; | |
178 | } | |
179 | tmp->tm_min = val; | |
180 | ||
181 | /* calculate day of week */ | |
182 | GregorianDay (tmp); | |
183 | ||
184 | return (0); | |
185 | default: | |
186 | break; | |
187 | } | |
188 | ||
189 | return (-1); | |
190 | } | |
191 | ||
8bde7f77 WD |
192 | /***************************************************/ |
193 | ||
0d498393 WD |
194 | U_BOOT_CMD( |
195 | date, 2, 1, do_date, | |
8bde7f77 WD |
196 | "date - get/set/reset date & time\n", |
197 | "[MMDDhhmm[[CC]YY][.ss]]\ndate reset\n" | |
198 | " - without arguments: print date & time\n" | |
199 | " - with numeric argument: set the system date & time\n" | |
200 | " - with 'reset' argument: reset the RTC\n" | |
201 | ); | |
202 | ||
3863585b | 203 | #endif /* CFG_CMD_DATE */ |