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

syslinux/iso2exe: use posixovl -F
author Pascal Bellard <pascal.bellard@slitaz.org>
date Sun Mar 24 23:14:15 2013 +0100 (2013-03-24)
parents a26ba54f3ea7
children 5ed4d6b2d690
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;
30 unsigned n;
31 #ifndef WIN32
32 char *bootiso;
33 for (bootiso = (char *) main;
34 bootiso[0] != 'M' || bootiso[1] != 'Z' || bootiso[2] != 0xEB;
35 bootiso++) if (bootiso < (char *) main) quit("bootiso not found");
36 #endif
37 if (argc < 2)
38 quit("Usage : isohybrid.exe file.iso");
39 fd = open(argv[1],O_RDWR|O_BINARY);
40 if (fd == -1)
41 quit("Can't open rw");
43 // Install hybridiso boot sector
44 readsector(17UL);
45 if (strncmp(buffer+7, "EL TORITO SPECIFICATION", 23))
46 quit("No EL TORITO boot record found");
47 catalog = * (unsigned long *) (buffer + 71);
48 readsector(catalog);
49 if (* (unsigned long *) buffer != 1 ||
50 * (unsigned long *) (buffer + 30) != 0x88AA55)
51 quit("invalid boot catalog.");
52 lba = * (unsigned long *) (buffer + 40);
53 readsector(lba);
54 if (* (unsigned long *) (buffer + 64) != 1886961915)
55 quit("no isolinux.bin hybrid signature in bootloader");
56 * (unsigned long *) &bootiso[512 + 432] = lba * 4;
57 * (unsigned long *) &bootiso[512 + 440] = rand();
58 * (unsigned long *) &bootiso[512 + partition] = 0x10080;
59 * (unsigned short *) &bootiso[512 + 510] = 0xAA55;
60 size = lseek(fd, 0UL, SEEK_END);
61 cylinders = (size + trksz - 1) / trksz;
62 bootiso[512 + partition + 4] = 23; // "Windows hidden IFS"
63 bootiso[512 + partition + 5] = heads - 1;
64 bootiso[512 + partition + 6] = (((cylinders - 1) & 0x300) >> 2) + sectors;
65 bootiso[512 + partition + 7] = (cylinders - 1) & 0xFF;
66 * (unsigned long *) &bootiso[512 + partition + 8] = 0;
67 * (unsigned long *) &bootiso[512 + partition + 12] = cylinders * sectors * heads;
69 // Install iso2exe boot sector
70 memcpy(bootiso + 512 - 66, bootiso + 1024 - 66, 66);
71 * (unsigned short *) (bootiso + 26) = rand();
73 // Update iso image
74 lseek(fd, 0UL, SEEK_SET);
75 write(fd, bootiso, 1024);
76 lseek(fd, 0x8400UL - BOOTISOSZ, SEEK_SET);
77 write(fd, bootiso + 1024, BOOTISOSZ - 1024);
79 // Compute the checksum
80 lseek(fd, 0UL, SEEK_SET);
81 for (i = 66, n = 0, j = 0; j < 16; j++, i = 0) {
82 if (read(fd, buffer, sizeof(buffer)) != sizeof(buffer))
83 goto nochksum;
84 for (; i < sizeof(buffer); i += 2)
85 n += * (unsigned short *) (buffer + i);
86 }
87 * (unsigned short *) (bootiso + 64) = -n;
88 lseek(fd, 0UL, SEEK_SET);
89 write(fd, bootiso, 512);
90 nochksum:
91 close(fd);
92 }