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> |
Stefan Reinauer | 6a00113 | 2017-07-13 02:20:27 +0200 | [diff] [blame^] | 21 | #include <compiler.h> |
Duncan Laurie | f4d3623 | 2012-06-23 16:37:45 -0700 | [diff] [blame] | 22 | #include <elog.h> |
| 23 | |
| 24 | /* |
| 25 | * We need a region in CMOS to store the boot counter. |
| 26 | * |
| 27 | * This can either be declared as part of the option |
| 28 | * table or statically defined in the board config. |
| 29 | */ |
| 30 | #if CONFIG_USE_OPTION_TABLE |
| 31 | # include "option_table.h" |
| 32 | # define BOOT_COUNT_CMOS_OFFSET (CMOS_VSTART_boot_count_offset >> 3) |
| 33 | #else |
Martin Roth | 46cf9f7 | 2015-07-11 13:56:58 -0600 | [diff] [blame] | 34 | # if defined(CONFIG_ELOG_BOOT_COUNT_CMOS_OFFSET) && CONFIG_ELOG_BOOT_COUNT_CMOS_OFFSET |
Duncan Laurie | f4d3623 | 2012-06-23 16:37:45 -0700 | [diff] [blame] | 35 | # define BOOT_COUNT_CMOS_OFFSET CONFIG_ELOG_BOOT_COUNT_CMOS_OFFSET |
| 36 | # else |
Martin Roth | 46cf9f7 | 2015-07-11 13:56:58 -0600 | [diff] [blame] | 37 | # error "Must configure CONFIG_ELOG_BOOT_COUNT_CMOS_OFFSET" |
Duncan Laurie | f4d3623 | 2012-06-23 16:37:45 -0700 | [diff] [blame] | 38 | # endif |
| 39 | #endif |
| 40 | |
| 41 | #define BOOT_COUNT_SIGNATURE 0x4342 /* 'BC' */ |
| 42 | |
| 43 | struct boot_count { |
| 44 | u16 signature; |
| 45 | u32 count; |
| 46 | u16 checksum; |
Stefan Reinauer | 6a00113 | 2017-07-13 02:20:27 +0200 | [diff] [blame^] | 47 | } __packed; |
Duncan Laurie | f4d3623 | 2012-06-23 16:37:45 -0700 | [diff] [blame] | 48 | |
| 49 | /* Read and validate boot count structure from CMOS */ |
| 50 | static int boot_count_cmos_read(struct boot_count *bc) |
| 51 | { |
| 52 | u8 i, *p; |
| 53 | u16 csum; |
| 54 | |
| 55 | for (p = (u8*)bc, i = 0; i < sizeof(*bc); i++, p++) |
| 56 | *p = cmos_read(BOOT_COUNT_CMOS_OFFSET + i); |
| 57 | |
| 58 | /* Verify signature */ |
| 59 | if (bc->signature != BOOT_COUNT_SIGNATURE) { |
| 60 | printk(BIOS_DEBUG, "Boot Count invalid signature\n"); |
| 61 | return -1; |
| 62 | } |
| 63 | |
| 64 | /* Verify checksum over signature and counter only */ |
| 65 | csum = compute_ip_checksum(bc, offsetof(struct boot_count, checksum)); |
| 66 | |
| 67 | if (csum != bc->checksum) { |
| 68 | printk(BIOS_DEBUG, "Boot Count checksum mismatch\n"); |
| 69 | return -1; |
| 70 | } |
| 71 | |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | /* Write boot count structure to CMOS */ |
| 76 | static void boot_count_cmos_write(struct boot_count *bc) |
| 77 | { |
| 78 | u8 i, *p; |
| 79 | |
| 80 | /* Checksum over signature and counter only */ |
| 81 | bc->checksum = compute_ip_checksum( |
| 82 | bc, offsetof(struct boot_count, checksum)); |
| 83 | |
| 84 | for (p = (u8*)bc, i = 0; i < sizeof(*bc); i++, p++) |
| 85 | cmos_write(*p, BOOT_COUNT_CMOS_OFFSET + i); |
| 86 | } |
| 87 | |
| 88 | /* Increment boot count and return the new value */ |
| 89 | u32 boot_count_increment(void) |
| 90 | { |
| 91 | struct boot_count bc; |
| 92 | |
| 93 | /* Read and increment boot count */ |
| 94 | if (boot_count_cmos_read(&bc) < 0) { |
| 95 | /* Structure invalid, re-initialize */ |
| 96 | bc.signature = BOOT_COUNT_SIGNATURE; |
| 97 | bc.count = 0; |
| 98 | } |
| 99 | |
| 100 | /* Increment boot counter */ |
| 101 | bc.count++; |
| 102 | |
| 103 | /* Write the new count to CMOS */ |
| 104 | boot_count_cmos_write(&bc); |
| 105 | |
| 106 | printk(BIOS_DEBUG, "Boot Count incremented to %u\n", bc.count); |
| 107 | return bc.count; |
| 108 | } |
| 109 | |
| 110 | /* Return the current boot count */ |
| 111 | u32 boot_count_read(void) |
| 112 | { |
| 113 | struct boot_count bc; |
| 114 | |
| 115 | if (boot_count_cmos_read(&bc) < 0) |
| 116 | return 0; |
| 117 | |
| 118 | return bc.count; |
| 119 | } |