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