blob: ce6492855de8c4c541c524a5b73c0c4e1a892dc8 [file] [log] [blame]
Duncan Laurie88b28ad2016-01-25 17:13:27 -08001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2016 Google Inc.
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
16#include <arch/early_variables.h>
17#include <string.h>
18#include <types.h>
Furquan Shaikh2a12e2e2016-07-25 11:48:03 -070019#include <vboot/vbnv.h>
20#include <vboot/vbnv_layout.h>
Duncan Laurie88b28ad2016-01-25 17:13:27 -080021
22static int vbnv_initialized CAR_GLOBAL;
Furquan Shaikh2a12e2e2016-07-25 11:48:03 -070023static uint8_t vbnv[VBOOT_VBNV_BLOCK_SIZE] CAR_GLOBAL;
Duncan Laurie88b28ad2016-01-25 17:13:27 -080024
25/* Wrappers for accessing the variables marked as CAR_GLOBAL. */
26static inline int is_vbnv_initialized(void)
27{
28 return car_get_var(vbnv_initialized);
29}
30
31static inline uint8_t *vbnv_data_addr(int index)
32{
33 uint8_t *vbnv_arr = car_get_var_ptr(vbnv);
34
35 return &vbnv_arr[index];
36}
37
38static inline uint8_t vbnv_data(int index)
39{
40 return *vbnv_data_addr(index);
41}
42
43/* Return CRC-8 of the data, using x^8 + x^2 + x + 1 polynomial. */
44static uint8_t crc8_vbnv(const uint8_t *data, int len)
45{
46 unsigned crc = 0;
47 int i, j;
48
49 for (j = len; j; j--, data++) {
50 crc ^= (*data << 8);
51 for (i = 8; i; i--) {
52 if (crc & 0x8000)
53 crc ^= (0x1070 << 3);
54 crc <<= 1;
55 }
56 }
57
58 return (uint8_t) (crc >> 8);
59}
60
Duncan Laurie88b28ad2016-01-25 17:13:27 -080061static void reset_vbnv(uint8_t *vbnv_copy)
62{
Furquan Shaikh2a12e2e2016-07-25 11:48:03 -070063 memset(vbnv_copy, 0, VBOOT_VBNV_BLOCK_SIZE);
Duncan Laurie88b28ad2016-01-25 17:13:27 -080064}
65
66/* Read VBNV data into cache. */
67static void vbnv_setup(void)
68{
69 if (!is_vbnv_initialized()) {
70 read_vbnv(vbnv_data_addr(0));
71 car_set_var(vbnv_initialized, 1);
72 }
73}
74
75/* Verify VBNV header and checksum. */
76int verify_vbnv(uint8_t *vbnv_copy)
77{
78 return (HEADER_SIGNATURE == (vbnv_copy[HEADER_OFFSET] & HEADER_MASK)) &&
79 (crc8_vbnv(vbnv_copy, CRC_OFFSET) == vbnv_copy[CRC_OFFSET]);
80}
81
Furquan Shaikh165b6cf2016-06-27 16:19:09 -070082/*
83 * Read VBNV data from configured storage backend.
84 * If VBNV verification fails, reset the vbnv copy.
Furquan Shaikh165b6cf2016-06-27 16:19:09 -070085 */
Furquan Shaikhbe87f732016-06-29 11:26:27 -070086void read_vbnv(uint8_t *vbnv_copy)
Duncan Laurie88b28ad2016-01-25 17:13:27 -080087{
Furquan Shaikh2a12e2e2016-07-25 11:48:03 -070088 if (IS_ENABLED(CONFIG_VBOOT_VBNV_CMOS))
Duncan Laurie88b28ad2016-01-25 17:13:27 -080089 read_vbnv_cmos(vbnv_copy);
Furquan Shaikh2a12e2e2016-07-25 11:48:03 -070090 else if (IS_ENABLED(CONFIG_VBOOT_VBNV_EC))
Duncan Laurie88b28ad2016-01-25 17:13:27 -080091 read_vbnv_ec(vbnv_copy);
Furquan Shaikh2a12e2e2016-07-25 11:48:03 -070092 else if (IS_ENABLED(CONFIG_VBOOT_VBNV_FLASH))
Duncan Laurie88b28ad2016-01-25 17:13:27 -080093 read_vbnv_flash(vbnv_copy);
94
95 /* Check data for consistency */
Furquan Shaikhbe87f732016-06-29 11:26:27 -070096 if (!verify_vbnv(vbnv_copy))
97 reset_vbnv(vbnv_copy);
Duncan Laurie88b28ad2016-01-25 17:13:27 -080098}
99
100/*
101 * Write VBNV data to configured storage backend.
102 * This assumes that the caller has updated the CRC already.
103 */
104void save_vbnv(const uint8_t *vbnv_copy)
105{
Furquan Shaikh2a12e2e2016-07-25 11:48:03 -0700106 if (IS_ENABLED(CONFIG_VBOOT_VBNV_CMOS))
Duncan Laurie88b28ad2016-01-25 17:13:27 -0800107 save_vbnv_cmos(vbnv_copy);
Furquan Shaikh2a12e2e2016-07-25 11:48:03 -0700108 else if (IS_ENABLED(CONFIG_VBOOT_VBNV_EC))
Duncan Laurie88b28ad2016-01-25 17:13:27 -0800109 save_vbnv_ec(vbnv_copy);
Furquan Shaikh2a12e2e2016-07-25 11:48:03 -0700110 else if (IS_ENABLED(CONFIG_VBOOT_VBNV_FLASH))
Duncan Laurie88b28ad2016-01-25 17:13:27 -0800111 save_vbnv_flash(vbnv_copy);
112
113 /* Clear initialized flag to force cached data to be updated */
114 car_set_var(vbnv_initialized, 0);
115}
116
117/* Save a recovery reason into VBNV. */
118void set_recovery_mode_into_vbnv(int recovery_reason)
119{
Furquan Shaikh2a12e2e2016-07-25 11:48:03 -0700120 uint8_t vbnv_copy[VBOOT_VBNV_BLOCK_SIZE];
Duncan Laurie88b28ad2016-01-25 17:13:27 -0800121
122 read_vbnv(vbnv_copy);
123
124 vbnv_copy[RECOVERY_OFFSET] = recovery_reason;
125 vbnv_copy[CRC_OFFSET] = crc8_vbnv(vbnv_copy, CRC_OFFSET);
126
127 save_vbnv(vbnv_copy);
128}
129
130/* Read the recovery reason from VBNV. */
131int get_recovery_mode_from_vbnv(void)
132{
133 vbnv_setup();
134 return vbnv_data(RECOVERY_OFFSET);
135}
136
137/* Read the BOOT_OPROM_NEEDED flag from VBNV. */
138int vboot_wants_oprom(void)
139{
140 vbnv_setup();
141 return (vbnv_data(BOOT_OFFSET) & BOOT_OPROM_NEEDED) ? 1 : 0;
142}