17f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler/* Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
27f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler * Use of this source code is governed by a BSD-style license that can be
37f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler * found in the LICENSE file.
47f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler *
57f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler * Tests for vboot_api_kernel, part 2
67f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler */
77f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler
80c3ba249abb1dc60f5ebabccf84ff13206440b83Bill Richardson#include <stdint.h>
97f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler#include <stdio.h>
107f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler#include <stdlib.h>
117f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler
127f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler#include "gbb_header.h"
137f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler#include "host_common.h"
147f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler#include "load_kernel_fw.h"
157f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler#include "rollback_index.h"
167f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler#include "test_common.h"
177f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler#include "vboot_audio.h"
187f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler#include "vboot_common.h"
197f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler#include "vboot_kernel.h"
207f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler#include "vboot_nvstorage.h"
217f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler#include "vboot_struct.h"
227f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler
237f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler/* Mock data */
247f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spanglerstatic VbCommonParams cparams;
257f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spanglerstatic uint8_t shared_data[VB_SHARED_DATA_MIN_SIZE];
267f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spanglerstatic VbSharedDataHeader *shared = (VbSharedDataHeader *)shared_data;
277f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spanglerstatic GoogleBinaryBlockHeader gbb;
287f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spanglerstatic LoadKernelParams lkp;
297f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler
307f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spanglerstatic int shutdown_request_calls_left;
317f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spanglerstatic int audio_looping_calls_left;
327f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spanglerstatic uint32_t vbtlk_retval;
337f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spanglerstatic int vbexlegacy_called;
347f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spanglerstatic int trust_ec;
357f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spanglerstatic int virtdev_set;
367f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spanglerstatic uint32_t virtdev_retval;
377f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spanglerstatic uint32_t mock_keypress[8];
38a53a0b040f45a1086515e7a5c8a8326c0b1d1f74Luigi Semenzatostatic uint32_t mock_keyflags[8];
397f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spanglerstatic uint32_t mock_keypress_count;
40a53a0b040f45a1086515e7a5c8a8326c0b1d1f74Luigi Semenzatostatic uint32_t mock_switches[8];
41a53a0b040f45a1086515e7a5c8a8326c0b1d1f74Luigi Semenzatostatic uint32_t mock_switches_count;
42a53a0b040f45a1086515e7a5c8a8326c0b1d1f74Luigi Semenzatostatic int mock_switches_are_stuck;
437f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spanglerstatic uint32_t screens_displayed[8];
447f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spanglerstatic uint32_t screens_count = 0;
457f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spanglerstatic uint32_t mock_num_disks[8];
467f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spanglerstatic uint32_t mock_num_disks_count;
477f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler
48487a54bcbe7b6dac1a856b0991e6d13c34a1c423Sheng-Liang Songextern enum VbEcBootMode_t VbGetMode(void);
49487a54bcbe7b6dac1a856b0991e6d13c34a1c423Sheng-Liang Song
507f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler/* Reset mock data (for use before each test) */
517f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spanglerstatic void ResetMocks(void)
527f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler{
537f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	Memset(&cparams, 0, sizeof(cparams));
547f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	cparams.shared_data_size = sizeof(shared_data);
557f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	cparams.shared_data_blob = shared_data;
567f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	cparams.gbb_data = &gbb;
57527ba810eff4006cf69579f6b96cb4350cb1e189Simon Glass	cparams.gbb = &gbb;
587f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler
597f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	Memset(&gbb, 0, sizeof(gbb));
607f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	gbb.major_version = GBB_MAJOR_VER;
617f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	gbb.minor_version = GBB_MINOR_VER;
627f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	gbb.flags = 0;
637f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler
647f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	/*
657f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	 * Only the outermost vboot_api_kernel call sets vboot_api_kernel's
667f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	 * vnc.  So clear it here too.
677f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	 */
687f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	Memset(VbApiKernelGetVnc(), 0, sizeof(VbNvContext));
697f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	VbNvSetup(VbApiKernelGetVnc());
707f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	VbNvTeardown(VbApiKernelGetVnc()); /* So CRC gets generated */
717f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler
727f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	Memset(&shared_data, 0, sizeof(shared_data));
737f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	VbSharedDataInit(shared, sizeof(shared_data));
747f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler
757f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	Memset(&lkp, 0, sizeof(lkp));
767f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler
777f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	shutdown_request_calls_left = -1;
787f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	audio_looping_calls_left = 30;
797f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	vbtlk_retval = 1000;
807f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	vbexlegacy_called = 0;
817f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	trust_ec = 0;
827f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	virtdev_set = 0;
837f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	virtdev_retval = 0;
847f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler
857f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	Memset(screens_displayed, 0, sizeof(screens_displayed));
867f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	screens_count = 0;
877f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler
887f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	Memset(mock_keypress, 0, sizeof(mock_keypress));
89a53a0b040f45a1086515e7a5c8a8326c0b1d1f74Luigi Semenzato	Memset(mock_keyflags, 0, sizeof(mock_keyflags));
907f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	mock_keypress_count = 0;
917f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler
92a53a0b040f45a1086515e7a5c8a8326c0b1d1f74Luigi Semenzato	Memset(mock_switches, 0, sizeof(mock_switches));
93a53a0b040f45a1086515e7a5c8a8326c0b1d1f74Luigi Semenzato	mock_switches_count = 0;
94a53a0b040f45a1086515e7a5c8a8326c0b1d1f74Luigi Semenzato	mock_switches_are_stuck = 0;
95a53a0b040f45a1086515e7a5c8a8326c0b1d1f74Luigi Semenzato
967f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	Memset(mock_num_disks, 0, sizeof(mock_num_disks));
977f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	mock_num_disks_count = 0;
987f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler}
997f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler
1007f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler/* Mock functions */
1017f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler
1027f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangleruint32_t VbExIsShutdownRequested(void)
1037f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler{
1047f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	if (shutdown_request_calls_left == 0)
1057f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler		return 1;
1067f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	else if (shutdown_request_calls_left > 0)
1077f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler		shutdown_request_calls_left--;
1087f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler
1097f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	return 0;
1107f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler}
1117f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler
1127f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangleruint32_t VbExKeyboardRead(void)
1137f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler{
114a53a0b040f45a1086515e7a5c8a8326c0b1d1f74Luigi Semenzato	return VbExKeyboardReadWithFlags(NULL);
115a53a0b040f45a1086515e7a5c8a8326c0b1d1f74Luigi Semenzato}
116a53a0b040f45a1086515e7a5c8a8326c0b1d1f74Luigi Semenzato
117a53a0b040f45a1086515e7a5c8a8326c0b1d1f74Luigi Semenzatouint32_t VbExKeyboardReadWithFlags(uint32_t *key_flags)
118a53a0b040f45a1086515e7a5c8a8326c0b1d1f74Luigi Semenzato{
119a53a0b040f45a1086515e7a5c8a8326c0b1d1f74Luigi Semenzato	if (mock_keypress_count < ARRAY_SIZE(mock_keypress)) {
120a53a0b040f45a1086515e7a5c8a8326c0b1d1f74Luigi Semenzato		if (key_flags != NULL)
121a53a0b040f45a1086515e7a5c8a8326c0b1d1f74Luigi Semenzato			*key_flags = mock_keyflags[mock_keypress_count];
1227f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler		return mock_keypress[mock_keypress_count++];
123a53a0b040f45a1086515e7a5c8a8326c0b1d1f74Luigi Semenzato	} else
124a53a0b040f45a1086515e7a5c8a8326c0b1d1f74Luigi Semenzato		return 0;
125a53a0b040f45a1086515e7a5c8a8326c0b1d1f74Luigi Semenzato}
126a53a0b040f45a1086515e7a5c8a8326c0b1d1f74Luigi Semenzato
127a53a0b040f45a1086515e7a5c8a8326c0b1d1f74Luigi Semenzatouint32_t VbExGetSwitches(uint32_t request_mask)
128a53a0b040f45a1086515e7a5c8a8326c0b1d1f74Luigi Semenzato{
129a53a0b040f45a1086515e7a5c8a8326c0b1d1f74Luigi Semenzato	if (mock_switches_are_stuck)
130a53a0b040f45a1086515e7a5c8a8326c0b1d1f74Luigi Semenzato		return mock_switches[0] & request_mask;
131a53a0b040f45a1086515e7a5c8a8326c0b1d1f74Luigi Semenzato	if (mock_switches_count < ARRAY_SIZE(mock_switches))
132a53a0b040f45a1086515e7a5c8a8326c0b1d1f74Luigi Semenzato		return mock_switches[mock_switches_count++] & request_mask;
1337f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	else
1347f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler		return 0;
1357f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler}
1367f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler
1377f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spanglerint VbExLegacy(void)
1387f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler{
1397f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	vbexlegacy_called++;
1407f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	return 0;
1417f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler}
1427f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler
1437f43669630cb42e40ca6ddc1128eefea8fd339d9Randall SpanglerVbError_t VbExDiskGetInfo(VbDiskInfo **infos_ptr, uint32_t *count,
1447f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler                          uint32_t disk_flags)
1457f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler{
1467f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	if (mock_num_disks_count < ARRAY_SIZE(mock_num_disks)) {
1477f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler		if (mock_num_disks[mock_num_disks_count] == -1)
1487f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler			return VBERROR_SIMULATED;
1497f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler		else
1507f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler			*count = mock_num_disks[mock_num_disks_count++];
1517f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	} else {
1527f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler		*count = 0;
1537f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	}
1547f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	return VBERROR_SUCCESS;
1557f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler}
1567f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler
1577f43669630cb42e40ca6ddc1128eefea8fd339d9Randall SpanglerVbError_t VbExDiskFreeInfo(VbDiskInfo *infos,
1587f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler                           VbExDiskHandle_t preserve_handle)
1597f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler{
1607f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	return VBERROR_SUCCESS;
1617f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler}
1627f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler
163e778adae83d2ac4ab94aa10acceab328bbd3ea7bRandall Spanglerint VbExTrustEC(int devidx)
1647f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler{
1657f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	return trust_ec;
1667f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler}
1677f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler
1687f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spanglerint VbAudioLooping(VbAudioContext *audio)
1697f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler{
1707f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	if (audio_looping_calls_left == 0)
1717f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler		return 0;
1727f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	else if (audio_looping_calls_left > 0)
1737f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler		audio_looping_calls_left--;
1747f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler
1757f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	return 1;
1767f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler}
1777f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler
1787f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangleruint32_t VbTryLoadKernel(VbCommonParams *cparams, LoadKernelParams *p,
1797f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler                         uint32_t get_info_flags)
1807f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler{
1817f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	return vbtlk_retval + get_info_flags;
1827f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler}
1837f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler
1847f43669630cb42e40ca6ddc1128eefea8fd339d9Randall SpanglerVbError_t VbDisplayScreen(VbCommonParams *cparams, uint32_t screen, int force,
1857f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler                          VbNvContext *vncptr)
1867f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler{
1877f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	if (screens_count < ARRAY_SIZE(screens_displayed))
1887f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler		screens_displayed[screens_count++] = screen;
1897f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler
1907f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	return VBERROR_SUCCESS;
1917f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler}
1927f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler
1937f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangleruint32_t SetVirtualDevMode(int val)
1947f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler{
1957f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	virtdev_set = val;
1967f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	return virtdev_retval;
1977f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler}
1987f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler
1997f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler/* Tests */
2007f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler
2017f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spanglerstatic void VbUserConfirmsTest(void)
2027f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler{
2037f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	printf("Testing VbUserConfirms()...\n");
2047f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler
2057f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	ResetMocks();
2067f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	shutdown_request_calls_left = 1;
2077f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	TEST_EQ(VbUserConfirms(&cparams, 0), -1, "Shutdown requested");
2087f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler
2097f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	ResetMocks();
2107f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	mock_keypress[0] = '\r';
2117f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	TEST_EQ(VbUserConfirms(&cparams, 0), 1, "Enter");
2127f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler
2137f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	ResetMocks();
2147f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	mock_keypress[0] = 0x1b;
2157f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	TEST_EQ(VbUserConfirms(&cparams, 0), 0, "Esc");
2167f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler
2177f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	ResetMocks();
2187f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	mock_keypress[0] = ' ';
2197f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	shutdown_request_calls_left = 1;
220a53a0b040f45a1086515e7a5c8a8326c0b1d1f74Luigi Semenzato	TEST_EQ(VbUserConfirms(&cparams, VB_CONFIRM_SPACE_MEANS_NO), 0,
221a53a0b040f45a1086515e7a5c8a8326c0b1d1f74Luigi Semenzato                "Space means no");
2227f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler
2237f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	ResetMocks();
2247f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	mock_keypress[0] = ' ';
2257f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	shutdown_request_calls_left = 1;
2267f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	TEST_EQ(VbUserConfirms(&cparams, 0), -1, "Space ignored");
2277f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler
228a53a0b040f45a1086515e7a5c8a8326c0b1d1f74Luigi Semenzato	ResetMocks();
229a53a0b040f45a1086515e7a5c8a8326c0b1d1f74Luigi Semenzato	mock_keypress[0] = '\r';
230a53a0b040f45a1086515e7a5c8a8326c0b1d1f74Luigi Semenzato	mock_keyflags[0] = VB_KEY_FLAG_TRUSTED_KEYBOARD;
231a53a0b040f45a1086515e7a5c8a8326c0b1d1f74Luigi Semenzato	TEST_EQ(VbUserConfirms(&cparams, VB_CONFIRM_MUST_TRUST_KEYBOARD),
232a53a0b040f45a1086515e7a5c8a8326c0b1d1f74Luigi Semenzato		1, "Enter with trusted keyboard");
233a53a0b040f45a1086515e7a5c8a8326c0b1d1f74Luigi Semenzato
234a53a0b040f45a1086515e7a5c8a8326c0b1d1f74Luigi Semenzato	ResetMocks();
235a53a0b040f45a1086515e7a5c8a8326c0b1d1f74Luigi Semenzato	mock_keypress[0] = '\r';	/* untrusted */
236a53a0b040f45a1086515e7a5c8a8326c0b1d1f74Luigi Semenzato	mock_keypress[1] = ' ';
237a53a0b040f45a1086515e7a5c8a8326c0b1d1f74Luigi Semenzato	TEST_EQ(VbUserConfirms(&cparams,
238a53a0b040f45a1086515e7a5c8a8326c0b1d1f74Luigi Semenzato			       VB_CONFIRM_SPACE_MEANS_NO |
239a53a0b040f45a1086515e7a5c8a8326c0b1d1f74Luigi Semenzato			       VB_CONFIRM_MUST_TRUST_KEYBOARD),
240a53a0b040f45a1086515e7a5c8a8326c0b1d1f74Luigi Semenzato		0, "Untrusted keyboard");
241a53a0b040f45a1086515e7a5c8a8326c0b1d1f74Luigi Semenzato
242a53a0b040f45a1086515e7a5c8a8326c0b1d1f74Luigi Semenzato	ResetMocks();
243a53a0b040f45a1086515e7a5c8a8326c0b1d1f74Luigi Semenzato	mock_switches[0] = VB_INIT_FLAG_REC_BUTTON_PRESSED;
244a53a0b040f45a1086515e7a5c8a8326c0b1d1f74Luigi Semenzato	TEST_EQ(VbUserConfirms(&cparams,
245a53a0b040f45a1086515e7a5c8a8326c0b1d1f74Luigi Semenzato			       VB_CONFIRM_SPACE_MEANS_NO |
246a53a0b040f45a1086515e7a5c8a8326c0b1d1f74Luigi Semenzato			       VB_CONFIRM_MUST_TRUST_KEYBOARD),
247a53a0b040f45a1086515e7a5c8a8326c0b1d1f74Luigi Semenzato		1, "Recovery button");
248a53a0b040f45a1086515e7a5c8a8326c0b1d1f74Luigi Semenzato
249a53a0b040f45a1086515e7a5c8a8326c0b1d1f74Luigi Semenzato	ResetMocks();
250a53a0b040f45a1086515e7a5c8a8326c0b1d1f74Luigi Semenzato	mock_keypress[0] = '\r';
251a53a0b040f45a1086515e7a5c8a8326c0b1d1f74Luigi Semenzato	mock_keypress[1] = 'y';
252a53a0b040f45a1086515e7a5c8a8326c0b1d1f74Luigi Semenzato	mock_keypress[2] = 'z';
253a53a0b040f45a1086515e7a5c8a8326c0b1d1f74Luigi Semenzato	mock_keypress[3] = ' ';
254a53a0b040f45a1086515e7a5c8a8326c0b1d1f74Luigi Semenzato	mock_switches[0] = VB_INIT_FLAG_REC_BUTTON_PRESSED;
255a53a0b040f45a1086515e7a5c8a8326c0b1d1f74Luigi Semenzato	mock_switches_are_stuck = 1;
256a53a0b040f45a1086515e7a5c8a8326c0b1d1f74Luigi Semenzato	TEST_EQ(VbUserConfirms(&cparams,
257a53a0b040f45a1086515e7a5c8a8326c0b1d1f74Luigi Semenzato			       VB_CONFIRM_SPACE_MEANS_NO |
258a53a0b040f45a1086515e7a5c8a8326c0b1d1f74Luigi Semenzato			       VB_CONFIRM_MUST_TRUST_KEYBOARD),
259a53a0b040f45a1086515e7a5c8a8326c0b1d1f74Luigi Semenzato		0, "Recovery button stuck");
260a53a0b040f45a1086515e7a5c8a8326c0b1d1f74Luigi Semenzato
2617f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	printf("...done.\n");
2627f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler}
2637f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler
2647f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spanglerstatic void VbBootTest(void)
2657f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler{
2667f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	ResetMocks();
267487a54bcbe7b6dac1a856b0991e6d13c34a1c423Sheng-Liang Song	VbExEcEnteringMode(0, VB_EC_NORMAL);
2687f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	TEST_EQ(VbBootNormal(&cparams, &lkp), 1002, "VbBootNormal()");
269487a54bcbe7b6dac1a856b0991e6d13c34a1c423Sheng-Liang Song	TEST_EQ(VbGetMode(), VB_EC_NORMAL, "vboot_mode normal");
2707f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler}
2717f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler
2727f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spanglerstatic void VbBootDevTest(void)
2737f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler{
2747f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	uint32_t u;
2757f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler
2767f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	printf("Testing VbBootDeveloper()...\n");
2777f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler
2787f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	/* Proceed after timeout */
2797f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	ResetMocks();
280487a54bcbe7b6dac1a856b0991e6d13c34a1c423Sheng-Liang Song	VbExEcEnteringMode(0, VB_EC_DEVELOPER);
2817f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	TEST_EQ(VbBootDeveloper(&cparams, &lkp), 1002, "Timeout");
282487a54bcbe7b6dac1a856b0991e6d13c34a1c423Sheng-Liang Song	TEST_EQ(VbGetMode(), VB_EC_DEVELOPER, "vboot_mode developer");
2837f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	TEST_EQ(screens_displayed[0], VB_SCREEN_DEVELOPER_WARNING,
2847f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler		"  warning screen");
2857f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	VbNvGet(VbApiKernelGetVnc(), VBNV_RECOVERY_REQUEST, &u);
2867f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	TEST_EQ(u, 0, "  recovery reason");
2877f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	TEST_EQ(audio_looping_calls_left, 0, "  used up audio");
2887f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler
289f2a1dc0a54ec542dd4be7b634483de317c89a2a2Randall Spangler	/* Proceed to legacy after timeout if GBB flag set */
290f2a1dc0a54ec542dd4be7b634483de317c89a2a2Randall Spangler	ResetMocks();
291f2a1dc0a54ec542dd4be7b634483de317c89a2a2Randall Spangler	gbb.flags |= GBB_FLAG_DEFAULT_DEV_BOOT_LEGACY;
292f2a1dc0a54ec542dd4be7b634483de317c89a2a2Randall Spangler	TEST_EQ(VbBootDeveloper(&cparams, &lkp), 1002, "Timeout");
293f2a1dc0a54ec542dd4be7b634483de317c89a2a2Randall Spangler	TEST_EQ(vbexlegacy_called, 1, "  try legacy");
294f2a1dc0a54ec542dd4be7b634483de317c89a2a2Randall Spangler
2957f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	/* Up arrow is uninteresting / passed to VbCheckDisplayKey() */
2967f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	ResetMocks();
2977f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	mock_keypress[0] = VB_KEY_UP;
2987f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	TEST_EQ(VbBootDeveloper(&cparams, &lkp), 1002, "Up arrow");
2997f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler
3007f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	/* Shutdown requested in loop */
3017f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	ResetMocks();
3027f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	shutdown_request_calls_left = 2;
3037f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	TEST_EQ(VbBootDeveloper(&cparams, &lkp), VBERROR_SHUTDOWN_REQUESTED,
3047f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler		"Shutdown requested");
3057f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	TEST_NEQ(audio_looping_calls_left, 0, "  aborts audio");
3067f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler
3077f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	/* Space goes straight to recovery if no virtual dev switch */
3087f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	ResetMocks();
3097f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	mock_keypress[0] = ' ';
3107f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	TEST_EQ(VbBootDeveloper(&cparams, &lkp), VBERROR_LOAD_KERNEL_RECOVERY,
3117f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler		"Space = recovery");
3127f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	VbNvGet(VbApiKernelGetVnc(), VBNV_RECOVERY_REQUEST, &u);
3137f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	TEST_EQ(u, VBNV_RECOVERY_RW_DEV_SCREEN, "  recovery reason");
3147f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler
3157f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	/* Space asks to disable virtual dev switch */
3167f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	ResetMocks();
3177f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	shared->flags = VBSD_HONOR_VIRT_DEV_SWITCH | VBSD_BOOT_DEV_SWITCH_ON;
3187f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	mock_keypress[0] = ' ';
3197f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	mock_keypress[1] = '\r';
3207f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	TEST_EQ(VbBootDeveloper(&cparams, &lkp), VBERROR_TPM_REBOOT_REQUIRED,
3217f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler		"Space = tonorm");
3227f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	TEST_EQ(screens_displayed[0], VB_SCREEN_DEVELOPER_WARNING,
3237f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler		"  warning screen");
3247f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	TEST_EQ(screens_displayed[1], VB_SCREEN_DEVELOPER_TO_NORM,
3257f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler		"  tonorm screen");
3267f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	TEST_EQ(screens_displayed[2], VB_SCREEN_TO_NORM_CONFIRMED,
3277f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler		"  confirm screen");
3287f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	VbNvGet(VbApiKernelGetVnc(), VBNV_DISABLE_DEV_REQUEST, &u);
3297f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	TEST_EQ(u, 1, "  disable dev request");
3307f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler
3317f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	/* Space-space doesn't disable it */
3327f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	ResetMocks();
3337f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	shared->flags = VBSD_HONOR_VIRT_DEV_SWITCH | VBSD_BOOT_DEV_SWITCH_ON;
3347f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	mock_keypress[0] = ' ';
3357f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	mock_keypress[1] = ' ';
3367f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	mock_keypress[2] = 0x1b;
3377f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	TEST_EQ(VbBootDeveloper(&cparams, &lkp), 1002, "Space-space");
3387f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	TEST_EQ(screens_displayed[0], VB_SCREEN_DEVELOPER_WARNING,
3397f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler		"  warning screen");
3407f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	TEST_EQ(screens_displayed[1], VB_SCREEN_DEVELOPER_TO_NORM,
3417f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler		"  tonorm screen");
3427f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	TEST_EQ(screens_displayed[2], VB_SCREEN_DEVELOPER_WARNING,
3437f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler		"  warning screen");
3447f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler
3457f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	/* Enter doesn't by default */
3467f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	ResetMocks();
3477f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	shared->flags = VBSD_HONOR_VIRT_DEV_SWITCH | VBSD_BOOT_DEV_SWITCH_ON;
3487f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	mock_keypress[0] = '\r';
3497f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	mock_keypress[1] = '\r';
3507f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	TEST_EQ(VbBootDeveloper(&cparams, &lkp), 1002, "Enter ignored");
3517f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler
3527f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	/* Enter does if GBB flag set */
3537f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	ResetMocks();
3547f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	shared->flags = VBSD_HONOR_VIRT_DEV_SWITCH | VBSD_BOOT_DEV_SWITCH_ON;
3557f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	gbb.flags |= GBB_FLAG_ENTER_TRIGGERS_TONORM;
3567f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	mock_keypress[0] = '\r';
3577f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	mock_keypress[1] = '\r';
3587f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	TEST_EQ(VbBootDeveloper(&cparams, &lkp), VBERROR_TPM_REBOOT_REQUIRED,
3597f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler		"Enter = tonorm");
3607f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler
3617f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	/* Tonorm ignored if GBB forces dev switch on */
3627f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	ResetMocks();
3637f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	shared->flags = VBSD_HONOR_VIRT_DEV_SWITCH | VBSD_BOOT_DEV_SWITCH_ON;
3647f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	gbb.flags |= GBB_FLAG_FORCE_DEV_SWITCH_ON;
3657f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	mock_keypress[0] = ' ';
3667f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	mock_keypress[1] = '\r';
3677f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	TEST_EQ(VbBootDeveloper(&cparams, &lkp), 1002, "Can't tonorm gbb-dev");
3687f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler
3697f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	/* Shutdown requested at tonorm screen */
3707f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	ResetMocks();
3717f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	shared->flags = VBSD_HONOR_VIRT_DEV_SWITCH | VBSD_BOOT_DEV_SWITCH_ON;
3727f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	mock_keypress[0] = ' ';
3737f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	shutdown_request_calls_left = 2;
3747f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	TEST_EQ(VbBootDeveloper(&cparams, &lkp), VBERROR_SHUTDOWN_REQUESTED,
3757f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler		"Shutdown requested at tonorm");
3767f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	TEST_EQ(screens_displayed[0], VB_SCREEN_DEVELOPER_WARNING,
3777f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler		"  warning screen");
3787f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	TEST_EQ(screens_displayed[1], VB_SCREEN_DEVELOPER_TO_NORM,
3797f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler		"  tonorm screen");
3807f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler
3817f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	/* Ctrl+D dismisses warning */
3827f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	ResetMocks();
3837f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	mock_keypress[0] = 0x04;
3847f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	TEST_EQ(VbBootDeveloper(&cparams, &lkp), 1002, "Ctrl+D");
3857f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	VbNvGet(VbApiKernelGetVnc(), VBNV_RECOVERY_REQUEST, &u);
3867f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	TEST_EQ(u, 0, "  recovery reason");
3877f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	TEST_NEQ(audio_looping_calls_left, 0, "  aborts audio");
388f2a1dc0a54ec542dd4be7b634483de317c89a2a2Randall Spangler	TEST_EQ(vbexlegacy_called, 0, "  not legacy");
389f2a1dc0a54ec542dd4be7b634483de317c89a2a2Randall Spangler
390f2a1dc0a54ec542dd4be7b634483de317c89a2a2Randall Spangler	/* Ctrl+D doesn't boot legacy even if GBB flag is set */
391f2a1dc0a54ec542dd4be7b634483de317c89a2a2Randall Spangler	ResetMocks();
392f2a1dc0a54ec542dd4be7b634483de317c89a2a2Randall Spangler	mock_keypress[0] = 0x04;
393f2a1dc0a54ec542dd4be7b634483de317c89a2a2Randall Spangler	gbb.flags |= GBB_FLAG_DEFAULT_DEV_BOOT_LEGACY;
394f2a1dc0a54ec542dd4be7b634483de317c89a2a2Randall Spangler	TEST_EQ(VbBootDeveloper(&cparams, &lkp), 1002, "Ctrl+D");
395f2a1dc0a54ec542dd4be7b634483de317c89a2a2Randall Spangler	TEST_EQ(vbexlegacy_called, 0, "  not legacy");
3967f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler
3977f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	/* Ctrl+L tries legacy boot mode only if enabled */
3987f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	ResetMocks();
3997f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	mock_keypress[0] = 0x0c;
4007f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	TEST_EQ(VbBootDeveloper(&cparams, &lkp), 1002, "Ctrl+L normal");
4017f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	TEST_EQ(vbexlegacy_called, 0, "  not legacy");
4027f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler
4037f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	ResetMocks();
4047f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler
4057f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	gbb.flags |= GBB_FLAG_FORCE_DEV_BOOT_LEGACY;
4067f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	mock_keypress[0] = 0x0c;
4077f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	TEST_EQ(VbBootDeveloper(&cparams, &lkp), 1002, "Ctrl+L force legacy");
4087f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	TEST_EQ(vbexlegacy_called, 1, "  try legacy");
4097f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler
4107f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	ResetMocks();
4117f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	VbNvSet(VbApiKernelGetVnc(), VBNV_DEV_BOOT_LEGACY, 1);
4127f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	mock_keypress[0] = 0x0c;
4137f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	TEST_EQ(VbBootDeveloper(&cparams, &lkp), 1002, "Ctrl+L nv legacy");
4147f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	TEST_EQ(vbexlegacy_called, 1, "  try legacy");
4157f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler
4167f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	/* Ctrl+U boots USB only if enabled */
4177f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	ResetMocks();
4187f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	mock_keypress[0] = 0x15;
4197f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	TEST_EQ(VbBootDeveloper(&cparams, &lkp), 1002, "Ctrl+U normal");
4207f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler
4217f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	/* Ctrl+U enabled, with good USB boot */
4227f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	ResetMocks();
4237f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	VbNvSet(VbApiKernelGetVnc(), VBNV_DEV_BOOT_USB, 1);
4247f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	mock_keypress[0] = 0x15;
4257f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	vbtlk_retval = VBERROR_SUCCESS - VB_DISK_FLAG_REMOVABLE;
4267f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	TEST_EQ(VbBootDeveloper(&cparams, &lkp), 0, "Ctrl+U USB");
4277f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler
4287f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	/* Ctrl+U enabled via GBB */
4297f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	ResetMocks();
4307f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	gbb.flags |= GBB_FLAG_FORCE_DEV_BOOT_USB;
4317f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	mock_keypress[0] = 0x15;
4327f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	vbtlk_retval = VBERROR_SUCCESS - VB_DISK_FLAG_REMOVABLE;
4337f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	TEST_EQ(VbBootDeveloper(&cparams, &lkp), 0, "Ctrl+U force USB");
4347f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler
4357f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	/* If no USB, eventually times out and tries fixed disk */
4367f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	ResetMocks();
4377f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	VbNvSet(VbApiKernelGetVnc(), VBNV_DEV_BOOT_USB, 1);
4387f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	mock_keypress[0] = 0x15;
4397f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	TEST_EQ(VbBootDeveloper(&cparams, &lkp), 1002, "Ctrl+U enabled");
4407f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	TEST_EQ(vbexlegacy_called, 0, "  not legacy");
4417f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	VbNvGet(VbApiKernelGetVnc(), VBNV_RECOVERY_REQUEST, &u);
4427f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	TEST_EQ(u, 0, "  recovery reason");
4437f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	TEST_EQ(audio_looping_calls_left, 0, "  used up audio");
4447f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler
4457f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	printf("...done.\n");
4467f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler}
4477f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler
4487f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spanglerstatic void VbBootRecTest(void)
4497f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler{
4507f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	uint32_t u;
4517f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler
4527f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	printf("Testing VbBootRecovery()...\n");
4537f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler
4547f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	/* Shutdown requested in loop */
4557f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	ResetMocks();
4567f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	shutdown_request_calls_left = 10;
457487a54bcbe7b6dac1a856b0991e6d13c34a1c423Sheng-Liang Song	VbExEcEnteringMode(0, VB_EC_RECOVERY);
4587f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	TEST_EQ(VbBootRecovery(&cparams, &lkp), VBERROR_SHUTDOWN_REQUESTED,
4597f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler		"Shutdown requested");
460487a54bcbe7b6dac1a856b0991e6d13c34a1c423Sheng-Liang Song	TEST_EQ(VbGetMode(), VB_EC_RECOVERY, "vboot_mode recovery");
461487a54bcbe7b6dac1a856b0991e6d13c34a1c423Sheng-Liang Song
4627f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	VbNvGet(VbApiKernelGetVnc(), VBNV_RECOVERY_REQUEST, &u);
4637f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	TEST_EQ(u, 0, "  recovery reason");
4647f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	TEST_EQ(screens_displayed[0], VB_SCREEN_BLANK,
4657f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler		"  blank screen");
4667f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	TEST_EQ(screens_displayed[1], VB_SCREEN_RECOVERY_NO_GOOD,
4677f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler		"  no good screen");
4687f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler
4697f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	/* Disk inserted after start */
4707f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	ResetMocks();
4717f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	vbtlk_retval = VBERROR_SUCCESS - VB_DISK_FLAG_REMOVABLE;
4727f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	TEST_EQ(VbBootRecovery(&cparams, &lkp), 0, "Good");
4737f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler
4747f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	/* No disk inserted */
4757f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	ResetMocks();
4767f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	vbtlk_retval = VBERROR_NO_DISK_FOUND - VB_DISK_FLAG_REMOVABLE;
4777f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	shutdown_request_calls_left = 10;
4787f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	TEST_EQ(VbBootRecovery(&cparams, &lkp), VBERROR_SHUTDOWN_REQUESTED,
4797f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler		"Bad disk");
4807f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	TEST_EQ(screens_displayed[0], VB_SCREEN_BLANK,
4817f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler		"  blank screen");
4827f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	TEST_EQ(screens_displayed[1], VB_SCREEN_RECOVERY_INSERT,
4837f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler		"  insert screen");
4847f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler
4857f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	/* Remove disks */
4867f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	ResetMocks();
4877f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	shutdown_request_calls_left = 100;
4887f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	mock_num_disks[0] = 1;
4897f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	mock_num_disks[1] = 1;
49004171532583052935121a3e33550cc39ef2625ecShawn Nematbakhsh	mock_num_disks[2] = 1;
4917f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	vbtlk_retval = VBERROR_NO_DISK_FOUND - VB_DISK_FLAG_REMOVABLE;
4927f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	TEST_EQ(VbBootRecovery(&cparams, &lkp), VBERROR_SHUTDOWN_REQUESTED,
4937f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler		"Remove");
4947f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	TEST_EQ(screens_displayed[0], VB_SCREEN_RECOVERY_REMOVE,
4957f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler		"  remove screen");
4967f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	TEST_EQ(screens_displayed[1], VB_SCREEN_RECOVERY_REMOVE,
4977f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler		"  remove screen");
4987f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	TEST_EQ(screens_displayed[2], VB_SCREEN_BLANK,
4997f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler		"  blank screen");
5007f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	TEST_EQ(screens_displayed[3], VB_SCREEN_RECOVERY_INSERT,
5017f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler		"  insert screen");
5027f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler
5037f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	/* No removal if dev switch is on */
5047f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	ResetMocks();
5057f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	shutdown_request_calls_left = 100;
5067f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	mock_num_disks[0] = 1;
5077f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	mock_num_disks[1] = 1;
5087f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	shared->flags |= VBSD_BOOT_DEV_SWITCH_ON;
5097f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	vbtlk_retval = VBERROR_NO_DISK_FOUND - VB_DISK_FLAG_REMOVABLE;
5107f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	TEST_EQ(VbBootRecovery(&cparams, &lkp), VBERROR_SHUTDOWN_REQUESTED,
5117f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler		"No remove in dev");
5127f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	TEST_EQ(screens_displayed[0], VB_SCREEN_RECOVERY_INSERT,
5137f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler		"  insert screen");
5147f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler
5157f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	/* No removal if recovery button physically pressed */
5167f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	ResetMocks();
5177f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	shutdown_request_calls_left = 100;
5187f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	mock_num_disks[0] = 1;
5197f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	mock_num_disks[1] = 1;
5207f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	shared->flags |= VBSD_BOOT_REC_SWITCH_ON;
5217f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	vbtlk_retval = VBERROR_NO_DISK_FOUND - VB_DISK_FLAG_REMOVABLE;
5227f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	TEST_EQ(VbBootRecovery(&cparams, &lkp), VBERROR_SHUTDOWN_REQUESTED,
5237f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler		"No remove in rec");
5247f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	TEST_EQ(screens_displayed[0], VB_SCREEN_RECOVERY_INSERT,
5257f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler		"  insert screen");
5267f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler
52704171532583052935121a3e33550cc39ef2625ecShawn Nematbakhsh	/* Removal if no disk initially found, but found on second attempt */
52804171532583052935121a3e33550cc39ef2625ecShawn Nematbakhsh	ResetMocks();
52904171532583052935121a3e33550cc39ef2625ecShawn Nematbakhsh	shutdown_request_calls_left = 100;
53004171532583052935121a3e33550cc39ef2625ecShawn Nematbakhsh	mock_num_disks[0] = 0;
53104171532583052935121a3e33550cc39ef2625ecShawn Nematbakhsh	mock_num_disks[1] = 1;
53204171532583052935121a3e33550cc39ef2625ecShawn Nematbakhsh	vbtlk_retval = VBERROR_NO_DISK_FOUND - VB_DISK_FLAG_REMOVABLE;
53304171532583052935121a3e33550cc39ef2625ecShawn Nematbakhsh	TEST_EQ(VbBootRecovery(&cparams, &lkp), VBERROR_SHUTDOWN_REQUESTED,
53404171532583052935121a3e33550cc39ef2625ecShawn Nematbakhsh		"Remove");
53504171532583052935121a3e33550cc39ef2625ecShawn Nematbakhsh	TEST_EQ(screens_displayed[0], VB_SCREEN_RECOVERY_REMOVE,
53604171532583052935121a3e33550cc39ef2625ecShawn Nematbakhsh		"  remove screen");
53704171532583052935121a3e33550cc39ef2625ecShawn Nematbakhsh	TEST_EQ(screens_displayed[1], VB_SCREEN_BLANK,
53804171532583052935121a3e33550cc39ef2625ecShawn Nematbakhsh		"  blank screen");
53904171532583052935121a3e33550cc39ef2625ecShawn Nematbakhsh	TEST_EQ(screens_displayed[2], VB_SCREEN_RECOVERY_INSERT,
54004171532583052935121a3e33550cc39ef2625ecShawn Nematbakhsh		"  insert screen");
54104171532583052935121a3e33550cc39ef2625ecShawn Nematbakhsh
5427f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	/* Bad disk count doesn't require removal */
5437f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	ResetMocks();
5447f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	mock_num_disks[0] = -1;
5457f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	vbtlk_retval = VBERROR_NO_DISK_FOUND - VB_DISK_FLAG_REMOVABLE;
5467f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	shutdown_request_calls_left = 10;
5477f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	TEST_EQ(VbBootRecovery(&cparams, &lkp), VBERROR_SHUTDOWN_REQUESTED,
5487f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler		"Bad disk count");
5497f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	TEST_EQ(screens_displayed[0], VB_SCREEN_BLANK,
5507f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler		"  blank screen");
5517f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	TEST_EQ(screens_displayed[1], VB_SCREEN_RECOVERY_INSERT,
5527f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler		"  insert screen");
5537f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler
5547f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	/* Ctrl+D ignored for many reasons... */
5557f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	ResetMocks();
5567f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	shared->flags = VBSD_HONOR_VIRT_DEV_SWITCH | VBSD_BOOT_REC_SWITCH_ON;
5577f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	shutdown_request_calls_left = 100;
5587f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	mock_keypress[0] = 0x04;
5597f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	trust_ec = 0;
5607f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	TEST_EQ(VbBootRecovery(&cparams, &lkp), VBERROR_SHUTDOWN_REQUESTED,
5617f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler		"Ctrl+D ignored if EC not trusted");
5627f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	TEST_EQ(virtdev_set, 0, "  virtual dev mode off");
5637f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	TEST_NEQ(screens_displayed[1], VB_SCREEN_RECOVERY_TO_DEV,
5647f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler		 "  todev screen");
5657f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler
5667f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	ResetMocks();
5677f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	shared->flags = VBSD_HONOR_VIRT_DEV_SWITCH | VBSD_BOOT_REC_SWITCH_ON |
5687f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler		VBSD_BOOT_DEV_SWITCH_ON;
5697f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	trust_ec = 1;
5707f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	shutdown_request_calls_left = 100;
5717f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	mock_keypress[0] = 0x04;
5727f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	TEST_EQ(VbBootRecovery(&cparams, &lkp), VBERROR_SHUTDOWN_REQUESTED,
5737f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler		"Ctrl+D ignored if already in dev mode");
5747f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	TEST_EQ(virtdev_set, 0, "  virtual dev mode off");
5757f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	TEST_NEQ(screens_displayed[1], VB_SCREEN_RECOVERY_TO_DEV,
5767f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler		 "  todev screen");
5777f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler
5787f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	ResetMocks();
5797f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	shared->flags = VBSD_HONOR_VIRT_DEV_SWITCH;
5807f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	trust_ec = 1;
5817f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	shutdown_request_calls_left = 100;
5827f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	mock_keypress[0] = 0x04;
5837f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	TEST_EQ(VbBootRecovery(&cparams, &lkp), VBERROR_SHUTDOWN_REQUESTED,
5847f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler		"Ctrl+D ignored if recovery not manually triggered");
5857f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	TEST_EQ(virtdev_set, 0, "  virtual dev mode off");
5867f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	TEST_NEQ(screens_displayed[1], VB_SCREEN_RECOVERY_TO_DEV,
5877f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler		 "  todev screen");
5887f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler
5897f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	ResetMocks();
5907f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	shared->flags = VBSD_BOOT_REC_SWITCH_ON;
5917f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	trust_ec = 1;
5927f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	shutdown_request_calls_left = 100;
5937f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	mock_keypress[0] = 0x04;
5947f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	TEST_EQ(VbBootRecovery(&cparams, &lkp), VBERROR_SHUTDOWN_REQUESTED,
5957f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler		"Ctrl+D ignored if no virtual dev switch");
5967f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	TEST_EQ(virtdev_set, 0, "  virtual dev mode off");
5977f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	TEST_NEQ(screens_displayed[1], VB_SCREEN_RECOVERY_TO_DEV,
5987f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler		 "  todev screen");
5997f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler
600a53a0b040f45a1086515e7a5c8a8326c0b1d1f74Luigi Semenzato	/* Ctrl+D ignored because the physical recovery switch is still pressed
601a53a0b040f45a1086515e7a5c8a8326c0b1d1f74Luigi Semenzato	 * and we don't like that.
602a53a0b040f45a1086515e7a5c8a8326c0b1d1f74Luigi Semenzato	 */
603a53a0b040f45a1086515e7a5c8a8326c0b1d1f74Luigi Semenzato	ResetMocks();
604a53a0b040f45a1086515e7a5c8a8326c0b1d1f74Luigi Semenzato	shared->flags = VBSD_BOOT_REC_SWITCH_ON;
605a53a0b040f45a1086515e7a5c8a8326c0b1d1f74Luigi Semenzato	trust_ec = 1;
606a53a0b040f45a1086515e7a5c8a8326c0b1d1f74Luigi Semenzato	shutdown_request_calls_left = 100;
607a53a0b040f45a1086515e7a5c8a8326c0b1d1f74Luigi Semenzato	mock_keypress[0] = 0x04;
608a53a0b040f45a1086515e7a5c8a8326c0b1d1f74Luigi Semenzato	mock_switches[0] = VB_INIT_FLAG_REC_BUTTON_PRESSED;
609a53a0b040f45a1086515e7a5c8a8326c0b1d1f74Luigi Semenzato	TEST_EQ(VbBootRecovery(&cparams, &lkp), VBERROR_SHUTDOWN_REQUESTED,
610a53a0b040f45a1086515e7a5c8a8326c0b1d1f74Luigi Semenzato		"Ctrl+D ignored if phys rec button is still pressed");
611a53a0b040f45a1086515e7a5c8a8326c0b1d1f74Luigi Semenzato	TEST_NEQ(screens_displayed[1], VB_SCREEN_RECOVERY_TO_DEV,
612a53a0b040f45a1086515e7a5c8a8326c0b1d1f74Luigi Semenzato		 "  todev screen");
613a53a0b040f45a1086515e7a5c8a8326c0b1d1f74Luigi Semenzato
6147f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	/* Ctrl+D then space means don't enable */
6157f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	ResetMocks();
6167f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	shared->flags = VBSD_HONOR_VIRT_DEV_SWITCH | VBSD_BOOT_REC_SWITCH_ON;
6177f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	shutdown_request_calls_left = 100;
6187f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	vbtlk_retval = VBERROR_NO_DISK_FOUND - VB_DISK_FLAG_REMOVABLE;
6197f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	trust_ec = 1;
6207f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	mock_keypress[0] = 0x04;
6217f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	mock_keypress[1] = ' ';
6227f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	TEST_EQ(VbBootRecovery(&cparams, &lkp), VBERROR_SHUTDOWN_REQUESTED,
6237f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler		"Ctrl+D todev abort");
6247f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	TEST_EQ(screens_displayed[0], VB_SCREEN_RECOVERY_INSERT,
6257f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler		"  insert screen");
6267f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	TEST_EQ(screens_displayed[1], VB_SCREEN_RECOVERY_TO_DEV,
6277f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler		"  todev screen");
6287f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	TEST_EQ(screens_displayed[2], VB_SCREEN_RECOVERY_INSERT,
6297f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler		"  insert screen");
6307f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	TEST_EQ(virtdev_set, 0, "  virtual dev mode off");
6317f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler
6327f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	/* Ctrl+D then enter means enable */
6337f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	ResetMocks();
6347f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	shared->flags = VBSD_HONOR_VIRT_DEV_SWITCH | VBSD_BOOT_REC_SWITCH_ON;
6357f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	shutdown_request_calls_left = 100;
6367f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	vbtlk_retval = VBERROR_NO_DISK_FOUND - VB_DISK_FLAG_REMOVABLE;
6377f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	trust_ec = 1;
6387f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	mock_keypress[0] = 0x04;
6397f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	mock_keypress[1] = '\r';
640a53a0b040f45a1086515e7a5c8a8326c0b1d1f74Luigi Semenzato	mock_keyflags[1] = VB_KEY_FLAG_TRUSTED_KEYBOARD;
6417f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	TEST_EQ(VbBootRecovery(&cparams, &lkp), VBERROR_TPM_REBOOT_REQUIRED,
6427f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler		"Ctrl+D todev confirm");
6437f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	TEST_EQ(virtdev_set, 1, "  virtual dev mode on");
6447f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler
6457f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	/* Handle TPM error in enabling dev mode */
6467f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	ResetMocks();
6477f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	shared->flags = VBSD_HONOR_VIRT_DEV_SWITCH | VBSD_BOOT_REC_SWITCH_ON;
6487f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	shutdown_request_calls_left = 100;
6497f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	vbtlk_retval = VBERROR_NO_DISK_FOUND - VB_DISK_FLAG_REMOVABLE;
6507f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	trust_ec = 1;
6517f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	mock_keypress[0] = 0x04;
6527f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	mock_keypress[1] = '\r';
653a53a0b040f45a1086515e7a5c8a8326c0b1d1f74Luigi Semenzato	mock_keyflags[1] = VB_KEY_FLAG_TRUSTED_KEYBOARD;
6547f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	virtdev_retval = VBERROR_SIMULATED;
6557f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	TEST_EQ(VbBootRecovery(&cparams, &lkp), VBERROR_TPM_SET_BOOT_MODE_STATE,
6567f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler		"Ctrl+D todev failure");
6577f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler
6587f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	printf("...done.\n");
6597f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler}
6607f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler
6617f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler
6627f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spanglerint main(void)
6637f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler{
6647f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	VbUserConfirmsTest();
6657f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	VbBootTest();
6667f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	VbBootDevTest();
6677f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	VbBootRecTest();
6687f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler
6692500185a83b453580f187087fffc6376f19f8ff0Simon Glass	if (vboot_api_stub_check_memory())
6702500185a83b453580f187087fffc6376f19f8ff0Simon Glass		return 255;
6712500185a83b453580f187087fffc6376f19f8ff0Simon Glass
6727f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler	return gTestSuccess ? 0 : 255;
6737f43669630cb42e40ca6ddc1128eefea8fd339d9Randall Spangler}
674