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