]> git.karo-electronics.de Git - karo-tx-linux.git/commitdiff
kvm tools: Fix reported year in RTC emulation
authorRonald G. Minnich <rminnich@gmail.com>
Mon, 15 Oct 2012 16:20:22 +0000 (09:20 -0700)
committerPekka Enberg <penberg@kernel.org>
Tue, 16 Oct 2012 07:33:08 +0000 (10:33 +0300)
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 <rminnich@gmail.com>
[ penberg@kernel.org: cleanups ]
Signed-off-by: Pekka Enberg <penberg@kernel.org>
tools/kvm/hw/rtc.c

index ad6dd65ebf3393385330ea454a3e7a6a53011ba9..4a862d6ebb5deb01fe5c81536520fd9469a62eb0 100644 (file)
@@ -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]);