chiark / gitweb /
610edb8876d8dc8fef61ee89f4bdd1991dfc6cdf
[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 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 "logging.h"
28 #include "util.h"
29 #include "sysv.h"
30
31 #define SYSV_NICINOD                    100
32 #define SYSV_NICFREE                    50
33
34 struct sysv_super
35 {
36         uint16_t        s_isize;
37         uint16_t        s_pad0;
38         uint32_t        s_fsize;
39         uint16_t        s_nfree;
40         uint16_t        s_pad1;
41         uint32_t        s_free[SYSV_NICFREE];
42         uint16_t        s_ninode;
43         uint16_t        s_pad2;
44         uint16_t        s_inode[SYSV_NICINOD];
45         uint8_t         s_flock;
46         uint8_t         s_ilock;
47         uint8_t         s_fmod;
48         uint8_t         s_ronly;
49         uint32_t        s_time;
50         uint16_t        s_dinfo[4];
51         uint32_t        s_tfree;
52         uint16_t        s_tinode;
53         uint16_t        s_pad3;
54         uint8_t         s_fname[6];
55         uint8_t         s_fpack[6];
56         uint32_t        s_fill[12];
57         uint32_t        s_state;
58         uint32_t        s_magic;
59         uint32_t        s_type;
60 } __attribute__((__packed__));
61
62 #define XENIX_NICINOD                           100
63 #define XENIX_NICFREE                           100
64
65 struct xenix_super {
66         uint16_t        s_isize;
67         uint32_t        s_fsize;
68         uint16_t        s_nfree;
69         uint32_t        s_free[XENIX_NICFREE];
70         uint16_t        s_ninode;
71         uint16_t        s_inode[XENIX_NICINOD];
72         uint8_t         s_flock;
73         uint8_t         s_ilock;
74         uint8_t         s_fmod;
75         uint8_t         s_ronly;
76         uint32_t        s_time;
77         uint32_t        s_tfree;
78         uint16_t        s_tinode;
79         uint16_t        s_dinfo[4];
80         uint8_t         s_fname[6];
81         uint8_t         s_fpack[6];
82         uint8_t         s_clean;
83         uint8_t         s_fill[371];
84         uint32_t        s_magic;
85         uint32_t        s_type;
86 } __attribute__((__packed__));
87
88 #define SYSV_SUPERBLOCK_BLOCK                   0x01
89 #define SYSV_MAGIC                              0xfd187e20
90 #define XENIX_SUPERBLOCK_BLOCK                  0x18
91 #define XENIX_MAGIC                             0x2b5544
92 #define SYSV_MAX_BLOCKSIZE                      0x800
93
94 int volume_id_probe_sysv(struct volume_id *id, uint64_t off)
95 {
96         struct sysv_super *vs;
97         struct xenix_super *xs;
98         unsigned int boff;
99
100         dbg("probing at offset 0x%llx", (unsigned long long) off);
101
102         for (boff = 0x200; boff <= SYSV_MAX_BLOCKSIZE; boff <<= 1) {
103                 vs = (struct sysv_super *)
104                         volume_id_get_buffer(id, off + (boff * SYSV_SUPERBLOCK_BLOCK), 0x200);
105                 if (vs == NULL)
106                         return -1;
107
108                 if (vs->s_magic == cpu_to_le32(SYSV_MAGIC) || vs->s_magic == cpu_to_be32(SYSV_MAGIC)) {
109                         volume_id_set_label_raw(id, vs->s_fname, 6);
110                         volume_id_set_label_string(id, vs->s_fname, 6);
111                         id->type = "sysv";
112                         goto found;
113                 }
114         }
115
116         for (boff = 0x200; boff <= SYSV_MAX_BLOCKSIZE; boff <<= 1) {
117                 xs = (struct xenix_super *)
118                         volume_id_get_buffer(id, off + (boff + XENIX_SUPERBLOCK_BLOCK), 0x200);
119                 if (xs == NULL)
120                         return -1;
121
122                 if (xs->s_magic == cpu_to_le32(XENIX_MAGIC) || xs->s_magic == cpu_to_be32(XENIX_MAGIC)) {
123                         volume_id_set_label_raw(id, xs->s_fname, 6);
124                         volume_id_set_label_string(id, xs->s_fname, 6);
125                         id->type = "xenix";
126                         goto found;
127                 }
128         }
129
130         return -1;
131
132 found:
133         volume_id_set_usage(id, VOLUME_ID_FILESYSTEM);
134         return 0;
135 }