diff options
author | Øyvind Harboe <oyvind.harboe@zylin.com> | 2009-11-22 19:06:44 +0100 |
---|---|---|
committer | Øyvind Harboe <oyvind.harboe@zylin.com> | 2009-11-22 19:14:06 +0100 |
commit | 5416c525d4e232161572fbbd1b200a7f3a7c2819 (patch) | |
tree | cd37bc246a460126682abfa755b225836adb12f9 /src | |
parent | aacc5b583c6415fe8d3e6fc99066d6ef819fa22c (diff) | |
download | openocd_libswd-5416c525d4e232161572fbbd1b200a7f3a7c2819.tar.gz openocd_libswd-5416c525d4e232161572fbbd1b200a7f3a7c2819.tar.bz2 openocd_libswd-5416c525d4e232161572fbbd1b200a7f3a7c2819.tar.xz openocd_libswd-5416c525d4e232161572fbbd1b200a7f3a7c2819.zip |
flash: dynamically allocate working storage
Allocate working memory rather than using stack.
Signed-off-by: Øyvind Harboe <oyvind.harboe@zylin.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/flash/flash.c | 68 |
1 files changed, 52 insertions, 16 deletions
diff --git a/src/flash/flash.c b/src/flash/flash.c index ef6c6da0..e2136b70 100644 --- a/src/flash/flash.c +++ b/src/flash/flash.c @@ -728,24 +728,40 @@ COMMAND_HANDLER(handle_flash_fill_command) uint32_t address; uint32_t pattern; uint32_t count; - uint8_t chunk[1024]; - uint8_t readback[1024]; uint32_t wrote = 0; uint32_t cur_size = 0; uint32_t chunk_count; struct target *target = get_current_target(CMD_CTX); uint32_t i; uint32_t wordsize; + int retval = ERROR_OK; + + static size_t const chunksize = 1024; + uint8_t *chunk = malloc(chunksize); + if (chunk == NULL) + return ERROR_FAIL; + + uint8_t *readback = malloc(chunksize); + if (readback == NULL) + { + free(chunk); + return ERROR_FAIL; + } + if (CMD_ARGC != 3) - return ERROR_COMMAND_SYNTAX_ERROR; + { + retval = ERROR_COMMAND_SYNTAX_ERROR; + goto done; + } + COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address); COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], pattern); COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], count); if (count == 0) - return ERROR_OK; + goto done; switch (CMD_NAME[4]) { @@ -759,10 +775,11 @@ COMMAND_HANDLER(handle_flash_fill_command) wordsize = 1; break; default: - return ERROR_COMMAND_SYNTAX_ERROR; + retval = ERROR_COMMAND_SYNTAX_ERROR; + goto done; } - chunk_count = MIN(count, (1024 / wordsize)); + chunk_count = MIN(count, (chunksize / wordsize)); switch (wordsize) { case 4: @@ -795,15 +812,22 @@ COMMAND_HANDLER(handle_flash_fill_command) bank = get_flash_bank_by_addr(target, address); if (bank == NULL) { - return ERROR_FAIL; + retval = ERROR_FAIL; + goto done; } err = flash_driver_write(bank, chunk, address - bank->base + wrote, cur_size); if (err != ERROR_OK) - return err; + { + retval = err; + goto done; + } err = target_read_buffer(target, address + wrote, cur_size, readback); if (err != ERROR_OK) - return err; + { + retval = err; + goto done; + } unsigned i; for (i = 0; i < cur_size; i++) @@ -812,7 +836,8 @@ COMMAND_HANDLER(handle_flash_fill_command) { LOG_ERROR("Verfication error address 0x%08" PRIx32 ", read back 0x%02x, expected 0x%02x", address + wrote + i, readback[i], chunk[i]); - return ERROR_FAIL; + retval = ERROR_FAIL; + goto done; } } } @@ -823,7 +848,12 @@ COMMAND_HANDLER(handle_flash_fill_command) " in %fs (%0.3f kb/s)", wrote, address, duration_elapsed(&bench), duration_kbps(&bench, wrote)); } - return ERROR_OK; + + done: + free(readback); + free(chunk); + + return retval; } COMMAND_HANDLER(handle_flash_write_bank_command) @@ -1152,10 +1182,10 @@ int flash_write(struct target *target, struct image *image, uint32_t *written, i int default_flash_mem_blank_check(struct flash_bank *bank) { struct target *target = bank->target; - uint8_t buffer[1024]; - int buffer_size = sizeof(buffer); + const int buffer_size = 1024; int i; uint32_t nBytes; + int retval = ERROR_OK; if (bank->target->state != TARGET_HALTED) { @@ -1163,6 +1193,8 @@ int default_flash_mem_blank_check(struct flash_bank *bank) return ERROR_TARGET_NOT_HALTED; } + uint8_t *buffer = malloc(buffer_size); + for (i = 0; i < bank->num_sectors; i++) { uint32_t j; @@ -1171,7 +1203,6 @@ int default_flash_mem_blank_check(struct flash_bank *bank) for (j = 0; j < bank->sectors[i].size; j += buffer_size) { uint32_t chunk; - int retval; chunk = buffer_size; if (chunk > (j - bank->sectors[i].size)) { @@ -1180,7 +1211,9 @@ int default_flash_mem_blank_check(struct flash_bank *bank) retval = target_read_memory(target, bank->base + bank->sectors[i].offset + j, 4, chunk/4, buffer); if (retval != ERROR_OK) - return retval; + { + goto done; + } for (nBytes = 0; nBytes < chunk; nBytes++) { @@ -1193,7 +1226,10 @@ int default_flash_mem_blank_check(struct flash_bank *bank) } } - return ERROR_OK; + done: + free(buffer); + + return retval; } int default_flash_blank_check(struct flash_bank *bank) |