File: commands.c

package info (click to toggle)
dfu-programmer 0.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, jessie, jessie-kfreebsd, sid, stretch, trixie
  • size: 720 kB
  • sloc: ansic: 3,653; sh: 789; makefile: 15
file content (846 lines) | stat: -rw-r--r-- 25,853 bytes parent folder | download
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
/*
 * dfu-programmer
 *
 * $Id: commands.c 154 2013-04-04 11:44:06Z slarge $
 *
 * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 */

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>

#include "dfu-bool.h"
#include "config.h"
#include "commands.h"
#include "arguments.h"
#include "intel_hex.h"
#include "atmel.h"
#include "util.h"

#define COMMAND_DEBUG_THRESHOLD 40

#define DEBUG(...)  dfu_debug( __FILE__, __FUNCTION__, __LINE__, \
                               COMMAND_DEBUG_THRESHOLD, __VA_ARGS__ )


static int security_bit_state;

static void security_check( dfu_device_t *device )
{
    if( ADC_AVR32 == device->type ) {
        // Get security bit state for AVR32.
        security_bit_state = atmel_getsecure( device );
        DEBUG( "Security bit check returned %d.\n", security_bit_state );
    } else {
        // Security bit not present or not testable.
        security_bit_state = ATMEL_SECURE_OFF;
    }
}

static void security_message()
{
    if( security_bit_state > ATMEL_SECURE_OFF ) {
        fprintf( stderr, "The security bit %s set.\n"
                         "Erase the device to clear temporarily.\n",
                         (ATMEL_SECURE_ON == security_bit_state) ? "is" : "may be" );
    }
}

static int32_t execute_erase( dfu_device_t *device,
                              struct programmer_arguments *args )
{
    int32_t result = 0;

    DEBUG( "erase %d bytes\n",
           (args->flash_address_top - args->flash_address_bottom) );

    result = atmel_erase_flash( device, ATMEL_ERASE_ALL );
    if( 0 != result ) {
        return result;
    }

    return atmel_blank_check( device, args->flash_address_bottom,
                              args->flash_address_top );
}


static int32_t execute_setsecure( dfu_device_t *device,
                                  struct programmer_arguments *args )
{
    int32_t result;

    if( ADC_AVR32 != args->device_type ) {
        DEBUG( "target doesn't support security bit set.\n" );
        fprintf( stderr, "target doesn't support security bit set.\n" );
        return -1;
    }

    result = atmel_secure( device );

    if( result < 0 ) {
        DEBUG( "Error while setting security bit. (%d)\n", result );
        fprintf( stderr, "Error while setting security bit.\n" );
        return -1;
    }

    return 0;
}

static int32_t serialize_memory_image(int16_t *hex_data,
                                     struct programmer_arguments *args )
{
    if ( NULL != args->com_flash_data.serial_data ) {
        int16_t *serial_data = args->com_flash_data.serial_data;
        uint32_t length = args->com_flash_data.serial_length;
        uint32_t offset = args->com_flash_data.serial_offset;
        uint32_t i;
        /* The Atmel flash page starts at address 0x80000000, we need to ignore that bit */
        offset &= 0x7fffffff;
        if ((offset + length) > args->memory_address_top) {
            fprintf(stderr,"The serial data falls outside of the memory region.\n");
            return -1;
        }
        for (i=0; i<length; ++i) {
            hex_data[offset + i] = serial_data[i];
        }
    }
    return 0;
}

static int32_t execute_flash_eeprom( dfu_device_t *device,
                                     struct programmer_arguments *args )
{
    int32_t result;
    int32_t i;
    int32_t retval;
    int32_t usage;
    uint8_t *buffer = NULL;
    int16_t *hex_data = NULL;

    retval = -1;

    if( 0 == args->eeprom_memory_size ) {
        fprintf( stderr, "This device has no eeprom.\n" );
        return -1;
    }

    buffer = (uint8_t *) malloc( args->eeprom_memory_size );
    if( NULL == buffer ) {
        fprintf( stderr, "Request for %lu bytes of memory failed.\n",
                 (unsigned long) args->eeprom_memory_size );
        goto error;
    }
    memset( buffer, 0, args->eeprom_memory_size );

    hex_data = intel_hex_to_buffer( args->com_flash_data.file,
                                    args->eeprom_memory_size, &usage );
    if( NULL == hex_data ) {
        DEBUG( "Something went wrong with creating the memory image.\n" );
        fprintf( stderr,
                 "Something went wrong with creating the memory image.\n" );
        goto error;
    }

