| 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 <stdarg.h> |
| 23 |
#include <stdint.h> |
| 24 |
#include <string.h> |
| 25 |
#include <stddef.h> |
| 26 |
#include <errno.h> |
| 27 |
#include <time.h> |
| 28 |
#include <unistd.h> |
| 29 |
#include <stdbool.h> |
| 30 |
|
| 31 |
#include "dfu-device.h" |
| 32 |
#include "config.h" |
| 33 |
#include "arguments.h" |
| 34 |
#include "dfu.h" |
| 35 |
#include "atmel.h" |
| 36 |
#include "util.h" |
| 37 |
|
| 38 |
|
| 39 |
/* Atmel's firmware doesn't export a DFU descriptor in its config |
| 40 |
* descriptor, so we have to guess about parameters listed there. |
| 41 |
* We use 3KB for wTransferSize (MAX_TRANSFER_SIZE). |
| 42 |
*/ |
| 43 |
/* a 64kb page contains 0x10000 values (0 to 0xFFFF). For the largest 512 kb |
| 44 |
* devices (2^19 bytes) there should be 8 pages. |
| 45 |
*/ |
| 46 |
#define ATMEL_64KB_PAGE 0x10000 |
| 47 |
#define ATMEL_MAX_TRANSFER_SIZE 0x0400 |
| 48 |
#define ATMEL_MAX_FLASH_BUFFER_SIZE (ATMEL_MAX_TRANSFER_SIZE + \ |
| 49 |
ATMEL_AVR32_CONTROL_BLOCK_SIZE + \ |
| 50 |
ATMEL_AVR32_CONTROL_BLOCK_SIZE + \ |
| 51 |
ATMEL_FOOTER_SIZE) |
| 52 |
|
| 53 |
#define ATMEL_FOOTER_SIZE 16 |
| 54 |
#define ATMEL_CONTROL_BLOCK_SIZE 32 |
| 55 |
#define ATMEL_AVR32_CONTROL_BLOCK_SIZE 64 |
| 56 |
|
| 57 |
#define ATMEL_DEBUG_THRESHOLD 50 |
| 58 |
#define ATMEL_TRACE_THRESHOLD 55 |
| 59 |
|
| 60 |
#define DEBUG(...) dfu_debug( __FILE__, __FUNCTION__, __LINE__, \ |
| 61 |
ATMEL_DEBUG_THRESHOLD, __VA_ARGS__ ) |
| 62 |
#define TRACE(...) dfu_debug( __FILE__, __FUNCTION__, __LINE__, \ |
| 63 |
ATMEL_TRACE_THRESHOLD, __VA_ARGS__ ) |
| 64 |
|
| 65 |
|
| 66 |
#define PROGRESS_METER "0%% 100%% " |
| 67 |
#define PROGRESS_START "[" |
| 68 |
#define PROGRESS_BAR ">" |
| 69 |
#define PROGRESS_END "] " |
| 70 |
#define PROGRESS_ERROR " X " |
| 71 |
|
| 72 |
extern int debug; /* defined in libdfu.c */ |
| 73 |
|
| 74 |
// ________ P R O T O T Y P E S _______________________________ |
| 75 |
static int32_t atmel_read_command( dfu_device_t *device, |
| 76 |
const uint8_t data0, |
| 77 |
const uint8_t data1 ); |
| 78 |
/* returns 0 - 255 on success, < 0 otherwise |
| 79 |
* |
| 80 |
* but what is it used for?? |
| 81 |
*/ |
| 82 |
|
| 83 |
static int32_t __atmel_flash_block( dfu_device_t *device, |
| 84 |
intel_buffer_out_t *bout, |
| 85 |
const bool eeprom ); |
| 86 |
/* flash the contents of memory into a block of memory. it is assumed that the |
| 87 |
* appropriate page has already been selected. start and end are the start and |
| 88 |
* end addresses of the flash data. returns 0 on success, positive dfu error |
| 89 |
* code if one is obtained, or negative if communication with device fails. |
| 90 |
*/ |
| 91 |
|
| 92 |
static int32_t atmel_select_memory_unit( dfu_device_t *device, |
| 93 |
enum atmel_memory_unit_enum unit ); |
| 94 |
/* select a memory unit from the following list (enumerated) |
| 95 |
* flash, eeprom, security, configuration, bootloader, signature, user page |
| 96 |
*/ |
| 97 |
|
| 98 |
static int32_t atmel_select_page( dfu_device_t *device, |
| 99 |
const uint16_t mem_page ); |
| 100 |
/* select a page in memory, numbering starts with 0, pages are |
| 101 |
* 64kb pages (0x10000 bytes). Select page when the memory unit |
| 102 |
* is set to the user page will cause an error. |
| 103 |
*/ |
| 104 |
|
| 105 |
static int32_t __atmel_blank_page_check( dfu_device_t *device, |
| 106 |
const uint32_t start, |
| 107 |
const uint32_t end ); |
| 108 |
/* use to check if a certain address range on the current page is blank |
| 109 |
* it assumes current page has previously been selected. |
| 110 |
* returns 0 if the page is blank |
| 111 |
* returns the first non-blank address + 1 if not blank (no zero!) |
| 112 |
* returns a negative number if the blank check fails |
| 113 |
*/ |
| 114 |
|
| 115 |
static int32_t __atmel_read_block( dfu_device_t *device, |
| 116 |
intel_buffer_in_t *buin, |
| 117 |
const bool eeprom ); |
| 118 |
/* assumes block does not cross 64 b page boundaries and ideally aligns |
| 119 |
* with flash pages. appropriate memory type and 64kb page has already |
| 120 |
* been selected, max transfer size is not violated it updates the buffer |
| 121 |
* data between data_start and data_end |
| 122 |
*/ |
| 123 |
|
| 124 |
static inline void __print_progress( intel_buffer_info_t *info, |
| 125 |
uint32_t *progress ); |
| 126 |
/* calculate how many progress indicator steps to print and print them |
| 127 |
* update progress value |
| 128 |
*/ |
| 129 |
|
| 130 |
// ________ F U N C T I O N S _______________________________ |
| 131 |
static int32_t atmel_read_command( dfu_device_t *device, |
| 132 |
const uint8_t data0, |
| 133 |
const uint8_t data1 ) { |
| 134 |
intel_buffer_in_t buin; |
| 135 |
uint8_t buffer[4]; |
| 136 |
|
| 137 |
TRACE( "%s( %p, 0x%02x, 0x%02x )\n", __FUNCTION__, device, data0, data1 ); |
| 138 |
|
| 139 |
// init the necessary parts of buin |
| 140 |
buin.info.block_start = data1; |
| 141 |
buin.info.block_end = data1; |
| 142 |
buin.data = buffer; |
| 143 |
|
| 144 |
if( NULL == device ) { |
| 145 |
DEBUG( "invalid arguments.\n" ); |
| 146 |
return -1; |
| 147 |
} |
| 148 |
|
| 149 |
if( GRP_AVR32 & device->type ) { |
| 150 |
//We need to talk to configuration memory. It comes |
| 151 |
//in two varieties in this chip. data0 is the command to |
| 152 |
//select it. Data1 is the byte of that group we want |
| 153 |
|
| 154 |
if( 0 != atmel_select_memory_unit(device, data0) ) { |
| 155 |
return -3; |
| 156 |
} |
| 157 |
|
| 158 |
if( 0 != __atmel_read_block(device, &buin, false) ) { |
| 159 |
return -5; |
| 160 |
} |
| 161 |
|
| 162 |
return (0xff & buffer[data1]); |
| 163 |
} else { |
| 164 |
uint8_t command[3] = { 0x05, 0x00, 0x00 }; |
| 165 |
uint8_t data[1] = { 0x00 }; |
| 166 |
dfu_status_t status; |
| 167 |
|
| 168 |
command[1] = data0; |
| 169 |
command[2] = data1; |
| 170 |
|
| 171 |
|
| 172 |
if( 3 != dfu_download(device, 3, command) ) { |
| 173 |
DEBUG( "dfu_download failed\n" ); |
| 174 |
return -1; |
| 175 |
} |
| 176 |
|
| 177 |
if( 0 != dfu_get_status(device, &status) ) { |
| 178 |
DEBUG( "dfu_get_status failed\n" ); |
| 179 |
return -2; |
| 180 |
} |
| 181 |
|
| 182 |
if( DFU_STATUS_OK != status.bStatus ) { |
| 183 |
DEBUG( "status(%s) was not OK.\n", |
| 184 |
dfu_status_to_string(status.bStatus) ); |
| 185 |
dfu_clear_status( device ); |
| 186 |
return -3; |
| 187 |
} |
| 188 |
|
| 189 |
if( 1 != dfu_upload(device, 1, data) ) { |
| 190 |
DEBUG( "dfu_upload failed\n" ); |
| 191 |
return -4; |
| 192 |
} |
| 193 |
|
| 194 |
return (0xff & data[0]); |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
static inline void __print_progress( intel_buffer_info_t *info, |
| 199 |
uint32_t *progress ) { |
| 200 |
if ( !(debug > ATMEL_DEBUG_THRESHOLD) && isatty(2) ) { |
| 201 |
while ( ((info->block_end - info->data_start + 1) * 32) > *progress ) { |
| 202 |
fprintf( stderr, PROGRESS_BAR ); |
| 203 |
*progress += info->data_end - info->data_start + 1; |
| 204 |
} |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
int32_t atmel_read_fuses( dfu_device_t *device, |
| 209 |
atmel_avr32_fuses_t *info ) { |
| 210 |
intel_buffer_in_t buin; |
| 211 |
uint8_t buffer[32]; |
| 212 |
int i; |
| 213 |
|
| 214 |
// init the necessary parts of buin |
| 215 |
buin.info.block_start = 0; |
| 216 |
buin.info.block_end = 31; |
| 217 |
buin.data = buffer; |
| 218 |
|
| 219 |
if( NULL == device ) { |
| 220 |
DEBUG( "invalid arguments.\n" ); |
| 221 |
return ARGUMENT_ERROR; |
| 222 |
} |
| 223 |
|
| 224 |
if( !(ADC_AVR32 & device->type) ) { |
| 225 |
DEBUG( "target does not support fuse operation.\n" ); |
| 226 |
fprintf( stderr, "target does not support fuse operation.\n" ); |
| 227 |
return ARGUMENT_ERROR; |
| 228 |
} |
| 229 |
|
| 230 |
if( 0 != atmel_select_memory_unit(device, mem_config) ) { |
| 231 |
return -3; |
| 232 |
} |
| 233 |
|
| 234 |
if( 0 != __atmel_read_block(device, &buin, false) ) { |
| 235 |
return -5; |
| 236 |
} |
| 237 |
|
| 238 |
info->lock = 0; |
| 239 |
for(i = 0; i < 16; i++) { |
| 240 |
info->lock = info->lock | (buffer[i] << i); |
| 241 |
} |
| 242 |
info->epfl = buffer[16]; |
| 243 |
info->bootprot = (buffer[19] << 2) | (buffer[18] << 1) | (buffer[17] << 0); |
| 244 |
info->bodlevel = 0; |
| 245 |
for(i = 20; i < 26; i++) { |
| 246 |
info->bodlevel = info->bodlevel | (buffer[i] << (i-20)); |
| 247 |
} |
| 248 |
info->bodhyst = buffer[26]; |
| 249 |
info->boden = (buffer[28] << 1) | (buffer[27] << 0); |
| 250 |
info->isp_bod_en = buffer[29]; |
| 251 |
info->isp_io_cond_en = buffer[30]; |
| 252 |
info->isp_force = buffer[31]; |
| 253 |
|
| 254 |
return 0; |
| 255 |
} |
| 256 |
|
| 257 |
int32_t atmel_read_config( dfu_device_t *device, |
| 258 |
atmel_device_info_t *info ) { |
| 259 |
typedef struct { |
| 260 |
uint8_t data0; |
| 261 |
uint8_t data1; |
| 262 |
uint8_t device_map; |
| 263 |
size_t offset; |
| 264 |
} atmel_read_config_t; |
| 265 |
|
| 266 |
/* These commands are documented in Appendix A of the |
| 267 |
* "AT89C5131A USB Bootloader Datasheet" or |
| 268 |
* "AT90usb128x/AT90usb64x USB DFU Bootloader Datasheet" |
| 269 |
*/ |
| 270 |
static const atmel_read_config_t data[] = { |
| 271 |
{ 0x00, 0x00, (ADC_8051 | ADC_AVR), offsetof(atmel_device_info_t, bootloaderVersion) }, |
| 272 |
{ 0x04, 0x00, (ADC_AVR32 | ADC_XMEGA), offsetof(atmel_device_info_t, bootloaderVersion) }, |
| 273 |
{ 0x00, 0x01, (ADC_8051 | ADC_AVR), offsetof(atmel_device_info_t, bootID1) }, |
| 274 |
{ 0x04, 0x01, (ADC_AVR32 | ADC_XMEGA), offsetof(atmel_device_info_t, bootID1) }, |
| 275 |
{ 0x00, 0x02, (ADC_8051 | ADC_AVR), offsetof(atmel_device_info_t, bootID2) }, |
| 276 |
{ 0x04, 0x02, (ADC_AVR32 | ADC_XMEGA), offsetof(atmel_device_info_t, bootID2) }, |
| 277 |
{ 0x01, 0x30, (ADC_8051 | ADC_AVR), offsetof(atmel_device_info_t, manufacturerCode) }, |
| 278 |
{ 0x05, 0x00, (ADC_AVR32 | ADC_XMEGA), offsetof(atmel_device_info_t, manufacturerCode) }, |
| 279 |
{ 0x01, 0x31, (ADC_8051 | ADC_AVR), offsetof(atmel_device_info_t, familyCode) }, |
| 280 |
{ 0x05, 0x01, (ADC_AVR32 | ADC_XMEGA), offsetof(atmel_device_info_t, familyCode) }, |
| 281 |
{ 0x01, 0x60, (ADC_8051 | ADC_AVR), offsetof(atmel_device_info_t, productName) }, |
| 282 |
{ 0x05, 0x02, (ADC_AVR32 | ADC_XMEGA), offsetof(atmel_device_info_t, productName) }, |
| 283 |
{ 0x01, 0x61, (ADC_8051 | ADC_AVR), offsetof(atmel_device_info_t, productRevision) }, |
| 284 |
{ 0x05, 0x03, (ADC_AVR32 | ADC_XMEGA), offsetof(atmel_device_info_t, productRevision) }, |
| 285 |
{ 0x01, 0x00, ADC_8051, offsetof(atmel_device_info_t, bsb) }, |
| 286 |
{ 0x01, 0x01, ADC_8051, offsetof(atmel_device_info_t, sbv) }, |
| 287 |
{ 0x01, 0x05, ADC_8051, offsetof(atmel_device_info_t, ssb) }, |
| 288 |
{ 0x01, 0x06, ADC_8051, offsetof(atmel_device_info_t, eb) }, |
| 289 |
{ 0x02, 0x00, ADC_8051, offsetof(atmel_device_info_t, hsb) } |
| 290 |
}; |
| 291 |
|
| 292 |
int32_t result; |
| 293 |
int32_t retVal = 0; |
| 294 |
int32_t i = 0; |
| 295 |
|
| 296 |
TRACE( "%s( %p, %p )\n", __FUNCTION__, device, info ); |
| 297 |
|
| 298 |
if( NULL == device ) { |
| 299 |
DEBUG( "invalid arguments.\n" ); |
| 300 |
return -1; |
| 301 |
} |
| 302 |
|
| 303 |
for( i = 0; i < sizeof(data)/sizeof(atmel_read_config_t); i++ ) { |
| 304 |
atmel_read_config_t *row = (atmel_read_config_t*) &data[i]; |
| 305 |
|
| 306 |
if( row->device_map & device->type ) |
| 307 |
{ |
| 308 |
int16_t *ptr = row->offset + (void *) info; |
| 309 |
|
| 310 |
result = atmel_read_command( device, row->data0, row->data1 ); |
| 311 |
if( result < 0 ) { |
| 312 |
retVal = result; |
| 313 |
} |
| 314 |
*ptr = result; |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
return retVal; |
| 319 |
} |
| 320 |
|
| 321 |
int32_t atmel_erase_flash( dfu_device_t *device, |
| 322 |
const uint8_t mode, |
| 323 |
bool quiet ) { |
| 324 |
uint8_t command[3] = { 0x04, 0x00, 0x00 }; |
| 325 |
dfu_status_t status; |
| 326 |
int32_t retries; |
| 327 |
time_t start; |
| 328 |
|
| 329 |
TRACE( "%s( %p, %d )\n", __FUNCTION__, device, mode ); |
| 330 |
|
| 331 |
switch( mode ) { |
| 332 |
case ATMEL_ERASE_BLOCK_0: |
| 333 |
command[2] = 0x00; |
| 334 |
break; |
| 335 |
case ATMEL_ERASE_BLOCK_1: |
| 336 |
command[2] = 0x20; |
| 337 |
break; |
| 338 |
case ATMEL_ERASE_BLOCK_2: |
| 339 |
command[2] = 0x40; |
| 340 |
break; |
| 341 |
case ATMEL_ERASE_BLOCK_3: |
| 342 |
command[2] = 0x80; |
| 343 |
break; |
| 344 |
case ATMEL_ERASE_ALL: |
| 345 |
command[2] = 0xff; |
| 346 |
break; |
| 347 |
|
| 348 |
default: |
| 349 |
return -1; |
| 350 |
} |
| 351 |
|
| 352 |
if( !quiet ) fprintf( stderr, "Erasing flash... " ); |
| 353 |
if( 3 != dfu_download(device, 3, command) ) { |
| 354 |
if( !quiet ) fprintf( stderr, "ERROR\n" ); |
| 355 |
DEBUG( "dfu_download failed\n" ); |
| 356 |
return -2; |
| 357 |
} |
| 358 |
|
| 359 |
/* It looks like it can take a while to erase the chip. |
| 360 |
* We will try for 20 seconds before giving up. |
| 361 |
* Different version of the bootloader behave in different ways. |
| 362 |
* In some the dfu_get_status() call blocks until the operation completes. |
| 363 |
* In others it returns immediately with an erase-in-progress status. |
| 364 |
*/ |
| 365 |
#define ERASE_SECONDS 20 |
| 366 |
start = time(NULL); |
| 367 |
retries = 0; |
| 368 |
do { |
| 369 |
if( 0 == dfu_get_status(device, &status) ) { |
| 370 |
// Status return is valid |
| 371 |
if( (DFU_STATUS_ERROR_NOTDONE == status.bStatus) && |
| 372 |
(STATE_DFU_DOWNLOAD_BUSY == status.bState) ) { |
| 373 |
// Erase is still in progress. |
| 374 |
// Wait 100ms and get status again. |
| 375 |
usleep(100000); |
| 376 |
} else { |
| 377 |
// Erase complete. |
| 378 |
if( !quiet ) fprintf( stderr, "Success\n" ); |
| 379 |
DEBUG ( "CMD_ERASE status: Erase Done.\n" ); |
| 380 |
return status.bStatus; |
| 381 |
} |
| 382 |
} else { |
| 383 |
// Status command failed. |
| 384 |
dfu_clear_status( device ); |
| 385 |
++retries; |
| 386 |
if( !quiet ) fprintf( stderr, "ERROR\n" ); |
| 387 |
DEBUG ( "CMD_ERASE status check %d returned nonzero.\n", retries ); |
| 388 |
} |
| 389 |
} while( (retries < 10) && (start != -1) && ((time(NULL) - start) < ERASE_SECONDS) ); |
| 390 |
|
| 391 |
if( retries < 10 ) |
| 392 |
DEBUG ( "CMD_ERASE time limit %ds exceeded.\n", ERASE_SECONDS ); |
| 393 |
|
| 394 |
return -3; |
| 395 |
} |
| 396 |
|
| 397 |
int32_t atmel_set_fuse( dfu_device_t *device, |
| 398 |
const uint8_t property, |
| 399 |
const uint32_t value ) { |
| 400 |
uint16_t buffer[16]; |
| 401 |
int32_t address; |
| 402 |
int8_t numbytes; |
| 403 |
int8_t i; |
| 404 |
intel_buffer_out_t bout; |
| 405 |
|
| 406 |
if( NULL == device ) { |
| 407 |
DEBUG( "invalid arguments.\n" ); |
| 408 |
return -1; |
| 409 |
} |
| 410 |
|
| 411 |
if( !(ADC_AVR32 & device->type) ) { |
| 412 |
DEBUG( "target does not support fuse operation.\n" ); |
| 413 |
fprintf( stderr, "target does not support fuse operation.\n" ); |
| 414 |
return -1; |
| 415 |
} |
| 416 |
|
| 417 |
if( 0 != atmel_select_memory_unit(device, mem_config) ) { |
| 418 |
return -3; |
| 419 |
} |
| 420 |
|
| 421 |
switch( property ) { |
| 422 |
case set_lock: |
| 423 |
for( i = 0; i < 16; i++ ) { |
| 424 |
buffer[i] = value & (0x0001 << i); |
| 425 |
} |
| 426 |
numbytes = 16; |
| 427 |
address = 0; |
| 428 |
break; |
| 429 |
case set_epfl: |
| 430 |
buffer[0] = value & 0x0001; |
| 431 |
numbytes = 1; |
| 432 |
address = 16; |
| 433 |
break; |
| 434 |
case set_bootprot: |
| 435 |
buffer[0] = value & 0x0001; |
| 436 |
buffer[1] = value & 0x0002; |
| 437 |
buffer[2] = value & 0x0004; |
| 438 |
numbytes = 3; |
| 439 |
address = 17; |
| 440 |
break; |
| 441 |
case set_bodlevel: |
| 442 |
#ifdef SUPPORT_SET_BOD_FUSES |
| 443 |
/* Enable at your own risk - this has not been tested & |
| 444 |
* may brick your device. */ |
| 445 |
for(i = 20;i < 26; i++){ |
| 446 |
buffer[i] = value & (0x0001 << (i-20)); |
| 447 |
} |
| 448 |
numbytes = 6; |
| 449 |
address = 20; |
| 450 |
break; |
| 451 |
#else |
| 452 |
DEBUG( "Setting BODLEVEL can break your chip. Operation not performed\n" ); |
| 453 |
DEBUG( "Rebuild with the SUPPORT_SET_BOD_FUSES #define enabled if you really want to do this.\n" ); |
| 454 |
fprintf( stderr, "Setting BODLEVEL can break your chip. Operation not performed.\n" ); |
| 455 |
return -1; |
| 456 |
#endif |
| 457 |
case set_bodhyst: |
| 458 |
#ifdef SUPPORT_SET_BOD_FUSES |
| 459 |
/* Enable at your own risk - this has not been tested & |
| 460 |
* may brick your device. */ |
| 461 |
buffer[0] = value & 0x0001; |
| 462 |
numbytes = 1; |
| 463 |
address = 26; |
| 464 |
break; |
| 465 |
#else |
| 466 |
DEBUG("Setting BODHYST can break your chip. Operation not performed\n"); |
| 467 |
DEBUG( "Rebuild with the SUPPORT_SET_BOD_FUSES #define enabled if you really want to do this.\n" ); |
| 468 |
fprintf( stderr, "Setting BODHYST can break your chip. Operation not performed.\n"); |
| 469 |
return -1; |
| 470 |
#endif |
| 471 |
case set_boden: |
| 472 |
#ifdef SUPPORT_SET_BOD_FUSES |
| 473 |
/* Enable at your own risk - this has not been tested & |
| 474 |
* may brick your device. */ |
| 475 |
buffer[0] = value & 0x0001; |
| 476 |
buffer[1] = value & 0x0002; |
| 477 |
numbytes = 2; |
| 478 |
address = 27; |
| 479 |
break; |
| 480 |
#else |
| 481 |
DEBUG( "Setting BODEN can break your chip. Operation not performed\n" ); |
| 482 |
DEBUG( "Rebuild with the SUPPORT_SET_BOD_FUSES #define enabled if you really want to do this.\n" ); |
| 483 |
fprintf( stderr, "Setting BODEN can break your chip. Operation not performed.\n" ); |
| 484 |
return -1; |
| 485 |
#endif |
| 486 |
case set_isp_bod_en: |
| 487 |
#ifdef SUPPORT_SET_BOD_FUSES |
| 488 |
/* Enable at your own risk - this has not been tested & |
| 489 |
* may brick your device. */ |
| 490 |
buffer[0] = value & 0x0001; |
| 491 |
numbytes = 1; |
| 492 |
address = 29; |
| 493 |
break; |
| 494 |
#else |
| 495 |
DEBUG( "Setting ISP_BOD_EN can break your chip. Operation not performed\n" ); |
| 496 |
DEBUG( "Rebuild with the SUPPORT_SET_BOD_FUSES #define enabled if you really want to do this.\n" ); |
| 497 |
fprintf( stderr, "Setting ISP_BOD_EN can break your chip. Operation not performed.\n" ); |
| 498 |
return -1; |
| 499 |
#endif |
| 500 |
case set_isp_io_cond_en: |
| 501 |
buffer[0] = value & 0x0001; |
| 502 |
numbytes = 1; |
| 503 |
address = 30; |
| 504 |
break; |
| 505 |
case set_isp_force: |
| 506 |
buffer[0] = value & 0x0001; |
| 507 |
numbytes = 1; |
| 508 |
address = 31; |
| 509 |
break; |
| 510 |
default: |
| 511 |
DEBUG( "Fuse bits unrecognized\n" ); |
| 512 |
fprintf( stderr, "Fuse bits unrecognized.\n" ); |
| 513 |
return -2; |
| 514 |
break; |
| 515 |
} |
| 516 |
|
| 517 |
bout.data = buffer; |
| 518 |
bout.info.block_start = address; |
| 519 |
bout.info.block_end = address + numbytes - 1; |
| 520 |
|
| 521 |
if( 0 != __atmel_flash_block(device, &bout, false) ) { |
| 522 |
return -6; |
| 523 |
} |
| 524 |
|
| 525 |
return 0; |
| 526 |
} |
| 527 |
|
| 528 |
int32_t atmel_set_config( dfu_device_t *device, |
| 529 |
const uint8_t property, |
| 530 |
const uint8_t value ) { |
| 531 |
uint8_t command[4] = { 0x04, 0x01, 0x00, 0x00 }; |
| 532 |
dfu_status_t status; |
| 533 |
|
| 534 |
TRACE( "%s( %p, %d, 0x%02x )\n", __FUNCTION__, device, property, value ); |
| 535 |
|
| 536 |
switch( property ) { |
| 537 |
case ATMEL_SET_CONFIG_BSB: |
| 538 |
break; |
| 539 |
case ATMEL_SET_CONFIG_SBV: |
| 540 |
command[2] = 0x01; |
| 541 |
break; |
| 542 |
case ATMEL_SET_CONFIG_SSB: |
| 543 |
command[2] = 0x05; |
| 544 |
break; |
| 545 |
case ATMEL_SET_CONFIG_EB: |
| 546 |
command[2] = 0x06; |
| 547 |
break; |
| 548 |
case ATMEL_SET_CONFIG_HSB: |
| 549 |
command[1] = 0x02; |
| 550 |
break; |
| 551 |
default: |
| 552 |
return -1; |
| 553 |
} |
| 554 |
|
| 555 |
command[3] = value; |
| 556 |
|
| 557 |
if( 4 != dfu_download(device, 4, command) ) { |
| 558 |
DEBUG( "dfu_download failed\n" ); |
| 559 |
return -2; |
| 560 |
} |
| 561 |
|
| 562 |
if( 0 != dfu_get_status(device, &status) ) { |
| 563 |
DEBUG( "dfu_get_status failed\n" ); |
| 564 |
return -3; |
| 565 |
} |
| 566 |
|
| 567 |
if( DFU_STATUS_ERROR_WRITE == status.bStatus ) { |
| 568 |
fprintf( stderr, "Device is write protected.\n" ); |
| 569 |
} |
| 570 |
|
| 571 |
return status.bStatus; |
| 572 |
} |
| 573 |
|
| 574 |
static int32_t __atmel_read_block( dfu_device_t *device, |
| 575 |
intel_buffer_in_t *buin, |
| 576 |
const bool eeprom ) { |
| 577 |
uint8_t command[6] = { 0x03, 0x00, 0x00, 0x00, 0x00, 0x00 }; |
| 578 |
int32_t result; |
| 579 |
|
| 580 |
if( buin->info.block_end < buin->info.block_start ) { |
| 581 |
// this would cause a problem bc read length could be way off |
| 582 |
DEBUG("ERROR: start address is after end address.\n"); |
| 583 |
return -1; |
| 584 |
} else if( buin->info.block_end - buin->info.block_start + 1 > ATMEL_MAX_TRANSFER_SIZE ) { |
| 585 |
// this could cause a read problem |
| 586 |
DEBUG("ERROR: transfer size must not exceed %d.\n", |
| 587 |
ATMEL_MAX_TRANSFER_SIZE ); |
| 588 |
return -1; |
| 589 |
} |
| 590 |
|
| 591 |
// AVR/8051 requires 0x02 here to read eeprom, XMEGA requires 0x00. |
| 592 |
if( true == eeprom && (GRP_AVR & device->type) ) { |
| 593 |
command[1] = 0x02; |
| 594 |
} |
| 595 |
|
| 596 |
command[2] = 0xff & (buin->info.block_start >> 8); |
| 597 |
command[3] = 0xff & (buin->info.block_start); |
| 598 |
command[4] = 0xff & (buin->info.block_end >> 8); |
| 599 |
command[5] = 0xff & (buin->info.block_end); |
| 600 |
|
| 601 |
if( 6 != dfu_download(device, 6, command) ) { |
| 602 |
DEBUG( "dfu_download failed\n" ); |
| 603 |
return -1; |
| 604 |
} |
| 605 |
|
| 606 |
result = dfu_upload( device, buin->info.block_end - buin->info.block_start + 1, |
| 607 |
&buin->data[buin->info.block_start] ); |
| 608 |
if( result < 0) { |
| 609 |
dfu_status_t status; |
| 610 |
|
| 611 |
DEBUG( "dfu_upload result: %d\n", result ); |
| 612 |
if( 0 == dfu_get_status(device, &status) ) { |
| 613 |
if( DFU_STATUS_ERROR_FILE == status.bStatus ) { |
| 614 |
fprintf( stderr, |
| 615 |
"The device is read protected.\n" ); |
| 616 |
} else { |
| 617 |
fprintf( stderr, "Unknown error. Try enabling debug.\n" ); |
| 618 |
} |
| 619 |
} else { |
| 620 |
fprintf( stderr, "Device is unresponsive.\n" ); |
| 621 |
} |
| 622 |
dfu_clear_status( device ); |
| 623 |
|
| 624 |
return result; |
| 625 |
} |
| 626 |
|
| 627 |
return 0; |
| 628 |
} |
| 629 |
|
| 630 |
int32_t atmel_read_flash( dfu_device_t *device, |
| 631 |
intel_buffer_in_t *buin, |
| 632 |
const uint8_t mem_segment, |
| 633 |
const bool quiet ) { |
| 634 |
uint8_t mem_page = 0; // tracks the current memory page |
| 635 |
uint32_t progress = 0; // used to indicate progress |
| 636 |
int32_t result = 0; |
| 637 |
// TODO : use status instead of result |
| 638 |
int32_t retval = -1; // the return value for this function |
| 639 |
|
| 640 |
TRACE( "%s( %p, %p, %u, %s )\n", __FUNCTION__, device, buin, |
| 641 |
mem_segment, ((true == quiet) ? "true" : "false")); |
| 642 |
|
| 643 |
if( (NULL == buin) || (NULL == device) ) { |
| 644 |
DEBUG( "invalid arguments.\n" ); |
| 645 |
if( !quiet ) |
| 646 |
fprintf( stderr, "Program Error, use debug for more info.\n" ); |
| 647 |
return -1; |
| 648 |
} else if ( mem_segment != mem_flash && |
| 649 |
mem_segment != mem_user && |
| 650 |
mem_segment != mem_eeprom ) { |
| 651 |
DEBUG( "Invalid memory segment %d to read.\n", mem_segment ); |
| 652 |
if( !quiet ) |
| 653 |
fprintf( stderr, "Program Error, use debug for more info.\n" ); |
| 654 |
return -1; |
| 655 |
} |
| 656 |
|
| 657 |
// For the AVR32/XMEGA chips, select the flash space. (safe for all parts) |
| 658 |
if( 0 != atmel_select_memory_unit(device, mem_segment) ) { |
| 659 |
DEBUG ("Error selecting memory unit.\n"); |
| 660 |
if( !quiet ) |
| 661 |
fprintf( stderr, "Memory access error, use debug for more info.\n" ); |
| 662 |
return -3; |
| 663 |
} |
| 664 |
|
| 665 |
if( !quiet ) { |
| 666 |
if( debug <= ATMEL_DEBUG_THRESHOLD && isatty(2) ) { |
| 667 |
// NOTE: From here on we should go to finally on error |
| 668 |
fprintf( stderr, PROGRESS_METER ); |
| 669 |
} |
| 670 |
fprintf( stderr, "Reading 0x%X bytes...\n", |
| 671 |
buin->info.data_end - buin->info.data_start + 1 ); |
| 672 |
if( debug <= ATMEL_DEBUG_THRESHOLD && isatty(2) ) { |
| 673 |
// NOTE: From here on we should go to finally on error |
| 674 |
fprintf( stderr, PROGRESS_START ); |
| 675 |
} |
| 676 |
} |
| 677 |
|
| 678 |
// select the first memory page ( not safe for mem_user ) |
| 679 |
buin->info.block_start = buin->info.data_start; |
| 680 |
mem_page = buin->info.block_start / ATMEL_64KB_PAGE; |
| 681 |
if ( mem_segment != mem_user ) { |
| 682 |
if( 0 != (result = atmel_select_page( device, mem_page )) ) { |
| 683 |
DEBUG( "ERROR selecting 64kB page %d.\n", result ); |
| 684 |
retval = -3; |
| 685 |
goto finally; |
| 686 |
} |
| 687 |
} |
| 688 |
|
| 689 |
while (buin->info.block_start <= buin->info.data_end) { |
| 690 |
// ensure the memory page is correct |
| 691 |
if ( buin->info.block_start / ATMEL_64KB_PAGE != mem_page ) { |
| 692 |
mem_page = buin->info.block_start / ATMEL_64KB_PAGE; |
| 693 |
if( 0 != (result = atmel_select_page( device, mem_page )) ) { |
| 694 |
DEBUG( "ERROR selecting 64kB page %d.\n", result ); |
| 695 |
retval = -3; |
| 696 |
} |
| 697 |
// check if the entire page is blank () |
| 698 |
} |
| 699 |
|
| 700 |
// find end value for the current transfer |
| 701 |
buin->info.block_end = buin->info.block_start + |
| 702 |
ATMEL_MAX_TRANSFER_SIZE - 1; |
| 703 |
if ( buin->info.block_end / ATMEL_64KB_PAGE > mem_page ) { |
| 704 |
buin->info.block_end = ATMEL_64KB_PAGE * mem_page - 1; |
| 705 |
} |
| 706 |
if ( buin->info.block_end > buin->info.data_end ) { |
| 707 |
buin->info.block_end = buin->info.data_end; |
| 708 |
} |
| 709 |
|
| 710 |
if( 0 != (result = __atmel_read_block(device, buin, |
| 711 |
mem_segment == mem_eeprom ? 1 : 0)) ) { |
| 712 |
DEBUG( "Error reading block 0x%X to 0x%X: err %d.\n", |
| 713 |
buin->info.block_start, buin->info.block_end, result ); |
| 714 |
retval = -5; |
| 715 |
goto finally; |
| 716 |
} |
| 717 |
|
| 718 |
buin->info.block_start = buin->info.block_end + 1; |
| 719 |
if ( !quiet ) __print_progress( &buin->info, &progress ); |
| 720 |
} |
| 721 |
retval = 0; |
| 722 |
|
| 723 |
finally: |
| 724 |
if ( !quiet ) { |
| 725 |
if( 0 == retval ) { |
| 726 |
if ( debug <= ATMEL_DEBUG_THRESHOLD && isatty(2) ) { |
| 727 |
fprintf( stderr, PROGRESS_END ); |
| 728 |
} |
| 729 |
fprintf( stderr, "Success\n" ); |
| 730 |
} else { |
| 731 |
if ( debug <= ATMEL_DEBUG_THRESHOLD && isatty(2) ) { |
| 732 |
fprintf( stderr, PROGRESS_ERROR ); |
| 733 |
} |
| 734 |
fprintf( stderr, "ERROR\n" ); |
| 735 |
if( retval==-3 ) |
| 736 |
fprintf( stderr, |
| 737 |
"Memory access error, use debug for more info.\n" ); |
| 738 |
else if( retval==-5 ) |
| 739 |
fprintf( stderr, |
| 740 |
"Memory read error, use debug for more info.\n" ); |
| 741 |
} |
| 742 |
} |
| 743 |
return retval; |
| 744 |
} |
| 745 |
|
| 746 |
static int32_t __atmel_blank_page_check( dfu_device_t *device, |
| 747 |
const uint32_t start, |
| 748 |
const uint32_t end ) { |
| 749 |
uint8_t command[6] = { 0x03, 0x01, 0x00, 0x00, 0x00, 0x00 }; |
| 750 |
dfu_status_t status; |
| 751 |
|
| 752 |
TRACE( "%s( %p, 0x%08x, 0x%08x )\n", __FUNCTION__, device, start, end ); |
| 753 |
|
| 754 |
if( (NULL == device) ) { |
| 755 |
DEBUG( "ERROR: Invalid arguments, device pointer is NULL.\n" ); |
| 756 |
return -1; |
| 757 |
} else if ( start > end ) { |
| 758 |
DEBUG( "ERROR: End address 0x%X before start address 0x%X.\n", |
| 759 |
end, start ); |
| 760 |
return -1; |
| 761 |
} else if ( end >= ATMEL_64KB_PAGE ) { |
| 762 |
DEBUG( "ERROR: Address out of 64kb (0x10000) byte page range.\n", |
| 763 |
end ); |
| 764 |
return -1; |
| 765 |
} |
| 766 |
|
| 767 |
command[2] = 0xff & (start >> 8); |
| 768 |
command[3] = 0xff & start; |
| 769 |
command[4] = 0xff & (end >> 8); |
| 770 |
command[5] = 0xff & end; |
| 771 |
|
| 772 |
if( 6 != dfu_download(device, 6, command) ) { |
| 773 |
DEBUG( "__atmel_blank_page_check DFU_DNLOAD failed.\n" ); |
| 774 |
return -2; |
| 775 |
} |
| 776 |
|
| 777 |
if( 0 != dfu_get_status(device, &status) ) { |
| 778 |
DEBUG( "__atmel_blank_page_check DFU_GETSTATUS failed.\n" ); |
| 779 |
return -3; |
| 780 |
} |
| 781 |
|
| 782 |
// check status and proceed accordingly |
| 783 |
if( DFU_STATUS_OK == status.bStatus ) { |
| 784 |
DEBUG( "Flash region from 0x%X to 0x%X is blank.\n", start, end ); |
| 785 |
} else if ( DFU_STATUS_ERROR_CHECK_ERASED == status.bStatus ) { |
| 786 |
// need to DFU upload to get the address |
| 787 |
DEBUG( "Region is NOT bank.\n" ); |
| 788 |
uint8_t addr[2] = { 0x00, 0x00 }; |
| 789 |
int32_t retval = 0; |
| 790 |
if ( 2 != dfu_upload(device, 2, addr) ) { |
| 791 |
DEBUG( "__atmel_blank_page_check DFU_UPLOAD failed.\n" ); |
| 792 |
return -4; |
| 793 |
} else { |
| 794 |
retval = (int32_t) ( ( ((uint16_t) addr[0]) << 8 ) + addr[1] ); |
| 795 |
DEBUG( " First non-blank address in region is 0x%X.\n", retval ); |
| 796 |
return retval + 1; |
| 797 |
} |
| 798 |
} else { |
| 799 |
DEBUG( "Error: status (%s) was not OK.\n", |
| 800 |
dfu_status_to_string(status.bStatus) ); |
| 801 |
if ( STATE_DFU_ERROR == status.bState ) { |
| 802 |
dfu_clear_status( device ); |
| 803 |
} |
| 804 |
return -4; |
| 805 |
} |
| 806 |
return 0; |
| 807 |
} |
| 808 |
|
| 809 |
int32_t atmel_blank_check( dfu_device_t *device, |
| 810 |
const uint32_t start, |
| 811 |
const uint32_t end, |
| 812 |
bool quiet ) { |
| 813 |
int32_t result; // blank_page_check_result |
| 814 |
uint32_t blank_upto = start; // up to is not inclusive |
| 815 |
uint32_t check_until; // end address of page check |
| 816 |
uint16_t current_page; // 64kb page number |
| 817 |
int32_t retval; |
| 818 |
|
| 819 |
TRACE( "%s( %p, 0x%08X, 0x%08X )\n", __FUNCTION__, device, start, end ); |
| 820 |
|
| 821 |
if( (NULL == device) ) { |
| 822 |
DEBUG( "ERROR: Invalid arguments, device pointer is NULL.\n" ); |
| 823 |
return -1; |
| 824 |
} else if ( start > end ) { |
| 825 |
DEBUG( "ERROR: End address 0x%X before start address 0x%X.\n", |
| 826 |
end, start ); |
| 827 |
return -1; |
| 828 |
} |
| 829 |
|
| 830 |
// safe to call this with any type of device |
| 831 |
if( 0 != atmel_select_memory_unit(device, mem_flash) ) { |
| 832 |
return -2; |
| 833 |
} |
| 834 |
|
| 835 |
if( !quiet ) { |
| 836 |
fprintf( stderr, "Checking memory from 0x%X to 0x%X... ", |
| 837 |
start, end ); |
| 838 |
// from here need to go to retval on error |
| 839 |
if( debug > ATMEL_DEBUG_THRESHOLD ) fprintf( stderr, "\n" ); |
| 840 |
} |
| 841 |
do { |
| 842 |
// want to have checks align with pages |
| 843 |
current_page = blank_upto / ATMEL_64KB_PAGE; |
| 844 |
check_until = ( current_page + 1 ) * ATMEL_64KB_PAGE - 1; |
| 845 |
check_until = check_until > end ? end : check_until; |
| 846 |
|
| 847 |
// safe to call with any type of device (just bc end address is |
| 848 |
// below 0x10000 doesn't mean you are definitely on page 0) |
| 849 |
if ( 0 != atmel_select_page(device, current_page) ) { |
| 850 |
DEBUG ("page select error.\n"); |
| 851 |
retval = -3; |
| 852 |
goto error; |
| 853 |
} |
| 854 |
|
| 855 |
// send the 'page' address, not absolute address |
| 856 |
result = __atmel_blank_page_check( device, |
| 857 |
blank_upto % ATMEL_64KB_PAGE, |
| 858 |
check_until % ATMEL_64KB_PAGE ); |
| 859 |
|
| 860 |
if ( result == 0 ) { |
| 861 |
DEBUG ( "Flash blank from 0x%X to 0x%X.\n", |
| 862 |
start, check_until ); |
| 863 |
blank_upto = check_until + 1; |
| 864 |
} else if ( result > 0 ) { |
| 865 |
blank_upto = result - 1 + ATMEL_64KB_PAGE * current_page; |
| 866 |
DEBUG ( "Flash NOT blank beginning at 0x%X.\n", blank_upto ); |
| 867 |
retval = blank_upto + 1; |
| 868 |
goto error; |
| 869 |
} else { |
| 870 |
DEBUG ( "Blank check fail err %d. Flash status unknown.\n", result ); |
| 871 |
retval = result; |
| 872 |
goto error; |
| 873 |
} |
| 874 |
} while ( blank_upto < end ); |
| 875 |
retval = 0; |
| 876 |
|
| 877 |
error: |
| 878 |
if( retval == 0 ) { |
| 879 |
if( !quiet ) fprintf( stderr, "Empty.\n" ); |
| 880 |
} else if ( retval > 0 ) { |
| 881 |
if( !quiet ) fprintf( stderr, "Not blank at 0x%X.\n", retval ); |
| 882 |
} else { |
| 883 |
if( !quiet ) fprintf( stderr, "ERROR.\n" ); |
| 884 |
} |
| 885 |
return retval; |
| 886 |
} |
| 887 |
|
| 888 |
int32_t atmel_start_app_reset( dfu_device_t *device ) { |
| 889 |
uint8_t command[3] = { 0x04, 0x03, 0x00 }; |
| 890 |
int32_t retval; |
| 891 |
|
| 892 |
TRACE( "%s( %p )\n", __FUNCTION__, device ); |
| 893 |
|
| 894 |
if( 3 != dfu_download(device, 3, command) ) { |
| 895 |
DEBUG( "dfu_download failed.\n" ); |
| 896 |
return -1; |
| 897 |
} |
| 898 |
|
| 899 |
if( 0 != (retval = dfu_download(device, 0, NULL)) ) { |
| 900 |
DEBUG( "dfu_download failed (rv=%d).\n", retval ); |
| 901 |
return -2; |
| 902 |
} |
| 903 |
|
| 904 |
return 0; |
| 905 |
} |
| 906 |
|
| 907 |
int32_t atmel_start_app_noreset( dfu_device_t *device ) { |
| 908 |
uint8_t command[5] = { 0x04, 0x03, 0x01, 0x00, 0x00 }; |
| 909 |
|
| 910 |
TRACE( "%s( %p )\n", __FUNCTION__, device ); |
| 911 |
|
| 912 |
if( 5 != dfu_download(device, 5, command) ) { |
| 913 |
DEBUG( "dfu_download failed.\n" ); |
| 914 |
return -1; |
| 915 |
} |
| 916 |
|
| 917 |
if( 0 != dfu_download(device, 0, NULL) ) { |
| 918 |
DEBUG( "dfu_download failed.\n" ); |
| 919 |
return -2; |
| 920 |
} |
| 921 |
|
| 922 |
return 0; |
| 923 |
} |
| 924 |
|
| 925 |
static int32_t atmel_select_memory_unit( dfu_device_t *device, |
| 926 |
enum atmel_memory_unit_enum unit ) { |
| 927 |
TRACE( "%s( %p, %d )\n", __FUNCTION__, device, unit ); |
| 928 |
|
| 929 |
uint8_t command[4] = { 0x06, 0x03, 0x00, (0xFF & unit) }; |
| 930 |
dfu_status_t status; |
| 931 |
char *mem_names[] = { ATMEL_MEM_UNIT_NAMES }; |
| 932 |
|
| 933 |
// input parameter checks |
| 934 |
if( NULL == device) { |
| 935 |
DEBUG ( "ERROR: Device pointer is NULL.\n" ); |
| 936 |
return -1; |
| 937 |
} |
| 938 |
|
| 939 |
// check compatiblity with various devices |
| 940 |
if( !(GRP_AVR32 & device->type) ) { |
| 941 |
DEBUG( "Ignore Select Memory Unit for non GRP_AVR32 device.\n" ); |
| 942 |
return 0; |
| 943 |
} else if ( (ADC_AVR32 & device->type) && !( unit == mem_flash || |
| 944 |
unit == mem_security || |
| 945 |
unit == mem_config || |
| 946 |
unit == mem_boot || |
| 947 |
unit == mem_sig || |
| 948 |
unit == mem_user ) ) { |
| 949 |
DEBUG( "%d is not a valid memory unit for AVR32 devices.\n", unit ); |
| 950 |
fprintf( stderr, "Invalid Memory Unit Selection.\n" ); |
| 951 |
return -1; |
| 952 |
} else if ( unit > mem_extdf ) { |
| 953 |
DEBUG( "Valid Memory Units 0 to 0x%X, not 0x%X.\n", mem_extdf, unit ); |
| 954 |
fprintf( stderr, "Invalid Memory Unit Selection.\n" ); |
| 955 |
return -1; |
| 956 |
} |
| 957 |
|
| 958 |
// select memory unit below is OK bc unit < len(mem_names) |
| 959 |
DEBUG( "Selecting %s memory unit.\n", mem_names[unit] ); |
| 960 |
if( 4 != dfu_download(device, 4, command) ) { |
| 961 |
DEBUG( "atmel_select_memory_unit 0x%02X dfu_download failed.\n", unit ); |
| 962 |
return -2; |
| 963 |
} |
| 964 |
|
| 965 |
// check that memory section was selected |
| 966 |
if( 0 != dfu_get_status(device, &status) ) { |
| 967 |
DEBUG( "DFU_GETSTATUS failed after atmel_select_memory_unit.\n" ); |
| 968 |
return -3; |
| 969 |
} |
| 970 |
|
| 971 |
// if error, report and clear |
| 972 |
if( DFU_STATUS_OK != status.bStatus ) { |
| 973 |
DEBUG( "Error: status (%s) was not OK.\n", |
| 974 |
dfu_status_to_string(status.bStatus) ); |
| 975 |
if ( STATE_DFU_ERROR == status.bState ) { |
| 976 |
dfu_clear_status( device ); |
| 977 |
} |
| 978 |
return -4; |
| 979 |
} |
| 980 |
|
| 981 |
return 0; |
| 982 |
} |
| 983 |
|
| 984 |
static int32_t atmel_select_page( dfu_device_t *device, |
| 985 |
const uint16_t mem_page ) { |
| 986 |
TRACE( "%s( %p, %u )\n", __FUNCTION__, device, mem_page ); |
| 987 |
dfu_status_t status; |
| 988 |
|
| 989 |
if( NULL == device ) { |
| 990 |
DEBUG ( "ERROR: Device pointer is NULL.\n" ); |
| 991 |
return -2; |
| 992 |
} |
| 993 |
|
| 994 |
if ( ADC_8051 & device->type ) { |
| 995 |
DEBUG( "Select page not implemented for 8051 device, ignoring.\n" ); |
| 996 |
return 0; |
| 997 |
} |
| 998 |
|
| 999 |
DEBUG( "Selecting page %d, address 0x%X.\n", |
| 1000 |
mem_page, ATMEL_64KB_PAGE * mem_page ); |
| 1001 |
|
| 1002 |
if( GRP_AVR32 & device->type ) { |
| 1003 |
uint8_t command[5] = { 0x06, 0x03, 0x01, 0x00, 0x00 }; |
| 1004 |
command[3] = 0xff & (mem_page >> 8); |
| 1005 |
command[4] = 0xff & mem_page; |
| 1006 |
|
| 1007 |
if( 5 != dfu_download(device, 5, command) ) { |
| 1008 |
DEBUG( "atmel_select_page DFU_DNLOAD failed.\n" ); |
| 1009 |
return -1; |
| 1010 |
} |
| 1011 |
} else if( ADC_AVR == device->type ) { // AVR but not 8051 |
| 1012 |
uint8_t command[4] = { 0x06, 0x03, 0x00, 0x00 }; |
| 1013 |
command[3] = 0xff & mem_page; |
| 1014 |
|
| 1015 |
if( 4 != dfu_download(device, 4, command) ) { |
| 1016 |
DEBUG( "atmel_select_page DFU_DNLOAD failed.\n" ); |
| 1017 |
return -1; |
| 1018 |
} |
| 1019 |
} |
| 1020 |
|
| 1021 |
// check that page number was set |
| 1022 |
if( 0 != dfu_get_status(device, &status) ) { |
| 1023 |
DEBUG( "atmel_select_page DFU_GETSTATUS failed.\n" ); |
| 1024 |
return -3; |
| 1025 |
} |
| 1026 |
|
| 1027 |
// if error, report and clear |
| 1028 |
if( DFU_STATUS_OK != status.bStatus ) { |
| 1029 |
DEBUG( "Error: status (%s) was not OK.\n", |
| 1030 |
dfu_status_to_string(status.bStatus) ); |
| 1031 |
if ( STATE_DFU_ERROR == status.bState ) { |
| 1032 |
dfu_clear_status( device ); |
| 1033 |
} |
| 1034 |
return -4; |
| 1035 |
} |
| 1036 |
|
| 1037 |
return 0; |
| 1038 |
} |
| 1039 |
|
| 1040 |
int32_t atmel_user( dfu_device_t *device, intel_buffer_out_t *bout ) { |
| 1041 |
int32_t result = 0; |
| 1042 |
|
| 1043 |
TRACE( "%s( %p, %p )\n", __FUNCTION__, device, bout ); |
| 1044 |
|
| 1045 |
if( (NULL == bout) || (NULL == device ) ) { |
| 1046 |
DEBUG( "invalid arguments.\n" ); |
| 1047 |
return -1; |
| 1048 |
} |
| 1049 |
|
| 1050 |
/* Select USER page */ |
| 1051 |
if( 0 != atmel_select_memory_unit(device, mem_user) ) { |
| 1052 |
DEBUG( "User Page Memory NOT selected.\n" ); |
| 1053 |
return -2; |
| 1054 |
} else { |
| 1055 |
DEBUG( "User Page memory selected.\n" ); |
| 1056 |
} |
| 1057 |
|
| 1058 |
bout->info.block_start = 0; |
| 1059 |
bout->info.block_end = bout->info.page_size - 1; |
| 1060 |
|
| 1061 |
//The user block is one flash page, so we'll just do it all in a block. |
| 1062 |
result = __atmel_flash_block( device, bout, false ); |
| 1063 |
|
| 1064 |
if( result != 0 ) { |
| 1065 |
DEBUG( "error flashing the block: %d\n", result ); |
| 1066 |
return -4; |
| 1067 |
} |
| 1068 |
|
| 1069 |
return 0; |
| 1070 |
} |
| 1071 |
|
| 1072 |
int32_t atmel_secure( dfu_device_t *device ) { |
| 1073 |
int32_t result = 0; |
| 1074 |
uint16_t buffer[1]; |
| 1075 |
intel_buffer_out_t bout; |
| 1076 |
TRACE( "%s( %p )\n", __FUNCTION__, device ); |
| 1077 |
|
| 1078 |
/* Select SECURITY page */ |
| 1079 |
uint8_t command[4] = { 0x06, 0x03, 0x00, 0x02 }; |
| 1080 |
if( 4 != dfu_download(device, 4, command) ) { |
| 1081 |
DEBUG( "dfu_download failed.\n" ); |
| 1082 |
return -2; |
| 1083 |
} |
| 1084 |
|
| 1085 |
bout.info.block_start = 0; |
| 1086 |
bout.info.block_end = 0; |
| 1087 |
bout.data = buffer; |
| 1088 |
|
| 1089 |
// The security block is a single byte, so we'll just do it all in a block. |
| 1090 |
buffer[0] = 0x01; // Non-zero to set security fuse. |
| 1091 |
result = __atmel_flash_block( device, &bout, false ); |
| 1092 |
|
| 1093 |
if( result != 0 ) { |
| 1094 |
DEBUG( "error flashing security fuse: %d\n", result ); |
| 1095 |
return -4; |
| 1096 |
} |
| 1097 |
|
| 1098 |
return 0; |
| 1099 |
} |
| 1100 |
|
| 1101 |
int32_t atmel_getsecure( dfu_device_t *device ) { |
| 1102 |
int32_t result = 0; |
| 1103 |
uint8_t buffer[1]; |
| 1104 |
TRACE( "%s( %p )\n", __FUNCTION__, device ); |
| 1105 |
|
| 1106 |
intel_buffer_in_t buin; |
| 1107 |
|
| 1108 |
// init the necessary parts of buin |
| 1109 |
buin.info.block_start = 0; |
| 1110 |
buin.info.block_end = 0; |
| 1111 |
buin.data = buffer; |
| 1112 |
|
| 1113 |
dfu_clear_status( device ); |
| 1114 |
|
| 1115 |
// TODO : Probably should use select_memory_unit command here |
| 1116 |
/* Select SECURITY page */ |
| 1117 |
uint8_t command[4] = { 0x06, 0x03, 0x00, 0x02 }; |
| 1118 |
result = dfu_download(device, 4, command); |
| 1119 |
if( 4 != result ) { |
| 1120 |
if( -EIO == result ) { |
| 1121 |
/* This also happens on most access attempts |
| 1122 |
* when the security bit is set. It may be a bug |
| 1123 |
* in the bootloader itself. |
| 1124 |
*/ |
| 1125 |
return ATMEL_SECURE_MAYBE; |
| 1126 |
} else { |
| 1127 |
DEBUG( "dfu_download failed.\n" ); |
| 1128 |
return -1; |
| 1129 |
} |
| 1130 |
} |
| 1131 |
|
| 1132 |
// The security block is a single byte, so we'll just do it all in a block. |
| 1133 |
if( 0 != __atmel_read_block(device, &buin, false) ) { |
| 1134 |
return -2; |
| 1135 |
} |
| 1136 |
|
| 1137 |
return( (0 == buffer[0]) ? ATMEL_SECURE_OFF : ATMEL_SECURE_ON ); |
| 1138 |
} |
| 1139 |
|
| 1140 |
int32_t atmel_flash( dfu_device_t *device, |
| 1141 |
intel_buffer_out_t *bout, |
| 1142 |
const bool eeprom, |
| 1143 |
const bool force, |
| 1144 |
const bool quiet ) { |
| 1145 |
uint32_t i; |
| 1146 |
uint32_t progress = 0; // keep record of sent progress as bytes * 32 |
| 1147 |
uint8_t mem_page = 0; // tracks the current memory page |
| 1148 |
int32_t result = 0; // result storage for many function calls |
| 1149 |
int32_t retval = -1; // the return value for this function |
| 1150 |
|
| 1151 |
TRACE( "%s( %p, %p, %s, %s )\n", __FUNCTION__, device, bout, |
| 1152 |
((true == eeprom) ? "true" : "false"), |
| 1153 |
((true == quiet) ? "true" : "false") ); |
| 1154 |
|
| 1155 |
// check arguments |
| 1156 |
if( (NULL == device) || (NULL == bout) ) { |
| 1157 |
DEBUG( "ERROR: Invalid arguments, device/buffer pointer is NULL.\n" ); |
| 1158 |
if( !quiet ) |
| 1159 |
fprintf( stderr, "Program Error, use debug for more info.\n" ); |
| 1160 |
return -1; |
| 1161 |
} else if ( bout->info.valid_start > bout->info.valid_end ) { |
| 1162 |
DEBUG( "ERROR: No valid target memory, end 0x%X before start 0x%X.\n", |
| 1163 |
bout->info.valid_end, bout->info.valid_start ); |
| 1164 |
if( !quiet ) |
| 1165 |
fprintf( stderr, "Program Error, use debug for more info.\n" ); |
| 1166 |
return -1; |
| 1167 |
} |
| 1168 |
|
| 1169 |
// for each page with data, fill unassigned values on the page with 0xFF |
| 1170 |
// bout->data[0] always aligns with a flash page boundary irrespective |
| 1171 |
// of where valid_start is located |
| 1172 |
if( 0 != intel_flash_prep_buffer( bout ) ) { |
| 1173 |
if( !quiet ) |
| 1174 |
fprintf( stderr, "Program Error, use debug for more info.\n" ); |
| 1175 |
return -2; |
| 1176 |
} |
| 1177 |
|
| 1178 |
// determine the limits of where actual data resides in the buffer |
| 1179 |
bout->info.data_start = UINT32_MAX; |
| 1180 |
for( i = 0; i < bout->info.total_size; i++ ) { |
| 1181 |
if( bout->data[i] <= UINT8_MAX ) { |
| 1182 |
bout->info.data_end = i; |
| 1183 |
if (bout->info.data_start == UINT32_MAX) |
| 1184 |
bout->info.data_start = i; |
| 1185 |
} |
| 1186 |
} |
| 1187 |
|
| 1188 |
// debug info about data limits |
| 1189 |
DEBUG("Flash available from 0x%X to 0x%X (64kB p. %u to %u), 0x%X bytes.\n", |
| 1190 |
bout->info.valid_start, bout->info.valid_end, |
| 1191 |
bout->info.valid_start / ATMEL_64KB_PAGE, |
| 1192 |
bout->info.valid_end / ATMEL_64KB_PAGE, |
| 1193 |
bout->info.valid_end - bout->info.valid_start + 1); // bytes inclusive so +1 |
| 1194 |
DEBUG("Data start @ 0x%X: 64kB p %u; %uB p 0x%X + 0x%X offset.\n", |
| 1195 |
bout->info.data_start, bout->info.data_start / ATMEL_64KB_PAGE, |
| 1196 |
bout->info.page_size, bout->info.data_start / bout->info.page_size, |
| 1197 |
bout->info.data_start % bout->info.page_size); |
| 1198 |
DEBUG("Data end @ 0x%X: 64kB p %u; %uB p 0x%X + 0x%X offset.\n", |
| 1199 |
bout->info.data_end, bout->info.data_end / ATMEL_64KB_PAGE, |
| 1200 |
bout->info.page_size, bout->info.data_end / bout->info.page_size, |
| 1201 |
bout->info.data_end % bout->info.page_size); |
| 1202 |
DEBUG("Totals: 0x%X bytes, %u %uB pages, %u 64kB byte pages.\n", |
| 1203 |
bout->info.data_end - bout->info.data_start + 1, |
| 1204 |
bout->info.data_end/bout->info.page_size - bout->info.data_start/bout->info.page_size + 1, |
| 1205 |
bout->info.page_size, |
| 1206 |
bout->info.data_end/ATMEL_64KB_PAGE - bout->info.data_start/ATMEL_64KB_PAGE + 1 ); |
| 1207 |
|
| 1208 |
// more error checking |
| 1209 |
if( (bout->info.data_start < bout->info.valid_start) || |
| 1210 |
(bout->info.data_end > bout->info.valid_end) ) { |
| 1211 |
DEBUG( "ERROR: Data exists outside of the valid target flash region.\n" ); |
| 1212 |
if( !quiet ) |
| 1213 |
fprintf( stderr, "Hex file error, use debug for more info.\n" ); |
| 1214 |
return -1; |
| 1215 |
} else if( bout->info.data_start == UINT32_MAX ) { |
| 1216 |
DEBUG( "ERROR: No valid data to flash.\n" ); |
| 1217 |
if( !quiet ) |
| 1218 |
fprintf( stderr, "Hex file error, use debug for more info.\n" ); |
| 1219 |
return -1; |
| 1220 |
} else if( !force && 0 != (result = atmel_blank_check(device, |
| 1221 |
bout->info.data_start, bout->info.data_end, quiet)) ) { |
| 1222 |
if ( !quiet ) |
| 1223 |
fprintf( stderr, |
| 1224 |
"The target memory for the program is not blank.\n" |
| 1225 |
"Use --force flag to override this error check.\n"); |
| 1226 |
DEBUG("The target memory is not blank.\n"); |
| 1227 |
return -1; |
| 1228 |
} |
| 1229 |
|
| 1230 |
// select eeprom/flash as the desired memory target, safe for non GRP_AVR32 |
| 1231 |
mem_page = eeprom ? mem_eeprom : mem_flash; |
| 1232 |
if( 0 != atmel_select_memory_unit(device, mem_page) ) { |
| 1233 |
DEBUG ("Error selecting memory unit.\n"); |
| 1234 |
if( !quiet ) |
| 1235 |
fprintf( stderr, "Memory access error, use debug for more info.\n" ); |
| 1236 |
return -2; |
| 1237 |
} |
| 1238 |
|
| 1239 |
if( !quiet ) { |
| 1240 |
if( debug <= ATMEL_DEBUG_THRESHOLD && isatty(2) ) { |
| 1241 |
// NOTE: from here on we need to run finally block |
| 1242 |
fprintf( stderr, PROGRESS_METER ); |
| 1243 |
} |
| 1244 |
fprintf( stderr, "Programming 0x%X bytes...\n", |
| 1245 |
bout->info.data_end - bout->info.data_start + 1 ); |
| 1246 |
if( debug <= ATMEL_DEBUG_THRESHOLD && isatty(2) ) { |
| 1247 |
// NOTE: from here on we need to run finally block |
| 1248 |
fprintf( stderr, PROGRESS_START ); |
| 1249 |
} |
| 1250 |
} |
| 1251 |
|
| 1252 |
// program the data |
| 1253 |
bout->info.block_start = bout->info.data_start; |
| 1254 |
mem_page = bout->info.block_start / ATMEL_64KB_PAGE; |
| 1255 |
if( 0 != (result = atmel_select_page( device, mem_page )) ) { |
| 1256 |
DEBUG( "ERROR selecting 64kB page %d.\n", result ); |
| 1257 |
retval = -3; |
| 1258 |
goto finally; |
| 1259 |
} |
| 1260 |
|
| 1261 |
while (bout->info.block_start <= bout->info.data_end) { |
| 1262 |
// select the memory page if needed (safe for non GRP_AVR32) |
| 1263 |
if ( bout->info.block_start / ATMEL_64KB_PAGE != mem_page ) { |
| 1264 |
mem_page = bout->info.block_start / ATMEL_64KB_PAGE; |
| 1265 |
if( 0 != (result = atmel_select_page( device, mem_page )) ) { |
| 1266 |
DEBUG( "ERROR selecting 64kB page %d.\n", result ); |
| 1267 |
retval = -3; |
| 1268 |
goto finally; |
| 1269 |
} |
| 1270 |
} |
| 1271 |
|
| 1272 |
// find end address (info.block_end) for data section to write |
| 1273 |
for(bout->info.block_end = bout->info.block_start; |
| 1274 |
bout->info.block_end <= bout->info.data_end; |
| 1275 |
bout->info.block_end++) { |
| 1276 |
// check if the current value is valid |
| 1277 |
if( bout->data[bout->info.block_end] > UINT8_MAX ) break; |
| 1278 |
// check if the current data packet is too big |
| 1279 |
if( (bout->info.block_end - bout->info.block_start + 1) > ATMEL_MAX_TRANSFER_SIZE ) break; |
| 1280 |
// check if the current data value is outside of the 64kB flash page |
| 1281 |
if( bout->info.block_end / ATMEL_64KB_PAGE - mem_page ) break; |
| 1282 |
} |
| 1283 |
bout->info.block_end--; // bout->info.block_end was one step beyond the last data value to flash |
| 1284 |
|
| 1285 |
// write the data |
| 1286 |
DEBUG("Program data block: 0x%X to 0x%X (p. %u), 0x%X bytes.\n", |
| 1287 |
bout->info.block_start, bout->info.block_end, |
| 1288 |
bout->info.block_end / ATMEL_64KB_PAGE, |
| 1289 |
bout->info.block_end - bout->info.block_start + 1); |
| 1290 |
result = __atmel_flash_block( device, bout, eeprom ); |
| 1291 |
if( 0 != result ) { |
| 1292 |
DEBUG( "Error flashing the block: err %d.\n", result ); |
| 1293 |
retval = -4; |
| 1294 |
goto finally; |
| 1295 |
} |
| 1296 |
|
| 1297 |
// increment bout->info.block_start to the next valid address |
| 1298 |
for(bout->info.block_start = bout->info.block_end + 1; |
| 1299 |
bout->info.block_start <= bout->info.data_end; |
| 1300 |
bout->info.block_start++) { |
| 1301 |
if( (bout->data[bout->info.block_start] <= UINT8_MAX) ) break; |
| 1302 |
} // bout->info.block_start is now on the first valid data for the next segment |
| 1303 |
|
| 1304 |
// display progress in 32 increments (if not hidden) |
| 1305 |
if ( !quiet ) __print_progress( &bout->info, &progress ); |
| 1306 |
} |
| 1307 |
retval = 0; |
| 1308 |
|
| 1309 |
finally: |
| 1310 |
if ( !quiet ) { |
| 1311 |
if( 0 == retval ) { |
| 1312 |
if ( debug <= ATMEL_DEBUG_THRESHOLD && isatty(2) ) { |
| 1313 |
fprintf( stderr, PROGRESS_END ); |
| 1314 |
} |
| 1315 |
fprintf( stderr, "Success\n" ); |
| 1316 |
} else { |
| 1317 |
if ( debug <= ATMEL_DEBUG_THRESHOLD && isatty(2) ) { |
| 1318 |
fprintf( stderr, PROGRESS_ERROR ); |
| 1319 |
} |
| 1320 |
fprintf( stderr, "ERROR\n" ); |
| 1321 |
if( retval==-3 ) |
| 1322 |
fprintf( stderr, |
| 1323 |
"Memory access error, use debug for more info.\n" ); |
| 1324 |
else if( retval==-4 ) |
| 1325 |
fprintf( stderr, |
| 1326 |
"Memory write error, use debug for more info.\n" ); |
| 1327 |
} |
| 1328 |
} |
| 1329 |
|
| 1330 |
return retval; |
| 1331 |
} |
| 1332 |
|
| 1333 |
static void atmel_flash_populate_footer( uint8_t *message, uint8_t *footer, |
| 1334 |
const uint16_t vendorId, |
| 1335 |
const uint16_t productId, |
| 1336 |
const uint16_t bcdFirmware ) { |
| 1337 |
int32_t crc; |
| 1338 |
|
| 1339 |
TRACE( "%s( %p, %p, %u, %u, %u )\n", __FUNCTION__, message, footer, |
| 1340 |
vendorId, productId, bcdFirmware ); |
| 1341 |
|
| 1342 |
if( (NULL == message) || (NULL == footer) ) { |
| 1343 |
return; |
| 1344 |
} |
| 1345 |
|
| 1346 |
/* TODO: Calculate the message CRC */ |
| 1347 |
crc = 0; |
| 1348 |
|
| 1349 |
/* CRC 4 bytes */ |
| 1350 |
footer[0] = 0xff & (crc >> 24); |
| 1351 |
footer[1] = 0xff & (crc >> 16); |
| 1352 |
footer[2] = 0xff & (crc >> 8); |
| 1353 |
footer[3] = 0xff & crc; |
| 1354 |
|
| 1355 |
/* Length of DFU suffix - always 16. */ |
| 1356 |
footer[4] = 16; |
| 1357 |
|
| 1358 |
/* ucdfuSignature - fixed 'DFU'. */ |
| 1359 |
footer[5] = 'D'; |
| 1360 |
footer[6] = 'F'; |
| 1361 |
footer[7] = 'U'; |
| 1362 |
|
| 1363 |
/* BCD DFU specification number (1.1)*/ |
| 1364 |
footer[8] = 0x01; |
| 1365 |
footer[9] = 0x10; |
| 1366 |
|
| 1367 |
/* Vendor ID or 0xFFFF */ |
| 1368 |
footer[10] = 0xff & (vendorId >> 8); |
| 1369 |
footer[11] = 0xff & vendorId; |
| 1370 |
|
| 1371 |
/* Product ID or 0xFFFF */ |
| 1372 |
footer[12] = 0xff & (productId >> 8); |
| 1373 |
footer[13] = 0xff & productId; |
| 1374 |
|
| 1375 |
/* BCD Firmware release number or 0xFFFF */ |
| 1376 |
footer[14] = 0xff & (bcdFirmware >> 8); |
| 1377 |
footer[15] = 0xff & bcdFirmware; |
| 1378 |
} |
| 1379 |
|
| 1380 |
static void atmel_flash_populate_header( uint8_t *header, |
| 1381 |
const uint32_t start, |
| 1382 |
const uint32_t end, |
| 1383 |
const bool eeprom ) { |
| 1384 |
|
| 1385 |
TRACE( "%s( %p, 0x%X, 0x%X, %s )\n", __FUNCTION__, header, start, |
| 1386 |
end, ((true == eeprom) ? "true" : "false") ); |
| 1387 |
|
| 1388 |
if( NULL == header ) { |
| 1389 |
return; |
| 1390 |
} |
| 1391 |
|
| 1392 |
/* Command Identifier */ |
| 1393 |
header[0] = 0x01; /* ld_prog_start */ |
| 1394 |
|
| 1395 |
/* data[0] */ |
| 1396 |
header[1] = ((true == eeprom) ? 0x01 : 0x00); |
| 1397 |
|
| 1398 |
/* start_address */ |
| 1399 |
header[2] = 0xff & (start >> 8); |
| 1400 |
header[3] = 0xff & start; |
| 1401 |
|
| 1402 |
/* end_address */ |
| 1403 |
header[4] = 0xff & (end >> 8); |
| 1404 |
header[5] = 0xff & end; |
| 1405 |
} |
| 1406 |
|
| 1407 |
static int32_t __atmel_flash_block( dfu_device_t *device, |
| 1408 |
intel_buffer_out_t *bout, |
| 1409 |
const bool eeprom ) { |
| 1410 |
// from doc7618, AT90 / ATmega app note protocol: |
| 1411 |
const size_t length = bout->info.block_end - bout->info.block_start + 1; |
| 1412 |
uint8_t message[ATMEL_MAX_FLASH_BUFFER_SIZE]; |
| 1413 |
uint8_t *header; |
| 1414 |
uint8_t *data; |
| 1415 |
uint8_t *footer; |
| 1416 |
size_t message_length; |
| 1417 |
int32_t result; |
| 1418 |
dfu_status_t status; |
| 1419 |
int32_t i; |
| 1420 |
size_t control_block_size; /* USB control block size */ |
| 1421 |
size_t alignment; |
| 1422 |
|
| 1423 |
TRACE( "%s( %p, %p, %s )\n", __FUNCTION__, device, bout, |
| 1424 |
((true == eeprom) ? "true" : "false") ); |
| 1425 |
|
| 1426 |
// check input args |
| 1427 |
if( (NULL == device) || (NULL == bout) ) { |
| 1428 |
DEBUG( "ERROR: Invalid arguments, device/buffer pointer is NULL.\n" ); |
| 1429 |
return -1; |
| 1430 |
} else if ( bout->info.block_start > bout->info.block_end ) { |
| 1431 |
DEBUG( "ERROR: End address 0x%X before start address 0x%X.\n", |
| 1432 |
bout->info.block_end, bout->info.block_start ); |
| 1433 |
return -1; |
| 1434 |
} else if ( length > ATMEL_MAX_TRANSFER_SIZE ) { |
| 1435 |
DEBUG( "ERROR: 0x%X byte message > MAX TRANSFER SIZE (0x%X).\n", |
| 1436 |
length, ATMEL_MAX_TRANSFER_SIZE ); |
| 1437 |
return -1; |
| 1438 |
} |
| 1439 |
|
| 1440 |
// 0 out the message |
| 1441 |
memset( message, 0, ATMEL_MAX_FLASH_BUFFER_SIZE ); |
| 1442 |
|
| 1443 |
if( GRP_AVR32 & device->type ) { |
| 1444 |
control_block_size = ATMEL_AVR32_CONTROL_BLOCK_SIZE; |
| 1445 |
alignment = bout->info.block_start % ATMEL_AVR32_CONTROL_BLOCK_SIZE; |
| 1446 |
} else { |
| 1447 |
control_block_size = ATMEL_CONTROL_BLOCK_SIZE; |
| 1448 |
alignment = 0; |
| 1449 |
} |
| 1450 |
|
| 1451 |
header = &message[0]; |
| 1452 |
data = &message[control_block_size + alignment]; |
| 1453 |
footer = &data[length]; |
| 1454 |
|
| 1455 |
atmel_flash_populate_header( header, |
| 1456 |
bout->info.block_start % ATMEL_64KB_PAGE, |
| 1457 |
bout->info.block_end % ATMEL_64KB_PAGE, |
| 1458 |
eeprom ); |
| 1459 |
/* for programming flash or eeprom for xmega or avr32, header[1] = 0x00 */ |
| 1460 |
/* for programming eeprom, memory was selected in atmel_flash */ |
| 1461 |
if( ADC_XMEGA & device->type ) { |
| 1462 |
header[1] = 0x00; |
| 1463 |
} |
| 1464 |
|
| 1465 |
// Copy the data |
| 1466 |
for( i = 0; i < length; i++ ) { |
| 1467 |
data[i] = (uint8_t) bout->data[bout->info.block_start + i]; |
| 1468 |
} |
| 1469 |
|
| 1470 |
atmel_flash_populate_footer( message, footer, 0xffff, 0xffff, 0xffff ); |
| 1471 |
|
| 1472 |
message_length = ((size_t) (footer - header)) + ATMEL_FOOTER_SIZE; |
| 1473 |
|
| 1474 |
result = dfu_download( device, message_length, message ); |
| 1475 |
|
| 1476 |
if( message_length != result ) { |
| 1477 |
if( -EPIPE == result ) { |
| 1478 |
/* The control pipe stalled - this is an error |
| 1479 |
* caused by the device saying "you can't do that" |
| 1480 |
* which means the device is write protected. |
| 1481 |
*/ |
| 1482 |
fprintf( stderr, "Device is write protected.\n" ); |
| 1483 |
dfu_clear_status( device ); |
| 1484 |
} else { |
| 1485 |
DEBUG( "atmel_flash: flash data dfu_download failed.\n" ); |
| 1486 |
DEBUG( "Expected message length of %d, got %d.\n", |
| 1487 |
message_length, result ); |
| 1488 |
} |
| 1489 |
return -2; |
| 1490 |
} |
| 1491 |
|
| 1492 |
// check status |
| 1493 |
if( 0 != dfu_get_status(device, &status) ) { |
| 1494 |
DEBUG( "dfu_get_status failed.\n" ); |
| 1495 |
return -3; |
| 1496 |
} |
| 1497 |
|
| 1498 |
if( DFU_STATUS_OK == status.bStatus ) { |
| 1499 |
DEBUG( "Page write success.\n" ); |
| 1500 |
} else { |
| 1501 |
DEBUG( "Page write not unsuccessful (err %s).\n", |
| 1502 |
dfu_status_to_string(status.bStatus) ); |
| 1503 |
if ( STATE_DFU_ERROR == status.bState ) { |
| 1504 |
dfu_clear_status( device ); |
| 1505 |
} |
| 1506 |
return (int32_t) status.bStatus; |
| 1507 |
} |
| 1508 |
return 0; |
| 1509 |
} |
| 1510 |
|
| 1511 |
void atmel_print_device_info( FILE *stream, atmel_device_info_t *info ) { |
| 1512 |
fprintf( stream, "%18s: 0x%04x - %d\n", "Bootloader Version", info->bootloaderVersion, info->bootloaderVersion ); |
| 1513 |
fprintf( stream, "%18s: 0x%04x - %d\n", "Device boot ID 1", info->bootID1, info->bootID1 ); |
| 1514 |
fprintf( stream, "%18s: 0x%04x - %d\n", "Device boot ID 2", info->bootID2, info->bootID2 ); |
| 1515 |
|
| 1516 |
if( /* device is 8051 based */ 0 ) { |
| 1517 |
fprintf( stream, "%18s: 0x%04x - %d\n", "Device BSB", info->bsb, info->bsb ); |
| 1518 |
fprintf( stream, "%18s: 0x%04x - %d\n", "Device SBV", info->sbv, info->sbv ); |
| 1519 |
fprintf( stream, "%18s: 0x%04x - %d\n", "Device SSB", info->ssb, info->ssb ); |
| 1520 |
fprintf( stream, "%18s: 0x%04x - %d\n", "Device EB", info->eb, info->eb ); |
| 1521 |
} |
| 1522 |
|
| 1523 |
fprintf( stream, "%18s: 0x%04x - %d\n", "Manufacturer Code", info->manufacturerCode, info->manufacturerCode ); |
| 1524 |
fprintf( stream, "%18s: 0x%04x - %d\n", "Family Code", info->familyCode, info->familyCode ); |
| 1525 |
fprintf( stream, "%18s: 0x%04x - %d\n", "Product Name", info->productName, info->productName ); |
| 1526 |
fprintf( stream, "%18s: 0x%04x - %d\n", "Product Revision", info->productRevision, info->productRevision ); |
| 1527 |
fprintf( stream, "%18s: 0x%04x - %d\n", "HWB", info->hsb, info->hsb ); |
| 1528 |
} |