]> git.ipfire.org Git - thirdparty/util-linux.git/blob - sys-utils/hwclock-rtc.c
Merge branch 'PR/lscpu-caches-sep' of github.com:karelzak/util-linux-work
[thirdparty/util-linux.git] / sys-utils / hwclock-rtc.c
1 /*
2 * SPDX-License-Identifier: GPL-2.0-or-later
3 *
4 * rtc.c - Use /dev/rtc for clock access
5 */
6 #include <asm/ioctl.h>
7 #include <errno.h>
8 #include <linux/rtc.h>
9 #include <linux/types.h>
10 #include <fcntl.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <sys/ioctl.h>
14 #include <sys/select.h>
15 #include <sys/time.h>
16 #include <time.h>
17 #include <unistd.h>
18
19 #include "monotonic.h"
20 #include "strutils.h"
21 #include "xalloc.h"
22 #include "nls.h"
23
24 #include "hwclock.h"
25
26 #ifndef RTC_PARAM_GET
27 struct rtc_param {
28 __u64 param;
29 union {
30 __u64 uvalue;
31 __s64 svalue;
32 __u64 ptr;
33 };
34 __u32 index;
35 __u32 __pad;
36 };
37
38 # define RTC_PARAM_GET _IOW('p', 0x13, struct rtc_param)
39 # define RTC_PARAM_SET _IOW('p', 0x14, struct rtc_param)
40
41 # define RTC_PARAM_FEATURES 0
42 # define RTC_PARAM_CORRECTION 1
43 # define RTC_PARAM_BACKUP_SWITCH_MODE 2
44 #endif /* RTC_PARAM_GET */
45
46 static const struct hwclock_param hwclock_params[] =
47 {
48 { RTC_PARAM_FEATURES, "features", N_("supported features") },
49 { RTC_PARAM_CORRECTION, "correction", N_("time correction") },
50 { RTC_PARAM_BACKUP_SWITCH_MODE, "bsm", N_("backup switch mode") },
51 { }
52 };
53
54 const struct hwclock_param *get_hwclock_params(void)
55 {
56 return hwclock_params;
57 }
58
59 /*
60 * /dev/rtc is conventionally chardev 10/135
61 * ia64 uses /dev/efirtc, chardev 10/136
62 * devfs (obsolete) used /dev/misc/... for miscdev
63 * new RTC framework + udev uses dynamic major and /dev/rtc0.../dev/rtcN
64 * ... so we need an overridable default
65 */
66
67 /* default or user defined dev (by hwclock --rtc=<path>) */
68 static const char *rtc_dev_name;
69 static int rtc_dev_fd = -1;
70
71 static void close_rtc(void)
72 {
73 if (rtc_dev_fd != -1)
74 close(rtc_dev_fd);
75 rtc_dev_fd = -1;
76 }
77
78 static int open_rtc(const struct hwclock_control *ctl)
79 {
80 static const char * const fls[] = {
81 #ifdef __ia64__
82 "/dev/efirtc",
83 "/dev/misc/efirtc",
84 #endif
85 "/dev/rtc0",
86 "/dev/rtc",
87 "/dev/misc/rtc"
88 };
89 size_t i;
90
91 if (rtc_dev_fd != -1)
92 return rtc_dev_fd;
93
94 /* --rtc option has been given */
95 if (ctl->rtc_dev_name) {
96 rtc_dev_name = ctl->rtc_dev_name;
97 rtc_dev_fd = open(rtc_dev_name, O_RDONLY);
98 } else {
99 for (i = 0; i < ARRAY_SIZE(fls); i++) {
100 if (ctl->verbose)
101 printf(_("Trying to open: %s\n"), fls[i]);
102 rtc_dev_fd = open(fls[i], O_RDONLY);
103
104 if (rtc_dev_fd < 0) {
105 if (errno == ENOENT || errno == ENODEV)
106 continue;
107 if (ctl->verbose)
108 warn(_("cannot open %s"), fls[i]);
109 }
110 rtc_dev_name = fls[i];
111 break;
112 }
113 if (rtc_dev_fd < 0)
114 rtc_dev_name = *fls; /* default for error messages */
115 }
116 if (rtc_dev_fd != -1)
117 atexit(close_rtc);
118 return rtc_dev_fd;
119 }
120
121 static int open_rtc_or_exit(const struct hwclock_control *ctl)
122 {
123 int rtc_fd = open_rtc(ctl);
124
125 if (rtc_fd < 0) {
126 warn(_("cannot open rtc device"));
127 hwclock_exit(ctl, EXIT_FAILURE);
128 }
129 return rtc_fd;
130 }
131
132 static int do_rtc_read_ioctl(int rtc_fd, struct tm *tm)
133 {
134 int rc = -1;
135 struct rtc_time rtc_tm = { 0 };
136
137 rc = ioctl(rtc_fd, RTC_RD_TIME, &rtc_tm);
138
139 if (rc == -1) {
140 warn(_("ioctl(RTC_RD_NAME) to %s to read the time failed"),
141 rtc_dev_name);
142 return -1;
143 }
144
145 /* kernel uses private struct tm definition to be self contained */
146 tm->tm_sec = rtc_tm.tm_sec;
147 tm->tm_min = rtc_tm.tm_min;
148 tm->tm_hour = rtc_tm.tm_hour;
149 tm->tm_mday = rtc_tm.tm_mday;
150 tm->tm_mon = rtc_tm.tm_mon;
151 tm->tm_year = rtc_tm.tm_year;
152 tm->tm_wday = rtc_tm.tm_wday;
153 tm->tm_yday = rtc_tm.tm_yday;
154 tm->tm_isdst = -1; /* don't know whether it's dst */
155 return 0;
156 }
157
158 /*
159 * Wait for the top of a clock tick by reading /dev/rtc in a busy loop
160 * until we see it. This function is used for rtc drivers without ioctl
161 * interrupts. This is typical on an Alpha, where the Hardware Clock
162 * interrupts are used by the kernel for the system clock, so aren't at
163 * the user's disposal.
164 */
165 static int busywait_for_rtc_clock_tick(const struct hwclock_control *ctl,
166 const int rtc_fd)
167 {
168 struct tm start_time = { 0 };
169 /* The time when we were called (and started waiting) */
170 struct tm nowtime = { 0 };
171 int rc;
172 struct timeval begin = { 0 }, now = { 0 };
173
174 if (ctl->verbose) {
175 printf("ioctl(%d, RTC_UIE_ON, 0): %s\n",
176 rtc_fd, strerror(errno));
177 printf(_("Waiting in loop for time from %s to change\n"),
178 rtc_dev_name);
179 }
180
181 if (do_rtc_read_ioctl(rtc_fd, &start_time))
182 return 1;
183
184 /*
185 * Wait for change. Should be within a second, but in case
186 * something weird happens, we have a time limit (1.5s) on this loop
187 * to reduce the impact of this failure.
188 */
189 gettime_monotonic(&begin);
190 do {
191 rc = do_rtc_read_ioctl(rtc_fd, &nowtime);
192 if (rc || start_time.tm_sec != nowtime.tm_sec)
193 break;
194 gettime_monotonic(&now);
195 if (time_diff(now, begin) > 1.5) {
196 warnx(_("Timed out waiting for time change."));
197 return 1;
198 }
199 } while (1);
200
201 if (rc)
202 return 1;
203 return 0;
204 }
205
206 /*
207 * Same as synchronize_to_clock_tick(), but just for /dev/rtc.
208 */
209 static int synchronize_to_clock_tick_rtc(const struct hwclock_control *ctl)
210 {
211 int rtc_fd; /* File descriptor of /dev/rtc */
212 int ret = 1;
213
214 rtc_fd = open_rtc(ctl);
215 if (rtc_fd == -1) {
216 warn(_("cannot open rtc device"));
217 return ret;
218 }
219
220 /* Turn on update interrupts (one per second) */
221 int rc = ioctl(rtc_fd, RTC_UIE_ON, 0);
222
223 if (rc != -1) {
224 /*
225 * Just reading rtc_fd fails on broken hardware: no
226 * update interrupt comes and a bootscript with a
227 * hwclock call hangs
228 */
229 fd_set rfds;
230 struct timeval tv;
231
232 /*
233 * Wait up to ten seconds for the next update
234 * interrupt
235 */
236 FD_ZERO(&rfds);
237 FD_SET(rtc_fd, &rfds);
238 tv.tv_sec = 10;
239 tv.tv_usec = 0;
240 rc = select(rtc_fd + 1, &rfds, NULL, NULL, &tv);
241 if (0 < rc)
242 ret = 0;
243 else if (rc == 0) {
244 warnx(_("select() to %s to wait for clock tick timed out"),
245 rtc_dev_name);
246 } else
247 warn(_("select() to %s to wait for clock tick failed"),
248 rtc_dev_name);
249 /* Turn off update interrupts */
250 rc = ioctl(rtc_fd, RTC_UIE_OFF, 0);
251 if (rc == -1)
252 warn(_("ioctl() to %s to turn off update interrupts failed"),
253 rtc_dev_name);
254 } else if (errno == ENOTTY || errno == EINVAL) {
255 /* rtc ioctl interrupts are unimplemented */
256 ret = busywait_for_rtc_clock_tick(ctl, rtc_fd);
257 } else
258 warn(_("ioctl(%d, RTC_UIE_ON, 0) to %s failed"),
259 rtc_fd, rtc_dev_name);
260 return ret;
261 }
262
263 static int read_hardware_clock_rtc(const struct hwclock_control *ctl,
264 struct tm *tm)
265 {
266 int rtc_fd, rc;
267
268 rtc_fd = open_rtc_or_exit(ctl);
269
270 /* Read the RTC time/date, return answer via tm */
271 rc = do_rtc_read_ioctl(rtc_fd, tm);
272
273 return rc;
274 }
275
276 /*
277 * Set the Hardware Clock to the broken down time <new_broken_time>. Use
278 * ioctls to "rtc" device /dev/rtc.
279 */
280 static int set_hardware_clock_rtc(const struct hwclock_control *ctl,
281 const struct tm *new_broken_time)
282 {
283 int rc = -1;
284 int rtc_fd;
285 struct rtc_time rtc_tm = { 0 };
286
287 rtc_fd = open_rtc_or_exit(ctl);
288
289 /* kernel uses private struct tm definition to be self contained */
290 rtc_tm.tm_sec = new_broken_time->tm_sec;
291 rtc_tm.tm_min = new_broken_time->tm_min;
292 rtc_tm.tm_hour = new_broken_time->tm_hour;
293 rtc_tm.tm_mday = new_broken_time->tm_mday;
294 rtc_tm.tm_mon = new_broken_time->tm_mon;
295 rtc_tm.tm_year = new_broken_time->tm_year;
296 rtc_tm.tm_wday = new_broken_time->tm_wday;
297 rtc_tm.tm_yday = new_broken_time->tm_yday;
298 rtc_tm.tm_isdst = new_broken_time->tm_isdst;
299
300 rc = ioctl(rtc_fd, RTC_SET_TIME, &rtc_tm);
301
302 if (rc == -1) {
303 warn(_("ioctl(RTC_SET_TIME) to %s to set the time failed"),
304 rtc_dev_name);
305 hwclock_exit(ctl, EXIT_FAILURE);
306 }
307
308 if (ctl->verbose)
309 printf(_("ioctl(RTC_SET_TIME) was successful.\n"));
310
311 return 0;
312 }
313
314 static int get_permissions_rtc(void)
315 {
316 return 0;
317 }
318
319 static const char *get_device_path(void)
320 {
321 return rtc_dev_name;
322 }
323
324 static const struct clock_ops rtc_interface = {
325 N_("Using the rtc interface to the clock."),
326 get_permissions_rtc,
327 read_hardware_clock_rtc,
328 set_hardware_clock_rtc,
329 synchronize_to_clock_tick_rtc,
330 get_device_path,
331 };
332
333 /* return &rtc if /dev/rtc can be opened, NULL otherwise */
334 const struct clock_ops *probe_for_rtc_clock(const struct hwclock_control *ctl)
335 {
336 const int rtc_fd = open_rtc(ctl);
337
338 if (rtc_fd < 0)
339 return NULL;
340 return &rtc_interface;
341 }
342
343 #ifdef __alpha__
344 /*
345 * Get the Hardware Clock epoch setting from the kernel.
346 */
347 int get_epoch_rtc(const struct hwclock_control *ctl, unsigned long *epoch_p)
348 {
349 int rtc_fd;
350
351 rtc_fd = open_rtc(ctl);
352 if (rtc_fd < 0) {
353 warn(_("cannot open %s"), rtc_dev_name);
354 return 1;
355 }
356
357 if (ioctl(rtc_fd, RTC_EPOCH_READ, epoch_p) == -1) {
358 warn(_("ioctl(%d, RTC_EPOCH_READ, epoch_p) to %s failed"),
359 rtc_fd, rtc_dev_name);
360 return 1;
361 }
362
363 if (ctl->verbose)
364 printf(_("ioctl(%d, RTC_EPOCH_READ, epoch_p) to %s succeeded.\n"),
365 rtc_fd, rtc_dev_name);
366
367 return 0;
368 }
369
370 /*
371 * Set the Hardware Clock epoch in the kernel.
372 */
373 int set_epoch_rtc(const struct hwclock_control *ctl)
374 {
375 int rtc_fd;
376 unsigned long epoch;
377
378 errno = 0;
379 epoch = strtoul(ctl->epoch_option, NULL, 10);
380
381 /* There were no RTC clocks before 1900. */
382 if (errno || epoch < 1900 || epoch == ULONG_MAX) {
383 warnx(_("invalid epoch '%s'."), ctl->epoch_option);
384 return 1;
385 }
386
387 rtc_fd = open_rtc(ctl);
388 if (rtc_fd < 0) {
389 warn(_("cannot open %s"), rtc_dev_name);
390 return 1;
391 }
392
393 if (ioctl(rtc_fd, RTC_EPOCH_SET, epoch) == -1) {
394 warn(_("ioctl(%d, RTC_EPOCH_SET, %lu) to %s failed"),
395 rtc_fd, epoch, rtc_dev_name);
396 return 1;
397 }
398
399 if (ctl->verbose)
400 printf(_("ioctl(%d, RTC_EPOCH_SET, %lu) to %s succeeded.\n"),
401 rtc_fd, epoch, rtc_dev_name);
402
403 return 0;
404 }
405 #endif /* __alpha__ */
406
407
408
409 static int resolve_rtc_param_alias(const char *alias, __u64 *value)
410 {
411 const struct hwclock_param *param = &hwclock_params[0];
412
413 while (param->name) {
414 if (!strcmp(alias, param->name)) {
415 *value = param->id;
416 return 0;
417 }
418 param++;
419 }
420
421 return 1;
422 }
423
424 /* kernel uapi __u64 can be defined differently than uint64_t */
425 static int strtoku64(const char *str, __u64 *num, int base)
426 {
427 return ul_strtou64(str, (uint64_t *) &num, base);
428 }
429
430 /*
431 * Get the Hardware Clock parameter setting from the kernel.
432 */
433 int get_param_rtc(const struct hwclock_control *ctl,
434 const char *name, uint64_t *id, uint64_t *value)
435 {
436 int rtc_fd;
437 struct rtc_param param = { .param = 0 };
438
439 /* handle name */
440 if (resolve_rtc_param_alias(name, &param.param) != 0
441 && strtoku64(name, &param.param, 0) != 0) {
442 warnx(_("could not convert parameter name to number"));
443 return 1;
444 }
445
446 /* get parameter */
447 rtc_fd = open_rtc(ctl);
448 if (rtc_fd < 0) {
449 warn(_("cannot open %s"), rtc_dev_name);
450 return 1;
451 }
452
453 if (ioctl(rtc_fd, RTC_PARAM_GET, &param) == -1) {
454 warn(_("ioctl(%d, RTC_PARAM_GET, param) to %s failed"),
455 rtc_fd, rtc_dev_name);
456 return 1;
457 }
458
459 if (id)
460 *id = param.param;
461 if (value)
462 *value = param.uvalue;
463
464 if (ctl->verbose)
465 printf(_("ioctl(%d, RTC_PARAM_GET, param) to %s succeeded.\n"),
466 rtc_fd, rtc_dev_name);
467
468 return 0;
469 }
470
471 /*
472 * Set the Hardware Clock parameter in the kernel.
473 */
474 int set_param_rtc(const struct hwclock_control *ctl, const char *opt0)
475 {
476 int rtc_fd, rc = 1;
477 struct rtc_param param = { .param = 0 };
478 char *tok, *opt = xstrdup(opt0);
479
480 /* handle name */
481 tok = strtok(opt, "=");
482 if (resolve_rtc_param_alias(tok, &param.param) != 0
483 && strtoku64(tok, &param.param, 0) != 0) {
484 warnx(_("could not convert parameter name to number"));
485 goto done;
486 }
487
488 /* handle value */
489 tok = strtok(NULL, "=");
490 if (!tok) {
491 warnx(_("expected <param>=<value>"));
492 goto done;
493 }
494 if (strtoku64(tok, &param.uvalue, 0) != 0) {
495 warnx(_("could not convert parameter value to number"));
496 goto done;
497 }
498
499 /* set parameter */
500 rtc_fd = open_rtc(ctl);
501 if (rtc_fd < 0) {
502 warnx(_("cannot open %s"), rtc_dev_name);
503 return 1;
504 }
505
506 if (ioctl(rtc_fd, RTC_PARAM_SET, &param) == -1) {
507 warn(_("ioctl(%d, RTC_PARAM_SET, param) to %s failed"),
508 rtc_fd, rtc_dev_name);
509 goto done;
510 }
511
512 if (ctl->verbose)
513 printf(_("ioctl(%d, RTC_PARAM_SET, param) to %s succeeded.\n"),
514 rtc_fd, rtc_dev_name);
515
516 rc = 0;
517 done:
518 free(opt);
519 return rc;
520 }
521
522 #ifndef RTC_VL_DATA_INVALID
523 #define RTC_VL_DATA_INVALID 0x1
524 #endif
525 #ifndef RTC_VL_BACKUP_LOW
526 #define RTC_VL_BACKUP_LOW 0x2
527 #endif
528 #ifndef RTC_VL_BACKUP_EMPTY
529 #define RTC_VL_BACKUP_EMPTY 0x4
530 #endif
531 #ifndef RTC_VL_ACCURACY_LOW
532 #define RTC_VL_ACCURACY_LOW 0x8
533 #endif
534 #ifndef RTC_VL_BACKUP_SWITCH
535 #define RTC_VL_BACKUP_SWITCH 0x10
536 #endif
537
538 int rtc_vl_read(const struct hwclock_control *ctl)
539 {
540 unsigned int vl;
541 int rtc_fd;
542 size_t i;
543 static const struct vl_bit {
544 unsigned int bit;
545 const char *desc;
546 } vl_bits[] = {
547 { RTC_VL_DATA_INVALID, N_("Voltage too low, RTC data is invalid") },
548 { RTC_VL_BACKUP_LOW, N_("Backup voltage is low") },
549 { RTC_VL_BACKUP_EMPTY, N_("Backup empty or not present") },
550 { RTC_VL_ACCURACY_LOW, N_("Voltage is low, RTC accuracy is reduced") },
551 { RTC_VL_BACKUP_SWITCH, N_("Backup switchover happened") },
552 };
553
554 rtc_fd = open_rtc(ctl);
555 if (rtc_fd < 0) {
556 warnx(_("cannot open %s"), rtc_dev_name);
557 return 1;
558 }
559
560 if (ioctl(rtc_fd, RTC_VL_READ, &vl) == -1) {
561 warn(_("ioctl(%d, RTC_VL_READ) on %s failed"),
562 rtc_fd, rtc_dev_name);
563 return 1;
564 }
565
566 if (ctl->verbose) {
567 printf(_("ioctl(%d, RTC_VL_READ) on %s returned 0x%x\n"),
568 rtc_fd, rtc_dev_name, vl);
569 }
570
571 for (i = 0; i < ARRAY_SIZE(vl_bits); ++i) {
572 const struct vl_bit *vlb = &vl_bits[i];
573
574 if (vl & vlb->bit) {
575 printf("0x%02x - %s\n", vlb->bit, vlb->desc);
576 vl &= ~vlb->bit;
577 }
578 }
579 if (vl)
580 printf("0x%02x - unknown bit(s)\n", vl);
581
582 return 0;
583 }
584
585 int rtc_vl_clear(const struct hwclock_control *ctl)
586 {
587 int rtc_fd;
588
589 rtc_fd = open_rtc(ctl);
590 if (rtc_fd < 0) {
591 warnx(_("cannot open %s"), rtc_dev_name);
592 return 1;
593 }
594
595 if (ioctl(rtc_fd, RTC_VL_CLR) == -1) {
596 warn(_("ioctl(%d, RTC_VL_CLEAR) on %s failed"),
597 rtc_fd, rtc_dev_name);
598 return 1;
599 }
600
601 if (ctl->verbose)
602 printf(_("ioctl(%d, RTC_VL_CLEAR) on %s succeeded.\n"),
603 rtc_fd, rtc_dev_name);
604
605 return 0;
606 }