Debian Codegraph @

cdrom.c

raw symbols SCIP
1 /*
2 * cdrom.c
3 * Detection of file systems for CD-ROM and similar media
4 *
5 * Copyright (c) 2003-2006 Christoph Pfisterer
6 *
7 * Permission is hereby granted, free of charge, to any person
8 * obtaining a copy of this software and associated documentation
9 * files (the "Software"), to deal in the Software without
10 * restriction, including without limitation the rights to use, copy,
11 * modify, merge, publish, distribute, sublicense, and/or sell copies
12 * of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
22 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
23 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * SOFTWARE.
26 */
27
28 #include "global.h"
29
30 /*
31 * sub-functions
32 */
33
34 static void dump_boot_catalog(SECTION *section, u8 pos, int level);
35
36 /*
37 * ISO9660 file system
38 */
39
40 void detect_iso(SECTION *section, int level)
41 {
42 char s[256], t[256];
43 int i, sector, type;
44 u4 blocksize;
45 u8 blocks, bcpos;
46 unsigned char *buf;
47
48 /* get the volume descriptor */
49 if (get_buffer(section, 32768, 2048, (void **)&buf) < 2048)
50 return;
51
52 /* check signature */
53 if (memcmp(buf, "\001CD001", 6) != 0)
54 return;
55
56 print_line(level, "ISO9660 file system");
57
58 /* read Volume ID and other info */
59 get_padded_string(buf + 40, 32, ' ', s);
60 print_line(level+1, "Volume name \"%s\"", s);
61
62 get_padded_string(buf + 318, 128, ' ', s);
63 if (s[0])
64 print_line(level+1, "Publisher \"%s\"", s);
65
66 get_padded_string(buf + 446, 128, ' ', s);
67 if (s[0])
68 print_line(level+1, "Preparer \"%s\"", s);
69
70 get_padded_string(buf + 574, 128, ' ', s);
71 if (s[0])
72 print_line(level+1, "Application \"%s\"", s);
73
74 /* some other interesting facts */
75 blocks = get_le_long(buf + 80);
76 blocksize = get_le_short(buf + 128);
77 format_blocky_size(s, blocks, blocksize, "blocks", NULL);
78 print_line(level+1, "Data size %s", s);
79
80 for (sector = 17; ; sector++) {
81 /* get next descriptor */
82 if (get_buffer(section, sector * 2048, 2048, (void **)&buf) < 2048)
83 return;
84
85 /* check signature */
86 if (memcmp(buf + 1, "CD001", 5) != 0) {
87 print_line(level+1, "Signature missing in sector %d", sector);
88 return;
89 }
90 type = buf[0];
91 if (type == 255)
92 break;
93
94 switch (type) {
95
96 case 0: /* El Torito */
97 /* check signature */
98 if (memcmp(buf+7, "EL TORITO SPECIFICATION", 23) != 0) {
99 print_line(level+1, "Boot record of unknown format");
100 break;
101 }
102
103 bcpos = get_le_long(buf + 0x47);
104 print_line(level+1, "El Torito boot record, catalog at %llu", bcpos);
105
106 /* boot catalog */
107 dump_boot_catalog(section, bcpos * 2048, level + 2);
108
109 break;
110
111 case 1: /* Primary Volume Descriptor */
112 print_line(level+1, "Additional Primary Volume Descriptor");
113 break;
114
115 case 2: /* Supplementary Volume Descriptor, Joliet */
116 /* read Volume ID */
117 format_utf16_be(buf + 40, 32, t);
118 for (i = strlen(t)-1; i >= 0 && t[i] == ' '; i--)
119 t[i] = 0;
120 print_line(level+1, "Joliet extension, volume name \"%s\"", t);
121 break;
122
123 case 3: /* Volume Partition Descriptor */
124 print_line(level+1, "Volume Partition Descriptor");
125 break;
126
127 default:
128 print_line(level+1, "Descriptor type %d at sector %d", type, sector);
129 break;
130 }
131 }
132 }
133
134 /*
135 * El Torito boot catalog
136 */
137
138 static char *media_types[16] = {
139 "non-emulated",
140 "1.2M floppy",
141 "1.44M floppy",
142 "2.88M floppy",
143 "hard disk",
144 "reserved type 5", "reserved type 6", "reserved type 7",
145 "reserved type 8", "reserved type 9", "reserved type 10",
146 "reserved type 11", "reserved type 12", "reserved type 13",
147 "reserved type 14", "reserved type 15"
148 };
149
150 static char * get_name_for_eltorito_platform(int id)
151 {
152 if (id == 0)
153 return "x86";
154 if (id == 1)
155 return "PowerPC";
156 if (id == 2)
157 return "Macintosh";
158 if (id == 0xEF)
159 return "EFI";
160 return "unknown";
161 }
162
163 static void dump_boot_catalog(SECTION *section, u8 pos, int level)
164 {
165 unsigned char *buf;
166 int bootable, media, platform, system_type;
167 u4 start, preload;
168 int entry, maxentry, off;
169 char s[256];
170
171 /* get boot catalog */
172 if (get_buffer(section, pos, 2048, (void **)&buf) < 2048)
173 return;
174
175 /* check validation entry (must be first) */
176 if (buf[0] != 0x01 || buf[30] != 0x55 || buf[31] != 0xAA) {
177 print_line(level, "Validation entry missing");
178 return;
179 }
180 /* TODO: check checksum of the validation entry */
181 platform = buf[1];
182 /* TODO: ID string at bytes 0x04 - 0x1B */
183
184 maxentry = 2;
185 for (entry = 1; entry < maxentry + 1; entry++) {
186 if ((entry & 63) == 0) {
187 /* get the next CD sector */
188 if (get_buffer(section, pos + (entry / 64) * 2048, 2048, (void **)&buf) < 2048)
189 return;
190 }
191 off = (entry * 32) % 2048;
192
193 if (entry >= maxentry) {
194 if (buf[off] == 0x88) /* more bootable entries without proper section headers */
195 maxentry++;
196 else
197 break;
198 }
199
200 if (entry == 1) {
201 if (!(buf[off] == 0x88 || buf[off] == 0x00)) {
202 print_line(level, "Initial/Default entry missing");
203 break;
204 }
205 if (buf[off + 32] == 0x90 || buf[off + 32] == 0x91)
206 maxentry = 3;
207 }
208
209 if (buf[off] == 0x88 || buf[off] == 0x00) { /* boot entry */
210 bootable = (buf[off] == 0x88) ? 1 : 0;
211 media = buf[off + 1] & 15;
212 system_type = buf[off + 4];
213 start = get_le_long(buf + off + 8);
214 preload = get_le_short(buf + off + 6);
215
216 /* print and analyze further */
217 format_size(s, preload * 512);
218 print_line(level, "%s %s image, starts at %lu, preloads %s",
219 bootable ? "Bootable" : "Non-bootable",
220 media_types[media], start, s);
221 print_line(level + 1, "Platform 0x%02X (%s), System Type 0x%02X (%s)",
222 platform, get_name_for_eltorito_platform(platform),
223 system_type, get_name_for_mbrtype(system_type));
224 if (start > 0) {
225 analyze_recursive(section, level + 1,
226 (u8)start * 2048, 0, 0);
227 /* TODO: calculate size in some way */
228 }
229
230 } else if (buf[off] == 0x44) { /* entry extension */
231 maxentry++; /* doesn't count against the entry count from the last section header */
232
233 } else if (buf[off] == 0x90 || buf[off] == 0x91) { /* section header */
234 platform = buf[off + 1];
235 maxentry = entry + 1 + get_le_short(buf + off + 2)
236 + ((buf[off] == 0x90) ? 1 : 0);
237 /* TODO: ID string at bytes 0x04 - 0x1F */
238
239 } else {
240 print_line(level, "Unknown entry type 0x%02X", buf[off]);
241 break;
242 }
243 }
244 }
245
246 /*
247 * Various CD file systems
248 */
249
250 void detect_cdrom_misc(SECTION *section, int level)
251 {
252 unsigned char *buf;
253
254 /* get first sector */
255 if (get_buffer(section, 0, 2048, (void **)&buf) < 2048)
256 return;
257
258 /* Sega Dreamcast signature */
259 if (memcmp(buf, "SEGA SEGAKATANA SEGA ENTERPRISES", 32) == 0) {
260 print_line(level, "Sega Dreamcast signature");
261 }
262
263 /* 3DO filesystem */
264 if (memcmp(buf, "\x01\x5a\x5a\x5a\x5a\x5a\x01\x00", 8) == 0 &&
265 memcmp(buf + 0x28, "CD-ROM", 6) == 0) {
266 print_line(level, "3DO CD-ROM file system");
267 }
268
269 /* get sector 32 */
270 if (get_buffer(section, 32*2048, 2048, (void **)&buf) < 2048)
271 return;
272
273 /* Xbox DVD file system */
274 if (memcmp(buf, "MICROSOFT*XBOX*MEDIA", 20) == 0 &&
275 memcmp(buf + 0x7ec, "MICROSOFT*XBOX*MEDIA", 20) == 0) {
276 print_line(level, "Xbox DVD file system");
277 }
278 }
279
280 /* EOF */