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