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
)
35 auto int iterate_device (const char *name
);
36 int iterate_device (const char *name
)
40 dev
= grub_device_open (name
);
45 fs
= grub_fs_probe (dev
);
50 (fs
->uuid
) (dev
, &uuid
);
51 if (grub_errno
== GRUB_ERR_NONE
&& uuid
)
55 if (grub_strcasecmp (uuid
, key
) == 0)
65 grub_device_close (dev
);
68 grub_errno
= GRUB_ERR_NONE
;
75 grub_device_iterate (iterate_device
);
81 grub_fs_uuid_open (const char *name
, grub_disk_t disk
)
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
);
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
));
98 grub_memcpy (disk
->partition
, dev
->disk
->partition
,
99 sizeof (*disk
->partition
));
102 disk
->partition
= NULL
;
106 return GRUB_ERR_NONE
;
110 grub_fs_uuid_close (grub_disk_t disk
__attribute((unused
)))
112 grub_device_t parent
= disk
->data
;
113 grub_device_close (parent
);
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
);
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
=
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
,
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
);