diff options
-rw-r--r-- | Makefile | 1 | ||||
-rw-r--r-- | firmware/2lib/2context.c | 100 | ||||
-rw-r--r-- | firmware/2lib/2misc.c | 89 |
3 files changed, 101 insertions, 89 deletions
@@ -351,6 +351,7 @@ FWLIB2X_SRCS = \ firmware/2lib/2api.c \ firmware/2lib/2auxfw_sync.c \ firmware/2lib/2common.c \ + firmware/2lib/2context.c \ firmware/2lib/2crc8.c \ firmware/2lib/2ec_sync.c \ firmware/2lib/2gbb.c \ diff --git a/firmware/2lib/2context.c b/firmware/2lib/2context.c new file mode 100644 index 00000000..1ee5a3cc --- /dev/null +++ b/firmware/2lib/2context.c @@ -0,0 +1,100 @@ +/* Copyright (c) 2019 The Chromium OS Authors. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + * + * Functions for initializing the vboot workbuffer and vb2_context. + */ + +#include "2api.h" +#include "2common.h" +#include "2misc.h" +#include "2sysincludes.h" + +void vb2_workbuf_from_ctx(struct vb2_context *ctx, struct vb2_workbuf *wb) +{ + struct vb2_shared_data *sd = vb2_get_sd(ctx); + vb2_workbuf_init(wb, (void *)sd + sd->workbuf_used, + sd->workbuf_size - sd->workbuf_used); +} + +void vb2_set_workbuf_used(struct vb2_context *ctx, uint32_t used) +{ + struct vb2_shared_data *sd = vb2_get_sd(ctx); + sd->workbuf_used = vb2_wb_round_up(used); +} + +vb2_error_t vb2api_init(void *workbuf, uint32_t size, + struct vb2_context **ctxptr) +{ + struct vb2_shared_data *sd = workbuf; + *ctxptr = NULL; + + if (!vb2_aligned(workbuf, VB2_WORKBUF_ALIGN)) + return VB2_ERROR_WORKBUF_ALIGN; + + if (size < vb2_wb_round_up(sizeof(*sd))) + return VB2_ERROR_WORKBUF_SMALL; + + /* Zero out vb2_shared_data (which includes vb2_context). */ + memset(sd, 0, sizeof(*sd)); + + /* Initialize shared data. */ + sd->magic = VB2_SHARED_DATA_MAGIC; + sd->struct_version_major = VB2_SHARED_DATA_VERSION_MAJOR; + sd->struct_version_minor = VB2_SHARED_DATA_VERSION_MINOR; + sd->workbuf_size = size; + sd->workbuf_used = vb2_wb_round_up(sizeof(*sd)); + + *ctxptr = &sd->ctx; + return VB2_SUCCESS; +} + +#pragma GCC diagnostic push +/* Don't warn for the version_minor check even if the checked version is 0. */ +#pragma GCC diagnostic ignored "-Wtype-limits" +vb2_error_t vb2api_relocate(void *new_workbuf, void *cur_workbuf, uint32_t size, + struct vb2_context **ctxptr) +{ + struct vb2_shared_data *sd = cur_workbuf; + + if (!vb2_aligned(new_workbuf, VB2_WORKBUF_ALIGN)) + return VB2_ERROR_WORKBUF_ALIGN; + + /* Check magic and version. */ + if (sd->magic != VB2_SHARED_DATA_MAGIC) + return VB2_ERROR_SHARED_DATA_MAGIC; + + if (sd->struct_version_major != VB2_SHARED_DATA_VERSION_MAJOR || + sd->struct_version_minor < VB2_SHARED_DATA_VERSION_MINOR) + return VB2_ERROR_SHARED_DATA_VERSION; + + /* Check workbuf integrity. */ + if (sd->workbuf_used < vb2_wb_round_up(sizeof(*sd))) + return VB2_ERROR_WORKBUF_INVALID; + + if (sd->workbuf_size < sd->workbuf_used) + return VB2_ERROR_WORKBUF_INVALID; + + if (sd->workbuf_used > size) + return VB2_ERROR_WORKBUF_SMALL; + + /* Relocate if necessary. */ + if (cur_workbuf != new_workbuf) + memmove(new_workbuf, cur_workbuf, sd->workbuf_used); + + /* Set the new size, and return the context pointer. */ + sd = new_workbuf; + sd->workbuf_size = size; + *ctxptr = &sd->ctx; + + return VB2_SUCCESS; +} +#pragma GCC diagnostic pop + +vb2_error_t vb2api_reinit(void *workbuf, struct vb2_context **ctxptr) +{ + /* Blindly retrieve workbuf_size. vb2api_relocate() will + perform workbuf validation checks. */ + struct vb2_shared_data *sd = workbuf; + return vb2api_relocate(workbuf, workbuf, sd->workbuf_size, ctxptr); +} diff --git a/firmware/2lib/2misc.c b/firmware/2lib/2misc.c index deb4f885..3775dcee 100644 --- a/firmware/2lib/2misc.c +++ b/firmware/2lib/2misc.c @@ -33,19 +33,6 @@ struct vb2_gbb_header *vb2_get_gbb(struct vb2_context *ctx) return (struct vb2_gbb_header *)((void *)sd + sd->gbb_offset); } -void vb2_workbuf_from_ctx(struct vb2_context *ctx, struct vb2_workbuf *wb) -{ - struct vb2_shared_data *sd = vb2_get_sd(ctx); - vb2_workbuf_init(wb, (void *)sd + sd->workbuf_used, - sd->workbuf_size - sd->workbuf_used); -} - -void vb2_set_workbuf_used(struct vb2_context *ctx, uint32_t used) -{ - struct vb2_shared_data *sd = vb2_get_sd(ctx); - sd->workbuf_used = vb2_wb_round_up(used); -} - vb2_error_t vb2_read_gbb_header(struct vb2_context *ctx, struct vb2_gbb_header *gbb) { @@ -128,82 +115,6 @@ void vb2api_fail(struct vb2_context *ctx, uint8_t reason, uint8_t subcode) } } -vb2_error_t vb2api_init(void *workbuf, uint32_t size, - struct vb2_context **ctxptr) -{ - struct vb2_shared_data *sd = workbuf; - *ctxptr = NULL; - - if (!vb2_aligned(workbuf, VB2_WORKBUF_ALIGN)) - return VB2_ERROR_WORKBUF_ALIGN; - - if (size < vb2_wb_round_up(sizeof(*sd))) - return VB2_ERROR_WORKBUF_SMALL; - - /* Zero out vb2_shared_data (which includes vb2_context). */ - memset(sd, 0, sizeof(*sd)); - - /* Initialize shared data. */ - sd->magic = VB2_SHARED_DATA_MAGIC; - sd->struct_version_major = VB2_SHARED_DATA_VERSION_MAJOR; - sd->struct_version_minor = VB2_SHARED_DATA_VERSION_MINOR; - sd->workbuf_size = size; - sd->workbuf_used = vb2_wb_round_up(sizeof(*sd)); - - *ctxptr = &sd->ctx; - return VB2_SUCCESS; -} - -#pragma GCC diagnostic push -/* Don't warn for the version_minor check even if the checked version is 0. */ -#pragma GCC diagnostic ignored "-Wtype-limits" -vb2_error_t vb2api_relocate(void *new_workbuf, void *cur_workbuf, uint32_t size, - struct vb2_context **ctxptr) -{ - struct vb2_shared_data *sd = cur_workbuf; - - if (!vb2_aligned(new_workbuf, VB2_WORKBUF_ALIGN)) - return VB2_ERROR_WORKBUF_ALIGN; - - /* Check magic and version. */ - if (sd->magic != VB2_SHARED_DATA_MAGIC) - return VB2_ERROR_SHARED_DATA_MAGIC; - - if (sd->struct_version_major != VB2_SHARED_DATA_VERSION_MAJOR || - sd->struct_version_minor < VB2_SHARED_DATA_VERSION_MINOR) - return VB2_ERROR_SHARED_DATA_VERSION; - - /* Check workbuf integrity. */ - if (sd->workbuf_used < vb2_wb_round_up(sizeof(*sd))) - return VB2_ERROR_WORKBUF_INVALID; - - if (sd->workbuf_size < sd->workbuf_used) - return VB2_ERROR_WORKBUF_INVALID; - - if (sd->workbuf_used > size) - return VB2_ERROR_WORKBUF_SMALL; - - /* Relocate if necessary. */ - if (cur_workbuf != new_workbuf) - memmove(new_workbuf, cur_workbuf, sd->workbuf_used); - - /* Set the new size, and return the context pointer. */ - sd = new_workbuf; - sd->workbuf_size = size; - *ctxptr = &sd->ctx; - - return VB2_SUCCESS; -} -#pragma GCC diagnostic pop - -vb2_error_t vb2api_reinit(void *workbuf, struct vb2_context **ctxptr) -{ - /* Blindly retrieve workbuf_size. vb2api_relocate() will - perform workbuf validation checks. */ - struct vb2_shared_data *sd = workbuf; - return vb2api_relocate(workbuf, workbuf, sd->workbuf_size, ctxptr); -} - void vb2_check_recovery(struct vb2_context *ctx) { struct vb2_shared_data *sd = vb2_get_sd(ctx); |