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