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>
34 static const struct grub_arg_option options
[] =
36 {"set", 's', GRUB_ARG_OPTION_OPTIONAL
,
37 "set a variable to return value", "VAR", ARG_TYPE_STRING
},
38 {"driver", 'd', 0, "determine driver", 0, 0},
39 {"partmap", 'p', 0, "determine partition map type", 0, 0},
40 {"fs", 'f', 0, "determine filesystem type", 0, 0},
41 {"fs-uuid", 'u', 0, "determine filesystem UUID", 0, 0},
42 {"label", 'l', 0, "determine filesystem label", 0, 0},
47 grub_cmd_probe (grub_extcmd_t cmd
, int argc
, char **args
)
49 struct grub_arg_list
*state
= cmd
->state
;
56 return grub_error (GRUB_ERR_BAD_ARGUMENT
, "device name required");
58 ptr
= args
[0] + grub_strlen (args
[0]) - 1;
59 if (args
[0][0] == '(' && *ptr
== ')')
62 dev
= grub_device_open (args
[0] + 1);
66 dev
= grub_device_open (args
[0]);
68 return grub_error (GRUB_ERR_BAD_DEVICE
, "couldn't open device");
72 const char *val
= "none";
74 val
= dev
->net
->dev
->name
;
76 val
= dev
->disk
->dev
->name
;
78 grub_env_set (state
[0].arg
, val
);
80 grub_printf ("%s", val
);
85 const char *val
= "none";
86 if (dev
->disk
&& dev
->disk
->partition
)
87 val
= dev
->disk
->partition
->partmap
->name
;
89 grub_env_set (state
[0].arg
, val
);
91 grub_printf ("%s", val
);
94 fs
= grub_fs_probe (dev
);
96 return grub_error (GRUB_ERR_UNKNOWN_FS
, "unrecognised fs");
100 grub_env_set (state
[0].arg
, fs
->name
);
102 grub_printf ("%s", fs
->name
);
103 return GRUB_ERR_NONE
;
109 return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET
,
110 "uuid for this FS isn't supported yet");
111 err
= fs
->uuid (dev
, &uuid
);
115 return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET
,
116 "uuid for this FS isn't supported yet");
119 grub_env_set (state
[0].arg
, uuid
);
121 grub_printf ("%s", uuid
);
123 return GRUB_ERR_NONE
;
129 return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET
,
130 "label for this FS isn't supported yet");
131 err
= fs
->label (dev
, &label
);
135 return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET
,
136 "uuid for this FS isn't supported yet");
139 grub_env_set (state
[0].arg
, label
);
141 grub_printf ("%s", label
);
143 return GRUB_ERR_NONE
;
145 return grub_error (GRUB_ERR_BAD_ARGUMENT
, "unrecognised target");
148 static grub_extcmd_t cmd
;
150 GRUB_MOD_INIT (probe
)
152 cmd
= grub_register_extcmd ("probe", grub_cmd_probe
, GRUB_COMMAND_FLAG_BOTH
,
154 "Retrieve device info.", options
);
157 GRUB_MOD_FINI (probe
)
159 grub_unregister_extcmd (cmd
);