1 /* $NetBSD: cd9660_eltorito.c,v 1.12 2008/07/27 10:29:32 reinoud Exp $ */
2
3 /*
4 * Copyright (c) 2010
5 * Thorsten Glaser <tg@mirbsd.org>
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 #include "cd9660.h"
37 #include "cd9660_eltorito.h"
38
39 #include <sys/cdefs.h>
40 #if defined(__RCSID) && !defined(__lint)
41 __RCSID("$NetBSD: cd9660_eltorito.c,v 1.12 2008/07/27 10:29:32 reinoud Exp $");
42 __IDSTRING(mbsdid, "$MirOS: src/usr.sbin/makefs/cd9660/cd9660_eltorito.c,v 1.10 2010/03/07 00:21:34 tg Exp $");
43 #endif /* !__lint */
44
45 #ifdef DEBUG
46 #define ELTORITO_DPRINTF(__x) printf __x
47 #else
48 #define ELTORITO_DPRINTF(__x)
49 #endif
50
51 #define cd9660_init_boot_catalog_entry() \
52 ((struct boot_catalog_entry *)(calloc(1, sizeof (struct boot_catalog_entry))))
53 static struct boot_catalog_entry *cd9660_boot_setup_validation_entry(char);
54 static struct boot_catalog_entry *cd9660_boot_setup_default_entry(
55 struct cd9660_boot_image *);
56 static struct boot_catalog_entry *cd9660_boot_setup_section_head(char);
57 #if 0
58 static u_char cd9660_boot_get_system_type(struct cd9660_boot_image *);
59 #endif
60
61 int
cd9660_add_boot_disk(const char * boot_info)62 cd9660_add_boot_disk(const char *boot_info)
63 {
64 struct stat stbuf;
65 const char *mode_msg;
66 char *temp;
67 char *sysname;
68 char *filename;
69 struct cd9660_boot_image *new_image, *tmp_image;
70
71 assert(boot_info != NULL);
72
73 if (*boot_info == '\0') {
74 warnx("Error: Boot disk information must be in the "
75 "format 'system;filename'");
76 return 0;
77 }
78
79 /* First decode the boot information */
80 if ((temp = strdup(boot_info)) == NULL) {
81 warn("%s: strdup", __func__);
82 return 0;
83 }
84
85 sysname = temp;
86 filename = strchr(sysname, ';');
87 if (filename == NULL) {
88 warnx("supply boot disk information in the format "
89 "'system;filename'");
90 free(temp);
91 return 0;
92 }
93
94 *filename++ = '\0';
95
96 if (diskStructure.verbose_level > 0) {
97 printf("Found bootdisk with system %s, and filename %s\n",
98 sysname, filename);
99 }
100 if ((new_image = calloc(1, sizeof (*new_image))) == NULL) {
101 warn("%s: malloc", __func__);
102 free(temp);
103 return 0;
104 }
105 new_image->loadSegment = 0; /* default for now */
106
107 /* Decode System */
108 if (strcmp(sysname, "i386") == 0)
109 new_image->system = ET_SYS_X86;
110 else if (strcmp(sysname, "powerpc") == 0)
111 new_image->system = ET_SYS_PPC;
112 else if (strcmp(sysname, "macppc") == 0 ||
113 strcmp(sysname, "mac68k") == 0)
114 new_image->system = ET_SYS_MAC;
115 else {
116 warnx("boot disk system must be "
117 "i386, powerpc, macppc, or mac68k");
118 free(temp);
119 free(new_image);
120 return 0;
121 }
122
123
124 if ((new_image->filename = strdup(filename)) == NULL) {
125 warn("%s: strdup", __func__);
126 free(temp);
127 free(new_image);
128 return 0;
129 }
130
131 free(temp);
132
133 /* Get information about the file */
134 if (lstat(new_image->filename, &stbuf) == -1)
135 err(EXIT_FAILURE, "%s: lstat(\"%s\")", __func__,
136 new_image->filename);
137
138 switch (stbuf.st_size) {
139 case 1440 * 1024:
140 new_image->targetMode = ET_MEDIA_144FDD;
141 mode_msg = "Assigned boot image to 1.44 emulation mode";
142 break;
143 case 1200 * 1024:
144 new_image->targetMode = ET_MEDIA_12FDD;
145 mode_msg = "Assigned boot image to 1.2 emulation mode";
146 break;
147 case 2880 * 1024:
148 new_image->targetMode = ET_MEDIA_288FDD;
149 mode_msg = "Assigned boot image to 2.88 emulation mode";
150 break;
151 default:
152 new_image->targetMode = ET_MEDIA_NOEM;
153 mode_msg = "Assigned boot image to no emulation mode";
154 break;
155 }
156
157 if (diskStructure.verbose_level > 0)
158 printf("%s\n", mode_msg);
159
160 new_image->size = stbuf.st_size;
161 new_image->num_sectors =
162 howmany(new_image->size, diskStructure.sectorSize) *
163 howmany(diskStructure.sectorSize, 512);
164 if (diskStructure.verbose_level > 0) {
165 printf("New image has size %d, uses %d 512-byte sectors\n",
166 new_image->size, new_image->num_sectors);
167 }
168 new_image->sector = -1;
169 /* Bootable by default */
170 new_image->bootable = ET_BOOTABLE;
171 /* Add boot disk */
172
173 /* Group images for the same platform together. */
174 TAILQ_FOREACH(tmp_image, &diskStructure.boot_images, image_list) {
175 if (tmp_image->system != new_image->system)
176 break;
177 }
178
179 if (tmp_image == NULL) {
180 TAILQ_INSERT_HEAD(&diskStructure.boot_images, new_image,
181 image_list);
182 } else
183 TAILQ_INSERT_BEFORE(tmp_image, new_image, image_list);
184
185 new_image->serialno = diskStructure.image_serialno++;
186
187 /* TODO : Need to do anything about the boot image in the tree? */
188 diskStructure.is_bootable = 1;
189
190 return 1;
191 }
192
193 int
cd9660_eltorito_add_boot_option(const char * option_string,const char * value)194 cd9660_eltorito_add_boot_option(const char *option_string, const char *value)
195 {
196 char *eptr;
197 struct cd9660_boot_image *image;
198
199 assert(option_string != NULL);
200
201 /* Find the last image added */
202 TAILQ_FOREACH(image, &diskStructure.boot_images, image_list) {
203 if (image->serialno + 1 == diskStructure.image_serialno)
204 break;
205 }
206 if (image == NULL)
207 errx(EXIT_FAILURE, "Attempted to add boot option, "
208 "but no boot images have been specified");
209
210 if (strcmp(option_string, "no-emul-boot") == 0) {
211 image->targetMode = ET_MEDIA_NOEM;
212 } else if (strcmp(option_string, "no-boot") == 0) {
213 image->bootable = ET_NOT_BOOTABLE;
214 } else if (strcmp(option_string, "hard-disk-boot") == 0) {
215 image->targetMode = ET_MEDIA_HDD;
216 } else if (strcmp(option_string, "boot-load-segment") == 0) {
217 image->loadSegment = strtoul(value, &eptr, 16);
218 if (eptr == value || *eptr != '\0' || errno != ERANGE) {
219 warn("%s: strtoul", __func__);
220 return 0;
221 }
222 } else if (strcmp(option_string, "boot-info-table") == 0) {
223 image->infoTable = 1;
224 } else {
225 return 0;
226 }
227 return 1;
228 }
229
230 static struct boot_catalog_entry *
cd9660_boot_setup_validation_entry(char sys)231 cd9660_boot_setup_validation_entry(char sys)
232 {
233 struct boot_catalog_entry *entry;
234 boot_catalog_validation_entry *ve;
235 int16_t checksum;
236 unsigned char *csptr;
237 unsigned int i;
238 entry = cd9660_init_boot_catalog_entry();
239
240 if (entry == NULL) {
241 warnx("Error: memory allocation failed in "
242 "cd9660_boot_setup_validation_entry");
243 return 0;
244 }
245 ve = &entry->entry_data.VE;
246
247 ve->header_id[0] = 1;
248 ve->platform_id[0] = sys;
249 ve->key[0] = 0x55;
250 ve->key[1] = 0xAA;
251
252 /* Calculate checksum */
253 checksum = 0;
254 cd9660_721(0, ve->checksum);
255 csptr = (unsigned char*)ve;
256 for (i = 0; i < sizeof(*ve); i += 2) {
257 checksum += (int16_t)csptr[i];
258 checksum += 256 * (int16_t)csptr[i + 1];
259 }
260 checksum = -checksum;
261 cd9660_721(checksum, ve->checksum);
262
263 ELTORITO_DPRINTF(("%s: header_id %d, platform_id %d, key[0] %d, key[1] %d, "
264 "checksum %04x\n", __func__, ve->header_id[0], ve->platform_id[0],
265 ve->key[0], ve->key[1], checksum));
266 return entry;
267 }
268
269 static struct boot_catalog_entry *
cd9660_boot_setup_default_entry(struct cd9660_boot_image * disk)270 cd9660_boot_setup_default_entry(struct cd9660_boot_image *disk)
271 {
272 struct boot_catalog_entry *default_entry;
273 boot_catalog_initial_entry *ie;
274
275 default_entry = cd9660_init_boot_catalog_entry();
276 if (default_entry == NULL)
277 return NULL;
278
279 ie = &default_entry->entry_data.IE;
280
281 ie->boot_indicator[0] = disk->bootable;
282 ie->media_type[0] = disk->targetMode;
283 cd9660_721(disk->loadSegment, ie->load_segment);
284 ie->system_type[0] = disk->system;
285 cd9660_721(disk->num_sectors, ie->sector_count);
286 cd9660_731(disk->sector, ie->load_rba);
287
288 ELTORITO_DPRINTF(("%s: boot indicator %d, media type %d, "
289 "load segment %04x, system type %d, sector count %d, "
290 "load rba %d\n", __func__, ie->boot_indicator[0],
291 ie->media_type[0], disk->loadSegment, ie->system_type[0],
292 disk->num_sectors, disk->sector));
293 return default_entry;
294 }
295
296 static struct boot_catalog_entry *
cd9660_boot_setup_section_head(char platform)297 cd9660_boot_setup_section_head(char platform)
298 {
299 struct boot_catalog_entry *entry;
300 boot_catalog_section_header *sh;
301
302 entry = cd9660_init_boot_catalog_entry();
303 if (entry == NULL)
304 return NULL;
305
306 sh = &entry->entry_data.SH;
307 /* More by default. The last one will manually be set to 0x91 */
308 sh->header_indicator[0] = ET_SECTION_HEADER_MORE;
309 sh->platform_id[0] = platform;
310 sh->num_section_entries[0] = 0;
311 return entry;
312 }
313
314 static struct boot_catalog_entry *
cd9660_boot_setup_section_entry(struct cd9660_boot_image * disk)315 cd9660_boot_setup_section_entry(struct cd9660_boot_image *disk)
316 {
317 struct boot_catalog_entry *entry;
318 boot_catalog_section_entry *se;
319 if ((entry = cd9660_init_boot_catalog_entry()) == NULL)
320 return NULL;
321
322 se = &entry->entry_data.SE;
323
324 se->boot_indicator[0] = ET_BOOTABLE;
325 se->media_type[0] = disk->targetMode;
326 cd9660_721(disk->loadSegment, se->load_segment);
327 cd9660_721(disk->num_sectors, se->sector_count);
328 cd9660_731(disk->sector, se->load_rba);
329 return entry;
330 }
331
332 #if 0
333 static u_char
334 cd9660_boot_get_system_type(struct cd9660_boot_image *disk)
335 {
336 /*
337 For hard drive booting, we need to examine the MBR to figure
338 out what the partition type is
339 */
340 return 0;
341 }
342 #endif
343
344 /*
345 * Set up the BVD, Boot catalog, and the boot entries, but do no writing
346 */
347 int
cd9660_setup_boot(int first_sector)348 cd9660_setup_boot(int first_sector)
349 {
350 int sector;
351 int used_sectors;
352 int num_entries = 0;
353 int catalog_sectors;
354 struct boot_catalog_entry *x86_head, *mac_head, *ppc_head,
355 *valid_entry, *default_entry, *temp, *head, **headp, *next;
356 struct cd9660_boot_image *tmp_disk;
357
358 headp = NULL;
359 x86_head = mac_head = ppc_head = NULL;
360
361 /* If there are no boot disks, don't bother building boot information */
362 if (TAILQ_EMPTY(&diskStructure.boot_images))
363 return 0;
364
365 /* Point to catalog: For now assume it consumes one sector */
366 ELTORITO_DPRINTF(("Boot catalog will go in sector %d\n", first_sector));
367 diskStructure.boot_catalog_sector = first_sector;
368 cd9660_731(first_sector,
369 diskStructure.boot_descriptor->boot_catalog_pointer);
370
371 /* Step 1: Generate boot catalog */
372 /* Step 1a: Validation entry */
373 valid_entry = cd9660_boot_setup_validation_entry(ET_SYS_X86);
374 if (valid_entry == NULL)
375 return -1;
376
377 /*
378 * Count how many boot images there are,
379 * and how many sectors they consume.
380 */
381 num_entries = 1;
382 used_sectors = 0;
383
384 TAILQ_FOREACH(tmp_disk, &diskStructure.boot_images, image_list) {
385 used_sectors += tmp_disk->num_sectors;
386
387 /* One default entry per image */
388 num_entries++;
389 }
390 catalog_sectors = howmany(num_entries * 0x20, diskStructure.sectorSize);
391 used_sectors += catalog_sectors;
392
393 if (diskStructure.verbose_level > 0) {
394 printf("%s: there will be %i entries consuming %i sectors. "
395 "Catalog is %i sectors\n", __func__, num_entries,
396 used_sectors, catalog_sectors);
397 }
398
399 /* Populate sector numbers */
400 sector = first_sector + catalog_sectors;
401 TAILQ_FOREACH(tmp_disk, &diskStructure.boot_images, image_list) {
402 tmp_disk->sector = sector;
403 sector += tmp_disk->num_sectors;
404 }
405
406 LIST_INSERT_HEAD(&diskStructure.boot_entries, valid_entry, ll_struct);
407
408 /* Step 1b: Initial/default entry */
409 /* TODO : PARAM */
410 tmp_disk = TAILQ_FIRST(&diskStructure.boot_images);
411 default_entry = cd9660_boot_setup_default_entry(tmp_disk);
412 if (default_entry == NULL) {
413 warnx("Error: memory allocation failed in cd9660_setup_boot");
414 return -1;
415 }
416
417 LIST_INSERT_AFTER(valid_entry, default_entry, ll_struct);
418
419 /* Todo: multiple default entries? */
420
421 tmp_disk = TAILQ_NEXT(tmp_disk, image_list);
422
423 temp = default_entry;
424
425 /* If multiple boot images are given : */
426 while (tmp_disk != NULL) {
427 /* Step 2: Section header */
428 switch (tmp_disk->system) {
429 case ET_SYS_X86:
430 headp = &x86_head;
431 break;
432 case ET_SYS_PPC:
433 headp = &ppc_head;
434 break;
435 case ET_SYS_MAC:
436 headp = &mac_head;
437 break;
438 default:
439 warnx("%s: internal error: unknown system type",
440 __func__);
441 return -1;
442 }
443
444 if (*headp == NULL) {
445 head =
446 cd9660_boot_setup_section_head(tmp_disk->system);
447 if (head == NULL) {
448 warnx("Error: memory allocation failed in "
449 "cd9660_setup_boot");
450 return -1;
451 }
452 LIST_INSERT_AFTER(default_entry, head, ll_struct);
453 *headp = head;
454 } else
455 head = *headp;
456
457 head->entry_data.SH.num_section_entries[0]++;
458
459 /* Step 2a: Section entry and extensions */
460 temp = cd9660_boot_setup_section_entry(tmp_disk);
461 if (temp == NULL) {
462 warn("%s: cd9660_boot_setup_section_entry", __func__);
463 return -1;
464 }
465
466 while ((next = LIST_NEXT(head, ll_struct)) != NULL &&
467 next->entry_type == ET_ENTRY_SE)
468 head = next;
469
470 LIST_INSERT_AFTER(head, temp, ll_struct);
471 tmp_disk = TAILQ_NEXT(tmp_disk, image_list);
472 }
473
474 /* TODO: Remaining boot disks when implemented */
475
476 return first_sector + used_sectors;
477 }
478
479 int
cd9660_setup_boot_volume_descriptor(volume_descriptor * bvd)480 cd9660_setup_boot_volume_descriptor(volume_descriptor *bvd)
481 {
482 boot_volume_descriptor *bvdData =
483 (boot_volume_descriptor*)bvd->volumeDescriptorData;
484
485 bvdData->boot_record_indicator[0] = ISO_VOLUME_DESCRIPTOR_BOOT;
486 memcpy(bvdData->identifier, ISO_VOLUME_DESCRIPTOR_STANDARD_ID, 5);
487 bvdData->version[0] = 1;
488 memcpy(bvdData->boot_system_identifier, ET_ID, 23);
489 memcpy(bvdData->identifier, ISO_VOLUME_DESCRIPTOR_STANDARD_ID, 5);
490 diskStructure.boot_descriptor =
491 (boot_volume_descriptor*) bvd->volumeDescriptorData;
492 return 1;
493 }
494
495 int
cd9660_write_boot(FILE * fd)496 cd9660_write_boot(FILE *fd)
497 {
498 struct boot_catalog_entry *e;
499 struct cd9660_boot_image *t;
500 long oofs, fofs;
501
502 /* write boot catalog */
503 fseek(fd, diskStructure.boot_catalog_sector * diskStructure.sectorSize,
504 SEEK_SET);
505
506 if (diskStructure.verbose_level > 0) {
507 printf("Writing boot catalog to sector %d\n",
508 diskStructure.boot_catalog_sector);
509 }
510 LIST_FOREACH(e, &diskStructure.boot_entries, ll_struct) {
511 if (diskStructure.verbose_level > 0) {
512 printf("Writing catalog entry of type %d\n",
513 e->entry_type);
514 }
515 /*
516 * It doesnt matter which one gets written
517 * since they are the same size
518 */
519 fwrite(&(e->entry_data.VE), 1, 32, fd);
520 }
521 if (diskStructure.verbose_level > 0)
522 printf("Finished writing boot catalog\n");
523
524 /* copy boot images */
525 TAILQ_FOREACH(t, &diskStructure.boot_images, image_list) {
526 if (diskStructure.verbose_level > 0) {
527 printf("Writing boot image from %s to sectors %d\n",
528 t->filename, t->sector);
529 }
530 cd9660_copy_file(fd, t->sector, t->filename);
531 if (t->infoTable) {
532 if (diskStructure.verbose_level > 0)
533 printf("Writing boot info table into image... ");
534 oofs = ftell(fd);
535 fseek(fd, t->sector * diskStructure.sectorSize, SEEK_SET);
536 fofs = ftell(fd);
537 if (oofs - fofs < 64)
538 printf("failed: boot image too small\n");
539 else {
540 uint32_t cksum_val = 0;
541 long cofs = fofs + 64;
542 unsigned char cksum_buf[4];
543 unsigned char bitable[56];
544 volume_descriptor *vd;
545
546 memset(bitable, 0, sizeof(bitable));
547 fseek(fd, cofs, SEEK_SET);
548
549 while (cofs < oofs) {
550 if (fread(cksum_buf, 1, 4, fd) != 4 ||
551 ferror(fd))
552 break;
553 cofs += 4;
554 cksum_val += cksum_buf[0] |
555 (cksum_buf[1] << 8) |
556 (cksum_buf[2] << 16) |
557 (cksum_buf[3] << 24);
558 }
559
560 vd = diskStructure.firstVolumeDescriptor;
561 while (vd->volumeDescriptorData[0] !=
562 ISO_VOLUME_DESCRIPTOR_PVD)
563 if (vd->volumeDescriptorData[0] ==
564 ISO_VOLUME_DESCRIPTOR_TERMINATOR)
565 break;
566 else if (vd->next)
567 vd = vd->next;
568 else
569 break;
570
571 cd9660_731(vd->sector, bitable + 8 - 8);
572 cd9660_731(t->sector, bitable + 12 - 8);
573 cd9660_731(oofs - fofs, bitable + 16 - 8);
574 cd9660_731(cksum_val, bitable + 20 - 8);
575
576 fseek(fd, fofs + 8, SEEK_SET);
577 fwrite(bitable, 1, 56, fd);
578
579 if (ferror(fd))
580 printf("failed: error during write\n");
581 else
582 printf("ok: PVD %u, boot %u@%u, cksum %08X\n",
583 (uint32_t)vd->sector,
584 (uint32_t)(oofs - fofs),
585 (uint32_t)t->sector, cksum_val);
586 }
587
588 fseek(fd, oofs, SEEK_SET);
589 }
590 }
591
592 return 0;
593 }
594