coreboot
coreboot is an Open Source project aimed at replacing the proprietary BIOS found in most computers.
common.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 
3 #include <assert.h>
4 #include <cbmem.h>
5 #include <fmap.h>
6 #include <vb2_api.h>
7 #include <security/vboot/misc.h>
10 
11 static struct vb2_context *vboot_ctx;
12 
13 static void *vboot_get_workbuf(void)
14 {
15  void *wb = NULL;
16 
19 
20  if (wb == NULL && !CONFIG(VBOOT_STARTS_IN_ROMSTAGE) && preram_symbols_available())
21  wb = _vboot2_work;
22 
23  assert(wb != NULL);
24 
25  return wb;
26 }
27 
28 struct vb2_context *vboot_get_context(void)
29 {
30  void *wb;
31 
32  /* Return if context has already been initialized/restored. */
33  if (vboot_ctx)
34  return vboot_ctx;
35 
36  wb = vboot_get_workbuf();
37 
38  /* Restore context from a previous stage. */
39  if (vboot_logic_executed()) {
40  assert(vb2api_reinit(wb, &vboot_ctx) == VB2_SUCCESS);
41  return vboot_ctx;
42  }
43 
45 
46  /* Initialize vb2_shared_data and friends. */
47  assert(vb2api_init(wb, VB2_FIRMWARE_WORKBUF_RECOMMENDED_SIZE,
48  &vboot_ctx) == VB2_SUCCESS);
49 
50  return vboot_ctx;
51 }
52 
53 int vboot_locate_firmware(struct vb2_context *ctx, struct region_device *fw)
54 {
55  const char *name;
56 
57  if (vboot_is_firmware_slot_a(ctx))
58  name = "FW_MAIN_A";
59  else
60  name = "FW_MAIN_B";
61 
62  int ret = fmap_locate_area_as_rdev(name, fw);
63  if (ret)
64  return ret;
65 
66  /* Truncate area to the size that was actually signed by vboot. */
67  return rdev_chain(fw, fw, 0, vb2api_get_firmware_size(ctx));
68 }
69 
70 static void vboot_setup_cbmem(int unused)
71 {
72  vb2_error_t rv;
73  const size_t cbmem_size = VB2_KERNEL_WORKBUF_RECOMMENDED_SIZE;
74  void *wb_cbmem = cbmem_add(CBMEM_ID_VBOOT_WORKBUF, cbmem_size);
75  assert(wb_cbmem != NULL);
76  /*
77  * On platforms where VBOOT_STARTS_BEFORE_BOOTBLOCK, the verification
78  * occurs before the main processor starts running. The vboot data-
79  * structure is available in the _vboot2_work memory area as soon
80  * as the main processor is released.
81  *
82  * For platforms where VBOOT_STARTS_IN_BOOTBLOCK, vboot verification
83  * occurs before CBMEM is brought online, using pre-RAM. In order to
84  * make vboot data structures available downstream, copy vboot workbuf
85  * from SRAM/CAR into CBMEM.
86  *
87  * For platforms where VBOOT_STARTS_IN_ROMSTAGE, verification occurs
88  * after CBMEM is brought online. Directly initialize vboot data
89  * structures in CBMEM, which will also be available downstream.
90  */
91  if (!CONFIG(VBOOT_STARTS_IN_ROMSTAGE))
92  rv = vb2api_relocate(wb_cbmem, _vboot2_work, cbmem_size,
93  &vboot_ctx);
94  else
95  rv = vb2api_init(wb_cbmem, cbmem_size, &vboot_ctx);
96 
97  assert(rv == VB2_SUCCESS);
98 }
const char * name
Definition: mmu.c:92
#define assert(statement)
Definition: assert.h:74
static int cbmem_possibly_online(void)
Definition: cbmem.h:158
void * cbmem_add(u32 id, u64 size)
Definition: imd_cbmem.c:144
void * cbmem_find(u32 id)
Definition: imd_cbmem.c:166
#define ROMSTAGE_CBMEM_INIT_HOOK(init_fn_)
Definition: cbmem.h:134
#define CBMEM_ID_VBOOT_WORKBUF
Definition: cbmem_id.h:67
@ CONFIG
Definition: dsi_common.h:201
int fmap_locate_area_as_rdev(const char *name, struct region_device *area)
Definition: fmap.c:144
static int preram_symbols_available(void)
Definition: symbols.h:83
static int vboot_is_firmware_slot_a(struct vb2_context *ctx)
Definition: misc.h:18
static int verification_should_run(void)
Definition: misc.h:46
static int vboot_logic_executed(void)
Definition: misc.h:66
int rdev_chain(struct region_device *child, const struct region_device *parent, size_t offset, size_t size)
Definition: region.c:136
struct vb2_context * vboot_get_context(void)
Definition: common.c:28
int vboot_locate_firmware(struct vb2_context *ctx, struct region_device *fw)
Definition: common.c:53
static struct vb2_context * vboot_ctx
Definition: common.c:11
static void vboot_setup_cbmem(int unused)
Definition: common.c:70
static void * vboot_get_workbuf(void)
Definition: common.c:13
#define NULL
Definition: stddef.h:19