blob: d65011a71e219d82fb42e2b9a3e8ade0c62e0311 [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.
Denys Vlasenkodd898c92016-11-23 11:46:32 +01008 */
9//config:config HWCLOCK
10//config: bool "hwclock"
11//config: default y
12//config: select PLATFORM_LINUX
13//config: help
14//config: The hwclock utility is used to read and set the hardware clock
15//config: on a system. This is primarily used to set the current time on
16//config: shutdown in the hardware clock, so the hardware will keep the
17//config: correct time when Linux is _not_ running.
18//config:
19//config:config FEATURE_HWCLOCK_LONG_OPTIONS
20//config: bool "Support long options (--hctosys,...)"
21//config: default y
22//config: depends on HWCLOCK && LONG_OPTS
Denys Vlasenkodd898c92016-11-23 11:46:32 +010023//config:
24//config:config FEATURE_HWCLOCK_ADJTIME_FHS
25//config: bool "Use FHS /var/lib/hwclock/adjtime"
26//config: default n # util-linux-ng in Fedora 13 still uses /etc/adjtime
27//config: depends on HWCLOCK
28//config: help
29//config: Starting with FHS 2.3, the adjtime state file is supposed to exist
30//config: at /var/lib/hwclock/adjtime instead of /etc/adjtime. If you wish
31//config: to use the FHS behavior, answer Y here, otherwise answer N for the
32//config: classic /etc/adjtime path.
33//config:
34//config: pathname.com/fhs/pub/fhs-2.3.html#VARLIBHWCLOCKSTATEDIRECTORYFORHWCLO
35
36//applet:IF_HWCLOCK(APPLET(hwclock, BB_DIR_SBIN, BB_SUID_DROP))
37
38//kbuild:lib-$(CONFIG_HWCLOCK) += hwclock.o
Robert Griebl1cd04452002-07-21 16:50:49 +000039
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000040#include "libbb.h"
Denys Vlasenko043b1e52009-09-06 12:47:55 +020041/* After libbb.h, since it needs sys/types.h on some systems */
42#include <sys/utsname.h>
Mike Frysinger6b160e42008-02-15 02:27:19 +000043#include "rtc_.h"
Eric Andersen8882ea52002-12-11 03:41:28 +000044
Denys Vlasenko6959f6b2010-01-07 08:31:46 +010045/* diff code is disabled: it's not sys/hw clock diff, it's some useless
46 * "time between hwclock was started and we saw CMOS tick" quantity.
47 * It's useless since hwclock is started at a random moment,
48 * thus the quantity is also random, useless. Showing 0.000000 does not
Denys Vlasenko0c58cc72010-01-07 10:36:41 +010049 * deprive us from any useful info.
50 *
51 * SHOW_HWCLOCK_DIFF code in this file shows the difference between system
52 * and hw clock. It is useful, but not compatible with standard hwclock.
53 * Thus disabled.
54 */
Denys Vlasenko6959f6b2010-01-07 08:31:46 +010055#define SHOW_HWCLOCK_DIFF 0
56
57
58#if !SHOW_HWCLOCK_DIFF
59# define read_rtc(pp_rtcname, sys_tv, utc) read_rtc(pp_rtcname, utc)
60#endif
61static time_t read_rtc(const char **pp_rtcname, struct timeval *sys_tv, int utc)
Denis Vlasenko92258542006-11-01 10:25:35 +000062{
Denys Vlasenkodc698bb2010-01-09 19:10:49 +010063 struct tm tm_time;
Mike Frysinger6b160e42008-02-15 02:27:19 +000064 int fd;
Robert Griebl1cd04452002-07-21 16:50:49 +000065
Denys Vlasenko6959f6b2010-01-07 08:31:46 +010066 fd = rtc_xopen(pp_rtcname, O_RDONLY);
Denys Vlasenko5e3b1402010-01-06 22:43:39 +010067
Denys Vlasenkodc698bb2010-01-09 19:10:49 +010068 rtc_read_tm(&tm_time, fd);
Denys Vlasenko6959f6b2010-01-07 08:31:46 +010069
70#if SHOW_HWCLOCK_DIFF
Denys Vlasenko0c58cc72010-01-07 10:36:41 +010071 {
Denys Vlasenkodc698bb2010-01-09 19:10:49 +010072 int before = tm_time.tm_sec;
Denys Vlasenko0c58cc72010-01-07 10:36:41 +010073 while (1) {
Denys Vlasenkodc698bb2010-01-09 19:10:49 +010074 rtc_read_tm(&tm_time, fd);
Denys Vlasenko0c58cc72010-01-07 10:36:41 +010075 gettimeofday(sys_tv, NULL);
Denys Vlasenkod778e6c2012-04-17 19:25:13 +020076 if (before != (int)tm_time.tm_sec)
Denys Vlasenko0c58cc72010-01-07 10:36:41 +010077 break;
78 }
Denys Vlasenko5e3b1402010-01-06 22:43:39 +010079 }
Denys Vlasenko6959f6b2010-01-07 08:31:46 +010080#endif
Denys Vlasenko5e3b1402010-01-06 22:43:39 +010081
Denys Vlasenko695fa512010-01-06 18:16:39 +010082 if (ENABLE_FEATURE_CLEAN_UP)
83 close(fd);
Eric Andersenc7bda1c2004-03-15 08:29:22 +000084
Denys Vlasenkodc698bb2010-01-09 19:10:49 +010085 return rtc_tm2time(&tm_time, utc);
Robert Griebl1cd04452002-07-21 16:50:49 +000086}
87
Denys Vlasenko6959f6b2010-01-07 08:31:46 +010088static void show_clock(const char **pp_rtcname, int utc)
Robert Griebl1cd04452002-07-21 16:50:49 +000089{
Denys Vlasenko0c58cc72010-01-07 10:36:41 +010090#if SHOW_HWCLOCK_DIFF
91 struct timeval sys_tv;
92#endif
Denys Vlasenko319b8bb2011-07-08 06:40:25 +020093 time_t t = read_rtc(pp_rtcname, &sys_tv, utc);
Robert Griebl1cd04452002-07-21 16:50:49 +000094
Denys Vlasenko319b8bb2011-07-08 06:40:25 +020095#if ENABLE_LOCALE_SUPPORT
96 /* Standard hwclock uses locale-specific output format */
97 char cp[64];
98 struct tm *ptm = localtime(&t);
99 strftime(cp, sizeof(cp), "%c", ptm);
100#else
101 char *cp = ctime(&t);
Ron Yorston8ec1ff32015-03-12 20:18:51 +0100102 chomp(cp);
Denys Vlasenko319b8bb2011-07-08 06:40:25 +0200103#endif
104
Denys Vlasenko0c58cc72010-01-07 10:36:41 +0100105#if !SHOW_HWCLOCK_DIFF
Denys Vlasenko6959f6b2010-01-07 08:31:46 +0100106 printf("%s 0.000000 seconds\n", cp);
Denys Vlasenko0c58cc72010-01-07 10:36:41 +0100107#else
108 {
109 long diff = sys_tv.tv_sec - t;
110 if (diff < 0 /*&& tv.tv_usec != 0*/) {
Denys Vlasenko319b8bb2011-07-08 06:40:25 +0200111 /* Why we need diff++? */
112 /* diff >= 0 is ok: | diff < 0, can't just use tv.tv_usec: */
113 /* 45.520820 | 43.520820 */
114 /* - 44.000000 | - 45.000000 */
115 /* = 1.520820 | = -1.479180, not -2.520820! */
Denys Vlasenko0c58cc72010-01-07 10:36:41 +0100116 diff++;
Denys Vlasenko319b8bb2011-07-08 06:40:25 +0200117 /* Should be 1000000 - tv.tv_usec, but then we must check tv.tv_usec != 0 */
Denys Vlasenko0c58cc72010-01-07 10:36:41 +0100118 sys_tv.tv_usec = 999999 - sys_tv.tv_usec;
119 }
120 printf("%s %ld.%06lu seconds\n", cp, diff, (unsigned long)sys_tv.tv_usec);
Denys Vlasenko5e3b1402010-01-06 22:43:39 +0100121 }
Denys Vlasenko6959f6b2010-01-07 08:31:46 +0100122#endif
Robert Griebl1cd04452002-07-21 16:50:49 +0000123}
124
Denys Vlasenko6959f6b2010-01-07 08:31:46 +0100125static void to_sys_clock(const char **pp_rtcname, int utc)
Robert Griebl1cd04452002-07-21 16:50:49 +0000126{
Denis Vlasenko459be352007-06-17 19:09:05 +0000127 struct timeval tv;
Denys Vlasenko043b1e52009-09-06 12:47:55 +0200128 struct timezone tz;
129
Denys Vlasenko589051b2014-02-25 17:52:10 +0100130 tz.tz_minuteswest = timezone/60;
131 /* ^^^ used to also subtract 60*daylight, but it's wrong:
132 * daylight!=0 means "this timezone has some DST
133 * during the year", not "DST is in effect now".
134 */
Denys Vlasenko043b1e52009-09-06 12:47:55 +0200135 tz.tz_dsttime = 0;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000136
Denys Vlasenko6959f6b2010-01-07 08:31:46 +0100137 tv.tv_sec = read_rtc(pp_rtcname, NULL, utc);
Denis Vlasenko459be352007-06-17 19:09:05 +0000138 tv.tv_usec = 0;
Denis Vlasenko92258542006-11-01 10:25:35 +0000139 if (settimeofday(&tv, &tz))
Denys Vlasenko6959f6b2010-01-07 08:31:46 +0100140 bb_perror_msg_and_die("settimeofday");
Robert Griebl1cd04452002-07-21 16:50:49 +0000141}
142
Denys Vlasenko6959f6b2010-01-07 08:31:46 +0100143static void from_sys_clock(const char **pp_rtcname, int utc)
Robert Griebl1cd04452002-07-21 16:50:49 +0000144{
Denys Vlasenko60f659f2010-04-14 09:19:20 -0700145#if 1
146 struct timeval tv;
147 struct tm tm_time;
148 int rtc;
149
150 rtc = rtc_xopen(pp_rtcname, O_WRONLY);
151 gettimeofday(&tv, NULL);
152 /* Prepare tm_time */
153 if (sizeof(time_t) == sizeof(tv.tv_sec)) {
154 if (utc)
155 gmtime_r((time_t*)&tv.tv_sec, &tm_time);
156 else
157 localtime_r((time_t*)&tv.tv_sec, &tm_time);
158 } else {
159 time_t t = tv.tv_sec;
160 if (utc)
161 gmtime_r(&t, &tm_time);
162 else
163 localtime_r(&t, &tm_time);
164 }
165#else
166/* Bloated code which tries to set hw clock with better precision.
167 * On x86, even though code does set hw clock within <1ms of exact
168 * whole seconds, apparently hw clock (at least on some machines)
169 * doesn't reset internal fractional seconds to 0,
170 * making all this a pointless excercise.
171 */
172 /* If we see that we are N usec away from whole second,
173 * we'll sleep for N-ADJ usecs. ADJ corrects for the fact
174 * that CPU is not infinitely fast.
175 * On infinitely fast CPU, next wakeup would be
176 * on (exactly_next_whole_second - ADJ). On real CPUs,
177 * this difference between current time and whole second
178 * is less than ADJ (assuming system isn't heavily loaded).
179 */
180 /* Small value of 256us gives very precise sync for 2+ GHz CPUs.
181 * Slower CPUs will fail to sync and will go to bigger
182 * ADJ values. qemu-emulated armv4tl with ~100 MHz
183 * performance ends up using ADJ ~= 4*1024 and it takes
184 * 2+ secs (2 tries with successively larger ADJ)
185 * to sync. Even straced one on the same qemu (very slow)
186 * takes only 4 tries.
187 */
188#define TWEAK_USEC 256
189 unsigned adj = TWEAK_USEC;
Denys Vlasenkodc698bb2010-01-09 19:10:49 +0100190 struct tm tm_time;
Denis Vlasenko459be352007-06-17 19:09:05 +0000191 struct timeval tv;
Denys Vlasenko6959f6b2010-01-07 08:31:46 +0100192 int rtc = rtc_xopen(pp_rtcname, O_WRONLY);
Robert Griebl1cd04452002-07-21 16:50:49 +0000193
Denys Vlasenko695fa512010-01-06 18:16:39 +0100194 /* Try to catch the moment when whole second is close */
195 while (1) {
196 unsigned rem_usec;
197 time_t t;
198
199 gettimeofday(&tv, NULL);
200
Denys Vlasenko5e3b1402010-01-06 22:43:39 +0100201 t = tv.tv_sec;
Denys Vlasenko695fa512010-01-06 18:16:39 +0100202 rem_usec = 1000000 - tv.tv_usec;
Denys Vlasenko60f659f2010-04-14 09:19:20 -0700203 if (rem_usec < adj) {
204 /* Close enough */
Denys Vlasenko695fa512010-01-06 18:16:39 +0100205 small_rem:
Denys Vlasenko5e3b1402010-01-06 22:43:39 +0100206 t++;
Denys Vlasenko695fa512010-01-06 18:16:39 +0100207 }
208
Denys Vlasenko60f659f2010-04-14 09:19:20 -0700209 /* Prepare tm_time from t */
Denys Vlasenko695fa512010-01-06 18:16:39 +0100210 if (utc)
Denys Vlasenkodc698bb2010-01-09 19:10:49 +0100211 gmtime_r(&t, &tm_time); /* may read /etc/xxx (it takes time) */
Denys Vlasenko695fa512010-01-06 18:16:39 +0100212 else
Denys Vlasenkodc698bb2010-01-09 19:10:49 +0100213 localtime_r(&t, &tm_time); /* same */
Denys Vlasenko60f659f2010-04-14 09:19:20 -0700214
215 if (adj >= 32*1024) {
216 break; /* 32 ms diff and still no luck?? give up trying to sync */
217 }
Denys Vlasenko695fa512010-01-06 18:16:39 +0100218
219 /* gmtime/localtime took some time, re-get cur time */
220 gettimeofday(&tv, NULL);
221
Denys Vlasenko60f659f2010-04-14 09:19:20 -0700222 if (tv.tv_sec < t /* we are still in old second */
223 || (tv.tv_sec == t && tv.tv_usec < adj) /* not too far into next second */
Denys Vlasenko695fa512010-01-06 18:16:39 +0100224 ) {
Denys Vlasenko60f659f2010-04-14 09:19:20 -0700225 break; /* good, we are in sync! */
Denys Vlasenko695fa512010-01-06 18:16:39 +0100226 }
Denys Vlasenko60f659f2010-04-14 09:19:20 -0700227
228 rem_usec = 1000000 - tv.tv_usec;
229 if (rem_usec < adj) {
230 t = tv.tv_sec;
231 goto small_rem; /* already close to next sec, don't sleep */
Denys Vlasenko695fa512010-01-06 18:16:39 +0100232 }
233
234 /* Try to sync up by sleeping */
Denys Vlasenko695fa512010-01-06 18:16:39 +0100235 usleep(rem_usec - adj);
Denys Vlasenko60f659f2010-04-14 09:19:20 -0700236
237 /* Jump to 1ms diff, then increase fast (x2): EVERY loop
238 * takes ~1 sec, people won't like slowly converging code here!
239 */
240 //bb_error_msg("adj:%d tv.tv_usec:%d", adj, (int)tv.tv_usec);
241 if (adj < 512)
242 adj = 512;
243 /* ... and if last "overshoot" does not look insanely big,
244 * just use it as adj increment. This makes convergence faster.
245 */
246 if (tv.tv_usec < adj * 8) {
247 adj += tv.tv_usec;
248 continue;
249 }
250 adj *= 2;
Denys Vlasenko695fa512010-01-06 18:16:39 +0100251 }
Denys Vlasenko60f659f2010-04-14 09:19:20 -0700252 /* Debug aid to find "optimal" TWEAK_USEC with nearly exact sync.
Denys Vlasenko695fa512010-01-06 18:16:39 +0100253 * Look for a value which makes tv_usec close to 999999 or 0.
Denys Vlasenko60f659f2010-04-14 09:19:20 -0700254 * For 2.20GHz Intel Core 2: optimal TWEAK_USEC ~= 200
Denys Vlasenko695fa512010-01-06 18:16:39 +0100255 */
Denys Vlasenko60f659f2010-04-14 09:19:20 -0700256 //bb_error_msg("tv.tv_usec:%d", (int)tv.tv_usec);
257#endif
258
259 tm_time.tm_isdst = 0;
260 xioctl(rtc, RTC_SET_TIME, &tm_time);
Denys Vlasenko695fa512010-01-06 18:16:39 +0100261
262 if (ENABLE_FEATURE_CLEAN_UP)
263 close(rtc);
Robert Griebl1cd04452002-07-21 16:50:49 +0000264}
265
Davide Cavalca658a4372011-01-22 18:55:32 +0100266/*
267 * At system boot, kernel may set system time from RTC,
268 * but it knows nothing about timezones. If RTC is in local time,
269 * then system time is wrong - it is offset by timezone.
270 * This option corrects system time if RTC is in local time,
271 * and (always) sets in-kernel timezone.
272 *
273 * This is an alternate option to --hctosys that does not read the
274 * hardware clock.
275 */
276static void set_system_clock_timezone(int utc)
277{
278 struct timeval tv;
279 struct tm *broken;
280 struct timezone tz;
281
282 gettimeofday(&tv, NULL);
283 broken = localtime(&tv.tv_sec);
284 tz.tz_minuteswest = timezone / 60;
Denys Vlasenko589051b2014-02-25 17:52:10 +0100285 if (broken->tm_isdst > 0)
Davide Cavalca658a4372011-01-22 18:55:32 +0100286 tz.tz_minuteswest -= 60;
287 tz.tz_dsttime = 0;
288 gettimeofday(&tv, NULL);
289 if (!utc)
290 tv.tv_sec += tz.tz_minuteswest * 60;
291 if (settimeofday(&tv, &tz))
292 bb_perror_msg_and_die("settimeofday");
293}
294
295//usage:#define hwclock_trivial_usage
296//usage: IF_FEATURE_HWCLOCK_LONG_OPTIONS(
297//usage: "[-r|--show] [-s|--hctosys] [-w|--systohc] [-t|--systz]"
298//usage: " [-l|--localtime] [-u|--utc]"
299//usage: " [-f|--rtc FILE]"
300//usage: )
301//usage: IF_NOT_FEATURE_HWCLOCK_LONG_OPTIONS(
302//usage: "[-r] [-s] [-w] [-t] [-l] [-u] [-f FILE]"
303//usage: )
304//usage:#define hwclock_full_usage "\n\n"
305//usage: "Query and set hardware clock (RTC)\n"
Davide Cavalca658a4372011-01-22 18:55:32 +0100306//usage: "\n -r Show hardware clock time"
307//usage: "\n -s Set system time from hardware clock"
308//usage: "\n -w Set hardware clock from system time"
309//usage: "\n -t Set in-kernel timezone, correct system time"
310//usage: "\n if hardware clock is in local time"
Denys Vlasenko46465ec2011-07-04 04:34:57 +0200311//usage: "\n -u Assume hardware clock is kept in UTC"
312//usage: "\n -l Assume hardware clock is kept in local time"
Davide Cavalca658a4372011-01-22 18:55:32 +0100313//usage: "\n -f FILE Use specified device (e.g. /dev/rtc2)"
314
Denis Vlasenko92258542006-11-01 10:25:35 +0000315#define HWCLOCK_OPT_LOCALTIME 0x01
316#define HWCLOCK_OPT_UTC 0x02
317#define HWCLOCK_OPT_SHOW 0x04
318#define HWCLOCK_OPT_HCTOSYS 0x08
319#define HWCLOCK_OPT_SYSTOHC 0x10
Davide Cavalca658a4372011-01-22 18:55:32 +0100320#define HWCLOCK_OPT_SYSTZ 0x20
321#define HWCLOCK_OPT_RTCFILE 0x40
Glenn L McGrath689e4b92004-02-22 09:11:33 +0000322
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000323int hwclock_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000324int hwclock_main(int argc UNUSED_PARAM, char **argv)
Robert Griebl1cd04452002-07-21 16:50:49 +0000325{
Denys Vlasenko6959f6b2010-01-07 08:31:46 +0100326 const char *rtcname = NULL;
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000327 unsigned opt;
Robert Griebl6bb80872004-03-22 21:27:39 +0000328 int utc;
Robert Griebl1cd04452002-07-21 16:50:49 +0000329
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +0000330#if ENABLE_FEATURE_HWCLOCK_LONG_OPTIONS
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000331 static const char hwclock_longopts[] ALIGN1 =
Davide Cavalca658a4372011-01-22 18:55:32 +0100332 "localtime\0" No_argument "l" /* short opt is non-standard */
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000333 "utc\0" No_argument "u"
334 "show\0" No_argument "r"
335 "hctosys\0" No_argument "s"
336 "systohc\0" No_argument "w"
Davide Cavalca658a4372011-01-22 18:55:32 +0100337 "systz\0" No_argument "t" /* short opt is non-standard */
338 "rtc\0" Required_argument "f"
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000339 ;
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000340 applet_long_options = hwclock_longopts;
Robert Griebl1cd04452002-07-21 16:50:49 +0000341#endif
Denys Vlasenko589051b2014-02-25 17:52:10 +0100342
343 /* Initialize "timezone" (libc global variable) */
344 tzset();
345
Davide Cavalca658a4372011-01-22 18:55:32 +0100346 opt_complementary = "r--wst:w--rst:s--wrt:t--rsw:l--u:u--l";
347 opt = getopt32(argv, "lurswtf:", &rtcname);
Robert Griebl1cd04452002-07-21 16:50:49 +0000348
Robert Griebl6bb80872004-03-22 21:27:39 +0000349 /* If -u or -l wasn't given check if we are using utc */
Mike Frysingerb31566e2005-04-16 04:48:48 +0000350 if (opt & (HWCLOCK_OPT_UTC | HWCLOCK_OPT_LOCALTIME))
Mike Frysinger6b160e42008-02-15 02:27:19 +0000351 utc = (opt & HWCLOCK_OPT_UTC);
Robert Griebl6bb80872004-03-22 21:27:39 +0000352 else
Mike Frysinger6b160e42008-02-15 02:27:19 +0000353 utc = rtc_adjtime_is_utc();
Mike Frysingerb31566e2005-04-16 04:48:48 +0000354
Mike Frysinger6b160e42008-02-15 02:27:19 +0000355 if (opt & HWCLOCK_OPT_HCTOSYS)
Denys Vlasenko6959f6b2010-01-07 08:31:46 +0100356 to_sys_clock(&rtcname, utc);
Mike Frysinger6b160e42008-02-15 02:27:19 +0000357 else if (opt & HWCLOCK_OPT_SYSTOHC)
Denys Vlasenko6959f6b2010-01-07 08:31:46 +0100358 from_sys_clock(&rtcname, utc);
Davide Cavalca658a4372011-01-22 18:55:32 +0100359 else if (opt & HWCLOCK_OPT_SYSTZ)
360 set_system_clock_timezone(utc);
Mike Frysinger6b160e42008-02-15 02:27:19 +0000361 else
362 /* default HWCLOCK_OPT_SHOW */
Denys Vlasenko6959f6b2010-01-07 08:31:46 +0100363 show_clock(&rtcname, utc);
Mike Frysinger6b160e42008-02-15 02:27:19 +0000364
Denis Vlasenko459be352007-06-17 19:09:05 +0000365 return 0;
Robert Griebl1cd04452002-07-21 16:50:49 +0000366}