Duncan Laurie | 88b28ad | 2016-01-25 17:13:27 -0800 | [diff] [blame] | 1 | /* |
| 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 Shaikh | 2a12e2e | 2016-07-25 11:48:03 -0700 | [diff] [blame] | 19 | #include <vboot/vbnv.h> |
| 20 | #include <vboot/vbnv_layout.h> |
Duncan Laurie | 88b28ad | 2016-01-25 17:13:27 -0800 | [diff] [blame] | 21 | |
| 22 | static int vbnv_initialized CAR_GLOBAL; |
Furquan Shaikh | 2a12e2e | 2016-07-25 11:48:03 -0700 | [diff] [blame] | 23 | static uint8_t vbnv[VBOOT_VBNV_BLOCK_SIZE] CAR_GLOBAL; |
Duncan Laurie | 88b28ad | 2016-01-25 17:13:27 -0800 | [diff] [blame] | 24 | |
| 25 | /* Wrappers for accessing the variables marked as CAR_GLOBAL. */ |
| 26 | static inline int is_vbnv_initialized(void) |
| 27 | { |
| 28 | return car_get_var(vbnv_initialized); |
| 29 | } |
| 30 | |
| 31 | static 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 | |
| 38 | static 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. */ |
| 44 | static 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 Laurie | 88b28ad | 2016-01-25 17:13:27 -0800 | [diff] [blame] | 61 | static void reset_vbnv(uint8_t *vbnv_copy) |
| 62 | { |
Furquan Shaikh | 2a12e2e | 2016-07-25 11:48:03 -0700 | [diff] [blame] | 63 | memset(vbnv_copy, 0, VBOOT_VBNV_BLOCK_SIZE); |
Duncan Laurie | 88b28ad | 2016-01-25 17:13:27 -0800 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | /* Read VBNV data into cache. */ |
| 67 | static 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. */ |
| 76 | int 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 Shaikh | 165b6cf | 2016-06-27 16:19:09 -0700 | [diff] [blame] | 82 | /* |
| 83 | * Read VBNV data from configured storage backend. |
| 84 | * If VBNV verification fails, reset the vbnv copy. |
Furquan Shaikh | 165b6cf | 2016-06-27 16:19:09 -0700 | [diff] [blame] | 85 | */ |
Furquan Shaikh | be87f73 | 2016-06-29 11:26:27 -0700 | [diff] [blame] | 86 | void read_vbnv(uint8_t *vbnv_copy) |
Duncan Laurie | 88b28ad | 2016-01-25 17:13:27 -0800 | [diff] [blame] | 87 | { |
Furquan Shaikh | 2a12e2e | 2016-07-25 11:48:03 -0700 | [diff] [blame] | 88 | if (IS_ENABLED(CONFIG_VBOOT_VBNV_CMOS)) |
Duncan Laurie | 88b28ad | 2016-01-25 17:13:27 -0800 | [diff] [blame] | 89 | read_vbnv_cmos(vbnv_copy); |
Furquan Shaikh | 2a12e2e | 2016-07-25 11:48:03 -0700 | [diff] [blame] | 90 | else if (IS_ENABLED(CONFIG_VBOOT_VBNV_EC)) |
Duncan Laurie | 88b28ad | 2016-01-25 17:13:27 -0800 | [diff] [blame] | 91 | read_vbnv_ec(vbnv_copy); |
Furquan Shaikh | 2a12e2e | 2016-07-25 11:48:03 -0700 | [diff] [blame] | 92 | else if (IS_ENABLED(CONFIG_VBOOT_VBNV_FLASH)) |
Duncan Laurie | 88b28ad | 2016-01-25 17:13:27 -0800 | [diff] [blame] | 93 | read_vbnv_flash(vbnv_copy); |
| 94 | |
| 95 | /* Check data for consistency */ |
Furquan Shaikh | be87f73 | 2016-06-29 11:26:27 -0700 | [diff] [blame] | 96 | if (!verify_vbnv(vbnv_copy)) |
| 97 | reset_vbnv(vbnv_copy); |
Duncan Laurie | 88b28ad | 2016-01-25 17:13:27 -0800 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | /* |
| 101 | * Write VBNV data to configured storage backend. |
| 102 | * This assumes that the caller has updated the CRC already. |
| 103 | */ |
| 104 | void save_vbnv(const uint8_t *vbnv_copy) |
| 105 | { |
Furquan Shaikh | 2a12e2e | 2016-07-25 11:48:03 -0700 | [diff] [blame] | 106 | if (IS_ENABLED(CONFIG_VBOOT_VBNV_CMOS)) |
Duncan Laurie | 88b28ad | 2016-01-25 17:13:27 -0800 | [diff] [blame] | 107 | save_vbnv_cmos(vbnv_copy); |
Furquan Shaikh | 2a12e2e | 2016-07-25 11:48:03 -0700 | [diff] [blame] | 108 | else if (IS_ENABLED(CONFIG_VBOOT_VBNV_EC)) |
Duncan Laurie | 88b28ad | 2016-01-25 17:13:27 -0800 | [diff] [blame] | 109 | save_vbnv_ec(vbnv_copy); |
Furquan Shaikh | 2a12e2e | 2016-07-25 11:48:03 -0700 | [diff] [blame] | 110 | else if (IS_ENABLED(CONFIG_VBOOT_VBNV_FLASH)) |
Duncan Laurie | 88b28ad | 2016-01-25 17:13:27 -0800 | [diff] [blame] | 111 | 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. */ |
| 118 | void set_recovery_mode_into_vbnv(int recovery_reason) |
| 119 | { |
Furquan Shaikh | 2a12e2e | 2016-07-25 11:48:03 -0700 | [diff] [blame] | 120 | uint8_t vbnv_copy[VBOOT_VBNV_BLOCK_SIZE]; |
Duncan Laurie | 88b28ad | 2016-01-25 17:13:27 -0800 | [diff] [blame] | 121 | |
| 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. */ |
| 131 | int 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. */ |
| 138 | int vboot_wants_oprom(void) |
| 139 | { |
| 140 | vbnv_setup(); |
| 141 | return (vbnv_data(BOOT_OFFSET) & BOOT_OPROM_NEEDED) ? 1 : 0; |
| 142 | } |