summaryrefslogtreecommitdiff
path: root/src/vendorcode/google/chromeos/vboot_loader.c
blob: d13608c50e1e6ea8402f57f87595c8bb63bcdee2 (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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/*
 * This file is part of the coreboot project.
 *
 * Copyright (C) 2013 Google, Inc.
 *
 * 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 <arch/stages.h>
#include <stdint.h>
#include <stddef.h>
#include <string.h>
#include <cbfs.h>
#include <cbmem.h>
#include <console/console.h>
#include <console/vtxprintf.h>
#include <program_loading.h>
#include <tpm.h>
#include <reset.h>
#include <romstage_handoff.h>
#include <rmodule.h>
#include <string.h>
#include <stdlib.h>
#include <timestamp.h>
#include "chromeos.h"
#include "vboot_context.h"
#include "vboot_handoff.h"

#define TEMP_CBMEM_ID_VBOOT	0xffffffff
#define TEMP_CBMEM_ID_VBLOCKS	0xfffffffe

static void vboot_run_stub(struct vboot_context *context)
{
	struct rmod_stage_load rmod_stage = {
		.cbmem_id = TEMP_CBMEM_ID_VBOOT,
		.name = CONFIG_CBFS_PREFIX "/vboot",
	};
	void (*entry)(struct vboot_context *context);

	if (rmodule_stage_load_from_cbfs(&rmod_stage)) {
		printk(BIOS_DEBUG, "Could not load vboot stub.\n");
		goto out;
	}

	entry = rmod_stage.entry;

	/* Call stub. */
	entry(context);

out:
	/* Tear down the region no longer needed. */
	if (rmod_stage.cbmem_entry != NULL)
		cbmem_entry_remove(rmod_stage.cbmem_entry);
}

/* Helper routines for the vboot stub. */
static void log_msg(const char *fmt, va_list args)
{
	do_vtxprintf(fmt, args);
}

static void fatal_error(void)
{
	printk(BIOS_ERR, "vboot encountered fatal error. Resetting.\n");
	hard_reset();
}

static int fw_region_size(struct vboot_region *r)
{
	struct vboot_components *fw_info;
	int32_t size;
	int i;

	fw_info = vboot_locate_components(r);
	if (fw_info == NULL)
		return -1;

	if (fw_info->num_components > MAX_PARSED_FW_COMPONENTS)
		return -1;

	size = sizeof(*fw_info);
	size += sizeof(struct vboot_component_entry) * fw_info->num_components;

	for (i = 0; i < fw_info->num_components; i++)
		size += ALIGN(fw_info->entries[i].size, sizeof(uint32_t));

	/* Check that size of comopnents does not exceed the region's size. */
	if (size > r->size)
		return -1;

	/* Update region with the correct size. */
	r->size = size;

	return 0;
}

static int vboot_fill_params(struct vboot_context *ctx)
{
	VbCommonParams *cparams;
	VbSelectFirmwareParams *fparams;

	if (fw_region_size(&ctx->fw_a))
		return -1;

	if (fw_region_size(&ctx->fw_b))
		return -1;

	cparams = ctx->cparams;
	fparams = ctx->fparams;

	cparams->gbb_size = ctx->gbb.size;
	fparams->verification_size_A = ctx->vblock_a.size;
	fparams->verification_size_B = ctx->vblock_b.size;

	if (IS_ENABLED(CONFIG_SPI_FLASH_MEMORY_MAPPED)) {
		/* Get memory-mapped pointers to the regions. */
		cparams->gbb_data = vboot_get_region(ctx->gbb.offset_addr,
		                                     ctx->gbb.size, NULL);
		fparams->verification_block_A =
			vboot_get_region(ctx->vblock_a.offset_addr,
			                 ctx->vblock_a.size, NULL);
		fparams->verification_block_B =
			vboot_get_region(ctx->vblock_b.offset_addr,
			                 ctx->vblock_b.size, NULL);
	} else {
		/*
		 * Copy the vblock info into a buffer in cbmem. The gbb will
		 * be read using VbExRegionRead().
		 */
		char *dest;
		size_t vblck_sz;

		vblck_sz = ctx->vblock_a.size + ctx->vblock_b.size;
		ctx->vblocks = cbmem_entry_add(TEMP_CBMEM_ID_VBLOCKS, vblck_sz);
		if (ctx->vblocks == NULL)
			return -1;
		dest = cbmem_entry_start(ctx->vblocks);
		if (vboot_get_region(ctx->vblock_a.offset_addr,
		                     ctx->vblock_a.size, dest) == NULL)
			return -1;
		fparams->verification_block_A = (void *)dest;
		dest += ctx->vblock_a.size;
		if (vboot_get_region(ctx->vblock_b.offset_addr,
		                     ctx->vblock_b.size, dest) == NULL)
			return -1;
		fparams->verification_block_B = (void *)dest;
	}

	return 0;
}

