From 092fd48a25b748ff9969883815554f7980b34fe3 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Wed, 12 May 2010 15:19:01 +0200 Subject: [PATCH 01/37] legacy parser prototype --- commands/legacycfg.c | 521 +++++++++++++++++++++++++++++++++++++++++++ conf/i386-pc.rmk | 6 + 2 files changed, 527 insertions(+) create mode 100644 commands/legacycfg.c diff --git a/commands/legacycfg.c b/commands/legacycfg.c new file mode 100644 index 000000000..de4631140 --- /dev/null +++ b/commands/legacycfg.c @@ -0,0 +1,521 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2010 Free Software Foundation, Inc. + * + * GRUB 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, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +struct legacy_command +{ + const char *name; + const char *map; + unsigned argc; + enum { + TYPE_VERBATIM, + TYPE_FORCE_OPTION, + TYPE_NOAPM_OPTION, + TYPE_FILE, + TYPE_PARTITION, + TYPE_BOOL, + TYPE_INT + } argt[3]; + enum { + FLAG_IGNORE_REST = 1, + FLAG_ALL_VERBATIM = 2 + } flags; +}; + +struct legacy_command legacy_commands[] = + { + {"blocklist", "blocklist '%s'\n", 1, {TYPE_FILE}, 0}, + {"boot", "boot\n", 0, {}, 0}, + /* bootp unsupported. */ + {"cat", "cat '%s'\n", 1, {TYPE_FILE}, 0}, + {"chainloader", "chainloader %s '%s'\n", 2, {TYPE_FORCE_OPTION, TYPE_FILE}, + 0}, + {"cmp", "cmp '%s' '%s'\n", 2, {TYPE_FILE, TYPE_FILE}, FLAG_IGNORE_REST}, + /* FIXME: Implement command. */ + {"color", "legacy_color '%s' '%s'\n", 2, {TYPE_VERBATIM, TYPE_VERBATIM}, + FLAG_IGNORE_REST}, + {"configfile", "legacy_configfile '%s'\n", 1, {TYPE_FILE}, 0}, + {"debug", + "if [ -z \"$debug\" ]; then set debug=all; else set debug=; fi\n", + 0, {}, 0}, + /* FIXME: Implement command. */ + {"default", "legacy_default %s\n", 1, {TYPE_INT}, 0}, + /* dhcp unsupported. */ + /* displayapm unsupported. */ + {"displaymem", "lsmem\n", 0, {}, 0}, + /* embed unsupported. */ + {"fallback", "set fallback='%s'\n", 1, {TYPE_VERBATIM}, 0}, + {"find", "search -f '%s'\n", 1, {TYPE_FILE}, 0}, + /* fstest unsupported. */ + /* geometry unsupported. */ + {"halt", "halt %s\n", 1, {TYPE_NOAPM_OPTION}, 0}, + /* help unsupported. */ /* NUL_TERMINATE */ + /* hiddenmenu unsupported. */ + {"hide", "parttool '%s' hidden+\n", 1, {TYPE_PARTITION}, 0}, + /* ifconfig unsupported. */ + /* impsprobe unsupported. */ + /* FIXME: Implement command. */ + {"initrd", "legacy_initrd '%s'\n", 1, {TYPE_FILE}, 0}, + /* install unsupported. */ + /* ioprobe unsupported. */ + /* FIXME: implement command. */ + {"kernel", "legacy_kernel %s\n", 0, {}, FLAG_ALL_VERBATIM}, + /* lock is handled separately. */ + {"makeactive", "parttool '%s' boot+\n", 1, {TYPE_PARTITION}, 0}, + {"map", "drivemap '%s' '%s'\n", 2, {TYPE_PARTITION, TYPE_PARTITION}, + FLAG_IGNORE_REST}, + /* md5crypt unsupported. */ + {"module", "legacy_initrd '%s'\n", 1, {TYPE_FILE}, 0}, + /* modulenounzip unsupported. */ + {"pager", "set pager=%d\n", 1, {TYPE_BOOL}, 0}, + /* partnew unsupported. */ + {"parttype", "parttool '%s' type=%s\n", 2, {TYPE_PARTITION, TYPE_INT}, 0}, + /* password unsupported. */ /* NUL_TERMINATE */ + /* pause unsupported. */ + /* rarp unsupported. */ + {"read", "read_dword %s\n", 1, {TYPE_INT}, 0}, + {"reboot", "reboot\n", 0, {}, 0}, + {"root", "set root='%s'\n", 1, {TYPE_PARTITION}, 0}, + {"rootnoverify", "set root='%s'\n", 1, {TYPE_PARTITION}, 0}, + {"savedefault", "saved_entry=${chosen}; save_env saved_entry\n", 0, {}, 0}, + /* serial unsupported. */ + /* setkey unsupported. */ /* NUL_TERMINATE */ + /* setup unsupported. */ + /* terminal unsupported. */ /* NUL_TERMINATE */ + /* terminfo unsupported. */ /* NUL_TERMINATE */ + /* testload unsupported. */ + /* testvbe unsupported. */ + /* tftpserver unsupported. */ + {"timeout", "set timeout=%s\n", 1, {TYPE_INT}, 0}, + /* title is handled separately. */ + {"unhide", "parttool '%s' hidden-\n", 1, {TYPE_PARTITION}, 0}, + /* uppermem unsupported. */ + /* vbeprobe unsupported. */ + }; + +static char * +escape (const char *in) +{ + const char *ptr; + char *ret, *outptr; + int overhead = 0; + for (ptr = in; *ptr; ptr++) + if (*ptr == '\'' || *ptr == '\\') + overhead++; + ret = grub_malloc (ptr - in + overhead); + if (!ret) + return NULL; + outptr = ret; + for (ptr = in; *ptr; ptr++) + { + if (*ptr == '\'' || *ptr == '\\') + *outptr++ = '\\'; + + *outptr++ = *ptr; + } + return ret; +} + +static char * +adjust_file (const char *in) +{ + const char *comma, *ptr, *rest; + char *ret, *outptr; + int overhead = 0; + int part; + if (in[0] != '(') + return escape (in); + for (ptr = in + 1; *ptr && *ptr != ')' && *ptr != ','; ptr++) + if (*ptr == '\'' || *ptr == '\\') + overhead++; + comma = ptr; + if (*comma != ',') + return escape (in); + part = grub_strtoull (comma + 1, (char **) &rest, 0); + for (ptr = rest; *ptr; ptr++) + if (*ptr == '\'' || *ptr == '\\') + overhead++; + + /* 30 is enough for any number. */ + ret = grub_malloc (ptr - in + overhead + 30); + if (!ret) + return NULL; + + outptr = ret; + for (ptr = in; ptr <= comma; ptr++) + { + if (*ptr == '\'' || *ptr == '\\') + *outptr++ = '\\'; + + *outptr++ = *ptr; + } + grub_snprintf (outptr, 30, "%d", part + 1); + while (*outptr) + outptr++; + for (ptr = rest; ptr <= comma; ptr++) + { + if (*ptr == '\'' || *ptr == '\\') + *outptr++ = '\\'; + + *outptr++ = *ptr; + } + return ret; +} + +static char * +legacy_parse (char *buf, char **entryname) +{ + char *ptr; + char *cmdname; + unsigned i, cmdnum; + + for (ptr = buf; *ptr && grub_isspace (*ptr); ptr++); + if ((!*ptr || *ptr == '#') && entryname && *entryname) + return buf; + if (!*ptr || *ptr == '#') + { + grub_free (buf); + return NULL; + } + + cmdname = ptr; + for (ptr = buf; !grub_isspace (*ptr) && *ptr != '='; ptr++); + + if (entryname && grub_strncmp ("title", cmdname, ptr - cmdname) == 0 + && ptr - cmdname == sizeof ("title") - 1) + { + for (; grub_isspace (*ptr) || *ptr == '='; ptr++); + *entryname = grub_strdup (ptr); + grub_free (buf); + return NULL; + } + + if (grub_strncmp ("lock", cmdname, ptr - cmdname) == 0 + && ptr - cmdname == sizeof ("lock") - 1) + { + /* FIXME */ + } + + for (cmdnum = 0; cmdnum < ARRAY_SIZE (legacy_commands); cmdnum++) + if (grub_strncmp (legacy_commands[cmdnum].name, cmdname, ptr - cmdname) == 0 + && legacy_commands[cmdnum].name[ptr - cmdname] == 0) + break; + if (cmdnum == ARRAY_SIZE (legacy_commands)) + return grub_xasprintf ("# Unsupported legacy command: %s\n", buf); + + for (; grub_isspace (*ptr) || *ptr == '='; ptr++); + + char *args[ARRAY_SIZE (legacy_commands[0].argt)]; + memset (args, 0, sizeof (args)); + + if (legacy_commands[cmdnum].flags & FLAG_ALL_VERBATIM) + { + char *arg0 = ptr, *outptr; + int overhead = 3; + while (*ptr) + { + char *curarg; + for (; grub_isspace (*ptr); ptr++); + curarg = ptr; + for (; *ptr && !grub_isspace (*ptr); ptr++) + if (*ptr == '\\' || *ptr == '\'') + overhead++; + if (ptr) + ptr++; + overhead += 3; + } + args[0] = grub_malloc (overhead + (ptr - arg0)); + if (!args[0]) + { + grub_free (buf); + return NULL; + } + ptr = arg0; + outptr = args[0]; + while (*ptr) + { + char *curarg; + for (; grub_isspace (*ptr); ptr++); + curarg = ptr; + if (outptr != args[0]) + *outptr++ = ' '; + *outptr++ = '\''; + for (; *ptr && !grub_isspace (*ptr); ptr++) + { + if (*ptr == '\\' || *ptr == '\'') + *outptr++ = '\\'; + *outptr++ = *ptr; + } + *outptr++ = '\''; + if (ptr) + ptr++; + overhead += 3; + } + } + + { + unsigned j = 0; + for (i = 0; i < legacy_commands[cmdnum].argc; i++) + { + char *curarg, *cptr = NULL, c; + for (; grub_isspace (*ptr); ptr++); + curarg = ptr; + for (; !grub_isspace (*ptr); ptr++); + if (i != legacy_commands[cmdnum].argc - 1 + || (legacy_commands[cmdnum].flags & FLAG_IGNORE_REST)) + { + cptr = ptr; + c = *cptr; + *ptr = 0; + } + ptr++; + switch (legacy_commands[cmdnum].argt[i]) + { + case TYPE_PARTITION: + case TYPE_FILE: + args[j++] = adjust_file (curarg); + break; + + case TYPE_VERBATIM: + args[j++] = escape (curarg); + break; + case TYPE_FORCE_OPTION: + if (grub_strcmp (curarg, "--force") == 0) + { + args[j++] = grub_strdup ("--force"); + break; + } + if (cptr) + *cptr = c; + ptr = curarg; + break; + case TYPE_NOAPM_OPTION: + if (grub_strcmp (curarg, "--no-apm") == 0) + { + args[j++] = grub_strdup ("--no-apm"); + break; + } + if (cptr) + *cptr = c; + ptr = curarg; + break; + case TYPE_INT: + { + char *brk; + int base = 10; + brk = curarg; + if (brk[0] == '0' && brk[1] == 'x') + base = 16; + else if (brk[0] == '0') + base = 8; + for (; *brk; brk++) + { + if (base == 8 && (*brk == '8' || *brk == '9')) + break; + if (grub_isdigit (*brk)) + continue; + if (base != 16) + break; + if (!(*brk >= 'a' && *brk <= 'f') + && !(*brk >= 'A' && *brk <= 'F')) + break; + } + if (brk == curarg) + args[j++] = grub_strdup ("0"); + else + args[j++] = grub_strndup (curarg, brk - curarg); + } + break; + case TYPE_BOOL: + if (curarg[0] == 'o' && curarg[1] == 'n' + && (curarg[2] == 0 || grub_isspace (curarg[2]))) + args[j++] = grub_strdup ("1"); + else + args[j++] = grub_strdup ("0"); + break; + } + } + } + grub_free (buf); + return grub_xasprintf (legacy_commands[cmdnum].map, args[0], args[1], args[2]); +} + +static grub_err_t +legacy_file (const char *filename) +{ + grub_file_t file; + char *entryname = NULL, *entrysrc = NULL; + grub_menu_t menu; + + file = grub_gzfile_open (filename, 1); + if (! file) + return grub_errno; + + menu = grub_env_get_menu (); + if (! menu) + { + menu = grub_zalloc (sizeof (*menu)); + if (! menu) + return grub_errno; + + grub_env_set_menu (menu); + } + + while (1) + { + char *buf = grub_file_getline (file); + char *parsed; + + if (!buf && grub_errno) + { + grub_file_close (file); + return grub_errno; + } + + if (!buf) + break; + + { + char *oldname = NULL; + + oldname = entryname; + parsed = legacy_parse (buf, &entryname); + if (oldname != entryname && oldname) + { + const char **args = grub_malloc (sizeof (args[0])); + if (!args) + { + grub_file_close (file); + return grub_errno; + } + args[0] = oldname; + grub_normal_add_menu_entry (1, args, entrysrc); + } + } + + if (parsed && !entryname) + { + auto grub_err_t getline (char **line, int cont); + grub_err_t getline (char **line __attribute__ ((unused)), + int cont __attribute__ ((unused))) + { + return GRUB_ERR_NONE; + } + + grub_normal_parse_line (parsed, getline); + grub_free (parsed); + } + else if (parsed) + { + if (!entrysrc) + entrysrc = parsed; + else + { + char *t; + + t = entrysrc; + entrysrc = grub_realloc (entrysrc, grub_strlen (entrysrc) + + grub_strlen (parsed) + 1); + if (!entrysrc) + { + grub_free (t); + grub_free (parsed); + return grub_errno; + } + grub_memcpy (entrysrc + grub_strlen (entrysrc), buf, + grub_strlen (parsed) + 1); + grub_free (parsed); + parsed = NULL; + } + } + } + grub_file_close (file); + + if (entryname) + { + const char **args = grub_malloc (sizeof (args[0])); + if (!args) + { + grub_file_close (file); + return grub_errno; + } + args[0] = entryname; + grub_normal_add_menu_entry (1, args, entrysrc); + } + + if (menu && menu->size) + grub_show_menu (menu, 1); + + return GRUB_ERR_NONE; +} + +static grub_err_t +grub_cmd_legacy_source (struct grub_command *cmd __attribute__ ((unused)), + int argc, char **args) +{ + if (argc != 1) + return grub_error (GRUB_ERR_BAD_ARGUMENT, "file name required"); + return legacy_file (args[0]); +} + +static grub_err_t +grub_cmd_legacy_configfile (struct grub_command *cmd __attribute__ ((unused)), + int argc, char **args) +{ + grub_err_t ret; + if (argc != 1) + return grub_error (GRUB_ERR_BAD_ARGUMENT, "file name required"); + + grub_cls (); + grub_env_context_open (1); + + ret = legacy_file (args[0]); + grub_env_context_close (); + + return ret; +} + +static grub_command_t cmd_source, cmd_configfile; + +GRUB_MOD_INIT(legacycfg) +{ + cmd_source = grub_register_command ("legacy_source", + grub_cmd_legacy_source, + N_("FILE"), N_("Parse legacy config")); + cmd_configfile = grub_register_command ("legacy_configfile", + grub_cmd_legacy_configfile, + N_("FILE"), + N_("Parse legacy config")); +} + +GRUB_MOD_FINI(legacycfg) +{ + grub_unregister_command (cmd_source); + grub_unregister_command (cmd_configfile); +} diff --git a/conf/i386-pc.rmk b/conf/i386-pc.rmk index 5ff852f2e..791d64349 100644 --- a/conf/i386-pc.rmk +++ b/conf/i386-pc.rmk @@ -255,6 +255,12 @@ hdparm_mod_SOURCES = commands/hdparm.c lib/hexdump.c hdparm_mod_CFLAGS = $(COMMON_CFLAGS) hdparm_mod_LDFLAGS = $(COMMON_LDFLAGS) +# grub legacy ever supported only i386-pc. +pkglib_MODULES += legacycfg.mod +legacycfg_mod_SOURCES = commands/legacycfg.c +legacycfg_mod_CFLAGS = $(COMMON_CFLAGS) +legacycfg_mod_LDFLAGS = $(COMMON_LDFLAGS) + ifeq ($(enable_efiemu), yes) efiemu32.o: efiemu/runtime/efiemu.c $(TARGET_OBJ2ELF) From 68cc4355f8b31612a4edd7a6d6bb844857e1cfb0 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Wed, 12 May 2010 17:51:22 +0200 Subject: [PATCH 02/37] Fix few bugs and put a cleaner way to handle kernel command --- commands/legacycfg.c | 151 +++++++++++++++++++++++++------------------ 1 file changed, 87 insertions(+), 64 deletions(-) diff --git a/commands/legacycfg.c b/commands/legacycfg.c index de4631140..6069fe746 100644 --- a/commands/legacycfg.c +++ b/commands/legacycfg.c @@ -27,24 +27,26 @@ #include #include #include +#include struct legacy_command { const char *name; const char *map; unsigned argc; - enum { + enum arg_type { TYPE_VERBATIM, TYPE_FORCE_OPTION, TYPE_NOAPM_OPTION, + TYPE_TYPE_OPTION, TYPE_FILE, TYPE_PARTITION, TYPE_BOOL, - TYPE_INT + TYPE_INT, + TYPE_REST_VERBATIM } argt[3]; enum { - FLAG_IGNORE_REST = 1, - FLAG_ALL_VERBATIM = 2 + FLAG_IGNORE_REST = 1 } flags; }; @@ -85,7 +87,8 @@ struct legacy_command legacy_commands[] = /* install unsupported. */ /* ioprobe unsupported. */ /* FIXME: implement command. */ - {"kernel", "legacy_kernel %s\n", 0, {}, FLAG_ALL_VERBATIM}, + {"kernel", "legacy_kernel %s '%s' %s\n", 3, {TYPE_TYPE_OPTION, TYPE_FILE, + TYPE_REST_VERBATIM}, 0}, /* lock is handled separately. */ {"makeactive", "parttool '%s' boot+\n", 1, {TYPE_PARTITION}, 0}, {"map", "drivemap '%s' '%s'\n", 2, {TYPE_PARTITION, TYPE_PARTITION}, @@ -116,6 +119,7 @@ struct legacy_command legacy_commands[] = /* title is handled separately. */ {"unhide", "parttool '%s' hidden-\n", 1, {TYPE_PARTITION}, 0}, /* uppermem unsupported. */ + {"uuid", "search -u '%s'\n", 1, {TYPE_VERBATIM}, 0}, /* vbeprobe unsupported. */ }; @@ -139,6 +143,7 @@ escape (const char *in) *outptr++ = *ptr; } + *outptr++ = 0; return ret; } @@ -188,6 +193,27 @@ adjust_file (const char *in) return ret; } +static int +is_option (enum arg_type opt, const char *curarg) +{ + switch (opt) + { + case TYPE_NOAPM_OPTION: + return grub_strcmp (curarg, "--no-apm") == 0; + case TYPE_FORCE_OPTION: + return grub_strcmp (curarg, "--force") == 0; + case TYPE_TYPE_OPTION: + return grub_strcmp (curarg, "--type=netbsd") == 0 + || grub_strcmp (curarg, "--type=freebsd") == 0 + || grub_strcmp (curarg, "--type=openbsd") == 0 + || grub_strcmp (curarg, "--type=linux") == 0 + || grub_strcmp (curarg, "--type=biglinux") == 0 + || grub_strcmp (curarg, "--type=multiboot") == 0; + default: + return 0; + } +} + static char * legacy_parse (char *buf, char **entryname) { @@ -197,7 +223,11 @@ legacy_parse (char *buf, char **entryname) for (ptr = buf; *ptr && grub_isspace (*ptr); ptr++); if ((!*ptr || *ptr == '#') && entryname && *entryname) - return buf; + { + char *ret = grub_xasprintf ("%s\n", buf); + grub_free (buf); + return ret; + } if (!*ptr || *ptr == '#') { grub_free (buf); @@ -234,51 +264,6 @@ legacy_parse (char *buf, char **entryname) char *args[ARRAY_SIZE (legacy_commands[0].argt)]; memset (args, 0, sizeof (args)); - if (legacy_commands[cmdnum].flags & FLAG_ALL_VERBATIM) - { - char *arg0 = ptr, *outptr; - int overhead = 3; - while (*ptr) - { - char *curarg; - for (; grub_isspace (*ptr); ptr++); - curarg = ptr; - for (; *ptr && !grub_isspace (*ptr); ptr++) - if (*ptr == '\\' || *ptr == '\'') - overhead++; - if (ptr) - ptr++; - overhead += 3; - } - args[0] = grub_malloc (overhead + (ptr - arg0)); - if (!args[0]) - { - grub_free (buf); - return NULL; - } - ptr = arg0; - outptr = args[0]; - while (*ptr) - { - char *curarg; - for (; grub_isspace (*ptr); ptr++); - curarg = ptr; - if (outptr != args[0]) - *outptr++ = ' '; - *outptr++ = '\''; - for (; *ptr && !grub_isspace (*ptr); ptr++) - { - if (*ptr == '\\' || *ptr == '\'') - *outptr++ = '\\'; - *outptr++ = *ptr; - } - *outptr++ = '\''; - if (ptr) - ptr++; - overhead += 3; - } - } - { unsigned j = 0; for (i = 0; i < legacy_commands[cmdnum].argc; i++) @@ -294,7 +279,8 @@ legacy_parse (char *buf, char **entryname) c = *cptr; *ptr = 0; } - ptr++; + if (*ptr) + ptr++; switch (legacy_commands[cmdnum].argt[i]) { case TYPE_PARTITION: @@ -302,28 +288,65 @@ legacy_parse (char *buf, char **entryname) args[j++] = adjust_file (curarg); break; + case TYPE_REST_VERBATIM: + { + char *outptr, *outptr0; + int overhead = 3; + ptr = curarg; + while (*ptr) + { + for (; grub_isspace (*ptr); ptr++); + for (; *ptr && !grub_isspace (*ptr); ptr++) + if (*ptr == '\\' || *ptr == '\'') + overhead++; + if (*ptr) + ptr++; + overhead += 3; + } + outptr0 = args[j++] = grub_malloc (overhead + (ptr - curarg)); + if (!outptr0) + { + grub_free (buf); + return NULL; + } + ptr = curarg; + outptr = outptr0; + while (*ptr) + { + for (; grub_isspace (*ptr); ptr++); + if (outptr != outptr0) + *outptr++ = ' '; + *outptr++ = '\''; + for (; *ptr && !grub_isspace (*ptr); ptr++) + { + if (*ptr == '\\' || *ptr == '\'') + *outptr++ = '\\'; + *outptr++ = *ptr; + } + *outptr++ = '\''; + if (*ptr) + ptr++; + overhead += 3; + } + *outptr++ = 0; + } + break; + case TYPE_VERBATIM: args[j++] = escape (curarg); break; case TYPE_FORCE_OPTION: - if (grub_strcmp (curarg, "--force") == 0) - { - args[j++] = grub_strdup ("--force"); - break; - } - if (cptr) - *cptr = c; - ptr = curarg; - break; case TYPE_NOAPM_OPTION: - if (grub_strcmp (curarg, "--no-apm") == 0) + case TYPE_TYPE_OPTION: + if (is_option (legacy_commands[cmdnum].argt[i], curarg)) { - args[j++] = grub_strdup ("--no-apm"); + args[j++] = grub_strdup (curarg); break; } if (cptr) *cptr = c; ptr = curarg; + args[j++] = ""; break; case TYPE_INT: { @@ -448,7 +471,7 @@ legacy_file (const char *filename) grub_free (parsed); return grub_errno; } - grub_memcpy (entrysrc + grub_strlen (entrysrc), buf, + grub_memcpy (entrysrc + grub_strlen (entrysrc), parsed, grub_strlen (parsed) + 1); grub_free (parsed); parsed = NULL; From b07e88dc6add00f8860d2ab7b47dbf4d2982b38d Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Wed, 12 May 2010 17:55:37 +0200 Subject: [PATCH 03/37] default support --- commands/legacycfg.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/commands/legacycfg.c b/commands/legacycfg.c index 6069fe746..46637c301 100644 --- a/commands/legacycfg.c +++ b/commands/legacycfg.c @@ -66,8 +66,7 @@ struct legacy_command legacy_commands[] = {"debug", "if [ -z \"$debug\" ]; then set debug=all; else set debug=; fi\n", 0, {}, 0}, - /* FIXME: Implement command. */ - {"default", "legacy_default %s\n", 1, {TYPE_INT}, 0}, + {"default", "set default='%s'; if [ x\"$default\" = xsaved ]; then load_env; set default=\"$saved_entry\"\n", 1, {TYPE_VERBATIM}, 0}, /* dhcp unsupported. */ /* displayapm unsupported. */ {"displaymem", "lsmem\n", 0, {}, 0}, From 67cb07a31bfc7269af7c3f411f636d8d275e72eb Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Wed, 12 May 2010 18:13:43 +0200 Subject: [PATCH 04/37] fix more bugs --- commands/legacycfg.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/commands/legacycfg.c b/commands/legacycfg.c index 46637c301..aaaac8112 100644 --- a/commands/legacycfg.c +++ b/commands/legacycfg.c @@ -66,10 +66,10 @@ struct legacy_command legacy_commands[] = {"debug", "if [ -z \"$debug\" ]; then set debug=all; else set debug=; fi\n", 0, {}, 0}, - {"default", "set default='%s'; if [ x\"$default\" = xsaved ]; then load_env; set default=\"$saved_entry\"\n", 1, {TYPE_VERBATIM}, 0}, + {"default", "set default='%s'; if [ x\"$default\" = xsaved ]; then load_env; set default=\"$saved_entry\"; fi\n", 1, {TYPE_VERBATIM}, 0}, /* dhcp unsupported. */ /* displayapm unsupported. */ - {"displaymem", "lsmem\n", 0, {}, 0}, + {"displaymem", "lsmmap\n", 0, {}, 0}, /* embed unsupported. */ {"fallback", "set fallback='%s'\n", 1, {TYPE_VERBATIM}, 0}, {"find", "search -f '%s'\n", 1, {TYPE_FILE}, 0}, @@ -234,7 +234,7 @@ legacy_parse (char *buf, char **entryname) } cmdname = ptr; - for (ptr = buf; !grub_isspace (*ptr) && *ptr != '='; ptr++); + for (ptr = buf; *ptr && !grub_isspace (*ptr) && *ptr != '='; ptr++); if (entryname && grub_strncmp ("title", cmdname, ptr - cmdname) == 0 && ptr - cmdname == sizeof ("title") - 1) @@ -267,7 +267,7 @@ legacy_parse (char *buf, char **entryname) unsigned j = 0; for (i = 0; i < legacy_commands[cmdnum].argc; i++) { - char *curarg, *cptr = NULL, c; + char *curarg, *cptr = NULL, c = 0; for (; grub_isspace (*ptr); ptr++); curarg = ptr; for (; !grub_isspace (*ptr); ptr++); @@ -451,6 +451,7 @@ legacy_file (const char *filename) } grub_normal_parse_line (parsed, getline); + grub_print_error (); grub_free (parsed); } else if (parsed) From a408e05187d5411e623c602e936df054c1934663 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Wed, 12 May 2010 18:24:02 +0200 Subject: [PATCH 05/37] Add serial command --- commands/legacycfg.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/commands/legacycfg.c b/commands/legacycfg.c index aaaac8112..8f02a028f 100644 --- a/commands/legacycfg.c +++ b/commands/legacycfg.c @@ -106,7 +106,7 @@ struct legacy_command legacy_commands[] = {"root", "set root='%s'\n", 1, {TYPE_PARTITION}, 0}, {"rootnoverify", "set root='%s'\n", 1, {TYPE_PARTITION}, 0}, {"savedefault", "saved_entry=${chosen}; save_env saved_entry\n", 0, {}, 0}, - /* serial unsupported. */ + {"serial", "serial %s\n", 1, {TYPE_REST_VERBATIM}, 0}, /* setkey unsupported. */ /* NUL_TERMINATE */ /* setup unsupported. */ /* terminal unsupported. */ /* NUL_TERMINATE */ From 8f937bfe07fd622bb982ca218e5e8e67345d7dba Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Wed, 12 May 2010 21:02:41 +0200 Subject: [PATCH 06/37] Fix makeactive syntax --- commands/legacycfg.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/commands/legacycfg.c b/commands/legacycfg.c index 8f02a028f..8077497f3 100644 --- a/commands/legacycfg.c +++ b/commands/legacycfg.c @@ -89,7 +89,7 @@ struct legacy_command legacy_commands[] = {"kernel", "legacy_kernel %s '%s' %s\n", 3, {TYPE_TYPE_OPTION, TYPE_FILE, TYPE_REST_VERBATIM}, 0}, /* lock is handled separately. */ - {"makeactive", "parttool '%s' boot+\n", 1, {TYPE_PARTITION}, 0}, + {"makeactive", "parttool \"$root\" boot+\n", 0, {}, 0}, {"map", "drivemap '%s' '%s'\n", 2, {TYPE_PARTITION, TYPE_PARTITION}, FLAG_IGNORE_REST}, /* md5crypt unsupported. */ From ea9ed87faa8d90d112b2e648046c5a2b0cd27a00 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Fri, 27 Aug 2010 18:52:17 +0200 Subject: [PATCH 07/37] add help descriptions to legacy commands --- commands/legacycfg.c | 168 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 137 insertions(+), 31 deletions(-) diff --git a/commands/legacycfg.c b/commands/legacycfg.c index 8077497f3..10e897226 100644 --- a/commands/legacycfg.c +++ b/commands/legacycfg.c @@ -48,65 +48,166 @@ struct legacy_command enum { FLAG_IGNORE_REST = 1 } flags; + const char *shortdesc; + const char *longdesc; }; struct legacy_command legacy_commands[] = { - {"blocklist", "blocklist '%s'\n", 1, {TYPE_FILE}, 0}, - {"boot", "boot\n", 0, {}, 0}, + {"blocklist", "blocklist '%s'\n", 1, {TYPE_FILE}, 0, "FILE", + "Print the blocklist notation of the file FILE." + }, + {"boot", "boot\n", 0, {}, 0, 0, + "Boot the OS/chain-loader which has been loaded."}, /* bootp unsupported. */ - {"cat", "cat '%s'\n", 1, {TYPE_FILE}, 0}, + {"cat", "cat '%s'\n", 1, {TYPE_FILE}, 0, "FILE", + "Print the contents of the file FILE."}, {"chainloader", "chainloader %s '%s'\n", 2, {TYPE_FORCE_OPTION, TYPE_FILE}, - 0}, - {"cmp", "cmp '%s' '%s'\n", 2, {TYPE_FILE, TYPE_FILE}, FLAG_IGNORE_REST}, + 0, "[--force] FILE", + "Load the chain-loader FILE. If --force is specified, then load it" + " forcibly, whether the boot loader signature is present or not."}, + {"cmp", "cmp '%s' '%s'\n", 2, {TYPE_FILE, TYPE_FILE}, FLAG_IGNORE_REST, + "FILE1 FILE2", + "Compare the file FILE1 with the FILE2 and inform the different values" + " if any."}, /* FIXME: Implement command. */ {"color", "legacy_color '%s' '%s'\n", 2, {TYPE_VERBATIM, TYPE_VERBATIM}, - FLAG_IGNORE_REST}, - {"configfile", "legacy_configfile '%s'\n", 1, {TYPE_FILE}, 0}, + FLAG_IGNORE_REST, "NORMAL [HIGHLIGHT]", + "Change the menu colors. The color NORMAL is used for most" + " lines in the menu, and the color HIGHLIGHT is used to highlight the" + " line where the cursor points. If you omit HIGHLIGHT, then the" + " inverted color of NORMAL is used for the highlighted line." + " The format of a color is \"FG/BG\". FG and BG are symbolic color names." + " A symbolic color name must be one of these: black, blue, green," + " cyan, red, magenta, brown, light-gray, dark-gray, light-blue," + " light-green, light-cyan, light-red, light-magenta, yellow and white." + " But only the first eight names can be used for BG. You can prefix" + " \"blink-\" to FG if you want a blinking foreground color."}, + {"configfile", "legacy_configfile '%s'\n", 1, {TYPE_FILE}, 0, "FILE", + "Load FILE as the configuration file."}, {"debug", "if [ -z \"$debug\" ]; then set debug=all; else set debug=; fi\n", - 0, {}, 0}, - {"default", "set default='%s'; if [ x\"$default\" = xsaved ]; then load_env; set default=\"$saved_entry\"; fi\n", 1, {TYPE_VERBATIM}, 0}, + 0, {}, 0, 0, "Turn on/off the debug mode."}, + {"default", + "set default='%s'; if [ x\"$default\" = xsaved ]; then load_env; " + "set default=\"$saved_entry\"; fi\n", 1, {TYPE_VERBATIM}, 0, + "[NUM | `saved']", + "Set the default entry to entry number NUM (if not specified, it is" + " 0, the first entry) or the entry number saved by savedefault."}, /* dhcp unsupported. */ /* displayapm unsupported. */ - {"displaymem", "lsmmap\n", 0, {}, 0}, + {"displaymem", "lsmmap\n", 0, {}, 0, 0, + "Display what GRUB thinks the system address space map of the" + " machine is, including all regions of physical RAM installed."}, /* embed unsupported. */ - {"fallback", "set fallback='%s'\n", 1, {TYPE_VERBATIM}, 0}, - {"find", "search -f '%s'\n", 1, {TYPE_FILE}, 0}, + {"fallback", "set fallback='%s'\n", 1, {TYPE_VERBATIM}, 0, "NUM...", + "Go into unattended boot mode: if the default boot entry has any" + " errors, instead of waiting for the user to do anything, it" + " immediately starts over using the NUM entry (same numbering as the" + " `default' command). This obviously won't help if the machine" + " was rebooted by a kernel that GRUB loaded."}, + {"find", "search -f '%s'\n", 1, {TYPE_FILE}, 0, "FILENAME", + "Search for the filename FILENAME in all of partitions and print the list of" + " the devices which contain the file."}, /* fstest unsupported. */ /* geometry unsupported. */ - {"halt", "halt %s\n", 1, {TYPE_NOAPM_OPTION}, 0}, + {"halt", "halt %s\n", 1, {TYPE_NOAPM_OPTION}, 0, "[--no-apm]", + "Halt your system. If APM is available on it, turn off the power using" + " the APM BIOS, unless you specify the option `--no-apm'."}, /* help unsupported. */ /* NUL_TERMINATE */ /* hiddenmenu unsupported. */ - {"hide", "parttool '%s' hidden+\n", 1, {TYPE_PARTITION}, 0}, + {"hide", "parttool '%s' hidden+\n", 1, {TYPE_PARTITION}, 0, "PARTITION", + "Hide PARTITION by setting the \"hidden\" bit in" + " its partition type code."}, /* ifconfig unsupported. */ /* impsprobe unsupported. */ /* FIXME: Implement command. */ - {"initrd", "legacy_initrd '%s'\n", 1, {TYPE_FILE}, 0}, + {"initrd", "legacy_initrd '%s' %s\n", 2, {TYPE_FILE, TYPE_REST_VERBATIM}, 0, + "FILE [ARG ...]", + "Load an initial ramdisk FILE for a Linux format boot image and set the" + " appropriate parameters in the Linux setup area in memory."}, /* install unsupported. */ /* ioprobe unsupported. */ /* FIXME: implement command. */ - {"kernel", "legacy_kernel %s '%s' %s\n", 3, {TYPE_TYPE_OPTION, TYPE_FILE, - TYPE_REST_VERBATIM}, 0}, + {"kernel", "legacy_kernel %s '%s' %s\n", 4, {TYPE_TYPE_OR_NOMEM_OPTION, + TYPE_TYPE_OR_NOMEM_OPTION, + TYPE_FILE, + TYPE_REST_VERBATIM}, 0, + "[--no-mem-option] [--type=TYPE] FILE [ARG ...]", + "Attempt to load the primary boot image from FILE. The rest of the" + " line is passed verbatim as the \"kernel command line\". Any modules" + " must be reloaded after using this command. The option --type is used" + " to suggest what type of kernel to be loaded. TYPE must be either of" + " \"netbsd\", \"freebsd\", \"openbsd\", \"linux\", \"biglinux\" and" + " \"multiboot\". The option --no-mem-option tells GRUB not to pass a" + " Linux's mem option automatically."}, /* lock is handled separately. */ - {"makeactive", "parttool \"$root\" boot+\n", 0, {}, 0}, + {"makeactive", "parttool \"$root\" boot+\n", 0, {}, 0, 0 + "Set the active partition on the root disk to GRUB's root device." + " This command is limited to _primary_ PC partitions on a hard disk."}, {"map", "drivemap '%s' '%s'\n", 2, {TYPE_PARTITION, TYPE_PARTITION}, - FLAG_IGNORE_REST}, + FLAG_IGNORE_REST, "TO_DRIVE FROM_DRIVE", + "Map the drive FROM_DRIVE to the drive TO_DRIVE. This is necessary" + " when you chain-load some operating systems, such as DOS, if such an" + " OS resides at a non-first drive."}, /* md5crypt unsupported. */ - {"module", "legacy_initrd '%s'\n", 1, {TYPE_FILE}, 0}, + {"module", "legacy_initrd '%s' %s\n", 1, {TYPE_FILE, TYPE_REST_VERBATIM}, 0, + "FILE [ARG ...]", + "Load a boot module FILE for a Multiboot format boot image (no" + " interpretation of the file contents is made, so users of this" + " command must know what the kernel in question expects). The" + " rest of the line is passed as the \"module command line\", like" + " the `kernel' command."}, /* modulenounzip unsupported. */ - {"pager", "set pager=%d\n", 1, {TYPE_BOOL}, 0}, + /* FIXME: allow toggle. */ + {"pager", "set pager=%d\n", 1, {TYPE_BOOL}, 0, "[FLAG]", + "Toggle pager mode with no argument. If FLAG is given and its value" + " is `on', turn on the mode. If FLAG is `off', turn off the mode."}, /* partnew unsupported. */ - {"parttype", "parttool '%s' type=%s\n", 2, {TYPE_PARTITION, TYPE_INT}, 0}, + {"parttype", "parttool '%s' type=%s\n", 2, {TYPE_PARTITION, TYPE_INT}, 0, + "PART TYPE", "Change the type of the partition PART to TYPE."}, /* password unsupported. */ /* NUL_TERMINATE */ /* pause unsupported. */ /* rarp unsupported. */ - {"read", "read_dword %s\n", 1, {TYPE_INT}, 0}, - {"reboot", "reboot\n", 0, {}, 0}, - {"root", "set root='%s'\n", 1, {TYPE_PARTITION}, 0}, - {"rootnoverify", "set root='%s'\n", 1, {TYPE_PARTITION}, 0}, - {"savedefault", "saved_entry=${chosen}; save_env saved_entry\n", 0, {}, 0}, - {"serial", "serial %s\n", 1, {TYPE_REST_VERBATIM}, 0}, + {"read", "read_dword %s\n", 1, {TYPE_INT}, 0, "ADDR", + "Read a 32-bit value from memory at address ADDR and" + " display it in hex format."}, + {"reboot", "reboot\n", 0, {}, 0, 0, "Reboot your system."}, + /* FIXME: Support HDBIAS. */ + {"root", "set root='%s'\n", 1, {TYPE_PARTITION}, 0, "[DEVICE [HDBIAS]]", + "Set the current \"root device\" to the device DEVICE, then" + " attempt to mount it to get the partition size (for passing the" + " partition descriptor in `ES:ESI', used by some chain-loaded" + " bootloaders), the BSD drive-type (for booting BSD kernels using" + " their native boot format), and correctly determine " + " the PC partition where a BSD sub-partition is located. The" + " optional HDBIAS parameter is a number to tell a BSD kernel" + " how many BIOS drive numbers are on controllers before the current" + " one. For example, if there is an IDE disk and a SCSI disk, and your" + " FreeBSD root partition is on the SCSI disk, then use a `1' for HDBIAS."}, + {"rootnoverify", "set root='%s'\n", 1, {TYPE_PARTITION}, 0, + "[DEVICE [HDBIAS]]", + "Similar to `root', but don't attempt to mount the partition. This" + " is useful for when an OS is outside of the area of the disk that" + " GRUB can read, but setting the correct root device is still" + " desired. Note that the items mentioned in `root' which" + " derived from attempting the mount will NOT work correctly."}, + /* FIXME: support arguments. */ + {"savedefault", "saved_entry=${chosen}; save_env saved_entry\n", 0, {}, 0, + "[NUM | `fallback']", + "Save the current entry as the default boot entry if no argument is" + " specified. If a number is specified, this number is saved. If" + " `fallback' is used, next fallback entry is saved."}, + {"serial", "serial %s\n", 1, {TYPE_REST_VERBATIM}, 0, + "[--unit=UNIT] [--port=PORT] [--speed=SPEED] [--word=WORD] " + "[--parity=PARITY] [--stop=STOP] [--device=DEV]", + "Initialize a serial device. UNIT is a digit that specifies which serial" + " device is used (e.g. 0 == COM1). If you need to specify the port number," + " set it by --port. SPEED is the DTE-DTE speed. WORD is the word length," + " PARITY is the type of parity, which is one of `no', `odd' and `even'." + " STOP is the length of stop bit(s). The option --device can be used only" + " in the grub shell, which specifies the file name of a tty device. The" + " default values are COM1, 9600, 8N1."}, /* setkey unsupported. */ /* NUL_TERMINATE */ /* setup unsupported. */ /* terminal unsupported. */ /* NUL_TERMINATE */ @@ -114,11 +215,16 @@ struct legacy_command legacy_commands[] = /* testload unsupported. */ /* testvbe unsupported. */ /* tftpserver unsupported. */ - {"timeout", "set timeout=%s\n", 1, {TYPE_INT}, 0}, + {"timeout", "set timeout=%s\n", 1, {TYPE_INT}, 0, "SEC", + "Set a timeout, in SEC seconds, before automatically booting the" + " default entry (normally the first entry defined)."}, /* title is handled separately. */ - {"unhide", "parttool '%s' hidden-\n", 1, {TYPE_PARTITION}, 0}, + {"unhide", "parttool '%s' hidden-\n", 1, {TYPE_PARTITION}, 0, "PARTITION", + "Unhide PARTITION by clearing the \"hidden\" bit in its" + " partition type code."}, /* uppermem unsupported. */ - {"uuid", "search -u '%s'\n", 1, {TYPE_VERBATIM}, 0}, + {"uuid", "search -u '%s'\n", 1, {TYPE_VERBATIM}, 0, "UUID", + "Find root by UUID"}, /* vbeprobe unsupported. */ }; From fff175c77f6490ace62df776660c7c75ea526775 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Fri, 27 Aug 2010 20:04:49 +0200 Subject: [PATCH 08/37] Implement grub-menulst2cfg and fix many bugs in legacy_parser --- Makefile.util.def | 9 + grub-core/Makefile.core.def | 3 +- grub-core/commands/legacycfg.c | 470 +------------------------------ grub-core/lib/legacy_parse.c | 496 +++++++++++++++++++++++++++++++++ include/grub/legacy_parse.h | 27 ++ util/grub-menulst2cfg.c | 99 +++++++ 6 files changed, 636 insertions(+), 468 deletions(-) create mode 100644 grub-core/lib/legacy_parse.c create mode 100644 include/grub/legacy_parse.h create mode 100644 util/grub-menulst2cfg.c diff --git a/Makefile.util.def b/Makefile.util.def index fd3428e76..b4f9fc60e 100644 --- a/Makefile.util.def +++ b/Makefile.util.def @@ -508,3 +508,12 @@ program = { ldadd = libgrub.a; ldflags = '$(LIBDEVMAPPER)'; }; + +program = { + name = grub-menulst2cfg; + mansection = 1; + common = util/grub-menulst2cfg.c; + common = grub-core/lib/legacy_parse.c; + ldadd = libgrub.a; + ldflags = '$(LIBDEVMAPPER)'; +}; diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def index 5ddb99fa6..0ca28eb19 100644 --- a/grub-core/Makefile.core.def +++ b/grub-core/Makefile.core.def @@ -1407,5 +1407,6 @@ module = { module = { name = legacycfg; common = commands/legacycfg.c; + common = lib/legacy_parse.c; enable = i386_pc; -}; \ No newline at end of file +}; diff --git a/grub-core/commands/legacycfg.c b/grub-core/commands/legacycfg.c index 772f48c15..4207531ec 100644 --- a/grub-core/commands/legacycfg.c +++ b/grub-core/commands/legacycfg.c @@ -28,472 +28,7 @@ #include #include #include - -struct legacy_command -{ - const char *name; - const char *map; - unsigned argc; - enum arg_type { - TYPE_VERBATIM, - TYPE_FORCE_OPTION, - TYPE_NOAPM_OPTION, - TYPE_TYPE_OR_NOMEM_OPTION, - TYPE_FILE, - TYPE_PARTITION, - TYPE_BOOL, - TYPE_INT, - TYPE_REST_VERBATIM - } argt[4]; - enum { - FLAG_IGNORE_REST = 1 - } flags; - const char *shortdesc; - const char *longdesc; -}; - -struct legacy_command legacy_commands[] = - { - {"blocklist", "blocklist '%s'\n", 1, {TYPE_FILE}, 0, "FILE", - "Print the blocklist notation of the file FILE." - }, - {"boot", "boot\n", 0, {}, 0, 0, - "Boot the OS/chain-loader which has been loaded."}, - /* bootp unsupported. */ - {"cat", "cat '%s'\n", 1, {TYPE_FILE}, 0, "FILE", - "Print the contents of the file FILE."}, - {"chainloader", "chainloader %s '%s'\n", 2, {TYPE_FORCE_OPTION, TYPE_FILE}, - 0, "[--force] FILE", - "Load the chain-loader FILE. If --force is specified, then load it" - " forcibly, whether the boot loader signature is present or not."}, - {"cmp", "cmp '%s' '%s'\n", 2, {TYPE_FILE, TYPE_FILE}, FLAG_IGNORE_REST, - "FILE1 FILE2", - "Compare the file FILE1 with the FILE2 and inform the different values" - " if any."}, - /* FIXME: Implement command. */ - {"color", "legacy_color '%s' '%s'\n", 2, {TYPE_VERBATIM, TYPE_VERBATIM}, - FLAG_IGNORE_REST, "NORMAL [HIGHLIGHT]", - "Change the menu colors. The color NORMAL is used for most" - " lines in the menu, and the color HIGHLIGHT is used to highlight the" - " line where the cursor points. If you omit HIGHLIGHT, then the" - " inverted color of NORMAL is used for the highlighted line." - " The format of a color is \"FG/BG\". FG and BG are symbolic color names." - " A symbolic color name must be one of these: black, blue, green," - " cyan, red, magenta, brown, light-gray, dark-gray, light-blue," - " light-green, light-cyan, light-red, light-magenta, yellow and white." - " But only the first eight names can be used for BG. You can prefix" - " \"blink-\" to FG if you want a blinking foreground color."}, - {"configfile", "legacy_configfile '%s'\n", 1, {TYPE_FILE}, 0, "FILE", - "Load FILE as the configuration file."}, - {"debug", - "if [ -z \"$debug\" ]; then set debug=all; else set debug=; fi\n", - 0, {}, 0, 0, "Turn on/off the debug mode."}, - {"default", - "set default='%s'; if [ x\"$default\" = xsaved ]; then load_env; " - "set default=\"$saved_entry\"; fi\n", 1, {TYPE_VERBATIM}, 0, - "[NUM | `saved']", - "Set the default entry to entry number NUM (if not specified, it is" - " 0, the first entry) or the entry number saved by savedefault."}, - /* dhcp unsupported. */ - /* displayapm unsupported. */ - {"displaymem", "lsmmap\n", 0, {}, 0, 0, - "Display what GRUB thinks the system address space map of the" - " machine is, including all regions of physical RAM installed."}, - /* embed unsupported. */ - {"fallback", "set fallback='%s'\n", 1, {TYPE_VERBATIM}, 0, "NUM...", - "Go into unattended boot mode: if the default boot entry has any" - " errors, instead of waiting for the user to do anything, it" - " immediately starts over using the NUM entry (same numbering as the" - " `default' command). This obviously won't help if the machine" - " was rebooted by a kernel that GRUB loaded."}, - {"find", "search -f '%s'\n", 1, {TYPE_FILE}, 0, "FILENAME", - "Search for the filename FILENAME in all of partitions and print the list of" - " the devices which contain the file."}, - /* fstest unsupported. */ - /* geometry unsupported. */ - {"halt", "halt %s\n", 1, {TYPE_NOAPM_OPTION}, 0, "[--no-apm]", - "Halt your system. If APM is available on it, turn off the power using" - " the APM BIOS, unless you specify the option `--no-apm'."}, - /* help unsupported. */ /* NUL_TERMINATE */ - /* hiddenmenu unsupported. */ - {"hide", "parttool '%s' hidden+\n", 1, {TYPE_PARTITION}, 0, "PARTITION", - "Hide PARTITION by setting the \"hidden\" bit in" - " its partition type code."}, - /* ifconfig unsupported. */ - /* impsprobe unsupported. */ - /* FIXME: Implement command. */ - {"initrd", "legacy_initrd '%s' %s\n", 2, {TYPE_FILE, TYPE_REST_VERBATIM}, 0, - "FILE [ARG ...]", - "Load an initial ramdisk FILE for a Linux format boot image and set the" - " appropriate parameters in the Linux setup area in memory."}, - /* install unsupported. */ - /* ioprobe unsupported. */ - /* FIXME: implement command. */ - {"kernel", "legacy_kernel %s '%s' %s\n", 4, {TYPE_TYPE_OR_NOMEM_OPTION, - TYPE_TYPE_OR_NOMEM_OPTION, - TYPE_FILE, - TYPE_REST_VERBATIM}, 0, - "[--no-mem-option] [--type=TYPE] FILE [ARG ...]", - "Attempt to load the primary boot image from FILE. The rest of the" - " line is passed verbatim as the \"kernel command line\". Any modules" - " must be reloaded after using this command. The option --type is used" - " to suggest what type of kernel to be loaded. TYPE must be either of" - " \"netbsd\", \"freebsd\", \"openbsd\", \"linux\", \"biglinux\" and" - " \"multiboot\". The option --no-mem-option tells GRUB not to pass a" - " Linux's mem option automatically."}, - /* lock is handled separately. */ - {"makeactive", "parttool \"$root\" boot+\n", 0, {}, 0, 0, - "Set the active partition on the root disk to GRUB's root device." - " This command is limited to _primary_ PC partitions on a hard disk."}, - {"map", "drivemap '%s' '%s'\n", 2, {TYPE_PARTITION, TYPE_PARTITION}, - FLAG_IGNORE_REST, "TO_DRIVE FROM_DRIVE", - "Map the drive FROM_DRIVE to the drive TO_DRIVE. This is necessary" - " when you chain-load some operating systems, such as DOS, if such an" - " OS resides at a non-first drive."}, - /* md5crypt unsupported. */ - {"module", "legacy_initrd '%s' %s\n", 1, {TYPE_FILE, TYPE_REST_VERBATIM}, 0, - "FILE [ARG ...]", - "Load a boot module FILE for a Multiboot format boot image (no" - " interpretation of the file contents is made, so users of this" - " command must know what the kernel in question expects). The" - " rest of the line is passed as the \"module command line\", like" - " the `kernel' command."}, - /* modulenounzip unsupported. */ - /* FIXME: allow toggle. */ - {"pager", "set pager=%d\n", 1, {TYPE_BOOL}, 0, "[FLAG]", - "Toggle pager mode with no argument. If FLAG is given and its value" - " is `on', turn on the mode. If FLAG is `off', turn off the mode."}, - /* partnew unsupported. */ - {"parttype", "parttool '%s' type=%s\n", 2, {TYPE_PARTITION, TYPE_INT}, 0, - "PART TYPE", "Change the type of the partition PART to TYPE."}, - /* password unsupported. */ /* NUL_TERMINATE */ - /* pause unsupported. */ - /* rarp unsupported. */ - {"read", "read_dword %s\n", 1, {TYPE_INT}, 0, "ADDR", - "Read a 32-bit value from memory at address ADDR and" - " display it in hex format."}, - {"reboot", "reboot\n", 0, {}, 0, 0, "Reboot your system."}, - /* FIXME: Support HDBIAS. */ - {"root", "set root='%s'\n", 1, {TYPE_PARTITION}, 0, "[DEVICE [HDBIAS]]", - "Set the current \"root device\" to the device DEVICE, then" - " attempt to mount it to get the partition size (for passing the" - " partition descriptor in `ES:ESI', used by some chain-loaded" - " bootloaders), the BSD drive-type (for booting BSD kernels using" - " their native boot format), and correctly determine " - " the PC partition where a BSD sub-partition is located. The" - " optional HDBIAS parameter is a number to tell a BSD kernel" - " how many BIOS drive numbers are on controllers before the current" - " one. For example, if there is an IDE disk and a SCSI disk, and your" - " FreeBSD root partition is on the SCSI disk, then use a `1' for HDBIAS."}, - {"rootnoverify", "set root='%s'\n", 1, {TYPE_PARTITION}, 0, - "[DEVICE [HDBIAS]]", - "Similar to `root', but don't attempt to mount the partition. This" - " is useful for when an OS is outside of the area of the disk that" - " GRUB can read, but setting the correct root device is still" - " desired. Note that the items mentioned in `root' which" - " derived from attempting the mount will NOT work correctly."}, - /* FIXME: support arguments. */ - {"savedefault", "saved_entry=${chosen}; save_env saved_entry\n", 0, {}, 0, - "[NUM | `fallback']", - "Save the current entry as the default boot entry if no argument is" - " specified. If a number is specified, this number is saved. If" - " `fallback' is used, next fallback entry is saved."}, - {"serial", "serial %s\n", 1, {TYPE_REST_VERBATIM}, 0, - "[--unit=UNIT] [--port=PORT] [--speed=SPEED] [--word=WORD] " - "[--parity=PARITY] [--stop=STOP] [--device=DEV]", - "Initialize a serial device. UNIT is a digit that specifies which serial" - " device is used (e.g. 0 == COM1). If you need to specify the port number," - " set it by --port. SPEED is the DTE-DTE speed. WORD is the word length," - " PARITY is the type of parity, which is one of `no', `odd' and `even'." - " STOP is the length of stop bit(s). The option --device can be used only" - " in the grub shell, which specifies the file name of a tty device. The" - " default values are COM1, 9600, 8N1."}, - /* setkey unsupported. */ /* NUL_TERMINATE */ - /* setup unsupported. */ - /* terminal unsupported. */ /* NUL_TERMINATE */ - /* terminfo unsupported. */ /* NUL_TERMINATE */ - /* testload unsupported. */ - /* testvbe unsupported. */ - /* tftpserver unsupported. */ - {"timeout", "set timeout=%s\n", 1, {TYPE_INT}, 0, "SEC", - "Set a timeout, in SEC seconds, before automatically booting the" - " default entry (normally the first entry defined)."}, - /* title is handled separately. */ - {"unhide", "parttool '%s' hidden-\n", 1, {TYPE_PARTITION}, 0, "PARTITION", - "Unhide PARTITION by clearing the \"hidden\" bit in its" - " partition type code."}, - /* uppermem unsupported. */ - {"uuid", "search -u '%s'\n", 1, {TYPE_VERBATIM}, 0, "UUID", - "Find root by UUID"}, - /* vbeprobe unsupported. */ - }; - -static char * -escape (const char *in) -{ - const char *ptr; - char *ret, *outptr; - int overhead = 0; - for (ptr = in; *ptr; ptr++) - if (*ptr == '\'' || *ptr == '\\') - overhead++; - ret = grub_malloc (ptr - in + overhead); - if (!ret) - return NULL; - outptr = ret; - for (ptr = in; *ptr; ptr++) - { - if (*ptr == '\'' || *ptr == '\\') - *outptr++ = '\\'; - - *outptr++ = *ptr; - } - *outptr++ = 0; - return ret; -} - -static char * -adjust_file (const char *in) -{ - const char *comma, *ptr, *rest; - char *ret, *outptr; - int overhead = 0; - int part; - if (in[0] != '(') - return escape (in); - for (ptr = in + 1; *ptr && *ptr != ')' && *ptr != ','; ptr++) - if (*ptr == '\'' || *ptr == '\\') - overhead++; - comma = ptr; - if (*comma != ',') - return escape (in); - part = grub_strtoull (comma + 1, (char **) &rest, 0); - for (ptr = rest; *ptr; ptr++) - if (*ptr == '\'' || *ptr == '\\') - overhead++; - - /* 30 is enough for any number. */ - ret = grub_malloc (ptr - in + overhead + 30); - if (!ret) - return NULL; - - outptr = ret; - for (ptr = in; ptr <= comma; ptr++) - { - if (*ptr == '\'' || *ptr == '\\') - *outptr++ = '\\'; - - *outptr++ = *ptr; - } - grub_snprintf (outptr, 30, "%d", part + 1); - while (*outptr) - outptr++; - for (ptr = rest; ptr <= comma; ptr++) - { - if (*ptr == '\'' || *ptr == '\\') - *outptr++ = '\\'; - - *outptr++ = *ptr; - } - return ret; -} - -static int -is_option (enum arg_type opt, const char *curarg) -{ - switch (opt) - { - case TYPE_NOAPM_OPTION: - return grub_strcmp (curarg, "--no-apm") == 0; - case TYPE_FORCE_OPTION: - return grub_strcmp (curarg, "--force") == 0; - case TYPE_TYPE_OR_NOMEM_OPTION: - return grub_strcmp (curarg, "--type=netbsd") == 0 - || grub_strcmp (curarg, "--type=freebsd") == 0 - || grub_strcmp (curarg, "--type=openbsd") == 0 - || grub_strcmp (curarg, "--type=linux") == 0 - || grub_strcmp (curarg, "--type=biglinux") == 0 - || grub_strcmp (curarg, "--type=multiboot") == 0 - || grub_strcmp (curarg, "--no-mem-option") == 0; - default: - return 0; - } -} - -static char * -legacy_parse (char *buf, char **entryname) -{ - char *ptr; - char *cmdname; - unsigned i, cmdnum; - - for (ptr = buf; *ptr && grub_isspace (*ptr); ptr++); - if ((!*ptr || *ptr == '#') && entryname && *entryname) - { - char *ret = grub_xasprintf ("%s\n", buf); - grub_free (buf); - return ret; - } - if (!*ptr || *ptr == '#') - { - grub_free (buf); - return NULL; - } - - cmdname = ptr; - for (ptr = buf; *ptr && !grub_isspace (*ptr) && *ptr != '='; ptr++); - - if (entryname && grub_strncmp ("title", cmdname, ptr - cmdname) == 0 - && ptr - cmdname == sizeof ("title") - 1) - { - for (; grub_isspace (*ptr) || *ptr == '='; ptr++); - *entryname = grub_strdup (ptr); - grub_free (buf); - return NULL; - } - - if (grub_strncmp ("lock", cmdname, ptr - cmdname) == 0 - && ptr - cmdname == sizeof ("lock") - 1) - { - /* FIXME */ - } - - for (cmdnum = 0; cmdnum < ARRAY_SIZE (legacy_commands); cmdnum++) - if (grub_strncmp (legacy_commands[cmdnum].name, cmdname, ptr - cmdname) == 0 - && legacy_commands[cmdnum].name[ptr - cmdname] == 0) - break; - if (cmdnum == ARRAY_SIZE (legacy_commands)) - return grub_xasprintf ("# Unsupported legacy command: %s\n", buf); - - for (; grub_isspace (*ptr) || *ptr == '='; ptr++); - - char *args[ARRAY_SIZE (legacy_commands[0].argt)]; - memset (args, 0, sizeof (args)); - - { - unsigned j = 0; - for (i = 0; i < legacy_commands[cmdnum].argc; i++) - { - char *curarg, *cptr = NULL, c = 0; - for (; grub_isspace (*ptr); ptr++); - curarg = ptr; - for (; !grub_isspace (*ptr); ptr++); - if (i != legacy_commands[cmdnum].argc - 1 - || (legacy_commands[cmdnum].flags & FLAG_IGNORE_REST)) - { - cptr = ptr; - c = *cptr; - *ptr = 0; - } - if (*ptr) - ptr++; - switch (legacy_commands[cmdnum].argt[i]) - { - case TYPE_PARTITION: - case TYPE_FILE: - args[j++] = adjust_file (curarg); - break; - - case TYPE_REST_VERBATIM: - { - char *outptr, *outptr0; - int overhead = 3; - ptr = curarg; - while (*ptr) - { - for (; grub_isspace (*ptr); ptr++); - for (; *ptr && !grub_isspace (*ptr); ptr++) - if (*ptr == '\\' || *ptr == '\'') - overhead++; - if (*ptr) - ptr++; - overhead += 3; - } - outptr0 = args[j++] = grub_malloc (overhead + (ptr - curarg)); - if (!outptr0) - { - grub_free (buf); - return NULL; - } - ptr = curarg; - outptr = outptr0; - while (*ptr) - { - for (; grub_isspace (*ptr); ptr++); - if (outptr != outptr0) - *outptr++ = ' '; - *outptr++ = '\''; - for (; *ptr && !grub_isspace (*ptr); ptr++) - { - if (*ptr == '\\' || *ptr == '\'') - *outptr++ = '\\'; - *outptr++ = *ptr; - } - *outptr++ = '\''; - if (*ptr) - ptr++; - overhead += 3; - } - *outptr++ = 0; - } - break; - - case TYPE_VERBATIM: - args[j++] = escape (curarg); - break; - case TYPE_FORCE_OPTION: - case TYPE_NOAPM_OPTION: - case TYPE_TYPE_OR_NOMEM_OPTION: - if (is_option (legacy_commands[cmdnum].argt[i], curarg)) - { - args[j++] = grub_strdup (curarg); - break; - } - if (cptr) - *cptr = c; - ptr = curarg; - args[j++] = ""; - break; - case TYPE_INT: - { - char *brk; - int base = 10; - brk = curarg; - if (brk[0] == '0' && brk[1] == 'x') - base = 16; - else if (brk[0] == '0') - base = 8; - for (; *brk; brk++) - { - if (base == 8 && (*brk == '8' || *brk == '9')) - break; - if (grub_isdigit (*brk)) - continue; - if (base != 16) - break; - if (!(*brk >= 'a' && *brk <= 'f') - && !(*brk >= 'A' && *brk <= 'F')) - break; - } - if (brk == curarg) - args[j++] = grub_strdup ("0"); - else - args[j++] = grub_strndup (curarg, brk - curarg); - } - break; - case TYPE_BOOL: - if (curarg[0] == 'o' && curarg[1] == 'n' - && (curarg[2] == 0 || grub_isspace (curarg[2]))) - args[j++] = grub_strdup ("1"); - else - args[j++] = grub_strdup ("0"); - break; - } - } - } - grub_free (buf); - return grub_xasprintf (legacy_commands[cmdnum].map, args[0], args[1], args[2]); -} +#include static grub_err_t legacy_file (const char *filename) @@ -534,7 +69,8 @@ legacy_file (const char *filename) char *oldname = NULL; oldname = entryname; - parsed = legacy_parse (buf, &entryname); + parsed = grub_legacy_parse (buf, &entryname); + grub_free (buf); if (oldname != entryname && oldname) { const char **args = grub_malloc (sizeof (args[0])); diff --git a/grub-core/lib/legacy_parse.c b/grub-core/lib/legacy_parse.c new file mode 100644 index 000000000..0b30ef3b1 --- /dev/null +++ b/grub-core/lib/legacy_parse.c @@ -0,0 +1,496 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 1999,2000,2001,2002,2003,2004,2010 Free Software Foundation, Inc. + * + * GRUB 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, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#include +#include +#include +#include +#include + +struct legacy_command +{ + const char *name; + const char *map; + unsigned argc; + enum arg_type { + TYPE_VERBATIM, + TYPE_FORCE_OPTION, + TYPE_NOAPM_OPTION, + TYPE_TYPE_OR_NOMEM_OPTION, + TYPE_FILE, + TYPE_PARTITION, + TYPE_BOOL, + TYPE_INT, + TYPE_REST_VERBATIM + } argt[4]; + enum { + FLAG_IGNORE_REST = 1 + } flags; + const char *shortdesc; + const char *longdesc; +}; + +struct legacy_command legacy_commands[] = + { + {"blocklist", "blocklist '%s'\n", 1, {TYPE_FILE}, 0, "FILE", + "Print the blocklist notation of the file FILE."}, + {"boot", "boot\n", 0, {}, 0, 0, + "Boot the OS/chain-loader which has been loaded."}, + /* bootp unsupported. */ + {"cat", "cat '%s'\n", 1, {TYPE_FILE}, 0, "FILE", + "Print the contents of the file FILE."}, + {"chainloader", "chainloader %s '%s'\n", 2, {TYPE_FORCE_OPTION, TYPE_FILE}, + 0, "[--force] FILE", + "Load the chain-loader FILE. If --force is specified, then load it" + " forcibly, whether the boot loader signature is present or not."}, + {"cmp", "cmp '%s' '%s'\n", 2, {TYPE_FILE, TYPE_FILE}, FLAG_IGNORE_REST, + "FILE1 FILE2", + "Compare the file FILE1 with the FILE2 and inform the different values" + " if any."}, + /* FIXME: Implement command. */ + {"color", "legacy_color '%s' '%s'\n", 2, {TYPE_VERBATIM, TYPE_VERBATIM}, + FLAG_IGNORE_REST, "NORMAL [HIGHLIGHT]", + "Change the menu colors. The color NORMAL is used for most" + " lines in the menu, and the color HIGHLIGHT is used to highlight the" + " line where the cursor points. If you omit HIGHLIGHT, then the" + " inverted color of NORMAL is used for the highlighted line." + " The format of a color is \"FG/BG\". FG and BG are symbolic color names." + " A symbolic color name must be one of these: black, blue, green," + " cyan, red, magenta, brown, light-gray, dark-gray, light-blue," + " light-green, light-cyan, light-red, light-magenta, yellow and white." + " But only the first eight names can be used for BG. You can prefix" + " \"blink-\" to FG if you want a blinking foreground color."}, + {"configfile", "legacy_configfile '%s'\n", 1, {TYPE_FILE}, 0, "FILE", + "Load FILE as the configuration file."}, + {"debug", + "if [ -z \"$debug\" ]; then set debug=all; else set debug=; fi\n", + 0, {}, 0, 0, "Turn on/off the debug mode."}, + {"default", + "set default='%s'; if [ x\"$default\" = xsaved ]; then load_env; " + "set default=\"$saved_entry\"; fi\n", 1, {TYPE_VERBATIM}, 0, + "[NUM | `saved']", + "Set the default entry to entry number NUM (if not specified, it is" + " 0, the first entry) or the entry number saved by savedefault."}, + /* dhcp unsupported. */ + /* displayapm unsupported. */ + {"displaymem", "lsmmap\n", 0, {}, 0, 0, + "Display what GRUB thinks the system address space map of the" + " machine is, including all regions of physical RAM installed."}, + /* embed unsupported. */ + {"fallback", "set fallback='%s'\n", 1, {TYPE_VERBATIM}, 0, "NUM...", + "Go into unattended boot mode: if the default boot entry has any" + " errors, instead of waiting for the user to do anything, it" + " immediately starts over using the NUM entry (same numbering as the" + " `default' command). This obviously won't help if the machine" + " was rebooted by a kernel that GRUB loaded."}, + {"find", "search -sf '%s'\n", 1, {TYPE_FILE}, 0, "FILENAME", + "Search for the filename FILENAME in all of partitions and print the list of" + " the devices which contain the file."}, + /* fstest unsupported. */ + /* geometry unsupported. */ + {"halt", "halt %s\n", 1, {TYPE_NOAPM_OPTION}, 0, "[--no-apm]", + "Halt your system. If APM is available on it, turn off the power using" + " the APM BIOS, unless you specify the option `--no-apm'."}, + /* help unsupported. */ /* NUL_TERMINATE */ + /* hiddenmenu unsupported. */ + {"hide", "parttool '%s' hidden+\n", 1, {TYPE_PARTITION}, 0, "PARTITION", + "Hide PARTITION by setting the \"hidden\" bit in" + " its partition type code."}, + /* ifconfig unsupported. */ + /* impsprobe unsupported. */ + /* FIXME: Implement command. */ + {"initrd", "legacy_initrd '%s' %s\n", 2, {TYPE_FILE, TYPE_REST_VERBATIM}, 0, + "FILE [ARG ...]", + "Load an initial ramdisk FILE for a Linux format boot image and set the" + " appropriate parameters in the Linux setup area in memory."}, + /* install unsupported. */ + /* ioprobe unsupported. */ + /* FIXME: implement command. */ + {"kernel", "legacy_kernel %s '%s' %s\n", 4, {TYPE_TYPE_OR_NOMEM_OPTION, + TYPE_TYPE_OR_NOMEM_OPTION, + TYPE_FILE, + TYPE_REST_VERBATIM}, 0, + "[--no-mem-option] [--type=TYPE] FILE [ARG ...]", + "Attempt to load the primary boot image from FILE. The rest of the" + " line is passed verbatim as the \"kernel command line\". Any modules" + " must be reloaded after using this command. The option --type is used" + " to suggest what type of kernel to be loaded. TYPE must be either of" + " \"netbsd\", \"freebsd\", \"openbsd\", \"linux\", \"biglinux\" and" + " \"multiboot\". The option --no-mem-option tells GRUB not to pass a" + " Linux's mem option automatically."}, + /* lock is handled separately. */ + {"makeactive", "parttool \"$root\" boot+\n", 0, {}, 0, 0, + "Set the active partition on the root disk to GRUB's root device." + " This command is limited to _primary_ PC partitions on a hard disk."}, + {"map", "drivemap '%s' '%s'\n", 2, {TYPE_PARTITION, TYPE_PARTITION}, + FLAG_IGNORE_REST, "TO_DRIVE FROM_DRIVE", + "Map the drive FROM_DRIVE to the drive TO_DRIVE. This is necessary" + " when you chain-load some operating systems, such as DOS, if such an" + " OS resides at a non-first drive."}, + /* md5crypt unsupported. */ + {"module", "legacy_initrd '%s' %s\n", 1, {TYPE_FILE, TYPE_REST_VERBATIM}, 0, + "FILE [ARG ...]", + "Load a boot module FILE for a Multiboot format boot image (no" + " interpretation of the file contents is made, so users of this" + " command must know what the kernel in question expects). The" + " rest of the line is passed as the \"module command line\", like" + " the `kernel' command."}, + /* modulenounzip unsupported. */ + /* FIXME: allow toggle. */ + {"pager", "set pager=%d\n", 1, {TYPE_BOOL}, 0, "[FLAG]", + "Toggle pager mode with no argument. If FLAG is given and its value" + " is `on', turn on the mode. If FLAG is `off', turn off the mode."}, + /* partnew unsupported. */ + {"parttype", "parttool '%s' type=%s\n", 2, {TYPE_PARTITION, TYPE_INT}, 0, + "PART TYPE", "Change the type of the partition PART to TYPE."}, + /* password unsupported. */ /* NUL_TERMINATE */ + /* pause unsupported. */ + /* rarp unsupported. */ + {"read", "read_dword %s\n", 1, {TYPE_INT}, 0, "ADDR", + "Read a 32-bit value from memory at address ADDR and" + " display it in hex format."}, + {"reboot", "reboot\n", 0, {}, 0, 0, "Reboot your system."}, + /* FIXME: Support HDBIAS. */ + /* FIXME: Support printing. */ + {"root", "set root='%s'\n", 1, {TYPE_PARTITION}, 0, "[DEVICE [HDBIAS]]", + "Set the current \"root device\" to the device DEVICE, then" + " attempt to mount it to get the partition size (for passing the" + " partition descriptor in `ES:ESI', used by some chain-loaded" + " bootloaders), the BSD drive-type (for booting BSD kernels using" + " their native boot format), and correctly determine " + " the PC partition where a BSD sub-partition is located. The" + " optional HDBIAS parameter is a number to tell a BSD kernel" + " how many BIOS drive numbers are on controllers before the current" + " one. For example, if there is an IDE disk and a SCSI disk, and your" + " FreeBSD root partition is on the SCSI disk, then use a `1' for HDBIAS."}, + {"rootnoverify", "set root='%s'\n", 1, {TYPE_PARTITION}, 0, + "[DEVICE [HDBIAS]]", + "Similar to `root', but don't attempt to mount the partition. This" + " is useful for when an OS is outside of the area of the disk that" + " GRUB can read, but setting the correct root device is still" + " desired. Note that the items mentioned in `root' which" + " derived from attempting the mount will NOT work correctly."}, + /* FIXME: support arguments. */ + {"savedefault", "saved_entry=${chosen}; save_env saved_entry\n", 0, {}, 0, + "[NUM | `fallback']", + "Save the current entry as the default boot entry if no argument is" + " specified. If a number is specified, this number is saved. If" + " `fallback' is used, next fallback entry is saved."}, + {"serial", "serial %s\n", 1, {TYPE_REST_VERBATIM}, 0, + "[--unit=UNIT] [--port=PORT] [--speed=SPEED] [--word=WORD] " + "[--parity=PARITY] [--stop=STOP] [--device=DEV]", + "Initialize a serial device. UNIT is a digit that specifies which serial" + " device is used (e.g. 0 == COM1). If you need to specify the port number," + " set it by --port. SPEED is the DTE-DTE speed. WORD is the word length," + " PARITY is the type of parity, which is one of `no', `odd' and `even'." + " STOP is the length of stop bit(s). The option --device can be used only" + " in the grub shell, which specifies the file name of a tty device. The" + " default values are COM1, 9600, 8N1."}, + /* setkey unsupported. */ /* NUL_TERMINATE */ + /* setup unsupported. */ + /* terminal unsupported. */ /* NUL_TERMINATE */ + /* terminfo unsupported. */ /* NUL_TERMINATE */ + /* testload unsupported. */ + /* testvbe unsupported. */ + /* tftpserver unsupported. */ + {"timeout", "set timeout=%s\n", 1, {TYPE_INT}, 0, "SEC", + "Set a timeout, in SEC seconds, before automatically booting the" + " default entry (normally the first entry defined)."}, + /* title is handled separately. */ + {"unhide", "parttool '%s' hidden-\n", 1, {TYPE_PARTITION}, 0, "PARTITION", + "Unhide PARTITION by clearing the \"hidden\" bit in its" + " partition type code."}, + /* uppermem unsupported. */ + {"uuid", "search -u '%s'\n", 1, {TYPE_VERBATIM}, 0, "UUID", + "Find root by UUID"}, + /* vbeprobe unsupported. */ + }; + +char * +grub_legacy_escape (const char *in, grub_size_t len) +{ + const char *ptr; + char *ret, *outptr; + int overhead = 0; + for (ptr = in; ptr < in + len && *ptr; ptr++) + if (*ptr == '\'' || *ptr == '\\') + overhead++; + ret = grub_malloc (ptr - in + overhead); + if (!ret) + return NULL; + outptr = ret; + for (ptr = in; ptr < in + len && *ptr; ptr++) + { + if (*ptr == '\'' || *ptr == '\\') + *outptr++ = '\\'; + + *outptr++ = *ptr; + } + *outptr++ = 0; + return ret; +} + +static char * +adjust_file (const char *in, grub_size_t len) +{ + const char *comma, *ptr, *rest; + char *ret, *outptr; + int overhead = 0; + int part; + if (in[0] != '(') + return grub_legacy_escape (in, len); + for (ptr = in + 1; ptr < in + len && *ptr && *ptr != ')' + && *ptr != ','; ptr++) + if (*ptr == '\'' || *ptr == '\\') + overhead++; + comma = ptr; + if (*comma != ',') + return grub_legacy_escape (in, len); + part = grub_strtoull (comma + 1, (char **) &rest, 0); + for (ptr = rest; ptr < in + len && *ptr; ptr++) + if (*ptr == '\'' || *ptr == '\\') + overhead++; + + /* 30 is enough for any number. */ + ret = grub_malloc (ptr - in + overhead + 30); + if (!ret) + return NULL; + + outptr = ret; + for (ptr = in; ptr < in + len && ptr <= comma; ptr++) + { + if (*ptr == '\'' || *ptr == '\\') + *outptr++ = '\\'; + + *outptr++ = *ptr; + } + grub_snprintf (outptr, 30, "%d", part + 1); + while (*outptr) + outptr++; + for (ptr = rest; ptr < in + len; ptr++) + { + if (*ptr == '\'' || *ptr == '\\') + *outptr++ = '\\'; + + *outptr++ = *ptr; + } + return ret; +} + +static int +check_option (const char *a, char *b, grub_size_t len) +{ + if (grub_strlen (b) != len) + return 0; + return grub_strncmp (a, b, len) == 0; +} + +static int +is_option (enum arg_type opt, const char *curarg, grub_size_t len) +{ + switch (opt) + { + case TYPE_NOAPM_OPTION: + return check_option (curarg, "--no-apm", len); + case TYPE_FORCE_OPTION: + return check_option (curarg, "--force", len); + case TYPE_TYPE_OR_NOMEM_OPTION: + return check_option (curarg, "--type=netbsd", len) + || check_option (curarg, "--type=freebsd", len) + || check_option (curarg, "--type=openbsd", len) + || check_option (curarg, "--type=linux", len) + || check_option (curarg, "--type=biglinux", len) + || check_option (curarg, "--type=multiboot", len) + || check_option (curarg, "--no-mem-option", len); + default: + return 0; + } +} + +char * +grub_legacy_parse (const char *buf, char **entryname) +{ + const char *ptr; + const char *cmdname; + unsigned i, cmdnum; + + for (ptr = buf; *ptr && grub_isspace (*ptr); ptr++); + if (!*ptr || *ptr == '#') + return grub_strdup (buf); + + cmdname = ptr; + for (ptr = buf; *ptr && !grub_isspace (*ptr) && *ptr != '='; ptr++); + + if (entryname && grub_strncmp ("title", cmdname, ptr - cmdname) == 0 + && ptr - cmdname == sizeof ("title") - 1) + { + const char *ptr2; + for (; grub_isspace (*ptr) || *ptr == '='; ptr++); + ptr2 = ptr + grub_strlen (ptr); + while (ptr2 > ptr && grub_isspace (*(ptr2 - 1))) + ptr2--; + *entryname = grub_strndup (ptr, ptr2 - ptr); + return NULL; + } + + if (grub_strncmp ("lock", cmdname, ptr - cmdname) == 0 + && ptr - cmdname == sizeof ("lock") - 1) + { + /* FIXME */ + } + + for (cmdnum = 0; cmdnum < ARRAY_SIZE (legacy_commands); cmdnum++) + if (grub_strncmp (legacy_commands[cmdnum].name, cmdname, ptr - cmdname) == 0 + && legacy_commands[cmdnum].name[ptr - cmdname] == 0) + break; + if (cmdnum == ARRAY_SIZE (legacy_commands)) + return grub_xasprintf ("# Unsupported legacy command: %s\n", buf); + + for (; grub_isspace (*ptr) || *ptr == '='; ptr++); + + char *args[ARRAY_SIZE (legacy_commands[0].argt)]; + grub_memset (args, 0, sizeof (args)); + + { + unsigned j = 0; + int hold_arg = 0; + for (i = 0; i < legacy_commands[cmdnum].argc; i++) + { + const char *curarg; + grub_size_t curarglen; + if (hold_arg) + { + ptr = curarg; + hold_arg = 0; + } + for (; grub_isspace (*ptr); ptr++); + curarg = ptr; + for (; !grub_isspace (*ptr); ptr++); + if (i != legacy_commands[cmdnum].argc - 1 + || (legacy_commands[cmdnum].flags & FLAG_IGNORE_REST)) + curarglen = ptr - curarg; + else + { + curarglen = grub_strlen (curarg); + while (curarglen > 0 && grub_isspace (curarg[curarglen - 1])) + curarglen--; + } + if (*ptr) + ptr++; + switch (legacy_commands[cmdnum].argt[i]) + { + case TYPE_PARTITION: + case TYPE_FILE: + args[j++] = adjust_file (curarg, curarglen); + break; + + case TYPE_REST_VERBATIM: + { + char *outptr, *outptr0; + int overhead = 3; + ptr = curarg; + while (*ptr) + { + for (; grub_isspace (*ptr); ptr++); + for (; *ptr && !grub_isspace (*ptr); ptr++) + if (*ptr == '\\' || *ptr == '\'') + overhead++; + if (*ptr) + ptr++; + overhead += 3; + } + outptr0 = args[j++] = grub_malloc (overhead + (ptr - curarg)); + if (!outptr0) + return NULL; + ptr = curarg; + outptr = outptr0; + while (*ptr) + { + for (; grub_isspace (*ptr); ptr++); + if (outptr != outptr0) + *outptr++ = ' '; + *outptr++ = '\''; + for (; *ptr && !grub_isspace (*ptr); ptr++) + { + if (*ptr == '\\' || *ptr == '\'') + *outptr++ = '\\'; + *outptr++ = *ptr; + } + *outptr++ = '\''; + if (*ptr) + ptr++; + overhead += 3; + } + *outptr++ = 0; + } + break; + + case TYPE_VERBATIM: + args[j++] = grub_legacy_escape (curarg, curarglen); + break; + case TYPE_FORCE_OPTION: + case TYPE_NOAPM_OPTION: + case TYPE_TYPE_OR_NOMEM_OPTION: + if (is_option (legacy_commands[cmdnum].argt[i], curarg, curarglen)) + { + args[j++] = grub_strndup (curarg, curarglen); + break; + } + args[j++] = ""; + hold_arg = 1; + break; + case TYPE_INT: + { + const char *brk; + int base = 10; + brk = curarg; + if (curarglen < 1) + args[j++] = grub_strdup ("0"); + if (brk[0] == '0' && brk[1] == 'x') + base = 16; + else if (brk[0] == '0') + base = 8; + for (; *brk && brk < curarg + curarglen; brk++) + { + if (base == 8 && (*brk == '8' || *brk == '9')) + break; + if (grub_isdigit (*brk)) + continue; + if (base != 16) + break; + if (!(*brk >= 'a' && *brk <= 'f') + && !(*brk >= 'A' && *brk <= 'F')) + break; + } + if (brk == curarg) + args[j++] = grub_strdup ("0"); + else + args[j++] = grub_strndup (curarg, brk - curarg); + } + break; + case TYPE_BOOL: + if (curarglen == 2 && curarg[0] == 'o' && curarg[1] == 'n') + args[j++] = grub_strdup ("1"); + else + args[j++] = grub_strdup ("0"); + break; + } + } + } + return grub_xasprintf (legacy_commands[cmdnum].map, args[0], args[1], args[2]); +} diff --git a/include/grub/legacy_parse.h b/include/grub/legacy_parse.h new file mode 100644 index 000000000..fce4e3e40 --- /dev/null +++ b/include/grub/legacy_parse.h @@ -0,0 +1,27 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2008 Free Software Foundation, Inc. + * + * GRUB 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, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#ifndef GRUB_LEGACY_PARSE_HEADER +#define GRUB_LEGACY_PARSE_HEADER 1 + +#include + +char *grub_legacy_parse (const char *buf, char **entryname); +char *grub_legacy_escape (const char *in, grub_size_t len); + +#endif diff --git a/util/grub-menulst2cfg.c b/util/grub-menulst2cfg.c new file mode 100644 index 000000000..fdbdda388 --- /dev/null +++ b/util/grub-menulst2cfg.c @@ -0,0 +1,99 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2010 Free Software Foundation, Inc. + * + * GRUB 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, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#include +#include +#include +#include + +int +main (int argc, char **argv) +{ + FILE *in, *out; + char *entryname = NULL; + char *buf = NULL; + size_t bufsize = 0; + + if (argc >= 2 && argv[1][0] == '-') + { + fprintf (stderr, "Usage: %s [INFILE [OUTFILE]]\n", argv[0]); + return 0; + } + + if (argc >= 2) + { + in = fopen (argv[1], "r"); + if (!in) + { + fprintf (stderr, "Couldn't open %s for reading: %s\n", + argv[1], strerror (errno)); + return 1; + } + } + else + in = stdin; + + if (argc >= 3) + { + out = fopen (argv[2], "w"); + if (!out) + { + if (in != stdin) + fclose (in); + fprintf (stderr, "Couldn't open %s for writing: %s\n", + argv[2], strerror (errno)); + return 1; + } + } + else + out = stdout; + + while (1) + { + char *parsed; + + if (getline (&buf, &bufsize, in) < 0) + break; + + { + char *oldname = NULL; + + oldname = entryname; + parsed = grub_legacy_parse (buf, &entryname); + if (oldname != entryname && oldname) + fprintf (out, "}\n\n"); + if (oldname != entryname) + fprintf (out, "menuentry \'%s\' {\n", + grub_legacy_escape (entryname, grub_strlen (entryname))); + } + + if (parsed) + fprintf (out, "%s%s", entryname ? " " : "", parsed); + } + + if (entryname) + fprintf (out, "}\n\n"); + + + if (in != stdin) + fclose (in); + if (out != stdout) + fclose (out); + + return 0; +} From 661cf422317447e70016ae99d222cc3601bb3e23 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Fri, 27 Aug 2010 20:23:39 +0200 Subject: [PATCH 09/37] Fix a problem with kernel command --- grub-core/lib/legacy_parse.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/grub-core/lib/legacy_parse.c b/grub-core/lib/legacy_parse.c index 0b30ef3b1..985a53733 100644 --- a/grub-core/lib/legacy_parse.c +++ b/grub-core/lib/legacy_parse.c @@ -121,7 +121,7 @@ struct legacy_command legacy_commands[] = /* install unsupported. */ /* ioprobe unsupported. */ /* FIXME: implement command. */ - {"kernel", "legacy_kernel %s '%s' %s\n", 4, {TYPE_TYPE_OR_NOMEM_OPTION, + {"kernel", "legacy_kernel %s %s '%s' %s\n", 4, {TYPE_TYPE_OR_NOMEM_OPTION, TYPE_TYPE_OR_NOMEM_OPTION, TYPE_FILE, TYPE_REST_VERBATIM}, 0, @@ -380,7 +380,7 @@ grub_legacy_parse (const char *buf, char **entryname) } for (; grub_isspace (*ptr); ptr++); curarg = ptr; - for (; !grub_isspace (*ptr); ptr++); + for (; *ptr && !grub_isspace (*ptr); ptr++); if (i != legacy_commands[cmdnum].argc - 1 || (legacy_commands[cmdnum].flags & FLAG_IGNORE_REST)) curarglen = ptr - curarg; @@ -406,7 +406,7 @@ grub_legacy_parse (const char *buf, char **entryname) ptr = curarg; while (*ptr) { - for (; grub_isspace (*ptr); ptr++); + for (; *ptr && grub_isspace (*ptr); ptr++); for (; *ptr && !grub_isspace (*ptr); ptr++) if (*ptr == '\\' || *ptr == '\'') overhead++; @@ -421,7 +421,7 @@ grub_legacy_parse (const char *buf, char **entryname) outptr = outptr0; while (*ptr) { - for (; grub_isspace (*ptr); ptr++); + for (; *ptr && grub_isspace (*ptr); ptr++); if (outptr != outptr0) *outptr++ = ' '; *outptr++ = '\''; @@ -434,7 +434,6 @@ grub_legacy_parse (const char *buf, char **entryname) *outptr++ = '\''; if (*ptr) ptr++; - overhead += 3; } *outptr++ = 0; } @@ -492,5 +491,7 @@ grub_legacy_parse (const char *buf, char **entryname) } } } - return grub_xasprintf (legacy_commands[cmdnum].map, args[0], args[1], args[2]); + + return grub_xasprintf (legacy_commands[cmdnum].map, args[0], args[1], args[2], + args[3]); } From 8fc6a271473ede7cdff29cbfab9558e9dbfd5597 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Fri, 27 Aug 2010 21:27:26 +0200 Subject: [PATCH 10/37] Implement legacy_kernel and legacy_initrd commands --- grub-core/commands/legacycfg.c | 190 ++++++++++++++++++++++++++++++++- grub-core/lib/legacy_parse.c | 8 +- 2 files changed, 194 insertions(+), 4 deletions(-) diff --git a/grub-core/commands/legacycfg.c b/grub-core/commands/legacycfg.c index 4207531ec..ed02fd4f2 100644 --- a/grub-core/commands/legacycfg.c +++ b/grub-core/commands/legacycfg.c @@ -167,13 +167,199 @@ grub_cmd_legacy_configfile (struct grub_command *cmd __attribute__ ((unused)), return ret; } -static grub_command_t cmd_source, cmd_configfile; +static enum + { + GUESS_IT, LINUX, MULTIBOOT, KFREEBSD, KNETBSD, KOPENBSD + } kernel_type; + +static grub_err_t +grub_cmd_legacy_kernel (struct grub_command *mycmd __attribute__ ((unused)), + int argc, char **args) +{ + int i; + int no_mem_option = 0; + struct grub_command *cmd; + for (i = 0; i < 2; i++) + { + /* FIXME: really support this. */ + if (argc >= 1 && grub_strcmp (args[0], "--no-mem-option") == 0) + { + no_mem_option = 1; + argc--; + args++; + continue; + } + + /* FIXME: what's the difference? */ + if (argc >= 1 && (grub_strcmp (args[0], "--type=linux") == 0 + || grub_strcmp (args[0], "--type=biglinux") == 0)) + { + kernel_type = LINUX; + argc--; + args++; + continue; + } + + if (argc >= 1 && grub_strcmp (args[0], "--type=multiboot") == 0) + { + kernel_type = MULTIBOOT; + argc--; + args++; + continue; + } + + if (argc >= 1 && grub_strcmp (args[0], "--type=freebsd") == 0) + { + kernel_type = KFREEBSD; + argc--; + args++; + continue; + } + + if (argc >= 1 && grub_strcmp (args[0], "--type=openbsd") == 0) + { + kernel_type = KOPENBSD; + argc--; + args++; + continue; + } + + if (argc >= 1 && grub_strcmp (args[0], "--type=netbsd") == 0) + { + kernel_type = KNETBSD; + argc--; + args++; + continue; + } + } + + if (!argc) + return grub_error (GRUB_ERR_BAD_ARGUMENT, "filename required"); + + do + { + /* First try Linux. */ + if (kernel_type == GUESS_IT || kernel_type == LINUX) + { + cmd = grub_command_find ("linux16"); + if (cmd) + { + if (!(cmd->func) (cmd, argc, args)) + { + kernel_type = LINUX; + return GRUB_ERR_NONE; + } + } + grub_errno = GRUB_ERR_NONE; + } + + /* Then multiboot. */ + /* FIXME: dublicate multiboot filename. */ + if (kernel_type == GUESS_IT || kernel_type == MULTIBOOT) + { + cmd = grub_command_find ("multiboot"); + if (cmd) + { + if (!(cmd->func) (cmd, argc, args)) + { + kernel_type = MULTIBOOT; + return GRUB_ERR_NONE; + } + } + grub_errno = GRUB_ERR_NONE; + } + + /* k*BSD didn't really work well with grub-legacy. */ + if (kernel_type == GUESS_IT || kernel_type == KFREEBSD) + { + cmd = grub_command_find ("kfreebsd"); + if (cmd) + { + if (!(cmd->func) (cmd, argc, args)) + { + kernel_type = KFREEBSD; + return GRUB_ERR_NONE; + } + } + grub_errno = GRUB_ERR_NONE; + } + if (kernel_type == GUESS_IT || kernel_type == KNETBSD) + { + cmd = grub_command_find ("knetbsd"); + if (cmd) + { + if (!(cmd->func) (cmd, argc, args)) + { + kernel_type = KNETBSD; + return GRUB_ERR_NONE; + } + } + grub_errno = GRUB_ERR_NONE; + } + if (kernel_type == GUESS_IT || kernel_type == KOPENBSD) + { + cmd = grub_command_find ("kopenbsd"); + if (cmd) + { + if (!(cmd->func) (cmd, argc, args)) + { + kernel_type = KOPENBSD; + return GRUB_ERR_NONE; + } + } + grub_errno = GRUB_ERR_NONE; + } + } + while (0); + + return grub_error (GRUB_ERR_BAD_OS, "couldn't load file %s\n", + args[0]); +} + +static grub_err_t +grub_cmd_legacy_initrd (struct grub_command *mycmd __attribute__ ((unused)), + int argc, char **args) +{ + struct grub_command *cmd; + + if (kernel_type == LINUX) + { + cmd = grub_command_find ("initrd16"); + if (!cmd) + return grub_error (GRUB_ERR_BAD_ARGUMENT, "command initrd16 not found"); + + return cmd->func (cmd, argc, args); + } + if (kernel_type == MULTIBOOT) + { + /* FIXME: dublicate module filename. */ + cmd = grub_command_find ("module"); + if (!cmd) + return grub_error (GRUB_ERR_BAD_ARGUMENT, "command module not found"); + + return cmd->func (cmd, argc, args); + } + + return grub_error (GRUB_ERR_BAD_ARGUMENT, + "no kernel with module support is loaded in legacy way"); +} + +static grub_command_t cmd_source, cmd_configfile, cmd_kernel, cmd_initrd; GRUB_MOD_INIT(legacycfg) { cmd_source = grub_register_command ("legacy_source", grub_cmd_legacy_source, N_("FILE"), N_("Parse legacy config")); + cmd_kernel = grub_register_command ("legacy_kernel", + grub_cmd_legacy_kernel, + N_("[--no-mem-option] [--type=TYPE] FILE [ARG ...]"), + N_("Simulate grub-legacy kernel command")); + + cmd_initrd = grub_register_command ("legacy_initrd", + grub_cmd_legacy_initrd, + N_("FILE [ARG ...]"), + N_("Simulate grub-legacy initrd command")); cmd_configfile = grub_register_command ("legacy_configfile", grub_cmd_legacy_configfile, N_("FILE"), @@ -184,4 +370,6 @@ GRUB_MOD_FINI(legacycfg) { grub_unregister_command (cmd_source); grub_unregister_command (cmd_configfile); + grub_unregister_command (cmd_kernel); + grub_unregister_command (cmd_initrd); } diff --git a/grub-core/lib/legacy_parse.c b/grub-core/lib/legacy_parse.c index 985a53733..61952d35d 100644 --- a/grub-core/lib/legacy_parse.c +++ b/grub-core/lib/legacy_parse.c @@ -113,14 +113,16 @@ struct legacy_command legacy_commands[] = " its partition type code."}, /* ifconfig unsupported. */ /* impsprobe unsupported. */ - /* FIXME: Implement command. */ + /* FIXME: dublicate multiboot filename. */ {"initrd", "legacy_initrd '%s' %s\n", 2, {TYPE_FILE, TYPE_REST_VERBATIM}, 0, "FILE [ARG ...]", "Load an initial ramdisk FILE for a Linux format boot image and set the" " appropriate parameters in the Linux setup area in memory."}, /* install unsupported. */ /* ioprobe unsupported. */ - /* FIXME: implement command. */ + /* FIXME: really support --no-mem-option. */ + /* FIXME: distinguish linux and biglinux. */ + /* FIXME: dublicate multiboot filename. */ {"kernel", "legacy_kernel %s %s '%s' %s\n", 4, {TYPE_TYPE_OR_NOMEM_OPTION, TYPE_TYPE_OR_NOMEM_OPTION, TYPE_FILE, @@ -133,7 +135,7 @@ struct legacy_command legacy_commands[] = " \"netbsd\", \"freebsd\", \"openbsd\", \"linux\", \"biglinux\" and" " \"multiboot\". The option --no-mem-option tells GRUB not to pass a" " Linux's mem option automatically."}, - /* lock is handled separately. */ + /* lock is unsupported. */ {"makeactive", "parttool \"$root\" boot+\n", 0, {}, 0, 0, "Set the active partition on the root disk to GRUB's root device." " This command is limited to _primary_ PC partitions on a hard disk."}, From 7ddbecf25fc97603da45d0783e5dec3372aa85b9 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Fri, 27 Aug 2010 22:09:09 +0200 Subject: [PATCH 11/37] implement legacy_color --- grub-core/commands/legacycfg.c | 39 ++++++++++++++++++++++++++++++++++ grub-core/lib/legacy_parse.c | 1 - 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/grub-core/commands/legacycfg.c b/grub-core/commands/legacycfg.c index ed02fd4f2..db53f2c92 100644 --- a/grub-core/commands/legacycfg.c +++ b/grub-core/commands/legacycfg.c @@ -344,7 +344,41 @@ grub_cmd_legacy_initrd (struct grub_command *mycmd __attribute__ ((unused)), "no kernel with module support is loaded in legacy way"); } +static grub_err_t +grub_cmd_legacy_color (struct grub_command *mycmd __attribute__ ((unused)), + int argc, char **args) +{ + if (argc < 1) + return grub_error (GRUB_ERR_BAD_ARGUMENT, "color required"); + grub_env_set ("color_normal", args[0]); + if (argc >= 2) + grub_env_set ("color_highlight", args[1]); + else + { + char *slash = grub_strchr (args[0], '/'); + char *invert; + grub_size_t len; + + len = grub_strlen (args[0]); + if (!slash) + return grub_error (GRUB_ERR_BAD_ARGUMENT, "bad color specification %s", + args[0]); + invert = grub_malloc (len + 1); + if (!invert) + return grub_errno; + grub_memcpy (invert, slash + 1, len - (slash - args[0]) - 1); + invert[len - (slash - args[0]) - 1] = '/'; + grub_memcpy (invert + len - (slash - args[0]), args[0], slash - args[0]); + invert[len] = 0; + grub_env_set ("color_highlight", invert); + grub_free (invert); + } + + return grub_errno; +} + static grub_command_t cmd_source, cmd_configfile, cmd_kernel, cmd_initrd; +static grub_command_t cmd_color; GRUB_MOD_INIT(legacycfg) { @@ -364,6 +398,10 @@ GRUB_MOD_INIT(legacycfg) grub_cmd_legacy_configfile, N_("FILE"), N_("Parse legacy config")); + cmd_color = grub_register_command ("legacy_color", + grub_cmd_legacy_color, + N_("NORMAL [HIGHLIGHT]"), + N_("Simulate grub-legacy color command")); } GRUB_MOD_FINI(legacycfg) @@ -372,4 +410,5 @@ GRUB_MOD_FINI(legacycfg) grub_unregister_command (cmd_configfile); grub_unregister_command (cmd_kernel); grub_unregister_command (cmd_initrd); + grub_unregister_command (cmd_color); } diff --git a/grub-core/lib/legacy_parse.c b/grub-core/lib/legacy_parse.c index 61952d35d..1c502187d 100644 --- a/grub-core/lib/legacy_parse.c +++ b/grub-core/lib/legacy_parse.c @@ -62,7 +62,6 @@ struct legacy_command legacy_commands[] = "FILE1 FILE2", "Compare the file FILE1 with the FILE2 and inform the different values" " if any."}, - /* FIXME: Implement command. */ {"color", "legacy_color '%s' '%s'\n", 2, {TYPE_VERBATIM, TYPE_VERBATIM}, FLAG_IGNORE_REST, "NORMAL [HIGHLIGHT]", "Change the menu colors. The color NORMAL is used for most" From 2a87d7d1b6ebf745cedc9284419dd13b5ae29770 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Fri, 27 Aug 2010 22:34:25 +0200 Subject: [PATCH 12/37] Remove biglinux FIXME comment. It's a non-issue --- grub-core/commands/legacycfg.c | 2 +- grub-core/lib/legacy_parse.c | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/grub-core/commands/legacycfg.c b/grub-core/commands/legacycfg.c index db53f2c92..aca6d1e1f 100644 --- a/grub-core/commands/legacycfg.c +++ b/grub-core/commands/legacycfg.c @@ -190,7 +190,7 @@ grub_cmd_legacy_kernel (struct grub_command *mycmd __attribute__ ((unused)), continue; } - /* FIXME: what's the difference? */ + /* linux16 handles both zImages and bzImages. */ if (argc >= 1 && (grub_strcmp (args[0], "--type=linux") == 0 || grub_strcmp (args[0], "--type=biglinux") == 0)) { diff --git a/grub-core/lib/legacy_parse.c b/grub-core/lib/legacy_parse.c index 1c502187d..f2d3dec0a 100644 --- a/grub-core/lib/legacy_parse.c +++ b/grub-core/lib/legacy_parse.c @@ -120,7 +120,6 @@ struct legacy_command legacy_commands[] = /* install unsupported. */ /* ioprobe unsupported. */ /* FIXME: really support --no-mem-option. */ - /* FIXME: distinguish linux and biglinux. */ /* FIXME: dublicate multiboot filename. */ {"kernel", "legacy_kernel %s %s '%s' %s\n", 4, {TYPE_TYPE_OR_NOMEM_OPTION, TYPE_TYPE_OR_NOMEM_OPTION, From 5cd837bd47208c9e51b214109d7a57758e045e76 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Sun, 5 Sep 2010 20:43:43 +0200 Subject: [PATCH 13/37] Add testload --- grub-core/lib/legacy_parse.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/grub-core/lib/legacy_parse.c b/grub-core/lib/legacy_parse.c index f2d3dec0a..f350aaf09 100644 --- a/grub-core/lib/legacy_parse.c +++ b/grub-core/lib/legacy_parse.c @@ -205,7 +205,12 @@ struct legacy_command legacy_commands[] = /* setup unsupported. */ /* terminal unsupported. */ /* NUL_TERMINATE */ /* terminfo unsupported. */ /* NUL_TERMINATE */ - /* testload unsupported. */ + {"testload", "cat '%s'\n", 1, {TYPE_FILE}, 0, "FILE", + "Read the entire contents of FILE in several different ways and" + " compares them, to test the filesystem code. " + " If this test succeeds, then a good next" + " step is to try loading a kernel."}, + "Print the contents of the file FILE."}, /* testvbe unsupported. */ /* tftpserver unsupported. */ {"timeout", "set timeout=%s\n", 1, {TYPE_INT}, 0, "SEC", From 09695ab80c5f602c15c1897a9497870307f19362 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Sat, 11 Sep 2010 22:18:06 +0200 Subject: [PATCH 14/37] Fix few compile errors --- docs/man/grub-menulst2cfg.h2m | 3 +++ util/grub-menulst2cfg.c | 4 ++-- 2 files changed, 5 insertions(+), 2 deletions(-) create mode 100644 docs/man/grub-menulst2cfg.h2m diff --git a/docs/man/grub-menulst2cfg.h2m b/docs/man/grub-menulst2cfg.h2m new file mode 100644 index 000000000..0c0570f27 --- /dev/null +++ b/docs/man/grub-menulst2cfg.h2m @@ -0,0 +1,3 @@ +[NAME] +grub-menulst2cfg \- transform legacy menu.lst into grub.cfg + diff --git a/util/grub-menulst2cfg.c b/util/grub-menulst2cfg.c index fdbdda388..89b792e9a 100644 --- a/util/grub-menulst2cfg.c +++ b/util/grub-menulst2cfg.c @@ -31,7 +31,7 @@ main (int argc, char **argv) if (argc >= 2 && argv[1][0] == '-') { - fprintf (stderr, "Usage: %s [INFILE [OUTFILE]]\n", argv[0]); + fprintf (stdout, "Usage: %s [INFILE [OUTFILE]]\n", argv[0]); return 0; } @@ -79,7 +79,7 @@ main (int argc, char **argv) fprintf (out, "}\n\n"); if (oldname != entryname) fprintf (out, "menuentry \'%s\' {\n", - grub_legacy_escape (entryname, grub_strlen (entryname))); + grub_legacy_escape (entryname, strlen (entryname))); } if (parsed) From 9fb175ed9a48102543867f8006842628cc41217c Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Sat, 11 Sep 2010 22:18:41 +0200 Subject: [PATCH 15/37] Implement multiboot filename duplication in legacy parser --- grub-core/commands/legacycfg.c | 17 ++++++++++++----- grub-core/lib/legacy_parse.c | 16 ++++++++++------ 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/grub-core/commands/legacycfg.c b/grub-core/commands/legacycfg.c index aca6d1e1f..100464e69 100644 --- a/grub-core/commands/legacycfg.c +++ b/grub-core/commands/legacycfg.c @@ -179,6 +179,8 @@ grub_cmd_legacy_kernel (struct grub_command *mycmd __attribute__ ((unused)), int i; int no_mem_option = 0; struct grub_command *cmd; + char **cutargs; + int cutargc; for (i = 0; i < 2; i++) { /* FIXME: really support this. */ @@ -233,9 +235,14 @@ grub_cmd_legacy_kernel (struct grub_command *mycmd __attribute__ ((unused)), } } - if (!argc) + if (argc < 2) return grub_error (GRUB_ERR_BAD_ARGUMENT, "filename required"); + cutargs = grub_malloc (sizeof (cutargsp[0]) * (argc - 1)); + cutargc = argc - 1; + grub_memcpy (cutargs + 1, args + 2, sizeof (cutargsp[0]) * (argc - 2)); + cutargs[0] = args[0]; + do { /* First try Linux. */ @@ -244,7 +251,7 @@ grub_cmd_legacy_kernel (struct grub_command *mycmd __attribute__ ((unused)), cmd = grub_command_find ("linux16"); if (cmd) { - if (!(cmd->func) (cmd, argc, args)) + if (!(cmd->func) (cmd, cutargc, cutargs)) { kernel_type = LINUX; return GRUB_ERR_NONE; @@ -275,7 +282,7 @@ grub_cmd_legacy_kernel (struct grub_command *mycmd __attribute__ ((unused)), cmd = grub_command_find ("kfreebsd"); if (cmd) { - if (!(cmd->func) (cmd, argc, args)) + if (!(cmd->func) (cmd, cutargc, cutargs)) { kernel_type = KFREEBSD; return GRUB_ERR_NONE; @@ -288,7 +295,7 @@ grub_cmd_legacy_kernel (struct grub_command *mycmd __attribute__ ((unused)), cmd = grub_command_find ("knetbsd"); if (cmd) { - if (!(cmd->func) (cmd, argc, args)) + if (!(cmd->func) (cmd, cutargc, cutargs)) { kernel_type = KNETBSD; return GRUB_ERR_NONE; @@ -301,7 +308,7 @@ grub_cmd_legacy_kernel (struct grub_command *mycmd __attribute__ ((unused)), cmd = grub_command_find ("kopenbsd"); if (cmd) { - if (!(cmd->func) (cmd, argc, args)) + if (!(cmd->func) (cmd, cutargc, cutargs)) { kernel_type = KOPENBSD; return GRUB_ERR_NONE; diff --git a/grub-core/lib/legacy_parse.c b/grub-core/lib/legacy_parse.c index f350aaf09..694de097b 100644 --- a/grub-core/lib/legacy_parse.c +++ b/grub-core/lib/legacy_parse.c @@ -33,6 +33,7 @@ struct legacy_command TYPE_NOAPM_OPTION, TYPE_TYPE_OR_NOMEM_OPTION, TYPE_FILE, + TYPE_FILE_NO_CONSUME, TYPE_PARTITION, TYPE_BOOL, TYPE_INT, @@ -113,7 +114,8 @@ struct legacy_command legacy_commands[] = /* ifconfig unsupported. */ /* impsprobe unsupported. */ /* FIXME: dublicate multiboot filename. */ - {"initrd", "legacy_initrd '%s' %s\n", 2, {TYPE_FILE, TYPE_REST_VERBATIM}, 0, + {"initrd", "legacy_initrd '%s' %s\n", 2, {TYPE_FILE_NO_CONSUME, + TYPE_REST_VERBATIM}, 0, "FILE [ARG ...]", "Load an initial ramdisk FILE for a Linux format boot image and set the" " appropriate parameters in the Linux setup area in memory."}, @@ -122,9 +124,9 @@ struct legacy_command legacy_commands[] = /* FIXME: really support --no-mem-option. */ /* FIXME: dublicate multiboot filename. */ {"kernel", "legacy_kernel %s %s '%s' %s\n", 4, {TYPE_TYPE_OR_NOMEM_OPTION, - TYPE_TYPE_OR_NOMEM_OPTION, - TYPE_FILE, - TYPE_REST_VERBATIM}, 0, + TYPE_TYPE_OR_NOMEM_OPTION, + TYPE_FILE_NO_CONSUME, + TYPE_REST_VERBATIM}, 0, "[--no-mem-option] [--type=TYPE] FILE [ARG ...]", "Attempt to load the primary boot image from FILE. The rest of the" " line is passed verbatim as the \"kernel command line\". Any modules" @@ -143,7 +145,8 @@ struct legacy_command legacy_commands[] = " when you chain-load some operating systems, such as DOS, if such an" " OS resides at a non-first drive."}, /* md5crypt unsupported. */ - {"module", "legacy_initrd '%s' %s\n", 1, {TYPE_FILE, TYPE_REST_VERBATIM}, 0, + {"module", "legacy_initrd '%s' %s\n", 1, {TYPE_FILE_NO_CONSUME, + TYPE_REST_VERBATIM}, 0, "FILE [ARG ...]", "Load a boot module FILE for a Multiboot format boot image (no" " interpretation of the file contents is made, so users of this" @@ -210,7 +213,6 @@ struct legacy_command legacy_commands[] = " compares them, to test the filesystem code. " " If this test succeeds, then a good next" " step is to try loading a kernel."}, - "Print the contents of the file FILE."}, /* testvbe unsupported. */ /* tftpserver unsupported. */ {"timeout", "set timeout=%s\n", 1, {TYPE_INT}, 0, "SEC", @@ -399,6 +401,8 @@ grub_legacy_parse (const char *buf, char **entryname) ptr++; switch (legacy_commands[cmdnum].argt[i]) { + case TYPE_FILE_NO_CONSUME: + hold_arg = 1; case TYPE_PARTITION: case TYPE_FILE: args[j++] = adjust_file (curarg, curarglen); From 8bc402fbda9048e0a5b2a8509a3427a8842d09c4 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Sat, 11 Sep 2010 22:47:49 +0200 Subject: [PATCH 16/37] Remove obsolete FIXME comments --- grub-core/commands/legacycfg.c | 2 -- grub-core/lib/legacy_parse.c | 2 -- 2 files changed, 4 deletions(-) diff --git a/grub-core/commands/legacycfg.c b/grub-core/commands/legacycfg.c index b0253e707..6c0caad98 100644 --- a/grub-core/commands/legacycfg.c +++ b/grub-core/commands/legacycfg.c @@ -261,7 +261,6 @@ grub_cmd_legacy_kernel (struct grub_command *mycmd __attribute__ ((unused)), } /* Then multiboot. */ - /* FIXME: dublicate multiboot filename. */ if (kernel_type == GUESS_IT || kernel_type == MULTIBOOT) { cmd = grub_command_find ("multiboot"); @@ -339,7 +338,6 @@ grub_cmd_legacy_initrd (struct grub_command *mycmd __attribute__ ((unused)), } if (kernel_type == MULTIBOOT) { - /* FIXME: dublicate module filename. */ cmd = grub_command_find ("module"); if (!cmd) return grub_error (GRUB_ERR_BAD_ARGUMENT, "command module not found"); diff --git a/grub-core/lib/legacy_parse.c b/grub-core/lib/legacy_parse.c index 832b6cd1a..585c91b22 100644 --- a/grub-core/lib/legacy_parse.c +++ b/grub-core/lib/legacy_parse.c @@ -113,7 +113,6 @@ struct legacy_command legacy_commands[] = " its partition type code."}, /* ifconfig unsupported. */ /* impsprobe unsupported. */ - /* FIXME: dublicate multiboot filename. */ {"initrd", "legacy_initrd '%s' %s\n", 2, {TYPE_FILE_NO_CONSUME, TYPE_REST_VERBATIM}, 0, "FILE [ARG ...]", @@ -122,7 +121,6 @@ struct legacy_command legacy_commands[] = /* install unsupported. */ /* ioprobe unsupported. */ /* FIXME: really support --no-mem-option. */ - /* FIXME: dublicate multiboot filename. */ {"kernel", "legacy_kernel %s %s '%s' %s\n", 4, {TYPE_TYPE_OR_NOMEM_OPTION, TYPE_TYPE_OR_NOMEM_OPTION, TYPE_FILE_NO_CONSUME, From a37376e72a3c2e4d449d308e38f571a84f732550 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Sun, 12 Sep 2010 01:07:41 +0200 Subject: [PATCH 17/37] legacy_password implementation --- grub-core/commands/legacycfg.c | 193 ++++++++++++++++++++++++++++++++- grub-core/commands/password.c | 23 ++-- grub-core/lib/legacy_parse.c | 20 +++- include/grub/normal.h | 3 + 4 files changed, 226 insertions(+), 13 deletions(-) diff --git a/grub-core/commands/legacycfg.c b/grub-core/commands/legacycfg.c index 6c0caad98..f8e91b876 100644 --- a/grub-core/commands/legacycfg.c +++ b/grub-core/commands/legacycfg.c @@ -1,6 +1,6 @@ /* * GRUB -- GRand Unified Bootloader - * Copyright (C) 2010 Free Software Foundation, Inc. + * Copyright (C) 2000, 2001, 2010 Free Software Foundation, Inc. * * GRUB is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -28,6 +28,8 @@ #include #include #include +#include +#include static grub_err_t legacy_file (const char *filename) @@ -351,7 +353,7 @@ grub_cmd_legacy_initrd (struct grub_command *mycmd __attribute__ ((unused)), static grub_err_t grub_cmd_legacy_color (struct grub_command *mycmd __attribute__ ((unused)), - int argc, char **args) + int argc, char **args) { if (argc < 1) return grub_error (GRUB_ERR_BAD_ARGUMENT, "color required"); @@ -382,8 +384,188 @@ grub_cmd_legacy_color (struct grub_command *mycmd __attribute__ ((unused)), return grub_errno; } +static grub_err_t +check_password_deny (const char *user __attribute__ ((unused)), + const char *entered __attribute__ ((unused)), + void *password __attribute__ ((unused))) +{ + return GRUB_ACCESS_DENIED; +} + +#define MD5_HASHLEN 16 + +struct legacy_md5_password +{ + grub_uint8_t *salt; + int saltlen; + grub_uint8_t hash[MD5_HASHLEN]; +}; + +static int +check_password_md5_real (const char *entered, + struct legacy_md5_password *pw) +{ + int enteredlen = grub_strlen (entered); + unsigned char alt_result[MD5_HASHLEN]; + unsigned char *digest; + grub_uint8_t ctx[GRUB_MD_MD5->contextsize]; + int i; + + GRUB_MD_MD5->init (ctx); + GRUB_MD_MD5->write (ctx, entered, enteredlen); + GRUB_MD_MD5->write (ctx, pw->salt + 3, pw->saltlen - 3); + GRUB_MD_MD5->write (ctx, entered, enteredlen); + digest = GRUB_MD_MD5->read (ctx); + GRUB_MD_MD5->final (ctx); + memcpy (alt_result, digest, MD5_HASHLEN); + + GRUB_MD_MD5->init (ctx); + GRUB_MD_MD5->write (ctx, entered, enteredlen); + GRUB_MD_MD5->write (ctx, pw->salt, pw->saltlen); /* include the $1$ header */ + for (i = enteredlen; i > 16; i -= 16) + GRUB_MD_MD5->write (ctx, alt_result, 16); + GRUB_MD_MD5->write (ctx, alt_result, i); + + for (i = enteredlen; i > 0; i >>= 1) + GRUB_MD_MD5->write (ctx, entered + ((i & 1) ? enteredlen : 0), 1); + digest = GRUB_MD_MD5->read (ctx); + GRUB_MD_MD5->final (ctx); + + for (i = 0; i < 1000; i++) + { + memcpy (alt_result, digest, 16); + + GRUB_MD_MD5->init (ctx); + if ((i & 1) != 0) + GRUB_MD_MD5->write (ctx, entered, enteredlen); + else + GRUB_MD_MD5->write (ctx, alt_result, 16); + + if (i % 3 != 0) + GRUB_MD_MD5->write (ctx, pw->salt + 3, pw->saltlen - 3); + + if (i % 7 != 0) + GRUB_MD_MD5->write (ctx, entered, enteredlen); + + if ((i & 1) != 0) + GRUB_MD_MD5->write (ctx, alt_result, 16); + else + GRUB_MD_MD5->write (ctx, entered, enteredlen); + digest = GRUB_MD_MD5->read (ctx); + GRUB_MD_MD5->final (ctx); + } + + return (grub_crypto_memcmp (digest, pw->hash, MD5_HASHLEN) == 0); +} + +static grub_err_t +check_password_md5 (const char *user, + const char *entered, + void *password) +{ + if (!check_password_md5_real (entered, password)) + return GRUB_ACCESS_DENIED; + + grub_auth_authenticate (user); + + return GRUB_ERR_NONE; +} + +static inline int +ib64t (char c) +{ + if (c == '.') + return 0; + if (c == '/') + return 1; + if (c >= '0' && c <= '9') + return c - '0' + 2; + if (c >= 'A' && c <= 'Z') + return c - 'A' + 12; + if (c >= 'a' && c <= 'z') + return c - 'a' + 38; + return -1; +} + +static grub_err_t +grub_cmd_legacy_password (struct grub_command *mycmd __attribute__ ((unused)), + int argc, char **args) +{ + const char *salt, *saltend; + const char *p; + struct legacy_md5_password *pw = NULL; + int i; + + if (argc == 0) + return grub_error (GRUB_ERR_BAD_ARGUMENT, "arguments expected"); + if (args[0][0] != '-' || args[0][1] != '-') + return grub_normal_set_password ("legacy", args[0]); + if (grub_memcmp (args[0], "--md5", sizeof ("--md5")) != 0) + goto fail; + if (argc == 1) + goto fail; + if (grub_strlen(args[1]) <= 3) + goto fail; + salt = args[1]; + saltend = grub_strchr (salt + 3, '$'); + if (!saltend) + goto fail; + pw = grub_malloc (sizeof (*pw)); + if (!pw) + goto fail; + + p = saltend + 1; + for (i = 0; i < 5; i++) + { + int n; + grub_uint32_t w = 0; + + for (n = 0; n < 4; n++) + { + int ww = ib64t(*p++); + if (ww == -1) + goto fail; + w |= ww << (n * 6); + } + pw->hash[i == 4 ? 5 : 12+i] = w & 0xff; + pw->hash[6+i] = (w >> 8) & 0xff; + pw->hash[i] = (w >> 16) & 0xff; + } + { + int n; + grub_uint32_t w = 0; + for (n = 0; n < 2; n++) + { + int ww = ib64t(*p++); + if (ww == -1) + goto fail; + w |= ww << (6 * n); + } + if (w >= 0x100) + goto fail; + pw->hash[11] = w; + } + + pw->saltlen = saltend - salt; + pw->salt = (grub_uint8_t *) grub_strndup (salt, pw->saltlen); + if (!pw->salt) + goto fail; + + return grub_auth_register_authentication ("legacy", check_password_md5, pw); + + fail: + grub_free (pw); + /* This is to imitate minor difference between grub-legacy in GRUB2. + If 2 password commands are executed in a row and second one fails + on GRUB2 the password of first one is used, whereas in grub-legacy + authenthication is denied. In case of no password command was executed + early both versions deny any access. */ + return grub_auth_register_authentication ("legacy", check_password_deny, + NULL); +} + static grub_command_t cmd_source, cmd_configfile, cmd_kernel, cmd_initrd; -static grub_command_t cmd_color; +static grub_command_t cmd_color, cmd_password; GRUB_MOD_INIT(legacycfg) { @@ -407,6 +589,10 @@ GRUB_MOD_INIT(legacycfg) grub_cmd_legacy_color, N_("NORMAL [HIGHLIGHT]"), N_("Simulate grub-legacy color command")); + cmd_password = grub_register_command ("legacy_password", + grub_cmd_legacy_password, + N_("[--md5] PASSWD [FILE]"), + N_("Simulate grub-legacy password command")); } GRUB_MOD_FINI(legacycfg) @@ -416,4 +602,5 @@ GRUB_MOD_FINI(legacycfg) grub_unregister_command (cmd_kernel); grub_unregister_command (cmd_initrd); grub_unregister_command (cmd_color); + grub_unregister_command (cmd_password); } diff --git a/grub-core/commands/password.c b/grub-core/commands/password.c index 04285254e..db5951cbb 100644 --- a/grub-core/commands/password.c +++ b/grub-core/commands/password.c @@ -40,26 +40,22 @@ check_password (const char *user, const char *entered, return GRUB_ERR_NONE; } -static grub_err_t -grub_cmd_password (grub_command_t cmd __attribute__ ((unused)), - int argc, char **args) +grub_err_t +grub_normal_set_password (const char *user, const char *password) { grub_err_t err; char *pass; int copylen; - if (argc != 2) - return grub_error (GRUB_ERR_BAD_ARGUMENT, "two arguments expected"); - pass = grub_zalloc (GRUB_AUTH_MAX_PASSLEN); if (!pass) return grub_errno; - copylen = grub_strlen (args[1]); + copylen = grub_strlen (password); if (copylen >= GRUB_AUTH_MAX_PASSLEN) copylen = GRUB_AUTH_MAX_PASSLEN - 1; - grub_memcpy (pass, args[1], copylen); + grub_memcpy (pass, password, copylen); - err = grub_auth_register_authentication (args[0], check_password, pass); + err = grub_auth_register_authentication (user, check_password, pass); if (err) { grub_free (pass); @@ -69,6 +65,15 @@ grub_cmd_password (grub_command_t cmd __attribute__ ((unused)), return GRUB_ERR_NONE; } +static grub_err_t +grub_cmd_password (grub_command_t cmd __attribute__ ((unused)), + int argc, char **args) +{ + if (argc != 2) + return grub_error (GRUB_ERR_BAD_ARGUMENT, "two arguments expected"); + return grub_normal_set_password (args[0], args[1]); +} + static grub_command_t cmd; GRUB_MOD_INIT(password) diff --git a/grub-core/lib/legacy_parse.c b/grub-core/lib/legacy_parse.c index 585c91b22..34ebd19c5 100644 --- a/grub-core/lib/legacy_parse.c +++ b/grub-core/lib/legacy_parse.c @@ -32,6 +32,7 @@ struct legacy_command TYPE_FORCE_OPTION, TYPE_NOAPM_OPTION, TYPE_TYPE_OR_NOMEM_OPTION, + TYPE_OPTION, TYPE_FILE, TYPE_FILE_NO_CONSUME, TYPE_PARTITION, @@ -159,7 +160,21 @@ struct legacy_command legacy_commands[] = /* partnew unsupported. */ {"parttype", "parttool '%s' type=%s\n", 2, {TYPE_PARTITION, TYPE_INT}, 0, "PART TYPE", "Change the type of the partition PART to TYPE."}, - /* password unsupported. */ /* NUL_TERMINATE */ + /* FIXME: support config file reloading. */ + /* FIXME: support usage in menuentry. */ + {"password", "if [ \"$superusers\" = "" ]; then superusers=legacy; fi; " + "legacy_password %s '%s' %s", 3, {TYPE_OPTION, TYPE_VERBATIM, + TYPE_FILE}, FLAG_IGNORE_REST, + "[--md5] PASSWD [FILE]", + "If used in the first section of a menu file, disable all" + " interactive editing control (menu entry editor and" + " command line). If the password PASSWD is entered, it loads the" + " FILE as a new config file and restarts the GRUB Stage 2. If you" + " omit the argument FILE, then GRUB just unlocks privileged" + " instructions. You can also use it in the script section, in" + " which case it will ask for the password, before continuing." + " The option --md5 tells GRUB that PASSWD is encrypted with" + " md5crypt."}, /* pause unsupported. */ /* rarp unsupported. */ {"read", "read_dword %s\n", 1, {TYPE_INT}, 0, "ADDR", @@ -323,6 +338,8 @@ is_option (enum arg_type opt, const char *curarg, grub_size_t len) || check_option (curarg, "--type=biglinux", len) || check_option (curarg, "--type=multiboot", len) || check_option (curarg, "--no-mem-option", len); + case TYPE_OPTION: + return (len >= 2 && curarg[0] == '-' && curarg[1] == '-'); default: return 0; } @@ -453,6 +470,7 @@ grub_legacy_parse (const char *buf, char **entryname) case TYPE_FORCE_OPTION: case TYPE_NOAPM_OPTION: case TYPE_TYPE_OR_NOMEM_OPTION: + case TYPE_OPTION: if (is_option (legacy_commands[cmdnum].argt[i], curarg, curarglen)) { args[j++] = grub_strndup (curarg, curarglen); diff --git a/include/grub/normal.h b/include/grub/normal.h index df7f70142..417560d9f 100644 --- a/include/grub/normal.h +++ b/include/grub/normal.h @@ -120,4 +120,7 @@ grub_normal_add_menu_entry (int argc, const char **args, char **classes, const char *users, const char *hotkey, const char *prefix, const char *sourcecode); +grub_err_t +grub_normal_set_password (const char *user, const char *password); + #endif /* ! GRUB_NORMAL_HEADER */ From 237a43b1c191211d4d64e749ba52c5ede622fce6 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Sun, 12 Sep 2010 02:30:48 +0200 Subject: [PATCH 18/37] Support few more legacy commands --- grub-core/commands/legacycfg.c | 44 +++++++++++++++++++++++++- grub-core/lib/legacy_parse.c | 56 +++++++++++++++++++--------------- 2 files changed, 75 insertions(+), 25 deletions(-) diff --git a/grub-core/commands/legacycfg.c b/grub-core/commands/legacycfg.c index f8e91b876..aeff78b8a 100644 --- a/grub-core/commands/legacycfg.c +++ b/grub-core/commands/legacycfg.c @@ -351,6 +351,42 @@ grub_cmd_legacy_initrd (struct grub_command *mycmd __attribute__ ((unused)), "no kernel with module support is loaded in legacy way"); } +static grub_err_t +grub_cmd_legacy_initrdnounzip (struct grub_command *mycmd __attribute__ ((unused)), + int argc, char **args) +{ + struct grub_command *cmd; + + if (kernel_type == LINUX) + { + cmd = grub_command_find ("initrd16"); + if (!cmd) + return grub_error (GRUB_ERR_BAD_ARGUMENT, "command initrd16 not found"); + + return cmd->func (cmd, argc, args); + } + if (kernel_type == MULTIBOOT) + { + char **newargs; + grub_err_t err; + newargs = grub_malloc ((argc + 1) * sizeof (newargs[0])); + if (!newargs) + return grub_errno; + grub_memcpy (newargs + 1, args, argc * sizeof (newargs[0])); + newargs[0] = "--nounzip"; + cmd = grub_command_find ("module"); + if (!cmd) + return grub_error (GRUB_ERR_BAD_ARGUMENT, "command module not found"); + + err = cmd->func (cmd, argc + 1, newargs); + grub_free (newargs); + return err; + } + + return grub_error (GRUB_ERR_BAD_ARGUMENT, + "no kernel with module support is loaded in legacy way"); +} + static grub_err_t grub_cmd_legacy_color (struct grub_command *mycmd __attribute__ ((unused)), int argc, char **args) @@ -565,7 +601,7 @@ grub_cmd_legacy_password (struct grub_command *mycmd __attribute__ ((unused)), } static grub_command_t cmd_source, cmd_configfile, cmd_kernel, cmd_initrd; -static grub_command_t cmd_color, cmd_password; +static grub_command_t cmd_color, cmd_password, cmd_initrdnounzip; GRUB_MOD_INIT(legacycfg) { @@ -581,6 +617,11 @@ GRUB_MOD_INIT(legacycfg) grub_cmd_legacy_initrd, N_("FILE [ARG ...]"), N_("Simulate grub-legacy initrd command")); + cmd_initrdnounzip = grub_register_command ("legacy_initrd_nounzip", + grub_cmd_legacy_initrdnounzip, + N_("FILE [ARG ...]"), + N_("Simulate grub-legacy modulenounzip command")); + cmd_configfile = grub_register_command ("legacy_configfile", grub_cmd_legacy_configfile, N_("FILE"), @@ -601,6 +642,7 @@ GRUB_MOD_FINI(legacycfg) grub_unregister_command (cmd_configfile); grub_unregister_command (cmd_kernel); grub_unregister_command (cmd_initrd); + grub_unregister_command (cmd_initrdnounzip); grub_unregister_command (cmd_color); grub_unregister_command (cmd_password); } diff --git a/grub-core/lib/legacy_parse.c b/grub-core/lib/legacy_parse.c index 34ebd19c5..671d0f3f8 100644 --- a/grub-core/lib/legacy_parse.c +++ b/grub-core/lib/legacy_parse.c @@ -53,7 +53,7 @@ struct legacy_command legacy_commands[] = "Print the blocklist notation of the file FILE."}, {"boot", "boot\n", 0, {}, 0, 0, "Boot the OS/chain-loader which has been loaded."}, - /* bootp unsupported. */ + /* FIXME: bootp unsupported. */ {"cat", "cat '%s'\n", 1, {TYPE_FILE}, 0, "FILE", "Print the contents of the file FILE."}, {"chainloader", "chainloader %s '%s'\n", 2, {TYPE_FORCE_OPTION, TYPE_FILE}, @@ -87,8 +87,8 @@ struct legacy_command legacy_commands[] = "[NUM | `saved']", "Set the default entry to entry number NUM (if not specified, it is" " 0, the first entry) or the entry number saved by savedefault."}, - /* dhcp unsupported. */ - /* displayapm unsupported. */ + /* FIXME: dhcp unsupported. */ + /* FIXME: displayapm unsupported. */ {"displaymem", "lsmmap\n", 0, {}, 0, 0, "Display what GRUB thinks the system address space map of the" " machine is, including all regions of physical RAM installed."}, @@ -102,25 +102,25 @@ struct legacy_command legacy_commands[] = {"find", "search -sf '%s'\n", 1, {TYPE_FILE}, 0, "FILENAME", "Search for the filename FILENAME in all of partitions and print the list of" " the devices which contain the file."}, - /* fstest unsupported. */ - /* geometry unsupported. */ + /* FIXME: fstest unsupported. */ + /* FIXME: geometry unsupported. */ {"halt", "halt %s\n", 1, {TYPE_NOAPM_OPTION}, 0, "[--no-apm]", "Halt your system. If APM is available on it, turn off the power using" " the APM BIOS, unless you specify the option `--no-apm'."}, - /* help unsupported. */ /* NUL_TERMINATE */ - /* hiddenmenu unsupported. */ + /* FIXME: help unsupported. */ /* NUL_TERMINATE */ + /* FIXME: hiddenmenu unsupported. */ {"hide", "parttool '%s' hidden+\n", 1, {TYPE_PARTITION}, 0, "PARTITION", "Hide PARTITION by setting the \"hidden\" bit in" " its partition type code."}, - /* ifconfig unsupported. */ - /* impsprobe unsupported. */ + /* FIXME: ifconfig unsupported. */ + /* FIXME: impsprobe unsupported. */ {"initrd", "legacy_initrd '%s' %s\n", 2, {TYPE_FILE_NO_CONSUME, TYPE_REST_VERBATIM}, 0, "FILE [ARG ...]", "Load an initial ramdisk FILE for a Linux format boot image and set the" " appropriate parameters in the Linux setup area in memory."}, /* install unsupported. */ - /* ioprobe unsupported. */ + /* FIXME: ioprobe unsupported. */ /* FIXME: really support --no-mem-option. */ {"kernel", "legacy_kernel %s %s '%s' %s\n", 4, {TYPE_TYPE_OR_NOMEM_OPTION, TYPE_TYPE_OR_NOMEM_OPTION, @@ -134,7 +134,7 @@ struct legacy_command legacy_commands[] = " \"netbsd\", \"freebsd\", \"openbsd\", \"linux\", \"biglinux\" and" " \"multiboot\". The option --no-mem-option tells GRUB not to pass a" " Linux's mem option automatically."}, - /* lock is unsupported. */ + /* FIXME: lock is unsupported. */ {"makeactive", "parttool \"$root\" boot+\n", 0, {}, 0, 0, "Set the active partition on the root disk to GRUB's root device." " This command is limited to _primary_ PC partitions on a hard disk."}, @@ -143,7 +143,8 @@ struct legacy_command legacy_commands[] = "Map the drive FROM_DRIVE to the drive TO_DRIVE. This is necessary" " when you chain-load some operating systems, such as DOS, if such an" " OS resides at a non-first drive."}, - /* md5crypt unsupported. */ + /* md5crypt unsupported since GRUB has not enough entropy and this + hash shouldn't be used anymore. */ {"module", "legacy_initrd '%s' %s\n", 1, {TYPE_FILE_NO_CONSUME, TYPE_REST_VERBATIM}, 0, "FILE [ARG ...]", @@ -152,12 +153,16 @@ struct legacy_command legacy_commands[] = " command must know what the kernel in question expects). The" " rest of the line is passed as the \"module command line\", like" " the `kernel' command."}, - /* modulenounzip unsupported. */ + {"modulenounzip", "legacy_initrd_nounzip '%s' %s\n", 1, + {TYPE_FILE_NO_CONSUME, TYPE_REST_VERBATIM}, 0, + "FILE [ARG ...]", + "The same as `module', except that automatic decompression is" + " disabled."}, /* FIXME: allow toggle. */ {"pager", "set pager=%d\n", 1, {TYPE_BOOL}, 0, "[FLAG]", "Toggle pager mode with no argument. If FLAG is given and its value" " is `on', turn on the mode. If FLAG is `off', turn off the mode."}, - /* partnew unsupported. */ + /* FIXME: partnew unsupported. */ {"parttype", "parttool '%s' type=%s\n", 2, {TYPE_PARTITION, TYPE_INT}, 0, "PART TYPE", "Change the type of the partition PART to TYPE."}, /* FIXME: support config file reloading. */ @@ -175,8 +180,8 @@ struct legacy_command legacy_commands[] = " which case it will ask for the password, before continuing." " The option --md5 tells GRUB that PASSWD is encrypted with" " md5crypt."}, - /* pause unsupported. */ - /* rarp unsupported. */ + /* FIXME: pause unsupported. */ + /* FIXME: rarp unsupported. */ {"read", "read_dword %s\n", 1, {TYPE_INT}, 0, "ADDR", "Read a 32-bit value from memory at address ADDR and" " display it in hex format."}, @@ -217,17 +222,17 @@ struct legacy_command legacy_commands[] = " STOP is the length of stop bit(s). The option --device can be used only" " in the grub shell, which specifies the file name of a tty device. The" " default values are COM1, 9600, 8N1."}, - /* setkey unsupported. */ /* NUL_TERMINATE */ - /* setup unsupported. */ - /* terminal unsupported. */ /* NUL_TERMINATE */ - /* terminfo unsupported. */ /* NUL_TERMINATE */ + /* FIXME: setkey unsupported. */ /* NUL_TERMINATE */ + /* FIXME: setup unsupported. */ + /* FIXME: terminal unsupported. */ /* NUL_TERMINATE */ + /* FIXME: terminfo unsupported. */ /* NUL_TERMINATE */ {"testload", "cat '%s'\n", 1, {TYPE_FILE}, 0, "FILE", "Read the entire contents of FILE in several different ways and" " compares them, to test the filesystem code. " " If this test succeeds, then a good next" " step is to try loading a kernel."}, - /* testvbe unsupported. */ - /* tftpserver unsupported. */ + /* FIXME: testvbe unsupported. */ + /* FIXME: tftpserver unsupported. */ {"timeout", "set timeout=%s\n", 1, {TYPE_INT}, 0, "SEC", "Set a timeout, in SEC seconds, before automatically booting the" " default entry (normally the first entry defined)."}, @@ -235,10 +240,13 @@ struct legacy_command legacy_commands[] = {"unhide", "parttool '%s' hidden-\n", 1, {TYPE_PARTITION}, 0, "PARTITION", "Unhide PARTITION by clearing the \"hidden\" bit in its" " partition type code."}, - /* uppermem unsupported. */ + /* FIXME: uppermem unsupported. */ {"uuid", "search -u '%s'\n", 1, {TYPE_VERBATIM}, 0, "UUID", "Find root by UUID"}, - /* vbeprobe unsupported. */ + /* FIXME: support MODE. */ + {"vbeprobe", "vbeinfo", 0, {}, 0, "[MODE]", + "Probe VBE information. If the mode number MODE is specified, show only" + " the information about only the mode."} }; char * From 6c6850ae13bff8975c9682aa59db0c09ad1edde8 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Sun, 12 Sep 2010 02:55:24 +0200 Subject: [PATCH 19/37] Implement hiddenmenu (not tested) --- grub-core/commands/legacycfg.c | 48 ++++++++++++++++++++++++++++------ grub-core/lib/legacy_parse.c | 12 ++++++--- include/grub/legacy_parse.h | 2 +- util/grub-menulst2cfg.c | 13 ++++++++- 4 files changed, 62 insertions(+), 13 deletions(-) diff --git a/grub-core/commands/legacycfg.c b/grub-core/commands/legacycfg.c index aeff78b8a..82901b0e7 100644 --- a/grub-core/commands/legacycfg.c +++ b/grub-core/commands/legacycfg.c @@ -37,6 +37,18 @@ legacy_file (const char *filename) grub_file_t file; char *entryname = NULL, *entrysrc = NULL; grub_menu_t menu; + char *suffix = grub_strdup (""); + + auto grub_err_t getline (char **line, int cont); + grub_err_t getline (char **line, + int cont __attribute__ ((unused))) + { + *line = 0; + return GRUB_ERR_NONE; + } + + if (!suffix) + return grub_errno; file = grub_file_open (filename); if (! file) @@ -68,10 +80,32 @@ legacy_file (const char *filename) { char *oldname = NULL; + int is_suffix; oldname = entryname; - parsed = grub_legacy_parse (buf, &entryname); + parsed = grub_legacy_parse (buf, &entryname, &is_suffix); grub_free (buf); + if (is_suffix) + { + char *t; + + t = suffix; + suffix = grub_realloc (suffix, grub_strlen (suffix) + + grub_strlen (parsed) + 1); + if (!suffix) + { + grub_free (t); + grub_free (entrysrc); + grub_free (parsed); + grub_free (suffix); + return grub_errno; + } + grub_memcpy (entrysrc + grub_strlen (entrysrc), parsed, + grub_strlen (parsed) + 1); + grub_free (parsed); + parsed = NULL; + continue; + } if (oldname != entryname && oldname) { const char **args = grub_malloc (sizeof (args[0])); @@ -88,13 +122,6 @@ legacy_file (const char *filename) if (parsed && !entryname) { - auto grub_err_t getline (char **line, int cont); - grub_err_t getline (char **line __attribute__ ((unused)), - int cont __attribute__ ((unused))) - { - return GRUB_ERR_NONE; - } - grub_normal_parse_line (parsed, getline); grub_print_error (); grub_free (parsed); @@ -114,6 +141,7 @@ legacy_file (const char *filename) { grub_free (t); grub_free (parsed); + grub_free (suffix); return grub_errno; } grub_memcpy (entrysrc + grub_strlen (entrysrc), parsed, @@ -137,6 +165,10 @@ legacy_file (const char *filename) grub_normal_add_menu_entry (1, args, NULL, NULL, NULL, NULL, entrysrc); } + grub_normal_parse_line (suffix, getline); + grub_print_error (); + grub_free (suffix); + if (menu && menu->size) grub_show_menu (menu, 1); diff --git a/grub-core/lib/legacy_parse.c b/grub-core/lib/legacy_parse.c index 671d0f3f8..3800d6ca5 100644 --- a/grub-core/lib/legacy_parse.c +++ b/grub-core/lib/legacy_parse.c @@ -41,7 +41,8 @@ struct legacy_command TYPE_REST_VERBATIM } argt[4]; enum { - FLAG_IGNORE_REST = 1 + FLAG_IGNORE_REST = 1, + FLAG_SUFFIX = 2 } flags; const char *shortdesc; const char *longdesc; @@ -108,7 +109,8 @@ struct legacy_command legacy_commands[] = "Halt your system. If APM is available on it, turn off the power using" " the APM BIOS, unless you specify the option `--no-apm'."}, /* FIXME: help unsupported. */ /* NUL_TERMINATE */ - /* FIXME: hiddenmenu unsupported. */ + {"hiddenmenu", "if sleep -i $timeout; then timeout=0; else timeout=-1; fi\n", + 0, {}, FLAG_SUFFIX, "", "Hide the menu."}, {"hide", "parttool '%s' hidden+\n", 1, {TYPE_PARTITION}, 0, "PARTITION", "Hide PARTITION by setting the \"hidden\" bit in" " its partition type code."}, @@ -354,12 +356,14 @@ is_option (enum arg_type opt, const char *curarg, grub_size_t len) } char * -grub_legacy_parse (const char *buf, char **entryname) +grub_legacy_parse (const char *buf, char **entryname, int *suffix) { const char *ptr; const char *cmdname; unsigned i, cmdnum; + *suffix = 0; + for (ptr = buf; *ptr && grub_isspace (*ptr); ptr++); if (!*ptr || *ptr == '#') return grub_strdup (buf); @@ -392,6 +396,8 @@ grub_legacy_parse (const char *buf, char **entryname) if (cmdnum == ARRAY_SIZE (legacy_commands)) return grub_xasprintf ("# Unsupported legacy command: %s\n", buf); + *suffix = !!(legacy_commands[cmdnum].flags & FLAG_SUFFIX); + for (; grub_isspace (*ptr) || *ptr == '='; ptr++); char *args[ARRAY_SIZE (legacy_commands[0].argt)]; diff --git a/include/grub/legacy_parse.h b/include/grub/legacy_parse.h index fce4e3e40..a6394496f 100644 --- a/include/grub/legacy_parse.h +++ b/include/grub/legacy_parse.h @@ -21,7 +21,7 @@ #include -char *grub_legacy_parse (const char *buf, char **entryname); +char *grub_legacy_parse (const char *buf, char **entryname, int *suffix); char *grub_legacy_escape (const char *in, grub_size_t len); #endif diff --git a/util/grub-menulst2cfg.c b/util/grub-menulst2cfg.c index 89b792e9a..0184b3fe3 100644 --- a/util/grub-menulst2cfg.c +++ b/util/grub-menulst2cfg.c @@ -28,6 +28,9 @@ main (int argc, char **argv) char *entryname = NULL; char *buf = NULL; size_t bufsize = 0; + char *suffix = xstrdup (""); + int suffixlen = 0; + int is_suffix = 0; if (argc >= 2 && argv[1][0] == '-') { @@ -74,7 +77,14 @@ main (int argc, char **argv) char *oldname = NULL; oldname = entryname; - parsed = grub_legacy_parse (buf, &entryname); + parsed = grub_legacy_parse (buf, &entryname, &is_suffix); + if (is_suffix) + { + suffixlen += strlen (parsed); + suffix = xrealloc (suffix, suffixlen + 1); + strcat (suffix, parsed); + continue; + } if (oldname != entryname && oldname) fprintf (out, "}\n\n"); if (oldname != entryname) @@ -89,6 +99,7 @@ main (int argc, char **argv) if (entryname) fprintf (out, "}\n\n"); + fwrite (out, 1, suffixlen, suffix); if (in != stdin) fclose (in); From 07473cf9175f5fc0d0b78b71f5bc3e3fe703820d Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Sun, 12 Sep 2010 03:05:19 +0200 Subject: [PATCH 20/37] Support pause --- grub-core/lib/legacy_parse.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/grub-core/lib/legacy_parse.c b/grub-core/lib/legacy_parse.c index 3800d6ca5..4e038807c 100644 --- a/grub-core/lib/legacy_parse.c +++ b/grub-core/lib/legacy_parse.c @@ -93,7 +93,7 @@ struct legacy_command legacy_commands[] = {"displaymem", "lsmmap\n", 0, {}, 0, 0, "Display what GRUB thinks the system address space map of the" " machine is, including all regions of physical RAM installed."}, - /* embed unsupported. */ + /* NOTE: embed unsupported. */ {"fallback", "set fallback='%s'\n", 1, {TYPE_VERBATIM}, 0, "NUM...", "Go into unattended boot mode: if the default boot entry has any" " errors, instead of waiting for the user to do anything, it" @@ -121,7 +121,7 @@ struct legacy_command legacy_commands[] = "FILE [ARG ...]", "Load an initial ramdisk FILE for a Linux format boot image and set the" " appropriate parameters in the Linux setup area in memory."}, - /* install unsupported. */ + /* NOTE: install unsupported. */ /* FIXME: ioprobe unsupported. */ /* FIXME: really support --no-mem-option. */ {"kernel", "legacy_kernel %s %s '%s' %s\n", 4, {TYPE_TYPE_OR_NOMEM_OPTION, @@ -145,7 +145,7 @@ struct legacy_command legacy_commands[] = "Map the drive FROM_DRIVE to the drive TO_DRIVE. This is necessary" " when you chain-load some operating systems, such as DOS, if such an" " OS resides at a non-first drive."}, - /* md5crypt unsupported since GRUB has not enough entropy and this + /* NOTE: md5crypt unsupported since GRUB has not enough entropy and this hash shouldn't be used anymore. */ {"module", "legacy_initrd '%s' %s\n", 1, {TYPE_FILE_NO_CONSUME, TYPE_REST_VERBATIM}, 0, @@ -182,7 +182,12 @@ struct legacy_command legacy_commands[] = " which case it will ask for the password, before continuing." " The option --md5 tells GRUB that PASSWD is encrypted with" " md5crypt."}, - /* FIXME: pause unsupported. */ + /* NOTE: GRUB2 has a design principle of not eternally waiting for user + input. 60 seconds should be enough. + */ + {"pause", "echo %s; if ! sleep -i 60; then return; fi", 1, + {TYPE_REST_VERBATIM}, 0, + "[MESSAGE ...]", "Print MESSAGE, then wait until a key is pressed."}, /* FIXME: rarp unsupported. */ {"read", "read_dword %s\n", 1, {TYPE_INT}, 0, "ADDR", "Read a 32-bit value from memory at address ADDR and" @@ -208,7 +213,7 @@ struct legacy_command legacy_commands[] = " GRUB can read, but setting the correct root device is still" " desired. Note that the items mentioned in `root' which" " derived from attempting the mount will NOT work correctly."}, - /* FIXME: support arguments. */ + /* FIXME: support saving NUM and fallback. */ {"savedefault", "saved_entry=${chosen}; save_env saved_entry\n", 0, {}, 0, "[NUM | `fallback']", "Save the current entry as the default boot entry if no argument is" From 43cce9e0955c22cda56eef3d3054aedb1b4892bf Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Sun, 12 Sep 2010 11:01:21 +0200 Subject: [PATCH 21/37] Fix uninitialised usage of curarg --- grub-core/lib/legacy_parse.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/grub-core/lib/legacy_parse.c b/grub-core/lib/legacy_parse.c index 4e038807c..797594c15 100644 --- a/grub-core/lib/legacy_parse.c +++ b/grub-core/lib/legacy_parse.c @@ -411,10 +411,10 @@ grub_legacy_parse (const char *buf, char **entryname, int *suffix) { unsigned j = 0; int hold_arg = 0; + const char *curarg = NULL; for (i = 0; i < legacy_commands[cmdnum].argc; i++) { - const char *curarg; - grub_size_t curarglen; + grub_size_t curarglen; if (hold_arg) { ptr = curarg; From 966446589259f507b5f327f3bb7ede0deab4b4db Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Sun, 12 Sep 2010 11:09:01 +0200 Subject: [PATCH 22/37] Add missing newlines --- grub-core/lib/legacy_parse.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/grub-core/lib/legacy_parse.c b/grub-core/lib/legacy_parse.c index 797594c15..dabc497be 100644 --- a/grub-core/lib/legacy_parse.c +++ b/grub-core/lib/legacy_parse.c @@ -169,7 +169,7 @@ struct legacy_command legacy_commands[] = "PART TYPE", "Change the type of the partition PART to TYPE."}, /* FIXME: support config file reloading. */ /* FIXME: support usage in menuentry. */ - {"password", "if [ \"$superusers\" = "" ]; then superusers=legacy; fi; " + {"password", "if [ \"$superusers\" = "" ]; then superusers=legacy; fi;\n" "legacy_password %s '%s' %s", 3, {TYPE_OPTION, TYPE_VERBATIM, TYPE_FILE}, FLAG_IGNORE_REST, "[--md5] PASSWD [FILE]", @@ -185,7 +185,7 @@ struct legacy_command legacy_commands[] = /* NOTE: GRUB2 has a design principle of not eternally waiting for user input. 60 seconds should be enough. */ - {"pause", "echo %s; if ! sleep -i 60; then return; fi", 1, + {"pause", "echo %s; if ! sleep -i 60; then return; fi\n", 1, {TYPE_REST_VERBATIM}, 0, "[MESSAGE ...]", "Print MESSAGE, then wait until a key is pressed."}, /* FIXME: rarp unsupported. */ @@ -251,7 +251,7 @@ struct legacy_command legacy_commands[] = {"uuid", "search -u '%s'\n", 1, {TYPE_VERBATIM}, 0, "UUID", "Find root by UUID"}, /* FIXME: support MODE. */ - {"vbeprobe", "vbeinfo", 0, {}, 0, "[MODE]", + {"vbeprobe", "vbeinfo\n", 0, {}, 0, "[MODE]", "Probe VBE information. If the mode number MODE is specified, show only" " the information about only the mode."} }; From abda0cade5a5d6af5017a9b06bf27614909b8255 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Sun, 12 Sep 2010 13:58:18 +0200 Subject: [PATCH 23/37] Enable legacy_parser on emu --- grub-core/Makefile.core.def | 1 + 1 file changed, 1 insertion(+) diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def index cbc5b7438..36a6e6564 100644 --- a/grub-core/Makefile.core.def +++ b/grub-core/Makefile.core.def @@ -1411,6 +1411,7 @@ module = { common = commands/legacycfg.c; common = lib/legacy_parse.c; enable = i386_pc; + enable = emu; }; module = { From 64ad6157ae4f9d68be1b96ad7882544241a3e0f4 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Sun, 12 Sep 2010 14:01:02 +0200 Subject: [PATCH 24/37] Fix bunch of memory problems and implement hdbias --- grub-core/commands/legacycfg.c | 147 +++++++++++++++++++++++++-------- grub-core/lib/legacy_parse.c | 36 ++++++-- util/grub-menulst2cfg.c | 14 +++- 3 files changed, 153 insertions(+), 44 deletions(-) diff --git a/grub-core/commands/legacycfg.c b/grub-core/commands/legacycfg.c index 82901b0e7..e42ca5878 100644 --- a/grub-core/commands/legacycfg.c +++ b/grub-core/commands/legacycfg.c @@ -30,6 +30,8 @@ #include #include #include +#include +#include static grub_err_t legacy_file (const char *filename) @@ -67,7 +69,7 @@ legacy_file (const char *filename) while (1) { char *buf = grub_file_getline (file); - char *parsed; + char *parsed = NULL; if (!buf && grub_errno) { @@ -84,7 +86,7 @@ legacy_file (const char *filename) oldname = entryname; parsed = grub_legacy_parse (buf, &entryname, &is_suffix); - grub_free (buf); + buf = NULL; if (is_suffix) { char *t; @@ -100,7 +102,7 @@ legacy_file (const char *filename) grub_free (suffix); return grub_errno; } - grub_memcpy (entrysrc + grub_strlen (entrysrc), parsed, + grub_memcpy (suffix + grub_strlen (suffix), parsed, grub_strlen (parsed) + 1); grub_free (parsed); parsed = NULL; @@ -117,6 +119,9 @@ legacy_file (const char *filename) args[0] = oldname; grub_normal_add_menu_entry (1, args, NULL, NULL, NULL, NULL, entrysrc); + grub_free (args); + entrysrc[0] = 0; + grub_free (oldname); } } @@ -125,6 +130,7 @@ legacy_file (const char *filename) grub_normal_parse_line (parsed, getline); grub_print_error (); grub_free (parsed); + parsed = NULL; } else if (parsed) { @@ -163,11 +169,13 @@ legacy_file (const char *filename) } args[0] = entryname; grub_normal_add_menu_entry (1, args, NULL, NULL, NULL, NULL, entrysrc); + grub_free (args); } grub_normal_parse_line (suffix, getline); grub_print_error (); grub_free (suffix); + grub_free (entrysrc); if (menu && menu->size) grub_show_menu (menu, 1); @@ -215,6 +223,7 @@ grub_cmd_legacy_kernel (struct grub_command *mycmd __attribute__ ((unused)), struct grub_command *cmd; char **cutargs; int cutargc; + for (i = 0; i < 2; i++) { /* FIXME: really support this. */ @@ -309,46 +318,118 @@ grub_cmd_legacy_kernel (struct grub_command *mycmd __attribute__ ((unused)), grub_errno = GRUB_ERR_NONE; } - /* k*BSD didn't really work well with grub-legacy. */ - if (kernel_type == GUESS_IT || kernel_type == KFREEBSD) + { + int bsd_device = -1; + int bsd_slice = -1; + int bsd_part = -1; { - cmd = grub_command_find ("kfreebsd"); - if (cmd) + grub_device_t dev; + char *hdbiasstr; + int hdbias = 0; + hdbiasstr = grub_env_get ("legacy_hdbias"); + if (hdbiasstr) { - if (!(cmd->func) (cmd, cutargc, cutargs)) - { - kernel_type = KFREEBSD; - return GRUB_ERR_NONE; - } + hdbias = grub_strtoul (hdbiasstr, 0, 0); + grub_errno = GRUB_ERR_NONE; + } + dev = grub_device_open (0); + if (dev && dev->disk + && dev->disk->dev->id == GRUB_DISK_DEVICE_BIOSDISK_ID + && dev->disk->dev->id >= 0x80 && dev->disk->dev->id <= 0x90) + { + struct grub_partition *part = dev->disk->partition; + bsd_device = dev->disk->id - 0x80 - hdbias; + if (part && (grub_strcmp (part->partmap->name, "netbsd") == 0 + || grub_strcmp (part->partmap->name, "openbsd") == 0 + || grub_strcmp (part->partmap->name, "bsd") == 0)) + { + bsd_part = part->number; + part = part->parent; + } + if (part && grub_strcmp (part->partmap->name, "msdos") == 0) + bsd_slice = part->number; } - grub_errno = GRUB_ERR_NONE; } - if (kernel_type == GUESS_IT || kernel_type == KNETBSD) + + /* k*BSD didn't really work well with grub-legacy. */ + if (kernel_type == GUESS_IT || kernel_type == KFREEBSD) + { + char buf[sizeof("adXXXXXXXXXXXXsXXXXXXXXXXXXYYY")]; + if (bsd_device != -1) + { + if (bsd_slice != -1 && bsd_part != -1) + grub_snprintf(buf, sizeof(buf), "ad%ds%d%c", bsd_device, + bsd_slice, 'a' + bsd_part); + else if (bsd_slice != -1) + grub_snprintf(buf, sizeof(buf), "ad%ds%d", bsd_device, + bsd_slice); + else + grub_snprintf(buf, sizeof(buf), "ad%d", bsd_device); + grub_env_set ("kFreeBSD.vfs.root.mountfrom", buf); + } + else + grub_env_unset ("kFreeBSD.vfs.root.mountfrom"); + cmd = grub_command_find ("kfreebsd"); + if (cmd) + { + if (!(cmd->func) (cmd, cutargc, cutargs)) + { + kernel_type = KFREEBSD; + return GRUB_ERR_NONE; + } + } + grub_errno = GRUB_ERR_NONE; + } { - cmd = grub_command_find ("knetbsd"); - if (cmd) + char **bsdargs; + int bsdargc; + char bsddevname[sizeof ("wdXXXXXXXXXXXXY")]; + if (bsd_device == -1) { - if (!(cmd->func) (cmd, cutargc, cutargs)) - { - kernel_type = KNETBSD; - return GRUB_ERR_NONE; - } + bsdargs = cutargs; + bsdargc = cutargc; } - grub_errno = GRUB_ERR_NONE; - } - if (kernel_type == GUESS_IT || kernel_type == KOPENBSD) - { - cmd = grub_command_find ("kopenbsd"); - if (cmd) + else { - if (!(cmd->func) (cmd, cutargc, cutargs)) - { - kernel_type = KOPENBSD; - return GRUB_ERR_NONE; - } + bsdargc = cutargc + 2; + bsdargs = grub_malloc (sizeof (bsdargs[0]) * bsdargc); + grub_memcpy (bsdargs, args, argc * sizeof (bsdargs[0])); + bsdargs[argc] = "-r"; + bsdargs[argc + 1] = bsddevname; + grub_snprintf (bsddevname, sizeof (bsddevname), + "wd%d%c", bsd_device, + bsd_part != -1 ? bsd_part + 'a' : 'c'); } - grub_errno = GRUB_ERR_NONE; + if (kernel_type == GUESS_IT || kernel_type == KNETBSD) + { + cmd = grub_command_find ("knetbsd"); + if (cmd) + { + if (!(cmd->func) (cmd, bsdargc, bsdargs)) + { + kernel_type = KNETBSD; + return GRUB_ERR_NONE; + } + } + grub_errno = GRUB_ERR_NONE; + } + if (kernel_type == GUESS_IT || kernel_type == KOPENBSD) + { + cmd = grub_command_find ("kopenbsd"); + if (cmd) + { + if (!(cmd->func) (cmd, bsdargc, bsdargs)) + { + kernel_type = KOPENBSD; + return GRUB_ERR_NONE; + } + } + grub_errno = GRUB_ERR_NONE; + } + if (bsdargs != cutargs) + grub_free (bsdargs); } + } } while (0); diff --git a/grub-core/lib/legacy_parse.c b/grub-core/lib/legacy_parse.c index dabc497be..01b836087 100644 --- a/grub-core/lib/legacy_parse.c +++ b/grub-core/lib/legacy_parse.c @@ -195,7 +195,9 @@ struct legacy_command legacy_commands[] = {"reboot", "reboot\n", 0, {}, 0, 0, "Reboot your system."}, /* FIXME: Support HDBIAS. */ /* FIXME: Support printing. */ - {"root", "set root='%s'\n", 1, {TYPE_PARTITION}, 0, "[DEVICE [HDBIAS]]", + {"root", "set root='%s'; set legacy_hdbias='%s'\n", + 2, {TYPE_PARTITION, TYPE_INT}, 0, + "[DEVICE [HDBIAS]]", "Set the current \"root device\" to the device DEVICE, then" " attempt to mount it to get the partition size (for passing the" " partition descriptor in `ES:ESI', used by some chain-loaded" @@ -206,7 +208,8 @@ struct legacy_command legacy_commands[] = " how many BIOS drive numbers are on controllers before the current" " one. For example, if there is an IDE disk and a SCSI disk, and your" " FreeBSD root partition is on the SCSI disk, then use a `1' for HDBIAS."}, - {"rootnoverify", "set root='%s'\n", 1, {TYPE_PARTITION}, 0, + {"rootnoverify", "set root='%s'; set legacy_hdbias='%s'\n", + 2, {TYPE_PARTITION, TYPE_INT}, 0, "[DEVICE [HDBIAS]]", "Similar to `root', but don't attempt to mount the partition. This" " is useful for when an OS is outside of the area of the disk that" @@ -265,7 +268,7 @@ grub_legacy_escape (const char *in, grub_size_t len) for (ptr = in; ptr < in + len && *ptr; ptr++) if (*ptr == '\'' || *ptr == '\\') overhead++; - ret = grub_malloc (ptr - in + overhead); + ret = grub_malloc (ptr - in + overhead + 1); if (!ret) return NULL; outptr = ret; @@ -371,7 +374,15 @@ grub_legacy_parse (const char *buf, char **entryname, int *suffix) for (ptr = buf; *ptr && grub_isspace (*ptr); ptr++); if (!*ptr || *ptr == '#') - return grub_strdup (buf); + { + char *ret; + int len = grub_strlen (buf); + ret = grub_malloc (len + 2); + grub_memcpy (ret, buf, len); + ret[len] = '\n'; + ret[len + 1] = 0; + return ret; + } cmdname = ptr; for (ptr = buf; *ptr && !grub_isspace (*ptr) && *ptr != '='; ptr++); @@ -412,7 +423,7 @@ grub_legacy_parse (const char *buf, char **entryname, int *suffix) unsigned j = 0; int hold_arg = 0; const char *curarg = NULL; - for (i = 0; i < legacy_commands[cmdnum].argc; i++) + for (i = 0; i < legacy_commands[cmdnum].argc + hold_arg; i++) { grub_size_t curarglen; if (hold_arg) @@ -495,7 +506,7 @@ grub_legacy_parse (const char *buf, char **entryname, int *suffix) args[j++] = grub_strndup (curarg, curarglen); break; } - args[j++] = ""; + args[j++] = grub_strdup (""); hold_arg = 1; break; case TYPE_INT: @@ -536,7 +547,14 @@ grub_legacy_parse (const char *buf, char **entryname, int *suffix) } } } - - return grub_xasprintf (legacy_commands[cmdnum].map, args[0], args[1], args[2], - args[3]); + + { + char *ret = grub_xasprintf (legacy_commands[cmdnum].map, args[0], args[1], + args[2], args[3]); + grub_free (args[0]); + grub_free (args[1]); + grub_free (args[2]); + grub_free (args[3]); + return ret; + } } diff --git a/util/grub-menulst2cfg.c b/util/grub-menulst2cfg.c index 0184b3fe3..38a906c0a 100644 --- a/util/grub-menulst2cfg.c +++ b/util/grub-menulst2cfg.c @@ -88,12 +88,18 @@ main (int argc, char **argv) if (oldname != entryname && oldname) fprintf (out, "}\n\n"); if (oldname != entryname) - fprintf (out, "menuentry \'%s\' {\n", - grub_legacy_escape (entryname, strlen (entryname))); + { + char *escaped = grub_legacy_escape (entryname, strlen (entryname)); + fprintf (out, "menuentry \'%s\' {\n", escaped); + grub_free (escaped); + grub_free (oldname); + } } if (parsed) fprintf (out, "%s%s", entryname ? " " : "", parsed); + grub_free (parsed); + parsed = NULL; } if (entryname) @@ -101,6 +107,10 @@ main (int argc, char **argv) fwrite (out, 1, suffixlen, suffix); + grub_free (buf); + grub_free (suffix); + grub_free (entryname); + if (in != stdin) fclose (in); if (out != stdout) From b2b260b9eb009126690c700e00b902d5ba1b7ba8 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Sun, 12 Sep 2010 14:04:10 +0200 Subject: [PATCH 25/37] REmove obsolete FIXME --- grub-core/lib/legacy_parse.c | 1 - 1 file changed, 1 deletion(-) diff --git a/grub-core/lib/legacy_parse.c b/grub-core/lib/legacy_parse.c index 01b836087..44dc31d81 100644 --- a/grub-core/lib/legacy_parse.c +++ b/grub-core/lib/legacy_parse.c @@ -193,7 +193,6 @@ struct legacy_command legacy_commands[] = "Read a 32-bit value from memory at address ADDR and" " display it in hex format."}, {"reboot", "reboot\n", 0, {}, 0, 0, "Reboot your system."}, - /* FIXME: Support HDBIAS. */ /* FIXME: Support printing. */ {"root", "set root='%s'; set legacy_hdbias='%s'\n", 2, {TYPE_PARTITION, TYPE_INT}, 0, From 21d7be66125b6cfe48dbc9110e42a7e436548138 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Sun, 12 Sep 2010 14:18:39 +0200 Subject: [PATCH 26/37] Support (hd0,1,a legacy partition specification --- grub-core/lib/legacy_parse.c | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/grub-core/lib/legacy_parse.c b/grub-core/lib/legacy_parse.c index 44dc31d81..2bf372de3 100644 --- a/grub-core/lib/legacy_parse.c +++ b/grub-core/lib/legacy_parse.c @@ -288,7 +288,7 @@ adjust_file (const char *in, grub_size_t len) const char *comma, *ptr, *rest; char *ret, *outptr; int overhead = 0; - int part; + int part = -1, subpart = -1; if (in[0] != '(') return grub_legacy_escape (in, len); for (ptr = in + 1; ptr < in + len && *ptr && *ptr != ')' @@ -299,12 +299,17 @@ adjust_file (const char *in, grub_size_t len) if (*comma != ',') return grub_legacy_escape (in, len); part = grub_strtoull (comma + 1, (char **) &rest, 0); + if (rest[0] == ',' && rest[1] >= 'a' && rest[1] <= 'z') + { + subpart = rest[1] - 'a'; + rest += 2; + } for (ptr = rest; ptr < in + len && *ptr; ptr++) if (*ptr == '\'' || *ptr == '\\') overhead++; - /* 30 is enough for any number. */ - ret = grub_malloc (ptr - in + overhead + 30); + /* 35 is enough for any 2 numbers. */ + ret = grub_malloc (ptr - in + overhead + 35); if (!ret) return NULL; @@ -316,7 +321,10 @@ adjust_file (const char *in, grub_size_t len) *outptr++ = *ptr; } - grub_snprintf (outptr, 30, "%d", part + 1); + if (subpart != -1) + grub_snprintf (outptr, 35, "%d,%d", part + 1, subpart + 1); + else + grub_snprintf (outptr, 35, "%d", part + 1); while (*outptr) outptr++; for (ptr = rest; ptr < in + len; ptr++) @@ -378,8 +386,13 @@ grub_legacy_parse (const char *buf, char **entryname, int *suffix) int len = grub_strlen (buf); ret = grub_malloc (len + 2); grub_memcpy (ret, buf, len); - ret[len] = '\n'; - ret[len + 1] = 0; + if (len && ret[len - 1] == '\n') + ret[len] = 0; + else + { + ret[len] = '\n'; + ret[len + 1] = 0; + } return ret; } From 281d690594631f846ea76585fe154aa79f4d8beb Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Sun, 12 Sep 2010 15:26:38 +0200 Subject: [PATCH 27/37] Add no-argument version of commands and remove legacy_color --- grub-core/commands/legacycfg.c | 49 ++-------------- grub-core/lib/legacy_parse.c | 102 ++++++++++++++++++++++++++------- 2 files changed, 88 insertions(+), 63 deletions(-) diff --git a/grub-core/commands/legacycfg.c b/grub-core/commands/legacycfg.c index e42ca5878..ca69a7817 100644 --- a/grub-core/commands/legacycfg.c +++ b/grub-core/commands/legacycfg.c @@ -500,39 +500,6 @@ grub_cmd_legacy_initrdnounzip (struct grub_command *mycmd __attribute__ ((unused "no kernel with module support is loaded in legacy way"); } -static grub_err_t -grub_cmd_legacy_color (struct grub_command *mycmd __attribute__ ((unused)), - int argc, char **args) -{ - if (argc < 1) - return grub_error (GRUB_ERR_BAD_ARGUMENT, "color required"); - grub_env_set ("color_normal", args[0]); - if (argc >= 2) - grub_env_set ("color_highlight", args[1]); - else - { - char *slash = grub_strchr (args[0], '/'); - char *invert; - grub_size_t len; - - len = grub_strlen (args[0]); - if (!slash) - return grub_error (GRUB_ERR_BAD_ARGUMENT, "bad color specification %s", - args[0]); - invert = grub_malloc (len + 1); - if (!invert) - return grub_errno; - grub_memcpy (invert, slash + 1, len - (slash - args[0]) - 1); - invert[len - (slash - args[0]) - 1] = '/'; - grub_memcpy (invert + len - (slash - args[0]), args[0], slash - args[0]); - invert[len] = 0; - grub_env_set ("color_highlight", invert); - grub_free (invert); - } - - return grub_errno; -} - static grub_err_t check_password_deny (const char *user __attribute__ ((unused)), const char *entered __attribute__ ((unused)), @@ -714,13 +681,18 @@ grub_cmd_legacy_password (struct grub_command *mycmd __attribute__ ((unused)), } static grub_command_t cmd_source, cmd_configfile, cmd_kernel, cmd_initrd; -static grub_command_t cmd_color, cmd_password, cmd_initrdnounzip; +static grub_command_t cmd_password, cmd_initrdnounzip; GRUB_MOD_INIT(legacycfg) { cmd_source = grub_register_command ("legacy_source", grub_cmd_legacy_source, N_("FILE"), N_("Parse legacy config")); + cmd_configfile = grub_register_command ("legacy_configfile", + grub_cmd_legacy_configfile, + N_("FILE"), + N_("Parse legacy config")); + cmd_kernel = grub_register_command ("legacy_kernel", grub_cmd_legacy_kernel, N_("[--no-mem-option] [--type=TYPE] FILE [ARG ...]"), @@ -735,14 +707,6 @@ GRUB_MOD_INIT(legacycfg) N_("FILE [ARG ...]"), N_("Simulate grub-legacy modulenounzip command")); - cmd_configfile = grub_register_command ("legacy_configfile", - grub_cmd_legacy_configfile, - N_("FILE"), - N_("Parse legacy config")); - cmd_color = grub_register_command ("legacy_color", - grub_cmd_legacy_color, - N_("NORMAL [HIGHLIGHT]"), - N_("Simulate grub-legacy color command")); cmd_password = grub_register_command ("legacy_password", grub_cmd_legacy_password, N_("[--md5] PASSWD [FILE]"), @@ -756,6 +720,5 @@ GRUB_MOD_FINI(legacycfg) grub_unregister_command (cmd_kernel); grub_unregister_command (cmd_initrd); grub_unregister_command (cmd_initrdnounzip); - grub_unregister_command (cmd_color); grub_unregister_command (cmd_password); } diff --git a/grub-core/lib/legacy_parse.c b/grub-core/lib/legacy_parse.c index 2bf372de3..096975434 100644 --- a/grub-core/lib/legacy_parse.c +++ b/grub-core/lib/legacy_parse.c @@ -41,8 +41,11 @@ struct legacy_command TYPE_REST_VERBATIM } argt[4]; enum { - FLAG_IGNORE_REST = 1, - FLAG_SUFFIX = 2 + FLAG_IGNORE_REST = 1, + FLAG_SUFFIX = 2, + FLAG_FALLBACK_AVAILABLE = 4, + FLAG_FALLBACK = 8, + FLAG_COLOR_INVERT = 16, } flags; const char *shortdesc; const char *longdesc; @@ -65,8 +68,9 @@ struct legacy_command legacy_commands[] = "FILE1 FILE2", "Compare the file FILE1 with the FILE2 and inform the different values" " if any."}, - {"color", "legacy_color '%s' '%s'\n", 2, {TYPE_VERBATIM, TYPE_VERBATIM}, - FLAG_IGNORE_REST, "NORMAL [HIGHLIGHT]", + {"color", "set color_normal='%s'; set color_highlight='%s'\n", + 2, {TYPE_VERBATIM, TYPE_VERBATIM}, + FLAG_IGNORE_REST | FLAG_FALLBACK_AVAILABLE, "NORMAL [HIGHLIGHT]", "Change the menu colors. The color NORMAL is used for most" " lines in the menu, and the color HIGHLIGHT is used to highlight the" " line where the cursor points. If you omit HIGHLIGHT, then the" @@ -77,6 +81,9 @@ struct legacy_command legacy_commands[] = " light-green, light-cyan, light-red, light-magenta, yellow and white." " But only the first eight names can be used for BG. You can prefix" " \"blink-\" to FG if you want a blinking foreground color."}, + {"color", "set color_normal='%s'; set color_highlight='%s'\n", + 1, {TYPE_VERBATIM}, + FLAG_IGNORE_REST | FLAG_FALLBACK | FLAG_COLOR_INVERT, NULL, NULL}, {"configfile", "legacy_configfile '%s'\n", 1, {TYPE_FILE}, 0, "FILE", "Load FILE as the configuration file."}, {"debug", @@ -160,10 +167,15 @@ struct legacy_command legacy_commands[] = "FILE [ARG ...]", "The same as `module', except that automatic decompression is" " disabled."}, - /* FIXME: allow toggle. */ - {"pager", "set pager=%d\n", 1, {TYPE_BOOL}, 0, "[FLAG]", + {"pager", "set pager=%s; if [ \"$pager\" = 0 ]; then " + " echo Internal pager is now off; else echo Internal pager is now on; fi\n", + 1, {TYPE_BOOL}, FLAG_FALLBACK_AVAILABLE, "[FLAG]", "Toggle pager mode with no argument. If FLAG is given and its value" " is `on', turn on the mode. If FLAG is `off', turn off the mode."}, + {"pager", + "if [ \"$pager\" = 1 ]; then pager=0; echo Internal pager is now off;" + "else pager=1; echo Internal pager is now on; fi\n", 0, {}, + FLAG_FALLBACK, NULL, NULL}, /* FIXME: partnew unsupported. */ {"parttype", "parttool '%s' type=%s\n", 2, {TYPE_PARTITION, TYPE_INT}, 0, "PART TYPE", "Change the type of the partition PART to TYPE."}, @@ -193,9 +205,8 @@ struct legacy_command legacy_commands[] = "Read a 32-bit value from memory at address ADDR and" " display it in hex format."}, {"reboot", "reboot\n", 0, {}, 0, 0, "Reboot your system."}, - /* FIXME: Support printing. */ {"root", "set root='%s'; set legacy_hdbias='%s'\n", - 2, {TYPE_PARTITION, TYPE_INT}, 0, + 2, {TYPE_PARTITION, TYPE_INT}, FLAG_FALLBACK_AVAILABLE, "[DEVICE [HDBIAS]]", "Set the current \"root device\" to the device DEVICE, then" " attempt to mount it to get the partition size (for passing the" @@ -207,6 +218,7 @@ struct legacy_command legacy_commands[] = " how many BIOS drive numbers are on controllers before the current" " one. For example, if there is an IDE disk and a SCSI disk, and your" " FreeBSD root partition is on the SCSI disk, then use a `1' for HDBIAS."}, + {"root", "echo \"$root\"\n", 0, {}, FLAG_FALLBACK, NULL, NULL}, {"rootnoverify", "set root='%s'; set legacy_hdbias='%s'\n", 2, {TYPE_PARTITION, TYPE_INT}, 0, "[DEVICE [HDBIAS]]", @@ -215,6 +227,7 @@ struct legacy_command legacy_commands[] = " GRUB can read, but setting the correct root device is still" " desired. Note that the items mentioned in `root' which" " derived from attempting the mount will NOT work correctly."}, + {"rootnoverify", "echo \"$root\"\n", 0, {}, FLAG_FALLBACK, NULL, NULL}, /* FIXME: support saving NUM and fallback. */ {"savedefault", "saved_entry=${chosen}; save_env saved_entry\n", 0, {}, 0, "[NUM | `fallback']", @@ -432,7 +445,6 @@ grub_legacy_parse (const char *buf, char **entryname, int *suffix) grub_memset (args, 0, sizeof (args)); { - unsigned j = 0; int hold_arg = 0; const char *curarg = NULL; for (i = 0; i < legacy_commands[cmdnum].argc + hold_arg; i++) @@ -445,6 +457,8 @@ grub_legacy_parse (const char *buf, char **entryname, int *suffix) } for (; grub_isspace (*ptr); ptr++); curarg = ptr; + if (!*curarg) + break; for (; *ptr && !grub_isspace (*ptr); ptr++); if (i != legacy_commands[cmdnum].argc - 1 || (legacy_commands[cmdnum].flags & FLAG_IGNORE_REST)) @@ -463,7 +477,7 @@ grub_legacy_parse (const char *buf, char **entryname, int *suffix) hold_arg = 1; case TYPE_PARTITION: case TYPE_FILE: - args[j++] = adjust_file (curarg, curarglen); + args[i] = adjust_file (curarg, curarglen); break; case TYPE_REST_VERBATIM: @@ -481,7 +495,7 @@ grub_legacy_parse (const char *buf, char **entryname, int *suffix) ptr++; overhead += 3; } - outptr0 = args[j++] = grub_malloc (overhead + (ptr - curarg)); + outptr0 = args[i] = grub_malloc (overhead + (ptr - curarg)); if (!outptr0) return NULL; ptr = curarg; @@ -507,7 +521,7 @@ grub_legacy_parse (const char *buf, char **entryname, int *suffix) break; case TYPE_VERBATIM: - args[j++] = grub_legacy_escape (curarg, curarglen); + args[i] = grub_legacy_escape (curarg, curarglen); break; case TYPE_FORCE_OPTION: case TYPE_NOAPM_OPTION: @@ -515,10 +529,10 @@ grub_legacy_parse (const char *buf, char **entryname, int *suffix) case TYPE_OPTION: if (is_option (legacy_commands[cmdnum].argt[i], curarg, curarglen)) { - args[j++] = grub_strndup (curarg, curarglen); + args[i] = grub_strndup (curarg, curarglen); break; } - args[j++] = grub_strdup (""); + args[i] = grub_strdup (""); hold_arg = 1; break; case TYPE_INT: @@ -526,8 +540,6 @@ grub_legacy_parse (const char *buf, char **entryname, int *suffix) const char *brk; int base = 10; brk = curarg; - if (curarglen < 1) - args[j++] = grub_strdup ("0"); if (brk[0] == '0' && brk[1] == 'x') base = 16; else if (brk[0] == '0') @@ -545,21 +557,71 @@ grub_legacy_parse (const char *buf, char **entryname, int *suffix) break; } if (brk == curarg) - args[j++] = grub_strdup ("0"); + args[i] = grub_strdup ("0"); else - args[j++] = grub_strndup (curarg, brk - curarg); + args[i] = grub_strndup (curarg, brk - curarg); } break; case TYPE_BOOL: if (curarglen == 2 && curarg[0] == 'o' && curarg[1] == 'n') - args[j++] = grub_strdup ("1"); + args[i] = grub_strdup ("1"); else - args[j++] = grub_strdup ("0"); + args[i] = grub_strdup ("0"); break; } } } + while (legacy_commands[cmdnum].argc > 0 + && args[legacy_commands[cmdnum].argc - 1] == NULL + && (legacy_commands[cmdnum].flags & FLAG_FALLBACK_AVAILABLE) + && args[legacy_commands[cmdnum + 1].argc] == NULL) + cmdnum++; + + for (; i < legacy_commands[cmdnum].argc; i++) + switch (legacy_commands[cmdnum].argt[i]) + { + case TYPE_FILE_NO_CONSUME: + case TYPE_PARTITION: + case TYPE_FILE: + case TYPE_REST_VERBATIM: + case TYPE_VERBATIM: + case TYPE_FORCE_OPTION: + case TYPE_NOAPM_OPTION: + case TYPE_TYPE_OR_NOMEM_OPTION: + case TYPE_OPTION: + args[i] = grub_strdup (""); + break; + case TYPE_BOOL: + case TYPE_INT: + args[i] = grub_strdup ("0"); + break; + } + + if (legacy_commands[cmdnum].flags & FLAG_COLOR_INVERT) + { + char *corig = args[legacy_commands[cmdnum].argc - 1]; + char *slash = grub_strchr (corig, '/'); + char *invert; + grub_size_t len; + + len = grub_strlen (corig); + if (!slash) + { + grub_error (GRUB_ERR_BAD_ARGUMENT, "bad color specification %s", + args[0]); + return NULL; + } + invert = grub_malloc (len + 1); + if (!invert) + return NULL; + grub_memcpy (invert, slash + 1, len - (slash - corig) - 1); + invert[len - (slash - args[0]) - 1] = '/'; + grub_memcpy (invert + len - (slash - corig), corig, slash - corig); + invert[len] = 0; + args[legacy_commands[cmdnum].argc] = invert; + } + { char *ret = grub_xasprintf (legacy_commands[cmdnum].map, args[0], args[1], args[2], args[3]); From e64334df29ed3394b9c5f3cb7ad46d06d3160b36 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Sun, 12 Sep 2010 15:50:52 +0200 Subject: [PATCH 28/37] Support mixed inline and suffix commands --- grub-core/commands/legacycfg.c | 18 ++--- grub-core/lib/legacy_parse.c | 128 ++++++++++++++++++--------------- include/grub/legacy_parse.h | 2 +- util/grub-menulst2cfg.c | 26 +++---- 4 files changed, 94 insertions(+), 80 deletions(-) diff --git a/grub-core/commands/legacycfg.c b/grub-core/commands/legacycfg.c index ca69a7817..bea608b9e 100644 --- a/grub-core/commands/legacycfg.c +++ b/grub-core/commands/legacycfg.c @@ -82,31 +82,31 @@ legacy_file (const char *filename) { char *oldname = NULL; - int is_suffix; + char *newsuffix; oldname = entryname; - parsed = grub_legacy_parse (buf, &entryname, &is_suffix); + parsed = grub_legacy_parse (buf, &entryname, &newsuffix); buf = NULL; - if (is_suffix) + if (newsuffix) { char *t; t = suffix; suffix = grub_realloc (suffix, grub_strlen (suffix) - + grub_strlen (parsed) + 1); + + grub_strlen (newsuffix) + 1); if (!suffix) { grub_free (t); grub_free (entrysrc); grub_free (parsed); + grub_free (newsuffix); grub_free (suffix); return grub_errno; } - grub_memcpy (suffix + grub_strlen (suffix), parsed, - grub_strlen (parsed) + 1); - grub_free (parsed); - parsed = NULL; - continue; + grub_memcpy (suffix + grub_strlen (suffix), newsuffix, + grub_strlen (newsuffix) + 1); + grub_free (newsuffix); + newsuffix = NULL; } if (oldname != entryname && oldname) { diff --git a/grub-core/lib/legacy_parse.c b/grub-core/lib/legacy_parse.c index 096975434..692b1b7d4 100644 --- a/grub-core/lib/legacy_parse.c +++ b/grub-core/lib/legacy_parse.c @@ -26,6 +26,8 @@ struct legacy_command { const char *name; const char *map; + const char *suffix; + unsigned suffixarg; unsigned argc; enum arg_type { TYPE_VERBATIM, @@ -42,7 +44,6 @@ struct legacy_command } argt[4]; enum { FLAG_IGNORE_REST = 1, - FLAG_SUFFIX = 2, FLAG_FALLBACK_AVAILABLE = 4, FLAG_FALLBACK = 8, FLAG_COLOR_INVERT = 16, @@ -53,22 +54,22 @@ struct legacy_command struct legacy_command legacy_commands[] = { - {"blocklist", "blocklist '%s'\n", 1, {TYPE_FILE}, 0, "FILE", + {"blocklist", "blocklist '%s'\n", NULL, 0, 1, {TYPE_FILE}, 0, "FILE", "Print the blocklist notation of the file FILE."}, - {"boot", "boot\n", 0, {}, 0, 0, + {"boot", "boot\n", NULL, 0, 0, {}, 0, 0, "Boot the OS/chain-loader which has been loaded."}, /* FIXME: bootp unsupported. */ - {"cat", "cat '%s'\n", 1, {TYPE_FILE}, 0, "FILE", + {"cat", "cat '%s'\n", NULL, 0, 1, {TYPE_FILE}, 0, "FILE", "Print the contents of the file FILE."}, - {"chainloader", "chainloader %s '%s'\n", 2, {TYPE_FORCE_OPTION, TYPE_FILE}, - 0, "[--force] FILE", + {"chainloader", "chainloader %s '%s'\n", NULL, 0, + 2, {TYPE_FORCE_OPTION, TYPE_FILE}, 0, "[--force] FILE", "Load the chain-loader FILE. If --force is specified, then load it" " forcibly, whether the boot loader signature is present or not."}, - {"cmp", "cmp '%s' '%s'\n", 2, {TYPE_FILE, TYPE_FILE}, FLAG_IGNORE_REST, - "FILE1 FILE2", + {"cmp", "cmp '%s' '%s'\n", NULL, 0, + 2, {TYPE_FILE, TYPE_FILE}, FLAG_IGNORE_REST, "FILE1 FILE2", "Compare the file FILE1 with the FILE2 and inform the different values" " if any."}, - {"color", "set color_normal='%s'; set color_highlight='%s'\n", + {"color", "set color_normal='%s'; set color_highlight='%s'\n", NULL, 0, 2, {TYPE_VERBATIM, TYPE_VERBATIM}, FLAG_IGNORE_REST | FLAG_FALLBACK_AVAILABLE, "NORMAL [HIGHLIGHT]", "Change the menu colors. The color NORMAL is used for most" @@ -81,60 +82,62 @@ struct legacy_command legacy_commands[] = " light-green, light-cyan, light-red, light-magenta, yellow and white." " But only the first eight names can be used for BG. You can prefix" " \"blink-\" to FG if you want a blinking foreground color."}, - {"color", "set color_normal='%s'; set color_highlight='%s'\n", + {"color", "set color_normal='%s'; set color_highlight='%s'\n", NULL, 0, 1, {TYPE_VERBATIM}, FLAG_IGNORE_REST | FLAG_FALLBACK | FLAG_COLOR_INVERT, NULL, NULL}, - {"configfile", "legacy_configfile '%s'\n", 1, {TYPE_FILE}, 0, "FILE", - "Load FILE as the configuration file."}, + {"configfile", "legacy_configfile '%s'\n", NULL, 0, 1, {TYPE_FILE}, + 0, "FILE", "Load FILE as the configuration file."}, {"debug", - "if [ -z \"$debug\" ]; then set debug=all; else set debug=; fi\n", + "if [ -z \"$debug\" ]; then set debug=all; else set debug=; fi\n", NULL, 0, 0, {}, 0, 0, "Turn on/off the debug mode."}, {"default", "set default='%s'; if [ x\"$default\" = xsaved ]; then load_env; " - "set default=\"$saved_entry\"; fi\n", 1, {TYPE_VERBATIM}, 0, + "set default=\"$saved_entry\"; fi\n", NULL, 0, 1, {TYPE_VERBATIM}, 0, "[NUM | `saved']", "Set the default entry to entry number NUM (if not specified, it is" " 0, the first entry) or the entry number saved by savedefault."}, /* FIXME: dhcp unsupported. */ /* FIXME: displayapm unsupported. */ - {"displaymem", "lsmmap\n", 0, {}, 0, 0, + {"displaymem", "lsmmap\n", NULL, 0, 0, {}, 0, 0, "Display what GRUB thinks the system address space map of the" " machine is, including all regions of physical RAM installed."}, /* NOTE: embed unsupported. */ - {"fallback", "set fallback='%s'\n", 1, {TYPE_VERBATIM}, 0, "NUM...", + {"fallback", "set fallback='%s'\n", NULL, 0, + 1, {TYPE_VERBATIM}, 0, "NUM...", "Go into unattended boot mode: if the default boot entry has any" " errors, instead of waiting for the user to do anything, it" " immediately starts over using the NUM entry (same numbering as the" " `default' command). This obviously won't help if the machine" " was rebooted by a kernel that GRUB loaded."}, - {"find", "search -sf '%s'\n", 1, {TYPE_FILE}, 0, "FILENAME", + {"find", "search -sf '%s'\n", NULL, 0, 1, {TYPE_FILE}, 0, "FILENAME", "Search for the filename FILENAME in all of partitions and print the list of" " the devices which contain the file."}, /* FIXME: fstest unsupported. */ /* FIXME: geometry unsupported. */ - {"halt", "halt %s\n", 1, {TYPE_NOAPM_OPTION}, 0, "[--no-apm]", + {"halt", "halt %s\n", NULL, 0, 1, {TYPE_NOAPM_OPTION}, 0, "[--no-apm]", "Halt your system. If APM is available on it, turn off the power using" " the APM BIOS, unless you specify the option `--no-apm'."}, /* FIXME: help unsupported. */ /* NUL_TERMINATE */ - {"hiddenmenu", "if sleep -i $timeout; then timeout=0; else timeout=-1; fi\n", - 0, {}, FLAG_SUFFIX, "", "Hide the menu."}, - {"hide", "parttool '%s' hidden+\n", 1, {TYPE_PARTITION}, 0, "PARTITION", + {"hiddenmenu", NULL, + "if sleep -i $timeout; then timeout=0; else timeout=-1; fi\n", 0, + 0, {}, 0, "", "Hide the menu."}, + {"hide", "parttool '%s' hidden+\n", NULL, 0, 1, {TYPE_PARTITION}, + 0, "PARTITION", "Hide PARTITION by setting the \"hidden\" bit in" " its partition type code."}, /* FIXME: ifconfig unsupported. */ /* FIXME: impsprobe unsupported. */ - {"initrd", "legacy_initrd '%s' %s\n", 2, {TYPE_FILE_NO_CONSUME, - TYPE_REST_VERBATIM}, 0, + {"initrd", "legacy_initrd '%s' %s\n", NULL, 0, 2, {TYPE_FILE_NO_CONSUME, + TYPE_REST_VERBATIM}, 0, "FILE [ARG ...]", "Load an initial ramdisk FILE for a Linux format boot image and set the" " appropriate parameters in the Linux setup area in memory."}, /* NOTE: install unsupported. */ /* FIXME: ioprobe unsupported. */ /* FIXME: really support --no-mem-option. */ - {"kernel", "legacy_kernel %s %s '%s' %s\n", 4, {TYPE_TYPE_OR_NOMEM_OPTION, - TYPE_TYPE_OR_NOMEM_OPTION, - TYPE_FILE_NO_CONSUME, - TYPE_REST_VERBATIM}, 0, + {"kernel", "legacy_kernel %s %s '%s' %s\n", NULL, 0, + 4, {TYPE_TYPE_OR_NOMEM_OPTION, TYPE_TYPE_OR_NOMEM_OPTION, + TYPE_FILE_NO_CONSUME, TYPE_REST_VERBATIM}, 0, "[--no-mem-option] [--type=TYPE] FILE [ARG ...]", "Attempt to load the primary boot image from FILE. The rest of the" " line is passed verbatim as the \"kernel command line\". Any modules" @@ -144,46 +147,49 @@ struct legacy_command legacy_commands[] = " \"multiboot\". The option --no-mem-option tells GRUB not to pass a" " Linux's mem option automatically."}, /* FIXME: lock is unsupported. */ - {"makeactive", "parttool \"$root\" boot+\n", 0, {}, 0, 0, + {"makeactive", "parttool \"$root\" boot+\n", NULL, 0, 0, {}, 0, 0, "Set the active partition on the root disk to GRUB's root device." " This command is limited to _primary_ PC partitions on a hard disk."}, - {"map", "drivemap '%s' '%s'\n", 2, {TYPE_PARTITION, TYPE_PARTITION}, + {"map", "drivemap '%s' '%s'\n", NULL, 0, + 2, {TYPE_PARTITION, TYPE_PARTITION}, FLAG_IGNORE_REST, "TO_DRIVE FROM_DRIVE", "Map the drive FROM_DRIVE to the drive TO_DRIVE. This is necessary" " when you chain-load some operating systems, such as DOS, if such an" " OS resides at a non-first drive."}, /* NOTE: md5crypt unsupported since GRUB has not enough entropy and this hash shouldn't be used anymore. */ - {"module", "legacy_initrd '%s' %s\n", 1, {TYPE_FILE_NO_CONSUME, - TYPE_REST_VERBATIM}, 0, + {"module", "legacy_initrd '%s' %s\n", NULL, 0, 2, {TYPE_FILE_NO_CONSUME, + TYPE_REST_VERBATIM}, 0, "FILE [ARG ...]", "Load a boot module FILE for a Multiboot format boot image (no" " interpretation of the file contents is made, so users of this" " command must know what the kernel in question expects). The" " rest of the line is passed as the \"module command line\", like" " the `kernel' command."}, - {"modulenounzip", "legacy_initrd_nounzip '%s' %s\n", 1, + {"modulenounzip", "legacy_initrd_nounzip '%s' %s\n", NULL, 0, 2, {TYPE_FILE_NO_CONSUME, TYPE_REST_VERBATIM}, 0, "FILE [ARG ...]", "The same as `module', except that automatic decompression is" " disabled."}, {"pager", "set pager=%s; if [ \"$pager\" = 0 ]; then " - " echo Internal pager is now off; else echo Internal pager is now on; fi\n", + " echo Internal pager is now off; else " + "echo Internal pager is now on; fi\n", NULL, 0, 1, {TYPE_BOOL}, FLAG_FALLBACK_AVAILABLE, "[FLAG]", "Toggle pager mode with no argument. If FLAG is given and its value" " is `on', turn on the mode. If FLAG is `off', turn off the mode."}, {"pager", "if [ \"$pager\" = 1 ]; then pager=0; echo Internal pager is now off;" - "else pager=1; echo Internal pager is now on; fi\n", 0, {}, + "else pager=1; echo Internal pager is now on; fi\n", NULL, 0, 0, {}, FLAG_FALLBACK, NULL, NULL}, /* FIXME: partnew unsupported. */ - {"parttype", "parttool '%s' type=%s\n", 2, {TYPE_PARTITION, TYPE_INT}, 0, + {"parttype", "parttool '%s' type=%s\n", NULL, 0, + 2, {TYPE_PARTITION, TYPE_INT}, 0, "PART TYPE", "Change the type of the partition PART to TYPE."}, /* FIXME: support config file reloading. */ /* FIXME: support usage in menuentry. */ {"password", "if [ \"$superusers\" = "" ]; then superusers=legacy; fi;\n" - "legacy_password %s '%s' %s", 3, {TYPE_OPTION, TYPE_VERBATIM, - TYPE_FILE}, FLAG_IGNORE_REST, + "legacy_password %s '%s' %s", NULL, 0, 3, {TYPE_OPTION, TYPE_VERBATIM, + TYPE_FILE}, FLAG_IGNORE_REST, "[--md5] PASSWD [FILE]", "If used in the first section of a menu file, disable all" " interactive editing control (menu entry editor and" @@ -197,15 +203,15 @@ struct legacy_command legacy_commands[] = /* NOTE: GRUB2 has a design principle of not eternally waiting for user input. 60 seconds should be enough. */ - {"pause", "echo %s; if ! sleep -i 60; then return; fi\n", 1, + {"pause", "echo %s; if ! sleep -i 60; then return; fi\n", NULL, 0, 1, {TYPE_REST_VERBATIM}, 0, "[MESSAGE ...]", "Print MESSAGE, then wait until a key is pressed."}, /* FIXME: rarp unsupported. */ - {"read", "read_dword %s\n", 1, {TYPE_INT}, 0, "ADDR", + {"read", "read_dword %s\n", NULL, 0, 1, {TYPE_INT}, 0, "ADDR", "Read a 32-bit value from memory at address ADDR and" " display it in hex format."}, - {"reboot", "reboot\n", 0, {}, 0, 0, "Reboot your system."}, - {"root", "set root='%s'; set legacy_hdbias='%s'\n", + {"reboot", "reboot\n", NULL, 0, 0, {}, 0, 0, "Reboot your system."}, + {"root", "set root='%s'; set legacy_hdbias='%s'\n", NULL, 0, 2, {TYPE_PARTITION, TYPE_INT}, FLAG_FALLBACK_AVAILABLE, "[DEVICE [HDBIAS]]", "Set the current \"root device\" to the device DEVICE, then" @@ -218,8 +224,8 @@ struct legacy_command legacy_commands[] = " how many BIOS drive numbers are on controllers before the current" " one. For example, if there is an IDE disk and a SCSI disk, and your" " FreeBSD root partition is on the SCSI disk, then use a `1' for HDBIAS."}, - {"root", "echo \"$root\"\n", 0, {}, FLAG_FALLBACK, NULL, NULL}, - {"rootnoverify", "set root='%s'; set legacy_hdbias='%s'\n", + {"root", "echo \"$root\"\n", NULL, 0, 0, {}, FLAG_FALLBACK, NULL, NULL}, + {"rootnoverify", "set root='%s'; set legacy_hdbias='%s'\n", NULL, 0, 2, {TYPE_PARTITION, TYPE_INT}, 0, "[DEVICE [HDBIAS]]", "Similar to `root', but don't attempt to mount the partition. This" @@ -227,14 +233,15 @@ struct legacy_command legacy_commands[] = " GRUB can read, but setting the correct root device is still" " desired. Note that the items mentioned in `root' which" " derived from attempting the mount will NOT work correctly."}, - {"rootnoverify", "echo \"$root\"\n", 0, {}, FLAG_FALLBACK, NULL, NULL}, + {"rootnoverify", "echo \"$root\"\n", NULL, 0, + 0, {}, FLAG_FALLBACK, NULL, NULL}, /* FIXME: support saving NUM and fallback. */ - {"savedefault", "saved_entry=${chosen}; save_env saved_entry\n", 0, {}, 0, - "[NUM | `fallback']", + {"savedefault", "saved_entry=${chosen}; save_env saved_entry\n", NULL, 0, + 0, {}, 0, "[NUM | `fallback']", "Save the current entry as the default boot entry if no argument is" " specified. If a number is specified, this number is saved. If" " `fallback' is used, next fallback entry is saved."}, - {"serial", "serial %s\n", 1, {TYPE_REST_VERBATIM}, 0, + {"serial", "serial %s\n", NULL, 0, 1, {TYPE_REST_VERBATIM}, 0, "[--unit=UNIT] [--port=PORT] [--speed=SPEED] [--word=WORD] " "[--parity=PARITY] [--stop=STOP] [--device=DEV]", "Initialize a serial device. UNIT is a digit that specifies which serial" @@ -248,25 +255,26 @@ struct legacy_command legacy_commands[] = /* FIXME: setup unsupported. */ /* FIXME: terminal unsupported. */ /* NUL_TERMINATE */ /* FIXME: terminfo unsupported. */ /* NUL_TERMINATE */ - {"testload", "cat '%s'\n", 1, {TYPE_FILE}, 0, "FILE", + {"testload", "cat '%s'\n", NULL, 0, 1, {TYPE_FILE}, 0, "FILE", "Read the entire contents of FILE in several different ways and" " compares them, to test the filesystem code. " " If this test succeeds, then a good next" " step is to try loading a kernel."}, /* FIXME: testvbe unsupported. */ /* FIXME: tftpserver unsupported. */ - {"timeout", "set timeout=%s\n", 1, {TYPE_INT}, 0, "SEC", + {"timeout", "set timeout=%s\n", NULL, 0, 1, {TYPE_INT}, 0, "SEC", "Set a timeout, in SEC seconds, before automatically booting the" " default entry (normally the first entry defined)."}, /* title is handled separately. */ - {"unhide", "parttool '%s' hidden-\n", 1, {TYPE_PARTITION}, 0, "PARTITION", + {"unhide", "parttool '%s' hidden-\n", NULL, 0, + 1, {TYPE_PARTITION}, 0, "PARTITION", "Unhide PARTITION by clearing the \"hidden\" bit in its" " partition type code."}, /* FIXME: uppermem unsupported. */ - {"uuid", "search -u '%s'\n", 1, {TYPE_VERBATIM}, 0, "UUID", + {"uuid", "search -u '%s'\n", NULL, 0, 1, {TYPE_VERBATIM}, 0, "UUID", "Find root by UUID"}, /* FIXME: support MODE. */ - {"vbeprobe", "vbeinfo\n", 0, {}, 0, "[MODE]", + {"vbeprobe", "vbeinfo\n", NULL, 0, 0, {}, 0, "[MODE]", "Probe VBE information. If the mode number MODE is specified, show only" " the information about only the mode."} }; @@ -384,13 +392,13 @@ is_option (enum arg_type opt, const char *curarg, grub_size_t len) } char * -grub_legacy_parse (const char *buf, char **entryname, int *suffix) +grub_legacy_parse (const char *buf, char **entryname, char **suffix) { const char *ptr; const char *cmdname; unsigned i, cmdnum; - *suffix = 0; + *suffix = NULL; for (ptr = buf; *ptr && grub_isspace (*ptr); ptr++); if (!*ptr || *ptr == '#') @@ -437,8 +445,6 @@ grub_legacy_parse (const char *buf, char **entryname, int *suffix) if (cmdnum == ARRAY_SIZE (legacy_commands)) return grub_xasprintf ("# Unsupported legacy command: %s\n", buf); - *suffix = !!(legacy_commands[cmdnum].flags & FLAG_SUFFIX); - for (; grub_isspace (*ptr) || *ptr == '='; ptr++); char *args[ARRAY_SIZE (legacy_commands[0].argt)]; @@ -447,7 +453,7 @@ grub_legacy_parse (const char *buf, char **entryname, int *suffix) { int hold_arg = 0; const char *curarg = NULL; - for (i = 0; i < legacy_commands[cmdnum].argc + hold_arg; i++) + for (i = 0; i < legacy_commands[cmdnum].argc; i++) { grub_size_t curarglen; if (hold_arg) @@ -622,6 +628,14 @@ grub_legacy_parse (const char *buf, char **entryname, int *suffix) args[legacy_commands[cmdnum].argc] = invert; } + if (legacy_commands[cmdnum].suffix) + { + *suffix = grub_xasprintf (legacy_commands[cmdnum].suffix, + args[legacy_commands[cmdnum].suffixarg]); + if (*suffix) + return NULL; + } + { char *ret = grub_xasprintf (legacy_commands[cmdnum].map, args[0], args[1], args[2], args[3]); diff --git a/include/grub/legacy_parse.h b/include/grub/legacy_parse.h index a6394496f..a5e67a071 100644 --- a/include/grub/legacy_parse.h +++ b/include/grub/legacy_parse.h @@ -21,7 +21,7 @@ #include -char *grub_legacy_parse (const char *buf, char **entryname, int *suffix); +char *grub_legacy_parse (const char *buf, char **entryname, char **suffix); char *grub_legacy_escape (const char *in, grub_size_t len); #endif diff --git a/util/grub-menulst2cfg.c b/util/grub-menulst2cfg.c index 38a906c0a..512239e89 100644 --- a/util/grub-menulst2cfg.c +++ b/util/grub-menulst2cfg.c @@ -20,6 +20,7 @@ #include #include #include +#include int main (int argc, char **argv) @@ -30,7 +31,6 @@ main (int argc, char **argv) size_t bufsize = 0; char *suffix = xstrdup (""); int suffixlen = 0; - int is_suffix = 0; if (argc >= 2 && argv[1][0] == '-') { @@ -75,15 +75,15 @@ main (int argc, char **argv) { char *oldname = NULL; + char *newsuffix; oldname = entryname; - parsed = grub_legacy_parse (buf, &entryname, &is_suffix); - if (is_suffix) + parsed = grub_legacy_parse (buf, &entryname, &newsuffix); + if (newsuffix) { - suffixlen += strlen (parsed); + suffixlen += strlen (newsuffix); suffix = xrealloc (suffix, suffixlen + 1); - strcat (suffix, parsed); - continue; + strcat (suffix, newsuffix); } if (oldname != entryname && oldname) fprintf (out, "}\n\n"); @@ -91,25 +91,25 @@ main (int argc, char **argv) { char *escaped = grub_legacy_escape (entryname, strlen (entryname)); fprintf (out, "menuentry \'%s\' {\n", escaped); - grub_free (escaped); - grub_free (oldname); + free (escaped); + free (oldname); } } if (parsed) fprintf (out, "%s%s", entryname ? " " : "", parsed); - grub_free (parsed); + free (parsed); parsed = NULL; } if (entryname) fprintf (out, "}\n\n"); - fwrite (out, 1, suffixlen, suffix); + fwrite (suffix, 1, suffixlen, out); - grub_free (buf); - grub_free (suffix); - grub_free (entryname); + free (buf); + free (suffix); + free (entryname); if (in != stdin) fclose (in); From 6492c85a42ce35b170e4477243834cf969048916 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Sun, 12 Sep 2010 15:54:39 +0200 Subject: [PATCH 29/37] Support config file reloading (not tested) --- grub-core/lib/legacy_parse.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/grub-core/lib/legacy_parse.c b/grub-core/lib/legacy_parse.c index 692b1b7d4..3f28544b3 100644 --- a/grub-core/lib/legacy_parse.c +++ b/grub-core/lib/legacy_parse.c @@ -185,12 +185,12 @@ struct legacy_command legacy_commands[] = {"parttype", "parttool '%s' type=%s\n", NULL, 0, 2, {TYPE_PARTITION, TYPE_INT}, 0, "PART TYPE", "Change the type of the partition PART to TYPE."}, - /* FIXME: support config file reloading. */ /* FIXME: support usage in menuentry. */ {"password", "if [ \"$superusers\" = "" ]; then superusers=legacy; fi;\n" - "legacy_password %s '%s' %s", NULL, 0, 3, {TYPE_OPTION, TYPE_VERBATIM, - TYPE_FILE}, FLAG_IGNORE_REST, - "[--md5] PASSWD [FILE]", + "legacy_password %s '%s'", + "menuentry \"Superuser menu\" --users \"legacy\" { configfile '%s'; }\n", + 2, 3, {TYPE_OPTION, TYPE_VERBATIM, TYPE_FILE}, + FLAG_IGNORE_REST | FLAG_FALLBACK_AVAILABLE, "[--md5] PASSWD [FILE]", "If used in the first section of a menu file, disable all" " interactive editing control (menu entry editor and" " command line). If the password PASSWD is entered, it loads the" @@ -200,6 +200,9 @@ struct legacy_command legacy_commands[] = " which case it will ask for the password, before continuing." " The option --md5 tells GRUB that PASSWD is encrypted with" " md5crypt."}, + {"password", "if [ \"$superusers\" = "" ]; then superusers=legacy; fi;\n" + "legacy_password %s '%s'", NULL, 0, 2, {TYPE_OPTION, TYPE_VERBATIM}, + FLAG_IGNORE_REST | FLAG_FALLBACK, NULL, NULL}, /* NOTE: GRUB2 has a design principle of not eternally waiting for user input. 60 seconds should be enough. */ From df8957929d631ffd298de7c2ab717c245934fe23 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Sun, 12 Sep 2010 16:11:41 +0200 Subject: [PATCH 30/37] lock support (not tested) --- grub-core/lib/legacy_parse.c | 9 ++------- grub-core/normal/auth.c | 24 ++++++++++++++++++++++++ grub-core/normal/main.c | 2 ++ include/grub/normal.h | 3 +++ 4 files changed, 31 insertions(+), 7 deletions(-) diff --git a/grub-core/lib/legacy_parse.c b/grub-core/lib/legacy_parse.c index 3f28544b3..868eab4ab 100644 --- a/grub-core/lib/legacy_parse.c +++ b/grub-core/lib/legacy_parse.c @@ -146,7 +146,8 @@ struct legacy_command legacy_commands[] = " \"netbsd\", \"freebsd\", \"openbsd\", \"linux\", \"biglinux\" and" " \"multiboot\". The option --no-mem-option tells GRUB not to pass a" " Linux's mem option automatically."}, - /* FIXME: lock is unsupported. */ + {"lock", "if ! authenticate legacy; then return; fi", NULL, 0, 0, {}, 0, + 0, "Break a command execution unless the user is authenticated."}, {"makeactive", "parttool \"$root\" boot+\n", NULL, 0, 0, {}, 0, 0, "Set the active partition on the root disk to GRUB's root device." " This command is limited to _primary_ PC partitions on a hard disk."}, @@ -435,12 +436,6 @@ grub_legacy_parse (const char *buf, char **entryname, char **suffix) return NULL; } - if (grub_strncmp ("lock", cmdname, ptr - cmdname) == 0 - && ptr - cmdname == sizeof ("lock") - 1) - { - /* FIXME */ - } - for (cmdnum = 0; cmdnum < ARRAY_SIZE (legacy_commands); cmdnum++) if (grub_strncmp (legacy_commands[cmdnum].name, cmdname, ptr - cmdname) == 0 && legacy_commands[cmdnum].name[ptr - cmdname] == 0) diff --git a/grub-core/normal/auth.c b/grub-core/normal/auth.c index bf1efbfdd..e5216da8c 100644 --- a/grub-core/normal/auth.c +++ b/grub-core/normal/auth.c @@ -248,3 +248,27 @@ grub_auth_check_authentication (const char *userlist) return GRUB_ACCESS_DENIED; } + +static grub_err_t +grub_cmd_authenticate (struct grub_command *cmd __attribute__ ((unused)), + int argc, char **args) +{ + return grub_auth_check_authentication ((argc >= 1) ? args[0] : ""); +} + +static grub_command_t cmd; + +void +grub_normal_auth_init (void) +{ + cmd = grub_register_command ("authenticate", + grub_cmd_authenticate, + N_("[USERLIST]"), N_("Authenticate users")); + +} + +void +grub_normal_auth_fini (void) +{ + grub_unregister_command (cmd); +} diff --git a/grub-core/normal/main.c b/grub-core/normal/main.c index c7e83fba0..f2e5eaf51 100644 --- a/grub-core/normal/main.c +++ b/grub-core/normal/main.c @@ -477,6 +477,7 @@ GRUB_MOD_INIT(normal) /* Previously many modules depended on gzio. Be nice to user and load it. */ grub_dl_load ("gzio"); + grub_normal_auth_init (); grub_context_init (); grub_script_init (); grub_menu_init (); @@ -520,6 +521,7 @@ GRUB_MOD_FINI(normal) grub_context_fini (); grub_script_fini (); grub_menu_fini (); + grub_normal_auth_fini (); grub_xputs = grub_xputs_saved; diff --git a/include/grub/normal.h b/include/grub/normal.h index 417560d9f..72912e524 100644 --- a/include/grub/normal.h +++ b/include/grub/normal.h @@ -123,4 +123,7 @@ grub_normal_add_menu_entry (int argc, const char **args, char **classes, grub_err_t grub_normal_set_password (const char *user, const char *password); +void grub_normal_auth_init (void); +void grub_normal_auth_fini (void); + #endif /* ! GRUB_NORMAL_HEADER */ From 898330b0973d88db9aebd15b08705485a1f433d1 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Sun, 12 Sep 2010 16:15:40 +0200 Subject: [PATCH 31/37] MArk setup as not to be implemented --- grub-core/lib/legacy_parse.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/grub-core/lib/legacy_parse.c b/grub-core/lib/legacy_parse.c index 868eab4ab..5b5b4d6e2 100644 --- a/grub-core/lib/legacy_parse.c +++ b/grub-core/lib/legacy_parse.c @@ -256,7 +256,7 @@ struct legacy_command legacy_commands[] = " in the grub shell, which specifies the file name of a tty device. The" " default values are COM1, 9600, 8N1."}, /* FIXME: setkey unsupported. */ /* NUL_TERMINATE */ - /* FIXME: setup unsupported. */ + /* NOTE: setup unsupported. */ /* FIXME: terminal unsupported. */ /* NUL_TERMINATE */ /* FIXME: terminfo unsupported. */ /* NUL_TERMINATE */ {"testload", "cat '%s'\n", NULL, 0, 1, {TYPE_FILE}, 0, "FILE", From 44224d3948d9952cb2928571f8aa914ebf6f040a Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Wed, 15 Sep 2010 02:16:12 +0200 Subject: [PATCH 32/37] Fix UUID command. Reported by: Jordan Uggla --- grub-core/lib/legacy_parse.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/grub-core/lib/legacy_parse.c b/grub-core/lib/legacy_parse.c index 5b5b4d6e2..7ecd1c74f 100644 --- a/grub-core/lib/legacy_parse.c +++ b/grub-core/lib/legacy_parse.c @@ -275,8 +275,8 @@ struct legacy_command legacy_commands[] = "Unhide PARTITION by clearing the \"hidden\" bit in its" " partition type code."}, /* FIXME: uppermem unsupported. */ - {"uuid", "search -u '%s'\n", NULL, 0, 1, {TYPE_VERBATIM}, 0, "UUID", - "Find root by UUID"}, + {"uuid", "search --set=root --fs-uuid '%s'\n", NULL, 0, 1, {TYPE_VERBATIM}, + 0, "UUID", "Find root by UUID"}, /* FIXME: support MODE. */ {"vbeprobe", "vbeinfo\n", NULL, 0, 0, {}, 0, "[MODE]", "Probe VBE information. If the mode number MODE is specified, show only" From e31bb619117766a74a7d5c7f6b87bdcaea7c55c5 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Wed, 15 Sep 2010 11:39:53 +0200 Subject: [PATCH 33/37] Transform legacy mode numbers into resolution specification --- Makefile.util.def | 2 + grub-core/Makefile.core.def | 1 + grub-core/lib/i386/pc/vesa_modes_table.c | 127 +++++++++++++++ grub-core/lib/legacy_parse.c | 42 ++++- grub-core/loader/i386/linux.c | 190 ++--------------------- 5 files changed, 179 insertions(+), 183 deletions(-) create mode 100644 grub-core/lib/i386/pc/vesa_modes_table.c diff --git a/Makefile.util.def b/Makefile.util.def index febbfd0ed..54ec6ee8a 100644 --- a/Makefile.util.def +++ b/Makefile.util.def @@ -563,6 +563,8 @@ program = { mansection = 1; common = util/grub-menulst2cfg.c; common = grub-core/lib/legacy_parse.c; + common = grub-core/lib/i386/pc/vesa_modes_table.c; + ldadd = libgrub.a; ldflags = '$(LIBDEVMAPPER)'; }; diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def index 36a6e6564..03505ad5a 100644 --- a/grub-core/Makefile.core.def +++ b/grub-core/Makefile.core.def @@ -1111,6 +1111,7 @@ module = { module = { name = linux; x86 = loader/i386/linux.c; + i386_pc = lib/i386/pc/vesa_modes_table.c; mips = loader/mips/linux.c; powerpc_ieee1275 = loader/powerpc/ieee1275/linux.c; sparc64_ieee1275 = loader/sparc64/ieee1275/linux.c; diff --git a/grub-core/lib/i386/pc/vesa_modes_table.c b/grub-core/lib/i386/pc/vesa_modes_table.c new file mode 100644 index 000000000..6dc4b7d8d --- /dev/null +++ b/grub-core/lib/i386/pc/vesa_modes_table.c @@ -0,0 +1,127 @@ + +#include + +/* This is the reverse of the table in [linux]/Documentation/fb/vesafb.txt + plus a few more modes based on the table in + http://en.wikipedia.org/wiki/VESA_BIOS_Extensions */ +struct grub_vesa_mode_table_entry +grub_vesa_mode_table[GRUB_VESA_MODE_TABLE_END + - GRUB_VESA_MODE_TABLE_START + 1] = + { + { 640, 400, 8 }, /* 0x300 */ + { 640, 480, 8 }, /* 0x301 */ + { 800, 600, 4 }, /* 0x302 */ + { 800, 600, 8 }, /* 0x303 */ + { 1024, 768, 4 }, /* 0x304 */ + { 1024, 768, 8 }, /* 0x305 */ + { 1280, 1024, 4 }, /* 0x306 */ + { 1280, 1024, 8 }, /* 0x307 */ + { 0, 0, 0 }, + { 0, 0, 0 }, + { 0, 0, 0 }, + { 0, 0, 0 }, + { 0, 0, 0 }, + { 320, 200, 15 }, /* 0x30d */ + { 320, 200, 16 }, /* 0x30e */ + { 320, 200, 24 }, /* 0x30f */ + { 640, 480, 15 }, /* 0x310 */ + { 640, 480, 16 }, /* 0x311 */ + { 640, 480, 24 }, /* 0x312 */ + { 800, 600, 15 }, /* 0x313 */ + { 800, 600, 16 }, /* 0x314 */ + { 800, 600, 24 }, /* 0x315 */ + { 1024, 768, 15 }, /* 0x316 */ + { 1024, 768, 16 }, /* 0x317 */ + { 1024, 768, 24 }, /* 0x318 */ + { 1280, 1024, 15 }, /* 0x319 */ + { 1280, 1024, 16 }, /* 0x31a */ + { 1280, 1024, 24 }, /* 0x31b */ + { 1600, 1200, 8 }, /* 0x31c */ + { 1600, 1200, 15 }, /* 0x31d */ + { 1600, 1200, 16 }, /* 0x31e */ + { 1600, 1200, 24 }, /* 0x31f */ + { 0, 0, 0 }, + { 640, 400, 15 }, /* 0x321 */ + { 640, 400, 16 }, /* 0x322 */ + { 640, 400, 24 }, /* 0x323 */ + { 640, 400, 32 }, /* 0x324 */ + { 0, 0, 0 }, + { 0, 0, 0 }, + { 0, 0, 0 }, + { 0, 0, 0 }, + { 640, 480, 32 }, /* 0x329 */ + { 0, 0, 0 }, + { 0, 0, 0 }, + { 0, 0, 0 }, + { 0, 0, 0 }, + { 0, 0, 0 }, + { 896, 672, 8 }, /* 0x32f */ + { 896, 672, 15 }, /* 0x330 */ + { 896, 672, 16 }, /* 0x331 */ + { 896, 672, 24 }, /* 0x332 */ + { 896, 672, 32 }, /* 0x333 */ + { 0, 0, 0 }, + { 0, 0, 0 }, + { 0, 0, 0 }, + { 0, 0, 0 }, + { 0, 0, 0 }, + { 0, 0, 0 }, + { 0, 0, 0 }, + { 0, 0, 0 }, + { 0, 0, 0 }, + { 0, 0, 0 }, + { 0, 0, 0 }, + { 0, 0, 0 }, + { 0, 0, 0 }, + { 0, 0, 0 }, + { 1600, 1200, 32 }, /* 0x342 */ + { 0, 0, 0 }, + { 0, 0, 0 }, + { 0, 0, 0 }, + { 0, 0, 0 }, + { 0, 0, 0 }, + { 0, 0, 0 }, + { 0, 0, 0 }, + { 0, 0, 0 }, + { 0, 0, 0 }, + { 0, 0, 0 }, + { 0, 0, 0 }, + { 0, 0, 0 }, + { 0, 0, 0 }, + { 0, 0, 0 }, + { 0, 0, 0 }, + { 0, 0, 0 }, + { 0, 0, 0 }, + { 0, 0, 0 }, + { 0, 0, 0 }, + { 0, 0, 0 }, + { 0, 0, 0 }, + { 0, 0, 0 }, + { 0, 0, 0 }, + { 0, 0, 0 }, + { 0, 0, 0 }, + { 0, 0, 0 }, + { 0, 0, 0 }, + { 0, 0, 0 }, + { 0, 0, 0 }, + { 1440, 900, 8 }, /* 0x360 */ + { 1440, 900, 15 }, /* 0x361 */ + { 1440, 900, 16 }, /* 0x362 */ + { 1440, 900, 24 }, /* 0x363 */ + { 1440, 900, 32 }, /* 0x364 */ + { 1152, 720, 8 }, /* 0x365 */ + { 1152, 720, 15 }, /* 0x366 */ + { 1152, 720, 16 }, /* 0x367 */ + { 1152, 720, 24 }, /* 0x368 */ + { 1152, 720, 32 }, /* 0x369 */ + { 1024, 640, 8 }, /* 0x36a */ + { 1024, 640, 15 }, /* 0x36b */ + { 1024, 640, 16 }, /* 0x36c */ + { 1024, 640, 24 }, /* 0x36d */ + { 1024, 640, 32 }, /* 0x36e */ + { 800, 500, 8 }, /* 0x36f */ + { 800, 500, 15 }, /* 0x370 */ + { 800, 500, 16 }, /* 0x371 */ + { 800, 500, 24 }, /* 0x372 */ + { 800, 500, 32 }, /* 0x373 */ + }; diff --git a/grub-core/lib/legacy_parse.c b/grub-core/lib/legacy_parse.c index 7ecd1c74f..959d8367d 100644 --- a/grub-core/lib/legacy_parse.c +++ b/grub-core/lib/legacy_parse.c @@ -21,6 +21,7 @@ #include #include #include +#include struct legacy_command { @@ -40,7 +41,8 @@ struct legacy_command TYPE_PARTITION, TYPE_BOOL, TYPE_INT, - TYPE_REST_VERBATIM + TYPE_REST_VERBATIM, + TYPE_VBE_MODE } argt[4]; enum { FLAG_IGNORE_REST = 1, @@ -264,7 +266,8 @@ struct legacy_command legacy_commands[] = " compares them, to test the filesystem code. " " If this test succeeds, then a good next" " step is to try loading a kernel."}, - /* FIXME: testvbe unsupported. */ + {"testvbe", "insmod vbe; videotest '%s'\n", NULL, 0, 1, {TYPE_VBE_MODE}, 0, + "MODE", "Test the VBE mode MODE. Hit any key to return."}, /* FIXME: tftpserver unsupported. */ {"timeout", "set timeout=%s\n", NULL, 0, 1, {TYPE_INT}, 0, "SEC", "Set a timeout, in SEC seconds, before automatically booting the" @@ -278,7 +281,7 @@ struct legacy_command legacy_commands[] = {"uuid", "search --set=root --fs-uuid '%s'\n", NULL, 0, 1, {TYPE_VERBATIM}, 0, "UUID", "Find root by UUID"}, /* FIXME: support MODE. */ - {"vbeprobe", "vbeinfo\n", NULL, 0, 0, {}, 0, "[MODE]", + {"vbeprobe", "insmod vbe; videoinfo\n", NULL, 0, 0, {}, 0, "[MODE]", "Probe VBE information. If the mode number MODE is specified, show only" " the information about only the mode."} }; @@ -566,6 +569,34 @@ grub_legacy_parse (const char *buf, char **entryname, char **suffix) args[i] = grub_strndup (curarg, brk - curarg); } break; + case TYPE_VBE_MODE: + { + unsigned mod; + struct grub_vesa_mode_table_entry *modedesc; + + mod = grub_strtoul (curarg, 0, 0); + if (grub_errno) + { + mod = 0; + grub_errno = GRUB_ERR_NONE; + } + if (mod < GRUB_VESA_MODE_TABLE_START + || mod > GRUB_VESA_MODE_TABLE_END) + { + args[i] = grub_strdup ("auto"); + break; + } + modedesc = &grub_vesa_mode_table[mod - GRUB_VESA_MODE_TABLE_START]; + if (!modedesc->width) + { + args[i] = grub_strdup ("auto"); + break; + } + args[i] = grub_xasprintf ("%ux%ux%u", + modedesc->width, modedesc->height, + modedesc->depth); + break; + } case TYPE_BOOL: if (curarglen == 2 && curarg[0] == 'o' && curarg[1] == 'n') args[i] = grub_strdup ("1"); @@ -599,7 +630,10 @@ grub_legacy_parse (const char *buf, char **entryname, char **suffix) case TYPE_BOOL: case TYPE_INT: args[i] = grub_strdup ("0"); - break; + break; + case TYPE_VBE_MODE: + args[i] = grub_strdup ("auto"); + break; } if (legacy_commands[cmdnum].flags & FLAG_COLOR_INVERT) diff --git a/grub-core/loader/i386/linux.c b/grub-core/loader/i386/linux.c index cc2d20af3..9d5b7b727 100644 --- a/grub-core/loader/i386/linux.c +++ b/grub-core/loader/i386/linux.c @@ -35,6 +35,10 @@ #include #include +#ifdef GRUB_MACHINE_PCBIOS +#include +#endif + #ifdef GRUB_MACHINE_EFI #include #define HAS_VGA_TEXT 0 @@ -89,175 +93,6 @@ static struct idt_descriptor idt_desc = }; #endif -#ifdef GRUB_MACHINE_PCBIOS -struct linux_vesafb_res -{ - grub_uint16_t width; - grub_uint16_t height; -}; - -struct linux_vesafb_mode -{ - grub_uint8_t res_index; - grub_uint8_t depth; -}; - -enum vga_modes - { - VGA_320_200, - VGA_640_400, - VGA_640_480, - VGA_800_500, - VGA_800_600, - VGA_896_672, - VGA_1024_640, - VGA_1024_768, - VGA_1152_720, - VGA_1280_1024, - VGA_1440_900, - VGA_1600_1200, - }; - -static struct linux_vesafb_res linux_vesafb_res[] = - { - { 320, 200 }, - { 640, 400 }, - { 640, 480 }, - { 800, 500 }, - { 800, 600 }, - { 896, 672 }, - { 1024, 640 }, - { 1024, 768 }, - { 1152, 720 }, - { 1280, 1024 }, - { 1440, 900 }, - { 1600, 1200 }, - }; - -/* This is the reverse of the table in [linux]/Documentation/fb/vesafb.txt - plus a few more modes based on the table in - http://en.wikipedia.org/wiki/VESA_BIOS_Extensions */ -struct linux_vesafb_mode linux_vesafb_modes[] = - { - { VGA_640_400, 8 }, /* 0x300 */ - { VGA_640_480, 8 }, /* 0x301 */ - { VGA_800_600, 4 }, /* 0x302 */ - { VGA_800_600, 8 }, /* 0x303 */ - { VGA_1024_768, 4 }, /* 0x304 */ - { VGA_1024_768, 8 }, /* 0x305 */ - { VGA_1280_1024, 4 }, /* 0x306 */ - { VGA_1280_1024, 8 }, /* 0x307 */ - { 0, 0 }, - { 0, 0 }, - { 0, 0 }, - { 0, 0 }, - { 0, 0 }, - { VGA_320_200, 15 }, /* 0x30d */ - { VGA_320_200, 16 }, /* 0x30e */ - { VGA_320_200, 24 }, /* 0x30f */ - { VGA_640_480, 15 }, /* 0x310 */ - { VGA_640_480, 16 }, /* 0x311 */ - { VGA_640_480, 24 }, /* 0x312 */ - { VGA_800_600, 15 }, /* 0x313 */ - { VGA_800_600, 16 }, /* 0x314 */ - { VGA_800_600, 24 }, /* 0x315 */ - { VGA_1024_768, 15 }, /* 0x316 */ - { VGA_1024_768, 16 }, /* 0x317 */ - { VGA_1024_768, 24 }, /* 0x318 */ - { VGA_1280_1024, 15 }, /* 0x319 */ - { VGA_1280_1024, 16 }, /* 0x31a */ - { VGA_1280_1024, 24 }, /* 0x31b */ - { VGA_1600_1200, 8 }, /* 0x31c */ - { VGA_1600_1200, 15 }, /* 0x31d */ - { VGA_1600_1200, 16 }, /* 0x31e */ - { VGA_1600_1200, 24 }, /* 0x31f */ - { 0, 0 }, - { VGA_640_400, 15 }, /* 0x321 */ - { VGA_640_400, 16 }, /* 0x322 */ - { VGA_640_400, 24 }, /* 0x323 */ - { VGA_640_400, 32 }, /* 0x324 */ - { 0, 0 }, - { 0, 0 }, - { 0, 0 }, - { 0, 0 }, - { VGA_640_480, 32 }, /* 0x329 */ - { 0, 0 }, - { 0, 0 }, - { 0, 0 }, - { 0, 0 }, - { 0, 0 }, - { VGA_896_672, 8 }, /* 0x32f */ - { VGA_896_672, 15 }, /* 0x330 */ - { VGA_896_672, 16 }, /* 0x331 */ - { VGA_896_672, 24 }, /* 0x332 */ - { VGA_896_672, 32 }, /* 0x333 */ - { 0, 0 }, - { 0, 0 }, - { 0, 0 }, - { 0, 0 }, - { 0, 0 }, - { 0, 0 }, - { 0, 0 }, - { 0, 0 }, - { 0, 0 }, - { 0, 0 }, - { 0, 0 }, - { 0, 0 }, - { 0, 0 }, - { 0, 0 }, - { VGA_1600_1200, 32 }, /* 0x342 */ - { 0, 0 }, - { 0, 0 }, - { 0, 0 }, - { 0, 0 }, - { 0, 0 }, - { 0, 0 }, - { 0, 0 }, - { 0, 0 }, - { 0, 0 }, - { 0, 0 }, - { 0, 0 }, - { 0, 0 }, - { 0, 0 }, - { 0, 0 }, - { 0, 0 }, - { 0, 0 }, - { 0, 0 }, - { 0, 0 }, - { 0, 0 }, - { 0, 0 }, - { 0, 0 }, - { 0, 0 }, - { 0, 0 }, - { 0, 0 }, - { 0, 0 }, - { 0, 0 }, - { 0, 0 }, - { 0, 0 }, - { 0, 0 }, - { VGA_1440_900, 8 }, /* 0x360 */ - { VGA_1440_900, 15 }, /* 0x361 */ - { VGA_1440_900, 16 }, /* 0x362 */ - { VGA_1440_900, 24 }, /* 0x363 */ - { VGA_1440_900, 32 }, /* 0x364 */ - { VGA_1152_720, 8 }, /* 0x365 */ - { VGA_1152_720, 15 }, /* 0x366 */ - { VGA_1152_720, 16 }, /* 0x367 */ - { VGA_1152_720, 24 }, /* 0x368 */ - { VGA_1152_720, 32 }, /* 0x369 */ - { VGA_1024_640, 8 }, /* 0x36a */ - { VGA_1024_640, 15 }, /* 0x36b */ - { VGA_1024_640, 16 }, /* 0x36c */ - { VGA_1024_640, 24 }, /* 0x36d */ - { VGA_1024_640, 32 }, /* 0x36e */ - { VGA_800_500, 8 }, /* 0x36f */ - { VGA_800_500, 15 }, /* 0x370 */ - { VGA_800_500, 16 }, /* 0x371 */ - { VGA_800_500, 24 }, /* 0x372 */ - { VGA_800_500, 32 }, /* 0x373 */ - }; -#endif - static inline grub_size_t page_align (grub_size_t size) { @@ -882,7 +717,7 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)), /* Video mode selection support. */ char *val = argv[i] + 4; unsigned vid_mode = GRUB_LINUX_VID_MODE_NORMAL; - struct linux_vesafb_mode *linux_mode; + struct grub_vesa_mode_table_entry *linux_mode; grub_err_t err; char *buf; @@ -925,9 +760,8 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)), break; default: /* Ignore invalid values. */ - if (vid_mode < GRUB_LINUX_VID_MODE_VESA_START || - vid_mode >= GRUB_LINUX_VID_MODE_VESA_START + - ARRAY_SIZE (linux_vesafb_modes)) + if (vid_mode < GRUB_VESA_MODE_TABLE_START || + vid_mode > GRUB_VESA_MODE_TABLE_END) { grub_env_set ("gfxpayload", "text"); grub_printf ("%s is deprecated. Mode %d isn't recognized. " @@ -941,15 +775,13 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)), is built-in because `vga=' parameter was used. */ params->have_vga = GRUB_VIDEO_LINUX_TYPE_VESA; - linux_mode - = &linux_vesafb_modes[vid_mode - GRUB_LINUX_VID_MODE_VESA_START]; + linux_mode = &grub_vesa_mode_table[vid_mode + - GRUB_VESA_MODE_TABLE_START]; buf = grub_xasprintf ("%ux%ux%u,%ux%u", - linux_vesafb_res[linux_mode->res_index].width, - linux_vesafb_res[linux_mode->res_index].height, + linux_mode->width, linux_mode->height, linux_mode->depth, - linux_vesafb_res[linux_mode->res_index].width, - linux_vesafb_res[linux_mode->res_index].height); + linux_mode->width, linux_mode->height); if (! buf) goto fail; From 890c9fa5f2fd0177d1222c2871ca40510904e800 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Wed, 15 Sep 2010 11:42:18 +0200 Subject: [PATCH 34/37] Implement APM --- grub-core/Makefile.core.def | 6 ++ grub-core/commands/i386/pc/lsapm.c | 113 ++++++++++++++++++++++++ grub-core/lib/legacy_parse.c | 3 +- grub-core/loader/i386/multiboot_mbi.c | 27 +++++- grub-core/loader/multiboot.c | 2 - grub-core/loader/multiboot_mbi2.c | 29 +++++- include/grub/i386/pc/apm.h | 48 ++++++++++ include/grub/i386/pc/int.h | 1 + include/grub/i386/pc/vesa_modes_table.h | 19 ++++ include/multiboot.h | 14 +++ 10 files changed, 257 insertions(+), 5 deletions(-) create mode 100644 grub-core/commands/i386/pc/lsapm.c create mode 100644 include/grub/i386/pc/apm.h create mode 100644 include/grub/i386/pc/vesa_modes_table.h diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def index 03505ad5a..584b9754d 100644 --- a/grub-core/Makefile.core.def +++ b/grub-core/Makefile.core.def @@ -1433,3 +1433,9 @@ module = { name = testload; common = commands/testload.c; }; + +module = { + name = lsapm; + common = commands/i386/pc/lsapm.c; + enable = i386_pc; +}; diff --git a/grub-core/commands/i386/pc/lsapm.c b/grub-core/commands/i386/pc/lsapm.c new file mode 100644 index 000000000..30475d2ec --- /dev/null +++ b/grub-core/commands/i386/pc/lsapm.c @@ -0,0 +1,113 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2010 Free Software Foundation, Inc. + * + * GRUB 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, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#include +#include +#include +#include +#include + +int +grub_apm_get_info (struct grub_apm_info *info) +{ + struct grub_bios_int_registers regs; + + /* detect APM */ + regs.eax = 0x5300; + regs.ebx = 0; + regs.flags = GRUB_CPU_INT_FLAGS_DEFAULT; + grub_bios_interrupt (0x15, ®s); + + if (regs.flags & GRUB_CPU_INT_FLAGS_CARRY) + return 0; + info->version = regs.eax & 0xffff; + info->flags = regs.ecx & 0xffff; + + /* disconnect APM first */ + regs.eax = 0x5304; + regs.ebx = 0; + regs.flags = GRUB_CPU_INT_FLAGS_DEFAULT; + grub_bios_interrupt (0x15, ®s); + + /* connect APM */ + regs.eax = 0x5303; + regs.ebx = 0; + regs.flags = GRUB_CPU_INT_FLAGS_DEFAULT; + grub_bios_interrupt (0x15, ®s); + + if (regs.flags & GRUB_CPU_INT_FLAGS_CARRY) + return 0; + + info->cseg = regs.eax & 0xffff; + info->offset = regs.ebx; + info->cseg_16 = regs.ecx & 0xffff; + info->dseg = regs.edx & 0xffff; + info->cseg_len = regs.esi >> 16; + info->cseg_16_len = regs.esi & 0xffff; + info->dseg_len = regs.edi; + + return 1; +} + +static grub_err_t +grub_cmd_lsapm (grub_command_t cmd __attribute__ ((unused)), + int argc __attribute__ ((unused)), char **args __attribute__ ((unused))) +{ + struct grub_apm_info info; + if (!grub_apm_get_info (&info)) + return grub_error (GRUB_ERR_IO, "no APM found"); + + grub_printf ("Vesion %u.%u\n" + "32-bit CS = 0x%x, len = 0x%x, offset = 0x%x\n" + "16-bit CS = 0x%x, len = 0x%x\n" + "DS = 0x%x, len = 0x%x\n", + info.version >> 8, info.version & 0xff, + info.cseg, info.cseg_len, info.offset, + info.cseg_16, info.cseg_16_len, + info.dseg, info.dseg_len); + grub_xputs (info.flags & GRUB_APM_FLAGS_16BITPROTECTED_SUPPORTED + ? "16-bit protected interface supported\n" + : "16-bit protected interface unsupported\n"); + grub_xputs (info.flags & GRUB_APM_FLAGS_32BITPROTECTED_SUPPORTED + ? "32-bit protected interface supported\n" + : "32-bit protected interface unsupported\n"); + grub_xputs (info.flags & GRUB_APM_FLAGS_CPUIDLE_SLOWS_DOWN + ? "CPU Idle slows down processor\n" + : "CPU Idle doesn't slow down processor\n"); + grub_xputs (info.flags & GRUB_APM_FLAGS_DISABLED + ? "APM disabled\n" : "APM enabled\n"); + grub_xputs (info.flags & GRUB_APM_FLAGS_DISENGAGED + ? "APM disengaged\n" : "APM engaged\n"); + + return GRUB_ERR_NONE; +} + +static grub_command_t cmd; + +GRUB_MOD_INIT(lsapm) +{ + cmd = grub_register_command ("lsapm", grub_cmd_lsapm, 0, + N_("Show APM information.")); +} + +GRUB_MOD_FINI(lsapm) +{ + grub_unregister_command (cmd); +} + + diff --git a/grub-core/lib/legacy_parse.c b/grub-core/lib/legacy_parse.c index 959d8367d..024d425e8 100644 --- a/grub-core/lib/legacy_parse.c +++ b/grub-core/lib/legacy_parse.c @@ -99,7 +99,8 @@ struct legacy_command legacy_commands[] = "Set the default entry to entry number NUM (if not specified, it is" " 0, the first entry) or the entry number saved by savedefault."}, /* FIXME: dhcp unsupported. */ - /* FIXME: displayapm unsupported. */ + {"displayapm", "lsapm\n", NULL, 0, 0, {}, 0, 0, + "Display APM BIOS information."}, {"displaymem", "lsmmap\n", NULL, 0, 0, {}, 0, 0, "Display what GRUB thinks the system address space map of the" " machine is, including all regions of physical RAM installed."}, diff --git a/grub-core/loader/i386/multiboot_mbi.c b/grub-core/loader/i386/multiboot_mbi.c index bf17863cf..2cce39746 100644 --- a/grub-core/loader/i386/multiboot_mbi.c +++ b/grub-core/loader/i386/multiboot_mbi.c @@ -20,6 +20,7 @@ #include #ifdef GRUB_MACHINE_PCBIOS #include +#include #endif #include #include @@ -194,7 +195,8 @@ grub_multiboot_get_mbi_size (void) + ALIGN_UP (sizeof(PACKAGE_STRING), 4) + grub_get_multiboot_mmap_count () * sizeof (struct multiboot_mmap_entry) + elf_sec_entsize * elf_sec_num - + 256 * sizeof (struct multiboot_color); + + 256 * sizeof (struct multiboot_color) + + ALIGN_UP (sizeof (struct multiboot_apm_info), 4); } /* Fill previously allocated Multiboot mmap. */ @@ -356,6 +358,29 @@ grub_multiboot_make_mbi (grub_uint32_t *target) ptrorig += ALIGN_UP (sizeof(PACKAGE_STRING), 4); ptrdest += ALIGN_UP (sizeof(PACKAGE_STRING), 4); +#ifdef GRUB_MACHINE_PCBIOS + { + struct grub_apm_info info; + if (grub_apm_get_info (&info)) + { + struct multiboot_apm_info *mbinfo = (void *) ptrorig; + + mbinfo->cseg = info.cseg; + mbinfo->offset = info.offset; + mbinfo->cseg_16 = info.cseg_16; + mbinfo->dseg = info.dseg; + mbinfo->flags = info.flags; + mbinfo->cseg_len = info.cseg_len; + mbinfo->dseg_len = info.dseg_len; + mbinfo->cseg_16_len = info.cseg_16_len; + mbinfo->version = info.version; + + ptrorig += ALIGN_UP (sizeof (struct multiboot_apm_info), 4); + ptrdest += ALIGN_UP (sizeof (struct multiboot_apm_info), 4); + } + } +#endif + if (modcnt) { mbi->flags |= MULTIBOOT_INFO_MODS; diff --git a/grub-core/loader/multiboot.c b/grub-core/loader/multiboot.c index 8780ec061..d5cb42604 100644 --- a/grub-core/loader/multiboot.c +++ b/grub-core/loader/multiboot.c @@ -21,10 +21,8 @@ * FIXME: The following features from the Multiboot specification still * need to be implemented: * - VBE support - * - symbol table * - drives table * - ROM configuration table - * - APM table */ #include diff --git a/grub-core/loader/multiboot_mbi2.c b/grub-core/loader/multiboot_mbi2.c index f453dcc6a..2e6801252 100644 --- a/grub-core/loader/multiboot_mbi2.c +++ b/grub-core/loader/multiboot_mbi2.c @@ -20,6 +20,7 @@ #include #ifdef GRUB_MACHINE_PCBIOS #include +#include #endif #include #include @@ -279,7 +280,8 @@ grub_multiboot_get_mbi_size (void) + elf_sec_entsize * elf_sec_num + (sizeof (struct multiboot_tag_mmap) + grub_get_multiboot_mmap_count () * sizeof (struct multiboot_mmap_entry)) - + sizeof (struct multiboot_tag_vbe) + MULTIBOOT_TAG_ALIGN - 1; + + sizeof (struct multiboot_tag_vbe) + MULTIBOOT_TAG_ALIGN - 1 + + sizeof (struct multiboot_tag_apm) + MULTIBOOT_TAG_ALIGN - 1; } /* Fill previously allocated Multiboot mmap. */ @@ -515,6 +517,31 @@ grub_multiboot_make_mbi (grub_uint32_t *target) ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN); } +#ifdef GRUB_MACHINE_PCBIOS + { + struct grub_apm_info info; + if (grub_apm_get_info (&info)) + { + struct multiboot_tag_apm *tag = (struct multiboot_tag_apm *) ptrorig; + + tag->type = MULTIBOOT_TAG_TYPE_APM; + tag->size = sizeof (struct multiboot_tag_apm); + + tag->cseg = info.cseg; + tag->offset = info.offset; + tag->cseg_16 = info.cseg_16; + tag->dseg = info.dseg; + tag->flags = info.flags; + tag->cseg_len = info.cseg_len; + tag->dseg_len = info.dseg_len; + tag->cseg_16_len = info.cseg_16_len; + tag->version = info.version; + + ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN); + } + } +#endif + { unsigned i; struct module *cur; diff --git a/include/grub/i386/pc/apm.h b/include/grub/i386/pc/apm.h new file mode 100644 index 000000000..6d9e8c61d --- /dev/null +++ b/include/grub/i386/pc/apm.h @@ -0,0 +1,48 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2010 Free Software Foundation, Inc. + * + * GRUB 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, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#ifndef GRUB_APM_MACHINE_HEADER +#define GRUB_APM_MACHINE_HEADER 1 + +#include + +struct grub_apm_info +{ + grub_uint16_t cseg; + grub_uint32_t offset; + grub_uint16_t cseg_16; + grub_uint16_t dseg; + grub_uint16_t flags; + grub_uint16_t cseg_len; + grub_uint16_t cseg_16_len; + grub_uint16_t dseg_len; + grub_uint16_t version; +}; + +enum + { + GRUB_APM_FLAGS_16BITPROTECTED_SUPPORTED = 1, + GRUB_APM_FLAGS_32BITPROTECTED_SUPPORTED = 2, + GRUB_APM_FLAGS_CPUIDLE_SLOWS_DOWN = 4, + GRUB_APM_FLAGS_DISABLED = 8, + GRUB_APM_FLAGS_DISENGAGED = 16, + }; + +int grub_apm_get_info (struct grub_apm_info *info); + +#endif diff --git a/include/grub/i386/pc/int.h b/include/grub/i386/pc/int.h index e1c463925..de23775d0 100644 --- a/include/grub/i386/pc/int.h +++ b/include/grub/i386/pc/int.h @@ -20,6 +20,7 @@ #define GRUB_INTERRUPT_MACHINE_HEADER 1 #include +#include struct grub_bios_int_registers { diff --git a/include/grub/i386/pc/vesa_modes_table.h b/include/grub/i386/pc/vesa_modes_table.h new file mode 100644 index 000000000..376ca376b --- /dev/null +++ b/include/grub/i386/pc/vesa_modes_table.h @@ -0,0 +1,19 @@ +#ifndef GRUB_VESA_MODE_TABLE_HEADER +#define GRUB_VESA_MODE_TABLE_HEADER 1 + +#include + +#define GRUB_VESA_MODE_TABLE_START 0x300 +#define GRUB_VESA_MODE_TABLE_END 0x373 + +struct grub_vesa_mode_table_entry { + grub_uint16_t width; + grub_uint16_t height; + grub_uint8_t depth; +}; + +extern struct grub_vesa_mode_table_entry +grub_vesa_mode_table[GRUB_VESA_MODE_TABLE_END + - GRUB_VESA_MODE_TABLE_START + 1]; + +#endif diff --git a/include/multiboot.h b/include/multiboot.h index fda863e85..ed71e6b96 100644 --- a/include/multiboot.h +++ b/include/multiboot.h @@ -254,6 +254,20 @@ struct multiboot_mod_list }; typedef struct multiboot_mod_list multiboot_module_t; +/* APM BIOS info. */ +struct multiboot_apm_info +{ + grub_uint16_t version; + grub_uint16_t cseg; + grub_uint32_t offset; + grub_uint16_t cseg_16; + grub_uint16_t dseg; + grub_uint16_t flags; + grub_uint16_t cseg_len; + grub_uint16_t cseg_16_len; + grub_uint16_t dseg_len; +}; + #endif /* ! ASM_FILE */ #endif /* ! MULTIBOOT_HEADER */ From e2830452f05001e1c24e718deec6c71b7dc39e70 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Wed, 15 Sep 2010 13:51:02 +0200 Subject: [PATCH 35/37] Support legacy_check_password --- grub-core/commands/legacycfg.c | 91 +++++++++++++++++++++++++++------- grub-core/lib/legacy_parse.c | 25 +++++++--- 2 files changed, 93 insertions(+), 23 deletions(-) diff --git a/grub-core/commands/legacycfg.c b/grub-core/commands/legacycfg.c index bea608b9e..463297810 100644 --- a/grub-core/commands/legacycfg.c +++ b/grub-core/commands/legacycfg.c @@ -603,19 +603,14 @@ ib64t (char c) return -1; } -static grub_err_t -grub_cmd_legacy_password (struct grub_command *mycmd __attribute__ ((unused)), - int argc, char **args) +static struct legacy_md5_password * +parse_legacy_md5 (int argc, char **args) { const char *salt, *saltend; - const char *p; struct legacy_md5_password *pw = NULL; int i; + const char *p; - if (argc == 0) - return grub_error (GRUB_ERR_BAD_ARGUMENT, "arguments expected"); - if (args[0][0] != '-' || args[0][1] != '-') - return grub_normal_set_password ("legacy", args[0]); if (grub_memcmp (args[0], "--md5", sizeof ("--md5")) != 0) goto fail; if (argc == 1) @@ -667,21 +662,76 @@ grub_cmd_legacy_password (struct grub_command *mycmd __attribute__ ((unused)), if (!pw->salt) goto fail; - return grub_auth_register_authentication ("legacy", check_password_md5, pw); + return pw; fail: grub_free (pw); - /* This is to imitate minor difference between grub-legacy in GRUB2. - If 2 password commands are executed in a row and second one fails - on GRUB2 the password of first one is used, whereas in grub-legacy - authenthication is denied. In case of no password command was executed - early both versions deny any access. */ - return grub_auth_register_authentication ("legacy", check_password_deny, - NULL); + return NULL; +} + +static grub_err_t +grub_cmd_legacy_password (struct grub_command *mycmd __attribute__ ((unused)), + int argc, char **args) +{ + struct legacy_md5_password *pw = NULL; + + if (argc == 0) + return grub_error (GRUB_ERR_BAD_ARGUMENT, "arguments expected"); + if (args[0][0] != '-' || args[0][1] != '-') + return grub_normal_set_password ("legacy", args[0]); + + pw = parse_legacy_md5 (argc, args); + + if (pw) + return grub_auth_register_authentication ("legacy", check_password_md5, pw); + else + /* This is to imitate minor difference between grub-legacy in GRUB2. + If 2 password commands are executed in a row and second one fails + on GRUB2 the password of first one is used, whereas in grub-legacy + authenthication is denied. In case of no password command was executed + early both versions deny any access. */ + return grub_auth_register_authentication ("legacy", check_password_deny, + NULL); +} + +static grub_err_t +grub_cmd_legacy_check_password (struct grub_command *mycmd __attribute__ ((unused)), + int argc, char **args) +{ + struct legacy_md5_password *pw = NULL; + char entered[GRUB_AUTH_MAX_PASSLEN]; + + if (argc == 0) + return grub_error (GRUB_ERR_BAD_ARGUMENT, "arguments expected"); + grub_printf ("Enter password:"); + if (!grub_password_get (entered, GRUB_AUTH_MAX_PASSLEN)) + return GRUB_ACCESS_DENIED; + + if (args[0][0] != '-' || args[0][1] != '-') + { + char correct[GRUB_AUTH_MAX_PASSLEN]; + + grub_memset (correct, 0, sizeof (correct)); + grub_strncpy (correct, args[0], sizeof (correct)); + + if (grub_crypto_memcmp (entered, correct, GRUB_AUTH_MAX_PASSLEN) != 0) + return GRUB_ACCESS_DENIED; + return GRUB_ERR_NONE; + } + + pw = parse_legacy_md5 (argc, args); + + if (!pw) + return GRUB_ACCESS_DENIED; + + if (!check_password_md5_real (entered, pw)) + return GRUB_ACCESS_DENIED; + + return GRUB_ERR_NONE; } static grub_command_t cmd_source, cmd_configfile, cmd_kernel, cmd_initrd; -static grub_command_t cmd_password, cmd_initrdnounzip; +static grub_command_t cmd_password, cmd_check_password, cmd_initrdnounzip; GRUB_MOD_INIT(legacycfg) { @@ -711,6 +761,12 @@ GRUB_MOD_INIT(legacycfg) grub_cmd_legacy_password, N_("[--md5] PASSWD [FILE]"), N_("Simulate grub-legacy password command")); + + cmd_check_password = grub_register_command ("legacy_check_password", + grub_cmd_legacy_check_password, + N_("[--md5] PASSWD [FILE]"), + N_("Simulate grub-legacy password command in menuentry mode")); + } GRUB_MOD_FINI(legacycfg) @@ -721,4 +777,5 @@ GRUB_MOD_FINI(legacycfg) grub_unregister_command (cmd_initrd); grub_unregister_command (cmd_initrdnounzip); grub_unregister_command (cmd_password); + grub_unregister_command (cmd_check_password); } diff --git a/grub-core/lib/legacy_parse.c b/grub-core/lib/legacy_parse.c index 024d425e8..e5014cdc7 100644 --- a/grub-core/lib/legacy_parse.c +++ b/grub-core/lib/legacy_parse.c @@ -49,6 +49,8 @@ struct legacy_command FLAG_FALLBACK_AVAILABLE = 4, FLAG_FALLBACK = 8, FLAG_COLOR_INVERT = 16, + FLAG_NO_MENUENTRY = 32, + FLAG_MENUENTRY_ONLY = 64, } flags; const char *shortdesc; const char *longdesc; @@ -189,12 +191,12 @@ struct legacy_command legacy_commands[] = {"parttype", "parttool '%s' type=%s\n", NULL, 0, 2, {TYPE_PARTITION, TYPE_INT}, 0, "PART TYPE", "Change the type of the partition PART to TYPE."}, - /* FIXME: support usage in menuentry. */ {"password", "if [ \"$superusers\" = "" ]; then superusers=legacy; fi;\n" - "legacy_password %s '%s'", + "legacy_password %s '%s'\n", "menuentry \"Superuser menu\" --users \"legacy\" { configfile '%s'; }\n", 2, 3, {TYPE_OPTION, TYPE_VERBATIM, TYPE_FILE}, - FLAG_IGNORE_REST | FLAG_FALLBACK_AVAILABLE, "[--md5] PASSWD [FILE]", + FLAG_IGNORE_REST | FLAG_FALLBACK_AVAILABLE | FLAG_NO_MENUENTRY, + "[--md5] PASSWD [FILE]", "If used in the first section of a menu file, disable all" " interactive editing control (menu entry editor and" " command line). If the password PASSWD is entered, it loads the" @@ -205,8 +207,15 @@ struct legacy_command legacy_commands[] = " The option --md5 tells GRUB that PASSWD is encrypted with" " md5crypt."}, {"password", "if [ \"$superusers\" = "" ]; then superusers=legacy; fi;\n" - "legacy_password %s '%s'", NULL, 0, 2, {TYPE_OPTION, TYPE_VERBATIM}, - FLAG_IGNORE_REST | FLAG_FALLBACK, NULL, NULL}, + "legacy_password %s '%s'\n", NULL, 0, 2, {TYPE_OPTION, TYPE_VERBATIM}, + FLAG_IGNORE_REST | FLAG_FALLBACK | FLAG_NO_MENUENTRY, NULL, NULL}, + {"password", "if legacy_check_password %s '%s'; then configfile '%s'; " + "else return; fi\n", NULL, 2, 3, {TYPE_OPTION, TYPE_VERBATIM, TYPE_FILE}, + FLAG_IGNORE_REST | FLAG_FALLBACK_AVAILABLE | FLAG_MENUENTRY_ONLY, + NULL, NULL}, + {"password", "if ! legacy_check_password %s '%s'; then return fi;\n", + NULL, 0, 2, {TYPE_OPTION, TYPE_VERBATIM}, + FLAG_IGNORE_REST | FLAG_FALLBACK | FLAG_MENUENTRY_ONLY, NULL, NULL}, /* NOTE: GRUB2 has a design principle of not eternally waiting for user input. 60 seconds should be enough. */ @@ -442,7 +451,11 @@ grub_legacy_parse (const char *buf, char **entryname, char **suffix) for (cmdnum = 0; cmdnum < ARRAY_SIZE (legacy_commands); cmdnum++) if (grub_strncmp (legacy_commands[cmdnum].name, cmdname, ptr - cmdname) == 0 - && legacy_commands[cmdnum].name[ptr - cmdname] == 0) + && legacy_commands[cmdnum].name[ptr - cmdname] == 0 + && (!(*entryname != NULL && (legacy_commands[cmdnum].flags + & FLAG_NO_MENUENTRY))) + && (!(*entryname == NULL && (legacy_commands[cmdnum].flags + & FLAG_MENUENTRY_ONLY)))) break; if (cmdnum == ARRAY_SIZE (legacy_commands)) return grub_xasprintf ("# Unsupported legacy command: %s\n", buf); From c99dead65448b083befbb9977f19d22c8ea14902 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Wed, 15 Sep 2010 14:11:08 +0200 Subject: [PATCH 36/37] Support geometry --- grub-core/lib/legacy_parse.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/grub-core/lib/legacy_parse.c b/grub-core/lib/legacy_parse.c index e5014cdc7..a0be27d60 100644 --- a/grub-core/lib/legacy_parse.c +++ b/grub-core/lib/legacy_parse.c @@ -118,7 +118,9 @@ struct legacy_command legacy_commands[] = "Search for the filename FILENAME in all of partitions and print the list of" " the devices which contain the file."}, /* FIXME: fstest unsupported. */ - /* FIXME: geometry unsupported. */ + /* NOTE: The obsolete C/H/S geometry isn't shown anymore. */ + {"geometry", "insmod regexp; ls -l (%s*)\n", NULL, 0, 1, {TYPE_VERBATIM}, 0, "DRIVE", + "Print the information for a drive DRIVE. "}, {"halt", "halt %s\n", NULL, 0, 1, {TYPE_NOAPM_OPTION}, 0, "[--no-apm]", "Halt your system. If APM is available on it, turn off the power using" " the APM BIOS, unless you specify the option `--no-apm'."}, From 3f8fcb6a24414ab86f1ea37501ae6d608a2487b6 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Wed, 15 Sep 2010 14:37:28 +0200 Subject: [PATCH 37/37] Support vbeprobe MODE --- grub-core/commands/videoinfo.c | 46 +++++++++++++++++++++++++++++----- grub-core/lib/legacy_parse.c | 8 +++--- 2 files changed, 45 insertions(+), 9 deletions(-) diff --git a/grub-core/commands/videoinfo.c b/grub-core/commands/videoinfo.c index 15f677e14..10f77915b 100644 --- a/grub-core/commands/videoinfo.c +++ b/grub-core/commands/videoinfo.c @@ -25,9 +25,17 @@ #include #include +static unsigned height, width, depth; + static int hook (const struct grub_video_mode_info *info) { + if (height && width && (info->width != width || info->height != height)) + return 0; + + if (depth && info->bpp != depth) + return 0; + if (info->mode_number == GRUB_VIDEO_MODE_NUMBER_INVALID) grub_printf (" "); else @@ -71,12 +79,34 @@ hook (const struct grub_video_mode_info *info) static grub_err_t grub_cmd_videoinfo (grub_command_t cmd __attribute__ ((unused)), - int argc __attribute__ ((unused)), - char **args __attribute__ ((unused))) + int argc, char **args) { grub_video_adapter_t adapter; grub_video_driver_id_t id; + height = width = depth = 0; + if (argc) + { + char *ptr; + ptr = args[0]; + width = grub_strtoul (ptr, &ptr, 0); + if (grub_errno) + return grub_errno; + if (*ptr != 'x') + return grub_error (GRUB_ERR_BAD_ARGUMENT, "invalid mode specification"); + ptr++; + height = grub_strtoul (ptr, &ptr, 0); + if (grub_errno) + return grub_errno; + if (*ptr == 'x') + { + ptr++; + depth = grub_strtoul (ptr, &ptr, 0); + if (grub_errno) + return grub_errno; + } + } + #ifdef GRUB_MACHINE_PCBIOS if (grub_strcmp (cmd->name, "vbeinfo") == 0) grub_dl_load ("vbe"); @@ -132,11 +162,15 @@ static grub_command_t cmd_vbe; GRUB_MOD_INIT(videoinfo) { - cmd = grub_register_command ("videoinfo", grub_cmd_videoinfo, 0, - N_("List available video modes.")); + cmd = grub_register_command ("videoinfo", grub_cmd_videoinfo, "[WxH[xD]]", + N_("List available video modes. If " + "resolution is given show only modes" + " matching it.")); #ifdef GRUB_MACHINE_PCBIOS - cmd_vbe = grub_register_command ("vbeinfo", grub_cmd_videoinfo, 0, - N_("List available video modes.")); + cmd_vbe = grub_register_command ("vbeinfo", grub_cmd_videoinfo, "[WxH[xD]]", + N_("List available video modes. If " + "resolution is given show only modes" + " matching it.")); #endif } diff --git a/grub-core/lib/legacy_parse.c b/grub-core/lib/legacy_parse.c index a0be27d60..6ad15dc49 100644 --- a/grub-core/lib/legacy_parse.c +++ b/grub-core/lib/legacy_parse.c @@ -292,10 +292,12 @@ struct legacy_command legacy_commands[] = /* FIXME: uppermem unsupported. */ {"uuid", "search --set=root --fs-uuid '%s'\n", NULL, 0, 1, {TYPE_VERBATIM}, 0, "UUID", "Find root by UUID"}, - /* FIXME: support MODE. */ - {"vbeprobe", "insmod vbe; videoinfo\n", NULL, 0, 0, {}, 0, "[MODE]", + {"vbeprobe", "insmod vbe; videoinfo '%s'\n", NULL, 0, 1, {TYPE_VBE_MODE}, + FLAG_FALLBACK_AVAILABLE, "[MODE]", "Probe VBE information. If the mode number MODE is specified, show only" - " the information about only the mode."} + " the information about only the mode."}, + {"vbeprobe", "insmod vbe; videoinfo\n", NULL, 0, 0, {}, + FLAG_FALLBACK, NULL, NULL} }; char *