Stefan Reinauer | ea5c2b6 | 2011-10-27 18:42:53 +0200 | [diff] [blame] | 1 | #include <stdint.h> |
Eric Biederman | 05f26fc | 2003-06-11 21:55:00 +0000 | [diff] [blame] | 2 | #include <pc80/mc146818rtc.h> |
Stefan Reinauer | de3206a | 2010-02-22 06:09:43 +0000 | [diff] [blame] | 3 | #include <fallback.h> |
Stefan Reinauer | 10ec0fe | 2010-09-25 10:40:47 +0000 | [diff] [blame] | 4 | #if CONFIG_USE_OPTION_TABLE |
| 5 | #include "option_table.h" |
| 6 | #endif |
Eric Biederman | 05f26fc | 2003-06-11 21:55:00 +0000 | [diff] [blame] | 7 | |
Stefan Reinauer | 0867062 | 2009-06-30 15:17:49 +0000 | [diff] [blame] | 8 | #if CONFIG_MAX_REBOOT_CNT > 15 |
| 9 | #error "CONFIG_MAX_REBOOT_CNT too high" |
Eric Biederman | 8d9c123 | 2003-06-17 08:42:17 +0000 | [diff] [blame] | 10 | #endif |
| 11 | |
Eric Biederman | 05f26fc | 2003-06-11 21:55:00 +0000 | [diff] [blame] | 12 | static int cmos_error(void) |
| 13 | { |
| 14 | unsigned char reg_d; |
| 15 | /* See if the cmos error condition has been flagged */ |
| 16 | reg_d = cmos_read(RTC_REG_D); |
| 17 | return (reg_d & RTC_VRT) == 0; |
| 18 | } |
| 19 | |
| 20 | static int cmos_chksum_valid(void) |
| 21 | { |
Edwin Beasant | eb50c7d | 2010-07-06 21:05:04 +0000 | [diff] [blame] | 22 | #if CONFIG_USE_OPTION_TABLE |
Eric Biederman | 05f26fc | 2003-06-11 21:55:00 +0000 | [diff] [blame] | 23 | unsigned char addr; |
Stefan Reinauer | ea5c2b6 | 2011-10-27 18:42:53 +0200 | [diff] [blame] | 24 | u16 sum, old_sum; |
Eric Biederman | 05f26fc | 2003-06-11 21:55:00 +0000 | [diff] [blame] | 25 | sum = 0; |
Stefan Reinauer | ea5c2b6 | 2011-10-27 18:42:53 +0200 | [diff] [blame] | 26 | /* Compute the cmos checksum */ |
Stefan Reinauer | b5828d7 | 2010-03-29 17:14:28 +0000 | [diff] [blame] | 27 | for(addr = LB_CKS_RANGE_START; addr <= LB_CKS_RANGE_END; addr++) { |
Eric Biederman | 05f26fc | 2003-06-11 21:55:00 +0000 | [diff] [blame] | 28 | sum += cmos_read(addr); |
| 29 | } |
Eric Biederman | 05f26fc | 2003-06-11 21:55:00 +0000 | [diff] [blame] | 30 | |
| 31 | /* Read the stored checksum */ |
Stefan Reinauer | b5828d7 | 2010-03-29 17:14:28 +0000 | [diff] [blame] | 32 | old_sum = cmos_read(LB_CKS_LOC) << 8; |
| 33 | old_sum |= cmos_read(LB_CKS_LOC+1); |
Eric Biederman | 05f26fc | 2003-06-11 21:55:00 +0000 | [diff] [blame] | 34 | |
| 35 | return sum == old_sum; |
Stefan Reinauer | 8e726b7 | 2010-03-29 23:01:35 +0000 | [diff] [blame] | 36 | #else |
| 37 | return 0; |
| 38 | #endif |
Eric Biederman | 05f26fc | 2003-06-11 21:55:00 +0000 | [diff] [blame] | 39 | } |
| 40 | |
Timothy Pearson | 3bfd7cc | 2015-11-01 02:13:17 -0600 | [diff] [blame] | 41 | static inline __attribute__((unused)) int boot_count(uint8_t rtc_byte) |
Eric Biederman | 9b4336c | 2003-07-19 04:28:22 +0000 | [diff] [blame] | 42 | { |
Timothy Pearson | 3bfd7cc | 2015-11-01 02:13:17 -0600 | [diff] [blame] | 43 | return rtc_byte >> 4; |
| 44 | } |
| 45 | |
| 46 | static inline __attribute__((unused)) uint8_t increment_boot_count(uint8_t rtc_byte) |
| 47 | { |
| 48 | return rtc_byte + (1 << 4); |
| 49 | } |
| 50 | |
| 51 | static inline __attribute__((unused)) uint8_t boot_set_fallback(uint8_t rtc_byte) |
| 52 | { |
| 53 | return rtc_byte & ~RTC_BOOT_NORMAL; |
| 54 | } |
| 55 | |
| 56 | static inline __attribute__((unused)) int boot_use_normal(uint8_t rtc_byte) |
| 57 | { |
| 58 | return rtc_byte & RTC_BOOT_NORMAL; |
Eric Biederman | 9b4336c | 2003-07-19 04:28:22 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Edward O'Callaghan | ec79d7a | 2014-06-26 18:14:04 +1000 | [diff] [blame] | 61 | static inline __attribute__((unused)) int do_normal_boot(void) |
Eric Biederman | 05f26fc | 2003-06-11 21:55:00 +0000 | [diff] [blame] | 62 | { |
| 63 | unsigned char byte; |
| 64 | |
| 65 | if (cmos_error() || !cmos_chksum_valid()) { |
Timothy Pearson | 3bfd7cc | 2015-11-01 02:13:17 -0600 | [diff] [blame] | 66 | /* Invalid CMOS checksum detected! |
| 67 | * Force fallback boot... |
Eric Biederman | 05f26fc | 2003-06-11 21:55:00 +0000 | [diff] [blame] | 68 | */ |
| 69 | byte = cmos_read(RTC_BOOT_BYTE); |
Timothy Pearson | 3bfd7cc | 2015-11-01 02:13:17 -0600 | [diff] [blame] | 70 | byte &= boot_set_fallback(byte) & 0x0f; |
| 71 | byte |= 0xf << 4; |
Eric Biederman | 05f26fc | 2003-06-11 21:55:00 +0000 | [diff] [blame] | 72 | cmos_write(byte, RTC_BOOT_BYTE); |
| 73 | } |
| 74 | |
| 75 | /* The RTC_BOOT_BYTE is now o.k. see where to go. */ |
| 76 | byte = cmos_read(RTC_BOOT_BYTE); |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 77 | |
Timothy Pearson | 3bfd7cc | 2015-11-01 02:13:17 -0600 | [diff] [blame] | 78 | /* Are we attempting to boot normally? */ |
| 79 | if (boot_use_normal(byte)) { |
| 80 | /* Are we already at the max count? */ |
| 81 | if (boot_count(byte) < CONFIG_MAX_REBOOT_CNT) |
| 82 | byte = increment_boot_count(byte); |
| 83 | else |
| 84 | byte = boot_set_fallback(byte); |
Eric Biederman | 05f26fc | 2003-06-11 21:55:00 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Eric Biederman | 05f26fc | 2003-06-11 21:55:00 +0000 | [diff] [blame] | 87 | /* Save the boot byte */ |
| 88 | cmos_write(byte, RTC_BOOT_BYTE); |
| 89 | |
Timothy Pearson | 3bfd7cc | 2015-11-01 02:13:17 -0600 | [diff] [blame] | 90 | /* Return selected code path for this boot attempt */ |
| 91 | return boot_use_normal(byte); |
Eric Biederman | 05f26fc | 2003-06-11 21:55:00 +0000 | [diff] [blame] | 92 | } |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 93 | |
Patrick Georgi | b251753 | 2011-05-10 21:53:13 +0000 | [diff] [blame] | 94 | unsigned read_option_lowlevel(unsigned start, unsigned size, unsigned def) |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 95 | { |
Edwin Beasant | eb50c7d | 2010-07-06 21:05:04 +0000 | [diff] [blame] | 96 | #if CONFIG_USE_OPTION_TABLE |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 97 | unsigned byte; |
| 98 | byte = cmos_read(start/8); |
| 99 | return (byte >> (start & 7U)) & ((1U << size) - 1U); |
| 100 | #else |
| 101 | return def; |
| 102 | #endif |
| 103 | } |