2 * This file is part of the coreboot project.
4 * Copyright (C) 2011 The ChromiumOS Authors. All rights reserved.
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.
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.
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
22 #include <console/console.h>
23 #include <pc80/mc146818rtc.h>
24 #include <cpu/x86/car.h>
27 #define VBNV_BLOCK_SIZE 16 /* Size of NV storage block in bytes */
29 /* Constants for NV storage. We use this rather than structs and
30 * bitfields so the data format is consistent across platforms and
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
40 #define BOOT_DEBUG_RESET_MODE 0x80
41 #define BOOT_TRY_B_COUNT_MASK 0x0F
43 #define RECOVERY_OFFSET 2
44 #define LOCALIZATION_OFFSET 3
46 #define DEV_FLAGS_OFFSET 4
47 #define DEV_BOOT_USB_MASK 0x01
49 #define FIRMWARE_FLAGS_OFFSET 5
50 #define FIRMWARE_TEST_ERR_FUNC_MASK 0x38
51 #define FIRMWARE_TEST_ERR_FUNC_SHIFT 3
52 #define FIRMWARE_TEST_ERR_NUM_MASK 0x07
54 #define KERNEL_FIELD_OFFSET 11
57 static int vbnv_initialized CAR_GLOBAL;
58 uint8_t vbnv[CONFIG_VBNV_SIZE] CAR_GLOBAL;
60 /* Return CRC-8 of the data, using x^8 + x^2 + x + 1 polynomial. A
61 * table-based algorithm would be faster, but for only 15 bytes isn't
62 * worth the code size.
65 static uint8_t crc8(const uint8_t * data, int len)
70 for (j = len; j; j--, data++) {
79 return (uint8_t) (crc >> 8);
82 static void vbnv_setup(void)
86 for (i = 0; i < CONFIG_VBNV_SIZE; i++)
87 vbnv[i] = cmos_read(CONFIG_VBNV_OFFSET + 14 + i);
89 /* Check data for consistency */
90 if ((HEADER_SIGNATURE != (vbnv[HEADER_OFFSET] & HEADER_MASK))
91 || (crc8(vbnv, CRC_OFFSET) != vbnv[CRC_OFFSET])) {
93 /* Data is inconsistent (bad CRC or header),
94 * so reset to defaults
96 memset(vbnv, 0, VBNV_BLOCK_SIZE);
98 (HEADER_SIGNATURE | HEADER_FIRMWARE_SETTINGS_RESET |
99 HEADER_KERNEL_SETTINGS_RESET);
101 vbnv_initialized = 1;
104 int get_recovery_mode_from_vbnv(void)
106 if (!vbnv_initialized)
108 return vbnv[RECOVERY_OFFSET];