Fix cross compilation (e.g. on Darwin). Following changes to make.tmpl,
[AROS.git] / arch / all-pc / boot / grub2-aros / grub-core / kern / device.c
blob73b8ecc0c09744bac6805dd77b7ba269522874bb
1 /* device.c - device manager */
2 /*
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>
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>
28 #include <grub/i18n.h>
30 grub_net_t (*grub_net_open) (const char *name) = NULL;
32 grub_device_t
33 grub_device_open (const char *name)
35 grub_device_t dev = 0;
37 if (! name)
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");
43 goto fail;
47 dev = grub_malloc (sizeof (*dev));
48 if (! dev)
49 goto fail;
51 dev->net = NULL;
52 /* Try to open a disk. */
53 dev->disk = grub_disk_open (name);
54 if (dev->disk)
55 return dev;
56 if (grub_net_open && grub_errno == GRUB_ERR_UNKNOWN_DEVICE)
58 grub_errno = GRUB_ERR_NONE;
59 dev->net = grub_net_open (name);
62 if (dev->net)
63 return dev;
65 fail:
66 grub_free (dev);
68 return 0;
71 grub_err_t
72 grub_device_close (grub_device_t device)
74 if (device->disk)
75 grub_disk_close (device->disk);
77 if (device->net)
79 grub_free (device->net->server);
80 grub_free (device->net);
83 grub_free (device);
85 return grub_errno;
88 struct part_ent
90 struct part_ent *next;
91 char *name;
94 /* Context for grub_device_iterate. */
95 struct grub_device_iterate_ctx
97 grub_device_iterate_hook_t hook;
98 void *hook_data;
99 struct part_ent *ents;
102 /* Helper for grub_device_iterate. */
103 static int
104 iterate_partition (grub_disk_t disk, const grub_partition_t partition,
105 void *data)
107 struct grub_device_iterate_ctx *ctx = data;
108 struct part_ent *p;
109 char *part_name;
111 p = grub_malloc (sizeof (*p));
112 if (!p)
114 return 1;
117 part_name = grub_partition_get_name (partition);
118 if (!part_name)
120 grub_free (p);
121 return 1;
123 p->name = grub_xasprintf ("%s,%s", disk->name, part_name);
124 grub_free (part_name);
125 if (!p->name)
127 grub_free (p);
128 return 1;
131 p->next = ctx->ents;
132 ctx->ents = p;
134 return 0;
137 /* Helper for grub_device_iterate. */
138 static int
139 iterate_disk (const char *disk_name, void *data)
141 struct grub_device_iterate_ctx *ctx = data;
142 grub_device_t dev;
144 if (ctx->hook (disk_name, ctx->hook_data))
145 return 1;
147 dev = grub_device_open (disk_name);
148 if (! dev)
150 grub_errno = GRUB_ERR_NONE;
151 return 0;
154 if (dev->disk)
156 struct part_ent *p;
157 int ret = 0;
159 ctx->ents = NULL;
160 (void) grub_partition_iterate (dev->disk, iterate_partition, ctx);
161 grub_device_close (dev);
163 grub_errno = GRUB_ERR_NONE;
165 p = ctx->ents;
166 while (p != NULL)
168 struct part_ent *next = p->next;
170 if (!ret)
171 ret = ctx->hook (p->name, ctx->hook_data);
172 grub_free (p->name);
173 grub_free (p);
174 p = next;
177 return ret;
180 grub_device_close (dev);
181 return 0;
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);