Home | History | Annotate | Line # | Download | only in cd9660
cd9660_eltorito.c revision 1.27
      1  1.27   tsutsui /*	$NetBSD: cd9660_eltorito.c,v 1.27 2023/12/28 12:13:56 tsutsui 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.27   tsutsui __RCSID("$NetBSD: cd9660_eltorito.c,v 1.27 2023/12/28 12:13:56 tsutsui 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.21    nonaka static struct cd9660_boot_image *default_boot_image;
     64  1.21    nonaka 
     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.24  jmcneill 	else if (strcmp(sysname, "efi") == 0)
    113  1.24  jmcneill 		new_image->system = ET_SYS_EFI;
    114   1.1      fvdl 	else {
    115   1.2    dyoung 		warnx("boot disk system must be "
    116  1.24  jmcneill 		      "i386, powerpc, macppc, mac68k, or efi");
    117  1.11  christos 		free(temp);
    118  1.11  christos 		free(new_image);
    119   1.1      fvdl 		return 0;
    120   1.1      fvdl 	}
    121   1.5    dyoung 
    122   1.5    dyoung 
    123  1.20  christos 	new_image->filename = estrdup(filename);
    124   1.1      fvdl 
    125   1.1      fvdl 	free(temp);
    126   1.5    dyoung 
    127   1.1      fvdl 	/* Get information about the file */
    128   1.1      fvdl 	if (lstat(new_image->filename, &stbuf) == -1)
    129   1.2    dyoung 		err(EXIT_FAILURE, "%s: lstat(\"%s\")", __func__,
    130   1.2    dyoung 		    new_image->filename);
    131   1.1      fvdl 
    132   1.1      fvdl 	switch (stbuf.st_size) {
    133   1.1      fvdl 	case 1440 * 1024:
    134   1.1      fvdl 		new_image->targetMode = ET_MEDIA_144FDD;
    135   1.9    dyoung 		mode_msg = "Assigned boot image to 1.44 emulation mode";
    136   1.1      fvdl 		break;
    137   1.1      fvdl 	case 1200 * 1024:
    138   1.1      fvdl 		new_image->targetMode = ET_MEDIA_12FDD;
    139   1.9    dyoung 		mode_msg = "Assigned boot image to 1.2 emulation mode";
    140   1.1      fvdl 		break;
    141   1.1      fvdl 	case 2880 * 1024:
    142   1.1      fvdl 		new_image->targetMode = ET_MEDIA_288FDD;
    143   1.9    dyoung 		mode_msg = "Assigned boot image to 2.88 emulation mode";
    144   1.1      fvdl 		break;
    145   1.1      fvdl 	default:
    146   1.1      fvdl 		new_image->targetMode = ET_MEDIA_NOEM;
    147   1.9    dyoung 		mode_msg = "Assigned boot image to no emulation mode";
    148   1.1      fvdl 		break;
    149   1.1      fvdl 	}
    150   1.5    dyoung 
    151  1.20  christos 	if (diskStructure->verbose_level > 0)
    152   1.9    dyoung 		printf("%s\n", mode_msg);
    153   1.9    dyoung 
    154   1.1      fvdl 	new_image->size = stbuf.st_size;
    155   1.1      fvdl 	new_image->num_sectors =
    156  1.20  christos 	    howmany(new_image->size, diskStructure->sectorSize) *
    157  1.20  christos 	    howmany(diskStructure->sectorSize, 512);
    158  1.20  christos 	if (diskStructure->verbose_level > 0) {
    159   1.9    dyoung 		printf("New image has size %d, uses %d 512-byte sectors\n",
    160   1.9    dyoung 		    new_image->size, new_image->num_sectors);
    161   1.9    dyoung 	}
    162   1.1      fvdl 	new_image->sector = -1;
    163   1.1      fvdl 	/* Bootable by default */
    164   1.1      fvdl 	new_image->bootable = ET_BOOTABLE;
    165   1.1      fvdl 	/* Add boot disk */
    166   1.1      fvdl 
    167   1.8    dyoung 	/* Group images for the same platform together. */
    168  1.20  christos 	TAILQ_FOREACH(tmp_image, &diskStructure->boot_images, image_list) {
    169   1.8    dyoung 		if (tmp_image->system != new_image->system)
    170   1.8    dyoung 			break;
    171   1.8    dyoung 	}
    172   1.8    dyoung 
    173   1.8    dyoung 	if (tmp_image == NULL) {
    174  1.20  christos 		TAILQ_INSERT_HEAD(&diskStructure->boot_images, new_image,
    175   1.1      fvdl 		    image_list);
    176   1.8    dyoung 	} else
    177   1.8    dyoung 		TAILQ_INSERT_BEFORE(tmp_image, new_image, image_list);
    178   1.8    dyoung 
    179  1.20  christos 	new_image->serialno = diskStructure->image_serialno++;
    180   1.5    dyoung 
    181  1.21    nonaka 	new_image->platform_id = new_image->system;
    182  1.21    nonaka 
    183   1.1      fvdl 	/* TODO : Need to do anything about the boot image in the tree? */
    184  1.20  christos 	diskStructure->is_bootable = 1;
    185   1.5    dyoung 
    186  1.21    nonaka 	/* First boot image is initial/default entry. */
    187  1.21    nonaka 	if (default_boot_image == NULL)
    188  1.21    nonaka 		default_boot_image = new_image;
    189  1.21    nonaka 
    190   1.1      fvdl 	return 1;
    191   1.1      fvdl }
    192   1.1      fvdl 
    193   1.1      fvdl int
    194  1.20  christos cd9660_eltorito_add_boot_option(iso9660_disk *diskStructure,
    195  1.20  christos     const char *option_string, const char *value)
    196   1.1      fvdl {
    197   1.7    dyoung 	char *eptr;
    198   1.1      fvdl 	struct cd9660_boot_image *image;
    199   1.5    dyoung 
    200   1.1      fvdl 	assert(option_string != NULL);
    201   1.5    dyoung 
    202   1.1      fvdl 	/* Find the last image added */
    203  1.20  christos 	TAILQ_FOREACH(image, &diskStructure->boot_images, image_list) {
    204  1.20  christos 		if (image->serialno + 1 == diskStructure->image_serialno)
    205   1.8    dyoung 			break;
    206   1.8    dyoung 	}
    207   1.1      fvdl 	if (image == NULL)
    208   1.2    dyoung 		errx(EXIT_FAILURE, "Attempted to add boot option, "
    209   1.2    dyoung 		    "but no boot images have been specified");
    210   1.1      fvdl 
    211   1.1      fvdl 	if (strcmp(option_string, "no-emul-boot") == 0) {
    212   1.5    dyoung 		image->targetMode = ET_MEDIA_NOEM;
    213   1.1      fvdl 	} else if (strcmp(option_string, "no-boot") == 0) {
    214   1.1      fvdl 		image->bootable = ET_NOT_BOOTABLE;
    215   1.1      fvdl 	} else if (strcmp(option_string, "hard-disk-boot") == 0) {
    216   1.1      fvdl 		image->targetMode = ET_MEDIA_HDD;
    217   1.1      fvdl 	} else if (strcmp(option_string, "boot-load-segment") == 0) {
    218   1.7    dyoung 		image->loadSegment = strtoul(value, &eptr, 16);
    219   1.7    dyoung 		if (eptr == value || *eptr != '\0' || errno != ERANGE) {
    220   1.7    dyoung 			warn("%s: strtoul", __func__);
    221   1.7    dyoung 			return 0;
    222   1.7    dyoung 		}
    223  1.21    nonaka 	} else if (strcmp(option_string, "platformid") == 0) {
    224  1.21    nonaka 		if (strcmp(value, "efi") == 0)
    225  1.21    nonaka 			image->platform_id = ET_SYS_EFI;
    226  1.21    nonaka 		else {
    227  1.21    nonaka 			warn("%s: unknown platform: %s", __func__, value);
    228  1.21    nonaka 			return 0;
    229  1.21    nonaka 		}
    230   1.1      fvdl 	} else {
    231   1.5    dyoung 		return 0;
    232   1.1      fvdl 	}
    233   1.1      fvdl 	return 1;
    234   1.1      fvdl }
    235   1.1      fvdl 
    236   1.1      fvdl static struct boot_catalog_entry *
    237   1.1      fvdl cd9660_init_boot_catalog_entry(void)
    238   1.1      fvdl {
    239  1.20  christos 	return ecalloc(1, sizeof(struct boot_catalog_entry));
    240   1.1      fvdl }
    241   1.1      fvdl 
    242   1.1      fvdl static struct boot_catalog_entry *
    243   1.1      fvdl cd9660_boot_setup_validation_entry(char sys)
    244   1.1      fvdl {
    245   1.6    dyoung 	struct boot_catalog_entry *entry;
    246   1.6    dyoung 	boot_catalog_validation_entry *ve;
    247   1.1      fvdl 	int16_t checksum;
    248   1.1      fvdl 	unsigned char *csptr;
    249  1.19  christos 	size_t i;
    250   1.6    dyoung 	entry = cd9660_init_boot_catalog_entry();
    251   1.5    dyoung 
    252  1.22    nonaka 	entry->entry_type = ET_ENTRY_VE;
    253   1.6    dyoung 	ve = &entry->entry_data.VE;
    254   1.6    dyoung 
    255   1.6    dyoung 	ve->header_id[0] = 1;
    256   1.6    dyoung 	ve->platform_id[0] = sys;
    257   1.6    dyoung 	ve->key[0] = 0x55;
    258   1.6    dyoung 	ve->key[1] = 0xAA;
    259   1.1      fvdl 
    260   1.1      fvdl 	/* Calculate checksum */
    261   1.1      fvdl 	checksum = 0;
    262   1.6    dyoung 	cd9660_721(0, ve->checksum);
    263   1.7    dyoung 	csptr = (unsigned char*)ve;
    264   1.6    dyoung 	for (i = 0; i < sizeof(*ve); i += 2) {
    265   1.6    dyoung 		checksum += (int16_t)csptr[i];
    266   1.6    dyoung 		checksum += 256 * (int16_t)csptr[i + 1];
    267   1.1      fvdl 	}
    268   1.1      fvdl 	checksum = -checksum;
    269   1.6    dyoung 	cd9660_721(checksum, ve->checksum);
    270   1.5    dyoung 
    271   1.7    dyoung         ELTORITO_DPRINTF(("%s: header_id %d, platform_id %d, key[0] %d, key[1] %d, "
    272   1.7    dyoung 	    "checksum %04x\n", __func__, ve->header_id[0], ve->platform_id[0],
    273   1.7    dyoung 	    ve->key[0], ve->key[1], checksum));
    274   1.6    dyoung 	return entry;
    275   1.1      fvdl }
    276   1.1      fvdl 
    277   1.1      fvdl static struct boot_catalog_entry *
    278   1.1      fvdl cd9660_boot_setup_default_entry(struct cd9660_boot_image *disk)
    279   1.1      fvdl {
    280   1.1      fvdl 	struct boot_catalog_entry *default_entry;
    281   1.6    dyoung 	boot_catalog_initial_entry *ie;
    282   1.1      fvdl 
    283   1.1      fvdl 	default_entry = cd9660_init_boot_catalog_entry();
    284   1.1      fvdl 	if (default_entry == NULL)
    285   1.1      fvdl 		return NULL;
    286   1.5    dyoung 
    287  1.22    nonaka 	default_entry->entry_type = ET_ENTRY_IE;
    288   1.6    dyoung 	ie = &default_entry->entry_data.IE;
    289   1.5    dyoung 
    290   1.6    dyoung 	ie->boot_indicator[0] = disk->bootable;
    291   1.6    dyoung 	ie->media_type[0] = disk->targetMode;
    292   1.6    dyoung 	cd9660_721(disk->loadSegment, ie->load_segment);
    293   1.6    dyoung 	ie->system_type[0] = disk->system;
    294   1.7    dyoung 	cd9660_721(disk->num_sectors, ie->sector_count);
    295   1.6    dyoung 	cd9660_731(disk->sector, ie->load_rba);
    296   1.5    dyoung 
    297   1.7    dyoung 	ELTORITO_DPRINTF(("%s: boot indicator %d, media type %d, "
    298   1.7    dyoung 	    "load segment %04x, system type %d, sector count %d, "
    299   1.7    dyoung 	    "load rba %d\n", __func__, ie->boot_indicator[0],
    300   1.7    dyoung 	    ie->media_type[0], disk->loadSegment, ie->system_type[0],
    301   1.7    dyoung 	    disk->num_sectors, disk->sector));
    302   1.1      fvdl 	return default_entry;
    303   1.1      fvdl }
    304   1.1      fvdl 
    305   1.1      fvdl static struct boot_catalog_entry *
    306   1.1      fvdl cd9660_boot_setup_section_head(char platform)
    307   1.1      fvdl {
    308   1.6    dyoung 	struct boot_catalog_entry *entry;
    309   1.6    dyoung 	boot_catalog_section_header *sh;
    310   1.1      fvdl 
    311   1.6    dyoung 	entry = cd9660_init_boot_catalog_entry();
    312   1.6    dyoung 	if (entry == NULL)
    313   1.1      fvdl 		return NULL;
    314   1.6    dyoung 
    315  1.22    nonaka 	entry->entry_type = ET_ENTRY_SH;
    316   1.6    dyoung 	sh = &entry->entry_data.SH;
    317   1.1      fvdl 	/* More by default. The last one will manually be set to 0x91 */
    318   1.6    dyoung 	sh->header_indicator[0] = ET_SECTION_HEADER_MORE;
    319   1.6    dyoung 	sh->platform_id[0] = platform;
    320   1.6    dyoung 	sh->num_section_entries[0] = 0;
    321   1.6    dyoung 	return entry;
    322   1.1      fvdl }
    323   1.1      fvdl 
    324   1.1      fvdl static struct boot_catalog_entry *
    325   1.1      fvdl cd9660_boot_setup_section_entry(struct cd9660_boot_image *disk)
    326   1.1      fvdl {
    327   1.1      fvdl 	struct boot_catalog_entry *entry;
    328   1.6    dyoung 	boot_catalog_section_entry *se;
    329   1.1      fvdl 	if ((entry = cd9660_init_boot_catalog_entry()) == NULL)
    330   1.1      fvdl 		return NULL;
    331   1.5    dyoung 
    332  1.22    nonaka 	entry->entry_type = ET_ENTRY_SE;
    333   1.6    dyoung 	se = &entry->entry_data.SE;
    334   1.6    dyoung 
    335   1.6    dyoung 	se->boot_indicator[0] = ET_BOOTABLE;
    336   1.6    dyoung 	se->media_type[0] = disk->targetMode;
    337   1.6    dyoung 	cd9660_721(disk->loadSegment, se->load_segment);
    338   1.7    dyoung 	cd9660_721(disk->num_sectors, se->sector_count);
    339   1.7    dyoung 	cd9660_731(disk->sector, se->load_rba);
    340   1.1      fvdl 	return entry;
    341   1.1      fvdl }
    342   1.1      fvdl 
    343   1.1      fvdl #if 0
    344   1.1      fvdl static u_char
    345   1.1      fvdl cd9660_boot_get_system_type(struct cd9660_boot_image *disk)
    346   1.1      fvdl {
    347   1.1      fvdl 	/*
    348   1.1      fvdl 		For hard drive booting, we need to examine the MBR to figure
    349   1.1      fvdl 		out what the partition type is
    350   1.1      fvdl 	*/
    351   1.1      fvdl 	return 0;
    352   1.1      fvdl }
    353   1.1      fvdl #endif
    354   1.1      fvdl 
    355   1.1      fvdl /*
    356   1.1      fvdl  * Set up the BVD, Boot catalog, and the boot entries, but do no writing
    357   1.1      fvdl  */
    358   1.1      fvdl int
    359  1.20  christos cd9660_setup_boot(iso9660_disk *diskStructure, int first_sector)
    360   1.1      fvdl {
    361   1.1      fvdl 	int sector;
    362   1.1      fvdl 	int used_sectors;
    363   1.1      fvdl 	int num_entries = 0;
    364   1.1      fvdl 	int catalog_sectors;
    365  1.21    nonaka 	struct boot_catalog_entry *x86_head, *mac_head, *ppc_head, *efi_head,
    366   1.8    dyoung 		*valid_entry, *default_entry, *temp, *head, **headp, *next;
    367   1.1      fvdl 	struct cd9660_boot_image *tmp_disk;
    368  1.24  jmcneill 	u_char system;
    369   1.1      fvdl 
    370   1.8    dyoung 	headp = NULL;
    371  1.21    nonaka 	x86_head = mac_head = ppc_head = efi_head = NULL;
    372   1.5    dyoung 
    373   1.1      fvdl 	/* If there are no boot disks, don't bother building boot information */
    374  1.20  christos 	if (TAILQ_EMPTY(&diskStructure->boot_images))
    375   1.1      fvdl 		return 0;
    376   1.1      fvdl 
    377   1.1      fvdl 	/* Point to catalog: For now assume it consumes one sector */
    378   1.9    dyoung 	ELTORITO_DPRINTF(("Boot catalog will go in sector %d\n", first_sector));
    379  1.20  christos 	diskStructure->boot_catalog_sector = first_sector;
    380  1.26  christos 	cd9660_731(first_sector,
    381  1.26  christos 	    diskStructure->boot_descriptor->boot_catalog_pointer);
    382   1.5    dyoung 
    383  1.24  jmcneill 	/*
    384  1.24  jmcneill 	 * Use system type of default image for validation entry. Fallback to
    385  1.24  jmcneill 	 * X86 system type if not found.
    386  1.24  jmcneill 	 */
    387  1.24  jmcneill 	system = default_boot_image != NULL ? default_boot_image->system :
    388  1.25  riastrad 					      ET_SYS_X86;
    389  1.24  jmcneill 
    390   1.1      fvdl 	/* Step 1: Generate boot catalog */
    391   1.1      fvdl 	/* Step 1a: Validation entry */
    392  1.24  jmcneill 	valid_entry = cd9660_boot_setup_validation_entry(system);
    393   1.1      fvdl 	if (valid_entry == NULL)
    394   1.1      fvdl 		return -1;
    395   1.5    dyoung 
    396   1.1      fvdl 	/*
    397   1.1      fvdl 	 * Count how many boot images there are,
    398   1.1      fvdl 	 * and how many sectors they consume.
    399   1.1      fvdl 	 */
    400   1.1      fvdl 	num_entries = 1;
    401   1.1      fvdl 	used_sectors = 0;
    402   1.5    dyoung 
    403  1.20  christos 	TAILQ_FOREACH(tmp_disk, &diskStructure->boot_images, image_list) {
    404   1.1      fvdl 		used_sectors += tmp_disk->num_sectors;
    405   1.5    dyoung 
    406   1.1      fvdl 		/* One default entry per image */
    407   1.1      fvdl 		num_entries++;
    408   1.1      fvdl 	}
    409  1.20  christos 	catalog_sectors = howmany(num_entries * 0x20, diskStructure->sectorSize);
    410   1.1      fvdl 	used_sectors += catalog_sectors;
    411   1.5    dyoung 
    412  1.20  christos 	if (diskStructure->verbose_level > 0) {
    413   1.9    dyoung 		printf("%s: there will be %i entries consuming %i sectors. "
    414   1.9    dyoung 		       "Catalog is %i sectors\n", __func__, num_entries,
    415   1.9    dyoung 		       used_sectors, catalog_sectors);
    416   1.9    dyoung 	}
    417   1.5    dyoung 
    418   1.1      fvdl 	/* Populate sector numbers */
    419   1.1      fvdl 	sector = first_sector + catalog_sectors;
    420  1.20  christos 	TAILQ_FOREACH(tmp_disk, &diskStructure->boot_images, image_list) {
    421   1.1      fvdl 		tmp_disk->sector = sector;
    422  1.21    nonaka 		sector += tmp_disk->num_sectors /
    423  1.21    nonaka 		    (diskStructure->sectorSize / 512);
    424   1.1      fvdl 	}
    425   1.5    dyoung 
    426  1.20  christos 	LIST_INSERT_HEAD(&diskStructure->boot_entries, valid_entry, ll_struct);
    427   1.5    dyoung 
    428   1.1      fvdl 	/* Step 1b: Initial/default entry */
    429   1.1      fvdl 	/* TODO : PARAM */
    430  1.21    nonaka 	if (default_boot_image != NULL) {
    431  1.21    nonaka 		struct cd9660_boot_image *tcbi;
    432  1.21    nonaka 		TAILQ_FOREACH(tcbi, &diskStructure->boot_images, image_list) {
    433  1.21    nonaka 			if (tcbi == default_boot_image) {
    434  1.21    nonaka 				tmp_disk = tcbi;
    435  1.21    nonaka 				break;
    436  1.21    nonaka 			}
    437  1.21    nonaka 		}
    438  1.21    nonaka 	}
    439  1.21    nonaka 	if (tmp_disk == NULL)
    440  1.21    nonaka 		tmp_disk = TAILQ_FIRST(&diskStructure->boot_images);
    441   1.1      fvdl 	default_entry = cd9660_boot_setup_default_entry(tmp_disk);
    442   1.1      fvdl 	if (default_entry == NULL) {
    443   1.1      fvdl 		warnx("Error: memory allocation failed in cd9660_setup_boot");
    444   1.1      fvdl 		return -1;
    445   1.1      fvdl 	}
    446   1.5    dyoung 
    447   1.1      fvdl 	LIST_INSERT_AFTER(valid_entry, default_entry, ll_struct);
    448   1.5    dyoung 
    449   1.1      fvdl 	/* Todo: multiple default entries? */
    450   1.5    dyoung 
    451  1.21    nonaka 	tmp_disk = TAILQ_FIRST(&diskStructure->boot_images);
    452   1.5    dyoung 
    453  1.21    nonaka 	head = NULL;
    454   1.1      fvdl 	temp = default_entry;
    455   1.5    dyoung 
    456   1.1      fvdl 	/* If multiple boot images are given : */
    457  1.21    nonaka 	for (; tmp_disk != NULL; tmp_disk = TAILQ_NEXT(tmp_disk, image_list)) {
    458  1.21    nonaka 		if (tmp_disk == default_boot_image)
    459  1.21    nonaka 			continue;
    460  1.21    nonaka 
    461   1.1      fvdl 		/* Step 2: Section header */
    462  1.21    nonaka 		switch (tmp_disk->platform_id) {
    463   1.1      fvdl 		case ET_SYS_X86:
    464   1.8    dyoung 			headp = &x86_head;
    465   1.1      fvdl 			break;
    466   1.1      fvdl 		case ET_SYS_PPC:
    467   1.8    dyoung 			headp = &ppc_head;
    468   1.1      fvdl 			break;
    469   1.1      fvdl 		case ET_SYS_MAC:
    470   1.8    dyoung 			headp = &mac_head;
    471   1.1      fvdl 			break;
    472  1.21    nonaka 		case ET_SYS_EFI:
    473  1.21    nonaka 			headp = &efi_head;
    474  1.21    nonaka 			break;
    475   1.8    dyoung 		default:
    476   1.8    dyoung 			warnx("%s: internal error: unknown system type",
    477   1.8    dyoung 			    __func__);
    478   1.8    dyoung 			return -1;
    479   1.1      fvdl 		}
    480   1.1      fvdl 
    481   1.8    dyoung 		if (*headp == NULL) {
    482   1.8    dyoung 			head =
    483  1.21    nonaka 			  cd9660_boot_setup_section_head(tmp_disk->platform_id);
    484   1.8    dyoung 			if (head == NULL) {
    485   1.1      fvdl 				warnx("Error: memory allocation failed in "
    486   1.1      fvdl 				      "cd9660_setup_boot");
    487   1.1      fvdl 				return -1;
    488   1.1      fvdl 			}
    489   1.8    dyoung 			LIST_INSERT_AFTER(default_entry, head, ll_struct);
    490   1.8    dyoung 			*headp = head;
    491   1.8    dyoung 		} else
    492   1.8    dyoung 			head = *headp;
    493   1.8    dyoung 
    494   1.8    dyoung 		head->entry_data.SH.num_section_entries[0]++;
    495   1.5    dyoung 
    496   1.1      fvdl 		/* Step 2a: Section entry and extensions */
    497   1.1      fvdl 		temp = cd9660_boot_setup_section_entry(tmp_disk);
    498   1.1      fvdl 		if (temp == NULL) {
    499   1.8    dyoung 			warn("%s: cd9660_boot_setup_section_entry", __func__);
    500   1.1      fvdl 			return -1;
    501   1.1      fvdl 		}
    502   1.1      fvdl 
    503   1.8    dyoung 		while ((next = LIST_NEXT(head, ll_struct)) != NULL &&
    504   1.3    dyoung 		       next->entry_type == ET_ENTRY_SE)
    505   1.8    dyoung 			head = next;
    506   1.5    dyoung 
    507   1.8    dyoung 		LIST_INSERT_AFTER(head, temp, ll_struct);
    508   1.1      fvdl 	}
    509   1.5    dyoung 
    510  1.23    nonaka 	/* Find the last Section Header entry and mark it as the last. */
    511  1.23    nonaka 	head = NULL;
    512  1.23    nonaka 	LIST_FOREACH(next, &diskStructure->boot_entries, ll_struct) {
    513  1.23    nonaka 		if (next->entry_type == ET_ENTRY_SH)
    514  1.23    nonaka 			head = next;
    515  1.23    nonaka 	}
    516  1.21    nonaka 	if (head != NULL)
    517  1.21    nonaka 		head->entry_data.SH.header_indicator[0] = ET_SECTION_HEADER_LAST;
    518  1.21    nonaka 
    519   1.1      fvdl 	/* TODO: Remaining boot disks when implemented */
    520   1.5    dyoung 
    521   1.1      fvdl 	return first_sector + used_sectors;
    522   1.1      fvdl }
    523   1.1      fvdl 
    524   1.1      fvdl int
    525  1.20  christos cd9660_setup_boot_volume_descriptor(iso9660_disk *diskStructure,
    526  1.20  christos     volume_descriptor *bvd)
    527   1.1      fvdl {
    528   1.1      fvdl 	boot_volume_descriptor *bvdData =
    529   1.1      fvdl 	    (boot_volume_descriptor*)bvd->volumeDescriptorData;
    530   1.1      fvdl 
    531   1.1      fvdl 	bvdData->boot_record_indicator[0] = ISO_VOLUME_DESCRIPTOR_BOOT;
    532   1.1      fvdl 	memcpy(bvdData->identifier, ISO_VOLUME_DESCRIPTOR_STANDARD_ID, 5);
    533   1.1      fvdl 	bvdData->version[0] = 1;
    534   1.1      fvdl 	memcpy(bvdData->boot_system_identifier, ET_ID, 23);
    535   1.1      fvdl 	memcpy(bvdData->identifier, ISO_VOLUME_DESCRIPTOR_STANDARD_ID, 5);
    536  1.20  christos 	diskStructure->boot_descriptor =
    537   1.1      fvdl 	    (boot_volume_descriptor*) bvd->volumeDescriptorData;
    538   1.1      fvdl 	return 1;
    539   1.1      fvdl }
    540   1.1      fvdl 
    541  1.15  christos static int
    542  1.15  christos cd9660_write_mbr_partition_entry(FILE *fd, int idx, off_t sector_start,
    543  1.15  christos     off_t nsectors, int type)
    544  1.15  christos {
    545  1.15  christos 	uint8_t val;
    546  1.15  christos 	uint32_t lba;
    547  1.15  christos 
    548  1.15  christos 	if (fseeko(fd, (off_t)(idx) * 16 + 0x1be, SEEK_SET) == -1)
    549  1.27   tsutsui 		err(EXIT_FAILURE, "fseeko");
    550  1.25  riastrad 
    551  1.15  christos 	val = 0x80; /* Bootable */
    552  1.15  christos 	fwrite(&val, sizeof(val), 1, fd);
    553  1.15  christos 
    554  1.15  christos 	val = 0xff; /* CHS begin */
    555  1.15  christos 	fwrite(&val, sizeof(val), 1, fd);
    556  1.15  christos 	fwrite(&val, sizeof(val), 1, fd);
    557  1.15  christos 	fwrite(&val, sizeof(val), 1, fd);
    558  1.15  christos 
    559  1.15  christos 	val = type; /* Part type */
    560  1.15  christos 	fwrite(&val, sizeof(val), 1, fd);
    561  1.15  christos 
    562  1.15  christos 	val = 0xff; /* CHS end */
    563  1.15  christos 	fwrite(&val, sizeof(val), 1, fd);
    564  1.15  christos 	fwrite(&val, sizeof(val), 1, fd);
    565  1.15  christos 	fwrite(&val, sizeof(val), 1, fd);
    566  1.15  christos 
    567  1.15  christos 	/* LBA extent */
    568  1.15  christos 	lba = htole32(sector_start);
    569  1.15  christos 	fwrite(&lba, sizeof(lba), 1, fd);
    570  1.15  christos 	lba = htole32(nsectors);
    571  1.15  christos 	fwrite(&lba, sizeof(lba), 1, fd);
    572  1.15  christos 
    573  1.15  christos 	return 0;
    574  1.15  christos }
    575  1.15  christos 
    576  1.15  christos static int
    577  1.15  christos cd9660_write_apm_partition_entry(FILE *fd, int idx, int total_partitions,
    578  1.15  christos     off_t sector_start, off_t nsectors, off_t sector_size,
    579  1.15  christos     const char *part_name, const char *part_type)
    580  1.15  christos {
    581  1.18  christos 	uint32_t apm32, part_status;
    582  1.15  christos 	uint16_t apm16;
    583  1.15  christos 
    584  1.18  christos 	/* See Apple Tech Note 1189 for the details about the pmPartStatus
    585  1.18  christos 	 * flags.
    586  1.18  christos 	 * Below the flags which are default:
    587  1.18  christos 	 * - IsValid     0x01
    588  1.18  christos 	 * - IsAllocated 0x02
    589  1.18  christos 	 * - IsReadable  0x10
    590  1.18  christos 	 * - IsWritable  0x20
    591  1.18  christos 	 */
    592  1.18  christos 	part_status = APPLE_PS_VALID | APPLE_PS_ALLOCATED | APPLE_PS_READABLE |
    593  1.18  christos 	    APPLE_PS_WRITABLE;
    594  1.18  christos 
    595  1.15  christos 	if (fseeko(fd, (off_t)(idx + 1) * sector_size, SEEK_SET) == -1)
    596  1.27   tsutsui 		err(EXIT_FAILURE, "fseeko");
    597  1.15  christos 
    598  1.15  christos 	/* Signature */
    599  1.15  christos 	apm16 = htobe16(0x504d);
    600  1.15  christos 	fwrite(&apm16, sizeof(apm16), 1, fd);
    601  1.15  christos 	apm16 = 0;
    602  1.15  christos 	fwrite(&apm16, sizeof(apm16), 1, fd);
    603  1.15  christos 
    604  1.15  christos 	/* Total number of partitions */
    605  1.15  christos 	apm32 = htobe32(total_partitions);
    606  1.15  christos 	fwrite(&apm32, sizeof(apm32), 1, fd);
    607  1.15  christos 	/* Bounds */
    608  1.15  christos 	apm32 = htobe32(sector_start);
    609  1.15  christos 	fwrite(&apm32, sizeof(apm32), 1, fd);
    610  1.15  christos 	apm32 = htobe32(nsectors);
    611  1.15  christos 	fwrite(&apm32, sizeof(apm32), 1, fd);
    612  1.15  christos 
    613  1.15  christos 	fwrite(part_name, strlen(part_name) + 1, 1, fd);
    614  1.15  christos 	fseek(fd, 32 - strlen(part_name) - 1, SEEK_CUR);
    615  1.15  christos 	fwrite(part_type, strlen(part_type) + 1, 1, fd);
    616  1.18  christos 	fseek(fd, 32 - strlen(part_type) - 1, SEEK_CUR);
    617  1.18  christos 
    618  1.18  christos 	apm32 = 0;
    619  1.18  christos 	/* pmLgDataStart */
    620  1.18  christos 	fwrite(&apm32, sizeof(apm32), 1, fd);
    621  1.25  riastrad 	/* pmDataCnt */
    622  1.18  christos 	apm32 = htobe32(nsectors);
    623  1.18  christos 	fwrite(&apm32, sizeof(apm32), 1, fd);
    624  1.18  christos 	/* pmPartStatus */
    625  1.18  christos 	apm32 = htobe32(part_status);
    626  1.18  christos 	fwrite(&apm32, sizeof(apm32), 1, fd);
    627  1.15  christos 
    628  1.15  christos 	return 0;
    629  1.15  christos }
    630  1.15  christos 
    631   1.5    dyoung int
    632  1.20  christos cd9660_write_boot(iso9660_disk *diskStructure, FILE *fd)
    633   1.1      fvdl {
    634   1.1      fvdl 	struct boot_catalog_entry *e;
    635   1.1      fvdl 	struct cd9660_boot_image *t;
    636  1.15  christos 	int apm_partitions = 0;
    637  1.15  christos 	int mbr_partitions = 0;
    638   1.1      fvdl 
    639   1.1      fvdl 	/* write boot catalog */
    640  1.20  christos 	if (fseeko(fd, (off_t)diskStructure->boot_catalog_sector *
    641  1.20  christos 	    diskStructure->sectorSize, SEEK_SET) == -1)
    642  1.27   tsutsui 		err(EXIT_FAILURE, "fseeko");
    643   1.5    dyoung 
    644  1.20  christos 	if (diskStructure->verbose_level > 0) {
    645  1.14  christos 		printf("Writing boot catalog to sector %" PRId64 "\n",
    646  1.20  christos 		    diskStructure->boot_catalog_sector);
    647   1.9    dyoung 	}
    648  1.20  christos 	LIST_FOREACH(e, &diskStructure->boot_entries, ll_struct) {
    649  1.20  christos 		if (diskStructure->verbose_level > 0) {
    650   1.9    dyoung 			printf("Writing catalog entry of type %d\n",
    651   1.9    dyoung 			    e->entry_type);
    652   1.9    dyoung 		}
    653   1.1      fvdl 		/*
    654   1.1      fvdl 		 * It doesnt matter which one gets written
    655   1.1      fvdl 		 * since they are the same size
    656   1.1      fvdl 		 */
    657   1.1      fvdl 		fwrite(&(e->entry_data.VE), 1, 32, fd);
    658   1.1      fvdl 	}
    659  1.20  christos 	if (diskStructure->verbose_level > 0)
    660  1.10    dyoung 		printf("Finished writing boot catalog\n");
    661   1.5    dyoung 
    662   1.1      fvdl 	/* copy boot images */
    663  1.20  christos 	TAILQ_FOREACH(t, &diskStructure->boot_images, image_list) {
    664  1.20  christos 		if (diskStructure->verbose_level > 0) {
    665   1.9    dyoung 			printf("Writing boot image from %s to sectors %d\n",
    666   1.9    dyoung 			    t->filename, t->sector);
    667   1.9    dyoung 		}
    668  1.20  christos 		cd9660_copy_file(diskStructure, fd, t->sector, t->filename);
    669  1.15  christos 
    670  1.25  riastrad 		if (t->system == ET_SYS_MAC)
    671  1.15  christos 			apm_partitions++;
    672  1.25  riastrad 		if (t->system == ET_SYS_PPC)
    673  1.15  christos 			mbr_partitions++;
    674  1.15  christos 	}
    675  1.15  christos 
    676  1.15  christos 	/* some systems need partition tables as well */
    677  1.20  christos 	if (mbr_partitions > 0 || diskStructure->chrp_boot) {
    678  1.15  christos 		uint16_t sig;
    679  1.15  christos 
    680  1.15  christos 		fseek(fd, 0x1fe, SEEK_SET);
    681  1.15  christos 		sig = htole16(0xaa55);
    682  1.15  christos 		fwrite(&sig, sizeof(sig), 1, fd);
    683  1.15  christos 
    684  1.15  christos 		mbr_partitions = 0;
    685  1.15  christos 
    686  1.15  christos 		/* Write ISO9660 descriptor, enclosing the whole disk */
    687  1.20  christos 		if (diskStructure->chrp_boot)
    688  1.15  christos 			cd9660_write_mbr_partition_entry(fd, mbr_partitions++,
    689  1.20  christos 			    0, diskStructure->totalSectors *
    690  1.20  christos 			    (diskStructure->sectorSize / 512), 0x96);
    691  1.15  christos 
    692  1.15  christos 		/* Write all partition entries */
    693  1.20  christos 		TAILQ_FOREACH(t, &diskStructure->boot_images, image_list) {
    694  1.15  christos 			if (t->system != ET_SYS_PPC)
    695  1.15  christos 				continue;
    696  1.15  christos 			cd9660_write_mbr_partition_entry(fd, mbr_partitions++,
    697  1.20  christos 			    t->sector * (diskStructure->sectorSize / 512),
    698  1.20  christos 			    t->num_sectors * (diskStructure->sectorSize / 512),
    699  1.15  christos 			    0x41 /* PReP Boot */);
    700  1.15  christos 		}
    701  1.15  christos 	}
    702  1.15  christos 
    703  1.15  christos 	if (apm_partitions > 0) {
    704  1.15  christos 		/* Write DDR and global APM info */
    705  1.15  christos 		uint32_t apm32;
    706  1.15  christos 		uint16_t apm16;
    707  1.15  christos 		int total_parts;
    708  1.15  christos 
    709  1.15  christos 		fseek(fd, 0, SEEK_SET);
    710  1.15  christos 		apm16 = htobe16(0x4552);
    711  1.15  christos 		fwrite(&apm16, sizeof(apm16), 1, fd);
    712  1.15  christos 		/* Device block size */
    713  1.15  christos 		apm16 = htobe16(512);
    714  1.15  christos 		fwrite(&apm16, sizeof(apm16), 1, fd);
    715  1.15  christos 		/* Device block count */
    716  1.20  christos 		apm32 = htobe32(diskStructure->totalSectors *
    717  1.20  christos 		    (diskStructure->sectorSize / 512));
    718  1.15  christos 		fwrite(&apm32, sizeof(apm32), 1, fd);
    719  1.15  christos 		/* Device type/id */
    720  1.15  christos 		apm16 = htobe16(1);
    721  1.15  christos 		fwrite(&apm16, sizeof(apm16), 1, fd);
    722  1.15  christos 		fwrite(&apm16, sizeof(apm16), 1, fd);
    723  1.15  christos 
    724  1.15  christos 		/* Count total needed entries */
    725  1.15  christos 		total_parts = 2 + apm_partitions; /* Self + ISO9660 */
    726  1.15  christos 
    727  1.15  christos 		/* Write self-descriptor */
    728  1.15  christos 		cd9660_write_apm_partition_entry(fd, 0, total_parts, 1,
    729  1.15  christos 		    total_parts, 512, "Apple", "Apple_partition_map");
    730  1.15  christos 
    731  1.15  christos 		/* Write all partition entries */
    732  1.15  christos 		apm_partitions = 0;
    733  1.20  christos 		TAILQ_FOREACH(t, &diskStructure->boot_images, image_list) {
    734  1.15  christos 			if (t->system != ET_SYS_MAC)
    735  1.15  christos 				continue;
    736  1.15  christos 
    737  1.15  christos 			cd9660_write_apm_partition_entry(fd,
    738  1.18  christos 			    1 + apm_partitions++, total_parts,
    739  1.20  christos 			    t->sector * (diskStructure->sectorSize / 512),
    740  1.20  christos 			    t->num_sectors * (diskStructure->sectorSize / 512),
    741  1.15  christos 			    512, "CD Boot", "Apple_Bootstrap");
    742  1.15  christos 		}
    743  1.18  christos 
    744  1.18  christos 		/* Write ISO9660 descriptor, enclosing the whole disk */
    745  1.18  christos 		cd9660_write_apm_partition_entry(fd, 2 + apm_partitions,
    746  1.20  christos 		    total_parts, 0, diskStructure->totalSectors *
    747  1.20  christos 		    (diskStructure->sectorSize / 512), 512, "ISO9660",
    748  1.18  christos 		    "CD_ROM_Mode_1");
    749   1.1      fvdl 	}
    750   1.1      fvdl 
    751   1.1      fvdl 	return 0;
    752   1.1      fvdl }
    753