]> Git Repo - J-u-boot.git/blame - cmd/date.c
Merge tag 'u-boot-imx-master-20250127' of https://gitlab.denx.de/u-boot/custodians...
[J-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, [email protected].
3863585b
WD
5 */
6
7/*
8 * RTC, Date & Time support: get and set date & time
9 */
3863585b 10#include <command.h>
f9951ead 11#include <dm.h>
3863585b 12#include <rtc.h>
0dc018ec 13#include <i2c.h>
401d1c4f 14#include <asm/global_data.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
bdbc1303 22int mk_date (const char *, struct rtc_time *);
3863585b 23
1a1fa240
MV
24static struct rtc_time default_tm = { 0, 0, 0, 1, 1, 2000, 6, 0, 0 };
25
09140113
SG
26static int do_date(struct cmd_tbl *cmdtp, int flag, int argc,
27 char *const argv[])
3863585b
WD
28{
29 struct rtc_time tm;
30 int rcode = 0;
f9951ead 31 int old_bus __maybe_unused;
0dc018ec
SR
32
33 /* switch to correct I2C bus */
f9951ead
SG
34 struct udevice *dev;
35
a1ae55ee 36 rcode = uclass_get_device_by_seq(UCLASS_RTC, 0, &dev);
f9951ead 37 if (rcode) {
a1ae55ee
MS
38 rcode = uclass_get_device(UCLASS_RTC, 0, &dev);
39 if (rcode) {
40 printf("Cannot find RTC: err=%d\n", rcode);
41 return CMD_RET_FAILURE;
42 }
f9951ead 43 }
3863585b
WD
44
45 switch (argc) {
46 case 2: /* set date & time */
47 if (strcmp(argv[1],"reset") == 0) {
4b9206ed 48 puts ("Reset RTC...\n");
f9951ead
SG
49 rcode = dm_rtc_reset(dev);
50 if (!rcode)
51 rcode = dm_rtc_set(dev, &default_tm);
1a1fa240
MV
52 if (rcode)
53 puts("## Failed to set date after RTC reset\n");
3863585b
WD
54 } else {
55 /* initialize tm with current time */
f9951ead 56 rcode = dm_rtc_get(dev, &tm);
f9951ead 57 if (!rcode) {
d1e23194 58 /* insert new date & time */
f9951ead 59 if (mk_date(argv[1], &tm) != 0) {
d1e23194
JCPV
60 puts ("## Bad date format\n");
61 break;
62 }
63 /* and write to RTC */
f9951ead 64 rcode = dm_rtc_set(dev, &tm);
f9951ead
SG
65 if (rcode) {
66 printf("## Set date failed: err=%d\n",
67 rcode);
68 }
d1e23194 69 } else {
d52e3e01 70 puts("## Get date failed\n");
3863585b 71 }
3863585b 72 }
06a94b3e 73 fallthrough;
3863585b 74 case 1: /* get date & time */
f9951ead 75 rcode = dm_rtc_get(dev, &tm);
d1e23194 76 if (rcode) {
d52e3e01 77 puts("## Get date failed\n");
d1e23194
JCPV
78 break;
79 }
3863585b
WD
80
81 printf ("Date: %4d-%02d-%02d (%sday) Time: %2d:%02d:%02d\n",
82 tm.tm_year, tm.tm_mon, tm.tm_mday,
83 (tm.tm_wday<0 || tm.tm_wday>6) ?
8658370a 84 "unknown " : weekdays[tm.tm_wday],
3863585b
WD
85 tm.tm_hour, tm.tm_min, tm.tm_sec);
86
0dc018ec 87 break;
3863585b 88 default:
4c12eeb8 89 rcode = CMD_RET_USAGE;
3863585b 90 }
0dc018ec 91
f9951ead 92 return rcode ? CMD_RET_FAILURE : 0;
3863585b
WD
93}
94
95/*
96 * simple conversion of two-digit string with error checking
97 */
bdbc1303 98static int cnvrt2 (const char *str, int *valp)
3863585b
WD
99{
100 int val;
101
102 if ((*str < '0') || (*str > '9'))
103 return (-1);
104
105 val = *str - '0';
106
107 ++str;
108
109 if ((*str < '0') || (*str > '9'))
110 return (-1);
111
112 *valp = 10 * val + (*str - '0');
113
114 return (0);
115}
116
117/*
118 * Convert date string: MMDDhhmm[[CC]YY][.ss]
119 *
120 * Some basic checking for valid values is done, but this will not catch
121 * all possible error conditions.
122 */
bdbc1303 123int mk_date (const char *datestr, struct rtc_time *tmp)
3863585b
WD
124{
125 int len, val;
126 char *ptr;
127
44ac80e7
RK
128 ptr = strchr(datestr, '.');
129 len = strlen(datestr);
3863585b
WD
130
131 /* Set seconds */
132 if (ptr) {
133 int sec;
134
44ac80e7 135 ptr++;
3863585b
WD
136 if ((len - (ptr - datestr)) != 2)
137 return (-1);
138
44ac80e7 139 len -= 3;
3863585b
WD
140
141 if (cnvrt2 (ptr, &sec))
142 return (-1);
143
144 tmp->tm_sec = sec;
145 } else {
146 tmp->tm_sec = 0;
147 }
148
149 if (len == 12) { /* MMDDhhmmCCYY */
150 int year, century;
151
152 if (cnvrt2 (datestr+ 8, &century) ||
153 cnvrt2 (datestr+10, &year) ) {
154 return (-1);
155 }
156 tmp->tm_year = 100 * century + year;
157 } else if (len == 10) { /* MMDDhhmmYY */
158 int year, century;
159
160 century = tmp->tm_year / 100;
161 if (cnvrt2 (datestr+ 8, &year))
162 return (-1);
163 tmp->tm_year = 100 * century + year;
164 }
165
166 switch (len) {
167 case 8: /* MMDDhhmm */
168 /* fall thru */
169 case 10: /* MMDDhhmmYY */
170 /* fall thru */
171 case 12: /* MMDDhhmmCCYY */
172 if (cnvrt2 (datestr+0, &val) ||
173 val > 12) {
174 break;
175 }
176 tmp->tm_mon = val;
177 if (cnvrt2 (datestr+2, &val) ||
178 val > ((tmp->tm_mon==2) ? 29 : 31)) {
179 break;
180 }
181 tmp->tm_mday = val;
182
183 if (cnvrt2 (datestr+4, &val) ||
184 val > 23) {
185 break;
186 }
187 tmp->tm_hour = val;
188
189 if (cnvrt2 (datestr+6, &val) ||
190 val > 59) {
191 break;
192 }
193 tmp->tm_min = val;
194
195 /* calculate day of week */
199e87c3 196 rtc_calc_weekday(tmp);
3863585b
WD
197
198 return (0);
199 default:
200 break;
201 }
202
203 return (-1);
204}
205
8bde7f77
WD
206/***************************************************/
207
0d498393
WD
208U_BOOT_CMD(
209 date, 2, 1, do_date,
2fb2604d 210 "get/set/reset date & time",
8bde7f77
WD
211 "[MMDDhhmm[[CC]YY][.ss]]\ndate reset\n"
212 " - without arguments: print date & time\n"
213 " - with numeric argument: set the system date & time\n"
a89c33db 214 " - with 'reset' argument: reset the RTC"
8bde7f77 215);
This page took 0.625743 seconds and 5 git commands to generate.