blob: 3512b149c560fc7fa32fa169bbfbd9ac70875bda [file] [log] [blame]
Lukasz Majewskid311c6e2012-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 Denkd79de1d2013-07-08 09:37:19 +02007 * SPDX-License-Identifier: GPL-2.0+
Lukasz Majewskid311c6e2012-08-06 14:41:07 +02008 */
9
10#include <common.h>
Heiko Schochera2f831e2013-06-12 06:05:51 +020011#include <errno.h>
Lukasz Majewskid311c6e2012-08-06 14:41:07 +020012#include <malloc.h>
13#include <mmc.h>
14#include <fat.h>
15#include <dfu.h>
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +020016#include <hash.h>
Lukasz Majewskid311c6e2012-08-06 14:41:07 +020017#include <linux/list.h>
18#include <linux/compiler.h>
19
Lukasz Majewski2968aa22013-07-18 13:19:14 +020020static bool dfu_reset_request;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +020021static LIST_HEAD(dfu_list);
22static int dfu_alt_num;
Przemyslaw Marczak4af3e082014-02-28 18:53:37 +010023static int alt_num_cnt;
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +020024static struct hash_algo *dfu_hash_algo;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +020025
Lukasz Majewski2968aa22013-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 Majewskid311c6e2012-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 Warren4afe50f2014-06-11 16:03:33 -060047int dfu_init_env_entities(char *interface, char *devstr)
Lukasz Majewski35e591e2013-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 Warren4afe50f2014-06-11 16:03:33 -060060 ret = dfu_config_entities(env_bkp, interface, devstr);
Lukasz Majewski35e591e2013-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 Schochera2f831e2013-06-12 06:05:51 +020070static unsigned char *dfu_buf;
71static unsigned long dfu_buf_size = CONFIG_SYS_DFU_DATA_BUF_SIZE;
72
Lukasz Majewski34264232013-10-08 14:30:39 +020073unsigned char *dfu_free_buf(void)
Heiko Schochera2f831e2013-06-12 06:05:51 +020074{
75 free(dfu_buf);
76 dfu_buf = NULL;
77 return dfu_buf;
78}
79
Lukasz Majewski031d4132013-12-09 16:20:13 +010080unsigned long dfu_get_buf_size(void)
81{
82 return dfu_buf_size;
83}
84
Stephen Warrenf9500982014-06-11 16:03:34 -060085unsigned char *dfu_get_buf(struct dfu_entity *dfu)
Heiko Schochera2f831e2013-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 Warrenf9500982014-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 Schochera2f831e2013-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 Majewskid311c6e2012-08-06 14:41:07 +0200105
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200106static char *dfu_get_hash_algo(void)
107{
108 char *s;
109
110 s = getenv("dfu_hash_algo");
Lukasz Majewski3e0d0282014-06-10 12:25:59 +0200111 if (!s)
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200112 return NULL;
113
Lukasz Majewski3e0d0282014-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 Majewskifa8dc1d2014-05-12 10:43:36 +0200120 return NULL;
121}
122
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000123static int dfu_write_buffer_drain(struct dfu_entity *dfu)
124{
125 long w_size;
126 int ret;
127
128 /* flush size? */
129 w_size = dfu->i_buf - dfu->i_buf_start;
130 if (w_size == 0)
131 return 0;
132
Lukasz Majewskifa8dc1d2014-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);
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000136
137 ret = dfu->write_medium(dfu, dfu->offset, dfu->i_buf_start, &w_size);
138 if (ret)
139 debug("%s: Write error!\n", __func__);
140
141 /* point back */
142 dfu->i_buf = dfu->i_buf_start;
143
144 /* update offset */
145 dfu->offset += w_size;
146
147 puts("#");
148
149 return ret;
150}
151
Stephen Warren7f450232014-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 Schocher941b3842014-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 Marczaka0f6a382014-05-12 12:05:33 +0200169 ret = dfu_write_buffer_drain(dfu);
170 if (ret)
171 return ret;
172
Heiko Schocher941b3842014-03-18 08:09:55 +0100173 if (dfu->flush_medium)
174 ret = dfu->flush_medium(dfu);
175
Lukasz Majewskifa8dc1d2014-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 Schocher941b3842014-03-18 08:09:55 +0100179
Stephen Warren7f450232014-06-11 12:48:08 -0600180 dfu_write_transaction_cleanup(dfu);
Heiko Schocher941b3842014-03-18 08:09:55 +0100181
182 return ret;
183}
184
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200185int dfu_write(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
186{
Stephen Warren7f450232014-06-11 12:48:08 -0600187 int ret;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200188
Pantelis Antonioua6e788d2013-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 Majewskid311c6e2012-08-06 14:41:07 +0200192
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000193 if (!dfu->inited) {
194 /* initial state */
195 dfu->crc = 0;
196 dfu->offset = 0;
Pantelis Antonioucf14d0d2013-03-14 05:32:52 +0000197 dfu->bad_skip = 0;
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000198 dfu->i_blk_seq_num = 0;
Stephen Warrenf9500982014-06-11 16:03:34 -0600199 dfu->i_buf_start = dfu_get_buf(dfu);
Heiko Schochera2f831e2013-06-12 06:05:51 +0200200 if (dfu->i_buf_start == NULL)
201 return -ENOMEM;
Stephen Warrenf9500982014-06-11 16:03:34 -0600202 dfu->i_buf_end = dfu_get_buf(dfu) + dfu_buf_size;
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000203 dfu->i_buf = dfu->i_buf_start;
204
205 dfu->inited = 1;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200206 }
207
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000208 if (dfu->i_blk_seq_num != blk_seq_num) {
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200209 printf("%s: Wrong sequence number! [%d] [%d]\n",
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000210 __func__, dfu->i_blk_seq_num, blk_seq_num);
Stephen Warren7f450232014-06-11 12:48:08 -0600211 dfu_write_transaction_cleanup(dfu);
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000212 return -1;
213 }
214
215 /* 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 */
227
228 /* 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 Warren7f450232014-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 }
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000238 }
239
240 /* we should be in buffer now (if not then size too large) */
241 if ((dfu->i_buf + size) > dfu->i_buf_end) {
Lukasz Majewski385c3212013-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 Warren7f450232014-06-11 12:48:08 -0600244 dfu_write_transaction_cleanup(dfu);
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200245 return -1;
246 }
247
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000248 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 Warren7f450232014-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 Antonioua6e788d2013-03-14 05:32:48 +0000258 }
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200259
Stephen Warren7f450232014-06-11 12:48:08 -0600260 return 0;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200261}
262
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000263static 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 Majewskifa8dc1d2014-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 Antonioua6e788d2013-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
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200315int dfu_read(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
316{
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200317 int ret = 0;
318
319 debug("%s: name: %s buf: 0x%p size: 0x%x p_num: 0x%x i_buf: 0x%p\n",
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000320 __func__, dfu->name, buf, size, blk_seq_num, dfu->i_buf);
321
322 if (!dfu->inited) {
Stephen Warrenf9500982014-06-11 16:03:34 -0600323 dfu->i_buf_start = dfu_get_buf(dfu);
Łukasz Majewski063ae5a2013-06-28 18:41:50 +0200324 if (dfu->i_buf_start == NULL)
325 return -ENOMEM;
326
Stephen Warren020e6f32014-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:
Stephen Warrenafb2cf02014-07-01 12:16:57 -0600335 if (dfu->r_left > dfu_buf_size) {
Stephen Warren020e6f32014-06-11 12:47:27 -0600336 printf("%s: File too big for buffer\n",
337 __func__);
338 return -EOVERFLOW;
339 }
Pantelis Antonioua6e788d2013-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 Warrenf9500982014-06-11 16:03:34 -0600347 dfu->i_buf_end = dfu_get_buf(dfu) + dfu_buf_size;
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000348 dfu->i_buf = dfu->i_buf_start;
Stephen Warren020e6f32014-06-11 12:47:27 -0600349 dfu->b_left = 0;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200350
Pantelis Antonioucf14d0d2013-03-14 05:32:52 +0000351 dfu->bad_skip = 0;
352
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000353 dfu->inited = 1;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200354 }
355
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000356 if (dfu->i_blk_seq_num != blk_seq_num) {
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200357 printf("%s: Wrong sequence number! [%d] [%d]\n",
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000358 __func__, dfu->i_blk_seq_num, blk_seq_num);
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200359 return -1;
360 }
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000361 /* handle rollover */
362 dfu->i_blk_seq_num = (dfu->i_blk_seq_num + 1) & 0xffff;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200363
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000364 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 Majewskifa8dc1d2014-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 Antonioua6e788d2013-03-14 05:32:48 +0000374 puts("\nUPLOAD ... done\nCtrl+C to exit ...\n");
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200375
Heiko Schochera2f831e2013-06-12 06:05:51 +0200376 dfu_free_buf();
Pantelis Antonioua6e788d2013-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 Schochera2f831e2013-06-12 06:05:51 +0200381 dfu->i_buf_end = dfu_buf;
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000382 dfu->i_buf = dfu->i_buf_start;
383 dfu->b_left = 0;
384
Pantelis Antonioucf14d0d2013-03-14 05:32:52 +0000385 dfu->bad_skip = 0;
386
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000387 dfu->inited = 0;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200388 }
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000389
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200390 return ret;
391}
392
393static int dfu_fill_entity(struct dfu_entity *dfu, char *s, int alt,
Stephen Warren4afe50f2014-06-11 16:03:33 -0600394 char *interface, char *devstr)
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200395{
396 char *st;
397
Stephen Warren4afe50f2014-06-11 16:03:33 -0600398 debug("%s: %s interface: %s dev: %s\n", __func__, s, interface, devstr);
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200399 st = strsep(&s, " ");
400 strcpy(dfu->name, st);
401
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200402 dfu->alt = alt;
Stephen Warrenf9500982014-06-11 16:03:34 -0600403 dfu->max_buf_size = 0;
Stephen Warrena5f77f62014-06-11 16:03:35 -0600404 dfu->free_entity = NULL;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200405
406 /* Specific for mmc device */
407 if (strcmp(interface, "mmc") == 0) {
Stephen Warren4afe50f2014-06-11 16:03:33 -0600408 if (dfu_fill_entity_mmc(dfu, devstr, s))
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200409 return -1;
Pantelis Antonioucf14d0d2013-03-14 05:32:52 +0000410 } else if (strcmp(interface, "nand") == 0) {
Stephen Warren4afe50f2014-06-11 16:03:33 -0600411 if (dfu_fill_entity_nand(dfu, devstr, s))
Pantelis Antonioucf14d0d2013-03-14 05:32:52 +0000412 return -1;
Afzal Mohammede3c687a2013-09-18 01:15:24 +0530413 } else if (strcmp(interface, "ram") == 0) {
Stephen Warren4afe50f2014-06-11 16:03:33 -0600414 if (dfu_fill_entity_ram(dfu, devstr, s))
Afzal Mohammede3c687a2013-09-18 01:15:24 +0530415 return -1;
Stephen Warren0280af72014-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 Majewskid311c6e2012-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 Warrena5f77f62014-06-11 16:03:35 -0600434 if (dfu->free_entity)
435 dfu->free_entity(dfu);
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200436 t = dfu;
437 }
438 if (t)
439 free(t);
440 INIT_LIST_HEAD(&dfu_list);
Przemyslaw Marczak4af3e082014-02-28 18:53:37 +0100441
442 alt_num_cnt = 0;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200443}
444
Stephen Warren4afe50f2014-06-11 16:03:33 -0600445int dfu_config_entities(char *env, char *interface, char *devstr)
Lukasz Majewskid311c6e2012-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 Majewskifa8dc1d2014-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 Majewskid311c6e2012-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 Warren4afe50f2014-06-11 16:03:33 -0600468 ret = dfu_fill_entity(&dfu[i], s, alt_num_cnt, interface,
469 devstr);
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200470 if (ret)
471 return -1;
472
473 list_add_tail(&dfu[i].list, &dfu_list);
Przemyslaw Marczak4af3e082014-02-28 18:53:37 +0100474 alt_num_cnt++;
Lukasz Majewskid311c6e2012-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 Mohammede3c687a2013-09-18 01:15:24 +0530482 const char *dev_t[] = {NULL, "eMMC", "OneNAND", "NAND", "RAM" };
Lukasz Majewskid311c6e2012-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 Mohammede3c687a2013-09-18 01:15:24 +0530489 "EXT3", "EXT4", "RAM_ADDR" };
Lukasz Majewskid311c6e2012-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 Majewski1737f0f2013-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}