cd9660_eltorito.c revision 1.23 1 /* $NetBSD: cd9660_eltorito.c,v 1.23 2018/03/28 06:48:55 nonaka Exp $ */
2
3 /*
4 * Copyright (c) 2005 Daniel Watt, Walter Deignan, Ryan Gabrys, Alan
5 * Perez-Rathke and Ram Vedam. All rights reserved.
6 *
7 * This code was written by Daniel Watt, Walter Deignan, Ryan Gabrys,
8 * Alan Perez-Rathke and Ram Vedam.
9 *
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
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.
19 *
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
32 * OF SUCH DAMAGE.
33 */
34
35
36 #include "cd9660.h"
37 #include "cd9660_eltorito.h"
38 #include <sys/bootblock.h>
39 #include <util.h>
40
41 #include <sys/cdefs.h>
42 #if defined(__RCSID) && !defined(__lint)
43 __RCSID("$NetBSD: cd9660_eltorito.c,v 1.23 2018/03/28 06:48:55 nonaka Exp $");
44 #endif /* !__lint */
45
46 #ifdef DEBUG
47 #define ELTORITO_DPRINTF(__x) printf __x
48 #else
49 #define ELTORITO_DPRINTF(__x)
50 #endif
51
52 #include <util.h>
53
54 static struct boot_catalog_entry *cd9660_init_boot_catalog_entry(void);
55 static struct boot_catalog_entry *cd9660_boot_setup_validation_entry(char);
56 static struct boot_catalog_entry *cd9660_boot_setup_default_entry(
57 struct cd9660_boot_image *);
58 static struct boot_catalog_entry *cd9660_boot_setup_section_head(char);
59 #if 0
60 static u_char cd9660_boot_get_system_type(struct cd9660_boot_image *);
61 #endif
62
63 static struct cd9660_boot_image *default_boot_image;
64
65 int
66 cd9660_add_boot_disk(iso9660_disk *diskStructure, const char *boot_info)
67 {
68 struct stat stbuf;
69 const char *mode_msg;
70 char *temp;
71 char *sysname;
72 char *filename;
73 struct cd9660_boot_image *new_image, *tmp_image;
74
75 assert(boot_info != NULL);
76
77 if (*boot_info == '\0') {
78 warnx("Error: Boot disk information must be in the "
79 "format 'system;filename'");
80 return 0;
81 }
82
83 /* First decode the boot information */
84 temp = estrdup(boot_info);
85
86 sysname = temp;
87 filename = strchr(sysname, ';');
88 if (filename == NULL) {
89 warnx("supply boot disk information in the format "
90 "'system;filename'");
91 free(temp);
92 return 0;
93 }
94
95 *filename++ = '\0';
96
97 if (diskStructure->verbose_level > 0) {
98 printf("Found bootdisk with system %s, and filename %s\n",
99 sysname, filename);
100 }
101 new_image = ecalloc(1, sizeof(*new_image));
102 new_image->loadSegment = 0; /* default for now */
103
104 /* Decode System */
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;
112 else {
113 warnx("boot disk system must be "
114 "i386, powerpc, macppc, or mac68k");
115 free(temp);
116 free(new_image);
117 return 0;
118 }
119
120
121 new_image->filename = estrdup(filename);
122
123 free(temp);
124
125 /* Get information about the file */
126 if (lstat(new_image->filename, &stbuf) == -1)
127 err(EXIT_FAILURE, "%s: lstat(\"%s\")", __func__,
128 new_image->filename);
129
130 switch (stbuf.st_size) {
131 case 1440 * 1024:
132 new_image->targetMode = ET_MEDIA_144FDD;
133 mode_msg = "Assigned boot image to 1.44 emulation mode";
134 break;
135 case 1200 * 1024:
136 new_image->targetMode = ET_MEDIA_12FDD;
137 mode_msg = "Assigned boot image to 1.2 emulation mode";
138 break;
139 case 2880 * 1024:
140 new_image->targetMode = ET_MEDIA_288FDD;
141 mode_msg = "Assigned boot image to 2.88 emulation mode";
142 break;
143 default:
144 new_image->targetMode = ET_MEDIA_NOEM;
145 mode_msg = "Assigned boot image to no emulation mode";
146 break;
147 }
148
149 if (diskStructure->verbose_level > 0)
150 printf("%s\n", mode_msg);
151
152 new_image->size = stbuf.st_size;
153 new_image->num_sectors =
154 howmany(new_image->size, diskStructure->sectorSize) *
155 howmany(diskStructure->sectorSize, 512);
156 if (diskStructure->verbose_level > 0) {
157 printf("New image has size %d, uses %d 512-byte sectors\n",
158 new_image->size, new_image->num_sectors);
159 }
160 new_image->sector = -1;
161 /* Bootable by default */
162 new_image->bootable = ET_BOOTABLE;
163 /* Add boot disk */
164
165 /* Group images for the same platform together. */
166 TAILQ_FOREACH(tmp_image, &diskStructure->boot_images, image_list) {
167 if (tmp_image->system != new_image->system)
168 break;
169 }
170
171 if (tmp_image == NULL) {
172 TAILQ_INSERT_HEAD(&diskStructure->boot_images, new_image,
173 image_list);
174 } else
175 TAILQ_INSERT_BEFORE(tmp_image, new_image, image_list);
176
177 new_image->serialno = diskStructure->image_serialno++;
178
179 new_image->platform_id = new_image->system;
180
181 /* TODO : Need to do anything about the boot image in the tree? */
182 diskStructure->is_bootable = 1;
183
184 /* First boot image is initial/default entry. */
185 if (default_boot_image == NULL)
186 default_boot_image = new_image;
187
188 return 1;
189 }
190
191 int
192 cd9660_eltorito_add_boot_option(iso9660_disk *diskStructure,
193 const char *option_string, const char *value)
194 {
195 char *eptr;
196 struct cd9660_boot_image *image;
197
198 assert(option_string != NULL);
199
200 /* Find the last image added */
201 TAILQ_FOREACH(image, &diskStructure->boot_images, image_list) {
202 if (image->serialno + 1 == diskStructure->image_serialno)
203 break;
204 }
205 if (image == NULL)
206 errx(EXIT_FAILURE, "Attempted to add boot option, "
207 "but no boot images have been specified");
208
209 if (strcmp(option_string, "no-emul-boot") == 0) {
210 image->targetMode = ET_MEDIA_NOEM;
211 } else if (strcmp(option_string, "no-boot") == 0) {
212 image->bootable = ET_NOT_BOOTABLE;
213 } else if (strcmp(option_string, "hard-disk-boot") == 0) {
214 image->targetMode = ET_MEDIA_HDD;
215 } else if (strcmp(option_string, "boot-load-segment") == 0) {
216 image->loadSegment = strtoul(value, &eptr, 16);
217 if (eptr == value || *eptr != '\0' || errno != ERANGE) {
218 warn("%s: strtoul", __func__);
219 return 0;
220 }
221 } else if (strcmp(option_string, "platformid") == 0) {
222 if (strcmp(value, "efi") == 0)
223 image->platform_id = ET_SYS_EFI;
224 else {
225 warn("%s: unknown platform: %s", __func__, value);
226 return 0;
227 }
228 } else {
229 return 0;
230 }
231 return 1;
232 }
233
234 static struct boot_catalog_entry *
235 cd9660_init_boot_catalog_entry(void)
236 {
237 return ecalloc(1, sizeof(struct boot_catalog_entry));
238 }
239
240 static struct boot_catalog_entry *
241 cd9660_boot_setup_validation_entry(char sys)
242 {
243 struct boot_catalog_entry *entry;
244 boot_catalog_validation_entry *ve;
245 int16_t checksum;
246 unsigned char *csptr;
247 size_t i;
248 entry = cd9660_init_boot_catalog_entry();
249
250 entry->entry_type = ET_ENTRY_VE;
251 ve = &entry->entry_data.VE;
252
253 ve->header_id[0] = 1;
254 ve->platform_id[0] = sys;
255 ve->key[0] = 0x55;
256 ve->key[1] = 0xAA;
257
258 /* Calculate checksum */
259 checksum = 0;
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];
265 }
266 checksum = -checksum;
267 cd9660_721(checksum, ve->checksum);
268
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));
272 return entry;
273 }
274
275 static struct boot_catalog_entry *
276 cd9660_boot_setup_default_entry(struct cd9660_boot_image *disk)
277 {
278 struct boot_catalog_entry *default_entry;
279 boot_catalog_initial_entry *ie;
280
281 default_entry = cd9660_init_boot_catalog_entry();
282 if (default_entry == NULL)
283 return NULL;
284
285 default_entry->entry_type = ET_ENTRY_IE;
286 ie = &default_entry->entry_data.IE;
287
288 ie->boot_indicator[0] = disk->bootable;
289 ie->media_type[0] = disk->targetMode;
290 cd9660_721(disk->loadSegment, ie->load_segment);
291 ie->system_type[0] = disk->system;
292 cd9660_721(disk->num_sectors, ie->sector_count);
293 cd9660_731(disk->sector, ie->load_rba);
294
295 ELTORITO_DPRINTF(("%s: boot indicator %d, media type %d, "
296 "load segment %04x, system type %d, sector count %d, "
297 "load rba %d\n", __func__, ie->boot_indicator[0],
298 ie->media_type[0], disk->loadSegment, ie->system_type[0],
299 disk->num_sectors, disk->sector));
300 return default_entry;
301 }
302
303 static struct boot_catalog_entry *
304 cd9660_boot_setup_section_head(char platform)
305 {
306 struct boot_catalog_entry *entry;
307 boot_catalog_section_header *sh;
308
309 entry = cd9660_init_boot_catalog_entry();
310 if (entry == NULL)
311 return NULL;
312
313 entry->entry_type = ET_ENTRY_SH;
314 sh = &entry->entry_data.SH;
315 /* More by default. The last one will manually be set to 0x91 */
316 sh->header_indicator[0] = ET_SECTION_HEADER_MORE;
317 sh->platform_id[0] = platform;
318 sh->num_section_entries[0] = 0;
319 return entry;
320 }
321
322 static struct boot_catalog_entry *
323 cd9660_boot_setup_section_entry(struct cd9660_boot_image *disk)
324 {
325 struct boot_catalog_entry *entry;
326 boot_catalog_section_entry *se;
327 if ((entry = cd9660_init_boot_catalog_entry()) == NULL)
328 return NULL;
329
330 entry->entry_type = ET_ENTRY_SE;
331 se = &entry->entry_data.SE;
332
333 se->boot_indicator[0] = ET_BOOTABLE;
334 se->media_type[0] = disk->targetMode;
335 cd9660_721(disk->loadSegment, se->load_segment);
336 cd9660_721(disk->num_sectors, se->sector_count);
337 cd9660_731(disk->sector, se->load_rba);
338 return entry;
339 }
340
341 #if 0
342 static u_char
343 cd9660_boot_get_system_type(struct cd9660_boot_image *disk)
344 {
345 /*
346 For hard drive booting, we need to examine the MBR to figure
347 out what the partition type is
348 */
349 return 0;
350 }
351 #endif
352
353 /*
354 * Set up the BVD, Boot catalog, and the boot entries, but do no writing
355 */
356 int
357 cd9660_setup_boot(iso9660_disk *diskStructure, int first_sector)
358 {
359 int sector;
360 int used_sectors;
361 int num_entries = 0;
362 int catalog_sectors;
363 struct boot_catalog_entry *x86_head, *mac_head, *ppc_head, *efi_head,
364 *valid_entry, *default_entry, *temp, *head, **headp, *next;
365 struct cd9660_boot_image *tmp_disk;
366
367 headp = NULL;
368 x86_head = mac_head = ppc_head = efi_head = NULL;
369
370 /* If there are no boot disks, don't bother building boot information */
371 if (TAILQ_EMPTY(&diskStructure->boot_images))
372 return 0;
373
374 /* Point to catalog: For now assume it consumes one sector */
375 ELTORITO_DPRINTF(("Boot catalog will go in sector %d\n", first_sector));
376 diskStructure->boot_catalog_sector = first_sector;
377 cd9660_bothendian_dword(first_sector,
378 diskStructure->boot_descriptor->boot_catalog_pointer);
379
380 /* Step 1: Generate boot catalog */
381 /* Step 1a: Validation entry */
382 valid_entry = cd9660_boot_setup_validation_entry(ET_SYS_X86);
383 if (valid_entry == NULL)
384 return -1;
385
386 /*
387 * Count how many boot images there are,
388 * and how many sectors they consume.
389 */
390 num_entries = 1;
391 used_sectors = 0;
392
393 TAILQ_FOREACH(tmp_disk, &diskStructure->boot_images, image_list) {
394 used_sectors += tmp_disk->num_sectors;
395
396 /* One default entry per image */
397 num_entries++;
398 }
399 catalog_sectors = howmany(num_entries * 0x20, diskStructure->sectorSize);
400 used_sectors += catalog_sectors;
401
402 if (diskStructure->verbose_level > 0) {
403 printf("%s: there will be %i entries consuming %i sectors. "
404 "Catalog is %i sectors\n", __func__, num_entries,
405 used_sectors, catalog_sectors);
406 }
407
408 /* Populate sector numbers */
409 sector = first_sector + catalog_sectors;
410 TAILQ_FOREACH(tmp_disk, &diskStructure->boot_images, image_list) {
411 tmp_disk->sector = sector;
412 sector += tmp_disk->num_sectors /
413 (diskStructure->sectorSize / 512);
414 }
415
416 LIST_INSERT_HEAD(&diskStructure->boot_entries, valid_entry, ll_struct);
417
418 /* Step 1b: Initial/default entry */
419 /* TODO : PARAM */
420 if (default_boot_image != NULL) {
421 struct cd9660_boot_image *tcbi;
422 TAILQ_FOREACH(tcbi, &diskStructure->boot_images, image_list) {
423 if (tcbi == default_boot_image) {
424 tmp_disk = tcbi;
425 break;
426 }
427 }
428 }
429 if (tmp_disk == NULL)
430 tmp_disk = TAILQ_FIRST(&diskStructure->boot_images);
431 default_entry = cd9660_boot_setup_default_entry(tmp_disk);
432 if (default_entry == NULL) {
433 warnx("Error: memory allocation failed in cd9660_setup_boot");
434 return -1;
435 }
436
437 LIST_INSERT_AFTER(valid_entry, default_entry, ll_struct);
438
439 /* Todo: multiple default entries? */
440
441 tmp_disk = TAILQ_FIRST(&diskStructure->boot_images);
442
443 head = NULL;
444 temp = default_entry;
445
446 /* If multiple boot images are given : */
447 for (; tmp_disk != NULL; tmp_disk = TAILQ_NEXT(tmp_disk, image_list)) {
448 if (tmp_disk == default_boot_image)
449 continue;
450
451 /* Step 2: Section header */
452 switch (tmp_disk->platform_id) {
453 case ET_SYS_X86:
454 headp = &x86_head;
455 break;
456 case ET_SYS_PPC:
457 headp = &ppc_head;
458 break;
459 case ET_SYS_MAC:
460 headp = &mac_head;
461 break;
462 case ET_SYS_EFI:
463 headp = &efi_head;
464 break;
465 default:
466 warnx("%s: internal error: unknown system type",
467 __func__);
468 return -1;
469 }
470
471 if (*headp == NULL) {
472 head =
473 cd9660_boot_setup_section_head(tmp_disk->platform_id);
474 if (head == NULL) {
475 warnx("Error: memory allocation failed in "
476 "cd9660_setup_boot");
477 return -1;
478 }
479 LIST_INSERT_AFTER(default_entry, head, ll_struct);
480 *headp = head;
481 } else
482 head = *headp;
483
484 head->entry_data.SH.num_section_entries[0]++;
485
486 /* Step 2a: Section entry and extensions */
487 temp = cd9660_boot_setup_section_entry(tmp_disk);
488 if (temp == NULL) {
489 warn("%s: cd9660_boot_setup_section_entry", __func__);
490 return -1;
491 }
492
493 while ((next = LIST_NEXT(head, ll_struct)) != NULL &&
494 next->entry_type == ET_ENTRY_SE)
495 head = next;
496
497 LIST_INSERT_AFTER(head, temp, ll_struct);
498 }
499
500 /* Find the last Section Header entry and mark it as the last. */
501 head = NULL;
502 LIST_FOREACH(next, &diskStructure->boot_entries, ll_struct) {
503 if (next->entry_type == ET_ENTRY_SH)
504 head = next;
505 }
506 if (head != NULL)
507 head->entry_data.SH.header_indicator[0] = ET_SECTION_HEADER_LAST;
508
509 /* TODO: Remaining boot disks when implemented */
510
511 return first_sector + used_sectors;
512 }
513
514 int
515 cd9660_setup_boot_volume_descriptor(iso9660_disk *diskStructure,
516 volume_descriptor *bvd)
517 {
518 boot_volume_descriptor *bvdData =
519 (boot_volume_descriptor*)bvd->volumeDescriptorData;
520
521 bvdData->boot_record_indicator[0] = ISO_VOLUME_DESCRIPTOR_BOOT;
522 memcpy(bvdData->identifier, ISO_VOLUME_DESCRIPTOR_STANDARD_ID, 5);
523 bvdData->version[0] = 1;
524 memcpy(bvdData->boot_system_identifier, ET_ID, 23);
525 memcpy(bvdData->identifier, ISO_VOLUME_DESCRIPTOR_STANDARD_ID, 5);
526 diskStructure->boot_descriptor =
527 (boot_volume_descriptor*) bvd->volumeDescriptorData;
528 return 1;
529 }
530
531 static int
532 cd9660_write_mbr_partition_entry(FILE *fd, int idx, off_t sector_start,
533 off_t nsectors, int type)
534 {
535 uint8_t val;
536 uint32_t lba;
537
538 if (fseeko(fd, (off_t)(idx) * 16 + 0x1be, SEEK_SET) == -1)
539 err(1, "fseeko");
540
541 val = 0x80; /* Bootable */
542 fwrite(&val, sizeof(val), 1, fd);
543
544 val = 0xff; /* CHS begin */
545 fwrite(&val, sizeof(val), 1, fd);
546 fwrite(&val, sizeof(val), 1, fd);
547 fwrite(&val, sizeof(val), 1, fd);
548
549 val = type; /* Part type */
550 fwrite(&val, sizeof(val), 1, fd);
551
552 val = 0xff; /* CHS end */
553 fwrite(&val, sizeof(val), 1, fd);
554 fwrite(&val, sizeof(val), 1, fd);
555 fwrite(&val, sizeof(val), 1, fd);
556
557 /* LBA extent */
558 lba = htole32(sector_start);
559 fwrite(&lba, sizeof(lba), 1, fd);
560 lba = htole32(nsectors);
561 fwrite(&lba, sizeof(lba), 1, fd);
562
563 return 0;
564 }
565
566 static int
567 cd9660_write_apm_partition_entry(FILE *fd, int idx, int total_partitions,
568 off_t sector_start, off_t nsectors, off_t sector_size,
569 const char *part_name, const char *part_type)
570 {
571 uint32_t apm32, part_status;
572 uint16_t apm16;
573
574 /* See Apple Tech Note 1189 for the details about the pmPartStatus
575 * flags.
576 * Below the flags which are default:
577 * - IsValid 0x01
578 * - IsAllocated 0x02
579 * - IsReadable 0x10
580 * - IsWritable 0x20
581 */
582 part_status = APPLE_PS_VALID | APPLE_PS_ALLOCATED | APPLE_PS_READABLE |
583 APPLE_PS_WRITABLE;
584
585 if (fseeko(fd, (off_t)(idx + 1) * sector_size, SEEK_SET) == -1)
586 err(1, "fseeko");
587
588 /* Signature */
589 apm16 = htobe16(0x504d);
590 fwrite(&apm16, sizeof(apm16), 1, fd);
591 apm16 = 0;
592 fwrite(&apm16, sizeof(apm16), 1, fd);
593
594 /* Total number of partitions */
595 apm32 = htobe32(total_partitions);
596 fwrite(&apm32, sizeof(apm32), 1, fd);
597 /* Bounds */
598 apm32 = htobe32(sector_start);
599 fwrite(&apm32, sizeof(apm32), 1, fd);
600 apm32 = htobe32(nsectors);
601 fwrite(&apm32, sizeof(apm32), 1, fd);
602
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);
607
608 apm32 = 0;
609 /* pmLgDataStart */
610 fwrite(&apm32, sizeof(apm32), 1, fd);
611 /* pmDataCnt */
612 apm32 = htobe32(nsectors);
613 fwrite(&apm32, sizeof(apm32), 1, fd);
614 /* pmPartStatus */
615 apm32 = htobe32(part_status);
616 fwrite(&apm32, sizeof(apm32), 1, fd);
617
618 return 0;
619 }
620
621 int
622 cd9660_write_boot(iso9660_disk *diskStructure, FILE *fd)
623 {
624 struct boot_catalog_entry *e;
625 struct cd9660_boot_image *t;
626 int apm_partitions = 0;
627 int mbr_partitions = 0;
628
629 /* write boot catalog */
630 if (fseeko(fd, (off_t)diskStructure->boot_catalog_sector *
631 diskStructure->sectorSize, SEEK_SET) == -1)
632 err(1, "fseeko");
633
634 if (diskStructure->verbose_level > 0) {
635 printf("Writing boot catalog to sector %" PRId64 "\n",
636 diskStructure->boot_catalog_sector);
637 }
638 LIST_FOREACH(e, &diskStructure->boot_entries, ll_struct) {
639 if (diskStructure->verbose_level > 0) {
640 printf("Writing catalog entry of type %d\n",
641 e->entry_type);
642 }
643 /*
644 * It doesnt matter which one gets written
645 * since they are the same size
646 */
647 fwrite(&(e->entry_data.VE), 1, 32, fd);
648 }
649 if (diskStructure->verbose_level > 0)
650 printf("Finished writing boot catalog\n");
651
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);
657 }
658 cd9660_copy_file(diskStructure, fd, t->sector, t->filename);
659
660 if (t->system == ET_SYS_MAC)
661 apm_partitions++;
662 if (t->system == ET_SYS_PPC)
663 mbr_partitions++;
664 }
665
666 /* some systems need partition tables as well */
667 if (mbr_partitions > 0 || diskStructure->chrp_boot) {
668 uint16_t sig;
669
670 fseek(fd, 0x1fe, SEEK_SET);
671 sig = htole16(0xaa55);
672 fwrite(&sig, sizeof(sig), 1, fd);
673
674 mbr_partitions = 0;
675
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);
681
682 /* Write all partition entries */
683 TAILQ_FOREACH(t, &diskStructure->boot_images, image_list) {
684 if (t->system != ET_SYS_PPC)
685 continue;
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 */);
690 }
691 }
692
693 if (apm_partitions > 0) {
694 /* Write DDR and global APM info */
695 uint32_t apm32;
696 uint16_t apm16;
697 int total_parts;
698
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);
709 /* Device type/id */
710 apm16 = htobe16(1);
711 fwrite(&apm16, sizeof(apm16), 1, fd);
712 fwrite(&apm16, sizeof(apm16), 1, fd);
713
714 /* Count total needed entries */
715 total_parts = 2 + apm_partitions; /* Self + ISO9660 */
716
717 /* Write self-descriptor */
718 cd9660_write_apm_partition_entry(fd, 0, total_parts, 1,
719 total_parts, 512, "Apple", "Apple_partition_map");
720
721 /* Write all partition entries */
722 apm_partitions = 0;
723 TAILQ_FOREACH(t, &diskStructure->boot_images, image_list) {
724 if (t->system != ET_SYS_MAC)
725 continue;
726
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");
732 }
733
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",
738 "CD_ROM_Mode_1");
739 }
740
741 return 0;
742 }
743