video_checksum.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2013 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /* All tests need to include test.h for GRUB testing framework. */
  19. #include <config.h>
  20. #include <grub/test.h>
  21. #include <grub/dl.h>
  22. #include <grub/video.h>
  23. #include <grub/lib/crc.h>
  24. #include <grub/mm.h>
  25. #include <grub/term.h>
  26. #ifdef GRUB_MACHINE_EMU
  27. #include <grub/emu/hostdisk.h>
  28. #include <grub/emu/misc.h>
  29. #endif
  30. GRUB_MOD_LICENSE ("GPLv3+");
  31. static int ctr;
  32. static int nchk;
  33. static char *basename;
  34. static const grub_uint32_t *checksums;
  35. static struct grub_video_mode_info capt_mode_info;
  36. struct grub_video_mode_info grub_test_video_modes[GRUB_TEST_VIDEO_ALL_N_MODES] = {
  37. {
  38. .width = 640,
  39. .height = 480,
  40. .pitch = 640,
  41. .mode_type = GRUB_VIDEO_MODE_TYPE_INDEX_COLOR,
  42. .bpp = 8,
  43. .bytes_per_pixel = 1,
  44. .number_of_colors = GRUB_VIDEO_FBSTD_NUMCOLORS
  45. },
  46. {
  47. .width = 800,
  48. .height = 600,
  49. .pitch = 800,
  50. .mode_type = GRUB_VIDEO_MODE_TYPE_INDEX_COLOR,
  51. .bpp = 8,
  52. .bytes_per_pixel = 1,
  53. .number_of_colors = GRUB_VIDEO_FBSTD_NUMCOLORS
  54. },
  55. {
  56. .width = 1024,
  57. .height = 768,
  58. .pitch = 1024,
  59. .mode_type = GRUB_VIDEO_MODE_TYPE_INDEX_COLOR,
  60. .bpp = 8,
  61. .bytes_per_pixel = 1,
  62. .number_of_colors = GRUB_VIDEO_FBSTD_NUMCOLORS
  63. },
  64. {
  65. .width = 640,
  66. .height = 480,
  67. .pitch = 640 * 4,
  68. GRUB_VIDEO_MI_RGBA8888()
  69. },
  70. {
  71. .width = 800,
  72. .height = 600,
  73. .pitch = 800 * 4,
  74. GRUB_VIDEO_MI_RGBA8888()
  75. },
  76. {
  77. .width = 1024,
  78. .height = 768,
  79. .pitch = 1024 * 4,
  80. GRUB_VIDEO_MI_RGBA8888()
  81. },
  82. {
  83. .width = 2560,
  84. .height = 1440,
  85. .pitch = 2560 * 4,
  86. GRUB_VIDEO_MI_RGBA8888()
  87. },
  88. {
  89. .width = 640,
  90. .height = 480,
  91. .pitch = 640,
  92. .mode_type = GRUB_VIDEO_MODE_TYPE_INDEX_COLOR,
  93. .bpp = 8,
  94. .bytes_per_pixel = 1,
  95. .number_of_colors = GRUB_VIDEO_FBSTD_EXT_NUMCOLORS
  96. },
  97. {
  98. .width = 800,
  99. .height = 600,
  100. .pitch = 800,
  101. .mode_type = GRUB_VIDEO_MODE_TYPE_INDEX_COLOR,
  102. .bpp = 8,
  103. .bytes_per_pixel = 1,
  104. .number_of_colors = GRUB_VIDEO_FBSTD_EXT_NUMCOLORS
  105. },
  106. {
  107. .width = 1024,
  108. .height = 768,
  109. .pitch = 1024,
  110. .mode_type = GRUB_VIDEO_MODE_TYPE_INDEX_COLOR,
  111. .bpp = 8,
  112. .bytes_per_pixel = 1,
  113. .number_of_colors = GRUB_VIDEO_FBSTD_EXT_NUMCOLORS
  114. },
  115. {
  116. .width = 640,
  117. .height = 480,
  118. .pitch = 1280,
  119. GRUB_VIDEO_MI_RGB555 ()
  120. },
  121. {
  122. .width = 800,
  123. .height = 600,
  124. .pitch = 1600,
  125. GRUB_VIDEO_MI_RGB555 ()
  126. },
  127. {
  128. .width = 1024,
  129. .height = 768,
  130. .pitch = 2048,
  131. GRUB_VIDEO_MI_RGB555 ()
  132. },
  133. {
  134. .width = 640,
  135. .height = 480,
  136. .pitch = 1280,
  137. GRUB_VIDEO_MI_RGB565 ()
  138. },
  139. {
  140. .width = 800,
  141. .height = 600,
  142. .pitch = 1600,
  143. GRUB_VIDEO_MI_RGB565 ()
  144. },
  145. {
  146. .width = 1024,
  147. .height = 768,
  148. .pitch = 2048,
  149. GRUB_VIDEO_MI_RGB565 ()
  150. },
  151. {
  152. .width = 640,
  153. .height = 480,
  154. .pitch = 640 * 3,
  155. GRUB_VIDEO_MI_RGB888 ()
  156. },
  157. {
  158. .width = 800,
  159. .height = 600,
  160. .pitch = 800 * 3,
  161. GRUB_VIDEO_MI_RGB888 ()
  162. },
  163. {
  164. .width = 1024,
  165. .height = 768,
  166. .pitch = 1024 * 3,
  167. GRUB_VIDEO_MI_RGB888 ()
  168. },
  169. {
  170. .width = 640,
  171. .height = 480,
  172. .pitch = 1280,
  173. GRUB_VIDEO_MI_BGR555 ()
  174. },
  175. {
  176. .width = 800,
  177. .height = 600,
  178. .pitch = 1600,
  179. GRUB_VIDEO_MI_BGR555 ()
  180. },
  181. {
  182. .width = 1024,
  183. .height = 768,
  184. .pitch = 2048,
  185. GRUB_VIDEO_MI_BGR555 ()
  186. },
  187. {
  188. .width = 640,
  189. .height = 480,
  190. .pitch = 1280,
  191. GRUB_VIDEO_MI_BGR565 ()
  192. },
  193. {
  194. .width = 800,
  195. .height = 600,
  196. .pitch = 1600,
  197. GRUB_VIDEO_MI_BGR565 ()
  198. },
  199. {
  200. .width = 1024,
  201. .height = 768,
  202. .pitch = 2048,
  203. GRUB_VIDEO_MI_BGR565 ()
  204. },
  205. {
  206. .width = 640,
  207. .height = 480,
  208. .pitch = 640 * 3,
  209. GRUB_VIDEO_MI_BGR888 ()
  210. },
  211. {
  212. .width = 800,
  213. .height = 600,
  214. .pitch = 800 * 3,
  215. GRUB_VIDEO_MI_BGR888 ()
  216. },
  217. {
  218. .width = 1024,
  219. .height = 768,
  220. .pitch = 1024 * 3,
  221. GRUB_VIDEO_MI_BGR888 ()
  222. },
  223. {
  224. .width = 640,
  225. .height = 480,
  226. .pitch = 640 * 4,
  227. GRUB_VIDEO_MI_BGRA8888()
  228. },
  229. {
  230. .width = 800,
  231. .height = 600,
  232. .pitch = 800 * 4,
  233. GRUB_VIDEO_MI_BGRA8888()
  234. },
  235. {
  236. .width = 1024,
  237. .height = 768,
  238. .pitch = 1024 * 4,
  239. GRUB_VIDEO_MI_BGRA8888()
  240. },
  241. };
  242. #ifdef GRUB_MACHINE_EMU
  243. #include <grub/emu/hostfile.h>
  244. struct bmp_header
  245. {
  246. grub_uint8_t magic[2];
  247. grub_uint32_t filesize;
  248. grub_uint32_t reserved;
  249. grub_uint32_t bmp_off;
  250. grub_uint32_t head_size;
  251. grub_uint16_t width;
  252. grub_uint16_t height;
  253. grub_uint16_t planes;
  254. grub_uint16_t bpp;
  255. } GRUB_PACKED;
  256. static void
  257. grub_video_capture_write_bmp (const char *fname,
  258. void *ptr,
  259. const struct grub_video_mode_info *mode_info)
  260. {
  261. grub_util_fd_t fd = grub_util_fd_open (fname, GRUB_UTIL_FD_O_WRONLY | GRUB_UTIL_FD_O_CREATTRUNC);
  262. struct bmp_header head;
  263. if (!GRUB_UTIL_FD_IS_VALID (fd))
  264. {
  265. grub_printf (_("cannot open `%s': %s"),
  266. fname, grub_util_fd_strerror ());
  267. }
  268. grub_memset (&head, 0, sizeof (head));
  269. head.magic[0] = 'B';
  270. head.magic[1] = 'M';
  271. if (mode_info->mode_type & GRUB_VIDEO_MODE_TYPE_RGB)
  272. {
  273. head.filesize = grub_cpu_to_le32 (sizeof (head) + mode_info->width * mode_info->height * 3);
  274. head.bmp_off = grub_cpu_to_le32_compile_time (sizeof (head));
  275. head.bpp = grub_cpu_to_le16_compile_time (24);
  276. }
  277. else
  278. {
  279. head.filesize = grub_cpu_to_le32 (sizeof (head) + 3 * 256 + mode_info->width * mode_info->height);
  280. head.bmp_off = grub_cpu_to_le32_compile_time (sizeof (head) + 3 * 256);
  281. head.bpp = grub_cpu_to_le16_compile_time (8);
  282. }
  283. head.head_size = grub_cpu_to_le32_compile_time (sizeof (head) - 14);
  284. head.width = grub_cpu_to_le16 (mode_info->width);
  285. head.height = grub_cpu_to_le16 (mode_info->height);
  286. head.planes = grub_cpu_to_le16_compile_time (1);
  287. head.width = grub_cpu_to_le16 (mode_info->width);
  288. head.height = grub_cpu_to_le16 (mode_info->height);
  289. grub_util_fd_write (fd, (char *) &head, sizeof (head));
  290. if (!(mode_info->mode_type & GRUB_VIDEO_MODE_TYPE_RGB))
  291. {
  292. struct grub_video_palette_data palette_data[256];
  293. int i;
  294. int palette_len = mode_info->number_of_colors;
  295. grub_memset (palette_data, 0, sizeof (palette_data));
  296. if (palette_len > 256)
  297. palette_len = 256;
  298. grub_video_get_palette (0, palette_len, palette_data);
  299. for (i = 0; i < 256; i++)
  300. {
  301. grub_uint8_t r, g, b;
  302. r = palette_data[i].r;
  303. g = palette_data[i].g;
  304. b = palette_data[i].b;
  305. grub_util_fd_write (fd, (char *) &b, 1);
  306. grub_util_fd_write (fd, (char *) &g, 1);
  307. grub_util_fd_write (fd, (char *) &r, 1);
  308. }
  309. }
  310. /* This does essentialy the same as some fbblit functions yet using
  311. them would mean testing them against themselves so keep this
  312. implemetation separate. */
  313. switch (mode_info->bytes_per_pixel)
  314. {
  315. case 4:
  316. {
  317. grub_uint8_t *buffer = xcalloc (3, mode_info->width);
  318. grub_uint32_t rmask = ((1 << mode_info->red_mask_size) - 1);
  319. grub_uint32_t gmask = ((1 << mode_info->green_mask_size) - 1);
  320. grub_uint32_t bmask = ((1 << mode_info->blue_mask_size) - 1);
  321. int rshift = mode_info->red_field_pos;
  322. int gshift = mode_info->green_field_pos;
  323. int bshift = mode_info->blue_field_pos;
  324. int mulrshift = (8 - mode_info->red_mask_size);
  325. int mulgshift = (8 - mode_info->green_mask_size);
  326. int mulbshift = (8 - mode_info->blue_mask_size);
  327. int y;
  328. for (y = mode_info->height - 1; y >= 0; y--)
  329. {
  330. grub_uint32_t *iptr = (grub_uint32_t *) ptr + (mode_info->pitch / 4) * y;
  331. int x;
  332. grub_uint8_t *optr = buffer;
  333. for (x = 0; x < (int) mode_info->width; x++)
  334. {
  335. grub_uint32_t val = *iptr++;
  336. *optr++ = ((val >> bshift) & bmask) << mulbshift;
  337. *optr++ = ((val >> gshift) & gmask) << mulgshift;
  338. *optr++ = ((val >> rshift) & rmask) << mulrshift;
  339. }
  340. grub_util_fd_write (fd, (char *) buffer, mode_info->width * 3);
  341. }
  342. grub_free (buffer);
  343. break;
  344. }
  345. case 3:
  346. {
  347. grub_uint8_t *buffer = xcalloc (3, mode_info->width);
  348. grub_uint32_t rmask = ((1 << mode_info->red_mask_size) - 1);
  349. grub_uint32_t gmask = ((1 << mode_info->green_mask_size) - 1);
  350. grub_uint32_t bmask = ((1 << mode_info->blue_mask_size) - 1);
  351. int rshift = mode_info->red_field_pos;
  352. int gshift = mode_info->green_field_pos;
  353. int bshift = mode_info->blue_field_pos;
  354. int mulrshift = (8 - mode_info->red_mask_size);
  355. int mulgshift = (8 - mode_info->green_mask_size);
  356. int mulbshift = (8 - mode_info->blue_mask_size);
  357. int y;
  358. for (y = mode_info->height - 1; y >= 0; y--)
  359. {
  360. grub_uint8_t *iptr = ((grub_uint8_t *) ptr + mode_info->pitch * y);
  361. int x;
  362. grub_uint8_t *optr = buffer;
  363. for (x = 0; x < (int) mode_info->width; x++)
  364. {
  365. grub_uint32_t val = 0;
  366. #ifdef GRUB_CPU_WORDS_BIGENDIAN
  367. val |= *iptr++ << 16;
  368. val |= *iptr++ << 8;
  369. val |= *iptr++;
  370. #else
  371. val |= *iptr++;
  372. val |= *iptr++ << 8;
  373. val |= *iptr++ << 16;
  374. #endif
  375. *optr++ = ((val >> bshift) & bmask) << mulbshift;
  376. *optr++ = ((val >> gshift) & gmask) << mulgshift;
  377. *optr++ = ((val >> rshift) & rmask) << mulrshift;
  378. }
  379. grub_util_fd_write (fd, (char *) buffer, mode_info->width * 3);
  380. }
  381. grub_free (buffer);
  382. break;
  383. }
  384. case 2:
  385. {
  386. grub_uint8_t *buffer = xcalloc (3, mode_info->width);
  387. grub_uint16_t rmask = ((1 << mode_info->red_mask_size) - 1);
  388. grub_uint16_t gmask = ((1 << mode_info->green_mask_size) - 1);
  389. grub_uint16_t bmask = ((1 << mode_info->blue_mask_size) - 1);
  390. int rshift = mode_info->red_field_pos;
  391. int gshift = mode_info->green_field_pos;
  392. int bshift = mode_info->blue_field_pos;
  393. int mulrshift = (8 - mode_info->red_mask_size);
  394. int mulgshift = (8 - mode_info->green_mask_size);
  395. int mulbshift = (8 - mode_info->blue_mask_size);
  396. int y;
  397. for (y = mode_info->height - 1; y >= 0; y--)
  398. {
  399. grub_uint16_t *iptr = (grub_uint16_t *) ptr + (mode_info->pitch / 2) * y;
  400. int x;
  401. grub_uint8_t *optr = buffer;
  402. for (x = 0; x < (int) mode_info->width; x++)
  403. {
  404. grub_uint16_t val = *iptr++;
  405. *optr++ = ((val >> bshift) & bmask) << mulbshift;
  406. *optr++ = ((val >> gshift) & gmask) << mulgshift;
  407. *optr++ = ((val >> rshift) & rmask) << mulrshift;
  408. }
  409. grub_util_fd_write (fd, (char *) buffer, mode_info->width * 3);
  410. }
  411. grub_free (buffer);
  412. break;
  413. }
  414. case 1:
  415. {
  416. int y;
  417. for (y = mode_info->height - 1; y >= 0; y--)
  418. grub_util_fd_write (fd, ((char *) ptr + mode_info->pitch * y),
  419. mode_info->width);
  420. break;
  421. }
  422. }
  423. grub_util_fd_close (fd);
  424. }
  425. #endif
  426. const char *
  427. grub_video_checksum_get_modename (void)
  428. {
  429. static char buf[40];
  430. if (capt_mode_info.mode_type & GRUB_VIDEO_MODE_TYPE_INDEX_COLOR)
  431. {
  432. grub_snprintf (buf, sizeof (buf), "i%d", capt_mode_info.number_of_colors);
  433. return buf;
  434. }
  435. if (capt_mode_info.red_field_pos == 0)
  436. {
  437. grub_snprintf (buf, sizeof (buf), "bgra%d%d%d%d", capt_mode_info.blue_mask_size,
  438. capt_mode_info.green_mask_size,
  439. capt_mode_info.red_mask_size,
  440. capt_mode_info.reserved_mask_size);
  441. return buf;
  442. }
  443. grub_snprintf (buf, sizeof (buf), "rgba%d%d%d%d", capt_mode_info.red_mask_size,
  444. capt_mode_info.green_mask_size,
  445. capt_mode_info.blue_mask_size,
  446. capt_mode_info.reserved_mask_size);
  447. return buf;
  448. }
  449. #define GENERATE_MODE 1
  450. //#define SAVE_ALL_IMAGES
  451. //#define COLLECT_TIME_STATISTICS 1
  452. #if defined (GENERATE_MODE) && defined (GRUB_MACHINE_EMU)
  453. grub_util_fd_t genfd = GRUB_UTIL_FD_INVALID;
  454. #endif
  455. #include <grub/time.h>
  456. static void
  457. write_time (void)
  458. {
  459. #if defined (GRUB_MACHINE_EMU) && defined (COLLECT_TIME_STATISTICS)
  460. char buf[60];
  461. static grub_uint64_t prev;
  462. grub_uint64_t cur;
  463. static grub_util_fd_t tmrfd = GRUB_UTIL_FD_INVALID;
  464. if (!GRUB_UTIL_FD_IS_VALID (tmrfd))
  465. tmrfd = grub_util_fd_open ("time.txt", GRUB_UTIL_FD_O_WRONLY
  466. | GRUB_UTIL_FD_O_CREATTRUNC);
  467. cur = grub_util_get_cpu_time_ms ();
  468. grub_snprintf (buf, sizeof (buf), "%s_%dx%dx%s:%d: %" PRIuGRUB_UINT64_T " ms\n",
  469. basename,
  470. capt_mode_info.width,
  471. capt_mode_info.height,
  472. grub_video_checksum_get_modename (), ctr,
  473. cur - prev);
  474. prev = cur;
  475. if (GRUB_UTIL_FD_IS_VALID (tmrfd))
  476. grub_util_fd_write (tmrfd, buf, grub_strlen (buf));
  477. #endif
  478. }
  479. static void
  480. checksum (void)
  481. {
  482. void *ptr;
  483. grub_uint32_t crc = 0;
  484. ptr = grub_video_capture_get_framebuffer ();
  485. write_time ();
  486. #ifdef GRUB_CPU_WORDS_BIGENDIAN
  487. switch (capt_mode_info.bytes_per_pixel)
  488. {
  489. case 1:
  490. crc = grub_getcrc32c (0, ptr, capt_mode_info.pitch
  491. * capt_mode_info.height);
  492. break;
  493. case 2:
  494. {
  495. unsigned x, y, rowskip;
  496. grub_uint8_t *iptr = ptr;
  497. crc = 0;
  498. rowskip = capt_mode_info.pitch - capt_mode_info.width * 2;
  499. for (y = 0; y < capt_mode_info.height; y++)
  500. {
  501. for (x = 0; x < capt_mode_info.width; x++)
  502. {
  503. crc = grub_getcrc32c (crc, iptr + 1, 1);
  504. crc = grub_getcrc32c (crc, iptr, 1);
  505. iptr += 2;
  506. }
  507. crc = grub_getcrc32c (crc, iptr, rowskip);
  508. iptr += rowskip;
  509. }
  510. break;
  511. }
  512. case 3:
  513. {
  514. unsigned x, y, rowskip;
  515. grub_uint8_t *iptr = ptr;
  516. crc = 0;
  517. rowskip = capt_mode_info.pitch - capt_mode_info.width * 3;
  518. for (y = 0; y < capt_mode_info.height; y++)
  519. {
  520. for (x = 0; x < capt_mode_info.width; x++)
  521. {
  522. crc = grub_getcrc32c (crc, iptr + 2, 1);
  523. crc = grub_getcrc32c (crc, iptr + 1, 1);
  524. crc = grub_getcrc32c (crc, iptr, 1);
  525. iptr += 3;
  526. }
  527. crc = grub_getcrc32c (crc, iptr, rowskip);
  528. iptr += rowskip;
  529. }
  530. break;
  531. }
  532. case 4:
  533. {
  534. unsigned x, y, rowskip;
  535. grub_uint8_t *iptr = ptr;
  536. crc = 0;
  537. rowskip = capt_mode_info.pitch - capt_mode_info.width * 4;
  538. for (y = 0; y < capt_mode_info.height; y++)
  539. {
  540. for (x = 0; x < capt_mode_info.width; x++)
  541. {
  542. crc = grub_getcrc32c (crc, iptr + 3, 1);
  543. crc = grub_getcrc32c (crc, iptr + 2, 1);
  544. crc = grub_getcrc32c (crc, iptr + 1, 1);
  545. crc = grub_getcrc32c (crc, iptr, 1);
  546. iptr += 4;
  547. }
  548. crc = grub_getcrc32c (crc, iptr, rowskip);
  549. iptr += rowskip;
  550. }
  551. break;
  552. }
  553. }
  554. #else
  555. crc = grub_getcrc32c (0, ptr, capt_mode_info.pitch * capt_mode_info.height);
  556. #endif
  557. #if defined (GENERATE_MODE) && defined (GRUB_MACHINE_EMU)
  558. if (GRUB_UTIL_FD_IS_VALID (genfd))
  559. {
  560. char buf[20];
  561. grub_snprintf (buf, sizeof (buf), "0x%x, ", crc);
  562. grub_util_fd_write (genfd, buf, grub_strlen (buf));
  563. }
  564. #endif
  565. if (!checksums || ctr >= nchk)
  566. {
  567. grub_test_assert (0, "Unexpected checksum %s_%dx%dx%s:%d: 0x%x",
  568. basename,
  569. capt_mode_info.width,
  570. capt_mode_info.height,
  571. grub_video_checksum_get_modename (), ctr, crc);
  572. }
  573. else if (crc != checksums[ctr])
  574. {
  575. grub_test_assert (0, "Checksum %s_%dx%dx%s:%d failed: 0x%x vs 0x%x",
  576. basename,
  577. capt_mode_info.width,
  578. capt_mode_info.height,
  579. grub_video_checksum_get_modename (),
  580. ctr, crc, checksums[ctr]);
  581. }
  582. #if !(defined (SAVE_ALL_IMAGES) && defined (GRUB_MACHINE_EMU))
  583. else
  584. {
  585. write_time ();
  586. ctr++;
  587. return;
  588. }
  589. #endif
  590. #ifdef GRUB_MACHINE_EMU
  591. char *name = grub_xasprintf ("%s_%dx%dx%s_%d.bmp", basename,
  592. capt_mode_info.width,
  593. capt_mode_info.height,
  594. grub_video_checksum_get_modename (),
  595. ctr);
  596. grub_video_capture_write_bmp (name, ptr, &capt_mode_info);
  597. grub_free (name);
  598. #endif
  599. write_time ();
  600. ctr++;
  601. }
  602. struct checksum_desc
  603. {
  604. const char *name;
  605. unsigned width;
  606. unsigned height;
  607. unsigned mode_type;
  608. unsigned number_of_colors;
  609. unsigned bpp;
  610. unsigned bytes_per_pixel;
  611. unsigned red_field_pos;
  612. unsigned red_mask_size;
  613. unsigned green_field_pos;
  614. unsigned green_mask_size;
  615. unsigned blue_field_pos;
  616. unsigned blue_mask_size;
  617. unsigned reserved_field_pos;
  618. unsigned reserved_mask_size;
  619. const grub_uint32_t *checksums;
  620. int nchk;
  621. };
  622. const struct checksum_desc checksum_table[] = {
  623. #include "checksums.h"
  624. };
  625. void
  626. grub_video_checksum (const char *basename_in)
  627. {
  628. unsigned i;
  629. grub_video_get_info (&capt_mode_info);
  630. #if defined (GENERATE_MODE) && defined (GRUB_MACHINE_EMU)
  631. if (!GRUB_UTIL_FD_IS_VALID (genfd))
  632. genfd = grub_util_fd_open ("checksums.h", GRUB_UTIL_FD_O_WRONLY
  633. | GRUB_UTIL_FD_O_CREATTRUNC);
  634. if (GRUB_UTIL_FD_IS_VALID (genfd))
  635. {
  636. char buf[400];
  637. grub_snprintf (buf, sizeof (buf), "\", %d, %d, 0x%x, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d /* %dx%dx%s */, (grub_uint32_t []) { ",
  638. capt_mode_info.width,
  639. capt_mode_info.height,
  640. capt_mode_info.mode_type,
  641. capt_mode_info.number_of_colors,
  642. capt_mode_info.bpp,
  643. capt_mode_info.bytes_per_pixel,
  644. capt_mode_info.red_field_pos,
  645. capt_mode_info.red_mask_size,
  646. capt_mode_info.green_field_pos,
  647. capt_mode_info.green_mask_size,
  648. capt_mode_info.blue_field_pos,
  649. capt_mode_info.blue_mask_size,
  650. capt_mode_info.reserved_field_pos,
  651. capt_mode_info.reserved_mask_size,
  652. capt_mode_info.width,
  653. capt_mode_info.height,
  654. grub_video_checksum_get_modename ());
  655. grub_util_fd_write (genfd, " { \"", 5);
  656. grub_util_fd_write (genfd, basename_in, grub_strlen (basename_in));
  657. grub_util_fd_write (genfd, buf, grub_strlen (buf));
  658. }
  659. #endif
  660. basename = grub_strdup (basename_in);
  661. nchk = 0;
  662. checksums = 0;
  663. /* FIXME: optimize this. */
  664. for (i = 0; i < ARRAY_SIZE (checksum_table); i++)
  665. if (grub_strcmp (checksum_table[i].name, basename_in) == 0
  666. && capt_mode_info.width == checksum_table[i].width
  667. && capt_mode_info.height == checksum_table[i].height
  668. && capt_mode_info.mode_type == checksum_table[i].mode_type
  669. && capt_mode_info.number_of_colors == checksum_table[i].number_of_colors
  670. && capt_mode_info.bpp == checksum_table[i].bpp
  671. && capt_mode_info.bytes_per_pixel == checksum_table[i].bytes_per_pixel
  672. && capt_mode_info.red_field_pos == checksum_table[i].red_field_pos
  673. && capt_mode_info.red_mask_size == checksum_table[i].red_mask_size
  674. && capt_mode_info.green_field_pos == checksum_table[i].green_field_pos
  675. && capt_mode_info.green_mask_size == checksum_table[i].green_mask_size
  676. && capt_mode_info.blue_field_pos == checksum_table[i].blue_field_pos
  677. && capt_mode_info.blue_mask_size == checksum_table[i].blue_mask_size
  678. && capt_mode_info.reserved_field_pos == checksum_table[i].reserved_field_pos
  679. && capt_mode_info.reserved_mask_size == checksum_table[i].reserved_mask_size)
  680. {
  681. nchk = checksum_table[i].nchk;
  682. checksums = checksum_table[i].checksums;
  683. break;
  684. }
  685. ctr = 0;
  686. grub_video_capture_refresh_cb = checksum;
  687. }
  688. void
  689. grub_video_checksum_end (void)
  690. {
  691. #if defined (GENERATE_MODE) && defined (GRUB_MACHINE_EMU)
  692. if (GRUB_UTIL_FD_IS_VALID (genfd))
  693. {
  694. char buf[40];
  695. grub_snprintf (buf, sizeof (buf), "}, %d },\n", ctr);
  696. grub_util_fd_write (genfd, buf, grub_strlen (buf));
  697. }
  698. #endif
  699. grub_test_assert (ctr == nchk, "Not enough checksums %s_%dx%dx%s: %d vs %d",
  700. basename,
  701. capt_mode_info.width,
  702. capt_mode_info.height,
  703. grub_video_checksum_get_modename (),
  704. ctr, nchk);
  705. grub_free (basename);
  706. basename = 0;
  707. nchk = 0;
  708. checksums = 0;
  709. ctr = 0;
  710. grub_video_capture_refresh_cb = 0;
  711. }
  712. static struct grub_term_output *saved_outputs;
  713. static struct grub_term_output *saved_gfxnext;
  714. static struct grub_term_output *gfxterm;
  715. static int use_gfxterm = 0;
  716. int
  717. grub_test_use_gfxterm (void)
  718. {
  719. FOR_ACTIVE_TERM_OUTPUTS (gfxterm)
  720. if (grub_strcmp (gfxterm->name, "gfxterm") == 0)
  721. break;
  722. if (!gfxterm)
  723. FOR_DISABLED_TERM_OUTPUTS (gfxterm)
  724. if (grub_strcmp (gfxterm->name, "gfxterm") == 0)
  725. break;
  726. if (!gfxterm)
  727. {
  728. grub_test_assert (0, "terminal `%s' isn't found", "gfxterm");
  729. return 1;
  730. }
  731. if (gfxterm->init (gfxterm))
  732. {
  733. grub_test_assert (0, "terminal `%s' failed: %s", "gfxterm", grub_errmsg);
  734. return 1;
  735. }
  736. saved_outputs = grub_term_outputs;
  737. saved_gfxnext = gfxterm->next;
  738. grub_term_outputs = gfxterm;
  739. gfxterm->next = 0;
  740. use_gfxterm = 1;
  741. return 0;
  742. }
  743. void
  744. grub_test_use_gfxterm_end (void)
  745. {
  746. if (!use_gfxterm)
  747. return;
  748. use_gfxterm = 0;
  749. gfxterm->fini (gfxterm);
  750. gfxterm->next = saved_gfxnext;
  751. grub_term_outputs = saved_outputs;
  752. saved_outputs = 0;
  753. saved_gfxnext = 0;
  754. }