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