Integrate hints into autogeneration scripts.

* docs/grub.texi (Filesystems): Add a hostdisk example.
	* Makefile.util.def (grub-mkdevicemap): Remove ofpath.
	(grub-probe): Add ofpath.
	* gentpl.py: Remove group nosparc64.
	* grub-core/commands/search.c (cache_entry): New struct.
	(cache): New var.
	(FUNC_NAME): Use and save cache. Fix handling of trailing comma.
	* grub-core/commands/search_wrap.c (options): Add platform-specific
	hint options.
	(grub_cmd_search): Handle platform-specific hints.
	(GRUB_MOD_INIT): Declare grub_cmd_search as accept_dash.
	* grub-core/kern/emu/hostdisk.c (map): New field device_map.
	(grub_util_biosdisk_data): Likewise.
	(grub_util_biosdisk_open): Set device_map.
	(read_device_map): Handle "" as indication of no map.
	Set device_map.
	(find_system_device): Add hostdisk/ prefix for autogenerated entries.
	(grub_util_biosdisk_get_compatibility_hint): New function.
	* grub-core/normal/main.c (features): Add feature_platform_search_hint.
	* include/grub/emu/hostdisk.h
	(grub_util_biosdisk_get_compatibility_hint): New proto.
	* util/grub-install.in: Don't call grub-mkdevicemap.
	Add platform-specific hint to load.cfg.
	* util/grub-mkconfig.in: Don't call grub-mkdevicemap.
	* util/grub-mkconfig_lib.in (prepare_grub_to_access_device): Add
	hints. Set root preliminary to compatibility hint, not to OS name.
	* util/grub-probe.c (PRINT_*): Add hints.
	(print): Make static.
	(escape_of_path): New function.
	(guess_bios_drive): Likewise.
	(guess_efi_drive): Likewise.
	(guess_baremetal_drive): Likewise.
	(print_full_name): Likewise.
	(probe): Handle hints.
	(main): Likewise.
	* util/ieee1275/devicemap.c: Removed.
	* util/ieee1275/ofpath.c (find_obppath): Allow to fail. All users
	updated.
	(grub_util_devname_to_ofpath): Return NULL on failure.

	* grub-core/kern/emu/hostdisk.c (grub_util_biosdisk_get_grub_dev): Fix
	resource leak.
	* util/getroot.c (grub_util_pull_device): Fix memory leak.

	* po/POTFILES.in: Regenerated.

	Allow purely long options

	* grub-core/lib/arg.c (SHORT_ARG_HELP): Removed.
	(SHORT_ARG_USAGE): Likewise.
	(grub_arg_show_help): Compare opt with help_options.
	(parse_option): Receive opt as argument. If makes big simplificatons.
	All users updated
This commit is contained in:
Vladimir 'phcoder' Serbinenko 2011-12-24 15:09:26 +01:00
commit d3c13cbd62
19 changed files with 741 additions and 176 deletions

View file

