2N/A/*
2N/A * GRUB -- GRand Unified Bootloader
2N/A * Copyright (C) 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/device.h>
2N/A#include <grub/disk.h>
2N/A#include <grub/partition.h>
2N/A#include <grub/net.h>
2N/A#include <grub/fs.h>
2N/A#include <grub/file.h>
2N/A#include <grub/misc.h>
2N/A#include <grub/env.h>
2N/A#include <grub/extcmd.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 {"set", 's', 0,
2N/A N_("Set a variable to return value."), "VAR", ARG_TYPE_STRING},
2N/A {"driver", 'd', 0, N_("Determine driver."), 0, 0},
2N/A {"partmap", 'p', 0, N_("Determine partition map type."), 0, 0},
2N/A {"fs", 'f', 0, N_("Determine filesystem type."), 0, 0},
2N/A {"fs-uuid", 'u', 0, N_("Determine filesystem UUID."), 0, 0},
2N/A {"label", 'l', 0, N_("Determine filesystem label."), 0, 0},
2N/A {0, 0, 0, 0, 0, 0}
2N/A };
2N/A
2N/Astatic grub_err_t
2N/Agrub_cmd_probe (grub_extcmd_context_t ctxt, int argc, char **args)
2N/A{
2N/A struct grub_arg_list *state = ctxt->state;
2N/A grub_device_t dev;
2N/A grub_fs_t fs;
2N/A char *ptr;
2N/A grub_err_t err;
2N/A
2N/A if (argc < 1)
2N/A return grub_error (GRUB_ERR_BAD_ARGUMENT, "device name required");
2N/A
2N/A ptr = args[0] + grub_strlen (args[0]) - 1;
2N/A if (args[0][0] == '(' && *ptr == ')')
2N/A {
2N/A *ptr = 0;
2N/A dev = grub_device_open (args[0] + 1);
2N/A *ptr = ')';
2N/A }
2N/A else
2N/A dev = grub_device_open (args[0]);
2N/A if (! dev)
2N/A return grub_error (GRUB_ERR_BAD_DEVICE, "couldn't open device");
2N/A
2N/A if (state[1].set)
2N/A {
2N/A const char *val = "none";
2N/A if (dev->net)
2N/A val = dev->net->protocol->name;
2N/A if (dev->disk)
2N/A val = dev->disk->dev->name;
2N/A if (state[0].set)
2N/A grub_env_set (state[0].arg, val);
2N/A else
2N/A grub_printf ("%s", val);
2N/A return GRUB_ERR_NONE;
2N/A }
2N/A if (state[2].set)
2N/A {
2N/A const char *val = "none";
2N/A if (dev->disk && dev->disk->partition)
2N/A val = dev->disk->partition->partmap->name;
2N/A if (state[0].set)
2N/A grub_env_set (state[0].arg, val);
2N/A else
2N/A grub_printf ("%s", val);
2N/A return GRUB_ERR_NONE;
2N/A }
2N/A fs = grub_fs_probe (dev);
2N/A if (! fs)
2N/A return grub_error (GRUB_ERR_UNKNOWN_FS, "unrecognised fs");
2N/A if (state[3].set)
2N/A {
2N/A if (state[0].set)
2N/A grub_env_set (state[0].arg, fs->name);
2N/A else
2N/A grub_printf ("%s", fs->name);
2N/A return GRUB_ERR_NONE;
2N/A }
2N/A if (state[4].set)
2N/A {
2N/A char *uuid;
2N/A if (! fs->uuid)
2N/A return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
2N/A "uuid for this FS isn't supported yet");
2N/A err = fs->uuid (dev, &uuid);
2N/A if (err)
2N/A return err;
2N/A if (! uuid)
2N/A return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
2N/A "uuid for this FS isn't supported yet");
2N/A
2N/A if (state[0].set)
2N/A grub_env_set (state[0].arg, uuid);
2N/A else
2N/A grub_printf ("%s", uuid);
2N/A grub_free (uuid);
2N/A return GRUB_ERR_NONE;
2N/A }
2N/A if (state[5].set)
2N/A {
2N/A char *label;
2N/A if (! fs->label)
2N/A return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
2N/A "label for this FS isn't supported yet");
2N/A err = fs->label (dev, &label);
2N/A if (err)
2N/A return err;
2N/A if (! label)
2N/A return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
2N/A "label for this FS isn't supported yet");
2N/A
2N/A if (state[0].set)
2N/A grub_env_set (state[0].arg, label);
2N/A else
2N/A grub_printf ("%s", label);
2N/A grub_free (label);
2N/A return GRUB_ERR_NONE;
2N/A }
2N/A return grub_error (GRUB_ERR_BAD_ARGUMENT, "unrecognised target");
2N/A}
2N/A
2N/Astatic grub_extcmd_t cmd;
2N/A
2N/AGRUB_MOD_INIT (probe)
2N/A{
2N/A cmd = grub_register_extcmd ("probe", grub_cmd_probe, 0, N_("DEVICE"),
2N/A N_("Retrieve device info."), options);
2N/A}
2N/A
2N/AGRUB_MOD_FINI (probe)
2N/A{
2N/A grub_unregister_extcmd (cmd);
2N/A}