video_checksum.c 20.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 *  GRUB  --  GRand Unified Bootloader
 *  Copyright (C) 2013 Free Software Foundation, Inc.
 *
 *  GRUB is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  GRUB is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
 */

/* All tests need to include test.h for GRUB testing framework.  */
20 21 22

#include <config.h>

23 24 25 26 27
#include <grub/test.h>
#include <grub/dl.h>
#include <grub/video.h>
#include <grub/lib/crc.h>
#include <grub/mm.h>
28
#include <grub/term.h>
29 30 31 32
#ifdef GRUB_MACHINE_EMU
#include <grub/emu/hostdisk.h>
#include <grub/emu/misc.h>
#endif
33 34 35 36 37 38 39 40 41

GRUB_MOD_LICENSE ("GPLv3+");

static int ctr;
static int nchk;
static char *basename;
static const grub_uint32_t *checksums;
static struct grub_video_mode_info capt_mode_info;

42
struct grub_video_mode_info grub_test_video_modes[GRUB_TEST_VIDEO_ALL_N_MODES] = {
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
  {
    .width = 640,
    .height = 480,
    .pitch = 640,
    .mode_type = GRUB_VIDEO_MODE_TYPE_INDEX_COLOR,
    .bpp = 8,
    .bytes_per_pixel = 1,
    .number_of_colors = GRUB_VIDEO_FBSTD_NUMCOLORS
  },
  {
    .width = 800,
    .height = 600,
    .pitch = 800,
    .mode_type = GRUB_VIDEO_MODE_TYPE_INDEX_COLOR,
    .bpp = 8,
    .bytes_per_pixel = 1,
    .number_of_colors = GRUB_VIDEO_FBSTD_NUMCOLORS
  },
  {
    .width = 1024,
    .height = 768,
    .pitch = 1024,
    .mode_type = GRUB_VIDEO_MODE_TYPE_INDEX_COLOR,
    .bpp = 8,
    .bytes_per_pixel = 1,
    .number_of_colors = GRUB_VIDEO_FBSTD_NUMCOLORS
  },
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
  {
    .width = 640,
    .height = 480,
    .pitch = 640 * 4,
    GRUB_VIDEO_MI_RGBA8888()
  },
  {
    .width = 800,
    .height = 600,
    .pitch = 800 * 4,
    GRUB_VIDEO_MI_RGBA8888()
  },
  {
    .width = 1024,
    .height = 768,
    .pitch = 1024 * 4,
    GRUB_VIDEO_MI_RGBA8888()
  },
88 89 90 91 92 93
  {
    .width = 2560,
    .height = 1440,
    .pitch = 2560 * 4,
    GRUB_VIDEO_MI_RGBA8888()
  },
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
  {
    .width = 640,
    .height = 480,
    .pitch = 640,
    .mode_type = GRUB_VIDEO_MODE_TYPE_INDEX_COLOR,
    .bpp = 8,
    .bytes_per_pixel = 1,
    .number_of_colors = GRUB_VIDEO_FBSTD_EXT_NUMCOLORS
  },
  {
    .width = 800,
    .height = 600,
    .pitch = 800,
    .mode_type = GRUB_VIDEO_MODE_TYPE_INDEX_COLOR,
    .bpp = 8,
    .bytes_per_pixel = 1,
    .number_of_colors = GRUB_VIDEO_FBSTD_EXT_NUMCOLORS
  },
  {
    .width = 1024,
    .height = 768,
    .pitch = 1024,
    .mode_type = GRUB_VIDEO_MODE_TYPE_INDEX_COLOR,
    .bpp = 8,
    .bytes_per_pixel = 1,
    .number_of_colors = GRUB_VIDEO_FBSTD_EXT_NUMCOLORS
  },
121 122 123 124




125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
  {
    .width = 640,
    .height = 480,
    .pitch = 1280,
    GRUB_VIDEO_MI_RGB555 ()
  },
  {
    .width = 800,
    .height = 600,
    .pitch = 1600,
    GRUB_VIDEO_MI_RGB555 ()
  },
  {
    .width = 1024,
    .height = 768,
    .pitch = 2048,
    GRUB_VIDEO_MI_RGB555 ()
  },
  {
    .width = 640,
    .height = 480,
    .pitch = 1280,
    GRUB_VIDEO_MI_RGB565 ()
  },
  {
    .width = 800,
    .height = 600,
    .pitch = 1600,
    GRUB_VIDEO_MI_RGB565 ()
  },
  {
    .width = 1024,
    .height = 768,
    .pitch = 2048,
    GRUB_VIDEO_MI_RGB565 ()
  },
  {
    .width = 640,
    .height = 480,
    .pitch = 640 * 3,
    GRUB_VIDEO_MI_RGB888 ()
  },
  {
    .width = 800,
    .height = 600,
    .pitch = 800 * 3,
    GRUB_VIDEO_MI_RGB888 ()
  },
  {
    .width = 1024,
    .height = 768,
    .pitch = 1024 * 3,
    GRUB_VIDEO_MI_RGB888 ()
  },
  {
    .width = 640,
    .height = 480,
    .pitch = 1280,
    GRUB_VIDEO_MI_BGR555 ()
  },
  {
    .width = 800,
    .height = 600,
    .pitch = 1600,
    GRUB_VIDEO_MI_BGR555 ()
  },
  {
    .width = 1024,
    .height = 768,
    .pitch = 2048,
    GRUB_VIDEO_MI_BGR555 ()
  },
  {
    .width = 640,
    .height = 480,
    .pitch = 1280,
    GRUB_VIDEO_MI_BGR565 ()
  },
  {
    .width = 800,
    .height = 600,
    .pitch = 1600,
    GRUB_VIDEO_MI_BGR565 ()
  },
  {
    .width = 1024,
    .height = 768,
    .pitch = 2048,
    GRUB_VIDEO_MI_BGR565 ()
  },
  {
    .width = 640,
    .height = 480,
    .pitch = 640 * 3,
    GRUB_VIDEO_MI_BGR888 ()
  },
  {
    .width = 800,
    .height = 600,
    .pitch = 800 * 3,
    GRUB_VIDEO_MI_BGR888 ()
  },
  {
    .width = 1024,
    .height = 768,
    .pitch = 1024 * 3,
    GRUB_VIDEO_MI_BGR888 ()
  },
  {
    .width = 640,
    .height = 480,
    .pitch = 640 * 4,
    GRUB_VIDEO_MI_BGRA8888()
  },
  {
    .width = 800,
    .height = 600,
    .pitch = 800 * 4,
    GRUB_VIDEO_MI_BGRA8888()
  },
  {
    .width = 1024,
    .height = 768,
    .pitch = 1024 * 4,
    GRUB_VIDEO_MI_BGRA8888()
  },
};

