blob: 0c7d822718136e4c9a98073bf1d4830ac64a96ad [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
Eric Biederman2c018fb2003-07-21 20:13:45 +00007#if MAX_REBOOT_CNT > 15
Eric Biederman8d9c1232003-06-17 08:42:17 +00008#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
Eric Biederman9b4336c2003-07-19 04:28:22 +000050static int last_boot_normal(void)
51{
52 unsigned char byte;
53 byte = cmos_read(RTC_BOOT_BYTE);
54 return (byte & (1 << 1));
55}
56
Eric Biederman05f26fc2003-06-11 21:55:00 +000057static int do_normal_boot(void)
58{
59 unsigned char byte;
60
61 if (cmos_error() || !cmos_chksum_valid()) {
62 unsigned char byte;
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.
66 */
67 byte = cmos_read(RTC_BOOT_BYTE);
68 byte &= 0x0c;
69 byte |= MAX_REBOOT_CNT << 4;
70 cmos_write(byte, RTC_BOOT_BYTE);
71 }
72
73 /* The RTC_BOOT_BYTE is now o.k. see where to go. */
74 byte = cmos_read(RTC_BOOT_BYTE);
75
76 /* Are we in normal mode? */
77 if (byte & 1) {
78 byte &= 0x0f; /* yes, clear the boot count */
79 }
80
Eric Biederman2c018fb2003-07-21 20:13:45 +000081 /* Properly set the last boot flag */
82 byte &= 0xfc;
83 if ((byte >> 4) < MAX_REBOOT_CNT) {
84 byte |= (1<<1);
85 }
86
Eric Biederman05f26fc2003-06-11 21:55:00 +000087 /* Are we already at the max count? */
88 if ((byte >> 4) < MAX_REBOOT_CNT) {
89 byte += 1 << 4; /* No, add 1 to the count */
90 }
91 else {
92 byte &= 0xfc; /* Yes, put in fallback mode */
93 }
94
Eric Biederman05f26fc2003-06-11 21:55:00 +000095 /* Save the boot byte */
96 cmos_write(byte, RTC_BOOT_BYTE);
97
Eric Biederman2c018fb2003-07-21 20:13:45 +000098 return (byte & (1<<1));
Eric Biederman05f26fc2003-06-11 21:55:00 +000099}
Eric Biederman5cd81732004-03-11 15:01:31 +0000100
101static unsigned read_option(unsigned start, unsigned size, unsigned def)
102{
103#if USE_OPTION_TABLE == 1
104 unsigned byte;
105 byte = cmos_read(start/8);
106 return (byte >> (start & 7U)) & ((1U << size) - 1U);
107#else
108 return def;
109#endif
110}