blob: 97d7098eebd3a18dff4e8b31d5db8544a3d9210c [file] [log] [blame]
Duncan Laurief4d36232012-06-23 16:37:45 -07001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2012 The ChromiumOS Authors. All rights reserved.
5 *
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.
9 *
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.
Duncan Laurief4d36232012-06-23 16:37:45 -070014 */
15
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>
22
23/*
24 * We need a region in CMOS to store the boot counter.
25 *
26 * This can either be declared as part of the option
27 * table or statically defined in the board config.
28 */
Julius Wernercd49cce2019-03-05 16:53:33 -080029#if CONFIG(USE_OPTION_TABLE)
Duncan Laurief4d36232012-06-23 16:37:45 -070030# include "option_table.h"
31# define BOOT_COUNT_CMOS_OFFSET (CMOS_VSTART_boot_count_offset >> 3)
32#else
Elyes HAOUASb58e99d2019-01-23 12:04:43 +010033# if (CONFIG_ELOG_BOOT_COUNT_CMOS_OFFSET != 0)
Duncan Laurief4d36232012-06-23 16:37:45 -070034# define BOOT_COUNT_CMOS_OFFSET CONFIG_ELOG_BOOT_COUNT_CMOS_OFFSET
35# else
Martin Roth46cf9f72015-07-11 13:56:58 -060036# error "Must configure CONFIG_ELOG_BOOT_COUNT_CMOS_OFFSET"
Duncan Laurief4d36232012-06-23 16:37:45 -070037# endif
38#endif
39
40#define BOOT_COUNT_SIGNATURE 0x4342 /* 'BC' */
41
42struct boot_count {
43 u16 signature;
44 u32 count;
45 u16 checksum;
Stefan Reinauer6a001132017-07-13 02:20:27 +020046} __packed;
Duncan Laurief4d36232012-06-23 16:37:45 -070047
48/* Read and validate boot count structure from CMOS */
49static int boot_count_cmos_read(struct boot_count *bc)
50{
51 u8 i, *p;
52 u16 csum;
53
54 for (p = (u8*)bc, i = 0; i < sizeof(*bc); i++, p++)
55 *p = cmos_read(BOOT_COUNT_CMOS_OFFSET + i);
56
57 /* Verify signature */
58 if (bc->signature != BOOT_COUNT_SIGNATURE) {
59 printk(BIOS_DEBUG, "Boot Count invalid signature\n");
60 return -1;
61 }
62
63 /* Verify checksum over signature and counter only */
64 csum = compute_ip_checksum(bc, offsetof(struct boot_count, checksum));
65
66 if (csum != bc->checksum) {
67 printk(BIOS_DEBUG, "Boot Count checksum mismatch\n");
68 return -1;
69 }
70
71 return 0;
72}
73
74/* Write boot count structure to CMOS */
75static void boot_count_cmos_write(struct boot_count *bc)
76{
77 u8 i, *p;
78
79 /* Checksum over signature and counter only */
80 bc->checksum = compute_ip_checksum(
81 bc, offsetof(struct boot_count, checksum));
82
83 for (p = (u8*)bc, i = 0; i < sizeof(*bc); i++, p++)
84 cmos_write(*p, BOOT_COUNT_CMOS_OFFSET + i);
85}
86
87/* Increment boot count and return the new value */
88u32 boot_count_increment(void)
89{
90 struct boot_count bc;
91
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;
97 }
98
99 /* Increment boot counter */
100 bc.count++;
101
102 /* Write the new count to CMOS */
103 boot_count_cmos_write(&bc);
104
105 printk(BIOS_DEBUG, "Boot Count incremented to %u\n", bc.count);
106 return bc.count;
107}
108
109/* Return the current boot count */
110u32 boot_count_read(void)
111{
112 struct boot_count bc;
113
114 if (boot_count_cmos_read(&bc) < 0)
115 return 0;
116
117 return bc.count;
118}