    if (0 != serialize_memory_image(hex_data,args))
      goto error;

    result = atmel_flash( device, hex_data, 0, args->eeprom_memory_size,
                          args->eeprom_page_size, true );

    if( result < 0 ) {
        DEBUG( "Error while programming eeprom. (%d)\n", result );
        fprintf( stderr, "Error while programming eeprom.\n" );
        goto error;
    }

    if( 0 == args->com_flash_data.suppress_validation ) {
        if( 0 == args->quiet ) {
            fprintf( stderr, "Validating...\n" );
        }

        result = atmel_read_flash( device, 0, args->eeprom_memory_size,
                                   buffer, args->eeprom_memory_size, true, false );

        if( args->eeprom_memory_size != result ) {
            DEBUG( "Error while reading back eeprom.\n" );
            fprintf( stderr, "Error while reading back eeprom.\n" );
            goto error;
        }

        for( i = 0; i < result; i++ ) {
            if( (0 <= hex_data[i]) && (hex_data[i] < UINT8_MAX) ) {
                /* Memory should have been programmed in this location. */
                if( ((uint8_t) hex_data[i]) != buffer[i] ) {
                    DEBUG( "Image did not validate at location: %d (%02x != %02x)\n", i,
                           (0xff & hex_data[i]), (0xff & buffer[i]) );
                    fprintf( stderr, "Eeprom did not validate.\n" );
                    goto error;
                }
            }
        }
    }

    if( 0 == args->quiet ) {
        fprintf( stderr, "%d bytes used (%.02f%%)\n", usage,
                         ((float)(usage*100)/(float)(args->eeprom_memory_size)) );
    }

    retval = 0;

error:
    if( NULL != buffer ) {
        free( buffer );
        buffer = NULL;
    }

    if( NULL != hex_data ) {
        free( hex_data );
        hex_data = NULL;
    }

    return retval;
}

static int32_t execute_flash_user_page( dfu_device_t *device,
                                        struct programmer_arguments *args )
{
    int32_t result;
    int32_t i;
    int32_t retval;
    int32_t usage;
    uint8_t *buffer = NULL;
    int16_t *hex_data = NULL;

    retval = -1;

    buffer = (uint8_t *) malloc( args->flash_page_size );
    if( NULL == buffer ) {
        fprintf( stderr, "Request for %lu bytes of memory failed.\n",
                 (unsigned long) args->flash_page_size );
        goto error;
    }
    memset( buffer, 0, args->flash_page_size );

    hex_data = intel_hex_to_buffer( args->com_flash_data.file,
                                    args->flash_page_size, &usage );
    if( NULL == hex_data ) {
        DEBUG( "Something went wrong with creating the memory image.\n" );
        fprintf( stderr,
                 "Something went wrong with creating the memory image.\n" );
        goto error;
    }

    if (0 != serialize_memory_image(hex_data,args))
      goto error;

    result = atmel_user( device, hex_data, args->flash_page_size );

    if( result < 0 ) {
        DEBUG( "Error while flashing user page. (%d)\n", result );
        fprintf( stderr, "Error while flashing user page.\n" );
        goto error;
    }

    if( 0 == args->com_flash_data.suppress_validation ) {
        if( 0 == args->quiet ) {
            fprintf( stderr, "Validating...\n" );
        }

        result = atmel_read_flash( device, 0, args->flash_page_size,
                                   buffer, args->flash_page_size, false, true );

        if( args->flash_page_size != result ) {
            DEBUG( "Error while reading back user flash.\n" );
            fprintf( stderr, "Error while reading back user flash.\n" );
            goto error;
        }

        for( i = 0; i < result; i++ ) {
            if( (0 <= hex_data[i]) && (hex_data[i] < UINT8_MAX) ) {
                /* Memory should have been programmed in this location. */
                if( ((uint8_t) hex_data[i]) != buffer[i] ) {
                    DEBUG( "Image did not validate at location: %d (%02x != %02x)\n", i,
                           (0xff & hex_data[i]), (0xff & buffer[i]) );
                    fprintf( stderr, "User flash did not validate. Did you erase first?\n" );
                    goto error;
                }
            }
        }
    }

    if( 0 == args->quiet ) {
        fprintf( stderr, "%d bytes used (%.02f%%)\n", usage,
                         ((float)(usage*100)/(float)(args->flash_page_size)) );
    }

    retval = 0;

error:
    if( NULL != buffer ) {
        free( buffer );
        buffer = NULL;
    }

    if( NULL != hex_data ) {
        free( hex_data );
        hex_data = NULL;
    }

    return retval;

}

