blob: 54e97e5bbb8eb641a57e5594f472109cb3a57c7c [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 Vlasenkodc698bb2010-01-09 19:10:49 +010046 if (before != 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
Robert Griebl1cd04452002-07-21 16:50:49 +000063 time_t t;
Denis Vlasenko459be352007-06-17 19:09:05 +000064 char *cp;
Robert Griebl1cd04452002-07-21 16:50:49 +000065
Denys Vlasenko6959f6b2010-01-07 08:31:46 +010066 t = read_rtc(pp_rtcname, &sys_tv, utc);
Denis Vlasenko459be352007-06-17 19:09:05 +000067 cp = ctime(&t);
Denys Vlasenko695fa512010-01-06 18:16:39 +010068 strchrnul(cp, '\n')[0] = '\0';
Denys Vlasenko0c58cc72010-01-07 10:36:41 +010069#if !SHOW_HWCLOCK_DIFF
Denys Vlasenko6959f6b2010-01-07 08:31:46 +010070 printf("%s 0.000000 seconds\n", cp);
Denys Vlasenko0c58cc72010-01-07 10:36:41 +010071#else
72 {
73 long diff = sys_tv.tv_sec - t;
74 if (diff < 0 /*&& tv.tv_usec != 0*/) {
75 /* Why? */
76 /* diff >= 0 is ok: diff < 0, can't just use tv.tv_usec: */
77 /* 45.520820 43.520820 */
78 /* - 44.000000 - 45.000000 */
Denys Vlasenko053ffee2010-01-07 10:52:20 +010079 /* = 1.520820 = -1.479180, not -2.520820! */
Denys Vlasenko0c58cc72010-01-07 10:36:41 +010080 diff++;
81 /* should be 1000000 - tv.tv_usec, but then we must check tv.tv_usec != 0 */
82 sys_tv.tv_usec = 999999 - sys_tv.tv_usec;
83 }
84 printf("%s %ld.%06lu seconds\n", cp, diff, (unsigned long)sys_tv.tv_usec);
Denys Vlasenko5e3b1402010-01-06 22:43:39 +010085 }
Denys Vlasenko6959f6b2010-01-07 08:31:46 +010086#endif
Robert Griebl1cd04452002-07-21 16:50:49 +000087}
88
Denys Vlasenko6959f6b2010-01-07 08:31:46 +010089static void to_sys_clock(const char **pp_rtcname, int utc)
Robert Griebl1cd04452002-07-21 16:50:49 +000090{
Denis Vlasenko459be352007-06-17 19:09:05 +000091 struct timeval tv;
Denys Vlasenko043b1e52009-09-06 12:47:55 +020092 struct timezone tz;
93
94 tz.tz_minuteswest = timezone/60 - 60*daylight;
95 tz.tz_dsttime = 0;
Eric Andersenc7bda1c2004-03-15 08:29:22 +000096
Denys Vlasenko6959f6b2010-01-07 08:31:46 +010097 tv.tv_sec = read_rtc(pp_rtcname, NULL, utc);
Denis Vlasenko459be352007-06-17 19:09:05 +000098 tv.tv_usec = 0;
Denis Vlasenko92258542006-11-01 10:25:35 +000099 if (settimeofday(&tv, &tz))
Denys Vlasenko6959f6b2010-01-07 08:31:46 +0100100 bb_perror_msg_and_die("settimeofday");
Robert Griebl1cd04452002-07-21 16:50:49 +0000101}
102
Denys Vlasenko6959f6b2010-01-07 08:31:46 +0100103static void from_sys_clock(const char **pp_rtcname, int utc)
Robert Griebl1cd04452002-07-21 16:50:49 +0000104{
Denys Vlasenko60f659f2010-04-14 09:19:20 -0700105#if 1
106 struct timeval tv;
107 struct tm tm_time;
108 int rtc;
109
110 rtc = rtc_xopen(pp_rtcname, O_WRONLY);
111 gettimeofday(&tv, NULL);
112 /* Prepare tm_time */
113 if (sizeof(time_t) == sizeof(tv.tv_sec)) {
114 if (utc)
115 gmtime_r((time_t*)&tv.tv_sec, &tm_time);
116 else
117 localtime_r((time_t*)&tv.tv_sec, &tm_time);
118 } else {
119 time_t t = tv.tv_sec;
120 if (utc)
121 gmtime_r(&t, &tm_time);
122 else
123 localtime_r(&t, &tm_time);
124 }
125#else
126/* Bloated code which tries to set hw clock with better precision.
127 * On x86, even though code does set hw clock within <1ms of exact
128 * whole seconds, apparently hw clock (at least on some machines)
129 * doesn't reset internal fractional seconds to 0,
130 * making all this a pointless excercise.
131 */
132 /* If we see that we are N usec away from whole second,
133 * we'll sleep for N-ADJ usecs. ADJ corrects for the fact
134 * that CPU is not infinitely fast.
135 * On infinitely fast CPU, next wakeup would be
136 * on (exactly_next_whole_second - ADJ). On real CPUs,
137 * this difference between current time and whole second
138 * is less than ADJ (assuming system isn't heavily loaded).
139 */
140 /* Small value of 256us gives very precise sync for 2+ GHz CPUs.
141 * Slower CPUs will fail to sync and will go to bigger
142 * ADJ values. qemu-emulated armv4tl with ~100 MHz
143 * performance ends up using ADJ ~= 4*1024 and it takes
144 * 2+ secs (2 tries with successively larger ADJ)
145 * to sync. Even straced one on the same qemu (very slow)
146 * takes only 4 tries.
147 */
148#define TWEAK_USEC 256
149 unsigned adj = TWEAK_USEC;
Denys Vlasenkodc698bb2010-01-09 19:10:49 +0100150 struct tm tm_time;
Denis Vlasenko459be352007-06-17 19:09:05 +0000151 struct timeval tv;
Denys Vlasenko6959f6b2010-01-07 08:31:46 +0100152 int rtc = rtc_xopen(pp_rtcname, O_WRONLY);
Robert Griebl1cd04452002-07-21 16:50:49 +0000153
Denys Vlasenko695fa512010-01-06 18:16:39 +0100154 /* Try to catch the moment when whole second is close */
155 while (1) {
156 unsigned rem_usec;
157 time_t t;
158
159 gettimeofday(&tv, NULL);
160
Denys Vlasenko5e3b1402010-01-06 22:43:39 +0100161 t = tv.tv_sec;
Denys Vlasenko695fa512010-01-06 18:16:39 +0100162 rem_usec = 1000000 - tv.tv_usec;
Denys Vlasenko60f659f2010-04-14 09:19:20 -0700163 if (rem_usec < adj) {
164 /* Close enough */
Denys Vlasenko695fa512010-01-06 18:16:39 +0100165 small_rem:
Denys Vlasenko5e3b1402010-01-06 22:43:39 +0100166 t++;
Denys Vlasenko695fa512010-01-06 18:16:39 +0100167 }
168
Denys Vlasenko60f659f2010-04-14 09:19:20 -0700169 /* Prepare tm_time from t */
Denys Vlasenko695fa512010-01-06 18:16:39 +0100170 if (utc)
Denys Vlasenkodc698bb2010-01-09 19:10:49 +0100171 gmtime_r(&t, &tm_time); /* may read /etc/xxx (it takes time) */
Denys Vlasenko695fa512010-01-06 18:16:39 +0100172 else
Denys Vlasenkodc698bb2010-01-09 19:10:49 +0100173 localtime_r(&t, &tm_time); /* same */
Denys Vlasenko60f659f2010-04-14 09:19:20 -0700174
175 if (adj >= 32*1024) {
176 break; /* 32 ms diff and still no luck?? give up trying to sync */
177 }
Denys Vlasenko695fa512010-01-06 18:16:39 +0100178
179 /* gmtime/localtime took some time, re-get cur time */
180 gettimeofday(&tv, NULL);
181
Denys Vlasenko60f659f2010-04-14 09:19:20 -0700182 if (tv.tv_sec < t /* we are still in old second */
183 || (tv.tv_sec == t && tv.tv_usec < adj) /* not too far into next second */
Denys Vlasenko695fa512010-01-06 18:16:39 +0100184 ) {
Denys Vlasenko60f659f2010-04-14 09:19:20 -0700185 break; /* good, we are in sync! */
Denys Vlasenko695fa512010-01-06 18:16:39 +0100186 }
Denys Vlasenko60f659f2010-04-14 09:19:20 -0700187
188 rem_usec = 1000000 - tv.tv_usec;
189 if (rem_usec < adj) {
190 t = tv.tv_sec;
191 goto small_rem; /* already close to next sec, don't sleep */
Denys Vlasenko695fa512010-01-06 18:16:39 +0100192 }
193
194 /* Try to sync up by sleeping */
Denys Vlasenko695fa512010-01-06 18:16:39 +0100195 usleep(rem_usec - adj);
Denys Vlasenko60f659f2010-04-14 09:19:20 -0700196
197 /* Jump to 1ms diff, then increase fast (x2): EVERY loop
198 * takes ~1 sec, people won't like slowly converging code here!
199 */
200 //bb_error_msg("adj:%d tv.tv_usec:%d", adj, (int)tv.tv_usec);
201 if (adj < 512)
202 adj = 512;
203 /* ... and if last "overshoot" does not look insanely big,
204 * just use it as adj increment. This makes convergence faster.
205 */
206 if (tv.tv_usec < adj * 8) {
207 adj += tv.tv_usec;
208 continue;
209 }
210 adj *= 2;
Denys Vlasenko695fa512010-01-06 18:16:39 +0100211 }
Denys Vlasenko60f659f2010-04-14 09:19:20 -0700212 /* Debug aid to find "optimal" TWEAK_USEC with nearly exact sync.
Denys Vlasenko695fa512010-01-06 18:16:39 +0100213 * Look for a value which makes tv_usec close to 999999 or 0.
Denys Vlasenko60f659f2010-04-14 09:19:20 -0700214 * For 2.20GHz Intel Core 2: optimal TWEAK_USEC ~= 200
Denys Vlasenko695fa512010-01-06 18:16:39 +0100215 */
Denys Vlasenko60f659f2010-04-14 09:19:20 -0700216 //bb_error_msg("tv.tv_usec:%d", (int)tv.tv_usec);
217#endif
218
219 tm_time.tm_isdst = 0;
220 xioctl(rtc, RTC_SET_TIME, &tm_time);
Denys Vlasenko695fa512010-01-06 18:16:39 +0100221
222 if (ENABLE_FEATURE_CLEAN_UP)
223 close(rtc);
Robert Griebl1cd04452002-07-21 16:50:49 +0000224}
225
Davide Cavalca658a4372011-01-22 18:55:32 +0100226/*
227 * At system boot, kernel may set system time from RTC,
228 * but it knows nothing about timezones. If RTC is in local time,
229 * then system time is wrong - it is offset by timezone.
230 * This option corrects system time if RTC is in local time,
231 * and (always) sets in-kernel timezone.
232 *
233 * This is an alternate option to --hctosys that does not read the
234 * hardware clock.
235 */
236static void set_system_clock_timezone(int utc)
237{
238 struct timeval tv;
239 struct tm *broken;
240 struct timezone tz;
241
242 gettimeofday(&tv, NULL);
243 broken = localtime(&tv.tv_sec);
244 tz.tz_minuteswest = timezone / 60;
245 if (broken->tm_isdst)
246 tz.tz_minuteswest -= 60;
247 tz.tz_dsttime = 0;
248 gettimeofday(&tv, NULL);
249 if (!utc)
250 tv.tv_sec += tz.tz_minuteswest * 60;
251 if (settimeofday(&tv, &tz))
252 bb_perror_msg_and_die("settimeofday");
253}
254
255//usage:#define hwclock_trivial_usage
256//usage: IF_FEATURE_HWCLOCK_LONG_OPTIONS(
257//usage: "[-r|--show] [-s|--hctosys] [-w|--systohc] [-t|--systz]"
258//usage: " [-l|--localtime] [-u|--utc]"
259//usage: " [-f|--rtc FILE]"
260//usage: )
261//usage: IF_NOT_FEATURE_HWCLOCK_LONG_OPTIONS(
262//usage: "[-r] [-s] [-w] [-t] [-l] [-u] [-f FILE]"
263//usage: )
264//usage:#define hwclock_full_usage "\n\n"
265//usage: "Query and set hardware clock (RTC)\n"
266//usage: "\nOptions:"
267//usage: "\n -r Show hardware clock time"
268//usage: "\n -s Set system time from hardware clock"
269//usage: "\n -w Set hardware clock from system time"
270//usage: "\n -t Set in-kernel timezone, correct system time"
271//usage: "\n if hardware clock is in local time"
272//usage: "\n -u Hardware clock is in UTC"
273//usage: "\n -l Hardware clock is in local time"
274//usage: "\n -f FILE Use specified device (e.g. /dev/rtc2)"
275
Denis Vlasenko92258542006-11-01 10:25:35 +0000276#define HWCLOCK_OPT_LOCALTIME 0x01
277#define HWCLOCK_OPT_UTC 0x02
278#define HWCLOCK_OPT_SHOW 0x04
279#define HWCLOCK_OPT_HCTOSYS 0x08
280#define HWCLOCK_OPT_SYSTOHC 0x10
Davide Cavalca658a4372011-01-22 18:55:32 +0100281#define HWCLOCK_OPT_SYSTZ 0x20
282#define HWCLOCK_OPT_RTCFILE 0x40
Glenn L McGrath689e4b92004-02-22 09:11:33 +0000283
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000284int hwclock_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000285int hwclock_main(int argc UNUSED_PARAM, char **argv)
Robert Griebl1cd04452002-07-21 16:50:49 +0000286{
Denys Vlasenko6959f6b2010-01-07 08:31:46 +0100287 const char *rtcname = NULL;
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000288 unsigned opt;
Robert Griebl6bb80872004-03-22 21:27:39 +0000289 int utc;
Robert Griebl1cd04452002-07-21 16:50:49 +0000290
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +0000291#if ENABLE_FEATURE_HWCLOCK_LONG_OPTIONS
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000292 static const char hwclock_longopts[] ALIGN1 =
Davide Cavalca658a4372011-01-22 18:55:32 +0100293 "localtime\0" No_argument "l" /* short opt is non-standard */
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000294 "utc\0" No_argument "u"
295 "show\0" No_argument "r"
296 "hctosys\0" No_argument "s"
297 "systohc\0" No_argument "w"
Davide Cavalca658a4372011-01-22 18:55:32 +0100298 "systz\0" No_argument "t" /* short opt is non-standard */
299 "rtc\0" Required_argument "f"
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000300 ;
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000301 applet_long_options = hwclock_longopts;
Robert Griebl1cd04452002-07-21 16:50:49 +0000302#endif
Davide Cavalca658a4372011-01-22 18:55:32 +0100303 opt_complementary = "r--wst:w--rst:s--wrt:t--rsw:l--u:u--l";
304 opt = getopt32(argv, "lurswtf:", &rtcname);
Robert Griebl1cd04452002-07-21 16:50:49 +0000305
Robert Griebl6bb80872004-03-22 21:27:39 +0000306 /* If -u or -l wasn't given check if we are using utc */
Mike Frysingerb31566e2005-04-16 04:48:48 +0000307 if (opt & (HWCLOCK_OPT_UTC | HWCLOCK_OPT_LOCALTIME))
Mike Frysinger6b160e42008-02-15 02:27:19 +0000308 utc = (opt & HWCLOCK_OPT_UTC);
Robert Griebl6bb80872004-03-22 21:27:39 +0000309 else
Mike Frysinger6b160e42008-02-15 02:27:19 +0000310 utc = rtc_adjtime_is_utc();
Mike Frysingerb31566e2005-04-16 04:48:48 +0000311
Mike Frysinger6b160e42008-02-15 02:27:19 +0000312 if (opt & HWCLOCK_OPT_HCTOSYS)
Denys Vlasenko6959f6b2010-01-07 08:31:46 +0100313 to_sys_clock(&rtcname, utc);
Mike Frysinger6b160e42008-02-15 02:27:19 +0000314 else if (opt & HWCLOCK_OPT_SYSTOHC)
Denys Vlasenko6959f6b2010-01-07 08:31:46 +0100315 from_sys_clock(&rtcname, utc);
Davide Cavalca658a4372011-01-22 18:55:32 +0100316 else if (opt & HWCLOCK_OPT_SYSTZ)
317 set_system_clock_timezone(utc);
Mike Frysinger6b160e42008-02-15 02:27:19 +0000318 else
319 /* default HWCLOCK_OPT_SHOW */
Denys Vlasenko6959f6b2010-01-07 08:31:46 +0100320 show_clock(&rtcname, utc);
Mike Frysinger6b160e42008-02-15 02:27:19 +0000321
Denis Vlasenko459be352007-06-17 19:09:05 +0000322 return 0;
Robert Griebl1cd04452002-07-21 16:50:49 +0000323}