blob: 9602134d9421bb5f40eb2086600973608d2284ac [file] [log] [blame]
Aaron Durbin0424c952015-03-28 23:56:22 -05001/*
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 Durbin0424c952015-03-28 23:56:22 -050014 */
15
Duncan Lauriebc2c0a32016-02-09 09:17:56 -080016#include <arch/early_variables.h>
Aaron Durbin0424c952015-03-28 23:56:22 -050017#include <boot_device.h>
18#include <console/console.h>
19#include <fmap.h>
Aaron Durbindc9f5cd2015-09-08 13:34:43 -050020#include <commonlib/fmap_serialized.h>
Aaron Durbin0424c952015-03-28 23:56:22 -050021#include <stddef.h>
22#include <string.h>
23
Aaron Durbinbf1e4812016-05-10 15:12:08 -050024#include "fmap_config.h"
25
Aaron Durbin0424c952015-03-28 23:56:22 -050026/*
27 * See http://code.google.com/p/flashmap/ for more information on FMAP.
28 */
29
Duncan Lauriebc2c0a32016-02-09 09:17:56 -080030static int fmap_print_once CAR_GLOBAL;
31
Patrick Georgi977587a2015-07-09 13:34:40 +020032int find_fmap_directory(struct region_device *fmrd)
Aaron Durbin0424c952015-03-28 23:56:22 -050033{
34 const struct region_device *boot;
35 struct fmap *fmap;
36 size_t fmap_size;
Aaron Durbinbf1e4812016-05-10 15:12:08 -050037 size_t offset = FMAP_OFFSET;
Aaron Durbin0424c952015-03-28 23:56:22 -050038
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 Lauriebc2c0a32016-02-09 09:17:56 -080058 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 Durbin0424c952015-03-28 23:56:22 -050065
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
73int 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 Durbinbccaab82016-08-12 12:42:04 -050083int 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 Durbin0424c952015-03-28 23:56:22 -050093int 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 Lauriebc2c0a32016-02-09 09:17:56 -0800118 printk(BIOS_DEBUG, "FMAP: area %s found @ %x (%d bytes)\n",
119 name, area->offset, area->size);
Aaron Durbin0424c952015-03-28 23:56:22 -0500120
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 Georgi99526902015-07-09 11:27:44 +0200133
134int 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}