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 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 *, const 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, const 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(const 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(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, const unsigned char* buf, int mode) 333 { 334 printf("<%s>%i</%s>\n", element, debug_get_encoded_number(buf, mode), 335 element); 336 } 337 338 void 339 debug_dump_string(const char *element __unused, const 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 struct iso_directory_record *rec = (struct iso_directory_record *)buf; 348 printf("<directoryrecord>\n"); 349 debug_dump_integer("length", rec->length, 711); 350 debug_dump_integer("ext_attr_length", rec->ext_attr_length, 711); 351 debug_dump_integer("extent", rec->extent, 733); 352 debug_dump_integer("size", rec->size, 733); 353 debug_dump_integer("flags", rec->flags, 711); 354 debug_dump_integer("file_unit_size", rec->file_unit_size, 711); 355 debug_dump_integer("interleave", rec->interleave, 711); 356 debug_dump_integer("volume_sequence_number", 357 rec->volume_sequence_number, 723); 358 debug_dump_integer("name_len", rec->name_len, 711); 359 debug_dump_to_xml_padded_hex_output("name", rec->name, 360 debug_get_encoded_number(rec->length, 711)); 361 printf("</directoryrecord>\n"); 362 } 363 364 365 void 366 debug_dump_to_xml_volume_descriptor(unsigned char* buf, int sector) 367 { 368 struct iso_primary_descriptor *desc = 369 (struct iso_primary_descriptor *)buf; 370 371 printf("<volumedescriptor sector=\"%i\">\n", sector); 372 printf("<vdtype>"); 373 switch(buf[0]) { 374 case 0: 375 printf("boot"); 376 break; 377 378 case 1: 379 printf("primary"); 380 break; 381 382 case 2: 383 printf("supplementary"); 384 break; 385 386 case 3: 387 printf("volume partition descriptor"); 388 break; 389 390 case 255: 391 printf("terminator"); 392 break; 393 } 394 395 printf("</vdtype>\n"); 396 switch(buf[0]) { 397 case 1: 398 debug_dump_integer("type", desc->type, 711); 399 debug_dump_to_xml_padded_hex_output("id", desc->id, 400 ISODCL(2, 6)); 401 debug_dump_integer("version", (u_char *)desc->version, 711); 402 debug_dump_to_xml_padded_hex_output("system_id", 403 desc->system_id, ISODCL(9, 40)); 404 debug_dump_to_xml_padded_hex_output("volume_id", 405 desc->volume_id, ISODCL(41, 72)); 406 debug_dump_integer("volume_space_size", 407 (u_char *)desc->volume_space_size, 733); 408 debug_dump_integer("volume_set_size", 409 (u_char *)desc->volume_set_size, 733); 410 debug_dump_integer("volume_sequence_number", 411 (u_char *)desc->volume_sequence_number, 723); 412 debug_dump_integer("logical_block_size", 413 (u_char *)desc->logical_block_size, 723); 414 debug_dump_integer("path_table_size", 415 (u_char *)desc->path_table_size, 733); 416 debug_dump_integer("type_l_path_table", 417 (u_char *)desc->type_l_path_table, 731); 418 debug_dump_integer("opt_type_l_path_table", 419 (u_char *)desc->opt_type_l_path_table, 731); 420 debug_dump_integer("type_m_path_table", 421 (u_char *)desc->type_m_path_table, 732); 422 debug_dump_integer("opt_type_m_path_table", 423 (u_char *)desc->opt_type_m_path_table, 732); 424 debug_dump_directory_record_9_1( 425 (u_char *)desc->root_directory_record); 426 debug_dump_to_xml_padded_hex_output("volume_set_id", 427 desc->volume_set_id, ISODCL(191, 318)); 428 debug_dump_to_xml_padded_hex_output("publisher_id", 429 desc->publisher_id, ISODCL(319, 446)); 430 debug_dump_to_xml_padded_hex_output("preparer_id", 431 desc->preparer_id, ISODCL(447, 574)); 432 debug_dump_to_xml_padded_hex_output("application_id", 433 desc->application_id, ISODCL(575, 702)); 434 debug_dump_to_xml_padded_hex_output("copyright_file_id", 435 desc->copyright_file_id, ISODCL(703, 739)); 436 debug_dump_to_xml_padded_hex_output("abstract_file_id", 437 desc->abstract_file_id, ISODCL(740, 776)); 438 debug_dump_to_xml_padded_hex_output("bibliographic_file_id", 439 desc->bibliographic_file_id, ISODCL(777, 813)); 440 441 debug_dump_to_xml_padded_hex_output("creation_date", 442 desc->creation_date, ISODCL(814, 830)); 443 debug_dump_to_xml_padded_hex_output("modification_date", 444 desc->modification_date, ISODCL(831, 847)); 445 debug_dump_to_xml_padded_hex_output("expiration_date", 446 desc->expiration_date, ISODCL(848, 864)); 447 debug_dump_to_xml_padded_hex_output("effective_date", 448 desc->effective_date, ISODCL(865, 881)); 449 450 debug_dump_to_xml_padded_hex_output("file_structure_version", 451 desc->file_structure_version, ISODCL(882, 882)); 452 break; 453 } 454 printf("</volumedescriptor>\n"); 455 } 456 457