Upload Tizen:Base source
[framework/base/util-linux-ng.git] / shlibs / blkid / src / probers / 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  *
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 "blkidP.h"
20
21 struct volume_descriptor {
22         struct descriptor_tag {
23                 uint16_t        id;
24                 uint16_t        version;
25                 uint8_t         checksum;
26                 uint8_t         reserved;
27                 uint16_t        serial;
28                 uint16_t        crc;
29                 uint16_t        crc_len;
30                 uint32_t        location;
31         } __attribute__((packed)) tag;
32
33         union {
34                 struct anchor_descriptor {
35                         uint32_t        length;
36                         uint32_t        location;
37                 } __attribute__((packed)) anchor;
38
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
48         } __attribute__((packed)) type;
49
50 } __attribute__((packed));
51
52 struct volume_structure_descriptor {
53         uint8_t         type;
54         uint8_t         id[5];
55         uint8_t         version;
56 } __attribute__((packed));
57
58 #define UDF_VSD_OFFSET                  0x8000
59
60 static int probe_udf(blkid_probe pr, const struct blkid_idmag *mag)
61 {
62         struct volume_descriptor *vd;
63         struct volume_structure_descriptor *vsd;
64         unsigned int bs;
65         unsigned int b;
66         unsigned int type;
67         unsigned int count;
68         unsigned int loc;
69
70         /* search Volume Sequence Descriptor (VSD) to get the logical
71          * block size of the volume */
72         for (bs = 0x800; bs < 0x8000; bs += 0x800) {
73                 vsd = (struct volume_structure_descriptor *)
74                         blkid_probe_get_buffer(pr,
75                                         UDF_VSD_OFFSET + bs,
76                                         sizeof(*vsd));
77                 if (!vsd)
78                         return 1;
79                 if (vsd->id[0] != '\0')
80                         goto nsr;
81         }
82         return -1;
83
84 nsr:
85         /* search the list of VSDs for a NSR descriptor */
86         for (b = 0; b < 64; b++) {
87                 vsd = (struct volume_structure_descriptor *)
88                         blkid_probe_get_buffer(pr,
89                                         UDF_VSD_OFFSET + (b * bs),
90                                         sizeof(*vsd));
91                 if (!vsd)
92                         return -1;
93                 if (vsd->id[0] == '\0')
94                         return -1;
95                 if (memcmp(vsd->id, "NSR02", 5) == 0)
96                         goto anchor;
97                 if (memcmp(vsd->id, "NSR03", 5) == 0)
98                         goto anchor;
99         }
100         return -1;
101
102 anchor:
103         /* read Anchor Volume Descriptor (AVDP) */
104         vd = (struct volume_descriptor *)
105                 blkid_probe_get_buffer(pr, 256 * bs, sizeof(*vd));
106         if (!vd)
107                 return -1;
108
109         type = le16_to_cpu(vd->tag.id);
110         if (type != 2) /* TAG_ID_AVDP */
111                 return 0;
112
113         /* get desriptor list address and block count */
114         count = le32_to_cpu(vd->type.anchor.length) / bs;
115         loc = le32_to_cpu(vd->type.anchor.location);
116
117         /* pick the primary descriptor from the list */
118         for (b = 0; b < count; b++) {
119                 vd = (struct volume_descriptor *)
120                         blkid_probe_get_buffer(pr, (loc + b) * bs, sizeof(*vd));
121                 if (!vd)
122                         return -1;
123
124                 type = le16_to_cpu(vd->tag.id);
125                 if (type == 0)
126                         break;
127                 if (le32_to_cpu(vd->tag.location) != loc + b)
128                         break;
129                 if (type == 1) { /* TAG_ID_PVD */
130                         uint8_t clen = vd->type.primary.ident.clen;
131
132                         if (clen == 8)
133                                 blkid_probe_set_label(pr,
134                                                 vd->type.primary.ident.c, 31);
135                         else if (clen == 16)
136                                 blkid_probe_set_utf8label(pr,
137                                                 vd->type.primary.ident.c,
138                                                 31, BLKID_ENC_UTF16BE);
139                 }
140         }
141
142         return 0;
143 }
144
145
146 const struct blkid_idinfo udf_idinfo =
147 {
148         .name           = "udf",
149         .usage          = BLKID_USAGE_FILESYSTEM,
150         .probefunc      = probe_udf,
151         .flags          = BLKID_IDINFO_TOLERANT,
152         .magics         =
153         {
154                 { .magic = "BEA01", .len = 5, .kboff = 32, .sboff = 1 },
155                 { .magic = "BOOT2", .len = 5, .kboff = 32, .sboff = 1 },
156                 { .magic = "CD001", .len = 5, .kboff = 32, .sboff = 1 },
157                 { .magic = "CDW02", .len = 5, .kboff = 32, .sboff = 1 },
158                 { .magic = "NSR02", .len = 5, .kboff = 32, .sboff = 1 },
159                 { .magic = "NSR03", .len = 5, .kboff = 32, .sboff = 1 },
160                 { .magic = "TEA01", .len = 5, .kboff = 32, .sboff = 1 },
161                 { NULL }
162         }
163 };