blob: 3f531555b81ea8bfb50ea853a33aef85db2d82d9 [file] [log] [blame]
Robert Griebl1cd04452002-07-21 16:50:49 +00001/* vi: set sw=4 ts=4: */
2/*
Robert Griebl6859d762002-08-05 02:57:12 +00003 * Mini hwclock implementation for busybox
4 *
Robert Griebl1cd04452002-07-21 16:50:49 +00005 * Copyright (C) 2002 Robert Griebl <griebl@gmx.de>
6 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Robert Griebl6859d762002-08-05 02:57:12 +00008*/
Robert Griebl1cd04452002-07-21 16:50:49 +00009
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000010#include "libbb.h"
Denys Vlasenko043b1e52009-09-06 12:47:55 +020011/* After libbb.h, since it needs sys/types.h on some systems */
12#include <sys/utsname.h>
Mike Frysinger6b160e42008-02-15 02:27:19 +000013#include "rtc_.h"
Eric Andersen8882ea52002-12-11 03:41:28 +000014
Denys Vlasenko6959f6b2010-01-07 08:31:46 +010015/* diff code is disabled: it's not sys/hw clock diff, it's some useless
16 * "time between hwclock was started and we saw CMOS tick" quantity.
17 * It's useless since hwclock is started at a random moment,
18 * thus the quantity is also random, useless. Showing 0.000000 does not
Denys Vlasenko0c58cc72010-01-07 10:36:41 +010019 * deprive us from any useful info.
20 *
21 * SHOW_HWCLOCK_DIFF code in this file shows the difference between system
22 * and hw clock. It is useful, but not compatible with standard hwclock.
23 * Thus disabled.
24 */
Denys Vlasenko6959f6b2010-01-07 08:31:46 +010025#define SHOW_HWCLOCK_DIFF 0
26
27
28#if !SHOW_HWCLOCK_DIFF
29# define read_rtc(pp_rtcname, sys_tv, utc) read_rtc(pp_rtcname, utc)
30#endif
31static time_t read_rtc(const char **pp_rtcname, struct timeval *sys_tv, int utc)
Denis Vlasenko92258542006-11-01 10:25:35 +000032{
Denys Vlasenkodc698bb2010-01-09 19:10:49 +010033 struct tm tm_time;
Mike Frysinger6b160e42008-02-15 02:27:19 +000034 int fd;
Robert Griebl1cd04452002-07-21 16:50:49 +000035
Denys Vlasenko6959f6b2010-01-07 08:31:46 +010036 fd = rtc_xopen(pp_rtcname, O_RDONLY);
Denys Vlasenko5e3b1402010-01-06 22:43:39 +010037
Denys Vlasenkodc698bb2010-01-09 19:10:49 +010038 rtc_read_tm(&tm_time, fd);
Denys Vlasenko6959f6b2010-01-07 08:31:46 +010039
40#if SHOW_HWCLOCK_DIFF
Denys Vlasenko0c58cc72010-01-07 10:36:41 +010041 {
Denys Vlasenkodc698bb2010-01-09 19:10:49 +010042 int before = tm_time.tm_sec;
Denys Vlasenko0c58cc72010-01-07 10:36:41 +010043 while (1) {
Denys Vlasenkodc698bb2010-01-09 19:10:49 +010044 rtc_read_tm(&tm_time, fd);
Denys Vlasenko0c58cc72010-01-07 10:36:41 +010045 gettimeofday(sys_tv, NULL);
Denys Vlasenkod778e6c2012-04-17 19:25:13 +020046 if (before != (int)tm_time.tm_sec)
Denys Vlasenko0c58cc72010-01-07 10:36:41 +010047 break;
48 }
Denys Vlasenko5e3b1402010-01-06 22:43:39 +010049 }
Denys Vlasenko6959f6b2010-01-07 08:31:46 +010050#endif
Denys Vlasenko5e3b1402010-01-06 22:43:39 +010051
Denys Vlasenko695fa512010-01-06 18:16:39 +010052 if (ENABLE_FEATURE_CLEAN_UP)
53 close(fd);
Eric Andersenc7bda1c2004-03-15 08:29:22 +000054
Denys Vlasenkodc698bb2010-01-09 19:10:49 +010055 return rtc_tm2time(&tm_time, utc);
Robert Griebl1cd04452002-07-21 16:50:49 +000056}
57
Denys Vlasenko6959f6b2010-01-07 08:31:46 +010058static void show_clock(const char **pp_rtcname, int utc)
Robert Griebl1cd04452002-07-21 16:50:49 +000059{
Denys Vlasenko0c58cc72010-01-07 10:36:41 +010060#if SHOW_HWCLOCK_DIFF
61 struct timeval sys_tv;
62#endif
Denys Vlasenko319b8bb2011-07-08 06:40:25 +020063 time_t t = read_rtc(pp_rtcname, &sys_tv, utc);
Robert Griebl1cd04452002-07-21 16:50:49 +000064
Denys Vlasenko319b8bb2011-07-08 06:40:25 +020065#if ENABLE_LOCALE_SUPPORT
66 /* Standard hwclock uses locale-specific output format */
67 char cp[64];
68 struct tm *ptm = localtime(&t);
69 strftime(cp, sizeof(cp), "%c", ptm);
70#else
71 char *cp = ctime(&t);
Denys Vlasenko695fa512010-01-06 18:16:39 +010072 strchrnul(cp, '\n')[0] = '\0';
Denys Vlasenko319b8bb2011-07-08 06:40:25 +020073#endif
74
Denys Vlasenko0c58cc72010-01-07 10:36:41 +010075#if !SHOW_HWCLOCK_DIFF
Denys Vlasenko6959f6b2010-01-07 08:31:46 +010076 printf("%s 0.000000 seconds\n", cp);
Denys Vlasenko0c58cc72010-01-07 10:36:41 +010077#else
78 {
79 long diff = sys_tv.tv_sec - t;
80 if (diff < 0 /*&& tv.tv_usec != 0*/) {
Denys Vlasenko319b8bb2011-07-08 06:40:25 +020081 /* Why we need diff++? */
82 /* diff >= 0 is ok: | diff < 0, can't just use tv.tv_usec: */
83 /* 45.520820 | 43.520820 */
84 /* - 44.000000 | - 45.000000 */
85 /* = 1.520820 | = -1.479180, not -2.520820! */
Denys Vlasenko0c58cc72010-01-07 10:36:41 +010086 diff++;
Denys Vlasenko319b8bb2011-07-08 06:40:25 +020087 /* Should be 1000000 - tv.tv_usec, but then we must check tv.tv_usec != 0 */
Denys Vlasenko0c58cc72010-01-07 10:36:41 +010088 sys_tv.tv_usec = 999999 - sys_tv.tv_usec;
89 }
90 printf("%s %ld.%06lu seconds\n", cp, diff, (unsigned long)sys_tv.tv_usec);
Denys Vlasenko5e3b1402010-01-06 22:43:39 +010091 }
Denys Vlasenko6959f6b2010-01-07 08:31:46 +010092#endif
Robert Griebl1cd04452002-07-21 16:50:49 +000093}
94
Denys Vlasenko6959f6b2010-01-07 08:31:46 +010095static void to_sys_clock(const char **pp_rtcname, int utc)
Robert Griebl1cd04452002-07-21 16:50:49 +000096{
Denis Vlasenko459be352007-06-17 19:09:05 +000097 struct timeval tv;
Denys Vlasenko043b1e52009-09-06 12:47:55 +020098 struct timezone tz;
99
Denys Vlasenko589051b2014-02-25 17:52:10 +0100100 tz.tz_minuteswest = timezone/60;
101 /* ^^^ used to also subtract 60*daylight, but it's wrong:
102 * daylight!=0 means "this timezone has some DST
103 * during the year", not "DST is in effect now".
104 */
Denys Vlasenko043b1e52009-09-06 12:47:55 +0200105 tz.tz_dsttime = 0;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000106
Denys Vlasenko6959f6b2010-01-07 08:31:46 +0100107 tv.tv_sec = read_rtc(pp_rtcname, NULL, utc);
Denis Vlasenko459be352007-06-17 19:09:05 +0000108 tv.tv_usec = 0;
Denis Vlasenko92258542006-11-01 10:25:35 +0000109 if (settimeofday(&tv, &tz))
Denys Vlasenko6959f6b2010-01-07 08:31:46 +0100110 bb_perror_msg_and_die("settimeofday");
Robert Griebl1cd04452002-07-21 16:50:49 +0000111}
112
Denys Vlasenko6959f6b2010-01-07 08:31:46 +0100113static void from_sys_clock(const char **pp_rtcname, int utc)
Robert Griebl1cd04452002-07-21 16:50:49 +0000114{
Denys Vlasenko60f659f2010-04-14 09:19:20 -0700115#if 1
116 struct timeval tv;
117 struct tm tm_time;
118 int rtc;
119
120 rtc = rtc_xopen(pp_rtcname, O_WRONLY);
121 gettimeofday(&tv, NULL);
122 /* Prepare tm_time */
123 if (sizeof(time_t) == sizeof(tv.tv_sec)) {
124 if (utc)
125 gmtime_r((time_t*)&tv.tv_sec, &tm_time);
126 else
127 localtime_r((time_t*)&tv.tv_sec, &tm_time);
128 } else {
129 time_t t = tv.tv_sec;
130 if (utc)
131 gmtime_r(&t, &tm_time);
132 else
133 localtime_r(&t, &tm_time);
134 }
135#else
136/* Bloated code which tries to set hw clock with better precision.
137 * On x86, even though code does set hw clock within <1ms of exact
138 * whole seconds, apparently hw clock (at least on some machines)
139 * doesn't reset internal fractional seconds to 0,
140 * making all this a pointless excercise.
141 */
142 /* If we see that we are N usec away from whole second,
143 * we'll sleep for N-ADJ usecs. ADJ corrects for the fact
144 * that CPU is not infinitely fast.
145 * On infinitely fast CPU, next wakeup would be
146 * on (exactly_next_whole_second - ADJ). On real CPUs,
147 * this difference between current time and whole second
148 * is less than ADJ (assuming system isn't heavily loaded).
149 */
150 /* Small value of 256us gives very precise sync for 2+ GHz CPUs.
151 * Slower CPUs will fail to sync and will go to bigger
152 * ADJ values. qemu-emulated armv4tl with ~100 MHz
153 * performance ends up using ADJ ~= 4*1024 and it takes
154 * 2+ secs (2 tries with successively larger ADJ)
155 * to sync. Even straced one on the same qemu (very slow)
156 * takes only 4 tries.
157 */
158#define TWEAK_USEC 256
159 unsigned adj = TWEAK_USEC;
Denys Vlasenkodc698bb2010-01-09 19:10:49 +0100160 struct tm tm_time;
Denis Vlasenko459be352007-06-17 19:09:05 +0000161 struct timeval tv;
Denys Vlasenko6959f6b2010-01-07 08:31:46 +0100162 int rtc = rtc_xopen(pp_rtcname, O_WRONLY);
Robert Griebl1cd04452002-07-21 16:50:49 +0000163
Denys Vlasenko695fa512010-01-06 18:16:39 +0100164 /* Try to catch the moment when whole second is close */
165 while (1) {
166 unsigned rem_usec;
167 time_t t;
168
169 gettimeofday(&tv, NULL);
170
Denys Vlasenko5e3b1402010-01-06 22:43:39 +0100171 t = tv.tv_sec;
Denys Vlasenko695fa512010-01-06 18:16:39 +0100172 rem_usec = 1000000 - tv.tv_usec;
Denys Vlasenko60f659f2010-04-14 09:19:20 -0700173 if (rem_usec < adj) {
174 /* Close enough */
Denys Vlasenko695fa512010-01-06 18:16:39 +0100175 small_rem:
Denys Vlasenko5e3b1402010-01-06 22:43:39 +0100176 t++;
Denys Vlasenko695fa512010-01-06 18:16:39 +0100177 }
178
Denys Vlasenko60f659f2010-04-14 09:19:20 -0700179 /* Prepare tm_time from t */
Denys Vlasenko695fa512010-01-06 18:16:39 +0100180 if (utc)
Denys Vlasenkodc698bb2010-01-09 19:10:49 +0100181 gmtime_r(&t, &tm_time); /* may read /etc/xxx (it takes time) */
Denys Vlasenko695fa512010-01-06 18:16:39 +0100182 else
Denys Vlasenkodc698bb2010-01-09 19:10:49 +0100183 localtime_r(&t, &tm_time); /* same */
Denys Vlasenko60f659f2010-04-14 09:19:20 -0700184
185 if (adj >= 32*1024) {
186 break; /* 32 ms diff and still no luck?? give up trying to sync */
187 }
Denys Vlasenko695fa512010-01-06 18:16:39 +0100188
189 /* gmtime/localtime took some time, re-get cur time */
190 gettimeofday(&tv, NULL);
191
Denys Vlasenko60f659f2010-04-14 09:19:20 -0700192 if (tv.tv_sec < t /* we are still in old second */
193 || (tv.tv_sec == t && tv.tv_usec < adj) /* not too far into next second */
Denys Vlasenko695fa512010-01-06 18:16:39 +0100194 ) {
Denys Vlasenko60f659f2010-04-14 09:19:20 -0700195 break; /* good, we are in sync! */
Denys Vlasenko695fa512010-01-06 18:16:39 +0100196 }
Denys Vlasenko60f659f2010-04-14 09:19:20 -0700197
198 rem_usec = 1000000 - tv.tv_usec;
199 if (rem_usec < adj) {
200 t = tv.tv_sec;
201 goto small_rem; /* already close to next sec, don't sleep */
Denys Vlasenko695fa512010-01-06 18:16:39 +0100202 }
203
204 /* Try to sync up by sleeping */
Denys Vlasenko695fa512010-01-06 18:16:39 +0100205 usleep(rem_usec - adj);
Denys Vlasenko60f659f2010-04-14 09:19:20 -0700206
207 /* Jump to 1ms diff, then increase fast (x2): EVERY loop
208 * takes ~1 sec, people won't like slowly converging code here!
209 */
210 //bb_error_msg("adj:%d tv.tv_usec:%d", adj, (int)tv.tv_usec);
211 if (adj < 512)
212 adj = 512;
213 /* ... and if last "overshoot" does not look insanely big,
214 * just use it as adj increment. This makes convergence faster.
215 */
216 if (tv.tv_usec < adj * 8) {
217 adj += tv.tv_usec;
218 continue;
219 }
220 adj *= 2;
Denys Vlasenko695fa512010-01-06 18:16:39 +0100221 }
Denys Vlasenko60f659f2010-04-14 09:19:20 -0700222 /* Debug aid to find "optimal" TWEAK_USEC with nearly exact sync.
Denys Vlasenko695fa512010-01-06 18:16:39 +0100223 * Look for a value which makes tv_usec close to 999999 or 0.
Denys Vlasenko60f659f2010-04-14 09:19:20 -0700224 * For 2.20GHz Intel Core 2: optimal TWEAK_USEC ~= 200
Denys Vlasenko695fa512010-01-06 18:16:39 +0100225 */
Denys Vlasenko60f659f2010-04-14 09:19:20 -0700226 //bb_error_msg("tv.tv_usec:%d", (int)tv.tv_usec);
227#endif
228
229 tm_time.tm_isdst = 0;
230 xioctl(rtc, RTC_SET_TIME, &tm_time);
Denys Vlasenko695fa512010-01-06 18:16:39 +0100231
232 if (ENABLE_FEATURE_CLEAN_UP)
233 close(rtc);
Robert Griebl1cd04452002-07-21 16:50:49 +0000234}
235
Davide Cavalca658a4372011-01-22 18:55:32 +0100236/*
237 * At system boot, kernel may set system time from RTC,
238 * but it knows nothing about timezones. If RTC is in local time,
239 * then system time is wrong - it is offset by timezone.
240 * This option corrects system time if RTC is in local time,
241 * and (always) sets in-kernel timezone.
242 *
243 * This is an alternate option to --hctosys that does not read the
244 * hardware clock.
245 */
246static void set_system_clock_timezone(int utc)
247{
248 struct timeval tv;
249 struct tm *broken;
250 struct timezone tz;
251
252 gettimeofday(&tv, NULL);
253 broken = localtime(&tv.tv_sec);
254 tz.tz_minuteswest = timezone / 60;
Denys Vlasenko589051b2014-02-25 17:52:10 +0100255 if (broken->tm_isdst > 0)
Davide Cavalca658a4372011-01-22 18:55:32 +0100256 tz.tz_minuteswest -= 60;
257 tz.tz_dsttime = 0;
258 gettimeofday(&tv, NULL);
259 if (!utc)
260 tv.tv_sec += tz.tz_minuteswest * 60;
261 if (settimeofday(&tv, &tz))
262 bb_perror_msg_and_die("settimeofday");
263}
264
265//usage:#define hwclock_trivial_usage
266//usage: IF_FEATURE_HWCLOCK_LONG_OPTIONS(
267//usage: "[-r|--show] [-s|--hctosys] [-w|--systohc] [-t|--systz]"
268//usage: " [-l|--localtime] [-u|--utc]"
269//usage: " [-f|--rtc FILE]"
270//usage: )
271//usage: IF_NOT_FEATURE_HWCLOCK_LONG_OPTIONS(
272//usage: "[-r] [-s] [-w] [-t] [-l] [-u] [-f FILE]"
273//usage: )
274//usage:#define hwclock_full_usage "\n\n"
275//usage: "Query and set hardware clock (RTC)\n"
Davide Cavalca658a4372011-01-22 18:55:32 +0100276//usage: "\n -r Show hardware clock time"
277//usage: "\n -s Set system time from hardware clock"
278//usage: "\n -w Set hardware clock from system time"
279//usage: "\n -t Set in-kernel timezone, correct system time"
280//usage: "\n if hardware clock is in local time"
Denys Vlasenko46465ec2011-07-04 04:34:57 +0200281//usage: "\n -u Assume hardware clock is kept in UTC"
282//usage: "\n -l Assume hardware clock is kept in local time"
Davide Cavalca658a4372011-01-22 18:55:32 +0100283//usage: "\n -f FILE Use specified device (e.g. /dev/rtc2)"
284
Denis Vlasenko92258542006-11-01 10:25:35 +0000285#define HWCLOCK_OPT_LOCALTIME 0x01
286#define HWCLOCK_OPT_UTC 0x02
287#define HWCLOCK_OPT_SHOW 0x04
288#define HWCLOCK_OPT_HCTOSYS 0x08
289#define HWCLOCK_OPT_SYSTOHC 0x10
Davide Cavalca658a4372011-01-22 18:55:32 +0100290#define HWCLOCK_OPT_SYSTZ 0x20
291#define HWCLOCK_OPT_RTCFILE 0x40
Glenn L McGrath689e4b92004-02-22 09:11:33 +0000292
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000293int hwclock_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000294int hwclock_main(int argc UNUSED_PARAM, char **argv)
Robert Griebl1cd04452002-07-21 16:50:49 +0000295{
Denys Vlasenko6959f6b2010-01-07 08:31:46 +0100296 const char *rtcname = NULL;
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000297 unsigned opt;
Robert Griebl6bb80872004-03-22 21:27:39 +0000298 int utc;
Robert Griebl1cd04452002-07-21 16:50:49 +0000299
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +0000300#if ENABLE_FEATURE_HWCLOCK_LONG_OPTIONS
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000301 static const char hwclock_longopts[] ALIGN1 =
Davide Cavalca658a4372011-01-22 18:55:32 +0100302 "localtime\0" No_argument "l" /* short opt is non-standard */
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000303 "utc\0" No_argument "u"
304 "show\0" No_argument "r"
305 "hctosys\0" No_argument "s"
306 "systohc\0" No_argument "w"
Davide Cavalca658a4372011-01-22 18:55:32 +0100307 "systz\0" No_argument "t" /* short opt is non-standard */
308 "rtc\0" Required_argument "f"
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000309 ;
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000310 applet_long_options = hwclock_longopts;
Robert Griebl1cd04452002-07-21 16:50:49 +0000311#endif
Denys Vlasenko589051b2014-02-25 17:52:10 +0100312
313 /* Initialize "timezone" (libc global variable) */
314 tzset();
315
Davide Cavalca658a4372011-01-22 18:55:32 +0100316 opt_complementary = "r--wst:w--rst:s--wrt:t--rsw:l--u:u--l";
317 opt = getopt32(argv, "lurswtf:", &rtcname);
Robert Griebl1cd04452002-07-21 16:50:49 +0000318
Robert Griebl6bb80872004-03-22 21:27:39 +0000319 /* If -u or -l wasn't given check if we are using utc */
Mike Frysingerb31566e2005-04-16 04:48:48 +0000320 if (opt & (HWCLOCK_OPT_UTC | HWCLOCK_OPT_LOCALTIME))
Mike Frysinger6b160e42008-02-15 02:27:19 +0000321 utc = (opt & HWCLOCK_OPT_UTC);
Robert Griebl6bb80872004-03-22 21:27:39 +0000322 else
Mike Frysinger6b160e42008-02-15 02:27:19 +0000323 utc = rtc_adjtime_is_utc();
Mike Frysingerb31566e2005-04-16 04:48:48 +0000324
Mike Frysinger6b160e42008-02-15 02:27:19 +0000325 if (opt & HWCLOCK_OPT_HCTOSYS)
Denys Vlasenko6959f6b2010-01-07 08:31:46 +0100326 to_sys_clock(&rtcname, utc);
Mike Frysinger6b160e42008-02-15 02:27:19 +0000327 else if (opt & HWCLOCK_OPT_SYSTOHC)
Denys Vlasenko6959f6b2010-01-07 08:31:46 +0100328 from_sys_clock(&rtcname, utc);
Davide Cavalca658a4372011-01-22 18:55:32 +0100329 else if (opt & HWCLOCK_OPT_SYSTZ)
330 set_system_clock_timezone(utc);
Mike Frysinger6b160e42008-02-15 02:27:19 +0000331 else
332 /* default HWCLOCK_OPT_SHOW */
Denys Vlasenko6959f6b2010-01-07 08:31:46 +0100333 show_clock(&rtcname, utc);
Mike Frysinger6b160e42008-02-15 02:27:19 +0000334
Denis Vlasenko459be352007-06-17 19:09:05 +0000335 return 0;
Robert Griebl1cd04452002-07-21 16:50:49 +0000336}