2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 2009 Free Software Foundation, Inc.
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.
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.
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/>.
19 #include <grub/types.h>
20 #include <grub/misc.h>
24 #include <grub/device.h>
25 #include <grub/disk.h>
26 #include <grub/partition.h>
29 #include <grub/file.h>
30 #include <grub/misc.h>
32 #include <grub/extcmd.h>
33 #include <grub/i18n.h>
35 GRUB_MOD_LICENSE ("GPLv3+");
37 static const struct grub_arg_option options
[] =
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
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},
52 grub_cmd_probe (grub_extcmd_context_t ctxt
, int argc
, char **args
)
54 struct grub_arg_list
*state
= ctxt
->state
;
61 return grub_error (GRUB_ERR_BAD_ARGUMENT
, "device name required");
63 ptr
= args
[0] + grub_strlen (args
[0]) - 1;
64 if (args
[0][0] == '(' && *ptr
== ')')
67 dev
= grub_device_open (args
[0] + 1);
71 dev
= grub_device_open (args
[0]);
77 const char *val
= "none";
79 val
= dev
->net
->protocol
->name
;
81 val
= dev
->disk
->dev
->name
;
83 grub_env_set (state
[0].arg
, val
);
85 grub_printf ("%s", val
);
86 grub_device_close (dev
);
91 const char *val
= "none";
92 if (dev
->disk
&& dev
->disk
->partition
)
93 val
= dev
->disk
->partition
->partmap
->name
;
95 grub_env_set (state
[0].arg
, val
);
97 grub_printf ("%s", val
);
98 grub_device_close (dev
);
101 fs
= grub_fs_probe (dev
);
107 grub_env_set (state
[0].arg
, fs
->name
);
109 grub_printf ("%s", fs
->name
);
110 grub_device_close (dev
);
111 return GRUB_ERR_NONE
;
117 return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET
,
118 N_("%s does not support UUIDs"), fs
->name
);
119 err
= fs
->uuid (dev
, &uuid
);
123 return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET
,
124 N_("%s does not support UUIDs"), fs
->name
);
127 grub_env_set (state
[0].arg
, uuid
);
129 grub_printf ("%s", uuid
);
131 grub_device_close (dev
);
132 return GRUB_ERR_NONE
;
138 return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET
,
139 N_("filesystem `%s' does not support labels"),
141 err
= fs
->label (dev
, &label
);
145 return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET
,
146 N_("filesystem `%s' does not support labels"),
150 grub_env_set (state
[0].arg
, label
);
152 grub_printf ("%s", label
);
154 grub_device_close (dev
);
155 return GRUB_ERR_NONE
;
157 grub_device_close (dev
);
158 return grub_error (GRUB_ERR_BAD_ARGUMENT
, "unrecognised target");
161 static grub_extcmd_t cmd
;
163 GRUB_MOD_INIT (probe
)
165 cmd
= grub_register_extcmd ("probe", grub_cmd_probe
, 0, N_("DEVICE"),
166 N_("Retrieve device info."), options
);
169 GRUB_MOD_FINI (probe
)
171 grub_unregister_extcmd (cmd
);