1 /* $NetBSD: cd9660_eltorito.c,v 1.23 2018/03/28 06:48:55 nonaka 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 * $FreeBSD: head/usr.sbin/makefs/cd9660/cd9660_eltorito.c 331843 2018-03-31 15:04:41Z benno $
37 */
38
39 #include "cd9660.h"
40 #include "cd9660_eltorito.h"
41 #include <util.h>
42
43 #ifdef DEBUG
44 #define ELTORITO_DPRINTF(__x) printf __x
45 #else
46 #define ELTORITO_DPRINTF(__x)
47 #endif
48
49 #include <util.h>
50
51 static struct boot_catalog_entry *cd9660_init_boot_catalog_entry(void);
52 static struct boot_catalog_entry *cd9660_boot_setup_validation_entry(char);
53 static struct boot_catalog_entry *cd9660_boot_setup_default_entry(
54 struct cd9660_boot_image *);
55 static struct boot_catalog_entry *cd9660_boot_setup_section_head(char);
56 #if 0
57 static u_char cd9660_boot_get_system_type(struct cd9660_boot_image *);
58 #endif
59
60 static struct cd9660_boot_image *default_boot_image;
61
62 int
cd9660_add_boot_disk(iso9660_disk * diskStructure,const char * boot_info)63 cd9660_add_boot_disk(iso9660_disk *diskStructure, const char *boot_info)
64 {
65 struct stat stbuf;
66 const char *mode_msg;
67 char *temp;
68 char *sysname;
69 char *filename;
70 struct cd9660_boot_image *new_image, *tmp_image;
71
72 assert(boot_info != NULL);
73
74 if (*boot_info == '\0') {
75 warnx("Error: Boot disk information must be in the "
76 "format 'system;filename'");
77 return 0;
78 }
79
80 /* First decode the boot information */
81 temp = estrdup(boot_info);
82
83 sysname = temp;
84 filename = strchr(sysname, ';');
85 if (filename == NULL) {
86 warnx("supply boot disk information in the format "
87 "'system;filename'");
88 free(temp);
89 return 0;
90 }
91
92 *filename++ = '\0';
93
94 if (diskStructure->verbose_level > 0) {
95 printf("Found bootdisk with system %s, and filename %s\n",
96 sysname, filename);
97 }
98 new_image = ecalloc(1, sizeof(*new_image));
99 new_image->loadSegment = 0; /* default for now */
100
101 /* Decode System */
102 if (strcmp(sysname, "i386") == 0)
103 new_image->system = ET_SYS_X86;
104 else if (strcmp(sysname, "powerpc") == 0)
105 new_image->system = ET_SYS_PPC;
106 else if (strcmp(sysname, "macppc") == 0 ||
107 strcmp(sysname, "mac68k") == 0)
108 new_image->system = ET_SYS_MAC;
109 else {
110 warnx("boot disk system must be "
111 "i386, powerpc, macppc, or mac68k");
112 free(temp);
113 free(new_image);
114 return 0;
115 }
116
117
118 new_image->filename = estrdup(filename);
119
120 free(temp);
121
122 /* Get information about the file */
123 if (lstat(new_image->filename, &stbuf) == -1)
124 err(EXIT_FAILURE, "%s: lstat(\"%s\")", __func__,
125 new_image->filename);
126
127 switch (stbuf.st_size) {
128 case 1440 * 1024:
129 new_image->targetMode = ET_MEDIA_144FDD;
130 mode_msg = "Assigned boot image to 1.44 emulation mode";
131 break;
132 case 1200 * 1024:
133 new_image->targetMode = ET_MEDIA_12FDD;
134 mode_msg = "Assigned boot image to 1.2 emulation mode";
135 break;
136 case 2880 * 1024:
137 new_image->targetMode = ET_MEDIA_288FDD;
138 mode_msg = "Assigned boot image to 2.88 emulation mode";
139 break;
140 default:
141 new_image->targetMode = ET_MEDIA_NOEM;
142 mode_msg = "Assigned boot image to no emulation mode";
143 break;
144 }
145
146 if (diskStructure->verbose_level > 0)
147 printf("%s\n", mode_msg);
148
149 new_image->size = stbuf.st_size;
150 new_image->num_sectors =
151 howmany(new_image->size, diskStructure->sectorSize) *
152 howmany(diskStructure->sectorSize, 512);
153 if (diskStructure->verbose_level > 0) {
154 printf("New image has size %d, uses %d 512-byte sectors\n",
155 new_image->size, new_image->num_sectors);
156 }
157 new_image->sector = -1;
158 /* Bootable by default */
159 new_image->bootable = ET_BOOTABLE;
160 /* Add boot disk */
161
162 /* Group images for the same platform together. */
163 TAILQ_FOREACH(tmp_image, &diskStructure->boot_images, image_list) {
164 if (tmp_image->system != new_image->system)
165 break;
166 }
167
168 if (tmp_image == NULL) {
169 TAILQ_INSERT_HEAD(&diskStructure->boot_images, new_image,
170 image_list);
171 } else
172 TAILQ_INSERT_BEFORE(tmp_image, new_image, image_list);
173
174 new_image->serialno = diskStructure->image_serialno++;
175
176 new_image->platform_id = new_image->system;
177
178 /* TODO : Need to do anything about the boot image in the tree? */
179 diskStructure->is_bootable = 1;
180
181 /* First boot image is initial/default entry. */
182 if (default_boot_image == NULL)
183 default_boot_image = new_image;
184
185 return 1;
186 }
187
188 int
cd9660_eltorito_add_boot_option(iso9660_disk * diskStructure,const char * option_string,const char * value)189 cd9660_eltorito_add_boot_option(iso9660_disk *diskStructure,
190 const char *option_string, const char *value)
191 {
192 char *eptr;
193 struct cd9660_boot_image *image;
194
195 assert(option_string != NULL);
196
197 /* Find the last image added */
198 TAILQ_FOREACH(image, &diskStructure->boot_images, image_list) {
199 if (image->serialno + 1 == diskStructure->image_serialno)
200 break;
201 }
202 if (image == NULL)
203 errx(EXIT_FAILURE, "Attempted to add boot option, "
204 "but no boot images have been specified");
205
206 if (strcmp(option_string, "no-emul-boot") == 0) {
207 image->targetMode = ET_MEDIA_NOEM;
208 } else if (strcmp(option_string, "no-boot") == 0) {
209 image->bootable = ET_NOT_BOOTABLE;
210 } else if (strcmp(option_string, "hard-disk-boot") == 0) {
211 image->targetMode = ET_MEDIA_HDD;
212 } else if (strcmp(option_string, "boot-load-segment") == 0) {
213 image->loadSegment = strtoul(value, &eptr, 16);
214 if (eptr == value || *eptr != '\0' || errno != ERANGE) {
215 warn("%s: strtoul", __func__);
216 return 0;
217 }
218 } else if (strcmp(option_string, "platformid") == 0) {
219 if (strcmp(value, "efi") == 0)
220 image->platform_id = ET_SYS_EFI;
221 else {
222 warn("%s: unknown platform: %s", __func__, value);
223 return 0;
224 }
225 } else {
226 return 0;
227 }
228 return 1;
229 }
230
231 static struct boot_catalog_entry *
cd9660_init_boot_catalog_entry(void)232 cd9660_init_boot_catalog_entry(void)
233 {
234 return ecalloc(1, sizeof(struct boot_catalog_entry));
235 }
236
237 static struct boot_catalog_entry *
cd9660_boot_setup_validation_entry(char sys)238 cd9660_boot_setup_validation_entry(char sys)
239 {
240 struct boot_catalog_entry *entry;
241 boot_catalog_validation_entry *ve;
242 int16_t checksum;
243 unsigned char *csptr;
244 size_t i;
245 entry = cd9660_init_boot_catalog_entry();
246
247 entry->entry_type = ET_ENTRY_VE;
248 ve = &entry->entry_data.VE;
249
250 ve->header_id[0] = 1;
251 ve->platform_id[0] = sys;
252 ve->key[0] = 0x55;
253 ve->key[1] = 0xAA;
254
255 /* Calculate checksum */
256 checksum = 0;
257 cd9660_721(0, ve->checksum);
258 csptr = (unsigned char*)ve;
259 for (i = 0; i < sizeof(*ve); i += 2) {
260 checksum += (int16_t)csptr[i];
261 checksum += 256 * (int16_t)csptr[i + 1];
262 }
263 checksum = -checksum;
264 cd9660_721(checksum, ve->checksum);
265
266 ELTORITO_DPRINTF(("%s: header_id %d, platform_id %d, key[0] %d, key[1] %d, "
267 "checksum %04x\n", __func__, ve->header_id[0], ve->platform_id[0],
268 ve->key[0], ve->key[1], checksum));
269 return entry;
270 }
271
272 static struct boot_catalog_entry *
cd9660_boot_setup_default_entry(struct cd9660_boot_image * disk)273 cd9660_boot_setup_default_entry(struct cd9660_boot_image *disk)
274 {
275 struct boot_catalog_entry *default_entry;
276 boot_catalog_initial_entry *ie;
277
278 default_entry = cd9660_init_boot_catalog_entry();
279 if (default_entry == NULL)
280 return NULL;
281
282 default_entry->entry_type = ET_ENTRY_IE;
283 ie = &default_entry->entry_data.IE;
284
285 ie->boot_indicator[0] = disk->bootable;
286 ie->media_type[0] = disk->targetMode;
287 cd9660_721(disk->loadSegment, ie->load_segment);
288 ie->system_type[0] = disk->system;
289 cd9660_721(disk->num_sectors, ie->sector_count);
290 cd9660_731(disk->sector, ie->load_rba);
291
292 ELTORITO_DPRINTF(("%s: boot indicator %d, media type %d, "
293 "load segment %04x, system type %d, sector count %d, "
294 "load rba %d\n", __func__, ie->boot_indicator[0],
295 ie->media_type[0], disk->loadSegment, ie->system_type[0],
296 disk->num_sectors, disk->sector));
297 return default_entry;
298 }
299
300 static struct boot_catalog_entry *
cd9660_boot_setup_section_head(char platform)301 cd9660_boot_setup_section_head(char platform)
302 {
303 struct boot_catalog_entry *entry;
304 boot_catalog_section_header *sh;
305
306 entry = cd9660_init_boot_catalog_entry();
307 if (entry == NULL)
308 return NULL;
309
310 entry->entry_type = ET_ENTRY_SH;
311 sh = &entry->entry_data.SH;
312 /*
313 * More by default.
314 * The last one will manually be set to ET_SECTION_HEADER_LAST
315 */
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 *
cd9660_boot_setup_section_entry(struct cd9660_boot_image * disk)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
cd9660_setup_boot(iso9660_disk * diskStructure,int first_sector)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
cd9660_setup_boot_volume_descriptor(iso9660_disk * diskStructure,volume_descriptor * bvd)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
cd9660_write_mbr_partition_entry(FILE * fd,int idx,off_t sector_start,off_t nsectors,int type)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
cd9660_write_apm_partition_entry(FILE * fd,int idx,int total_partitions,off_t sector_start,off_t nsectors,off_t sector_size,const char * part_name,const char * part_type)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 = 0x01 | 0x02 | 0x10 | 0x20;
583
584 if (fseeko(fd, (off_t)(idx + 1) * sector_size, SEEK_SET) == -1)
585 err(1, "fseeko");
586
587 /* Signature */
588 apm16 = htobe16(0x504d);
589 fwrite(&apm16, sizeof(apm16), 1, fd);
590 apm16 = 0;
591 fwrite(&apm16, sizeof(apm16), 1, fd);
592
593 /* Total number of partitions */
594 apm32 = htobe32(total_partitions);
595 fwrite(&apm32, sizeof(apm32), 1, fd);
596 /* Bounds */
597 apm32 = htobe32(sector_start);
598 fwrite(&apm32, sizeof(apm32), 1, fd);
599 apm32 = htobe32(nsectors);
600 fwrite(&apm32, sizeof(apm32), 1, fd);
601
602 fwrite(part_name, strlen(part_name) + 1, 1, fd);
603 fseek(fd, 32 - strlen(part_name) - 1, SEEK_CUR);
604 fwrite(part_type, strlen(part_type) + 1, 1, fd);
605 fseek(fd, 32 - strlen(part_type) - 1, SEEK_CUR);
606
607 apm32 = 0;
608 /* pmLgDataStart */
609 fwrite(&apm32, sizeof(apm32), 1, fd);
610 /* pmDataCnt */
611 apm32 = htobe32(nsectors);
612 fwrite(&apm32, sizeof(apm32), 1, fd);
613 /* pmPartStatus */
614 apm32 = htobe32(part_status);
615 fwrite(&apm32, sizeof(apm32), 1, fd);
616
617 return 0;
618 }
619
620 int
cd9660_write_boot(iso9660_disk * diskStructure,FILE * fd)621 cd9660_write_boot(iso9660_disk *diskStructure, FILE *fd)
622 {
623 struct boot_catalog_entry *e;
624 struct cd9660_boot_image *t;
625 int apm_partitions = 0;
626 int mbr_partitions = 0;
627
628 /* write boot catalog */
629 if (fseeko(fd, (off_t)diskStructure->boot_catalog_sector *
630 diskStructure->sectorSize, SEEK_SET) == -1)
631 err(1, "fseeko");
632
633 if (diskStructure->verbose_level > 0) {
634 printf("Writing boot catalog to sector %" PRId64 "\n",
635 diskStructure->boot_catalog_sector);
636 }
637 LIST_FOREACH(e, &diskStructure->boot_entries, ll_struct) {
638 if (diskStructure->verbose_level > 0) {
639 printf("Writing catalog entry of type %d\n",
640 e->entry_type);
641 }
642 /*
643 * It doesn't matter which one gets written
644 * since they are the same size
645 */
646 fwrite(&(e->entry_data.VE), 1, 32, fd);
647 }
648 if (diskStructure->verbose_level > 0)
649 printf("Finished writing boot catalog\n");
650
651 /* copy boot images */
652 TAILQ_FOREACH(t, &diskStructure->boot_images, image_list) {
653 if (diskStructure->verbose_level > 0) {
654 printf("Writing boot image from %s to sectors %d\n",
655 t->filename, t->sector);
656 }
657 cd9660_copy_file(diskStructure, fd, t->sector, t->filename);
658
659 if (t->system == ET_SYS_MAC)
660 apm_partitions++;
661 if (t->system == ET_SYS_PPC)
662 mbr_partitions++;
663 }
664
665 /* some systems need partition tables as well */
666 if (mbr_partitions > 0 || diskStructure->chrp_boot) {
667 uint16_t sig;
668
669 fseek(fd, 0x1fe, SEEK_SET);
670 sig = htole16(0xaa55);
671 fwrite(&sig, sizeof(sig), 1, fd);
672
673 mbr_partitions = 0;
674
675 /* Write ISO9660 descriptor, enclosing the whole disk */
676 if (diskStructure->chrp_boot)
677 cd9660_write_mbr_partition_entry(fd, mbr_partitions++,
678 0, diskStructure->totalSectors *
679 (diskStructure->sectorSize / 512), 0x96);
680
681 /* Write all partition entries */
682 TAILQ_FOREACH(t, &diskStructure->boot_images, image_list) {
683 if (t->system != ET_SYS_PPC)
684 continue;
685 cd9660_write_mbr_partition_entry(fd, mbr_partitions++,
686 t->sector * (diskStructure->sectorSize / 512),
687 t->num_sectors * (diskStructure->sectorSize / 512),
688 0x41 /* PReP Boot */);
689 }
690 }
691
692 if (apm_partitions > 0) {
693 /* Write DDR and global APM info */
694 uint32_t apm32;
695 uint16_t apm16;
696 int total_parts;
697
698 fseek(fd, 0, SEEK_SET);
699 apm16 = htobe16(0x4552);
700 fwrite(&apm16, sizeof(apm16), 1, fd);
701 /* Device block size */
702 apm16 = htobe16(512);
703 fwrite(&apm16, sizeof(apm16), 1, fd);
704 /* Device block count */
705 apm32 = htobe32(diskStructure->totalSectors *
706 (diskStructure->sectorSize / 512));
707 fwrite(&apm32, sizeof(apm32), 1, fd);
708 /* Device type/id */
709 apm16 = htobe16(1);
710 fwrite(&apm16, sizeof(apm16), 1, fd);
711 fwrite(&apm16, sizeof(apm16), 1, fd);
712
713 /* Count total needed entries */
714 total_parts = 2 + apm_partitions; /* Self + ISO9660 */
715
716 /* Write self-descriptor */
717 cd9660_write_apm_partition_entry(fd, 0, total_parts, 1,
718 total_parts, 512, "Apple", "Apple_partition_map");
719
720 /* Write all partition entries */
721 apm_partitions = 0;
722 TAILQ_FOREACH(t, &diskStructure->boot_images, image_list) {
723 if (t->system != ET_SYS_MAC)
724 continue;
725
726 cd9660_write_apm_partition_entry(fd,
727 1 + apm_partitions++, total_parts,
728 t->sector * (diskStructure->sectorSize / 512),
729 t->num_sectors * (diskStructure->sectorSize / 512),
730 512, "CD Boot", "Apple_Bootstrap");
731 }
732
733 /* Write ISO9660 descriptor, enclosing the whole disk */
734 cd9660_write_apm_partition_entry(fd, 2 + apm_partitions,
735 total_parts, 0, diskStructure->totalSectors *
736 (diskStructure->sectorSize / 512), 512, "ISO9660",
737 "CD_ROM_Mode_1");
738 }
739
740 return 0;
741 }
742