253
#ifdef GRUB_MACHINE_EMU
254
#include <grub/emu/hostfile.h>
255 256 257 258 259 260 261 262 263 264 265 266

struct bmp_header
{
  grub_uint8_t magic[2];
  grub_uint32_t filesize;
  grub_uint32_t reserved;
  grub_uint32_t bmp_off;
  grub_uint32_t head_size;
  grub_uint16_t width;
  grub_uint16_t height;
  grub_uint16_t planes;
  grub_uint16_t bpp;
267
} GRUB_PACKED;
268 269 270 271 272 273

static void
grub_video_capture_write_bmp (const char *fname,
			      void *ptr,
			      const struct grub_video_mode_info *mode_info)
{
274
  grub_util_fd_t fd = grub_util_fd_open (fname, GRUB_UTIL_FD_O_WRONLY | GRUB_UTIL_FD_O_CREATTRUNC);
275 276
  struct bmp_header head;

277
  if (!GRUB_UTIL_FD_IS_VALID (fd))
278 279
    {
      grub_printf (_("cannot open `%s': %s"),
280
		   fname, grub_util_fd_strerror ());
281 282 283 284 285 286 287 288 289 290
    }

  grub_memset (&head, 0, sizeof (head));

  head.magic[0] = 'B';
  head.magic[1] = 'M';

  if (mode_info->mode_type & GRUB_VIDEO_MODE_TYPE_RGB)
    {
      head.filesize = grub_cpu_to_le32 (sizeof (head) + mode_info->width * mode_info->height * 3);
291
      head.bmp_off = grub_cpu_to_le32_compile_time (sizeof (head));
292 293 294 295 296
      head.bpp = grub_cpu_to_le16_compile_time (24);
    }
  else
    {
      head.filesize = grub_cpu_to_le32 (sizeof (head) + 3 * 256 + mode_info->width * mode_info->height);
297
      head.bmp_off = grub_cpu_to_le32_compile_time (sizeof (head) + 3 * 256);
298 299
      head.bpp = grub_cpu_to_le16_compile_time (8);
    }
300
  head.head_size = grub_cpu_to_le32_compile_time (sizeof (head) - 14);
301 302 303 304 305 306 307
  head.width = grub_cpu_to_le16 (mode_info->width);
  head.height = grub_cpu_to_le16 (mode_info->height);
  head.planes = grub_cpu_to_le16_compile_time (1);

  head.width = grub_cpu_to_le16 (mode_info->width);
  head.height = grub_cpu_to_le16 (mode_info->height);

308
  grub_util_fd_write (fd, (char *) &head, sizeof (head));
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325

  if (!(mode_info->mode_type & GRUB_VIDEO_MODE_TYPE_RGB))
    {
      struct grub_video_palette_data palette_data[256];
      int i;
      int palette_len = mode_info->number_of_colors;
      grub_memset (palette_data, 0, sizeof (palette_data));
      if (palette_len > 256)
	palette_len = 256;
      grub_video_get_palette (0, palette_len, palette_data);
      for (i = 0; i < 256; i++)
	{
	  grub_uint8_t r, g, b;
	  r = palette_data[i].r;
	  g = palette_data[i].g;
	  b = palette_data[i].b;

326 327 328
	  grub_util_fd_write (fd, (char *) &b, 1);
	  grub_util_fd_write (fd, (char *) &g, 1);
	  grub_util_fd_write (fd, (char *) &r, 1);
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
	}
    }

  /* This does essentialy the same as some fbblit functions yet using
     them would mean testing them against themselves so keep this
     implemetation separate.  */
  switch (mode_info->bytes_per_pixel)
    {
    case 4:
      {
	grub_uint8_t *buffer = xmalloc (mode_info->width * 3);
	grub_uint32_t rmask = ((1 << mode_info->red_mask_size) - 1);
	grub_uint32_t gmask = ((1 << mode_info->green_mask_size) - 1);
	grub_uint32_t bmask = ((1 << mode_info->blue_mask_size) - 1);
	int rshift = mode_info->red_field_pos;
	int gshift = mode_info->green_field_pos;
	int bshift = mode_info->blue_field_pos;
	int mulrshift = (8 - mode_info->red_mask_size);
	int mulgshift = (8 - mode_info->green_mask_size);
	int mulbshift = (8 - mode_info->blue_mask_size);
	int y;

	for (y = mode_info->height - 1; y >= 0; y--)
	  {
353
	    grub_uint32_t *iptr = (grub_uint32_t *) ptr + (mode_info->pitch / 4) * y;
354 355 356 357 358 359 360 361 362
	    int x;
	    grub_uint8_t *optr = buffer;
	    for (x = 0; x < (int) mode_info->width; x++)
	      {
		grub_uint32_t val = *iptr++;
		*optr++ = ((val >> bshift) & bmask) << mulbshift;
		*optr++ = ((val >> gshift) & gmask) << mulgshift;
		*optr++ = ((val >> rshift) & rmask) << mulrshift;
	      }
363
	    grub_util_fd_write (fd, (char *) buffer, mode_info->width * 3);
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
	  }
	grub_free (buffer);
	break;
      }
    case 3:
      {
	grub_uint8_t *buffer = xmalloc (mode_info->width * 3);
	grub_uint32_t rmask = ((1 << mode_info->red_mask_size) - 1);
	grub_uint32_t gmask = ((1 << mode_info->green_mask_size) - 1);
	grub_uint32_t bmask = ((1 << mode_info->blue_mask_size) - 1);
	int rshift = mode_info->red_field_pos;
	int gshift = mode_info->green_field_pos;
	int bshift = mode_info->blue_field_pos;
	int mulrshift = (8 - mode_info->red_mask_size);
	int mulgshift = (8 - mode_info->green_mask_size);
	int mulbshift = (8 - mode_info->blue_mask_size);
	int y;

	for (y = mode_info->height - 1; y >= 0; y--)
	  {
	    grub_uint8_t *iptr = ((grub_uint8_t *) ptr + mode_info->pitch * y);
	    int x;
	    grub_uint8_t *optr = buffer;
	    for (x = 0; x < (int) mode_info->width; x++)
	      {
		grub_uint32_t val = 0;
#ifdef GRUB_CPU_WORDS_BIGENDIAN
		val |= *iptr++ << 16;
		val |= *iptr++ << 8;
		val |= *iptr++;
#else
		val |= *iptr++;
		val |= *iptr++ << 8;
		val |= *iptr++ << 16;
#endif
		*optr++ = ((val >> bshift) & bmask) << mulbshift;
		*optr++ = ((val >> gshift) & gmask) << mulgshift;
		*optr++ = ((val >> rshift) & rmask) << mulrshift;
	      }
403
	    grub_util_fd_write (fd, (char *) buffer, mode_info->width * 3);
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
	  }
	grub_free (buffer);
	break;
      }
    case 2:
      {
	grub_uint8_t *buffer = xmalloc (mode_info->width * 3);
	grub_uint16_t rmask = ((1 << mode_info->red_mask_size) - 1);
	grub_uint16_t gmask = ((1 << mode_info->green_mask_size) - 1);
	grub_uint16_t bmask = ((1 << mode_info->blue_mask_size) - 1);
	int rshift = mode_info->red_field_pos;
	int gshift = mode_info->green_field_pos;
	int bshift = mode_info->blue_field_pos;
	int mulrshift = (8 - mode_info->red_mask_size);
	int mulgshift = (8 - mode_info->green_mask_size);
	int mulbshift = (8 - mode_info->blue_mask_size);
	int y;

	for (y = mode_info->height - 1; y >= 0; y--)
	  {
424
	    grub_uint16_t *iptr = (grub_uint16_t *) ptr + (mode_info->pitch / 2) * y;
425 426 427 428 429 430 431 432 433
	    int x;
	    grub_uint8_t *optr = buffer;
	    for (x = 0; x < (int) mode_info->width; x++)
	      {
		grub_uint16_t val = *iptr++;
		*optr++ = ((val >> bshift) & bmask) << mulbshift;
		*optr++ = ((val >> gshift) & gmask) << mulgshift;
		*optr++ = ((val >> rshift) & rmask) << mulrshift;
	      }
434
	    grub_util_fd_write (fd, (char *) buffer, mode_info->width * 3);
435 436 437 438 439 440 441 442 443
	  }
	grub_free (buffer);
	break;
      }
    case 1:
      {
	int y;

	for (y = mode_info->height - 1; y >= 0; y--)
444 445
	  grub_util_fd_write (fd, ((char *) ptr + mode_info->pitch * y),
			      mode_info->width);
446 447 448
	break;
      }
    }
449
  grub_util_fd_close (fd);
450 451 452 453
}

