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