chiark / gitweb /
[PATCH] udev_volume_id: new version of volume_id
[elogind.git] / extras / volume_id / volume_id / iso9660 / 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 #include <asm/types.h>
36
37 #include "../volume_id.h"
38 #include "../logging.h"
39 #include "../util.h"
40 #include "iso9660.h"
41
42 #define ISO_SUPERBLOCK_OFFSET           0x8000
43 #define ISO_SECTOR_SIZE                 0x800
44 #define ISO_VD_OFFSET                   (ISO_SUPERBLOCK_OFFSET + ISO_SECTOR_SIZE)
45 #define ISO_VD_PRIMARY                  0x1
46 #define ISO_VD_SUPPLEMENTARY            0x2
47 #define ISO_VD_END                      0xff
48 #define ISO_VD_MAX                      16
49
50 int volume_id_probe_iso9660(struct volume_id *id, __u64 off)
51 {
52         union iso_super_block {
53                 struct iso_header {
54                         __u8    type;
55                         __u8    id[5];
56                         __u8    version;
57                         __u8    unused1;
58                         __u8            system_id[32];
59                         __u8            volume_id[32];
60                 } __attribute__((__packed__)) iso;
61                 struct hs_header {
62                         __u8    foo[8];
63                         __u8    type;
64                         __u8    id[4];
65                         __u8    version;
66                 } __attribute__((__packed__)) hs;
67         } __attribute__((__packed__)) *is;
68
69         is = (union iso_super_block *) volume_id_get_buffer(id, off + ISO_SUPERBLOCK_OFFSET, 0x200);
70         if (is == NULL)
71                 return -1;
72
73         if (strncmp(is->iso.id, "CD001", 5) == 0) {
74                 char root_label[VOLUME_ID_LABEL_SIZE+1];
75                 int vd_offset;
76                 int i;
77                 int found_svd;
78
79                 memset(root_label, 0, sizeof(root_label));
80                 strncpy(root_label, is->iso.volume_id, sizeof(root_label)-1);
81
82                 found_svd = 0;
83                 vd_offset = ISO_VD_OFFSET;
84                 for (i = 0; i < ISO_VD_MAX; i++) {
85                         is = (union iso_super_block *) volume_id_get_buffer(id, off + vd_offset, 0x200);
86                         if (is == NULL || is->iso.type == ISO_VD_END)
87                                 break;
88                         if (is->iso.type == ISO_VD_SUPPLEMENTARY) {
89                                 dbg("found ISO supplementary VD at offset 0x%llx", off + vd_offset);
90                                 volume_id_set_label_raw(id, is->iso.volume_id, 32);
91                                 volume_id_set_label_unicode16(id, is->iso.volume_id, BE, 32);
92                                 found_svd = 1;
93                                 break;
94                         }
95                         vd_offset += ISO_SECTOR_SIZE;
96                 }
97
98                 if (!found_svd ||
99                     (found_svd && !strncmp(root_label, id->label, 16)))
100                 {
101                         volume_id_set_label_raw(id, root_label, 32);
102                         volume_id_set_label_string(id, root_label, 32);
103                 }
104                 goto found;
105         }
106         if (strncmp(is->hs.id, "CDROM", 5) == 0)
107                 goto found;
108         return -1;
109
110 found:
111         volume_id_set_usage(id, VOLUME_ID_FILESYSTEM);
112         id->type = "iso9660";
113
114         return 0;
115 }