blob: ee5bb773dbfbd920b4eb38ae5004236c6748f03d [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
Denis Vlasenkode7684a2008-02-18 21:08:49 +000023#include "volume_id_internal.h"
24
25struct volume_descriptor {
26 struct descriptor_tag {
27 uint16_t id;
28 uint16_t version;
29 uint8_t checksum;
30 uint8_t reserved;
31 uint16_t serial;
32 uint16_t crc;
33 uint16_t crc_len;
34 uint32_t location;
Denys Vlasenko8dc0e192009-09-16 00:58:11 +020035 } PACKED tag;
Denis Vlasenkode7684a2008-02-18 21:08:49 +000036 union {
37 struct anchor_descriptor {
38 uint32_t length;
39 uint32_t location;
Denys Vlasenko8dc0e192009-09-16 00:58:11 +020040 } PACKED anchor;
Denis Vlasenkode7684a2008-02-18 21:08:49 +000041 struct primary_descriptor {
42 uint32_t seq_num;
43 uint32_t desc_num;
44 struct dstring {
45 uint8_t clen;
46 uint8_t c[31];
Denys Vlasenko8dc0e192009-09-16 00:58:11 +020047 } PACKED ident;
48 } PACKED primary;
49 } PACKED type;
50} PACKED;
Denis Vlasenkode7684a2008-02-18 21:08:49 +000051
52struct volume_structure_descriptor {
53 uint8_t type;
54 uint8_t id[5];
55 uint8_t version;
Denys Vlasenko8dc0e192009-09-16 00:58:11 +020056} PACKED;
Denis Vlasenkode7684a2008-02-18 21:08:49 +000057
58#define UDF_VSD_OFFSET 0x8000
59
Denys Vlasenko89300962009-11-01 23:07:18 +010060int FAST_FUNC volume_id_probe_udf(struct volume_id *id /*,uint64_t off*/)
Denis Vlasenkode7684a2008-02-18 21:08:49 +000061{
Denis Vlasenko28ea4292009-02-15 05:51:19 +000062#define off ((uint64_t)0)
Denis Vlasenkode7684a2008-02-18 21:08:49 +000063 struct volume_descriptor *vd;
64 struct volume_structure_descriptor *vsd;
65 unsigned bs;
66 unsigned b;
67 unsigned type;
68 unsigned count;
69 unsigned loc;
70 unsigned clen;
71
72 dbg("probing at offset 0x%llx", (unsigned long long) off);
73
74 vsd = volume_id_get_buffer(id, off + UDF_VSD_OFFSET, 0x200);
75 if (vsd == NULL)
76 return -1;
77
78 if (memcmp(vsd->id, "NSR02", 5) == 0)
79 goto blocksize;
80 if (memcmp(vsd->id, "NSR03", 5) == 0)
81 goto blocksize;
82 if (memcmp(vsd->id, "BEA01", 5) == 0)
83 goto blocksize;
84 if (memcmp(vsd->id, "BOOT2", 5) == 0)
85 goto blocksize;
86 if (memcmp(vsd->id, "CD001", 5) == 0)
87 goto blocksize;
88 if (memcmp(vsd->id, "CDW02", 5) == 0)
89 goto blocksize;
90 if (memcmp(vsd->id, "TEA03", 5) == 0)
91 goto blocksize;
92 return -1;
93
94blocksize:
95 /* search the next VSD to get the logical block size of the volume */
96 for (bs = 0x800; bs < 0x8000; bs += 0x800) {
97 vsd = volume_id_get_buffer(id, off + UDF_VSD_OFFSET + bs, 0x800);
98 if (vsd == NULL)
99 return -1;
100 dbg("test for blocksize: 0x%x", bs);
101 if (vsd->id[0] != '\0')
102 goto nsr;
103 }
104 return -1;
105
106nsr:
107 /* search the list of VSDs for a NSR descriptor */
108 for (b = 0; b < 64; b++) {
109 vsd = volume_id_get_buffer(id, off + UDF_VSD_OFFSET + (b * bs), 0x800);
110 if (vsd == NULL)
111 return -1;
112
113 dbg("vsd: %c%c%c%c%c",
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100114 vsd->id[0], vsd->id[1], vsd->id[2], vsd->id[3], vsd->id[4]);
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000115
116 if (vsd->id[0] == '\0')
117 return -1;
118 if (memcmp(vsd->id, "NSR02", 5) == 0)
119 goto anchor;
120 if (memcmp(vsd->id, "NSR03", 5) == 0)
121 goto anchor;
122 }
123 return -1;
124
125anchor:
126 /* read anchor volume descriptor */
127 vd = volume_id_get_buffer(id, off + (256 * bs), 0x200);
128 if (vd == NULL)
129 return -1;
130
131 type = le16_to_cpu(vd->tag.id);
132 if (type != 2) /* TAG_ID_AVDP */
133 goto found;
134
135 /* get desriptor list address and block count */
136 count = le32_to_cpu(vd->type.anchor.length) / bs;
137 loc = le32_to_cpu(vd->type.anchor.location);
138 dbg("0x%x descriptors starting at logical secor 0x%x", count, loc);
139
140 /* pick the primary descriptor from the list */
141 for (b = 0; b < count; b++) {
142 vd = volume_id_get_buffer(id, off + ((loc + b) * bs), 0x200);
143 if (vd == NULL)
144 return -1;
145
146 type = le16_to_cpu(vd->tag.id);
147 dbg("descriptor type %i", type);
148
149 /* check validity */
150 if (type == 0)
151 goto found;
152 if (le32_to_cpu(vd->tag.location) != loc + b)
153 goto found;
154
155 if (type == 1) /* TAG_ID_PVD */
156 goto pvd;
157 }
158 goto found;
159
160 pvd:
Denis Vlasenkoc5b73722008-03-17 09:21:26 +0000161// volume_id_set_label_raw(id, &(vd->type.primary.ident.clen), 32);
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000162
163 clen = vd->type.primary.ident.clen;
164 dbg("label string charsize=%i bit", clen);
165 if (clen == 8)
166 volume_id_set_label_string(id, vd->type.primary.ident.c, 31);
167 else if (clen == 16)
168 volume_id_set_label_unicode16(id, vd->type.primary.ident.c, BE, 31);
169
170 found:
Denis Vlasenkoc5b73722008-03-17 09:21:26 +0000171// volume_id_set_usage(id, VOLUME_ID_FILESYSTEM);
Denys Vlasenko90615a02010-12-30 00:40:11 +0100172 IF_FEATURE_BLKID_TYPE(id->type = "udf";)
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000173 return 0;
174}