dfu.c 10.2 KB
Newer Older
1 2 3 4 5 6
/*
 * dfu.c -- DFU back-end routines
 *
 * Copyright (C) 2012 Samsung Electronics
 * author: Lukasz Majewski <l.majewski@samsung.com>
 *
7
 * SPDX-License-Identifier:	GPL-2.0+
8 9 10
 */

#include <common.h>
11
#include <errno.h>
12 13 14 15
#include <malloc.h>
#include <mmc.h>
#include <fat.h>
#include <dfu.h>
16
#include <hash.h>
17 18 19
#include <linux/list.h>
#include <linux/compiler.h>

20
static bool dfu_reset_request;
21 22
static LIST_HEAD(dfu_list);
static int dfu_alt_num;
23
static int alt_num_cnt;
24
static struct hash_algo *dfu_hash_algo;
25

26 27 28 29 30 31 32 33 34 35
bool dfu_reset(void)
{
	return dfu_reset_request;
}

void dfu_trigger_reset()
{
	dfu_reset_request = true;
}

36 37 38 39 40 41 42 43 44 45 46
static int dfu_find_alt_num(const char *s)
{
	int i = 0;

	for (; *s; s++)
		if (*s == ';')
			i++;

	return ++i;
}

47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
int dfu_init_env_entities(char *interface, int dev)
{
	const char *str_env;
	char *env_bkp;
	int ret;

	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);
	ret = dfu_config_entities(env_bkp, interface, dev);
	if (ret) {
		error("DFU entities configuration failed!\n");
		return ret;
	}

	free(env_bkp);
	return 0;
}

70 71 72
static unsigned char *dfu_buf;
static unsigned long dfu_buf_size = CONFIG_SYS_DFU_DATA_BUF_SIZE;

73
unsigned char *dfu_free_buf(void)
74 75 76 77 78 79
{
	free(dfu_buf);
	dfu_buf = NULL;
	return dfu_buf;
}

80 81 82 83 84
unsigned long dfu_get_buf_size(void)
{
	return dfu_buf_size;
}

85
unsigned char *dfu_get_buf(void)
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
{
	char *s;

	if (dfu_buf != NULL)
		return dfu_buf;

	s = getenv("dfu_bufsiz");
	dfu_buf_size = s ? (unsigned long)simple_strtol(s, NULL, 16) :
			CONFIG_SYS_DFU_DATA_BUF_SIZE;

	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;
}
103

104 105 106 107 108
static char *dfu_get_hash_algo(void)
{
	char *s;

	s = getenv("dfu_hash_algo");
109
	if (!s)
110 111
		return NULL;

112 113 114 115 116 117
	if (!strcmp(s, "crc32")) {
		debug("%s: DFU hash method: %s\n", __func__, s);
		return s;
	}

	error("DFU hash method: %s not supported!\n", s);
118 119 120
	return NULL;
}

121 122 123 124 125 126 127 128 129 130
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;

131 132 133
	if (dfu_hash_algo)
		dfu_hash_algo->hash_update(dfu_hash_algo, &dfu->crc,
					   dfu->i_buf_start, w_size, 0);
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149

	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;
}

