~ubuntu-branches/ubuntu/trusty/util-linux/trusty-proposed

« back to all changes in this revision

Viewing changes to shlibs/blkid/src/superblocks/udf.c

  • Committer: Package Import Robot
  • Author(s): LaMont Jones
  • Date: 2011-11-03 15:38:23 UTC
  • mto: (4.5.5 sid) (1.6.4)
  • mto: This revision was merged to the branch mainline in revision 85.
  • Revision ID: package-import@ubuntu.com-20111103153823-10sx16jprzxlhkqf
ImportĀ upstreamĀ versionĀ 2.20.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
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 "superblocks.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                  0x8000LL
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 + ((blkid_loff_t) 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,
121
 
                                        (blkid_loff_t) (loc + b) * bs,
122
 
                                        sizeof(*vd));
123
 
                if (!vd)
124
 
                        return -1;
125
 
 
126
 
                type = le16_to_cpu(vd->tag.id);
127
 
                if (type == 0)
128
 
                        break;
129
 
                if (le32_to_cpu(vd->tag.location) != loc + b)
130
 
                        break;
131
 
                if (type == 1) { /* TAG_ID_PVD */
132
 
                        uint8_t clen = vd->type.primary.ident.clen;
133
 
 
134
 
                        if (clen == 8)
135
 
                                blkid_probe_set_label(pr,
136
 
                                                vd->type.primary.ident.c, 31);
137
 
                        else if (clen == 16)
138
 
                                blkid_probe_set_utf8label(pr,
139
 
                                                vd->type.primary.ident.c,
140
 
                                                31, BLKID_ENC_UTF16BE);
141
 
 
142
 
                        if (clen == 8 || clen == 16)
143
 
                                break;
144
 
                }
145
 
        }
146
 
 
147
 
        return 0;
148
 
}
149
 
 
150
 
 
151
 
const struct blkid_idinfo udf_idinfo =
152
 
{
153
 
        .name           = "udf",
154
 
        .usage          = BLKID_USAGE_FILESYSTEM,
155
 
        .probefunc      = probe_udf,
156
 
        .flags          = BLKID_IDINFO_TOLERANT,
157
 
        .magics         =
158
 
        {
159
 
                { .magic = "BEA01", .len = 5, .kboff = 32, .sboff = 1 },
160
 
                { .magic = "BOOT2", .len = 5, .kboff = 32, .sboff = 1 },
161
 
                { .magic = "CD001", .len = 5, .kboff = 32, .sboff = 1 },
162
 
                { .magic = "CDW02", .len = 5, .kboff = 32, .sboff = 1 },
163
 
                { .magic = "NSR02", .len = 5, .kboff = 32, .sboff = 1 },
164
 
                { .magic = "NSR03", .len = 5, .kboff = 32, .sboff = 1 },
165
 
                { .magic = "TEA01", .len = 5, .kboff = 32, .sboff = 1 },
166
 
                { NULL }
167
 
        }
168
 
};