1 /* device.c - device manager */
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>
25 #include <grub/misc.h>
27 #include <grub/partition.h>
30 grub_device_open (const char *name
)
33 grub_device_t dev
= 0;
37 name
= grub_env_get ("root");
40 grub_error (GRUB_ERR_BAD_DEVICE
, "no device is set");
45 dev
= grub_malloc (sizeof (*dev
));
49 /* Try to open a disk. */
50 disk
= grub_disk_open (name
);
55 dev
->net
= 0; /* FIXME */
61 grub_disk_close (disk
);
69 grub_device_close (grub_device_t device
)
72 grub_disk_close (device
->disk
);
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
);
88 struct part_ent
*next
;
92 int iterate_disk (const char *disk_name
)
99 dev
= grub_device_open (disk_name
);
103 if (dev
->disk
&& dev
->disk
->has_partitions
)
108 (void) grub_partition_iterate (dev
->disk
, iterate_partition
);
109 grub_device_close (dev
);
115 struct part_ent
*next
= p
->next
;
118 ret
= hook (p
->name
);
127 grub_device_close (dev
);
131 int iterate_partition (grub_disk_t disk
, const grub_partition_t partition
)
133 char *partition_name
;
136 partition_name
= grub_partition_get_name (partition
);
137 if (! partition_name
)
140 p
= grub_malloc (sizeof (*p
));
144 p
->name
= grub_malloc (grub_strlen (disk
->name
) + 1
145 + grub_strlen (partition_name
) + 1);
149 grub_free (partition_name
);
153 grub_sprintf (p
->name
, "%s,%s", disk
->name
, partition_name
);
154 grub_free (partition_name
);
162 /* Only disk devices are supported at the moment. */
163 return grub_disk_dev_iterate (iterate_disk
);