blob: 501a60b34400526b873293fc5c25bfe40124788b [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>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Lukasz Majewskid311c6e2012-08-06 14:41:07 +020013#include <malloc.h>
14#include <mmc.h>
15#include <fat.h>
16#include <dfu.h>
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +020017#include <hash.h>
Lukasz Majewskid311c6e2012-08-06 14:41:07 +020018#include <linux/list.h>
19#include <linux/compiler.h>
20
AKASHI Takahirob24b8292020-10-29 13:47:44 +090021LIST_HEAD(dfu_list);
Lukasz Majewskid311c6e2012-08-06 14:41:07 +020022static int dfu_alt_num;
Przemyslaw Marczak4af3e082014-02-28 18:53:37 +010023static int alt_num_cnt;
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +020024static struct hash_algo *dfu_hash_algo;
Andy Shevchenko1e414012019-11-27 18:12:15 +020025#ifdef CONFIG_DFU_TIMEOUT
26static unsigned long dfu_timeout = 0;
27#endif
Lukasz Majewskid311c6e2012-08-06 14:41:07 +020028
Lukasz Majewskid9eae702014-08-25 11:07:28 +020029/*
Patrick Delaunay773771f2019-10-14 09:28:07 +020030 * The purpose of the dfu_flush_callback() function is to
31 * provide callback for dfu user
32 */
33__weak void dfu_flush_callback(struct dfu_entity *dfu)
34{
35}
36
37/*
38 * The purpose of the dfu_initiated_callback() function is to
39 * provide callback for dfu user
40 */
41__weak void dfu_initiated_callback(struct dfu_entity *dfu)
42{
43}
44
45/*
Lukasz Majewskid9eae702014-08-25 11:07:28 +020046 * The purpose of the dfu_usb_get_reset() function is to
47 * provide information if after USB_DETACH request
48 * being sent the dfu-util performed reset of USB
49 * bus.
50 *
51 * Described behaviour is the only way to distinct if
52 * user has typed -e (detach) or -R (reset) when invoking
53 * dfu-util command.
54 *
55 */
56__weak bool dfu_usb_get_reset(void)
Lukasz Majewski2968aa22013-07-18 13:19:14 +020057{
B, Ravi4cca5cf2017-05-04 15:45:29 +053058#ifdef CONFIG_SPL_DFU_NO_RESET
59 return false;
60#else
Lukasz Majewskid9eae702014-08-25 11:07:28 +020061 return true;
B, Ravi4cca5cf2017-05-04 15:45:29 +053062#endif
Lukasz Majewski2968aa22013-07-18 13:19:14 +020063}
64
Andy Shevchenko1e414012019-11-27 18:12:15 +020065#ifdef CONFIG_DFU_TIMEOUT
66void dfu_set_timeout(unsigned long timeout)
67{
68 dfu_timeout = timeout;
69}
70
71unsigned long dfu_get_timeout(void)
72{
73 return dfu_timeout;
74}
75#endif
76
Lukasz Majewskid311c6e2012-08-06 14:41:07 +020077static int dfu_find_alt_num(const char *s)
78{
79 int i = 0;
80
81 for (; *s; s++)
82 if (*s == ';')
83 i++;
84
85 return ++i;
86}
87
Patrick Delaunay06164032019-10-14 09:28:02 +020088/*
89 * treat dfu_alt_info with several interface information
90 * to allow DFU on several device with one command,
91 * the string format is
92 * interface devstring'='alternate list (';' separated)
93 * and each interface separated by '&'
94 */
95int dfu_config_interfaces(char *env)
96{
97 struct dfu_entity *dfu;
98 char *s, *i, *d, *a, *part;
99 int ret = -EINVAL;
100 int n = 1;
101
102 s = env;
103 for (; *s; s++) {
104 if (*s == ';')
105 n++;
106 if (*s == '&')
107 n++;
108 }
109 ret = dfu_alt_init(n, &dfu);
110 if (ret)
111 return ret;
112
113 s = env;
114 while (s) {
115 ret = -EINVAL;
116 i = strsep(&s, " ");
117 if (!i)
118 break;
119 d = strsep(&s, "=");
120 if (!d)
121 break;
122 a = strsep(&s, "&");
123 if (!a)
124 a = s;
125 do {
126 part = strsep(&a, ";");
127 ret = dfu_alt_add(dfu, i, d, part);
128 if (ret)
129 return ret;
130 } while (a);
131 }
132
133 return ret;
134}
135
Stephen Warren4afe50f2014-06-11 16:03:33 -0600136int dfu_init_env_entities(char *interface, char *devstr)
Lukasz Majewski35e591e2013-09-11 14:53:35 +0200137{
138 const char *str_env;
139 char *env_bkp;
Sam Protsenko6500cde2018-07-13 16:35:46 +0300140 int ret = 0;
Lukasz Majewski35e591e2013-09-11 14:53:35 +0200141
Przemyslaw Marczakd0dd6092015-02-17 12:24:11 +0100142#ifdef CONFIG_SET_DFU_ALT_INFO
143 set_dfu_alt_info(interface, devstr);
144#endif
Simon Glass64b723f2017-08-03 12:22:12 -0600145 str_env = env_get("dfu_alt_info");
Lukasz Majewski35e591e2013-09-11 14:53:35 +0200146 if (!str_env) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900147 pr_err("\"dfu_alt_info\" env variable not defined!\n");
Lukasz Majewski35e591e2013-09-11 14:53:35 +0200148 return -EINVAL;
149 }
150
151 env_bkp = strdup(str_env);
Patrick Delaunay06164032019-10-14 09:28:02 +0200152 if (!interface && !devstr)
153 ret = dfu_config_interfaces(env_bkp);
154 else
155 ret = dfu_config_entities(env_bkp, interface, devstr);
156
Lukasz Majewski35e591e2013-09-11 14:53:35 +0200157 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900158 pr_err("DFU entities configuration failed!\n");
Sam Protsenkoab79fb02018-07-13 16:35:47 +0300159 pr_err("(partition table does not match dfu_alt_info?)\n");
Sam Protsenko6500cde2018-07-13 16:35:46 +0300160 goto done;
Lukasz Majewski35e591e2013-09-11 14:53:35 +0200161 }
162
Sam Protsenko6500cde2018-07-13 16:35:46 +0300163done:
Lukasz Majewski35e591e2013-09-11 14:53:35 +0200164 free(env_bkp);
Sam Protsenko6500cde2018-07-13 16:35:46 +0300165 return ret;
Lukasz Majewski35e591e2013-09-11 14:53:35 +0200166}
167
Heiko Schochera2f831e2013-06-12 06:05:51 +0200168static unsigned char *dfu_buf;
Lukasz Majewski0af4df82015-07-08 23:43:18 +0200169static unsigned long dfu_buf_size;
Patrick Delaunay06164032019-10-14 09:28:02 +0200170static enum dfu_device_type dfu_buf_device_type;
Heiko Schochera2f831e2013-06-12 06:05:51 +0200171
Lukasz Majewski34264232013-10-08 14:30:39 +0200172unsigned char *dfu_free_buf(void)
Heiko Schochera2f831e2013-06-12 06:05:51 +0200173{
174 free(dfu_buf);
175 dfu_buf = NULL;
176 return dfu_buf;
177}
178
Lukasz Majewski031d4132013-12-09 16:20:13 +0100179unsigned long dfu_get_buf_size(void)
180{
181 return dfu_buf_size;
182}
183
Stephen Warrenf9500982014-06-11 16:03:34 -0600184unsigned char *dfu_get_buf(struct dfu_entity *dfu)
Heiko Schochera2f831e2013-06-12 06:05:51 +0200185{
186 char *s;
187
Patrick Delaunay06164032019-10-14 09:28:02 +0200188 /* manage several entity with several contraint */
189 if (dfu_buf && dfu->dev_type != dfu_buf_device_type)
190 dfu_free_buf();
191
Heiko Schochera2f831e2013-06-12 06:05:51 +0200192 if (dfu_buf != NULL)
193 return dfu_buf;
194
Simon Glass64b723f2017-08-03 12:22:12 -0600195 s = env_get("dfu_bufsiz");
Przemyslaw Marczak24883502014-12-15 10:34:11 +0100196 if (s)
197 dfu_buf_size = (unsigned long)simple_strtol(s, NULL, 0);
198
199 if (!s || !dfu_buf_size)
200 dfu_buf_size = CONFIG_SYS_DFU_DATA_BUF_SIZE;
201
Stephen Warrenf9500982014-06-11 16:03:34 -0600202 if (dfu->max_buf_size && dfu_buf_size > dfu->max_buf_size)
203 dfu_buf_size = dfu->max_buf_size;
Heiko Schochera2f831e2013-06-12 06:05:51 +0200204
205 dfu_buf = memalign(CONFIG_SYS_CACHELINE_SIZE, dfu_buf_size);
206 if (dfu_buf == NULL)
207 printf("%s: Could not memalign 0x%lx bytes\n",
208 __func__, dfu_buf_size);
209
Patrick Delaunay06164032019-10-14 09:28:02 +0200210 dfu_buf_device_type = dfu->dev_type;
Heiko Schochera2f831e2013-06-12 06:05:51 +0200211 return dfu_buf;
212}
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200213
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200214static char *dfu_get_hash_algo(void)
215{
216 char *s;
217
Simon Glass64b723f2017-08-03 12:22:12 -0600218 s = env_get("dfu_hash_algo");
Lukasz Majewski3e0d0282014-06-10 12:25:59 +0200219 if (!s)
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200220 return NULL;
221
Lukasz Majewski3e0d0282014-06-10 12:25:59 +0200222 if (!strcmp(s, "crc32")) {
223 debug("%s: DFU hash method: %s\n", __func__, s);
224 return s;
225 }
226
Masahiro Yamada81e10422017-09-16 14:10:41 +0900227 pr_err("DFU hash method: %s not supported!\n", s);
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200228 return NULL;
229}
230
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000231static int dfu_write_buffer_drain(struct dfu_entity *dfu)
232{
233 long w_size;
234 int ret;
235
236 /* flush size? */
237 w_size = dfu->i_buf - dfu->i_buf_start;
238 if (w_size == 0)
239 return 0;
240
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200241 if (dfu_hash_algo)
242 dfu_hash_algo->hash_update(dfu_hash_algo, &dfu->crc,
243 dfu->i_buf_start, w_size, 0);
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000244
245 ret = dfu->write_medium(dfu, dfu->offset, dfu->i_buf_start, &w_size);
246 if (ret)
247 debug("%s: Write error!\n", __func__);
248
249 /* point back */
250 dfu->i_buf = dfu->i_buf_start;
251
252 /* update offset */
253 dfu->offset += w_size;
254
255 puts("#");
256
257 return ret;
258}
259
Patrick Delaunay35be83e2017-07-19 16:39:24 +0200260void dfu_transaction_cleanup(struct dfu_entity *dfu)
Stephen Warren7f450232014-06-11 12:48:08 -0600261{
262 /* clear everything */
Stephen Warren7f450232014-06-11 12:48:08 -0600263 dfu->crc = 0;
264 dfu->offset = 0;
265 dfu->i_blk_seq_num = 0;
Patrick Delaunaydec4ecb2017-07-19 16:39:25 +0200266 dfu->i_buf_start = dfu_get_buf(dfu);
267 dfu->i_buf_end = dfu->i_buf_start;
Stephen Warren7f450232014-06-11 12:48:08 -0600268 dfu->i_buf = dfu->i_buf_start;
Patrick Delaunay35be83e2017-07-19 16:39:24 +0200269 dfu->r_left = 0;
270 dfu->b_left = 0;
271 dfu->bad_skip = 0;
272
Stephen Warren7f450232014-06-11 12:48:08 -0600273 dfu->inited = 0;
274}
275
Patrick Delaunaydec4ecb2017-07-19 16:39:25 +0200276int dfu_transaction_initiate(struct dfu_entity *dfu, bool read)
277{
278 int ret = 0;
279
280 if (dfu->inited)
281 return 0;
282
283 dfu_transaction_cleanup(dfu);
284
285 if (dfu->i_buf_start == NULL)
286 return -ENOMEM;
287
288 dfu->i_buf_end = dfu->i_buf_start + dfu_get_buf_size();
289
290 if (read) {
291 ret = dfu->get_medium_size(dfu, &dfu->r_left);
292 if (ret < 0)
293 return ret;
294 debug("%s: %s %lld [B]\n", __func__, dfu->name, dfu->r_left);
295 }
296
297 dfu->inited = 1;
Patrick Delaunay773771f2019-10-14 09:28:07 +0200298 dfu_initiated_callback(dfu);
Patrick Delaunaydec4ecb2017-07-19 16:39:25 +0200299
300 return 0;
301}
302
Heiko Schocher941b3842014-03-18 08:09:55 +0100303int dfu_flush(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
304{
305 int ret = 0;
306
Przemyslaw Marczaka0f6a382014-05-12 12:05:33 +0200307 ret = dfu_write_buffer_drain(dfu);
308 if (ret)
309 return ret;
310
Heiko Schocher941b3842014-03-18 08:09:55 +0100311 if (dfu->flush_medium)
312 ret = dfu->flush_medium(dfu);
313
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200314 if (dfu_hash_algo)
315 printf("\nDFU complete %s: 0x%08x\n", dfu_hash_algo->name,
316 dfu->crc);
Heiko Schocher941b3842014-03-18 08:09:55 +0100317
Patrick Delaunay773771f2019-10-14 09:28:07 +0200318 dfu_flush_callback(dfu);
319
Patrick Delaunay35be83e2017-07-19 16:39:24 +0200320 dfu_transaction_cleanup(dfu);
Heiko Schocher941b3842014-03-18 08:09:55 +0100321
322 return ret;
323}
324
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200325int dfu_write(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
326{
Stephen Warren7f450232014-06-11 12:48:08 -0600327 int ret;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200328
Stephen Warrenc00315b2015-07-22 14:54:04 -0600329 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 +0000330 __func__, dfu->name, buf, size, blk_seq_num, dfu->offset,
Stephen Warrenc00315b2015-07-22 14:54:04 -0600331 (unsigned long)(dfu->i_buf - dfu->i_buf_start));
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200332
Patrick Delaunaydec4ecb2017-07-19 16:39:25 +0200333 ret = dfu_transaction_initiate(dfu, false);
334 if (ret < 0)
335 return ret;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200336
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000337 if (dfu->i_blk_seq_num != blk_seq_num) {
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200338 printf("%s: Wrong sequence number! [%d] [%d]\n",
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000339 __func__, dfu->i_blk_seq_num, blk_seq_num);
Patrick Delaunay35be83e2017-07-19 16:39:24 +0200340 dfu_transaction_cleanup(dfu);
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000341 return -1;
342 }
343
344 /* DFU 1.1 standard says:
345 * The wBlockNum field is a block sequence number. It increments each
346 * time a block is transferred, wrapping to zero from 65,535. It is used
347 * to provide useful context to the DFU loader in the device."
348 *
349 * This means that it's a 16 bit counter that roll-overs at
350 * 0xffff -> 0x0000. By having a typical 4K transfer block
351 * we roll-over at exactly 256MB. Not very fun to debug.
352 *
353 * Handling rollover, and having an inited variable,
354 * makes things work.
355 */
356
357 /* handle rollover */
358 dfu->i_blk_seq_num = (dfu->i_blk_seq_num + 1) & 0xffff;
359
360 /* flush buffer if overflow */
361 if ((dfu->i_buf + size) > dfu->i_buf_end) {
Stephen Warren7f450232014-06-11 12:48:08 -0600362 ret = dfu_write_buffer_drain(dfu);
363 if (ret) {
Patrick Delaunay35be83e2017-07-19 16:39:24 +0200364 dfu_transaction_cleanup(dfu);
Stephen Warren7f450232014-06-11 12:48:08 -0600365 return ret;
366 }
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000367 }
368
369 /* we should be in buffer now (if not then size too large) */
370 if ((dfu->i_buf + size) > dfu->i_buf_end) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900371 pr_err("Buffer overflow! (0x%p + 0x%x > 0x%p)\n", dfu->i_buf,
Lukasz Majewski385c3212013-09-10 15:29:22 +0200372 size, dfu->i_buf_end);
Patrick Delaunay35be83e2017-07-19 16:39:24 +0200373 dfu_transaction_cleanup(dfu);
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200374 return -1;
375 }
376
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000377 memcpy(dfu->i_buf, buf, size);
378 dfu->i_buf += size;
379
380 /* if end or if buffer full flush */
381 if (size == 0 || (dfu->i_buf + size) > dfu->i_buf_end) {
Stephen Warren7f450232014-06-11 12:48:08 -0600382 ret = dfu_write_buffer_drain(dfu);
383 if (ret) {
Patrick Delaunay35be83e2017-07-19 16:39:24 +0200384 dfu_transaction_cleanup(dfu);
Stephen Warren7f450232014-06-11 12:48:08 -0600385 return ret;
386 }
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000387 }
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200388
Stephen Warren7f450232014-06-11 12:48:08 -0600389 return 0;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200390}
391
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000392static int dfu_read_buffer_fill(struct dfu_entity *dfu, void *buf, int size)
393{
394 long chunk;
395 int ret, readn;
396
397 readn = 0;
398 while (size > 0) {
399 /* get chunk that can be read */
Masahiro Yamadadb204642014-11-07 03:03:31 +0900400 chunk = min((long)size, dfu->b_left);
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000401 /* consume */
402 if (chunk > 0) {
403 memcpy(buf, dfu->i_buf, chunk);
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200404 if (dfu_hash_algo)
405 dfu_hash_algo->hash_update(dfu_hash_algo,
406 &dfu->crc, buf,
407 chunk, 0);
408
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000409 dfu->i_buf += chunk;
410 dfu->b_left -= chunk;
411 size -= chunk;
412 buf += chunk;
413 readn += chunk;
414 }
415
416 /* all done */
417 if (size > 0) {
418 /* no more to read */
419 if (dfu->r_left == 0)
420 break;
421
422 dfu->i_buf = dfu->i_buf_start;
423 dfu->b_left = dfu->i_buf_end - dfu->i_buf_start;
424
425 /* got to read, but buffer is empty */
426 if (dfu->b_left > dfu->r_left)
427 dfu->b_left = dfu->r_left;
428 ret = dfu->read_medium(dfu, dfu->offset, dfu->i_buf,
429 &dfu->b_left);
430 if (ret != 0) {
431 debug("%s: Read error!\n", __func__);
432 return ret;
433 }
Patrick Delaunay7ef84362019-10-14 09:28:03 +0200434 if (dfu->b_left == 0)
435 break;
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000436 dfu->offset += dfu->b_left;
437 dfu->r_left -= dfu->b_left;
438
439 puts("#");
440 }
441 }
442
443 return readn;
444}
445
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200446int dfu_read(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
447{
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200448 int ret = 0;
449
450 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 +0000451 __func__, dfu->name, buf, size, blk_seq_num, dfu->i_buf);
452
Patrick Delaunaydec4ecb2017-07-19 16:39:25 +0200453 ret = dfu_transaction_initiate(dfu, true);
454 if (ret < 0)
455 return ret;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200456
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000457 if (dfu->i_blk_seq_num != blk_seq_num) {
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200458 printf("%s: Wrong sequence number! [%d] [%d]\n",
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000459 __func__, dfu->i_blk_seq_num, blk_seq_num);
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200460 return -1;
461 }
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000462 /* handle rollover */
463 dfu->i_blk_seq_num = (dfu->i_blk_seq_num + 1) & 0xffff;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200464
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000465 ret = dfu_read_buffer_fill(dfu, buf, size);
466 if (ret < 0) {
467 printf("%s: Failed to fill buffer\n", __func__);
468 return -1;
469 }
470
471 if (ret < size) {
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200472 if (dfu_hash_algo)
473 debug("%s: %s %s: 0x%x\n", __func__, dfu->name,
474 dfu_hash_algo->name, dfu->crc);
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000475 puts("\nUPLOAD ... done\nCtrl+C to exit ...\n");
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200476
Patrick Delaunay35be83e2017-07-19 16:39:24 +0200477 dfu_transaction_cleanup(dfu);
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200478 }
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000479
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200480 return ret;
481}
482
483static int dfu_fill_entity(struct dfu_entity *dfu, char *s, int alt,
Stephen Warren4afe50f2014-06-11 16:03:33 -0600484 char *interface, char *devstr)
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200485{
486 char *st;
487
Stephen Warren4afe50f2014-06-11 16:03:33 -0600488 debug("%s: %s interface: %s dev: %s\n", __func__, s, interface, devstr);
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200489 st = strsep(&s, " ");
490 strcpy(dfu->name, st);
491
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200492 dfu->alt = alt;
Stephen Warrenf9500982014-06-11 16:03:34 -0600493 dfu->max_buf_size = 0;
Stephen Warrena5f77f62014-06-11 16:03:35 -0600494 dfu->free_entity = NULL;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200495
496 /* Specific for mmc device */
497 if (strcmp(interface, "mmc") == 0) {
Stephen Warren4afe50f2014-06-11 16:03:33 -0600498 if (dfu_fill_entity_mmc(dfu, devstr, s))
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200499 return -1;
Patrick Delaunayf7aee232019-10-14 09:28:04 +0200500 } else if (strcmp(interface, "mtd") == 0) {
501 if (dfu_fill_entity_mtd(dfu, devstr, s))
502 return -1;
Pantelis Antonioucf14d0d2013-03-14 05:32:52 +0000503 } else if (strcmp(interface, "nand") == 0) {
Stephen Warren4afe50f2014-06-11 16:03:33 -0600504 if (dfu_fill_entity_nand(dfu, devstr, s))
Pantelis Antonioucf14d0d2013-03-14 05:32:52 +0000505 return -1;
Afzal Mohammede3c687a2013-09-18 01:15:24 +0530506 } else if (strcmp(interface, "ram") == 0) {
Stephen Warren4afe50f2014-06-11 16:03:33 -0600507 if (dfu_fill_entity_ram(dfu, devstr, s))
Afzal Mohammede3c687a2013-09-18 01:15:24 +0530508 return -1;
Stephen Warren0280af72014-06-11 16:03:36 -0600509 } else if (strcmp(interface, "sf") == 0) {
510 if (dfu_fill_entity_sf(dfu, devstr, s))
511 return -1;
Patrick Delaunay2111e3c2019-10-14 09:28:06 +0200512 } else if (strcmp(interface, "virt") == 0) {
513 if (dfu_fill_entity_virt(dfu, devstr, s))
514 return -1;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200515 } else {
516 printf("%s: Device %s not (yet) supported!\n",
517 __func__, interface);
518 return -1;
519 }
Stephen Warren0898da52015-09-04 22:03:46 -0600520 dfu_get_buf(dfu);
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200521
522 return 0;
523}
524
525void dfu_free_entities(void)
526{
527 struct dfu_entity *dfu, *p, *t = NULL;
528
Stephen Warren0898da52015-09-04 22:03:46 -0600529 dfu_free_buf();
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200530 list_for_each_entry_safe_reverse(dfu, p, &dfu_list, list) {
531 list_del(&dfu->list);
Stephen Warrena5f77f62014-06-11 16:03:35 -0600532 if (dfu->free_entity)
533 dfu->free_entity(dfu);
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200534 t = dfu;
535 }
536 if (t)
537 free(t);
538 INIT_LIST_HEAD(&dfu_list);
Przemyslaw Marczak4af3e082014-02-28 18:53:37 +0100539
540 alt_num_cnt = 0;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200541}
542
Patrick Delaunay0f8fad82019-10-14 09:28:01 +0200543int dfu_alt_init(int num, struct dfu_entity **dfu)
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200544{
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200545 char *s;
Patrick Delaunay0f8fad82019-10-14 09:28:01 +0200546 int ret;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200547
Patrick Delaunay0f8fad82019-10-14 09:28:01 +0200548 dfu_alt_num = num;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200549 debug("%s: dfu_alt_num=%d\n", __func__, dfu_alt_num);
550
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200551 dfu_hash_algo = NULL;
552 s = dfu_get_hash_algo();
553 if (s) {
554 ret = hash_lookup_algo(s, &dfu_hash_algo);
555 if (ret)
Masahiro Yamada81e10422017-09-16 14:10:41 +0900556 pr_err("Hash algorithm %s not supported\n", s);
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200557 }
558
Patrick Delaunay0f8fad82019-10-14 09:28:01 +0200559 *dfu = calloc(sizeof(struct dfu_entity), dfu_alt_num);
560 if (!*dfu)
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200561 return -1;
Patrick Delaunay0f8fad82019-10-14 09:28:01 +0200562
563 return 0;
564}
565
566int dfu_alt_add(struct dfu_entity *dfu, char *interface, char *devstr, char *s)
567{
568 struct dfu_entity *p_dfu;
569 int ret;
570
571 if (alt_num_cnt >= dfu_alt_num)
572 return -1;
573
574 p_dfu = &dfu[alt_num_cnt];
575 ret = dfu_fill_entity(p_dfu, s, alt_num_cnt, interface, devstr);
576 if (ret)
577 return -1;
578
579 list_add_tail(&p_dfu->list, &dfu_list);
580 alt_num_cnt++;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200581
Patrick Delaunay0f8fad82019-10-14 09:28:01 +0200582 return 0;
583}
584
585int dfu_config_entities(char *env, char *interface, char *devstr)
586{
587 struct dfu_entity *dfu;
588 int i, ret;
589 char *s;
590
591 ret = dfu_alt_init(dfu_find_alt_num(env), &dfu);
592 if (ret)
593 return -1;
594
595 for (i = 0; i < dfu_alt_num; i++) {
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200596 s = strsep(&env, ";");
Patrick Delaunay0f8fad82019-10-14 09:28:01 +0200597 ret = dfu_alt_add(dfu, interface, devstr, s);
Peng Fan529d4fd2016-05-03 10:24:52 +0800598 if (ret) {
Sam Protsenko6d29e5c2018-07-13 16:35:45 +0300599 /* We will free "dfu" in dfu_free_entities() */
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200600 return -1;
Peng Fan529d4fd2016-05-03 10:24:52 +0800601 }
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200602 }
603
604 return 0;
605}
606
607const char *dfu_get_dev_type(enum dfu_device_type t)
608{
Patrick Delaunayae224c32019-10-14 09:27:58 +0200609 const char *const dev_t[] = {NULL, "eMMC", "OneNAND", "NAND", "RAM",
Patrick Delaunay2111e3c2019-10-14 09:28:06 +0200610 "SF", "MTD", "VIRT"};
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200611 return dev_t[t];
612}
613
614const char *dfu_get_layout(enum dfu_layout l)
615{
Patrick Delaunayae224c32019-10-14 09:27:58 +0200616 const char *const dfu_layout[] = {NULL, "RAW_ADDR", "FAT", "EXT2",
617 "EXT3", "EXT4", "RAM_ADDR" };
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200618 return dfu_layout[l];
619}
620
621void dfu_show_entities(void)
622{
623 struct dfu_entity *dfu;
624
625 puts("DFU alt settings list:\n");
626
627 list_for_each_entry(dfu, &dfu_list, list) {
628 printf("dev: %s alt: %d name: %s layout: %s\n",
629 dfu_get_dev_type(dfu->dev_type), dfu->alt,
630 dfu->name, dfu_get_layout(dfu->layout));
631 }
632}
633
634int dfu_get_alt_number(void)
635{
636 return dfu_alt_num;
637}
638
639struct dfu_entity *dfu_get_entity(int alt)
640{
641 struct dfu_entity *dfu;
642
643 list_for_each_entry(dfu, &dfu_list, list) {
644 if (dfu->alt == alt)
645 return dfu;
646 }
647
648 return NULL;
649}
Lukasz Majewski1737f0f2013-10-08 14:30:38 +0200650
651int dfu_get_alt(char *name)
652{
653 struct dfu_entity *dfu;
Lukasz Majewski3a39d012014-11-05 10:54:16 +0100654 char *str;
Lukasz Majewski1737f0f2013-10-08 14:30:38 +0200655
656 list_for_each_entry(dfu, &dfu_list, list) {
Lukasz Majewski3a39d012014-11-05 10:54:16 +0100657 if (dfu->name[0] != '/') {
658 if (!strncmp(dfu->name, name, strlen(dfu->name)))
659 return dfu->alt;
660 } else {
661 /*
662 * One must also consider absolute path
663 * (/boot/bin/uImage) available at dfu->name when
664 * compared "plain" file name (uImage)
665 *
666 * It is the case for e.g. thor gadget where lthor SW
667 * sends only the file name, so only the very last part
668 * of path must be checked for equality
669 */
670
671 str = strstr(dfu->name, name);
672 if (!str)
673 continue;
674
675 /*
676 * Check if matching substring is the last element of
677 * dfu->name (uImage)
678 */
679 if (strlen(dfu->name) ==
680 ((str - dfu->name) + strlen(name)))
681 return dfu->alt;
682 }
Lukasz Majewski1737f0f2013-10-08 14:30:38 +0200683 }
684
685 return -ENODEV;
686}
Lukasz Majewskicc2d0262015-08-24 00:21:46 +0200687
688int dfu_write_from_mem_addr(struct dfu_entity *dfu, void *buf, int size)
689{
690 unsigned long dfu_buf_size, write, left = size;
691 int i, ret = 0;
692 void *dp = buf;
693
694 /*
695 * Here we must call dfu_get_buf(dfu) first to be sure that dfu_buf_size
696 * has been properly initialized - e.g. if "dfu_bufsiz" has been taken
697 * into account.
698 */
699 dfu_get_buf(dfu);
700 dfu_buf_size = dfu_get_buf_size();
701 debug("%s: dfu buf size: %lu\n", __func__, dfu_buf_size);
702
703 for (i = 0; left > 0; i++) {
704 write = min(dfu_buf_size, left);
705
706 debug("%s: dp: 0x%p left: %lu write: %lu\n", __func__,
707 dp, left, write);
708 ret = dfu_write(dfu, dp, write, i);
709 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900710 pr_err("DFU write failed\n");
Lukasz Majewskicc2d0262015-08-24 00:21:46 +0200711 return ret;
712 }
713
714 dp += write;
715 left -= write;
716 }
717
718 ret = dfu_flush(dfu, NULL, 0, i);
719 if (ret)
Masahiro Yamada81e10422017-09-16 14:10:41 +0900720 pr_err("DFU flush failed!");
Lukasz Majewskicc2d0262015-08-24 00:21:46 +0200721
722 return ret;
723}