| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
#include <libpayload.h> |
| 30 |
#include <video_console.h> |
| 31 |
|
| 32 |
struct video_console vga_video_console; |
| 33 |
|
| 34 |
#define VGA_COLOR_WHITE 7 |
| 35 |
|
| 36 |
#define CRTC_INDEX 0x3d4 |
| 37 |
#define CRTC_DATA 0x3d5 |
| 38 |
|
| 39 |
#define VIDEO(_r, _c)\ |
| 40 |
((u16 *) (phys_to_virt(0xB8000) + ((_r) * (vga_video_console.columns * 2)) + ((_c) * 2))) |
| 41 |
|
| 42 |
static u8 crtc_read(u8 index) |
| 43 |
{ |
| 44 |
outb(index, CRTC_INDEX); |
| 45 |
return inb(CRTC_DATA); |
| 46 |
} |
| 47 |
|
| 48 |
static void crtc_write(u8 data, u8 index) |
| 49 |
{ |
| 50 |
outb(index, CRTC_INDEX); |
| 51 |
outb(data, CRTC_DATA); |
| 52 |
} |
| 53 |
|
| 54 |
static void vga_get_cursor(unsigned int *x, unsigned int *y, unsigned int *en) |
| 55 |
{ |
| 56 |
unsigned int addr; |
| 57 |
addr = ((unsigned int) crtc_read(0x0E)) << 8; |
| 58 |
addr += crtc_read(0x0F); |
| 59 |
|
| 60 |
*x = addr % vga_video_console.columns; |
| 61 |
*y = addr / vga_video_console.columns; |
| 62 |
|
| 63 |
*en = !(crtc_read(0x0A) & (1 << 5)); |
| 64 |
} |
| 65 |
|
| 66 |
static void vga_set_cursor(unsigned int x, unsigned int y) |
| 67 |
{ |
| 68 |
unsigned int addr; |
| 69 |
|
| 70 |
addr = x + (vga_video_console.columns * y); |
| 71 |
crtc_write(addr >> 8, 0x0E); |
| 72 |
crtc_write(addr, 0x0F); |
| 73 |
} |
| 74 |
|
| 75 |
static void vga_enable_cursor(int state) |
| 76 |
{ |
| 77 |
unsigned char tmp = crtc_read(0x0a); |
| 78 |
|
| 79 |
if (state == 0) |
| 80 |
tmp |= (1 << 5); |
| 81 |
|
| 82 |
else |
| 83 |
tmp &= ~(1 << 5); |
| 84 |
|
| 85 |
crtc_write(tmp, 0x0a); |
| 86 |
} |
| 87 |
|
| 88 |
static void vga_clear_line(u8 row, u8 ch, u8 attr) |
| 89 |
{ |
| 90 |
int col; |
| 91 |
u16 *ptr = VIDEO(row, 0); |
| 92 |
|
| 93 |
for(col = 0; col < vga_video_console.columns; col++) |
| 94 |
ptr[col] = ((attr & 0xFF) << 8) | (ch & 0xFF); |
| 95 |
} |
| 96 |
|
| 97 |
static void vga_scroll_up(void) |
| 98 |
{ |
| 99 |
u16 *src = VIDEO(1,0); |
| 100 |
u16 *dst = VIDEO(0,0); |
| 101 |
int i; |
| 102 |
|
| 103 |
for(i = 0; i < (vga_video_console.rows - 1) * vga_video_console.columns; i++) |
| 104 |
*dst++ = *src++; |
| 105 |
|
| 106 |
vga_clear_line(vga_video_console.rows - 1, ' ', VGA_COLOR_WHITE); |
| 107 |
} |
| 108 |
|
| 109 |
static void vga_fill(u8 ch, u8 attr) |
| 110 |
{ |
| 111 |
u8 row; |
| 112 |
for(row = 0; row < vga_video_console.rows; row++) |
| 113 |
vga_clear_line(row, ch, attr); |
| 114 |
} |
| 115 |
|
| 116 |
static void vga_clear(void) |
| 117 |
{ |
| 118 |
vga_fill(' ', VGA_COLOR_WHITE); |
| 119 |
} |
| 120 |
|
| 121 |
static void vga_putc(u8 row, u8 col, unsigned int c) |
| 122 |
{ |
| 123 |
u16 *ptr = VIDEO(row, col); |
| 124 |
*ptr = (u16) (c & 0xFFFF); |
| 125 |
} |
| 126 |
|
| 127 |
static void vga_init_cursor(void) |
| 128 |
{ |
| 129 |
u8 val; |
| 130 |
|
| 131 |
#define CURSOR_MSL 0x09 |
| 132 |
#define CURSOR_START 0x0A |
| 133 |
#define CURSOR_END 0x0B |
| 134 |
|
| 135 |
val = crtc_read(CURSOR_MSL) & 0x1f; |
| 136 |
crtc_write(0, CURSOR_START); |
| 137 |
crtc_write(val - 2, CURSOR_END); |
| 138 |
} |
| 139 |
|
| 140 |
static int vga_init(void) |
| 141 |
{ |
| 142 |
vga_init_cursor(); |
| 143 |
return 0; |
| 144 |
} |
| 145 |
|
| 146 |
struct video_console vga_video_console = { |
| 147 |
.init = vga_init, |
| 148 |
.putc = vga_putc, |
| 149 |
.clear = vga_clear, |
| 150 |
.scroll_up = vga_scroll_up, |
| 151 |
|
| 152 |
.get_cursor = vga_get_cursor, |
| 153 |
.set_cursor = vga_set_cursor, |
| 154 |
.enable_cursor = vga_enable_cursor, |
| 155 |
|
| 156 |
.columns = 80, |
| 157 |
.rows = 25 |
| 158 |
}; |