xref: /openbmc/u-boot/drivers/dfu/dfu.c (revision 86a390d3)
1 /*
2  * dfu.c -- DFU back-end routines
3  *
4  * Copyright (C) 2012 Samsung Electronics
5  * author: Lukasz Majewski <l.majewski@samsung.com>
6  *
7  * SPDX-License-Identifier:	GPL-2.0+
8  */
9 
10 #include <common.h>
11 #include <errno.h>
12 #include <malloc.h>
13 #include <mmc.h>
14 #include <fat.h>
15 #include <dfu.h>
16 #include <hash.h>
17 #include <linux/list.h>
18 #include <linux/compiler.h>
19 
20 static LIST_HEAD(dfu_list);
21 static int dfu_alt_num;
22 static int alt_num_cnt;
23 static struct hash_algo *dfu_hash_algo;
24 
25 /*
26  * The purpose of the dfu_usb_get_reset() function is to
27  * provide information if after USB_DETACH request
28  * being sent the dfu-util performed reset of USB
29  * bus.
30  *
31  * Described behaviour is the only way to distinct if
32  * user has typed -e (detach) or -R (reset) when invoking
33  * dfu-util command.
34  *
35  */
36 __weak bool dfu_usb_get_reset(void)
37 {
38 	return true;
39 }
40 
41 static int dfu_find_alt_num(const char *s)
42 {
43 	int i = 0;
44 
45 	for (; *s; s++)
46 		if (*s == ';')
47 			i++;
48 
49 	return ++i;
50 }
51 
52 int dfu_init_env_entities(char *interface, char *devstr)
53 {
54 	const char *str_env;
55 	char *env_bkp;
56 	int ret;
57 
58 	str_env = getenv("dfu_alt_info");
59 	if (!str_env) {
60 		error("\"dfu_alt_info\" env variable not defined!\n");
61 		return -EINVAL;
62 	}
63 
64 	env_bkp = strdup(str_env);
65 	ret = dfu_config_entities(env_bkp, interface, devstr);
66 	if (ret) {
67 		error("DFU entities configuration failed!\n");
68 		return ret;
69 	}
70 
71 	free(env_bkp);
72 	return 0;
73 }
74 
75 static unsigned char *dfu_buf;
76 static unsigned long dfu_buf_size = CONFIG_SYS_DFU_DATA_BUF_SIZE;
77 
78 unsigned char *dfu_free_buf(void)
79 {
80 	free(dfu_buf);
81 	dfu_buf = NULL;
82 	return dfu_buf;
83 }
84 
85 unsigned long dfu_get_buf_size(void)
86 {
87 	return dfu_buf_size;
88 }
89 
90 unsigned char *dfu_get_buf(struct dfu_entity *dfu)
91 {
92 	char *s;
93 
94 	if (dfu_buf != NULL)
95 		return dfu_buf;
96 
97 	s = getenv("dfu_bufsiz");
98 	if (s)
99 		dfu_buf_size = (unsigned long)simple_strtol(s, NULL, 0);
100 
101 	if (!s || !dfu_buf_size)
102 		dfu_buf_size = CONFIG_SYS_DFU_DATA_BUF_SIZE;
103 
104 	if (dfu->max_buf_size && dfu_buf_size > dfu->max_buf_size)
105 		dfu_buf_size = dfu->max_buf_size;
106 
107 	dfu_buf = memalign(CONFIG_SYS_CACHELINE_SIZE, dfu_buf_size);
108 	if (dfu_buf == NULL)
109 		printf("%s: Could not memalign 0x%lx bytes\n",
110 		       __func__, dfu_buf_size);
111 
112 	return dfu_buf;
113 }
114 
115 static char *dfu_get_hash_algo(void)
116 {
117 	char *s;
118 
119 	s = getenv("dfu_hash_algo");
120 	if (!s)
121 		return NULL;
122 
123 	if (!strcmp(s, "crc32")) {
124 		debug("%s: DFU hash method: %s\n", __func__, s);
125 		return s;
126 	}
127 
128 	error("DFU hash method: %s not supported!\n", s);
129 	return NULL;
130 }
131 
132 static int dfu_write_buffer_drain(struct dfu_entity *dfu)
133 {
134 	long w_size;
135 	int ret;
136 
137 	/* flush size? */
138 	w_size = dfu->i_buf - dfu->i_buf_start;
139 	if (w_size == 0)
140 		return 0;
141 
142 	if (dfu_hash_algo)
143 		dfu_hash_algo->hash_update(dfu_hash_algo, &dfu->crc,
144 					   dfu->i_buf_start, w_size, 0);
145 
146 	ret = dfu->write_medium(dfu, dfu->offset, dfu->i_buf_start, &w_size);
147 	if (ret)
148 		debug("%s: Write error!\n", __func__);
149 
150 	/* point back */
151 	dfu->i_buf = dfu->i_buf_start;
152 
153 	/* update offset */
154 	dfu->offset += w_size;
155 
156 	puts("#");
157 
158 	return ret;
159 }
160 
161 void dfu_write_transaction_cleanup(struct dfu_entity *dfu)
162 {
163 	/* clear everything */
164 	dfu_free_buf();
165 	dfu->crc = 0;
166 	dfu->offset = 0;
167 	dfu->i_blk_seq_num = 0;
168 	dfu->i_buf_start = dfu_buf;
169 	dfu->i_buf_end = dfu_buf;
170 	dfu->i_buf = dfu->i_buf_start;
171 	dfu->inited = 0;
172 }
173 
174 int dfu_flush(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
175 {
176 	int ret = 0;
177 
178 	ret = dfu_write_buffer_drain(dfu);
179 	if (ret)
180 		return ret;
181 
182 	if (dfu->flush_medium)
183 		ret = dfu->flush_medium(dfu);
184 
185 	if (dfu_hash_algo)
186 		printf("\nDFU complete %s: 0x%08x\n", dfu_hash_algo->name,
187 		       dfu->crc);
188 
189 	dfu_write_transaction_cleanup(dfu);
190 
191 	return ret;
192 }
193 
194 int dfu_write(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
195 {
196 	int ret;
197 
198 	debug("%s: name: %s buf: 0x%p size: 0x%x p_num: 0x%x offset: 0x%llx bufoffset: 0x%x\n",
199 	      __func__, dfu->name, buf, size, blk_seq_num, dfu->offset,
200 	      dfu->i_buf - dfu->i_buf_start);
201 
202 	if (!dfu->inited) {
203 		/* initial state */
204 		dfu->crc = 0;
205 		dfu->offset = 0;
206 		dfu->bad_skip = 0;
207 		dfu->i_blk_seq_num = 0;
208 		dfu->i_buf_start = dfu_get_buf(dfu);
209 		if (dfu->i_buf_start == NULL)
210 			return -ENOMEM;
211 		dfu->i_buf_end = dfu_get_buf(dfu) + dfu_buf_size;
212 		dfu->i_buf = dfu->i_buf_start;
213 
214 		dfu->inited = 1;
215 	}
216 
217 	if (dfu->i_blk_seq_num != blk_seq_num) {
218 		printf("%s: Wrong sequence number! [%d] [%d]\n",
219 		       __func__, dfu->i_blk_seq_num, blk_seq_num);
220 		dfu_write_transaction_cleanup(dfu);
221 		return -1;
222 	}
223 
224 	/* DFU 1.1 standard says:
225 	 * The wBlockNum field is a block sequence number. It increments each
226 	 * time a block is transferred, wrapping to zero from 65,535. It is used
227 	 * to provide useful context to the DFU loader in the device."
228 	 *
229 	 * This means that it's a 16 bit counter that roll-overs at
230 	 * 0xffff -> 0x0000. By having a typical 4K transfer block
231 	 * we roll-over at exactly 256MB. Not very fun to debug.
232 	 *
233 	 * Handling rollover, and having an inited variable,
234 	 * makes things work.
235 	 */
236 
237 	/* handle rollover */
238 	dfu->i_blk_seq_num = (dfu->i_blk_seq_num + 1) & 0xffff;
239 
240 	/* flush buffer if overflow */
241 	if ((dfu->i_buf + size) > dfu->i_buf_end) {
242 		ret = dfu_write_buffer_drain(dfu);
243 		if (ret) {
244 			dfu_write_transaction_cleanup(dfu);
245 			return ret;
246 		}
247 	}
248 
249 	/* we should be in buffer now (if not then size too large) */
250 	if ((dfu->i_buf + size) > dfu->i_buf_end) {
251 		error("Buffer overflow! (0x%p + 0x%x > 0x%p)\n", dfu->i_buf,
252 		      size, dfu->i_buf_end);
253 		dfu_write_transaction_cleanup(dfu);
254 		return -1;
255 	}
256 
257 	memcpy(dfu->i_buf, buf, size);
258 	dfu->i_buf += size;
259 
260 	/* if end or if buffer full flush */
261 	if (size == 0 || (dfu->i_buf + size) > dfu->i_buf_end) {
262 		ret = dfu_write_buffer_drain(dfu);
263 		if (ret) {
264 			dfu_write_transaction_cleanup(dfu);
265 			return ret;
266 		}
267 	}
268 
269 	return 0;
270 }
271 
272 static int dfu_read_buffer_fill(struct dfu_entity *dfu, void *buf, int size)
273 {
274 	long chunk;
275 	int ret, readn;
276 
277 	readn = 0;
278 	while (size > 0) {
279 		/* get chunk that can be read */
280 		chunk = min((long)size, dfu->b_left);
281 		/* consume */
282 		if (chunk > 0) {
283 			memcpy(buf, dfu->i_buf, chunk);
284 			if (dfu_hash_algo)
285 				dfu_hash_algo->hash_update(dfu_hash_algo,
286 							   &dfu->crc, buf,
287 							   chunk, 0);
288 
289 			dfu->i_buf += chunk;
290 			dfu->b_left -= chunk;
291 			size -= chunk;
292 			buf += chunk;
293 			readn += chunk;
294 		}
295 
296 		/* all done */
297 		if (size > 0) {
298 			/* no more to read */
299 			if (dfu->r_left == 0)
300 				break;
301 
302 			dfu->i_buf = dfu->i_buf_start;
303 			dfu->b_left = dfu->i_buf_end - dfu->i_buf_start;
304 
305 			/* got to read, but buffer is empty */
306 			if (dfu->b_left > dfu->r_left)
307 				dfu->b_left = dfu->r_left;
308 			ret = dfu->read_medium(dfu, dfu->offset, dfu->i_buf,
309 					&dfu->b_left);
310 			if (ret != 0) {
311 				debug("%s: Read error!\n", __func__);
312 				return ret;
313 			}
314 			dfu->offset += dfu->b_left;
315 			dfu->r_left -= dfu->b_left;
316 
317 			puts("#");
318 		}
319 	}
320 
321 	return readn;
322 }
323 
324 int dfu_read(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
325 {
326 	int ret = 0;
327 
328 	debug("%s: name: %s buf: 0x%p size: 0x%x p_num: 0x%x i_buf: 0x%p\n",
329 	       __func__, dfu->name, buf, size, blk_seq_num, dfu->i_buf);
330 
331 	if (!dfu->inited) {
332 		dfu->i_buf_start = dfu_get_buf(dfu);
333 		if (dfu->i_buf_start == NULL)
334 			return -ENOMEM;
335 
336 		dfu->r_left = dfu->get_medium_size(dfu);
337 		if (dfu->r_left < 0)
338 			return dfu->r_left;
339 		switch (dfu->layout) {
340 		case DFU_RAW_ADDR:
341 		case DFU_RAM_ADDR:
342 			break;
343 		default:
344 			if (dfu->r_left > dfu_buf_size) {
345 				printf("%s: File too big for buffer\n",
346 				       __func__);
347 				return -EOVERFLOW;
348 			}
349 		}
350 
351 		debug("%s: %s %ld [B]\n", __func__, dfu->name, dfu->r_left);
352 
353 		dfu->i_blk_seq_num = 0;
354 		dfu->crc = 0;
355 		dfu->offset = 0;
356 		dfu->i_buf_end = dfu_get_buf(dfu) + dfu_buf_size;
357 		dfu->i_buf = dfu->i_buf_start;
358 		dfu->b_left = 0;
359 
360 		dfu->bad_skip = 0;
361 
362 		dfu->inited = 1;
363 	}
364 
365 	if (dfu->i_blk_seq_num != blk_seq_num) {
366 		printf("%s: Wrong sequence number! [%d] [%d]\n",
367 		       __func__, dfu->i_blk_seq_num, blk_seq_num);
368 		return -1;
369 	}
370 	/* handle rollover */
371 	dfu->i_blk_seq_num = (dfu->i_blk_seq_num + 1) & 0xffff;
372 
373 	ret = dfu_read_buffer_fill(dfu, buf, size);
374 	if (ret < 0) {
375 		printf("%s: Failed to fill buffer\n", __func__);
376 		return -1;
377 	}
378 
379 	if (ret < size) {
380 		if (dfu_hash_algo)
381 			debug("%s: %s %s: 0x%x\n", __func__, dfu->name,
382 			      dfu_hash_algo->name, dfu->crc);
383 		puts("\nUPLOAD ... done\nCtrl+C to exit ...\n");
384 
385 		dfu_free_buf();
386 		dfu->i_blk_seq_num = 0;
387 		dfu->crc = 0;
388 		dfu->offset = 0;
389 		dfu->i_buf_start = dfu_buf;
390 		dfu->i_buf_end = dfu_buf;
391 		dfu->i_buf = dfu->i_buf_start;
392 		dfu->b_left = 0;
393 
394 		dfu->bad_skip = 0;
395 
396 		dfu->inited = 0;
397 	}
398 
399 	return ret;
400 }
401 
402 static int dfu_fill_entity(struct dfu_entity *dfu, char *s, int alt,
403 			   char *interface, char *devstr)
404 {
405 	char *st;
406 
407 	debug("%s: %s interface: %s dev: %s\n", __func__, s, interface, devstr);
408 	st = strsep(&s, " ");
409 	strcpy(dfu->name, st);
410 
411 	dfu->alt = alt;
412 	dfu->max_buf_size = 0;
413 	dfu->free_entity = NULL;
414 
415 	/* Specific for mmc device */
416 	if (strcmp(interface, "mmc") == 0) {
417 		if (dfu_fill_entity_mmc(dfu, devstr, s))
418 			return -1;
419 	} else if (strcmp(interface, "nand") == 0) {
420 		if (dfu_fill_entity_nand(dfu, devstr, s))
421 			return -1;
422 	} else if (strcmp(interface, "ram") == 0) {
423 		if (dfu_fill_entity_ram(dfu, devstr, s))
424 			return -1;
425 	} else if (strcmp(interface, "sf") == 0) {
426 		if (dfu_fill_entity_sf(dfu, devstr, s))
427 			return -1;
428 	} else {
429 		printf("%s: Device %s not (yet) supported!\n",
430 		       __func__,  interface);
431 		return -1;
432 	}
433 
434 	return 0;
435 }
436 
437 void dfu_free_entities(void)
438 {
439 	struct dfu_entity *dfu, *p, *t = NULL;
440 
441 	list_for_each_entry_safe_reverse(dfu, p, &dfu_list, list) {
442 		list_del(&dfu->list);
443 		if (dfu->free_entity)
444 			dfu->free_entity(dfu);
445 		t = dfu;
446 	}
447 	if (t)
448 		free(t);
449 	INIT_LIST_HEAD(&dfu_list);
450 
451 	alt_num_cnt = 0;
452 }
453 
454 int dfu_config_entities(char *env, char *interface, char *devstr)
455 {
456 	struct dfu_entity *dfu;
457 	int i, ret;
458 	char *s;
459 
460 	dfu_alt_num = dfu_find_alt_num(env);
461 	debug("%s: dfu_alt_num=%d\n", __func__, dfu_alt_num);
462 
463 	dfu_hash_algo = NULL;
464 	s = dfu_get_hash_algo();
465 	if (s) {
466 		ret = hash_lookup_algo(s, &dfu_hash_algo);
467 		if (ret)
468 			error("Hash algorithm %s not supported\n", s);
469 	}
470 
471 	dfu = calloc(sizeof(*dfu), dfu_alt_num);
472 	if (!dfu)
473 		return -1;
474 	for (i = 0; i < dfu_alt_num; i++) {
475 
476 		s = strsep(&env, ";");
477 		ret = dfu_fill_entity(&dfu[i], s, alt_num_cnt, interface,
478 				      devstr);
479 		if (ret)
480 			return -1;
481 
482 		list_add_tail(&dfu[i].list, &dfu_list);
483 		alt_num_cnt++;
484 	}
485 
486 	return 0;
487 }
488 
489 const char *dfu_get_dev_type(enum dfu_device_type t)
490 {
491 	const char *dev_t[] = {NULL, "eMMC", "OneNAND", "NAND", "RAM" };
492 	return dev_t[t];
493 }
494 
495 const char *dfu_get_layout(enum dfu_layout l)
496 {
497 	const char *dfu_layout[] = {NULL, "RAW_ADDR", "FAT", "EXT2",
498 					   "EXT3", "EXT4", "RAM_ADDR" };
499 	return dfu_layout[l];
500 }
501 
502 void dfu_show_entities(void)
503 {
504 	struct dfu_entity *dfu;
505 
506 	puts("DFU alt settings list:\n");
507 
508 	list_for_each_entry(dfu, &dfu_list, list) {
509 		printf("dev: %s alt: %d name: %s layout: %s\n",
510 		       dfu_get_dev_type(dfu->dev_type), dfu->alt,
511 		       dfu->name, dfu_get_layout(dfu->layout));
512 	}
513 }
514 
515 int dfu_get_alt_number(void)
516 {
517 	return dfu_alt_num;
518 }
519 
520 struct dfu_entity *dfu_get_entity(int alt)
521 {
522 	struct dfu_entity *dfu;
523 
524 	list_for_each_entry(dfu, &dfu_list, list) {
525 		if (dfu->alt == alt)
526 			return dfu;
527 	}
528 
529 	return NULL;
530 }
531 
532 int dfu_get_alt(char *name)
533 {
534 	struct dfu_entity *dfu;
535 	char *str;
536 
537 	list_for_each_entry(dfu, &dfu_list, list) {
538 		if (dfu->name[0] != '/') {
539 			if (!strncmp(dfu->name, name, strlen(dfu->name)))
540 				return dfu->alt;
541 		} else {
542 			/*
543 			 * One must also consider absolute path
544 			 * (/boot/bin/uImage) available at dfu->name when
545 			 * compared "plain" file name (uImage)
546 			 *
547 			 * It is the case for e.g. thor gadget where lthor SW
548 			 * sends only the file name, so only the very last part
549 			 * of path must be checked for equality
550 			 */
551 
552 			str = strstr(dfu->name, name);
553 			if (!str)
554 				continue;
555 
556 			/*
557 			 * Check if matching substring is the last element of
558 			 * dfu->name (uImage)
559 			 */
560 			if (strlen(dfu->name) ==
561 			    ((str - dfu->name) + strlen(name)))
562 				return dfu->alt;
563 		}
564 	}
565 
566 	return -ENODEV;
567 }
568