GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.sbin/makefs/cd9660/cd9660_eltorito.c Lines: 0 267 0.0 %
Date: 2017-11-07 Branches: 0 116 0.0 %

Line Branch Exec Source
1
/*	$OpenBSD: cd9660_eltorito.c,v 1.9 2016/12/17 16:22:04 krw Exp $	*/
2
/*	$NetBSD: cd9660_eltorito.c,v 1.20 2013/01/28 21:03:28 christos Exp $	*/
3
4
/*
5
 * Copyright (c) 2005 Daniel Watt, Walter Deignan, Ryan Gabrys, Alan
6
 * Perez-Rathke and Ram Vedam.  All rights reserved.
7
 *
8
 * This code was written by Daniel Watt, Walter Deignan, Ryan Gabrys,
9
 * Alan Perez-Rathke and Ram Vedam.
10
 *
11
 * Redistribution and use in source and binary forms, with or
12
 * without modification, are permitted provided that the following
13
 * conditions are met:
14
 * 1. Redistributions of source code must retain the above copyright
15
 *    notice, this list of conditions and the following disclaimer.
16
 * 2. Redistributions in binary form must reproduce the above
17
 *    copyright notice, this list of conditions and the following
18
 *    disclaimer in the documentation and/or other materials provided
19
 *    with the distribution.
20
 *
21
 * THIS SOFTWARE IS PROVIDED BY DANIEL WATT, WALTER DEIGNAN, RYAN
22
 * GABRYS, ALAN PEREZ-RATHKE AND RAM VEDAM ``AS IS'' AND ANY EXPRESS OR
23
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25
 * DISCLAIMED.  IN NO EVENT SHALL DANIEL WATT, WALTER DEIGNAN, RYAN
26
 * GABRYS, ALAN PEREZ-RATHKE AND RAM VEDAM BE LIABLE FOR ANY DIRECT, INDIRECT,
27
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
29
 * USE,DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
33
 * OF SUCH DAMAGE.
34
 */
35
36
37
#include "cd9660.h"
38
#include "cd9660_eltorito.h"
39
#include <inttypes.h>
40
41
/*
42
 * Partition Status Information from Apple Tech Note 1189
43
 */
44
#define	APPLE_PS_VALID		0x00000001	/* Entry is valid */
45
#define	APPLE_PS_ALLOCATED	0x00000002	/* Entry is allocated */
46
#define	APPLE_PS_READABLE	0x00000010	/* Entry is readable */
47
#define	APPLE_PS_WRITABLE	0x00000020	/* Entry is writable */
48
49
#ifdef DEBUG
50
#define	ELTORITO_DPRINTF(__x)	printf __x
51
#else
52
#define	ELTORITO_DPRINTF(__x)
53
#endif
54
55
static struct boot_catalog_entry *cd9660_init_boot_catalog_entry(void);
56
static struct boot_catalog_entry *cd9660_boot_setup_validation_entry(char);
57
static struct boot_catalog_entry *cd9660_boot_setup_default_entry(
58
    struct cd9660_boot_image *);
