blob: 318949529b302db73867f4bd311aa6203539fe7b [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>
Heiko Schochere7e75c72013-06-12 06:05:51 +020010#include <errno.h>
Lukasz Majewskif22b11c2012-08-06 14:41:07 +020011#include <malloc.h>
12#include <mmc.h>
13#include <fat.h>
14#include <dfu.h>
Lukasz Majewskibd694242014-05-12 10:43:36 +020015#include <hash.h>
Lukasz Majewskif22b11c2012-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 Marczakb627eb42014-02-28 18:53:37 +010021static int alt_num_cnt;
Lukasz Majewskibd694242014-05-12 10:43:36 +020022static struct hash_algo *dfu_hash_algo;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +020023
Lukasz Majewski1cc03c52014-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 Majewski6bed7ce2013-07-18 13:19:14 +020036{
B, Ravi66928af2017-05-04 15:45:29 +053037#ifdef CONFIG_SPL_DFU_NO_RESET
38 return false;
39#else
Lukasz Majewski1cc03c52014-08-25 11:07:28 +020040 return true;
B, Ravi66928af2017-05-04 15:45:29 +053041#endif
Lukasz Majewski6bed7ce2013-07-18 13:19:14 +020042}
43
Lukasz Majewskif22b11c2012-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 Warrendd648272014-06-11 16:03:33 -060055int dfu_init_env_entities(char *interface, char *devstr)
Lukasz Majewski765c5ae2013-09-11 14:53:35 +020056{
57 const char *str_env;
58 char *env_bkp;
Sam Protsenko87a8ca92018-07-13 16:35:46 +030059 int ret = 0;
Lukasz Majewski765c5ae2013-09-11 14:53:35 +020060
Przemyslaw Marczak899a5282015-02-17 12:24:11 +010061#ifdef CONFIG_SET_DFU_ALT_INFO
62 set_dfu_alt_info(interface, devstr);
63#endif
Simon Glass00caae62017-08-03 12:22:12 -060064 str_env = env_get("dfu_alt_info");
Lukasz Majewski765c5ae2013-09-11 14:53:35 +020065 if (!str_env) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +090066 pr_err("\"dfu_alt_info\" env variable not defined!\n");
Lukasz Majewski765c5ae2013-09-11 14:53:35 +020067 return -EINVAL;
68 }
69
70 env_bkp = strdup(str_env);
Stephen Warrendd648272014-06-11 16:03:33 -060071 ret = dfu_config_entities(env_bkp, interface, devstr);
Lukasz Majewski765c5ae2013-09-11 14:53:35 +020072 if (ret) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +090073 pr_err("DFU entities configuration failed!\n");
Sam Protsenko28a5c882018-07-13 16:35:47 +030074 pr_err("(partition table does not match dfu_alt_info?)\n");
Sam Protsenko87a8ca92018-07-13 16:35:46 +030075 goto done;
Lukasz Majewski765c5ae2013-09-11 14:53:35 +020076 }
77
Sam Protsenko87a8ca92018-07-13 16:35:46 +030078done:
Lukasz Majewski765c5ae2013-09-11 14:53:35 +020079 free(env_bkp);
Sam Protsenko87a8ca92018-07-13 16:35:46 +030080 return ret;
Lukasz Majewski765c5ae2013-09-11 14:53:35 +020081}
82
Heiko Schochere7e75c72013-06-12 06:05:51 +020083static unsigned char *dfu_buf;
Lukasz Majewskia7e68922015-07-08 23:43:18 +020084static unsigned long dfu_buf_size;
Heiko Schochere7e75c72013-06-12 06:05:51 +020085
Lukasz Majewskid4278262013-10-08 14:30:39 +020086unsigned char *dfu_free_buf(void)
Heiko Schochere7e75c72013-06-12 06:05:51 +020087{
88 free(dfu_buf);
89 dfu_buf = NULL;
90 return dfu_buf;
91}
92
Lukasz Majewski4fb12782013-12-09 16:20:13 +010093unsigned long dfu_get_buf_size(void)
94{
95 return dfu_buf_size;
96}
97
Stephen Warren7ac1b412014-06-11 16:03:34 -060098unsigned char *dfu_get_buf(struct dfu_entity *dfu)
Heiko Schochere7e75c72013-06-12 06:05:51 +020099{
100 char *s;
101
102 if (dfu_buf != NULL)
103 return dfu_buf;
104
Simon Glass00caae62017-08-03 12:22:12 -0600105 s = env_get("dfu_bufsiz");
Przemyslaw Marczakf597fc32014-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 Warren7ac1b412014-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 Schochere7e75c72013-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 Majewskif22b11c2012-08-06 14:41:07 +0200122
Lukasz Majewskibd694242014-05-12 10:43:36 +0200123static char *dfu_get_hash_algo(void)
124{
125 char *s;
126
Simon Glass00caae62017-08-03 12:22:12 -0600127 s = env_get("dfu_hash_algo");
Lukasz Majewski3d83e672014-06-10 12:25:59 +0200128 if (!s)
Lukasz Majewskibd694242014-05-12 10:43:36 +0200129 return NULL;
130
Lukasz Majewski3d83e672014-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 Yamada9b643e32017-09-16 14:10:41 +0900136 pr_err("DFU hash method: %s not supported!\n", s);
Lukasz Majewskibd694242014-05-12 10:43:36 +0200137 return NULL;
138}
139
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000140static int dfu_write_buffer_drain(struct dfu_entity *dfu)
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200141{
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000142 long w_size;
143 int ret;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200144
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000145 /* flush size? */
146 w_size = dfu->i_buf - dfu->i_buf_start;
147 if (w_size == 0)
148 return 0;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200149
Lukasz Majewskibd694242014-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);
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200153
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000154 ret = dfu->write_medium(dfu, dfu->offset, dfu->i_buf_start, &w_size);
155 if (ret)
156 debug("%s: Write error!\n", __func__);
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200157
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000158 /* point back */
159 dfu->i_buf = dfu->i_buf_start;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200160
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000161 /* update offset */
162 dfu->offset += w_size;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200163
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000164 puts("#");
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200165
166 return ret;
167}
168
Patrick Delaunay57da0602017-07-19 16:39:24 +0200169void dfu_transaction_cleanup(struct dfu_entity *dfu)
Stephen Warren3ee95932014-06-11 12:48:08 -0600170{
171 /* clear everything */
Stephen Warren3ee95932014-06-11 12:48:08 -0600172 dfu->crc = 0;
173 dfu->offset = 0;
174 dfu->i_blk_seq_num = 0;
Patrick Delaunay6fa8ddd2017-07-19 16:39:25 +0200175 dfu->i_buf_start = dfu_get_buf(dfu);
176 dfu->i_buf_end = dfu->i_buf_start;
Stephen Warren3ee95932014-06-11 12:48:08 -0600177 dfu->i_buf = dfu->i_buf_start;
Patrick Delaunay57da0602017-07-19 16:39:24 +0200178 dfu->r_left = 0;
179 dfu->b_left = 0;
180 dfu->bad_skip = 0;
181
Stephen Warren3ee95932014-06-11 12:48:08 -0600182 dfu->inited = 0;
183}
184
Patrick Delaunay6fa8ddd2017-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 Schochera2199af2014-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 Marczak1aa4bdc2014-05-12 12:05:33 +0200215 ret = dfu_write_buffer_drain(dfu);
216 if (ret)
217 return ret;
218
Heiko Schochera2199af2014-03-18 08:09:55 +0100219 if (dfu->flush_medium)
220 ret = dfu->flush_medium(dfu);
221
Lukasz Majewskibd694242014-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 Schochera2199af2014-03-18 08:09:55 +0100225
Patrick Delaunay57da0602017-07-19 16:39:24 +0200226 dfu_transaction_cleanup(dfu);
Heiko Schochera2199af2014-03-18 08:09:55 +0100227
228 return ret;
229}
230
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000231int dfu_write(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200232{
Stephen Warren3ee95932014-06-11 12:48:08 -0600233 int ret;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200234
Stephen Warrene621c7a2015-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 Antoniouea2453d2013-03-14 05:32:48 +0000236 __func__, dfu->name, buf, size, blk_seq_num, dfu->offset,
Stephen Warrene621c7a2015-07-22 14:54:04 -0600237 (unsigned long)(dfu->i_buf - dfu->i_buf_start));
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200238
Patrick Delaunay6fa8ddd2017-07-19 16:39:25 +0200239 ret = dfu_transaction_initiate(dfu, false);
240 if (ret < 0)
241 return ret;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200242
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000243 if (dfu->i_blk_seq_num != blk_seq_num) {
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200244 printf("%s: Wrong sequence number! [%d] [%d]\n",
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000245 __func__, dfu->i_blk_seq_num, blk_seq_num);
Patrick Delaunay57da0602017-07-19 16:39:24 +0200246 dfu_transaction_cleanup(dfu);
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200247 return -1;
248 }
249
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000250 /* 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 */
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200262
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000263 /* 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 Warren3ee95932014-06-11 12:48:08 -0600268 ret = dfu_write_buffer_drain(dfu);
269 if (ret) {
Patrick Delaunay57da0602017-07-19 16:39:24 +0200270 dfu_transaction_cleanup(dfu);
Stephen Warren3ee95932014-06-11 12:48:08 -0600271 return ret;
272 }
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200273 }
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000274
275 /* we should be in buffer now (if not then size too large) */
276 if ((dfu->i_buf + size) > dfu->i_buf_end) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900277 pr_err("Buffer overflow! (0x%p + 0x%x > 0x%p)\n", dfu->i_buf,
Lukasz Majewskia7d2c3c2013-09-10 15:29:22 +0200278 size, dfu->i_buf_end);
Patrick Delaunay57da0602017-07-19 16:39:24 +0200279 dfu_transaction_cleanup(dfu);
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000280 return -1;
281 }
282
283 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 Warren3ee95932014-06-11 12:48:08 -0600288 ret = dfu_write_buffer_drain(dfu);
289 if (ret) {
Patrick Delaunay57da0602017-07-19 16:39:24 +0200290 dfu_transaction_cleanup(dfu);
Stephen Warren3ee95932014-06-11 12:48:08 -0600291 return ret;
292 }
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000293 }
294
Stephen Warren3ee95932014-06-11 12:48:08 -0600295 return 0;
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000296}
297
298static 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 Yamadab4141192014-11-07 03:03:31 +0900306 chunk = min((long)size, dfu->b_left);
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000307 /* consume */
308 if (chunk > 0) {
309 memcpy(buf, dfu->i_buf, chunk);
Lukasz Majewskibd694242014-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 Antoniouea2453d2013-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
350int dfu_read(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
351{
352 int ret = 0;
353
354 debug("%s: name: %s buf: 0x%p size: 0x%x p_num: 0x%x i_buf: 0x%p\n",
355 __func__, dfu->name, buf, size, blk_seq_num, dfu->i_buf);
356
Patrick Delaunay6fa8ddd2017-07-19 16:39:25 +0200357 ret = dfu_transaction_initiate(dfu, true);
358 if (ret < 0)
359 return ret;
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000360
361 if (dfu->i_blk_seq_num != blk_seq_num) {
362 printf("%s: Wrong sequence number! [%d] [%d]\n",
363 __func__, dfu->i_blk_seq_num, blk_seq_num);
364 return -1;
365 }
366 /* handle rollover */
367 dfu->i_blk_seq_num = (dfu->i_blk_seq_num + 1) & 0xffff;
368
369 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 Majewskibd694242014-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 Antoniouea2453d2013-03-14 05:32:48 +0000379 puts("\nUPLOAD ... done\nCtrl+C to exit ...\n");
380
Patrick Delaunay57da0602017-07-19 16:39:24 +0200381 dfu_transaction_cleanup(dfu);
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000382 }
383
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200384 return ret;
385}
386
387static int dfu_fill_entity(struct dfu_entity *dfu, char *s, int alt,
Stephen Warrendd648272014-06-11 16:03:33 -0600388 char *interface, char *devstr)
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200389{
390 char *st;
391
Stephen Warrendd648272014-06-11 16:03:33 -0600392 debug("%s: %s interface: %s dev: %s\n", __func__, s, interface, devstr);
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200393 st = strsep(&s, " ");
394 strcpy(dfu->name, st);
395
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200396 dfu->alt = alt;
Stephen Warren7ac1b412014-06-11 16:03:34 -0600397 dfu->max_buf_size = 0;
Stephen Warrencb7bd2e2014-06-11 16:03:35 -0600398 dfu->free_entity = NULL;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200399
400 /* Specific for mmc device */
401 if (strcmp(interface, "mmc") == 0) {
Stephen Warrendd648272014-06-11 16:03:33 -0600402 if (dfu_fill_entity_mmc(dfu, devstr, s))
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200403 return -1;
Pantelis Antoniouc6631762013-03-14 05:32:52 +0000404 } else if (strcmp(interface, "nand") == 0) {
Stephen Warrendd648272014-06-11 16:03:33 -0600405 if (dfu_fill_entity_nand(dfu, devstr, s))
Pantelis Antoniouc6631762013-03-14 05:32:52 +0000406 return -1;
Afzal Mohammeda9479f02013-09-18 01:15:24 +0530407 } else if (strcmp(interface, "ram") == 0) {
Stephen Warrendd648272014-06-11 16:03:33 -0600408 if (dfu_fill_entity_ram(dfu, devstr, s))
Afzal Mohammeda9479f02013-09-18 01:15:24 +0530409 return -1;
Stephen Warren6f12ebf2014-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 Majewskif22b11c2012-08-06 14:41:07 +0200413 } else {
414 printf("%s: Device %s not (yet) supported!\n",
415 __func__, interface);
416 return -1;
417 }
Stephen Warren806bd242015-09-04 22:03:46 -0600418 dfu_get_buf(dfu);
Lukasz Majewskif22b11c2012-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 Warren806bd242015-09-04 22:03:46 -0600427 dfu_free_buf();
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200428 list_for_each_entry_safe_reverse(dfu, p, &dfu_list, list) {
429 list_del(&dfu->list);
Stephen Warrencb7bd2e2014-06-11 16:03:35 -0600430 if (dfu->free_entity)
431 dfu->free_entity(dfu);
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200432 t = dfu;
433 }
434 if (t)
435 free(t);
436 INIT_LIST_HEAD(&dfu_list);
Przemyslaw Marczakb627eb42014-02-28 18:53:37 +0100437
438 alt_num_cnt = 0;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200439}
440
Stephen Warrendd648272014-06-11 16:03:33 -0600441int dfu_config_entities(char *env, char *interface, char *devstr)
Lukasz Majewskif22b11c2012-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 Majewskibd694242014-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 Yamada9b643e32017-09-16 14:10:41 +0900455 pr_err("Hash algorithm %s not supported\n", s);
Lukasz Majewskibd694242014-05-12 10:43:36 +0200456 }
457
Lukasz Majewskif22b11c2012-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 Warrendd648272014-06-11 16:03:33 -0600464 ret = dfu_fill_entity(&dfu[i], s, alt_num_cnt, interface,
465 devstr);
Peng Fan5d8fae72016-05-03 10:24:52 +0800466 if (ret) {
Sam Protsenkofeaa7852018-07-13 16:35:45 +0300467 /* We will free "dfu" in dfu_free_entities() */
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200468 return -1;
Peng Fan5d8fae72016-05-03 10:24:52 +0800469 }
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200470
471 list_add_tail(&dfu[i].list, &dfu_list);
Przemyslaw Marczakb627eb42014-02-28 18:53:37 +0100472 alt_num_cnt++;
Lukasz Majewskif22b11c2012-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 Delaunaydd93a8e2016-10-28 09:44:26 +0200480 const char *dev_t[] = {NULL, "eMMC", "OneNAND", "NAND", "RAM", "SF" };
Lukasz Majewskif22b11c2012-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 Mohammeda9479f02013-09-18 01:15:24 +0530487 "EXT3", "EXT4", "RAM_ADDR" };
Lukasz Majewskif22b11c2012-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 Majewskifed936e2013-10-08 14:30:38 +0200520
521int dfu_get_alt(char *name)
522{
523 struct dfu_entity *dfu;
Lukasz Majewski5610b052014-11-05 10:54:16 +0100524 char *str;
Lukasz Majewskifed936e2013-10-08 14:30:38 +0200525
526 list_for_each_entry(dfu, &dfu_list, list) {
Lukasz Majewski5610b052014-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 Majewskifed936e2013-10-08 14:30:38 +0200553 }
554
555 return -ENODEV;
556}
Lukasz Majewski2092e462015-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 Yamada9b643e32017-09-16 14:10:41 +0900580 pr_err("DFU write failed\n");
Lukasz Majewski2092e462015-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 Yamada9b643e32017-09-16 14:10:41 +0900590 pr_err("DFU flush failed!");
Lukasz Majewski2092e462015-08-24 00:21:46 +0200591
592 return ret;
593}