summaryrefslogtreecommitdiff
path: root/usr.sbin/makefs/cd9660/cd9660_eltorito.c
blob: 347b596fdc57d965a7f238f367ff49185e555fa2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
/*	$OpenBSD: cd9660_eltorito.c,v 1.15 2021/07/12 15:09:21 beck Exp $	*/
/*	$NetBSD: cd9660_eltorito.c,v 1.20 2013/01/28 21:03:28 christos Exp $	*/

/*
 * Copyright (c) 2005 Daniel Watt, Walter Deignan, Ryan Gabrys, Alan
 * Perez-Rathke and Ram Vedam.  All rights reserved.
 *
 * This code was written by Daniel Watt, Walter Deignan, Ryan Gabrys,
 * Alan Perez-Rathke and Ram Vedam.
 *
 * Redistribution and use in source and binary forms, with or
 * without modification, are permitted provided that the following
 * conditions are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above
 *    copyright notice, this list of conditions and the following
 *    disclaimer in the documentation and/or other materials provided
 *    with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY DANIEL WATT, WALTER DEIGNAN, RYAN
 * GABRYS, ALAN PEREZ-RATHKE AND RAM VEDAM ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL DANIEL WATT, WALTER DEIGNAN, RYAN
 * GABRYS, ALAN PEREZ-RATHKE AND RAM VEDAM BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE,DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
 * OF SUCH DAMAGE.
 */


#include "cd9660.h"
#include "cd9660_eltorito.h"
#include <inttypes.h>

/*
 * Partition Status Information from Apple Tech Note 1189
 */
#define	APPLE_PS_VALID		0x00000001	/* Entry is valid */
#define	APPLE_PS_ALLOCATED	0x00000002	/* Entry is allocated */
#define	APPLE_PS_READABLE	0x00000010	/* Entry is readable */
#define	APPLE_PS_WRITABLE	0x00000020	/* Entry is writable */

#ifdef DEBUG
#define	ELTORITO_DPRINTF(__x)	printf __x
#else
#define	ELTORITO_DPRINTF(__x)
#endif

static struct boot_catalog_entry *cd9660_init_boot_catalog_entry(void);
static struct boot_catalog_entry *cd9660_boot_setup_validation_entry(char);
static struct boot_catalog_entry *cd9660_boot_setup_default_entry(
    struct cd9660_boot_image *);
static struct boot_catalog_entry *cd9660_boot_setup_section_head(char);
static struct boot_catalog_entry *cd9660_boot_setup_validation_entry(char);
#if 0
static u_char cd9660_boot_get_system_type(struct cd9660_boot_image *);
#endif