59
static struct boot_catalog_entry *cd9660_boot_setup_section_head(char);
60
static struct boot_catalog_entry *cd9660_boot_setup_validation_entry(char);
61
#if 0
62
static u_char cd9660_boot_get_system_type(struct cd9660_boot_image *);
63
#endif
64
65
int
66
cd9660_add_boot_disk(iso9660_disk *diskStructure, const char *boot_info)
67
{
68
	struct stat stbuf;
69
	const char *mode_msg;
70
	char *temp;
71
	char *sysname;
72
	char *filename;
73
	struct cd9660_boot_image *new_image, *tmp_image;
74
75
	assert(boot_info != NULL);
76
77
	if (*boot_info == '\0') {
78
		warnx("Error: Boot disk information must be in the "
79
		      "format 'system;filename'");
80
		return 0;
81
	}
82
83
	/* First decode the boot information */
84
	temp = estrdup(boot_info);
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
	new_image = ecalloc(1, sizeof(*new_image));
98
	new_image->loadSegment = 0;	/* default for now */
99
100
	/* Decode System */
101
	if (strcmp(sysname, "i386") == 0)
102
		new_image->system = ET_SYS_X86;
103
	else if (strcmp(sysname, "powerpc") == 0)
104
		new_image->system = ET_SYS_PPC;
105
	else if (strcmp(sysname, "macppc") == 0)
106
		new_image->system = ET_SYS_MAC;
107
	else {
108
		warnx("boot disk system must be "
109
		      "i386, macppc, or powerpc");
110
		free(temp);
111
		free(new_image);
112
		return 0;
113
	}
114
115
116
	new_image->filename = estrdup(filename);
117
118
	free(temp);
119
120
	/* Get information about the file */
121
	if (lstat(new_image->filename, &stbuf) == -1)
122
		err(1, "%s: lstat(\"%s\")", __func__, new_image->filename);
123
124
	switch (stbuf.st_size) {
125
	case 1440 * 1024:
126
		new_image->targetMode = ET_MEDIA_144FDD;
127
		mode_msg = "Assigned boot image to 1.44 emulation mode";
128
		break;
129
	case 1200 * 1024:
130
		new_image->targetMode = ET_MEDIA_12FDD;
131
		mode_msg = "Assigned boot image to 1.2 emulation mode";
132
		break;
133
	case 2880 * 1024:
134
		new_image->targetMode = ET_MEDIA_288FDD;
135
		mode_msg = "Assigned boot image to 2.88 emulation mode";
136
		break;
137
	default:
138
		new_image->targetMode = ET_MEDIA_NOEM;
139
		mode_msg = "Assigned boot image to no emulation mode";
140
		break;
141
	}
142
143
	new_image->size = stbuf.st_size;
144
	new_image->num_sectors =
145
	    howmany(new_image->size, diskStructure->sectorSize) *
146
	    howmany(diskStructure->sectorSize, 512);
147
	new_image->sector = -1;
148
	/* Bootable by default */
149
	new_image->bootable = ET_BOOTABLE;
150
	/* Add boot disk */
151
152
	/* Group images for the same platform together. */
153
	TAILQ_FOREACH(tmp_image, &diskStructure->boot_images, image_list) {
154
		if (tmp_image->system != new_image->system)
155
			break;
156
	}
157
158
	if (tmp_image == NULL) {
159
		TAILQ_INSERT_HEAD(&diskStructure->boot_images, new_image,
160
		    image_list);
161
	} else
162
		TAILQ_INSERT_BEFORE(tmp_image, new_image, image_list);
163
164
	new_image->serialno = diskStructure->image_serialno++;
165
166
	/* TODO : Need to do anything about the boot image in the tree? */
167
	diskStructure->is_bootable = 1;
168
169
	return 1;
170
}
171
172
int
173
cd9660_eltorito_add_boot_option(iso9660_disk *diskStructure,
174
    const char *option_string, const char *value)
