blob: 6cd3fbb58ae4a6e1f13a4125b8dc536b521fb8ab [file] [log] [blame]
Lukasz Majewskif22b11c2012-08-06 14:41:07 +02001/*
2 * dfu.c -- DFU back-end routines
3 *
4 * Copyright (C) 2012 Samsung Electronics
5 * author: Lukasz Majewski <l.majewski@samsung.com>
6 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02007 * SPDX-License-Identifier: GPL-2.0+
Lukasz Majewskif22b11c2012-08-06 14:41:07 +02008 */
9
10#include <common.h>
Heiko Schochere7e75c72013-06-12 06:05:51 +020011#include <errno.h>
Lukasz Majewskif22b11c2012-08-06 14:41:07 +020012#include <malloc.h>
13#include <mmc.h>
14#include <fat.h>
15#include <dfu.h>
Lukasz Majewskibd694242014-05-12 10:43:36 +020016#include <hash.h>
Lukasz Majewskif22b11c2012-08-06 14:41:07 +020017#include <linux/list.h>
18#include <linux/compiler.h>
19
Lukasz Majewski6bed7ce2013-07-18 13:19:14 +020020static bool dfu_reset_request;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +020021static LIST_HEAD(dfu_list);
22static int dfu_alt_num;
Przemyslaw Marczakb627eb42014-02-28 18:53:37 +010023static int alt_num_cnt;
Lukasz Majewskibd694242014-05-12 10:43:36 +020024static struct hash_algo *dfu_hash_algo;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +020025
Lukasz Majewski6bed7ce2013-07-18 13:19:14 +020026bool dfu_reset(void)
27{
28 return dfu_reset_request;
29}
30
31void dfu_trigger_reset()
32{
33 dfu_reset_request = true;
34}
35
Lukasz Majewskif22b11c2012-08-06 14:41:07 +020036static int dfu_find_alt_num(const char *s)
37{
38 int i = 0;
39
40 for (; *s; s++)
41 if (*s == ';')
42 i++;
43
44 return ++i;
45}
46
Stephen Warrendd648272014-06-11 16:03:33 -060047int dfu_init_env_entities(char *interface, char *devstr)
Lukasz Majewski765c5ae2013-09-11 14:53:35 +020048{
49 const char *str_env;
50 char *env_bkp;
51 int ret;
52
53 str_env = getenv("dfu_alt_info");
54 if (!str_env) {
55 error("\"dfu_alt_info\" env variable not defined!\n");
56 return -EINVAL;
57 }
58
59 env_bkp = strdup(str_env);
Stephen Warrendd648272014-06-11 16:03:33 -060060 ret = dfu_config_entities(env_bkp, interface, devstr);
Lukasz Majewski765c5ae2013-09-11 14:53:35 +020061 if (ret) {
62 error("DFU entities configuration failed!\n");
63 return ret;
64 }
65
66 free(env_bkp);
67 return 0;
68}
69
Heiko Schochere7e75c72013-06-12 06:05:51 +020070static unsigned char *dfu_buf;
71static unsigned long dfu_buf_size = CONFIG_SYS_DFU_DATA_BUF_SIZE;
72
Lukasz Majewskid4278262013-10-08 14:30:39 +020073unsigned char *dfu_free_buf(void)
Heiko Schochere7e75c72013-06-12 06:05:51 +020074{
75 free(dfu_buf);
76 dfu_buf = NULL;
77 return dfu_buf;
78}
79
Lukasz Majewski4fb12782013-12-09 16:20:13 +010080unsigned long dfu_get_buf_size(void)
81{
82 return dfu_buf_size;
83}
84
Stephen Warren7ac1b412014-06-11 16:03:34 -060085unsigned char *dfu_get_buf(struct dfu_entity *dfu)
Heiko Schochere7e75c72013-06-12 06:05:51 +020086{
87 char *s;
88
89 if (dfu_buf != NULL)
90 return dfu_buf;
91
92 s = getenv("dfu_bufsiz");
93 dfu_buf_size = s ? (unsigned long)simple_strtol(s, NULL, 16) :
94 CONFIG_SYS_DFU_DATA_BUF_SIZE;
Stephen Warren7ac1b412014-06-11 16:03:34 -060095 if (dfu->max_buf_size && dfu_buf_size > dfu->max_buf_size)
96 dfu_buf_size = dfu->max_buf_size;
Heiko Schochere7e75c72013-06-12 06:05:51 +020097
98 dfu_buf = memalign(CONFIG_SYS_CACHELINE_SIZE, dfu_buf_size);
99 if (dfu_buf == NULL)
100 printf("%s: Could not memalign 0x%lx bytes\n",
101 __func__, dfu_buf_size);
102
103 return dfu_buf;
104}
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200105
Lukasz Majewskibd694242014-05-12 10:43:36 +0200106static char *dfu_get_hash_algo(void)
107{
108 char *s;
109
110 s = getenv("dfu_hash_algo");
Lukasz Majewski3d83e672014-06-10 12:25:59 +0200111 if (!s)
Lukasz Majewskibd694242014-05-12 10:43:36 +0200112 return NULL;
113
Lukasz Majewski3d83e672014-06-10 12:25:59 +0200114 if (!strcmp(s, "crc32")) {
115 debug("%s: DFU hash method: %s\n", __func__, s);
116 return s;
117 }
118
119 error("DFU hash method: %s not supported!\n", s);
Lukasz Majewskibd694242014-05-12 10:43:36 +0200120 return NULL;
121}
122
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000123static int dfu_write_buffer_drain(struct dfu_entity *dfu)
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200124{
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000125 long w_size;
126 int ret;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200127
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000128 /* flush size? */
129 w_size = dfu->i_buf - dfu->i_buf_start;
130 if (w_size == 0)
131 return 0;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200132
Lukasz Majewskibd694242014-05-12 10:43:36 +0200133 if (dfu_hash_algo)
134 dfu_hash_algo->hash_update(dfu_hash_algo, &dfu->crc,
135 dfu->i_buf_start, w_size, 0);
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200136
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000137 ret = dfu->write_medium(dfu, dfu->offset, dfu->i_buf_start, &w_size);
138 if (ret)
139 debug("%s: Write error!\n", __func__);
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200140
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000141 /* point back */
142 dfu->i_buf = dfu->i_buf_start;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200143
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000144 /* update offset */
145 dfu->offset += w_size;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200146
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000147 puts("#");
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200148
149 return ret;
150}
151
Stephen Warren3ee95932014-06-11 12:48:08 -0600152void dfu_write_transaction_cleanup(struct dfu_entity *dfu)
153{
154 /* clear everything */
155 dfu_free_buf();
156 dfu->crc = 0;
157 dfu->offset = 0;
158 dfu->i_blk_seq_num = 0;
159 dfu->i_buf_start = dfu_buf;
160 dfu->i_buf_end = dfu_buf;
161 dfu->i_buf = dfu->i_buf_start;
162 dfu->inited = 0;
163}
164
Heiko Schochera2199af2014-03-18 08:09:55 +0100165int dfu_flush(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
166{
167 int ret = 0;
168
Przemyslaw Marczak1aa4bdc2014-05-12 12:05:33 +0200169 ret = dfu_write_buffer_drain(dfu);
170 if (ret)
171 return ret;
172
Heiko Schochera2199af2014-03-18 08:09:55 +0100173 if (dfu->flush_medium)
174 ret = dfu->flush_medium(dfu);
175
Lukasz Majewskibd694242014-05-12 10:43:36 +0200176 if (dfu_hash_algo)
177 printf("\nDFU complete %s: 0x%08x\n", dfu_hash_algo->name,
178 dfu->crc);
Heiko Schochera2199af2014-03-18 08:09:55 +0100179
Stephen Warren3ee95932014-06-11 12:48:08 -0600180 dfu_write_transaction_cleanup(dfu);
Heiko Schochera2199af2014-03-18 08:09:55 +0100181
182 return ret;
183}
184
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000185int dfu_write(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200186{
Stephen Warren3ee95932014-06-11 12:48:08 -0600187 int ret;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200188
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000189 debug("%s: name: %s buf: 0x%p size: 0x%x p_num: 0x%x offset: 0x%llx bufoffset: 0x%x\n",
190 __func__, dfu->name, buf, size, blk_seq_num, dfu->offset,
191 dfu->i_buf - dfu->i_buf_start);
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200192
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000193 if (!dfu->inited) {
194 /* initial state */
195 dfu->crc = 0;
196 dfu->offset = 0;
Pantelis Antoniouc6631762013-03-14 05:32:52 +0000197 dfu->bad_skip = 0;
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000198 dfu->i_blk_seq_num = 0;
Stephen Warren7ac1b412014-06-11 16:03:34 -0600199 dfu->i_buf_start = dfu_get_buf(dfu);
Heiko Schochere7e75c72013-06-12 06:05:51 +0200200 if (dfu->i_buf_start == NULL)
201 return -ENOMEM;
Stephen Warren7ac1b412014-06-11 16:03:34 -0600202 dfu->i_buf_end = dfu_get_buf(dfu) + dfu_buf_size;
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000203 dfu->i_buf = dfu->i_buf_start;
204
205 dfu->inited = 1;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200206 }
207
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000208 if (dfu->i_blk_seq_num != blk_seq_num) {
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200209 printf("%s: Wrong sequence number! [%d] [%d]\n",
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000210 __func__, dfu->i_blk_seq_num, blk_seq_num);
Stephen Warren3ee95932014-06-11 12:48:08 -0600211 dfu_write_transaction_cleanup(dfu);
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200212 return -1;
213 }
214
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000215 /* DFU 1.1 standard says:
216 * The wBlockNum field is a block sequence number. It increments each
217 * time a block is transferred, wrapping to zero from 65,535. It is used
218 * to provide useful context to the DFU loader in the device."
219 *
220 * This means that it's a 16 bit counter that roll-overs at
221 * 0xffff -> 0x0000. By having a typical 4K transfer block
222 * we roll-over at exactly 256MB. Not very fun to debug.
223 *
224 * Handling rollover, and having an inited variable,
225 * makes things work.
226 */
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200227
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000228 /* handle rollover */
229 dfu->i_blk_seq_num = (dfu->i_blk_seq_num + 1) & 0xffff;
230
231 /* flush buffer if overflow */
232 if ((dfu->i_buf + size) > dfu->i_buf_end) {
Stephen Warren3ee95932014-06-11 12:48:08 -0600233 ret = dfu_write_buffer_drain(dfu);
234 if (ret) {
235 dfu_write_transaction_cleanup(dfu);
236 return ret;
237 }
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200238 }
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000239
240 /* we should be in buffer now (if not then size too large) */
241 if ((dfu->i_buf + size) > dfu->i_buf_end) {
Lukasz Majewskia7d2c3c2013-09-10 15:29:22 +0200242 error("Buffer overflow! (0x%p + 0x%x > 0x%p)\n", dfu->i_buf,
243 size, dfu->i_buf_end);
Stephen Warren3ee95932014-06-11 12:48:08 -0600244 dfu_write_transaction_cleanup(dfu);
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000245 return -1;
246 }
247
248 memcpy(dfu->i_buf, buf, size);
249 dfu->i_buf += size;
250
251 /* if end or if buffer full flush */
252 if (size == 0 || (dfu->i_buf + size) > dfu->i_buf_end) {
Stephen Warren3ee95932014-06-11 12:48:08 -0600253 ret = dfu_write_buffer_drain(dfu);
254 if (ret) {
255 dfu_write_transaction_cleanup(dfu);
256 return ret;
257 }
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000258 }
259
Stephen Warren3ee95932014-06-11 12:48:08 -0600260 return 0;
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000261}
262
263static int dfu_read_buffer_fill(struct dfu_entity *dfu, void *buf, int size)
264{
265 long chunk;
266 int ret, readn;
267
268 readn = 0;
269 while (size > 0) {
270 /* get chunk that can be read */
271 chunk = min(size, dfu->b_left);
272 /* consume */
273 if (chunk > 0) {
274 memcpy(buf, dfu->i_buf, chunk);
Lukasz Majewskibd694242014-05-12 10:43:36 +0200275 if (dfu_hash_algo)
276 dfu_hash_algo->hash_update(dfu_hash_algo,
277 &dfu->crc, buf,
278 chunk, 0);
279
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000280 dfu->i_buf += chunk;
281 dfu->b_left -= chunk;
282 size -= chunk;
283 buf += chunk;
284 readn += chunk;
285 }
286
287 /* all done */
288 if (size > 0) {
289 /* no more to read */
290 if (dfu->r_left == 0)
291 break;
292
293 dfu->i_buf = dfu->i_buf_start;
294 dfu->b_left = dfu->i_buf_end - dfu->i_buf_start;
295
296 /* got to read, but buffer is empty */
297 if (dfu->b_left > dfu->r_left)
298 dfu->b_left = dfu->r_left;
299 ret = dfu->read_medium(dfu, dfu->offset, dfu->i_buf,
300 &dfu->b_left);
301 if (ret != 0) {
302 debug("%s: Read error!\n", __func__);
303 return ret;
304 }
305 dfu->offset += dfu->b_left;
306 dfu->r_left -= dfu->b_left;
307
308 puts("#");
309 }
310 }
311
312 return readn;
313}
314
315int dfu_read(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
316{
317 int ret = 0;
318
319 debug("%s: name: %s buf: 0x%p size: 0x%x p_num: 0x%x i_buf: 0x%p\n",
320 __func__, dfu->name, buf, size, blk_seq_num, dfu->i_buf);
321
322 if (!dfu->inited) {
Stephen Warren7ac1b412014-06-11 16:03:34 -0600323 dfu->i_buf_start = dfu_get_buf(dfu);
Łukasz Majewski9df49552013-06-28 18:41:50 +0200324 if (dfu->i_buf_start == NULL)
325 return -ENOMEM;
326
Stephen Warren0e285b52014-06-11 12:47:27 -0600327 dfu->r_left = dfu->get_medium_size(dfu);
328 if (dfu->r_left < 0)
329 return dfu->r_left;
330 switch (dfu->layout) {
331 case DFU_RAW_ADDR:
332 case DFU_RAM_ADDR:
333 break;
334 default:
335 if (dfu->r_left >= dfu_buf_size) {
336 printf("%s: File too big for buffer\n",
337 __func__);
338 return -EOVERFLOW;
339 }
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000340 }
341
342 debug("%s: %s %ld [B]\n", __func__, dfu->name, dfu->r_left);
343
344 dfu->i_blk_seq_num = 0;
345 dfu->crc = 0;
346 dfu->offset = 0;
Stephen Warren7ac1b412014-06-11 16:03:34 -0600347 dfu->i_buf_end = dfu_get_buf(dfu) + dfu_buf_size;
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000348 dfu->i_buf = dfu->i_buf_start;
Stephen Warren0e285b52014-06-11 12:47:27 -0600349 dfu->b_left = 0;
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000350
Pantelis Antoniouc6631762013-03-14 05:32:52 +0000351 dfu->bad_skip = 0;
352
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000353 dfu->inited = 1;
354 }
355
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);
359 return -1;
360 }
361 /* handle rollover */
362 dfu->i_blk_seq_num = (dfu->i_blk_seq_num + 1) & 0xffff;
363
364 ret = dfu_read_buffer_fill(dfu, buf, size);
365 if (ret < 0) {
366 printf("%s: Failed to fill buffer\n", __func__);
367 return -1;
368 }
369
370 if (ret < size) {
Lukasz Majewskibd694242014-05-12 10:43:36 +0200371 if (dfu_hash_algo)
372 debug("%s: %s %s: 0x%x\n", __func__, dfu->name,
373 dfu_hash_algo->name, dfu->crc);
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000374 puts("\nUPLOAD ... done\nCtrl+C to exit ...\n");
375
Heiko Schochere7e75c72013-06-12 06:05:51 +0200376 dfu_free_buf();
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000377 dfu->i_blk_seq_num = 0;
378 dfu->crc = 0;
379 dfu->offset = 0;
380 dfu->i_buf_start = dfu_buf;
Heiko Schochere7e75c72013-06-12 06:05:51 +0200381 dfu->i_buf_end = dfu_buf;
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000382 dfu->i_buf = dfu->i_buf_start;
383 dfu->b_left = 0;
384
Pantelis Antoniouc6631762013-03-14 05:32:52 +0000385 dfu->bad_skip = 0;
386
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000387 dfu->inited = 0;
388 }
389
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200390 return ret;
391}
392
393static int dfu_fill_entity(struct dfu_entity *dfu, char *s, int alt,
Stephen Warrendd648272014-06-11 16:03:33 -0600394 char *interface, char *devstr)
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200395{
396 char *st;
397
Stephen Warrendd648272014-06-11 16:03:33 -0600398 debug("%s: %s interface: %s dev: %s\n", __func__, s, interface, devstr);
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200399 st = strsep(&s, " ");
400 strcpy(dfu->name, st);
401
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200402 dfu->alt = alt;
Stephen Warren7ac1b412014-06-11 16:03:34 -0600403 dfu->max_buf_size = 0;
Stephen Warrencb7bd2e2014-06-11 16:03:35 -0600404 dfu->free_entity = NULL;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200405
406 /* Specific for mmc device */
407 if (strcmp(interface, "mmc") == 0) {
Stephen Warrendd648272014-06-11 16:03:33 -0600408 if (dfu_fill_entity_mmc(dfu, devstr, s))
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200409 return -1;
Pantelis Antoniouc6631762013-03-14 05:32:52 +0000410 } else if (strcmp(interface, "nand") == 0) {
Stephen Warrendd648272014-06-11 16:03:33 -0600411 if (dfu_fill_entity_nand(dfu, devstr, s))
Pantelis Antoniouc6631762013-03-14 05:32:52 +0000412 return -1;
Afzal Mohammeda9479f02013-09-18 01:15:24 +0530413 } else if (strcmp(interface, "ram") == 0) {
Stephen Warrendd648272014-06-11 16:03:33 -0600414 if (dfu_fill_entity_ram(dfu, devstr, s))
Afzal Mohammeda9479f02013-09-18 01:15:24 +0530415 return -1;
Stephen Warren6f12ebf2014-06-11 16:03:36 -0600416 } else if (strcmp(interface, "sf") == 0) {
417 if (dfu_fill_entity_sf(dfu, devstr, s))
418 return -1;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200419 } else {
420 printf("%s: Device %s not (yet) supported!\n",
421 __func__, interface);
422 return -1;
423 }
424
425 return 0;
426}
427
428void dfu_free_entities(void)
429{
430 struct dfu_entity *dfu, *p, *t = NULL;
431
432 list_for_each_entry_safe_reverse(dfu, p, &dfu_list, list) {
433 list_del(&dfu->list);
Stephen Warrencb7bd2e2014-06-11 16:03:35 -0600434 if (dfu->free_entity)
435 dfu->free_entity(dfu);
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200436 t = dfu;
437 }
438 if (t)
439 free(t);
440 INIT_LIST_HEAD(&dfu_list);
Przemyslaw Marczakb627eb42014-02-28 18:53:37 +0100441
442 alt_num_cnt = 0;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200443}
444
Stephen Warrendd648272014-06-11 16:03:33 -0600445int dfu_config_entities(char *env, char *interface, char *devstr)
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200446{
447 struct dfu_entity *dfu;
448 int i, ret;
449 char *s;
450
451 dfu_alt_num = dfu_find_alt_num(env);
452 debug("%s: dfu_alt_num=%d\n", __func__, dfu_alt_num);
453
Lukasz Majewskibd694242014-05-12 10:43:36 +0200454 dfu_hash_algo = NULL;
455 s = dfu_get_hash_algo();
456 if (s) {
457 ret = hash_lookup_algo(s, &dfu_hash_algo);
458 if (ret)
459 error("Hash algorithm %s not supported\n", s);
460 }
461
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200462 dfu = calloc(sizeof(*dfu), dfu_alt_num);
463 if (!dfu)
464 return -1;
465 for (i = 0; i < dfu_alt_num; i++) {
466
467 s = strsep(&env, ";");
Stephen Warrendd648272014-06-11 16:03:33 -0600468 ret = dfu_fill_entity(&dfu[i], s, alt_num_cnt, interface,
469 devstr);
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200470 if (ret)
471 return -1;
472
473 list_add_tail(&dfu[i].list, &dfu_list);
Przemyslaw Marczakb627eb42014-02-28 18:53:37 +0100474 alt_num_cnt++;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200475 }
476
477 return 0;
478}
479
480const char *dfu_get_dev_type(enum dfu_device_type t)
481{
Afzal Mohammeda9479f02013-09-18 01:15:24 +0530482 const char *dev_t[] = {NULL, "eMMC", "OneNAND", "NAND", "RAM" };
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200483 return dev_t[t];
484}
485
486const char *dfu_get_layout(enum dfu_layout l)
487{
488 const char *dfu_layout[] = {NULL, "RAW_ADDR", "FAT", "EXT2",
Afzal Mohammeda9479f02013-09-18 01:15:24 +0530489 "EXT3", "EXT4", "RAM_ADDR" };
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200490 return dfu_layout[l];
491}
492
493void dfu_show_entities(void)
494{
495 struct dfu_entity *dfu;
496
497 puts("DFU alt settings list:\n");
498
499 list_for_each_entry(dfu, &dfu_list, list) {
500 printf("dev: %s alt: %d name: %s layout: %s\n",
501 dfu_get_dev_type(dfu->dev_type), dfu->alt,
502 dfu->name, dfu_get_layout(dfu->layout));
503 }
504}
505
506int dfu_get_alt_number(void)
507{
508 return dfu_alt_num;
509}
510
511struct dfu_entity *dfu_get_entity(int alt)
512{
513 struct dfu_entity *dfu;
514
515 list_for_each_entry(dfu, &dfu_list, list) {
516 if (dfu->alt == alt)
517 return dfu;
518 }
519
520 return NULL;
521}
Lukasz Majewskifed936e2013-10-08 14:30:38 +0200522
523int dfu_get_alt(char *name)
524{
525 struct dfu_entity *dfu;
526
527 list_for_each_entry(dfu, &dfu_list, list) {
528 if (!strncmp(dfu->name, name, strlen(dfu->name)))
529 return dfu->alt;
530 }
531
532 return -ENODEV;
533}