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; |
| 56 | uint8_t vbnv[CONFIG_VBNV_SIZE] CAR_GLOBAL; |
| 57 | |
| 58 | /* Return CRC-8 of the data, using x^8 + x^2 + x + 1 polynomial. A |
| 59 | * table-based algorithm would be faster, but for only 15 bytes isn't |
| 60 | * worth the code size. |
| 61 | */ |
| 62 | |
| 63 | static uint8_t crc8(const uint8_t * data, int len) |
| 64 | { |
| 65 | unsigned crc = 0; |
| 66 | int i, j; |
| 67 | |
| 68 | for (j = len; j; j--, data++) { |
| 69 | crc ^= (*data << 8); |
| 70 | for (i = 8; i; i--) { |
| 71 | if (crc & 0x8000) |
| 72 | crc ^= (0x1070 << 3); |
| 73 | crc <<= 1; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | return (uint8_t) (crc >> 8); |
| 78 | } |
| 79 | |
| 80 | static void vbnv_setup(void) |
| 81 | { |
| 82 | int i; |
| 83 | |
| 84 | for (i = 0; i < CONFIG_VBNV_SIZE; i++) |
| 85 | vbnv[i] = cmos_read(CONFIG_VBNV_OFFSET + 14 + i); |
| 86 | |
| 87 | /* Check data for consistency */ |
| 88 | if ((HEADER_SIGNATURE != (vbnv[HEADER_OFFSET] & HEADER_MASK)) |
| 89 | || (crc8(vbnv, CRC_OFFSET) != vbnv[CRC_OFFSET])) { |
| 90 | |
| 91 | /* Data is inconsistent (bad CRC or header), |
| 92 | * so reset to defaults |
| 93 | */ |
| 94 | memset(vbnv, 0, VBNV_BLOCK_SIZE); |
| 95 | vbnv[HEADER_OFFSET] = |
| 96 | (HEADER_SIGNATURE | HEADER_FIRMWARE_SETTINGS_RESET | |
| 97 | HEADER_KERNEL_SETTINGS_RESET); |
| 98 | } |
| 99 | vbnv_initialized = 1; |
| 100 | } |
| 101 | |
| 102 | int get_recovery_mode_from_vbnv(void) |
| 103 | { |
| 104 | if (!vbnv_initialized) |
| 105 | vbnv_setup(); |
| 106 | return vbnv[RECOVERY_OFFSET]; |
| 107 | } |
Bill Richardson | 0a405ba | 2012-06-26 16:33:45 -0700 | [diff] [blame] | 108 | |
| 109 | int vboot_wants_oprom(void) |
| 110 | { |
| 111 | if (!vbnv_initialized) |
| 112 | vbnv_setup(); |
| 113 | |
| 114 | /* FIXME(crosbug.com/p/8789). The following commented-out line does the |
| 115 | * right thing, assuming that vboot has requested the option ROM and |
| 116 | * rebooted if it finds that it's needed but not loaded. At the moment, |
| 117 | * it doesn't yet do that, so we must always say we want it. */ |
| 118 | |
| 119 | /* return (vbnv[BOOT_OFFSET] & BOOT_OPROM_NEEDED) ? 1 : 0; */ |
| 120 | return 1; |
| 121 | } |