2012-08-06 12:41:07 +00:00
|
|
|
/*
|
|
|
|
* dfu.c -- DFU back-end routines
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012 Samsung Electronics
|
|
|
|
* author: Lukasz Majewski <l.majewski@samsung.com>
|
|
|
|
*
|
2013-07-08 07:37:19 +00:00
|
|
|
* SPDX-License-Identifier: GPL-2.0+
|
2012-08-06 12:41:07 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
2013-06-12 04:05:51 +00:00
|
|
|
#include <errno.h>
|
2012-08-06 12:41:07 +00:00
|
|
|
#include <malloc.h>
|
|
|
|
#include <mmc.h>
|
|
|
|
#include <fat.h>
|
|
|
|
#include <dfu.h>
|
2014-05-12 08:43:36 +00:00
|
|
|
#include <hash.h>
|
2012-08-06 12:41:07 +00:00
|
|
|
#include <linux/list.h>
|
|
|
|
#include <linux/compiler.h>
|
|
|
|
|
|
|
|
static LIST_HEAD(dfu_list);
|
|
|
|
static int dfu_alt_num;
|
2014-02-28 17:53:37 +00:00
|
|
|
static int alt_num_cnt;
|
2014-05-12 08:43:36 +00:00
|
|
|
static struct hash_algo *dfu_hash_algo;
|
2012-08-06 12:41:07 +00:00
|
|
|
|
2014-08-25 09:07:28 +00:00
|
|
|
/*
|
|
|
|
* The purpose of the dfu_usb_get_reset() function is to
|
|
|
|
* provide information if after USB_DETACH request
|
|
|
|
* being sent the dfu-util performed reset of USB
|
|
|
|
* bus.
|
|
|
|
*
|
|
|
|
* Described behaviour is the only way to distinct if
|
|
|
|
* user has typed -e (detach) or -R (reset) when invoking
|
|
|
|
* dfu-util command.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
__weak bool dfu_usb_get_reset(void)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-08-06 12:41:07 +00:00
|
|
|
static int dfu_find_alt_num(const char *s)
|
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
for (; *s; s++)
|
|
|
|
if (*s == ';')
|
|
|
|
i++;
|
|
|
|
|
|
|
|
return ++i;
|
|
|
|
}
|
|
|
|
|
2014-06-11 22:03:33 +00:00
|
|
|
int dfu_init_env_entities(char *interface, char *devstr)
|
2013-09-11 12:53:35 +00:00
|
|
|
{
|
|
|
|
const char *str_env;
|
|
|
|
char *env_bkp;
|
|
|
|
int ret;
|
|
|
|
|
2015-02-17 11:24:11 +00:00
|
|
|
#ifdef CONFIG_SET_DFU_ALT_INFO
|
|
|
|
set_dfu_alt_info(interface, devstr);
|
|
|
|
#endif
|
2013-09-11 12:53:35 +00:00
|
|
|
str_env = getenv("dfu_alt_info");
|
|
|
|
if (!str_env) {
|
|
|
|
error("\"dfu_alt_info\" env variable not defined!\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
env_bkp = strdup(str_env);
|
2014-06-11 22:03:33 +00:00
|
|
|
ret = dfu_config_entities(env_bkp, interface, devstr);
|
2013-09-11 12:53:35 +00:00
|
|
|
if (ret) {
|
|
|
|
error("DFU entities configuration failed!\n");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(env_bkp);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-06-12 04:05:51 +00:00
|
|
|
static unsigned char *dfu_buf;
|
2015-07-08 21:43:18 +00:00
|
|
|
static unsigned long dfu_buf_size;
|
2013-06-12 04:05:51 +00:00
|
|
|
|
2013-10-08 12:30:39 +00:00
|
|
|
unsigned char *dfu_free_buf(void)
|
2013-06-12 04:05:51 +00:00
|
|
|
{
|
|
|
|
free(dfu_buf);
|
|
|
|
dfu_buf = NULL;
|
|
|
|
return dfu_buf;
|
|
|
|
}
|
|
|
|
|
2013-12-09 15:20:13 +00:00
|
|
|
unsigned long dfu_get_buf_size(void)
|
|
|
|
{
|
|
|
|
return dfu_buf_size;
|
|
|
|
}
|
|
|
|
|
2014-06-11 22:03:34 +00:00
|
|
|
unsigned char *dfu_get_buf(struct dfu_entity *dfu)
|
2013-06-12 04:05:51 +00:00
|
|
|
{
|
|
|
|
char *s;
|
|
|
|
|
|
|
|
if (dfu_buf != NULL)
|
|
|
|
return dfu_buf;
|
|
|
|
|
|
|
|
s = getenv("dfu_bufsiz");
|
2014-12-15 09:34:11 +00:00
|
|
|
if (s)
|
|
|
|
dfu_buf_size = (unsigned long)simple_strtol(s, NULL, 0);
|
|
|
|
|
|
|
|
if (!s || !dfu_buf_size)
|
|
|
|
dfu_buf_size = CONFIG_SYS_DFU_DATA_BUF_SIZE;
|
|
|
|
|
2014-06-11 22:03:34 +00:00
|
|
|
if (dfu->max_buf_size && dfu_buf_size > dfu->max_buf_size)
|
|
|
|
dfu_buf_size = dfu->max_buf_size;
|
2013-06-12 04:05:51 +00:00
|
|
|
|
|
|
|
dfu_buf = memalign(CONFIG_SYS_CACHELINE_SIZE, dfu_buf_size);
|
|
|
|
if (dfu_buf == NULL)
|
|
|
|
printf("%s: Could not memalign 0x%lx bytes\n",
|
|
|
|
__func__, dfu_buf_size);
|
|
|
|
|
|
|
|
return dfu_buf;
|
|
|
|
}
|
2012-08-06 12:41:07 +00:00
|
|
|
|
2014-05-12 08:43:36 +00:00
|
|
|
static char *dfu_get_hash_algo(void)
|
|
|
|
{
|
|
|
|
char *s;
|
|
|
|
|
|
|
|
s = getenv("dfu_hash_algo");
|
2014-06-10 10:25:59 +00:00
|
|
|
if (!s)
|
2014-05-12 08:43:36 +00:00
|
|
|
return NULL;
|
|
|
|
|
2014-06-10 10:25:59 +00:00
|
|
|
if (!strcmp(s, "crc32")) {
|
|
|
|
debug("%s: DFU hash method: %s\n", __func__, s);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
error("DFU hash method: %s not supported!\n", s);
|
2014-05-12 08:43:36 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-03-14 05:32:48 +00:00
|
|
|
static int dfu_write_buffer_drain(struct dfu_entity *dfu)
|
|
|
|
{
|
|
|
|
long w_size;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* flush size? */
|
|
|
|
w_size = dfu->i_buf - dfu->i_buf_start;
|
|
|
|
if (w_size == 0)
|
|
|
|
return 0;
|
|
|
|
|
2014-05-12 08:43:36 +00:00
|
|
|
if (dfu_hash_algo)
|
|
|
|
dfu_hash_algo->hash_update(dfu_hash_algo, &dfu->crc,
|
|
|
|
dfu->i_buf_start, w_size, 0);
|
2013-03-14 05:32:48 +00:00
|
|
|
|
|
|
|
ret = dfu->write_medium(dfu, dfu->offset, dfu->i_buf_start, &w_size);
|
|
|
|
if (ret)
|
|
|
|
debug("%s: Write error!\n", __func__);
|
|
|
|
|
|
|
|
/* point back */
|
|
|
|
dfu->i_buf = dfu->i_buf_start;
|
|
|
|
|
|
|
|
/* update offset */
|
|
|
|
dfu->offset += w_size;
|
|
|
|
|
|
|
|
puts("#");
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-06-11 18:48:08 +00:00
|
|
|
void dfu_write_transaction_cleanup(struct dfu_entity *dfu)
|
|
|
|
{
|
|
|
|
/* clear everything */
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-03-18 07:09:55 +00:00
|
|
|
int dfu_flush(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
|
2014-05-12 10:05:33 +00:00
|
|
|
ret = dfu_write_buffer_drain(dfu);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2014-03-18 07:09:55 +00:00
|
|
|
if (dfu->flush_medium)
|
|
|
|
ret = dfu->flush_medium(dfu);
|
|
|
|
|
2014-05-12 08:43:36 +00:00
|
|
|
if (dfu_hash_algo)
|
|
|
|
printf("\nDFU complete %s: 0x%08x\n", dfu_hash_algo->name,
|
|
|
|
dfu->crc);
|
2014-03-18 07:09:55 +00:00
|
|
|
|
2014-06-11 18:48:08 +00:00
|
|
|
dfu_write_transaction_cleanup(dfu);
|
2014-03-18 07:09:55 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-08-06 12:41:07 +00:00
|
|
|
int dfu_write(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
|
|
|
|
{
|
2014-06-11 18:48:08 +00:00
|
|
|
int ret;
|
2012-08-06 12:41:07 +00:00
|
|
|
|
2015-07-22 20:54:04 +00:00
|
|
|
debug("%s: name: %s buf: 0x%p size: 0x%x p_num: 0x%x offset: 0x%llx bufoffset: 0x%lx\n",
|
2013-03-14 05:32:48 +00:00
|
|
|
__func__, dfu->name, buf, size, blk_seq_num, dfu->offset,
|
2015-07-22 20:54:04 +00:00
|
|
|
(unsigned long)(dfu->i_buf - dfu->i_buf_start));
|
2012-08-06 12:41:07 +00:00
|
|
|
|
2013-03-14 05:32:48 +00:00
|
|
|
if (!dfu->inited) {
|
|
|
|
/* initial state */
|
|
|
|
dfu->crc = 0;
|
|
|
|
dfu->offset = 0;
|
2013-03-14 05:32:52 +00:00
|
|
|
dfu->bad_skip = 0;
|
2013-03-14 05:32:48 +00:00
|
|
|
dfu->i_blk_seq_num = 0;
|
2014-06-11 22:03:34 +00:00
|
|
|
dfu->i_buf_start = dfu_get_buf(dfu);
|
2013-06-12 04:05:51 +00:00
|
|
|
if (dfu->i_buf_start == NULL)
|
|
|
|
return -ENOMEM;
|
2014-06-11 22:03:34 +00:00
|
|
|
dfu->i_buf_end = dfu_get_buf(dfu) + dfu_buf_size;
|
2013-03-14 05:32:48 +00:00
|
|
|
dfu->i_buf = dfu->i_buf_start;
|
|
|
|
|
|
|
|
dfu->inited = 1;
|
2012-08-06 12:41:07 +00:00
|
|
|
}
|
|
|
|
|
2013-03-14 05:32:48 +00:00
|
|
|
if (dfu->i_blk_seq_num != blk_seq_num) {
|
2012-08-06 12:41:07 +00:00
|
|
|
printf("%s: Wrong sequence number! [%d] [%d]\n",
|
2013-03-14 05:32:48 +00:00
|
|
|
__func__, dfu->i_blk_seq_num, blk_seq_num);
|
2014-06-11 18:48:08 +00:00
|
|
|
dfu_write_transaction_cleanup(dfu);
|
2012-08-06 12:41:07 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-03-14 05:32:48 +00:00
|
|
|
/* DFU 1.1 standard says:
|
|
|
|
* The wBlockNum field is a block sequence number. It increments each
|
|
|
|
* time a block is transferred, wrapping to zero from 65,535. It is used
|
|
|
|
* to provide useful context to the DFU loader in the device."
|
|
|
|
*
|
|
|
|
* This means that it's a 16 bit counter that roll-overs at
|
|
|
|
* 0xffff -> 0x0000. By having a typical 4K transfer block
|
|
|
|
* we roll-over at exactly 256MB. Not very fun to debug.
|
|
|
|
*
|
|
|
|
* Handling rollover, and having an inited variable,
|
|
|
|
* makes things work.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* handle rollover */
|
|
|
|
dfu->i_blk_seq_num = (dfu->i_blk_seq_num + 1) & 0xffff;
|
|
|
|
|
|
|
|
/* flush buffer if overflow */
|
|
|
|
if ((dfu->i_buf + size) > dfu->i_buf_end) {
|
2014-06-11 18:48:08 +00:00
|
|
|
ret = dfu_write_buffer_drain(dfu);
|
|
|
|
if (ret) {
|
|
|
|
dfu_write_transaction_cleanup(dfu);
|
|
|
|
return ret;
|
|
|
|
}
|
2013-03-14 05:32:48 +00:00
|
|
|
}
|
2012-08-06 12:41:07 +00:00
|
|
|
|
2013-03-14 05:32:48 +00:00
|
|
|
/* we should be in buffer now (if not then size too large) */
|
|
|
|
if ((dfu->i_buf + size) > dfu->i_buf_end) {
|
2013-09-10 13:29:22 +00:00
|
|
|
error("Buffer overflow! (0x%p + 0x%x > 0x%p)\n", dfu->i_buf,
|
|
|
|
size, dfu->i_buf_end);
|
2014-06-11 18:48:08 +00:00
|
|
|
dfu_write_transaction_cleanup(dfu);
|
2013-03-14 05:32:48 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(dfu->i_buf, buf, size);
|
|
|
|
dfu->i_buf += size;
|
|
|
|
|
|
|
|
/* if end or if buffer full flush */
|
|
|
|
if (size == 0 || (dfu->i_buf + size) > dfu->i_buf_end) {
|
2014-06-11 18:48:08 +00:00
|
|
|
ret = dfu_write_buffer_drain(dfu);
|
|
|
|
if (ret) {
|
|
|
|
dfu_write_transaction_cleanup(dfu);
|
|
|
|
return ret;
|
|
|
|
}
|
2013-03-14 05:32:48 +00:00
|
|
|
}
|
|
|
|
|
2014-06-11 18:48:08 +00:00
|
|
|
return 0;
|
2013-03-14 05:32:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int dfu_read_buffer_fill(struct dfu_entity *dfu, void *buf, int size)
|
|
|
|
{
|
|
|
|
long chunk;
|
|
|
|
int ret, readn;
|
|
|
|
|
|
|
|
readn = 0;
|
|
|
|
while (size > 0) {
|
|
|
|
/* get chunk that can be read */
|
linux/kernel.h: sync min, max, min3, max3 macros with Linux
U-Boot has never cared about the type when we get max/min of two
values, but Linux Kernel does. This commit gets min, max, min3, max3
macros synced with the kernel introducing type checks.
Many of references of those macros must be fixed to suppress warnings.
We have two options:
- Use min, max, min3, max3 only when the arguments have the same type
(or add casts to the arguments)
- Use min_t/max_t instead with the appropriate type for the first
argument
Signed-off-by: Masahiro Yamada <yamada.m@jp.panasonic.com>
Acked-by: Pavel Machek <pavel@denx.de>
Acked-by: Lukasz Majewski <l.majewski@samsung.com>
Tested-by: Lukasz Majewski <l.majewski@samsung.com>
[trini: Fixup arch/blackfin/lib/string.c]
Signed-off-by: Tom Rini <trini@ti.com>
2014-11-06 18:03:31 +00:00
|
|
|
chunk = min((long)size, dfu->b_left);
|
2013-03-14 05:32:48 +00:00
|
|
|
/* consume */
|
|
|
|
if (chunk > 0) {
|
|
|
|
memcpy(buf, dfu->i_buf, chunk);
|
2014-05-12 08:43:36 +00:00
|
|
|
if (dfu_hash_algo)
|
|
|
|
dfu_hash_algo->hash_update(dfu_hash_algo,
|
|
|
|
&dfu->crc, buf,
|
|
|
|
chunk, 0);
|
|
|
|
|
2013-03-14 05:32:48 +00:00
|
|
|
dfu->i_buf += chunk;
|
|
|
|
dfu->b_left -= chunk;
|
|
|
|
size -= chunk;
|
|
|
|
buf += chunk;
|
|
|
|
readn += chunk;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* all done */
|
|
|
|
if (size > 0) {
|
|
|
|
/* no more to read */
|
|
|
|
if (dfu->r_left == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
dfu->i_buf = dfu->i_buf_start;
|
|
|
|
dfu->b_left = dfu->i_buf_end - dfu->i_buf_start;
|
|
|
|
|
|
|
|
/* got to read, but buffer is empty */
|
|
|
|
if (dfu->b_left > dfu->r_left)
|
|
|
|
dfu->b_left = dfu->r_left;
|
|
|
|
ret = dfu->read_medium(dfu, dfu->offset, dfu->i_buf,
|
|
|
|
&dfu->b_left);
|
|
|
|
if (ret != 0) {
|
|
|
|
debug("%s: Read error!\n", __func__);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
dfu->offset += dfu->b_left;
|
|
|
|
dfu->r_left -= dfu->b_left;
|
|
|
|
|
|
|
|
puts("#");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return readn;
|
2012-08-06 12:41:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int dfu_read(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
debug("%s: name: %s buf: 0x%p size: 0x%x p_num: 0x%x i_buf: 0x%p\n",
|
2013-03-14 05:32:48 +00:00
|
|
|
__func__, dfu->name, buf, size, blk_seq_num, dfu->i_buf);
|
|
|
|
|
|
|
|
if (!dfu->inited) {
|
2014-06-11 22:03:34 +00:00
|
|
|
dfu->i_buf_start = dfu_get_buf(dfu);
|
2013-06-28 16:41:50 +00:00
|
|
|
if (dfu->i_buf_start == NULL)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2014-06-11 18:47:27 +00:00
|
|
|
dfu->r_left = dfu->get_medium_size(dfu);
|
|
|
|
if (dfu->r_left < 0)
|
|
|
|
return dfu->r_left;
|
2013-03-14 05:32:48 +00:00
|
|
|
|
|
|
|
debug("%s: %s %ld [B]\n", __func__, dfu->name, dfu->r_left);
|
|
|
|
|
|
|
|
dfu->i_blk_seq_num = 0;
|
|
|
|
dfu->crc = 0;
|
|
|
|
dfu->offset = 0;
|
2014-06-11 22:03:34 +00:00
|
|
|
dfu->i_buf_end = dfu_get_buf(dfu) + dfu_buf_size;
|
2013-03-14 05:32:48 +00:00
|
|
|
dfu->i_buf = dfu->i_buf_start;
|
2014-06-11 18:47:27 +00:00
|
|
|
dfu->b_left = 0;
|
2013-03-14 05:32:48 +00:00
|
|
|
|
2013-03-14 05:32:52 +00:00
|
|
|
dfu->bad_skip = 0;
|
|
|
|
|
2013-03-14 05:32:48 +00:00
|
|
|
dfu->inited = 1;
|
2012-08-06 12:41:07 +00:00
|
|
|
}
|
|
|
|
|
2013-03-14 05:32:48 +00:00
|
|
|
if (dfu->i_blk_seq_num != blk_seq_num) {
|
2012-08-06 12:41:07 +00:00
|
|
|
printf("%s: Wrong sequence number! [%d] [%d]\n",
|
2013-03-14 05:32:48 +00:00
|
|
|
__func__, dfu->i_blk_seq_num, blk_seq_num);
|
2012-08-06 12:41:07 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2013-03-14 05:32:48 +00:00
|
|
|
/* handle rollover */
|
|
|
|
dfu->i_blk_seq_num = (dfu->i_blk_seq_num + 1) & 0xffff;
|
2012-08-06 12:41:07 +00:00
|
|
|
|
2013-03-14 05:32:48 +00:00
|
|
|
ret = dfu_read_buffer_fill(dfu, buf, size);
|
|
|
|
if (ret < 0) {
|
|
|
|
printf("%s: Failed to fill buffer\n", __func__);
|
|
|
|
return -1;
|
2012-08-06 12:41:07 +00:00
|
|
|
}
|
2013-03-14 05:32:48 +00:00
|
|
|
|
|
|
|
if (ret < size) {
|
2014-05-12 08:43:36 +00:00
|
|
|
if (dfu_hash_algo)
|
|
|
|
debug("%s: %s %s: 0x%x\n", __func__, dfu->name,
|
|
|
|
dfu_hash_algo->name, dfu->crc);
|
2013-03-14 05:32:48 +00:00
|
|
|
puts("\nUPLOAD ... done\nCtrl+C to exit ...\n");
|
|
|
|
|
|
|
|
dfu->i_blk_seq_num = 0;
|
|
|
|
dfu->crc = 0;
|
|
|
|
dfu->offset = 0;
|
|
|
|
dfu->i_buf_start = dfu_buf;
|
2013-06-12 04:05:51 +00:00
|
|
|
dfu->i_buf_end = dfu_buf;
|
2013-03-14 05:32:48 +00:00
|
|
|
dfu->i_buf = dfu->i_buf_start;
|
|
|
|
dfu->b_left = 0;
|
|
|
|
|
2013-03-14 05:32:52 +00:00
|
|
|
dfu->bad_skip = 0;
|
|
|
|
|
2013-03-14 05:32:48 +00:00
|
|
|
dfu->inited = 0;
|
|
|
|
}
|
|
|
|
|
2012-08-06 12:41:07 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int dfu_fill_entity(struct dfu_entity *dfu, char *s, int alt,
|
2014-06-11 22:03:33 +00:00
|
|
|
char *interface, char *devstr)
|
2012-08-06 12:41:07 +00:00
|
|
|
{
|
|
|
|
char *st;
|
|
|
|
|
2014-06-11 22:03:33 +00:00
|
|
|
debug("%s: %s interface: %s dev: %s\n", __func__, s, interface, devstr);
|
2012-08-06 12:41:07 +00:00
|
|
|
st = strsep(&s, " ");
|
|
|
|
strcpy(dfu->name, st);
|
|
|
|
|
|
|
|
dfu->alt = alt;
|
2014-06-11 22:03:34 +00:00
|
|
|
dfu->max_buf_size = 0;
|
2014-06-11 22:03:35 +00:00
|
|
|
dfu->free_entity = NULL;
|
2012-08-06 12:41:07 +00:00
|
|
|
|
|
|
|
/* Specific for mmc device */
|
|
|
|
if (strcmp(interface, "mmc") == 0) {
|
2014-06-11 22:03:33 +00:00
|
|
|
if (dfu_fill_entity_mmc(dfu, devstr, s))
|
2012-08-06 12:41:07 +00:00
|
|
|
return -1;
|
2013-03-14 05:32:52 +00:00
|
|
|
} else if (strcmp(interface, "nand") == 0) {
|
2014-06-11 22:03:33 +00:00
|
|
|
if (dfu_fill_entity_nand(dfu, devstr, s))
|
2013-03-14 05:32:52 +00:00
|
|
|
return -1;
|
2013-09-17 19:45:24 +00:00
|
|
|
} else if (strcmp(interface, "ram") == 0) {
|
2014-06-11 22:03:33 +00:00
|
|
|
if (dfu_fill_entity_ram(dfu, devstr, s))
|
2013-09-17 19:45:24 +00:00
|
|
|
return -1;
|
2014-06-11 22:03:36 +00:00
|
|
|
} else if (strcmp(interface, "sf") == 0) {
|
|
|
|
if (dfu_fill_entity_sf(dfu, devstr, s))
|
|
|
|
return -1;
|
2012-08-06 12:41:07 +00:00
|
|
|
} else {
|
|
|
|
printf("%s: Device %s not (yet) supported!\n",
|
|
|
|
__func__, interface);
|
|
|
|
return -1;
|
|
|
|
}
|
dfu: don't keep freeing/reallocating
DFU currently allocates buffer memory at the start of each data transfer
operation and frees it at the end. Especially since memalign() is used to
allocate the buffer, and various other allocations happen during the
transfer, this can expose the code to heap fragmentation, which prevents
the allocation from succeeding on subsequent transfers.
Fix the code to allocate the buffer once when DFU mode is initialized,
and free the buffer once when DFU mode is exited, to reduce the exposure
to heap fragmentation.
The failure mode is:
// Internally to memalign(), this allocates a lot more than s to guarantee
// that alignment can occur, then returns chunks of memory at the start/
// end of the allocated buffer to the heap.
p = memalign(a, s);
// Various other malloc()s occur here, some of which allocate the RAM
// immediately before/after "p".
//
// DFU transfer is complete, so buffer is released.
free(p);
// By chance, no other malloc()/free() here, in DFU at least.
//
// A new DFU transfer starts, so the buffer is allocated again.
// In theory this should succeed since we just free()d a buffer of the
// same size. However, this fails because memalign() internally attempts
// to allocate much more than "s", yet free(p) above only free()d a
// little more than "s".
p = memalign(a, s);
Signed-off-by: Stephen Warren <swarren@nvidia.com>
Acked-by: Lukasz Majewski <l.majewski@samsung.com>
Tested-by: Lukasz Majewski <l.majewski@samsung.com>
2015-09-05 04:03:46 +00:00
|
|
|
dfu_get_buf(dfu);
|
2012-08-06 12:41:07 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void dfu_free_entities(void)
|
|
|
|
{
|
|
|
|
struct dfu_entity *dfu, *p, *t = NULL;
|
|
|
|
|
dfu: don't keep freeing/reallocating
DFU currently allocates buffer memory at the start of each data transfer
operation and frees it at the end. Especially since memalign() is used to
allocate the buffer, and various other allocations happen during the
transfer, this can expose the code to heap fragmentation, which prevents
the allocation from succeeding on subsequent transfers.
Fix the code to allocate the buffer once when DFU mode is initialized,
and free the buffer once when DFU mode is exited, to reduce the exposure
to heap fragmentation.
The failure mode is:
// Internally to memalign(), this allocates a lot more than s to guarantee
// that alignment can occur, then returns chunks of memory at the start/
// end of the allocated buffer to the heap.
p = memalign(a, s);
// Various other malloc()s occur here, some of which allocate the RAM
// immediately before/after "p".
//
// DFU transfer is complete, so buffer is released.
free(p);
// By chance, no other malloc()/free() here, in DFU at least.
//
// A new DFU transfer starts, so the buffer is allocated again.
// In theory this should succeed since we just free()d a buffer of the
// same size. However, this fails because memalign() internally attempts
// to allocate much more than "s", yet free(p) above only free()d a
// little more than "s".
p = memalign(a, s);
Signed-off-by: Stephen Warren <swarren@nvidia.com>
Acked-by: Lukasz Majewski <l.majewski@samsung.com>
Tested-by: Lukasz Majewski <l.majewski@samsung.com>
2015-09-05 04:03:46 +00:00
|
|
|
dfu_free_buf();
|
2012-08-06 12:41:07 +00:00
|
|
|
list_for_each_entry_safe_reverse(dfu, p, &dfu_list, list) {
|
|
|
|
list_del(&dfu->list);
|
2014-06-11 22:03:35 +00:00
|
|
|
if (dfu->free_entity)
|
|
|
|
dfu->free_entity(dfu);
|
2012-08-06 12:41:07 +00:00
|
|
|
t = dfu;
|
|
|
|
}
|
|
|
|
if (t)
|
|
|
|
free(t);
|
|
|
|
INIT_LIST_HEAD(&dfu_list);
|
2014-02-28 17:53:37 +00:00
|
|
|
|
|
|
|
alt_num_cnt = 0;
|
2012-08-06 12:41:07 +00:00
|
|
|
}
|
|
|
|
|
2014-06-11 22:03:33 +00:00
|
|
|
int dfu_config_entities(char *env, char *interface, char *devstr)
|
2012-08-06 12:41:07 +00:00
|
|
|
{
|
|
|
|
struct dfu_entity *dfu;
|
|
|
|
int i, ret;
|
|
|
|
char *s;
|
|
|
|
|
|
|
|
dfu_alt_num = dfu_find_alt_num(env);
|
|
|
|
debug("%s: dfu_alt_num=%d\n", __func__, dfu_alt_num);
|
|
|
|
|
2014-05-12 08:43:36 +00:00
|
|
|
dfu_hash_algo = NULL;
|
|
|
|
s = dfu_get_hash_algo();
|
|
|
|
if (s) {
|
|
|
|
ret = hash_lookup_algo(s, &dfu_hash_algo);
|
|
|
|
if (ret)
|
|
|
|
error("Hash algorithm %s not supported\n", s);
|
|
|
|
}
|
|
|
|
|
2012-08-06 12:41:07 +00:00
|
|
|
dfu = calloc(sizeof(*dfu), dfu_alt_num);
|
|
|
|
if (!dfu)
|
|
|
|
return -1;
|
|
|
|
for (i = 0; i < dfu_alt_num; i++) {
|
|
|
|
|
|
|
|
s = strsep(&env, ";");
|
2014-06-11 22:03:33 +00:00
|
|
|
ret = dfu_fill_entity(&dfu[i], s, alt_num_cnt, interface,
|
|
|
|
devstr);
|
2016-05-03 02:24:52 +00:00
|
|
|
if (ret) {
|
|
|
|
free(dfu);
|
2012-08-06 12:41:07 +00:00
|
|
|
return -1;
|
2016-05-03 02:24:52 +00:00
|
|
|
}
|
2012-08-06 12:41:07 +00:00
|
|
|
|
|
|
|
list_add_tail(&dfu[i].list, &dfu_list);
|
2014-02-28 17:53:37 +00:00
|
|
|
alt_num_cnt++;
|
2012-08-06 12:41:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *dfu_get_dev_type(enum dfu_device_type t)
|
|
|
|
{
|
2016-10-28 07:44:26 +00:00
|
|
|
const char *dev_t[] = {NULL, "eMMC", "OneNAND", "NAND", "RAM", "SF" };
|
2012-08-06 12:41:07 +00:00
|
|
|
return dev_t[t];
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *dfu_get_layout(enum dfu_layout l)
|
|
|
|
{
|
|
|
|
const char *dfu_layout[] = {NULL, "RAW_ADDR", "FAT", "EXT2",
|
2013-09-17 19:45:24 +00:00
|
|
|
"EXT3", "EXT4", "RAM_ADDR" };
|
2012-08-06 12:41:07 +00:00
|
|
|
return dfu_layout[l];
|
|
|
|
}
|
|
|
|
|
|
|
|
void dfu_show_entities(void)
|
|
|
|
{
|
|
|
|
struct dfu_entity *dfu;
|
|
|
|
|
|
|
|
puts("DFU alt settings list:\n");
|
|
|
|
|
|
|
|
list_for_each_entry(dfu, &dfu_list, list) {
|
|
|
|
printf("dev: %s alt: %d name: %s layout: %s\n",
|
|
|
|
dfu_get_dev_type(dfu->dev_type), dfu->alt,
|
|
|
|
dfu->name, dfu_get_layout(dfu->layout));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int dfu_get_alt_number(void)
|
|
|
|
{
|
|
|
|
return dfu_alt_num;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct dfu_entity *dfu_get_entity(int alt)
|
|
|
|
{
|
|
|
|
struct dfu_entity *dfu;
|
|
|
|
|
|
|
|
list_for_each_entry(dfu, &dfu_list, list) {
|
|
|
|
if (dfu->alt == alt)
|
|
|
|
return dfu;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
2013-10-08 12:30:38 +00:00
|
|
|
|
|
|
|
int dfu_get_alt(char *name)
|
|
|
|
{
|
|
|
|
struct dfu_entity *dfu;
|
2014-11-05 09:54:16 +00:00
|
|
|
char *str;
|
2013-10-08 12:30:38 +00:00
|
|
|
|
|
|
|
list_for_each_entry(dfu, &dfu_list, list) {
|
2014-11-05 09:54:16 +00:00
|
|
|
if (dfu->name[0] != '/') {
|
|
|
|
if (!strncmp(dfu->name, name, strlen(dfu->name)))
|
|
|
|
return dfu->alt;
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* One must also consider absolute path
|
|
|
|
* (/boot/bin/uImage) available at dfu->name when
|
|
|
|
* compared "plain" file name (uImage)
|
|
|
|
*
|
|
|
|
* It is the case for e.g. thor gadget where lthor SW
|
|
|
|
* sends only the file name, so only the very last part
|
|
|
|
* of path must be checked for equality
|
|
|
|
*/
|
|
|
|
|
|
|
|
str = strstr(dfu->name, name);
|
|
|
|
if (!str)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check if matching substring is the last element of
|
|
|
|
* dfu->name (uImage)
|
|
|
|
*/
|
|
|
|
if (strlen(dfu->name) ==
|
|
|
|
((str - dfu->name) + strlen(name)))
|
|
|
|
return dfu->alt;
|
|
|
|
}
|
2013-10-08 12:30:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
2015-08-23 22:21:46 +00:00
|
|
|
|
|
|
|
int dfu_write_from_mem_addr(struct dfu_entity *dfu, void *buf, int size)
|
|
|
|
{
|
|
|
|
unsigned long dfu_buf_size, write, left = size;
|
|
|
|
int i, ret = 0;
|
|
|
|
void *dp = buf;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Here we must call dfu_get_buf(dfu) first to be sure that dfu_buf_size
|
|
|
|
* has been properly initialized - e.g. if "dfu_bufsiz" has been taken
|
|
|
|
* into account.
|
|
|
|
*/
|
|
|
|
dfu_get_buf(dfu);
|
|
|
|
dfu_buf_size = dfu_get_buf_size();
|
|
|
|
debug("%s: dfu buf size: %lu\n", __func__, dfu_buf_size);
|
|
|
|
|
|
|
|
for (i = 0; left > 0; i++) {
|
|
|
|
write = min(dfu_buf_size, left);
|
|
|
|
|
|
|
|
debug("%s: dp: 0x%p left: %lu write: %lu\n", __func__,
|
|
|
|
dp, left, write);
|
|
|
|
ret = dfu_write(dfu, dp, write, i);
|
|
|
|
if (ret) {
|
|
|
|
error("DFU write failed\n");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
dp += write;
|
|
|
|
left -= write;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = dfu_flush(dfu, NULL, 0, i);
|
|
|
|
if (ret)
|
|
|
|
error("DFU flush failed!");
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|