1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * dfu.c -- DFU back-end routines
4 *
5 * Copyright (C) 2012 Samsung Electronics
6 * author: Lukasz Majewski <l.majewski@samsung.com>
7 */
8
9 #include <common.h>
10 #include <env.h>
11 #include <errno.h>
12 #include <log.h>
13 #include <malloc.h>
14 #include <mmc.h>
15 #include <fat.h>
16 #include <dfu.h>
17 #include <hash.h>
18 #include <linux/list.h>
19 #include <linux/compiler.h>
20
21 LIST_HEAD(dfu_list);
22 static int dfu_alt_num;
23 static int alt_num_cnt;
24 static struct hash_algo *dfu_hash_algo;
25 #ifdef CONFIG_DFU_TIMEOUT
26 static unsigned long dfu_timeout = 0;
27 #endif
28
29 bool dfu_reinit_needed = false;
30
31 /*
32 * The purpose of the dfu_flush_callback() function is to
33 * provide callback for dfu user
34 */
dfu_flush_callback(struct dfu_entity * dfu)35 __weak void dfu_flush_callback(struct dfu_entity *dfu)
36 {
37 }
38
39 /*
40 * The purpose of the dfu_initiated_callback() function is to
41 * provide callback for dfu user
42 */
dfu_initiated_callback(struct dfu_entity * dfu)43 __weak void dfu_initiated_callback(struct dfu_entity *dfu)
44 {
45 }
46
47 /*
48 * The purpose of the dfu_usb_get_reset() function is to
49 * provide information if after USB_DETACH request
50 * being sent the dfu-util performed reset of USB
51 * bus.
52 *
53 * Described behaviour is the only way to distinct if
54 * user has typed -e (detach) or -R (reset) when invoking
55 * dfu-util command.
56 *
57 */
dfu_usb_get_reset(void)58 __weak bool dfu_usb_get_reset(void)
59 {
60 #ifdef CONFIG_SPL_DFU_NO_RESET
61 return false;
62 #else
63 return true;
64 #endif
65 }
66
67 #ifdef CONFIG_DFU_TIMEOUT
dfu_set_timeout(unsigned long timeout)68 void dfu_set_timeout(unsigned long timeout)
69 {
70 dfu_timeout = timeout;
71 }
72
dfu_get_timeout(void)73 unsigned long dfu_get_timeout(void)
74 {
75 return dfu_timeout;
76 }
77 #endif
78
dfu_find_alt_num(const char * s)79 static int dfu_find_alt_num(const char *s)
80 {
81 int i = 0;
82
83 for (; *s; s++)
84 if (*s == ';')
85 i++;
86
87 return ++i;
88 }
89
90 /*
91 * treat dfu_alt_info with several interface information
92 * to allow DFU on several device with one command,
93 * the string format is
94 * interface devstring'='alternate list (';' separated)
95 * and each interface separated by '&'
96 */
dfu_config_interfaces(char * env)97 int dfu_config_interfaces(char *env)
98 {
99 struct dfu_entity *dfu;
100 char *s, *i, *d, *a, *part;
101 int ret = -EINVAL;
102 int n = 1;
103
104 s = env;
105 for (; *s; s++) {
106 if (*s == ';')
107 n++;
108 if (*s == '&')
109 n++;
110 }
111 ret = dfu_alt_init(n, &dfu);
112 if (ret)
113 return ret;
114
115 s = env;
116 while (s) {
117 ret = -EINVAL;
118 i = strsep(&s, " ");
119 if (!i)
120 break;
121 d = strsep(&s, "=");
122 if (!d)
123 break;
124 a = strsep(&s, "&");
125 if (!a)
126 a = s;
127 do {
128 part = strsep(&a, ";");
129 ret = dfu_alt_add(dfu, i, d, part);
130 if (ret)
131 return ret;
132 } while (a);
133 }
134
135 return ret;
136 }
137
dfu_init_env_entities(char * interface,char * devstr)138 int dfu_init_env_entities(char *interface, char *devstr)
139 {
140 const char *str_env;
141 char *env_bkp;
142 int ret = 0;
143
144 dfu_reinit_needed = false;
145
146 #ifdef CONFIG_SET_DFU_ALT_INFO
147 set_dfu_alt_info(interface, devstr);
148 #endif
149 str_env = env_get("dfu_alt_info");
150 if (!str_env) {
151 pr_err("\"dfu_alt_info\" env variable not defined!\n");
152 return -EINVAL;
153 }
154
155 env_bkp = strdup(str_env);
156 if (!interface && !devstr)
157 ret = dfu_config_interfaces(env_bkp);
158 else
159 ret = dfu_config_entities(env_bkp, interface, devstr);
160
161 if (ret) {
162 pr_err("DFU entities configuration failed!\n");
163 pr_err("(partition table does not match dfu_alt_info?)\n");
164 goto done;
165 }
166
167 done:
168 free(env_bkp);
169 return ret;
170 }
171
172 static unsigned char *dfu_buf;
173 static unsigned long dfu_buf_size;
174 static enum dfu_device_type dfu_buf_device_type;
175
dfu_free_buf(void)176 unsigned char *dfu_free_buf(void)
177 {
178 free(dfu_buf);
179 dfu_buf = NULL;
180 return dfu_buf;
181 }
182
dfu_get_buf_size(void)183 unsigned long dfu_get_buf_size(void)
184 {
185 return dfu_buf_size;
186 }
187
dfu_get_buf(struct dfu_entity * dfu)188 unsigned char *dfu_get_buf(struct dfu_entity *dfu)
189 {
190 char *s;
191
192 /* manage several entity with several contraint */
193 if (dfu_buf && dfu->dev_type != dfu_buf_device_type)
194 dfu_free_buf();
195
196 if (dfu_buf != NULL)
197 return dfu_buf;
198
199 s = env_get("dfu_bufsiz");
200 if (s)
201 dfu_buf_size = (unsigned long)simple_strtol(s, NULL, 0);
202
203 if (!s || !dfu_buf_size)
204 dfu_buf_size = CONFIG_SYS_DFU_DATA_BUF_SIZE;
205
206 if (dfu->max_buf_size && dfu_buf_size > dfu->max_buf_size)
207 dfu_buf_size = dfu->max_buf_size;
208
209 dfu_buf = memalign(CONFIG_SYS_CACHELINE_SIZE, dfu_buf_size);
210 if (dfu_buf == NULL)
211 printf("%s: Could not memalign 0x%lx bytes\n",
212 __func__, dfu_buf_size);
213
214 dfu_buf_device_type = dfu->dev_type;
215 return dfu_buf;
216 }
217
dfu_get_hash_algo(void)218 static char *dfu_get_hash_algo(void)
219 {
220 char *s;
221
222 s = env_get("dfu_hash_algo");
223 if (!s)
224 return NULL;
225
226 if (!strcmp(s, "crc32")) {
227 debug("%s: DFU hash method: %s\n", __func__, s);
228 return s;
229 }
230
231 pr_err("DFU hash method: %s not supported!\n", s);
232 return NULL;
233 }
234
dfu_write_buffer_drain(struct dfu_entity * dfu)235 static int dfu_write_buffer_drain(struct dfu_entity *dfu)
236 {
237 long w_size;
238 int ret;
239
240 /* flush size? */
241 w_size = dfu->i_buf - dfu->i_buf_start;
242 if (w_size == 0)
243 return 0;
244
245 if (dfu_hash_algo)
246 dfu_hash_algo->hash_update(dfu_hash_algo, &dfu->crc,
247 dfu->i_buf_start, w_size, 0);
248
249 ret = dfu->write_medium(dfu, dfu->offset, dfu->i_buf_start, &w_size);
250 if (ret)
251 debug("%s: Write error!\n", __func__);
252
253 /* point back */
254 dfu->i_buf = dfu->i_buf_start;
255
256 /* update offset */
257 dfu->offset += w_size;
258
259 puts("#");
260
261 return ret;
262 }
263
dfu_transaction_cleanup(struct dfu_entity * dfu)264 void dfu_transaction_cleanup(struct dfu_entity *dfu)
265 {
266 /* clear everything */
267 dfu->crc = 0;
268 dfu->offset = 0;
269 dfu->i_blk_seq_num = 0;
270 dfu->i_buf_start = dfu_get_buf(dfu);
271 dfu->i_buf_end = dfu->i_buf_start;
272 dfu->i_buf = dfu->i_buf_start;
273 dfu->r_left = 0;
274 dfu->b_left = 0;
275 dfu->bad_skip = 0;
276
277 dfu->inited = 0;
278 }
279
dfu_transaction_initiate(struct dfu_entity * dfu,bool read)280 int dfu_transaction_initiate(struct dfu_entity *dfu, bool read)
281 {
282 int ret = 0;
283
284 if (dfu->inited)
285 return 0;
286
287 dfu_transaction_cleanup(dfu);
288
289 if (dfu->i_buf_start == NULL)
290 return -ENOMEM;
291
292 dfu->i_buf_end = dfu->i_buf_start + dfu_get_buf_size();
293
294 if (read) {
295 ret = dfu->get_medium_size(dfu, &dfu->r_left);
296 if (ret < 0)
297 return ret;
298 debug("%s: %s %lld [B]\n", __func__, dfu->name, dfu->r_left);
299 }
300
301 dfu->inited = 1;
302 dfu_initiated_callback(dfu);
303
304 return 0;
305 }
306
dfu_flush(struct dfu_entity * dfu,void * buf,int size,int blk_seq_num)307 int dfu_flush(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
308 {
309 int ret = 0;
310
311 ret = dfu_write_buffer_drain(dfu);
312 if (ret)
313 return ret;
314
315 if (dfu->flush_medium)
316 ret = dfu->flush_medium(dfu);
317
318 if (dfu_hash_algo)
319 printf("\nDFU complete %s: 0x%08x\n", dfu_hash_algo->name,
320 dfu->crc);
321
322 dfu_flush_callback(dfu);
323
324 dfu_transaction_cleanup(dfu);
325
326 return ret;
327 }
328
dfu_write(struct dfu_entity * dfu,void * buf,int size,int blk_seq_num)329 int dfu_write(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
330 {
331 int ret;
332
333 debug("%s: name: %s buf: 0x%p size: 0x%x p_num: 0x%x offset: 0x%llx bufoffset: 0x%lx\n",
334 __func__, dfu->name, buf, size, blk_seq_num, dfu->offset,
335 (unsigned long)(dfu->i_buf - dfu->i_buf_start));
336
337 ret = dfu_transaction_initiate(dfu, false);
338 if (ret < 0)
339 return ret;
340
341 if (dfu->i_blk_seq_num != blk_seq_num) {
342 printf("%s: Wrong sequence number! [%d] [%d]\n",
343 __func__, dfu->i_blk_seq_num, blk_seq_num);
344 dfu_transaction_cleanup(dfu);
345 return -1;
346 }
347
348 /* DFU 1.1 standard says:
349 * The wBlockNum field is a block sequence number. It increments each
350 * time a block is transferred, wrapping to zero from 65,535. It is used
351 * to provide useful context to the DFU loader in the device."
352 *
353 * This means that it's a 16 bit counter that roll-overs at
354 * 0xffff -> 0x0000. By having a typical 4K transfer block
355 * we roll-over at exactly 256MB. Not very fun to debug.
356 *
357 * Handling rollover, and having an inited variable,
358 * makes things work.
359 */
360
361 /* handle rollover */
362 dfu->i_blk_seq_num = (dfu->i_blk_seq_num + 1) & 0xffff;
363
364 /* flush buffer if overflow */
365 if ((dfu->i_buf + size) > dfu->i_buf_end) {
366 ret = dfu_write_buffer_drain(dfu);
367 if (ret) {
368 dfu_transaction_cleanup(dfu);
369 return ret;
370 }
371 }
372
373 /* we should be in buffer now (if not then size too large) */
374 if ((dfu->i_buf + size) > dfu->i_buf_end) {
375 pr_err("Buffer overflow! (0x%p + 0x%x > 0x%p)\n", dfu->i_buf,
376 size, dfu->i_buf_end);
377 dfu_transaction_cleanup(dfu);
378 return -1;
379 }
380
381 memcpy(dfu->i_buf, buf, size);
382 dfu->i_buf += size;
383
384 /* if end or if buffer full flush */
385 if (size == 0 || (dfu->i_buf + size) > dfu->i_buf_end) {
386 ret = dfu_write_buffer_drain(dfu);
387 if (ret) {
388 dfu_transaction_cleanup(dfu);
389 return ret;
390 }
391 }
392
393 return 0;
394 }
395
dfu_read_buffer_fill(struct dfu_entity * dfu,void * buf,int size)396 static int dfu_read_buffer_fill(struct dfu_entity *dfu, void *buf, int size)
397 {
398 long chunk;
399 int ret, readn;
400
401 readn = 0;
402 while (size > 0) {
403 /* get chunk that can be read */
404 chunk = min((long)size, dfu->b_left);
405 /* consume */
406 if (chunk > 0) {
407 memcpy(buf, dfu->i_buf, chunk);
408 if (dfu_hash_algo)
409 dfu_hash_algo->hash_update(dfu_hash_algo,
410 &dfu->crc, buf,
411 chunk, 0);
412
413 dfu->i_buf += chunk;
414 dfu->b_left -= chunk;
415 size -= chunk;
416 buf += chunk;
417 readn += chunk;
418 }
419
420 /* all done */
421 if (size > 0) {
422 /* no more to read */
423 if (dfu->r_left == 0)
424 break;
425
426 dfu->i_buf = dfu->i_buf_start;
427 dfu->b_left = dfu->i_buf_end - dfu->i_buf_start;
428
429 /* got to read, but buffer is empty */
430 if (dfu->b_left > dfu->r_left)
431 dfu->b_left = dfu->r_left;
432 ret = dfu->read_medium(dfu, dfu->offset, dfu->i_buf,
433 &dfu->b_left);
434 if (ret != 0) {
435 debug("%s: Read error!\n", __func__);
436 return ret;
437 }
438 if (dfu->b_left == 0)
439 break;
440 dfu->offset += dfu->b_left;
441 dfu->r_left -= dfu->b_left;
442
443 puts("#");
444 }
445 }
446
447 return readn;
448 }
449
dfu_read(struct dfu_entity * dfu,void * buf,int size,int blk_seq_num)450 int dfu_read(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
451 {
452 int ret = 0;
453
454 debug("%s: name: %s buf: 0x%p size: 0x%x p_num: 0x%x i_buf: 0x%p\n",
455 __func__, dfu->name, buf, size, blk_seq_num, dfu->i_buf);
456
457 ret = dfu_transaction_initiate(dfu, true);
458 if (ret < 0)
459 return ret;
460
461 if (dfu->i_blk_seq_num != blk_seq_num) {
462 printf("%s: Wrong sequence number! [%d] [%d]\n",
463 __func__, dfu->i_blk_seq_num, blk_seq_num);
464 return -1;
465 }
466 /* handle rollover */
467 dfu->i_blk_seq_num = (dfu->i_blk_seq_num + 1) & 0xffff;
468
469 ret = dfu_read_buffer_fill(dfu, buf, size);
470 if (ret < 0) {
471 printf("%s: Failed to fill buffer\n", __func__);
472 return -1;
473 }
474
475 if (ret < size) {
476 if (dfu_hash_algo)
477 debug("%s: %s %s: 0x%x\n", __func__, dfu->name,
478 dfu_hash_algo->name, dfu->crc);
479 puts("\nUPLOAD ... done\nCtrl+C to exit ...\n");
480
481 dfu_transaction_cleanup(dfu);
482 }
483
484 return ret;
485 }
486
dfu_fill_entity(struct dfu_entity * dfu,char * s,int alt,char * interface,char * devstr)487 static int dfu_fill_entity(struct dfu_entity *dfu, char *s, int alt,
488 char *interface, char *devstr)
489 {
490 char *st;
491
492 debug("%s: %s interface: %s dev: %s\n", __func__, s, interface, devstr);
493 st = strsep(&s, " ");
494 strcpy(dfu->name, st);
495
496 dfu->alt = alt;
497 dfu->max_buf_size = 0;
498 dfu->free_entity = NULL;
499
500 /* Specific for mmc device */
501 if (strcmp(interface, "mmc") == 0) {
502 if (dfu_fill_entity_mmc(dfu, devstr, s))
503 return -1;
504 } else if (strcmp(interface, "mtd") == 0) {
505 if (dfu_fill_entity_mtd(dfu, devstr, s))
506 return -1;
507 } else if (strcmp(interface, "nand") == 0) {
508 if (dfu_fill_entity_nand(dfu, devstr, s))
509 return -1;
510 } else if (strcmp(interface, "ram") == 0) {
511 if (dfu_fill_entity_ram(dfu, devstr, s))
512 return -1;
513 } else if (strcmp(interface, "sf") == 0) {
514 if (dfu_fill_entity_sf(dfu, devstr, s))
515 return -1;
516 } else if (strcmp(interface, "virt") == 0) {
517 if (dfu_fill_entity_virt(dfu, devstr, s))
518 return -1;
519 } else {
520 printf("%s: Device %s not (yet) supported!\n",
521 __func__, interface);
522 return -1;
523 }
524 dfu_get_buf(dfu);
525
526 return 0;
527 }
528
dfu_free_entities(void)529 void dfu_free_entities(void)
530 {
531 struct dfu_entity *dfu, *p, *t = NULL;
532
533 dfu_free_buf();
534 list_for_each_entry_safe_reverse(dfu, p, &dfu_list, list) {
535 list_del(&dfu->list);
536 if (dfu->free_entity)
537 dfu->free_entity(dfu);
538 t = dfu;
539 }
540 if (t)
541 free(t);
542 INIT_LIST_HEAD(&dfu_list);
543
544 alt_num_cnt = 0;
545 }
546
dfu_alt_init(int num,struct dfu_entity ** dfu)547 int dfu_alt_init(int num, struct dfu_entity **dfu)
548 {
549 char *s;
550 int ret;
551
552 dfu_alt_num = num;
553 debug("%s: dfu_alt_num=%d\n", __func__, dfu_alt_num);
554
555 dfu_hash_algo = NULL;
556 s = dfu_get_hash_algo();
557 if (s) {
558 ret = hash_lookup_algo(s, &dfu_hash_algo);
559 if (ret)
560 pr_err("Hash algorithm %s not supported\n", s);
561 }
562
563 *dfu = calloc(sizeof(struct dfu_entity), dfu_alt_num);
564 if (!*dfu)
565 return -1;
566
567 return 0;
568 }
569
dfu_alt_add(struct dfu_entity * dfu,char * interface,char * devstr,char * s)570 int dfu_alt_add(struct dfu_entity *dfu, char *interface, char *devstr, char *s)
571 {
572 struct dfu_entity *p_dfu;
573 int ret;
574
575 if (alt_num_cnt >= dfu_alt_num)
576 return -1;
577
578 p_dfu = &dfu[alt_num_cnt];
579 ret = dfu_fill_entity(p_dfu, s, alt_num_cnt, interface, devstr);
580 if (ret)
581 return -1;
582
583 list_add_tail(&p_dfu->list, &dfu_list);
584 alt_num_cnt++;
585
586 return 0;
587 }
588
dfu_config_entities(char * env,char * interface,char * devstr)589 int dfu_config_entities(char *env, char *interface, char *devstr)
590 {
591 struct dfu_entity *dfu;
592 int i, ret;
593 char *s;
594
595 ret = dfu_alt_init(dfu_find_alt_num(env), &dfu);
596 if (ret)
597 return -1;
598
599 for (i = 0; i < dfu_alt_num; i++) {
600 s = strsep(&env, ";");
601 ret = dfu_alt_add(dfu, interface, devstr, s);
602 if (ret) {
603 /* We will free "dfu" in dfu_free_entities() */
604 return -1;
605 }
606 }
607
608 return 0;
609 }
610
dfu_get_dev_type(enum dfu_device_type t)611 const char *dfu_get_dev_type(enum dfu_device_type t)
612 {
613 const char *const dev_t[] = {NULL, "eMMC", "OneNAND", "NAND", "RAM",
614 "SF", "MTD", "VIRT"};
615 return dev_t[t];
616 }
617
dfu_get_layout(enum dfu_layout l)618 const char *dfu_get_layout(enum dfu_layout l)
619 {
620 const char *const dfu_layout[] = {NULL, "RAW_ADDR", "FAT", "EXT2",
621 "EXT3", "EXT4", "RAM_ADDR", "SKIP",
622 "SCRIPT" };
623 return dfu_layout[l];
624 }
625
dfu_show_entities(void)626 void dfu_show_entities(void)
627 {
628 struct dfu_entity *dfu;
629
630 puts("DFU alt settings list:\n");
631
632 list_for_each_entry(dfu, &dfu_list, list) {
633 printf("dev: %s alt: %d name: %s layout: %s\n",
634 dfu_get_dev_type(dfu->dev_type), dfu->alt,
635 dfu->name, dfu_get_layout(dfu->layout));
636 }
637 }
638
dfu_get_alt_number(void)639 int dfu_get_alt_number(void)
640 {
641 return dfu_alt_num;
642 }
643
dfu_get_entity(int alt)644 struct dfu_entity *dfu_get_entity(int alt)
645 {
646 struct dfu_entity *dfu;
647
648 list_for_each_entry(dfu, &dfu_list, list) {
649 if (dfu->alt == alt)
650 return dfu;
651 }
652
653 return NULL;
654 }
655
dfu_get_alt(char * name)656 int dfu_get_alt(char *name)
657 {
658 struct dfu_entity *dfu;
659 char *str;
660
661 list_for_each_entry(dfu, &dfu_list, list) {
662 if (dfu->name[0] != '/') {
663 if (!strncmp(dfu->name, name, strlen(dfu->name)))
664 return dfu->alt;
665 } else {
666 /*
667 * One must also consider absolute path
668 * (/boot/bin/uImage) available at dfu->name when
669 * compared "plain" file name (uImage)
670 *
671 * It is the case for e.g. thor gadget where lthor SW
672 * sends only the file name, so only the very last part
673 * of path must be checked for equality
674 */
675
676 str = strstr(dfu->name, name);
677 if (!str)
678 continue;
679
680 /*
681 * Check if matching substring is the last element of
682 * dfu->name (uImage)
683 */
684 if (strlen(dfu->name) ==
685 ((str - dfu->name) + strlen(name)))
686 return dfu->alt;
687 }
688 }
689
690 return -ENODEV;
691 }
692
dfu_write_from_mem_addr(struct dfu_entity * dfu,void * buf,int size)693 int dfu_write_from_mem_addr(struct dfu_entity *dfu, void *buf, int size)
694 {
695 unsigned long dfu_buf_size, write, left = size;
696 int i, ret = 0;
697 void *dp = buf;
698
699 /*
700 * Here we must call dfu_get_buf(dfu) first to be sure that dfu_buf_size
701 * has been properly initialized - e.g. if "dfu_bufsiz" has been taken
702 * into account.
703 */
704 dfu_get_buf(dfu);
705 dfu_buf_size = dfu_get_buf_size();
706 debug("%s: dfu buf size: %lu\n", __func__, dfu_buf_size);
707
708 for (i = 0; left > 0; i++) {
709 write = min(dfu_buf_size, left);
710
711 debug("%s: dp: 0x%p left: %lu write: %lu\n", __func__,
712 dp, left, write);
713 ret = dfu_write(dfu, dp, write, i);
714 if (ret) {
715 pr_err("DFU write failed\n");
716 return ret;
717 }
718
719 dp += write;
720 left -= write;
721 }
722
723 ret = dfu_flush(dfu, NULL, 0, i);
724 if (ret)
725 pr_err("DFU flush failed!");
726
727 return ret;
728 }
729