int
cd9660_add_boot_disk(iso9660_disk *diskStructure, const char *boot_info)
{
	struct stat stbuf;
	const char *mode_msg;
	char *temp;
	char *sysname;
	char *filename;
	struct cd9660_boot_image *new_image, *tmp_image;

	assert(boot_info != NULL);

	if (*boot_info == '\0') {
		warnx("Error: Boot disk information must be in the "
		      "format 'system;filename'");
		return 0;
	}

	/* First decode the boot information */
	temp = estrdup(boot_info);

	sysname = temp;
	filename = strchr(sysname, ';');
	if (filename == NULL) {
		warnx("supply boot disk information in the format "
		    "'system;filename'");
		free(temp);
		return 0;
	}

	*filename++ = '\0';

	new_image = ecalloc(1, sizeof(*new_image));
	new_image->loadSegment = 0;	/* default for now */

	/* Decode System */
	if (strcmp(sysname, "i386") == 0)
		new_image->system = ET_SYS_X86;
	else if (strcmp(sysname, "powerpc") == 0)
		new_image->system = ET_SYS_PPC;
	else if (strcmp(sysname, "macppc") == 0)
		new_image->system = ET_SYS_MAC;
	else if (strcmp(sysname, "efi") == 0)
		new_image->system = ET_SYS_EFI;
	else {
		warnx("boot disk system must be "
		      "i386, macppc, powerpc, or efi");
		free(temp);
		free(new_image);
		return 0;
	}


	new_image->filename = estrdup(filename);

	free(temp);

	if (unveil(new_image->filename, "r") == -1)
		err(1, "unveil %s", new_image->filename);
	/* Get information about the file */
	if (lstat(new_image->filename, &stbuf) == -1)
		err(1, "%s: lstat(\"%s\")", __func__, new_image->filename);

	switch (stbuf.st_size) {
	case 1440 * 1024:
		new_image->targetMode = ET_MEDIA_144FDD;
		mode_msg = "Assigned boot image to 1.44 emulation mode";
		break;
	case 1200 * 1024:
		new_image->targetMode = ET_MEDIA_12FDD;
		mode_msg = "Assigned boot image to 1.2 emulation mode";
		break;
	case 2880 * 1024:
		new_image->targetMode = ET_MEDIA_288FDD;
		mode_msg = "Assigned boot image to 2.88 emulation mode";
		break;
	default:
		new_image->targetMode = ET_MEDIA_NOEM;
		mode_msg = "Assigned boot image to no emulation mode";
		break;
	}

	new_image->size = stbuf.st_size;
	new_image->num_sectors =
	    howmany(new_image->size, diskStructure->sectorSize) *
	    howmany(diskStructure->sectorSize, 512);
	new_image->sector = -1;
	/* Bootable by default */
	new_image->bootable = ET_BOOTABLE;
	/* Add boot disk */

	/* Group images for the same platform together. */
	TAILQ_FOREACH(tmp_image, &diskStructure->boot_images, image_list) {
		if (tmp_image->system != new_image->system)
			break;
	}

	if (tmp_image == NULL)
		TAILQ_INSERT_HEAD(&diskStructure->boot_images, new_image,
		    image_list);
	else
		TAILQ_INSERT_AFTER(&diskStructure->boot_images, tmp_image,
		    new_image, image_list);

	new_image->serialno = diskStructure->image_serialno++;

	/* TODO : Need to do anything about the boot image in the tree? */
	diskStructure->is_bootable = 1;

	return 1;
}

int
cd9660_eltorito_add_boot_option(iso9660_disk *diskStructure,
    const char *option_string, const char *value)
{
	char *eptr;
	struct cd9660_boot_image *image;

	assert(option_string != NULL);

	/* Find the last image added */
	TAILQ_FOREACH(image, &diskStructure->boot_images, image_list) {
		if (image->serialno + 1 == diskStructure->image_serialno)
			break;
	}
	if (image == NULL)
		errx(1, "Attempted to add boot option, "
		    "but no boot images have been specified");

	if (strcmp(option_string, "no-emul-boot") == 0) {
		image->targetMode = ET_MEDIA_NOEM;
	} else if (strcmp(option_string, "no-boot") == 0) {
		image->bootable = ET_NOT_BOOTABLE;
	} else if (strcmp(option_string, "hard-disk-boot") == 0) {
		image->targetMode = ET_MEDIA_HDD;
	} else if (strcmp(option_string, "boot-load-segment") == 0) {
		image->loadSegment = strtoul(value, &eptr, 16);
		if (eptr == value || *eptr != '\0' || errno != ERANGE) {
			warn("%s: strtoul", __func__);
			return 0;
		}
	} else {
		return 0;
	}
	return 1;
}

static struct boot_catalog_entry *
cd9660_init_boot_catalog_entry(void)
{
	return ecalloc(1, sizeof(struct boot_catalog_entry));
}

