5 * A com32 module to load gfxboot graphics.
7 * Copyright (c) 2009 Steffen Winterfeldt.
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.
22 #include <sys/types.h>
27 #include <syslinux/loadfile.h>
28 #include <syslinux/config.h>
29 #include <syslinux/linux.h>
30 #include <syslinux/boot.h>
35 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
36 #define MAX_CONFIG_LINE_LEN 2048
37 #define MAX_CMDLINE_LEN 2048
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
45 // gfxboot working memory in MB
46 #define GFX_MEMORY_SIZE 7
48 // read chunk size for progress bar
49 #define CHUNK_SIZE (64 << 10)
51 // callback function numbers
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
65 // real mode code chunk, will be placed into lowmem buffer
66 extern const char realmode_callback_start[], realmode_callback_end[];
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)
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:
108 // gfxboot menu description (18 bytes)
109 typedef struct __attribute__ ((packed)) {
120 typedef struct menu_s {
122 char *label; // config entry name
123 char *menu_label; // text to show in boot menu
124 char *kernel; // name of program to load
125 char *alt_kernel; // alternative name in case user has replaced it
126 char *linux; // de facto an alias for 'kernel'
127 char *localboot; // boot from local disk
128 char *initrd; // initrd as separate line (instead of as part of 'append')
129 char *append; // kernel args
130 char *ipappend; // append special pxelinux args (see doc)
134 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
135 gfx_config_t gfx_config;
139 menu_t *menu_default;
140 static menu_t *menu_ptr, **menu_next;
143 uint32_t jmp_table[12];
152 char cmdline[MAX_CMDLINE_LEN];
154 // progress bar is visible
155 unsigned progress_active;
158 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
159 void show_message(char *file);
160 char *get_config_file_name(void);
161 char *skip_nonspaces(char *s);
162 void chop_line(char *s);
163 int read_config_file(const char *filename);
164 unsigned magic_ok(unsigned char *buf, unsigned *code_size);
165 unsigned find_file(unsigned char *buf, unsigned len, unsigned *gfx_file_start, unsigned *file_len, unsigned *code_size);
166 int gfx_init(char *file);
167 int gfx_menu_init(void);
170 void gfx_infobox(int type, char *str1, char *str2);
171 void gfx_progress_init(ssize_t kernel_size, char *label);
172 void gfx_progress_update(ssize_t size);
173 void gfx_progress_done(void);
174 void *load_one(char *file, ssize_t *file_size);
175 void boot(int index);
176 void boot_entry(menu_t *menu_ptr, char *arg);
179 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
180 int main(int argc, char **argv)
183 const union syslinux_derivative_info *sdi;
185 openconsole(&dev_stdcon_r, &dev_stdcon_w);
187 lowmem_buf = lmalloc(LOWMEM_BUF_SIZE);
189 printf("Could not allocate memory.\n");
193 sdi = syslinux_derivative_info();
195 gfx_config.sector_shift = sdi->disk.sector_shift;
196 gfx_config.boot_drive = sdi->disk.drive_number;
198 if(sdi->c.filesystem == SYSLINUX_FS_PXELINUX) {
199 gfx_config.sector_shift = 11;
200 gfx_config.boot_drive = 0;
203 gfx_config.media_type = gfx_config.boot_drive < 0x80 ? 1 : 0;
205 if(sdi->c.filesystem == SYSLINUX_FS_ISOLINUX) {
206 gfx_config.media_type = sdi->iso.cd_mode ? 0 : 2;
209 gfx_config.bootloader = 1;
210 gfx_config.sysconfig_size = sizeof gfx_config;
211 gfx_config.bootloader_seg = 0; // apparently not needed
214 printf("Usage: gfxboot.c32 bootlogo_file [message_file]\n");
215 if(argc > 2) show_message(argv[2]);
220 if(read_config_file("~")) {
221 printf("Error reading config file\n");
222 if(argc > 2) show_message(argv[2]);
227 if(gfx_init(argv[1])) {
228 printf("Error setting up gfxboot\n");
229 if(argc > 2) show_message(argv[2]);
237 menu_index = gfx_input();
239 // abort gfx, return to text mode prompt
240 if(menu_index == -1) {
245 // does not return if it succeeds
249 if(argc > 2) show_message(argv[2]);
255 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
256 void show_message(char *file)
261 if(!(f = fopen(file, "r"))) return;
263 while((c = getc(f)) != EOF) {
264 if(c < ' ' && c != '\n' && c != '\t') continue;
272 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
273 char *skip_nonspaces(char *s)
275 while(*s && *s != ' ' && *s != '\t') s++;
281 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
282 void chop_line(char *s)
289 if(s[i] == ' ' || s[i] == '\t' || s[i] == '\n') {
299 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
300 // Read and parse syslinux config file.
305 int read_config_file(const char *filename)
308 char *s, *t, buf[MAX_CONFIG_LINE_LEN];
309 unsigned u, top_level = 0, text = 0;
311 if(!strcmp(filename, "~")) {
313 filename = syslinux_config_file();
314 gfx_menu.entries = 0;
315 gfx_menu.label_size = 0;
316 gfx_menu.arg_size = 0;
319 menu_default = calloc(1, sizeof *menu_default);
322 if(!(f = fopen(filename, "r"))) return 1;
324 while((s = fgets(buf, sizeof buf, f))) {
327 if(!*s || *s == '#') continue;
328 t = skip_nonspaces(s);
332 if(!strcasecmp(s, "endtext")) {
340 if(!strcasecmp(s, "timeout")) {
345 if(!strcasecmp(s, "default")) {
346 menu_default->label = strdup(t);
348 if(u > gfx_menu.label_size) gfx_menu.label_size = u;
352 if(!strcasecmp(s, "label")) {
353 menu_ptr = *menu_next = calloc(1, sizeof **menu_next);
354 menu_next = &menu_ptr->next;
356 menu_ptr->label = menu_ptr->menu_label = strdup(t);
358 if(u > gfx_menu.label_size) gfx_menu.label_size = u;
362 if(!strcasecmp(s, "kernel") && menu_ptr) {
363 menu_ptr->kernel = strdup(t);
367 if(!strcasecmp(s, "linux") && menu_ptr) {
368 menu_ptr->linux = strdup(t);
372 if(!strcasecmp(s, "localboot") && menu_ptr) {
373 menu_ptr->localboot = strdup(t);
377 if(!strcasecmp(s, "initrd") && menu_ptr) {
378 menu_ptr->initrd = strdup(t);
382 if(!strcasecmp(s, "append")) {
383 (menu_ptr ?: menu_default)->append = strdup(t);
385 if(u > gfx_menu.arg_size) gfx_menu.arg_size = u;
389 if(!strcasecmp(s, "ipappend")) {
390 (menu_ptr ?: menu_default)->ipappend = strdup(t);
394 if(!strcasecmp(s, "text")) {
399 if(!strcasecmp(s, "menu") && menu_ptr) {
401 t = skip_nonspaces(s);
405 if(!strcasecmp(s, "label")) {
406 menu_ptr->menu_label = strdup(t);
408 if(u > gfx_menu.label_size) gfx_menu.label_size = u;
412 if(!strcasecmp(s, "include")) {
417 if (!strcasecmp(s, "include")) {
420 t = skip_nonspaces(s);
431 if (gfx_menu.entries == 0) {
432 printf("No LABEL keywords found.\n");
437 gfx_menu.label_size++;
440 // ensure we have a default entry
441 if(!menu_default->label) menu_default->label = menu->label;
443 if(menu_default->label) {
444 for(menu_ptr = menu; menu_ptr; menu_ptr = menu_ptr->next) {
445 if(!strcmp(menu_default->label, menu_ptr->label)) {
446 menu_default->menu_label = menu_ptr->menu_label;
452 gfx_menu.default_entry = menu_default->menu_label;
453 gfx_menu.label_list = calloc(gfx_menu.entries, gfx_menu.label_size);
454 gfx_menu.arg_list = calloc(gfx_menu.entries, gfx_menu.arg_size);
456 for(u = 0, menu_ptr = menu; menu_ptr; menu_ptr = menu_ptr->next, u++) {
457 if(!menu_ptr->append) menu_ptr->append = menu_default->append;
458 if(!menu_ptr->ipappend) menu_ptr->ipappend = menu_default->ipappend;
460 if(menu_ptr->menu_label) strcpy(gfx_menu.label_list + u * gfx_menu.label_size, menu_ptr->menu_label);
461 if(menu_ptr->append) strcpy(gfx_menu.arg_list + u * gfx_menu.arg_size, menu_ptr->append);
468 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
469 // Check header and return code start offset.
471 unsigned magic_ok(unsigned char *buf, unsigned *code_size)
474 *(unsigned *) buf == 0x0b2d97f00 && // magic id
475 (buf[4] == 8) // version 8
477 *code_size = *(unsigned *) (buf + 12);
478 return *(unsigned *) (buf + 8);
485 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
486 // Search (cpio archive) for gfx file.
488 unsigned find_file(unsigned char *buf, unsigned len, unsigned *gfx_file_start, unsigned *file_len, unsigned *code_size)
490 unsigned i, fname_len, code_start = 0;
495 if((code_start = magic_ok(buf, code_size))) return code_start;
497 for(i = 0; i < len;) {
498 if((len - i) >= 0x1a && (buf[i] + (buf[i + 1] << 8)) == 0x71c7) {
499 fname_len = *(unsigned short *) (buf + i + 20);
500 *file_len = *(unsigned short *) (buf + i + 24) + (*(unsigned short *) (buf + i + 22) << 16);
503 if((code_start = magic_ok(buf + i, code_size))) {
519 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
520 // Initialize gfxboot code.
525 int gfx_init(char *file)
527 size_t archive_size = 0;
529 unsigned code_start, code_size, file_start, file_len, u;
531 void *lowmem = lowmem_buf;
532 unsigned lowmem_size = LOWMEM_BUF_SIZE;
536 printf("Loading %s...\n", file);
537 if(loadfile(file, &archive, &archive_size)) return 1;
539 if(!archive_size) return 1;
541 // printf("%s: %d\n", file, archive_size);
543 gfx_config.archive_start = (uint32_t) archive;
544 gfx_config.archive_end = gfx_config.archive_start + archive_size;
546 // locate file inside cpio archive
547 if(!(code_start = find_file(archive, archive_size, &file_start, &file_len, &code_size))) {
548 printf("%s: invalid file format\n", file);
554 "code_start = 0x%x, code_size = 0x%x\n"
555 "archive_start = 0x%x, archive size = 0x%x\n"
556 "file_start = 0x%x, file_len = 0x%x\n",
557 code_start, code_size,
558 gfx_config.archive_start, archive_size,
563 gfx_config.file = gfx_config.archive_start + file_start;
565 u = realmode_callback_end - realmode_callback_start;
566 u = (u + REALMODE_BUF_SIZE + 0xf) & ~0xf;
568 if(u + code_size > lowmem_size) {
569 printf("lowmem buffer too small: size %u, needed %u\n", lowmem_size, u + code_size);
573 memcpy(lowmem + REALMODE_BUF_SIZE, realmode_callback_start,
574 realmode_callback_end - realmode_callback_start);
576 // fill in buffer size and location
577 *(uint16_t *) (lowmem + REALMODE_BUF_SIZE) = REALMODE_BUF_SIZE;
578 *(uint16_t *) (lowmem + REALMODE_BUF_SIZE + 2) = (uint32_t) lowmem >> 4;
580 gfx_config.bootloader_seg = ((uint32_t) lowmem + REALMODE_BUF_SIZE) >> 4;
581 gfx_config.callback = 4; // start address
586 memcpy(lowmem, archive + file_start + code_start, code_size);
588 gfx_config.mem0_start = (uint32_t) lowmem + code_size;
589 gfx_config.mem0_end = (uint32_t) lowmem + lowmem_size;
591 gfx_config.mem0_start = (gfx_config.mem0_start + 0xf) & ~0xf;
593 gfx_config.xmem_start = (uint32_t) malloc(GFX_MEMORY_SIZE << 20);
594 if(gfx_config.xmem_start) {
595 gfx_config.xmem_end = gfx_config.xmem_start + (GFX_MEMORY_SIZE << 20);
598 // fake; not used anyway
599 gfx_config.bios_mem_size = 256 << 20;
601 gfx.code_seg = (uint32_t) lowmem >> 4;
603 for(u = 0; u < sizeof gfx.jmp_table / sizeof *gfx.jmp_table; u++) {
604 gfx.jmp_table[u] = (gfx.code_seg << 16) + *(uint16_t *) (lowmem + 2 * u);
608 for(u = 0; u < sizeof gfx.jmp_table / sizeof *gfx.jmp_table; u++) {
609 printf("%d: 0x%08x\n", u, gfx.jmp_table[u]);
613 // we are ready to start
615 r.esi.l = (uint32_t) &gfx_config;
616 __farcall(gfx.code_seg, gfx.jmp_table[GFX_CB_INIT], &r, &r);
618 if((r.eflags.l & EFLAGS_CF)) {
619 printf("graphics initialization failed\n");
624 if((gfx_config.features & 3) != 3) {
627 printf("%s: boot graphics code too old, please use newer version\n", file);
637 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
638 int gfx_menu_init(void)
642 r.esi.l = (uint32_t) &gfx_menu;
643 __farcall(gfx.code_seg, gfx.jmp_table[GFX_CB_MENU_INIT], &r, &r);
649 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
656 __farcall(gfx.code_seg, gfx.jmp_table[GFX_CB_DONE], &r, &r);
660 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
661 // Run gfxboot main loop.
664 // boot menu index (-1: go to text mode prompt)
670 r.edi.l = (uint32_t) cmdline;
671 r.ecx.l = sizeof cmdline;
672 r.eax.l = timeout * 182 / 100;
673 timeout = 0; // use timeout only first time
674 __farcall(gfx.code_seg, gfx.jmp_table[GFX_CB_INPUT], &r, &r);
675 if((r.eflags.l & EFLAGS_CF)) r.eax.l = 1;
677 if(r.eax.l == 1) return -1;
683 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
684 void gfx_infobox(int type, char *str1, char *str2)
689 r.esi.l = (uint32_t) str1;
690 r.edi.l = (uint32_t) str2;
691 __farcall(gfx.code_seg, gfx.jmp_table[GFX_CB_INFOBOX_INIT], &r, &r);
692 r.edi.l = r.eax.l = 0;
693 __farcall(gfx.code_seg, gfx.jmp_table[GFX_CB_INPUT], &r, &r);
694 __farcall(gfx.code_seg, gfx.jmp_table[GFX_CB_INFOBOX_DONE], &r, &r);
698 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
699 void gfx_progress_init(ssize_t kernel_size, char *label)
703 if(!progress_active) {
704 r.eax.l = kernel_size >> gfx_config.sector_shift; // in sectors
705 r.esi.l = (uint32_t) label;
706 __farcall(gfx.code_seg, gfx.jmp_table[GFX_CB_PROGRESS_INIT], &r, &r);
713 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
714 void gfx_progress_update(ssize_t advance)
718 if(progress_active) {
719 r.eax.l = advance >> gfx_config.sector_shift; // in sectors
720 __farcall(gfx.code_seg, gfx.jmp_table[GFX_CB_PROGRESS_UPDATE], &r, &r);
725 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
726 void gfx_progress_done(void)
730 if(progress_active) {
731 __farcall(gfx.code_seg, gfx.jmp_table[GFX_CB_PROGRESS_DONE], &r, &r);
738 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
739 // Read file and update progress bar.
741 void *load_one(char *file, ssize_t *file_size)
747 ssize_t size = 0, cur, i;
751 if((fd = open(file, O_RDONLY)) == -1) {
752 asprintf(&str, "%s: file not found", file);
753 gfx_infobox(0, str, NULL);
758 if(!fstat(fd, &sbuf) && S_ISREG(sbuf.st_mode)) size = sbuf.st_size;
764 for(i = 1, cur = 0 ; cur < size && i > 0; cur += i) {
765 i = read(fd, buf + cur, min(CHUNK_SIZE, size - cur));
767 gfx_progress_update(i);
772 buf = realloc(buf, size + CHUNK_SIZE);
773 i = read(fd, buf + size, CHUNK_SIZE);
776 gfx_progress_update(i);
783 asprintf(&str, "%s: read error @ %d", file, size);
784 gfx_infobox(0, str, NULL);
797 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
800 // cmdline can optionally start with label string.
804 char *arg, *alt_kernel;
808 const struct syslinux_ipappend_strings *ipappend;
810 for(menu_ptr = menu; menu_ptr; menu_ptr = menu_ptr->next, index--) {
814 // invalid index or menu entry
815 if(!menu_ptr || !menu_ptr->menu_label) return;
817 arg = skipspace(cmdline);
818 label_len = strlen(menu_ptr->menu_label);
820 // if it does not start with label string, assume first word is kernel name
821 if(strncmp(arg, menu_ptr->menu_label, label_len)) {
823 arg = skip_nonspaces(arg);
825 if(*alt_kernel) menu_ptr->alt_kernel = alt_kernel;
831 arg = skipspace(arg);
834 if(menu_ptr->ipappend && (ipapp = atoi(menu_ptr->ipappend))) {
835 ipappend = syslinux_ipappend_strings();
836 for(i = 0; i < ipappend->count; i++) {
837 if((ipapp & (1 << i)) && ipappend->ptr[i]) {
838 sprintf(arg + strlen(arg), " %s", ipappend->ptr[i]);
843 boot_entry(menu_ptr, arg);
849 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
850 // Load & run kernel.
852 // Returns only on error.
854 void boot_entry(menu_t *menu_ptr, char *arg)
856 void *kernel, *initrd_buf;
857 ssize_t kernel_size = 0, initrd_size = 0;
858 struct initramfs *initrd = NULL;
859 char *file, *cmd_buf;
862 char *s, *s0, *t, *initrd_arg;
864 if(!menu_ptr) return;
866 if(menu_ptr->localboot) {
868 syslinux_local_boot(strtol(menu_ptr->localboot, NULL, 0));
873 file = menu_ptr->alt_kernel;
874 if(!file) file = menu_ptr->kernel;
875 if(!file) file = menu_ptr->linux;
878 asprintf(&cmd_buf, "%s %s", menu_ptr->label, arg);
879 syslinux_run_command(cmd_buf);
883 // first, load kernel
887 if((fd = open(file, O_RDONLY)) >= 0) {
888 if(!fstat(fd, &sbuf) && S_ISREG(sbuf.st_mode)) kernel_size = sbuf.st_size;
892 gfx_progress_init(kernel_size, file);
894 kernel = load_one(file, &kernel_size);
900 if(kernel_size < 1024 || *(uint32_t *) (kernel + 0x202) != 0x53726448) {
901 // not a linux kernel
903 asprintf(&cmd_buf, "%s %s", menu_ptr->label, arg);
904 syslinux_run_command(cmd_buf);
908 // printf("kernel = %p, size = %d\n", kernel, kernel_size);
910 // parse cmdline for "initrd" option
912 initrd_arg = menu_ptr->initrd;
914 s = s0 = strdup(arg);
916 while(*s && strncmp(s, "initrd=", sizeof "initrd=" - 1)) {
917 s = skipspace(skip_nonspaces(s));
921 s += sizeof "initrd=" - 1;
922 *skip_nonspaces(s) = 0;
925 else if(initrd_arg) {
927 initrd_arg = s0 = strdup(initrd_arg);
931 initrd = initramfs_init();
933 while((t = strsep(&initrd_arg, ","))) {
934 initrd_buf = load_one(t, &initrd_size);
937 printf("%s: read error\n", t);
942 initramfs_add_data(initrd, initrd_buf, initrd_size, initrd_size, 4);
944 // printf("initrd = %p, size = %d\n", initrd_buf, initrd_size);
952 syslinux_boot_linux(kernel, kernel_size, initrd, arg);