bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 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 | #include "iso9660.h" |
| 21 | |
| 22 | struct 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 | |
| 53 | struct 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 | |
| 61 | static 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; |
| 67 | unsigned int b; |
| 68 | unsigned int type; |
| 69 | unsigned int count; |
| 70 | unsigned int loc; |
| 71 | |
| 72 | /* search Volume Sequence Descriptor (VSD) to get the logical |
| 73 | * block size of the volume */ |
| 74 | for (bs = 0x800; bs < 0x8000; bs += 0x800) { |
| 75 | vsd = (struct volume_structure_descriptor *) |
| 76 | blkid_probe_get_buffer(pr, |
| 77 | UDF_VSD_OFFSET + bs, |
| 78 | sizeof(*vsd)); |
| 79 | if (!vsd) |
| 80 | return 1; |
| 81 | if (vsd->id[0] != '\0') |
| 82 | goto nsr; |
| 83 | } |
| 84 | return -1; |
| 85 | |
| 86 | nsr: |
| 87 | /* search the list of VSDs for a NSR descriptor */ |
| 88 | for (b = 0; b < 64; b++) { |
| 89 | vsd = (struct volume_structure_descriptor *) |
| 90 | blkid_probe_get_buffer(pr, |
| 91 | UDF_VSD_OFFSET + ((blkid_loff_t) b * bs), |
| 92 | sizeof(*vsd)); |
| 93 | if (!vsd) |
| 94 | return -1; |
| 95 | if (vsd->id[0] == '\0') |
| 96 | return -1; |
| 97 | if (memcmp(vsd->id, "NSR02", 5) == 0) |
| 98 | goto anchor; |
| 99 | if (memcmp(vsd->id, "NSR03", 5) == 0) |
| 100 | goto anchor; |
| 101 | } |
| 102 | return -1; |
| 103 | |
| 104 | anchor: |
| 105 | /* read Anchor Volume Descriptor (AVDP) */ |
| 106 | vd = (struct volume_descriptor *) |
| 107 | blkid_probe_get_buffer(pr, 256 * bs, sizeof(*vd)); |
| 108 | if (!vd) |
| 109 | return -1; |
| 110 | |
| 111 | type = le16_to_cpu(vd->tag.id); |
| 112 | if (type != 2) /* TAG_ID_AVDP */ |
| 113 | return 0; |
| 114 | |
| 115 | /* get desriptor list address and block count */ |
| 116 | count = le32_to_cpu(vd->type.anchor.length) / bs; |
| 117 | loc = le32_to_cpu(vd->type.anchor.location); |
| 118 | |
| 119 | /* check if the list is usable */ |
| 120 | for (b = 0; b < count; b++) { |
| 121 | vd = (struct volume_descriptor *) |
| 122 | blkid_probe_get_buffer(pr, |
| 123 | (blkid_loff_t) (loc + b) * bs, |
| 124 | sizeof(*vd)); |
| 125 | if (!vd) |
| 126 | return -1; |
| 127 | } |
| 128 | |
| 129 | /* Try extract all possible ISO9660 information -- if there is |
| 130 | * usable LABEL in ISO header then use it, otherwise read UDF |
| 131 | * specific LABEL */ |
| 132 | if (probe_iso9660(pr, mag) == 0 && |
| 133 | __blkid_probe_lookup_value(pr, "LABEL") != NULL) |
| 134 | return 0; |
| 135 | |
| 136 | /* Read UDF label */ |
| 137 | for (b = 0; b < count; b++) { |
| 138 | vd = (struct volume_descriptor *) |
| 139 | blkid_probe_get_buffer(pr, |
| 140 | (blkid_loff_t) (loc + b) * bs, |
| 141 | sizeof(*vd)); |
| 142 | |
| 143 | type = le16_to_cpu(vd->tag.id); |
| 144 | if (type == 0) |
| 145 | break; |
| 146 | if (le32_to_cpu(vd->tag.location) != loc + b) |
| 147 | break; |
| 148 | if (type == 1) { /* TAG_ID_PVD */ |
| 149 | uint8_t clen = vd->type.primary.ident.clen; |
| 150 | |
| 151 | if (clen == 8) |
| 152 | blkid_probe_set_label(pr, |
| 153 | vd->type.primary.ident.c, 31); |
| 154 | else if (clen == 16) |
| 155 | blkid_probe_set_utf8label(pr, |
| 156 | vd->type.primary.ident.c, |
| 157 | 31, BLKID_ENC_UTF16BE); |
| 158 | |
| 159 | if (clen == 8 || clen == 16) |
| 160 | break; |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | return 0; |
| 165 | } |
| 166 | |
| 167 | |
| 168 | const struct blkid_idinfo udf_idinfo = |
| 169 | { |
| 170 | .name = "udf", |
| 171 | .usage = BLKID_USAGE_FILESYSTEM, |
| 172 | .probefunc = probe_udf, |
| 173 | .flags = BLKID_IDINFO_TOLERANT, |
| 174 | .magics = |
| 175 | { |
| 176 | { .magic = "BEA01", .len = 5, .kboff = 32, .sboff = 1 }, |
| 177 | { .magic = "BOOT2", .len = 5, .kboff = 32, .sboff = 1 }, |
| 178 | { .magic = "CD001", .len = 5, .kboff = 32, .sboff = 1 }, |
| 179 | { .magic = "CDW02", .len = 5, .kboff = 32, .sboff = 1 }, |
| 180 | { .magic = "NSR02", .len = 5, .kboff = 32, .sboff = 1 }, |
| 181 | { .magic = "NSR03", .len = 5, .kboff = 32, .sboff = 1 }, |
| 182 | { .magic = "TEA01", .len = 5, .kboff = 32, .sboff = 1 }, |
| 183 | { NULL } |
| 184 | } |
| 185 | }; |