Aaron Durbin | 0424c95 | 2015-03-28 23:56:22 -0500 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright 2012-2015 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. |
Aaron Durbin | 0424c95 | 2015-03-28 23:56:22 -0500 | [diff] [blame] | 14 | */ |
| 15 | |
Duncan Laurie | bc2c0a3 | 2016-02-09 09:17:56 -0800 | [diff] [blame] | 16 | #include <arch/early_variables.h> |
Aaron Durbin | 0424c95 | 2015-03-28 23:56:22 -0500 | [diff] [blame] | 17 | #include <boot_device.h> |
| 18 | #include <console/console.h> |
| 19 | #include <fmap.h> |
Aaron Durbin | dc9f5cd | 2015-09-08 13:34:43 -0500 | [diff] [blame] | 20 | #include <commonlib/fmap_serialized.h> |
Aaron Durbin | 0424c95 | 2015-03-28 23:56:22 -0500 | [diff] [blame] | 21 | #include <stddef.h> |
| 22 | #include <string.h> |
Patrick Rudolph | 6d787c2 | 2019-09-12 13:21:37 +0200 | [diff] [blame] | 23 | #include <cbmem.h> |
Aaron Durbin | 0424c95 | 2015-03-28 23:56:22 -0500 | [diff] [blame] | 24 | |
Aaron Durbin | bf1e481 | 2016-05-10 15:12:08 -0500 | [diff] [blame] | 25 | #include "fmap_config.h" |
| 26 | |
Aaron Durbin | 0424c95 | 2015-03-28 23:56:22 -0500 | [diff] [blame] | 27 | /* |
| 28 | * See http://code.google.com/p/flashmap/ for more information on FMAP. |
| 29 | */ |
| 30 | |
Duncan Laurie | bc2c0a3 | 2016-02-09 09:17:56 -0800 | [diff] [blame] | 31 | static int fmap_print_once CAR_GLOBAL; |
Patrick Rudolph | 6d787c2 | 2019-09-12 13:21:37 +0200 | [diff] [blame] | 32 | static struct mem_region_device fmap_cache CAR_GLOBAL; |
Duncan Laurie | bc2c0a3 | 2016-02-09 09:17:56 -0800 | [diff] [blame] | 33 | |
Furquan Shaikh | b33a2b0 | 2019-09-26 23:51:46 -0700 | [diff] [blame^] | 34 | uint64_t get_fmap_flash_offset(void) |
| 35 | { |
| 36 | return FMAP_OFFSET; |
| 37 | } |
| 38 | |
| 39 | static int find_fmap_directory(struct region_device *fmrd) |
Aaron Durbin | 0424c95 | 2015-03-28 23:56:22 -0500 | [diff] [blame] | 40 | { |
| 41 | const struct region_device *boot; |
| 42 | struct fmap *fmap; |
| 43 | size_t fmap_size; |
Aaron Durbin | bf1e481 | 2016-05-10 15:12:08 -0500 | [diff] [blame] | 44 | size_t offset = FMAP_OFFSET; |
Aaron Durbin | 0424c95 | 2015-03-28 23:56:22 -0500 | [diff] [blame] | 45 | |
Patrick Rudolph | 6d787c2 | 2019-09-12 13:21:37 +0200 | [diff] [blame] | 46 | if (cbmem_possibly_online() && !ENV_SMM) { |
| 47 | /* Try FMAP cache first */ |
| 48 | const struct mem_region_device *cache; |
| 49 | |
| 50 | cache = car_get_var_ptr(&fmap_cache); |
| 51 | if (region_device_sz(&cache->rdev)) |
| 52 | return rdev_chain(fmrd, &cache->rdev, 0, |
| 53 | region_device_sz(&cache->rdev)); |
| 54 | } |
| 55 | |
Aaron Durbin | 0424c95 | 2015-03-28 23:56:22 -0500 | [diff] [blame] | 56 | boot_device_init(); |
| 57 | boot = boot_device_ro(); |
| 58 | |
| 59 | if (boot == NULL) |
| 60 | return -1; |
| 61 | |
| 62 | fmap_size = sizeof(struct fmap); |
| 63 | |
| 64 | fmap = rdev_mmap(boot, offset, fmap_size); |
| 65 | |
| 66 | if (fmap == NULL) |
| 67 | return -1; |
| 68 | |
| 69 | if (memcmp(fmap->signature, FMAP_SIGNATURE, sizeof(fmap->signature))) { |
| 70 | printk(BIOS_DEBUG, "No FMAP found at %zx offset.\n", offset); |
| 71 | rdev_munmap(boot, fmap); |
| 72 | return -1; |
| 73 | } |
| 74 | |
Duncan Laurie | bc2c0a3 | 2016-02-09 09:17:56 -0800 | [diff] [blame] | 75 | if (!car_get_var(fmap_print_once)) { |
| 76 | printk(BIOS_DEBUG, "FMAP: Found \"%s\" version %d.%d at %zx.\n", |
| 77 | fmap->name, fmap->ver_major, fmap->ver_minor, offset); |
| 78 | printk(BIOS_DEBUG, "FMAP: base = %llx size = %x #areas = %d\n", |
| 79 | (long long)fmap->base, fmap->size, fmap->nareas); |
| 80 | car_set_var(fmap_print_once, 1); |
| 81 | } |
Aaron Durbin | 0424c95 | 2015-03-28 23:56:22 -0500 | [diff] [blame] | 82 | |
| 83 | fmap_size += fmap->nareas * sizeof(struct fmap_area); |
| 84 | |
| 85 | rdev_munmap(boot, fmap); |
| 86 | |
| 87 | return rdev_chain(fmrd, boot, offset, fmap_size); |
| 88 | } |
| 89 | |
| 90 | int fmap_locate_area_as_rdev(const char *name, struct region_device *area) |
| 91 | { |
| 92 | struct region ar; |
| 93 | |
| 94 | if (fmap_locate_area(name, &ar)) |
| 95 | return -1; |
| 96 | |
| 97 | return boot_device_ro_subregion(&ar, area); |
| 98 | } |
| 99 | |
Aaron Durbin | bccaab8 | 2016-08-12 12:42:04 -0500 | [diff] [blame] | 100 | int fmap_locate_area_as_rdev_rw(const char *name, struct region_device *area) |
| 101 | { |
| 102 | struct region ar; |
| 103 | |
| 104 | if (fmap_locate_area(name, &ar)) |
| 105 | return -1; |
| 106 | |
| 107 | return boot_device_rw_subregion(&ar, area); |
| 108 | } |
| 109 | |
Aaron Durbin | 0424c95 | 2015-03-28 23:56:22 -0500 | [diff] [blame] | 110 | int fmap_locate_area(const char *name, struct region *ar) |
| 111 | { |
| 112 | struct region_device fmrd; |
| 113 | size_t offset; |
| 114 | |
| 115 | if (find_fmap_directory(&fmrd)) |
| 116 | return -1; |
| 117 | |
| 118 | /* Start reading the areas just after fmap header. */ |
| 119 | offset = sizeof(struct fmap); |
| 120 | |
| 121 | while (1) { |
| 122 | struct fmap_area *area; |
| 123 | |
| 124 | area = rdev_mmap(&fmrd, offset, sizeof(*area)); |
| 125 | |
| 126 | if (area == NULL) |
| 127 | return -1; |
| 128 | |
| 129 | if (strcmp((const char *)area->name, name)) { |
| 130 | rdev_munmap(&fmrd, area); |
| 131 | offset += sizeof(struct fmap_area); |
| 132 | continue; |
| 133 | } |
| 134 | |
Duncan Laurie | bc2c0a3 | 2016-02-09 09:17:56 -0800 | [diff] [blame] | 135 | printk(BIOS_DEBUG, "FMAP: area %s found @ %x (%d bytes)\n", |
| 136 | name, area->offset, area->size); |
Aaron Durbin | 0424c95 | 2015-03-28 23:56:22 -0500 | [diff] [blame] | 137 | |
| 138 | ar->offset = area->offset; |
| 139 | ar->size = area->size; |
| 140 | |
| 141 | rdev_munmap(&fmrd, area); |
| 142 | |
| 143 | return 0; |
| 144 | } |
| 145 | |
| 146 | printk(BIOS_DEBUG, "FMAP: area %s not found\n", name); |
| 147 | |
| 148 | return -1; |
| 149 | } |
Patrick Georgi | 9952690 | 2015-07-09 11:27:44 +0200 | [diff] [blame] | 150 | |
| 151 | int fmap_find_region_name(const struct region * const ar, |
| 152 | char name[FMAP_STRLEN]) |
| 153 | { |
| 154 | struct region_device fmrd; |
| 155 | size_t offset; |
| 156 | |
| 157 | if (find_fmap_directory(&fmrd)) |
| 158 | return -1; |
| 159 | |
| 160 | /* Start reading the areas just after fmap header. */ |
| 161 | offset = sizeof(struct fmap); |
| 162 | |
| 163 | while (1) { |
| 164 | struct fmap_area *area; |
| 165 | |
| 166 | area = rdev_mmap(&fmrd, offset, sizeof(*area)); |
| 167 | |
| 168 | if (area == NULL) |
| 169 | return -1; |
| 170 | |
| 171 | if ((ar->offset != area->offset) || |
| 172 | (ar->size != area->size)) { |
| 173 | rdev_munmap(&fmrd, area); |
| 174 | offset += sizeof(struct fmap_area); |
| 175 | continue; |
| 176 | } |
| 177 | |
| 178 | printk(BIOS_DEBUG, "FMAP: area (%zx, %zx) found, named %s\n", |
| 179 | ar->offset, ar->size, area->name); |
| 180 | |
| 181 | memcpy(name, area->name, FMAP_STRLEN); |
| 182 | |
| 183 | rdev_munmap(&fmrd, area); |
| 184 | |
| 185 | return 0; |
| 186 | } |
| 187 | |
| 188 | printk(BIOS_DEBUG, "FMAP: area (%zx, %zx) not found\n", |
| 189 | ar->offset, ar->size); |
| 190 | |
| 191 | return -1; |
| 192 | } |
T Michael Turney | 19fcc89 | 2019-03-20 14:37:34 -0700 | [diff] [blame] | 193 | |
| 194 | ssize_t fmap_read_area(const char *name, void *buffer, size_t size) |
| 195 | { |
| 196 | struct region_device rdev; |
| 197 | if (fmap_locate_area_as_rdev(name, &rdev)) |
| 198 | return -1; |
| 199 | return rdev_readat(&rdev, buffer, 0, |
| 200 | MIN(size, region_device_sz(&rdev))); |
| 201 | } |
| 202 | |
| 203 | ssize_t fmap_overwrite_area(const char *name, const void *buffer, size_t size) |
| 204 | { |
| 205 | struct region_device rdev; |
| 206 | |
| 207 | if (fmap_locate_area_as_rdev_rw(name, &rdev)) |
| 208 | return -1; |
| 209 | if (size > region_device_sz(&rdev)) |
| 210 | return -1; |
| 211 | if (rdev_eraseat(&rdev, 0, region_device_sz(&rdev)) < 0) |
| 212 | return -1; |
| 213 | return rdev_writeat(&rdev, buffer, 0, size); |
| 214 | } |
Patrick Rudolph | 6d787c2 | 2019-09-12 13:21:37 +0200 | [diff] [blame] | 215 | |
| 216 | static void fmap_register_cache(int unused) |
| 217 | { |
| 218 | const struct cbmem_entry *e; |
| 219 | struct mem_region_device *mdev; |
| 220 | |
| 221 | mdev = car_get_var_ptr(&fmap_cache); |
| 222 | |
| 223 | /* Find the FMAP cache installed by previous stage */ |
| 224 | e = cbmem_entry_find(CBMEM_ID_FMAP); |
| 225 | /* Don't set fmap_cache so that find_fmap_directory will use regular path */ |
| 226 | if (!e) |
| 227 | return; |
| 228 | |
| 229 | mem_region_device_ro_init(mdev, cbmem_entry_start(e), cbmem_entry_size(e)); |
| 230 | } |
| 231 | |
| 232 | /* |
| 233 | * The main reason to copy the FMAP into CBMEM is to make it available to the |
| 234 | * OS on every architecture. As side effect use the CBMEM copy as cache. |
| 235 | */ |
| 236 | static void fmap_setup_cache(int unused) |
| 237 | { |
| 238 | struct region_device fmrd; |
| 239 | |
| 240 | if (find_fmap_directory(&fmrd)) |
| 241 | return; |
| 242 | |
| 243 | /* Reloads the FMAP even on ACPI S3 resume */ |
| 244 | const size_t s = region_device_sz(&fmrd); |
| 245 | struct fmap *fmap = cbmem_add(CBMEM_ID_FMAP, s); |
| 246 | if (!fmap) { |
| 247 | printk(BIOS_ERR, "ERROR: Failed to allocate CBMEM\n"); |
| 248 | return; |
| 249 | } |
| 250 | |
| 251 | const ssize_t ret = rdev_readat(&fmrd, fmap, 0, s); |
| 252 | if (ret != s) { |
| 253 | printk(BIOS_ERR, "ERROR: Failed to read FMAP into CBMEM\n"); |
| 254 | cbmem_entry_remove(cbmem_entry_find(CBMEM_ID_FMAP)); |
| 255 | return; |
| 256 | } |
| 257 | |
| 258 | /* Finally advertise the cache for the current stage */ |
| 259 | fmap_register_cache(unused); |
| 260 | } |
| 261 | |
| 262 | ROMSTAGE_CBMEM_INIT_HOOK(fmap_setup_cache) |
| 263 | RAMSTAGE_CBMEM_INIT_HOOK(fmap_register_cache) |
| 264 | POSTCAR_CBMEM_INIT_HOOK(fmap_register_cache) |