4 * default:: Set the default entry
5 * fallback:: Set the fallback entry
6 * hiddenmenu:: Hide the menu interface
7 +* gfxmenu:: Use graphical menu interface
8 * timeout:: Set the timeout
9 * title:: Start a menu entry
11 @@ -2150,6 +2151,15 @@
18 +@deffn Command gfxmenu file
19 +Use the graphical menu interface. The graphics data are taken from
20 +@var{file} and must be created using 'mkbootmsg' from the gfxboot package.
25 @subsection hiddenmenu
33 +/* graphical menu functions . */
35 +gfx_init (gfx_data_t *gfx_data)
41 +gfx_done (gfx_data_t *gfx_data)
47 +gfx_input (gfx_data_t *gfx_data, int *menu_entry)
53 +gfx_setup_menu (gfx_data_t *gfx_data)
59 /* low-level timing info */
64 @@ -1614,6 +1614,286 @@
70 + * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
72 + * graphical menu functions
77 + * int gfx_init (gfx_data_t *gfx_data)
97 + leal gfx_ofs_sys_cfg(%edx),%esi
103 + call EXT_C(prot_to_real)
109 + lcall *gfx_ofs_jmp_table + 4 * 0 (%di)
116 + DATA32 call EXT_C(real_to_prot)
124 + movl %ebx,gfx_ofs_ok(%edx)
135 + * int gfx_done (gfx_data_t *gfx_data)
137 + * shut down gfx things
141 + * sets gfx_data->ok
159 + call EXT_C(prot_to_real)
166 + lcall *gfx_ofs_jmp_table + 4 * 1 (%bx)
170 + DATA32 call EXT_C(real_to_prot)
177 + movl %eax,gfx_ofs_ok(%edx)
188 + * int gfx_input (gfx_data_t *gfx_data, int *menu_entry)
190 + * let user enter a command line
192 + * uses gfx_data->cmdline as buffer
197 + * menu_entry: selected entry
210 + leal gfx_ofs_sys_cfg(%edx),%esi
216 + call EXT_C(prot_to_real)
223 + movl gfx_ofs_cmdline(%bx),%edi
224 + movl gfx_ofs_cmdline_len(%bx),%ecx
225 + movl gfx_ofs_timeout(%bx),%eax
228 + lcall *gfx_ofs_jmp_table + 4 * 2 (%bx)
234 + DATA32 call EXT_C(real_to_prot)
253 + * int gfx_setup_menu (gfx_data_t *gfx_data)
261 +/* menu entry descriptor */
262 +#define menu_entries 0
263 +#define menu_default 2 /* seg:ofs */
264 +#define menu_ent_list 6 /* seg:ofs */
265 +#define menu_ent_size 10
266 +#define menu_arg_list 12 /* seg:ofs */
267 +#define menu_arg_size 16
268 +#define sizeof_menu_desc 18
270 +ENTRY(gfx_setup_menu)
283 + call EXT_C(prot_to_real)
291 + subw $sizeof_menu_desc,%sp
294 + movl gfx_ofs_menu_entries(%bx),%eax
295 + movw %ax,menu_entries(%bp)
297 + movl gfx_ofs_menu_default_entry(%bx),%eax
299 + movw %ax,menu_default(%bp)
300 + movw %ds,menu_default+2(%bp)
302 + movl gfx_ofs_menu_list(%bx),%eax
304 + movw %ax,menu_ent_list(%bp)
305 + movw %ds,menu_ent_list+2(%bp)
307 + movl gfx_ofs_menu_entry_len(%bx),%eax
308 + movw %ax,menu_ent_size(%bp)
310 + movl gfx_ofs_args_list(%bx),%eax
312 + movw %ax,menu_arg_list(%bp)
313 + movw %ds,menu_arg_list+2(%bp)
315 + movl gfx_ofs_args_entry_len(%bx),%eax
316 + movw %ax,menu_arg_size(%bp)
322 + lcall %ds: *gfx_ofs_jmp_table + 4 * 3 (%bx)
324 + addw $sizeof_menu_desc,%sp
328 + DATA32 call EXT_C(real_to_prot)
343 + * end graphics stuff
345 + * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
350 * gateA20(int linear)
351 --- stage2/builtins.c
352 +++ stage2/builtins.c
354 int fallback_entries[MAX_FALLBACK_ENTRIES];
355 /* The number of current entry. */
358 +char graphics_file[64];
359 /* The address for Multiboot command-line buffer. */
360 static char *mb_cmdline;
362 @@ -1351,6 +1353,26 @@
368 +gfxmenu_func (char *arg, int flags)
370 + memmove(graphics_file, arg, sizeof graphics_file - 1);
371 + graphics_file[sizeof graphics_file - 1] = 0;
376 +static struct builtin builtin_gfxmenu =
380 + BUILTIN_MENU | BUILTIN_HELP_LIST,
382 + "Use the graphical menu from FILE."
388 geometry_func (char *arg, int flags)
389 @@ -4874,6 +4896,7 @@
400 #endif /* WITHOUT_LIBC_STUBS */
403 +/* see typedef gfx_data_t below */
404 +#define gfx_ofs_ok 0x00
405 +#define gfx_ofs_code_seg 0x04
406 +#define gfx_ofs_jmp_table 0x08
407 +#define gfx_ofs_sys_cfg 0x38
408 +#define gfx_ofs_cmdline 0x6c
409 +#define gfx_ofs_cmdline_len 0x70
410 +#define gfx_ofs_menu_list 0x74
411 +#define gfx_ofs_menu_default_entry 0x78
412 +#define gfx_ofs_menu_entries 0x7c
413 +#define gfx_ofs_menu_entry_len 0x80
414 +#define gfx_ofs_args_list 0x84
415 +#define gfx_ofs_args_entry_len 0x88
416 +#define gfx_ofs_timeout 0x8c
421 * Below this should be ONLY defines and other constructs for C code.
423 extern int default_entry;
424 extern int current_entryno;
428 + * graphics menu stuff
430 + * Note: gfx_data and all data referred to in it must lie within a 64k area.
433 + unsigned ok; /* set while we're in graphics mode */
434 + unsigned code_seg; /* code segment of binary graphics code */
435 + unsigned jmp_table[12]; /* link to graphics functions */
436 + unsigned char sys_cfg[52]; /* sys_cfg[0]: identifies boot loader (grub == 2) */
437 + char *cmdline; /* command line returned by gfx_input() */
438 + unsigned cmdline_len; /* length of the above */
439 + char *menu_list; /* list of menu entries, each of fixed length (menu_entry_len) */
440 + char *menu_default_entry; /* the default entry */
441 + unsigned menu_entries; /* number of entries in menu_list */
442 + unsigned menu_entry_len; /* one entry */
443 + char *args_list; /* same structure as menu_list, menu_entries entries */
444 + unsigned args_entry_len; /* one entry */
445 + unsigned timeout; /* in seconds (0: no timeout) */
446 +} __attribute__ ((packed)) gfx_data_t;
448 +extern gfx_data_t *graphics_data;
450 +/* pointer to graphics image data */
451 +extern char graphics_file[64];
453 +int gfx_init(gfx_data_t *gfx_data);
454 +int gfx_done(gfx_data_t *gfx_data);
455 +int gfx_input(gfx_data_t *gfx_data, int *menu_entry);
456 +int gfx_setup_menu(gfx_data_t *gfx_data);
458 /* The constants for password types. */
465 grub_jmp_buf restart_env;
467 +gfx_data_t *graphics_data;
469 #if defined(PRESET_MENU_STRING) || defined(SUPPORT_DISKLESS)
471 # if defined(PRESET_MENU_STRING)
474 if (! auth && password)
476 + if (*graphics_file)
479 + WARNING: graphical menu doesn\'t work\
480 + in conjunction with the password feature\n" );
483 Press enter to boot the selected OS or \'p\' to enter a\n\
484 password to unlock the next set of features.");
485 @@ -753,6 +761,493 @@
492 +static void hexdump(unsigned char *buf, unsigned len)
496 + unsigned addr = (unsigned) buf;
502 + s[j & 15] = (i >= 0x20 && i <= 0x7e) ? i : '.';
504 + printf("%x ", j + addr);
506 + if(!(j & 7) && (j & 15)) printf(" ");
507 + /* stupid grub_printf */
508 + printf("%x", (i >> 4) & 0x0f);
509 + printf("%x ", i & 0x0f);
511 + printf(" %s\n", s);
517 + if(!(j & 8)) printf(" ");
518 + i = 1 + 3 * (16 - (j & 15));
519 + while(i--) printf(" ");
526 +/* kernel + (grub-)module options */
527 +#define GFX_CMD_BUF_SIZE 512
529 +/* command line separator char */
530 +#define GFX_CMD_SEP 1
533 + * Go through config entry and find kernel args, if any.
534 + * Put things into buf and return it.
536 +static char *get_kernel_args(char *cfg, char *buf)
539 + char *s, *t = "", *p, *t2;
543 + for(j = 0; ; j++) {
544 + s = get_entry(cfg, j, 0);
547 + (!memcmp(s, "kernel", 6) || !memcmp(s, "module", 6)) &&
548 + (s[6] == ' ' || s[6] == '\t')
551 + t2 = s[0] == 'm' ? strstr(t, "initrd") : NULL;
552 + if(*t) t = skip_to(0, t);
553 + if(t2 && t2 < t) break; /* module is likely a normal initrd -> skip */
555 + if(p - buf + i > GFX_CMD_BUF_SIZE - 2) break;
556 + *p++ = GFX_CMD_SEP;
564 + if(*buf) buf++; /* skip initial separator char */
571 + * Check header and return code start offset.
573 +static unsigned magic_ok(unsigned char *buf)
576 + *(unsigned *) buf == 0x0b2d97f00 && /* magic id */
577 + (buf[4] == 8) /* version 8 */
579 + return *(unsigned *) (buf + 8);
587 + * Search cpio archive for gfx file.
589 +static unsigned find_file(unsigned char *buf, unsigned len, unsigned *gfx_file_start, unsigned *file_len)
591 + unsigned i, fname_len, code_start = 0;
593 + *gfx_file_start = 0;
595 + for(i = 0; i < len;) {
596 + if((len - i) >= 0x1a && (buf[i] + (buf[i + 1] << 8)) == 0x71c7) {
597 + fname_len = *(unsigned short *) (buf + i + 20);
598 + *file_len = *(unsigned short *) (buf + i + 24) + (*(unsigned short *) (buf + i + 22) << 16);
599 + i += 26 + fname_len;
600 + i = ((i + 1) & ~1);
601 + if((code_start = magic_ok(buf + i))) {
602 + *gfx_file_start = i;
606 + i = ((i + 1) & ~1);
616 +static inline unsigned char * stack_ptr(void)
620 + asm("movl %%esp, %0" : "=r" (u));
625 +static void sleep(int delay)
627 + int tick, last_tick = currticks();
632 + while((tick = currticks()) == last_tick) { }
637 +static void wait_for_key()
639 + printf("Press a key to continue...");
646 + * Leave that much space on the heap. Everything else goes to the graphics
649 + * 0x2000 is _not_ enough
651 +#define MIN_HEAP_SIZE 0x4000
652 +#define MIN_GFX_FREE 0x1000
654 +#define SC_BOOTLOADER 0
655 +#define SC_FAILSAFE 3
656 +#define SC_SYSCONFIG_SIZE 4
657 +#define SC_BOOTLOADER_SEG 8
658 +#define SC_XMEM_0 24
659 +#define SC_XMEM_1 26
660 +#define SC_XMEM_2 28
661 +#define SC_XMEM_3 30
663 +#define SC_ARCHIVE_START 36
664 +#define SC_ARCHIVE_END 40
665 +#define SC_MEM0_START 44
666 +#define SC_MEM0_END 48
669 + * Does normally not return.
672 +run_graphics_menu (char *menu_entries, char *config_entries, int num_entries,
673 + char *heap, int entryno)
675 + unsigned char *buf, *buf_ext;
676 + unsigned buf_size, buf_ext_size, code_start, file_start;
677 + char *s, *t, *t2, *cfg, *new_config, *p;
679 + int i, j, max_len, gfx_file_size, verbose;
680 + int selected_entry;
681 + gfx_data_t *gfx_data;
683 + unsigned mem0_start, mem0_end, file_len;
686 + * check gfx_data_t struct offsets for consistency; gcc will optimize away
690 + /* dummy function to make ld fail */
692 + extern void wrong_struct_size(void);
693 + #define gfx_ofs_check(a) if(gfx_ofs_##a != (char *) &gfx_data->a - (char *) gfx_data) wrong_struct_size();
695 + gfx_ofs_check(code_seg);
696 + gfx_ofs_check(jmp_table);
697 + gfx_ofs_check(sys_cfg);
698 + gfx_ofs_check(cmdline);
699 + gfx_ofs_check(cmdline_len);
700 + gfx_ofs_check(menu_list);
701 + gfx_ofs_check(menu_default_entry);
702 + gfx_ofs_check(menu_entries);
703 + gfx_ofs_check(menu_entry_len);
704 + gfx_ofs_check(args_list);
705 + gfx_ofs_check(args_entry_len);
706 + gfx_ofs_check(timeout);
707 + #undef gfx_ofs_check
710 + if(!num_entries) return;
712 + graphics_data = gfx_data = (gfx_data_t *) heap;
713 + heap += sizeof *gfx_data;
714 + memset(gfx_data, 0, sizeof *gfx_data);
716 + gfx_data->sys_cfg[SC_BOOTLOADER] = 2; /* bootloader: grub */
717 + gfx_data->sys_cfg[SC_SYSCONFIG_SIZE] = 52; /* config data size */
718 + *(unsigned short *) (gfx_data->sys_cfg + SC_BOOTLOADER_SEG) = (unsigned) gfx_data >> 4; /* segment */
719 + gfx_data->sys_cfg[SC_XMEM_0] = 0x21; /* 1MB @ 2MB */
720 + gfx_data->sys_cfg[SC_XMEM_1] = 0x41; /* 1MB @ 4MB */
721 + verbose = (*(unsigned char *) 0x417) & 3 ? 1 : 0; /* SHIFT pressed */
722 + gfx_data->sys_cfg[SC_FAILSAFE] = verbose;
724 + gfx_data->timeout = grub_timeout >= 0 ? grub_timeout : 0;
727 + /* setup command line edit buffer */
729 + gfx_data->cmdline_len = 256;
731 + gfx_data->cmdline = heap;
732 + heap += gfx_data->cmdline_len;
733 + memset(gfx_data->cmdline, 0, gfx_data->cmdline_len);
736 + heap += GFX_CMD_BUF_SIZE;
738 + /* setup menu entries */
740 + for(i = max_len = 0; i < num_entries; i++) {
741 + j = strlen(get_entry(menu_entries, i, 0));
742 + if(j > max_len) max_len = j;
745 + if(!max_len) return;
747 + gfx_data->menu_entry_len = max_len + 1;
748 + gfx_data->menu_entries = num_entries;
750 + gfx_data->menu_list = heap;
751 + heap += gfx_data->menu_entry_len * gfx_data->menu_entries;
753 + memset(gfx_data->menu_list, 0, gfx_data->menu_entry_len * gfx_data->menu_entries);
755 + for(i = 0; i < (int) gfx_data->menu_entries; i++) {
756 + strcpy(gfx_data->menu_list + i * gfx_data->menu_entry_len, get_entry(menu_entries, i, 0));
759 + gfx_data->menu_default_entry = gfx_data->menu_list + entryno * gfx_data->menu_entry_len;
762 + /* setup list of kernel args */
764 + for(i = max_len = 0; i < num_entries; i++) {
765 + s = get_kernel_args(get_entry(config_entries, i, 1), cmd_buf);
767 + if(j > max_len) max_len = j;
770 + gfx_data->args_entry_len = max_len + 1;
772 + gfx_data->args_list = heap;
773 + heap += gfx_data->args_entry_len * gfx_data->menu_entries;
775 + memset(gfx_data->args_list, 0, gfx_data->args_entry_len * gfx_data->menu_entries);
777 + for(i = 0; i < (int) gfx_data->menu_entries; i++) {
778 + strcpy(gfx_data->args_list + i* gfx_data->args_entry_len, get_kernel_args(get_entry(config_entries, i, 1), cmd_buf));
782 + /* go back here when we no longer need the graphics data */
786 + /* get memory area to be used by graphics functions */
788 + /* use 1MB starting at 2MB as file buffer */
789 + buf_ext = (unsigned char *) (2 << 20);
790 + buf_ext_size = 1 << 20;
792 + /* must be 16-byte aligned */
793 + buf = (unsigned char *) (((unsigned) heap + 0xf) & ~0xf);
795 + buf_size = stack_ptr() - buf - MIN_HEAP_SIZE;
798 + mem0_start = (unsigned) buf;
799 + mem0_end = mem0_start + buf_size;
802 + printf("low memory 0x%x - 0x%x (%d bytes)\n", mem0_start, mem0_end, buf_size);
808 + /* read the file */
810 + if(!grub_open(graphics_file)) {
811 + printf("%s: file not found\n", graphics_file);
817 + gfx_file_size = grub_read(buf_ext, buf_ext_size);
821 + if(gfx_file_size <= 0) {
822 + printf("%s: read error\n", graphics_file);
829 + printf("%s: %d bytes (%d bytes left)\n", graphics_file, gfx_file_size, buf_ext_size - gfx_file_size);
833 + /* locate file inside cpio archive */
834 + if(!(code_start = find_file(buf_ext, gfx_file_size, &file_start, &file_len))) {
835 + printf("%s: invalid file format\n", graphics_file);
842 + printf("init: start 0x%x, len %d; code offset 0x%x\n", file_start, file_len, code_start);
846 + if(file_len - code_start + MIN_GFX_FREE > buf_size) {
847 + printf("not enough free memory: %d extra bytes need\n", file_len - code_start + MIN_GFX_FREE - buf_size);
853 + memcpy((void *) buf, (void *) (buf_ext + file_start + code_start), file_len - code_start);
855 + mem0_start += file_len - code_start;
856 + mem0_start = (mem0_start + 3) & ~3; /* align */
858 + /* init interface to graphics functions */
860 + *(unsigned *) (gfx_data->sys_cfg + SC_FILE) = (unsigned) buf_ext + file_start;
861 + *(unsigned *) (gfx_data->sys_cfg + SC_ARCHIVE_START) = (unsigned) buf_ext;
862 + *(unsigned *) (gfx_data->sys_cfg + SC_ARCHIVE_END) = (unsigned) buf_ext + gfx_file_size;
863 + *(unsigned *) (gfx_data->sys_cfg + SC_MEM0_START) = mem0_start;
864 + *(unsigned *) (gfx_data->sys_cfg + SC_MEM0_END) = mem0_end;
866 + gfx_data->code_seg = (unsigned) buf >> 4;
869 + printf("init 0x%x, archive 0x%x - 0x%x, low mem 0x%x - 0x%x\ncode seg 0x%x\n",
870 + (unsigned) buf_ext + file_start,
871 + (unsigned) buf_ext, (unsigned) buf_ext + gfx_file_size,
872 + mem0_start, mem0_end, gfx_data->code_seg
877 + for(i = 0; (unsigned) i < sizeof gfx_data->jmp_table / sizeof *gfx_data->jmp_table; i++) {
878 + gfx_data->jmp_table[i] = (gfx_data->code_seg << 16) + ((unsigned short *) buf)[i];
882 + for(i = 0; i < 12; i++) {
883 + printf("%d: 0x%x\n", i, gfx_data->jmp_table[i]);
886 + for(i = 0; i < gfx_data->menu_entries; i++) {
887 + printf("\"%s\" -- \"%s\"\n",
888 + gfx_data->menu_list + i * gfx_data->menu_entry_len,
889 + gfx_data->args_list + i * gfx_data->args_entry_len
893 + printf("default: \"%s\"\n", gfx_data->menu_default_entry);
897 + /* switch to graphics mode */
899 + if(gfx_init(gfx_data)) {
900 + printf("graphics initialization failed\n");
906 + gfx_setup_menu(gfx_data);
908 + i = gfx_input(gfx_data, &selected_entry);
910 + /* ESC -> show text menu */
912 + gfx_done(gfx_data);
919 + gfx_done(gfx_data);
921 + heap = saved_heap; /* free most of the graphics data */
923 + // printf("cmdline: >%s<, entry = %d\n", gfx_data->cmdline, selected_entry);
925 + if(selected_entry < 0 || selected_entry > num_entries) return;
928 + /* create new config with modified kernel option */
930 + cfg = get_entry(config_entries, selected_entry, 1);
934 + for(p = gfx_data->cmdline, i = 0; ; i++) {
935 + s = get_entry(cfg, i, 0);
937 + if(!i) *heap++ = 0;
941 + /* note: must match get_kernel_args() */
943 + (!memcmp(s, "kernel", 6) || !memcmp(s, "module", 6)) &&
944 + (s[6] == ' ' || s[6] == '\t')
947 + t2 = s[0] == 'm' ? strstr(t, "initrd") : NULL;
948 + if(*t) t = skip_to(0, t);
949 + if(t2 && t2 < t) { /* module is likely a normal initrd -> skip */
951 + heap += strlen(s) + 1;
954 + memmove(heap, s, t - s);
957 + while(*p && *p != GFX_CMD_SEP) *heap++ = *p++;
959 + if(*p == GFX_CMD_SEP) p++;
963 + heap += strlen(s) + 1;
969 + // hexdump(new_config, heap - new_config);
972 + run_script(new_config, heap);
977 get_line_from_config (char *cmdline, int maxlen, int read_from_file)
979 @@ -1062,9 +1557,12 @@
983 - /* Run menu interface. */
984 - run_menu (menu_entries, config_entries, num_entries,
985 - menu_entries + menu_len, default_entry);
986 + if (*graphics_file && !password && show_menu && grub_timeout)
988 + run_graphics_menu(menu_entries, config_entries, num_entries,menu_entries + menu_len, default_entry);
990 + /* Run menu interface. */
991 + run_menu (menu_entries, config_entries, num_entries, menu_entries + menu_len, default_entry);