chiark / gitweb /
volume_id: better DDF raid detection
[elogind.git] / extras / volume_id / lib / iso9660.c
1 /*
2  * volume_id - reads filesystem label and uuid
3  *
4  * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #ifndef _GNU_SOURCE
21 #define _GNU_SOURCE 1
22 #endif
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <ctype.h>
30
31 #include "libvolume_id.h"
32 #include "libvolume_id-private.h"
33
34 #define ISO_SUPERBLOCK_OFFSET           0x8000
35 #define ISO_SECTOR_SIZE                 0x800
36 #define ISO_VD_OFFSET                   (ISO_SUPERBLOCK_OFFSET + ISO_SECTOR_SIZE)
37 #define ISO_VD_PRIMARY                  0x1
38 #define ISO_VD_SUPPLEMENTARY            0x2
39 #define ISO_VD_END                      0xff
40 #define ISO_VD_MAX                      16
41
42 struct iso_volume_descriptor {
43         uint8_t         type;
44         uint8_t         id[5];
45         uint8_t         version;
46         uint8_t         flags;
47         uint8_t         system_id[32];
48         uint8_t         volume_id[32];
49         uint8_t         unused[8];
50         uint8_t         space_size[8];
51         uint8_t         escape_sequences[8];
52 } PACKED;
53
54 struct high_sierra_volume_descriptor {
55         uint8_t         foo[8];
56         uint8_t         type;
57         uint8_t         id[5];
58         uint8_t         version;
59 } PACKED;
60
61 int volume_id_probe_iso9660(struct volume_id *id, uint64_t off, uint64_t size)
62 {
63         uint8_t *buf;
64         struct iso_volume_descriptor *is;
65         struct high_sierra_volume_descriptor *hs;
66
67         info("probing at offset 0x%llx\n", (unsigned long long) off);
68
69         buf = volume_id_get_buffer(id, off + ISO_SUPERBLOCK_OFFSET, 0x200);
70         if (buf == NULL)
71                 return -1;
72
73         is = (struct iso_volume_descriptor *) buf;
74
75         if (memcmp(is->id, "CD001", 5) == 0) {
76                 int vd_offset;
77                 int i;
78
79                 dbg("read label from PVD\n");
80                 volume_id_set_label_raw(id, is->volume_id, 32);
81                 volume_id_set_label_string(id, is->volume_id, 32);
82
83                 dbg("looking for SVDs\n");
84                 vd_offset = ISO_VD_OFFSET;
85                 for (i = 0; i < ISO_VD_MAX; i++) {
86                         uint8_t svd_label[64];
87
88                         is = (struct iso_volume_descriptor *) volume_id_get_buffer(id, off + vd_offset, 0x200);
89                         if (is == NULL || is->type == ISO_VD_END)
90                                 break;
91                         if (is->type != ISO_VD_SUPPLEMENTARY)
92                                 continue;
93
94                         dbg("found SVD at offset 0x%llx\n", (unsigned long long) (off + vd_offset));
95                         if (memcmp(is->escape_sequences, "%/@", 3) == 0||
96                             memcmp(is->escape_sequences, "%/C", 3) == 0||
97                             memcmp(is->escape_sequences, "%/E", 3) == 0) {
98                                 dbg("Joliet extension found\n");
99                                 volume_id_set_unicode16(svd_label, sizeof(svd_label), is->volume_id, BE, 32);
100                                 if (memcmp(id->label, svd_label, 16) == 0) {
101                                         dbg("SVD label is identical, use the possibly longer PVD one\n");
102                                         break;
103                                 }
104
105                                 volume_id_set_label_raw(id, is->volume_id, 32);
106                                 volume_id_set_label_string(id, svd_label, 32);
107                                 strcpy(id->type_version, "Joliet Extension");
108                                 goto found;
109                         }
110                         vd_offset += ISO_SECTOR_SIZE;
111                 }
112                 goto found;
113         }
114
115         hs = (struct high_sierra_volume_descriptor *) buf;
116
117         if (memcmp(hs->id, "CDROM", 5) == 0) {
118                 strcpy(id->type_version, "High Sierra");
119                 goto found;
120         }
121
122         return -1;
123
124 found:
125         volume_id_set_usage(id, VOLUME_ID_FILESYSTEM);
126         id->type = "iso9660";
127
128         return 0;
129 }