2009-08-17 Robert Millan <rmh.grub@aybabtu.com>
[grub2/phcoder/solaris.git] / commands / probe.c
blobfabdb2a4a5a6e4a0731b7332dcf0a23b15eb9c8b
1 /*
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>
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>
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},
43 {0, 0, 0, 0, 0, 0}
46 static grub_err_t
47 grub_cmd_probe (grub_extcmd_t cmd, int argc, char **args)
49 struct grub_arg_list *state = cmd->state;
50 grub_device_t dev;
51 grub_fs_t fs;
52 char *ptr;
53 grub_err_t err;
55 if (argc < 1)
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 == ')')
61 *ptr = 0;
62 dev = grub_device_open (args[0] + 1);
63 *ptr = ')';
65 else
66 dev = grub_device_open (args[0]);
67 if (! dev)
68 return grub_error (GRUB_ERR_BAD_DEVICE, "couldn't open device");
70 if (state[1].set)
72 const char *val = "none";
73 if (dev->net)
74 val = dev->net->dev->name;
75 if (dev->disk)
76 val = dev->disk->dev->name;
77 if (state[0].set)
78 grub_env_set (state[0].arg, val);
79 else
80 grub_printf ("%s", val);
81 return GRUB_ERR_NONE;
83 if (state[2].set)
85 const char *val = "none";
86 if (dev->disk && dev->disk->partition)
87 val = dev->disk->partition->partmap->name;
88 if (state[0].set)
89 grub_env_set (state[0].arg, val);
90 else
91 grub_printf ("%s", val);
92 return GRUB_ERR_NONE;
94 fs = grub_fs_probe (dev);
95 if (! fs)
96 return grub_error (GRUB_ERR_UNKNOWN_FS, "unrecognised fs");
97 if (state[3].set)
99 if (state[0].set)
100 grub_env_set (state[0].arg, fs->name);
101 else
102 grub_printf ("%s", fs->name);
103 return GRUB_ERR_NONE;
105 if (state[4].set)
107 char *uuid;
108 if (! fs->uuid)
109 return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
110 "uuid for this FS isn't supported yet");
111 err = fs->uuid (dev, &uuid);
112 if (err)
113 return err;
114 if (! uuid)
115 return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
116 "uuid for this FS isn't supported yet");
118 if (state[0].set)
119 grub_env_set (state[0].arg, uuid);
120 else
121 grub_printf ("%s", uuid);
122 grub_free (uuid);
123 return GRUB_ERR_NONE;
125 if (state[5].set)
127 char *label;
128 if (! fs->label)
129 return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
130 "label for this FS isn't supported yet");
131 err = fs->label (dev, &label);
132 if (err)
133 return err;
134 if (! label)
135 return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
136 "uuid for this FS isn't supported yet");
138 if (state[0].set)
139 grub_env_set (state[0].arg, label);
140 else
141 grub_printf ("%s", label);
142 grub_free (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,
153 "probe [DEVICE]",
154 "Retrieve device info.", options);
157 GRUB_MOD_FINI (probe)
159 grub_unregister_extcmd (cmd);