2009-07-01 Pavel Roskin <proski@gnu.org>
[grub2/jjazz.git] / disk / fs_uuid.c
blob6901dbacf645decd30de69231728328f0ceb5df6
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 *count = 0;
34 grub_device_t ret = NULL;
36 auto int iterate_device (const char *name);
37 int iterate_device (const char *name)
39 grub_device_t dev;
41 dev = grub_device_open (name);
42 if (dev)
44 grub_fs_t fs;
46 fs = grub_fs_probe (dev);
47 if (fs && fs->uuid)
49 char *uuid;
51 (fs->uuid) (dev, &uuid);
52 if (grub_errno == GRUB_ERR_NONE && uuid)
54 (*count)++;
56 if (grub_strcasecmp (uuid, key) == 0)
58 ret = dev;
59 grub_free (uuid);
60 return 1;
62 grub_free (uuid);
66 grub_device_close (dev);
69 grub_errno = GRUB_ERR_NONE;
70 return 0;
73 grub_device_iterate (iterate_device);
75 return ret;
78 static grub_err_t
79 grub_fs_uuid_open (const char *name, grub_disk_t disk)
81 grub_device_t dev;
83 if (grub_strncmp (name, "UUID=", sizeof ("UUID=")-1))
84 return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "not a UUID virtual volume");
86 dev = search_fs_uuid (name + sizeof ("UUID=") - 1, &disk->id);
87 if (! dev)
88 return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "no matching UUID found");
90 disk->total_sectors = dev->disk->total_sectors;
91 disk->has_partitions = 0;
92 if (dev->disk->partition)
94 disk->partition = grub_malloc (sizeof (*disk->partition));
95 if (disk->partition)
96 grub_memcpy (disk->partition, dev->disk->partition,
97 sizeof (*disk->partition));
99 else
100 disk->partition = NULL;
102 disk->data = dev;
104 return GRUB_ERR_NONE;
107 static void
108 grub_fs_uuid_close (grub_disk_t disk __attribute((unused)))
110 grub_device_t parent = disk->data;
111 grub_device_close (parent);
114 static grub_err_t
115 grub_fs_uuid_read (grub_disk_t disk, grub_disk_addr_t sector,
116 grub_size_t size, char *buf)
118 grub_device_t parent = disk->data;
119 return parent->disk->dev->read (parent->disk, sector, size, buf);
122 static grub_err_t
123 grub_fs_uuid_write (grub_disk_t disk, grub_disk_addr_t sector,
124 grub_size_t size, const char *buf)
126 grub_device_t parent = disk->data;
127 return parent->disk->dev->write (parent->disk, sector, size, buf);
130 static struct grub_disk_dev grub_fs_uuid_dev =
132 .name = "fs_uuid",
133 .id = GRUB_DISK_DEVICE_UUID_ID,
134 .open = grub_fs_uuid_open,
135 .close = grub_fs_uuid_close,
136 .read = grub_fs_uuid_read,
137 .write = grub_fs_uuid_write,
138 .next = 0
141 GRUB_MOD_INIT(fs_uuid)
143 grub_disk_dev_register (&grub_fs_uuid_dev);
146 GRUB_MOD_FINI(fs_uuid)
148 grub_disk_dev_unregister (&grub_fs_uuid_dev);