@ -33,11 +33,21 @@
GRUB_MOD_LICENSE ("GPLv3+");
struct cache_entry
{
struct cache_entry *next;
char *key;
char *value;
};
static struct cache_entry *cache;
void
FUNC_NAME (const char *key, const char *var, int no_floppy,
char **hints, unsigned nhints)
{
int count = 0;
int is_cache = 0;
grub_fs_autoload_hook_t saved_autoload;
auto int iterate_device (const char *name);
@ -50,6 +60,12 @@ FUNC_NAME (const char *key, const char *var, int no_floppy,
name[0] == 'f' && name[1] == 'd' && name[2] >= '0' && name[2] <= '9')
return 0;
#ifdef DO_SEARCH_FS_UUID
#define compare_fn grub_strcasecmp
#else
#define compare_fn grub_strcmp
#endif
#ifdef DO_SEARCH_FILE
{
char *buf;
@ -81,10 +97,8 @@ FUNC_NAME (const char *key, const char *var, int no_floppy,
fs = grub_fs_probe (dev);
#ifdef DO_SEARCH_FS_UUID
#define compare_fn grub_strcasecmp
#define read_fn uuid
#else
#define compare_fn grub_strcmp
#define read_fn label
#endif
@ -106,6 +120,31 @@ FUNC_NAME (const char *key, const char *var, int no_floppy,
}
#endif
if (!is_cache && found && count == 0)
{
struct cache_entry *cache_ent;
cache_ent = grub_malloc (sizeof (*cache_ent));
if (cache_ent)
{
cache_ent->key = grub_strdup (key);
cache_ent->value = grub_strdup (name);
if (cache_ent->value && cache_ent->key)
{
cache_ent->next = cache;
cache = cache_ent;
}
else
{
grub_free (cache_ent->value);
grub_free (cache_ent->key);
grub_free (cache_ent);
grub_errno = GRUB_ERR_NONE;
}
}
else
grub_errno = GRUB_ERR_NONE;
}
if (found)
{
count++;
@ -143,6 +182,32 @@ FUNC_NAME (const char *key, const char *var, int no_floppy,
void try (void)
{
unsigned i;
struct cache_entry **prev;
struct cache_entry *cache_ent;
for (prev = &cache, cache_ent = *prev; cache_ent;
prev = &cache_ent->next, cache_ent = *prev)
if (compare_fn (cache_ent->key, key) == 0)
break;
if (cache_ent)
{
is_cache = 1;
if (iterate_device (cache_ent->value))
{
is_cache = 0;
return;
}
is_cache = 0;
/* Cache entry was outdated. Remove it. */
if (!count)
{
grub_free (cache_ent->key);
grub_free (cache_ent->value);
grub_free (cache_ent);
*prev = cache_ent->next;
}
}
for (i = 0; i < nhints; i++)
{
char *end;
@ -164,17 +229,20 @@ FUNC_NAME (const char *key, const char *var, int no_floppy,
dev = grub_device_open (hints[i]);
if (!dev)
{
*end = ',';
if (!*end)
*end = ',';
continue;
}
if (!dev->disk)
{
grub_device_close (dev);
*end = ',';
if (!*end)
*end = ',';
continue;
}
ret = grub_partition_iterate (dev->disk, part_hook);
*end = ',';
if (!*end)
*end = ',';
grub_device_close (dev);
if (ret)
return;

View file

@ -42,6 +42,21 @@ static const struct grub_arg_option options[] =
{"hint", 'h', GRUB_ARG_OPTION_REPEATABLE,
N_("First try the device HINT. If HINT ends in comma, "
"also try subpartitions"), N_("HINT"), ARG_TYPE_STRING},
{"hint-ieee1275", 0, GRUB_ARG_OPTION_REPEATABLE,
N_("First try the device HINT if on IEEE1275. If HINT ends in comma, "
"also try subpartitions"), N_("HINT"), ARG_TYPE_STRING},
{"hint-bios", 0, GRUB_ARG_OPTION_REPEATABLE,
N_("First try the device HINT if on BIOS. If HINT ends in comma, "
"also try subpartitions"), N_("HINT"), ARG_TYPE_STRING},
{"hint-baremetal", 0, GRUB_ARG_OPTION_REPEATABLE,
N_("First try the device HINT. If HINT ends in comma, "
"also try subpartitions"), N_("HINT"), ARG_TYPE_STRING},
{"hint-efi", 0, GRUB_ARG_OPTION_REPEATABLE,
N_("First try the device HINT if on EFI. If HINT ends in comma, "
"also try subpartitions"), N_("HINT"), ARG_TYPE_STRING},
{"hint-arc", 0, GRUB_ARG_OPTION_REPEATABLE,
N_("First try the device HINT if on ARC. If HINT ends in comma, "
"also try subpartitions"), N_("HINT"), ARG_TYPE_STRING},
{0, 0, 0, 0, 0, 0}
};
@ -52,7 +67,12 @@ enum options
SEARCH_FS_UUID,
SEARCH_SET,
SEARCH_NO_FLOPPY,
SEARCH_HINT
SEARCH_HINT,
SEARCH_HINT_IEEE1275,
SEARCH_HINT_BIOS,
SEARCH_HINT_BAREMETAL,
SEARCH_HINT_EFI,
SEARCH_HINT_ARC,
};
static grub_err_t
@ -60,27 +80,98 @@ grub_cmd_search (grub_extcmd_context_t ctxt, int argc, char **args)
{
struct grub_arg_list *state = ctxt->state;
const char *var = 0;
int nhints = 0;
int i = 0, j = 0, nhints = 0;
char **hints = NULL;
if (state[SEARCH_HINT].set)
while (state[SEARCH_HINT].args[nhints])
for (i = 0; state[SEARCH_HINT].args[i]; i++)
nhints++;
if (argc == 0)
#ifdef GRUB_MACHINE_IEEE1275
if (state[SEARCH_HINT_IEEE1275].set)
for (i = 0; state[SEARCH_HINT_IEEE1275].args[i]; i++)
nhints++;
#endif
#ifdef GRUB_MACHINE_EFI
if (state[SEARCH_HINT_EFI].set)
for (i = 0; state[SEARCH_HINT_EFI].args[i]; i++)
nhints++;
#endif
#ifdef GRUB_MACHINE_PCBIOS
if (state[SEARCH_HINT_BIOS].set)
for (i = 0; state[SEARCH_HINT_BIOS].args[i]; i++)
nhints++;
#endif
#ifdef GRUB_MACHINE_ARC
if (state[SEARCH_HINT_ARC].set)
for (i = 0; state[SEARCH_HINT_ARC].args[i]; i++)
nhints++;
#endif
if (state[SEARCH_HINT_BAREMETAL].set)
for (i = 0; state[SEARCH_HINT_BAREMETAL].args[i]; i++)
nhints++;
hints = grub_malloc (sizeof (hints[0]) * nhints);
if (!hints)
return grub_errno;
j = 0;
if (state[SEARCH_HINT].set)
for (i = 0; state[SEARCH_HINT].args[i]; i++)
hints[j++] = state[SEARCH_HINT].args[i];
#ifdef GRUB_MACHINE_IEEE1275
if (state[SEARCH_HINT_IEEE1275].set)
for (i = 0; state[SEARCH_HINT_IEEE1275].args[i]; i++)
hints[j++] = state[SEARCH_HINT_IEEE1275].args[i];
#endif
#ifdef GRUB_MACHINE_EFI
if (state[SEARCH_HINT_EFI].set)
for (i = 0; state[SEARCH_HINT_EFI].args[i]; i++)
hints[j++] = state[SEARCH_HINT_EFI].args[i];
#endif
#ifdef GRUB_MACHINE_ARC
if (state[SEARCH_HINT_ARC].set)
for (i = 0; state[SEARCH_HINT_ARC].args[i]; i++)
hints[j++] = state[SEARCH_HINT_ARC].args[i];
#endif
#ifdef GRUB_MACHINE_PCBIOS
if (state[SEARCH_HINT_BIOS].set)
for (i = 0; state[SEARCH_HINT_BIOS].args[i]; i++)
hints[j++] = state[SEARCH_HINT_BIOS].args[i];
#endif
if (state[SEARCH_HINT_BAREMETAL].set)
for (i = 0; state[SEARCH_HINT_BAREMETAL].args[i]; i++)
hints[j++] = state[SEARCH_HINT_BAREMETAL].args[i];
/* Skip hints for future platforms. */
for (j = 0; j < argc; j++)
if (grub_memcmp (args[j], "--hint-", sizeof ("--hint-") - 1) != 0)
break;
if (argc == j)
return grub_error (GRUB_ERR_BAD_ARGUMENT, "no argument specified");
if (state[SEARCH_SET].set)
var = state[SEARCH_SET].arg ? state[SEARCH_SET].arg : "root";
if (state[SEARCH_LABEL].set)
grub_search_label (args[0], var, state[SEARCH_NO_FLOPPY].set,
state[SEARCH_HINT].args, nhints);
grub_search_label (args[j], var, state[SEARCH_NO_FLOPPY].set,
hints, nhints);
else if (state[SEARCH_FS_UUID].set)
grub_search_fs_uuid (args[0], var, state[SEARCH_NO_FLOPPY].set,
state[SEARCH_HINT].args, nhints);
grub_search_fs_uuid (args[j], var, state[SEARCH_NO_FLOPPY].set,
hints, nhints);
else if (state[SEARCH_FILE].set)
grub_search_fs_file (args[0], var, state[SEARCH_NO_FLOPPY].set,
state[SEARCH_HINT].args, nhints);
grub_search_fs_file (args[j], var, state[SEARCH_NO_FLOPPY].set,
hints, nhints);
else
return grub_error (GRUB_ERR_INVALID_COMMAND, "unspecified search type");
@ -92,7 +183,8 @@ static grub_extcmd_t cmd;
GRUB_MOD_INIT(search)
{
cmd =
grub_register_extcmd ("search", grub_cmd_search, GRUB_COMMAND_FLAG_EXTRACTOR,
grub_register_extcmd ("search", grub_cmd_search,
GRUB_COMMAND_FLAG_EXTRACTOR | GRUB_COMMAND_ACCEPT_DASH,
N_("[-f|-l|-u|-s|-n] [--hint HINT [--hint HINT] ...]"
" NAME"),
N_("Search devices by file, filesystem label"

View file

@ -723,6 +723,8 @@ GRUB_MOD_INIT (tar)
GRUB_MOD_INIT (odc)
#elif defined (MODE_NEWC)
GRUB_MOD_INIT (newc)
#elif defined (MODE_BIGENDIAN)
GRUB_MOD_INIT (cpio_be)
#else
GRUB_MOD_INIT (cpio)
#endif
@ -737,6 +739,8 @@ GRUB_MOD_FINI (tar)
GRUB_MOD_FINI (odc)
#elif defined (MODE_NEWC)
GRUB_MOD_FINI (newc)
#elif defined (MODE_BIGENDIAN)
GRUB_MOD_FINI (cpio_be)
#else
GRUB_MOD_FINI (cpio)
#endif

View file

@ -137,6 +137,7 @@ struct
{
char *drive;
char *device;
int device_map;
} map[256];
struct grub_util_biosdisk_data
@ -145,6 +146,7 @@ struct grub_util_biosdisk_data
int access_mode;
int fd;
int is_disk;
int device_map;
};
#ifdef __linux__
@ -364,6 +366,7 @@ grub_util_biosdisk_open (const char *name, grub_disk_t disk)
data->access_mode = 0;
data->fd = -1;
data->is_disk = 0;
data->device_map = map[drive].device_map;
/* Get the size. */
#if defined(__MINGW32__)
@ -1120,6 +1123,12 @@ read_device_map (const char *dev_map)
grub_util_error ("%s:%d: %s", dev_map, lineno, msg);
}
if (dev_map[0] == '\0')
{
grub_util_info (_("no device.map"));
return;
}
fp = fopen (dev_map, "r");
if (! fp)
{
@ -1160,6 +1169,7 @@ read_device_map (const char *dev_map)
map[drive].drive = xmalloc (p - e + sizeof ('\0'));
strncpy (map[drive].drive, e, p - e + sizeof ('\0'));
map[drive].drive[p - e] = '\0';
map[drive].device_map = 1;
p++;
/* Skip leading spaces. */
@ -1760,7 +1770,10 @@ find_system_device (const char *os_dev, struct stat *st, int convert, int add)
grub_util_error (_("device count exceeds limit"));
map[i].device = os_disk;
map[i].drive = xstrdup (os_disk);
map[i].drive = xmalloc (sizeof ("hostdisk/") + strlen (os_disk));
strcpy (map[i].drive, "hostdisk/");
strcpy (map[i].drive + sizeof ("hostdisk/") - 1, os_disk);
map[i].device_map = 0;
return i;
}
@ -1927,6 +1940,7 @@ grub_util_biosdisk_get_grub_dev (const char *os_dev)
name = grub_xasprintf ("%s,%s", disk->name, partname);
free (partname);
grub_disk_close (disk);
return name;
}
@ -1963,6 +1977,14 @@ grub_util_biosdisk_get_grub_dev (const char *os_dev)
#endif
}
const char *
grub_util_biosdisk_get_compatibility_hint (grub_disk_t disk)
{
if (disk->dev != &grub_util_biosdisk_dev || map[disk->id].device_map)
return disk->name;
return 0;
}
const char *
grub_util_biosdisk_get_osdev (grub_disk_t disk)
{

View file

@ -25,14 +25,11 @@
#include <grub/i18n.h>
/* Built-in parser for default options. */
#define SHORT_ARG_HELP -100
#define SHORT_ARG_USAGE -101
static const struct grub_arg_option help_options[] =
{
{"help", SHORT_ARG_HELP, 0,
{"help", 0, 0,
N_("Display this help and exit."), 0, ARG_TYPE_NONE},
{"usage", SHORT_ARG_USAGE, 0,
{"usage", 0, 0,
N_("Display the usage of this command and exit."), 0, ARG_TYPE_NONE},
{0, 0, 0, 0, 0, 0}
};
@ -125,9 +122,9 @@ grub_arg_show_help (grub_extcmd_t cmd)
if (opt->shortarg && grub_isgraph (opt->shortarg))
grub_printf ("-%c%c ", opt->shortarg, opt->longarg ? ',':' ');
else if (opt->shortarg == SHORT_ARG_HELP && ! h_is_used)
else if (opt == help_options && ! h_is_used)
grub_printf ("-h, ");
else if (opt->shortarg == SHORT_ARG_USAGE && ! u_is_used)
else if (opt == help_options + 1 && ! u_is_used)
grub_printf ("-u, ");
else
grub_printf (" ");
@ -180,50 +177,34 @@ grub_arg_show_help (grub_extcmd_t cmd)
static int
parse_option (grub_extcmd_t cmd, int key, char *arg, struct grub_arg_list *usr)
parse_option (grub_extcmd_t cmd, const struct grub_arg_option *opt,
char *arg, struct grub_arg_list *usr)
{
switch (key)
if (opt == help_options)
{
case SHORT_ARG_HELP:
grub_arg_show_help (cmd);
return -1;
}
case SHORT_ARG_USAGE:
if (opt == help_options + 1)
{
show_usage (cmd);
return -1;
default:
{
int found = -1;
int i = 0;
const struct grub_arg_option *opt = cmd->options;
while (opt->doc)
{
if (opt->shortarg && key == opt->shortarg)
{
found = i;
break;
}
opt++;
i++;
}
if (found == -1)
return -1;
if (opt->flags & GRUB_ARG_OPTION_REPEATABLE)
{
usr[found].args[usr[found].set++] = arg;
usr[found].args[usr[found].set] = NULL;
}
else
{
usr[found].set = 1;
usr[found].arg = arg;
}
}
}
{
int found = opt - cmd->options;
if (opt->flags & GRUB_ARG_OPTION_REPEATABLE)
{
usr[found].args[usr[found].set++] = arg;
usr[found].args[usr[found].set] = NULL;
}
else
{
usr[found].set = 1;
usr[found].arg = arg;
}
}
return 0;
}
@ -307,7 +288,7 @@ grub_arg_parse (grub_extcmd_t cmd, int argc, char **argv,
it can have an argument value. */
if (*curshort)
{
if (parse_option (cmd, opt->shortarg, 0, usr) || grub_errno)
if (parse_option (cmd, opt, 0, usr) || grub_errno)
goto fail;
}
else
@ -411,7 +392,7 @@ grub_arg_parse (grub_extcmd_t cmd, int argc, char **argv,
/* XXX: Not implemented. */
break;
}
if (parse_option (cmd, opt->shortarg, option, usr) || grub_errno)
if (parse_option (cmd, opt, option, usr) || grub_errno)
goto fail;
}
else
@ -424,7 +405,7 @@ grub_arg_parse (grub_extcmd_t cmd, int argc, char **argv,
goto fail;
}
if (parse_option (cmd, opt->shortarg, 0, usr) || grub_errno)
if (parse_option (cmd, opt, 0, usr) || grub_errno)
goto fail;
}
}

View file

@ -473,7 +473,7 @@ static grub_command_t cmd_clear;
static void (*grub_xputs_saved) (const char *str);
static const char *features[] = {
"feature_chainloader_bpb", "feature_ntldr"
"feature_chainloader_bpb", "feature_ntldr", "feature_platform_search_hint"
};
GRUB_MOD_INIT(normal)