xref: /netbsd-src/usr.sbin/makefs/cd9660/cd9660_debug.c (revision d48f14661dda8638fee055ba15d35bdfb29b9fa8)
1 /*	$NetBSD: cd9660_debug.c,v 1.4 2005/12/24 20:56:41 perry 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.4 2005/12/24 20:56:41 perry 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 	TAILQ_FOREACH(t, &node->head, rr_ll) {
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 	TAILQ_FOREACH(t, &n->head, rr_ll) {
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 *node, int level)
96 {
97 	cd9660node *cn;
98 
99 	print_n_tabs(level);
100 	if (node->type & CD9660_TYPE_DOT) {
101 		printf(". (%i)\n",
102 		    isonum_733(node->isoDirRecord->extent));
103 	} else if (node->type & CD9660_TYPE_DOTDOT) {
104 		printf("..(%i)\n",
105 		    isonum_733(node->isoDirRecord->extent));
106 	} else if (node->isoDirRecord->name[0]=='\0') {
107 		printf("(ROOT) (%i to %i)\n",
108 		    node->fileDataSector,
109 		    node->fileDataSector +
110 			node->fileSectorsUsed - 1);
111 	} else {
112 		printf("%s (%s) (%i to %i)\n",
113 		    node->isoDirRecord->name,
114 		    (node->isoDirRecord->flags[0]
115 			& ISO_FLAG_DIRECTORY) ?  "DIR" : "FILE",
116 		    node->fileDataSector,
117 		    (node->fileSectorsUsed == 0) ?
118 			node->fileDataSector :
119 			node->fileDataSector
120 			    + node->fileSectorsUsed - 1);
121 	}
122 	if (diskStructure.rock_ridge_enabled)
123 		debug_print_susp_attrs(node, level + 1);
124 	TAILQ_FOREACH(cn, &node->cn_children, cn_next_child)
125 		debug_print_tree(cn, level + 1);
126 }
127 
128 void
129 debug_print_path_tree(cd9660node *n)
130 {
131 	cd9660node *iterator = n;
132 
133 	/* Only display this message when called with the root node */
134 	if (n->parent == NULL)
135 		printf("debug_print_path_table: Dumping path table contents\n");
136 
137 	while (iterator != NULL) {
138 		if (iterator->isoDirRecord->name[0] == '\0')
139 			printf("0) (ROOT)\n");
140 		else
141 			printf("%i) %s\n", iterator->level,
142 			    iterator->isoDirRecord->name);
143 
144 		iterator = iterator->ptnext;
145 	}
146 }
147 
148 void
149 debug_print_volume_descriptor_information(void)
150 {
151 	volume_descriptor *tmp = diskStructure.firstVolumeDescriptor;
152 	char temp[2048];
153 
154 	printf("==Listing Volume Descriptors==\n");
155 
156 	while (tmp != NULL) {
157 		memset(temp, 0, 2048);
158 		memcpy(temp, tmp->volumeDescriptorData + 1, 5);
159 		printf("Volume descriptor in sector %i: type %i, ID %s\n",
160 		    tmp->sector, tmp->volumeDescriptorData[0], temp);
161 		switch(tmp->volumeDescriptorData[0]) {
162 		case 0:/*boot record*/
163 			break;
164 
165 		case 1:		/* PVD */
166 			break;
167 
168 		case 2:		/* SVD */
169 			break;
170 
171 		case 3:		/* Volume Partition Descriptor */
172 			break;
173 
174 		case 255:	/* terminator */
175 			break;
176 		}
177 		tmp = tmp->next;
178 	}
179 
180 	printf("==Done Listing Volume Descriptors==\n");
181 }
182 
183 void
184 debug_dump_to_xml_ptentry(path_table_entry *pttemp, int num, int mode)
185 {
186 	printf("<ptentry num=\"%i\">\n" ,num);
187 	printf("<length>%i</length>\n", pttemp->length[0]);
188 	printf("<extended_attribute_length>%i</extended_attribute_length>\n",
189 	    pttemp->extended_attribute_length[0]);
190 	printf("<parent_number>%i</parent_number>\n",
191 	    debug_get_encoded_number(pttemp->parent_number,mode));
192 	debug_dump_to_xml_padded_hex_output("name",
193 	    pttemp->name, pttemp->length[0]);
194 	printf("</ptentry>\n");
195 }
196 
197 void
198 debug_dump_to_xml_path_table(FILE *fd, int sector, int size, int mode)
199 {
200 	path_table_entry pttemp;
201 	int t = 0;
202 	int n = 0;
203 
204 	fseek(fd, 2048 * sector, SEEK_SET);
205 
206 	while (t < size) {
207 		/* Read fixed data first */
208 		fread(&pttemp, 1, 8, fd);
209 		t += 8;
210 		/* Read variable */
211 		fread(((unsigned char*)&pttemp) + 8, 1, pttemp.length[0], fd);
212 		t += pttemp.length[0];
213 		debug_dump_to_xml_ptentry(&pttemp, n, mode);
214 		n++;
215 	}
216 
217 }
218 
219 /*
220  * XML Debug output functions
221  * Dump hierarchy of CD, as well as volume info, to XML
222  * Can be used later to diff against a standard,
223  * or just provide easy to read detailed debug output
224  */
225 void
226 debug_dump_to_xml(FILE *fd)
227 {
228 	unsigned char buf[2048];
229 	int sector;
230 	int t, t2;
231 	struct iso_primary_descriptor primaryVD;
232 	struct _boot_volume_descriptor bootVD;
233 
234 	printf("<cd9660dump>\n");
235 
236 	/* Display Volume Descriptors */
237 	sector = 16;
238 	do {
239 		fseek(fd, 2048*sector, SEEK_SET);
240 		fread(buf, 1, 2048, fd);
241 		t = (int)((unsigned char)buf[0]);
242 		switch (t) {
243 		case 0:
244 			memcpy(&bootVD, buf, 2048);
245 			break;
246 		case 1:
247 			memcpy(&primaryVD, buf, 2048);
248 			break;
249 		}
250 		debug_dump_to_xml_volume_descriptor(buf, sector);
251 		sector++;
252 	} while (t != 255);
253 
254 	t = debug_get_encoded_number((u_char *)primaryVD.type_l_path_table,
255 	    731);
256 	t2 = debug_get_encoded_number((u_char *)primaryVD.path_table_size, 733);
257 	printf("Path table 1 located at sector %i and is %i bytes long\n",
258 	    t,t2);
259 	debug_dump_to_xml_path_table(fd, t, t2, 721);
260 
261 	t = debug_get_encoded_number((u_char *)primaryVD.type_m_path_table,
262 	    731);
263 	debug_dump_to_xml_path_table(fd, t, t2, 722);
264 
265 	printf("</cd9660dump>\n");
266 }
267 
268 static void
269 debug_dump_to_xml_padded_hex_output(const char *element, unsigned char *buf,
270 				    int len)
271 {
272 	int i;
273 	int t;
274 
275 	printf("<%s>",element);
276 	for (i = 0; i < len; i++) {
277 		t = (unsigned char)buf[i];
278 		if (t >= 32 && t < 127)
279 			printf("%c",t);
280 	}
281 	printf("</%s>\n",element);
282 
283 	printf("<%s:hex>",element);
284 	for (i = 0; i < len; i++) {
285 		t = (unsigned char)buf[i];
286 		printf(" %x",t);
287 	}
288 	printf("</%s:hex>\n",element);
289 }
290 
291 int
292 debug_get_encoded_number(unsigned char* buf, int mode)
293 {
294 	switch (mode) {
295 	/* 711: Single bite */
296 	case 711:
297 		return isonum_711(buf);
298 
299 	/* 712: Single signed byte */
300 	case 712:
301 		return isonum_712((signed char *)buf);
302 
303 	/* 721: 16 bit LE */
304 	case 721:
305 		return isonum_721(buf);
306 
307 	/* 731: 32 bit LE */
308 	case 731:
309 		return isonum_731(buf);
310 
311 	/* 722: 16 bit BE */
312 	case 722:
313 		return isonum_722(buf);
314 
315 	/* 732: 32 bit BE */
316 	case 732:
317 		return isonum_732(buf);
318 
319 	/* 723: 16 bit bothE */
320 	case 723:
321 		return isonum_723(buf);
322 
323 	/* 733: 32 bit bothE */
324 	case 733:
325 		return isonum_733(buf);
326 	}
327 	return 0;
328 }
329 
330 void
331 debug_dump_integer(const char *element, char* buf, int mode)
332 {
333 	printf("<%s>%i</%s>\n", element,
334 	    debug_get_encoded_number((unsigned char *)buf, mode), element);
335 }
336 
337 void
338 debug_dump_string(const char *element, unsigned char *buf, int len)
339 {
340 
341 }
342 
343 void
344 debug_dump_directory_record_9_1(unsigned char* buf)
345 {
346 	printf("<directoryrecord>\n");
347 	debug_dump_integer("length",
348 	    ((struct iso_directory_record*) buf)->length, 711);
349 	debug_dump_integer("ext_attr_length",
350 	    ((struct iso_directory_record*) buf)->ext_attr_length,711);
351 	debug_dump_integer("extent",
352 	    (char *)((struct iso_directory_record*) buf)->extent, 733);
353 	debug_dump_integer("size",
354 	    (char *)((struct iso_directory_record*) buf)->size, 733);
355 	debug_dump_integer("flags",
356 	    ((struct iso_directory_record*) buf)->flags, 711);
357 	debug_dump_integer("file_unit_size",
358 	    ((struct iso_directory_record*) buf)->file_unit_size,711);
359 	debug_dump_integer("interleave",
360 	    ((struct iso_directory_record*) buf)->interleave, 711);
361 	debug_dump_integer("volume_sequence_number",
362 	    ((struct iso_directory_record*) buf)->volume_sequence_number,
363 	    723);
364 	debug_dump_integer("name_len",
365 	    ((struct iso_directory_record*) buf)->name_len, 711);
366 	debug_dump_to_xml_padded_hex_output("name",
367 	    (u_char *)((struct iso_directory_record*) buf)->name,
368 		debug_get_encoded_number((u_char *)
369 		    ((struct iso_directory_record*) buf)->length, 711));
370 	printf("</directoryrecord>\n");
371 }
372 
373 
374 void
375 debug_dump_to_xml_volume_descriptor(unsigned char* buf, int sector)
376 {
377 	printf("<volumedescriptor sector=\"%i\">\n", sector);
378 	printf("<vdtype>");
379 	switch(buf[0]) {
380 	case 0:
381 		printf("boot");
382 		break;
383 
384 	case 1:
385 		printf("primary");
386 		break;
387 
388 	case 2:
389 		printf("supplementary");
390 		break;
391 
392 	case 3:
393 		printf("volume partition descriptor");
394 		break;
395 
396 	case 255:
397 		printf("terminator");
398 		break;
399 	}
400 
401 	printf("</vdtype>\n");
402 	switch(buf[0]) {
403 	case 1:
404 		debug_dump_integer("type",
405 		    ((struct iso_primary_descriptor*)buf)->type, 711);
406 		debug_dump_to_xml_padded_hex_output("id",
407 		    (u_char *)((struct iso_primary_descriptor*) buf)->id,
408 		    ISODCL (  2,   6));
409 		debug_dump_integer("version",
410 		    ((struct iso_primary_descriptor*)buf)->version,
411 		     711);
412 		debug_dump_to_xml_padded_hex_output("system_id",
413 		    (u_char *)((struct iso_primary_descriptor*)buf)->system_id,
414 		    ISODCL(9,40));
415 		debug_dump_to_xml_padded_hex_output("volume_id",
416 		    (u_char *)((struct iso_primary_descriptor*)buf)->volume_id,
417 		    ISODCL(41,72));
418 		debug_dump_integer("volume_space_size",
419 		    ((struct iso_primary_descriptor*)buf)->volume_space_size,
420 		    733);
421 		debug_dump_integer("volume_set_size",
422 		    ((struct iso_primary_descriptor*)buf)->volume_set_size,
423 			    733);
424 		debug_dump_integer("volume_sequence_number",
425 		    ((struct iso_primary_descriptor*)buf)->volume_sequence_number,
426 		    723);
427 		debug_dump_integer("logical_block_size",
428 		    ((struct iso_primary_descriptor*)buf)->logical_block_size,
429 			    723);
430 		debug_dump_integer("path_table_size",
431 		    ((struct iso_primary_descriptor*)buf)->path_table_size,
432 			    733);
433 		debug_dump_integer("type_l_path_table",
434 		    ((struct iso_primary_descriptor*)buf)->type_l_path_table,
435 		    731);
436 		debug_dump_integer("opt_type_l_path_table",
437 		    ((struct iso_primary_descriptor*)buf)->opt_type_l_path_table,
438 		    731);
439 		debug_dump_integer("type_m_path_table",
440 		    ((struct iso_primary_descriptor*)buf)->type_m_path_table,
441 		    732);
442 		debug_dump_integer("opt_type_m_path_table",
443 			((struct iso_primary_descriptor*)buf)->opt_type_m_path_table,732);
444 		debug_dump_directory_record_9_1(
445 		    (u_char *)((struct iso_primary_descriptor*)buf)->root_directory_record);
446 		debug_dump_to_xml_padded_hex_output("volume_set_id",
447 		    (u_char *)((struct iso_primary_descriptor*) buf)->volume_set_id,
448 		    ISODCL (191, 318));
449 		debug_dump_to_xml_padded_hex_output("publisher_id",
450 		    (u_char *)((struct iso_primary_descriptor*) buf)->publisher_id,
451 		    ISODCL (319, 446));
452 		debug_dump_to_xml_padded_hex_output("preparer_id",
453 		    (u_char *)((struct iso_primary_descriptor*) buf)->preparer_id,
454 		    ISODCL (447, 574));
455 		debug_dump_to_xml_padded_hex_output("application_id",
456 		    (u_char *)((struct iso_primary_descriptor*) buf)->application_id,
457 		    ISODCL (575, 702));
458 		debug_dump_to_xml_padded_hex_output("copyright_file_id",
459 		    (u_char *)((struct iso_primary_descriptor*) buf)->copyright_file_id,
460 		    ISODCL (703, 739));
461 		debug_dump_to_xml_padded_hex_output("abstract_file_id",
462 		    (u_char *)((struct iso_primary_descriptor*) buf)->abstract_file_id,
463 		    ISODCL (740, 776));
464 		debug_dump_to_xml_padded_hex_output("bibliographic_file_id",
465 		    (u_char *)((struct iso_primary_descriptor*) buf)->bibliographic_file_id,
466 		    ISODCL (777, 813));
467 
468 		debug_dump_to_xml_padded_hex_output("creation_date",
469 		    (u_char *)((struct iso_primary_descriptor*) buf)->creation_date,
470 		    ISODCL (814, 830));
471 		debug_dump_to_xml_padded_hex_output("modification_date",
472 		    (u_char *)((struct iso_primary_descriptor*) buf)->modification_date,
473 		    ISODCL (831, 847));
474 		debug_dump_to_xml_padded_hex_output("expiration_date",
475 		    (u_char *)((struct iso_primary_descriptor*) buf)->expiration_date,
476 		    ISODCL (848, 864));
477 		debug_dump_to_xml_padded_hex_output("effective_date",
478 		    (u_char *)((struct iso_primary_descriptor*) buf)->effective_date,
479 		    ISODCL (865, 881));
480 
481 		debug_dump_to_xml_padded_hex_output("file_structure_version",
482 		    (u_char *)((struct iso_primary_descriptor*) buf)->file_structure_version,
483 		    ISODCL(882,882));
484 		break;
485 	}
486 	printf("</volumedescriptor>\n");
487 }
488 
489