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

syslinux/iso2exe: add --undo
author Pascal Bellard <pascal.bellard@slitaz.org>
date Wed Dec 17 12:25:20 2014 +0100 (2014-12-17)
parents 9c889f88b9fd
children 054f70cb9bec
line source
1 #ifdef __TURBOC__
2 #include <io.h>
3 #endif
4 #include <sys/types.h>
5 #include <fcntl.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #ifdef WIN32
10 #include <windows.h>
11 #endif
12 #include "iso2exe.h"
14 static int fd, forced, status = 1;
15 static char tazlitoinfo[10*1024];
16 #define buffer tazlitoinfo
17 #define BUFFERSZ 2048
18 #define WORD(n) * (unsigned short *) (n)
19 #define LONG(n) * (unsigned long *) (n)
21 static void readsector(unsigned long sector)
22 {
23 if (lseek(fd, sector * BUFFERSZ, SEEK_SET) == -1 ||
24 read(fd, buffer, BUFFERSZ) != BUFFERSZ) {
25 puts(bootiso+READSECTORERR);
26 exit(1);
27 }
28 }
30 static unsigned install(char *filename)
31 {
32 #define heads 64
33 #define sectors 32
34 #define partition 446
35 #define trksz (512UL * heads * sectors)
36 unsigned long size, catalog, lba;
37 int cylinders, i, j, isohybrid;
38 unsigned n;
39 #ifndef WIN32
40 for (bootiso = (char *) install;
41 bootiso[0] != 'M' || bootiso[1] != 'Z' || bootiso[2] != '\xEB';
42 bootiso++) if (bootiso < (char *) install) {
43 bootiso = "No bootiso data";
44 return 0;
45 }
46 #endif
47 if (!filename)
48 return USAGE;
49 fd = open(filename,O_RDWR|O_BINARY);
50 if (fd == -1)
51 return OPENERR;
53 if (forced == 0) {
54 status = 2;
55 /* Install hybridiso boot sector */
56 readsector(17UL);
57 if (strncmp(buffer+7, bootiso+ELTORITOERR+ELTORITOOFS, 23))
58 return ELTORITOERR;
59 catalog = LONG(buffer + 71);
60 readsector(catalog);
61 if (LONG(buffer) != 1 || LONG(buffer + 30) != 0x88AA55UL)
62 return CATALOGERR;
63 lba = LONG(buffer + 40);
64 readsector(lba);
65 if (LONG(buffer + 64) != 1886961915UL)
66 return HYBRIDERR;
67 isohybrid = bootiso[69] * 512;
68 LONG(bootiso + isohybrid + 432) = lba * 4;
69 LONG(bootiso + isohybrid + 440) = rand();
70 LONG(bootiso + isohybrid + partition) = 0x10080UL;
71 WORD(bootiso + isohybrid + 510) = 0xAA55U;
72 size = lseek(fd, 0UL, SEEK_END);
73 cylinders = (size + trksz - 1) / trksz;
74 bootiso[isohybrid + partition + 4] = 23; /* "Windows hidden IFS" */
75 bootiso[isohybrid + partition + 5] = heads - 1;
76 bootiso[isohybrid + partition + 6] = (((cylinders - 1) & 0x300) >> 2) + sectors;
77 bootiso[isohybrid + partition + 7] = (cylinders - 1) & 0xFF;
78 LONG(bootiso + isohybrid + partition + 8) = 0;
79 LONG(bootiso + isohybrid + partition + 12) = cylinders * sectors * heads;
81 /* Copy the partition table */
82 memcpy(bootiso + 0x1BE, bootiso + isohybrid + 0x1BE, 66);
83 }
85 /* Install iso2exe boot sector */
86 WORD(bootiso + 26) = rand();
88 /* read tazlito flavor data */
89 lseek(fd, 1024UL, SEEK_SET);
90 read(fd, tazlitoinfo, sizeof(tazlitoinfo));
92 /* Update iso image */
93 n = (bootiso[69] + 1) * 512;
94 lseek(fd, 0UL, SEEK_SET);
95 write(fd, bootiso, n); /* EXE/PE + isohybrid mbr */
96 write(fd, tazlitoinfo, ((0x8000U - BOOTISOSZ) > sizeof(tazlitoinfo))
97 ? sizeof(tazlitoinfo) : (0x8000U - BOOTISOSZ));
98 write(fd, bootiso + n, BOOTISOSZ - n); /* COM + rootfs + EXE/DOS */
100 /* Compute the checksum */
101 lseek(fd, 0UL, SEEK_SET);
102 for (i = 66, n = 0, j = 0; j < 16; j++, i = 0) {
103 if (read(fd, buffer, BUFFERSZ) != BUFFERSZ)
104 goto nochksum;
105 for (; i < BUFFERSZ; i += 2)
106 n += WORD(buffer + i);
107 }
108 WORD(bootiso + 64) = -n;
109 lseek(fd, 0UL, SEEK_SET);
110 write(fd, bootiso, 512);
111 nochksum:
112 close(fd);
113 status = 0;
114 return SUCCESSMSG;
115 }
117 int main(int argc, char *argv[])
118 {
119 forced = (argc > 2);
120 puts(bootiso + install(argv[1]));
121 if (status > 1)
122 puts(bootiso + FORCEMSG);
123 #ifdef WIN32
124 Sleep(2000);
125 #endif
126 return status;
127 }