make the linux-ppc packags be in synch with other platforms
[tangerine.git] / arch / common / boot / grub2 / util / grub-probe.c
blob063b8f63d0bf405cc24650714841232469f0ca1d
1 /* grub-probe.c - probe device information for a given path */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2005,2006,2007,2008 Free Software Foundation, Inc.
6 * GRUB is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * GRUB is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
20 #include <config.h>
21 #include <grub/types.h>
22 #include <grub/util/misc.h>
23 #include <grub/device.h>
24 #include <grub/disk.h>
25 #include <grub/file.h>
26 #include <grub/fs.h>
27 #include <grub/partition.h>
28 #include <grub/pc_partition.h>
29 #include <grub/util/biosdisk.h>
30 #include <grub/util/getroot.h>
31 #include <grub/term.h>
32 #include <grub/env.h>
34 #include <grub_probe_init.h>
36 #include <stdio.h>
37 #include <unistd.h>
38 #include <string.h>
39 #include <stdlib.h>
40 #include <sys/stat.h>
42 #define _GNU_SOURCE 1
43 #include <getopt.h>
45 enum {
46 PRINT_FS,
47 PRINT_DRIVE,
48 PRINT_DEVICE,
49 PRINT_PARTMAP,
50 PRINT_ABSTRACTION,
53 int print = PRINT_FS;
54 static unsigned int argument_is_device = 0;
56 void
57 grub_putchar (int c)
59 putchar (c);
62 int
63 grub_getkey (void)
65 return -1;
68 grub_term_t
69 grub_term_get_current (void)
71 return 0;
74 void
75 grub_refresh (void)
79 static void
80 probe_partmap (grub_disk_t disk)
82 char *name;
83 char *underscore;
85 if (disk->partition == NULL)
87 grub_util_info ("No partition map found for %s", disk->name);
88 return;
91 name = strdup (disk->partition->partmap->name);
92 if (! name)
93 grub_util_error ("Not enough memory");
95 underscore = strchr (name, '_');
96 if (! underscore)
97 grub_util_error ("Invalid partition map %s", name);
99 *underscore = '\0';
100 printf ("%s\n", name);
101 free (name);
104 static void
105 probe (const char *path, char *device_name)
107 char *drive_name = NULL;
108 char *grub_path = NULL;
109 char *filebuf_via_grub = NULL, *filebuf_via_sys = NULL;
110 int abstraction_type;
111 grub_device_t dev = NULL;
113 if (path == NULL)
115 if (! grub_util_check_block_device (device_name))
116 grub_util_error ("%s is not a block device.\n", device_name);
118 else
119 device_name = grub_guess_root_device (path);
121 if (! device_name)
122 grub_util_error ("cannot find a device for %s.\n", path);
124 if (print == PRINT_DEVICE)
126 printf ("%s\n", device_name);
127 goto end;
130 abstraction_type = grub_util_get_dev_abstraction (device_name);
131 /* No need to check for errors; lack of abstraction is permissible. */
133 if (print == PRINT_ABSTRACTION)
135 char *abstraction_name;
136 switch (abstraction_type)
138 case GRUB_DEV_ABSTRACTION_LVM:
139 abstraction_name = "lvm";
140 break;
141 case GRUB_DEV_ABSTRACTION_RAID:
142 abstraction_name = "raid";
143 break;
144 default:
145 grub_util_info ("did not find LVM/RAID in %s, assuming raw device", device_name);
146 goto end;
148 printf ("%s\n", abstraction_name);
149 goto end;
152 drive_name = grub_util_get_grub_dev (device_name);
153 if (! drive_name)
154 grub_util_error ("cannot find a GRUB drive for %s.\n", device_name);
156 if (print == PRINT_DRIVE)
158 printf ("(%s)\n", drive_name);
159 goto end;
162 grub_util_info ("opening %s", drive_name);
163 dev = grub_device_open (drive_name);
164 if (! dev)
165 grub_util_error ("%s", grub_errmsg);
167 if (print == PRINT_PARTMAP)
169 grub_disk_memberlist_t list = NULL, tmp;
171 /* Check if dev->disk itself is contained in a partmap. */
172 probe_partmap (dev->disk);
174 /* In case of LVM/RAID, check the member devices as well. */
175 if (dev->disk->dev->memberlist)
176 list = dev->disk->dev->memberlist (dev->disk);
177 while (list)
179 probe_partmap (list->disk);
180 tmp = list->next;
181 free (list);
182 list = tmp;
184 goto end;
187 if (print == PRINT_FS)
189 struct stat st;
190 grub_fs_t fs;
192 stat (path, &st);
194 if (st.st_mode == S_IFREG)
196 /* Regular file. Verify that we can read it properly. */
198 grub_file_t file;
199 grub_util_info ("reading %s via OS facilities", path);
200 filebuf_via_sys = grub_util_read_image (path);
202 grub_util_info ("reading %s via GRUB facilities", path);
203 asprintf (&grub_path, "(%s)%s", drive_name, path);
204 file = grub_file_open (grub_path);
205 filebuf_via_grub = xmalloc (file->size);
206 grub_file_read (file, filebuf_via_grub, file->size);
208 grub_util_info ("comparing");
210 if (memcmp (filebuf_via_grub, filebuf_via_sys, file->size))
211 grub_util_error ("files differ");
213 fs = file->fs;
215 else
217 fs = grub_fs_probe (dev);
218 if (! fs)
219 grub_util_error ("%s", grub_errmsg);
222 printf ("%s\n", fs->name);
225 end:
226 if (dev)
227 grub_device_close (dev);
228 free (grub_path);
229 free (filebuf_via_grub);
230 free (filebuf_via_sys);
231 free (drive_name);
234 static struct option options[] =
236 {"device", no_argument, 0, 'd'},
237 {"device-map", required_argument, 0, 'm'},
238 {"target", required_argument, 0, 't'},
239 {"help", no_argument, 0, 'h'},
240 {"version", no_argument, 0, 'V'},
241 {"verbose", no_argument, 0, 'v'},
242 {0, 0, 0, 0}
245 static void
246 usage (int status)
248 if (status)
249 fprintf (stderr,
250 "Try ``grub-probe --help'' for more information.\n");
251 else
252 printf ("\
253 Usage: grub-probe [OPTION]... [PATH|DEVICE]\n\
255 Probe device information for a given path (or device, if the -d option is given).\n\
257 -d, --device given argument is a system device, not a path\n\
258 -m, --device-map=FILE use FILE as the device map [default=%s]\n\
259 -t, --target=(fs|drive|device|partmap|abstraction)\n\
260 print filesystem module, GRUB drive, system device, partition map module or abstraction module [default=fs]\n\
261 -h, --help display this message and exit\n\
262 -V, --version print version information and exit\n\
263 -v, --verbose print verbose messages\n\
265 Report bugs to <%s>.\n\
267 DEFAULT_DEVICE_MAP, PACKAGE_BUGREPORT);
269 exit (status);
273 main (int argc, char *argv[])
275 char *dev_map = 0;
276 char *argument;
278 progname = "grub-probe";
280 /* Check for options. */
281 while (1)
283 int c = getopt_long (argc, argv, "dm:t:hVv", options, 0);
285 if (c == -1)
286 break;
287 else
288 switch (c)
290 case 'd':
291 argument_is_device = 1;
292 break;
294 case 'm':
295 if (dev_map)
296 free (dev_map);
298 dev_map = xstrdup (optarg);
299 break;
301 case 't':
302 if (!strcmp (optarg, "fs"))
303 print = PRINT_FS;
304 else if (!strcmp (optarg, "drive"))
305 print = PRINT_DRIVE;
306 else if (!strcmp (optarg, "device"))
307 print = PRINT_DEVICE;
308 else if (!strcmp (optarg, "partmap"))
309 print = PRINT_PARTMAP;
310 else if (!strcmp (optarg, "abstraction"))
311 print = PRINT_ABSTRACTION;
312 else
313 usage (1);
314 break;
316 case 'h':
317 usage (0);
318 break;
320 case 'V':
321 printf ("%s (%s) %s\n", progname, PACKAGE_NAME, PACKAGE_VERSION);
322 return 0;
324 case 'v':
325 verbosity++;
326 break;
328 default:
329 usage (1);
330 break;
334 if (verbosity > 1)
335 grub_env_set ("debug", "all");
337 /* Obtain ARGUMENT. */
338 if (optind >= argc)
340 fprintf (stderr, "No path or device is specified.\n");
341 usage (1);
344 if (optind + 1 != argc)
346 fprintf (stderr, "Unknown extra argument `%s'.\n", argv[optind + 1]);
347 usage (1);
350 argument = argv[optind];
352 /* Initialize the emulated biosdisk driver. */
353 grub_util_biosdisk_init (dev_map ? : DEFAULT_DEVICE_MAP);
355 /* Initialize all modules. */
356 grub_init_all ();
358 /* Do it. */
359 if (argument_is_device)
360 probe (NULL, argument);
361 else
362 probe (argument, NULL);
364 /* Free resources. */
365 grub_fini_all ();
366 grub_util_biosdisk_fini ();
368 free (dev_map);
370 return 0;