xref: /aosp_15_r20/external/vboot_reference/firmware/2lib/include/2misc.h (revision 8617a60d3594060b7ecbd21bc622a7c14f3cf2bc)
1 /* Copyright 2014 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  * Misc functions which need access to vb2_context but are not public APIs
6  */
7 
8 #ifndef VBOOT_REFERENCE_2MISC_H_
9 #define VBOOT_REFERENCE_2MISC_H_
10 
11 #include "2api.h"
12 #include "2struct.h"
13 
14 struct vb2_gbb_header;
15 struct vb2_workbuf;
16 
17 #define vb2_container_of(ptr, type, member) ({                     \
18 	const typeof(((type *)0)->member) *__mptr = (ptr);         \
19 	(type *)((uint8_t *)__mptr - offsetof(type, member) );})   \
20 
21 /**
22  * Get the shared data pointer from the vboot context
23  *
24  * @param ctx		Vboot context
25  * @return The shared data pointer.
26  */
vb2_get_sd(struct vb2_context * ctx)27 static inline struct vb2_shared_data *vb2_get_sd(struct vb2_context *ctx)
28 {
29 	return vb2_container_of(ctx, struct vb2_shared_data, ctx);
30 }
31 
32 /**
33  * Get the GBB header pointer from a vboot context's shared data
34  *
35  * @param ctx		Vboot context
36  * @return The GBB header pointer.
37  */
38 struct vb2_gbb_header *vb2_get_gbb(struct vb2_context *ctx);
39 
40 /**
41  * Validate gbb signature (the magic number)
42  *
43  * @param sig		Pointer to the signature bytes to validate
44  * @return VB2_SUCCESS if valid or non-zero if error.
45  */
46 vb2_error_t vb2_validate_gbb_signature(uint8_t *sig);
47 
48 /**
49  * Initialize a work buffer from the vboot context.
50  *
51  * This sets the work buffer to the unused portion of the context work buffer.
52  *
53  * @param ctx		Vboot context
54  * @param wb		Work buffer to initialize
55  */
56 void vb2_workbuf_from_ctx(struct vb2_context *ctx, struct vb2_workbuf *wb);
57 
58 /**
59  * Set the amount of work buffer used in the vboot context.
60  *
61  * This will round up to VB2_WORKBUF_ALIGN, so that the next allocation will
62  * be aligned as expected.
63  *
64  * @param ctx		Vboot context
65  * @param used		Number of bytes used
66  */
67 void vb2_set_workbuf_used(struct vb2_context *ctx, uint32_t used);
68 
69 /**
70  * Read the GBB header.
71  *
72  * @param ctx		Vboot context
73  * @param gbb		Destination for header
74  * @return VB2_SUCCESS, or non-zero if error.
75  */
76 vb2_error_t vb2_read_gbb_header(struct vb2_context *ctx,
77 				struct vb2_gbb_header *gbb);
78 
79 /**
80  * Check for recovery reasons we can determine early in the boot process.
81  *
82  * On exit, check ctx->flags for VB2_CONTEXT_RECOVERY_MODE; if present, jump to
83  * the recovery path instead of continuing with normal boot.  This is the only
84  * direct path to recovery mode.  All other errors later in the boot process
85  * should induce a reboot instead of jumping to recovery, so that recovery mode
86  * starts from a consistent firmware state.
87  *
88  * @param ctx		Vboot context
89  */
90 void vb2_check_recovery(struct vb2_context *ctx);
91 
92 /**
93  * Parse the GBB header.
94  *
95  * @param ctx		Vboot context
96  * @return VB2_SUCCESS, or error code on error.
97  */
98 vb2_error_t vb2_fw_init_gbb(struct vb2_context *ctx);
99 
100 /**
101  * Check developer switch position.
102  *
103  * @param ctx		Vboot context
104  * @return VB2_SUCCESS, or error code on error.
105  */
106 vb2_error_t vb2_check_dev_switch(struct vb2_context *ctx);
107 
108 /**
109  * Check if we need to clear the TPM owner.
110  *
111  * @param ctx		Vboot context
112  * @return VB2_SUCCESS, or error code on error.
113  */
114 vb2_error_t vb2_check_tpm_clear(struct vb2_context *ctx);
115 
116 /**
117  * Decide which firmware slot to try this boot.
118  *
119  * @param ctx		Vboot context
120  * @return VB2_SUCCESS, or error code on error.
121  */
122 vb2_error_t vb2_select_fw_slot(struct vb2_context *ctx);
123 
124 /**
125  * Verify the firmware keyblock using the root key.
126  *
127  * After this call, the data key is stored in the work buffer.
128  *
129  * @param ctx		Vboot context
130  * @return VB2_SUCCESS, or error code on error.
131  */
132 vb2_error_t vb2_load_fw_keyblock(struct vb2_context *ctx);
133 
134 /**
135  * Verify the firmware preamble using the data subkey from the keyblock.
136  *
137  * After this call, the preamble is stored in the work buffer.
138  *
139  * @param ctx		Vboot context
140  * @return VB2_SUCCESS, or error code on error.
141  */
142 vb2_error_t vb2_load_fw_preamble(struct vb2_context *ctx);
143 
144 /**
145  * Verify the kernel keyblock using the previously-loaded kernel key.
146  *
147  * After this call, the data key is stored in the work buffer.
148  *
149  * @param ctx		Vboot context
150  * @return VB2_SUCCESS, or error code on error.
151  */
152 vb2_error_t vb2_load_kernel_keyblock(struct vb2_context *ctx);
153 
154 /**
155  * Verify the kernel preamble using the data subkey from the keyblock.
156  *
157  * After this call, the preamble is stored in the work buffer.
158  *
159  * @param ctx		Vboot context
160  * @return VB2_SUCCESS, or error code on error.
161  */
162 vb2_error_t vb2_load_kernel_preamble(struct vb2_context *ctx);
163 
164 /**
165  * Fill VB2_CONTEXT_DEV_BOOT_ALLOWED, VB2_CONTEXT_DEV_BOOT_EXTERNAL_ALLOWED and
166  * VB2_CONTEXT_DEV_BOOT_ALTFW_ALLOWED flags in ctx->flags.
167  *
168  * @param ctx		Vboot context.
169  */
170 void vb2_fill_dev_boot_flags(struct vb2_context *ctx);
171 
172 /**
173  * Determine and set a mutually exclusive boot mode in the vboot context.
174  *
175  * Determine the most relevant boot mode for current boot, store into
176  * ctx->boot_mode, which is a ctx field introduced in struct version 3.1.
177  *
178  * This function should be only called by vb2api_fw_phase1.
179  * The vb2api_fw_phase1 should call this function at its end phase once and all
180  * the following steps should directly access ctx->boot_mode to retrieve the
181  * most relevant boot mode.
182  *
183  * @param ctx		Vboot context.
184  */
185 void vb2_set_boot_mode(struct vb2_context *ctx);
186 
187 #endif  /* VBOOT_REFERENCE_2MISC_H_ */
188