]> git.ipfire.org Git - thirdparty/u-boot.git/blame - cmd/date.c
Merge branch 'master' of git://git.denx.de/u-boot-sh
[thirdparty/u-boot.git] / cmd / date.c
CommitLineData
83d290c5 1// SPDX-License-Identifier: GPL-2.0+
3863585b
WD
2/*
3 * (C) Copyright 2001
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
3863585b
WD
5 */
6
7/*
8 * RTC, Date & Time support: get and set date & time
9 */
10#include <common.h>
11#include <command.h>
f9951ead 12#include <dm.h>
3863585b 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
1a1fa240
MV
30static struct rtc_time default_tm = { 0, 0, 0, 1, 1, 2000, 6, 0, 0 };
31
088f1b19 32static int do_date(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
3863585b
WD
33{
34 struct rtc_time tm;
35 int rcode = 0;
f9951ead 36 int old_bus __maybe_unused;
0dc018ec
SR
37
38 /* switch to correct I2C bus */
ffe38798 39#ifdef CONFIG_DM_RTC
f9951ead
SG
40 struct udevice *dev;
41
42 rcode = uclass_get_device(UCLASS_RTC, 0, &dev);
43 if (rcode) {
44 printf("Cannot find RTC: err=%d\n", rcode);
45 return CMD_RET_FAILURE;
46 }
47#elif defined(CONFIG_SYS_I2C)
3f4978c7
HS
48 old_bus = i2c_get_bus_num();
49 i2c_set_bus_num(CONFIG_SYS_RTC_BUS_NUM);
50#else
0dc018ec 51 old_bus = I2C_GET_BUS();
6d0f6bcf 52 I2C_SET_BUS(CONFIG_SYS_RTC_BUS_NUM);
3f4978c7 53#endif
3863585b
WD
54
55 switch (argc) {
56 case 2: /* set date & time */
57 if (strcmp(argv[1],"reset") == 0) {
4b9206ed 58 puts ("Reset RTC...\n");
ffe38798 59#ifdef CONFIG_DM_RTC
f9951ead
SG
60 rcode = dm_rtc_reset(dev);
61 if (!rcode)
62 rcode = dm_rtc_set(dev, &default_tm);
63#else
64 rtc_reset();
1a1fa240 65 rcode = rtc_set(&default_tm);
f9951ead 66#endif
1a1fa240
MV
67 if (rcode)
68 puts("## Failed to set date after RTC reset\n");
3863585b
WD
69 } else {
70 /* initialize tm with current time */
ffe38798 71#ifdef CONFIG_DM_RTC
f9951ead
SG
72 rcode = dm_rtc_get(dev, &tm);
73#else
74 rcode = rtc_get(&tm);
75#endif
76 if (!rcode) {
d1e23194 77 /* insert new date & time */
f9951ead 78 if (mk_date(argv[1], &tm) != 0) {
d1e23194
JCPV
79 puts ("## Bad date format\n");
80 break;
81 }
82 /* and write to RTC */
ffe38798 83#ifdef CONFIG_DM_RTC
f9951ead
SG
84 rcode = dm_rtc_set(dev, &tm);
85#else
86 rcode = rtc_set(&tm);
87#endif
88 if (rcode) {
89 printf("## Set date failed: err=%d\n",
90 rcode);
91 }
d1e23194 92 } else {
d52e3e01 93 puts("## Get date failed\n");
3863585b 94 }
3863585b
WD
95 }
96 /* FALL TROUGH */
97 case 1: /* get date & time */
ffe38798 98#ifdef CONFIG_DM_RTC
f9951ead
SG
99 rcode = dm_rtc_get(dev, &tm);
100#else
101 rcode = rtc_get(&tm);
102#endif
d1e23194 103 if (rcode) {
d52e3e01 104 puts("## Get date failed\n");
d1e23194
JCPV
105 break;
106 }
3863585b
WD
107
108 printf ("Date: %4d-%02d-%02d (%sday) Time: %2d:%02d:%02d\n",
109 tm.tm_year, tm.tm_mon, tm.tm_mday,
110 (tm.tm_wday<0 || tm.tm_wday>6) ?
3e38691e 111 "unknown " : RELOC(weekdays[tm.tm_wday]),
3863585b
WD
112 tm.tm_hour, tm.tm_min, tm.tm_sec);
113
0dc018ec 114 break;
3863585b 115 default:
4c12eeb8 116 rcode = CMD_RET_USAGE;
3863585b 117 }
0dc018ec
SR
118
119 /* switch back to original I2C bus */
3f4978c7
HS
120#ifdef CONFIG_SYS_I2C
121 i2c_set_bus_num(old_bus);
ffe38798 122#elif !defined(CONFIG_DM_RTC)
0dc018ec 123 I2C_SET_BUS(old_bus);
3f4978c7 124#endif
0dc018ec 125
f9951ead 126 return rcode ? CMD_RET_FAILURE : 0;
3863585b
WD
127}
128
129/*
130 * simple conversion of two-digit string with error checking
131 */
bdbc1303 132static int cnvrt2 (const char *str, int *valp)
3863585b
WD
133{
134 int val;
135
136 if ((*str < '0') || (*str > '9'))
137 return (-1);
138
139 val = *str - '0';
140
141 ++str;
142
143 if ((*str < '0') || (*str > '9'))
144 return (-1);
145
146 *valp = 10 * val + (*str - '0');
147
148 return (0);
149}
150
151/*
152 * Convert date string: MMDDhhmm[[CC]YY][.ss]
153 *
154 * Some basic checking for valid values is done, but this will not catch
155 * all possible error conditions.
156 */
bdbc1303 157int mk_date (const char *datestr, struct rtc_time *tmp)
3863585b
WD
158{
159 int len, val;
160 char *ptr;
161
162 ptr = strchr (datestr,'.');
163 len = strlen (datestr);
164
165 /* Set seconds */
166 if (ptr) {
167 int sec;
168
169 *ptr++ = '\0';
170 if ((len - (ptr - datestr)) != 2)
171 return (-1);
172
173 len = strlen (datestr);
174
175 if (cnvrt2 (ptr, &sec))
176 return (-1);
177
178 tmp->tm_sec = sec;
179 } else {
180 tmp->tm_sec = 0;
181 }
182
183 if (len == 12) { /* MMDDhhmmCCYY */
184 int year, century;
185
186 if (cnvrt2 (datestr+ 8, &century) ||
187 cnvrt2 (datestr+10, &year) ) {
188 return (-1);
189 }
190 tmp->tm_year = 100 * century + year;
191 } else if (len == 10) { /* MMDDhhmmYY */
192 int year, century;
193
194 century = tmp->tm_year / 100;
195 if (cnvrt2 (datestr+ 8, &year))
196 return (-1);
197 tmp->tm_year = 100 * century + year;
198 }
199
200 switch (len) {
201 case 8: /* MMDDhhmm */
202 /* fall thru */
203 case 10: /* MMDDhhmmYY */
204 /* fall thru */
205 case 12: /* MMDDhhmmCCYY */
206 if (cnvrt2 (datestr+0, &val) ||
207 val > 12) {
208 break;
209 }
210 tmp->tm_mon = val;
211 if (cnvrt2 (datestr+2, &val) ||
212 val > ((tmp->tm_mon==2) ? 29 : 31)) {
213 break;
214 }
215 tmp->tm_mday = val;
216
217 if (cnvrt2 (datestr+4, &val) ||
218 val > 23) {
219 break;
220 }
221 tmp->tm_hour = val;
222
223 if (cnvrt2 (datestr+6, &val) ||
224 val > 59) {
225 break;
226 }
227 tmp->tm_min = val;
228
229 /* calculate day of week */
199e87c3 230 rtc_calc_weekday(tmp);
3863585b
WD
231
232 return (0);
233 default:
234 break;
235 }
236
237 return (-1);
238}
239
8bde7f77
WD
240/***************************************************/
241
0d498393
WD
242U_BOOT_CMD(
243 date, 2, 1, do_date,
2fb2604d 244 "get/set/reset date & time",
8bde7f77
WD
245 "[MMDDhhmm[[CC]YY][.ss]]\ndate reset\n"
246 " - without arguments: print date & time\n"
247 " - with numeric argument: set the system date & time\n"
a89c33db 248 " - with 'reset' argument: reset the RTC"
8bde7f77 249);