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