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