static int32_t execute_flash_normal( dfu_device_t *device,
                                     struct programmer_arguments *args )
{
    int16_t *hex_data = NULL;
    int32_t  usage = 0;
    int32_t  retval = -1;
    int32_t  result = 0;
    uint8_t *buffer = NULL;
    uint32_t  i,j;
    uint32_t memory_size;
    uint32_t adjusted_flash_top_address;

    /* Why +1? Because the flash_address_top location is inclusive, as
     * apposed to most times when sizes are specified by length, etc.
     * and they are exclusive. */
    adjusted_flash_top_address = args->flash_address_top + 1;

    memory_size = adjusted_flash_top_address - args->flash_address_bottom;

    buffer = (uint8_t *) malloc( memory_size );

    if( NULL == buffer ) {
        fprintf( stderr, "Request for %d bytes of memory failed.\n",
                 memory_size );
        goto error;
    }

    memset( buffer, 0, memory_size );

    hex_data = intel_hex_to_buffer( args->com_flash_data.file,
                                    args->memory_address_top + 1, &usage );
    if( NULL == hex_data ) {
        DEBUG( "Something went wrong with creating the memory image.\n" );
        fprintf( stderr,
                 "Something went wrong with creating the memory image.\n" );
        goto error;
    }

    if (0 != serialize_memory_image(hex_data,args))
      goto error;

    for( i = args->bootloader_bottom; i <= args->bootloader_top; i++) {
        if( -1 != hex_data[i] ) {
            if( true == args->suppressbootloader ) {
                //If we're ignoring the bootloader, don't write to it
                hex_data[i] = -1;
            } else {
                fprintf( stderr, "Bootloader and code overlap.\n" );
                fprintf( stderr, "Use --suppress-bootloader-mem to ignore\n" );
                goto error;
            }
        }
    }

    DEBUG( "write %d/%d bytes\n", usage, memory_size );

    result = atmel_flash( device, hex_data, args->flash_address_bottom,
                          adjusted_flash_top_address, args->flash_page_size, false );

    if( result < 0 ) {
        DEBUG( "Error while flashing. (%d)\n", result );
        fprintf( stderr, "Error while flashing.\n" );
        goto error;
    }

    if( 0 == args->com_flash_data.suppress_validation ) {
        if( 0 == args->quiet ) {
            fprintf( stderr, "Validating...\n" );
        }

        result = atmel_read_flash( device, args->flash_address_bottom,
                                   adjusted_flash_top_address, buffer,
                                   memory_size, false, false );

        if( memory_size != result ) {
            DEBUG( "Error while reading back flash.\n" );
            fprintf( stderr, "Error while reading back flash.\n" );
            goto error;
        }

        for( i = 0, j = args->flash_address_bottom; i < result; i++, j++ ) {
            if( (0 <= hex_data[j]) && (hex_data[j] < UINT8_MAX) ) {
                /* Memory should have been programmed in this location. */
                if( ((uint8_t) hex_data[j]) != buffer[i] ) {
                    DEBUG( "Image did not validate at location: %d (%02x != %02x)\n", i,
                           (0xff & hex_data[j]), (0xff & buffer[i]) );
                    fprintf( stderr, "Flash did not validate. Did you erase first?\n" );
                    goto error;
                }
            }
        }
    }

    if( 0 == args->quiet ) {
        fprintf( stderr, "%d bytes used (%.02f%%)\n", usage,
                         ((float)(usage*100)/(float)
                         (adjusted_flash_top_address - args->flash_address_bottom)) );
    }

    retval = 0;

error:
    if( NULL != buffer ) {
        free( buffer );
        buffer = NULL;
    }

    if( NULL != hex_data ) {
        free( hex_data );
        hex_data = NULL;
    }

    return retval;

    return 0;
}

static int32_t execute_getfuse( dfu_device_t *device,
                            struct programmer_arguments *args )
{
    atmel_avr32_fuses_t info;
    char *message = NULL;
    int32_t value = 0;
    int32_t status;

    /* Check AVR32 security bit in order to provide a better error message. */
    security_check( device );

    status = atmel_read_fuses( device, &info );

    if( 0 != status ) {
        DEBUG( "Error reading %s config information.\n",
               args->device_type_string );
        fprintf( stderr, "Error reading %s config information.\n",
                         args->device_type_string );
        security_message();
        return status;
    }

