malloc->zalloc
[grub2/phcoder.git] / util / deviceiter.c
blob6443afa5e3230f3471da91ad96aa167c76ff26ea
1 /* deviceiter.c - iterate over system devices */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 1999,2000,2001,2002,2003,2004,2005,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>
22 #include <stdio.h>
23 #include <unistd.h>
24 #include <string.h>
25 #include <stdlib.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <errno.h>
29 #include <fcntl.h>
30 #include <limits.h>
32 #include <grub/util/misc.h>
33 #include <grub/util/deviceiter.h>
35 #ifdef __linux__
36 # if !defined(__GLIBC__) || \
37 ((__GLIBC__ < 2) || ((__GLIBC__ == 2) && (__GLIBC_MINOR__ < 1)))
38 /* Maybe libc doesn't have large file support. */
39 # include <linux/unistd.h> /* _llseek */
40 # endif /* (GLIBC < 2) || ((__GLIBC__ == 2) && (__GLIBC_MINOR < 1)) */
41 # include <sys/ioctl.h> /* ioctl */
42 # ifndef HDIO_GETGEO
43 # define HDIO_GETGEO 0x0301 /* get device geometry */
44 /* If HDIO_GETGEO is not defined, it is unlikely that hd_geometry is
45 defined. */
46 struct hd_geometry
48 unsigned char heads;
49 unsigned char sectors;
50 unsigned short cylinders;
51 unsigned long start;
53 # endif /* ! HDIO_GETGEO */
54 # ifndef FLOPPY_MAJOR
55 # define FLOPPY_MAJOR 2 /* the major number for floppy */
56 # endif /* ! FLOPPY_MAJOR */
57 # ifndef MAJOR
58 # define MAJOR(dev) \
59 ({ \
60 unsigned long long __dev = (dev); \
61 (unsigned) ((__dev >> 8) & 0xfff) \
62 | ((unsigned int) (__dev >> 32) & ~0xfff); \
64 # endif /* ! MAJOR */
65 # ifndef CDROM_GET_CAPABILITY
66 # define CDROM_GET_CAPABILITY 0x5331 /* get capabilities */
67 # endif /* ! CDROM_GET_CAPABILITY */
68 # ifndef BLKGETSIZE
69 # define BLKGETSIZE _IO(0x12,96) /* return device size */
70 # endif /* ! BLKGETSIZE */
71 #endif /* __linux__ */
73 /* Use __FreeBSD_kernel__ instead of __FreeBSD__ for compatibility with
74 kFreeBSD-based non-FreeBSD systems (e.g. GNU/kFreeBSD) */
75 #if defined(__FreeBSD__) && ! defined(__FreeBSD_kernel__)
76 # define __FreeBSD_kernel__
77 #endif
78 #ifdef __FreeBSD_kernel__
79 /* Obtain version of kFreeBSD headers */
80 # include <osreldate.h>
81 # ifndef __FreeBSD_kernel_version
82 # define __FreeBSD_kernel_version __FreeBSD_version
83 # endif
85 /* Runtime detection of kernel */
86 # include <sys/utsname.h>
87 int
88 get_kfreebsd_version (void)
90 struct utsname uts;
91 int major;
92 int minor;
93 int v[2];
95 uname (&uts);
96 sscanf (uts.release, "%d.%d", &major, &minor);
98 if (major >= 9)
99 major = 9;
100 if (major >= 5)
102 v[0] = minor/10; v[1] = minor%10;
104 else
106 v[0] = minor%10; v[1] = minor/10;
108 return major*100000+v[0]*10000+v[1]*1000;
110 #endif /* __FreeBSD_kernel__ */
112 #if defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__OpenBSD__)
113 # include <sys/ioctl.h> /* ioctl */
114 # include <sys/disklabel.h>
115 # include <sys/cdio.h> /* CDIOCCLRDEBUG */
116 # if defined(__FreeBSD_kernel__)
117 # include <sys/param.h>
118 # if __FreeBSD_kernel_version >= 500040
119 # include <sys/disk.h>
120 # endif
121 # endif /* __FreeBSD_kernel__ */
122 #endif /* __FreeBSD_kernel__ || __NetBSD__ || __OpenBSD__ */
124 #ifdef HAVE_OPENDISK
125 # include <util.h>
126 #endif /* HAVE_OPENDISK */
128 #ifdef __linux__
129 /* Check if we have devfs support. */
130 static int
131 have_devfs (void)
133 struct stat st;
134 return stat ("/dev/.devfsd", &st) == 0;
136 #endif /* __linux__ */
138 /* These three functions are quite different among OSes. */
139 static void
140 get_floppy_disk_name (char *name, int unit)
142 #if defined(__linux__)
143 /* GNU/Linux */
144 if (have_devfs ())
145 sprintf (name, "/dev/floppy/%d", unit);
146 else
147 sprintf (name, "/dev/fd%d", unit);
148 #elif defined(__GNU__)
149 /* GNU/Hurd */
150 sprintf (name, "/dev/fd%d", unit);
151 #elif defined(__FreeBSD_kernel__)
152 /* kFreeBSD */
153 if (get_kfreebsd_version () >= 400000)
154 sprintf (name, "/dev/fd%d", unit);
155 else
156 sprintf (name, "/dev/rfd%d", unit);
157 #elif defined(__NetBSD__)
158 /* NetBSD */
159 /* opendisk() doesn't work for floppies. */
160 sprintf (name, "/dev/rfd%da", unit);
161 #elif defined(__OpenBSD__)
162 /* OpenBSD */
163 sprintf (name, "/dev/rfd%dc", unit);
164 #elif defined(__QNXNTO__)
165 /* QNX RTP */
166 sprintf (name, "/dev/fd%d", unit);
167 #elif defined(__CYGWIN__)
168 /* Cygwin */
169 sprintf (name, "/dev/fd%d", unit);
170 #elif defined(__MINGW32__)
171 (void) unit;
172 *name = 0;
173 #else
174 # warning "BIOS floppy drives cannot be guessed in your operating system."
175 /* Set NAME to a bogus string. */
176 *name = 0;
177 #endif
180 static void
181 get_ide_disk_name (char *name, int unit)
183 #if defined(__linux__)
184 /* GNU/Linux */
185 sprintf (name, "/dev/hd%c", unit + 'a');
186 #elif defined(__GNU__)
187 /* GNU/Hurd */
188 sprintf (name, "/dev/hd%d", unit);
189 #elif defined(__FreeBSD_kernel__)
190 /* kFreeBSD */
191 if (get_kfreebsd_version () >= 400000)
192 sprintf (name, "/dev/ad%d", unit);
193 else
194 sprintf (name, "/dev/rwd%d", unit);
195 #elif defined(__NetBSD__) && defined(HAVE_OPENDISK)
196 /* NetBSD */
197 char shortname[16];
198 int fd;
200 sprintf (shortname, "wd%d", unit);
201 fd = opendisk (shortname, O_RDONLY, name,
202 16, /* length of NAME */
203 0 /* char device */
205 close (fd);
206 #elif defined(__OpenBSD__)
207 /* OpenBSD */
208 sprintf (name, "/dev/rwd%dc", unit);
209 #elif defined(__QNXNTO__)
210 /* QNX RTP */
211 /* Actually, QNX RTP doesn't distinguish IDE from SCSI, so this could
212 contain SCSI disks. */
213 sprintf (name, "/dev/hd%d", unit);
214 #elif defined(__CYGWIN__)
215 /* Cygwin emulates all disks as /dev/sdX. */
216 (void) unit;
217 *name = 0;
218 #elif defined(__MINGW32__)
219 sprintf (name, "//./PHYSICALDRIVE%d", unit);
220 #else
221 # warning "BIOS IDE drives cannot be guessed in your operating system."
222 /* Set NAME to a bogus string. */
223 *name = 0;
224 #endif
227 static void
228 get_scsi_disk_name (char *name, int unit)
230 #if defined(__linux__)
231 /* GNU/Linux */
232 sprintf (name, "/dev/sd%c", unit + 'a');
233 #elif defined(__GNU__)
234 /* GNU/Hurd */
235 sprintf (name, "/dev/sd%d", unit);
236 #elif defined(__FreeBSD_kernel__)
237 /* kFreeBSD */
238 if (get_kfreebsd_version () >= 400000)
239 sprintf (name, "/dev/da%d", unit);
240 else
241 sprintf (name, "/dev/rda%d", unit);
242 #elif defined(__NetBSD__) && defined(HAVE_OPENDISK)
243 /* NetBSD */
244 char shortname[16];
245 int fd;
247 sprintf (shortname, "sd%d", unit);
248 fd = opendisk (shortname, O_RDONLY, name,
249 16, /* length of NAME */
250 0 /* char device */
252 close (fd);
253 #elif defined(__OpenBSD__)
254 /* OpenBSD */
255 sprintf (name, "/dev/rsd%dc", unit);
256 #elif defined(__QNXNTO__)
257 /* QNX RTP */
258 /* QNX RTP doesn't distinguish SCSI from IDE, so it is better to
259 disable the detection of SCSI disks here. */
260 *name = 0;
261 #elif defined(__CYGWIN__)
262 /* Cygwin emulates all disks as /dev/sdX. */
263 sprintf (name, "/dev/sd%c", unit + 'a');
264 #elif defined(__MINGW32__)
265 (void) unit;
266 *name = 0;
267 #else
268 # warning "BIOS SCSI drives cannot be guessed in your operating system."
269 /* Set NAME to a bogus string. */
270 *name = 0;
271 #endif
274 #ifdef __linux__
275 static void
276 get_virtio_disk_name (char *name, int unit)
278 #ifdef __sparc__
279 sprintf (name, "/dev/vdisk%c", unit + 'a');
280 #else
281 sprintf (name, "/dev/vd%c", unit + 'a');
282 #endif
285 static void
286 get_dac960_disk_name (char *name, int controller, int drive)
288 sprintf (name, "/dev/rd/c%dd%d", controller, drive);
291 static void
292 get_ataraid_disk_name (char *name, int unit)
294 sprintf (name, "/dev/ataraid/d%c", unit + '0');
297 static void
298 get_i2o_disk_name (char *name, char unit)
300 sprintf (name, "/dev/i2o/hd%c", unit);
303 static void
304 get_cciss_disk_name (char *name, int controller, int drive)
306 sprintf (name, "/dev/cciss/c%dd%d", controller, drive);
309 static void
310 get_ida_disk_name (char *name, int controller, int drive)
312 sprintf (name, "/dev/ida/c%dd%d", controller, drive);
315 static void
316 get_mmc_disk_name (char *name, int unit)
318 sprintf (name, "/dev/mmcblk%d", unit);
321 static void
322 get_xvd_disk_name (char *name, int unit)
324 sprintf (name, "/dev/xvd%c", unit + 'a');
326 #endif
328 /* Check if DEVICE can be read. If an error occurs, return zero,
329 otherwise return non-zero. */
330 static int
331 check_device (const char *device)
333 char buf[512];
334 FILE *fp;
336 /* If DEVICE is empty, just return error. */
337 if (*device == 0)
338 return 0;
340 fp = fopen (device, "r");
341 if (! fp)
343 switch (errno)
345 #ifdef ENOMEDIUM
346 case ENOMEDIUM:
347 # if 0
348 /* At the moment, this finds only CDROMs, which can't be
349 read anyway, so leave it out. Code should be
350 reactivated if `removable disks' and CDROMs are
351 supported. */
352 /* Accept it, it may be inserted. */
353 return 1;
354 # endif
355 break;
356 #endif /* ENOMEDIUM */
357 default:
358 /* Break case and leave. */
359 break;
361 /* Error opening the device. */
362 return 0;
365 /* Make sure CD-ROMs don't get assigned a BIOS disk number
366 before SCSI disks! */
367 #ifdef __linux__
368 # ifdef CDROM_GET_CAPABILITY
369 if (ioctl (fileno (fp), CDROM_GET_CAPABILITY, 0) >= 0)
370 return 0;
371 # else /* ! CDROM_GET_CAPABILITY */
372 /* Check if DEVICE is a CD-ROM drive by the HDIO_GETGEO ioctl. */
374 struct hd_geometry hdg;
375 struct stat st;
377 if (fstat (fileno (fp), &st))
378 return 0;
380 /* If it is a block device and isn't a floppy, check if HDIO_GETGEO
381 succeeds. */
382 if (S_ISBLK (st.st_mode)
383 && MAJOR (st.st_rdev) != FLOPPY_MAJOR
384 && ioctl (fileno (fp), HDIO_GETGEO, &hdg))
385 return 0;
387 # endif /* ! CDROM_GET_CAPABILITY */
388 #endif /* __linux__ */
390 #if defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__OpenBSD__)
391 # ifdef CDIOCCLRDEBUG
392 if (ioctl (fileno (fp), CDIOCCLRDEBUG, 0) >= 0)
393 return 0;
394 # endif /* CDIOCCLRDEBUG */
395 #endif /* __FreeBSD_kernel__ || __NetBSD__ || __OpenBSD__ */
397 /* Attempt to read the first sector. */
398 if (fread (buf, 1, 512, fp) != 512)
400 fclose (fp);
401 return 0;
404 fclose (fp);
405 return 1;
408 void
409 grub_util_iterate_devices (int NESTED_FUNC_ATTR (*hook) (const char *, int),
410 int floppy_disks)
412 int i;
414 /* Floppies. */
415 for (i = 0; i < floppy_disks; i++)
417 char name[16];
418 struct stat st;
420 get_floppy_disk_name (name, i);
421 if (stat (name, &st) < 0)
422 break;
423 /* In floppies, write the map, whether check_device succeeds
424 or not, because the user just may not insert floppies. */
425 if (hook (name, 1))
426 return;
429 #ifdef __linux__
430 if (have_devfs ())
432 i = 0;
433 while (1)
435 char discn[32];
436 char name[PATH_MAX];
437 struct stat st;
439 /* Linux creates symlinks "/dev/discs/discN" for convenience.
440 The way to number disks is the same as GRUB's. */
441 sprintf (discn, "/dev/discs/disc%d", i++);
442 if (stat (discn, &st) < 0)
443 break;
445 if (realpath (discn, name))
447 strcat (name, "/disc");
448 if (hook (name, 0))
449 return;
452 return;
454 #endif /* __linux__ */
456 /* IDE disks. */
457 for (i = 0; i < 26; i++)
459 char name[16];
461 get_ide_disk_name (name, i);
462 if (check_device (name))
464 if (hook (name, 0))
465 return;
469 #ifdef __linux__
470 /* Virtio disks. */
471 for (i = 0; i < 26; i++)
473 char name[16];
475 get_virtio_disk_name (name, i);
476 if (check_device (name))
478 if (hook (name, 0))
479 return;
483 /* ATARAID disks. */
484 for (i = 0; i < 8; i++)
486 char name[20];
488 get_ataraid_disk_name (name, i);
489 if (check_device (name))
491 if (hook (name, 0))
492 return;
496 /* Xen virtual block devices. */
497 for (i = 0; i < 26; i++)
499 char name[16];
501 get_xvd_disk_name (name, i);
502 if (check_device (name))
504 if (hook (name, 0))
505 return;
508 #endif /* __linux__ */
510 /* The rest is SCSI disks. */
511 for (i = 0; i < 26; i++)
513 char name[16];
515 get_scsi_disk_name (name, i);
516 if (check_device (name))
518 if (hook (name, 0))
519 return;
523 #ifdef __linux__
524 /* This is for DAC960 - we have
525 /dev/rd/c<controller>d<logical drive>p<partition>.
527 DAC960 driver currently supports up to 8 controllers, 32 logical
528 drives, and 7 partitions. */
530 int controller, drive;
532 for (controller = 0; controller < 8; controller++)
534 for (drive = 0; drive < 15; drive++)
536 char name[24];
538 get_dac960_disk_name (name, controller, drive);
539 if (check_device (name))
541 if (hook (name, 0))
542 return;
548 /* This is for CCISS - we have
549 /dev/cciss/c<controller>d<logical drive>p<partition>. */
551 int controller, drive;
553 for (controller = 0; controller < 3; controller++)
555 for (drive = 0; drive < 16; drive++)
557 char name[24];
559 get_cciss_disk_name (name, controller, drive);
560 if (check_device (name))
562 if (hook (name, 0))
563 return;
569 /* This is for Compaq Intelligent Drive Array - we have
570 /dev/ida/c<controller>d<logical drive>p<partition>. */
572 int controller, drive;
574 for (controller = 0; controller < 3; controller++)
576 for (drive = 0; drive < 16; drive++)
578 char name[24];
580 get_ida_disk_name (name, controller, drive);
581 if (check_device (name))
583 if (hook (name, 0))
584 return;
590 /* This is for I2O - we have /dev/i2o/hd<logical drive><partition> */
592 char unit;
594 for (unit = 'a'; unit < 'f'; unit++)
596 char name[24];
598 get_i2o_disk_name (name, unit);
599 if (check_device (name))
601 if (hook (name, 0))
602 return;
607 /* MultiMediaCard (MMC). */
608 for (i = 0; i < 10; i++)
610 char name[16];
612 get_mmc_disk_name (name, i);
613 if (check_device (name))
615 if (hook (name, 0))
616 return;
619 #endif /* __linux__ */