blob: 55e6a83b9ad6956ccc1013bdb227d99d5f9ddfbc [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 Majewskid9eae702014-08-25 11:07:28 +020020static bool dfu_detach_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 Majewskid9eae702014-08-25 11:07:28 +020026/*
27 * The purpose of the dfu_usb_get_reset() function is to
28 * provide information if after USB_DETACH request
29 * being sent the dfu-util performed reset of USB
30 * bus.
31 *
32 * Described behaviour is the only way to distinct if
33 * user has typed -e (detach) or -R (reset) when invoking
34 * dfu-util command.
35 *
36 */
37__weak bool dfu_usb_get_reset(void)
Lukasz Majewski2968aa22013-07-18 13:19:14 +020038{
Lukasz Majewskid9eae702014-08-25 11:07:28 +020039 return true;
Lukasz Majewski2968aa22013-07-18 13:19:14 +020040}
41
Lukasz Majewskid9eae702014-08-25 11:07:28 +020042bool dfu_detach(void)
43{
44 return dfu_detach_request;
45}
46
47void dfu_trigger_detach(void)
48{
49 dfu_detach_request = true;
50}
51
52void dfu_clear_detach(void)
Lukasz Majewski2968aa22013-07-18 13:19:14 +020053{
Lukasz Majewskid9eae702014-08-25 11:07:28 +020054 dfu_detach_request = false;
Lukasz Majewski2968aa22013-07-18 13:19:14 +020055}
56
Lukasz Majewskid311c6e2012-08-06 14:41:07 +020057static int dfu_find_alt_num(const char *s)
58{
59 int i = 0;
60
61 for (; *s; s++)
62 if (*s == ';')
63 i++;
64
65 return ++i;
66}
67
Stephen Warren4afe50f2014-06-11 16:03:33 -060068int dfu_init_env_entities(char *interface, char *devstr)
Lukasz Majewski35e591e2013-09-11 14:53:35 +020069{
70 const char *str_env;
71 char *env_bkp;
72 int ret;
73
74 str_env = getenv("dfu_alt_info");
75 if (!str_env) {
76 error("\"dfu_alt_info\" env variable not defined!\n");
77 return -EINVAL;
78 }
79
80 env_bkp = strdup(str_env);
Stephen Warren4afe50f2014-06-11 16:03:33 -060081 ret = dfu_config_entities(env_bkp, interface, devstr);
Lukasz Majewski35e591e2013-09-11 14:53:35 +020082 if (ret) {
83 error("DFU entities configuration failed!\n");
84 return ret;
85 }
86
87 free(env_bkp);
88 return 0;
89}
90
Heiko Schochera2f831e2013-06-12 06:05:51 +020091static unsigned char *dfu_buf;
92static unsigned long dfu_buf_size = CONFIG_SYS_DFU_DATA_BUF_SIZE;
93
Lukasz Majewski34264232013-10-08 14:30:39 +020094unsigned char *dfu_free_buf(void)
Heiko Schochera2f831e2013-06-12 06:05:51 +020095{
96 free(dfu_buf);
97 dfu_buf = NULL;
98 return dfu_buf;
99}
100
Lukasz Majewski031d4132013-12-09 16:20:13 +0100101unsigned long dfu_get_buf_size(void)
102{
103 return dfu_buf_size;
104}
105
Stephen Warrenf9500982014-06-11 16:03:34 -0600106unsigned char *dfu_get_buf(struct dfu_entity *dfu)
Heiko Schochera2f831e2013-06-12 06:05:51 +0200107{
108 char *s;
109
110 if (dfu_buf != NULL)
111 return dfu_buf;
112
113 s = getenv("dfu_bufsiz");
114 dfu_buf_size = s ? (unsigned long)simple_strtol(s, NULL, 16) :
115 CONFIG_SYS_DFU_DATA_BUF_SIZE;
Stephen Warrenf9500982014-06-11 16:03:34 -0600116 if (dfu->max_buf_size && dfu_buf_size > dfu->max_buf_size)
117 dfu_buf_size = dfu->max_buf_size;
Heiko Schochera2f831e2013-06-12 06:05:51 +0200118
119 dfu_buf = memalign(CONFIG_SYS_CACHELINE_SIZE, dfu_buf_size);
120 if (dfu_buf == NULL)
121 printf("%s: Could not memalign 0x%lx bytes\n",
122 __func__, dfu_buf_size);
123
124 return dfu_buf;
125}
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200126
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200127static char *dfu_get_hash_algo(void)
128{
129 char *s;
130
131 s = getenv("dfu_hash_algo");
Lukasz Majewski3e0d0282014-06-10 12:25:59 +0200132 if (!s)
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200133 return NULL;
134
Lukasz Majewski3e0d0282014-06-10 12:25:59 +0200135 if (!strcmp(s, "crc32")) {
136 debug("%s: DFU hash method: %s\n", __func__, s);
137 return s;
138 }
139
140 error("DFU hash method: %s not supported!\n", s);
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200141 return NULL;
142}
143
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000144static int dfu_write_buffer_drain(struct dfu_entity *dfu)
145{
146 long w_size;
147 int ret;
148
149 /* flush size? */
150 w_size = dfu->i_buf - dfu->i_buf_start;
151 if (w_size == 0)
152 return 0;
153
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200154 if (dfu_hash_algo)
155 dfu_hash_algo->hash_update(dfu_hash_algo, &dfu->crc,
156 dfu->i_buf_start, w_size, 0);
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000157
158 ret = dfu->write_medium(dfu, dfu->offset, dfu->i_buf_start, &w_size);
159 if (ret)
160 debug("%s: Write error!\n", __func__);
161
162 /* point back */
163 dfu->i_buf = dfu->i_buf_start;
164
165 /* update offset */
166 dfu->offset += w_size;
167
168 puts("#");
169
170 return ret;
171}
172
Stephen Warren7f450232014-06-11 12:48:08 -0600173void dfu_write_transaction_cleanup(struct dfu_entity *dfu)
174{
175 /* clear everything */
176 dfu_free_buf();
177 dfu->crc = 0;
178 dfu->offset = 0;
179 dfu->i_blk_seq_num = 0;
180 dfu->i_buf_start = dfu_buf;
181 dfu->i_buf_end = dfu_buf;
182 dfu->i_buf = dfu->i_buf_start;
183 dfu->inited = 0;
184}
185
Heiko Schocher941b3842014-03-18 08:09:55 +0100186int dfu_flush(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
187{
188 int ret = 0;
189
Przemyslaw Marczaka0f6a382014-05-12 12:05:33 +0200190 ret = dfu_write_buffer_drain(dfu);
191 if (ret)
192 return ret;
193
Heiko Schocher941b3842014-03-18 08:09:55 +0100194 if (dfu->flush_medium)
195 ret = dfu->flush_medium(dfu);
196
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200197 if (dfu_hash_algo)
198 printf("\nDFU complete %s: 0x%08x\n", dfu_hash_algo->name,
199 dfu->crc);
Heiko Schocher941b3842014-03-18 08:09:55 +0100200
Stephen Warren7f450232014-06-11 12:48:08 -0600201 dfu_write_transaction_cleanup(dfu);
Heiko Schocher941b3842014-03-18 08:09:55 +0100202
203 return ret;
204}
205
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200206int dfu_write(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
207{
Stephen Warren7f450232014-06-11 12:48:08 -0600208 int ret;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200209
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000210 debug("%s: name: %s buf: 0x%p size: 0x%x p_num: 0x%x offset: 0x%llx bufoffset: 0x%x\n",
211 __func__, dfu->name, buf, size, blk_seq_num, dfu->offset,
212 dfu->i_buf - dfu->i_buf_start);
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200213
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000214 if (!dfu->inited) {
215 /* initial state */
216 dfu->crc = 0;
217 dfu->offset = 0;
Pantelis Antonioucf14d0d2013-03-14 05:32:52 +0000218 dfu->bad_skip = 0;
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000219 dfu->i_blk_seq_num = 0;
Stephen Warrenf9500982014-06-11 16:03:34 -0600220 dfu->i_buf_start = dfu_get_buf(dfu);
Heiko Schochera2f831e2013-06-12 06:05:51 +0200221 if (dfu->i_buf_start == NULL)
222 return -ENOMEM;
Stephen Warrenf9500982014-06-11 16:03:34 -0600223 dfu->i_buf_end = dfu_get_buf(dfu) + dfu_buf_size;
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000224 dfu->i_buf = dfu->i_buf_start;
225
226 dfu->inited = 1;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200227 }
228
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000229 if (dfu->i_blk_seq_num != blk_seq_num) {
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200230 printf("%s: Wrong sequence number! [%d] [%d]\n",
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000231 __func__, dfu->i_blk_seq_num, blk_seq_num);
Stephen Warren7f450232014-06-11 12:48:08 -0600232 dfu_write_transaction_cleanup(dfu);
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000233 return -1;
234 }
235
236 /* DFU 1.1 standard says:
237 * The wBlockNum field is a block sequence number. It increments each
238 * time a block is transferred, wrapping to zero from 65,535. It is used
239 * to provide useful context to the DFU loader in the device."
240 *
241 * This means that it's a 16 bit counter that roll-overs at
242 * 0xffff -> 0x0000. By having a typical 4K transfer block
243 * we roll-over at exactly 256MB. Not very fun to debug.
244 *
245 * Handling rollover, and having an inited variable,
246 * makes things work.
247 */
248
249 /* handle rollover */
250 dfu->i_blk_seq_num = (dfu->i_blk_seq_num + 1) & 0xffff;
251
252 /* flush buffer if overflow */
253 if ((dfu->i_buf + size) > dfu->i_buf_end) {
Stephen Warren7f450232014-06-11 12:48:08 -0600254 ret = dfu_write_buffer_drain(dfu);
255 if (ret) {
256 dfu_write_transaction_cleanup(dfu);
257 return ret;
258 }
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000259 }
260
261 /* we should be in buffer now (if not then size too large) */
262 if ((dfu->i_buf + size) > dfu->i_buf_end) {
Lukasz Majewski385c3212013-09-10 15:29:22 +0200263 error("Buffer overflow! (0x%p + 0x%x > 0x%p)\n", dfu->i_buf,
264 size, dfu->i_buf_end);
Stephen Warren7f450232014-06-11 12:48:08 -0600265 dfu_write_transaction_cleanup(dfu);
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200266 return -1;
267 }
268
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000269 memcpy(dfu->i_buf, buf, size);
270 dfu->i_buf += size;
271
272 /* if end or if buffer full flush */
273 if (size == 0 || (dfu->i_buf + size) > dfu->i_buf_end) {
Stephen Warren7f450232014-06-11 12:48:08 -0600274 ret = dfu_write_buffer_drain(dfu);
275 if (ret) {
276 dfu_write_transaction_cleanup(dfu);
277 return ret;
278 }
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000279 }
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200280
Stephen Warren7f450232014-06-11 12:48:08 -0600281 return 0;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200282}
283
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000284static int dfu_read_buffer_fill(struct dfu_entity *dfu, void *buf, int size)
285{
286 long chunk;
287 int ret, readn;
288
289 readn = 0;
290 while (size > 0) {
291 /* get chunk that can be read */
292 chunk = min(size, dfu->b_left);
293 /* consume */
294 if (chunk > 0) {
295 memcpy(buf, dfu->i_buf, chunk);
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200296 if (dfu_hash_algo)
297 dfu_hash_algo->hash_update(dfu_hash_algo,
298 &dfu->crc, buf,
299 chunk, 0);
300
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000301 dfu->i_buf += chunk;
302 dfu->b_left -= chunk;
303 size -= chunk;
304 buf += chunk;
305 readn += chunk;
306 }
307
308 /* all done */
309 if (size > 0) {
310 /* no more to read */
311 if (dfu->r_left == 0)
312 break;
313
314 dfu->i_buf = dfu->i_buf_start;
315 dfu->b_left = dfu->i_buf_end - dfu->i_buf_start;
316
317 /* got to read, but buffer is empty */
318 if (dfu->b_left > dfu->r_left)
319 dfu->b_left = dfu->r_left;
320 ret = dfu->read_medium(dfu, dfu->offset, dfu->i_buf,
321 &dfu->b_left);
322 if (ret != 0) {
323 debug("%s: Read error!\n", __func__);
324 return ret;
325 }
326 dfu->offset += dfu->b_left;
327 dfu->r_left -= dfu->b_left;
328
329 puts("#");
330 }
331 }
332
333 return readn;
334}
335
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200336int dfu_read(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
337{
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200338 int ret = 0;
339
340 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 +0000341 __func__, dfu->name, buf, size, blk_seq_num, dfu->i_buf);
342
343 if (!dfu->inited) {
Stephen Warrenf9500982014-06-11 16:03:34 -0600344 dfu->i_buf_start = dfu_get_buf(dfu);
Łukasz Majewski063ae5a2013-06-28 18:41:50 +0200345 if (dfu->i_buf_start == NULL)
346 return -ENOMEM;
347
Stephen Warren020e6f32014-06-11 12:47:27 -0600348 dfu->r_left = dfu->get_medium_size(dfu);
349 if (dfu->r_left < 0)
350 return dfu->r_left;
351 switch (dfu->layout) {
352 case DFU_RAW_ADDR:
353 case DFU_RAM_ADDR:
354 break;
355 default:
Stephen Warrenafb2cf02014-07-01 12:16:57 -0600356 if (dfu->r_left > dfu_buf_size) {
Stephen Warren020e6f32014-06-11 12:47:27 -0600357 printf("%s: File too big for buffer\n",
358 __func__);
359 return -EOVERFLOW;
360 }
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000361 }
362
363 debug("%s: %s %ld [B]\n", __func__, dfu->name, dfu->r_left);
364
365 dfu->i_blk_seq_num = 0;
366 dfu->crc = 0;
367 dfu->offset = 0;
Stephen Warrenf9500982014-06-11 16:03:34 -0600368 dfu->i_buf_end = dfu_get_buf(dfu) + dfu_buf_size;
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000369 dfu->i_buf = dfu->i_buf_start;
Stephen Warren020e6f32014-06-11 12:47:27 -0600370 dfu->b_left = 0;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200371
Pantelis Antonioucf14d0d2013-03-14 05:32:52 +0000372 dfu->bad_skip = 0;
373
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000374 dfu->inited = 1;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200375 }
376
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000377 if (dfu->i_blk_seq_num != blk_seq_num) {
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200378 printf("%s: Wrong sequence number! [%d] [%d]\n",
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000379 __func__, dfu->i_blk_seq_num, blk_seq_num);
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200380 return -1;
381 }
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000382 /* handle rollover */
383 dfu->i_blk_seq_num = (dfu->i_blk_seq_num + 1) & 0xffff;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200384
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000385 ret = dfu_read_buffer_fill(dfu, buf, size);
386 if (ret < 0) {
387 printf("%s: Failed to fill buffer\n", __func__);
388 return -1;
389 }
390
391 if (ret < size) {
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200392 if (dfu_hash_algo)
393 debug("%s: %s %s: 0x%x\n", __func__, dfu->name,
394 dfu_hash_algo->name, dfu->crc);
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000395 puts("\nUPLOAD ... done\nCtrl+C to exit ...\n");
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200396
Heiko Schochera2f831e2013-06-12 06:05:51 +0200397 dfu_free_buf();
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000398 dfu->i_blk_seq_num = 0;
399 dfu->crc = 0;
400 dfu->offset = 0;
401 dfu->i_buf_start = dfu_buf;
Heiko Schochera2f831e2013-06-12 06:05:51 +0200402 dfu->i_buf_end = dfu_buf;
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000403 dfu->i_buf = dfu->i_buf_start;
404 dfu->b_left = 0;
405
Pantelis Antonioucf14d0d2013-03-14 05:32:52 +0000406 dfu->bad_skip = 0;
407
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000408 dfu->inited = 0;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200409 }
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000410
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200411 return ret;
412}
413
414static int dfu_fill_entity(struct dfu_entity *dfu, char *s, int alt,
Stephen Warren4afe50f2014-06-11 16:03:33 -0600415 char *interface, char *devstr)
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200416{
417 char *st;
418
Stephen Warren4afe50f2014-06-11 16:03:33 -0600419 debug("%s: %s interface: %s dev: %s\n", __func__, s, interface, devstr);
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200420 st = strsep(&s, " ");
421 strcpy(dfu->name, st);
422
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200423 dfu->alt = alt;
Stephen Warrenf9500982014-06-11 16:03:34 -0600424 dfu->max_buf_size = 0;
Stephen Warrena5f77f62014-06-11 16:03:35 -0600425 dfu->free_entity = NULL;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200426
427 /* Specific for mmc device */
428 if (strcmp(interface, "mmc") == 0) {
Stephen Warren4afe50f2014-06-11 16:03:33 -0600429 if (dfu_fill_entity_mmc(dfu, devstr, s))
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200430 return -1;
Pantelis Antonioucf14d0d2013-03-14 05:32:52 +0000431 } else if (strcmp(interface, "nand") == 0) {
Stephen Warren4afe50f2014-06-11 16:03:33 -0600432 if (dfu_fill_entity_nand(dfu, devstr, s))
Pantelis Antonioucf14d0d2013-03-14 05:32:52 +0000433 return -1;
Afzal Mohammede3c687a2013-09-18 01:15:24 +0530434 } else if (strcmp(interface, "ram") == 0) {
Stephen Warren4afe50f2014-06-11 16:03:33 -0600435 if (dfu_fill_entity_ram(dfu, devstr, s))
Afzal Mohammede3c687a2013-09-18 01:15:24 +0530436 return -1;
Stephen Warren0280af72014-06-11 16:03:36 -0600437 } else if (strcmp(interface, "sf") == 0) {
438 if (dfu_fill_entity_sf(dfu, devstr, s))
439 return -1;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200440 } else {
441 printf("%s: Device %s not (yet) supported!\n",
442 __func__, interface);
443 return -1;
444 }
445
446 return 0;
447}
448
449void dfu_free_entities(void)
450{
451 struct dfu_entity *dfu, *p, *t = NULL;
452
453 list_for_each_entry_safe_reverse(dfu, p, &dfu_list, list) {
454 list_del(&dfu->list);
Stephen Warrena5f77f62014-06-11 16:03:35 -0600455 if (dfu->free_entity)
456 dfu->free_entity(dfu);
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200457 t = dfu;
458 }
459 if (t)
460 free(t);
461 INIT_LIST_HEAD(&dfu_list);
Przemyslaw Marczak4af3e082014-02-28 18:53:37 +0100462
463 alt_num_cnt = 0;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200464}
465
Stephen Warren4afe50f2014-06-11 16:03:33 -0600466int dfu_config_entities(char *env, char *interface, char *devstr)
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200467{
468 struct dfu_entity *dfu;
469 int i, ret;
470 char *s;
471
472 dfu_alt_num = dfu_find_alt_num(env);
473 debug("%s: dfu_alt_num=%d\n", __func__, dfu_alt_num);
474
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200475 dfu_hash_algo = NULL;
476 s = dfu_get_hash_algo();
477 if (s) {
478 ret = hash_lookup_algo(s, &dfu_hash_algo);
479 if (ret)
480 error("Hash algorithm %s not supported\n", s);
481 }
482
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200483 dfu = calloc(sizeof(*dfu), dfu_alt_num);
484 if (!dfu)
485 return -1;
486 for (i = 0; i < dfu_alt_num; i++) {
487
488 s = strsep(&env, ";");
Stephen Warren4afe50f2014-06-11 16:03:33 -0600489 ret = dfu_fill_entity(&dfu[i], s, alt_num_cnt, interface,
490 devstr);
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200491 if (ret)
492 return -1;
493
494 list_add_tail(&dfu[i].list, &dfu_list);
Przemyslaw Marczak4af3e082014-02-28 18:53:37 +0100495 alt_num_cnt++;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200496 }
497
498 return 0;
499}
500
501const char *dfu_get_dev_type(enum dfu_device_type t)
502{
Afzal Mohammede3c687a2013-09-18 01:15:24 +0530503 const char *dev_t[] = {NULL, "eMMC", "OneNAND", "NAND", "RAM" };
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200504 return dev_t[t];
505}
506
507const char *dfu_get_layout(enum dfu_layout l)
508{
509 const char *dfu_layout[] = {NULL, "RAW_ADDR", "FAT", "EXT2",
Afzal Mohammede3c687a2013-09-18 01:15:24 +0530510 "EXT3", "EXT4", "RAM_ADDR" };
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200511 return dfu_layout[l];
512}
513
514void dfu_show_entities(void)
515{
516 struct dfu_entity *dfu;
517
518 puts("DFU alt settings list:\n");
519
520 list_for_each_entry(dfu, &dfu_list, list) {
521 printf("dev: %s alt: %d name: %s layout: %s\n",
522 dfu_get_dev_type(dfu->dev_type), dfu->alt,
523 dfu->name, dfu_get_layout(dfu->layout));
524 }
525}
526
527int dfu_get_alt_number(void)
528{
529 return dfu_alt_num;
530}
531
532struct dfu_entity *dfu_get_entity(int alt)
533{
534 struct dfu_entity *dfu;
535
536 list_for_each_entry(dfu, &dfu_list, list) {
537 if (dfu->alt == alt)
538 return dfu;
539 }
540
541 return NULL;
542}
Lukasz Majewski1737f0f2013-10-08 14:30:38 +0200543
544int dfu_get_alt(char *name)
545{
546 struct dfu_entity *dfu;
547
548 list_for_each_entry(dfu, &dfu_list, list) {
549 if (!strncmp(dfu->name, name, strlen(dfu->name)))
550 return dfu->alt;
551 }
552
553 return -ENODEV;
554}