1 /* vi: set sw=4 ts=4: */
3 * Mini hwclock implementation for busybox
5 * Copyright (C) 2002 Robert Griebl <griebl@gmx.de>
7 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
9 //config:config HWCLOCK
10 //config: bool "hwclock"
12 //config: select PLATFORM_LINUX
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.
19 //config:config FEATURE_HWCLOCK_LONG_OPTIONS
20 //config: bool "Support long options (--hctosys,...)"
22 //config: depends on HWCLOCK && LONG_OPTS
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
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.
34 //config: pathname.com/fhs/pub/fhs-2.3.html#VARLIBHWCLOCKSTATEDIRECTORYFORHWCLO
36 //applet:IF_HWCLOCK(APPLET(hwclock, BB_DIR_SBIN, BB_SUID_DROP))
38 //kbuild:lib-$(CONFIG_HWCLOCK) += hwclock.o
41 /* After libbb.h, since it needs sys/types.h on some systems */
42 #include <sys/utsname.h>
45 /* 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
49 * deprive us from any useful info.
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.
55 #define SHOW_HWCLOCK_DIFF 0
58 #if !SHOW_HWCLOCK_DIFF
59 # define read_rtc(pp_rtcname, sys_tv, utc) read_rtc(pp_rtcname, utc)
61 static time_t read_rtc(const char **pp_rtcname, struct timeval *sys_tv, int utc)
66 fd = rtc_xopen(pp_rtcname, O_RDONLY);
68 rtc_read_tm(&tm_time, fd);
72 int before = tm_time.tm_sec;
74 rtc_read_tm(&tm_time, fd);
75 gettimeofday(sys_tv, NULL);
76 if (before != (int)tm_time.tm_sec)
82 if (ENABLE_FEATURE_CLEAN_UP)
85 return rtc_tm2time(&tm_time, utc);
88 static void show_clock(const char **pp_rtcname, int utc)
91 struct timeval sys_tv;
93 time_t t = read_rtc(pp_rtcname, &sys_tv, utc);
95 #if ENABLE_LOCALE_SUPPORT
96 /* Standard hwclock uses locale-specific output format */
98 struct tm *ptm = localtime(&t);
99 strftime(cp, sizeof(cp), "%c", ptm);
101 char *cp = ctime(&t);
105 #if !SHOW_HWCLOCK_DIFF
106 printf("%s 0.000000 seconds\n", cp);
109 long diff = sys_tv.tv_sec - t;
110 if (diff < 0 /*&& tv.tv_usec != 0*/) {
111 /* 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! */
117 /* Should be 1000000 - tv.tv_usec, but then we must check tv.tv_usec != 0 */
118 sys_tv.tv_usec = 999999 - sys_tv.tv_usec;
120 printf("%s %ld.%06lu seconds\n", cp, diff, (unsigned long)sys_tv.tv_usec);
125 static void to_sys_clock(const char **pp_rtcname, int utc)
130 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".
137 tv.tv_sec = read_rtc(pp_rtcname, NULL, utc);
139 if (settimeofday(&tv, &tz))
140 bb_perror_msg_and_die("settimeofday");
143 static void from_sys_clock(const char **pp_rtcname, int utc)
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)) {
155 gmtime_r((time_t*)&tv.tv_sec, &tm_time);
157 localtime_r((time_t*)&tv.tv_sec, &tm_time);
159 time_t t = tv.tv_sec;
161 gmtime_r(&t, &tm_time);
163 localtime_r(&t, &tm_time);
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 exercise.
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).
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.
188 #define TWEAK_USEC 256
189 unsigned adj = TWEAK_USEC;
192 int rtc = rtc_xopen(pp_rtcname, O_WRONLY);
194 /* Try to catch the moment when whole second is close */
199 gettimeofday(&tv, NULL);
202 rem_usec = 1000000 - tv.tv_usec;
203 if (rem_usec < adj) {
209 /* Prepare tm_time from t */
211 gmtime_r(&t, &tm_time); /* may read /etc/xxx (it takes time) */
213 localtime_r(&t, &tm_time); /* same */
215 if (adj >= 32*1024) {
216 break; /* 32 ms diff and still no luck?? give up trying to sync */
219 /* gmtime/localtime took some time, re-get cur time */
220 gettimeofday(&tv, NULL);
222 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 */
225 break; /* good, we are in sync! */
228 rem_usec = 1000000 - tv.tv_usec;
229 if (rem_usec < adj) {
231 goto small_rem; /* already close to next sec, don't sleep */
234 /* Try to sync up by sleeping */
235 usleep(rem_usec - adj);
237 /* Jump to 1ms diff, then increase fast (x2): EVERY loop
238 * takes ~1 sec, people won't like slowly converging code here!
240 //bb_error_msg("adj:%d tv.tv_usec:%d", adj, (int)tv.tv_usec);
243 /* ... and if last "overshoot" does not look insanely big,
244 * just use it as adj increment. This makes convergence faster.
246 if (tv.tv_usec < adj * 8) {
252 /* Debug aid to find "optimal" TWEAK_USEC with nearly exact sync.
253 * Look for a value which makes tv_usec close to 999999 or 0.
254 * For 2.20GHz Intel Core 2: optimal TWEAK_USEC ~= 200
256 //bb_error_msg("tv.tv_usec:%d", (int)tv.tv_usec);
259 tm_time.tm_isdst = 0;
260 xioctl(rtc, RTC_SET_TIME, &tm_time);
262 if (ENABLE_FEATURE_CLEAN_UP)
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.
273 * This is an alternate option to --hctosys that does not read the
276 static void set_system_clock_timezone(int utc)
282 gettimeofday(&tv, NULL);
283 broken = localtime(&tv.tv_sec);
284 tz.tz_minuteswest = timezone / 60;
285 if (broken->tm_isdst > 0)
286 tz.tz_minuteswest -= 60;
288 gettimeofday(&tv, NULL);
290 tv.tv_sec += tz.tz_minuteswest * 60;
291 if (settimeofday(&tv, &tz))
292 bb_perror_msg_and_die("settimeofday");
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]"
301 //usage: IF_NOT_FEATURE_HWCLOCK_LONG_OPTIONS(
302 //usage: "[-r] [-s] [-w] [-t] [-l] [-u] [-f FILE]"
304 //usage:#define hwclock_full_usage "\n\n"
305 //usage: "Query and set hardware clock (RTC)\n"
306 //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"
311 //usage: "\n -u Assume hardware clock is kept in UTC"
312 //usage: "\n -l Assume hardware clock is kept in local time"
313 //usage: "\n -f FILE Use specified device (e.g. /dev/rtc2)"
315 #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
320 #define HWCLOCK_OPT_SYSTZ 0x20
321 #define HWCLOCK_OPT_RTCFILE 0x40
323 int hwclock_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
324 int hwclock_main(int argc UNUSED_PARAM, char **argv)
326 const char *rtcname = NULL;
330 #if ENABLE_FEATURE_HWCLOCK_LONG_OPTIONS
331 static const char hwclock_longopts[] ALIGN1 =
332 "localtime\0" No_argument "l" /* short opt is non-standard */
333 "utc\0" No_argument "u"
334 "show\0" No_argument "r"
335 "hctosys\0" No_argument "s"
336 "systohc\0" No_argument "w"
337 "systz\0" No_argument "t" /* short opt is non-standard */
338 "rtc\0" Required_argument "f"
340 applet_long_options = hwclock_longopts;
343 /* Initialize "timezone" (libc global variable) */
346 opt_complementary = "r--wst:w--rst:s--wrt:t--rsw:l--u:u--l";
347 opt = getopt32(argv, "lurswtf:", &rtcname);
349 /* If -u or -l wasn't given check if we are using utc */
350 if (opt & (HWCLOCK_OPT_UTC | HWCLOCK_OPT_LOCALTIME))
351 utc = (opt & HWCLOCK_OPT_UTC);
353 utc = rtc_adjtime_is_utc();
355 if (opt & HWCLOCK_OPT_HCTOSYS)
356 to_sys_clock(&rtcname, utc);
357 else if (opt & HWCLOCK_OPT_SYSTOHC)
358 from_sys_clock(&rtcname, utc);
359 else if (opt & HWCLOCK_OPT_SYSTZ)
360 set_system_clock_timezone(utc);
362 /* default HWCLOCK_OPT_SHOW */
363 show_clock(&rtcname, utc);