1 // SPDX-License-Identifier: GPL-2.0+
3 * dfu.c -- DFU back-end routines
5 * Copyright (C) 2012 Samsung Electronics
18 #include <linux/list.h>
19 #include <linux/compiler.h>
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;
29 bool dfu_reinit_needed = false;
32 * The purpose of the dfu_flush_callback() function is to
33 * provide callback for dfu user
35 __weak void dfu_flush_callback(struct dfu_entity *dfu)
40 * The purpose of the dfu_initiated_callback() function is to
41 * provide callback for dfu user
43 __weak void dfu_initiated_callback(struct dfu_entity *dfu)
48 * The purpose of the dfu_error_callback() function is to
49 * provide callback for dfu user
51 __weak void dfu_error_callback(struct dfu_entity *dfu, const char *msg)
56 * The purpose of the dfu_usb_get_reset() function is to
57 * provide information if after USB_DETACH request
58 * being sent the dfu-util performed reset of USB
61 * Described behaviour is the only way to distinct if
62 * user has typed -e (detach) or -R (reset) when invoking
66 __weak bool dfu_usb_get_reset(void)
68 #ifdef CONFIG_SPL_DFU_NO_RESET
75 #ifdef CONFIG_DFU_TIMEOUT
76 void dfu_set_timeout(unsigned long timeout)
78 dfu_timeout = timeout;
81 unsigned long dfu_get_timeout(void)
87 static int dfu_find_alt_num(const char *s)
99 * treat dfu_alt_info with several interface information
100 * to allow DFU on several device with one command,
101 * the string format is
102 * interface devstring'='alternate list (';' separated)
103 * and each interface separated by '&'
105 int dfu_config_interfaces(char *env)
107 struct dfu_entity *dfu;
108 char *s, *i, *d, *a, *part;
119 ret = dfu_alt_init(n, &dfu);
136 part = strsep(&a, ";");
137 ret = dfu_alt_add(dfu, i, d, part);
146 int dfu_init_env_entities(char *interface, char *devstr)
152 dfu_reinit_needed = false;
154 #ifdef CONFIG_SET_DFU_ALT_INFO
155 set_dfu_alt_info(interface, devstr);
157 str_env = env_get("dfu_alt_info");
159 pr_err("\"dfu_alt_info\" env variable not defined!\n");
163 env_bkp = strdup(str_env);
164 if (!interface && !devstr)
165 ret = dfu_config_interfaces(env_bkp);
167 ret = dfu_config_entities(env_bkp, interface, devstr);
170 pr_err("DFU entities configuration failed!\n");
171 pr_err("(partition table does not match dfu_alt_info?)\n");
180 static unsigned char *dfu_buf;
181 static unsigned long dfu_buf_size;
182 static enum dfu_device_type dfu_buf_device_type;
184 unsigned char *dfu_free_buf(void)
191 unsigned long dfu_get_buf_size(void)
196 unsigned char *dfu_get_buf(struct dfu_entity *dfu)
200 /* manage several entity with several contraint */
201 if (dfu_buf && dfu->dev_type != dfu_buf_device_type)
207 s = env_get("dfu_bufsiz");
209 dfu_buf_size = (unsigned long)simple_strtol(s, NULL, 0);
211 if (!s || !dfu_buf_size)
212 dfu_buf_size = CONFIG_SYS_DFU_DATA_BUF_SIZE;
214 if (dfu->max_buf_size && dfu_buf_size > dfu->max_buf_size)
215 dfu_buf_size = dfu->max_buf_size;
217 dfu_buf = memalign(CONFIG_SYS_CACHELINE_SIZE, dfu_buf_size);
219 printf("%s: Could not memalign 0x%lx bytes\n",
220 __func__, dfu_buf_size);
222 dfu_buf_device_type = dfu->dev_type;
226 static char *dfu_get_hash_algo(void)
230 s = env_get("dfu_hash_algo");
234 if (!strcmp(s, "crc32")) {
235 debug("%s: DFU hash method: %s\n", __func__, s);
239 pr_err("DFU hash method: %s not supported!\n", s);
243 static int dfu_write_buffer_drain(struct dfu_entity *dfu)
249 w_size = dfu->i_buf - dfu->i_buf_start;
254 dfu_hash_algo->hash_update(dfu_hash_algo, &dfu->crc,
255 dfu->i_buf_start, w_size, 0);
257 ret = dfu->write_medium(dfu, dfu->offset, dfu->i_buf_start, &w_size);
259 debug("%s: Write error!\n", __func__);
262 dfu->i_buf = dfu->i_buf_start;
265 dfu->offset += w_size;
272 void dfu_transaction_cleanup(struct dfu_entity *dfu)
274 /* clear everything */
277 dfu->i_blk_seq_num = 0;
278 dfu->i_buf_start = dfu_get_buf(dfu);
279 dfu->i_buf_end = dfu->i_buf_start;
280 dfu->i_buf = dfu->i_buf_start;
288 int dfu_transaction_initiate(struct dfu_entity *dfu, bool read)
295 dfu_transaction_cleanup(dfu);
297 if (dfu->i_buf_start == NULL)
300 dfu->i_buf_end = dfu->i_buf_start + dfu_get_buf_size();
303 ret = dfu->get_medium_size(dfu, &dfu->r_left);
306 debug("%s: %s %lld [B]\n", __func__, dfu->name, dfu->r_left);
310 dfu_initiated_callback(dfu);
315 int dfu_flush(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
319 ret = dfu_write_buffer_drain(dfu);
323 if (dfu->flush_medium)
324 ret = dfu->flush_medium(dfu);
327 printf("\nDFU complete %s: 0x%08x\n", dfu_hash_algo->name,
330 dfu_flush_callback(dfu);
332 dfu_transaction_cleanup(dfu);
337 int dfu_write(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
341 debug("%s: name: %s buf: 0x%p size: 0x%x p_num: 0x%x offset: 0x%llx bufoffset: 0x%lx\n",
342 __func__, dfu->name, buf, size, blk_seq_num, dfu->offset,
343 (unsigned long)(dfu->i_buf - dfu->i_buf_start));
345 ret = dfu_transaction_initiate(dfu, false);
349 if (dfu->i_blk_seq_num != blk_seq_num) {
350 printf("%s: Wrong sequence number! [%d] [%d]\n",
351 __func__, dfu->i_blk_seq_num, blk_seq_num);
352 dfu_transaction_cleanup(dfu);
353 dfu_error_callback(dfu, "Wrong sequence number");
357 /* DFU 1.1 standard says:
358 * The wBlockNum field is a block sequence number. It increments each
359 * time a block is transferred, wrapping to zero from 65,535. It is used
360 * to provide useful context to the DFU loader in the device."
362 * This means that it's a 16 bit counter that roll-overs at
363 * 0xffff -> 0x0000. By having a typical 4K transfer block
364 * we roll-over at exactly 256MB. Not very fun to debug.
366 * Handling rollover, and having an inited variable,
370 /* handle rollover */
371 dfu->i_blk_seq_num = (dfu->i_blk_seq_num + 1) & 0xffff;
373 /* flush buffer if overflow */
374 if ((dfu->i_buf + size) > dfu->i_buf_end) {
375 ret = dfu_write_buffer_drain(dfu);
377 dfu_transaction_cleanup(dfu);
378 dfu_error_callback(dfu, "DFU write error");
383 /* we should be in buffer now (if not then size too large) */
384 if ((dfu->i_buf + size) > dfu->i_buf_end) {
385 pr_err("Buffer overflow! (0x%p + 0x%x > 0x%p)\n", dfu->i_buf,
386 size, dfu->i_buf_end);
387 dfu_transaction_cleanup(dfu);
388 dfu_error_callback(dfu, "Buffer overflow");
392 memcpy(dfu->i_buf, buf, size);
395 /* if end or if buffer full flush */
396 if (size == 0 || (dfu->i_buf + size) > dfu->i_buf_end) {
397 ret = dfu_write_buffer_drain(dfu);
399 dfu_transaction_cleanup(dfu);
400 dfu_error_callback(dfu, "DFU write error");
408 static int dfu_read_buffer_fill(struct dfu_entity *dfu, void *buf, int size)
415 /* get chunk that can be read */
416 chunk = min((long)size, dfu->b_left);
419 memcpy(buf, dfu->i_buf, chunk);
421 dfu_hash_algo->hash_update(dfu_hash_algo,
426 dfu->b_left -= chunk;
434 /* no more to read */
435 if (dfu->r_left == 0)
438 dfu->i_buf = dfu->i_buf_start;
439 dfu->b_left = dfu->i_buf_end - dfu->i_buf_start;
441 /* got to read, but buffer is empty */
442 if (dfu->b_left > dfu->r_left)
443 dfu->b_left = dfu->r_left;
444 ret = dfu->read_medium(dfu, dfu->offset, dfu->i_buf,
447 debug("%s: Read error!\n", __func__);
450 if (dfu->b_left == 0)
452 dfu->offset += dfu->b_left;
453 dfu->r_left -= dfu->b_left;
462 int dfu_read(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
466 debug("%s: name: %s buf: 0x%p size: 0x%x p_num: 0x%x i_buf: 0x%p\n",
467 __func__, dfu->name, buf, size, blk_seq_num, dfu->i_buf);
469 ret = dfu_transaction_initiate(dfu, true);
473 if (dfu->i_blk_seq_num != blk_seq_num) {
474 printf("%s: Wrong sequence number! [%d] [%d]\n",
475 __func__, dfu->i_blk_seq_num, blk_seq_num);
478 /* handle rollover */
479 dfu->i_blk_seq_num = (dfu->i_blk_seq_num + 1) & 0xffff;
481 ret = dfu_read_buffer_fill(dfu, buf, size);
483 printf("%s: Failed to fill buffer\n", __func__);
489 debug("%s: %s %s: 0x%x\n", __func__, dfu->name,
490 dfu_hash_algo->name, dfu->crc);
491 puts("\nUPLOAD ... done\nCtrl+C to exit ...\n");
493 dfu_transaction_cleanup(dfu);
499 static int dfu_fill_entity(struct dfu_entity *dfu, char *s, int alt,
500 char *interface, char *devstr)
504 debug("%s: %s interface: %s dev: %s\n", __func__, s, interface, devstr);
505 st = strsep(&s, " ");
506 strcpy(dfu->name, st);
509 dfu->max_buf_size = 0;
510 dfu->free_entity = NULL;
512 /* Specific for mmc device */
513 if (strcmp(interface, "mmc") == 0) {
514 if (dfu_fill_entity_mmc(dfu, devstr, s))
516 } else if (strcmp(interface, "mtd") == 0) {
517 if (dfu_fill_entity_mtd(dfu, devstr, s))
519 } else if (strcmp(interface, "nand") == 0) {
520 if (dfu_fill_entity_nand(dfu, devstr, s))
522 } else if (strcmp(interface, "ram") == 0) {
523 if (dfu_fill_entity_ram(dfu, devstr, s))
525 } else if (strcmp(interface, "sf") == 0) {
526 if (dfu_fill_entity_sf(dfu, devstr, s))
528 } else if (strcmp(interface, "virt") == 0) {
529 if (dfu_fill_entity_virt(dfu, devstr, s))
532 printf("%s: Device %s not (yet) supported!\n",
533 __func__, interface);
541 void dfu_free_entities(void)
543 struct dfu_entity *dfu, *p, *t = NULL;
546 list_for_each_entry_safe_reverse(dfu, p, &dfu_list, list) {
547 list_del(&dfu->list);
548 if (dfu->free_entity)
549 dfu->free_entity(dfu);
554 INIT_LIST_HEAD(&dfu_list);
559 int dfu_alt_init(int num, struct dfu_entity **dfu)
565 debug("%s: dfu_alt_num=%d\n", __func__, dfu_alt_num);
567 dfu_hash_algo = NULL;
568 s = dfu_get_hash_algo();
570 ret = hash_lookup_algo(s, &dfu_hash_algo);
572 pr_err("Hash algorithm %s not supported\n", s);
575 *dfu = calloc(sizeof(struct dfu_entity), dfu_alt_num);
582 int dfu_alt_add(struct dfu_entity *dfu, char *interface, char *devstr, char *s)
584 struct dfu_entity *p_dfu;
587 if (alt_num_cnt >= dfu_alt_num)
590 p_dfu = &dfu[alt_num_cnt];
591 ret = dfu_fill_entity(p_dfu, s, alt_num_cnt, interface, devstr);
595 list_add_tail(&p_dfu->list, &dfu_list);
601 int dfu_config_entities(char *env, char *interface, char *devstr)
603 struct dfu_entity *dfu;
607 ret = dfu_alt_init(dfu_find_alt_num(env), &dfu);
611 for (i = 0; i < dfu_alt_num; i++) {
612 s = strsep(&env, ";");
613 ret = dfu_alt_add(dfu, interface, devstr, s);
615 /* We will free "dfu" in dfu_free_entities() */
623 const char *dfu_get_dev_type(enum dfu_device_type t)
625 const char *const dev_t[] = {NULL, "eMMC", "OneNAND", "NAND", "RAM",
626 "SF", "MTD", "VIRT"};
630 const char *dfu_get_layout(enum dfu_layout l)
632 const char *const dfu_layout[] = {NULL, "RAW_ADDR", "FAT", "EXT2",
633 "EXT3", "EXT4", "RAM_ADDR", "SKIP",
635 return dfu_layout[l];
638 void dfu_show_entities(void)
640 struct dfu_entity *dfu;
642 puts("DFU alt settings list:\n");
644 list_for_each_entry(dfu, &dfu_list, list) {
645 printf("dev: %s alt: %d name: %s layout: %s\n",
646 dfu_get_dev_type(dfu->dev_type), dfu->alt,
647 dfu->name, dfu_get_layout(dfu->layout));
651 int dfu_get_alt_number(void)
656 struct dfu_entity *dfu_get_entity(int alt)
658 struct dfu_entity *dfu;
660 list_for_each_entry(dfu, &dfu_list, list) {
668 int dfu_get_alt(char *name)
670 struct dfu_entity *dfu;
673 list_for_each_entry(dfu, &dfu_list, list) {
674 if (dfu->name[0] != '/') {
675 if (!strncmp(dfu->name, name, strlen(dfu->name)))
679 * One must also consider absolute path
680 * (/boot/bin/uImage) available at dfu->name when
681 * compared "plain" file name (uImage)
683 * It is the case for e.g. thor gadget where lthor SW
684 * sends only the file name, so only the very last part
685 * of path must be checked for equality
688 str = strstr(dfu->name, name);
693 * Check if matching substring is the last element of
696 if (strlen(dfu->name) ==
697 ((str - dfu->name) + strlen(name)))
705 int dfu_write_from_mem_addr(struct dfu_entity *dfu, void *buf, int size)
707 unsigned long dfu_buf_size, write, left = size;
712 * Here we must call dfu_get_buf(dfu) first to be sure that dfu_buf_size
713 * has been properly initialized - e.g. if "dfu_bufsiz" has been taken
717 dfu_buf_size = dfu_get_buf_size();
718 debug("%s: dfu buf size: %lu\n", __func__, dfu_buf_size);
720 for (i = 0; left > 0; i++) {
721 write = min(dfu_buf_size, left);
723 debug("%s: dp: 0x%p left: %lu write: %lu\n", __func__,
725 ret = dfu_write(dfu, dp, write, i);
727 pr_err("DFU write failed\n");
735 ret = dfu_flush(dfu, NULL, 0, i);
737 pr_err("DFU flush failed!");