    switch( args->com_getfuse_data.name ) {
        case get_lock:
            value = info.lock;
            message = "Locked regions";
            break;
        case get_epfl:
            value = info.epfl;
            message = "External Privileged Fetch Lock";
            break;
        case get_bootprot:
            value = info.bootprot;
            message = "Bootloader protected area";
            break;
        case get_bodlevel:
            value = info.bodlevel;
            message = "Brown-out detector trigger level";
            break;
        case get_bodhyst:
            value = info.bodhyst;
            message = "BOD Hysteresis enable";
            break;
        case get_boden:
            value = info.boden;
            message = "BOD Enable";
            break;
        case get_isp_bod_en:
            value = info.isp_bod_en;
            message = "ISP BOD enable";
            break;
        case get_isp_io_cond_en:
            value = info.isp_io_cond_en;
            message = "ISP IO condition enable";
            break;
        case get_isp_force:
            value = info.isp_force;
            message = "ISP Force";
            break;
    }
    fprintf( stdout, "%s%s0x%02x (%d)\n", 
             ((0 == args->quiet) ? message : ""),
             ((0 == args->quiet) ? ": " : ""),
             value, value );
    return 0;
}


static int32_t execute_get( dfu_device_t *device,
                            struct programmer_arguments *args )
{
    atmel_device_info_t info;
    char *message = NULL;
    int16_t value = 0;
    int32_t status;
    int32_t controller_error = 0;

    /* Check AVR32 security bit in order to provide a better error message. */
    security_check( device );

    status = atmel_read_config( device, &info );

