wok view syslinux/stuff/iso2exe/iso2exe.c @ rev 14262

syslinux/iso2exe: fix isohybrib.exe dos
author Pascal Bellard <pascal.bellard@slitaz.org>
date Sat Mar 30 20:33:34 2013 +0100 (2013-03-30)
parents 5ed4d6b2d690
children bf8be127c60b
line source
1 #include <sys/types.h>
2 #include <fcntl.h>
3 #include <stdio.h>
4 #include "iso2exe.h"
6 static int fd;
7 static char buffer[2048];
9 static void quit(char *msg)
10 {
11 fprintf(stderr,"%s.\n", msg);
12 exit(1);
13 }
15 static void readsector(unsigned long sector)
16 {
17 if (lseek(fd, sector * sizeof(buffer), SEEK_SET) == -1 ||
18 read(fd, buffer, sizeof(buffer)) != sizeof(buffer))
19 quit("read sector failure");
20 }
22 int main(int argc, char *argv[])
23 {
24 #define heads 64
25 #define sectors 32
26 #define partition 446
27 #define trksz (512 * heads * sectors)
28 unsigned long size, catalog, lba;
29 int cylinders, i, j, isohybrid;
30 unsigned n;
31 char tazlitoinfo[10*1024];
32 #ifndef WIN32
33 char *bootiso;
34 for (bootiso = (char *) main;
35 bootiso[0] != 'M' || bootiso[1] != 'Z' || bootiso[2] != 0xEB;
36 bootiso++) if (bootiso < (char *) main) quit("bootiso not found");
37 #endif
38 if (argc < 2)
39 quit("Usage : isohybrid.exe file.iso");
40 fd = open(argv[1],O_RDWR|O_BINARY);
41 if (fd == -1)
42 quit("Can't open rw");
44 // Install hybridiso boot sector
45 readsector(17UL);
46 if (strncmp(buffer+7, "EL TORITO SPECIFICATION", 23))
47 quit("No EL TORITO boot record found");
48 catalog = * (unsigned long *) (buffer + 71);
49 readsector(catalog);
50 if (* (unsigned long *) buffer != 1 ||
51 * (unsigned long *) (buffer + 30) != 0x88AA55)
52 quit("invalid boot catalog.");
53 lba = * (unsigned long *) (buffer + 40);
54 readsector(lba);
55 if (* (unsigned long *) (buffer + 64) != 1886961915)
56 quit("no isolinux.bin hybrid signature in bootloader");
57 isohybrid = bootiso[69] * 512;
58 * (unsigned long *) &bootiso[isohybrid + 432] = lba * 4;
59 * (unsigned long *) &bootiso[isohybrid + 440] = rand();
60 * (unsigned long *) &bootiso[isohybrid + partition] = 0x10080;
61 * (unsigned short *) &bootiso[isohybrid + 510] = 0xAA55;
62 size = lseek(fd, 0UL, SEEK_END);
63 cylinders = (size + trksz - 1) / trksz;
64 bootiso[isohybrid + partition + 4] = 23; // "Windows hidden IFS"
65 bootiso[isohybrid + partition + 5] = heads - 1;
66 bootiso[isohybrid + partition + 6] = (((cylinders - 1) & 0x300) >> 2) + sectors;
67 bootiso[isohybrid + partition + 7] = (cylinders - 1) & 0xFF;
68 * (unsigned long *) &bootiso[isohybrid + partition + 8] = 0;
69 * (unsigned long *) &bootiso[isohybrid + partition + 12] = cylinders * sectors * heads;
71 // Install iso2exe boot sector
72 * (unsigned short *) (bootiso + 26) = rand();
74 // read tazlito flavor data
75 lseek(fd, 1024UL, SEEK_SET);
76 read(fd, tazlitoinfo, sizeof(tazlitoinfo));
78 // Update iso image
79 n = (bootiso[69] + 1) * 512;
80 lseek(fd, 0UL, SEEK_SET);
81 write(fd, bootiso, n); // EXE/PE + isohybrid mbr
82 write(fd, tazlitoinfo, ((0x8000U - BOOTISOSZ) > sizeof(tazlitoinfo))
83 ? sizeof(tazlitoinfo) : (0x8000U - BOOTISOSZ));
84 write(fd, bootiso + n, BOOTISOSZ - n); // COM + rootfs + EXE/DOS
86 // Compute the checksum
87 lseek(fd, 0UL, SEEK_SET);
88 for (i = 66, n = 0, j = 0; j < 16; j++, i = 0) {
89 if (read(fd, buffer, sizeof(buffer)) != sizeof(buffer))
90 goto nochksum;
91 for (; i < sizeof(buffer); i += 2)
92 n += * (unsigned short *) (buffer + i);
93 }
94 * (unsigned short *) (bootiso + 64) = -n;
95 lseek(fd, 0UL, SEEK_SET);
96 write(fd, bootiso, 512);
97 nochksum:
98 close(fd);
99 printf("Note you can create a USB key with %s.\n"
100 "Simply rename it to a .exe file and run it.\n", argv[1]);
101 }