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> |
| 23 | |
Aaron Durbin | bf1e481 | 2016-05-10 15:12:08 -0500 | [diff] [blame] | 24 | #include "fmap_config.h" |
| 25 | |
Aaron Durbin | 0424c95 | 2015-03-28 23:56:22 -0500 | [diff] [blame] | 26 | /* |
| 27 | * See http://code.google.com/p/flashmap/ for more information on FMAP. |
| 28 | */ |
| 29 | |
Duncan Laurie | bc2c0a3 | 2016-02-09 09:17:56 -0800 | [diff] [blame] | 30 | static int fmap_print_once CAR_GLOBAL; |
| 31 | |
Patrick Georgi | 977587a | 2015-07-09 13:34:40 +0200 | [diff] [blame] | 32 | int find_fmap_directory(struct region_device *fmrd) |
Aaron Durbin | 0424c95 | 2015-03-28 23:56:22 -0500 | [diff] [blame] | 33 | { |
| 34 | const struct region_device *boot; |
| 35 | struct fmap *fmap; |
| 36 | size_t fmap_size; |
Aaron Durbin | bf1e481 | 2016-05-10 15:12:08 -0500 | [diff] [blame] | 37 | size_t offset = FMAP_OFFSET; |
Aaron Durbin | 0424c95 | 2015-03-28 23:56:22 -0500 | [diff] [blame] | 38 | |
| 39 | boot_device_init(); |
| 40 | boot = boot_device_ro(); |
| 41 | |
| 42 | if (boot == NULL) |
| 43 | return -1; |
| 44 | |
| 45 | fmap_size = sizeof(struct fmap); |
| 46 | |
| 47 | fmap = rdev_mmap(boot, offset, fmap_size); |
| 48 | |
| 49 | if (fmap == NULL) |
| 50 | return -1; |
| 51 | |
| 52 | if (memcmp(fmap->signature, FMAP_SIGNATURE, sizeof(fmap->signature))) { |
| 53 | printk(BIOS_DEBUG, "No FMAP found at %zx offset.\n", offset); |
| 54 | rdev_munmap(boot, fmap); |
| 55 | return -1; |
| 56 | } |
| 57 | |
Duncan Laurie | bc2c0a3 | 2016-02-09 09:17:56 -0800 | [diff] [blame] | 58 | if (!car_get_var(fmap_print_once)) { |
| 59 | printk(BIOS_DEBUG, "FMAP: Found \"%s\" version %d.%d at %zx.\n", |
| 60 | fmap->name, fmap->ver_major, fmap->ver_minor, offset); |
| 61 | printk(BIOS_DEBUG, "FMAP: base = %llx size = %x #areas = %d\n", |
| 62 | (long long)fmap->base, fmap->size, fmap->nareas); |
| 63 | car_set_var(fmap_print_once, 1); |
| 64 | } |
Aaron Durbin | 0424c95 | 2015-03-28 23:56:22 -0500 | [diff] [blame] | 65 | |
| 66 | fmap_size += fmap->nareas * sizeof(struct fmap_area); |
| 67 | |
| 68 | rdev_munmap(boot, fmap); |
| 69 | |
| 70 | return rdev_chain(fmrd, boot, offset, fmap_size); |
| 71 | } |
| 72 | |
| 73 | int fmap_locate_area_as_rdev(const char *name, struct region_device *area) |
| 74 | { |
| 75 | struct region ar; |
| 76 | |
| 77 | if (fmap_locate_area(name, &ar)) |
| 78 | return -1; |
| 79 | |
| 80 | return boot_device_ro_subregion(&ar, area); |
| 81 | } |
| 82 | |
Aaron Durbin | bccaab8 | 2016-08-12 12:42:04 -0500 | [diff] [blame] | 83 | int fmap_locate_area_as_rdev_rw(const char *name, struct region_device *area) |
| 84 | { |
| 85 | struct region ar; |
| 86 | |
| 87 | if (fmap_locate_area(name, &ar)) |
| 88 | return -1; |
| 89 | |
| 90 | return boot_device_rw_subregion(&ar, area); |
| 91 | } |
| 92 | |
Aaron Durbin | 0424c95 | 2015-03-28 23:56:22 -0500 | [diff] [blame] | 93 | int fmap_locate_area(const char *name, struct region *ar) |
| 94 | { |
| 95 | struct region_device fmrd; |
| 96 | size_t offset; |
| 97 | |
| 98 | if (find_fmap_directory(&fmrd)) |
| 99 | return -1; |
| 100 | |
| 101 | /* Start reading the areas just after fmap header. */ |
| 102 | offset = sizeof(struct fmap); |
| 103 | |
| 104 | while (1) { |
| 105 | struct fmap_area *area; |
| 106 | |
| 107 | area = rdev_mmap(&fmrd, offset, sizeof(*area)); |
| 108 | |
| 109 | if (area == NULL) |
| 110 | return -1; |
| 111 | |
| 112 | if (strcmp((const char *)area->name, name)) { |
| 113 | rdev_munmap(&fmrd, area); |
| 114 | offset += sizeof(struct fmap_area); |
| 115 | continue; |
| 116 | } |
| 117 | |
Duncan Laurie | bc2c0a3 | 2016-02-09 09:17:56 -0800 | [diff] [blame] | 118 | printk(BIOS_DEBUG, "FMAP: area %s found @ %x (%d bytes)\n", |
| 119 | name, area->offset, area->size); |
Aaron Durbin | 0424c95 | 2015-03-28 23:56:22 -0500 | [diff] [blame] | 120 | |
| 121 | ar->offset = area->offset; |
| 122 | ar->size = area->size; |
| 123 | |
| 124 | rdev_munmap(&fmrd, area); |
| 125 | |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | printk(BIOS_DEBUG, "FMAP: area %s not found\n", name); |
| 130 | |
| 131 | return -1; |
| 132 | } |
Patrick Georgi | 9952690 | 2015-07-09 11:27:44 +0200 | [diff] [blame] | 133 | |
| 134 | int fmap_find_region_name(const struct region * const ar, |
| 135 | char name[FMAP_STRLEN]) |
| 136 | { |
| 137 | struct region_device fmrd; |
| 138 | size_t offset; |
| 139 | |
| 140 | if (find_fmap_directory(&fmrd)) |
| 141 | return -1; |
| 142 | |
| 143 | /* Start reading the areas just after fmap header. */ |
| 144 | offset = sizeof(struct fmap); |
| 145 | |
| 146 | while (1) { |
| 147 | struct fmap_area *area; |
| 148 | |
| 149 | area = rdev_mmap(&fmrd, offset, sizeof(*area)); |
| 150 | |
| 151 | if (area == NULL) |
| 152 | return -1; |
| 153 | |
| 154 | if ((ar->offset != area->offset) || |
| 155 | (ar->size != area->size)) { |
| 156 | rdev_munmap(&fmrd, area); |
| 157 | offset += sizeof(struct fmap_area); |
| 158 | continue; |
| 159 | } |
| 160 | |
| 161 | printk(BIOS_DEBUG, "FMAP: area (%zx, %zx) found, named %s\n", |
| 162 | ar->offset, ar->size, area->name); |
| 163 | |
| 164 | memcpy(name, area->name, FMAP_STRLEN); |
| 165 | |
| 166 | rdev_munmap(&fmrd, area); |
| 167 | |
| 168 | return 0; |
| 169 | } |
| 170 | |
| 171 | printk(BIOS_DEBUG, "FMAP: area (%zx, %zx) not found\n", |
| 172 | ar->offset, ar->size); |
| 173 | |
| 174 | return -1; |
| 175 | } |