summaryrefslogtreecommitdiff
path: root/src/vendorcode/google/chromeos/fmap.c
blob: 7c256ce6678f84b817e09fc751f42a9bd530571b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
 * This file is part of the coreboot project.
 *
 * Copyright (C) 2012 The ChromiumOS Authors.  All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include <stdint.h>
#include <stddef.h>
#include <string.h>
#include <console/console.h>
#include <cbfs.h>
#include "fmap.h"

/* Find FMAP data structure in ROM.
 * See http://code.google.com/p/flashmap/ for more information on FMAP.
 */
const struct fmap *fmap_find(void)
{
	/* FIXME: Get rid of the hard codes. The "easy" way would be to
	 * do a binary search, but since ROM accesses are slow, we don't
	 * want to spend a lot of time looking for the FMAP. An elegant
	 * solution would be to store a pointer to the FMAP in the CBFS
	 * master header; that would require some more changes to cbfstool
	 * and possibly cros_bundle_firmware.
	 */

#if CONFIG_ARCH_X86
	/* wrapping around 0x100000000 */
	const struct fmap *fmap = (void *)
		(CONFIG_FLASHMAP_OFFSET - CONFIG_ROM_SIZE);
#else
	struct cbfs_media default_media, *media;
	media = &default_media;
	init_default_cbfs_media(media);
	media->open(media);
	const struct fmap *fmap = (void *)
		media->map(media, CONFIG_FLASHMAP_OFFSET, 4096); // FIXME size
	media->close(media);
#endif

	if (fmap == CBFS_MEDIA_INVALID_MAP_ADDRESS ||
	    memcmp(fmap, FMAP_SIGNATURE, sizeof(FMAP_SIGNATURE) - 1)) {
		printk(BIOS_DEBUG, "No FMAP found at %p.\n", fmap);
		return NULL;
	}

	printk(BIOS_DEBUG, "FMAP: Found \"%s\" version %d.%d at %p.\n",
	       fmap->name, fmap->ver_major, fmap->ver_minor, fmap);
	printk(BIOS_DEBUG, "FMAP: base = %llx size = %x #areas = %d\n",
	       (unsigned long long)fmap->base, fmap->size, fmap->nareas);

	return fmap;
}

const struct fmap_area *find_fmap_area(const struct fmap *fmap,
							const char name[])
{
	const struct fmap_area *area = NULL;

	if (fmap) {
		int i;
		for (i = 0; i < fmap->nareas; i++) {
			if (!strcmp((const char *)fmap->areas[i].name, name)) {
				area = &fmap->areas[i];
				break;
			}
		}
	}

	if (area) {
		printk(BIOS_DEBUG, "FMAP: area %s found\n", name);
		printk(BIOS_DEBUG, "FMAP:   offset: %x\n", area->offset);
		printk(BIOS_DEBUG, "FMAP:   size:   %d bytes\n", area->size);
	} else {
		printk(BIOS_DEBUG, "FMAP: area %s not found\n", name);
	}

	return area;
}

int find_fmap_entry(const char name[], void **pointer)
{
	MAYBE_STATIC const struct fmap *fmap = NULL;
	const struct fmap_area *area;
	void *base = NULL;

	if (!fmap)
		fmap = fmap_find();

	area = find_fmap_area(fmap, name);

	if (!area)
		return -1;

	/* Right now cros_bundle_firmware does not write a valid
	 * base address into the FMAP. Hence, if base is 0, assume
	 * 4GB-8MB as base address.
	 */
	if (fmap->base) {
		base = (void *)(unsigned long)fmap->base;
		printk(BIOS_DEBUG, "FMAP: %s base at %p\n", name, base);
	} else {
#if CONFIG_ARCH_X86
		base = (void *)(0 - CONFIG_ROM_SIZE);
		printk(BIOS_WARNING, "FMAP: No valid base address, using"
				" 0x%p\n", base);
#endif
	}

	*pointer = (void*) ((uintptr_t)base + area->offset);
	printk(BIOS_DEBUG, "FMAP: %s at %p (offset %x)\n",
	       name, *pointer, area->offset);
	return area->size;
}