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