static struct boot_catalog_entry *
cd9660_boot_setup_validation_entry(char sys)
{
	struct boot_catalog_entry *entry;
	boot_catalog_validation_entry *ve;
	int16_t checksum;
	unsigned char *csptr;
	size_t i;
	entry = cd9660_init_boot_catalog_entry();

	entry->entry_type = ET_ENTRY_VE;
	ve = &entry->entry_data.VE;

	ve->header_id[0] = 1;
	ve->platform_id[0] = sys;
	ve->key[0] = 0x55;
	ve->key[1] = 0xAA;

	/* Calculate checksum */
	checksum = 0;
	cd9660_721(0, ve->checksum);
	csptr = (unsigned char*)ve;
	for (i = 0; i < sizeof(*ve); i += 2) {
		checksum += (int16_t)csptr[i];
		checksum += 256 * (int16_t)csptr[i + 1];
	}
	checksum = -checksum;
	cd9660_721(checksum, ve->checksum);

        ELTORITO_DPRINTF(("%s: header_id %d, platform_id %d, key[0] %d, key[1] %d, "
	    "checksum %04x\n", __func__, ve->header_id[0], ve->platform_id[0],
	    ve->key[0], ve->key[1], checksum));
	return entry;
}

static struct boot_catalog_entry *
cd9660_boot_setup_default_entry(struct cd9660_boot_image *disk)
{
	struct boot_catalog_entry *default_entry;
	boot_catalog_initial_entry *ie;

	default_entry = cd9660_init_boot_catalog_entry();
	if (default_entry == NULL)
		return NULL;

	default_entry->entry_type = ET_ENTRY_IE;
	ie = &default_entry->entry_data.IE;

	ie->boot_indicator[0] = disk->bootable;
	ie->media_type[0] = disk->targetMode;
	cd9660_721(disk->loadSegment, ie->load_segment);
	ie->system_type[0] = disk->system;
	cd9660_721(disk->num_sectors, ie->sector_count);
	cd9660_731(disk->sector, ie->load_rba);

	ELTORITO_DPRINTF(("%s: boot indicator %d, media type %d, "
	    "load segment %04x, system type %d, sector count %d, "
	    "load rba %d\n", __func__, ie->boot_indicator[0],
	    ie->media_type[0], disk->loadSegment, ie->system_type[0],
	    disk->num_sectors, disk->sector));
	return default_entry;
}

static struct boot_catalog_entry *
cd9660_boot_setup_section_head(char platform)
{
	struct boot_catalog_entry *entry;
	boot_catalog_section_header *sh;

	entry = cd9660_init_boot_catalog_entry();
	if (entry == NULL)
		return NULL;

	entry->entry_type = ET_ENTRY_SH;
	sh = &entry->entry_data.SH;
	/* More by default. The last one will manually be set to 0x91 */
	sh->header_indicator[0] = ET_SECTION_HEADER_MORE;
	sh->platform_id[0] = platform;
	sh->num_section_entries[0] = 0;
	return entry;
}

static struct boot_catalog_entry *
cd9660_boot_setup_section_entry(struct cd9660_boot_image *disk)
{
	struct boot_catalog_entry *entry;
	boot_catalog_section_entry *se;
	if ((entry = cd9660_init_boot_catalog_entry()) == NULL)
		return NULL;

	entry->entry_type = ET_ENTRY_SE;
	se = &entry->entry_data.SE;

	se->boot_indicator[0] = ET_BOOTABLE;
	se->media_type[0] = disk->targetMode;
	cd9660_721(disk->loadSegment, se->load_segment);
	cd9660_721(disk->num_sectors, se->sector_count);
	cd9660_731(disk->sector, se->load_rba);
	return entry;
}

#if 0
static u_char
cd9660_boot_get_system_type(struct cd9660_boot_image *disk)
{
	/*
		For hard drive booting, we need to examine the MBR to figure
		out what the partition type is
	*/
	return 0;
}
#endif

/*
 * Set up the BVD, Boot catalog, and the boot entries, but do no writing
 */
