blob: 52930336663af666ea605de16382f76d6d5d74c6 [file] [log] [blame]
Furquan Shaikh7632ce02018-03-29 14:49:40 -07001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright 2018 Google LLC
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15
Joel Kitching1a6b5c22019-04-25 19:11:13 +080016#define NEED_VB20_INTERNALS /* Peeking into vb2_gbb_header */
17
Furquan Shaikh7632ce02018-03-29 14:49:40 -070018#include <commonlib/region.h>
19#include <console/console.h>
20#include <fmap.h>
Furquan Shaikh7632ce02018-03-29 14:49:40 -070021#include <security/vboot/gbb.h>
22#include <string.h>
Joel Kitching1a6b5c22019-04-25 19:11:13 +080023#include <vb2_api.h>
Furquan Shaikh7632ce02018-03-29 14:49:40 -070024
25#define GBB_FMAP_REGION_NAME "GBB"
26
27/* Copy of GBB header read from boot media. */
Joel Kitching1a6b5c22019-04-25 19:11:13 +080028static struct vb2_gbb_header gbb_header;
Furquan Shaikh7632ce02018-03-29 14:49:40 -070029
30/*
31 * Read "GBB" region from SPI flash to obtain GBB header and validate
32 * signature.
33 *
34 * Return value:
35 * Success = 0
36 * Error = 1
37 */
38static int gbb_init(void)
39{
40 static bool init_done = false;
41 struct region_device gbb_rdev;
42
43 if (init_done != false)
44 return 0;
45
46 if (fmap_locate_area_as_rdev(GBB_FMAP_REGION_NAME, &gbb_rdev))
47 return 1;
48
49 if (rdev_readat(&gbb_rdev, &gbb_header, 0,
Joel Kitching1a6b5c22019-04-25 19:11:13 +080050 sizeof(struct vb2_gbb_header)) !=
51 sizeof(struct vb2_gbb_header)) {
Furquan Shaikh7632ce02018-03-29 14:49:40 -070052 printk(BIOS_ERR, "%s: Failure to read GBB header!\n", __func__);
53 return 1;
54 }
55
Joel Kitching1a6b5c22019-04-25 19:11:13 +080056 if (memcmp(gbb_header.signature, VB2_GBB_SIGNATURE,
57 VB2_GBB_SIGNATURE_SIZE)) {
Furquan Shaikh7632ce02018-03-29 14:49:40 -070058 printk(BIOS_ERR, "%s: Signature check failed!\n", __func__);
59 return 1;
60 }
61
62 init_done = true;
63 return 0;
64}
65
66uint32_t gbb_get_flags(void)
67{
68 if (gbb_init()) {
69 printk(BIOS_ERR,
70 "%s: Failure to initialize GBB. Returning flags as 0!\n",
71 __func__);
72 return 0;
73 }
74 return gbb_header.flags;
75}
76
77bool gbb_is_flag_set(uint32_t flag)
78{
79 return !!(gbb_get_flags() & flag);
80}