2 * dfu.c -- DFU back-end routines
4 * Copyright (C) 2012 Samsung Electronics
7 * SPDX-License-Identifier: GPL-2.0+
17 #include <linux/list.h>
18 #include <linux/compiler.h>
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;
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
31 * Described behaviour is the only way to distinct if
32 * user has typed -e (detach) or -R (reset) when invoking
36 __weak bool dfu_usb_get_reset(void)
38 #ifdef CONFIG_SPL_DFU_NO_RESET
45 static int dfu_find_alt_num(const char *s)
56 int dfu_init_env_entities(char *interface, char *devstr)
62 #ifdef CONFIG_SET_DFU_ALT_INFO
63 set_dfu_alt_info(interface, devstr);
65 str_env = getenv("dfu_alt_info");
67 error("\"dfu_alt_info\" env variable not defined!\n");
71 env_bkp = strdup(str_env);
72 ret = dfu_config_entities(env_bkp, interface, devstr);
74 error("DFU entities configuration failed!\n");
82 static unsigned char *dfu_buf;
83 static unsigned long dfu_buf_size;
85 unsigned char *dfu_free_buf(void)
92 unsigned long dfu_get_buf_size(void)
97 unsigned char *dfu_get_buf(struct dfu_entity *dfu)
104 s = getenv("dfu_bufsiz");
106 dfu_buf_size = (unsigned long)simple_strtol(s, NULL, 0);
108 if (!s || !dfu_buf_size)
109 dfu_buf_size = CONFIG_SYS_DFU_DATA_BUF_SIZE;
111 if (dfu->max_buf_size && dfu_buf_size > dfu->max_buf_size)
112 dfu_buf_size = dfu->max_buf_size;
114 dfu_buf = memalign(CONFIG_SYS_CACHELINE_SIZE, dfu_buf_size);
116 printf("%s: Could not memalign 0x%lx bytes\n",
117 __func__, dfu_buf_size);
122 static char *dfu_get_hash_algo(void)
126 s = getenv("dfu_hash_algo");
130 if (!strcmp(s, "crc32")) {
131 debug("%s: DFU hash method: %s\n", __func__, s);
135 error("DFU hash method: %s not supported!\n", s);
139 static int dfu_write_buffer_drain(struct dfu_entity *dfu)
145 w_size = dfu->i_buf - dfu->i_buf_start;
150 dfu_hash_algo->hash_update(dfu_hash_algo, &dfu->crc,
151 dfu->i_buf_start, w_size, 0);
153 ret = dfu->write_medium(dfu, dfu->offset, dfu->i_buf_start, &w_size);
155 debug("%s: Write error!\n", __func__);
158 dfu->i_buf = dfu->i_buf_start;
161 dfu->offset += w_size;
168 void dfu_write_transaction_cleanup(struct dfu_entity *dfu)
170 /* clear everything */
173 dfu->i_blk_seq_num = 0;
174 dfu->i_buf_start = dfu_buf;
175 dfu->i_buf_end = dfu_buf;
176 dfu->i_buf = dfu->i_buf_start;
180 int dfu_flush(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
184 ret = dfu_write_buffer_drain(dfu);
188 if (dfu->flush_medium)
189 ret = dfu->flush_medium(dfu);
192 printf("\nDFU complete %s: 0x%08x\n", dfu_hash_algo->name,
195 dfu_write_transaction_cleanup(dfu);
200 int dfu_write(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
204 debug("%s: name: %s buf: 0x%p size: 0x%x p_num: 0x%x offset: 0x%llx bufoffset: 0x%lx\n",
205 __func__, dfu->name, buf, size, blk_seq_num, dfu->offset,
206 (unsigned long)(dfu->i_buf - dfu->i_buf_start));
213 dfu->i_blk_seq_num = 0;
214 dfu->i_buf_start = dfu_get_buf(dfu);
215 if (dfu->i_buf_start == NULL)
217 dfu->i_buf_end = dfu_get_buf(dfu) + dfu_buf_size;
218 dfu->i_buf = dfu->i_buf_start;
223 if (dfu->i_blk_seq_num != blk_seq_num) {
224 printf("%s: Wrong sequence number! [%d] [%d]\n",
225 __func__, dfu->i_blk_seq_num, blk_seq_num);
226 dfu_write_transaction_cleanup(dfu);
230 /* DFU 1.1 standard says:
231 * The wBlockNum field is a block sequence number. It increments each
232 * time a block is transferred, wrapping to zero from 65,535. It is used
233 * to provide useful context to the DFU loader in the device."
235 * This means that it's a 16 bit counter that roll-overs at
236 * 0xffff -> 0x0000. By having a typical 4K transfer block
237 * we roll-over at exactly 256MB. Not very fun to debug.
239 * Handling rollover, and having an inited variable,
243 /* handle rollover */
244 dfu->i_blk_seq_num = (dfu->i_blk_seq_num + 1) & 0xffff;
246 /* flush buffer if overflow */
247 if ((dfu->i_buf + size) > dfu->i_buf_end) {
248 ret = dfu_write_buffer_drain(dfu);
250 dfu_write_transaction_cleanup(dfu);
255 /* we should be in buffer now (if not then size too large) */
256 if ((dfu->i_buf + size) > dfu->i_buf_end) {
257 error("Buffer overflow! (0x%p + 0x%x > 0x%p)\n", dfu->i_buf,
258 size, dfu->i_buf_end);
259 dfu_write_transaction_cleanup(dfu);
263 memcpy(dfu->i_buf, buf, size);
266 /* if end or if buffer full flush */
267 if (size == 0 || (dfu->i_buf + size) > dfu->i_buf_end) {
268 ret = dfu_write_buffer_drain(dfu);
270 dfu_write_transaction_cleanup(dfu);
278 static int dfu_read_buffer_fill(struct dfu_entity *dfu, void *buf, int size)
285 /* get chunk that can be read */
286 chunk = min((long)size, dfu->b_left);
289 memcpy(buf, dfu->i_buf, chunk);
291 dfu_hash_algo->hash_update(dfu_hash_algo,
296 dfu->b_left -= chunk;
304 /* no more to read */
305 if (dfu->r_left == 0)
308 dfu->i_buf = dfu->i_buf_start;
309 dfu->b_left = dfu->i_buf_end - dfu->i_buf_start;
311 /* got to read, but buffer is empty */
312 if (dfu->b_left > dfu->r_left)
313 dfu->b_left = dfu->r_left;
314 ret = dfu->read_medium(dfu, dfu->offset, dfu->i_buf,
317 debug("%s: Read error!\n", __func__);
320 dfu->offset += dfu->b_left;
321 dfu->r_left -= dfu->b_left;
330 int dfu_read(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
334 debug("%s: name: %s buf: 0x%p size: 0x%x p_num: 0x%x i_buf: 0x%p\n",
335 __func__, dfu->name, buf, size, blk_seq_num, dfu->i_buf);
338 dfu->i_buf_start = dfu_get_buf(dfu);
339 if (dfu->i_buf_start == NULL)
342 dfu->r_left = dfu->get_medium_size(dfu);
346 debug("%s: %s %ld [B]\n", __func__, dfu->name, dfu->r_left);
348 dfu->i_blk_seq_num = 0;
351 dfu->i_buf_end = dfu_get_buf(dfu) + dfu_buf_size;
352 dfu->i_buf = dfu->i_buf_start;
360 if (dfu->i_blk_seq_num != blk_seq_num) {
361 printf("%s: Wrong sequence number! [%d] [%d]\n",
362 __func__, dfu->i_blk_seq_num, blk_seq_num);
365 /* handle rollover */
366 dfu->i_blk_seq_num = (dfu->i_blk_seq_num + 1) & 0xffff;
368 ret = dfu_read_buffer_fill(dfu, buf, size);
370 printf("%s: Failed to fill buffer\n", __func__);
376 debug("%s: %s %s: 0x%x\n", __func__, dfu->name,
377 dfu_hash_algo->name, dfu->crc);
378 puts("\nUPLOAD ... done\nCtrl+C to exit ...\n");
380 dfu->i_blk_seq_num = 0;
383 dfu->i_buf_start = dfu_buf;
384 dfu->i_buf_end = dfu_buf;
385 dfu->i_buf = dfu->i_buf_start;
396 static int dfu_fill_entity(struct dfu_entity *dfu, char *s, int alt,
397 char *interface, char *devstr)
401 debug("%s: %s interface: %s dev: %s\n", __func__, s, interface, devstr);
402 st = strsep(&s, " ");
403 strcpy(dfu->name, st);
406 dfu->max_buf_size = 0;
407 dfu->free_entity = NULL;
409 /* Specific for mmc device */
410 if (strcmp(interface, "mmc") == 0) {
411 if (dfu_fill_entity_mmc(dfu, devstr, s))
413 } else if (strcmp(interface, "nand") == 0) {
414 if (dfu_fill_entity_nand(dfu, devstr, s))
416 } else if (strcmp(interface, "ram") == 0) {
417 if (dfu_fill_entity_ram(dfu, devstr, s))
419 } else if (strcmp(interface, "sf") == 0) {
420 if (dfu_fill_entity_sf(dfu, devstr, s))
423 printf("%s: Device %s not (yet) supported!\n",
424 __func__, interface);
432 void dfu_free_entities(void)
434 struct dfu_entity *dfu, *p, *t = NULL;
437 list_for_each_entry_safe_reverse(dfu, p, &dfu_list, list) {
438 list_del(&dfu->list);
439 if (dfu->free_entity)
440 dfu->free_entity(dfu);
445 INIT_LIST_HEAD(&dfu_list);
450 int dfu_config_entities(char *env, char *interface, char *devstr)
452 struct dfu_entity *dfu;
456 dfu_alt_num = dfu_find_alt_num(env);
457 debug("%s: dfu_alt_num=%d\n", __func__, dfu_alt_num);
459 dfu_hash_algo = NULL;
460 s = dfu_get_hash_algo();
462 ret = hash_lookup_algo(s, &dfu_hash_algo);
464 error("Hash algorithm %s not supported\n", s);
467 dfu = calloc(sizeof(*dfu), dfu_alt_num);
470 for (i = 0; i < dfu_alt_num; i++) {
472 s = strsep(&env, ";");
473 ret = dfu_fill_entity(&dfu[i], s, alt_num_cnt, interface,
480 list_add_tail(&dfu[i].list, &dfu_list);
487 const char *dfu_get_dev_type(enum dfu_device_type t)
489 const char *dev_t[] = {NULL, "eMMC", "OneNAND", "NAND", "RAM", "SF" };
493 const char *dfu_get_layout(enum dfu_layout l)
495 const char *dfu_layout[] = {NULL, "RAW_ADDR", "FAT", "EXT2",
496 "EXT3", "EXT4", "RAM_ADDR" };
497 return dfu_layout[l];
500 void dfu_show_entities(void)
502 struct dfu_entity *dfu;
504 puts("DFU alt settings list:\n");
506 list_for_each_entry(dfu, &dfu_list, list) {
507 printf("dev: %s alt: %d name: %s layout: %s\n",
508 dfu_get_dev_type(dfu->dev_type), dfu->alt,
509 dfu->name, dfu_get_layout(dfu->layout));
513 int dfu_get_alt_number(void)
518 struct dfu_entity *dfu_get_entity(int alt)
520 struct dfu_entity *dfu;
522 list_for_each_entry(dfu, &dfu_list, list) {
530 int dfu_get_alt(char *name)
532 struct dfu_entity *dfu;
535 list_for_each_entry(dfu, &dfu_list, list) {
536 if (dfu->name[0] != '/') {
537 if (!strncmp(dfu->name, name, strlen(dfu->name)))
541 * One must also consider absolute path
542 * (/boot/bin/uImage) available at dfu->name when
543 * compared "plain" file name (uImage)
545 * It is the case for e.g. thor gadget where lthor SW
546 * sends only the file name, so only the very last part
547 * of path must be checked for equality
550 str = strstr(dfu->name, name);
555 * Check if matching substring is the last element of
558 if (strlen(dfu->name) ==
559 ((str - dfu->name) + strlen(name)))
567 int dfu_write_from_mem_addr(struct dfu_entity *dfu, void *buf, int size)
569 unsigned long dfu_buf_size, write, left = size;
574 * Here we must call dfu_get_buf(dfu) first to be sure that dfu_buf_size
575 * has been properly initialized - e.g. if "dfu_bufsiz" has been taken
579 dfu_buf_size = dfu_get_buf_size();
580 debug("%s: dfu buf size: %lu\n", __func__, dfu_buf_size);
582 for (i = 0; left > 0; i++) {
583 write = min(dfu_buf_size, left);
585 debug("%s: dp: 0x%p left: %lu write: %lu\n", __func__,
587 ret = dfu_write(dfu, dp, write, i);
589 error("DFU write failed\n");
597 ret = dfu_flush(dfu, NULL, 0, i);
599 error("DFU flush failed!");