int
cd9660_setup_boot(iso9660_disk *diskStructure, int first_sector)
{
	int sector;
	int used_sectors;
	int num_entries = 0;
	int catalog_sectors;
	struct boot_catalog_entry *x86_head, *mac_head, *ppc_head, *efi_head,
		*valid_entry, *default_entry, *temp, *head, **headp, *next;
	struct cd9660_boot_image *tmp_disk;

	headp = NULL;
	x86_head = mac_head = ppc_head = efi_head = NULL;

	/* If there are no boot disks, don't bother building boot information */
	if (TAILQ_EMPTY(&diskStructure->boot_images))
		return 0;

	/* Point to catalog: For now assume it consumes one sector */
	ELTORITO_DPRINTF(("Boot catalog will go in sector %d\n", first_sector));
	diskStructure->boot_catalog_sector = first_sector;
	cd9660_bothendian_dword(first_sector,
		diskStructure->boot_descriptor->boot_catalog_pointer);

	/* Step 1: Generate boot catalog */
	/* Step 1a: Validation entry */
	valid_entry = cd9660_boot_setup_validation_entry(ET_SYS_X86);
	if (valid_entry == NULL)
		return -1;

	/*
	 * Count how many boot images there are,
	 * and how many sectors they consume.
	 */
	num_entries = 1;
	used_sectors = 0;

	TAILQ_FOREACH(tmp_disk, &diskStructure->boot_images, image_list) {
		used_sectors += tmp_disk->num_sectors;

		/* One default entry per image */
		num_entries++;
	}
	catalog_sectors = howmany(num_entries * 0x20, diskStructure->sectorSize);
	used_sectors += catalog_sectors;

	/* Populate sector numbers */
	sector = first_sector + catalog_sectors;
	TAILQ_FOREACH(tmp_disk, &diskStructure->boot_images, image_list) {
		tmp_disk->sector = sector;
		sector += tmp_disk->num_sectors;
	}

	LIST_INSERT_HEAD(&diskStructure->boot_entries, valid_entry, ll_struct);

	/* Step 1b: Initial/default entry */
	/* TODO : PARAM */
	tmp_disk = TAILQ_FIRST(&diskStructure->boot_images);
	default_entry = cd9660_boot_setup_default_entry(tmp_disk);
	if (default_entry == NULL) {
		warnx("Error: memory allocation failed in cd9660_setup_boot");
		return -1;
	}

	LIST_INSERT_AFTER(valid_entry, default_entry, ll_struct);

	/* Todo: multiple default entries? */

	tmp_disk = TAILQ_NEXT(tmp_disk, image_list);

	temp = default_entry;

	/* If multiple boot images are given : */
	while (tmp_disk != NULL) {
		/* Step 2: Section header */
		switch (tmp_disk->system) {
		case ET_SYS_X86:
			headp = &x86_head;
			break;
		case ET_SYS_PPC:
			headp = &ppc_head;
			break;
		case ET_SYS_MAC:
			headp = &mac_head;
			break;
		case ET_SYS_EFI:
			headp = &efi_head;
			break;
		default:
			warnx("%s: internal error: unknown system type",
			    __func__);
			return -1;
		}

		if (*headp == NULL) {
			head =
			    cd9660_boot_setup_section_head(tmp_disk->system);
			if (head == NULL) {
				warnx("Error: memory allocation failed in "
				      "cd9660_setup_boot");
				return -1;
			}
			LIST_INSERT_AFTER(default_entry, head, ll_struct);
			*headp = head;
		} else
			head = *headp;

		head->entry_data.SH.num_section_entries[0]++;

		/* Step 2a: Section entry and extensions */
		temp = cd9660_boot_setup_section_entry(tmp_disk);
		if (temp == NULL) {
			warn("%s: cd9660_boot_setup_section_entry", __func__);
			return -1;
		}

		while ((next = LIST_NEXT(head, ll_struct)) != NULL &&
		       next->entry_type == ET_ENTRY_SE)
			head = next;

		LIST_INSERT_AFTER(head, temp, ll_struct);
		tmp_disk = TAILQ_NEXT(tmp_disk, image_list);
	}

	/* TODO: Remaining boot disks when implemented */

	return first_sector + used_sectors;
}

