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)
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 = CONFIG_SYS_DFU_DATA_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 */
170 dfu->i_blk_seq_num = 0;
171 dfu->i_buf_start = dfu_buf;
172 dfu->i_buf_end = dfu_buf;
173 dfu->i_buf = dfu->i_buf_start;
177 int dfu_flush(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
181 ret = dfu_write_buffer_drain(dfu);
185 if (dfu->flush_medium)
186 ret = dfu->flush_medium(dfu);
189 printf("\nDFU complete %s: 0x%08x\n", dfu_hash_algo->name,
192 dfu_write_transaction_cleanup(dfu);
197 int dfu_write(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
201 debug("%s: name: %s buf: 0x%p size: 0x%x p_num: 0x%x offset: 0x%llx bufoffset: 0x%x\n",
202 __func__, dfu->name, buf, size, blk_seq_num, dfu->offset,
203 dfu->i_buf - dfu->i_buf_start);
210 dfu->i_blk_seq_num = 0;
211 dfu->i_buf_start = dfu_get_buf(dfu);
212 if (dfu->i_buf_start == NULL)
214 dfu->i_buf_end = dfu_get_buf(dfu) + dfu_buf_size;
215 dfu->i_buf = dfu->i_buf_start;
220 if (dfu->i_blk_seq_num != blk_seq_num) {
221 printf("%s: Wrong sequence number! [%d] [%d]\n",
222 __func__, dfu->i_blk_seq_num, blk_seq_num);
223 dfu_write_transaction_cleanup(dfu);
227 /* DFU 1.1 standard says:
228 * The wBlockNum field is a block sequence number. It increments each
229 * time a block is transferred, wrapping to zero from 65,535. It is used
230 * to provide useful context to the DFU loader in the device."
232 * This means that it's a 16 bit counter that roll-overs at
233 * 0xffff -> 0x0000. By having a typical 4K transfer block
234 * we roll-over at exactly 256MB. Not very fun to debug.
236 * Handling rollover, and having an inited variable,
240 /* handle rollover */
241 dfu->i_blk_seq_num = (dfu->i_blk_seq_num + 1) & 0xffff;
243 /* flush buffer if overflow */
244 if ((dfu->i_buf + size) > dfu->i_buf_end) {
245 ret = dfu_write_buffer_drain(dfu);
247 dfu_write_transaction_cleanup(dfu);
252 /* we should be in buffer now (if not then size too large) */
253 if ((dfu->i_buf + size) > dfu->i_buf_end) {
254 error("Buffer overflow! (0x%p + 0x%x > 0x%p)\n", dfu->i_buf,
255 size, dfu->i_buf_end);
256 dfu_write_transaction_cleanup(dfu);
260 memcpy(dfu->i_buf, buf, size);
263 /* if end or if buffer full flush */
264 if (size == 0 || (dfu->i_buf + size) > dfu->i_buf_end) {
265 ret = dfu_write_buffer_drain(dfu);
267 dfu_write_transaction_cleanup(dfu);
275 static int dfu_read_buffer_fill(struct dfu_entity *dfu, void *buf, int size)
282 /* get chunk that can be read */
283 chunk = min((long)size, dfu->b_left);
286 memcpy(buf, dfu->i_buf, chunk);
288 dfu_hash_algo->hash_update(dfu_hash_algo,
293 dfu->b_left -= chunk;
301 /* no more to read */
302 if (dfu->r_left == 0)
305 dfu->i_buf = dfu->i_buf_start;
306 dfu->b_left = dfu->i_buf_end - dfu->i_buf_start;
308 /* got to read, but buffer is empty */
309 if (dfu->b_left > dfu->r_left)
310 dfu->b_left = dfu->r_left;
311 ret = dfu->read_medium(dfu, dfu->offset, dfu->i_buf,
314 debug("%s: Read error!\n", __func__);
317 dfu->offset += dfu->b_left;
318 dfu->r_left -= dfu->b_left;
327 int dfu_read(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
331 debug("%s: name: %s buf: 0x%p size: 0x%x p_num: 0x%x i_buf: 0x%p\n",
332 __func__, dfu->name, buf, size, blk_seq_num, dfu->i_buf);
335 dfu->i_buf_start = dfu_get_buf(dfu);
336 if (dfu->i_buf_start == NULL)
339 dfu->r_left = dfu->get_medium_size(dfu);
342 switch (dfu->layout) {
347 if (dfu->r_left > dfu_buf_size) {
348 printf("%s: File too big for buffer\n",
354 debug("%s: %s %ld [B]\n", __func__, dfu->name, dfu->r_left);
356 dfu->i_blk_seq_num = 0;
359 dfu->i_buf_end = dfu_get_buf(dfu) + dfu_buf_size;
360 dfu->i_buf = dfu->i_buf_start;
368 if (dfu->i_blk_seq_num != blk_seq_num) {
369 printf("%s: Wrong sequence number! [%d] [%d]\n",
370 __func__, dfu->i_blk_seq_num, blk_seq_num);
373 /* handle rollover */
374 dfu->i_blk_seq_num = (dfu->i_blk_seq_num + 1) & 0xffff;
376 ret = dfu_read_buffer_fill(dfu, buf, size);
378 printf("%s: Failed to fill buffer\n", __func__);
384 debug("%s: %s %s: 0x%x\n", __func__, dfu->name,
385 dfu_hash_algo->name, dfu->crc);
386 puts("\nUPLOAD ... done\nCtrl+C to exit ...\n");
389 dfu->i_blk_seq_num = 0;
392 dfu->i_buf_start = dfu_buf;
393 dfu->i_buf_end = dfu_buf;
394 dfu->i_buf = dfu->i_buf_start;
405 static int dfu_fill_entity(struct dfu_entity *dfu, char *s, int alt,
406 char *interface, char *devstr)
410 debug("%s: %s interface: %s dev: %s\n", __func__, s, interface, devstr);
411 st = strsep(&s, " ");
412 strcpy(dfu->name, st);
415 dfu->max_buf_size = 0;
416 dfu->free_entity = NULL;
418 /* Specific for mmc device */
419 if (strcmp(interface, "mmc") == 0) {
420 if (dfu_fill_entity_mmc(dfu, devstr, s))
422 } else if (strcmp(interface, "nand") == 0) {
423 if (dfu_fill_entity_nand(dfu, devstr, s))
425 } else if (strcmp(interface, "ram") == 0) {
426 if (dfu_fill_entity_ram(dfu, devstr, s))
428 } else if (strcmp(interface, "sf") == 0) {
429 if (dfu_fill_entity_sf(dfu, devstr, s))
432 printf("%s: Device %s not (yet) supported!\n",
433 __func__, interface);
440 void dfu_free_entities(void)
442 struct dfu_entity *dfu, *p, *t = NULL;
444 list_for_each_entry_safe_reverse(dfu, p, &dfu_list, list) {
445 list_del(&dfu->list);
446 if (dfu->free_entity)
447 dfu->free_entity(dfu);
452 INIT_LIST_HEAD(&dfu_list);
457 int dfu_config_entities(char *env, char *interface, char *devstr)
459 struct dfu_entity *dfu;
463 dfu_alt_num = dfu_find_alt_num(env);
464 debug("%s: dfu_alt_num=%d\n", __func__, dfu_alt_num);
466 dfu_hash_algo = NULL;
467 s = dfu_get_hash_algo();
469 ret = hash_lookup_algo(s, &dfu_hash_algo);
471 error("Hash algorithm %s not supported\n", s);
474 dfu = calloc(sizeof(*dfu), dfu_alt_num);
477 for (i = 0; i < dfu_alt_num; i++) {
479 s = strsep(&env, ";");
480 ret = dfu_fill_entity(&dfu[i], s, alt_num_cnt, interface,
485 list_add_tail(&dfu[i].list, &dfu_list);
492 const char *dfu_get_dev_type(enum dfu_device_type t)
494 const char *dev_t[] = {NULL, "eMMC", "OneNAND", "NAND", "RAM" };
498 const char *dfu_get_layout(enum dfu_layout l)
500 const char *dfu_layout[] = {NULL, "RAW_ADDR", "FAT", "EXT2",
501 "EXT3", "EXT4", "RAM_ADDR" };
502 return dfu_layout[l];
505 void dfu_show_entities(void)
507 struct dfu_entity *dfu;
509 puts("DFU alt settings list:\n");
511 list_for_each_entry(dfu, &dfu_list, list) {
512 printf("dev: %s alt: %d name: %s layout: %s\n",
513 dfu_get_dev_type(dfu->dev_type), dfu->alt,
514 dfu->name, dfu_get_layout(dfu->layout));
518 int dfu_get_alt_number(void)
523 struct dfu_entity *dfu_get_entity(int alt)
525 struct dfu_entity *dfu;
527 list_for_each_entry(dfu, &dfu_list, list) {
535 int dfu_get_alt(char *name)
537 struct dfu_entity *dfu;
540 list_for_each_entry(dfu, &dfu_list, list) {
541 if (dfu->name[0] != '/') {
542 if (!strncmp(dfu->name, name, strlen(dfu->name)))
546 * One must also consider absolute path
547 * (/boot/bin/uImage) available at dfu->name when
548 * compared "plain" file name (uImage)
550 * It is the case for e.g. thor gadget where lthor SW
551 * sends only the file name, so only the very last part
552 * of path must be checked for equality
555 str = strstr(dfu->name, name);
560 * Check if matching substring is the last element of
563 if (strlen(dfu->name) ==
564 ((str - dfu->name) + strlen(name)))