chiark / gitweb /
a72d5025a989c3359e470f63ad7f6e109d937529
[elogind.git] / extras / volume_id / volume_id / sysv.c
1 /*
2  * volume_id - reads filesystem label and uuid
3  *
4  * Copyright (C) 2005 Kay Sievers <kay.sievers@vrfy.org>
5  *
6  *      This library is free software; you can redistribute it and/or
7  *      modify it under the terms of the GNU Lesser General Public
8  *      License as published by the Free Software Foundation; either
9  *      version 2.1 of the License, or (at your option) any later version.
10  *
11  *      This library 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 GNU
14  *      Lesser General Public License for more details.
15  *
16  *      You should have received a copy of the GNU Lesser General Public
17  *      License along with this library; if not, write to the Free Software
18  *      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */
20
21 #ifndef _GNU_SOURCE
22 #define _GNU_SOURCE 1
23 #endif
24
25 #ifdef HAVE_CONFIG_H
26 #  include <config.h>
27 #endif
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32 #include <string.h>
33 #include <errno.h>
34 #include <ctype.h>
35
36 #include "volume_id.h"
37 #include "logging.h"
38 #include "util.h"
39 #include "sysv.h"
40
41 #define SYSV_NICINOD                    100
42 #define SYSV_NICFREE                    50
43
44 struct sysv_super
45 {
46         uint16_t        s_isize;
47         uint16_t        s_pad0;
48         uint32_t        s_fsize;
49         uint16_t        s_nfree;
50         uint16_t        s_pad1;
51         uint32_t        s_free[SYSV_NICFREE];
52         uint16_t        s_ninode;
53         uint16_t        s_pad2;
54         uint16_t        s_inode[SYSV_NICINOD];
55         uint8_t         s_flock;
56         uint8_t         s_ilock;
57         uint8_t         s_fmod;
58         uint8_t         s_ronly;
59         uint32_t        s_time;
60         uint16_t        s_dinfo[4];
61         uint32_t        s_tfree;
62         uint16_t        s_tinode;
63         uint16_t        s_pad3;
64         uint8_t         s_fname[6];
65         uint8_t         s_fpack[6];
66         uint32_t        s_fill[12];
67         uint32_t        s_state;
68         uint32_t        s_magic;
69         uint32_t        s_type;
70 } __attribute__((__packed__));
71
72 #define XENIX_NICINOD                           100
73 #define XENIX_NICFREE                           100
74
75 struct xenix_super {
76         uint16_t        s_isize;
77         uint32_t        s_fsize;
78         uint16_t        s_nfree;
79         uint32_t        s_free[XENIX_NICFREE];
80         uint16_t        s_ninode;
81         uint16_t        s_inode[XENIX_NICINOD];
82         uint8_t         s_flock;
83         uint8_t         s_ilock;
84         uint8_t         s_fmod;
85         uint8_t         s_ronly;
86         uint32_t        s_time;
87         uint32_t        s_tfree;
88         uint16_t        s_tinode;
89         uint16_t        s_dinfo[4];
90         uint8_t         s_fname[6];
91         uint8_t         s_fpack[6];
92         uint8_t         s_clean;
93         uint8_t         s_fill[371];
94         uint32_t        s_magic;
95         uint32_t        s_type;
96 } __attribute__((__packed__));
97
98 #define SYSV_SUPERBLOCK_BLOCK                   0x01
99 #define SYSV_MAGIC                              0xfd187e20
100 #define XENIX_SUPERBLOCK_BLOCK                  0x18
101 #define XENIX_MAGIC                             0x2b5544
102 #define SYSV_MAX_BLOCKSIZE                      0x800
103
104 int volume_id_probe_sysv(struct volume_id *id, uint64_t off)
105 {
106         struct sysv_super *vs;
107         struct xenix_super *xs;
108         unsigned int boff;
109
110         dbg("probing at offset 0x%llx", (unsigned long long) off);
111
112         for (boff = 0x200; boff <= SYSV_MAX_BLOCKSIZE; boff <<= 1) {
113                 vs = (struct sysv_super *)
114                         volume_id_get_buffer(id, off + (boff * SYSV_SUPERBLOCK_BLOCK), 0x200);
115                 if (vs == NULL)
116                         return -1;
117
118                 if (vs->s_magic == cpu_to_le32(SYSV_MAGIC) || vs->s_magic == cpu_to_be32(SYSV_MAGIC)) {
119                         volume_id_set_label_raw(id, vs->s_fname, 6);
120                         volume_id_set_label_string(id, vs->s_fname, 6);
121                         id->type = "sysv";
122                         goto found;
123                 }
124         }
125
126         for (boff = 0x200; boff <= SYSV_MAX_BLOCKSIZE; boff <<= 1) {
127                 xs = (struct xenix_super *)
128                         volume_id_get_buffer(id, off + (boff + XENIX_SUPERBLOCK_BLOCK), 0x200);
129                 if (xs == NULL)
130                         return -1;
131
132                 if (xs->s_magic == cpu_to_le32(XENIX_MAGIC) || xs->s_magic == cpu_to_be32(XENIX_MAGIC)) {
133                         volume_id_set_label_raw(id, xs->s_fname, 6);
134                         volume_id_set_label_string(id, xs->s_fname, 6);
135                         id->type = "xenix";
136                         goto found;
137                 }
138         }
139
140         return -1;
141
142 found:
143         volume_id_set_usage(id, VOLUME_ID_FILESYSTEM);
144         return 0;
145 }