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