blob: df50196dfda5e16727724569bcb65d4fee7c57a5 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Lukasz Majewskid311c6e2012-08-06 14:41:07 +02002/*
3 * dfu.c -- DFU back-end routines
4 *
5 * Copyright (C) 2012 Samsung Electronics
6 * author: Lukasz Majewski <l.majewski@samsung.com>
Lukasz Majewskid311c6e2012-08-06 14:41:07 +02007 */
8
9#include <common.h>
Simon Glass0af6e2d2019-08-01 09:46:52 -060010#include <env.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;
Andy Shevchenko1e414012019-11-27 18:12:15 +020024#ifdef CONFIG_DFU_TIMEOUT
25static unsigned long dfu_timeout = 0;
26#endif
Lukasz Majewskid311c6e2012-08-06 14:41:07 +020027
Lukasz Majewskid9eae702014-08-25 11:07:28 +020028/*
Patrick Delaunay773771f2019-10-14 09:28:07 +020029 * The purpose of the dfu_flush_callback() function is to
30 * provide callback for dfu user
31 */
32__weak void dfu_flush_callback(struct dfu_entity *dfu)
33{
34}
35
36/*
37 * The purpose of the dfu_initiated_callback() function is to
38 * provide callback for dfu user
39 */
40__weak void dfu_initiated_callback(struct dfu_entity *dfu)
41{
42}
43
44/*
Lukasz Majewskid9eae702014-08-25 11:07:28 +020045 * The purpose of the dfu_usb_get_reset() function is to
46 * provide information if after USB_DETACH request
47 * being sent the dfu-util performed reset of USB
48 * bus.
49 *
50 * Described behaviour is the only way to distinct if
51 * user has typed -e (detach) or -R (reset) when invoking
52 * dfu-util command.
53 *
54 */
55__weak bool dfu_usb_get_reset(void)
Lukasz Majewski2968aa22013-07-18 13:19:14 +020056{
B, Ravi4cca5cf2017-05-04 15:45:29 +053057#ifdef CONFIG_SPL_DFU_NO_RESET
58 return false;
59#else
Lukasz Majewskid9eae702014-08-25 11:07:28 +020060 return true;
B, Ravi4cca5cf2017-05-04 15:45:29 +053061#endif
Lukasz Majewski2968aa22013-07-18 13:19:14 +020062}
63
Andy Shevchenko1e414012019-11-27 18:12:15 +020064#ifdef CONFIG_DFU_TIMEOUT
65void dfu_set_timeout(unsigned long timeout)
66{
67 dfu_timeout = timeout;
68}
69
70unsigned long dfu_get_timeout(void)
71{
72 return dfu_timeout;
73}
74#endif
75
Lukasz Majewskid311c6e2012-08-06 14:41:07 +020076static int dfu_find_alt_num(const char *s)
77{
78 int i = 0;
79
80 for (; *s; s++)
81 if (*s == ';')
82 i++;
83
84 return ++i;
85}
86
Patrick Delaunay06164032019-10-14 09:28:02 +020087/*
88 * treat dfu_alt_info with several interface information
89 * to allow DFU on several device with one command,
90 * the string format is
91 * interface devstring'='alternate list (';' separated)
92 * and each interface separated by '&'
93 */
94int dfu_config_interfaces(char *env)
95{
96 struct dfu_entity *dfu;
97 char *s, *i, *d, *a, *part;
98 int ret = -EINVAL;
99 int n = 1;
100
101 s = env;
102 for (; *s; s++) {
103 if (*s == ';')
104 n++;
105 if (*s == '&')
106 n++;
107 }
108 ret = dfu_alt_init(n, &dfu);
109 if (ret)
110 return ret;
111
112 s = env;
113 while (s) {
114 ret = -EINVAL;
115 i = strsep(&s, " ");
116 if (!i)
117 break;
118 d = strsep(&s, "=");
119 if (!d)
120 break;
121 a = strsep(&s, "&");
122 if (!a)
123 a = s;
124 do {
125 part = strsep(&a, ";");
126 ret = dfu_alt_add(dfu, i, d, part);
127 if (ret)
128 return ret;
129 } while (a);
130 }
131
132 return ret;
133}
134
Stephen Warren4afe50f2014-06-11 16:03:33 -0600135int dfu_init_env_entities(char *interface, char *devstr)
Lukasz Majewski35e591e2013-09-11 14:53:35 +0200136{
137 const char *str_env;
138 char *env_bkp;
Sam Protsenko6500cde2018-07-13 16:35:46 +0300139 int ret = 0;
Lukasz Majewski35e591e2013-09-11 14:53:35 +0200140
Przemyslaw Marczakd0dd6092015-02-17 12:24:11 +0100141#ifdef CONFIG_SET_DFU_ALT_INFO
142 set_dfu_alt_info(interface, devstr);
143#endif
Simon Glass64b723f2017-08-03 12:22:12 -0600144 str_env = env_get("dfu_alt_info");
Lukasz Majewski35e591e2013-09-11 14:53:35 +0200145 if (!str_env) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900146 pr_err("\"dfu_alt_info\" env variable not defined!\n");
Lukasz Majewski35e591e2013-09-11 14:53:35 +0200147 return -EINVAL;
148 }
149
150 env_bkp = strdup(str_env);
Patrick Delaunay06164032019-10-14 09:28:02 +0200151 if (!interface && !devstr)
152 ret = dfu_config_interfaces(env_bkp);
153 else
154 ret = dfu_config_entities(env_bkp, interface, devstr);
155
Lukasz Majewski35e591e2013-09-11 14:53:35 +0200156 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900157 pr_err("DFU entities configuration failed!\n");
Sam Protsenkoab79fb02018-07-13 16:35:47 +0300158 pr_err("(partition table does not match dfu_alt_info?)\n");
Sam Protsenko6500cde2018-07-13 16:35:46 +0300159 goto done;
Lukasz Majewski35e591e2013-09-11 14:53:35 +0200160 }
161
Sam Protsenko6500cde2018-07-13 16:35:46 +0300162done:
Lukasz Majewski35e591e2013-09-11 14:53:35 +0200163 free(env_bkp);
Sam Protsenko6500cde2018-07-13 16:35:46 +0300164 return ret;
Lukasz Majewski35e591e2013-09-11 14:53:35 +0200165}
166
Heiko Schochera2f831e2013-06-12 06:05:51 +0200167static unsigned char *dfu_buf;
Lukasz Majewski0af4df82015-07-08 23:43:18 +0200168static unsigned long dfu_buf_size;
Patrick Delaunay06164032019-10-14 09:28:02 +0200169static enum dfu_device_type dfu_buf_device_type;
Heiko Schochera2f831e2013-06-12 06:05:51 +0200170
Lukasz Majewski34264232013-10-08 14:30:39 +0200171unsigned char *dfu_free_buf(void)
Heiko Schochera2f831e2013-06-12 06:05:51 +0200172{
173 free(dfu_buf);
174 dfu_buf = NULL;
175 return dfu_buf;
176}
177
Lukasz Majewski031d4132013-12-09 16:20:13 +0100178unsigned long dfu_get_buf_size(void)
179{
180 return dfu_buf_size;
181}
182
Stephen Warrenf9500982014-06-11 16:03:34 -0600183unsigned char *dfu_get_buf(struct dfu_entity *dfu)
Heiko Schochera2f831e2013-06-12 06:05:51 +0200184{
185 char *s;
186
Patrick Delaunay06164032019-10-14 09:28:02 +0200187 /* manage several entity with several contraint */
188 if (dfu_buf && dfu->dev_type != dfu_buf_device_type)
189 dfu_free_buf();
190
Heiko Schochera2f831e2013-06-12 06:05:51 +0200191 if (dfu_buf != NULL)
192 return dfu_buf;
193
Simon Glass64b723f2017-08-03 12:22:12 -0600194 s = env_get("dfu_bufsiz");
Przemyslaw Marczak24883502014-12-15 10:34:11 +0100195 if (s)
196 dfu_buf_size = (unsigned long)simple_strtol(s, NULL, 0);
197
198 if (!s || !dfu_buf_size)
199 dfu_buf_size = CONFIG_SYS_DFU_DATA_BUF_SIZE;
200
Stephen Warrenf9500982014-06-11 16:03:34 -0600201 if (dfu->max_buf_size && dfu_buf_size > dfu->max_buf_size)
202 dfu_buf_size = dfu->max_buf_size;
Heiko Schochera2f831e2013-06-12 06:05:51 +0200203
204 dfu_buf = memalign(CONFIG_SYS_CACHELINE_SIZE, dfu_buf_size);
205 if (dfu_buf == NULL)
206 printf("%s: Could not memalign 0x%lx bytes\n",
207 __func__, dfu_buf_size);
208
Patrick Delaunay06164032019-10-14 09:28:02 +0200209 dfu_buf_device_type = dfu->dev_type;
Heiko Schochera2f831e2013-06-12 06:05:51 +0200210 return dfu_buf;
211}
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200212
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200213static char *dfu_get_hash_algo(void)
214{
215 char *s;
216
Simon Glass64b723f2017-08-03 12:22:12 -0600217 s = env_get("dfu_hash_algo");
Lukasz Majewski3e0d0282014-06-10 12:25:59 +0200218 if (!s)
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200219 return NULL;
220
Lukasz Majewski3e0d0282014-06-10 12:25:59 +0200221 if (!strcmp(s, "crc32")) {
222 debug("%s: DFU hash method: %s\n", __func__, s);
223 return s;
224 }
225
Masahiro Yamada81e10422017-09-16 14:10:41 +0900226 pr_err("DFU hash method: %s not supported!\n", s);
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200227 return NULL;
228}
229
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000230static int dfu_write_buffer_drain(struct dfu_entity *dfu)
231{
232 long w_size;
233 int ret;
234
235 /* flush size? */
236 w_size = dfu->i_buf - dfu->i_buf_start;
237 if (w_size == 0)
238 return 0;
239
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200240 if (dfu_hash_algo)
241 dfu_hash_algo->hash_update(dfu_hash_algo, &dfu->crc,
242 dfu->i_buf_start, w_size, 0);
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000243
244 ret = dfu->write_medium(dfu, dfu->offset, dfu->i_buf_start, &w_size);
245 if (ret)
246 debug("%s: Write error!\n", __func__);
247
248 /* point back */
249 dfu->i_buf = dfu->i_buf_start;
250
251 /* update offset */
252 dfu->offset += w_size;
253
254 puts("#");
255
256 return ret;
257}
258
Patrick Delaunay35be83e2017-07-19 16:39:24 +0200259void dfu_transaction_cleanup(struct dfu_entity *dfu)
Stephen Warren7f450232014-06-11 12:48:08 -0600260{
261 /* clear everything */
Stephen Warren7f450232014-06-11 12:48:08 -0600262 dfu->crc = 0;
263 dfu->offset = 0;
264 dfu->i_blk_seq_num = 0;
Patrick Delaunaydec4ecb2017-07-19 16:39:25 +0200265 dfu->i_buf_start = dfu_get_buf(dfu);
266 dfu->i_buf_end = dfu->i_buf_start;
Stephen Warren7f450232014-06-11 12:48:08 -0600267 dfu->i_buf = dfu->i_buf_start;
Patrick Delaunay35be83e2017-07-19 16:39:24 +0200268 dfu->r_left = 0;
269 dfu->b_left = 0;
270 dfu->bad_skip = 0;
271
Stephen Warren7f450232014-06-11 12:48:08 -0600272 dfu->inited = 0;
273}
274
Patrick Delaunaydec4ecb2017-07-19 16:39:25 +0200275int dfu_transaction_initiate(struct dfu_entity *dfu, bool read)
276{
277 int ret = 0;
278
279 if (dfu->inited)
280 return 0;
281
282 dfu_transaction_cleanup(dfu);
283
284 if (dfu->i_buf_start == NULL)
285 return -ENOMEM;
286
287 dfu->i_buf_end = dfu->i_buf_start + dfu_get_buf_size();
288
289 if (read) {
290 ret = dfu->get_medium_size(dfu, &dfu->r_left);
291 if (ret < 0)
292 return ret;
293 debug("%s: %s %lld [B]\n", __func__, dfu->name, dfu->r_left);
294 }
295
296 dfu->inited = 1;
Patrick Delaunay773771f2019-10-14 09:28:07 +0200297 dfu_initiated_callback(dfu);
Patrick Delaunaydec4ecb2017-07-19 16:39:25 +0200298
299 return 0;
300}
301
Heiko Schocher941b3842014-03-18 08:09:55 +0100302int dfu_flush(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
303{
304 int ret = 0;
305
Przemyslaw Marczaka0f6a382014-05-12 12:05:33 +0200306 ret = dfu_write_buffer_drain(dfu);
307 if (ret)
308 return ret;
309
Heiko Schocher941b3842014-03-18 08:09:55 +0100310 if (dfu->flush_medium)
311 ret = dfu->flush_medium(dfu);
312
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200313 if (dfu_hash_algo)
314 printf("\nDFU complete %s: 0x%08x\n", dfu_hash_algo->name,
315 dfu->crc);
Heiko Schocher941b3842014-03-18 08:09:55 +0100316
Patrick Delaunay773771f2019-10-14 09:28:07 +0200317 dfu_flush_callback(dfu);
318
Patrick Delaunay35be83e2017-07-19 16:39:24 +0200319 dfu_transaction_cleanup(dfu);
Heiko Schocher941b3842014-03-18 08:09:55 +0100320
321 return ret;
322}
323
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200324int dfu_write(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
325{
Stephen Warren7f450232014-06-11 12:48:08 -0600326 int ret;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200327
Stephen Warrenc00315b2015-07-22 14:54:04 -0600328 debug("%s: name: %s buf: 0x%p size: 0x%x p_num: 0x%x offset: 0x%llx bufoffset: 0x%lx\n",
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000329 __func__, dfu->name, buf, size, blk_seq_num, dfu->offset,
Stephen Warrenc00315b2015-07-22 14:54:04 -0600330 (unsigned long)(dfu->i_buf - dfu->i_buf_start));
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200331
Patrick Delaunaydec4ecb2017-07-19 16:39:25 +0200332 ret = dfu_transaction_initiate(dfu, false);
333 if (ret < 0)
334 return ret;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200335
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000336 if (dfu->i_blk_seq_num != blk_seq_num) {
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200337 printf("%s: Wrong sequence number! [%d] [%d]\n",
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000338 __func__, dfu->i_blk_seq_num, blk_seq_num);
Patrick Delaunay35be83e2017-07-19 16:39:24 +0200339 dfu_transaction_cleanup(dfu);
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000340 return -1;
341 }
342
343 /* DFU 1.1 standard says:
344 * The wBlockNum field is a block sequence number. It increments each
345 * time a block is transferred, wrapping to zero from 65,535. It is used
346 * to provide useful context to the DFU loader in the device."
347 *
348 * This means that it's a 16 bit counter that roll-overs at
349 * 0xffff -> 0x0000. By having a typical 4K transfer block
350 * we roll-over at exactly 256MB. Not very fun to debug.
351 *
352 * Handling rollover, and having an inited variable,
353 * makes things work.
354 */
355
356 /* handle rollover */
357 dfu->i_blk_seq_num = (dfu->i_blk_seq_num + 1) & 0xffff;
358
359 /* flush buffer if overflow */
360 if ((dfu->i_buf + size) > dfu->i_buf_end) {
Stephen Warren7f450232014-06-11 12:48:08 -0600361 ret = dfu_write_buffer_drain(dfu);
362 if (ret) {
Patrick Delaunay35be83e2017-07-19 16:39:24 +0200363 dfu_transaction_cleanup(dfu);
Stephen Warren7f450232014-06-11 12:48:08 -0600364 return ret;
365 }
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000366 }
367
368 /* we should be in buffer now (if not then size too large) */
369 if ((dfu->i_buf + size) > dfu->i_buf_end) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900370 pr_err("Buffer overflow! (0x%p + 0x%x > 0x%p)\n", dfu->i_buf,
Lukasz Majewski385c3212013-09-10 15:29:22 +0200371 size, dfu->i_buf_end);
Patrick Delaunay35be83e2017-07-19 16:39:24 +0200372 dfu_transaction_cleanup(dfu);
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200373 return -1;
374 }
375
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000376 memcpy(dfu->i_buf, buf, size);
377 dfu->i_buf += size;
378
379 /* if end or if buffer full flush */
380 if (size == 0 || (dfu->i_buf + size) > dfu->i_buf_end) {
Stephen Warren7f450232014-06-11 12:48:08 -0600381 ret = dfu_write_buffer_drain(dfu);
382 if (ret) {
Patrick Delaunay35be83e2017-07-19 16:39:24 +0200383 dfu_transaction_cleanup(dfu);
Stephen Warren7f450232014-06-11 12:48:08 -0600384 return ret;
385 }
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000386 }
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200387
Stephen Warren7f450232014-06-11 12:48:08 -0600388 return 0;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200389}
390
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000391static int dfu_read_buffer_fill(struct dfu_entity *dfu, void *buf, int size)
392{
393 long chunk;
394 int ret, readn;
395
396 readn = 0;
397 while (size > 0) {
398 /* get chunk that can be read */
Masahiro Yamadadb204642014-11-07 03:03:31 +0900399 chunk = min((long)size, dfu->b_left);
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000400 /* consume */
401 if (chunk > 0) {
402 memcpy(buf, dfu->i_buf, chunk);
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200403 if (dfu_hash_algo)
404 dfu_hash_algo->hash_update(dfu_hash_algo,
405 &dfu->crc, buf,
406 chunk, 0);
407
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000408 dfu->i_buf += chunk;
409 dfu->b_left -= chunk;
410 size -= chunk;
411 buf += chunk;
412 readn += chunk;
413 }
414
415 /* all done */
416 if (size > 0) {
417 /* no more to read */
418 if (dfu->r_left == 0)
419 break;
420
421 dfu->i_buf = dfu->i_buf_start;
422 dfu->b_left = dfu->i_buf_end - dfu->i_buf_start;
423
424 /* got to read, but buffer is empty */
425 if (dfu->b_left > dfu->r_left)
426 dfu->b_left = dfu->r_left;
427 ret = dfu->read_medium(dfu, dfu->offset, dfu->i_buf,
428 &dfu->b_left);
429 if (ret != 0) {
430 debug("%s: Read error!\n", __func__);
431 return ret;
432 }
Patrick Delaunay7ef84362019-10-14 09:28:03 +0200433 if (dfu->b_left == 0)
434 break;
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000435 dfu->offset += dfu->b_left;
436 dfu->r_left -= dfu->b_left;
437
438 puts("#");
439 }
440 }
441
442 return readn;
443}
444
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200445int dfu_read(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
446{
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200447 int ret = 0;
448
449 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 +0000450 __func__, dfu->name, buf, size, blk_seq_num, dfu->i_buf);
451
Patrick Delaunaydec4ecb2017-07-19 16:39:25 +0200452 ret = dfu_transaction_initiate(dfu, true);
453 if (ret < 0)
454 return ret;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200455
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000456 if (dfu->i_blk_seq_num != blk_seq_num) {
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200457 printf("%s: Wrong sequence number! [%d] [%d]\n",
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000458 __func__, dfu->i_blk_seq_num, blk_seq_num);
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200459 return -1;
460 }
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000461 /* handle rollover */
462 dfu->i_blk_seq_num = (dfu->i_blk_seq_num + 1) & 0xffff;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200463
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000464 ret = dfu_read_buffer_fill(dfu, buf, size);
465 if (ret < 0) {
466 printf("%s: Failed to fill buffer\n", __func__);
467 return -1;
468 }
469
470 if (ret < size) {
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200471 if (dfu_hash_algo)
472 debug("%s: %s %s: 0x%x\n", __func__, dfu->name,
473 dfu_hash_algo->name, dfu->crc);
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000474 puts("\nUPLOAD ... done\nCtrl+C to exit ...\n");
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200475
Patrick Delaunay35be83e2017-07-19 16:39:24 +0200476 dfu_transaction_cleanup(dfu);
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200477 }
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000478
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200479 return ret;
480}
481
482static int dfu_fill_entity(struct dfu_entity *dfu, char *s, int alt,
Stephen Warren4afe50f2014-06-11 16:03:33 -0600483 char *interface, char *devstr)
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200484{
485 char *st;
486
Stephen Warren4afe50f2014-06-11 16:03:33 -0600487 debug("%s: %s interface: %s dev: %s\n", __func__, s, interface, devstr);
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200488 st = strsep(&s, " ");
489 strcpy(dfu->name, st);
490
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200491 dfu->alt = alt;
Stephen Warrenf9500982014-06-11 16:03:34 -0600492 dfu->max_buf_size = 0;
Stephen Warrena5f77f62014-06-11 16:03:35 -0600493 dfu->free_entity = NULL;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200494
495 /* Specific for mmc device */
496 if (strcmp(interface, "mmc") == 0) {
Stephen Warren4afe50f2014-06-11 16:03:33 -0600497 if (dfu_fill_entity_mmc(dfu, devstr, s))
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200498 return -1;
Patrick Delaunayf7aee232019-10-14 09:28:04 +0200499 } else if (strcmp(interface, "mtd") == 0) {
500 if (dfu_fill_entity_mtd(dfu, devstr, s))
501 return -1;
Pantelis Antonioucf14d0d2013-03-14 05:32:52 +0000502 } else if (strcmp(interface, "nand") == 0) {
Stephen Warren4afe50f2014-06-11 16:03:33 -0600503 if (dfu_fill_entity_nand(dfu, devstr, s))
Pantelis Antonioucf14d0d2013-03-14 05:32:52 +0000504 return -1;
Afzal Mohammede3c687a2013-09-18 01:15:24 +0530505 } else if (strcmp(interface, "ram") == 0) {
Stephen Warren4afe50f2014-06-11 16:03:33 -0600506 if (dfu_fill_entity_ram(dfu, devstr, s))
Afzal Mohammede3c687a2013-09-18 01:15:24 +0530507 return -1;
Stephen Warren0280af72014-06-11 16:03:36 -0600508 } else if (strcmp(interface, "sf") == 0) {
509 if (dfu_fill_entity_sf(dfu, devstr, s))
510 return -1;
Patrick Delaunay2111e3c2019-10-14 09:28:06 +0200511 } else if (strcmp(interface, "virt") == 0) {
512 if (dfu_fill_entity_virt(dfu, devstr, s))
513 return -1;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200514 } else {
515 printf("%s: Device %s not (yet) supported!\n",
516 __func__, interface);
517 return -1;
518 }
Stephen Warren0898da52015-09-04 22:03:46 -0600519 dfu_get_buf(dfu);
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200520
521 return 0;
522}
523
524void dfu_free_entities(void)
525{
526 struct dfu_entity *dfu, *p, *t = NULL;
527
Stephen Warren0898da52015-09-04 22:03:46 -0600528 dfu_free_buf();
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200529 list_for_each_entry_safe_reverse(dfu, p, &dfu_list, list) {
530 list_del(&dfu->list);
Stephen Warrena5f77f62014-06-11 16:03:35 -0600531 if (dfu->free_entity)
532 dfu->free_entity(dfu);
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200533 t = dfu;
534 }
535 if (t)
536 free(t);
537 INIT_LIST_HEAD(&dfu_list);
Przemyslaw Marczak4af3e082014-02-28 18:53:37 +0100538
539 alt_num_cnt = 0;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200540}
541
Patrick Delaunay0f8fad82019-10-14 09:28:01 +0200542int dfu_alt_init(int num, struct dfu_entity **dfu)
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200543{
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200544 char *s;
Patrick Delaunay0f8fad82019-10-14 09:28:01 +0200545 int ret;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200546
Patrick Delaunay0f8fad82019-10-14 09:28:01 +0200547 dfu_alt_num = num;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200548 debug("%s: dfu_alt_num=%d\n", __func__, dfu_alt_num);
549
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200550 dfu_hash_algo = NULL;
551 s = dfu_get_hash_algo();
552 if (s) {
553 ret = hash_lookup_algo(s, &dfu_hash_algo);
554 if (ret)
Masahiro Yamada81e10422017-09-16 14:10:41 +0900555 pr_err("Hash algorithm %s not supported\n", s);
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200556 }
557
Patrick Delaunay0f8fad82019-10-14 09:28:01 +0200558 *dfu = calloc(sizeof(struct dfu_entity), dfu_alt_num);
559 if (!*dfu)
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200560 return -1;
Patrick Delaunay0f8fad82019-10-14 09:28:01 +0200561
562 return 0;
563}
564
565int dfu_alt_add(struct dfu_entity *dfu, char *interface, char *devstr, char *s)
566{
567 struct dfu_entity *p_dfu;
568 int ret;
569
570 if (alt_num_cnt >= dfu_alt_num)
571 return -1;
572
573 p_dfu = &dfu[alt_num_cnt];
574 ret = dfu_fill_entity(p_dfu, s, alt_num_cnt, interface, devstr);
575 if (ret)
576 return -1;
577
578 list_add_tail(&p_dfu->list, &dfu_list);
579 alt_num_cnt++;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200580
Patrick Delaunay0f8fad82019-10-14 09:28:01 +0200581 return 0;
582}
583
584int dfu_config_entities(char *env, char *interface, char *devstr)
585{
586 struct dfu_entity *dfu;
587 int i, ret;
588 char *s;
589
590 ret = dfu_alt_init(dfu_find_alt_num(env), &dfu);
591 if (ret)
592 return -1;
593
594 for (i = 0; i < dfu_alt_num; i++) {
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200595 s = strsep(&env, ";");
Patrick Delaunay0f8fad82019-10-14 09:28:01 +0200596 ret = dfu_alt_add(dfu, interface, devstr, s);
Peng Fan529d4fd2016-05-03 10:24:52 +0800597 if (ret) {
Sam Protsenko6d29e5c2018-07-13 16:35:45 +0300598 /* We will free "dfu" in dfu_free_entities() */
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200599 return -1;
Peng Fan529d4fd2016-05-03 10:24:52 +0800600 }
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200601 }
602
603 return 0;
604}
605
606const char *dfu_get_dev_type(enum dfu_device_type t)
607{
Patrick Delaunayae224c32019-10-14 09:27:58 +0200608 const char *const dev_t[] = {NULL, "eMMC", "OneNAND", "NAND", "RAM",
Patrick Delaunay2111e3c2019-10-14 09:28:06 +0200609 "SF", "MTD", "VIRT"};
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200610 return dev_t[t];
611}
612
613const char *dfu_get_layout(enum dfu_layout l)
614{
Patrick Delaunayae224c32019-10-14 09:27:58 +0200615 const char *const dfu_layout[] = {NULL, "RAW_ADDR", "FAT", "EXT2",
616 "EXT3", "EXT4", "RAM_ADDR" };
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200617 return dfu_layout[l];
618}
619
620void dfu_show_entities(void)
621{
622 struct dfu_entity *dfu;
623
624 puts("DFU alt settings list:\n");
625
626 list_for_each_entry(dfu, &dfu_list, list) {
627 printf("dev: %s alt: %d name: %s layout: %s\n",
628 dfu_get_dev_type(dfu->dev_type), dfu->alt,
629 dfu->name, dfu_get_layout(dfu->layout));
630 }
631}
632
633int dfu_get_alt_number(void)
634{
635 return dfu_alt_num;
636}
637
638struct dfu_entity *dfu_get_entity(int alt)
639{
640 struct dfu_entity *dfu;
641
642 list_for_each_entry(dfu, &dfu_list, list) {
643 if (dfu->alt == alt)
644 return dfu;
645 }
646
647 return NULL;
648}
Lukasz Majewski1737f0f2013-10-08 14:30:38 +0200649
650int dfu_get_alt(char *name)
651{
652 struct dfu_entity *dfu;
Lukasz Majewski3a39d012014-11-05 10:54:16 +0100653 char *str;
Lukasz Majewski1737f0f2013-10-08 14:30:38 +0200654
655 list_for_each_entry(dfu, &dfu_list, list) {
Lukasz Majewski3a39d012014-11-05 10:54:16 +0100656 if (dfu->name[0] != '/') {
657 if (!strncmp(dfu->name, name, strlen(dfu->name)))
658 return dfu->alt;
659 } else {
660 /*
661 * One must also consider absolute path
662 * (/boot/bin/uImage) available at dfu->name when
663 * compared "plain" file name (uImage)
664 *
665 * It is the case for e.g. thor gadget where lthor SW
666 * sends only the file name, so only the very last part
667 * of path must be checked for equality
668 */
669
670 str = strstr(dfu->name, name);
671 if (!str)
672 continue;
673
674 /*
675 * Check if matching substring is the last element of
676 * dfu->name (uImage)
677 */
678 if (strlen(dfu->name) ==
679 ((str - dfu->name) + strlen(name)))
680 return dfu->alt;
681 }
Lukasz Majewski1737f0f2013-10-08 14:30:38 +0200682 }
683
684 return -ENODEV;
685}
Lukasz Majewskicc2d0262015-08-24 00:21:46 +0200686
687int dfu_write_from_mem_addr(struct dfu_entity *dfu, void *buf, int size)
688{
689 unsigned long dfu_buf_size, write, left = size;
690 int i, ret = 0;
691 void *dp = buf;
692
693 /*
694 * Here we must call dfu_get_buf(dfu) first to be sure that dfu_buf_size
695 * has been properly initialized - e.g. if "dfu_bufsiz" has been taken
696 * into account.
697 */
698 dfu_get_buf(dfu);
699 dfu_buf_size = dfu_get_buf_size();
700 debug("%s: dfu buf size: %lu\n", __func__, dfu_buf_size);
701
702 for (i = 0; left > 0; i++) {
703 write = min(dfu_buf_size, left);
704
705 debug("%s: dp: 0x%p left: %lu write: %lu\n", __func__,
706 dp, left, write);
707 ret = dfu_write(dfu, dp, write, i);
708 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900709 pr_err("DFU write failed\n");
Lukasz Majewskicc2d0262015-08-24 00:21:46 +0200710 return ret;
711 }
712
713 dp += write;
714 left -= write;
715 }
716
717 ret = dfu_flush(dfu, NULL, 0, i);
718 if (ret)
Masahiro Yamada81e10422017-09-16 14:10:41 +0900719 pr_err("DFU flush failed!");
Lukasz Majewskicc2d0262015-08-24 00:21:46 +0200720
721 return ret;
722}