Duncan Laurie | f4d3623 | 2012-06-23 16:37:45 -0700 | [diff] [blame] | 1 | /* |
| 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 Laurie | f4d3623 | 2012-06-23 16:37:45 -0700 | [diff] [blame] | 14 | */ |
| 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 Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame^] | 29 | #if CONFIG(USE_OPTION_TABLE) |
Duncan Laurie | f4d3623 | 2012-06-23 16:37:45 -0700 | [diff] [blame] | 30 | # include "option_table.h" |
| 31 | # define BOOT_COUNT_CMOS_OFFSET (CMOS_VSTART_boot_count_offset >> 3) |
| 32 | #else |
Elyes HAOUAS | b58e99d | 2019-01-23 12:04:43 +0100 | [diff] [blame] | 33 | # if (CONFIG_ELOG_BOOT_COUNT_CMOS_OFFSET != 0) |
Duncan Laurie | f4d3623 | 2012-06-23 16:37:45 -0700 | [diff] [blame] | 34 | # define BOOT_COUNT_CMOS_OFFSET CONFIG_ELOG_BOOT_COUNT_CMOS_OFFSET |
| 35 | # else |
Martin Roth | 46cf9f7 | 2015-07-11 13:56:58 -0600 | [diff] [blame] | 36 | # error "Must configure CONFIG_ELOG_BOOT_COUNT_CMOS_OFFSET" |
Duncan Laurie | f4d3623 | 2012-06-23 16:37:45 -0700 | [diff] [blame] | 37 | # endif |
| 38 | #endif |
| 39 | |
| 40 | #define BOOT_COUNT_SIGNATURE 0x4342 /* 'BC' */ |
| 41 | |
| 42 | struct boot_count { |
| 43 | u16 signature; |
| 44 | u32 count; |
| 45 | u16 checksum; |
Stefan Reinauer | 6a00113 | 2017-07-13 02:20:27 +0200 | [diff] [blame] | 46 | } __packed; |
Duncan Laurie | f4d3623 | 2012-06-23 16:37:45 -0700 | [diff] [blame] | 47 | |
| 48 | /* Read and validate boot count structure from CMOS */ |
| 49 | static 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 */ |
| 75 | static 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 */ |
| 88 | u32 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 */ |
| 110 | u32 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 | } |