summaryrefslogtreecommitdiff
path: root/src/drivers/vpd/vpd_cbmem.c
blob: a0bb161287079bb8b1a0c997a1760f24d4246313 (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
/*
 * Copyright (c) 2014 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.
 */

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

#include "vpd_tables.h"
#include "vpd.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).
	 */
};

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

	timestamp_add_now(TS_START_COPYVPD);

	blob = vpd_load_blob();

	/* Return if no VPD at all */
	if (blob->ro_size == 0 && blob->rw_size == 0)
		return;

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

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

	if (blob->ro_size) {
		memcpy(cbmem->blob, blob->ro_base, blob->ro_size);
		timestamp_add_now(TS_END_COPYVPD_RO);
	}

	if (blob->rw_size) {
		memcpy(cbmem->blob + blob->ro_size, blob->rw_base,
			blob->rw_size);
		timestamp_add_now(TS_END_COPYVPD_RW);
	}
}

void vpd_get_buffers(struct vpd_blob *blob)
{
	const struct vpd_cbmem *vpd;

	vpd = cbmem_find(CBMEM_ID_VPD);
	if (!vpd || !vpd->ro_size)
		return;

	blob->ro_base = (void *)vpd->blob;
	blob->ro_size = vpd->ro_size;
	blob->rw_base = (void *)vpd->blob + vpd->ro_size;
	blob->rw_size = vpd->rw_size;
}

RAMSTAGE_CBMEM_INIT_HOOK(cbmem_add_cros_vpd)