blob: df50196dfda5e16727724569bcb65d4fee7c57a5 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Lukasz Majewskif22b11c2012-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 Majewskif22b11c2012-08-06 14:41:07 +02007 */
8
9#include <common.h>
Simon Glass7b51b572019-08-01 09:46:52 -060010#include <env.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
20static LIST_HEAD(dfu_list);
21static int dfu_alt_num;
Przemyslaw Marczakb627eb42014-02-28 18:53:37 +010022static int alt_num_cnt;
Lukasz Majewskibd694242014-05-12 10:43:36 +020023static struct hash_algo *dfu_hash_algo;
Andy Shevchenko98a8f442019-11-27 18:12:15 +020024#ifdef CONFIG_DFU_TIMEOUT
25static unsigned long dfu_timeout = 0;
26#endif
Lukasz Majewskif22b11c2012-08-06 14:41:07 +020027
Lukasz Majewski1cc03c52014-08-25 11:07:28 +020028/*
Patrick Delaunay067c13c2019-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 Majewski1cc03c52014-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 Majewski6bed7ce2013-07-18 13:19:14 +020056{
B, Ravi66928af2017-05-04 15:45:29 +053057#ifdef CONFIG_SPL_DFU_NO_RESET
58 return false;
59#else
Lukasz Majewski1cc03c52014-08-25 11:07:28 +020060 return true;
B, Ravi66928af2017-05-04 15:45:29 +053061#endif
Lukasz Majewski6bed7ce2013-07-18 13:19:14 +020062}
63
Andy Shevchenko98a8f442019-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 Majewskif22b11c2012-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 Delaunayfebabe32019-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 Warrendd648272014-06-11 16:03:33 -0600135int dfu_init_env_entities(char *interface, char *devstr)
Lukasz Majewski765c5ae2013-09-11 14:53:35 +0200136{
137 const char *str_env;
138 char *env_bkp;
Sam Protsenko87a8ca92018-07-13 16:35:46 +0300139 int ret = 0;
Lukasz Majewski765c5ae2013-09-11 14:53:35 +0200140
Przemyslaw Marczak899a5282015-02-17 12:24:11 +0100141#ifdef CONFIG_SET_DFU_ALT_INFO
142 set_dfu_alt_info(interface, devstr);
143#endif
Simon Glass00caae62017-08-03 12:22:12 -0600144 str_env = env_get("dfu_alt_info");
Lukasz Majewski765c5ae2013-09-11 14:53:35 +0200145 if (!str_env) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900146 pr_err("\"dfu_alt_info\" env variable not defined!\n");
Lukasz Majewski765c5ae2013-09-11 14:53:35 +0200147 return -EINVAL;
148 }
149
150 env_bkp = strdup(str_env);
Patrick Delaunayfebabe32019-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 Majewski765c5ae2013-09-11 14:53:35 +0200156 if (ret) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900157 pr_err("DFU entities configuration failed!\n");
Sam Protsenko28a5c882018-07-13 16:35:47 +0300158 pr_err("(partition table does not match dfu_alt_info?)\n");
Sam Protsenko87a8ca92018-07-13 16:35:46 +0300159 goto done;
Lukasz Majewski765c5ae2013-09-11 14:53:35 +0200160 }
161
Sam Protsenko87a8ca92018-07-13 16:35:46 +0300162done:
Lukasz Majewski765c5ae2013-09-11 14:53:35 +0200163 free(env_bkp);
Sam Protsenko87a8ca92018-07-13 16:35:46 +0300164 return ret;
Lukasz Majewski765c5ae2013-09-11 14:53:35 +0200165}
166
Heiko Schochere7e75c72013-06-12 06:05:51 +0200167static unsigned char *dfu_buf;
Lukasz Majewskia7e68922015-07-08 23:43:18 +0200168static unsigned long dfu_buf_size;
Patrick Delaunayfebabe32019-10-14 09:28:02 +0200169static enum dfu_device_type dfu_buf_device_type;
Heiko Schochere7e75c72013-06-12 06:05:51 +0200170
Lukasz Majewskid4278262013-10-08 14:30:39 +0200171unsigned char *dfu_free_buf(void)
Heiko Schochere7e75c72013-06-12 06:05:51 +0200172{
173 free(dfu_buf);
174 dfu_buf = NULL;
175 return dfu_buf;
176}
177
Lukasz Majewski4fb12782013-12-09 16:20:13 +0100178unsigned long dfu_get_buf_size(void)
179{
180 return dfu_buf_size;
181}
182
Stephen Warren7ac1b412014-06-11 16:03:34 -0600183unsigned char *dfu_get_buf(struct dfu_entity *dfu)
Heiko Schochere7e75c72013-06-12 06:05:51 +0200184{
185 char *s;
186
Patrick Delaunayfebabe32019-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 Schochere7e75c72013-06-12 06:05:51 +0200191 if (dfu_buf != NULL)
192 return dfu_buf;
193
Simon Glass00caae62017-08-03 12:22:12 -0600194 s = env_get("dfu_bufsiz");
Przemyslaw Marczakf597fc32014-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 Warren7ac1b412014-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 Schochere7e75c72013-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 Delaunayfebabe32019-10-14 09:28:02 +0200209 dfu_buf_device_type = dfu->dev_type;
Heiko Schochere7e75c72013-06-12 06:05:51 +0200210 return dfu_buf;
211}
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200212
Lukasz Majewskibd694242014-05-12 10:43:36 +0200213static char *dfu_get_hash_algo(void)
214{
215 char *s;
216
Simon Glass00caae62017-08-03 12:22:12 -0600217 s = env_get("dfu_hash_algo");
Lukasz Majewski3d83e672014-06-10 12:25:59 +0200218 if (!s)
Lukasz Majewskibd694242014-05-12 10:43:36 +0200219 return NULL;
220
Lukasz Majewski3d83e672014-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 Yamada9b643e32017-09-16 14:10:41 +0900226 pr_err("DFU hash method: %s not supported!\n", s);
Lukasz Majewskibd694242014-05-12 10:43:36 +0200227 return NULL;
228}
229
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000230static int dfu_write_buffer_drain(struct dfu_entity *dfu)
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200231{
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000232 long w_size;
233 int ret;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200234
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000235 /* flush size? */
236 w_size = dfu->i_buf - dfu->i_buf_start;
237 if (w_size == 0)
238 return 0;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200239
Lukasz Majewskibd694242014-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);
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200243
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000244 ret = dfu->write_medium(dfu, dfu->offset, dfu->i_buf_start, &w_size);
245 if (ret)
246 debug("%s: Write error!\n", __func__);
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200247
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000248 /* point back */
249 dfu->i_buf = dfu->i_buf_start;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200250
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000251 /* update offset */
252 dfu->offset += w_size;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200253
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000254 puts("#");
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200255
256 return ret;
257}
258
Patrick Delaunay57da0602017-07-19 16:39:24 +0200259void dfu_transaction_cleanup(struct dfu_entity *dfu)
Stephen Warren3ee95932014-06-11 12:48:08 -0600260{
261 /* clear everything */
Stephen Warren3ee95932014-06-11 12:48:08 -0600262 dfu->crc = 0;
263 dfu->offset = 0;
264 dfu->i_blk_seq_num = 0;
Patrick Delaunay6fa8ddd2017-07-19 16:39:25 +0200265 dfu->i_buf_start = dfu_get_buf(dfu);
266 dfu->i_buf_end = dfu->i_buf_start;
Stephen Warren3ee95932014-06-11 12:48:08 -0600267 dfu->i_buf = dfu->i_buf_start;
Patrick Delaunay57da0602017-07-19 16:39:24 +0200268 dfu->r_left = 0;
269 dfu->b_left = 0;
270 dfu->bad_skip = 0;
271
Stephen Warren3ee95932014-06-11 12:48:08 -0600272 dfu->inited = 0;
273}
274
Patrick Delaunay6fa8ddd2017-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 Delaunay067c13c2019-10-14 09:28:07 +0200297 dfu_initiated_callback(dfu);
Patrick Delaunay6fa8ddd2017-07-19 16:39:25 +0200298
299 return 0;
300}
301
Heiko Schochera2199af2014-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 Marczak1aa4bdc2014-05-12 12:05:33 +0200306 ret = dfu_write_buffer_drain(dfu);
307 if (ret)
308 return ret;
309
Heiko Schochera2199af2014-03-18 08:09:55 +0100310 if (dfu->flush_medium)
311 ret = dfu->flush_medium(dfu);
312
Lukasz Majewskibd694242014-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 Schochera2199af2014-03-18 08:09:55 +0100316
Patrick Delaunay067c13c2019-10-14 09:28:07 +0200317 dfu_flush_callback(dfu);
318
Patrick Delaunay57da0602017-07-19 16:39:24 +0200319 dfu_transaction_cleanup(dfu);
Heiko Schochera2199af2014-03-18 08:09:55 +0100320
321 return ret;
322}
323
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000324int dfu_write(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200325{
Stephen Warren3ee95932014-06-11 12:48:08 -0600326 int ret;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200327
Stephen Warrene621c7a2015-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 Antoniouea2453d2013-03-14 05:32:48 +0000329 __func__, dfu->name, buf, size, blk_seq_num, dfu->offset,
Stephen Warrene621c7a2015-07-22 14:54:04 -0600330 (unsigned long)(dfu->i_buf - dfu->i_buf_start));
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200331
Patrick Delaunay6fa8ddd2017-07-19 16:39:25 +0200332 ret = dfu_transaction_initiate(dfu, false);
333 if (ret < 0)
334 return ret;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200335
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000336 if (dfu->i_blk_seq_num != blk_seq_num) {
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200337 printf("%s: Wrong sequence number! [%d] [%d]\n",
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000338 __func__, dfu->i_blk_seq_num, blk_seq_num);
Patrick Delaunay57da0602017-07-19 16:39:24 +0200339 dfu_transaction_cleanup(dfu);
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200340 return -1;
341 }
342
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000343 /* 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 */
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200355
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000356 /* 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 Warren3ee95932014-06-11 12:48:08 -0600361 ret = dfu_write_buffer_drain(dfu);
362 if (ret) {
Patrick Delaunay57da0602017-07-19 16:39:24 +0200363 dfu_transaction_cleanup(dfu);
Stephen Warren3ee95932014-06-11 12:48:08 -0600364 return ret;
365 }
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200366 }
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000367
368 /* we should be in buffer now (if not then size too large) */
369 if ((dfu->i_buf + size) > dfu->i_buf_end) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900370 pr_err("Buffer overflow! (0x%p + 0x%x > 0x%p)\n", dfu->i_buf,
Lukasz Majewskia7d2c3c2013-09-10 15:29:22 +0200371 size, dfu->i_buf_end);
Patrick Delaunay57da0602017-07-19 16:39:24 +0200372 dfu_transaction_cleanup(dfu);
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000373 return -1;
374 }
375
376 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 Warren3ee95932014-06-11 12:48:08 -0600381 ret = dfu_write_buffer_drain(dfu);
382 if (ret) {
Patrick Delaunay57da0602017-07-19 16:39:24 +0200383 dfu_transaction_cleanup(dfu);
Stephen Warren3ee95932014-06-11 12:48:08 -0600384 return ret;
385 }
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000386 }
387
Stephen Warren3ee95932014-06-11 12:48:08 -0600388 return 0;
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000389}
390
391static 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 Yamadab4141192014-11-07 03:03:31 +0900399 chunk = min((long)size, dfu->b_left);
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000400 /* consume */
401 if (chunk > 0) {
402 memcpy(buf, dfu->i_buf, chunk);
Lukasz Majewskibd694242014-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 Antoniouea2453d2013-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 Delaunay0de10222019-10-14 09:28:03 +0200433 if (dfu->b_left == 0)
434 break;
Pantelis Antoniouea2453d2013-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
445int dfu_read(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
446{
447 int ret = 0;
448
449 debug("%s: name: %s buf: 0x%p size: 0x%x p_num: 0x%x i_buf: 0x%p\n",
450 __func__, dfu->name, buf, size, blk_seq_num, dfu->i_buf);
451
Patrick Delaunay6fa8ddd2017-07-19 16:39:25 +0200452 ret = dfu_transaction_initiate(dfu, true);
453 if (ret < 0)
454 return ret;
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000455
456 if (dfu->i_blk_seq_num != blk_seq_num) {
457 printf("%s: Wrong sequence number! [%d] [%d]\n",
458 __func__, dfu->i_blk_seq_num, blk_seq_num);
459 return -1;
460 }
461 /* handle rollover */
462 dfu->i_blk_seq_num = (dfu->i_blk_seq_num + 1) & 0xffff;
463
464 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 Majewskibd694242014-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 Antoniouea2453d2013-03-14 05:32:48 +0000474 puts("\nUPLOAD ... done\nCtrl+C to exit ...\n");
475
Patrick Delaunay57da0602017-07-19 16:39:24 +0200476 dfu_transaction_cleanup(dfu);
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000477 }
478
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200479 return ret;
480}
481
482static int dfu_fill_entity(struct dfu_entity *dfu, char *s, int alt,
Stephen Warrendd648272014-06-11 16:03:33 -0600483 char *interface, char *devstr)
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200484{
485 char *st;
486
Stephen Warrendd648272014-06-11 16:03:33 -0600487 debug("%s: %s interface: %s dev: %s\n", __func__, s, interface, devstr);
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200488 st = strsep(&s, " ");
489 strcpy(dfu->name, st);
490
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200491 dfu->alt = alt;
Stephen Warren7ac1b412014-06-11 16:03:34 -0600492 dfu->max_buf_size = 0;
Stephen Warrencb7bd2e2014-06-11 16:03:35 -0600493 dfu->free_entity = NULL;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200494
495 /* Specific for mmc device */
496 if (strcmp(interface, "mmc") == 0) {
Stephen Warrendd648272014-06-11 16:03:33 -0600497 if (dfu_fill_entity_mmc(dfu, devstr, s))
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200498 return -1;
Patrick Delaunay6015af22019-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 Antoniouc6631762013-03-14 05:32:52 +0000502 } else if (strcmp(interface, "nand") == 0) {
Stephen Warrendd648272014-06-11 16:03:33 -0600503 if (dfu_fill_entity_nand(dfu, devstr, s))
Pantelis Antoniouc6631762013-03-14 05:32:52 +0000504 return -1;
Afzal Mohammeda9479f02013-09-18 01:15:24 +0530505 } else if (strcmp(interface, "ram") == 0) {
Stephen Warrendd648272014-06-11 16:03:33 -0600506 if (dfu_fill_entity_ram(dfu, devstr, s))
Afzal Mohammeda9479f02013-09-18 01:15:24 +0530507 return -1;
Stephen Warren6f12ebf2014-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 Delaunayec44cac2019-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 Majewskif22b11c2012-08-06 14:41:07 +0200514 } else {
515 printf("%s: Device %s not (yet) supported!\n",
516 __func__, interface);
517 return -1;
518 }
Stephen Warren806bd242015-09-04 22:03:46 -0600519 dfu_get_buf(dfu);
Lukasz Majewskif22b11c2012-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 Warren806bd242015-09-04 22:03:46 -0600528 dfu_free_buf();
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200529 list_for_each_entry_safe_reverse(dfu, p, &dfu_list, list) {
530 list_del(&dfu->list);
Stephen Warrencb7bd2e2014-06-11 16:03:35 -0600531 if (dfu->free_entity)
532 dfu->free_entity(dfu);
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200533 t = dfu;
534 }
535 if (t)
536 free(t);
537 INIT_LIST_HEAD(&dfu_list);
Przemyslaw Marczakb627eb42014-02-28 18:53:37 +0100538
539 alt_num_cnt = 0;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200540}
541
Patrick Delaunay9ada6832019-10-14 09:28:01 +0200542int dfu_alt_init(int num, struct dfu_entity **dfu)
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200543{
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200544 char *s;
Patrick Delaunay9ada6832019-10-14 09:28:01 +0200545 int ret;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200546
Patrick Delaunay9ada6832019-10-14 09:28:01 +0200547 dfu_alt_num = num;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200548 debug("%s: dfu_alt_num=%d\n", __func__, dfu_alt_num);
549
Lukasz Majewskibd694242014-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 Yamada9b643e32017-09-16 14:10:41 +0900555 pr_err("Hash algorithm %s not supported\n", s);
Lukasz Majewskibd694242014-05-12 10:43:36 +0200556 }
557
Patrick Delaunay9ada6832019-10-14 09:28:01 +0200558 *dfu = calloc(sizeof(struct dfu_entity), dfu_alt_num);
559 if (!*dfu)
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200560 return -1;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200561
Patrick Delaunay9ada6832019-10-14 09:28:01 +0200562 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++;
580
581 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 Majewskif22b11c2012-08-06 14:41:07 +0200595 s = strsep(&env, ";");
Patrick Delaunay9ada6832019-10-14 09:28:01 +0200596 ret = dfu_alt_add(dfu, interface, devstr, s);
Peng Fan5d8fae72016-05-03 10:24:52 +0800597 if (ret) {
Sam Protsenkofeaa7852018-07-13 16:35:45 +0300598 /* We will free "dfu" in dfu_free_entities() */
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200599 return -1;
Peng Fan5d8fae72016-05-03 10:24:52 +0800600 }
Lukasz Majewskif22b11c2012-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 Delaunay909b6902019-10-14 09:27:58 +0200608 const char *const dev_t[] = {NULL, "eMMC", "OneNAND", "NAND", "RAM",
Patrick Delaunayec44cac2019-10-14 09:28:06 +0200609 "SF", "MTD", "VIRT"};
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200610 return dev_t[t];
611}
612
613const char *dfu_get_layout(enum dfu_layout l)
614{
Patrick Delaunay909b6902019-10-14 09:27:58 +0200615 const char *const dfu_layout[] = {NULL, "RAW_ADDR", "FAT", "EXT2",
616 "EXT3", "EXT4", "RAM_ADDR" };
Lukasz Majewskif22b11c2012-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 Majewskifed936e2013-10-08 14:30:38 +0200649
650int dfu_get_alt(char *name)
651{
652 struct dfu_entity *dfu;
Lukasz Majewski5610b052014-11-05 10:54:16 +0100653 char *str;
Lukasz Majewskifed936e2013-10-08 14:30:38 +0200654
655 list_for_each_entry(dfu, &dfu_list, list) {
Lukasz Majewski5610b052014-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 Majewskifed936e2013-10-08 14:30:38 +0200682 }
683
684 return -ENODEV;
685}
Lukasz Majewski2092e462015-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 Yamada9b643e32017-09-16 14:10:41 +0900709 pr_err("DFU write failed\n");
Lukasz Majewski2092e462015-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 Yamada9b643e32017-09-16 14:10:41 +0900719 pr_err("DFU flush failed!");
Lukasz Majewski2092e462015-08-24 00:21:46 +0200720
721 return ret;
722}