chiark / gitweb /
8811ffbc03e9e0a0acd6c11ac200fccc4ab6b9bb
[elogind.git] / extras / volume_id / volume_id / 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 library is free software; you can redistribute it and/or
7  *      modify it under the terms of the GNU Lesser General Public
8  *      License as published by the Free Software Foundation; either
9  *      version 2.1 of the License, or (at your option) any later version.
10  *
11  *      This library 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 GNU
14  *      Lesser General Public License for more details.
15  *
16  *      You should have received a copy of the GNU Lesser General Public
17  *      License along with this library; if not, write to the Free Software
18  *      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */
20
21 #ifndef _GNU_SOURCE
22 #define _GNU_SOURCE 1
23 #endif
24
25 #ifdef HAVE_CONFIG_H
26 #  include <config.h>
27 #endif
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32 #include <string.h>
33 #include <errno.h>
34 #include <ctype.h>
35
36 #include "volume_id.h"
37 #include "logging.h"
38 #include "util.h"
39 #include "iso9660.h"
40
41 #define ISO_SUPERBLOCK_OFFSET           0x8000
42 #define ISO_SECTOR_SIZE                 0x800
43 #define ISO_VD_OFFSET                   (ISO_SUPERBLOCK_OFFSET + ISO_SECTOR_SIZE)
44 #define ISO_VD_PRIMARY                  0x1
45 #define ISO_VD_SUPPLEMENTARY            0x2
46 #define ISO_VD_END                      0xff
47 #define ISO_VD_MAX                      16
48
49 struct iso_volume_descriptor {
50         uint8_t         vd_type;
51         uint8_t         vd_id[5];
52         uint8_t         vd_version;
53         uint8_t         flags;
54         uint8_t         system_id[32];
55         uint8_t         volume_id[32];
56         uint8_t         unused[8];
57         uint8_t         space_size[8];
58         uint8_t         escape_sequences[8];
59 } __attribute__((__packed__));
60
61 struct high_sierra_volume_descriptor {
62         uint8_t         foo[8];
63         uint8_t         type;
64         uint8_t         id[4];
65         uint8_t         version;
66 } __attribute__((__packed__));
67
68 int volume_id_probe_iso9660(struct volume_id *id, uint64_t off)
69 {
70         uint8_t *buf;
71         struct iso_volume_descriptor *is;
72         struct high_sierra_volume_descriptor *hs;
73
74         dbg("probing at offset 0x%llx", (unsigned long long) off);
75
76         buf = volume_id_get_buffer(id, off + ISO_SUPERBLOCK_OFFSET, 0x200);
77         if (buf == NULL)
78                 return -1;
79
80         is = (struct iso_volume_descriptor *) buf;
81
82         if (memcmp(is->vd_id, "CD001", 5) == 0) {
83                 int vd_offset;
84                 int i;
85
86                 dbg("read label from PVD");
87                 volume_id_set_label_raw(id, is->volume_id, 32);
88                 volume_id_set_label_string(id, is->volume_id, 32);
89
90                 dbg("looking for SVDs");
91                 vd_offset = ISO_VD_OFFSET;
92                 for (i = 0; i < ISO_VD_MAX; i++) {
93                         uint8_t svd_label[64];
94
95                         is = (struct iso_volume_descriptor *) volume_id_get_buffer(id, off + vd_offset, 0x200);
96                         if (is == NULL || is->vd_type == ISO_VD_END)
97                                 break;
98                         if (is->vd_type != ISO_VD_SUPPLEMENTARY)
99                                 continue;
100
101                         dbg("found SVD at offset 0x%llx", (unsigned long long) (off + vd_offset));
102                         if (memcmp(is->escape_sequences, "%/@", 3) == 0||
103                             memcmp(is->escape_sequences, "%/C", 3) == 0||
104                             memcmp(is->escape_sequences, "%/E", 3) == 0) {
105                                 dbg("Joliet extension found");
106                                 volume_id_set_unicode16((char *)svd_label, sizeof(svd_label), is->volume_id, BE, 32);
107                                 if (memcmp(id->label, svd_label, 16) == 0) {
108                                         dbg("SVD label is identical, use the possibly longer PVD one");
109                                         break;
110                                 }
111
112                                 volume_id_set_label_raw(id, is->volume_id, 32);
113                                 volume_id_set_label_string(id, svd_label, 32);
114                                 strcpy(id->type_version, "Joliet Extension");
115                                 goto found;
116                         }
117                         vd_offset += ISO_SECTOR_SIZE;
118                 }
119                 goto found;
120         }
121
122         hs = (struct high_sierra_volume_descriptor *) buf;
123
124         if (memcmp(hs->id, "CDROM", 5) == 0) {
125                 strcpy(id->type_version, "High Sierra");
126                 goto found;
127         }
128
129         return -1;
130
131 found:
132         volume_id_set_usage(id, VOLUME_ID_FILESYSTEM);
133         id->type = "iso9660";
134
135         return 0;
136 }