]> git.sur5r.net Git - u-boot/blob - common/cmd_date.c
f7ae1da1e246491ada89b3abba64d8b66486e973
[u-boot] / common / cmd_date.c
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 #include <i2c.h>
31
32 DECLARE_GLOBAL_DATA_PTR;
33
34 static const char * const weekdays[] = {
35         "Sun", "Mon", "Tues", "Wednes", "Thurs", "Fri", "Satur",
36 };
37
38 #ifdef CONFIG_NEEDS_MANUAL_RELOC
39 #define RELOC(a)        ((typeof(a))((unsigned long)(a) + gd->reloc_off))
40 #else
41 #define RELOC(a)        a
42 #endif
43
44 int mk_date (const char *, struct rtc_time *);
45
46 static int do_date(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
47 {
48         struct rtc_time tm;
49         int rcode = 0;
50         int old_bus;
51
52         /* switch to correct I2C bus */
53 #ifdef CONFIG_SYS_I2C
54         old_bus = i2c_get_bus_num();
55         i2c_set_bus_num(CONFIG_SYS_RTC_BUS_NUM);
56 #else
57         old_bus = I2C_GET_BUS();
58         I2C_SET_BUS(CONFIG_SYS_RTC_BUS_NUM);
59 #endif
60
61         switch (argc) {
62         case 2:                 /* set date & time */
63                 if (strcmp(argv[1],"reset") == 0) {
64                         puts ("Reset RTC...\n");
65                         rtc_reset ();
66                 } else {
67                         /* initialize tm with current time */
68                         rcode = rtc_get (&tm);
69
70                         if(!rcode) {
71                                 /* insert new date & time */
72                                 if (mk_date (argv[1], &tm) != 0) {
73                                         puts ("## Bad date format\n");
74                                         break;
75                                 }
76                                 /* and write to RTC */
77                                 rcode = rtc_set (&tm);
78                                 if(rcode)
79                                         puts("## Set date failed\n");
80                         } else {
81                                 puts("## Get date failed\n");
82                         }
83                 }
84                 /* FALL TROUGH */
85         case 1:                 /* get date & time */
86                 rcode = rtc_get (&tm);
87
88                 if (rcode) {
89                         puts("## Get date failed\n");
90                         break;
91                 }
92
93                 printf ("Date: %4d-%02d-%02d (%sday)    Time: %2d:%02d:%02d\n",
94                         tm.tm_year, tm.tm_mon, tm.tm_mday,
95                         (tm.tm_wday<0 || tm.tm_wday>6) ?
96                                 "unknown " : RELOC(weekdays[tm.tm_wday]),
97                         tm.tm_hour, tm.tm_min, tm.tm_sec);
98
99                 break;
100         default:
101                 rcode = CMD_RET_USAGE;
102         }
103
104         /* switch back to original I2C bus */
105 #ifdef CONFIG_SYS_I2C
106         i2c_set_bus_num(old_bus);
107 #else
108         I2C_SET_BUS(old_bus);
109 #endif
110
111         return rcode;
112 }
113
114 /*
115  * simple conversion of two-digit string with error checking
116  */
117 static int cnvrt2 (const char *str, int *valp)
118 {
119         int val;
120
121         if ((*str < '0') || (*str > '9'))
122                 return (-1);
123
124         val = *str - '0';
125
126         ++str;
127
128         if ((*str < '0') || (*str > '9'))
129                 return (-1);
130
131         *valp = 10 * val + (*str - '0');
132
133         return (0);
134 }
135
136 /*
137  * Convert date string: MMDDhhmm[[CC]YY][.ss]
138  *
139  * Some basic checking for valid values is done, but this will not catch
140  * all possible error conditions.
141  */
142 int mk_date (const char *datestr, struct rtc_time *tmp)
143 {
144         int len, val;
145         char *ptr;
146
147         ptr = strchr (datestr,'.');
148         len = strlen (datestr);
149
150         /* Set seconds */
151         if (ptr) {
152                 int sec;
153
154                 *ptr++ = '\0';
155                 if ((len - (ptr - datestr)) != 2)
156                         return (-1);
157
158                 len = strlen (datestr);
159
160                 if (cnvrt2 (ptr, &sec))
161                         return (-1);
162
163                 tmp->tm_sec = sec;
164         } else {
165                 tmp->tm_sec = 0;
166         }
167
168         if (len == 12) {                /* MMDDhhmmCCYY */
169                 int year, century;
170
171                 if (cnvrt2 (datestr+ 8, &century) ||
172                     cnvrt2 (datestr+10, &year) ) {
173                         return (-1);
174                 }
175                 tmp->tm_year = 100 * century + year;
176         } else if (len == 10) {         /* MMDDhhmmYY   */
177                 int year, century;
178
179                 century = tmp->tm_year / 100;
180                 if (cnvrt2 (datestr+ 8, &year))
181                         return (-1);
182                 tmp->tm_year = 100 * century + year;
183         }
184
185         switch (len) {
186         case 8:                 /* MMDDhhmm     */
187                 /* fall thru */
188         case 10:                /* MMDDhhmmYY   */
189                 /* fall thru */
190         case 12:                /* MMDDhhmmCCYY */
191                 if (cnvrt2 (datestr+0, &val) ||
192                     val > 12) {
193                         break;
194                 }
195                 tmp->tm_mon  = val;
196                 if (cnvrt2 (datestr+2, &val) ||
197                     val > ((tmp->tm_mon==2) ? 29 : 31)) {
198                         break;
199                 }
200                 tmp->tm_mday = val;
201
202                 if (cnvrt2 (datestr+4, &val) ||
203                     val > 23) {
204                         break;
205                 }
206                 tmp->tm_hour = val;
207
208                 if (cnvrt2 (datestr+6, &val) ||
209                     val > 59) {
210                         break;
211                 }
212                 tmp->tm_min  = val;
213
214                 /* calculate day of week */
215                 GregorianDay (tmp);
216
217                 return (0);
218         default:
219                 break;
220         }
221
222         return (-1);
223 }
224
225 /***************************************************/
226
227 U_BOOT_CMD(
228         date,   2,      1,      do_date,
229         "get/set/reset date & time",
230         "[MMDDhhmm[[CC]YY][.ss]]\ndate reset\n"
231         "  - without arguments: print date & time\n"
232         "  - with numeric argument: set the system date & time\n"
233         "  - with 'reset' argument: reset the RTC"
234 );