1 /* $NetBSD: cd9660_eltorito.c,v 1.17 2011/06/23 02:35:56 enami Exp $ */
4 * Copyright (c) 2005 Daniel Watt, Walter Deignan, Ryan Gabrys, Alan
5 * Perez-Rathke and Ram Vedam. All rights reserved.
7 * This code was written by Daniel Watt, Walter Deignan, Ryan Gabrys,
8 * Alan Perez-Rathke and Ram Vedam.
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer in the documentation and/or other materials provided
18 * with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY DANIEL WATT, WALTER DEIGNAN, RYAN
21 * GABRYS, ALAN PEREZ-RATHKE AND RAM VEDAM ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 * DISCLAIMED. IN NO EVENT SHALL DANIEL WATT, WALTER DEIGNAN, RYAN
25 * GABRYS, ALAN PEREZ-RATHKE AND RAM VEDAM BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
28 * USE,DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
36 #include "cd9660_eltorito.h"
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
42 #define ELTORITO_DPRINTF(__x) printf __x
44 #define ELTORITO_DPRINTF(__x)
47 static struct boot_catalog_entry *cd9660_init_boot_catalog_entry(void);
48 static struct boot_catalog_entry *cd9660_boot_setup_validation_entry(char);
49 static struct boot_catalog_entry *cd9660_boot_setup_default_entry(
50 struct cd9660_boot_image *);
51 static struct boot_catalog_entry *cd9660_boot_setup_section_head(char);
52 static struct boot_catalog_entry *cd9660_boot_setup_validation_entry(char);
54 static u_char cd9660_boot_get_system_type(struct cd9660_boot_image *);
58 cd9660_add_boot_disk(const char *boot_info)
65 struct cd9660_boot_image *new_image, *tmp_image;
67 assert(boot_info != NULL);
69 if (*boot_info == '\0') {
70 warnx("Error: Boot disk information must be in the "
71 "format 'system;filename'");
75 /* First decode the boot information */
76 if ((temp = strdup(boot_info)) == NULL) {
77 warn("%s: strdup", __func__);
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 if ((new_image = malloc(sizeof(*new_image))) == NULL) {
97 warn("%s: malloc", __func__);
101 (void)memset(new_image, 0, sizeof(*new_image));
102 new_image->loadSegment = 0; /* default for now */
105 if (strcmp(sysname, "i386") == 0)
106 new_image->system = ET_SYS_X86;
107 else if (strcmp(sysname, "powerpc") == 0)
108 new_image->system = ET_SYS_PPC;
109 else if (strcmp(sysname, "macppc") == 0 ||
110 strcmp(sysname, "mac68k") == 0)
111 new_image->system = ET_SYS_MAC;
113 warnx("boot disk system must be "
114 "i386, powerpc, macppc, or mac68k");
121 if ((new_image->filename = strdup(filename)) == NULL) {
122 warn("%s: strdup", __func__);
130 /* Get information about the file */
131 if (lstat(new_image->filename, &stbuf) == -1)
132 err(EXIT_FAILURE, "%s: lstat(\"%s\")", __func__,
133 new_image->filename);
135 switch (stbuf.st_size) {
137 new_image->targetMode = ET_MEDIA_144FDD;
138 mode_msg = "Assigned boot image to 1.44 emulation mode";
141 new_image->targetMode = ET_MEDIA_12FDD;
142 mode_msg = "Assigned boot image to 1.2 emulation mode";
145 new_image->targetMode = ET_MEDIA_288FDD;
146 mode_msg = "Assigned boot image to 2.88 emulation mode";
149 new_image->targetMode = ET_MEDIA_NOEM;
150 mode_msg = "Assigned boot image to no emulation mode";
154 if (diskStructure.verbose_level > 0)
155 printf("%s\n", mode_msg);
157 new_image->size = stbuf.st_size;
158 new_image->num_sectors =
159 howmany(new_image->size, diskStructure.sectorSize) *
160 howmany(diskStructure.sectorSize, 512);
161 if (diskStructure.verbose_level > 0) {
162 printf("New image has size %d, uses %d 512-byte sectors\n",
163 new_image->size, new_image->num_sectors);
165 new_image->sector = -1;
166 /* Bootable by default */
167 new_image->bootable = ET_BOOTABLE;
170 /* Group images for the same platform together. */
171 TAILQ_FOREACH(tmp_image, &diskStructure.boot_images, image_list) {
172 if (tmp_image->system != new_image->system)
176 if (tmp_image == NULL) {
177 TAILQ_INSERT_HEAD(&diskStructure.boot_images, new_image,
180 TAILQ_INSERT_BEFORE(tmp_image, new_image, image_list);
182 new_image->serialno = diskStructure.image_serialno++;
184 /* TODO : Need to do anything about the boot image in the tree? */
185 diskStructure.is_bootable = 1;
191 cd9660_eltorito_add_boot_option(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__);
225 static struct boot_catalog_entry *
226 cd9660_init_boot_catalog_entry(void)
228 struct boot_catalog_entry *temp;
230 if ((temp = malloc(sizeof(*temp))) == NULL)
233 return memset(temp, 0, sizeof(*temp));
236 static struct boot_catalog_entry *
237 cd9660_boot_setup_validation_entry(char sys)
239 struct boot_catalog_entry *entry;
240 boot_catalog_validation_entry *ve;
242 unsigned char *csptr;
244 entry = cd9660_init_boot_catalog_entry();
247 warnx("Error: memory allocation failed in "
248 "cd9660_boot_setup_validation_entry");
251 ve = &entry->entry_data.VE;
253 ve->header_id[0] = 1;
254 ve->platform_id[0] = sys;
258 /* Calculate checksum */
260 cd9660_721(0, ve->checksum);
261 csptr = (unsigned char*)ve;
262 for (i = 0; i < sizeof(*ve); i += 2) {
263 checksum += (int16_t)csptr[i];
264 checksum += 256 * (int16_t)csptr[i + 1];
266 checksum = -checksum;
267 cd9660_721(checksum, ve->checksum);
269 ELTORITO_DPRINTF(("%s: header_id %d, platform_id %d, key[0] %d, key[1] %d, "
270 "checksum %04x\n", __func__, ve->header_id[0], ve->platform_id[0],
271 ve->key[0], ve->key[1], checksum));
275 static struct boot_catalog_entry *
276 cd9660_boot_setup_default_entry(struct cd9660_boot_image *disk)
278 struct boot_catalog_entry *default_entry;
279 boot_catalog_initial_entry *ie;
281 default_entry = cd9660_init_boot_catalog_entry();
282 if (default_entry == NULL)
285 ie = &default_entry->entry_data.IE;
287 ie->boot_indicator[0] = disk->bootable;
288 ie->media_type[0] = disk->targetMode;
289 cd9660_721(disk->loadSegment, ie->load_segment);
290 ie->system_type[0] = disk->system;
291 cd9660_721(disk->num_sectors, ie->sector_count);
292 cd9660_731(disk->sector, ie->load_rba);
294 ELTORITO_DPRINTF(("%s: boot indicator %d, media type %d, "
295 "load segment %04x, system type %d, sector count %d, "
296 "load rba %d\n", __func__, ie->boot_indicator[0],
297 ie->media_type[0], disk->loadSegment, ie->system_type[0],
298 disk->num_sectors, disk->sector));
299 return default_entry;
302 static struct boot_catalog_entry *
303 cd9660_boot_setup_section_head(char platform)
305 struct boot_catalog_entry *entry;
306 boot_catalog_section_header *sh;
308 entry = cd9660_init_boot_catalog_entry();
312 sh = &entry->entry_data.SH;
313 /* More by default. The last one will manually be set to 0x91 */
314 sh->header_indicator[0] = ET_SECTION_HEADER_MORE;
315 sh->platform_id[0] = platform;
316 sh->num_section_entries[0] = 0;
320 static struct boot_catalog_entry *
321 cd9660_boot_setup_section_entry(struct cd9660_boot_image *disk)
323 struct boot_catalog_entry *entry;
324 boot_catalog_section_entry *se;
325 if ((entry = cd9660_init_boot_catalog_entry()) == NULL)
328 se = &entry->entry_data.SE;
330 se->boot_indicator[0] = ET_BOOTABLE;
331 se->media_type[0] = disk->targetMode;
332 cd9660_721(disk->loadSegment, se->load_segment);
333 cd9660_721(disk->num_sectors, se->sector_count);
334 cd9660_731(disk->sector, se->load_rba);
340 cd9660_boot_get_system_type(struct cd9660_boot_image *disk)
343 For hard drive booting, we need to examine the MBR to figure
344 out what the partition type is
351 * Set up the BVD, Boot catalog, and the boot entries, but do no writing
354 cd9660_setup_boot(int first_sector)
360 struct boot_catalog_entry *x86_head, *mac_head, *ppc_head,
361 *valid_entry, *default_entry, *temp, *head, **headp, *next;
362 struct cd9660_boot_image *tmp_disk;
365 x86_head = mac_head = ppc_head = NULL;
367 /* If there are no boot disks, don't bother building boot information */
368 if (TAILQ_EMPTY(&diskStructure.boot_images))
371 /* Point to catalog: For now assume it consumes one sector */
372 ELTORITO_DPRINTF(("Boot catalog will go in sector %d\n", first_sector));
373 diskStructure.boot_catalog_sector = first_sector;
374 cd9660_bothendian_dword(first_sector,
375 diskStructure.boot_descriptor->boot_catalog_pointer);
377 /* Step 1: Generate boot catalog */
378 /* Step 1a: Validation entry */
379 valid_entry = cd9660_boot_setup_validation_entry(ET_SYS_X86);
380 if (valid_entry == NULL)
384 * Count how many boot images there are,
385 * and how many sectors they consume.
390 TAILQ_FOREACH(tmp_disk, &diskStructure.boot_images, image_list) {
391 used_sectors += tmp_disk->num_sectors;
393 /* One default entry per image */
396 catalog_sectors = howmany(num_entries * 0x20, diskStructure.sectorSize);
397 used_sectors += catalog_sectors;
399 if (diskStructure.verbose_level > 0) {
400 printf("%s: there will be %i entries consuming %i sectors. "
401 "Catalog is %i sectors\n", __func__, num_entries,
402 used_sectors, catalog_sectors);
405 /* Populate sector numbers */
406 sector = first_sector + catalog_sectors;
407 TAILQ_FOREACH(tmp_disk, &diskStructure.boot_images, image_list) {
408 tmp_disk->sector = sector;
409 sector += tmp_disk->num_sectors;
412 LIST_INSERT_HEAD(&diskStructure.boot_entries, valid_entry, ll_struct);
414 /* Step 1b: Initial/default entry */
416 tmp_disk = TAILQ_FIRST(&diskStructure.boot_images);
417 default_entry = cd9660_boot_setup_default_entry(tmp_disk);
418 if (default_entry == NULL) {
419 warnx("Error: memory allocation failed in cd9660_setup_boot");
423 LIST_INSERT_AFTER(valid_entry, default_entry, ll_struct);
425 /* Todo: multiple default entries? */
427 tmp_disk = TAILQ_NEXT(tmp_disk, image_list);
429 temp = default_entry;
431 /* If multiple boot images are given : */
432 while (tmp_disk != NULL) {
433 /* Step 2: Section header */
434 switch (tmp_disk->system) {
445 warnx("%s: internal error: unknown system type",
450 if (*headp == NULL) {
452 cd9660_boot_setup_section_head(tmp_disk->system);
454 warnx("Error: memory allocation failed in "
455 "cd9660_setup_boot");
458 LIST_INSERT_AFTER(default_entry, head, ll_struct);
463 head->entry_data.SH.num_section_entries[0]++;
465 /* Step 2a: Section entry and extensions */
466 temp = cd9660_boot_setup_section_entry(tmp_disk);
468 warn("%s: cd9660_boot_setup_section_entry", __func__);
472 while ((next = LIST_NEXT(head, ll_struct)) != NULL &&
473 next->entry_type == ET_ENTRY_SE)
476 LIST_INSERT_AFTER(head, temp, ll_struct);
477 tmp_disk = TAILQ_NEXT(tmp_disk, image_list);
480 /* TODO: Remaining boot disks when implemented */
482 return first_sector + used_sectors;
486 cd9660_setup_boot_volume_descriptor(volume_descriptor *bvd)
488 boot_volume_descriptor *bvdData =
489 (boot_volume_descriptor*)bvd->volumeDescriptorData;
491 bvdData->boot_record_indicator[0] = ISO_VOLUME_DESCRIPTOR_BOOT;
492 memcpy(bvdData->identifier, ISO_VOLUME_DESCRIPTOR_STANDARD_ID, 5);
493 bvdData->version[0] = 1;
494 memcpy(bvdData->boot_system_identifier, ET_ID, 23);
495 memcpy(bvdData->identifier, ISO_VOLUME_DESCRIPTOR_STANDARD_ID, 5);
496 diskStructure.boot_descriptor =
497 (boot_volume_descriptor*) bvd->volumeDescriptorData;
502 cd9660_write_mbr_partition_entry(FILE *fd, int idx, off_t sector_start,
503 off_t nsectors, int type)
508 if (fseeko(fd, (off_t)(idx) * 16 + 0x1be, SEEK_SET) == -1)
511 val = 0x80; /* Bootable */
512 fwrite(&val, sizeof(val), 1, fd);
514 val = 0xff; /* CHS begin */
515 fwrite(&val, sizeof(val), 1, fd);
516 fwrite(&val, sizeof(val), 1, fd);
517 fwrite(&val, sizeof(val), 1, fd);
519 val = type; /* Part type */
520 fwrite(&val, sizeof(val), 1, fd);
522 val = 0xff; /* CHS end */
523 fwrite(&val, sizeof(val), 1, fd);
524 fwrite(&val, sizeof(val), 1, fd);
525 fwrite(&val, sizeof(val), 1, fd);
528 lba = htole32(sector_start);
529 fwrite(&lba, sizeof(lba), 1, fd);
530 lba = htole32(nsectors);
531 fwrite(&lba, sizeof(lba), 1, fd);
537 cd9660_write_apm_partition_entry(FILE *fd, int idx, int total_partitions,
538 off_t sector_start, off_t nsectors, off_t sector_size,
539 const char *part_name, const char *part_type)
544 if (fseeko(fd, (off_t)(idx + 1) * sector_size, SEEK_SET) == -1)
548 apm16 = htobe16(0x504d);
549 fwrite(&apm16, sizeof(apm16), 1, fd);
551 fwrite(&apm16, sizeof(apm16), 1, fd);
553 /* Total number of partitions */
554 apm32 = htobe32(total_partitions);
555 fwrite(&apm32, sizeof(apm32), 1, fd);
557 apm32 = htobe32(sector_start);
558 fwrite(&apm32, sizeof(apm32), 1, fd);
559 apm32 = htobe32(nsectors);
560 fwrite(&apm32, sizeof(apm32), 1, fd);
562 fwrite(part_name, strlen(part_name) + 1, 1, fd);
563 fseek(fd, 32 - strlen(part_name) - 1, SEEK_CUR);
564 fwrite(part_type, strlen(part_type) + 1, 1, fd);
570 cd9660_write_boot(FILE *fd)
572 struct boot_catalog_entry *e;
573 struct cd9660_boot_image *t;
574 int apm_partitions = 0;
575 int mbr_partitions = 0;
577 /* write boot catalog */
578 if (fseeko(fd, (off_t)diskStructure.boot_catalog_sector *
579 diskStructure.sectorSize, SEEK_SET) == -1)
582 if (diskStructure.verbose_level > 0) {
583 printf("Writing boot catalog to sector %" PRId64 "\n",
584 diskStructure.boot_catalog_sector);
586 LIST_FOREACH(e, &diskStructure.boot_entries, ll_struct) {
587 if (diskStructure.verbose_level > 0) {
588 printf("Writing catalog entry of type %d\n",
592 * It doesn't matter which one gets written
593 * since they are the same size
595 fwrite(&(e->entry_data.VE), 1, 32, fd);
597 if (diskStructure.verbose_level > 0)
598 printf("Finished writing boot catalog\n");
600 /* copy boot images */
601 TAILQ_FOREACH(t, &diskStructure.boot_images, image_list) {
602 if (diskStructure.verbose_level > 0) {
603 printf("Writing boot image from %s to sectors %d\n",
604 t->filename, t->sector);
606 cd9660_copy_file(fd, t->sector, t->filename);
608 if (t->system == ET_SYS_MAC)
610 if (t->system == ET_SYS_PPC)
614 /* some systems need partition tables as well */
615 if (mbr_partitions > 0 || diskStructure.chrp_boot) {
618 fseek(fd, 0x1fe, SEEK_SET);
619 sig = htole16(0xaa55);
620 fwrite(&sig, sizeof(sig), 1, fd);
624 /* Write ISO9660 descriptor, enclosing the whole disk */
625 if (diskStructure.chrp_boot)
626 cd9660_write_mbr_partition_entry(fd, mbr_partitions++,
627 0, diskStructure.totalSectors *
628 (diskStructure.sectorSize / 512), 0x96);
630 /* Write all partition entries */
631 TAILQ_FOREACH(t, &diskStructure.boot_images, image_list) {
632 if (t->system != ET_SYS_PPC)
634 cd9660_write_mbr_partition_entry(fd, mbr_partitions++,
635 t->sector * (diskStructure.sectorSize / 512),
636 t->num_sectors * (diskStructure.sectorSize / 512),
637 0x41 /* PReP Boot */);
641 if (apm_partitions > 0) {
642 /* Write DDR and global APM info */
647 fseek(fd, 0, SEEK_SET);
648 apm16 = htobe16(0x4552);
649 fwrite(&apm16, sizeof(apm16), 1, fd);
650 /* Device block size */
651 apm16 = htobe16(512);
652 fwrite(&apm16, sizeof(apm16), 1, fd);
653 /* Device block count */
654 apm32 = htobe32(diskStructure.totalSectors *
655 (diskStructure.sectorSize / 512));
656 fwrite(&apm32, sizeof(apm32), 1, fd);
659 fwrite(&apm16, sizeof(apm16), 1, fd);
660 fwrite(&apm16, sizeof(apm16), 1, fd);
662 /* Count total needed entries */
663 total_parts = 2 + apm_partitions; /* Self + ISO9660 */
665 /* Write self-descriptor */
666 cd9660_write_apm_partition_entry(fd, 0, total_parts, 1,
667 total_parts, 512, "Apple", "Apple_partition_map");
669 /* Write ISO9660 descriptor, enclosing the whole disk */
670 cd9660_write_apm_partition_entry(fd, 1, total_parts, 0,
671 diskStructure.totalSectors *
672 (diskStructure.sectorSize / 512), 512, "ISO9660",
675 /* Write all partition entries */
677 TAILQ_FOREACH(t, &diskStructure.boot_images, image_list) {
678 if (t->system != ET_SYS_MAC)
681 cd9660_write_apm_partition_entry(fd,
682 2 + apm_partitions++, total_parts,
683 t->sector * (diskStructure.sectorSize / 512),
684 t->num_sectors * (diskStructure.sectorSize / 512),
685 512, "CD Boot", "Apple_Bootstrap");