vboot_api_devmode_tests.c 9.2 KB

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