1 /* $NetBSD: cd9660_eltorito.c,v 1.23 2018/03/28 06:48:55 nonaka Exp $ */
4 * SPDX-License-Identifier: BSD-2-Clause-NetBSD
6 * Copyright (c) 2005 Daniel Watt, Walter Deignan, Ryan Gabrys, Alan
7 * Perez-Rathke and Ram Vedam. All rights reserved.
9 * This code was written by Daniel Watt, Walter Deignan, Ryan Gabrys,
10 * Alan Perez-Rathke and Ram Vedam.
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above
18 * copyright notice, this list of conditions and the following
19 * disclaimer in the documentation and/or other materials provided
20 * with the distribution.
22 * THIS SOFTWARE IS PROVIDED BY DANIEL WATT, WALTER DEIGNAN, RYAN
23 * GABRYS, ALAN PEREZ-RATHKE AND RAM VEDAM ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 * DISCLAIMED. IN NO EVENT SHALL DANIEL WATT, WALTER DEIGNAN, RYAN
27 * GABRYS, ALAN PEREZ-RATHKE AND RAM VEDAM BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
30 * USE,DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
38 #include "cd9660_eltorito.h"
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
45 #define ELTORITO_DPRINTF(__x) printf __x
47 #define ELTORITO_DPRINTF(__x)
52 static struct boot_catalog_entry *cd9660_init_boot_catalog_entry(void);
53 static struct boot_catalog_entry *cd9660_boot_setup_validation_entry(char);
54 static struct boot_catalog_entry *cd9660_boot_setup_default_entry(
55 struct cd9660_boot_image *);
56 static struct boot_catalog_entry *cd9660_boot_setup_section_head(char);
58 static u_char cd9660_boot_get_system_type(struct cd9660_boot_image *);
61 static struct cd9660_boot_image *default_boot_image;
64 cd9660_add_boot_disk(iso9660_disk *diskStructure, const char *boot_info)
71 struct cd9660_boot_image *new_image, *tmp_image;
73 assert(boot_info != NULL);
75 if (*boot_info == '\0') {
76 warnx("Error: Boot disk information must be in the "
77 "format 'system;filename'");
81 /* First decode the boot information */
82 temp = estrdup(boot_info);
85 filename = strchr(sysname, ';');
86 if (filename == NULL) {
87 warnx("supply boot disk information in the format "
95 if (diskStructure->verbose_level > 0) {
96 printf("Found bootdisk with system %s, and filename %s\n",
99 new_image = ecalloc(1, sizeof(*new_image));
100 new_image->loadSegment = 0; /* default for now */
103 if (strcmp(sysname, "i386") == 0)
104 new_image->system = ET_SYS_X86;
105 else if (strcmp(sysname, "powerpc") == 0)
106 new_image->system = ET_SYS_PPC;
107 else if (strcmp(sysname, "macppc") == 0 ||
108 strcmp(sysname, "mac68k") == 0)
109 new_image->system = ET_SYS_MAC;
111 warnx("boot disk system must be "
112 "i386, powerpc, macppc, or mac68k");
119 new_image->filename = estrdup(filename);
123 /* Get information about the file */
124 if (lstat(new_image->filename, &stbuf) == -1)
125 err(EXIT_FAILURE, "%s: lstat(\"%s\")", __func__,
126 new_image->filename);
128 switch (stbuf.st_size) {
130 new_image->targetMode = ET_MEDIA_144FDD;
131 mode_msg = "Assigned boot image to 1.44 emulation mode";
134 new_image->targetMode = ET_MEDIA_12FDD;
135 mode_msg = "Assigned boot image to 1.2 emulation mode";
138 new_image->targetMode = ET_MEDIA_288FDD;
139 mode_msg = "Assigned boot image to 2.88 emulation mode";
142 new_image->targetMode = ET_MEDIA_NOEM;
143 mode_msg = "Assigned boot image to no emulation mode";
147 if (diskStructure->verbose_level > 0)
148 printf("%s\n", mode_msg);
150 new_image->size = stbuf.st_size;
151 new_image->num_sectors =
152 howmany(new_image->size, diskStructure->sectorSize) *
153 howmany(diskStructure->sectorSize, 512);
154 if (diskStructure->verbose_level > 0) {
155 printf("New image has size %d, uses %d 512-byte sectors\n",
156 new_image->size, new_image->num_sectors);
158 new_image->sector = -1;
159 /* Bootable by default */
160 new_image->bootable = ET_BOOTABLE;
163 /* Group images for the same platform together. */
164 TAILQ_FOREACH(tmp_image, &diskStructure->boot_images, image_list) {
165 if (tmp_image->system != new_image->system)
169 if (tmp_image == NULL) {
170 TAILQ_INSERT_HEAD(&diskStructure->boot_images, new_image,
173 TAILQ_INSERT_BEFORE(tmp_image, new_image, image_list);
175 new_image->serialno = diskStructure->image_serialno++;
177 new_image->platform_id = new_image->system;
179 /* TODO : Need to do anything about the boot image in the tree? */
180 diskStructure->is_bootable = 1;
182 /* First boot image is initial/default entry. */
183 if (default_boot_image == NULL)
184 default_boot_image = new_image;
190 cd9660_eltorito_add_boot_option(iso9660_disk *diskStructure,
191 const char *option_string, const char *value)
194 struct cd9660_boot_image *image;
196 assert(option_string != NULL);
198 /* Find the last image added */
199 TAILQ_FOREACH(image, &diskStructure->boot_images, image_list) {
200 if (image->serialno + 1 == diskStructure->image_serialno)
204 errx(EXIT_FAILURE, "Attempted to add boot option, "
205 "but no boot images have been specified");
207 if (strcmp(option_string, "no-emul-boot") == 0) {
208 image->targetMode = ET_MEDIA_NOEM;
209 } else if (strcmp(option_string, "no-boot") == 0) {
210 image->bootable = ET_NOT_BOOTABLE;
211 } else if (strcmp(option_string, "hard-disk-boot") == 0) {
212 image->targetMode = ET_MEDIA_HDD;
213 } else if (strcmp(option_string, "boot-load-segment") == 0) {
214 image->loadSegment = strtoul(value, &eptr, 16);
215 if (eptr == value || *eptr != '\0' || errno != ERANGE) {
216 warn("%s: strtoul", __func__);
219 } else if (strcmp(option_string, "platformid") == 0) {
220 if (strcmp(value, "efi") == 0)
221 image->platform_id = ET_SYS_EFI;
223 warn("%s: unknown platform: %s", __func__, value);
232 static struct boot_catalog_entry *
233 cd9660_init_boot_catalog_entry(void)
235 return ecalloc(1, sizeof(struct boot_catalog_entry));
238 static struct boot_catalog_entry *
239 cd9660_boot_setup_validation_entry(char sys)
241 struct boot_catalog_entry *entry;
242 boot_catalog_validation_entry *ve;
244 unsigned char *csptr;
246 entry = cd9660_init_boot_catalog_entry();
248 entry->entry_type = ET_ENTRY_VE;
249 ve = &entry->entry_data.VE;
251 ve->header_id[0] = 1;
252 ve->platform_id[0] = sys;
256 /* Calculate checksum */
258 cd9660_721(0, ve->checksum);
259 csptr = (unsigned char*)ve;
260 for (i = 0; i < sizeof(*ve); i += 2) {
261 checksum += (int16_t)csptr[i];
262 checksum += 256 * (int16_t)csptr[i + 1];
264 checksum = -checksum;
265 cd9660_721(checksum, ve->checksum);
267 ELTORITO_DPRINTF(("%s: header_id %d, platform_id %d, key[0] %d, key[1] %d, "
268 "checksum %04x\n", __func__, ve->header_id[0], ve->platform_id[0],
269 ve->key[0], ve->key[1], checksum));
273 static struct boot_catalog_entry *
274 cd9660_boot_setup_default_entry(struct cd9660_boot_image *disk)
276 struct boot_catalog_entry *default_entry;
277 boot_catalog_initial_entry *ie;
279 default_entry = cd9660_init_boot_catalog_entry();
280 if (default_entry == NULL)
283 default_entry->entry_type = ET_ENTRY_IE;
284 ie = &default_entry->entry_data.IE;
286 ie->boot_indicator[0] = disk->bootable;
287 ie->media_type[0] = disk->targetMode;
288 cd9660_721(disk->loadSegment, ie->load_segment);
289 ie->system_type[0] = disk->system;
290 cd9660_721(disk->num_sectors, ie->sector_count);
291 cd9660_731(disk->sector, ie->load_rba);
293 ELTORITO_DPRINTF(("%s: boot indicator %d, media type %d, "
294 "load segment %04x, system type %d, sector count %d, "
295 "load rba %d\n", __func__, ie->boot_indicator[0],
296 ie->media_type[0], disk->loadSegment, ie->system_type[0],
297 disk->num_sectors, disk->sector));
298 return default_entry;
301 static struct boot_catalog_entry *
302 cd9660_boot_setup_section_head(char platform)
304 struct boot_catalog_entry *entry;
305 boot_catalog_section_header *sh;
307 entry = cd9660_init_boot_catalog_entry();
311 entry->entry_type = ET_ENTRY_SH;
312 sh = &entry->entry_data.SH;
315 * The last one will manually be set to ET_SECTION_HEADER_LAST
317 sh->header_indicator[0] = ET_SECTION_HEADER_MORE;
318 sh->platform_id[0] = platform;
319 sh->num_section_entries[0] = 0;
323 static struct boot_catalog_entry *
324 cd9660_boot_setup_section_entry(struct cd9660_boot_image *disk)
326 struct boot_catalog_entry *entry;
327 boot_catalog_section_entry *se;
328 if ((entry = cd9660_init_boot_catalog_entry()) == NULL)
331 entry->entry_type = ET_ENTRY_SE;
332 se = &entry->entry_data.SE;
334 se->boot_indicator[0] = ET_BOOTABLE;
335 se->media_type[0] = disk->targetMode;
336 cd9660_721(disk->loadSegment, se->load_segment);
337 cd9660_721(disk->num_sectors, se->sector_count);
338 cd9660_731(disk->sector, se->load_rba);
344 cd9660_boot_get_system_type(struct cd9660_boot_image *disk)
347 For hard drive booting, we need to examine the MBR to figure
348 out what the partition type is
355 * Set up the BVD, Boot catalog, and the boot entries, but do no writing
358 cd9660_setup_boot(iso9660_disk *diskStructure, int first_sector)
364 struct boot_catalog_entry *x86_head, *mac_head, *ppc_head, *efi_head,
365 *valid_entry, *default_entry, *temp, *head, **headp, *next;
366 struct cd9660_boot_image *tmp_disk;
369 x86_head = mac_head = ppc_head = efi_head = NULL;
371 /* If there are no boot disks, don't bother building boot information */
372 if (TAILQ_EMPTY(&diskStructure->boot_images))
375 /* Point to catalog: For now assume it consumes one sector */
376 ELTORITO_DPRINTF(("Boot catalog will go in sector %d\n", first_sector));
377 diskStructure->boot_catalog_sector = first_sector;
378 cd9660_bothendian_dword(first_sector,
379 diskStructure->boot_descriptor->boot_catalog_pointer);
381 /* Step 1: Generate boot catalog */
382 /* Step 1a: Validation entry */
383 valid_entry = cd9660_boot_setup_validation_entry(ET_SYS_X86);
384 if (valid_entry == NULL)
388 * Count how many boot images there are,
389 * and how many sectors they consume.
394 TAILQ_FOREACH(tmp_disk, &diskStructure->boot_images, image_list) {
395 used_sectors += tmp_disk->num_sectors;
397 /* One default entry per image */
400 catalog_sectors = howmany(num_entries * 0x20, diskStructure->sectorSize);
401 used_sectors += catalog_sectors;
403 if (diskStructure->verbose_level > 0) {
404 printf("%s: there will be %i entries consuming %i sectors. "
405 "Catalog is %i sectors\n", __func__, num_entries,
406 used_sectors, catalog_sectors);
409 /* Populate sector numbers */
410 sector = first_sector + catalog_sectors;
411 TAILQ_FOREACH(tmp_disk, &diskStructure->boot_images, image_list) {
412 tmp_disk->sector = sector;
413 sector += tmp_disk->num_sectors /
414 (diskStructure->sectorSize / 512);
417 LIST_INSERT_HEAD(&diskStructure->boot_entries, valid_entry, ll_struct);
419 /* Step 1b: Initial/default entry */
421 if (default_boot_image != NULL) {
422 struct cd9660_boot_image *tcbi;
423 TAILQ_FOREACH(tcbi, &diskStructure->boot_images, image_list) {
424 if (tcbi == default_boot_image) {
430 if (tmp_disk == NULL)
431 tmp_disk = TAILQ_FIRST(&diskStructure->boot_images);
432 default_entry = cd9660_boot_setup_default_entry(tmp_disk);
433 if (default_entry == NULL) {
434 warnx("Error: memory allocation failed in cd9660_setup_boot");
438 LIST_INSERT_AFTER(valid_entry, default_entry, ll_struct);
440 /* Todo: multiple default entries? */
442 tmp_disk = TAILQ_FIRST(&diskStructure->boot_images);
445 temp = default_entry;
447 /* If multiple boot images are given : */
448 for (; tmp_disk != NULL; tmp_disk = TAILQ_NEXT(tmp_disk, image_list)) {
449 if (tmp_disk == default_boot_image)
452 /* Step 2: Section header */
453 switch (tmp_disk->platform_id) {
467 warnx("%s: internal error: unknown system type",
472 if (*headp == NULL) {
474 cd9660_boot_setup_section_head(tmp_disk->platform_id);
476 warnx("Error: memory allocation failed in "
477 "cd9660_setup_boot");
480 LIST_INSERT_AFTER(default_entry, head, ll_struct);
485 head->entry_data.SH.num_section_entries[0]++;
487 /* Step 2a: Section entry and extensions */
488 temp = cd9660_boot_setup_section_entry(tmp_disk);
490 warn("%s: cd9660_boot_setup_section_entry", __func__);
494 while ((next = LIST_NEXT(head, ll_struct)) != NULL &&
495 next->entry_type == ET_ENTRY_SE)
498 LIST_INSERT_AFTER(head, temp, ll_struct);
501 /* Find the last Section Header entry and mark it as the last. */
503 LIST_FOREACH(next, &diskStructure->boot_entries, ll_struct) {
504 if (next->entry_type == ET_ENTRY_SH)
508 head->entry_data.SH.header_indicator[0] = ET_SECTION_HEADER_LAST;
510 /* TODO: Remaining boot disks when implemented */
512 return first_sector + used_sectors;
516 cd9660_setup_boot_volume_descriptor(iso9660_disk *diskStructure,
517 volume_descriptor *bvd)
519 boot_volume_descriptor *bvdData =
520 (boot_volume_descriptor*)bvd->volumeDescriptorData;
522 bvdData->boot_record_indicator[0] = ISO_VOLUME_DESCRIPTOR_BOOT;
523 memcpy(bvdData->identifier, ISO_VOLUME_DESCRIPTOR_STANDARD_ID, 5);
524 bvdData->version[0] = 1;
525 memcpy(bvdData->boot_system_identifier, ET_ID, 23);
526 memcpy(bvdData->identifier, ISO_VOLUME_DESCRIPTOR_STANDARD_ID, 5);
527 diskStructure->boot_descriptor =
528 (boot_volume_descriptor*) bvd->volumeDescriptorData;
533 cd9660_write_mbr_partition_entry(FILE *fd, int idx, off_t sector_start,
534 off_t nsectors, int type)
539 if (fseeko(fd, (off_t)(idx) * 16 + 0x1be, SEEK_SET) == -1)
542 val = 0x80; /* Bootable */
543 fwrite(&val, sizeof(val), 1, fd);
545 val = 0xff; /* CHS begin */
546 fwrite(&val, sizeof(val), 1, fd);
547 fwrite(&val, sizeof(val), 1, fd);
548 fwrite(&val, sizeof(val), 1, fd);
550 val = type; /* Part type */
551 fwrite(&val, sizeof(val), 1, fd);
553 val = 0xff; /* CHS end */
554 fwrite(&val, sizeof(val), 1, fd);
555 fwrite(&val, sizeof(val), 1, fd);
556 fwrite(&val, sizeof(val), 1, fd);
559 lba = htole32(sector_start);
560 fwrite(&lba, sizeof(lba), 1, fd);
561 lba = htole32(nsectors);
562 fwrite(&lba, sizeof(lba), 1, fd);
568 cd9660_write_apm_partition_entry(FILE *fd, int idx, int total_partitions,
569 off_t sector_start, off_t nsectors, off_t sector_size,
570 const char *part_name, const char *part_type)
572 uint32_t apm32, part_status;
575 /* See Apple Tech Note 1189 for the details about the pmPartStatus
577 * Below the flags which are default:
583 part_status = 0x01 | 0x02 | 0x10 | 0x20;
585 if (fseeko(fd, (off_t)(idx + 1) * sector_size, SEEK_SET) == -1)
589 apm16 = htobe16(0x504d);
590 fwrite(&apm16, sizeof(apm16), 1, fd);
592 fwrite(&apm16, sizeof(apm16), 1, fd);
594 /* Total number of partitions */
595 apm32 = htobe32(total_partitions);
596 fwrite(&apm32, sizeof(apm32), 1, fd);
598 apm32 = htobe32(sector_start);
599 fwrite(&apm32, sizeof(apm32), 1, fd);
600 apm32 = htobe32(nsectors);
601 fwrite(&apm32, sizeof(apm32), 1, fd);
603 fwrite(part_name, strlen(part_name) + 1, 1, fd);
604 fseek(fd, 32 - strlen(part_name) - 1, SEEK_CUR);
605 fwrite(part_type, strlen(part_type) + 1, 1, fd);
606 fseek(fd, 32 - strlen(part_type) - 1, SEEK_CUR);
610 fwrite(&apm32, sizeof(apm32), 1, fd);
612 apm32 = htobe32(nsectors);
613 fwrite(&apm32, sizeof(apm32), 1, fd);
615 apm32 = htobe32(part_status);
616 fwrite(&apm32, sizeof(apm32), 1, fd);
622 cd9660_write_boot(iso9660_disk *diskStructure, FILE *fd)
624 struct boot_catalog_entry *e;
625 struct cd9660_boot_image *t;
626 int apm_partitions = 0;
627 int mbr_partitions = 0;
629 /* write boot catalog */
630 if (fseeko(fd, (off_t)diskStructure->boot_catalog_sector *
631 diskStructure->sectorSize, SEEK_SET) == -1)
634 if (diskStructure->verbose_level > 0) {
635 printf("Writing boot catalog to sector %" PRId64 "\n",
636 diskStructure->boot_catalog_sector);
638 LIST_FOREACH(e, &diskStructure->boot_entries, ll_struct) {
639 if (diskStructure->verbose_level > 0) {
640 printf("Writing catalog entry of type %d\n",
644 * It doesn't matter which one gets written
645 * since they are the same size
647 fwrite(&(e->entry_data.VE), 1, 32, fd);
649 if (diskStructure->verbose_level > 0)
650 printf("Finished writing boot catalog\n");
652 /* copy boot images */
653 TAILQ_FOREACH(t, &diskStructure->boot_images, image_list) {
654 if (diskStructure->verbose_level > 0) {
655 printf("Writing boot image from %s to sectors %d\n",
656 t->filename, t->sector);
658 cd9660_copy_file(diskStructure, fd, t->sector, t->filename);
660 if (t->system == ET_SYS_MAC)
662 if (t->system == ET_SYS_PPC)
666 /* some systems need partition tables as well */
667 if (mbr_partitions > 0 || diskStructure->chrp_boot) {
670 fseek(fd, 0x1fe, SEEK_SET);
671 sig = htole16(0xaa55);
672 fwrite(&sig, sizeof(sig), 1, fd);
676 /* Write ISO9660 descriptor, enclosing the whole disk */
677 if (diskStructure->chrp_boot)
678 cd9660_write_mbr_partition_entry(fd, mbr_partitions++,
679 0, diskStructure->totalSectors *
680 (diskStructure->sectorSize / 512), 0x96);
682 /* Write all partition entries */
683 TAILQ_FOREACH(t, &diskStructure->boot_images, image_list) {
684 if (t->system != ET_SYS_PPC)
686 cd9660_write_mbr_partition_entry(fd, mbr_partitions++,
687 t->sector * (diskStructure->sectorSize / 512),
688 t->num_sectors * (diskStructure->sectorSize / 512),
689 0x41 /* PReP Boot */);
693 if (apm_partitions > 0) {
694 /* Write DDR and global APM info */
699 fseek(fd, 0, SEEK_SET);
700 apm16 = htobe16(0x4552);
701 fwrite(&apm16, sizeof(apm16), 1, fd);
702 /* Device block size */
703 apm16 = htobe16(512);
704 fwrite(&apm16, sizeof(apm16), 1, fd);
705 /* Device block count */
706 apm32 = htobe32(diskStructure->totalSectors *
707 (diskStructure->sectorSize / 512));
708 fwrite(&apm32, sizeof(apm32), 1, fd);
711 fwrite(&apm16, sizeof(apm16), 1, fd);
712 fwrite(&apm16, sizeof(apm16), 1, fd);
714 /* Count total needed entries */
715 total_parts = 2 + apm_partitions; /* Self + ISO9660 */
717 /* Write self-descriptor */
718 cd9660_write_apm_partition_entry(fd, 0, total_parts, 1,
719 total_parts, 512, "Apple", "Apple_partition_map");
721 /* Write all partition entries */
723 TAILQ_FOREACH(t, &diskStructure->boot_images, image_list) {
724 if (t->system != ET_SYS_MAC)
727 cd9660_write_apm_partition_entry(fd,
728 1 + apm_partitions++, total_parts,
729 t->sector * (diskStructure->sectorSize / 512),
730 t->num_sectors * (diskStructure->sectorSize / 512),
731 512, "CD Boot", "Apple_Bootstrap");
734 /* Write ISO9660 descriptor, enclosing the whole disk */
735 cd9660_write_apm_partition_entry(fd, 2 + apm_partitions,
736 total_parts, 0, diskStructure->totalSectors *
737 (diskStructure->sectorSize / 512), 512, "ISO9660",