blob: 38aecd3a0586f42bb9c132398efecc8897d63255 [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;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +020024
Lukasz Majewski1cc03c52014-08-25 11:07:28 +020025/*
Patrick Delaunay067c13c2019-10-14 09:28:07 +020026 * The purpose of the dfu_flush_callback() function is to
27 * provide callback for dfu user
28 */
29__weak void dfu_flush_callback(struct dfu_entity *dfu)
30{
31}
32
33/*
34 * The purpose of the dfu_initiated_callback() function is to
35 * provide callback for dfu user
36 */
37__weak void dfu_initiated_callback(struct dfu_entity *dfu)
38{
39}
40
41/*
Lukasz Majewski1cc03c52014-08-25 11:07:28 +020042 * The purpose of the dfu_usb_get_reset() function is to
43 * provide information if after USB_DETACH request
44 * being sent the dfu-util performed reset of USB
45 * bus.
46 *
47 * Described behaviour is the only way to distinct if
48 * user has typed -e (detach) or -R (reset) when invoking
49 * dfu-util command.
50 *
51 */
52__weak bool dfu_usb_get_reset(void)
Lukasz Majewski6bed7ce2013-07-18 13:19:14 +020053{
B, Ravi66928af2017-05-04 15:45:29 +053054#ifdef CONFIG_SPL_DFU_NO_RESET
55 return false;
56#else
Lukasz Majewski1cc03c52014-08-25 11:07:28 +020057 return true;
B, Ravi66928af2017-05-04 15:45:29 +053058#endif
Lukasz Majewski6bed7ce2013-07-18 13:19:14 +020059}
60
Lukasz Majewskif22b11c2012-08-06 14:41:07 +020061static int dfu_find_alt_num(const char *s)
62{
63 int i = 0;
64
65 for (; *s; s++)
66 if (*s == ';')
67 i++;
68
69 return ++i;
70}
71
Patrick Delaunayfebabe32019-10-14 09:28:02 +020072/*
73 * treat dfu_alt_info with several interface information
74 * to allow DFU on several device with one command,
75 * the string format is
76 * interface devstring'='alternate list (';' separated)
77 * and each interface separated by '&'
78 */
79int dfu_config_interfaces(char *env)
80{
81 struct dfu_entity *dfu;
82 char *s, *i, *d, *a, *part;
83 int ret = -EINVAL;
84 int n = 1;
85
86 s = env;
87 for (; *s; s++) {
88 if (*s == ';')
89 n++;
90 if (*s == '&')
91 n++;
92 }
93 ret = dfu_alt_init(n, &dfu);
94 if (ret)
95 return ret;
96
97 s = env;
98 while (s) {
99 ret = -EINVAL;
100 i = strsep(&s, " ");
101 if (!i)
102 break;
103 d = strsep(&s, "=");
104 if (!d)
105 break;
106 a = strsep(&s, "&");
107 if (!a)
108 a = s;
109 do {
110 part = strsep(&a, ";");
111 ret = dfu_alt_add(dfu, i, d, part);
112 if (ret)
113 return ret;
114 } while (a);
115 }
116
117 return ret;
118}
119
Stephen Warrendd648272014-06-11 16:03:33 -0600120int dfu_init_env_entities(char *interface, char *devstr)
Lukasz Majewski765c5ae2013-09-11 14:53:35 +0200121{
122 const char *str_env;
123 char *env_bkp;
Sam Protsenko87a8ca92018-07-13 16:35:46 +0300124 int ret = 0;
Lukasz Majewski765c5ae2013-09-11 14:53:35 +0200125
Przemyslaw Marczak899a5282015-02-17 12:24:11 +0100126#ifdef CONFIG_SET_DFU_ALT_INFO
127 set_dfu_alt_info(interface, devstr);
128#endif
Simon Glass00caae62017-08-03 12:22:12 -0600129 str_env = env_get("dfu_alt_info");
Lukasz Majewski765c5ae2013-09-11 14:53:35 +0200130 if (!str_env) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900131 pr_err("\"dfu_alt_info\" env variable not defined!\n");
Lukasz Majewski765c5ae2013-09-11 14:53:35 +0200132 return -EINVAL;
133 }
134
135 env_bkp = strdup(str_env);
Patrick Delaunayfebabe32019-10-14 09:28:02 +0200136 if (!interface && !devstr)
137 ret = dfu_config_interfaces(env_bkp);
138 else
139 ret = dfu_config_entities(env_bkp, interface, devstr);
140
Lukasz Majewski765c5ae2013-09-11 14:53:35 +0200141 if (ret) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900142 pr_err("DFU entities configuration failed!\n");
Sam Protsenko28a5c882018-07-13 16:35:47 +0300143 pr_err("(partition table does not match dfu_alt_info?)\n");
Sam Protsenko87a8ca92018-07-13 16:35:46 +0300144 goto done;
Lukasz Majewski765c5ae2013-09-11 14:53:35 +0200145 }
146
Sam Protsenko87a8ca92018-07-13 16:35:46 +0300147done:
Lukasz Majewski765c5ae2013-09-11 14:53:35 +0200148 free(env_bkp);
Sam Protsenko87a8ca92018-07-13 16:35:46 +0300149 return ret;
Lukasz Majewski765c5ae2013-09-11 14:53:35 +0200150}
151
Heiko Schochere7e75c72013-06-12 06:05:51 +0200152static unsigned char *dfu_buf;
Lukasz Majewskia7e68922015-07-08 23:43:18 +0200153static unsigned long dfu_buf_size;
Patrick Delaunayfebabe32019-10-14 09:28:02 +0200154static enum dfu_device_type dfu_buf_device_type;
Heiko Schochere7e75c72013-06-12 06:05:51 +0200155
Lukasz Majewskid4278262013-10-08 14:30:39 +0200156unsigned char *dfu_free_buf(void)
Heiko Schochere7e75c72013-06-12 06:05:51 +0200157{
158 free(dfu_buf);
159 dfu_buf = NULL;
160 return dfu_buf;
161}
162
Lukasz Majewski4fb12782013-12-09 16:20:13 +0100163unsigned long dfu_get_buf_size(void)
164{
165 return dfu_buf_size;
166}
167
Stephen Warren7ac1b412014-06-11 16:03:34 -0600168unsigned char *dfu_get_buf(struct dfu_entity *dfu)
Heiko Schochere7e75c72013-06-12 06:05:51 +0200169{
170 char *s;
171
Patrick Delaunayfebabe32019-10-14 09:28:02 +0200172 /* manage several entity with several contraint */
173 if (dfu_buf && dfu->dev_type != dfu_buf_device_type)
174 dfu_free_buf();
175
Heiko Schochere7e75c72013-06-12 06:05:51 +0200176 if (dfu_buf != NULL)
177 return dfu_buf;
178
Simon Glass00caae62017-08-03 12:22:12 -0600179 s = env_get("dfu_bufsiz");
Przemyslaw Marczakf597fc32014-12-15 10:34:11 +0100180 if (s)
181 dfu_buf_size = (unsigned long)simple_strtol(s, NULL, 0);
182
183 if (!s || !dfu_buf_size)
184 dfu_buf_size = CONFIG_SYS_DFU_DATA_BUF_SIZE;
185
Stephen Warren7ac1b412014-06-11 16:03:34 -0600186 if (dfu->max_buf_size && dfu_buf_size > dfu->max_buf_size)
187 dfu_buf_size = dfu->max_buf_size;
Heiko Schochere7e75c72013-06-12 06:05:51 +0200188
189 dfu_buf = memalign(CONFIG_SYS_CACHELINE_SIZE, dfu_buf_size);
190 if (dfu_buf == NULL)
191 printf("%s: Could not memalign 0x%lx bytes\n",
192 __func__, dfu_buf_size);
193
Patrick Delaunayfebabe32019-10-14 09:28:02 +0200194 dfu_buf_device_type = dfu->dev_type;
Heiko Schochere7e75c72013-06-12 06:05:51 +0200195 return dfu_buf;
196}
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200197
Lukasz Majewskibd694242014-05-12 10:43:36 +0200198static char *dfu_get_hash_algo(void)
199{
200 char *s;
201
Simon Glass00caae62017-08-03 12:22:12 -0600202 s = env_get("dfu_hash_algo");
Lukasz Majewski3d83e672014-06-10 12:25:59 +0200203 if (!s)
Lukasz Majewskibd694242014-05-12 10:43:36 +0200204 return NULL;
205
Lukasz Majewski3d83e672014-06-10 12:25:59 +0200206 if (!strcmp(s, "crc32")) {
207 debug("%s: DFU hash method: %s\n", __func__, s);
208 return s;
209 }
210
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900211 pr_err("DFU hash method: %s not supported!\n", s);
Lukasz Majewskibd694242014-05-12 10:43:36 +0200212 return NULL;
213}
214
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000215static int dfu_write_buffer_drain(struct dfu_entity *dfu)
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200216{
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000217 long w_size;
218 int ret;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200219
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000220 /* flush size? */
221 w_size = dfu->i_buf - dfu->i_buf_start;
222 if (w_size == 0)
223 return 0;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200224
Lukasz Majewskibd694242014-05-12 10:43:36 +0200225 if (dfu_hash_algo)
226 dfu_hash_algo->hash_update(dfu_hash_algo, &dfu->crc,
227 dfu->i_buf_start, w_size, 0);
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200228
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000229 ret = dfu->write_medium(dfu, dfu->offset, dfu->i_buf_start, &w_size);
230 if (ret)
231 debug("%s: Write error!\n", __func__);
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200232
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000233 /* point back */
234 dfu->i_buf = dfu->i_buf_start;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200235
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000236 /* update offset */
237 dfu->offset += w_size;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200238
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000239 puts("#");
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200240
241 return ret;
242}
243
Patrick Delaunay57da0602017-07-19 16:39:24 +0200244void dfu_transaction_cleanup(struct dfu_entity *dfu)
Stephen Warren3ee95932014-06-11 12:48:08 -0600245{
246 /* clear everything */
Stephen Warren3ee95932014-06-11 12:48:08 -0600247 dfu->crc = 0;
248 dfu->offset = 0;
249 dfu->i_blk_seq_num = 0;
Patrick Delaunay6fa8ddd2017-07-19 16:39:25 +0200250 dfu->i_buf_start = dfu_get_buf(dfu);
251 dfu->i_buf_end = dfu->i_buf_start;
Stephen Warren3ee95932014-06-11 12:48:08 -0600252 dfu->i_buf = dfu->i_buf_start;
Patrick Delaunay57da0602017-07-19 16:39:24 +0200253 dfu->r_left = 0;
254 dfu->b_left = 0;
255 dfu->bad_skip = 0;
256
Stephen Warren3ee95932014-06-11 12:48:08 -0600257 dfu->inited = 0;
258}
259
Patrick Delaunay6fa8ddd2017-07-19 16:39:25 +0200260int dfu_transaction_initiate(struct dfu_entity *dfu, bool read)
261{
262 int ret = 0;
263
264 if (dfu->inited)
265 return 0;
266
267 dfu_transaction_cleanup(dfu);
268
269 if (dfu->i_buf_start == NULL)
270 return -ENOMEM;
271
272 dfu->i_buf_end = dfu->i_buf_start + dfu_get_buf_size();
273
274 if (read) {
275 ret = dfu->get_medium_size(dfu, &dfu->r_left);
276 if (ret < 0)
277 return ret;
278 debug("%s: %s %lld [B]\n", __func__, dfu->name, dfu->r_left);
279 }
280
281 dfu->inited = 1;
Patrick Delaunay067c13c2019-10-14 09:28:07 +0200282 dfu_initiated_callback(dfu);
Patrick Delaunay6fa8ddd2017-07-19 16:39:25 +0200283
284 return 0;
285}
286
Heiko Schochera2199af2014-03-18 08:09:55 +0100287int dfu_flush(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
288{
289 int ret = 0;
290
Przemyslaw Marczak1aa4bdc2014-05-12 12:05:33 +0200291 ret = dfu_write_buffer_drain(dfu);
292 if (ret)
293 return ret;
294
Heiko Schochera2199af2014-03-18 08:09:55 +0100295 if (dfu->flush_medium)
296 ret = dfu->flush_medium(dfu);
297
Lukasz Majewskibd694242014-05-12 10:43:36 +0200298 if (dfu_hash_algo)
299 printf("\nDFU complete %s: 0x%08x\n", dfu_hash_algo->name,
300 dfu->crc);
Heiko Schochera2199af2014-03-18 08:09:55 +0100301
Patrick Delaunay067c13c2019-10-14 09:28:07 +0200302 dfu_flush_callback(dfu);
303
Patrick Delaunay57da0602017-07-19 16:39:24 +0200304 dfu_transaction_cleanup(dfu);
Heiko Schochera2199af2014-03-18 08:09:55 +0100305
306 return ret;
307}
308
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000309int dfu_write(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200310{
Stephen Warren3ee95932014-06-11 12:48:08 -0600311 int ret;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200312
Stephen Warrene621c7a2015-07-22 14:54:04 -0600313 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 +0000314 __func__, dfu->name, buf, size, blk_seq_num, dfu->offset,
Stephen Warrene621c7a2015-07-22 14:54:04 -0600315 (unsigned long)(dfu->i_buf - dfu->i_buf_start));
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200316
Patrick Delaunay6fa8ddd2017-07-19 16:39:25 +0200317 ret = dfu_transaction_initiate(dfu, false);
318 if (ret < 0)
319 return ret;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200320
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000321 if (dfu->i_blk_seq_num != blk_seq_num) {
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200322 printf("%s: Wrong sequence number! [%d] [%d]\n",
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000323 __func__, dfu->i_blk_seq_num, blk_seq_num);
Patrick Delaunay57da0602017-07-19 16:39:24 +0200324 dfu_transaction_cleanup(dfu);
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200325 return -1;
326 }
327
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000328 /* DFU 1.1 standard says:
329 * The wBlockNum field is a block sequence number. It increments each
330 * time a block is transferred, wrapping to zero from 65,535. It is used
331 * to provide useful context to the DFU loader in the device."
332 *
333 * This means that it's a 16 bit counter that roll-overs at
334 * 0xffff -> 0x0000. By having a typical 4K transfer block
335 * we roll-over at exactly 256MB. Not very fun to debug.
336 *
337 * Handling rollover, and having an inited variable,
338 * makes things work.
339 */
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200340
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000341 /* handle rollover */
342 dfu->i_blk_seq_num = (dfu->i_blk_seq_num + 1) & 0xffff;
343
344 /* flush buffer if overflow */
345 if ((dfu->i_buf + size) > dfu->i_buf_end) {
Stephen Warren3ee95932014-06-11 12:48:08 -0600346 ret = dfu_write_buffer_drain(dfu);
347 if (ret) {
Patrick Delaunay57da0602017-07-19 16:39:24 +0200348 dfu_transaction_cleanup(dfu);
Stephen Warren3ee95932014-06-11 12:48:08 -0600349 return ret;
350 }
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200351 }
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000352
353 /* we should be in buffer now (if not then size too large) */
354 if ((dfu->i_buf + size) > dfu->i_buf_end) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900355 pr_err("Buffer overflow! (0x%p + 0x%x > 0x%p)\n", dfu->i_buf,
Lukasz Majewskia7d2c3c2013-09-10 15:29:22 +0200356 size, dfu->i_buf_end);
Patrick Delaunay57da0602017-07-19 16:39:24 +0200357 dfu_transaction_cleanup(dfu);
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000358 return -1;
359 }
360
361 memcpy(dfu->i_buf, buf, size);
362 dfu->i_buf += size;
363
364 /* if end or if buffer full flush */
365 if (size == 0 || (dfu->i_buf + size) > dfu->i_buf_end) {
Stephen Warren3ee95932014-06-11 12:48:08 -0600366 ret = dfu_write_buffer_drain(dfu);
367 if (ret) {
Patrick Delaunay57da0602017-07-19 16:39:24 +0200368 dfu_transaction_cleanup(dfu);
Stephen Warren3ee95932014-06-11 12:48:08 -0600369 return ret;
370 }
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000371 }
372
Stephen Warren3ee95932014-06-11 12:48:08 -0600373 return 0;
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000374}
375
376static int dfu_read_buffer_fill(struct dfu_entity *dfu, void *buf, int size)
377{
378 long chunk;
379 int ret, readn;
380
381 readn = 0;
382 while (size > 0) {
383 /* get chunk that can be read */
Masahiro Yamadab4141192014-11-07 03:03:31 +0900384 chunk = min((long)size, dfu->b_left);
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000385 /* consume */
386 if (chunk > 0) {
387 memcpy(buf, dfu->i_buf, chunk);
Lukasz Majewskibd694242014-05-12 10:43:36 +0200388 if (dfu_hash_algo)
389 dfu_hash_algo->hash_update(dfu_hash_algo,
390 &dfu->crc, buf,
391 chunk, 0);
392
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000393 dfu->i_buf += chunk;
394 dfu->b_left -= chunk;
395 size -= chunk;
396 buf += chunk;
397 readn += chunk;
398 }
399
400 /* all done */
401 if (size > 0) {
402 /* no more to read */
403 if (dfu->r_left == 0)
404 break;
405
406 dfu->i_buf = dfu->i_buf_start;
407 dfu->b_left = dfu->i_buf_end - dfu->i_buf_start;
408
409 /* got to read, but buffer is empty */
410 if (dfu->b_left > dfu->r_left)
411 dfu->b_left = dfu->r_left;
412 ret = dfu->read_medium(dfu, dfu->offset, dfu->i_buf,
413 &dfu->b_left);
414 if (ret != 0) {
415 debug("%s: Read error!\n", __func__);
416 return ret;
417 }
Patrick Delaunay0de10222019-10-14 09:28:03 +0200418 if (dfu->b_left == 0)
419 break;
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000420 dfu->offset += dfu->b_left;
421 dfu->r_left -= dfu->b_left;
422
423 puts("#");
424 }
425 }
426
427 return readn;
428}
429
430int dfu_read(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
431{
432 int ret = 0;
433
434 debug("%s: name: %s buf: 0x%p size: 0x%x p_num: 0x%x i_buf: 0x%p\n",
435 __func__, dfu->name, buf, size, blk_seq_num, dfu->i_buf);
436
Patrick Delaunay6fa8ddd2017-07-19 16:39:25 +0200437 ret = dfu_transaction_initiate(dfu, true);
438 if (ret < 0)
439 return ret;
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000440
441 if (dfu->i_blk_seq_num != blk_seq_num) {
442 printf("%s: Wrong sequence number! [%d] [%d]\n",
443 __func__, dfu->i_blk_seq_num, blk_seq_num);
444 return -1;
445 }
446 /* handle rollover */
447 dfu->i_blk_seq_num = (dfu->i_blk_seq_num + 1) & 0xffff;
448
449 ret = dfu_read_buffer_fill(dfu, buf, size);
450 if (ret < 0) {
451 printf("%s: Failed to fill buffer\n", __func__);
452 return -1;
453 }
454
455 if (ret < size) {
Lukasz Majewskibd694242014-05-12 10:43:36 +0200456 if (dfu_hash_algo)
457 debug("%s: %s %s: 0x%x\n", __func__, dfu->name,
458 dfu_hash_algo->name, dfu->crc);
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000459 puts("\nUPLOAD ... done\nCtrl+C to exit ...\n");
460
Patrick Delaunay57da0602017-07-19 16:39:24 +0200461 dfu_transaction_cleanup(dfu);
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000462 }
463
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200464 return ret;
465}
466
467static int dfu_fill_entity(struct dfu_entity *dfu, char *s, int alt,
Stephen Warrendd648272014-06-11 16:03:33 -0600468 char *interface, char *devstr)
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200469{
470 char *st;
471
Stephen Warrendd648272014-06-11 16:03:33 -0600472 debug("%s: %s interface: %s dev: %s\n", __func__, s, interface, devstr);
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200473 st = strsep(&s, " ");
474 strcpy(dfu->name, st);
475
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200476 dfu->alt = alt;
Stephen Warren7ac1b412014-06-11 16:03:34 -0600477 dfu->max_buf_size = 0;
Stephen Warrencb7bd2e2014-06-11 16:03:35 -0600478 dfu->free_entity = NULL;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200479
480 /* Specific for mmc device */
481 if (strcmp(interface, "mmc") == 0) {
Stephen Warrendd648272014-06-11 16:03:33 -0600482 if (dfu_fill_entity_mmc(dfu, devstr, s))
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200483 return -1;
Patrick Delaunay6015af22019-10-14 09:28:04 +0200484 } else if (strcmp(interface, "mtd") == 0) {
485 if (dfu_fill_entity_mtd(dfu, devstr, s))
486 return -1;
Pantelis Antoniouc6631762013-03-14 05:32:52 +0000487 } else if (strcmp(interface, "nand") == 0) {
Stephen Warrendd648272014-06-11 16:03:33 -0600488 if (dfu_fill_entity_nand(dfu, devstr, s))
Pantelis Antoniouc6631762013-03-14 05:32:52 +0000489 return -1;
Afzal Mohammeda9479f02013-09-18 01:15:24 +0530490 } else if (strcmp(interface, "ram") == 0) {
Stephen Warrendd648272014-06-11 16:03:33 -0600491 if (dfu_fill_entity_ram(dfu, devstr, s))
Afzal Mohammeda9479f02013-09-18 01:15:24 +0530492 return -1;
Stephen Warren6f12ebf2014-06-11 16:03:36 -0600493 } else if (strcmp(interface, "sf") == 0) {
494 if (dfu_fill_entity_sf(dfu, devstr, s))
495 return -1;
Patrick Delaunayec44cac2019-10-14 09:28:06 +0200496 } else if (strcmp(interface, "virt") == 0) {
497 if (dfu_fill_entity_virt(dfu, devstr, s))
498 return -1;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200499 } else {
500 printf("%s: Device %s not (yet) supported!\n",
501 __func__, interface);
502 return -1;
503 }
Stephen Warren806bd242015-09-04 22:03:46 -0600504 dfu_get_buf(dfu);
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200505
506 return 0;
507}
508
509void dfu_free_entities(void)
510{
511 struct dfu_entity *dfu, *p, *t = NULL;
512
Stephen Warren806bd242015-09-04 22:03:46 -0600513 dfu_free_buf();
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200514 list_for_each_entry_safe_reverse(dfu, p, &dfu_list, list) {
515 list_del(&dfu->list);
Stephen Warrencb7bd2e2014-06-11 16:03:35 -0600516 if (dfu->free_entity)
517 dfu->free_entity(dfu);
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200518 t = dfu;
519 }
520 if (t)
521 free(t);
522 INIT_LIST_HEAD(&dfu_list);
Przemyslaw Marczakb627eb42014-02-28 18:53:37 +0100523
524 alt_num_cnt = 0;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200525}
526
Patrick Delaunay9ada6832019-10-14 09:28:01 +0200527int dfu_alt_init(int num, struct dfu_entity **dfu)
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200528{
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200529 char *s;
Patrick Delaunay9ada6832019-10-14 09:28:01 +0200530 int ret;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200531
Patrick Delaunay9ada6832019-10-14 09:28:01 +0200532 dfu_alt_num = num;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200533 debug("%s: dfu_alt_num=%d\n", __func__, dfu_alt_num);
534
Lukasz Majewskibd694242014-05-12 10:43:36 +0200535 dfu_hash_algo = NULL;
536 s = dfu_get_hash_algo();
537 if (s) {
538 ret = hash_lookup_algo(s, &dfu_hash_algo);
539 if (ret)
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900540 pr_err("Hash algorithm %s not supported\n", s);
Lukasz Majewskibd694242014-05-12 10:43:36 +0200541 }
542
Patrick Delaunay9ada6832019-10-14 09:28:01 +0200543 *dfu = calloc(sizeof(struct dfu_entity), dfu_alt_num);
544 if (!*dfu)
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200545 return -1;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200546
Patrick Delaunay9ada6832019-10-14 09:28:01 +0200547 return 0;
548}
549
550int dfu_alt_add(struct dfu_entity *dfu, char *interface, char *devstr, char *s)
551{
552 struct dfu_entity *p_dfu;
553 int ret;
554
555 if (alt_num_cnt >= dfu_alt_num)
556 return -1;
557
558 p_dfu = &dfu[alt_num_cnt];
559 ret = dfu_fill_entity(p_dfu, s, alt_num_cnt, interface, devstr);
560 if (ret)
561 return -1;
562
563 list_add_tail(&p_dfu->list, &dfu_list);
564 alt_num_cnt++;
565
566 return 0;
567}
568
569int dfu_config_entities(char *env, char *interface, char *devstr)
570{
571 struct dfu_entity *dfu;
572 int i, ret;
573 char *s;
574
575 ret = dfu_alt_init(dfu_find_alt_num(env), &dfu);
576 if (ret)
577 return -1;
578
579 for (i = 0; i < dfu_alt_num; i++) {
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200580 s = strsep(&env, ";");
Patrick Delaunay9ada6832019-10-14 09:28:01 +0200581 ret = dfu_alt_add(dfu, interface, devstr, s);
Peng Fan5d8fae72016-05-03 10:24:52 +0800582 if (ret) {
Sam Protsenkofeaa7852018-07-13 16:35:45 +0300583 /* We will free "dfu" in dfu_free_entities() */
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200584 return -1;
Peng Fan5d8fae72016-05-03 10:24:52 +0800585 }
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200586 }
587
588 return 0;
589}
590
591const char *dfu_get_dev_type(enum dfu_device_type t)
592{
Patrick Delaunay909b6902019-10-14 09:27:58 +0200593 const char *const dev_t[] = {NULL, "eMMC", "OneNAND", "NAND", "RAM",
Patrick Delaunayec44cac2019-10-14 09:28:06 +0200594 "SF", "MTD", "VIRT"};
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200595 return dev_t[t];
596}
597
598const char *dfu_get_layout(enum dfu_layout l)
599{
Patrick Delaunay909b6902019-10-14 09:27:58 +0200600 const char *const dfu_layout[] = {NULL, "RAW_ADDR", "FAT", "EXT2",
601 "EXT3", "EXT4", "RAM_ADDR" };
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200602 return dfu_layout[l];
603}
604
605void dfu_show_entities(void)
606{
607 struct dfu_entity *dfu;
608
609 puts("DFU alt settings list:\n");
610
611 list_for_each_entry(dfu, &dfu_list, list) {
612 printf("dev: %s alt: %d name: %s layout: %s\n",
613 dfu_get_dev_type(dfu->dev_type), dfu->alt,
614 dfu->name, dfu_get_layout(dfu->layout));
615 }
616}
617
618int dfu_get_alt_number(void)
619{
620 return dfu_alt_num;
621}
622
623struct dfu_entity *dfu_get_entity(int alt)
624{
625 struct dfu_entity *dfu;
626
627 list_for_each_entry(dfu, &dfu_list, list) {
628 if (dfu->alt == alt)
629 return dfu;
630 }
631
632 return NULL;
633}
Lukasz Majewskifed936e2013-10-08 14:30:38 +0200634
635int dfu_get_alt(char *name)
636{
637 struct dfu_entity *dfu;
Lukasz Majewski5610b052014-11-05 10:54:16 +0100638 char *str;
Lukasz Majewskifed936e2013-10-08 14:30:38 +0200639
640 list_for_each_entry(dfu, &dfu_list, list) {
Lukasz Majewski5610b052014-11-05 10:54:16 +0100641 if (dfu->name[0] != '/') {
642 if (!strncmp(dfu->name, name, strlen(dfu->name)))
643 return dfu->alt;
644 } else {
645 /*
646 * One must also consider absolute path
647 * (/boot/bin/uImage) available at dfu->name when
648 * compared "plain" file name (uImage)
649 *
650 * It is the case for e.g. thor gadget where lthor SW
651 * sends only the file name, so only the very last part
652 * of path must be checked for equality
653 */
654
655 str = strstr(dfu->name, name);
656 if (!str)
657 continue;
658
659 /*
660 * Check if matching substring is the last element of
661 * dfu->name (uImage)
662 */
663 if (strlen(dfu->name) ==
664 ((str - dfu->name) + strlen(name)))
665 return dfu->alt;
666 }
Lukasz Majewskifed936e2013-10-08 14:30:38 +0200667 }
668
669 return -ENODEV;
670}
Lukasz Majewski2092e462015-08-24 00:21:46 +0200671
672int dfu_write_from_mem_addr(struct dfu_entity *dfu, void *buf, int size)
673{
674 unsigned long dfu_buf_size, write, left = size;
675 int i, ret = 0;
676 void *dp = buf;
677
678 /*
679 * Here we must call dfu_get_buf(dfu) first to be sure that dfu_buf_size
680 * has been properly initialized - e.g. if "dfu_bufsiz" has been taken
681 * into account.
682 */
683 dfu_get_buf(dfu);
684 dfu_buf_size = dfu_get_buf_size();
685 debug("%s: dfu buf size: %lu\n", __func__, dfu_buf_size);
686
687 for (i = 0; left > 0; i++) {
688 write = min(dfu_buf_size, left);
689
690 debug("%s: dp: 0x%p left: %lu write: %lu\n", __func__,
691 dp, left, write);
692 ret = dfu_write(dfu, dp, write, i);
693 if (ret) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900694 pr_err("DFU write failed\n");
Lukasz Majewski2092e462015-08-24 00:21:46 +0200695 return ret;
696 }
697
698 dp += write;
699 left -= write;
700 }
701
702 ret = dfu_flush(dfu, NULL, 0, i);
703 if (ret)
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900704 pr_err("DFU flush failed!");
Lukasz Majewski2092e462015-08-24 00:21:46 +0200705
706 return ret;
707}