1 /* Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
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_VBOOT_2MISC_H_
9 #define VBOOT_REFERENCE_VBOOT_2MISC_H_
10 
11 #include "2api.h"
12 
13 struct vb2_gbb_header;
14 struct vb2_workbuf;
15 
16 /**
17  * Get the shared data pointer from the vboot context
18  *
19  * @param ctx		Vboot context
20  * @return The shared data pointer.
21  */
vb2_get_sd(struct vb2_context * ctx)22 static __inline struct vb2_shared_data *vb2_get_sd(struct vb2_context *ctx) {
23 	return (struct vb2_shared_data *)ctx->workbuf;
24 }
25 
26 /**
27  * Validate gbb signature (the magic number)
28  *
29  * @param sig		Pointer to the signature bytes to validate
30  * @return VB2_SUCCESS if valid or non-zero if error.
31  */
32 int vb2_validate_gbb_signature(uint8_t *sig);
33 
34 /**
35  * Initialize a work buffer from the vboot context.
36  *
37  * This sets the work buffer to the unused portion of the context work buffer.
38  *
39  * @param ctx		Vboot context
40  * @param wb		Work buffer to initialize
41  */
42 void vb2_workbuf_from_ctx(struct vb2_context *ctx, struct vb2_workbuf *wb);
43 
44 /**
45  * Read the GBB header.
46  *
47  * @param ctx		Vboot context
48  * @param gbb		Destination for header
49  * @return VB2_SUCCESS, or non-zero if error.
50  */
51 int vb2_read_gbb_header(struct vb2_context *ctx, struct vb2_gbb_header *gbb);
52 
53 /**
54  * Handle vboot failure.
55  *
56  * If the failure occurred after choosing a firmware slot, and the other
57  * firmware slot is not known-bad, try the other firmware slot after reboot.
58  *
59  * If the failure occurred before choosing a firmware slot, or both slots have
60  * failed in successive boots, request recovery.
61  *
62  * @param reason	Recovery reason
63  * @param subcode	Recovery subcode
64  */
65 void vb2_fail(struct vb2_context *ctx, uint8_t reason, uint8_t subcode);
66 
67 /**
68  * Set up the verified boot context data, if not already set up.
69  *
70  * This uses ctx->workbuf_used=0 as a flag to indicate that the data has not
71  * yet been set up.  Caller must set that before calling any voot functions;
72  * see 2api.h.
73  *
74  * @param ctx		Vboot context to initialize
75  * @return VB2_SUCCESS, or error code on error.
76  */
77 int vb2_init_context(struct vb2_context *ctx);
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 int vb2_fw_parse_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 int 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 int 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 int 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 int 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 int vb2_load_fw_preamble(struct vb2_context *ctx);
143 
144 #endif  /* VBOOT_REFERENCE_VBOOT_2MISC_H_ */
145