1 |
/* |
2 |
* Copyright (C) 2003-2007 Anders Gavare. All rights reserved. |
3 |
* |
4 |
* Redistribution and use in source and binary forms, with or without |
5 |
* modification, are permitted provided that the following conditions are met: |
6 |
* |
7 |
* 1. Redistributions of source code must retain the above copyright |
8 |
* notice, this list of conditions and the following disclaimer. |
9 |
* 2. Redistributions in binary form must reproduce the above copyright |
10 |
* notice, this list of conditions and the following disclaimer in the |
11 |
* documentation and/or other materials provided with the distribution. |
12 |
* 3. The name of the author may not be used to endorse or promote products |
13 |
* derived from this software without specific prior written permission. |
14 |
* |
15 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
16 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
17 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
18 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
19 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
20 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
21 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
22 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
23 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
24 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
25 |
* SUCH DAMAGE. |
26 |
* |
27 |
* |
28 |
* $Id: bootblock_iso9660.c,v 1.4 2007/06/23 23:59:14 debug Exp $ |
29 |
* |
30 |
* ISO9660 CD-ROM "bootblock" handling. |
31 |
* |
32 |
* There is really no bootblock; instead, the file which is to be booted |
33 |
* is extracted into a temporary file, and started as if it was given |
34 |
* as a normal file argument on the command line. |
35 |
* |
36 |
* TODO: This is really ugly. It's a quick hack. All the magic constants |
37 |
* need to be replaced with real code! |
38 |
*/ |
39 |
|
40 |
#include <stdio.h> |
41 |
#include <stdlib.h> |
42 |
#include <string.h> |
43 |
#include <sys/types.h> |
44 |
#include <unistd.h> |
45 |
|
46 |
#include "cpu.h" |
47 |
#include "diskimage.h" |
48 |
#include "machine.h" |
49 |
#include "misc.h" |
50 |
|
51 |
/* #define ISO_DEBUG */ |
52 |
|
53 |
|
54 |
static void debug_print_volume_id_and_filename(int iso_type, |
55 |
unsigned char *buf, char *filename) |
56 |
{ |
57 |
/* Volume ID: */ |
58 |
char str[35]; |
59 |
int i, ofs = iso_type == 3? 48 : 40; |
60 |
|
61 |
memcpy(str, buf + ofs, sizeof(str)); |
62 |
str[32] = '\0'; i = 31; |
63 |
|
64 |
while (i >= 0 && str[i]==' ') |
65 |
str[i--] = '\0'; |
66 |
if (str[0]) |
67 |
debug("\"%s\"", str); |
68 |
else { |
69 |
/* System ID: */ |
70 |
ofs = iso_type == 3? 16 : 8; |
71 |
memcpy(str, buf + ofs, sizeof(str)); |
72 |
str[32] = '\0'; i = 31; |
73 |
while (i >= 0 && str[i]==' ') |
74 |
str[i--] = '\0'; |
75 |
if (str[0]) |
76 |
debug("\"%s\"", str); |
77 |
else |
78 |
debug("(no ID)"); |
79 |
} |
80 |
|
81 |
debug(":%s\n", filename); |
82 |
} |
83 |
|
84 |
|
85 |
/* |
86 |
* iso_load_bootblock(): |
87 |
* |
88 |
* Try to load a kernel from an ISO 9660 disk image. iso_type is 1 for |
89 |
* "CD001" (standard), 2 for "CDW01" (ECMA), and 3 for "CDROM" (Sierra). |
90 |
* |
91 |
* TODO: This function uses too many magic offsets and so on; it should be |
92 |
* cleaned up some day. |
93 |
* |
94 |
* Returns 1 on success, 0 on failure. |
95 |
*/ |
96 |
int iso_load_bootblock(struct machine *m, struct cpu *cpu, |
97 |
int disk_id, int disk_type, int iso_type, unsigned char *buf, |
98 |
int *n_loadp, char ***load_namesp) |
99 |
{ |
100 |
int filenr, dirlen, res = 0, res2, iadd = DEBUG_INDENTATION, found_dir; |
101 |
uint64_t dirofs, fileofs, filelen; |
102 |
unsigned char *dirbuf = NULL, *dp, *match_entry = NULL, *filebuf = NULL; |
103 |
char *p, *filename_orig, *filename, *tmpfname = NULL; |
104 |
char **new_array; |
105 |
char *tmpdir = getenv("TMPDIR"); |
106 |
int tmpfile_handle; |
107 |
|
108 |
if (tmpdir == NULL) |
109 |
tmpdir = DEFAULT_TMP_DIR; |
110 |
|
111 |
CHECK_ALLOCATION(filename = strdup(cpu->machine->boot_kernel_filename)); |
112 |
filename_orig = filename; |
113 |
|
114 |
debug("ISO9660 boot:\n"); |
115 |
debug_indentation(iadd); |
116 |
|
117 |
debug_print_volume_id_and_filename(iso_type, buf, filename); |
118 |
|
119 |
|
120 |
/* |
121 |
* Traverse the directory structure to find the kernel. |
122 |
*/ |
123 |
|
124 |
dirlen = buf[0x84] + 256*buf[0x85] + 65536*buf[0x86]; |
125 |
if (dirlen != buf[0x8b] + 256*buf[0x8a] + 65536*buf[0x89]) |
126 |
fatal("WARNING: Root directory length mismatch?\n"); |
127 |
|
128 |
dirofs = (int64_t)(buf[0x8c] + (buf[0x8d] << 8) + (buf[0x8e] << 16) + |
129 |
((uint64_t)buf[0x8f] << 24)) * 2048; |
130 |
|
131 |
#ifdef ISO_DEBUG |
132 |
debug("root = %i bytes at 0x%llx\n", dirlen, (long long)dirofs); |
133 |
#endif |
134 |
|
135 |
CHECK_ALLOCATION(dirbuf = malloc(dirlen)); |
136 |
res2 = diskimage_access(m, disk_id, disk_type, 0, dirofs, dirbuf, |
137 |
dirlen); |
138 |
if (!res2) { |
139 |
fatal("Couldn't read the disk image. Aborting.\n"); |
140 |
goto ret; |
141 |
} |
142 |
|
143 |
found_dir = 1; /* Assume root dir */ |
144 |
dp = dirbuf; filenr = 1; |
145 |
p = NULL; |
146 |
while (dp < dirbuf + dirlen) { |
147 |
size_t i, nlen = dp[0]; |
148 |
int x = dp[2] + (dp[3] << 8) + (dp[4] << 16) + |
149 |
((uint64_t)dp[5] << 24); |
150 |
int y = dp[6] + (dp[7] << 8); |
151 |
char direntry[65]; |
152 |
|
153 |
dp += 8; |
154 |
|
155 |
/* |
156 |
* As long as there is an \ or / in the filename, then we |
157 |
* have not yet found the directory. |
158 |
*/ |
159 |
p = strchr(filename, '/'); |
160 |
if (p == NULL) |
161 |
p = strchr(filename, '\\'); |
162 |
|
163 |
#ifdef ISO_DEBUG |
164 |
debug("%i%s: %i, %i, \"", filenr, filenr == found_dir? |
165 |
" [CURRENT]" : "", x, y); |
166 |
#endif |
167 |
for (i=0; i<nlen && i<sizeof(direntry)-1; i++) |
168 |
if (dp[i]) { |
169 |
direntry[i] = dp[i]; |
170 |
#ifdef ISO_DEBUG |
171 |
debug("%c", dp[i]); |
172 |
#endif |
173 |
} else |
174 |
break; |
175 |
#ifdef ISO_DEBUG |
176 |
debug("\"\n"); |
177 |
#endif |
178 |
direntry[i] = '\0'; |
179 |
|
180 |
/* A directory name match? */ |
181 |
if ((p != NULL && strncasecmp(filename, direntry, nlen) == 0 |
182 |
&& nlen == (size_t)p - (size_t)filename && found_dir == y) |
183 |
|| (p == NULL && direntry[0] == '\0') ) { |
184 |
found_dir = filenr; |
185 |
if (p != NULL) |
186 |
filename = p+1; |
187 |
dirofs = 2048 * (int64_t)x; |
188 |
} |
189 |
|
190 |
dp += nlen; |
191 |
|
192 |
/* 16-bit aligned lenght: */ |
193 |
if (nlen & 1) |
194 |
dp ++; |
195 |
|
196 |
filenr ++; |
197 |
} |
198 |
|
199 |
p = strchr(filename, '/'); |
200 |
if (p == NULL) |
201 |
p = strchr(filename, '\\'); |
202 |
|
203 |
if (p != NULL) { |
204 |
char *blah = filename_orig; |
205 |
|
206 |
fatal("could not find '%s' in /", filename); |
207 |
|
208 |
/* Print the first part of the filename: */ |
209 |
while (blah != filename) |
210 |
fatal("%c", *blah++); |
211 |
|
212 |
fatal("\n"); |
213 |
goto ret; |
214 |
} |
215 |
|
216 |
/* debug("dirofs = 0x%llx\n", (long long)dirofs); */ |
217 |
|
218 |
/* Free the old dirbuf, and allocate a new one: */ |
219 |
free(dirbuf); |
220 |
CHECK_ALLOCATION(dirbuf = malloc(512)); |
221 |
|
222 |
for (;;) { |
223 |
size_t len, i; |
224 |
|
225 |
/* Too close to another sector? Then realign. */ |
226 |
if ((dirofs & 2047) + 70 > 2047) { |
227 |
dirofs = (dirofs | 2047) + 1; |
228 |
/* debug("realign dirofs = 0x%llx\n", dirofs); */ |
229 |
} |
230 |
|
231 |
res2 = diskimage_access(m, disk_id, disk_type, 0, dirofs, |
232 |
dirbuf, 256); |
233 |
if (!res2) { |
234 |
fatal("Couldn't read the disk image. Aborting.\n"); |
235 |
goto ret; |
236 |
} |
237 |
|
238 |
dp = dirbuf; |
239 |
len = dp[0]; |
240 |
if (len < 2) |
241 |
break; |
242 |
|
243 |
/* |
244 |
* TODO: Actually parse the directory entry! |
245 |
* |
246 |
* Haha, this must be rewritten. |
247 |
*/ |
248 |
|
249 |
#if 0 |
250 |
/* hahahaha */ |
251 |
printf("filename = '%s'\n", filename); |
252 |
{ |
253 |
int j; |
254 |
for (j=32; j<len; j++) |
255 |
printf("%c", dp[j] >= ' ' && dp[j] < 128? dp[j] : '.'); |
256 |
printf("\n"); |
257 |
} |
258 |
#endif |
259 |
|
260 |
for (i=32; i<len; i++) { |
261 |
if (i < len - strlen(filename)) |
262 |
if (strncmp(filename, (char *)dp + i, |
263 |
strlen(filename)) == 0) { |
264 |
/* The filename was found somewhere |
265 |
in the directory entry. */ |
266 |
if (match_entry != NULL) { |
267 |
fatal("TODO: I'm too lazy to" |
268 |
" implement a correct " |
269 |
"directory parser right " |
270 |
"now... (BUG)\n"); |
271 |
exit(1); |
272 |
} |
273 |
CHECK_ALLOCATION(match_entry = |
274 |
malloc(512)); |
275 |
memcpy(match_entry, dp, 512); |
276 |
break; |
277 |
} |
278 |
} |
279 |
|
280 |
if (match_entry != NULL) |
281 |
break; |
282 |
|
283 |
dirofs += len; |
284 |
} |
285 |
|
286 |
if (match_entry == NULL) { |
287 |
char *blah = filename_orig; |
288 |
|
289 |
fatal("could not find '%s' in /", filename); |
290 |
|
291 |
/* Print the first part of the filename: */ |
292 |
while (blah != filename) |
293 |
fatal("%c", *blah++); |
294 |
|
295 |
fatal("\n"); |
296 |
goto ret; |
297 |
} |
298 |
|
299 |
fileofs = match_entry[2] + (match_entry[3] << 8) + |
300 |
(match_entry[4] << 16) + ((uint64_t)match_entry[5] << 24); |
301 |
filelen = match_entry[10] + (match_entry[11] << 8) + |
302 |
(match_entry[12] << 16) + ((uint64_t)match_entry[13] << 24); |
303 |
fileofs *= 2048; |
304 |
|
305 |
/* debug("filelen=%llx fileofs=%llx\n", (long long)filelen, |
306 |
(long long)fileofs); */ |
307 |
|
308 |
CHECK_ALLOCATION(filebuf = malloc(filelen)); |
309 |
|
310 |
CHECK_ALLOCATION(tmpfname = malloc(300)); |
311 |
snprintf(tmpfname, 300, "%s/gxemul.XXXXXXXXXXXX", tmpdir); |
312 |
|
313 |
res2 = diskimage_access(m, disk_id, disk_type, 0, fileofs, filebuf, |
314 |
filelen); |
315 |
if (!res2) { |
316 |
fatal("could not read the file from the disk image!\n"); |
317 |
goto ret; |
318 |
} |
319 |
|
320 |
tmpfile_handle = mkstemp(tmpfname); |
321 |
if (tmpfile_handle < 0) { |
322 |
fatal("could not create %s\n", tmpfname); |
323 |
exit(1); |
324 |
} |
325 |
write(tmpfile_handle, filebuf, filelen); |
326 |
close(tmpfile_handle); |
327 |
|
328 |
debug("extracted %lli bytes into %s\n", (long long)filelen, tmpfname); |
329 |
|
330 |
/* Add the temporary filename to the load_namesp array: */ |
331 |
(*n_loadp)++; |
332 |
CHECK_ALLOCATION(new_array = malloc(sizeof(char *) * (*n_loadp))); |
333 |
memcpy(new_array, *load_namesp, sizeof(char *) * (*n_loadp)); |
334 |
*load_namesp = new_array; |
335 |
|
336 |
/* This adds a Backspace char in front of the filename; this |
337 |
is a special hack which causes the file to be removed once |
338 |
it has been loaded. */ |
339 |
CHECK_ALLOCATION(tmpfname = realloc(tmpfname, strlen(tmpfname) + 2)); |
340 |
memmove(tmpfname + 1, tmpfname, strlen(tmpfname) + 1); |
341 |
tmpfname[0] = 8; |
342 |
|
343 |
(*load_namesp)[*n_loadp - 1] = tmpfname; |
344 |
|
345 |
res = 1; |
346 |
|
347 |
ret: |
348 |
if (dirbuf != NULL) |
349 |
free(dirbuf); |
350 |
|
351 |
if (filebuf != NULL) |
352 |
free(filebuf); |
353 |
|
354 |
if (match_entry != NULL) |
355 |
free(match_entry); |
356 |
|
357 |
if (tmpfname != NULL) |
358 |
free(tmpfname); |
359 |
|
360 |
free(filename_orig); |
361 |
|
362 |
debug_indentation(-iadd); |
363 |
return res; |
364 |
} |
365 |
|