]>
Commit | Line | Data |
---|---|---|
8b5f3938 | 1 | /* grub-probefs.c - probe a filesystem module for a given path */ |
2 | /* | |
3 | * GRUB -- GRand Unified Bootloader | |
4 | * Copyright (C) 2005 Free Software Foundation, Inc. | |
5 | * | |
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 2 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program 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. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with GRUB; if not, write to the Free Software | |
18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
19 | */ | |
20 | ||
21 | #include <config.h> | |
22 | #include <grub/types.h> | |
23 | #include <grub/util/misc.h> | |
24 | #include <grub/device.h> | |
25 | #include <grub/disk.h> | |
26 | #include <grub/fs.h> | |
27 | #include <grub/partition.h> | |
28 | #include <grub/pc_partition.h> | |
29 | #include <grub/machine/util/biosdisk.h> | |
30 | #include <grub/util/getroot.h> | |
31 | ||
32 | #include <stdio.h> | |
33 | #include <unistd.h> | |
34 | #include <string.h> | |
35 | #include <stdlib.h> | |
36 | ||
37 | #define _GNU_SOURCE 1 | |
38 | #include <getopt.h> | |
39 | ||
40 | #ifdef __NetBSD__ | |
41 | /* NetBSD uses /boot for its boot block. */ | |
42 | # define DEFAULT_DIRECTORY "/grub" | |
43 | #else | |
44 | # define DEFAULT_DIRECTORY "/boot/grub" | |
45 | #endif | |
46 | ||
47 | #define DEFAULT_DEVICE_MAP DEFAULT_DIRECTORY "/device.map" | |
48 | ||
49 | void | |
50 | grub_putchar (int c) | |
51 | { | |
52 | putchar (c); | |
53 | } | |
54 | ||
55 | void | |
56 | grub_refresh (void) | |
57 | { | |
58 | } | |
59 | ||
60 | static void | |
61 | probe (const char *path) | |
62 | { | |
63 | char *device_name; | |
64 | grub_device_t dev; | |
65 | grub_fs_t fs; | |
66 | ||
67 | device_name = grub_guess_root_device (path); | |
68 | if (! device_name) | |
69 | { | |
70 | fprintf (stderr, "cannot find a GRUB device for %s.\n", path); | |
71 | return; | |
72 | } | |
73 | ||
74 | grub_util_info ("opening %s", device_name); | |
75 | dev = grub_device_open (device_name); | |
76 | if (! dev) | |
77 | grub_util_error ("%s", grub_errmsg); | |
78 | ||
79 | fs = grub_fs_probe (dev); | |
80 | if (! fs) | |
81 | grub_util_error ("%s", grub_errmsg); | |
82 | ||
83 | printf ("%s\n", fs->name); | |
84 | ||
85 | grub_device_close (dev); | |
86 | free (device_name); | |
87 | } | |
88 | ||
89 | static struct option options[] = | |
90 | { | |
91 | {"device-map", required_argument, 0, 'm'}, | |
92 | {"help", no_argument, 0, 'h'}, | |
93 | {"version", no_argument, 0, 'V'}, | |
94 | {"verbose", no_argument, 0, 'v'}, | |
95 | {0, 0, 0, 0} | |
96 | }; | |
97 | ||
98 | static void | |
99 | usage (int status) | |
100 | { | |
101 | if (status) | |
102 | fprintf (stderr, | |
103 | "Try ``grub-probefs --help'' for more information.\n"); | |
104 | else | |
105 | printf ("\ | |
106 | Usage: grub-probefs [OPTION]... PATH\n\ | |
107 | \n\ | |
108 | Probe a filesystem module for a given path.\n\ | |
109 | \n\ | |
110 | -m, --device-map=FILE use FILE as the device map [default=%s]\n\ | |
111 | -h, --help display this message and exit\n\ | |
112 | -V, --version print version information and exit\n\ | |
113 | -v, --verbose print verbose messages\n\ | |
114 | \n\ | |
115 | Report bugs to <%s>.\n\ | |
116 | ", | |
117 | DEFAULT_DEVICE_MAP, PACKAGE_BUGREPORT); | |
118 | ||
119 | exit (status); | |
120 | } | |
121 | ||
122 | int | |
123 | main (int argc, char *argv[]) | |
124 | { | |
125 | char *dev_map = 0; | |
126 | char *path; | |
127 | ||
128 | progname = "grub-probefs"; | |
129 | ||
130 | /* Check for options. */ | |
131 | while (1) | |
132 | { | |
133 | int c = getopt_long (argc, argv, "m:hVv", options, 0); | |
134 | ||
135 | if (c == -1) | |
136 | break; | |
137 | else | |
138 | switch (c) | |
139 | { | |
140 | case 'm': | |
141 | if (dev_map) | |
142 | free (dev_map); | |
143 | ||
144 | dev_map = xstrdup (optarg); | |
145 | break; | |
146 | ||
147 | case 'h': | |
148 | usage (0); | |
149 | break; | |
150 | ||
151 | case 'V': | |
152 | printf ("%s (%s) %s\n", progname, PACKAGE_NAME, PACKAGE_VERSION); | |
153 | return 0; | |
154 | ||
155 | case 'v': | |
156 | verbosity++; | |
157 | break; | |
158 | ||
159 | default: | |
160 | usage (1); | |
161 | break; | |
162 | } | |
163 | } | |
164 | ||
165 | /* Obtain PATH. */ | |
166 | if (optind >= argc) | |
167 | { | |
168 | fprintf (stderr, "No path is specified.\n"); | |
169 | usage (1); | |
170 | } | |
171 | ||
172 | if (optind + 1 != argc) | |
173 | { | |
174 | fprintf (stderr, "Unknown extra argument `%s'.\n", argv[optind + 1]); | |
175 | usage (1); | |
176 | } | |
177 | ||
178 | path = argv[optind]; | |
179 | ||
180 | /* Initialize the emulated biosdisk driver. */ | |
181 | grub_util_biosdisk_init (dev_map ? : DEFAULT_DEVICE_MAP); | |
182 | grub_pc_partition_map_init (); | |
183 | ||
184 | /* Initialize filesystems. */ | |
185 | grub_fat_init (); | |
186 | grub_ext2_init (); | |
187 | grub_ufs_init (); | |
188 | grub_minix_init (); | |
189 | grub_hfs_init (); | |
190 | grub_jfs_init (); | |
191 | ||
192 | /* Do it. */ | |
193 | probe (path); | |
194 | ||
195 | /* Free resources. */ | |
196 | grub_ext2_fini (); | |
197 | grub_fat_fini (); | |
198 | grub_ufs_fini (); | |
199 | grub_minix_fini (); | |
200 | grub_hfs_fini (); | |
201 | grub_jfs_fini (); | |
202 | ||
203 | grub_pc_partition_map_fini (); | |
204 | grub_util_biosdisk_fini (); | |
205 | ||
206 | free (dev_map); | |
207 | ||
208 | return 0; | |
209 | } |