From 13d509712aa10399af28935fda45d4a5f0c9bb1f Mon Sep 17 00:00:00 2001 From: "Ronald G. Minnich" Date: Mon, 15 Oct 2012 09:20:22 -0700 Subject: [PATCH] kvm tools: Fix reported year in RTC emulation MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit The gmtime() function returns the number of years since 1900. On PCs, the "design" of CMOS dates is such that the years (in this case) are years since 2000. Fix up RTC emulation as follows: - if year is > 99, subtract 100 - if year is 99 (20th century) take it as it is Signed-off-by: Ronald G. Minnich [ penberg@kernel.org: cleanups ] Signed-off-by: Pekka Enberg --- tools/kvm/hw/rtc.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/tools/kvm/hw/rtc.c b/tools/kvm/hw/rtc.c index ad6dd65ebf33..4a862d6ebb5d 100644 --- a/tools/kvm/hw/rtc.c +++ b/tools/kvm/hw/rtc.c @@ -40,6 +40,7 @@ static bool cmos_ram_data_in(struct ioport *ioport, struct kvm *kvm, u16 port, v { struct tm *tm; time_t ti; + int year; time(&ti); @@ -65,7 +66,16 @@ static bool cmos_ram_data_in(struct ioport *ioport, struct kvm *kvm, u16 port, v ioport__write8(data, bin2bcd(tm->tm_mon + 1)); break; case RTC_YEAR: - ioport__write8(data, bin2bcd(tm->tm_year)); + /* + * The gmtime() function returns time since 1900. The CMOS + * standard is time since 2000. If the year is < 100, do + * nothing; if it is > 100, subtract 100; this is the best fit + * with the twisted CMOS logic. + */ + year = tm->tm_year; + if (year > 99) + year -= 100; + ioport__write8(data, bin2bcd(year)); break; default: ioport__write8(data, rtc.cmos_data[rtc.cmos_idx]); -- 2.39.5