blob: 96f8f5d0a417638dd73aba876db738526a9a8a26 [file] [log] [blame]
Eric Biederman05f26fc2003-06-11 21:55:00 +00001#include <pc80/mc146818rtc.h>
2#include <part/fallback_boot.h>
3
Eric Biederman8d9c1232003-06-17 08:42:17 +00004#ifndef MAX_REBOOT_CNT
5#error "MAX_REBOOT_CNT not defined"
6#endif
7#if MAX_REBOOT_CNT > 15
8#error "MAX_REBOOT_CNT too high"
9#endif
10
Eric Biederman05f26fc2003-06-11 21:55:00 +000011static unsigned char cmos_read(unsigned char addr)
12{
13 outb(addr, RTC_BASE_PORT + 0);
14 return inb(RTC_BASE_PORT + 1);
15}
16
17static void cmos_write(unsigned char val, unsigned char addr)
18{
19 outb(addr, RTC_BASE_PORT + 0);
20 outb(val, RTC_BASE_PORT + 1);
21}
22
23static int cmos_error(void)
24{
25 unsigned char reg_d;
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;
29}
30
31static int cmos_chksum_valid(void)
32{
33 unsigned char addr;
34 unsigned long sum, old_sum;
35 sum = 0;
36 /* Comput the cmos checksum */
37 for(addr = LB_CKS_RANGE_START; addr <= LB_CKS_RANGE_END; addr++) {
38 sum += cmos_read(addr);
39 }
40 sum = (sum & 0xffff) ^ 0xffff;
41
42 /* Read the stored checksum */
43 old_sum = cmos_read(LB_CKS_LOC) << 8;
44 old_sum |= cmos_read(LB_CKS_LOC+1);
45
46 return sum == old_sum;
47}
48
49
50static int do_normal_boot(void)
51{
52 unsigned char byte;
53
54 if (cmos_error() || !cmos_chksum_valid()) {
55 unsigned char byte;
56 /* There are no impossible values, no cheksums so just
57 * trust whatever value we have in the the cmos,
58 * but clear the fallback bit.
59 */
60 byte = cmos_read(RTC_BOOT_BYTE);
61 byte &= 0x0c;
62 byte |= MAX_REBOOT_CNT << 4;
63 cmos_write(byte, RTC_BOOT_BYTE);
64 }
65
66 /* The RTC_BOOT_BYTE is now o.k. see where to go. */
67 byte = cmos_read(RTC_BOOT_BYTE);
68
69 /* Are we in normal mode? */
70 if (byte & 1) {
71 byte &= 0x0f; /* yes, clear the boot count */
72 }
73
74 /* Are we already at the max count? */
75 if ((byte >> 4) < MAX_REBOOT_CNT) {
76 byte += 1 << 4; /* No, add 1 to the count */
77 }
78 else {
79 byte &= 0xfc; /* Yes, put in fallback mode */
80 }
81
82 /* Is this the first boot? */
83 if ((byte >> 4) <= 1) {
84 byte = (byte & 0xfc) | ((byte & 1) << 1); /* yes, shift the boot bits */
85 }
86
87 /* Save the boot byte */
88 cmos_write(byte, RTC_BOOT_BYTE);
89
90 return ((byte >> 4) < MAX_REBOOT_CNT);
91}