blob: 46b78e1dadd77846dfb29426ea70866357ca994a [file] [log] [blame]
Furquan Shaikh6d448e32016-07-22 08:28:57 -07001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright 2016 Google Inc.
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
16#include <assert.h>
Furquan Shaikh2a12e2e2016-07-25 11:48:03 -070017#include <bootmode.h>
Furquan Shaikh85aa1352016-07-22 08:56:43 -070018#include <bootstate.h>
Aaron Durbinb2a5f482016-12-14 14:40:43 -060019#include <cbmem.h>
Furquan Shaikh6d448e32016-07-22 08:28:57 -070020#include <rules.h>
21#include <string.h>
22#include <vb2_api.h>
Furquan Shaikh2a12e2e2016-07-25 11:48:03 -070023#include <vboot/misc.h>
24#include <vboot/vbnv.h>
25#include <vboot/vboot_common.h>
Furquan Shaikh6d448e32016-07-22 08:28:57 -070026
27static int vb2_get_recovery_reason_shared_data(void)
28{
29 /* Shared data does not exist for Ramstage and Post-CAR stage. */
30 if (ENV_RAMSTAGE || ENV_POSTCAR)
31 return 0;
32
33 struct vb2_shared_data *sd = vb2_get_shared_data();
34 assert(sd);
35 return sd->recovery_reason;
36}
37
Furquan Shaikh85aa1352016-07-22 08:56:43 -070038void vb2_save_recovery_reason_vbnv(void)
39{
40 if (!IS_ENABLED(CONFIG_VBOOT_SAVE_RECOVERY_REASON_ON_REBOOT))
41 return;
42
43 int reason = vb2_get_recovery_reason_shared_data();
44 if (!reason)
45 return;
46
47 set_recovery_mode_into_vbnv(reason);
48}
49
50static void vb2_clear_recovery_reason_vbnv(void *unused)
51{
52 if (!IS_ENABLED(CONFIG_VBOOT_SAVE_RECOVERY_REASON_ON_REBOOT))
53 return;
54
55 set_recovery_mode_into_vbnv(0);
56}
57
58/*
59 * Recovery reason stored in VBNV needs to be cleared before the state of VBNV
60 * is backed-up anywhere or jumping to the payload (whichever occurs
61 * first). Currently, vbnv_cmos.c backs up VBNV on POST_DEVICE. Thus, we need to
62 * make sure that the stored recovery reason is cleared off before that
63 * happens.
64 * IMPORTANT: Any reboot occurring after BS_DEV_INIT state will cause loss of
65 * recovery reason on reboot. Until now, we have seen reboots occuring on x86
66 * only in FSP stages which run before BS_DEV_INIT.
67 */
68BOOT_STATE_INIT_ENTRY(BS_DEV_INIT, BS_ON_EXIT,
69 vb2_clear_recovery_reason_vbnv, NULL);
70
Furquan Shaikh6d448e32016-07-22 08:28:57 -070071/*
Furquan Shaikh6d448e32016-07-22 08:28:57 -070072 * Returns 1 if vboot is being used and currently in a stage which might have
73 * already executed vboot verification.
74 */
75static int vboot_possibly_executed(void)
76{
Furquan Shaikh0325dc62016-07-25 13:02:36 -070077 if (!IS_ENABLED(CONFIG_VBOOT))
78 return 0;
79
Furquan Shaikh6d448e32016-07-22 08:28:57 -070080 if (IS_ENABLED(CONFIG_VBOOT_STARTS_IN_BOOTBLOCK)) {
81 if (ENV_BOOTBLOCK && IS_ENABLED(CONFIG_SEPARATE_VERSTAGE))
82 return 0;
83 return 1;
84 }
85
86 if (IS_ENABLED(CONFIG_VBOOT_STARTS_IN_ROMSTAGE)) {
87 if (ENV_BOOTBLOCK)
88 return 0;
89 return 1;
90 }
91
92 return 0;
93}
94
95/*
96 * vb2_check_recovery_request looks up different components to identify if there
97 * is a recovery request and returns appropriate reason code:
98 * 1. Checks if recovery mode is initiated by EC. If yes, returns
99 * VB2_RECOVERY_RO_MANUAL.
100 * 2. Checks if recovery request is present in VBNV and returns the code read
101 * from it.
102 * 3. Checks recovery request in handoff for stages post-cbmem.
103 * 4. For non-CBMEM stages, check if vboot verification is done and look-up
104 * selected region to identify if vboot_refence library has requested recovery
105 * path. If yes, return the reason code from shared data.
106 * 5. If nothing applies, return 0 indicating no recovery request.
107 */
108int vboot_check_recovery_request(void)
109{
110 int reason = 0;
111
112 /* EC-initiated recovery. */
113 if (get_recovery_mode_switch())
114 return VB2_RECOVERY_RO_MANUAL;
115
116 /* Recovery request in VBNV. */
117 if ((reason = get_recovery_mode_from_vbnv()) != 0)
118 return reason;
119
120 /*
121 * Check recovery flag in vboot_handoff for stages post CBMEM coming
122 * online. Since for some stages there is no way to know if cbmem has
123 * already come online, try looking up handoff anyways. If it fails,
124 * flow will fallback to looking up shared data.
125 */
126 if (cbmem_possibly_online() &&
127 ((reason = vboot_handoff_get_recovery_reason()) != 0))
128 return reason;
129
130 /*
131 * For stages where CBMEM might not be online, identify if vboot
132 * verification is already complete and no slot was selected
133 * i.e. recovery path was requested.
134 */
135 if (vboot_possibly_executed() && vb2_logic_executed() &&
136 !vb2_is_slot_selected())
137 return vb2_get_recovery_reason_shared_data();
138
139 return 0;
140}
Furquan Shaikh0325dc62016-07-25 13:02:36 -0700141
142int vboot_recovery_mode_enabled(void)
143{
144 if (!IS_ENABLED(CONFIG_VBOOT))
145 return 0;
146
147 return !!vboot_check_recovery_request();
148}
149
Furquan Shaikh470852b2016-11-05 23:52:08 -0700150int __attribute__((weak)) get_recovery_mode_retrain_switch(void)
151{
152 return 0;
153}
154
155int vboot_recovery_mode_memory_retrain(void)
156{
157 return get_recovery_mode_retrain_switch();
158}
159
Furquan Shaikh0325dc62016-07-25 13:02:36 -0700160int vboot_developer_mode_enabled(void)
161{
162 if (!IS_ENABLED(CONFIG_VBOOT))
163 return 0;
164
165 if (get_developer_mode_switch())
166 return 1;
167
168 if (cbmem_possibly_online() && vboot_handoff_check_developer_flag())
169 return 1;
170
171 return 0;
172}