2 * volume_id - reads volume label and uuid
4 * Copyright (C) 2005-2007 Kay Sievers <kay.sievers@vrfy.org>
6 * This program 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 2 of the License, or
9 * (at your option) any later version.
11 * This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
33 #include "libvolume_id.h"
34 #include "libvolume_id-private.h"
36 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
39 volume_id_probe_fn_t prober;
43 static const struct prober prober_raid[] = {
44 { volume_id_probe_linux_raid, { "linux_raid", } },
45 { volume_id_probe_ddf_raid, { "ddf_raid", } },
46 { volume_id_probe_intel_software_raid, { "isw_raid", } },
47 { volume_id_probe_lsi_mega_raid, { "lsi_mega_raid", } },
48 { volume_id_probe_via_raid, { "via_raid", } },
49 { volume_id_probe_silicon_medley_raid, { "silicon_medley_raid", } },
50 { volume_id_probe_nvidia_raid, { "nvidia_raid", } },
51 { volume_id_probe_promise_fasttrack_raid, { "promise_fasttrack_raid", } },
52 { volume_id_probe_highpoint_45x_raid, { "highpoint_raid", } },
53 { volume_id_probe_adaptec_raid, { "adaptec_raid", } },
54 { volume_id_probe_jmicron_raid, { "jmicron_raid", } },
55 { volume_id_probe_lvm1, { "lvm1", } },
56 { volume_id_probe_lvm2, { "lvm2", } },
57 { volume_id_probe_highpoint_37x_raid, { "highpoint_raid", } },
60 static const struct prober prober_filesystem[] = {
61 { volume_id_probe_vfat, { "vfat", } },
62 { volume_id_probe_linux_swap, { "swap", } },
63 { volume_id_probe_luks, { "luks", } },
64 { volume_id_probe_xfs, { "xfs", } },
65 { volume_id_probe_ext, { "ext2", "ext3", "jbd", } },
66 { volume_id_probe_reiserfs, { "reiserfs", "reiser4", } },
67 { volume_id_probe_jfs, { "jfs", } },
68 { volume_id_probe_udf, { "udf", } },
69 { volume_id_probe_iso9660, { "iso9660", } },
70 { volume_id_probe_hfs_hfsplus, { "hfs", "hfsplus", } },
71 { volume_id_probe_ufs, { "ufs", } },
72 { volume_id_probe_ntfs, { "ntfs", } },
73 { volume_id_probe_cramfs, { "cramfs", } },
74 { volume_id_probe_romfs, { "romfs", } },
75 { volume_id_probe_hpfs, { "hpfs", } },
76 { volume_id_probe_sysv, { "sysv", "xenix", } },
77 { volume_id_probe_minix, { "minix", } },
78 { volume_id_probe_gfs, { "gfs", } },
79 { volume_id_probe_gfs2, { "gfs2", } },
80 { volume_id_probe_ocfs1, { "ocfs1", } },
81 { volume_id_probe_ocfs2, { "ocfs2", } },
82 { volume_id_probe_vxfs, { "vxfs", } },
83 { volume_id_probe_squashfs, { "squashfs", } },
84 { volume_id_probe_netware, { "netware", } },
85 { volume_id_probe_oracleasm, { "oracleasm", } },
88 /* the user can overwrite this log function */
89 static void default_log(int priority, const char *file, int line, const char *format, ...)
94 volume_id_log_fn_t volume_id_log_fn = default_log;
97 * volume_id_get_prober_by_type:
100 * Lookup the probing function for a specific type.
102 * Returns: The probing function for the given type, #NULL otherwise.
104 const volume_id_probe_fn_t *volume_id_get_prober_by_type(const char *type)
111 for (p = 0; p < ARRAY_SIZE(prober_raid); p++)
112 for (n = 0; prober_raid[p].name[n] != NULL; n++)
113 if (strcmp(type, prober_raid[p].name[n]) == 0)
114 return &prober_raid[p].prober;
115 for (p = 0; p < ARRAY_SIZE(prober_filesystem); p++)
116 for (n = 0; prober_filesystem[p].name[n] != NULL; n++)
117 if (strcmp(type, prober_filesystem[p].name[n]) == 0)
118 return &prober_filesystem[p].prober;
123 * volume_id_get_label:
124 * @id: Probing context.
125 * @label: Label string. Must not be freed by the caller.
127 * Get the label string after a successful probe. Unicode
128 * is translated to UTF-8.
130 * Returns: 1 if the value was set, 0 otherwise.
132 int volume_id_get_label(struct volume_id *id, const char **label)
138 if (id->usage_id == VOLUME_ID_UNUSED)
146 * volume_id_get_label_raw:
147 * @id: Probing context.
148 * @label: Label byte array. Must not be freed by the caller.
149 * @len: Length of raw label byte array.
151 * Get the raw label byte array after a successful probe. It may
152 * contain undecoded multibyte character streams.
154 * Returns: 1 if the value was set, 0 otherwise.
156 int volume_id_get_label_raw(struct volume_id *id, const uint8_t **label, size_t *len)
164 if (id->usage_id == VOLUME_ID_UNUSED)
167 *label = id->label_raw;
168 *len = id->label_raw_len;
173 * volume_id_get_uuid:
174 * @id: Probing context.
175 * @uuid: UUID string. Must not be freed by the caller.
177 * Get the raw UUID string after a successful probe.
179 * Returns: 1 if the value was set, 0 otherwise.
181 int volume_id_get_uuid(struct volume_id *id, const char **uuid)
187 if (id->usage_id == VOLUME_ID_UNUSED)
195 * volume_id_get_uuid_raw:
196 * @id: Probing context.
197 * @uuid: UUID byte array. Must not be freed by the caller.
198 * @len: Length of raw UUID byte array.
200 * Get the raw UUID byte array after a successful probe. It may
201 * contain unconverted endianes values.
203 * Returns: 1 if the value was set, 0 otherwise.
205 int volume_id_get_uuid_raw(struct volume_id *id, const uint8_t **uuid, size_t *len)
213 if (id->usage_id == VOLUME_ID_UNUSED)
216 *uuid = id->uuid_raw;
217 *len = id->uuid_raw_len;
222 * volume_id_get_usage:
223 * @id: Probing context.
224 * @usage: Usage string. Must not be freed by the caller.
226 * Get the usage string after a successful probe.
228 * Returns: 1 if the value was set, 0 otherwise.
230 int volume_id_get_usage(struct volume_id *id, const char **usage)
236 if (id->usage_id == VOLUME_ID_UNUSED)
244 * volume_id_get_type:
245 * @id: Probing context
246 * @type: Type string. Must not be freed by the caller.
248 * Get the type string after a successful probe.
250 * Returns: 1 if the value was set, 0 otherwise.
252 int volume_id_get_type(struct volume_id *id, const char **type)
258 if (id->usage_id == VOLUME_ID_UNUSED)
266 * volume_id_get_type_version:
267 * @id: Probing context.
268 * @type_version: Type version string. Must not be freed by the caller.
270 * Get the Type version string after a successful probe.
272 * Returns: 1 if the value was set, 0 otherwise.
274 int volume_id_get_type_version(struct volume_id *id, const char **type_version)
278 if (type_version == NULL)
280 if (id->usage_id == VOLUME_ID_UNUSED)
283 *type_version = id->type_version;
287 static int needs_encoding(const char c)
289 if ((c >= '0' && c <= '9') ||
290 (c >= 'A' && c <= 'Z') ||
291 (c >= 'a' && c <= 'z') ||
292 strchr(ALLOWED_CHARS, c))
298 * volume_id_encode_string:
299 * @str: Input string to be encoded.
300 * @str_enc: Target string to store the encoded input.
301 * @len: Location to store the encoded string. The target string,
302 * which may be four times as long as the input string.
304 * Encode all potentially unsafe characters of a string to the
305 * corresponding hex value prefixed by '\x'.
307 * Returns: 1 if the entire string was copied, 0 otherwise.
309 int volume_id_encode_string(const char *str, char *str_enc, size_t len)
313 if (str == NULL || str_enc == NULL || len == 0)
317 for (i = 0, j = 0; str[i] != '\0'; i++) {
320 seqlen = volume_id_utf8_encoded_valid_unichar(&str[i]);
322 memcpy(&str_enc[j], &str[i], seqlen);
325 } else if (str[i] == '\\' || needs_encoding(str[i])) {
326 sprintf(&str_enc[j], "\\x%02x", (unsigned char) str[i]);
341 /* run only once into a timeout for unreadable devices */
342 static int device_is_readable(struct volume_id *id, uint64_t off)
344 if (volume_id_get_buffer(id, off, 0x200) != NULL)
350 * volume_id_probe_raid:
351 * @id: Probing context.
352 * @off: Probing offset relative to the start of the device.
353 * @size: Total size of the device.
355 * Probe device for all known raid signatures.
357 * Returns: 0 on successful probe, otherwise negative value.
359 int volume_id_probe_raid(struct volume_id *id, uint64_t off, uint64_t size)
366 if (!device_is_readable(id, off))
369 info("probing at offset 0x%llx, size 0x%llx\n",
370 (unsigned long long) off, (unsigned long long) size);
372 for (i = 0; i < ARRAY_SIZE(prober_raid); i++)
373 if (prober_raid[i].prober(id, off, size) == 0)
378 /* If recognized, we free the allocated buffers */
379 volume_id_free_buffer(id);
384 * volume_id_probe_filesystem:
385 * @id: Probing context.
386 * @off: Probing offset relative to the start of the device.
387 * @size: Total size of the device.
389 * Probe device for all known filesystem signatures.
391 * Returns: 0 on successful probe, otherwise negative value.
393 int volume_id_probe_filesystem(struct volume_id *id, uint64_t off, uint64_t size)
400 if (!device_is_readable(id, off))
403 info("probing at offset 0x%llx, size 0x%llx\n",
404 (unsigned long long) off, (unsigned long long) size);
406 for (i = 0; i < ARRAY_SIZE(prober_filesystem); i++)
407 if (prober_filesystem[i].prober(id, off, size) == 0)
412 /* If recognized, we free the allocated buffers */
413 volume_id_free_buffer(id);
418 * volume_id_probe_all:
419 * @id: Probing context.
420 * @off: Probing offset relative to the start of the device.
421 * @size: Total size of the device.
423 * Probe device for all known raid and filesystem signatures.
425 * Returns: 0 on successful probe, otherwise negative value.
427 int volume_id_probe_all(struct volume_id *id, uint64_t off, uint64_t size)
432 if (!device_is_readable(id, off))
435 /* probe for raid first, because fs probes may be successful on raid members */
436 if (volume_id_probe_raid(id, off, size) == 0)
439 if (volume_id_probe_filesystem(id, off, size) == 0)
446 * volume_id_probe_raid:
447 * @all_probers_fn: prober function to called for all known probing routines.
448 * @id: Context passed to prober function.
449 * @off: Offset value passed to prober function.
450 * @size: Size value passed to prober function.
451 * @data: Arbitrary data passed to the prober function.
453 * Run a custom function for all known probing routines.
455 void volume_id_all_probers(all_probers_fn_t all_probers_fn,
456 struct volume_id *id, uint64_t off, uint64_t size,
461 if (all_probers_fn == NULL)
464 for (i = 0; i < ARRAY_SIZE(prober_raid); i++)
465 if (all_probers_fn(prober_raid[i].prober, id, off, size, data) != 0)
467 for (i = 0; i < ARRAY_SIZE(prober_filesystem); i++)
468 if (all_probers_fn(prober_filesystem[i].prober, id, off, size, data) != 0)
476 * @id: Probing context.
477 * @fd: Open file descriptor of device to read from.
479 * Create the context for probing.
481 * Returns: Probing context, or #NULL on failure.
483 struct volume_id *volume_id_open_fd(int fd)
485 struct volume_id *id;
487 id = malloc(sizeof(struct volume_id));
490 memset(id, 0x00, sizeof(struct volume_id));
499 * @id: Probing context.
501 * Release probing context and free all associated data.
503 void volume_id_close(struct volume_id *id)
508 volume_id_free_buffer(id);