blob: e9db7f8d6616cab20e6e58b85279b590e05d5335 [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
Stephen Warren4afe50f2014-06-11 16:03:33 -060056int dfu_init_env_entities(char *interface, char *devstr)
Lukasz Majewski35e591e2013-09-11 14:53:35 +020057{
58 const char *str_env;
59 char *env_bkp;
Sam Protsenko6500cde2018-07-13 16:35:46 +030060 int ret = 0;
Lukasz Majewski35e591e2013-09-11 14:53:35 +020061
Przemyslaw Marczakd0dd6092015-02-17 12:24:11 +010062#ifdef CONFIG_SET_DFU_ALT_INFO
63 set_dfu_alt_info(interface, devstr);
64#endif
Simon Glass64b723f2017-08-03 12:22:12 -060065 str_env = env_get("dfu_alt_info");
Lukasz Majewski35e591e2013-09-11 14:53:35 +020066 if (!str_env) {
Masahiro Yamada81e10422017-09-16 14:10:41 +090067 pr_err("\"dfu_alt_info\" env variable not defined!\n");
Lukasz Majewski35e591e2013-09-11 14:53:35 +020068 return -EINVAL;
69 }
70
71 env_bkp = strdup(str_env);
Stephen Warren4afe50f2014-06-11 16:03:33 -060072 ret = dfu_config_entities(env_bkp, interface, devstr);
Lukasz Majewski35e591e2013-09-11 14:53:35 +020073 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +090074 pr_err("DFU entities configuration failed!\n");
Sam Protsenkoab79fb02018-07-13 16:35:47 +030075 pr_err("(partition table does not match dfu_alt_info?)\n");
Sam Protsenko6500cde2018-07-13 16:35:46 +030076 goto done;
Lukasz Majewski35e591e2013-09-11 14:53:35 +020077 }
78
Sam Protsenko6500cde2018-07-13 16:35:46 +030079done:
Lukasz Majewski35e591e2013-09-11 14:53:35 +020080 free(env_bkp);
Sam Protsenko6500cde2018-07-13 16:35:46 +030081 return ret;
Lukasz Majewski35e591e2013-09-11 14:53:35 +020082}
83
Heiko Schochera2f831e2013-06-12 06:05:51 +020084static unsigned char *dfu_buf;
Lukasz Majewski0af4df82015-07-08 23:43:18 +020085static unsigned long dfu_buf_size;
Heiko Schochera2f831e2013-06-12 06:05:51 +020086
Lukasz Majewski34264232013-10-08 14:30:39 +020087unsigned char *dfu_free_buf(void)
Heiko Schochera2f831e2013-06-12 06:05:51 +020088{
89 free(dfu_buf);
90 dfu_buf = NULL;
91 return dfu_buf;
92}
93
Lukasz Majewski031d4132013-12-09 16:20:13 +010094unsigned long dfu_get_buf_size(void)
95{
96 return dfu_buf_size;
97}
98
Stephen Warrenf9500982014-06-11 16:03:34 -060099unsigned char *dfu_get_buf(struct dfu_entity *dfu)
Heiko Schochera2f831e2013-06-12 06:05:51 +0200100{
101 char *s;
102
103 if (dfu_buf != NULL)
104 return dfu_buf;
105
Simon Glass64b723f2017-08-03 12:22:12 -0600106 s = env_get("dfu_bufsiz");
Przemyslaw Marczak24883502014-12-15 10:34:11 +0100107 if (s)
108 dfu_buf_size = (unsigned long)simple_strtol(s, NULL, 0);
109
110 if (!s || !dfu_buf_size)
111 dfu_buf_size = CONFIG_SYS_DFU_DATA_BUF_SIZE;
112
Stephen Warrenf9500982014-06-11 16:03:34 -0600113 if (dfu->max_buf_size && dfu_buf_size > dfu->max_buf_size)
114 dfu_buf_size = dfu->max_buf_size;
Heiko Schochera2f831e2013-06-12 06:05:51 +0200115
116 dfu_buf = memalign(CONFIG_SYS_CACHELINE_SIZE, dfu_buf_size);
117 if (dfu_buf == NULL)
118 printf("%s: Could not memalign 0x%lx bytes\n",
119 __func__, dfu_buf_size);
120
121 return dfu_buf;
122}
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200123
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200124static char *dfu_get_hash_algo(void)
125{
126 char *s;
127
Simon Glass64b723f2017-08-03 12:22:12 -0600128 s = env_get("dfu_hash_algo");
Lukasz Majewski3e0d0282014-06-10 12:25:59 +0200129 if (!s)
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200130 return NULL;
131
Lukasz Majewski3e0d0282014-06-10 12:25:59 +0200132 if (!strcmp(s, "crc32")) {
133 debug("%s: DFU hash method: %s\n", __func__, s);
134 return s;
135 }
136
Masahiro Yamada81e10422017-09-16 14:10:41 +0900137 pr_err("DFU hash method: %s not supported!\n", s);
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200138 return NULL;
139}
140
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000141static int dfu_write_buffer_drain(struct dfu_entity *dfu)
142{
143 long w_size;
144 int ret;
145
146 /* flush size? */
147 w_size = dfu->i_buf - dfu->i_buf_start;
148 if (w_size == 0)
149 return 0;
150
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200151 if (dfu_hash_algo)
152 dfu_hash_algo->hash_update(dfu_hash_algo, &dfu->crc,
153 dfu->i_buf_start, w_size, 0);
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000154
155 ret = dfu->write_medium(dfu, dfu->offset, dfu->i_buf_start, &w_size);
156 if (ret)
157 debug("%s: Write error!\n", __func__);
158
159 /* point back */
160 dfu->i_buf = dfu->i_buf_start;
161
162 /* update offset */
163 dfu->offset += w_size;
164
165 puts("#");
166
167 return ret;
168}
169
Patrick Delaunay35be83e2017-07-19 16:39:24 +0200170void dfu_transaction_cleanup(struct dfu_entity *dfu)
Stephen Warren7f450232014-06-11 12:48:08 -0600171{
172 /* clear everything */
Stephen Warren7f450232014-06-11 12:48:08 -0600173 dfu->crc = 0;
174 dfu->offset = 0;
175 dfu->i_blk_seq_num = 0;
Patrick Delaunaydec4ecb2017-07-19 16:39:25 +0200176 dfu->i_buf_start = dfu_get_buf(dfu);
177 dfu->i_buf_end = dfu->i_buf_start;
Stephen Warren7f450232014-06-11 12:48:08 -0600178 dfu->i_buf = dfu->i_buf_start;
Patrick Delaunay35be83e2017-07-19 16:39:24 +0200179 dfu->r_left = 0;
180 dfu->b_left = 0;
181 dfu->bad_skip = 0;
182
Stephen Warren7f450232014-06-11 12:48:08 -0600183 dfu->inited = 0;
184}
185
Patrick Delaunaydec4ecb2017-07-19 16:39:25 +0200186int dfu_transaction_initiate(struct dfu_entity *dfu, bool read)
187{
188 int ret = 0;
189
190 if (dfu->inited)
191 return 0;
192
193 dfu_transaction_cleanup(dfu);
194
195 if (dfu->i_buf_start == NULL)
196 return -ENOMEM;
197
198 dfu->i_buf_end = dfu->i_buf_start + dfu_get_buf_size();
199
200 if (read) {
201 ret = dfu->get_medium_size(dfu, &dfu->r_left);
202 if (ret < 0)
203 return ret;
204 debug("%s: %s %lld [B]\n", __func__, dfu->name, dfu->r_left);
205 }
206
207 dfu->inited = 1;
208
209 return 0;
210}
211
Heiko Schocher941b3842014-03-18 08:09:55 +0100212int dfu_flush(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
213{
214 int ret = 0;
215
Przemyslaw Marczaka0f6a382014-05-12 12:05:33 +0200216 ret = dfu_write_buffer_drain(dfu);
217 if (ret)
218 return ret;
219
Heiko Schocher941b3842014-03-18 08:09:55 +0100220 if (dfu->flush_medium)
221 ret = dfu->flush_medium(dfu);
222
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200223 if (dfu_hash_algo)
224 printf("\nDFU complete %s: 0x%08x\n", dfu_hash_algo->name,
225 dfu->crc);
Heiko Schocher941b3842014-03-18 08:09:55 +0100226
Patrick Delaunay35be83e2017-07-19 16:39:24 +0200227 dfu_transaction_cleanup(dfu);
Heiko Schocher941b3842014-03-18 08:09:55 +0100228
229 return ret;
230}
231
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200232int dfu_write(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
233{
Stephen Warren7f450232014-06-11 12:48:08 -0600234 int ret;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200235
Stephen Warrenc00315b2015-07-22 14:54:04 -0600236 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 +0000237 __func__, dfu->name, buf, size, blk_seq_num, dfu->offset,
Stephen Warrenc00315b2015-07-22 14:54:04 -0600238 (unsigned long)(dfu->i_buf - dfu->i_buf_start));
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200239
Patrick Delaunaydec4ecb2017-07-19 16:39:25 +0200240 ret = dfu_transaction_initiate(dfu, false);
241 if (ret < 0)
242 return ret;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200243
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000244 if (dfu->i_blk_seq_num != blk_seq_num) {
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200245 printf("%s: Wrong sequence number! [%d] [%d]\n",
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000246 __func__, dfu->i_blk_seq_num, blk_seq_num);
Patrick Delaunay35be83e2017-07-19 16:39:24 +0200247 dfu_transaction_cleanup(dfu);
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000248 return -1;
249 }
250
251 /* DFU 1.1 standard says:
252 * The wBlockNum field is a block sequence number. It increments each
253 * time a block is transferred, wrapping to zero from 65,535. It is used
254 * to provide useful context to the DFU loader in the device."
255 *
256 * This means that it's a 16 bit counter that roll-overs at
257 * 0xffff -> 0x0000. By having a typical 4K transfer block
258 * we roll-over at exactly 256MB. Not very fun to debug.
259 *
260 * Handling rollover, and having an inited variable,
261 * makes things work.
262 */
263
264 /* handle rollover */
265 dfu->i_blk_seq_num = (dfu->i_blk_seq_num + 1) & 0xffff;
266
267 /* flush buffer if overflow */
268 if ((dfu->i_buf + size) > dfu->i_buf_end) {
Stephen Warren7f450232014-06-11 12:48:08 -0600269 ret = dfu_write_buffer_drain(dfu);
270 if (ret) {
Patrick Delaunay35be83e2017-07-19 16:39:24 +0200271 dfu_transaction_cleanup(dfu);
Stephen Warren7f450232014-06-11 12:48:08 -0600272 return ret;
273 }
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000274 }
275
276 /* we should be in buffer now (if not then size too large) */
277 if ((dfu->i_buf + size) > dfu->i_buf_end) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900278 pr_err("Buffer overflow! (0x%p + 0x%x > 0x%p)\n", dfu->i_buf,
Lukasz Majewski385c3212013-09-10 15:29:22 +0200279 size, dfu->i_buf_end);
Patrick Delaunay35be83e2017-07-19 16:39:24 +0200280 dfu_transaction_cleanup(dfu);
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200281 return -1;
282 }
283
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000284 memcpy(dfu->i_buf, buf, size);
285 dfu->i_buf += size;
286
287 /* if end or if buffer full flush */
288 if (size == 0 || (dfu->i_buf + size) > dfu->i_buf_end) {
Stephen Warren7f450232014-06-11 12:48:08 -0600289 ret = dfu_write_buffer_drain(dfu);
290 if (ret) {
Patrick Delaunay35be83e2017-07-19 16:39:24 +0200291 dfu_transaction_cleanup(dfu);
Stephen Warren7f450232014-06-11 12:48:08 -0600292 return ret;
293 }
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000294 }
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200295
Stephen Warren7f450232014-06-11 12:48:08 -0600296 return 0;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200297}
298
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000299static int dfu_read_buffer_fill(struct dfu_entity *dfu, void *buf, int size)
300{
301 long chunk;
302 int ret, readn;
303
304 readn = 0;
305 while (size > 0) {
306 /* get chunk that can be read */
Masahiro Yamadadb204642014-11-07 03:03:31 +0900307 chunk = min((long)size, dfu->b_left);
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000308 /* consume */
309 if (chunk > 0) {
310 memcpy(buf, dfu->i_buf, chunk);
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200311 if (dfu_hash_algo)
312 dfu_hash_algo->hash_update(dfu_hash_algo,
313 &dfu->crc, buf,
314 chunk, 0);
315
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000316 dfu->i_buf += chunk;
317 dfu->b_left -= chunk;
318 size -= chunk;
319 buf += chunk;
320 readn += chunk;
321 }
322
323 /* all done */
324 if (size > 0) {
325 /* no more to read */
326 if (dfu->r_left == 0)
327 break;
328
329 dfu->i_buf = dfu->i_buf_start;
330 dfu->b_left = dfu->i_buf_end - dfu->i_buf_start;
331
332 /* got to read, but buffer is empty */
333 if (dfu->b_left > dfu->r_left)
334 dfu->b_left = dfu->r_left;
335 ret = dfu->read_medium(dfu, dfu->offset, dfu->i_buf,
336 &dfu->b_left);
337 if (ret != 0) {
338 debug("%s: Read error!\n", __func__);
339 return ret;
340 }
341 dfu->offset += dfu->b_left;
342 dfu->r_left -= dfu->b_left;
343
344 puts("#");
345 }
346 }
347
348 return readn;
349}
350
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200351int dfu_read(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
352{
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200353 int ret = 0;
354
355 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 +0000356 __func__, dfu->name, buf, size, blk_seq_num, dfu->i_buf);
357
Patrick Delaunaydec4ecb2017-07-19 16:39:25 +0200358 ret = dfu_transaction_initiate(dfu, true);
359 if (ret < 0)
360 return ret;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200361
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000362 if (dfu->i_blk_seq_num != blk_seq_num) {
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200363 printf("%s: Wrong sequence number! [%d] [%d]\n",
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000364 __func__, dfu->i_blk_seq_num, blk_seq_num);
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200365 return -1;
366 }
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000367 /* handle rollover */
368 dfu->i_blk_seq_num = (dfu->i_blk_seq_num + 1) & 0xffff;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200369
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000370 ret = dfu_read_buffer_fill(dfu, buf, size);
371 if (ret < 0) {
372 printf("%s: Failed to fill buffer\n", __func__);
373 return -1;
374 }
375
376 if (ret < size) {
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200377 if (dfu_hash_algo)
378 debug("%s: %s %s: 0x%x\n", __func__, dfu->name,
379 dfu_hash_algo->name, dfu->crc);
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000380 puts("\nUPLOAD ... done\nCtrl+C to exit ...\n");
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200381
Patrick Delaunay35be83e2017-07-19 16:39:24 +0200382 dfu_transaction_cleanup(dfu);
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200383 }
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000384
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200385 return ret;
386}
387
388static int dfu_fill_entity(struct dfu_entity *dfu, char *s, int alt,
Stephen Warren4afe50f2014-06-11 16:03:33 -0600389 char *interface, char *devstr)
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200390{
391 char *st;
392
Stephen Warren4afe50f2014-06-11 16:03:33 -0600393 debug("%s: %s interface: %s dev: %s\n", __func__, s, interface, devstr);
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200394 st = strsep(&s, " ");
395 strcpy(dfu->name, st);
396
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200397 dfu->alt = alt;
Stephen Warrenf9500982014-06-11 16:03:34 -0600398 dfu->max_buf_size = 0;
Stephen Warrena5f77f62014-06-11 16:03:35 -0600399 dfu->free_entity = NULL;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200400
401 /* Specific for mmc device */
402 if (strcmp(interface, "mmc") == 0) {
Stephen Warren4afe50f2014-06-11 16:03:33 -0600403 if (dfu_fill_entity_mmc(dfu, devstr, s))
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200404 return -1;
Pantelis Antonioucf14d0d2013-03-14 05:32:52 +0000405 } else if (strcmp(interface, "nand") == 0) {
Stephen Warren4afe50f2014-06-11 16:03:33 -0600406 if (dfu_fill_entity_nand(dfu, devstr, s))
Pantelis Antonioucf14d0d2013-03-14 05:32:52 +0000407 return -1;
Afzal Mohammede3c687a2013-09-18 01:15:24 +0530408 } else if (strcmp(interface, "ram") == 0) {
Stephen Warren4afe50f2014-06-11 16:03:33 -0600409 if (dfu_fill_entity_ram(dfu, devstr, s))
Afzal Mohammede3c687a2013-09-18 01:15:24 +0530410 return -1;
Stephen Warren0280af72014-06-11 16:03:36 -0600411 } else if (strcmp(interface, "sf") == 0) {
412 if (dfu_fill_entity_sf(dfu, devstr, s))
413 return -1;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200414 } else {
415 printf("%s: Device %s not (yet) supported!\n",
416 __func__, interface);
417 return -1;
418 }
Stephen Warren0898da52015-09-04 22:03:46 -0600419 dfu_get_buf(dfu);
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200420
421 return 0;
422}
423
424void dfu_free_entities(void)
425{
426 struct dfu_entity *dfu, *p, *t = NULL;
427
Stephen Warren0898da52015-09-04 22:03:46 -0600428 dfu_free_buf();
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200429 list_for_each_entry_safe_reverse(dfu, p, &dfu_list, list) {
430 list_del(&dfu->list);
Stephen Warrena5f77f62014-06-11 16:03:35 -0600431 if (dfu->free_entity)
432 dfu->free_entity(dfu);
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200433 t = dfu;
434 }
435 if (t)
436 free(t);
437 INIT_LIST_HEAD(&dfu_list);
Przemyslaw Marczak4af3e082014-02-28 18:53:37 +0100438
439 alt_num_cnt = 0;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200440}
441
Stephen Warren4afe50f2014-06-11 16:03:33 -0600442int dfu_config_entities(char *env, char *interface, char *devstr)
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200443{
444 struct dfu_entity *dfu;
445 int i, ret;
446 char *s;
447
448 dfu_alt_num = dfu_find_alt_num(env);
449 debug("%s: dfu_alt_num=%d\n", __func__, dfu_alt_num);
450
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200451 dfu_hash_algo = NULL;
452 s = dfu_get_hash_algo();
453 if (s) {
454 ret = hash_lookup_algo(s, &dfu_hash_algo);
455 if (ret)
Masahiro Yamada81e10422017-09-16 14:10:41 +0900456 pr_err("Hash algorithm %s not supported\n", s);
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200457 }
458
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200459 dfu = calloc(sizeof(*dfu), dfu_alt_num);
460 if (!dfu)
461 return -1;
462 for (i = 0; i < dfu_alt_num; i++) {
463
464 s = strsep(&env, ";");
Stephen Warren4afe50f2014-06-11 16:03:33 -0600465 ret = dfu_fill_entity(&dfu[i], s, alt_num_cnt, interface,
466 devstr);
Peng Fan529d4fd2016-05-03 10:24:52 +0800467 if (ret) {
Sam Protsenko6d29e5c2018-07-13 16:35:45 +0300468 /* We will free "dfu" in dfu_free_entities() */
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200469 return -1;
Peng Fan529d4fd2016-05-03 10:24:52 +0800470 }
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200471
472 list_add_tail(&dfu[i].list, &dfu_list);
Przemyslaw Marczak4af3e082014-02-28 18:53:37 +0100473 alt_num_cnt++;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200474 }
475
476 return 0;
477}
478
479const char *dfu_get_dev_type(enum dfu_device_type t)
480{
Patrick Delaunayae224c32019-10-14 09:27:58 +0200481 const char *const dev_t[] = {NULL, "eMMC", "OneNAND", "NAND", "RAM",
482 "SF"};
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200483 return dev_t[t];
484}
485
486const char *dfu_get_layout(enum dfu_layout l)
487{
Patrick Delaunayae224c32019-10-14 09:27:58 +0200488 const char *const dfu_layout[] = {NULL, "RAW_ADDR", "FAT", "EXT2",
489 "EXT3", "EXT4", "RAM_ADDR" };
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200490 return dfu_layout[l];
491}
492
493void dfu_show_entities(void)
494{
495 struct dfu_entity *dfu;
496
497 puts("DFU alt settings list:\n");
498
499 list_for_each_entry(dfu, &dfu_list, list) {
500 printf("dev: %s alt: %d name: %s layout: %s\n",
501 dfu_get_dev_type(dfu->dev_type), dfu->alt,
502 dfu->name, dfu_get_layout(dfu->layout));
503 }
504}
505
506int dfu_get_alt_number(void)
507{
508 return dfu_alt_num;
509}
510
511struct dfu_entity *dfu_get_entity(int alt)
512{
513 struct dfu_entity *dfu;
514
515 list_for_each_entry(dfu, &dfu_list, list) {
516 if (dfu->alt == alt)
517 return dfu;
518 }
519
520 return NULL;
521}
Lukasz Majewski1737f0f2013-10-08 14:30:38 +0200522
523int dfu_get_alt(char *name)
524{
525 struct dfu_entity *dfu;
Lukasz Majewski3a39d012014-11-05 10:54:16 +0100526 char *str;
Lukasz Majewski1737f0f2013-10-08 14:30:38 +0200527
528 list_for_each_entry(dfu, &dfu_list, list) {
Lukasz Majewski3a39d012014-11-05 10:54:16 +0100529 if (dfu->name[0] != '/') {
530 if (!strncmp(dfu->name, name, strlen(dfu->name)))
531 return dfu->alt;
532 } else {
533 /*
534 * One must also consider absolute path
535 * (/boot/bin/uImage) available at dfu->name when
536 * compared "plain" file name (uImage)
537 *
538 * It is the case for e.g. thor gadget where lthor SW
539 * sends only the file name, so only the very last part
540 * of path must be checked for equality
541 */
542
543 str = strstr(dfu->name, name);
544 if (!str)
545 continue;
546
547 /*
548 * Check if matching substring is the last element of
549 * dfu->name (uImage)
550 */
551 if (strlen(dfu->name) ==
552 ((str - dfu->name) + strlen(name)))
553 return dfu->alt;
554 }
Lukasz Majewski1737f0f2013-10-08 14:30:38 +0200555 }
556
557 return -ENODEV;
558}
Lukasz Majewskicc2d0262015-08-24 00:21:46 +0200559
560int dfu_write_from_mem_addr(struct dfu_entity *dfu, void *buf, int size)
561{
562 unsigned long dfu_buf_size, write, left = size;
563 int i, ret = 0;
564 void *dp = buf;
565
566 /*
567 * Here we must call dfu_get_buf(dfu) first to be sure that dfu_buf_size
568 * has been properly initialized - e.g. if "dfu_bufsiz" has been taken
569 * into account.
570 */
571 dfu_get_buf(dfu);
572 dfu_buf_size = dfu_get_buf_size();
573 debug("%s: dfu buf size: %lu\n", __func__, dfu_buf_size);
574
575 for (i = 0; left > 0; i++) {
576 write = min(dfu_buf_size, left);
577
578 debug("%s: dp: 0x%p left: %lu write: %lu\n", __func__,
579 dp, left, write);
580 ret = dfu_write(dfu, dp, write, i);
581 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900582 pr_err("DFU write failed\n");
Lukasz Majewskicc2d0262015-08-24 00:21:46 +0200583 return ret;
584 }
585
586 dp += write;
587 left -= write;
588 }
589
590 ret = dfu_flush(dfu, NULL, 0, i);
591 if (ret)
Masahiro Yamada81e10422017-09-16 14:10:41 +0900592 pr_err("DFU flush failed!");
Lukasz Majewskicc2d0262015-08-24 00:21:46 +0200593
594 return ret;
595}