blob: b2ee5f1ede6d16f83bde2446c64db22688944fdb [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/*
Patrick Delaunayf1357352021-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 Majewskid9eae702014-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 Majewski2968aa22013-07-18 13:19:14 +020067{
B, Ravi4cca5cf2017-05-04 15:45:29 +053068#ifdef CONFIG_SPL_DFU_NO_RESET
69 return false;
70#else
Lukasz Majewskid9eae702014-08-25 11:07:28 +020071 return true;
B, Ravi4cca5cf2017-05-04 15:45:29 +053072#endif
Lukasz Majewski2968aa22013-07-18 13:19:14 +020073}
74
Andy Shevchenko1e414012019-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 Majewskid311c6e2012-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 Delaunay06164032019-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;
Masami Hiramatsuf815c332022-01-31 11:52:29 +0900126 i = strsep(&s, " \t");
Patrick Delaunay06164032019-10-14 09:28:02 +0200127 if (!i)
128 break;
Masami Hiramatsuf815c332022-01-31 11:52:29 +0900129 s = skip_spaces(s);
Patrick Delaunay06164032019-10-14 09:28:02 +0200130 d = strsep(&s, "=");
131 if (!d)
132 break;
133 a = strsep(&s, "&");
134 if (!a)
135 a = s;
136 do {
137 part = strsep(&a, ";");
Ravi Gunasekaran9ed024d2023-05-18 12:06:23 +0530138 part = skip_spaces(part);
Patrick Delaunay06164032019-10-14 09:28:02 +0200139 ret = dfu_alt_add(dfu, i, d, part);
140 if (ret)
141 return ret;
142 } while (a);
143 }
144
145 return ret;
146}
147
Stephen Warren4afe50f2014-06-11 16:03:33 -0600148int dfu_init_env_entities(char *interface, char *devstr)
Lukasz Majewski35e591e2013-09-11 14:53:35 +0200149{
150 const char *str_env;
151 char *env_bkp;
Sam Protsenko6500cde2018-07-13 16:35:46 +0300152 int ret = 0;
Lukasz Majewski35e591e2013-09-11 14:53:35 +0200153
Marek Szyprowskib1fa1ad2020-12-22 11:32:22 +0100154 dfu_reinit_needed = false;
155
Przemyslaw Marczakd0dd6092015-02-17 12:24:11 +0100156#ifdef CONFIG_SET_DFU_ALT_INFO
157 set_dfu_alt_info(interface, devstr);
158#endif
Simon Glass64b723f2017-08-03 12:22:12 -0600159 str_env = env_get("dfu_alt_info");
Lukasz Majewski35e591e2013-09-11 14:53:35 +0200160 if (!str_env) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900161 pr_err("\"dfu_alt_info\" env variable not defined!\n");
Lukasz Majewski35e591e2013-09-11 14:53:35 +0200162 return -EINVAL;
163 }
164
165 env_bkp = strdup(str_env);
Patrick Delaunay06164032019-10-14 09:28:02 +0200166 if (!interface && !devstr)
167 ret = dfu_config_interfaces(env_bkp);
168 else
169 ret = dfu_config_entities(env_bkp, interface, devstr);
170
Lukasz Majewski35e591e2013-09-11 14:53:35 +0200171 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900172 pr_err("DFU entities configuration failed!\n");
Sam Protsenkoab79fb02018-07-13 16:35:47 +0300173 pr_err("(partition table does not match dfu_alt_info?)\n");
Sam Protsenko6500cde2018-07-13 16:35:46 +0300174 goto done;
Lukasz Majewski35e591e2013-09-11 14:53:35 +0200175 }
176
Sam Protsenko6500cde2018-07-13 16:35:46 +0300177done:
Lukasz Majewski35e591e2013-09-11 14:53:35 +0200178 free(env_bkp);
Sam Protsenko6500cde2018-07-13 16:35:46 +0300179 return ret;
Lukasz Majewski35e591e2013-09-11 14:53:35 +0200180}
181
Heiko Schochera2f831e2013-06-12 06:05:51 +0200182static unsigned char *dfu_buf;
Lukasz Majewski0af4df82015-07-08 23:43:18 +0200183static unsigned long dfu_buf_size;
Patrick Delaunay06164032019-10-14 09:28:02 +0200184static enum dfu_device_type dfu_buf_device_type;
Heiko Schochera2f831e2013-06-12 06:05:51 +0200185
Lukasz Majewski34264232013-10-08 14:30:39 +0200186unsigned char *dfu_free_buf(void)
Heiko Schochera2f831e2013-06-12 06:05:51 +0200187{
188 free(dfu_buf);
189 dfu_buf = NULL;
190 return dfu_buf;
191}
192
Lukasz Majewski031d4132013-12-09 16:20:13 +0100193unsigned long dfu_get_buf_size(void)
194{
195 return dfu_buf_size;
196}
197
Stephen Warrenf9500982014-06-11 16:03:34 -0600198unsigned char *dfu_get_buf(struct dfu_entity *dfu)
Heiko Schochera2f831e2013-06-12 06:05:51 +0200199{
200 char *s;
201
Patrick Delaunay06164032019-10-14 09:28:02 +0200202 /* manage several entity with several contraint */
203 if (dfu_buf && dfu->dev_type != dfu_buf_device_type)
204 dfu_free_buf();
205
Heiko Schochera2f831e2013-06-12 06:05:51 +0200206 if (dfu_buf != NULL)
207 return dfu_buf;
208
Simon Glass64b723f2017-08-03 12:22:12 -0600209 s = env_get("dfu_bufsiz");
Przemyslaw Marczak24883502014-12-15 10:34:11 +0100210 if (s)
211 dfu_buf_size = (unsigned long)simple_strtol(s, NULL, 0);
212
213 if (!s || !dfu_buf_size)
214 dfu_buf_size = CONFIG_SYS_DFU_DATA_BUF_SIZE;
215
Stephen Warrenf9500982014-06-11 16:03:34 -0600216 if (dfu->max_buf_size && dfu_buf_size > dfu->max_buf_size)
217 dfu_buf_size = dfu->max_buf_size;
Heiko Schochera2f831e2013-06-12 06:05:51 +0200218
219 dfu_buf = memalign(CONFIG_SYS_CACHELINE_SIZE, dfu_buf_size);
220 if (dfu_buf == NULL)
221 printf("%s: Could not memalign 0x%lx bytes\n",
222 __func__, dfu_buf_size);
223
Patrick Delaunay06164032019-10-14 09:28:02 +0200224 dfu_buf_device_type = dfu->dev_type;
Heiko Schochera2f831e2013-06-12 06:05:51 +0200225 return dfu_buf;
226}
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200227
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200228static char *dfu_get_hash_algo(void)
229{
230 char *s;
231
Simon Glass64b723f2017-08-03 12:22:12 -0600232 s = env_get("dfu_hash_algo");
Lukasz Majewski3e0d0282014-06-10 12:25:59 +0200233 if (!s)
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200234 return NULL;
235
Lukasz Majewski3e0d0282014-06-10 12:25:59 +0200236 if (!strcmp(s, "crc32")) {
237 debug("%s: DFU hash method: %s\n", __func__, s);
238 return s;
239 }
240
Masahiro Yamada81e10422017-09-16 14:10:41 +0900241 pr_err("DFU hash method: %s not supported!\n", s);
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200242 return NULL;
243}
244
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000245static int dfu_write_buffer_drain(struct dfu_entity *dfu)
246{
247 long w_size;
248 int ret;
249
250 /* flush size? */
251 w_size = dfu->i_buf - dfu->i_buf_start;
252 if (w_size == 0)
253 return 0;
254
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200255 if (dfu_hash_algo)
256 dfu_hash_algo->hash_update(dfu_hash_algo, &dfu->crc,
257 dfu->i_buf_start, w_size, 0);
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000258
259 ret = dfu->write_medium(dfu, dfu->offset, dfu->i_buf_start, &w_size);
260 if (ret)
261 debug("%s: Write error!\n", __func__);
262
263 /* point back */
264 dfu->i_buf = dfu->i_buf_start;
265
266 /* update offset */
267 dfu->offset += w_size;
268
269 puts("#");
270
271 return ret;
272}
273
Patrick Delaunay35be83e2017-07-19 16:39:24 +0200274void dfu_transaction_cleanup(struct dfu_entity *dfu)
Stephen Warren7f450232014-06-11 12:48:08 -0600275{
276 /* clear everything */
Stephen Warren7f450232014-06-11 12:48:08 -0600277 dfu->crc = 0;
278 dfu->offset = 0;
279 dfu->i_blk_seq_num = 0;
Patrick Delaunaydec4ecb2017-07-19 16:39:25 +0200280 dfu->i_buf_start = dfu_get_buf(dfu);
281 dfu->i_buf_end = dfu->i_buf_start;
Stephen Warren7f450232014-06-11 12:48:08 -0600282 dfu->i_buf = dfu->i_buf_start;
Patrick Delaunay35be83e2017-07-19 16:39:24 +0200283 dfu->r_left = 0;
284 dfu->b_left = 0;
285 dfu->bad_skip = 0;
286
Stephen Warren7f450232014-06-11 12:48:08 -0600287 dfu->inited = 0;
288}
289
Patrick Delaunaydec4ecb2017-07-19 16:39:25 +0200290int dfu_transaction_initiate(struct dfu_entity *dfu, bool read)
291{
292 int ret = 0;
293
294 if (dfu->inited)
295 return 0;
296
297 dfu_transaction_cleanup(dfu);
298
299 if (dfu->i_buf_start == NULL)
300 return -ENOMEM;
301
302 dfu->i_buf_end = dfu->i_buf_start + dfu_get_buf_size();
303
304 if (read) {
305 ret = dfu->get_medium_size(dfu, &dfu->r_left);
306 if (ret < 0)
307 return ret;
308 debug("%s: %s %lld [B]\n", __func__, dfu->name, dfu->r_left);
309 }
310
311 dfu->inited = 1;
Patrick Delaunay773771f2019-10-14 09:28:07 +0200312 dfu_initiated_callback(dfu);
Patrick Delaunaydec4ecb2017-07-19 16:39:25 +0200313
314 return 0;
315}
316
Heiko Schocher941b3842014-03-18 08:09:55 +0100317int dfu_flush(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
318{
319 int ret = 0;
320
Przemyslaw Marczaka0f6a382014-05-12 12:05:33 +0200321 ret = dfu_write_buffer_drain(dfu);
322 if (ret)
323 return ret;
324
Heiko Schocher941b3842014-03-18 08:09:55 +0100325 if (dfu->flush_medium)
326 ret = dfu->flush_medium(dfu);
327
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200328 if (dfu_hash_algo)
329 printf("\nDFU complete %s: 0x%08x\n", dfu_hash_algo->name,
330 dfu->crc);
Heiko Schocher941b3842014-03-18 08:09:55 +0100331
Patrick Delaunay773771f2019-10-14 09:28:07 +0200332 dfu_flush_callback(dfu);
333
Patrick Delaunay35be83e2017-07-19 16:39:24 +0200334 dfu_transaction_cleanup(dfu);
Heiko Schocher941b3842014-03-18 08:09:55 +0100335
336 return ret;
337}
338
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200339int dfu_write(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
340{
Stephen Warren7f450232014-06-11 12:48:08 -0600341 int ret;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200342
Stephen Warrenc00315b2015-07-22 14:54:04 -0600343 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 +0000344 __func__, dfu->name, buf, size, blk_seq_num, dfu->offset,
Stephen Warrenc00315b2015-07-22 14:54:04 -0600345 (unsigned long)(dfu->i_buf - dfu->i_buf_start));
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200346
Patrick Delaunaydec4ecb2017-07-19 16:39:25 +0200347 ret = dfu_transaction_initiate(dfu, false);
348 if (ret < 0)
349 return ret;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200350
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000351 if (dfu->i_blk_seq_num != blk_seq_num) {
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200352 printf("%s: Wrong sequence number! [%d] [%d]\n",
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000353 __func__, dfu->i_blk_seq_num, blk_seq_num);
Patrick Delaunay35be83e2017-07-19 16:39:24 +0200354 dfu_transaction_cleanup(dfu);
Patrick Delaunayf1357352021-05-18 15:12:12 +0200355 dfu_error_callback(dfu, "Wrong sequence number");
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000356 return -1;
357 }
358
359 /* DFU 1.1 standard says:
360 * The wBlockNum field is a block sequence number. It increments each
361 * time a block is transferred, wrapping to zero from 65,535. It is used
362 * to provide useful context to the DFU loader in the device."
363 *
364 * This means that it's a 16 bit counter that roll-overs at
365 * 0xffff -> 0x0000. By having a typical 4K transfer block
366 * we roll-over at exactly 256MB. Not very fun to debug.
367 *
368 * Handling rollover, and having an inited variable,
369 * makes things work.
370 */
371
372 /* handle rollover */
373 dfu->i_blk_seq_num = (dfu->i_blk_seq_num + 1) & 0xffff;
374
375 /* flush buffer if overflow */
376 if ((dfu->i_buf + size) > dfu->i_buf_end) {
Stephen Warren7f450232014-06-11 12:48:08 -0600377 ret = dfu_write_buffer_drain(dfu);
378 if (ret) {
Patrick Delaunay35be83e2017-07-19 16:39:24 +0200379 dfu_transaction_cleanup(dfu);
Patrick Delaunayf1357352021-05-18 15:12:12 +0200380 dfu_error_callback(dfu, "DFU write error");
Stephen Warren7f450232014-06-11 12:48:08 -0600381 return ret;
382 }
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000383 }
384
385 /* we should be in buffer now (if not then size too large) */
386 if ((dfu->i_buf + size) > dfu->i_buf_end) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900387 pr_err("Buffer overflow! (0x%p + 0x%x > 0x%p)\n", dfu->i_buf,
Lukasz Majewski385c3212013-09-10 15:29:22 +0200388 size, dfu->i_buf_end);
Patrick Delaunay35be83e2017-07-19 16:39:24 +0200389 dfu_transaction_cleanup(dfu);
Patrick Delaunayf1357352021-05-18 15:12:12 +0200390 dfu_error_callback(dfu, "Buffer overflow");
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200391 return -1;
392 }
393
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000394 memcpy(dfu->i_buf, buf, size);
395 dfu->i_buf += size;
396
397 /* if end or if buffer full flush */
398 if (size == 0 || (dfu->i_buf + size) > dfu->i_buf_end) {
Stephen Warren7f450232014-06-11 12:48:08 -0600399 ret = dfu_write_buffer_drain(dfu);
400 if (ret) {
Patrick Delaunay35be83e2017-07-19 16:39:24 +0200401 dfu_transaction_cleanup(dfu);
Patrick Delaunayf1357352021-05-18 15:12:12 +0200402 dfu_error_callback(dfu, "DFU write error");
Stephen Warren7f450232014-06-11 12:48:08 -0600403 return ret;
404 }
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000405 }
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200406
Stephen Warren7f450232014-06-11 12:48:08 -0600407 return 0;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200408}
409
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000410static int dfu_read_buffer_fill(struct dfu_entity *dfu, void *buf, int size)
411{
412 long chunk;
413 int ret, readn;
414
415 readn = 0;
416 while (size > 0) {
417 /* get chunk that can be read */
Masahiro Yamadadb204642014-11-07 03:03:31 +0900418 chunk = min((long)size, dfu->b_left);
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000419 /* consume */
420 if (chunk > 0) {
421 memcpy(buf, dfu->i_buf, chunk);
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200422 if (dfu_hash_algo)
423 dfu_hash_algo->hash_update(dfu_hash_algo,
424 &dfu->crc, buf,
425 chunk, 0);
426
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000427 dfu->i_buf += chunk;
428 dfu->b_left -= chunk;
429 size -= chunk;
430 buf += chunk;
431 readn += chunk;
432 }
433
434 /* all done */
435 if (size > 0) {
436 /* no more to read */
437 if (dfu->r_left == 0)
438 break;
439
440 dfu->i_buf = dfu->i_buf_start;
441 dfu->b_left = dfu->i_buf_end - dfu->i_buf_start;
442
443 /* got to read, but buffer is empty */
444 if (dfu->b_left > dfu->r_left)
445 dfu->b_left = dfu->r_left;
446 ret = dfu->read_medium(dfu, dfu->offset, dfu->i_buf,
447 &dfu->b_left);
448 if (ret != 0) {
449 debug("%s: Read error!\n", __func__);
450 return ret;
451 }
Patrick Delaunay7ef84362019-10-14 09:28:03 +0200452 if (dfu->b_left == 0)
453 break;
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000454 dfu->offset += dfu->b_left;
455 dfu->r_left -= dfu->b_left;
456
457 puts("#");
458 }
459 }
460
461 return readn;
462}
463
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200464int dfu_read(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
465{
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200466 int ret = 0;
467
468 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 +0000469 __func__, dfu->name, buf, size, blk_seq_num, dfu->i_buf);
470
Patrick Delaunaydec4ecb2017-07-19 16:39:25 +0200471 ret = dfu_transaction_initiate(dfu, true);
472 if (ret < 0)
473 return ret;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200474
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000475 if (dfu->i_blk_seq_num != blk_seq_num) {
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200476 printf("%s: Wrong sequence number! [%d] [%d]\n",
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000477 __func__, dfu->i_blk_seq_num, blk_seq_num);
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200478 return -1;
479 }
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000480 /* handle rollover */
481 dfu->i_blk_seq_num = (dfu->i_blk_seq_num + 1) & 0xffff;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200482
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000483 ret = dfu_read_buffer_fill(dfu, buf, size);
484 if (ret < 0) {
485 printf("%s: Failed to fill buffer\n", __func__);
486 return -1;
487 }
488
489 if (ret < size) {
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200490 if (dfu_hash_algo)
491 debug("%s: %s %s: 0x%x\n", __func__, dfu->name,
492 dfu_hash_algo->name, dfu->crc);
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000493 puts("\nUPLOAD ... done\nCtrl+C to exit ...\n");
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200494
Patrick Delaunay35be83e2017-07-19 16:39:24 +0200495 dfu_transaction_cleanup(dfu);
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200496 }
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000497
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200498 return ret;
499}
500
501static int dfu_fill_entity(struct dfu_entity *dfu, char *s, int alt,
Stephen Warren4afe50f2014-06-11 16:03:33 -0600502 char *interface, char *devstr)
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200503{
Masami Hiramatsufa4282a2022-01-31 11:52:37 +0900504 char *argv[DFU_MAX_ENTITY_ARGS];
505 int argc;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200506 char *st;
507
Stephen Warren4afe50f2014-06-11 16:03:33 -0600508 debug("%s: %s interface: %s dev: %s\n", __func__, s, interface, devstr);
Masami Hiramatsuf815c332022-01-31 11:52:29 +0900509 st = strsep(&s, " \t");
Masami Hiramatsu020bb2c2022-01-31 11:52:20 +0900510 strlcpy(dfu->name, st, DFU_NAME_SIZE);
Masami Hiramatsufa4282a2022-01-31 11:52:37 +0900511
512 /* Parse arguments */
513 for (argc = 0; s && argc < DFU_MAX_ENTITY_ARGS; argc++) {
514 s = skip_spaces(s);
515 if (!*s)
516 break;
517 argv[argc] = strsep(&s, " \t");
518 }
519
520 if (argc == DFU_MAX_ENTITY_ARGS && s) {
521 s = skip_spaces(s);
522 if (*s) {
523 log_err("Too many arguments for %s\n", dfu->name);
524 return -EINVAL;
525 }
526 }
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200527
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200528 dfu->alt = alt;
Stephen Warrenf9500982014-06-11 16:03:34 -0600529 dfu->max_buf_size = 0;
Stephen Warrena5f77f62014-06-11 16:03:35 -0600530 dfu->free_entity = NULL;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200531
532 /* Specific for mmc device */
533 if (strcmp(interface, "mmc") == 0) {
Masami Hiramatsufa4282a2022-01-31 11:52:37 +0900534 if (dfu_fill_entity_mmc(dfu, devstr, argv, argc))
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200535 return -1;
Patrick Delaunayf7aee232019-10-14 09:28:04 +0200536 } else if (strcmp(interface, "mtd") == 0) {
Masami Hiramatsufa4282a2022-01-31 11:52:37 +0900537 if (dfu_fill_entity_mtd(dfu, devstr, argv, argc))
Patrick Delaunayf7aee232019-10-14 09:28:04 +0200538 return -1;
Pantelis Antonioucf14d0d2013-03-14 05:32:52 +0000539 } else if (strcmp(interface, "nand") == 0) {
Masami Hiramatsufa4282a2022-01-31 11:52:37 +0900540 if (dfu_fill_entity_nand(dfu, devstr, argv, argc))
Pantelis Antonioucf14d0d2013-03-14 05:32:52 +0000541 return -1;
Afzal Mohammede3c687a2013-09-18 01:15:24 +0530542 } else if (strcmp(interface, "ram") == 0) {
Masami Hiramatsufa4282a2022-01-31 11:52:37 +0900543 if (dfu_fill_entity_ram(dfu, devstr, argv, argc))
Afzal Mohammede3c687a2013-09-18 01:15:24 +0530544 return -1;
Stephen Warren0280af72014-06-11 16:03:36 -0600545 } else if (strcmp(interface, "sf") == 0) {
Masami Hiramatsufa4282a2022-01-31 11:52:37 +0900546 if (dfu_fill_entity_sf(dfu, devstr, argv, argc))
Stephen Warren0280af72014-06-11 16:03:36 -0600547 return -1;
Patrick Delaunay2111e3c2019-10-14 09:28:06 +0200548 } else if (strcmp(interface, "virt") == 0) {
Masami Hiramatsufa4282a2022-01-31 11:52:37 +0900549 if (dfu_fill_entity_virt(dfu, devstr, argv, argc))
Patrick Delaunay2111e3c2019-10-14 09:28:06 +0200550 return -1;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200551 } else {
552 printf("%s: Device %s not (yet) supported!\n",
553 __func__, interface);
554 return -1;
555 }
Stephen Warren0898da52015-09-04 22:03:46 -0600556 dfu_get_buf(dfu);
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200557
558 return 0;
559}
560
561void dfu_free_entities(void)
562{
563 struct dfu_entity *dfu, *p, *t = NULL;
564
Stephen Warren0898da52015-09-04 22:03:46 -0600565 dfu_free_buf();
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200566 list_for_each_entry_safe_reverse(dfu, p, &dfu_list, list) {
567 list_del(&dfu->list);
Stephen Warrena5f77f62014-06-11 16:03:35 -0600568 if (dfu->free_entity)
569 dfu->free_entity(dfu);
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200570 t = dfu;
571 }
572 if (t)
573 free(t);
574 INIT_LIST_HEAD(&dfu_list);
Przemyslaw Marczak4af3e082014-02-28 18:53:37 +0100575
576 alt_num_cnt = 0;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200577}
578
Patrick Delaunay0f8fad82019-10-14 09:28:01 +0200579int dfu_alt_init(int num, struct dfu_entity **dfu)
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200580{
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200581 char *s;
Patrick Delaunay0f8fad82019-10-14 09:28:01 +0200582 int ret;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200583
Patrick Delaunay0f8fad82019-10-14 09:28:01 +0200584 dfu_alt_num = num;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200585 debug("%s: dfu_alt_num=%d\n", __func__, dfu_alt_num);
586
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200587 dfu_hash_algo = NULL;
588 s = dfu_get_hash_algo();
589 if (s) {
590 ret = hash_lookup_algo(s, &dfu_hash_algo);
591 if (ret)
Masahiro Yamada81e10422017-09-16 14:10:41 +0900592 pr_err("Hash algorithm %s not supported\n", s);
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200593 }
594
Patrick Delaunay0f8fad82019-10-14 09:28:01 +0200595 *dfu = calloc(sizeof(struct dfu_entity), dfu_alt_num);
596 if (!*dfu)
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200597 return -1;
Patrick Delaunay0f8fad82019-10-14 09:28:01 +0200598
599 return 0;
600}
601
602int dfu_alt_add(struct dfu_entity *dfu, char *interface, char *devstr, char *s)
603{
604 struct dfu_entity *p_dfu;
605 int ret;
606
607 if (alt_num_cnt >= dfu_alt_num)
608 return -1;
609
610 p_dfu = &dfu[alt_num_cnt];
611 ret = dfu_fill_entity(p_dfu, s, alt_num_cnt, interface, devstr);
612 if (ret)
613 return -1;
614
615 list_add_tail(&p_dfu->list, &dfu_list);
616 alt_num_cnt++;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200617
Patrick Delaunay0f8fad82019-10-14 09:28:01 +0200618 return 0;
619}
620
621int dfu_config_entities(char *env, char *interface, char *devstr)
622{
623 struct dfu_entity *dfu;
624 int i, ret;
625 char *s;
626
627 ret = dfu_alt_init(dfu_find_alt_num(env), &dfu);
628 if (ret)
629 return -1;
630
631 for (i = 0; i < dfu_alt_num; i++) {
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200632 s = strsep(&env, ";");
Ravi Gunasekaran9ed024d2023-05-18 12:06:23 +0530633 s = skip_spaces(s);
Patrick Delaunay0f8fad82019-10-14 09:28:01 +0200634 ret = dfu_alt_add(dfu, interface, devstr, s);
Peng Fan529d4fd2016-05-03 10:24:52 +0800635 if (ret) {
Sam Protsenko6d29e5c2018-07-13 16:35:45 +0300636 /* We will free "dfu" in dfu_free_entities() */
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200637 return -1;
Peng Fan529d4fd2016-05-03 10:24:52 +0800638 }
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200639 }
640
641 return 0;
642}
643
644const char *dfu_get_dev_type(enum dfu_device_type t)
645{
Patrick Delaunayae224c32019-10-14 09:27:58 +0200646 const char *const dev_t[] = {NULL, "eMMC", "OneNAND", "NAND", "RAM",
Patrick Delaunay2111e3c2019-10-14 09:28:06 +0200647 "SF", "MTD", "VIRT"};
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200648 return dev_t[t];
649}
650
651const char *dfu_get_layout(enum dfu_layout l)
652{
Patrick Delaunayae224c32019-10-14 09:27:58 +0200653 const char *const dfu_layout[] = {NULL, "RAW_ADDR", "FAT", "EXT2",
Marek Szyprowskib1fa1ad2020-12-22 11:32:22 +0100654 "EXT3", "EXT4", "RAM_ADDR", "SKIP",
655 "SCRIPT" };
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200656 return dfu_layout[l];
657}
658
659void dfu_show_entities(void)
660{
661 struct dfu_entity *dfu;
662
663 puts("DFU alt settings list:\n");
664
665 list_for_each_entry(dfu, &dfu_list, list) {
666 printf("dev: %s alt: %d name: %s layout: %s\n",
667 dfu_get_dev_type(dfu->dev_type), dfu->alt,
668 dfu->name, dfu_get_layout(dfu->layout));
669 }
670}
671
672int dfu_get_alt_number(void)
673{
674 return dfu_alt_num;
675}
676
677struct dfu_entity *dfu_get_entity(int alt)
678{
679 struct dfu_entity *dfu;
680
681 list_for_each_entry(dfu, &dfu_list, list) {
682 if (dfu->alt == alt)
683 return dfu;
684 }
685
686 return NULL;
687}
Lukasz Majewski1737f0f2013-10-08 14:30:38 +0200688
689int dfu_get_alt(char *name)
690{
691 struct dfu_entity *dfu;
Lukasz Majewski3a39d012014-11-05 10:54:16 +0100692 char *str;
Lukasz Majewski1737f0f2013-10-08 14:30:38 +0200693
694 list_for_each_entry(dfu, &dfu_list, list) {
Lukasz Majewski3a39d012014-11-05 10:54:16 +0100695 if (dfu->name[0] != '/') {
696 if (!strncmp(dfu->name, name, strlen(dfu->name)))
697 return dfu->alt;
698 } else {
699 /*
700 * One must also consider absolute path
701 * (/boot/bin/uImage) available at dfu->name when
702 * compared "plain" file name (uImage)
703 *
704 * It is the case for e.g. thor gadget where lthor SW
705 * sends only the file name, so only the very last part
706 * of path must be checked for equality
707 */
708
709 str = strstr(dfu->name, name);
710 if (!str)
711 continue;
712
713 /*
714 * Check if matching substring is the last element of
715 * dfu->name (uImage)
716 */
717 if (strlen(dfu->name) ==
718 ((str - dfu->name) + strlen(name)))
719 return dfu->alt;
720 }
Lukasz Majewski1737f0f2013-10-08 14:30:38 +0200721 }
722
723 return -ENODEV;
724}
Lukasz Majewskicc2d0262015-08-24 00:21:46 +0200725
726int dfu_write_from_mem_addr(struct dfu_entity *dfu, void *buf, int size)
727{
728 unsigned long dfu_buf_size, write, left = size;
729 int i, ret = 0;
730 void *dp = buf;
731
732 /*
733 * Here we must call dfu_get_buf(dfu) first to be sure that dfu_buf_size
734 * has been properly initialized - e.g. if "dfu_bufsiz" has been taken
735 * into account.
736 */
737 dfu_get_buf(dfu);
738 dfu_buf_size = dfu_get_buf_size();
739 debug("%s: dfu buf size: %lu\n", __func__, dfu_buf_size);
740
741 for (i = 0; left > 0; i++) {
742 write = min(dfu_buf_size, left);
743
744 debug("%s: dp: 0x%p left: %lu write: %lu\n", __func__,
745 dp, left, write);
746 ret = dfu_write(dfu, dp, write, i);
747 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900748 pr_err("DFU write failed\n");
Lukasz Majewskicc2d0262015-08-24 00:21:46 +0200749 return ret;
750 }
751
752 dp += write;
753 left -= write;
754 }
755
756 ret = dfu_flush(dfu, NULL, 0, i);
757 if (ret)
Masahiro Yamada81e10422017-09-16 14:10:41 +0900758 pr_err("DFU flush failed!");
Heinrich Schuchardt54fb1fa2021-03-05 18:36:37 +0100759 puts("\n");
Lukasz Majewskicc2d0262015-08-24 00:21:46 +0200760
761 return ret;
762}