blob: 97944d92a6256800dc0a9f3b4a6a52cf16ea54e1 [file] [log] [blame]
Daisuke Nojiri742fc8d2014-10-10 10:51:06 -07001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2014 The ChromiumOS Authors. All rights reserved.
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.
Daisuke Nojiri742fc8d2014-10-10 10:51:06 -070014 */
15
Furquan Shaikh2a12e2e2016-07-25 11:48:03 -070016#ifndef __VBOOT_MISC_H__
17#define __VBOOT_MISC_H__
Daisuke Nojiri742fc8d2014-10-10 10:51:06 -070018
Julius Werner998dc172019-05-09 14:16:13 -070019#include <assert.h>
Philipp Deppenwiesefea24292017-10-17 17:02:29 +020020#include <security/vboot/vboot_common.h>
Joel Kitching814c8652020-02-14 13:18:06 +080021#include <vb2_api.h>
Aaron Durbinb5a20b22015-10-06 17:29:03 -050022
Joel Kitching8d0f5992019-03-13 18:10:52 +080023/*
Joel Kitchingaf8471c2019-03-13 22:38:07 +080024 * Source: security/vboot/common.c
25 */
Joel Kitching2332c742019-10-23 15:01:37 +080026struct vb2_context *vboot_get_context(void);
Daisuke Nojiri742fc8d2014-10-10 10:51:06 -070027
Yu-Ping Wuaeb652a2019-11-14 15:42:25 +080028/*
29 * Returns 1 if firmware slot A is used, 0 if slot B is used.
30 */
Julius Wernerf8e17642019-12-12 13:23:06 -080031static inline int vboot_is_firmware_slot_a(struct vb2_context *ctx)
Yu-Ping Wuaeb652a2019-11-14 15:42:25 +080032{
33 return !(ctx->flags & VB2_CONTEXT_FW_SLOT_B);
34}
Daisuke Nojiri742fc8d2014-10-10 10:51:06 -070035
Yu-Ping Wuaeb652a2019-11-14 15:42:25 +080036/*
Julius Wernerd618aac2019-11-26 17:58:11 -080037 * Check if given flag is set in the flags field in GBB header.
38 * Return value:
39 * true: Flag is set.
40 * false: Flag is not set.
41 */
42static inline bool vboot_is_gbb_flag_set(enum vb2_gbb_flag flag)
43{
44 return !!(vb2api_gbb_get_flags(vboot_get_context()) & flag);
45}
46
47/*
Yu-Ping Wuaeb652a2019-11-14 15:42:25 +080048 * Locates firmware as a region device. Returns 0 on success, -1 on failure.
49 */
Julius Wernerf8e17642019-12-12 13:23:06 -080050int vboot_locate_firmware(struct vb2_context *ctx, struct region_device *fw);
Joel Kitchingaf8471c2019-03-13 22:38:07 +080051
52/*
Julius Werner998dc172019-05-09 14:16:13 -070053 * The stage loading code is compiled and entered from multiple stages. The
54 * helper functions below attempt to provide more clarity on when certain
55 * code should be called. They are implemented inline for better compile-time
56 * code elimination.
57 */
58
59static inline int verification_should_run(void)
60{
61 if (CONFIG(VBOOT_SEPARATE_VERSTAGE))
62 return ENV_VERSTAGE;
63 else if (CONFIG(VBOOT_STARTS_IN_ROMSTAGE))
64 return ENV_ROMSTAGE;
65 else if (CONFIG(VBOOT_STARTS_IN_BOOTBLOCK))
66 return ENV_BOOTBLOCK;
67 else
68 dead_code();
69}
70
71static inline int verstage_should_load(void)
72{
73 if (CONFIG(VBOOT_SEPARATE_VERSTAGE))
74 return ENV_BOOTBLOCK;
75 else
76 return 0;
77}
78
79static inline int vboot_logic_executed(void)
80{
81 extern int vboot_executed; /* should not be globally accessible */
82
83 /* If we are in the stage that runs verification, or in the stage that
84 both loads the verstage and is returned to from it afterwards, we
Elyes HAOUAS58f3fd62020-01-05 13:14:06 +010085 need to check a global to see if verification has run. */
Julius Werner998dc172019-05-09 14:16:13 -070086 if (verification_should_run() ||
87 (verstage_should_load() && CONFIG(VBOOT_RETURN_FROM_VERSTAGE)))
Arthur Heymans344e86b2019-11-20 19:47:10 +010088 return vboot_executed;
Julius Werner998dc172019-05-09 14:16:13 -070089
90 if (CONFIG(VBOOT_STARTS_IN_BOOTBLOCK)) {
91 /* All other stages are "after the bootblock" */
92 return !ENV_BOOTBLOCK;
93 } else if (CONFIG(VBOOT_STARTS_IN_ROMSTAGE)) {
94 /* Post-RAM stages are "after the romstage" */
Kyösti Mälkkib590a042019-09-12 17:09:55 +030095 return !ENV_ROMSTAGE_OR_BEFORE;
Julius Werner998dc172019-05-09 14:16:13 -070096 } else {
97 dead_code();
98 }
99}
100
101
Furquan Shaikh2a12e2e2016-07-25 11:48:03 -0700102#endif /* __VBOOT_MISC_H__ */