xref: /aosp_15_r20/external/vboot_reference/firmware/2lib/2gbb.c (revision 8617a60d3594060b7ecbd21bc622a7c14f3cf2bc)
1 /* Copyright 2019 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  * GBB accessor functions.
6  */
7 
8 #include "2common.h"
9 #include "2misc.h"
10 
vb2_gbb_read_key(struct vb2_context * ctx,uint32_t offset,uint32_t * size,struct vb2_packed_key ** keyp,struct vb2_workbuf * wb)11 static vb2_error_t vb2_gbb_read_key(struct vb2_context *ctx, uint32_t offset,
12 				    uint32_t *size,
13 				    struct vb2_packed_key **keyp,
14 				    struct vb2_workbuf *wb)
15 {
16 	struct vb2_workbuf wblocal = *wb;
17 
18 	/* Check offset and size. */
19 	if (offset < sizeof(struct vb2_gbb_header))
20 		return VB2_ERROR_GBB_INVALID;
21 	if (*size < sizeof(**keyp))
22 		return VB2_ERROR_GBB_INVALID;
23 
24 	/* GBB header might be padded.  Retrieve the vb2_packed_key
25 	   header so we can find out what the real size is. */
26 	*keyp = vb2_workbuf_alloc(&wblocal, sizeof(**keyp));
27 	if (!*keyp)
28 		return VB2_ERROR_GBB_WORKBUF;
29 	VB2_TRY(vb2ex_read_resource(ctx, VB2_RES_GBB, offset, *keyp,
30 				    sizeof(**keyp)));
31 
32 	VB2_TRY(vb2_verify_packed_key_inside(*keyp, *size, *keyp));
33 
34 	/* Deal with a zero-size key (used in testing). */
35 	*size = (*keyp)->key_offset + (*keyp)->key_size;
36 	*size = VB2_MAX(*size, sizeof(**keyp));
37 
38 	/* Now that we know the real size of the key, retrieve the key
39 	   data, and write it on the workbuf, directly after vb2_packed_key. */
40 	*keyp = vb2_workbuf_realloc(&wblocal, sizeof(**keyp), *size);
41 	if (!*keyp)
42 		return VB2_ERROR_GBB_WORKBUF;
43 
44 	VB2_TRY(vb2ex_read_resource(ctx, VB2_RES_GBB,
45 				    offset + sizeof(**keyp),
46 				    (void *)*keyp + sizeof(**keyp),
47 				    *size - sizeof(**keyp)));
48 	*wb = wblocal;
49 	return VB2_SUCCESS;
50 }
51 
52 test_mockable
vb2_gbb_read_root_key(struct vb2_context * ctx,struct vb2_packed_key ** keyp,uint32_t * size,struct vb2_workbuf * wb)53 vb2_error_t vb2_gbb_read_root_key(struct vb2_context *ctx,
54 				  struct vb2_packed_key **keyp, uint32_t *size,
55 				  struct vb2_workbuf *wb)
56 {
57 	struct vb2_gbb_header *gbb = vb2_get_gbb(ctx);
58 	uint32_t size_in = gbb->rootkey_size;
59 	vb2_error_t ret = vb2_gbb_read_key(ctx, gbb->rootkey_offset,
60 					   &size_in, keyp, wb);
61 	if (size)
62 		*size = size_in;
63 	return ret;
64 }
65 
66 test_mockable
vb2_gbb_read_recovery_key(struct vb2_context * ctx,struct vb2_packed_key ** keyp,uint32_t * size,struct vb2_workbuf * wb)67 vb2_error_t vb2_gbb_read_recovery_key(struct vb2_context *ctx,
68 				      struct vb2_packed_key **keyp,
69 				      uint32_t *size, struct vb2_workbuf *wb)
70 {
71 	struct vb2_gbb_header *gbb = vb2_get_gbb(ctx);
72 	uint32_t size_in = gbb->recovery_key_size;
73 	vb2_error_t ret = vb2_gbb_read_key(ctx, gbb->recovery_key_offset,
74 					   &size_in, keyp, wb);
75 	if (size)
76 		*size = size_in;
77 	return ret;
78 }
79 
vb2api_gbb_read_hwid(struct vb2_context * ctx,char * hwid,uint32_t * size)80 vb2_error_t vb2api_gbb_read_hwid(struct vb2_context *ctx, char *hwid,
81 				 uint32_t *size)
82 {
83 	struct vb2_gbb_header *gbb = vb2_get_gbb(ctx);
84 	uint32_t i;
85 	vb2_error_t ret;
86 
87 	if (gbb->hwid_size == 0) {
88 		VB2_DEBUG("invalid HWID size %d\n", gbb->hwid_size);
89 		return VB2_ERROR_GBB_INVALID;
90 	}
91 
92 	*size = VB2_MIN(*size, VB2_GBB_HWID_MAX_SIZE);
93 	*size = VB2_MIN(*size, gbb->hwid_size);
94 
95 	ret = vb2ex_read_resource(ctx, VB2_RES_GBB, gbb->hwid_offset,
96 				  hwid, *size);
97 	if (ret) {
98 		VB2_DEBUG("read resource failure: %d\n", ret);
99 		return ret;
100 	}
101 
102 	/* Count HWID size, and ensure that it fits in the given buffer. */
103 	for (i = 0; i < *size; i++) {
104 		if (hwid[i] == '\0') {
105 			*size = i + 1;
106 			break;
107 		}
108 	}
109 	if (hwid[*size - 1] != '\0')
110 		return VB2_ERROR_INVALID_PARAMETER;
111 
112 	return VB2_SUCCESS;
113 }
114 
vb2api_gbb_get_flags(struct vb2_context * ctx)115 vb2_gbb_flags_t vb2api_gbb_get_flags(struct vb2_context *ctx)
116 {
117 	struct vb2_gbb_header *gbb = vb2_get_gbb(ctx);
118 	return gbb->flags;
119 }
120 
vb2_get_gbb_flag_description(enum vb2_gbb_flag flag,const char ** name,const char ** description)121 vb2_error_t vb2_get_gbb_flag_description(enum vb2_gbb_flag flag,
122 					 const char **name,
123 					 const char **description)
124 {
125 	switch (flag) {
126 	case VB2_GBB_FLAG_DEV_SCREEN_SHORT_DELAY:
127 		*name = "VB2_GBB_FLAG_DEV_SCREEN_SHORT_DELAY";
128 		*description = "Reduce the dev screen delay to 2 sec from 30 sec.";
129 		break;
130 	case VB2_GBB_FLAG_LOAD_OPTION_ROMS:
131 		*name = "VB2_GBB_FLAG_LOAD_OPTION_ROMS";
132 		*description = "BIOS should load option ROMs from arbitrary PCI devices.";
133 		break;
134 	case VB2_GBB_FLAG_ENABLE_ALTERNATE_OS:
135 		*name = "VB2_GBB_FLAG_ENABLE_ALTERNATE_OS";
136 		*description = "Boot a non-ChromeOS kernel.";
137 		break;
138 	case VB2_GBB_FLAG_FORCE_DEV_SWITCH_ON:
139 		*name = "VB2_GBB_FLAG_FORCE_DEV_SWITCH_ON";
140 		*description = "Force dev switch on, regardless of physical/keyboard dev switch.";
141 		break;
142 	case VB2_GBB_FLAG_FORCE_DEV_BOOT_USB:
143 		*name = "VB2_GBB_FLAG_FORCE_DEV_BOOT_USB";
144 		*description = "Allow booting from external disk even if dev_boot_usb=0.";
145 		break;
146 	case VB2_GBB_FLAG_DISABLE_FW_ROLLBACK_CHECK:
147 		*name = "VB2_GBB_FLAG_DISABLE_FW_ROLLBACK_CHECK";
148 		*description = "Disable firmware rollback protection.";
149 		break;
150 	case VB2_GBB_FLAG_ENTER_TRIGGERS_TONORM:
151 		*name = "VB2_GBB_FLAG_ENTER_TRIGGERS_TONORM";
152 		*description = "Allow Enter key to trigger dev->tonorm screen transition.";
153 		break;
154 	case VB2_GBB_FLAG_FORCE_DEV_BOOT_ALTFW:
155 		*name = "VB2_GBB_FLAG_FORCE_DEV_BOOT_ALTFW";
156 		*description =
157 			"Allow booting Legacy OSes even if dev_boot_altfw=0.";
158 		break;
159 	case VB2_GBB_FLAG_DEPRECATED_RUNNING_FAFT:
160 		*name = "VB2_GBB_FLAG_DEPRECATED_RUNNING_FAFT";
161 		*description = "Deprecated, do not use.";
162 		break;
163 	case VB2_GBB_FLAG_DISABLE_EC_SOFTWARE_SYNC:
164 		*name = "VB2_GBB_FLAG_DISABLE_EC_SOFTWARE_SYNC";
165 		*description = "Disable EC software sync.";
166 		break;
167 	case VB2_GBB_FLAG_DEFAULT_DEV_BOOT_ALTFW:
168 		*name = "VB2_GBB_FLAG_DEFAULT_DEV_BOOT_ALTFW";
169 		*description = "Default to booting legacy OS when dev screen times out.";
170 		break;
171 	case VB2_GBB_FLAG_DISABLE_AUXFW_SOFTWARE_SYNC:
172 		*name = "VB2_GBB_FLAG_DISABLE_AUXFW_SOFTWARE_SYNC";
173 		*description =
174 			"Disable auxiliary firmware (auxfw) software sync.";
175 		break;
176 	case VB2_GBB_FLAG_DISABLE_LID_SHUTDOWN:
177 		*name = "VB2_GBB_FLAG_DISABLE_LID_SHUTDOWN";
178 		*description = "Disable shutdown on lid closed.";
179 		break;
180 	case VB2_GBB_FLAG_DEPRECATED_FORCE_DEV_BOOT_FASTBOOT_FULL_CAP:
181 		*name = "VB2_GBB_FLAG_DEPRECATED_FORCE_DEV_BOOT_FASTBOOT_FULL_CAP";
182 		*description = "Allow full fastboot capability in firmware even if dev_boot_fastboot_full_cap=0.";
183 		break;
184 	case VB2_GBB_FLAG_FORCE_MANUAL_RECOVERY:
185 		*name = "VB2_GBB_FLAG_FORCE_MANUAL_RECOVERY";
186 		*description = "Recovery mode always assumes manual recovery, even if EC_IN_RW=1.";
187 		break;
188 	case VB2_GBB_FLAG_DISABLE_FWMP:
189 		*name = "VB2_GBB_FLAG_DISABLE_FWMP";
190 		*description = "Disable FWMP.";
191 		break;
192 	case VB2_GBB_FLAG_ENABLE_UDC:
193 		*name = "VB2_GBB_FLAG_ENABLE_UDC";
194 		*description = "Enable USB Device Controller.";
195 		break;
196 	case VB2_GBB_FLAG_FORCE_CSE_SYNC:
197 		*name = "VB2_GBB_FLAG_FORCE_CSE_SYNC";
198 		*description = "Always sync CSE, even if it is same as CBFS CSE";
199 		break;
200 	default:
201 		*name = NULL;
202 		*description = NULL;
203 		return VB2_ERROR_UNKNOWN;
204 	}
205 	return VB2_SUCCESS;
206 }
207