1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #include <boot_device.h>
5 #include <console/console.h>
7 #include <metadata_hash.h>
13 #include "fmap_config.h"
16 * See http://code.google.com/p/flashmap/ for more information on FMAP.
19 static int fmap_print_once
;
20 static struct region_device fmap_cache
;
22 #define print_once(...) do { \
23 if (!fmap_print_once) \
24 printk(__VA_ARGS__); \
27 uint64_t get_fmap_flash_offset(void)
32 static int verify_fmap(const struct fmap
*fmap
)
34 if (memcmp(fmap
->signature
, FMAP_SIGNATURE
, sizeof(fmap
->signature
)))
37 static bool done
= false;
38 if (!CONFIG(CBFS_VERIFICATION
) || !ENV_INITIAL_STAGE
|| done
)
39 return 0; /* Only need to check hash in first stage. */
41 if (metadata_hash_verify_fmap(fmap
, FMAP_SIZE
) != VB2_SUCCESS
)
48 static void report(const struct fmap
*fmap
)
50 print_once(BIOS_DEBUG
, "FMAP: Found \"%s\" version %d.%d at %#x.\n",
51 fmap
->name
, fmap
->ver_major
, fmap
->ver_minor
, FMAP_OFFSET
);
52 print_once(BIOS_DEBUG
, "FMAP: base = %#llx size = %#x #areas = %d\n",
53 (long long)le64toh(fmap
->base
), le32toh(fmap
->size
),
54 le16toh(fmap
->nareas
));
58 static void setup_preram_cache(struct region_device
*cache_rdev
)
60 if (CONFIG(NO_FMAP_CACHE
))
63 /* No need to use FMAP cache in SMM */
67 if (!ENV_ROMSTAGE_OR_BEFORE
) {
68 /* We get here if ramstage makes an FMAP access before calling
69 cbmem_initialize(). We should avoid letting it come to that,
70 so print a warning. */
71 print_once(BIOS_WARNING
,
72 "WARNING: Post-RAM FMAP access too early for cache!\n");
76 struct fmap
*fmap
= (struct fmap
*)_fmap_cache
;
77 if (!(ENV_INITIAL_STAGE
)) {
78 /* NOTE: This assumes that the first stage will make
79 at least one FMAP access (usually from finding CBFS). */
80 if (!verify_fmap(fmap
))
83 printk(BIOS_ERR
, "FMAP cache corrupted?!\n");
84 if (CONFIG(TOCTOU_SAFETY
))
85 die("TOCTOU safety relies on FMAP cache");
88 /* In case we fail below, make sure the cache is invalid. */
89 memset(fmap
->signature
, 0, sizeof(fmap
->signature
));
92 const struct region_device
*boot_rdev
= boot_device_ro();
96 /* memlayout statically guarantees that the FMAP_CACHE is big enough. */
97 if (rdev_readat(boot_rdev
, fmap
, FMAP_OFFSET
, FMAP_SIZE
) != FMAP_SIZE
)
99 if (verify_fmap(fmap
))
104 rdev_chain_mem(cache_rdev
, fmap
, FMAP_SIZE
);
107 static int find_fmap_directory(struct region_device
*fmrd
)
109 const struct region_device
*boot
;
111 size_t offset
= FMAP_OFFSET
;
113 /* Try FMAP cache first */
114 if (!region_device_sz(&fmap_cache
))
115 setup_preram_cache(&fmap_cache
);
116 if (region_device_sz(&fmap_cache
))
117 return rdev_chain_full(fmrd
, &fmap_cache
);
120 boot
= boot_device_ro();
125 fmap
= rdev_mmap(boot
, offset
, sizeof(struct fmap
));
130 if (verify_fmap(fmap
)) {
131 printk(BIOS_ERR
, "FMAP missing or corrupted at offset 0x%zx!\n",
133 rdev_munmap(boot
, fmap
);
139 rdev_munmap(boot
, fmap
);
141 return rdev_chain(fmrd
, boot
, offset
, FMAP_SIZE
);
144 int fmap_locate_area_as_rdev(const char *name
, struct region_device
*area
)
148 if (fmap_locate_area(name
, &ar
))
151 return boot_device_ro_subregion(&ar
, area
);
154 int fmap_locate_area_as_rdev_rw(const char *name
, struct region_device
*area
)
158 if (fmap_locate_area(name
, &ar
))
161 return boot_device_rw_subregion(&ar
, area
);
164 int fmap_locate_area(const char *name
, struct region
*ar
)
166 struct region_device fmrd
;
169 if (name
== NULL
|| ar
== NULL
)
172 if (find_fmap_directory(&fmrd
))
175 /* Start reading the areas just after fmap header. */
176 offset
= sizeof(struct fmap
);
179 struct fmap_area
*area
;
181 area
= rdev_mmap(&fmrd
, offset
, sizeof(*area
));
186 if (strcmp((const char *)area
->name
, name
)) {
187 rdev_munmap(&fmrd
, area
);
188 offset
+= sizeof(struct fmap_area
);
192 printk(BIOS_DEBUG
, "FMAP: area %s found @ %x (%d bytes)\n",
193 name
, le32toh(area
->offset
), le32toh(area
->size
));
195 ar
->offset
= le32toh(area
->offset
);
196 ar
->size
= le32toh(area
->size
);
198 rdev_munmap(&fmrd
, area
);
203 printk(BIOS_DEBUG
, "FMAP: area %s not found\n", name
);
208 int fmap_find_region_name(const struct region
* const ar
,
209 char name
[FMAP_STRLEN
])
211 struct region_device fmrd
;
214 if (name
== NULL
|| ar
== NULL
)
217 if (find_fmap_directory(&fmrd
))
220 /* Start reading the areas just after fmap header. */
221 offset
= sizeof(struct fmap
);
224 struct fmap_area
*area
;
226 area
= rdev_mmap(&fmrd
, offset
, sizeof(*area
));
231 if ((ar
->offset
!= le32toh(area
->offset
)) ||
232 (ar
->size
!= le32toh(area
->size
))) {
233 rdev_munmap(&fmrd
, area
);
234 offset
+= sizeof(struct fmap_area
);
238 printk(BIOS_DEBUG
, "FMAP: area (%zx, %zx) found, named %s\n",
239 ar
->offset
, ar
->size
, area
->name
);
241 memcpy(name
, area
->name
, FMAP_STRLEN
);
243 rdev_munmap(&fmrd
, area
);
248 printk(BIOS_DEBUG
, "FMAP: area (%zx, %zx) not found\n",
249 ar
->offset
, ar
->size
);
254 ssize_t
fmap_read_area(const char *name
, void *buffer
, size_t size
)
256 struct region_device rdev
;
257 if (fmap_locate_area_as_rdev(name
, &rdev
))
259 return rdev_readat(&rdev
, buffer
, 0,
260 MIN(size
, region_device_sz(&rdev
)));
263 ssize_t
fmap_overwrite_area(const char *name
, const void *buffer
, size_t size
)
265 struct region_device rdev
;
267 if (fmap_locate_area_as_rdev_rw(name
, &rdev
))
269 if (size
> region_device_sz(&rdev
))
271 if (rdev_eraseat(&rdev
, 0, region_device_sz(&rdev
)) < 0)
273 return rdev_writeat(&rdev
, buffer
, 0, size
);
276 static void fmap_register_cbmem_cache(void)
278 const struct cbmem_entry
*e
;
280 /* Find the FMAP cache installed by previous stage */
281 e
= cbmem_entry_find(CBMEM_ID_FMAP
);
282 /* Don't set fmap_cache so that find_fmap_directory will use regular path */
286 rdev_chain_mem(&fmap_cache
, cbmem_entry_start(e
), cbmem_entry_size(e
));
290 * The main reason to copy the FMAP into CBMEM is to make it available to the
291 * OS on every architecture. As side effect use the CBMEM copy as cache.
293 static void fmap_add_cbmem_cache(void)
295 struct region_device fmrd
;
297 if (find_fmap_directory(&fmrd
))
300 /* Reloads the FMAP even on ACPI S3 resume */
301 const size_t s
= region_device_sz(&fmrd
);
302 struct fmap
*fmap
= cbmem_add(CBMEM_ID_FMAP
, s
);
304 printk(BIOS_ERR
, "Failed to allocate CBMEM\n");
308 const ssize_t ret
= rdev_readat(&fmrd
, fmap
, 0, s
);
310 printk(BIOS_ERR
, "Failed to read FMAP into CBMEM\n");
311 cbmem_entry_remove(cbmem_entry_find(CBMEM_ID_FMAP
));
316 static void fmap_setup_cbmem_cache(int unused
)
318 if (ENV_CREATES_CBMEM
)
319 fmap_add_cbmem_cache();
321 /* Finally advertise the cache for the current stage */
322 fmap_register_cbmem_cache();
325 CBMEM_READY_HOOK(fmap_setup_cbmem_cache
);