blob: 8024ad98a7dc3a57fa44da2e54e3ad225f2322d7 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Lukasz Majewskif22b11c2012-08-06 14:41:07 +02002/*
3 * dfu.c -- DFU back-end routines
4 *
5 * Copyright (C) 2012 Samsung Electronics
6 * author: Lukasz Majewski <l.majewski@samsung.com>
Lukasz Majewskif22b11c2012-08-06 14:41:07 +02007 */
8
9#include <common.h>
Simon Glass7b51b572019-08-01 09:46:52 -060010#include <env.h>
Heiko Schochere7e75c72013-06-12 06:05:51 +020011#include <errno.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060012#include <log.h>
Lukasz Majewskif22b11c2012-08-06 14:41:07 +020013#include <malloc.h>
14#include <mmc.h>
15#include <fat.h>
16#include <dfu.h>
Lukasz Majewskibd694242014-05-12 10:43:36 +020017#include <hash.h>
Lukasz Majewskif22b11c2012-08-06 14:41:07 +020018#include <linux/list.h>
19#include <linux/compiler.h>
Simon Glass1e94b462023-09-14 18:21:46 -060020#include <linux/printk.h>
Lukasz Majewskif22b11c2012-08-06 14:41:07 +020021
AKASHI Takahiro6beaa472020-10-29 13:47:44 +090022LIST_HEAD(dfu_list);
Lukasz Majewskif22b11c2012-08-06 14:41:07 +020023static int dfu_alt_num;
Przemyslaw Marczakb627eb42014-02-28 18:53:37 +010024static int alt_num_cnt;
Lukasz Majewskibd694242014-05-12 10:43:36 +020025static struct hash_algo *dfu_hash_algo;
Andy Shevchenko98a8f442019-11-27 18:12:15 +020026#ifdef CONFIG_DFU_TIMEOUT
27static unsigned long dfu_timeout = 0;
28#endif
Lukasz Majewskif22b11c2012-08-06 14:41:07 +020029
Marek Szyprowskic533f942020-12-22 11:32:22 +010030bool dfu_reinit_needed = false;
31
Lukasz Majewski1cc03c52014-08-25 11:07:28 +020032/*
Patrick Delaunay067c13c2019-10-14 09:28:07 +020033 * The purpose of the dfu_flush_callback() function is to
34 * provide callback for dfu user
35 */
36__weak void dfu_flush_callback(struct dfu_entity *dfu)
37{
38}
39
40/*
41 * The purpose of the dfu_initiated_callback() function is to
42 * provide callback for dfu user
43 */
44__weak void dfu_initiated_callback(struct dfu_entity *dfu)
45{
46}
47
48/*
Patrick Delaunayd4710322021-05-18 15:12:12 +020049 * The purpose of the dfu_error_callback() function is to
50 * provide callback for dfu user
51 */
52__weak void dfu_error_callback(struct dfu_entity *dfu, const char *msg)
53{
54}
55
56/*
Lukasz Majewski1cc03c52014-08-25 11:07:28 +020057 * The purpose of the dfu_usb_get_reset() function is to
58 * provide information if after USB_DETACH request
59 * being sent the dfu-util performed reset of USB
60 * bus.
61 *
62 * Described behaviour is the only way to distinct if
63 * user has typed -e (detach) or -R (reset) when invoking
64 * dfu-util command.
65 *
66 */
67__weak bool dfu_usb_get_reset(void)
Lukasz Majewski6bed7ce2013-07-18 13:19:14 +020068{
B, Ravi66928af2017-05-04 15:45:29 +053069#ifdef CONFIG_SPL_DFU_NO_RESET
70 return false;
71#else
Lukasz Majewski1cc03c52014-08-25 11:07:28 +020072 return true;
B, Ravi66928af2017-05-04 15:45:29 +053073#endif
Lukasz Majewski6bed7ce2013-07-18 13:19:14 +020074}
75
Andy Shevchenko98a8f442019-11-27 18:12:15 +020076#ifdef CONFIG_DFU_TIMEOUT
77void dfu_set_timeout(unsigned long timeout)
78{
79 dfu_timeout = timeout;
80}
81
82unsigned long dfu_get_timeout(void)
83{
84 return dfu_timeout;
85}
86#endif
87
Lukasz Majewskif22b11c2012-08-06 14:41:07 +020088static int dfu_find_alt_num(const char *s)
89{
90 int i = 0;
91
92 for (; *s; s++)
93 if (*s == ';')
94 i++;
95
96 return ++i;
97}
98
Patrick Delaunayfebabe32019-10-14 09:28:02 +020099/*
100 * treat dfu_alt_info with several interface information
101 * to allow DFU on several device with one command,
102 * the string format is
103 * interface devstring'='alternate list (';' separated)
104 * and each interface separated by '&'
105 */
106int dfu_config_interfaces(char *env)
107{
108 struct dfu_entity *dfu;
109 char *s, *i, *d, *a, *part;
110 int ret = -EINVAL;
111 int n = 1;
112
113 s = env;
114 for (; *s; s++) {
115 if (*s == ';')
116 n++;
117 if (*s == '&')
118 n++;
119 }
120 ret = dfu_alt_init(n, &dfu);
121 if (ret)
122 return ret;
123
124 s = env;
125 while (s) {
126 ret = -EINVAL;
Masami Hiramatsu8db74c12022-01-31 11:52:29 +0900127 i = strsep(&s, " \t");
Patrick Delaunayfebabe32019-10-14 09:28:02 +0200128 if (!i)
129 break;
Masami Hiramatsu8db74c12022-01-31 11:52:29 +0900130 s = skip_spaces(s);
Patrick Delaunayfebabe32019-10-14 09:28:02 +0200131 d = strsep(&s, "=");
132 if (!d)
133 break;
134 a = strsep(&s, "&");
135 if (!a)
136 a = s;
137 do {
138 part = strsep(&a, ";");
Ravi Gunasekarande060832023-05-18 12:06:23 +0530139 part = skip_spaces(part);
Patrick Delaunayfebabe32019-10-14 09:28:02 +0200140 ret = dfu_alt_add(dfu, i, d, part);
141 if (ret)
142 return ret;
143 } while (a);
144 }
145
146 return ret;
147}
148
Stephen Warrendd648272014-06-11 16:03:33 -0600149int dfu_init_env_entities(char *interface, char *devstr)
Lukasz Majewski765c5ae2013-09-11 14:53:35 +0200150{
151 const char *str_env;
152 char *env_bkp;
Sam Protsenko87a8ca92018-07-13 16:35:46 +0300153 int ret = 0;
Lukasz Majewski765c5ae2013-09-11 14:53:35 +0200154
Marek Szyprowskic533f942020-12-22 11:32:22 +0100155 dfu_reinit_needed = false;
156
Przemyslaw Marczak899a5282015-02-17 12:24:11 +0100157#ifdef CONFIG_SET_DFU_ALT_INFO
158 set_dfu_alt_info(interface, devstr);
159#endif
Simon Glass00caae62017-08-03 12:22:12 -0600160 str_env = env_get("dfu_alt_info");
Lukasz Majewski765c5ae2013-09-11 14:53:35 +0200161 if (!str_env) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900162 pr_err("\"dfu_alt_info\" env variable not defined!\n");
Lukasz Majewski765c5ae2013-09-11 14:53:35 +0200163 return -EINVAL;
164 }
165
166 env_bkp = strdup(str_env);
Patrick Delaunayfebabe32019-10-14 09:28:02 +0200167 if (!interface && !devstr)
168 ret = dfu_config_interfaces(env_bkp);
169 else
170 ret = dfu_config_entities(env_bkp, interface, devstr);
171
Lukasz Majewski765c5ae2013-09-11 14:53:35 +0200172 if (ret) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900173 pr_err("DFU entities configuration failed!\n");
Sam Protsenko28a5c882018-07-13 16:35:47 +0300174 pr_err("(partition table does not match dfu_alt_info?)\n");
Sam Protsenko87a8ca92018-07-13 16:35:46 +0300175 goto done;
Lukasz Majewski765c5ae2013-09-11 14:53:35 +0200176 }
177
Sam Protsenko87a8ca92018-07-13 16:35:46 +0300178done:
Lukasz Majewski765c5ae2013-09-11 14:53:35 +0200179 free(env_bkp);
Sam Protsenko87a8ca92018-07-13 16:35:46 +0300180 return ret;
Lukasz Majewski765c5ae2013-09-11 14:53:35 +0200181}
182
Heiko Schochere7e75c72013-06-12 06:05:51 +0200183static unsigned char *dfu_buf;
Lukasz Majewskia7e68922015-07-08 23:43:18 +0200184static unsigned long dfu_buf_size;
Patrick Delaunayfebabe32019-10-14 09:28:02 +0200185static enum dfu_device_type dfu_buf_device_type;
Heiko Schochere7e75c72013-06-12 06:05:51 +0200186
Lukasz Majewskid4278262013-10-08 14:30:39 +0200187unsigned char *dfu_free_buf(void)
Heiko Schochere7e75c72013-06-12 06:05:51 +0200188{
189 free(dfu_buf);
190 dfu_buf = NULL;
191 return dfu_buf;
192}
193
Lukasz Majewski4fb12782013-12-09 16:20:13 +0100194unsigned long dfu_get_buf_size(void)
195{
196 return dfu_buf_size;
197}
198
Stephen Warren7ac1b412014-06-11 16:03:34 -0600199unsigned char *dfu_get_buf(struct dfu_entity *dfu)
Heiko Schochere7e75c72013-06-12 06:05:51 +0200200{
201 char *s;
202
Patrick Delaunayfebabe32019-10-14 09:28:02 +0200203 /* manage several entity with several contraint */
204 if (dfu_buf && dfu->dev_type != dfu_buf_device_type)
205 dfu_free_buf();
206
Heiko Schochere7e75c72013-06-12 06:05:51 +0200207 if (dfu_buf != NULL)
208 return dfu_buf;
209
Simon Glass00caae62017-08-03 12:22:12 -0600210 s = env_get("dfu_bufsiz");
Przemyslaw Marczakf597fc32014-12-15 10:34:11 +0100211 if (s)
212 dfu_buf_size = (unsigned long)simple_strtol(s, NULL, 0);
213
214 if (!s || !dfu_buf_size)
215 dfu_buf_size = CONFIG_SYS_DFU_DATA_BUF_SIZE;
216
Stephen Warren7ac1b412014-06-11 16:03:34 -0600217 if (dfu->max_buf_size && dfu_buf_size > dfu->max_buf_size)
218 dfu_buf_size = dfu->max_buf_size;
Heiko Schochere7e75c72013-06-12 06:05:51 +0200219
220 dfu_buf = memalign(CONFIG_SYS_CACHELINE_SIZE, dfu_buf_size);
221 if (dfu_buf == NULL)
222 printf("%s: Could not memalign 0x%lx bytes\n",
223 __func__, dfu_buf_size);
224
Patrick Delaunayfebabe32019-10-14 09:28:02 +0200225 dfu_buf_device_type = dfu->dev_type;
Heiko Schochere7e75c72013-06-12 06:05:51 +0200226 return dfu_buf;
227}
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200228
Lukasz Majewskibd694242014-05-12 10:43:36 +0200229static char *dfu_get_hash_algo(void)
230{
231 char *s;
232
Simon Glass00caae62017-08-03 12:22:12 -0600233 s = env_get("dfu_hash_algo");
Lukasz Majewski3d83e672014-06-10 12:25:59 +0200234 if (!s)
Lukasz Majewskibd694242014-05-12 10:43:36 +0200235 return NULL;
236
Lukasz Majewski3d83e672014-06-10 12:25:59 +0200237 if (!strcmp(s, "crc32")) {
238 debug("%s: DFU hash method: %s\n", __func__, s);
239 return s;
240 }
241
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900242 pr_err("DFU hash method: %s not supported!\n", s);
Lukasz Majewskibd694242014-05-12 10:43:36 +0200243 return NULL;
244}
245
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000246static int dfu_write_buffer_drain(struct dfu_entity *dfu)
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200247{
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000248 long w_size;
249 int ret;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200250
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000251 /* flush size? */
252 w_size = dfu->i_buf - dfu->i_buf_start;
253 if (w_size == 0)
254 return 0;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200255
Lukasz Majewskibd694242014-05-12 10:43:36 +0200256 if (dfu_hash_algo)
257 dfu_hash_algo->hash_update(dfu_hash_algo, &dfu->crc,
258 dfu->i_buf_start, w_size, 0);
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200259
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000260 ret = dfu->write_medium(dfu, dfu->offset, dfu->i_buf_start, &w_size);
261 if (ret)
262 debug("%s: Write error!\n", __func__);
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200263
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000264 /* point back */
265 dfu->i_buf = dfu->i_buf_start;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200266
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000267 /* update offset */
268 dfu->offset += w_size;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200269
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000270 puts("#");
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200271
272 return ret;
273}
274
Patrick Delaunay57da0602017-07-19 16:39:24 +0200275void dfu_transaction_cleanup(struct dfu_entity *dfu)
Stephen Warren3ee95932014-06-11 12:48:08 -0600276{
277 /* clear everything */
Stephen Warren3ee95932014-06-11 12:48:08 -0600278 dfu->crc = 0;
279 dfu->offset = 0;
280 dfu->i_blk_seq_num = 0;
Patrick Delaunay6fa8ddd2017-07-19 16:39:25 +0200281 dfu->i_buf_start = dfu_get_buf(dfu);
282 dfu->i_buf_end = dfu->i_buf_start;
Stephen Warren3ee95932014-06-11 12:48:08 -0600283 dfu->i_buf = dfu->i_buf_start;
Patrick Delaunay57da0602017-07-19 16:39:24 +0200284 dfu->r_left = 0;
285 dfu->b_left = 0;
286 dfu->bad_skip = 0;
287
Stephen Warren3ee95932014-06-11 12:48:08 -0600288 dfu->inited = 0;
289}
290
Patrick Delaunay6fa8ddd2017-07-19 16:39:25 +0200291int dfu_transaction_initiate(struct dfu_entity *dfu, bool read)
292{
293 int ret = 0;
294
295 if (dfu->inited)
296 return 0;
297
298 dfu_transaction_cleanup(dfu);
299
300 if (dfu->i_buf_start == NULL)
301 return -ENOMEM;
302
303 dfu->i_buf_end = dfu->i_buf_start + dfu_get_buf_size();
304
305 if (read) {
306 ret = dfu->get_medium_size(dfu, &dfu->r_left);
307 if (ret < 0)
308 return ret;
309 debug("%s: %s %lld [B]\n", __func__, dfu->name, dfu->r_left);
310 }
311
312 dfu->inited = 1;
Patrick Delaunay067c13c2019-10-14 09:28:07 +0200313 dfu_initiated_callback(dfu);
Patrick Delaunay6fa8ddd2017-07-19 16:39:25 +0200314
315 return 0;
316}
317
Heiko Schochera2199af2014-03-18 08:09:55 +0100318int dfu_flush(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
319{
320 int ret = 0;
321
Przemyslaw Marczak1aa4bdc2014-05-12 12:05:33 +0200322 ret = dfu_write_buffer_drain(dfu);
323 if (ret)
324 return ret;
325
Heiko Schochera2199af2014-03-18 08:09:55 +0100326 if (dfu->flush_medium)
327 ret = dfu->flush_medium(dfu);
328
Lukasz Majewskibd694242014-05-12 10:43:36 +0200329 if (dfu_hash_algo)
330 printf("\nDFU complete %s: 0x%08x\n", dfu_hash_algo->name,
331 dfu->crc);
Heiko Schochera2199af2014-03-18 08:09:55 +0100332
Patrick Delaunay067c13c2019-10-14 09:28:07 +0200333 dfu_flush_callback(dfu);
334
Patrick Delaunay57da0602017-07-19 16:39:24 +0200335 dfu_transaction_cleanup(dfu);
Heiko Schochera2199af2014-03-18 08:09:55 +0100336
337 return ret;
338}
339
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000340int dfu_write(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200341{
Stephen Warren3ee95932014-06-11 12:48:08 -0600342 int ret;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200343
Stephen Warrene621c7a2015-07-22 14:54:04 -0600344 debug("%s: name: %s buf: 0x%p size: 0x%x p_num: 0x%x offset: 0x%llx bufoffset: 0x%lx\n",
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000345 __func__, dfu->name, buf, size, blk_seq_num, dfu->offset,
Stephen Warrene621c7a2015-07-22 14:54:04 -0600346 (unsigned long)(dfu->i_buf - dfu->i_buf_start));
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200347
Patrick Delaunay6fa8ddd2017-07-19 16:39:25 +0200348 ret = dfu_transaction_initiate(dfu, false);
349 if (ret < 0)
350 return ret;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200351
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000352 if (dfu->i_blk_seq_num != blk_seq_num) {
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200353 printf("%s: Wrong sequence number! [%d] [%d]\n",
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000354 __func__, dfu->i_blk_seq_num, blk_seq_num);
Patrick Delaunay57da0602017-07-19 16:39:24 +0200355 dfu_transaction_cleanup(dfu);
Patrick Delaunayd4710322021-05-18 15:12:12 +0200356 dfu_error_callback(dfu, "Wrong sequence number");
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200357 return -1;
358 }
359
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000360 /* DFU 1.1 standard says:
361 * The wBlockNum field is a block sequence number. It increments each
362 * time a block is transferred, wrapping to zero from 65,535. It is used
363 * to provide useful context to the DFU loader in the device."
364 *
365 * This means that it's a 16 bit counter that roll-overs at
366 * 0xffff -> 0x0000. By having a typical 4K transfer block
367 * we roll-over at exactly 256MB. Not very fun to debug.
368 *
369 * Handling rollover, and having an inited variable,
370 * makes things work.
371 */
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200372
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000373 /* handle rollover */
374 dfu->i_blk_seq_num = (dfu->i_blk_seq_num + 1) & 0xffff;
375
376 /* flush buffer if overflow */
377 if ((dfu->i_buf + size) > dfu->i_buf_end) {
Stephen Warren3ee95932014-06-11 12:48:08 -0600378 ret = dfu_write_buffer_drain(dfu);
379 if (ret) {
Patrick Delaunay57da0602017-07-19 16:39:24 +0200380 dfu_transaction_cleanup(dfu);
Patrick Delaunayd4710322021-05-18 15:12:12 +0200381 dfu_error_callback(dfu, "DFU write error");
Stephen Warren3ee95932014-06-11 12:48:08 -0600382 return ret;
383 }
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200384 }
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000385
386 /* we should be in buffer now (if not then size too large) */
387 if ((dfu->i_buf + size) > dfu->i_buf_end) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900388 pr_err("Buffer overflow! (0x%p + 0x%x > 0x%p)\n", dfu->i_buf,
Lukasz Majewskia7d2c3c2013-09-10 15:29:22 +0200389 size, dfu->i_buf_end);
Patrick Delaunay57da0602017-07-19 16:39:24 +0200390 dfu_transaction_cleanup(dfu);
Patrick Delaunayd4710322021-05-18 15:12:12 +0200391 dfu_error_callback(dfu, "Buffer overflow");
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000392 return -1;
393 }
394
395 memcpy(dfu->i_buf, buf, size);
396 dfu->i_buf += size;
397
398 /* if end or if buffer full flush */
399 if (size == 0 || (dfu->i_buf + size) > dfu->i_buf_end) {
Stephen Warren3ee95932014-06-11 12:48:08 -0600400 ret = dfu_write_buffer_drain(dfu);
401 if (ret) {
Patrick Delaunay57da0602017-07-19 16:39:24 +0200402 dfu_transaction_cleanup(dfu);
Patrick Delaunayd4710322021-05-18 15:12:12 +0200403 dfu_error_callback(dfu, "DFU write error");
Stephen Warren3ee95932014-06-11 12:48:08 -0600404 return ret;
405 }
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000406 }
407
Stephen Warren3ee95932014-06-11 12:48:08 -0600408 return 0;
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000409}
410
411static int dfu_read_buffer_fill(struct dfu_entity *dfu, void *buf, int size)
412{
413 long chunk;
414 int ret, readn;
415
416 readn = 0;
417 while (size > 0) {
418 /* get chunk that can be read */
Masahiro Yamadab4141192014-11-07 03:03:31 +0900419 chunk = min((long)size, dfu->b_left);
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000420 /* consume */
421 if (chunk > 0) {
422 memcpy(buf, dfu->i_buf, chunk);
Lukasz Majewskibd694242014-05-12 10:43:36 +0200423 if (dfu_hash_algo)
424 dfu_hash_algo->hash_update(dfu_hash_algo,
425 &dfu->crc, buf,
426 chunk, 0);
427
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000428 dfu->i_buf += chunk;
429 dfu->b_left -= chunk;
430 size -= chunk;
431 buf += chunk;
432 readn += chunk;
433 }
434
435 /* all done */
436 if (size > 0) {
437 /* no more to read */
438 if (dfu->r_left == 0)
439 break;
440
441 dfu->i_buf = dfu->i_buf_start;
442 dfu->b_left = dfu->i_buf_end - dfu->i_buf_start;
443
444 /* got to read, but buffer is empty */
445 if (dfu->b_left > dfu->r_left)
446 dfu->b_left = dfu->r_left;
447 ret = dfu->read_medium(dfu, dfu->offset, dfu->i_buf,
448 &dfu->b_left);
449 if (ret != 0) {
450 debug("%s: Read error!\n", __func__);
451 return ret;
452 }
Patrick Delaunay0de10222019-10-14 09:28:03 +0200453 if (dfu->b_left == 0)
454 break;
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000455 dfu->offset += dfu->b_left;
456 dfu->r_left -= dfu->b_left;
457
458 puts("#");
459 }
460 }
461
462 return readn;
463}
464
465int dfu_read(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
466{
467 int ret = 0;
468
469 debug("%s: name: %s buf: 0x%p size: 0x%x p_num: 0x%x i_buf: 0x%p\n",
470 __func__, dfu->name, buf, size, blk_seq_num, dfu->i_buf);
471
Patrick Delaunay6fa8ddd2017-07-19 16:39:25 +0200472 ret = dfu_transaction_initiate(dfu, true);
473 if (ret < 0)
474 return ret;
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000475
476 if (dfu->i_blk_seq_num != blk_seq_num) {
477 printf("%s: Wrong sequence number! [%d] [%d]\n",
478 __func__, dfu->i_blk_seq_num, blk_seq_num);
479 return -1;
480 }
481 /* handle rollover */
482 dfu->i_blk_seq_num = (dfu->i_blk_seq_num + 1) & 0xffff;
483
484 ret = dfu_read_buffer_fill(dfu, buf, size);
485 if (ret < 0) {
486 printf("%s: Failed to fill buffer\n", __func__);
487 return -1;
488 }
489
490 if (ret < size) {
Lukasz Majewskibd694242014-05-12 10:43:36 +0200491 if (dfu_hash_algo)
492 debug("%s: %s %s: 0x%x\n", __func__, dfu->name,
493 dfu_hash_algo->name, dfu->crc);
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000494 puts("\nUPLOAD ... done\nCtrl+C to exit ...\n");
495
Patrick Delaunay57da0602017-07-19 16:39:24 +0200496 dfu_transaction_cleanup(dfu);
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000497 }
498
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200499 return ret;
500}
501
502static int dfu_fill_entity(struct dfu_entity *dfu, char *s, int alt,
Stephen Warrendd648272014-06-11 16:03:33 -0600503 char *interface, char *devstr)
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200504{
Masami Hiramatsu53b40632022-01-31 11:52:37 +0900505 char *argv[DFU_MAX_ENTITY_ARGS];
506 int argc;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200507 char *st;
508
Stephen Warrendd648272014-06-11 16:03:33 -0600509 debug("%s: %s interface: %s dev: %s\n", __func__, s, interface, devstr);
Masami Hiramatsu8db74c12022-01-31 11:52:29 +0900510 st = strsep(&s, " \t");
Masami Hiramatsud8ae90a2022-01-31 11:52:20 +0900511 strlcpy(dfu->name, st, DFU_NAME_SIZE);
Masami Hiramatsu53b40632022-01-31 11:52:37 +0900512
513 /* Parse arguments */
514 for (argc = 0; s && argc < DFU_MAX_ENTITY_ARGS; argc++) {
515 s = skip_spaces(s);
516 if (!*s)
517 break;
518 argv[argc] = strsep(&s, " \t");
519 }
520
521 if (argc == DFU_MAX_ENTITY_ARGS && s) {
522 s = skip_spaces(s);
523 if (*s) {
524 log_err("Too many arguments for %s\n", dfu->name);
525 return -EINVAL;
526 }
527 }
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200528
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200529 dfu->alt = alt;
Stephen Warren7ac1b412014-06-11 16:03:34 -0600530 dfu->max_buf_size = 0;
Stephen Warrencb7bd2e2014-06-11 16:03:35 -0600531 dfu->free_entity = NULL;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200532
533 /* Specific for mmc device */
534 if (strcmp(interface, "mmc") == 0) {
Masami Hiramatsu53b40632022-01-31 11:52:37 +0900535 if (dfu_fill_entity_mmc(dfu, devstr, argv, argc))
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200536 return -1;
Patrick Delaunay6015af22019-10-14 09:28:04 +0200537 } else if (strcmp(interface, "mtd") == 0) {
Masami Hiramatsu53b40632022-01-31 11:52:37 +0900538 if (dfu_fill_entity_mtd(dfu, devstr, argv, argc))
Patrick Delaunay6015af22019-10-14 09:28:04 +0200539 return -1;
Pantelis Antoniouc6631762013-03-14 05:32:52 +0000540 } else if (strcmp(interface, "nand") == 0) {
Masami Hiramatsu53b40632022-01-31 11:52:37 +0900541 if (dfu_fill_entity_nand(dfu, devstr, argv, argc))
Pantelis Antoniouc6631762013-03-14 05:32:52 +0000542 return -1;
Afzal Mohammeda9479f02013-09-18 01:15:24 +0530543 } else if (strcmp(interface, "ram") == 0) {
Masami Hiramatsu53b40632022-01-31 11:52:37 +0900544 if (dfu_fill_entity_ram(dfu, devstr, argv, argc))
Afzal Mohammeda9479f02013-09-18 01:15:24 +0530545 return -1;
Stephen Warren6f12ebf2014-06-11 16:03:36 -0600546 } else if (strcmp(interface, "sf") == 0) {
Masami Hiramatsu53b40632022-01-31 11:52:37 +0900547 if (dfu_fill_entity_sf(dfu, devstr, argv, argc))
Stephen Warren6f12ebf2014-06-11 16:03:36 -0600548 return -1;
Patrick Delaunayec44cac2019-10-14 09:28:06 +0200549 } else if (strcmp(interface, "virt") == 0) {
Masami Hiramatsu53b40632022-01-31 11:52:37 +0900550 if (dfu_fill_entity_virt(dfu, devstr, argv, argc))
Patrick Delaunayec44cac2019-10-14 09:28:06 +0200551 return -1;
Caleb Connolly5e602aa2023-12-06 22:01:30 +0000552 } else if (strcmp(interface, "scsi") == 0) {
553 if (dfu_fill_entity_scsi(dfu, devstr, argv, argc))
554 return -1;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200555 } else {
556 printf("%s: Device %s not (yet) supported!\n",
557 __func__, interface);
558 return -1;
559 }
Stephen Warren806bd242015-09-04 22:03:46 -0600560 dfu_get_buf(dfu);
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200561
562 return 0;
563}
564
565void dfu_free_entities(void)
566{
567 struct dfu_entity *dfu, *p, *t = NULL;
568
Stephen Warren806bd242015-09-04 22:03:46 -0600569 dfu_free_buf();
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200570 list_for_each_entry_safe_reverse(dfu, p, &dfu_list, list) {
571 list_del(&dfu->list);
Stephen Warrencb7bd2e2014-06-11 16:03:35 -0600572 if (dfu->free_entity)
573 dfu->free_entity(dfu);
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200574 t = dfu;
575 }
576 if (t)
577 free(t);
578 INIT_LIST_HEAD(&dfu_list);
Przemyslaw Marczakb627eb42014-02-28 18:53:37 +0100579
580 alt_num_cnt = 0;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200581}
582
Patrick Delaunay9ada6832019-10-14 09:28:01 +0200583int dfu_alt_init(int num, struct dfu_entity **dfu)
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200584{
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200585 char *s;
Patrick Delaunay9ada6832019-10-14 09:28:01 +0200586 int ret;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200587
Patrick Delaunay9ada6832019-10-14 09:28:01 +0200588 dfu_alt_num = num;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200589 debug("%s: dfu_alt_num=%d\n", __func__, dfu_alt_num);
590
Lukasz Majewskibd694242014-05-12 10:43:36 +0200591 dfu_hash_algo = NULL;
592 s = dfu_get_hash_algo();
593 if (s) {
594 ret = hash_lookup_algo(s, &dfu_hash_algo);
595 if (ret)
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900596 pr_err("Hash algorithm %s not supported\n", s);
Lukasz Majewskibd694242014-05-12 10:43:36 +0200597 }
598
Patrick Delaunay9ada6832019-10-14 09:28:01 +0200599 *dfu = calloc(sizeof(struct dfu_entity), dfu_alt_num);
600 if (!*dfu)
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200601 return -1;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200602
Patrick Delaunay9ada6832019-10-14 09:28:01 +0200603 return 0;
604}
605
606int dfu_alt_add(struct dfu_entity *dfu, char *interface, char *devstr, char *s)
607{
608 struct dfu_entity *p_dfu;
609 int ret;
610
611 if (alt_num_cnt >= dfu_alt_num)
612 return -1;
613
614 p_dfu = &dfu[alt_num_cnt];
615 ret = dfu_fill_entity(p_dfu, s, alt_num_cnt, interface, devstr);
616 if (ret)
617 return -1;
618
619 list_add_tail(&p_dfu->list, &dfu_list);
620 alt_num_cnt++;
621
622 return 0;
623}
624
625int dfu_config_entities(char *env, char *interface, char *devstr)
626{
627 struct dfu_entity *dfu;
628 int i, ret;
629 char *s;
630
631 ret = dfu_alt_init(dfu_find_alt_num(env), &dfu);
632 if (ret)
633 return -1;
634
635 for (i = 0; i < dfu_alt_num; i++) {
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200636 s = strsep(&env, ";");
Ravi Gunasekarande060832023-05-18 12:06:23 +0530637 s = skip_spaces(s);
Patrick Delaunay9ada6832019-10-14 09:28:01 +0200638 ret = dfu_alt_add(dfu, interface, devstr, s);
Peng Fan5d8fae72016-05-03 10:24:52 +0800639 if (ret) {
Sam Protsenkofeaa7852018-07-13 16:35:45 +0300640 /* We will free "dfu" in dfu_free_entities() */
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200641 return -1;
Peng Fan5d8fae72016-05-03 10:24:52 +0800642 }
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200643 }
644
645 return 0;
646}
647
648const char *dfu_get_dev_type(enum dfu_device_type t)
649{
Patrick Delaunay909b6902019-10-14 09:27:58 +0200650 const char *const dev_t[] = {NULL, "eMMC", "OneNAND", "NAND", "RAM",
Caleb Connolly5e602aa2023-12-06 22:01:30 +0000651 "SF", "MTD", "VIRT", "SCSI"};
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200652 return dev_t[t];
653}
654
655const char *dfu_get_layout(enum dfu_layout l)
656{
Patrick Delaunay909b6902019-10-14 09:27:58 +0200657 const char *const dfu_layout[] = {NULL, "RAW_ADDR", "FAT", "EXT2",
Marek Szyprowskic533f942020-12-22 11:32:22 +0100658 "EXT3", "EXT4", "RAM_ADDR", "SKIP",
659 "SCRIPT" };
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200660 return dfu_layout[l];
661}
662
663void dfu_show_entities(void)
664{
665 struct dfu_entity *dfu;
666
667 puts("DFU alt settings list:\n");
668
669 list_for_each_entry(dfu, &dfu_list, list) {
670 printf("dev: %s alt: %d name: %s layout: %s\n",
671 dfu_get_dev_type(dfu->dev_type), dfu->alt,
672 dfu->name, dfu_get_layout(dfu->layout));
673 }
674}
675
676int dfu_get_alt_number(void)
677{
678 return dfu_alt_num;
679}
680
681struct dfu_entity *dfu_get_entity(int alt)
682{
683 struct dfu_entity *dfu;
684
685 list_for_each_entry(dfu, &dfu_list, list) {
686 if (dfu->alt == alt)
687 return dfu;
688 }
689
690 return NULL;
691}
Lukasz Majewskifed936e2013-10-08 14:30:38 +0200692
693int dfu_get_alt(char *name)
694{
695 struct dfu_entity *dfu;
Lukasz Majewski5610b052014-11-05 10:54:16 +0100696 char *str;
Lukasz Majewskifed936e2013-10-08 14:30:38 +0200697
698 list_for_each_entry(dfu, &dfu_list, list) {
Lukasz Majewski5610b052014-11-05 10:54:16 +0100699 if (dfu->name[0] != '/') {
700 if (!strncmp(dfu->name, name, strlen(dfu->name)))
701 return dfu->alt;
702 } else {
703 /*
704 * One must also consider absolute path
705 * (/boot/bin/uImage) available at dfu->name when
706 * compared "plain" file name (uImage)
707 *
708 * It is the case for e.g. thor gadget where lthor SW
709 * sends only the file name, so only the very last part
710 * of path must be checked for equality
711 */
712
713 str = strstr(dfu->name, name);
714 if (!str)
715 continue;
716
717 /*
718 * Check if matching substring is the last element of
719 * dfu->name (uImage)
720 */
721 if (strlen(dfu->name) ==
722 ((str - dfu->name) + strlen(name)))
723 return dfu->alt;
724 }
Lukasz Majewskifed936e2013-10-08 14:30:38 +0200725 }
726
727 return -ENODEV;
728}
Lukasz Majewski2092e462015-08-24 00:21:46 +0200729
730int dfu_write_from_mem_addr(struct dfu_entity *dfu, void *buf, int size)
731{
732 unsigned long dfu_buf_size, write, left = size;
733 int i, ret = 0;
734 void *dp = buf;
735
736 /*
737 * Here we must call dfu_get_buf(dfu) first to be sure that dfu_buf_size
738 * has been properly initialized - e.g. if "dfu_bufsiz" has been taken
739 * into account.
740 */
741 dfu_get_buf(dfu);
742 dfu_buf_size = dfu_get_buf_size();
743 debug("%s: dfu buf size: %lu\n", __func__, dfu_buf_size);
744
745 for (i = 0; left > 0; i++) {
746 write = min(dfu_buf_size, left);
747
748 debug("%s: dp: 0x%p left: %lu write: %lu\n", __func__,
749 dp, left, write);
750 ret = dfu_write(dfu, dp, write, i);
751 if (ret) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900752 pr_err("DFU write failed\n");
Lukasz Majewski2092e462015-08-24 00:21:46 +0200753 return ret;
754 }
755
756 dp += write;
757 left -= write;
758 }
759
760 ret = dfu_flush(dfu, NULL, 0, i);
761 if (ret)
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900762 pr_err("DFU flush failed!");
Heinrich Schuchardtf8d7e6e2021-03-05 18:36:37 +0100763 puts("\n");
Lukasz Majewski2092e462015-08-24 00:21:46 +0200764
765 return ret;
766}