Home | History | Annotate | Line # | Download | only in cd9660
cd9660_eltorito.c revision 1.8
      1  1.8  dyoung /*	$NetBSD: cd9660_eltorito.c,v 1.8 2005/10/30 07:33:57 dyoung 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.1    fvdl #include "cd9660.h"
     35  1.1    fvdl #include "cd9660_eltorito.h"
     36  1.1    fvdl 
     37  1.1    fvdl #include <sys/cdefs.h>
     38  1.1    fvdl #if defined(__RCSID) && !defined(__lint)
     39  1.8  dyoung __RCSID("$NetBSD: cd9660_eltorito.c,v 1.8 2005/10/30 07:33:57 dyoung Exp $");
     40  1.1    fvdl #endif  /* !__lint */
     41  1.1    fvdl 
     42  1.7  dyoung #ifdef DEBUG
     43  1.7  dyoung #define	ELTORITO_DPRINTF(__x)	printf __x
     44  1.7  dyoung #else
     45  1.7  dyoung #define	ELTORITO_DPRINTF(__x)
     46  1.7  dyoung #endif
     47  1.7  dyoung 
     48  1.1    fvdl static struct boot_catalog_entry *cd9660_init_boot_catalog_entry(void);
     49  1.1    fvdl static struct boot_catalog_entry *cd9660_boot_setup_validation_entry(char);
     50  1.1    fvdl static struct boot_catalog_entry *cd9660_boot_setup_default_entry(
     51  1.1    fvdl     struct cd9660_boot_image *);
     52  1.1    fvdl static struct boot_catalog_entry *cd9660_boot_setup_section_head(char);
     53  1.1    fvdl static struct boot_catalog_entry *cd9660_boot_setup_validation_entry(char);
     54  1.1    fvdl #if 0
     55  1.1    fvdl static u_char cd9660_boot_get_system_type(struct cd9660_boot_image *);
     56  1.1    fvdl #endif
     57  1.1    fvdl 
     58  1.1    fvdl int
     59  1.6  dyoung cd9660_add_boot_disk(const char *boot_info)
     60  1.1    fvdl {
     61  1.1    fvdl 	struct stat stbuf;
     62  1.1    fvdl 	char *temp;
     63  1.1    fvdl 	char *sysname;
     64  1.1    fvdl 	char *filename;
     65  1.8  dyoung 	struct cd9660_boot_image *new_image, *tmp_image;
     66  1.5  dyoung 
     67  1.1    fvdl 	assert(boot_info != NULL);
     68  1.1    fvdl 
     69  1.2  dyoung 	if (*boot_info == '\0') {
     70  1.2  dyoung 		warnx("Error: Boot disk information must be in the "
     71  1.2  dyoung 		      "format 'system;filename'");
     72  1.5  dyoung 		return 0;
     73  1.2  dyoung 	}
     74  1.2  dyoung 
     75  1.1    fvdl 	/* First decode the boot information */
     76  1.2  dyoung 	if ((temp = strdup(boot_info)) == NULL) {
     77  1.2  dyoung 		warn("%s: strdup", __func__);
     78  1.1    fvdl 		return 0;
     79  1.1    fvdl 	}
     80  1.1    fvdl 
     81  1.1    fvdl 	sysname = temp;
     82  1.1    fvdl 	filename = strchr(sysname, ';');
     83  1.1    fvdl 	if (filename == NULL) {
     84  1.2  dyoung 		warnx("supply boot disk information in the format "
     85  1.2  dyoung 		    "'system;filename'");
     86  1.5  dyoung 		return 0;
     87  1.1    fvdl 	}
     88  1.1    fvdl 
     89  1.2  dyoung 	*filename++ = '\0';
     90  1.2  dyoung 
     91  1.1    fvdl 	printf("Found bootdisk with system %s, and filename %s\n",
     92  1.1    fvdl 	    sysname, filename);
     93  1.7  dyoung 	if ((new_image = malloc(sizeof(*new_image))) == NULL) {
     94  1.2  dyoung 		warn("%s: malloc", __func__);
     95  1.1    fvdl 		return 0;
     96  1.1    fvdl 	}
     97  1.7  dyoung 	(void)memset(new_image, 0, sizeof(*new_image));
     98  1.1    fvdl 	new_image->loadSegment = 0;	/* default for now */
     99  1.1    fvdl 
    100  1.1    fvdl 	/* Decode System */
    101  1.2  dyoung 	if (strcmp(sysname, "i386") == 0)
    102  1.1    fvdl 		new_image->system = ET_SYS_X86;
    103  1.2  dyoung 	else if (strcmp(sysname, "powerpc") == 0)
    104  1.1    fvdl 		new_image->system = ET_SYS_PPC;
    105  1.4  dyoung 	else if (strcmp(sysname, "macppc") == 0 ||
    106  1.4  dyoung 	         strcmp(sysname, "mac68k") == 0)
    107  1.1    fvdl 		new_image->system = ET_SYS_MAC;
    108  1.1    fvdl 	else {
    109  1.2  dyoung 		warnx("boot disk system must be "
    110  1.2  dyoung 		      "i386, powerpc, macppc, or mac68k");
    111  1.1    fvdl 		return 0;
    112  1.1    fvdl 	}
    113  1.5  dyoung 
    114  1.5  dyoung 
    115  1.2  dyoung 	if ((new_image->filename = strdup(filename)) == NULL) {
    116  1.2  dyoung 		warn("%s: strdup", __func__);
    117  1.1    fvdl 		return 0;
    118  1.1    fvdl 	}
    119  1.1    fvdl 
    120  1.1    fvdl 	free(temp);
    121  1.5  dyoung 
    122  1.1    fvdl 	/* Get information about the file */
    123  1.1    fvdl 	if (lstat(new_image->filename, &stbuf) == -1)
    124  1.2  dyoung 		err(EXIT_FAILURE, "%s: lstat(\"%s\")", __func__,
    125  1.2  dyoung 		    new_image->filename);
    126  1.1    fvdl 
    127  1.1    fvdl 	switch (stbuf.st_size) {
    128  1.1    fvdl 	case 1440 * 1024:
    129  1.1    fvdl 		new_image->targetMode = ET_MEDIA_144FDD;
    130  1.1    fvdl 		printf("Assigned boot image to 1.44 emulation mode\n");
    131  1.1    fvdl 		break;
    132  1.1    fvdl 	case 1200 * 1024:
    133  1.1    fvdl 		new_image->targetMode = ET_MEDIA_12FDD;
    134  1.1    fvdl 		printf("Assigned boot image to 1.2 emulation mode\n");
    135  1.1    fvdl 		break;
    136  1.1    fvdl 	case 2880 * 1024:
    137  1.1    fvdl 		new_image->targetMode = ET_MEDIA_288FDD;
    138  1.1    fvdl 		printf("Assigned boot image to 2.88 emulation mode\n");
    139  1.1    fvdl 		break;
    140  1.1    fvdl 	default:
    141  1.1    fvdl 		new_image->targetMode = ET_MEDIA_NOEM;
    142  1.1    fvdl 		printf("Assigned boot image to no emulation mode\n");
    143  1.1    fvdl 		break;
    144  1.1    fvdl 	}
    145  1.5  dyoung 
    146  1.1    fvdl 	new_image->size = stbuf.st_size;
    147  1.1    fvdl 	new_image->num_sectors =
    148  1.7  dyoung 	    howmany(new_image->size, diskStructure.sectorSize) *
    149  1.7  dyoung 	    howmany(diskStructure.sectorSize, 512);
    150  1.7  dyoung 	printf("New image has size %i, uses %i 512-byte sectors\n",
    151  1.7  dyoung 	    new_image->size, new_image->num_sectors);
    152  1.1    fvdl 	new_image->sector = -1;
    153  1.1    fvdl 	/* Bootable by default */
    154  1.1    fvdl 	new_image->bootable = ET_BOOTABLE;
    155  1.1    fvdl 	/* Add boot disk */
    156  1.1    fvdl 
    157  1.8  dyoung 	/* Group images for the same platform together. */
    158  1.8  dyoung 	TAILQ_FOREACH(tmp_image, &diskStructure.boot_images, image_list) {
    159  1.8  dyoung 		if (tmp_image->system != new_image->system)
    160  1.8  dyoung 			break;
    161  1.8  dyoung 	}
    162  1.8  dyoung 
    163  1.8  dyoung 	if (tmp_image == NULL) {
    164  1.7  dyoung 		TAILQ_INSERT_HEAD(&diskStructure.boot_images, new_image,
    165  1.1    fvdl 		    image_list);
    166  1.8  dyoung 	} else
    167  1.8  dyoung 		TAILQ_INSERT_BEFORE(tmp_image, new_image, image_list);
    168  1.8  dyoung 
    169  1.8  dyoung 	new_image->serialno = diskStructure.image_serialno++;
    170  1.5  dyoung 
    171  1.1    fvdl 	/* TODO : Need to do anything about the boot image in the tree? */
    172  1.1    fvdl 	diskStructure.is_bootable = 1;
    173  1.5  dyoung 
    174  1.1    fvdl 	return 1;
    175  1.1    fvdl }
    176  1.1    fvdl 
    177  1.1    fvdl int
    178  1.6  dyoung cd9660_eltorito_add_boot_option(const char *option_string, const char *value)
    179  1.1    fvdl {
    180  1.7  dyoung 	char *eptr;
    181  1.1    fvdl 	struct cd9660_boot_image *image;
    182  1.5  dyoung 
    183  1.1    fvdl 	assert(option_string != NULL);
    184  1.5  dyoung 
    185  1.1    fvdl 	/* Find the last image added */
    186  1.8  dyoung 	TAILQ_FOREACH(image, &diskStructure.boot_images, image_list) {
    187  1.8  dyoung 		if (image->serialno + 1 == diskStructure.image_serialno)
    188  1.8  dyoung 			break;
    189  1.8  dyoung 	}
    190  1.1    fvdl 	if (image == NULL)
    191  1.2  dyoung 		errx(EXIT_FAILURE, "Attempted to add boot option, "
    192  1.2  dyoung 		    "but no boot images have been specified");
    193  1.1    fvdl 
    194  1.1    fvdl 	if (strcmp(option_string, "no-emul-boot") == 0) {
    195  1.5  dyoung 		image->targetMode = ET_MEDIA_NOEM;
    196  1.1    fvdl 	} else if (strcmp(option_string, "no-boot") == 0) {
    197  1.1    fvdl 		image->bootable = ET_NOT_BOOTABLE;
    198  1.1    fvdl 	} else if (strcmp(option_string, "hard-disk-boot") == 0) {
    199  1.1    fvdl 		image->targetMode = ET_MEDIA_HDD;
    200  1.1    fvdl 	} else if (strcmp(option_string, "boot-load-segment") == 0) {
    201  1.7  dyoung 		image->loadSegment = strtoul(value, &eptr, 16);
    202  1.7  dyoung 		if (eptr == value || *eptr != '\0' || errno != ERANGE) {
    203  1.7  dyoung 			warn("%s: strtoul", __func__);
    204  1.7  dyoung 			return 0;
    205  1.7  dyoung 		}
    206  1.1    fvdl 	} else {
    207  1.5  dyoung 		return 0;
    208  1.1    fvdl 	}
    209  1.1    fvdl 	return 1;
    210  1.1    fvdl }
    211  1.1    fvdl 
    212  1.1    fvdl static struct boot_catalog_entry *
    213  1.1    fvdl cd9660_init_boot_catalog_entry(void)
    214  1.1    fvdl {
    215  1.1    fvdl 	struct boot_catalog_entry *temp;
    216  1.1    fvdl 
    217  1.7  dyoung 	if ((temp = malloc(sizeof(*temp))) == NULL)
    218  1.7  dyoung 		return NULL;
    219  1.7  dyoung 
    220  1.7  dyoung 	return memset(temp, 0, sizeof(*temp));
    221  1.1    fvdl }
    222  1.1    fvdl 
    223  1.1    fvdl static struct boot_catalog_entry *
    224  1.1    fvdl cd9660_boot_setup_validation_entry(char sys)
    225  1.1    fvdl {
    226  1.6  dyoung 	struct boot_catalog_entry *entry;
    227  1.6  dyoung 	boot_catalog_validation_entry *ve;
    228  1.1    fvdl 	int16_t checksum;
    229  1.1    fvdl 	unsigned char *csptr;
    230  1.1    fvdl 	int i;
    231  1.6  dyoung 	entry = cd9660_init_boot_catalog_entry();
    232  1.5  dyoung 
    233  1.6  dyoung 	if (entry == NULL) {
    234  1.1    fvdl 		warnx("Error: memory allocation failed in "
    235  1.1    fvdl 		      "cd9660_boot_setup_validation_entry");
    236  1.1    fvdl 		return 0;
    237  1.1    fvdl 	}
    238  1.6  dyoung 	ve = &entry->entry_data.VE;
    239  1.6  dyoung 
    240  1.6  dyoung 	ve->header_id[0] = 1;
    241  1.6  dyoung 	ve->platform_id[0] = sys;
    242  1.6  dyoung 	ve->key[0] = 0x55;
    243  1.6  dyoung 	ve->key[1] = 0xAA;
    244  1.1    fvdl 
    245  1.1    fvdl 	/* Calculate checksum */
    246  1.1    fvdl 	checksum = 0;
    247  1.6  dyoung 	cd9660_721(0, ve->checksum);
    248  1.7  dyoung 	csptr = (unsigned char*)ve;
    249  1.6  dyoung 	for (i = 0; i < sizeof(*ve); i += 2) {
    250  1.6  dyoung 		checksum += (int16_t)csptr[i];
    251  1.6  dyoung 		checksum += 256 * (int16_t)csptr[i + 1];
    252  1.1    fvdl 	}
    253  1.1    fvdl 	checksum = -checksum;
    254  1.6  dyoung 	cd9660_721(checksum, ve->checksum);
    255  1.5  dyoung 
    256  1.7  dyoung         ELTORITO_DPRINTF(("%s: header_id %d, platform_id %d, key[0] %d, key[1] %d, "
    257  1.7  dyoung 	    "checksum %04x\n", __func__, ve->header_id[0], ve->platform_id[0],
    258  1.7  dyoung 	    ve->key[0], ve->key[1], checksum));
    259  1.6  dyoung 	return entry;
    260  1.1    fvdl }
    261  1.1    fvdl 
    262  1.1    fvdl static struct boot_catalog_entry *
    263  1.1    fvdl cd9660_boot_setup_default_entry(struct cd9660_boot_image *disk)
    264  1.1    fvdl {
    265  1.1    fvdl 	struct boot_catalog_entry *default_entry;
    266  1.6  dyoung 	boot_catalog_initial_entry *ie;
    267  1.1    fvdl 
    268  1.1    fvdl 	default_entry = cd9660_init_boot_catalog_entry();
    269  1.1    fvdl 	if (default_entry == NULL)
    270  1.1    fvdl 		return NULL;
    271  1.5  dyoung 
    272  1.6  dyoung 	ie = &default_entry->entry_data.IE;
    273  1.5  dyoung 
    274  1.6  dyoung 	ie->boot_indicator[0] = disk->bootable;
    275  1.6  dyoung 	ie->media_type[0] = disk->targetMode;
    276  1.6  dyoung 	cd9660_721(disk->loadSegment, ie->load_segment);
    277  1.6  dyoung 	ie->system_type[0] = disk->system;
    278  1.7  dyoung 	cd9660_721(disk->num_sectors, ie->sector_count);
    279  1.6  dyoung 	cd9660_731(disk->sector, ie->load_rba);
    280  1.5  dyoung 
    281  1.7  dyoung 	ELTORITO_DPRINTF(("%s: boot indicator %d, media type %d, "
    282  1.7  dyoung 	    "load segment %04x, system type %d, sector count %d, "
    283  1.7  dyoung 	    "load rba %d\n", __func__, ie->boot_indicator[0],
    284  1.7  dyoung 	    ie->media_type[0], disk->loadSegment, ie->system_type[0],
    285  1.7  dyoung 	    disk->num_sectors, disk->sector));
    286  1.1    fvdl 	return default_entry;
    287  1.1    fvdl }
    288  1.1    fvdl 
    289  1.1    fvdl static struct boot_catalog_entry *
    290  1.1    fvdl cd9660_boot_setup_section_head(char platform)
    291  1.1    fvdl {
    292  1.6  dyoung 	struct boot_catalog_entry *entry;
    293  1.6  dyoung 	boot_catalog_section_header *sh;
    294  1.1    fvdl 
    295  1.6  dyoung 	entry = cd9660_init_boot_catalog_entry();
    296  1.6  dyoung 	if (entry == NULL)
    297  1.1    fvdl 		return NULL;
    298  1.6  dyoung 
    299  1.6  dyoung 	sh = &entry->entry_data.SH;
    300  1.1    fvdl 	/* More by default. The last one will manually be set to 0x91 */
    301  1.6  dyoung 	sh->header_indicator[0] = ET_SECTION_HEADER_MORE;
    302  1.6  dyoung 	sh->platform_id[0] = platform;
    303  1.6  dyoung 	sh->num_section_entries[0] = 0;
    304  1.6  dyoung 	return entry;
    305  1.1    fvdl }
    306  1.1    fvdl 
    307  1.1    fvdl static struct boot_catalog_entry *
    308  1.1    fvdl cd9660_boot_setup_section_entry(struct cd9660_boot_image *disk)
    309  1.1    fvdl {
    310  1.1    fvdl 	struct boot_catalog_entry *entry;
    311  1.6  dyoung 	boot_catalog_section_entry *se;
    312  1.1    fvdl 	if ((entry = cd9660_init_boot_catalog_entry()) == NULL)
    313  1.1    fvdl 		return NULL;
    314  1.5  dyoung 
    315  1.6  dyoung 	se = &entry->entry_data.SE;
    316  1.6  dyoung 
    317  1.6  dyoung 	se->boot_indicator[0] = ET_BOOTABLE;
    318  1.6  dyoung 	se->media_type[0] = disk->targetMode;
    319  1.6  dyoung 	cd9660_721(disk->loadSegment, se->load_segment);
    320  1.7  dyoung 	cd9660_721(disk->num_sectors, se->sector_count);
    321  1.7  dyoung 	cd9660_731(disk->sector, se->load_rba);
    322  1.1    fvdl 	return entry;
    323  1.1    fvdl }
    324  1.1    fvdl 
    325  1.1    fvdl #if 0
    326  1.1    fvdl static u_char
    327  1.1    fvdl cd9660_boot_get_system_type(struct cd9660_boot_image *disk)
    328  1.1    fvdl {
    329  1.1    fvdl 	/*
    330  1.1    fvdl 		For hard drive booting, we need to examine the MBR to figure
    331  1.1    fvdl 		out what the partition type is
    332  1.1    fvdl 	*/
    333  1.1    fvdl 	return 0;
    334  1.1    fvdl }
    335  1.1    fvdl #endif
    336  1.1    fvdl 
    337  1.1    fvdl /*
    338  1.1    fvdl  * Set up the BVD, Boot catalog, and the boot entries, but do no writing
    339  1.1    fvdl  */
    340  1.1    fvdl int
    341  1.1    fvdl cd9660_setup_boot(int first_sector)
    342  1.1    fvdl {
    343  1.1    fvdl 	int sector;
    344  1.1    fvdl 	int used_sectors;
    345  1.1    fvdl 	int num_entries = 0;
    346  1.1    fvdl 	int catalog_sectors;
    347  1.1    fvdl 	struct boot_catalog_entry *x86_head, *mac_head, *ppc_head,
    348  1.8  dyoung 		*valid_entry, *default_entry, *temp, *head, **headp, *next;
    349  1.1    fvdl 	struct cd9660_boot_image *tmp_disk;
    350  1.1    fvdl 
    351  1.8  dyoung 	headp = NULL;
    352  1.8  dyoung 	x86_head = mac_head = ppc_head = NULL;
    353  1.5  dyoung 
    354  1.1    fvdl 	/* If there are no boot disks, don't bother building boot information */
    355  1.8  dyoung 	if (TAILQ_EMPTY(&diskStructure.boot_images))
    356  1.1    fvdl 		return 0;
    357  1.1    fvdl 
    358  1.1    fvdl 	/* Point to catalog: For now assume it consumes one sector */
    359  1.7  dyoung 	ELTORITO_DPRINTF(("Boot catalog will go in sector %i\n", first_sector));
    360  1.1    fvdl 	diskStructure.boot_catalog_sector = first_sector;
    361  1.1    fvdl 	cd9660_bothendian_dword(first_sector,
    362  1.1    fvdl 		diskStructure.boot_descriptor->boot_catalog_pointer);
    363  1.5  dyoung 
    364  1.1    fvdl 	/* Step 1: Generate boot catalog */
    365  1.1    fvdl 	/* Step 1a: Validation entry */
    366  1.1    fvdl 	valid_entry = cd9660_boot_setup_validation_entry(ET_SYS_X86);
    367  1.1    fvdl 	if (valid_entry == NULL)
    368  1.1    fvdl 		return -1;
    369  1.5  dyoung 
    370  1.1    fvdl 	/*
    371  1.1    fvdl 	 * Count how many boot images there are,
    372  1.1    fvdl 	 * and how many sectors they consume.
    373  1.1    fvdl 	 */
    374  1.1    fvdl 	num_entries = 1;
    375  1.1    fvdl 	used_sectors = 0;
    376  1.5  dyoung 
    377  1.7  dyoung 	TAILQ_FOREACH(tmp_disk, &diskStructure.boot_images, image_list) {
    378  1.1    fvdl 		used_sectors += tmp_disk->num_sectors;
    379  1.5  dyoung 
    380  1.1    fvdl 		/* One default entry per image */
    381  1.1    fvdl 		num_entries++;
    382  1.1    fvdl 	}
    383  1.1    fvdl 	catalog_sectors = howmany(num_entries * 0x20, diskStructure.sectorSize);
    384  1.1    fvdl 	used_sectors += catalog_sectors;
    385  1.5  dyoung 
    386  1.7  dyoung 	printf("%s: there will be %i entries consuming %i sectors. "
    387  1.7  dyoung 	       "Catalog is %i sectors\n", __func__, num_entries, used_sectors,
    388  1.1    fvdl 		catalog_sectors);
    389  1.5  dyoung 
    390  1.1    fvdl 	/* Populate sector numbers */
    391  1.1    fvdl 	sector = first_sector + catalog_sectors;
    392  1.7  dyoung 	TAILQ_FOREACH(tmp_disk, &diskStructure.boot_images, image_list) {
    393  1.1    fvdl 		tmp_disk->sector = sector;
    394  1.1    fvdl 		sector += tmp_disk->num_sectors;
    395  1.1    fvdl 	}
    396  1.5  dyoung 
    397  1.1    fvdl 	LIST_INSERT_HEAD(&diskStructure.boot_entries, valid_entry, ll_struct);
    398  1.5  dyoung 
    399  1.1    fvdl 	/* Step 1b: Initial/default entry */
    400  1.1    fvdl 	/* TODO : PARAM */
    401  1.7  dyoung 	tmp_disk = TAILQ_FIRST(&diskStructure.boot_images);
    402  1.1    fvdl 	default_entry = cd9660_boot_setup_default_entry(tmp_disk);
    403  1.1    fvdl 	if (default_entry == NULL) {
    404  1.1    fvdl 		warnx("Error: memory allocation failed in cd9660_setup_boot");
    405  1.1    fvdl 		return -1;
    406  1.1    fvdl 	}
    407  1.5  dyoung 
    408  1.1    fvdl 	LIST_INSERT_AFTER(valid_entry, default_entry, ll_struct);
    409  1.5  dyoung 
    410  1.1    fvdl 	/* Todo: multiple default entries? */
    411  1.5  dyoung 
    412  1.7  dyoung 	tmp_disk = TAILQ_NEXT(tmp_disk, image_list);
    413  1.5  dyoung 
    414  1.1    fvdl 	temp = default_entry;
    415  1.5  dyoung 
    416  1.1    fvdl 	/* If multiple boot images are given : */
    417  1.1    fvdl 	while (tmp_disk != NULL) {
    418  1.1    fvdl 		/* Step 2: Section header */
    419  1.1    fvdl 		switch (tmp_disk->system) {
    420  1.1    fvdl 		case ET_SYS_X86:
    421  1.8  dyoung 			headp = &x86_head;
    422  1.1    fvdl 			break;
    423  1.1    fvdl 		case ET_SYS_PPC:
    424  1.8  dyoung 			headp = &ppc_head;
    425  1.1    fvdl 			break;
    426  1.1    fvdl 		case ET_SYS_MAC:
    427  1.8  dyoung 			headp = &mac_head;
    428  1.1    fvdl 			break;
    429  1.8  dyoung 		default:
    430  1.8  dyoung 			warnx("%s: internal error: unknown system type",
    431  1.8  dyoung 			    __func__);
    432  1.8  dyoung 			return -1;
    433  1.1    fvdl 		}
    434  1.1    fvdl 
    435  1.8  dyoung 		if (*headp == NULL) {
    436  1.8  dyoung 			head =
    437  1.5  dyoung 			    cd9660_boot_setup_section_head(tmp_disk->system);
    438  1.8  dyoung 			if (head == NULL) {
    439  1.1    fvdl 				warnx("Error: memory allocation failed in "
    440  1.1    fvdl 				      "cd9660_setup_boot");
    441  1.1    fvdl 				return -1;
    442  1.1    fvdl 			}
    443  1.8  dyoung 			LIST_INSERT_AFTER(default_entry, head, ll_struct);
    444  1.8  dyoung 			*headp = head;
    445  1.8  dyoung 		} else
    446  1.8  dyoung 			head = *headp;
    447  1.8  dyoung 
    448  1.8  dyoung 		head->entry_data.SH.num_section_entries[0]++;
    449  1.5  dyoung 
    450  1.1    fvdl 		/* Step 2a: Section entry and extensions */
    451  1.1    fvdl 		temp = cd9660_boot_setup_section_entry(tmp_disk);
    452  1.1    fvdl 		if (temp == NULL) {
    453  1.8  dyoung 			warn("%s: cd9660_boot_setup_section_entry", __func__);
    454  1.1    fvdl 			return -1;
    455  1.1    fvdl 		}
    456  1.1    fvdl 
    457  1.8  dyoung 		while ((next = LIST_NEXT(head, ll_struct)) != NULL &&
    458  1.3  dyoung 		       next->entry_type == ET_ENTRY_SE)
    459  1.8  dyoung 			head = next;
    460  1.5  dyoung 
    461  1.8  dyoung 		LIST_INSERT_AFTER(head, temp, ll_struct);
    462  1.7  dyoung 		tmp_disk = TAILQ_NEXT(tmp_disk, image_list);
    463  1.1    fvdl 	}
    464  1.5  dyoung 
    465  1.1    fvdl 	/* TODO: Remaining boot disks when implemented */
    466  1.5  dyoung 
    467  1.1    fvdl 	return first_sector + used_sectors;
    468  1.1    fvdl }
    469  1.1    fvdl 
    470  1.1    fvdl int
    471  1.1    fvdl cd9660_setup_boot_volume_descritpor(volume_descriptor *bvd)
    472  1.1    fvdl {
    473  1.1    fvdl 	boot_volume_descriptor *bvdData =
    474  1.1    fvdl 	    (boot_volume_descriptor*)bvd->volumeDescriptorData;
    475  1.1    fvdl 
    476  1.1    fvdl 	bvdData->boot_record_indicator[0] = ISO_VOLUME_DESCRIPTOR_BOOT;
    477  1.1    fvdl 	memcpy(bvdData->identifier, ISO_VOLUME_DESCRIPTOR_STANDARD_ID, 5);
    478  1.1    fvdl 	bvdData->version[0] = 1;
    479  1.1    fvdl 	memcpy(bvdData->boot_system_identifier, ET_ID, 23);
    480  1.1    fvdl 	memcpy(bvdData->identifier, ISO_VOLUME_DESCRIPTOR_STANDARD_ID, 5);
    481  1.1    fvdl 	diskStructure.boot_descriptor =
    482  1.1    fvdl 	    (boot_volume_descriptor*) bvd->volumeDescriptorData;
    483  1.1    fvdl 	return 1;
    484  1.1    fvdl }
    485  1.1    fvdl 
    486  1.5  dyoung int
    487  1.1    fvdl cd9660_write_boot(FILE *fd)
    488  1.1    fvdl {
    489  1.1    fvdl 	struct boot_catalog_entry *e;
    490  1.1    fvdl 	struct cd9660_boot_image *t;
    491  1.1    fvdl 
    492  1.1    fvdl 	/* write boot catalog */
    493  1.1    fvdl 	fseek(fd, diskStructure.boot_catalog_sector * diskStructure.sectorSize,
    494  1.1    fvdl 	    SEEK_SET);
    495  1.5  dyoung 
    496  1.1    fvdl 	printf("Writing boot catalog to sector %i\n",
    497  1.1    fvdl 	    diskStructure.boot_catalog_sector);
    498  1.3  dyoung 	LIST_FOREACH(e, &diskStructure.boot_entries, ll_struct) {
    499  1.1    fvdl 		printf("Writing catalog entry of type %i\n", e->entry_type);
    500  1.1    fvdl 		/*
    501  1.1    fvdl 		 * It doesnt matter which one gets written
    502  1.1    fvdl 		 * since they are the same size
    503  1.1    fvdl 		 */
    504  1.1    fvdl 		fwrite(&(e->entry_data.VE), 1, 32, fd);
    505  1.1    fvdl 	}
    506  1.1    fvdl 	printf("Finished writing boot catalog\n");
    507  1.5  dyoung 
    508  1.1    fvdl 	/* copy boot images */
    509  1.7  dyoung 	TAILQ_FOREACH(t, &diskStructure.boot_images, image_list) {
    510  1.1    fvdl 		printf("Writing boot image from %s to sectors %i\n",
    511  1.1    fvdl 		    t->filename,t->sector);
    512  1.1    fvdl 		cd9660_copy_file(fd, t->sector, t->filename);
    513  1.1    fvdl 	}
    514  1.1    fvdl 
    515  1.1    fvdl 	return 0;
    516  1.1    fvdl }
    517