2 * isohybrid.c: Post process an ISO 9660 image generated with mkisofs or
3 * genisoimage to allow - hybrid booting - as a CD-ROM or as a hard
6 * This is based on the original Perl script written by H. Peter Anvin. The
7 * rewrite in C is to avoid dependency on Perl on a system under installation.
9 * Copyright (C) 2010 P J P <pj.pandit@yahoo.co.in>
11 * isohybrid is a free software; you can redistribute it and/or modify it
12 * under the terms of GNU General Public License as published by Free Software
13 * Foundation; either version 2 of the license, or (at your option) any later
16 * isohybrid is distributed in the hope that it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
21 * You should have received a copy of the GNU General Public License along
22 * with isohybrid; if not, see: <http://www.gnu.org/licenses>.
40 #include "isohybrid.h"
43 extern int opterr, optind;
49 uint16_t head = 64; /* 1 <= head <= 256 */
50 uint8_t sector = 32; /* 1 <= sector <= 63 */
52 uint8_t entry = 1; /* partition number: 1 <= entry <= 4 */
53 uint8_t offset = 0; /* partition offset: 0 <= offset <= 64 */
54 uint16_t type = 0x17; /* partition type: 0 <= type <= 255 */
55 uint32_t id = 0; /* MBR: 0 <= id <= 0xFFFFFFFF(4294967296) */
57 uint8_t hd0 = 0; /* 0 <= hd0 <= 2 */
58 uint8_t partok = 0; /* 0 <= partok <= 1 */
61 uint32_t catoffset = 0;
62 uint32_t c = 0, cc = 0, cs = 0;
64 /* boot catalogue parameters */
66 uint16_t de_seg = 0, de_count = 0, de_mbz2 = 0;
67 uint8_t de_boot = 0, de_media = 0, de_sys = 0, de_mbz1 = 0;
73 printf("Usage: %s [OPTIONS] <boot.iso>\n", prog);
80 #define FMT "%-18s %s\n"
86 printf(FMT, " -h <X>", "Number of default geometry heads");
87 printf(FMT, " -s <X>", "Number of default geometry sectors");
88 printf(FMT, " -e --entry", "Specify partition entry number (1-4)");
89 printf(FMT, " -o --offset", "Specify partition offset (default 0)");
90 printf(FMT, " -t --type", "Specify partition type (default 0x17)");
91 printf(FMT, " -i --id", "Specify MBR ID (default random)");
94 printf(FMT, " --forcehd0", "Assume we are loaded as disk ID 0");
95 printf(FMT, " --ctrlhd0", "Assume disk ID 0 if the Ctrl key is pressed");
96 printf(FMT, " --partok", "Allow booting from within a partition");
99 printf(FMT, " -? --help", "Display this help");
100 printf(FMT, " -v --verbose", "Display verbose output");
101 printf(FMT, " -V --version", "Display version information");
104 printf("Report bugs to <pj.pandit@yahoo.co.in>\n");
109 check_option(int argc, char *argv[])
114 const char optstr[] = ":h:s:e:o:t:i:fcp?vV";
115 struct option lopt[] = \
117 { "entry", required_argument, NULL, 'e' },
118 { "offset", required_argument, NULL, 'o' },
119 { "type", required_argument, NULL, 't' },
120 { "id", required_argument, NULL, 'i' },
122 { "forcehd0", no_argument, NULL, 'f' },
123 { "ctrlhd0", no_argument, NULL, 'c' },
124 { "partok", no_argument, NULL, 'p'},
126 { "help", no_argument, NULL, '?' },
127 { "verbose", no_argument, NULL, 'v' },
128 { "version", no_argument, NULL, 'V' },
134 while ((n = getopt_long_only(argc, argv, optstr, lopt, &ind)) != -1)
139 head = strtoul(optarg, &err, 0);
140 if (head < 1 || head > 256)
141 errx(1, "invalid head: `%s', 1 <= head <= 256", optarg);
145 sector = strtoul(optarg, &err, 0);
146 if (sector < 1 || sector > 63)
147 errx(1, "invalid sector: `%s', 1 <= sector <= 63", optarg);
151 entry = strtoul(optarg, &err, 0);
152 if (entry < 1 || entry > 4)
153 errx(1, "invalid entry: `%s', 1 <= entry <= 4", optarg);
157 offset = strtoul(optarg, &err, 0);
158 if (*err || offset > 64)
159 errx(1, "invalid offset: `%s', 0 <= offset <= 64", optarg);
163 type = strtoul(optarg, &err, 0);
164 if (*err || type > 255)
165 errx(1, "invalid type: `%s', 0 <= type <= 255", optarg);
169 id = strtoul(optarg, &err, 0);
171 errx(1, "invalid id: `%s'", optarg);
191 printf("%s version %s\n", prog, VERSION);
195 errx(1, "option `-%c' takes an argument", optopt);
200 errx(1, "invalid option `-%c', see --help", optopt);
212 lendian_short(const uint16_t s)
219 r = (s & 0x00FF) << 8 | (s & 0xFF00) >> 8;
226 lendian_int(const uint32_t s)
233 r = (s & 0x000000FF) << 24 | (s & 0xFF000000) >> 24
234 | (s & 0x0000FF00) << 8 | (s & 0x00FF0000) >> 8;
241 check_banner(const uint8_t *buf)
243 static const char banner[] = "\0CD001\1EL TORITO SPECIFICATION\0\0\0\0" \
244 "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" \
247 if (!buf || memcmp(buf, banner, sizeof(banner) - 1))
250 buf += sizeof(banner) - 1;
251 memcpy(&catoffset, buf, sizeof(catoffset));
253 catoffset = lendian_int(catoffset);
260 check_catalogue(const uint8_t *buf)
264 for (i = 0, cs = 0; i < 16; i++)
267 memcpy(&ve[i], buf, sizeof(ve[i]));
269 ve[i] = lendian_short(ve[i]);
275 printf("ve[%d]: %d, cs: %d\n", i, ve[i], cs);
277 if ((ve[0] != 0x0001) || (ve[15] != 0xAA55) || (cs & 0xFFFF))
285 read_catalogue(const uint8_t *buf)
287 memcpy(&de_boot, buf++, 1);
288 memcpy(&de_media, buf++, 1);
290 memcpy(&de_seg, buf, 2);
291 de_seg = lendian_short(de_seg);
294 memcpy(&de_sys, buf++, 1);
295 memcpy(&de_mbz1, buf++, 1);
297 memcpy(&de_count, buf, 2);
298 de_count = lendian_short(de_count);
301 memcpy(&de_lba, buf, 4);
302 de_lba = lendian_int(de_lba);
305 memcpy(&de_mbz2, buf, 2);
306 de_mbz2 = lendian_short(de_mbz2);
309 if (de_boot != 0x88 || de_media != 0
310 || (de_seg != 0 && de_seg != 0x7C0) || de_count != 4)
318 display_catalogue(void)
320 printf("de_boot: %hhu\n", de_boot);
321 printf("de_media: %hhu\n", de_media);
322 printf("de_seg: %hu\n", de_seg);
323 printf("de_sys: %hhu\n", de_sys);
324 printf("de_mbz1: %hhu\n", de_mbz1);
325 printf("de_count: %hu\n", de_count);
326 printf("de_lba: %u\n", de_lba);
327 printf("de_mbz2: %hu\n", de_mbz2);
332 initialise_mbr(uint8_t *mbr)
335 uint32_t psize = 0, tmp = 0;
336 uint8_t ptype = 0, *rbm = mbr;
337 uint8_t bhead = 0, bsect = 0, bcyle = 0;
338 uint8_t ehead = 0, esect = 0, ecyle = 0;
340 extern unsigned char isohdpfx[][MBRSIZE];
342 memcpy(mbr, &isohdpfx[hd0 + 3 * partok], MBRSIZE);
343 mbr += MBRSIZE; /* offset 432 */
345 tmp = lendian_int(de_lba * 4);
346 memcpy(mbr, &tmp, sizeof(tmp));
347 mbr += sizeof(tmp); /* offset 436 */
350 memcpy(mbr, &tmp, sizeof(tmp));
351 mbr += sizeof(tmp); /* offset 440 */
353 tmp = lendian_int(id);
354 memcpy(mbr, &tmp, sizeof(tmp));
355 mbr += sizeof(tmp); /* offset 444 */
359 mbr += 2; /* offset 446 */
362 psize = c * head * sector - offset;
364 bhead = (offset / sector) % head;
365 bsect = (offset % sector) + 1;
366 bcyle = offset / (head * sector);
368 bsect += (bcyle & 0x300) >> 2;
372 esect = sector + (((cc - 1) & 0x300) >> 2);
373 ecyle = (cc - 1) & 0xFF;
375 for (i = 1; i <= 4; i++)
389 tmp = lendian_int(offset);
390 memcpy(&mbr[8], &tmp, sizeof(tmp));
392 tmp = lendian_int(psize);
393 memcpy(&mbr[12], &tmp, sizeof(tmp));
406 display_mbr(const uint8_t *mbr, size_t len)
409 unsigned int i = 0, j = 0;
411 printf("sizeof(MBR): %zu bytes\n", len);
412 for (i = 0; i < len; i++)
427 printf("%c", isprint(mbr[j]) ? mbr[j] : '.');
435 main(int argc, char *argv[])
440 uint8_t *buf = NULL, *bufz = NULL;
441 int cylsize = 0, frac = 0, padding = 0;
443 prog = strcpy(alloca(strlen(argv[0]) + 1), argv[0]);
444 i = check_option(argc, argv);
453 srand(time(NULL) << (getppid() << getpid()));
455 if (!(fp = fopen(argv[0], "r+")))
456 err(1, "could not open file `%s'", argv[0]);
457 if (fseek(fp, 17 * 2048, SEEK_SET))
458 err(1, "%s: seek error - 1", argv[0]);
460 bufz = buf = calloc(BUFSIZE, sizeof(char));
461 if (fread(buf, sizeof(char), BUFSIZE, fp) != BUFSIZE)
462 err(1, "%s", argv[0]);
464 if (check_banner(buf))
465 errx(1, "%s: could not find boot record", argv[0]);
468 printf("catalogue offset: %d\n", catoffset);
470 if (fseek(fp, catoffset * 2048, SEEK_SET))
471 err(1, "%s: seek error - 2", argv[0]);
474 memset(buf, 0, BUFSIZE);
475 if (fread(buf, sizeof(char), BUFSIZE, fp) != BUFSIZE)
476 err(1, "%s", argv[0]);
478 if (check_catalogue(buf))
479 errx(1, "%s: invalid boot catalogue", argv[0]);
482 if (read_catalogue(buf))
483 errx(1, "%s: unexpected boot catalogue parameters", argv[0]);
488 if (fseek(fp, (de_lba * 2048 + 0x40), SEEK_SET))
489 err(1, "%s: seek error - 3", argv[0]);
492 memset(buf, 0, BUFSIZE);
493 if (fread(buf, sizeof(char), 4, fp) != 4)
494 err(1, "%s", argv[0]);
496 if (memcmp(buf, "\xFB\xC0\x78\x70", 4))
497 errx(1, "%s: boot loader does not have an isolinux.bin hybrid " \
498 "signature. Note that isolinux-debug.bin does not support " \
499 "hybrid booting", argv[0]);
501 if (stat(argv[0], &isostat))
502 err(1, "%s", argv[0]);
504 cylsize = head * sector * 512;
505 frac = isostat.st_size % cylsize;
506 padding = (frac > 0) ? cylsize - frac : 0;
509 printf("imgsize: %zu, padding: %d\n", (size_t)isostat.st_size, padding);
511 cc = c = (isostat.st_size + padding) / cylsize;
514 warnx("Warning: more than 1024 cylinders: %d", c);
515 warnx("Not all BIOSes will be able to boot this device");
521 if (fseek(fp, 440, SEEK_SET))
522 err(1, "%s: seek error - 4", argv[0]);
524 if (fread(&id, 1, 4, fp) != 4)
525 err(1, "%s: read error", argv[0]);
527 id = lendian_int(id);
536 printf("id: %u\n", id);
539 memset(buf, 0, BUFSIZE);
540 i = initialise_mbr(buf);
545 if (fseek(fp, 0, SEEK_SET))
546 err(1, "%s: seek error - 5", argv[0]);
548 if (fwrite(buf, sizeof(char), i, fp) != (size_t)i)
549 err(1, "%s: write error - 1", argv[0]);
553 if (fsync(fileno(fp)))
554 err(1, "%s: could not synchronise", argv[0]);
556 if (ftruncate(fileno(fp), isostat.st_size + padding))
557 err(1, "%s: could not add padding bytes", argv[0]);