2 * dfu.c -- DFU back-end routines
4 * Copyright (C) 2012 Samsung Electronics
5 * author: Lukasz Majewski <l.majewski@samsung.com>
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)
41 static int dfu_find_alt_num(const char *s)
52 int dfu_init_env_entities(char *interface, char *devstr)
58 #ifdef CONFIG_SET_DFU_ALT_INFO
59 set_dfu_alt_info(interface, devstr);
61 str_env = getenv("dfu_alt_info");
63 error("\"dfu_alt_info\" env variable not defined!\n");
67 env_bkp = strdup(str_env);
68 ret = dfu_config_entities(env_bkp, interface, devstr);
70 error("DFU entities configuration failed!\n");
78 static unsigned char *dfu_buf;
79 static unsigned long dfu_buf_size;
81 unsigned char *dfu_free_buf(void)
88 unsigned long dfu_get_buf_size(void)
93 unsigned char *dfu_get_buf(struct dfu_entity *dfu)
100 s = getenv("dfu_bufsiz");
102 dfu_buf_size = (unsigned long)simple_strtol(s, NULL, 0);
104 if (!s || !dfu_buf_size)
105 dfu_buf_size = CONFIG_SYS_DFU_DATA_BUF_SIZE;
107 if (dfu->max_buf_size && dfu_buf_size > dfu->max_buf_size)
108 dfu_buf_size = dfu->max_buf_size;
110 dfu_buf = memalign(CONFIG_SYS_CACHELINE_SIZE, dfu_buf_size);
112 printf("%s: Could not memalign 0x%lx bytes\n",
113 __func__, dfu_buf_size);
118 static char *dfu_get_hash_algo(void)
122 s = getenv("dfu_hash_algo");
126 if (!strcmp(s, "crc32")) {
127 debug("%s: DFU hash method: %s\n", __func__, s);
131 error("DFU hash method: %s not supported!\n", s);
135 static int dfu_write_buffer_drain(struct dfu_entity *dfu)
141 w_size = dfu->i_buf - dfu->i_buf_start;
146 dfu_hash_algo->hash_update(dfu_hash_algo, &dfu->crc,
147 dfu->i_buf_start, w_size, 0);
149 ret = dfu->write_medium(dfu, dfu->offset, dfu->i_buf_start, &w_size);
151 debug("%s: Write error!\n", __func__);
154 dfu->i_buf = dfu->i_buf_start;
157 dfu->offset += w_size;
164 void dfu_write_transaction_cleanup(struct dfu_entity *dfu)
166 /* clear everything */
169 dfu->i_blk_seq_num = 0;
170 dfu->i_buf_start = dfu_buf;
171 dfu->i_buf_end = dfu_buf;
172 dfu->i_buf = dfu->i_buf_start;
176 int dfu_flush(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
180 ret = dfu_write_buffer_drain(dfu);
184 if (dfu->flush_medium)
185 ret = dfu->flush_medium(dfu);
188 printf("\nDFU complete %s: 0x%08x\n", dfu_hash_algo->name,
191 dfu_write_transaction_cleanup(dfu);
196 int dfu_write(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
200 debug("%s: name: %s buf: 0x%p size: 0x%x p_num: 0x%x offset: 0x%llx bufoffset: 0x%lx\n",
201 __func__, dfu->name, buf, size, blk_seq_num, dfu->offset,
202 (unsigned long)(dfu->i_buf - dfu->i_buf_start));
209 dfu->i_blk_seq_num = 0;
210 dfu->i_buf_start = dfu_get_buf(dfu);
211 if (dfu->i_buf_start == NULL)
213 dfu->i_buf_end = dfu_get_buf(dfu) + dfu_buf_size;
214 dfu->i_buf = dfu->i_buf_start;
219 if (dfu->i_blk_seq_num != blk_seq_num) {
220 printf("%s: Wrong sequence number! [%d] [%d]\n",
221 __func__, dfu->i_blk_seq_num, blk_seq_num);
222 dfu_write_transaction_cleanup(dfu);
226 /* DFU 1.1 standard says:
227 * The wBlockNum field is a block sequence number. It increments each
228 * time a block is transferred, wrapping to zero from 65,535. It is used
229 * to provide useful context to the DFU loader in the device."
231 * This means that it's a 16 bit counter that roll-overs at
232 * 0xffff -> 0x0000. By having a typical 4K transfer block
233 * we roll-over at exactly 256MB. Not very fun to debug.
235 * Handling rollover, and having an inited variable,
239 /* handle rollover */
240 dfu->i_blk_seq_num = (dfu->i_blk_seq_num + 1) & 0xffff;
242 /* flush buffer if overflow */
243 if ((dfu->i_buf + size) > dfu->i_buf_end) {
244 ret = dfu_write_buffer_drain(dfu);
246 dfu_write_transaction_cleanup(dfu);
251 /* we should be in buffer now (if not then size too large) */
252 if ((dfu->i_buf + size) > dfu->i_buf_end) {
253 error("Buffer overflow! (0x%p + 0x%x > 0x%p)\n", dfu->i_buf,
254 size, dfu->i_buf_end);
255 dfu_write_transaction_cleanup(dfu);
259 memcpy(dfu->i_buf, buf, size);
262 /* if end or if buffer full flush */
263 if (size == 0 || (dfu->i_buf + size) > dfu->i_buf_end) {
264 ret = dfu_write_buffer_drain(dfu);
266 dfu_write_transaction_cleanup(dfu);
274 static int dfu_read_buffer_fill(struct dfu_entity *dfu, void *buf, int size)
281 /* get chunk that can be read */
282 chunk = min((long)size, dfu->b_left);
285 memcpy(buf, dfu->i_buf, chunk);
287 dfu_hash_algo->hash_update(dfu_hash_algo,
292 dfu->b_left -= chunk;
300 /* no more to read */
301 if (dfu->r_left == 0)
304 dfu->i_buf = dfu->i_buf_start;
305 dfu->b_left = dfu->i_buf_end - dfu->i_buf_start;
307 /* got to read, but buffer is empty */
308 if (dfu->b_left > dfu->r_left)
309 dfu->b_left = dfu->r_left;
310 ret = dfu->read_medium(dfu, dfu->offset, dfu->i_buf,
313 debug("%s: Read error!\n", __func__);
316 dfu->offset += dfu->b_left;
317 dfu->r_left -= dfu->b_left;
326 int dfu_read(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
330 debug("%s: name: %s buf: 0x%p size: 0x%x p_num: 0x%x i_buf: 0x%p\n",
331 __func__, dfu->name, buf, size, blk_seq_num, dfu->i_buf);
334 dfu->i_buf_start = dfu_get_buf(dfu);
335 if (dfu->i_buf_start == NULL)
338 dfu->r_left = dfu->get_medium_size(dfu);
342 debug("%s: %s %ld [B]\n", __func__, dfu->name, dfu->r_left);
344 dfu->i_blk_seq_num = 0;
347 dfu->i_buf_end = dfu_get_buf(dfu) + dfu_buf_size;
348 dfu->i_buf = dfu->i_buf_start;
356 if (dfu->i_blk_seq_num != blk_seq_num) {
357 printf("%s: Wrong sequence number! [%d] [%d]\n",
358 __func__, dfu->i_blk_seq_num, blk_seq_num);
361 /* handle rollover */
362 dfu->i_blk_seq_num = (dfu->i_blk_seq_num + 1) & 0xffff;
364 ret = dfu_read_buffer_fill(dfu, buf, size);
366 printf("%s: Failed to fill buffer\n", __func__);
372 debug("%s: %s %s: 0x%x\n", __func__, dfu->name,
373 dfu_hash_algo->name, dfu->crc);
374 puts("\nUPLOAD ... done\nCtrl+C to exit ...\n");
376 dfu->i_blk_seq_num = 0;
379 dfu->i_buf_start = dfu_buf;
380 dfu->i_buf_end = dfu_buf;
381 dfu->i_buf = dfu->i_buf_start;
392 static int dfu_fill_entity(struct dfu_entity *dfu, char *s, int alt,
393 char *interface, char *devstr)
397 debug("%s: %s interface: %s dev: %s\n", __func__, s, interface, devstr);
398 st = strsep(&s, " ");
399 strcpy(dfu->name, st);
402 dfu->max_buf_size = 0;
403 dfu->free_entity = NULL;
405 /* Specific for mmc device */
406 if (strcmp(interface, "mmc") == 0) {
407 if (dfu_fill_entity_mmc(dfu, devstr, s))
409 } else if (strcmp(interface, "nand") == 0) {
410 if (dfu_fill_entity_nand(dfu, devstr, s))
412 } else if (strcmp(interface, "ram") == 0) {
413 if (dfu_fill_entity_ram(dfu, devstr, s))
415 } else if (strcmp(interface, "sf") == 0) {
416 if (dfu_fill_entity_sf(dfu, devstr, s))
419 printf("%s: Device %s not (yet) supported!\n",
420 __func__, interface);
428 void dfu_free_entities(void)
430 struct dfu_entity *dfu, *p, *t = NULL;
433 list_for_each_entry_safe_reverse(dfu, p, &dfu_list, list) {
434 list_del(&dfu->list);
435 if (dfu->free_entity)
436 dfu->free_entity(dfu);
441 INIT_LIST_HEAD(&dfu_list);
446 int dfu_config_entities(char *env, char *interface, char *devstr)
448 struct dfu_entity *dfu;
452 dfu_alt_num = dfu_find_alt_num(env);
453 debug("%s: dfu_alt_num=%d\n", __func__, dfu_alt_num);
455 dfu_hash_algo = NULL;
456 s = dfu_get_hash_algo();
458 ret = hash_lookup_algo(s, &dfu_hash_algo);
460 error("Hash algorithm %s not supported\n", s);
463 dfu = calloc(sizeof(*dfu), dfu_alt_num);
466 for (i = 0; i < dfu_alt_num; i++) {
468 s = strsep(&env, ";");
469 ret = dfu_fill_entity(&dfu[i], s, alt_num_cnt, interface,
474 list_add_tail(&dfu[i].list, &dfu_list);
481 const char *dfu_get_dev_type(enum dfu_device_type t)
483 const char *dev_t[] = {NULL, "eMMC", "OneNAND", "NAND", "RAM" };
487 const char *dfu_get_layout(enum dfu_layout l)
489 const char *dfu_layout[] = {NULL, "RAW_ADDR", "FAT", "EXT2",
490 "EXT3", "EXT4", "RAM_ADDR" };
491 return dfu_layout[l];
494 void dfu_show_entities(void)
496 struct dfu_entity *dfu;
498 puts("DFU alt settings list:\n");
500 list_for_each_entry(dfu, &dfu_list, list) {
501 printf("dev: %s alt: %d name: %s layout: %s\n",
502 dfu_get_dev_type(dfu->dev_type), dfu->alt,
503 dfu->name, dfu_get_layout(dfu->layout));
507 int dfu_get_alt_number(void)
512 struct dfu_entity *dfu_get_entity(int alt)
514 struct dfu_entity *dfu;
516 list_for_each_entry(dfu, &dfu_list, list) {
524 int dfu_get_alt(char *name)
526 struct dfu_entity *dfu;
529 list_for_each_entry(dfu, &dfu_list, list) {
530 if (dfu->name[0] != '/') {
531 if (!strncmp(dfu->name, name, strlen(dfu->name)))
535 * One must also consider absolute path
536 * (/boot/bin/uImage) available at dfu->name when
537 * compared "plain" file name (uImage)
539 * It is the case for e.g. thor gadget where lthor SW
540 * sends only the file name, so only the very last part
541 * of path must be checked for equality
544 str = strstr(dfu->name, name);
549 * Check if matching substring is the last element of
552 if (strlen(dfu->name) ==
553 ((str - dfu->name) + strlen(name)))
561 int dfu_write_from_mem_addr(struct dfu_entity *dfu, void *buf, int size)
563 unsigned long dfu_buf_size, write, left = size;
568 * Here we must call dfu_get_buf(dfu) first to be sure that dfu_buf_size
569 * has been properly initialized - e.g. if "dfu_bufsiz" has been taken
573 dfu_buf_size = dfu_get_buf_size();
574 debug("%s: dfu buf size: %lu\n", __func__, dfu_buf_size);
576 for (i = 0; left > 0; i++) {
577 write = min(dfu_buf_size, left);
579 debug("%s: dp: 0x%p left: %lu write: %lu\n", __func__,
581 ret = dfu_write(dfu, dp, write, i);
583 error("DFU write failed\n");
591 ret = dfu_flush(dfu, NULL, 0, i);
593 error("DFU flush failed!");