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