150 151 152 153
int dfu_flush(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
{
	int ret = 0;

154 155 156 157
	ret = dfu_write_buffer_drain(dfu);
	if (ret)
		return ret;

158 159 160
	if (dfu->flush_medium)
		ret = dfu->flush_medium(dfu);

161 162 163
	if (dfu_hash_algo)
		printf("\nDFU complete %s: 0x%08x\n", dfu_hash_algo->name,
		       dfu->crc);
164 165 166 167 168 169 170 171 172 173 174 175 176 177

	/* 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;

	return ret;
}

178 179 180
int dfu_write(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
{
	int ret = 0;
181
	int tret;
182

183 184 185
	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,
	      dfu->i_buf - dfu->i_buf_start);
186

187 188 189 190
	if (!dfu->inited) {
		/* initial state */
		dfu->crc = 0;
		dfu->offset = 0;
191
		dfu->bad_skip = 0;
192
		dfu->i_blk_seq_num = 0;
193 194 195 196
		dfu->i_buf_start = dfu_get_buf();
		if (dfu->i_buf_start == NULL)
			return -ENOMEM;
		dfu->i_buf_end = dfu_get_buf() + dfu_buf_size;
197 198 199
		dfu->i_buf = dfu->i_buf_start;

		dfu->inited = 1;
200 201
	}

202
	if (dfu->i_blk_seq_num != blk_seq_num) {
203
		printf("%s: Wrong sequence number! [%d] [%d]\n",
204
		       __func__, dfu->i_blk_seq_num, blk_seq_num);
205 206 207
		return -1;
	}

208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
	/* 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) {
		tret = dfu_write_buffer_drain(dfu);
		if (ret == 0)
			ret = tret;
	}
230

231 232
	/* we should be in buffer now (if not then size too large) */
	if ((dfu->i_buf + size) > dfu->i_buf_end) {
233 234
		error("Buffer overflow! (0x%p + 0x%x > 0x%p)\n", dfu->i_buf,
		      size, dfu->i_buf_end);
235 236 237 238 239 240 241 242 243 244 245 246 247
		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) {
		tret = dfu_write_buffer_drain(dfu);
		if (ret == 0)
			ret = tret;
	}

248
	return ret;
249 250 251 252 253 254 255 256 257 258 259 260 261 262
}

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 */
		chunk = min(size, dfu->b_left);
		/* consume */
		if (chunk > 0) {
			memcpy(buf, dfu->i_buf, chunk);
263 264 265 266 267
			if (dfu_hash_algo)
				dfu_hash_algo->hash_update(dfu_hash_algo,
							   &dfu->crc, buf,
							   chunk, 0);

268 269
			dfu->i_buf += chunk;
			dfu->b_left -= chunk;
270
			dfu->r_left -= chunk;
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
			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;
302 303 304 305 306 307 308
}

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",
309 310 311
	       __func__, dfu->name, buf, size, blk_seq_num, dfu->i_buf);

	if (!dfu->inited) {
312 313 314 315 316
		dfu->i_buf_start = dfu_get_buf();
		if (dfu->i_buf_start == NULL)
			return -ENOMEM;

		ret = dfu->read_medium(dfu, 0, dfu->i_buf_start, &dfu->r_left);
317 318 319 320 321 322 323 324 325 326
		if (ret != 0) {
			debug("%s: failed to get r_left\n", __func__);
			return ret;
		}

		debug("%s: %s %ld [B]\n", __func__, dfu->name, dfu->r_left);

		dfu->i_blk_seq_num = 0;
		dfu->crc = 0;
		dfu->offset = 0;
327
		dfu->i_buf_end = dfu_get_buf() + dfu_buf_size;
328
		dfu->i_buf = dfu->i_buf_start;
329
		dfu->b_left = min(dfu_buf_size, dfu->r_left);
330

331 332
		dfu->bad_skip = 0;

333
		dfu->inited = 1;
334 335
	}

336
	if (dfu->i_blk_seq_num != blk_seq_num) {
337
		printf("%s: Wrong sequence number! [%d] [%d]\n",
338
		       __func__, dfu->i_blk_seq_num, blk_seq_num);
339 340
		return -1;
	}
341 342
	/* handle rollover */
	dfu->i_blk_seq_num = (dfu->i_blk_seq_num + 1) & 0xffff;
343

344 345 346 347
	ret = dfu_read_buffer_fill(dfu, buf, size);
	if (ret < 0) {
		printf("%s: Failed to fill buffer\n", __func__);
		return -1;
348
	}
