]> git.proxmox.com Git - grub2.git/blob - grub-core/commands/probe.c
tpm: Fix bug in GRUB2 TPM module
[grub2.git] / grub-core / commands / probe.c
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>
33 #include <grub/i18n.h>
34
35 GRUB_MOD_LICENSE ("GPLv3+");
36
37 static const struct grub_arg_option options[] =
38 {
39 {"set", 's', 0,
40 N_("Set a variable to return value."), N_("VARNAME"), ARG_TYPE_STRING},
41 /* TRANSLATORS: It's a driver that is currently in use to access
42 the diven disk. */
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},
48 {0, 0, 0, 0, 0, 0}
49 };
50
51 static grub_err_t
52 grub_cmd_probe (grub_extcmd_context_t ctxt, int argc, char **args)
53 {
54 struct grub_arg_list *state = ctxt->state;
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)
73 return grub_errno;
74
75 if (state[1].set)
76 {
77 const char *val = "none";
78 if (dev->net)
79 val = dev->net->protocol->name;
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 grub_device_close (dev);
87 return GRUB_ERR_NONE;
88 }
89 if (state[2].set)
90 {
91 const char *val = "none";
92 if (dev->disk && dev->disk->partition)
93 val = dev->disk->partition->partmap->name;
94 if (state[0].set)
95 grub_env_set (state[0].arg, val);
96 else
97 grub_printf ("%s", val);
98 grub_device_close (dev);
99 return GRUB_ERR_NONE;
100 }
101 fs = grub_fs_probe (dev);
102 if (! fs)
103 return grub_errno;
104 if (state[3].set)
105 {
106 if (state[0].set)
107 grub_env_set (state[0].arg, fs->name);
108 else
109 grub_printf ("%s", fs->name);
110 grub_device_close (dev);
111 return GRUB_ERR_NONE;
112 }
113 if (state[4].set)
114 {
115 char *uuid;
116 if (! fs->uuid)
117 return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
118 N_("%s does not support UUIDs"), fs->name);
119 err = fs->uuid (dev, &uuid);
120 if (err)
121 return err;
122 if (! uuid)
123 return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
124 N_("%s does not support UUIDs"), fs->name);
125
126 if (state[0].set)
127 grub_env_set (state[0].arg, uuid);
128 else
129 grub_printf ("%s", uuid);
130 grub_free (uuid);
131 grub_device_close (dev);
132 return GRUB_ERR_NONE;
133 }
134 if (state[5].set)
135 {
136 char *label;
137 if (! fs->label)
138 return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
139 N_("filesystem `%s' does not support labels"),
140 fs->name);
141 err = fs->label (dev, &label);
142 if (err)
143 return err;
144 if (! label)
145 return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
146 N_("filesystem `%s' does not support labels"),
147 fs->name);
148
149 if (state[0].set)
150 grub_env_set (state[0].arg, label);
151 else
152 grub_printf ("%s", label);
153 grub_free (label);
154 grub_device_close (dev);
155 return GRUB_ERR_NONE;
156 }
157 grub_device_close (dev);
158 return grub_error (GRUB_ERR_BAD_ARGUMENT, "unrecognised target");
159 }
160
161 static grub_extcmd_t cmd;
162
163 GRUB_MOD_INIT (probe)
164 {
165 cmd = grub_register_extcmd ("probe", grub_cmd_probe, 0, N_("DEVICE"),
166 N_("Retrieve device info."), options);
167 }
168
169 GRUB_MOD_FINI (probe)
170 {
171 grub_unregister_extcmd (cmd);
172 }