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