2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 2006,2007,2008,2009 Free Software Foundation, Inc.
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.
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.
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/>.
19 #include <grub/video.h>
20 #include <grub/types.h>
22 #include <grub/misc.h>
24 #include <grub/i18n.h>
26 GRUB_MOD_LICENSE ("GPLv3+");
28 /* The list of video adapters registered to system. */
29 grub_video_adapter_t grub_video_adapter_list
= NULL
;
31 /* Active video adapter. */
32 grub_video_adapter_t grub_video_adapter_active
;
34 /* Restore back to initial mode (where applicable). */
36 grub_video_restore (void)
38 if (grub_video_adapter_active
)
40 grub_video_adapter_active
->fini ();
41 if (grub_errno
!= GRUB_ERR_NONE
)
44 grub_video_adapter_active
= 0;
49 /* Get information about active video mode. */
51 grub_video_get_info (struct grub_video_mode_info
*mode_info
)
53 if (! grub_video_adapter_active
)
54 return grub_error (GRUB_ERR_BAD_DEVICE
, "no video mode activated");
56 /* If mode_info is NULL just report that video adapter is active. */
59 grub_errno
= GRUB_ERR_NONE
;
63 return grub_video_adapter_active
->get_info (mode_info
);
66 grub_video_driver_id_t
67 grub_video_get_driver_id (void)
69 if (! grub_video_adapter_active
)
70 return GRUB_VIDEO_DRIVER_NONE
;
71 return grub_video_adapter_active
->id
;
74 /* Get information about active video mode. */
76 grub_video_get_info_and_fini (struct grub_video_mode_info
*mode_info
,
81 if (! grub_video_adapter_active
)
82 return grub_error (GRUB_ERR_BAD_DEVICE
, "no video mode activated");
84 err
= grub_video_adapter_active
->get_info_and_fini (mode_info
, framebuffer
);
88 grub_video_adapter_active
= 0;
92 /* Determine optimized blitting formation for specified video mode info. */
93 enum grub_video_blit_format
94 grub_video_get_blit_format (struct grub_video_mode_info
*mode_info
)
96 /* Check if we have any known 32 bit modes. */
97 if (mode_info
->bpp
== 32)
99 if ((mode_info
->red_mask_size
== 8)
100 && (mode_info
->red_field_pos
== 16)
101 && (mode_info
->green_mask_size
== 8)
102 && (mode_info
->green_field_pos
== 8)
103 && (mode_info
->blue_mask_size
== 8)
104 && (mode_info
->blue_field_pos
== 0))
106 return GRUB_VIDEO_BLIT_FORMAT_BGRA_8888
;
108 else if ((mode_info
->red_mask_size
== 8)
109 && (mode_info
->red_field_pos
== 0)
110 && (mode_info
->green_mask_size
== 8)
111 && (mode_info
->green_field_pos
== 8)
112 && (mode_info
->blue_mask_size
== 8)
113 && (mode_info
->blue_field_pos
== 16))
115 return GRUB_VIDEO_BLIT_FORMAT_RGBA_8888
;
118 /* Check if we have any known 24 bit modes. */
119 else if (mode_info
->bpp
== 24)
121 if ((mode_info
->red_mask_size
== 8)
122 && (mode_info
->red_field_pos
== 16)
123 && (mode_info
->green_mask_size
== 8)
124 && (mode_info
->green_field_pos
== 8)
125 && (mode_info
->blue_mask_size
== 8)
126 && (mode_info
->blue_field_pos
== 0))
128 return GRUB_VIDEO_BLIT_FORMAT_BGR_888
;
130 else if ((mode_info
->red_mask_size
== 8)
131 && (mode_info
->red_field_pos
== 0)
132 && (mode_info
->green_mask_size
== 8)
133 && (mode_info
->green_field_pos
== 8)
134 && (mode_info
->blue_mask_size
== 8)
135 && (mode_info
->blue_field_pos
== 16))
137 return GRUB_VIDEO_BLIT_FORMAT_RGB_888
;
140 /* Check if we have any known 16 bit modes. */
141 else if (mode_info
->bpp
== 16)
143 if ((mode_info
->red_mask_size
== 5)
144 && (mode_info
->red_field_pos
== 11)
145 && (mode_info
->green_mask_size
== 6)
146 && (mode_info
->green_field_pos
== 5)
147 && (mode_info
->blue_mask_size
== 5)
148 && (mode_info
->blue_field_pos
== 0))
150 return GRUB_VIDEO_BLIT_FORMAT_BGR_565
;
152 else if ((mode_info
->red_mask_size
== 5)
153 && (mode_info
->red_field_pos
== 0)
154 && (mode_info
->green_mask_size
== 6)
155 && (mode_info
->green_field_pos
== 5)
156 && (mode_info
->blue_mask_size
== 5)
157 && (mode_info
->blue_field_pos
== 11))
159 return GRUB_VIDEO_BLIT_FORMAT_RGB_565
;
162 else if (mode_info
->bpp
== 1)
163 return GRUB_VIDEO_BLIT_FORMAT_1BIT_PACKED
;
165 /* Backup route. Unknown format. */
167 /* If there are more than 8 bits per color, assume RGB(A) mode. */
168 if (mode_info
->bpp
> 8)
170 if (mode_info
->reserved_mask_size
> 0)
172 return GRUB_VIDEO_BLIT_FORMAT_RGBA
;
176 return GRUB_VIDEO_BLIT_FORMAT_RGB
;
180 /* Assume as indexcolor mode. */
181 return GRUB_VIDEO_BLIT_FORMAT_INDEXCOLOR
;
184 /* Set new indexed color palette entries. */
186 grub_video_set_palette (unsigned int start
, unsigned int count
,
187 struct grub_video_palette_data
*palette_data
)
189 if (! grub_video_adapter_active
)
190 return grub_error (GRUB_ERR_BAD_DEVICE
, "no video mode activated");
192 return grub_video_adapter_active
->set_palette (start
, count
, palette_data
);
195 /* Get indexed color palette entries. */
197 grub_video_get_palette (unsigned int start
, unsigned int count
,
198 struct grub_video_palette_data
*palette_data
)
200 if (! grub_video_adapter_active
)
201 return grub_error (GRUB_ERR_BAD_DEVICE
, "no video mode activated");
203 return grub_video_adapter_active
->get_palette (start
, count
, palette_data
);
206 /* Set viewport dimensions. */
208 grub_video_set_viewport (unsigned int x
, unsigned int y
,
209 unsigned int width
, unsigned int height
)
211 if (! grub_video_adapter_active
)
212 return grub_error (GRUB_ERR_BAD_DEVICE
, "no video mode activated");
214 return grub_video_adapter_active
->set_viewport (x
, y
, width
, height
);
217 /* Get viewport dimensions. */
219 grub_video_get_viewport (unsigned int *x
, unsigned int *y
,
220 unsigned int *width
, unsigned int *height
)
222 if (! grub_video_adapter_active
)
223 return grub_error (GRUB_ERR_BAD_DEVICE
, "no video mode activated");
225 return grub_video_adapter_active
->get_viewport (x
, y
, width
, height
);
228 /* Set region dimensions. */
230 grub_video_set_region (unsigned int x
, unsigned int y
,
231 unsigned int width
, unsigned int height
)
233 if (! grub_video_adapter_active
)
234 return grub_error (GRUB_ERR_BAD_DEVICE
, "no video mode activated");
236 return grub_video_adapter_active
->set_region (x
, y
, width
, height
);
239 /* Get region dimensions. */
241 grub_video_get_region (unsigned int *x
, unsigned int *y
,
242 unsigned int *width
, unsigned int *height
)
244 if (! grub_video_adapter_active
)
245 return grub_error (GRUB_ERR_BAD_DEVICE
, "no video mode activated");
247 return grub_video_adapter_active
->get_region (x
, y
, width
, height
);
250 /* Set status of the intersection of the viewport and the region. */
252 grub_video_set_area_status (grub_video_area_status_t area_status
)
254 if (! grub_video_adapter_active
)
255 return grub_error (GRUB_ERR_BAD_DEVICE
, "no video mode activated");
257 return grub_video_adapter_active
->set_area_status (area_status
);
260 /* Get status of the intersection of the viewport and the region. */
262 grub_video_get_area_status (grub_video_area_status_t
*area_status
)
264 if (! grub_video_adapter_active
)
265 return grub_error (GRUB_ERR_BAD_DEVICE
, "no video mode activated");
267 return grub_video_adapter_active
->get_area_status (area_status
);
270 /* Map color name to adapter specific color. */
272 grub_video_map_color (grub_uint32_t color_name
)
274 if (! grub_video_adapter_active
)
277 return grub_video_adapter_active
->map_color (color_name
);
280 /* Map RGB value to adapter specific color. */
282 grub_video_map_rgb (grub_uint8_t red
, grub_uint8_t green
, grub_uint8_t blue
)
284 if (! grub_video_adapter_active
)
287 return grub_video_adapter_active
->map_rgb (red
, green
, blue
);
290 /* Map RGBA value to adapter specific color. */
292 grub_video_map_rgba (grub_uint8_t red
, grub_uint8_t green
, grub_uint8_t blue
,
295 if (! grub_video_adapter_active
)
298 return grub_video_adapter_active
->map_rgba (red
, green
, blue
, alpha
);
301 /* Unmap video color back to RGBA components. */
303 grub_video_unmap_color (grub_video_color_t color
, grub_uint8_t
*red
,
304 grub_uint8_t
*green
, grub_uint8_t
*blue
,
307 if (! grub_video_adapter_active
)
308 return grub_error (GRUB_ERR_BAD_DEVICE
, "no video mode activated");
310 return grub_video_adapter_active
->unmap_color (color
,
317 /* Fill rectangle using specified color. */
319 grub_video_fill_rect (grub_video_color_t color
, int x
, int y
,
320 unsigned int width
, unsigned int height
)
322 if (! grub_video_adapter_active
)
323 return grub_error (GRUB_ERR_BAD_DEVICE
, "no video mode activated");
325 return grub_video_adapter_active
->fill_rect (color
, x
, y
, width
, height
);
328 /* Blit bitmap to screen. */
330 grub_video_blit_bitmap (struct grub_video_bitmap
*bitmap
,
331 enum grub_video_blit_operators oper
,
332 int x
, int y
, int offset_x
, int offset_y
,
333 unsigned int width
, unsigned int height
)
335 if (! grub_video_adapter_active
)
336 return grub_error (GRUB_ERR_BAD_DEVICE
, "no video mode activated");
338 return grub_video_adapter_active
->blit_bitmap (bitmap
, oper
, x
, y
,
343 /* Blit render target to active render target. */
345 grub_video_blit_render_target (struct grub_video_render_target
*target
,
346 enum grub_video_blit_operators oper
,
347 int x
, int y
, int offset_x
, int offset_y
,
348 unsigned int width
, unsigned int height
)
350 if (! grub_video_adapter_active
)
351 return grub_error (GRUB_ERR_BAD_DEVICE
, "no video mode activated");
353 return grub_video_adapter_active
->blit_render_target (target
, oper
, x
, y
,
358 /* Scroll viewport and fill new areas with specified color. */
360 grub_video_scroll (grub_video_color_t color
, int dx
, int dy
)
362 if (! grub_video_adapter_active
)
363 return grub_error (GRUB_ERR_BAD_DEVICE
, "no video mode activated");
365 return grub_video_adapter_active
->scroll (color
, dx
, dy
);
368 /* Swap buffers (swap active render target). */
370 grub_video_swap_buffers (void)
372 if (! grub_video_adapter_active
)
373 return grub_error (GRUB_ERR_BAD_DEVICE
, "no video mode activated");
375 return grub_video_adapter_active
->swap_buffers ();
378 /* Create new render target. */
380 grub_video_create_render_target (struct grub_video_render_target
**result
,
381 unsigned int width
, unsigned int height
,
382 unsigned int mode_type
)
385 if (! grub_video_adapter_active
)
386 return grub_error (GRUB_ERR_BAD_DEVICE
, "no video mode activated");
388 return grub_video_adapter_active
->create_render_target (result
,
393 /* Delete render target. */
395 grub_video_delete_render_target (struct grub_video_render_target
*target
)
398 return GRUB_ERR_NONE
;
399 if (! grub_video_adapter_active
)
400 return grub_error (GRUB_ERR_BAD_DEVICE
, "no video mode activated");
402 return grub_video_adapter_active
->delete_render_target (target
);
405 /* Set active render target. */
407 grub_video_set_active_render_target (struct grub_video_render_target
*target
)
409 if (! grub_video_adapter_active
)
410 return grub_error (GRUB_ERR_BAD_DEVICE
, "no video mode activated");
412 return grub_video_adapter_active
->set_active_render_target (target
);
415 /* Get active render target. */
417 grub_video_get_active_render_target (struct grub_video_render_target
**target
)
419 if (! grub_video_adapter_active
)
420 return grub_error (GRUB_ERR_BAD_DEVICE
, "no video mode activated");
422 return grub_video_adapter_active
->get_active_render_target (target
);
426 grub_video_edid_checksum (struct grub_video_edid_info
*edid_info
)
428 const char *edid_bytes
= (const char *) edid_info
;
432 /* Check EDID checksum. */
433 for (i
= 0; i
< 128; ++i
)
434 checksum
+= edid_bytes
[i
];
437 return grub_error (GRUB_ERR_BAD_DEVICE
,
438 "invalid EDID checksum %d", checksum
);
440 grub_errno
= GRUB_ERR_NONE
;
445 grub_video_edid_preferred_mode (struct grub_video_edid_info
*edid_info
,
446 unsigned int *width
, unsigned int *height
)
448 /* Bit 1 in the Feature Support field indicates that the first
449 Detailed Timing Description is the preferred timing mode. */
450 if (edid_info
->version
== 1 /* we don't understand later versions */
451 && (edid_info
->feature_support
452 & GRUB_VIDEO_EDID_FEATURE_PREFERRED_TIMING_MODE
)
453 && edid_info
->detailed_timings
[0].pixel_clock
)
455 *width
= edid_info
->detailed_timings
[0].horizontal_active_lo
457 (edid_info
->detailed_timings
[0].horizontal_hi
& 0xf0))
459 *height
= edid_info
->detailed_timings
[0].vertical_active_lo
461 (edid_info
->detailed_timings
[0].vertical_hi
& 0xf0))
463 if (*width
&& *height
)
464 return GRUB_ERR_NONE
;
467 return grub_error (GRUB_ERR_BAD_DEVICE
, "no preferred mode available");
470 /* Parse <width>x<height>[x<depth>]*/
472 parse_modespec (const char *current_mode
, int *width
, int *height
, int *depth
)
475 const char *param
= current_mode
;
477 *width
= *height
= *depth
= -1;
479 if (grub_strcmp (param
, "auto") == 0)
481 *width
= *height
= 0;
482 return GRUB_ERR_NONE
;
485 /* Find width value. */
487 param
= grub_strchr(param
, 'x');
489 return grub_error (GRUB_ERR_BAD_ARGUMENT
,
490 N_("invalid video mode specification `%s'"),
495 *width
= grub_strtoul (value
, 0, 0);
496 if (grub_errno
!= GRUB_ERR_NONE
)
497 return grub_error (GRUB_ERR_BAD_ARGUMENT
,
498 N_("invalid video mode specification `%s'"),
501 /* Find height value. */
503 param
= grub_strchr(param
, 'x');
506 *height
= grub_strtoul (value
, 0, 0);
507 if (grub_errno
!= GRUB_ERR_NONE
)
508 return grub_error (GRUB_ERR_BAD_ARGUMENT
,
509 N_("invalid video mode specification `%s'"),
514 /* We have optional color depth value. */
517 *height
= grub_strtoul (value
, 0, 0);
518 if (grub_errno
!= GRUB_ERR_NONE
)
519 return grub_error (GRUB_ERR_BAD_ARGUMENT
,
520 N_("invalid video mode specification `%s'"),
523 /* Convert color depth value. */
525 *depth
= grub_strtoul (value
, 0, 0);
526 if (grub_errno
!= GRUB_ERR_NONE
)
527 return grub_error (GRUB_ERR_BAD_ARGUMENT
,
528 N_("invalid video mode specification `%s'"),
531 return GRUB_ERR_NONE
;
535 grub_video_set_mode (const char *modestring
,
536 unsigned int modemask
,
537 unsigned int modevalue
)
544 if (grub_video_adapter_active
&& grub_video_adapter_active
->id
== GRUB_VIDEO_ADAPTER_CAPTURE
)
545 return GRUB_ERR_NONE
;
547 modevalue
&= modemask
;
549 /* Take copy of env.var. as we don't want to modify that. */
550 modevar
= grub_strdup (modestring
);
552 /* Initialize next mode. */
558 if (grub_memcmp (next_mode
, "keep", sizeof ("keep")) == 0
559 || grub_memcmp (next_mode
, "keep,", sizeof ("keep,") - 1) == 0
560 || grub_memcmp (next_mode
, "keep;", sizeof ("keep;") - 1) == 0)
565 if (grub_video_adapter_active
)
567 struct grub_video_mode_info mode_info
;
568 grub_memset (&mode_info
, 0, sizeof (mode_info
));
569 err
= grub_video_get_info (&mode_info
);
573 grub_errno
= GRUB_ERR_NONE
;
575 if ((mode_info
.mode_type
& modemask
) != modevalue
)
578 else if (((GRUB_VIDEO_MODE_TYPE_PURE_TEXT
& modemask
) != 0)
579 && ((GRUB_VIDEO_MODE_TYPE_PURE_TEXT
& modevalue
) == 0))
585 return GRUB_ERR_NONE
;
587 next_mode
+= sizeof ("keep") - 1;
592 /* TRANSLATORS: This doesn't imply that there is no available video
593 mode at all. All modes may have been filtered out by some criteria.
595 return grub_error (GRUB_ERR_BAD_ARGUMENT
,
596 N_("no suitable video mode found"));
599 /* Skip separator. */
603 /* De-activate last set video adapter. */
604 if (grub_video_adapter_active
)
606 /* Finalize adapter. */
607 grub_video_adapter_active
->fini ();
608 if (grub_errno
!= GRUB_ERR_NONE
)
609 grub_errno
= GRUB_ERR_NONE
;
611 /* Mark active adapter as not set. */
612 grub_video_adapter_active
= 0;
615 /* Loop until all modes has been tested out. */
616 while (next_mode
!= NULL
)
622 unsigned int flags
= modevalue
;
623 unsigned int flagmask
= modemask
;
625 /* Use last next_mode as current mode. */
628 /* Save position of next mode and separate modes. */
629 for (; *next_mode
; next_mode
++)
630 if (*next_mode
== ',' || *next_mode
== ';')
640 /* Skip whitespace. */
641 while (grub_isspace (*tmp
))
644 /* Initialize token holders. */
647 /* XXX: we assume that we're in pure text mode if
648 no video mode is initialized. Is it always true? */
649 if (grub_strcmp (current_mode
, "text") == 0)
651 struct grub_video_mode_info mode_info
;
653 grub_memset (&mode_info
, 0, sizeof (mode_info
));
654 if (((GRUB_VIDEO_MODE_TYPE_PURE_TEXT
& modemask
) == 0)
655 || ((GRUB_VIDEO_MODE_TYPE_PURE_TEXT
& modevalue
) != 0))
657 /* Valid mode found from adapter, and it has been activated.
658 Specify it as active adapter. */
659 grub_video_adapter_active
= NULL
;
664 return GRUB_ERR_NONE
;
668 err
= parse_modespec (current_mode
, &width
, &height
, &depth
);
671 /* Free memory before returning. */
677 /* Try out video mode. */
679 /* If user requested specific depth check if this depth is supported. */
680 if (depth
!= -1 && (flagmask
& GRUB_VIDEO_MODE_TYPE_DEPTH_MASK
)
682 (((flags
& GRUB_VIDEO_MODE_TYPE_DEPTH_MASK
)
683 != ((depth
<< GRUB_VIDEO_MODE_TYPE_DEPTH_POS
)
684 & GRUB_VIDEO_MODE_TYPE_DEPTH_MASK
))))
689 flags
|= (depth
<< GRUB_VIDEO_MODE_TYPE_DEPTH_POS
)
690 & GRUB_VIDEO_MODE_TYPE_DEPTH_MASK
;
691 flagmask
|= GRUB_VIDEO_MODE_TYPE_DEPTH_MASK
;
694 /* Try to initialize requested mode. Ignore any errors. */
695 grub_video_adapter_t p
;
697 /* Loop thru all possible video adapter trying to find requested mode. */
698 for (p
= grub_video_adapter_list
; p
; p
= p
->next
)
700 struct grub_video_mode_info mode_info
;
702 grub_memset (&mode_info
, 0, sizeof (mode_info
));
704 /* Try to initialize adapter, if it fails, skip to next adapter. */
706 if (err
!= GRUB_ERR_NONE
)
708 grub_errno
= GRUB_ERR_NONE
;
712 /* Try to initialize video mode. */
713 err
= p
->setup (width
, height
, flags
, flagmask
);
714 if (err
!= GRUB_ERR_NONE
)
717 grub_errno
= GRUB_ERR_NONE
;
721 err
= p
->get_info (&mode_info
);
722 if (err
!= GRUB_ERR_NONE
)
725 grub_errno
= GRUB_ERR_NONE
;
729 flags
= mode_info
.mode_type
& ~GRUB_VIDEO_MODE_TYPE_DEPTH_MASK
;
730 flags
|= (mode_info
.bpp
<< GRUB_VIDEO_MODE_TYPE_DEPTH_POS
)
731 & GRUB_VIDEO_MODE_TYPE_DEPTH_MASK
;
733 /* Check that mode is suitable for upper layer. */
734 if ((flags
& GRUB_VIDEO_MODE_TYPE_PURE_TEXT
)
735 ? (((GRUB_VIDEO_MODE_TYPE_PURE_TEXT
& modemask
) != 0)
736 && ((GRUB_VIDEO_MODE_TYPE_PURE_TEXT
& modevalue
) == 0))
737 : ((flags
& modemask
) != modevalue
))
740 grub_errno
= GRUB_ERR_NONE
;
744 /* Valid mode found from adapter, and it has been activated.
745 Specify it as active adapter. */
746 grub_video_adapter_active
= p
;
751 return GRUB_ERR_NONE
;
759 return grub_error (GRUB_ERR_BAD_ARGUMENT
,
760 N_("no suitable video mode found"));