static void fill_handoff(struct vboot_context *context)
{
	struct vboot_components *fw_info;
	struct vboot_region *region;
	int i;

	/* Fix up the handoff structure. */
	context->handoff->selected_firmware =
		context->fparams->selected_firmware;

	/* Parse out the components for downstream consumption. */
	if (context->handoff->selected_firmware == VB_SELECT_FIRMWARE_A)
		region = &context->fw_a;
	else if  (context->handoff->selected_firmware == VB_SELECT_FIRMWARE_B)
		region = &context->fw_b;
	else
		return;

	fw_info = vboot_locate_components(region);
	if (fw_info == NULL)
		return;

	for (i = 0; i < fw_info->num_components; i++) {
		context->handoff->components[i].address =
			region->offset_addr + fw_info->entries[i].offset;
		context->handoff->components[i].size = fw_info->entries[i].size;
	}
}

static void vboot_clean_up(struct vboot_context *context)
{
	if (context->vblocks != NULL)
		cbmem_entry_remove(context->vblocks);
}

static void reset(void)
{
	hard_reset();
}

static void vboot_invoke_wrapper(struct vboot_handoff *vboot_handoff)
{
	VbCommonParams cparams;
	VbSelectFirmwareParams fparams;
	struct vboot_context context;
	uint32_t *iflags;

	vboot_handoff->selected_firmware = VB_SELECT_FIRMWARE_READONLY;

	memset(&cparams, 0, sizeof(cparams));
	memset(&fparams, 0, sizeof(fparams));
	memset(&context, 0, sizeof(context));

	iflags = &vboot_handoff->init_params.flags;
	if (get_developer_mode_switch())
		*iflags |= VB_INIT_FLAG_DEV_SWITCH_ON;
	if (get_recovery_mode_switch()) {
		clear_recovery_mode_switch();
		*iflags |= VB_INIT_FLAG_REC_BUTTON_PRESSED;
	}
	if (get_write_protect_state())
		*iflags |= VB_INIT_FLAG_WP_ENABLED;
	if (vboot_get_sw_write_protect())
		*iflags |= VB_INIT_FLAG_SW_WP_ENABLED;
	if (CONFIG_VIRTUAL_DEV_SWITCH)
		*iflags |= VB_INIT_FLAG_VIRTUAL_DEV_SWITCH;
	if (CONFIG_EC_SOFTWARE_SYNC) {
		*iflags |= VB_INIT_FLAG_EC_SOFTWARE_SYNC;
		*iflags |= VB_INIT_FLAG_VIRTUAL_REC_SWITCH;
	}
	if (CONFIG_VBOOT_EC_SLOW_UPDATE)
		*iflags |= VB_INIT_FLAG_EC_SLOW_UPDATE;
	if (CONFIG_VBOOT_OPROM_MATTERS) {
		*iflags |= VB_INIT_FLAG_OPROM_MATTERS;
		/* Will load VGA option rom during this boot */
		if (developer_mode_enabled() || recovery_mode_enabled() ||
		    vboot_wants_oprom()) {
			*iflags |= VB_INIT_FLAG_OPROM_LOADED;
		}
	}

	context.handoff = vboot_handoff;
	context.cparams = &cparams;
	context.fparams = &fparams;

	cparams.shared_data_blob = &vboot_handoff->shared_data[0];
	cparams.shared_data_size = VB_SHARED_DATA_MIN_SIZE;
	cparams.caller_context = &context;

	vboot_locate_region("GBB", &context.gbb);
	vboot_locate_region("VBLOCK_A", &context.vblock_a);
	vboot_locate_region("VBLOCK_B", &context.vblock_b);
	vboot_locate_region("FW_MAIN_A", &context.fw_a);
	vboot_locate_region("FW_MAIN_B", &context.fw_b);

	/* Check all fmap entries. */
	if (context.fw_a.size < 0 || context.fw_b.size < 0 ||
	    context.vblock_a.size < 0 || context.vblock_b.size < 0 ||
	    context.gbb.size < 0) {
		printk(BIOS_DEBUG, "Not all fmap entries found for vboot.\n");
		return;
	}

	/* Fill in vboot parameters. */
	if (vboot_fill_params(&context)) {
		vboot_clean_up(&context);
		return;
	}

	/* Initialize callbacks. */
	context.read_vbnv = &read_vbnv;
	context.save_vbnv = &save_vbnv;
	context.tis_init = &tis_init;
	context.tis_open = &tis_open;
	context.tis_close = &tis_close;
	context.tis_sendrecv = &tis_sendrecv;
	context.log_msg = &log_msg;
	context.fatal_error = &fatal_error;
	context.get_region = &vboot_get_region;
	context.reset = &reset;

	vboot_run_stub(&context);

	fill_handoff(&context);

	vboot_clean_up(&context);
}

