blob: ff1859d946387ec5c8f4d2e7601dde8c522b482d [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>
Simon Glassf7ae49f2020-05-10 11:40:05 -060012#include <log.h>
Lukasz Majewskif22b11c2012-08-06 14:41:07 +020013#include <malloc.h>
14#include <mmc.h>
15#include <fat.h>
16#include <dfu.h>
Lukasz Majewskibd694242014-05-12 10:43:36 +020017#include <hash.h>
Lukasz Majewskif22b11c2012-08-06 14:41:07 +020018#include <linux/list.h>
19#include <linux/compiler.h>
20
AKASHI Takahiro6beaa472020-10-29 13:47:44 +090021LIST_HEAD(dfu_list);
Lukasz Majewskif22b11c2012-08-06 14:41:07 +020022static int dfu_alt_num;
Przemyslaw Marczakb627eb42014-02-28 18:53:37 +010023static int alt_num_cnt;
Lukasz Majewskibd694242014-05-12 10:43:36 +020024static struct hash_algo *dfu_hash_algo;
Andy Shevchenko98a8f442019-11-27 18:12:15 +020025#ifdef CONFIG_DFU_TIMEOUT
26static unsigned long dfu_timeout = 0;
27#endif
Lukasz Majewskif22b11c2012-08-06 14:41:07 +020028
Marek Szyprowskic533f942020-12-22 11:32:22 +010029bool dfu_reinit_needed = false;
30
Lukasz Majewski1cc03c52014-08-25 11:07:28 +020031/*
Patrick Delaunay067c13c2019-10-14 09:28:07 +020032 * The purpose of the dfu_flush_callback() function is to
33 * provide callback for dfu user
34 */
35__weak void dfu_flush_callback(struct dfu_entity *dfu)
36{
37}
38
39/*
40 * The purpose of the dfu_initiated_callback() function is to
41 * provide callback for dfu user
42 */
43__weak void dfu_initiated_callback(struct dfu_entity *dfu)
44{
45}
46
47/*
Patrick Delaunayd4710322021-05-18 15:12:12 +020048 * The purpose of the dfu_error_callback() function is to
49 * provide callback for dfu user
50 */
51__weak void dfu_error_callback(struct dfu_entity *dfu, const char *msg)
52{
53}
54
55/*
Lukasz Majewski1cc03c52014-08-25 11:07:28 +020056 * The purpose of the dfu_usb_get_reset() function is to
57 * provide information if after USB_DETACH request
58 * being sent the dfu-util performed reset of USB
59 * bus.
60 *
61 * Described behaviour is the only way to distinct if
62 * user has typed -e (detach) or -R (reset) when invoking
63 * dfu-util command.
64 *
65 */
66__weak bool dfu_usb_get_reset(void)
Lukasz Majewski6bed7ce2013-07-18 13:19:14 +020067{
B, Ravi66928af2017-05-04 15:45:29 +053068#ifdef CONFIG_SPL_DFU_NO_RESET
69 return false;
70#else
Lukasz Majewski1cc03c52014-08-25 11:07:28 +020071 return true;
B, Ravi66928af2017-05-04 15:45:29 +053072#endif
Lukasz Majewski6bed7ce2013-07-18 13:19:14 +020073}
74
Andy Shevchenko98a8f442019-11-27 18:12:15 +020075#ifdef CONFIG_DFU_TIMEOUT
76void dfu_set_timeout(unsigned long timeout)
77{
78 dfu_timeout = timeout;
79}
80
81unsigned long dfu_get_timeout(void)
82{
83 return dfu_timeout;
84}
85#endif
86
Lukasz Majewskif22b11c2012-08-06 14:41:07 +020087static int dfu_find_alt_num(const char *s)
88{
89 int i = 0;
90
91 for (; *s; s++)
92 if (*s == ';')
93 i++;
94
95 return ++i;
96}
97
Patrick Delaunayfebabe32019-10-14 09:28:02 +020098/*
99 * treat dfu_alt_info with several interface information
100 * to allow DFU on several device with one command,
101 * the string format is
102 * interface devstring'='alternate list (';' separated)
103 * and each interface separated by '&'
104 */
105int dfu_config_interfaces(char *env)
106{
107 struct dfu_entity *dfu;
108 char *s, *i, *d, *a, *part;
109 int ret = -EINVAL;
110 int n = 1;
111
112 s = env;
113 for (; *s; s++) {
114 if (*s == ';')
115 n++;
116 if (*s == '&')
117 n++;
118 }
119 ret = dfu_alt_init(n, &dfu);
120 if (ret)
121 return ret;
122
123 s = env;
124 while (s) {
125 ret = -EINVAL;
126 i = strsep(&s, " ");
127 if (!i)
128 break;
129 d = strsep(&s, "=");
130 if (!d)
131 break;
132 a = strsep(&s, "&");
133 if (!a)
134 a = s;
135 do {
136 part = strsep(&a, ";");
137 ret = dfu_alt_add(dfu, i, d, part);
138 if (ret)
139 return ret;
140 } while (a);
141 }
142
143 return ret;
144}
145
Stephen Warrendd648272014-06-11 16:03:33 -0600146int dfu_init_env_entities(char *interface, char *devstr)
Lukasz Majewski765c5ae2013-09-11 14:53:35 +0200147{
148 const char *str_env;
149 char *env_bkp;
Sam Protsenko87a8ca92018-07-13 16:35:46 +0300150 int ret = 0;
Lukasz Majewski765c5ae2013-09-11 14:53:35 +0200151
Marek Szyprowskic533f942020-12-22 11:32:22 +0100152 dfu_reinit_needed = false;
153
Przemyslaw Marczak899a5282015-02-17 12:24:11 +0100154#ifdef CONFIG_SET_DFU_ALT_INFO
155 set_dfu_alt_info(interface, devstr);
156#endif
Simon Glass00caae62017-08-03 12:22:12 -0600157 str_env = env_get("dfu_alt_info");
Lukasz Majewski765c5ae2013-09-11 14:53:35 +0200158 if (!str_env) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900159 pr_err("\"dfu_alt_info\" env variable not defined!\n");
Lukasz Majewski765c5ae2013-09-11 14:53:35 +0200160 return -EINVAL;
161 }
162
163 env_bkp = strdup(str_env);
Patrick Delaunayfebabe32019-10-14 09:28:02 +0200164 if (!interface && !devstr)
165 ret = dfu_config_interfaces(env_bkp);
166 else
167 ret = dfu_config_entities(env_bkp, interface, devstr);
168
Lukasz Majewski765c5ae2013-09-11 14:53:35 +0200169 if (ret) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900170 pr_err("DFU entities configuration failed!\n");
Sam Protsenko28a5c882018-07-13 16:35:47 +0300171 pr_err("(partition table does not match dfu_alt_info?)\n");
Sam Protsenko87a8ca92018-07-13 16:35:46 +0300172 goto done;
Lukasz Majewski765c5ae2013-09-11 14:53:35 +0200173 }
174
Sam Protsenko87a8ca92018-07-13 16:35:46 +0300175done:
Lukasz Majewski765c5ae2013-09-11 14:53:35 +0200176 free(env_bkp);
Sam Protsenko87a8ca92018-07-13 16:35:46 +0300177 return ret;
Lukasz Majewski765c5ae2013-09-11 14:53:35 +0200178}
179
Heiko Schochere7e75c72013-06-12 06:05:51 +0200180static unsigned char *dfu_buf;
Lukasz Majewskia7e68922015-07-08 23:43:18 +0200181static unsigned long dfu_buf_size;
Patrick Delaunayfebabe32019-10-14 09:28:02 +0200182static enum dfu_device_type dfu_buf_device_type;
Heiko Schochere7e75c72013-06-12 06:05:51 +0200183
Lukasz Majewskid4278262013-10-08 14:30:39 +0200184unsigned char *dfu_free_buf(void)
Heiko Schochere7e75c72013-06-12 06:05:51 +0200185{
186 free(dfu_buf);
187 dfu_buf = NULL;
188 return dfu_buf;
189}
190
Lukasz Majewski4fb12782013-12-09 16:20:13 +0100191unsigned long dfu_get_buf_size(void)
192{
193 return dfu_buf_size;
194}
195
Stephen Warren7ac1b412014-06-11 16:03:34 -0600196unsigned char *dfu_get_buf(struct dfu_entity *dfu)
Heiko Schochere7e75c72013-06-12 06:05:51 +0200197{
198 char *s;
199
Patrick Delaunayfebabe32019-10-14 09:28:02 +0200200 /* manage several entity with several contraint */
201 if (dfu_buf && dfu->dev_type != dfu_buf_device_type)
202 dfu_free_buf();
203
Heiko Schochere7e75c72013-06-12 06:05:51 +0200204 if (dfu_buf != NULL)
205 return dfu_buf;
206
Simon Glass00caae62017-08-03 12:22:12 -0600207 s = env_get("dfu_bufsiz");
Przemyslaw Marczakf597fc32014-12-15 10:34:11 +0100208 if (s)
209 dfu_buf_size = (unsigned long)simple_strtol(s, NULL, 0);
210
211 if (!s || !dfu_buf_size)
212 dfu_buf_size = CONFIG_SYS_DFU_DATA_BUF_SIZE;
213
Stephen Warren7ac1b412014-06-11 16:03:34 -0600214 if (dfu->max_buf_size && dfu_buf_size > dfu->max_buf_size)
215 dfu_buf_size = dfu->max_buf_size;
Heiko Schochere7e75c72013-06-12 06:05:51 +0200216
217 dfu_buf = memalign(CONFIG_SYS_CACHELINE_SIZE, dfu_buf_size);
218 if (dfu_buf == NULL)
219 printf("%s: Could not memalign 0x%lx bytes\n",
220 __func__, dfu_buf_size);
221
Patrick Delaunayfebabe32019-10-14 09:28:02 +0200222 dfu_buf_device_type = dfu->dev_type;
Heiko Schochere7e75c72013-06-12 06:05:51 +0200223 return dfu_buf;
224}
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200225
Lukasz Majewskibd694242014-05-12 10:43:36 +0200226static char *dfu_get_hash_algo(void)
227{
228 char *s;
229
Simon Glass00caae62017-08-03 12:22:12 -0600230 s = env_get("dfu_hash_algo");
Lukasz Majewski3d83e672014-06-10 12:25:59 +0200231 if (!s)
Lukasz Majewskibd694242014-05-12 10:43:36 +0200232 return NULL;
233
Lukasz Majewski3d83e672014-06-10 12:25:59 +0200234 if (!strcmp(s, "crc32")) {
235 debug("%s: DFU hash method: %s\n", __func__, s);
236 return s;
237 }
238
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900239 pr_err("DFU hash method: %s not supported!\n", s);
Lukasz Majewskibd694242014-05-12 10:43:36 +0200240 return NULL;
241}
242
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000243static int dfu_write_buffer_drain(struct dfu_entity *dfu)
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200244{
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000245 long w_size;
246 int ret;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200247
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000248 /* flush size? */
249 w_size = dfu->i_buf - dfu->i_buf_start;
250 if (w_size == 0)
251 return 0;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200252
Lukasz Majewskibd694242014-05-12 10:43:36 +0200253 if (dfu_hash_algo)
254 dfu_hash_algo->hash_update(dfu_hash_algo, &dfu->crc,
255 dfu->i_buf_start, w_size, 0);
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200256
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000257 ret = dfu->write_medium(dfu, dfu->offset, dfu->i_buf_start, &w_size);
258 if (ret)
259 debug("%s: Write error!\n", __func__);
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200260
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000261 /* point back */
262 dfu->i_buf = dfu->i_buf_start;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200263
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000264 /* update offset */
265 dfu->offset += w_size;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200266
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000267 puts("#");
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200268
269 return ret;
270}
271
Patrick Delaunay57da0602017-07-19 16:39:24 +0200272void dfu_transaction_cleanup(struct dfu_entity *dfu)
Stephen Warren3ee95932014-06-11 12:48:08 -0600273{
274 /* clear everything */
Stephen Warren3ee95932014-06-11 12:48:08 -0600275 dfu->crc = 0;
276 dfu->offset = 0;
277 dfu->i_blk_seq_num = 0;
Patrick Delaunay6fa8ddd2017-07-19 16:39:25 +0200278 dfu->i_buf_start = dfu_get_buf(dfu);
279 dfu->i_buf_end = dfu->i_buf_start;
Stephen Warren3ee95932014-06-11 12:48:08 -0600280 dfu->i_buf = dfu->i_buf_start;
Patrick Delaunay57da0602017-07-19 16:39:24 +0200281 dfu->r_left = 0;
282 dfu->b_left = 0;
283 dfu->bad_skip = 0;
284
Stephen Warren3ee95932014-06-11 12:48:08 -0600285 dfu->inited = 0;
286}
287
Patrick Delaunay6fa8ddd2017-07-19 16:39:25 +0200288int dfu_transaction_initiate(struct dfu_entity *dfu, bool read)
289{
290 int ret = 0;
291
292 if (dfu->inited)
293 return 0;
294
295 dfu_transaction_cleanup(dfu);
296
297 if (dfu->i_buf_start == NULL)
298 return -ENOMEM;
299
300 dfu->i_buf_end = dfu->i_buf_start + dfu_get_buf_size();
301
302 if (read) {
303 ret = dfu->get_medium_size(dfu, &dfu->r_left);
304 if (ret < 0)
305 return ret;
306 debug("%s: %s %lld [B]\n", __func__, dfu->name, dfu->r_left);
307 }
308
309 dfu->inited = 1;
Patrick Delaunay067c13c2019-10-14 09:28:07 +0200310 dfu_initiated_callback(dfu);
Patrick Delaunay6fa8ddd2017-07-19 16:39:25 +0200311
312 return 0;
313}
314
Heiko Schochera2199af2014-03-18 08:09:55 +0100315int dfu_flush(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
316{
317 int ret = 0;
318
Przemyslaw Marczak1aa4bdc2014-05-12 12:05:33 +0200319 ret = dfu_write_buffer_drain(dfu);
320 if (ret)
321 return ret;
322
Heiko Schochera2199af2014-03-18 08:09:55 +0100323 if (dfu->flush_medium)
324 ret = dfu->flush_medium(dfu);
325
Lukasz Majewskibd694242014-05-12 10:43:36 +0200326 if (dfu_hash_algo)
327 printf("\nDFU complete %s: 0x%08x\n", dfu_hash_algo->name,
328 dfu->crc);
Heiko Schochera2199af2014-03-18 08:09:55 +0100329
Patrick Delaunay067c13c2019-10-14 09:28:07 +0200330 dfu_flush_callback(dfu);
331
Patrick Delaunay57da0602017-07-19 16:39:24 +0200332 dfu_transaction_cleanup(dfu);
Heiko Schochera2199af2014-03-18 08:09:55 +0100333
334 return ret;
335}
336
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000337int dfu_write(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200338{
Stephen Warren3ee95932014-06-11 12:48:08 -0600339 int ret;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200340
Stephen Warrene621c7a2015-07-22 14:54:04 -0600341 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 +0000342 __func__, dfu->name, buf, size, blk_seq_num, dfu->offset,
Stephen Warrene621c7a2015-07-22 14:54:04 -0600343 (unsigned long)(dfu->i_buf - dfu->i_buf_start));
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200344
Patrick Delaunay6fa8ddd2017-07-19 16:39:25 +0200345 ret = dfu_transaction_initiate(dfu, false);
346 if (ret < 0)
347 return ret;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200348
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000349 if (dfu->i_blk_seq_num != blk_seq_num) {
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200350 printf("%s: Wrong sequence number! [%d] [%d]\n",
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000351 __func__, dfu->i_blk_seq_num, blk_seq_num);
Patrick Delaunay57da0602017-07-19 16:39:24 +0200352 dfu_transaction_cleanup(dfu);
Patrick Delaunayd4710322021-05-18 15:12:12 +0200353 dfu_error_callback(dfu, "Wrong sequence number");
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200354 return -1;
355 }
356
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000357 /* DFU 1.1 standard says:
358 * The wBlockNum field is a block sequence number. It increments each
359 * time a block is transferred, wrapping to zero from 65,535. It is used
360 * to provide useful context to the DFU loader in the device."
361 *
362 * This means that it's a 16 bit counter that roll-overs at
363 * 0xffff -> 0x0000. By having a typical 4K transfer block
364 * we roll-over at exactly 256MB. Not very fun to debug.
365 *
366 * Handling rollover, and having an inited variable,
367 * makes things work.
368 */
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200369
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000370 /* handle rollover */
371 dfu->i_blk_seq_num = (dfu->i_blk_seq_num + 1) & 0xffff;
372
373 /* flush buffer if overflow */
374 if ((dfu->i_buf + size) > dfu->i_buf_end) {
Stephen Warren3ee95932014-06-11 12:48:08 -0600375 ret = dfu_write_buffer_drain(dfu);
376 if (ret) {
Patrick Delaunay57da0602017-07-19 16:39:24 +0200377 dfu_transaction_cleanup(dfu);
Patrick Delaunayd4710322021-05-18 15:12:12 +0200378 dfu_error_callback(dfu, "DFU write error");
Stephen Warren3ee95932014-06-11 12:48:08 -0600379 return ret;
380 }
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200381 }
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000382
383 /* we should be in buffer now (if not then size too large) */
384 if ((dfu->i_buf + size) > dfu->i_buf_end) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900385 pr_err("Buffer overflow! (0x%p + 0x%x > 0x%p)\n", dfu->i_buf,
Lukasz Majewskia7d2c3c2013-09-10 15:29:22 +0200386 size, dfu->i_buf_end);
Patrick Delaunay57da0602017-07-19 16:39:24 +0200387 dfu_transaction_cleanup(dfu);
Patrick Delaunayd4710322021-05-18 15:12:12 +0200388 dfu_error_callback(dfu, "Buffer overflow");
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000389 return -1;
390 }
391
392 memcpy(dfu->i_buf, buf, size);
393 dfu->i_buf += size;
394
395 /* if end or if buffer full flush */
396 if (size == 0 || (dfu->i_buf + size) > dfu->i_buf_end) {
Stephen Warren3ee95932014-06-11 12:48:08 -0600397 ret = dfu_write_buffer_drain(dfu);
398 if (ret) {
Patrick Delaunay57da0602017-07-19 16:39:24 +0200399 dfu_transaction_cleanup(dfu);
Patrick Delaunayd4710322021-05-18 15:12:12 +0200400 dfu_error_callback(dfu, "DFU write error");
Stephen Warren3ee95932014-06-11 12:48:08 -0600401 return ret;
402 }
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000403 }
404
Stephen Warren3ee95932014-06-11 12:48:08 -0600405 return 0;
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000406}
407
408static int dfu_read_buffer_fill(struct dfu_entity *dfu, void *buf, int size)
409{
410 long chunk;
411 int ret, readn;
412
413 readn = 0;
414 while (size > 0) {
415 /* get chunk that can be read */
Masahiro Yamadab4141192014-11-07 03:03:31 +0900416 chunk = min((long)size, dfu->b_left);
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000417 /* consume */
418 if (chunk > 0) {
419 memcpy(buf, dfu->i_buf, chunk);
Lukasz Majewskibd694242014-05-12 10:43:36 +0200420 if (dfu_hash_algo)
421 dfu_hash_algo->hash_update(dfu_hash_algo,
422 &dfu->crc, buf,
423 chunk, 0);
424
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000425 dfu->i_buf += chunk;
426 dfu->b_left -= chunk;
427 size -= chunk;
428 buf += chunk;
429 readn += chunk;
430 }
431
432 /* all done */
433 if (size > 0) {
434 /* no more to read */
435 if (dfu->r_left == 0)
436 break;
437
438 dfu->i_buf = dfu->i_buf_start;
439 dfu->b_left = dfu->i_buf_end - dfu->i_buf_start;
440
441 /* got to read, but buffer is empty */
442 if (dfu->b_left > dfu->r_left)
443 dfu->b_left = dfu->r_left;
444 ret = dfu->read_medium(dfu, dfu->offset, dfu->i_buf,
445 &dfu->b_left);
446 if (ret != 0) {
447 debug("%s: Read error!\n", __func__);
448 return ret;
449 }
Patrick Delaunay0de10222019-10-14 09:28:03 +0200450 if (dfu->b_left == 0)
451 break;
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000452 dfu->offset += dfu->b_left;
453 dfu->r_left -= dfu->b_left;
454
455 puts("#");
456 }
457 }
458
459 return readn;
460}
461
462int dfu_read(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
463{
464 int ret = 0;
465
466 debug("%s: name: %s buf: 0x%p size: 0x%x p_num: 0x%x i_buf: 0x%p\n",
467 __func__, dfu->name, buf, size, blk_seq_num, dfu->i_buf);
468
Patrick Delaunay6fa8ddd2017-07-19 16:39:25 +0200469 ret = dfu_transaction_initiate(dfu, true);
470 if (ret < 0)
471 return ret;
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000472
473 if (dfu->i_blk_seq_num != blk_seq_num) {
474 printf("%s: Wrong sequence number! [%d] [%d]\n",
475 __func__, dfu->i_blk_seq_num, blk_seq_num);
476 return -1;
477 }
478 /* handle rollover */
479 dfu->i_blk_seq_num = (dfu->i_blk_seq_num + 1) & 0xffff;
480
481 ret = dfu_read_buffer_fill(dfu, buf, size);
482 if (ret < 0) {
483 printf("%s: Failed to fill buffer\n", __func__);
484 return -1;
485 }
486
487 if (ret < size) {
Lukasz Majewskibd694242014-05-12 10:43:36 +0200488 if (dfu_hash_algo)
489 debug("%s: %s %s: 0x%x\n", __func__, dfu->name,
490 dfu_hash_algo->name, dfu->crc);
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000491 puts("\nUPLOAD ... done\nCtrl+C to exit ...\n");
492
Patrick Delaunay57da0602017-07-19 16:39:24 +0200493 dfu_transaction_cleanup(dfu);
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000494 }
495
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200496 return ret;
497}
498
499static int dfu_fill_entity(struct dfu_entity *dfu, char *s, int alt,
Stephen Warrendd648272014-06-11 16:03:33 -0600500 char *interface, char *devstr)
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200501{
502 char *st;
503
Stephen Warrendd648272014-06-11 16:03:33 -0600504 debug("%s: %s interface: %s dev: %s\n", __func__, s, interface, devstr);
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200505 st = strsep(&s, " ");
506 strcpy(dfu->name, st);
507
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200508 dfu->alt = alt;
Stephen Warren7ac1b412014-06-11 16:03:34 -0600509 dfu->max_buf_size = 0;
Stephen Warrencb7bd2e2014-06-11 16:03:35 -0600510 dfu->free_entity = NULL;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200511
512 /* Specific for mmc device */
513 if (strcmp(interface, "mmc") == 0) {
Stephen Warrendd648272014-06-11 16:03:33 -0600514 if (dfu_fill_entity_mmc(dfu, devstr, s))
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200515 return -1;
Patrick Delaunay6015af22019-10-14 09:28:04 +0200516 } else if (strcmp(interface, "mtd") == 0) {
517 if (dfu_fill_entity_mtd(dfu, devstr, s))
518 return -1;
Pantelis Antoniouc6631762013-03-14 05:32:52 +0000519 } else if (strcmp(interface, "nand") == 0) {
Stephen Warrendd648272014-06-11 16:03:33 -0600520 if (dfu_fill_entity_nand(dfu, devstr, s))
Pantelis Antoniouc6631762013-03-14 05:32:52 +0000521 return -1;
Afzal Mohammeda9479f02013-09-18 01:15:24 +0530522 } else if (strcmp(interface, "ram") == 0) {
Stephen Warrendd648272014-06-11 16:03:33 -0600523 if (dfu_fill_entity_ram(dfu, devstr, s))
Afzal Mohammeda9479f02013-09-18 01:15:24 +0530524 return -1;
Stephen Warren6f12ebf2014-06-11 16:03:36 -0600525 } else if (strcmp(interface, "sf") == 0) {
526 if (dfu_fill_entity_sf(dfu, devstr, s))
527 return -1;
Patrick Delaunayec44cac2019-10-14 09:28:06 +0200528 } else if (strcmp(interface, "virt") == 0) {
529 if (dfu_fill_entity_virt(dfu, devstr, s))
530 return -1;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200531 } else {
532 printf("%s: Device %s not (yet) supported!\n",
533 __func__, interface);
534 return -1;
535 }
Stephen Warren806bd242015-09-04 22:03:46 -0600536 dfu_get_buf(dfu);
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200537
538 return 0;
539}
540
541void dfu_free_entities(void)
542{
543 struct dfu_entity *dfu, *p, *t = NULL;
544
Stephen Warren806bd242015-09-04 22:03:46 -0600545 dfu_free_buf();
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200546 list_for_each_entry_safe_reverse(dfu, p, &dfu_list, list) {
547 list_del(&dfu->list);
Stephen Warrencb7bd2e2014-06-11 16:03:35 -0600548 if (dfu->free_entity)
549 dfu->free_entity(dfu);
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200550 t = dfu;
551 }
552 if (t)
553 free(t);
554 INIT_LIST_HEAD(&dfu_list);
Przemyslaw Marczakb627eb42014-02-28 18:53:37 +0100555
556 alt_num_cnt = 0;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200557}
558
Patrick Delaunay9ada6832019-10-14 09:28:01 +0200559int dfu_alt_init(int num, struct dfu_entity **dfu)
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200560{
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200561 char *s;
Patrick Delaunay9ada6832019-10-14 09:28:01 +0200562 int ret;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200563
Patrick Delaunay9ada6832019-10-14 09:28:01 +0200564 dfu_alt_num = num;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200565 debug("%s: dfu_alt_num=%d\n", __func__, dfu_alt_num);
566
Lukasz Majewskibd694242014-05-12 10:43:36 +0200567 dfu_hash_algo = NULL;
568 s = dfu_get_hash_algo();
569 if (s) {
570 ret = hash_lookup_algo(s, &dfu_hash_algo);
571 if (ret)
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900572 pr_err("Hash algorithm %s not supported\n", s);
Lukasz Majewskibd694242014-05-12 10:43:36 +0200573 }
574
Patrick Delaunay9ada6832019-10-14 09:28:01 +0200575 *dfu = calloc(sizeof(struct dfu_entity), dfu_alt_num);
576 if (!*dfu)
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200577 return -1;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200578
Patrick Delaunay9ada6832019-10-14 09:28:01 +0200579 return 0;
580}
581
582int dfu_alt_add(struct dfu_entity *dfu, char *interface, char *devstr, char *s)
583{
584 struct dfu_entity *p_dfu;
585 int ret;
586
587 if (alt_num_cnt >= dfu_alt_num)
588 return -1;
589
590 p_dfu = &dfu[alt_num_cnt];
591 ret = dfu_fill_entity(p_dfu, s, alt_num_cnt, interface, devstr);
592 if (ret)
593 return -1;
594
595 list_add_tail(&p_dfu->list, &dfu_list);
596 alt_num_cnt++;
597
598 return 0;
599}
600
601int dfu_config_entities(char *env, char *interface, char *devstr)
602{
603 struct dfu_entity *dfu;
604 int i, ret;
605 char *s;
606
607 ret = dfu_alt_init(dfu_find_alt_num(env), &dfu);
608 if (ret)
609 return -1;
610
611 for (i = 0; i < dfu_alt_num; i++) {
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200612 s = strsep(&env, ";");
Patrick Delaunay9ada6832019-10-14 09:28:01 +0200613 ret = dfu_alt_add(dfu, interface, devstr, s);
Peng Fan5d8fae72016-05-03 10:24:52 +0800614 if (ret) {
Sam Protsenkofeaa7852018-07-13 16:35:45 +0300615 /* We will free "dfu" in dfu_free_entities() */
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200616 return -1;
Peng Fan5d8fae72016-05-03 10:24:52 +0800617 }
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200618 }
619
620 return 0;
621}
622
623const char *dfu_get_dev_type(enum dfu_device_type t)
624{
Patrick Delaunay909b6902019-10-14 09:27:58 +0200625 const char *const dev_t[] = {NULL, "eMMC", "OneNAND", "NAND", "RAM",
Patrick Delaunayec44cac2019-10-14 09:28:06 +0200626 "SF", "MTD", "VIRT"};
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200627 return dev_t[t];
628}
629
630const char *dfu_get_layout(enum dfu_layout l)
631{
Patrick Delaunay909b6902019-10-14 09:27:58 +0200632 const char *const dfu_layout[] = {NULL, "RAW_ADDR", "FAT", "EXT2",
Marek Szyprowskic533f942020-12-22 11:32:22 +0100633 "EXT3", "EXT4", "RAM_ADDR", "SKIP",
634 "SCRIPT" };
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200635 return dfu_layout[l];
636}
637
638void dfu_show_entities(void)
639{
640 struct dfu_entity *dfu;
641
642 puts("DFU alt settings list:\n");
643
644 list_for_each_entry(dfu, &dfu_list, list) {
645 printf("dev: %s alt: %d name: %s layout: %s\n",
646 dfu_get_dev_type(dfu->dev_type), dfu->alt,
647 dfu->name, dfu_get_layout(dfu->layout));
648 }
649}
650
651int dfu_get_alt_number(void)
652{
653 return dfu_alt_num;
654}
655
656struct dfu_entity *dfu_get_entity(int alt)
657{
658 struct dfu_entity *dfu;
659
660 list_for_each_entry(dfu, &dfu_list, list) {
661 if (dfu->alt == alt)
662 return dfu;
663 }
664
665 return NULL;
666}
Lukasz Majewskifed936e2013-10-08 14:30:38 +0200667
668int dfu_get_alt(char *name)
669{
670 struct dfu_entity *dfu;
Lukasz Majewski5610b052014-11-05 10:54:16 +0100671 char *str;
Lukasz Majewskifed936e2013-10-08 14:30:38 +0200672
673 list_for_each_entry(dfu, &dfu_list, list) {
Lukasz Majewski5610b052014-11-05 10:54:16 +0100674 if (dfu->name[0] != '/') {
675 if (!strncmp(dfu->name, name, strlen(dfu->name)))
676 return dfu->alt;
677 } else {
678 /*
679 * One must also consider absolute path
680 * (/boot/bin/uImage) available at dfu->name when
681 * compared "plain" file name (uImage)
682 *
683 * It is the case for e.g. thor gadget where lthor SW
684 * sends only the file name, so only the very last part
685 * of path must be checked for equality
686 */
687
688 str = strstr(dfu->name, name);
689 if (!str)
690 continue;
691
692 /*
693 * Check if matching substring is the last element of
694 * dfu->name (uImage)
695 */
696 if (strlen(dfu->name) ==
697 ((str - dfu->name) + strlen(name)))
698 return dfu->alt;
699 }
Lukasz Majewskifed936e2013-10-08 14:30:38 +0200700 }
701
702 return -ENODEV;
703}
Lukasz Majewski2092e462015-08-24 00:21:46 +0200704
705int dfu_write_from_mem_addr(struct dfu_entity *dfu, void *buf, int size)
706{
707 unsigned long dfu_buf_size, write, left = size;
708 int i, ret = 0;
709 void *dp = buf;
710
711 /*
712 * Here we must call dfu_get_buf(dfu) first to be sure that dfu_buf_size
713 * has been properly initialized - e.g. if "dfu_bufsiz" has been taken
714 * into account.
715 */
716 dfu_get_buf(dfu);
717 dfu_buf_size = dfu_get_buf_size();
718 debug("%s: dfu buf size: %lu\n", __func__, dfu_buf_size);
719
720 for (i = 0; left > 0; i++) {
721 write = min(dfu_buf_size, left);
722
723 debug("%s: dp: 0x%p left: %lu write: %lu\n", __func__,
724 dp, left, write);
725 ret = dfu_write(dfu, dp, write, i);
726 if (ret) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900727 pr_err("DFU write failed\n");
Lukasz Majewski2092e462015-08-24 00:21:46 +0200728 return ret;
729 }
730
731 dp += write;
732 left -= write;
733 }
734
735 ret = dfu_flush(dfu, NULL, 0, i);
736 if (ret)
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900737 pr_err("DFU flush failed!");
Lukasz Majewski2092e462015-08-24 00:21:46 +0200738
739 return ret;
740}