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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
|
/*
*
* gfxboot.c
*
* A com32 module to load gfxboot graphics.
*
* Copyright (c) 2009 Steffen Winterfeldt.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation, Inc., 53 Temple Place Ste 330, Boston MA
* 02111-1307, USA; either version 2 of the License, or (at your option) any
* later version; incorporated herein by reference.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <syslinux/loadfile.h>
#include <syslinux/config.h>
#include <syslinux/linux.h>
#include <syslinux/boot.h>
#include <console.h>
#include <com32.h>
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#define MAX_CONFIG_LINE_LEN 2048
#define MAX_CMDLINE_LEN 2048
// buffer for realmode callback
// must be at least block size; can in theory be larger than 4k, but there's
// not enough space left
#define REALMODE_BUF_SIZE 4096
// gfxboot working memory in MB
#define GFX_MEMORY_SIZE 7
// read chunk size for progress bar
#define CHUNK_SIZE (64 << 10)
// callback function numbers
#define GFX_CB_INIT 0
#define GFX_CB_DONE 1
#define GFX_CB_INPUT 2
#define GFX_CB_MENU_INIT 3
#define GFX_CB_INFOBOX_INIT 4
#define GFX_CB_INFOBOX_DONE 5
#define GFX_CB_PROGRESS_INIT 6
#define GFX_CB_PROGRESS_DONE 7
#define GFX_CB_PROGRESS_UPDATE 8
#define GFX_CB_PROGRESS_LIMIT 9 // unused
#define GFX_CB_PASSWORD_INIT 10
#define GFX_CB_PASSWORD_DONE 11
// real mode code chunk, will be placed into bounce buffer
extern void realmode_callback_start, realmode_callback_end;
// gets in the way
#undef linux
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// gfxboot config data (64 bytes)
typedef struct __attribute__ ((packed)) {
uint8_t bootloader; // 0: boot loader type (0: lilo, 1: syslinux, 2: grub)
uint8_t sector_shift; // 1: sector shift
uint8_t media_type; // 2: media type (0: disk, 1: floppy, 2: cdrom)
uint8_t failsafe; // 3: turn on failsafe mode (bitmask)
// 0: SHIFT pressed
// 1: skip gfxboot
// 2: skip monitor detection
uint8_t sysconfig_size; // 4: size of sysconfig data
uint8_t boot_drive; // 5: BIOS boot drive
uint16_t callback; // 6: offset to callback handler
uint16_t bootloader_seg; // 8: code/data segment used by bootloader; must follow gfx_callback
uint16_t serial_port; // 10: syslinux initialized serial port from 'serial' option
uint32_t user_info_0; // 12: data for info box
uint32_t user_info_1; // 16: data for info box
uint32_t bios_mem_size; // 20: BIOS memory size (in bytes)
uint16_t xmem_0; // 24: extended mem area 0 (start:size in MB; 12:4 bits) - obsolete
uint16_t xmem_1; // 26: extended mem area 1 - obsolete
uint16_t xmem_2; // 28: extended mem area 2 - obsolete
uint16_t xmem_3; // 30: extended mem area 3 - obsolete
uint32_t file; // 32: start of gfx file
uint32_t archive_start; // 36: start of cpio archive
uint32_t archive_end; // 40: end of cpio archive
uint32_t mem0_start; // 44: low free memory start
uint32_t mem0_end; // 48: low free memory end
uint32_t xmem_start; // 52: extended mem start
uint32_t xmem_end; // 56: extended mem end
uint16_t features; // 60: feature flags returned by GFX_CB_INIT
// 0: GFX_CB_MENU_INIT accepts 32 bit addresses
// 1: knows about xmem_start, xmem_end
uint16_t reserved_1; // 62:
} gfx_config_t;
// gfxboot menu description (18 bytes)
typedef struct __attribute__ ((packed)) {
uint16_t entries;
char *default_entry;
char *label_list;
uint16_t label_size;
char *arg_list;
uint16_t arg_size;
} gfx_menu_t;
// menu description
typedef struct menu_s {
struct menu_s *next;
char *label;
char *kernel;
char *linux;
char *localboot;
char *initrd;
char *append;
char *ipappend;
} menu_t;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
gfx_config_t gfx_config;
gfx_menu_t gfx_menu;
menu_t *menu;
menu_t *menu_default;
struct {
uint32_t jmp_table[12];
uint16_t code_seg;
char fname_buf[64];
} gfx;
void *lowmem_buf;
unsigned lowmem_buf_size;
int timeout;
char cmdline[MAX_CMDLINE_LEN];
void *save_buf;
unsigned save_buf_size;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void show_message(char *file);
char *get_config_file_name(void);
char *skip_spaces(char *s);
char *skip_nonspaces(char *s);
void chop_line(char *s);
int read_config_file(void);
unsigned magic_ok(unsigned char *buf, unsigned *code_size);
unsigned find_file(unsigned char *buf, unsigned len, unsigned *gfx_file_start, unsigned *file_len, unsigned *code_size);
int gfx_init(char *file);
int gfx_menu_init(void);
void gfx_done(void);
int gfx_input(void);
ssize_t save_read(int fd, void *buf, size_t size);
void *load_one(char *file, ssize_t *file_size);
void boot(void);
void boot_entry(menu_t *menu_ptr, char *arg);
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
int main(int argc, char **argv)
{
int menu_index;
enum syslinux_filesystem syslinux_id;
com32sys_t r;
openconsole(&dev_stdcon_r, &dev_stdcon_w);
syslinux_id = syslinux_version()->filesystem;
lowmem_buf = __com32.cs_bounce;
lowmem_buf_size = __com32.cs_bounce_size;
r.eax.l = 0x0a; // Get Derivative-Specific Information
r.ecx.l = 9;
__intcall(0x22, &r, &r);
gfx_config.sector_shift = (uint8_t) r.ecx.l;
gfx_config.boot_drive = (uint8_t) r.edx.l;
if(syslinux_id == SYSLINUX_FS_PXELINUX) {
gfx_config.sector_shift = 11;
gfx_config.boot_drive = 0;
}
gfx_config.bootloader = 1;
gfx_config.sysconfig_size = sizeof gfx_config;
gfx_config.bootloader_seg = 0; // apparently not needed
save_buf_size = lowmem_buf_size;
save_buf = malloc(save_buf_size);
if(argc < 2) {
printf("Usage: gfxboot.c32 bootlogo_file [message_file]\n");
if(argc > 2) show_message(argv[2]);
return 0;
}
if(read_config_file()) {
printf("Error reading config file\n");
if(argc > 2) show_message(argv[2]);
return 0;
}
if(gfx_init(argv[1])) {
printf("Error setting up gfxboot\n");
if(argc > 2) show_message(argv[2]);
return 0;
}
gfx_menu_init();
for(;;) {
menu_index = gfx_input();
// abort gfx, return to text mode prompt
if(menu_index == -1) {
gfx_done();
break;
}
// does not return if it succeeds
boot();
}
if(argc > 2) show_message(argv[2]);
return 0;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void show_message(char *file)
{
int c;
FILE *f;
if(!(f = fopen(file, "r"))) return;
while((c = getc(f)) != EOF) {
if(c < ' ' && c != '\n' && c != '\t') continue;
printf("%c", c);
}
fclose(f);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
char *skip_spaces(char *s)
{
while(*s && (*s == ' ' || *s == '\t')) s++;
return s;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
char *skip_nonspaces(char *s)
{
while(*s && *s != ' ' && *s != '\t') s++;
return s;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void chop_line(char *s)
{
int i = strlen(s);
if(!i) return;
while(--i >= 0) {
if(s[i] == ' ' || s[i] == '\t' || s[i] == '\n') {
s[i] = 0;
}
else {
break;
}
}
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Read and parse syslinux config file.
//
// return:
// 0: ok, 1: error
//
int read_config_file(void)
{
FILE *f;
char *s, *t, buf[MAX_CONFIG_LINE_LEN];
unsigned u, menu_idx = 0, label_size = 0, append_size = 0;
menu_t *menu_ptr = NULL, **menu_next = &menu;
menu_default = calloc(1, sizeof *menu_default);
if(!(f = fopen(syslinux_config_file(), "r"))) return 1;
while((s = fgets(buf, sizeof buf, f))) {
chop_line(s);
s = skip_spaces(s);
if(!*s || *s == '#') continue;
t = skip_nonspaces(s);
if(*t) *t++ = 0;
t = skip_spaces(t);
if(!strcmp(s, "timeout")) {
timeout = atoi(t);
continue;
}
if(!strcmp(s, "default")) {
menu_default->label = strdup(t);
u = strlen(t);
if(u > label_size) label_size = u;
continue;
}
if(!strcmp(s, "label")) {
menu_ptr = *menu_next = calloc(1, sizeof **menu_next);
menu_next = &menu_ptr->next;
menu_idx++;
menu_ptr->label = strdup(t);
u = strlen(t);
if(u > label_size) label_size = u;
continue;
}
if(!strcmp(s, "kernel") && menu_ptr) {
menu_ptr->kernel = strdup(t);
continue;
}
if(!strcmp(s, "linux") && menu_ptr) {
menu_ptr->linux = strdup(t);
continue;
}
if(!strcmp(s, "localboot") && menu_ptr) {
menu_ptr->localboot = strdup(t);
continue;
}
if(!strcmp(s, "initrd") && menu_ptr) {
menu_ptr->initrd = strdup(t);
continue;
}
if(!strcmp(s, "append")) {
(menu_ptr ?: menu_default)->append = strdup(t);
u = strlen(t);
if(u > append_size) append_size = u;
continue;
}
if(!strcmp(s, "ipappend")) {
(menu_ptr ?: menu_default)->ipappend = strdup(t);
continue;
}
}
fclose(f);
// final '\0'
label_size++;
append_size++;
gfx_menu.entries = menu_idx;
gfx_menu.label_size = label_size;
gfx_menu.arg_size = append_size;
gfx_menu.default_entry = menu_default->label;
if(!gfx_menu.default_entry && menu) {
gfx_menu.default_entry = menu->label;
}
gfx_menu.label_list = calloc(menu_idx, label_size);
gfx_menu.arg_list = calloc(menu_idx, append_size);
for(u = 0, menu_ptr = menu; menu_ptr; menu_ptr = menu_ptr->next, u++) {
if(!menu_ptr->append) menu_ptr->append = menu_default->append;
if(!menu_ptr->ipappend) menu_ptr->ipappend = menu_default->ipappend;
if(menu_ptr->label) strcpy(gfx_menu.label_list + u * label_size, menu_ptr->label);
if(menu_ptr->append) strcpy(gfx_menu.arg_list + u * append_size, menu_ptr->append);
}
return 0;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Check header and return code start offset.
//
unsigned magic_ok(unsigned char *buf, unsigned *code_size)
{
if(
*(unsigned *) buf == 0x0b2d97f00 && // magic id
(buf[4] == 8) // version 8
) {
*code_size = *(unsigned *) (buf + 12);
return *(unsigned *) (buf + 8);
}
return 0;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Search (cpio archive) for gfx file.
//
unsigned find_file(unsigned char *buf, unsigned len, unsigned *gfx_file_start, unsigned *file_len, unsigned *code_size)
{
unsigned i, fname_len, code_start = 0;
*gfx_file_start = 0;
*code_size = 0;
if((code_start = magic_ok(buf, code_size))) return code_start;
for(i = 0; i < len;) {
if((len - i) >= 0x1a && (buf[i] + (buf[i + 1] << 8)) == 0x71c7) {
fname_len = *(unsigned short *) (buf + i + 20);
*file_len = *(unsigned short *) (buf + i + 24) + (*(unsigned short *) (buf + i + 22) << 16);
i += 26 + fname_len;
i = ((i + 1) & ~1);
if((code_start = magic_ok(buf + i, code_size))) {
*gfx_file_start = i;
return code_start;
}
i += *file_len;
i = ((i + 1) & ~1);
}
else {
break;
}
}
return code_start;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Initialize gfxboot code.
//
// return:
// 0: ok, 1: error
//
int gfx_init(char *file)
{
size_t archive_size = 0;
void *archive;
unsigned code_start, code_size, file_start, file_len, u;
com32sys_t r;
void *lowmem = lowmem_buf;
unsigned lowmem_size = lowmem_buf_size;
printf("Loading %s...\n", file);
if(loadfile(file, &archive, &archive_size)) return 1;
if(!archive_size) return 1;
// printf("%s: %d\n", file, archive_size);
gfx_config.archive_start = (uint32_t) archive;
gfx_config.archive_end = gfx_config.archive_start + archive_size;
// locate file inside cpio archive
if(!(code_start = find_file(archive, archive_size, &file_start, &file_len, &code_size))) {
printf("%s: invalid file format\n", file);
return 1;
}
#if 0
printf(
"code_start = 0x%x, code_size = 0x%x\n"
"archive_start = 0x%x, archive size = 0x%x\n"
"file_start = 0x%x, file_len = 0x%x\n",
code_start, code_size,
gfx_config.archive_start, archive_size,
file_start, file_len
);
#endif
gfx_config.file = gfx_config.archive_start + file_start;
u = &realmode_callback_end - &realmode_callback_start;
u = (u + REALMODE_BUF_SIZE + 0xf) & ~0xf;
if(u + code_size > lowmem_size) {
printf("bounce buffer too small: size %u, needed %u\n", lowmem_size, u + code_size);
return 1;
}
memcpy(lowmem + REALMODE_BUF_SIZE, &realmode_callback_start, &realmode_callback_end - &realmode_callback_start);
// fill in buffer size and location
*(uint16_t *) (lowmem + REALMODE_BUF_SIZE) = REALMODE_BUF_SIZE;
*(uint16_t *) (lowmem + REALMODE_BUF_SIZE + 2) = (uint32_t) lowmem >> 4;
gfx_config.bootloader_seg = ((uint32_t) lowmem + REALMODE_BUF_SIZE) >> 4;
gfx_config.callback = 4; // start address
lowmem += u;
lowmem_size -= u;
memcpy(lowmem, archive + file_start + code_start, code_size);
gfx_config.mem0_start = (uint32_t) lowmem + code_size;
gfx_config.mem0_end = (uint32_t) lowmem + lowmem_size;
// align a bit
gfx_config.mem0_start = (gfx_config.mem0_start + 0xf) & ~0xf;
gfx_config.xmem_start = (uint32_t) malloc(GFX_MEMORY_SIZE << 20);
if(gfx_config.xmem_start) {
gfx_config.xmem_end = gfx_config.xmem_start + (GFX_MEMORY_SIZE << 20);
}
// fake; not used anyway
gfx_config.bios_mem_size = 256 << 20;
gfx.code_seg = (uint32_t) lowmem >> 4;
for(u = 0; u < sizeof gfx.jmp_table / sizeof *gfx.jmp_table; u++) {
gfx.jmp_table[u] = (gfx.code_seg << 16) + *(uint16_t *) (lowmem + 2 * u);
}
#if 0
for(u = 0; u < sizeof gfx.jmp_table / sizeof *gfx.jmp_table; u++) {
printf("%d: 0x%08x\n", u, gfx.jmp_table[u]);
}
#endif
// we are ready to start
r.esi.l = (uint32_t) &gfx_config;
__farcall(gfx.code_seg, gfx.jmp_table[GFX_CB_INIT], &r, &r);
if((r.eflags.l & EFLAGS_CF)) {
printf("graphics initialization failed\n");
return 1;
}
if((gfx_config.features & 3) != 3) {
gfx_done();
printf("%s: boot graphics code too old, please use newer version\n", file);
return 1;
}
return 0;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
int gfx_menu_init(void)
{
com32sys_t r;
r.esi.l = (uint32_t) &gfx_menu;
__farcall(gfx.code_seg, gfx.jmp_table[GFX_CB_MENU_INIT], &r, &r);
return 0;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void gfx_done(void)
{
com32sys_t r;
__farcall(gfx.code_seg, gfx.jmp_table[GFX_CB_DONE], &r, &r);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Run gfxboot main loop.
//
// return:
// boot menu index (-1: go to text mode prompt)
//
int gfx_input(void)
{
com32sys_t r;
r.edi.l = (uint32_t) cmdline;
r.ecx.l = sizeof cmdline;
r.eax.l = timeout * 182 / 100;
timeout = 0; // use timeout only first time
__farcall(gfx.code_seg, gfx.jmp_table[GFX_CB_INPUT], &r, &r);
if((r.eflags.l & EFLAGS_CF)) r.eax.l = 1;
if(r.eax.l == 1) return -1;
return r.ebx.l;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Like read(2) but preserve bounce buffer.
//
ssize_t save_read(int fd, void *buf, size_t size)
{
ssize_t i;
memcpy(save_buf, lowmem_buf, save_buf_size);
i = read(fd, buf, size);
memcpy(lowmem_buf, save_buf, save_buf_size);
return i;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Read file and update progress bar.
//
void *load_one(char *file, ssize_t *file_size)
{
int fd;
void *buf = NULL;
struct stat sbuf;
ssize_t size = 0, cur, i;
com32sys_t r;
*file_size = 0;
if((fd = open(file, O_RDONLY)) == -1) {
printf("%s: file not found\n", file);
return buf;
}
if(!fstat(fd, &sbuf) && S_ISREG(sbuf.st_mode)) size = sbuf.st_size;
i = 0;
if(size) {
buf = malloc(size);
for(i = 1, cur = 0 ; cur < size && i > 0; cur += i) {
i = save_read(fd, buf + cur, CHUNK_SIZE);
r.eax.l = i >> gfx_config.sector_shift;
__farcall(gfx.code_seg, gfx.jmp_table[GFX_CB_PROGRESS_UPDATE], &r, &r);
}
}
else {
do {
buf = realloc(buf, size + CHUNK_SIZE);
i = save_read(fd, buf + size, CHUNK_SIZE);
size += i;
r.eax.l = i >> gfx_config.sector_shift;
__farcall(gfx.code_seg, gfx.jmp_table[GFX_CB_PROGRESS_UPDATE], &r, &r);
} while(i > 0);
}
close(fd);
if(i == -1) {
free(buf);
buf = NULL;
size = 0;
}
*file_size = size;
return buf;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Locate menu entry and boot.
//
void boot(void)
{
char *label, *arg, *s;
menu_t *menu_ptr;
label = skip_spaces(cmdline);
arg = skip_spaces(s = skip_nonspaces(label));
*s = 0;
for(menu_ptr = menu; menu_ptr; menu_ptr = menu_ptr->next) {
if(menu_ptr->label && !strcmp(menu_ptr->label, label)) break;
}
boot_entry(menu_ptr, arg);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Load & run kernel.
//
// Returns only on error.
//
void boot_entry(menu_t *menu_ptr, char *arg)
{
void *kernel, *initrd_buf;
ssize_t kernel_size = 0, initrd_size = 0;
struct initramfs *initrd = NULL;
char *file, *cmd_buf;
int fd;
struct stat sbuf;
com32sys_t r;
char *s, *s0, *t, *initrd_arg;
if(!menu_ptr) return;
if(menu_ptr->localboot) {
gfx_done();
syslinux_local_boot(atoi(arg));
return;
}
file = menu_ptr->kernel;
if(!file) file = menu_ptr->linux;
if(!file) {
gfx_done();
asprintf(&cmd_buf, "%s %s", menu_ptr->label, arg);
syslinux_run_command(cmd_buf);
return;
}
// first, load kernel
r.eax.l = 0; // kernel size in sectors
if((fd = open(file, O_RDONLY)) >= 0) {
if(!fstat(fd, &sbuf) && S_ISREG(sbuf.st_mode)) r.eax.l = sbuf.st_size >> gfx_config.sector_shift;
close(fd);
}
r.esi.l = (uint32_t) file;
__farcall(gfx.code_seg, gfx.jmp_table[GFX_CB_PROGRESS_INIT], &r, &r);
kernel = load_one(file, &kernel_size);
if(!kernel) {
gfx_done();
printf("%s: read error\n", file);
return;
}
if(kernel_size < 1024 || *(uint32_t *) (kernel + 0x202) != 0x53726448) {
// not a linux kernel
gfx_done();
asprintf(&cmd_buf, "%s %s", menu_ptr->label, arg);
syslinux_run_command(cmd_buf);
return;
}
// printf("kernel = %p, size = %d\n", kernel, kernel_size);
// parse cmdline for "initrd" option
initrd_arg = menu_ptr->initrd;
s = s0 = strdup(arg);
while(*s && strncmp(s, "initrd=", sizeof "initrd=" - 1)) {
s = skip_spaces(skip_nonspaces(s));
}
if(*s) {
s += sizeof "initrd=" - 1;
*skip_nonspaces(s) = 0;
initrd_arg = s;
}
if(initrd_arg) {
initrd = initramfs_init();
while((t = strsep(&s, ","))) {
initrd_buf = load_one(t, &initrd_size);
if(!initrd_buf) {
printf("%s: read error\n", t);
free(s0);
return;
}
initramfs_add_data(initrd, initrd_buf, initrd_size, initrd_size, 4);
// printf("initrd = %p, size = %d\n", initrd_buf, initrd_size);
}
}
free(s0);
__farcall(gfx.code_seg, gfx.jmp_table[GFX_CB_PROGRESS_DONE], &r, &r);
syslinux_boot_linux(kernel, kernel_size, initrd, arg);
}
|