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

syslinux/isohybrid.exe add -r support
author Pascal Bellard <pascal.bellard@slitaz.org>
date Sun Feb 14 22:06:06 2016 +0100 (2016-02-14)
parents ee7f5b80836f
children 62104f2454a3
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 #ifdef __MSDOS__
13 int ftruncate(int fd, long newsize)
14 {
15 if (lseek(fd, newsize, SEEK_SET) != -1L)
16 return write(fd, NULL, 0);
17 return -1;
18 }
19 #endif
20 #ifdef __MINGW32__
21 #define ftruncate chsize
22 #endif
23 #if !defined(__MSDOS__) && !defined(WIN32)
24 #define O_BINARY 0
25 #endif
26 typedef unsigned char uint8_t;
27 typedef unsigned long uint32_t;
28 #include "iso2exe.h"
30 static int fd, forced, uninstall;
31 static unsigned status = 1;
32 static char *append, *initrd;
33 static char tazlitoinfo[0x8000U - BOOTISOSZ];
34 #define buffer tazlitoinfo
35 #define BUFFERSZ 2048
36 #define BYTE(n) * (unsigned char *) (n)
37 #define WORD(n) * (unsigned short *) (n)
38 #define LONG(n) * (unsigned long *) (n)
40 static void readsector(unsigned long sector)
41 {
42 if (lseek(fd, sector * BUFFERSZ, SEEK_SET) == -1 ||
43 read(fd, buffer, BUFFERSZ) != BUFFERSZ) {
44 puts(bootiso+READSECTORERR);
45 exit(1);
46 }
47 }
49 static unsigned long getcustomsector(void)
50 {
51 readsector(16UL);
52 return 16UL + LONG(buffer + 80);
53 }
55 static int skipmd5 = 0;
56 #define ALIGN1
58 typedef struct {
59 uint32_t l;
60 uint32_t h;
61 } uint64_t;
62 static uint8_t wbuffer[64]; /* always correctly aligned for uint64_t */
63 static uint64_t total64; /* must be directly before hash[] */
64 static uint32_t hash[8]; /* 4 elements for md5, 5 for sha1, 8 for sha256 */
66 //#define rotl32(x,n) (((x) << (n)) | ((x) >> (32 - (n))))
67 static uint32_t rotl32(uint32_t x, unsigned n)
68 {
69 return (x << n) | (x >> (32 - n));
70 }
72 static void md5_process_block64(void);
74 /* Feed data through a temporary buffer.
75 * The internal buffer remembers previous data until it has 64
76 * bytes worth to pass on.
77 */
78 static void common64_hash(const void *buffer, size_t len)
79 {
80 unsigned bufpos = total64.l & 63;
82 total64.l += len; if (total64.l < len) total64.h++;
84 while (1) {
85 unsigned remaining = 64 - bufpos;
86 if (remaining > len)
87 remaining = len;
88 /* Copy data into aligned buffer */
89 memcpy(wbuffer + bufpos, buffer, remaining);
90 len -= remaining;
91 buffer = (const char *)buffer + remaining;
92 bufpos += remaining;
93 /* clever way to do "if (bufpos != 64) break; ... ; bufpos = 0;" */
94 bufpos -= 64;
95 if (bufpos != 0)
96 break;
97 /* Buffer is filled up, process it */
98 md5_process_block64();
99 /*bufpos = 0; - already is */
100 }
101 }
103 /* Process the remaining bytes in the buffer */
104 static void common64_end(void)
105 {
106 unsigned bufpos = total64.l & 63;
107 /* Pad the buffer to the next 64-byte boundary with 0x80,0,0,0... */
108 wbuffer[bufpos++] = 0x80;
110 /* This loop iterates either once or twice, no more, no less */
111 while (1) {
112 unsigned remaining = 64 - bufpos;
113 memset(wbuffer + bufpos, 0, remaining);
114 /* Do we have enough space for the length count? */
115 if (remaining >= 8) {
116 /* Store the 64-bit counter of bits in the buffer */
117 //uint64_t t = total64 << 3;
118 uint32_t *t = (uint32_t *) (&wbuffer[64 - 8]);
119 /* wbuffer is suitably aligned for this */
120 //*(uint64_t *) (&wbuffer[64 - 8]) = t;
121 t[0] = total64.l << 3;
122 t[1] = (total64.h << 3) | (total64.l >> 29);
123 }
124 md5_process_block64();
125 if (remaining >= 8)
126 break;
127 bufpos = 0;
128 }
129 }
131 /* These are the four functions used in the four steps of the MD5 algorithm
132 * and defined in the RFC 1321. The first function is a little bit optimized
133 * (as found in Colin Plumbs public domain implementation).
134 * #define FF(b, c, d) ((b & c) | (~b & d))
135 */
136 #undef FF
137 #undef FG
138 #undef FH
139 #undef FI
140 #define FF(b, c, d) (d ^ (b & (c ^ d)))
141 #define FG(b, c, d) FF(d, b, c)
142 #define FH(b, c, d) (b ^ c ^ d)
143 #define FI(b, c, d) (c ^ (b | ~d))
145 /* Hash a single block, 64 bytes long and 4-byte aligned */
146 static void md5_process_block64(void)
147 {
148 uint32_t *words = (void*) wbuffer;
149 uint32_t A = hash[0];
150 uint32_t B = hash[1];
151 uint32_t C = hash[2];
152 uint32_t D = hash[3];
154 const uint32_t *pc;
155 const char *pp;
156 const char *ps;
157 int i;
158 uint32_t temp;
161 pc = C_array;
162 pp = P_array;
163 ps = S_array - 4;
165 for (i = 0; i < 64; i++) {
166 if ((i & 0x0f) == 0)
167 ps += 4;
168 temp = A;
169 switch (i >> 4) {
170 case 0:
171 temp += FF(B, C, D);
172 break;
173 case 1:
174 temp += FG(B, C, D);
175 break;
176 case 2:
177 temp += FH(B, C, D);
178 break;
179 case 3:
180 temp += FI(B, C, D);
181 }
182 temp += words[(int) (*pp++)] + *pc++;
183 temp = rotl32(temp, ps[i & 3]);
184 temp += B;
185 A = D;
186 D = C;
187 C = B;
188 B = temp;
189 }
190 /* Add checksum to the starting values */
191 hash[0] += A;
192 hash[1] += B;
193 hash[2] += C;
194 hash[3] += D;
196 }
197 #undef FF
198 #undef FG
199 #undef FH
200 #undef FI
202 /* Initialize structure containing state of computation.
203 * (RFC 1321, 3.3: Step 3)
204 */
205 static void md5_begin(void)
206 {
207 hash[0] = 0x67452301;
208 hash[1] = 0xefcdab89;
209 hash[2] = 0x98badcfe;
210 hash[3] = 0x10325476;
211 total64.l = total64.h = 0;
212 }
214 /* Used also for sha1 and sha256 */
215 #define md5_hash common64_hash
217 /* Process the remaining bytes in the buffer and put result from CTX
218 * in first 16 bytes following RESBUF. The result is always in little
219 * endian byte order, so that a byte-wise output yields to the wanted
220 * ASCII representation of the message digest.
221 */
222 #define md5_end common64_end
224 static int writenhash(void *buffer, size_t len)
225 {
226 md5_hash(buffer, len);
227 return write(fd, buffer, len);
228 }
230 static void md5sum(void)
231 {
232 unsigned long sectors = 0;
233 int count;
235 lseek(fd, 32768UL, SEEK_SET);
237 md5_begin();
238 while ((count = read(fd, buffer, BUFFERSZ)) > 0) {
239 if (sectors == 0)
240 sectors = LONG(buffer + 80);
241 md5_hash(buffer, count);
242 if (--sectors == 0)
243 break;
244 }
246 if (count < 0)
247 return;
249 md5_end();
251 lseek(fd, 32752UL, SEEK_SET);
252 write(fd, hash, 16);
253 memcpy(bootiso + BOOTISOSZ - 16, hash, 16);
254 }
256 static unsigned chksum(unsigned start, unsigned stop)
257 {
258 unsigned i, n = 0;
260 lseek(fd, 0UL /* (unsigned long) (start / BUFFERSZ) */, SEEK_SET);
261 while (1) {
262 if (read(fd, buffer, BUFFERSZ) != BUFFERSZ)
263 return 0;
264 for (i = start % BUFFERSZ; i < BUFFERSZ; i += 2, start += 2) {
265 if (start >= stop)
266 return - n;
267 n += WORD(buffer + i);
268 }
269 }
270 }
272 static unsigned clear_config(unsigned i)
273 {
274 for (;i % 512; i++) {
275 /* clear custom config */
276 write(fd, buffer + 2048, 2048);
277 }
278 return i;
279 }
281 static unsigned install(char *filename)
282 {
283 #define heads 64
284 #define sectors 32
285 #define partition (446+16)
286 #define trksz (512UL * heads * sectors)
287 unsigned long size, catalog, lba;
288 int cylinders, i, j, isohybrid;
289 unsigned n;
290 #ifdef __MSDOS__
291 for (bootiso = (char *) install;
292 bootiso[0] != 'M' || bootiso[1] != 'Z' || bootiso[2] != '\xEB';
293 bootiso++) if (bootiso < (char *) install) {
294 bootiso = "No bootiso data";
295 return 0;
296 }
297 #endif
298 if (!filename)
299 return USAGE;
300 fd = open(filename,O_RDWR|O_BINARY);
301 if (fd == -1)
302 return OPENERR;
304 if (uninstall) {
305 struct { char check[sizeof(tazlitoinfo) - BUFFERSZ - 1024]; };
306 readsector(0UL);
307 n = BUFFERSZ; /* fill with zeros */
308 if (WORD(buffer) == 23117) {
309 /* restore isolinux hybrid boot */
310 readsector((unsigned long) buffer[417]);
311 n = 0; /* fill with hybrid boot */
312 }
313 lseek(fd, 0UL, SEEK_SET);
314 for (i = 0; i < 32; i++, n = BUFFERSZ) {
315 write(fd, buffer + n, BUFFERSZ);
316 }
317 i = getcustomsector();
318 lseek(fd, i * 2048UL, SEEK_SET);
319 i = clear_config(i);
320 ftruncate(fd, i * 2048UL);
321 close(fd);
322 status = 0;
323 return UNINSTALLMSG;
324 }
326 readsector(0UL);
327 if (buffer[0] == 'M' && buffer[1] == 'Z') {
328 if (forced == 0)
329 return ALREADYEXEERR;
330 n = (buffer[417] + 1) * 512;
331 i = 0x8000 - 1024;
332 if (i > sizeof(tazlitoinfo))
333 i = sizeof(tazlitoinfo);
334 if (lseek(fd, n, SEEK_SET) == -1 ||
335 read(fd, tazlitoinfo, sizeof(tazlitoinfo)) != sizeof(tazlitoinfo) ||
336 lseek(fd, 1024UL, SEEK_SET) == -1 ||
337 write(fd, tazlitoinfo, i) != i) {
338 puts(bootiso+READSECTORERR);
339 exit(1);
340 }
341 }
343 do {
344 /* Install hybridiso boot sector */
345 readsector(17UL);
346 status = ELTORITOERR;
347 if (strncmp(buffer+7, bootiso+ELTORITOERR+ELTORITOOFS, 23))
348 break;
349 catalog = LONG(buffer + 71);
350 readsector(catalog);
351 status = CATALOGERR;
352 if (LONG(buffer) != 1 || LONG(buffer + 30) != 0x88AA55UL)
353 break;
354 lba = LONG(buffer + 40);
355 readsector(lba);
356 status = HYBRIDERR;
357 if (LONG(buffer + 64) != 1886961915UL)
358 break;
359 isohybrid = bootiso[417] * 512;
360 LONG(bootiso + isohybrid + 432) = lba * 4;
361 LONG(bootiso + isohybrid + 440) = rand();
362 LONG(bootiso + isohybrid + partition) = 0x10080UL;
363 WORD(bootiso + isohybrid + 510) = 0xAA55U;
364 #if 0
365 size = lseek(fd, 0UL, SEEK_END);
366 size += 0x000FFFFFUL;
367 size &= 0xFFF00000UL;
368 #else
369 for (size = 0x000FFFFFUL; /* 1M - 1 */
370 read(fd, tazlitoinfo, 1024) == 1024;
371 size += 1024);
372 size &= 0xFFF00000UL; /* round */
373 #endif
374 cylinders = (size >> 20) - 1;
375 bootiso[isohybrid + partition + 4] = 23; /* "Windows hidden IFS" */
376 bootiso[isohybrid + partition + 5] = heads - 1;
377 bootiso[isohybrid + partition + 6] = ((cylinders & 0x300) >> 2) + sectors;
378 bootiso[isohybrid + partition + 7] = cylinders & 0xFF;
379 LONG(bootiso + isohybrid + partition + 8) = 0;
380 LONG(bootiso + isohybrid + partition + 12) = (size >> 9);
382 /* Copy the partition table */
383 memcpy(bootiso + 0x1BE, bootiso + isohybrid + 0x1BE, 66);
384 status = 0;
385 } while (0);
387 if (forced == 0 && status)
388 return status;
390 status = 1;
391 if (append || initrd) {
392 unsigned long pos = getcustomsector() * 2048UL;
393 lseek(fd, pos, SEEK_SET);
394 clear_config(pos);
395 lseek(fd, pos, SEEK_SET);
396 write(fd, "#!boot 00000000000000000000000000000000\n", 40);
397 n = pos + 40;
398 md5_begin();
399 if (append) {
400 i = strlen(append);
401 writenhash("append=", 7);
402 writenhash(append, i);
403 writenhash("\n", 1);
404 n += i + 8;
405 }
406 if (initrd) {
407 char number[16], *p;
408 unsigned long end, x;
409 int data = open(initrd,O_RDONLY|O_BINARY);
410 if (data == -1)
411 return OPENINITRDERR;
412 for (end = 0;; end += i) {
413 i = read(data, buffer, BUFFERSZ);
414 if (i <= 0)
415 break;
416 }
417 p = number + sizeof(number) -1;
418 x = end; *p-- = '\n';
419 do {
420 *p-- = '0' + (x % 10);
421 x /= 10;
422 } while (x);
423 if (*++p != '0') {
424 writenhash("initrd:", 7);
425 i = number - p + sizeof(number);
426 writenhash(p, i);
427 n += i + 7;
428 lseek(data, 0UL, SEEK_SET);
429 do {
430 i = read(data, buffer, BUFFERSZ);
431 if (i <= 0)
432 break;
433 if (i > end)
434 i = end;
435 writenhash(buffer, i);
436 n += i;
437 end -= i;
438 } while (end != 0);
439 }
440 close(data);
441 }
442 while (n & 0x000FFFFFUL) {
443 unsigned long i = 0x100000UL - (n & 0x000FFFFFUL);
444 if (i > BUFFERSZ)
445 i = BUFFERSZ;
446 i = write(fd, buffer + BUFFERSZ, i);
447 if (i <= 0)
448 break;
449 n += i;
450 }
451 ftruncate(fd, n);
452 md5_end();
453 {
454 static char h[] = "0123456789abcdef";
455 char string[32], *s = string + 30;
456 unsigned char *p = (void *) hash;
458 lseek(fd, 7 + pos, SEEK_SET);
459 for (p += 15; s >= string; p--, s -= 2) {
460 s[1] = h[ *p & 15 ];
461 s[0] = h[ *p >> 4 ];
462 }
463 write(fd, string, 32);
464 }
465 }
467 /* Install iso2exe boot sector */
468 LONG(bootiso + 440) = time(NULL);
470 /* read tazlito flavor data */
471 lseek(fd, 1024UL, SEEK_SET);
472 read(fd, tazlitoinfo, sizeof(tazlitoinfo));
474 /* Update iso image */
475 n = (bootiso[417] + 1) * 512;
476 lseek(fd, 0UL, SEEK_SET);
477 write(fd, bootiso, n); /* EXE/PE + isohybrid mbr */
478 write(fd, tazlitoinfo, sizeof(tazlitoinfo));
479 write(fd, bootiso + n, BOOTISOSZ - n); /* COM + rootfs + EXE/DOS */
481 /* Compute the boot checksums */
482 if (!skipmd5) {
483 puts(bootiso + MD5MSG);
484 md5sum();
485 lseek(fd, 0UL, SEEK_SET);
486 write(fd, bootiso, 512);
487 n = WORD(bootiso + 2) - 512*(WORD(bootiso + 4) - 1);
488 WORD(bootiso + 18) = chksum(0, (unsigned short) n) - 1;
489 }
490 lseek(fd, 0UL, SEEK_SET);
491 write(fd, bootiso, 512);
492 close(fd);
493 status = 0;
494 return SUCCESSMSG;
495 }
497 static unsigned short files[] = { // to move to iso2exe.sh ....
498 WIN32_EXE, /* 0 */
499 SYSLINUX_MBR, /* 1 */
500 FLAVOR_INFO, /* 2 */
501 FLOPPY_BOOT, /* 3 */
502 TAZBOOT_COM, /* 4 */
503 ROOTFS_GZ, /* 5 */
504 DOSSTUB, /* 6 */
505 BOOT_MD5, /* 7 */
506 FS_ISO, /* 8 */
507 CUSTOM_MAGIC, /* 9 */
508 CUSTOM_APPEND, /* 10 */
509 CUSTOM_INITRD /* 11 */
510 };
512 static long file_offset, file_size;
513 static void fileofs(int number)
514 {
515 unsigned long i, c, stub;
516 char *s;
518 c = getcustomsector();
519 readsector(0);
520 i = 1024;
521 if (WORD(buffer+1024) != 35615) i = 512 * (1 + BYTE(buffer+417));
522 stub = WORD(buffer+20) - 0xC0;
523 file_size = file_offset = 0;
524 switch (files[number]) {
525 case WIN32_EXE: /* win32.exe */
526 if (i != 1024) file_size = i - 512; break;
527 case SYSLINUX_MBR: /* syslinux.mbr */
528 if (i != 1024) file_offset = i - 512;
529 file_size = 512; break;
530 case FLAVOR_INFO: /* flavor.info */
531 file_offset = i; file_size = 0; break;
532 case FLOPPY_BOOT: /* floppy.boot */
533 file_size = BYTE(buffer+26)*512;
534 file_offset = WORD(buffer+64) - 0xC0 - file_size; break;
535 case TAZBOOT_COM: /* tazboot.com */
536 file_offset = WORD(buffer+64) - 0xC0;
537 file_size = stub - WORD(buffer+24) - file_offset; break;
538 case ROOTFS_GZ: /* rootfs.gz */
539 file_size = WORD(buffer+24);
540 file_offset = stub - file_size; break;
541 case DOSSTUB: /* dosstub */
542 file_offset = stub;
543 file_size = 0x8000U - file_offset; break;
544 case BOOT_MD5: /* boot.md5 */
545 file_offset = 0x7FF0U; file_size = 16; break;
546 case FS_ISO: /* fs.iso */
547 file_offset = 0x8000U; file_size = 2048*c - file_offset; break;
548 case CUSTOM_MAGIC: /* custom.magic */
549 readsector(c);
550 if (!strncmp(buffer, "#!boot", 6)) {
551 file_size = 39; file_offset = 2048*c;
552 }; break;
553 case CUSTOM_APPEND: /* custom.append */
554 readsector(c);
555 file_offset = 2048*c + 47; s = strstr(buffer, "append=");
556 if (s) file_size = strchr(s,'\n') - s - 7;
557 break;
558 case CUSTOM_INITRD: /* custom.initrd */
559 readsector(c);
560 s = strstr(buffer,"initrd:");
561 if (!s) break;
562 file_size = atoi(s + 7);
563 s = strchr(s,'\n') + 1;
564 file_offset = 2048*c + (s - buffer);
565 }
566 }
568 static void list(void)
569 {
570 int num, heap = 0;
572 for (num = 0; num < sizeof(files)/sizeof(files[0]); num++) {
573 fileofs(num);
574 if (file_size <= 0 || file_offset > 0x3FFFFFFFUL) continue;
575 readsector(file_offset / 2048);
576 if (WORD(buffer + file_offset % 2048) == 0) continue;
577 if (file_offset > heap && (file_offset - heap) > 16)
578 printf("%d free bytes in %04X..%04X\n",
579 file_offset - heap, heap, file_offset);
580 if (file_offset >= heap) heap = file_offset + file_size;
581 printf("%s at %04X (%d bytes).\n", bootiso + files[num],
582 file_offset, file_size);
583 }
584 file_offset=lseek(fd, 0UL, SEEK_END);
585 if (file_offset > heap)
586 printf("%d free bytes in %04X..%04X\n",
587 file_offset - heap, heap, file_offset);
588 }
590 static void extract(char *name)
591 {
592 int num;
594 for (num = sizeof(files)/sizeof(files[0]) - 1;
595 strcmp(name,bootiso + files[num]); num--) if (num <= 0) return;
596 fileofs(num);
597 if (file_size == 0) return;
598 lseek(fd, file_offset, SEEK_SET);
599 num = open(name, O_WRONLY|O_BINARY|O_CREAT, 0x644);
600 while (file_size > 0) {
601 int n = read(fd, buffer, BUFFERSZ);
602 if (n <= 0) break;
603 if (n > file_size) n = file_size;
604 write(num,buffer,n);
605 file_size -= n;
606 }
607 close(num);
608 }
610 int main(int argc, char *argv[])
611 {
612 int i;
613 char *s;
615 for (i = 0; argc > 2;) {
616 s = argv[1];
617 if (*s != '-') break;
618 while (*s == '-') s++;
619 switch (*s | 0x20) {
620 case 'a' : append=argv[2]; break;
621 case 'i' : initrd=argv[2]; break;
622 case 'r' : case 'l' :
623 i++; argv++; argc--; continue;
624 }
625 argv += 2;
626 argc -= 2;
627 }
628 if (i != 0) {
629 fd = open(argv[i],O_RDONLY|O_BINARY);
630 if (fd == -1) puts(bootiso + OPENERR);
631 else if (argc <= 2) list();
632 else for (i = 2; i < argc; i++) extract(argv[i]);
633 return 0;
634 }
635 for (i = 2; i < argc; i++) {
636 char *s = argv[i];
637 while ((unsigned)(*s - '-') <= ('/' - '-')) s++;
638 switch (*s | 0x20) {
639 case 'f' : forced++; break;
640 case 'q' : skipmd5++; break;
641 case 'u' : uninstall++; break;
642 }
643 }
644 puts(bootiso + install(argv[1]));
645 if (status > 1)
646 puts(bootiso + FORCEMSG);
647 #ifdef WIN32
648 Sleep(2000);
649 #endif
650 return status;
651 }