| 1 |
/** dfu-programmer |
| 2 |
* |
| 3 |
* This program is free software; you can redistribute it and/or modify |
| 4 |
* it under the terms of the GNU General Public License as published by |
| 5 |
* the Free Software Foundation; either version 2 of the License, or |
| 6 |
* (at your option) any later version. |
| 7 |
* |
| 8 |
* This program is distributed in the hope that it will be useful, |
| 9 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 |
* GNU General Public License for more details. |
| 12 |
* |
| 13 |
* You should have received a copy of the GNU General Public License |
| 14 |
* along with this program; if not, write to the Free Software Foundation, |
| 15 |
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
| 16 |
*/ |
| 17 |
|
| 18 |
//___ I N C L U D E S ________________________________________________________ |
| 19 |
#include <stdio.h> |
| 20 |
#include <stdarg.h> |
| 21 |
#include <stdint.h> |
| 22 |
#include <string.h> |
| 23 |
#include <stddef.h> |
| 24 |
#include <errno.h> |
| 25 |
#include <stdbool.h> |
| 26 |
|
| 27 |
#include "dfu-device.h" |
| 28 |
#include "config.h" |
| 29 |
#include "arguments.h" |
| 30 |
#include "dfu.h" |
| 31 |
#include "stm32.h" |
| 32 |
#include "util.h" |
| 33 |
|
| 34 |
|
| 35 |
//___ M A C R O S ( P R I V A T E ) ________________________________________ |
| 36 |
#define STM32_DEBUG_THRESHOLD 50 |
| 37 |
#define STM32_TRACE_THRESHOLD 55 |
| 38 |
|
| 39 |
#define DEBUG(...) dfu_debug( __FILE__, __FUNCTION__, __LINE__, STM32_DEBUG_THRESHOLD, __VA_ARGS__ ) |
| 40 |
#define TRACE(...) dfu_debug( __FILE__, __FUNCTION__, __LINE__, STM32_TRACE_THRESHOLD, __VA_ARGS__ ) |
| 41 |
|
| 42 |
#define STM32_MAX_TRANSFER_SIZE 0x0800 /* 2048 */ |
| 43 |
#define STM32_MIN_SECTOR_BOUND 0x4000 /* 16 kb */ |
| 44 |
#define STM32_OTP_BYTES_SIZE 528 /* number of OTP bytes (0x210) */ |
| 45 |
#define STM32_OPTION_BYTES_SIZE 16 /* number option bytes */ |
| 46 |
|
| 47 |
#define SET_ADDR_PTR 0x21 |
| 48 |
#define ERASE_CMD 0x41 |
| 49 |
#define READ_UNPROTECT 0x92 |
| 50 |
#define GET_CMD 0x00 |
| 51 |
|
| 52 |
//___ T Y P E D E F S ( P R I V A T E ) ____________________________________ |
| 53 |
|
| 54 |
//___ P R O T O T Y P E S ( P R I V A T E ) ________________________________ |
| 55 |
static inline int32_t stm32_get_status( dfu_device_t *device ); |
| 56 |
/* run dfu_get_status to get the current status |
| 57 |
* return 0 on status OK, -1 on status req fail, -2 on bad status |
| 58 |
*/ |
| 59 |
|
| 60 |
static int32_t stm32_set_address_ptr( dfu_device_t *device, uint32_t address ); |
| 61 |
/* @brief set the address pointer to a certain address |
| 62 |
* @param the address to set |
| 63 |
* @return 0 on success, negative on failure |
| 64 |
*/ |
| 65 |
|
| 66 |
static int32_t stm32_write_block( dfu_device_t *device, |
| 67 |
size_t xfer_len, |
| 68 |
uint8_t *buffer ); |
| 69 |
/* flash the contents of memory into a block of memory. it is assumed that |
| 70 |
* the appropriate page has already been selected. start and end are the |
| 71 |
* start and end addresses of the flash data. returns 0 on success, |
| 72 |
* positive dfu error code if one is obtained, or negative if communication |
| 73 |
* with device fails. |
| 74 |
*/ |
| 75 |
|
| 76 |
static int32_t stm32_read_block( dfu_device_t *device, |
| 77 |
size_t xfer_len, |
| 78 |
uint8_t *buffer ); |
| 79 |
/* read a block of memory, assumes address pointer is already set |
| 80 |
*/ |
| 81 |
|
| 82 |
static inline void print_progress( intel_buffer_info_t *info, |
| 83 |
uint32_t *progress ); |
| 84 |
/* calculate how many progress indicator steps to print and print them |
| 85 |
* update progress value |
| 86 |
*/ |
| 87 |
|
| 88 |
static int32_t stm32_erase( dfu_device_t *device, uint8_t *command, |
| 89 |
uint8_t command_length, bool quiet ); |
| 90 |
/* erase, erase page, and read unprotect all share this functionality |
| 91 |
* although with different commands |
| 92 |
*/ |
| 93 |
|
| 94 |
|
| 95 |
//___ V A R I A B L E S ______________________________________________________ |
| 96 |
extern int debug; /* defined in libdfu.c */ |
| 97 |
|
| 98 |
/* FIXME : these should be read from usb device descriptor because they are |
| 99 |
* device specific */ |
| 100 |
static const uint32_t stm32_sector_addresses[] = { |
| 101 |
0x08000000, /* sector 0, 16 kb */ |
| 102 |
0x08004000, /* sector 1, 16 kb */ |
| 103 |
0x08008000, /* sector 2, 16 kb */ |
| 104 |
0x0800C000, /* sector 3, 16 kb */ |
| 105 |
0x08010000, /* sector 4, 64 kb */ |
| 106 |
0x08020000, /* sector 5, 128 kb */ |
| 107 |
0x08040000, /* sector 6, 128 kb */ |
| 108 |
0x08060000, /* sector 7, 128 kb */ |
| 109 |
0x08080000, /* sector 8, 128 kb */ |
| 110 |
0x080A0000, /* sector 9, 128 kb */ |
| 111 |
0x080C0000, /* sector 10, 128 kb */ |
| 112 |
0x080E0000, /* sector 11, 128 kb */ |
| 113 |
0x1FFF0000, /* system memory, 30 kb */ |
| 114 |
0x1FFF7800, /* OTP area, 528 bytes */ |
| 115 |
0x1FFFC000, /* Option bytes, 16 bytes */ |
| 116 |
}; |
| 117 |
|
| 118 |
//___ F U N C T I O N S ( P R I V A T E ) __________________________________ |
| 119 |
static inline int32_t stm32_get_status( dfu_device_t *device ) { |
| 120 |
dfu_status_t status; |
| 121 |
if( 0 == dfu_get_status(device, &status) ) { |
| 122 |
if( status.bStatus == DFU_STATUS_OK ) { |
| 123 |
DEBUG( "Status OK\n" ); |
| 124 |
} else { |
| 125 |
DEBUG( "Status %s not OK, use DFU_CLRSTATUS\n", |
| 126 |
dfu_status_to_string(status.bStatus) ); |
| 127 |
dfu_clear_status( device ); |
| 128 |
return -2; |
| 129 |
} |
| 130 |
} else { |
| 131 |
DEBUG( "DFU_GETSTATUS request failed\n" ); |
| 132 |
return -1; |
| 133 |
} |
| 134 |
|
| 135 |
return 0; |
| 136 |
} |
| 137 |
|
| 138 |
static int32_t stm32_set_address_ptr( dfu_device_t *device, uint32_t address ) { |
| 139 |
TRACE( "%s( 0x%X )\n", __FUNCTION__, address ); |
| 140 |
const uint8_t length = 5; |
| 141 |
int32_t status; |
| 142 |
|
| 143 |
uint8_t command[] = { |
| 144 |
(uint8_t) SET_ADDR_PTR, |
| 145 |
(uint8_t) address & 0xFF, /* address LSB */ |
| 146 |
(uint8_t) (address>>8) & 0xFF, |
| 147 |
(uint8_t) (address>>16) & 0xFF, |
| 148 |
(uint8_t) (address>>24) & 0xFF /* address MSB */ |
| 149 |
}; |
| 150 |
|
| 151 |
/* check dfu status for okay to send */ |
| 152 |
if( (status = stm32_get_status(device)) ) { |
| 153 |
DEBUG("Error %d getting status on start\n", status); |
| 154 |
return -1; |
| 155 |
} |
| 156 |
|
| 157 |
dfu_set_transaction_num( device, 0 ); /* set wValue to zero */ |
| 158 |
if( length != dfu_download(device, length, command) ) { |
| 159 |
DEBUG( "dfu_download failed\n" ); |
| 160 |
return -2; |
| 161 |
} |
| 162 |
|
| 163 |
/* call dfu get status to trigger command */ |
| 164 |
if( (status = stm32_get_status(device)) ) { |
| 165 |
DEBUG("Error %d triggering %s\n", status, __FUNCTION__); |
| 166 |
return -3; |
| 167 |
} |
| 168 |
|
| 169 |
/* check command success */ |
| 170 |
if( (status = stm32_get_status(device)) ) { |
| 171 |
DEBUG("Error %d: %s unsuccessful\n", status, __FUNCTION__); |
| 172 |
return -4; |
| 173 |
} |
| 174 |
|
| 175 |
return 0; |
| 176 |
} |
| 177 |
|
| 178 |
static int32_t stm32_write_block( dfu_device_t *device, |
| 179 |
size_t xfer_len, |
| 180 |
uint8_t *buffer ) { |
| 181 |
TRACE( "%s( %p, %u, %p )\n", __FUNCTION__, device, xfer_len, buffer ); |
| 182 |
int32_t status; |
| 183 |
|
| 184 |
/* check input args */ |
| 185 |
if( (NULL == device) || (NULL == buffer) ) { |
| 186 |
DEBUG( "ERROR: Invalid arguments, device/buffer pointer is NULL.\n" ); |
| 187 |
return -1; |
| 188 |
} else if ( xfer_len > STM32_MAX_TRANSFER_SIZE ) { |
| 189 |
DEBUG( "ERROR: 0x%X byte message > MAX TRANSFER SIZE (0x%X).\n", |
| 190 |
xfer_len, STM32_MAX_TRANSFER_SIZE ); |
| 191 |
return -1; |
| 192 |
} else if ( xfer_len < 1 ) { |
| 193 |
DEBUG( "ERROR: xfer_len is %u\n", xfer_len ); |
| 194 |
return -1; |
| 195 |
} |
| 196 |
|
| 197 |
if( xfer_len != dfu_download(device, xfer_len, buffer) ) { |
| 198 |
DEBUG( "dfu_download failed\n" ); |
| 199 |
return -3; |
| 200 |
} |
| 201 |
|
| 202 |
/* call dfu get status to trigger command */ |
| 203 |
if( (status = stm32_get_status(device)) ) { |
| 204 |
DEBUG("Error %d triggering %s\n", status, __FUNCTION__); |
| 205 |
return -3; |
| 206 |
} |
| 207 |
|
| 208 |
/* check that the command was successfully executed */ |
| 209 |
if( (status = stm32_get_status(device)) ) { |
| 210 |
DEBUG("Error %d: %s unsuccessful\n", status, __FUNCTION__); |
| 211 |
return -4; |
| 212 |
} |
| 213 |
|
| 214 |
return 0; |
| 215 |
} |
| 216 |
|
| 217 |
static int32_t stm32_read_block( dfu_device_t *device, |
| 218 |
size_t xfer_len, |
| 219 |
uint8_t *buffer ) { |
| 220 |
TRACE( "%s( %p, %u, %p )\n", __FUNCTION__, device, xfer_len, buffer ); |
| 221 |
int32_t result; |
| 222 |
|
| 223 |
if( buffer == NULL ) { |
| 224 |
DEBUG("ERROR: buffer ptr is NULL\n"); |
| 225 |
return -1; |
| 226 |
} else if( xfer_len > STM32_MAX_TRANSFER_SIZE ) { |
| 227 |
/* this could cause a read problem */ |
| 228 |
DEBUG("ERROR: transfer size %d exceeds max %d.\n", |
| 229 |
xfer_len, STM32_MAX_TRANSFER_SIZE ); |
| 230 |
return -1; |
| 231 |
} |
| 232 |
|
| 233 |
/* check status before read */ |
| 234 |
if( (result = stm32_get_status(device)) ) { |
| 235 |
DEBUG("Status Error %d before read\n", result ); |
| 236 |
return -2; |
| 237 |
} |
| 238 |
|
| 239 |
result = dfu_upload( device, xfer_len, buffer ); |
| 240 |
if( result < 0) { |
| 241 |
dfu_status_t status; |
| 242 |
|
| 243 |
DEBUG( "ERROR: dfu_upload result: %d\n", result ); |
| 244 |
if( 0 == dfu_get_status(device, &status) ) { |
| 245 |
DEBUG( "Error Status %s, state %s\n", |
| 246 |
dfu_status_to_string(status.bStatus), |
| 247 |
dfu_state_to_string(status.bState) ); |
| 248 |
if( status.bStatus == DFU_STATUS_ERROR_VENDOR ) { |
| 249 |
/* status = dfuERROR and state = errVENDOR */ |
| 250 |
DEBUG("Device is read protected\n"); |
| 251 |
return STM32_READ_PROT_ERROR; |
| 252 |
} |
| 253 |
} else { |
| 254 |
DEBUG("DFU GET_STATUS fail\n"); |
| 255 |
} |
| 256 |
dfu_clear_status( device ); |
| 257 |
|
| 258 |
return result; |
| 259 |
} |
| 260 |
|
| 261 |
return 0; |
| 262 |
} |
| 263 |
|
| 264 |
static inline void print_progress( intel_buffer_info_t *info, |
| 265 |
uint32_t *progress ) { |
| 266 |
if ( !(debug > STM32_DEBUG_THRESHOLD) ) { |
| 267 |
while ( ((info->block_end - info->data_start + 1) * 32) > *progress ) { |
| 268 |
fprintf( stderr, ">" ); |
| 269 |
*progress += info->data_end - info->data_start + 1; |
| 270 |
} |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
static int32_t stm32_erase( dfu_device_t *device, uint8_t *command, |
| 275 |
uint8_t command_length, bool quiet ) { |
| 276 |
int32_t status; |
| 277 |
dfu_set_transaction_num( device, 0 ); /* set wValue to zero */ |
| 278 |
if( command_length != dfu_download(device, command_length, command) ) { |
| 279 |
if( !quiet ) fprintf( stderr, "ERROR\n" ); |
| 280 |
DEBUG( "dfu_download failed\n" ); |
| 281 |
return UNSPECIFIED_ERROR; |
| 282 |
} |
| 283 |
|
| 284 |
/* call dfu get status to trigger command */ |
| 285 |
if( (status = stm32_get_status(device)) ) { |
| 286 |
if( !quiet ) fprintf( stderr, "ERROR\n" ); |
| 287 |
DEBUG("Error %d triggering %s\n", status, __FUNCTION__); |
| 288 |
return UNSPECIFIED_ERROR; |
| 289 |
} |
| 290 |
|
| 291 |
/* check status again for erase status, this can take a while */ |
| 292 |
if( (status = stm32_get_status(device)) ) { |
| 293 |
DEBUG("Error %d: %s unsuccessful\n", status, __FUNCTION__); |
| 294 |
if( !quiet ) fprintf( stderr, "ERROR\n" ); |
| 295 |
return UNSPECIFIED_ERROR; |
| 296 |
} else { |
| 297 |
if( !quiet ) fprintf( stderr, "DONE\n" ); |
| 298 |
} |
| 299 |
|
| 300 |
return SUCCESS; |
| 301 |
} |
| 302 |
|
| 303 |
//___ F U N C T I O N S ______________________________________________________ |
| 304 |
int32_t stm32_erase_flash( dfu_device_t *device, bool quiet ) { |
| 305 |
TRACE( "%s( %p, %s )\n", __FUNCTION__, device, quiet ? "true" : "false" ); |
| 306 |
uint8_t command[] = { ERASE_CMD }; |
| 307 |
uint8_t length = 1; |
| 308 |
|
| 309 |
if( !quiet ) { |
| 310 |
fprintf( stderr, "Erasing flash... " ); |
| 311 |
DEBUG("\n"); |
| 312 |
} |
| 313 |
|
| 314 |
return stm32_erase( device, command, length, quiet ); |
| 315 |
} |
| 316 |
|
| 317 |
int32_t stm32_page_erase( dfu_device_t *device, uint32_t address, |
| 318 |
bool quiet ) { |
| 319 |
TRACE( "%s( %p, 0x%X, %s )\n", __FUNCTION__, device, address, |
| 320 |
quiet ? "true" : "false" ); |
| 321 |
uint8_t length = 5; |
| 322 |
|
| 323 |
uint8_t command[5] = { |
| 324 |
ERASE_CMD, |
| 325 |
(uint8_t) (0xff & address), // page LSB |
| 326 |
(uint8_t) (0xff & ((address)>>8)), |
| 327 |
(uint8_t) (0xff & ((address)>>16)), |
| 328 |
(uint8_t) (0xff & ((address)>>24)) // page MSB |
| 329 |
}; |
| 330 |
|
| 331 |
return stm32_erase( device, command, length, quiet ); |
| 332 |
} |
| 333 |
|
| 334 |
int32_t stm32_start_app( dfu_device_t *device, bool quiet ) { |
| 335 |
TRACE( "%s( %p )\n", __FUNCTION__, device ); |
| 336 |
int32_t status; |
| 337 |
|
| 338 |
/* set address pointer (jump target) to start address */ |
| 339 |
if( (status = stm32_set_address_ptr( device, STM32_FLASH_OFFSET )) ) { |
| 340 |
DEBUG("Error setting address pointer\n"); |
| 341 |
return UNSPECIFIED_ERROR; |
| 342 |
} |
| 343 |
|
| 344 |
/* check dfu status for ok to send */ |
| 345 |
if( (status = stm32_get_status(device)) ) { |
| 346 |
DEBUG("Error %d getting status on start\n", status); |
| 347 |
return UNSPECIFIED_ERROR; |
| 348 |
} |
| 349 |
|
| 350 |
if( !quiet ) fprintf( stderr, "Launching program... \n" ); |
| 351 |
dfu_set_transaction_num( device, 0 ); /* set wValue to zero */ |
| 352 |
if( 0 != dfu_download(device, 0, NULL) ) { |
| 353 |
if( !quiet ) fprintf( stderr, "ERROR\n" ); |
| 354 |
DEBUG( "dfu_download failed\n" ); |
| 355 |
return UNSPECIFIED_ERROR; |
| 356 |
} |
| 357 |
|
| 358 |
/* call dfu get status to trigger command */ |
| 359 |
if( (status = stm32_get_status(device)) ) { |
| 360 |
DEBUG("Error %d triggering %s\n", status, __FUNCTION__); |
| 361 |
return UNSPECIFIED_ERROR; |
| 362 |
} |
| 363 |
|
| 364 |
return SUCCESS; |
| 365 |
} |
| 366 |
|
| 367 |
int32_t stm32_read_flash( dfu_device_t *device, intel_buffer_in_t *buin, |
| 368 |
const uint8_t mem_segment, const bool quiet ) { |
| 369 |
TRACE( "%s( %p, %p, %u, %s )\n", __FUNCTION__, device, buin, |
| 370 |
mem_segment, ((true == quiet) ? "true" : "false")); |
| 371 |
|
| 372 |
uint8_t reset_address_flag; // reset address offset required |
| 373 |
uint32_t address_offset; // keep record of sent progress as bytes * 32 |
| 374 |
uint16_t xfer_size = 0; // the size of a transfer |
| 375 |
uint8_t mem_section = 0; // tracks the current memory page |
| 376 |
uint32_t progress = 0; // used to indicate progress |
| 377 |
int32_t status; |
| 378 |
int32_t retval = UNSPECIFIED_ERROR; // the return value for this function |
| 379 |
|
| 380 |
if( (NULL == buin) || (NULL == device) ) { |
| 381 |
DEBUG( "invalid arguments.\n" ); |
| 382 |
if( !quiet ) |
| 383 |
fprintf( stderr, "Program Error, use debug for more info.\n" ); |
| 384 |
return ARGUMENT_ERROR; |
| 385 |
} |
| 386 |
|
| 387 |
if( !quiet ) { |
| 388 |
if( debug <= STM32_DEBUG_THRESHOLD ) { |
| 389 |
/* NOTE: From here on we should go to finally on error */ |
| 390 |
fprintf( stderr, "[================================] " ); |
| 391 |
} |
| 392 |
fprintf( stderr, "Reading 0x%X bytes...\n", |
| 393 |
buin->info.data_end - buin->info.data_start + 1 ); |
| 394 |
if( debug <= STM32_DEBUG_THRESHOLD ) { |
| 395 |
/* NOTE: From here on we should go to finally on error */ |
| 396 |
fprintf( stderr, "[" ); |
| 397 |
} |
| 398 |
} |
| 399 |
|
| 400 |
/* read the data */ |
| 401 |
buin->info.block_start = buin->info.data_start; |
| 402 |
reset_address_flag = 0; |
| 403 |
address_offset = buin->info.block_start; |
| 404 |
|
| 405 |
while( buin->info.block_start <= buin->info.data_end ) { |
| 406 |
if( reset_address_flag ) { |
| 407 |
address_offset = buin->info.block_start; |
| 408 |
if( (status = stm32_set_address_ptr(device, |
| 409 |
STM32_FLASH_OFFSET + address_offset)) ) { |
| 410 |
DEBUG("Error setting address 0x%X\n", address_offset); |
| 411 |
retval = UNSPECIFIED_ERROR; |
| 412 |
goto finally; |
| 413 |
} |
| 414 |
dfu_set_transaction_num( device, 2 ); /* sets block offset 0 */ |
| 415 |
reset_address_flag = 0; |
| 416 |
} |
| 417 |
|
| 418 |
// find end value for the current transfer |
| 419 |
buin->info.block_end = buin->info.block_start + STM32_MAX_TRANSFER_SIZE - 1; |
| 420 |
mem_section = buin->info.block_start / STM32_MIN_SECTOR_BOUND; |
| 421 |
if( buin->info.block_end / STM32_MIN_SECTOR_BOUND > mem_section ) { |
| 422 |
buin->info.block_end = STM32_MIN_SECTOR_BOUND * mem_section - 1; |
| 423 |
} |
| 424 |
if( buin->info.block_end > buin->info.data_end ) { |
| 425 |
buin->info.block_end = buin->info.data_end; |
| 426 |
} |
| 427 |
xfer_size = buin->info.block_end - buin->info.block_start + 1; |
| 428 |
if( xfer_size != STM32_MAX_TRANSFER_SIZE ) { |
| 429 |
DEBUG("xfer_size change, need addr reset\n"); |
| 430 |
reset_address_flag = 1; |
| 431 |
} |
| 432 |
|
| 433 |
if( (status = stm32_read_block( device, xfer_size, |
| 434 |
&buin->data[buin->info.block_start] )) ) { |
| 435 |
DEBUG( "Error reading block 0x%X to 0x%X: err %d.\n", |
| 436 |
buin->info.block_start, buin->info.block_end, status ); |
| 437 |
retval = ( status == -10 ) ? DEVICE_ACCESS_ERROR : FLASH_READ_ERROR; |
| 438 |
/* read protect error code in read_block is -10 */ |
| 439 |
goto finally; |
| 440 |
} |
| 441 |
|
| 442 |
buin->info.block_start = buin->info.block_end + 1; |
| 443 |
if( reset_address_flag == 0 && (buin->info.block_start != |
| 444 |
(STM32_MAX_TRANSFER_SIZE * (dfu_get_transaction_num( device ) - 2)) |
| 445 |
+ address_offset) ) { |
| 446 |
DEBUG("block start & address mismatch, reset req\n"); |
| 447 |
reset_address_flag = 1; |
| 448 |
} |
| 449 |
|
| 450 |
if( !quiet ) print_progress( &buin->info, &progress ); |
| 451 |
} |
| 452 |
retval = SUCCESS; |
| 453 |
|
| 454 |
finally: |
| 455 |
if ( !quiet ) { |
| 456 |
if( SUCCESS == retval ) { |
| 457 |
if ( debug <= STM32_DEBUG_THRESHOLD ) { |
| 458 |
fprintf( stderr, "] " ); |
| 459 |
} |
| 460 |
fprintf( stderr, "SUCCESS\n" ); |
| 461 |
} else { |
| 462 |
if ( debug <= STM32_DEBUG_THRESHOLD ) { |
| 463 |
fprintf( stderr, " X "); |
| 464 |
} |
| 465 |
fprintf( stderr, "ERROR\n" ); |
| 466 |
if( retval==DEVICE_ACCESS_ERROR ) |
| 467 |
fprintf( stderr, |
| 468 |
"Memory access error, use debug for more info.\n" ); |
| 469 |
else if( retval==FLASH_READ_ERROR ) |
| 470 |
fprintf( stderr, |
| 471 |
"Memory read error, use debug for more info.\n" ); |
| 472 |
} |
| 473 |
} |
| 474 |
return retval; |
| 475 |
} |
| 476 |
|
| 477 |
int32_t stm32_write_flash( dfu_device_t *device, intel_buffer_out_t *bout, |
| 478 |
const bool eeprom, const bool force, const bool quiet ) { |
| 479 |
TRACE( "%s( %p, %p, %s, %s )\n", __FUNCTION__, device, bout, |
| 480 |
((true == eeprom) ? "true" : "false"), |
| 481 |
((true == quiet) ? "true" : "false") ); |
| 482 |
|
| 483 |
uint32_t i; |
| 484 |
uint32_t progress = 0; // keep record of sent progress as bytes * 32 |
| 485 |
uint32_t address_offset; // keep record of sent progress as bytes * 32 |
| 486 |
uint8_t reset_address_flag; // reset address offset required |
| 487 |
uint16_t xfer_size = 0; // the size of a transfer |
| 488 |
uint8_t mem_section = 0; // tracks the current memory page |
| 489 |
int32_t retval = UNSPECIFIED_ERROR; // the return value for this function |
| 490 |
uint8_t buffer[STM32_MAX_TRANSFER_SIZE]; // buffer holding out data |
| 491 |
int32_t status; |
| 492 |
|
| 493 |
/* check arguments */ |
| 494 |
if( (NULL == device) || (NULL == bout) ) { |
| 495 |
DEBUG( "ERROR: Invalid arguments, device/buffer pointer is NULL.\n" ); |
| 496 |
if( !quiet ) |
| 497 |
fprintf( stderr, "Program Error, use debug for more info.\n" ); |
| 498 |
return ARGUMENT_ERROR; |
| 499 |
} else if( bout->info.valid_start > bout->info.valid_end ) { |
| 500 |
DEBUG( "ERROR: No valid target memory, end 0x%X before start 0x%X.\n", |
| 501 |
bout->info.valid_end, bout->info.valid_start ); |
| 502 |
if( !quiet ) |
| 503 |
fprintf( stderr, "Program Error, use debug for more info.\n" ); |
| 504 |
return BUFFER_INIT_ERROR; |
| 505 |
} |
| 506 |
|
| 507 |
/* for each page with data, fill unassigned values on the page with 0xFF |
| 508 |
* bout->data[0] always aligns with a flash page boundary irrespective |
| 509 |
* of where valid_start is located */ |
| 510 |
if( 0 != intel_flash_prep_buffer( bout ) ) { |
| 511 |
if( !quiet ) |
| 512 |
fprintf( stderr, "Program Error, use debug for more info.\n" ); |
| 513 |
return BUFFER_INIT_ERROR; |
| 514 |
} |
| 515 |
|
| 516 |
/* determine the limits of where actual data resides in the buffer */ |
| 517 |
bout->info.data_start = UINT32_MAX; |
| 518 |
for( i = 0; i < bout->info.total_size; i++ ) { |
| 519 |
if( bout->data[i] <= UINT8_MAX ) { |
| 520 |
bout->info.data_end = i; |
| 521 |
if( bout->info.data_start == UINT32_MAX ) |
| 522 |
bout->info.data_start = i; |
| 523 |
} |
| 524 |
} |
| 525 |
|
| 526 |
/* debug info about data limits */ |
| 527 |
DEBUG("Flash available from 0x%X to 0x%X, 0x%X bytes.\n", |
| 528 |
bout->info.valid_start, bout->info.valid_end, |
| 529 |
bout->info.valid_end - bout->info.valid_start + 1); // bytes inclusive so +1 |
| 530 |
DEBUG("Data start @ 0x%X; %uB p 0x%X + 0x%X offset.\n", |
| 531 |
bout->info.data_start, bout->info.page_size, |
| 532 |
bout->info.data_start / bout->info.page_size, |
| 533 |
bout->info.data_start % bout->info.page_size); |
| 534 |
DEBUG("Data end @ 0x%X; %uB p 0x%X + 0x%X offset.\n", |
| 535 |
bout->info.data_end, bout->info.page_size, |
| 536 |
bout->info.data_end / bout->info.page_size, |
| 537 |
bout->info.data_end % bout->info.page_size); |
| 538 |
DEBUG("Totals: 0x%X bytes, %u %uB pages.\n", |
| 539 |
bout->info.data_end - bout->info.data_start + 1, |
| 540 |
bout->info.data_end / bout->info.page_size \ |
| 541 |
- bout->info.data_start/bout->info.page_size + 1, |
| 542 |
bout->info.page_size ); |
| 543 |
|
| 544 |
/* more error checking */ |
| 545 |
if( (bout->info.data_start < bout->info.valid_start) || |
| 546 |
(bout->info.data_end > bout->info.valid_end) ) { |
| 547 |
DEBUG( "ERROR: Data exists outside of the valid target flash region.\n" ); |
| 548 |
if( !quiet ) |
| 549 |
fprintf( stderr, "Hex file error, use debug for more info.\n" ); |
| 550 |
return BUFFER_INIT_ERROR; |
| 551 |
} else if( bout->info.data_start == UINT32_MAX ) { |
| 552 |
DEBUG( "ERROR: No valid data to flash.\n" ); |
| 553 |
if( !quiet ) |
| 554 |
fprintf( stderr, "Hex file error, use debug for more info.\n" ); |
| 555 |
return BUFFER_INIT_ERROR; |
| 556 |
} |
| 557 |
|
| 558 |
if( !quiet ) { |
| 559 |
if( debug <= STM32_DEBUG_THRESHOLD ) { |
| 560 |
/* NOTE: from here on we should run finally block */ |
| 561 |
fprintf( stderr, "[================================] " ); |
| 562 |
} |
| 563 |
fprintf( stderr, "Programming 0x%X bytes...\n", |
| 564 |
bout->info.data_end - bout->info.data_start + 1 ); |
| 565 |
if( debug <= STM32_DEBUG_THRESHOLD ) { |
| 566 |
/* NOTE: from here on we need to run finally block */ |
| 567 |
fprintf( stderr, "[" ); |
| 568 |
} |
| 569 |
} |
| 570 |
|
| 571 |
/* program the data */ |
| 572 |
bout->info.block_start = bout->info.data_start; |
| 573 |
reset_address_flag = 1; |
| 574 |
|
| 575 |
while( bout->info.block_start <= bout->info.data_end ) { |
| 576 |
if( reset_address_flag ) { |
| 577 |
address_offset = bout->info.block_start; |
| 578 |
if( (status = stm32_set_address_ptr(device, |
| 579 |
STM32_FLASH_OFFSET + address_offset)) ) { |
| 580 |
DEBUG("Error setting address 0x%X\n", address_offset); |
| 581 |
retval = DEVICE_ACCESS_ERROR; |
| 582 |
goto finally; |
| 583 |
} |
| 584 |
dfu_set_transaction_num( device, 2 ); /* sets block offset 0 */ |
| 585 |
reset_address_flag = 0; |
| 586 |
} |
| 587 |
|
| 588 |
/* find end address (info.block_end) for data section to write */ |
| 589 |
mem_section = bout->info.block_start / STM32_MIN_SECTOR_BOUND; |
| 590 |
for( i = 0, bout->info.block_end = bout->info.block_start; |
| 591 |
bout->info.block_end <= bout->info.data_end; |
| 592 |
bout->info.block_end++, i++ ) { |
| 593 |
xfer_size = bout->info.block_end - bout->info.block_start + 1; |
| 594 |
// check if the current value is valid |
| 595 |
if( bout->data[bout->info.block_end] > UINT8_MAX ) break; |
| 596 |
// check if the current data packet is too big |
| 597 |
if( xfer_size > STM32_MAX_TRANSFER_SIZE ) break; |
| 598 |
// check if the current data value is outside of the memory sector |
| 599 |
if( bout->info.block_end / STM32_MIN_SECTOR_BOUND - mem_section ) break; |
| 600 |
|
| 601 |
buffer[i] = (uint8_t) bout->data[bout->info.block_end]; |
| 602 |
} |
| 603 |
bout->info.block_end--; // bout->info.block_end was one step beyond the last data value to flash |
| 604 |
xfer_size = bout->info.block_end - bout->info.block_start + 1; |
| 605 |
if( xfer_size != STM32_MAX_TRANSFER_SIZE ) { |
| 606 |
DEBUG("xfer_size %u not max %u, need addr reset\n", |
| 607 |
xfer_size, STM32_MAX_TRANSFER_SIZE); |
| 608 |
reset_address_flag = 1; |
| 609 |
} |
| 610 |
|
| 611 |
/* write the data */ |
| 612 |
DEBUG("Program data block: 0x%X to 0x%X, 0x%X bytes.\n", |
| 613 |
bout->info.block_start, bout->info.block_end, xfer_size); |
| 614 |
|
| 615 |
if( (status = stm32_write_block( device, xfer_size, buffer )) ) { |
| 616 |
DEBUG( "Error flashing the block: err %d.\n", status ); |
| 617 |
retval = FLASH_WRITE_ERROR; |
| 618 |
goto finally; |
| 619 |
} |
| 620 |
|
| 621 |
// increment bout->info.block_start to the next valid address |
| 622 |
for( bout->info.block_start = bout->info.block_end + 1; |
| 623 |
bout->info.block_start <= bout->info.data_end; |
| 624 |
bout->info.block_start++ ) { |
| 625 |
if( (bout->data[bout->info.block_start] <= UINT8_MAX) ) break; |
| 626 |
} // bout->info.block_start is now on the first valid data for the next segment |
| 627 |
|
| 628 |
if( reset_address_flag == 0 && (bout->info.block_start != |
| 629 |
(STM32_MAX_TRANSFER_SIZE * (dfu_get_transaction_num( device ) - 2)) |
| 630 |
+ address_offset) ) { |
| 631 |
DEBUG("block start does not match addr, reset req\n"); |
| 632 |
reset_address_flag = 1; |
| 633 |
} |
| 634 |
|
| 635 |
// display progress in 32 increments (if not hidden) |
| 636 |
if ( !quiet ) print_progress( &bout->info, &progress ); |
| 637 |
} |
| 638 |
retval = SUCCESS; |
| 639 |
|
| 640 |
finally: |
| 641 |
if ( !quiet ) { |
| 642 |
if( SUCCESS == retval ) { |
| 643 |
if ( debug <= STM32_DEBUG_THRESHOLD ) { |
| 644 |
fprintf( stderr, "] " ); |
| 645 |
} |
| 646 |
fprintf( stderr, "SUCCESS\n" ); |
| 647 |
} else { |
| 648 |
if ( debug <= STM32_DEBUG_THRESHOLD ) { |
| 649 |
fprintf( stderr, " X "); |
| 650 |
} |
| 651 |
fprintf( stderr, "ERROR\n" ); |
| 652 |
if( retval==DEVICE_ACCESS_ERROR ) |
| 653 |
fprintf( stderr, |
| 654 |
"Memory access error, use debug for more info.\n" ); |
| 655 |
else if( retval==FLASH_WRITE_ERROR ) |
| 656 |
fprintf( stderr, |
| 657 |
"Memory write error, use debug for more info.\n" ); |
| 658 |
} |
| 659 |
} |
| 660 |
|
| 661 |
return retval; |
| 662 |
} |
| 663 |
|
| 664 |
int32_t stm32_get_commands( dfu_device_t *device ) { |
| 665 |
TRACE("%s( %p )\n", __FUNCTION__, device); |
| 666 |
int32_t result; |
| 667 |
uint8_t i; |
| 668 |
const size_t xfer_len = 80; |
| 669 |
uint8_t buffer[xfer_len]; |
| 670 |
|
| 671 |
/* check status before read */ |
| 672 |
if( (result = stm32_get_status(device)) ) { |
| 673 |
DEBUG("Status Error %d before read\n", result ); |
| 674 |
return UNSPECIFIED_ERROR; |
| 675 |
} |
| 676 |
|
| 677 |
dfu_set_transaction_num( device, 0 ); |
| 678 |
result = dfu_upload( device, xfer_len, buffer ); |
| 679 |
if( result < 0) { |
| 680 |
dfu_status_t status; |
| 681 |
|
| 682 |
DEBUG( "dfu_upload result: %d\n", result ); |
| 683 |
result = UNSPECIFIED_ERROR; |
| 684 |
if( 0 == dfu_get_status(device, &status) ) { |
| 685 |
if( status.bStatus == DFU_STATUS_OK ) { |
| 686 |
DEBUG("DFU Status OK, state %d\n", status.bState); |
| 687 |
} else if( status.bStatus == DFU_STATUS_ERROR_VENDOR ) { |
| 688 |
DEBUG("Device is read protected\n"); |
| 689 |
/* status = dfuERROR and state = errVENDOR */ |
| 690 |
result = DEVICE_ACCESS_ERROR; |
| 691 |
} else { |
| 692 |
DEBUG("Unknown error status %d / state %d\n", |
| 693 |
status.bStatus, status.bState ); |
| 694 |
} |
| 695 |
} else { |
| 696 |
DEBUG("DFU GET_STATUS fail\n"); |
| 697 |
} |
| 698 |
dfu_clear_status( device ); |
| 699 |
|
| 700 |
return result; |
| 701 |
} |
| 702 |
|
| 703 |
fprintf( stdout, "There are %d commands:\n", result ); |
| 704 |
for( i = 0; i < result; i++ ) { |
| 705 |
fprintf( stdout, " 0x%02X\n", buffer[i] ); |
| 706 |
} |
| 707 |
|
| 708 |
return SUCCESS; |
| 709 |
} |
| 710 |
|
| 711 |
int32_t stm32_get_configuration( dfu_device_t *device ) { |
| 712 |
TRACE("%s( %p )\n", __FUNCTION__, device); |
| 713 |
int32_t status; |
| 714 |
uint8_t i; |
| 715 |
uint8_t buffer[STM32_OPTION_BYTES_SIZE]; |
| 716 |
|
| 717 |
if( (status = stm32_set_address_ptr(device, |
| 718 |
stm32_sector_addresses[mem_st_option_bytes])) ) { |
| 719 |
DEBUG("Error (%d) setting address 0x%X\n", |
| 720 |
status, stm32_sector_addresses[mem_st_option_bytes]); |
| 721 |
return UNSPECIFIED_ERROR; |
| 722 |
} |
| 723 |
|
| 724 |
if( (status = stm32_read_block(device, STM32_OPTION_BYTES_SIZE, buffer)) ) { |
| 725 |
DEBUG("Error (%d) reading option buffer block\n", status ); |
| 726 |
return FLASH_READ_ERROR; |
| 727 |
} |
| 728 |
|
| 729 |
fprintf( stdout, "There are %d option bytes:\n", STM32_OPTION_BYTES_SIZE ); |
| 730 |
fprintf( stdout, "0x%02X", buffer[0] ); |
| 731 |
for( i = 1; i < STM32_OPTION_BYTES_SIZE; i++ ) { |
| 732 |
fprintf( stdout, ", 0x%02X", buffer[i] ); |
| 733 |
} |
| 734 |
fprintf( stdout, "\n" ); |
| 735 |
|
| 736 |
return SUCCESS; |
| 737 |
} |
| 738 |
|
| 739 |
int32_t stm32_read_unprotect( dfu_device_t *device, bool quiet ) { |
| 740 |
TRACE( "%s( %p, %s )\n", __FUNCTION__, device, quiet ? "true" : "false" ); |
| 741 |
uint8_t command[] = { READ_UNPROTECT }; |
| 742 |
uint8_t length = 1; |
| 743 |
|
| 744 |
if( !quiet ) { |
| 745 |
fprintf( stderr, "Read Unprotect, Erasing flash... " ); |
| 746 |
DEBUG("\n"); |
| 747 |
} |
| 748 |
|
| 749 |
return stm32_erase( device, command, length, quiet ); |
| 750 |
} |
| 751 |
|
| 752 |
// cSpell:ignore shiftwidth |
| 753 |
// vim: shiftwidth=2 |