Merge branch 'makefile' into haiku
[grub2/phcoder.git] / disk / fs_uuid.c
blobfaae85f48fabc43db6b5a65bd3b393ca79584290
1 /* fs_uuid.c - Access disks by their filesystem UUID. */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 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/disk.h>
21 #include <grub/dl.h>
22 #include <grub/kernel.h>
23 #include <grub/misc.h>
24 #include <grub/mm.h>
25 #include <grub/types.h>
27 #include <grub/fs.h>
28 #include <grub/partition.h>
30 static grub_device_t
31 search_fs_uuid (const char *key, unsigned long *count)
33 grub_device_t ret;
35 auto int iterate_device (const char *name);
36 int iterate_device (const char *name)
38 grub_device_t dev;
40 dev = grub_device_open (name);
41 if (dev)
43 grub_fs_t fs;
45 fs = grub_fs_probe (dev);
46 if (fs && fs->uuid)
48 char *uuid;
50 (fs->uuid) (dev, &uuid);
51 if (grub_errno == GRUB_ERR_NONE && uuid)
53 (*count)++;
55 if (grub_strcasecmp (uuid, key) == 0)
57 ret = dev;
58 grub_free (uuid);
59 return 1;
61 grub_free (uuid);
65 grub_device_close (dev);
68 grub_errno = GRUB_ERR_NONE;
69 return 0;
72 *count = 0;
73 ret = NULL;
75 grub_device_iterate (iterate_device);
77 return ret;
80 static grub_err_t
81 grub_fs_uuid_open (const char *name, grub_disk_t disk)
83 grub_device_t dev;
85 if (grub_strncmp (name, "UUID=", sizeof ("UUID=")-1))
86 return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "not a UUID virtual volume");
88 dev = search_fs_uuid (name + sizeof ("UUID=") - 1, &disk->id);
89 if (! dev)
90 return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "no matching UUID found");
92 disk->total_sectors = dev->disk->total_sectors;
93 disk->has_partitions = 0;
94 if (dev->disk->partition)
96 disk->partition = grub_malloc (sizeof (*disk->partition));
97 if (disk->partition)
98 grub_memcpy (disk->partition, dev->disk->partition,
99 sizeof (*disk->partition));
101 else
102 disk->partition = NULL;
104 disk->data = dev;
106 return GRUB_ERR_NONE;
109 static void
110 grub_fs_uuid_close (grub_disk_t disk __attribute((unused)))
112 grub_device_t parent = disk->data;
113 grub_device_close (parent);
116 static grub_err_t
117 grub_fs_uuid_read (grub_disk_t disk, grub_disk_addr_t sector,
118 grub_size_t size, char *buf)
120 grub_device_t parent = disk->data;
121 return parent->disk->dev->read (parent->disk, sector, size, buf);
124 static grub_err_t
125 grub_fs_uuid_write (grub_disk_t disk, grub_disk_addr_t sector,
126 grub_size_t size, const char *buf)
128 grub_device_t parent = disk->data;
129 return parent->disk->dev->write (parent->disk, sector, size, buf);
132 static struct grub_disk_dev grub_fs_uuid_dev =
134 .name = "fs_uuid",
135 .id = GRUB_DISK_DEVICE_UUID_ID,
136 .open = grub_fs_uuid_open,
137 .close = grub_fs_uuid_close,
138 .read = grub_fs_uuid_read,
139 .write = grub_fs_uuid_write,
140 .next = 0
143 GRUB_MOD_INIT(fs_uuid)
145 grub_disk_dev_register (&grub_fs_uuid_dev);
148 GRUB_MOD_FINI(fs_uuid)
150 grub_disk_dev_unregister (&grub_fs_uuid_dev);