~hamo/ubuntu/precise/grub2/grub2.hi_res

« back to all changes in this revision

Viewing changes to term/i386/pc/vga.c

  • Committer: Bazaar Package Importer
  • Author(s): Colin Watson, Colin Watson, Robert Millan, Updated translations
  • Date: 2010-11-22 12:24:56 UTC
  • mfrom: (1.26.4 upstream) (17.3.36 sid)
  • mto: (17.3.43 sid)
  • mto: This revision was merged to the branch mainline in revision 89.
  • Revision ID: james.westby@ubuntu.com-20101122122456-y82z3sfb7k4zfdcc
Tags: 1.99~20101122-1
[ Colin Watson ]
* New Bazaar snapshot.  Too many changes to list in full, but some of the
  more user-visible ones are as follows:
  - GRUB script:
    + Function parameters, "break", "continue", "shift", "setparams",
      "return", and "!".
    + "export" command supports multiple variable names.
    + Multi-line quoted strings support.
    + Wildcard expansion.
  - sendkey support.
  - USB hotunplugging and USB serial support.
  - Rename CD-ROM to cd on BIOS.
  - Add new --boot-directory option to grub-install, grub-reboot, and
    grub-set-default; the old --root-directory option is still accepted
    but was often confusing.
  - Basic btrfs detection/UUID support (but no file reading yet).
  - bash-completion for utilities.
  - If a device is listed in device.map, always assume that it is
    BIOS-visible rather than using extra layers such as LVM or RAID.
  - Add grub-mknetdir script (closes: #550658).
  - Remove deprecated "root" command.
  - Handle RAID devices containing virtio components.
  - GRUB Legacy configuration file support (via grub-menulst2cfg).
  - Keyboard layout support (via grub-mklayout and grub-kbdcomp).
  - Check generated grub.cfg for syntax errors before saving.
  - Pause execution for at most ten seconds if any errors are displayed,
    so that the user has a chance to see them.
  - Support submenus.
  - Write embedding zone using Reed-Solomon, so that it's robust against
    being partially overwritten (closes: #550702, #591416, #593347).
  - GRUB_DISABLE_LINUX_RECOVERY and GRUB_DISABLE_NETBSD_RECOVERY merged
    into a single GRUB_DISABLE_RECOVERY variable.
  - Fix loader memory allocation failure (closes: #551627).
  - Don't call savedefault on recovery entries (closes: #589325).
  - Support triple-indirect blocks on ext2 (closes: #543924).
  - Recognise DDF1 fake RAID (closes: #603354).

[ Robert Millan ]
* Use dpkg architecture wildcards.

[ Updated translations ]
* Slovenian (Vanja Cvelbar).  Closes: #604003
* Dzongkha (dawa pemo via Tenzin Dendup).  Closes: #604102

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 *  GRUB  --  GRand Unified Bootloader
3
 
 *  Copyright (C) 2000,2001,2002,2003,2004,2005,2007,2008,2009  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
 
 
19
 
// TODO: Deprecated and broken. Needs to be converted to Video Driver!
20
 
 
21
 
#include <grub/machine/vga.h>
22
 
#include <grub/machine/console.h>
23
 
#include <grub/cpu/io.h>
24
 
#include <grub/term.h>
25
 
#include <grub/types.h>
26
 
#include <grub/dl.h>
27
 
#include <grub/misc.h>
28
 
#include <grub/font.h>
29
 
 
30
 
#define DEBUG_VGA       0
31
 
 
32
 
#define VGA_WIDTH       640
33
 
#define VGA_HEIGHT      350
34
 
#define CHAR_WIDTH      8
35
 
#define CHAR_HEIGHT     16
36
 
#define TEXT_WIDTH      (VGA_WIDTH / CHAR_WIDTH)
37
 
#define TEXT_HEIGHT     (VGA_HEIGHT / CHAR_HEIGHT)
38
 
#define VGA_MEM         ((grub_uint8_t *) GRUB_MEMORY_MACHINE_VGA_ADDR)
39
 
#define PAGE_OFFSET(x)  ((x) * (VGA_WIDTH * VGA_HEIGHT / 8))
40
 
 
41
 
#define DEFAULT_FG_COLOR        0xa
42
 
#define DEFAULT_BG_COLOR        0x0
43
 
 
44
 
struct colored_char
45
 
{
46
 
  /* An Unicode codepoint.  */
47
 
  grub_uint32_t code;
48
 
 
49
 
  /* Color indexes.  */
50
 
  unsigned char fg_color;
51
 
  unsigned char bg_color;
52
 
 
53
 
  /* The width of this character minus one.  */
54
 
  unsigned char width;
55
 
 
56
 
  /* The column index of this character.  */
57
 
  unsigned char index;
58
 
};
59
 
 
60
 
static unsigned char text_mode;
61
 
static unsigned xpos, ypos;
62
 
static int cursor_state;
63
 
static unsigned char fg_color, bg_color;
64
 
static struct colored_char text_buf[TEXT_WIDTH * TEXT_HEIGHT];
65
 
static unsigned char saved_map_mask;
66
 
static int page = 0;
67
 
static grub_font_t font = 0;
68
 
 
69
 
#define SEQUENCER_ADDR_PORT     0x3C4
70
 
#define SEQUENCER_DATA_PORT     0x3C5
71
 
#define MAP_MASK_REGISTER       0x02
72
 
 
73
 
#define CRTC_ADDR_PORT          0x3D4
74
 
#define CRTC_DATA_PORT          0x3D5
75
 
#define START_ADDR_HIGH_REGISTER 0x0C
76
 
#define START_ADDR_LOW_REGISTER 0x0D
77
 
 
78
 
#define GRAPHICS_ADDR_PORT      0x3CE
79
 
#define GRAPHICS_DATA_PORT      0x3CF
80
 
#define READ_MAP_REGISTER       0x04
81
 
 
82
 
#define INPUT_STATUS1_REGISTER  0x3DA
83
 
#define INPUT_STATUS1_VERTR_BIT 0x08
84
 
 
85
 
static inline void
86
 
wait_vretrace (void)
87
 
{
88
 
  /* Wait until there is a vertical retrace.  */
89
 
  while (! (grub_inb (INPUT_STATUS1_REGISTER) & INPUT_STATUS1_VERTR_BIT));
90
 
}
91
 
 
92
 
/* Get Map Mask Register.  */
93
 
static unsigned char
94
 
get_map_mask (void)
95
 
{
96
 
  unsigned char old_addr;
97
 
  unsigned char old_data;
98
 
 
99
 
  old_addr = grub_inb (SEQUENCER_ADDR_PORT);
100
 
  grub_outb (MAP_MASK_REGISTER, SEQUENCER_ADDR_PORT);
101
 
 
102
 
  old_data = grub_inb (SEQUENCER_DATA_PORT);
103
 
 
104
 
  grub_outb (old_addr, SEQUENCER_ADDR_PORT);
105
 
 
106
 
  return old_data;
107
 
}
108
 
 
109
 
/* Set Map Mask Register.  */
110
 
static void
111
 
set_map_mask (unsigned char mask)
112
 
{
113
 
  unsigned char old_addr;
114
 
 
115
 
  old_addr = grub_inb (SEQUENCER_ADDR_PORT);
116
 
  grub_outb (MAP_MASK_REGISTER, SEQUENCER_ADDR_PORT);
117
 
 
118
 
  grub_outb (mask, SEQUENCER_DATA_PORT);
119
 
 
120
 
  grub_outb (old_addr, SEQUENCER_ADDR_PORT);
121
 
}
122
 
 
123
 
/* Set Read Map Register.  */
124
 
static void
125
 
set_read_map (unsigned char map)
126
 
{
127
 
  unsigned char old_addr;
128
 
 
129
 
  old_addr = grub_inb (GRAPHICS_ADDR_PORT);
130
 
 
131
 
  grub_outb (READ_MAP_REGISTER, GRAPHICS_ADDR_PORT);
132
 
  grub_outb (map, GRAPHICS_DATA_PORT);
133
 
 
134
 
  grub_outb (old_addr, GRAPHICS_ADDR_PORT);
135
 
}
136
 
 
137
 
/* Set start address.  */
138
 
static void
139
 
set_start_address (unsigned int start)
140
 
{
141
 
  unsigned char old_addr;
142
 
 
143
 
  old_addr = grub_inb (CRTC_ADDR_PORT);
144
 
 
145
 
  grub_outb (START_ADDR_LOW_REGISTER, CRTC_ADDR_PORT);
146
 
  grub_outb (start & 0xFF, CRTC_DATA_PORT);
147
 
 
148
 
  grub_outb (START_ADDR_HIGH_REGISTER, CRTC_ADDR_PORT);
149
 
  grub_outb (start >> 8, CRTC_DATA_PORT);
150
 
 
151
 
  grub_outb (old_addr, CRTC_ADDR_PORT);
152
 
}
153
 
 
154
 
static grub_err_t
155
 
grub_vga_mod_init (void)
156
 
{
157
 
  text_mode = grub_vga_set_mode (0x10);
158
 
  cursor_state = 1;
159
 
  fg_color = DEFAULT_FG_COLOR;
160
 
  bg_color = DEFAULT_BG_COLOR;
161
 
  saved_map_mask = get_map_mask ();
162
 
  set_map_mask (0x0f);
163
 
  set_start_address (PAGE_OFFSET (page));
164
 
  font = grub_font_get ("");  /* Choose any font, for now. */
165
 
  if (!font)
166
 
    return grub_error (GRUB_ERR_BAD_FONT, "no font loaded");
167
 
 
168
 
  return GRUB_ERR_NONE;
169
 
}
170
 
 
171
 
static grub_err_t
172
 
grub_vga_mod_fini (void)
173
 
{
174
 
  set_map_mask (saved_map_mask);
175
 
  grub_vga_set_mode (text_mode);
176
 
  return GRUB_ERR_NONE;
177
 
}
178
 
 
179
 
static int
180
 
check_vga_mem (void *p)
181
 
{
182
 
  return (p >= (void *) (VGA_MEM + PAGE_OFFSET (page))
183
 
          && p <= (void *) (VGA_MEM + PAGE_OFFSET (page)
184
 
                            + VGA_WIDTH * VGA_HEIGHT / 8));
185
 
}
186
 
 
187
 
static void
188
 
write_char (void)
189
 
{
190
 
  struct colored_char *p = text_buf + xpos + ypos * TEXT_WIDTH;
191
 
  struct grub_font_glyph *glyph;
192
 
  unsigned char *mem_base;
193
 
  unsigned plane;
194
 
 
195
 
  mem_base = (VGA_MEM + xpos +
196
 
              ypos * CHAR_HEIGHT * TEXT_WIDTH + PAGE_OFFSET (page)) - p->index;
197
 
  p -= p->index;
198
 
 
199
 
  /* Get glyph for character.  */
200
 
  glyph = grub_font_get_glyph (font, p->code);
201
 
 
202
 
  for (plane = 0x01; plane <= 0x08; plane <<= 1)
203
 
    {
204
 
      unsigned y;
205
 
      unsigned offset;
206
 
      unsigned char *mem;
207
 
 
208
 
      set_map_mask (plane);
209
 
 
210
 
      for (y = 0, offset = 0, mem = mem_base;
211
 
           y < CHAR_HEIGHT;
212
 
           y++, mem += TEXT_WIDTH)
213
 
        {
214
 
          /* TODO Re-implement glyph drawing for vga module.  */
215
 
#if 0
216
 
          unsigned i;
217
 
 
218
 
          unsigned char_width = 1; /* TODO Figure out wide characters.  */
219
 
          for (i = 0; i < char_width && offset < 32; i++)
220
 
            {
221
 
              unsigned char fg_mask, bg_mask;
222
 
 
223
 
              fg_mask = (p->fg_color & plane) ? glyph->bitmap[offset] : 0;
224
 
              bg_mask = (p->bg_color & plane) ? ~(glyph->bitmap[offset]) : 0;
225
 
              offset++;
226
 
 
227
 
              if (check_vga_mem (mem + i))
228
 
                mem[i] = (fg_mask | bg_mask);
229
 
            }
230
 
#endif /* 0 */
231
 
        }
232
 
    }
233
 
 
234
 
  set_map_mask (0x0f);
235
 
}
236
 
 
237
 
static void
238
 
write_cursor (void)
239
 
{
240
 
  unsigned char *mem = (VGA_MEM + PAGE_OFFSET (page) + xpos
241
 
                        + (ypos * CHAR_HEIGHT + CHAR_HEIGHT - 3) * TEXT_WIDTH);
242
 
  if (check_vga_mem (mem))
243
 
    *mem = 0xff;
244
 
 
245
 
  mem += TEXT_WIDTH;
246
 
  if (check_vga_mem (mem))
247
 
    *mem = 0xff;
248
 
}
249
 
 
250
 
static void
251
 
scroll_up (void)
252
 
{
253
 
  unsigned i;
254
 
  unsigned plane;
255
 
 
256
 
  /* Do all the work in the other page.  */
257
 
  grub_memmove (text_buf, text_buf + TEXT_WIDTH,
258
 
                sizeof (struct colored_char) * TEXT_WIDTH * (TEXT_HEIGHT - 1));
259
 
 
260
 
  for (i = TEXT_WIDTH * (TEXT_HEIGHT - 1); i < TEXT_WIDTH * TEXT_HEIGHT; i++)
261
 
    {
262
 
      text_buf[i].code = ' ';
263
 
      text_buf[i].fg_color = 0;
264
 
      text_buf[i].bg_color = 0;
265
 
      text_buf[i].width = 0;
266
 
      text_buf[i].index = 0;
267
 
    }
268
 
 
269
 
  for (plane = 1; plane <= 4; plane++)
270
 
    {
271
 
      set_read_map (plane);
272
 
      set_map_mask (1 << plane);
273
 
      grub_memmove (VGA_MEM + PAGE_OFFSET (1 - page), VGA_MEM
274
 
                    + PAGE_OFFSET (page) + VGA_WIDTH * CHAR_HEIGHT / 8,
275
 
                    VGA_WIDTH * (VGA_HEIGHT - CHAR_HEIGHT) / 8);
276
 
    }
277
 
 
278
 
  set_map_mask (0x0f);
279
 
  grub_memset (VGA_MEM + PAGE_OFFSET (1 - page)
280
 
               + VGA_WIDTH * (VGA_HEIGHT - CHAR_HEIGHT) / 8, 0,
281
 
               VGA_WIDTH * CHAR_HEIGHT / 8);
282
 
 
283
 
  /* Activate the other page.  */
284
 
  page = 1 - page;
285
 
  wait_vretrace ();
286
 
  set_start_address (PAGE_OFFSET (page));
287
 
}
288
 
 
289
 
static void
290
 
grub_vga_putchar (grub_uint32_t c)
291
 
{
292
 
#if DEBUG_VGA
293
 
  static int show = 1;
294
 
#endif
295
 
 
296
 
  if (c == '\a')
297
 
    /* FIXME */
298
 
    return;
299
 
 
300
 
  if (c == '\b' || c == '\n' || c == '\r')
301
 
    {
302
 
      /* Erase current cursor, if any.  */
303
 
      if (cursor_state)
304
 
        write_char ();
305
 
 
306
 
      switch (c)
307
 
        {
308
 
        case '\b':
309
 
          if (xpos > 0)
310
 
            xpos--;
311
 
          break;
312
 
 
313
 
        case '\n':
314
 
          if (ypos >= TEXT_HEIGHT - 1)
315
 
            scroll_up ();
316
 
          else
317
 
            ypos++;
318
 
          break;
319
 
 
320
 
        case '\r':
321
 
          xpos = 0;
322
 
          break;
323
 
        }
324
 
 
325
 
      if (cursor_state)
326
 
        write_cursor ();
327
 
    }
328
 
  else
329
 
    {
330
 
      struct grub_font_glyph *glyph;
331
 
      struct colored_char *p;
332
 
      unsigned char_width = 1;
333
 
 
334
 
      glyph = grub_font_get_glyph(font, c);
335
 
 
336
 
      if (xpos + char_width > TEXT_WIDTH)
337
 
        grub_putchar ('\n');
338
 
 
339
 
      p = text_buf + xpos + ypos * TEXT_WIDTH;
340
 
      p->code = c;
341
 
      p->fg_color = fg_color;
342
 
      p->bg_color = bg_color;
343
 
      p->width = char_width - 1;
344
 
      p->index = 0;
345
 
 
346
 
      if (char_width > 1)
347
 
        {
348
 
          unsigned i;
349
 
 
350
 
          for (i = 1; i < char_width; i++)
351
 
            {
352
 
              p[i].code = ' ';
353
 
              p[i].width = char_width - 1;
354
 
              p[i].index = i;
355
 
            }
356
 
        }
357
 
 
358
 
      write_char ();
359
 
 
360
 
      xpos += char_width;
361
 
      if (xpos >= TEXT_WIDTH)
362
 
        {
363
 
          xpos = 0;
364
 
 
365
 
          if (ypos >= TEXT_HEIGHT - 1)
366
 
            scroll_up ();
367
 
          else
368
 
            ypos++;
369
 
        }
370
 
 
371
 
      if (cursor_state)
372
 
        write_cursor ();
373
 
    }
374
 
 
375
 
#if DEBUG_VGA
376
 
  if (show)
377
 
    {
378
 
      grub_uint16_t pos = grub_getxy ();
379
 
 
380
 
      show = 0;
381
 
      grub_gotoxy (0, 0);
382
 
      grub_printf ("[%u:%u]", (unsigned) (pos >> 8), (unsigned) (pos & 0xff));
383
 
      grub_gotoxy (pos >> 8, pos & 0xff);
384
 
      show = 1;
385
 
    }
386
 
#endif
387
 
}
388
 
 
389
 
static grub_ssize_t
390
 
grub_vga_getcharwidth (grub_uint32_t c)
391
 
{
392
 
#if 0
393
 
  struct grub_font_glyph glyph;
394
 
 
395
 
  glyph = grub_font_get_glyph (c);
396
 
 
397
 
  return glyph.char_width;
398
 
#else
399
 
  (void) c;   /* Prevent warning.  */
400
 
  return 1;   /* TODO Fix wide characters?  */
401
 
#endif
402
 
}
403
 
 
404
 
static grub_uint16_t
405
 
grub_vga_getwh (void)
406
 
{
407
 
  return (TEXT_WIDTH << 8) | TEXT_HEIGHT;
408
 
}
409
 
 
410
 
static grub_uint16_t
411
 
grub_vga_getxy (void)
412
 
{
413
 
  return ((xpos << 8) | ypos);
414
 
}
415
 
 
416
 
static void
417
 
grub_vga_gotoxy (grub_uint8_t x, grub_uint8_t y)
418
 
{
419
 
  if (x >= TEXT_WIDTH || y >= TEXT_HEIGHT)
420
 
    {
421
 
      grub_error (GRUB_ERR_OUT_OF_RANGE, "invalid point (%u,%u)",
422
 
                  (unsigned) x, (unsigned) y);
423
 
      return;
424
 
    }
425
 
 
426
 
  if (cursor_state)
427
 
    write_char ();
428
 
 
429
 
  xpos = x;
430
 
  ypos = y;
431
 
 
432
 
  if (cursor_state)
433
 
    write_cursor ();
434
 
}
435
 
 
436
 
static void
437
 
grub_vga_cls (void)
438
 
{
439
 
  unsigned i;
440
 
 
441
 
  wait_vretrace ();
442
 
  for (i = 0; i < TEXT_WIDTH * TEXT_HEIGHT; i++)
443
 
    {
444
 
      text_buf[i].code = ' ';
445
 
      text_buf[i].fg_color = 0;
446
 
      text_buf[i].bg_color = 0;
447
 
      text_buf[i].width = 0;
448
 
      text_buf[i].index = 0;
449
 
    }
450
 
 
451
 
  grub_memset (VGA_MEM + PAGE_OFFSET (page), 0, VGA_WIDTH * VGA_HEIGHT / 8);
452
 
 
453
 
  xpos = ypos = 0;
454
 
}
455
 
 
456
 
static void
457
 
grub_vga_setcolorstate (grub_term_color_state state)
458
 
{
459
 
  switch (state)
460
 
    {
461
 
    case GRUB_TERM_COLOR_STANDARD:
462
 
    case GRUB_TERM_COLOR_NORMAL:
463
 
      fg_color = DEFAULT_FG_COLOR;
464
 
      bg_color = DEFAULT_BG_COLOR;
465
 
      break;
466
 
    case GRUB_TERM_COLOR_HIGHLIGHT:
467
 
      fg_color = DEFAULT_BG_COLOR;
468
 
      bg_color = DEFAULT_FG_COLOR;
469
 
      break;
470
 
    default:
471
 
      break;
472
 
    }
473
 
}
474
 
 
475
 
static void
476
 
grub_vga_setcursor (int on)
477
 
{
478
 
  if (cursor_state != on)
479
 
    {
480
 
      if (cursor_state)
481
 
        write_char ();
482
 
      else
483
 
        write_cursor ();
484
 
 
485
 
      cursor_state = on;
486
 
    }
487
 
}
488
 
 
489
 
static struct grub_term_output grub_vga_term =
490
 
  {
491
 
    .name = "vga",
492
 
    .init = grub_vga_mod_init,
493
 
    .fini = grub_vga_mod_fini,
494
 
    .putchar = grub_vga_putchar,
495
 
    .getcharwidth = grub_vga_getcharwidth,
496
 
    .getwh = grub_vga_getwh,
497
 
    .getxy = grub_vga_getxy,
498
 
    .gotoxy = grub_vga_gotoxy,
499
 
    .cls = grub_vga_cls,
500
 
    .setcolorstate = grub_vga_setcolorstate,
501
 
    .setcursor = grub_vga_setcursor,
502
 
    .flags = 0,
503
 
  };
504
 
 
505
 
GRUB_MOD_INIT(vga)
506
 
{
507
 
  grub_term_register_output ("vga", &grub_vga_term);
508
 
}
509
 
 
510
 
GRUB_MOD_FINI(vga)
511
 
{
512
 
  grub_term_unregister_output (&grub_vga_term);
513
 
}