1 /* Copyright (c) 2013 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 * High-level firmware API for loading and verifying rewritable firmware.
6 * (Firmware portion)
7 */
8
9 #include "sysincludes.h"
10
11 #include "region.h"
12 #include "gbb_access.h"
13 #include "gbb_header.h"
14 #include "load_firmware_fw.h"
15 #include "utility.h"
16 #include "vboot_api.h"
17 #include "vboot_common.h"
18 #include "vboot_nvstorage.h"
19
20 /*
21 * Static variables for UpdateFirmwareBodyHash(). It's less than optimal to
22 * have static variables in a library, but in UEFI the caller is deep inside a
23 * different firmware stack and doesn't have a good way to pass the params
24 * struct back to us.
25 */
26 typedef struct VbLoadFirmwareInternal {
27 DigestContext body_digest_context;
28 uint32_t body_size_accum;
29 } VbLoadFirmwareInternal;
30
VbUpdateFirmwareBodyHash(VbCommonParams * cparams,uint8_t * data,uint32_t size)31 void VbUpdateFirmwareBodyHash(VbCommonParams *cparams, uint8_t *data,
32 uint32_t size)
33 {
34 VbLoadFirmwareInternal *lfi =
35 (VbLoadFirmwareInternal*)cparams->vboot_context;
36
37 DigestUpdate(&lfi->body_digest_context, data, size);
38 lfi->body_size_accum += size;
39 }
40
LoadFirmware(VbCommonParams * cparams,VbSelectFirmwareParams * fparams,VbNvContext * vnc)41 int LoadFirmware(VbCommonParams *cparams, VbSelectFirmwareParams *fparams,
42 VbNvContext *vnc)
43 {
44 VbSharedDataHeader *shared =
45 (VbSharedDataHeader *)cparams->shared_data_blob;
46 GoogleBinaryBlockHeader *gbb = cparams->gbb;
47 VbPublicKey *root_key = NULL;
48 VbLoadFirmwareInternal *lfi;
49
50 uint32_t try_b_count;
51 uint32_t lowest_version = 0xFFFFFFFF;
52 int good_index = -1;
53 int is_dev;
54 int index;
55 int i;
56
57 int retval = VBERROR_UNKNOWN;
58 int recovery = VBNV_RECOVERY_RO_UNSPECIFIED;
59
60 /* Clear output params in case we fail */
61 shared->firmware_index = 0xFF;
62
63 VBDEBUG(("LoadFirmware started...\n"));
64
65 /* Must have a root key from the GBB */
66 retval = VbGbbReadRootKey(cparams, &root_key);
67 if (retval) {
68 VBDEBUG(("No GBB\n"));
69 retval = VBERROR_INVALID_GBB;
70 goto LoadFirmwareExit;
71 }
72
73 /* Parse flags */
74 is_dev = (shared->flags & VBSD_BOOT_DEV_SWITCH_ON ? 1 : 0);
75 if (is_dev)
76 shared->flags |= VBSD_LF_DEV_SWITCH_ON;
77
78 /* Read try-b count and decrement if necessary */
79 VbNvGet(vnc, VBNV_TRY_B_COUNT, &try_b_count);
80 if (0 != try_b_count) {
81 VbNvSet(vnc, VBNV_TRY_B_COUNT, try_b_count - 1);
82 shared->flags |= VBSD_FWB_TRIED;
83 }
84
85 /* Allocate our internal data */
86 lfi = (VbLoadFirmwareInternal *)
87 VbExMalloc(sizeof(VbLoadFirmwareInternal));
88 cparams->vboot_context = lfi;
89
90 /* Loop over indices */
91 for (i = 0; i < 2; i++) {
92 VbKeyBlockHeader *key_block;
93 uint32_t vblock_size;
94 VbFirmwarePreambleHeader *preamble;
95 RSAPublicKey *data_key;
96 uint64_t key_version;
97 uint32_t combined_version;
98 uint8_t *body_digest;
99 uint8_t *check_result;
100
101 /* If try B count is non-zero try firmware B first */
102 index = (try_b_count ? 1 - i : i);
103 if (0 == index) {
104 key_block = (VbKeyBlockHeader *)
105 fparams->verification_block_A;
106 vblock_size = fparams->verification_size_A;
107 check_result = &shared->check_fw_a_result;
108 } else {
109 key_block = (VbKeyBlockHeader *)
110 fparams->verification_block_B;
111 vblock_size = fparams->verification_size_B;
112 check_result = &shared->check_fw_b_result;
113 }
114
115 /*
116 * Check the key block flags against the current boot mode. Do
117 * this before verifying the key block, since flags are faster
118 * to check than the RSA signature.
119 */
120 if (!(key_block->key_block_flags &
121 (is_dev ? KEY_BLOCK_FLAG_DEVELOPER_1 :
122 KEY_BLOCK_FLAG_DEVELOPER_0))) {
123 VBDEBUG(("Developer flag mismatch.\n"));
124 *check_result = VBSD_LF_CHECK_DEV_MISMATCH;
125 continue;
126 }
127
128 /* RW firmware never runs in recovery mode. */
129 if (!(key_block->key_block_flags & KEY_BLOCK_FLAG_RECOVERY_0)) {
130 VBDEBUG(("Recovery flag mismatch.\n"));
131 *check_result = VBSD_LF_CHECK_REC_MISMATCH;
132 continue;
133 }
134
135 /* Verify the key block */
136 if ((0 != KeyBlockVerify(key_block, vblock_size,
137 root_key, 0))) {
138 VBDEBUG(("Key block verification failed.\n"));
139 *check_result = VBSD_LF_CHECK_VERIFY_KEYBLOCK;
140 continue;
141 }
142
143 /* Check for rollback of key version. */
144 key_version = key_block->data_key.key_version;
145 if (!(gbb->flags & GBB_FLAG_DISABLE_FW_ROLLBACK_CHECK)) {
146 if (key_version < (shared->fw_version_tpm >> 16)) {
147 VBDEBUG(("Key rollback detected.\n"));
148 *check_result = VBSD_LF_CHECK_KEY_ROLLBACK;
149 continue;
150 }
151 if (key_version > 0xFFFF) {
152 /*
153 * Key version is stored in 16 bits in the TPM,
154 * so key versions greater than 0xFFFF can't be
155 * stored properly.
156 */
157 VBDEBUG(("Key version > 0xFFFF.\n"));
158 *check_result = VBSD_LF_CHECK_KEY_ROLLBACK;
159 continue;
160 }
161 }
162
163 /* Get key for preamble/data verification from the key block. */
164 data_key = PublicKeyToRSA(&key_block->data_key);
165 if (!data_key) {
166 VBDEBUG(("Unable to parse data key.\n"));
167 *check_result = VBSD_LF_CHECK_DATA_KEY_PARSE;
168 continue;
169 }
170
171 /* Verify the preamble, which follows the key block. */
172 preamble = (VbFirmwarePreambleHeader *)
173 ((uint8_t *)key_block + key_block->key_block_size);
174 if ((0 != VerifyFirmwarePreamble(
175 preamble,
176 vblock_size - key_block->key_block_size,
177 data_key))) {
178 VBDEBUG(("Preamble verfication failed.\n"));
179 *check_result = VBSD_LF_CHECK_VERIFY_PREAMBLE;
180 RSAPublicKeyFree(data_key);
181 continue;
182 }
183
184 /* Check for rollback of firmware version. */
185 combined_version = (uint32_t)((key_version << 16) |
186 (preamble->firmware_version & 0xFFFF));
187 if (combined_version < shared->fw_version_tpm &&
188 !(gbb->flags & GBB_FLAG_DISABLE_FW_ROLLBACK_CHECK)) {
189 VBDEBUG(("Firmware version rollback detected.\n"));
190 *check_result = VBSD_LF_CHECK_FW_ROLLBACK;
191 RSAPublicKeyFree(data_key);
192 continue;
193 }
194
195 /* Header for this firmware is valid */
196 *check_result = VBSD_LF_CHECK_HEADER_VALID;
197
198 /* Check for lowest key version from a valid header. */
199 if (lowest_version > combined_version)
200 lowest_version = combined_version;
201
202 /*
203 * If we already have good firmware, no need to read another
204 * one; we only needed to look at the versions to check for
205 * rollback.
206 */
207 if (-1 != good_index) {
208 RSAPublicKeyFree(data_key);
209 continue;
210 }
211
212 /* Handle preamble flag for using the RO normal/dev code path */
213 VBDEBUG(("Preamble flags %#x\n", VbGetFirmwarePreambleFlags(preamble)));
214 if (VbGetFirmwarePreambleFlags(preamble) &
215 VB_FIRMWARE_PREAMBLE_USE_RO_NORMAL) {
216
217 /* Fail if calling firmware doesn't support RO normal */
218 if (!(shared->flags & VBSD_BOOT_RO_NORMAL_SUPPORT)) {
219 VBDEBUG(("No RO normal support.\n"));
220 *check_result = VBSD_LF_CHECK_NO_RO_NORMAL;
221 RSAPublicKeyFree(data_key);
222 continue;
223 }
224
225 /* Use the RO normal code path */
226 shared->flags |= VBSD_LF_USE_RO_NORMAL;
227
228 } else {
229 VbError_t rv;
230
231 /* Read the firmware data */
232 DigestInit(&lfi->body_digest_context,
233 data_key->algorithm);
234 lfi->body_size_accum = 0;
235 rv = VbExHashFirmwareBody(
236 cparams,
237 (index ? VB_SELECT_FIRMWARE_B :
238 VB_SELECT_FIRMWARE_A));
239 if (VBERROR_SUCCESS != rv) {
240 VBDEBUG(("VbExHashFirmwareBody() failed for "
241 "index %d\n", index));
242 *check_result = VBSD_LF_CHECK_GET_FW_BODY;
243 RSAPublicKeyFree(data_key);
244 continue;
245 }
246 if (lfi->body_size_accum !=
247 preamble->body_signature.data_size) {
248 VBDEBUG(("Hashed %d bytes but expected %d\n",
249 (int)lfi->body_size_accum,
250 (int)preamble->body_signature.data_size));
251 *check_result = VBSD_LF_CHECK_HASH_WRONG_SIZE;
252 RSAPublicKeyFree(data_key);
253 continue;
254 }
255
256 /* Verify firmware data */
257 body_digest = DigestFinal(&lfi->body_digest_context);
258 if (0 != VerifyDigest(body_digest,
259 &preamble->body_signature,
260 data_key)) {
261 VBDEBUG(("FW body verification failed.\n"));
262 *check_result = VBSD_LF_CHECK_VERIFY_BODY;
263 RSAPublicKeyFree(data_key);
264 VbExFree(body_digest);
265 continue;
266 }
267 VbExFree(body_digest);
268 }
269
270 /* Done with the data key, so can free it now */
271 RSAPublicKeyFree(data_key);
272
273 /* If we're still here, the firmware is valid. */
274 VBDEBUG(("Firmware %d is valid.\n", index));
275 *check_result = VBSD_LF_CHECK_VALID;
276 if (-1 == good_index) {
277 /* Save the key we actually used */
278 if (0 != VbSharedDataSetKernelKey(
279 shared, &preamble->kernel_subkey)) {
280 /*
281 * The firmware signature was good, but the
282 * public key was bigger that the caller can
283 * handle.
284 */
285 VBDEBUG(("Unable to save kernel subkey.\n"));
286 continue;
287 }
288
289 /*
290 * Save the good index, now that we're sure we can
291 * actually use this firmware. That's the one we'll
292 * boot.
293 */
294 good_index = index;
295 shared->firmware_index = (uint8_t)index;
296 shared->fw_keyblock_flags = key_block->key_block_flags;
297
298 /*
299 * If the good firmware's key version is the same as
300 * the tpm, then the TPM doesn't need updating; we can
301 * stop now. Otherwise, we'll check all the other
302 * headers to see if they contain a newer key.
303 */
304 if (combined_version == shared->fw_version_tpm)
305 break;
306 }
307 }
308
309 /* Free internal data */
310 VbExFree(lfi);
311 cparams->vboot_context = NULL;
312
313 /* Handle finding good firmware */
314 if (good_index >= 0) {
315
316 /* Save versions we found */
317 shared->fw_version_lowest = lowest_version;
318 if (lowest_version > shared->fw_version_tpm)
319 shared->fw_version_tpm = lowest_version;
320
321 /* Success */
322 VBDEBUG(("Will boot firmware index %d\n",
323 (int)shared->firmware_index));
324 retval = VBERROR_SUCCESS;
325
326 } else {
327 uint8_t a = shared->check_fw_a_result;
328 uint8_t b = shared->check_fw_b_result;
329 uint8_t best_check;
330
331 /* No good firmware, so go to recovery mode. */
332 VBDEBUG(("Alas, no good firmware.\n"));
333 recovery = VBNV_RECOVERY_RO_INVALID_RW;
334 retval = VBERROR_LOAD_FIRMWARE;
335
336 /*
337 * If the best check result fits in the range of recovery
338 * reasons, provide more detail on how far we got in
339 * validation.
340 */
341 best_check = (a > b ? a : b) +
342 VBNV_RECOVERY_RO_INVALID_RW_CHECK_MIN;
343 if (best_check >= VBNV_RECOVERY_RO_INVALID_RW_CHECK_MIN &&
344 best_check <= VBNV_RECOVERY_RO_INVALID_RW_CHECK_MAX)
345 recovery = best_check;
346 }
347
348 LoadFirmwareExit:
349 VbExFree(root_key);
350
351 /* Store recovery request, if any */
352 VbNvSet(vnc, VBNV_RECOVERY_REQUEST, VBERROR_SUCCESS != retval ?
353 recovery : VBNV_RECOVERY_NOT_REQUESTED);
354 /* If the system does not support RO_NORMAL and LoadFirmware()
355 * encountered an error, update the shared recovery reason if
356 * recovery was not previously requested. */
357 if (!(shared->flags & VBSD_BOOT_RO_NORMAL_SUPPORT) &&
358 VBNV_RECOVERY_NOT_REQUESTED == shared->recovery_reason &&
359 VBERROR_SUCCESS != retval) {
360 VBDEBUG(("RO normal but we got an error.\n"));
361 shared->recovery_reason = recovery;
362 }
363
364 return retval;
365 }
366