Big cleanup in config help and description
[oweals/busybox.git] / util-linux / 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 //kbuild:lib-$(CONFIG_FEATURE_VOLUMEID_UDF) += udf.o
22
23 //config:config FEATURE_VOLUMEID_UDF
24 //config:       bool "udf filesystem"
25 //config:       default y
26 //config:       depends on VOLUMEID
27
28 #include "volume_id_internal.h"
29
30 struct volume_descriptor {
31         struct descriptor_tag {
32                 uint16_t        id;
33                 uint16_t        version;
34                 uint8_t         checksum;
35                 uint8_t         reserved;
36                 uint16_t        serial;
37                 uint16_t        crc;
38                 uint16_t        crc_len;
39                 uint32_t        location;
40         } PACKED tag;
41         union {
42                 struct anchor_descriptor {
43                         uint32_t        length;
44                         uint32_t        location;
45                 } PACKED anchor;
46                 struct primary_descriptor {
47                         uint32_t        seq_num;
48                         uint32_t        desc_num;
49                         struct dstring {
50                                 uint8_t clen;
51                                 uint8_t c[31];
52                         } PACKED ident;
53                 } PACKED primary;
54         } PACKED type;
55 } PACKED;
56
57 struct volume_structure_descriptor {
58         uint8_t         type;
59         uint8_t         id[5];
60         uint8_t         version;
61 } PACKED;
62
63 #define UDF_VSD_OFFSET                  0x8000
64
65 int FAST_FUNC volume_id_probe_udf(struct volume_id *id /*,uint64_t off*/)
66 {
67 #define off ((uint64_t)0)
68         struct volume_descriptor *vd;
69         struct volume_structure_descriptor *vsd;
70         unsigned bs;
71         unsigned b;
72         unsigned type;
73         unsigned count;
74         unsigned loc;
75         unsigned clen;
76
77         dbg("probing at offset 0x%llx", (unsigned long long) off);
78
79         vsd = 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 = 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 = 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 = 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 = 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         IF_FEATURE_BLKID_TYPE(id->type = "udf";)
178         return 0;
179 }