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