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