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