Daisuke Nojiri | 742fc8d | 2014-10-10 10:51:06 -0700 | [diff] [blame] | 1 | /* |
| 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 Nojiri | 742fc8d | 2014-10-10 10:51:06 -0700 | [diff] [blame] | 14 | */ |
| 15 | |
Furquan Shaikh | 2a12e2e | 2016-07-25 11:48:03 -0700 | [diff] [blame] | 16 | #ifndef __VBOOT_MISC_H__ |
| 17 | #define __VBOOT_MISC_H__ |
Daisuke Nojiri | 742fc8d | 2014-10-10 10:51:06 -0700 | [diff] [blame] | 18 | |
Julius Werner | 998dc17 | 2019-05-09 14:16:13 -0700 | [diff] [blame] | 19 | #include <assert.h> |
Philipp Deppenwiese | fea2429 | 2017-10-17 17:02:29 +0200 | [diff] [blame] | 20 | #include <security/vboot/vboot_common.h> |
Joel Kitching | 814c865 | 2020-02-14 13:18:06 +0800 | [diff] [blame] | 21 | #include <vb2_api.h> |
Aaron Durbin | b5a20b2 | 2015-10-06 17:29:03 -0500 | [diff] [blame] | 22 | |
Joel Kitching | 8d0f599 | 2019-03-13 18:10:52 +0800 | [diff] [blame] | 23 | /* |
Joel Kitching | af8471c | 2019-03-13 22:38:07 +0800 | [diff] [blame] | 24 | * Source: security/vboot/common.c |
| 25 | */ |
Joel Kitching | 2332c74 | 2019-10-23 15:01:37 +0800 | [diff] [blame] | 26 | struct vb2_context *vboot_get_context(void); |
Daisuke Nojiri | 742fc8d | 2014-10-10 10:51:06 -0700 | [diff] [blame] | 27 | |
Yu-Ping Wu | aeb652a | 2019-11-14 15:42:25 +0800 | [diff] [blame] | 28 | /* |
| 29 | * Returns 1 if firmware slot A is used, 0 if slot B is used. |
| 30 | */ |
Julius Werner | f8e1764 | 2019-12-12 13:23:06 -0800 | [diff] [blame] | 31 | static inline int vboot_is_firmware_slot_a(struct vb2_context *ctx) |
Yu-Ping Wu | aeb652a | 2019-11-14 15:42:25 +0800 | [diff] [blame] | 32 | { |
| 33 | return !(ctx->flags & VB2_CONTEXT_FW_SLOT_B); |
| 34 | } |
Daisuke Nojiri | 742fc8d | 2014-10-10 10:51:06 -0700 | [diff] [blame] | 35 | |
Yu-Ping Wu | aeb652a | 2019-11-14 15:42:25 +0800 | [diff] [blame] | 36 | /* |
Julius Werner | d618aac | 2019-11-26 17:58:11 -0800 | [diff] [blame] | 37 | * 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 | */ |
| 42 | static 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 Wu | aeb652a | 2019-11-14 15:42:25 +0800 | [diff] [blame] | 48 | * Locates firmware as a region device. Returns 0 on success, -1 on failure. |
| 49 | */ |
Julius Werner | f8e1764 | 2019-12-12 13:23:06 -0800 | [diff] [blame] | 50 | int vboot_locate_firmware(struct vb2_context *ctx, struct region_device *fw); |
Joel Kitching | af8471c | 2019-03-13 22:38:07 +0800 | [diff] [blame] | 51 | |
| 52 | /* |
Julius Werner | 998dc17 | 2019-05-09 14:16:13 -0700 | [diff] [blame] | 53 | * 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 | |
| 59 | static 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 | |
| 71 | static inline int verstage_should_load(void) |
| 72 | { |
| 73 | if (CONFIG(VBOOT_SEPARATE_VERSTAGE)) |
| 74 | return ENV_BOOTBLOCK; |
| 75 | else |
| 76 | return 0; |
| 77 | } |
| 78 | |
| 79 | static 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 HAOUAS | 58f3fd6 | 2020-01-05 13:14:06 +0100 | [diff] [blame] | 85 | need to check a global to see if verification has run. */ |
Julius Werner | 998dc17 | 2019-05-09 14:16:13 -0700 | [diff] [blame] | 86 | if (verification_should_run() || |
| 87 | (verstage_should_load() && CONFIG(VBOOT_RETURN_FROM_VERSTAGE))) |
Arthur Heymans | 344e86b | 2019-11-20 19:47:10 +0100 | [diff] [blame] | 88 | return vboot_executed; |
Julius Werner | 998dc17 | 2019-05-09 14:16:13 -0700 | [diff] [blame] | 89 | |
| 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älkki | b590a04 | 2019-09-12 17:09:55 +0300 | [diff] [blame] | 95 | return !ENV_ROMSTAGE_OR_BEFORE; |
Julius Werner | 998dc17 | 2019-05-09 14:16:13 -0700 | [diff] [blame] | 96 | } else { |
| 97 | dead_code(); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | |
Furquan Shaikh | 2a12e2e | 2016-07-25 11:48:03 -0700 | [diff] [blame] | 102 | #endif /* __VBOOT_MISC_H__ */ |