1 /* $NetBSD: cd9660_debug.c,v 1.11 2010/10/27 18:51:35 christos Exp $ */
2
3 /*-
4 * SPDX-License-Identifier: BSD-2-Clause
5 *
6 * Copyright (c) 2005 Daniel Watt, Walter Deignan, Ryan Gabrys, Alan
7 * Perez-Rathke and Ram Vedam. All rights reserved.
8 *
9 * This code was written by Daniel Watt, Walter Deignan, Ryan Gabrys,
10 * Alan Perez-Rathke and Ram Vedam.
11 *
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
14 * conditions are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above
18 * copyright notice, this list of conditions and the following
19 * disclaimer in the documentation and/or other materials provided
20 * with the distribution.
21 *
22 * THIS SOFTWARE IS PROVIDED BY DANIEL WATT, WALTER DEIGNAN, RYAN
23 * GABRYS, ALAN PEREZ-RATHKE AND RAM VEDAM ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 * DISCLAIMED. IN NO EVENT SHALL DANIEL WATT, WALTER DEIGNAN, RYAN
27 * GABRYS, ALAN PEREZ-RATHKE AND RAM VEDAM BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
30 * USE,DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
34 * OF SUCH DAMAGE.
35 */
36
37 #include <sys/cdefs.h>
38 #include <sys/param.h>
39
40 #include <sys/mount.h>
41
42 #include "makefs.h"
43 #include "cd9660.h"
44 #include "iso9660_rrip.h"
45
46 static void debug_print_susp_attrs(cd9660node *, int);
47 static void debug_dump_to_xml_padded_hex_output(const char *, const char *,
48 int);
49
50 static inline void
print_n_tabs(int n)51 print_n_tabs(int n)
52 {
53 int i;
54
55 for (i = 1; i <= n; i ++)
56 printf("\t");
57 }
58
59 #if 0
60 void
61 debug_print_rrip_info(cd9660node *n)
62 {
63 struct ISO_SUSP_ATTRIBUTES *t;
64 TAILQ_FOREACH(t, &node->head, rr_ll) {
65
66 }
67 }
68 #endif
69
70 static void
debug_print_susp_attrs(cd9660node * n,int indent)71 debug_print_susp_attrs(cd9660node *n, int indent)
72 {
73 struct ISO_SUSP_ATTRIBUTES *t;
74
75 TAILQ_FOREACH(t, &n->head, rr_ll) {
76 print_n_tabs(indent);
77 printf("-");
78 printf("%c%c: L:%i",t->attr.su_entry.SP.h.type[0],
79 t->attr.su_entry.SP.h.type[1],
80 (int)t->attr.su_entry.SP.h.length[0]);
81 printf("\n");
82 }
83 }
84
85 void
debug_print_tree(iso9660_disk * diskStructure,cd9660node * node,int level)86 debug_print_tree(iso9660_disk *diskStructure, cd9660node *node, int level)
87 {
88 #if !HAVE_NBTOOL_CONFIG_H
89 cd9660node *cn;
90
91 print_n_tabs(level);
92 if (node->type & CD9660_TYPE_DOT) {
93 printf(". (%i)\n",
94 isonum_733(node->isoDirRecord->extent));
95 } else if (node->type & CD9660_TYPE_DOTDOT) {
96 printf("..(%i)\n",
97 isonum_733(node->isoDirRecord->extent));
98 } else if (node->isoDirRecord->name[0]=='\0') {
99 printf("(ROOT) (%" PRIu32 " to %" PRId64 ")\n",
100 node->fileDataSector,
101 node->fileDataSector +
102 node->fileSectorsUsed - 1);
103 } else {
104 printf("%s (%s) (%" PRIu32 " to %" PRId64 ")\n",
105 node->isoDirRecord->name,
106 (node->isoDirRecord->flags[0]
107 & ISO_FLAG_DIRECTORY) ? "DIR" : "FILE",
108 node->fileDataSector,
109 (node->fileSectorsUsed == 0) ?
110 node->fileDataSector :
111 node->fileDataSector
112 + node->fileSectorsUsed - 1);
113 }
114 if (diskStructure->rock_ridge_enabled)
115 debug_print_susp_attrs(node, level + 1);
116 TAILQ_FOREACH(cn, &node->cn_children, cn_next_child)
117 debug_print_tree(diskStructure, cn, level + 1);
118 #else
119 printf("Sorry, debugging is not supported in host-tools mode.\n");
120 #endif
121 }
122
123 void
debug_print_path_tree(cd9660node * n)124 debug_print_path_tree(cd9660node *n)
125 {
126 cd9660node *iterator = n;
127
128 /* Only display this message when called with the root node */
129 if (n->parent == NULL)
130 printf("debug_print_path_table: Dumping path table contents\n");
131
132 while (iterator != NULL) {
133 if (iterator->isoDirRecord->name[0] == '\0')
134 printf("0) (ROOT)\n");
135 else
136 printf("%i) %s\n", iterator->level,
137 iterator->isoDirRecord->name);
138
139 iterator = iterator->ptnext;
140 }
141 }
142
143 void
debug_print_volume_descriptor_information(iso9660_disk * diskStructure)144 debug_print_volume_descriptor_information(iso9660_disk *diskStructure)
145 {
146 volume_descriptor *tmp = diskStructure->firstVolumeDescriptor;
147 char temp[CD9660_SECTOR_SIZE];
148
149 printf("==Listing Volume Descriptors==\n");
150
151 while (tmp != NULL) {
152 memset(temp, 0, CD9660_SECTOR_SIZE);
153 memcpy(temp, tmp->volumeDescriptorData + 1, 5);
154 printf("Volume descriptor in sector %" PRId64
155 ": type %i, ID %s\n",
156 tmp->sector, tmp->volumeDescriptorData[0], temp);
157 switch(tmp->volumeDescriptorData[0]) {
158 case 0:/*boot record*/
159 break;
160
161 case 1: /* PVD */
162 break;
163
164 case 2: /* SVD */
165 break;
166
167 case 3: /* Volume Partition Descriptor */
168 break;
169
170 case 255: /* terminator */
171 break;
172 }
173 tmp = tmp->next;
174 }
175
176 printf("==Done Listing Volume Descriptors==\n");
177 }
178
179 void
debug_dump_to_xml_ptentry(path_table_entry * pttemp,int num,int mode)180 debug_dump_to_xml_ptentry(path_table_entry *pttemp, int num, int mode)
181 {
182 printf("<ptentry num=\"%i\">\n" ,num);
183 printf("<length>%i</length>\n", pttemp->length[0]);
184 printf("<extended_attribute_length>%i</extended_attribute_length>\n",
185 pttemp->extended_attribute_length[0]);
186 printf("<parent_number>%i</parent_number>\n",
187 debug_get_encoded_number(pttemp->parent_number,mode));
188 debug_dump_to_xml_padded_hex_output("name",
189 pttemp->name, pttemp->length[0]);
190 printf("</ptentry>\n");
191 }
192
193 void
debug_dump_to_xml_path_table(FILE * fd,off_t sector,int size,int mode)194 debug_dump_to_xml_path_table(FILE *fd, off_t sector, int size, int mode)
195 {
196 path_table_entry pttemp;
197 int t = 0;
198 int n = 0;
199
200 if (fseeko(fd, CD9660_SECTOR_SIZE * sector, SEEK_SET) == -1)
201 err(1, "fseeko");
202
203 while (t < size) {
204 /* Read fixed data first */
205 fread(&pttemp, 1, 8, fd);
206 t += 8;
207 /* Read variable */
208 fread(((unsigned char*)&pttemp) + 8, 1, pttemp.length[0], fd);
209 t += pttemp.length[0];
210 debug_dump_to_xml_ptentry(&pttemp, n, mode);
211 n++;
212 }
213
214 }
215
216 /*
217 * XML Debug output functions
218 * Dump hierarchy of CD, as well as volume info, to XML
219 * Can be used later to diff against a standard,
220 * or just provide easy to read detailed debug output
221 */
222 void
debug_dump_to_xml(FILE * fd)223 debug_dump_to_xml(FILE *fd)
224 {
225 unsigned char buf[CD9660_SECTOR_SIZE];
226 off_t sector;
227 int t, t2;
228 struct iso_primary_descriptor primaryVD;
229 struct _boot_volume_descriptor bootVD;
230
231 printf("<cd9660dump>\n");
232
233 /* Display Volume Descriptors */
234 sector = 16;
235 do {
236 if (fseeko(fd, CD9660_SECTOR_SIZE * sector, SEEK_SET) == -1)
237 err(1, "fseeko");
238 fread(buf, 1, CD9660_SECTOR_SIZE, fd);
239 t = (int)((unsigned char)buf[0]);
240 switch (t) {
241 case 0:
242 memcpy(&bootVD, buf, CD9660_SECTOR_SIZE);
243 break;
244 case 1:
245 memcpy(&primaryVD, buf, CD9660_SECTOR_SIZE);
246 break;
247 }
248 debug_dump_to_xml_volume_descriptor(buf, sector);
249 sector++;
250 } while (t != 255);
251
252 t = debug_get_encoded_number((u_char *)primaryVD.type_l_path_table,
253 731);
254 t2 = debug_get_encoded_number((u_char *)primaryVD.path_table_size, 733);
255 printf("Path table 1 located at sector %i and is %i bytes long\n",
256 t,t2);
257 debug_dump_to_xml_path_table(fd, t, t2, 721);
258
259 t = debug_get_encoded_number((u_char *)primaryVD.type_m_path_table,
260 731);
261 debug_dump_to_xml_path_table(fd, t, t2, 722);
262
263 printf("</cd9660dump>\n");
264 }
265
266 static void
debug_dump_to_xml_padded_hex_output(const char * element,const char * buf,int len)267 debug_dump_to_xml_padded_hex_output(const char *element, const char *buf,
268 int len)
269 {
270 int i;
271 int t;
272
273 printf("<%s>",element);
274 for (i = 0; i < len; i++) {
275 t = (unsigned char)buf[i];
276 if (t >= 32 && t < 127)
277 printf("%c",t);
278 }
279 printf("</%s>\n",element);
280
281 printf("<%s:hex>",element);
282 for (i = 0; i < len; i++) {
283 t = (unsigned char)buf[i];
284 printf(" %x",t);
285 }
286 printf("</%s:hex>\n",element);
287 }
288
289 int
debug_get_encoded_number(const unsigned char * buf,int mode)290 debug_get_encoded_number(const unsigned char* buf, int mode)
291 {
292 #if !HAVE_NBTOOL_CONFIG_H
293 switch (mode) {
294 /* 711: Single bite */
295 case 711:
296 return isonum_711(buf);
297
298 /* 712: Single signed byte */
299 case 712:
300 return isonum_712(buf);
301
302 /* 721: 16 bit LE */
303 case 721:
304 return isonum_721(buf);
305
306 /* 731: 32 bit LE */
307 case 731:
308 return isonum_731(buf);
309
310 /* 722: 16 bit BE */
311 case 722:
312 return isonum_722(buf);
313
314 /* 732: 32 bit BE */
315 case 732:
316 return isonum_732(buf);
317
318 /* 723: 16 bit bothE */
319 case 723:
320 return isonum_723(buf);
321
322 /* 733: 32 bit bothE */
323 case 733:
324 return isonum_733(buf);
325 }
326 #endif
327 return 0;
328 }
329
330 void
debug_dump_integer(const char * element,const unsigned char * buf,int mode)331 debug_dump_integer(const char *element, const unsigned char* buf, int mode)
332 {
333 printf("<%s>%i</%s>\n", element, debug_get_encoded_number(buf, mode),
334 element);
335 }
336
337 void
debug_dump_string(const char * element __unused,const unsigned char * buf __unused,int len __unused)338 debug_dump_string(const char *element __unused, const unsigned char *buf __unused, int len __unused)
339 {
340
341 }
342
343 void
debug_dump_directory_record_9_1(unsigned char * buf)344 debug_dump_directory_record_9_1(unsigned char* buf)
345 {
346 struct iso_directory_record *rec = (struct iso_directory_record *)buf;
347 printf("<directoryrecord>\n");
348 debug_dump_integer("length", rec->length, 711);
349 debug_dump_integer("ext_attr_length", rec->ext_attr_length, 711);
350 debug_dump_integer("extent", rec->extent, 733);
351 debug_dump_integer("size", rec->size, 733);
352 debug_dump_integer("flags", rec->flags, 711);
353 debug_dump_integer("file_unit_size", rec->file_unit_size, 711);
354 debug_dump_integer("interleave", rec->interleave, 711);
355 debug_dump_integer("volume_sequence_number",
356 rec->volume_sequence_number, 723);
357 debug_dump_integer("name_len", rec->name_len, 711);
358 debug_dump_to_xml_padded_hex_output("name", rec->name,
359 debug_get_encoded_number(rec->length, 711));
360 printf("</directoryrecord>\n");
361 }
362
363
364 void
debug_dump_to_xml_volume_descriptor(unsigned char * buf,int sector)365 debug_dump_to_xml_volume_descriptor(unsigned char* buf, int sector)
366 {
367 struct iso_primary_descriptor *desc =
368 (struct iso_primary_descriptor *)buf;
369
370 printf("<volumedescriptor sector=\"%i\">\n", sector);
371 printf("<vdtype>");
372 switch(buf[0]) {
373 case 0:
374 printf("boot");
375 break;
376
377 case 1:
378 printf("primary");
379 break;
380
381 case 2:
382 printf("supplementary");
383 break;
384
385 case 3:
386 printf("volume partition descriptor");
387 break;
388
389 case 255:
390 printf("terminator");
391 break;
392 }
393
394 printf("</vdtype>\n");
395 switch(buf[0]) {
396 case 1:
397 debug_dump_integer("type", desc->type, 711);
398 debug_dump_to_xml_padded_hex_output("id", desc->id,
399 ISODCL(2, 6));
400 debug_dump_integer("version", (u_char *)desc->version, 711);
401 debug_dump_to_xml_padded_hex_output("system_id",
402 desc->system_id, ISODCL(9, 40));
403 debug_dump_to_xml_padded_hex_output("volume_id",
404 desc->volume_id, ISODCL(41, 72));
405 debug_dump_integer("volume_space_size",
406 (u_char *)desc->volume_space_size, 733);
407 debug_dump_integer("volume_set_size",
408 (u_char *)desc->volume_set_size, 733);
409 debug_dump_integer("volume_sequence_number",
410 (u_char *)desc->volume_sequence_number, 723);
411 debug_dump_integer("logical_block_size",
412 (u_char *)desc->logical_block_size, 723);
413 debug_dump_integer("path_table_size",
414 (u_char *)desc->path_table_size, 733);
415 debug_dump_integer("type_l_path_table",
416 (u_char *)desc->type_l_path_table, 731);
417 debug_dump_integer("opt_type_l_path_table",
418 (u_char *)desc->opt_type_l_path_table, 731);
419 debug_dump_integer("type_m_path_table",
420 (u_char *)desc->type_m_path_table, 732);
421 debug_dump_integer("opt_type_m_path_table",
422 (u_char *)desc->opt_type_m_path_table, 732);
423 debug_dump_directory_record_9_1(
424 (u_char *)desc->root_directory_record);
425 debug_dump_to_xml_padded_hex_output("volume_set_id",
426 desc->volume_set_id, ISODCL(191, 318));
427 debug_dump_to_xml_padded_hex_output("publisher_id",
428 desc->publisher_id, ISODCL(319, 446));
429 debug_dump_to_xml_padded_hex_output("preparer_id",
430 desc->preparer_id, ISODCL(447, 574));
431 debug_dump_to_xml_padded_hex_output("application_id",
432 desc->application_id, ISODCL(575, 702));
433 debug_dump_to_xml_padded_hex_output("copyright_file_id",
434 desc->copyright_file_id, ISODCL(703, 739));
435 debug_dump_to_xml_padded_hex_output("abstract_file_id",
436 desc->abstract_file_id, ISODCL(740, 776));
437 debug_dump_to_xml_padded_hex_output("bibliographic_file_id",
438 desc->bibliographic_file_id, ISODCL(777, 813));
439
440 debug_dump_to_xml_padded_hex_output("creation_date",
441 desc->creation_date, ISODCL(814, 830));
442 debug_dump_to_xml_padded_hex_output("modification_date",
443 desc->modification_date, ISODCL(831, 847));
444 debug_dump_to_xml_padded_hex_output("expiration_date",
445 desc->expiration_date, ISODCL(848, 864));
446 debug_dump_to_xml_padded_hex_output("effective_date",
447 desc->effective_date, ISODCL(865, 881));
448
449 debug_dump_to_xml_padded_hex_output("file_structure_version",
450 desc->file_structure_version, ISODCL(882, 882));
451 break;
452 }
453 printf("</volumedescriptor>\n");
454 }
455
456