Upgraded GRUB2 to 2.00 release.
[AROS.git] / arch / all-pc / boot / grub2-aros / grub-core / commands / probe.c
blobeeece876d5056ee897d799f04a1be06b7d2021a8
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>
33 #include <grub/i18n.h>
35 GRUB_MOD_LICENSE ("GPLv3+");
37 static const struct grub_arg_option options[] =
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}
51 static grub_err_t
52 grub_cmd_probe (grub_extcmd_context_t ctxt, int argc, char **args)
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;
60 if (argc < 1)
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 == ')')
66 *ptr = 0;
67 dev = grub_device_open (args[0] + 1);
68 *ptr = ')';
70 else
71 dev = grub_device_open (args[0]);
72 if (! dev)
73 return grub_errno;
75 if (state[1].set)
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 return GRUB_ERR_NONE;
88 if (state[2].set)
90 const char *val = "none";
91 if (dev->disk && dev->disk->partition)
92 val = dev->disk->partition->partmap->name;
93 if (state[0].set)
94 grub_env_set (state[0].arg, val);
95 else
96 grub_printf ("%s", val);
97 return GRUB_ERR_NONE;
99 fs = grub_fs_probe (dev);
100 if (! fs)
101 return grub_errno;
102 if (state[3].set)
104 if (state[0].set)
105 grub_env_set (state[0].arg, fs->name);
106 else
107 grub_printf ("%s", fs->name);
108 return GRUB_ERR_NONE;
110 if (state[4].set)
112 char *uuid;
113 if (! fs->uuid)
114 return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
115 N_("%s does not support UUIDs"), fs->name);
116 err = fs->uuid (dev, &uuid);
117 if (err)
118 return err;
119 if (! uuid)
120 return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
121 N_("%s does not support UUIDs"), fs->name);
123 if (state[0].set)
124 grub_env_set (state[0].arg, uuid);
125 else
126 grub_printf ("%s", uuid);
127 grub_free (uuid);
128 return GRUB_ERR_NONE;
130 if (state[5].set)
132 char *label;
133 if (! fs->label)
134 return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
135 N_("filesystem `%s' does not support labels"),
136 fs->name);
137 err = fs->label (dev, &label);
138 if (err)
139 return err;
140 if (! label)
141 return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
142 N_("filesystem `%s' does not support labels"),
143 fs->name);
145 if (state[0].set)
146 grub_env_set (state[0].arg, label);
147 else
148 grub_printf ("%s", label);
149 grub_free (label);
150 return GRUB_ERR_NONE;
152 return grub_error (GRUB_ERR_BAD_ARGUMENT, "unrecognised target");
155 static grub_extcmd_t cmd;
157 GRUB_MOD_INIT (probe)
159 cmd = grub_register_extcmd ("probe", grub_cmd_probe, 0, N_("DEVICE"),
160 N_("Retrieve device info."), options);
163 GRUB_MOD_FINI (probe)
165 grub_unregister_extcmd (cmd);