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>
27 #include <grub/gpt_partition.h>
30 #include <grub/file.h>
31 #include <grub/misc.h>
33 #include <grub/extcmd.h>
34 #include <grub/i18n.h>
36 GRUB_MOD_LICENSE ("GPLv3+");
38 static const struct grub_arg_option options
[] =
41 N_("Set a variable to return value."), N_("VARNAME"), ARG_TYPE_STRING
},
42 /* TRANSLATORS: It's a driver that is currently in use to access
44 {"driver", 'd', 0, N_("Determine driver."), 0, 0},
45 {"partmap", 'p', 0, N_("Determine partition map type."), 0, 0},
46 {"fs", 'f', 0, N_("Determine filesystem type."), 0, 0},
47 {"fs-uuid", 'u', 0, N_("Determine filesystem UUID."), 0, 0},
48 {"label", 'l', 0, N_("Determine filesystem label."), 0, 0},
49 {"part-uuid", 0, 0, N_("Determine partition UUID."), 0, 0},
54 grub_cmd_probe (grub_extcmd_context_t ctxt
, int argc
, char **args
)
56 struct grub_arg_list
*state
= ctxt
->state
;
63 return grub_error (GRUB_ERR_BAD_ARGUMENT
, "device name required");
65 ptr
= args
[0] + grub_strlen (args
[0]) - 1;
66 if (args
[0][0] == '(' && *ptr
== ')')
69 dev
= grub_device_open (args
[0] + 1);
73 dev
= grub_device_open (args
[0]);
79 const char *val
= "none";
81 val
= dev
->net
->protocol
->name
;
83 val
= dev
->disk
->dev
->name
;
85 grub_env_set (state
[0].arg
, val
);
87 grub_printf ("%s", val
);
88 grub_device_close (dev
);
93 const char *val
= "none";
94 if (dev
->disk
&& dev
->disk
->partition
)
95 val
= dev
->disk
->partition
->partmap
->name
;
97 grub_env_set (state
[0].arg
, val
);
99 grub_printf ("%s", val
);
100 grub_device_close (dev
);
101 return GRUB_ERR_NONE
;
105 /* AAAABBBB-CCCC-DDDD-EEEE-FFFFFFFFFFFF + null terminator */
106 char val
[37] = "none";
107 if (dev
->disk
&& dev
->disk
->partition
&&
108 grub_strcmp(dev
->disk
->partition
->partmap
->name
, "gpt") == 0)
110 struct grub_gpt_partentry entry
;
111 struct grub_partition
*p
= dev
->disk
->partition
;
112 grub_disk_t disk
= grub_disk_open(dev
->disk
->name
);
115 if (grub_disk_read(disk
, p
->offset
, p
->index
, sizeof(entry
), &entry
))
117 grub_disk_close(disk
);
118 grub_gpt_part_guid_t
*guid
= &entry
.guid
;
119 grub_snprintf (val
, sizeof(val
),
120 "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
121 grub_le_to_cpu32 (guid
->data1
),
122 grub_le_to_cpu16 (guid
->data2
),
123 grub_le_to_cpu16 (guid
->data3
),
124 guid
->data4
[0], guid
->data4
[1], guid
->data4
[2],
125 guid
->data4
[3], guid
->data4
[4], guid
->data4
[5],
126 guid
->data4
[6], guid
->data4
[7]);
129 grub_env_set (state
[0].arg
, val
);
131 grub_printf ("%s", val
);
132 grub_device_close (dev
);
133 return GRUB_ERR_NONE
;
135 fs
= grub_fs_probe (dev
);
141 grub_env_set (state
[0].arg
, fs
->name
);
143 grub_printf ("%s", fs
->name
);
144 grub_device_close (dev
);
145 return GRUB_ERR_NONE
;
151 return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET
,
152 N_("%s does not support UUIDs"), fs
->name
);
153 err
= fs
->fs_uuid (dev
, &uuid
);
157 return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET
,
158 N_("%s does not support UUIDs"), fs
->name
);
161 grub_env_set (state
[0].arg
, uuid
);
163 grub_printf ("%s", uuid
);
165 grub_device_close (dev
);
166 return GRUB_ERR_NONE
;
172 return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET
,
173 N_("filesystem `%s' does not support labels"),
175 err
= fs
->fs_label (dev
, &label
);
179 return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET
,
180 N_("filesystem `%s' does not support labels"),
184 grub_env_set (state
[0].arg
, label
);
186 grub_printf ("%s", label
);
188 grub_device_close (dev
);
189 return GRUB_ERR_NONE
;
191 grub_device_close (dev
);
192 return grub_error (GRUB_ERR_BAD_ARGUMENT
, "unrecognised target");
195 static grub_extcmd_t cmd
;
197 GRUB_MOD_INIT (probe
)
199 cmd
= grub_register_extcmd ("probe", grub_cmd_probe
, 0, N_("DEVICE"),
200 N_("Retrieve device info."), options
);
203 GRUB_MOD_FINI (probe
)
205 grub_unregister_extcmd (cmd
);