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