1 #include <pc80/mc146818rtc.h>
2 #include <part/fallback_boot.h>
5 #error "MAX_REBOOT_CNT not defined"
7 #if MAX_REBOOT_CNT > 14
8 #error "MAX_REBOOT_CNT too high"
11 static unsigned char cmos_read(unsigned char addr)
13 outb(addr, RTC_BASE_PORT + 0);
14 return inb(RTC_BASE_PORT + 1);
17 static void cmos_write(unsigned char val, unsigned char addr)
19 outb(addr, RTC_BASE_PORT + 0);
20 outb(val, RTC_BASE_PORT + 1);
23 static int cmos_error(void)
26 /* See if the cmos error condition has been flagged */
27 reg_d = cmos_read(RTC_REG_D);
28 return (reg_d & RTC_VRT) == 0;
31 static int cmos_chksum_valid(void)
34 unsigned long sum, old_sum;
36 /* Comput the cmos checksum */
37 for(addr = LB_CKS_RANGE_START; addr <= LB_CKS_RANGE_END; addr++) {
38 sum += cmos_read(addr);
40 sum = (sum & 0xffff) ^ 0xffff;
42 /* Read the stored checksum */
43 old_sum = cmos_read(LB_CKS_LOC) << 8;
44 old_sum |= cmos_read(LB_CKS_LOC+1);
46 return sum == old_sum;
50 static int last_boot_normal(void)
53 byte = cmos_read(RTC_BOOT_BYTE);
54 return (byte & (1 << 1));
57 static int do_normal_boot(void)
61 if (cmos_error() || !cmos_chksum_valid()) {
63 /* There are no impossible values, no cheksums so just
64 * trust whatever value we have in the the cmos,
65 * but clear the fallback bit.
67 byte = cmos_read(RTC_BOOT_BYTE);
69 byte |= MAX_REBOOT_CNT << 4;
70 cmos_write(byte, RTC_BOOT_BYTE);
73 /* The RTC_BOOT_BYTE is now o.k. see where to go. */
74 byte = cmos_read(RTC_BOOT_BYTE);
76 /* Are we in normal mode? */
78 byte &= 0x0f; /* yes, clear the boot count */
81 /* Are we already at the max count? */
82 if ((byte >> 4) < MAX_REBOOT_CNT) {
83 byte += 1 << 4; /* No, add 1 to the count */
86 byte &= 0xfc; /* Yes, put in fallback mode */
89 /* Is this the first boot? */
90 if ((byte >> 4) <= 1) {
91 byte = (byte & 0xfc) | ((byte & 1) << 1); /* yes, shift the boot bits */
94 /* Save the boot byte */
95 cmos_write(byte, RTC_BOOT_BYTE);
97 return ((byte >> 4) < MAX_REBOOT_CNT);