wok view syslinux/stuff/iso2exe/win32.c @ rev 14266

syslinux/iso2exe: add partition table
author Pascal Bellard <pascal.bellard@slitaz.org>
date Mon Apr 01 17:47:04 2013 +0200 (2013-04-01)
parents bf8be127c60b
children 19258b949c1b
line source
1 #include <windows.h>
2 #include <winnt.h>
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <fcntl.h>
7 #define BOOTSTRAP_SECTOR_COUNT_OFFSET 28
9 static int fullread(int fd, char *p, int n)
10 {
11 int i, j;
12 for (i = 0; n > 0; i += j, n -= j) {
13 j = read(fd, p + i, n);
14 if (j <= 0) break;
15 }
16 return i;
17 #define read fullread
18 }
20 static int fullwrite(int fd, char *p, int n)
21 {
22 int i, j;
23 for (i = 0; n > 0; i += j, n -= j) {
24 j = write(fd, p + i, n);
25 if (j <= 0) break;
26 }
27 return i;
28 #define write fullwrite
29 }
31 static void exec16bits(char *isoFileName)
32 {
33 int fdiso, fdtmp, i;
34 long buffer[512/4];
35 char tmpFileName[MAX_PATH], *p;
37 strcpy(tmpFileName, isoFileName);
38 p = strrchr(tmpFileName, '\\');
39 if (!p++) p = tmpFileName;
40 strcpy(p, "tazboot.exe");
41 fdiso = open(isoFileName, O_RDONLY|O_BINARY);
42 fdtmp = open(tmpFileName, O_WRONLY|O_BINARY|O_CREAT,0555);
43 for (i = 0; i < 0x8000; i += sizeof(buffer)) {
44 read(fdiso, (char *) buffer, sizeof(buffer));
45 if (i == 0) buffer[15] = 0; // kill PE header
46 write(fdtmp, (char *) buffer, sizeof(buffer));
47 }
48 close(fdiso);
49 close(fdtmp);
50 execl(tmpFileName, isoFileName);
51 }
53 static int iswinnt(void)
54 {
55 OSVERSIONINFOA Version;
56 Version.dwOSVersionInfoSize = sizeof(Version);
57 return (GetVersionEx(&Version) &&
58 Version.dwPlatformId != VER_PLATFORM_WIN32_WINDOWS); // not Win9x
59 }
61 static int ishybrid(char *isoFileName)
62 {
63 int fdiso;
64 char buffer[2048];
65 unsigned long magic = 0;
67 fdiso = open(isoFileName, O_RDONLY|O_BINARY);
68 if (lseek(fdiso, 17 * 2048L, SEEK_SET) != -1 &&
69 read(fdiso, buffer, 2048) == 2048 &&
70 strncmp(buffer+7,"EL TORITO SPECIFICATION",23) == 0) {
71 unsigned long lba = * (unsigned long *) (buffer + 71);
73 if (lseek(fdiso, lba * 2048L, SEEK_SET) != -1 &&
74 read(fdiso, buffer, 2048) == 2048 &&
75 * (unsigned long *) (buffer + 0) == 1 &&
76 * (unsigned long *) (buffer + 30) == 0x88AA55) {
77 lba = * (unsigned long *) (buffer + 40);
78 if (lseek(fdiso, lba * 2048L, SEEK_SET) != -1 &&
79 read(fdiso, buffer, 2048) == 2048)
80 magic = * (unsigned long *) (buffer + 64);
81 }
82 }
83 close(fdiso);
84 return (magic == 1886961915);
85 }
87 #define MODE_READ 0
88 #define MODE_WRITE 1
89 static int rdwrsector(int mode, int drive, unsigned long startingsector,
90 unsigned long count, char *buffer)
91 {
92 HANDLE hDevice;
93 DWORD result;
94 char devname[sizeof("\\\\.\\PhysicalDrive0")];
96 if (drive >= 128) {
97 strcpy(devname, "\\\\.\\PhysicalDrive0");
98 devname[17] += drive - 128;
99 }
100 else {
101 strcpy(devname, "\\\\.\\A:");
102 devname[4] += drive;
103 }
104 hDevice = CreateFile (devname, GENERIC_READ,
105 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
106 if (hDevice == INVALID_HANDLE_VALUE)
107 return -1;
108 SetFilePointer(hDevice, (startingsector*512), NULL, FILE_BEGIN);
109 if (mode == MODE_READ) {
110 if (!ReadFile(hDevice, buffer, 512*count, &result, NULL))
111 result = -1;
112 }
113 else {
114 if (!WriteFile(hDevice, buffer, 512*count, &result, NULL))
115 result = -1;
116 }
117 CloseHandle(hDevice);
118 return result;
119 }
121 static int rawrite(unsigned long drive, char *isoFileName)
122 {
123 int fdiso, s, dev;
124 char buffer[2048];
126 if (drive == 0) return;
127 for (dev = 128; (drive & 1) == 0; dev++)
128 drive >>= 1;
129 fdiso = open(isoFileName, O_RDONLY|O_BINARY);
130 for (s = 0;;) {
131 int s, n = read(fdiso, buffer, sizeof(buffer));
132 if (n <= 0) break;
133 n = (n+511)/512;
134 rdwrsector(MODE_WRITE, dev, s, n, buffer);
135 s += n;
136 }
137 close(fdiso);
138 return dev;
139 }
141 static unsigned long drives(void)
142 {
143 int i, mask, result;
144 char buffer[512];
145 for (i = result = 0, mask = 1; i < 8; i++, mask <<= 1) {
146 if (rdwrsector(MODE_READ, i+128, 0, 1, buffer) != -1)
147 result |= mask;
148 }
149 return result;
150 }
152 static void writefloppy(char *isoFileName)
153 {
154 char buffer[512];
155 int i, n, fd;
157 buffer[BOOTSTRAP_SECTOR_COUNT_OFFSET] = 0;
158 fd = open(isoFileName, O_RDONLY|O_BINARY);
159 if (fd != -1) {
160 read(fd, buffer, sizeof(buffer));
161 n = buffer[BOOTSTRAP_SECTOR_COUNT_OFFSET];
162 if (n != 0 &&
163 lseek(fd, * (unsigned short *) (buffer + 66) - (512 * n),
164 SEEK_SET) != -1) {
165 for (i = 0; i < n; i++) {
166 read(fd, buffer, 512);
167 if (i == 1) strncpy(buffer, isoFileName, 512);
168 rdwrsector(MODE_WRITE, 0, i, 1, buffer);
169 }
170 }
171 close(fd);
172 }
173 }
175 //TODO #define VCPI_LINUX_LOADER The DOS/EXE loader can boot in VM86 using VCPI API
176 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
177 LPSTR lpCmdLine, int nCmdShow)
178 {
179 char isoFileName[MAX_PATH];
180 char header[32];
181 int fd;
182 int usbkeyicon = MB_ICONASTERISK;
183 char *usbkeymsg = "You can either:\n\n"
184 "- create a SliTaz USB boot key or a boot memory card.\n"
185 " (note that this will be read only like a CDROM.\n"
186 " The Slitaz utility 'tazusb' can be used later to create\n"
187 " a true read/write USB key).\n\n"
188 "- create a SliTaz bootstrap floppy for the ISO image\n"
189 " on the hard disk.\n"
190 "\nDo you want to create a boot key now ?";
192 GetModuleFileName(hInstance, isoFileName, MAX_PATH);
193 if (!iswinnt()) {
194 #ifdef VCPI_LINUX_LOADER
195 exec16bits(isoFileName);
196 #else
197 MessageBox(NULL,"No support for Win9x yet.\n"
198 "Retry in DOS mode without emm386.\n",
199 "Sorry", MB_OK|MB_ICONERROR);
200 exit(1);
201 #endif
202 }
203 if (!ishybrid(isoFileName)) {
204 if (MessageBox(NULL,"Not an isolinux hybrid ISO.\n"
205 "This ISO image will not boot\n"
206 "from the media that you create !",
207 "Will not boot !",
208 MB_OKCANCEL|MB_ICONWARNING) == IDCANCEL)
209 exit(0);
210 }
211 header[BOOTSTRAP_SECTOR_COUNT_OFFSET] = 0;
212 fd = open(isoFileName, O_RDONLY|O_BINARY);
213 if (fd != -1) {
214 read(fd, header, sizeof(header));
215 close(fd);
216 }
217 if (header[BOOTSTRAP_SECTOR_COUNT_OFFSET] == 0) { // No floppy bootstrap available
218 usbkeyicon = MB_ICONQUESTION;
219 usbkeymsg = "Do you want to create a boot key ?";
220 }
221 if (MessageBox(NULL,usbkeymsg, "Create a boot stick ?",
222 MB_YESNO|usbkeyicon) == IDYES) {
223 unsigned long base, new;
224 int retry;
226 if (MessageBox(NULL,"Step 1: unplug the USB stick.",
227 "Drive detection 1/2",
228 MB_OKCANCEL|MB_ICONEXCLAMATION) == IDCANCEL)
229 exit(0);
230 base = drives();
231 if (MessageBox(NULL,"Step 2: plug the USB stick in.",
232 "Drive detection 2/2",
233 MB_OKCANCEL|MB_ICONEXCLAMATION) == IDCANCEL)
234 exit(0);
235 retry = 0;
236 do {
237 Sleep(1000); // ms
238 new = drives();
239 } while (new == base && retry++ < 10);
240 if (new == base) {
241 MessageBox(NULL,"No USB stick found.","Sorry",
242 MB_OK|MB_ICONERROR);
243 }
244 else {
245 char *msg = "(hd0) is up to date.";
246 msg[3] += rawrite(base ^ new, isoFileName) - 128;
247 MessageBox(NULL,msg,"Finished",MB_OK);
248 }
249 }
250 else if (header[BOOTSTRAP_SECTOR_COUNT_OFFSET] != 0 &&
251 MessageBox(NULL,"Do you want to create a bootstrap floppy ?",
252 "Create a bootstrap floppy ?",
253 MB_YESNO|MB_ICONQUESTION) == IDYES &&
254 MessageBox(NULL,"Insert a floppy disk in drive now",
255 "Insert floppy",
256 MB_OKCANCEL|MB_ICONEXCLAMATION) != IDCANCEL) {
258 // Create a 9k bootstrap with vfat, ext2 & ntfs drivers
259 // to boot the ISO image on hard disk
260 writefloppy(isoFileName);
261 MessageBox(NULL,"The bootstrap floppy is up to date.",
262 "Finished",MB_OK);
263 }
264 }