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 "bmpblk_header.h"
12 #include "region.h"
13 #include "gbb_access.h"
14 #include "gbb_header.h"
15 #include "load_kernel_fw.h"
16 #include "utility.h"
17 #include "vboot_api.h"
18 #include "vboot_struct.h"
19 
VbGbbReadKey(VbCommonParams * cparams,uint32_t offset,VbPublicKey ** keyp)20 static VbError_t VbGbbReadKey(VbCommonParams *cparams, uint32_t offset,
21 			      VbPublicKey **keyp)
22 {
23 	VbPublicKey hdr, *key;
24 	VbError_t ret;
25 	uint32_t size;
26 
27 	ret = VbRegionReadData(cparams, VB_REGION_GBB, offset,
28 			       sizeof(VbPublicKey), &hdr);
29 	if (ret)
30 		return ret;
31 
32 	/* Deal with a zero-size key (used in testing) */
33 	size = hdr.key_offset + hdr.key_size;
34 	if (size < sizeof(hdr))
35 		size = sizeof(hdr);
36 	key = VbExMalloc(size);
37 	ret = VbRegionReadData(cparams, VB_REGION_GBB, offset, size, key);
38 	if (ret) {
39 		VbExFree(key);
40 		return ret;
41 	}
42 
43 	*keyp = key;
44 	return VBERROR_SUCCESS;
45 }
46 
VbGbbReadRootKey(VbCommonParams * cparams,VbPublicKey ** keyp)47 VbError_t VbGbbReadRootKey(VbCommonParams *cparams, VbPublicKey **keyp)
48 {
49 	return VbGbbReadKey(cparams, cparams->gbb->rootkey_offset, keyp);
50 }
51 
VbGbbReadRecoveryKey(VbCommonParams * cparams,VbPublicKey ** keyp)52 VbError_t VbGbbReadRecoveryKey(VbCommonParams *cparams, VbPublicKey **keyp)
53 {
54 	return VbGbbReadKey(cparams, cparams->gbb->recovery_key_offset, keyp);
55 }
56