blob: 925a146bf11546a31563baaf44282c821ef34d37 [file] [log] [blame]
Bill Richardson791c95f2011-09-30 15:23:15 -07001/* 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
Bill Richardson791c95f2011-09-30 15:23:15 -07008#include <stddef.h>
Bill Richardson0c3ba242013-03-29 11:09:30 -07009#include <stdint.h>
Bill Richardson94d70342011-10-04 08:36:09 -070010#include <stdio.h>
Bill Richardson791c95f2011-09-30 15:23:15 -070011#include <stdlib.h>
12
13#include "crc32.h"
Bill Richardson94d70342011-10-04 08:36:09 -070014#include "gbb_header.h"
Bill Richardson791c95f2011-09-30 15:23:15 -070015#include "host_common.h"
Bill Richardson94d70342011-10-04 08:36:09 -070016#include "load_kernel_fw.h"
Bill Richardson791c95f2011-09-30 15:23:15 -070017#include "rollback_index.h"
18#include "test_common.h"
19#include "vboot_common.h"
Bill Richardson94d70342011-10-04 08:36:09 -070020#include "vboot_display.h"
Bill Richardson791c95f2011-09-30 15:23:15 -070021#include "vboot_nvstorage.h"
22#include "vboot_struct.h"
Bill Richardson791c95f2011-09-30 15:23:15 -070023
24
25/* Expected results */
26
27#define MAX_NOTE_EVENTS 10
Bill Richardson037dba22012-01-19 13:47:33 -080028#define TICKS_PER_MSEC 1900ULL
29#define TIME_FUZZ 500
30#define KBD_READ_TIME 60
Bill Richardson791c95f2011-09-30 15:23:15 -070031
32typedef struct {
33 uint16_t msec;
34 uint16_t freq;
Bill Richardson037dba22012-01-19 13:47:33 -080035 int time;
Bill Richardson791c95f2011-09-30 15:23:15 -070036} 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,
Bill Richardson037dba22012-01-19 13:47:33 -080053 2,
Bill Richardson791c95f2011-09-30 15:23:15 -070054 {
55 {0, 0, 0}, // probing for capability
Bill Richardson791c95f2011-09-30 15:23:15 -070056 {0, 0, 2000}, // off and return at 2 seconds
57 }},
58
59 { "VbBootDeveloperSoundTest( normal, background )",
60 0x00000000, VBERROR_SUCCESS,
61 0, 0,
Bill Richardson037dba22012-01-19 13:47:33 -080062 6,
Bill Richardson791c95f2011-09-30 15:23:15 -070063 {
64 {0, 0, 0}, // probing for capability
Bill Richardson791c95f2011-09-30 15:23:15 -070065 {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
Bill Richardson791c95f2011-09-30 15:23:15 -070092 // Now with some keypresses
93
94 { "VbBootDeveloperSoundTest( normal, background, Ctrl-D )",
95 0x00000000, VBERROR_SUCCESS,
96 4, 10000, // Ctrl-D at 10 seconds
Bill Richardson037dba22012-01-19 13:47:33 -080097 2,
Bill Richardson791c95f2011-09-30 15:23:15 -070098 {
99 {0, 0, 0}, // probing for capability
Bill Richardson791c95f2011-09-30 15:23:15 -0700100 {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
Bill Richardson037dba22012-01-19 13:47:33 -0800116 8,
Bill Richardson791c95f2011-09-30 15:23:15 -0700117 {
118 {0, 0, 0}, // probing for capability
Bill Richardson791c95f2011-09-30 15:23:15 -0700119 {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
Bill Richardson791c95f2011-09-30 15:23:15 -0700130};
131
Bill Richardson791c95f2011-09-30 15:23:15 -0700132/* 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;
Bill Richardson037dba22012-01-19 13:47:33 -0800140static uint64_t current_ticks;
Bill Richardson791c95f2011-09-30 15:23:15 -0700141static 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
Bill Richardson791c95f2011-09-30 15:23:15 -0700149/* 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;
Simon Glass527ba812013-07-25 08:48:47 -0600156 cparams.gbb = &gbb;
Bill Richardson791c95f2011-09-30 15:23:15 -0700157
158 Memset(&lkparams, 0, sizeof(lkparams));
159
160 Memset(&vnc, 0, sizeof(vnc));
161 VbNvSetup(&vnc);
162 VbNvTeardown(&vnc); /* So CRC gets generated */
163
164 Memset(&shared_data, 0, sizeof(shared_data));
165 VbSharedDataInit(shared, sizeof(shared_data));
166 shared->fw_keyblock_flags = 0xABCDE0;
167
168 Memset(&gbb, 0, sizeof(gbb));
169 gbb.major_version = GBB_MAJOR_VER;
170 gbb.minor_version = GBB_MINOR_VER;
171 gbb.flags = 0;
172
Bill Richardson037dba22012-01-19 13:47:33 -0800173 current_ticks = 0;
Bill Richardson791c95f2011-09-30 15:23:15 -0700174 current_time = 0;
Bill Richardson037dba22012-01-19 13:47:33 -0800175
Bill Richardson791c95f2011-09-30 15:23:15 -0700176 current_event = 0;
177 kbd_fire_at = 0;
178 kbd_fire_key = 0;
179
180 beep_return = VBERROR_SUCCESS;
181
182 matched_events = 0;
183 max_events = 0;
184}
185
186/****************************************************************************/
187/* Mocked verification functions */
188
189VbError_t VbExNvStorageRead(uint8_t* buf) {
190 Memcpy(buf, vnc.raw, sizeof(vnc.raw));
191 return VBERROR_SUCCESS;
192}
193
194VbError_t VbExNvStorageWrite(const uint8_t* buf) {
195 Memcpy(vnc.raw, buf, sizeof(vnc.raw));
196 return VBERROR_SUCCESS;
197}
198
199VbError_t VbExDiskGetInfo(VbDiskInfo** infos_ptr, uint32_t* count,
200 uint32_t disk_flags) {
201 return VBERROR_UNKNOWN;
202}
203
204VbError_t VbExDiskFreeInfo(VbDiskInfo* infos,
205 VbExDiskHandle_t preserve_handle) {
206 return VBERROR_SUCCESS;
207}
208
209VbError_t VbExDiskRead(VbExDiskHandle_t handle, uint64_t lba_start,
210 uint64_t lba_count, void* buffer) {
211 return VBERROR_UNKNOWN;
212}
213
214VbError_t VbExDiskWrite(VbExDiskHandle_t handle, uint64_t lba_start,
215 uint64_t lba_count, const void* buffer) {
216 return VBERROR_UNKNOWN;
217}
218
219uint32_t VbExIsShutdownRequested(void) {
220 return 0;
221}
222
223uint32_t VbExKeyboardRead(void) {
224 uint32_t tmp;
Bill Richardson037dba22012-01-19 13:47:33 -0800225 uint32_t now;
226
227 VbExSleepMs(KBD_READ_TIME);
228 now = current_time;
229
230 if (kbd_fire_key && now >= kbd_fire_at) {
Bill Richardson791c95f2011-09-30 15:23:15 -0700231 VBDEBUG((" VbExKeyboardRead() - returning %d at %d msec\n",
Bill Richardson037dba22012-01-19 13:47:33 -0800232 kbd_fire_key, now));
Bill Richardson791c95f2011-09-30 15:23:15 -0700233 tmp = kbd_fire_key;
234 kbd_fire_key = 0;
235 return tmp;
236 }
Bill Richardson037dba22012-01-19 13:47:33 -0800237 VBDEBUG((" VbExKeyboardRead() - returning %d at %d msec\n",
238 0, now));
Bill Richardson791c95f2011-09-30 15:23:15 -0700239 return 0;
240}
241
242void VbExSleepMs(uint32_t msec) {
Bill Richardson037dba22012-01-19 13:47:33 -0800243 current_ticks += (uint64_t)msec * TICKS_PER_MSEC;
244 current_time = current_ticks / TICKS_PER_MSEC;
245 VBDEBUG(("VbExSleepMs(%d) -> %d\n", msec, current_time));
246}
247
248uint64_t VbExGetTimer(void) {
249 return current_ticks;
Bill Richardson791c95f2011-09-30 15:23:15 -0700250}
251
252VbError_t VbExBeep(uint32_t msec, uint32_t frequency) {
253 VBDEBUG(("VbExBeep(%d, %d) at %d msec\n", msec, frequency, current_time));
254
255 if (current_event < max_events &&
256 msec == expected_event[current_event].msec &&
257 frequency == expected_event[current_event].freq &&
Bill Richardson037dba22012-01-19 13:47:33 -0800258 abs(current_time - expected_event[current_event].time) < TIME_FUZZ ) {
Bill Richardson791c95f2011-09-30 15:23:15 -0700259 matched_events++;
260 }
261
Bill Richardson037dba22012-01-19 13:47:33 -0800262 if (msec)
263 VbExSleepMs(msec);
Bill Richardson791c95f2011-09-30 15:23:15 -0700264 current_event++;
265 return beep_return;
266}
267
268VbError_t VbExDisplayScreen(uint32_t screen_type) {
269 switch(screen_type) {
270 case VB_SCREEN_BLANK:
271 VBDEBUG(("VbExDisplayScreen(BLANK)\n"));
272 break;
273 case VB_SCREEN_DEVELOPER_WARNING:
274 VBDEBUG(("VbExDisplayScreen(DEV)\n"));
275 break;
276 case VB_SCREEN_DEVELOPER_EGG:
277 VBDEBUG(("VbExDisplayScreen(EGG)\n"));
278 break;
279 case VB_SCREEN_RECOVERY_REMOVE:
280 VBDEBUG(("VbExDisplayScreen(REMOVE)\n"));
281 break;
282 case VB_SCREEN_RECOVERY_INSERT:
283 VBDEBUG(("VbExDisplayScreen(INSERT)\n"));
284 break;
285 case VB_SCREEN_RECOVERY_NO_GOOD:
286 VBDEBUG(("VbExDisplayScreen(NO_GOOD)\n"));
287 break;
288 default:
289 VBDEBUG(("VbExDisplayScreen(%d)\n", screen_type));
290 }
291
292 VBDEBUG((" current_time is %d msec\n", current_time));
293
294 return VBERROR_SUCCESS;
295}
296
Bill Richardson791c95f2011-09-30 15:23:15 -0700297/****************************************************************************/
298
299VbError_t VbBootDeveloper(VbCommonParams* cparams, LoadKernelParams* p);
300
301
302static void VbBootDeveloperSoundTest(void) {
303 int i;
304 int num_tests = sizeof(test) / sizeof(test_case_t);
305
306 for (i=0; i<num_tests; i++) {
307 VBDEBUG(("STARTING %s ...\n", test[i].name));
308 ResetMocks();
309 gbb.flags = test[i].gbb_flags;
310 beep_return = test[i].beep_return;
311 kbd_fire_key = test[i].keypress_key;
312 kbd_fire_at = test[i].keypress_at_count;
313 max_events = test[i].num_events;
314 expected_event = test[i].notes;
315 (void) VbBootDeveloper(&cparams, &lkparams);
316 VBDEBUG(("INFO: matched %d total %d expected %d\n",
317 matched_events, current_event, test[i].num_events));
Bill Richardson94d70342011-10-04 08:36:09 -0700318 TEST_TRUE(matched_events == test[i].num_events &&
319 current_event == test[i].num_events, test[i].name);
Bill Richardson791c95f2011-09-30 15:23:15 -0700320 }
321}
322
323
Bill Richardson791c95f2011-09-30 15:23:15 -0700324int main(int argc, char* argv[]) {
325 int error_code = 0;
326
327 VbBootDeveloperSoundTest();
328
329 if (!gTestSuccess)
330 error_code = 255;
Simon Glass25001852013-08-16 02:47:57 -0600331 if (vboot_api_stub_check_memory())
332 error_code = 255;
Bill Richardson791c95f2011-09-30 15:23:15 -0700333
334 return error_code;
335}