]> git.proxmox.com Git - grub2.git/blame - grub-core/commands/probe.c
* grub-core/commands/ls.c (grub_ls_list_devices): Disable
[grub2.git] / grub-core / commands / probe.c
CommitLineData
9c6f4596 1/*
2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 2009 Free Software Foundation, Inc.
4 *
5 * GRUB is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * GRUB is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#include <grub/types.h>
20#include <grub/misc.h>
21#include <grub/mm.h>
22#include <grub/err.h>
23#include <grub/dl.h>
24#include <grub/device.h>
25#include <grub/disk.h>
26#include <grub/partition.h>
27#include <grub/net.h>
28#include <grub/fs.h>
29#include <grub/file.h>
30#include <grub/misc.h>
31#include <grub/env.h>
32#include <grub/extcmd.h>
77a79592 33#include <grub/i18n.h>
9c6f4596 34
e745cf0c
VS
35GRUB_MOD_LICENSE ("GPLv3+");
36
a2c1332b 37static const struct grub_arg_option options[] =
9c6f4596 38 {
34c09785 39 {"set", 's', 0,
292fdaff 40 N_("Set a variable to return value."), N_("VARNAME"), ARG_TYPE_STRING},
40211ab8
VS
41 /* TRANSLATORS: It's a driver that is currently in use to access
42 the diven disk. */
77a79592 43 {"driver", 'd', 0, N_("Determine driver."), 0, 0},
44 {"partmap", 'p', 0, N_("Determine partition map type."), 0, 0},
45 {"fs", 'f', 0, N_("Determine filesystem type."), 0, 0},
46 {"fs-uuid", 'u', 0, N_("Determine filesystem UUID."), 0, 0},
47 {"label", 'l', 0, N_("Determine filesystem label."), 0, 0},
9c6f4596 48 {0, 0, 0, 0, 0, 0}
49 };
50
51static grub_err_t
28be0e94 52grub_cmd_probe (grub_extcmd_context_t ctxt, int argc, char **args)
9c6f4596 53{
28be0e94 54 struct grub_arg_list *state = ctxt->state;
9c6f4596 55 grub_device_t dev;
56 grub_fs_t fs;
57 char *ptr;
58 grub_err_t err;
59
60 if (argc < 1)
61 return grub_error (GRUB_ERR_BAD_ARGUMENT, "device name required");
62
63 ptr = args[0] + grub_strlen (args[0]) - 1;
64 if (args[0][0] == '(' && *ptr == ')')
65 {
66 *ptr = 0;
67 dev = grub_device_open (args[0] + 1);
68 *ptr = ')';
69 }
70 else
71 dev = grub_device_open (args[0]);
72 if (! dev)
9c4b5c13 73 return grub_errno;
9c6f4596 74
75 if (state[1].set)
76 {
77 const char *val = "none";
78 if (dev->net)
80ca2505 79 val = dev->net->protocol->name;
9c6f4596 80 if (dev->disk)
81 val = dev->disk->dev->name;
82 if (state[0].set)
83 grub_env_set (state[0].arg, val);
84 else
85 grub_printf ("%s", val);
86 return GRUB_ERR_NONE;
87 }
88 if (state[2].set)
89 {
90 const char *val = "none";
91 if (dev->disk && dev->disk->partition)
92 val = dev->disk->partition->partmap->name;
93 if (state[0].set)
94 grub_env_set (state[0].arg, val);
95 else
96 grub_printf ("%s", val);
97 return GRUB_ERR_NONE;
98 }
99 fs = grub_fs_probe (dev);
100 if (! fs)
9c4b5c13 101 return grub_errno;
9c6f4596 102 if (state[3].set)
103 {
104 if (state[0].set)
105 grub_env_set (state[0].arg, fs->name);
106 else
107 grub_printf ("%s", fs->name);
108 return GRUB_ERR_NONE;
109 }
110 if (state[4].set)
111 {
112 char *uuid;
113 if (! fs->uuid)
114 return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
47b40053 115 N_("%s does not support UUIDs"), fs->name);
9c6f4596 116 err = fs->uuid (dev, &uuid);
117 if (err)
118 return err;
119 if (! uuid)
120 return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
47b40053 121 N_("%s does not support UUIDs"), fs->name);
9c6f4596 122
123 if (state[0].set)
124 grub_env_set (state[0].arg, uuid);
125 else
126 grub_printf ("%s", uuid);
127 grub_free (uuid);
128 return GRUB_ERR_NONE;
129 }
130 if (state[5].set)
131 {
132 char *label;
133 if (! fs->label)
134 return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
47b40053
VS
135 N_("filesystem `%s' does not support labels"),
136 fs->name);
9c6f4596 137 err = fs->label (dev, &label);
138 if (err)
139 return err;
140 if (! label)
141 return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
47b40053
VS
142 N_("filesystem `%s' does not support labels"),
143 fs->name);
9c6f4596 144
145 if (state[0].set)
146 grub_env_set (state[0].arg, label);
147 else
148 grub_printf ("%s", label);
149 grub_free (label);
150 return GRUB_ERR_NONE;
151 }
152 return grub_error (GRUB_ERR_BAD_ARGUMENT, "unrecognised target");
153}
154
155static grub_extcmd_t cmd;
156
157GRUB_MOD_INIT (probe)
158{
34c09785 159 cmd = grub_register_extcmd ("probe", grub_cmd_probe, 0, N_("DEVICE"),
77a79592 160 N_("Retrieve device info."), options);
9c6f4596 161}
162
163GRUB_MOD_FINI (probe)
164{
165 grub_unregister_extcmd (cmd);
166}