blob: 516dda6179627cff6e1e649ad1b47f387c25ad72 [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>
20
AKASHI Takahiro6beaa472020-10-29 13:47:44 +090021LIST_HEAD(dfu_list);
Lukasz Majewskif22b11c2012-08-06 14:41:07 +020022static int dfu_alt_num;
Przemyslaw Marczakb627eb42014-02-28 18:53:37 +010023static int alt_num_cnt;
Lukasz Majewskibd694242014-05-12 10:43:36 +020024static struct hash_algo *dfu_hash_algo;
Andy Shevchenko98a8f442019-11-27 18:12:15 +020025#ifdef CONFIG_DFU_TIMEOUT
26static unsigned long dfu_timeout = 0;
27#endif
Lukasz Majewskif22b11c2012-08-06 14:41:07 +020028
Marek Szyprowskic533f942020-12-22 11:32:22 +010029bool dfu_reinit_needed = false;
30
Lukasz Majewski1cc03c52014-08-25 11:07:28 +020031/*
Patrick Delaunay067c13c2019-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 Delaunayd4710322021-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 Majewski1cc03c52014-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 Majewski6bed7ce2013-07-18 13:19:14 +020067{
B, Ravi66928af2017-05-04 15:45:29 +053068#ifdef CONFIG_SPL_DFU_NO_RESET
69 return false;
70#else
Lukasz Majewski1cc03c52014-08-25 11:07:28 +020071 return true;
B, Ravi66928af2017-05-04 15:45:29 +053072#endif
Lukasz Majewski6bed7ce2013-07-18 13:19:14 +020073}
74
Andy Shevchenko98a8f442019-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 Majewskif22b11c2012-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 Delaunayfebabe32019-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 Hiramatsu8db74c12022-01-31 11:52:29 +0900126 i = strsep(&s, " \t");
Patrick Delaunayfebabe32019-10-14 09:28:02 +0200127 if (!i)
128 break;
Masami Hiramatsu8db74c12022-01-31 11:52:29 +0900129 s = skip_spaces(s);
Patrick Delaunayfebabe32019-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, ";");
138 ret = dfu_alt_add(dfu, i, d, part);
139 if (ret)
140 return ret;
141 } while (a);
142 }
143
144 return ret;
145}
146
Stephen Warrendd648272014-06-11 16:03:33 -0600147int dfu_init_env_entities(char *interface, char *devstr)
Lukasz Majewski765c5ae2013-09-11 14:53:35 +0200148{
149 const char *str_env;
150 char *env_bkp;
Sam Protsenko87a8ca92018-07-13 16:35:46 +0300151 int ret = 0;
Lukasz Majewski765c5ae2013-09-11 14:53:35 +0200152
Marek Szyprowskic533f942020-12-22 11:32:22 +0100153 dfu_reinit_needed = false;
154
Przemyslaw Marczak899a5282015-02-17 12:24:11 +0100155#ifdef CONFIG_SET_DFU_ALT_INFO
156 set_dfu_alt_info(interface, devstr);
157#endif
Simon Glass00caae62017-08-03 12:22:12 -0600158 str_env = env_get("dfu_alt_info");
Lukasz Majewski765c5ae2013-09-11 14:53:35 +0200159 if (!str_env) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900160 pr_err("\"dfu_alt_info\" env variable not defined!\n");
Lukasz Majewski765c5ae2013-09-11 14:53:35 +0200161 return -EINVAL;
162 }
163
164 env_bkp = strdup(str_env);
Patrick Delaunayfebabe32019-10-14 09:28:02 +0200165 if (!interface && !devstr)
166 ret = dfu_config_interfaces(env_bkp);
167 else
168 ret = dfu_config_entities(env_bkp, interface, devstr);
169
Lukasz Majewski765c5ae2013-09-11 14:53:35 +0200170 if (ret) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900171 pr_err("DFU entities configuration failed!\n");
Sam Protsenko28a5c882018-07-13 16:35:47 +0300172 pr_err("(partition table does not match dfu_alt_info?)\n");
Sam Protsenko87a8ca92018-07-13 16:35:46 +0300173 goto done;
Lukasz Majewski765c5ae2013-09-11 14:53:35 +0200174 }
175
Sam Protsenko87a8ca92018-07-13 16:35:46 +0300176done:
Lukasz Majewski765c5ae2013-09-11 14:53:35 +0200177 free(env_bkp);
Sam Protsenko87a8ca92018-07-13 16:35:46 +0300178 return ret;
Lukasz Majewski765c5ae2013-09-11 14:53:35 +0200179}
180
Heiko Schochere7e75c72013-06-12 06:05:51 +0200181static unsigned char *dfu_buf;
Lukasz Majewskia7e68922015-07-08 23:43:18 +0200182static unsigned long dfu_buf_size;
Patrick Delaunayfebabe32019-10-14 09:28:02 +0200183static enum dfu_device_type dfu_buf_device_type;
Heiko Schochere7e75c72013-06-12 06:05:51 +0200184
Lukasz Majewskid4278262013-10-08 14:30:39 +0200185unsigned char *dfu_free_buf(void)
Heiko Schochere7e75c72013-06-12 06:05:51 +0200186{
187 free(dfu_buf);
188 dfu_buf = NULL;
189 return dfu_buf;
190}
191
Lukasz Majewski4fb12782013-12-09 16:20:13 +0100192unsigned long dfu_get_buf_size(void)
193{
194 return dfu_buf_size;
195}
196
Stephen Warren7ac1b412014-06-11 16:03:34 -0600197unsigned char *dfu_get_buf(struct dfu_entity *dfu)
Heiko Schochere7e75c72013-06-12 06:05:51 +0200198{
199 char *s;
200
Patrick Delaunayfebabe32019-10-14 09:28:02 +0200201 /* manage several entity with several contraint */
202 if (dfu_buf && dfu->dev_type != dfu_buf_device_type)
203 dfu_free_buf();
204
Heiko Schochere7e75c72013-06-12 06:05:51 +0200205 if (dfu_buf != NULL)
206 return dfu_buf;
207
Simon Glass00caae62017-08-03 12:22:12 -0600208 s = env_get("dfu_bufsiz");
Przemyslaw Marczakf597fc32014-12-15 10:34:11 +0100209 if (s)
210 dfu_buf_size = (unsigned long)simple_strtol(s, NULL, 0);
211
212 if (!s || !dfu_buf_size)
213 dfu_buf_size = CONFIG_SYS_DFU_DATA_BUF_SIZE;
214
Stephen Warren7ac1b412014-06-11 16:03:34 -0600215 if (dfu->max_buf_size && dfu_buf_size > dfu->max_buf_size)
216 dfu_buf_size = dfu->max_buf_size;
Heiko Schochere7e75c72013-06-12 06:05:51 +0200217
218 dfu_buf = memalign(CONFIG_SYS_CACHELINE_SIZE, dfu_buf_size);
219 if (dfu_buf == NULL)
220 printf("%s: Could not memalign 0x%lx bytes\n",
221 __func__, dfu_buf_size);
222
Patrick Delaunayfebabe32019-10-14 09:28:02 +0200223 dfu_buf_device_type = dfu->dev_type;
Heiko Schochere7e75c72013-06-12 06:05:51 +0200224 return dfu_buf;
225}
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200226
Lukasz Majewskibd694242014-05-12 10:43:36 +0200227static char *dfu_get_hash_algo(void)
228{
229 char *s;
230
Simon Glass00caae62017-08-03 12:22:12 -0600231 s = env_get("dfu_hash_algo");
Lukasz Majewski3d83e672014-06-10 12:25:59 +0200232 if (!s)
Lukasz Majewskibd694242014-05-12 10:43:36 +0200233 return NULL;
234
Lukasz Majewski3d83e672014-06-10 12:25:59 +0200235 if (!strcmp(s, "crc32")) {
236 debug("%s: DFU hash method: %s\n", __func__, s);
237 return s;
238 }
239
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900240 pr_err("DFU hash method: %s not supported!\n", s);
Lukasz Majewskibd694242014-05-12 10:43:36 +0200241 return NULL;
242}
243
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000244static int dfu_write_buffer_drain(struct dfu_entity *dfu)
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200245{
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000246 long w_size;
247 int ret;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200248
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000249 /* flush size? */
250 w_size = dfu->i_buf - dfu->i_buf_start;
251 if (w_size == 0)
252 return 0;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200253
Lukasz Majewskibd694242014-05-12 10:43:36 +0200254 if (dfu_hash_algo)
255 dfu_hash_algo->hash_update(dfu_hash_algo, &dfu->crc,
256 dfu->i_buf_start, w_size, 0);
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200257
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000258 ret = dfu->write_medium(dfu, dfu->offset, dfu->i_buf_start, &w_size);
259 if (ret)
260 debug("%s: Write error!\n", __func__);
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200261
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000262 /* point back */
263 dfu->i_buf = dfu->i_buf_start;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200264
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000265 /* update offset */
266 dfu->offset += w_size;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200267
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000268 puts("#");
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200269
270 return ret;
271}
272
Patrick Delaunay57da0602017-07-19 16:39:24 +0200273void dfu_transaction_cleanup(struct dfu_entity *dfu)
Stephen Warren3ee95932014-06-11 12:48:08 -0600274{
275 /* clear everything */
Stephen Warren3ee95932014-06-11 12:48:08 -0600276 dfu->crc = 0;
277 dfu->offset = 0;
278 dfu->i_blk_seq_num = 0;
Patrick Delaunay6fa8ddd2017-07-19 16:39:25 +0200279 dfu->i_buf_start = dfu_get_buf(dfu);
280 dfu->i_buf_end = dfu->i_buf_start;
Stephen Warren3ee95932014-06-11 12:48:08 -0600281 dfu->i_buf = dfu->i_buf_start;
Patrick Delaunay57da0602017-07-19 16:39:24 +0200282 dfu->r_left = 0;
283 dfu->b_left = 0;
284 dfu->bad_skip = 0;
285
Stephen Warren3ee95932014-06-11 12:48:08 -0600286 dfu->inited = 0;
287}
288
Patrick Delaunay6fa8ddd2017-07-19 16:39:25 +0200289int dfu_transaction_initiate(struct dfu_entity *dfu, bool read)
290{
291 int ret = 0;
292
293 if (dfu->inited)
294 return 0;
295
296 dfu_transaction_cleanup(dfu);
297
298 if (dfu->i_buf_start == NULL)
299 return -ENOMEM;
300
301 dfu->i_buf_end = dfu->i_buf_start + dfu_get_buf_size();
302
303 if (read) {
304 ret = dfu->get_medium_size(dfu, &dfu->r_left);
305 if (ret < 0)
306 return ret;
307 debug("%s: %s %lld [B]\n", __func__, dfu->name, dfu->r_left);
308 }
309
310 dfu->inited = 1;
Patrick Delaunay067c13c2019-10-14 09:28:07 +0200311 dfu_initiated_callback(dfu);
Patrick Delaunay6fa8ddd2017-07-19 16:39:25 +0200312
313 return 0;
314}
315
Heiko Schochera2199af2014-03-18 08:09:55 +0100316int dfu_flush(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
317{
318 int ret = 0;
319
Przemyslaw Marczak1aa4bdc2014-05-12 12:05:33 +0200320 ret = dfu_write_buffer_drain(dfu);
321 if (ret)
322 return ret;
323
Heiko Schochera2199af2014-03-18 08:09:55 +0100324 if (dfu->flush_medium)
325 ret = dfu->flush_medium(dfu);
326
Lukasz Majewskibd694242014-05-12 10:43:36 +0200327 if (dfu_hash_algo)
328 printf("\nDFU complete %s: 0x%08x\n", dfu_hash_algo->name,
329 dfu->crc);
Heiko Schochera2199af2014-03-18 08:09:55 +0100330
Patrick Delaunay067c13c2019-10-14 09:28:07 +0200331 dfu_flush_callback(dfu);
332
Patrick Delaunay57da0602017-07-19 16:39:24 +0200333 dfu_transaction_cleanup(dfu);
Heiko Schochera2199af2014-03-18 08:09:55 +0100334
335 return ret;
336}
337
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000338int dfu_write(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200339{
Stephen Warren3ee95932014-06-11 12:48:08 -0600340 int ret;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200341
Stephen Warrene621c7a2015-07-22 14:54:04 -0600342 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 +0000343 __func__, dfu->name, buf, size, blk_seq_num, dfu->offset,
Stephen Warrene621c7a2015-07-22 14:54:04 -0600344 (unsigned long)(dfu->i_buf - dfu->i_buf_start));
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200345
Patrick Delaunay6fa8ddd2017-07-19 16:39:25 +0200346 ret = dfu_transaction_initiate(dfu, false);
347 if (ret < 0)
348 return ret;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200349
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000350 if (dfu->i_blk_seq_num != blk_seq_num) {
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200351 printf("%s: Wrong sequence number! [%d] [%d]\n",
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000352 __func__, dfu->i_blk_seq_num, blk_seq_num);
Patrick Delaunay57da0602017-07-19 16:39:24 +0200353 dfu_transaction_cleanup(dfu);
Patrick Delaunayd4710322021-05-18 15:12:12 +0200354 dfu_error_callback(dfu, "Wrong sequence number");
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200355 return -1;
356 }
357
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000358 /* DFU 1.1 standard says:
359 * The wBlockNum field is a block sequence number. It increments each
360 * time a block is transferred, wrapping to zero from 65,535. It is used
361 * to provide useful context to the DFU loader in the device."
362 *
363 * This means that it's a 16 bit counter that roll-overs at
364 * 0xffff -> 0x0000. By having a typical 4K transfer block
365 * we roll-over at exactly 256MB. Not very fun to debug.
366 *
367 * Handling rollover, and having an inited variable,
368 * makes things work.
369 */
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200370
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000371 /* handle rollover */
372 dfu->i_blk_seq_num = (dfu->i_blk_seq_num + 1) & 0xffff;
373
374 /* flush buffer if overflow */
375 if ((dfu->i_buf + size) > dfu->i_buf_end) {
Stephen Warren3ee95932014-06-11 12:48:08 -0600376 ret = dfu_write_buffer_drain(dfu);
377 if (ret) {
Patrick Delaunay57da0602017-07-19 16:39:24 +0200378 dfu_transaction_cleanup(dfu);
Patrick Delaunayd4710322021-05-18 15:12:12 +0200379 dfu_error_callback(dfu, "DFU write error");
Stephen Warren3ee95932014-06-11 12:48:08 -0600380 return ret;
381 }
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200382 }
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000383
384 /* we should be in buffer now (if not then size too large) */
385 if ((dfu->i_buf + size) > dfu->i_buf_end) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900386 pr_err("Buffer overflow! (0x%p + 0x%x > 0x%p)\n", dfu->i_buf,
Lukasz Majewskia7d2c3c2013-09-10 15:29:22 +0200387 size, dfu->i_buf_end);
Patrick Delaunay57da0602017-07-19 16:39:24 +0200388 dfu_transaction_cleanup(dfu);
Patrick Delaunayd4710322021-05-18 15:12:12 +0200389 dfu_error_callback(dfu, "Buffer overflow");
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000390 return -1;
391 }
392
393 memcpy(dfu->i_buf, buf, size);
394 dfu->i_buf += size;
395
396 /* if end or if buffer full flush */
397 if (size == 0 || (dfu->i_buf + size) > dfu->i_buf_end) {
Stephen Warren3ee95932014-06-11 12:48:08 -0600398 ret = dfu_write_buffer_drain(dfu);
399 if (ret) {
Patrick Delaunay57da0602017-07-19 16:39:24 +0200400 dfu_transaction_cleanup(dfu);
Patrick Delaunayd4710322021-05-18 15:12:12 +0200401 dfu_error_callback(dfu, "DFU write error");
Stephen Warren3ee95932014-06-11 12:48:08 -0600402 return ret;
403 }
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000404 }
405
Stephen Warren3ee95932014-06-11 12:48:08 -0600406 return 0;
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000407}
408
409static int dfu_read_buffer_fill(struct dfu_entity *dfu, void *buf, int size)
410{
411 long chunk;
412 int ret, readn;
413
414 readn = 0;
415 while (size > 0) {
416 /* get chunk that can be read */
Masahiro Yamadab4141192014-11-07 03:03:31 +0900417 chunk = min((long)size, dfu->b_left);
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000418 /* consume */
419 if (chunk > 0) {
420 memcpy(buf, dfu->i_buf, chunk);
Lukasz Majewskibd694242014-05-12 10:43:36 +0200421 if (dfu_hash_algo)
422 dfu_hash_algo->hash_update(dfu_hash_algo,
423 &dfu->crc, buf,
424 chunk, 0);
425
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000426 dfu->i_buf += chunk;
427 dfu->b_left -= chunk;
428 size -= chunk;
429 buf += chunk;
430 readn += chunk;
431 }
432
433 /* all done */
434 if (size > 0) {
435 /* no more to read */
436 if (dfu->r_left == 0)
437 break;
438
439 dfu->i_buf = dfu->i_buf_start;
440 dfu->b_left = dfu->i_buf_end - dfu->i_buf_start;
441
442 /* got to read, but buffer is empty */
443 if (dfu->b_left > dfu->r_left)
444 dfu->b_left = dfu->r_left;
445 ret = dfu->read_medium(dfu, dfu->offset, dfu->i_buf,
446 &dfu->b_left);
447 if (ret != 0) {
448 debug("%s: Read error!\n", __func__);
449 return ret;
450 }
Patrick Delaunay0de10222019-10-14 09:28:03 +0200451 if (dfu->b_left == 0)
452 break;
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000453 dfu->offset += dfu->b_left;
454 dfu->r_left -= dfu->b_left;
455
456 puts("#");
457 }
458 }
459
460 return readn;
461}
462
463int dfu_read(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
464{
465 int ret = 0;
466
467 debug("%s: name: %s buf: 0x%p size: 0x%x p_num: 0x%x i_buf: 0x%p\n",
468 __func__, dfu->name, buf, size, blk_seq_num, dfu->i_buf);
469
Patrick Delaunay6fa8ddd2017-07-19 16:39:25 +0200470 ret = dfu_transaction_initiate(dfu, true);
471 if (ret < 0)
472 return ret;
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000473
474 if (dfu->i_blk_seq_num != blk_seq_num) {
475 printf("%s: Wrong sequence number! [%d] [%d]\n",
476 __func__, dfu->i_blk_seq_num, blk_seq_num);
477 return -1;
478 }
479 /* handle rollover */
480 dfu->i_blk_seq_num = (dfu->i_blk_seq_num + 1) & 0xffff;
481
482 ret = dfu_read_buffer_fill(dfu, buf, size);
483 if (ret < 0) {
484 printf("%s: Failed to fill buffer\n", __func__);
485 return -1;
486 }
487
488 if (ret < size) {
Lukasz Majewskibd694242014-05-12 10:43:36 +0200489 if (dfu_hash_algo)
490 debug("%s: %s %s: 0x%x\n", __func__, dfu->name,
491 dfu_hash_algo->name, dfu->crc);
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000492 puts("\nUPLOAD ... done\nCtrl+C to exit ...\n");
493
Patrick Delaunay57da0602017-07-19 16:39:24 +0200494 dfu_transaction_cleanup(dfu);
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000495 }
496
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200497 return ret;
498}
499
500static int dfu_fill_entity(struct dfu_entity *dfu, char *s, int alt,
Stephen Warrendd648272014-06-11 16:03:33 -0600501 char *interface, char *devstr)
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200502{
Masami Hiramatsu53b40632022-01-31 11:52:37 +0900503 char *argv[DFU_MAX_ENTITY_ARGS];
504 int argc;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200505 char *st;
506
Stephen Warrendd648272014-06-11 16:03:33 -0600507 debug("%s: %s interface: %s dev: %s\n", __func__, s, interface, devstr);
Masami Hiramatsu8db74c12022-01-31 11:52:29 +0900508 st = strsep(&s, " \t");
Masami Hiramatsud8ae90a2022-01-31 11:52:20 +0900509 strlcpy(dfu->name, st, DFU_NAME_SIZE);
Masami Hiramatsu53b40632022-01-31 11:52:37 +0900510
511 /* Parse arguments */
512 for (argc = 0; s && argc < DFU_MAX_ENTITY_ARGS; argc++) {
513 s = skip_spaces(s);
514 if (!*s)
515 break;
516 argv[argc] = strsep(&s, " \t");
517 }
518
519 if (argc == DFU_MAX_ENTITY_ARGS && s) {
520 s = skip_spaces(s);
521 if (*s) {
522 log_err("Too many arguments for %s\n", dfu->name);
523 return -EINVAL;
524 }
525 }
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200526
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200527 dfu->alt = alt;
Stephen Warren7ac1b412014-06-11 16:03:34 -0600528 dfu->max_buf_size = 0;
Stephen Warrencb7bd2e2014-06-11 16:03:35 -0600529 dfu->free_entity = NULL;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200530
531 /* Specific for mmc device */
532 if (strcmp(interface, "mmc") == 0) {
Masami Hiramatsu53b40632022-01-31 11:52:37 +0900533 if (dfu_fill_entity_mmc(dfu, devstr, argv, argc))
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200534 return -1;
Patrick Delaunay6015af22019-10-14 09:28:04 +0200535 } else if (strcmp(interface, "mtd") == 0) {
Masami Hiramatsu53b40632022-01-31 11:52:37 +0900536 if (dfu_fill_entity_mtd(dfu, devstr, argv, argc))
Patrick Delaunay6015af22019-10-14 09:28:04 +0200537 return -1;
Pantelis Antoniouc6631762013-03-14 05:32:52 +0000538 } else if (strcmp(interface, "nand") == 0) {
Masami Hiramatsu53b40632022-01-31 11:52:37 +0900539 if (dfu_fill_entity_nand(dfu, devstr, argv, argc))
Pantelis Antoniouc6631762013-03-14 05:32:52 +0000540 return -1;
Afzal Mohammeda9479f02013-09-18 01:15:24 +0530541 } else if (strcmp(interface, "ram") == 0) {
Masami Hiramatsu53b40632022-01-31 11:52:37 +0900542 if (dfu_fill_entity_ram(dfu, devstr, argv, argc))
Afzal Mohammeda9479f02013-09-18 01:15:24 +0530543 return -1;
Stephen Warren6f12ebf2014-06-11 16:03:36 -0600544 } else if (strcmp(interface, "sf") == 0) {
Masami Hiramatsu53b40632022-01-31 11:52:37 +0900545 if (dfu_fill_entity_sf(dfu, devstr, argv, argc))
Stephen Warren6f12ebf2014-06-11 16:03:36 -0600546 return -1;
Patrick Delaunayec44cac2019-10-14 09:28:06 +0200547 } else if (strcmp(interface, "virt") == 0) {
Masami Hiramatsu53b40632022-01-31 11:52:37 +0900548 if (dfu_fill_entity_virt(dfu, devstr, argv, argc))
Patrick Delaunayec44cac2019-10-14 09:28:06 +0200549 return -1;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200550 } else {
551 printf("%s: Device %s not (yet) supported!\n",
552 __func__, interface);
553 return -1;
554 }
Stephen Warren806bd242015-09-04 22:03:46 -0600555 dfu_get_buf(dfu);
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200556
557 return 0;
558}
559
560void dfu_free_entities(void)
561{
562 struct dfu_entity *dfu, *p, *t = NULL;
563
Stephen Warren806bd242015-09-04 22:03:46 -0600564 dfu_free_buf();
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200565 list_for_each_entry_safe_reverse(dfu, p, &dfu_list, list) {
566 list_del(&dfu->list);
Stephen Warrencb7bd2e2014-06-11 16:03:35 -0600567 if (dfu->free_entity)
568 dfu->free_entity(dfu);
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200569 t = dfu;
570 }
571 if (t)
572 free(t);
573 INIT_LIST_HEAD(&dfu_list);
Przemyslaw Marczakb627eb42014-02-28 18:53:37 +0100574
575 alt_num_cnt = 0;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200576}
577
Patrick Delaunay9ada6832019-10-14 09:28:01 +0200578int dfu_alt_init(int num, struct dfu_entity **dfu)
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200579{
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200580 char *s;
Patrick Delaunay9ada6832019-10-14 09:28:01 +0200581 int ret;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200582
Patrick Delaunay9ada6832019-10-14 09:28:01 +0200583 dfu_alt_num = num;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200584 debug("%s: dfu_alt_num=%d\n", __func__, dfu_alt_num);
585
Lukasz Majewskibd694242014-05-12 10:43:36 +0200586 dfu_hash_algo = NULL;
587 s = dfu_get_hash_algo();
588 if (s) {
589 ret = hash_lookup_algo(s, &dfu_hash_algo);
590 if (ret)
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900591 pr_err("Hash algorithm %s not supported\n", s);
Lukasz Majewskibd694242014-05-12 10:43:36 +0200592 }
593
Patrick Delaunay9ada6832019-10-14 09:28:01 +0200594 *dfu = calloc(sizeof(struct dfu_entity), dfu_alt_num);
595 if (!*dfu)
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200596 return -1;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200597
Patrick Delaunay9ada6832019-10-14 09:28:01 +0200598 return 0;
599}
600
601int dfu_alt_add(struct dfu_entity *dfu, char *interface, char *devstr, char *s)
602{
603 struct dfu_entity *p_dfu;
604 int ret;
605
606 if (alt_num_cnt >= dfu_alt_num)
607 return -1;
608
609 p_dfu = &dfu[alt_num_cnt];
610 ret = dfu_fill_entity(p_dfu, s, alt_num_cnt, interface, devstr);
611 if (ret)
612 return -1;
613
614 list_add_tail(&p_dfu->list, &dfu_list);
615 alt_num_cnt++;
616
617 return 0;
618}
619
620int dfu_config_entities(char *env, char *interface, char *devstr)
621{
622 struct dfu_entity *dfu;
623 int i, ret;
624 char *s;
625
626 ret = dfu_alt_init(dfu_find_alt_num(env), &dfu);
627 if (ret)
628 return -1;
629
630 for (i = 0; i < dfu_alt_num; i++) {
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200631 s = strsep(&env, ";");
Patrick Delaunay9ada6832019-10-14 09:28:01 +0200632 ret = dfu_alt_add(dfu, interface, devstr, s);
Peng Fan5d8fae72016-05-03 10:24:52 +0800633 if (ret) {
Sam Protsenkofeaa7852018-07-13 16:35:45 +0300634 /* We will free "dfu" in dfu_free_entities() */
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200635 return -1;
Peng Fan5d8fae72016-05-03 10:24:52 +0800636 }
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200637 }
638
639 return 0;
640}
641
642const char *dfu_get_dev_type(enum dfu_device_type t)
643{
Patrick Delaunay909b6902019-10-14 09:27:58 +0200644 const char *const dev_t[] = {NULL, "eMMC", "OneNAND", "NAND", "RAM",
Patrick Delaunayec44cac2019-10-14 09:28:06 +0200645 "SF", "MTD", "VIRT"};
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200646 return dev_t[t];
647}
648
649const char *dfu_get_layout(enum dfu_layout l)
650{
Patrick Delaunay909b6902019-10-14 09:27:58 +0200651 const char *const dfu_layout[] = {NULL, "RAW_ADDR", "FAT", "EXT2",
Marek Szyprowskic533f942020-12-22 11:32:22 +0100652 "EXT3", "EXT4", "RAM_ADDR", "SKIP",
653 "SCRIPT" };
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200654 return dfu_layout[l];
655}
656
657void dfu_show_entities(void)
658{
659 struct dfu_entity *dfu;
660
661 puts("DFU alt settings list:\n");
662
663 list_for_each_entry(dfu, &dfu_list, list) {
664 printf("dev: %s alt: %d name: %s layout: %s\n",
665 dfu_get_dev_type(dfu->dev_type), dfu->alt,
666 dfu->name, dfu_get_layout(dfu->layout));
667 }
668}
669
670int dfu_get_alt_number(void)
671{
672 return dfu_alt_num;
673}
674
675struct dfu_entity *dfu_get_entity(int alt)
676{
677 struct dfu_entity *dfu;
678
679 list_for_each_entry(dfu, &dfu_list, list) {
680 if (dfu->alt == alt)
681 return dfu;
682 }
683
684 return NULL;
685}
Lukasz Majewskifed936e2013-10-08 14:30:38 +0200686
687int dfu_get_alt(char *name)
688{
689 struct dfu_entity *dfu;
Lukasz Majewski5610b052014-11-05 10:54:16 +0100690 char *str;
Lukasz Majewskifed936e2013-10-08 14:30:38 +0200691
692 list_for_each_entry(dfu, &dfu_list, list) {
Lukasz Majewski5610b052014-11-05 10:54:16 +0100693 if (dfu->name[0] != '/') {
694 if (!strncmp(dfu->name, name, strlen(dfu->name)))
695 return dfu->alt;
696 } else {
697 /*
698 * One must also consider absolute path
699 * (/boot/bin/uImage) available at dfu->name when
700 * compared "plain" file name (uImage)
701 *
702 * It is the case for e.g. thor gadget where lthor SW
703 * sends only the file name, so only the very last part
704 * of path must be checked for equality
705 */
706
707 str = strstr(dfu->name, name);
708 if (!str)
709 continue;
710
711 /*
712 * Check if matching substring is the last element of
713 * dfu->name (uImage)
714 */
715 if (strlen(dfu->name) ==
716 ((str - dfu->name) + strlen(name)))
717 return dfu->alt;
718 }
Lukasz Majewskifed936e2013-10-08 14:30:38 +0200719 }
720
721 return -ENODEV;
722}
Lukasz Majewski2092e462015-08-24 00:21:46 +0200723
724int dfu_write_from_mem_addr(struct dfu_entity *dfu, void *buf, int size)
725{
726 unsigned long dfu_buf_size, write, left = size;
727 int i, ret = 0;
728 void *dp = buf;
729
730 /*
731 * Here we must call dfu_get_buf(dfu) first to be sure that dfu_buf_size
732 * has been properly initialized - e.g. if "dfu_bufsiz" has been taken
733 * into account.
734 */
735 dfu_get_buf(dfu);
736 dfu_buf_size = dfu_get_buf_size();
737 debug("%s: dfu buf size: %lu\n", __func__, dfu_buf_size);
738
739 for (i = 0; left > 0; i++) {
740 write = min(dfu_buf_size, left);
741
742 debug("%s: dp: 0x%p left: %lu write: %lu\n", __func__,
743 dp, left, write);
744 ret = dfu_write(dfu, dp, write, i);
745 if (ret) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900746 pr_err("DFU write failed\n");
Lukasz Majewski2092e462015-08-24 00:21:46 +0200747 return ret;
748 }
749
750 dp += write;
751 left -= write;
752 }
753
754 ret = dfu_flush(dfu, NULL, 0, i);
755 if (ret)
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900756 pr_err("DFU flush failed!");
Heinrich Schuchardtf8d7e6e2021-03-05 18:36:37 +0100757 puts("\n");
Lukasz Majewski2092e462015-08-24 00:21:46 +0200758
759 return ret;
760}