Blame view
cmd/date.c
4.83 KB
3863585bb Initial revision |
1 2 3 4 |
/* * (C) Copyright 2001 * Wolfgang Denk, DENX Software Engineering, wd@denx.de. * |
1a4596601 Add GPL-2.0+ SPDX... |
5 |
* SPDX-License-Identifier: GPL-2.0+ |
3863585bb Initial revision |
6 7 8 9 10 11 12 |
*/ /* * RTC, Date & Time support: get and set date & time */ #include <common.h> #include <command.h> |
f9951eadb dm: rtc: Convert ... |
13 |
#include <dm.h> |
3863585bb Initial revision |
14 |
#include <rtc.h> |
0dc018ece [PATCH] I2C: Add ... |
15 |
#include <i2c.h> |
3863585bb Initial revision |
16 |
|
d87080b72 GCC-4.x fixes: cl... |
17 |
DECLARE_GLOBAL_DATA_PTR; |
bdbc1303c cmd_date: constify |
18 |
static const char * const weekdays[] = { |
3863585bb Initial revision |
19 20 |
"Sun", "Mon", "Tues", "Wednes", "Thurs", "Fri", "Satur", }; |
2e5167cca Replace CONFIG_RE... |
21 |
#ifdef CONFIG_NEEDS_MANUAL_RELOC |
3e38691e8 * Patch by Arun D... |
22 |
#define RELOC(a) ((typeof(a))((unsigned long)(a) + gd->reloc_off)) |
2e5167cca Replace CONFIG_RE... |
23 24 |
#else #define RELOC(a) a |
521af04d8 Conditionally per... |
25 |
#endif |
3e38691e8 * Patch by Arun D... |
26 |
|
bdbc1303c cmd_date: constify |
27 |
int mk_date (const char *, struct rtc_time *); |
3863585bb Initial revision |
28 |
|
1a1fa2406 rtc: Set valid da... |
29 |
static struct rtc_time default_tm = { 0, 0, 0, 1, 1, 2000, 6, 0, 0 }; |
088f1b199 common/cmd_*.c: s... |
30 |
static int do_date(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
3863585bb Initial revision |
31 32 33 |
{ struct rtc_time tm; int rcode = 0; |
f9951eadb dm: rtc: Convert ... |
34 |
int old_bus __maybe_unused; |
0dc018ece [PATCH] I2C: Add ... |
35 36 |
/* switch to correct I2C bus */ |
ffe387988 cmd: date: Change... |
37 |
#ifdef CONFIG_DM_RTC |
f9951eadb dm: rtc: Convert ... |
38 39 40 41 42 43 44 45 46 |
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 i2c: common chang... |
47 48 49 |
old_bus = i2c_get_bus_num(); i2c_set_bus_num(CONFIG_SYS_RTC_BUS_NUM); #else |
0dc018ece [PATCH] I2C: Add ... |
50 |
old_bus = I2C_GET_BUS(); |
6d0f6bcf3 rename CFG_ macro... |
51 |
I2C_SET_BUS(CONFIG_SYS_RTC_BUS_NUM); |
3f4978c71 i2c: common chang... |
52 |
#endif |
3863585bb Initial revision |
53 54 55 56 |
switch (argc) { case 2: /* set date & time */ if (strcmp(argv[1],"reset") == 0) { |
4b9206ed5 * Patches by Thom... |
57 58 |
puts ("Reset RTC... "); |
ffe387988 cmd: date: Change... |
59 |
#ifdef CONFIG_DM_RTC |
f9951eadb dm: rtc: Convert ... |
60 61 62 63 64 |
rcode = dm_rtc_reset(dev); if (!rcode) rcode = dm_rtc_set(dev, &default_tm); #else rtc_reset(); |
1a1fa2406 rtc: Set valid da... |
65 |
rcode = rtc_set(&default_tm); |
f9951eadb dm: rtc: Convert ... |
66 |
#endif |
1a1fa2406 rtc: Set valid da... |
67 68 69 |
if (rcode) puts("## Failed to set date after RTC reset "); |
3863585bb Initial revision |
70 71 |
} else { /* initialize tm with current time */ |
ffe387988 cmd: date: Change... |
72 |
#ifdef CONFIG_DM_RTC |
f9951eadb dm: rtc: Convert ... |
73 74 75 76 77 |
rcode = dm_rtc_get(dev, &tm); #else rcode = rtc_get(&tm); #endif if (!rcode) { |
d1e231941 rtc: allow rtc_se... |
78 |
/* insert new date & time */ |
f9951eadb dm: rtc: Convert ... |
79 |
if (mk_date(argv[1], &tm) != 0) { |
d1e231941 rtc: allow rtc_se... |
80 81 82 83 84 |
puts ("## Bad date format "); break; } /* and write to RTC */ |
ffe387988 cmd: date: Change... |
85 |
#ifdef CONFIG_DM_RTC |
f9951eadb dm: rtc: Convert ... |
86 87 88 89 90 91 92 93 94 |
rcode = dm_rtc_set(dev, &tm); #else rcode = rtc_set(&tm); #endif if (rcode) { printf("## Set date failed: err=%d ", rcode); } |
d1e231941 rtc: allow rtc_se... |
95 |
} else { |
d52e3e017 cmd_date: Fix spe... |
96 97 |
puts("## Get date failed "); |
3863585bb Initial revision |
98 |
} |
3863585bb Initial revision |
99 100 101 |
} /* FALL TROUGH */ case 1: /* get date & time */ |
ffe387988 cmd: date: Change... |
102 |
#ifdef CONFIG_DM_RTC |
f9951eadb dm: rtc: Convert ... |
103 104 105 106 |
rcode = dm_rtc_get(dev, &tm); #else rcode = rtc_get(&tm); #endif |
d1e231941 rtc: allow rtc_se... |
107 |
if (rcode) { |
d52e3e017 cmd_date: Fix spe... |
108 109 |
puts("## Get date failed "); |
d1e231941 rtc: allow rtc_se... |
110 111 |
break; } |
3863585bb Initial revision |
112 113 114 115 116 |
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 * Patch by Arun D... |
117 |
"unknown " : RELOC(weekdays[tm.tm_wday]), |
3863585bb Initial revision |
118 |
tm.tm_hour, tm.tm_min, tm.tm_sec); |
0dc018ece [PATCH] I2C: Add ... |
119 |
break; |
3863585bb Initial revision |
120 |
default: |
4c12eeb8b Convert cmd_usage... |
121 |
rcode = CMD_RET_USAGE; |
3863585bb Initial revision |
122 |
} |
0dc018ece [PATCH] I2C: Add ... |
123 124 |
/* switch back to original I2C bus */ |
3f4978c71 i2c: common chang... |
125 126 |
#ifdef CONFIG_SYS_I2C i2c_set_bus_num(old_bus); |
ffe387988 cmd: date: Change... |
127 |
#elif !defined(CONFIG_DM_RTC) |
0dc018ece [PATCH] I2C: Add ... |
128 |
I2C_SET_BUS(old_bus); |
3f4978c71 i2c: common chang... |
129 |
#endif |
0dc018ece [PATCH] I2C: Add ... |
130 |
|
f9951eadb dm: rtc: Convert ... |
131 |
return rcode ? CMD_RET_FAILURE : 0; |
3863585bb Initial revision |
132 133 134 135 136 |
} /* * simple conversion of two-digit string with error checking */ |
bdbc1303c cmd_date: constify |
137 |
static int cnvrt2 (const char *str, int *valp) |
3863585bb Initial revision |
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
{ 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 cmd_date: constify |
162 |
int mk_date (const char *datestr, struct rtc_time *tmp) |
3863585bb Initial revision |
163 164 165 166 167 168 169 170 171 172 173 174 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 231 232 233 234 |
{ int len, val; char *ptr; ptr = strchr (datestr,'.'); len = strlen (datestr); /* Set seconds */ if (ptr) { int sec; *ptr++ = '\0'; if ((len - (ptr - datestr)) != 2) return (-1); len = strlen (datestr); 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, ¢ury) || 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 dm: rtc: Rename g... |
235 |
rtc_calc_weekday(tmp); |
3863585bb Initial revision |
236 237 238 239 240 241 242 243 |
return (0); default: break; } return (-1); } |
8bde7f776 * Code cleanup: |
244 |
/***************************************************/ |
0d4983930 Patch by Kenneth ... |
245 246 |
U_BOOT_CMD( date, 2, 1, do_date, |
2fb2604d5 Command usage cle... |
247 |
"get/set/reset date & time", |
8bde7f776 * Code cleanup: |
248 249 250 251 252 253 254 |
"[MMDDhhmm[[CC]YY][.ss]] date reset " " - without arguments: print date & time " " - with numeric argument: set the system date & time " |
a89c33db9 General help mess... |
255 |
" - with 'reset' argument: reset the RTC" |
8bde7f776 * Code cleanup: |
256 |
); |