Debian Codegraph @

src/jtag2rw.cc

raw symbols SCIP
1 /*
2 * avarice - The "avarice" program.
3 * Copyright (C) 2001 Scott Finneran
4 * Copyright (C) 2002, 2003, 2004 Intel Corporation
5 * Copyright (C) 2005,2006 Joerg Wunsch
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License Version 2
9 * as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
19 *
20 * This file implements access to the target memory for the mkII protocol.
21 *
22 * $Id: jtag2rw.cc 364 2016-03-03 20:49:47Z joerg_wunsch $
23 */
24
25
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <unistd.h>
30 #include <sys/stat.h>
31 #include <termios.h>
32 #include <fcntl.h>
33 #include <string.h>
34 #include <assert.h>
35
36 #include "avarice.h"
37 #include "jtag.h"
38 #include "jtag2.h"
39 #include "remote.h"
40
41
42 /** Return the memory space code for the memory space indicated by the
43 high-order bits of 'addr'. Also clear these high order bits in 'addr'
44 **/
45 uchar jtag2::memorySpace(unsigned long &addr)
46 {
47 int mask;
48
49 // We can't just mask the bits off, because 0x10000->0x1ffff are
50 // valid code addresses
51 if (addr & DATA_SPACE_ADDR_OFFSET)
52 {
53 mask = addr & ADDR_SPACE_MASK;
54 addr &= ~ADDR_SPACE_MASK;
55 }
56 else
57 mask = 0;
58
59 switch (mask)
60 {
61 case EEPROM_SPACE_ADDR_OFFSET:
62 if (proto != PROTO_DW && programmingEnabled)
63 return MTYPE_EEPROM_PAGE;
64 else
65 return MTYPE_EEPROM;
66 case FUSE_SPACE_ADDR_OFFSET:
67 return MTYPE_FUSE_BITS;
68 case LOCK_SPACE_ADDR_OFFSET:
69 return MTYPE_LOCK_BITS;
70 case SIG_SPACE_ADDR_OFFSET:
71 return MTYPE_SIGN_JTAG;
72 // ... return MTYPE_OSCCAL_BYTE;
73 // ... return MTYPE_CAN;
74 case BREAKPOINT_SPACE_ADDR_OFFSET:
75 return MTYPE_EVENT;
76 case REGISTER_SPACE_ADDR_OFFSET:
77 return MTYPE_XMEGA_REG;
78 case DATA_SPACE_ADDR_OFFSET:
79 return MTYPE_SRAM;
80 default:
81 if (is_xmega && has_full_xmega_support)
82 return MTYPE_XMEGA_APP_FLASH;
83 else if (proto == PROTO_DW || programmingEnabled)
84 return MTYPE_FLASH_PAGE;
85 else
86 return MTYPE_SPM;
87 }
88 }
89
90 uchar *jtag2::jtagRead(unsigned long addr, unsigned int numBytes)
91 {
92 uchar *response;
93 int responseSize;
94
95 if (numBytes == 0)
96 {
97 response = new uchar[1];
98 response[0] = '\0';
99 return response;
100 }
101
102 debugOut("jtagRead ");
103 uchar whichSpace = memorySpace(addr);
104 bool needProgmode = whichSpace >= MTYPE_FLASH_PAGE &&
105 whichSpace < MTYPE_XMEGA_REG;
106 unsigned int pageSize = 0;
107 unsigned int offset = 0;
108 bool wasProgmode = programmingEnabled;
109 if (needProgmode && !programmingEnabled)
110 enableProgramming();
111
112 unsigned char *cachePtr = NULL;
113 unsigned int *cacheBaseAddr = NULL;
114
115 switch (whichSpace)
116 {
117 // Pad to even byte count for flash memory.
118 // Even MTYPE_SPM appears to cause a RSP_FAILED
119 // otherwise.
120 case MTYPE_SPM:
121 offset = addr & 1;
122 addr &= ~1;
123 numBytes = (numBytes + 1) & ~1;
124 break;
125
126 case MTYPE_FLASH_PAGE:
127 case MTYPE_XMEGA_APP_FLASH:
128 pageSize = deviceDef->flash_page_size;
129 cachePtr = flashCache;
130 cacheBaseAddr = &flashCachePageAddr;
131 break;
132
133 case MTYPE_EEPROM_PAGE:
134 pageSize = deviceDef->eeprom_page_size;
135 cachePtr = eepromCache;
136 cacheBaseAddr = &eepromCachePageAddr;
137 break;
138 }
139
140 uchar command[10] = { CMND_READ_MEMORY };
141 command[1] = whichSpace;
142
143 if (pageSize > 0) {
144 if (pageSize > 256)
145 pageSize = 256; // more cannot be handled
146 u32_to_b4(command + 2, pageSize);
147 response = new uchar[numBytes];
148
149 unsigned int mask = pageSize - 1;
150 unsigned int pageAddr = addr & ~mask;
151 unsigned int chunksize = numBytes;
152 unsigned int targetOffset = 0;
153
154 if (addr + chunksize >= pageAddr + pageSize)
155 // Chunk would cross a page boundary, reduce it
156 // appropriately.
157 chunksize -= (addr + numBytes - (pageAddr + pageSize));
158 offset = addr - pageAddr;
159
160 while (numBytes > 0)
161 {
162 uchar *resp;
163
164 if (pageAddr == *cacheBaseAddr)
165 {
166 // quickly fetch from page cache
167 memcpy(response + targetOffset,
168 cachePtr + offset,
169 chunksize);
170 }
171 else
172 {
173 // read from device, cache result, and copy over our part
174 u32_to_b4(command + 6, pageAddr);
175 try
176 {
177 doJtagCommand(command, sizeof command, resp, responseSize, true);
178 }
179 catch (jtag_exception& e)
180 {
181 fprintf(stderr, "Failed to read target memory space: %s\n",
182 e.what());
183 delete [] response;
184 throw;
185 }
186 memcpy(cachePtr, resp + 1, pageSize);
187 *cacheBaseAddr = pageAddr;
188 memcpy(response + targetOffset,
189 cachePtr + offset,
190 chunksize);
191 delete [] resp;
192 }
193
194 numBytes -= chunksize;
195 targetOffset += chunksize;
196
197 chunksize = numBytes > pageSize? pageSize: numBytes;
198 pageAddr += pageSize;
199 }
200 } else {
201 u32_to_b4(command + 2, numBytes);
202 u32_to_b4(command + 6, addr);
203
204 try
205 {
206 doJtagCommand(command, sizeof command, response, responseSize, true);
207 }
208 catch (jtag_exception& e)
209 {
210 fprintf(stderr, "Failed to read target memory space: %s\n",
211 e.what());
212 throw;
213 }
214 if (offset > 0)
215 memmove(response, response + 1 + offset, responseSize - 1 - offset);
216 else
217 memmove(response, response + 1, responseSize - 1);
218 }
219
220 if (needProgmode && !wasProgmode)
221 disableProgramming();
222
223 return response;
224 }
225
226 void jtag2::jtagWrite(unsigned long addr, unsigned int numBytes, uchar buffer[])
227 {
228 if (numBytes == 0)
229 return;
230
231 debugOut("jtagWrite ");
232 uchar whichSpace = memorySpace(addr);
233
234 // Hack to detect the start of a GDB "load" command. Iff this
235 // address is tied to flash ROM, and it is address 0, and the size
236 // is larger than 4 bytes, assume it's the first block of a "load"
237 // command. If so, chip erase the device, and switch over to
238 // programming mode to speed up things (drastically).
239
240 if (proto != PROTO_DW &&
241 whichSpace == MTYPE_SPM &&
242 addr == 0 &&
243 numBytes > 4)
244 {
245 debugOut("Detected GDB \"load\" command, erasing flash.\n");
246 //whichSpace = MTYPE_FLASH_PAGE; // this will turn on progmode
247 eraseProgramMemory();
248 }
249
250 bool needProgmode = whichSpace >= MTYPE_FLASH_PAGE &&
251 whichSpace < MTYPE_XMEGA_REG;
252 unsigned int pageSize = 0;
253 bool wasProgmode = programmingEnabled;
254 if (needProgmode && !programmingEnabled)
255 enableProgramming();
256
257 switch (whichSpace)
258 {
259 case MTYPE_FLASH_PAGE:
260 case MTYPE_XMEGA_APP_FLASH:
261 pageSize = deviceDef->flash_page_size;
262 break;
263
264 case MTYPE_EEPROM_PAGE:
265 pageSize = deviceDef->eeprom_page_size;
266 break;
267 }
268 unsigned int chunksize = numBytes;
269 if (pageSize > 0) {
270 unsigned int mask = pageSize - 1;
271 addr &= ~mask;
272 if (numBytes != pageSize)
273 throw ("jtagWrite(): numByte does not match page size");
274 if (pageSize > 256) {
275 chunksize = 256; // that's all the JTAGICEmkII can handle at a time
276 }
277 }
278 uchar *command = new uchar [10 + chunksize];
279 command[0] = CMND_WRITE_MEMORY;
280 command[1] = whichSpace;
281 uchar *bp = buffer;
282
283 do
284 {
285 u32_to_b4(command + 2, chunksize);
286 u32_to_b4(command + 6, addr);
287 memcpy(command + 10, bp, chunksize);
288
289 uchar *response;
290 int responseSize;
291
292 try
293 {
294 doJtagCommand(command, 10 + chunksize, response, responseSize);
295 }
296 catch (jtag_exception& e)
297 {
298 fprintf(stderr, "Failed to write target memory space: %s\n",
299 e.what());
300 throw;
301 }
302
303 delete [] response;
304
305 addr += chunksize;
306 numBytes -= chunksize;
307 bp += chunksize;
308 } while (numBytes != 0);
309
310 delete [] command;
311
312 if (needProgmode && !wasProgmode)
313 disableProgramming();
314 }