blob: 7077b1ed6e7f57d72815088f14670e3f538427c2 [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
Randall Spanglere4136dc2016-10-27 14:34:59 -070013#include "2sysincludes.h"
Joel Kitching8a4f0be2019-04-25 15:41:35 +080014#include "2common.h"
Randall Spangler51421322017-12-14 15:43:17 -080015#include "2misc.h"
Randall Spanglerdff58522017-11-27 15:37:13 -080016#include "2nvstorage.h"
Bill Richardson791c95f2011-09-30 15:23:15 -070017#include "crc32.h"
18#include "host_common.h"
Bill Richardson94d70342011-10-04 08:36:09 -070019#include "load_kernel_fw.h"
Bill Richardson791c95f2011-09-30 15:23:15 -070020#include "rollback_index.h"
21#include "test_common.h"
22#include "vboot_common.h"
Bill Richardson94d70342011-10-04 08:36:09 -070023#include "vboot_display.h"
Randall Spanglere4136dc2016-10-27 14:34:59 -070024#include "vboot_kernel.h"
Bill Richardson791c95f2011-09-30 15:23:15 -070025#include "vboot_struct.h"
Bill Richardson791c95f2011-09-30 15:23:15 -070026
27
28/* Expected results */
29
30#define MAX_NOTE_EVENTS 10
Bill Richardson037dba22012-01-19 13:47:33 -080031#define TIME_FUZZ 500
32#define KBD_READ_TIME 60
Bill Richardson791c95f2011-09-30 15:23:15 -070033
34typedef struct {
Randall Spanglerde818cc2017-12-12 14:05:19 -080035 uint16_t msec;
36 uint16_t freq;
37 int time;
Bill Richardson791c95f2011-09-30 15:23:15 -070038} note_event_t;
39
40typedef struct {
Julius Werner52fa8c12019-05-07 12:59:47 -070041 const char *name;
Randall Spanglerde818cc2017-12-12 14:05:19 -080042 uint32_t gbb_flags;
Joel Kitching90671fa2019-07-31 13:17:08 +080043 vb2_error_t beep_return;
Randall Spanglerde818cc2017-12-12 14:05:19 -080044 uint32_t keypress_key;
45 int keypress_at_count;
46 int num_events;
47 note_event_t notes[MAX_NOTE_EVENTS];
Bill Richardson791c95f2011-09-30 15:23:15 -070048} test_case_t;
49
50test_case_t test[] = {
51
Randall Spanglerde818cc2017-12-12 14:05:19 -080052 { "VbBootDeveloperSoundTest( fast )",
53 VB2_GBB_FLAG_DEV_SCREEN_SHORT_DELAY, VBERROR_NO_BACKGROUND_SOUND,
54 0, 0,
55 1,
56 {
Joel Kitching9ad8a412018-08-02 16:21:17 +080057 {0, 0, 2000}, // off and return at 2 seconds
Randall Spanglerde818cc2017-12-12 14:05:19 -080058 }},
Bill Richardson791c95f2011-09-30 15:23:15 -070059
Randall Spanglerde818cc2017-12-12 14:05:19 -080060 { "VbBootDeveloperSoundTest( normal )",
61 0, VBERROR_NO_BACKGROUND_SOUND,
62 0, 0,
63 3,
64 {
Joel Kitching9ad8a412018-08-02 16:21:17 +080065 {250, 400, 20000}, // first beep at 20 seconds
66 {250, 400, 20510}, // second beep shortly after
67 {0, 0, 30020}, // off and return at 30 seconds
Randall Spanglerde818cc2017-12-12 14:05:19 -080068 }},
Bill Richardson791c95f2011-09-30 15:23:15 -070069
Randall Spanglerde818cc2017-12-12 14:05:19 -080070 // Now with some keypresses
Bill Richardson791c95f2011-09-30 15:23:15 -070071
Randall Spanglerde818cc2017-12-12 14:05:19 -080072 { "VbBootDeveloperSoundTest( normal, Ctrl-D )",
73 0, VBERROR_NO_BACKGROUND_SOUND,
74 4, 20400, // Ctrl-D between beeps
75 2,
76 {
Joel Kitching9ad8a412018-08-02 16:21:17 +080077 {250, 400, 20000}, // first beep at 20 seconds
78 {0, 0, 20400}, // sees Ctrl-D, sound off, return
Randall Spanglerde818cc2017-12-12 14:05:19 -080079 }},
Bill Richardson791c95f2011-09-30 15:23:15 -070080
Randall Spanglerde818cc2017-12-12 14:05:19 -080081 { "VbBootDeveloperSoundTest( normal, Ctrl-U not allowed )",
82 0, VBERROR_NO_BACKGROUND_SOUND,
83 21, 10000, // Ctrl-U at 10 seconds
84 5,
85 {
Joel Kitching9ad8a412018-08-02 16:21:17 +080086 {120, 400, 10000}, // complains about Ctrl-U (one beep)
87 {120, 400, 10240}, // complains about Ctrl-U (two beeps)
88 {250, 400, 20000}, // starts first beep at 20 seconds
89 {250, 400, 20510}, // starts second beep
90 {0, 0, 30020}, // returns at 30 seconds + 360ms
Randall Spanglerde818cc2017-12-12 14:05:19 -080091 }},
Bill Richardson791c95f2011-09-30 15:23:15 -070092};
93
Bill Richardson791c95f2011-09-30 15:23:15 -070094/* Mock data */
Randall Spangler51421322017-12-14 15:43:17 -080095static uint8_t workbuf[VB2_KERNEL_WORKBUF_RECOMMENDED_SIZE];
Randall Spanglere4136dc2016-10-27 14:34:59 -070096static struct vb2_context ctx;
Randall Spanglerde818cc2017-12-12 14:05:19 -080097static struct vb2_shared_data *sd;
Joel Kitchingde2cae62019-05-21 12:20:38 +080098static struct vb2_gbb_header gbb;
Bill Richardson791c95f2011-09-30 15:23:15 -070099static uint8_t shared_data[VB_SHARED_DATA_MIN_SIZE];
100static VbSharedDataHeader* shared = (VbSharedDataHeader*)shared_data;
Bill Richardson791c95f2011-09-30 15:23:15 -0700101static int current_time;
Bill Richardson037dba22012-01-19 13:47:33 -0800102static uint64_t current_ticks;
Bill Richardson791c95f2011-09-30 15:23:15 -0700103static int current_event;
104static int max_events;
105static int matched_events;
106static int kbd_fire_at;
107static uint32_t kbd_fire_key;
Joel Kitching90671fa2019-07-31 13:17:08 +0800108static vb2_error_t beep_return;
Bill Richardson791c95f2011-09-30 15:23:15 -0700109static note_event_t *expected_event;
110
Randall Spanglerde818cc2017-12-12 14:05:19 -0800111/* Audio open count, so we can reset it */
Shelley Chen356eab12017-07-07 09:57:22 -0700112extern int audio_open_count;
113
Bill Richardson791c95f2011-09-30 15:23:15 -0700114/* Reset mock data (for use before each test) */
Randall Spanglerde818cc2017-12-12 14:05:19 -0800115static void ResetMocks(void)
116{
Randall Spanglerde818cc2017-12-12 14:05:19 -0800117 memset(&ctx, 0, sizeof(ctx));
118 ctx.workbuf = workbuf;
119 ctx.workbuf_size = sizeof(workbuf);
120 vb2_init_context(&ctx);
121 vb2_nv_init(&ctx);
Randall Spangler79c1c612018-01-03 13:42:40 -0800122
Randall Spanglerde818cc2017-12-12 14:05:19 -0800123 sd = vb2_get_sd(&ctx);
Randall Spangler79c1c612018-01-03 13:42:40 -0800124 sd->vbsd = shared;
Bill Richardson791c95f2011-09-30 15:23:15 -0700125
Joel Kitchingde2cae62019-05-21 12:20:38 +0800126 memset(&gbb, 0, sizeof(gbb));
127
Randall Spanglerde818cc2017-12-12 14:05:19 -0800128 memset(&shared_data, 0, sizeof(shared_data));
129 VbSharedDataInit(shared, sizeof(shared_data));
130 shared->fw_keyblock_flags = 0xABCDE0;
Bill Richardson791c95f2011-09-30 15:23:15 -0700131
Randall Spanglerde818cc2017-12-12 14:05:19 -0800132 current_ticks = 0;
133 current_time = 0;
Bill Richardson791c95f2011-09-30 15:23:15 -0700134
Randall Spanglerde818cc2017-12-12 14:05:19 -0800135 current_event = 0;
136 kbd_fire_at = 0;
137 kbd_fire_key = 0;
Bill Richardson037dba22012-01-19 13:47:33 -0800138
Joel Kitchingcf49e7b2019-07-29 18:51:00 +0800139 beep_return = VB2_SUCCESS;
Randall Spanglerde818cc2017-12-12 14:05:19 -0800140 audio_open_count = 0;
Bill Richardson791c95f2011-09-30 15:23:15 -0700141
Randall Spanglerde818cc2017-12-12 14:05:19 -0800142 matched_events = 0;
143 max_events = 0;
Bill Richardson791c95f2011-09-30 15:23:15 -0700144}
145
146/****************************************************************************/
147/* Mocked verification functions */
Joel Kitchingde2cae62019-05-21 12:20:38 +0800148struct vb2_gbb_header *vb2_get_gbb(struct vb2_context *c)
149{
150 return &gbb;
151}
Bill Richardson791c95f2011-09-30 15:23:15 -0700152
Joel Kitching90671fa2019-07-31 13:17:08 +0800153vb2_error_t VbExNvStorageRead(uint8_t* buf)
Randall Spanglerde818cc2017-12-12 14:05:19 -0800154{
Joel Kitchingcf49e7b2019-07-29 18:51:00 +0800155 return VB2_SUCCESS;
Bill Richardson791c95f2011-09-30 15:23:15 -0700156}
157
Joel Kitching90671fa2019-07-31 13:17:08 +0800158vb2_error_t VbExNvStorageWrite(const uint8_t* buf)
Randall Spanglerde818cc2017-12-12 14:05:19 -0800159{
Joel Kitchingcf49e7b2019-07-29 18:51:00 +0800160 return VB2_SUCCESS;
Bill Richardson791c95f2011-09-30 15:23:15 -0700161}
162
Joel Kitching90671fa2019-07-31 13:17:08 +0800163vb2_error_t VbExDiskGetInfo(VbDiskInfo** infos_ptr, uint32_t* count,
164 uint32_t disk_flags)
Randall Spanglerde818cc2017-12-12 14:05:19 -0800165{
Joel Kitching9908a9a2019-07-29 18:53:38 +0800166 return VB2_ERROR_UNKNOWN;
Bill Richardson791c95f2011-09-30 15:23:15 -0700167}
168
Joel Kitching90671fa2019-07-31 13:17:08 +0800169vb2_error_t VbExDiskFreeInfo(VbDiskInfo* infos,
170 VbExDiskHandle_t preserve_handle)
Randall Spanglerde818cc2017-12-12 14:05:19 -0800171{
Joel Kitchingcf49e7b2019-07-29 18:51:00 +0800172 return VB2_SUCCESS;
Bill Richardson791c95f2011-09-30 15:23:15 -0700173}
174
Joel Kitching90671fa2019-07-31 13:17:08 +0800175vb2_error_t VbExDiskRead(VbExDiskHandle_t handle, uint64_t lba_start,
176 uint64_t lba_count, void* buffer)
Randall Spanglerde818cc2017-12-12 14:05:19 -0800177{
Joel Kitching9908a9a2019-07-29 18:53:38 +0800178 return VB2_ERROR_UNKNOWN;
Bill Richardson791c95f2011-09-30 15:23:15 -0700179}
180
Joel Kitching90671fa2019-07-31 13:17:08 +0800181vb2_error_t VbExDiskWrite(VbExDiskHandle_t handle, uint64_t lba_start,
182 uint64_t lba_count, const void* buffer)
Randall Spanglerde818cc2017-12-12 14:05:19 -0800183{
Joel Kitching9908a9a2019-07-29 18:53:38 +0800184 return VB2_ERROR_UNKNOWN;
Bill Richardson791c95f2011-09-30 15:23:15 -0700185}
186
Randall Spanglerde818cc2017-12-12 14:05:19 -0800187uint32_t VbExIsShutdownRequested(void)
188{
189 return 0;
Bill Richardson791c95f2011-09-30 15:23:15 -0700190}
191
Randall Spanglerde818cc2017-12-12 14:05:19 -0800192uint32_t VbExKeyboardRead(void)
193{
194 uint32_t tmp;
195 uint32_t now;
Bill Richardson037dba22012-01-19 13:47:33 -0800196
Randall Spanglerde818cc2017-12-12 14:05:19 -0800197 VbExSleepMs(KBD_READ_TIME);
198 now = current_time;
Bill Richardson037dba22012-01-19 13:47:33 -0800199
Randall Spanglerde818cc2017-12-12 14:05:19 -0800200 if (kbd_fire_key && now >= kbd_fire_at) {
201 VB2_DEBUG(" VbExKeyboardRead() - returning %d at %d msec\n",
202 kbd_fire_key, now);
203 tmp = kbd_fire_key;
204 kbd_fire_key = 0;
205 return tmp;
206 }
207 VB2_DEBUG(" VbExKeyboardRead() - returning %d at %d msec\n",
208 0, now);
209 return 0;
Bill Richardson791c95f2011-09-30 15:23:15 -0700210}
211
Randall Spanglerde818cc2017-12-12 14:05:19 -0800212void VbExSleepMs(uint32_t msec)
213{
Matt Delco45e420b2019-03-05 16:38:28 -0800214 current_ticks += (uint64_t)msec * VB_USEC_PER_MSEC;
215 current_time = current_ticks / VB_USEC_PER_MSEC;
Randall Spanglerde818cc2017-12-12 14:05:19 -0800216 VB2_DEBUG("VbExSleepMs(%d) -> %d\n", msec, current_time);
Bill Richardson037dba22012-01-19 13:47:33 -0800217}
218
Randall Spanglerde818cc2017-12-12 14:05:19 -0800219uint64_t VbExGetTimer(void)
220{
221 return current_ticks;
Bill Richardson791c95f2011-09-30 15:23:15 -0700222}
223
Joel Kitching90671fa2019-07-31 13:17:08 +0800224vb2_error_t VbExBeep(uint32_t msec, uint32_t frequency)
Randall Spanglerde818cc2017-12-12 14:05:19 -0800225{
226 VB2_DEBUG("VbExBeep(%d, %d) at %d msec\n",
227 msec, frequency, current_time);
Bill Richardson791c95f2011-09-30 15:23:15 -0700228
Randall Spanglerde818cc2017-12-12 14:05:19 -0800229 if (current_event < max_events &&
230 msec == expected_event[current_event].msec &&
231 frequency == expected_event[current_event].freq &&
232 abs(current_time - expected_event[current_event].time)
233 < TIME_FUZZ ) {
234 matched_events++;
235 }
Bill Richardson791c95f2011-09-30 15:23:15 -0700236
Randall Spanglerde818cc2017-12-12 14:05:19 -0800237 if (msec)
238 VbExSleepMs(msec);
239 current_event++;
240 return beep_return;
Bill Richardson791c95f2011-09-30 15:23:15 -0700241}
242
Joel Kitching90671fa2019-07-31 13:17:08 +0800243vb2_error_t VbExDisplayScreen(uint32_t screen_type, uint32_t locale,
244 const VbScreenData *data)
Randall Spanglerde818cc2017-12-12 14:05:19 -0800245{
246 switch(screen_type) {
247 case VB_SCREEN_BLANK:
248 VB2_DEBUG("VbExDisplayScreen(BLANK)\n");
249 break;
250 case VB_SCREEN_DEVELOPER_WARNING:
251 VB2_DEBUG("VbExDisplayScreen(DEV)\n");
252 break;
Randall Spanglerde818cc2017-12-12 14:05:19 -0800253 case VB_SCREEN_RECOVERY_INSERT:
254 VB2_DEBUG("VbExDisplayScreen(INSERT)\n");
255 break;
256 case VB_SCREEN_RECOVERY_NO_GOOD:
257 VB2_DEBUG("VbExDisplayScreen(NO_GOOD)\n");
258 break;
259 case VB_SCREEN_OS_BROKEN:
260 VB2_DEBUG("VbExDisplayScreen(BROKEN)\n");
261 break;
262 default:
263 VB2_DEBUG("VbExDisplayScreen(%d)\n", screen_type);
264 }
Bill Richardson791c95f2011-09-30 15:23:15 -0700265
Randall Spanglerde818cc2017-12-12 14:05:19 -0800266 VB2_DEBUG(" current_time is %d msec\n", current_time);
Bill Richardson791c95f2011-09-30 15:23:15 -0700267
Joel Kitchingcf49e7b2019-07-29 18:51:00 +0800268 return VB2_SUCCESS;
Bill Richardson791c95f2011-09-30 15:23:15 -0700269}
270
Bill Richardson791c95f2011-09-30 15:23:15 -0700271/****************************************************************************/
272
Randall Spanglerde818cc2017-12-12 14:05:19 -0800273static void VbBootDeveloperSoundTest(void)
274{
275 int i;
276 int num_tests = sizeof(test) / sizeof(test_case_t);
Bill Richardson791c95f2011-09-30 15:23:15 -0700277
Randall Spanglerde818cc2017-12-12 14:05:19 -0800278 for (i=0; i<num_tests; i++) {
279 VB2_DEBUG("STARTING %s ...\n", test[i].name);
280 ResetMocks();
Joel Kitchingde2cae62019-05-21 12:20:38 +0800281 gbb.flags = test[i].gbb_flags;
Randall Spanglerde818cc2017-12-12 14:05:19 -0800282 beep_return = test[i].beep_return;
283 kbd_fire_key = test[i].keypress_key;
284 kbd_fire_at = test[i].keypress_at_count;
285 max_events = test[i].num_events;
286 expected_event = test[i].notes;
Randall Spanglerb07b4b92018-01-04 17:58:01 -0800287 (void) VbBootDeveloper(&ctx);
Randall Spanglerde818cc2017-12-12 14:05:19 -0800288 VbExBeep(0, 0); /* Dummy call to determine end time */
289 VB2_DEBUG("INFO: matched %d total %d expected %d\n",
290 matched_events, current_event, test[i].num_events);
291 TEST_TRUE(matched_events == test[i].num_events &&
292 current_event == test[i].num_events, test[i].name);
293 }
Bill Richardson791c95f2011-09-30 15:23:15 -0700294}
295
Randall Spangler559a1102016-10-18 14:41:22 -0700296int main(int argc, char* argv[])
297{
Randall Spanglerde818cc2017-12-12 14:05:19 -0800298 VbBootDeveloperSoundTest();
299 return gTestSuccess ? 0 : 255;
Bill Richardson791c95f2011-09-30 15:23:15 -0700300}