]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/makefs/cd9660/cd9660_eltorito.c
MFC r316579
[FreeBSD/FreeBSD.git] / usr.sbin / makefs / cd9660 / cd9660_eltorito.c
1 /*      $NetBSD: cd9660_eltorito.c,v 1.17 2011/06/23 02:35:56 enami Exp $       */
2
3 /*-
4  * SPDX-License-Identifier: BSD-2-Clause-NetBSD
5  *
6  * Copyright (c) 2005 Daniel Watt, Walter Deignan, Ryan Gabrys, Alan
7  * Perez-Rathke and Ram Vedam.  All rights reserved.
8  *
9  * This code was written by Daniel Watt, Walter Deignan, Ryan Gabrys,
10  * Alan Perez-Rathke and Ram Vedam.
11  *
12  * Redistribution and use in source and binary forms, with or
13  * without modification, are permitted provided that the following
14  * conditions are met:
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.
21  *
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
34  * OF SUCH DAMAGE.
35  */
36
37 #include "cd9660.h"
38 #include "cd9660_eltorito.h"
39 #include <util.h>
40
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
43
44 #ifdef DEBUG
45 #define ELTORITO_DPRINTF(__x)   printf __x
46 #else
47 #define ELTORITO_DPRINTF(__x)
48 #endif
49
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);
56 #if 0
57 static u_char cd9660_boot_get_system_type(struct cd9660_boot_image *);
58 #endif
59
60 int
61 cd9660_add_boot_disk(iso9660_disk *diskStructure, const char *boot_info)
62 {
63         struct stat stbuf;
64         const char *mode_msg;
65         char *temp;
66         char *sysname;
67         char *filename;
68         struct cd9660_boot_image *new_image, *tmp_image;
69
70         assert(boot_info != NULL);
71
72         if (*boot_info == '\0') {
73                 warnx("Error: Boot disk information must be in the "
74                       "format 'system;filename'");
75                 return 0;
76         }
77
78         /* First decode the boot information */
79         temp = estrdup(boot_info);
80
81         sysname = temp;
82         filename = strchr(sysname, ';');
83         if (filename == NULL) {
84                 warnx("supply boot disk information in the format "
85                     "'system;filename'");
86                 free(temp);
87                 return 0;
88         }
89
90         *filename++ = '\0';
91
92         if (diskStructure->verbose_level > 0) {
93                 printf("Found bootdisk with system %s, and filename %s\n",
94                     sysname, filename);
95         }
96         new_image = ecalloc(1, sizeof(*new_image));
97         new_image->loadSegment = 0;     /* default for now */
98
99         /* Decode System */
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;
107         else {
108                 warnx("boot disk system must be "
109                       "i386, powerpc, macppc, or mac68k");
110                 free(temp);
111                 free(new_image);
112                 return 0;
113         }
114
115
116         new_image->filename = estrdup(filename);
117
118         free(temp);
119
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);
124
125         switch (stbuf.st_size) {
126         case 1440 * 1024:
127                 new_image->targetMode = ET_MEDIA_144FDD;
128                 mode_msg = "Assigned boot image to 1.44 emulation mode";
129                 break;
130         case 1200 * 1024:
131                 new_image->targetMode = ET_MEDIA_12FDD;
132                 mode_msg = "Assigned boot image to 1.2 emulation mode";
133                 break;
134         case 2880 * 1024:
135                 new_image->targetMode = ET_MEDIA_288FDD;
136                 mode_msg = "Assigned boot image to 2.88 emulation mode";
137                 break;
138         default:
139                 new_image->targetMode = ET_MEDIA_NOEM;
140                 mode_msg = "Assigned boot image to no emulation mode";
141                 break;
142         }
143
144         if (diskStructure->verbose_level > 0)
145                 printf("%s\n", mode_msg);
146
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);
154         }
155         new_image->sector = -1;
156         /* Bootable by default */
157         new_image->bootable = ET_BOOTABLE;
158         /* Add boot disk */
159
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)
163                         break;
164         }
165
166         if (tmp_image == NULL) {
167                 TAILQ_INSERT_HEAD(&diskStructure->boot_images, new_image,
168                     image_list);
169         } else
170                 TAILQ_INSERT_BEFORE(tmp_image, new_image, image_list);
171
172         new_image->serialno = diskStructure->image_serialno++;
173
174         /* TODO : Need to do anything about the boot image in the tree? */
175         diskStructure->is_bootable = 1;
176
177         return 1;
178 }
179
180 int
181 cd9660_eltorito_add_boot_option(iso9660_disk *diskStructure,
182     const char *option_string, const char *value)
183 {
184         char *eptr;
185         struct cd9660_boot_image *image;
186
187         assert(option_string != NULL);
188
189         /* Find the last image added */
190         TAILQ_FOREACH(image, &diskStructure->boot_images, image_list) {
191                 if (image->serialno + 1 == diskStructure->image_serialno)
192                         break;
193         }
194         if (image == NULL)
195                 errx(EXIT_FAILURE, "Attempted to add boot option, "
196                     "but no boot images have been specified");
197
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__);
208                         return 0;
209                 }
210         } else {
211                 return 0;
212         }
213         return 1;
214 }
215
216 static struct boot_catalog_entry *
217 cd9660_init_boot_catalog_entry(void)
218 {
219         return ecalloc(1, sizeof(struct boot_catalog_entry));
220 }
221
222 static struct boot_catalog_entry *
223 cd9660_boot_setup_validation_entry(char sys)
224 {
225         struct boot_catalog_entry *entry;
226         boot_catalog_validation_entry *ve;
227         int16_t checksum;
228         unsigned char *csptr;
229         int i;
230         entry = cd9660_init_boot_catalog_entry();
231
232         ve = &entry->entry_data.VE;
233
234         ve->header_id[0] = 1;
235         ve->platform_id[0] = sys;
236         ve->key[0] = 0x55;
237         ve->key[1] = 0xAA;
238
239         /* Calculate checksum */
240         checksum = 0;
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];
246         }
247         checksum = -checksum;
248         cd9660_721(checksum, ve->checksum);
249
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));
253         return entry;
254 }
255
256 static struct boot_catalog_entry *
257 cd9660_boot_setup_default_entry(struct cd9660_boot_image *disk)
258 {
259         struct boot_catalog_entry *default_entry;
260         boot_catalog_initial_entry *ie;
261
262         default_entry = cd9660_init_boot_catalog_entry();
263         if (default_entry == NULL)
264                 return NULL;
265
266         ie = &default_entry->entry_data.IE;
267
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);
274
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;
281 }
282
283 static struct boot_catalog_entry *
284 cd9660_boot_setup_section_head(char platform)
285 {
286         struct boot_catalog_entry *entry;
287         boot_catalog_section_header *sh;
288
289         entry = cd9660_init_boot_catalog_entry();
290         if (entry == NULL)
291                 return NULL;
292
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;
298         return entry;
299 }
300
301 static struct boot_catalog_entry *
302 cd9660_boot_setup_section_entry(struct cd9660_boot_image *disk)
303 {
304         struct boot_catalog_entry *entry;
305         boot_catalog_section_entry *se;
306         if ((entry = cd9660_init_boot_catalog_entry()) == NULL)
307                 return NULL;
308
309         se = &entry->entry_data.SE;
310
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);
316         return entry;
317 }
318
319 #if 0
320 static u_char
321 cd9660_boot_get_system_type(struct cd9660_boot_image *disk)
322 {
323         /*
324                 For hard drive booting, we need to examine the MBR to figure
325                 out what the partition type is
326         */
327         return 0;
328 }
329 #endif
330
331 /*
332  * Set up the BVD, Boot catalog, and the boot entries, but do no writing
333  */
334 int
335 cd9660_setup_boot(iso9660_disk *diskStructure, int first_sector)
336 {
337         int sector;
338         int used_sectors;
339         int num_entries = 0;
340         int catalog_sectors;
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;
344
345         headp = NULL;
346         x86_head = mac_head = ppc_head = NULL;
347
348         /* If there are no boot disks, don't bother building boot information */
349         if (TAILQ_EMPTY(&diskStructure->boot_images))
350                 return 0;
351
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);
357
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)
362                 return -1;
363
364         /*
365          * Count how many boot images there are,
366          * and how many sectors they consume.
367          */
368         num_entries = 1;
369         used_sectors = 0;
370
371         TAILQ_FOREACH(tmp_disk, &diskStructure->boot_images, image_list) {
372                 used_sectors += tmp_disk->num_sectors;
373
374                 /* One default entry per image */
375                 num_entries++;
376         }
377         catalog_sectors = howmany(num_entries * 0x20, diskStructure->sectorSize);
378         used_sectors += catalog_sectors;
379
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);
384         }
385
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;
391         }
392
393         LIST_INSERT_HEAD(&diskStructure->boot_entries, valid_entry, ll_struct);
394
395         /* Step 1b: Initial/default entry */
396         /* TODO : PARAM */
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");
401                 return -1;
402         }
403
404         LIST_INSERT_AFTER(valid_entry, default_entry, ll_struct);
405
406         /* Todo: multiple default entries? */
407
408         tmp_disk = TAILQ_NEXT(tmp_disk, image_list);
409
410         temp = default_entry;
411
412         /* If multiple boot images are given : */
413         while (tmp_disk != NULL) {
414                 /* Step 2: Section header */
415                 switch (tmp_disk->system) {
416                 case ET_SYS_X86:
417                         headp = &x86_head;
418                         break;
419                 case ET_SYS_PPC:
420                         headp = &ppc_head;
421                         break;
422                 case ET_SYS_MAC:
423                         headp = &mac_head;
424                         break;
425                 default:
426                         warnx("%s: internal error: unknown system type",
427                             __func__);
428                         return -1;
429                 }
430
431                 if (*headp == NULL) {
432                         head =
433                             cd9660_boot_setup_section_head(tmp_disk->system);
434                         if (head == NULL) {
435                                 warnx("Error: memory allocation failed in "
436                                       "cd9660_setup_boot");
437                                 return -1;
438                         }
439                         LIST_INSERT_AFTER(default_entry, head, ll_struct);
440                         *headp = head;
441                 } else
442                         head = *headp;
443
444                 head->entry_data.SH.num_section_entries[0]++;
445
446                 /* Step 2a: Section entry and extensions */
447                 temp = cd9660_boot_setup_section_entry(tmp_disk);
448                 if (temp == NULL) {
449                         warn("%s: cd9660_boot_setup_section_entry", __func__);
450                         return -1;
451                 }
452
453                 while ((next = LIST_NEXT(head, ll_struct)) != NULL &&
454                        next->entry_type == ET_ENTRY_SE)
455                         head = next;
456
457                 LIST_INSERT_AFTER(head, temp, ll_struct);
458                 tmp_disk = TAILQ_NEXT(tmp_disk, image_list);
459         }
460
461         /* TODO: Remaining boot disks when implemented */
462
463         return first_sector + used_sectors;
464 }
465
466 int
467 cd9660_setup_boot_volume_descriptor(iso9660_disk *diskStructure,
468     volume_descriptor *bvd)
469 {
470         boot_volume_descriptor *bvdData =
471             (boot_volume_descriptor*)bvd->volumeDescriptorData;
472
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;
480         return 1;
481 }
482
483 static int
484 cd9660_write_mbr_partition_entry(FILE *fd, int idx, off_t sector_start,
485     off_t nsectors, int type)
486 {
487         uint8_t val;
488         uint32_t lba;
489
490         if (fseeko(fd, (off_t)(idx) * 16 + 0x1be, SEEK_SET) == -1)
491                 err(1, "fseeko");
492         
493         val = 0x80; /* Bootable */
494         fwrite(&val, sizeof(val), 1, fd);
495
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);
500
501         val = type; /* Part type */
502         fwrite(&val, sizeof(val), 1, fd);
503
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);
508
509         /* LBA extent */
510         lba = htole32(sector_start);
511         fwrite(&lba, sizeof(lba), 1, fd);
512         lba = htole32(nsectors);
513         fwrite(&lba, sizeof(lba), 1, fd);
514
515         return 0;
516 }
517
518 static int
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)
522 {
523         uint32_t apm32, part_status;
524         uint16_t apm16;
525
526         /* See Apple Tech Note 1189 for the details about the pmPartStatus
527          * flags.
528          * Below the flags which are default:
529          * - IsValid     0x01
530          * - IsAllocated 0x02
531          * - IsReadable  0x10
532          * - IsWritable  0x20
533          */
534         part_status = 0x01 | 0x02 | 0x10 | 0x20;
535
536         if (fseeko(fd, (off_t)(idx + 1) * sector_size, SEEK_SET) == -1)
537                 err(1, "fseeko");
538
539         /* Signature */
540         apm16 = htobe16(0x504d);
541         fwrite(&apm16, sizeof(apm16), 1, fd);
542         apm16 = 0;
543         fwrite(&apm16, sizeof(apm16), 1, fd);
544
545         /* Total number of partitions */
546         apm32 = htobe32(total_partitions);
547         fwrite(&apm32, sizeof(apm32), 1, fd);
548         /* Bounds */
549         apm32 = htobe32(sector_start);
550         fwrite(&apm32, sizeof(apm32), 1, fd);
551         apm32 = htobe32(nsectors);
552         fwrite(&apm32, sizeof(apm32), 1, fd);
553
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);
558
559         apm32 = 0;
560         /* pmLgDataStart */
561         fwrite(&apm32, sizeof(apm32), 1, fd);
562         /* pmDataCnt */ 
563         apm32 = htobe32(nsectors);
564         fwrite(&apm32, sizeof(apm32), 1, fd);
565         /* pmPartStatus */
566         apm32 = htobe32(part_status);
567         fwrite(&apm32, sizeof(apm32), 1, fd);
568
569         return 0;
570 }
571
572 int
573 cd9660_write_boot(iso9660_disk *diskStructure, FILE *fd)
574 {
575         struct boot_catalog_entry *e;
576         struct cd9660_boot_image *t;
577         int apm_partitions = 0;
578         int mbr_partitions = 0;
579
580         /* write boot catalog */
581         if (fseeko(fd, (off_t)diskStructure->boot_catalog_sector *
582             diskStructure->sectorSize, SEEK_SET) == -1)
583                 err(1, "fseeko");
584
585         if (diskStructure->verbose_level > 0) {
586                 printf("Writing boot catalog to sector %" PRId64 "\n",
587                     diskStructure->boot_catalog_sector);
588         }
589         LIST_FOREACH(e, &diskStructure->boot_entries, ll_struct) {
590                 if (diskStructure->verbose_level > 0) {
591                         printf("Writing catalog entry of type %d\n",
592                             e->entry_type);
593                 }
594                 /*
595                  * It doesn't matter which one gets written
596                  * since they are the same size
597                  */
598                 fwrite(&(e->entry_data.VE), 1, 32, fd);
599         }
600         if (diskStructure->verbose_level > 0)
601                 printf("Finished writing boot catalog\n");
602
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);
608                 }
609                 cd9660_copy_file(diskStructure, fd, t->sector, t->filename);
610
611                 if (t->system == ET_SYS_MAC) 
612                         apm_partitions++;
613                 if (t->system == ET_SYS_PPC) 
614                         mbr_partitions++;
615         }
616
617         /* some systems need partition tables as well */
618         if (mbr_partitions > 0 || diskStructure->chrp_boot) {
619                 uint16_t sig;
620
621                 fseek(fd, 0x1fe, SEEK_SET);
622                 sig = htole16(0xaa55);
623                 fwrite(&sig, sizeof(sig), 1, fd);
624
625                 mbr_partitions = 0;
626
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);
632
633                 /* Write all partition entries */
634                 TAILQ_FOREACH(t, &diskStructure->boot_images, image_list) {
635                         if (t->system != ET_SYS_PPC)
636                                 continue;
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 */);
641                 }
642         }
643
644         if (apm_partitions > 0) {
645                 /* Write DDR and global APM info */
646                 uint32_t apm32;
647                 uint16_t apm16;
648                 int total_parts;
649
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);
660                 /* Device type/id */
661                 apm16 = htobe16(1);
662                 fwrite(&apm16, sizeof(apm16), 1, fd);
663                 fwrite(&apm16, sizeof(apm16), 1, fd);
664
665                 /* Count total needed entries */
666                 total_parts = 2 + apm_partitions; /* Self + ISO9660 */
667
668                 /* Write self-descriptor */
669                 cd9660_write_apm_partition_entry(fd, 0, total_parts, 1,
670                     total_parts, 512, "Apple", "Apple_partition_map");
671
672                 /* Write all partition entries */
673                 apm_partitions = 0;
674                 TAILQ_FOREACH(t, &diskStructure->boot_images, image_list) {
675                         if (t->system != ET_SYS_MAC)
676                                 continue;
677
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");
683                 }
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",
688                     "CD_ROM_Mode_1");
689         }
690
691         return 0;
692 }
693