int
cd9660_setup_boot_volume_descriptor(iso9660_disk *diskStructure,
    volume_descriptor *bvd)
{
	boot_volume_descriptor *bvdData =
	    (boot_volume_descriptor*)bvd->volumeDescriptorData;

	bvdData->boot_record_indicator[0] = ISO_VOLUME_DESCRIPTOR_BOOT;
	memcpy(bvdData->identifier, ISO_VOLUME_DESCRIPTOR_STANDARD_ID, 5);
	bvdData->version[0] = 1;
	memcpy(bvdData->boot_system_identifier, ET_ID, 23);
	memcpy(bvdData->identifier, ISO_VOLUME_DESCRIPTOR_STANDARD_ID, 5);
	diskStructure->boot_descriptor =
	    (boot_volume_descriptor*) bvd->volumeDescriptorData;
	return 1;
}

static int
cd9660_write_mbr_partition_entry(FILE *fd, int idx, off_t sector_start,
    off_t nsectors, int type)
{
	uint8_t val;
	uint32_t lba;

	if (fseeko(fd, (off_t)(idx) * 16 + 0x1be, SEEK_SET) == -1)
		err(1, "fseeko");

	val = 0x80; /* Bootable */
	fwrite(&val, sizeof(val), 1, fd);

	val = 0xff; /* CHS begin */
	fwrite(&val, sizeof(val), 1, fd);
	fwrite(&val, sizeof(val), 1, fd);
	fwrite(&val, sizeof(val), 1, fd);

	val = type; /* Part type */
	fwrite(&val, sizeof(val), 1, fd);

	val = 0xff; /* CHS end */
	fwrite(&val, sizeof(val), 1, fd);
	fwrite(&val, sizeof(val), 1, fd);
	fwrite(&val, sizeof(val), 1, fd);

	/* LBA extent */
	lba = htole32(sector_start);
	fwrite(&lba, sizeof(lba), 1, fd);
	lba = htole32(nsectors);
	fwrite(&lba, sizeof(lba), 1, fd);

	return 0;
}

static int
cd9660_write_apm_partition_entry(FILE *fd, int idx, int total_partitions,
    off_t sector_start, off_t nsectors, off_t sector_size,
    const char *part_name, const char *part_type)
{
	uint32_t apm32, part_status;
	uint16_t apm16;

	part_status = APPLE_PS_VALID | APPLE_PS_ALLOCATED | APPLE_PS_READABLE |
	    APPLE_PS_WRITABLE;

	if (fseeko(fd, (off_t)(idx + 1) * sector_size, SEEK_SET) == -1)
		err(1, "fseeko");

	/* Signature */
	apm16 = htobe16(0x504d);
	fwrite(&apm16, sizeof(apm16), 1, fd);
	apm16 = 0;
	fwrite(&apm16, sizeof(apm16), 1, fd);

	/* Total number of partitions */
	apm32 = htobe32(total_partitions);
	fwrite(&apm32, sizeof(apm32), 1, fd);
	/* Bounds */
	apm32 = htobe32(sector_start);
	fwrite(&apm32, sizeof(apm32), 1, fd);
	apm32 = htobe32(nsectors);
	fwrite(&apm32, sizeof(apm32), 1, fd);

	fwrite(part_name, strlen(part_name) + 1, 1, fd);
	fseek(fd, 32 - strlen(part_name) - 1, SEEK_CUR);
	fwrite(part_type, strlen(part_type) + 1, 1, fd);
	fseek(fd, 32 - strlen(part_type) - 1, SEEK_CUR);

	apm32 = 0;
	/* pmLgDataStart */
	fwrite(&apm32, sizeof(apm32), 1, fd);
	/* pmDataCnt */
	apm32 = htobe32(nsectors);
	fwrite(&apm32, sizeof(apm32), 1, fd);
	/* pmPartStatus */
	apm32 = htobe32(part_status);
	fwrite(&apm32, sizeof(apm32), 1, fd);

	return 0;
}

