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