file_srec.cc Source File

Back to the index.

file_srec.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2003-2009 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  * COMMENT: SREC file support
29  */
30 
31 /* Note: Included from file.c. */
32 
33 
34 /*
35  * file_load_srec():
36  *
37  * Loads a Motorola SREC file into emulated memory. Description of the SREC
38  * file format can be found at here:
39  *
40  * http://www.ndsu.nodak.edu/instruct/tareski/373f98/notes/srecord.htm
41  * or http://www.amelek.gda.pl/avr/uisp/srecord.htm
42  */
43 static void file_load_srec(struct machine *m, struct memory *mem,
44  char *filename, uint64_t *entrypointp)
45 {
46  FILE *f;
47  unsigned char buf[516];
48  unsigned char bytes[270];
49  uint64_t entry = 0, vaddr = 0;
50  int i, j, count;
51  char ch;
52  int buf_len, data_start = 0;
53  int entry_set = 0;
54  int warning = 0;
55  int warning_len = 0;
56  int total_bytes_loaded = 0;
57 
58  f = fopen(filename, "r");
59  if (f == NULL) {
60  perror(filename);
61  exit(1);
62  }
63 
64  /* Load file contents: */
65  while (!feof(f)) {
66  memset(buf, 0, sizeof(buf));
67  if (fgets((char *)buf, sizeof(buf)-1, f) == NULL ||
68  buf[0] == 0 || buf[0]=='\r' || buf[0]=='\n')
69  continue;
70 
71  if (buf[0] != 'S') {
72  if (!warning)
73  debug("WARNING! non-S-record found\n");
74  warning = 1;
75  continue;
76  }
77 
78  buf_len = strlen((char *)buf);
79 
80  if (buf_len < 10) {
81  if (!warning_len)
82  debug("WARNING! invalid S-record found\n");
83  warning_len = 1;
84  continue;
85  }
86 
87  /*
88  * Stype count address data checksum
89  * 01 23 4.. .. (last 2 bytes)
90  *
91  * TODO: actually check the checksum
92  */
93 
94  j = 0;
95  for (i=1; i<buf_len; i++) {
96  if (buf[i]>='a' && buf[i]<='f')
97  buf[i] += 10 - 'a';
98  else if (buf[i] >= 'A' && buf[i] <= 'F')
99  buf[i] += 10 - 'A';
100  else if (buf[i] >= '0' && buf[i] <= '9')
101  buf[i] -= '0';
102  else if (buf[i] == '\r' || buf[i] == '\n') {
103  } else
104  fatal("invalid characters '%c' in S-record\n",
105  buf[i]);
106 
107  if (i >= 4) {
108  if (i & 1)
109  bytes[j++] += buf[i];
110  else
111  bytes[j] = buf[i] * 16;
112  }
113  }
114 
115  count = buf[2] * 16 + buf[3];
116  /* debug("count=%i j=%i\n", count, j); */
117  /* count is j - 1. */
118 
119  switch (buf[1]) {
120  case 0:
121  debug("SREC \"");
122  for (i=2; i<count-1; i++) {
123  ch = bytes[i];
124  if (ch >= ' ' && ch < 127)
125  debug("%c", ch);
126  else
127  debug("?");
128  }
129  debug("\"\n");
130  break;
131  case 1:
132  case 2:
133  case 3:
134  /* switch again, to get the load address: */
135  switch (buf[1]) {
136  case 1: data_start = 2;
137  vaddr = (bytes[0] << 8) + bytes[1];
138  break;
139  case 2: data_start = 3;
140  vaddr = (bytes[0] << 16) + (bytes[1] << 8) +
141  bytes[2];
142  break;
143  case 3: data_start = 4;
144  vaddr = ((uint64_t)bytes[0] << 24) +
145  (bytes[1] << 16) + (bytes[2]<<8) + bytes[3];
146  }
147  m->cpus[0]->memory_rw(m->cpus[0], mem, vaddr,
148  &bytes[data_start], count - 1 - data_start,
150  total_bytes_loaded += count - 1 - data_start;
151  break;
152  case 7:
153  case 8:
154  case 9:
155  /* switch again, to get the entry point: */
156  switch (buf[1]) {
157  case 7: entry = ((uint64_t)bytes[0] << 24) +
158  (bytes[1] << 16) + (bytes[2]<<8) + bytes[3];
159  break;
160  case 8: entry = (bytes[0] << 16) + (bytes[1] << 8) +
161  bytes[2];
162  break;
163  case 9: entry = (bytes[0] << 8) + bytes[1];
164  break;
165  }
166  entry_set = 1;
167  debug("entry point 0x%08x\n", (unsigned int)entry);
168  break;
169  default:
170  debug("unimplemented S-record type %i\n", buf[1]);
171  }
172  }
173 
174  debug("0x%x bytes loaded\n", total_bytes_loaded);
175 
176  fclose(f);
177 
178  if (!entry_set)
179  debug("WARNING! no entrypoint found!\n");
180  else
181  *entrypointp = entry;
182 
183  n_executables_loaded ++;
184 }
185 
void fatal(const char *fmt,...)
Definition: main.cc:152
void f(int s, int func, int only_name)
struct cpu ** cpus
Definition: machine.h:140
int(* memory_rw)(struct cpu *cpu, struct memory *mem, uint64_t vaddr, unsigned char *data, size_t len, int writeflag, int cache_flags)
Definition: cpu.h:365
#define MEM_WRITE
Definition: memory.h:117
#define debug
Definition: dev_adb.cc:57
#define NO_EXCEPTIONS
Definition: memory.h:125
void COMBINE() strlen(struct cpu *cpu, struct arm_instr_call *ic, int low_addr)
Definition: memory.h:75

Generated on Fri Dec 7 2018 19:52:23 for GXemul by doxygen 1.8.13