summaryrefslogtreecommitdiff
path: root/src/drivers/vpd/vpd.c
blob: c332a6e94c3ee54ff5e9b41420be6411bc964b47 (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/* SPDX-License-Identifier: BSD-3-Clause */

#include <assert.h>
#include <console/console.h>
#include <cbmem.h>
#include <fmap.h>
#include <program_loading.h>
#include <string.h>
#include <timestamp.h>

#include "vpd.h"
#include "vpd_decode.h"
#include "vpd_tables.h"

/* Currently we only support Google VPD 2.0, which has a fixed offset. */
enum {
	CROSVPD_CBMEM_MAGIC = 0x43524f53,
	CROSVPD_CBMEM_VERSION = 0x0001,
};

struct vpd_cbmem {
	uint32_t magic;
	uint32_t version;
	uint32_t ro_size;
	uint32_t rw_size;
	uint8_t blob[0];
	/* The blob contains both RO and RW data. It starts with RO (0 ..
	 * ro_size) and then RW (ro_size .. ro_size+rw_size).
	 */
};

struct vpd_gets_arg {
	const uint8_t *key;
	const uint8_t *value;
	int32_t key_len, value_len;
	int matched;
};

static struct region_device ro_vpd, rw_vpd;

/*
 * Initializes a region_device to represent the requested VPD 2.0 formatted
 * region on flash. On errors rdev->size will be set to 0.
 */
static void init_vpd_rdev(const char *fmap_name, struct region_device *rdev)
{
	struct google_vpd_info info;
	int32_t size;

	if (fmap_locate_area_as_rdev(fmap_name, rdev)) {
		printk(BIOS_ERR, "%s: No %s FMAP section.\n", __func__,
			fmap_name);
		goto fail;
	}

	size = region_device_sz(rdev);

	if ((size < GOOGLE_VPD_2_0_OFFSET + sizeof(info)) ||
	    rdev_chain(rdev, rdev, GOOGLE_VPD_2_0_OFFSET,
			size - GOOGLE_VPD_2_0_OFFSET)) {
		printk(BIOS_ERR, "%s: Too small (%d) for Google VPD 2.0.\n",
		       __func__, size);
		goto fail;
	}

	/* Try if we can find a google_vpd_info, otherwise read whole VPD. */
	if (rdev_readat(rdev, &info, 0, sizeof(info)) != sizeof(info)) {
		printk(BIOS_ERR, "ERROR: Failed to read %s header.\n",
		       fmap_name);
		goto fail;
	}

	if (memcmp(info.header.magic, VPD_INFO_MAGIC, sizeof(info.header.magic))
	    == 0) {
		if (rdev_chain(rdev, rdev, sizeof(info), info.size)) {
			printk(BIOS_ERR, "ERROR: %s info size too large.\n",
			       fmap_name);
			goto fail;
		}
	} else if (info.header.tlv.type == VPD_TYPE_TERMINATOR ||
		   info.header.tlv.type == VPD_TYPE_IMPLICIT_TERMINATOR) {
		printk(BIOS_WARNING, "WARNING: %s is uninitialized or empty.\n",
		       fmap_name);
		goto fail;
	}

	return;

fail:
	memset(rdev, 0, sizeof(*rdev));
}

static int init_vpd_rdevs_from_cbmem(void)
{
	if (!cbmem_possibly_online())
		return -1;

	struct vpd_cbmem *cbmem = cbmem_find(CBMEM_ID_VPD);
	if (!cbmem)
		return -1;

	rdev_chain(&ro_vpd, &addrspace_32bit.rdev,
		   (uintptr_t)cbmem->blob, cbmem->ro_size);
	rdev_chain(&rw_vpd, &addrspace_32bit.rdev,
		   (uintptr_t)cbmem->blob + cbmem->ro_size, cbmem->rw_size);

	return 0;
}

static void init_vpd_rdevs(void)
{
	static bool done = false;

	if (done)
		return;

	if (init_vpd_rdevs_from_cbmem() != 0) {
		init_vpd_rdev("RO_VPD", &ro_vpd);
		init_vpd_rdev("RW_VPD", &rw_vpd);
	}

	done = true;
}

static void cbmem_add_cros_vpd(int is_recovery)
{
	struct vpd_cbmem *cbmem;

	timestamp_add_now(TS_START_COPYVPD);

	init_vpd_rdevs();

	/* Return if no VPD at all */
	if (region_device_sz(&ro_vpd) == 0 && region_device_sz(&rw_vpd) == 0)
		return;

	size_t ro_size = region_device_sz(&ro_vpd);
	size_t rw_size = region_device_sz(&rw_vpd);

	cbmem = cbmem_add(CBMEM_ID_VPD, sizeof(*cbmem) + ro_size + rw_size);
	if (!cbmem) {
		printk(BIOS_ERR, "%s: Failed to allocate CBMEM (%zu+%zu).\n",
			__func__, ro_size, rw_size);
		return;
	}

	cbmem->magic = CROSVPD_CBMEM_MAGIC;
	cbmem->version = CROSVPD_CBMEM_VERSION;
	cbmem->ro_size = ro_size;
	cbmem->rw_size = rw_size;

	if (ro_size) {
		if (rdev_readat(&ro_vpd, cbmem->blob, 0, ro_size) != ro_size) {
			printk(BIOS_ERR, "ERROR: Couldn't read RO VPD\n");
			cbmem->ro_size = ro_size = 0;
		}
		timestamp_add_now(TS_END_COPYVPD_RO);
	}

	if (rw_size) {
		if (rdev_readat(&rw_vpd, cbmem->blob + ro_size, 0, rw_size)
								 != rw_size) {
			printk(BIOS_ERR, "ERROR: Couldn't read RW VPD\n");
			cbmem->rw_size = rw_size = 0;
		}
		timestamp_add_now(TS_END_COPYVPD_RW);
	}

	init_vpd_rdevs_from_cbmem();
}

static int vpd_gets_callback(const uint8_t *key, uint32_t key_len,
			     const uint8_t *value, uint32_t value_len,
			     void *arg)
{
	struct vpd_gets_arg *result = (struct vpd_gets_arg *)arg;
	if (key_len != result->key_len ||
	    memcmp(key, result->key, key_len) != 0)
		/* Returns VPD_DECODE_OK to continue parsing. */
		return VPD_DECODE_OK;

	result->matched = 1;
	result->value = value;
	result->value_len = value_len;
	/* Returns VPD_DECODE_FAIL to stop parsing. */
	return VPD_DECODE_FAIL;
}

static void vpd_find_in(struct region_device *rdev, struct vpd_gets_arg *arg)
{
	if (region_device_sz(rdev) == 0)
		return;

	uint32_t consumed = 0;
	void *mapping = rdev_mmap_full(rdev);
	while (vpd_decode_string(region_device_sz(rdev), mapping,
		&consumed, vpd_gets_callback, arg) == VPD_DECODE_OK) {
	/* Iterate until found or no more entries. */
	}
	rdev_munmap(rdev, mapping);
}

const void *vpd_find(const char *key, int *size, enum vpd_region region)
{
	struct vpd_gets_arg arg = {0};

	arg.key = (const uint8_t *)key;
	arg.key_len = strlen(key);

	init_vpd_rdevs();

	if (region == VPD_RW_THEN_RO)
		vpd_find_in(&rw_vpd, &arg);

	if (!arg.matched && (region == VPD_RO || region == VPD_RO_THEN_RW ||
			region == VPD_RW_THEN_RO))
		vpd_find_in(&ro_vpd, &arg);

	if (!arg.matched && (region == VPD_RW || region == VPD_RO_THEN_RW))
		vpd_find_in(&rw_vpd, &arg);

	if (!arg.matched)
		return NULL;

	*size = arg.value_len;
	return arg.value;
}

char *vpd_gets(const char *key, char *buffer, int size, enum vpd_region region)
{
	const void *string_address;
	int string_size;

	string_address = vpd_find(key, &string_size, region);

	if (!string_address)
		return NULL;

	assert(size > 0);
	int copy_size = MIN(size - 1, string_size);
	memcpy(buffer, string_address, copy_size);
	buffer[copy_size] = '\0';
	return buffer;
}

/*
 * Find value of boolean type vpd key.
 *
 * During the process, necessary checking is done, such as making
 * sure the value length is 1, and value is either '1' or '0'.
 */
bool vpd_get_bool(const char *key, enum vpd_region region, uint8_t *val)
{
	int size;
	const char *value;

	value = vpd_find(key, &size, region);
	if (!value) {
		printk(BIOS_CRIT, "problem returning from vpd_find.\n");
		return false;
	}

	if (size != 1)
		return false;

	/* Make sure the value is either '1' or '0' */
	if (*value == '1') {
		*val = 1;
		return true;
	} else if (*value == '0') {
		*val = 0;
		return true;
	} else
		return false;
}

ROMSTAGE_CBMEM_INIT_HOOK(cbmem_add_cros_vpd)