blob: 213a20e7bcb1b65377c6d66780989505ce8232d5 [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
Marek Szyprowskib1fa1ad2020-12-22 11:32:22 +010029bool dfu_reinit_needed = false;
30
Lukasz Majewskid9eae702014-08-25 11:07:28 +020031/*
Patrick Delaunay773771f2019-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/*
Lukasz Majewskid9eae702014-08-25 11:07:28 +020048 * The purpose of the dfu_usb_get_reset() function is to
49 * provide information if after USB_DETACH request
50 * being sent the dfu-util performed reset of USB
51 * bus.
52 *
53 * Described behaviour is the only way to distinct if
54 * user has typed -e (detach) or -R (reset) when invoking
55 * dfu-util command.
56 *
57 */
58__weak bool dfu_usb_get_reset(void)
Lukasz Majewski2968aa22013-07-18 13:19:14 +020059{
B, Ravi4cca5cf2017-05-04 15:45:29 +053060#ifdef CONFIG_SPL_DFU_NO_RESET
61 return false;
62#else
Lukasz Majewskid9eae702014-08-25 11:07:28 +020063 return true;
B, Ravi4cca5cf2017-05-04 15:45:29 +053064#endif
Lukasz Majewski2968aa22013-07-18 13:19:14 +020065}
66
Andy Shevchenko1e414012019-11-27 18:12:15 +020067#ifdef CONFIG_DFU_TIMEOUT
68void dfu_set_timeout(unsigned long timeout)
69{
70 dfu_timeout = timeout;
71}
72
73unsigned long dfu_get_timeout(void)
74{
75 return dfu_timeout;
76}
77#endif
78
Lukasz Majewskid311c6e2012-08-06 14:41:07 +020079static int dfu_find_alt_num(const char *s)
80{
81 int i = 0;
82
83 for (; *s; s++)
84 if (*s == ';')
85 i++;
86
87 return ++i;
88}
89
Patrick Delaunay06164032019-10-14 09:28:02 +020090/*
91 * treat dfu_alt_info with several interface information
92 * to allow DFU on several device with one command,
93 * the string format is
94 * interface devstring'='alternate list (';' separated)
95 * and each interface separated by '&'
96 */
97int dfu_config_interfaces(char *env)
98{
99 struct dfu_entity *dfu;
100 char *s, *i, *d, *a, *part;
101 int ret = -EINVAL;
102 int n = 1;
103
104 s = env;
105 for (; *s; s++) {
106 if (*s == ';')
107 n++;
108 if (*s == '&')
109 n++;
110 }
111 ret = dfu_alt_init(n, &dfu);
112 if (ret)
113 return ret;
114
115 s = env;
116 while (s) {
117 ret = -EINVAL;
118 i = strsep(&s, " ");
119 if (!i)
120 break;
121 d = strsep(&s, "=");
122 if (!d)
123 break;
124 a = strsep(&s, "&");
125 if (!a)
126 a = s;
127 do {
128 part = strsep(&a, ";");
129 ret = dfu_alt_add(dfu, i, d, part);
130 if (ret)
131 return ret;
132 } while (a);
133 }
134
135 return ret;
136}
137
Stephen Warren4afe50f2014-06-11 16:03:33 -0600138int dfu_init_env_entities(char *interface, char *devstr)
Lukasz Majewski35e591e2013-09-11 14:53:35 +0200139{
140 const char *str_env;
141 char *env_bkp;
Sam Protsenko6500cde2018-07-13 16:35:46 +0300142 int ret = 0;
Lukasz Majewski35e591e2013-09-11 14:53:35 +0200143
Marek Szyprowskib1fa1ad2020-12-22 11:32:22 +0100144 dfu_reinit_needed = false;
145
Przemyslaw Marczakd0dd6092015-02-17 12:24:11 +0100146#ifdef CONFIG_SET_DFU_ALT_INFO
147 set_dfu_alt_info(interface, devstr);
148#endif
Simon Glass64b723f2017-08-03 12:22:12 -0600149 str_env = env_get("dfu_alt_info");
Lukasz Majewski35e591e2013-09-11 14:53:35 +0200150 if (!str_env) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900151 pr_err("\"dfu_alt_info\" env variable not defined!\n");
Lukasz Majewski35e591e2013-09-11 14:53:35 +0200152 return -EINVAL;
153 }
154
155 env_bkp = strdup(str_env);
Patrick Delaunay06164032019-10-14 09:28:02 +0200156 if (!interface && !devstr)
157 ret = dfu_config_interfaces(env_bkp);
158 else
159 ret = dfu_config_entities(env_bkp, interface, devstr);
160
Lukasz Majewski35e591e2013-09-11 14:53:35 +0200161 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900162 pr_err("DFU entities configuration failed!\n");
Sam Protsenkoab79fb02018-07-13 16:35:47 +0300163 pr_err("(partition table does not match dfu_alt_info?)\n");
Sam Protsenko6500cde2018-07-13 16:35:46 +0300164 goto done;
Lukasz Majewski35e591e2013-09-11 14:53:35 +0200165 }
166
Sam Protsenko6500cde2018-07-13 16:35:46 +0300167done:
Lukasz Majewski35e591e2013-09-11 14:53:35 +0200168 free(env_bkp);
Sam Protsenko6500cde2018-07-13 16:35:46 +0300169 return ret;
Lukasz Majewski35e591e2013-09-11 14:53:35 +0200170}
171
Heiko Schochera2f831e2013-06-12 06:05:51 +0200172static unsigned char *dfu_buf;
Lukasz Majewski0af4df82015-07-08 23:43:18 +0200173static unsigned long dfu_buf_size;
Patrick Delaunay06164032019-10-14 09:28:02 +0200174static enum dfu_device_type dfu_buf_device_type;
Heiko Schochera2f831e2013-06-12 06:05:51 +0200175
Lukasz Majewski34264232013-10-08 14:30:39 +0200176unsigned char *dfu_free_buf(void)
Heiko Schochera2f831e2013-06-12 06:05:51 +0200177{
178 free(dfu_buf);
179 dfu_buf = NULL;
180 return dfu_buf;
181}
182
Lukasz Majewski031d4132013-12-09 16:20:13 +0100183unsigned long dfu_get_buf_size(void)
184{
185 return dfu_buf_size;
186}
187
Stephen Warrenf9500982014-06-11 16:03:34 -0600188unsigned char *dfu_get_buf(struct dfu_entity *dfu)
Heiko Schochera2f831e2013-06-12 06:05:51 +0200189{
190 char *s;
191
Patrick Delaunay06164032019-10-14 09:28:02 +0200192 /* manage several entity with several contraint */
193 if (dfu_buf && dfu->dev_type != dfu_buf_device_type)
194 dfu_free_buf();
195
Heiko Schochera2f831e2013-06-12 06:05:51 +0200196 if (dfu_buf != NULL)
197 return dfu_buf;
198
Simon Glass64b723f2017-08-03 12:22:12 -0600199 s = env_get("dfu_bufsiz");
Przemyslaw Marczak24883502014-12-15 10:34:11 +0100200 if (s)
201 dfu_buf_size = (unsigned long)simple_strtol(s, NULL, 0);
202
203 if (!s || !dfu_buf_size)
204 dfu_buf_size = CONFIG_SYS_DFU_DATA_BUF_SIZE;
205
Stephen Warrenf9500982014-06-11 16:03:34 -0600206 if (dfu->max_buf_size && dfu_buf_size > dfu->max_buf_size)
207 dfu_buf_size = dfu->max_buf_size;
Heiko Schochera2f831e2013-06-12 06:05:51 +0200208
209 dfu_buf = memalign(CONFIG_SYS_CACHELINE_SIZE, dfu_buf_size);
210 if (dfu_buf == NULL)
211 printf("%s: Could not memalign 0x%lx bytes\n",
212 __func__, dfu_buf_size);
213
Patrick Delaunay06164032019-10-14 09:28:02 +0200214 dfu_buf_device_type = dfu->dev_type;
Heiko Schochera2f831e2013-06-12 06:05:51 +0200215 return dfu_buf;
216}
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200217
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200218static char *dfu_get_hash_algo(void)
219{
220 char *s;
221
Simon Glass64b723f2017-08-03 12:22:12 -0600222 s = env_get("dfu_hash_algo");
Lukasz Majewski3e0d0282014-06-10 12:25:59 +0200223 if (!s)
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200224 return NULL;
225
Lukasz Majewski3e0d0282014-06-10 12:25:59 +0200226 if (!strcmp(s, "crc32")) {
227 debug("%s: DFU hash method: %s\n", __func__, s);
228 return s;
229 }
230
Masahiro Yamada81e10422017-09-16 14:10:41 +0900231 pr_err("DFU hash method: %s not supported!\n", s);
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200232 return NULL;
233}
234
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000235static int dfu_write_buffer_drain(struct dfu_entity *dfu)
236{
237 long w_size;
238 int ret;
239
240 /* flush size? */
241 w_size = dfu->i_buf - dfu->i_buf_start;
242 if (w_size == 0)
243 return 0;
244
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200245 if (dfu_hash_algo)
246 dfu_hash_algo->hash_update(dfu_hash_algo, &dfu->crc,
247 dfu->i_buf_start, w_size, 0);
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000248
249 ret = dfu->write_medium(dfu, dfu->offset, dfu->i_buf_start, &w_size);
250 if (ret)
251 debug("%s: Write error!\n", __func__);
252
253 /* point back */
254 dfu->i_buf = dfu->i_buf_start;
255
256 /* update offset */
257 dfu->offset += w_size;
258
259 puts("#");
260
261 return ret;
262}
263
Patrick Delaunay35be83e2017-07-19 16:39:24 +0200264void dfu_transaction_cleanup(struct dfu_entity *dfu)
Stephen Warren7f450232014-06-11 12:48:08 -0600265{
266 /* clear everything */
Stephen Warren7f450232014-06-11 12:48:08 -0600267 dfu->crc = 0;
268 dfu->offset = 0;
269 dfu->i_blk_seq_num = 0;
Patrick Delaunaydec4ecb2017-07-19 16:39:25 +0200270 dfu->i_buf_start = dfu_get_buf(dfu);
271 dfu->i_buf_end = dfu->i_buf_start;
Stephen Warren7f450232014-06-11 12:48:08 -0600272 dfu->i_buf = dfu->i_buf_start;
Patrick Delaunay35be83e2017-07-19 16:39:24 +0200273 dfu->r_left = 0;
274 dfu->b_left = 0;
275 dfu->bad_skip = 0;
276
Stephen Warren7f450232014-06-11 12:48:08 -0600277 dfu->inited = 0;
278}
279
Patrick Delaunaydec4ecb2017-07-19 16:39:25 +0200280int dfu_transaction_initiate(struct dfu_entity *dfu, bool read)
281{
282 int ret = 0;
283
284 if (dfu->inited)
285 return 0;
286
287 dfu_transaction_cleanup(dfu);
288
289 if (dfu->i_buf_start == NULL)
290 return -ENOMEM;
291
292 dfu->i_buf_end = dfu->i_buf_start + dfu_get_buf_size();
293
294 if (read) {
295 ret = dfu->get_medium_size(dfu, &dfu->r_left);
296 if (ret < 0)
297 return ret;
298 debug("%s: %s %lld [B]\n", __func__, dfu->name, dfu->r_left);
299 }
300
301 dfu->inited = 1;
Patrick Delaunay773771f2019-10-14 09:28:07 +0200302 dfu_initiated_callback(dfu);
Patrick Delaunaydec4ecb2017-07-19 16:39:25 +0200303
304 return 0;
305}
306
Heiko Schocher941b3842014-03-18 08:09:55 +0100307int dfu_flush(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
308{
309 int ret = 0;
310
Przemyslaw Marczaka0f6a382014-05-12 12:05:33 +0200311 ret = dfu_write_buffer_drain(dfu);
312 if (ret)
313 return ret;
314
Heiko Schocher941b3842014-03-18 08:09:55 +0100315 if (dfu->flush_medium)
316 ret = dfu->flush_medium(dfu);
317
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200318 if (dfu_hash_algo)
319 printf("\nDFU complete %s: 0x%08x\n", dfu_hash_algo->name,
320 dfu->crc);
Heiko Schocher941b3842014-03-18 08:09:55 +0100321
Patrick Delaunay773771f2019-10-14 09:28:07 +0200322 dfu_flush_callback(dfu);
323
Patrick Delaunay35be83e2017-07-19 16:39:24 +0200324 dfu_transaction_cleanup(dfu);
Heiko Schocher941b3842014-03-18 08:09:55 +0100325
326 return ret;
327}
328
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200329int dfu_write(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
330{
Stephen Warren7f450232014-06-11 12:48:08 -0600331 int ret;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200332
Stephen Warrenc00315b2015-07-22 14:54:04 -0600333 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 +0000334 __func__, dfu->name, buf, size, blk_seq_num, dfu->offset,
Stephen Warrenc00315b2015-07-22 14:54:04 -0600335 (unsigned long)(dfu->i_buf - dfu->i_buf_start));
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200336
Patrick Delaunaydec4ecb2017-07-19 16:39:25 +0200337 ret = dfu_transaction_initiate(dfu, false);
338 if (ret < 0)
339 return ret;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200340
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000341 if (dfu->i_blk_seq_num != blk_seq_num) {
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200342 printf("%s: Wrong sequence number! [%d] [%d]\n",
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000343 __func__, dfu->i_blk_seq_num, blk_seq_num);
Patrick Delaunay35be83e2017-07-19 16:39:24 +0200344 dfu_transaction_cleanup(dfu);
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000345 return -1;
346 }
347
348 /* DFU 1.1 standard says:
349 * The wBlockNum field is a block sequence number. It increments each
350 * time a block is transferred, wrapping to zero from 65,535. It is used
351 * to provide useful context to the DFU loader in the device."
352 *
353 * This means that it's a 16 bit counter that roll-overs at
354 * 0xffff -> 0x0000. By having a typical 4K transfer block
355 * we roll-over at exactly 256MB. Not very fun to debug.
356 *
357 * Handling rollover, and having an inited variable,
358 * makes things work.
359 */
360
361 /* handle rollover */
362 dfu->i_blk_seq_num = (dfu->i_blk_seq_num + 1) & 0xffff;
363
364 /* flush buffer if overflow */
365 if ((dfu->i_buf + size) > dfu->i_buf_end) {
Stephen Warren7f450232014-06-11 12:48:08 -0600366 ret = dfu_write_buffer_drain(dfu);
367 if (ret) {
Patrick Delaunay35be83e2017-07-19 16:39:24 +0200368 dfu_transaction_cleanup(dfu);
Stephen Warren7f450232014-06-11 12:48:08 -0600369 return ret;
370 }
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000371 }
372
373 /* we should be in buffer now (if not then size too large) */
374 if ((dfu->i_buf + size) > dfu->i_buf_end) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900375 pr_err("Buffer overflow! (0x%p + 0x%x > 0x%p)\n", dfu->i_buf,
Lukasz Majewski385c3212013-09-10 15:29:22 +0200376 size, dfu->i_buf_end);
Patrick Delaunay35be83e2017-07-19 16:39:24 +0200377 dfu_transaction_cleanup(dfu);
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200378 return -1;
379 }
380
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000381 memcpy(dfu->i_buf, buf, size);
382 dfu->i_buf += size;
383
384 /* if end or if buffer full flush */
385 if (size == 0 || (dfu->i_buf + size) > dfu->i_buf_end) {
Stephen Warren7f450232014-06-11 12:48:08 -0600386 ret = dfu_write_buffer_drain(dfu);
387 if (ret) {
Patrick Delaunay35be83e2017-07-19 16:39:24 +0200388 dfu_transaction_cleanup(dfu);
Stephen Warren7f450232014-06-11 12:48:08 -0600389 return ret;
390 }
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000391 }
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200392
Stephen Warren7f450232014-06-11 12:48:08 -0600393 return 0;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200394}
395
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000396static int dfu_read_buffer_fill(struct dfu_entity *dfu, void *buf, int size)
397{
398 long chunk;
399 int ret, readn;
400
401 readn = 0;
402 while (size > 0) {
403 /* get chunk that can be read */
Masahiro Yamadadb204642014-11-07 03:03:31 +0900404 chunk = min((long)size, dfu->b_left);
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000405 /* consume */
406 if (chunk > 0) {
407 memcpy(buf, dfu->i_buf, chunk);
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200408 if (dfu_hash_algo)
409 dfu_hash_algo->hash_update(dfu_hash_algo,
410 &dfu->crc, buf,
411 chunk, 0);
412
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000413 dfu->i_buf += chunk;
414 dfu->b_left -= chunk;
415 size -= chunk;
416 buf += chunk;
417 readn += chunk;
418 }
419
420 /* all done */
421 if (size > 0) {
422 /* no more to read */
423 if (dfu->r_left == 0)
424 break;
425
426 dfu->i_buf = dfu->i_buf_start;
427 dfu->b_left = dfu->i_buf_end - dfu->i_buf_start;
428
429 /* got to read, but buffer is empty */
430 if (dfu->b_left > dfu->r_left)
431 dfu->b_left = dfu->r_left;
432 ret = dfu->read_medium(dfu, dfu->offset, dfu->i_buf,
433 &dfu->b_left);
434 if (ret != 0) {
435 debug("%s: Read error!\n", __func__);
436 return ret;
437 }
Patrick Delaunay7ef84362019-10-14 09:28:03 +0200438 if (dfu->b_left == 0)
439 break;
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000440 dfu->offset += dfu->b_left;
441 dfu->r_left -= dfu->b_left;
442
443 puts("#");
444 }
445 }
446
447 return readn;
448}
449
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200450int dfu_read(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
451{
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200452 int ret = 0;
453
454 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 +0000455 __func__, dfu->name, buf, size, blk_seq_num, dfu->i_buf);
456
Patrick Delaunaydec4ecb2017-07-19 16:39:25 +0200457 ret = dfu_transaction_initiate(dfu, true);
458 if (ret < 0)
459 return ret;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200460
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000461 if (dfu->i_blk_seq_num != blk_seq_num) {
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200462 printf("%s: Wrong sequence number! [%d] [%d]\n",
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000463 __func__, dfu->i_blk_seq_num, blk_seq_num);
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200464 return -1;
465 }
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000466 /* handle rollover */
467 dfu->i_blk_seq_num = (dfu->i_blk_seq_num + 1) & 0xffff;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200468
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000469 ret = dfu_read_buffer_fill(dfu, buf, size);
470 if (ret < 0) {
471 printf("%s: Failed to fill buffer\n", __func__);
472 return -1;
473 }
474
475 if (ret < size) {
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200476 if (dfu_hash_algo)
477 debug("%s: %s %s: 0x%x\n", __func__, dfu->name,
478 dfu_hash_algo->name, dfu->crc);
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000479 puts("\nUPLOAD ... done\nCtrl+C to exit ...\n");
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200480
Patrick Delaunay35be83e2017-07-19 16:39:24 +0200481 dfu_transaction_cleanup(dfu);
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200482 }
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000483
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200484 return ret;
485}
486
487static int dfu_fill_entity(struct dfu_entity *dfu, char *s, int alt,
Stephen Warren4afe50f2014-06-11 16:03:33 -0600488 char *interface, char *devstr)
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200489{
490 char *st;
491
Stephen Warren4afe50f2014-06-11 16:03:33 -0600492 debug("%s: %s interface: %s dev: %s\n", __func__, s, interface, devstr);
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200493 st = strsep(&s, " ");
494 strcpy(dfu->name, st);
495
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200496 dfu->alt = alt;
Stephen Warrenf9500982014-06-11 16:03:34 -0600497 dfu->max_buf_size = 0;
Stephen Warrena5f77f62014-06-11 16:03:35 -0600498 dfu->free_entity = NULL;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200499
500 /* Specific for mmc device */
501 if (strcmp(interface, "mmc") == 0) {
Stephen Warren4afe50f2014-06-11 16:03:33 -0600502 if (dfu_fill_entity_mmc(dfu, devstr, s))
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200503 return -1;
Patrick Delaunayf7aee232019-10-14 09:28:04 +0200504 } else if (strcmp(interface, "mtd") == 0) {
505 if (dfu_fill_entity_mtd(dfu, devstr, s))
506 return -1;
Pantelis Antonioucf14d0d2013-03-14 05:32:52 +0000507 } else if (strcmp(interface, "nand") == 0) {
Stephen Warren4afe50f2014-06-11 16:03:33 -0600508 if (dfu_fill_entity_nand(dfu, devstr, s))
Pantelis Antonioucf14d0d2013-03-14 05:32:52 +0000509 return -1;
Afzal Mohammede3c687a2013-09-18 01:15:24 +0530510 } else if (strcmp(interface, "ram") == 0) {
Stephen Warren4afe50f2014-06-11 16:03:33 -0600511 if (dfu_fill_entity_ram(dfu, devstr, s))
Afzal Mohammede3c687a2013-09-18 01:15:24 +0530512 return -1;
Stephen Warren0280af72014-06-11 16:03:36 -0600513 } else if (strcmp(interface, "sf") == 0) {
514 if (dfu_fill_entity_sf(dfu, devstr, s))
515 return -1;
Patrick Delaunay2111e3c2019-10-14 09:28:06 +0200516 } else if (strcmp(interface, "virt") == 0) {
517 if (dfu_fill_entity_virt(dfu, devstr, s))
518 return -1;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200519 } else {
520 printf("%s: Device %s not (yet) supported!\n",
521 __func__, interface);
522 return -1;
523 }
Stephen Warren0898da52015-09-04 22:03:46 -0600524 dfu_get_buf(dfu);
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200525
526 return 0;
527}
528
529void dfu_free_entities(void)
530{
531 struct dfu_entity *dfu, *p, *t = NULL;
532
Stephen Warren0898da52015-09-04 22:03:46 -0600533 dfu_free_buf();
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200534 list_for_each_entry_safe_reverse(dfu, p, &dfu_list, list) {
535 list_del(&dfu->list);
Stephen Warrena5f77f62014-06-11 16:03:35 -0600536 if (dfu->free_entity)
537 dfu->free_entity(dfu);
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200538 t = dfu;
539 }
540 if (t)
541 free(t);
542 INIT_LIST_HEAD(&dfu_list);
Przemyslaw Marczak4af3e082014-02-28 18:53:37 +0100543
544 alt_num_cnt = 0;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200545}
546
Patrick Delaunay0f8fad82019-10-14 09:28:01 +0200547int dfu_alt_init(int num, struct dfu_entity **dfu)
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200548{
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200549 char *s;
Patrick Delaunay0f8fad82019-10-14 09:28:01 +0200550 int ret;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200551
Patrick Delaunay0f8fad82019-10-14 09:28:01 +0200552 dfu_alt_num = num;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200553 debug("%s: dfu_alt_num=%d\n", __func__, dfu_alt_num);
554
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200555 dfu_hash_algo = NULL;
556 s = dfu_get_hash_algo();
557 if (s) {
558 ret = hash_lookup_algo(s, &dfu_hash_algo);
559 if (ret)
Masahiro Yamada81e10422017-09-16 14:10:41 +0900560 pr_err("Hash algorithm %s not supported\n", s);
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200561 }
562
Patrick Delaunay0f8fad82019-10-14 09:28:01 +0200563 *dfu = calloc(sizeof(struct dfu_entity), dfu_alt_num);
564 if (!*dfu)
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200565 return -1;
Patrick Delaunay0f8fad82019-10-14 09:28:01 +0200566
567 return 0;
568}
569
570int dfu_alt_add(struct dfu_entity *dfu, char *interface, char *devstr, char *s)
571{
572 struct dfu_entity *p_dfu;
573 int ret;
574
575 if (alt_num_cnt >= dfu_alt_num)
576 return -1;
577
578 p_dfu = &dfu[alt_num_cnt];
579 ret = dfu_fill_entity(p_dfu, s, alt_num_cnt, interface, devstr);
580 if (ret)
581 return -1;
582
583 list_add_tail(&p_dfu->list, &dfu_list);
584 alt_num_cnt++;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200585
Patrick Delaunay0f8fad82019-10-14 09:28:01 +0200586 return 0;
587}
588
589int dfu_config_entities(char *env, char *interface, char *devstr)
590{
591 struct dfu_entity *dfu;
592 int i, ret;
593 char *s;
594
595 ret = dfu_alt_init(dfu_find_alt_num(env), &dfu);
596 if (ret)
597 return -1;
598
599 for (i = 0; i < dfu_alt_num; i++) {
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200600 s = strsep(&env, ";");
Patrick Delaunay0f8fad82019-10-14 09:28:01 +0200601 ret = dfu_alt_add(dfu, interface, devstr, s);
Peng Fan529d4fd2016-05-03 10:24:52 +0800602 if (ret) {
Sam Protsenko6d29e5c2018-07-13 16:35:45 +0300603 /* We will free "dfu" in dfu_free_entities() */
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200604 return -1;
Peng Fan529d4fd2016-05-03 10:24:52 +0800605 }
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200606 }
607
608 return 0;
609}
610
611const char *dfu_get_dev_type(enum dfu_device_type t)
612{
Patrick Delaunayae224c32019-10-14 09:27:58 +0200613 const char *const dev_t[] = {NULL, "eMMC", "OneNAND", "NAND", "RAM",
Patrick Delaunay2111e3c2019-10-14 09:28:06 +0200614 "SF", "MTD", "VIRT"};
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200615 return dev_t[t];
616}
617
618const char *dfu_get_layout(enum dfu_layout l)
619{
Patrick Delaunayae224c32019-10-14 09:27:58 +0200620 const char *const dfu_layout[] = {NULL, "RAW_ADDR", "FAT", "EXT2",
Marek Szyprowskib1fa1ad2020-12-22 11:32:22 +0100621 "EXT3", "EXT4", "RAM_ADDR", "SKIP",
622 "SCRIPT" };
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200623 return dfu_layout[l];
624}
625
626void dfu_show_entities(void)
627{
628 struct dfu_entity *dfu;
629
630 puts("DFU alt settings list:\n");
631
632 list_for_each_entry(dfu, &dfu_list, list) {
633 printf("dev: %s alt: %d name: %s layout: %s\n",
634 dfu_get_dev_type(dfu->dev_type), dfu->alt,
635 dfu->name, dfu_get_layout(dfu->layout));
636 }
637}
638
639int dfu_get_alt_number(void)
640{
641 return dfu_alt_num;
642}
643
644struct dfu_entity *dfu_get_entity(int alt)
645{
646 struct dfu_entity *dfu;
647
648 list_for_each_entry(dfu, &dfu_list, list) {
649 if (dfu->alt == alt)
650 return dfu;
651 }
652
653 return NULL;
654}
Lukasz Majewski1737f0f2013-10-08 14:30:38 +0200655
656int dfu_get_alt(char *name)
657{
658 struct dfu_entity *dfu;
Lukasz Majewski3a39d012014-11-05 10:54:16 +0100659 char *str;
Lukasz Majewski1737f0f2013-10-08 14:30:38 +0200660
661 list_for_each_entry(dfu, &dfu_list, list) {
Lukasz Majewski3a39d012014-11-05 10:54:16 +0100662 if (dfu->name[0] != '/') {
663 if (!strncmp(dfu->name, name, strlen(dfu->name)))
664 return dfu->alt;
665 } else {
666 /*
667 * One must also consider absolute path
668 * (/boot/bin/uImage) available at dfu->name when
669 * compared "plain" file name (uImage)
670 *
671 * It is the case for e.g. thor gadget where lthor SW
672 * sends only the file name, so only the very last part
673 * of path must be checked for equality
674 */
675
676 str = strstr(dfu->name, name);
677 if (!str)
678 continue;
679
680 /*
681 * Check if matching substring is the last element of
682 * dfu->name (uImage)
683 */
684 if (strlen(dfu->name) ==
685 ((str - dfu->name) + strlen(name)))
686 return dfu->alt;
687 }
Lukasz Majewski1737f0f2013-10-08 14:30:38 +0200688 }
689
690 return -ENODEV;
691}
Lukasz Majewskicc2d0262015-08-24 00:21:46 +0200692
693int dfu_write_from_mem_addr(struct dfu_entity *dfu, void *buf, int size)
694{
695 unsigned long dfu_buf_size, write, left = size;
696 int i, ret = 0;
697 void *dp = buf;
698
699 /*
700 * Here we must call dfu_get_buf(dfu) first to be sure that dfu_buf_size
701 * has been properly initialized - e.g. if "dfu_bufsiz" has been taken
702 * into account.
703 */
704 dfu_get_buf(dfu);
705 dfu_buf_size = dfu_get_buf_size();
706 debug("%s: dfu buf size: %lu\n", __func__, dfu_buf_size);
707
708 for (i = 0; left > 0; i++) {
709 write = min(dfu_buf_size, left);
710
711 debug("%s: dp: 0x%p left: %lu write: %lu\n", __func__,
712 dp, left, write);
713 ret = dfu_write(dfu, dp, write, i);
714 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900715 pr_err("DFU write failed\n");
Lukasz Majewskicc2d0262015-08-24 00:21:46 +0200716 return ret;
717 }
718
719 dp += write;
720 left -= write;
721 }
722
723 ret = dfu_flush(dfu, NULL, 0, i);
724 if (ret)
Masahiro Yamada81e10422017-09-16 14:10:41 +0900725 pr_err("DFU flush failed!");
Lukasz Majewskicc2d0262015-08-24 00:21:46 +0200726
727 return ret;
728}