2N/A/* search.c - search devices based on a file or a filesystem label */
2N/A/*
2N/A * GRUB -- GRand Unified Bootloader
2N/A * Copyright (C) 2005,2007,2008,2009 Free Software Foundation, Inc.
2N/A * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
2N/A *
2N/A * GRUB is free software: you can redistribute it and/or modify
2N/A * it under the terms of the GNU General Public License as published by
2N/A * the Free Software Foundation, either version 3 of the License, or
2N/A * (at your option) any later version.
2N/A *
2N/A * GRUB is distributed in the hope that it will be useful,
2N/A * but WITHOUT ANY WARRANTY; without even the implied warranty of
2N/A * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2N/A * GNU General Public License for more details.
2N/A *
2N/A * You should have received a copy of the GNU General Public License
2N/A * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
2N/A */
2N/A
2N/A#include <grub/types.h>
2N/A#include <grub/misc.h>
2N/A#include <grub/mm.h>
2N/A#include <grub/err.h>
2N/A#include <grub/dl.h>
2N/A#include <grub/device.h>
2N/A#include <grub/file.h>
2N/A#include <grub/env.h>
2N/A#include <grub/command.h>
2N/A#include <grub/search.h>
2N/A#include <grub/i18n.h>
2N/A#include <grub/disk.h>
2N/A#include <grub/partition.h>
2N/A
2N/AGRUB_MOD_LICENSE ("GPLv3+");
2N/A
2N/Astruct cache_entry
2N/A{
2N/A struct cache_entry *next;
2N/A char *key;
2N/A char *value;
2N/A};
2N/A
2N/Astatic struct cache_entry *cache;
2N/A
2N/Avoid
2N/AFUNC_NAME (const char *key, const char *var, int no_floppy,
2N/A char **hints, unsigned nhints)
2N/A{
2N/A int count = 0;
2N/A int is_cache = 0;
2N/A grub_fs_autoload_hook_t saved_autoload;
2N/A
2N/A auto int iterate_device (const char *name);
2N/A int iterate_device (const char *name)
2N/A {
2N/A int found = 0;
2N/A
2N/A /* Skip floppy drives when requested. */
2N/A if (no_floppy &&
2N/A name[0] == 'f' && name[1] == 'd' && name[2] >= '0' && name[2] <= '9')
2N/A return 0;
2N/A
2N/A#ifdef DO_SEARCH_FS_UUID
2N/A#define compare_fn grub_strcasecmp
2N/A#else
2N/A#define compare_fn grub_strcmp
2N/A#endif
2N/A
2N/A#ifdef DO_SEARCH_FILE
2N/A {
2N/A char *buf;
2N/A const char *tempkey;
2N/A grub_file_t file;
2N/A
2N/A grub_dprintf("search", "key = %s\n", key);
2N/A
2N/A if (*key == '(' && (tempkey = grub_strchr(key, ')')))
2N/A tempkey++;
2N/A else
2N/A tempkey = key;
2N/A
2N/A buf = grub_xasprintf ("(%s)%s", name, tempkey);
2N/A if (! buf)
2N/A return 1;
2N/A
2N/A grub_file_filter_disable_compression ();
2N/A file = grub_file_open (buf);
2N/A if (file)
2N/A {
2N/A found = 1;
2N/A grub_file_close (file);
2N/A }
2N/A grub_free (buf);
2N/A }
2N/A#else
2N/A {
2N/A /* SEARCH_FS_UUID or SEARCH_LABEL */
2N/A grub_device_t dev;
2N/A grub_fs_t fs;
2N/A char *quid;
2N/A
2N/A dev = grub_device_open (name);
2N/A if (dev)
2N/A {
2N/A fs = grub_fs_probe (dev);
2N/A
2N/A#ifdef DO_SEARCH_FS_UUID
2N/A#define read_fn uuid
2N/A#else
2N/A#define read_fn label
2N/A#endif
2N/A
2N/A if (fs && fs->read_fn)
2N/A {
2N/A fs->read_fn (dev, &quid);
2N/A
2N/A if (grub_errno == GRUB_ERR_NONE && quid)
2N/A {
2N/A if (compare_fn (quid, key) == 0)
2N/A found = 1;
2N/A
2N/A grub_free (quid);
2N/A }
2N/A }
2N/A
2N/A grub_device_close (dev);
2N/A }
2N/A }
2N/A#endif
2N/A
2N/A if (!is_cache && found && count == 0)
2N/A {
2N/A struct cache_entry *cache_ent;
2N/A cache_ent = grub_malloc (sizeof (*cache_ent));
2N/A if (cache_ent)
2N/A {
2N/A cache_ent->key = grub_strdup (key);
2N/A cache_ent->value = grub_strdup (name);
2N/A if (cache_ent->value && cache_ent->key)
2N/A {
2N/A cache_ent->next = cache;
2N/A cache = cache_ent;
2N/A }
2N/A else
2N/A {
2N/A grub_free (cache_ent->value);
2N/A grub_free (cache_ent->key);
2N/A grub_free (cache_ent);
2N/A grub_errno = GRUB_ERR_NONE;
2N/A }
2N/A }
2N/A else
2N/A grub_errno = GRUB_ERR_NONE;
2N/A }
2N/A
2N/A if (found)
2N/A {
2N/A count++;
2N/A if (var)
2N/A grub_env_set (var, name);
2N/A else
2N/A grub_printf (" %s", name);
2N/A }
2N/A
2N/A grub_errno = GRUB_ERR_NONE;
2N/A return (found && var);
2N/A }
2N/A
2N/A auto int part_hook (grub_disk_t disk, const grub_partition_t partition);
2N/A int part_hook (grub_disk_t disk, const grub_partition_t partition)
2N/A {
2N/A char *partition_name, *devname;
2N/A int ret;
2N/A
2N/A partition_name = grub_partition_get_name (partition);
2N/A if (! partition_name)
2N/A return 1;
2N/A
2N/A devname = grub_xasprintf ("%s,%s", disk->name, partition_name);
2N/A grub_free (partition_name);
2N/A if (!devname)
2N/A return 1;
2N/A ret = iterate_device (devname);
2N/A grub_free (devname);
2N/A
2N/A return ret;
2N/A }
2N/A
2N/A auto void try (void);
2N/A void try (void)
2N/A {
2N/A unsigned i;
2N/A struct cache_entry **prev;
2N/A struct cache_entry *cache_ent;
2N/A
2N/A for (prev = &cache, cache_ent = *prev; cache_ent;
2N/A prev = &cache_ent->next, cache_ent = *prev)
2N/A if (compare_fn (cache_ent->key, key) == 0)
2N/A break;
2N/A if (cache_ent)
2N/A {
2N/A is_cache = 1;
2N/A if (iterate_device (cache_ent->value))
2N/A {
2N/A is_cache = 0;
2N/A return;
2N/A }
2N/A is_cache = 0;
2N/A /* Cache entry was outdated. Remove it. */
2N/A if (!count)
2N/A {
2N/A grub_free (cache_ent->key);
2N/A grub_free (cache_ent->value);
2N/A grub_free (cache_ent);
2N/A *prev = cache_ent->next;
2N/A }
2N/A }
2N/A
2N/A for (i = 0; i < nhints; i++)
2N/A {
2N/A char *end;
2N/A if (!hints[i][0])
2N/A continue;
2N/A end = hints[i] + grub_strlen (hints[i]) - 1;
2N/A if (*end == ',')
2N/A *end = 0;
2N/A if (iterate_device (hints[i]))
2N/A {
2N/A if (!*end)
2N/A *end = ',';
2N/A return;
2N/A }
2N/A if (!*end)
2N/A {
2N/A grub_device_t dev;
2N/A int ret;
2N/A dev = grub_device_open (hints[i]);
2N/A if (!dev)
2N/A {
2N/A if (!*end)
2N/A *end = ',';
2N/A continue;
2N/A }
2N/A if (!dev->disk)
2N/A {
2N/A grub_device_close (dev);
2N/A if (!*end)
2N/A *end = ',';
2N/A continue;
2N/A }
2N/A ret = grub_partition_iterate (dev->disk, part_hook);
2N/A if (!*end)
2N/A *end = ',';
2N/A grub_device_close (dev);
2N/A if (ret)
2N/A return;
2N/A }
2N/A }
2N/A grub_device_iterate (iterate_device);
2N/A }
2N/A
2N/A /* First try without autoloading if we're setting variable. */
2N/A if (var)
2N/A {
2N/A saved_autoload = grub_fs_autoload_hook;
2N/A grub_fs_autoload_hook = 0;
2N/A try ();
2N/A
2N/A /* Restore autoload hook. */
2N/A grub_fs_autoload_hook = saved_autoload;
2N/A
2N/A /* Retry with autoload if nothing found. */
2N/A if (grub_errno == GRUB_ERR_NONE && count == 0)
2N/A try ();
2N/A }
2N/A else
2N/A try ();
2N/A
2N/A if (grub_errno == GRUB_ERR_NONE && count == 0)
2N/A grub_error (GRUB_ERR_FILE_NOT_FOUND, "no such device: %s", key);
2N/A}
2N/A
2N/Astatic grub_err_t
2N/Agrub_cmd_do_search (grub_command_t cmd __attribute__ ((unused)), int argc,
2N/A char **args)
2N/A{
2N/A if (argc == 0)
2N/A return grub_error (GRUB_ERR_BAD_ARGUMENT, "no argument specified");
2N/A
2N/A FUNC_NAME (args[0], argc == 1 ? 0 : args[1], 0, (args + 2),
2N/A argc > 2 ? argc - 2 : 0);
2N/A
2N/A return grub_errno;
2N/A}
2N/A
2N/Astatic grub_command_t cmd;
2N/A
2N/A#ifdef DO_SEARCH_FILE
2N/AGRUB_MOD_INIT(search_fs_file)
2N/A#elif defined (DO_SEARCH_FS_UUID)
2N/AGRUB_MOD_INIT(search_fs_uuid)
2N/A#else
2N/AGRUB_MOD_INIT(search_label)
2N/A#endif
2N/A{
2N/A cmd =
2N/A grub_register_command (COMMAND_NAME, grub_cmd_do_search,
2N/A N_("NAME [VARIABLE] [HINTS]"),
2N/A HELP_MESSAGE);
2N/A}
2N/A
2N/A#ifdef DO_SEARCH_FILE
2N/AGRUB_MOD_FINI(search_fs_file)
2N/A#elif defined (DO_SEARCH_FS_UUID)
2N/AGRUB_MOD_FINI(search_fs_uuid)
2N/A#else
2N/AGRUB_MOD_FINI(search_label)
2N/A#endif
2N/A{
2N/A grub_unregister_command (cmd);
2N/A}