#include "cd9660.h"
#include "cd9660_eltorito.h"
#include <util.h>
#include <sys/cdefs.h>
#define APPLE_PS_VALID 0x00000001
#define APPLE_PS_ALLOCATED 0x00000002
#define APPLE_PS_READABLE 0x00000010
#define APPLE_PS_WRITABLE 0x00000020
#ifdef DEBUG
#define ELTORITO_DPRINTF(__x) printf __x
#else
#define ELTORITO_DPRINTF(__x)
#endif
#include <util.h>
static struct boot_catalog_entry *cd9660_init_boot_catalog_entry(void);
static struct boot_catalog_entry *cd9660_boot_setup_validation_entry(char);
static struct boot_catalog_entry *cd9660_boot_setup_default_entry(
struct cd9660_boot_image *);
static struct boot_catalog_entry *cd9660_boot_setup_section_head(char);
#if 0
static u_char cd9660_boot_get_system_type(struct cd9660_boot_image *);
#endif
static struct cd9660_boot_image *default_boot_image;
int
cd9660_add_boot_disk(iso9660_disk *diskStructure, const char *boot_info)
{
struct stat stbuf;
const char *mode_msg;
char *temp;
char *sysname;
char *filename;
struct cd9660_boot_image *new_image, *tmp_image;
assert(boot_info != NULL);
if (*boot_info == '\0') {
warnx("Error: Boot disk information must be in the "
"format 'system;filename'");
return 0;
}
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;
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);
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);
new_image->size = stbuf.st_size;
new_image->num_sectors =
howmany(new_image->size, diskStructure->sectorSize) *
howmany(diskStructure->sectorSize, 512);
if (diskStructure->verbose_level > 0) {
printf("New image has size %d, uses %d 512-byte sectors\n",
new_image->size, new_image->num_sectors);
}
new_image->sector = -1;
new_image->bootable = ET_BOOTABLE;
TAILQ_FOREACH(tmp_image, &diskStructure->boot_images, image_list) {
if (tmp_image->system != new_image->system)
break;
}
if (tmp_image == NULL) {
TAILQ_INSERT_HEAD(&diskStructure->boot_images, new_image,
image_list);
} else
TAILQ_INSERT_BEFORE(tmp_image, new_image, image_list);
new_image->serialno = diskStructure->image_serialno++;
new_image->platform_id = new_image->system;
diskStructure->is_bootable = 1;
if (default_boot_image == NULL)
default_boot_image = new_image;
return 1;
}
int
cd9660_eltorito_add_boot_option(iso9660_disk *diskStructure,
const char *option_string, const char *value)
{
char *eptr;
struct cd9660_boot_image *image;
assert(option_string != NULL);
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");
if (strcmp(option_string, "no-emul-boot") == 0) {
image->targetMode = ET_MEDIA_NOEM;
} else if (strcmp(option_string, "no-boot") == 0) {
image->bootable = ET_NOT_BOOTABLE;
} else if (strcmp(option_string, "hard-disk-boot") == 0) {
image->targetMode = ET_MEDIA_HDD;
} else if (strcmp(option_string, "boot-load-segment") == 0) {
image->loadSegment = strtoul(value, &eptr, 16);
if (eptr == value || *eptr != '\0' || errno != ERANGE) {
warn("%s: strtoul", __func__);
return 0;
}
} else if (strcmp(option_string, "platformid") == 0) {
if (strcmp(value, "efi") == 0)
image->platform_id = ET_SYS_EFI;
else {
warn("%s: unknown platform: %s", __func__, value);
return 0;
}
} else {
return 0;
}
return 1;
}
static struct boot_catalog_entry *
cd9660_init_boot_catalog_entry(void)
{
return ecalloc(1, sizeof(struct boot_catalog_entry));
}
static struct boot_catalog_entry *
cd9660_boot_setup_validation_entry(char sys)
{
struct boot_catalog_entry *entry;
boot_catalog_validation_entry *ve;
int16_t checksum;
unsigned char *csptr;
size_t i;
entry = cd9660_init_boot_catalog_entry();
entry->entry_type = ET_ENTRY_VE;
ve = &entry->entry_data.VE;
ve->header_id[0] = 1;
ve->platform_id[0] = sys;
ve->key[0] = 0x55;
ve->key[1] = 0xAA;
checksum = 0;
cd9660_721(0, ve->checksum);
csptr = (unsigned char*)ve;
for (i = 0; i < sizeof(*ve); i += 2) {
checksum += (int16_t)csptr[i];
checksum += 256 * (int16_t)csptr[i + 1];
}
checksum = -checksum;
cd9660_721(checksum, ve->checksum);
ELTORITO_DPRINTF(("%s: header_id %d, platform_id %d, key[0] %d, key[1] %d, "
"checksum %04x\n", __func__, ve->header_id[0], ve->platform_id[0],
ve->key[0], ve->key[1], checksum));
return entry;
}
static struct boot_catalog_entry *
cd9660_boot_setup_default_entry(struct cd9660_boot_image *disk)
{
struct boot_catalog_entry *default_entry;
boot_catalog_initial_entry *ie;
default_entry = cd9660_init_boot_catalog_entry();
if (default_entry == NULL)
return NULL;
default_entry->entry_type = ET_ENTRY_IE;
ie = &default_entry->entry_data.IE;
ie->boot_indicator[0] = disk->bootable;
ie->media_type[0] = disk->targetMode;
cd9660_721(disk->loadSegment, ie->load_segment);
ie->system_type[0] = disk->system;
cd9660_721(disk->num_sectors, ie->sector_count);
cd9660_731(disk->sector, ie->load_rba);
ELTORITO_DPRINTF(("%s: boot indicator %d, media type %d, "
"load segment %04x, system type %d, sector count %d, "
"load rba %d\n", __func__, ie->boot_indicator[0],
ie->media_type[0], disk->loadSegment, ie->system_type[0],
disk->num_sectors, disk->sector));
return default_entry;
}
static struct boot_catalog_entry *
cd9660_boot_setup_section_head(char platform)
{
struct boot_catalog_entry *entry;
boot_catalog_section_header *sh;
entry = cd9660_init_boot_catalog_entry();
if (entry == NULL)
return NULL;
entry->entry_type = ET_ENTRY_SH;
sh = &entry->entry_data.SH;
sh->header_indicator[0] = ET_SECTION_HEADER_MORE;
sh->platform_id[0] = platform;
sh->num_section_entries[0] = 0;
return entry;
}
static struct boot_catalog_entry *
cd9660_boot_setup_section_entry(struct cd9660_boot_image *disk)
{
struct boot_catalog_entry *entry;
boot_catalog_section_entry *se;
if ((entry = cd9660_init_boot_catalog_entry()) == NULL)
return NULL;
entry->entry_type = ET_ENTRY_SE;
se = &entry->entry_data.SE;
se->boot_indicator[0] = ET_BOOTABLE;
se->media_type[0] = disk->targetMode;
cd9660_721(disk->loadSegment, se->load_segment);
cd9660_721(disk->num_sectors, se->sector_count);
cd9660_731(disk->sector, se->load_rba);
return entry;
}
#if 0
static u_char
cd9660_boot_get_system_type(struct cd9660_boot_image *disk)
{
return 0;
}
#endif
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;
uint8_t system;
headp = NULL;
x86_head = mac_head = ppc_head = efi_head = NULL;
if (TAILQ_EMPTY(&diskStructure->boot_images))
return 0;
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);
system = default_boot_image != NULL ? default_boot_image->system :
ET_SYS_X86;
valid_entry = cd9660_boot_setup_validation_entry(system);
if (valid_entry == NULL)
return -1;
num_entries = 1;
used_sectors = 0;
TAILQ_FOREACH(tmp_disk, &diskStructure->boot_images, image_list) {
used_sectors += tmp_disk->num_sectors;
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);
}
sector = first_sector + catalog_sectors;
TAILQ_FOREACH(tmp_disk, &diskStructure->boot_images, image_list) {
tmp_disk->sector = sector;
sector += tmp_disk->num_sectors /
(diskStructure->sectorSize / 512);
}
LIST_INSERT_HEAD(&diskStructure->boot_entries, valid_entry, ll_struct);
if (default_boot_image != NULL) {
struct cd9660_boot_image *tcbi;
TAILQ_FOREACH(tcbi, &diskStructure->boot_images, image_list) {
if (tcbi == default_boot_image) {
tmp_disk = tcbi;
break;
}
}
}
if (tmp_disk == NULL)
tmp_disk = TAILQ_FIRST(&diskStructure->boot_images);
default_entry = cd9660_boot_setup_default_entry(tmp_disk);
if (default_entry == NULL) {
warnx("Error: memory allocation failed in cd9660_setup_boot");
return -1;
}
LIST_INSERT_AFTER(valid_entry, default_entry, ll_struct);
tmp_disk = TAILQ_FIRST(&diskStructure->boot_images);
head = NULL;
temp = default_entry;
for (; tmp_disk != NULL; tmp_disk = TAILQ_NEXT(tmp_disk, image_list)) {
if (tmp_disk == default_boot_image)
continue;
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;
head->entry_data.SH.num_section_entries[0]++;
temp = cd9660_boot_setup_section_entry(tmp_disk);
if (temp == NULL) {
warn("%s: cd9660_boot_setup_section_entry", __func__);
return -1;
}
while ((next = LIST_NEXT(head, ll_struct)) != NULL &&
next->entry_type == ET_ENTRY_SE)
head = next;
LIST_INSERT_AFTER(head, temp, ll_struct);
}
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;
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;
bvdData->boot_record_indicator[0] = ISO_VOLUME_DESCRIPTOR_BOOT;
memcpy(bvdData->identifier, ISO_VOLUME_DESCRIPTOR_STANDARD_ID, 5);
bvdData->version[0] = 1;
memcpy(bvdData->boot_system_identifier, ET_ID, 23);
memcpy(bvdData->identifier, ISO_VOLUME_DESCRIPTOR_STANDARD_ID, 5);
diskStructure->boot_descriptor =
(boot_volume_descriptor*) bvd->volumeDescriptorData;
return 1;
}
static int
cd9660_write_mbr_partition_entry(FILE *fd, int idx, off_t sector_start,
off_t nsectors, int type)
{
uint8_t val;
uint32_t lba;
if (fseeko(fd, (off_t)(idx) * 16 + 0x1be, SEEK_SET) == -1)
err(1, "fseeko");
val = 0x80;
fwrite(&val, sizeof(val), 1, fd);
val = 0xff;
fwrite(&val, sizeof(val), 1, fd);
fwrite(&val, sizeof(val), 1, fd);
fwrite(&val, sizeof(val), 1, fd);
val = type;
fwrite(&val, sizeof(val), 1, fd);
val = 0xff;
fwrite(&val, sizeof(val), 1, fd);
fwrite(&val, sizeof(val), 1, fd);
fwrite(&val, sizeof(val), 1, fd);
lba = htole32(sector_start);
fwrite(&lba, sizeof(lba), 1, fd);
lba = htole32(nsectors);
fwrite(&lba, sizeof(lba), 1, fd);
return 0;
}
static int
cd9660_write_apm_partition_entry(FILE *fd, int idx, int total_partitions,
off_t sector_start, off_t nsectors, off_t sector_size,
const char *part_name, const char *part_type)
{
uint32_t apm32, part_status;
uint16_t apm16;
part_status = APPLE_PS_VALID | APPLE_PS_ALLOCATED | APPLE_PS_READABLE |
APPLE_PS_WRITABLE;
if (fseeko(fd, (off_t)(idx + 1) * sector_size, SEEK_SET) == -1)
err(1, "fseeko");
apm16 = htobe16(0x504d);
fwrite(&apm16, sizeof(apm16), 1, fd);
apm16 = 0;
fwrite(&apm16, sizeof(apm16), 1, fd);
apm32 = htobe32(total_partitions);
fwrite(&apm32, sizeof(apm32), 1, fd);
apm32 = htobe32(sector_start);
fwrite(&apm32, sizeof(apm32), 1, fd);
apm32 = htobe32(nsectors);
fwrite(&apm32, sizeof(apm32), 1, fd);
fwrite(part_name, strlen(part_name) + 1, 1, fd);
fseek(fd, 32 - strlen(part_name) - 1, SEEK_CUR);
fwrite(part_type, strlen(part_type) + 1, 1, fd);
fseek(fd, 32 - strlen(part_type) - 1, SEEK_CUR);
apm32 = 0;
fwrite(&apm32, sizeof(apm32), 1, fd);
apm32 = htobe32(nsectors);
fwrite(&apm32, sizeof(apm32), 1, fd);
apm32 = htobe32(part_status);
fwrite(&apm32, sizeof(apm32), 1, fd);
return 0;
}
int
cd9660_write_boot(iso9660_disk *diskStructure, FILE *fd)
{
struct boot_catalog_entry *e;
struct cd9660_boot_image *t;
int apm_partitions = 0;
int mbr_partitions = 0;
if (fseeko(fd, (off_t)diskStructure->boot_catalog_sector *
diskStructure->sectorSize, SEEK_SET) == -1)
err(1, "fseeko");
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);
}
fwrite(&(e->entry_data.VE), 1, 32, fd);
}
if (diskStructure->verbose_level > 0)
printf("Finished writing boot catalog\n");
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++;
}
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;
if (diskStructure->chrp_boot)
cd9660_write_mbr_partition_entry(fd, mbr_partitions++,
0, diskStructure->totalSectors *
(diskStructure->sectorSize / 512), 0x96);
TAILQ_FOREACH(t, &diskStructure->boot_images, image_list) {
if (t->system != ET_SYS_PPC)
continue;
cd9660_write_mbr_partition_entry(fd, mbr_partitions++,
t->sector * (diskStructure->sectorSize / 512),
t->num_sectors * (diskStructure->sectorSize / 512),
0x41 );
}
}
if (apm_partitions > 0) {
uint32_t apm32;
uint16_t apm16;
int total_parts;
fseek(fd, 0, SEEK_SET);
apm16 = htobe16(0x4552);
fwrite(&apm16, sizeof(apm16), 1, fd);
apm16 = htobe16(512);
fwrite(&apm16, sizeof(apm16), 1, fd);
apm32 = htobe32(diskStructure->totalSectors *
(diskStructure->sectorSize / 512));
fwrite(&apm32, sizeof(apm32), 1, fd);
apm16 = htobe16(1);
fwrite(&apm16, sizeof(apm16), 1, fd);
fwrite(&apm16, sizeof(apm16), 1, fd);
total_parts = 2 + apm_partitions;
cd9660_write_apm_partition_entry(fd, 0, total_parts, 1,
total_parts, 512, "Apple", "Apple_partition_map");
apm_partitions = 0;
TAILQ_FOREACH(t, &diskStructure->boot_images, image_list) {
if (t->system != ET_SYS_MAC)
continue;
cd9660_write_apm_partition_entry(fd,
1 + apm_partitions++, total_parts,
t->sector * (diskStructure->sectorSize / 512),
t->num_sectors * (diskStructure->sectorSize / 512),
512, "CD Boot", "Apple_Bootstrap");
}
cd9660_write_apm_partition_entry(fd, 2 + apm_partitions,
total_parts, 0, diskStructure->totalSectors *
(diskStructure->sectorSize / 512), 512, "ISO9660",
"CD_ROM_Mode_1");
}
return 0;
}