| 1 | /* |
| 2 | * avarice - The "avarice" program. |
| 3 | * Copyright (C) 2001 Scott Finneran |
| 4 | * Copyright (C) 2002 Intel Corporation |
| 5 | * Copyright (C) 2005,2006 Joerg Wunsch |
| 6 | * |
| 7 | * File format support using BFD contributed and copyright 2003 |
| 8 | * Nils Kr. Strom |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License Version 2 |
| 12 | * as published by the Free Software Foundation. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA. |
| 22 | * |
| 23 | * This file implements the target memory programming interface for |
| 24 | * the mkII protocol. |
| 25 | * |
| 26 | * $Id: jtag2prog.cc 384 2020-08-31 22:38:14Z joerg_wunsch $ |
| 27 | */ |
| 28 | |
| 29 | |
| 30 | #include <stdarg.h> |
| 31 | #include <stdio.h> |
| 32 | #include <stdlib.h> |
| 33 | #include <unistd.h> |
| 34 | #include <sys/stat.h> |
| 35 | #include <termios.h> |
| 36 | #include <fcntl.h> |
| 37 | #include <string.h> |
| 38 | #include <math.h> |
| 39 | |
| 40 | #if ENABLE_TARGET_PROGRAMMING |
| 41 | # include "autoconf.h" |
| 42 | # include <bfd.h> |
| 43 | #endif |
| 44 | |
| 45 | #include "avarice.h" |
| 46 | #include "jtag.h" |
| 47 | #include "jtag2.h" |
| 48 | |
| 49 | #if ENABLE_TARGET_PROGRAMMING |
| 50 | // The API changed for this in bfd.h. This is a work around. |
| 51 | #ifndef bfd_get_section_name |
| 52 | # define bfd_get_section_name(bfd, ptr) bfd_section_name(ptr) |
| 53 | #endif |
| 54 | #ifndef bfd_get_section_size |
| 55 | # define bfd_get_section_size bfd_section_size |
| 56 | #endif |
| 57 | |
| 58 | static void initImage(BFDimage *image) |
| 59 | { |
| 60 | unsigned int i; |
| 61 | image->last_address = 0; |
| 62 | image->first_address = 0; |
| 63 | image->first_address_ok = false; |
| 64 | image->has_data = false; |
| 65 | for (i=0;i<MAX_IMAGE_SIZE;i++) |
| 66 | { |
| 67 | image->image[i].val = 0x00; |
| 68 | image->image[i].used = false; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | // Check if file format is supported. |
| 73 | // return nonzero on errors. |
| 74 | static int check_file_format(bfd *file) |
| 75 | { |
| 76 | char **matching; |
| 77 | int err = 1; |
| 78 | |
| 79 | // Check if archive, not plain file. |
| 80 | if (bfd_check_format(file, bfd_archive) == true) |
| 81 | { |
| 82 | fprintf(stderr, "Input file is archive\n"); |
| 83 | } |
| 84 | |
| 85 | else if (bfd_check_format_matches (file, bfd_object, &matching)) |
| 86 | err = 0; |
| 87 | |
| 88 | else if (bfd_get_error () == bfd_error_file_ambiguously_recognized) |
| 89 | { |
| 90 | fprintf(stderr, "File format ambiguous: %s\n", |
| 91 | bfd_errmsg(bfd_get_error())); |
| 92 | } |
| 93 | |
| 94 | else if (bfd_get_error () != bfd_error_file_not_recognized) |
| 95 | { |
| 96 | fprintf(stderr, "File format not supported: %s\n", |
| 97 | bfd_errmsg(bfd_get_error())); |
| 98 | } |
| 99 | |
| 100 | else if (bfd_check_format_matches (file, bfd_core, &matching)) |
| 101 | err = 0; |
| 102 | |
| 103 | return err; |
| 104 | } |
| 105 | |
| 106 | |
| 107 | // Get address of section. |
| 108 | // We have two different scenarios (both with same result). |
| 109 | // 1. vma == lma : Normal section |
| 110 | // Return real address (mask gcc-hacked MSB's away). |
| 111 | // |
| 112 | // 2. vma != lma : For sections to be relocated (e.g. .data) |
| 113 | // lma is the address where the duplicate initialized data is stored. |
| 114 | // vma is the destination address after relocation. |
| 115 | // Return real address (mask gcc-hacked MSB's away). |
| 116 | // |
| 117 | // 3. Not correct memory type: return 0x800000. |
| 118 | // |
| 119 | static unsigned int get_section_addr(asection *section, BFDmemoryType memtype) |
| 120 | { |
| 121 | BFDmemoryType sectmemtype; |
| 122 | |
| 123 | if ((section->flags & SEC_HAS_CONTENTS) && |
| 124 | ((section->flags & SEC_ALLOC) || (section->flags & SEC_LOAD))) |
| 125 | { |
| 126 | if (section->lma < DATA_SPACE_ADDR_OFFSET) // < 0x80... |
| 127 | sectmemtype = MEM_FLASH; |
| 128 | else if (section->lma < EEPROM_SPACE_ADDR_OFFSET) // < 0x81... |
| 129 | sectmemtype = MEM_RAM; |
| 130 | else if (section->lma < FUSE_SPACE_ADDR_OFFSET) // < 0x82... |
| 131 | sectmemtype = MEM_EEPROM; |
| 132 | else // e.g. .fuses |
| 133 | return 0xffffff; |
| 134 | |
| 135 | if (memtype == sectmemtype) { |
| 136 | if (sectmemtype == MEM_FLASH) { |
| 137 | /* Don't mask the lma or you will not be able to handle more |
| 138 | than 64K of flash. */ |
| 139 | return (section->lma); |
| 140 | } |
| 141 | return (section->lma &~ ADDR_SPACE_MASK); |
| 142 | } |
| 143 | else |
| 144 | return 0xffffff; |
| 145 | } |
| 146 | else |
| 147 | return 0xffffff; |
| 148 | } |
| 149 | |
| 150 | |
| 151 | |
| 152 | // Add section of memtype BFDmemoryType to image. |
| 153 | static void jtag_create_image(bfd *file, asection *section, |
| 154 | BFDimage *image, |
| 155 | BFDmemoryType memtype) |
| 156 | { |
| 157 | const char *name; |
| 158 | unsigned int addr; |
| 159 | unsigned int size; |
| 160 | static uchar buf[MAX_IMAGE_SIZE]; |
| 161 | unsigned int i; |
| 162 | |
| 163 | // If section is empty (although unexpected) return |
| 164 | if (! section) |
| 165 | return; |
| 166 | |
| 167 | // Get information about section |
| 168 | name = bfd_get_section_name(file, section); |
| 169 | size = bfd_get_section_size(section); |
| 170 | |
| 171 | if ((addr = get_section_addr(section, memtype)) != 0xffffff) |
| 172 | { |
| 173 | debugOut("Getting section contents, addr=0x%lx size=0x%lx\n", |
| 174 | addr, size); |
| 175 | |
| 176 | // Read entire section into buffer, at correct byte address. |
| 177 | bfd_get_section_contents(file, section, buf, 0, size); |
| 178 | |
| 179 | // Copy section into memory struct. Mark as used. |
| 180 | for (i=0; i<size; i++) |
| 181 | { |
| 182 | unsigned int c = i+addr; |
| 183 | image->image[c].val = buf[i]; |
| 184 | image->image[c].used = true; |
| 185 | } |
| 186 | |
| 187 | // Remember last address in image |
| 188 | if (addr+size > image->last_address) |
| 189 | image->last_address = addr+size; |
| 190 | |
| 191 | // Remember first address in image |
| 192 | if ((! image->first_address_ok) || (addr < image->first_address)) |
| 193 | { |
| 194 | image->first_address = addr; |
| 195 | image->first_address_ok = true; |
| 196 | } |
| 197 | debugOut("%s Image create: Adding %s at addr 0x%lx size %d (0x%lx)\n", |
| 198 | BFDmemoryTypeString[memtype], name, addr, size, size); |
| 199 | |
| 200 | // Indicate image has data |
| 201 | image->has_data = true; |
| 202 | } |
| 203 | } |
| 204 | #endif // ENABLE_TARGET_PROGRAMMING |
| 205 | |
| 206 | void jtag2::enableProgramming(void) |
| 207 | { |
| 208 | if (proto != PROTO_DW) |
| 209 | { |
| 210 | programmingEnabled = true; |
| 211 | doSimpleJtagCommand(CMND_ENTER_PROGMODE); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | |
| 216 | void jtag2::disableProgramming(void) |
| 217 | { |
| 218 | if (proto != PROTO_DW) |
| 219 | { |
| 220 | programmingEnabled = false; |
| 221 | doSimpleJtagCommand(CMND_LEAVE_PROGMODE); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | |
| 226 | // This is really a chip-erase which erases flash, lock-bits and eeprom |
| 227 | // (unless the save-eeprom fuse is set). |
| 228 | void jtag2::eraseProgramMemory(void) |
| 229 | { |
| 230 | if (proto == PROTO_DW) |
| 231 | // debugWIRE auto-erases when programming |
| 232 | return; |
| 233 | |
| 234 | if (is_xmega) |
| 235 | { |
| 236 | uchar *response; |
| 237 | int respSize; |
| 238 | uchar command[6] = { CMND_XMEGA_ERASE }; |
| 239 | |
| 240 | // ERASE_MODE (erase chip) |
| 241 | command[1] = 0x00; |
| 242 | |
| 243 | // ADDRESS |
| 244 | command[2] = 0x00; |
| 245 | command[3] = 0x00; |
| 246 | command[4] = 0x00; |
| 247 | command[5] = 0x00; |
| 248 | |
| 249 | try |
| 250 | { |
| 251 | doJtagCommand(command, sizeof(command), |
| 252 | response, respSize); |
| 253 | } |
| 254 | catch (jtag_exception& e) |
| 255 | { |
| 256 | fprintf(stderr, "Failed to erase Xmega program memory: %s\n", |
| 257 | e.what()); |
| 258 | throw; |
| 259 | } |
| 260 | delete [] response; |
| 261 | } |
| 262 | else |
| 263 | { |
| 264 | doSimpleJtagCommand(CMND_CHIP_ERASE); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | void jtag2::eraseProgramPage(unsigned long address) |
| 269 | { |
| 270 | uchar *response; |
| 271 | int respSize; |
| 272 | uchar command[5] = { CMND_ERASEPAGE_SPM }; |
| 273 | |
| 274 | command[1] = (address & 0xff000000) >> 24; |
| 275 | command[2] = (address & 0xff0000) >> 16; |
| 276 | command[3] = (address & 0xff00) >> 8; |
| 277 | command[4] = address; |
| 278 | |
| 279 | try |
| 280 | { |
| 281 | doJtagCommand(command, sizeof(command), |
| 282 | response, respSize); |
| 283 | } |
| 284 | catch (jtag_exception& e) |
| 285 | { |
| 286 | fprintf(stderr, "Page erase failed: %s\n", |
| 287 | e.what()); |
| 288 | throw; |
| 289 | } |
| 290 | |
| 291 | delete [] response; |
| 292 | } |
| 293 | |
| 294 | |
| 295 | void jtag2::downloadToTarget(const char* filename, bool program, bool verify) |
| 296 | { |
| 297 | #if ENABLE_TARGET_PROGRAMMING |
| 298 | // Basically, we just open the file and copy blocks over to the JTAG |
| 299 | // box. |
| 300 | struct stat ifstat; |
| 301 | const char *target = NULL; |
| 302 | const char *default_target = "binary"; |
| 303 | unsigned int page_size; |
| 304 | bool done = 0; |
| 305 | bfd *file; |
| 306 | asection *p; |
| 307 | |
| 308 | static BFDimage flashimg, eepromimg; |
| 309 | |
| 310 | initImage(&flashimg); |
| 311 | initImage(&eepromimg); |
| 312 | |
| 313 | flashimg.name = BFDmemoryTypeString[MEM_FLASH]; |
| 314 | eepromimg.name = BFDmemoryTypeString[MEM_EEPROM]; |
| 315 | |
| 316 | if (stat(filename, &ifstat) < 0) |
| 317 | throw jtag_exception("Can't stat() image file"); |
| 318 | |
| 319 | // Open the input file. |
| 320 | bfd_init(); |
| 321 | |
| 322 | // Auto detect file format by a loop iterated at most two times. |
| 323 | // 1. Auto-detect file format. |
| 324 | // 2. If auto-detect failed, assume binary and iterate once more over |
| 325 | // loop. |
| 326 | while (! done) |
| 327 | { |
| 328 | file = bfd_openr(filename, target); |
| 329 | if (! file) |
| 330 | { |
| 331 | fprintf( stderr, "Could not open input file %s:%s\n", filename, |
| 332 | bfd_errmsg(bfd_get_error()) ); |
| 333 | exit(-1); |
| 334 | } |
| 335 | |
| 336 | // Check if file format is supported. If not, go for binary mode. |
| 337 | else if (check_file_format(file)) |
| 338 | { |
| 339 | // File format detection failed. Assuming binary file |
| 340 | // BFD section flags are CONTENTS,ALLOC,LOAD,DATA |
| 341 | // We must force CODE in stead of DATA |
| 342 | fprintf(stderr, "Warning: File format unknown, assuming " |
| 343 | "binary.\n"); |
| 344 | target = default_target; |
| 345 | } |
| 346 | |
| 347 | else |
| 348 | done = 1; |
| 349 | } |
| 350 | |
| 351 | |
| 352 | // Configure for JTAG download/programming |
| 353 | |
| 354 | // Set the flash page and eeprom page sizes (These are device dependent) |
| 355 | page_size = get_page_size(MEM_FLASH); |
| 356 | |
| 357 | debugOut("Flash page size: 0x%0x\nEEPROM page size: 0x%0x\n", |
| 358 | page_size, get_page_size(MEM_EEPROM)); |
| 359 | |
| 360 | #if notneeded // already addressed by setting the device descriptor |
| 361 | setJtagParameter(JTAG_P_FLASH_PAGESIZE_LOW, page_size & 0xff); |
| 362 | setJtagParameter(JTAG_P_FLASH_PAGESIZE_HIGH, page_size >> 8); |
| 363 | |
| 364 | setJtagParameter(JTAG_P_EEPROM_PAGESIZE, |
| 365 | get_page_size(MEM_EEPROM)); |
| 366 | #endif |
| 367 | |
| 368 | // Create RAM image by reading all sections in file |
| 369 | p = file->sections; |
| 370 | while (p) |
| 371 | { |
| 372 | jtag_create_image(file, p, &flashimg, MEM_FLASH); |
| 373 | jtag_create_image(file, p, &eepromimg, MEM_EEPROM); |
| 374 | p = p->next; |
| 375 | } |
| 376 | |
| 377 | enableProgramming(); |
| 378 | |
| 379 | // Write the complete FLASH/EEPROM images to the device. |
| 380 | if (flashimg.has_data) |
| 381 | jtag_flash_image(&flashimg, MEM_FLASH, program, verify); |
| 382 | if (eepromimg.has_data) |
| 383 | jtag_flash_image(&eepromimg, MEM_EEPROM, program, verify); |
| 384 | |
| 385 | disableProgramming(); |
| 386 | |
| 387 | (void)(bfd_close(file)); |
| 388 | |
| 389 | statusOut("\nDownload complete.\n"); |
| 390 | #else // !ENABLE_TARGET_PROGRAMMING |
| 391 | (void)filename; |
| 392 | (void)program; |
| 393 | (void)verify; |
| 394 | statusOut("\nDownload not done.\n"); |
| 395 | throw jtag_exception("AVaRICE was not configured for target programming"); |
| 396 | #endif // ENABLE_TARGET_PROGRAMMING |
| 397 | } |