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 *
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/env.h>
2N/A#include <grub/extcmd.h>
2N/A#include <grub/search.h>
2N/A#include <grub/i18n.h>
2N/A
2N/AGRUB_MOD_LICENSE ("GPLv3+");
2N/A
2N/Astatic const struct grub_arg_option options[] =
2N/A {
2N/A {"file", 'f', 0, N_("Search devices by a file."), 0, 0},
2N/A {"label", 'l', 0, N_("Search devices by a filesystem label."),
2N/A 0, 0},
2N/A {"fs-uuid", 'u', 0, N_("Search devices by a filesystem UUID."),
2N/A 0, 0},
2N/A {"set", 's', GRUB_ARG_OPTION_OPTIONAL,
2N/A N_("Set a variable to the first device found."), "VAR", ARG_TYPE_STRING},
2N/A {"no-floppy", 'n', 0, N_("Do not probe any floppy drive."), 0, 0},
2N/A {"hint", 'h', GRUB_ARG_OPTION_REPEATABLE,
2N/A N_("First try the device HINT. If HINT ends in comma, "
2N/A "also try subpartitions"), N_("HINT"), ARG_TYPE_STRING},
2N/A {"hint-ieee1275", 0, GRUB_ARG_OPTION_REPEATABLE,
2N/A N_("First try the device HINT if on IEEE1275. If HINT ends in comma, "
2N/A "also try subpartitions"), N_("HINT"), ARG_TYPE_STRING},
2N/A {"hint-bios", 0, GRUB_ARG_OPTION_REPEATABLE,
2N/A N_("First try the device HINT if on BIOS. If HINT ends in comma, "
2N/A "also try subpartitions"), N_("HINT"), ARG_TYPE_STRING},
2N/A {"hint-baremetal", 0, GRUB_ARG_OPTION_REPEATABLE,
2N/A N_("First try the device HINT. If HINT ends in comma, "
2N/A "also try subpartitions"), N_("HINT"), ARG_TYPE_STRING},
2N/A {"hint-efi", 0, GRUB_ARG_OPTION_REPEATABLE,
2N/A N_("First try the device HINT if on EFI. If HINT ends in comma, "
2N/A "also try subpartitions"), N_("HINT"), ARG_TYPE_STRING},
2N/A {"hint-arc", 0, GRUB_ARG_OPTION_REPEATABLE,
2N/A N_("First try the device HINT if on ARC. If HINT ends in comma, "
2N/A "also try subpartitions"), N_("HINT"), ARG_TYPE_STRING},
2N/A {0, 0, 0, 0, 0, 0}
2N/A };
2N/A
2N/Aenum options
2N/A {
2N/A SEARCH_FILE,
2N/A SEARCH_LABEL,
2N/A SEARCH_FS_UUID,
2N/A SEARCH_SET,
2N/A SEARCH_NO_FLOPPY,
2N/A SEARCH_HINT,
2N/A SEARCH_HINT_IEEE1275,
2N/A SEARCH_HINT_BIOS,
2N/A SEARCH_HINT_BAREMETAL,
2N/A SEARCH_HINT_EFI,
2N/A SEARCH_HINT_ARC,
2N/A };
2N/A
2N/Astatic grub_err_t
2N/Agrub_cmd_search (grub_extcmd_context_t ctxt, int argc, char **args)
2N/A{
2N/A struct grub_arg_list *state = ctxt->state;
2N/A const char *var = 0;
2N/A int i = 0, j = 0, nhints = 0;
2N/A char **hints = NULL;
2N/A
2N/A if (state[SEARCH_HINT].set)
2N/A for (i = 0; state[SEARCH_HINT].args[i]; i++)
2N/A nhints++;
2N/A
2N/A#ifdef GRUB_MACHINE_IEEE1275
2N/A if (state[SEARCH_HINT_IEEE1275].set)
2N/A for (i = 0; state[SEARCH_HINT_IEEE1275].args[i]; i++)
2N/A nhints++;
2N/A#endif
2N/A
2N/A#ifdef GRUB_MACHINE_EFI
2N/A if (state[SEARCH_HINT_EFI].set)
2N/A for (i = 0; state[SEARCH_HINT_EFI].args[i]; i++)
2N/A nhints++;
2N/A#endif
2N/A
2N/A#ifdef GRUB_MACHINE_PCBIOS
2N/A if (state[SEARCH_HINT_BIOS].set)
2N/A for (i = 0; state[SEARCH_HINT_BIOS].args[i]; i++)
2N/A nhints++;
2N/A#endif
2N/A
2N/A#ifdef GRUB_MACHINE_ARC
2N/A if (state[SEARCH_HINT_ARC].set)
2N/A for (i = 0; state[SEARCH_HINT_ARC].args[i]; i++)
2N/A nhints++;
2N/A#endif
2N/A
2N/A if (state[SEARCH_HINT_BAREMETAL].set)
2N/A for (i = 0; state[SEARCH_HINT_BAREMETAL].args[i]; i++)
2N/A nhints++;
2N/A
2N/A hints = grub_malloc (sizeof (hints[0]) * nhints);
2N/A if (!hints)
2N/A return grub_errno;
2N/A j = 0;
2N/A
2N/A if (state[SEARCH_HINT].set)
2N/A for (i = 0; state[SEARCH_HINT].args[i]; i++)
2N/A hints[j++] = state[SEARCH_HINT].args[i];
2N/A
2N/A#ifdef GRUB_MACHINE_IEEE1275
2N/A if (state[SEARCH_HINT_IEEE1275].set)
2N/A for (i = 0; state[SEARCH_HINT_IEEE1275].args[i]; i++)
2N/A hints[j++] = state[SEARCH_HINT_IEEE1275].args[i];
2N/A#endif
2N/A
2N/A#ifdef GRUB_MACHINE_EFI
2N/A if (state[SEARCH_HINT_EFI].set)
2N/A for (i = 0; state[SEARCH_HINT_EFI].args[i]; i++)
2N/A hints[j++] = state[SEARCH_HINT_EFI].args[i];
2N/A#endif
2N/A
2N/A#ifdef GRUB_MACHINE_ARC
2N/A if (state[SEARCH_HINT_ARC].set)
2N/A for (i = 0; state[SEARCH_HINT_ARC].args[i]; i++)
2N/A hints[j++] = state[SEARCH_HINT_ARC].args[i];
2N/A#endif
2N/A
2N/A#ifdef GRUB_MACHINE_PCBIOS
2N/A if (state[SEARCH_HINT_BIOS].set)
2N/A for (i = 0; state[SEARCH_HINT_BIOS].args[i]; i++)
2N/A hints[j++] = state[SEARCH_HINT_BIOS].args[i];
2N/A#endif
2N/A
2N/A if (state[SEARCH_HINT_BAREMETAL].set)
2N/A for (i = 0; state[SEARCH_HINT_BAREMETAL].args[i]; i++)
2N/A hints[j++] = state[SEARCH_HINT_BAREMETAL].args[i];
2N/A
2N/A /* Skip hints for future platforms. */
2N/A for (j = 0; j < argc; j++)
2N/A if (grub_memcmp (args[j], "--hint-", sizeof ("--hint-") - 1) != 0)
2N/A break;
2N/A
2N/A if (argc == j)
2N/A return grub_error (GRUB_ERR_BAD_ARGUMENT, "no argument specified");
2N/A
2N/A if (state[SEARCH_SET].set)
2N/A var = state[SEARCH_SET].arg ? state[SEARCH_SET].arg : "root";
2N/A
2N/A if (state[SEARCH_LABEL].set)
2N/A grub_search_label (args[j], var, state[SEARCH_NO_FLOPPY].set,
2N/A hints, nhints);
2N/A else if (state[SEARCH_FS_UUID].set)
2N/A grub_search_fs_uuid (args[j], var, state[SEARCH_NO_FLOPPY].set,
2N/A hints, nhints);
2N/A else if (state[SEARCH_FILE].set)
2N/A grub_search_fs_file (args[j], var, state[SEARCH_NO_FLOPPY].set,
2N/A hints, nhints);
2N/A else
2N/A return grub_error (GRUB_ERR_INVALID_COMMAND, "unspecified search type");
2N/A
2N/A return grub_errno;
2N/A}
2N/A
2N/Astatic grub_extcmd_t cmd;
2N/A
2N/AGRUB_MOD_INIT(search)
2N/A{
2N/A cmd =
2N/A grub_register_extcmd ("search", grub_cmd_search,
2N/A GRUB_COMMAND_FLAG_EXTRACTOR | GRUB_COMMAND_ACCEPT_DASH,
2N/A N_("[-f|-l|-u|-s|-n] [--hint HINT [--hint HINT] ...]"
2N/A " NAME"),
2N/A N_("Search devices by file, filesystem label"
2N/A " or filesystem UUID."
2N/A " If --set is specified, the first device found is"
2N/A " set to a variable. If no variable name is"
2N/A " specified, \"root\" is used."),
2N/A options);
2N/A}
2N/A
2N/AGRUB_MOD_FINI(search)
2N/A{
2N/A grub_unregister_extcmd (cmd);
2N/A}