[go: up one dir, main page]

Menu

[6491a7]: / src / commands.c  Maximize  Restore  History

Download this file

376 lines (317 with data), 10.6 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*
* dfu-programmer
*
* $Id: commands.c,v 1.6 2006/06/25 00:01:37 schmidtw Exp $
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <stdio.h>
#include <string.h>
#include "config.h"
#include "commands.h"
#include "arguments.h"
#include "intel_hex.h"
#include "atmel.h"
static int execute_erase( struct usb_dev_handle *device,
int interface,
struct programmer_arguments args )
{
int result = 0;
if( 2 < debug ) {
fprintf( stderr, "%s: erase %d bytes\n", __FUNCTION__,
args.memory_size );
}
result = atmel_erase_flash( device, interface, ATMEL_ERASE_ALL );
if( 0 != result )
return result;
return atmel_blank_check( device, interface, 0, args.top_memory_address );
}
static int execute_flash( struct usb_dev_handle *device,
int interface,
struct programmer_arguments args )
{
char *hex_data = NULL;
int usage = 0;
int retval = -1;
int result = 0;
char *buffer = NULL;
int i;
buffer = (char *) malloc( args.memory_size );
if( NULL == buffer ) {
fprintf( stderr, "Request for %d bytes of memory failed.\n",
args.memory_size );
goto error;
}
memset( buffer, 0, args.memory_size );
hex_data = intel_hex_to_buffer( args.com_flash_data.file,
args.memory_size, 0xff, &usage );
if( NULL == hex_data ) {
fprintf( stderr,
"Something went wrong with creating the memory image.\n" );
goto error;
}
if( 2 < debug ) {
fprintf( stderr, "%s: write %d/%d bytes\n", __FUNCTION__,
usage, args.memory_size );
}
result = atmel_flash( device, interface, 0, args.top_memory_address,
hex_data );
if( args.memory_size != result ) {
fprintf( stderr, "Error while flashing. (%d)\n", result );
goto error;
}
if( 0 == args.com_flash_data.suppress_validation ) {
fprintf( stderr, "Validating...\n" );
result = atmel_read_flash( device, interface, 0,
args.top_memory_address, buffer,
args.memory_size );
if( args.memory_size != result ) {
fprintf( stderr, "Error while validating.\n" );
goto error;
}
if( 0 != memcmp(hex_data, buffer, args.memory_size) ) {
fprintf( stderr, "Image did not validate.\n" );
goto error;
}
}
if( 0 == args.quiet ) {
fprintf( stderr, "%d bytes used (%.02f%%)\n", usage,
((float)(usage*100)/(float)(args.top_memory_address)) );
}
retval = 0;
error:
if( NULL != buffer ) {
free( buffer );
buffer = NULL;
}
if( NULL != hex_data ) {
free( hex_data );
hex_data = NULL;
}
return retval;
}
static int execute_start_app( struct usb_dev_handle *device,
int interface,
struct programmer_arguments args )
{
return atmel_start_app( device, interface );
}
static int execute_get( struct usb_dev_handle *device,
int interface,
struct programmer_arguments args )
{
struct atmel_device_info info;
char *message = NULL;
short value = 0;
int status;
int controller_error = 0;
if( device_8051 == args.device_type ) {
status = atmel_read_config_8051( device, interface, &info );
} else {
status = atmel_read_config_avr( device, interface, &info );
}
if( 0 != status ) {
fprintf( stderr, "Error reading %s config information.\n",
args.device_type_string );
return status;
}
switch( args.com_get_data.name ) {
case get_bootloader:
value = info.bootloaderVersion;
message = "Bootloader Version";
break;
case get_ID1:
value = info.bootID1;
message = "Device boot ID 1";
break;
case get_ID2:
value = info.bootID2;
message = "Device boot ID 2";
break;
case get_BSB:
value = info.bsb;
message = "Boot Status Byte";
if( device_8051 != args.device_type ) {
controller_error = 1;
}
break;
case get_SBV:
value = info.sbv;
message = "Software Boot Vector";
if( device_8051 != args.device_type ) {
controller_error = 1;
}
break;
case get_SSB:
value = info.ssb;
message = "Software Security Byte";
if( device_8051 != args.device_type ) {
controller_error = 1;
}
break;
case get_EB:
value = info.eb;
message = "Extra Byte";
if( device_8051 != args.device_type ) {
controller_error = 1;
}
break;
case get_manufacturer:
value = info.manufacturerCode;
message = "Manufacturer Code";
break;
case get_family:
value = info.familyCode;
message = "Family Code";
break;
case get_product_name:
value = info.productName;
message = "Product Name";
break;
case get_product_rev:
value = info.productRevision;
message = "Product Revision";
break;
case get_HSB:
value = info.hsb;
message = "Hardware Security Byte";
break;
}
if( 0 != controller_error ) {
fprintf( stderr, "%s requires 8051 based controller\n",
message );
return -1;
}
if( value < 0 ) {
fprintf( stderr, "The requested device info is unavailable.\n" );
return -2;
}
fprintf( stdout, "%s%s0x%02x (%d)\n",
((0 == args.quiet) ? message : ""),
((0 == args.quiet) ? ": " : ""),
value, value );
return 0;
}
static int execute_dump( struct usb_dev_handle *device,
int interface,
struct programmer_arguments args )
{
int i = 0;
char *buffer = NULL;
buffer = (char *) malloc( (args.memory_size) );
if( NULL == buffer ) {
fprintf( stderr, "Request for %d bytes of memory failed.\n",
args.memory_size );
goto error;
}
if( 2 < debug ) {
fprintf( stderr, "%s: dump %d bytes\n", __FUNCTION__,
args.memory_size );
}
if( args.memory_size != atmel_read_flash(device, interface, 0,
args.top_memory_address, buffer,
args.memory_size) )
{
fprintf( stderr, "Error while validating.\n" );
return -1;
}
for( i = 0; i < args.memory_size; i++ ) {
fprintf( stdout, "%c", buffer[i] );
}
fflush( stdout );
error:
if( NULL != buffer ) {
free( buffer );
buffer = NULL;
}
return 0;
}
static int execute_configure( struct usb_dev_handle *device,
int interface,
struct programmer_arguments args )
{
int value = args.com_configure_data.value;
int name = args.com_configure_data.name;
if( device_8051 != args.device_type ) {
fprintf( stderr, "target doesn't support configure operation.\n" );
return -1;
}
if( (0xff & value) != value ) {
fprintf( stderr, "Value to configure must be in range 0-255.\n" );
return -1;
}
if( 0 != atmel_set_config(device, interface, name, value) )
{
fprintf( stderr, "Configuration set failed.\n" );
return -1;
}
return 0;
}
static int execute_dnload( struct usb_dev_handle *device,
int interface,
struct programmer_arguments args )
{
if( device_SAM7 != args.device_type || device_NEO1973 != args.device_type) {
fprintf( stderr, "target doesn't support dfu download operation.\n" );
return -1;
}
if ( 0 > sam7dfu_do_dnloa(device, interface, 256, /* FIXME */
args.com_flash_data.file) )
{
fprintf( stderr, "Download failed.\n" );
return -1;
}
}
static int execute_upload( struct usb_dev_handle *device,
int interface,
struct programmer_arguments args )
{
if( device_SAM7 != args.device_type || device_NEO1973 != args.device_type) {
fprintf( stderr, "target doesn't support dfu upload operation.\n" );
return -1;
}
if ( 0 > sam7dfu_do_upload(device, interface, 256, /* FIXME */
args.com_flash_data.file) )
{
fprintf( stderr, "Upload failed.\n" );
return -1;
}
}
int execute_command( struct usb_dev_handle *device,
int interface,
struct programmer_arguments args )
{
switch( args.command ) {
case com_erase:
return execute_erase( device, interface, args );
case com_flash:
return execute_flash( device, interface, args );
case com_start_app:
return execute_start_app( device, interface, args );
case com_get:
return execute_get( device, interface, args );
case com_dump:
return execute_dump( device, interface, args );
case com_configure:
return execute_configure( device, interface, args );
case com_dnload:
return execute_dnload( device, interface, args );
case com_upload:
return execute_upload( device, interface, args );
default:
fprintf( stderr, "Not supported at this time.\n" );
}
return -1;
}