#endif

454 455
const char *
grub_video_checksum_get_modename (void)
456 457 458 459 460 461 462 463 464
{
  static char buf[40];
  if (capt_mode_info.mode_type & GRUB_VIDEO_MODE_TYPE_INDEX_COLOR)
    {
      grub_snprintf (buf, sizeof (buf), "i%d", capt_mode_info.number_of_colors);
      return buf;
    }
  if (capt_mode_info.red_field_pos == 0)
    {
465
      grub_snprintf (buf, sizeof (buf), "bgra%d%d%d%d", capt_mode_info.blue_mask_size,
466
		     capt_mode_info.green_mask_size,
467 468
		     capt_mode_info.red_mask_size,
		     capt_mode_info.reserved_mask_size);
469 470
      return buf;
    }
471
  grub_snprintf (buf, sizeof (buf), "rgba%d%d%d%d", capt_mode_info.red_mask_size,
472
		 capt_mode_info.green_mask_size,
473 474
		 capt_mode_info.blue_mask_size,
		 capt_mode_info.reserved_mask_size);
475 476 477
  return buf;
}

478
#define GENERATE_MODE 1
479 480
//#define SAVE_ALL_IMAGES
//#define COLLECT_TIME_STATISTICS 1
481 482

#if defined (GENERATE_MODE) && defined (GRUB_MACHINE_EMU)
483
grub_util_fd_t genfd = GRUB_UTIL_FD_INVALID;
484 485
#endif

