soc/intel/xeon_sp/util: Enhance lock_pam0123
[coreboot2.git] / payloads / libpayload / libc / fmap.c
blob87a531a6a7a5948ede54ba83da655d4d1ee3bb23
1 /*
3 * Copyright (C) 2015 Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
29 #include <libpayload-config.h>
30 #include <libpayload.h>
31 #include <commonlib/bsd/fmap_serialized.h>
32 #include <coreboot_tables.h>
33 #include <cbfs.h>
34 #include <boot_device.h>
35 #include <stdint.h>
36 #include <arch/virtual.h>
38 /* Private fmap cache. */
39 static struct fmap *_fmap_cache;
41 static enum cb_err fmap_find_area(struct fmap *fmap, const char *name, size_t *offset,
42 size_t *size)
44 for (size_t i = 0; i < le32toh(fmap->nareas); ++i) {
45 if (strncmp((const char *)fmap->areas[i].name, name, FMAP_STRLEN) != 0)
46 continue;
47 if (offset)
48 *offset = le32toh(fmap->areas[i].offset);
49 if (size)
50 *size = le32toh(fmap->areas[i].size);
51 return CB_SUCCESS;
54 return CB_ERR;
57 static bool fmap_is_signature_valid(struct fmap *fmap)
59 return memcmp(fmap->signature, FMAP_SIGNATURE, sizeof(fmap->signature)) == 0;
62 static bool fmap_setup_cache(void)
64 /* Use FMAP cache if available */
65 if (lib_sysinfo.fmap_cache
66 && fmap_is_signature_valid((struct fmap *)phys_to_virt(lib_sysinfo.fmap_cache))) {
67 _fmap_cache = (struct fmap *)phys_to_virt(lib_sysinfo.fmap_cache);
68 return true;
71 return false;
74 enum cb_err fmap_locate_area(const char *name, size_t *offset, size_t *size)
76 if (!_fmap_cache && !fmap_setup_cache())
77 return CB_ERR;
79 return fmap_find_area(_fmap_cache, name, offset, size);