coreboot
coreboot is an Open Source project aimed at replacing the proprietary BIOS found in most computers.
misc.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 
3 #ifndef __VBOOT_MISC_H__
4 #define __VBOOT_MISC_H__
5 
6 #include <assert.h>
8 #include <vb2_api.h>
9 
10 /*
11  * Source: security/vboot/common.c
12  */
13 struct vb2_context *vboot_get_context(void);
14 
15 /*
16  * Returns 1 if firmware slot A is used, 0 if slot B is used.
17  */
18 static inline int vboot_is_firmware_slot_a(struct vb2_context *ctx)
19 {
20  return !(ctx->flags & VB2_CONTEXT_FW_SLOT_B);
21 }
22 
23 /*
24  * Check if given flag is set in the flags field in GBB header.
25  * Return value:
26  * true: Flag is set.
27  * false: Flag is not set.
28  */
29 static inline bool vboot_is_gbb_flag_set(enum vb2_gbb_flag flag)
30 {
31  return !!(vb2api_gbb_get_flags(vboot_get_context()) & flag);
32 }
33 
34 /*
35  * Locates firmware as a region device. Returns 0 on success, -1 on failure.
36  */
37 int vboot_locate_firmware(struct vb2_context *ctx, struct region_device *fw);
38 
39 /*
40  * The stage loading code is compiled and entered from multiple stages. The
41  * helper functions below attempt to provide more clarity on when certain
42  * code should be called. They are implemented inline for better compile-time
43  * code elimination.
44  */
45 
46 static inline int verification_should_run(void)
47 {
48  if (CONFIG(VBOOT_SEPARATE_VERSTAGE))
49  return ENV_SEPARATE_VERSTAGE;
50  else if (CONFIG(VBOOT_STARTS_IN_ROMSTAGE))
51  return ENV_ROMSTAGE;
52  else if (CONFIG(VBOOT_STARTS_IN_BOOTBLOCK))
53  return ENV_BOOTBLOCK;
54  else
55  dead_code();
56 }
57 
58 static inline int verstage_should_load(void)
59 {
60  if (CONFIG(VBOOT_SEPARATE_VERSTAGE) && !CONFIG(VBOOT_STARTS_BEFORE_BOOTBLOCK))
61  return ENV_BOOTBLOCK;
62  else
63  return 0;
64 }
65 
66 static inline int vboot_logic_executed(void)
67 {
68  extern int vboot_executed; /* should not be globally accessible */
69 
70  /* If we are in the stage that runs verification, or in the stage that
71  both loads the verstage and is returned to from it afterwards, we
72  need to check a global to see if verification has run. */
74  (verstage_should_load() && CONFIG(VBOOT_RETURN_FROM_VERSTAGE)))
75  return vboot_executed;
76 
77  if (CONFIG(VBOOT_STARTS_IN_BOOTBLOCK)) {
78  /* All other stages are "after the bootblock" */
79  return !ENV_BOOTBLOCK;
80  } else if (CONFIG(VBOOT_STARTS_IN_ROMSTAGE)) {
81  /* Post-RAM stages are "after the romstage" */
82  return !ENV_ROMSTAGE_OR_BEFORE;
83  } else if (CONFIG(VBOOT_STARTS_BEFORE_BOOTBLOCK)) {
84  return !ENV_SEPARATE_VERSTAGE;
85  } else {
86  dead_code();
87  }
88 }
89 
90 #endif /* __VBOOT_MISC_H__ */
#define dead_code()
Definition: assert.h:89
@ CONFIG
Definition: dsi_common.h:201
static int vboot_is_firmware_slot_a(struct vb2_context *ctx)
Definition: misc.h:18
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 int verstage_should_load(void)
Definition: misc.h:58
static bool vboot_is_gbb_flag_set(enum vb2_gbb_flag flag)
Definition: misc.h:29
static int verification_should_run(void)
Definition: misc.h:46
static int vboot_logic_executed(void)
Definition: misc.h:66
#define ENV_BOOTBLOCK
Definition: rules.h:148
#define ENV_ROMSTAGE
Definition: rules.h:149
#define ENV_ROMSTAGE_OR_BEFORE
Definition: rules.h:263
#define ENV_SEPARATE_VERSTAGE
Definition: rules.h:152
int vboot_executed
Definition: vboot_loader.c:27