diff options
Diffstat (limited to 'drivers/dfu/dfu.c')
-rw-r--r-- | drivers/dfu/dfu.c | 105 |
1 files changed, 66 insertions, 39 deletions
diff --git a/drivers/dfu/dfu.c b/drivers/dfu/dfu.c index dc09ff6466..3512b149c5 100644 --- a/drivers/dfu/dfu.c +++ b/drivers/dfu/dfu.c @@ -44,7 +44,7 @@ static int dfu_find_alt_num(const char *s) return ++i; } -int dfu_init_env_entities(char *interface, int dev) +int dfu_init_env_entities(char *interface, char *devstr) { const char *str_env; char *env_bkp; @@ -57,7 +57,7 @@ int dfu_init_env_entities(char *interface, int dev) } env_bkp = strdup(str_env); - ret = dfu_config_entities(env_bkp, interface, dev); + ret = dfu_config_entities(env_bkp, interface, devstr); if (ret) { error("DFU entities configuration failed!\n"); return ret; @@ -82,7 +82,7 @@ unsigned long dfu_get_buf_size(void) return dfu_buf_size; } -unsigned char *dfu_get_buf(void) +unsigned char *dfu_get_buf(struct dfu_entity *dfu) { char *s; @@ -92,6 +92,8 @@ unsigned char *dfu_get_buf(void) s = getenv("dfu_bufsiz"); dfu_buf_size = s ? (unsigned long)simple_strtol(s, NULL, 16) : CONFIG_SYS_DFU_DATA_BUF_SIZE; + if (dfu->max_buf_size && dfu_buf_size > dfu->max_buf_size) + dfu_buf_size = dfu->max_buf_size; dfu_buf = memalign(CONFIG_SYS_CACHELINE_SIZE, dfu_buf_size); if (dfu_buf == NULL) @@ -147,6 +149,19 @@ static int dfu_write_buffer_drain(struct dfu_entity *dfu) return ret; } +void dfu_write_transaction_cleanup(struct dfu_entity *dfu) +{ + /* clear everything */ + dfu_free_buf(); + dfu->crc = 0; + dfu->offset = 0; + dfu->i_blk_seq_num = 0; + dfu->i_buf_start = dfu_buf; + dfu->i_buf_end = dfu_buf; + dfu->i_buf = dfu->i_buf_start; + dfu->inited = 0; +} + int dfu_flush(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num) { int ret = 0; @@ -162,23 +177,14 @@ int dfu_flush(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num) printf("\nDFU complete %s: 0x%08x\n", dfu_hash_algo->name, dfu->crc); - /* clear everything */ - dfu_free_buf(); - dfu->crc = 0; - dfu->offset = 0; - dfu->i_blk_seq_num = 0; - dfu->i_buf_start = dfu_buf; - dfu->i_buf_end = dfu_buf; - dfu->i_buf = dfu->i_buf_start; - dfu->inited = 0; + dfu_write_transaction_cleanup(dfu); return ret; } int dfu_write(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num) { - int ret = 0; - int tret; + int ret; debug("%s: name: %s buf: 0x%p size: 0x%x p_num: 0x%x offset: 0x%llx bufoffset: 0x%x\n", __func__, dfu->name, buf, size, blk_seq_num, dfu->offset, @@ -190,10 +196,10 @@ int dfu_write(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num) dfu->offset = 0; dfu->bad_skip = 0; dfu->i_blk_seq_num = 0; - dfu->i_buf_start = dfu_get_buf(); + dfu->i_buf_start = dfu_get_buf(dfu); if (dfu->i_buf_start == NULL) return -ENOMEM; - dfu->i_buf_end = dfu_get_buf() + dfu_buf_size; + dfu->i_buf_end = dfu_get_buf(dfu) + dfu_buf_size; dfu->i_buf = dfu->i_buf_start; dfu->inited = 1; @@ -202,6 +208,7 @@ int dfu_write(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num) if (dfu->i_blk_seq_num != blk_seq_num) { printf("%s: Wrong sequence number! [%d] [%d]\n", __func__, dfu->i_blk_seq_num, blk_seq_num); + dfu_write_transaction_cleanup(dfu); return -1; } @@ -223,15 +230,18 @@ int dfu_write(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num) /* flush buffer if overflow */ if ((dfu->i_buf + size) > dfu->i_buf_end) { - tret = dfu_write_buffer_drain(dfu); - if (ret == 0) - ret = tret; + ret = dfu_write_buffer_drain(dfu); + if (ret) { + dfu_write_transaction_cleanup(dfu); + return ret; + } } /* we should be in buffer now (if not then size too large) */ if ((dfu->i_buf + size) > dfu->i_buf_end) { error("Buffer overflow! (0x%p + 0x%x > 0x%p)\n", dfu->i_buf, size, dfu->i_buf_end); + dfu_write_transaction_cleanup(dfu); return -1; } @@ -240,12 +250,14 @@ int dfu_write(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num) /* if end or if buffer full flush */ if (size == 0 || (dfu->i_buf + size) > dfu->i_buf_end) { - tret = dfu_write_buffer_drain(dfu); - if (ret == 0) - ret = tret; + ret = dfu_write_buffer_drain(dfu); + if (ret) { + dfu_write_transaction_cleanup(dfu); + return ret; + } } - return ret; + return 0; } static int dfu_read_buffer_fill(struct dfu_entity *dfu, void *buf, int size) @@ -267,7 +279,6 @@ static int dfu_read_buffer_fill(struct dfu_entity *dfu, void *buf, int size) dfu->i_buf += chunk; dfu->b_left -= chunk; - dfu->r_left -= chunk; size -= chunk; buf += chunk; readn += chunk; @@ -309,14 +320,23 @@ int dfu_read(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num) __func__, dfu->name, buf, size, blk_seq_num, dfu->i_buf); if (!dfu->inited) { - dfu->i_buf_start = dfu_get_buf(); + dfu->i_buf_start = dfu_get_buf(dfu); if (dfu->i_buf_start == NULL) return -ENOMEM; - ret = dfu->read_medium(dfu, 0, dfu->i_buf_start, &dfu->r_left); - if (ret != 0) { - debug("%s: failed to get r_left\n", __func__); - return ret; + dfu->r_left = dfu->get_medium_size(dfu); + if (dfu->r_left < 0) + return dfu->r_left; + switch (dfu->layout) { + case DFU_RAW_ADDR: + case DFU_RAM_ADDR: + break; + default: + if (dfu->r_left > dfu_buf_size) { + printf("%s: File too big for buffer\n", + __func__); + return -EOVERFLOW; + } } debug("%s: %s %ld [B]\n", __func__, dfu->name, dfu->r_left); @@ -324,9 +344,9 @@ int dfu_read(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num) dfu->i_blk_seq_num = 0; dfu->crc = 0; dfu->offset = 0; - dfu->i_buf_end = dfu_get_buf() + dfu_buf_size; + dfu->i_buf_end = dfu_get_buf(dfu) + dfu_buf_size; dfu->i_buf = dfu->i_buf_start; - dfu->b_left = min(dfu_buf_size, dfu->r_left); + dfu->b_left = 0; dfu->bad_skip = 0; @@ -371,26 +391,30 @@ int dfu_read(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num) } static int dfu_fill_entity(struct dfu_entity *dfu, char *s, int alt, - char *interface, int num) + char *interface, char *devstr) { char *st; - debug("%s: %s interface: %s num: %d\n", __func__, s, interface, num); + debug("%s: %s interface: %s dev: %s\n", __func__, s, interface, devstr); st = strsep(&s, " "); strcpy(dfu->name, st); - dfu->dev_num = num; dfu->alt = alt; + dfu->max_buf_size = 0; + dfu->free_entity = NULL; /* Specific for mmc device */ if (strcmp(interface, "mmc") == 0) { - if (dfu_fill_entity_mmc(dfu, s)) + if (dfu_fill_entity_mmc(dfu, devstr, s)) return -1; } else if (strcmp(interface, "nand") == 0) { - if (dfu_fill_entity_nand(dfu, s)) + if (dfu_fill_entity_nand(dfu, devstr, s)) return -1; } else if (strcmp(interface, "ram") == 0) { - if (dfu_fill_entity_ram(dfu, s)) + if (dfu_fill_entity_ram(dfu, devstr, s)) + return -1; + } else if (strcmp(interface, "sf") == 0) { + if (dfu_fill_entity_sf(dfu, devstr, s)) return -1; } else { printf("%s: Device %s not (yet) supported!\n", @@ -407,6 +431,8 @@ void dfu_free_entities(void) list_for_each_entry_safe_reverse(dfu, p, &dfu_list, list) { list_del(&dfu->list); + if (dfu->free_entity) + dfu->free_entity(dfu); t = dfu; } if (t) @@ -416,7 +442,7 @@ void dfu_free_entities(void) alt_num_cnt = 0; } -int dfu_config_entities(char *env, char *interface, int num) +int dfu_config_entities(char *env, char *interface, char *devstr) { struct dfu_entity *dfu; int i, ret; @@ -439,7 +465,8 @@ int dfu_config_entities(char *env, char *interface, int num) for (i = 0; i < dfu_alt_num; i++) { s = strsep(&env, ";"); - ret = dfu_fill_entity(&dfu[i], s, alt_num_cnt, interface, num); + ret = dfu_fill_entity(&dfu[i], s, alt_num_cnt, interface, + devstr); if (ret) return -1; |