vboot_api_devmode_tests.c revision 8fa13ad6f727d44fdc0ae1d2bde5f54b32dab9b9
1/* Copyright (c) 2011 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_firmware
6 */
7
8#include <stddef.h>
9#include <stdint.h>
10#include <stdio.h>
11#include <stdlib.h>
12
13#include "crc32.h"
14#include "gbb_header.h"
15#include "host_common.h"
16#include "load_kernel_fw.h"
17#include "rollback_index.h"
18#include "test_common.h"
19#include "vboot_common.h"
20#include "vboot_display.h"
21#include "vboot_nvstorage.h"
22#include "vboot_struct.h"
23
24
25/* Expected results */
26
27#define MAX_NOTE_EVENTS 10
28#define TICKS_PER_MSEC 1900ULL
29#define TIME_FUZZ 500
30#define KBD_READ_TIME 60
31
32typedef struct {
33  uint16_t msec;
34  uint16_t freq;
35  int time;
36} note_event_t;
37
38typedef struct {
39  char *name;
40  uint32_t gbb_flags;
41  VbError_t beep_return;
42  uint32_t keypress_key;
43  int keypress_at_count;
44  int num_events;
45  note_event_t notes[MAX_NOTE_EVENTS];
46} test_case_t;
47
48test_case_t test[] = {
49
50  { "VbBootDeveloperSoundTest( fast, background )",
51    0x00000001, VBERROR_SUCCESS,
52    0, 0,
53    2,
54    {
55      {0, 0, 0},                        // probing for capability
56      {0, 0, 2000},                     // off and return at 2 seconds
57    }},
58
59  { "VbBootDeveloperSoundTest( normal, background )",
60    0x00000000, VBERROR_SUCCESS,
61    0, 0,
62    6,
63    {
64      {0, 0, 0},                        // probing for capability
65      {0, 400, 20000},                  // starts first beep at 20 seconds
66      {0, 0, 20250},                    // stops 250ms later
67      {0, 400, 20500},                  // starts second beep
68      {0, 0, 20750},                    // stops 250ms later
69      {0, 0, 30000},                    // off and return at 30 seconds
70    }},
71
72  { "VbBootDeveloperSoundTest( fast, no background )",
73    0x00000001, VBERROR_NO_BACKGROUND_SOUND,
74    0, 0,
75    2,
76    {
77      {0, 0, 0},                        // probing for capability
78      {0, 0, 2000},                     // off and return at 2 seconds
79    }},
80
81  { "VbBootDeveloperSoundTest( normal, no background )",
82    0x00000000, VBERROR_NO_BACKGROUND_SOUND,
83    0, 0,
84    4,
85    {
86      {0, 0, 0},                        // probing for capability
87      {250, 400, 20000},                // first beep at 20 seconds
88      {250, 400, 20510},                // second beep shortly after
89      {0, 0, 30020},                    // off and return at 30 seconds
90    }},
91
92  // Now with some keypresses
93
94  { "VbBootDeveloperSoundTest( normal, background, Ctrl-D )",
95    0x00000000, VBERROR_SUCCESS,
96    4, 10000,                           // Ctrl-D at 10 seconds
97    2,
98    {
99      {0, 0, 0},                        // probing for capability
100      {0, 0, 10000},                    // sees Ctrl-D, sound off, return
101    }},
102
103  { "VbBootDeveloperSoundTest( normal, no background, Ctrl-D )",
104    0x00000000, VBERROR_NO_BACKGROUND_SOUND,
105    4, 20400,                           // Ctrl-D between beeps
106    3,
107    {
108      {0, 0, 0},                        // probing for capability
109      {250, 400, 20000},                // first beep at 20 seconds
110      {0, 0, 20400},                    // sees Ctrl-D, sound off, return
111    }},
112
113  { "VbBootDeveloperSoundTest( normal, background, Ctrl-U not allowed )",
114    0x00000000, VBERROR_SUCCESS,
115    21, 10000,                          // Ctrl-U at 10 seconds
116    8,
117    {
118      {0, 0, 0},                        // probing for capability
119      {120, 400, 10000},                // complains about Ctrl-U (one beep)
120                                        // waits 120ms...
121      {120, 400, 10240},                // complains about Ctrl-U (two beeps)
122                                        // original sequence is now shifted...
123      {0, 400, 20360},                  // starts first beep at 20 seconds
124      {0, 0, 20610},                    // stops 250ms later
125      {0, 400, 20860},                  // starts second beep
126      {0, 0, 21110},                    // stops 250ms later
127      {0, 0, 30360},                    // returns at 30 seconds + 360ms
128    }},
129
130};
131
132/* Mock data */
133static VbCommonParams cparams;
134static LoadKernelParams lkparams;
135static VbNvContext vnc;
136static uint8_t shared_data[VB_SHARED_DATA_MIN_SIZE];
137static VbSharedDataHeader* shared = (VbSharedDataHeader*)shared_data;
138static GoogleBinaryBlockHeader gbb;
139static int current_time;
140static uint64_t current_ticks;
141static int current_event;
142static int max_events;
143static int matched_events;
144static int kbd_fire_at;
145static uint32_t kbd_fire_key;
146static VbError_t beep_return;
147static note_event_t *expected_event;
148
149/* Reset mock data (for use before each test) */
150static void ResetMocks(void) {
151
152  Memset(&cparams, 0, sizeof(cparams));
153  cparams.shared_data_size = sizeof(shared_data);
154  cparams.shared_data_blob = shared_data;
155  cparams.gbb_data = &gbb;
156
157  Memset(&lkparams, 0, sizeof(lkparams));
158
159  Memset(&vnc, 0, sizeof(vnc));
160  VbNvSetup(&vnc);
161  VbNvTeardown(&vnc);  /* So CRC gets generated */
162
163  Memset(&shared_data, 0, sizeof(shared_data));
164  VbSharedDataInit(shared, sizeof(shared_data));
165  shared->fw_keyblock_flags = 0xABCDE0;
166
167  Memset(&gbb, 0, sizeof(gbb));
168  gbb.major_version = GBB_MAJOR_VER;
169  gbb.minor_version = GBB_MINOR_VER;
170  gbb.flags = 0;
171
172  current_ticks = 0;
173  current_time = 0;
174
175  current_event = 0;
176  kbd_fire_at = 0;
177  kbd_fire_key = 0;
178
179  beep_return = VBERROR_SUCCESS;
180
181  matched_events = 0;
182  max_events = 0;
183}
184
185/****************************************************************************/
186/* Mocked verification functions */
187
188VbError_t VbExNvStorageRead(uint8_t* buf) {
189  Memcpy(buf, vnc.raw, sizeof(vnc.raw));
190  return VBERROR_SUCCESS;
191}
192
193VbError_t VbExNvStorageWrite(const uint8_t* buf) {
194  Memcpy(vnc.raw, buf, sizeof(vnc.raw));
195  return VBERROR_SUCCESS;
196}
197
198VbError_t VbExDiskGetInfo(VbDiskInfo** infos_ptr, uint32_t* count,
199                          uint32_t disk_flags) {
200  return VBERROR_UNKNOWN;
201}
202
203VbError_t VbExDiskFreeInfo(VbDiskInfo* infos,
204                           VbExDiskHandle_t preserve_handle) {
205  return VBERROR_SUCCESS;
206}
207
208VbError_t VbExDiskRead(VbExDiskHandle_t handle, uint64_t lba_start,
209                       uint64_t lba_count, void* buffer) {
210  return VBERROR_UNKNOWN;
211}
212
213VbError_t VbExDiskWrite(VbExDiskHandle_t handle, uint64_t lba_start,
214                        uint64_t lba_count, const void* buffer) {
215  return VBERROR_UNKNOWN;
216}
217
218uint32_t VbExIsShutdownRequested(void) {
219  return 0;
220}
221
222uint32_t VbExKeyboardRead(void) {
223  uint32_t tmp;
224  uint32_t now;
225
226  VbExSleepMs(KBD_READ_TIME);
227  now = current_time;
228
229  if (kbd_fire_key && now >= kbd_fire_at) {
230    VBDEBUG(("  VbExKeyboardRead() - returning %d at %d msec\n",
231             kbd_fire_key, now));
232    tmp = kbd_fire_key;
233    kbd_fire_key = 0;
234    return tmp;
235  }
236  VBDEBUG(("  VbExKeyboardRead() - returning %d at %d msec\n",
237           0, now));
238  return 0;
239}
240
241void VbExSleepMs(uint32_t msec) {
242  current_ticks += (uint64_t)msec * TICKS_PER_MSEC;
243  current_time = current_ticks / TICKS_PER_MSEC;
244  VBDEBUG(("VbExSleepMs(%d) -> %d\n", msec, current_time));
245}
246
247uint64_t VbExGetTimer(void) {
248  return current_ticks;
249}
250
251VbError_t VbExBeep(uint32_t msec, uint32_t frequency) {
252  VBDEBUG(("VbExBeep(%d, %d) at %d msec\n", msec, frequency, current_time));
253
254  if (current_event < max_events &&
255      msec == expected_event[current_event].msec &&
256      frequency == expected_event[current_event].freq &&
257      abs(current_time - expected_event[current_event].time) < TIME_FUZZ ) {
258    matched_events++;
259  }
260
261  if (msec)
262    VbExSleepMs(msec);
263  current_event++;
264  return beep_return;
265}
266
267VbError_t VbExDisplayScreen(uint32_t screen_type) {
268  switch(screen_type) {
269  case VB_SCREEN_BLANK:
270    VBDEBUG(("VbExDisplayScreen(BLANK)\n"));
271    break;
272  case VB_SCREEN_DEVELOPER_WARNING:
273    VBDEBUG(("VbExDisplayScreen(DEV)\n"));
274    break;
275  case VB_SCREEN_DEVELOPER_EGG:
276    VBDEBUG(("VbExDisplayScreen(EGG)\n"));
277    break;
278  case VB_SCREEN_RECOVERY_REMOVE:
279    VBDEBUG(("VbExDisplayScreen(REMOVE)\n"));
280    break;
281  case VB_SCREEN_RECOVERY_INSERT:
282    VBDEBUG(("VbExDisplayScreen(INSERT)\n"));
283    break;
284  case VB_SCREEN_RECOVERY_NO_GOOD:
285    VBDEBUG(("VbExDisplayScreen(NO_GOOD)\n"));
286    break;
287  default:
288    VBDEBUG(("VbExDisplayScreen(%d)\n", screen_type));
289  }
290
291  VBDEBUG(("  current_time is %d msec\n", current_time));
292
293  return VBERROR_SUCCESS;
294}
295
296/****************************************************************************/
297
298VbError_t VbBootDeveloper(VbCommonParams* cparams, LoadKernelParams* p);
299
300
301static void VbBootDeveloperSoundTest(void) {
302  int i;
303  int num_tests =  sizeof(test) / sizeof(test_case_t);
304
305  for (i=0; i<num_tests; i++) {
306    VBDEBUG(("STARTING %s ...\n", test[i].name));
307    ResetMocks();
308    gbb.flags = test[i].gbb_flags;
309    beep_return = test[i].beep_return;
310    kbd_fire_key = test[i].keypress_key;
311    kbd_fire_at = test[i].keypress_at_count;
312    max_events = test[i].num_events;
313    expected_event = test[i].notes;
314    (void) VbBootDeveloper(&cparams, &lkparams);
315    VBDEBUG(("INFO: matched %d total %d expected %d\n",
316             matched_events, current_event, test[i].num_events));
317    TEST_TRUE(matched_events == test[i].num_events &&
318              current_event == test[i].num_events, test[i].name);
319  }
320}
321
322
323int main(int argc, char* argv[]) {
324  int error_code = 0;
325
326  VbBootDeveloperSoundTest();
327
328  if (!gTestSuccess)
329    error_code = 255;
330
331  return error_code;
332}
333