1 /* fs_uuid.c - Access disks by their filesystem UUID. */
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>
22 #include <grub/kernel.h>
23 #include <grub/misc.h>
25 #include <grub/types.h>
28 #include <grub/partition.h>
31 search_fs_uuid (const char *key
, unsigned long *count
)
34 grub_device_t ret
= NULL
;
36 auto int iterate_device (const char *name
);
37 int iterate_device (const char *name
)
41 dev
= grub_device_open (name
);
46 fs
= grub_fs_probe (dev
);
51 (fs
->uuid
) (dev
, &uuid
);
52 if (grub_errno
== GRUB_ERR_NONE
&& uuid
)
56 if (grub_strcasecmp (uuid
, key
) == 0)
66 grub_device_close (dev
);
69 grub_errno
= GRUB_ERR_NONE
;
73 grub_device_iterate (iterate_device
);
79 grub_fs_uuid_open (const char *name
, grub_disk_t disk
)
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
);
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
));
96 grub_memcpy (disk
->partition
, dev
->disk
->partition
,
97 sizeof (*disk
->partition
));
100 disk
->partition
= NULL
;
104 return GRUB_ERR_NONE
;
108 grub_fs_uuid_close (grub_disk_t disk
__attribute((unused
)))
110 grub_device_t parent
= disk
->data
;
111 grub_device_close (parent
);
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
);
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
=
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
,
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
);