Blame view

cmd/date.c 4.81 KB
83d290c56   Tom Rini   SPDX: Convert all...
1
  // SPDX-License-Identifier: GPL-2.0+
3863585bb   wdenk   Initial revision
2
3
4
  /*
   * (C) Copyright 2001
   * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
3863585bb   wdenk   Initial revision
5
6
7
8
9
10
11
   */
  
  /*
   * RTC, Date & Time support: get and set date & time
   */
  #include <common.h>
  #include <command.h>
f9951eadb   Simon Glass   dm: rtc: Convert ...
12
  #include <dm.h>
3863585bb   wdenk   Initial revision
13
  #include <rtc.h>
0dc018ece   Stefan Roese   [PATCH] I2C: Add ...
14
  #include <i2c.h>
3863585bb   wdenk   Initial revision
15

d87080b72   Wolfgang Denk   GCC-4.x fixes: cl...
16
  DECLARE_GLOBAL_DATA_PTR;
bdbc1303c   Mike Frysinger   cmd_date: constify
17
  static const char * const weekdays[] = {
3863585bb   wdenk   Initial revision
18
19
  	"Sun", "Mon", "Tues", "Wednes", "Thurs", "Fri", "Satur",
  };
2e5167cca   Wolfgang Denk   Replace CONFIG_RE...
20
  #ifdef CONFIG_NEEDS_MANUAL_RELOC
3e38691e8   wdenk   * Patch by Arun D...
21
  #define RELOC(a)	((typeof(a))((unsigned long)(a) + gd->reloc_off))
2e5167cca   Wolfgang Denk   Replace CONFIG_RE...
22
23
  #else
  #define RELOC(a)	a
521af04d8   Peter Tyser   Conditionally per...
24
  #endif
3e38691e8   wdenk   * Patch by Arun D...
25

bdbc1303c   Mike Frysinger   cmd_date: constify
26
  int mk_date (const char *, struct rtc_time *);
3863585bb   wdenk   Initial revision
27

1a1fa2406   Marek Vasut   rtc: Set valid da...
28
  static struct rtc_time default_tm = { 0, 0, 0, 1, 1, 2000, 6, 0, 0 };
