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