175
{
176
	char *eptr;
177
	struct cd9660_boot_image *image;
178
179
	assert(option_string != NULL);
180
181
	/* Find the last image added */
182
	TAILQ_FOREACH(image, &diskStructure->boot_images, image_list) {
183
		if (image->serialno + 1 == diskStructure->image_serialno)
184
			break;
185
	}
186
	if (image == NULL)
187
		errx(1, "Attempted to add boot option, "
188
		    "but no boot images have been specified");
189
190
	if (strcmp(option_string, "no-emul-boot") == 0) {
191
		image->targetMode = ET_MEDIA_NOEM;
192
	} else if (strcmp(option_string, "no-boot") == 0) {
193
		image->bootable = ET_NOT_BOOTABLE;
194
	} else if (strcmp(option_string, "hard-disk-boot") == 0) {
195
		image->targetMode = ET_MEDIA_HDD;
196
	} else if (strcmp(option_string, "boot-load-segment") == 0) {
197
		image->loadSegment = strtoul(value, &eptr, 16);
198
		if (eptr == value || *eptr != '\0' || errno != ERANGE) {
199
			warn("%s: strtoul", __func__);
200
			return 0;
201
		}
202
	} else {
203
		return 0;
204
	}
205
	return 1;
206
}
207
208
static struct boot_catalog_entry *
209
cd9660_init_boot_catalog_entry(void)
210
{
211
	return ecalloc(1, sizeof(struct boot_catalog_entry));
212
}
213
214
static struct boot_catalog_entry *
215
cd9660_boot_setup_validation_entry(char sys)
216
{
217
	struct boot_catalog_entry *entry;
218
	boot_catalog_validation_entry *ve;
219
	int16_t checksum;
220
	unsigned char *csptr;
221
	size_t i;
222
	entry = cd9660_init_boot_catalog_entry();
223
224
	ve = &entry->entry_data.VE;
225
226
	ve->header_id[0] = 1;
227
	ve->platform_id[0] = sys;
228
	ve->key[0] = 0x55;
229
	ve->key[1] = 0xAA;
230
231
	/* Calculate checksum */
232
	checksum = 0;
233
	cd9660_721(0, ve->checksum);
234
	csptr = (unsigned char*)ve;
235
	for (i = 0; i < sizeof(*ve); i += 2) {
236
		checksum += (int16_t)csptr[i];
237
		checksum += 256 * (int16_t)csptr[i + 1];
238
	}
239
	checksum = -checksum;
240
	cd9660_721(checksum, ve->checksum);
241
242
        ELTORITO_DPRINTF(("%s: header_id %d, platform_id %d, key[0] %d, key[1] %d, "
243
	    "checksum %04x\n", __func__, ve->header_id[0], ve->platform_id[0],
244
	    ve->key[0], ve->key[1], checksum));
245
	return entry;
246
}
247
248
static struct boot_catalog_entry *
249
cd9660_boot_setup_default_entry(struct cd9660_boot_image *disk)
250
{
251
	struct boot_catalog_entry *default_entry;
252
	boot_catalog_initial_entry *ie;
253
254
	default_entry = cd9660_init_boot_catalog_entry();
255
	if (default_entry == NULL)
256
		return NULL;
257
258
	ie = &default_entry->entry_data.IE;
259
260
	ie->boot_indicator[0] = disk->bootable;
261
	ie->media_type[0] = disk->targetMode;
262
	cd9660_721(disk->loadSegment, ie->load_segment);
263
	ie->system_type[0] = disk->system;
264
	cd9660_721(disk->num_sectors, ie->sector_count);
265
	cd9660_731(disk->sector, ie->load_rba);
266
267
	ELTORITO_DPRINTF(("%s: boot indicator %d, media type %d, "
268
	    "load segment %04x, system type %d, sector count %d, "
269
	    "load rba %d\n", __func__, ie->boot_indicator[0],
270
	    ie->media_type[0], disk->loadSegment, ie->system_type[0],
271
	    disk->num_sectors, disk->sector));
272
	return default_entry;
273
}
274
275
static struct boot_catalog_entry *
276
cd9660_boot_setup_section_head(char platform)
277
{
278
	struct boot_catalog_entry *entry;
279
	boot_catalog_section_header *sh;
280
281
	entry = cd9660_init_boot_catalog_entry();
282
	if (entry == NULL)
283
		return NULL;
284
285
	sh = &entry->entry_data.SH;
286
	/* More by default. The last one will manually be set to 0x91 */
287
	sh->header_indicator[0] = ET_SECTION_HEADER_MORE;
288
	sh->platform_id[0] = platform;
289
	sh->num_section_entries[0] = 0;
290
	return entry;
291
}
292
293
static struct boot_catalog_entry *
294
cd9660_boot_setup_section_entry(struct cd9660_boot_image *disk)
295
{
296
	struct boot_catalog_entry *entry;
297
	boot_catalog_section_entry *se;
298
	if ((entry = cd9660_init_boot_catalog_entry()) == NULL)
299
		return NULL;
300
301
	se = &entry->entry_data.SE;
302
303
	se->boot_indicator[0] = ET_BOOTABLE;
304
	se->media_type[0] = disk->targetMode;
305
	cd9660_721(disk->loadSegment, se->load_segment);
306
	cd9660_721(disk->num_sectors, se->sector_count);
307
	cd9660_731(disk->sector, se->load_rba);
308
	return entry;
309
}
310
311
#if 0
312
static u_char
313
cd9660_boot_get_system_type(struct cd9660_boot_image *disk)
314
{
315
	/*
316
		For hard drive booting, we need to examine the MBR to figure
317
		out what the partition type is
318
	*/
319
	return 0;
320
}
321
#endif
322
323
/*
324
 * Set up the BVD, Boot catalog, and the boot entries, but do no writing
325
 */
