xref: /freebsd/usr.sbin/makefs/cd9660/cd9660_eltorito.c (revision fee65b7e21eeeb625dcaba8cf2b9e0cf5af83498)
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 
378 	headp = NULL;
379 	x86_head = mac_head = ppc_head = efi_head = NULL;
380 
381 	/* If there are no boot disks, don't bother building boot information */
382 	if (TAILQ_EMPTY(&diskStructure->boot_images))
383 		return 0;
384 
385 	/* Point to catalog: For now assume it consumes one sector */
386 	ELTORITO_DPRINTF(("Boot catalog will go in sector %d\n", first_sector));
387 	diskStructure->boot_catalog_sector = first_sector;
388 	cd9660_731(first_sector,
389 	    diskStructure->boot_descriptor->boot_catalog_pointer);
390 
391 	/* Step 1: Generate boot catalog */
392 	/* Step 1a: Validation entry */
393 	valid_entry = cd9660_boot_setup_validation_entry(ET_SYS_X86);
394 	if (valid_entry == NULL)
395 		return -1;
396 
397 	/*
398 	 * Count how many boot images there are,
399 	 * and how many sectors they consume.
400 	 */
401 	num_entries = 1;
402 	used_sectors = 0;
403 
404 	TAILQ_FOREACH(tmp_disk, &diskStructure->boot_images, image_list) {
405 		used_sectors += tmp_disk->num_sectors;
406 
407 		/* One default entry per image */
408 		num_entries++;
409 	}
410 	catalog_sectors = howmany(num_entries * 0x20, diskStructure->sectorSize);
411 	used_sectors += catalog_sectors;
412 
413 	if (diskStructure->verbose_level > 0) {
414 		printf("%s: there will be %i entries consuming %i sectors. "
415 		       "Catalog is %i sectors\n", __func__, num_entries,
416 		       used_sectors, catalog_sectors);
417 	}
418 
419 	/* Populate sector numbers */
420 	sector = first_sector + catalog_sectors;
421 	TAILQ_FOREACH(tmp_disk, &diskStructure->boot_images, image_list) {
422 		tmp_disk->sector = sector;
423 		sector += tmp_disk->num_sectors /
424 		    (diskStructure->sectorSize / 512);
425 	}
426 
427 	LIST_INSERT_HEAD(&diskStructure->boot_entries, valid_entry, ll_struct);
428 
429 	/* Step 1b: Initial/default entry */
430 	/* TODO : PARAM */
431 	if (default_boot_image != NULL) {
432 		struct cd9660_boot_image *tcbi;
433 		TAILQ_FOREACH(tcbi, &diskStructure->boot_images, image_list) {
434 			if (tcbi == default_boot_image) {
435 				tmp_disk = tcbi;
436 				break;
437 			}
438 		}
439 	}
440 	if (tmp_disk == NULL)
441 		tmp_disk = TAILQ_FIRST(&diskStructure->boot_images);
442 	default_entry = cd9660_boot_setup_default_entry(tmp_disk);
443 	if (default_entry == NULL) {
444 		warnx("Error: memory allocation failed in cd9660_setup_boot");
445 		return -1;
446 	}
447 
448 	LIST_INSERT_AFTER(valid_entry, default_entry, ll_struct);
449 
450 	/* Todo: multiple default entries? */
451 
452 	tmp_disk = TAILQ_FIRST(&diskStructure->boot_images);
453 
454 	head = NULL;
455 	temp = default_entry;
456 
457 	/* If multiple boot images are given : */
458 	for (; tmp_disk != NULL; tmp_disk = TAILQ_NEXT(tmp_disk, image_list)) {
459 		if (tmp_disk == default_boot_image)
460 			continue;
461 
462 		/* Step 2: Section header */
463 		switch (tmp_disk->platform_id) {
464 		case ET_SYS_X86:
465 			headp = &x86_head;
466 			break;
467 		case ET_SYS_PPC:
468 			headp = &ppc_head;
469 			break;
470 		case ET_SYS_MAC:
471 			headp = &mac_head;
472 			break;
473 		case ET_SYS_EFI:
474 			headp = &efi_head;
475 			break;
476 		default:
477 			warnx("%s: internal error: unknown system type",
478 			    __func__);
479 			return -1;
480 		}
481 
482 		if (*headp == NULL) {
483 			head =
484 			  cd9660_boot_setup_section_head(tmp_disk->platform_id);
485 			if (head == NULL) {
486 				warnx("Error: memory allocation failed in "
487 				      "cd9660_setup_boot");
488 				return -1;
489 			}
490 			LIST_INSERT_AFTER(default_entry, head, ll_struct);
491 			*headp = head;
492 		} else
493 			head = *headp;
494 
495 		head->entry_data.SH.num_section_entries[0]++;
496 
497 		/* Step 2a: Section entry and extensions */
498 		temp = cd9660_boot_setup_section_entry(tmp_disk);
499 		if (temp == NULL) {
500 			warn("%s: cd9660_boot_setup_section_entry", __func__);
501 			return -1;
502 		}
503 
504 		while ((next = LIST_NEXT(head, ll_struct)) != NULL &&
505 		       next->entry_type == ET_ENTRY_SE)
506 			head = next;
507 
508 		LIST_INSERT_AFTER(head, temp, ll_struct);
509 	}
510 
511 	/* Find the last Section Header entry and mark it as the last. */
512 	head = NULL;
513 	LIST_FOREACH(next, &diskStructure->boot_entries, ll_struct) {
514 		if (next->entry_type == ET_ENTRY_SH)
515 			head = next;
516 	}
517 	if (head != NULL)
518 		head->entry_data.SH.header_indicator[0] = ET_SECTION_HEADER_LAST;
519 
520 	/* TODO: Remaining boot disks when implemented */
521 
522 	return first_sector + used_sectors;
523 }
524 
525 int
526 cd9660_setup_boot_volume_descriptor(iso9660_disk *diskStructure,
527     volume_descriptor *bvd)
528 {
529 	boot_volume_descriptor *bvdData =
530 	    (boot_volume_descriptor*)bvd->volumeDescriptorData;
531 
532 	bvdData->boot_record_indicator[0] = ISO_VOLUME_DESCRIPTOR_BOOT;
533 	memcpy(bvdData->identifier, ISO_VOLUME_DESCRIPTOR_STANDARD_ID, 5);
534 	bvdData->version[0] = 1;
535 	memcpy(bvdData->boot_system_identifier, ET_ID, 23);
536 	memcpy(bvdData->identifier, ISO_VOLUME_DESCRIPTOR_STANDARD_ID, 5);
537 	diskStructure->boot_descriptor =
538 	    (boot_volume_descriptor*) bvd->volumeDescriptorData;
539 	return 1;
540 }
541 
542 static int
543 cd9660_write_mbr_partition_entry(FILE *fd, int idx, off_t sector_start,
544     off_t nsectors, int type)
545 {
546 	uint8_t val;
547 	uint32_t lba;
548 
549 	if (fseeko(fd, (off_t)(idx) * 16 + 0x1be, SEEK_SET) == -1)
550 		err(1, "fseeko");
551 
552 	val = 0x80; /* Bootable */
553 	fwrite(&val, sizeof(val), 1, fd);
554 
555 	val = 0xff; /* CHS begin */
556 	fwrite(&val, sizeof(val), 1, fd);
557 	fwrite(&val, sizeof(val), 1, fd);
558 	fwrite(&val, sizeof(val), 1, fd);
559 
560 	val = type; /* Part type */
561 	fwrite(&val, sizeof(val), 1, fd);
562 
563 	val = 0xff; /* CHS end */
564 	fwrite(&val, sizeof(val), 1, fd);
565 	fwrite(&val, sizeof(val), 1, fd);
566 	fwrite(&val, sizeof(val), 1, fd);
567 
568 	/* LBA extent */
569 	lba = htole32(sector_start);
570 	fwrite(&lba, sizeof(lba), 1, fd);
571 	lba = htole32(nsectors);
572 	fwrite(&lba, sizeof(lba), 1, fd);
573 
574 	return 0;
575 }
576 
577 static int
578 cd9660_write_apm_partition_entry(FILE *fd, int idx, int total_partitions,
579     off_t sector_start, off_t nsectors, off_t sector_size,
580     const char *part_name, const char *part_type)
581 {
582 	uint32_t apm32, part_status;
583 	uint16_t apm16;
584 
585 	part_status = APPLE_PS_VALID | APPLE_PS_ALLOCATED | APPLE_PS_READABLE |
586 	    APPLE_PS_WRITABLE;
587 
588 	if (fseeko(fd, (off_t)(idx + 1) * sector_size, SEEK_SET) == -1)
589 		err(1, "fseeko");
590 
591 	/* Signature */
592 	apm16 = htobe16(0x504d);
593 	fwrite(&apm16, sizeof(apm16), 1, fd);
594 	apm16 = 0;
595 	fwrite(&apm16, sizeof(apm16), 1, fd);
596 
597 	/* Total number of partitions */
598 	apm32 = htobe32(total_partitions);
599 	fwrite(&apm32, sizeof(apm32), 1, fd);
600 	/* Bounds */
601 	apm32 = htobe32(sector_start);
602 	fwrite(&apm32, sizeof(apm32), 1, fd);
603 	apm32 = htobe32(nsectors);
604 	fwrite(&apm32, sizeof(apm32), 1, fd);
605 
606 	fwrite(part_name, strlen(part_name) + 1, 1, fd);
607 	fseek(fd, 32 - strlen(part_name) - 1, SEEK_CUR);
608 	fwrite(part_type, strlen(part_type) + 1, 1, fd);
609 	fseek(fd, 32 - strlen(part_type) - 1, SEEK_CUR);
610 
611 	apm32 = 0;
612 	/* pmLgDataStart */
613 	fwrite(&apm32, sizeof(apm32), 1, fd);
614 	/* pmDataCnt */
615 	apm32 = htobe32(nsectors);
616 	fwrite(&apm32, sizeof(apm32), 1, fd);
617 	/* pmPartStatus */
618 	apm32 = htobe32(part_status);
619 	fwrite(&apm32, sizeof(apm32), 1, fd);
620 
621 	return 0;
622 }
623 
624 int
625 cd9660_write_boot(iso9660_disk *diskStructure, FILE *fd)
626 {
627 	struct boot_catalog_entry *e;
628 	struct cd9660_boot_image *t;
629 	int apm_partitions = 0;
630 	int mbr_partitions = 0;
631 
632 	/* write boot catalog */
633 	if (fseeko(fd, (off_t)diskStructure->boot_catalog_sector *
634 	    diskStructure->sectorSize, SEEK_SET) == -1)
635 		err(1, "fseeko");
636 
637 	if (diskStructure->verbose_level > 0) {
638 		printf("Writing boot catalog to sector %" PRId64 "\n",
639 		    diskStructure->boot_catalog_sector);
640 	}
641 	LIST_FOREACH(e, &diskStructure->boot_entries, ll_struct) {
642 		if (diskStructure->verbose_level > 0) {
643 			printf("Writing catalog entry of type %d\n",
644 			    e->entry_type);
645 		}
646 		/*
647 		 * It doesn't matter which one gets written
648 		 * since they are the same size
649 		 */
650 		fwrite(&(e->entry_data.VE), 1, 32, fd);
651 	}
652 	if (diskStructure->verbose_level > 0)
653 		printf("Finished writing boot catalog\n");
654 
655 	/* copy boot images */
656 	TAILQ_FOREACH(t, &diskStructure->boot_images, image_list) {
657 		if (diskStructure->verbose_level > 0) {
658 			printf("Writing boot image from %s to sectors %d\n",
659 			    t->filename, t->sector);
660 		}
661 		cd9660_copy_file(diskStructure, fd, t->sector, t->filename);
662 
663 		if (t->system == ET_SYS_MAC)
664 			apm_partitions++;
665 		if (t->system == ET_SYS_PPC)
666 			mbr_partitions++;
667 	}
668 
669 	/* some systems need partition tables as well */
670 	if (mbr_partitions > 0 || diskStructure->chrp_boot) {
671 		uint16_t sig;
672 
673 		fseek(fd, 0x1fe, SEEK_SET);
674 		sig = htole16(0xaa55);
675 		fwrite(&sig, sizeof(sig), 1, fd);
676 
677 		mbr_partitions = 0;
678 
679 		/* Write ISO9660 descriptor, enclosing the whole disk */
680 		if (diskStructure->chrp_boot)
681 			cd9660_write_mbr_partition_entry(fd, mbr_partitions++,
682 			    0, diskStructure->totalSectors *
683 			    (diskStructure->sectorSize / 512), 0x96);
684 
685 		/* Write all partition entries */
686 		TAILQ_FOREACH(t, &diskStructure->boot_images, image_list) {
687 			if (t->system != ET_SYS_PPC)
688 				continue;
689 			cd9660_write_mbr_partition_entry(fd, mbr_partitions++,
690 			    t->sector * (diskStructure->sectorSize / 512),
691 			    t->num_sectors * (diskStructure->sectorSize / 512),
692 			    0x41 /* PReP Boot */);
693 		}
694 	}
695 
696 	if (apm_partitions > 0) {
697 		/* Write DDR and global APM info */
698 		uint32_t apm32;
699 		uint16_t apm16;
700 		int total_parts;
701 
702 		fseek(fd, 0, SEEK_SET);
703 		apm16 = htobe16(0x4552);
704 		fwrite(&apm16, sizeof(apm16), 1, fd);
705 		/* Device block size */
706 		apm16 = htobe16(512);
707 		fwrite(&apm16, sizeof(apm16), 1, fd);
708 		/* Device block count */
709 		apm32 = htobe32(diskStructure->totalSectors *
710 		    (diskStructure->sectorSize / 512));
711 		fwrite(&apm32, sizeof(apm32), 1, fd);
712 		/* Device type/id */
713 		apm16 = htobe16(1);
714 		fwrite(&apm16, sizeof(apm16), 1, fd);
715 		fwrite(&apm16, sizeof(apm16), 1, fd);
716 
717 		/* Count total needed entries */
718 		total_parts = 2 + apm_partitions; /* Self + ISO9660 */
719 
720 		/* Write self-descriptor */
721 		cd9660_write_apm_partition_entry(fd, 0, total_parts, 1,
722 		    total_parts, 512, "Apple", "Apple_partition_map");
723 
724 		/* Write all partition entries */
725 		apm_partitions = 0;
726 		TAILQ_FOREACH(t, &diskStructure->boot_images, image_list) {
727 			if (t->system != ET_SYS_MAC)
728 				continue;
729 
730 			cd9660_write_apm_partition_entry(fd,
731 			    1 + apm_partitions++, total_parts,
732 			    t->sector * (diskStructure->sectorSize / 512),
733 			    t->num_sectors * (diskStructure->sectorSize / 512),
734 			    512, "CD Boot", "Apple_Bootstrap");
735 		}
736 
737 		/* Write ISO9660 descriptor, enclosing the whole disk */
738 		cd9660_write_apm_partition_entry(fd, 2 + apm_partitions,
739 		    total_parts, 0, diskStructure->totalSectors *
740 		    (diskStructure->sectorSize / 512), 512, "ISO9660",
741 		    "CD_ROM_Mode_1");
742 	}
743 
744 	return 0;
745 }
746