blob: 318949529b302db73867f4bd311aa6203539fe7b [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>
Heiko Schochera2f831e2013-06-12 06:05:51 +020010#include <errno.h>
Lukasz Majewskid311c6e2012-08-06 14:41:07 +020011#include <malloc.h>
12#include <mmc.h>
13#include <fat.h>
14#include <dfu.h>
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +020015#include <hash.h>
Lukasz Majewskid311c6e2012-08-06 14:41:07 +020016#include <linux/list.h>
17#include <linux/compiler.h>
18
19static LIST_HEAD(dfu_list);
20static int dfu_alt_num;
Przemyslaw Marczak4af3e082014-02-28 18:53:37 +010021static int alt_num_cnt;
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +020022static struct hash_algo *dfu_hash_algo;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +020023
Lukasz Majewskid9eae702014-08-25 11:07:28 +020024/*
25 * The purpose of the dfu_usb_get_reset() function is to
26 * provide information if after USB_DETACH request
27 * being sent the dfu-util performed reset of USB
28 * bus.
29 *
30 * Described behaviour is the only way to distinct if
31 * user has typed -e (detach) or -R (reset) when invoking
32 * dfu-util command.
33 *
34 */
35__weak bool dfu_usb_get_reset(void)
Lukasz Majewski2968aa22013-07-18 13:19:14 +020036{
B, Ravi4cca5cf2017-05-04 15:45:29 +053037#ifdef CONFIG_SPL_DFU_NO_RESET
38 return false;
39#else
Lukasz Majewskid9eae702014-08-25 11:07:28 +020040 return true;
B, Ravi4cca5cf2017-05-04 15:45:29 +053041#endif
Lukasz Majewski2968aa22013-07-18 13:19:14 +020042}
43
Lukasz Majewskid311c6e2012-08-06 14:41:07 +020044static int dfu_find_alt_num(const char *s)
45{
46 int i = 0;
47
48 for (; *s; s++)
49 if (*s == ';')
50 i++;
51
52 return ++i;
53}
54
Stephen Warren4afe50f2014-06-11 16:03:33 -060055int dfu_init_env_entities(char *interface, char *devstr)
Lukasz Majewski35e591e2013-09-11 14:53:35 +020056{
57 const char *str_env;
58 char *env_bkp;
Sam Protsenko6500cde2018-07-13 16:35:46 +030059 int ret = 0;
Lukasz Majewski35e591e2013-09-11 14:53:35 +020060
Przemyslaw Marczakd0dd6092015-02-17 12:24:11 +010061#ifdef CONFIG_SET_DFU_ALT_INFO
62 set_dfu_alt_info(interface, devstr);
63#endif
Simon Glass64b723f2017-08-03 12:22:12 -060064 str_env = env_get("dfu_alt_info");
Lukasz Majewski35e591e2013-09-11 14:53:35 +020065 if (!str_env) {
Masahiro Yamada81e10422017-09-16 14:10:41 +090066 pr_err("\"dfu_alt_info\" env variable not defined!\n");
Lukasz Majewski35e591e2013-09-11 14:53:35 +020067 return -EINVAL;
68 }
69
70 env_bkp = strdup(str_env);
Stephen Warren4afe50f2014-06-11 16:03:33 -060071 ret = dfu_config_entities(env_bkp, interface, devstr);
Lukasz Majewski35e591e2013-09-11 14:53:35 +020072 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +090073 pr_err("DFU entities configuration failed!\n");
Sam Protsenkoab79fb02018-07-13 16:35:47 +030074 pr_err("(partition table does not match dfu_alt_info?)\n");
Sam Protsenko6500cde2018-07-13 16:35:46 +030075 goto done;
Lukasz Majewski35e591e2013-09-11 14:53:35 +020076 }
77
Sam Protsenko6500cde2018-07-13 16:35:46 +030078done:
Lukasz Majewski35e591e2013-09-11 14:53:35 +020079 free(env_bkp);
Sam Protsenko6500cde2018-07-13 16:35:46 +030080 return ret;
Lukasz Majewski35e591e2013-09-11 14:53:35 +020081}
82
Heiko Schochera2f831e2013-06-12 06:05:51 +020083static unsigned char *dfu_buf;
Lukasz Majewski0af4df82015-07-08 23:43:18 +020084static unsigned long dfu_buf_size;
Heiko Schochera2f831e2013-06-12 06:05:51 +020085
Lukasz Majewski34264232013-10-08 14:30:39 +020086unsigned char *dfu_free_buf(void)
Heiko Schochera2f831e2013-06-12 06:05:51 +020087{
88 free(dfu_buf);
89 dfu_buf = NULL;
90 return dfu_buf;
91}
92
Lukasz Majewski031d4132013-12-09 16:20:13 +010093unsigned long dfu_get_buf_size(void)
94{
95 return dfu_buf_size;
96}
97
Stephen Warrenf9500982014-06-11 16:03:34 -060098unsigned char *dfu_get_buf(struct dfu_entity *dfu)
Heiko Schochera2f831e2013-06-12 06:05:51 +020099{
100 char *s;
101
102 if (dfu_buf != NULL)
103 return dfu_buf;
104
Simon Glass64b723f2017-08-03 12:22:12 -0600105 s = env_get("dfu_bufsiz");
Przemyslaw Marczak24883502014-12-15 10:34:11 +0100106 if (s)
107 dfu_buf_size = (unsigned long)simple_strtol(s, NULL, 0);
108
109 if (!s || !dfu_buf_size)
110 dfu_buf_size = CONFIG_SYS_DFU_DATA_BUF_SIZE;
111
Stephen Warrenf9500982014-06-11 16:03:34 -0600112 if (dfu->max_buf_size && dfu_buf_size > dfu->max_buf_size)
113 dfu_buf_size = dfu->max_buf_size;
Heiko Schochera2f831e2013-06-12 06:05:51 +0200114
115 dfu_buf = memalign(CONFIG_SYS_CACHELINE_SIZE, dfu_buf_size);
116 if (dfu_buf == NULL)
117 printf("%s: Could not memalign 0x%lx bytes\n",
118 __func__, dfu_buf_size);
119
120 return dfu_buf;
121}
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200122
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200123static char *dfu_get_hash_algo(void)
124{
125 char *s;
126
Simon Glass64b723f2017-08-03 12:22:12 -0600127 s = env_get("dfu_hash_algo");
Lukasz Majewski3e0d0282014-06-10 12:25:59 +0200128 if (!s)
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200129 return NULL;
130
Lukasz Majewski3e0d0282014-06-10 12:25:59 +0200131 if (!strcmp(s, "crc32")) {
132 debug("%s: DFU hash method: %s\n", __func__, s);
133 return s;
134 }
135
Masahiro Yamada81e10422017-09-16 14:10:41 +0900136 pr_err("DFU hash method: %s not supported!\n", s);
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200137 return NULL;
138}
139
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000140static int dfu_write_buffer_drain(struct dfu_entity *dfu)
141{
142 long w_size;
143 int ret;
144
145 /* flush size? */
146 w_size = dfu->i_buf - dfu->i_buf_start;
147 if (w_size == 0)
148 return 0;
149
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200150 if (dfu_hash_algo)
151 dfu_hash_algo->hash_update(dfu_hash_algo, &dfu->crc,
152 dfu->i_buf_start, w_size, 0);
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000153
154 ret = dfu->write_medium(dfu, dfu->offset, dfu->i_buf_start, &w_size);
155 if (ret)
156 debug("%s: Write error!\n", __func__);
157
158 /* point back */
159 dfu->i_buf = dfu->i_buf_start;
160
161 /* update offset */
162 dfu->offset += w_size;
163
164 puts("#");
165
166 return ret;
167}
168
Patrick Delaunay35be83e2017-07-19 16:39:24 +0200169void dfu_transaction_cleanup(struct dfu_entity *dfu)
Stephen Warren7f450232014-06-11 12:48:08 -0600170{
171 /* clear everything */
Stephen Warren7f450232014-06-11 12:48:08 -0600172 dfu->crc = 0;
173 dfu->offset = 0;
174 dfu->i_blk_seq_num = 0;
Patrick Delaunaydec4ecb2017-07-19 16:39:25 +0200175 dfu->i_buf_start = dfu_get_buf(dfu);
176 dfu->i_buf_end = dfu->i_buf_start;
Stephen Warren7f450232014-06-11 12:48:08 -0600177 dfu->i_buf = dfu->i_buf_start;
Patrick Delaunay35be83e2017-07-19 16:39:24 +0200178 dfu->r_left = 0;
179 dfu->b_left = 0;
180 dfu->bad_skip = 0;
181
Stephen Warren7f450232014-06-11 12:48:08 -0600182 dfu->inited = 0;
183}
184
Patrick Delaunaydec4ecb2017-07-19 16:39:25 +0200185int dfu_transaction_initiate(struct dfu_entity *dfu, bool read)
186{
187 int ret = 0;
188
189 if (dfu->inited)
190 return 0;
191
192 dfu_transaction_cleanup(dfu);
193
194 if (dfu->i_buf_start == NULL)
195 return -ENOMEM;
196
197 dfu->i_buf_end = dfu->i_buf_start + dfu_get_buf_size();
198
199 if (read) {
200 ret = dfu->get_medium_size(dfu, &dfu->r_left);
201 if (ret < 0)
202 return ret;
203 debug("%s: %s %lld [B]\n", __func__, dfu->name, dfu->r_left);
204 }
205
206 dfu->inited = 1;
207
208 return 0;
209}
210
Heiko Schocher941b3842014-03-18 08:09:55 +0100211int dfu_flush(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
212{
213 int ret = 0;
214
Przemyslaw Marczaka0f6a382014-05-12 12:05:33 +0200215 ret = dfu_write_buffer_drain(dfu);
216 if (ret)
217 return ret;
218
Heiko Schocher941b3842014-03-18 08:09:55 +0100219 if (dfu->flush_medium)
220 ret = dfu->flush_medium(dfu);
221
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200222 if (dfu_hash_algo)
223 printf("\nDFU complete %s: 0x%08x\n", dfu_hash_algo->name,
224 dfu->crc);
Heiko Schocher941b3842014-03-18 08:09:55 +0100225
Patrick Delaunay35be83e2017-07-19 16:39:24 +0200226 dfu_transaction_cleanup(dfu);
Heiko Schocher941b3842014-03-18 08:09:55 +0100227
228 return ret;
229}
230
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200231int dfu_write(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
232{
Stephen Warren7f450232014-06-11 12:48:08 -0600233 int ret;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200234
Stephen Warrenc00315b2015-07-22 14:54:04 -0600235 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 +0000236 __func__, dfu->name, buf, size, blk_seq_num, dfu->offset,
Stephen Warrenc00315b2015-07-22 14:54:04 -0600237 (unsigned long)(dfu->i_buf - dfu->i_buf_start));
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200238
Patrick Delaunaydec4ecb2017-07-19 16:39:25 +0200239 ret = dfu_transaction_initiate(dfu, false);
240 if (ret < 0)
241 return ret;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200242
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000243 if (dfu->i_blk_seq_num != blk_seq_num) {
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200244 printf("%s: Wrong sequence number! [%d] [%d]\n",
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000245 __func__, dfu->i_blk_seq_num, blk_seq_num);
Patrick Delaunay35be83e2017-07-19 16:39:24 +0200246 dfu_transaction_cleanup(dfu);
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000247 return -1;
248 }
249
250 /* DFU 1.1 standard says:
251 * The wBlockNum field is a block sequence number. It increments each
252 * time a block is transferred, wrapping to zero from 65,535. It is used
253 * to provide useful context to the DFU loader in the device."
254 *
255 * This means that it's a 16 bit counter that roll-overs at
256 * 0xffff -> 0x0000. By having a typical 4K transfer block
257 * we roll-over at exactly 256MB. Not very fun to debug.
258 *
259 * Handling rollover, and having an inited variable,
260 * makes things work.
261 */
262
263 /* handle rollover */
264 dfu->i_blk_seq_num = (dfu->i_blk_seq_num + 1) & 0xffff;
265
266 /* flush buffer if overflow */
267 if ((dfu->i_buf + size) > dfu->i_buf_end) {
Stephen Warren7f450232014-06-11 12:48:08 -0600268 ret = dfu_write_buffer_drain(dfu);
269 if (ret) {
Patrick Delaunay35be83e2017-07-19 16:39:24 +0200270 dfu_transaction_cleanup(dfu);
Stephen Warren7f450232014-06-11 12:48:08 -0600271 return ret;
272 }
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000273 }
274
275 /* we should be in buffer now (if not then size too large) */
276 if ((dfu->i_buf + size) > dfu->i_buf_end) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900277 pr_err("Buffer overflow! (0x%p + 0x%x > 0x%p)\n", dfu->i_buf,
Lukasz Majewski385c3212013-09-10 15:29:22 +0200278 size, dfu->i_buf_end);
Patrick Delaunay35be83e2017-07-19 16:39:24 +0200279 dfu_transaction_cleanup(dfu);
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200280 return -1;
281 }
282
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000283 memcpy(dfu->i_buf, buf, size);
284 dfu->i_buf += size;
285
286 /* if end or if buffer full flush */
287 if (size == 0 || (dfu->i_buf + size) > dfu->i_buf_end) {
Stephen Warren7f450232014-06-11 12:48:08 -0600288 ret = dfu_write_buffer_drain(dfu);
289 if (ret) {
Patrick Delaunay35be83e2017-07-19 16:39:24 +0200290 dfu_transaction_cleanup(dfu);
Stephen Warren7f450232014-06-11 12:48:08 -0600291 return ret;
292 }
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000293 }
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200294
Stephen Warren7f450232014-06-11 12:48:08 -0600295 return 0;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200296}
297
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000298static int dfu_read_buffer_fill(struct dfu_entity *dfu, void *buf, int size)
299{
300 long chunk;
301 int ret, readn;
302
303 readn = 0;
304 while (size > 0) {
305 /* get chunk that can be read */
Masahiro Yamadadb204642014-11-07 03:03:31 +0900306 chunk = min((long)size, dfu->b_left);
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000307 /* consume */
308 if (chunk > 0) {
309 memcpy(buf, dfu->i_buf, chunk);
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200310 if (dfu_hash_algo)
311 dfu_hash_algo->hash_update(dfu_hash_algo,
312 &dfu->crc, buf,
313 chunk, 0);
314
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000315 dfu->i_buf += chunk;
316 dfu->b_left -= chunk;
317 size -= chunk;
318 buf += chunk;
319 readn += chunk;
320 }
321
322 /* all done */
323 if (size > 0) {
324 /* no more to read */
325 if (dfu->r_left == 0)
326 break;
327
328 dfu->i_buf = dfu->i_buf_start;
329 dfu->b_left = dfu->i_buf_end - dfu->i_buf_start;
330
331 /* got to read, but buffer is empty */
332 if (dfu->b_left > dfu->r_left)
333 dfu->b_left = dfu->r_left;
334 ret = dfu->read_medium(dfu, dfu->offset, dfu->i_buf,
335 &dfu->b_left);
336 if (ret != 0) {
337 debug("%s: Read error!\n", __func__);
338 return ret;
339 }
340 dfu->offset += dfu->b_left;
341 dfu->r_left -= dfu->b_left;
342
343 puts("#");
344 }
345 }
346
347 return readn;
348}
349
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200350int dfu_read(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
351{
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200352 int ret = 0;
353
354 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 +0000355 __func__, dfu->name, buf, size, blk_seq_num, dfu->i_buf);
356
Patrick Delaunaydec4ecb2017-07-19 16:39:25 +0200357 ret = dfu_transaction_initiate(dfu, true);
358 if (ret < 0)
359 return ret;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200360
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000361 if (dfu->i_blk_seq_num != blk_seq_num) {
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200362 printf("%s: Wrong sequence number! [%d] [%d]\n",
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000363 __func__, dfu->i_blk_seq_num, blk_seq_num);
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200364 return -1;
365 }
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000366 /* handle rollover */
367 dfu->i_blk_seq_num = (dfu->i_blk_seq_num + 1) & 0xffff;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200368
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000369 ret = dfu_read_buffer_fill(dfu, buf, size);
370 if (ret < 0) {
371 printf("%s: Failed to fill buffer\n", __func__);
372 return -1;
373 }
374
375 if (ret < size) {
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200376 if (dfu_hash_algo)
377 debug("%s: %s %s: 0x%x\n", __func__, dfu->name,
378 dfu_hash_algo->name, dfu->crc);
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000379 puts("\nUPLOAD ... done\nCtrl+C to exit ...\n");
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200380
Patrick Delaunay35be83e2017-07-19 16:39:24 +0200381 dfu_transaction_cleanup(dfu);
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200382 }
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000383
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200384 return ret;
385}
386
387static int dfu_fill_entity(struct dfu_entity *dfu, char *s, int alt,
Stephen Warren4afe50f2014-06-11 16:03:33 -0600388 char *interface, char *devstr)
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200389{
390 char *st;
391
Stephen Warren4afe50f2014-06-11 16:03:33 -0600392 debug("%s: %s interface: %s dev: %s\n", __func__, s, interface, devstr);
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200393 st = strsep(&s, " ");
394 strcpy(dfu->name, st);
395
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200396 dfu->alt = alt;
Stephen Warrenf9500982014-06-11 16:03:34 -0600397 dfu->max_buf_size = 0;
Stephen Warrena5f77f62014-06-11 16:03:35 -0600398 dfu->free_entity = NULL;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200399
400 /* Specific for mmc device */
401 if (strcmp(interface, "mmc") == 0) {
Stephen Warren4afe50f2014-06-11 16:03:33 -0600402 if (dfu_fill_entity_mmc(dfu, devstr, s))
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200403 return -1;
Pantelis Antonioucf14d0d2013-03-14 05:32:52 +0000404 } else if (strcmp(interface, "nand") == 0) {
Stephen Warren4afe50f2014-06-11 16:03:33 -0600405 if (dfu_fill_entity_nand(dfu, devstr, s))
Pantelis Antonioucf14d0d2013-03-14 05:32:52 +0000406 return -1;
Afzal Mohammede3c687a2013-09-18 01:15:24 +0530407 } else if (strcmp(interface, "ram") == 0) {
Stephen Warren4afe50f2014-06-11 16:03:33 -0600408 if (dfu_fill_entity_ram(dfu, devstr, s))
Afzal Mohammede3c687a2013-09-18 01:15:24 +0530409 return -1;
Stephen Warren0280af72014-06-11 16:03:36 -0600410 } else if (strcmp(interface, "sf") == 0) {
411 if (dfu_fill_entity_sf(dfu, devstr, s))
412 return -1;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200413 } else {
414 printf("%s: Device %s not (yet) supported!\n",
415 __func__, interface);
416 return -1;
417 }
Stephen Warren0898da52015-09-04 22:03:46 -0600418 dfu_get_buf(dfu);
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200419
420 return 0;
421}
422
423void dfu_free_entities(void)
424{
425 struct dfu_entity *dfu, *p, *t = NULL;
426
Stephen Warren0898da52015-09-04 22:03:46 -0600427 dfu_free_buf();
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200428 list_for_each_entry_safe_reverse(dfu, p, &dfu_list, list) {
429 list_del(&dfu->list);
Stephen Warrena5f77f62014-06-11 16:03:35 -0600430 if (dfu->free_entity)
431 dfu->free_entity(dfu);
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200432 t = dfu;
433 }
434 if (t)
435 free(t);
436 INIT_LIST_HEAD(&dfu_list);
Przemyslaw Marczak4af3e082014-02-28 18:53:37 +0100437
438 alt_num_cnt = 0;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200439}
440
Stephen Warren4afe50f2014-06-11 16:03:33 -0600441int dfu_config_entities(char *env, char *interface, char *devstr)
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200442{
443 struct dfu_entity *dfu;
444 int i, ret;
445 char *s;
446
447 dfu_alt_num = dfu_find_alt_num(env);
448 debug("%s: dfu_alt_num=%d\n", __func__, dfu_alt_num);
449
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200450 dfu_hash_algo = NULL;
451 s = dfu_get_hash_algo();
452 if (s) {
453 ret = hash_lookup_algo(s, &dfu_hash_algo);
454 if (ret)
Masahiro Yamada81e10422017-09-16 14:10:41 +0900455 pr_err("Hash algorithm %s not supported\n", s);
Lukasz Majewskifa8dc1d2014-05-12 10:43:36 +0200456 }
457
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200458 dfu = calloc(sizeof(*dfu), dfu_alt_num);
459 if (!dfu)
460 return -1;
461 for (i = 0; i < dfu_alt_num; i++) {
462
463 s = strsep(&env, ";");
Stephen Warren4afe50f2014-06-11 16:03:33 -0600464 ret = dfu_fill_entity(&dfu[i], s, alt_num_cnt, interface,
465 devstr);
Peng Fan529d4fd2016-05-03 10:24:52 +0800466 if (ret) {
Sam Protsenko6d29e5c2018-07-13 16:35:45 +0300467 /* We will free "dfu" in dfu_free_entities() */
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200468 return -1;
Peng Fan529d4fd2016-05-03 10:24:52 +0800469 }
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200470
471 list_add_tail(&dfu[i].list, &dfu_list);
Przemyslaw Marczak4af3e082014-02-28 18:53:37 +0100472 alt_num_cnt++;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200473 }
474
475 return 0;
476}
477
478const char *dfu_get_dev_type(enum dfu_device_type t)
479{
Patrick Delaunay9eb430d2016-10-28 09:44:26 +0200480 const char *dev_t[] = {NULL, "eMMC", "OneNAND", "NAND", "RAM", "SF" };
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200481 return dev_t[t];
482}
483
484const char *dfu_get_layout(enum dfu_layout l)
485{
486 const char *dfu_layout[] = {NULL, "RAW_ADDR", "FAT", "EXT2",
Afzal Mohammede3c687a2013-09-18 01:15:24 +0530487 "EXT3", "EXT4", "RAM_ADDR" };
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200488 return dfu_layout[l];
489}
490
491void dfu_show_entities(void)
492{
493 struct dfu_entity *dfu;
494
495 puts("DFU alt settings list:\n");
496
497 list_for_each_entry(dfu, &dfu_list, list) {
498 printf("dev: %s alt: %d name: %s layout: %s\n",
499 dfu_get_dev_type(dfu->dev_type), dfu->alt,
500 dfu->name, dfu_get_layout(dfu->layout));
501 }
502}
503
504int dfu_get_alt_number(void)
505{
506 return dfu_alt_num;
507}
508
509struct dfu_entity *dfu_get_entity(int alt)
510{
511 struct dfu_entity *dfu;
512
513 list_for_each_entry(dfu, &dfu_list, list) {
514 if (dfu->alt == alt)
515 return dfu;
516 }
517
518 return NULL;
519}
Lukasz Majewski1737f0f2013-10-08 14:30:38 +0200520
521int dfu_get_alt(char *name)
522{
523 struct dfu_entity *dfu;
Lukasz Majewski3a39d012014-11-05 10:54:16 +0100524 char *str;
Lukasz Majewski1737f0f2013-10-08 14:30:38 +0200525
526 list_for_each_entry(dfu, &dfu_list, list) {
Lukasz Majewski3a39d012014-11-05 10:54:16 +0100527 if (dfu->name[0] != '/') {
528 if (!strncmp(dfu->name, name, strlen(dfu->name)))
529 return dfu->alt;
530 } else {
531 /*
532 * One must also consider absolute path
533 * (/boot/bin/uImage) available at dfu->name when
534 * compared "plain" file name (uImage)
535 *
536 * It is the case for e.g. thor gadget where lthor SW
537 * sends only the file name, so only the very last part
538 * of path must be checked for equality
539 */
540
541 str = strstr(dfu->name, name);
542 if (!str)
543 continue;
544
545 /*
546 * Check if matching substring is the last element of
547 * dfu->name (uImage)
548 */
549 if (strlen(dfu->name) ==
550 ((str - dfu->name) + strlen(name)))
551 return dfu->alt;
552 }
Lukasz Majewski1737f0f2013-10-08 14:30:38 +0200553 }
554
555 return -ENODEV;
556}
Lukasz Majewskicc2d0262015-08-24 00:21:46 +0200557
558int dfu_write_from_mem_addr(struct dfu_entity *dfu, void *buf, int size)
559{
560 unsigned long dfu_buf_size, write, left = size;
561 int i, ret = 0;
562 void *dp = buf;
563
564 /*
565 * Here we must call dfu_get_buf(dfu) first to be sure that dfu_buf_size
566 * has been properly initialized - e.g. if "dfu_bufsiz" has been taken
567 * into account.
568 */
569 dfu_get_buf(dfu);
570 dfu_buf_size = dfu_get_buf_size();
571 debug("%s: dfu buf size: %lu\n", __func__, dfu_buf_size);
572
573 for (i = 0; left > 0; i++) {
574 write = min(dfu_buf_size, left);
575
576 debug("%s: dp: 0x%p left: %lu write: %lu\n", __func__,
577 dp, left, write);
578 ret = dfu_write(dfu, dp, write, i);
579 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900580 pr_err("DFU write failed\n");
Lukasz Majewskicc2d0262015-08-24 00:21:46 +0200581 return ret;
582 }
583
584 dp += write;
585 left -= write;
586 }
587
588 ret = dfu_flush(dfu, NULL, 0, i);
589 if (ret)
Masahiro Yamada81e10422017-09-16 14:10:41 +0900590 pr_err("DFU flush failed!");
Lukasz Majewskicc2d0262015-08-24 00:21:46 +0200591
592 return ret;
593}