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