-
Notifications
You must be signed in to change notification settings - Fork 0
/
gfxboot_grub.c
511 lines (373 loc) · 11.9 KB
/
gfxboot_grub.c
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
#include <grub/types.h>
#include <grub/misc.h>
#include <grub/mm.h>
#include <grub/err.h>
#include <grub/dl.h>
#include <grub/command.h>
#include <grub/video.h>
#include <grub/gfxterm.h>
#include <grub/term.h>
#include <grub/env.h>
#include <grub/normal.h>
#include <grub/menu.h>
#include <grub/menu_viewer.h>
#include <grub/time.h>
#include <grub/cpu/io.h>
#include <gfxboot/gfxboot.h>
GRUB_MOD_LICENSE ("GPLv3+");
#define GFX_MEMORY_IN_MIB 16
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
gfxboot_data_t *gfxboot_data;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
static void serial_init(void);
static void serial_putc(int key);
static void grub_gfxboot_page_0(void);
static grub_err_t grub_gfxboot_init(int entry, grub_menu_t menu, int nested);
static int grub_gfxboot_process_key(int *key);
static void grub_gfxboot_set_chosen_entry(int entry, void *data);
static void grub_gfxboot_print_timeout(int timeout, void *data);
static void grub_gfxboot_clear_timeout(void *data);
static void grub_gfxboot_data_free(void);
static void grub_gfxboot_fini(void *data);
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void serial_init()
{
int x;
unsigned port = 0x3f8;
gfxboot_data->serial.port = port;
// DLAB = 1
grub_outb(0x83, port + 3);
// set to 115200 baud
grub_outb(1, port + 0);
grub_outb(0, port + 1);
// DLAB = 0, 8 bits, no parity
grub_outb(0x03, port + 3);
x = grub_inb(port + 3);
// gfxboot_log("x = 0x%02x\n", x);
if(x == 0xff) {
gfxboot_data->serial.port = 0;
return;
}
// IRQ disable
grub_outb(0x00, port + 1);
// FIFO enable
grub_outb(0x01, port + 2);
// set terminal colors to normal, looks better
gfxboot_log("\033[00m");
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void serial_putc(int key)
{
unsigned cnt = 1 << 20;
if(!gfxboot_data || !gfxboot_data->serial.port) return;
// wait until ready, but not indefinitely...
while(!(grub_inb(gfxboot_data->serial.port + 5) & 0x20) && cnt--);
grub_outb(key, gfxboot_data->serial.port);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Print to serial line and/or console.
//
// dst - bitmask; bit 0: serial, bit 1: console
//
int gfxboot_printf(int dst, const char *format, ...)
{
char *s;
int log_level_serial = (signed char) ((dst >> 8) & 0xff);
int log_level_console = (signed char) ((dst >> 16) & 0xff);
if(!format) return 0;
va_list args;
va_start(args, format);
s = grub_xvasprintf(format, args);
va_end(args);
if(
(dst & 1) &&
gfxboot_data &&
log_level_serial <= gfxboot_data->vm.debug.log_level_serial
) {
char *t = s;
while(*t) {
if(*t == '\n') serial_putc('\r');
serial_putc(*t++);
}
}
if(
(dst & 2) &&
gfxboot_data &&
log_level_console <= gfxboot_data->vm.debug.log_level_console
) {
char *t = s;
while(*t) {
if(*t == '\n') gfx_console_putc('\r', 1);
gfx_console_putc(*t++, 1);
}
}
grub_free(s);
return grub_strlen(s);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Print to buffer.
//
int gfxboot_asprintf(char **str, const char *format, ...)
{
char *s;
va_list args;
va_start(args, format);
*str = s = grub_xvasprintf(format, args);
va_end(args);
return s ? (int) grub_strlen(s) : -1;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Print to static buffer.
//
int gfxboot_snprintf(char *str, unsigned size, const char *format, ...)
{
int len;
va_list args;
va_start(args, format);
len = grub_vsnprintf(str, size, format, args);
va_end(args);
return len;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void grub_gfxboot_page_0()
{
void *fb;
grub_video_get_raw_info(NULL, &fb);
if(fb != gfxboot_data->screen.real.ptr) grub_video_swap_buffers();
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
grub_err_t grub_gfxboot_init(int entry, grub_menu_t menu, int nested)
{
struct grub_video_mode_info mode_info;
struct grub_menu_viewer *instance;
grub_err_t err = GRUB_ERR_NONE;
void *fb0 = NULL, *fb1 = NULL;
int i;
grub_gfxboot_data_free();
gfxboot_data = grub_zalloc(sizeof *gfxboot_data);
if(!gfxboot_data) return grub_errno;
instance = grub_zalloc(sizeof *instance);
if(!instance) return grub_errno;
// don't log to console
gfxboot_data->vm.debug.log_level_console = -1;
gfxboot_data->menu.entry = entry;
gfxboot_data->menu.nested = nested;
gfxboot_data->menu.timeout.max = grub_menu_get_timeout();
if(menu->size > 0) {
grub_menu_entry_t me;
gfxboot_data->menu.size = menu->size;
gfxboot_data->menu.entries = grub_zalloc(menu->size * sizeof *gfxboot_data->menu.entries);
if(!gfxboot_data->menu.entries) return grub_errno;
for(i = 0, me = menu->entry_list; me && i < menu->size; me = me->next, i++) {
gfxboot_data->menu.entries[i].title = me->title;
}
}
err = grub_video_get_raw_info(&mode_info, &fb0);
if(err) return err;
grub_video_swap_buffers();
grub_video_get_raw_info(NULL, &fb1);
if(fb1 == NULL || fb0 <= fb1) {
grub_video_swap_buffers();
gfxboot_data->screen.real.ptr = fb0;
}
else {
gfxboot_data->screen.real.ptr = fb1;
}
if(!gfxboot_data->screen.real.ptr) return GRUB_ERR_BAD_DEVICE;
gfxboot_data->screen.real.width = mode_info.width;
gfxboot_data->screen.real.height = mode_info.height;
gfxboot_data->screen.real.bytes_per_line = mode_info.pitch;
gfxboot_data->screen.real.bytes_per_pixel = mode_info.bytes_per_pixel;
gfxboot_data->screen.real.bits_per_pixel = mode_info.bpp;
gfxboot_data->screen.real.red.pos = mode_info.red_field_pos;
gfxboot_data->screen.real.red.size = mode_info.red_mask_size;
gfxboot_data->screen.real.green.pos = mode_info.green_field_pos;
gfxboot_data->screen.real.green.size = mode_info.green_mask_size;
gfxboot_data->screen.real.blue.pos = mode_info.blue_field_pos;
gfxboot_data->screen.real.blue.size = mode_info.blue_mask_size;
gfxboot_data->screen.real.res.pos = mode_info.reserved_field_pos;
gfxboot_data->screen.real.res.size = mode_info.reserved_mask_size;
grub_video_set_viewport(0, 0, gfxboot_data->screen.real.width, gfxboot_data->screen.real.height);
#if 0
// setup a commandline terminal window
// cf. gfxmenu/init_terminal()
grub_gfxterm_set_window(
GRUB_VIDEO_RENDER_TARGET_DISPLAY,
100, 100,
300, 200,
0,
grub_font_get(""),
0
);
#endif
instance->data = NULL;
instance->set_chosen_entry = grub_gfxboot_set_chosen_entry;
instance->print_timeout = grub_gfxboot_print_timeout;
instance->clear_timeout = grub_gfxboot_clear_timeout;
instance->process_key = grub_gfxboot_process_key;
instance->fini = grub_gfxboot_fini;
serial_init();
gfxboot_data->vm.mem.size = GFX_MEMORY_IN_MIB * (1 << 20);
gfxboot_data->vm.mem.ptr = grub_zalloc(gfxboot_data->vm.mem.size);
if(!gfxboot_data->vm.mem.ptr) return grub_errno;
if(grub_video_adapter_active) {
gfxboot_log("adapter name: %s\n", grub_video_adapter_active->name);
}
gfxboot_log(
"video mode info:\n type = 0x%x\n fb0 = %p, fb1 = %p\n",
mode_info.mode_type, fb0, fb1
);
if(gfxboot_init(1)) {
err = GRUB_ERR_MENU;
}
else {
grub_menu_register_viewer(instance);
}
return err;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
int grub_gfxboot_process_key(int *key)
{
int action;
int key2;
grub_gfxboot_page_0();
if(!gfxboot_data || !key) return 0;
key2 = *key;
if((key2 & GRUB_TERM_CTRL)) {
key2 &= 0x1f;
}
gfxboot_debug(2, 2, "grub_gfxboot_process_key: key = 0x%x '%c'\n", key2, key2 >= ' ' ? key2 : ' ');
if(gfxboot_data->menu.timeout.current > 0) {
grub_env_unset("timeout");
grub_env_unset("fallback");
grub_gfxboot_clear_timeout(NULL);
}
action = gfxboot_process_key(key2);
gfxboot_debug(1, 2, "grub_gfxboot_process_key: action = %d.0x%02x\n", action >> 8, action & 0xff);
*key = 0;
return action;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void grub_gfxboot_set_chosen_entry(int entry, void *data __attribute__ ((unused)))
{
gfxboot_data->menu.entry = entry;
gfxboot_log("set_chosen_entry: %d\n", entry);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void grub_gfxboot_print_timeout(int timeout, void *data __attribute__ ((unused)))
{
gfxboot_data->menu.timeout.current = timeout;
gfxboot_timeout();
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void grub_gfxboot_clear_timeout(void *data __attribute__ ((unused)))
{
gfxboot_data->menu.timeout.current = 0;
gfxboot_timeout();
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
int gfxboot_sys_read_file(char *name, void **buf)
{
int size = -1;
char *tmp = NULL;
*buf = NULL;
grub_errno = 0;
if(!name) return size;
if(*name != '/' && *name != '(') {
const char *prefix = grub_env_get("prefix");
if(!prefix) prefix = "";
int prefix_len = grub_strlen(prefix);
int name_len = grub_strlen(name);
tmp = grub_zalloc(prefix_len + name_len + 2);
if(!tmp) return size;
gfx_memcpy(tmp, prefix, prefix_len);
gfx_memcpy(tmp + prefix_len, "/", 1);
gfx_memcpy(tmp + prefix_len + 1, name, name_len);
name = tmp;
}
grub_file_t file = grub_file_open(name, GRUB_FILE_TYPE_NONE);
gfxboot_log("open(%s) = %p\n", name, file);
if(file) {
if(file->size == 0) {
size = 0;
}
else if(file->size > 0) {
*buf = grub_zalloc(file->size);
if(*buf) {
size = grub_file_read(file, *buf, file->size);
if(size != (int) file->size) {
grub_free(*buf);
*buf = NULL;
size = -1;
}
}
}
grub_file_close(file);
}
gfxboot_log("read(%s) = %d\n", name, size);
grub_free(tmp);
return size;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void gfxboot_sys_free(void *ptr)
{
if(ptr) grub_free(ptr);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
unsigned long gfxboot_sys_strlen(const char *s)
{
return grub_strlen(s);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
int gfxboot_sys_strcmp(const char *s1, const char *s2)
{
return grub_strcmp(s1, s2);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
long int gfxboot_sys_strtol(const char *nptr, char **endptr, int base)
{
return grub_strtol(nptr, (const char ** const restrict) endptr, base);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void gfxboot_screen_update(area_t area __attribute__ ((unused)))
{
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
int gfxboot_getkey()
{
return grub_getkey();
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void grub_gfxboot_data_free()
{
if(gfxboot_data) {
grub_free(gfxboot_data->menu.entries);
grub_free(gfxboot_data->vm.mem.ptr);
grub_free(gfxboot_data);
}
gfxboot_data = NULL;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void grub_gfxboot_fini(void *data __attribute__ ((unused)))
{
gfxboot_log("grub_gfxboot_fini %p\n", gfxboot_data);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
GRUB_MOD_INIT(gfxboot)
{
struct grub_term_output *term;
FOR_ACTIVE_TERM_OUTPUTS(term) {
if(grub_gfxmenu_try_hook && term->fullscreen) {
term->fullscreen();
break;
}
}
grub_gfxmenu_try_hook = grub_gfxboot_init;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
GRUB_MOD_FINI(gfxboot)
{
gfxboot_log("gfxboot fini %p\n", gfxboot_data);
grub_gfxboot_data_free();
grub_gfxmenu_try_hook = NULL;
}