| 1 | /* |
| 2 | * avarice - The "avarice" program. |
| 3 | * Copyright (C) 2001 Scott Finneran |
| 4 | * Copyright (C) 2002, 2003, 2004 Intel Corporation |
| 5 | * Copyright (C) 2005 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 contains functions for interfacing with the JTAG box. |
| 21 | * |
| 22 | * $Id: jtagrw.cc 333 2013-01-10 20:44:28Z 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 "jtag1.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 | static uchar 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: return ADDR_EEPROM_SPACE; |
| 62 | case FUSE_SPACE_ADDR_OFFSET: return ADDR_FUSE_SPACE; |
| 63 | case LOCK_SPACE_ADDR_OFFSET: return ADDR_LOCK_SPACE; |
| 64 | case SIG_SPACE_ADDR_OFFSET: return ADDR_SIG_SPACE; |
| 65 | case BREAKPOINT_SPACE_ADDR_OFFSET: return ADDR_BREAKPOINT_SPACE; |
| 66 | case DATA_SPACE_ADDR_OFFSET: return ADDR_DATA_SPACE; |
| 67 | default: return 0; // program memory, handled specially |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | static void swapBytes(uchar *buffer, unsigned int count) |
| 72 | { |
| 73 | assert(!(count & 1)); |
| 74 | for (unsigned int i = 0; i < count; i += 2) |
| 75 | { |
| 76 | uchar temp = buffer[i]; |
| 77 | buffer[i] = buffer[i + 1]; |
| 78 | buffer[i + 1] = temp; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | |
| 83 | uchar *jtag1::jtagRead(unsigned long addr, unsigned int numBytes) |
| 84 | { |
| 85 | uchar *response; |
| 86 | int whichSpace = 0; |
| 87 | uchar command[] = { 'R', 0, 0, 0, 0, 0, JTAG_EOM }; |
| 88 | |
| 89 | if (numBytes == 0) |
| 90 | { |
| 91 | response = new uchar[1]; |
| 92 | response[0] = '\0'; |
| 93 | return response; |
| 94 | } |
| 95 | |
| 96 | debugOut("jtagRead "); |
| 97 | whichSpace = memorySpace(&addr); |
| 98 | if (whichSpace) |
| 99 | { |
| 100 | command[1] = whichSpace; |
| 101 | if (numBytes > 256) |
| 102 | return NULL; |
| 103 | command[2] = numBytes - 1; |
| 104 | encodeAddress(&command[3], addr); |
| 105 | |
| 106 | // Response will be the number of data bytes with an 'A' at the |
| 107 | // start and end. As such, the response size will be number of bytes |
| 108 | // + 2. Then add an additional byte for the trailing zero (see |
| 109 | // protocol document). |
| 110 | |
| 111 | response = doJtagCommand(command, sizeof command, numBytes + 2); |
| 112 | |
| 113 | if (response[numBytes + 1] == JTAG_R_OK) |
| 114 | return response; |
| 115 | |
| 116 | delete [] response; |
| 117 | |
| 118 | throw jtag_exception(); |
| 119 | } |
| 120 | else |
| 121 | { |
| 122 | |
| 123 | // Reading program memory |
| 124 | whichSpace = programmingEnabled ? |
| 125 | ADDR_PROG_SPACE_PROG_ENABLED : ADDR_PROG_SPACE_PROG_DISABLED; |
| 126 | |
| 127 | // Program space is 16 bits wide, with word reads |
| 128 | int numLocations; |
| 129 | if (addr & 1) |
| 130 | numLocations = (numBytes + 2) / 2; |
| 131 | else |
| 132 | numLocations = (numBytes + 1) / 2; |
| 133 | if (numLocations > 256) |
| 134 | throw jtag_exception(); |
| 135 | |
| 136 | command[1] = whichSpace; |
| 137 | command[2] = numLocations - 1; |
| 138 | encodeAddress(&command[3], addr / 2); |
| 139 | |
| 140 | response = doJtagCommand(command, sizeof command, numLocations * 2 + 2); |
| 141 | |
| 142 | if (response[numLocations * 2 + 1] == JTAG_R_OK) |
| 143 | { |
| 144 | // Programming mode and regular mode are byte-swapped... |
| 145 | if (!programmingEnabled) |
| 146 | swapBytes(response, numLocations * 2); |
| 147 | |
| 148 | if (addr & 1) |
| 149 | // we read one byte early. move stuff down. |
| 150 | memmove(response, response + 1, numBytes); |
| 151 | |
| 152 | return response; |
| 153 | } |
| 154 | |
| 155 | delete [] response; |
| 156 | |
| 157 | throw jtag_exception(); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | void jtag1::jtagWrite(unsigned long addr, unsigned int numBytes, uchar buffer[]) |
| 162 | { |
| 163 | uchar *response; |
| 164 | int whichSpace = 0; |
| 165 | unsigned int numLocations; |
| 166 | uchar command[] = { 'W', 0, 0, 0, 0, 0, JTAG_EOM }; |
| 167 | |
| 168 | if (numBytes == 0) |
| 169 | return; |
| 170 | |
| 171 | debugOut("jtagWrite "); |
| 172 | whichSpace = memorySpace(&addr); |
| 173 | |
| 174 | if (whichSpace) |
| 175 | numLocations = numBytes; |
| 176 | else |
| 177 | { |
| 178 | // Writing program memory, which is word (16-bit) addressed |
| 179 | |
| 180 | // We don't handle odd lengths or start addresses |
| 181 | if ((addr & 1)) |
| 182 | { |
| 183 | throw jtag_exception("Odd program memory write operation"); |
| 184 | } |
| 185 | |
| 186 | // Odd length: Write one more byte. |
| 187 | if ((numBytes & 1)) |
| 188 | { |
| 189 | debugOut ("\nOdd pgm wr length\n"); |
| 190 | numBytes+=1; |
| 191 | } |
| 192 | |
| 193 | addr /= 2; |
| 194 | numLocations = numBytes / 2; |
| 195 | |
| 196 | if (programmingEnabled) |
| 197 | whichSpace = ADDR_PROG_SPACE_PROG_ENABLED; |
| 198 | else |
| 199 | { |
| 200 | whichSpace = ADDR_PROG_SPACE_PROG_DISABLED; |
| 201 | swapBytes(buffer, numBytes); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | // This is the maximum write size |
| 206 | if (numLocations > 256) |
| 207 | throw jtag_exception("Attempt to write more than 256 bytes"); |
| 208 | |
| 209 | // Writing is a two part process |
| 210 | |
| 211 | // Part 1: send the address |
| 212 | command[1] = whichSpace; |
| 213 | command[2] = numLocations - 1; |
| 214 | encodeAddress(&command[3], addr); |
| 215 | |
| 216 | response = doJtagCommand(command, sizeof command, 0); |
| 217 | if (!response) |
| 218 | throw jtag_exception(); |
| 219 | delete [] response; |
| 220 | |
| 221 | // Part 2: send the data in the following form: |
| 222 | // h [data byte]...[data byte] __ |
| 223 | |
| 224 | // Before we begin, a little note on the endianness. |
| 225 | // Firstly, data space is 8 bits wide and the only data written by |
| 226 | // this routine will be byte-wide, so endianness does not matter. |
| 227 | |
| 228 | // For code space, the data is 16 bit wide. The data appears to be |
| 229 | // formatted big endian in the processor memory. The JTAG box |
| 230 | // expects little endian data. The object files output from GCC are |
| 231 | // already word-wise little endian. |
| 232 | |
| 233 | // As this data is already formatted, and currently the only writes |
| 234 | // to program space are for code download, it is simpler at this |
| 235 | // stage to simply pass the data straight through. This may need to |
| 236 | // change in the future. |
| 237 | uchar *txBuffer = new uchar[numBytes + 3]; // allow for header and trailer |
| 238 | txBuffer[0] = 'h'; |
| 239 | |
| 240 | memcpy(&txBuffer[1], buffer, numBytes); |
| 241 | |
| 242 | txBuffer[numBytes + 1] = ' '; |
| 243 | txBuffer[numBytes + 2] = ' '; |
| 244 | |
| 245 | response = doJtagCommand(txBuffer, numBytes + 3, 1); |
| 246 | delete [] txBuffer; |
| 247 | |
| 248 | if (!response) |
| 249 | throw jtag_exception(); |
| 250 | delete [] response; |
| 251 | } |
| 252 |