~pr0gg3d/ubuntu/oneiric/util-linux/bug-805886

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Scott James Remnant
  • Date: 2009-07-16 15:48:23 UTC
  • mfrom: (1.3.7 upstream)
  • Revision ID: james.westby@ubuntu.com-20090716154823-i26fshvs4v8h90qh
Tags: 2.16-1ubuntu1
* Merge from Debian, remaining changes:
  - Since udev is required in Ubuntu, the hwclock.sh init script is
    not called on startup and the hwclockfirst.sh init script is
    removed.
  - Remove /etc/adjtime on upgrade if it was not used.
  - Install custom blkid.conf to use /dev/.blkid.tab since we don't
    expect device names to survive a reboot
  - No lsb_release call in mount.preinst since we'd need Pre-Depends
    (LP: #383697).

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