2misc.c revision 224f5ac761852cd9ffe56438f6807732bd9ee445
1/* Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
2 * Use of this source code is governed by a BSD-style license that can be
3 * found in the LICENSE file.
4 *
5 * Misc functions which need access to vb2_context but are not public APIs
6 */
7
8#include "2sysincludes.h"
9#include "2api.h"
10#include "2common.h"
11#include "2misc.h"
12#include "2nvstorage.h"
13#include "2secdata.h"
14#include "2sha.h"
15#include "2rsa.h"
16
17int vb2_init_context(struct vb2_context *ctx)
18{
19	struct vb2_shared_data *sd = vb2_get_sd(ctx);
20
21	/* Don't do anything if the context has already been initialized */
22	if (ctx->workbuf_used)
23		return VB2_SUCCESS;
24
25	/*
26	 * Workbuf had better be big enough for our shared data struct and
27	 * aligned.  Not much we can do if it isn't; we'll die before we can
28	 * store a recovery reason.
29	 */
30	if (ctx->workbuf_size < sizeof(*sd))
31		return VB2_ERROR_INITCTX_WORKBUF_SMALL;
32	if (!vb_aligned(ctx->workbuf, sizeof(uint32_t)))
33		return VB2_ERROR_INITCTX_WORKBUF_ALIGN;
34
35	/* Initialize the shared data at the start of the work buffer */
36	memset(sd, 0, sizeof(*sd));
37	ctx->workbuf_used = sizeof(*sd);
38	return VB2_SUCCESS;
39}
40