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>
25
/* The list of video adapters registered to system. */
26
grub_video_adapter_t grub_video_adapter_list = NULL;
28
/* Active video adapter. */
29
static grub_video_adapter_t grub_video_adapter_active;
31
/* Restore back to initial mode (where applicable). */
33
grub_video_restore (void)
35
if (grub_video_adapter_active)
37
grub_video_adapter_active->fini ();
38
if (grub_errno != GRUB_ERR_NONE)
41
grub_video_adapter_active = 0;
46
/* Get information about active video mode. */
48
grub_video_get_info (struct grub_video_mode_info *mode_info)
50
if (! grub_video_adapter_active)
51
return grub_error (GRUB_ERR_BAD_DEVICE, "no video mode activated");
53
/* If mode_info is NULL just report that video adapter is active. */
56
grub_errno = GRUB_ERR_NONE;
60
return grub_video_adapter_active->get_info (mode_info);
63
grub_video_driver_id_t
64
grub_video_get_driver_id (void)
66
if (! grub_video_adapter_active)
67
return GRUB_VIDEO_DRIVER_NONE;
68
return grub_video_adapter_active->id;
71
/* Get information about active video mode. */
73
grub_video_get_info_and_fini (struct grub_video_mode_info *mode_info,
78
if (! grub_video_adapter_active)
79
return grub_error (GRUB_ERR_BAD_DEVICE, "no video mode activated");
81
err = grub_video_adapter_active->get_info_and_fini (mode_info, framebuffer);
85
grub_video_adapter_active = 0;
89
/* Determine optimized blitting formation for specified video mode info. */
90
enum grub_video_blit_format
91
grub_video_get_blit_format (struct grub_video_mode_info *mode_info)
93
/* Check if we have any known 32 bit modes. */
94
if (mode_info->bpp == 32)
96
if ((mode_info->red_mask_size == 8)
97
&& (mode_info->red_field_pos == 16)
98
&& (mode_info->green_mask_size == 8)
99
&& (mode_info->green_field_pos == 8)
100
&& (mode_info->blue_mask_size == 8)
101
&& (mode_info->blue_field_pos == 0))
103
return GRUB_VIDEO_BLIT_FORMAT_BGRA_8888;
105
else if ((mode_info->red_mask_size == 8)
106
&& (mode_info->red_field_pos == 0)
107
&& (mode_info->green_mask_size == 8)
108
&& (mode_info->green_field_pos == 8)
109
&& (mode_info->blue_mask_size == 8)
110
&& (mode_info->blue_field_pos == 16))
112
return GRUB_VIDEO_BLIT_FORMAT_RGBA_8888;
115
/* Check if we have any known 24 bit modes. */
116
else if (mode_info->bpp == 24)
118
if ((mode_info->red_mask_size == 8)
119
&& (mode_info->red_field_pos == 16)
120
&& (mode_info->green_mask_size == 8)
121
&& (mode_info->green_field_pos == 8)
122
&& (mode_info->blue_mask_size == 8)
123
&& (mode_info->blue_field_pos == 0))
125
return GRUB_VIDEO_BLIT_FORMAT_BGR_888;
127
else if ((mode_info->red_mask_size == 8)
128
&& (mode_info->red_field_pos == 0)
129
&& (mode_info->green_mask_size == 8)
130
&& (mode_info->green_field_pos == 8)
131
&& (mode_info->blue_mask_size == 8)
132
&& (mode_info->blue_field_pos == 16))
134
return GRUB_VIDEO_BLIT_FORMAT_RGB_888;
137
/* Check if we have any known 16 bit modes. */
138
else if (mode_info->bpp == 16)
140
if ((mode_info->red_mask_size == 5)
141
&& (mode_info->red_field_pos == 11)
142
&& (mode_info->green_mask_size == 6)
143
&& (mode_info->green_field_pos == 5)
144
&& (mode_info->blue_mask_size == 5)
145
&& (mode_info->blue_field_pos == 0))
147
return GRUB_VIDEO_BLIT_FORMAT_BGR_565;
149
else if ((mode_info->red_mask_size == 5)
150
&& (mode_info->red_field_pos == 0)
151
&& (mode_info->green_mask_size == 6)
152
&& (mode_info->green_field_pos == 5)
153
&& (mode_info->blue_mask_size == 5)
154
&& (mode_info->blue_field_pos == 11))
156
return GRUB_VIDEO_BLIT_FORMAT_RGB_565;
159
else if (mode_info->bpp == 1)
160
return GRUB_VIDEO_BLIT_FORMAT_1BIT_PACKED;
162
/* Backup route. Unknown format. */
164
/* If there are more than 8 bits per color, assume RGB(A) mode. */
165
if (mode_info->bpp > 8)
167
if (mode_info->reserved_mask_size > 0)
169
return GRUB_VIDEO_BLIT_FORMAT_RGBA;
173
return GRUB_VIDEO_BLIT_FORMAT_RGB;
177
/* Assume as indexcolor mode. */
178
return GRUB_VIDEO_BLIT_FORMAT_INDEXCOLOR;
181
/* Set new indexed color palette entries. */
183
grub_video_set_palette (unsigned int start, unsigned int count,
184
struct grub_video_palette_data *palette_data)
186
if (! grub_video_adapter_active)
187
return grub_error (GRUB_ERR_BAD_DEVICE, "no video mode activated");
189
return grub_video_adapter_active->set_palette (start, count, palette_data);
192
/* Get indexed color palette entries. */
194
grub_video_get_palette (unsigned int start, unsigned int count,
195
struct grub_video_palette_data *palette_data)
197
if (! grub_video_adapter_active)
198
return grub_error (GRUB_ERR_BAD_DEVICE, "no video mode activated");
200
return grub_video_adapter_active->get_palette (start, count, palette_data);
203
/* Set viewport dimensions. */
205
grub_video_set_viewport (unsigned int x, unsigned int y,
206
unsigned int width, unsigned int height)
208
if (! grub_video_adapter_active)
209
return grub_error (GRUB_ERR_BAD_DEVICE, "no video mode activated");
211
return grub_video_adapter_active->set_viewport (x, y, width, height);
214
/* Get viewport dimensions. */
216
grub_video_get_viewport (unsigned int *x, unsigned int *y,
217
unsigned int *width, unsigned int *height)
219
if (! grub_video_adapter_active)
220
return grub_error (GRUB_ERR_BAD_DEVICE, "no video mode activated");
222
return grub_video_adapter_active->get_viewport (x, y, width, height);
225
/* Map color name to adapter specific color. */
227
grub_video_map_color (grub_uint32_t color_name)
229
if (! grub_video_adapter_active)
232
return grub_video_adapter_active->map_color (color_name);
235
/* Map RGB value to adapter specific color. */
237
grub_video_map_rgb (grub_uint8_t red, grub_uint8_t green, grub_uint8_t blue)
239
if (! grub_video_adapter_active)
242
return grub_video_adapter_active->map_rgb (red, green, blue);
245
/* Map RGBA value to adapter specific color. */
247
grub_video_map_rgba (grub_uint8_t red, grub_uint8_t green, grub_uint8_t blue,
250
if (! grub_video_adapter_active)
253
return grub_video_adapter_active->map_rgba (red, green, blue, alpha);
256
/* Unmap video color back to RGBA components. */
258
grub_video_unmap_color (grub_video_color_t color, grub_uint8_t *red,
259
grub_uint8_t *green, grub_uint8_t *blue,
262
if (! grub_video_adapter_active)
263
return grub_error (GRUB_ERR_BAD_DEVICE, "no video mode activated");
265
return grub_video_adapter_active->unmap_color (color,
272
/* Fill rectangle using specified color. */
274
grub_video_fill_rect (grub_video_color_t color, int x, int y,
275
unsigned int width, unsigned int height)
277
if (! grub_video_adapter_active)
278
return grub_error (GRUB_ERR_BAD_DEVICE, "no video mode activated");
280
return grub_video_adapter_active->fill_rect (color, x, y, width, height);
283
/* Blit bitmap to screen. */
285
grub_video_blit_bitmap (struct grub_video_bitmap *bitmap,
286
enum grub_video_blit_operators oper,
287
int x, int y, int offset_x, int offset_y,
288
unsigned int width, unsigned int height)
290
if (! grub_video_adapter_active)
291
return grub_error (GRUB_ERR_BAD_DEVICE, "no video mode activated");
293
return grub_video_adapter_active->blit_bitmap (bitmap, oper, x, y,
298
/* Blit render target to active render target. */
300
grub_video_blit_render_target (struct grub_video_render_target *target,
301
enum grub_video_blit_operators oper,
302
int x, int y, int offset_x, int offset_y,
303
unsigned int width, unsigned int height)
305
if (! grub_video_adapter_active)
306
return grub_error (GRUB_ERR_BAD_DEVICE, "no video mode activated");
308
return grub_video_adapter_active->blit_render_target (target, oper, x, y,
313
/* Scroll viewport and fill new areas with specified color. */
315
grub_video_scroll (grub_video_color_t color, int dx, int dy)
317
if (! grub_video_adapter_active)
318
return grub_error (GRUB_ERR_BAD_DEVICE, "no video mode activated");
320
return grub_video_adapter_active->scroll (color, dx, dy);
323
/* Swap buffers (swap active render target). */
325
grub_video_swap_buffers (void)
327
if (! grub_video_adapter_active)
328
return grub_error (GRUB_ERR_BAD_DEVICE, "no video mode activated");
330
return grub_video_adapter_active->swap_buffers ();
333
/* Create new render target. */
335
grub_video_create_render_target (struct grub_video_render_target **result,
336
unsigned int width, unsigned int height,
337
unsigned int mode_type)
339
if (! grub_video_adapter_active)
340
return grub_error (GRUB_ERR_BAD_DEVICE, "no video mode activated");
342
return grub_video_adapter_active->create_render_target (result,
347
/* Delete render target. */
349
grub_video_delete_render_target (struct grub_video_render_target *target)
351
if (! grub_video_adapter_active)
352
return grub_error (GRUB_ERR_BAD_DEVICE, "no video mode activated");
354
return grub_video_adapter_active->delete_render_target (target);
357
/* Set active render target. */
359
grub_video_set_active_render_target (struct grub_video_render_target *target)
361
if (! grub_video_adapter_active)
362
return grub_error (GRUB_ERR_BAD_DEVICE, "no video mode activated");
364
return grub_video_adapter_active->set_active_render_target (target);
367
/* Get active render target. */
369
grub_video_get_active_render_target (struct grub_video_render_target **target)
371
if (! grub_video_adapter_active)
372
return grub_error (GRUB_ERR_BAD_DEVICE, "no video mode activated");
374
return grub_video_adapter_active->get_active_render_target (target);
377
/* Parse <width>x<height>[x<depth>]*/
379
parse_modespec (const char *current_mode, int *width, int *height, int *depth)
382
const char *param = current_mode;
384
*width = *height = *depth = -1;
386
if (grub_strcmp (param, "auto") == 0)
388
*width = *height = 0;
389
return GRUB_ERR_NONE;
392
/* Find width value. */
394
param = grub_strchr(param, 'x');
396
return grub_error (GRUB_ERR_BAD_ARGUMENT,
397
"Invalid mode: %s\n",
402
*width = grub_strtoul (value, 0, 0);
403
if (grub_errno != GRUB_ERR_NONE)
404
return grub_error (GRUB_ERR_BAD_ARGUMENT,
405
"Invalid mode: %s\n",
408
/* Find height value. */
410
param = grub_strchr(param, 'x');
413
*height = grub_strtoul (value, 0, 0);
414
if (grub_errno != GRUB_ERR_NONE)
415
return grub_error (GRUB_ERR_BAD_ARGUMENT,
416
"Invalid mode: %s\n",
421
/* We have optional color depth value. */
424
*height = grub_strtoul (value, 0, 0);
425
if (grub_errno != GRUB_ERR_NONE)
426
return grub_error (GRUB_ERR_BAD_ARGUMENT,
427
"Invalid mode: %s\n",
430
/* Convert color depth value. */
432
*depth = grub_strtoul (value, 0, 0);
433
if (grub_errno != GRUB_ERR_NONE)
434
return grub_error (GRUB_ERR_BAD_ARGUMENT,
435
"Invalid mode: %s\n",
438
return GRUB_ERR_NONE;
442
grub_video_set_mode (const char *modestring,
443
unsigned int modemask,
444
unsigned int modevalue)
451
modevalue &= modemask;
453
/* Take copy of env.var. as we don't want to modify that. */
454
modevar = grub_strdup (modestring);
456
/* Initialize next mode. */
460
return grub_error (GRUB_ERR_OUT_OF_MEMORY,
461
"couldn't allocate space for local modevar copy");
463
if (grub_memcmp (next_mode, "keep", sizeof ("keep")) == 0
464
|| grub_memcmp (next_mode, "keep,", sizeof ("keep,") - 1) == 0
465
|| grub_memcmp (next_mode, "keep;", sizeof ("keep;") - 1) == 0)
470
if (grub_video_adapter_active)
472
struct grub_video_mode_info mode_info;
473
grub_memset (&mode_info, 0, sizeof (mode_info));
474
err = grub_video_get_info (&mode_info);
478
grub_errno = GRUB_ERR_NONE;
480
if ((mode_info.mode_type & modemask) != modevalue)
483
else if (((GRUB_VIDEO_MODE_TYPE_PURE_TEXT & modemask) != 0)
484
&& ((GRUB_VIDEO_MODE_TYPE_PURE_TEXT & modevalue) == 0))
490
return GRUB_ERR_NONE;
492
next_mode += sizeof ("keep") - 1;
497
return grub_error (GRUB_ERR_BAD_ARGUMENT,
498
"no suitable mode found");
501
/* Skip separator. */
505
/* De-activate last set video adapter. */
506
if (grub_video_adapter_active)
508
/* Finalize adapter. */
509
grub_video_adapter_active->fini ();
510
if (grub_errno != GRUB_ERR_NONE)
511
grub_errno = GRUB_ERR_NONE;
513
/* Mark active adapter as not set. */
514
grub_video_adapter_active = 0;
517
/* Loop until all modes has been tested out. */
518
while (next_mode != NULL)
524
unsigned int flags = modevalue;
525
unsigned int flagmask = modemask;
527
/* Use last next_mode as current mode. */
530
/* Save position of next mode and separate modes. */
531
for (; *next_mode; next_mode++)
532
if (*next_mode == ',' || *next_mode == ';')
542
/* Skip whitespace. */
543
while (grub_isspace (*tmp))
546
/* Initialize token holders. */
549
/* XXX: we assume that we're in pure text mode if
550
no video mode is initialized. Is it always true? */
551
if (grub_strcmp (current_mode, "text") == 0)
553
struct grub_video_mode_info mode_info;
555
grub_memset (&mode_info, 0, sizeof (mode_info));
556
if (((GRUB_VIDEO_MODE_TYPE_PURE_TEXT & modemask) == 0)
557
|| ((GRUB_VIDEO_MODE_TYPE_PURE_TEXT & modevalue) != 0))
559
/* Valid mode found from adapter, and it has been activated.
560
Specify it as active adapter. */
561
grub_video_adapter_active = NULL;
566
return GRUB_ERR_NONE;
570
err = parse_modespec (current_mode, &width, &height, &depth);
573
/* Free memory before returning. */
579
/* Try out video mode. */
581
/* If user requested specific depth check if this depth is supported. */
582
if (depth != -1 && (flagmask & GRUB_VIDEO_MODE_TYPE_DEPTH_MASK)
584
(((flags & GRUB_VIDEO_MODE_TYPE_DEPTH_MASK)
585
!= ((depth << GRUB_VIDEO_MODE_TYPE_DEPTH_POS)
586
& GRUB_VIDEO_MODE_TYPE_DEPTH_MASK))))
591
flags |= (depth << GRUB_VIDEO_MODE_TYPE_DEPTH_POS)
592
& GRUB_VIDEO_MODE_TYPE_DEPTH_MASK;
593
flagmask |= GRUB_VIDEO_MODE_TYPE_DEPTH_MASK;
596
/* Try to initialize requested mode. Ignore any errors. */
597
grub_video_adapter_t p;
599
/* Loop thru all possible video adapter trying to find requested mode. */
600
for (p = grub_video_adapter_list; p; p = p->next)
602
struct grub_video_mode_info mode_info;
604
grub_memset (&mode_info, 0, sizeof (mode_info));
606
/* Try to initialize adapter, if it fails, skip to next adapter. */
608
if (err != GRUB_ERR_NONE)
610
grub_errno = GRUB_ERR_NONE;
614
/* Try to initialize video mode. */
615
err = p->setup (width, height, flags, flagmask);
616
if (err != GRUB_ERR_NONE)
619
grub_errno = GRUB_ERR_NONE;
623
err = p->get_info (&mode_info);
624
if (err != GRUB_ERR_NONE)
627
grub_errno = GRUB_ERR_NONE;
631
flags = mode_info.mode_type & ~GRUB_VIDEO_MODE_TYPE_DEPTH_MASK;
632
flags |= (mode_info.bpp << GRUB_VIDEO_MODE_TYPE_DEPTH_POS)
633
& GRUB_VIDEO_MODE_TYPE_DEPTH_MASK;
635
/* Check that mode is suitable for upper layer. */
636
if ((flags & GRUB_VIDEO_MODE_TYPE_PURE_TEXT)
637
? (((GRUB_VIDEO_MODE_TYPE_PURE_TEXT & modemask) != 0)
638
&& ((GRUB_VIDEO_MODE_TYPE_PURE_TEXT & modevalue) == 0))
639
: ((flags & modemask) != modevalue))
642
grub_errno = GRUB_ERR_NONE;
646
/* Valid mode found from adapter, and it has been activated.
647
Specify it as active adapter. */
648
grub_video_adapter_active = p;
653
return GRUB_ERR_NONE;
661
return grub_error (GRUB_ERR_BAD_ARGUMENT,
662
"no suitable mode found");
665
/* Initialize Video API module. */
670
/* Finalize Video API module. */