blob: 6537bf04dc1730d6e30d2e392eb1f6bfca3131b9 [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
Hung-Te Linfe2fc832016-12-29 20:59:37 +080082/* Re-generate VBNV checksum. */
83void regen_vbnv_crc(uint8_t *vbnv_copy)
84{
85 vbnv_copy[CRC_OFFSET] = crc8_vbnv(vbnv_copy, CRC_OFFSET);
86}
87
Furquan Shaikh165b6cf2016-06-27 16:19:09 -070088/*
89 * Read VBNV data from configured storage backend.
90 * If VBNV verification fails, reset the vbnv copy.
Furquan Shaikh165b6cf2016-06-27 16:19:09 -070091 */
Furquan Shaikhbe87f732016-06-29 11:26:27 -070092void read_vbnv(uint8_t *vbnv_copy)
Duncan Laurie88b28ad2016-01-25 17:13:27 -080093{
Furquan Shaikh2a12e2e2016-07-25 11:48:03 -070094 if (IS_ENABLED(CONFIG_VBOOT_VBNV_CMOS))
Duncan Laurie88b28ad2016-01-25 17:13:27 -080095 read_vbnv_cmos(vbnv_copy);
Furquan Shaikh2a12e2e2016-07-25 11:48:03 -070096 else if (IS_ENABLED(CONFIG_VBOOT_VBNV_EC))
Duncan Laurie88b28ad2016-01-25 17:13:27 -080097 read_vbnv_ec(vbnv_copy);
Furquan Shaikh2a12e2e2016-07-25 11:48:03 -070098 else if (IS_ENABLED(CONFIG_VBOOT_VBNV_FLASH))
Duncan Laurie88b28ad2016-01-25 17:13:27 -080099 read_vbnv_flash(vbnv_copy);
100
101 /* Check data for consistency */
Furquan Shaikhbe87f732016-06-29 11:26:27 -0700102 if (!verify_vbnv(vbnv_copy))
103 reset_vbnv(vbnv_copy);
Duncan Laurie88b28ad2016-01-25 17:13:27 -0800104}
105
106/*
107 * Write VBNV data to configured storage backend.
108 * This assumes that the caller has updated the CRC already.
109 */
110void save_vbnv(const uint8_t *vbnv_copy)
111{
Furquan Shaikh2a12e2e2016-07-25 11:48:03 -0700112 if (IS_ENABLED(CONFIG_VBOOT_VBNV_CMOS))
Duncan Laurie88b28ad2016-01-25 17:13:27 -0800113 save_vbnv_cmos(vbnv_copy);
Furquan Shaikh2a12e2e2016-07-25 11:48:03 -0700114 else if (IS_ENABLED(CONFIG_VBOOT_VBNV_EC))
Duncan Laurie88b28ad2016-01-25 17:13:27 -0800115 save_vbnv_ec(vbnv_copy);
Furquan Shaikh2a12e2e2016-07-25 11:48:03 -0700116 else if (IS_ENABLED(CONFIG_VBOOT_VBNV_FLASH))
Duncan Laurie88b28ad2016-01-25 17:13:27 -0800117 save_vbnv_flash(vbnv_copy);
118
119 /* Clear initialized flag to force cached data to be updated */
120 car_set_var(vbnv_initialized, 0);
121}
122
123/* Save a recovery reason into VBNV. */
124void set_recovery_mode_into_vbnv(int recovery_reason)
125{
Furquan Shaikh2a12e2e2016-07-25 11:48:03 -0700126 uint8_t vbnv_copy[VBOOT_VBNV_BLOCK_SIZE];
Duncan Laurie88b28ad2016-01-25 17:13:27 -0800127
128 read_vbnv(vbnv_copy);
129
130 vbnv_copy[RECOVERY_OFFSET] = recovery_reason;
131 vbnv_copy[CRC_OFFSET] = crc8_vbnv(vbnv_copy, CRC_OFFSET);
132
133 save_vbnv(vbnv_copy);
134}
135
136/* Read the recovery reason from VBNV. */
137int get_recovery_mode_from_vbnv(void)
138{
139 vbnv_setup();
140 return vbnv_data(RECOVERY_OFFSET);
141}
142
143/* Read the BOOT_OPROM_NEEDED flag from VBNV. */
144int vboot_wants_oprom(void)
145{
146 vbnv_setup();
147 return (vbnv_data(BOOT_OFFSET) & BOOT_OPROM_NEEDED) ? 1 : 0;
148}