088f1b199   Kim Phillips   common/cmd_*.c: s...
29
  static int do_date(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
3863585bb   wdenk   Initial revision
30
31
32
  {
  	struct rtc_time tm;
  	int rcode = 0;
f9951eadb   Simon Glass   dm: rtc: Convert ...
33
  	int old_bus __maybe_unused;
0dc018ece   Stefan Roese   [PATCH] I2C: Add ...
34
35
  
  	/* switch to correct I2C bus */
ffe387988   Bin Meng   cmd: date: Change...
36
  #ifdef CONFIG_DM_RTC
f9951eadb   Simon Glass   dm: rtc: Convert ...
37
38
39
40
41
42
43
44
45
  	struct udevice *dev;
  
  	rcode = uclass_get_device(UCLASS_RTC, 0, &dev);
  	if (rcode) {
  		printf("Cannot find RTC: err=%d
  ", rcode);
  		return CMD_RET_FAILURE;
  	}
  #elif defined(CONFIG_SYS_I2C)
3f4978c71   Heiko Schocher   i2c: common chang...
46
47
48
  	old_bus = i2c_get_bus_num();
  	i2c_set_bus_num(CONFIG_SYS_RTC_BUS_NUM);
  #else
0dc018ece   Stefan Roese   [PATCH] I2C: Add ...
49
  	old_bus = I2C_GET_BUS();
6d0f6bcf3   Jean-Christophe PLAGNIOL-VILLARD   rename CFG_ macro...
50
  	I2C_SET_BUS(CONFIG_SYS_RTC_BUS_NUM);
3f4978c71   Heiko Schocher   i2c: common chang...
51
  #endif
3863585bb   wdenk   Initial revision
52
53
54
55
  
  	switch (argc) {
  	case 2:			/* set date & time */
  		if (strcmp(argv[1],"reset") == 0) {
4b9206ed5   wdenk   * Patches by Thom...
56
57
  			puts ("Reset RTC...
  ");
ffe387988   Bin Meng   cmd: date: Change...
58
  #ifdef CONFIG_DM_RTC
f9951eadb   Simon Glass   dm: rtc: Convert ...
59
60
61
62
63
  			rcode = dm_rtc_reset(dev);
  			if (!rcode)
  				rcode = dm_rtc_set(dev, &default_tm);
  #else
  			rtc_reset();
1a1fa2406   Marek Vasut   rtc: Set valid da...
64
  			rcode = rtc_set(&default_tm);
f9951eadb   Simon Glass   dm: rtc: Convert ...
65
  #endif
1a1fa2406   Marek Vasut   rtc: Set valid da...
66
67
68
  			if (rcode)
  				puts("## Failed to set date after RTC reset
  ");
3863585bb   wdenk   Initial revision
69
70
  		} else {
  			/* initialize tm with current time */
ffe387988   Bin Meng   cmd: date: Change...
71
  #ifdef CONFIG_DM_RTC
f9951eadb   Simon Glass   dm: rtc: Convert ...
72
73
74
75
76
  			rcode = dm_rtc_get(dev, &tm);
  #else
  			rcode = rtc_get(&tm);
  #endif
  			if (!rcode) {
d1e231941   Jean-Christophe PLAGNIOL-VILLARD   rtc: allow rtc_se...
77
  				/* insert new date & time */
f9951eadb   Simon Glass   dm: rtc: Convert ...
78
  				if (mk_date(argv[1], &tm) != 0) {
d1e231941   Jean-Christophe PLAGNIOL-VILLARD   rtc: allow rtc_se...
79
80
81
82
83
  					puts ("## Bad date format
  ");
  					break;
  				}
  				/* and write to RTC */
ffe387988   Bin Meng   cmd: date: Change...
84
  #ifdef CONFIG_DM_RTC
f9951eadb   Simon Glass   dm: rtc: Convert ...
85
86
87
88
89
90
91
92
93
  				rcode = dm_rtc_set(dev, &tm);
  #else
  				rcode = rtc_set(&tm);
  #endif
  				if (rcode) {
  					printf("## Set date failed: err=%d
  ",
  					       rcode);
  				}
d1e231941   Jean-Christophe PLAGNIOL-VILLARD   rtc: allow rtc_se...
94
  			} else {
d52e3e017   Magnus Lilja   cmd_date: Fix spe...
95
96
  				puts("## Get date failed
  ");
3863585bb   wdenk   Initial revision
97
  			}
3863585bb   wdenk   Initial revision
98
99
100
  		}
  		/* FALL TROUGH */
  	case 1:			/* get date & time */
ffe387988   Bin Meng   cmd: date: Change...
101
  #ifdef CONFIG_DM_RTC
f9951eadb   Simon Glass   dm: rtc: Convert ...
102
103
104
105
  		rcode = dm_rtc_get(dev, &tm);
  #else
  		rcode = rtc_get(&tm);
  #endif
d1e231941   Jean-Christophe PLAGNIOL-VILLARD   rtc: allow rtc_se...
106
  		if (rcode) {
d52e3e017   Magnus Lilja   cmd_date: Fix spe...
107
108
  			puts("## Get date failed
  ");
d1e231941   Jean-Christophe PLAGNIOL-VILLARD   rtc: allow rtc_se...
109
110
  			break;
  		}
3863585bb   wdenk   Initial revision
111
112
113
114
115
  
  		printf ("Date: %4d-%02d-%02d (%sday)    Time: %2d:%02d:%02d
  ",
  			tm.tm_year, tm.tm_mon, tm.tm_mday,
  			(tm.tm_wday<0 || tm.tm_wday>6) ?
3e38691e8   wdenk   * Patch by Arun D...
116
  				"unknown " : RELOC(weekdays[tm.tm_wday]),
3863585bb   wdenk   Initial revision
117
  			tm.tm_hour, tm.tm_min, tm.tm_sec);
0dc018ece   Stefan Roese   [PATCH] I2C: Add ...
118
  		break;
3863585bb   wdenk   Initial revision
119
  	default:
4c12eeb8b   Simon Glass   Convert cmd_usage...
120
  		rcode = CMD_RET_USAGE;
3863585bb   wdenk   Initial revision
121
  	}
0dc018ece   Stefan Roese   [PATCH] I2C: Add ...
122
123
  
  	/* switch back to original I2C bus */
3f4978c71   Heiko Schocher   i2c: common chang...
124
125
  #ifdef CONFIG_SYS_I2C
  	i2c_set_bus_num(old_bus);
ffe387988   Bin Meng   cmd: date: Change...
126
  #elif !defined(CONFIG_DM_RTC)
0dc018ece   Stefan Roese   [PATCH] I2C: Add ...
127
  	I2C_SET_BUS(old_bus);
3f4978c71   Heiko Schocher   i2c: common chang...
128
  #endif
0dc018ece   Stefan Roese   [PATCH] I2C: Add ...
129

f9951eadb   Simon Glass   dm: rtc: Convert ...
130
  	return rcode ? CMD_RET_FAILURE : 0;
3863585bb   wdenk   Initial revision
131
132
133
134
135
  }
  
  /*
   * simple conversion of two-digit string with error checking
   */
bdbc1303c   Mike Frysinger   cmd_date: constify
136
  static int cnvrt2 (const char *str, int *valp)
3863585bb   wdenk   Initial revision
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
  {
  	int val;
  
  	if ((*str < '0') || (*str > '9'))
  		return (-1);
  
  	val = *str - '0';
  
  	++str;
  
  	if ((*str < '0') || (*str > '9'))
  		return (-1);
  
  	*valp = 10 * val + (*str - '0');
  
  	return (0);
  }
  
  /*
   * Convert date string: MMDDhhmm[[CC]YY][.ss]
   *
   * Some basic checking for valid values is done, but this will not catch
   * all possible error conditions.
   */
bdbc1303c   Mike Frysinger   cmd_date: constify
161
  int mk_date (const char *datestr, struct rtc_time *tmp)
3863585bb   wdenk   Initial revision
162
163
164
  {
  	int len, val;
  	char *ptr;
44ac80e7e   Roman Kapl   cmd: date: Do not...
165
166
  	ptr = strchr(datestr, '.');
  	len = strlen(datestr);
3863585bb   wdenk   Initial revision
167
168
169
170
  
  	/* Set seconds */
  	if (ptr) {
  		int sec;
44ac80e7e   Roman Kapl   cmd: date: Do not...
171
  		ptr++;
3863585bb   wdenk   Initial revision
172
173
  		if ((len - (ptr - datestr)) != 2)
  			return (-1);
44ac80e7e   Roman Kapl   cmd: date: Do not...
174
  		len -= 3;
3863585bb   wdenk   Initial revision
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
  
  		if (cnvrt2 (ptr, &sec))
  			return (-1);
  
  		tmp->tm_sec = sec;
  	} else {
  		tmp->tm_sec = 0;
  	}
  
  	if (len == 12) {		/* MMDDhhmmCCYY	*/
  		int year, century;
  
  		if (cnvrt2 (datestr+ 8, &century) ||
  		    cnvrt2 (datestr+10, &year) ) {
  			return (-1);
  		}
  		tmp->tm_year = 100 * century + year;
  	} else if (len == 10) {		/* MMDDhhmmYY	*/
  		int year, century;
  
  		century = tmp->tm_year / 100;
  		if (cnvrt2 (datestr+ 8, &year))
  			return (-1);
  		tmp->tm_year = 100 * century + year;
  	}
  
  	switch (len) {
  	case 8:			/* MMDDhhmm	*/
  		/* fall thru */
  	case 10:		/* MMDDhhmmYY	*/
  		/* fall thru */
  	case 12:		/* MMDDhhmmCCYY	*/
  		if (cnvrt2 (datestr+0, &val) ||
  		    val > 12) {
  			break;
  		}
  		tmp->tm_mon  = val;
  		if (cnvrt2 (datestr+2, &val) ||
  		    val > ((tmp->tm_mon==2) ? 29 : 31)) {
  			break;
  		}
  		tmp->tm_mday = val;
  
  		if (cnvrt2 (datestr+4, &val) ||
  		    val > 23) {
  			break;
  		}
  		tmp->tm_hour = val;
  
  		if (cnvrt2 (datestr+6, &val) ||
  		    val > 59) {
  			break;
  		}
  		tmp->tm_min  = val;
  
  		/* calculate day of week */
199e87c34   Simon Glass   dm: rtc: Rename g...
231
  		rtc_calc_weekday(tmp);
3863585bb   wdenk   Initial revision
232
233
234
235
236
237
238
239
  
  		return (0);
  	default:
  		break;
  	}
  
  	return (-1);
  }
8bde7f776   wdenk   * Code cleanup:
240
  /***************************************************/
0d4983930   wdenk   Patch by Kenneth ...
241
242
  U_BOOT_CMD(
  	date,	2,	1,	do_date,
2fb2604d5   Peter Tyser   Command usage cle...
243
  	"get/set/reset date & time",
8bde7f776   wdenk   * Code cleanup:
244
245
246
247
248
249
250
  	"[MMDDhhmm[[CC]YY][.ss]]
  date reset
  "
  	"  - without arguments: print date & time
  "
  	"  - with numeric argument: set the system date & time
  "
a89c33db9   Wolfgang Denk   General help mess...
251
  	"  - with 'reset' argument: reset the RTC"
8bde7f776   wdenk   * Code cleanup:
252
  );