349 350

	if (ret < size) {
351 352 353
		if (dfu_hash_algo)
			debug("%s: %s %s: 0x%x\n", __func__, dfu->name,
			      dfu_hash_algo->name, dfu->crc);
354 355
		puts("\nUPLOAD ... done\nCtrl+C to exit ...\n");

356
		dfu_free_buf();
357 358 359 360
		dfu->i_blk_seq_num = 0;
		dfu->crc = 0;
		dfu->offset = 0;
		dfu->i_buf_start = dfu_buf;
361
		dfu->i_buf_end = dfu_buf;
362 363 364
		dfu->i_buf = dfu->i_buf_start;
		dfu->b_left = 0;

365 366
		dfu->bad_skip = 0;

367 368 369
		dfu->inited = 0;
	}

370 371 372 373
	return ret;
}

static int dfu_fill_entity(struct dfu_entity *dfu, char *s, int alt,
374
			   char *interface, int num)
375 376 377 378 379 380 381 382 383 384 385 386 387 388
{
	char *st;

	debug("%s: %s interface: %s num: %d\n", __func__, s, interface, num);
	st = strsep(&s, " ");
	strcpy(dfu->name, st);

	dfu->dev_num = num;
	dfu->alt = alt;

	/* Specific for mmc device */
	if (strcmp(interface, "mmc") == 0) {
		if (dfu_fill_entity_mmc(dfu, s))
			return -1;
389 390 391
	} else if (strcmp(interface, "nand") == 0) {
		if (dfu_fill_entity_nand(dfu, s))
			return -1;
Afzal Mohammed's avatar
Afzal Mohammed committed
392 393 394
	} else if (strcmp(interface, "ram") == 0) {
		if (dfu_fill_entity_ram(dfu, s))
			return -1;
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
	} else {
		printf("%s: Device %s not (yet) supported!\n",
		       __func__,  interface);
		return -1;
	}

	return 0;
}

void dfu_free_entities(void)
{
	struct dfu_entity *dfu, *p, *t = NULL;

	list_for_each_entry_safe_reverse(dfu, p, &dfu_list, list) {
		list_del(&dfu->list);
		t = dfu;
	}
	if (t)
		free(t);
	INIT_LIST_HEAD(&dfu_list);
415 416

	alt_num_cnt = 0;
417 418 419 420 421 422 423 424 425 426 427
}

int dfu_config_entities(char *env, char *interface, int num)
{
	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);

428 429 430 431 432 433 434 435
	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);
	}

436 437 438 439 440 441
	dfu = calloc(sizeof(*dfu), dfu_alt_num);
	if (!dfu)
		return -1;
	for (i = 0; i < dfu_alt_num; i++) {

		s = strsep(&env, ";");
442
		ret = dfu_fill_entity(&dfu[i], s, alt_num_cnt, interface, num);
443 444 445 446
		if (ret)
			return -1;

		list_add_tail(&dfu[i].list, &dfu_list);
447
		alt_num_cnt++;
448 449 450 451 452 453 454
	}

	return 0;
}

const char *dfu_get_dev_type(enum dfu_device_type t)
{
Afzal Mohammed's avatar
Afzal Mohammed committed
455
	const char *dev_t[] = {NULL, "eMMC", "OneNAND", "NAND", "RAM" };
456 457 458 459 460 461
	return dev_t[t];
}

const char *dfu_get_layout(enum dfu_layout l)
{
	const char *dfu_layout[] = {NULL, "RAW_ADDR", "FAT", "EXT2",
Afzal Mohammed's avatar
Afzal Mohammed committed
462
					   "EXT3", "EXT4", "RAM_ADDR" };
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
	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;
}
495 496 497 498 499 500 501 502 503 504 505 506

int dfu_get_alt(char *name)
{
	struct dfu_entity *dfu;

	list_for_each_entry(dfu, &dfu_list, list) {
		if (!strncmp(dfu->name, name, strlen(dfu->name)))
			return dfu->alt;
	}

	return -ENODEV;
}