486 487 488 489 490 491 492 493 494
#include <grub/time.h>

static void
write_time (void)
{
#if defined (GRUB_MACHINE_EMU) && defined (COLLECT_TIME_STATISTICS)
  char buf[60];
  static grub_uint64_t prev;
  grub_uint64_t cur;
495 496 497 498
  static grub_util_fd_t tmrfd = GRUB_UTIL_FD_INVALID;
  if (!GRUB_UTIL_FD_IS_VALID (tmrfd))
    tmrfd = grub_util_fd_open ("time.txt", GRUB_UTIL_FD_O_WRONLY
			       | GRUB_UTIL_FD_O_CREATTRUNC);
499

500
  cur = grub_util_get_cpu_time_ms ();
501 502 503 504 505 506 507
  grub_snprintf (buf, sizeof (buf), "%s_%dx%dx%s:%d: %" PRIuGRUB_UINT64_T " ms\n",
		 basename, 			
		 capt_mode_info.width,
		 capt_mode_info.height, 
		 grub_video_checksum_get_modename (), ctr,
		 cur - prev);
  prev = cur;
508 509
  if (GRUB_UTIL_FD_IS_VALID (tmrfd))
    grub_util_fd_write (tmrfd, buf, grub_strlen (buf));
510 511 512 513
#endif
}


