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