]> git.ipfire.org Git - thirdparty/u-boot.git/blame - common/cmd_date.c
Add GPL-2.0+ SPDX-License-Identifier to source files
[thirdparty/u-boot.git] / common / cmd_date.c
CommitLineData
3863585b
WD
1/*
2 * (C) Copyright 2001
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
1a459660 5 * SPDX-License-Identifier: GPL-2.0+
3863585b
WD
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>
0dc018ec 14#include <i2c.h>
3863585b 15
d87080b7
WD
16DECLARE_GLOBAL_DATA_PTR;
17
bdbc1303 18static const char * const weekdays[] = {
3863585b
WD
19 "Sun", "Mon", "Tues", "Wednes", "Thurs", "Fri", "Satur",
20};
21
2e5167cc 22#ifdef CONFIG_NEEDS_MANUAL_RELOC
3e38691e 23#define RELOC(a) ((typeof(a))((unsigned long)(a) + gd->reloc_off))
2e5167cc
WD
24#else
25#define RELOC(a) a
521af04d 26#endif
3e38691e 27
bdbc1303 28int mk_date (const char *, struct rtc_time *);
3863585b 29
088f1b19 30static int do_date(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
3863585b
WD
31{
32 struct rtc_time tm;
33 int rcode = 0;
0dc018ec
SR
34 int old_bus;
35
36 /* switch to correct I2C bus */
37 old_bus = I2C_GET_BUS();
6d0f6bcf 38 I2C_SET_BUS(CONFIG_SYS_RTC_BUS_NUM);
3863585b
WD
39
40 switch (argc) {
41 case 2: /* set date & time */
42 if (strcmp(argv[1],"reset") == 0) {
4b9206ed 43 puts ("Reset RTC...\n");
3863585b
WD
44 rtc_reset ();
45 } else {
46 /* initialize tm with current time */
d1e23194
JCPV
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)
d52e3e01 58 puts("## Set date failed\n");
d1e23194 59 } else {
d52e3e01 60 puts("## Get date failed\n");
3863585b 61 }
3863585b
WD
62 }
63 /* FALL TROUGH */
64 case 1: /* get date & time */
d1e23194
JCPV
65 rcode = rtc_get (&tm);
66
67 if (rcode) {
d52e3e01 68 puts("## Get date failed\n");
d1e23194
JCPV
69 break;
70 }
3863585b
WD
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) ?
3e38691e 75 "unknown " : RELOC(weekdays[tm.tm_wday]),
3863585b
WD
76 tm.tm_hour, tm.tm_min, tm.tm_sec);
77
0dc018ec 78 break;
3863585b 79 default:
4c12eeb8 80 rcode = CMD_RET_USAGE;
3863585b 81 }
0dc018ec
SR
82
83 /* switch back to original I2C bus */
84 I2C_SET_BUS(old_bus);
85
3863585b
WD
86 return rcode;
87}
88
89/*
90 * simple conversion of two-digit string with error checking
91 */
bdbc1303 92static int cnvrt2 (const char *str, int *valp)
3863585b
WD
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 */
bdbc1303 117int mk_date (const char *datestr, struct rtc_time *tmp)
3863585b
WD
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
8bde7f77
WD
200/***************************************************/
201
0d498393
WD
202U_BOOT_CMD(
203 date, 2, 1, do_date,
2fb2604d 204 "get/set/reset date & time",
8bde7f77
WD
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"
a89c33db 208 " - with 'reset' argument: reset the RTC"
8bde7f77 209);