blob: 5cde3cc2af6d5e42e2ba2fe64204d9c7244a4c0f [file] [log] [blame]
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001/*
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 *
8 * This file may be redistributed under the terms of the
9 * GNU Lesser General Public License.
10 */
11#include <stdio.h>
12#include <stdlib.h>
13#include <unistd.h>
14#include <string.h>
15#include <errno.h>
16#include <ctype.h>
17#include <stdint.h>
18
19#include "superblocks.h"
20#include "iso9660.h"
21
22struct volume_descriptor {
23 struct descriptor_tag {
24 uint16_t id;
25 uint16_t version;
26 uint8_t checksum;
27 uint8_t reserved;
28 uint16_t serial;
29 uint16_t crc;
30 uint16_t crc_len;
31 uint32_t location;
32 } __attribute__((packed)) tag;
33
34 union {
35 struct anchor_descriptor {
36 uint32_t length;
37 uint32_t location;
38 } __attribute__((packed)) anchor;
39
40 struct primary_descriptor {
41 uint32_t seq_num;
42 uint32_t desc_num;
43 struct dstring {
44 uint8_t clen;
45 uint8_t c[31];
46 } __attribute__((packed)) ident;
47 } __attribute__((packed)) primary;
48
49 } __attribute__((packed)) type;
50
51} __attribute__((packed));
52
53struct volume_structure_descriptor {
54 uint8_t type;
55 uint8_t id[5];
56 uint8_t version;
57} __attribute__((packed));
58
59#define UDF_VSD_OFFSET 0x8000LL
60
61static int probe_udf(blkid_probe pr,
62 const struct blkid_idmag *mag __attribute__((__unused__)))
63{
64 struct volume_descriptor *vd;
65 struct volume_structure_descriptor *vsd;
66 unsigned int bs;
bigbiff7b4c7a62015-01-01 19:44:14 -050067 unsigned int pbs[2];
bigbiff bigbiffe60683a2013-02-22 20:55:50 -050068 unsigned int b;
69 unsigned int type;
70 unsigned int count;
71 unsigned int loc;
bigbiff7b4c7a62015-01-01 19:44:14 -050072 unsigned int i;
bigbiff bigbiffe60683a2013-02-22 20:55:50 -050073
bigbiff7b4c7a62015-01-01 19:44:14 -050074 /* The block size of a UDF filesystem is that of the underlying
75 * storage; we check later on for the special case of image files,
76 * which may have the 2048-byte block size of optical media. */
77 pbs[0] = blkid_probe_get_sectorsize(pr);
78 pbs[1] = 0x800;
79
80 /* check for a Volume Structure Descriptor (VSD); each is
81 * 2048 bytes long */
82 for (b = 0; b < 0x8000; b += 0x800) {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -050083 vsd = (struct volume_structure_descriptor *)
84 blkid_probe_get_buffer(pr,
bigbiff7b4c7a62015-01-01 19:44:14 -050085 UDF_VSD_OFFSET + b,
bigbiff bigbiffe60683a2013-02-22 20:55:50 -050086 sizeof(*vsd));
87 if (!vsd)
bigbiff7b4c7a62015-01-01 19:44:14 -050088 return errno ? -errno : 1;
bigbiff bigbiffe60683a2013-02-22 20:55:50 -050089 if (vsd->id[0] != '\0')
90 goto nsr;
91 }
bigbiff7b4c7a62015-01-01 19:44:14 -050092 return 1;
bigbiff bigbiffe60683a2013-02-22 20:55:50 -050093
94nsr:
95 /* search the list of VSDs for a NSR descriptor */
96 for (b = 0; b < 64; b++) {
97 vsd = (struct volume_structure_descriptor *)
98 blkid_probe_get_buffer(pr,
bigbiff7b4c7a62015-01-01 19:44:14 -050099 UDF_VSD_OFFSET + ((blkid_loff_t) b * 0x800),
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500100 sizeof(*vsd));
101 if (!vsd)
bigbiff7b4c7a62015-01-01 19:44:14 -0500102 return errno ? -errno : 1;
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500103 if (vsd->id[0] == '\0')
bigbiff7b4c7a62015-01-01 19:44:14 -0500104 return 1;
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500105 if (memcmp(vsd->id, "NSR02", 5) == 0)
106 goto anchor;
107 if (memcmp(vsd->id, "NSR03", 5) == 0)
108 goto anchor;
109 }
bigbiff7b4c7a62015-01-01 19:44:14 -0500110 return 1;
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500111
112anchor:
bigbiff7b4c7a62015-01-01 19:44:14 -0500113 /* read Anchor Volume Descriptor (AVDP), checking block size */
114 for (i = 0; i < 2; i++) {
115 vd = (struct volume_descriptor *)
116 blkid_probe_get_buffer(pr, 256 * pbs[i], sizeof(*vd));
117 if (!vd)
118 return errno ? -errno : 1;
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500119
bigbiff7b4c7a62015-01-01 19:44:14 -0500120 type = le16_to_cpu(vd->tag.id);
121 if (type == 2) /* TAG_ID_AVDP */
122 goto real_blksz;
123 }
124 return 0;
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500125
bigbiff7b4c7a62015-01-01 19:44:14 -0500126real_blksz:
127 /* Use the actual block size from here on out */
128 bs = pbs[i];
129
130 /* get descriptor list address and block count */
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500131 count = le32_to_cpu(vd->type.anchor.length) / bs;
132 loc = le32_to_cpu(vd->type.anchor.location);
133
134 /* check if the list is usable */
135 for (b = 0; b < count; b++) {
136 vd = (struct volume_descriptor *)
137 blkid_probe_get_buffer(pr,
138 (blkid_loff_t) (loc + b) * bs,
139 sizeof(*vd));
140 if (!vd)
bigbiff7b4c7a62015-01-01 19:44:14 -0500141 return errno ? -errno : 1;
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500142 }
143
144 /* Try extract all possible ISO9660 information -- if there is
145 * usable LABEL in ISO header then use it, otherwise read UDF
146 * specific LABEL */
147 if (probe_iso9660(pr, mag) == 0 &&
148 __blkid_probe_lookup_value(pr, "LABEL") != NULL)
149 return 0;
150
151 /* Read UDF label */
152 for (b = 0; b < count; b++) {
153 vd = (struct volume_descriptor *)
154 blkid_probe_get_buffer(pr,
155 (blkid_loff_t) (loc + b) * bs,
156 sizeof(*vd));
bigbiff7b4c7a62015-01-01 19:44:14 -0500157 if (!vd)
158 return errno ? -errno : 1;
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500159 type = le16_to_cpu(vd->tag.id);
160 if (type == 0)
161 break;
162 if (le32_to_cpu(vd->tag.location) != loc + b)
163 break;
164 if (type == 1) { /* TAG_ID_PVD */
165 uint8_t clen = vd->type.primary.ident.clen;
166
167 if (clen == 8)
168 blkid_probe_set_label(pr,
169 vd->type.primary.ident.c, 31);
170 else if (clen == 16)
171 blkid_probe_set_utf8label(pr,
172 vd->type.primary.ident.c,
173 31, BLKID_ENC_UTF16BE);
174
175 if (clen == 8 || clen == 16)
176 break;
177 }
178 }
179
180 return 0;
181}
182
183
184const struct blkid_idinfo udf_idinfo =
185{
186 .name = "udf",
187 .usage = BLKID_USAGE_FILESYSTEM,
188 .probefunc = probe_udf,
189 .flags = BLKID_IDINFO_TOLERANT,
190 .magics =
191 {
192 { .magic = "BEA01", .len = 5, .kboff = 32, .sboff = 1 },
193 { .magic = "BOOT2", .len = 5, .kboff = 32, .sboff = 1 },
194 { .magic = "CD001", .len = 5, .kboff = 32, .sboff = 1 },
195 { .magic = "CDW02", .len = 5, .kboff = 32, .sboff = 1 },
196 { .magic = "NSR02", .len = 5, .kboff = 32, .sboff = 1 },
197 { .magic = "NSR03", .len = 5, .kboff = 32, .sboff = 1 },
198 { .magic = "TEA01", .len = 5, .kboff = 32, .sboff = 1 },
199 { NULL }
200 }
201};