Use <dprintf.h> instead of an ad hoc definition
[profile/ivi/syslinux.git] / com32 / gfxboot / gfxboot.c
1 /*
2  *
3  * gfxboot.c
4  *
5  * A com32 module to load gfxboot graphics.
6  *
7  * Copyright (c) 2009 Steffen Winterfeldt.
8  *
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU General Public License as published by the
11  * Free Software Foundation, Inc., 53 Temple Place Ste 330, Boston MA
12  * 02111-1307, USA; either version 2 of the License, or (at your option) any
13  * later version; incorporated herein by reference.
14  *
15  */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <unistd.h>
20 #include <string.h>
21 #include <fcntl.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <minmax.h>
25 #include <ctype.h>
26
27 #include <syslinux/loadfile.h>
28 #include <syslinux/config.h>
29 #include <syslinux/linux.h>
30 #include <syslinux/boot.h>
31 #include <console.h>
32 #include <com32.h>
33
34
35 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
36 #define MAX_CONFIG_LINE_LEN     2048
37 #define MAX_CMDLINE_LEN         2048
38
39 // buffer for realmode callback
40 // must be at least block size; can in theory be larger than 4k, but there's
41 // not enough space left
42 #define REALMODE_BUF_SIZE       4096
43 #define LOWMEM_BUF_SIZE         65536
44
45 // gfxboot working memory in MB
46 #define GFX_MEMORY_SIZE         7
47
48 // read chunk size for progress bar
49 #define CHUNK_SIZE      (64 << 10)
50
51 // callback function numbers
52 #define GFX_CB_INIT             0
53 #define GFX_CB_DONE             1
54 #define GFX_CB_INPUT            2
55 #define GFX_CB_MENU_INIT        3
56 #define GFX_CB_INFOBOX_INIT     4
57 #define GFX_CB_INFOBOX_DONE     5
58 #define GFX_CB_PROGRESS_INIT    6
59 #define GFX_CB_PROGRESS_DONE    7
60 #define GFX_CB_PROGRESS_UPDATE  8
61 #define GFX_CB_PROGRESS_LIMIT   9               // unused
62 #define GFX_CB_PASSWORD_INIT    10
63 #define GFX_CB_PASSWORD_DONE    11
64
65 // real mode code chunk, will be placed into lowmem buffer
66 extern const char realmode_callback_start[], realmode_callback_end[];
67
68 // gets in the way
69 #undef linux
70
71
72 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
73 // gfxboot config data (64 bytes)
74 typedef struct __attribute__ ((packed)) {
75   uint8_t bootloader;           //  0: boot loader type (0: lilo, 1: syslinux, 2: grub)
76   uint8_t sector_shift;         //  1: sector shift
77   uint8_t media_type;           //  2: media type (0: disk, 1: floppy, 2: cdrom)
78   uint8_t failsafe;             //  3: turn on failsafe mode (bitmask)
79                                 //    0: SHIFT pressed
80                                 //    1: skip gfxboot
81                                 //    2: skip monitor detection
82   uint8_t sysconfig_size;       //  4: size of sysconfig data
83   uint8_t boot_drive;           //  5: BIOS boot drive
84   uint16_t callback;            //  6: offset to callback handler
85   uint16_t bootloader_seg;      //  8: code/data segment used by bootloader; must follow gfx_callback
86   uint16_t serial_port;         // 10: syslinux initialized serial port from 'serial' option
87   uint32_t user_info_0;         // 12: data for info box
88   uint32_t user_info_1;         // 16: data for info box
89   uint32_t bios_mem_size;       // 20: BIOS memory size (in bytes)
90   uint16_t xmem_0;              // 24: extended mem area 0 (start:size in MB; 12:4 bits) - obsolete
91   uint16_t xmem_1;              // 26: extended mem area 1 - obsolete
92   uint16_t xmem_2;              // 28: extended mem area 2 - obsolete
93   uint16_t xmem_3;              // 30: extended mem area 3 - obsolete
94   uint32_t file;                // 32: start of gfx file
95   uint32_t archive_start;       // 36: start of cpio archive
96   uint32_t archive_end;         // 40: end of cpio archive
97   uint32_t mem0_start;          // 44: low free memory start
98   uint32_t mem0_end;            // 48: low free memory end
99   uint32_t xmem_start;          // 52: extended mem start
100   uint32_t xmem_end;            // 56: extended mem end
101   uint16_t features;            // 60: feature flags returned by GFX_CB_INIT
102                                 //    0: GFX_CB_MENU_INIT accepts 32 bit addresses
103                                 //    1: knows about xmem_start, xmem_end
104   uint16_t reserved_1;          // 62:
105   uint32_t gfxboot_cwd;         // 64: if set, points to current gfxboot working directory relative
106                                 //     to syslinux working directory
107 } gfx_config_t;
108
109
110 // gfxboot menu description (18 bytes)
111 typedef struct __attribute__ ((packed)) {
112   uint16_t entries;
113   char *default_entry;
114   char *label_list;
115   uint16_t label_size;
116   char *arg_list;
117   uint16_t arg_size;
118 } gfx_menu_t;
119
120
121 // menu description
122 typedef struct menu_s {
123   struct menu_s *next;
124   char *label;          // config entry name
125   char *menu_label;     // text to show in boot menu
126   char *kernel;         // name of program to load
127   char *alt_kernel;     // alternative name in case user has replaced it
128   char *linux;          // de facto an alias for 'kernel'
129   char *localboot;      // boot from local disk
130   char *initrd;         // initrd as separate line (instead of as part of 'append')
131   char *append;         // kernel args
132   char *ipappend;       // append special pxelinux args (see doc)
133 } menu_t;
134
135
136 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
137 gfx_config_t gfx_config;
138 gfx_menu_t gfx_menu;
139
140 menu_t *menu;
141 menu_t *menu_default;
142 static menu_t *menu_ptr, **menu_next;
143
144 struct {
145   uint32_t jmp_table[12];
146   uint16_t code_seg;
147   char fname_buf[64];
148 } gfx;
149
150 void *lowmem_buf;
151
152 int timeout;
153
154 char cmdline[MAX_CMDLINE_LEN];
155
156 // progress bar is visible
157 unsigned progress_active;
158
159
160 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
161 void show_message(char *file);
162 char *get_config_file_name(void);
163 char *skip_nonspaces(char *s);
164 void chop_line(char *s);
165 int read_config_file(const char *filename);
166 unsigned magic_ok(unsigned char *buf, unsigned *code_size);
167 unsigned find_file(unsigned char *buf, unsigned len, unsigned *gfx_file_start, unsigned *file_len, unsigned *code_size);
168 int gfx_init(char *file);
169 int gfx_menu_init(void);
170 void gfx_done(void);
171 int gfx_input(void);
172 void gfx_infobox(int type, char *str1, char *str2);
173 void gfx_progress_init(ssize_t kernel_size, char *label);
174 void gfx_progress_update(ssize_t size);
175 void gfx_progress_done(void);
176 void *load_one(char *file, ssize_t *file_size);
177 void boot(int index);
178 void boot_entry(menu_t *menu_ptr, char *arg);
179
180
181 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
182 int main(int argc, char **argv)
183 {
184   int menu_index;
185   const union syslinux_derivative_info *sdi;
186   char working_dir[256];
187
188   openconsole(&dev_stdcon_r, &dev_stdcon_w);
189
190   lowmem_buf = lmalloc(LOWMEM_BUF_SIZE);
191   if (!lowmem_buf) {
192     printf("Could not allocate memory.\n");
193     return 1;
194   }
195
196   sdi = syslinux_derivative_info();
197
198   gfx_config.sector_shift = sdi->disk.sector_shift;
199   gfx_config.boot_drive = sdi->disk.drive_number;
200
201   if(sdi->c.filesystem == SYSLINUX_FS_PXELINUX) {
202     gfx_config.sector_shift = 11;
203     gfx_config.boot_drive = 0;
204   }
205
206   gfx_config.media_type = gfx_config.boot_drive < 0x80 ? 1 : 0;
207
208   if(sdi->c.filesystem == SYSLINUX_FS_ISOLINUX) {
209     gfx_config.media_type = sdi->iso.cd_mode ? 0 : 2;
210   }
211
212   gfx_config.bootloader = 1;
213   gfx_config.sysconfig_size = sizeof gfx_config;
214   gfx_config.bootloader_seg = 0;        // apparently not needed
215
216   if(argc < 2) {
217     printf("Usage: gfxboot.c32 bootlogo_file [message_file]\n");
218     if(argc > 2) show_message(argv[2]);
219
220     return 0;
221   }
222
223   if(read_config_file("~")) {
224     printf("Error reading config file\n");
225     if(argc > 2) show_message(argv[2]);
226
227     return 0;
228   }
229
230   if(getcwd(working_dir, sizeof working_dir)) {
231     gfx_config.gfxboot_cwd = (uint32_t) working_dir;
232   }
233
234   if(gfx_init(argv[1])) {
235     printf("Error setting up gfxboot\n");
236     if(argc > 2) show_message(argv[2]);
237
238     return 0;
239   }
240
241   gfx_menu_init();
242
243   for(;;) {
244     menu_index = gfx_input();
245
246     // abort gfx, return to text mode prompt
247     if(menu_index == -1) {
248       gfx_done();
249       break;
250     }
251
252     // does not return if it succeeds
253     boot(menu_index);
254   }
255
256   if(argc > 2) show_message(argv[2]);
257
258   return 0;
259 }
260
261
262 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
263 void show_message(char *file)
264 {
265   int c;
266   FILE *f;
267
268   if(!(f = fopen(file, "r"))) return;
269
270   while((c = getc(f)) != EOF) {
271     if(c < ' ' && c != '\n' && c != '\t') continue;
272     printf("%c", c);
273   }
274
275   fclose(f);
276 }
277
278
279 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
280 char *skip_nonspaces(char *s)
281 {
282   while(*s && *s != ' ' && *s != '\t') s++;
283
284   return s;
285 }
286
287
288 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
289 void chop_line(char *s)
290 {
291   int i = strlen(s);
292
293   if(!i) return;
294
295   while(--i >= 0) {
296     if(s[i] == ' ' || s[i] == '\t' || s[i] == '\n') {
297       s[i] = 0;
298     }
299     else {
300       break;
301     }
302   }
303 }
304
305
306 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
307 // Read and parse syslinux config file.
308 //
309 // return:
310 //   0: ok, 1: error
311 //
312 int read_config_file(const char *filename)
313 {
314   FILE *f;
315   char *s, *t, buf[MAX_CONFIG_LINE_LEN];
316   unsigned u, top_level = 0, text = 0;
317
318   if(!strcmp(filename, "~")) {
319     top_level = 1;
320     filename = syslinux_config_file();
321     gfx_menu.entries = 0;
322     gfx_menu.label_size = 0;
323     gfx_menu.arg_size = 0;
324     menu_ptr = NULL;
325     menu_next = &menu;
326     menu_default = calloc(1, sizeof *menu_default);
327   }
328
329   if(!(f = fopen(filename, "r"))) return 1;
330
331   while((s = fgets(buf, sizeof buf, f))) {
332     chop_line(s);
333     s = skipspace(s);
334     if(!*s || *s == '#') continue;
335     t = skip_nonspaces(s);
336     if(*t) *t++ = 0;
337     t = skipspace(t);
338
339     if(!strcasecmp(s, "endtext")) {
340       text = 0;
341       continue;
342     }
343
344     if (text)
345       continue;
346
347     if(!strcasecmp(s, "timeout")) {
348       timeout = atoi(t);
349       continue;
350     }
351
352     if(!strcasecmp(s, "default")) {
353       menu_default->label = strdup(t);
354       u = strlen(t);
355       if(u > gfx_menu.label_size) gfx_menu.label_size = u;
356       continue;
357     }
358
359     if(!strcasecmp(s, "label")) {
360       menu_ptr = *menu_next = calloc(1, sizeof **menu_next);
361       menu_next = &menu_ptr->next;
362       gfx_menu.entries++;
363       menu_ptr->label = menu_ptr->menu_label = strdup(t);
364       u = strlen(t);
365       if(u > gfx_menu.label_size) gfx_menu.label_size = u;
366       continue;
367     }
368
369     if(!strcasecmp(s, "kernel") && menu_ptr) {
370       menu_ptr->kernel = strdup(t);
371       continue;
372     }
373
374     if(!strcasecmp(s, "linux") && menu_ptr) {
375       menu_ptr->linux = strdup(t);
376       continue;
377     }
378
379     if(!strcasecmp(s, "localboot") && menu_ptr) {
380       menu_ptr->localboot = strdup(t);
381       continue;
382     }
383
384     if(!strcasecmp(s, "initrd") && menu_ptr) {
385       menu_ptr->initrd = strdup(t);
386       continue;
387     }
388
389     if(!strcasecmp(s, "append")) {
390       (menu_ptr ?: menu_default)->append = strdup(t);
391       u = strlen(t);
392       if(u > gfx_menu.arg_size) gfx_menu.arg_size = u;
393       continue;
394     }
395
396     if(!strcasecmp(s, "ipappend")) {
397       (menu_ptr ?: menu_default)->ipappend = strdup(t);
398       continue;
399     }
400
401     if(!strcasecmp(s, "text")) {
402       text = 1;
403       continue;
404     }
405
406     if(!strcasecmp(s, "menu") && menu_ptr) {
407       s = skipspace(t);
408       t = skip_nonspaces(s);
409       if(*t) *t++ = 0;
410       t = skipspace(t);
411
412       if(!strcasecmp(s, "label")) {
413         menu_ptr->menu_label = strdup(t);
414         u = strlen(t);
415         if(u > gfx_menu.label_size) gfx_menu.label_size = u;
416         continue;
417       }
418
419       if(!strcasecmp(s, "include")) {
420         goto do_include;
421       }
422     }
423
424     if (!strcasecmp(s, "include")) {
425 do_include:
426       s = t;
427       t = skip_nonspaces(s);
428       if (*t) *t = 0;
429       read_config_file(s);
430     }
431   }
432
433   fclose(f);
434
435   if (!top_level)
436     return 0;
437
438   if (gfx_menu.entries == 0) {
439     printf("No LABEL keywords found.\n");
440     return 1;
441   }
442
443   // final '\0'
444   gfx_menu.label_size++;
445   gfx_menu.arg_size++;
446
447   // ensure we have a default entry
448   if(!menu_default->label) menu_default->label = menu->label;
449
450   if(menu_default->label) {
451     for(menu_ptr = menu; menu_ptr; menu_ptr = menu_ptr->next) {
452       if(!strcmp(menu_default->label, menu_ptr->label)) {
453         menu_default->menu_label = menu_ptr->menu_label;
454         break;
455       }
456     }
457   }
458
459   gfx_menu.default_entry = menu_default->menu_label;
460   gfx_menu.label_list = calloc(gfx_menu.entries, gfx_menu.label_size);
461   gfx_menu.arg_list = calloc(gfx_menu.entries, gfx_menu.arg_size);
462
463   for(u = 0, menu_ptr = menu; menu_ptr; menu_ptr = menu_ptr->next, u++) {
464     if(!menu_ptr->append) menu_ptr->append = menu_default->append;
465     if(!menu_ptr->ipappend) menu_ptr->ipappend = menu_default->ipappend;
466
467     if(menu_ptr->menu_label) strcpy(gfx_menu.label_list + u * gfx_menu.label_size, menu_ptr->menu_label);
468     if(menu_ptr->append) strcpy(gfx_menu.arg_list + u * gfx_menu.arg_size, menu_ptr->append);
469   }
470
471   return 0;
472 }
473
474
475 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
476 // Check header and return code start offset.
477 //
478 unsigned magic_ok(unsigned char *buf, unsigned *code_size)
479 {
480   if(
481     *(unsigned *) buf == 0x0b2d97f00 &&         // magic id
482     (buf[4] == 8)                               // version 8
483   ) {
484     *code_size = *(unsigned *) (buf + 12);
485     return *(unsigned *) (buf + 8);
486   }
487
488   return 0;
489 }
490
491
492 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
493 // Search (cpio archive) for gfx file.
494 //
495 unsigned find_file(unsigned char *buf, unsigned len, unsigned *gfx_file_start, unsigned *file_len, unsigned *code_size)
496 {
497   unsigned i, fname_len, code_start = 0;
498
499   *gfx_file_start = 0;
500   *code_size = 0;
501
502   if((code_start = magic_ok(buf, code_size))) return code_start;
503
504   for(i = 0; i < len;) {
505     if((len - i) >= 0x1a && (buf[i] + (buf[i + 1] << 8)) == 0x71c7) {
506       fname_len = *(unsigned short *) (buf + i + 20);
507       *file_len = *(unsigned short *) (buf + i + 24) + (*(unsigned short *) (buf + i + 22) << 16);
508       i += 26 + fname_len;
509       i = ((i + 1) & ~1);
510       if((code_start = magic_ok(buf + i, code_size))) {
511         *gfx_file_start = i;
512         return code_start;
513       }
514       i += *file_len;
515       i = ((i + 1) & ~1);
516     }
517     else {
518       break;
519     }
520   }
521
522   return code_start;
523 }
524
525
526 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
527 // Initialize gfxboot code.
528 //
529 // return:
530 //   0: ok, 1: error
531 //
532 int gfx_init(char *file)
533 {
534   size_t archive_size = 0;
535   void *archive;
536   unsigned code_start, code_size, file_start, file_len, u;
537   com32sys_t r;
538   void *lowmem = lowmem_buf;
539   unsigned lowmem_size = LOWMEM_BUF_SIZE;
540
541   progress_active = 0;
542
543   printf("Loading %s...\n", file);
544   if(loadfile(file, &archive, &archive_size)) return 1;
545
546   if(!archive_size) return 1;
547
548   // printf("%s: %d\n", file, archive_size);
549
550   gfx_config.archive_start = (uint32_t) archive;
551   gfx_config.archive_end = gfx_config.archive_start + archive_size;
552
553   // locate file inside cpio archive
554   if(!(code_start = find_file(archive, archive_size, &file_start, &file_len, &code_size))) {
555     printf("%s: invalid file format\n", file);
556     return 1;
557   }
558
559 #if 0
560   printf(
561     "code_start = 0x%x, code_size = 0x%x\n"
562     "archive_start = 0x%x, archive size = 0x%x\n"
563     "file_start = 0x%x, file_len = 0x%x\n",
564     code_start, code_size,
565     gfx_config.archive_start, archive_size,
566     file_start, file_len
567   );
568 #endif
569
570   gfx_config.file = gfx_config.archive_start + file_start;
571
572   u = realmode_callback_end - realmode_callback_start;
573   u = (u + REALMODE_BUF_SIZE + 0xf) & ~0xf;
574
575   if(u + code_size > lowmem_size) {
576     printf("lowmem buffer too small: size %u, needed %u\n", lowmem_size, u + code_size);
577     return 1;
578   }
579
580   memcpy(lowmem + REALMODE_BUF_SIZE, realmode_callback_start,
581          realmode_callback_end - realmode_callback_start);
582
583   // fill in buffer size and location
584   *(uint16_t *) (lowmem + REALMODE_BUF_SIZE) = REALMODE_BUF_SIZE;
585   *(uint16_t *) (lowmem + REALMODE_BUF_SIZE + 2) = (uint32_t) lowmem >> 4;
586
587   gfx_config.bootloader_seg = ((uint32_t) lowmem + REALMODE_BUF_SIZE) >> 4;
588   gfx_config.callback = 4;      // start address
589
590   lowmem += u;
591   lowmem_size -= u;
592
593   memcpy(lowmem, archive + file_start + code_start, code_size);
594
595   gfx_config.mem0_start = (uint32_t) lowmem + code_size;
596   gfx_config.mem0_end = (uint32_t) lowmem + lowmem_size;
597   // align a bit
598   gfx_config.mem0_start = (gfx_config.mem0_start + 0xf) & ~0xf;
599
600   gfx_config.xmem_start = (uint32_t) malloc(GFX_MEMORY_SIZE << 20);
601   if(gfx_config.xmem_start) {
602     gfx_config.xmem_end = gfx_config.xmem_start + (GFX_MEMORY_SIZE << 20);
603   }
604
605   // fake; not used anyway
606   gfx_config.bios_mem_size = 256 << 20;
607
608   gfx.code_seg = (uint32_t) lowmem >> 4;
609
610   for(u = 0; u < sizeof gfx.jmp_table / sizeof *gfx.jmp_table; u++) {
611     gfx.jmp_table[u] = (gfx.code_seg << 16) + *(uint16_t *) (lowmem + 2 * u);
612   }
613
614 #if 0
615   for(u = 0; u < sizeof gfx.jmp_table / sizeof *gfx.jmp_table; u++) {
616     printf("%d: 0x%08x\n", u, gfx.jmp_table[u]);
617   }
618 #endif
619
620   // we are ready to start
621
622   r.esi.l = (uint32_t) &gfx_config;
623   __farcall(gfx.code_seg, gfx.jmp_table[GFX_CB_INIT], &r, &r);
624
625   if((r.eflags.l & EFLAGS_CF)) {
626     printf("graphics initialization failed\n");
627
628     return 1;
629   }
630
631   if((gfx_config.features & 3) != 3) {
632     gfx_done();
633
634     printf("%s: boot graphics code too old, please use newer version\n", file);
635
636     return 1;
637   }
638
639
640   return 0;
641 }
642
643
644 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
645 int gfx_menu_init(void)
646 {
647   com32sys_t r;
648
649   r.esi.l = (uint32_t) &gfx_menu;
650   __farcall(gfx.code_seg, gfx.jmp_table[GFX_CB_MENU_INIT], &r, &r);
651
652   return 0;
653 }
654
655
656 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
657 void gfx_done(void)
658 {
659   com32sys_t r;
660
661   gfx_progress_done();
662
663   __farcall(gfx.code_seg, gfx.jmp_table[GFX_CB_DONE], &r, &r);
664 }
665
666
667 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
668 // Run gfxboot main loop.
669 //
670 // return:
671 //   boot menu index (-1: go to text mode prompt)
672 //
673 int gfx_input(void)
674 {
675   com32sys_t r;
676
677   r.edi.l = (uint32_t) cmdline;
678   r.ecx.l = sizeof cmdline;
679   r.eax.l = timeout * 182 / 100;
680   timeout = 0;          // use timeout only first time
681   __farcall(gfx.code_seg, gfx.jmp_table[GFX_CB_INPUT], &r, &r);
682   if((r.eflags.l & EFLAGS_CF)) r.eax.l = 1;
683
684   if(r.eax.l == 1) return -1;
685
686   return r.ebx.l;
687 }
688
689
690 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
691 void gfx_infobox(int type, char *str1, char *str2)
692 {
693   com32sys_t r;
694
695   r.eax.l = type;
696   r.esi.l = (uint32_t) str1;
697   r.edi.l = (uint32_t) str2;
698   __farcall(gfx.code_seg, gfx.jmp_table[GFX_CB_INFOBOX_INIT], &r, &r);
699   r.edi.l = r.eax.l = 0;
700   __farcall(gfx.code_seg, gfx.jmp_table[GFX_CB_INPUT], &r, &r);
701   __farcall(gfx.code_seg, gfx.jmp_table[GFX_CB_INFOBOX_DONE], &r, &r);
702 }
703
704
705 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
706 void gfx_progress_init(ssize_t kernel_size, char *label)
707 {
708   com32sys_t r;
709
710   if(!progress_active) {
711     r.eax.l = kernel_size >> gfx_config.sector_shift;           // in sectors
712     r.esi.l = (uint32_t) label;
713     __farcall(gfx.code_seg, gfx.jmp_table[GFX_CB_PROGRESS_INIT], &r, &r);
714   }
715
716   progress_active = 1;
717 }
718
719
720 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
721 void gfx_progress_update(ssize_t advance)
722 {
723   com32sys_t r;
724
725   if(progress_active) {
726     r.eax.l = advance >> gfx_config.sector_shift;               // in sectors
727     __farcall(gfx.code_seg, gfx.jmp_table[GFX_CB_PROGRESS_UPDATE], &r, &r);
728   }
729 }
730
731
732 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
733 void gfx_progress_done(void)
734 {
735   com32sys_t r;
736
737   if(progress_active) {
738     __farcall(gfx.code_seg, gfx.jmp_table[GFX_CB_PROGRESS_DONE], &r, &r);
739   }
740
741   progress_active = 0;
742 }
743
744
745 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
746 // Read file and update progress bar.
747 //
748 void *load_one(char *file, ssize_t *file_size)
749 {
750   int fd;
751   void *buf = NULL;
752   char *str;
753   struct stat sbuf;
754   ssize_t size = 0, cur, i;
755
756   *file_size = 0;
757
758   if((fd = open(file, O_RDONLY)) == -1) {
759     asprintf(&str, "%s: file not found", file);
760     gfx_infobox(0, str, NULL);
761     free(str);
762     return buf;
763   }
764
765   if(!fstat(fd, &sbuf) && S_ISREG(sbuf.st_mode)) size = sbuf.st_size;
766
767   i = 0;
768
769   if(size) {
770     buf = malloc(size);
771     for(i = 1, cur = 0 ; cur < size && i > 0; cur += i) {
772       i = read(fd, buf + cur, min(CHUNK_SIZE, size - cur));
773       if(i == -1) break;
774       gfx_progress_update(i);
775     }
776   }
777   else {
778     do {
779       buf = realloc(buf, size + CHUNK_SIZE);
780       i = read(fd, buf + size, CHUNK_SIZE);
781       if(i == -1) break;
782       size += i;
783       gfx_progress_update(i);
784     } while(i > 0);
785   }
786
787   close(fd);
788
789   if(i == -1) {
790     asprintf(&str, "%s: read error @ %d", file, size);
791     gfx_infobox(0, str, NULL);
792     free(str);
793     free(buf);
794     buf = NULL;
795     size = 0;
796   }
797
798   *file_size = size;
799
800   return buf;
801 }
802
803
804 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
805 // Boot menu entry.
806 //
807 // cmdline can optionally start with label string.
808 //
809 void boot(int index)
810 {
811   char *arg, *alt_kernel;
812   menu_t *menu_ptr;
813   int i, label_len;
814   unsigned ipapp;
815   const struct syslinux_ipappend_strings *ipappend;
816   char *gfxboot_cwd = (char *) gfx_config.gfxboot_cwd;
817
818   if(gfxboot_cwd) {
819     chdir(gfxboot_cwd);
820     gfx_config.gfxboot_cwd = 0;
821   }
822
823   for(menu_ptr = menu; menu_ptr; menu_ptr = menu_ptr->next, index--) {
824     if(!index) break;
825   }
826
827   // invalid index or menu entry
828   if(!menu_ptr || !menu_ptr->menu_label) return;
829
830   arg = skipspace(cmdline);
831   label_len = strlen(menu_ptr->menu_label);
832
833   // if it does not start with label string, assume first word is kernel name
834   if(strncmp(arg, menu_ptr->menu_label, label_len)) {
835     alt_kernel = arg;
836     arg = skip_nonspaces(arg);
837     if(*arg) *arg++ = 0;
838     if(*alt_kernel) menu_ptr->alt_kernel = alt_kernel;
839   }
840   else {
841     arg += label_len;
842   }
843
844   arg = skipspace(arg);
845
846   // handle IPAPPEND
847   if(menu_ptr->ipappend && (ipapp = atoi(menu_ptr->ipappend))) {
848     ipappend = syslinux_ipappend_strings();
849     for(i = 0; i < ipappend->count; i++) {
850       if((ipapp & (1 << i)) && ipappend->ptr[i]) {
851         sprintf(arg + strlen(arg), " %s", ipappend->ptr[i]);
852       }
853     }
854   }
855
856   boot_entry(menu_ptr, arg);
857
858   gfx_progress_done();
859 }
860
861
862 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
863 // Load & run kernel.
864 //
865 // Returns only on error.
866 //
867 void boot_entry(menu_t *menu_ptr, char *arg)
868 {
869   void *kernel, *initrd_buf;
870   ssize_t kernel_size = 0, initrd_size = 0;
871   struct initramfs *initrd = NULL;
872   char *file, *cmd_buf;
873   int fd;
874   struct stat sbuf;
875   char *s, *s0, *t, *initrd_arg;
876
877   if(!menu_ptr) return;
878
879   if(menu_ptr->localboot) {
880     gfx_done();
881     syslinux_local_boot(strtol(menu_ptr->localboot, NULL, 0));
882
883     return;
884   }
885
886   file = menu_ptr->alt_kernel;
887   if(!file) file = menu_ptr->kernel;
888   if(!file) file = menu_ptr->linux;
889   if(!file) {
890     gfx_done();
891     asprintf(&cmd_buf, "%s %s", menu_ptr->label, arg);
892     syslinux_run_command(cmd_buf);
893     return;
894   }
895
896   // first, load kernel
897
898   kernel_size = 0;
899
900   if((fd = open(file, O_RDONLY)) >= 0) {
901     if(!fstat(fd, &sbuf) && S_ISREG(sbuf.st_mode)) kernel_size = sbuf.st_size;
902     close(fd);
903   }
904
905   gfx_progress_init(kernel_size, file);
906
907   kernel = load_one(file, &kernel_size);
908
909   if(!kernel) {
910     return;
911   }
912
913   if(kernel_size < 1024 || *(uint32_t *) (kernel + 0x202) != 0x53726448) {
914     // not a linux kernel
915     gfx_done();
916     asprintf(&cmd_buf, "%s %s", menu_ptr->label, arg);
917     syslinux_run_command(cmd_buf);
918     return;
919   }
920
921   // printf("kernel = %p, size = %d\n", kernel, kernel_size);
922
923   // parse cmdline for "initrd" option
924
925   initrd_arg = menu_ptr->initrd;
926
927   s = s0 = strdup(arg);
928
929   while(*s && strncmp(s, "initrd=", sizeof "initrd=" - 1)) {
930     s = skipspace(skip_nonspaces(s));
931   }
932
933   if(*s) {
934     s += sizeof "initrd=" - 1;
935     *skip_nonspaces(s) = 0;
936     initrd_arg = s;
937   }
938   else if(initrd_arg) {
939     free(s0);
940     initrd_arg = s0 = strdup(initrd_arg);
941   }
942
943   if(initrd_arg) {
944     initrd = initramfs_init();
945
946     while((t = strsep(&initrd_arg, ","))) {
947       initrd_buf = load_one(t, &initrd_size);
948
949       if(!initrd_buf) {
950         printf("%s: read error\n", t);
951         free(s0);
952         return;
953       }
954
955       initramfs_add_data(initrd, initrd_buf, initrd_size, initrd_size, 4);
956
957       // printf("initrd = %p, size = %d\n", initrd_buf, initrd_size);
958     }
959   }
960
961   free(s0);
962
963   gfx_done();
964
965   syslinux_boot_linux(kernel, kernel_size, initrd, arg);
966 }
967
968