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>
30 search_fs_uuid (const char *key
, unsigned long *count
)
33 grub_device_t ret
= NULL
;
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_strcmp (uuid
, key
) == 0)
65 grub_device_close (dev
);
68 grub_errno
= GRUB_ERR_NONE
;
72 grub_device_iterate (iterate_device
);
78 grub_fs_uuid_open (const char *name
, grub_disk_t disk
)
82 if (grub_strncmp (name
, "UUID=", sizeof ("UUID=")-1))
83 return grub_error (GRUB_ERR_UNKNOWN_DEVICE
, "not a UUID virtual volume");
85 dev
= search_fs_uuid (name
+ sizeof ("UUID=") - 1, &disk
->id
);
87 return grub_error (GRUB_ERR_UNKNOWN_DEVICE
, "no matching UUID found");
89 disk
->total_sectors
= dev
->disk
->total_sectors
;
90 disk
->has_partitions
= 0;
91 disk
->partition
= dev
->disk
->partition
;
92 disk
->data
= dev
->disk
;
98 grub_fs_uuid_close (grub_disk_t disk
__attribute((unused
)))
103 grub_fs_uuid_read (grub_disk_t disk
, grub_disk_addr_t sector
,
104 grub_size_t size
, char *buf
)
106 grub_disk_t parent
= disk
->data
;
107 return parent
->dev
->read (parent
, sector
, size
, buf
);
111 grub_fs_uuid_write (grub_disk_t disk
, grub_disk_addr_t sector
,
112 grub_size_t size
, const char *buf
)
114 grub_disk_t parent
= disk
->data
;
115 return parent
->dev
->write (parent
, sector
, size
, buf
);
118 static struct grub_disk_dev grub_fs_uuid_dev
=
121 .id
= GRUB_DISK_DEVICE_UUID_ID
,
122 .open
= grub_fs_uuid_open
,
123 .close
= grub_fs_uuid_close
,
124 .read
= grub_fs_uuid_read
,
125 .write
= grub_fs_uuid_write
,
129 GRUB_MOD_INIT(fs_uuid
)
131 grub_disk_dev_register (&grub_fs_uuid_dev
);
134 GRUB_MOD_FINI(fs_uuid
)
136 grub_disk_dev_unregister (&grub_fs_uuid_dev
);