flash: dynamically allocate working storage

Allocate working memory rather than using stack.

Signed-off-by: Øyvind Harboe <oyvind.harboe@zylin.com>
This commit is contained in:
Øyvind Harboe 2009-11-22 19:06:44 +01:00
parent aacc5b583c
commit 5416c525d4
1 changed files with 52 additions and 16 deletions

View File

@ -728,24 +728,40 @@ COMMAND_HANDLER(handle_flash_fill_command)
uint32_t address; uint32_t address;
uint32_t pattern; uint32_t pattern;
uint32_t count; uint32_t count;
uint8_t chunk[1024];
uint8_t readback[1024];
uint32_t wrote = 0; uint32_t wrote = 0;
uint32_t cur_size = 0; uint32_t cur_size = 0;
uint32_t chunk_count; uint32_t chunk_count;
struct target *target = get_current_target(CMD_CTX); struct target *target = get_current_target(CMD_CTX);
uint32_t i; uint32_t i;
uint32_t wordsize; 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) 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[0], address);
COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], pattern); COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], pattern);
COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], count); COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], count);
if (count == 0) if (count == 0)
return ERROR_OK; goto done;
switch (CMD_NAME[4]) switch (CMD_NAME[4])
{ {
@ -759,10 +775,11 @@ COMMAND_HANDLER(handle_flash_fill_command)
wordsize = 1; wordsize = 1;
break; break;
default: 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) switch (wordsize)
{ {
case 4: case 4:
@ -795,15 +812,22 @@ COMMAND_HANDLER(handle_flash_fill_command)
bank = get_flash_bank_by_addr(target, address); bank = get_flash_bank_by_addr(target, address);
if (bank == NULL) if (bank == NULL)
{ {
return ERROR_FAIL; retval = ERROR_FAIL;
goto done;
} }
err = flash_driver_write(bank, chunk, address - bank->base + wrote, cur_size); err = flash_driver_write(bank, chunk, address - bank->base + wrote, cur_size);
if (err != ERROR_OK) if (err != ERROR_OK)
return err; {
retval = err;
goto done;
}
err = target_read_buffer(target, address + wrote, cur_size, readback); err = target_read_buffer(target, address + wrote, cur_size, readback);
if (err != ERROR_OK) if (err != ERROR_OK)
return err; {
retval = err;
goto done;
}
unsigned i; unsigned i;
for (i = 0; i < cur_size; 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", LOG_ERROR("Verfication error address 0x%08" PRIx32 ", read back 0x%02x, expected 0x%02x",
address + wrote + i, readback[i], chunk[i]); 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, " in %fs (%0.3f kb/s)", wrote, address,
duration_elapsed(&bench), duration_kbps(&bench, wrote)); duration_elapsed(&bench), duration_kbps(&bench, wrote));
} }
return ERROR_OK;
done:
free(readback);
free(chunk);
return retval;
} }
COMMAND_HANDLER(handle_flash_write_bank_command) 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) int default_flash_mem_blank_check(struct flash_bank *bank)
{ {
struct target *target = bank->target; struct target *target = bank->target;
uint8_t buffer[1024]; const int buffer_size = 1024;
int buffer_size = sizeof(buffer);
int i; int i;
uint32_t nBytes; uint32_t nBytes;
int retval = ERROR_OK;
if (bank->target->state != TARGET_HALTED) 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; return ERROR_TARGET_NOT_HALTED;
} }
uint8_t *buffer = malloc(buffer_size);
for (i = 0; i < bank->num_sectors; i++) for (i = 0; i < bank->num_sectors; i++)
{ {
uint32_t j; 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) for (j = 0; j < bank->sectors[i].size; j += buffer_size)
{ {
uint32_t chunk; uint32_t chunk;
int retval;
chunk = buffer_size; chunk = buffer_size;
if (chunk > (j - bank->sectors[i].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); retval = target_read_memory(target, bank->base + bank->sectors[i].offset + j, 4, chunk/4, buffer);
if (retval != ERROR_OK) if (retval != ERROR_OK)
return retval; {
goto done;
}
for (nBytes = 0; nBytes < chunk; nBytes++) 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) int default_flash_blank_check(struct flash_bank *bank)