Bug 195703 - Incorrect pre-y2k time retrieval from RTC
Summary: Incorrect pre-y2k time retrieval from RTC
Status: Closed FIXED
Alias: None
Product: Base System
Classification: Unclassified
Component: kern (show other bugs)
Version: 10.1-STABLE
Hardware: i386 Any
: --- Affects Some People
Assignee: freebsd-bugs (Nobody)
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-12-05 08:29 UTC by cs
Modified: 2015-07-13 12:01 UTC (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description cs 2014-12-05 08:29:05 UTC
When booting on old hardware with CMOS clock set before year 2000, initial system time appears to be in the end of twenty first century. On 32-bit systems it falls beyond 2038 with interesting consequences.

This regression was introduced in 2006 by r162963 (then it was sys/i386/isa/clock.c).

Here is the patch to correct this problem on modern systems:

--- /usr/src/sys/x86/isa/atrtc.c        2014-08-14 06:33:26.168778002 +0000
+++ atrtc.c     2014-12-04 10:50:10.597250292 +0000
@@ -354,7 +354,7 @@
 #ifdef USE_RTC_CENTURY
        ct.year += readrtc(RTC_CENTURY) * 100;
 #else
-       ct.year += 2000;
+       ct.year += ( ct.year < 70 ? 2000 : 1900 );
 #endif
        critical_exit();
        /* Set dow = -1 because some clocks don't set it correctly. */
Comment 1 Warner Losh freebsd_committer freebsd_triage 2015-06-29 16:04:28 UTC
1980 would be a better pivot year:

The PC didn't exist before 1985. MS DOS doesn't have a filesystem that can cope before 1980 in any meaningful way, so most BIOSs default to 1980 or later when they have no clue. They don't know anything about 1970 being magic to unix. Also, it buys us 10 years before we have to look at this again, though predicting about when the i386 port will run out of steam in FreeBSD is a bit tricky...

Other than that, this change looks good to me.
Comment 2 commit-hook freebsd_committer freebsd_triage 2015-06-29 17:02:25 UTC
A commit references this bug:

Author: brueffer
Date: Mon Jun 29 17:02:10 UTC 2015
New revision: 284931
URL: https://svnweb.freebsd.org/changeset/base/284931

Log:
  Set the initial system time to a sane (as in: not end of 21st century) value when
  booting on a PC with CMOS clock set to a year before 2000.

  This uses 1980 (instead of 1970 as in the initial patch) as pivot year as
  suggested by imp in the PR followup.

  PR:		195703
  Submitted by:	cs@soi.spb.ru
  Reviewed by:	imp
  MFC after:	1 weeks

Changes:
  head/sys/x86/isa/atrtc.c
Comment 3 Christian Brueffer freebsd_committer freebsd_triage 2015-06-29 17:04:07 UTC
Patch committed, including the adjustment suggested by Warner.

Thanks for the submission!
Comment 4 commit-hook freebsd_committer freebsd_triage 2015-07-13 11:58:27 UTC
A commit references this bug:

Author: brueffer
Date: Mon Jul 13 11:58:09 UTC 2015
New revision: 285446
URL: https://svnweb.freebsd.org/changeset/base/285446

Log:
  MFC: r284931

  Set the initial system time to a sane (as in: not end of 21st century)
  value when booting on a PC with CMOS clock set to a year before 2000.

  This uses 1980 (instead of 1970 as in the initial patch) as pivot year as
  suggested by imp in the PR followup.

  PR:		195703
  Submitted by:	cs@soi.spb.ru
  Reviewed by:	imp
  Approved by:	re (gjb)

Changes:
_U  stable/10/
  stable/10/sys/x86/isa/atrtc.c
Comment 5 commit-hook freebsd_committer freebsd_triage 2015-07-13 12:00:30 UTC
A commit references this bug:

Author: brueffer
Date: Mon Jul 13 11:59:29 UTC 2015
New revision: 285447
URL: https://svnweb.freebsd.org/changeset/base/285447

Log:
  MFC: r284931

  Set the initial system time to a sane (as in: not end of 21st century)
  value when booting on a PC with CMOS clock set to a year before 2000.

  This uses 1980 (instead of 1970 as in the initial patch) as pivot year as
  suggested by imp in the PR followup.

  PR:		195703
  Submitted by:	cs@soi.spb.ru
  Reviewed by:	imp

Changes:
_U  stable/9/sys/
  stable/9/sys/x86/isa/atrtc.c
Comment 6 Christian Brueffer freebsd_committer freebsd_triage 2015-07-13 12:01:17 UTC
Merges done, thanks again!