chiark / gitweb /
volume_id: add Veritas fs
[elogind.git] / extras / volume_id / volume_id / udf.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 "logging.h"
28 #include "util.h"
29 #include "udf.h"
30
31 struct volume_descriptor {
32         struct descriptor_tag {
33                 uint16_t        id;
34                 uint16_t        version;
35                 uint8_t         checksum;
36                 uint8_t         reserved;
37                 uint16_t        serial;
38                 uint16_t        crc;
39                 uint16_t        crc_len;
40                 uint32_t        location;
41         } __attribute__((__packed__)) tag;
42         union {
43                 struct anchor_descriptor {
44                         uint32_t        length;
45                         uint32_t        location;
46                 } __attribute__((__packed__)) anchor;
47                 struct primary_descriptor {
48                         uint32_t        seq_num;
49                         uint32_t        desc_num;
50                         struct dstring {
51                                 uint8_t clen;
52                                 uint8_t c[31];
53                         } __attribute__((__packed__)) ident;
54                 } __attribute__((__packed__)) primary;
55         } __attribute__((__packed__)) type;
56 } __attribute__((__packed__));
57
58 struct volume_structure_descriptor {
59         uint8_t         type;
60         uint8_t         id[5];
61         uint8_t         version;
62 } __attribute__((__packed__));
63
64 #define UDF_VSD_OFFSET                  0x8000
65
66 int volume_id_probe_udf(struct volume_id *id, uint64_t off)
67 {
68         struct volume_descriptor *vd;
69         struct volume_structure_descriptor *vsd;
70         unsigned int bs;
71         unsigned int b;
72         unsigned int type;
73         unsigned int count;
74         unsigned int loc;
75         unsigned int clen;
76
77         dbg("probing at offset 0x%llx", (unsigned long long) off);
78
79         vsd = (struct volume_structure_descriptor *) volume_id_get_buffer(id, off + UDF_VSD_OFFSET, 0x200);
80         if (vsd == NULL)
81                 return -1;
82
83         if (memcmp(vsd->id, "NSR02", 5) == 0)
84                 goto blocksize;
85         if (memcmp(vsd->id, "NSR03", 5) == 0)
86                 goto blocksize;
87         if (memcmp(vsd->id, "BEA01", 5) == 0)
88                 goto blocksize;
89         if (memcmp(vsd->id, "BOOT2", 5) == 0)
90                 goto blocksize;
91         if (memcmp(vsd->id, "CD001", 5) == 0)
92                 goto blocksize;
93         if (memcmp(vsd->id, "CDW02", 5) == 0)
94                 goto blocksize;
95         if (memcmp(vsd->id, "TEA03", 5) == 0)
96                 goto blocksize;
97         return -1;
98
99 blocksize:
100         /* search the next VSD to get the logical block size of the volume */
101         for (bs = 0x800; bs < 0x8000; bs += 0x800) {
102                 vsd = (struct volume_structure_descriptor *) volume_id_get_buffer(id, off + UDF_VSD_OFFSET + bs, 0x800);
103                 if (vsd == NULL)
104                         return -1;
105                 dbg("test for blocksize: 0x%x", bs);
106                 if (vsd->id[0] != '\0')
107                         goto nsr;
108         }
109         return -1;
110
111 nsr:
112         /* search the list of VSDs for a NSR descriptor */
113         for (b = 0; b < 64; b++) {
114                 vsd = (struct volume_structure_descriptor *) volume_id_get_buffer(id, off + UDF_VSD_OFFSET + (b * bs), 0x800);
115                 if (vsd == NULL)
116                         return -1;
117
118                 dbg("vsd: %c%c%c%c%c",
119                     vsd->id[0], vsd->id[1], vsd->id[2], vsd->id[3], vsd->id[4]);
120
121                 if (vsd->id[0] == '\0')
122                         return -1;
123                 if (memcmp(vsd->id, "NSR02", 5) == 0)
124                         goto anchor;
125                 if (memcmp(vsd->id, "NSR03", 5) == 0)
126                         goto anchor;
127         }
128         return -1;
129
130 anchor:
131         /* read anchor volume descriptor */
132         vd = (struct volume_descriptor *) volume_id_get_buffer(id, off + (256 * bs), 0x200);
133         if (vd == NULL)
134                 return -1;
135
136         type = le16_to_cpu(vd->tag.id);
137         if (type != 2) /* TAG_ID_AVDP */
138                 goto found;
139
140         /* get desriptor list address and block count */
141         count = le32_to_cpu(vd->type.anchor.length) / bs;
142         loc = le32_to_cpu(vd->type.anchor.location);
143         dbg("0x%x descriptors starting at logical secor 0x%x", count, loc);
144
145         /* pick the primary descriptor from the list */
146         for (b = 0; b < count; b++) {
147                 vd = (struct volume_descriptor *) volume_id_get_buffer(id, off + ((loc + b) * bs), 0x200);
148                 if (vd == NULL)
149                         return -1;
150
151                 type = le16_to_cpu(vd->tag.id);
152                 dbg("descriptor type %i", type);
153
154                 /* check validity */
155                 if (type == 0)
156                         goto found;
157                 if (le32_to_cpu(vd->tag.location) != loc + b)
158                         goto found;
159
160                 if (type == 1) /* TAG_ID_PVD */
161                         goto pvd;
162         }
163         goto found;
164
165 pvd:
166         volume_id_set_label_raw(id, &(vd->type.primary.ident.clen), 32);
167
168         clen = vd->type.primary.ident.clen;
169         dbg("label string charsize=%i bit", clen);
170         if (clen == 8)
171                 volume_id_set_label_string(id, vd->type.primary.ident.c, 31);
172         else if (clen == 16)
173                 volume_id_set_label_unicode16(id, vd->type.primary.ident.c, BE,31);
174
175 found:
176         volume_id_set_usage(id, VOLUME_ID_FILESYSTEM);
177         id->type = "udf";
178
179         return 0;
180 }