chiark / gitweb /
switch tools and volume_id from LGPL to GPLv2
[elogind.git] / extras / volume_id / volume_id / ext.c
1 /*
2  * volume_id - reads filesystem label and uuid
3  *
4  * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
5  *
6  *      This program is free software; you can redistribute it and/or modify it
7  *      under the terms of the GNU General Public License as published by the
8  *      Free Software Foundation version 2 of the License.
9  */
10
11 #ifndef _GNU_SOURCE
12 #define _GNU_SOURCE 1
13 #endif
14
15 #ifdef HAVE_CONFIG_H
16 #  include <config.h>
17 #endif
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <string.h>
23 #include <errno.h>
24 #include <ctype.h>
25
26 #include "volume_id.h"
27 #include "util.h"
28 #include "logging.h"
29 #include "ext.h"
30
31 struct ext2_super_block {
32         uint32_t        inodes_count;
33         uint32_t        blocks_count;
34         uint32_t        r_blocks_count;
35         uint32_t        free_blocks_count;
36         uint32_t        free_inodes_count;
37         uint32_t        first_data_block;
38         uint32_t        log_block_size;
39         uint32_t        dummy3[7];
40         uint8_t magic[2];
41         uint16_t        state;
42         uint32_t        dummy5[8];
43         uint32_t        feature_compat;
44         uint32_t        feature_incompat;
45         uint32_t        feature_ro_compat;
46         uint8_t uuid[16];
47         uint8_t volume_name[16];
48 } __attribute__((__packed__));
49
50 #define EXT3_FEATURE_COMPAT_HAS_JOURNAL         0x00000004
51 #define EXT3_FEATURE_INCOMPAT_JOURNAL_DEV       0x00000008
52 #define EXT_SUPERBLOCK_OFFSET                   0x400
53
54 int volume_id_probe_ext(struct volume_id *id, uint64_t off)
55 {
56         struct ext2_super_block *es;
57
58         dbg("probing at offset 0x%llx", (unsigned long long) off);
59
60         es = (struct ext2_super_block *) volume_id_get_buffer(id, off + EXT_SUPERBLOCK_OFFSET, 0x200);
61         if (es == NULL)
62                 return -1;
63
64         if (es->magic[0] != 0123 ||
65             es->magic[1] != 0357)
66                 return -1;
67
68         volume_id_set_usage(id, VOLUME_ID_FILESYSTEM);
69         volume_id_set_label_raw(id, es->volume_name, 16);
70         volume_id_set_label_string(id, es->volume_name, 16);
71         volume_id_set_uuid(id, es->uuid, UUID_DCE);
72
73         if ((le32_to_cpu(es->feature_compat) & EXT3_FEATURE_COMPAT_HAS_JOURNAL) != 0)
74                 id->type = "ext3";
75         else
76                 id->type = "ext2";
77
78         return 0;
79 }