tree: drop last paragraph of GPL copyright header
[coreboot.git] / src / drivers / elog / boot_count.c
blob9d717d8888491e3a925816750317fbd704d93055
1 /*
2 * This file is part of the coreboot project.
4 * Copyright (C) 2012 The ChromiumOS Authors. All rights reserved.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
16 #include <console/console.h>
17 #include <ip_checksum.h>
18 #include <pc80/mc146818rtc.h>
19 #include <stddef.h>
20 #include <stdint.h>
21 #include <elog.h>
24 * We need a region in CMOS to store the boot counter.
26 * This can either be declared as part of the option
27 * table or statically defined in the board config.
29 #if CONFIG_USE_OPTION_TABLE
30 # include "option_table.h"
31 # define BOOT_COUNT_CMOS_OFFSET (CMOS_VSTART_boot_count_offset >> 3)
32 #else
33 # if defined(CONFIG_ELOG_BOOT_COUNT_CMOS_OFFSET) && CONFIG_ELOG_BOOT_COUNT_CMOS_OFFSET
34 # define BOOT_COUNT_CMOS_OFFSET CONFIG_ELOG_BOOT_COUNT_CMOS_OFFSET
35 # else
36 # error "Must configure CONFIG_ELOG_BOOT_COUNT_CMOS_OFFSET"
37 # endif
38 #endif
40 #define BOOT_COUNT_SIGNATURE 0x4342 /* 'BC' */
42 struct boot_count {
43 u16 signature;
44 u32 count;
45 u16 checksum;
46 } __attribute__ ((packed));
48 /* Read and validate boot count structure from CMOS */
49 static int boot_count_cmos_read(struct boot_count *bc)
51 u8 i, *p;
52 u16 csum;
54 for (p = (u8*)bc, i = 0; i < sizeof(*bc); i++, p++)
55 *p = cmos_read(BOOT_COUNT_CMOS_OFFSET + i);
57 /* Verify signature */
58 if (bc->signature != BOOT_COUNT_SIGNATURE) {
59 printk(BIOS_DEBUG, "Boot Count invalid signature\n");
60 return -1;
63 /* Verify checksum over signature and counter only */
64 csum = compute_ip_checksum(bc, offsetof(struct boot_count, checksum));
66 if (csum != bc->checksum) {
67 printk(BIOS_DEBUG, "Boot Count checksum mismatch\n");
68 return -1;
71 return 0;
74 /* Write boot count structure to CMOS */
75 static void boot_count_cmos_write(struct boot_count *bc)
77 u8 i, *p;
79 /* Checksum over signature and counter only */
80 bc->checksum = compute_ip_checksum(
81 bc, offsetof(struct boot_count, checksum));
83 for (p = (u8*)bc, i = 0; i < sizeof(*bc); i++, p++)
84 cmos_write(*p, BOOT_COUNT_CMOS_OFFSET + i);
87 /* Increment boot count and return the new value */
88 u32 boot_count_increment(void)
90 struct boot_count bc;
92 /* Read and increment boot count */
93 if (boot_count_cmos_read(&bc) < 0) {
94 /* Structure invalid, re-initialize */
95 bc.signature = BOOT_COUNT_SIGNATURE;
96 bc.count = 0;
99 /* Increment boot counter */
100 bc.count++;
102 /* Write the new count to CMOS */
103 boot_count_cmos_write(&bc);
105 printk(BIOS_DEBUG, "Boot Count incremented to %u\n", bc.count);
106 return bc.count;
109 /* Return the current boot count */
110 u32 boot_count_read(void)
112 struct boot_count bc;
114 if (boot_count_cmos_read(&bc) < 0)
115 return 0;
117 return bc.count;