diff --git a/src/core/time.c b/src/core/time.c index c353ac5bd..ecdb8613e 100644 --- a/src/core/time.c +++ b/src/core/time.c @@ -44,7 +44,7 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL ); */ /** Current system clock offset */ -signed long time_offset; +signed long long time_offset; /** Days of week (for debugging) */ static const char *weekdays[] = { diff --git a/src/include/ipxe/time.h b/src/include/ipxe/time.h index 89bf90e03..892a747a2 100644 --- a/src/include/ipxe/time.h +++ b/src/include/ipxe/time.h @@ -50,7 +50,7 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL ); /* Include all architecture-dependent time API headers */ #include -extern signed long time_offset; +extern signed long long time_offset; /** * Get current time in seconds (ignoring system clock offset) @@ -65,7 +65,7 @@ time_t time_now ( void ); * @v delta Clock adjustment, in seconds */ static inline __attribute__ (( always_inline )) void -time_adjust ( signed long delta ) { +time_adjust ( signed long long delta ) { time_offset += delta; } diff --git a/src/net/udp/ntp.c b/src/net/udp/ntp.c index 11f8ccc00..9fe252979 100644 --- a/src/net/udp/ntp.c +++ b/src/net/udp/ntp.c @@ -90,6 +90,10 @@ static int ntp_request ( struct ntp_client *ntp ) { hdr.transmit.seconds = htonl ( time ( NULL ) + NTP_EPOCH ); hdr.transmit.fraction = htonl ( NTP_FRACTION_MAGIC ); + DBGC ( ntp, "NTP %p local time (epoch) %lld seconds " + "(when adding NTP_EPOCH 0x%08x)\n", + ntp, time ( NULL ), hdr.transmit.seconds ); + /* Send request */ if ( ( rc = xfer_deliver_raw ( &ntp->xfer, &hdr, sizeof ( hdr ) ) ) != 0 ) { @@ -113,7 +117,7 @@ static int ntp_deliver ( struct ntp_client *ntp, struct io_buffer *iobuf, struct xfer_metadata *meta ) { struct ntp_header *hdr; struct sockaddr_tcpip *st_src; - int32_t delta; + int64_t delta; int rc; /* Check source port */ @@ -154,10 +158,18 @@ static int ntp_deliver ( struct ntp_client *ntp, struct io_buffer *iobuf, goto close; } - /* Calculate clock delta */ - delta = ( ntohl ( hdr->receive.seconds ) - - ntohl ( hdr->originate.seconds ) ); - DBGC ( ntp, "NTP %p delta %d seconds\n", ntp, delta ); + int64_t receive = ntohl( hdr->receive.seconds ); + int64_t originate = ntohl( hdr->originate.seconds ); + + int64_t receive_epoch = receive - NTP_EPOCH; + int64_t originate_epoch = originate - NTP_EPOCH; + + DBGC ( ntp, "NTP %p answered 'receive' %lld (0x%08llx) and 'originate' %lld (0x%08llx)\n", + ntp, receive_epoch, receive_epoch, originate_epoch, originate_epoch ); + + /* Calculate clock delta on 64-bits signed integer */ + delta = receive - originate; + DBGC ( ntp, "NTP %p delta %lld seconds (0x%016llx)\n", ntp, delta, delta ); /* Adjust system clock */ time_adjust ( delta );