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: file_raw.c,v 1.2 2007/06/17 23:32:20 debug Exp $ |
29 |
* |
30 |
* COMMENT: Raw file support |
31 |
* |
32 |
* (Simply loads raw data into emulated memory space). |
33 |
*/ |
34 |
|
35 |
/* Note: Included from file.c. */ |
36 |
|
37 |
|
38 |
/* |
39 |
* file_load_raw(): |
40 |
* |
41 |
* Loads a raw binary into emulated memory. The filename should be |
42 |
* of the following form: loadaddress:filename |
43 |
* or loadaddress:skiplen:filename |
44 |
* or loadaddress:skiplen:pc:filename |
45 |
*/ |
46 |
static void file_load_raw(struct machine *m, struct memory *mem, |
47 |
char *filename, uint64_t *entrypointp) |
48 |
{ |
49 |
FILE *f; |
50 |
int len, sign3264; |
51 |
unsigned char buf[16384]; |
52 |
uint64_t entry, loadaddr, vaddr, skip = 0; |
53 |
char *p, *p2; |
54 |
|
55 |
/* Special case for 32-bit MIPS: */ |
56 |
sign3264 = 0; |
57 |
if (m->arch == ARCH_MIPS && m->cpus[0]->is_32bit) |
58 |
sign3264 = 1; |
59 |
|
60 |
p = strchr(filename, ':'); |
61 |
if (p == NULL) { |
62 |
fprintf(stderr, "\n"); |
63 |
perror(filename); |
64 |
exit(1); |
65 |
} |
66 |
|
67 |
loadaddr = vaddr = entry = strtoull(filename, NULL, 0); |
68 |
p2 = p+1; |
69 |
|
70 |
/* A second value? That's the optional skip value */ |
71 |
p = strchr(p2, ':'); |
72 |
if (p != NULL) { |
73 |
skip = strtoull(p2, NULL, 0); |
74 |
p = p+1; |
75 |
/* A third value? That's the initial pc: */ |
76 |
if (strchr(p, ':') != NULL) { |
77 |
entry = strtoull(p, NULL, 0); |
78 |
p = strchr(p, ':') + 1; |
79 |
} |
80 |
} else |
81 |
p = p2; |
82 |
|
83 |
if (sign3264) { |
84 |
loadaddr = (int64_t)(int32_t)loadaddr; |
85 |
entry = (int64_t)(int32_t)entry; |
86 |
vaddr = (int64_t)(int32_t)vaddr; |
87 |
skip = (int64_t)(int32_t)skip; |
88 |
} |
89 |
|
90 |
f = fopen(strrchr(filename, ':')+1, "r"); |
91 |
if (f == NULL) { |
92 |
perror(p); |
93 |
exit(1); |
94 |
} |
95 |
|
96 |
fseek(f, skip, SEEK_SET); |
97 |
|
98 |
/* Load file contents: */ |
99 |
while (!feof(f)) { |
100 |
size_t to_read = sizeof(buf); |
101 |
|
102 |
/* If vaddr isn't buf-size aligned, then start with a |
103 |
smaller buffer: */ |
104 |
if (vaddr & (sizeof(buf) - 1)) |
105 |
to_read = sizeof(buf) - (vaddr & (sizeof(buf)-1)); |
106 |
|
107 |
len = fread(buf, 1, to_read, f); |
108 |
|
109 |
if (len > 0) |
110 |
m->cpus[0]->memory_rw(m->cpus[0], mem, vaddr, &buf[0], |
111 |
len, MEM_WRITE, NO_EXCEPTIONS); |
112 |
|
113 |
vaddr += len; |
114 |
} |
115 |
|
116 |
debug("RAW: 0x%"PRIx64" bytes @ 0x%08"PRIx64, |
117 |
(uint64_t) (ftello(f) - skip), (uint64_t) loadaddr); |
118 |
|
119 |
if (skip != 0) |
120 |
debug(" (0x%"PRIx64" bytes of header skipped)", |
121 |
(uint64_t) skip); |
122 |
|
123 |
debug("\n"); |
124 |
|
125 |
fclose(f); |
126 |
|
127 |
*entrypointp = entry; |
128 |
|
129 |
n_executables_loaded ++; |
130 |
} |
131 |
|