blob: d94203ad6d1b1862267594a12d5fa57a53b9ef73 [file] [log] [blame]
Stefan Reinauer9aea04a2012-03-30 12:01:06 -07001/*
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 Richardson0a405ba2012-06-26 16:33:45 -070041#define BOOT_DISABLE_DEV_REQUEST 0x40
42#define BOOT_OPROM_NEEDED 0x20
Stefan Reinauer9aea04a2012-03-30 12:01:06 -070043#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 Richardson0a405ba2012-06-26 16:33:45 -070050#define DEV_BOOT_SIGNED_ONLY_MASK 0x02
Stefan Reinauer9aea04a2012-03-30 12:01:06 -070051
52#define KERNEL_FIELD_OFFSET 11
53#define CRC_OFFSET 15
54
55static int vbnv_initialized CAR_GLOBAL;
Aaron Durbin677e1552013-05-10 00:42:14 -050056static uint8_t vbnv[CONFIG_VBNV_SIZE] CAR_GLOBAL;
57
58/* Wrappers for accessing the variables marked as CAR_GLOBAL. */
59static inline int is_vbnv_initialized(void)
60{
61 return car_get_var(vbnv_initialized);
62}
63
64static 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
71static inline uint8_t vbnv_data(int index)
72{
73 return *vbnv_data_addr(index);
74}
Stefan Reinauer9aea04a2012-03-30 12:01:06 -070075
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
81static 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 Durbinfd795622013-03-01 17:12:26 -060098void read_vbnv(uint8_t *vbnv_copy)
Stefan Reinauer9aea04a2012-03-30 12:01:06 -070099{
100 int i;
101
102 for (i = 0; i < CONFIG_VBNV_SIZE; i++)
Aaron Durbinfd795622013-03-01 17:12:26 -0600103 vbnv_copy[i] = cmos_read(CONFIG_VBNV_OFFSET + 14 + i);
Stefan Reinauer9aea04a2012-03-30 12:01:06 -0700104
105 /* Check data for consistency */
Aaron Durbinfd795622013-03-01 17:12:26 -0600106 if ((HEADER_SIGNATURE != (vbnv_copy[HEADER_OFFSET] & HEADER_MASK))
107 || (crc8(vbnv_copy, CRC_OFFSET) != vbnv_copy[CRC_OFFSET])) {
Stefan Reinauer9aea04a2012-03-30 12:01:06 -0700108
109 /* Data is inconsistent (bad CRC or header),
110 * so reset to defaults
111 */
Aaron Durbinfd795622013-03-01 17:12:26 -0600112 memset(vbnv_copy, 0, VBNV_BLOCK_SIZE);
113 vbnv_copy[HEADER_OFFSET] =
Stefan Reinauer9aea04a2012-03-30 12:01:06 -0700114 (HEADER_SIGNATURE | HEADER_FIRMWARE_SETTINGS_RESET |
115 HEADER_KERNEL_SETTINGS_RESET);
116 }
Aaron Durbinfd795622013-03-01 17:12:26 -0600117}
118
119void 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
128static void vbnv_setup(void)
129{
Aaron Durbin677e1552013-05-10 00:42:14 -0500130 read_vbnv(vbnv_data_addr(0));
131 car_set_var(vbnv_initialized, 1);
Stefan Reinauer9aea04a2012-03-30 12:01:06 -0700132}
133
134int get_recovery_mode_from_vbnv(void)
135{
Aaron Durbin677e1552013-05-10 00:42:14 -0500136 if (!is_vbnv_initialized())
Stefan Reinauer9aea04a2012-03-30 12:01:06 -0700137 vbnv_setup();
Aaron Durbin677e1552013-05-10 00:42:14 -0500138 return vbnv_data(RECOVERY_OFFSET);
Stefan Reinauer9aea04a2012-03-30 12:01:06 -0700139}
Bill Richardson0a405ba2012-06-26 16:33:45 -0700140
141int vboot_wants_oprom(void)
142{
Aaron Durbin677e1552013-05-10 00:42:14 -0500143 if (!is_vbnv_initialized())
Bill Richardson0a405ba2012-06-26 16:33:45 -0700144 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 Durbin677e1552013-05-10 00:42:14 -0500151 /* return (vbnv_data(BOOT_OFFSET) & BOOT_OPROM_NEEDED) ? 1 : 0; */
Bill Richardson0a405ba2012-06-26 16:33:45 -0700152 return 1;
153}