514 515 516 517
static void
checksum (void)
{
  void *ptr;
518
  grub_uint32_t crc = 0;
519 520 521

  ptr = grub_video_capture_get_framebuffer ();

522 523
  write_time ();

524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592
#ifdef GRUB_CPU_WORDS_BIGENDIAN
  switch (capt_mode_info.bytes_per_pixel)
    {
    case 1:
      crc = grub_getcrc32c (0, ptr, capt_mode_info.pitch
			    * capt_mode_info.height);
      break;
    case 2:
      {
	unsigned x, y, rowskip;
	grub_uint8_t *iptr = ptr;
	crc = 0;
	rowskip = capt_mode_info.pitch - capt_mode_info.width * 2;
	for (y = 0; y < capt_mode_info.height; y++)
	  {
	    for (x = 0; x < capt_mode_info.width; x++)
	      {
		crc = grub_getcrc32c (crc, iptr + 1, 1);
		crc = grub_getcrc32c (crc, iptr, 1);
		iptr += 2;
	      }
	    crc = grub_getcrc32c (crc, iptr, rowskip);
	    iptr += rowskip;
	  }
	break;
      }
    case 3:
      {
	unsigned x, y, rowskip;
	grub_uint8_t *iptr = ptr;
	crc = 0;
	rowskip = capt_mode_info.pitch - capt_mode_info.width * 3;
	for (y = 0; y < capt_mode_info.height; y++)
	  {
	    for (x = 0; x < capt_mode_info.width; x++)
	      {
		crc = grub_getcrc32c (crc, iptr + 2, 1);
		crc = grub_getcrc32c (crc, iptr + 1, 1);
		crc = grub_getcrc32c (crc, iptr, 1);
		iptr += 3;
	      }
	    crc = grub_getcrc32c (crc, iptr, rowskip);
	    iptr += rowskip;
	  }
	break;
      }
    case 4:
      {
	unsigned x, y, rowskip;
	grub_uint8_t *iptr = ptr;
	crc = 0;
	rowskip = capt_mode_info.pitch - capt_mode_info.width * 4;
	for (y = 0; y < capt_mode_info.height; y++)
	  {
	    for (x = 0; x < capt_mode_info.width; x++)
	      {
		crc = grub_getcrc32c (crc, iptr + 3, 1);
		crc = grub_getcrc32c (crc, iptr + 2, 1);
		crc = grub_getcrc32c (crc, iptr + 1, 1);
		crc = grub_getcrc32c (crc, iptr, 1);
		iptr += 4;
	      }
	    crc = grub_getcrc32c (crc, iptr, rowskip);
	    iptr += rowskip;
	  }
	break;
      }
    }
#else
593
  crc = grub_getcrc32c (0, ptr, capt_mode_info.pitch * capt_mode_info.height);
594
#endif
595 596

#if defined (GENERATE_MODE) && defined (GRUB_MACHINE_EMU)
597
  if (GRUB_UTIL_FD_IS_VALID (genfd))
598 599 600
    {
      char buf[20];
      grub_snprintf (buf, sizeof (buf), "0x%x, ", crc);
601
      grub_util_fd_write (genfd, buf, grub_strlen (buf));
602 603 604
    }
#endif

605 606 607 608 609
  if (!checksums || ctr >= nchk)
    {
      grub_test_assert (0, "Unexpected checksum %s_%dx%dx%s:%d: 0x%x",
			basename, 			
			capt_mode_info.width,
610 611
			capt_mode_info.height, 
			grub_video_checksum_get_modename (), ctr, crc);
612 613 614 615 616 617
    }
  else if (crc != checksums[ctr])
    {
      grub_test_assert (0, "Checksum %s_%dx%dx%s:%d failed: 0x%x vs 0x%x",
			basename,
			capt_mode_info.width,
618 619
			capt_mode_info.height,
			grub_video_checksum_get_modename (),
620 621
			ctr, crc, checksums[ctr]);
    }
622
#if !(defined (SAVE_ALL_IMAGES) && defined (GRUB_MACHINE_EMU))
623 624
  else
    {
625
      write_time ();
626 627 628
      ctr++;
      return;
    }
629
#endif
630 631 632
#ifdef GRUB_MACHINE_EMU
  char *name = grub_xasprintf ("%s_%dx%dx%s_%d.bmp", basename, 
			       capt_mode_info.width,
633 634
			       capt_mode_info.height,
			       grub_video_checksum_get_modename (),
635 636
			       ctr);
  grub_video_capture_write_bmp (name, ptr, &capt_mode_info);
637
  grub_free (name);
638 639
#endif

640
  write_time ();
641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666

  ctr++;
}

