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