File: gfxboot.c

package info (click to toggle)
syslinux 2:4.05+dfsg-6+deb7u1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 25,684 kB
  • sloc: ansic: 276,159; asm: 9,973; pascal: 9,907; perl: 3,492; makefile: 1,736; sh: 626; python: 266; xml: 39
file content (972 lines) | stat: -rw-r--r-- 24,071 bytes parent folder | download
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
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
/*
 *
 * 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 <minmax.h>
#include <ctype.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
#define LOWMEM_BUF_SIZE		65536

// 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 lowmem buffer
extern const char 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:
  uint32_t gfxboot_cwd;		// 64: if set, points to current gfxboot working directory relative
				//     to syslinux working directory
} 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;		// config entry name
  char *menu_label;	// text to show in boot menu
  char *kernel;		// name of program to load
  char *alt_kernel;	// alternative name in case user has replaced it
  char *linux;		// de facto an alias for 'kernel'
  char *localboot;	// boot from local disk
  char *initrd;		// initrd as separate line (instead of as part of 'append')
  char *append;		// kernel args
  char *ipappend;	// append special pxelinux args (see doc)
} menu_t;


// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
gfx_config_t gfx_config;
gfx_menu_t gfx_menu;

menu_t *menu;
menu_t *menu_default;
static menu_t *menu_ptr, **menu_next;

struct {
  uint32_t jmp_table[12];
  uint16_t code_seg;
  char fname_buf[64];
} gfx;

void *lowmem_buf;

int timeout;

char cmdline[MAX_CMDLINE_LEN];

// progress bar is visible
unsigned progress_active;


// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void show_message(char *file);
char *get_config_file_name(void);
char *skip_nonspaces(char *s);
void chop_line(char *s);
int read_config_file(const char *filename);
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);
void gfx_infobox(int type, char *str1, char *str2);
void gfx_progress_init(ssize_t kernel_size, char *label);
void gfx_progress_update(ssize_t size);
void gfx_progress_done(void);
void *load_one(char *file, ssize_t *file_size);
void boot(int index);
void boot_entry(menu_t *menu_ptr, char *arg);


// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
int main(int argc, char **argv)
{
  int menu_index;
  const union syslinux_derivative_info *sdi;
  char working_dir[256];

  openconsole(&dev_stdcon_r, &dev_stdcon_w);

  lowmem_buf = lmalloc(LOWMEM_BUF_SIZE);
  if (!lowmem_buf) {
    printf("Could not allocate memory.\n");
    return 1;
  }

  sdi = syslinux_derivative_info();

  gfx_config.sector_shift = sdi->disk.sector_shift;
  gfx_config.boot_drive = sdi->disk.drive_number;

  if(sdi->c.filesystem == SYSLINUX_FS_PXELINUX) {
    gfx_config.sector_shift = 11;
    gfx_config.boot_drive = 0;
  }

  gfx_config.media_type = gfx_config.boot_drive < 0x80 ? 1 : 0;

  if(sdi->c.filesystem == SYSLINUX_FS_ISOLINUX) {
    gfx_config.media_type = sdi->iso.cd_mode ? 0 : 2;
  }

  gfx_config.bootloader = 1;
  gfx_config.sysconfig_size = sizeof gfx_config;
  gfx_config.bootloader_seg = 0;	// apparently not needed

  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(getcwd(working_dir, sizeof working_dir)) {
    gfx_config.gfxboot_cwd = (uint32_t) working_dir;
  }

  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(menu_index);
  }

  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_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(const char *filename)
{
  FILE *f;
  char *s, *t, buf[MAX_CONFIG_LINE_LEN];
  unsigned u, top_level = 0, text = 0;

  if(!strcmp(filename, "~")) {
    top_level = 1;
    filename = syslinux_config_file();
    gfx_menu.entries = 0;
    gfx_menu.label_size = 0;
    gfx_menu.arg_size = 0;
    menu_ptr = NULL;
    menu_next = &menu;
    menu_default = calloc(1, sizeof *menu_default);
  }

  if(!(f = fopen(filename, "r"))) return 1;

  while((s = fgets(buf, sizeof buf, f))) {
    chop_line(s);
    s = skipspace(s);
    if(!*s || *s == '#') continue;
    t = skip_nonspaces(s);
    if(*t) *t++ = 0;
    t = skipspace(t);

    if(!strcasecmp(s, "endtext")) {
      text = 0;
      continue;
    }

    if (text)
      continue;

    if(!strcasecmp(s, "timeout")) {
      timeout = atoi(t);
      continue;
    }

    if(!strcasecmp(s, "default")) {
      menu_default->label = strdup(t);
      u = strlen(t);
      if(u > gfx_menu.label_size) gfx_menu.label_size = u;
      continue;
    }

    if(!strcasecmp(s, "label")) {
      menu_ptr = *menu_next = calloc(1, sizeof **menu_next);
      menu_next = &menu_ptr->next;
      gfx_menu.entries++;
      menu_ptr->label = menu_ptr->menu_label = strdup(t);
      u = strlen(t);
      if(u > gfx_menu.label_size) gfx_menu.label_size = u;
      continue;
    }

    if(!strcasecmp(s, "kernel") && menu_ptr) {
      menu_ptr->kernel = strdup(t);
      continue;
    }

    if(!strcasecmp(s, "linux") && menu_ptr) {
      menu_ptr->linux = strdup(t);
      continue;
    }

    if(!strcasecmp(s, "localboot") && menu_ptr) {
      menu_ptr->localboot = strdup(t);
      continue;
    }

    if(!strcasecmp(s, "initrd") && menu_ptr) {
      menu_ptr->initrd = strdup(t);
      continue;
    }

    if(!strcasecmp(s, "append")) {
      (menu_ptr ?: menu_default)->append = strdup(t);
      u = strlen(t);
      if(u > gfx_menu.arg_size) gfx_menu.arg_size = u;
      continue;
    }

    if(!strcasecmp(s, "ipappend")) {
      (menu_ptr ?: menu_default)->ipappend = strdup(t);
      continue;
    }

    if(!strcasecmp(s, "text")) {
      text = 1;
      continue;
    }

    if(!strcasecmp(s, "menu") && menu_ptr) {
      s = skipspace(t);
      t = skip_nonspaces(s);
      if(*t) *t++ = 0;
      t = skipspace(t);

      if(!strcasecmp(s, "label")) {
        menu_ptr->menu_label = strdup(t);
        u = strlen(t);
        if(u > gfx_menu.label_size) gfx_menu.label_size = u;
        continue;
      }

      if(!strcasecmp(s, "include")) {
        goto do_include;
      }
    }

    if (!strcasecmp(s, "include")) {
do_include:
      s = t;
      t = skip_nonspaces(s);
      if (*t) *t = 0;
      read_config_file(s);
    }
  }

  fclose(f);

  if (!top_level)
    return 0;

  if (gfx_menu.entries == 0) {
    printf("No LABEL keywords found.\n");
    return 1;
  }

  // final '\0'
  gfx_menu.label_size++;
  gfx_menu.arg_size++;

  // ensure we have a default entry
  if(!menu_default->label) menu_default->label = menu->label;

  if(menu_default->label) {
    for(menu_ptr = menu; menu_ptr; menu_ptr = menu_ptr->next) {
      if(!strcmp(menu_default->label, menu_ptr->label)) {
        menu_default->menu_label = menu_ptr->menu_label;
        break;
      }
    }
  }

  gfx_menu.default_entry = menu_default->menu_label;
  gfx_menu.label_list = calloc(gfx_menu.entries, gfx_menu.label_size);
  gfx_menu.arg_list = calloc(gfx_menu.entries, gfx_menu.arg_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->menu_label) strcpy(gfx_menu.label_list + u * gfx_menu.label_size, menu_ptr->menu_label);
    if(menu_ptr->append) strcpy(gfx_menu.arg_list + u * gfx_menu.arg_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;

  progress_active = 0;

  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("lowmem 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;

  gfx_progress_done();

  __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;
}


// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void gfx_infobox(int type, char *str1, char *str2)
{
  com32sys_t r;

  r.eax.l = type;
  r.esi.l = (uint32_t) str1;
  r.edi.l = (uint32_t) str2;
  __farcall(gfx.code_seg, gfx.jmp_table[GFX_CB_INFOBOX_INIT], &r, &r);
  r.edi.l = r.eax.l = 0;
  __farcall(gfx.code_seg, gfx.jmp_table[GFX_CB_INPUT], &r, &r);
  __farcall(gfx.code_seg, gfx.jmp_table[GFX_CB_INFOBOX_DONE], &r, &r);
}


// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void gfx_progress_init(ssize_t kernel_size, char *label)
{
  com32sys_t r;

  if(!progress_active) {
    r.eax.l = kernel_size >> gfx_config.sector_shift;		// in sectors
    r.esi.l = (uint32_t) label;
    __farcall(gfx.code_seg, gfx.jmp_table[GFX_CB_PROGRESS_INIT], &r, &r);
  }

  progress_active = 1;
}


// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void gfx_progress_update(ssize_t advance)
{
  com32sys_t r;

  if(progress_active) {
    r.eax.l = advance >> gfx_config.sector_shift;		// in sectors
    __farcall(gfx.code_seg, gfx.jmp_table[GFX_CB_PROGRESS_UPDATE], &r, &r);
  }
}


// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void gfx_progress_done(void)
{
  com32sys_t r;

  if(progress_active) {
    __farcall(gfx.code_seg, gfx.jmp_table[GFX_CB_PROGRESS_DONE], &r, &r);
  }

  progress_active = 0;
}


// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Read file and update progress bar.
//
void *load_one(char *file, ssize_t *file_size)
{
  int fd;
  void *buf = NULL;
  char *str;
  struct stat sbuf;
  ssize_t size = 0, cur, i;

  *file_size = 0;

  if((fd = open(file, O_RDONLY)) == -1) {
    asprintf(&str, "%s: file not found", file);
    gfx_infobox(0, str, NULL);
    free(str);
    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 = read(fd, buf + cur, min(CHUNK_SIZE, size - cur));
      if(i == -1) break;
      gfx_progress_update(i);
    }
  }
  else {
    do {
      buf = realloc(buf, size + CHUNK_SIZE);
      i = read(fd, buf + size, CHUNK_SIZE);
      if(i == -1) break;
      size += i;
      gfx_progress_update(i);
    } while(i > 0);
  }

  close(fd);

  if(i == -1) {
    asprintf(&str, "%s: read error @ %d", file, size);
    gfx_infobox(0, str, NULL);
    free(str);
    free(buf);
    buf = NULL;
    size = 0;
  }

  *file_size = size;

  return buf;
}


// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Boot menu entry.
//
// cmdline can optionally start with label string.
//
void boot(int index)
{
  char *arg, *alt_kernel;
  menu_t *menu_ptr;
  int i, label_len, menu_label_len;
  unsigned ipapp;
  const struct syslinux_ipappend_strings *ipappend;
  char *gfxboot_cwd = (char *) gfx_config.gfxboot_cwd;

  if(gfxboot_cwd) {
    chdir(gfxboot_cwd);
    gfx_config.gfxboot_cwd = 0;
  }

  for(menu_ptr = menu; menu_ptr; menu_ptr = menu_ptr->next, index--) {
    if(!index) break;
  }

  // invalid index or menu entry
  if(!menu_ptr || !menu_ptr->menu_label) return;

  arg = skipspace(cmdline);
  label_len = strlen(menu_ptr->label);
  menu_label_len = strlen(menu_ptr->menu_label);

  // if it does not start with label string, assume first word is kernel name
  if(!strncmp(arg, menu_ptr->label, label_len)) {
    arg += label_len;
  }
  else if(!strncmp(arg, menu_ptr->menu_label, menu_label_len)) {
    arg += menu_label_len;
  }
  else {
    alt_kernel = arg;
    arg = skip_nonspaces(arg);
    if(*arg) *arg++ = 0;
    if(*alt_kernel) menu_ptr->alt_kernel = alt_kernel;
  }

  arg = skipspace(arg);

  // handle IPAPPEND
  if(menu_ptr->ipappend && (ipapp = atoi(menu_ptr->ipappend))) {
    ipappend = syslinux_ipappend_strings();
    for(i = 0; i < ipappend->count; i++) {
      if((ipapp & (1 << i)) && ipappend->ptr[i]) {
        sprintf(arg + strlen(arg), " %s", ipappend->ptr[i]);
      }
    }
  }

  boot_entry(menu_ptr, arg);

  gfx_progress_done();
}


// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// 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;
  char *s, *s0, *t, *initrd_arg;

  if(!menu_ptr) return;

  if(menu_ptr->localboot) {
    gfx_done();
    syslinux_local_boot(strtol(menu_ptr->localboot, NULL, 0));

    return;
  }

  file = menu_ptr->alt_kernel;
  if(!file) 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

  kernel_size = 0;

  if((fd = open(file, O_RDONLY)) >= 0) {
    if(!fstat(fd, &sbuf) && S_ISREG(sbuf.st_mode)) kernel_size = sbuf.st_size;
    close(fd);
  }

  gfx_progress_init(kernel_size, file);

  kernel = load_one(file, &kernel_size);

  if(!kernel) {
    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 = skipspace(skip_nonspaces(s));
  }

  if(*s) {
    s += sizeof "initrd=" - 1;
    *skip_nonspaces(s) = 0;
    initrd_arg = s;
  }
  else if(initrd_arg) {
    free(s0);
    initrd_arg = s0 = strdup(initrd_arg);
  }

  if(initrd_arg) {
    initrd = initramfs_init();

    while((t = strsep(&initrd_arg, ","))) {
      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);

  gfx_done();

  syslinux_boot_linux(kernel, kernel_size, initrd, arg);
}