Add Google ChromeOS vendor support
[coreboot.git] / src / vendorcode / google / chromeos / vbnv.c
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
41 #define BOOT_TRY_B_COUNT_MASK           0x0F
42
43 #define RECOVERY_OFFSET              2
44 #define LOCALIZATION_OFFSET          3
45
46 #define DEV_FLAGS_OFFSET             4
47 #define DEV_BOOT_USB_MASK               0x01
48
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
53
54 #define KERNEL_FIELD_OFFSET         11
55 #define CRC_OFFSET                  15
56
57 static int vbnv_initialized CAR_GLOBAL;
58 uint8_t vbnv[CONFIG_VBNV_SIZE] CAR_GLOBAL;
59
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.
63  */
64
65 static uint8_t crc8(const uint8_t * data, int len)
66 {
67         unsigned crc = 0;
68         int i, j;
69
70         for (j = len; j; j--, data++) {
71                 crc ^= (*data << 8);
72                 for (i = 8; i; i--) {
73                         if (crc & 0x8000)
74                                 crc ^= (0x1070 << 3);
75                         crc <<= 1;
76                 }
77         }
78
79         return (uint8_t) (crc >> 8);
80 }
81
82 static void vbnv_setup(void)
83 {
84         int i;
85
86         for (i = 0; i < CONFIG_VBNV_SIZE; i++)
87                 vbnv[i] = cmos_read(CONFIG_VBNV_OFFSET + 14 + i);
88
89         /* Check data for consistency */
90         if ((HEADER_SIGNATURE != (vbnv[HEADER_OFFSET] & HEADER_MASK))
91             || (crc8(vbnv, CRC_OFFSET) != vbnv[CRC_OFFSET])) {
92
93                 /* Data is inconsistent (bad CRC or header),
94                  * so reset to defaults
95                  */
96                 memset(vbnv, 0, VBNV_BLOCK_SIZE);
97                 vbnv[HEADER_OFFSET] =
98                     (HEADER_SIGNATURE | HEADER_FIRMWARE_SETTINGS_RESET |
99                      HEADER_KERNEL_SETTINGS_RESET);
100         }
101         vbnv_initialized = 1;
102 }
103
104 int get_recovery_mode_from_vbnv(void)
105 {
106         if (!vbnv_initialized)
107                 vbnv_setup();
108         return vbnv[RECOVERY_OFFSET];
109 }