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;
30 * The purpose of the dfu_flush_callback() function is to
31 * provide callback for dfu user
33 __weak void dfu_flush_callback(struct dfu_entity *dfu)
38 * The purpose of the dfu_initiated_callback() function is to
39 * provide callback for dfu user
41 __weak void dfu_initiated_callback(struct dfu_entity *dfu)
46 * The purpose of the dfu_usb_get_reset() function is to
47 * provide information if after USB_DETACH request
48 * being sent the dfu-util performed reset of USB
51 * Described behaviour is the only way to distinct if
52 * user has typed -e (detach) or -R (reset) when invoking
56 __weak bool dfu_usb_get_reset(void)
58 #ifdef CONFIG_SPL_DFU_NO_RESET
65 #ifdef CONFIG_DFU_TIMEOUT
66 void dfu_set_timeout(unsigned long timeout)
68 dfu_timeout = timeout;
71 unsigned long dfu_get_timeout(void)
77 static int dfu_find_alt_num(const char *s)
89 * treat dfu_alt_info with several interface information
90 * to allow DFU on several device with one command,
91 * the string format is
92 * interface devstring'='alternate list (';' separated)
93 * and each interface separated by '&'
95 int dfu_config_interfaces(char *env)
97 struct dfu_entity *dfu;
98 char *s, *i, *d, *a, *part;
109 ret = dfu_alt_init(n, &dfu);
126 part = strsep(&a, ";");
127 ret = dfu_alt_add(dfu, i, d, part);
136 int dfu_init_env_entities(char *interface, char *devstr)
142 #ifdef CONFIG_SET_DFU_ALT_INFO
143 set_dfu_alt_info(interface, devstr);
145 str_env = env_get("dfu_alt_info");
147 pr_err("\"dfu_alt_info\" env variable not defined!\n");
151 env_bkp = strdup(str_env);
152 if (!interface && !devstr)
153 ret = dfu_config_interfaces(env_bkp);
155 ret = dfu_config_entities(env_bkp, interface, devstr);
158 pr_err("DFU entities configuration failed!\n");
159 pr_err("(partition table does not match dfu_alt_info?)\n");
168 static unsigned char *dfu_buf;
169 static unsigned long dfu_buf_size;
170 static enum dfu_device_type dfu_buf_device_type;
172 unsigned char *dfu_free_buf(void)
179 unsigned long dfu_get_buf_size(void)
184 unsigned char *dfu_get_buf(struct dfu_entity *dfu)
188 /* manage several entity with several contraint */
189 if (dfu_buf && dfu->dev_type != dfu_buf_device_type)
195 s = env_get("dfu_bufsiz");
197 dfu_buf_size = (unsigned long)simple_strtol(s, NULL, 0);
199 if (!s || !dfu_buf_size)
200 dfu_buf_size = CONFIG_SYS_DFU_DATA_BUF_SIZE;
202 if (dfu->max_buf_size && dfu_buf_size > dfu->max_buf_size)
203 dfu_buf_size = dfu->max_buf_size;
205 dfu_buf = memalign(CONFIG_SYS_CACHELINE_SIZE, dfu_buf_size);
207 printf("%s: Could not memalign 0x%lx bytes\n",
208 __func__, dfu_buf_size);
210 dfu_buf_device_type = dfu->dev_type;
214 static char *dfu_get_hash_algo(void)
218 s = env_get("dfu_hash_algo");
222 if (!strcmp(s, "crc32")) {
223 debug("%s: DFU hash method: %s\n", __func__, s);
227 pr_err("DFU hash method: %s not supported!\n", s);
231 static int dfu_write_buffer_drain(struct dfu_entity *dfu)
237 w_size = dfu->i_buf - dfu->i_buf_start;
242 dfu_hash_algo->hash_update(dfu_hash_algo, &dfu->crc,
243 dfu->i_buf_start, w_size, 0);
245 ret = dfu->write_medium(dfu, dfu->offset, dfu->i_buf_start, &w_size);
247 debug("%s: Write error!\n", __func__);
250 dfu->i_buf = dfu->i_buf_start;
253 dfu->offset += w_size;
260 void dfu_transaction_cleanup(struct dfu_entity *dfu)
262 /* clear everything */
265 dfu->i_blk_seq_num = 0;
266 dfu->i_buf_start = dfu_get_buf(dfu);
267 dfu->i_buf_end = dfu->i_buf_start;
268 dfu->i_buf = dfu->i_buf_start;
276 int dfu_transaction_initiate(struct dfu_entity *dfu, bool read)
283 dfu_transaction_cleanup(dfu);
285 if (dfu->i_buf_start == NULL)
288 dfu->i_buf_end = dfu->i_buf_start + dfu_get_buf_size();
291 ret = dfu->get_medium_size(dfu, &dfu->r_left);
294 debug("%s: %s %lld [B]\n", __func__, dfu->name, dfu->r_left);
298 dfu_initiated_callback(dfu);
303 int dfu_flush(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
307 ret = dfu_write_buffer_drain(dfu);
311 if (dfu->flush_medium)
312 ret = dfu->flush_medium(dfu);
315 printf("\nDFU complete %s: 0x%08x\n", dfu_hash_algo->name,
318 dfu_flush_callback(dfu);
320 dfu_transaction_cleanup(dfu);
325 int dfu_write(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
329 debug("%s: name: %s buf: 0x%p size: 0x%x p_num: 0x%x offset: 0x%llx bufoffset: 0x%lx\n",
330 __func__, dfu->name, buf, size, blk_seq_num, dfu->offset,
331 (unsigned long)(dfu->i_buf - dfu->i_buf_start));
333 ret = dfu_transaction_initiate(dfu, false);
337 if (dfu->i_blk_seq_num != blk_seq_num) {
338 printf("%s: Wrong sequence number! [%d] [%d]\n",
339 __func__, dfu->i_blk_seq_num, blk_seq_num);
340 dfu_transaction_cleanup(dfu);
344 /* DFU 1.1 standard says:
345 * The wBlockNum field is a block sequence number. It increments each
346 * time a block is transferred, wrapping to zero from 65,535. It is used
347 * to provide useful context to the DFU loader in the device."
349 * This means that it's a 16 bit counter that roll-overs at
350 * 0xffff -> 0x0000. By having a typical 4K transfer block
351 * we roll-over at exactly 256MB. Not very fun to debug.
353 * Handling rollover, and having an inited variable,
357 /* handle rollover */
358 dfu->i_blk_seq_num = (dfu->i_blk_seq_num + 1) & 0xffff;
360 /* flush buffer if overflow */
361 if ((dfu->i_buf + size) > dfu->i_buf_end) {
362 ret = dfu_write_buffer_drain(dfu);
364 dfu_transaction_cleanup(dfu);
369 /* we should be in buffer now (if not then size too large) */
370 if ((dfu->i_buf + size) > dfu->i_buf_end) {
371 pr_err("Buffer overflow! (0x%p + 0x%x > 0x%p)\n", dfu->i_buf,
372 size, dfu->i_buf_end);
373 dfu_transaction_cleanup(dfu);
377 memcpy(dfu->i_buf, buf, size);
380 /* if end or if buffer full flush */
381 if (size == 0 || (dfu->i_buf + size) > dfu->i_buf_end) {
382 ret = dfu_write_buffer_drain(dfu);
384 dfu_transaction_cleanup(dfu);
392 static int dfu_read_buffer_fill(struct dfu_entity *dfu, void *buf, int size)
399 /* get chunk that can be read */
400 chunk = min((long)size, dfu->b_left);
403 memcpy(buf, dfu->i_buf, chunk);
405 dfu_hash_algo->hash_update(dfu_hash_algo,
410 dfu->b_left -= chunk;
418 /* no more to read */
419 if (dfu->r_left == 0)
422 dfu->i_buf = dfu->i_buf_start;
423 dfu->b_left = dfu->i_buf_end - dfu->i_buf_start;
425 /* got to read, but buffer is empty */
426 if (dfu->b_left > dfu->r_left)
427 dfu->b_left = dfu->r_left;
428 ret = dfu->read_medium(dfu, dfu->offset, dfu->i_buf,
431 debug("%s: Read error!\n", __func__);
434 if (dfu->b_left == 0)
436 dfu->offset += dfu->b_left;
437 dfu->r_left -= dfu->b_left;
446 int dfu_read(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
450 debug("%s: name: %s buf: 0x%p size: 0x%x p_num: 0x%x i_buf: 0x%p\n",
451 __func__, dfu->name, buf, size, blk_seq_num, dfu->i_buf);
453 ret = dfu_transaction_initiate(dfu, true);
457 if (dfu->i_blk_seq_num != blk_seq_num) {
458 printf("%s: Wrong sequence number! [%d] [%d]\n",
459 __func__, dfu->i_blk_seq_num, blk_seq_num);
462 /* handle rollover */
463 dfu->i_blk_seq_num = (dfu->i_blk_seq_num + 1) & 0xffff;
465 ret = dfu_read_buffer_fill(dfu, buf, size);
467 printf("%s: Failed to fill buffer\n", __func__);
473 debug("%s: %s %s: 0x%x\n", __func__, dfu->name,
474 dfu_hash_algo->name, dfu->crc);
475 puts("\nUPLOAD ... done\nCtrl+C to exit ...\n");
477 dfu_transaction_cleanup(dfu);
483 static int dfu_fill_entity(struct dfu_entity *dfu, char *s, int alt,
484 char *interface, char *devstr)
488 debug("%s: %s interface: %s dev: %s\n", __func__, s, interface, devstr);
489 st = strsep(&s, " ");
490 strcpy(dfu->name, st);
493 dfu->max_buf_size = 0;
494 dfu->free_entity = NULL;
496 /* Specific for mmc device */
497 if (strcmp(interface, "mmc") == 0) {
498 if (dfu_fill_entity_mmc(dfu, devstr, s))
500 } else if (strcmp(interface, "mtd") == 0) {
501 if (dfu_fill_entity_mtd(dfu, devstr, s))
503 } else if (strcmp(interface, "nand") == 0) {
504 if (dfu_fill_entity_nand(dfu, devstr, s))
506 } else if (strcmp(interface, "ram") == 0) {
507 if (dfu_fill_entity_ram(dfu, devstr, s))
509 } else if (strcmp(interface, "sf") == 0) {
510 if (dfu_fill_entity_sf(dfu, devstr, s))
512 } else if (strcmp(interface, "virt") == 0) {
513 if (dfu_fill_entity_virt(dfu, devstr, s))
516 printf("%s: Device %s not (yet) supported!\n",
517 __func__, interface);
525 void dfu_free_entities(void)
527 struct dfu_entity *dfu, *p, *t = NULL;
530 list_for_each_entry_safe_reverse(dfu, p, &dfu_list, list) {
531 list_del(&dfu->list);
532 if (dfu->free_entity)
533 dfu->free_entity(dfu);
538 INIT_LIST_HEAD(&dfu_list);
543 int dfu_alt_init(int num, struct dfu_entity **dfu)
549 debug("%s: dfu_alt_num=%d\n", __func__, dfu_alt_num);
551 dfu_hash_algo = NULL;
552 s = dfu_get_hash_algo();
554 ret = hash_lookup_algo(s, &dfu_hash_algo);
556 pr_err("Hash algorithm %s not supported\n", s);
559 *dfu = calloc(sizeof(struct dfu_entity), dfu_alt_num);
566 int dfu_alt_add(struct dfu_entity *dfu, char *interface, char *devstr, char *s)
568 struct dfu_entity *p_dfu;
571 if (alt_num_cnt >= dfu_alt_num)
574 p_dfu = &dfu[alt_num_cnt];
575 ret = dfu_fill_entity(p_dfu, s, alt_num_cnt, interface, devstr);
579 list_add_tail(&p_dfu->list, &dfu_list);
585 int dfu_config_entities(char *env, char *interface, char *devstr)
587 struct dfu_entity *dfu;
591 ret = dfu_alt_init(dfu_find_alt_num(env), &dfu);
595 for (i = 0; i < dfu_alt_num; i++) {
596 s = strsep(&env, ";");
597 ret = dfu_alt_add(dfu, interface, devstr, s);
599 /* We will free "dfu" in dfu_free_entities() */
607 const char *dfu_get_dev_type(enum dfu_device_type t)
609 const char *const dev_t[] = {NULL, "eMMC", "OneNAND", "NAND", "RAM",
610 "SF", "MTD", "VIRT"};
614 const char *dfu_get_layout(enum dfu_layout l)
616 const char *const dfu_layout[] = {NULL, "RAW_ADDR", "FAT", "EXT2",
617 "EXT3", "EXT4", "RAM_ADDR" };
618 return dfu_layout[l];
621 void dfu_show_entities(void)
623 struct dfu_entity *dfu;
625 puts("DFU alt settings list:\n");
627 list_for_each_entry(dfu, &dfu_list, list) {
628 printf("dev: %s alt: %d name: %s layout: %s\n",
629 dfu_get_dev_type(dfu->dev_type), dfu->alt,
630 dfu->name, dfu_get_layout(dfu->layout));
634 int dfu_get_alt_number(void)
639 struct dfu_entity *dfu_get_entity(int alt)
641 struct dfu_entity *dfu;
643 list_for_each_entry(dfu, &dfu_list, list) {
651 int dfu_get_alt(char *name)
653 struct dfu_entity *dfu;
656 list_for_each_entry(dfu, &dfu_list, list) {
657 if (dfu->name[0] != '/') {
658 if (!strncmp(dfu->name, name, strlen(dfu->name)))
662 * One must also consider absolute path
663 * (/boot/bin/uImage) available at dfu->name when
664 * compared "plain" file name (uImage)
666 * It is the case for e.g. thor gadget where lthor SW
667 * sends only the file name, so only the very last part
668 * of path must be checked for equality
671 str = strstr(dfu->name, name);
676 * Check if matching substring is the last element of
679 if (strlen(dfu->name) ==
680 ((str - dfu->name) + strlen(name)))
688 int dfu_write_from_mem_addr(struct dfu_entity *dfu, void *buf, int size)
690 unsigned long dfu_buf_size, write, left = size;
695 * Here we must call dfu_get_buf(dfu) first to be sure that dfu_buf_size
696 * has been properly initialized - e.g. if "dfu_bufsiz" has been taken
700 dfu_buf_size = dfu_get_buf_size();
701 debug("%s: dfu buf size: %lu\n", __func__, dfu_buf_size);
703 for (i = 0; left > 0; i++) {
704 write = min(dfu_buf_size, left);
706 debug("%s: dp: 0x%p left: %lu write: %lu\n", __func__,
708 ret = dfu_write(dfu, dp, write, i);
710 pr_err("DFU write failed\n");
718 ret = dfu_flush(dfu, NULL, 0, i);
720 pr_err("DFU flush failed!");