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