]> git.ipfire.org Git - thirdparty/util-linux.git/blob - libblkid/src/superblocks/udf.c
libblkid: udf: Fix reporting UDF 2.60 revision
[thirdparty/util-linux.git] / libblkid / src / superblocks / udf.c
1 /*
2 * Copyright (C) 1999 by Andries Brouwer
3 * Copyright (C) 1999, 2000, 2003 by Theodore Ts'o
4 * Copyright (C) 2001 by Andreas Dilger
5 * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
6 * Copyright (C) 2008 Karel Zak <kzak@redhat.com>
7 * Copyright (C) 2014-2017 Pali Rohár <pali.rohar@gmail.com>
8 *
9 * This file may be redistributed under the terms of the
10 * GNU Lesser General Public License.
11 */
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <unistd.h>
15 #include <string.h>
16 #include <errno.h>
17 #include <ctype.h>
18 #include <stdint.h>
19
20 #include "superblocks.h"
21
22 #define udf_cid_to_enc(cid) ((cid) == 8 ? BLKID_ENC_LATIN1 : (cid) == 16 ? BLKID_ENC_UTF16BE : -1)
23
24 struct dstring128 {
25 uint8_t cid;
26 uint8_t c[126];
27 uint8_t clen;
28 } __attribute__((packed));
29
30 struct dstring32 {
31 uint8_t cid;
32 uint8_t c[30];
33 uint8_t clen;
34 } __attribute__((packed));
35
36 struct volume_descriptor {
37 struct descriptor_tag {
38 uint16_t id;
39 uint16_t version;
40 uint8_t checksum;
41 uint8_t reserved;
42 uint16_t serial;
43 uint16_t crc;
44 uint16_t crc_len;
45 uint32_t location;
46 } __attribute__((packed)) tag;
47
48 union {
49 struct anchor_descriptor {
50 uint32_t length;
51 uint32_t location;
52 } __attribute__((packed)) anchor;
53
54 struct primary_descriptor {
55 uint32_t seq_num;
56 uint32_t desc_num;
57 struct dstring32 ident;
58 uint16_t vds_num;
59 uint16_t max_vol_seq;
60 uint16_t ichg_lvl;
61 uint16_t max_ichg_lvl;
62 uint32_t charset_list;
63 uint32_t max_charset_list;
64 struct dstring128 volset_id;
65 } __attribute__((packed)) primary;
66
67 struct logical_descriptor {
68 uint32_t seq_num;
69 uint8_t desc_charset[64];
70 struct dstring128 logvol_id;
71 uint32_t logical_blocksize;
72 uint8_t domain_id_flags;
73 char domain_id[23];
74 uint16_t udf_rev;
75 uint8_t domain_suffix_flags;
76 uint8_t reserved[5];
77 uint8_t logical_contents_use[16];
78 uint32_t map_table_length;
79 uint32_t num_partition_maps;
80 uint8_t imp_id[32];
81 uint8_t imp_use[128];
82 uint32_t lvid_length;
83 uint32_t lvid_location;
84 } __attribute__((packed)) logical;
85
86 struct logical_vol_integ_descriptor {
87 uint8_t recording_date[12];
88 uint32_t type;
89 uint32_t next_lvid_length;
90 uint32_t next_lvid_location;
91 uint8_t logical_contents_use[32];
92 uint32_t num_partitions;
93 uint32_t imp_use_length;
94 } __attribute__((packed)) logical_vol_integ;
95 } __attribute__((packed)) type;
96
97 } __attribute__((packed));
98
99 #define TAG_ID_PVD 1
100 #define TAG_ID_AVDP 2
101 #define TAG_ID_LVD 6
102 #define TAG_ID_TD 8
103 #define TAG_ID_LVID 9
104
105 struct volume_structure_descriptor {
106 uint8_t type;
107 uint8_t id[5];
108 uint8_t version;
109 } __attribute__((packed));
110
111 #define UDF_VSD_OFFSET 0x8000LL
112
113 struct logical_vol_integ_descriptor_imp_use
114 {
115 uint8_t imp_id[32];
116 uint32_t num_files;
117 uint32_t num_dirs;
118 uint16_t min_udf_read_rev;
119 uint16_t min_udf_write_rev;
120 uint16_t max_udf_write_rev;
121 } __attribute__ ((packed));
122
123 #define UDF_LVIDIU_OFFSET(vd) (sizeof((vd).tag) + sizeof((vd).type.logical_vol_integ) + 2 * 4 * le32_to_cpu((vd).type.logical_vol_integ.num_partitions))
124 #define UDF_LVIDIU_LENGTH(vd) (le32_to_cpu((vd).type.logical_vol_integ.imp_use_length))
125
126 static inline int gen_uuid_from_volset_id(unsigned char uuid[17], struct dstring128 *volset_id)
127 {
128 int enc;
129 size_t i;
130 size_t len;
131 size_t clen;
132 size_t nonhexpos;
133 unsigned char buf[17];
134
135 memset(buf, 0, sizeof(buf));
136
137 clen = volset_id->clen;
138 if (clen > 0)
139 --clen;
140 if (clen > sizeof(volset_id->c))
141 clen = sizeof(volset_id->c);
142
143 enc = udf_cid_to_enc(volset_id->cid);
144 if (enc == -1)
145 return -1;
146
147 len = blkid_encode_to_utf8(enc, buf, sizeof(buf), volset_id->c, clen);
148 if (len < 8)
149 return -1;
150
151 nonhexpos = 16;
152 for (i = 0; i < 16; ++i) {
153 if (!isxdigit(buf[i])) {
154 nonhexpos = i;
155 break;
156 }
157 }
158
159 if (nonhexpos < 8) {
160 snprintf((char *) uuid, 17, "%02x%02x%02x%02x%02x%02x%02x%02x",
161 buf[0], buf[1], buf[2], buf[3],
162 buf[4], buf[5], buf[6], buf[7]);
163 } else if (nonhexpos < 16) {
164 for (i = 0; i < 8; ++i)
165 uuid[i] = tolower(buf[i]);
166 snprintf((char *) uuid + 8, 9, "%02x%02x%02x%02x",
167 buf[8], buf[9], buf[10], buf[11]);
168 } else {
169 for (i = 0; i < 16; ++i)
170 uuid[i] = tolower(buf[i]);
171 uuid[16] = 0;
172 }
173
174 return 0;
175 }
176
177 static int probe_udf(blkid_probe pr,
178 const struct blkid_idmag *mag __attribute__((__unused__)))
179 {
180 struct volume_descriptor *vd;
181 struct volume_structure_descriptor *vsd;
182 struct logical_vol_integ_descriptor_imp_use *lvidiu;
183 uint32_t lvid_len = 0;
184 uint32_t lvid_loc = 0;
185 uint32_t bs;
186 uint32_t b;
187 uint16_t type;
188 uint32_t count;
189 uint32_t loc;
190 size_t i;
191 uint32_t vsd_len;
192 uint16_t udf_rev = 0;
193 int vsd_2048_valid = -1;
194 int have_label = 0;
195 int have_uuid = 0;
196 int have_logvolid = 0;
197 int have_volid = 0;
198 int have_volsetid = 0;
199
200 /* The block size of a UDF filesystem is that of the underlying
201 * storage; we check later on for the special case of image files,
202 * which may have any block size valid for UDF filesystem */
203 uint32_t pbs[] = { 0, 512, 1024, 2048, 4096 };
204 pbs[0] = blkid_probe_get_sectorsize(pr);
205
206 for (i = 0; i < ARRAY_SIZE(pbs); i++) {
207 /* Do not try with block size same as sector size two times */
208 if (i != 0 && pbs[0] == pbs[i])
209 continue;
210
211 /* ECMA-167 2/8.4, 2/9.1: Each VSD is either 2048 bytes long or
212 * its size is same as blocksize (for blocksize > 2048 bytes)
213 * plus padded with zeros */
214 vsd_len = pbs[i] > 2048 ? pbs[i] : 2048;
215
216 /* Process 2048 bytes long VSD only once */
217 if (vsd_len == 2048) {
218 if (vsd_2048_valid == 0)
219 continue;
220 else if (vsd_2048_valid == 1)
221 goto anchor;
222 }
223
224 /* Check for a Volume Structure Descriptor (VSD) */
225 for (b = 0; b < 64; b++) {
226 vsd = (struct volume_structure_descriptor *)
227 blkid_probe_get_buffer(pr,
228 UDF_VSD_OFFSET + b * vsd_len,
229 sizeof(*vsd));
230 if (!vsd)
231 return errno ? -errno : 1;
232 if (vsd->id[0] == '\0')
233 break;
234 if (memcmp(vsd->id, "NSR02", 5) == 0 ||
235 memcmp(vsd->id, "NSR03", 5) == 0)
236 goto anchor;
237 else if (memcmp(vsd->id, "BEA01", 5) != 0 &&
238 memcmp(vsd->id, "BOOT2", 5) != 0 &&
239 memcmp(vsd->id, "CD001", 5) != 0 &&
240 memcmp(vsd->id, "CDW02", 5) != 0 &&
241 memcmp(vsd->id, "TEA01", 5) != 0)
242 /* ECMA-167 2/8.3.1: The volume recognition sequence is
243 * terminated by the first sector which is not a valid
244 * descriptor.
245 * UDF-2.60 2.1.7: UDF 2.00 and lower revisions do not
246 * have requirement that NSR descritor is in Extended Area
247 * (between BEA01 and TEA01) and that there is only one
248 * Extended Area. So do not stop scanning after TEA01. */
249 break;
250 }
251
252 if (vsd_len == 2048)
253 vsd_2048_valid = 0;
254
255 /* NSR was not found, try with next block size */
256 continue;
257
258 anchor:
259 if (vsd_len == 2048)
260 vsd_2048_valid = 1;
261
262 /* Read Anchor Volume Descriptor (AVDP), detect block size */
263 vd = (struct volume_descriptor *)
264 blkid_probe_get_buffer(pr, 256 * pbs[i], sizeof(*vd));
265 if (!vd)
266 return errno ? -errno : 1;
267
268 /* Check that we read correct sector and detected correct block size */
269 if (le32_to_cpu(vd->tag.location) != 256)
270 continue;
271
272 type = le16_to_cpu(vd->tag.id);
273 if (type == TAG_ID_AVDP)
274 goto real_blksz;
275
276 }
277 return 1;
278
279 real_blksz:
280 /* Use the actual block size from here on out */
281 bs = pbs[i];
282
283 /* get descriptor list address and block count */
284 count = le32_to_cpu(vd->type.anchor.length) / bs;
285 loc = le32_to_cpu(vd->type.anchor.location);
286
287 /* pick the primary descriptor from the list and read UDF identifiers */
288 for (b = 0; b < count; b++) {
289 vd = (struct volume_descriptor *)
290 blkid_probe_get_buffer(pr,
291 (uint64_t) (loc + b) * bs,
292 sizeof(*vd));
293 if (!vd)
294 return errno ? -errno : 1;
295 type = le16_to_cpu(vd->tag.id);
296 if (type == 0)
297 break;
298 if (le32_to_cpu(vd->tag.location) != loc + b)
299 break;
300 if (type == TAG_ID_TD)
301 break;
302 if (type == TAG_ID_PVD) {
303 if (!have_volid) {
304 int enc = udf_cid_to_enc(vd->type.primary.ident.cid);
305 uint8_t clen = vd->type.primary.ident.clen;
306 if (clen > 0)
307 --clen;
308 if (clen > sizeof(vd->type.primary.ident.c))
309 clen = sizeof(vd->type.primary.ident.c);
310 if (enc != -1)
311 have_volid = !blkid_probe_set_utf8_id_label(pr, "VOLUME_ID",
312 vd->type.primary.ident.c, clen, enc);
313 }
314 if (!have_uuid) {
315 /* VolumeSetIdentifier in UDF 2.01 specification:
316 * =================================================================================
317 * 2.2.2.5 dstring VolumeSetIdentifier
318 *
319 * Interpreted as specifying the identifier for the volume set.
320 *
321 * The first 16 characters of this field should be set to a unique value. The
322 * remainder of the field may be set to any allowed value. Specifically, software
323 * generating volumes conforming to this specification shall not set this field to a
324 * fixed or trivial value. Duplicate disks which are intended to be identical may
325 * contain the same value in this field.
326 *
327 * NOTE: The intended purpose of this is to guarantee Volume Sets with unique
328 * identifiers. The first 8 characters of the unique part should come from a CS0
329 * hexadecimal representation of a 32-bit time value. The remaining 8 characters
330 * are free for implementation use.
331 * =================================================================================
332 *
333 * Implementation in libblkid:
334 * The first 16 (Unicode) characters of VolumeSetIdentifier are encoded to UTF-8
335 * and then first 16 UTF-8 bytes are used to generate UUID. If all 16 bytes are
336 * hexadecimal digits then their lowercase variants are used as UUID. If one of
337 * the first 8 bytes (time value) is not hexadecimal digit then first 8 bytes are
338 * encoded to their hexadecimal representations, resulting in 16 characters and
339 * set as UUID. If all first 8 bytes (time value) are hexadecimal digits but some
340 * remaining not then lowercase variant of the first 8 bytes are used as first
341 * part of UUID and next 4 bytes encoded in hexadecimal representations (resulting
342 * in 8 characters) are used as second part of UUID string.
343 */
344 unsigned char uuid[17];
345 if (gen_uuid_from_volset_id(uuid, &vd->type.primary.volset_id) == 0)
346 have_uuid = !blkid_probe_strncpy_uuid(pr, uuid, sizeof(uuid));
347 }
348 if (!have_volsetid) {
349 int enc = udf_cid_to_enc(vd->type.primary.volset_id.cid);
350 uint8_t clen = vd->type.primary.volset_id.clen;
351 if (clen > 0)
352 --clen;
353 if (clen > sizeof(vd->type.primary.volset_id.c))
354 clen = sizeof(vd->type.primary.volset_id.c);
355 if (enc != -1)
356 have_volsetid = !blkid_probe_set_utf8_id_label(pr, "VOLUME_SET_ID",
357 vd->type.primary.volset_id.c, clen, enc);
358 }
359 } else if (type == TAG_ID_LVD) {
360 if (!lvid_len || !lvid_loc) {
361 uint32_t num_partition_maps = le32_to_cpu(vd->type.logical.num_partition_maps);
362 /* ECMA-167 3/10.6.12: If num_partition_maps is 0, then no LVID is specified */
363 if (num_partition_maps) {
364 lvid_len = le32_to_cpu(vd->type.logical.lvid_length);
365 lvid_loc = le32_to_cpu(vd->type.logical.lvid_location);
366 }
367 }
368 if (!udf_rev) {
369 /* UDF-2.60: 2.1.5.3: UDF revision field shall indicate revision of UDF document
370 * We use this field as fallback value for ID_FS_VERSION when LVIDIU is missing */
371 if (strncmp(vd->type.logical.domain_id, "*OSTA UDF Compliant", sizeof(vd->type.logical.domain_id)) == 0)
372 udf_rev = le16_to_cpu(vd->type.logical.udf_rev);
373 }
374 if (!have_logvolid || !have_label) {
375 /* LogicalVolumeIdentifier in UDF 2.01 specification:
376 * ===============================================================
377 * 2. Basic Restrictions & Requirements
378 *
379 * Logical Volume Descriptor
380 *
381 * There shall be exactly one prevailing Logical Volume
382 * Descriptor recorded per Volume Set.
383 *
384 * The LogicalVolumeIdentifier field shall not be null and
385 * should contain an identifier that aids in the identification of
386 * the logical volume. Specifically, software generating
387 * volumes conforming to this specification shall not set this
388 * field to a fixed or trivial value. Duplicate disks, which are
389 * intended to be identical, may contain the same value in this
390 * field. This field is extremely important in logical volume
391 * identification when multiple media are present within a
392 * jukebox. This name is typically what is displayed to the user.
393 * ===============================================================
394 *
395 * Implementation in libblkid:
396 * The LogicalVolumeIdentifier field is used for LABEL. MS Windows
397 * read Volume Label also from LogicalVolumeIdentifier. Grub2 read
398 * LABEL also from this field. Program newfs_udf (from UDFclient)
399 * when formatting disk set this field from user option Disc Name.
400 */
401 int enc = udf_cid_to_enc(vd->type.logical.logvol_id.cid);
402 uint8_t clen = vd->type.logical.logvol_id.clen;
403 if (clen > 0)
404 --clen;
405 if (clen > sizeof(vd->type.logical.logvol_id.c))
406 clen = sizeof(vd->type.logical.logvol_id.c);
407 if (enc != -1) {
408 if (!have_label)
409 have_label = !blkid_probe_set_utf8label(pr,
410 vd->type.logical.logvol_id.c, clen, enc);
411 if (!have_logvolid)
412 have_logvolid = !blkid_probe_set_utf8_id_label(pr, "LOGICAL_VOLUME_ID",
413 vd->type.logical.logvol_id.c, clen, enc);
414 }
415 }
416 }
417 if (have_volid && have_uuid && have_volsetid && have_logvolid && have_label && lvid_len && lvid_loc)
418 break;
419 }
420
421 /* Pick the first logical volume integrity descriptor and read UDF revision */
422 if (lvid_loc && lvid_len >= sizeof(*vd)) {
423 vd = (struct volume_descriptor *)
424 blkid_probe_get_buffer(pr,
425 (uint64_t) lvid_loc * bs,
426 sizeof(*vd));
427 if (!vd)
428 return errno ? -errno : 1;
429 type = le16_to_cpu(vd->tag.id);
430 if (type == TAG_ID_LVID &&
431 le32_to_cpu(vd->tag.location) == lvid_loc &&
432 UDF_LVIDIU_LENGTH(*vd) >= sizeof(*lvidiu)) {
433 /* ECMA-167 3/8.8.2: There is stored sequence of LVIDs and valid is just last
434 * one. So correctly we should jump to next_lvid_location and read next LVID
435 * until we find last one. This could be time consuming process and could
436 * lead to scanning lot of disk blocks. Because we use LVID only for UDF
437 * version, in the worst case we would report only wrong ID_FS_VERSION. */
438 uint16_t lvidiu_udf_rev;
439 lvidiu = (struct logical_vol_integ_descriptor_imp_use *)
440 blkid_probe_get_buffer(pr,
441 (uint64_t) lvid_loc * bs + UDF_LVIDIU_OFFSET(*vd),
442 sizeof(*lvidiu));
443 if (!lvidiu)
444 return errno ? -errno : 1;
445 /* Use Minimum UDF Read Revision as ID_FS_VERSION */
446 lvidiu_udf_rev = le16_to_cpu(lvidiu->min_udf_read_rev);
447 if (lvidiu_udf_rev)
448 udf_rev = lvidiu_udf_rev;
449 /* UDF-2.60: 2. Basic Restrictions & Requirements:
450 * The Minimum UDF Read Revision value shall be at most #0250
451 * for all media with a UDF 2.60 file system.
452 * So in this case use Minimum UDF Write Revision as ID_FS_VERSION
453 * to distinguish between UDF 2.50 and UDF 2.60 discs. */
454 if (lvidiu_udf_rev == 0x250) {
455 lvidiu_udf_rev = le16_to_cpu(lvidiu->min_udf_write_rev);
456 if (lvidiu_udf_rev > 0x250)
457 udf_rev = lvidiu_udf_rev;
458 }
459 }
460 }
461
462 if (udf_rev)
463 /* UDF revision is stored as decimal number in hexadecimal format.
464 * E.g. number 0x0150 is revision 1.50, number 0x0201 is revision 2.01. */
465 blkid_probe_sprintf_version(pr, "%x.%02x", (unsigned int)(udf_rev >> 8), (unsigned int)(udf_rev & 0xFF));
466
467 return 0;
468 }
469
470
471 const struct blkid_idinfo udf_idinfo =
472 {
473 .name = "udf",
474 .usage = BLKID_USAGE_FILESYSTEM,
475 .probefunc = probe_udf,
476 .flags = BLKID_IDINFO_TOLERANT,
477 .magics =
478 {
479 { .magic = "BEA01", .len = 5, .kboff = 32, .sboff = 1 },
480 { .magic = "BOOT2", .len = 5, .kboff = 32, .sboff = 1 },
481 { .magic = "CD001", .len = 5, .kboff = 32, .sboff = 1 },
482 { .magic = "CDW02", .len = 5, .kboff = 32, .sboff = 1 },
483 { .magic = "NSR02", .len = 5, .kboff = 32, .sboff = 1 },
484 { .magic = "NSR03", .len = 5, .kboff = 32, .sboff = 1 },
485 { .magic = "TEA01", .len = 5, .kboff = 32, .sboff = 1 },
486 { NULL }
487 }
488 };