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