1 // SPDX-License-Identifier: GPL-2.0+
3 * dfu.c -- DFU back-end routines
5 * Copyright (C) 2012 Samsung Electronics
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 = env_get("dfu_alt_info");
67 pr_err("\"dfu_alt_info\" env variable not defined!\n");
71 env_bkp = strdup(str_env);
72 ret = dfu_config_entities(env_bkp, interface, devstr);
74 pr_err("DFU entities configuration failed!\n");
75 pr_err("(partition table does not match dfu_alt_info?)\n");
84 static unsigned char *dfu_buf;
85 static unsigned long dfu_buf_size;
87 unsigned char *dfu_free_buf(void)
94 unsigned long dfu_get_buf_size(void)
99 unsigned char *dfu_get_buf(struct dfu_entity *dfu)
106 s = env_get("dfu_bufsiz");
108 dfu_buf_size = (unsigned long)simple_strtol(s, NULL, 0);
110 if (!s || !dfu_buf_size)
111 dfu_buf_size = CONFIG_SYS_DFU_DATA_BUF_SIZE;
113 if (dfu->max_buf_size && dfu_buf_size > dfu->max_buf_size)
114 dfu_buf_size = dfu->max_buf_size;
116 dfu_buf = memalign(CONFIG_SYS_CACHELINE_SIZE, dfu_buf_size);
118 printf("%s: Could not memalign 0x%lx bytes\n",
119 __func__, dfu_buf_size);
124 static char *dfu_get_hash_algo(void)
128 s = env_get("dfu_hash_algo");
132 if (!strcmp(s, "crc32")) {
133 debug("%s: DFU hash method: %s\n", __func__, s);
137 pr_err("DFU hash method: %s not supported!\n", s);
141 static int dfu_write_buffer_drain(struct dfu_entity *dfu)
147 w_size = dfu->i_buf - dfu->i_buf_start;
152 dfu_hash_algo->hash_update(dfu_hash_algo, &dfu->crc,
153 dfu->i_buf_start, w_size, 0);
155 ret = dfu->write_medium(dfu, dfu->offset, dfu->i_buf_start, &w_size);
157 debug("%s: Write error!\n", __func__);
160 dfu->i_buf = dfu->i_buf_start;
163 dfu->offset += w_size;
170 void dfu_transaction_cleanup(struct dfu_entity *dfu)
172 /* clear everything */
175 dfu->i_blk_seq_num = 0;
176 dfu->i_buf_start = dfu_get_buf(dfu);
177 dfu->i_buf_end = dfu->i_buf_start;
178 dfu->i_buf = dfu->i_buf_start;
186 int dfu_transaction_initiate(struct dfu_entity *dfu, bool read)
193 dfu_transaction_cleanup(dfu);
195 if (dfu->i_buf_start == NULL)
198 dfu->i_buf_end = dfu->i_buf_start + dfu_get_buf_size();
201 ret = dfu->get_medium_size(dfu, &dfu->r_left);
204 debug("%s: %s %lld [B]\n", __func__, dfu->name, dfu->r_left);
212 int dfu_flush(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
216 ret = dfu_write_buffer_drain(dfu);
220 if (dfu->flush_medium)
221 ret = dfu->flush_medium(dfu);
224 printf("\nDFU complete %s: 0x%08x\n", dfu_hash_algo->name,
227 dfu_transaction_cleanup(dfu);
232 int dfu_write(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
236 debug("%s: name: %s buf: 0x%p size: 0x%x p_num: 0x%x offset: 0x%llx bufoffset: 0x%lx\n",
237 __func__, dfu->name, buf, size, blk_seq_num, dfu->offset,
238 (unsigned long)(dfu->i_buf - dfu->i_buf_start));
240 ret = dfu_transaction_initiate(dfu, false);
244 if (dfu->i_blk_seq_num != blk_seq_num) {
245 printf("%s: Wrong sequence number! [%d] [%d]\n",
246 __func__, dfu->i_blk_seq_num, blk_seq_num);
247 dfu_transaction_cleanup(dfu);
251 /* DFU 1.1 standard says:
252 * The wBlockNum field is a block sequence number. It increments each
253 * time a block is transferred, wrapping to zero from 65,535. It is used
254 * to provide useful context to the DFU loader in the device."
256 * This means that it's a 16 bit counter that roll-overs at
257 * 0xffff -> 0x0000. By having a typical 4K transfer block
258 * we roll-over at exactly 256MB. Not very fun to debug.
260 * Handling rollover, and having an inited variable,
264 /* handle rollover */
265 dfu->i_blk_seq_num = (dfu->i_blk_seq_num + 1) & 0xffff;
267 /* flush buffer if overflow */
268 if ((dfu->i_buf + size) > dfu->i_buf_end) {
269 ret = dfu_write_buffer_drain(dfu);
271 dfu_transaction_cleanup(dfu);
276 /* we should be in buffer now (if not then size too large) */
277 if ((dfu->i_buf + size) > dfu->i_buf_end) {
278 pr_err("Buffer overflow! (0x%p + 0x%x > 0x%p)\n", dfu->i_buf,
279 size, dfu->i_buf_end);
280 dfu_transaction_cleanup(dfu);
284 memcpy(dfu->i_buf, buf, size);
287 /* if end or if buffer full flush */
288 if (size == 0 || (dfu->i_buf + size) > dfu->i_buf_end) {
289 ret = dfu_write_buffer_drain(dfu);
291 dfu_transaction_cleanup(dfu);
299 static int dfu_read_buffer_fill(struct dfu_entity *dfu, void *buf, int size)
306 /* get chunk that can be read */
307 chunk = min((long)size, dfu->b_left);
310 memcpy(buf, dfu->i_buf, chunk);
312 dfu_hash_algo->hash_update(dfu_hash_algo,
317 dfu->b_left -= chunk;
325 /* no more to read */
326 if (dfu->r_left == 0)
329 dfu->i_buf = dfu->i_buf_start;
330 dfu->b_left = dfu->i_buf_end - dfu->i_buf_start;
332 /* got to read, but buffer is empty */
333 if (dfu->b_left > dfu->r_left)
334 dfu->b_left = dfu->r_left;
335 ret = dfu->read_medium(dfu, dfu->offset, dfu->i_buf,
338 debug("%s: Read error!\n", __func__);
341 dfu->offset += dfu->b_left;
342 dfu->r_left -= dfu->b_left;
351 int dfu_read(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
355 debug("%s: name: %s buf: 0x%p size: 0x%x p_num: 0x%x i_buf: 0x%p\n",
356 __func__, dfu->name, buf, size, blk_seq_num, dfu->i_buf);
358 ret = dfu_transaction_initiate(dfu, true);
362 if (dfu->i_blk_seq_num != blk_seq_num) {
363 printf("%s: Wrong sequence number! [%d] [%d]\n",
364 __func__, dfu->i_blk_seq_num, blk_seq_num);
367 /* handle rollover */
368 dfu->i_blk_seq_num = (dfu->i_blk_seq_num + 1) & 0xffff;
370 ret = dfu_read_buffer_fill(dfu, buf, size);
372 printf("%s: Failed to fill buffer\n", __func__);
378 debug("%s: %s %s: 0x%x\n", __func__, dfu->name,
379 dfu_hash_algo->name, dfu->crc);
380 puts("\nUPLOAD ... done\nCtrl+C to exit ...\n");
382 dfu_transaction_cleanup(dfu);
388 static int dfu_fill_entity(struct dfu_entity *dfu, char *s, int alt,
389 char *interface, char *devstr)
393 debug("%s: %s interface: %s dev: %s\n", __func__, s, interface, devstr);
394 st = strsep(&s, " ");
395 strcpy(dfu->name, st);
398 dfu->max_buf_size = 0;
399 dfu->free_entity = NULL;
401 /* Specific for mmc device */
402 if (strcmp(interface, "mmc") == 0) {
403 if (dfu_fill_entity_mmc(dfu, devstr, s))
405 } else if (strcmp(interface, "nand") == 0) {
406 if (dfu_fill_entity_nand(dfu, devstr, s))
408 } else if (strcmp(interface, "ram") == 0) {
409 if (dfu_fill_entity_ram(dfu, devstr, s))
411 } else if (strcmp(interface, "sf") == 0) {
412 if (dfu_fill_entity_sf(dfu, devstr, s))
415 printf("%s: Device %s not (yet) supported!\n",
416 __func__, interface);
424 void dfu_free_entities(void)
426 struct dfu_entity *dfu, *p, *t = NULL;
429 list_for_each_entry_safe_reverse(dfu, p, &dfu_list, list) {
430 list_del(&dfu->list);
431 if (dfu->free_entity)
432 dfu->free_entity(dfu);
437 INIT_LIST_HEAD(&dfu_list);
442 int dfu_config_entities(char *env, char *interface, char *devstr)
444 struct dfu_entity *dfu;
448 dfu_alt_num = dfu_find_alt_num(env);
449 debug("%s: dfu_alt_num=%d\n", __func__, dfu_alt_num);
451 dfu_hash_algo = NULL;
452 s = dfu_get_hash_algo();
454 ret = hash_lookup_algo(s, &dfu_hash_algo);
456 pr_err("Hash algorithm %s not supported\n", s);
459 dfu = calloc(sizeof(*dfu), dfu_alt_num);
462 for (i = 0; i < dfu_alt_num; i++) {
464 s = strsep(&env, ";");
465 ret = dfu_fill_entity(&dfu[i], s, alt_num_cnt, interface,
468 /* We will free "dfu" in dfu_free_entities() */
472 list_add_tail(&dfu[i].list, &dfu_list);
479 const char *dfu_get_dev_type(enum dfu_device_type t)
481 const char *dev_t[] = {NULL, "eMMC", "OneNAND", "NAND", "RAM", "SF" };
485 const char *dfu_get_layout(enum dfu_layout l)
487 const char *dfu_layout[] = {NULL, "RAW_ADDR", "FAT", "EXT2",
488 "EXT3", "EXT4", "RAM_ADDR" };
489 return dfu_layout[l];
492 void dfu_show_entities(void)
494 struct dfu_entity *dfu;
496 puts("DFU alt settings list:\n");
498 list_for_each_entry(dfu, &dfu_list, list) {
499 printf("dev: %s alt: %d name: %s layout: %s\n",
500 dfu_get_dev_type(dfu->dev_type), dfu->alt,
501 dfu->name, dfu_get_layout(dfu->layout));
505 int dfu_get_alt_number(void)
510 struct dfu_entity *dfu_get_entity(int alt)
512 struct dfu_entity *dfu;
514 list_for_each_entry(dfu, &dfu_list, list) {
522 int dfu_get_alt(char *name)
524 struct dfu_entity *dfu;
527 list_for_each_entry(dfu, &dfu_list, list) {
528 if (dfu->name[0] != '/') {
529 if (!strncmp(dfu->name, name, strlen(dfu->name)))
533 * One must also consider absolute path
534 * (/boot/bin/uImage) available at dfu->name when
535 * compared "plain" file name (uImage)
537 * It is the case for e.g. thor gadget where lthor SW
538 * sends only the file name, so only the very last part
539 * of path must be checked for equality
542 str = strstr(dfu->name, name);
547 * Check if matching substring is the last element of
550 if (strlen(dfu->name) ==
551 ((str - dfu->name) + strlen(name)))
559 int dfu_write_from_mem_addr(struct dfu_entity *dfu, void *buf, int size)
561 unsigned long dfu_buf_size, write, left = size;
566 * Here we must call dfu_get_buf(dfu) first to be sure that dfu_buf_size
567 * has been properly initialized - e.g. if "dfu_bufsiz" has been taken
571 dfu_buf_size = dfu_get_buf_size();
572 debug("%s: dfu buf size: %lu\n", __func__, dfu_buf_size);
574 for (i = 0; left > 0; i++) {
575 write = min(dfu_buf_size, left);
577 debug("%s: dp: 0x%p left: %lu write: %lu\n", __func__,
579 ret = dfu_write(dfu, dp, write, i);
581 pr_err("DFU write failed\n");
589 ret = dfu_flush(dfu, NULL, 0, i);
591 pr_err("DFU flush failed!");