326
int
327
cd9660_setup_boot(iso9660_disk *diskStructure, int first_sector)
328
{
329
	int sector;
330
	int used_sectors;
331
	int num_entries = 0;
332
	int catalog_sectors;
333
	struct boot_catalog_entry *x86_head, *mac_head, *ppc_head,
334
		*valid_entry, *default_entry, *temp, *head, **headp, *next;
335
	struct cd9660_boot_image *tmp_disk;
336
337
	headp = NULL;
338
	x86_head = mac_head = ppc_head = NULL;
339
340
	/* If there are no boot disks, don't bother building boot information */
341
	if (TAILQ_EMPTY(&diskStructure->boot_images))
342
		return 0;
343
344
	/* Point to catalog: For now assume it consumes one sector */
345
	ELTORITO_DPRINTF(("Boot catalog will go in sector %d\n", first_sector));
346
	diskStructure->boot_catalog_sector = first_sector;
347
	cd9660_bothendian_dword(first_sector,
348
		diskStructure->boot_descriptor->boot_catalog_pointer);
349
350
	/* Step 1: Generate boot catalog */
351
	/* Step 1a: Validation entry */
352
	valid_entry = cd9660_boot_setup_validation_entry(ET_SYS_X86);
353
	if (valid_entry == NULL)
354
		return -1;
355
356
	/*
357
	 * Count how many boot images there are,
358
	 * and how many sectors they consume.
359
	 */
360
	num_entries = 1;
361
	used_sectors = 0;
362
363
	TAILQ_FOREACH(tmp_disk, &diskStructure->boot_images, image_list) {
364
		used_sectors += tmp_disk->num_sectors;
365
366
		/* One default entry per image */
367
		num_entries++;
368
	}
369
	catalog_sectors = howmany(num_entries * 0x20, diskStructure->sectorSize);
370
	used_sectors += catalog_sectors;
371
372
	/* Populate sector numbers */
373
	sector = first_sector + catalog_sectors;
374
	TAILQ_FOREACH(tmp_disk, &diskStructure->boot_images, image_list) {
375
		tmp_disk->sector = sector;
376
		sector += tmp_disk->num_sectors;
377
	}
378
379
	LIST_INSERT_HEAD(&diskStructure->boot_entries, valid_entry, ll_struct);
380
381
	/* Step 1b: Initial/default entry */
382
	/* TODO : PARAM */
383
	tmp_disk = TAILQ_FIRST(&diskStructure->boot_images);
384
	default_entry = cd9660_boot_setup_default_entry(tmp_disk);
385
	if (default_entry == NULL) {
386
		warnx("Error: memory allocation failed in cd9660_setup_boot");
387
		return -1;
388
	}
389
390
	LIST_INSERT_AFTER(valid_entry, default_entry, ll_struct);
391
392
	/* Todo: multiple default entries? */
393
394
	tmp_disk = TAILQ_NEXT(tmp_disk, image_list);
395
396
	temp = default_entry;
397
398
	/* If multiple boot images are given : */
399
	while (tmp_disk != NULL) {
400
		/* Step 2: Section header */
401
		switch (tmp_disk->system) {
402
		case ET_SYS_X86:
403
			headp = &x86_head;
404
			break;
405
		case ET_SYS_PPC:
406
			headp = &ppc_head;
407
			break;
408
		case ET_SYS_MAC:
409
			headp = &mac_head;
410
			break;
411
		default:
412
			warnx("%s: internal error: unknown system type",
413
			    __func__);
414
			return -1;
415
		}
416
417
		if (*headp == NULL) {
418
			head =
419
			    cd9660_boot_setup_section_head(tmp_disk->system);
420
			if (head == NULL) {
421
				warnx("Error: memory allocation failed in "
422
				      "cd9660_setup_boot");
423
				return -1;
424
			}
425
			LIST_INSERT_AFTER(default_entry, head, ll_struct);
426
			*headp = head;
427
		} else
428
			head = *headp;
429
430
		head->entry_data.SH.num_section_entries[0]++;
431
432
		/* Step 2a: Section entry and extensions */
433
		temp = cd9660_boot_setup_section_entry(tmp_disk);
434
		if (temp == NULL) {
435
			warn("%s: cd9660_boot_setup_section_entry", __func__);
436
			return -1;
437
		}
438
439
		while ((next = LIST_NEXT(head, ll_struct)) != NULL &&
440
		       next->entry_type == ET_ENTRY_SE)
441
			head = next;
442
443
		LIST_INSERT_AFTER(head, temp, ll_struct);
444
		tmp_disk = TAILQ_NEXT(tmp_disk, image_list);
445
	}
446
447
	/* TODO: Remaining boot disks when implemented */
448
449
	return first_sector + used_sectors;
450
}
451
452
int
453
cd9660_setup_boot_volume_descriptor(iso9660_disk *diskStructure,
454
    volume_descriptor *bvd)
