/* rtcsync.c */ /* read RTC and SetLocalTime (Ver.0.01) */ /* written by linear a */ /* create: Fri Feb 11 22:19:48 JST 2000 */ /* last update: Thu Feb 17 01:42:26 JST 2000 */ /* license: GPL */ /* require: directio.zip (DDJ 1996/5) and instdrv (NT DDK) or equivalent */ #include #include #include #include #define DEBUG 0 #if DEBUG #define dprintf(a) printf a #else #define dprintf(a) /* epsilon */ #endif #define bcd2int(a) (((((a) & 0xf0) >> 4) * 10) + ((a) & 0xf)) /* These macros come from Linux kernel arch/i386/time.c */ #define CMOS_READ(addr) (_outp(0x70, (addr)), _inp(0x71)) #define RTC_SECONDS 0 #define RTC_MINUTES 2 #define RTC_HOURS 4 #define RTC_DAY_OF_WEEK 6 #define RTC_DAY_OF_MONTH 7 #define RTC_MONTH 8 #define RTC_YEAR 9 SYSTEMTIME tm; int main(int argc, char *argv[]) { unsigned char sec, year; HANDLE h; h = CreateFile("\\\\.\\giveio", GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if(h == INVALID_HANDLE_VALUE) { printf("Couldn't access giveio device\n"); return -1; } CloseHandle(h); GetLocalTime(&tm); dprintf(("kernel time is %d/%d/%d %d:%d:%d\n", tm.wYear, tm.wMonth, tm.wDay, tm.wHour, tm.wMinute, tm.wSecond)); do { sec = CMOS_READ(RTC_SECONDS); tm.wMinute = bcd2int(CMOS_READ(RTC_MINUTES)); tm.wHour = bcd2int(CMOS_READ(RTC_HOURS)); tm.wDay = bcd2int(CMOS_READ(RTC_DAY_OF_MONTH)); tm.wMonth = bcd2int(CMOS_READ(RTC_MONTH)); year = bcd2int(CMOS_READ(RTC_YEAR)); } while (sec != CMOS_READ(RTC_SECONDS)); tm.wSecond = bcd2int(sec); /* avoid patent ?! */ if (year < 50) { tm.wYear = year + 2000; } else { tm.wYear = year + 1900; } dprintf(("hardware time is %d/%d/%d %d:%d:%d\n", tm.wYear, tm.wMonth, tm.wDay, tm.wHour, tm.wMinute, tm.wSecond)); if (argc > 1 && strcmp(argv[1], "-n") == 0) { dprintf(("time is not set\n")); } else { SetLocalTime(&tm); dprintf(("time is set\n")); } return 0; }