#if CONFIG_RELOCATABLE_RAMSTAGE
static void *vboot_load_ramstage(uint32_t cbmem_id, const char *name,
				const struct cbmem_entry **cbmem_entry)
{
	struct vboot_handoff *vboot_handoff;
	struct cbfs_stage *stage;
	const struct firmware_component *fwc;
	struct rmod_stage_load rmod_load = {
		.cbmem_id = cbmem_id,
		.name = name,
	};

	timestamp_add_now(TS_START_VBOOT);

	vboot_handoff = cbmem_add(CBMEM_ID_VBOOT_HANDOFF,
	                          sizeof(*vboot_handoff));

	if (vboot_handoff == NULL) {
		printk(BIOS_DEBUG, "Could not add vboot_handoff structure.\n");
		return NULL;
	}

	memset(vboot_handoff, 0, sizeof(*vboot_handoff));

	vboot_invoke_wrapper(vboot_handoff);

	timestamp_add_now(TS_END_VBOOT);

	/* Take RO firmware path since no RW area was selected. */
	if (vboot_handoff->selected_firmware != VB_SELECT_FIRMWARE_A &&
	    vboot_handoff->selected_firmware != VB_SELECT_FIRMWARE_B) {
		printk(BIOS_DEBUG, "No RW firmware selected: 0x%08x\n",
		       vboot_handoff->selected_firmware);
		return NULL;
	}

	if (CONFIG_VBOOT_RAMSTAGE_INDEX >= MAX_PARSED_FW_COMPONENTS) {
		printk(BIOS_ERR, "Invalid ramstage index: %d\n",
		       CONFIG_VBOOT_RAMSTAGE_INDEX);
		return NULL;
	}

	/* Check for invalid address. */
	fwc = &vboot_handoff->components[CONFIG_VBOOT_RAMSTAGE_INDEX];
	if (fwc->address == 0) {
		printk(BIOS_DEBUG, "RW ramstage image address invalid.\n");
		return NULL;
	}

	printk(BIOS_DEBUG, "RW ramstage image at 0x%08x, 0x%08x bytes.\n",
	       fwc->address, fwc->size);

	stage = (void *)fwc->address;

	if (rmodule_stage_load(&rmod_load, stage)) {
		vboot_handoff->selected_firmware = VB_SELECT_FIRMWARE_READONLY;
		printk(BIOS_DEBUG, "Could not load ramstage region.\n");
		return NULL;
	}

	*cbmem_entry = rmod_load.cbmem_entry;

	return rmod_load.entry;
}
#else /* CONFIG_RELOCATABLE_RAMSTAGE */
static void vboot_load_ramstage(struct vboot_handoff *vboot_handoff,
                                struct romstage_handoff *handoff)
{
	struct cbfs_stage *stage;
	const struct firmware_component *fwc;

	if (CONFIG_VBOOT_RAMSTAGE_INDEX >= MAX_PARSED_FW_COMPONENTS) {
		printk(BIOS_ERR, "Invalid ramstage index: %d\n",
		       CONFIG_VBOOT_RAMSTAGE_INDEX);
		return;
	}

	/* Check for invalid address. */
	fwc = &vboot_handoff->components[CONFIG_VBOOT_RAMSTAGE_INDEX];
	if (fwc->address == 0) {
		printk(BIOS_DEBUG, "RW ramstage image address invalid.\n");
		return;
	}

	printk(BIOS_DEBUG, "RW ramstage image at 0x%08x, 0x%08x bytes.\n",
	       fwc->address, fwc->size);

	/* This will leak a mapping. */
	stage = vboot_get_region(fwc->address, fwc->size, NULL);

	if (stage == NULL) {
		printk(BIOS_DEBUG, "Unable to get RW ramstage region.\n");
		return;
	}

	timestamp_add_now(TS_START_COPYRAM);

	/* Stages rely the below clearing so that the bss is initialized. */
	memset((void *) (uintptr_t) stage->load, 0, stage->memlen);

	if (cbfs_decompress(stage->compression,
			     ((unsigned char *) stage) +
			     sizeof(struct cbfs_stage),
			     (void *) (uintptr_t) stage->load,
			     stage->len))
		return;

	timestamp_add_now(TS_END_COPYRAM);

#if CONFIG_ARCH_X86
	__asm__ volatile (
		"movl $0, %%ebp\n"
		"jmp  *%%edi\n"
		:: "D"(stage->entry)
	);
#elif CONFIG_ARCH_ARM
	stage_exit((void *)(uintptr_t)stage->entry);
#endif
}
#endif /* CONFIG_RELOCATABLE_RAMSTAGE */


const struct ramstage_loader_ops vboot_ramstage_loader = {
	.name = "VBOOT",
	.load = vboot_load_ramstage,
};