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. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | */ |
| 19 | |
| 20 | #include <types.h> |
| 21 | #include <string.h> |
| 22 | #include <console/console.h> |
| 23 | #include <pc80/mc146818rtc.h> |
| 24 | #include <cpu/x86/car.h> |
| 25 | #include "chromeos.h" |
| 26 | |
| 27 | #define VBNV_BLOCK_SIZE 16 /* Size of NV storage block in bytes */ |
| 28 | |
| 29 | /* Constants for NV storage. We use this rather than structs and |
| 30 | * bitfields so the data format is consistent across platforms and |
| 31 | * compilers. |
| 32 | */ |
| 33 | #define HEADER_OFFSET 0 |
| 34 | #define HEADER_MASK 0xC0 |
| 35 | #define HEADER_SIGNATURE 0x40 |
| 36 | #define HEADER_FIRMWARE_SETTINGS_RESET 0x20 |
| 37 | #define HEADER_KERNEL_SETTINGS_RESET 0x10 |
| 38 | |
| 39 | #define BOOT_OFFSET 1 |
| 40 | #define BOOT_DEBUG_RESET_MODE 0x80 |
Bill Richardson | 0a405ba | 2012-06-26 16:33:45 -0700 | [diff] [blame] | 41 | #define BOOT_DISABLE_DEV_REQUEST 0x40 |
| 42 | #define BOOT_OPROM_NEEDED 0x20 |
Stefan Reinauer | 9aea04a | 2012-03-30 12:01:06 -0700 | [diff] [blame] | 43 | #define BOOT_TRY_B_COUNT_MASK 0x0F |
| 44 | |
| 45 | #define RECOVERY_OFFSET 2 |
| 46 | #define LOCALIZATION_OFFSET 3 |
| 47 | |
| 48 | #define DEV_FLAGS_OFFSET 4 |
| 49 | #define DEV_BOOT_USB_MASK 0x01 |
Bill Richardson | 0a405ba | 2012-06-26 16:33:45 -0700 | [diff] [blame] | 50 | #define DEV_BOOT_SIGNED_ONLY_MASK 0x02 |
Stefan Reinauer | 9aea04a | 2012-03-30 12:01:06 -0700 | [diff] [blame] | 51 | |
| 52 | #define KERNEL_FIELD_OFFSET 11 |
| 53 | #define CRC_OFFSET 15 |
| 54 | |
| 55 | static int vbnv_initialized CAR_GLOBAL; |
Aaron Durbin | 677e155 | 2013-05-10 00:42:14 -0500 | [diff] [blame] | 56 | static uint8_t vbnv[CONFIG_VBNV_SIZE] CAR_GLOBAL; |
| 57 | |
| 58 | /* Wrappers for accessing the variables marked as CAR_GLOBAL. */ |
| 59 | static inline int is_vbnv_initialized(void) |
| 60 | { |
| 61 | return car_get_var(vbnv_initialized); |
| 62 | } |
| 63 | |
| 64 | static inline uint8_t *vbnv_data_addr(int index) |
| 65 | { |
| 66 | uint8_t *vbnv_arr = car_get_var_ptr(vbnv); |
| 67 | |
| 68 | return &vbnv_arr[index]; |
| 69 | } |
| 70 | |
| 71 | static inline uint8_t vbnv_data(int index) |
| 72 | { |
| 73 | return *vbnv_data_addr(index); |
| 74 | } |
Stefan Reinauer | 9aea04a | 2012-03-30 12:01:06 -0700 | [diff] [blame] | 75 | |
| 76 | /* Return CRC-8 of the data, using x^8 + x^2 + x + 1 polynomial. A |
| 77 | * table-based algorithm would be faster, but for only 15 bytes isn't |
| 78 | * worth the code size. |
| 79 | */ |
| 80 | |
| 81 | static uint8_t crc8(const uint8_t * data, int len) |
| 82 | { |
| 83 | unsigned crc = 0; |
| 84 | int i, j; |
| 85 | |
| 86 | for (j = len; j; j--, data++) { |
| 87 | crc ^= (*data << 8); |
| 88 | for (i = 8; i; i--) { |
| 89 | if (crc & 0x8000) |
| 90 | crc ^= (0x1070 << 3); |
| 91 | crc <<= 1; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | return (uint8_t) (crc >> 8); |
| 96 | } |
| 97 | |
Aaron Durbin | fd79562 | 2013-03-01 17:12:26 -0600 | [diff] [blame] | 98 | void read_vbnv(uint8_t *vbnv_copy) |
Stefan Reinauer | 9aea04a | 2012-03-30 12:01:06 -0700 | [diff] [blame] | 99 | { |
| 100 | int i; |
| 101 | |
| 102 | for (i = 0; i < CONFIG_VBNV_SIZE; i++) |
Aaron Durbin | fd79562 | 2013-03-01 17:12:26 -0600 | [diff] [blame] | 103 | vbnv_copy[i] = cmos_read(CONFIG_VBNV_OFFSET + 14 + i); |
Stefan Reinauer | 9aea04a | 2012-03-30 12:01:06 -0700 | [diff] [blame] | 104 | |
| 105 | /* Check data for consistency */ |
Aaron Durbin | fd79562 | 2013-03-01 17:12:26 -0600 | [diff] [blame] | 106 | if ((HEADER_SIGNATURE != (vbnv_copy[HEADER_OFFSET] & HEADER_MASK)) |
| 107 | || (crc8(vbnv_copy, CRC_OFFSET) != vbnv_copy[CRC_OFFSET])) { |
Stefan Reinauer | 9aea04a | 2012-03-30 12:01:06 -0700 | [diff] [blame] | 108 | |
| 109 | /* Data is inconsistent (bad CRC or header), |
| 110 | * so reset to defaults |
| 111 | */ |
Aaron Durbin | fd79562 | 2013-03-01 17:12:26 -0600 | [diff] [blame] | 112 | memset(vbnv_copy, 0, VBNV_BLOCK_SIZE); |
| 113 | vbnv_copy[HEADER_OFFSET] = |
Stefan Reinauer | 9aea04a | 2012-03-30 12:01:06 -0700 | [diff] [blame] | 114 | (HEADER_SIGNATURE | HEADER_FIRMWARE_SETTINGS_RESET | |
| 115 | HEADER_KERNEL_SETTINGS_RESET); |
| 116 | } |
Aaron Durbin | fd79562 | 2013-03-01 17:12:26 -0600 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | void save_vbnv(const uint8_t *vbnv_copy) |
| 120 | { |
| 121 | int i; |
| 122 | |
| 123 | for (i = 0; i < CONFIG_VBNV_SIZE; i++) |
| 124 | cmos_write(vbnv_copy[i], CONFIG_VBNV_OFFSET + 14 + i); |
| 125 | } |
| 126 | |
| 127 | |
| 128 | static void vbnv_setup(void) |
| 129 | { |
Aaron Durbin | 677e155 | 2013-05-10 00:42:14 -0500 | [diff] [blame] | 130 | read_vbnv(vbnv_data_addr(0)); |
| 131 | car_set_var(vbnv_initialized, 1); |
Stefan Reinauer | 9aea04a | 2012-03-30 12:01:06 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | int get_recovery_mode_from_vbnv(void) |
| 135 | { |
Aaron Durbin | 677e155 | 2013-05-10 00:42:14 -0500 | [diff] [blame] | 136 | if (!is_vbnv_initialized()) |
Stefan Reinauer | 9aea04a | 2012-03-30 12:01:06 -0700 | [diff] [blame] | 137 | vbnv_setup(); |
Aaron Durbin | 677e155 | 2013-05-10 00:42:14 -0500 | [diff] [blame] | 138 | return vbnv_data(RECOVERY_OFFSET); |
Stefan Reinauer | 9aea04a | 2012-03-30 12:01:06 -0700 | [diff] [blame] | 139 | } |
Bill Richardson | 0a405ba | 2012-06-26 16:33:45 -0700 | [diff] [blame] | 140 | |
| 141 | int vboot_wants_oprom(void) |
| 142 | { |
Aaron Durbin | 677e155 | 2013-05-10 00:42:14 -0500 | [diff] [blame] | 143 | if (!is_vbnv_initialized()) |
Bill Richardson | 0a405ba | 2012-06-26 16:33:45 -0700 | [diff] [blame] | 144 | vbnv_setup(); |
| 145 | |
| 146 | /* FIXME(crosbug.com/p/8789). The following commented-out line does the |
| 147 | * right thing, assuming that vboot has requested the option ROM and |
| 148 | * rebooted if it finds that it's needed but not loaded. At the moment, |
| 149 | * it doesn't yet do that, so we must always say we want it. */ |
| 150 | |
Aaron Durbin | 677e155 | 2013-05-10 00:42:14 -0500 | [diff] [blame] | 151 | /* return (vbnv_data(BOOT_OFFSET) & BOOT_OPROM_NEEDED) ? 1 : 0; */ |
Bill Richardson | 0a405ba | 2012-06-26 16:33:45 -0700 | [diff] [blame] | 152 | return 1; |
| 153 | } |