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