blob: 2fa017d91b552aa36fc24cacfa919cde79286f39 [file] [log] [blame]
Julius Werner54fc81c2019-11-14 11:42:53 -08001/* Copyright (c) 2019 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 * Functions for initializing the vboot workbuffer and vb2_context.
6 */
7
8#include "2api.h"
9#include "2common.h"
10#include "2misc.h"
11#include "2sysincludes.h"
12
13void vb2_workbuf_from_ctx(struct vb2_context *ctx, struct vb2_workbuf *wb)
14{
15 struct vb2_shared_data *sd = vb2_get_sd(ctx);
16 vb2_workbuf_init(wb, (void *)sd + sd->workbuf_used,
17 sd->workbuf_size - sd->workbuf_used);
18}
19
20void vb2_set_workbuf_used(struct vb2_context *ctx, uint32_t used)
21{
22 struct vb2_shared_data *sd = vb2_get_sd(ctx);
23 sd->workbuf_used = vb2_wb_round_up(used);
24}
25
26vb2_error_t vb2api_init(void *workbuf, uint32_t size,
27 struct vb2_context **ctxptr)
28{
29 struct vb2_shared_data *sd = workbuf;
30 *ctxptr = NULL;
31
32 if (!vb2_aligned(workbuf, VB2_WORKBUF_ALIGN))
33 return VB2_ERROR_WORKBUF_ALIGN;
34
35 if (size < vb2_wb_round_up(sizeof(*sd)))
36 return VB2_ERROR_WORKBUF_SMALL;
37
38 /* Zero out vb2_shared_data (which includes vb2_context). */
39 memset(sd, 0, sizeof(*sd));
40
41 /* Initialize shared data. */
42 sd->magic = VB2_SHARED_DATA_MAGIC;
43 sd->struct_version_major = VB2_SHARED_DATA_VERSION_MAJOR;
44 sd->struct_version_minor = VB2_SHARED_DATA_VERSION_MINOR;
45 sd->workbuf_size = size;
46 sd->workbuf_used = vb2_wb_round_up(sizeof(*sd));
47
48 *ctxptr = &sd->ctx;
49 return VB2_SUCCESS;
50}
51
52#pragma GCC diagnostic push
53/* Don't warn for the version_minor check even if the checked version is 0. */
54#pragma GCC diagnostic ignored "-Wtype-limits"
Yu-Ping Wu16e6fe32019-11-26 11:04:07 +080055vb2_error_t vb2api_relocate(void *new_workbuf, const void *cur_workbuf,
56 uint32_t size, struct vb2_context **ctxptr)
Julius Werner54fc81c2019-11-14 11:42:53 -080057{
Yu-Ping Wu16e6fe32019-11-26 11:04:07 +080058 const struct vb2_shared_data *cur_sd = cur_workbuf;
59 struct vb2_shared_data *new_sd;
Julius Werner54fc81c2019-11-14 11:42:53 -080060
61 if (!vb2_aligned(new_workbuf, VB2_WORKBUF_ALIGN))
62 return VB2_ERROR_WORKBUF_ALIGN;
63
64 /* Check magic and version. */
Yu-Ping Wu16e6fe32019-11-26 11:04:07 +080065 if (cur_sd->magic != VB2_SHARED_DATA_MAGIC)
Julius Werner54fc81c2019-11-14 11:42:53 -080066 return VB2_ERROR_SHARED_DATA_MAGIC;
67
Yu-Ping Wu16e6fe32019-11-26 11:04:07 +080068 if (cur_sd->struct_version_major != VB2_SHARED_DATA_VERSION_MAJOR ||
69 cur_sd->struct_version_minor < VB2_SHARED_DATA_VERSION_MINOR)
Julius Werner54fc81c2019-11-14 11:42:53 -080070 return VB2_ERROR_SHARED_DATA_VERSION;
71
72 /* Check workbuf integrity. */
Yu-Ping Wu16e6fe32019-11-26 11:04:07 +080073 if (cur_sd->workbuf_used < vb2_wb_round_up(sizeof(*cur_sd)))
Julius Werner54fc81c2019-11-14 11:42:53 -080074 return VB2_ERROR_WORKBUF_INVALID;
75
Yu-Ping Wu16e6fe32019-11-26 11:04:07 +080076 if (cur_sd->workbuf_size < cur_sd->workbuf_used)
Julius Werner54fc81c2019-11-14 11:42:53 -080077 return VB2_ERROR_WORKBUF_INVALID;
78
Yu-Ping Wu16e6fe32019-11-26 11:04:07 +080079 if (cur_sd->workbuf_used > size)
Julius Werner54fc81c2019-11-14 11:42:53 -080080 return VB2_ERROR_WORKBUF_SMALL;
81
82 /* Relocate if necessary. */
83 if (cur_workbuf != new_workbuf)
Yu-Ping Wu16e6fe32019-11-26 11:04:07 +080084 memmove(new_workbuf, cur_workbuf, cur_sd->workbuf_used);
Julius Werner54fc81c2019-11-14 11:42:53 -080085
86 /* Set the new size, and return the context pointer. */
Yu-Ping Wu16e6fe32019-11-26 11:04:07 +080087 new_sd = new_workbuf;
88 new_sd->workbuf_size = size;
89 *ctxptr = &new_sd->ctx;
Julius Werner54fc81c2019-11-14 11:42:53 -080090
91 return VB2_SUCCESS;
92}
93#pragma GCC diagnostic pop
94
95vb2_error_t vb2api_reinit(void *workbuf, struct vb2_context **ctxptr)
96{
97 /* Blindly retrieve workbuf_size. vb2api_relocate() will
98 perform workbuf validation checks. */
99 struct vb2_shared_data *sd = workbuf;
100 return vb2api_relocate(workbuf, workbuf, sd->workbuf_size, ctxptr);
101}