xref: /aosp_15_r20/external/vboot_reference/firmware/2lib/2context.c (revision 8617a60d3594060b7ecbd21bc622a7c14f3cf2bc)
1 /* Copyright 2019 The ChromiumOS Authors
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 
vb2_workbuf_from_ctx(struct vb2_context * ctx,struct vb2_workbuf * wb)13 void 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 
vb2_set_workbuf_used(struct vb2_context * ctx,uint32_t used)20 void 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 
vb2api_init(void * workbuf,uint32_t size,struct vb2_context ** ctxptr)26 vb2_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"
vb2api_relocate(void * new_workbuf,const void * cur_workbuf,uint32_t size,struct vb2_context ** ctxptr)55 vb2_error_t vb2api_relocate(void *new_workbuf, const void *cur_workbuf,
56 			    uint32_t size, struct vb2_context **ctxptr)
57 {
58 	const struct vb2_shared_data *cur_sd = cur_workbuf;
59 	struct vb2_shared_data *new_sd;
60 
61 	if (!vb2_aligned(new_workbuf, VB2_WORKBUF_ALIGN))
62 		return VB2_ERROR_WORKBUF_ALIGN;
63 
64 	/* Check magic and version. */
65 	if (cur_sd->magic != VB2_SHARED_DATA_MAGIC)
66 		return VB2_ERROR_SHARED_DATA_MAGIC;
67 
68 	if (cur_sd->struct_version_major != VB2_SHARED_DATA_VERSION_MAJOR ||
69 	    cur_sd->struct_version_minor < VB2_SHARED_DATA_VERSION_MINOR)
70 		return VB2_ERROR_SHARED_DATA_VERSION;
71 
72 	/* Check workbuf integrity. */
73 	if (cur_sd->workbuf_used < vb2_wb_round_up(sizeof(*cur_sd)))
74 		return VB2_ERROR_WORKBUF_INVALID;
75 
76 	if (cur_sd->workbuf_size < cur_sd->workbuf_used)
77 		return VB2_ERROR_WORKBUF_INVALID;
78 
79 	if (cur_sd->workbuf_used > size)
80 		return VB2_ERROR_WORKBUF_SMALL;
81 
82 	/* Relocate if necessary. */
83 	if (cur_workbuf != new_workbuf)
84 		memmove(new_workbuf, cur_workbuf, cur_sd->workbuf_used);
85 
86 	/* Set the new size, and return the context pointer. */
87 	new_sd = new_workbuf;
88 	new_sd->workbuf_size = size;
89 	*ctxptr = &new_sd->ctx;
90 
91 	return VB2_SUCCESS;
92 }
93 #pragma GCC diagnostic pop
94 
vb2api_reinit(void * workbuf,struct vb2_context ** ctxptr)95 vb2_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 }
102