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