blob: 8bd5216017a5689f544266b2f85a81f88e6cc2ab [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>
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>
19
20static LIST_HEAD(dfu_list);
21static int dfu_alt_num;
Przemyslaw Marczak4af3e082014-02-28 18:53:37 +010022static int alt_num_cnt;
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +020023static struct hash_algo *dfu_hash_algo;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +020024
Lukasz Majewskid9eae702014-08-25 11:07:28 +020025/*
26 * The purpose of the dfu_usb_get_reset() function is to
27 * provide information if after USB_DETACH request
28 * being sent the dfu-util performed reset of USB
29 * bus.
30 *
31 * Described behaviour is the only way to distinct if
32 * user has typed -e (detach) or -R (reset) when invoking
33 * dfu-util command.
34 *
35 */
36__weak bool dfu_usb_get_reset(void)
Lukasz Majewski2968aa22013-07-18 13:19:14 +020037{
B, Ravi4cca5cf2017-05-04 15:45:29 +053038#ifdef CONFIG_SPL_DFU_NO_RESET
39 return false;
40#else
Lukasz Majewskid9eae702014-08-25 11:07:28 +020041 return true;
B, Ravi4cca5cf2017-05-04 15:45:29 +053042#endif
Lukasz Majewski2968aa22013-07-18 13:19:14 +020043}
44
Lukasz Majewskid311c6e2012-08-06 14:41:07 +020045static int dfu_find_alt_num(const char *s)
46{
47 int i = 0;
48
49 for (; *s; s++)
50 if (*s == ';')
51 i++;
52
53 return ++i;
54}
55
Patrick Delaunay06164032019-10-14 09:28:02 +020056/*
57 * treat dfu_alt_info with several interface information
58 * to allow DFU on several device with one command,
59 * the string format is
60 * interface devstring'='alternate list (';' separated)
61 * and each interface separated by '&'
62 */
63int dfu_config_interfaces(char *env)
64{
65 struct dfu_entity *dfu;
66 char *s, *i, *d, *a, *part;
67 int ret = -EINVAL;
68 int n = 1;
69
70 s = env;
71 for (; *s; s++) {
72 if (*s == ';')
73 n++;
74 if (*s == '&')
75 n++;
76 }
77 ret = dfu_alt_init(n, &dfu);
78 if (ret)
79 return ret;
80
81 s = env;
82 while (s) {
83 ret = -EINVAL;
84 i = strsep(&s, " ");
85 if (!i)
86 break;
87 d = strsep(&s, "=");
88 if (!d)
89 break;
90 a = strsep(&s, "&");
91 if (!a)
92 a = s;
93 do {
94 part = strsep(&a, ";");
95 ret = dfu_alt_add(dfu, i, d, part);
96 if (ret)
97 return ret;
98 } while (a);
99 }
100
101 return ret;
102}
103
Stephen Warren4afe50f2014-06-11 16:03:33 -0600104int dfu_init_env_entities(char *interface, char *devstr)
Lukasz Majewski35e591e2013-09-11 14:53:35 +0200105{
106 const char *str_env;
107 char *env_bkp;
Sam Protsenko6500cde2018-07-13 16:35:46 +0300108 int ret = 0;
Lukasz Majewski35e591e2013-09-11 14:53:35 +0200109
Przemyslaw Marczakd0dd6092015-02-17 12:24:11 +0100110#ifdef CONFIG_SET_DFU_ALT_INFO
111 set_dfu_alt_info(interface, devstr);
112#endif
Simon Glass64b723f2017-08-03 12:22:12 -0600113 str_env = env_get("dfu_alt_info");
Lukasz Majewski35e591e2013-09-11 14:53:35 +0200114 if (!str_env) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900115 pr_err("\"dfu_alt_info\" env variable not defined!\n");
Lukasz Majewski35e591e2013-09-11 14:53:35 +0200116 return -EINVAL;
117 }
118
119 env_bkp = strdup(str_env);
Patrick Delaunay06164032019-10-14 09:28:02 +0200120 if (!interface && !devstr)
121 ret = dfu_config_interfaces(env_bkp);
122 else
123 ret = dfu_config_entities(env_bkp, interface, devstr);
124
Lukasz Majewski35e591e2013-09-11 14:53:35 +0200125 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900126 pr_err("DFU entities configuration failed!\n");
Sam Protsenkoab79fb02018-07-13 16:35:47 +0300127 pr_err("(partition table does not match dfu_alt_info?)\n");
Sam Protsenko6500cde2018-07-13 16:35:46 +0300128 goto done;
Lukasz Majewski35e591e2013-09-11 14:53:35 +0200129 }
130
Sam Protsenko6500cde2018-07-13 16:35:46 +0300131done:
Lukasz Majewski35e591e2013-09-11 14:53:35 +0200132 free(env_bkp);
Sam Protsenko6500cde2018-07-13 16:35:46 +0300133 return ret;
Lukasz Majewski35e591e2013-09-11 14:53:35 +0200134}
135
Heiko Schochera2f831e2013-06-12 06:05:51 +0200136static unsigned char *dfu_buf;
Lukasz Majewski0af4df82015-07-08 23:43:18 +0200137static unsigned long dfu_buf_size;
Patrick Delaunay06164032019-10-14 09:28:02 +0200138static enum dfu_device_type dfu_buf_device_type;
Heiko Schochera2f831e2013-06-12 06:05:51 +0200139
Lukasz Majewski34264232013-10-08 14:30:39 +0200140unsigned char *dfu_free_buf(void)
Heiko Schochera2f831e2013-06-12 06:05:51 +0200141{
142 free(dfu_buf);
143 dfu_buf = NULL;
144 return dfu_buf;
145}
146
Lukasz Majewski031d4132013-12-09 16:20:13 +0100147unsigned long dfu_get_buf_size(void)
148{
149 return dfu_buf_size;
150}
151
Stephen Warrenf9500982014-06-11 16:03:34 -0600152unsigned char *dfu_get_buf(struct dfu_entity *dfu)
Heiko Schochera2f831e2013-06-12 06:05:51 +0200153{
154 char *s;
155
Patrick Delaunay06164032019-10-14 09:28:02 +0200156 /* manage several entity with several contraint */
157 if (dfu_buf && dfu->dev_type != dfu_buf_device_type)
158 dfu_free_buf();
159
Heiko Schochera2f831e2013-06-12 06:05:51 +0200160 if (dfu_buf != NULL)
161 return dfu_buf;
162
Simon Glass64b723f2017-08-03 12:22:12 -0600163 s = env_get("dfu_bufsiz");
Przemyslaw Marczak24883502014-12-15 10:34:11 +0100164 if (s)
165 dfu_buf_size = (unsigned long)simple_strtol(s, NULL, 0);
166
167 if (!s || !dfu_buf_size)
168 dfu_buf_size = CONFIG_SYS_DFU_DATA_BUF_SIZE;
169
Stephen Warrenf9500982014-06-11 16:03:34 -0600170 if (dfu->max_buf_size && dfu_buf_size > dfu->max_buf_size)
171 dfu_buf_size = dfu->max_buf_size;
Heiko Schochera2f831e2013-06-12 06:05:51 +0200172
173 dfu_buf = memalign(CONFIG_SYS_CACHELINE_SIZE, dfu_buf_size);
174 if (dfu_buf == NULL)
175 printf("%s: Could not memalign 0x%lx bytes\n",
176 __func__, dfu_buf_size);
177
Patrick Delaunay06164032019-10-14 09:28:02 +0200178 dfu_buf_device_type = dfu->dev_type;
Heiko Schochera2f831e2013-06-12 06:05:51 +0200179 return dfu_buf;
180}
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200181
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200182static char *dfu_get_hash_algo(void)
183{
184 char *s;
185
Simon Glass64b723f2017-08-03 12:22:12 -0600186 s = env_get("dfu_hash_algo");
Lukasz Majewski3e0d0282014-06-10 12:25:59 +0200187 if (!s)
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200188 return NULL;
189
Lukasz Majewski3e0d0282014-06-10 12:25:59 +0200190 if (!strcmp(s, "crc32")) {
191 debug("%s: DFU hash method: %s\n", __func__, s);
192 return s;
193 }
194
Masahiro Yamada81e10422017-09-16 14:10:41 +0900195 pr_err("DFU hash method: %s not supported!\n", s);
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200196 return NULL;
197}
198
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000199static int dfu_write_buffer_drain(struct dfu_entity *dfu)
200{
201 long w_size;
202 int ret;
203
204 /* flush size? */
205 w_size = dfu->i_buf - dfu->i_buf_start;
206 if (w_size == 0)
207 return 0;
208
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200209 if (dfu_hash_algo)
210 dfu_hash_algo->hash_update(dfu_hash_algo, &dfu->crc,
211 dfu->i_buf_start, w_size, 0);
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000212
213 ret = dfu->write_medium(dfu, dfu->offset, dfu->i_buf_start, &w_size);
214 if (ret)
215 debug("%s: Write error!\n", __func__);
216
217 /* point back */
218 dfu->i_buf = dfu->i_buf_start;
219
220 /* update offset */
221 dfu->offset += w_size;
222
223 puts("#");
224
225 return ret;
226}
227
Patrick Delaunay35be83e2017-07-19 16:39:24 +0200228void dfu_transaction_cleanup(struct dfu_entity *dfu)
Stephen Warren7f450232014-06-11 12:48:08 -0600229{
230 /* clear everything */
Stephen Warren7f450232014-06-11 12:48:08 -0600231 dfu->crc = 0;
232 dfu->offset = 0;
233 dfu->i_blk_seq_num = 0;
Patrick Delaunaydec4ecb2017-07-19 16:39:25 +0200234 dfu->i_buf_start = dfu_get_buf(dfu);
235 dfu->i_buf_end = dfu->i_buf_start;
Stephen Warren7f450232014-06-11 12:48:08 -0600236 dfu->i_buf = dfu->i_buf_start;
Patrick Delaunay35be83e2017-07-19 16:39:24 +0200237 dfu->r_left = 0;
238 dfu->b_left = 0;
239 dfu->bad_skip = 0;
240
Stephen Warren7f450232014-06-11 12:48:08 -0600241 dfu->inited = 0;
242}
243
Patrick Delaunaydec4ecb2017-07-19 16:39:25 +0200244int dfu_transaction_initiate(struct dfu_entity *dfu, bool read)
245{
246 int ret = 0;
247
248 if (dfu->inited)
249 return 0;
250
251 dfu_transaction_cleanup(dfu);
252
253 if (dfu->i_buf_start == NULL)
254 return -ENOMEM;
255
256 dfu->i_buf_end = dfu->i_buf_start + dfu_get_buf_size();
257
258 if (read) {
259 ret = dfu->get_medium_size(dfu, &dfu->r_left);
260 if (ret < 0)
261 return ret;
262 debug("%s: %s %lld [B]\n", __func__, dfu->name, dfu->r_left);
263 }
264
265 dfu->inited = 1;
266
267 return 0;
268}
269
Heiko Schocher941b3842014-03-18 08:09:55 +0100270int dfu_flush(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
271{
272 int ret = 0;
273
Przemyslaw Marczaka0f6a382014-05-12 12:05:33 +0200274 ret = dfu_write_buffer_drain(dfu);
275 if (ret)
276 return ret;
277
Heiko Schocher941b3842014-03-18 08:09:55 +0100278 if (dfu->flush_medium)
279 ret = dfu->flush_medium(dfu);
280
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200281 if (dfu_hash_algo)
282 printf("\nDFU complete %s: 0x%08x\n", dfu_hash_algo->name,
283 dfu->crc);
Heiko Schocher941b3842014-03-18 08:09:55 +0100284
Patrick Delaunay35be83e2017-07-19 16:39:24 +0200285 dfu_transaction_cleanup(dfu);
Heiko Schocher941b3842014-03-18 08:09:55 +0100286
287 return ret;
288}
289
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200290int dfu_write(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
291{
Stephen Warren7f450232014-06-11 12:48:08 -0600292 int ret;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200293
Stephen Warrenc00315b2015-07-22 14:54:04 -0600294 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 +0000295 __func__, dfu->name, buf, size, blk_seq_num, dfu->offset,
Stephen Warrenc00315b2015-07-22 14:54:04 -0600296 (unsigned long)(dfu->i_buf - dfu->i_buf_start));
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200297
Patrick Delaunaydec4ecb2017-07-19 16:39:25 +0200298 ret = dfu_transaction_initiate(dfu, false);
299 if (ret < 0)
300 return ret;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200301
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000302 if (dfu->i_blk_seq_num != blk_seq_num) {
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200303 printf("%s: Wrong sequence number! [%d] [%d]\n",
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000304 __func__, dfu->i_blk_seq_num, blk_seq_num);
Patrick Delaunay35be83e2017-07-19 16:39:24 +0200305 dfu_transaction_cleanup(dfu);
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000306 return -1;
307 }
308
309 /* DFU 1.1 standard says:
310 * The wBlockNum field is a block sequence number. It increments each
311 * time a block is transferred, wrapping to zero from 65,535. It is used
312 * to provide useful context to the DFU loader in the device."
313 *
314 * This means that it's a 16 bit counter that roll-overs at
315 * 0xffff -> 0x0000. By having a typical 4K transfer block
316 * we roll-over at exactly 256MB. Not very fun to debug.
317 *
318 * Handling rollover, and having an inited variable,
319 * makes things work.
320 */
321
322 /* handle rollover */
323 dfu->i_blk_seq_num = (dfu->i_blk_seq_num + 1) & 0xffff;
324
325 /* flush buffer if overflow */
326 if ((dfu->i_buf + size) > dfu->i_buf_end) {
Stephen Warren7f450232014-06-11 12:48:08 -0600327 ret = dfu_write_buffer_drain(dfu);
328 if (ret) {
Patrick Delaunay35be83e2017-07-19 16:39:24 +0200329 dfu_transaction_cleanup(dfu);
Stephen Warren7f450232014-06-11 12:48:08 -0600330 return ret;
331 }
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000332 }
333
334 /* we should be in buffer now (if not then size too large) */
335 if ((dfu->i_buf + size) > dfu->i_buf_end) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900336 pr_err("Buffer overflow! (0x%p + 0x%x > 0x%p)\n", dfu->i_buf,
Lukasz Majewski385c3212013-09-10 15:29:22 +0200337 size, dfu->i_buf_end);
Patrick Delaunay35be83e2017-07-19 16:39:24 +0200338 dfu_transaction_cleanup(dfu);
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200339 return -1;
340 }
341
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000342 memcpy(dfu->i_buf, buf, size);
343 dfu->i_buf += size;
344
345 /* if end or if buffer full flush */
346 if (size == 0 || (dfu->i_buf + size) > dfu->i_buf_end) {
Stephen Warren7f450232014-06-11 12:48:08 -0600347 ret = dfu_write_buffer_drain(dfu);
348 if (ret) {
Patrick Delaunay35be83e2017-07-19 16:39:24 +0200349 dfu_transaction_cleanup(dfu);
Stephen Warren7f450232014-06-11 12:48:08 -0600350 return ret;
351 }
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000352 }
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200353
Stephen Warren7f450232014-06-11 12:48:08 -0600354 return 0;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200355}
356
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000357static int dfu_read_buffer_fill(struct dfu_entity *dfu, void *buf, int size)
358{
359 long chunk;
360 int ret, readn;
361
362 readn = 0;
363 while (size > 0) {
364 /* get chunk that can be read */
Masahiro Yamadadb204642014-11-07 03:03:31 +0900365 chunk = min((long)size, dfu->b_left);
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000366 /* consume */
367 if (chunk > 0) {
368 memcpy(buf, dfu->i_buf, chunk);
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200369 if (dfu_hash_algo)
370 dfu_hash_algo->hash_update(dfu_hash_algo,
371 &dfu->crc, buf,
372 chunk, 0);
373
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000374 dfu->i_buf += chunk;
375 dfu->b_left -= chunk;
376 size -= chunk;
377 buf += chunk;
378 readn += chunk;
379 }
380
381 /* all done */
382 if (size > 0) {
383 /* no more to read */
384 if (dfu->r_left == 0)
385 break;
386
387 dfu->i_buf = dfu->i_buf_start;
388 dfu->b_left = dfu->i_buf_end - dfu->i_buf_start;
389
390 /* got to read, but buffer is empty */
391 if (dfu->b_left > dfu->r_left)
392 dfu->b_left = dfu->r_left;
393 ret = dfu->read_medium(dfu, dfu->offset, dfu->i_buf,
394 &dfu->b_left);
395 if (ret != 0) {
396 debug("%s: Read error!\n", __func__);
397 return ret;
398 }
399 dfu->offset += dfu->b_left;
400 dfu->r_left -= dfu->b_left;
401
402 puts("#");
403 }
404 }
405
406 return readn;
407}
408
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200409int dfu_read(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
410{
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200411 int ret = 0;
412
413 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 +0000414 __func__, dfu->name, buf, size, blk_seq_num, dfu->i_buf);
415
Patrick Delaunaydec4ecb2017-07-19 16:39:25 +0200416 ret = dfu_transaction_initiate(dfu, true);
417 if (ret < 0)
418 return ret;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200419
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000420 if (dfu->i_blk_seq_num != blk_seq_num) {
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200421 printf("%s: Wrong sequence number! [%d] [%d]\n",
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000422 __func__, dfu->i_blk_seq_num, blk_seq_num);
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200423 return -1;
424 }
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000425 /* handle rollover */
426 dfu->i_blk_seq_num = (dfu->i_blk_seq_num + 1) & 0xffff;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200427
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000428 ret = dfu_read_buffer_fill(dfu, buf, size);
429 if (ret < 0) {
430 printf("%s: Failed to fill buffer\n", __func__);
431 return -1;
432 }
433
434 if (ret < size) {
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200435 if (dfu_hash_algo)
436 debug("%s: %s %s: 0x%x\n", __func__, dfu->name,
437 dfu_hash_algo->name, dfu->crc);
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000438 puts("\nUPLOAD ... done\nCtrl+C to exit ...\n");
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200439
Patrick Delaunay35be83e2017-07-19 16:39:24 +0200440 dfu_transaction_cleanup(dfu);
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200441 }
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000442
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200443 return ret;
444}
445
446static int dfu_fill_entity(struct dfu_entity *dfu, char *s, int alt,
Stephen Warren4afe50f2014-06-11 16:03:33 -0600447 char *interface, char *devstr)
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200448{
449 char *st;
450
Stephen Warren4afe50f2014-06-11 16:03:33 -0600451 debug("%s: %s interface: %s dev: %s\n", __func__, s, interface, devstr);
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200452 st = strsep(&s, " ");
453 strcpy(dfu->name, st);
454
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200455 dfu->alt = alt;
Stephen Warrenf9500982014-06-11 16:03:34 -0600456 dfu->max_buf_size = 0;
Stephen Warrena5f77f62014-06-11 16:03:35 -0600457 dfu->free_entity = NULL;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200458
459 /* Specific for mmc device */
460 if (strcmp(interface, "mmc") == 0) {
Stephen Warren4afe50f2014-06-11 16:03:33 -0600461 if (dfu_fill_entity_mmc(dfu, devstr, s))
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200462 return -1;
Pantelis Antonioucf14d0d2013-03-14 05:32:52 +0000463 } else if (strcmp(interface, "nand") == 0) {
Stephen Warren4afe50f2014-06-11 16:03:33 -0600464 if (dfu_fill_entity_nand(dfu, devstr, s))
Pantelis Antonioucf14d0d2013-03-14 05:32:52 +0000465 return -1;
Afzal Mohammede3c687a2013-09-18 01:15:24 +0530466 } else if (strcmp(interface, "ram") == 0) {
Stephen Warren4afe50f2014-06-11 16:03:33 -0600467 if (dfu_fill_entity_ram(dfu, devstr, s))
Afzal Mohammede3c687a2013-09-18 01:15:24 +0530468 return -1;
Stephen Warren0280af72014-06-11 16:03:36 -0600469 } else if (strcmp(interface, "sf") == 0) {
470 if (dfu_fill_entity_sf(dfu, devstr, s))
471 return -1;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200472 } else {
473 printf("%s: Device %s not (yet) supported!\n",
474 __func__, interface);
475 return -1;
476 }
Stephen Warren0898da52015-09-04 22:03:46 -0600477 dfu_get_buf(dfu);
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200478
479 return 0;
480}
481
482void dfu_free_entities(void)
483{
484 struct dfu_entity *dfu, *p, *t = NULL;
485
Stephen Warren0898da52015-09-04 22:03:46 -0600486 dfu_free_buf();
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200487 list_for_each_entry_safe_reverse(dfu, p, &dfu_list, list) {
488 list_del(&dfu->list);
Stephen Warrena5f77f62014-06-11 16:03:35 -0600489 if (dfu->free_entity)
490 dfu->free_entity(dfu);
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200491 t = dfu;
492 }
493 if (t)
494 free(t);
495 INIT_LIST_HEAD(&dfu_list);
Przemyslaw Marczak4af3e082014-02-28 18:53:37 +0100496
497 alt_num_cnt = 0;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200498}
499
Patrick Delaunay0f8fad82019-10-14 09:28:01 +0200500int dfu_alt_init(int num, struct dfu_entity **dfu)
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200501{
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200502 char *s;
Patrick Delaunay0f8fad82019-10-14 09:28:01 +0200503 int ret;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200504
Patrick Delaunay0f8fad82019-10-14 09:28:01 +0200505 dfu_alt_num = num;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200506 debug("%s: dfu_alt_num=%d\n", __func__, dfu_alt_num);
507
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200508 dfu_hash_algo = NULL;
509 s = dfu_get_hash_algo();
510 if (s) {
511 ret = hash_lookup_algo(s, &dfu_hash_algo);
512 if (ret)
Masahiro Yamada81e10422017-09-16 14:10:41 +0900513 pr_err("Hash algorithm %s not supported\n", s);
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200514 }
515
Patrick Delaunay0f8fad82019-10-14 09:28:01 +0200516 *dfu = calloc(sizeof(struct dfu_entity), dfu_alt_num);
517 if (!*dfu)
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200518 return -1;
Patrick Delaunay0f8fad82019-10-14 09:28:01 +0200519
520 return 0;
521}
522
523int dfu_alt_add(struct dfu_entity *dfu, char *interface, char *devstr, char *s)
524{
525 struct dfu_entity *p_dfu;
526 int ret;
527
528 if (alt_num_cnt >= dfu_alt_num)
529 return -1;
530
531 p_dfu = &dfu[alt_num_cnt];
532 ret = dfu_fill_entity(p_dfu, s, alt_num_cnt, interface, devstr);
533 if (ret)
534 return -1;
535
536 list_add_tail(&p_dfu->list, &dfu_list);
537 alt_num_cnt++;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200538
Patrick Delaunay0f8fad82019-10-14 09:28:01 +0200539 return 0;
540}
541
542int dfu_config_entities(char *env, char *interface, char *devstr)
543{
544 struct dfu_entity *dfu;
545 int i, ret;
546 char *s;
547
548 ret = dfu_alt_init(dfu_find_alt_num(env), &dfu);
549 if (ret)
550 return -1;
551
552 for (i = 0; i < dfu_alt_num; i++) {
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200553 s = strsep(&env, ";");
Patrick Delaunay0f8fad82019-10-14 09:28:01 +0200554 ret = dfu_alt_add(dfu, interface, devstr, s);
Peng Fan529d4fd2016-05-03 10:24:52 +0800555 if (ret) {
Sam Protsenko6d29e5c2018-07-13 16:35:45 +0300556 /* We will free "dfu" in dfu_free_entities() */
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200557 return -1;
Peng Fan529d4fd2016-05-03 10:24:52 +0800558 }
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200559 }
560
561 return 0;
562}
563
564const char *dfu_get_dev_type(enum dfu_device_type t)
565{
Patrick Delaunayae224c32019-10-14 09:27:58 +0200566 const char *const dev_t[] = {NULL, "eMMC", "OneNAND", "NAND", "RAM",
567 "SF"};
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200568 return dev_t[t];
569}
570
571const char *dfu_get_layout(enum dfu_layout l)
572{
Patrick Delaunayae224c32019-10-14 09:27:58 +0200573 const char *const dfu_layout[] = {NULL, "RAW_ADDR", "FAT", "EXT2",
574 "EXT3", "EXT4", "RAM_ADDR" };
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200575 return dfu_layout[l];
576}
577
578void dfu_show_entities(void)
579{
580 struct dfu_entity *dfu;
581
582 puts("DFU alt settings list:\n");
583
584 list_for_each_entry(dfu, &dfu_list, list) {
585 printf("dev: %s alt: %d name: %s layout: %s\n",
586 dfu_get_dev_type(dfu->dev_type), dfu->alt,
587 dfu->name, dfu_get_layout(dfu->layout));
588 }
589}
590
591int dfu_get_alt_number(void)
592{
593 return dfu_alt_num;
594}
595
596struct dfu_entity *dfu_get_entity(int alt)
597{
598 struct dfu_entity *dfu;
599
600 list_for_each_entry(dfu, &dfu_list, list) {
601 if (dfu->alt == alt)
602 return dfu;
603 }
604
605 return NULL;
606}
Lukasz Majewski1737f0f2013-10-08 14:30:38 +0200607
608int dfu_get_alt(char *name)
609{
610 struct dfu_entity *dfu;
Lukasz Majewski3a39d012014-11-05 10:54:16 +0100611 char *str;
Lukasz Majewski1737f0f2013-10-08 14:30:38 +0200612
613 list_for_each_entry(dfu, &dfu_list, list) {
Lukasz Majewski3a39d012014-11-05 10:54:16 +0100614 if (dfu->name[0] != '/') {
615 if (!strncmp(dfu->name, name, strlen(dfu->name)))
616 return dfu->alt;
617 } else {
618 /*
619 * One must also consider absolute path
620 * (/boot/bin/uImage) available at dfu->name when
621 * compared "plain" file name (uImage)
622 *
623 * It is the case for e.g. thor gadget where lthor SW
624 * sends only the file name, so only the very last part
625 * of path must be checked for equality
626 */
627
628 str = strstr(dfu->name, name);
629 if (!str)
630 continue;
631
632 /*
633 * Check if matching substring is the last element of
634 * dfu->name (uImage)
635 */
636 if (strlen(dfu->name) ==
637 ((str - dfu->name) + strlen(name)))
638 return dfu->alt;
639 }
Lukasz Majewski1737f0f2013-10-08 14:30:38 +0200640 }
641
642 return -ENODEV;
643}
Lukasz Majewskicc2d0262015-08-24 00:21:46 +0200644
645int dfu_write_from_mem_addr(struct dfu_entity *dfu, void *buf, int size)
646{
647 unsigned long dfu_buf_size, write, left = size;
648 int i, ret = 0;
649 void *dp = buf;
650
651 /*
652 * Here we must call dfu_get_buf(dfu) first to be sure that dfu_buf_size
653 * has been properly initialized - e.g. if "dfu_bufsiz" has been taken
654 * into account.
655 */
656 dfu_get_buf(dfu);
657 dfu_buf_size = dfu_get_buf_size();
658 debug("%s: dfu buf size: %lu\n", __func__, dfu_buf_size);
659
660 for (i = 0; left > 0; i++) {
661 write = min(dfu_buf_size, left);
662
663 debug("%s: dp: 0x%p left: %lu write: %lu\n", __func__,
664 dp, left, write);
665 ret = dfu_write(dfu, dp, write, i);
666 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900667 pr_err("DFU write failed\n");
Lukasz Majewskicc2d0262015-08-24 00:21:46 +0200668 return ret;
669 }
670
671 dp += write;
672 left -= write;
673 }
674
675 ret = dfu_flush(dfu, NULL, 0, i);
676 if (ret)
Masahiro Yamada81e10422017-09-16 14:10:41 +0900677 pr_err("DFU flush failed!");
Lukasz Majewskicc2d0262015-08-24 00:21:46 +0200678
679 return ret;
680}