]> git.proxmox.com Git - grub2.git/blob - term/i386/pc/vga_text.c
ec3c0f56534394b53b9a0d3372a8f8d240a6b995
[grub2.git] / term / i386 / pc / vga_text.c
1 /*
2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 2007 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 #include <grub/machine/console.h>
20 #include <grub/cpu/io.h>
21 #include <grub/types.h>
22
23 #define COLS 80
24 #define ROWS 25
25
26 static int grub_curr_x, grub_curr_y;
27
28 #define VGA_TEXT_SCREEN 0xb8000
29
30 #define CRTC_ADDR_PORT 0x3D4
31 #define CRTC_DATA_PORT 0x3D5
32
33 #define CRTC_CURSOR 0x0a
34 #define CRTC_CURSOR_ADDR_HIGH 0x0e
35 #define CRTC_CURSOR_ADDR_LOW 0x0f
36
37 #define CRTC_CURSOR_DISABLE (1 << 5)
38
39 static void
40 screen_write_char (int x, int y, short c)
41 {
42 ((short *) VGA_TEXT_SCREEN)[y * COLS + x] = c;
43 }
44
45 static short
46 screen_read_char (int x, int y)
47 {
48 return ((short *) VGA_TEXT_SCREEN)[y * COLS + x];
49 }
50
51 static void
52 update_cursor (void)
53 {
54 unsigned int pos = grub_curr_y * COLS + grub_curr_x;
55 grub_outb (CRTC_CURSOR_ADDR_HIGH, CRTC_ADDR_PORT);
56 grub_outb (pos >> 8, CRTC_DATA_PORT);
57 grub_outb (CRTC_CURSOR_ADDR_LOW, CRTC_ADDR_PORT);
58 grub_outb (pos & 0xFF, CRTC_DATA_PORT);
59 }
60
61 static void
62 inc_y (void)
63 {
64 grub_curr_x = 0;
65 if (grub_curr_y < ROWS - 1)
66 grub_curr_y++;
67 else
68 {
69 int x, y;
70 for (y = 0; y < ROWS; y++)
71 for (x = 0; x < COLS; x++)
72 screen_write_char (x, y, screen_read_char (x, y + 1));
73 }
74 }
75
76 static void
77 inc_x (void)
78 {
79 if (grub_curr_x >= COLS - 2)
80 inc_y ();
81 else
82 grub_curr_x++;
83 }
84
85 void
86 grub_console_real_putchar (int c)
87 {
88 switch (c)
89 {
90 case '\b':
91 if (grub_curr_x != 0)
92 screen_write_char (grub_curr_x--, grub_curr_y, ' ');
93 break;
94 case '\n':
95 inc_y ();
96 break;
97 case '\r':
98 grub_curr_x = 0;
99 break;
100 default:
101 screen_write_char (grub_curr_x,
102 grub_curr_y, c | (grub_console_cur_color << 8));
103 inc_x ();
104 }
105
106 update_cursor ();
107 }
108
109 grub_uint16_t
110 grub_console_getxy (void)
111 {
112 return (grub_curr_x << 8) | grub_curr_y;
113 }
114
115 void
116 grub_console_gotoxy (grub_uint8_t x, grub_uint8_t y)
117 {
118 grub_curr_x = x;
119 grub_curr_y = y;
120 update_cursor ();
121 }
122
123 void
124 grub_console_cls (void)
125 {
126 int i;
127 for (i = 0; i < ROWS * COLS; i++)
128 ((short *) VGA_TEXT_SCREEN)[i] = ' ' | (grub_console_cur_color << 8);
129 grub_console_gotoxy (0, 0);
130 }
131
132 void
133 grub_console_setcursor (int on)
134 {
135 grub_uint8_t old;
136 grub_outb (CRTC_CURSOR, CRTC_ADDR_PORT);
137 old = grub_inb (CRTC_DATA_PORT);
138 if (on)
139 grub_outb (old & ~CRTC_CURSOR_DISABLE, CRTC_DATA_PORT);
140 else
141 grub_outb (old | CRTC_CURSOR_DISABLE, CRTC_DATA_PORT);
142 }