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