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

syslinux/iso2exe: add boot error message
author Pascal Bellard <pascal.bellard@slitaz.org>
date Tue Apr 02 08:37:23 2013 +0200 (2013-04-02)
parents ff85ea851c53
children 19258b949c1b
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 tazlitoinfo[10*1024];
8 #define buffer tazlitoinfo
9 #define BUFFERSZ 2048
11 static void quit(char *msg)
12 {
13 fprintf(stderr,"%s.\n", msg);
14 exit(1);
15 }
17 static void readsector(unsigned long sector)
18 {
19 if (lseek(fd, sector * BUFFERSZ, SEEK_SET) == -1 ||
20 read(fd, buffer, BUFFERSZ) != BUFFERSZ)
21 quit("read sector failure");
22 }
24 int main(int argc, char *argv[])
25 {
26 #define heads 64
27 #define sectors 32
28 #define partition 446
29 #define trksz (512 * heads * sectors)
30 unsigned long size, catalog, lba;
31 int cylinders, i, j, isohybrid;
32 unsigned n;
33 #ifndef WIN32
34 char *bootiso;
35 for (bootiso = (char *) main;
36 bootiso[0] != 'M' || bootiso[1] != 'Z' || bootiso[2] != 0xEB;
37 bootiso++) if (bootiso < (char *) main) quit("bootiso not found");
38 #endif
39 if (argc < 2)
40 quit("Usage : isohybrid.exe file.iso");
41 fd = open(argv[1],O_RDWR|O_BINARY);
42 if (fd == -1)
43 quit("Can't open rw");
45 // Install hybridiso boot sector
46 readsector(17UL);
47 if (strncmp(buffer+7, "EL TORITO SPECIFICATION", 23))
48 quit("No EL TORITO boot record found");
49 catalog = * (unsigned long *) (buffer + 71);
50 readsector(catalog);
51 if (* (unsigned long *) buffer != 1 ||
52 * (unsigned long *) (buffer + 30) != 0x88AA55)
53 quit("invalid boot catalog.");
54 lba = * (unsigned long *) (buffer + 40);
55 readsector(lba);
56 if (* (unsigned long *) (buffer + 64) != 1886961915)
57 quit("no isolinux.bin hybrid signature in bootloader");
58 isohybrid = bootiso[69] * 512;
59 * (unsigned long *) &bootiso[isohybrid + 432] = lba * 4;
60 * (unsigned long *) &bootiso[isohybrid + 440] = rand();
61 * (unsigned long *) &bootiso[isohybrid + partition] = 0x10080;
62 * (unsigned short *) &bootiso[isohybrid + 510] = 0xAA55;
63 size = lseek(fd, 0UL, SEEK_END);
64 cylinders = (size + trksz - 1) / trksz;
65 bootiso[isohybrid + partition + 4] = 23; // "Windows hidden IFS"
66 bootiso[isohybrid + partition + 5] = heads - 1;
67 bootiso[isohybrid + partition + 6] = (((cylinders - 1) & 0x300) >> 2) + sectors;
68 bootiso[isohybrid + partition + 7] = (cylinders - 1) & 0xFF;
69 * (unsigned long *) &bootiso[isohybrid + partition + 8] = 0;
70 * (unsigned long *) &bootiso[isohybrid + partition + 12] = cylinders * sectors * heads;
72 // Copy the partition table
73 memcpy(bootiso + 0x1BE, bootiso + isohybrid + 0x1BE, 66);
75 // Install iso2exe boot sector
76 * (unsigned short *) (bootiso + 26) = rand();
78 // read tazlito flavor data
79 lseek(fd, 1024UL, SEEK_SET);
80 read(fd, tazlitoinfo, sizeof(tazlitoinfo));
82 // Update iso image
83 n = (bootiso[69] + 1) * 512;
84 lseek(fd, 0UL, SEEK_SET);
85 write(fd, bootiso, n); // EXE/PE + isohybrid mbr
86 write(fd, tazlitoinfo, ((0x8000U - BOOTISOSZ) > sizeof(tazlitoinfo))
87 ? sizeof(tazlitoinfo) : (0x8000U - BOOTISOSZ));
88 write(fd, bootiso + n, BOOTISOSZ - n); // COM + rootfs + EXE/DOS
90 // Compute the checksum
91 lseek(fd, 0UL, SEEK_SET);
92 for (i = 66, n = 0, j = 0; j < 16; j++, i = 0) {
93 if (read(fd, buffer, BUFFERSZ) != BUFFERSZ)
94 goto nochksum;
95 for (; i < BUFFERSZ; i += 2)
96 n += * (unsigned short *) (buffer + i);
97 }
98 * (unsigned short *) (bootiso + 64) = -n;
99 lseek(fd, 0UL, SEEK_SET);
100 write(fd, bootiso, 512);
101 nochksum:
102 close(fd);
103 printf("Note you can create a USB key with %s.\n"
104 "Simply rename it to a .exe file and run it.\n", argv[1]);
105 }