Eric Biederman | 05f26fc | 2003-06-11 21:55:00 +0000 | [diff] [blame] | 1 | #include <pc80/mc146818rtc.h> |
| 2 | #include <part/fallback_boot.h> |
| 3 | |
Eric Biederman | 8d9c123 | 2003-06-17 08:42:17 +0000 | [diff] [blame] | 4 | #ifndef MAX_REBOOT_CNT |
| 5 | #error "MAX_REBOOT_CNT not defined" |
| 6 | #endif |
Eric Biederman | 9b4336c | 2003-07-19 04:28:22 +0000 | [diff] [blame^] | 7 | #if MAX_REBOOT_CNT > 14 |
Eric Biederman | 8d9c123 | 2003-06-17 08:42:17 +0000 | [diff] [blame] | 8 | #error "MAX_REBOOT_CNT too high" |
| 9 | #endif |
| 10 | |
Eric Biederman | 05f26fc | 2003-06-11 21:55:00 +0000 | [diff] [blame] | 11 | static unsigned char cmos_read(unsigned char addr) |
| 12 | { |
| 13 | outb(addr, RTC_BASE_PORT + 0); |
| 14 | return inb(RTC_BASE_PORT + 1); |
| 15 | } |
| 16 | |
| 17 | static 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 | |
| 23 | static 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 | |
| 31 | static 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 Biederman | 9b4336c | 2003-07-19 04:28:22 +0000 | [diff] [blame^] | 50 | static 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 Biederman | 05f26fc | 2003-06-11 21:55:00 +0000 | [diff] [blame] | 57 | static 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 | |
| 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 */ |
| 84 | } |
| 85 | else { |
| 86 | byte &= 0xfc; /* Yes, put in fallback mode */ |
| 87 | } |
| 88 | |
| 89 | /* Is this the first boot? */ |
| 90 | if ((byte >> 4) <= 1) { |
| 91 | byte = (byte & 0xfc) | ((byte & 1) << 1); /* yes, shift the boot bits */ |
| 92 | } |
| 93 | |
| 94 | /* Save the boot byte */ |
| 95 | cmos_write(byte, RTC_BOOT_BYTE); |
| 96 | |
| 97 | return ((byte >> 4) < MAX_REBOOT_CNT); |
| 98 | } |