blob: 1791dc71e05baf60028679723ecc1ba4df693d80 [file] [log] [blame]
Randall Spangler7f436692013-02-05 12:42:36 -08001/* Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
2 * Use of this source code is governed by a BSD-style license that can be
3 * found in the LICENSE file.
4 *
5 * Tests for vboot_api_kernel, part 2
6 */
7
Bill Richardson0c3ba242013-03-29 11:09:30 -07008#include <stdint.h>
Randall Spangler7f436692013-02-05 12:42:36 -08009#include <stdio.h>
10#include <stdlib.h>
11
Joel Kitchingde2cae62019-05-21 12:20:38 +080012#include "2common.h"
Randall Spanglere4136dc2016-10-27 14:34:59 -070013#include "2misc.h"
14#include "2nvstorage.h"
Randall Spangler7f436692013-02-05 12:42:36 -080015#include "host_common.h"
16#include "load_kernel_fw.h"
17#include "rollback_index.h"
18#include "test_common.h"
19#include "vboot_audio.h"
20#include "vboot_common.h"
Randall Spanglere4136dc2016-10-27 14:34:59 -070021#include "vboot_display.h"
Randall Spangler7f436692013-02-05 12:42:36 -080022#include "vboot_kernel.h"
Randall Spangler7f436692013-02-05 12:42:36 -080023#include "vboot_struct.h"
Julius Werner52fa8c12019-05-07 12:59:47 -070024#include "vboot_test.h"
Randall Spangler7f436692013-02-05 12:42:36 -080025
26/* Mock data */
Randall Spangler7f436692013-02-05 12:42:36 -080027static uint8_t shared_data[VB_SHARED_DATA_MIN_SIZE];
28static VbSharedDataHeader *shared = (VbSharedDataHeader *)shared_data;
Randall Spangler7f436692013-02-05 12:42:36 -080029static LoadKernelParams lkp;
Randall Spanglere4136dc2016-10-27 14:34:59 -070030static uint8_t workbuf[VB2_KERNEL_WORKBUF_RECOMMENDED_SIZE];
31static struct vb2_context ctx;
Randall Spangler51421322017-12-14 15:43:17 -080032static struct vb2_shared_data *sd;
Joel Kitchingde2cae62019-05-21 12:20:38 +080033static struct vb2_gbb_header gbb;
Randall Spangler7f436692013-02-05 12:42:36 -080034
Randall Spangler7f436692013-02-05 12:42:36 -080035static int audio_looping_calls_left;
36static uint32_t vbtlk_retval;
37static int vbexlegacy_called;
Matt Delco3bfaab12019-02-13 11:13:27 -080038static enum VbAltFwIndex_t altfw_num;
Matt Delcob6f4def2019-02-13 12:58:58 -080039static uint64_t current_ticks;
Randall Spangler7f436692013-02-05 12:42:36 -080040static int trust_ec;
41static int virtdev_set;
42static uint32_t virtdev_retval;
Mathew King9d6dc0d2019-02-11 10:27:59 -070043static uint32_t mock_keypress[16];
Luigi Semenzatoa53a0b02014-01-10 16:26:08 -080044static uint32_t mock_keyflags[8];
Randall Spangler7f436692013-02-05 12:42:36 -080045static uint32_t mock_keypress_count;
Matt Delco28c21d92019-04-23 13:37:26 -070046
47#define GPIO_SHUTDOWN 1
48#define GPIO_PRESENCE 2
49#define GPIO_LID_CLOSED 4
50typedef struct GpioState {
51 uint32_t gpio_flags;
52 uint32_t count;
53} GpioState;
54struct GpioState mock_gpio[8];
55static uint32_t mock_gpio_count;
56
Randall Spangler7f436692013-02-05 12:42:36 -080057static uint32_t screens_displayed[8];
58static uint32_t screens_count = 0;
59static uint32_t mock_num_disks[8];
60static uint32_t mock_num_disks_count;
Matt Delcob6f4def2019-02-13 12:58:58 -080061static int tpm_set_mode_called;
62static enum vb2_tpm_mode tpm_mode;
Randall Spangler7f436692013-02-05 12:42:36 -080063
Mathew King9d6dc0d2019-02-11 10:27:59 -070064static char set_vendor_data[32];
65static int set_vendor_data_called;
66
Matt Delco28c21d92019-04-23 13:37:26 -070067/*
68 * Mocks the assertion of 1 or more gpios in |gpio_flags| for 100 ticks after
69 * an optional |ticks| delay.
70 */
71static void MockGpioAfter(uint32_t ticks, uint32_t gpio_flags)
72{
73 uint32_t index = 0;
74 if (ticks > 0)
75 mock_gpio[index++].count = ticks - 1;
76
77 mock_gpio[index].gpio_flags = gpio_flags;
78 mock_gpio[index].count = 100;
79}
80
Randall Spangler7f436692013-02-05 12:42:36 -080081/* Reset mock data (for use before each test) */
82static void ResetMocks(void)
83{
Matt Delco28c21d92019-04-23 13:37:26 -070084 vb2_init_ui();
Randall Spangler664096b2016-10-13 16:16:41 -070085 memset(VbApiKernelGetFwmp(), 0, sizeof(struct RollbackSpaceFwmp));
Randall Spangler946abf12016-04-15 14:49:40 -070086
Randall Spangler664096b2016-10-13 16:16:41 -070087 memset(&shared_data, 0, sizeof(shared_data));
Randall Spangler7f436692013-02-05 12:42:36 -080088 VbSharedDataInit(shared, sizeof(shared_data));
89
Randall Spangler664096b2016-10-13 16:16:41 -070090 memset(&lkp, 0, sizeof(lkp));
Randall Spangler7f436692013-02-05 12:42:36 -080091
Randall Spanglere4136dc2016-10-27 14:34:59 -070092 memset(&ctx, 0, sizeof(ctx));
93 ctx.workbuf = workbuf;
94 ctx.workbuf_size = sizeof(workbuf);
95 vb2_init_context(&ctx);
96 vb2_nv_init(&ctx);
Randall Spangler79c1c612018-01-03 13:42:40 -080097
Randall Spangler51421322017-12-14 15:43:17 -080098 sd = vb2_get_sd(&ctx);
Randall Spangler79c1c612018-01-03 13:42:40 -080099 sd->vbsd = shared;
Joel Kitching43f6cab2019-05-10 12:17:18 +0800100 sd->flags |= VB2_SD_FLAG_DISPLAY_AVAILABLE;
Randall Spanglere4136dc2016-10-27 14:34:59 -0700101
Joel Kitchingde2cae62019-05-21 12:20:38 +0800102 memset(&gbb, 0, sizeof(gbb));
103
Randall Spangler7f436692013-02-05 12:42:36 -0800104 audio_looping_calls_left = 30;
105 vbtlk_retval = 1000;
106 vbexlegacy_called = 0;
Matt Delco3bfaab12019-02-13 11:13:27 -0800107 altfw_num = -100;
Matt Delcob6f4def2019-02-13 12:58:58 -0800108 current_ticks = 0;
Randall Spangler7f436692013-02-05 12:42:36 -0800109 trust_ec = 0;
110 virtdev_set = 0;
111 virtdev_retval = 0;
Mathew King9d6dc0d2019-02-11 10:27:59 -0700112 set_vendor_data_called = 0;
Randall Spangler7f436692013-02-05 12:42:36 -0800113
Randall Spangler664096b2016-10-13 16:16:41 -0700114 memset(screens_displayed, 0, sizeof(screens_displayed));
Randall Spangler7f436692013-02-05 12:42:36 -0800115 screens_count = 0;
116
Randall Spangler664096b2016-10-13 16:16:41 -0700117 memset(mock_keypress, 0, sizeof(mock_keypress));
118 memset(mock_keyflags, 0, sizeof(mock_keyflags));
Randall Spangler7f436692013-02-05 12:42:36 -0800119 mock_keypress_count = 0;
Matt Delco28c21d92019-04-23 13:37:26 -0700120 memset(mock_gpio, 0, sizeof(mock_gpio));
121 mock_gpio_count = 0;
Randall Spangler664096b2016-10-13 16:16:41 -0700122 memset(mock_num_disks, 0, sizeof(mock_num_disks));
Randall Spangler7f436692013-02-05 12:42:36 -0800123 mock_num_disks_count = 0;
Matt Delcob6f4def2019-02-13 12:58:58 -0800124
125 tpm_set_mode_called = 0;
126 tpm_mode = VB2_TPM_MODE_ENABLED_TENTATIVE;
Randall Spangler7f436692013-02-05 12:42:36 -0800127}
128
129/* Mock functions */
Joel Kitchingd0dae802019-08-22 16:32:05 +0800130uint32_t RollbackKernelLock(int recovery_mode)
131{
132 return TPM_SUCCESS;
133}
134
Joel Kitchingde2cae62019-05-21 12:20:38 +0800135struct vb2_gbb_header *vb2_get_gbb(struct vb2_context *c)
136{
137 return &gbb;
138}
Randall Spangler7f436692013-02-05 12:42:36 -0800139
140uint32_t VbExIsShutdownRequested(void)
141{
Matt Delco28c21d92019-04-23 13:37:26 -0700142 uint32_t result = 0;
143 if (mock_gpio_count >= ARRAY_SIZE(mock_gpio))
144 return 0;
145 if (mock_gpio[mock_gpio_count].gpio_flags & GPIO_SHUTDOWN)
146 result |= VB_SHUTDOWN_REQUEST_POWER_BUTTON;
147 if (mock_gpio[mock_gpio_count].gpio_flags & GPIO_LID_CLOSED)
148 result |= VB_SHUTDOWN_REQUEST_LID_CLOSED;
149 if (mock_gpio[mock_gpio_count].count > 0) {
150 --mock_gpio[mock_gpio_count].count;
151 } else {
152 ++mock_gpio_count;
Edward Hill00d4be62018-10-01 17:42:17 -0600153 }
Matt Delco28c21d92019-04-23 13:37:26 -0700154 return result;
Randall Spangler7f436692013-02-05 12:42:36 -0800155}
156
157uint32_t VbExKeyboardRead(void)
158{
Luigi Semenzatoa53a0b02014-01-10 16:26:08 -0800159 return VbExKeyboardReadWithFlags(NULL);
160}
161
162uint32_t VbExKeyboardReadWithFlags(uint32_t *key_flags)
163{
164 if (mock_keypress_count < ARRAY_SIZE(mock_keypress)) {
Jacob Garber7cdd8792019-08-13 14:50:16 -0600165 if (key_flags != NULL &&
166 mock_keypress_count < ARRAY_SIZE(mock_keyflags))
Luigi Semenzatoa53a0b02014-01-10 16:26:08 -0800167 *key_flags = mock_keyflags[mock_keypress_count];
Randall Spangler7f436692013-02-05 12:42:36 -0800168 return mock_keypress[mock_keypress_count++];
Luigi Semenzatoa53a0b02014-01-10 16:26:08 -0800169 } else
170 return 0;
171}
172
173uint32_t VbExGetSwitches(uint32_t request_mask)
174{
Matt Delco28c21d92019-04-23 13:37:26 -0700175 uint32_t result = 0;
176 if (mock_gpio_count >= ARRAY_SIZE(mock_gpio))
Randall Spangler7f436692013-02-05 12:42:36 -0800177 return 0;
Matt Delco28c21d92019-04-23 13:37:26 -0700178 if ((request_mask & VB_SWITCH_FLAG_PHYS_PRESENCE_PRESSED) &&
179 (mock_gpio[mock_gpio_count].gpio_flags & GPIO_PRESENCE))
180 result |= VB_SWITCH_FLAG_PHYS_PRESENCE_PRESSED;
181 if (mock_gpio[mock_gpio_count].count > 0) {
182 --mock_gpio[mock_gpio_count].count;
183 } else {
184 ++mock_gpio_count;
185 }
186 return result;
Randall Spangler7f436692013-02-05 12:42:36 -0800187}
188
Joel Kitchinge6700f42019-07-31 14:12:30 +0800189vb2_error_t VbExLegacy(enum VbAltFwIndex_t _altfw_num)
Randall Spangler7f436692013-02-05 12:42:36 -0800190{
191 vbexlegacy_called++;
Simon Glass89a51772018-04-08 23:26:57 -0400192 altfw_num = _altfw_num;
193
Matt Delcob6f4def2019-02-13 12:58:58 -0800194 /* VbExLegacy() can only return failure, or not return at all. */
Joel Kitching9908a9a2019-07-29 18:53:38 +0800195 return VB2_ERROR_UNKNOWN;
Matt Delcob6f4def2019-02-13 12:58:58 -0800196}
197
198void VbExSleepMs(uint32_t msec)
199{
200 current_ticks += (uint64_t)msec * VB_USEC_PER_MSEC;
201}
202
203uint64_t VbExGetTimer(void)
204{
205 return current_ticks;
Randall Spangler7f436692013-02-05 12:42:36 -0800206}
207
Joel Kitching90671fa2019-07-31 13:17:08 +0800208vb2_error_t VbExDiskGetInfo(VbDiskInfo **infos_ptr, uint32_t *count,
209 uint32_t disk_flags)
Randall Spangler7f436692013-02-05 12:42:36 -0800210{
211 if (mock_num_disks_count < ARRAY_SIZE(mock_num_disks)) {
212 if (mock_num_disks[mock_num_disks_count] == -1)
Joel Kitching166a07d2019-07-29 18:55:59 +0800213 return VB2_ERROR_MOCK;
Randall Spangler7f436692013-02-05 12:42:36 -0800214 else
215 *count = mock_num_disks[mock_num_disks_count++];
216 } else {
217 *count = 0;
218 }
Joel Kitchingcf49e7b2019-07-29 18:51:00 +0800219 return VB2_SUCCESS;
Randall Spangler7f436692013-02-05 12:42:36 -0800220}
221
Joel Kitching90671fa2019-07-31 13:17:08 +0800222vb2_error_t VbExDiskFreeInfo(VbDiskInfo *infos,
223 VbExDiskHandle_t preserve_handle)
Randall Spangler7f436692013-02-05 12:42:36 -0800224{
Joel Kitchingcf49e7b2019-07-29 18:51:00 +0800225 return VB2_SUCCESS;
Randall Spangler7f436692013-02-05 12:42:36 -0800226}
227
Randall Spanglere778ada2014-07-15 16:14:21 -0700228int VbExTrustEC(int devidx)
Randall Spangler7f436692013-02-05 12:42:36 -0800229{
230 return trust_ec;
231}
232
Randall Spanglerde818cc2017-12-12 14:05:19 -0800233int vb2_audio_looping(void)
Randall Spangler7f436692013-02-05 12:42:36 -0800234{
235 if (audio_looping_calls_left == 0)
236 return 0;
237 else if (audio_looping_calls_left > 0)
238 audio_looping_calls_left--;
239
240 return 1;
241}
242
Joel Kitchinge6700f42019-07-31 14:12:30 +0800243vb2_error_t VbTryLoadKernel(struct vb2_context *c, uint32_t get_info_flags)
Randall Spangler7f436692013-02-05 12:42:36 -0800244{
245 return vbtlk_retval + get_info_flags;
246}
247
Joel Kitching90671fa2019-07-31 13:17:08 +0800248vb2_error_t VbDisplayScreen(struct vb2_context *c, uint32_t screen, int force,
249 const VbScreenData *data)
Randall Spangler7f436692013-02-05 12:42:36 -0800250{
251 if (screens_count < ARRAY_SIZE(screens_displayed))
252 screens_displayed[screens_count++] = screen;
253
Joel Kitchingcf49e7b2019-07-29 18:51:00 +0800254 return VB2_SUCCESS;
Randall Spangler7f436692013-02-05 12:42:36 -0800255}
256
Joel Kitchinge6700f42019-07-31 14:12:30 +0800257vb2_error_t SetVirtualDevMode(int val)
Randall Spangler7f436692013-02-05 12:42:36 -0800258{
259 virtdev_set = val;
260 return virtdev_retval;
261}
262
Joel Kitching90671fa2019-07-31 13:17:08 +0800263vb2_error_t VbExSetVendorData(const char *vendor_data_value)
Mathew King9d6dc0d2019-02-11 10:27:59 -0700264{
265 set_vendor_data_called = 1;
266 strncpy(set_vendor_data, vendor_data_value, sizeof(set_vendor_data));
267
Joel Kitchingcf49e7b2019-07-29 18:51:00 +0800268 return VB2_SUCCESS;
Mathew King9d6dc0d2019-02-11 10:27:59 -0700269}
270
Joel Kitchinge6700f42019-07-31 14:12:30 +0800271vb2_error_t vb2ex_tpm_set_mode(enum vb2_tpm_mode mode_val)
Matt Delcob6f4def2019-02-13 12:58:58 -0800272{
273 tpm_set_mode_called = 1;
274 /*
275 * This mock will pretend that any call will fail if the tpm is
276 * already disabled (e.g., as if the code always tries to contact the
277 * tpm to issue a command). The real version may eventually be changed
278 * to return success if the incoming request is also to disable, but
279 * the point here is to have a way to simulate failure.
280 */
281 if (tpm_mode == VB2_TPM_MODE_DISABLED) {
282 return VB2_ERROR_UNKNOWN;
283 }
284 tpm_mode = mode_val;
285 return VB2_SUCCESS;
286}
287
Randall Spangler7f436692013-02-05 12:42:36 -0800288/* Tests */
289
Matt Delco28c21d92019-04-23 13:37:26 -0700290/*
291 * Helper function to test VbUserConfirms against a sequence of gpio events.
292 * caller specifies a sequence of gpio events and the expected confirm vs.
293 * reboot result.
294 *
295 * Non-asserted gpios are used for 5 events, then 'first' for 5 events,
296 * 'second' for 5 events, and 'third' for 5 events.
297 * Typically most tests want 5 events of each type (so they'll specify 0 for
298 * 'first' and use 'second' through 'fourth'), but a few tests want the
299 * shutdown event to be seen once.
300 */
301static void VbUserConfirmsTestGpio(uint32_t first, uint32_t second,
302 uint32_t third, uint32_t confirm,
303 const char *msg)
304{
305 ResetMocks();
306 mock_gpio[0].gpio_flags = 0;
307 mock_gpio[0].count = 4;
308 mock_gpio[1].gpio_flags = first;
309 mock_gpio[1].count = 4;
310 mock_gpio[2].gpio_flags = second;
311 mock_gpio[2].count = 4;
312 mock_gpio[3].gpio_flags = third;
313 mock_gpio[3].count = 4;
314 if (confirm) {
315 TEST_EQ(VbUserConfirms(&ctx,
316 VB_CONFIRM_SPACE_MEANS_NO |
317 VB_CONFIRM_MUST_TRUST_KEYBOARD),
318 1, msg);
319 } else {
320 TEST_EQ(VbUserConfirms(&ctx,
321 VB_CONFIRM_SPACE_MEANS_NO |
322 VB_CONFIRM_MUST_TRUST_KEYBOARD),
323 -1, msg);
324 }
325}
326
Randall Spangler7f436692013-02-05 12:42:36 -0800327static void VbUserConfirmsTest(void)
328{
329 printf("Testing VbUserConfirms()...\n");
330
331 ResetMocks();
Matt Delco28c21d92019-04-23 13:37:26 -0700332 MockGpioAfter(1, GPIO_SHUTDOWN);
Randall Spangler98616d72018-01-04 16:08:47 -0800333 TEST_EQ(VbUserConfirms(&ctx, 0), -1, "Shutdown requested");
Randall Spangler7f436692013-02-05 12:42:36 -0800334
335 ResetMocks();
Daisuke Nojirieb13c062017-12-06 09:30:08 -0800336 mock_keypress[0] = VB_BUTTON_POWER_SHORT_PRESS;
Randall Spangler98616d72018-01-04 16:08:47 -0800337 TEST_EQ(VbUserConfirms(&ctx, 0), -1, "Shutdown requested");
Daisuke Nojirieb13c062017-12-06 09:30:08 -0800338
339 ResetMocks();
Mathew Kingaf26dc12019-02-19 10:43:32 -0700340 mock_keypress[0] = VB_KEY_ENTER;
Randall Spangler98616d72018-01-04 16:08:47 -0800341 TEST_EQ(VbUserConfirms(&ctx, 0), 1, "Enter");
Randall Spangler7f436692013-02-05 12:42:36 -0800342
343 ResetMocks();
Simon Glass4d4c36e2018-10-17 08:41:53 -0600344 mock_keypress[0] = VB_KEY_ESC;
Randall Spangler98616d72018-01-04 16:08:47 -0800345 TEST_EQ(VbUserConfirms(&ctx, 0), 0, "Esc");
Randall Spangler7f436692013-02-05 12:42:36 -0800346
347 ResetMocks();
348 mock_keypress[0] = ' ';
Matt Delco28c21d92019-04-23 13:37:26 -0700349 MockGpioAfter(1, GPIO_SHUTDOWN);
Randall Spangler98616d72018-01-04 16:08:47 -0800350 TEST_EQ(VbUserConfirms(&ctx, VB_CONFIRM_SPACE_MEANS_NO), 0,
Joel Kitching9ad8a412018-08-02 16:21:17 +0800351 "Space means no");
Randall Spangler7f436692013-02-05 12:42:36 -0800352
353 ResetMocks();
354 mock_keypress[0] = ' ';
Matt Delco28c21d92019-04-23 13:37:26 -0700355 MockGpioAfter(1, GPIO_SHUTDOWN);
Randall Spangler98616d72018-01-04 16:08:47 -0800356 TEST_EQ(VbUserConfirms(&ctx, 0), -1, "Space ignored");
Randall Spangler7f436692013-02-05 12:42:36 -0800357
Luigi Semenzatoa53a0b02014-01-10 16:26:08 -0800358 ResetMocks();
Mathew Kingaf26dc12019-02-19 10:43:32 -0700359 mock_keypress[0] = VB_KEY_ENTER;
Luigi Semenzatoa53a0b02014-01-10 16:26:08 -0800360 mock_keyflags[0] = VB_KEY_FLAG_TRUSTED_KEYBOARD;
Randall Spangler98616d72018-01-04 16:08:47 -0800361 TEST_EQ(VbUserConfirms(&ctx, VB_CONFIRM_MUST_TRUST_KEYBOARD),
Luigi Semenzatoa53a0b02014-01-10 16:26:08 -0800362 1, "Enter with trusted keyboard");
363
364 ResetMocks();
Mathew Kingaf26dc12019-02-19 10:43:32 -0700365 mock_keypress[0] = VB_KEY_ENTER; /* untrusted */
Luigi Semenzatoa53a0b02014-01-10 16:26:08 -0800366 mock_keypress[1] = ' ';
Randall Spangler98616d72018-01-04 16:08:47 -0800367 TEST_EQ(VbUserConfirms(&ctx,
Luigi Semenzatoa53a0b02014-01-10 16:26:08 -0800368 VB_CONFIRM_SPACE_MEANS_NO |
369 VB_CONFIRM_MUST_TRUST_KEYBOARD),
370 0, "Untrusted keyboard");
371
372 ResetMocks();
Matt Delco28c21d92019-04-23 13:37:26 -0700373 MockGpioAfter(0, GPIO_PRESENCE);
Randall Spangler98616d72018-01-04 16:08:47 -0800374 TEST_EQ(VbUserConfirms(&ctx,
Luigi Semenzatoa53a0b02014-01-10 16:26:08 -0800375 VB_CONFIRM_SPACE_MEANS_NO |
376 VB_CONFIRM_MUST_TRUST_KEYBOARD),
Matt Delco28c21d92019-04-23 13:37:26 -0700377 1, "Presence button");
378
379 /*
380 * List of possiblities for shutdown and physical presence events that
381 * occur over time. Time advanced from left to right (where each
382 * represents the gpio[s] that are seen during a given iteration of
383 * the loop). The meaning of the characters:
384 *
385 * _ means no gpio
386 * s means shutdown gpio
387 * p means presence gpio
388 * B means both shutdown and presence gpio
389 *
390 * 1: ______ppp______ -> confirm
391 * 2: ______sss______ -> shutdown
392 * 3: ___pppsss______ -> confirm
393 * 4: ___sssppp______ -> shutdown
394 * 5: ___pppBBB______ -> confirm
395 * 6: ___pppBBBppp___ -> shutdown
396 * 7: ___pppBBBsss___ -> confirm
397 * 8: ___sssBBB______ -> confirm
398 * 9: ___sssBBBppp___ -> shutdown
399 * 10: ___sssBBBsss___ -> confirm
400 * 11: ______BBB______ -> confirm
401 * 12: ______BBBsss___ -> confirm
402 * 13: ______BBBppp___ -> shutdown
403 */
404
405 /* 1: presence means confirm */
406 VbUserConfirmsTestGpio(GPIO_PRESENCE, 0, 0, 1, "presence");
407
408 /* 2: shutdown means shutdown */
409 VbUserConfirmsTestGpio(GPIO_SHUTDOWN, 0, 0, 0, "shutdown");
410
411 /* 3: presence then shutdown means confirm */
412 VbUserConfirmsTestGpio(GPIO_PRESENCE, GPIO_SHUTDOWN, 0, 1,
413 "presence then shutdown");
414
415 /* 4: shutdown then presence means shutdown */
416 VbUserConfirmsTestGpio(GPIO_SHUTDOWN, GPIO_PRESENCE, 0, 0,
417 "shutdown then presence");
418
419 /* 5: presence then shutdown+presence then none mean confirm */
420 VbUserConfirmsTestGpio(GPIO_PRESENCE, GPIO_PRESENCE | GPIO_SHUTDOWN,
421 0, 1, "presence, both, none");
422
423 /* 6: presence then shutdown+presence then presence means shutdown */
424 VbUserConfirmsTestGpio(GPIO_PRESENCE, GPIO_PRESENCE | GPIO_SHUTDOWN,
425 GPIO_PRESENCE, 0, "presence, both, presence");
426
427 /* 7: presence then shutdown+presence then shutdown means confirm */
428 VbUserConfirmsTestGpio(GPIO_PRESENCE, GPIO_PRESENCE | GPIO_SHUTDOWN,
429 GPIO_SHUTDOWN, 1, "presence, both, shutdown");
430
431 /* 8: shutdown then shutdown+presence then none means confirm */
432 VbUserConfirmsTestGpio(GPIO_SHUTDOWN, GPIO_PRESENCE | GPIO_SHUTDOWN,
433 0, 1, "shutdown, both, none");
434
435 /* 9: shutdown then shutdown+presence then presence means shutdown */
436 VbUserConfirmsTestGpio(GPIO_SHUTDOWN, GPIO_PRESENCE | GPIO_SHUTDOWN,
437 GPIO_PRESENCE, 0, "shutdown, both, presence");
438
439 /* 10: shutdown then shutdown+presence then shutdown means confirm */
440 VbUserConfirmsTestGpio(GPIO_SHUTDOWN, GPIO_PRESENCE | GPIO_SHUTDOWN,
441 GPIO_SHUTDOWN, 1, "shutdown, both, shutdown");
442
443 /* 11: shutdown+presence then none means confirm */
444 VbUserConfirmsTestGpio(GPIO_PRESENCE | GPIO_SHUTDOWN, 0, 0, 1,
445 "both");
446
447 /* 12: shutdown+presence then shutdown means confirm */
448 VbUserConfirmsTestGpio(GPIO_PRESENCE | GPIO_SHUTDOWN,
449 GPIO_SHUTDOWN, 0, 1, "both, shutdown");
450
451 /* 13: shutdown+presence then presence means shutdown */
452 VbUserConfirmsTestGpio(GPIO_PRESENCE | GPIO_SHUTDOWN,
453 GPIO_PRESENCE, 0, 0, "both, presence");
Luigi Semenzatoa53a0b02014-01-10 16:26:08 -0800454
455 ResetMocks();
Mathew Kingaf26dc12019-02-19 10:43:32 -0700456 mock_keypress[0] = VB_KEY_ENTER;
Luigi Semenzatoa53a0b02014-01-10 16:26:08 -0800457 mock_keypress[1] = 'y';
458 mock_keypress[2] = 'z';
459 mock_keypress[3] = ' ';
Matt Delco28c21d92019-04-23 13:37:26 -0700460 mock_gpio[0].gpio_flags = GPIO_PRESENCE;
461 mock_gpio[0].count = ~0;
Randall Spangler98616d72018-01-04 16:08:47 -0800462 TEST_EQ(VbUserConfirms(&ctx,
Luigi Semenzatoa53a0b02014-01-10 16:26:08 -0800463 VB_CONFIRM_SPACE_MEANS_NO |
464 VB_CONFIRM_MUST_TRUST_KEYBOARD),
465 0, "Recovery button stuck");
Randall Spangler7f436692013-02-05 12:42:36 -0800466 printf("...done.\n");
467}
468
469static void VbBootTest(void)
470{
471 ResetMocks();
Sheng-Liang Song487a54b2014-07-26 21:34:28 -0700472 VbExEcEnteringMode(0, VB_EC_NORMAL);
Randall Spanglerb07b4b92018-01-04 17:58:01 -0800473 TEST_EQ(VbBootNormal(&ctx), 1002, "VbBootNormal()");
Sheng-Liang Song487a54b2014-07-26 21:34:28 -0700474 TEST_EQ(VbGetMode(), VB_EC_NORMAL, "vboot_mode normal");
Joel Kitchingefd0dc22019-05-29 16:25:05 +0800475
476 ResetMocks();
477 vb2_nv_set(&ctx, VB2_NV_DISPLAY_REQUEST, 1);
478 TEST_EQ(VbBootNormal(&ctx), VBERROR_REBOOT_REQUIRED,
479 "VbBootNormal() reboot to reset NVRAM display request");
480 TEST_EQ(vb2_nv_get(&ctx, VB2_NV_DISPLAY_REQUEST), 0,
481 " display request reset");
482
483 ResetMocks();
484 vb2_nv_set(&ctx, VB2_NV_DIAG_REQUEST, 1);
485 TEST_EQ(VbBootNormal(&ctx), VBERROR_REBOOT_REQUIRED,
486 "VbBootNormal() reboot to reset NVRAM diag request");
487 TEST_EQ(vb2_nv_get(&ctx, VB2_NV_DIAG_REQUEST), 0,
488 " diag request reset");
Randall Spangler7f436692013-02-05 12:42:36 -0800489}
490
491static void VbBootDevTest(void)
492{
Simon Glass89a51772018-04-08 23:26:57 -0400493 int key;
494
Randall Spangler7f436692013-02-05 12:42:36 -0800495 printf("Testing VbBootDeveloper()...\n");
496
497 /* Proceed after timeout */
498 ResetMocks();
Sheng-Liang Song487a54b2014-07-26 21:34:28 -0700499 VbExEcEnteringMode(0, VB_EC_DEVELOPER);
Randall Spanglerb07b4b92018-01-04 17:58:01 -0800500 TEST_EQ(VbBootDeveloper(&ctx), 1002, "Timeout");
Sheng-Liang Song487a54b2014-07-26 21:34:28 -0700501 TEST_EQ(VbGetMode(), VB_EC_DEVELOPER, "vboot_mode developer");
Randall Spangler7f436692013-02-05 12:42:36 -0800502 TEST_EQ(screens_displayed[0], VB_SCREEN_DEVELOPER_WARNING,
503 " warning screen");
Randall Spanglere4136dc2016-10-27 14:34:59 -0700504 TEST_EQ(vb2_nv_get(&ctx, VB2_NV_RECOVERY_REQUEST), 0,
505 " recovery reason");
Randall Spangler7f436692013-02-05 12:42:36 -0800506 TEST_EQ(audio_looping_calls_left, 0, " used up audio");
507
Randall Spanglerf2a1dc02013-06-11 16:53:07 -0700508 /* Proceed to legacy after timeout if GBB flag set */
509 ResetMocks();
Joel Kitchingde2cae62019-05-21 12:20:38 +0800510 gbb.flags |= VB2_GBB_FLAG_DEFAULT_DEV_BOOT_LEGACY |
Randall Spangler51421322017-12-14 15:43:17 -0800511 VB2_GBB_FLAG_FORCE_DEV_BOOT_LEGACY;
Randall Spanglerb07b4b92018-01-04 17:58:01 -0800512 TEST_EQ(VbBootDeveloper(&ctx), 1002, "Timeout");
Randall Spanglerf2a1dc02013-06-11 16:53:07 -0700513 TEST_EQ(vbexlegacy_called, 1, " try legacy");
Simon Glass89a51772018-04-08 23:26:57 -0400514 TEST_EQ(altfw_num, 0, " check altfw_num");
515
516 /* Proceed to legacy after timeout if GBB flag set */
517 ResetMocks();
Joel Kitchingde2cae62019-05-21 12:20:38 +0800518 gbb.flags |= VB2_GBB_FLAG_DEFAULT_DEV_BOOT_LEGACY |
Simon Glass89a51772018-04-08 23:26:57 -0400519 VB2_GBB_FLAG_FORCE_DEV_BOOT_LEGACY;
520 TEST_EQ(VbBootDeveloper(&ctx), 1002, "Timeout");
521 TEST_EQ(vbexlegacy_called, 1, " try legacy");
522 TEST_EQ(altfw_num, 0, " check altfw_num");
Randall Spanglerf2a1dc02013-06-11 16:53:07 -0700523
Mary Ruthven12a55f22015-10-06 10:42:31 -0700524 /* Proceed to legacy after timeout if boot legacy and default boot
525 * legacy are set */
526 ResetMocks();
Randall Spanglere4136dc2016-10-27 14:34:59 -0700527 vb2_nv_set(&ctx, VB2_NV_DEV_DEFAULT_BOOT,
Randall Spanglerdff58522017-11-27 15:37:13 -0800528 VB2_DEV_DEFAULT_BOOT_LEGACY);
Randall Spanglere4136dc2016-10-27 14:34:59 -0700529 vb2_nv_set(&ctx, VB2_NV_DEV_BOOT_LEGACY, 1);
Randall Spanglerb07b4b92018-01-04 17:58:01 -0800530 TEST_EQ(VbBootDeveloper(&ctx), 1002, "Timeout");
Mary Ruthven12a55f22015-10-06 10:42:31 -0700531 TEST_EQ(vbexlegacy_called, 1, " try legacy");
Simon Glass89a51772018-04-08 23:26:57 -0400532 TEST_EQ(altfw_num, 0, " check altfw_num");
Mary Ruthven12a55f22015-10-06 10:42:31 -0700533
534 /* Proceed to legacy boot mode only if enabled */
535 ResetMocks();
Randall Spanglere4136dc2016-10-27 14:34:59 -0700536 vb2_nv_set(&ctx, VB2_NV_DEV_DEFAULT_BOOT,
Randall Spanglerdff58522017-11-27 15:37:13 -0800537 VB2_DEV_DEFAULT_BOOT_LEGACY);
Randall Spanglerb07b4b92018-01-04 17:58:01 -0800538 TEST_EQ(VbBootDeveloper(&ctx), 1002, "Timeout");
Mary Ruthven12a55f22015-10-06 10:42:31 -0700539 TEST_EQ(vbexlegacy_called, 0, " not legacy");
540
541 /* Proceed to usb after timeout if boot usb and default boot
542 * usb are set */
543 ResetMocks();
Randall Spanglere4136dc2016-10-27 14:34:59 -0700544 vb2_nv_set(&ctx, VB2_NV_DEV_DEFAULT_BOOT,
Randall Spanglerdff58522017-11-27 15:37:13 -0800545 VB2_DEV_DEFAULT_BOOT_USB);
Randall Spanglere4136dc2016-10-27 14:34:59 -0700546 vb2_nv_set(&ctx, VB2_NV_DEV_BOOT_USB, 1);
Joel Kitchingcf49e7b2019-07-29 18:51:00 +0800547 vbtlk_retval = VB2_SUCCESS - VB_DISK_FLAG_REMOVABLE;
Randall Spanglerb07b4b92018-01-04 17:58:01 -0800548 TEST_EQ(VbBootDeveloper(&ctx), 0, "Ctrl+U USB");
Mary Ruthven12a55f22015-10-06 10:42:31 -0700549
550 /* Proceed to usb boot mode only if enabled */
551 ResetMocks();
Randall Spanglere4136dc2016-10-27 14:34:59 -0700552 vb2_nv_set(&ctx, VB2_NV_DEV_DEFAULT_BOOT,
Randall Spanglerdff58522017-11-27 15:37:13 -0800553 VB2_DEV_DEFAULT_BOOT_USB);
Randall Spanglerb07b4b92018-01-04 17:58:01 -0800554 TEST_EQ(VbBootDeveloper(&ctx), 1002, "Timeout");
Mary Ruthven12a55f22015-10-06 10:42:31 -0700555
556 /* If no USB tries fixed disk */
557 ResetMocks();
Randall Spanglere4136dc2016-10-27 14:34:59 -0700558 vb2_nv_set(&ctx, VB2_NV_DEV_BOOT_USB, 1);
559 vb2_nv_set(&ctx, VB2_NV_DEV_DEFAULT_BOOT,
Randall Spanglerdff58522017-11-27 15:37:13 -0800560 VB2_DEV_DEFAULT_BOOT_USB);
Randall Spanglerb07b4b92018-01-04 17:58:01 -0800561 TEST_EQ(VbBootDeveloper(&ctx), 1002, "Ctrl+U enabled");
Mary Ruthven12a55f22015-10-06 10:42:31 -0700562 TEST_EQ(vbexlegacy_called, 0, " not legacy");
563
Randall Spangler7f436692013-02-05 12:42:36 -0800564 /* Up arrow is uninteresting / passed to VbCheckDisplayKey() */
565 ResetMocks();
566 mock_keypress[0] = VB_KEY_UP;
Randall Spanglerb07b4b92018-01-04 17:58:01 -0800567 TEST_EQ(VbBootDeveloper(&ctx), 1002, "Up arrow");
Randall Spangler7f436692013-02-05 12:42:36 -0800568
569 /* Shutdown requested in loop */
570 ResetMocks();
Matt Delco28c21d92019-04-23 13:37:26 -0700571 mock_gpio[0].gpio_flags = 0;
572 mock_gpio[0].count = 2;
573 mock_gpio[1].gpio_flags = GPIO_SHUTDOWN;
Randall Spanglerb07b4b92018-01-04 17:58:01 -0800574 TEST_EQ(VbBootDeveloper(&ctx),
Randall Spanglere4136dc2016-10-27 14:34:59 -0700575 VBERROR_SHUTDOWN_REQUESTED,
Randall Spangler7f436692013-02-05 12:42:36 -0800576 "Shutdown requested");
577 TEST_NEQ(audio_looping_calls_left, 0, " aborts audio");
578
Daisuke Nojirieb13c062017-12-06 09:30:08 -0800579 /* Shutdown requested by keyboard in loop */
580 ResetMocks();
581 mock_keypress[0] = VB_BUTTON_POWER_SHORT_PRESS;
Randall Spanglerb07b4b92018-01-04 17:58:01 -0800582 TEST_EQ(VbBootDeveloper(&ctx),
Daisuke Nojirieb13c062017-12-06 09:30:08 -0800583 VBERROR_SHUTDOWN_REQUESTED,
584 "Shutdown requested by keyboard");
585
Randall Spangler7f436692013-02-05 12:42:36 -0800586 /* Space goes straight to recovery if no virtual dev switch */
587 ResetMocks();
588 mock_keypress[0] = ' ';
Randall Spanglerb07b4b92018-01-04 17:58:01 -0800589 TEST_EQ(VbBootDeveloper(&ctx),
Randall Spanglere4136dc2016-10-27 14:34:59 -0700590 VBERROR_LOAD_KERNEL_RECOVERY,
Randall Spangler7f436692013-02-05 12:42:36 -0800591 "Space = recovery");
Randall Spanglere4136dc2016-10-27 14:34:59 -0700592 TEST_EQ(vb2_nv_get(&ctx, VB2_NV_RECOVERY_REQUEST),
Randall Spanglerdff58522017-11-27 15:37:13 -0800593 VB2_RECOVERY_RW_DEV_SCREEN, " recovery reason");
Randall Spangler7f436692013-02-05 12:42:36 -0800594
595 /* Space asks to disable virtual dev switch */
596 ResetMocks();
Joel Kitchinge1761d62019-03-18 16:36:36 +0800597 shared->flags = VBSD_BOOT_DEV_SWITCH_ON;
Randall Spangler7f436692013-02-05 12:42:36 -0800598 mock_keypress[0] = ' ';
Mathew Kingaf26dc12019-02-19 10:43:32 -0700599 mock_keypress[1] = VB_KEY_ENTER;
Randall Spanglerb07b4b92018-01-04 17:58:01 -0800600 TEST_EQ(VbBootDeveloper(&ctx), VBERROR_REBOOT_REQUIRED,
Randall Spangler7f436692013-02-05 12:42:36 -0800601 "Space = tonorm");
602 TEST_EQ(screens_displayed[0], VB_SCREEN_DEVELOPER_WARNING,
603 " warning screen");
604 TEST_EQ(screens_displayed[1], VB_SCREEN_DEVELOPER_TO_NORM,
605 " tonorm screen");
606 TEST_EQ(screens_displayed[2], VB_SCREEN_TO_NORM_CONFIRMED,
607 " confirm screen");
Randall Spanglere4136dc2016-10-27 14:34:59 -0700608 TEST_EQ(vb2_nv_get(&ctx, VB2_NV_DISABLE_DEV_REQUEST), 1,
609 " disable dev request");
Randall Spangler7f436692013-02-05 12:42:36 -0800610
611 /* Space-space doesn't disable it */
612 ResetMocks();
Joel Kitchinge1761d62019-03-18 16:36:36 +0800613 shared->flags = VBSD_BOOT_DEV_SWITCH_ON;
Randall Spangler7f436692013-02-05 12:42:36 -0800614 mock_keypress[0] = ' ';
615 mock_keypress[1] = ' ';
Simon Glass4d4c36e2018-10-17 08:41:53 -0600616 mock_keypress[2] = VB_KEY_ESC;
Randall Spanglerb07b4b92018-01-04 17:58:01 -0800617 TEST_EQ(VbBootDeveloper(&ctx), 1002, "Space-space");
Randall Spangler7f436692013-02-05 12:42:36 -0800618 TEST_EQ(screens_displayed[0], VB_SCREEN_DEVELOPER_WARNING,
619 " warning screen");
620 TEST_EQ(screens_displayed[1], VB_SCREEN_DEVELOPER_TO_NORM,
621 " tonorm screen");
622 TEST_EQ(screens_displayed[2], VB_SCREEN_DEVELOPER_WARNING,
623 " warning screen");
624
625 /* Enter doesn't by default */
626 ResetMocks();
Joel Kitchinge1761d62019-03-18 16:36:36 +0800627 shared->flags = VBSD_BOOT_DEV_SWITCH_ON;
Mathew Kingaf26dc12019-02-19 10:43:32 -0700628 mock_keypress[0] = VB_KEY_ENTER;
629 mock_keypress[1] = VB_KEY_ENTER;
Randall Spanglerb07b4b92018-01-04 17:58:01 -0800630 TEST_EQ(VbBootDeveloper(&ctx), 1002, "Enter ignored");
Randall Spangler7f436692013-02-05 12:42:36 -0800631
632 /* Enter does if GBB flag set */
633 ResetMocks();
Joel Kitchinge1761d62019-03-18 16:36:36 +0800634 shared->flags = VBSD_BOOT_DEV_SWITCH_ON;
Joel Kitchingde2cae62019-05-21 12:20:38 +0800635 gbb.flags |= VB2_GBB_FLAG_ENTER_TRIGGERS_TONORM;
Mathew Kingaf26dc12019-02-19 10:43:32 -0700636 mock_keypress[0] = VB_KEY_ENTER;
637 mock_keypress[1] = VB_KEY_ENTER;
Randall Spanglerb07b4b92018-01-04 17:58:01 -0800638 TEST_EQ(VbBootDeveloper(&ctx), VBERROR_REBOOT_REQUIRED,
Randall Spangler7f436692013-02-05 12:42:36 -0800639 "Enter = tonorm");
640
641 /* Tonorm ignored if GBB forces dev switch on */
642 ResetMocks();
Joel Kitchinge1761d62019-03-18 16:36:36 +0800643 shared->flags = VBSD_BOOT_DEV_SWITCH_ON;
Joel Kitchingde2cae62019-05-21 12:20:38 +0800644 gbb.flags |= VB2_GBB_FLAG_FORCE_DEV_SWITCH_ON;
Randall Spangler7f436692013-02-05 12:42:36 -0800645 mock_keypress[0] = ' ';
Mathew Kingaf26dc12019-02-19 10:43:32 -0700646 mock_keypress[1] = VB_KEY_ENTER;
Randall Spanglerb07b4b92018-01-04 17:58:01 -0800647 TEST_EQ(VbBootDeveloper(&ctx), 1002,
Randall Spanglere4136dc2016-10-27 14:34:59 -0700648 "Can't tonorm gbb-dev");
Randall Spangler7f436692013-02-05 12:42:36 -0800649
650 /* Shutdown requested at tonorm screen */
651 ResetMocks();
Joel Kitchinge1761d62019-03-18 16:36:36 +0800652 shared->flags = VBSD_BOOT_DEV_SWITCH_ON;
Randall Spangler7f436692013-02-05 12:42:36 -0800653 mock_keypress[0] = ' ';
Matt Delco28c21d92019-04-23 13:37:26 -0700654 MockGpioAfter(3, GPIO_SHUTDOWN);
Randall Spanglerb07b4b92018-01-04 17:58:01 -0800655 TEST_EQ(VbBootDeveloper(&ctx),
Randall Spanglere4136dc2016-10-27 14:34:59 -0700656 VBERROR_SHUTDOWN_REQUESTED,
Randall Spangler7f436692013-02-05 12:42:36 -0800657 "Shutdown requested at tonorm");
658 TEST_EQ(screens_displayed[0], VB_SCREEN_DEVELOPER_WARNING,
659 " warning screen");
660 TEST_EQ(screens_displayed[1], VB_SCREEN_DEVELOPER_TO_NORM,
661 " tonorm screen");
662
Daisuke Nojirieb13c062017-12-06 09:30:08 -0800663 /* Shutdown requested by keyboard at tonorm screen */
664 ResetMocks();
Joel Kitchinge1761d62019-03-18 16:36:36 +0800665 shared->flags = VBSD_BOOT_DEV_SWITCH_ON;
Daisuke Nojirieb13c062017-12-06 09:30:08 -0800666 mock_keypress[0] = VB_BUTTON_POWER_SHORT_PRESS;
Randall Spanglerb07b4b92018-01-04 17:58:01 -0800667 TEST_EQ(VbBootDeveloper(&ctx),
Daisuke Nojirieb13c062017-12-06 09:30:08 -0800668 VBERROR_SHUTDOWN_REQUESTED,
669 "Shutdown requested by keyboard at nonorm");
670
Randall Spangler7f436692013-02-05 12:42:36 -0800671 /* Ctrl+D dismisses warning */
672 ResetMocks();
Mathew Kingaf26dc12019-02-19 10:43:32 -0700673 mock_keypress[0] = VB_KEY_CTRL('D');
Randall Spanglerb07b4b92018-01-04 17:58:01 -0800674 TEST_EQ(VbBootDeveloper(&ctx), 1002, "Ctrl+D");
Randall Spanglere4136dc2016-10-27 14:34:59 -0700675 TEST_EQ(vb2_nv_get(&ctx, VB2_NV_RECOVERY_REQUEST), 0,
676 " recovery reason");
Randall Spangler7f436692013-02-05 12:42:36 -0800677 TEST_NEQ(audio_looping_calls_left, 0, " aborts audio");
Randall Spanglerf2a1dc02013-06-11 16:53:07 -0700678 TEST_EQ(vbexlegacy_called, 0, " not legacy");
679
680 /* Ctrl+D doesn't boot legacy even if GBB flag is set */
681 ResetMocks();
Mathew Kingaf26dc12019-02-19 10:43:32 -0700682 mock_keypress[0] = VB_KEY_CTRL('D');
Joel Kitchingde2cae62019-05-21 12:20:38 +0800683 gbb.flags |= VB2_GBB_FLAG_DEFAULT_DEV_BOOT_LEGACY;
Randall Spanglerb07b4b92018-01-04 17:58:01 -0800684 TEST_EQ(VbBootDeveloper(&ctx), 1002, "Ctrl+D");
Randall Spanglerf2a1dc02013-06-11 16:53:07 -0700685 TEST_EQ(vbexlegacy_called, 0, " not legacy");
Randall Spangler7f436692013-02-05 12:42:36 -0800686
687 /* Ctrl+L tries legacy boot mode only if enabled */
688 ResetMocks();
Mathew Kingaf26dc12019-02-19 10:43:32 -0700689 mock_keypress[0] = VB_KEY_CTRL('L');
Randall Spanglerb07b4b92018-01-04 17:58:01 -0800690 TEST_EQ(VbBootDeveloper(&ctx), 1002, "Ctrl+L normal");
Randall Spangler7f436692013-02-05 12:42:36 -0800691 TEST_EQ(vbexlegacy_called, 0, " not legacy");
692
Simon Glassa0ae3182018-10-16 19:36:48 -0600693 /* Enter altfw menu and time out */
694 ResetMocks();
Matt Delco28c21d92019-04-23 13:37:26 -0700695 MockGpioAfter(1000, GPIO_SHUTDOWN);
Joel Kitchingde2cae62019-05-21 12:20:38 +0800696 gbb.flags |= VB2_GBB_FLAG_FORCE_DEV_BOOT_LEGACY;
Mathew Kingaf26dc12019-02-19 10:43:32 -0700697 mock_keypress[0] = VB_KEY_CTRL('L');
Simon Glassa0ae3182018-10-16 19:36:48 -0600698 TEST_EQ(VbBootDeveloper(&ctx), VBERROR_SHUTDOWN_REQUESTED,
699 "Ctrl+L force legacy");
700 TEST_EQ(vbexlegacy_called, 0, " try legacy");
701
702 /* Enter altfw menu and select firmware 0 */
Randall Spangler7f436692013-02-05 12:42:36 -0800703 ResetMocks();
Joel Kitchingde2cae62019-05-21 12:20:38 +0800704 gbb.flags |= VB2_GBB_FLAG_FORCE_DEV_BOOT_LEGACY;
Mathew Kingaf26dc12019-02-19 10:43:32 -0700705 mock_keypress[0] = VB_KEY_CTRL('L');
Simon Glassa0ae3182018-10-16 19:36:48 -0600706 mock_keypress[1] = '0';
Randall Spanglerb07b4b92018-01-04 17:58:01 -0800707 TEST_EQ(VbBootDeveloper(&ctx), 1002,
Randall Spanglere4136dc2016-10-27 14:34:59 -0700708 "Ctrl+L force legacy");
Randall Spangler7f436692013-02-05 12:42:36 -0800709 TEST_EQ(vbexlegacy_called, 1, " try legacy");
Simon Glass89a51772018-04-08 23:26:57 -0400710 TEST_EQ(altfw_num, 0, " check altfw_num");
Randall Spangler7f436692013-02-05 12:42:36 -0800711
Simon Glassa0ae3182018-10-16 19:36:48 -0600712 /* Enter altfw menu and then exit it */
Randall Spangler7f436692013-02-05 12:42:36 -0800713 ResetMocks();
Randall Spanglere4136dc2016-10-27 14:34:59 -0700714 vb2_nv_set(&ctx, VB2_NV_DEV_BOOT_LEGACY, 1);
Mathew Kingaf26dc12019-02-19 10:43:32 -0700715 mock_keypress[0] = VB_KEY_CTRL('L');
Simon Glassa0ae3182018-10-16 19:36:48 -0600716 mock_keypress[1] = VB_KEY_ESC;
717 TEST_EQ(VbBootDeveloper(&ctx), 1002,
718 "Ctrl+L nv legacy");
719 TEST_EQ(vbexlegacy_called, 0, " try legacy");
720
721 /* Enter altfw menu and select firmware 0 */
722 ResetMocks();
723 vb2_nv_set(&ctx, VB2_NV_DEV_BOOT_LEGACY, 1);
Mathew Kingaf26dc12019-02-19 10:43:32 -0700724 mock_keypress[0] = VB_KEY_CTRL('L');
Simon Glassa0ae3182018-10-16 19:36:48 -0600725 mock_keypress[1] = '0';
Randall Spanglerb07b4b92018-01-04 17:58:01 -0800726 TEST_EQ(VbBootDeveloper(&ctx), 1002,
Randall Spanglere4136dc2016-10-27 14:34:59 -0700727 "Ctrl+L nv legacy");
Randall Spangler7f436692013-02-05 12:42:36 -0800728 TEST_EQ(vbexlegacy_called, 1, " try legacy");
Simon Glass89a51772018-04-08 23:26:57 -0400729 TEST_EQ(altfw_num, 0, " check altfw_num");
Randall Spangler7f436692013-02-05 12:42:36 -0800730
Simon Glassa0ae3182018-10-16 19:36:48 -0600731 /* Enter altfw menu and select firmware 0 */
Randall Spangler946abf12016-04-15 14:49:40 -0700732 ResetMocks();
733 VbApiKernelGetFwmp()->flags |= FWMP_DEV_ENABLE_LEGACY;
Mathew Kingaf26dc12019-02-19 10:43:32 -0700734 mock_keypress[0] = VB_KEY_CTRL('L');
Simon Glassa0ae3182018-10-16 19:36:48 -0600735 mock_keypress[1] = '0';
Randall Spanglerb07b4b92018-01-04 17:58:01 -0800736 TEST_EQ(VbBootDeveloper(&ctx), 1002,
Randall Spanglere4136dc2016-10-27 14:34:59 -0700737 "Ctrl+L fwmp legacy");
Randall Spangler946abf12016-04-15 14:49:40 -0700738 TEST_EQ(vbexlegacy_called, 1, " fwmp legacy");
Simon Glass89a51772018-04-08 23:26:57 -0400739 TEST_EQ(altfw_num, 0, " check altfw_num");
740
741 /* Pressing 1-9 boots alternative firmware only if enabled */
742 for (key = '1'; key <= '9'; key++) {
743 ResetMocks();
744 mock_keypress[0] = key;
745 TEST_EQ(VbBootDeveloper(&ctx), 1002, "'1' normal");
746 TEST_EQ(vbexlegacy_called, 0, " not legacy");
747
748 ResetMocks();
Joel Kitchingde2cae62019-05-21 12:20:38 +0800749 gbb.flags |= VB2_GBB_FLAG_FORCE_DEV_BOOT_LEGACY;
Simon Glass89a51772018-04-08 23:26:57 -0400750 mock_keypress[0] = key;
751 TEST_EQ(VbBootDeveloper(&ctx), 1002,
752 "Ctrl+L force legacy");
753 TEST_EQ(vbexlegacy_called, 1, " try legacy");
754 TEST_EQ(altfw_num, key - '0', " check altfw_num");
755
756 ResetMocks();
757 vb2_nv_set(&ctx, VB2_NV_DEV_BOOT_LEGACY, 1);
758 mock_keypress[0] = key;
759 TEST_EQ(VbBootDeveloper(&ctx), 1002,
760 "Ctrl+L nv legacy");
761 TEST_EQ(vbexlegacy_called, 1, " try legacy");
762 TEST_EQ(altfw_num, key - '0', " check altfw_num");
763
764 ResetMocks();
765 VbApiKernelGetFwmp()->flags |= FWMP_DEV_ENABLE_LEGACY;
766 mock_keypress[0] = key;
767 TEST_EQ(VbBootDeveloper(&ctx), 1002,
768 "Ctrl+L fwmp legacy");
769 TEST_EQ(vbexlegacy_called, 1, " fwmp legacy");
770 TEST_EQ(altfw_num, key - '0', " check altfw_num");
771 }
Randall Spangler946abf12016-04-15 14:49:40 -0700772
Randall Spangler7f436692013-02-05 12:42:36 -0800773 /* Ctrl+U boots USB only if enabled */
774 ResetMocks();
Mathew Kingaf26dc12019-02-19 10:43:32 -0700775 mock_keypress[0] = VB_KEY_CTRL('U');
Randall Spanglerb07b4b92018-01-04 17:58:01 -0800776 TEST_EQ(VbBootDeveloper(&ctx), 1002, "Ctrl+U normal");
Randall Spangler7f436692013-02-05 12:42:36 -0800777
778 /* Ctrl+U enabled, with good USB boot */
779 ResetMocks();
Randall Spanglere4136dc2016-10-27 14:34:59 -0700780 vb2_nv_set(&ctx, VB2_NV_DEV_BOOT_USB, 1);
Mathew Kingaf26dc12019-02-19 10:43:32 -0700781 mock_keypress[0] = VB_KEY_CTRL('U');
Joel Kitchingcf49e7b2019-07-29 18:51:00 +0800782 vbtlk_retval = VB2_SUCCESS - VB_DISK_FLAG_REMOVABLE;
Randall Spanglerb07b4b92018-01-04 17:58:01 -0800783 TEST_EQ(VbBootDeveloper(&ctx), 0, "Ctrl+U USB");
Randall Spangler7f436692013-02-05 12:42:36 -0800784
785 /* Ctrl+U enabled via GBB */
786 ResetMocks();
Joel Kitchingde2cae62019-05-21 12:20:38 +0800787 gbb.flags |= VB2_GBB_FLAG_FORCE_DEV_BOOT_USB;
Mathew Kingaf26dc12019-02-19 10:43:32 -0700788 mock_keypress[0] = VB_KEY_CTRL('U');
Joel Kitchingcf49e7b2019-07-29 18:51:00 +0800789 vbtlk_retval = VB2_SUCCESS - VB_DISK_FLAG_REMOVABLE;
Randall Spanglerb07b4b92018-01-04 17:58:01 -0800790 TEST_EQ(VbBootDeveloper(&ctx), 0, "Ctrl+U force USB");
Randall Spangler7f436692013-02-05 12:42:36 -0800791
Randall Spangler946abf12016-04-15 14:49:40 -0700792 /* Ctrl+U enabled via FWMP */
793 ResetMocks();
794 VbApiKernelGetFwmp()->flags |= FWMP_DEV_ENABLE_USB;
Mathew Kingaf26dc12019-02-19 10:43:32 -0700795 mock_keypress[0] = VB_KEY_CTRL('U');
Joel Kitchingcf49e7b2019-07-29 18:51:00 +0800796 vbtlk_retval = VB2_SUCCESS - VB_DISK_FLAG_REMOVABLE;
Randall Spanglerb07b4b92018-01-04 17:58:01 -0800797 TEST_EQ(VbBootDeveloper(&ctx), 0, "Ctrl+U force USB");
Randall Spangler946abf12016-04-15 14:49:40 -0700798
Mathew King9d6dc0d2019-02-11 10:27:59 -0700799 /* Ctrl+S set vendor data and reboot */
800 ResetMocks();
801 ctx.flags |= VB2_CONTEXT_VENDOR_DATA_SETTABLE;
802 mock_keypress[0] = VB_KEY_CTRL('S');
803 mock_keypress[1] = '4';
804 mock_keypress[2] = '3';
805 mock_keypress[3] = '2';
806 mock_keypress[4] = '1';
807 mock_keypress[5] = VB_KEY_ENTER; // Set vendor data
808 mock_keypress[6] = VB_KEY_ENTER; // Confirm vendor data
809 TEST_EQ(VbBootDeveloper(&ctx), VBERROR_REBOOT_REQUIRED,
810 "Ctrl+S set vendor data and reboot");
811 TEST_EQ(set_vendor_data_called, 1, " VbExSetVendorData() called");
812 TEST_STR_EQ(set_vendor_data, "4321", " Vendor data correct");
813
814 /* Ctrl+S extra keys ignored */
815 ResetMocks();
816 ctx.flags |= VB2_CONTEXT_VENDOR_DATA_SETTABLE;
817 mock_keypress[0] = VB_KEY_CTRL('S');
818 mock_keypress[1] = '4';
819 mock_keypress[2] = '3';
820 mock_keypress[3] = '2';
821 mock_keypress[4] = '1';
822 mock_keypress[5] = '5';
823 mock_keypress[6] = VB_KEY_ENTER; // Set vendor data
824 mock_keypress[7] = VB_KEY_ENTER; // Confirm vendor data
825 TEST_EQ(VbBootDeveloper(&ctx), VBERROR_REBOOT_REQUIRED,
826 "Ctrl+S extra keys ignored");
827 TEST_EQ(set_vendor_data_called, 1, " VbExSetVendorData() called");
828 TEST_STR_EQ(set_vendor_data, "4321", " Vendor data correct");
829
830 /* Ctrl+S converts case */
831 ResetMocks();
832 ctx.flags |= VB2_CONTEXT_VENDOR_DATA_SETTABLE;
833 mock_keypress[0] = VB_KEY_CTRL('S');
834 mock_keypress[1] = 'a';
835 mock_keypress[2] = 'B';
836 mock_keypress[3] = 'Y';
837 mock_keypress[4] = 'z';
838 mock_keypress[5] = VB_KEY_ENTER; // Set vendor data
839 mock_keypress[6] = VB_KEY_ENTER; // Confirm vendor data
840 TEST_EQ(VbBootDeveloper(&ctx), VBERROR_REBOOT_REQUIRED,
841 "Ctrl+S converts case");
842 TEST_EQ(set_vendor_data_called, 1, " VbExSetVendorData() called");
843 TEST_STR_EQ(set_vendor_data, "ABYZ", " Vendor data correct");
844
845 /* Ctrl+S backspace works */
846 ResetMocks();
847 ctx.flags |= VB2_CONTEXT_VENDOR_DATA_SETTABLE;
848 mock_keypress[0] = VB_KEY_CTRL('S');
849 mock_keypress[1] = 'A';
850 mock_keypress[2] = 'B';
851 mock_keypress[3] = 'C';
852 mock_keypress[4] = VB_KEY_BACKSPACE;
853 mock_keypress[5] = VB_KEY_BACKSPACE;
854 mock_keypress[6] = '3';
855 mock_keypress[7] = '2';
856 mock_keypress[8] = '1';
857 mock_keypress[9] = VB_KEY_ENTER; // Set vendor data
858 mock_keypress[10] = VB_KEY_ENTER; // Confirm vendor data
859 TEST_EQ(VbBootDeveloper(&ctx), VBERROR_REBOOT_REQUIRED,
860 "Ctrl+S backspace works");
861 TEST_EQ(set_vendor_data_called, 1, " VbExSetVendorData() called");
862 TEST_STR_EQ(set_vendor_data, "A321", " Vendor data correct");
863
864 /* Ctrl+S invalid chars don't print */
865 ResetMocks();
866 ctx.flags |= VB2_CONTEXT_VENDOR_DATA_SETTABLE;
867 mock_keypress[0] = VB_KEY_CTRL('S');
868 mock_keypress[1] = '4';
869 mock_keypress[2] = '-';
870 mock_keypress[3] = '^';
871 mock_keypress[4] = '&';
872 mock_keypress[5] = '$';
873 mock_keypress[6] = '.';
874 mock_keypress[7] = '3';
875 mock_keypress[8] = '2';
876 mock_keypress[9] = '1';
877 mock_keypress[10] = VB_KEY_ENTER; // Set vendor data
878 mock_keypress[11] = VB_KEY_ENTER; // Confirm vendor data
879 TEST_EQ(VbBootDeveloper(&ctx), VBERROR_REBOOT_REQUIRED,
880 "Ctrl+S invalid chars don't print");
881 TEST_EQ(set_vendor_data_called, 1, " VbExSetVendorData() called");
882 TEST_STR_EQ(set_vendor_data, "4321", " Vendor data correct");
883
884 /* Ctrl+S invalid chars don't print with backspace */
885 ResetMocks();
886 ctx.flags |= VB2_CONTEXT_VENDOR_DATA_SETTABLE;
887 mock_keypress[0] = VB_KEY_CTRL('S');
888 mock_keypress[1] = '4';
889 mock_keypress[2] = '-';
890 mock_keypress[3] = VB_KEY_BACKSPACE; // Should delete 4
891 mock_keypress[4] = '3';
892 mock_keypress[5] = '2';
893 mock_keypress[6] = '1';
894 mock_keypress[7] = '0';
895 mock_keypress[8] = VB_KEY_ENTER; // Set vendor data
896 mock_keypress[9] = VB_KEY_ENTER; // Confirm vendor data
897 TEST_EQ(VbBootDeveloper(&ctx), VBERROR_REBOOT_REQUIRED,
898 "Ctrl+S invalid chars don't print with backspace");
899 TEST_EQ(set_vendor_data_called, 1, " VbExSetVendorData() called");
900 TEST_STR_EQ(set_vendor_data, "3210", " Vendor data correct");
901
902 /* Ctrl+S backspace only doesn't underrun */
903 ResetMocks();
904 ctx.flags |= VB2_CONTEXT_VENDOR_DATA_SETTABLE;
905 mock_keypress[0] = VB_KEY_CTRL('S');
906 mock_keypress[1] = 'A';
907 mock_keypress[2] = VB_KEY_BACKSPACE;
908 mock_keypress[3] = VB_KEY_BACKSPACE;
909 mock_keypress[4] = '4';
910 mock_keypress[5] = '3';
911 mock_keypress[6] = '2';
912 mock_keypress[7] = '1';
913 mock_keypress[8] = VB_KEY_ENTER; // Set vendor data
914 mock_keypress[9] = VB_KEY_ENTER; // Confirm vendor data
915 TEST_EQ(VbBootDeveloper(&ctx), VBERROR_REBOOT_REQUIRED,
916 "Ctrl+S backspace only doesn't underrun");
917 TEST_EQ(set_vendor_data_called, 1, " VbExSetVendorData() called");
918 TEST_STR_EQ(set_vendor_data, "4321", " Vendor data correct");
919
Mathew King06a65e72019-03-13 10:24:06 -0600920 /* Ctrl+S vowels not allowed after first char */
921 ResetMocks();
922 ctx.flags |= VB2_CONTEXT_VENDOR_DATA_SETTABLE;
923 mock_keypress[0] = VB_KEY_CTRL('S');
924 mock_keypress[1] = 'A';
925 mock_keypress[2] = 'A';
926 mock_keypress[3] = 'B';
927 mock_keypress[4] = 'E';
928 mock_keypress[5] = 'i';
929 mock_keypress[6] = 'C';
930 mock_keypress[7] = 'O';
931 mock_keypress[8] = 'u';
932 mock_keypress[9] = 'D';
933 mock_keypress[10] = VB_KEY_ENTER; // Set vendor data
934 mock_keypress[11] = VB_KEY_ENTER; // Confirm vendor data
935 TEST_EQ(VbBootDeveloper(&ctx), VBERROR_REBOOT_REQUIRED,
936 "Ctrl+S vowels not allowed after first char");
937 TEST_EQ(set_vendor_data_called, 1, " VbExSetVendorData() called");
938 TEST_STR_EQ(set_vendor_data, "ABCD", " Vendor data correct");
939
Mathew King9d6dc0d2019-02-11 10:27:59 -0700940 /* Ctrl+S too short */
941 ResetMocks();
942 ctx.flags |= VB2_CONTEXT_VENDOR_DATA_SETTABLE;
943 mock_keypress[0] = VB_KEY_CTRL('S');
944 mock_keypress[1] = '1';
945 mock_keypress[2] = '2';
946 mock_keypress[3] = '3';
947 mock_keypress[4] = VB_KEY_ENTER; // Set vendor data (Nothing happens)
948 mock_keypress[5] = VB_KEY_ENTER; // Confirm vendor data (Nothing happens)
949 mock_keypress[6] = VB_KEY_ESC;
950 TEST_EQ(VbBootDeveloper(&ctx), 1002, "Ctrl+S too short");
951 TEST_EQ(set_vendor_data_called, 0, " VbExSetVendorData() not called");
952
953 /* Ctrl+S esc from set screen */
954 ResetMocks();
955 ctx.flags |= VB2_CONTEXT_VENDOR_DATA_SETTABLE;
956 mock_keypress[0] = VB_KEY_CTRL('S');
957 mock_keypress[1] = VB_KEY_ESC;
958 TEST_EQ(VbBootDeveloper(&ctx), 1002, "Ctrl+S esc from set screen");
959 TEST_EQ(set_vendor_data_called, 0, " VbExSetVendorData() not called");
960
961 /* Ctrl+S esc from set screen with tag */
962 ResetMocks();
963 ctx.flags |= VB2_CONTEXT_VENDOR_DATA_SETTABLE;
964 mock_keypress[0] = VB_KEY_CTRL('S');
965 mock_keypress[1] = '4';
966 mock_keypress[2] = '3';
967 mock_keypress[3] = '2';
968 mock_keypress[4] = '1';
969 mock_keypress[5] = VB_KEY_ESC;
970 TEST_EQ(VbBootDeveloper(&ctx), 1002,
971 "Ctrl+S esc from set screen with tag");
972 TEST_EQ(set_vendor_data_called, 0, " VbExSetVendorData() not called");
973
974 /* Ctrl+S esc from confirm screen */
975 ResetMocks();
976 ctx.flags |= VB2_CONTEXT_VENDOR_DATA_SETTABLE;
977 mock_keypress[0] = VB_KEY_CTRL('S');
978 mock_keypress[1] = '4';
979 mock_keypress[2] = '3';
980 mock_keypress[3] = '2';
981 mock_keypress[4] = '1';
982 mock_keypress[5] = VB_KEY_ENTER; // Set vendor data
983 mock_keypress[6] = VB_KEY_ESC;
984 TEST_EQ(VbBootDeveloper(&ctx), 1002, "Ctrl+S esc from set screen");
985 TEST_EQ(set_vendor_data_called, 0, " VbExSetVendorData() not called");
986
Randall Spangler7f436692013-02-05 12:42:36 -0800987 /* If no USB, eventually times out and tries fixed disk */
988 ResetMocks();
Randall Spanglere4136dc2016-10-27 14:34:59 -0700989 vb2_nv_set(&ctx, VB2_NV_DEV_BOOT_USB, 1);
Mathew Kingaf26dc12019-02-19 10:43:32 -0700990 mock_keypress[0] = VB_KEY_CTRL('U');
Randall Spanglerb07b4b92018-01-04 17:58:01 -0800991 TEST_EQ(VbBootDeveloper(&ctx), 1002, "Ctrl+U enabled");
Randall Spangler7f436692013-02-05 12:42:36 -0800992 TEST_EQ(vbexlegacy_called, 0, " not legacy");
Randall Spanglere4136dc2016-10-27 14:34:59 -0700993 TEST_EQ(vb2_nv_get(&ctx, VB2_NV_RECOVERY_REQUEST), 0,
994 " recovery reason");
Randall Spangler7f436692013-02-05 12:42:36 -0800995 TEST_EQ(audio_looping_calls_left, 0, " used up audio");
996
Randall Spangler946abf12016-04-15 14:49:40 -0700997 /* If dev mode is disabled, goes to TONORM screen repeatedly */
998 ResetMocks();
999 VbApiKernelGetFwmp()->flags |= FWMP_DEV_DISABLE_BOOT;
Mathew Kingaf26dc12019-02-19 10:43:32 -07001000 mock_keypress[0] = VB_KEY_ESC; /* Just causes TONORM again */
1001 mock_keypress[1] = VB_KEY_ENTER;
Randall Spanglerb07b4b92018-01-04 17:58:01 -08001002 TEST_EQ(VbBootDeveloper(&ctx), VBERROR_REBOOT_REQUIRED,
Randall Spangler946abf12016-04-15 14:49:40 -07001003 "FWMP dev disabled");
1004 TEST_EQ(screens_displayed[0], VB_SCREEN_DEVELOPER_TO_NORM,
1005 " tonorm screen");
1006 TEST_EQ(screens_displayed[1], VB_SCREEN_DEVELOPER_TO_NORM,
1007 " tonorm screen");
1008 TEST_EQ(screens_displayed[2], VB_SCREEN_TO_NORM_CONFIRMED,
1009 " confirm screen");
Randall Spanglere4136dc2016-10-27 14:34:59 -07001010 TEST_EQ(vb2_nv_get(&ctx, VB2_NV_DISABLE_DEV_REQUEST), 1,
1011 " disable dev request");
Randall Spangler946abf12016-04-15 14:49:40 -07001012
1013 /* Shutdown requested when dev disabled */
1014 ResetMocks();
Joel Kitchinge1761d62019-03-18 16:36:36 +08001015 shared->flags = VBSD_BOOT_DEV_SWITCH_ON;
Randall Spangler946abf12016-04-15 14:49:40 -07001016 VbApiKernelGetFwmp()->flags |= FWMP_DEV_DISABLE_BOOT;
Matt Delco28c21d92019-04-23 13:37:26 -07001017 MockGpioAfter(1, GPIO_SHUTDOWN);
Randall Spanglerb07b4b92018-01-04 17:58:01 -08001018 TEST_EQ(VbBootDeveloper(&ctx),
Randall Spanglere4136dc2016-10-27 14:34:59 -07001019 VBERROR_SHUTDOWN_REQUESTED,
Randall Spangler946abf12016-04-15 14:49:40 -07001020 "Shutdown requested when dev disabled");
1021 TEST_EQ(screens_displayed[0], VB_SCREEN_DEVELOPER_TO_NORM,
1022 " tonorm screen");
1023
Daisuke Nojirieb13c062017-12-06 09:30:08 -08001024 /* Shutdown requested by keyboard when dev disabled */
1025 ResetMocks();
Joel Kitchinge1761d62019-03-18 16:36:36 +08001026 shared->flags = VBSD_BOOT_DEV_SWITCH_ON;
Daisuke Nojirieb13c062017-12-06 09:30:08 -08001027 VbApiKernelGetFwmp()->flags |= FWMP_DEV_DISABLE_BOOT;
1028 mock_keypress[0] = VB_BUTTON_POWER_SHORT_PRESS;
Randall Spanglerb07b4b92018-01-04 17:58:01 -08001029 TEST_EQ(VbBootDeveloper(&ctx),
Daisuke Nojirieb13c062017-12-06 09:30:08 -08001030 VBERROR_SHUTDOWN_REQUESTED,
1031 "Shutdown requested by keyboard when dev disabled");
1032
Randall Spangler7f436692013-02-05 12:42:36 -08001033 printf("...done.\n");
1034}
1035
Matt Delco28c21d92019-04-23 13:37:26 -07001036/*
1037 * Helper function to test VbBootRecovery against a sequence of gpio events.
1038 * caller specifies a sequence of gpio events and the expected confirm vs.
1039 * reboot result.
1040 *
1041 * Non-asserted gpios are used for 5 events, then 'first' for 5 events,
1042 * 'second' for 5 events, and 'third' for 5 events.
1043 */
1044static void VbBootRecTestGpio(uint32_t first, uint32_t second, uint32_t third,
1045 uint32_t confirm, const char *msg)
1046{
1047 ResetMocks();
1048 shared->flags = VBSD_BOOT_REC_SWITCH_ON;
1049 vbtlk_retval = VBERROR_NO_DISK_FOUND - VB_DISK_FLAG_REMOVABLE;
1050 trust_ec = 1;
1051 mock_keypress[0] = VB_KEY_CTRL('D');
1052 mock_gpio[0].gpio_flags = 0;
1053 mock_gpio[0].count = 4;
1054 mock_gpio[1].gpio_flags = first;
1055 mock_gpio[1].count = 4;
1056 mock_gpio[2].gpio_flags = second;
1057 mock_gpio[2].count = 4;
1058 mock_gpio[3].gpio_flags = third;
1059 mock_gpio[3].count = 4;
1060
1061 if (confirm) {
1062 TEST_EQ(VbBootRecovery(&ctx), VBERROR_EC_REBOOT_TO_RO_REQUIRED,
1063 msg);
1064 TEST_EQ(virtdev_set, 1, " virtual dev mode on");
1065 } else {
1066 TEST_EQ(VbBootRecovery(&ctx), VBERROR_SHUTDOWN_REQUESTED, msg);
1067 TEST_EQ(virtdev_set, 0, " virtual dev mode off");
1068 }
1069}
1070
Randall Spangler7f436692013-02-05 12:42:36 -08001071static void VbBootRecTest(void)
1072{
Randall Spangler7f436692013-02-05 12:42:36 -08001073 printf("Testing VbBootRecovery()...\n");
1074
1075 /* Shutdown requested in loop */
1076 ResetMocks();
Matt Delco28c21d92019-04-23 13:37:26 -07001077 MockGpioAfter(10, GPIO_SHUTDOWN);
Sheng-Liang Song487a54b2014-07-26 21:34:28 -07001078 VbExEcEnteringMode(0, VB_EC_RECOVERY);
Randall Spanglerb07b4b92018-01-04 17:58:01 -08001079 TEST_EQ(VbBootRecovery(&ctx),
Randall Spanglere4136dc2016-10-27 14:34:59 -07001080 VBERROR_SHUTDOWN_REQUESTED,
Randall Spangler7f436692013-02-05 12:42:36 -08001081 "Shutdown requested");
Sheng-Liang Song487a54b2014-07-26 21:34:28 -07001082 TEST_EQ(VbGetMode(), VB_EC_RECOVERY, "vboot_mode recovery");
1083
Randall Spanglere4136dc2016-10-27 14:34:59 -07001084 TEST_EQ(vb2_nv_get(&ctx, VB2_NV_RECOVERY_REQUEST), 0,
1085 " recovery reason");
Daisuke Nojiri73a63722015-04-30 12:41:24 -07001086 TEST_EQ(screens_displayed[0], VB_SCREEN_OS_BROKEN,
1087 " broken screen");
Randall Spangler7f436692013-02-05 12:42:36 -08001088
Daisuke Nojirieb13c062017-12-06 09:30:08 -08001089 /* Shutdown requested by keyboard */
1090 ResetMocks();
1091 VbExEcEnteringMode(0, VB_EC_RECOVERY);
1092 mock_keypress[0] = VB_BUTTON_POWER_SHORT_PRESS;
Randall Spanglerb07b4b92018-01-04 17:58:01 -08001093 TEST_EQ(VbBootRecovery(&ctx),
Daisuke Nojirieb13c062017-12-06 09:30:08 -08001094 VBERROR_SHUTDOWN_REQUESTED,
1095 "Shutdown requested by keyboard");
1096
Edward Hill00d4be62018-10-01 17:42:17 -06001097 /* Ignore power button held on boot */
1098 ResetMocks();
Matt Delco28c21d92019-04-23 13:37:26 -07001099 mock_gpio[0].gpio_flags = GPIO_SHUTDOWN;
1100 mock_gpio[0].count = 10;
1101 mock_gpio[1].gpio_flags = 0;
1102 mock_gpio[1].count = 10;
1103 mock_gpio[2].gpio_flags = GPIO_SHUTDOWN;
1104 mock_gpio[2].count = 10;
1105 mock_gpio[3].gpio_flags = 0;
1106 mock_gpio[3].count = 100;
Edward Hill00d4be62018-10-01 17:42:17 -06001107 shared->flags = VBSD_BOOT_REC_SWITCH_ON;
1108 trust_ec = 1;
1109 vbtlk_retval = VBERROR_NO_DISK_FOUND - VB_DISK_FLAG_REMOVABLE;
1110 TEST_EQ(VbBootRecovery(&ctx),
1111 VBERROR_SHUTDOWN_REQUESTED,
1112 "Ignore power button held on boot");
1113 TEST_EQ(screens_displayed[0], VB_SCREEN_RECOVERY_INSERT,
1114 " insert screen");
Matt Delco28c21d92019-04-23 13:37:26 -07001115 /* Shutdown should happen while we're sending the 2nd block of events */
1116 TEST_EQ(mock_gpio_count, 3, " ignore held button");
Edward Hill00d4be62018-10-01 17:42:17 -06001117
Julius Werner5b26e402018-02-16 13:19:42 -08001118 /* Broken screen */
Randall Spangler7f436692013-02-05 12:42:36 -08001119 ResetMocks();
Matt Delco28c21d92019-04-23 13:37:26 -07001120 MockGpioAfter(100, GPIO_SHUTDOWN);
Randall Spangler7f436692013-02-05 12:42:36 -08001121 mock_num_disks[0] = 1;
1122 mock_num_disks[1] = 1;
Shawn Nematbakhsh04171532013-12-13 14:41:47 -08001123 mock_num_disks[2] = 1;
Randall Spangler7f436692013-02-05 12:42:36 -08001124 vbtlk_retval = VBERROR_NO_DISK_FOUND - VB_DISK_FLAG_REMOVABLE;
Randall Spanglerb07b4b92018-01-04 17:58:01 -08001125 TEST_EQ(VbBootRecovery(&ctx),
Randall Spanglere4136dc2016-10-27 14:34:59 -07001126 VBERROR_SHUTDOWN_REQUESTED,
Julius Werner5b26e402018-02-16 13:19:42 -08001127 "Broken");
Daisuke Nojiri73a63722015-04-30 12:41:24 -07001128 TEST_EQ(screens_displayed[0], VB_SCREEN_OS_BROKEN,
1129 " broken screen");
Randall Spangler7f436692013-02-05 12:42:36 -08001130
Julius Werner5b26e402018-02-16 13:19:42 -08001131 /* Broken screen even if dev switch is on */
Randall Spangler7f436692013-02-05 12:42:36 -08001132 ResetMocks();
Matt Delco28c21d92019-04-23 13:37:26 -07001133 MockGpioAfter(100, GPIO_SHUTDOWN);
Randall Spangler7f436692013-02-05 12:42:36 -08001134 mock_num_disks[0] = 1;
1135 mock_num_disks[1] = 1;
1136 shared->flags |= VBSD_BOOT_DEV_SWITCH_ON;
1137 vbtlk_retval = VBERROR_NO_DISK_FOUND - VB_DISK_FLAG_REMOVABLE;
Randall Spanglerb07b4b92018-01-04 17:58:01 -08001138 TEST_EQ(VbBootRecovery(&ctx),
Randall Spanglere4136dc2016-10-27 14:34:59 -07001139 VBERROR_SHUTDOWN_REQUESTED,
Julius Werner5b26e402018-02-16 13:19:42 -08001140 "Broken (dev)");
1141 TEST_EQ(screens_displayed[0], VB_SCREEN_OS_BROKEN,
1142 " broken screen");
Randall Spangler7f436692013-02-05 12:42:36 -08001143
Julius Werner3f896a52018-03-22 14:38:11 -07001144 /* Force insert screen with GBB flag */
1145 ResetMocks();
Matt Delco28c21d92019-04-23 13:37:26 -07001146 MockGpioAfter(100, GPIO_SHUTDOWN);
Joel Kitchingde2cae62019-05-21 12:20:38 +08001147 gbb.flags |= VB2_GBB_FLAG_FORCE_MANUAL_RECOVERY;
Julius Werner3f896a52018-03-22 14:38:11 -07001148 vbtlk_retval = VBERROR_NO_DISK_FOUND - VB_DISK_FLAG_REMOVABLE;
1149 TEST_EQ(VbBootRecovery(&ctx),
1150 VBERROR_SHUTDOWN_REQUESTED,
1151 "Insert (forced by GBB)");
1152 TEST_EQ(screens_displayed[0], VB_SCREEN_RECOVERY_INSERT,
1153 " insert screen");
1154
Randall Spangler7f436692013-02-05 12:42:36 -08001155 /* No removal if recovery button physically pressed */
1156 ResetMocks();
Matt Delco28c21d92019-04-23 13:37:26 -07001157 MockGpioAfter(100, GPIO_SHUTDOWN);
Randall Spangler7f436692013-02-05 12:42:36 -08001158 mock_num_disks[0] = 1;
1159 mock_num_disks[1] = 1;
1160 shared->flags |= VBSD_BOOT_REC_SWITCH_ON;
1161 vbtlk_retval = VBERROR_NO_DISK_FOUND - VB_DISK_FLAG_REMOVABLE;
Randall Spanglerb07b4b92018-01-04 17:58:01 -08001162 TEST_EQ(VbBootRecovery(&ctx),
Randall Spanglere4136dc2016-10-27 14:34:59 -07001163 VBERROR_SHUTDOWN_REQUESTED,
Randall Spangler7f436692013-02-05 12:42:36 -08001164 "No remove in rec");
Daisuke Nojiri95554e42017-09-28 15:53:21 -07001165 TEST_EQ(screens_displayed[0], VB_SCREEN_OS_BROKEN,
1166 " broken screen");
Randall Spangler7f436692013-02-05 12:42:36 -08001167
Shawn Nematbakhsh04171532013-12-13 14:41:47 -08001168 /* Removal if no disk initially found, but found on second attempt */
1169 ResetMocks();
Matt Delco28c21d92019-04-23 13:37:26 -07001170 MockGpioAfter(100, GPIO_SHUTDOWN);
Shawn Nematbakhsh04171532013-12-13 14:41:47 -08001171 mock_num_disks[0] = 0;
1172 mock_num_disks[1] = 1;
1173 vbtlk_retval = VBERROR_NO_DISK_FOUND - VB_DISK_FLAG_REMOVABLE;
Randall Spanglerb07b4b92018-01-04 17:58:01 -08001174 TEST_EQ(VbBootRecovery(&ctx),
Randall Spanglere4136dc2016-10-27 14:34:59 -07001175 VBERROR_SHUTDOWN_REQUESTED,
Shawn Nematbakhsh04171532013-12-13 14:41:47 -08001176 "Remove");
Daisuke Nojiri73a63722015-04-30 12:41:24 -07001177 TEST_EQ(screens_displayed[0], VB_SCREEN_OS_BROKEN,
1178 " broken screen");
Shawn Nematbakhsh04171532013-12-13 14:41:47 -08001179
Randall Spangler7f436692013-02-05 12:42:36 -08001180 /* Bad disk count doesn't require removal */
1181 ResetMocks();
Matt Delco28c21d92019-04-23 13:37:26 -07001182 MockGpioAfter(10, GPIO_SHUTDOWN);
Randall Spangler7f436692013-02-05 12:42:36 -08001183 mock_num_disks[0] = -1;
1184 vbtlk_retval = VBERROR_NO_DISK_FOUND - VB_DISK_FLAG_REMOVABLE;
Randall Spanglerb07b4b92018-01-04 17:58:01 -08001185 TEST_EQ(VbBootRecovery(&ctx),
Randall Spanglere4136dc2016-10-27 14:34:59 -07001186 VBERROR_SHUTDOWN_REQUESTED,
Randall Spangler7f436692013-02-05 12:42:36 -08001187 "Bad disk count");
Daisuke Nojiri73a63722015-04-30 12:41:24 -07001188 TEST_EQ(screens_displayed[0], VB_SCREEN_OS_BROKEN,
1189 " broken screen");
Randall Spangler7f436692013-02-05 12:42:36 -08001190
1191 /* Ctrl+D ignored for many reasons... */
1192 ResetMocks();
Joel Kitchinge1761d62019-03-18 16:36:36 +08001193 shared->flags = VBSD_BOOT_REC_SWITCH_ON;
Matt Delco28c21d92019-04-23 13:37:26 -07001194 MockGpioAfter(100, GPIO_SHUTDOWN);
Mathew Kingaf26dc12019-02-19 10:43:32 -07001195 mock_keypress[0] = VB_KEY_CTRL('D');
Randall Spangler7f436692013-02-05 12:42:36 -08001196 trust_ec = 0;
Randall Spanglerb07b4b92018-01-04 17:58:01 -08001197 TEST_EQ(VbBootRecovery(&ctx),
Randall Spanglere4136dc2016-10-27 14:34:59 -07001198 VBERROR_SHUTDOWN_REQUESTED,
Randall Spangler7f436692013-02-05 12:42:36 -08001199 "Ctrl+D ignored if EC not trusted");
1200 TEST_EQ(virtdev_set, 0, " virtual dev mode off");
1201 TEST_NEQ(screens_displayed[1], VB_SCREEN_RECOVERY_TO_DEV,
1202 " todev screen");
1203
1204 ResetMocks();
Joel Kitchinge1761d62019-03-18 16:36:36 +08001205 shared->flags = VBSD_BOOT_REC_SWITCH_ON | VBSD_BOOT_DEV_SWITCH_ON;
Randall Spangler7f436692013-02-05 12:42:36 -08001206 trust_ec = 1;
Matt Delco28c21d92019-04-23 13:37:26 -07001207 MockGpioAfter(100, GPIO_SHUTDOWN);
Mathew Kingaf26dc12019-02-19 10:43:32 -07001208 mock_keypress[0] = VB_KEY_CTRL('D');
Randall Spanglerb07b4b92018-01-04 17:58:01 -08001209 TEST_EQ(VbBootRecovery(&ctx),
Randall Spanglere4136dc2016-10-27 14:34:59 -07001210 VBERROR_SHUTDOWN_REQUESTED,
Randall Spangler7f436692013-02-05 12:42:36 -08001211 "Ctrl+D ignored if already in dev mode");
1212 TEST_EQ(virtdev_set, 0, " virtual dev mode off");
1213 TEST_NEQ(screens_displayed[1], VB_SCREEN_RECOVERY_TO_DEV,
1214 " todev screen");
1215
1216 ResetMocks();
Randall Spangler7f436692013-02-05 12:42:36 -08001217 trust_ec = 1;
Matt Delco28c21d92019-04-23 13:37:26 -07001218 MockGpioAfter(100, GPIO_SHUTDOWN);
Mathew Kingaf26dc12019-02-19 10:43:32 -07001219 mock_keypress[0] = VB_KEY_CTRL('D');
Randall Spanglerb07b4b92018-01-04 17:58:01 -08001220 TEST_EQ(VbBootRecovery(&ctx),
Randall Spanglere4136dc2016-10-27 14:34:59 -07001221 VBERROR_SHUTDOWN_REQUESTED,
Randall Spangler7f436692013-02-05 12:42:36 -08001222 "Ctrl+D ignored if recovery not manually triggered");
1223 TEST_EQ(virtdev_set, 0, " virtual dev mode off");
1224 TEST_NEQ(screens_displayed[1], VB_SCREEN_RECOVERY_TO_DEV,
1225 " todev screen");
1226
Matt Delco28c21d92019-04-23 13:37:26 -07001227 /* Ctrl+D ignored because the physical presence switch is still pressed
Luigi Semenzatoa53a0b02014-01-10 16:26:08 -08001228 * and we don't like that.
1229 */
1230 ResetMocks();
1231 shared->flags = VBSD_BOOT_REC_SWITCH_ON;
1232 trust_ec = 1;
Mathew Kingaf26dc12019-02-19 10:43:32 -07001233 mock_keypress[0] = VB_KEY_CTRL('D');
Matt Delco28c21d92019-04-23 13:37:26 -07001234 mock_gpio[0].gpio_flags = GPIO_PRESENCE;
1235 mock_gpio[0].count = 100;
1236 mock_gpio[1].gpio_flags = GPIO_PRESENCE | GPIO_SHUTDOWN;
1237 mock_gpio[1].count = 100;
Randall Spanglerb07b4b92018-01-04 17:58:01 -08001238 TEST_EQ(VbBootRecovery(&ctx),
Randall Spanglere4136dc2016-10-27 14:34:59 -07001239 VBERROR_SHUTDOWN_REQUESTED,
Matt Delco28c21d92019-04-23 13:37:26 -07001240 "Ctrl+D ignored if phys pres button is still pressed");
Luigi Semenzatoa53a0b02014-01-10 16:26:08 -08001241 TEST_NEQ(screens_displayed[1], VB_SCREEN_RECOVERY_TO_DEV,
1242 " todev screen");
1243
Randall Spangler7f436692013-02-05 12:42:36 -08001244 /* Ctrl+D then space means don't enable */
1245 ResetMocks();
Joel Kitchinge1761d62019-03-18 16:36:36 +08001246 shared->flags = VBSD_BOOT_REC_SWITCH_ON;
Matt Delco28c21d92019-04-23 13:37:26 -07001247 MockGpioAfter(100, GPIO_SHUTDOWN);
Randall Spangler7f436692013-02-05 12:42:36 -08001248 vbtlk_retval = VBERROR_NO_DISK_FOUND - VB_DISK_FLAG_REMOVABLE;
1249 trust_ec = 1;
Mathew Kingaf26dc12019-02-19 10:43:32 -07001250 mock_keypress[0] = VB_KEY_CTRL('D');
Randall Spangler7f436692013-02-05 12:42:36 -08001251 mock_keypress[1] = ' ';
Randall Spanglerb07b4b92018-01-04 17:58:01 -08001252 TEST_EQ(VbBootRecovery(&ctx),
Randall Spanglere4136dc2016-10-27 14:34:59 -07001253 VBERROR_SHUTDOWN_REQUESTED,
Randall Spangler7f436692013-02-05 12:42:36 -08001254 "Ctrl+D todev abort");
1255 TEST_EQ(screens_displayed[0], VB_SCREEN_RECOVERY_INSERT,
1256 " insert screen");
1257 TEST_EQ(screens_displayed[1], VB_SCREEN_RECOVERY_TO_DEV,
1258 " todev screen");
1259 TEST_EQ(screens_displayed[2], VB_SCREEN_RECOVERY_INSERT,
1260 " insert screen");
1261 TEST_EQ(virtdev_set, 0, " virtual dev mode off");
1262
1263 /* Ctrl+D then enter means enable */
1264 ResetMocks();
Joel Kitchinge1761d62019-03-18 16:36:36 +08001265 shared->flags = VBSD_BOOT_REC_SWITCH_ON;
Matt Delco28c21d92019-04-23 13:37:26 -07001266 MockGpioAfter(100, GPIO_SHUTDOWN);
Randall Spangler7f436692013-02-05 12:42:36 -08001267 vbtlk_retval = VBERROR_NO_DISK_FOUND - VB_DISK_FLAG_REMOVABLE;
1268 trust_ec = 1;
Mathew Kingaf26dc12019-02-19 10:43:32 -07001269 mock_keypress[0] = VB_KEY_CTRL('D');
1270 mock_keypress[1] = VB_KEY_ENTER;
Luigi Semenzatoa53a0b02014-01-10 16:26:08 -08001271 mock_keyflags[1] = VB_KEY_FLAG_TRUSTED_KEYBOARD;
Daisuke Nojiric7293182018-02-07 15:09:30 -08001272 TEST_EQ(VbBootRecovery(&ctx), VBERROR_EC_REBOOT_TO_RO_REQUIRED,
Matt Delco28c21d92019-04-23 13:37:26 -07001273 "Ctrl+D todev confirm via enter");
Randall Spangler7f436692013-02-05 12:42:36 -08001274 TEST_EQ(virtdev_set, 1, " virtual dev mode on");
1275
Matt Delco28c21d92019-04-23 13:37:26 -07001276 /*
1277 * List of possiblities for shutdown and physical presence events that
1278 * occur over time. Time advanced from left to right (where each
1279 * represents the gpio[s] that are seen during a given iteration of
1280 * the loop). The meaning of the characters:
1281 *
1282 * _ means no gpio
1283 * s means shutdown gpio
1284 * p means presence gpio
1285 * B means both shutdown and presence gpio
1286 *
1287 * 1: ______ppp______ -> confirm
1288 * 2: ______sss______ -> shutdown
1289 * 3: ___pppsss______ -> confirm
1290 * 4: ___sssppp______ -> shutdown
1291 * 5: ___pppBBB______ -> confirm
1292 * 6: ___pppBBBppp___ -> shutdown
1293 * 7: ___pppBBBsss___ -> confirm
1294 * 8: ___sssBBB______ -> confirm
1295 * 9: ___sssBBBppp___ -> shutdown
1296 * 10: ___sssBBBsss___ -> confirm
1297 * 11: ______BBB______ -> confirm
1298 * 12: ______BBBsss___ -> confirm
1299 * 13: ______BBBppp___ -> shutdown
1300 */
1301
1302 /* 1: Ctrl+D then presence means enable */
1303 VbBootRecTestGpio(GPIO_PRESENCE, 0, 0, 1,
1304 "Ctrl+D todev confirm via presence");
1305
1306 /* 2: Ctrl+D then shutdown means shutdown */
1307 VbBootRecTestGpio(GPIO_SHUTDOWN, 0, 0, 0,
1308 "Ctrl+D todev then shutdown");
1309
1310 /* 3: Ctrl+D then presence then shutdown means confirm */
1311 VbBootRecTestGpio(GPIO_PRESENCE, GPIO_SHUTDOWN, 0, 1,
1312 "Ctrl+D todev confirm via presence then shutdown");
1313
1314 /* 4: Ctrl+D then 2+ instance shutdown then presence means shutdown */
1315 VbBootRecTestGpio(GPIO_SHUTDOWN, GPIO_PRESENCE, 0, 0,
1316 "Ctrl+D todev then 2+ shutdown then presence");
1317
1318 /* 5: Ctrl+D then presence then shutdown+presence then none */
1319 VbBootRecTestGpio(GPIO_PRESENCE, GPIO_PRESENCE | GPIO_SHUTDOWN, 0, 1,
1320 "Ctrl+D todev confirm via presence, both, none");
1321
1322 /* 6: Ctrl+D then presence then shutdown+presence then presence */
1323 VbBootRecTestGpio(GPIO_PRESENCE, GPIO_PRESENCE | GPIO_SHUTDOWN,
1324 GPIO_PRESENCE, 0,
1325 "Ctrl+D todev confirm via presence, both, presence");
1326
1327 /* 7: Ctrl+D then presence then shutdown+presence then shutdown */
1328 VbBootRecTestGpio(GPIO_PRESENCE, GPIO_PRESENCE | GPIO_SHUTDOWN,
1329 GPIO_SHUTDOWN, 1,
1330 "Ctrl+D todev confirm via presence, both, shutdown");
1331
1332 /* 8: Ctrl+D then shutdown then shutdown+presence then none */
1333 VbBootRecTestGpio(GPIO_SHUTDOWN, GPIO_PRESENCE | GPIO_SHUTDOWN, 0, 1,
1334 "Ctrl+D todev then 2+ shutdown, both, none");
1335
1336 /* 9: Ctrl+D then shutdown then shutdown+presence then presence */
1337 VbBootRecTestGpio(GPIO_SHUTDOWN, GPIO_PRESENCE | GPIO_SHUTDOWN,
1338 GPIO_PRESENCE, 0,
1339 "Ctrl+D todev then 2+ shutdown, both, presence");
1340
1341 /* 10: Ctrl+D then shutdown then shutdown+presence then shutdown */
1342 VbBootRecTestGpio(GPIO_SHUTDOWN, GPIO_PRESENCE | GPIO_SHUTDOWN,
1343 GPIO_SHUTDOWN, 1,
1344 "Ctrl+D todev then 2+ shutdown, both, shutdown");
1345
1346 /* 11: Ctrl+D then shutdown+presence then none */
1347 VbBootRecTestGpio(GPIO_PRESENCE | GPIO_SHUTDOWN, 0, 0, 1,
1348 "Ctrl+D todev confirm via both then none");
1349
1350 /* 12: Ctrl+D then shutdown+presence then shutdown */
1351 VbBootRecTestGpio(GPIO_PRESENCE | GPIO_SHUTDOWN, GPIO_SHUTDOWN, 0, 1,
1352 "Ctrl+D todev confirm via both then shutdown");
1353
1354 /* 13: Ctrl+D then shutdown+presence then presence */
1355 VbBootRecTestGpio(GPIO_PRESENCE | GPIO_SHUTDOWN, GPIO_PRESENCE, 0, 0,
1356 "Ctrl+D todev confirm via both then presence");
1357
Randall Spangler7f436692013-02-05 12:42:36 -08001358 /* Handle TPM error in enabling dev mode */
1359 ResetMocks();
Joel Kitchinge1761d62019-03-18 16:36:36 +08001360 shared->flags = VBSD_BOOT_REC_SWITCH_ON;
Matt Delco28c21d92019-04-23 13:37:26 -07001361 MockGpioAfter(100, GPIO_SHUTDOWN);
Randall Spangler7f436692013-02-05 12:42:36 -08001362 vbtlk_retval = VBERROR_NO_DISK_FOUND - VB_DISK_FLAG_REMOVABLE;
1363 trust_ec = 1;
Mathew Kingaf26dc12019-02-19 10:43:32 -07001364 mock_keypress[0] = VB_KEY_CTRL('D');
1365 mock_keypress[1] = VB_KEY_ENTER;
Luigi Semenzatoa53a0b02014-01-10 16:26:08 -08001366 mock_keyflags[1] = VB_KEY_FLAG_TRUSTED_KEYBOARD;
Joel Kitching166a07d2019-07-29 18:55:59 +08001367 virtdev_retval = VB2_ERROR_MOCK;
Randall Spanglerb07b4b92018-01-04 17:58:01 -08001368 TEST_EQ(VbBootRecovery(&ctx),
Randall Spanglere4136dc2016-10-27 14:34:59 -07001369 VBERROR_TPM_SET_BOOT_MODE_STATE,
Randall Spangler7f436692013-02-05 12:42:36 -08001370 "Ctrl+D todev failure");
1371
Joel Kitching43f6cab2019-05-10 12:17:18 +08001372 /* Test Diagnostic Mode via Ctrl-C - display available */
Matt Delcob6f4def2019-02-13 12:58:58 -08001373 ResetMocks();
1374 shared->flags = VBSD_BOOT_REC_SWITCH_ON;
1375 trust_ec = 1;
Mathew King351c0052019-04-11 11:56:47 -06001376 vbtlk_retval = VBERROR_NO_DISK_FOUND - VB_DISK_FLAG_REMOVABLE;
Matt Delco28c21d92019-04-23 13:37:26 -07001377 MockGpioAfter(100, GPIO_SHUTDOWN);
Mathew King351c0052019-04-11 11:56:47 -06001378 mock_keypress[0] = VB_KEY_CTRL('C');
Matt Delcob6f4def2019-02-13 12:58:58 -08001379 TEST_EQ(vb2_nv_get(&ctx, VB2_NV_DIAG_REQUEST), 0,
1380 "todiag is zero");
1381 if (DIAGNOSTIC_UI)
1382 TEST_EQ(VbBootRecovery(&ctx),
1383 VBERROR_REBOOT_REQUIRED,
1384 "Ctrl+C todiag - enabled");
1385 else
1386 TEST_EQ(VbBootRecovery(&ctx),
1387 VBERROR_SHUTDOWN_REQUESTED,
1388 "Ctrl+C todiag - disabled");
Mathew King351c0052019-04-11 11:56:47 -06001389
Matt Delcob6f4def2019-02-13 12:58:58 -08001390 TEST_EQ(vb2_nv_get(&ctx, VB2_NV_DIAG_REQUEST), DIAGNOSTIC_UI,
Mathew King351c0052019-04-11 11:56:47 -06001391 " todiag is updated for Ctrl-C");
Joel Kitching43f6cab2019-05-10 12:17:18 +08001392 TEST_EQ(vb2_nv_get(&ctx, VB2_NV_DISPLAY_REQUEST), 0,
1393 " todiag doesn't set unneeded DISPLAY_REQUEST");
Mathew King351c0052019-04-11 11:56:47 -06001394 TEST_EQ(screens_displayed[0], VB_SCREEN_RECOVERY_INSERT,
1395 " insert screen");
Matt Delcob6f4def2019-02-13 12:58:58 -08001396
Joel Kitching43f6cab2019-05-10 12:17:18 +08001397 /* Test Diagnostic Mode via F12 - display disabled */
Matt Delcob6f4def2019-02-13 12:58:58 -08001398 ResetMocks();
Joel Kitching43f6cab2019-05-10 12:17:18 +08001399 shared->flags = VBSD_BOOT_REC_SWITCH_ON;
1400 sd->flags &= ~VB2_SD_FLAG_DISPLAY_AVAILABLE;
Matt Delcob6f4def2019-02-13 12:58:58 -08001401 trust_ec = 1;
Mathew King351c0052019-04-11 11:56:47 -06001402 vbtlk_retval = VBERROR_NO_DISK_FOUND - VB_DISK_FLAG_REMOVABLE;
Matt Delco28c21d92019-04-23 13:37:26 -07001403 MockGpioAfter(100, GPIO_SHUTDOWN);
Mathew King351c0052019-04-11 11:56:47 -06001404 mock_keypress[0] = VB_KEY_F(12);
Matt Delcob6f4def2019-02-13 12:58:58 -08001405 TEST_EQ(vb2_nv_get(&ctx, VB2_NV_DIAG_REQUEST), 0,
1406 "todiag is zero");
1407 if (DIAGNOSTIC_UI)
1408 TEST_EQ(VbBootRecovery(&ctx),
1409 VBERROR_REBOOT_REQUIRED,
1410 "F12 todiag - enabled");
1411 else
1412 TEST_EQ(VbBootRecovery(&ctx),
1413 VBERROR_SHUTDOWN_REQUESTED,
1414 "F12 todiag - disabled");
1415 TEST_EQ(vb2_nv_get(&ctx, VB2_NV_DIAG_REQUEST), DIAGNOSTIC_UI,
Mathew King351c0052019-04-11 11:56:47 -06001416 " todiag is updated for F12");
Joel Kitching451813e2019-05-29 15:22:54 +08001417 TEST_EQ(vb2_nv_get(&ctx, VB2_NV_DISPLAY_REQUEST), 0,
1418 " todiag doesn't set unneeded DISPLAY_REQUEST");
Mathew King351c0052019-04-11 11:56:47 -06001419 TEST_EQ(screens_displayed[0], VB_SCREEN_RECOVERY_INSERT,
1420 " insert screen");
1421
Joel Kitching43f6cab2019-05-10 12:17:18 +08001422 /* Test Diagnostic Mode via Ctrl-C OS broken - display available */
Mathew King351c0052019-04-11 11:56:47 -06001423 ResetMocks();
1424 shared->flags = 0;
Matt Delco28c21d92019-04-23 13:37:26 -07001425 MockGpioAfter(100, GPIO_SHUTDOWN);
Mathew King351c0052019-04-11 11:56:47 -06001426 mock_keypress[0] = VB_KEY_CTRL('C');
1427 TEST_EQ(vb2_nv_get(&ctx, VB2_NV_DIAG_REQUEST), 0,
1428 "todiag is zero");
1429 if (DIAGNOSTIC_UI)
1430 TEST_EQ(VbBootRecovery(&ctx),
1431 VBERROR_REBOOT_REQUIRED,
1432 "Ctrl+C todiag os broken - enabled");
1433 else
1434 TEST_EQ(VbBootRecovery(&ctx),
1435 VBERROR_SHUTDOWN_REQUESTED,
1436 "Ctrl+C todiag os broken - disabled");
1437 TEST_EQ(vb2_nv_get(&ctx, VB2_NV_DIAG_REQUEST), DIAGNOSTIC_UI,
1438 " todiag is updated for Ctrl-C");
Joel Kitching43f6cab2019-05-10 12:17:18 +08001439 TEST_EQ(vb2_nv_get(&ctx, VB2_NV_DISPLAY_REQUEST), 0,
1440 " todiag doesn't set unneeded DISPLAY_REQUEST");
Mathew King351c0052019-04-11 11:56:47 -06001441 TEST_EQ(screens_displayed[0], VB_SCREEN_OS_BROKEN,
1442 " os broken screen");
Matt Delcob6f4def2019-02-13 12:58:58 -08001443
1444 printf("...done.\n");
1445}
1446
1447static void VbBootDiagTest(void)
1448{
1449 printf("Testing VbBootDiagnostic()...\n");
1450
1451 /* No key pressed - timeout. */
1452 ResetMocks();
1453 TEST_EQ(VbBootDiagnostic(&ctx), VBERROR_REBOOT_REQUIRED, "Timeout");
1454 TEST_EQ(screens_displayed[0], VB_SCREEN_CONFIRM_DIAG,
1455 " confirm screen");
1456 TEST_EQ(screens_displayed[1], VB_SCREEN_BLANK,
1457 " blank screen");
1458 TEST_EQ(tpm_set_mode_called, 0, " no tpm call");
1459 TEST_EQ(vbexlegacy_called, 0, " not legacy");
1460 TEST_EQ(current_ticks, 30 * VB_USEC_PER_SEC,
1461 " waited for 30 seconds");
1462
1463 /* Esc key pressed. */
1464 ResetMocks();
1465 mock_keypress[0] = VB_KEY_ESC;
1466 TEST_EQ(VbBootDiagnostic(&ctx), VBERROR_REBOOT_REQUIRED, "Esc key");
1467 TEST_EQ(screens_displayed[0], VB_SCREEN_CONFIRM_DIAG,
1468 " confirm screen");
1469 TEST_EQ(screens_displayed[1], VB_SCREEN_BLANK,
1470 " blank screen");
1471 TEST_EQ(tpm_set_mode_called, 0, " no tpm call");
1472 TEST_EQ(vbexlegacy_called, 0, " not legacy");
1473 TEST_EQ(current_ticks, 0, " didn't wait at all");
1474
1475 /* Shutdown requested via lid close */
1476 ResetMocks();
Matt Delco28c21d92019-04-23 13:37:26 -07001477 MockGpioAfter(10, GPIO_LID_CLOSED);
Matt Delcob6f4def2019-02-13 12:58:58 -08001478 TEST_EQ(VbBootDiagnostic(&ctx), VBERROR_SHUTDOWN_REQUESTED, "Shutdown");
1479 TEST_EQ(screens_displayed[0], VB_SCREEN_CONFIRM_DIAG,
1480 " confirm screen");
1481 TEST_EQ(screens_displayed[1], VB_SCREEN_BLANK,
1482 " blank screen");
1483 TEST_EQ(tpm_set_mode_called, 0, " no tpm call");
1484 TEST_EQ(vbexlegacy_called, 0, " not legacy");
1485 TEST_TRUE(current_ticks < VB_USEC_PER_SEC, " didn't wait long");
1486
1487 /* Power button pressed but not released. */
1488 ResetMocks();
Matt Delco28c21d92019-04-23 13:37:26 -07001489 mock_gpio[0].gpio_flags = GPIO_PRESENCE;
1490 mock_gpio[0].count = ~0;
Matt Delcob6f4def2019-02-13 12:58:58 -08001491 TEST_EQ(VbBootDiagnostic(&ctx), VBERROR_REBOOT_REQUIRED, "Power held");
1492 TEST_EQ(screens_displayed[0], VB_SCREEN_CONFIRM_DIAG,
1493 " confirm screen");
1494 TEST_EQ(screens_displayed[1], VB_SCREEN_BLANK,
1495 " blank screen");
1496 TEST_EQ(tpm_set_mode_called, 0, " no tpm call");
1497 TEST_EQ(vbexlegacy_called, 0, " not legacy");
1498
1499 /* Power button is pressed and released. */
1500 ResetMocks();
Matt Delco28c21d92019-04-23 13:37:26 -07001501 MockGpioAfter(3, GPIO_PRESENCE);
Matt Delcob6f4def2019-02-13 12:58:58 -08001502 TEST_EQ(VbBootDiagnostic(&ctx), VBERROR_REBOOT_REQUIRED, "Confirm");
1503 TEST_EQ(screens_displayed[0], VB_SCREEN_CONFIRM_DIAG,
1504 " confirm screen");
1505 TEST_EQ(screens_displayed[1], VB_SCREEN_BLANK,
1506 " blank screen");
1507 TEST_EQ(tpm_set_mode_called, 1, " tpm call");
1508 TEST_EQ(tpm_mode, VB2_TPM_MODE_DISABLED, " tpm disabled");
1509 TEST_EQ(vbexlegacy_called, 1, " legacy");
1510 TEST_EQ(altfw_num, VB_ALTFW_DIAGNOSTIC, " check altfw_num");
1511 /*
1512 * Ideally we'd that no recovery request was recorded, but
1513 * VbExLegacy() can only fail or crash the tests.
1514 */
1515 TEST_EQ(vb2_nv_get(&ctx, VB2_NV_RECOVERY_REQUEST),
1516 VB2_RECOVERY_ALTFW_HASH_FAILED,
1517 " recovery request");
1518
1519 /* Power button confirm, but now with a tpm failure. */
1520 ResetMocks();
1521 tpm_mode = VB2_TPM_MODE_DISABLED;
Matt Delco28c21d92019-04-23 13:37:26 -07001522 mock_gpio[0].gpio_flags = 0;
1523 mock_gpio[0].count = 2;
1524 mock_gpio[1].gpio_flags = GPIO_PRESENCE;
1525 mock_gpio[1].count = 2;
Matt Delcob6f4def2019-02-13 12:58:58 -08001526 TEST_EQ(VbBootDiagnostic(&ctx), VBERROR_REBOOT_REQUIRED,
1527 "Confirm but tpm fail");
1528 TEST_EQ(screens_displayed[0], VB_SCREEN_CONFIRM_DIAG,
1529 " confirm screen");
1530 TEST_EQ(screens_displayed[1], VB_SCREEN_BLANK,
1531 " blank screen");
1532 TEST_EQ(tpm_set_mode_called, 1, " tpm call");
1533 TEST_EQ(tpm_mode, VB2_TPM_MODE_DISABLED, " tpm disabled");
1534 TEST_EQ(vbexlegacy_called, 0, " legacy not called");
1535 TEST_EQ(vb2_nv_get(&ctx, VB2_NV_RECOVERY_REQUEST),
1536 VB2_RECOVERY_TPM_DISABLE_FAILED,
1537 " recovery request");
1538
Randall Spangler7f436692013-02-05 12:42:36 -08001539 printf("...done.\n");
1540}
1541
1542
1543int main(void)
1544{
1545 VbUserConfirmsTest();
1546 VbBootTest();
1547 VbBootDevTest();
1548 VbBootRecTest();
Matt Delcob6f4def2019-02-13 12:58:58 -08001549 if (DIAGNOSTIC_UI)
1550 VbBootDiagTest();
Randall Spangler7f436692013-02-05 12:42:36 -08001551
Randall Spangler7f436692013-02-05 12:42:36 -08001552 return gTestSuccess ? 0 : 255;
1553}