1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #include <commonlib/bsd/ipchksum.h>
4 #include <console/console.h>
5 #include <pc80/mc146818rtc.h>
10 * We need a region in CMOS to store the boot counter.
12 * This can either be declared as part of the option
13 * table or statically defined in the board config.
15 #if CONFIG(USE_OPTION_TABLE)
16 # include "option_table.h"
17 # define BOOT_COUNT_CMOS_OFFSET (CMOS_VSTART_boot_count_offset >> 3)
19 # if (CONFIG_ELOG_BOOT_COUNT_CMOS_OFFSET != 0)
20 # define BOOT_COUNT_CMOS_OFFSET CONFIG_ELOG_BOOT_COUNT_CMOS_OFFSET
22 # error "Must configure CONFIG_ELOG_BOOT_COUNT_CMOS_OFFSET"
26 #define BOOT_COUNT_SIGNATURE 0x4342 /* 'BC' */
34 /* Read and validate boot count structure from CMOS */
35 static int boot_count_cmos_read(struct boot_count
*bc
)
40 for (p
= (u8
*)bc
, i
= 0; i
< sizeof(*bc
); i
++, p
++)
41 *p
= cmos_read(BOOT_COUNT_CMOS_OFFSET
+ i
);
43 /* Verify signature */
44 if (bc
->signature
!= BOOT_COUNT_SIGNATURE
) {
45 printk(BIOS_DEBUG
, "Boot Count invalid signature\n");
49 /* Verify checksum over signature and counter only */
50 csum
= ipchksum(bc
, offsetof(struct boot_count
, checksum
));
52 if (csum
!= bc
->checksum
) {
53 printk(BIOS_DEBUG
, "Boot Count checksum mismatch\n");
60 /* Write boot count structure to CMOS */
61 static void boot_count_cmos_write(struct boot_count
*bc
)
65 /* Checksum over signature and counter only */
66 bc
->checksum
= ipchksum(
67 bc
, offsetof(struct boot_count
, checksum
));
69 for (p
= (u8
*)bc
, i
= 0; i
< sizeof(*bc
); i
++, p
++)
70 cmos_write(*p
, BOOT_COUNT_CMOS_OFFSET
+ i
);
73 /* Increment boot count and return the new value */
74 u32
boot_count_increment(void)
78 /* Read and increment boot count */
79 if (boot_count_cmos_read(&bc
) < 0) {
80 /* Structure invalid, re-initialize */
81 bc
.signature
= BOOT_COUNT_SIGNATURE
;
85 /* Increment boot counter */
88 /* Write the new count to CMOS */
89 boot_count_cmos_write(&bc
);
91 printk(BIOS_DEBUG
, "Boot Count incremented to %u\n", bc
.count
);
95 /* Return the current boot count */
96 u32
boot_count_read(void)
100 if (boot_count_cmos_read(&bc
) < 0)