chiark / gitweb /
vol_id: fix logging glue
[elogind.git] / extras / volume_id / lib / minix.c
1 /*
2  * volume_id - reads filesystem label and uuid
3  *
4  * Copyright (C) 2005-2007 Kay Sievers <kay.sievers@vrfy.org>
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 #ifndef _GNU_SOURCE
21 #define _GNU_SOURCE 1
22 #endif
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <ctype.h>
30
31 #include "libvolume_id.h"
32 #include "libvolume_id-private.h"
33
34 #define MINIX_SUPERBLOCK_OFFSET                 0x400
35
36 #define MINIX_SUPER_MAGIC                       0x137F
37 #define MINIX_SUPER_MAGIC2                      0x138F
38 #define MINIX2_SUPER_MAGIC                      0x2468
39 #define MINIX2_SUPER_MAGIC2                     0x2478
40 #define MINIX3_SUPER_MAGIC                      0x4d5a
41
42 struct minix_super_block
43 {
44         uint16_t        s_ninodes;
45         uint16_t        s_nzones;
46         uint16_t        s_imap_blocks;
47         uint16_t        s_zmap_blocks;
48         uint16_t        s_firstdatazone;
49         uint16_t        s_log_zone_size;
50         uint32_t        s_max_size;
51         uint16_t        s_magic;
52         uint16_t        s_state;
53         uint32_t        s_zones;
54 } PACKED;
55
56 struct minix3_super_block {
57         uint32_t        s_ninodes;
58         uint16_t        s_pad0;
59         uint16_t        s_imap_blocks;
60         uint16_t        s_zmap_blocks;
61         uint16_t        s_firstdatazone;
62         uint16_t        s_log_zone_size;
63         uint16_t        s_pad1;
64         uint32_t        s_max_size;
65         uint32_t        s_zones;
66         uint16_t        s_magic;
67         uint16_t        s_pad2;
68         uint16_t        s_blocksize;
69         uint8_t         s_disk_version;
70 } PACKED;
71
72 int volume_id_probe_minix(struct volume_id *id, uint64_t off, uint64_t size)
73 {
74         uint8_t *buf;
75         struct minix_super_block *ms;
76         struct minix3_super_block *m3s;
77
78         info("probing at offset 0x%llx\n", (unsigned long long) off);
79
80         buf = volume_id_get_buffer(id, off + MINIX_SUPERBLOCK_OFFSET, 0x200);
81         if (buf == NULL)
82                 return -1;
83
84         ms = (struct minix_super_block *) buf;
85
86         if (ms->s_magic == MINIX_SUPER_MAGIC ||
87             ms->s_magic == bswap_16(MINIX_SUPER_MAGIC)) {
88                 strcpy(id->type_version, "1");
89                 goto found;
90         }
91         if (ms->s_magic == MINIX_SUPER_MAGIC2 ||
92             ms->s_magic == bswap_16(MINIX_SUPER_MAGIC2)) {
93                 strcpy(id->type_version, "1");
94                 goto found;
95         }
96         if (ms->s_magic == MINIX2_SUPER_MAGIC ||
97             ms->s_magic == bswap_16(MINIX2_SUPER_MAGIC)) {
98                 strcpy(id->type_version, "2");
99                 goto found;
100         }
101         if (ms->s_magic == MINIX2_SUPER_MAGIC2 ||
102             ms->s_magic == bswap_16(MINIX2_SUPER_MAGIC2)) {
103                 strcpy(id->type_version, "2");
104                 goto found;
105         }
106
107         m3s = (struct minix3_super_block *) buf;
108         if (m3s->s_magic == MINIX3_SUPER_MAGIC ||
109             m3s->s_magic == bswap_16(MINIX3_SUPER_MAGIC)) {
110                 strcpy(id->type_version, "3");
111                 goto found;
112         }
113         goto exit;
114
115 found:
116         volume_id_set_usage(id, VOLUME_ID_FILESYSTEM);
117         id->type = "minix";
118         return 0;
119
120 exit:
121         return -1;
122 }