blob: bf99037f5431c575194f9f87512a1eb1c8228c82 [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
83int fmap_locate_area(const char *name, struct region *ar)
84{
85 struct region_device fmrd;
86 size_t offset;
87
88 if (find_fmap_directory(&fmrd))
89 return -1;
90
91 /* Start reading the areas just after fmap header. */
92 offset = sizeof(struct fmap);
93
94 while (1) {
95 struct fmap_area *area;
96
97 area = rdev_mmap(&fmrd, offset, sizeof(*area));
98
99 if (area == NULL)
100 return -1;
101
102 if (strcmp((const char *)area->name, name)) {
103 rdev_munmap(&fmrd, area);
104 offset += sizeof(struct fmap_area);
105 continue;
106 }
107
Duncan Lauriebc2c0a32016-02-09 09:17:56 -0800108 printk(BIOS_DEBUG, "FMAP: area %s found @ %x (%d bytes)\n",
109 name, area->offset, area->size);
Aaron Durbin0424c952015-03-28 23:56:22 -0500110
111 ar->offset = area->offset;
112 ar->size = area->size;
113
114 rdev_munmap(&fmrd, area);
115
116 return 0;
117 }
118
119 printk(BIOS_DEBUG, "FMAP: area %s not found\n", name);
120
121 return -1;
122}
Patrick Georgi99526902015-07-09 11:27:44 +0200123
124int fmap_find_region_name(const struct region * const ar,
125 char name[FMAP_STRLEN])
126{
127 struct region_device fmrd;
128 size_t offset;
129
130 if (find_fmap_directory(&fmrd))
131 return -1;
132
133 /* Start reading the areas just after fmap header. */
134 offset = sizeof(struct fmap);
135
136 while (1) {
137 struct fmap_area *area;
138
139 area = rdev_mmap(&fmrd, offset, sizeof(*area));
140
141 if (area == NULL)
142 return -1;
143
144 if ((ar->offset != area->offset) ||
145 (ar->size != area->size)) {
146 rdev_munmap(&fmrd, area);
147 offset += sizeof(struct fmap_area);
148 continue;
149 }
150
151 printk(BIOS_DEBUG, "FMAP: area (%zx, %zx) found, named %s\n",
152 ar->offset, ar->size, area->name);
153
154 memcpy(name, area->name, FMAP_STRLEN);
155
156 rdev_munmap(&fmrd, area);
157
158 return 0;
159 }
160
161 printk(BIOS_DEBUG, "FMAP: area (%zx, %zx) not found\n",
162 ar->offset, ar->size);
163
164 return -1;
165}