blob: 7a4d7ba2a7ff080307bbc0072edb82ccf8a2da36 [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
Simon Glass0af6e2d2019-08-01 09:46:52 -06009#include <env.h>
Heiko Schochera2f831e2013-06-12 06:05:51 +020010#include <errno.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Lukasz Majewskid311c6e2012-08-06 14:41:07 +020012#include <malloc.h>
13#include <mmc.h>
14#include <fat.h>
15#include <dfu.h>
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +020016#include <hash.h>
Lukasz Majewskid311c6e2012-08-06 14:41:07 +020017#include <linux/list.h>
18#include <linux/compiler.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060019#include <linux/printk.h>
Lukasz Majewskid311c6e2012-08-06 14:41:07 +020020
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;
Rasmus Villemoesa51a2f02024-09-11 15:39:00 +020030bool dfu_alt_info_changed = false;
31
32static int on_dfu_alt_info(const char *name, const char *value, enum env_op op,
33 int flags)
34{
35 switch (op) {
36 case env_op_create:
37 case env_op_overwrite:
38 case env_op_delete:
39 dfu_alt_info_changed = true;
40 break;
41 }
42 return 0;
43}
44U_BOOT_ENV_CALLBACK(dfu_alt_info, on_dfu_alt_info);
Marek Szyprowskib1fa1ad2020-12-22 11:32:22 +010045
Lukasz Majewskid9eae702014-08-25 11:07:28 +020046/*
Patrick Delaunay773771f2019-10-14 09:28:07 +020047 * The purpose of the dfu_flush_callback() function is to
48 * provide callback for dfu user
49 */
50__weak void dfu_flush_callback(struct dfu_entity *dfu)
51{
52}
53
54/*
55 * The purpose of the dfu_initiated_callback() function is to
56 * provide callback for dfu user
57 */
58__weak void dfu_initiated_callback(struct dfu_entity *dfu)
59{
60}
61
62/*
Patrick Delaunayf1357352021-05-18 15:12:12 +020063 * The purpose of the dfu_error_callback() function is to
64 * provide callback for dfu user
65 */
66__weak void dfu_error_callback(struct dfu_entity *dfu, const char *msg)
67{
68}
69
70/*
Lukasz Majewskid9eae702014-08-25 11:07:28 +020071 * The purpose of the dfu_usb_get_reset() function is to
72 * provide information if after USB_DETACH request
73 * being sent the dfu-util performed reset of USB
74 * bus.
75 *
76 * Described behaviour is the only way to distinct if
77 * user has typed -e (detach) or -R (reset) when invoking
78 * dfu-util command.
79 *
80 */
81__weak bool dfu_usb_get_reset(void)
Lukasz Majewski2968aa22013-07-18 13:19:14 +020082{
B, Ravi4cca5cf2017-05-04 15:45:29 +053083#ifdef CONFIG_SPL_DFU_NO_RESET
84 return false;
85#else
Lukasz Majewskid9eae702014-08-25 11:07:28 +020086 return true;
B, Ravi4cca5cf2017-05-04 15:45:29 +053087#endif
Lukasz Majewski2968aa22013-07-18 13:19:14 +020088}
89
Andy Shevchenko1e414012019-11-27 18:12:15 +020090#ifdef CONFIG_DFU_TIMEOUT
91void dfu_set_timeout(unsigned long timeout)
92{
93 dfu_timeout = timeout;
94}
95
96unsigned long dfu_get_timeout(void)
97{
98 return dfu_timeout;
99}
100#endif
101
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200102static int dfu_find_alt_num(const char *s)
103{
104 int i = 0;
105
106 for (; *s; s++)
107 if (*s == ';')
108 i++;
109
110 return ++i;
111}
112
Patrick Delaunay06164032019-10-14 09:28:02 +0200113/*
114 * treat dfu_alt_info with several interface information
115 * to allow DFU on several device with one command,
116 * the string format is
117 * interface devstring'='alternate list (';' separated)
118 * and each interface separated by '&'
119 */
120int dfu_config_interfaces(char *env)
121{
122 struct dfu_entity *dfu;
123 char *s, *i, *d, *a, *part;
124 int ret = -EINVAL;
125 int n = 1;
126
127 s = env;
128 for (; *s; s++) {
129 if (*s == ';')
130 n++;
131 if (*s == '&')
132 n++;
133 }
134 ret = dfu_alt_init(n, &dfu);
135 if (ret)
136 return ret;
137
138 s = env;
139 while (s) {
140 ret = -EINVAL;
Masami Hiramatsuf815c332022-01-31 11:52:29 +0900141 i = strsep(&s, " \t");
Patrick Delaunay06164032019-10-14 09:28:02 +0200142 if (!i)
143 break;
Masami Hiramatsuf815c332022-01-31 11:52:29 +0900144 s = skip_spaces(s);
Patrick Delaunay06164032019-10-14 09:28:02 +0200145 d = strsep(&s, "=");
146 if (!d)
147 break;
148 a = strsep(&s, "&");
149 if (!a)
150 a = s;
151 do {
152 part = strsep(&a, ";");
Ravi Gunasekaran9ed024d2023-05-18 12:06:23 +0530153 part = skip_spaces(part);
Patrick Delaunay06164032019-10-14 09:28:02 +0200154 ret = dfu_alt_add(dfu, i, d, part);
155 if (ret)
156 return ret;
157 } while (a);
158 }
159
160 return ret;
161}
162
Stephen Warren4afe50f2014-06-11 16:03:33 -0600163int dfu_init_env_entities(char *interface, char *devstr)
Lukasz Majewski35e591e2013-09-11 14:53:35 +0200164{
165 const char *str_env;
166 char *env_bkp;
Sam Protsenko6500cde2018-07-13 16:35:46 +0300167 int ret = 0;
Lukasz Majewski35e591e2013-09-11 14:53:35 +0200168
Marek Szyprowskib1fa1ad2020-12-22 11:32:22 +0100169 dfu_reinit_needed = false;
Rasmus Villemoesa51a2f02024-09-11 15:39:00 +0200170 dfu_alt_info_changed = false;
Marek Szyprowskib1fa1ad2020-12-22 11:32:22 +0100171
Przemyslaw Marczakd0dd6092015-02-17 12:24:11 +0100172#ifdef CONFIG_SET_DFU_ALT_INFO
173 set_dfu_alt_info(interface, devstr);
174#endif
Simon Glass64b723f2017-08-03 12:22:12 -0600175 str_env = env_get("dfu_alt_info");
Lukasz Majewski35e591e2013-09-11 14:53:35 +0200176 if (!str_env) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900177 pr_err("\"dfu_alt_info\" env variable not defined!\n");
Lukasz Majewski35e591e2013-09-11 14:53:35 +0200178 return -EINVAL;
179 }
180
181 env_bkp = strdup(str_env);
Patrick Delaunay06164032019-10-14 09:28:02 +0200182 if (!interface && !devstr)
183 ret = dfu_config_interfaces(env_bkp);
184 else
185 ret = dfu_config_entities(env_bkp, interface, devstr);
186
Lukasz Majewski35e591e2013-09-11 14:53:35 +0200187 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900188 pr_err("DFU entities configuration failed!\n");
Sam Protsenkoab79fb02018-07-13 16:35:47 +0300189 pr_err("(partition table does not match dfu_alt_info?)\n");
Sam Protsenko6500cde2018-07-13 16:35:46 +0300190 goto done;
Lukasz Majewski35e591e2013-09-11 14:53:35 +0200191 }
192
Sam Protsenko6500cde2018-07-13 16:35:46 +0300193done:
Lukasz Majewski35e591e2013-09-11 14:53:35 +0200194 free(env_bkp);
Sam Protsenko6500cde2018-07-13 16:35:46 +0300195 return ret;
Lukasz Majewski35e591e2013-09-11 14:53:35 +0200196}
197
Heiko Schochera2f831e2013-06-12 06:05:51 +0200198static unsigned char *dfu_buf;
Lukasz Majewski0af4df82015-07-08 23:43:18 +0200199static unsigned long dfu_buf_size;
Patrick Delaunay06164032019-10-14 09:28:02 +0200200static enum dfu_device_type dfu_buf_device_type;
Heiko Schochera2f831e2013-06-12 06:05:51 +0200201
Lukasz Majewski34264232013-10-08 14:30:39 +0200202unsigned char *dfu_free_buf(void)
Heiko Schochera2f831e2013-06-12 06:05:51 +0200203{
204 free(dfu_buf);
205 dfu_buf = NULL;
206 return dfu_buf;
207}
208
Lukasz Majewski031d4132013-12-09 16:20:13 +0100209unsigned long dfu_get_buf_size(void)
210{
211 return dfu_buf_size;
212}
213
Stephen Warrenf9500982014-06-11 16:03:34 -0600214unsigned char *dfu_get_buf(struct dfu_entity *dfu)
Heiko Schochera2f831e2013-06-12 06:05:51 +0200215{
216 char *s;
217
Patrick Delaunay06164032019-10-14 09:28:02 +0200218 /* manage several entity with several contraint */
219 if (dfu_buf && dfu->dev_type != dfu_buf_device_type)
220 dfu_free_buf();
221
Heiko Schochera2f831e2013-06-12 06:05:51 +0200222 if (dfu_buf != NULL)
223 return dfu_buf;
224
Simon Glass64b723f2017-08-03 12:22:12 -0600225 s = env_get("dfu_bufsiz");
Przemyslaw Marczak24883502014-12-15 10:34:11 +0100226 if (s)
227 dfu_buf_size = (unsigned long)simple_strtol(s, NULL, 0);
228
229 if (!s || !dfu_buf_size)
230 dfu_buf_size = CONFIG_SYS_DFU_DATA_BUF_SIZE;
231
Stephen Warrenf9500982014-06-11 16:03:34 -0600232 if (dfu->max_buf_size && dfu_buf_size > dfu->max_buf_size)
233 dfu_buf_size = dfu->max_buf_size;
Heiko Schochera2f831e2013-06-12 06:05:51 +0200234
235 dfu_buf = memalign(CONFIG_SYS_CACHELINE_SIZE, dfu_buf_size);
236 if (dfu_buf == NULL)
237 printf("%s: Could not memalign 0x%lx bytes\n",
238 __func__, dfu_buf_size);
239
Patrick Delaunay06164032019-10-14 09:28:02 +0200240 dfu_buf_device_type = dfu->dev_type;
Heiko Schochera2f831e2013-06-12 06:05:51 +0200241 return dfu_buf;
242}
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200243
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200244static char *dfu_get_hash_algo(void)
245{
246 char *s;
247
Simon Glass64b723f2017-08-03 12:22:12 -0600248 s = env_get("dfu_hash_algo");
Lukasz Majewski3e0d0282014-06-10 12:25:59 +0200249 if (!s)
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200250 return NULL;
251
Lukasz Majewski3e0d0282014-06-10 12:25:59 +0200252 if (!strcmp(s, "crc32")) {
253 debug("%s: DFU hash method: %s\n", __func__, s);
254 return s;
255 }
256
Masahiro Yamada81e10422017-09-16 14:10:41 +0900257 pr_err("DFU hash method: %s not supported!\n", s);
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200258 return NULL;
259}
260
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000261static int dfu_write_buffer_drain(struct dfu_entity *dfu)
262{
263 long w_size;
264 int ret;
265
266 /* flush size? */
267 w_size = dfu->i_buf - dfu->i_buf_start;
268 if (w_size == 0)
269 return 0;
270
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200271 if (dfu_hash_algo)
272 dfu_hash_algo->hash_update(dfu_hash_algo, &dfu->crc,
273 dfu->i_buf_start, w_size, 0);
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000274
275 ret = dfu->write_medium(dfu, dfu->offset, dfu->i_buf_start, &w_size);
276 if (ret)
277 debug("%s: Write error!\n", __func__);
278
279 /* point back */
280 dfu->i_buf = dfu->i_buf_start;
281
282 /* update offset */
283 dfu->offset += w_size;
284
285 puts("#");
286
287 return ret;
288}
289
Patrick Delaunay35be83e2017-07-19 16:39:24 +0200290void dfu_transaction_cleanup(struct dfu_entity *dfu)
Stephen Warren7f450232014-06-11 12:48:08 -0600291{
292 /* clear everything */
Stephen Warren7f450232014-06-11 12:48:08 -0600293 dfu->crc = 0;
294 dfu->offset = 0;
295 dfu->i_blk_seq_num = 0;
Patrick Delaunaydec4ecb2017-07-19 16:39:25 +0200296 dfu->i_buf_start = dfu_get_buf(dfu);
297 dfu->i_buf_end = dfu->i_buf_start;
Stephen Warren7f450232014-06-11 12:48:08 -0600298 dfu->i_buf = dfu->i_buf_start;
Patrick Delaunay35be83e2017-07-19 16:39:24 +0200299 dfu->r_left = 0;
300 dfu->b_left = 0;
301 dfu->bad_skip = 0;
302
Stephen Warren7f450232014-06-11 12:48:08 -0600303 dfu->inited = 0;
304}
305
Patrick Delaunaydec4ecb2017-07-19 16:39:25 +0200306int dfu_transaction_initiate(struct dfu_entity *dfu, bool read)
307{
308 int ret = 0;
309
310 if (dfu->inited)
311 return 0;
312
313 dfu_transaction_cleanup(dfu);
314
315 if (dfu->i_buf_start == NULL)
316 return -ENOMEM;
317
318 dfu->i_buf_end = dfu->i_buf_start + dfu_get_buf_size();
319
320 if (read) {
321 ret = dfu->get_medium_size(dfu, &dfu->r_left);
322 if (ret < 0)
323 return ret;
324 debug("%s: %s %lld [B]\n", __func__, dfu->name, dfu->r_left);
325 }
326
327 dfu->inited = 1;
Patrick Delaunay773771f2019-10-14 09:28:07 +0200328 dfu_initiated_callback(dfu);
Patrick Delaunaydec4ecb2017-07-19 16:39:25 +0200329
330 return 0;
331}
332
Heiko Schocher941b3842014-03-18 08:09:55 +0100333int dfu_flush(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
334{
335 int ret = 0;
336
Przemyslaw Marczaka0f6a382014-05-12 12:05:33 +0200337 ret = dfu_write_buffer_drain(dfu);
338 if (ret)
339 return ret;
340
Heiko Schocher941b3842014-03-18 08:09:55 +0100341 if (dfu->flush_medium)
342 ret = dfu->flush_medium(dfu);
343
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200344 if (dfu_hash_algo)
345 printf("\nDFU complete %s: 0x%08x\n", dfu_hash_algo->name,
346 dfu->crc);
Heiko Schocher941b3842014-03-18 08:09:55 +0100347
Patrick Delaunay773771f2019-10-14 09:28:07 +0200348 dfu_flush_callback(dfu);
349
Patrick Delaunay35be83e2017-07-19 16:39:24 +0200350 dfu_transaction_cleanup(dfu);
Heiko Schocher941b3842014-03-18 08:09:55 +0100351
352 return ret;
353}
354
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200355int dfu_write(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
356{
Stephen Warren7f450232014-06-11 12:48:08 -0600357 int ret;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200358
Stephen Warrenc00315b2015-07-22 14:54:04 -0600359 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 +0000360 __func__, dfu->name, buf, size, blk_seq_num, dfu->offset,
Stephen Warrenc00315b2015-07-22 14:54:04 -0600361 (unsigned long)(dfu->i_buf - dfu->i_buf_start));
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200362
Patrick Delaunaydec4ecb2017-07-19 16:39:25 +0200363 ret = dfu_transaction_initiate(dfu, false);
364 if (ret < 0)
365 return ret;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200366
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000367 if (dfu->i_blk_seq_num != blk_seq_num) {
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200368 printf("%s: Wrong sequence number! [%d] [%d]\n",
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000369 __func__, dfu->i_blk_seq_num, blk_seq_num);
Patrick Delaunay35be83e2017-07-19 16:39:24 +0200370 dfu_transaction_cleanup(dfu);
Patrick Delaunayf1357352021-05-18 15:12:12 +0200371 dfu_error_callback(dfu, "Wrong sequence number");
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000372 return -1;
373 }
374
375 /* DFU 1.1 standard says:
376 * The wBlockNum field is a block sequence number. It increments each
377 * time a block is transferred, wrapping to zero from 65,535. It is used
378 * to provide useful context to the DFU loader in the device."
379 *
380 * This means that it's a 16 bit counter that roll-overs at
381 * 0xffff -> 0x0000. By having a typical 4K transfer block
382 * we roll-over at exactly 256MB. Not very fun to debug.
383 *
384 * Handling rollover, and having an inited variable,
385 * makes things work.
386 */
387
388 /* handle rollover */
389 dfu->i_blk_seq_num = (dfu->i_blk_seq_num + 1) & 0xffff;
390
391 /* flush buffer if overflow */
392 if ((dfu->i_buf + size) > dfu->i_buf_end) {
Stephen Warren7f450232014-06-11 12:48:08 -0600393 ret = dfu_write_buffer_drain(dfu);
394 if (ret) {
Patrick Delaunay35be83e2017-07-19 16:39:24 +0200395 dfu_transaction_cleanup(dfu);
Patrick Delaunayf1357352021-05-18 15:12:12 +0200396 dfu_error_callback(dfu, "DFU write error");
Stephen Warren7f450232014-06-11 12:48:08 -0600397 return ret;
398 }
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000399 }
400
401 /* we should be in buffer now (if not then size too large) */
402 if ((dfu->i_buf + size) > dfu->i_buf_end) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900403 pr_err("Buffer overflow! (0x%p + 0x%x > 0x%p)\n", dfu->i_buf,
Lukasz Majewski385c3212013-09-10 15:29:22 +0200404 size, dfu->i_buf_end);
Patrick Delaunay35be83e2017-07-19 16:39:24 +0200405 dfu_transaction_cleanup(dfu);
Patrick Delaunayf1357352021-05-18 15:12:12 +0200406 dfu_error_callback(dfu, "Buffer overflow");
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200407 return -1;
408 }
409
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000410 memcpy(dfu->i_buf, buf, size);
411 dfu->i_buf += size;
412
413 /* if end or if buffer full flush */
414 if (size == 0 || (dfu->i_buf + size) > dfu->i_buf_end) {
Stephen Warren7f450232014-06-11 12:48:08 -0600415 ret = dfu_write_buffer_drain(dfu);
416 if (ret) {
Patrick Delaunay35be83e2017-07-19 16:39:24 +0200417 dfu_transaction_cleanup(dfu);
Patrick Delaunayf1357352021-05-18 15:12:12 +0200418 dfu_error_callback(dfu, "DFU write error");
Stephen Warren7f450232014-06-11 12:48:08 -0600419 return ret;
420 }
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000421 }
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200422
Stephen Warren7f450232014-06-11 12:48:08 -0600423 return 0;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200424}
425
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000426static int dfu_read_buffer_fill(struct dfu_entity *dfu, void *buf, int size)
427{
428 long chunk;
429 int ret, readn;
430
431 readn = 0;
432 while (size > 0) {
433 /* get chunk that can be read */
Masahiro Yamadadb204642014-11-07 03:03:31 +0900434 chunk = min((long)size, dfu->b_left);
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000435 /* consume */
436 if (chunk > 0) {
437 memcpy(buf, dfu->i_buf, chunk);
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200438 if (dfu_hash_algo)
439 dfu_hash_algo->hash_update(dfu_hash_algo,
440 &dfu->crc, buf,
441 chunk, 0);
442
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000443 dfu->i_buf += chunk;
444 dfu->b_left -= chunk;
445 size -= chunk;
446 buf += chunk;
447 readn += chunk;
448 }
449
450 /* all done */
451 if (size > 0) {
452 /* no more to read */
453 if (dfu->r_left == 0)
454 break;
455
456 dfu->i_buf = dfu->i_buf_start;
457 dfu->b_left = dfu->i_buf_end - dfu->i_buf_start;
458
459 /* got to read, but buffer is empty */
460 if (dfu->b_left > dfu->r_left)
461 dfu->b_left = dfu->r_left;
462 ret = dfu->read_medium(dfu, dfu->offset, dfu->i_buf,
463 &dfu->b_left);
464 if (ret != 0) {
465 debug("%s: Read error!\n", __func__);
466 return ret;
467 }
Patrick Delaunay7ef84362019-10-14 09:28:03 +0200468 if (dfu->b_left == 0)
469 break;
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000470 dfu->offset += dfu->b_left;
471 dfu->r_left -= dfu->b_left;
472
473 puts("#");
474 }
475 }
476
477 return readn;
478}
479
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200480int dfu_read(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
481{
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200482 int ret = 0;
483
484 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 +0000485 __func__, dfu->name, buf, size, blk_seq_num, dfu->i_buf);
486
Patrick Delaunaydec4ecb2017-07-19 16:39:25 +0200487 ret = dfu_transaction_initiate(dfu, true);
488 if (ret < 0)
489 return ret;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200490
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000491 if (dfu->i_blk_seq_num != blk_seq_num) {
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200492 printf("%s: Wrong sequence number! [%d] [%d]\n",
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000493 __func__, dfu->i_blk_seq_num, blk_seq_num);
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200494 return -1;
495 }
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000496 /* handle rollover */
497 dfu->i_blk_seq_num = (dfu->i_blk_seq_num + 1) & 0xffff;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200498
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000499 ret = dfu_read_buffer_fill(dfu, buf, size);
500 if (ret < 0) {
501 printf("%s: Failed to fill buffer\n", __func__);
502 return -1;
503 }
504
505 if (ret < size) {
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200506 if (dfu_hash_algo)
507 debug("%s: %s %s: 0x%x\n", __func__, dfu->name,
508 dfu_hash_algo->name, dfu->crc);
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000509 puts("\nUPLOAD ... done\nCtrl+C to exit ...\n");
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200510
Patrick Delaunay35be83e2017-07-19 16:39:24 +0200511 dfu_transaction_cleanup(dfu);
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200512 }
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000513
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200514 return ret;
515}
516
517static int dfu_fill_entity(struct dfu_entity *dfu, char *s, int alt,
Stephen Warren4afe50f2014-06-11 16:03:33 -0600518 char *interface, char *devstr)
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200519{
Masami Hiramatsufa4282a2022-01-31 11:52:37 +0900520 char *argv[DFU_MAX_ENTITY_ARGS];
521 int argc;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200522 char *st;
523
Stephen Warren4afe50f2014-06-11 16:03:33 -0600524 debug("%s: %s interface: %s dev: %s\n", __func__, s, interface, devstr);
Masami Hiramatsuf815c332022-01-31 11:52:29 +0900525 st = strsep(&s, " \t");
Masami Hiramatsu020bb2c2022-01-31 11:52:20 +0900526 strlcpy(dfu->name, st, DFU_NAME_SIZE);
Masami Hiramatsufa4282a2022-01-31 11:52:37 +0900527
528 /* Parse arguments */
529 for (argc = 0; s && argc < DFU_MAX_ENTITY_ARGS; argc++) {
530 s = skip_spaces(s);
531 if (!*s)
532 break;
533 argv[argc] = strsep(&s, " \t");
534 }
535
536 if (argc == DFU_MAX_ENTITY_ARGS && s) {
537 s = skip_spaces(s);
538 if (*s) {
539 log_err("Too many arguments for %s\n", dfu->name);
540 return -EINVAL;
541 }
542 }
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200543
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200544 dfu->alt = alt;
Stephen Warrenf9500982014-06-11 16:03:34 -0600545 dfu->max_buf_size = 0;
Stephen Warrena5f77f62014-06-11 16:03:35 -0600546 dfu->free_entity = NULL;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200547
548 /* Specific for mmc device */
549 if (strcmp(interface, "mmc") == 0) {
Masami Hiramatsufa4282a2022-01-31 11:52:37 +0900550 if (dfu_fill_entity_mmc(dfu, devstr, argv, argc))
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200551 return -1;
Patrick Delaunayf7aee232019-10-14 09:28:04 +0200552 } else if (strcmp(interface, "mtd") == 0) {
Masami Hiramatsufa4282a2022-01-31 11:52:37 +0900553 if (dfu_fill_entity_mtd(dfu, devstr, argv, argc))
Patrick Delaunayf7aee232019-10-14 09:28:04 +0200554 return -1;
Pantelis Antonioucf14d0d2013-03-14 05:32:52 +0000555 } else if (strcmp(interface, "nand") == 0) {
Masami Hiramatsufa4282a2022-01-31 11:52:37 +0900556 if (dfu_fill_entity_nand(dfu, devstr, argv, argc))
Pantelis Antonioucf14d0d2013-03-14 05:32:52 +0000557 return -1;
Afzal Mohammede3c687a2013-09-18 01:15:24 +0530558 } else if (strcmp(interface, "ram") == 0) {
Masami Hiramatsufa4282a2022-01-31 11:52:37 +0900559 if (dfu_fill_entity_ram(dfu, devstr, argv, argc))
Afzal Mohammede3c687a2013-09-18 01:15:24 +0530560 return -1;
Stephen Warren0280af72014-06-11 16:03:36 -0600561 } else if (strcmp(interface, "sf") == 0) {
Masami Hiramatsufa4282a2022-01-31 11:52:37 +0900562 if (dfu_fill_entity_sf(dfu, devstr, argv, argc))
Stephen Warren0280af72014-06-11 16:03:36 -0600563 return -1;
Patrick Delaunay2111e3c2019-10-14 09:28:06 +0200564 } else if (strcmp(interface, "virt") == 0) {
Masami Hiramatsufa4282a2022-01-31 11:52:37 +0900565 if (dfu_fill_entity_virt(dfu, devstr, argv, argc))
Patrick Delaunay2111e3c2019-10-14 09:28:06 +0200566 return -1;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200567 } else {
568 printf("%s: Device %s not (yet) supported!\n",
569 __func__, interface);
570 return -1;
571 }
Stephen Warren0898da52015-09-04 22:03:46 -0600572 dfu_get_buf(dfu);
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200573
574 return 0;
575}
576
577void dfu_free_entities(void)
578{
579 struct dfu_entity *dfu, *p, *t = NULL;
580
Stephen Warren0898da52015-09-04 22:03:46 -0600581 dfu_free_buf();
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200582 list_for_each_entry_safe_reverse(dfu, p, &dfu_list, list) {
583 list_del(&dfu->list);
Stephen Warrena5f77f62014-06-11 16:03:35 -0600584 if (dfu->free_entity)
585 dfu->free_entity(dfu);
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200586 t = dfu;
587 }
588 if (t)
589 free(t);
590 INIT_LIST_HEAD(&dfu_list);
Przemyslaw Marczak4af3e082014-02-28 18:53:37 +0100591
592 alt_num_cnt = 0;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200593}
594
Patrick Delaunay0f8fad82019-10-14 09:28:01 +0200595int dfu_alt_init(int num, struct dfu_entity **dfu)
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200596{
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200597 char *s;
Patrick Delaunay0f8fad82019-10-14 09:28:01 +0200598 int ret;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200599
Patrick Delaunay0f8fad82019-10-14 09:28:01 +0200600 dfu_alt_num = num;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200601 debug("%s: dfu_alt_num=%d\n", __func__, dfu_alt_num);
602
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200603 dfu_hash_algo = NULL;
604 s = dfu_get_hash_algo();
605 if (s) {
606 ret = hash_lookup_algo(s, &dfu_hash_algo);
607 if (ret)
Masahiro Yamada81e10422017-09-16 14:10:41 +0900608 pr_err("Hash algorithm %s not supported\n", s);
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200609 }
610
Patrick Delaunay0f8fad82019-10-14 09:28:01 +0200611 *dfu = calloc(sizeof(struct dfu_entity), dfu_alt_num);
612 if (!*dfu)
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200613 return -1;
Patrick Delaunay0f8fad82019-10-14 09:28:01 +0200614
615 return 0;
616}
617
618int dfu_alt_add(struct dfu_entity *dfu, char *interface, char *devstr, char *s)
619{
620 struct dfu_entity *p_dfu;
621 int ret;
622
623 if (alt_num_cnt >= dfu_alt_num)
624 return -1;
625
626 p_dfu = &dfu[alt_num_cnt];
627 ret = dfu_fill_entity(p_dfu, s, alt_num_cnt, interface, devstr);
628 if (ret)
629 return -1;
630
631 list_add_tail(&p_dfu->list, &dfu_list);
632 alt_num_cnt++;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200633
Patrick Delaunay0f8fad82019-10-14 09:28:01 +0200634 return 0;
635}
636
637int dfu_config_entities(char *env, char *interface, char *devstr)
638{
639 struct dfu_entity *dfu;
640 int i, ret;
641 char *s;
642
643 ret = dfu_alt_init(dfu_find_alt_num(env), &dfu);
644 if (ret)
645 return -1;
646
647 for (i = 0; i < dfu_alt_num; i++) {
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200648 s = strsep(&env, ";");
Ravi Gunasekaran9ed024d2023-05-18 12:06:23 +0530649 s = skip_spaces(s);
Patrick Delaunay0f8fad82019-10-14 09:28:01 +0200650 ret = dfu_alt_add(dfu, interface, devstr, s);
Peng Fan529d4fd2016-05-03 10:24:52 +0800651 if (ret) {
Sam Protsenko6d29e5c2018-07-13 16:35:45 +0300652 /* We will free "dfu" in dfu_free_entities() */
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200653 return -1;
Peng Fan529d4fd2016-05-03 10:24:52 +0800654 }
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200655 }
656
657 return 0;
658}
659
660const char *dfu_get_dev_type(enum dfu_device_type t)
661{
Patrick Delaunayae224c32019-10-14 09:27:58 +0200662 const char *const dev_t[] = {NULL, "eMMC", "OneNAND", "NAND", "RAM",
Patrick Delaunay2111e3c2019-10-14 09:28:06 +0200663 "SF", "MTD", "VIRT"};
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200664 return dev_t[t];
665}
666
667const char *dfu_get_layout(enum dfu_layout l)
668{
Patrick Delaunayae224c32019-10-14 09:27:58 +0200669 const char *const dfu_layout[] = {NULL, "RAW_ADDR", "FAT", "EXT2",
Marek Szyprowskib1fa1ad2020-12-22 11:32:22 +0100670 "EXT3", "EXT4", "RAM_ADDR", "SKIP",
671 "SCRIPT" };
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200672 return dfu_layout[l];
673}
674
675void dfu_show_entities(void)
676{
677 struct dfu_entity *dfu;
678
679 puts("DFU alt settings list:\n");
680
681 list_for_each_entry(dfu, &dfu_list, list) {
682 printf("dev: %s alt: %d name: %s layout: %s\n",
683 dfu_get_dev_type(dfu->dev_type), dfu->alt,
684 dfu->name, dfu_get_layout(dfu->layout));
685 }
686}
687
688int dfu_get_alt_number(void)
689{
690 return dfu_alt_num;
691}
692
693struct dfu_entity *dfu_get_entity(int alt)
694{
695 struct dfu_entity *dfu;
696
697 list_for_each_entry(dfu, &dfu_list, list) {
698 if (dfu->alt == alt)
699 return dfu;
700 }
701
702 return NULL;
703}
Lukasz Majewski1737f0f2013-10-08 14:30:38 +0200704
705int dfu_get_alt(char *name)
706{
707 struct dfu_entity *dfu;
Lukasz Majewski3a39d012014-11-05 10:54:16 +0100708 char *str;
Lukasz Majewski1737f0f2013-10-08 14:30:38 +0200709
710 list_for_each_entry(dfu, &dfu_list, list) {
Lukasz Majewski3a39d012014-11-05 10:54:16 +0100711 if (dfu->name[0] != '/') {
712 if (!strncmp(dfu->name, name, strlen(dfu->name)))
713 return dfu->alt;
714 } else {
715 /*
716 * One must also consider absolute path
717 * (/boot/bin/uImage) available at dfu->name when
718 * compared "plain" file name (uImage)
719 *
720 * It is the case for e.g. thor gadget where lthor SW
721 * sends only the file name, so only the very last part
722 * of path must be checked for equality
723 */
724
725 str = strstr(dfu->name, name);
726 if (!str)
727 continue;
728
729 /*
730 * Check if matching substring is the last element of
731 * dfu->name (uImage)
732 */
733 if (strlen(dfu->name) ==
734 ((str - dfu->name) + strlen(name)))
735 return dfu->alt;
736 }
Lukasz Majewski1737f0f2013-10-08 14:30:38 +0200737 }
738
739 return -ENODEV;
740}
Lukasz Majewskicc2d0262015-08-24 00:21:46 +0200741
742int dfu_write_from_mem_addr(struct dfu_entity *dfu, void *buf, int size)
743{
744 unsigned long dfu_buf_size, write, left = size;
745 int i, ret = 0;
746 void *dp = buf;
747
748 /*
749 * Here we must call dfu_get_buf(dfu) first to be sure that dfu_buf_size
750 * has been properly initialized - e.g. if "dfu_bufsiz" has been taken
751 * into account.
752 */
753 dfu_get_buf(dfu);
754 dfu_buf_size = dfu_get_buf_size();
755 debug("%s: dfu buf size: %lu\n", __func__, dfu_buf_size);
756
757 for (i = 0; left > 0; i++) {
758 write = min(dfu_buf_size, left);
759
760 debug("%s: dp: 0x%p left: %lu write: %lu\n", __func__,
761 dp, left, write);
762 ret = dfu_write(dfu, dp, write, i);
763 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900764 pr_err("DFU write failed\n");
Lukasz Majewskicc2d0262015-08-24 00:21:46 +0200765 return ret;
766 }
767
768 dp += write;
769 left -= write;
770 }
771
772 ret = dfu_flush(dfu, NULL, 0, i);
773 if (ret)
Masahiro Yamada81e10422017-09-16 14:10:41 +0900774 pr_err("DFU flush failed!");
Heinrich Schuchardt54fb1fa2021-03-05 18:36:37 +0100775 puts("\n");
Lukasz Majewskicc2d0262015-08-24 00:21:46 +0200776
777 return ret;
778}