| 1 |
|
| 2 |
|
| 3 |
#include <commonlib/bsd/ipchksum.h> |
| 4 |
#include <console/console.h> |
| 5 |
#include <pc80/mc146818rtc.h> |
| 6 |
#include <stdint.h> |
| 7 |
#include <elog.h> |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
#if CONFIG(USE_OPTION_TABLE) |
| 16 |
# include "option_table.h" |
| 17 |
# define BOOT_COUNT_CMOS_OFFSET (CMOS_VSTART_boot_count_offset >> 3) |
| 18 |
#else |
| 19 |
# if (CONFIG_ELOG_BOOT_COUNT_CMOS_OFFSET != 0) |
| 20 |
# define BOOT_COUNT_CMOS_OFFSET CONFIG_ELOG_BOOT_COUNT_CMOS_OFFSET |
| 21 |
# else |
| 22 |
# error "Must configure CONFIG_ELOG_BOOT_COUNT_CMOS_OFFSET" |
| 23 |
# endif |
| 24 |
#endif |
| 25 |
|
| 26 |
#define BOOT_COUNT_SIGNATURE 0x4342 |
| 27 |
|
| 28 |
struct boot_count { |
| 29 |
u16 signature; |
| 30 |
u32 count; |
| 31 |
u16 checksum; |
| 32 |
} __packed; |
| 33 |
|
| 34 |
|
| 35 |
static int boot_count_cmos_read(struct boot_count *bc) |
| 36 |
{ |
| 37 |
u8 i, *p; |
| 38 |
u16 csum; |
| 39 |
|
| 40 |
for (p = (u8 *)bc, i = 0; i < sizeof(*bc); i++, p++) |
| 41 |
*p = cmos_read(BOOT_COUNT_CMOS_OFFSET + i); |
| 42 |
|
| 43 |
|
| 44 |
if (bc->signature != BOOT_COUNT_SIGNATURE) { |
| 45 |
printk(BIOS_DEBUG, "Boot Count invalid signature\n"); |
| 46 |
return -1; |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
csum = ipchksum(bc, offsetof(struct boot_count, checksum)); |
| 51 |
|
| 52 |
if (csum != bc->checksum) { |
| 53 |
printk(BIOS_DEBUG, "Boot Count checksum mismatch\n"); |
| 54 |
return -1; |
| 55 |
} |
| 56 |
|
| 57 |
return 0; |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
static void boot_count_cmos_write(struct boot_count *bc) |
| 62 |
{ |
| 63 |
u8 i, *p; |
| 64 |
|
| 65 |
|
| 66 |
bc->checksum = ipchksum( |
| 67 |
bc, offsetof(struct boot_count, checksum)); |
| 68 |
|
| 69 |
for (p = (u8 *)bc, i = 0; i < sizeof(*bc); i++, p++) |
| 70 |
cmos_write(*p, BOOT_COUNT_CMOS_OFFSET + i); |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
u32 boot_count_increment(void) |
| 75 |
{ |
| 76 |
struct boot_count bc; |
| 77 |
|
| 78 |
|
| 79 |
if (boot_count_cmos_read(&bc) < 0) { |
| 80 |
|
| 81 |
bc.signature = BOOT_COUNT_SIGNATURE; |
| 82 |
bc.count = 0; |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
bc.count++; |
| 87 |
|
| 88 |
|
| 89 |
boot_count_cmos_write(&bc); |
| 90 |
|
| 91 |
printk(BIOS_DEBUG, "Boot Count incremented to %u\n", bc.count); |
| 92 |
return bc.count; |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
u32 boot_count_read(void) |
| 97 |
{ |
| 98 |
struct boot_count bc; |
| 99 |
|
| 100 |
if (boot_count_cmos_read(&bc) < 0) |
| 101 |
return 0; |
| 102 |
|
| 103 |
return bc.count; |
| 104 |
} |