]> git.ipfire.org Git - thirdparty/u-boot.git/blame - net/sntp.c
malloc: Enable assertions if UNIT_TEST is enabled
[thirdparty/u-boot.git] / net / sntp.c
CommitLineData
ea287deb
WD
1/*
2 * SNTP support driver
3 *
4 * Masami Komiya <mkomiya@sonare.it> 2005
5 *
6 */
7
8#include <common.h>
9#include <command.h>
17f0ac60 10#include <dm.h>
f7ae49fc 11#include <log.h>
ea287deb
WD
12#include <net.h>
13#include <rtc.h>
14
912ece4c 15#include <net/sntp.h>
ea287deb 16
49f3bdbb 17#define SNTP_TIMEOUT 10000UL
ea287deb 18
38ba2558 19static int sntp_our_port;
ea287deb 20
912ece4c
PR
21/* NTP server IP address */
22struct in_addr net_ntp_server;
23/* offset time from UTC */
24int net_ntp_time_offset;
25
38ba2558 26static void sntp_send(void)
ea287deb
WD
27{
28 struct sntp_pkt_t pkt;
29 int pktlen = SNTP_PACKET_LEN;
30 int sport;
31
0ebf04c6 32 debug("%s\n", __func__);
ea287deb 33
6c3234a3 34 memset(&pkt, 0, sizeof(pkt));
ea287deb
WD
35
36 pkt.li = NTP_LI_NOLEAP;
37 pkt.vn = NTP_VERSION;
38 pkt.mode = NTP_MODE_CLIENT;
39
1203fcce
JH
40 memcpy((char *)net_tx_packet + net_eth_hdr_size() + IP_UDP_HDR_SIZE,
41 (char *)&pkt, pktlen);
ea287deb 42
38ba2558 43 sntp_our_port = 10000 + (get_timer(0) % 4096);
ea287deb
WD
44 sport = NTP_SERVICE_PORT;
45
1203fcce 46 net_send_udp_packet(net_server_ethaddr, net_ntp_server, sport,
38ba2558 47 sntp_our_port, pktlen);
ea287deb
WD
48}
49
38ba2558 50static void sntp_timeout_handler(void)
ea287deb 51{
6c3234a3 52 puts("Timeout\n");
22f6e99d 53 net_set_state(NETLOOP_FAIL);
ea287deb
WD
54 return;
55}
56
049a95a7
JH
57static void sntp_handler(uchar *pkt, unsigned dest, struct in_addr sip,
58 unsigned src, unsigned len)
ea287deb 59{
414eec35 60 struct sntp_pkt_t *rpktp = (struct sntp_pkt_t *)pkt;
ea287deb 61 struct rtc_time tm;
414eec35 62 ulong seconds;
ea287deb 63
0ebf04c6 64 debug("%s\n", __func__);
ea287deb 65
38ba2558 66 if (dest != sntp_our_port)
6c3234a3 67 return;
ea287deb 68
414eec35 69 /*
3c56fb82 70 * As the RTC's used in U-Boot support second resolution only
414eec35
WD
71 * we simply ignore the sub-second field.
72 */
6c3234a3 73 memcpy(&seconds, &rpktp->transmit_timestamp, sizeof(ulong));
ea287deb 74
9f9276c3 75 rtc_to_tm(ntohl(seconds) - 2208988800UL + net_ntp_time_offset, &tm);
e1864db6 76#ifdef CONFIG_DM_RTC
17f0ac60
SG
77 struct udevice *dev;
78 int ret;
79
80 ret = uclass_get_device(UCLASS_RTC, 0, &dev);
81 if (ret)
82 printf("SNTP: cannot find RTC: err=%d\n", ret);
83 else
84 dm_rtc_set(dev, &tm);
e1864db6 85#elif defined(CONFIG_CMD_DATE)
6c3234a3 86 rtc_set(&tm);
ea287deb 87#endif
6c3234a3 88 printf("Date: %4d-%02d-%02d Time: %2d:%02d:%02d\n",
38ba2558
JH
89 tm.tm_year, tm.tm_mon, tm.tm_mday,
90 tm.tm_hour, tm.tm_min, tm.tm_sec);
ea287deb 91
22f6e99d 92 net_set_state(NETLOOP_SUCCESS);
ea287deb
WD
93}
94
912ece4c
PR
95/*
96 * SNTP:
97 *
98 * Prerequisites: - own ethernet address
99 * - own IP address
100 * We want: - network time
101 * Next step: none
102 */
103int sntp_prereq(void *data)
104{
105 if (net_ntp_server.s_addr == 0) {
106 puts("*** ERROR: NTP server address not given\n");
107 return 1;
108 }
109
110 return 0;
111}
112
113int sntp_start(void *data)
ea287deb 114{
0ebf04c6 115 debug("%s\n", __func__);
ea287deb 116
bc0571fc 117 net_set_timeout_handler(SNTP_TIMEOUT, sntp_timeout_handler);
049a95a7 118 net_set_udp_handler(sntp_handler);
0adb5b76 119 memset(net_server_ethaddr, 0, sizeof(net_server_ethaddr));
ea287deb 120
38ba2558 121 sntp_send();
912ece4c
PR
122
123 return 0;
ea287deb 124}