1 /* $NetBSD: cd9660_eltorito.c,v 1.17 2011/06/23 02:35:56 enami 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)
50 static struct boot_catalog_entry *cd9660_init_boot_catalog_entry(void);
51 static struct boot_catalog_entry *cd9660_boot_setup_validation_entry(char);
52 static struct boot_catalog_entry *cd9660_boot_setup_default_entry(
53 struct cd9660_boot_image *);
54 static struct boot_catalog_entry *cd9660_boot_setup_section_head(char);
55 static struct boot_catalog_entry *cd9660_boot_setup_validation_entry(char);
57 static u_char cd9660_boot_get_system_type(struct cd9660_boot_image *);
61 cd9660_add_boot_disk(iso9660_disk *diskStructure, const char *boot_info)
68 struct cd9660_boot_image *new_image, *tmp_image;
70 assert(boot_info != NULL);
72 if (*boot_info == '\0') {
73 warnx("Error: Boot disk information must be in the "
74 "format 'system;filename'");
78 /* First decode the boot information */
79 temp = estrdup(boot_info);
82 filename = strchr(sysname, ';');
83 if (filename == NULL) {
84 warnx("supply boot disk information in the format "
92 if (diskStructure->verbose_level > 0) {
93 printf("Found bootdisk with system %s, and filename %s\n",
96 new_image = ecalloc(1, sizeof(*new_image));
97 new_image->loadSegment = 0; /* default for now */
100 if (strcmp(sysname, "i386") == 0)
101 new_image->system = ET_SYS_X86;
102 else if (strcmp(sysname, "powerpc") == 0)
103 new_image->system = ET_SYS_PPC;
104 else if (strcmp(sysname, "macppc") == 0 ||
105 strcmp(sysname, "mac68k") == 0)
106 new_image->system = ET_SYS_MAC;
108 warnx("boot disk system must be "
109 "i386, powerpc, macppc, or mac68k");
116 new_image->filename = estrdup(filename);
120 /* Get information about the file */
121 if (lstat(new_image->filename, &stbuf) == -1)
122 err(EXIT_FAILURE, "%s: lstat(\"%s\")", __func__,
123 new_image->filename);
125 switch (stbuf.st_size) {
127 new_image->targetMode = ET_MEDIA_144FDD;
128 mode_msg = "Assigned boot image to 1.44 emulation mode";
131 new_image->targetMode = ET_MEDIA_12FDD;
132 mode_msg = "Assigned boot image to 1.2 emulation mode";
135 new_image->targetMode = ET_MEDIA_288FDD;
136 mode_msg = "Assigned boot image to 2.88 emulation mode";
139 new_image->targetMode = ET_MEDIA_NOEM;
140 mode_msg = "Assigned boot image to no emulation mode";
144 if (diskStructure->verbose_level > 0)
145 printf("%s\n", mode_msg);
147 new_image->size = stbuf.st_size;
148 new_image->num_sectors =
149 howmany(new_image->size, diskStructure->sectorSize) *
150 howmany(diskStructure->sectorSize, 512);
151 if (diskStructure->verbose_level > 0) {
152 printf("New image has size %d, uses %d 512-byte sectors\n",
153 new_image->size, new_image->num_sectors);
155 new_image->sector = -1;
156 /* Bootable by default */
157 new_image->bootable = ET_BOOTABLE;
160 /* Group images for the same platform together. */
161 TAILQ_FOREACH(tmp_image, &diskStructure->boot_images, image_list) {
162 if (tmp_image->system != new_image->system)
166 if (tmp_image == NULL) {
167 TAILQ_INSERT_HEAD(&diskStructure->boot_images, new_image,
170 TAILQ_INSERT_BEFORE(tmp_image, new_image, image_list);
172 new_image->serialno = diskStructure->image_serialno++;
174 /* TODO : Need to do anything about the boot image in the tree? */
175 diskStructure->is_bootable = 1;
181 cd9660_eltorito_add_boot_option(iso9660_disk *diskStructure,
182 const char *option_string, const char *value)
185 struct cd9660_boot_image *image;
187 assert(option_string != NULL);
189 /* Find the last image added */
190 TAILQ_FOREACH(image, &diskStructure->boot_images, image_list) {
191 if (image->serialno + 1 == diskStructure->image_serialno)
195 errx(EXIT_FAILURE, "Attempted to add boot option, "
196 "but no boot images have been specified");
198 if (strcmp(option_string, "no-emul-boot") == 0) {
199 image->targetMode = ET_MEDIA_NOEM;
200 } else if (strcmp(option_string, "no-boot") == 0) {
201 image->bootable = ET_NOT_BOOTABLE;
202 } else if (strcmp(option_string, "hard-disk-boot") == 0) {
203 image->targetMode = ET_MEDIA_HDD;
204 } else if (strcmp(option_string, "boot-load-segment") == 0) {
205 image->loadSegment = strtoul(value, &eptr, 16);
206 if (eptr == value || *eptr != '\0' || errno != ERANGE) {
207 warn("%s: strtoul", __func__);
216 static struct boot_catalog_entry *
217 cd9660_init_boot_catalog_entry(void)
219 return ecalloc(1, sizeof(struct boot_catalog_entry));
222 static struct boot_catalog_entry *
223 cd9660_boot_setup_validation_entry(char sys)
225 struct boot_catalog_entry *entry;
226 boot_catalog_validation_entry *ve;
228 unsigned char *csptr;
230 entry = cd9660_init_boot_catalog_entry();
232 ve = &entry->entry_data.VE;
234 ve->header_id[0] = 1;
235 ve->platform_id[0] = sys;
239 /* Calculate checksum */
241 cd9660_721(0, ve->checksum);
242 csptr = (unsigned char*)ve;
243 for (i = 0; i < sizeof(*ve); i += 2) {
244 checksum += (int16_t)csptr[i];
245 checksum += 256 * (int16_t)csptr[i + 1];
247 checksum = -checksum;
248 cd9660_721(checksum, ve->checksum);
250 ELTORITO_DPRINTF(("%s: header_id %d, platform_id %d, key[0] %d, key[1] %d, "
251 "checksum %04x\n", __func__, ve->header_id[0], ve->platform_id[0],
252 ve->key[0], ve->key[1], checksum));
256 static struct boot_catalog_entry *
257 cd9660_boot_setup_default_entry(struct cd9660_boot_image *disk)
259 struct boot_catalog_entry *default_entry;
260 boot_catalog_initial_entry *ie;
262 default_entry = cd9660_init_boot_catalog_entry();
263 if (default_entry == NULL)
266 ie = &default_entry->entry_data.IE;
268 ie->boot_indicator[0] = disk->bootable;
269 ie->media_type[0] = disk->targetMode;
270 cd9660_721(disk->loadSegment, ie->load_segment);
271 ie->system_type[0] = disk->system;
272 cd9660_721(disk->num_sectors, ie->sector_count);
273 cd9660_731(disk->sector, ie->load_rba);
275 ELTORITO_DPRINTF(("%s: boot indicator %d, media type %d, "
276 "load segment %04x, system type %d, sector count %d, "
277 "load rba %d\n", __func__, ie->boot_indicator[0],
278 ie->media_type[0], disk->loadSegment, ie->system_type[0],
279 disk->num_sectors, disk->sector));
280 return default_entry;
283 static struct boot_catalog_entry *
284 cd9660_boot_setup_section_head(char platform)
286 struct boot_catalog_entry *entry;
287 boot_catalog_section_header *sh;
289 entry = cd9660_init_boot_catalog_entry();
293 sh = &entry->entry_data.SH;
294 /* More by default. The last one will manually be set to 0x91 */
295 sh->header_indicator[0] = ET_SECTION_HEADER_MORE;
296 sh->platform_id[0] = platform;
297 sh->num_section_entries[0] = 0;
301 static struct boot_catalog_entry *
302 cd9660_boot_setup_section_entry(struct cd9660_boot_image *disk)
304 struct boot_catalog_entry *entry;
305 boot_catalog_section_entry *se;
306 if ((entry = cd9660_init_boot_catalog_entry()) == NULL)
309 se = &entry->entry_data.SE;
311 se->boot_indicator[0] = ET_BOOTABLE;
312 se->media_type[0] = disk->targetMode;
313 cd9660_721(disk->loadSegment, se->load_segment);
314 cd9660_721(disk->num_sectors, se->sector_count);
315 cd9660_731(disk->sector, se->load_rba);
321 cd9660_boot_get_system_type(struct cd9660_boot_image *disk)
324 For hard drive booting, we need to examine the MBR to figure
325 out what the partition type is
332 * Set up the BVD, Boot catalog, and the boot entries, but do no writing
335 cd9660_setup_boot(iso9660_disk *diskStructure, int first_sector)
341 struct boot_catalog_entry *x86_head, *mac_head, *ppc_head,
342 *valid_entry, *default_entry, *temp, *head, **headp, *next;
343 struct cd9660_boot_image *tmp_disk;
346 x86_head = mac_head = ppc_head = NULL;
348 /* If there are no boot disks, don't bother building boot information */
349 if (TAILQ_EMPTY(&diskStructure->boot_images))
352 /* Point to catalog: For now assume it consumes one sector */
353 ELTORITO_DPRINTF(("Boot catalog will go in sector %d\n", first_sector));
354 diskStructure->boot_catalog_sector = first_sector;
355 cd9660_bothendian_dword(first_sector,
356 diskStructure->boot_descriptor->boot_catalog_pointer);
358 /* Step 1: Generate boot catalog */
359 /* Step 1a: Validation entry */
360 valid_entry = cd9660_boot_setup_validation_entry(ET_SYS_X86);
361 if (valid_entry == NULL)
365 * Count how many boot images there are,
366 * and how many sectors they consume.
371 TAILQ_FOREACH(tmp_disk, &diskStructure->boot_images, image_list) {
372 used_sectors += tmp_disk->num_sectors;
374 /* One default entry per image */
377 catalog_sectors = howmany(num_entries * 0x20, diskStructure->sectorSize);
378 used_sectors += catalog_sectors;
380 if (diskStructure->verbose_level > 0) {
381 printf("%s: there will be %i entries consuming %i sectors. "
382 "Catalog is %i sectors\n", __func__, num_entries,
383 used_sectors, catalog_sectors);
386 /* Populate sector numbers */
387 sector = first_sector + catalog_sectors;
388 TAILQ_FOREACH(tmp_disk, &diskStructure->boot_images, image_list) {
389 tmp_disk->sector = sector;
390 sector += tmp_disk->num_sectors;
393 LIST_INSERT_HEAD(&diskStructure->boot_entries, valid_entry, ll_struct);
395 /* Step 1b: Initial/default entry */
397 tmp_disk = TAILQ_FIRST(&diskStructure->boot_images);
398 default_entry = cd9660_boot_setup_default_entry(tmp_disk);
399 if (default_entry == NULL) {
400 warnx("Error: memory allocation failed in cd9660_setup_boot");
404 LIST_INSERT_AFTER(valid_entry, default_entry, ll_struct);
406 /* Todo: multiple default entries? */
408 tmp_disk = TAILQ_NEXT(tmp_disk, image_list);
410 temp = default_entry;
412 /* If multiple boot images are given : */
413 while (tmp_disk != NULL) {
414 /* Step 2: Section header */
415 switch (tmp_disk->system) {
426 warnx("%s: internal error: unknown system type",
431 if (*headp == NULL) {
433 cd9660_boot_setup_section_head(tmp_disk->system);
435 warnx("Error: memory allocation failed in "
436 "cd9660_setup_boot");
439 LIST_INSERT_AFTER(default_entry, head, ll_struct);
444 head->entry_data.SH.num_section_entries[0]++;
446 /* Step 2a: Section entry and extensions */
447 temp = cd9660_boot_setup_section_entry(tmp_disk);
449 warn("%s: cd9660_boot_setup_section_entry", __func__);
453 while ((next = LIST_NEXT(head, ll_struct)) != NULL &&
454 next->entry_type == ET_ENTRY_SE)
457 LIST_INSERT_AFTER(head, temp, ll_struct);
458 tmp_disk = TAILQ_NEXT(tmp_disk, image_list);
461 /* TODO: Remaining boot disks when implemented */
463 return first_sector + used_sectors;
467 cd9660_setup_boot_volume_descriptor(iso9660_disk *diskStructure,
468 volume_descriptor *bvd)
470 boot_volume_descriptor *bvdData =
471 (boot_volume_descriptor*)bvd->volumeDescriptorData;
473 bvdData->boot_record_indicator[0] = ISO_VOLUME_DESCRIPTOR_BOOT;
474 memcpy(bvdData->identifier, ISO_VOLUME_DESCRIPTOR_STANDARD_ID, 5);
475 bvdData->version[0] = 1;
476 memcpy(bvdData->boot_system_identifier, ET_ID, 23);
477 memcpy(bvdData->identifier, ISO_VOLUME_DESCRIPTOR_STANDARD_ID, 5);
478 diskStructure->boot_descriptor =
479 (boot_volume_descriptor*) bvd->volumeDescriptorData;
484 cd9660_write_mbr_partition_entry(FILE *fd, int idx, off_t sector_start,
485 off_t nsectors, int type)
490 if (fseeko(fd, (off_t)(idx) * 16 + 0x1be, SEEK_SET) == -1)
493 val = 0x80; /* Bootable */
494 fwrite(&val, sizeof(val), 1, fd);
496 val = 0xff; /* CHS begin */
497 fwrite(&val, sizeof(val), 1, fd);
498 fwrite(&val, sizeof(val), 1, fd);
499 fwrite(&val, sizeof(val), 1, fd);
501 val = type; /* Part type */
502 fwrite(&val, sizeof(val), 1, fd);
504 val = 0xff; /* CHS end */
505 fwrite(&val, sizeof(val), 1, fd);
506 fwrite(&val, sizeof(val), 1, fd);
507 fwrite(&val, sizeof(val), 1, fd);
510 lba = htole32(sector_start);
511 fwrite(&lba, sizeof(lba), 1, fd);
512 lba = htole32(nsectors);
513 fwrite(&lba, sizeof(lba), 1, fd);
519 cd9660_write_apm_partition_entry(FILE *fd, int idx, int total_partitions,
520 off_t sector_start, off_t nsectors, off_t sector_size,
521 const char *part_name, const char *part_type)
523 uint32_t apm32, part_status;
526 /* See Apple Tech Note 1189 for the details about the pmPartStatus
528 * Below the flags which are default:
534 part_status = 0x01 | 0x02 | 0x10 | 0x20;
536 if (fseeko(fd, (off_t)(idx + 1) * sector_size, SEEK_SET) == -1)
540 apm16 = htobe16(0x504d);
541 fwrite(&apm16, sizeof(apm16), 1, fd);
543 fwrite(&apm16, sizeof(apm16), 1, fd);
545 /* Total number of partitions */
546 apm32 = htobe32(total_partitions);
547 fwrite(&apm32, sizeof(apm32), 1, fd);
549 apm32 = htobe32(sector_start);
550 fwrite(&apm32, sizeof(apm32), 1, fd);
551 apm32 = htobe32(nsectors);
552 fwrite(&apm32, sizeof(apm32), 1, fd);
554 fwrite(part_name, strlen(part_name) + 1, 1, fd);
555 fseek(fd, 32 - strlen(part_name) - 1, SEEK_CUR);
556 fwrite(part_type, strlen(part_type) + 1, 1, fd);
557 fseek(fd, 32 - strlen(part_type) - 1, SEEK_CUR);
561 fwrite(&apm32, sizeof(apm32), 1, fd);
563 apm32 = htobe32(nsectors);
564 fwrite(&apm32, sizeof(apm32), 1, fd);
566 apm32 = htobe32(part_status);
567 fwrite(&apm32, sizeof(apm32), 1, fd);
573 cd9660_write_boot(iso9660_disk *diskStructure, FILE *fd)
575 struct boot_catalog_entry *e;
576 struct cd9660_boot_image *t;
577 int apm_partitions = 0;
578 int mbr_partitions = 0;
580 /* write boot catalog */
581 if (fseeko(fd, (off_t)diskStructure->boot_catalog_sector *
582 diskStructure->sectorSize, SEEK_SET) == -1)
585 if (diskStructure->verbose_level > 0) {
586 printf("Writing boot catalog to sector %" PRId64 "\n",
587 diskStructure->boot_catalog_sector);
589 LIST_FOREACH(e, &diskStructure->boot_entries, ll_struct) {
590 if (diskStructure->verbose_level > 0) {
591 printf("Writing catalog entry of type %d\n",
595 * It doesn't matter which one gets written
596 * since they are the same size
598 fwrite(&(e->entry_data.VE), 1, 32, fd);
600 if (diskStructure->verbose_level > 0)
601 printf("Finished writing boot catalog\n");
603 /* copy boot images */
604 TAILQ_FOREACH(t, &diskStructure->boot_images, image_list) {
605 if (diskStructure->verbose_level > 0) {
606 printf("Writing boot image from %s to sectors %d\n",
607 t->filename, t->sector);
609 cd9660_copy_file(diskStructure, fd, t->sector, t->filename);
611 if (t->system == ET_SYS_MAC)
613 if (t->system == ET_SYS_PPC)
617 /* some systems need partition tables as well */
618 if (mbr_partitions > 0 || diskStructure->chrp_boot) {
621 fseek(fd, 0x1fe, SEEK_SET);
622 sig = htole16(0xaa55);
623 fwrite(&sig, sizeof(sig), 1, fd);
627 /* Write ISO9660 descriptor, enclosing the whole disk */
628 if (diskStructure->chrp_boot)
629 cd9660_write_mbr_partition_entry(fd, mbr_partitions++,
630 0, diskStructure->totalSectors *
631 (diskStructure->sectorSize / 512), 0x96);
633 /* Write all partition entries */
634 TAILQ_FOREACH(t, &diskStructure->boot_images, image_list) {
635 if (t->system != ET_SYS_PPC)
637 cd9660_write_mbr_partition_entry(fd, mbr_partitions++,
638 t->sector * (diskStructure->sectorSize / 512),
639 t->num_sectors * (diskStructure->sectorSize / 512),
640 0x41 /* PReP Boot */);
644 if (apm_partitions > 0) {
645 /* Write DDR and global APM info */
650 fseek(fd, 0, SEEK_SET);
651 apm16 = htobe16(0x4552);
652 fwrite(&apm16, sizeof(apm16), 1, fd);
653 /* Device block size */
654 apm16 = htobe16(512);
655 fwrite(&apm16, sizeof(apm16), 1, fd);
656 /* Device block count */
657 apm32 = htobe32(diskStructure->totalSectors *
658 (diskStructure->sectorSize / 512));
659 fwrite(&apm32, sizeof(apm32), 1, fd);
662 fwrite(&apm16, sizeof(apm16), 1, fd);
663 fwrite(&apm16, sizeof(apm16), 1, fd);
665 /* Count total needed entries */
666 total_parts = 2 + apm_partitions; /* Self + ISO9660 */
668 /* Write self-descriptor */
669 cd9660_write_apm_partition_entry(fd, 0, total_parts, 1,
670 total_parts, 512, "Apple", "Apple_partition_map");
672 /* Write all partition entries */
674 TAILQ_FOREACH(t, &diskStructure->boot_images, image_list) {
675 if (t->system != ET_SYS_MAC)
678 cd9660_write_apm_partition_entry(fd,
679 1 + apm_partitions++, total_parts,
680 t->sector * (diskStructure->sectorSize / 512),
681 t->num_sectors * (diskStructure->sectorSize / 512),
682 512, "CD Boot", "Apple_Bootstrap");
684 /* Write ISO9660 descriptor, enclosing the whole disk */
685 cd9660_write_apm_partition_entry(fd, 2 + apm_partitions,
686 total_parts, 0, diskStructure->totalSectors *
687 (diskStructure->sectorSize / 512), 512, "ISO9660",