/*
* Copyright (c) 2005 Daniel Watt, Walter Deignan, Ryan Gabrys, Alan
* Perez-Rathke and Ram Vedam. All rights reserved.
*
* This code was written by Daniel Watt, Walter Deignan, Ryan Gabrys,
* Alan Perez-Rathke and Ram Vedam.
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY DANIEL WATT, WALTER DEIGNAN, RYAN
* GABRYS, ALAN PEREZ-RATHKE AND RAM VEDAM ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL DANIEL WATT, WALTER DEIGNAN, RYAN
* GABRYS, ALAN PEREZ-RATHKE AND RAM VEDAM BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE,DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGE.
*/
if (*boot_info == '\0') {
warnx("Error: Boot disk information must be in the "
"format 'system;filename'");
return 0;
}
/* First decode the boot information */
temp = estrdup(boot_info);
sysname = temp;
filename = strchr(sysname, ';');
if (filename == NULL) {
warnx("supply boot disk information in the format "
"'system;filename'");
free(temp);
return 0;
}
*filename++ = '\0';
if (diskStructure->verbose_level > 0) {
printf("Found bootdisk with system %s, and filename %s\n",
sysname, filename);
}
new_image = ecalloc(1, sizeof(*new_image));
new_image->loadSegment = 0; /* default for now */
/* Decode System */
if (strcmp(sysname, "i386") == 0)
new_image->system = ET_SYS_X86;
else if (strcmp(sysname, "powerpc") == 0)
new_image->system = ET_SYS_PPC;
else if (strcmp(sysname, "macppc") == 0 ||
strcmp(sysname, "mac68k") == 0)
new_image->system = ET_SYS_MAC;
else if (strcmp(sysname, "efi") == 0)
new_image->system = ET_SYS_EFI;
else {
warnx("boot disk system must be "
"i386, powerpc, macppc, mac68k, or efi");
free(temp);
free(new_image);
return 0;
}
new_image->filename = estrdup(filename);
free(temp);
/* Get information about the file */
if (lstat(new_image->filename, &stbuf) == -1)
err(EXIT_FAILURE, "%s: lstat(\"%s\")", __func__,
new_image->filename);
switch (stbuf.st_size) {
case 1440 * 1024:
new_image->targetMode = ET_MEDIA_144FDD;
mode_msg = "Assigned boot image to 1.44 emulation mode";
break;
case 1200 * 1024:
new_image->targetMode = ET_MEDIA_12FDD;
mode_msg = "Assigned boot image to 1.2 emulation mode";
break;
case 2880 * 1024:
new_image->targetMode = ET_MEDIA_288FDD;
mode_msg = "Assigned boot image to 2.88 emulation mode";
break;
default:
new_image->targetMode = ET_MEDIA_NOEM;
mode_msg = "Assigned boot image to no emulation mode";
break;
}
if (diskStructure->verbose_level > 0)
printf("%s\n", mode_msg);
/* Group images for the same platform together. */
TAILQ_FOREACH(tmp_image, &diskStructure->boot_images, image_list) {
if (tmp_image->system != new_image->system)
break;
}
/* Find the last image added */
TAILQ_FOREACH(image, &diskStructure->boot_images, image_list) {
if (image->serialno + 1 == diskStructure->image_serialno)
break;
}
if (image == NULL)
errx(EXIT_FAILURE, "Attempted to add boot option, "
"but no boot images have been specified");
entry = cd9660_init_boot_catalog_entry();
if (entry == NULL)
return NULL;
entry->entry_type = ET_ENTRY_SH;
sh = &entry->entry_data.SH;
/* More by default. The last one will manually be set to 0x91 */
sh->header_indicator[0] = ET_SECTION_HEADER_MORE;
sh->platform_id[0] = platform;
sh->num_section_entries[0] = 0;
return entry;
}
#if 0
static u_char
cd9660_boot_get_system_type(struct cd9660_boot_image *disk)
{
/*
For hard drive booting, we need to examine the MBR to figure
out what the partition type is
*/
return 0;
}
#endif
/*
* Set up the BVD, Boot catalog, and the boot entries, but do no writing
*/
int
cd9660_setup_boot(iso9660_disk *diskStructure, int first_sector)
{
int sector;
int used_sectors;
int num_entries = 0;
int catalog_sectors;
struct boot_catalog_entry *x86_head, *mac_head, *ppc_head, *efi_head,
*valid_entry, *default_entry, *temp, *head, **headp, *next;
struct cd9660_boot_image *tmp_disk;
u_char system;
/* If there are no boot disks, don't bother building boot information */
if (TAILQ_EMPTY(&diskStructure->boot_images))
return 0;
/* Point to catalog: For now assume it consumes one sector */
ELTORITO_DPRINTF(("Boot catalog will go in sector %d\n", first_sector));
diskStructure->boot_catalog_sector = first_sector;
cd9660_731(first_sector,
diskStructure->boot_descriptor->boot_catalog_pointer);
/*
* Use system type of default image for validation entry. Fallback to
* X86 system type if not found.
*/
system = default_boot_image != NULL ? default_boot_image->system :
ET_SYS_X86;
/* One default entry per image */
num_entries++;
}
catalog_sectors = howmany(num_entries * 0x20, diskStructure->sectorSize);
used_sectors += catalog_sectors;
if (diskStructure->verbose_level > 0) {
printf("%s: there will be %i entries consuming %i sectors. "
"Catalog is %i sectors\n", __func__, num_entries,
used_sectors, catalog_sectors);
}
/* If multiple boot images are given : */
for (; tmp_disk != NULL; tmp_disk = TAILQ_NEXT(tmp_disk, image_list)) {
if (tmp_disk == default_boot_image)
continue;
/* Step 2: Section header */
switch (tmp_disk->platform_id) {
case ET_SYS_X86:
headp = &x86_head;
break;
case ET_SYS_PPC:
headp = &ppc_head;
break;
case ET_SYS_MAC:
headp = &mac_head;
break;
case ET_SYS_EFI:
headp = &efi_head;
break;
default:
warnx("%s: internal error: unknown system type",
__func__);
return -1;
}
if (*headp == NULL) {
head =
cd9660_boot_setup_section_head(tmp_disk->platform_id);
if (head == NULL) {
warnx("Error: memory allocation failed in "
"cd9660_setup_boot");
return -1;
}
LIST_INSERT_AFTER(default_entry, head, ll_struct);
*headp = head;
} else
head = *headp;
while ((next = LIST_NEXT(head, ll_struct)) != NULL &&
next->entry_type == ET_ENTRY_SE)
head = next;
LIST_INSERT_AFTER(head, temp, ll_struct);
}
/* Find the last Section Header entry and mark it as the last. */
head = NULL;
LIST_FOREACH(next, &diskStructure->boot_entries, ll_struct) {
if (next->entry_type == ET_ENTRY_SH)
head = next;
}
if (head != NULL)
head->entry_data.SH.header_indicator[0] = ET_SECTION_HEADER_LAST;
/* TODO: Remaining boot disks when implemented */
return first_sector + used_sectors;
}
int
cd9660_setup_boot_volume_descriptor(iso9660_disk *diskStructure,
volume_descriptor *bvd)
{
boot_volume_descriptor *bvdData =
(boot_volume_descriptor*)bvd->volumeDescriptorData;
if (diskStructure->verbose_level > 0) {
printf("Writing boot catalog to sector %" PRId64 "\n",
diskStructure->boot_catalog_sector);
}
LIST_FOREACH(e, &diskStructure->boot_entries, ll_struct) {
if (diskStructure->verbose_level > 0) {
printf("Writing catalog entry of type %d\n",
e->entry_type);
}
/*
* It doesnt matter which one gets written
* since they are the same size
*/
fwrite(&(e->entry_data.VE), 1, 32, fd);
}
if (diskStructure->verbose_level > 0)
printf("Finished writing boot catalog\n");
/* copy boot images */
TAILQ_FOREACH(t, &diskStructure->boot_images, image_list) {
if (diskStructure->verbose_level > 0) {
printf("Writing boot image from %s to sectors %d\n",
t->filename, t->sector);
}
cd9660_copy_file(diskStructure, fd, t->sector, t->filename);
if (t->system == ET_SYS_MAC)
apm_partitions++;
if (t->system == ET_SYS_PPC)
mbr_partitions++;
}
/* some systems need partition tables as well */
if (mbr_partitions > 0 || diskStructure->chrp_boot) {
uint16_t sig;
fseek(fd, 0x1fe, SEEK_SET);
sig = htole16(0xaa55);
fwrite(&sig, sizeof(sig), 1, fd);
mbr_partitions = 0;
/* Write ISO9660 descriptor, enclosing the whole disk */
if (diskStructure->chrp_boot)
cd9660_write_mbr_partition_entry(fd, mbr_partitions++,
0, diskStructure->totalSectors *
(diskStructure->sectorSize / 512), 0x96);