Home | History | Annotate | Line # | Download | only in cd9660
cd9660_eltorito.c revision 1.22
      1  1.22    nonaka /*	$NetBSD: cd9660_eltorito.c,v 1.22 2017/11/09 01:28:05 nonaka 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.22    nonaka __RCSID("$NetBSD: cd9660_eltorito.c,v 1.22 2017/11/09 01:28:05 nonaka 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.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.21    nonaka 	new_image->platform_id = new_image->system;
    180  1.21    nonaka 
    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.21    nonaka 	/* First boot image is initial/default entry. */
    185  1.21    nonaka 	if (default_boot_image == NULL)
    186  1.21    nonaka 		default_boot_image = new_image;
    187  1.21    nonaka 
    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.21    nonaka 	} else if (strcmp(option_string, "platformid") == 0) {
    222  1.21    nonaka 		if (strcmp(value, "efi") == 0)
    223  1.21    nonaka 			image->platform_id = ET_SYS_EFI;
    224  1.21    nonaka 		else {
    225  1.21    nonaka 			warn("%s: unknown platform: %s", __func__, value);
    226  1.21    nonaka 			return 0;
    227  1.21    nonaka 		}
    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.22    nonaka 	entry->entry_type = ET_ENTRY_VE;
    251   1.6    dyoung 	ve = &entry->entry_data.VE;
    252   1.6    dyoung 
    253   1.6    dyoung 	ve->header_id[0] = 1;
    254   1.6    dyoung 	ve->platform_id[0] = sys;
    255   1.6    dyoung 	ve->key[0] = 0x55;
    256   1.6    dyoung 	ve->key[1] = 0xAA;
    257   1.1      fvdl 
    258   1.1      fvdl 	/* Calculate checksum */
    259   1.1      fvdl 	checksum = 0;
    260   1.6    dyoung 	cd9660_721(0, ve->checksum);
    261   1.7    dyoung 	csptr = (unsigned char*)ve;
    262   1.6    dyoung 	for (i = 0; i < sizeof(*ve); i += 2) {
    263   1.6    dyoung 		checksum += (int16_t)csptr[i];
    264   1.6    dyoung 		checksum += 256 * (int16_t)csptr[i + 1];
    265   1.1      fvdl 	}
    266   1.1      fvdl 	checksum = -checksum;
    267   1.6    dyoung 	cd9660_721(checksum, ve->checksum);
    268   1.5    dyoung 
    269   1.7    dyoung         ELTORITO_DPRINTF(("%s: header_id %d, platform_id %d, key[0] %d, key[1] %d, "
    270   1.7    dyoung 	    "checksum %04x\n", __func__, ve->header_id[0], ve->platform_id[0],
    271   1.7    dyoung 	    ve->key[0], ve->key[1], checksum));
    272   1.6    dyoung 	return entry;
    273   1.1      fvdl }
    274   1.1      fvdl 
    275   1.1      fvdl static struct boot_catalog_entry *
    276   1.1      fvdl cd9660_boot_setup_default_entry(struct cd9660_boot_image *disk)
    277   1.1      fvdl {
    278   1.1      fvdl 	struct boot_catalog_entry *default_entry;
    279   1.6    dyoung 	boot_catalog_initial_entry *ie;
    280   1.1      fvdl 
    281   1.1      fvdl 	default_entry = cd9660_init_boot_catalog_entry();
    282   1.1      fvdl 	if (default_entry == NULL)
    283   1.1      fvdl 		return NULL;
    284   1.5    dyoung 
    285  1.22    nonaka 	default_entry->entry_type = ET_ENTRY_IE;
    286   1.6    dyoung 	ie = &default_entry->entry_data.IE;
    287   1.5    dyoung 
    288   1.6    dyoung 	ie->boot_indicator[0] = disk->bootable;
    289   1.6    dyoung 	ie->media_type[0] = disk->targetMode;
    290   1.6    dyoung 	cd9660_721(disk->loadSegment, ie->load_segment);
    291   1.6    dyoung 	ie->system_type[0] = disk->system;
    292   1.7    dyoung 	cd9660_721(disk->num_sectors, ie->sector_count);
    293   1.6    dyoung 	cd9660_731(disk->sector, ie->load_rba);
    294   1.5    dyoung 
    295   1.7    dyoung 	ELTORITO_DPRINTF(("%s: boot indicator %d, media type %d, "
    296   1.7    dyoung 	    "load segment %04x, system type %d, sector count %d, "
    297   1.7    dyoung 	    "load rba %d\n", __func__, ie->boot_indicator[0],
    298   1.7    dyoung 	    ie->media_type[0], disk->loadSegment, ie->system_type[0],
    299   1.7    dyoung 	    disk->num_sectors, disk->sector));
    300   1.1      fvdl 	return default_entry;
    301   1.1      fvdl }
    302   1.1      fvdl 
    303   1.1      fvdl static struct boot_catalog_entry *
    304   1.1      fvdl cd9660_boot_setup_section_head(char platform)
    305   1.1      fvdl {
    306   1.6    dyoung 	struct boot_catalog_entry *entry;
    307   1.6    dyoung 	boot_catalog_section_header *sh;
    308   1.1      fvdl 
    309   1.6    dyoung 	entry = cd9660_init_boot_catalog_entry();
    310   1.6    dyoung 	if (entry == NULL)
    311   1.1      fvdl 		return NULL;
    312   1.6    dyoung 
    313  1.22    nonaka 	entry->entry_type = ET_ENTRY_SH;
    314   1.6    dyoung 	sh = &entry->entry_data.SH;
    315   1.1      fvdl 	/* More by default. The last one will manually be set to 0x91 */
    316   1.6    dyoung 	sh->header_indicator[0] = ET_SECTION_HEADER_MORE;
    317   1.6    dyoung 	sh->platform_id[0] = platform;
    318   1.6    dyoung 	sh->num_section_entries[0] = 0;
    319   1.6    dyoung 	return entry;
    320   1.1      fvdl }
    321   1.1      fvdl 
    322   1.1      fvdl static struct boot_catalog_entry *
    323   1.1      fvdl cd9660_boot_setup_section_entry(struct cd9660_boot_image *disk)
    324   1.1      fvdl {
    325   1.1      fvdl 	struct boot_catalog_entry *entry;
    326   1.6    dyoung 	boot_catalog_section_entry *se;
    327   1.1      fvdl 	if ((entry = cd9660_init_boot_catalog_entry()) == NULL)
    328   1.1      fvdl 		return NULL;
    329   1.5    dyoung 
    330  1.22    nonaka 	entry->entry_type = ET_ENTRY_SE;
    331   1.6    dyoung 	se = &entry->entry_data.SE;
    332   1.6    dyoung 
    333   1.6    dyoung 	se->boot_indicator[0] = ET_BOOTABLE;
    334   1.6    dyoung 	se->media_type[0] = disk->targetMode;
    335   1.6    dyoung 	cd9660_721(disk->loadSegment, se->load_segment);
    336   1.7    dyoung 	cd9660_721(disk->num_sectors, se->sector_count);
    337   1.7    dyoung 	cd9660_731(disk->sector, se->load_rba);
    338   1.1      fvdl 	return entry;
    339   1.1      fvdl }
    340   1.1      fvdl 
    341   1.1      fvdl #if 0
    342   1.1      fvdl static u_char
    343   1.1      fvdl cd9660_boot_get_system_type(struct cd9660_boot_image *disk)
    344   1.1      fvdl {
    345   1.1      fvdl 	/*
    346   1.1      fvdl 		For hard drive booting, we need to examine the MBR to figure
    347   1.1      fvdl 		out what the partition type is
    348   1.1      fvdl 	*/
    349   1.1      fvdl 	return 0;
    350   1.1      fvdl }
    351   1.1      fvdl #endif
    352   1.1      fvdl 
    353   1.1      fvdl /*
    354   1.1      fvdl  * Set up the BVD, Boot catalog, and the boot entries, but do no writing
    355   1.1      fvdl  */
    356   1.1      fvdl int
    357  1.20  christos cd9660_setup_boot(iso9660_disk *diskStructure, int first_sector)
    358   1.1      fvdl {
    359   1.1      fvdl 	int sector;
    360   1.1      fvdl 	int used_sectors;
    361   1.1      fvdl 	int num_entries = 0;
    362   1.1      fvdl 	int catalog_sectors;
    363  1.21    nonaka 	struct boot_catalog_entry *x86_head, *mac_head, *ppc_head, *efi_head,
    364   1.8    dyoung 		*valid_entry, *default_entry, *temp, *head, **headp, *next;
    365   1.1      fvdl 	struct cd9660_boot_image *tmp_disk;
    366   1.1      fvdl 
    367   1.8    dyoung 	headp = NULL;
    368  1.21    nonaka 	x86_head = mac_head = ppc_head = efi_head = NULL;
    369   1.5    dyoung 
    370   1.1      fvdl 	/* If there are no boot disks, don't bother building boot information */
    371  1.20  christos 	if (TAILQ_EMPTY(&diskStructure->boot_images))
    372   1.1      fvdl 		return 0;
    373   1.1      fvdl 
    374   1.1      fvdl 	/* Point to catalog: For now assume it consumes one sector */
    375   1.9    dyoung 	ELTORITO_DPRINTF(("Boot catalog will go in sector %d\n", first_sector));
    376  1.20  christos 	diskStructure->boot_catalog_sector = first_sector;
    377   1.1      fvdl 	cd9660_bothendian_dword(first_sector,
    378  1.20  christos 		diskStructure->boot_descriptor->boot_catalog_pointer);
    379   1.5    dyoung 
    380   1.1      fvdl 	/* Step 1: Generate boot catalog */
    381   1.1      fvdl 	/* Step 1a: Validation entry */
    382   1.1      fvdl 	valid_entry = cd9660_boot_setup_validation_entry(ET_SYS_X86);
    383   1.1      fvdl 	if (valid_entry == NULL)
    384   1.1      fvdl 		return -1;
    385   1.5    dyoung 
    386   1.1      fvdl 	/*
    387   1.1      fvdl 	 * Count how many boot images there are,
    388   1.1      fvdl 	 * and how many sectors they consume.
    389   1.1      fvdl 	 */
    390   1.1      fvdl 	num_entries = 1;
    391   1.1      fvdl 	used_sectors = 0;
    392   1.5    dyoung 
    393  1.20  christos 	TAILQ_FOREACH(tmp_disk, &diskStructure->boot_images, image_list) {
    394   1.1      fvdl 		used_sectors += tmp_disk->num_sectors;
    395   1.5    dyoung 
    396   1.1      fvdl 		/* One default entry per image */
    397   1.1      fvdl 		num_entries++;
    398   1.1      fvdl 	}
    399  1.20  christos 	catalog_sectors = howmany(num_entries * 0x20, diskStructure->sectorSize);
    400   1.1      fvdl 	used_sectors += catalog_sectors;
    401   1.5    dyoung 
    402  1.20  christos 	if (diskStructure->verbose_level > 0) {
    403   1.9    dyoung 		printf("%s: there will be %i entries consuming %i sectors. "
    404   1.9    dyoung 		       "Catalog is %i sectors\n", __func__, num_entries,
    405   1.9    dyoung 		       used_sectors, catalog_sectors);
    406   1.9    dyoung 	}
    407   1.5    dyoung 
    408   1.1      fvdl 	/* Populate sector numbers */
    409   1.1      fvdl 	sector = first_sector + catalog_sectors;
    410  1.20  christos 	TAILQ_FOREACH(tmp_disk, &diskStructure->boot_images, image_list) {
    411   1.1      fvdl 		tmp_disk->sector = sector;
    412  1.21    nonaka 		sector += tmp_disk->num_sectors /
    413  1.21    nonaka 		    (diskStructure->sectorSize / 512);
    414   1.1      fvdl 	}
    415   1.5    dyoung 
    416  1.20  christos 	LIST_INSERT_HEAD(&diskStructure->boot_entries, valid_entry, ll_struct);
    417   1.5    dyoung 
    418   1.1      fvdl 	/* Step 1b: Initial/default entry */
    419   1.1      fvdl 	/* TODO : PARAM */
    420  1.21    nonaka 	if (default_boot_image != NULL) {
    421  1.21    nonaka 		struct cd9660_boot_image *tcbi;
    422  1.21    nonaka 		TAILQ_FOREACH(tcbi, &diskStructure->boot_images, image_list) {
    423  1.21    nonaka 			if (tcbi == default_boot_image) {
    424  1.21    nonaka 				tmp_disk = tcbi;
    425  1.21    nonaka 				break;
    426  1.21    nonaka 			}
    427  1.21    nonaka 		}
    428  1.21    nonaka 	}
    429  1.21    nonaka 	if (tmp_disk == NULL)
    430  1.21    nonaka 		tmp_disk = TAILQ_FIRST(&diskStructure->boot_images);
    431   1.1      fvdl 	default_entry = cd9660_boot_setup_default_entry(tmp_disk);
    432   1.1      fvdl 	if (default_entry == NULL) {
    433   1.1      fvdl 		warnx("Error: memory allocation failed in cd9660_setup_boot");
    434   1.1      fvdl 		return -1;
    435   1.1      fvdl 	}
    436   1.5    dyoung 
    437   1.1      fvdl 	LIST_INSERT_AFTER(valid_entry, default_entry, ll_struct);
    438   1.5    dyoung 
    439   1.1      fvdl 	/* Todo: multiple default entries? */
    440   1.5    dyoung 
    441  1.21    nonaka 	tmp_disk = TAILQ_FIRST(&diskStructure->boot_images);
    442   1.5    dyoung 
    443  1.21    nonaka 	head = NULL;
    444   1.1      fvdl 	temp = default_entry;
    445   1.5    dyoung 
    446   1.1      fvdl 	/* If multiple boot images are given : */
    447  1.21    nonaka 	for (; tmp_disk != NULL; tmp_disk = TAILQ_NEXT(tmp_disk, image_list)) {
    448  1.21    nonaka 		if (tmp_disk == default_boot_image)
    449  1.21    nonaka 			continue;
    450  1.21    nonaka 
    451   1.1      fvdl 		/* Step 2: Section header */
    452  1.21    nonaka 		switch (tmp_disk->platform_id) {
    453   1.1      fvdl 		case ET_SYS_X86:
    454   1.8    dyoung 			headp = &x86_head;
    455   1.1      fvdl 			break;
    456   1.1      fvdl 		case ET_SYS_PPC:
    457   1.8    dyoung 			headp = &ppc_head;
    458   1.1      fvdl 			break;
    459   1.1      fvdl 		case ET_SYS_MAC:
    460   1.8    dyoung 			headp = &mac_head;
    461   1.1      fvdl 			break;
    462  1.21    nonaka 		case ET_SYS_EFI:
    463  1.21    nonaka 			headp = &efi_head;
    464  1.21    nonaka 			break;
    465   1.8    dyoung 		default:
    466   1.8    dyoung 			warnx("%s: internal error: unknown system type",
    467   1.8    dyoung 			    __func__);
    468   1.8    dyoung 			return -1;
    469   1.1      fvdl 		}
    470   1.1      fvdl 
    471   1.8    dyoung 		if (*headp == NULL) {
    472   1.8    dyoung 			head =
    473  1.21    nonaka 			  cd9660_boot_setup_section_head(tmp_disk->platform_id);
    474   1.8    dyoung 			if (head == NULL) {
    475   1.1      fvdl 				warnx("Error: memory allocation failed in "
    476   1.1      fvdl 				      "cd9660_setup_boot");
    477   1.1      fvdl 				return -1;
    478   1.1      fvdl 			}
    479   1.8    dyoung 			LIST_INSERT_AFTER(default_entry, head, ll_struct);
    480   1.8    dyoung 			*headp = head;
    481   1.8    dyoung 		} else
    482   1.8    dyoung 			head = *headp;
    483   1.8    dyoung 
    484   1.8    dyoung 		head->entry_data.SH.num_section_entries[0]++;
    485   1.5    dyoung 
    486   1.1      fvdl 		/* Step 2a: Section entry and extensions */
    487   1.1      fvdl 		temp = cd9660_boot_setup_section_entry(tmp_disk);
    488   1.1      fvdl 		if (temp == NULL) {
    489   1.8    dyoung 			warn("%s: cd9660_boot_setup_section_entry", __func__);
    490   1.1      fvdl 			return -1;
    491   1.1      fvdl 		}
    492   1.1      fvdl 
    493   1.8    dyoung 		while ((next = LIST_NEXT(head, ll_struct)) != NULL &&
    494   1.3    dyoung 		       next->entry_type == ET_ENTRY_SE)
    495   1.8    dyoung 			head = next;
    496   1.5    dyoung 
    497   1.8    dyoung 		LIST_INSERT_AFTER(head, temp, ll_struct);
    498   1.1      fvdl 	}
    499   1.5    dyoung 
    500  1.21    nonaka 	if (head != NULL)
    501  1.21    nonaka 		head->entry_data.SH.header_indicator[0] = ET_SECTION_HEADER_LAST;
    502  1.21    nonaka 
    503   1.1      fvdl 	/* TODO: Remaining boot disks when implemented */
    504   1.5    dyoung 
    505   1.1      fvdl 	return first_sector + used_sectors;
    506   1.1      fvdl }
    507   1.1      fvdl 
    508   1.1      fvdl int
    509  1.20  christos cd9660_setup_boot_volume_descriptor(iso9660_disk *diskStructure,
    510  1.20  christos     volume_descriptor *bvd)
    511   1.1      fvdl {
    512   1.1      fvdl 	boot_volume_descriptor *bvdData =
    513   1.1      fvdl 	    (boot_volume_descriptor*)bvd->volumeDescriptorData;
    514   1.1      fvdl 
    515   1.1      fvdl 	bvdData->boot_record_indicator[0] = ISO_VOLUME_DESCRIPTOR_BOOT;
    516   1.1      fvdl 	memcpy(bvdData->identifier, ISO_VOLUME_DESCRIPTOR_STANDARD_ID, 5);
    517   1.1      fvdl 	bvdData->version[0] = 1;
    518   1.1      fvdl 	memcpy(bvdData->boot_system_identifier, ET_ID, 23);
    519   1.1      fvdl 	memcpy(bvdData->identifier, ISO_VOLUME_DESCRIPTOR_STANDARD_ID, 5);
    520  1.20  christos 	diskStructure->boot_descriptor =
    521   1.1      fvdl 	    (boot_volume_descriptor*) bvd->volumeDescriptorData;
    522   1.1      fvdl 	return 1;
    523   1.1      fvdl }
    524   1.1      fvdl 
    525  1.15  christos static int
    526  1.15  christos cd9660_write_mbr_partition_entry(FILE *fd, int idx, off_t sector_start,
    527  1.15  christos     off_t nsectors, int type)
    528  1.15  christos {
    529  1.15  christos 	uint8_t val;
    530  1.15  christos 	uint32_t lba;
    531  1.15  christos 
    532  1.15  christos 	if (fseeko(fd, (off_t)(idx) * 16 + 0x1be, SEEK_SET) == -1)
    533  1.15  christos 		err(1, "fseeko");
    534  1.15  christos 
    535  1.15  christos 	val = 0x80; /* Bootable */
    536  1.15  christos 	fwrite(&val, sizeof(val), 1, fd);
    537  1.15  christos 
    538  1.15  christos 	val = 0xff; /* CHS begin */
    539  1.15  christos 	fwrite(&val, sizeof(val), 1, fd);
    540  1.15  christos 	fwrite(&val, sizeof(val), 1, fd);
    541  1.15  christos 	fwrite(&val, sizeof(val), 1, fd);
    542  1.15  christos 
    543  1.15  christos 	val = type; /* Part type */
    544  1.15  christos 	fwrite(&val, sizeof(val), 1, fd);
    545  1.15  christos 
    546  1.15  christos 	val = 0xff; /* CHS end */
    547  1.15  christos 	fwrite(&val, sizeof(val), 1, fd);
    548  1.15  christos 	fwrite(&val, sizeof(val), 1, fd);
    549  1.15  christos 	fwrite(&val, sizeof(val), 1, fd);
    550  1.15  christos 
    551  1.15  christos 	/* LBA extent */
    552  1.15  christos 	lba = htole32(sector_start);
    553  1.15  christos 	fwrite(&lba, sizeof(lba), 1, fd);
    554  1.15  christos 	lba = htole32(nsectors);
    555  1.15  christos 	fwrite(&lba, sizeof(lba), 1, fd);
    556  1.15  christos 
    557  1.15  christos 	return 0;
    558  1.15  christos }
    559  1.15  christos 
    560  1.15  christos static int
    561  1.15  christos cd9660_write_apm_partition_entry(FILE *fd, int idx, int total_partitions,
    562  1.15  christos     off_t sector_start, off_t nsectors, off_t sector_size,
    563  1.15  christos     const char *part_name, const char *part_type)
    564  1.15  christos {
    565  1.18  christos 	uint32_t apm32, part_status;
    566  1.15  christos 	uint16_t apm16;
    567  1.15  christos 
    568  1.18  christos 	/* See Apple Tech Note 1189 for the details about the pmPartStatus
    569  1.18  christos 	 * flags.
    570  1.18  christos 	 * Below the flags which are default:
    571  1.18  christos 	 * - IsValid     0x01
    572  1.18  christos 	 * - IsAllocated 0x02
    573  1.18  christos 	 * - IsReadable  0x10
    574  1.18  christos 	 * - IsWritable  0x20
    575  1.18  christos 	 */
    576  1.18  christos 	part_status = APPLE_PS_VALID | APPLE_PS_ALLOCATED | APPLE_PS_READABLE |
    577  1.18  christos 	    APPLE_PS_WRITABLE;
    578  1.18  christos 
    579  1.15  christos 	if (fseeko(fd, (off_t)(idx + 1) * sector_size, SEEK_SET) == -1)
    580  1.15  christos 		err(1, "fseeko");
    581  1.15  christos 
    582  1.15  christos 	/* Signature */
    583  1.15  christos 	apm16 = htobe16(0x504d);
    584  1.15  christos 	fwrite(&apm16, sizeof(apm16), 1, fd);
    585  1.15  christos 	apm16 = 0;
    586  1.15  christos 	fwrite(&apm16, sizeof(apm16), 1, fd);
    587  1.15  christos 
    588  1.15  christos 	/* Total number of partitions */
    589  1.15  christos 	apm32 = htobe32(total_partitions);
    590  1.15  christos 	fwrite(&apm32, sizeof(apm32), 1, fd);
    591  1.15  christos 	/* Bounds */
    592  1.15  christos 	apm32 = htobe32(sector_start);
    593  1.15  christos 	fwrite(&apm32, sizeof(apm32), 1, fd);
    594  1.15  christos 	apm32 = htobe32(nsectors);
    595  1.15  christos 	fwrite(&apm32, sizeof(apm32), 1, fd);
    596  1.15  christos 
    597  1.15  christos 	fwrite(part_name, strlen(part_name) + 1, 1, fd);
    598  1.15  christos 	fseek(fd, 32 - strlen(part_name) - 1, SEEK_CUR);
    599  1.15  christos 	fwrite(part_type, strlen(part_type) + 1, 1, fd);
    600  1.18  christos 	fseek(fd, 32 - strlen(part_type) - 1, SEEK_CUR);
    601  1.18  christos 
    602  1.18  christos 	apm32 = 0;
    603  1.18  christos 	/* pmLgDataStart */
    604  1.18  christos 	fwrite(&apm32, sizeof(apm32), 1, fd);
    605  1.18  christos 	/* pmDataCnt */
    606  1.18  christos 	apm32 = htobe32(nsectors);
    607  1.18  christos 	fwrite(&apm32, sizeof(apm32), 1, fd);
    608  1.18  christos 	/* pmPartStatus */
    609  1.18  christos 	apm32 = htobe32(part_status);
    610  1.18  christos 	fwrite(&apm32, sizeof(apm32), 1, fd);
    611  1.15  christos 
    612  1.15  christos 	return 0;
    613  1.15  christos }
    614  1.15  christos 
    615   1.5    dyoung int
    616  1.20  christos cd9660_write_boot(iso9660_disk *diskStructure, FILE *fd)
    617   1.1      fvdl {
    618   1.1      fvdl 	struct boot_catalog_entry *e;
    619   1.1      fvdl 	struct cd9660_boot_image *t;
    620  1.15  christos 	int apm_partitions = 0;
    621  1.15  christos 	int mbr_partitions = 0;
    622   1.1      fvdl 
    623   1.1      fvdl 	/* write boot catalog */
    624  1.20  christos 	if (fseeko(fd, (off_t)diskStructure->boot_catalog_sector *
    625  1.20  christos 	    diskStructure->sectorSize, SEEK_SET) == -1)
    626  1.13  christos 		err(1, "fseeko");
    627   1.5    dyoung 
    628  1.20  christos 	if (diskStructure->verbose_level > 0) {
    629  1.14  christos 		printf("Writing boot catalog to sector %" PRId64 "\n",
    630  1.20  christos 		    diskStructure->boot_catalog_sector);
    631   1.9    dyoung 	}
    632  1.20  christos 	LIST_FOREACH(e, &diskStructure->boot_entries, ll_struct) {
    633  1.20  christos 		if (diskStructure->verbose_level > 0) {
    634   1.9    dyoung 			printf("Writing catalog entry of type %d\n",
    635   1.9    dyoung 			    e->entry_type);
    636   1.9    dyoung 		}
    637   1.1      fvdl 		/*
    638   1.1      fvdl 		 * It doesnt matter which one gets written
    639   1.1      fvdl 		 * since they are the same size
    640   1.1      fvdl 		 */
    641   1.1      fvdl 		fwrite(&(e->entry_data.VE), 1, 32, fd);
    642   1.1      fvdl 	}
    643  1.20  christos 	if (diskStructure->verbose_level > 0)
    644  1.10    dyoung 		printf("Finished writing boot catalog\n");
    645   1.5    dyoung 
    646   1.1      fvdl 	/* copy boot images */
    647  1.20  christos 	TAILQ_FOREACH(t, &diskStructure->boot_images, image_list) {
    648  1.20  christos 		if (diskStructure->verbose_level > 0) {
    649   1.9    dyoung 			printf("Writing boot image from %s to sectors %d\n",
    650   1.9    dyoung 			    t->filename, t->sector);
    651   1.9    dyoung 		}
    652  1.20  christos 		cd9660_copy_file(diskStructure, fd, t->sector, t->filename);
    653  1.15  christos 
    654  1.15  christos 		if (t->system == ET_SYS_MAC)
    655  1.15  christos 			apm_partitions++;
    656  1.15  christos 		if (t->system == ET_SYS_PPC)
    657  1.15  christos 			mbr_partitions++;
    658  1.15  christos 	}
    659  1.15  christos 
    660  1.15  christos 	/* some systems need partition tables as well */
    661  1.20  christos 	if (mbr_partitions > 0 || diskStructure->chrp_boot) {
    662  1.15  christos 		uint16_t sig;
    663  1.15  christos 
    664  1.15  christos 		fseek(fd, 0x1fe, SEEK_SET);
    665  1.15  christos 		sig = htole16(0xaa55);
    666  1.15  christos 		fwrite(&sig, sizeof(sig), 1, fd);
    667  1.15  christos 
    668  1.15  christos 		mbr_partitions = 0;
    669  1.15  christos 
    670  1.15  christos 		/* Write ISO9660 descriptor, enclosing the whole disk */
    671  1.20  christos 		if (diskStructure->chrp_boot)
    672  1.15  christos 			cd9660_write_mbr_partition_entry(fd, mbr_partitions++,
    673  1.20  christos 			    0, diskStructure->totalSectors *
    674  1.20  christos 			    (diskStructure->sectorSize / 512), 0x96);
    675  1.15  christos 
    676  1.15  christos 		/* Write all partition entries */
    677  1.20  christos 		TAILQ_FOREACH(t, &diskStructure->boot_images, image_list) {
    678  1.15  christos 			if (t->system != ET_SYS_PPC)
    679  1.15  christos 				continue;
    680  1.15  christos 			cd9660_write_mbr_partition_entry(fd, mbr_partitions++,
    681  1.20  christos 			    t->sector * (diskStructure->sectorSize / 512),
    682  1.20  christos 			    t->num_sectors * (diskStructure->sectorSize / 512),
    683  1.15  christos 			    0x41 /* PReP Boot */);
    684  1.15  christos 		}
    685  1.15  christos 	}
    686  1.15  christos 
    687  1.15  christos 	if (apm_partitions > 0) {
    688  1.15  christos 		/* Write DDR and global APM info */
    689  1.15  christos 		uint32_t apm32;
    690  1.15  christos 		uint16_t apm16;
    691  1.15  christos 		int total_parts;
    692  1.15  christos 
    693  1.15  christos 		fseek(fd, 0, SEEK_SET);
    694  1.15  christos 		apm16 = htobe16(0x4552);
    695  1.15  christos 		fwrite(&apm16, sizeof(apm16), 1, fd);
    696  1.15  christos 		/* Device block size */
    697  1.15  christos 		apm16 = htobe16(512);
    698  1.15  christos 		fwrite(&apm16, sizeof(apm16), 1, fd);
    699  1.15  christos 		/* Device block count */
    700  1.20  christos 		apm32 = htobe32(diskStructure->totalSectors *
    701  1.20  christos 		    (diskStructure->sectorSize / 512));
    702  1.15  christos 		fwrite(&apm32, sizeof(apm32), 1, fd);
    703  1.15  christos 		/* Device type/id */
    704  1.15  christos 		apm16 = htobe16(1);
    705  1.15  christos 		fwrite(&apm16, sizeof(apm16), 1, fd);
    706  1.15  christos 		fwrite(&apm16, sizeof(apm16), 1, fd);
    707  1.15  christos 
    708  1.15  christos 		/* Count total needed entries */
    709  1.15  christos 		total_parts = 2 + apm_partitions; /* Self + ISO9660 */
    710  1.15  christos 
    711  1.15  christos 		/* Write self-descriptor */
    712  1.15  christos 		cd9660_write_apm_partition_entry(fd, 0, total_parts, 1,
    713  1.15  christos 		    total_parts, 512, "Apple", "Apple_partition_map");
    714  1.15  christos 
    715  1.15  christos 		/* Write all partition entries */
    716  1.15  christos 		apm_partitions = 0;
    717  1.20  christos 		TAILQ_FOREACH(t, &diskStructure->boot_images, image_list) {
    718  1.15  christos 			if (t->system != ET_SYS_MAC)
    719  1.15  christos 				continue;
    720  1.15  christos 
    721  1.15  christos 			cd9660_write_apm_partition_entry(fd,
    722  1.18  christos 			    1 + apm_partitions++, total_parts,
    723  1.20  christos 			    t->sector * (diskStructure->sectorSize / 512),
    724  1.20  christos 			    t->num_sectors * (diskStructure->sectorSize / 512),
    725  1.15  christos 			    512, "CD Boot", "Apple_Bootstrap");
    726  1.15  christos 		}
    727  1.18  christos 
    728  1.18  christos 		/* Write ISO9660 descriptor, enclosing the whole disk */
    729  1.18  christos 		cd9660_write_apm_partition_entry(fd, 2 + apm_partitions,
    730  1.20  christos 		    total_parts, 0, diskStructure->totalSectors *
    731  1.20  christos 		    (diskStructure->sectorSize / 512), 512, "ISO9660",
    732  1.18  christos 		    "CD_ROM_Mode_1");
    733   1.1      fvdl 	}
    734   1.1      fvdl 
    735   1.1      fvdl 	return 0;
    736   1.1      fvdl }
    737