455
{
456
	boot_volume_descriptor *bvdData =
457
	    (boot_volume_descriptor*)bvd->volumeDescriptorData;
458
459
	bvdData->boot_record_indicator[0] = ISO_VOLUME_DESCRIPTOR_BOOT;
460
	memcpy(bvdData->identifier, ISO_VOLUME_DESCRIPTOR_STANDARD_ID, 5);
461
	bvdData->version[0] = 1;
462
	memcpy(bvdData->boot_system_identifier, ET_ID, 23);
463
	memcpy(bvdData->identifier, ISO_VOLUME_DESCRIPTOR_STANDARD_ID, 5);
464
	diskStructure->boot_descriptor =
465
	    (boot_volume_descriptor*) bvd->volumeDescriptorData;
466
	return 1;
467
}
468
469
static int
470
cd9660_write_mbr_partition_entry(FILE *fd, int idx, off_t sector_start,
471
    off_t nsectors, int type)
472
{
473
	uint8_t val;
474
	uint32_t lba;
475
476
	if (fseeko(fd, (off_t)(idx) * 16 + 0x1be, SEEK_SET) == -1)
477
		err(1, "fseeko");
478
479
	val = 0x80; /* Bootable */
480
	fwrite(&val, sizeof(val), 1, fd);
481
482
	val = 0xff; /* CHS begin */
483
	fwrite(&val, sizeof(val), 1, fd);
484
	fwrite(&val, sizeof(val), 1, fd);
485
	fwrite(&val, sizeof(val), 1, fd);
486
487
	val = type; /* Part type */
488
	fwrite(&val, sizeof(val), 1, fd);
489
490
	val = 0xff; /* CHS end */
491
	fwrite(&val, sizeof(val), 1, fd);
492
	fwrite(&val, sizeof(val), 1, fd);
493
	fwrite(&val, sizeof(val), 1, fd);
494
495
	/* LBA extent */
496
	lba = htole32(sector_start);
497
	fwrite(&lba, sizeof(lba), 1, fd);
498
	lba = htole32(nsectors);
499
	fwrite(&lba, sizeof(lba), 1, fd);
500
501
	return 0;
502
}
503
504
static int
505
cd9660_write_apm_partition_entry(FILE *fd, int idx, int total_partitions,
506
    off_t sector_start, off_t nsectors, off_t sector_size,
507
    const char *part_name, const char *part_type)