struct checksum_desc
{
  const char *name;
  unsigned width;
  unsigned height;
  unsigned mode_type;
  unsigned number_of_colors;
  unsigned bpp;
  unsigned bytes_per_pixel;
  unsigned red_field_pos;
  unsigned red_mask_size;
  unsigned green_field_pos;
  unsigned green_mask_size;
  unsigned blue_field_pos;
  unsigned blue_mask_size;
  unsigned reserved_field_pos;
  unsigned reserved_mask_size;
  const grub_uint32_t *checksums;
  int nchk;
};

const struct checksum_desc checksum_table[] = {
667
#include "checksums.h"
668 669 670 671 672 673 674 675 676 677
};

void
grub_video_checksum (const char *basename_in)
{
  unsigned i;

  grub_video_get_info (&capt_mode_info);

#if defined (GENERATE_MODE) && defined (GRUB_MACHINE_EMU)
678 679 680 681
  if (!GRUB_UTIL_FD_IS_VALID (genfd))
    genfd = grub_util_fd_open ("checksums.h", GRUB_UTIL_FD_O_WRONLY
			       | GRUB_UTIL_FD_O_CREATTRUNC);
  if (GRUB_UTIL_FD_IS_VALID (genfd))
682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700
    {
      char buf[400];

      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 []) { ",
		     capt_mode_info.width,
		     capt_mode_info.height,
		     capt_mode_info.mode_type,
		     capt_mode_info.number_of_colors,
		     capt_mode_info.bpp,
		     capt_mode_info.bytes_per_pixel,
		     capt_mode_info.red_field_pos,
		     capt_mode_info.red_mask_size,
		     capt_mode_info.green_field_pos,
		     capt_mode_info.green_mask_size,
		     capt_mode_info.blue_field_pos,
		     capt_mode_info.blue_mask_size,
		     capt_mode_info.reserved_field_pos,
		     capt_mode_info.reserved_mask_size,
		     capt_mode_info.width,
701 702
		     capt_mode_info.height,
		     grub_video_checksum_get_modename ());
703

704 705 706
      grub_util_fd_write (genfd, "  { \"", 5);
      grub_util_fd_write (genfd, basename_in, grub_strlen (basename_in));
      grub_util_fd_write (genfd, buf, grub_strlen (buf));
707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743
    }
