Stefan Reinauer | 9aea04a | 2012-03-30 12:01:06 -0700 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright (C) 2011 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. |
Stefan Reinauer | 9aea04a | 2012-03-30 12:01:06 -0700 | [diff] [blame] | 14 | */ |
| 15 | |
Duncan Laurie | fe97013 | 2016-01-26 16:30:36 -0800 | [diff] [blame] | 16 | #include <bootstate.h> |
| 17 | #include <console/console.h> |
Stefan Reinauer | 9aea04a | 2012-03-30 12:01:06 -0700 | [diff] [blame] | 18 | #include <types.h> |
Stefan Reinauer | 9aea04a | 2012-03-30 12:01:06 -0700 | [diff] [blame] | 19 | #include <pc80/mc146818rtc.h> |
Furquan Shaikh | 2a12e2e | 2016-07-25 11:48:03 -0700 | [diff] [blame] | 20 | #include <vboot/vbnv.h> |
| 21 | #include <vboot/vbnv_layout.h> |
Stefan Reinauer | 9aea04a | 2012-03-30 12:01:06 -0700 | [diff] [blame] | 22 | |
Hung-Te Lin | fe2fc83 | 2016-12-29 20:59:37 +0800 | [diff] [blame] | 23 | static void clear_vbnv_battery_cutoff_flag(uint8_t *vbnv_copy) |
| 24 | { |
| 25 | /* |
| 26 | * Currently battery cutoff is done in payload stage, which does not |
| 27 | * update backup VBNV. And doing battery cutoff will invalidate CMOS. |
| 28 | * This means for every reboot after cutoff, read_vbnv_cmos will reload |
| 29 | * backup VBNV and try to cutoff again, causing endless reboot loop. |
| 30 | * So we should always clear battery cutoff flag from loaded backup. |
| 31 | */ |
| 32 | if (vbnv_copy[MISC_FLAGS_OFFSET] & MISC_FLAGS_BATTERY_CUTOFF_MASK) { |
| 33 | printk(BIOS_INFO, "VBNV: Remove battery cut-off request.\n"); |
| 34 | vbnv_copy[MISC_FLAGS_OFFSET] &= ~MISC_FLAGS_BATTERY_CUTOFF_MASK; |
| 35 | regen_vbnv_crc(vbnv_copy); |
| 36 | } |
| 37 | } |
| 38 | |
Duncan Laurie | 88b28ad | 2016-01-25 17:13:27 -0800 | [diff] [blame] | 39 | void read_vbnv_cmos(uint8_t *vbnv_copy) |
Stefan Reinauer | 9aea04a | 2012-03-30 12:01:06 -0700 | [diff] [blame] | 40 | { |
| 41 | int i; |
| 42 | |
Furquan Shaikh | 2a12e2e | 2016-07-25 11:48:03 -0700 | [diff] [blame] | 43 | for (i = 0; i < VBOOT_VBNV_BLOCK_SIZE; i++) |
| 44 | vbnv_copy[i] = cmos_read(CONFIG_VBOOT_VBNV_OFFSET + 14 + i); |
Duncan Laurie | fe97013 | 2016-01-26 16:30:36 -0800 | [diff] [blame] | 45 | |
Furquan Shaikh | 2a12e2e | 2016-07-25 11:48:03 -0700 | [diff] [blame] | 46 | if (IS_ENABLED(CONFIG_VBOOT_VBNV_CMOS_BACKUP_TO_FLASH)) { |
Duncan Laurie | fe97013 | 2016-01-26 16:30:36 -0800 | [diff] [blame] | 47 | if (verify_vbnv(vbnv_copy)) |
| 48 | return; |
| 49 | |
| 50 | printk(BIOS_INFO, "VBNV: CMOS invalid, restoring from flash\n"); |
| 51 | read_vbnv_flash(vbnv_copy); |
| 52 | |
| 53 | if (verify_vbnv(vbnv_copy)) { |
Hung-Te Lin | fe2fc83 | 2016-12-29 20:59:37 +0800 | [diff] [blame] | 54 | clear_vbnv_battery_cutoff_flag(vbnv_copy); |
Duncan Laurie | fe97013 | 2016-01-26 16:30:36 -0800 | [diff] [blame] | 55 | save_vbnv_cmos(vbnv_copy); |
| 56 | printk(BIOS_INFO, "VBNV: Flash backup restored\n"); |
| 57 | } else { |
| 58 | printk(BIOS_INFO, "VBNV: Restore from flash failed\n"); |
| 59 | } |
| 60 | } |
Aaron Durbin | fd79562 | 2013-03-01 17:12:26 -0600 | [diff] [blame] | 61 | } |
| 62 | |
Duncan Laurie | 88b28ad | 2016-01-25 17:13:27 -0800 | [diff] [blame] | 63 | void save_vbnv_cmos(const uint8_t *vbnv_copy) |
Aaron Durbin | fd79562 | 2013-03-01 17:12:26 -0600 | [diff] [blame] | 64 | { |
| 65 | int i; |
| 66 | |
Furquan Shaikh | 2a12e2e | 2016-07-25 11:48:03 -0700 | [diff] [blame] | 67 | for (i = 0; i < VBOOT_VBNV_BLOCK_SIZE; i++) |
| 68 | cmos_write(vbnv_copy[i], CONFIG_VBOOT_VBNV_OFFSET + 14 + i); |
Aaron Durbin | fd79562 | 2013-03-01 17:12:26 -0600 | [diff] [blame] | 69 | } |
Duncan Laurie | fe97013 | 2016-01-26 16:30:36 -0800 | [diff] [blame] | 70 | |
Jagadish Krishnamoorthy | e2e561d | 2016-05-18 15:18:58 -0700 | [diff] [blame] | 71 | void init_vbnv_cmos(int rtc_fail) |
| 72 | { |
Furquan Shaikh | 2a12e2e | 2016-07-25 11:48:03 -0700 | [diff] [blame] | 73 | uint8_t vbnv[VBOOT_VBNV_BLOCK_SIZE]; |
Jagadish Krishnamoorthy | e2e561d | 2016-05-18 15:18:58 -0700 | [diff] [blame] | 74 | |
| 75 | if (rtc_fail) |
| 76 | read_vbnv_cmos(vbnv); |
| 77 | |
| 78 | cmos_init(rtc_fail); |
| 79 | |
| 80 | if (rtc_fail) |
| 81 | save_vbnv_cmos(vbnv); |
| 82 | } |
| 83 | |
Furquan Shaikh | 2a12e2e | 2016-07-25 11:48:03 -0700 | [diff] [blame] | 84 | #if IS_ENABLED(CONFIG_VBOOT_VBNV_CMOS_BACKUP_TO_FLASH) |
Patrick Georgi | 6b881b2 | 2016-02-09 14:01:08 +0100 | [diff] [blame] | 85 | static void back_up_vbnv_cmos(void *unused) |
Duncan Laurie | fe97013 | 2016-01-26 16:30:36 -0800 | [diff] [blame] | 86 | { |
Furquan Shaikh | 2a12e2e | 2016-07-25 11:48:03 -0700 | [diff] [blame] | 87 | uint8_t vbnv_cmos[VBOOT_VBNV_BLOCK_SIZE]; |
Duncan Laurie | fe97013 | 2016-01-26 16:30:36 -0800 | [diff] [blame] | 88 | |
| 89 | /* Read current VBNV from CMOS. */ |
| 90 | read_vbnv_cmos(vbnv_cmos); |
| 91 | |
| 92 | /* Save to flash, will only be saved if different. */ |
| 93 | save_vbnv_flash(vbnv_cmos); |
| 94 | } |
Patrick Georgi | 6b881b2 | 2016-02-09 14:01:08 +0100 | [diff] [blame] | 95 | BOOT_STATE_INIT_ENTRY(BS_POST_DEVICE, BS_ON_EXIT, back_up_vbnv_cmos, NULL); |
Duncan Laurie | fe97013 | 2016-01-26 16:30:36 -0800 | [diff] [blame] | 96 | #endif |