508
{
509
	uint32_t apm32, part_status;
510
	uint16_t apm16;
511
512
	part_status = APPLE_PS_VALID | APPLE_PS_ALLOCATED | APPLE_PS_READABLE |
513
	    APPLE_PS_WRITABLE;
514
515
	if (fseeko(fd, (off_t)(idx + 1) * sector_size, SEEK_SET) == -1)
516
		err(1, "fseeko");
517
518
	/* Signature */
519
	apm16 = htobe16(0x504d);
520
	fwrite(&apm16, sizeof(apm16), 1, fd);
521
	apm16 = 0;
522
	fwrite(&apm16, sizeof(apm16), 1, fd);
523
524
	/* Total number of partitions */
525
	apm32 = htobe32(total_partitions);
526
	fwrite(&apm32, sizeof(apm32), 1, fd);
527
	/* Bounds */
528
	apm32 = htobe32(sector_start);
529
	fwrite(&apm32, sizeof(apm32), 1, fd);
530
	apm32 = htobe32(nsectors);
531
	fwrite(&apm32, sizeof(apm32), 1, fd);
532
533
	fwrite(part_name, strlen(part_name) + 1, 1, fd);
534
	fseek(fd, 32 - strlen(part_name) - 1, SEEK_CUR);
535
	fwrite(part_type, strlen(part_type) + 1, 1, fd);
536
	fseek(fd, 32 - strlen(part_type) - 1, SEEK_CUR);
537
538
	apm32 = 0;
539
	/* pmLgDataStart */
540
	fwrite(&apm32, sizeof(apm32), 1, fd);
541
	/* pmDataCnt */
542
	apm32 = htobe32(nsectors);
543
	fwrite(&apm32, sizeof(apm32), 1, fd);
544
	/* pmPartStatus */
545
	apm32 = htobe32(part_status);
546
	fwrite(&apm32, sizeof(apm32), 1, fd);
547
548
	return 0;
549
}
550
551
int
552
cd9660_write_boot(iso9660_disk *diskStructure, FILE *fd)
553
{
554
	struct boot_catalog_entry *e;
555
	struct cd9660_boot_image *t;
556
	int apm_partitions = 0;
557
	int mbr_partitions = 0;
558
559
	/* write boot catalog */
560
	if (fseeko(fd, (off_t)diskStructure->boot_catalog_sector *
561
	    diskStructure->sectorSize, SEEK_SET) == -1)
562
		err(1, "fseeko");
563
564
	LIST_FOREACH(e, &diskStructure->boot_entries, ll_struct) {
565
		/*
566
		 * It doesnt matter which one gets written
567
		 * since they are the same size
568
		 */
569
		fwrite(&(e->entry_data.VE), 1, 32, fd);
570
	}
571
572
	/* copy boot images */
573
	TAILQ_FOREACH(t, &diskStructure->boot_images, image_list) {
574
		cd9660_copy_file(diskStructure, fd, t->sector, t->filename);
575
576
		if (t->system == ET_SYS_MAC)
577
			apm_partitions++;
578
		if (t->system == ET_SYS_PPC)
579
			mbr_partitions++;
580
	}
581
582
	/* some systems need partition tables as well */
583
	if (mbr_partitions > 0) {
584
		uint16_t sig;
585
586
		fseek(fd, 0x1fe, SEEK_SET);
587
		sig = htole16(0xaa55);
588
		fwrite(&sig, sizeof(sig), 1, fd);
589
590
		mbr_partitions = 0;
591
592
		/* Write all partition entries */
593
		TAILQ_FOREACH(t, &diskStructure->boot_images, image_list) {
594
			if (t->system != ET_SYS_PPC)
595
				continue;
596
			cd9660_write_mbr_partition_entry(fd, mbr_partitions++,
597
			    t->sector * (diskStructure->sectorSize / 512),
598
			    t->num_sectors * (diskStructure->sectorSize / 512),
599
			    0x41 /* PReP Boot */);
600
		}
601
	}
602
603
	if (apm_partitions > 0) {
604
		/* Write DDR and global APM info */
605
		uint32_t apm32;
606
		uint16_t apm16;
607
		int total_parts;
608
609
		fseek(fd, 0, SEEK_SET);
610
		apm16 = htobe16(0x4552);
611
		fwrite(&apm16, sizeof(apm16), 1, fd);
612
		/* Device block size */
613
		apm16 = htobe16(512);
614
		fwrite(&apm16, sizeof(apm16), 1, fd);
615
		/* Device block count */
616
		apm32 = htobe32(diskStructure->totalSectors *
617
		    (diskStructure->sectorSize / 512));
618
		fwrite(&apm32, sizeof(apm32), 1, fd);
619
		/* Device type/id */
620
		apm16 = htobe16(1);
621
		fwrite(&apm16, sizeof(apm16), 1, fd);
622
		fwrite(&apm16, sizeof(apm16), 1, fd);
623
624
		/* Count total needed entries */
625
		total_parts = 2 + apm_partitions; /* Self + ISO9660 */
626
627
		/* Write self-descriptor */
628
		cd9660_write_apm_partition_entry(fd, 0, total_parts, 1,
629
		    total_parts, 512, "Apple", "Apple_partition_map");
630
631
		/* Write all partition entries */
632
		apm_partitions = 0;
633
		TAILQ_FOREACH(t, &diskStructure->boot_images, image_list) {
634
			if (t->system != ET_SYS_MAC)
635
				continue;
636
637
			cd9660_write_apm_partition_entry(fd,
638
			    1 + apm_partitions++, total_parts,
639
			    t->sector * (diskStructure->sectorSize / 512),
640
			    t->num_sectors * (diskStructure->sectorSize / 512),
641
			    512, "CD Boot", "Apple_Bootstrap");
642
		}
643
644
		/* Write ISO9660 descriptor, enclosing the whole disk */
645
		cd9660_write_apm_partition_entry(fd, 2 + apm_partitions,
646
		    total_parts, 0, diskStructure->totalSectors *
647
		    (diskStructure->sectorSize / 512), 512, "ISO9660",
648
		    "CD_ROM_Mode_1");
649
	}
650
651
	return 0;
652
}