#endif

  basename = grub_strdup (basename_in);
  nchk = 0;
  checksums = 0;
  /* FIXME: optimize this.  */
  for (i = 0; i < ARRAY_SIZE (checksum_table); i++)
    if (grub_strcmp (checksum_table[i].name, basename_in) == 0
	&& capt_mode_info.width == checksum_table[i].width
	&& capt_mode_info.height == checksum_table[i].height
	&& capt_mode_info.mode_type == checksum_table[i].mode_type
	&& capt_mode_info.number_of_colors == checksum_table[i].number_of_colors
	&& capt_mode_info.bpp == checksum_table[i].bpp
	&& capt_mode_info.bytes_per_pixel == checksum_table[i].bytes_per_pixel
	&& capt_mode_info.red_field_pos == checksum_table[i].red_field_pos
	&& capt_mode_info.red_mask_size == checksum_table[i].red_mask_size
	&& capt_mode_info.green_field_pos == checksum_table[i].green_field_pos
	&& capt_mode_info.green_mask_size == checksum_table[i].green_mask_size
	&& capt_mode_info.blue_field_pos == checksum_table[i].blue_field_pos
	&& capt_mode_info.blue_mask_size == checksum_table[i].blue_mask_size
	&& capt_mode_info.reserved_field_pos == checksum_table[i].reserved_field_pos
	&& capt_mode_info.reserved_mask_size == checksum_table[i].reserved_mask_size)
      {
	nchk = checksum_table[i].nchk;
	checksums = checksum_table[i].checksums;
	break;
      }

  ctr = 0;
  grub_video_capture_refresh_cb = checksum;
}

void
grub_video_checksum_end (void)
{
#if defined (GENERATE_MODE) && defined (GRUB_MACHINE_EMU)
744
  if (GRUB_UTIL_FD_IS_VALID (genfd))
745 746
    {
      char buf[40];
747
      grub_snprintf (buf, sizeof (buf), "}, %d },\n", ctr);
748
      grub_util_fd_write (genfd, buf, grub_strlen (buf));
749 750 751 752 753
    }
#endif
  grub_test_assert (ctr == nchk, "Not enough checksums %s_%dx%dx%s: %d vs %d",
		    basename,
		    capt_mode_info.width,
754 755
		    capt_mode_info.height,
		    grub_video_checksum_get_modename (),
756 757 758 759 760 761 762 763
		    ctr, nchk);
  grub_free (basename);
  basename = 0;
  nchk = 0;
  checksums = 0;
  ctr = 0;
  grub_video_capture_refresh_cb = 0;
}
764 765 766 767

static struct grub_term_output *saved_outputs;
static struct grub_term_output *saved_gfxnext;
static struct grub_term_output *gfxterm;
768
static int use_gfxterm = 0;
769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786

int
grub_test_use_gfxterm (void)
{
  FOR_ACTIVE_TERM_OUTPUTS (gfxterm)
    if (grub_strcmp (gfxterm->name, "gfxterm") == 0)
      break;
  if (!gfxterm)
    FOR_DISABLED_TERM_OUTPUTS (gfxterm)
      if (grub_strcmp (gfxterm->name, "gfxterm") == 0)
	break;

  if (!gfxterm)
    {
      grub_test_assert (0, "terminal `%s' isn't found", "gfxterm");
      return 1;
    }

787 788 789 790 791 792 793
  if (gfxterm->init (gfxterm))
    { 
      grub_test_assert (0, "terminal `%s' failed: %s", "gfxterm", grub_errmsg);
      return 1;
    }

  saved_outputs = grub_term_outputs;
794 795 796
  saved_gfxnext = gfxterm->next;
  grub_term_outputs = gfxterm;
  gfxterm->next = 0;
797
  use_gfxterm = 1;
798 799 800 801 802 803 804

  return 0;
}

void
grub_test_use_gfxterm_end (void)
{
805 806 807
  if (!use_gfxterm)
    return;
  use_gfxterm = 0;
808 809 810
  gfxterm->fini (gfxterm);
  gfxterm->next = saved_gfxnext;
  grub_term_outputs = saved_outputs;
811 812
  saved_outputs = 0;
  saved_gfxnext = 0;
813
}