Randall Spangler | 7f43669 | 2013-02-05 12:42:36 -0800 | [diff] [blame] | 1 | /* 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 Richardson | 0c3ba24 | 2013-03-29 11:09:30 -0700 | [diff] [blame] | 8 | #include <stdint.h> |
Randall Spangler | 7f43669 | 2013-02-05 12:42:36 -0800 | [diff] [blame] | 9 | #include <stdio.h> |
| 10 | #include <stdlib.h> |
| 11 | |
| 12 | #include "gbb_header.h" |
| 13 | #include "host_common.h" |
| 14 | #include "load_kernel_fw.h" |
| 15 | #include "rollback_index.h" |
| 16 | #include "test_common.h" |
| 17 | #include "vboot_audio.h" |
| 18 | #include "vboot_common.h" |
| 19 | #include "vboot_kernel.h" |
| 20 | #include "vboot_nvstorage.h" |
| 21 | #include "vboot_struct.h" |
| 22 | |
| 23 | /* Mock data */ |
| 24 | static VbCommonParams cparams; |
| 25 | static uint8_t shared_data[VB_SHARED_DATA_MIN_SIZE]; |
| 26 | static VbSharedDataHeader *shared = (VbSharedDataHeader *)shared_data; |
| 27 | static GoogleBinaryBlockHeader gbb; |
| 28 | static LoadKernelParams lkp; |
| 29 | |
| 30 | static int shutdown_request_calls_left; |
| 31 | static int audio_looping_calls_left; |
| 32 | static uint32_t vbtlk_retval; |
| 33 | static int vbexlegacy_called; |
| 34 | static int trust_ec; |
| 35 | static int virtdev_set; |
| 36 | static uint32_t virtdev_retval; |
| 37 | |
| 38 | static uint32_t mock_keypress[8]; |
| 39 | static uint32_t mock_keypress_count; |
| 40 | static uint32_t screens_displayed[8]; |
| 41 | static uint32_t screens_count = 0; |
| 42 | static uint32_t mock_num_disks[8]; |
| 43 | static uint32_t mock_num_disks_count; |
| 44 | |
| 45 | /* Reset mock data (for use before each test) */ |
| 46 | static void ResetMocks(void) |
| 47 | { |
| 48 | Memset(&cparams, 0, sizeof(cparams)); |
| 49 | cparams.shared_data_size = sizeof(shared_data); |
| 50 | cparams.shared_data_blob = shared_data; |
| 51 | cparams.gbb_data = &gbb; |
Simon Glass | 527ba81 | 2013-07-25 08:48:47 -0600 | [diff] [blame] | 52 | cparams.gbb = &gbb; |
Randall Spangler | 7f43669 | 2013-02-05 12:42:36 -0800 | [diff] [blame] | 53 | |
| 54 | Memset(&gbb, 0, sizeof(gbb)); |
| 55 | gbb.major_version = GBB_MAJOR_VER; |
| 56 | gbb.minor_version = GBB_MINOR_VER; |
| 57 | gbb.flags = 0; |
| 58 | |
| 59 | /* |
| 60 | * Only the outermost vboot_api_kernel call sets vboot_api_kernel's |
| 61 | * vnc. So clear it here too. |
| 62 | */ |
| 63 | Memset(VbApiKernelGetVnc(), 0, sizeof(VbNvContext)); |
| 64 | VbNvSetup(VbApiKernelGetVnc()); |
| 65 | VbNvTeardown(VbApiKernelGetVnc()); /* So CRC gets generated */ |
| 66 | |
| 67 | Memset(&shared_data, 0, sizeof(shared_data)); |
| 68 | VbSharedDataInit(shared, sizeof(shared_data)); |
| 69 | |
| 70 | Memset(&lkp, 0, sizeof(lkp)); |
| 71 | |
| 72 | shutdown_request_calls_left = -1; |
| 73 | audio_looping_calls_left = 30; |
| 74 | vbtlk_retval = 1000; |
| 75 | vbexlegacy_called = 0; |
| 76 | trust_ec = 0; |
| 77 | virtdev_set = 0; |
| 78 | virtdev_retval = 0; |
| 79 | |
| 80 | Memset(screens_displayed, 0, sizeof(screens_displayed)); |
| 81 | screens_count = 0; |
| 82 | |
| 83 | Memset(mock_keypress, 0, sizeof(mock_keypress)); |
| 84 | mock_keypress_count = 0; |
| 85 | |
| 86 | Memset(mock_num_disks, 0, sizeof(mock_num_disks)); |
| 87 | mock_num_disks_count = 0; |
| 88 | } |
| 89 | |
| 90 | /* Mock functions */ |
| 91 | |
| 92 | uint32_t VbExIsShutdownRequested(void) |
| 93 | { |
| 94 | if (shutdown_request_calls_left == 0) |
| 95 | return 1; |
| 96 | else if (shutdown_request_calls_left > 0) |
| 97 | shutdown_request_calls_left--; |
| 98 | |
| 99 | return 0; |
| 100 | } |
| 101 | |
| 102 | uint32_t VbExKeyboardRead(void) |
| 103 | { |
| 104 | if (mock_keypress_count < ARRAY_SIZE(mock_keypress)) |
| 105 | return mock_keypress[mock_keypress_count++]; |
| 106 | else |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | int VbExLegacy(void) |
| 111 | { |
| 112 | vbexlegacy_called++; |
| 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | VbError_t VbExDiskGetInfo(VbDiskInfo **infos_ptr, uint32_t *count, |
| 117 | uint32_t disk_flags) |
| 118 | { |
| 119 | if (mock_num_disks_count < ARRAY_SIZE(mock_num_disks)) { |
| 120 | if (mock_num_disks[mock_num_disks_count] == -1) |
| 121 | return VBERROR_SIMULATED; |
| 122 | else |
| 123 | *count = mock_num_disks[mock_num_disks_count++]; |
| 124 | } else { |
| 125 | *count = 0; |
| 126 | } |
| 127 | return VBERROR_SUCCESS; |
| 128 | } |
| 129 | |
| 130 | VbError_t VbExDiskFreeInfo(VbDiskInfo *infos, |
| 131 | VbExDiskHandle_t preserve_handle) |
| 132 | { |
| 133 | return VBERROR_SUCCESS; |
| 134 | } |
| 135 | |
| 136 | int VbExTrustEC(void) |
| 137 | { |
| 138 | return trust_ec; |
| 139 | } |
| 140 | |
| 141 | int VbAudioLooping(VbAudioContext *audio) |
| 142 | { |
| 143 | if (audio_looping_calls_left == 0) |
| 144 | return 0; |
| 145 | else if (audio_looping_calls_left > 0) |
| 146 | audio_looping_calls_left--; |
| 147 | |
| 148 | return 1; |
| 149 | } |
| 150 | |
| 151 | uint32_t VbTryLoadKernel(VbCommonParams *cparams, LoadKernelParams *p, |
| 152 | uint32_t get_info_flags) |
| 153 | { |
| 154 | return vbtlk_retval + get_info_flags; |
| 155 | } |
| 156 | |
| 157 | VbError_t VbDisplayScreen(VbCommonParams *cparams, uint32_t screen, int force, |
| 158 | VbNvContext *vncptr) |
| 159 | { |
| 160 | if (screens_count < ARRAY_SIZE(screens_displayed)) |
| 161 | screens_displayed[screens_count++] = screen; |
| 162 | |
| 163 | return VBERROR_SUCCESS; |
| 164 | } |
| 165 | |
| 166 | uint32_t SetVirtualDevMode(int val) |
| 167 | { |
| 168 | virtdev_set = val; |
| 169 | return virtdev_retval; |
| 170 | } |
| 171 | |
| 172 | /* Tests */ |
| 173 | |
| 174 | static void VbUserConfirmsTest(void) |
| 175 | { |
| 176 | printf("Testing VbUserConfirms()...\n"); |
| 177 | |
| 178 | ResetMocks(); |
| 179 | shutdown_request_calls_left = 1; |
| 180 | TEST_EQ(VbUserConfirms(&cparams, 0), -1, "Shutdown requested"); |
| 181 | |
| 182 | ResetMocks(); |
| 183 | mock_keypress[0] = '\r'; |
| 184 | TEST_EQ(VbUserConfirms(&cparams, 0), 1, "Enter"); |
| 185 | |
| 186 | ResetMocks(); |
| 187 | mock_keypress[0] = 0x1b; |
| 188 | TEST_EQ(VbUserConfirms(&cparams, 0), 0, "Esc"); |
| 189 | |
| 190 | ResetMocks(); |
| 191 | mock_keypress[0] = ' '; |
| 192 | shutdown_request_calls_left = 1; |
| 193 | TEST_EQ(VbUserConfirms(&cparams, 1), 0, "Space means no"); |
| 194 | |
| 195 | ResetMocks(); |
| 196 | mock_keypress[0] = ' '; |
| 197 | shutdown_request_calls_left = 1; |
| 198 | TEST_EQ(VbUserConfirms(&cparams, 0), -1, "Space ignored"); |
| 199 | |
| 200 | printf("...done.\n"); |
| 201 | } |
| 202 | |
| 203 | static void VbBootTest(void) |
| 204 | { |
| 205 | ResetMocks(); |
| 206 | TEST_EQ(VbBootNormal(&cparams, &lkp), 1002, "VbBootNormal()"); |
| 207 | } |
| 208 | |
| 209 | static void VbBootDevTest(void) |
| 210 | { |
| 211 | uint32_t u; |
| 212 | |
| 213 | printf("Testing VbBootDeveloper()...\n"); |
| 214 | |
| 215 | /* Proceed after timeout */ |
| 216 | ResetMocks(); |
| 217 | TEST_EQ(VbBootDeveloper(&cparams, &lkp), 1002, "Timeout"); |
| 218 | TEST_EQ(screens_displayed[0], VB_SCREEN_DEVELOPER_WARNING, |
| 219 | " warning screen"); |
| 220 | VbNvGet(VbApiKernelGetVnc(), VBNV_RECOVERY_REQUEST, &u); |
| 221 | TEST_EQ(u, 0, " recovery reason"); |
| 222 | TEST_EQ(audio_looping_calls_left, 0, " used up audio"); |
| 223 | |
Randall Spangler | f2a1dc0 | 2013-06-11 16:53:07 -0700 | [diff] [blame] | 224 | /* Proceed to legacy after timeout if GBB flag set */ |
| 225 | ResetMocks(); |
| 226 | gbb.flags |= GBB_FLAG_DEFAULT_DEV_BOOT_LEGACY; |
| 227 | TEST_EQ(VbBootDeveloper(&cparams, &lkp), 1002, "Timeout"); |
| 228 | TEST_EQ(vbexlegacy_called, 1, " try legacy"); |
| 229 | |
Randall Spangler | 7f43669 | 2013-02-05 12:42:36 -0800 | [diff] [blame] | 230 | /* Up arrow is uninteresting / passed to VbCheckDisplayKey() */ |
| 231 | ResetMocks(); |
| 232 | mock_keypress[0] = VB_KEY_UP; |
| 233 | TEST_EQ(VbBootDeveloper(&cparams, &lkp), 1002, "Up arrow"); |
| 234 | |
| 235 | /* Shutdown requested in loop */ |
| 236 | ResetMocks(); |
| 237 | shutdown_request_calls_left = 2; |
| 238 | TEST_EQ(VbBootDeveloper(&cparams, &lkp), VBERROR_SHUTDOWN_REQUESTED, |
| 239 | "Shutdown requested"); |
| 240 | TEST_NEQ(audio_looping_calls_left, 0, " aborts audio"); |
| 241 | |
| 242 | /* Space goes straight to recovery if no virtual dev switch */ |
| 243 | ResetMocks(); |
| 244 | mock_keypress[0] = ' '; |
| 245 | TEST_EQ(VbBootDeveloper(&cparams, &lkp), VBERROR_LOAD_KERNEL_RECOVERY, |
| 246 | "Space = recovery"); |
| 247 | VbNvGet(VbApiKernelGetVnc(), VBNV_RECOVERY_REQUEST, &u); |
| 248 | TEST_EQ(u, VBNV_RECOVERY_RW_DEV_SCREEN, " recovery reason"); |
| 249 | |
| 250 | /* Space asks to disable virtual dev switch */ |
| 251 | ResetMocks(); |
| 252 | shared->flags = VBSD_HONOR_VIRT_DEV_SWITCH | VBSD_BOOT_DEV_SWITCH_ON; |
| 253 | mock_keypress[0] = ' '; |
| 254 | mock_keypress[1] = '\r'; |
| 255 | TEST_EQ(VbBootDeveloper(&cparams, &lkp), VBERROR_TPM_REBOOT_REQUIRED, |
| 256 | "Space = tonorm"); |
| 257 | TEST_EQ(screens_displayed[0], VB_SCREEN_DEVELOPER_WARNING, |
| 258 | " warning screen"); |
| 259 | TEST_EQ(screens_displayed[1], VB_SCREEN_DEVELOPER_TO_NORM, |
| 260 | " tonorm screen"); |
| 261 | TEST_EQ(screens_displayed[2], VB_SCREEN_TO_NORM_CONFIRMED, |
| 262 | " confirm screen"); |
| 263 | VbNvGet(VbApiKernelGetVnc(), VBNV_DISABLE_DEV_REQUEST, &u); |
| 264 | TEST_EQ(u, 1, " disable dev request"); |
| 265 | |
| 266 | /* Space-space doesn't disable it */ |
| 267 | ResetMocks(); |
| 268 | shared->flags = VBSD_HONOR_VIRT_DEV_SWITCH | VBSD_BOOT_DEV_SWITCH_ON; |
| 269 | mock_keypress[0] = ' '; |
| 270 | mock_keypress[1] = ' '; |
| 271 | mock_keypress[2] = 0x1b; |
| 272 | TEST_EQ(VbBootDeveloper(&cparams, &lkp), 1002, "Space-space"); |
| 273 | TEST_EQ(screens_displayed[0], VB_SCREEN_DEVELOPER_WARNING, |
| 274 | " warning screen"); |
| 275 | TEST_EQ(screens_displayed[1], VB_SCREEN_DEVELOPER_TO_NORM, |
| 276 | " tonorm screen"); |
| 277 | TEST_EQ(screens_displayed[2], VB_SCREEN_DEVELOPER_WARNING, |
| 278 | " warning screen"); |
| 279 | |
| 280 | /* Enter doesn't by default */ |
| 281 | ResetMocks(); |
| 282 | shared->flags = VBSD_HONOR_VIRT_DEV_SWITCH | VBSD_BOOT_DEV_SWITCH_ON; |
| 283 | mock_keypress[0] = '\r'; |
| 284 | mock_keypress[1] = '\r'; |
| 285 | TEST_EQ(VbBootDeveloper(&cparams, &lkp), 1002, "Enter ignored"); |
| 286 | |
| 287 | /* Enter does if GBB flag set */ |
| 288 | ResetMocks(); |
| 289 | shared->flags = VBSD_HONOR_VIRT_DEV_SWITCH | VBSD_BOOT_DEV_SWITCH_ON; |
| 290 | gbb.flags |= GBB_FLAG_ENTER_TRIGGERS_TONORM; |
| 291 | mock_keypress[0] = '\r'; |
| 292 | mock_keypress[1] = '\r'; |
| 293 | TEST_EQ(VbBootDeveloper(&cparams, &lkp), VBERROR_TPM_REBOOT_REQUIRED, |
| 294 | "Enter = tonorm"); |
| 295 | |
| 296 | /* Tonorm ignored if GBB forces dev switch on */ |
| 297 | ResetMocks(); |
| 298 | shared->flags = VBSD_HONOR_VIRT_DEV_SWITCH | VBSD_BOOT_DEV_SWITCH_ON; |
| 299 | gbb.flags |= GBB_FLAG_FORCE_DEV_SWITCH_ON; |
| 300 | mock_keypress[0] = ' '; |
| 301 | mock_keypress[1] = '\r'; |
| 302 | TEST_EQ(VbBootDeveloper(&cparams, &lkp), 1002, "Can't tonorm gbb-dev"); |
| 303 | |
| 304 | /* Shutdown requested at tonorm screen */ |
| 305 | ResetMocks(); |
| 306 | shared->flags = VBSD_HONOR_VIRT_DEV_SWITCH | VBSD_BOOT_DEV_SWITCH_ON; |
| 307 | mock_keypress[0] = ' '; |
| 308 | shutdown_request_calls_left = 2; |
| 309 | TEST_EQ(VbBootDeveloper(&cparams, &lkp), VBERROR_SHUTDOWN_REQUESTED, |
| 310 | "Shutdown requested at tonorm"); |
| 311 | TEST_EQ(screens_displayed[0], VB_SCREEN_DEVELOPER_WARNING, |
| 312 | " warning screen"); |
| 313 | TEST_EQ(screens_displayed[1], VB_SCREEN_DEVELOPER_TO_NORM, |
| 314 | " tonorm screen"); |
| 315 | |
| 316 | /* Ctrl+D dismisses warning */ |
| 317 | ResetMocks(); |
| 318 | mock_keypress[0] = 0x04; |
| 319 | TEST_EQ(VbBootDeveloper(&cparams, &lkp), 1002, "Ctrl+D"); |
| 320 | VbNvGet(VbApiKernelGetVnc(), VBNV_RECOVERY_REQUEST, &u); |
| 321 | TEST_EQ(u, 0, " recovery reason"); |
| 322 | TEST_NEQ(audio_looping_calls_left, 0, " aborts audio"); |
Randall Spangler | f2a1dc0 | 2013-06-11 16:53:07 -0700 | [diff] [blame] | 323 | TEST_EQ(vbexlegacy_called, 0, " not legacy"); |
| 324 | |
| 325 | /* Ctrl+D doesn't boot legacy even if GBB flag is set */ |
| 326 | ResetMocks(); |
| 327 | mock_keypress[0] = 0x04; |
| 328 | gbb.flags |= GBB_FLAG_DEFAULT_DEV_BOOT_LEGACY; |
| 329 | TEST_EQ(VbBootDeveloper(&cparams, &lkp), 1002, "Ctrl+D"); |
| 330 | TEST_EQ(vbexlegacy_called, 0, " not legacy"); |
Randall Spangler | 7f43669 | 2013-02-05 12:42:36 -0800 | [diff] [blame] | 331 | |
| 332 | /* Ctrl+L tries legacy boot mode only if enabled */ |
| 333 | ResetMocks(); |
| 334 | mock_keypress[0] = 0x0c; |
| 335 | TEST_EQ(VbBootDeveloper(&cparams, &lkp), 1002, "Ctrl+L normal"); |
| 336 | TEST_EQ(vbexlegacy_called, 0, " not legacy"); |
| 337 | |
| 338 | ResetMocks(); |
| 339 | |
| 340 | gbb.flags |= GBB_FLAG_FORCE_DEV_BOOT_LEGACY; |
| 341 | mock_keypress[0] = 0x0c; |
| 342 | TEST_EQ(VbBootDeveloper(&cparams, &lkp), 1002, "Ctrl+L force legacy"); |
| 343 | TEST_EQ(vbexlegacy_called, 1, " try legacy"); |
| 344 | |
| 345 | ResetMocks(); |
| 346 | VbNvSet(VbApiKernelGetVnc(), VBNV_DEV_BOOT_LEGACY, 1); |
| 347 | mock_keypress[0] = 0x0c; |
| 348 | TEST_EQ(VbBootDeveloper(&cparams, &lkp), 1002, "Ctrl+L nv legacy"); |
| 349 | TEST_EQ(vbexlegacy_called, 1, " try legacy"); |
| 350 | |
| 351 | /* Ctrl+U boots USB only if enabled */ |
| 352 | ResetMocks(); |
| 353 | mock_keypress[0] = 0x15; |
| 354 | TEST_EQ(VbBootDeveloper(&cparams, &lkp), 1002, "Ctrl+U normal"); |
| 355 | |
| 356 | /* Ctrl+U enabled, with good USB boot */ |
| 357 | ResetMocks(); |
| 358 | VbNvSet(VbApiKernelGetVnc(), VBNV_DEV_BOOT_USB, 1); |
| 359 | mock_keypress[0] = 0x15; |
| 360 | vbtlk_retval = VBERROR_SUCCESS - VB_DISK_FLAG_REMOVABLE; |
| 361 | TEST_EQ(VbBootDeveloper(&cparams, &lkp), 0, "Ctrl+U USB"); |
| 362 | |
| 363 | /* Ctrl+U enabled via GBB */ |
| 364 | ResetMocks(); |
| 365 | gbb.flags |= GBB_FLAG_FORCE_DEV_BOOT_USB; |
| 366 | mock_keypress[0] = 0x15; |
| 367 | vbtlk_retval = VBERROR_SUCCESS - VB_DISK_FLAG_REMOVABLE; |
| 368 | TEST_EQ(VbBootDeveloper(&cparams, &lkp), 0, "Ctrl+U force USB"); |
| 369 | |
| 370 | /* If no USB, eventually times out and tries fixed disk */ |
| 371 | ResetMocks(); |
| 372 | VbNvSet(VbApiKernelGetVnc(), VBNV_DEV_BOOT_USB, 1); |
| 373 | mock_keypress[0] = 0x15; |
| 374 | TEST_EQ(VbBootDeveloper(&cparams, &lkp), 1002, "Ctrl+U enabled"); |
| 375 | TEST_EQ(vbexlegacy_called, 0, " not legacy"); |
| 376 | VbNvGet(VbApiKernelGetVnc(), VBNV_RECOVERY_REQUEST, &u); |
| 377 | TEST_EQ(u, 0, " recovery reason"); |
| 378 | TEST_EQ(audio_looping_calls_left, 0, " used up audio"); |
| 379 | |
| 380 | printf("...done.\n"); |
| 381 | } |
| 382 | |
| 383 | static void VbBootRecTest(void) |
| 384 | { |
| 385 | uint32_t u; |
| 386 | |
| 387 | printf("Testing VbBootRecovery()...\n"); |
| 388 | |
| 389 | /* Shutdown requested in loop */ |
| 390 | ResetMocks(); |
| 391 | shutdown_request_calls_left = 10; |
| 392 | TEST_EQ(VbBootRecovery(&cparams, &lkp), VBERROR_SHUTDOWN_REQUESTED, |
| 393 | "Shutdown requested"); |
| 394 | VbNvGet(VbApiKernelGetVnc(), VBNV_RECOVERY_REQUEST, &u); |
| 395 | TEST_EQ(u, 0, " recovery reason"); |
| 396 | TEST_EQ(screens_displayed[0], VB_SCREEN_BLANK, |
| 397 | " blank screen"); |
| 398 | TEST_EQ(screens_displayed[1], VB_SCREEN_RECOVERY_NO_GOOD, |
| 399 | " no good screen"); |
| 400 | |
| 401 | /* Disk inserted after start */ |
| 402 | ResetMocks(); |
| 403 | vbtlk_retval = VBERROR_SUCCESS - VB_DISK_FLAG_REMOVABLE; |
| 404 | TEST_EQ(VbBootRecovery(&cparams, &lkp), 0, "Good"); |
| 405 | |
| 406 | /* No disk inserted */ |
| 407 | ResetMocks(); |
| 408 | vbtlk_retval = VBERROR_NO_DISK_FOUND - VB_DISK_FLAG_REMOVABLE; |
| 409 | shutdown_request_calls_left = 10; |
| 410 | TEST_EQ(VbBootRecovery(&cparams, &lkp), VBERROR_SHUTDOWN_REQUESTED, |
| 411 | "Bad disk"); |
| 412 | TEST_EQ(screens_displayed[0], VB_SCREEN_BLANK, |
| 413 | " blank screen"); |
| 414 | TEST_EQ(screens_displayed[1], VB_SCREEN_RECOVERY_INSERT, |
| 415 | " insert screen"); |
| 416 | |
| 417 | /* Remove disks */ |
| 418 | ResetMocks(); |
| 419 | shutdown_request_calls_left = 100; |
| 420 | mock_num_disks[0] = 1; |
| 421 | mock_num_disks[1] = 1; |
Shawn Nematbakhsh | 0417153 | 2013-12-13 14:41:47 -0800 | [diff] [blame^] | 422 | mock_num_disks[2] = 1; |
Randall Spangler | 7f43669 | 2013-02-05 12:42:36 -0800 | [diff] [blame] | 423 | vbtlk_retval = VBERROR_NO_DISK_FOUND - VB_DISK_FLAG_REMOVABLE; |
| 424 | TEST_EQ(VbBootRecovery(&cparams, &lkp), VBERROR_SHUTDOWN_REQUESTED, |
| 425 | "Remove"); |
| 426 | TEST_EQ(screens_displayed[0], VB_SCREEN_RECOVERY_REMOVE, |
| 427 | " remove screen"); |
| 428 | TEST_EQ(screens_displayed[1], VB_SCREEN_RECOVERY_REMOVE, |
| 429 | " remove screen"); |
| 430 | TEST_EQ(screens_displayed[2], VB_SCREEN_BLANK, |
| 431 | " blank screen"); |
| 432 | TEST_EQ(screens_displayed[3], VB_SCREEN_RECOVERY_INSERT, |
| 433 | " insert screen"); |
| 434 | |
| 435 | /* No removal if dev switch is on */ |
| 436 | ResetMocks(); |
| 437 | shutdown_request_calls_left = 100; |
| 438 | mock_num_disks[0] = 1; |
| 439 | mock_num_disks[1] = 1; |
| 440 | shared->flags |= VBSD_BOOT_DEV_SWITCH_ON; |
| 441 | vbtlk_retval = VBERROR_NO_DISK_FOUND - VB_DISK_FLAG_REMOVABLE; |
| 442 | TEST_EQ(VbBootRecovery(&cparams, &lkp), VBERROR_SHUTDOWN_REQUESTED, |
| 443 | "No remove in dev"); |
| 444 | TEST_EQ(screens_displayed[0], VB_SCREEN_RECOVERY_INSERT, |
| 445 | " insert screen"); |
| 446 | |
| 447 | /* No removal if recovery button physically pressed */ |
| 448 | ResetMocks(); |
| 449 | shutdown_request_calls_left = 100; |
| 450 | mock_num_disks[0] = 1; |
| 451 | mock_num_disks[1] = 1; |
| 452 | shared->flags |= VBSD_BOOT_REC_SWITCH_ON; |
| 453 | vbtlk_retval = VBERROR_NO_DISK_FOUND - VB_DISK_FLAG_REMOVABLE; |
| 454 | TEST_EQ(VbBootRecovery(&cparams, &lkp), VBERROR_SHUTDOWN_REQUESTED, |
| 455 | "No remove in rec"); |
| 456 | TEST_EQ(screens_displayed[0], VB_SCREEN_RECOVERY_INSERT, |
| 457 | " insert screen"); |
| 458 | |
Shawn Nematbakhsh | 0417153 | 2013-12-13 14:41:47 -0800 | [diff] [blame^] | 459 | /* Removal if no disk initially found, but found on second attempt */ |
| 460 | ResetMocks(); |
| 461 | shutdown_request_calls_left = 100; |
| 462 | mock_num_disks[0] = 0; |
| 463 | mock_num_disks[1] = 1; |
| 464 | vbtlk_retval = VBERROR_NO_DISK_FOUND - VB_DISK_FLAG_REMOVABLE; |
| 465 | TEST_EQ(VbBootRecovery(&cparams, &lkp), VBERROR_SHUTDOWN_REQUESTED, |
| 466 | "Remove"); |
| 467 | TEST_EQ(screens_displayed[0], VB_SCREEN_RECOVERY_REMOVE, |
| 468 | " remove screen"); |
| 469 | TEST_EQ(screens_displayed[1], VB_SCREEN_BLANK, |
| 470 | " blank screen"); |
| 471 | TEST_EQ(screens_displayed[2], VB_SCREEN_RECOVERY_INSERT, |
| 472 | " insert screen"); |
| 473 | |
Randall Spangler | 7f43669 | 2013-02-05 12:42:36 -0800 | [diff] [blame] | 474 | /* Bad disk count doesn't require removal */ |
| 475 | ResetMocks(); |
| 476 | mock_num_disks[0] = -1; |
| 477 | vbtlk_retval = VBERROR_NO_DISK_FOUND - VB_DISK_FLAG_REMOVABLE; |
| 478 | shutdown_request_calls_left = 10; |
| 479 | TEST_EQ(VbBootRecovery(&cparams, &lkp), VBERROR_SHUTDOWN_REQUESTED, |
| 480 | "Bad disk count"); |
| 481 | TEST_EQ(screens_displayed[0], VB_SCREEN_BLANK, |
| 482 | " blank screen"); |
| 483 | TEST_EQ(screens_displayed[1], VB_SCREEN_RECOVERY_INSERT, |
| 484 | " insert screen"); |
| 485 | |
| 486 | /* Ctrl+D ignored for many reasons... */ |
| 487 | ResetMocks(); |
| 488 | shared->flags = VBSD_HONOR_VIRT_DEV_SWITCH | VBSD_BOOT_REC_SWITCH_ON; |
| 489 | shutdown_request_calls_left = 100; |
| 490 | mock_keypress[0] = 0x04; |
| 491 | trust_ec = 0; |
| 492 | TEST_EQ(VbBootRecovery(&cparams, &lkp), VBERROR_SHUTDOWN_REQUESTED, |
| 493 | "Ctrl+D ignored if EC not trusted"); |
| 494 | TEST_EQ(virtdev_set, 0, " virtual dev mode off"); |
| 495 | TEST_NEQ(screens_displayed[1], VB_SCREEN_RECOVERY_TO_DEV, |
| 496 | " todev screen"); |
| 497 | |
| 498 | ResetMocks(); |
| 499 | shared->flags = VBSD_HONOR_VIRT_DEV_SWITCH | VBSD_BOOT_REC_SWITCH_ON | |
| 500 | VBSD_BOOT_DEV_SWITCH_ON; |
| 501 | trust_ec = 1; |
| 502 | shutdown_request_calls_left = 100; |
| 503 | mock_keypress[0] = 0x04; |
| 504 | TEST_EQ(VbBootRecovery(&cparams, &lkp), VBERROR_SHUTDOWN_REQUESTED, |
| 505 | "Ctrl+D ignored if already in dev mode"); |
| 506 | TEST_EQ(virtdev_set, 0, " virtual dev mode off"); |
| 507 | TEST_NEQ(screens_displayed[1], VB_SCREEN_RECOVERY_TO_DEV, |
| 508 | " todev screen"); |
| 509 | |
| 510 | ResetMocks(); |
| 511 | shared->flags = VBSD_HONOR_VIRT_DEV_SWITCH; |
| 512 | trust_ec = 1; |
| 513 | shutdown_request_calls_left = 100; |
| 514 | mock_keypress[0] = 0x04; |
| 515 | TEST_EQ(VbBootRecovery(&cparams, &lkp), VBERROR_SHUTDOWN_REQUESTED, |
| 516 | "Ctrl+D ignored if recovery not manually triggered"); |
| 517 | TEST_EQ(virtdev_set, 0, " virtual dev mode off"); |
| 518 | TEST_NEQ(screens_displayed[1], VB_SCREEN_RECOVERY_TO_DEV, |
| 519 | " todev screen"); |
| 520 | |
| 521 | ResetMocks(); |
| 522 | shared->flags = VBSD_BOOT_REC_SWITCH_ON; |
| 523 | trust_ec = 1; |
| 524 | shutdown_request_calls_left = 100; |
| 525 | mock_keypress[0] = 0x04; |
| 526 | TEST_EQ(VbBootRecovery(&cparams, &lkp), VBERROR_SHUTDOWN_REQUESTED, |
| 527 | "Ctrl+D ignored if no virtual dev switch"); |
| 528 | TEST_EQ(virtdev_set, 0, " virtual dev mode off"); |
| 529 | TEST_NEQ(screens_displayed[1], VB_SCREEN_RECOVERY_TO_DEV, |
| 530 | " todev screen"); |
| 531 | |
| 532 | /* Ctrl+D then space means don't enable */ |
| 533 | ResetMocks(); |
| 534 | shared->flags = VBSD_HONOR_VIRT_DEV_SWITCH | VBSD_BOOT_REC_SWITCH_ON; |
| 535 | shutdown_request_calls_left = 100; |
| 536 | vbtlk_retval = VBERROR_NO_DISK_FOUND - VB_DISK_FLAG_REMOVABLE; |
| 537 | trust_ec = 1; |
| 538 | mock_keypress[0] = 0x04; |
| 539 | mock_keypress[1] = ' '; |
| 540 | TEST_EQ(VbBootRecovery(&cparams, &lkp), VBERROR_SHUTDOWN_REQUESTED, |
| 541 | "Ctrl+D todev abort"); |
| 542 | TEST_EQ(screens_displayed[0], VB_SCREEN_RECOVERY_INSERT, |
| 543 | " insert screen"); |
| 544 | TEST_EQ(screens_displayed[1], VB_SCREEN_RECOVERY_TO_DEV, |
| 545 | " todev screen"); |
| 546 | TEST_EQ(screens_displayed[2], VB_SCREEN_RECOVERY_INSERT, |
| 547 | " insert screen"); |
| 548 | TEST_EQ(virtdev_set, 0, " virtual dev mode off"); |
| 549 | |
| 550 | /* Ctrl+D then enter means enable */ |
| 551 | ResetMocks(); |
| 552 | shared->flags = VBSD_HONOR_VIRT_DEV_SWITCH | VBSD_BOOT_REC_SWITCH_ON; |
| 553 | shutdown_request_calls_left = 100; |
| 554 | vbtlk_retval = VBERROR_NO_DISK_FOUND - VB_DISK_FLAG_REMOVABLE; |
| 555 | trust_ec = 1; |
| 556 | mock_keypress[0] = 0x04; |
| 557 | mock_keypress[1] = '\r'; |
| 558 | TEST_EQ(VbBootRecovery(&cparams, &lkp), VBERROR_TPM_REBOOT_REQUIRED, |
| 559 | "Ctrl+D todev confirm"); |
| 560 | TEST_EQ(virtdev_set, 1, " virtual dev mode on"); |
| 561 | |
| 562 | /* Handle TPM error in enabling dev mode */ |
| 563 | ResetMocks(); |
| 564 | shared->flags = VBSD_HONOR_VIRT_DEV_SWITCH | VBSD_BOOT_REC_SWITCH_ON; |
| 565 | shutdown_request_calls_left = 100; |
| 566 | vbtlk_retval = VBERROR_NO_DISK_FOUND - VB_DISK_FLAG_REMOVABLE; |
| 567 | trust_ec = 1; |
| 568 | mock_keypress[0] = 0x04; |
| 569 | mock_keypress[1] = '\r'; |
| 570 | virtdev_retval = VBERROR_SIMULATED; |
| 571 | TEST_EQ(VbBootRecovery(&cparams, &lkp), VBERROR_TPM_SET_BOOT_MODE_STATE, |
| 572 | "Ctrl+D todev failure"); |
| 573 | |
| 574 | printf("...done.\n"); |
| 575 | } |
| 576 | |
| 577 | |
| 578 | int main(void) |
| 579 | { |
| 580 | VbUserConfirmsTest(); |
| 581 | VbBootTest(); |
| 582 | VbBootDevTest(); |
| 583 | VbBootRecTest(); |
| 584 | |
Simon Glass | 2500185 | 2013-08-16 02:47:57 -0600 | [diff] [blame] | 585 | if (vboot_api_stub_check_memory()) |
| 586 | return 255; |
| 587 | |
Randall Spangler | 7f43669 | 2013-02-05 12:42:36 -0800 | [diff] [blame] | 588 | return gTestSuccess ? 0 : 255; |
| 589 | } |