blob: 63f689032a949c6dcece99f7460ade4d1c434182 [file] [log] [blame]
Randall Spangler786a5dc2013-01-24 14:19:56 -08001/* 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 firmware display library.
6 */
7
Bill Richardson0c3ba242013-03-29 11:09:30 -07008#include <stdint.h>
Randall Spangler786a5dc2013-01-24 14:19:56 -08009#include <stdio.h>
10#include <stdlib.h>
11#include <string.h>
12
Randall Spanglerfe510c02013-02-06 17:10:55 -080013#include "bmpblk_font.h"
Randall Spangler786a5dc2013-01-24 14:19:56 -080014#include "gbb_header.h"
15#include "host_common.h"
Simon Glass527ba812013-07-25 08:48:47 -060016#include "region.h"
Randall Spangler786a5dc2013-01-24 14:19:56 -080017#include "test_common.h"
18#include "vboot_common.h"
19#include "vboot_display.h"
Simon Glass527ba812013-07-25 08:48:47 -060020#include "vboot_kernel.h"
Randall Spangler786a5dc2013-01-24 14:19:56 -080021#include "vboot_nvstorage.h"
22
23/* Mock data */
24static VbCommonParams cparams;
25static VbNvContext vnc;
26static uint8_t shared_data[VB_SHARED_DATA_MIN_SIZE];
27static VbSharedDataHeader *shared = (VbSharedDataHeader *)shared_data;
Randall Spangler5a4e9fa2013-01-28 13:43:04 -080028static char gbb_data[4096 + sizeof(GoogleBinaryBlockHeader)];
29static GoogleBinaryBlockHeader *gbb = (GoogleBinaryBlockHeader *)gbb_data;
30static BmpBlockHeader *bhdr;
Randall Spangler786a5dc2013-01-24 14:19:56 -080031static char debug_info[4096];
32
33/* Reset mock data (for use before each test) */
34static void ResetMocks(void)
35{
Randall Spangler5a4e9fa2013-01-28 13:43:04 -080036 int gbb_used;
37
38 Memset(gbb_data, 0, sizeof(gbb_data));
39 gbb->major_version = GBB_MAJOR_VER;
40 gbb->minor_version = GBB_MINOR_VER;
41 gbb->flags = 0;
42 gbb_used = sizeof(GoogleBinaryBlockHeader);
43
44 gbb->hwid_offset = gbb_used;
45 strcpy(gbb_data + gbb->hwid_offset, "Test HWID");
46 gbb->hwid_size = strlen(gbb_data + gbb->hwid_offset) + 1;
47 gbb_used = (gbb_used + gbb->hwid_size + 7) & ~7;
48
49 gbb->bmpfv_offset = gbb_used;
50 bhdr = (BmpBlockHeader *)(gbb_data + gbb->bmpfv_offset);
51 gbb->bmpfv_size = sizeof(BmpBlockHeader);
52 gbb_used = (gbb_used + gbb->bmpfv_size + 7) & ~7;
53 memcpy(bhdr->signature, BMPBLOCK_SIGNATURE, BMPBLOCK_SIGNATURE_SIZE);
54 bhdr->major_version = BMPBLOCK_MAJOR_VERSION;
55 bhdr->minor_version = BMPBLOCK_MINOR_VERSION;
56 bhdr->number_of_localizations = 3;
57
Randall Spangler786a5dc2013-01-24 14:19:56 -080058 Memset(&cparams, 0, sizeof(cparams));
59 cparams.shared_data_size = sizeof(shared_data);
60 cparams.shared_data_blob = shared_data;
Randall Spangler5a4e9fa2013-01-28 13:43:04 -080061 cparams.gbb_data = gbb;
62 cparams.gbb_size = sizeof(gbb_data);
Randall Spangler786a5dc2013-01-24 14:19:56 -080063
Simon Glass527ba812013-07-25 08:48:47 -060064 /*
65 * Note, VbApiKernelFree() expects this to be allocated by
66 * VbExMalloc(), so we cannot just assign it staticly.
67 */
68 cparams.gbb = VbExMalloc(sizeof(*gbb));
69 gbb->header_size = sizeof(*gbb);
70 gbb->rootkey_offset = gbb_used;
71 gbb->rootkey_size = 64;
72 gbb_used += 64;
73 gbb->recovery_key_offset = gbb_used;
74 gbb->recovery_key_size = 64;
75 gbb_used += 64;
76 memcpy(cparams.gbb, gbb, sizeof(*gbb));
77
Randall Spangler786a5dc2013-01-24 14:19:56 -080078 Memset(&vnc, 0, sizeof(vnc));
79 VbNvSetup(&vnc);
80 VbNvTeardown(&vnc); /* So CRC gets generated */
81
82 Memset(&shared_data, 0, sizeof(shared_data));
83 VbSharedDataInit(shared, sizeof(shared_data));
84
85 *debug_info = 0;
86}
87
88/* Mocks */
89
90VbError_t VbExDisplayDebugInfo(const char *info_str)
91{
92 strncpy(debug_info, info_str, sizeof(debug_info));
93 debug_info[sizeof(debug_info) - 1] = '\0';
94 return VBERROR_SUCCESS;
95}
96
97/* Test displaying debug info */
98static void DebugInfoTest(void)
99{
Simon Glass527ba812013-07-25 08:48:47 -0600100 char hwid[VB_REGION_HWID_LEN];
Randall Spangler786a5dc2013-01-24 14:19:56 -0800101 int i;
102
103 /* Recovery string should be non-null for any code */
104 for (i = 0; i < 0x100; i++)
105 TEST_PTR_NEQ(RecoveryReasonString(i), NULL, "Non-null reason");
106
Randall Spangler5a4e9fa2013-01-28 13:43:04 -0800107 /* HWID should come from the gbb */
108 ResetMocks();
Simon Glass527ba812013-07-25 08:48:47 -0600109 VbRegionReadHWID(&cparams, hwid, sizeof(hwid));
110 TEST_EQ(strcmp(hwid, "Test HWID"), 0, "HWID");
111 VbApiKernelFree(&cparams);
Randall Spangler5a4e9fa2013-01-28 13:43:04 -0800112
113 ResetMocks();
114 cparams.gbb_size = 0;
Simon Glass527ba812013-07-25 08:48:47 -0600115 VbRegionReadHWID(&cparams, hwid, sizeof(hwid));
116 TEST_EQ(strcmp(hwid, "{INVALID}"), 0, "HWID bad gbb");
117 VbApiKernelFree(&cparams);
Randall Spangler5a4e9fa2013-01-28 13:43:04 -0800118
119 ResetMocks();
Simon Glass527ba812013-07-25 08:48:47 -0600120 cparams.gbb->hwid_size = 0;
121 VbRegionReadHWID(&cparams, hwid, sizeof(hwid));
122 TEST_EQ(strcmp(hwid, "{INVALID}"), 0, "HWID missing");
123 VbApiKernelFree(&cparams);
Randall Spangler5a4e9fa2013-01-28 13:43:04 -0800124
125 ResetMocks();
Simon Glass527ba812013-07-25 08:48:47 -0600126 cparams.gbb->hwid_offset = cparams.gbb_size + 1;
127 VbRegionReadHWID(&cparams, hwid, sizeof(hwid));
128 TEST_EQ(strcmp(hwid, "{INVALID}"), 0, "HWID past end");
129 VbApiKernelFree(&cparams);
Randall Spangler5a4e9fa2013-01-28 13:43:04 -0800130
131 ResetMocks();
Simon Glass527ba812013-07-25 08:48:47 -0600132 cparams.gbb->hwid_size = cparams.gbb_size;
133 VbRegionReadHWID(&cparams, hwid, sizeof(hwid));
134 TEST_EQ(strcmp(hwid, "{INVALID}"), 0, "HWID overflow");
135 VbApiKernelFree(&cparams);
Randall Spangler5a4e9fa2013-01-28 13:43:04 -0800136
Randall Spangler786a5dc2013-01-24 14:19:56 -0800137 /* Display debug info */
Randall Spangler5a4e9fa2013-01-28 13:43:04 -0800138 ResetMocks();
Randall Spangler786a5dc2013-01-24 14:19:56 -0800139 VbDisplayDebugInfo(&cparams, &vnc);
140 TEST_NEQ(*debug_info, '\0', "Some debug info was displayed");
Simon Glass527ba812013-07-25 08:48:47 -0600141 VbApiKernelFree(&cparams);
Randall Spangler786a5dc2013-01-24 14:19:56 -0800142}
143
Randall Spangler5a4e9fa2013-01-28 13:43:04 -0800144/* Test localization */
145static void LocalizationTest(void)
146{
147 uint32_t count = 6;
148
149 ResetMocks();
Simon Glass527ba812013-07-25 08:48:47 -0600150 cparams.gbb->bmpfv_size = 0;
Randall Spangler5a4e9fa2013-01-28 13:43:04 -0800151 TEST_EQ(VbGetLocalizationCount(&cparams, &count),
152 VBERROR_INVALID_GBB, "VbGetLocalizationCount bad gbb");
153 TEST_EQ(count, 0, " count");
Simon Glass527ba812013-07-25 08:48:47 -0600154 VbApiKernelFree(&cparams);
Randall Spangler5a4e9fa2013-01-28 13:43:04 -0800155
156 ResetMocks();
157 bhdr->signature[0] ^= 0x5a;
158 TEST_EQ(VbGetLocalizationCount(&cparams, &count),
159 VBERROR_INVALID_BMPFV, "VbGetLocalizationCount bad bmpfv");
Simon Glass527ba812013-07-25 08:48:47 -0600160 VbApiKernelFree(&cparams);
Randall Spangler5a4e9fa2013-01-28 13:43:04 -0800161
162 ResetMocks();
163 TEST_EQ(VbGetLocalizationCount(&cparams, &count), 0,
164 "VbGetLocalizationCount()");
165 TEST_EQ(count, 3, " count");
Simon Glass527ba812013-07-25 08:48:47 -0600166 VbApiKernelFree(&cparams);
Randall Spangler5a4e9fa2013-01-28 13:43:04 -0800167}
168
169/* Test display key checking */
170static void DisplayKeyTest(void)
171{
172 uint32_t u;
173
174 ResetMocks();
175 VbCheckDisplayKey(&cparams, 'q', &vnc);
176 TEST_EQ(*debug_info, '\0', "DisplayKey q = does nothing");
Simon Glass527ba812013-07-25 08:48:47 -0600177 VbApiKernelFree(&cparams);
Randall Spangler5a4e9fa2013-01-28 13:43:04 -0800178
179 ResetMocks();
180 VbCheckDisplayKey(&cparams, '\t', &vnc);
181 TEST_NEQ(*debug_info, '\0', "DisplayKey tab = display");
Simon Glass527ba812013-07-25 08:48:47 -0600182 VbApiKernelFree(&cparams);
Randall Spangler5a4e9fa2013-01-28 13:43:04 -0800183
184 /* Toggle localization */
Randall Spanglerfe510c02013-02-06 17:10:55 -0800185 ResetMocks();
Randall Spangler5a4e9fa2013-01-28 13:43:04 -0800186 VbNvSet(&vnc, VBNV_LOCALIZATION_INDEX, 0);
187 VbNvTeardown(&vnc);
188 VbCheckDisplayKey(&cparams, VB_KEY_DOWN, &vnc);
189 VbNvGet(&vnc, VBNV_LOCALIZATION_INDEX, &u);
190 TEST_EQ(u, 2, "DisplayKey up");
191 VbCheckDisplayKey(&cparams, VB_KEY_LEFT, &vnc);
192 VbNvGet(&vnc, VBNV_LOCALIZATION_INDEX, &u);
193 TEST_EQ(u, 1, "DisplayKey left");
194 VbCheckDisplayKey(&cparams, VB_KEY_RIGHT, &vnc);
195 VbNvGet(&vnc, VBNV_LOCALIZATION_INDEX, &u);
196 TEST_EQ(u, 2, "DisplayKey right");
197 VbCheckDisplayKey(&cparams, VB_KEY_UP, &vnc);
198 VbNvGet(&vnc, VBNV_LOCALIZATION_INDEX, &u);
199 TEST_EQ(u, 0, "DisplayKey up");
Simon Glass527ba812013-07-25 08:48:47 -0600200 VbApiKernelFree(&cparams);
Randall Spangler5a4e9fa2013-01-28 13:43:04 -0800201
202 /* Reset localization if localization count is invalid */
Randall Spanglerfe510c02013-02-06 17:10:55 -0800203 ResetMocks();
Randall Spangler5a4e9fa2013-01-28 13:43:04 -0800204 VbNvSet(&vnc, VBNV_LOCALIZATION_INDEX, 1);
205 VbNvTeardown(&vnc);
206 bhdr->signature[0] ^= 0x5a;
207 VbCheckDisplayKey(&cparams, VB_KEY_UP, &vnc);
208 VbNvGet(&vnc, VBNV_LOCALIZATION_INDEX, &u);
209 TEST_EQ(u, 0, "DisplayKey invalid");
Simon Glass527ba812013-07-25 08:48:47 -0600210 VbApiKernelFree(&cparams);
Randall Spangler5a4e9fa2013-01-28 13:43:04 -0800211}
212
Randall Spanglerfe510c02013-02-06 17:10:55 -0800213static void FontTest(void)
214{
215 FontArrayHeader h;
216 FontArrayEntryHeader eh[3] = {
217 {
218 .ascii = 'A',
219 .info.original_size = 10,
220 },
221 {
222 .ascii = 'B',
223 .info.original_size = 20,
224 },
225 {
226 .ascii = 'C',
227 .info.original_size = 30,
228 },
229 };
230 FontArrayEntryHeader *eptr;
231 uint8_t buf[sizeof(h) + sizeof(eh)];
232 VbFont_t *fptr;
233 void *bufferptr;
234 uint32_t buffersize;
Randall Spangler786a5dc2013-01-24 14:19:56 -0800235
Randall Spanglerfe510c02013-02-06 17:10:55 -0800236 /* Create font data */
237 h.num_entries = ARRAY_SIZE(eh);
238 Memcpy(buf, &h, sizeof(h));
239 eptr = (FontArrayEntryHeader *)(buf + sizeof(h));
240 Memcpy(eptr, eh, sizeof(eh));
241
242 fptr = VbInternalizeFontData((FontArrayHeader *)buf);
243 TEST_PTR_EQ(fptr, buf, "Internalize");
244
245 TEST_PTR_EQ(VbFindFontGlyph(fptr, 'B', &bufferptr, &buffersize),
246 &eptr[1].info, "Glyph found");
247 TEST_EQ(buffersize, eptr[1].info.original_size, " size");
248 TEST_PTR_EQ(VbFindFontGlyph(fptr, 'X', &bufferptr, &buffersize),
249 &eptr[0].info, "Glyph not found");
250 TEST_EQ(buffersize, eptr[0].info.original_size, " size");
251
252 /* Test invalid rendering params */
253 VbRenderTextAtPos(NULL, 0, 0, 0, fptr);
254 VbRenderTextAtPos("ABC", 0, 0, 0, NULL);
255
256 VbDoneWithFontForNow(fptr);
257
258}
259
260int main(void)
Randall Spangler786a5dc2013-01-24 14:19:56 -0800261{
Randall Spangler786a5dc2013-01-24 14:19:56 -0800262 DebugInfoTest();
Randall Spangler5a4e9fa2013-01-28 13:43:04 -0800263 LocalizationTest();
264 DisplayKeyTest();
Randall Spanglerfe510c02013-02-06 17:10:55 -0800265 FontTest();
Randall Spangler786a5dc2013-01-24 14:19:56 -0800266
Simon Glass25001852013-08-16 02:47:57 -0600267 if (vboot_api_stub_check_memory())
268 return 255;
269
Randall Spangler786a5dc2013-01-24 14:19:56 -0800270 return gTestSuccess ? 0 : 255;
271}