blob: d2b67b18cf020e4288d71806d94d4b3ecaaa2c0d [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Lukasz Majewskif22b11c2012-08-06 14:41:07 +02002/*
3 * dfu.c -- DFU back-end routines
4 *
5 * Copyright (C) 2012 Samsung Electronics
6 * author: Lukasz Majewski <l.majewski@samsung.com>
Lukasz Majewskif22b11c2012-08-06 14:41:07 +02007 */
8
9#include <common.h>
Simon Glass7b51b572019-08-01 09:46:52 -060010#include <env.h>
Heiko Schochere7e75c72013-06-12 06:05:51 +020011#include <errno.h>
Lukasz Majewskif22b11c2012-08-06 14:41:07 +020012#include <malloc.h>
13#include <mmc.h>
14#include <fat.h>
15#include <dfu.h>
Lukasz Majewskibd694242014-05-12 10:43:36 +020016#include <hash.h>
Lukasz Majewskif22b11c2012-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 Marczakb627eb42014-02-28 18:53:37 +010022static int alt_num_cnt;
Lukasz Majewskibd694242014-05-12 10:43:36 +020023static struct hash_algo *dfu_hash_algo;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +020024
Lukasz Majewski1cc03c52014-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 Majewski6bed7ce2013-07-18 13:19:14 +020037{
B, Ravi66928af2017-05-04 15:45:29 +053038#ifdef CONFIG_SPL_DFU_NO_RESET
39 return false;
40#else
Lukasz Majewski1cc03c52014-08-25 11:07:28 +020041 return true;
B, Ravi66928af2017-05-04 15:45:29 +053042#endif
Lukasz Majewski6bed7ce2013-07-18 13:19:14 +020043}
44
Lukasz Majewskif22b11c2012-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 Warrendd648272014-06-11 16:03:33 -060056int dfu_init_env_entities(char *interface, char *devstr)
Lukasz Majewski765c5ae2013-09-11 14:53:35 +020057{
58 const char *str_env;
59 char *env_bkp;
Sam Protsenko87a8ca92018-07-13 16:35:46 +030060 int ret = 0;
Lukasz Majewski765c5ae2013-09-11 14:53:35 +020061
Przemyslaw Marczak899a5282015-02-17 12:24:11 +010062#ifdef CONFIG_SET_DFU_ALT_INFO
63 set_dfu_alt_info(interface, devstr);
64#endif
Simon Glass00caae62017-08-03 12:22:12 -060065 str_env = env_get("dfu_alt_info");
Lukasz Majewski765c5ae2013-09-11 14:53:35 +020066 if (!str_env) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +090067 pr_err("\"dfu_alt_info\" env variable not defined!\n");
Lukasz Majewski765c5ae2013-09-11 14:53:35 +020068 return -EINVAL;
69 }
70
71 env_bkp = strdup(str_env);
Stephen Warrendd648272014-06-11 16:03:33 -060072 ret = dfu_config_entities(env_bkp, interface, devstr);
Lukasz Majewski765c5ae2013-09-11 14:53:35 +020073 if (ret) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +090074 pr_err("DFU entities configuration failed!\n");
Sam Protsenko28a5c882018-07-13 16:35:47 +030075 pr_err("(partition table does not match dfu_alt_info?)\n");
Sam Protsenko87a8ca92018-07-13 16:35:46 +030076 goto done;
Lukasz Majewski765c5ae2013-09-11 14:53:35 +020077 }
78
Sam Protsenko87a8ca92018-07-13 16:35:46 +030079done:
Lukasz Majewski765c5ae2013-09-11 14:53:35 +020080 free(env_bkp);
Sam Protsenko87a8ca92018-07-13 16:35:46 +030081 return ret;
Lukasz Majewski765c5ae2013-09-11 14:53:35 +020082}
83
Heiko Schochere7e75c72013-06-12 06:05:51 +020084static unsigned char *dfu_buf;
Lukasz Majewskia7e68922015-07-08 23:43:18 +020085static unsigned long dfu_buf_size;
Heiko Schochere7e75c72013-06-12 06:05:51 +020086
Lukasz Majewskid4278262013-10-08 14:30:39 +020087unsigned char *dfu_free_buf(void)
Heiko Schochere7e75c72013-06-12 06:05:51 +020088{
89 free(dfu_buf);
90 dfu_buf = NULL;
91 return dfu_buf;
92}
93
Lukasz Majewski4fb12782013-12-09 16:20:13 +010094unsigned long dfu_get_buf_size(void)
95{
96 return dfu_buf_size;
97}
98
Stephen Warren7ac1b412014-06-11 16:03:34 -060099unsigned char *dfu_get_buf(struct dfu_entity *dfu)
Heiko Schochere7e75c72013-06-12 06:05:51 +0200100{
101 char *s;
102
103 if (dfu_buf != NULL)
104 return dfu_buf;
105
Simon Glass00caae62017-08-03 12:22:12 -0600106 s = env_get("dfu_bufsiz");
Przemyslaw Marczakf597fc32014-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 Warren7ac1b412014-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 Schochere7e75c72013-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 Majewskif22b11c2012-08-06 14:41:07 +0200123
Lukasz Majewskibd694242014-05-12 10:43:36 +0200124static char *dfu_get_hash_algo(void)
125{
126 char *s;
127
Simon Glass00caae62017-08-03 12:22:12 -0600128 s = env_get("dfu_hash_algo");
Lukasz Majewski3d83e672014-06-10 12:25:59 +0200129 if (!s)
Lukasz Majewskibd694242014-05-12 10:43:36 +0200130 return NULL;
131
Lukasz Majewski3d83e672014-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 Yamada9b643e32017-09-16 14:10:41 +0900137 pr_err("DFU hash method: %s not supported!\n", s);
Lukasz Majewskibd694242014-05-12 10:43:36 +0200138 return NULL;
139}
140
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000141static int dfu_write_buffer_drain(struct dfu_entity *dfu)
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200142{
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000143 long w_size;
144 int ret;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200145
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000146 /* flush size? */
147 w_size = dfu->i_buf - dfu->i_buf_start;
148 if (w_size == 0)
149 return 0;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200150
Lukasz Majewskibd694242014-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);
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200154
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000155 ret = dfu->write_medium(dfu, dfu->offset, dfu->i_buf_start, &w_size);
156 if (ret)
157 debug("%s: Write error!\n", __func__);
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200158
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000159 /* point back */
160 dfu->i_buf = dfu->i_buf_start;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200161
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000162 /* update offset */
163 dfu->offset += w_size;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200164
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000165 puts("#");
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200166
167 return ret;
168}
169
Patrick Delaunay57da0602017-07-19 16:39:24 +0200170void dfu_transaction_cleanup(struct dfu_entity *dfu)
Stephen Warren3ee95932014-06-11 12:48:08 -0600171{
172 /* clear everything */
Stephen Warren3ee95932014-06-11 12:48:08 -0600173 dfu->crc = 0;
174 dfu->offset = 0;
175 dfu->i_blk_seq_num = 0;
Patrick Delaunay6fa8ddd2017-07-19 16:39:25 +0200176 dfu->i_buf_start = dfu_get_buf(dfu);
177 dfu->i_buf_end = dfu->i_buf_start;
Stephen Warren3ee95932014-06-11 12:48:08 -0600178 dfu->i_buf = dfu->i_buf_start;
Patrick Delaunay57da0602017-07-19 16:39:24 +0200179 dfu->r_left = 0;
180 dfu->b_left = 0;
181 dfu->bad_skip = 0;
182
Stephen Warren3ee95932014-06-11 12:48:08 -0600183 dfu->inited = 0;
184}
185
Patrick Delaunay6fa8ddd2017-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 Schochera2199af2014-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 Marczak1aa4bdc2014-05-12 12:05:33 +0200216 ret = dfu_write_buffer_drain(dfu);
217 if (ret)
218 return ret;
219
Heiko Schochera2199af2014-03-18 08:09:55 +0100220 if (dfu->flush_medium)
221 ret = dfu->flush_medium(dfu);
222
Lukasz Majewskibd694242014-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 Schochera2199af2014-03-18 08:09:55 +0100226
Patrick Delaunay57da0602017-07-19 16:39:24 +0200227 dfu_transaction_cleanup(dfu);
Heiko Schochera2199af2014-03-18 08:09:55 +0100228
229 return ret;
230}
231
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000232int dfu_write(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200233{
Stephen Warren3ee95932014-06-11 12:48:08 -0600234 int ret;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200235
Stephen Warrene621c7a2015-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 Antoniouea2453d2013-03-14 05:32:48 +0000237 __func__, dfu->name, buf, size, blk_seq_num, dfu->offset,
Stephen Warrene621c7a2015-07-22 14:54:04 -0600238 (unsigned long)(dfu->i_buf - dfu->i_buf_start));
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200239
Patrick Delaunay6fa8ddd2017-07-19 16:39:25 +0200240 ret = dfu_transaction_initiate(dfu, false);
241 if (ret < 0)
242 return ret;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200243
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000244 if (dfu->i_blk_seq_num != blk_seq_num) {
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200245 printf("%s: Wrong sequence number! [%d] [%d]\n",
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000246 __func__, dfu->i_blk_seq_num, blk_seq_num);
Patrick Delaunay57da0602017-07-19 16:39:24 +0200247 dfu_transaction_cleanup(dfu);
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200248 return -1;
249 }
250
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000251 /* 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 */
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200263
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000264 /* 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 Warren3ee95932014-06-11 12:48:08 -0600269 ret = dfu_write_buffer_drain(dfu);
270 if (ret) {
Patrick Delaunay57da0602017-07-19 16:39:24 +0200271 dfu_transaction_cleanup(dfu);
Stephen Warren3ee95932014-06-11 12:48:08 -0600272 return ret;
273 }
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200274 }
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000275
276 /* we should be in buffer now (if not then size too large) */
277 if ((dfu->i_buf + size) > dfu->i_buf_end) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900278 pr_err("Buffer overflow! (0x%p + 0x%x > 0x%p)\n", dfu->i_buf,
Lukasz Majewskia7d2c3c2013-09-10 15:29:22 +0200279 size, dfu->i_buf_end);
Patrick Delaunay57da0602017-07-19 16:39:24 +0200280 dfu_transaction_cleanup(dfu);
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000281 return -1;
282 }
283
284 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 Warren3ee95932014-06-11 12:48:08 -0600289 ret = dfu_write_buffer_drain(dfu);
290 if (ret) {
Patrick Delaunay57da0602017-07-19 16:39:24 +0200291 dfu_transaction_cleanup(dfu);
Stephen Warren3ee95932014-06-11 12:48:08 -0600292 return ret;
293 }
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000294 }
295
Stephen Warren3ee95932014-06-11 12:48:08 -0600296 return 0;
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000297}
298
299static 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 Yamadab4141192014-11-07 03:03:31 +0900307 chunk = min((long)size, dfu->b_left);
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000308 /* consume */
309 if (chunk > 0) {
310 memcpy(buf, dfu->i_buf, chunk);
Lukasz Majewskibd694242014-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 Antoniouea2453d2013-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
351int dfu_read(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
352{
353 int ret = 0;
354
355 debug("%s: name: %s buf: 0x%p size: 0x%x p_num: 0x%x i_buf: 0x%p\n",
356 __func__, dfu->name, buf, size, blk_seq_num, dfu->i_buf);
357
Patrick Delaunay6fa8ddd2017-07-19 16:39:25 +0200358 ret = dfu_transaction_initiate(dfu, true);
359 if (ret < 0)
360 return ret;
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000361
362 if (dfu->i_blk_seq_num != blk_seq_num) {
363 printf("%s: Wrong sequence number! [%d] [%d]\n",
364 __func__, dfu->i_blk_seq_num, blk_seq_num);
365 return -1;
366 }
367 /* handle rollover */
368 dfu->i_blk_seq_num = (dfu->i_blk_seq_num + 1) & 0xffff;
369
370 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 Majewskibd694242014-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 Antoniouea2453d2013-03-14 05:32:48 +0000380 puts("\nUPLOAD ... done\nCtrl+C to exit ...\n");
381
Patrick Delaunay57da0602017-07-19 16:39:24 +0200382 dfu_transaction_cleanup(dfu);
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000383 }
384
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200385 return ret;
386}
387
388static int dfu_fill_entity(struct dfu_entity *dfu, char *s, int alt,
Stephen Warrendd648272014-06-11 16:03:33 -0600389 char *interface, char *devstr)
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200390{
391 char *st;
392
Stephen Warrendd648272014-06-11 16:03:33 -0600393 debug("%s: %s interface: %s dev: %s\n", __func__, s, interface, devstr);
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200394 st = strsep(&s, " ");
395 strcpy(dfu->name, st);
396
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200397 dfu->alt = alt;
Stephen Warren7ac1b412014-06-11 16:03:34 -0600398 dfu->max_buf_size = 0;
Stephen Warrencb7bd2e2014-06-11 16:03:35 -0600399 dfu->free_entity = NULL;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200400
401 /* Specific for mmc device */
402 if (strcmp(interface, "mmc") == 0) {
Stephen Warrendd648272014-06-11 16:03:33 -0600403 if (dfu_fill_entity_mmc(dfu, devstr, s))
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200404 return -1;
Pantelis Antoniouc6631762013-03-14 05:32:52 +0000405 } else if (strcmp(interface, "nand") == 0) {
Stephen Warrendd648272014-06-11 16:03:33 -0600406 if (dfu_fill_entity_nand(dfu, devstr, s))
Pantelis Antoniouc6631762013-03-14 05:32:52 +0000407 return -1;
Afzal Mohammeda9479f02013-09-18 01:15:24 +0530408 } else if (strcmp(interface, "ram") == 0) {
Stephen Warrendd648272014-06-11 16:03:33 -0600409 if (dfu_fill_entity_ram(dfu, devstr, s))
Afzal Mohammeda9479f02013-09-18 01:15:24 +0530410 return -1;
Stephen Warren6f12ebf2014-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 Majewskif22b11c2012-08-06 14:41:07 +0200414 } else {
415 printf("%s: Device %s not (yet) supported!\n",
416 __func__, interface);
417 return -1;
418 }
Stephen Warren806bd242015-09-04 22:03:46 -0600419 dfu_get_buf(dfu);
Lukasz Majewskif22b11c2012-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 Warren806bd242015-09-04 22:03:46 -0600428 dfu_free_buf();
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200429 list_for_each_entry_safe_reverse(dfu, p, &dfu_list, list) {
430 list_del(&dfu->list);
Stephen Warrencb7bd2e2014-06-11 16:03:35 -0600431 if (dfu->free_entity)
432 dfu->free_entity(dfu);
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200433 t = dfu;
434 }
435 if (t)
436 free(t);
437 INIT_LIST_HEAD(&dfu_list);
Przemyslaw Marczakb627eb42014-02-28 18:53:37 +0100438
439 alt_num_cnt = 0;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200440}
441
Stephen Warrendd648272014-06-11 16:03:33 -0600442int dfu_config_entities(char *env, char *interface, char *devstr)
Lukasz Majewskif22b11c2012-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 Majewskibd694242014-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 Yamada9b643e32017-09-16 14:10:41 +0900456 pr_err("Hash algorithm %s not supported\n", s);
Lukasz Majewskibd694242014-05-12 10:43:36 +0200457 }
458
Lukasz Majewskif22b11c2012-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 Warrendd648272014-06-11 16:03:33 -0600465 ret = dfu_fill_entity(&dfu[i], s, alt_num_cnt, interface,
466 devstr);
Peng Fan5d8fae72016-05-03 10:24:52 +0800467 if (ret) {
Sam Protsenkofeaa7852018-07-13 16:35:45 +0300468 /* We will free "dfu" in dfu_free_entities() */
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200469 return -1;
Peng Fan5d8fae72016-05-03 10:24:52 +0800470 }
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200471
472 list_add_tail(&dfu[i].list, &dfu_list);
Przemyslaw Marczakb627eb42014-02-28 18:53:37 +0100473 alt_num_cnt++;
Lukasz Majewskif22b11c2012-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 Delaunaydd93a8e2016-10-28 09:44:26 +0200481 const char *dev_t[] = {NULL, "eMMC", "OneNAND", "NAND", "RAM", "SF" };
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200482 return dev_t[t];
483}
484
485const char *dfu_get_layout(enum dfu_layout l)
486{
487 const char *dfu_layout[] = {NULL, "RAW_ADDR", "FAT", "EXT2",
Afzal Mohammeda9479f02013-09-18 01:15:24 +0530488 "EXT3", "EXT4", "RAM_ADDR" };
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200489 return dfu_layout[l];
490}
491
492void dfu_show_entities(void)
493{
494 struct dfu_entity *dfu;
495
496 puts("DFU alt settings list:\n");
497
498 list_for_each_entry(dfu, &dfu_list, list) {
499 printf("dev: %s alt: %d name: %s layout: %s\n",
500 dfu_get_dev_type(dfu->dev_type), dfu->alt,
501 dfu->name, dfu_get_layout(dfu->layout));
502 }
503}
504
505int dfu_get_alt_number(void)
506{
507 return dfu_alt_num;
508}
509
510struct dfu_entity *dfu_get_entity(int alt)
511{
512 struct dfu_entity *dfu;
513
514 list_for_each_entry(dfu, &dfu_list, list) {
515 if (dfu->alt == alt)
516 return dfu;
517 }
518
519 return NULL;
520}
Lukasz Majewskifed936e2013-10-08 14:30:38 +0200521
522int dfu_get_alt(char *name)
523{
524 struct dfu_entity *dfu;
Lukasz Majewski5610b052014-11-05 10:54:16 +0100525 char *str;
Lukasz Majewskifed936e2013-10-08 14:30:38 +0200526
527 list_for_each_entry(dfu, &dfu_list, list) {
Lukasz Majewski5610b052014-11-05 10:54:16 +0100528 if (dfu->name[0] != '/') {
529 if (!strncmp(dfu->name, name, strlen(dfu->name)))
530 return dfu->alt;
531 } else {
532 /*
533 * One must also consider absolute path
534 * (/boot/bin/uImage) available at dfu->name when
535 * compared "plain" file name (uImage)
536 *
537 * It is the case for e.g. thor gadget where lthor SW
538 * sends only the file name, so only the very last part
539 * of path must be checked for equality
540 */
541
542 str = strstr(dfu->name, name);
543 if (!str)
544 continue;
545
546 /*
547 * Check if matching substring is the last element of
548 * dfu->name (uImage)
549 */
550 if (strlen(dfu->name) ==
551 ((str - dfu->name) + strlen(name)))
552 return dfu->alt;
553 }
Lukasz Majewskifed936e2013-10-08 14:30:38 +0200554 }
555
556 return -ENODEV;
557}
Lukasz Majewski2092e462015-08-24 00:21:46 +0200558
559int dfu_write_from_mem_addr(struct dfu_entity *dfu, void *buf, int size)
560{
561 unsigned long dfu_buf_size, write, left = size;
562 int i, ret = 0;
563 void *dp = buf;
564
565 /*
566 * Here we must call dfu_get_buf(dfu) first to be sure that dfu_buf_size
567 * has been properly initialized - e.g. if "dfu_bufsiz" has been taken
568 * into account.
569 */
570 dfu_get_buf(dfu);
571 dfu_buf_size = dfu_get_buf_size();
572 debug("%s: dfu buf size: %lu\n", __func__, dfu_buf_size);
573
574 for (i = 0; left > 0; i++) {
575 write = min(dfu_buf_size, left);
576
577 debug("%s: dp: 0x%p left: %lu write: %lu\n", __func__,
578 dp, left, write);
579 ret = dfu_write(dfu, dp, write, i);
580 if (ret) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900581 pr_err("DFU write failed\n");
Lukasz Majewski2092e462015-08-24 00:21:46 +0200582 return ret;
583 }
584
585 dp += write;
586 left -= write;
587 }
588
589 ret = dfu_flush(dfu, NULL, 0, i);
590 if (ret)
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900591 pr_err("DFU flush failed!");
Lukasz Majewski2092e462015-08-24 00:21:46 +0200592
593 return ret;
594}