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

syslinux/iso2exe: add zimage support
author Pascal Bellard <pascal.bellard@slitaz.org>
date Thu Mar 06 19:57:41 2014 +0000 (2014-03-06)
parents ada914860f33
children 9c889f88b9fd
line source
1 #include <sys/types.h>
2 #include <fcntl.h>
3 #include <stdio.h>
4 #ifdef WIN32
5 #include <windows.h>
6 #endif
7 #include "iso2exe.h"
9 static int fd, status = 1;
10 static char tazlitoinfo[10*1024];
11 #define buffer tazlitoinfo
12 #define BUFFERSZ 2048
14 static void readsector(unsigned long sector)
15 {
16 if (lseek(fd, sector * BUFFERSZ, SEEK_SET) == -1 ||
17 read(fd, buffer, BUFFERSZ) != BUFFERSZ) {
18 puts("Read sector failure.\n");
19 exit(1);
20 }
21 }
23 static char *install(char *filename)
24 {
25 #define heads 64
26 #define sectors 32
27 #define partition 446
28 #define trksz (512 * heads * sectors)
29 unsigned long size, catalog, lba;
30 int cylinders, i, j, isohybrid;
31 unsigned n;
32 #ifndef WIN32
33 char *bootiso;
34 for (bootiso = (char *) install;
35 bootiso[0] != 'M' || bootiso[1] != 'Z' || bootiso[2] != 0xEB;
36 bootiso++) if (bootiso < (char *) install) return "No bootiso data.\n";
37 #endif
38 if (!filename)
39 return "Usage: isohybrid.exe file.iso\n";
40 fd = open(filename,O_RDWR|O_BINARY);
41 if (fd == -1)
42 return "Can't open rw the iso file.\n";
44 // Install hybridiso boot sector
45 readsector(17UL);
46 if (strncmp(buffer+7, "EL TORITO SPECIFICATION", 23))
47 return "No EL TORITO signature.\n";
48 catalog = * (unsigned long *) (buffer + 71);
49 readsector(catalog);
50 if (* (unsigned long *) buffer != 1 ||
51 * (unsigned long *) (buffer + 30) != 0x88AA55)
52 return "Invalid boot catalog.\n";
53 lba = * (unsigned long *) (buffer + 40);
54 readsector(lba);
55 if (* (unsigned long *) (buffer + 64) != 1886961915)
56 return "No isolinux.bin hybrid signature.\n";
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 // Copy the partition table
72 memcpy(bootiso + 0x1BE, bootiso + isohybrid + 0x1BE, 66);
74 // Install iso2exe boot sector
75 * (unsigned short *) (bootiso + 26) = rand();
77 // read tazlito flavor data
78 lseek(fd, 1024UL, SEEK_SET);
79 read(fd, tazlitoinfo, sizeof(tazlitoinfo));
81 // Update iso image
82 n = (bootiso[69] + 1) * 512;
83 lseek(fd, 0UL, SEEK_SET);
84 write(fd, bootiso, n); // EXE/PE + isohybrid mbr
85 write(fd, tazlitoinfo, ((0x8000U - BOOTISOSZ) > sizeof(tazlitoinfo))
86 ? sizeof(tazlitoinfo) : (0x8000U - BOOTISOSZ));
87 write(fd, bootiso + n, BOOTISOSZ - n); // COM + rootfs + EXE/DOS
89 // Compute the checksum
90 lseek(fd, 0UL, SEEK_SET);
91 for (i = 66, n = 0, j = 0; j < 16; j++, i = 0) {
92 if (read(fd, buffer, BUFFERSZ) != BUFFERSZ)
93 goto nochksum;
94 for (; i < BUFFERSZ; i += 2)
95 n += * (unsigned short *) (buffer + i);
96 }
97 * (unsigned short *) (bootiso + 64) = -n;
98 lseek(fd, 0UL, SEEK_SET);
99 write(fd, bootiso, 512);
100 nochksum:
101 close(fd);
102 status = 0;
103 return "Now you can create a USB key with your .iso file.\n"
104 "Simply rename it to a .exe file and run it.\n";
105 }
107 int main(int argc, char *argv[])
108 {
109 puts(install(argv[1]));
110 #ifdef WIN32
111 Sleep(2000);
112 #endif
113 return status;
114 }