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>
9 * This file may be redistributed under the terms of the
10 * GNU Lesser General Public License.
20 #include "superblocks.h"
22 #define udf_cid_to_enc(cid) ((cid) == 8 ? BLKID_ENC_LATIN1 : (cid) == 16 ? BLKID_ENC_UTF16BE : -1)
28 } __attribute__((packed
));
34 } __attribute__((packed
));
36 struct volume_descriptor
{
37 struct descriptor_tag
{
46 } __attribute__((packed
)) tag
;
49 struct anchor_descriptor
{
52 } __attribute__((packed
)) anchor
;
54 struct primary_descriptor
{
57 struct dstring32 ident
;
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
;
67 struct logical_descriptor
{
69 uint8_t desc_charset
[64];
70 struct dstring128 logvol_id
;
71 uint32_t logical_blocksize
;
72 uint8_t domain_id_flags
;
75 uint8_t domain_suffix_flags
;
77 uint8_t logical_contents_use
[16];
78 uint32_t map_table_length
;
79 uint32_t num_partition_maps
;
83 uint32_t lvid_location
;
84 } __attribute__((packed
)) logical
;
86 struct logical_vol_integ_descriptor
{
87 uint8_t recording_date
[12];
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
;
97 } __attribute__((packed
));
100 #define TAG_ID_AVDP 2
103 #define TAG_ID_LVID 9
105 struct volume_structure_descriptor
{
109 } __attribute__((packed
));
111 #define UDF_VSD_OFFSET 0x8000LL
113 struct logical_vol_integ_descriptor_imp_use
118 uint16_t min_udf_read_rev
;
119 uint16_t min_udf_write_rev
;
120 uint16_t max_udf_write_rev
;
121 } __attribute__ ((packed
));
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))
126 static inline int gen_uuid_from_volset_id(unsigned char uuid
[17], struct dstring128
*volset_id
)
133 unsigned char buf
[17];
135 memset(buf
, 0, sizeof(buf
));
137 clen
= volset_id
->clen
;
140 if (clen
> sizeof(volset_id
->c
))
141 clen
= sizeof(volset_id
->c
);
143 enc
= udf_cid_to_enc(volset_id
->cid
);
147 len
= blkid_encode_to_utf8(enc
, buf
, sizeof(buf
), volset_id
->c
, clen
);
152 for (i
= 0; i
< 16; ++i
) {
153 if (!isxdigit(buf
[i
])) {
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]);
169 for (i
= 0; i
< 16; ++i
)
170 uuid
[i
] = tolower(buf
[i
]);
177 static int probe_udf(blkid_probe pr
,
178 const struct blkid_idmag
*mag
__attribute__((__unused__
)))
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;
192 uint16_t udf_rev
= 0;
193 int vsd_2048_valid
= -1;
196 int have_logvolid
= 0;
198 int have_volsetid
= 0;
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
);
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
])
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;
216 /* Process 2048 bytes long VSD only once */
217 if (vsd_len
== 2048) {
218 if (vsd_2048_valid
== 0)
220 else if (vsd_2048_valid
== 1)
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
,
231 return errno
? -errno
: 1;
232 if (vsd
->id
[0] == '\0')
234 if (memcmp(vsd
->id
, "NSR02", 5) == 0 ||
235 memcmp(vsd
->id
, "NSR03", 5) == 0)
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
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. */
255 /* NSR was not found, try with next block size */
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
));
266 return errno
? -errno
: 1;
268 /* Check that we read correct sector and detected correct block size */
269 if (le32_to_cpu(vd
->tag
.location
) != 256)
272 type
= le16_to_cpu(vd
->tag
.id
);
273 if (type
== TAG_ID_AVDP
)
280 /* Use the actual block size from here on out */
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
);
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
,
294 return errno
? -errno
: 1;
295 type
= le16_to_cpu(vd
->tag
.id
);
298 if (le32_to_cpu(vd
->tag
.location
) != loc
+ b
)
300 if (type
== TAG_ID_TD
)
302 if (type
== TAG_ID_PVD
) {
304 int enc
= udf_cid_to_enc(vd
->type
.primary
.ident
.cid
);
305 uint8_t clen
= vd
->type
.primary
.ident
.clen
;
308 if (clen
> sizeof(vd
->type
.primary
.ident
.c
))
309 clen
= sizeof(vd
->type
.primary
.ident
.c
);
311 have_volid
= !blkid_probe_set_utf8_id_label(pr
, "VOLUME_ID",
312 vd
->type
.primary
.ident
.c
, clen
, enc
);
315 /* VolumeSetIdentifier in UDF 2.01 specification:
316 * =================================================================================
317 * 2.2.2.5 dstring VolumeSetIdentifier
319 * Interpreted as specifying the identifier for the volume set.
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.
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 * =================================================================================
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.
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
));
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
;
353 if (clen
> sizeof(vd
->type
.primary
.volset_id
.c
))
354 clen
= sizeof(vd
->type
.primary
.volset_id
.c
);
356 have_volsetid
= !blkid_probe_set_utf8_id_label(pr
, "VOLUME_SET_ID",
357 vd
->type
.primary
.volset_id
.c
, clen
, enc
);
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
);
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
);
374 if (!have_logvolid
|| !have_label
) {
375 /* LogicalVolumeIdentifier in UDF 2.01 specification:
376 * ===============================================================
377 * 2. Basic Restrictions & Requirements
379 * Logical Volume Descriptor
381 * There shall be exactly one prevailing Logical Volume
382 * Descriptor recorded per Volume Set.
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 * ===============================================================
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.
401 int enc
= udf_cid_to_enc(vd
->type
.logical
.logvol_id
.cid
);
402 uint8_t clen
= vd
->type
.logical
.logvol_id
.clen
;
405 if (clen
> sizeof(vd
->type
.logical
.logvol_id
.c
))
406 clen
= sizeof(vd
->type
.logical
.logvol_id
.c
);
409 have_label
= !blkid_probe_set_utf8label(pr
,
410 vd
->type
.logical
.logvol_id
.c
, clen
, enc
);
412 have_logvolid
= !blkid_probe_set_utf8_id_label(pr
, "LOGICAL_VOLUME_ID",
413 vd
->type
.logical
.logvol_id
.c
, clen
, enc
);
417 if (have_volid
&& have_uuid
&& have_volsetid
&& have_logvolid
&& have_label
&& lvid_len
&& lvid_loc
)
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
,
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
),
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
);
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
;
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));
471 const struct blkid_idinfo udf_idinfo
=
474 .usage
= BLKID_USAGE_FILESYSTEM
,
475 .probefunc
= probe_udf
,
476 .flags
= BLKID_IDINFO_TOLERANT
,
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 },