| 1 | /* |
| 2 | * avarice - The "avarice" program. |
| 3 | * Copyright (C) 2001 Scott Finneran |
| 4 | * Copyright (C) 2002, 2003, 2004 Intel Corporation |
| 5 | * Copyright (C) 2005, 2007 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 those parts of class "jtag" that are common |
| 21 | * for both, the mkI and mkII protocol. |
| 22 | * |
| 23 | * $Id: jtaggeneric.cc 328 2013-01-08 20:23:00Z joerg_wunsch $ |
| 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 <sys/time.h> |
| 32 | #include <termios.h> |
| 33 | #include <fcntl.h> |
| 34 | #include <string.h> |
| 35 | #include <errno.h> |
| 36 | |
| 37 | #include "avarice.h" |
| 38 | #include "jtag.h" |
| 39 | |
| 40 | const char *BFDmemoryTypeString[] = { |
| 41 | "FLASH", |
| 42 | "EEPROM", |
| 43 | "RAM", |
| 44 | }; |
| 45 | |
| 46 | const int BFDmemorySpaceOffset[] = { |
| 47 | FLASH_SPACE_ADDR_OFFSET, |
| 48 | EEPROM_SPACE_ADDR_OFFSET, |
| 49 | DATA_SPACE_ADDR_OFFSET, |
| 50 | }; |
| 51 | |
| 52 | /* |
| 53 | * Generic functions applicable to both, the mkI and mkII ICE. |
| 54 | */ |
| 55 | |
| 56 | void jtag::restoreSerialPort() |
| 57 | { |
| 58 | if (!is_usb && jtagBox >= 0 && oldtioValid) |
| 59 | tcsetattr(jtagBox, TCSANOW, &oldtio); |
| 60 | } |
| 61 | |
| 62 | jtag::jtag(void) |
| 63 | { |
| 64 | jtagBox = 0; |
| 65 | softbp_only = is_xmega = oldtioValid = is_usb = false; |
| 66 | } |
| 67 | |
| 68 | jtag::jtag(const char *jtagDeviceName, char *name, emulator type) |
| 69 | { |
| 70 | struct termios newtio; |
| 71 | |
| 72 | jtagBox = 0; |
| 73 | oldtioValid = is_usb = false; |
| 74 | device_name = name; |
| 75 | emu_type = type; |
| 76 | programmingEnabled = 0; |
| 77 | deviceDef = NULL; |
| 78 | if (strncmp(jtagDeviceName, "usb", 3) == 0) |
| 79 | { |
| 80 | #ifdef HAVE_LIBUSB |
| 81 | is_usb = true; |
| 82 | openUSB(jtagDeviceName); |
| 83 | #else |
| 84 | throw "avarice has not been compiled with libusb support\n"; |
| 85 | #endif |
| 86 | } |
| 87 | else |
| 88 | { |
| 89 | // Open modem device for reading and writing and not as controlling |
| 90 | // tty because we don't want to get killed if linenoise sends |
| 91 | // CTRL-C. |
| 92 | jtagBox = open(jtagDeviceName, O_RDWR | O_NOCTTY | O_NONBLOCK); |
| 93 | if (jtagBox < 0) |
| 94 | { |
| 95 | fprintf(stderr, "Failed to open %s", jtagDeviceName); |
| 96 | throw jtag_exception(); |
| 97 | } |
| 98 | |
| 99 | // save current serial port settings and plan to restore them on exit |
| 100 | if (tcgetattr(jtagBox, &oldtio) < 0) |
| 101 | throw jtag_exception(); |
| 102 | oldtioValid = true; |
| 103 | |
| 104 | memset(&newtio, 0, sizeof(newtio)); |
| 105 | newtio.c_cflag = CS8 | CLOCAL | CREAD; |
| 106 | |
| 107 | // set baud rates in a platform-independent manner |
| 108 | if (cfsetospeed(&newtio, B19200) < 0 || |
| 109 | cfsetispeed(&newtio, B19200) < 0) |
| 110 | throw jtag_exception(); |
| 111 | |
| 112 | // IGNPAR : ignore bytes with parity errors |
| 113 | // otherwise make device raw (no other input processing) |
| 114 | newtio.c_iflag = IGNPAR; |
| 115 | |
| 116 | // Raw output. |
| 117 | newtio.c_oflag = 0; |
| 118 | |
| 119 | // Raw input. |
| 120 | newtio.c_lflag = 0; |
| 121 | |
| 122 | // The following configuration should cause read to return if 2 |
| 123 | // characters are immediately avaible or if the period between |
| 124 | // characters exceeds 5 * .1 seconds. |
| 125 | newtio.c_cc[VTIME] = 5; // inter-character timer unused |
| 126 | newtio.c_cc[VMIN] = 255; // blocking read until VMIN character |
| 127 | // arrives |
| 128 | |
| 129 | // now clean the serial line and activate the settings for the port |
| 130 | if (tcflush(jtagBox, TCIFLUSH) < 0 || |
| 131 | tcsetattr(jtagBox, TCSANOW, &newtio) < 0) |
| 132 | throw jtag_exception(); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | // NB: the destructor is virtual; class jtag2 extends it |
| 137 | jtag::~jtag(void) |
| 138 | { |
| 139 | restoreSerialPort(); |
| 140 | } |
| 141 | |
| 142 | |
| 143 | int jtag::timeout_read(void *buf, size_t count, unsigned long timeout) |
| 144 | { |
| 145 | char *buffer = (char *)buf; |
| 146 | size_t actual = 0; |
| 147 | |
| 148 | while (actual < count) |
| 149 | { |
| 150 | fd_set readfds; |
| 151 | FD_ZERO(&readfds); |
| 152 | FD_SET(jtagBox, &readfds); |
| 153 | |
| 154 | struct timeval tmout; |
| 155 | tmout.tv_sec = timeout / 1000000; |
| 156 | tmout.tv_usec = timeout % 1000000; |
| 157 | |
| 158 | int selected = select(jtagBox + 1, &readfds, NULL, NULL, &tmout); |
| 159 | /* Even though select() is not supposed to set errno to EAGAIN |
| 160 | (according to the linux man page), it seems that errno can be set |
| 161 | to EAGAIN on some cygwin systems. Thus, we need to catch that |
| 162 | here. */ |
| 163 | if ((selected < 0) && (errno == EAGAIN || errno == EINTR)) |
| 164 | continue; |
| 165 | |
| 166 | if (selected == 0) |
| 167 | return actual; |
| 168 | |
| 169 | ssize_t thisread = read(jtagBox, &buffer[actual], count - actual); |
| 170 | if ((thisread < 0) && (errno == EAGAIN)) |
| 171 | continue; |
| 172 | if (thisread < 0) |
| 173 | throw jtag_exception(); |
| 174 | |
| 175 | actual += thisread; |
| 176 | } |
| 177 | |
| 178 | return count; |
| 179 | } |
| 180 | |
| 181 | int jtag::safewrite(const void *b, int count) |
| 182 | { |
| 183 | char *buffer = (char *)b; |
| 184 | int actual = 0; |
| 185 | int flags = fcntl(jtagBox, F_GETFL); |
| 186 | |
| 187 | fcntl(jtagBox, F_SETFL, 0); // blocking mode |
| 188 | while (count > 0) |
| 189 | { |
| 190 | int n = write(jtagBox, buffer, count); |
| 191 | |
| 192 | if (n == -1 && errno == EINTR) |
| 193 | continue; |
| 194 | if (n == -1) |
| 195 | { |
| 196 | actual = -1; |
| 197 | break; |
| 198 | } |
| 199 | |
| 200 | count -= n; |
| 201 | actual += n; |
| 202 | buffer += n; |
| 203 | } |
| 204 | fcntl(jtagBox, F_SETFL, flags); |
| 205 | return actual; |
| 206 | } |
| 207 | |
| 208 | /** Change bitrate of PC's serial port as specified by BIT_RATE_xxx in |
| 209 | 'newBitRate' **/ |
| 210 | void jtag::changeLocalBitRate(int newBitRate) |
| 211 | { |
| 212 | if (is_usb) |
| 213 | return; |
| 214 | |
| 215 | // Change the local port bitrate. |
| 216 | struct termios tio; |
| 217 | |
| 218 | if (tcgetattr(jtagBox, &tio) < 0) |
| 219 | throw jtag_exception(); |
| 220 | |
| 221 | speed_t newPortSpeed = B19200; |
| 222 | // Linux doesn't support 14400. Let's hope it doesn't end up there... |
| 223 | switch(newBitRate) |
| 224 | { |
| 225 | case 9600: |
| 226 | newPortSpeed = B9600; |
| 227 | break; |
| 228 | case 19200: |
| 229 | newPortSpeed = B19200; |
| 230 | break; |
| 231 | case 38400: |
| 232 | newPortSpeed = B38400; |
| 233 | break; |
| 234 | case 57600: |
| 235 | newPortSpeed = B57600; |
| 236 | break; |
| 237 | case 115200: |
| 238 | newPortSpeed = B115200; |
| 239 | break; |
| 240 | default: |
| 241 | debugOut("unsupported bitrate: %d\n", newBitRate); |
| 242 | throw jtag_exception("unsupported bitrate"); |
| 243 | } |
| 244 | |
| 245 | cfsetospeed(&tio, newPortSpeed); |
| 246 | cfsetispeed(&tio, newPortSpeed); |
| 247 | |
| 248 | if (tcsetattr(jtagBox,TCSANOW,&tio) < 0 || |
| 249 | tcflush(jtagBox, TCIFLUSH) < 0) |
| 250 | throw jtag_exception(); |
| 251 | } |
| 252 | |
| 253 | static bool pageIsEmpty(BFDimage *image, unsigned int addr, unsigned int size, |
| 254 | BFDmemoryType memtype) |
| 255 | { |
| 256 | bool emptyPage = true; |
| 257 | |
| 258 | // Check if page is used |
| 259 | for (unsigned int idx=addr; idx<addr+size; idx++) |
| 260 | { |
| 261 | if (idx >= image->last_address) |
| 262 | break; |
| 263 | |
| 264 | // 1. If this address existed in input file, mark as ! empty. |
| 265 | // 2. If we are programming FLASH, and contents == 0xff, we need |
| 266 | // not program (is 0xff after erase). |
| 267 | if (image->image[idx].used) |
| 268 | { |
| 269 | if (!((memtype == MEM_FLASH) && |
| 270 | (image->image[idx].val == 0xff))) |
| 271 | { |
| 272 | emptyPage = false; |
| 273 | break; |
| 274 | } |
| 275 | } |
| 276 | } |
| 277 | return emptyPage; |
| 278 | } |
| 279 | |
| 280 | |
| 281 | unsigned int jtag::get_page_size(BFDmemoryType memtype) |
| 282 | { |
| 283 | unsigned int page_size; |
| 284 | switch( memtype ) { |
| 285 | case MEM_FLASH: |
| 286 | page_size = deviceDef->flash_page_size; |
| 287 | break; |
| 288 | case MEM_EEPROM: |
| 289 | page_size = deviceDef->eeprom_page_size; |
| 290 | break; |
| 291 | default: |
| 292 | page_size = 1; |
| 293 | break; |
| 294 | } |
| 295 | return page_size; |
| 296 | } |
| 297 | |
| 298 | |
| 299 | void jtag::jtag_flash_image(BFDimage *image, BFDmemoryType memtype, |
| 300 | bool program, bool verify) |
| 301 | { |
| 302 | unsigned int page_size = get_page_size(memtype); |
| 303 | static uchar buf[MAX_IMAGE_SIZE]; |
| 304 | unsigned int i; |
| 305 | uchar *response = NULL; |
| 306 | unsigned int addr; |
| 307 | |
| 308 | if (! image->has_data) |
| 309 | { |
| 310 | fprintf(stderr, "File contains no data.\n"); |
| 311 | return; |
| 312 | } |
| 313 | |
| 314 | |
| 315 | if (program) |
| 316 | { |
| 317 | // First address must start on page boundary. |
| 318 | addr = page_addr(image->first_address, memtype); |
| 319 | |
| 320 | statusOut("Downloading %s image to target.", image->name); |
| 321 | statusFlush(); |
| 322 | |
| 323 | while (addr < image->last_address) |
| 324 | { |
| 325 | if (!pageIsEmpty(image, addr, page_size, memtype)) |
| 326 | { |
| 327 | // Must also convert address to gcc-hacked addr for jtagWrite |
| 328 | debugOut("Writing page at addr 0x%.4lx size 0x%lx\n", |
| 329 | addr, page_size); |
| 330 | |
| 331 | // Create raw data buffer |
| 332 | for (i=0; i<page_size; i++) |
| 333 | buf[i] = image->image[i+addr].val; |
| 334 | |
| 335 | try |
| 336 | { |
| 337 | jtagWrite(BFDmemorySpaceOffset[memtype] + addr, |
| 338 | page_size, |
| 339 | buf); |
| 340 | } |
| 341 | catch (jtag_exception& e) |
| 342 | { |
| 343 | fprintf(stderr, "Error writing to target: %s\n", |
| 344 | e.what()); |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | addr += page_size; |
| 349 | |
| 350 | statusOut("."); |
| 351 | statusFlush(); |
| 352 | } |
| 353 | |
| 354 | statusOut("\n"); |
| 355 | statusFlush(); |
| 356 | } |
| 357 | |
| 358 | if (verify) |
| 359 | { |
| 360 | bool is_verified = true; |
| 361 | |
| 362 | // First address must start on page boundary. |
| 363 | addr = page_addr(image->first_address, memtype); |
| 364 | |
| 365 | statusOut("\nVerifying %s", image->name); |
| 366 | statusFlush(); |
| 367 | |
| 368 | while (addr < image->last_address) |
| 369 | { |
| 370 | // Must also convert address to gcc-hacked addr for jtagWrite |
| 371 | debugOut("Verifying page at addr 0x%.4lx size 0x%lx\n", |
| 372 | addr, page_size); |
| 373 | |
| 374 | response = jtagRead(BFDmemorySpaceOffset[memtype] + addr, |
| 375 | page_size); |
| 376 | |
| 377 | // Verify buffer, but only addresses in use. |
| 378 | for (i=0; i < page_size; i++) |
| 379 | { |
| 380 | unsigned int c = i + addr; |
| 381 | if (image->image[c].used ) |
| 382 | { |
| 383 | if (image->image[c].val != response[i]) |
| 384 | { |
| 385 | statusOut("\nError verifying target addr %.4x. " |
| 386 | "Expect [0x%02x] Got [0x%02x]", |
| 387 | c, image->image[c].val, response[i]); |
| 388 | statusFlush(); |
| 389 | is_verified = false; |
| 390 | } |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | addr += page_size; |
| 395 | |
| 396 | statusOut("."); |
| 397 | statusFlush(); |
| 398 | } |
| 399 | delete [] response; |
| 400 | |
| 401 | statusOut("\n"); |
| 402 | statusFlush(); |
| 403 | |
| 404 | if (!is_verified) |
| 405 | { |
| 406 | fprintf(stderr, "\nVerification failed!\n"); |
| 407 | throw jtag_exception(); |
| 408 | } |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | void jtag::jtagWriteFuses(char *fuses) |
| 413 | { |
| 414 | int temp[3]; |
| 415 | uchar fuseBits[3]; |
| 416 | uchar *readfuseBits; |
| 417 | unsigned int c; |
| 418 | |
| 419 | if (deviceDef->fusemap > 0x07) |
| 420 | { |
| 421 | fprintf(stderr, "Fuse byte writing not supported on this device.\n"); |
| 422 | return; |
| 423 | } |
| 424 | |
| 425 | if (fuses == 0) |
| 426 | { |
| 427 | fprintf(stderr, "Error: No fuses string given"); |
| 428 | return; |
| 429 | } |
| 430 | |
| 431 | // Convert fuses to hex values (this avoids endianess issues) |
| 432 | c = sscanf(fuses, "%02x%02x%02x", temp+2, temp+1, temp ); |
| 433 | if (c != 3) |
| 434 | { |
| 435 | fprintf(stderr, "Error: Fuses specified are not in hexidecimal"); |
| 436 | return; |
| 437 | } |
| 438 | |
| 439 | fuseBits[0] = (uchar)temp[0]; |
| 440 | fuseBits[1] = (uchar)temp[1]; |
| 441 | fuseBits[2] = (uchar)temp[2]; |
| 442 | |
| 443 | statusOut("\nWriting Fuse Bytes:\n"); |
| 444 | jtagDisplayFuses(fuseBits); |
| 445 | |
| 446 | try |
| 447 | { |
| 448 | jtagWrite(FUSE_SPACE_ADDR_OFFSET + 0, 3, fuseBits); |
| 449 | } |
| 450 | catch (jtag_exception& e) |
| 451 | { |
| 452 | fprintf(stderr, "Error writing fuses: %s\n", |
| 453 | e.what()); |
| 454 | } |
| 455 | |
| 456 | readfuseBits = jtagRead(FUSE_SPACE_ADDR_OFFSET + 0, 3); |
| 457 | |
| 458 | if (memcmp(fuseBits, readfuseBits, 3) != 0) |
| 459 | { |
| 460 | fprintf(stderr, "Error verifying written fuses"); |
| 461 | } |
| 462 | |
| 463 | delete [] readfuseBits; |
| 464 | } |
| 465 | |
| 466 | |
| 467 | static unsigned int countFuses(unsigned int fusemap) |
| 468 | { |
| 469 | unsigned int nfuses = 0; |
| 470 | |
| 471 | for (unsigned int i = 7, mask = 0x80; |
| 472 | mask != 0; |
| 473 | i--, mask >>= 1) |
| 474 | { |
| 475 | if ((fusemap & mask) != 0) |
| 476 | { |
| 477 | nfuses = i + 1; |
| 478 | break; |
| 479 | } |
| 480 | } |
| 481 | if (nfuses == 0) |
| 482 | { |
| 483 | fprintf(stderr, "Device has no fuses? Confused."); |
| 484 | throw jtag_exception(); |
| 485 | } |
| 486 | |
| 487 | return nfuses; |
| 488 | } |
| 489 | |
| 490 | |
| 491 | void jtag::jtagReadFuses(void) |
| 492 | { |
| 493 | uchar *fuseBits = 0; |
| 494 | |
| 495 | statusOut("\nReading Fuse Bytes:\n"); |
| 496 | fuseBits = jtagRead(FUSE_SPACE_ADDR_OFFSET + 0, |
| 497 | countFuses(deviceDef->fusemap)); |
| 498 | |
| 499 | jtagDisplayFuses(fuseBits); |
| 500 | |
| 501 | delete [] fuseBits; |
| 502 | } |
| 503 | |
| 504 | |
| 505 | void jtag::jtagActivateOcdenFuse(void) |
| 506 | { |
| 507 | if (deviceDef->ocden_fuse == 0) |
| 508 | return; // device without an OCDEN fuse |
| 509 | |
| 510 | unsigned int nfuses = countFuses(deviceDef->fusemap); |
| 511 | |
| 512 | if (nfuses > 3) |
| 513 | statusOut("jtag::jtagActivateOcdenFuse(): " |
| 514 | "Device has more than 3 fuses: %d, cannot handle\n", |
| 515 | nfuses); |
| 516 | |
| 517 | uchar *fuseBits = 0; |
| 518 | fuseBits = jtagRead(FUSE_SPACE_ADDR_OFFSET + 0, 3); |
| 519 | |
| 520 | unsigned int fusevect = (fuseBits[2] << 16) | |
| 521 | (fuseBits[1] << 8) | |
| 522 | fuseBits[0]; |
| 523 | |
| 524 | if ((fusevect & deviceDef->ocden_fuse) != 0) |
| 525 | { |
| 526 | statusOut("\nEnabling on-chip debugging:\n"); |
| 527 | |
| 528 | fusevect &= ~deviceDef->ocden_fuse; // clear bit |
| 529 | fuseBits[2] = fusevect >> 16; |
| 530 | fuseBits[1] = fusevect >> 8; |
| 531 | fuseBits[0] = fusevect; |
| 532 | unsigned int offset; |
| 533 | if (deviceDef->ocden_fuse > 0x8000) |
| 534 | offset = 2; |
| 535 | else if (deviceDef->ocden_fuse > 0x80) |
| 536 | offset = 1; |
| 537 | else |
| 538 | offset = 0; |
| 539 | jtagWrite(FUSE_SPACE_ADDR_OFFSET + offset, 1, &fuseBits[offset]); |
| 540 | } |
| 541 | delete [] fuseBits; |
| 542 | } |
| 543 | |
| 544 | void jtag::jtagDisplayFuses(uchar *fuseBits) |
| 545 | { |
| 546 | if (deviceDef->fusemap <= 0x07) |
| 547 | { |
| 548 | // tinyAVR/megaAVR: low/high/[extended] fuse |
| 549 | const char *fusenames[3] = { " Low", |
| 550 | " High", |
| 551 | " Extended" }; |
| 552 | for (unsigned int i = 2, mask = 0x04; |
| 553 | mask != 0; |
| 554 | i--, mask >>= 1) |
| 555 | { |
| 556 | if ((deviceDef->fusemap & mask) != 0) |
| 557 | statusOut("%s Fuse byte -> 0x%02x\n", |
| 558 | fusenames[i], fuseBits[i]); |
| 559 | } |
| 560 | } |
| 561 | else |
| 562 | { |
| 563 | // Xmega: fuse0 ... fuse7 (or just some of them) |
| 564 | for (unsigned int i = 7, mask = 0x80; |
| 565 | mask != 0; |
| 566 | i--, mask >>= 1) |
| 567 | { |
| 568 | if ((deviceDef->fusemap & mask) != 0) |
| 569 | statusOut(" Fuse byte %d -> 0x%02x\n", |
| 570 | i, fuseBits[i]); |
| 571 | } |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | |
| 576 | void jtag::jtagWriteLockBits(char *lock) |
| 577 | { |
| 578 | int temp[1]; |
| 579 | uchar lockBits[1]; |
| 580 | uchar *readlockBits; |
| 581 | unsigned int c; |
| 582 | |
| 583 | if (lock == 0) |
| 584 | { |
| 585 | fprintf(stderr, "Error: No lock bit string given"); |
| 586 | return; |
| 587 | } |
| 588 | |
| 589 | if (strlen(lock) != 2) |
| 590 | { |
| 591 | fprintf(stderr, "Error: Fuses must be one byte exactly"); |
| 592 | return; |
| 593 | } |
| 594 | |
| 595 | // Convert lockbits to hex value |
| 596 | c = sscanf(lock, "%02x", temp); |
| 597 | if (c != 1) |
| 598 | { |
| 599 | fprintf(stderr, "Error: Fuses specified are not in hexidecimal"); |
| 600 | return; |
| 601 | } |
| 602 | |
| 603 | lockBits[0] = (uchar)temp[0]; |
| 604 | |
| 605 | statusOut("\nWriting Lock Bits -> 0x%02x\n", lockBits[0]); |
| 606 | |
| 607 | enableProgramming(); |
| 608 | |
| 609 | try |
| 610 | { |
| 611 | jtagWrite(LOCK_SPACE_ADDR_OFFSET + 0, 1, lockBits); |
| 612 | } |
| 613 | catch (jtag_exception& e) |
| 614 | { |
| 615 | fprintf(stderr, "Error writing lockbits: %s\n", |
| 616 | e.what()); |
| 617 | } |
| 618 | |
| 619 | readlockBits = jtagRead(LOCK_SPACE_ADDR_OFFSET + 0, 1); |
| 620 | |
| 621 | disableProgramming(); |
| 622 | |
| 623 | if (memcmp(lockBits, readlockBits, 1) != 0) |
| 624 | { |
| 625 | fprintf(stderr, "Error verifying written lock bits"); |
| 626 | } |
| 627 | |
| 628 | delete [] readlockBits; |
| 629 | } |
| 630 | |
| 631 | |
| 632 | void jtag::jtagReadLockBits(void) |
| 633 | { |
| 634 | uchar *lockBits = 0; |
| 635 | |
| 636 | enableProgramming(); |
| 637 | statusOut("\nReading Lock Bits:\n"); |
| 638 | lockBits = jtagRead(LOCK_SPACE_ADDR_OFFSET + 0, 1); |
| 639 | disableProgramming(); |
| 640 | |
| 641 | jtagDisplayLockBits(lockBits); |
| 642 | |
| 643 | delete [] lockBits; |
| 644 | } |
| 645 | |
| 646 | |
| 647 | void jtag::jtagDisplayLockBits(uchar *lockBits) |
| 648 | { |
| 649 | statusOut("Lock bits -> 0x%02x\n\n", lockBits[0]); |
| 650 | |
| 651 | statusOut(" Bit 7 [ Reserved ] -> %d\n", (lockBits[0] >> 7) & 1); |
| 652 | statusOut(" Bit 6 [ Reserved ] -> %d\n", (lockBits[0] >> 6) & 1); |
| 653 | statusOut(" Bit 5 [ BLB12 ] -> %d\n", (lockBits[0] >> 5) & 1); |
| 654 | statusOut(" Bit 4 [ BLB11 ] -> %d\n", (lockBits[0] >> 4) & 1); |
| 655 | statusOut(" Bit 3 [ BLB02 ] -> %d\n", (lockBits[0] >> 3) & 1); |
| 656 | statusOut(" Bit 2 [ BLB01 ] -> %d\n", (lockBits[0] >> 2) & 1); |
| 657 | statusOut(" Bit 1 [ LB2 ] -> %d\n", (lockBits[0] >> 1) & 1); |
| 658 | statusOut(" Bit 0 [ LB1 ] -> %d\n", (lockBits[0] >> 0) & 1); |
| 659 | } |
| 660 | |
| 661 | bool jtag::addBreakpoint(unsigned int address, bpType type, unsigned int length) |
| 662 | { |
| 663 | int bp_i; |
| 664 | |
| 665 | debugOut("BP ADD type: %d addr: 0x%x ", type, address); |
| 666 | |
| 667 | |
| 668 | // Perhaps we have already set this breakpoint, and it is just |
| 669 | // marked as disabled In that case we don't need to make a new |
| 670 | // one, just flag this one as enabled again |
| 671 | bp_i = 0; |
| 672 | while (!bp[bp_i].last) |
| 673 | { |
| 674 | if ((bp[bp_i].address == address) && (bp[bp_i].type == type)) |
| 675 | { |
| 676 | bp[bp_i].enabled = true; |
| 677 | debugOut("ENABLED\n"); |
| 678 | break; |
| 679 | } |
| 680 | bp_i++; |
| 681 | } |
| 682 | |
| 683 | // Was what we just did not successful, if so... |
| 684 | if (!bp[bp_i].enabled || (bp[bp_i].address != address) || (bp[bp_i].type == type)) |
| 685 | { |
| 686 | |
| 687 | // Uhh... we are out of space. Try to find a disabled one and just |
| 688 | // write over it. |
| 689 | if ((bp_i + 1) == MAX_TOTAL_BREAKPOINTS2) |
| 690 | { |
| 691 | bp_i = 0; |
| 692 | // We can't remove enabled breakpoints, or ones that |
| 693 | // have JUST been disabled. The just disabled ones |
| 694 | // because they have to sync to the ICE |
| 695 | while (bp[bp_i].enabled || bp[bp_i].toremove) |
| 696 | { |
| 697 | bp_i++; |
| 698 | } |
| 699 | } |
| 700 | |
| 701 | // Sorry.. out of room :( |
| 702 | if ((bp_i + 1) == MAX_TOTAL_BREAKPOINTS2) |
| 703 | { |
| 704 | debugOut("FAILED\n"); |
| 705 | return false; |
| 706 | } |
| 707 | |
| 708 | if (bp[bp_i].last) |
| 709 | { |
| 710 | //See if we need to set the new endpoint |
| 711 | bp[bp_i + 1].last = true; |
| 712 | bp[bp_i + 1].enabled = false; |
| 713 | bp[bp_i + 1].address = 0; |
| 714 | bp[bp_i + 1].type = NONE; |
| 715 | } |
| 716 | |
| 717 | // bp_i now has the new breakpoint we are going to use. |
| 718 | bp[bp_i].last = false; |
| 719 | bp[bp_i].enabled = true; |
| 720 | bp[bp_i].address = address; |
| 721 | bp[bp_i].type = type; |
| 722 | |
| 723 | // Is it a range breakpoint? |
| 724 | // Range breakpoint needs to be aligned, and the length must |
| 725 | // be representable as a bitmask. |
| 726 | if ((length > 1) && ((type == READ_DATA) || |
| 727 | (type == WRITE_DATA) || |
| 728 | (type == ACCESS_DATA))) |
| 729 | { |
| 730 | int bitno = ffs((int)length); |
| 731 | unsigned int mask = 1 << (bitno - 1); |
| 732 | if (mask != length) |
| 733 | { |
| 734 | debugOut("FAILED: length not power of 2 in range BP\n"); |
| 735 | bp[bp_i].last = true; |
| 736 | bp[bp_i].enabled = false; |
| 737 | return false; |
| 738 | } |
| 739 | mask--; |
| 740 | if ((address & mask) != 0) |
| 741 | { |
| 742 | debugOut("FAILED: address in range BP is not base-aligned\n"); |
| 743 | bp[bp_i].last = true; |
| 744 | bp[bp_i].enabled = false; |
| 745 | return false; |
| 746 | } |
| 747 | mask = ~mask; |
| 748 | |
| 749 | // add the breakpoint as a data mask.. only thing is we |
| 750 | // need to find it afterwards |
| 751 | if (!addBreakpoint(mask, DATA_MASK, 1)) |
| 752 | { |
| 753 | debugOut("FAILED\n"); |
| 754 | bp[bp_i].last = true; |
| 755 | bp[bp_i].enabled = false; |
| 756 | bp[bp_i].has_mask = true; |
| 757 | return false; |
| 758 | } |
| 759 | |
| 760 | unsigned int j; |
| 761 | for(j = 0; !bp[j].last; j++) |
| 762 | { |
| 763 | if ((bp[j].type == DATA_MASK) && (bp[bp_i].address == mask)) |
| 764 | break; |
| 765 | } |
| 766 | |
| 767 | bp[bp_i].mask_pointer = j; |
| 768 | |
| 769 | debugOut("range BP ADDED: 0x%x/0x%x\n", address, mask); |
| 770 | } |
| 771 | |
| 772 | } |
| 773 | |
| 774 | // Is this breakpoint new? |
| 775 | if (!bp[bp_i].icestatus) |
| 776 | { |
| 777 | // Yup - flag it as something to download |
| 778 | bp[bp_i].toadd = true; |
| 779 | bp[bp_i].toremove = false; |
| 780 | } |
| 781 | else |
| 782 | { |
| 783 | bp[bp_i].toadd = false; |
| 784 | bp[bp_i].toremove = false; |
| 785 | } |
| 786 | |
| 787 | if (!layoutBreakpoints()) |
| 788 | { |
| 789 | debugOut("Not enough room in ICE for breakpoint. FAILED.\n"); |
| 790 | bp[bp_i].enabled = false; |
| 791 | bp[bp_i].toadd = false; |
| 792 | |
| 793 | if (bp[bp_i].has_mask) |
| 794 | // these BP types have an associated mask |
| 795 | { |
| 796 | bp[bp[bp_i].mask_pointer].enabled = false; |
| 797 | bp[bp[bp_i].mask_pointer].toadd = false; |
| 798 | } |
| 799 | |
| 800 | return false; |
| 801 | } |
| 802 | |
| 803 | return true; |
| 804 | } |
| 805 | |
| 806 | PRAGMA_DIAG_PUSH |
| 807 | PRAGMA_DIAG_IGNORED("-Wunused-parameter") |
| 808 | |
| 809 | bool jtag::deleteBreakpoint(unsigned int address, bpType type, unsigned int length) |
| 810 | { |
| 811 | int bp_i; |
| 812 | |
| 813 | debugOut("BP DEL type: %d addr: 0x%x ", type, address); |
| 814 | |
| 815 | bp_i = 0; |
| 816 | while (!bp[bp_i].last) |
| 817 | { |
| 818 | if ((bp[bp_i].address == address) && (bp[bp_i].type == type)) |
| 819 | { |
| 820 | bp[bp_i].enabled = false; |
| 821 | debugOut("DISABLED\n"); |
| 822 | break; |
| 823 | } |
| 824 | bp_i++; |
| 825 | } |
| 826 | |
| 827 | // If it somehow failed, got to tell.. |
| 828 | if (bp[bp_i].enabled || (bp[bp_i].address != address) || (bp[bp_i].type != type)) |
| 829 | { |
| 830 | debugOut("FAILED\n"); |
| 831 | return false; |
| 832 | } |
| 833 | |
| 834 | // Is this breakpoint actually enabled? |
| 835 | if (bp[bp_i].icestatus) |
| 836 | { |
| 837 | // Yup - flag it as something to delete |
| 838 | bp[bp_i].toadd = false; |
| 839 | bp[bp_i].toremove = true; |
| 840 | } |
| 841 | else |
| 842 | { |
| 843 | bp[bp_i].toadd = false; |
| 844 | bp[bp_i].toremove = false; |
| 845 | } |
| 846 | |
| 847 | return true; |
| 848 | } |
| 849 | |
| 850 | PRAGMA_DIAG_POP |
| 851 | |
| 852 | /* |
| 853 | * This routine is where all the logic of what breakpoints go into the |
| 854 | * ICE and what don't happens. When called it assumes all the "toadd" |
| 855 | * breakpoints don't have valid "bpnum" attached to them. It then |
| 856 | * attempts to fit all the needed breakpoints into the hardware |
| 857 | * breakpoints first. If that won't fly it then adds software |
| 858 | * breakpoints as needed... or just fails. |
| 859 | * |
| 860 | * |
| 861 | * TODO: Some logic to decide which breakpoints change a lot and |
| 862 | * should go in hardware, vs. which breakpoints are more fixed and |
| 863 | * should just be done in code would be handy |
| 864 | */ |
| 865 | bool jtag::layoutBreakpoints(void) |
| 866 | { |
| 867 | // remaining_bps is an array showing which breakpoints are still |
| 868 | // available, starting at 0x00 Note 0x00 is software breakpoint |
| 869 | // and will always be available in theory so just ignore the first |
| 870 | // array element, it's meaningless... FIXME: Slot 4 is set to |
| 871 | // 'false', doesn't seem to work? |
| 872 | bool remaining_bps[MAX_BREAKPOINTS2 + 2] = {false, true, true, true, false, false}; |
| 873 | int bp_i; |
| 874 | uchar bpnum; |
| 875 | bool softwarebps = true; |
| 876 | bool hadroom = true; |
| 877 | |
| 878 | if (deviceDef->device_flags == DEVFL_NO_SOFTBP) |
| 879 | { |
| 880 | softwarebps = false; |
| 881 | } |
| 882 | |
| 883 | // Turn off everything but software breakpoints for DebugWire, |
| 884 | // or for old firmware JTAGICEmkII with Xmega devices |
| 885 | if (softbp_only) |
| 886 | { |
| 887 | int k; |
| 888 | for (k = 1; k < MAX_BREAKPOINTS2 + 1; k++) |
| 889 | { |
| 890 | remaining_bps[k] = false; |
| 891 | } |
| 892 | } |
| 893 | else if (is_xmega) |
| 894 | { |
| 895 | // Xmega has only two hardware slots? |
| 896 | remaining_bps[BREAKPOINT2_XMEGA_UNAVAIL] = false; |
| 897 | } |
| 898 | |
| 899 | bp_i = 0; |
| 900 | while (!bp[bp_i].last) |
| 901 | { |
| 902 | // check we have an enabled "stable" breakpoint that's not |
| 903 | // about to change |
| 904 | if (bp[bp_i].enabled && !bp[bp_i].toremove && bp[bp_i].icestatus) |
| 905 | { |
| 906 | remaining_bps[bp[bp_i].bpnum] = false; |
| 907 | } |
| 908 | |
| 909 | bp_i++; |
| 910 | } |
| 911 | |
| 912 | // Do data watchpoints first |
| 913 | for (bp_i = 0; !bp[bp_i].last; bp_i++) |
| 914 | { |
| 915 | if (bp[bp_i].enabled && bp[bp_i].toadd && |
| 916 | bp[bp_i].type == DATA_MASK) |
| 917 | { |
| 918 | // Check if we have the mask slot available |
| 919 | if (!remaining_bps[BREAKPOINT2_DATA_MASK]) |
| 920 | { |
| 921 | debugOut("Not enough room to store range breakpoint\n"); |
| 922 | bp[bp[bp_i].mask_pointer].enabled = false; |
| 923 | bp[bp[bp_i].mask_pointer].toadd = false; |
| 924 | bp[bp_i].enabled = false; |
| 925 | bp[bp_i].toadd = false; |
| 926 | hadroom = false; |
| 927 | continue; // Skip this breakpoint |
| 928 | } |
| 929 | remaining_bps[BREAKPOINT2_DATA_MASK] = false; |
| 930 | bp[bp_i].bpnum = BREAKPOINT2_DATA_MASK; |
| 931 | continue; |
| 932 | } |
| 933 | |
| 934 | // Find the next data breakpoint that needs somewhere to live |
| 935 | if (bp[bp_i].enabled && bp[bp_i].toadd && |
| 936 | ((bp[bp_i].type == READ_DATA) || |
| 937 | (bp[bp_i].type == WRITE_DATA) || |
| 938 | (bp[bp_i].type == ACCESS_DATA))) |
| 939 | { |
| 940 | // Check if we have one of both slots available |
| 941 | if (!remaining_bps[BREAKPOINT2_DATA_MASK] && |
| 942 | !remaining_bps[BREAKPOINT2_FIRST_DATA]) |
| 943 | { |
| 944 | debugOut("Not enough room to store range breakpoint\n"); |
| 945 | bp[bp_i].enabled = false; |
| 946 | bp[bp_i].toadd = false; |
| 947 | hadroom = false; |
| 948 | continue; // Skip this breakpoint |
| 949 | } |
| 950 | |
| 951 | // Find next available slot |
| 952 | bpnum = BREAKPOINT2_FIRST_DATA; |
| 953 | while (!remaining_bps[bpnum] && (bpnum <= MAX_BREAKPOINTS2)) |
| 954 | { |
| 955 | bpnum++; |
| 956 | } |
| 957 | |
| 958 | if (bpnum > MAX_BREAKPOINTS2) |
| 959 | { |
| 960 | debugOut("No more room for data breakpoints.\n"); |
| 961 | hadroom = false; |
| 962 | break; |
| 963 | } |
| 964 | |
| 965 | bp[bp_i].bpnum = bpnum; |
| 966 | remaining_bps[bpnum] = false; |
| 967 | } |
| 968 | } |
| 969 | |
| 970 | // Do CODE breakpoints now |
| 971 | |
| 972 | bp_i = 0; |
| 973 | while (!bp[bp_i].last) |
| 974 | { |
| 975 | //Find the next spot to live in. |
| 976 | bpnum = 0x00; |
| 977 | while (!remaining_bps[bpnum] && (bpnum <= MAX_BREAKPOINTS2)) |
| 978 | { |
| 979 | //debugOut("Slot %d full\n", bpnum); |
| 980 | bpnum++; |
| 981 | } |
| 982 | |
| 983 | if (bpnum > MAX_BREAKPOINTS2) |
| 984 | { |
| 985 | if (softwarebps) |
| 986 | { |
| 987 | bpnum = 0x00; |
| 988 | } |
| 989 | else |
| 990 | { |
| 991 | bpnum = 0xFF; // flag for NO MORE BREAKPOINTS |
| 992 | } |
| 993 | } |
| 994 | |
| 995 | // Find the next breakpoint that needs somewhere to live |
| 996 | if (bp[bp_i].enabled && bp[bp_i].toadd && (bp[bp_i].type == CODE)) |
| 997 | { |
| 998 | if (bpnum == 0xFF) |
| 999 | { |
| 1000 | debugOut("No more room for code breakpoints.\n"); |
| 1001 | hadroom = false; |
| 1002 | break; |
| 1003 | } |
| 1004 | bp[bp_i].bpnum = bpnum; |
| 1005 | remaining_bps[bpnum] = false; |
| 1006 | } |
| 1007 | |
| 1008 | bp_i++; |
| 1009 | } |
| 1010 | |
| 1011 | return hadroom; |
| 1012 | } |
| 1013 |