summaryrefslogtreecommitdiff
path: root/lib/chromeos/fdt_decode.c
blob: 5c93dcdf8955689b119d650de6bbf9ea9b77ac92 (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
 * Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 *
 * Alternatively, this software may be distributed under the terms of the
 * GNU General Public License ("GPL") version 2 as published by the Free
 * Software Foundation.
 */

#include <common.h>
#include <libfdt.h>
#include <chromeos/common.h>
#include <chromeos/fdt_decode.h>
#include <chromeos/fmap.h>
#include <fdt_decode.h>
#include <linux/string.h>

#define PREFIX "chromeos/fdt_decode: "

static int relpath_offset(const void *blob, int offset, const char *in_path)
{
	const char *path = in_path;
	const char *sep;

	/* skip leading '/' */
	while (*path == '/')
		path++;

	for (sep = path; *sep; path = sep + 1) {
		/* this is equivalent to strchrnul() */
		sep = strchr(path, '/');
		if (!sep)
			sep = path + strlen(path);

		offset = fdt_subnode_offset_namelen(blob, offset, path,
				sep - path);
		if (offset < 0) {
			VBDEBUG(PREFIX "Node '%s' is missing\n", in_path);
			return offset;
		}
	}

	return offset;
}

static int decode_fmap_entry(const void *blob, int offset, const char *base,
		const char *name, struct fmap_entry *entry)
{
	char path[50];
	int length;
	uint32_t *property;

	/* Form the node to look up as <base>-<name> */
	assert(strlen(base) + strlen(name) + 1 < sizeof(path));
	strcpy(path, base);
	strcat(path, "-");
	strcat(path, name);

	offset = relpath_offset(blob, offset, path);
	if (offset < 0)
		return offset;
	property = (uint32_t *)fdt_getprop(blob, offset, "reg", &length);
	if (!property) {
		VBDEBUG(PREFIX "Node '%s' is missing property '%s'\n",
			path, "reg");
		return -FDT_ERR_MISSING;
	}
	entry->offset = fdt32_to_cpu(property[0]);
	entry->length = fdt32_to_cpu(property[1]);

	return 0;
}

static int decode_block_lba(const void *blob, int offset, const char *path,
		uint64_t *out)
{
	int length;
	uint32_t *property;

	offset = relpath_offset(blob, offset, path);
	if (offset < 0)
		return offset;

	property = (uint32_t *)fdt_getprop(blob, offset, "block-lba", &length);
	if (!property) {
		VBDEBUG(PREFIX "failed to load LBA '%s/block-lba'\n", path);
		return -FDT_ERR_MISSING;
	}
	*out = fdt32_to_cpu(*property);
	return 0;
}

int decode_firmware_entry(const char *blob, int fmap_offset, const char *name,
		struct fmap_firmware_entry *entry)
{
	int err;

	err = decode_fmap_entry(blob, fmap_offset, name, "boot", &entry->boot);
	err |= decode_fmap_entry(blob, fmap_offset, name, "vblock",
			&entry->vblock);
	err |= decode_fmap_entry(blob, fmap_offset, name, "firmware-id",
			&entry->firmware_id);
	err |= decode_block_lba(blob, fmap_offset, name, &entry->block_lba);
	return err;
}

int fdt_decode_twostop_fmap(const void *blob, struct twostop_fmap *config)
{
	int fmap_offset;
	int err;

	fmap_offset = fdt_node_offset_by_compatible(blob, -1,
			"chromeos,flashmap");
	if (fmap_offset < 0) {
		VBDEBUG(PREFIX "chromeos,flashmap node is missing\n");
		return fmap_offset;
	}
	err = decode_firmware_entry(blob, fmap_offset, "rw-a",
			&config->readwrite_a);
	err |= decode_firmware_entry(blob, fmap_offset, "rw-b",
			&config->readwrite_b);

	err |= decode_fmap_entry(blob, fmap_offset, "ro", "fmap",
			&config->readonly.fmap); \
	err |= decode_fmap_entry(blob, fmap_offset, "ro", "gbb",
			&config->readonly.gbb); \
	err |= decode_fmap_entry(blob, fmap_offset, "ro", "firmware-id",
			&config->readonly.firmware_id);

	return 0;
}

int fdt_decode_chromeos_config_has_prop(const void *blob, const char *name)
{
	int nodeoffset = fdt_path_offset(blob, "/chromeos-config");
	int len;

	if (nodeoffset < 0)
		return 0;

	return fdt_get_property(blob, nodeoffset, name, &len) != NULL;
}

void *fdt_decode_chromeos_alloc_region(const void *blob,
		const char *prop_name, size_t *size)
{
	int node = fdt_path_offset(blob, "/chromeos-config");

	if (node < 0)
		return NULL;

	return fdt_decode_alloc_region(blob, node, prop_name, size);
}