Debian Codegraph @

src/jtagprog.cc

raw symbols SCIP
1 /*
2 * avarice - The "avarice" program.
3 * Copyright (C) 2001 Scott Finneran
4 * Copyright (C) 2002 Intel Corporation
5 * Copyright (C) 2005 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 contains functions for interfacing with the JTAG box.
24 *
25 * $Id: jtagprog.cc 384 2020-08-31 22:38:14Z joerg_wunsch $
26 */
27
28
29 #include <stdarg.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33 #include <sys/stat.h>
34 #include <termios.h>
35 #include <fcntl.h>
36 #include <string.h>
37 #include <math.h>
38
39 #if ENABLE_TARGET_PROGRAMMING
40 # include "autoconf.h"
41 # include <bfd.h>
42 #endif
43
44 #include "avarice.h"
45 #include "jtag.h"
46 #include "jtag1.h"
47
48 #if ENABLE_TARGET_PROGRAMMING
49 // The API changed for this in bfd.h. This is a work around.
50 #ifndef bfd_get_section_name
51 # define bfd_get_section_name(bfd, ptr) bfd_section_name(ptr)
52 #endif
53 #ifndef bfd_get_section_size
54 # define bfd_get_section_size bfd_section_size
55 #endif
56
57 static void initImage(BFDimage *image)
58 {
59 unsigned int i;
60 image->last_address = 0;
61 image->first_address = 0;
62 image->first_address_ok = false;
63 image->has_data = false;
64 for (i=0;i<MAX_IMAGE_SIZE;i++)
65 {
66 image->image[i].val = 0x00;
67 image->image[i].used = false;
68 }
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
207 void jtag1::enableProgramming(void)
208 {
209 programmingEnabled = true;
210 if (!doSimpleJtagCommand(0xa3, 1))
211 {
212 fprintf(stderr, "JTAG ICE: Failed to enable programming\n");
213 throw jtag_exception();
214 }
215 }
216
217
218 void jtag1::disableProgramming(void)
219 {
220 programmingEnabled = false;
221 if (!doSimpleJtagCommand(0xa4, 1))
222 {
223 fprintf(stderr, "JTAG ICE: Failed to disable programming\n");
224 throw jtag_exception();
225 }
226 }
227
228
229 // This is really a chip-erase which erases flash, lock-bits and eeprom
230 // (unless the save-eeprom fuse is set).
231 void jtag1::eraseProgramMemory(void)
232 {
233 if (!doSimpleJtagCommand(0xa5, 1))
234 {
235 fprintf(stderr, "JTAG ICE: Failed to erase program memory\n");
236 throw jtag_exception();
237 }
238 }
239
240 void jtag1::eraseProgramPage(unsigned long address)
241 {
242 uchar *response = NULL;
243 uchar command[] = { 0xa1, 0, 0, 0, JTAG_EOM };
244
245 command[1] = address >> 8;
246 command[2] = address;
247
248 response = doJtagCommand(command, sizeof(command), 1);
249 if (response[0] != JTAG_R_OK)
250 {
251 fprintf(stderr, "Page erase failed\n");
252 throw jtag_exception();
253 }
254
255 delete [] response;
256 }
257
258
259 void jtag1::downloadToTarget(const char* filename, bool program, bool verify)
260 {
261 #if ENABLE_TARGET_PROGRAMMING
262 // Basically, we just open the file and copy blocks over to the JTAG
263 // box.
264 struct stat ifstat;
265 const char *target = NULL;
266 const char *default_target = "binary";
267 unsigned int page_size;
268 bool done = 0;
269 bfd *file;
270 asection *p;
271
272 static BFDimage flashimg, eepromimg;
273
274 initImage(&flashimg);
275 initImage(&eepromimg);
276
277 flashimg.name = BFDmemoryTypeString[MEM_FLASH];
278 eepromimg.name = BFDmemoryTypeString[MEM_EEPROM];
279
280 if (stat(filename, &ifstat) < 0)
281 throw jtag_exception("Can't stat() image file");
282
283 // Open the input file.
284 bfd_init();
285
286 // Auto detect file format by a loop iterated at most two times.
287 // 1. Auto-detect file format.
288 // 2. If auto-detect failed, assume binary and iterate once more over
289 // loop.
290 while (! done)
291 {
292 file = bfd_openr(filename, target);
293 if (! file)
294 {
295 fprintf( stderr, "Could not open input file %s:%s\n", filename,
296 bfd_errmsg(bfd_get_error()) );
297 return;
298 }
299
300 // Check if file format is supported. If not, go for binary mode.
301 else if (check_file_format(file))
302 {
303 // File format detection failed. Assuming binary file
304 // BFD section flags are CONTENTS,ALLOC,LOAD,DATA
305 // We must force CODE in stead of DATA
306 fprintf(stderr, "Warning: File format unknown, assuming "
307 "binary.\n");
308 target = default_target;
309 }
310
311 else
312 done = 1;
313 }
314
315
316 // Configure for JTAG download/programming
317
318 // Set the flash page and eeprom page sizes (These are device dependent)
319 page_size = get_page_size(MEM_FLASH);
320
321 debugOut("Flash page size: 0x%0x\nEEPROM page size: 0x%0x\n",
322 page_size, get_page_size(MEM_EEPROM));
323
324 setJtagParameter(JTAG_P_FLASH_PAGESIZE_LOW, page_size & 0xff);
325 setJtagParameter(JTAG_P_FLASH_PAGESIZE_HIGH, page_size >> 8);
326
327 setJtagParameter(JTAG_P_EEPROM_PAGESIZE,
328 get_page_size(MEM_EEPROM));
329
330 // Create RAM image by reading all sections in file
331 p = file->sections;
332 while (p)
333 {
334 jtag_create_image(file, p, &flashimg, MEM_FLASH);
335 jtag_create_image(file, p, &eepromimg, MEM_EEPROM);
336 p = p->next;
337 }
338
339 enableProgramming();
340
341 // Write the complete FLASH/EEPROM images to the device.
342 if (flashimg.has_data)
343 jtag_flash_image(&flashimg, MEM_FLASH, program, verify);
344 if (eepromimg.has_data)
345 jtag_flash_image(&eepromimg, MEM_EEPROM, program, verify);
346
347 disableProgramming();
348
349 (void)(bfd_close(file));
350
351 statusOut("\nDownload complete.\n");
352 #else // !ENABLE_TARGET_PROGRAMMING
353 (void)filename;
354 (void)program;
355 (void)verify;
356 statusOut("\nDownload not done.\n");
357 throw jtag_exception("AVaRICE was not configured for target programming");
358 #endif // ENABLE_TARGET_PROGRAMMING
359 }