blob: ad0a7e7c25f9f4cd5f6c543cdb3deab56d97ceca [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
20static LIST_HEAD(dfu_list);
21static int dfu_alt_num;
Przemyslaw Marczak4af3e082014-02-28 18:53:37 +010022static int alt_num_cnt;
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +020023static struct hash_algo *dfu_hash_algo;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +020024
Lukasz Majewskid9eae702014-08-25 11:07:28 +020025/*
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
29 * bus.
30 *
31 * Described behaviour is the only way to distinct if
32 * user has typed -e (detach) or -R (reset) when invoking
33 * dfu-util command.
34 *
35 */
36__weak bool dfu_usb_get_reset(void)
Lukasz Majewski2968aa22013-07-18 13:19:14 +020037{
Lukasz Majewskid9eae702014-08-25 11:07:28 +020038 return true;
Lukasz Majewski2968aa22013-07-18 13:19:14 +020039}
40
Lukasz Majewskid311c6e2012-08-06 14:41:07 +020041static int dfu_find_alt_num(const char *s)
42{
43 int i = 0;
44
45 for (; *s; s++)
46 if (*s == ';')
47 i++;
48
49 return ++i;
50}
51
Stephen Warren4afe50f2014-06-11 16:03:33 -060052int dfu_init_env_entities(char *interface, char *devstr)
Lukasz Majewski35e591e2013-09-11 14:53:35 +020053{
54 const char *str_env;
55 char *env_bkp;
56 int ret;
57
58 str_env = getenv("dfu_alt_info");
59 if (!str_env) {
60 error("\"dfu_alt_info\" env variable not defined!\n");
61 return -EINVAL;
62 }
63
64 env_bkp = strdup(str_env);
Stephen Warren4afe50f2014-06-11 16:03:33 -060065 ret = dfu_config_entities(env_bkp, interface, devstr);
Lukasz Majewski35e591e2013-09-11 14:53:35 +020066 if (ret) {
67 error("DFU entities configuration failed!\n");
68 return ret;
69 }
70
71 free(env_bkp);
72 return 0;
73}
74
Heiko Schochera2f831e2013-06-12 06:05:51 +020075static unsigned char *dfu_buf;
76static unsigned long dfu_buf_size = CONFIG_SYS_DFU_DATA_BUF_SIZE;
77
Lukasz Majewski34264232013-10-08 14:30:39 +020078unsigned char *dfu_free_buf(void)
Heiko Schochera2f831e2013-06-12 06:05:51 +020079{
80 free(dfu_buf);
81 dfu_buf = NULL;
82 return dfu_buf;
83}
84
Lukasz Majewski031d4132013-12-09 16:20:13 +010085unsigned long dfu_get_buf_size(void)
86{
87 return dfu_buf_size;
88}
89
Stephen Warrenf9500982014-06-11 16:03:34 -060090unsigned char *dfu_get_buf(struct dfu_entity *dfu)
Heiko Schochera2f831e2013-06-12 06:05:51 +020091{
92 char *s;
93
94 if (dfu_buf != NULL)
95 return dfu_buf;
96
97 s = getenv("dfu_bufsiz");
Przemyslaw Marczak24883502014-12-15 10:34:11 +010098 if (s)
99 dfu_buf_size = (unsigned long)simple_strtol(s, NULL, 0);
100
101 if (!s || !dfu_buf_size)
102 dfu_buf_size = CONFIG_SYS_DFU_DATA_BUF_SIZE;
103
Stephen Warrenf9500982014-06-11 16:03:34 -0600104 if (dfu->max_buf_size && dfu_buf_size > dfu->max_buf_size)
105 dfu_buf_size = dfu->max_buf_size;
Heiko Schochera2f831e2013-06-12 06:05:51 +0200106
107 dfu_buf = memalign(CONFIG_SYS_CACHELINE_SIZE, dfu_buf_size);
108 if (dfu_buf == NULL)
109 printf("%s: Could not memalign 0x%lx bytes\n",
110 __func__, dfu_buf_size);
111
112 return dfu_buf;
113}
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200114
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200115static char *dfu_get_hash_algo(void)
116{
117 char *s;
118
119 s = getenv("dfu_hash_algo");
Lukasz Majewski3e0d0282014-06-10 12:25:59 +0200120 if (!s)
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200121 return NULL;
122
Lukasz Majewski3e0d0282014-06-10 12:25:59 +0200123 if (!strcmp(s, "crc32")) {
124 debug("%s: DFU hash method: %s\n", __func__, s);
125 return s;
126 }
127
128 error("DFU hash method: %s not supported!\n", s);
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200129 return NULL;
130}
131
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000132static int dfu_write_buffer_drain(struct dfu_entity *dfu)
133{
134 long w_size;
135 int ret;
136
137 /* flush size? */
138 w_size = dfu->i_buf - dfu->i_buf_start;
139 if (w_size == 0)
140 return 0;
141
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200142 if (dfu_hash_algo)
143 dfu_hash_algo->hash_update(dfu_hash_algo, &dfu->crc,
144 dfu->i_buf_start, w_size, 0);
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000145
146 ret = dfu->write_medium(dfu, dfu->offset, dfu->i_buf_start, &w_size);
147 if (ret)
148 debug("%s: Write error!\n", __func__);
149
150 /* point back */
151 dfu->i_buf = dfu->i_buf_start;
152
153 /* update offset */
154 dfu->offset += w_size;
155
156 puts("#");
157
158 return ret;
159}
160
Stephen Warren7f450232014-06-11 12:48:08 -0600161void dfu_write_transaction_cleanup(struct dfu_entity *dfu)
162{
163 /* clear everything */
164 dfu_free_buf();
165 dfu->crc = 0;
166 dfu->offset = 0;
167 dfu->i_blk_seq_num = 0;
168 dfu->i_buf_start = dfu_buf;
169 dfu->i_buf_end = dfu_buf;
170 dfu->i_buf = dfu->i_buf_start;
171 dfu->inited = 0;
172}
173
Heiko Schocher941b3842014-03-18 08:09:55 +0100174int dfu_flush(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
175{
176 int ret = 0;
177
Przemyslaw Marczaka0f6a382014-05-12 12:05:33 +0200178 ret = dfu_write_buffer_drain(dfu);
179 if (ret)
180 return ret;
181
Heiko Schocher941b3842014-03-18 08:09:55 +0100182 if (dfu->flush_medium)
183 ret = dfu->flush_medium(dfu);
184
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200185 if (dfu_hash_algo)
186 printf("\nDFU complete %s: 0x%08x\n", dfu_hash_algo->name,
187 dfu->crc);
Heiko Schocher941b3842014-03-18 08:09:55 +0100188
Stephen Warren7f450232014-06-11 12:48:08 -0600189 dfu_write_transaction_cleanup(dfu);
Heiko Schocher941b3842014-03-18 08:09:55 +0100190
191 return ret;
192}
193
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200194int dfu_write(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
195{
Stephen Warren7f450232014-06-11 12:48:08 -0600196 int ret;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200197
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000198 debug("%s: name: %s buf: 0x%p size: 0x%x p_num: 0x%x offset: 0x%llx bufoffset: 0x%x\n",
199 __func__, dfu->name, buf, size, blk_seq_num, dfu->offset,
200 dfu->i_buf - dfu->i_buf_start);
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200201
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000202 if (!dfu->inited) {
203 /* initial state */
204 dfu->crc = 0;
205 dfu->offset = 0;
Pantelis Antonioucf14d0d2013-03-14 05:32:52 +0000206 dfu->bad_skip = 0;
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000207 dfu->i_blk_seq_num = 0;
Stephen Warrenf9500982014-06-11 16:03:34 -0600208 dfu->i_buf_start = dfu_get_buf(dfu);
Heiko Schochera2f831e2013-06-12 06:05:51 +0200209 if (dfu->i_buf_start == NULL)
210 return -ENOMEM;
Stephen Warrenf9500982014-06-11 16:03:34 -0600211 dfu->i_buf_end = dfu_get_buf(dfu) + dfu_buf_size;
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000212 dfu->i_buf = dfu->i_buf_start;
213
214 dfu->inited = 1;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200215 }
216
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000217 if (dfu->i_blk_seq_num != blk_seq_num) {
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200218 printf("%s: Wrong sequence number! [%d] [%d]\n",
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000219 __func__, dfu->i_blk_seq_num, blk_seq_num);
Stephen Warren7f450232014-06-11 12:48:08 -0600220 dfu_write_transaction_cleanup(dfu);
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000221 return -1;
222 }
223
224 /* DFU 1.1 standard says:
225 * The wBlockNum field is a block sequence number. It increments each
226 * time a block is transferred, wrapping to zero from 65,535. It is used
227 * to provide useful context to the DFU loader in the device."
228 *
229 * This means that it's a 16 bit counter that roll-overs at
230 * 0xffff -> 0x0000. By having a typical 4K transfer block
231 * we roll-over at exactly 256MB. Not very fun to debug.
232 *
233 * Handling rollover, and having an inited variable,
234 * makes things work.
235 */
236
237 /* handle rollover */
238 dfu->i_blk_seq_num = (dfu->i_blk_seq_num + 1) & 0xffff;
239
240 /* flush buffer if overflow */
241 if ((dfu->i_buf + size) > dfu->i_buf_end) {
Stephen Warren7f450232014-06-11 12:48:08 -0600242 ret = dfu_write_buffer_drain(dfu);
243 if (ret) {
244 dfu_write_transaction_cleanup(dfu);
245 return ret;
246 }
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000247 }
248
249 /* we should be in buffer now (if not then size too large) */
250 if ((dfu->i_buf + size) > dfu->i_buf_end) {
Lukasz Majewski385c3212013-09-10 15:29:22 +0200251 error("Buffer overflow! (0x%p + 0x%x > 0x%p)\n", dfu->i_buf,
252 size, dfu->i_buf_end);
Stephen Warren7f450232014-06-11 12:48:08 -0600253 dfu_write_transaction_cleanup(dfu);
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200254 return -1;
255 }
256
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000257 memcpy(dfu->i_buf, buf, size);
258 dfu->i_buf += size;
259
260 /* if end or if buffer full flush */
261 if (size == 0 || (dfu->i_buf + size) > dfu->i_buf_end) {
Stephen Warren7f450232014-06-11 12:48:08 -0600262 ret = dfu_write_buffer_drain(dfu);
263 if (ret) {
264 dfu_write_transaction_cleanup(dfu);
265 return ret;
266 }
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000267 }
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200268
Stephen Warren7f450232014-06-11 12:48:08 -0600269 return 0;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200270}
271
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000272static int dfu_read_buffer_fill(struct dfu_entity *dfu, void *buf, int size)
273{
274 long chunk;
275 int ret, readn;
276
277 readn = 0;
278 while (size > 0) {
279 /* get chunk that can be read */
Masahiro Yamadadb204642014-11-07 03:03:31 +0900280 chunk = min((long)size, dfu->b_left);
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000281 /* consume */
282 if (chunk > 0) {
283 memcpy(buf, dfu->i_buf, chunk);
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200284 if (dfu_hash_algo)
285 dfu_hash_algo->hash_update(dfu_hash_algo,
286 &dfu->crc, buf,
287 chunk, 0);
288
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000289 dfu->i_buf += chunk;
290 dfu->b_left -= chunk;
291 size -= chunk;
292 buf += chunk;
293 readn += chunk;
294 }
295
296 /* all done */
297 if (size > 0) {
298 /* no more to read */
299 if (dfu->r_left == 0)
300 break;
301
302 dfu->i_buf = dfu->i_buf_start;
303 dfu->b_left = dfu->i_buf_end - dfu->i_buf_start;
304
305 /* got to read, but buffer is empty */
306 if (dfu->b_left > dfu->r_left)
307 dfu->b_left = dfu->r_left;
308 ret = dfu->read_medium(dfu, dfu->offset, dfu->i_buf,
309 &dfu->b_left);
310 if (ret != 0) {
311 debug("%s: Read error!\n", __func__);
312 return ret;
313 }
314 dfu->offset += dfu->b_left;
315 dfu->r_left -= dfu->b_left;
316
317 puts("#");
318 }
319 }
320
321 return readn;
322}
323
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200324int dfu_read(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
325{
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200326 int ret = 0;
327
328 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 +0000329 __func__, dfu->name, buf, size, blk_seq_num, dfu->i_buf);
330
331 if (!dfu->inited) {
Stephen Warrenf9500982014-06-11 16:03:34 -0600332 dfu->i_buf_start = dfu_get_buf(dfu);
Łukasz Majewski063ae5a2013-06-28 18:41:50 +0200333 if (dfu->i_buf_start == NULL)
334 return -ENOMEM;
335
Stephen Warren020e6f32014-06-11 12:47:27 -0600336 dfu->r_left = dfu->get_medium_size(dfu);
337 if (dfu->r_left < 0)
338 return dfu->r_left;
339 switch (dfu->layout) {
340 case DFU_RAW_ADDR:
341 case DFU_RAM_ADDR:
342 break;
343 default:
Stephen Warrenafb2cf02014-07-01 12:16:57 -0600344 if (dfu->r_left > dfu_buf_size) {
Stephen Warren020e6f32014-06-11 12:47:27 -0600345 printf("%s: File too big for buffer\n",
346 __func__);
347 return -EOVERFLOW;
348 }
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000349 }
350
351 debug("%s: %s %ld [B]\n", __func__, dfu->name, dfu->r_left);
352
353 dfu->i_blk_seq_num = 0;
354 dfu->crc = 0;
355 dfu->offset = 0;
Stephen Warrenf9500982014-06-11 16:03:34 -0600356 dfu->i_buf_end = dfu_get_buf(dfu) + dfu_buf_size;
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000357 dfu->i_buf = dfu->i_buf_start;
Stephen Warren020e6f32014-06-11 12:47:27 -0600358 dfu->b_left = 0;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200359
Pantelis Antonioucf14d0d2013-03-14 05:32:52 +0000360 dfu->bad_skip = 0;
361
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000362 dfu->inited = 1;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200363 }
364
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000365 if (dfu->i_blk_seq_num != blk_seq_num) {
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200366 printf("%s: Wrong sequence number! [%d] [%d]\n",
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000367 __func__, dfu->i_blk_seq_num, blk_seq_num);
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200368 return -1;
369 }
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000370 /* handle rollover */
371 dfu->i_blk_seq_num = (dfu->i_blk_seq_num + 1) & 0xffff;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200372
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000373 ret = dfu_read_buffer_fill(dfu, buf, size);
374 if (ret < 0) {
375 printf("%s: Failed to fill buffer\n", __func__);
376 return -1;
377 }
378
379 if (ret < size) {
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200380 if (dfu_hash_algo)
381 debug("%s: %s %s: 0x%x\n", __func__, dfu->name,
382 dfu_hash_algo->name, dfu->crc);
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000383 puts("\nUPLOAD ... done\nCtrl+C to exit ...\n");
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200384
Heiko Schochera2f831e2013-06-12 06:05:51 +0200385 dfu_free_buf();
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000386 dfu->i_blk_seq_num = 0;
387 dfu->crc = 0;
388 dfu->offset = 0;
389 dfu->i_buf_start = dfu_buf;
Heiko Schochera2f831e2013-06-12 06:05:51 +0200390 dfu->i_buf_end = dfu_buf;
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000391 dfu->i_buf = dfu->i_buf_start;
392 dfu->b_left = 0;
393
Pantelis Antonioucf14d0d2013-03-14 05:32:52 +0000394 dfu->bad_skip = 0;
395
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000396 dfu->inited = 0;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200397 }
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000398
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200399 return ret;
400}
401
402static int dfu_fill_entity(struct dfu_entity *dfu, char *s, int alt,
Stephen Warren4afe50f2014-06-11 16:03:33 -0600403 char *interface, char *devstr)
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200404{
405 char *st;
406
Stephen Warren4afe50f2014-06-11 16:03:33 -0600407 debug("%s: %s interface: %s dev: %s\n", __func__, s, interface, devstr);
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200408 st = strsep(&s, " ");
409 strcpy(dfu->name, st);
410
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200411 dfu->alt = alt;
Stephen Warrenf9500982014-06-11 16:03:34 -0600412 dfu->max_buf_size = 0;
Stephen Warrena5f77f62014-06-11 16:03:35 -0600413 dfu->free_entity = NULL;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200414
415 /* Specific for mmc device */
416 if (strcmp(interface, "mmc") == 0) {
Stephen Warren4afe50f2014-06-11 16:03:33 -0600417 if (dfu_fill_entity_mmc(dfu, devstr, s))
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200418 return -1;
Pantelis Antonioucf14d0d2013-03-14 05:32:52 +0000419 } else if (strcmp(interface, "nand") == 0) {
Stephen Warren4afe50f2014-06-11 16:03:33 -0600420 if (dfu_fill_entity_nand(dfu, devstr, s))
Pantelis Antonioucf14d0d2013-03-14 05:32:52 +0000421 return -1;
Afzal Mohammede3c687a2013-09-18 01:15:24 +0530422 } else if (strcmp(interface, "ram") == 0) {
Stephen Warren4afe50f2014-06-11 16:03:33 -0600423 if (dfu_fill_entity_ram(dfu, devstr, s))
Afzal Mohammede3c687a2013-09-18 01:15:24 +0530424 return -1;
Stephen Warren0280af72014-06-11 16:03:36 -0600425 } else if (strcmp(interface, "sf") == 0) {
426 if (dfu_fill_entity_sf(dfu, devstr, s))
427 return -1;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200428 } else {
429 printf("%s: Device %s not (yet) supported!\n",
430 __func__, interface);
431 return -1;
432 }
433
434 return 0;
435}
436
437void dfu_free_entities(void)
438{
439 struct dfu_entity *dfu, *p, *t = NULL;
440
441 list_for_each_entry_safe_reverse(dfu, p, &dfu_list, list) {
442 list_del(&dfu->list);
Stephen Warrena5f77f62014-06-11 16:03:35 -0600443 if (dfu->free_entity)
444 dfu->free_entity(dfu);
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200445 t = dfu;
446 }
447 if (t)
448 free(t);
449 INIT_LIST_HEAD(&dfu_list);
Przemyslaw Marczak4af3e082014-02-28 18:53:37 +0100450
451 alt_num_cnt = 0;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200452}
453
Stephen Warren4afe50f2014-06-11 16:03:33 -0600454int dfu_config_entities(char *env, char *interface, char *devstr)
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200455{
456 struct dfu_entity *dfu;
457 int i, ret;
458 char *s;
459
460 dfu_alt_num = dfu_find_alt_num(env);
461 debug("%s: dfu_alt_num=%d\n", __func__, dfu_alt_num);
462
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200463 dfu_hash_algo = NULL;
464 s = dfu_get_hash_algo();
465 if (s) {
466 ret = hash_lookup_algo(s, &dfu_hash_algo);
467 if (ret)
468 error("Hash algorithm %s not supported\n", s);
469 }
470
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200471 dfu = calloc(sizeof(*dfu), dfu_alt_num);
472 if (!dfu)
473 return -1;
474 for (i = 0; i < dfu_alt_num; i++) {
475
476 s = strsep(&env, ";");
Stephen Warren4afe50f2014-06-11 16:03:33 -0600477 ret = dfu_fill_entity(&dfu[i], s, alt_num_cnt, interface,
478 devstr);
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200479 if (ret)
480 return -1;
481
482 list_add_tail(&dfu[i].list, &dfu_list);
Przemyslaw Marczak4af3e082014-02-28 18:53:37 +0100483 alt_num_cnt++;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200484 }
485
486 return 0;
487}
488
489const char *dfu_get_dev_type(enum dfu_device_type t)
490{
Afzal Mohammede3c687a2013-09-18 01:15:24 +0530491 const char *dev_t[] = {NULL, "eMMC", "OneNAND", "NAND", "RAM" };
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200492 return dev_t[t];
493}
494
495const char *dfu_get_layout(enum dfu_layout l)
496{
497 const char *dfu_layout[] = {NULL, "RAW_ADDR", "FAT", "EXT2",
Afzal Mohammede3c687a2013-09-18 01:15:24 +0530498 "EXT3", "EXT4", "RAM_ADDR" };
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200499 return dfu_layout[l];
500}
501
502void dfu_show_entities(void)
503{
504 struct dfu_entity *dfu;
505
506 puts("DFU alt settings list:\n");
507
508 list_for_each_entry(dfu, &dfu_list, list) {
509 printf("dev: %s alt: %d name: %s layout: %s\n",
510 dfu_get_dev_type(dfu->dev_type), dfu->alt,
511 dfu->name, dfu_get_layout(dfu->layout));
512 }
513}
514
515int dfu_get_alt_number(void)
516{
517 return dfu_alt_num;
518}
519
520struct dfu_entity *dfu_get_entity(int alt)
521{
522 struct dfu_entity *dfu;
523
524 list_for_each_entry(dfu, &dfu_list, list) {
525 if (dfu->alt == alt)
526 return dfu;
527 }
528
529 return NULL;
530}
Lukasz Majewski1737f0f2013-10-08 14:30:38 +0200531
532int dfu_get_alt(char *name)
533{
534 struct dfu_entity *dfu;
Lukasz Majewski3a39d012014-11-05 10:54:16 +0100535 char *str;
Lukasz Majewski1737f0f2013-10-08 14:30:38 +0200536
537 list_for_each_entry(dfu, &dfu_list, list) {
Lukasz Majewski3a39d012014-11-05 10:54:16 +0100538 if (dfu->name[0] != '/') {
539 if (!strncmp(dfu->name, name, strlen(dfu->name)))
540 return dfu->alt;
541 } else {
542 /*
543 * One must also consider absolute path
544 * (/boot/bin/uImage) available at dfu->name when
545 * compared "plain" file name (uImage)
546 *
547 * It is the case for e.g. thor gadget where lthor SW
548 * sends only the file name, so only the very last part
549 * of path must be checked for equality
550 */
551
552 str = strstr(dfu->name, name);
553 if (!str)
554 continue;
555
556 /*
557 * Check if matching substring is the last element of
558 * dfu->name (uImage)
559 */
560 if (strlen(dfu->name) ==
561 ((str - dfu->name) + strlen(name)))
562 return dfu->alt;
563 }
Lukasz Majewski1737f0f2013-10-08 14:30:38 +0200564 }
565
566 return -ENODEV;
567}