summaryrefslogtreecommitdiffstats
path: root/commands/hwclock.c
diff options
context:
space:
mode:
authorSascha Hauer <s.hauer@pengutronix.de>2015-12-11 15:40:55 +0100
committerSascha Hauer <s.hauer@pengutronix.de>2016-01-07 08:46:09 +0100
commit616143ca8eeab42f5e043b0a86709622772cdc34 (patch)
treedc817b1b88754ba88e04fa9eb2ced95271d6ee8c /commands/hwclock.c
parentf98b02365ff32646475b1c39e3affd9502d27280 (diff)
downloadbarebox-616143ca8eeab42f5e043b0a86709622772cdc34.tar.gz
hwclock: Allow to set hwclock from sntp
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Diffstat (limited to 'commands/hwclock.c')
-rw-r--r--commands/hwclock.c27
1 files changed, 26 insertions, 1 deletions
diff --git a/commands/hwclock.c b/commands/hwclock.c
index 3f89cce..7633132 100644
--- a/commands/hwclock.c
+++ b/commands/hwclock.c
@@ -4,6 +4,7 @@
#include <linux/err.h>
#include <linux/ctype.h>
#include <rtc.h>
+#include <sntp.h>
#include <linux/rtc.h>
#include <string.h>
#include <environment.h>
@@ -92,8 +93,10 @@ static int do_hwclock(int argc, char *argv[])
char *env_name = NULL;
int opt;
int set = 0;
+ int ntp_to_hw = 0;
+ char *ntpserver = NULL;
- while ((opt = getopt(argc, argv, "f:s:e:")) > 0) {
+ while ((opt = getopt(argc, argv, "f:s:e:n:")) > 0) {
int ret;
switch (opt) {
@@ -116,6 +119,10 @@ static int do_hwclock(int argc, char *argv[])
case 'e':
env_name = optarg;
break;
+ case 'n':
+ ntp_to_hw = 1;
+ ntpserver = optarg;
+ break;
}
}
@@ -127,6 +134,23 @@ static int do_hwclock(int argc, char *argv[])
return rtc_set_time(r, &stm);
}
+ if (ntp_to_hw) {
+ s64 now;
+
+ if (!IS_ENABLED(CONFIG_NET_SNTP)) {
+ printf("SNTP support is disabled\n");
+ return 1;
+ }
+
+ now = sntp(ntpserver);
+ if (now < 0)
+ return now;
+
+ rtc_time_to_tm(now, &stm);
+ printf("%s\n", time_str(&stm));
+ return rtc_set_time(r, &stm);
+ }
+
rtc_read_time(r, &tm);
if (env_name) {
@@ -147,6 +171,7 @@ BAREBOX_CMD_HELP_START(hwclock)
BAREBOX_CMD_HELP_TEXT("Options:")
BAREBOX_CMD_HELP_OPT ("-f NAME\t\t\t", "RTC device name (default rtc0)")
BAREBOX_CMD_HELP_OPT ("-e VARNAME\t\t", "store RTC readout into variable VARNAME")
+BAREBOX_CMD_HELP_OPT ("-n NTPSERVER\t", "set RTC from NTP server")
BAREBOX_CMD_HELP_OPT ("-s ccyymmddHHMM[.SS]\t", "set time")
BAREBOX_CMD_HELP_END