args changelog should mention this too
[grub2/phcoder.git] / kern / device.c
blob60f25c19237eb6a0598665482a8a22278f3586f8
1 /* device.c - device manager */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2002,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 <grub/device.h>
21 #include <grub/disk.h>
22 #include <grub/net.h>
23 #include <grub/fs.h>
24 #include <grub/mm.h>
25 #include <grub/misc.h>
26 #include <grub/env.h>
27 #include <grub/partition.h>
29 grub_device_t
30 grub_device_open (const char *name)
32 grub_disk_t disk = 0;
33 grub_device_t dev = 0;
35 if (! name)
37 name = grub_env_get ("root");
38 if (*name == '\0')
40 grub_error (GRUB_ERR_BAD_DEVICE, "no device is set");
41 goto fail;
45 dev = grub_malloc (sizeof (*dev));
46 if (! dev)
47 goto fail;
49 /* Try to open a disk. */
50 disk = grub_disk_open (name);
51 if (! disk)
52 goto fail;
54 dev->disk = disk;
55 dev->net = 0; /* FIXME */
57 return dev;
59 fail:
60 if (disk)
61 grub_disk_close (disk);
63 grub_free (dev);
65 return 0;
68 grub_err_t
69 grub_device_close (grub_device_t device)
71 if (device->disk)
72 grub_disk_close (device->disk);
74 grub_free (device);
76 return grub_errno;
79 int
80 grub_device_iterate (int (*hook) (const char *name))
82 auto int iterate_disk (const char *disk_name);
83 auto int iterate_partition (grub_disk_t disk,
84 const grub_partition_t partition);
86 int iterate_disk (const char *disk_name)
88 grub_device_t dev;
90 if (hook (disk_name))
91 return 1;
93 dev = grub_device_open (disk_name);
94 if (! dev)
95 return 0;
97 if (dev->disk && dev->disk->has_partitions)
98 if (grub_partition_iterate (dev->disk, iterate_partition))
100 grub_device_close (dev);
101 return 1;
104 grub_device_close (dev);
105 return 0;
108 int iterate_partition (grub_disk_t disk, const grub_partition_t partition)
110 char *partition_name;
111 char *device_name;
112 int ret;
114 partition_name = grub_partition_get_name (partition);
115 if (! partition_name)
116 return 1;
118 device_name = grub_malloc (grub_strlen (disk->name) + 1
119 + grub_strlen (partition_name) + 1);
120 if (! device_name)
122 grub_free (partition_name);
123 return 1;
126 grub_sprintf (device_name, "%s,%s", disk->name, partition_name);
127 grub_free (partition_name);
129 ret = hook (device_name);
130 grub_free (device_name);
131 return ret;
134 /* Only disk devices are supported at the moment. */
135 return grub_disk_dev_iterate (iterate_disk);