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