1 /* device.c - device manager */
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2002,2005,2007,2008,2009 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>
28 #include <grub/i18n.h>
30 grub_net_t (*grub_net_open
) (const char *name
) = NULL
;
33 grub_device_open (const char *name
)
35 grub_device_t dev
= 0;
39 name
= grub_env_get ("root");
40 if (name
== NULL
|| *name
== '\0')
42 grub_error (GRUB_ERR_BAD_DEVICE
, N_("variable `%s' isn't set"), "root");
47 dev
= grub_malloc (sizeof (*dev
));
52 /* Try to open a disk. */
53 dev
->disk
= grub_disk_open (name
);
56 if (grub_net_open
&& grub_errno
== GRUB_ERR_UNKNOWN_DEVICE
)
58 grub_errno
= GRUB_ERR_NONE
;
59 dev
->net
= grub_net_open (name
);
72 grub_device_close (grub_device_t device
)
75 grub_disk_close (device
->disk
);
79 grub_free (device
->net
->server
);
80 grub_free (device
->net
);
90 struct part_ent
*next
;
94 /* Context for grub_device_iterate. */
95 struct grub_device_iterate_ctx
97 grub_device_iterate_hook_t hook
;
99 struct part_ent
*ents
;
102 /* Helper for grub_device_iterate. */
104 iterate_partition (grub_disk_t disk
, const grub_partition_t partition
,
107 struct grub_device_iterate_ctx
*ctx
= data
;
111 p
= grub_malloc (sizeof (*p
));
117 part_name
= grub_partition_get_name (partition
);
123 p
->name
= grub_xasprintf ("%s,%s", disk
->name
, part_name
);
124 grub_free (part_name
);
137 /* Helper for grub_device_iterate. */
139 iterate_disk (const char *disk_name
, void *data
)
141 struct grub_device_iterate_ctx
*ctx
= data
;
144 if (ctx
->hook (disk_name
, ctx
->hook_data
))
147 dev
= grub_device_open (disk_name
);
150 grub_errno
= GRUB_ERR_NONE
;
160 (void) grub_partition_iterate (dev
->disk
, iterate_partition
, ctx
);
161 grub_device_close (dev
);
163 grub_errno
= GRUB_ERR_NONE
;
168 struct part_ent
*next
= p
->next
;
171 ret
= ctx
->hook (p
->name
, ctx
->hook_data
);
180 grub_device_close (dev
);
185 grub_device_iterate (grub_device_iterate_hook_t hook
, void *hook_data
)
187 struct grub_device_iterate_ctx ctx
= { hook
, hook_data
, NULL
};
189 /* Only disk devices are supported at the moment. */
190 return grub_disk_dev_iterate (iterate_disk
, &ctx
);