xref: /freebsd/usr.sbin/makefs/cd9660/cd9660_eltorito.c (revision 1d386b48a555f61cb7325543adbbb5c3f3407a66)
1756e49c3SBenno Rice /*	$NetBSD: cd9660_eltorito.c,v 1.23 2018/03/28 06:48:55 nonaka Exp $	*/
201a0f853SOlivier Houchard 
31de7b4b8SPedro F. Giffuni /*-
4*b61a5730SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
51de7b4b8SPedro F. Giffuni  *
601a0f853SOlivier Houchard  * Copyright (c) 2005 Daniel Watt, Walter Deignan, Ryan Gabrys, Alan
701a0f853SOlivier Houchard  * Perez-Rathke and Ram Vedam.  All rights reserved.
801a0f853SOlivier Houchard  *
901a0f853SOlivier Houchard  * This code was written by Daniel Watt, Walter Deignan, Ryan Gabrys,
1001a0f853SOlivier Houchard  * Alan Perez-Rathke and Ram Vedam.
1101a0f853SOlivier Houchard  *
1201a0f853SOlivier Houchard  * Redistribution and use in source and binary forms, with or
1301a0f853SOlivier Houchard  * without modification, are permitted provided that the following
1401a0f853SOlivier Houchard  * conditions are met:
1501a0f853SOlivier Houchard  * 1. Redistributions of source code must retain the above copyright
1601a0f853SOlivier Houchard  *    notice, this list of conditions and the following disclaimer.
1701a0f853SOlivier Houchard  * 2. Redistributions in binary form must reproduce the above
1801a0f853SOlivier Houchard  *    copyright notice, this list of conditions and the following
1901a0f853SOlivier Houchard  *    disclaimer in the documentation and/or other materials provided
2001a0f853SOlivier Houchard  *    with the distribution.
2101a0f853SOlivier Houchard  *
2201a0f853SOlivier Houchard  * THIS SOFTWARE IS PROVIDED BY DANIEL WATT, WALTER DEIGNAN, RYAN
2301a0f853SOlivier Houchard  * GABRYS, ALAN PEREZ-RATHKE AND RAM VEDAM ``AS IS'' AND ANY EXPRESS OR
2401a0f853SOlivier Houchard  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2501a0f853SOlivier Houchard  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
2601a0f853SOlivier Houchard  * DISCLAIMED.  IN NO EVENT SHALL DANIEL WATT, WALTER DEIGNAN, RYAN
2701a0f853SOlivier Houchard  * GABRYS, ALAN PEREZ-RATHKE AND RAM VEDAM BE LIABLE FOR ANY DIRECT, INDIRECT,
2801a0f853SOlivier Houchard  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2901a0f853SOlivier Houchard  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
3001a0f853SOlivier Houchard  * USE,DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
3101a0f853SOlivier Houchard  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
3201a0f853SOlivier Houchard  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3301a0f853SOlivier Houchard  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
3401a0f853SOlivier Houchard  * OF SUCH DAMAGE.
3501a0f853SOlivier Houchard  */
36b3fa6d2cSNathan Whitehorn 
3701a0f853SOlivier Houchard #include "cd9660.h"
3801a0f853SOlivier Houchard #include "cd9660_eltorito.h"
395f5598b1SEd Maste #include <util.h>
4001a0f853SOlivier Houchard 
4101a0f853SOlivier Houchard #include <sys/cdefs.h>
42f0bef3d2SEd Maste /*
43f0bef3d2SEd Maste  * Partition Status Information from Apple Tech Note 1189
44f0bef3d2SEd Maste  */
45f0bef3d2SEd Maste #define	APPLE_PS_VALID		0x00000001	/* Entry is valid */
46f0bef3d2SEd Maste #define	APPLE_PS_ALLOCATED	0x00000002	/* Entry is allocated */
47f0bef3d2SEd Maste #define	APPLE_PS_READABLE	0x00000010	/* Entry is readable */
48f0bef3d2SEd Maste #define	APPLE_PS_WRITABLE	0x00000020	/* Entry is writable */
49f0bef3d2SEd Maste 
5001a0f853SOlivier Houchard #ifdef DEBUG
5101a0f853SOlivier Houchard #define	ELTORITO_DPRINTF(__x)	printf __x
5201a0f853SOlivier Houchard #else
5301a0f853SOlivier Houchard #define	ELTORITO_DPRINTF(__x)
5401a0f853SOlivier Houchard #endif
5501a0f853SOlivier Houchard 
56756e49c3SBenno Rice #include <util.h>
57756e49c3SBenno Rice 
5801a0f853SOlivier Houchard static struct boot_catalog_entry *cd9660_init_boot_catalog_entry(void);
5901a0f853SOlivier Houchard static struct boot_catalog_entry *cd9660_boot_setup_validation_entry(char);
6001a0f853SOlivier Houchard static struct boot_catalog_entry *cd9660_boot_setup_default_entry(
6101a0f853SOlivier Houchard     struct cd9660_boot_image *);
6201a0f853SOlivier Houchard static struct boot_catalog_entry *cd9660_boot_setup_section_head(char);
6301a0f853SOlivier Houchard #if 0
6401a0f853SOlivier Houchard static u_char cd9660_boot_get_system_type(struct cd9660_boot_image *);
6501a0f853SOlivier Houchard #endif
6601a0f853SOlivier Houchard 
67756e49c3SBenno Rice static struct cd9660_boot_image *default_boot_image;
68756e49c3SBenno Rice 
6901a0f853SOlivier Houchard int
cd9660_add_boot_disk(iso9660_disk * diskStructure,const char * boot_info)701631d42aSEd Maste cd9660_add_boot_disk(iso9660_disk *diskStructure, const char *boot_info)
7101a0f853SOlivier Houchard {
7201a0f853SOlivier Houchard 	struct stat stbuf;
7301a0f853SOlivier Houchard 	const char *mode_msg;
7401a0f853SOlivier Houchard 	char *temp;
7501a0f853SOlivier Houchard 	char *sysname;
7601a0f853SOlivier Houchard 	char *filename;
7701a0f853SOlivier Houchard 	struct cd9660_boot_image *new_image, *tmp_image;
7801a0f853SOlivier Houchard 
7901a0f853SOlivier Houchard 	assert(boot_info != NULL);
8001a0f853SOlivier Houchard 
8101a0f853SOlivier Houchard 	if (*boot_info == '\0') {
8201a0f853SOlivier Houchard 		warnx("Error: Boot disk information must be in the "
8301a0f853SOlivier Houchard 		      "format 'system;filename'");
8401a0f853SOlivier Houchard 		return 0;
8501a0f853SOlivier Houchard 	}
8601a0f853SOlivier Houchard 
8701a0f853SOlivier Houchard 	/* First decode the boot information */
885f5598b1SEd Maste 	temp = estrdup(boot_info);
8901a0f853SOlivier Houchard 
9001a0f853SOlivier Houchard 	sysname = temp;
9101a0f853SOlivier Houchard 	filename = strchr(sysname, ';');
9201a0f853SOlivier Houchard 	if (filename == NULL) {
9301a0f853SOlivier Houchard 		warnx("supply boot disk information in the format "
9401a0f853SOlivier Houchard 		    "'system;filename'");
9501a0f853SOlivier Houchard 		free(temp);
9601a0f853SOlivier Houchard 		return 0;
9701a0f853SOlivier Houchard 	}
9801a0f853SOlivier Houchard 
9901a0f853SOlivier Houchard 	*filename++ = '\0';
10001a0f853SOlivier Houchard 
1011631d42aSEd Maste 	if (diskStructure->verbose_level > 0) {
10201a0f853SOlivier Houchard 		printf("Found bootdisk with system %s, and filename %s\n",
10301a0f853SOlivier Houchard 		    sysname, filename);
10401a0f853SOlivier Houchard 	}
1055f5598b1SEd Maste 	new_image = ecalloc(1, sizeof(*new_image));
10601a0f853SOlivier Houchard 	new_image->loadSegment = 0;	/* default for now */
10701a0f853SOlivier Houchard 
10801a0f853SOlivier Houchard 	/* Decode System */
10901a0f853SOlivier Houchard 	if (strcmp(sysname, "i386") == 0)
11001a0f853SOlivier Houchard 		new_image->system = ET_SYS_X86;
11101a0f853SOlivier Houchard 	else if (strcmp(sysname, "powerpc") == 0)
11201a0f853SOlivier Houchard 		new_image->system = ET_SYS_PPC;
11301a0f853SOlivier Houchard 	else if (strcmp(sysname, "macppc") == 0 ||
11401a0f853SOlivier Houchard 	         strcmp(sysname, "mac68k") == 0)
11501a0f853SOlivier Houchard 		new_image->system = ET_SYS_MAC;
11653d8e037SEd Maste 	else if (strcmp(sysname, "efi") == 0)
11753d8e037SEd Maste 		new_image->system = ET_SYS_EFI;
11801a0f853SOlivier Houchard 	else {
11901a0f853SOlivier Houchard 		warnx("boot disk system must be "
12053d8e037SEd Maste 		      "i386, powerpc, macppc, mac68k, or efi");
12101a0f853SOlivier Houchard 		free(temp);
12201a0f853SOlivier Houchard 		free(new_image);
12301a0f853SOlivier Houchard 		return 0;
12401a0f853SOlivier Houchard 	}
12501a0f853SOlivier Houchard 
12601a0f853SOlivier Houchard 
1275f5598b1SEd Maste 	new_image->filename = estrdup(filename);
12801a0f853SOlivier Houchard 
12901a0f853SOlivier Houchard 	free(temp);
13001a0f853SOlivier Houchard 
13101a0f853SOlivier Houchard 	/* Get information about the file */
13201a0f853SOlivier Houchard 	if (lstat(new_image->filename, &stbuf) == -1)
13301a0f853SOlivier Houchard 		err(EXIT_FAILURE, "%s: lstat(\"%s\")", __func__,
13401a0f853SOlivier Houchard 		    new_image->filename);
13501a0f853SOlivier Houchard 
13601a0f853SOlivier Houchard 	switch (stbuf.st_size) {
13701a0f853SOlivier Houchard 	case 1440 * 1024:
13801a0f853SOlivier Houchard 		new_image->targetMode = ET_MEDIA_144FDD;
13901a0f853SOlivier Houchard 		mode_msg = "Assigned boot image to 1.44 emulation mode";
14001a0f853SOlivier Houchard 		break;
14101a0f853SOlivier Houchard 	case 1200 * 1024:
14201a0f853SOlivier Houchard 		new_image->targetMode = ET_MEDIA_12FDD;
14301a0f853SOlivier Houchard 		mode_msg = "Assigned boot image to 1.2 emulation mode";
14401a0f853SOlivier Houchard 		break;
14501a0f853SOlivier Houchard 	case 2880 * 1024:
14601a0f853SOlivier Houchard 		new_image->targetMode = ET_MEDIA_288FDD;
14701a0f853SOlivier Houchard 		mode_msg = "Assigned boot image to 2.88 emulation mode";
14801a0f853SOlivier Houchard 		break;
14901a0f853SOlivier Houchard 	default:
15001a0f853SOlivier Houchard 		new_image->targetMode = ET_MEDIA_NOEM;
15101a0f853SOlivier Houchard 		mode_msg = "Assigned boot image to no emulation mode";
15201a0f853SOlivier Houchard 		break;
15301a0f853SOlivier Houchard 	}
15401a0f853SOlivier Houchard 
1551631d42aSEd Maste 	if (diskStructure->verbose_level > 0)
15601a0f853SOlivier Houchard 		printf("%s\n", mode_msg);
15701a0f853SOlivier Houchard 
15801a0f853SOlivier Houchard 	new_image->size = stbuf.st_size;
15901a0f853SOlivier Houchard 	new_image->num_sectors =
1601631d42aSEd Maste 	    howmany(new_image->size, diskStructure->sectorSize) *
1611631d42aSEd Maste 	    howmany(diskStructure->sectorSize, 512);
1621631d42aSEd Maste 	if (diskStructure->verbose_level > 0) {
16301a0f853SOlivier Houchard 		printf("New image has size %d, uses %d 512-byte sectors\n",
16401a0f853SOlivier Houchard 		    new_image->size, new_image->num_sectors);
16501a0f853SOlivier Houchard 	}
16601a0f853SOlivier Houchard 	new_image->sector = -1;
16701a0f853SOlivier Houchard 	/* Bootable by default */
16801a0f853SOlivier Houchard 	new_image->bootable = ET_BOOTABLE;
16901a0f853SOlivier Houchard 	/* Add boot disk */
17001a0f853SOlivier Houchard 
17101a0f853SOlivier Houchard 	/* Group images for the same platform together. */
1721631d42aSEd Maste 	TAILQ_FOREACH(tmp_image, &diskStructure->boot_images, image_list) {
17301a0f853SOlivier Houchard 		if (tmp_image->system != new_image->system)
17401a0f853SOlivier Houchard 			break;
17501a0f853SOlivier Houchard 	}
17601a0f853SOlivier Houchard 
17701a0f853SOlivier Houchard 	if (tmp_image == NULL) {
1781631d42aSEd Maste 		TAILQ_INSERT_HEAD(&diskStructure->boot_images, new_image,
17901a0f853SOlivier Houchard 		    image_list);
18001a0f853SOlivier Houchard 	} else
18101a0f853SOlivier Houchard 		TAILQ_INSERT_BEFORE(tmp_image, new_image, image_list);
18201a0f853SOlivier Houchard 
1831631d42aSEd Maste 	new_image->serialno = diskStructure->image_serialno++;
18401a0f853SOlivier Houchard 
185756e49c3SBenno Rice 	new_image->platform_id = new_image->system;
186756e49c3SBenno Rice 
18701a0f853SOlivier Houchard 	/* TODO : Need to do anything about the boot image in the tree? */
1881631d42aSEd Maste 	diskStructure->is_bootable = 1;
18901a0f853SOlivier Houchard 
190756e49c3SBenno Rice 	/* First boot image is initial/default entry. */
191756e49c3SBenno Rice 	if (default_boot_image == NULL)
192756e49c3SBenno Rice 		default_boot_image = new_image;
193756e49c3SBenno Rice 
19401a0f853SOlivier Houchard 	return 1;
19501a0f853SOlivier Houchard }
19601a0f853SOlivier Houchard 
19701a0f853SOlivier Houchard int
cd9660_eltorito_add_boot_option(iso9660_disk * diskStructure,const char * option_string,const char * value)1981631d42aSEd Maste cd9660_eltorito_add_boot_option(iso9660_disk *diskStructure,
1991631d42aSEd Maste     const char *option_string, const char *value)
20001a0f853SOlivier Houchard {
20101a0f853SOlivier Houchard 	char *eptr;
20201a0f853SOlivier Houchard 	struct cd9660_boot_image *image;
20301a0f853SOlivier Houchard 
20401a0f853SOlivier Houchard 	assert(option_string != NULL);
20501a0f853SOlivier Houchard 
20601a0f853SOlivier Houchard 	/* Find the last image added */
2071631d42aSEd Maste 	TAILQ_FOREACH(image, &diskStructure->boot_images, image_list) {
2081631d42aSEd Maste 		if (image->serialno + 1 == diskStructure->image_serialno)
20901a0f853SOlivier Houchard 			break;
21001a0f853SOlivier Houchard 	}
21101a0f853SOlivier Houchard 	if (image == NULL)
21201a0f853SOlivier Houchard 		errx(EXIT_FAILURE, "Attempted to add boot option, "
21301a0f853SOlivier Houchard 		    "but no boot images have been specified");
21401a0f853SOlivier Houchard 
21501a0f853SOlivier Houchard 	if (strcmp(option_string, "no-emul-boot") == 0) {
21601a0f853SOlivier Houchard 		image->targetMode = ET_MEDIA_NOEM;
21701a0f853SOlivier Houchard 	} else if (strcmp(option_string, "no-boot") == 0) {
21801a0f853SOlivier Houchard 		image->bootable = ET_NOT_BOOTABLE;
21901a0f853SOlivier Houchard 	} else if (strcmp(option_string, "hard-disk-boot") == 0) {
22001a0f853SOlivier Houchard 		image->targetMode = ET_MEDIA_HDD;
22101a0f853SOlivier Houchard 	} else if (strcmp(option_string, "boot-load-segment") == 0) {
22201a0f853SOlivier Houchard 		image->loadSegment = strtoul(value, &eptr, 16);
22301a0f853SOlivier Houchard 		if (eptr == value || *eptr != '\0' || errno != ERANGE) {
22401a0f853SOlivier Houchard 			warn("%s: strtoul", __func__);
22501a0f853SOlivier Houchard 			return 0;
22601a0f853SOlivier Houchard 		}
227756e49c3SBenno Rice 	} else if (strcmp(option_string, "platformid") == 0) {
228756e49c3SBenno Rice 		if (strcmp(value, "efi") == 0)
229756e49c3SBenno Rice 			image->platform_id = ET_SYS_EFI;
230756e49c3SBenno Rice 		else {
231756e49c3SBenno Rice 			warn("%s: unknown platform: %s", __func__, value);
232756e49c3SBenno Rice 			return 0;
233756e49c3SBenno Rice 		}
23401a0f853SOlivier Houchard 	} else {
23501a0f853SOlivier Houchard 		return 0;
23601a0f853SOlivier Houchard 	}
23701a0f853SOlivier Houchard 	return 1;
23801a0f853SOlivier Houchard }
23901a0f853SOlivier Houchard 
24001a0f853SOlivier Houchard static struct boot_catalog_entry *
cd9660_init_boot_catalog_entry(void)24101a0f853SOlivier Houchard cd9660_init_boot_catalog_entry(void)
24201a0f853SOlivier Houchard {
2435f5598b1SEd Maste 	return ecalloc(1, sizeof(struct boot_catalog_entry));
24401a0f853SOlivier Houchard }
24501a0f853SOlivier Houchard 
24601a0f853SOlivier Houchard static struct boot_catalog_entry *
cd9660_boot_setup_validation_entry(char sys)24701a0f853SOlivier Houchard cd9660_boot_setup_validation_entry(char sys)
24801a0f853SOlivier Houchard {
24901a0f853SOlivier Houchard 	struct boot_catalog_entry *entry;
25001a0f853SOlivier Houchard 	boot_catalog_validation_entry *ve;
25101a0f853SOlivier Houchard 	int16_t checksum;
25201a0f853SOlivier Houchard 	unsigned char *csptr;
253856d87c5SEd Maste 	size_t i;
25401a0f853SOlivier Houchard 	entry = cd9660_init_boot_catalog_entry();
25501a0f853SOlivier Houchard 
256756e49c3SBenno Rice 	entry->entry_type = ET_ENTRY_VE;
25701a0f853SOlivier Houchard 	ve = &entry->entry_data.VE;
25801a0f853SOlivier Houchard 
25901a0f853SOlivier Houchard 	ve->header_id[0] = 1;
26001a0f853SOlivier Houchard 	ve->platform_id[0] = sys;
26101a0f853SOlivier Houchard 	ve->key[0] = 0x55;
26201a0f853SOlivier Houchard 	ve->key[1] = 0xAA;
26301a0f853SOlivier Houchard 
26401a0f853SOlivier Houchard 	/* Calculate checksum */
26501a0f853SOlivier Houchard 	checksum = 0;
26601a0f853SOlivier Houchard 	cd9660_721(0, ve->checksum);
26701a0f853SOlivier Houchard 	csptr = (unsigned char*)ve;
26801a0f853SOlivier Houchard 	for (i = 0; i < sizeof(*ve); i += 2) {
26901a0f853SOlivier Houchard 		checksum += (int16_t)csptr[i];
27001a0f853SOlivier Houchard 		checksum += 256 * (int16_t)csptr[i + 1];
27101a0f853SOlivier Houchard 	}
27201a0f853SOlivier Houchard 	checksum = -checksum;
27301a0f853SOlivier Houchard 	cd9660_721(checksum, ve->checksum);
27401a0f853SOlivier Houchard 
27501a0f853SOlivier Houchard         ELTORITO_DPRINTF(("%s: header_id %d, platform_id %d, key[0] %d, key[1] %d, "
27601a0f853SOlivier Houchard 	    "checksum %04x\n", __func__, ve->header_id[0], ve->platform_id[0],
27701a0f853SOlivier Houchard 	    ve->key[0], ve->key[1], checksum));
27801a0f853SOlivier Houchard 	return entry;
27901a0f853SOlivier Houchard }
28001a0f853SOlivier Houchard 
28101a0f853SOlivier Houchard static struct boot_catalog_entry *
cd9660_boot_setup_default_entry(struct cd9660_boot_image * disk)28201a0f853SOlivier Houchard cd9660_boot_setup_default_entry(struct cd9660_boot_image *disk)
28301a0f853SOlivier Houchard {
28401a0f853SOlivier Houchard 	struct boot_catalog_entry *default_entry;
28501a0f853SOlivier Houchard 	boot_catalog_initial_entry *ie;
28601a0f853SOlivier Houchard 
28701a0f853SOlivier Houchard 	default_entry = cd9660_init_boot_catalog_entry();
28801a0f853SOlivier Houchard 	if (default_entry == NULL)
28901a0f853SOlivier Houchard 		return NULL;
29001a0f853SOlivier Houchard 
291756e49c3SBenno Rice 	default_entry->entry_type = ET_ENTRY_IE;
29201a0f853SOlivier Houchard 	ie = &default_entry->entry_data.IE;
29301a0f853SOlivier Houchard 
29401a0f853SOlivier Houchard 	ie->boot_indicator[0] = disk->bootable;
29501a0f853SOlivier Houchard 	ie->media_type[0] = disk->targetMode;
29601a0f853SOlivier Houchard 	cd9660_721(disk->loadSegment, ie->load_segment);
29701a0f853SOlivier Houchard 	ie->system_type[0] = disk->system;
29801a0f853SOlivier Houchard 	cd9660_721(disk->num_sectors, ie->sector_count);
29901a0f853SOlivier Houchard 	cd9660_731(disk->sector, ie->load_rba);
30001a0f853SOlivier Houchard 
30101a0f853SOlivier Houchard 	ELTORITO_DPRINTF(("%s: boot indicator %d, media type %d, "
30201a0f853SOlivier Houchard 	    "load segment %04x, system type %d, sector count %d, "
30301a0f853SOlivier Houchard 	    "load rba %d\n", __func__, ie->boot_indicator[0],
30401a0f853SOlivier Houchard 	    ie->media_type[0], disk->loadSegment, ie->system_type[0],
30501a0f853SOlivier Houchard 	    disk->num_sectors, disk->sector));
30601a0f853SOlivier Houchard 	return default_entry;
30701a0f853SOlivier Houchard }
30801a0f853SOlivier Houchard 
30901a0f853SOlivier Houchard static struct boot_catalog_entry *
cd9660_boot_setup_section_head(char platform)31001a0f853SOlivier Houchard cd9660_boot_setup_section_head(char platform)
31101a0f853SOlivier Houchard {
31201a0f853SOlivier Houchard 	struct boot_catalog_entry *entry;
31301a0f853SOlivier Houchard 	boot_catalog_section_header *sh;
31401a0f853SOlivier Houchard 
31501a0f853SOlivier Houchard 	entry = cd9660_init_boot_catalog_entry();
31601a0f853SOlivier Houchard 	if (entry == NULL)
31701a0f853SOlivier Houchard 		return NULL;
31801a0f853SOlivier Houchard 
319756e49c3SBenno Rice 	entry->entry_type = ET_ENTRY_SH;
32001a0f853SOlivier Houchard 	sh = &entry->entry_data.SH;
321ebf1c089SBenno Rice 	/*
322ebf1c089SBenno Rice 	 * More by default.
323ebf1c089SBenno Rice 	 * The last one will manually be set to ET_SECTION_HEADER_LAST
324ebf1c089SBenno Rice 	 */
32501a0f853SOlivier Houchard 	sh->header_indicator[0] = ET_SECTION_HEADER_MORE;
32601a0f853SOlivier Houchard 	sh->platform_id[0] = platform;
32701a0f853SOlivier Houchard 	sh->num_section_entries[0] = 0;
32801a0f853SOlivier Houchard 	return entry;
32901a0f853SOlivier Houchard }
33001a0f853SOlivier Houchard 
33101a0f853SOlivier Houchard static struct boot_catalog_entry *
cd9660_boot_setup_section_entry(struct cd9660_boot_image * disk)33201a0f853SOlivier Houchard cd9660_boot_setup_section_entry(struct cd9660_boot_image *disk)
33301a0f853SOlivier Houchard {
33401a0f853SOlivier Houchard 	struct boot_catalog_entry *entry;
33501a0f853SOlivier Houchard 	boot_catalog_section_entry *se;
33601a0f853SOlivier Houchard 	if ((entry = cd9660_init_boot_catalog_entry()) == NULL)
33701a0f853SOlivier Houchard 		return NULL;
33801a0f853SOlivier Houchard 
339756e49c3SBenno Rice 	entry->entry_type = ET_ENTRY_SE;
34001a0f853SOlivier Houchard 	se = &entry->entry_data.SE;
34101a0f853SOlivier Houchard 
34201a0f853SOlivier Houchard 	se->boot_indicator[0] = ET_BOOTABLE;
34301a0f853SOlivier Houchard 	se->media_type[0] = disk->targetMode;
34401a0f853SOlivier Houchard 	cd9660_721(disk->loadSegment, se->load_segment);
34501a0f853SOlivier Houchard 	cd9660_721(disk->num_sectors, se->sector_count);
34601a0f853SOlivier Houchard 	cd9660_731(disk->sector, se->load_rba);
34701a0f853SOlivier Houchard 	return entry;
34801a0f853SOlivier Houchard }
34901a0f853SOlivier Houchard 
35001a0f853SOlivier Houchard #if 0
35101a0f853SOlivier Houchard static u_char
35201a0f853SOlivier Houchard cd9660_boot_get_system_type(struct cd9660_boot_image *disk)
35301a0f853SOlivier Houchard {
35401a0f853SOlivier Houchard 	/*
35501a0f853SOlivier Houchard 		For hard drive booting, we need to examine the MBR to figure
35601a0f853SOlivier Houchard 		out what the partition type is
35701a0f853SOlivier Houchard 	*/
35801a0f853SOlivier Houchard 	return 0;
35901a0f853SOlivier Houchard }
36001a0f853SOlivier Houchard #endif
36101a0f853SOlivier Houchard 
36201a0f853SOlivier Houchard /*
36301a0f853SOlivier Houchard  * Set up the BVD, Boot catalog, and the boot entries, but do no writing
36401a0f853SOlivier Houchard  */
36501a0f853SOlivier Houchard int
cd9660_setup_boot(iso9660_disk * diskStructure,int first_sector)3661631d42aSEd Maste cd9660_setup_boot(iso9660_disk *diskStructure, int first_sector)
36701a0f853SOlivier Houchard {
36801a0f853SOlivier Houchard 	int sector;
36901a0f853SOlivier Houchard 	int used_sectors;
37001a0f853SOlivier Houchard 	int num_entries = 0;
37101a0f853SOlivier Houchard 	int catalog_sectors;
372756e49c3SBenno Rice 	struct boot_catalog_entry *x86_head, *mac_head, *ppc_head, *efi_head,
37301a0f853SOlivier Houchard 		*valid_entry, *default_entry, *temp, *head, **headp, *next;
37401a0f853SOlivier Houchard 	struct cd9660_boot_image *tmp_disk;
37522db5e5dSEd Maste 	uint8_t system;
37601a0f853SOlivier Houchard 
37701a0f853SOlivier Houchard 	headp = NULL;
378756e49c3SBenno Rice 	x86_head = mac_head = ppc_head = efi_head = NULL;
37901a0f853SOlivier Houchard 
38001a0f853SOlivier Houchard 	/* If there are no boot disks, don't bother building boot information */
3811631d42aSEd Maste 	if (TAILQ_EMPTY(&diskStructure->boot_images))
38201a0f853SOlivier Houchard 		return 0;
38301a0f853SOlivier Houchard 
38401a0f853SOlivier Houchard 	/* Point to catalog: For now assume it consumes one sector */
38501a0f853SOlivier Houchard 	ELTORITO_DPRINTF(("Boot catalog will go in sector %d\n", first_sector));
3861631d42aSEd Maste 	diskStructure->boot_catalog_sector = first_sector;
3879f2a5253SEd Maste 	cd9660_731(first_sector,
3881631d42aSEd Maste 	    diskStructure->boot_descriptor->boot_catalog_pointer);
38901a0f853SOlivier Houchard 
39022db5e5dSEd Maste 	/*
39122db5e5dSEd Maste 	 * Use system type of default image for validation entry. Fallback to
39222db5e5dSEd Maste 	 * X86 system type if not found.
39322db5e5dSEd Maste 	 */
39422db5e5dSEd Maste 	system = default_boot_image != NULL ? default_boot_image->system :
39522db5e5dSEd Maste 	    ET_SYS_X86;
39622db5e5dSEd Maste 
39701a0f853SOlivier Houchard 	/* Step 1: Generate boot catalog */
39801a0f853SOlivier Houchard 	/* Step 1a: Validation entry */
39922db5e5dSEd Maste 	valid_entry = cd9660_boot_setup_validation_entry(system);
40001a0f853SOlivier Houchard 	if (valid_entry == NULL)
40101a0f853SOlivier Houchard 		return -1;
40201a0f853SOlivier Houchard 
40301a0f853SOlivier Houchard 	/*
40401a0f853SOlivier Houchard 	 * Count how many boot images there are,
40501a0f853SOlivier Houchard 	 * and how many sectors they consume.
40601a0f853SOlivier Houchard 	 */
40701a0f853SOlivier Houchard 	num_entries = 1;
40801a0f853SOlivier Houchard 	used_sectors = 0;
40901a0f853SOlivier Houchard 
4101631d42aSEd Maste 	TAILQ_FOREACH(tmp_disk, &diskStructure->boot_images, image_list) {
41101a0f853SOlivier Houchard 		used_sectors += tmp_disk->num_sectors;
41201a0f853SOlivier Houchard 
41301a0f853SOlivier Houchard 		/* One default entry per image */
41401a0f853SOlivier Houchard 		num_entries++;
41501a0f853SOlivier Houchard 	}
4161631d42aSEd Maste 	catalog_sectors = howmany(num_entries * 0x20, diskStructure->sectorSize);
41701a0f853SOlivier Houchard 	used_sectors += catalog_sectors;
41801a0f853SOlivier Houchard 
4191631d42aSEd Maste 	if (diskStructure->verbose_level > 0) {
42001a0f853SOlivier Houchard 		printf("%s: there will be %i entries consuming %i sectors. "
42101a0f853SOlivier Houchard 		       "Catalog is %i sectors\n", __func__, num_entries,
42201a0f853SOlivier Houchard 		       used_sectors, catalog_sectors);
42301a0f853SOlivier Houchard 	}
42401a0f853SOlivier Houchard 
42501a0f853SOlivier Houchard 	/* Populate sector numbers */
42601a0f853SOlivier Houchard 	sector = first_sector + catalog_sectors;
4271631d42aSEd Maste 	TAILQ_FOREACH(tmp_disk, &diskStructure->boot_images, image_list) {
42801a0f853SOlivier Houchard 		tmp_disk->sector = sector;
429756e49c3SBenno Rice 		sector += tmp_disk->num_sectors /
430756e49c3SBenno Rice 		    (diskStructure->sectorSize / 512);
43101a0f853SOlivier Houchard 	}
43201a0f853SOlivier Houchard 
4331631d42aSEd Maste 	LIST_INSERT_HEAD(&diskStructure->boot_entries, valid_entry, ll_struct);
43401a0f853SOlivier Houchard 
43501a0f853SOlivier Houchard 	/* Step 1b: Initial/default entry */
43601a0f853SOlivier Houchard 	/* TODO : PARAM */
437756e49c3SBenno Rice 	if (default_boot_image != NULL) {
438756e49c3SBenno Rice 		struct cd9660_boot_image *tcbi;
439756e49c3SBenno Rice 		TAILQ_FOREACH(tcbi, &diskStructure->boot_images, image_list) {
440756e49c3SBenno Rice 			if (tcbi == default_boot_image) {
441756e49c3SBenno Rice 				tmp_disk = tcbi;
442756e49c3SBenno Rice 				break;
443756e49c3SBenno Rice 			}
444756e49c3SBenno Rice 		}
445756e49c3SBenno Rice 	}
446756e49c3SBenno Rice 	if (tmp_disk == NULL)
4471631d42aSEd Maste 		tmp_disk = TAILQ_FIRST(&diskStructure->boot_images);
44801a0f853SOlivier Houchard 	default_entry = cd9660_boot_setup_default_entry(tmp_disk);
44901a0f853SOlivier Houchard 	if (default_entry == NULL) {
45001a0f853SOlivier Houchard 		warnx("Error: memory allocation failed in cd9660_setup_boot");
45101a0f853SOlivier Houchard 		return -1;
45201a0f853SOlivier Houchard 	}
45301a0f853SOlivier Houchard 
45401a0f853SOlivier Houchard 	LIST_INSERT_AFTER(valid_entry, default_entry, ll_struct);
45501a0f853SOlivier Houchard 
45601a0f853SOlivier Houchard 	/* Todo: multiple default entries? */
45701a0f853SOlivier Houchard 
458756e49c3SBenno Rice 	tmp_disk = TAILQ_FIRST(&diskStructure->boot_images);
45901a0f853SOlivier Houchard 
460756e49c3SBenno Rice 	head = NULL;
46101a0f853SOlivier Houchard 	temp = default_entry;
46201a0f853SOlivier Houchard 
46301a0f853SOlivier Houchard 	/* If multiple boot images are given : */
464756e49c3SBenno Rice 	for (; tmp_disk != NULL; tmp_disk = TAILQ_NEXT(tmp_disk, image_list)) {
465756e49c3SBenno Rice 		if (tmp_disk == default_boot_image)
466756e49c3SBenno Rice 			continue;
467756e49c3SBenno Rice 
46801a0f853SOlivier Houchard 		/* Step 2: Section header */
469756e49c3SBenno Rice 		switch (tmp_disk->platform_id) {
47001a0f853SOlivier Houchard 		case ET_SYS_X86:
47101a0f853SOlivier Houchard 			headp = &x86_head;
47201a0f853SOlivier Houchard 			break;
47301a0f853SOlivier Houchard 		case ET_SYS_PPC:
47401a0f853SOlivier Houchard 			headp = &ppc_head;
47501a0f853SOlivier Houchard 			break;
47601a0f853SOlivier Houchard 		case ET_SYS_MAC:
47701a0f853SOlivier Houchard 			headp = &mac_head;
47801a0f853SOlivier Houchard 			break;
479756e49c3SBenno Rice 		case ET_SYS_EFI:
480756e49c3SBenno Rice 			headp = &efi_head;
4818dbc3905SBenno Rice 			break;
48201a0f853SOlivier Houchard 		default:
48301a0f853SOlivier Houchard 			warnx("%s: internal error: unknown system type",
48401a0f853SOlivier Houchard 			    __func__);
48501a0f853SOlivier Houchard 			return -1;
48601a0f853SOlivier Houchard 		}
48701a0f853SOlivier Houchard 
48801a0f853SOlivier Houchard 		if (*headp == NULL) {
48901a0f853SOlivier Houchard 			head =
490756e49c3SBenno Rice 			  cd9660_boot_setup_section_head(tmp_disk->platform_id);
49101a0f853SOlivier Houchard 			if (head == NULL) {
49201a0f853SOlivier Houchard 				warnx("Error: memory allocation failed in "
49301a0f853SOlivier Houchard 				      "cd9660_setup_boot");
49401a0f853SOlivier Houchard 				return -1;
49501a0f853SOlivier Houchard 			}
49601a0f853SOlivier Houchard 			LIST_INSERT_AFTER(default_entry, head, ll_struct);
49701a0f853SOlivier Houchard 			*headp = head;
49801a0f853SOlivier Houchard 		} else
49901a0f853SOlivier Houchard 			head = *headp;
50001a0f853SOlivier Houchard 
50101a0f853SOlivier Houchard 		head->entry_data.SH.num_section_entries[0]++;
50201a0f853SOlivier Houchard 
50301a0f853SOlivier Houchard 		/* Step 2a: Section entry and extensions */
50401a0f853SOlivier Houchard 		temp = cd9660_boot_setup_section_entry(tmp_disk);
50501a0f853SOlivier Houchard 		if (temp == NULL) {
50601a0f853SOlivier Houchard 			warn("%s: cd9660_boot_setup_section_entry", __func__);
50701a0f853SOlivier Houchard 			return -1;
50801a0f853SOlivier Houchard 		}
50901a0f853SOlivier Houchard 
51001a0f853SOlivier Houchard 		while ((next = LIST_NEXT(head, ll_struct)) != NULL &&
51101a0f853SOlivier Houchard 		       next->entry_type == ET_ENTRY_SE)
51201a0f853SOlivier Houchard 			head = next;
51301a0f853SOlivier Houchard 
51401a0f853SOlivier Houchard 		LIST_INSERT_AFTER(head, temp, ll_struct);
51501a0f853SOlivier Houchard 	}
51601a0f853SOlivier Houchard 
517ebf1c089SBenno Rice 	/* Find the last Section Header entry and mark it as the last. */
518633cb765SBenno Rice 	head = NULL;
519ebf1c089SBenno Rice 	LIST_FOREACH(next, &diskStructure->boot_entries, ll_struct) {
520ebf1c089SBenno Rice 		if (next->entry_type == ET_ENTRY_SH)
521ebf1c089SBenno Rice 			head = next;
522ebf1c089SBenno Rice 	}
523633cb765SBenno Rice 	if (head != NULL)
524756e49c3SBenno Rice 		head->entry_data.SH.header_indicator[0] = ET_SECTION_HEADER_LAST;
525ebf1c089SBenno Rice 
52601a0f853SOlivier Houchard 	/* TODO: Remaining boot disks when implemented */
52701a0f853SOlivier Houchard 
52801a0f853SOlivier Houchard 	return first_sector + used_sectors;
52901a0f853SOlivier Houchard }
53001a0f853SOlivier Houchard 
53101a0f853SOlivier Houchard int
cd9660_setup_boot_volume_descriptor(iso9660_disk * diskStructure,volume_descriptor * bvd)5321631d42aSEd Maste cd9660_setup_boot_volume_descriptor(iso9660_disk *diskStructure,
5331631d42aSEd Maste     volume_descriptor *bvd)
53401a0f853SOlivier Houchard {
53501a0f853SOlivier Houchard 	boot_volume_descriptor *bvdData =
53601a0f853SOlivier Houchard 	    (boot_volume_descriptor*)bvd->volumeDescriptorData;
53701a0f853SOlivier Houchard 
53801a0f853SOlivier Houchard 	bvdData->boot_record_indicator[0] = ISO_VOLUME_DESCRIPTOR_BOOT;
53901a0f853SOlivier Houchard 	memcpy(bvdData->identifier, ISO_VOLUME_DESCRIPTOR_STANDARD_ID, 5);
54001a0f853SOlivier Houchard 	bvdData->version[0] = 1;
54101a0f853SOlivier Houchard 	memcpy(bvdData->boot_system_identifier, ET_ID, 23);
54201a0f853SOlivier Houchard 	memcpy(bvdData->identifier, ISO_VOLUME_DESCRIPTOR_STANDARD_ID, 5);
5431631d42aSEd Maste 	diskStructure->boot_descriptor =
54401a0f853SOlivier Houchard 	    (boot_volume_descriptor*) bvd->volumeDescriptorData;
54501a0f853SOlivier Houchard 	return 1;
54601a0f853SOlivier Houchard }
54701a0f853SOlivier Houchard 
548b3fa6d2cSNathan Whitehorn static int
cd9660_write_mbr_partition_entry(FILE * fd,int idx,off_t sector_start,off_t nsectors,int type)549852f933dSMarius Strobl cd9660_write_mbr_partition_entry(FILE *fd, int idx, off_t sector_start,
550df73c41aSNathan Whitehorn     off_t nsectors, int type)
551df73c41aSNathan Whitehorn {
552df73c41aSNathan Whitehorn 	uint8_t val;
553df73c41aSNathan Whitehorn 	uint32_t lba;
554df73c41aSNathan Whitehorn 
555852f933dSMarius Strobl 	if (fseeko(fd, (off_t)(idx) * 16 + 0x1be, SEEK_SET) == -1)
556852f933dSMarius Strobl 		err(1, "fseeko");
557df73c41aSNathan Whitehorn 
558df73c41aSNathan Whitehorn 	val = 0x80; /* Bootable */
559df73c41aSNathan Whitehorn 	fwrite(&val, sizeof(val), 1, fd);
560df73c41aSNathan Whitehorn 
561df73c41aSNathan Whitehorn 	val = 0xff; /* CHS begin */
562df73c41aSNathan Whitehorn 	fwrite(&val, sizeof(val), 1, fd);
563df73c41aSNathan Whitehorn 	fwrite(&val, sizeof(val), 1, fd);
564df73c41aSNathan Whitehorn 	fwrite(&val, sizeof(val), 1, fd);
565df73c41aSNathan Whitehorn 
566df73c41aSNathan Whitehorn 	val = type; /* Part type */
567df73c41aSNathan Whitehorn 	fwrite(&val, sizeof(val), 1, fd);
568df73c41aSNathan Whitehorn 
569df73c41aSNathan Whitehorn 	val = 0xff; /* CHS end */
570df73c41aSNathan Whitehorn 	fwrite(&val, sizeof(val), 1, fd);
571df73c41aSNathan Whitehorn 	fwrite(&val, sizeof(val), 1, fd);
572df73c41aSNathan Whitehorn 	fwrite(&val, sizeof(val), 1, fd);
573df73c41aSNathan Whitehorn 
574df73c41aSNathan Whitehorn 	/* LBA extent */
575df73c41aSNathan Whitehorn 	lba = htole32(sector_start);
576df73c41aSNathan Whitehorn 	fwrite(&lba, sizeof(lba), 1, fd);
577df73c41aSNathan Whitehorn 	lba = htole32(nsectors);
578df73c41aSNathan Whitehorn 	fwrite(&lba, sizeof(lba), 1, fd);
579df73c41aSNathan Whitehorn 
580852f933dSMarius Strobl 	return 0;
581df73c41aSNathan Whitehorn }
582df73c41aSNathan Whitehorn 
583df73c41aSNathan Whitehorn 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)584852f933dSMarius Strobl cd9660_write_apm_partition_entry(FILE *fd, int idx, int total_partitions,
585b3fa6d2cSNathan Whitehorn     off_t sector_start, off_t nsectors, off_t sector_size,
586df73c41aSNathan Whitehorn     const char *part_name, const char *part_type)
587df73c41aSNathan Whitehorn {
588bff5cf16SAndreas Tobler 	uint32_t apm32, part_status;
589b3fa6d2cSNathan Whitehorn 	uint16_t apm16;
590b3fa6d2cSNathan Whitehorn 
591f0bef3d2SEd Maste 	part_status = APPLE_PS_VALID | APPLE_PS_ALLOCATED | APPLE_PS_READABLE |
592f0bef3d2SEd Maste 	    APPLE_PS_WRITABLE;
593bff5cf16SAndreas Tobler 
594852f933dSMarius Strobl 	if (fseeko(fd, (off_t)(idx + 1) * sector_size, SEEK_SET) == -1)
595852f933dSMarius Strobl 		err(1, "fseeko");
596b3fa6d2cSNathan Whitehorn 
597b3fa6d2cSNathan Whitehorn 	/* Signature */
598df73c41aSNathan Whitehorn 	apm16 = htobe16(0x504d);
599b3fa6d2cSNathan Whitehorn 	fwrite(&apm16, sizeof(apm16), 1, fd);
600b3fa6d2cSNathan Whitehorn 	apm16 = 0;
601b3fa6d2cSNathan Whitehorn 	fwrite(&apm16, sizeof(apm16), 1, fd);
602b3fa6d2cSNathan Whitehorn 
603b3fa6d2cSNathan Whitehorn 	/* Total number of partitions */
604df73c41aSNathan Whitehorn 	apm32 = htobe32(total_partitions);
605b3fa6d2cSNathan Whitehorn 	fwrite(&apm32, sizeof(apm32), 1, fd);
606b3fa6d2cSNathan Whitehorn 	/* Bounds */
607df73c41aSNathan Whitehorn 	apm32 = htobe32(sector_start);
608b3fa6d2cSNathan Whitehorn 	fwrite(&apm32, sizeof(apm32), 1, fd);
609df73c41aSNathan Whitehorn 	apm32 = htobe32(nsectors);
610b3fa6d2cSNathan Whitehorn 	fwrite(&apm32, sizeof(apm32), 1, fd);
611b3fa6d2cSNathan Whitehorn 
612b3fa6d2cSNathan Whitehorn 	fwrite(part_name, strlen(part_name) + 1, 1, fd);
613b3fa6d2cSNathan Whitehorn 	fseek(fd, 32 - strlen(part_name) - 1, SEEK_CUR);
614b3fa6d2cSNathan Whitehorn 	fwrite(part_type, strlen(part_type) + 1, 1, fd);
615bff5cf16SAndreas Tobler 	fseek(fd, 32 - strlen(part_type) - 1, SEEK_CUR);
616bff5cf16SAndreas Tobler 
617bff5cf16SAndreas Tobler 	apm32 = 0;
618bff5cf16SAndreas Tobler 	/* pmLgDataStart */
619bff5cf16SAndreas Tobler 	fwrite(&apm32, sizeof(apm32), 1, fd);
620bff5cf16SAndreas Tobler 	/* pmDataCnt */
621bff5cf16SAndreas Tobler 	apm32 = htobe32(nsectors);
622bff5cf16SAndreas Tobler 	fwrite(&apm32, sizeof(apm32), 1, fd);
623bff5cf16SAndreas Tobler 	/* pmPartStatus */
624bff5cf16SAndreas Tobler 	apm32 = htobe32(part_status);
625bff5cf16SAndreas Tobler 	fwrite(&apm32, sizeof(apm32), 1, fd);
626b3fa6d2cSNathan Whitehorn 
627b3fa6d2cSNathan Whitehorn 	return 0;
628b3fa6d2cSNathan Whitehorn }
629b3fa6d2cSNathan Whitehorn 
63001a0f853SOlivier Houchard int
cd9660_write_boot(iso9660_disk * diskStructure,FILE * fd)6311631d42aSEd Maste cd9660_write_boot(iso9660_disk *diskStructure, FILE *fd)
63201a0f853SOlivier Houchard {
63301a0f853SOlivier Houchard 	struct boot_catalog_entry *e;
63401a0f853SOlivier Houchard 	struct cd9660_boot_image *t;
635b3fa6d2cSNathan Whitehorn 	int apm_partitions = 0;
636df73c41aSNathan Whitehorn 	int mbr_partitions = 0;
63701a0f853SOlivier Houchard 
63801a0f853SOlivier Houchard 	/* write boot catalog */
6391631d42aSEd Maste 	if (fseeko(fd, (off_t)diskStructure->boot_catalog_sector *
6401631d42aSEd Maste 	    diskStructure->sectorSize, SEEK_SET) == -1)
64101a0f853SOlivier Houchard 		err(1, "fseeko");
64201a0f853SOlivier Houchard 
6431631d42aSEd Maste 	if (diskStructure->verbose_level > 0) {
64401a0f853SOlivier Houchard 		printf("Writing boot catalog to sector %" PRId64 "\n",
6451631d42aSEd Maste 		    diskStructure->boot_catalog_sector);
64601a0f853SOlivier Houchard 	}
6471631d42aSEd Maste 	LIST_FOREACH(e, &diskStructure->boot_entries, ll_struct) {
6481631d42aSEd Maste 		if (diskStructure->verbose_level > 0) {
64901a0f853SOlivier Houchard 			printf("Writing catalog entry of type %d\n",
65001a0f853SOlivier Houchard 			    e->entry_type);
65101a0f853SOlivier Houchard 		}
65201a0f853SOlivier Houchard 		/*
6533df5ecacSUlrich Spörlein 		 * It doesn't matter which one gets written
65401a0f853SOlivier Houchard 		 * since they are the same size
65501a0f853SOlivier Houchard 		 */
65601a0f853SOlivier Houchard 		fwrite(&(e->entry_data.VE), 1, 32, fd);
65701a0f853SOlivier Houchard 	}
6581631d42aSEd Maste 	if (diskStructure->verbose_level > 0)
65901a0f853SOlivier Houchard 		printf("Finished writing boot catalog\n");
66001a0f853SOlivier Houchard 
66101a0f853SOlivier Houchard 	/* copy boot images */
6621631d42aSEd Maste 	TAILQ_FOREACH(t, &diskStructure->boot_images, image_list) {
6631631d42aSEd Maste 		if (diskStructure->verbose_level > 0) {
66401a0f853SOlivier Houchard 			printf("Writing boot image from %s to sectors %d\n",
66501a0f853SOlivier Houchard 			    t->filename, t->sector);
66601a0f853SOlivier Houchard 		}
6671631d42aSEd Maste 		cd9660_copy_file(diskStructure, fd, t->sector, t->filename);
668b3fa6d2cSNathan Whitehorn 
669b3fa6d2cSNathan Whitehorn 		if (t->system == ET_SYS_MAC)
670b3fa6d2cSNathan Whitehorn 			apm_partitions++;
671df73c41aSNathan Whitehorn 		if (t->system == ET_SYS_PPC)
672df73c41aSNathan Whitehorn 			mbr_partitions++;
673df73c41aSNathan Whitehorn 	}
674df73c41aSNathan Whitehorn 
675df73c41aSNathan Whitehorn 	/* some systems need partition tables as well */
6761631d42aSEd Maste 	if (mbr_partitions > 0 || diskStructure->chrp_boot) {
677df73c41aSNathan Whitehorn 		uint16_t sig;
678df73c41aSNathan Whitehorn 
679df73c41aSNathan Whitehorn 		fseek(fd, 0x1fe, SEEK_SET);
680df73c41aSNathan Whitehorn 		sig = htole16(0xaa55);
681df73c41aSNathan Whitehorn 		fwrite(&sig, sizeof(sig), 1, fd);
682df73c41aSNathan Whitehorn 
683df73c41aSNathan Whitehorn 		mbr_partitions = 0;
684df73c41aSNathan Whitehorn 
685df73c41aSNathan Whitehorn 		/* Write ISO9660 descriptor, enclosing the whole disk */
6861631d42aSEd Maste 		if (diskStructure->chrp_boot)
687df73c41aSNathan Whitehorn 			cd9660_write_mbr_partition_entry(fd, mbr_partitions++,
6881631d42aSEd Maste 			    0, diskStructure->totalSectors *
6891631d42aSEd Maste 			    (diskStructure->sectorSize / 512), 0x96);
690df73c41aSNathan Whitehorn 
691df73c41aSNathan Whitehorn 		/* Write all partition entries */
6921631d42aSEd Maste 		TAILQ_FOREACH(t, &diskStructure->boot_images, image_list) {
693df73c41aSNathan Whitehorn 			if (t->system != ET_SYS_PPC)
694df73c41aSNathan Whitehorn 				continue;
695df73c41aSNathan Whitehorn 			cd9660_write_mbr_partition_entry(fd, mbr_partitions++,
6961631d42aSEd Maste 			    t->sector * (diskStructure->sectorSize / 512),
6971631d42aSEd Maste 			    t->num_sectors * (diskStructure->sectorSize / 512),
698df73c41aSNathan Whitehorn 			    0x41 /* PReP Boot */);
699df73c41aSNathan Whitehorn 		}
700b3fa6d2cSNathan Whitehorn 	}
701b3fa6d2cSNathan Whitehorn 
702b3fa6d2cSNathan Whitehorn 	if (apm_partitions > 0) {
703b3fa6d2cSNathan Whitehorn 		/* Write DDR and global APM info */
704b3fa6d2cSNathan Whitehorn 		uint32_t apm32;
705b3fa6d2cSNathan Whitehorn 		uint16_t apm16;
706b3fa6d2cSNathan Whitehorn 		int total_parts;
707b3fa6d2cSNathan Whitehorn 
708b3fa6d2cSNathan Whitehorn 		fseek(fd, 0, SEEK_SET);
709df73c41aSNathan Whitehorn 		apm16 = htobe16(0x4552);
710b3fa6d2cSNathan Whitehorn 		fwrite(&apm16, sizeof(apm16), 1, fd);
711a3829099SNathan Whitehorn 		/* Device block size */
712df73c41aSNathan Whitehorn 		apm16 = htobe16(512);
713b3fa6d2cSNathan Whitehorn 		fwrite(&apm16, sizeof(apm16), 1, fd);
714a3829099SNathan Whitehorn 		/* Device block count */
7151631d42aSEd Maste 		apm32 = htobe32(diskStructure->totalSectors *
7161631d42aSEd Maste 		    (diskStructure->sectorSize / 512));
717b3fa6d2cSNathan Whitehorn 		fwrite(&apm32, sizeof(apm32), 1, fd);
718a3829099SNathan Whitehorn 		/* Device type/id */
719df73c41aSNathan Whitehorn 		apm16 = htobe16(1);
720a3829099SNathan Whitehorn 		fwrite(&apm16, sizeof(apm16), 1, fd);
721a3829099SNathan Whitehorn 		fwrite(&apm16, sizeof(apm16), 1, fd);
722b3fa6d2cSNathan Whitehorn 
723b3fa6d2cSNathan Whitehorn 		/* Count total needed entries */
724b3fa6d2cSNathan Whitehorn 		total_parts = 2 + apm_partitions; /* Self + ISO9660 */
725b3fa6d2cSNathan Whitehorn 
726b3fa6d2cSNathan Whitehorn 		/* Write self-descriptor */
727a3829099SNathan Whitehorn 		cd9660_write_apm_partition_entry(fd, 0, total_parts, 1,
728a3829099SNathan Whitehorn 		    total_parts, 512, "Apple", "Apple_partition_map");
729b3fa6d2cSNathan Whitehorn 
730b3fa6d2cSNathan Whitehorn 		/* Write all partition entries */
731b3fa6d2cSNathan Whitehorn 		apm_partitions = 0;
7321631d42aSEd Maste 		TAILQ_FOREACH(t, &diskStructure->boot_images, image_list) {
733b3fa6d2cSNathan Whitehorn 			if (t->system != ET_SYS_MAC)
734b3fa6d2cSNathan Whitehorn 				continue;
735b3fa6d2cSNathan Whitehorn 
736b3fa6d2cSNathan Whitehorn 			cd9660_write_apm_partition_entry(fd,
737bff5cf16SAndreas Tobler 			    1 + apm_partitions++, total_parts,
7381631d42aSEd Maste 			    t->sector * (diskStructure->sectorSize / 512),
7391631d42aSEd Maste 			    t->num_sectors * (diskStructure->sectorSize / 512),
740a3829099SNathan Whitehorn 			    512, "CD Boot", "Apple_Bootstrap");
741b3fa6d2cSNathan Whitehorn 		}
742756e49c3SBenno Rice 
743bff5cf16SAndreas Tobler 		/* Write ISO9660 descriptor, enclosing the whole disk */
744bff5cf16SAndreas Tobler 		cd9660_write_apm_partition_entry(fd, 2 + apm_partitions,
7451631d42aSEd Maste 		    total_parts, 0, diskStructure->totalSectors *
7461631d42aSEd Maste 		    (diskStructure->sectorSize / 512), 512, "ISO9660",
747bff5cf16SAndreas Tobler 		    "CD_ROM_Mode_1");
74801a0f853SOlivier Houchard 	}
74901a0f853SOlivier Houchard 
75001a0f853SOlivier Houchard 	return 0;
75101a0f853SOlivier Houchard }
752