Debian Codegraph @

src/commands.c

raw
1 /*
2 * dfu-programmer
3 *
4 * $Id$
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 */
20
21 #include <stdio.h>
22 #include <stdint.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <stdbool.h>
26
27 #include "config.h"
28 #include "commands.h"
29 #include "arguments.h"
30 #include "intel_hex.h"
31 #include "stm32.h"
32 #include "atmel.h"
33 #include "util.h"
34
35 #define COMMAND_DEBUG_THRESHOLD 40
36
37 #define DEBUG(...) dfu_debug( __FILE__, __FUNCTION__, __LINE__, \
38 COMMAND_DEBUG_THRESHOLD, __VA_ARGS__ )
39
40 // ________ P R O T O T Y P E S _______________________________
41 static int32_t execute_validate( dfu_device_t *device,
42 intel_buffer_out_t *bout,
43 uint8_t mem_segment,
44 bool quiet,
45 bool ignore_outside);
46 /* provide an out buffer to validate and whether this is from
47 * flash or eeprom data sections, also wether you want it quiet
48 */
49
50 // ________ F U N C T I O N S _______________________________
51 static void security_check( dfu_device_t *device ) {
52 if( ADC_AVR32 == device->type ) {
53 // Get security bit state for AVR32.
54 device->security_bit_state = atmel_getsecure( device );
55 DEBUG( "Security bit check returned %d.\n", device->security_bit_state );
56 } else {
57 // Security bit not present or not testable.
58 device->security_bit_state = ATMEL_SECURE_OFF;
59 }
60 }
61
62 static void security_message( dfu_device_t *device ) {
63 if( device->security_bit_state > ATMEL_SECURE_OFF ) {
64 fprintf( stderr, "The security bit %s set.\n"
65 "Erase the device to clear temporarily.\n",
66 (ATMEL_SECURE_ON == device->security_bit_state) ? "is" : "may be" );
67 }
68 }
69
70 static int32_t execute_erase( dfu_device_t *device,
71 struct programmer_arguments *args ) {
72 int32_t result = SUCCESS;
73
74 if( !(GRP_STM32 & args->device_type) && !args->com_erase_data.force ) {
75 if( 0 == atmel_blank_check( device, args->flash_address_bottom,
76 args->flash_address_top,
77 args->quiet ) ) {
78 if ( !args->quiet ) {
79 fprintf( stderr, "Chip already blank, to force erase use --force.\n");
80 }
81 return SUCCESS;
82 }
83 }
84
85 DEBUG( "erase 0x%X bytes.\n",
86 (args->flash_address_top - args->flash_address_bottom) );
87
88 if( GRP_STM32 & args->device_type ) {
89 result = stm32_erase_flash( device, args->quiet );
90 } else {
91 result = atmel_erase_flash( device, ATMEL_ERASE_ALL, args->quiet );
92 }
93
94 if( 0 != result ) {
95 return result;
96 }
97
98 if( !(GRP_STM32 & args->device_type) &&
99 !args->com_erase_data.suppress_validation ) {
100 result = atmel_blank_check( device, args->flash_address_bottom,
101 args->flash_address_top,
102 args->quiet );
103 }
104 return result;
105 }
106
107 static int32_t execute_setsecure( dfu_device_t *device,
108 struct programmer_arguments *args ) {
109 int32_t result;
110
111 if( ADC_AVR32 != args->device_type ) {
112 DEBUG( "target doesn't support security bit set.\n" );
113 fprintf( stderr, "Operation not supported on %s\n",
114 args->device_type_string );
115 return ARGUMENT_ERROR;
116 }
117
118 result = atmel_secure( device );
119
120 if( result < 0 ) {
121 DEBUG( "Error while setting security bit. (%d)\n", result );
122 fprintf( stderr, "Error setting security bit.\n" );
123 return UNSPECIFIED_ERROR;
124 }
125
126 return SUCCESS;
127 }
128
129 // TODO : split this into a new command (no file is needed) - also general
130 // format of this program is that only 1 command is run at a time.. caveat is
131 // that if program sets a section in memory to '\0' and serialize sets it
132 // otherwise, the section will end up '\0' unless a page erase is used.. so may
133 // need to keep this part of the flash command, but specify that serialize data
134 // 'wins' over data from the hex file
135 static int32_t serialize_memory_image( intel_buffer_out_t *bout,
136 struct programmer_arguments *args ) {
137 uint32_t target_offset = 0;
138 if( args->command == com_user )
139 target_offset = ATMEL_USER_PAGE_OFFSET;
140 else if( args->device_type & GRP_STM32 )
141 target_offset = STM32_FLASH_OFFSET;
142
143 if ( NULL != args->com_flash_data.serial_data ) {
144 int16_t *serial_data = args->com_flash_data.serial_data;
145 uint32_t length = args->com_flash_data.serial_length;
146 uint32_t offset = args->com_flash_data.serial_offset;
147 uint32_t i;
148
149 for( i=0; i < length; ++i ) {
150 if ( 0 != intel_process_data(bout, serial_data[i],
151 target_offset, offset + i) ) {
152 return BUFFER_INIT_ERROR;
153 }
154 }
155 }
156 return SUCCESS;
157 }
158
159 static int32_t execute_validate( dfu_device_t *device,
160 intel_buffer_out_t *bout,
161 uint8_t mem_segment,
162 const bool quiet,
163 const bool ignore_outside) {
164 int32_t retval = UNSPECIFIED_ERROR;
165 int32_t result; // result of fcn calls
166 intel_buffer_in_t buin; // buffer in for storing read mem
167
168 if( 0 != intel_init_buffer_in(&buin, bout->info.total_size,
169 bout->info.page_size ) ) {
170 DEBUG("ERROR initializing a buffer.\n");
171 retval = BUFFER_INIT_ERROR;
172 goto error;
173 }
174 buin.info.data_start = bout->info.valid_start;
175 buin.info.data_end = bout->info.valid_end;
176
177 if( device->type & GRP_STM32 ) {
178 result = stm32_read_flash( device, &buin, mem_segment, quiet );
179 } else {
180 result = atmel_read_flash( device, &buin, mem_segment, quiet );
181 }
182
183 if( 0 != result ) {
184 DEBUG("ERROR: could not read memory, err %d.\n", result);
185 retval = FLASH_READ_ERROR;
186 goto error;
187 }
188
189 if( 0 != (result = intel_validate_buffer( &buin, bout, quiet )) ) {
190 if( result < 0 ) {
191 retval = VALIDATION_ERROR_IN_REGION;
192 } else {
193 if (ignore_outside) {
194 retval = SUCCESS;
195 } else {
196 retval = VALIDATION_ERROR_OUTSIDE_REGION;
197 }
198 }
199 goto error;
200 }
201
202 retval = SUCCESS;
203
204 error:
205 if( !quiet && SUCCESS != retval ) fprintf( stderr, "FAIL\n" );
206
207 if( NULL != buin.data ) {
208 free( buin.data );
209 buin.data = NULL;
210 }
211
212 return retval;
213 }
214
215 static void print_flash_usage( intel_buffer_info_t *info ) {
216 fprintf( stderr,
217 "0x%X bytes written into 0x%X bytes memory (%.02f%%).\n",
218 info->data_end - info->data_start + 1,
219 info->valid_end - info->valid_start + 1,
220 ((float) (100 * (info->data_end - info->data_start + 1)) /
221 (float) (info->valid_end - info->valid_start + 1)) ) ;
222 }
223
224 static int32_t execute_flash( dfu_device_t *device,
225 struct programmer_arguments *args ) {
226 int32_t retval = UNSPECIFIED_ERROR;
227 int32_t result;
228 uint32_t i;
229 intel_buffer_out_t bout;
230 size_t memory_size;
231 size_t page_size;
232 enum atmel_memory_unit_enum mem_type = args->com_flash_data.segment;
233 uint32_t target_offset = 0;
234
235 /* assign the correct memory size */
236 switch ( mem_type ) {
237 case mem_flash:
238 if( args->device_type & GRP_STM32 ) {
239 target_offset = STM32_FLASH_OFFSET;
240 }
241 memory_size = args->memory_address_top + 1;
242 page_size = args->flash_page_size;
243 break;
244 case mem_eeprom:
245 if( 0 == args->eeprom_memory_size ) {
246 fprintf( stderr, "This device has no eeprom.\n" );
247 return ARGUMENT_ERROR;
248 }
249 memory_size = args->eeprom_memory_size;
250 page_size = args->eeprom_page_size;
251 break;
252 case mem_user:
253 memory_size = args->flash_page_size;
254 page_size = args->flash_page_size;
255 mem_type = mem_user;
256 target_offset = ATMEL_USER_PAGE_OFFSET;
257 if( args->device_type != ADC_AVR32 ){
258 fprintf(stderr, "Flash User only implemented for ADC_AVR32 devices.\n");
259 retval = ARGUMENT_ERROR;
260 goto error;
261 }
262 break;
263 default:
264 DEBUG("Unknown memory type %d\n", mem_type);
265 return ARGUMENT_ERROR;
266 }
267
268 // ----------------- CONVERT HEX FILE TO BINARY -------------------------
269 if( 0 != intel_init_buffer_out(&bout, memory_size, page_size) ) {
270 DEBUG("ERROR initializing a buffer.\n");
271 retval = BUFFER_INIT_ERROR;
272 goto error;
273 }
274
275 result = intel_hex_to_buffer( args->com_flash_data.file, &bout,
276 target_offset, args->quiet );
277
278 if ( result < 0 ) {
279 DEBUG( "Something went wrong with creating the memory image.\n" );
280 retval = BUFFER_INIT_ERROR;
281 goto error;
282 } else if ( result > 0 ) {
283 DEBUG( "WARNING: File contains 0x%X bytes outside target memory.\n",
284 result );
285 if( mem_type == mem_flash ) {
286 DEBUG( "There may be data in the user page (offset %#X).\n",
287 ATMEL_USER_PAGE_OFFSET );
288 DEBUG( "Inspect the hex file or try flash-user.\n" );
289 }
290 if( !args->quiet ) {
291 fprintf( stderr,
292 "WARNING: 0x%X bytes are outside target memory,\n", result );
293 fprintf( stderr, " and will not be written.\n" );
294 }
295 }
296 // TODO : consider accepting a string to flash to the user page as well as a hex
297 // file.. this would be easier than using serialize and could return the address
298 // location of the start of the string (to be used in the program file)
299
300 if (0 != serialize_memory_image( &bout, args )) {
301 retval = BUFFER_INIT_ERROR;
302 goto error;
303 }
304
305 if( mem_type == mem_flash ) {
306 bout.info.valid_start = args->flash_address_bottom;
307 bout.info.valid_end = args->flash_address_top;
308
309 // check that there isn't anything overlapping the bootloader
310 for( i = args->bootloader_bottom; i <= args->bootloader_top; i++) {
311 if( bout.data[i] <= UINT8_MAX ) {
312 if( true == args->suppressBootloader ) {
313 //If we're ignoring the bootloader, don't write to it
314 bout.data[i] = UINT16_MAX;
315 } else {
316 fprintf( stderr, "Bootloader and code overlap.\n" );
317 fprintf( stderr, "Use --suppress-bootloader-mem to ignore\n" );
318 retval = BUFFER_INIT_ERROR;
319 goto error;
320 }
321 }
322 }
323 } else if ( mem_type == mem_user ) {
324 // check here about overwriting?
325
326 if ( bout.info.data_start == UINT32_MAX ) {
327 fprintf( stderr,
328 "ERROR: No data to write into the user page.\n" );
329 retval = BUFFER_INIT_ERROR;
330 goto error;
331 } else {
332 DEBUG("Hex file contains %u bytes to write.\n",
333 bout.info.data_end - bout.info.data_start + 1 );
334 }
335
336 if ( !(args->com_flash_data.force) ) {
337 /* depending on the version of the bootloader, there could be
338 * configuration values in the last word or last two words of the
339 * user page. If these are overwritten the device may not start.
340 * A warning should be issued before these values can be changed. */
341 fprintf( stderr,
342 "ERROR: --force flag is required to write user page.\n" );
343 fprintf( stderr,
344 " Last word(s) in user page contain configuration data.\n");
345 fprintf( stderr,
346 " The user page is erased whenever any data is written.\n");
347 fprintf( stderr,
348 " Without valid config. device always resets in bootloader.\n");
349 fprintf( stderr,
350 " Use dump-user to obtain valid configuration words.\n");
351 retval = ARGUMENT_ERROR;
352 goto error;
353 // TODO : implement so this error only appears when data overlaps the
354 // bootloader configuration words. This would require reading the user
355 // page to add that data to the buffer, and also should include
356 // checking the bootloader version to make sure the right number of
357 // words are blocked / written.
358 // ----------- the below for loop is not currently in use -----------
359 for ( i = bout.info.total_size - 8; i < bout.info.total_size; i++ ) {
360 if ( -1 != bout.data[i] ) {
361 fprintf( stderr,
362 "ERROR: data overlap with bootloader configuration word(s).\n" );
363 DEBUG( "At position %d, value is %d.\n", i, bout.data[i] );
364 fprintf( stderr,
365 "ERROR: use the --force-config flag to write the data.\n" );
366 retval = ARGUMENT_ERROR;
367 goto error;
368 }
369 }
370 }
371 }
372
373 // ------------------ INITIAL VALIDATE (if required) -------------------
374 if ( 1 == args->com_flash_data.validate_first ) {
375 if( 0 == ( retval = execute_validate(device, &bout, mem_type, args->quiet,
376 args->com_flash_data.ignore_outside)) ) {
377 goto success;
378 }
379 // Else, continue to flash
380 }
381
382 // ------------------ WRITE PROGRAM DATA -------------------------------
383 if( mem_type == mem_user ) {
384 result = atmel_user( device, &bout );
385 } else {
386 if ( 1 == args->com_flash_data.erase_first ) {
387 if( args->device_type & GRP_STM32 ) {
388 result = stm32_erase_flash( device, args->quiet );
389 } else {
390 result = atmel_erase_flash( device, ATMEL_ERASE_ALL, args->quiet );
391 }
392
393 if( 0 != result ) {
394 DEBUG( "Error erasing flash. (err %d)\n", result );
395 return result;
396 }
397 }
398
399 if( args->device_type & GRP_STM32 ) {
400 result = stm32_write_flash( device, &bout,
401 mem_type == mem_eeprom ? true : false,
402 args->com_flash_data.force, args->quiet );
403 } else {
404 result = atmel_flash(device, &bout,
405 mem_type == mem_eeprom ? true : false,
406 args->com_flash_data.force, args->quiet);
407 }
408 }
409 if( 0 != result ) {
410 DEBUG( "Error writing %s data. (err %d)\n", "memory", result );
411 retval = FLASH_WRITE_ERROR;
412 goto error;
413 }
414
415 // ------------------ VALIDATE PROGRAM ------------------------------
416 if( 0 == args->com_flash_data.suppress_validation ) {
417 if( 0 != ( retval = execute_validate(device, &bout, mem_type, args->quiet,
418 args->com_flash_data.ignore_outside)) ) {
419 fprintf( stderr, "Memory did not validate. Did you erase?\n" );
420 goto error;
421 } else if ( 0 == args->quiet ) {
422 print_flash_usage( &bout.info );
423 }
424 } else if( 0 == args->quiet ) {
425 print_flash_usage( &bout.info );
426 }
427
428 success:
429 retval = SUCCESS;
430
431 error:
432 if( NULL != bout.data ) {
433 free( bout.data );
434 bout.data = NULL;
435 }
436
437 return retval;
438 }
439
440 static int32_t execute_getfuse( dfu_device_t *device,
441 struct programmer_arguments *args ) {
442 atmel_avr32_fuses_t info;
443 char *message = NULL;
444 int32_t value = 0;
445 int32_t status;
446
447 /* only ADC_AVR32 seems to support fuse operation */
448 if( !(ADC_AVR32 & args->device_type) ) {
449 DEBUG( "target doesn't support fuse set operation.\n" );
450 fprintf( stderr, "target doesn't support fuse set operation.\n" );
451 return ARGUMENT_ERROR;
452 }
453
454 /* Check AVR32 security bit in order to provide a better error message. */
455 security_check( device );
456
457 if( args->device_type & GRP_STM32 ) {
458 fprintf( stderr, "Operation not supported on %s.\n",
459 args->device_type_string );
460 return ARGUMENT_ERROR;
461 } else {
462 status = atmel_read_fuses( device, &info );
463 }
464
465 if( 0 != status ) {
466 DEBUG( "Error reading %s config information.\n",
467 args->device_type_string );
468 fprintf( stderr, "Error reading %s config information.\n",
469 args->device_type_string );
470 security_message( device );
471 return status;
472 }
473
474 switch( args->com_getfuse_data.name ) {
475 case get_lock:
476 value = info.lock;
477 message = "Locked regions";
478 break;
479 case get_epfl:
480 value = info.epfl;
481 message = "External Privileged Fetch Lock";
482 break;
483 case get_bootprot:
484 value = info.bootprot;
485 message = "Bootloader protected area";
486 break;
487 case get_bodlevel:
488 value = info.bodlevel;
489 message = "Brown-out detector trigger level";
490 break;
491 case get_bodhyst:
492 value = info.bodhyst;
493 message = "BOD Hysteresis enable";
494 break;
495 case get_boden:
496 value = info.boden;
497 message = "BOD Enable";
498 break;
499 case get_isp_bod_en:
500 value = info.isp_bod_en;
501 message = "ISP BOD enable";
502 break;
503 case get_isp_io_cond_en:
504 value = info.isp_io_cond_en;
505 message = "ISP IO condition enable";
506 break;
507 case get_isp_force:
508 value = info.isp_force;
509 message = "ISP Force";
510 break;
511 }
512 fprintf( stdout, "%s%s0x%02x (%d)\n",
513 ((0 == args->quiet) ? message : ""),
514 ((0 == args->quiet) ? ": " : ""),
515 value, value );
516 return 0;
517 }
518
519 static int32_t execute_get( dfu_device_t *device,
520 struct programmer_arguments *args ) {
521 atmel_device_info_t info;
522 char *message = NULL;
523 int16_t value = 0;
524 int32_t status;
525 int32_t controller_error = 0;
526
527 /* Check AVR32 security bit in order to provide a better error message. */
528 security_check( device );
529
530 if( args->device_type & GRP_STM32 ) {
531 fprintf( stderr, "Operation not supported on %s.\n",
532 args->device_type_string );
533 return -1;
534 } else {
535 status = atmel_read_config( device, &info );
536 }
537
538 if( 0 != status ) {
539 DEBUG( "Error reading %s config information.\n",
540 args->device_type_string );
541 fprintf( stderr, "Error reading %s config information.\n",
542 args->device_type_string );
543 security_message( device );
544 return status;
545 }
546
547 switch( args->com_get_data.name ) {
548 case get_bootloader:
549 value = info.bootloaderVersion;
550 message = "Bootloader Version";
551 break;
552 case get_ID1:
553 value = info.bootID1;
554 message = "Device boot ID 1";
555 break;
556 case get_ID2:
557 value = info.bootID2;
558 message = "Device boot ID 2";
559 break;
560 case get_BSB:
561 value = info.bsb;
562 message = "Boot Status Byte";
563 if( ADC_8051 != args->device_type ) {
564 controller_error = 1;
565 }
566 break;
567 case get_SBV:
568 value = info.sbv;
569 message = "Software Boot Vector";
570 if( ADC_8051 != args->device_type ) {
571 controller_error = 1;
572 }
573 break;
574 case get_SSB:
575 value = info.ssb;
576 message = "Software Security Byte";
577 if( ADC_8051 != args->device_type ) {
578 controller_error = 1;
579 }
580 break;
581 case get_EB:
582 value = info.eb;
583 message = "Extra Byte";
584 if( ADC_8051 != args->device_type ) {
585 controller_error = 1;
586 }
587 break;
588 case get_manufacturer:
589 value = info.manufacturerCode;
590 message = "Manufacturer Code";
591 break;
592 case get_family:
593 value = info.familyCode;
594 message = "Family Code";
595 break;
596 case get_product_name:
597 value = info.productName;
598 message = "Product Name";
599 break;
600 case get_product_rev:
601 value = info.productRevision;
602 message = "Product Revision";
603 break;
604 case get_HSB:
605 value = info.hsb;
606 message = "Hardware Security Byte";
607 if( ADC_8051 != args->device_type ) {
608 controller_error = 1;
609 }
610 break;
611 }
612
613 if( 0 != controller_error ) {
614 DEBUG( "%s requires 8051 based controller\n", message );
615 fprintf( stderr, "%s requires 8051 based controller\n",
616 message );
617 return -1;
618 }
619
620 if( value < 0 ) {
621 fprintf( stderr, "The requested device info is unavailable.\n" );
622 return -2;
623 }
624
625 fprintf( stdout, "%s%s0x%02x (%d)\n",
626 ((0 == args->quiet) ? message : ""),
627 ((0 == args->quiet) ? ": " : ""),
628 value, value );
629 return 0;
630 }
631
632 static int32_t execute_dump( dfu_device_t *device,
633 struct programmer_arguments *args ) {
634 int32_t i = 0;
635 int32_t retval = UNSPECIFIED_ERROR;
636 int32_t result; // result of fcn calls
637 intel_buffer_in_t buin; // buffer in for storing read mem
638 enum atmel_memory_unit_enum mem_segment = args->com_read_data.segment;
639 size_t mem_size = 0;
640 size_t page_size = 0;
641 uint32_t target_offset = 0; // address offset on the target device
642 // NOTE: target_offset may not be set appropriately for device
643 // classes other than ADC_AVR32
644
645 switch( mem_segment ) {
646 case mem_flash:
647 mem_size = args->memory_address_top + 1;
648 page_size = args->flash_page_size;
649 if( ADC_AVR32 == args->device_type ) {
650 target_offset = 0x80000000;
651 } else if( GRP_STM32 & args->device_type ) {
652 target_offset = STM32_FLASH_OFFSET;
653 }
654 break;
655 case mem_eeprom:
656 mem_size = args->eeprom_memory_size;
657 page_size = args->eeprom_page_size;
658 break;
659 case mem_user:
660 mem_size = args->flash_page_size;
661 page_size = args->flash_page_size;
662 target_offset = 0x80800000;
663 break;
664 default:
665 fprintf( stderr, "Dump not currently supported for this memory.\n" );
666 retval = ARGUMENT_ERROR;
667 goto error;
668 }
669
670 if( 0 != intel_init_buffer_in(&buin, mem_size, page_size) ) {
671 DEBUG("ERROR initializing a buffer.\n");
672 retval = BUFFER_INIT_ERROR;
673 goto error;
674 }
675
676 if( mem_segment == mem_flash ) {
677 buin.info.data_start = args->flash_address_bottom;
678 buin.info.data_end = args->flash_address_top;
679 }
680
681 if( args->device_type & GRP_STM32 ) {
682 result = stm32_read_flash(device, &buin, mem_segment, args->quiet);
683 } else {
684 /* Check AVR32 security bit in order to provide a better error message */
685 security_check( device ); // avr32 has no eeprom, but OK
686 result = atmel_read_flash(device, &buin, mem_segment, args->quiet);
687 }
688
689 if( 0 != result ) {
690 DEBUG("ERROR: could not read memory, err %d.\n", result);
691 security_message( device );
692 retval = FLASH_READ_ERROR;
693 goto error;
694 }
695
696 // determine first & last page with non-blank data
697 if( args->com_read_data.force ) {
698 buin.info.data_start = 0;
699 } else {
700 // find first page with data
701 for( i = buin.info.data_start; i < buin.info.data_end; i++ ) {
702 if( buin.data[i] != 0xFF ) break;
703 if( i / buin.info.page_size >
704 buin.info.data_start / buin.info.page_size ) {
705 // i has just jumped to a different page than buin.data_start
706 buin.info.data_start = i;
707 }
708 }
709 if( i == buin.info.data_end ) {
710 if( !args->quiet )
711 fprintf( stderr,
712 "Memory is blank, returning a single blank page.\n"
713 "Use --force to return the entire memory regardless.\n");
714 buin.info.data_start = 0;
715 buin.info.data_end = buin.info.page_size - 1;
716 } else { // find last page with data
717 for( i = buin.info.data_end; i > buin.info.data_start; i-- ) {
718 if( buin.data[i] !=0xFF ) break;
719 if( i / buin.info.page_size <
720 buin.info.data_end / buin.info.page_size ) {
721 buin.info.data_end = i;
722 }
723 }
724 }
725 }
726
727 if( args->com_read_data.bin ) {
728 if( !args->quiet )
729 fprintf( stderr, "Dumping 0x%X bytes from address offset 0x%X.\n",
730 buin.info.data_end + 1, target_offset );
731 for( i = 0; i <= buin.info.data_end; i++ ) {
732 fprintf( stdout, "%c", buin.data[i] );
733 }
734 } else {
735 if( !args->quiet )
736 fprintf( stderr, "Dumping 0x%X bytes from address offset 0x%X.\n",
737 buin.info.data_end - buin.info.data_start + 1,
738 target_offset + buin.info.data_start );
739 intel_hex_from_buffer( &buin,
740 args->com_read_data.force, target_offset );
741 }
742
743 fflush( stdout );
744
745 retval = SUCCESS;
746
747 error:
748 if( NULL != buin.data ) {
749 free( buin.data );
750 buin.data = NULL;
751 }
752
753 return retval;
754 }
755
756 static int32_t execute_setfuse( dfu_device_t *device,
757 struct programmer_arguments *args ) {
758 int32_t value = args->com_setfuse_data.value;
759 int32_t name = args->com_setfuse_data.name;
760
761 /* only ADC_AVR32 seems to support fuse operation */
762 if( !(ADC_AVR32 & args->device_type) || (GRP_STM32 & args->device_type) ) {
763 fprintf( stderr, "Operation not supported on %s\n",
764 args->device_type_string );
765 DEBUG( "target doesn't support fuse set operation.\n" );
766 return -1;
767 }
768
769 /* Check AVR32 security bit in order to provide a better error message. */
770 security_check( device );
771
772 if( 0 != atmel_set_fuse(device, name, value) ) {
773 DEBUG( "Fuse set failed.\n" );
774 fprintf( stderr, "Fuse set failed.\n" );
775 security_message( device );
776 return -1;
777 }
778
779 return 0;
780 }
781
782 static int32_t execute_configure( dfu_device_t *device,
783 struct programmer_arguments *args ) {
784 int32_t value = args->com_configure_data.value;
785 int32_t name = args->com_configure_data.name;
786
787 if( ADC_8051 != args->device_type ) {
788 fprintf( stderr, "Operation not supported on %s\n",
789 args->device_type_string );
790 DEBUG( "target doesn't support configure operation.\n" );
791 return -1;
792 }
793
794 if( (0xff & value) != value ) {
795 DEBUG( "Value to configure must be in range 0-255.\n" );
796 fprintf( stderr, "Value to configure must be in range 0-255.\n" );
797 return -1;
798 }
799
800 if( 0 != atmel_set_config(device, name, value) )
801 {
802 DEBUG( "Configuration set failed.\n" );
803 fprintf( stderr, "Configuration set failed.\n" );
804 return -1;
805 }
806
807 return 0;
808 }
809
810 static int32_t execute_launch( dfu_device_t *device,
811 struct programmer_arguments *args ) {
812 if( args->device_type & GRP_STM32 ) {
813 return stm32_start_app( device, args->quiet );
814 } else if( args->com_launch_config.noreset ) {
815 return atmel_start_app_noreset( device );
816 } else {
817 return atmel_start_app_reset( device );
818 }
819 }
820
821 int32_t execute_command( dfu_device_t *device,
822 struct programmer_arguments *args ) {
823 device->type = args->device_type;
824 switch( args->command ) {
825 case com_erase:
826 return execute_erase( device, args );
827 case com_flash:
828 return execute_flash( device, args );
829 case com_eflash:
830 args->com_flash_data.segment = mem_eeprom;
831 args->command = com_launch;
832 return execute_flash( device, args );
833 case com_user:
834 args->com_flash_data.segment = mem_user;
835 args->command = com_launch;
836 return execute_flash( device, args );
837 case com_start_app:
838 args->com_launch_config.noreset = true;
839 case com_reset:
840 args->command = com_launch;
841 case com_launch:
842 return execute_launch( device, args );
843 case com_get:
844 return execute_get( device, args );
845 case com_getfuse:
846 return execute_getfuse( device, args );
847 case com_dump:
848 args->command = com_read;
849 args->com_read_data.force = true;
850 args->com_read_data.bin = 1;
851 return execute_dump( device, args );
852 case com_edump:
853 args->com_read_data.segment = mem_eeprom;
854 args->com_read_data.force = true;
855 args->command = com_read;
856 args->com_read_data.bin = 1;
857 return execute_dump( device, args );
858 case com_udump:
859 args->com_read_data.segment = mem_eeprom;
860 args->com_read_data.force = true;
861 args->command = com_read;
862 args->com_read_data.bin = 1;
863 return execute_dump( device, args );
864 case com_read:
865 return execute_dump( device, args );
866 case com_configure:
867 return execute_configure( device, args );
868 case com_setfuse:
869 return execute_setfuse( device, args );
870 case com_setsecure:
871 return execute_setsecure( device, args );
872 default:
873 fprintf( stderr, "Not supported at this time.\n" );
874 }
875
876 return ARGUMENT_ERROR;
877 }