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

syslinux/iso2exe: check ISO md5
author Pascal Bellard <pascal.bellard@slitaz.org>
date Sat Jan 03 21:41:48 2015 +0100 (2015-01-03)
parents aca562aa836e
children 52c9cc2da3c6
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[0x8000U - BOOTISOSZ];
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+16)
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 #if 0
73 size = lseek(fd, 0UL, SEEK_END);
74 size += 0x000FFFFFUL;
75 size &= 0xFFF00000UL;
76 #else
77 for (size = 0x000FFFFFUL; /* 1M - 1 */
78 read(fd, tazlitoinfo, 1024) == 1024;
79 size += 1024);
80 size &= 0xFFF00000UL; /* round */
81 #endif
82 cylinders = (size >> 20) - 1;
83 bootiso[isohybrid + partition + 4] = 23; /* "Windows hidden IFS" */
84 bootiso[isohybrid + partition + 5] = heads - 1;
85 bootiso[isohybrid + partition + 6] = ((cylinders & 0x300) >> 2) + sectors;
86 bootiso[isohybrid + partition + 7] = cylinders & 0xFF;
87 LONG(bootiso + isohybrid + partition + 8) = 0;
88 LONG(bootiso + isohybrid + partition + 12) = (size >> 9);
90 /* Copy the partition table */
91 memcpy(bootiso + 0x1BE, bootiso + isohybrid + 0x1BE, 66);
92 }
94 /* Install iso2exe boot sector */
95 WORD(bootiso + 26) = rand();
97 /* read tazlito flavor data */
98 lseek(fd, 1024UL, SEEK_SET);
99 read(fd, tazlitoinfo, sizeof(tazlitoinfo));
101 /* Update iso image */
102 n = (bootiso[69] + 1) * 512;
103 lseek(fd, 0UL, SEEK_SET);
104 write(fd, bootiso, n); /* EXE/PE + isohybrid mbr */
105 write(fd, tazlitoinfo, sizeof(tazlitoinfo));
106 write(fd, bootiso + n, BOOTISOSZ - n); /* COM + rootfs + EXE/DOS */
108 /* Compute the checksum */
109 lseek(fd, 0UL, SEEK_SET);
110 for (i = 66, n = 0, j = 0; j < 16; j++, i = 0) {
111 if (read(fd, buffer, BUFFERSZ) != BUFFERSZ)
112 goto nochksum;
113 for (; i < BUFFERSZ; i += 2)
114 n += WORD(buffer + i);
115 }
116 WORD(bootiso + 64) = -n;
117 lseek(fd, 0UL, SEEK_SET);
118 write(fd, bootiso, 512);
119 nochksum:
120 close(fd);
121 status = 0;
122 return SUCCESSMSG;
123 }
125 int main(int argc, char *argv[])
126 {
127 forced = (argc > 2);
128 puts(bootiso + install(argv[1]));
129 if (status > 1)
130 puts(bootiso + FORCEMSG);
131 #ifdef WIN32
132 Sleep(2000);
133 #endif
134 return status;
135 }