1/* $NetBSD: cd9660_eltorito.c,v 1.27 2023/12/28 12:13:56 tsutsui 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 36#include "cd9660.h" 37#include "cd9660_eltorito.h" 38#include <sys/bootblock.h> 39#include <util.h> 40 41#include <sys/cdefs.h> 42#if defined(__RCSID) && !defined(__lint) 43__RCSID("$NetBSD: cd9660_eltorito.c,v 1.27 2023/12/28 12:13:56 tsutsui Exp $"); 44#endif /* !__lint */ 45 46#ifdef DEBUG 47#define ELTORITO_DPRINTF(__x) printf __x 48#else 49#define ELTORITO_DPRINTF(__x) 50#endif 51 52#include <util.h> 53 54static struct boot_catalog_entry *cd9660_init_boot_catalog_entry(void); 55static struct boot_catalog_entry *cd9660_boot_setup_validation_entry(char); 56static struct boot_catalog_entry *cd9660_boot_setup_default_entry( 57 struct cd9660_boot_image *); 58static struct boot_catalog_entry *cd9660_boot_setup_section_head(char); 59#if 0 60static u_char cd9660_boot_get_system_type(struct cd9660_boot_image *); 61#endif 62 63static struct cd9660_boot_image *default_boot_image; 64 65int 66cd9660_add_boot_disk(iso9660_disk *diskStructure, const char *boot_info) 67{ 68 struct stat stbuf; 69 const char *mode_msg; 70 char *temp; 71 char *sysname; 72 char *filename; 73 struct cd9660_boot_image *new_image, *tmp_image; 74 75 assert(boot_info != NULL); 76 77 if (*boot_info == '\0') { 78 warnx("Error: Boot disk information must be in the " 79 "format 'system;filename'"); 80 return 0; 81 } 82 83 /* First decode the boot information */ 84 temp = estrdup(boot_info); 85 86 sysname = temp; 87 filename = strchr(sysname, ';'); 88 if (filename == NULL) { 89 warnx("supply boot disk information in the format " 90 "'system;filename'"); 91 free(temp); 92 return 0; 93 } 94 95 *filename++ = '\0'; 96 97 if (diskStructure->verbose_level > 0) { 98 printf("Found bootdisk with system %s, and filename %s\n", 99 sysname, filename); 100 } 101 new_image = ecalloc(1, sizeof(*new_image)); 102 new_image->loadSegment = 0; /* default for now */ 103 104 /* Decode System */ 105 if (strcmp(sysname, "i386") == 0) 106 new_image->system = ET_SYS_X86; 107 else if (strcmp(sysname, "powerpc") == 0) 108 new_image->system = ET_SYS_PPC; 109 else if (strcmp(sysname, "macppc") == 0 || 110 strcmp(sysname, "mac68k") == 0) 111 new_image->system = ET_SYS_MAC; 112 else if (strcmp(sysname, "efi") == 0) 113 new_image->system = ET_SYS_EFI; 114 else { 115 warnx("boot disk system must be " 116 "i386, powerpc, macppc, mac68k, or efi"); 117 free(temp); 118 free(new_image); 119 return 0; 120 } 121 122 123 new_image->filename = estrdup(filename); 124 125 free(temp); 126 127 /* Get information about the file */ 128 if (lstat(new_image->filename, &stbuf) == -1) 129 err(EXIT_FAILURE, "%s: lstat(\"%s\")", __func__, 130 new_image->filename); 131 132 switch (stbuf.st_size) { 133 case 1440 * 1024: 134 new_image->targetMode = ET_MEDIA_144FDD; 135 mode_msg = "Assigned boot image to 1.44 emulation mode"; 136 break; 137 case 1200 * 1024: 138 new_image->targetMode = ET_MEDIA_12FDD; 139 mode_msg = "Assigned boot image to 1.2 emulation mode"; 140 break; 141 case 2880 * 1024: 142 new_image->targetMode = ET_MEDIA_288FDD; 143 mode_msg = "Assigned boot image to 2.88 emulation mode"; 144 break; 145 default: 146 new_image->targetMode = ET_MEDIA_NOEM; 147 mode_msg = "Assigned boot image to no emulation mode"; 148 break; 149 } 150 151 if (diskStructure->verbose_level > 0) 152 printf("%s\n", mode_msg); 153 154 new_image->size = stbuf.st_size; 155 new_image->num_sectors = 156 howmany(new_image->size, diskStructure->sectorSize) * 157 howmany(diskStructure->sectorSize, 512); 158 if (diskStructure->verbose_level > 0) { 159 printf("New image has size %d, uses %d 512-byte sectors\n", 160 new_image->size, new_image->num_sectors); 161 } 162 new_image->sector = -1; 163 /* Bootable by default */ 164 new_image->bootable = ET_BOOTABLE; 165 /* Add boot disk */ 166 167 /* Group images for the same platform together. */ 168 TAILQ_FOREACH(tmp_image, &diskStructure->boot_images, image_list) { 169 if (tmp_image->system != new_image->system) 170 break; 171 } 172 173 if (tmp_image == NULL) { 174 TAILQ_INSERT_HEAD(&diskStructure->boot_images, new_image, 175 image_list); 176 } else 177 TAILQ_INSERT_BEFORE(tmp_image, new_image, image_list); 178 179 new_image->serialno = diskStructure->image_serialno++; 180 181 new_image->platform_id = new_image->system; 182 183 /* TODO : Need to do anything about the boot image in the tree? */ 184 diskStructure->is_bootable = 1; 185 186 /* First boot image is initial/default entry. */ 187 if (default_boot_image == NULL) 188 default_boot_image = new_image; 189 190 return 1; 191} 192 193int 194cd9660_eltorito_add_boot_option(iso9660_disk *diskStructure, 195 const char *option_string, const char *value) 196{ 197 char *eptr; 198 struct cd9660_boot_image *image; 199 200 assert(option_string != NULL); 201 202 /* Find the last image added */ 203 TAILQ_FOREACH(image, &diskStructure->boot_images, image_list) { 204 if (image->serialno + 1 == diskStructure->image_serialno) 205 break; 206 } 207 if (image == NULL) 208 errx(EXIT_FAILURE, "Attempted to add boot option, " 209 "but no boot images have been specified"); 210 211 if (strcmp(option_string, "no-emul-boot") == 0) { 212 image->targetMode = ET_MEDIA_NOEM; 213 } else if (strcmp(option_string, "no-boot") == 0) { 214 image->bootable = ET_NOT_BOOTABLE; 215 } else if (strcmp(option_string, "hard-disk-boot") == 0) { 216 image->targetMode = ET_MEDIA_HDD; 217 } else if (strcmp(option_string, "boot-load-segment") == 0) { 218 image->loadSegment = strtoul(value, &eptr, 16); 219 if (eptr == value || *eptr != '\0' || errno != ERANGE) { 220 warn("%s: strtoul", __func__); 221 return 0; 222 } 223 } else if (strcmp(option_string, "platformid") == 0) { 224 if (strcmp(value, "efi") == 0) 225 image->platform_id = ET_SYS_EFI; 226 else { 227 warn("%s: unknown platform: %s", __func__, value); 228 return 0; 229 } 230 } else { 231 return 0; 232 } 233 return 1; 234} 235 236static struct boot_catalog_entry * 237cd9660_init_boot_catalog_entry(void) 238{ 239 return ecalloc(1, sizeof(struct boot_catalog_entry)); 240} 241 242static struct boot_catalog_entry * 243cd9660_boot_setup_validation_entry(char sys) 244{ 245 struct boot_catalog_entry *entry; 246 boot_catalog_validation_entry *ve; 247 int16_t checksum; 248 unsigned char *csptr; 249 size_t i; 250 entry = cd9660_init_boot_catalog_entry(); 251 252 entry->entry_type = ET_ENTRY_VE; 253 ve = &entry->entry_data.VE; 254 255 ve->header_id[0] = 1; 256 ve->platform_id[0] = sys; 257 ve->key[0] = 0x55; 258 ve->key[1] = 0xAA; 259 260 /* Calculate checksum */ 261 checksum = 0; 262 cd9660_721(0, ve->checksum); 263 csptr = (unsigned char*)ve; 264 for (i = 0; i < sizeof(*ve); i += 2) { 265 checksum += (int16_t)csptr[i]; 266 checksum += 256 * (int16_t)csptr[i + 1]; 267 } 268 checksum = -checksum; 269 cd9660_721(checksum, ve->checksum); 270 271 ELTORITO_DPRINTF(("%s: header_id %d, platform_id %d, key[0] %d, key[1] %d, " 272 "checksum %04x\n", __func__, ve->header_id[0], ve->platform_id[0], 273 ve->key[0], ve->key[1], checksum)); 274 return entry; 275} 276 277static struct boot_catalog_entry * 278cd9660_boot_setup_default_entry(struct cd9660_boot_image *disk) 279{ 280 struct boot_catalog_entry *default_entry; 281 boot_catalog_initial_entry *ie; 282 283 default_entry = cd9660_init_boot_catalog_entry(); 284 if (default_entry == NULL) 285 return NULL; 286 287 default_entry->entry_type = ET_ENTRY_IE; 288 ie = &default_entry->entry_data.IE; 289 290 ie->boot_indicator[0] = disk->bootable; 291 ie->media_type[0] = disk->targetMode; 292 cd9660_721(disk->loadSegment, ie->load_segment); 293 ie->system_type[0] = disk->system; 294 cd9660_721(disk->num_sectors, ie->sector_count); 295 cd9660_731(disk->sector, ie->load_rba); 296 297 ELTORITO_DPRINTF(("%s: boot indicator %d, media type %d, " 298 "load segment %04x, system type %d, sector count %d, " 299 "load rba %d\n", __func__, ie->boot_indicator[0], 300 ie->media_type[0], disk->loadSegment, ie->system_type[0], 301 disk->num_sectors, disk->sector)); 302 return default_entry; 303} 304 305static struct boot_catalog_entry * 306cd9660_boot_setup_section_head(char platform) 307{ 308 struct boot_catalog_entry *entry; 309 boot_catalog_section_header *sh; 310 311 entry = cd9660_init_boot_catalog_entry(); 312 if (entry == NULL) 313 return NULL; 314 315 entry->entry_type = ET_ENTRY_SH; 316 sh = &entry->entry_data.SH; 317 /* More by default. The last one will manually be set to 0x91 */ 318 sh->header_indicator[0] = ET_SECTION_HEADER_MORE; 319 sh->platform_id[0] = platform; 320 sh->num_section_entries[0] = 0; 321 return entry; 322} 323 324static struct boot_catalog_entry * 325cd9660_boot_setup_section_entry(struct cd9660_boot_image *disk) 326{ 327 struct boot_catalog_entry *entry; 328 boot_catalog_section_entry *se; 329 if ((entry = cd9660_init_boot_catalog_entry()) == NULL) 330 return NULL; 331 332 entry->entry_type = ET_ENTRY_SE; 333 se = &entry->entry_data.SE; 334 335 se->boot_indicator[0] = ET_BOOTABLE; 336 se->media_type[0] = disk->targetMode; 337 cd9660_721(disk->loadSegment, se->load_segment); 338 cd9660_721(disk->num_sectors, se->sector_count); 339 cd9660_731(disk->sector, se->load_rba); 340 return entry; 341} 342 343#if 0 344static u_char 345cd9660_boot_get_system_type(struct cd9660_boot_image *disk) 346{ 347 /* 348 For hard drive booting, we need to examine the MBR to figure 349 out what the partition type is 350 */ 351 return 0; 352} 353#endif 354 355/* 356 * Set up the BVD, Boot catalog, and the boot entries, but do no writing 357 */ 358int 359cd9660_setup_boot(iso9660_disk *diskStructure, int first_sector) 360{ 361 int sector; 362 int used_sectors; 363 int num_entries = 0; 364 int catalog_sectors; 365 struct boot_catalog_entry *x86_head, *mac_head, *ppc_head, *efi_head, 366 *valid_entry, *default_entry, *temp, *head, **headp, *next; 367 struct cd9660_boot_image *tmp_disk; 368 u_char system; 369 370 headp = NULL; 371 x86_head = mac_head = ppc_head = efi_head = NULL; 372 373 /* If there are no boot disks, don't bother building boot information */ 374 if (TAILQ_EMPTY(&diskStructure->boot_images)) 375 return 0; 376 377 /* Point to catalog: For now assume it consumes one sector */ 378 ELTORITO_DPRINTF(("Boot catalog will go in sector %d\n", first_sector)); 379 diskStructure->boot_catalog_sector = first_sector; 380 cd9660_731(first_sector, 381 diskStructure->boot_descriptor->boot_catalog_pointer); 382 383 /* 384 * Use system type of default image for validation entry. Fallback to 385 * X86 system type if not found. 386 */ 387 system = default_boot_image != NULL ? default_boot_image->system : 388 ET_SYS_X86; 389 390 /* Step 1: Generate boot catalog */ 391 /* Step 1a: Validation entry */ 392 valid_entry = cd9660_boot_setup_validation_entry(system); 393 if (valid_entry == NULL) 394 return -1; 395 396 /* 397 * Count how many boot images there are, 398 * and how many sectors they consume. 399 */ 400 num_entries = 1; 401 used_sectors = 0; 402 403 TAILQ_FOREACH(tmp_disk, &diskStructure->boot_images, image_list) { 404 used_sectors += tmp_disk->num_sectors; 405 406 /* One default entry per image */ 407 num_entries++; 408 } 409 catalog_sectors = howmany(num_entries * 0x20, diskStructure->sectorSize); 410 used_sectors += catalog_sectors; 411 412 if (diskStructure->verbose_level > 0) { 413 printf("%s: there will be %i entries consuming %i sectors. " 414 "Catalog is %i sectors\n", __func__, num_entries, 415 used_sectors, catalog_sectors); 416 } 417 418 /* Populate sector numbers */ 419 sector = first_sector + catalog_sectors; 420 TAILQ_FOREACH(tmp_disk, &diskStructure->boot_images, image_list) { 421 tmp_disk->sector = sector; 422 sector += tmp_disk->num_sectors / 423 (diskStructure->sectorSize / 512); 424 } 425 426 LIST_INSERT_HEAD(&diskStructure->boot_entries, valid_entry, ll_struct); 427 428 /* Step 1b: Initial/default entry */ 429 /* TODO : PARAM */ 430 if (default_boot_image != NULL) { 431 struct cd9660_boot_image *tcbi; 432 TAILQ_FOREACH(tcbi, &diskStructure->boot_images, image_list) { 433 if (tcbi == default_boot_image) { 434 tmp_disk = tcbi; 435 break; 436 } 437 } 438 } 439 if (tmp_disk == NULL) 440 tmp_disk = TAILQ_FIRST(&diskStructure->boot_images); 441 default_entry = cd9660_boot_setup_default_entry(tmp_disk); 442 if (default_entry == NULL) { 443 warnx("Error: memory allocation failed in cd9660_setup_boot"); 444 return -1; 445 } 446 447 LIST_INSERT_AFTER(valid_entry, default_entry, ll_struct); 448 449 /* Todo: multiple default entries? */ 450 451 tmp_disk = TAILQ_FIRST(&diskStructure->boot_images); 452 453 head = NULL; 454 temp = default_entry; 455 456 /* If multiple boot images are given : */ 457 for (; tmp_disk != NULL; tmp_disk = TAILQ_NEXT(tmp_disk, image_list)) { 458 if (tmp_disk == default_boot_image) 459 continue; 460 461 /* Step 2: Section header */ 462 switch (tmp_disk->platform_id) { 463 case ET_SYS_X86: 464 headp = &x86_head; 465 break; 466 case ET_SYS_PPC: 467 headp = &ppc_head; 468 break; 469 case ET_SYS_MAC: 470 headp = &mac_head; 471 break; 472 case ET_SYS_EFI: 473 headp = &efi_head; 474 break; 475 default: 476 warnx("%s: internal error: unknown system type", 477 __func__); 478 return -1; 479 } 480 481 if (*headp == NULL) { 482 head = 483 cd9660_boot_setup_section_head(tmp_disk->platform_id); 484 if (head == NULL) { 485 warnx("Error: memory allocation failed in " 486 "cd9660_setup_boot"); 487 return -1; 488 } 489 LIST_INSERT_AFTER(default_entry, head, ll_struct); 490 *headp = head; 491 } else 492 head = *headp; 493 494 head->entry_data.SH.num_section_entries[0]++; 495 496 /* Step 2a: Section entry and extensions */ 497 temp = cd9660_boot_setup_section_entry(tmp_disk); 498 if (temp == NULL) { 499 warn("%s: cd9660_boot_setup_section_entry", __func__); 500 return -1; 501 } 502 503 while ((next = LIST_NEXT(head, ll_struct)) != NULL && 504 next->entry_type == ET_ENTRY_SE) 505 head = next; 506 507 LIST_INSERT_AFTER(head, temp, ll_struct); 508 } 509 510 /* Find the last Section Header entry and mark it as the last. */ 511 head = NULL; 512 LIST_FOREACH(next, &diskStructure->boot_entries, ll_struct) { 513 if (next->entry_type == ET_ENTRY_SH) 514 head = next; 515 } 516 if (head != NULL) 517 head->entry_data.SH.header_indicator[0] = ET_SECTION_HEADER_LAST; 518 519 /* TODO: Remaining boot disks when implemented */ 520 521 return first_sector + used_sectors; 522} 523 524int 525cd9660_setup_boot_volume_descriptor(iso9660_disk *diskStructure, 526 volume_descriptor *bvd) 527{ 528 boot_volume_descriptor *bvdData = 529 (boot_volume_descriptor*)bvd->volumeDescriptorData; 530 531 bvdData->boot_record_indicator[0] = ISO_VOLUME_DESCRIPTOR_BOOT; 532 memcpy(bvdData->identifier, ISO_VOLUME_DESCRIPTOR_STANDARD_ID, 5); 533 bvdData->version[0] = 1; 534 memcpy(bvdData->boot_system_identifier, ET_ID, 23); 535 memcpy(bvdData->identifier, ISO_VOLUME_DESCRIPTOR_STANDARD_ID, 5); 536 diskStructure->boot_descriptor = 537 (boot_volume_descriptor*) bvd->volumeDescriptorData; 538 return 1; 539} 540 541static int 542cd9660_write_mbr_partition_entry(FILE *fd, int idx, off_t sector_start, 543 off_t nsectors, int type) 544{ 545 uint8_t val; 546 uint32_t lba; 547 548 if (fseeko(fd, (off_t)(idx) * 16 + 0x1be, SEEK_SET) == -1) 549 err(EXIT_FAILURE, "fseeko"); 550 551 val = 0x80; /* Bootable */ 552 fwrite(&val, sizeof(val), 1, fd); 553 554 val = 0xff; /* CHS begin */ 555 fwrite(&val, sizeof(val), 1, fd); 556 fwrite(&val, sizeof(val), 1, fd); 557 fwrite(&val, sizeof(val), 1, fd); 558 559 val = type; /* Part type */ 560 fwrite(&val, sizeof(val), 1, fd); 561 562 val = 0xff; /* CHS end */ 563 fwrite(&val, sizeof(val), 1, fd); 564 fwrite(&val, sizeof(val), 1, fd); 565 fwrite(&val, sizeof(val), 1, fd); 566 567 /* LBA extent */ 568 lba = htole32(sector_start); 569 fwrite(&lba, sizeof(lba), 1, fd); 570 lba = htole32(nsectors); 571 fwrite(&lba, sizeof(lba), 1, fd); 572 573 return 0; 574} 575 576static int 577cd9660_write_apm_partition_entry(FILE *fd, int idx, int total_partitions, 578 off_t sector_start, off_t nsectors, off_t sector_size, 579 const char *part_name, const char *part_type) 580{ 581 uint32_t apm32, part_status; 582 uint16_t apm16; 583 584 /* See Apple Tech Note 1189 for the details about the pmPartStatus 585 * flags. 586 * Below the flags which are default: 587 * - IsValid 0x01 588 * - IsAllocated 0x02 589 * - IsReadable 0x10 590 * - IsWritable 0x20 591 */ 592 part_status = APPLE_PS_VALID | APPLE_PS_ALLOCATED | APPLE_PS_READABLE | 593 APPLE_PS_WRITABLE; 594 595 if (fseeko(fd, (off_t)(idx + 1) * sector_size, SEEK_SET) == -1) 596 err(EXIT_FAILURE, "fseeko"); 597 598 /* Signature */ 599 apm16 = htobe16(0x504d); 600 fwrite(&apm16, sizeof(apm16), 1, fd); 601 apm16 = 0; 602 fwrite(&apm16, sizeof(apm16), 1, fd); 603 604 /* Total number of partitions */ 605 apm32 = htobe32(total_partitions); 606 fwrite(&apm32, sizeof(apm32), 1, fd); 607 /* Bounds */ 608 apm32 = htobe32(sector_start); 609 fwrite(&apm32, sizeof(apm32), 1, fd); 610 apm32 = htobe32(nsectors); 611 fwrite(&apm32, sizeof(apm32), 1, fd); 612 613 fwrite(part_name, strlen(part_name) + 1, 1, fd); 614 fseek(fd, 32 - strlen(part_name) - 1, SEEK_CUR); 615 fwrite(part_type, strlen(part_type) + 1, 1, fd); 616 fseek(fd, 32 - strlen(part_type) - 1, SEEK_CUR); 617 618 apm32 = 0; 619 /* pmLgDataStart */ 620 fwrite(&apm32, sizeof(apm32), 1, fd); 621 /* pmDataCnt */ 622 apm32 = htobe32(nsectors); 623 fwrite(&apm32, sizeof(apm32), 1, fd); 624 /* pmPartStatus */ 625 apm32 = htobe32(part_status); 626 fwrite(&apm32, sizeof(apm32), 1, fd); 627 628 return 0; 629} 630 631int 632cd9660_write_boot(iso9660_disk *diskStructure, FILE *fd) 633{ 634 struct boot_catalog_entry *e; 635 struct cd9660_boot_image *t; 636 int apm_partitions = 0; 637 int mbr_partitions = 0; 638 639 /* write boot catalog */ 640 if (fseeko(fd, (off_t)diskStructure->boot_catalog_sector * 641 diskStructure->sectorSize, SEEK_SET) == -1) 642 err(EXIT_FAILURE, "fseeko"); 643 644 if (diskStructure->verbose_level > 0) { 645 printf("Writing boot catalog to sector %" PRId64 "\n", 646 diskStructure->boot_catalog_sector); 647 } 648 LIST_FOREACH(e, &diskStructure->boot_entries, ll_struct) { 649 if (diskStructure->verbose_level > 0) { 650 printf("Writing catalog entry of type %d\n", 651 e->entry_type); 652 } 653 /* 654 * It doesnt matter which one gets written 655 * since they are the same size 656 */ 657 fwrite(&(e->entry_data.VE), 1, 32, fd); 658 } 659 if (diskStructure->verbose_level > 0) 660 printf("Finished writing boot catalog\n"); 661 662 /* copy boot images */ 663 TAILQ_FOREACH(t, &diskStructure->boot_images, image_list) { 664 if (diskStructure->verbose_level > 0) { 665 printf("Writing boot image from %s to sectors %d\n", 666 t->filename, t->sector); 667 } 668 cd9660_copy_file(diskStructure, fd, t->sector, t->filename); 669 670 if (t->system == ET_SYS_MAC) 671 apm_partitions++; 672 if (t->system == ET_SYS_PPC) 673 mbr_partitions++; 674 } 675 676 /* some systems need partition tables as well */ 677 if (mbr_partitions > 0 || diskStructure->chrp_boot) { 678 uint16_t sig; 679 680 fseek(fd, 0x1fe, SEEK_SET); 681 sig = htole16(0xaa55); 682 fwrite(&sig, sizeof(sig), 1, fd); 683 684 mbr_partitions = 0; 685 686 /* Write ISO9660 descriptor, enclosing the whole disk */ 687 if (diskStructure->chrp_boot) 688 cd9660_write_mbr_partition_entry(fd, mbr_partitions++, 689 0, diskStructure->totalSectors * 690 (diskStructure->sectorSize / 512), 0x96); 691 692 /* Write all partition entries */ 693 TAILQ_FOREACH(t, &diskStructure->boot_images, image_list) { 694 if (t->system != ET_SYS_PPC) 695 continue; 696 cd9660_write_mbr_partition_entry(fd, mbr_partitions++, 697 t->sector * (diskStructure->sectorSize / 512), 698 t->num_sectors * (diskStructure->sectorSize / 512), 699 0x41 /* PReP Boot */); 700 } 701 } 702 703 if (apm_partitions > 0) { 704 /* Write DDR and global APM info */ 705 uint32_t apm32; 706 uint16_t apm16; 707 int total_parts; 708 709 fseek(fd, 0, SEEK_SET); 710 apm16 = htobe16(0x4552); 711 fwrite(&apm16, sizeof(apm16), 1, fd); 712 /* Device block size */ 713 apm16 = htobe16(512); 714 fwrite(&apm16, sizeof(apm16), 1, fd); 715 /* Device block count */ 716 apm32 = htobe32(diskStructure->totalSectors * 717 (diskStructure->sectorSize / 512)); 718 fwrite(&apm32, sizeof(apm32), 1, fd); 719 /* Device type/id */ 720 apm16 = htobe16(1); 721 fwrite(&apm16, sizeof(apm16), 1, fd); 722 fwrite(&apm16, sizeof(apm16), 1, fd); 723 724 /* Count total needed entries */ 725 total_parts = 2 + apm_partitions; /* Self + ISO9660 */ 726 727 /* Write self-descriptor */ 728 cd9660_write_apm_partition_entry(fd, 0, total_parts, 1, 729 total_parts, 512, "Apple", "Apple_partition_map"); 730 731 /* Write all partition entries */ 732 apm_partitions = 0; 733 TAILQ_FOREACH(t, &diskStructure->boot_images, image_list) { 734 if (t->system != ET_SYS_MAC) 735 continue; 736 737 cd9660_write_apm_partition_entry(fd, 738 1 + apm_partitions++, total_parts, 739 t->sector * (diskStructure->sectorSize / 512), 740 t->num_sectors * (diskStructure->sectorSize / 512), 741 512, "CD Boot", "Apple_Bootstrap"); 742 } 743 744 /* Write ISO9660 descriptor, enclosing the whole disk */ 745 cd9660_write_apm_partition_entry(fd, 2 + apm_partitions, 746 total_parts, 0, diskStructure->totalSectors * 747 (diskStructure->sectorSize / 512), 512, "ISO9660", 748 "CD_ROM_Mode_1"); 749 } 750 751 return 0; 752} 753