    if( 0 != status ) {
        DEBUG( "Error reading %s config information.\n",
               args->device_type_string );
        fprintf( stderr, "Error reading %s config information.\n",
                         args->device_type_string );
        security_message();
        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( ADC_8051 != args->device_type ) {
                controller_error = 1;
            }
            break;
        case get_SBV:
            value = info.sbv;
            message = "Software Boot Vector";
            if( ADC_8051 != args->device_type ) {
                controller_error = 1;
            }
            break;
        case get_SSB:
            value = info.ssb;
            message = "Software Security Byte";
            if( ADC_8051 != args->device_type ) {
                controller_error = 1;
            }
            break;
        case get_EB:
            value = info.eb;
            message = "Extra Byte";
            if( ADC_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";
            if( ADC_8051 != args->device_type ) {
                controller_error = 1;
            }
            break;
    }

    if( 0 != controller_error ) {
        DEBUG( "%s requires 8051 based controller\n", message );
        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 int32_t execute_dump_normal( dfu_device_t *device,
                                    struct programmer_arguments *args )
{
    int32_t i = 0;
    uint8_t *buffer = NULL;
    size_t memory_size;
    size_t adjusted_flash_top_address;

    /* Why +1? Because the flash_address_top location is inclusive, as
     * apposed to most times when sizes are specified by length, etc.
     * and they are exclusive. */
    adjusted_flash_top_address = args->flash_address_top + 1;
    memory_size = adjusted_flash_top_address - args->flash_address_bottom;

    buffer = (uint8_t *) malloc( memory_size );
    if( NULL == buffer ) {
        fprintf( stderr, "Request for %lu bytes of memory failed.\n",
                 (unsigned long) memory_size );
        goto error;
    }

    /* Check AVR32 security bit in order to provide a better error message. */
    security_check( device );

    DEBUG( "dump %d bytes\n", memory_size );

    if( memory_size != atmel_read_flash(device, args->flash_address_bottom,
                                  adjusted_flash_top_address, buffer,
                                  memory_size, false, false) )
    {
        fprintf( stderr, "Failed to read %lu bytes from device.\n",
                 (unsigned long) memory_size );
        security_message();
        return -1;
    }

    if( false == args->bootloader_at_highmem ) {
        for( i = 0; i <= args->bootloader_top; i++ ) {
            fprintf( stdout, "%c", 0xff );
        }
    }
    for( i = 0; i < memory_size; i++ ) {
        fprintf( stdout, "%c", buffer[i] );
    }

    fflush( stdout );

error:
    if( NULL != buffer ) {
        free( buffer );
        buffer = NULL;
    }

    return 0;
}

static int32_t execute_dump_eeprom( dfu_device_t *device,
                                    struct programmer_arguments *args )
{
    int32_t i = 0;
    uint8_t *buffer = NULL;
    size_t memory_size;

    if( 0 == args->eeprom_memory_size ) {
        fprintf( stderr, "This device has no eeprom.\n" );
        return -1;
    }

    memory_size = args->eeprom_memory_size;

    buffer = (uint8_t *) malloc( args->eeprom_memory_size );
    if( NULL == buffer ) {
        fprintf( stderr, "Request for %lu bytes of memory failed.\n",
                 (unsigned long) memory_size );
        goto error;
    }

    /* Check AVR32 security bit in order to provide a better error message. */
    security_check( device );

    DEBUG( "dump %d bytes\n", memory_size );

    if( memory_size != atmel_read_flash(device, 0,
                                  args->eeprom_memory_size, buffer,
                                  memory_size, true, false) )
    {
        fprintf( stderr, "Failed to read %lu bytes from device.\n",
                 (unsigned long) memory_size );
        security_message();
        return -1;
    }

    for( i = 0; i < memory_size; i++ ) {
        fprintf( stdout, "%c", buffer[i] );
    }

    fflush( stdout );

error:
    if( NULL != buffer ) {
        free( buffer );
        buffer = NULL;
    }

    return 0;
}

static int32_t execute_dump_user_page( dfu_device_t *device,
                             struct programmer_arguments *args )
{
    int32_t i = 0;
    uint8_t *buffer = NULL;
    size_t page_size = args->flash_page_size;

    buffer = (uint8_t *) malloc( page_size );
    if( NULL == buffer ) {
        fprintf( stderr, "Request for %lu bytes of memory failed.\n",
                 (unsigned long) page_size );
        goto error;
    }

    /* Check AVR32 security bit in order to provide a better error message. */
    security_check( device );

    DEBUG( "dump %d bytes\n", page_size );

    if( page_size != atmel_read_flash(device, 0,
                                  page_size, buffer,
                                  page_size, false, true) )
    {
        fprintf( stderr, "Failed to read %lu bytes from device.\n",
                 (unsigned long) page_size );
        security_message();
        return -1;
    }

    for( i = 0; i < page_size; i++ ) {
        fprintf( stdout, "%c", buffer[i] );
    }

    fflush( stdout );

error:
    if( NULL != buffer ) {
        free( buffer );
        buffer = NULL;
    }

    return 0;
}

static int32_t execute_setfuse( dfu_device_t *device,
                                  struct programmer_arguments *args )
{
    int32_t value = args->com_setfuse_data.value;
    int32_t name = args->com_setfuse_data.name;

    if( GRP_AVR & args->device_type ) {
        DEBUG( "target doesn't support fuse set operation.\n" );
        fprintf( stderr, "target doesn't support fuse set operation.\n" );
        return -1;
    }

    /* Check AVR32 security bit in order to provide a better error message. */
    security_check( device );

    if( 0 != atmel_set_fuse(device, name, value) )
    {
        DEBUG( "Fuse set failed.\n" );
        fprintf( stderr, "Fuse set failed.\n" );
        security_message();
        return -1;
    }

    return 0;
}


static int32_t execute_configure( dfu_device_t *device,
                                  struct programmer_arguments *args )
{
    int32_t value = args->com_configure_data.value;
    int32_t name = args->com_configure_data.name;

    if( ADC_8051 != args->device_type ) {
        DEBUG( "target doesn't support configure operation.\n" );
        fprintf( stderr, "target doesn't support configure operation.\n" );
        return -1;
    }

    if( (0xff & value) != value ) {
        DEBUG( "Value to configure must be in range 0-255.\n" );
        fprintf( stderr, "Value to configure must be in range 0-255.\n" );
        return -1;
    }

    if( 0 != atmel_set_config(device, name, value) )
    {
        DEBUG( "Configuration set failed.\n" );
        fprintf( stderr, "Configuration set failed.\n" );
        return -1;
    }

    return 0;
}


int32_t execute_command( dfu_device_t *device,
                         struct programmer_arguments *args )
{
    device->type = args->device_type;
    switch( args->command ) {
        case com_erase:
            return execute_erase( device, args );
        case com_flash:
            return execute_flash_normal( device, args );
        case com_eflash:
            return execute_flash_eeprom( device, args );
        case com_user:
            return execute_flash_user_page( device, args );
        case com_reset:
            return atmel_reset( device ); 
        case com_start_app:
            return atmel_start_app( device );
        case com_get:
            return execute_get( device, args );
        case com_getfuse:
            return execute_getfuse( device, args );
        case com_dump:
            return execute_dump_normal( device, args );
        case com_edump:
            return execute_dump_eeprom( device, args );
        case com_udump:
            return execute_dump_user_page( device, args );
        case com_configure:
            return execute_configure( device, args );
        case com_setfuse:
            return execute_setfuse( device, args );
        case com_setsecure:
            return execute_setsecure( device, args );
        default:
            fprintf( stderr, "Not supported at this time.\n" );
    }

    return -1;
}