summaryrefslogtreecommitdiff
path: root/src/security/vboot/common.c
blob: aeb44988397f302754bb799b6d7980a9b8ce1b6a (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
/*
 * This file is part of the coreboot project.
 *
 * Copyright (C) 2014 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.
 */

#include <assert.h>
#include <cbmem.h>
#include <console/console.h>
#include <fmap.h>
#include <stdint.h>
#include <symbols.h>
#include <vb2_api.h>
#include <security/vboot/misc.h>
#include <security/vboot/symbols.h>
#include <security/vboot/vboot_common.h>

static struct vb2_context *vboot_ctx;

static void *vboot_get_workbuf(void)
{
	void *wb = NULL;

	if (cbmem_possibly_online())
		wb = cbmem_find(CBMEM_ID_VBOOT_WORKBUF);

	if (wb == NULL && CONFIG(VBOOT_STARTS_IN_BOOTBLOCK) &&
	    preram_symbols_available())
		wb = _vboot2_work;

	assert(wb != NULL);

	return wb;
}

struct vb2_context *vboot_get_context(void)
{
	void *wb;

	/* Return if context has already been initialized/restored. */
	if (vboot_ctx)
		return vboot_ctx;

	wb = vboot_get_workbuf();

	/* Restore context from a previous stage. */
	if (vboot_logic_executed()) {
		assert(vb2api_reinit(wb, &vboot_ctx) == VB2_SUCCESS);
		return vboot_ctx;
	}

	assert(verification_should_run());

	/* Initialize vb2_shared_data and friends. */
	assert(vb2api_init(wb, VB2_FIRMWARE_WORKBUF_RECOMMENDED_SIZE,
			   &vboot_ctx) == VB2_SUCCESS);

	return vboot_ctx;
}

int vboot_locate_firmware(struct vb2_context *ctx, struct region_device *fw)
{
	const char *name;

	if (vboot_is_firmware_slot_a(ctx))
		name = "FW_MAIN_A";
	else
		name = "FW_MAIN_B";

	int ret = fmap_locate_area_as_rdev(name, fw);
	if (ret)
		return ret;

	/* Truncate area to the size that was actually signed by vboot. */
	return rdev_chain(fw, fw, 0, vb2api_get_firmware_size(ctx));
}

static void vboot_setup_cbmem(int unused)
{
	const size_t cbmem_size = VB2_KERNEL_WORKBUF_RECOMMENDED_SIZE;
	void *wb_cbmem = cbmem_add(CBMEM_ID_VBOOT_WORKBUF, cbmem_size);
	assert(wb_cbmem != NULL);
	/*
	 * For platforms where VBOOT_STARTS_IN_BOOTBLOCK, vboot verification
	 * occurs before CBMEM is brought online, using pre-RAM. In order to
	 * make vboot data structures available downstream, copy vboot workbuf
	 * from SRAM/CAR into CBMEM.
	 */
	if (CONFIG(VBOOT_STARTS_IN_BOOTBLOCK))
		assert(vb2api_relocate(wb_cbmem, _vboot2_work, cbmem_size,
				       &vboot_ctx) == VB2_SUCCESS);
}
ROMSTAGE_CBMEM_INIT_HOOK(vboot_setup_cbmem)