blob: fa5dccee7077905e08df91df5b947aaa44370d8d [file] [log] [blame]
Denis Vlasenkode7684a2008-02-18 21:08:49 +00001/*
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
Sven-Göran Bergh3b458012013-07-31 15:45:20 +020021//kbuild:lib-$(CONFIG_FEATURE_VOLUMEID_UDF) += udf.o
22
Sven-Göran Bergh15d0a862013-07-31 15:57:59 +020023//config:config FEATURE_VOLUMEID_UDF
24//config: bool "udf filesystem"
25//config: default y
26//config: depends on VOLUMEID
Sven-Göran Bergh15d0a862013-07-31 15:57:59 +020027
Denis Vlasenkode7684a2008-02-18 21:08:49 +000028#include "volume_id_internal.h"
29
30struct 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;
Denys Vlasenko8dc0e192009-09-16 00:58:11 +020040 } PACKED tag;
Denis Vlasenkode7684a2008-02-18 21:08:49 +000041 union {
42 struct anchor_descriptor {
43 uint32_t length;
44 uint32_t location;
Denys Vlasenko8dc0e192009-09-16 00:58:11 +020045 } PACKED anchor;
Denis Vlasenkode7684a2008-02-18 21:08:49 +000046 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];
Denys Vlasenko8dc0e192009-09-16 00:58:11 +020052 } PACKED ident;
53 } PACKED primary;
54 } PACKED type;
55} PACKED;
Denis Vlasenkode7684a2008-02-18 21:08:49 +000056
57struct volume_structure_descriptor {
58 uint8_t type;
59 uint8_t id[5];
60 uint8_t version;
Denys Vlasenko8dc0e192009-09-16 00:58:11 +020061} PACKED;
Denis Vlasenkode7684a2008-02-18 21:08:49 +000062
63#define UDF_VSD_OFFSET 0x8000
64
Denys Vlasenko89300962009-11-01 23:07:18 +010065int FAST_FUNC volume_id_probe_udf(struct volume_id *id /*,uint64_t off*/)
Denis Vlasenkode7684a2008-02-18 21:08:49 +000066{
Denis Vlasenko28ea4292009-02-15 05:51:19 +000067#define off ((uint64_t)0)
Denis Vlasenkode7684a2008-02-18 21:08:49 +000068 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
99blocksize:
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
111nsr:
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",
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100119 vsd->id[0], vsd->id[1], vsd->id[2], vsd->id[3], vsd->id[4]);
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000120
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
130anchor:
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
Denys Vlasenko10ad6222017-04-17 16:13:32 +0200140 /* get descriptor list address and block count */
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000141 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:
Denis Vlasenkoc5b73722008-03-17 09:21:26 +0000166// volume_id_set_label_raw(id, &(vd->type.primary.ident.clen), 32);
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000167
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:
Denis Vlasenkoc5b73722008-03-17 09:21:26 +0000176// volume_id_set_usage(id, VOLUME_ID_FILESYSTEM);
Denys Vlasenko90615a02010-12-30 00:40:11 +0100177 IF_FEATURE_BLKID_TYPE(id->type = "udf";)
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000178 return 0;
179}