mb/google/brya/var/omnigul: Modify NVMe and UFS Storage support
[coreboot.git] / payloads / libpayload / libc / fmap.c
blob2ee9e3eeaa72377343af3530ee7d84fa8aeff8aa
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);
82 /***********************************************************************************************
83 * LEGACY CODE *
84 **********************************************************************************************/
86 int fmap_region_by_name(const uint32_t fmap_offset, const char * const name,
87 uint32_t * const offset, uint32_t * const size)
89 int i;
91 struct fmap *fmap;
92 struct fmap fmap_head;
93 struct cbfs_media default_media;
94 struct cbfs_media *media = &default_media;
96 if (init_default_cbfs_media(media) != 0)
97 return -1;
99 media->open(media);
101 if (!media->read(media, &fmap_head, fmap_offset, sizeof(fmap_head)))
102 return -1;
104 if (memcmp(fmap_head.signature, FMAP_SIGNATURE, sizeof(fmap_head.signature))) {
105 return -1;
108 int fmap_size = sizeof(*fmap) +
109 fmap_head.nareas * sizeof(struct fmap_area);
111 fmap = malloc(fmap_size);
112 if (!fmap)
113 return -1;
115 if (!media->read(media, fmap, fmap_offset, fmap_size))
116 goto err;
118 media->close(media);
120 for (i = 0; i < fmap->nareas; i++) {
121 if (strcmp((const char *)fmap->areas[i].name, name) != 0)
122 continue;
123 if (offset)
124 *offset = fmap->areas[i].offset;
125 if (size)
126 *size = fmap->areas[i].size;
127 free(fmap);
128 return 0;
130 err:
131 free(fmap);
132 return -1;