int
cd9660_write_boot(iso9660_disk *diskStructure, FILE *fd)
{
	struct boot_catalog_entry *e;
	struct cd9660_boot_image *t;
	int apm_partitions = 0;
	int mbr_partitions = 0;

	/* write boot catalog */
	if (fseeko(fd, (off_t)diskStructure->boot_catalog_sector *
	    diskStructure->sectorSize, SEEK_SET) == -1)
		err(1, "fseeko");

	LIST_FOREACH(e, &diskStructure->boot_entries, ll_struct) {
		/*
		 * It doesnt matter which one gets written
		 * since they are the same size
		 */
		fwrite(&(e->entry_data.VE), 1, 32, fd);
	}

	/* copy boot images */
	TAILQ_FOREACH(t, &diskStructure->boot_images, image_list) {
		cd9660_copy_file(diskStructure, fd, t->sector, t->filename);

		if (t->system == ET_SYS_MAC)
			apm_partitions++;
		if (t->system == ET_SYS_PPC)
			mbr_partitions++;
	}

	/* some systems need partition tables as well */
	if (mbr_partitions > 0) {
		uint16_t sig;

		fseek(fd, 0x1fe, SEEK_SET);
		sig = htole16(0xaa55);
		fwrite(&sig, sizeof(sig), 1, fd);

		mbr_partitions = 0;

		/* Write all partition entries */
		TAILQ_FOREACH(t, &diskStructure->boot_images, image_list) {
			if (t->system != ET_SYS_PPC)
				continue;
			cd9660_write_mbr_partition_entry(fd, mbr_partitions++,
			    t->sector * (diskStructure->sectorSize / 512),
			    t->num_sectors * (diskStructure->sectorSize / 512),
			    0x41 /* PReP Boot */);
		}
	}

	if (apm_partitions > 0) {
		/* Write DDR and global APM info */
		uint32_t apm32;
		uint16_t apm16;
		int total_parts;

		fseek(fd, 0, SEEK_SET);
		apm16 = htobe16(0x4552);
		fwrite(&apm16, sizeof(apm16), 1, fd);
		/* Device block size */
		apm16 = htobe16(512);
		fwrite(&apm16, sizeof(apm16), 1, fd);
		/* Device block count */
		apm32 = htobe32(diskStructure->totalSectors *
		    (diskStructure->sectorSize / 512));
		fwrite(&apm32, sizeof(apm32), 1, fd);
		/* Device type/id */
		apm16 = htobe16(1);
		fwrite(&apm16, sizeof(apm16), 1, fd);
		fwrite(&apm16, sizeof(apm16), 1, fd);

		/* Count total needed entries */
		total_parts = 2 + apm_partitions; /* Self + ISO9660 */

		/* Write self-descriptor */
		cd9660_write_apm_partition_entry(fd, 0, total_parts, 1,
		    total_parts, 512, "Apple", "Apple_partition_map");

		/* Write all partition entries */
		apm_partitions = 0;
		TAILQ_FOREACH(t, &diskStructure->boot_images, image_list) {
			if (t->system != ET_SYS_MAC)
				continue;

			cd9660_write_apm_partition_entry(fd,
			    1 + apm_partitions++, total_parts,
			    t->sector * (diskStructure->sectorSize / 512),
			    t->num_sectors * (diskStructure->sectorSize / 512),
			    512, "CD Boot", "Apple_Bootstrap");
		}

		/* Write ISO9660 descriptor, enclosing the whole disk */
		cd9660_write_apm_partition_entry(fd, 2 + apm_partitions,
		    total_parts, 0, diskStructure->totalSectors *
		    (diskStructure->sectorSize / 512), 512, "ISO9660",
		    "CD_ROM_Mode_1");
	}

	return 0;
}