blob: 68df1406a769ae46f264bab5e36e802522fc495c [file] [log] [blame]
Angel Pons986d50e2020-04-02 23:48:53 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Daisuke Nojiri742fc8d2014-10-10 10:51:06 -07002
Patrick Georgi3e18aca2015-04-29 18:59:04 +02003#include <console/cbmem_console.h>
Vadim Bendeburyc83687d2015-04-10 17:50:11 -07004#include <reset.h>
Jon Murphyd7b8dc92023-09-05 11:36:43 -06005#include <security/tpm/tss_errors.h>
Julius Wernerd618aac2019-11-26 17:58:11 -08006#include <security/vboot/misc.h>
Philipp Deppenwiesefea24292017-10-17 17:02:29 +02007#include <security/vboot/vboot_common.h>
Furquan Shaikh95673af2018-04-25 18:15:44 -07008#include <security/vboot/vbnv.h>
Joel Kitching1a6b5c22019-04-25 19:11:13 +08009#include <vb2_api.h>
Daisuke Nojiri742fc8d2014-10-10 10:51:06 -070010
Sridhar Siricilla89ac87a2020-04-20 18:45:22 +053011#include "antirollback.h"
12
Jakub Czapigad27fff52022-11-17 09:13:31 +000013static void save_secdata(struct vb2_context *ctx)
Sridhar Siricilla89ac87a2020-04-20 18:45:22 +053014{
Jakub Czapigad27fff52022-11-17 09:13:31 +000015 if (ctx->flags & VB2_CONTEXT_SECDATA_FIRMWARE_CHANGED
Jon Murphyd7b8dc92023-09-05 11:36:43 -060016 && (CONFIG(VBOOT_MOCK_SECDATA) || tlcl_lib_init() == TPM_SUCCESS)) {
Sridhar Siricilla89ac87a2020-04-20 18:45:22 +053017 printk(BIOS_INFO, "Saving secdata firmware\n");
18 antirollback_write_space_firmware(ctx);
19 ctx->flags &= ~VB2_CONTEXT_SECDATA_FIRMWARE_CHANGED;
20 }
21
Jakub Czapigad27fff52022-11-17 09:13:31 +000022 if (ctx->flags & VB2_CONTEXT_SECDATA_KERNEL_CHANGED
Jon Murphyd7b8dc92023-09-05 11:36:43 -060023 && (CONFIG(VBOOT_MOCK_SECDATA) || tlcl_lib_init() == TPM_SUCCESS)) {
Sridhar Siricilla89ac87a2020-04-20 18:45:22 +053024 printk(BIOS_INFO, "Saving secdata kernel\n");
25 antirollback_write_space_kernel(ctx);
26 ctx->flags &= ~VB2_CONTEXT_SECDATA_KERNEL_CHANGED;
27 }
Jakub Czapigad27fff52022-11-17 09:13:31 +000028}
29
30void vboot_save_data(struct vb2_context *ctx)
31{
Arthur Heymansa2bc2542021-05-29 08:10:49 +020032 if (!verification_should_run() && !(ENV_RAMINIT && CONFIG(VBOOT_EARLY_EC_SYNC))) {
Jakub Czapigad27fff52022-11-17 09:13:31 +000033 if (ctx->flags
34 & (VB2_CONTEXT_SECDATA_FIRMWARE_CHANGED
35 | VB2_CONTEXT_SECDATA_KERNEL_CHANGED))
36 die("TPM writeback in " ENV_STRING "?");
37 } else {
38 save_secdata(ctx);
39 }
Sridhar Siricilla89ac87a2020-04-20 18:45:22 +053040
41 if (ctx->flags & VB2_CONTEXT_NVDATA_CHANGED) {
42 printk(BIOS_INFO, "Saving nvdata\n");
43 save_vbnv(ctx->nvdata);
44 ctx->flags &= ~VB2_CONTEXT_NVDATA_CHANGED;
45 }
46}
47
Furquan Shaikh95673af2018-04-25 18:15:44 -070048/* Check if it is okay to enable USB Device Controller (UDC). */
49int vboot_can_enable_udc(void)
50{
Wim Vervoorn50337f162020-01-14 16:18:27 +010051 /* Allow UDC in all vboot modes. */
52 if (!CONFIG(CHROMEOS) && CONFIG(VBOOT_ALWAYS_ALLOW_UDC))
53 return 1;
54
Furquan Shaikh95673af2018-04-25 18:15:44 -070055 /* Always disable if not in developer mode */
56 if (!vboot_developer_mode_enabled())
57 return 0;
58 /* Enable if GBB flag is set */
Julius Wernerd618aac2019-11-26 17:58:11 -080059 if (vboot_is_gbb_flag_set(VB2_GBB_FLAG_ENABLE_UDC))
Furquan Shaikh95673af2018-04-25 18:15:44 -070060 return 1;
61 /* Enable if VBNV flag is set */
62 if (vbnv_udc_enable_flag())
63 return 1;
64 /* Otherwise disable */
65 return 0;
66}
67
Furquan Shaikha6c5ddd2016-07-22 06:59:40 -070068/* ============================ VBOOT REBOOT ============================== */
Aaron Durbin64031672018-04-21 14:45:32 -060069void __weak vboot_platform_prepare_reboot(void)
Aaron Durbin5dbefd92016-01-22 16:33:57 -060070{
71}
72
Vadim Bendeburyc83687d2015-04-10 17:50:11 -070073void vboot_reboot(void)
74{
Julius Wernercd49cce2019-03-05 16:53:33 -080075 if (CONFIG(CONSOLE_CBMEM_DUMP_TO_UART))
Raul E Rangel41a1a9e2022-01-11 12:44:38 -070076 cbmem_dump_console_to_uart();
Aaron Durbin5dbefd92016-01-22 16:33:57 -060077 vboot_platform_prepare_reboot();
Nico Huber4f32b642018-10-05 23:40:21 +020078 board_reset();
Vadim Bendeburyc83687d2015-04-10 17:50:11 -070079}
Jakub Czapiga605f7932022-11-04 12:18:04 +000080
81void vboot_save_and_reboot(struct vb2_context *ctx, uint8_t subcode)
82{
83 printk(BIOS_INFO, "vboot: reboot requested (%#x)\n", subcode);
84 vboot_save_data(ctx);
85 vboot_reboot();
86}
87
88void vboot_fail_and_reboot(struct vb2_context *ctx, uint8_t reason, uint8_t subcode)
89{
90 if (reason)
91 vb2api_fail(ctx, reason, subcode);
92
93 vboot_save_and_reboot(ctx, subcode);
94}