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