Aaron Durbin | 9a7d7bc | 2013-09-07 00:41:48 -0500 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
Duncan Laurie | d8c4f2b | 2014-04-22 10:46:06 -0700 | [diff] [blame] | 4 | * Copyright (C) 2014 Google Inc. |
Aaron Durbin | 9a7d7bc | 2013-09-07 00:41:48 -0500 | [diff] [blame] | 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 |
Patrick Georgi | b890a12 | 2015-03-26 15:17:45 +0100 | [diff] [blame] | 17 | * Foundation, Inc. |
Aaron Durbin | 9a7d7bc | 2013-09-07 00:41:48 -0500 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | #include <string.h> |
Aaron Durbin | 4d3de7e | 2015-09-02 17:34:04 -0500 | [diff] [blame^] | 21 | #include <bootstate.h> |
Aaron Durbin | 9a7d7bc | 2013-09-07 00:41:48 -0500 | [diff] [blame] | 22 | #include <console/console.h> |
| 23 | #include <cbmem.h> |
Aaron Durbin | 0424c95 | 2015-03-28 23:56:22 -0500 | [diff] [blame] | 24 | #include <fmap.h> |
Aaron Durbin | 9a7d7bc | 2013-09-07 00:41:48 -0500 | [diff] [blame] | 25 | #include <ip_checksum.h> |
Duncan Laurie | d8c4f2b | 2014-04-22 10:46:06 -0700 | [diff] [blame] | 26 | #include "mrc_cache.h" |
Aaron Durbin | 4d3de7e | 2015-09-02 17:34:04 -0500 | [diff] [blame^] | 27 | #include "nvm.h" |
Aaron Durbin | 9a7d7bc | 2013-09-07 00:41:48 -0500 | [diff] [blame] | 28 | |
| 29 | #define MRC_DATA_ALIGN 0x1000 |
| 30 | #define MRC_DATA_SIGNATURE (('M'<<0)|('R'<<8)|('C'<<16)|('D'<<24)) |
| 31 | |
| 32 | /* The mrc_data_region describes the larger non-volatile area to store |
| 33 | * mrc_saved_data objects.*/ |
| 34 | struct mrc_data_region { |
| 35 | void *base; |
| 36 | uint32_t size; |
| 37 | }; |
| 38 | |
| 39 | /* common code */ |
| 40 | static int mrc_cache_get_region(struct mrc_data_region *region) |
| 41 | { |
Aaron Durbin | 0424c95 | 2015-03-28 23:56:22 -0500 | [diff] [blame] | 42 | if (IS_ENABLED(CONFIG_CHROMEOS)) { |
| 43 | struct region_device rdev; |
| 44 | |
| 45 | if (fmap_locate_area_as_rdev("RW_MRC_CACHE", &rdev)) |
| 46 | return -1; |
| 47 | |
| 48 | region->size = region_device_sz(&rdev); |
| 49 | region->base = rdev_mmap_full(&rdev); |
| 50 | |
| 51 | if (region->base == NULL) |
| 52 | return -1; |
| 53 | |
Aaron Durbin | 9a7d7bc | 2013-09-07 00:41:48 -0500 | [diff] [blame] | 54 | return 0; |
Aaron Durbin | 0424c95 | 2015-03-28 23:56:22 -0500 | [diff] [blame] | 55 | } else { |
| 56 | region->base = (void *)CONFIG_MRC_SETTINGS_CACHE_BASE; |
| 57 | region->size = CONFIG_MRC_SETTINGS_CACHE_SIZE; |
Aaron Durbin | 9a7d7bc | 2013-09-07 00:41:48 -0500 | [diff] [blame] | 58 | } |
Aaron Durbin | 9a7d7bc | 2013-09-07 00:41:48 -0500 | [diff] [blame] | 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | static int mrc_cache_in_region(const struct mrc_data_region *region, |
| 63 | const struct mrc_saved_data *cache) |
| 64 | { |
| 65 | uintptr_t region_end; |
| 66 | uintptr_t cache_end; |
| 67 | |
| 68 | if ((uintptr_t)cache < (uintptr_t)region->base) |
| 69 | return 0; |
| 70 | |
| 71 | region_end = (uintptr_t)region->base; |
| 72 | region_end += region->size; |
| 73 | |
| 74 | if ((uintptr_t)cache >= region_end) |
| 75 | return 0; |
| 76 | |
| 77 | if ((sizeof(*cache) + (uintptr_t)cache) >= region_end) |
| 78 | return 0; |
| 79 | |
| 80 | cache_end = (uintptr_t)cache; |
| 81 | cache_end += cache->size + sizeof(*cache); |
| 82 | |
| 83 | if (cache_end > region_end) |
| 84 | return 0; |
| 85 | |
| 86 | return 1; |
| 87 | } |
| 88 | |
| 89 | static int mrc_cache_valid(const struct mrc_data_region *region, |
| 90 | const struct mrc_saved_data *cache) |
| 91 | { |
| 92 | uint32_t checksum; |
| 93 | |
| 94 | if (cache->signature != MRC_DATA_SIGNATURE) |
| 95 | return 0; |
| 96 | |
| 97 | if (cache->size > region->size) |
| 98 | return 0; |
| 99 | |
| 100 | if (cache->reserved != 0) |
| 101 | return 0; |
| 102 | |
| 103 | checksum = compute_ip_checksum((void *)&cache->data[0], cache->size); |
| 104 | |
| 105 | if (cache->checksum != checksum) |
| 106 | return 0; |
| 107 | |
| 108 | return 1; |
| 109 | } |
| 110 | |
| 111 | static const struct mrc_saved_data * |
| 112 | next_cache_block(const struct mrc_saved_data *cache) |
| 113 | { |
| 114 | uintptr_t next = (uintptr_t)cache; |
| 115 | |
| 116 | next += ALIGN(cache->size + sizeof(*cache), MRC_DATA_ALIGN); |
| 117 | |
| 118 | return (const struct mrc_saved_data *)next; |
| 119 | } |
| 120 | |
| 121 | /* Locate the most recently saved MRC data. */ |
| 122 | static int __mrc_cache_get_current(const struct mrc_data_region *region, |
| 123 | const struct mrc_saved_data **cache) |
| 124 | { |
| 125 | const struct mrc_saved_data *msd; |
| 126 | const struct mrc_saved_data *verified_cache; |
Duncan Laurie | d8c4f2b | 2014-04-22 10:46:06 -0700 | [diff] [blame] | 127 | int slot = 0; |
Aaron Durbin | 9a7d7bc | 2013-09-07 00:41:48 -0500 | [diff] [blame] | 128 | |
| 129 | msd = region->base; |
| 130 | |
| 131 | verified_cache = NULL; |
| 132 | |
| 133 | while (mrc_cache_in_region(region, msd) && |
| 134 | mrc_cache_valid(region, msd)) { |
| 135 | verified_cache = msd; |
| 136 | msd = next_cache_block(msd); |
Duncan Laurie | d8c4f2b | 2014-04-22 10:46:06 -0700 | [diff] [blame] | 137 | slot++; |
Aaron Durbin | 9a7d7bc | 2013-09-07 00:41:48 -0500 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | if (verified_cache == NULL) |
| 141 | return -1; |
| 142 | |
| 143 | *cache = verified_cache; |
Duncan Laurie | d8c4f2b | 2014-04-22 10:46:06 -0700 | [diff] [blame] | 144 | printk(BIOS_DEBUG, "MRC cache slot %d @ %p\n", slot-1, verified_cache); |
| 145 | |
Aaron Durbin | 9a7d7bc | 2013-09-07 00:41:48 -0500 | [diff] [blame] | 146 | return 0; |
| 147 | } |
| 148 | |
| 149 | int mrc_cache_get_current(const struct mrc_saved_data **cache) |
| 150 | { |
| 151 | struct mrc_data_region region; |
| 152 | |
| 153 | if (mrc_cache_get_region(®ion) < 0) |
| 154 | return -1; |
| 155 | |
| 156 | return __mrc_cache_get_current(®ion, cache); |
| 157 | } |
| 158 | |
Aaron Durbin | 9a7d7bc | 2013-09-07 00:41:48 -0500 | [diff] [blame] | 159 | /* Fill in mrc_saved_data structure with payload. */ |
| 160 | static void mrc_cache_fill(struct mrc_saved_data *cache, void *data, |
| 161 | size_t size) |
| 162 | { |
| 163 | cache->signature = MRC_DATA_SIGNATURE; |
| 164 | cache->size = size; |
| 165 | cache->reserved = 0; |
| 166 | memcpy(&cache->data[0], data, size); |
| 167 | cache->checksum = compute_ip_checksum((void *)&cache->data[0], |
| 168 | cache->size); |
| 169 | } |
| 170 | |
| 171 | int mrc_cache_stash_data(void *data, size_t size) |
| 172 | { |
| 173 | int cbmem_size; |
| 174 | struct mrc_saved_data *cache; |
| 175 | |
| 176 | cbmem_size = sizeof(*cache) + ALIGN(size, 16); |
| 177 | |
| 178 | cache = cbmem_add(CBMEM_ID_MRCDATA, cbmem_size); |
| 179 | |
| 180 | if (cache == NULL) { |
| 181 | printk(BIOS_ERR, "No space in cbmem for MRC data.\n"); |
| 182 | return -1; |
| 183 | } |
| 184 | |
Aaron Durbin | 4177db5 | 2014-02-05 14:55:26 -0600 | [diff] [blame] | 185 | /* Clear alignment padding bytes at end of data. */ |
| 186 | memset(&cache->data[size], 0, cbmem_size - size - sizeof(*cache)); |
Aaron Durbin | 9a7d7bc | 2013-09-07 00:41:48 -0500 | [diff] [blame] | 187 | |
| 188 | printk(BIOS_DEBUG, "Relocate MRC DATA from %p to %p (%zu bytes)\n", |
| 189 | data, cache, size); |
| 190 | |
| 191 | mrc_cache_fill(cache, data, size); |
| 192 | |
| 193 | return 0; |
| 194 | } |
| 195 | |
Aaron Durbin | 9a7d7bc | 2013-09-07 00:41:48 -0500 | [diff] [blame] | 196 | static int mrc_slot_valid(const struct mrc_data_region *region, |
| 197 | const struct mrc_saved_data *slot, |
| 198 | const struct mrc_saved_data *to_save) |
| 199 | { |
| 200 | uintptr_t region_begin; |
| 201 | uintptr_t region_end; |
| 202 | uintptr_t slot_end; |
| 203 | uintptr_t slot_begin; |
| 204 | uint32_t size; |
| 205 | |
| 206 | region_begin = (uintptr_t)region->base; |
| 207 | region_end = region_begin + region->size; |
| 208 | slot_begin = (uintptr_t)slot; |
| 209 | size = to_save->size + sizeof(*to_save); |
| 210 | slot_end = slot_begin + size; |
| 211 | |
Aaron Durbin | 4177db5 | 2014-02-05 14:55:26 -0600 | [diff] [blame] | 212 | if (slot_begin < region_begin || slot_begin >= region_end) |
Aaron Durbin | 9a7d7bc | 2013-09-07 00:41:48 -0500 | [diff] [blame] | 213 | return 0; |
| 214 | |
| 215 | if (size > region->size) |
| 216 | return 0; |
| 217 | |
| 218 | if (slot_end > region_end || slot_end < region_begin) |
| 219 | return 0; |
| 220 | |
| 221 | if (!nvm_is_erased(slot, size)) |
| 222 | return 0; |
| 223 | |
| 224 | return 1; |
| 225 | } |
| 226 | |
| 227 | static const struct mrc_saved_data * |
| 228 | mrc_cache_next_slot(const struct mrc_data_region *region, |
| 229 | const struct mrc_saved_data *current_slot) |
| 230 | { |
| 231 | const struct mrc_saved_data *next_slot; |
| 232 | |
| 233 | if (current_slot == NULL) { |
| 234 | next_slot = region->base; |
| 235 | } else { |
| 236 | next_slot = next_cache_block(current_slot); |
| 237 | } |
| 238 | |
| 239 | return next_slot; |
| 240 | } |
| 241 | |
Duncan Laurie | a32b6b9 | 2015-01-15 15:49:07 -0800 | [diff] [blame] | 242 | /* Protect RW_MRC_CACHE region with a Protected Range Register */ |
| 243 | static int protect_mrc_cache(const struct mrc_data_region *region) |
| 244 | { |
| 245 | #if IS_ENABLED(CONFIG_MRC_SETTINGS_PROTECT) |
| 246 | if (nvm_is_write_protected() <= 0) { |
| 247 | printk(BIOS_INFO, "NOT enabling PRR for RW_MRC_CACHE region\n"); |
| 248 | return 1; |
| 249 | } |
| 250 | |
| 251 | if (nvm_protect(region->base, region->size) < 0) { |
| 252 | printk(BIOS_ERR, "ERROR setting PRR for RW_MRC_CACHE region\n"); |
| 253 | return -1; |
| 254 | } |
| 255 | |
| 256 | printk(BIOS_INFO, "Enabled Protected Range on RW_MRC_CACHE region\n"); |
| 257 | #endif |
| 258 | return 0; |
| 259 | } |
| 260 | |
Aaron Durbin | 9a7d7bc | 2013-09-07 00:41:48 -0500 | [diff] [blame] | 261 | static void update_mrc_cache(void *unused) |
| 262 | { |
| 263 | const struct mrc_saved_data *current_boot; |
| 264 | const struct mrc_saved_data *current_saved; |
| 265 | const struct mrc_saved_data *next_slot; |
| 266 | struct mrc_data_region region; |
| 267 | |
| 268 | printk(BIOS_DEBUG, "Updating MRC cache data.\n"); |
| 269 | |
| 270 | current_boot = cbmem_find(CBMEM_ID_MRCDATA); |
| 271 | if (!current_boot) { |
| 272 | printk(BIOS_ERR, "No MRC cache in cbmem.\n"); |
| 273 | return; |
| 274 | } |
| 275 | |
| 276 | if (mrc_cache_get_region(®ion)) { |
| 277 | printk(BIOS_ERR, "Could not obtain MRC cache region.\n"); |
| 278 | return; |
| 279 | } |
| 280 | |
| 281 | if (!mrc_cache_valid(®ion, current_boot)) { |
| 282 | printk(BIOS_ERR, "MRC cache data in cbmem invalid.\n"); |
| 283 | return; |
| 284 | } |
| 285 | |
| 286 | current_saved = NULL; |
| 287 | |
| 288 | if (!__mrc_cache_get_current(®ion, ¤t_saved)) { |
| 289 | if (current_saved->size == current_boot->size && |
| 290 | !memcmp(¤t_saved->data[0], ¤t_boot->data[0], |
| 291 | current_saved->size)) { |
| 292 | printk(BIOS_DEBUG, "MRC cache up to date.\n"); |
Duncan Laurie | a32b6b9 | 2015-01-15 15:49:07 -0800 | [diff] [blame] | 293 | protect_mrc_cache(®ion); |
Aaron Durbin | 9a7d7bc | 2013-09-07 00:41:48 -0500 | [diff] [blame] | 294 | return; |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | next_slot = mrc_cache_next_slot(®ion, current_saved); |
| 299 | |
| 300 | if (!mrc_slot_valid(®ion, next_slot, current_boot)) { |
Aaron Durbin | 931e590 | 2014-01-13 11:34:51 -0600 | [diff] [blame] | 301 | printk(BIOS_DEBUG, "Slot @ %p is invalid.\n", next_slot); |
| 302 | if (!nvm_is_erased(region.base, region.size)) { |
| 303 | if (nvm_erase(region.base, region.size) < 0) { |
| 304 | printk(BIOS_DEBUG, "Failure erasing region.\n"); |
| 305 | return; |
| 306 | } |
Aaron Durbin | 9a7d7bc | 2013-09-07 00:41:48 -0500 | [diff] [blame] | 307 | } |
| 308 | next_slot = region.base; |
| 309 | } |
| 310 | |
Aaron Durbin | 4177db5 | 2014-02-05 14:55:26 -0600 | [diff] [blame] | 311 | if (nvm_write((void *)next_slot, current_boot, |
| 312 | current_boot->size + sizeof(*current_boot))) { |
| 313 | printk(BIOS_DEBUG, "Failure writing MRC cache to %p.\n", |
| 314 | next_slot); |
| 315 | } |
Duncan Laurie | a32b6b9 | 2015-01-15 15:49:07 -0800 | [diff] [blame] | 316 | protect_mrc_cache(®ion); |
Aaron Durbin | 9a7d7bc | 2013-09-07 00:41:48 -0500 | [diff] [blame] | 317 | } |
| 318 | |
Aaron Durbin | 9ef9d85 | 2015-03-16 17:30:09 -0500 | [diff] [blame] | 319 | BOOT_STATE_INIT_ENTRY(BS_WRITE_TABLES, BS_ON_ENTRY, update_mrc_cache, NULL); |