blob: 56e69fd231551a2d8320dbc1036e7a15c24c37fa [file] [log] [blame]
Lukasz Majewskif22b11c2012-08-06 14:41:07 +02001/*
2 * dfu.c -- DFU back-end routines
3 *
4 * Copyright (C) 2012 Samsung Electronics
5 * author: Lukasz Majewski <l.majewski@samsung.com>
6 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02007 * SPDX-License-Identifier: GPL-2.0+
Lukasz Majewskif22b11c2012-08-06 14:41:07 +02008 */
9
10#include <common.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>
16#include <linux/list.h>
17#include <linux/compiler.h>
18
Lukasz Majewski6bed7ce2013-07-18 13:19:14 +020019static bool dfu_reset_request;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +020020static LIST_HEAD(dfu_list);
21static int dfu_alt_num;
Przemyslaw Marczakb627eb42014-02-28 18:53:37 +010022static int alt_num_cnt;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +020023
Lukasz Majewski6bed7ce2013-07-18 13:19:14 +020024bool dfu_reset(void)
25{
26 return dfu_reset_request;
27}
28
29void dfu_trigger_reset()
30{
31 dfu_reset_request = true;
32}
33
Lukasz Majewskif22b11c2012-08-06 14:41:07 +020034static int dfu_find_alt_num(const char *s)
35{
36 int i = 0;
37
38 for (; *s; s++)
39 if (*s == ';')
40 i++;
41
42 return ++i;
43}
44
Lukasz Majewski765c5ae2013-09-11 14:53:35 +020045int dfu_init_env_entities(char *interface, int dev)
46{
47 const char *str_env;
48 char *env_bkp;
49 int ret;
50
51 str_env = getenv("dfu_alt_info");
52 if (!str_env) {
53 error("\"dfu_alt_info\" env variable not defined!\n");
54 return -EINVAL;
55 }
56
57 env_bkp = strdup(str_env);
58 ret = dfu_config_entities(env_bkp, interface, dev);
59 if (ret) {
60 error("DFU entities configuration failed!\n");
61 return ret;
62 }
63
64 free(env_bkp);
65 return 0;
66}
67
Heiko Schochere7e75c72013-06-12 06:05:51 +020068static unsigned char *dfu_buf;
69static unsigned long dfu_buf_size = CONFIG_SYS_DFU_DATA_BUF_SIZE;
70
Lukasz Majewskid4278262013-10-08 14:30:39 +020071unsigned char *dfu_free_buf(void)
Heiko Schochere7e75c72013-06-12 06:05:51 +020072{
73 free(dfu_buf);
74 dfu_buf = NULL;
75 return dfu_buf;
76}
77
Lukasz Majewski4fb12782013-12-09 16:20:13 +010078unsigned long dfu_get_buf_size(void)
79{
80 return dfu_buf_size;
81}
82
Lukasz Majewskid4278262013-10-08 14:30:39 +020083unsigned char *dfu_get_buf(void)
Heiko Schochere7e75c72013-06-12 06:05:51 +020084{
85 char *s;
86
87 if (dfu_buf != NULL)
88 return dfu_buf;
89
90 s = getenv("dfu_bufsiz");
91 dfu_buf_size = s ? (unsigned long)simple_strtol(s, NULL, 16) :
92 CONFIG_SYS_DFU_DATA_BUF_SIZE;
93
94 dfu_buf = memalign(CONFIG_SYS_CACHELINE_SIZE, dfu_buf_size);
95 if (dfu_buf == NULL)
96 printf("%s: Could not memalign 0x%lx bytes\n",
97 __func__, dfu_buf_size);
98
99 return dfu_buf;
100}
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200101
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000102static int dfu_write_buffer_drain(struct dfu_entity *dfu)
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200103{
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000104 long w_size;
105 int ret;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200106
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000107 /* flush size? */
108 w_size = dfu->i_buf - dfu->i_buf_start;
109 if (w_size == 0)
110 return 0;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200111
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000112 /* update CRC32 */
113 dfu->crc = crc32(dfu->crc, dfu->i_buf_start, w_size);
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200114
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000115 ret = dfu->write_medium(dfu, dfu->offset, dfu->i_buf_start, &w_size);
116 if (ret)
117 debug("%s: Write error!\n", __func__);
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200118
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000119 /* point back */
120 dfu->i_buf = dfu->i_buf_start;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200121
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000122 /* update offset */
123 dfu->offset += w_size;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200124
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000125 puts("#");
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200126
127 return ret;
128}
129
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000130int dfu_write(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200131{
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200132 int ret = 0;
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000133 int tret;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200134
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000135 debug("%s: name: %s buf: 0x%p size: 0x%x p_num: 0x%x offset: 0x%llx bufoffset: 0x%x\n",
136 __func__, dfu->name, buf, size, blk_seq_num, dfu->offset,
137 dfu->i_buf - dfu->i_buf_start);
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200138
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000139 if (!dfu->inited) {
140 /* initial state */
141 dfu->crc = 0;
142 dfu->offset = 0;
Pantelis Antoniouc6631762013-03-14 05:32:52 +0000143 dfu->bad_skip = 0;
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000144 dfu->i_blk_seq_num = 0;
Heiko Schochere7e75c72013-06-12 06:05:51 +0200145 dfu->i_buf_start = dfu_get_buf();
146 if (dfu->i_buf_start == NULL)
147 return -ENOMEM;
148 dfu->i_buf_end = dfu_get_buf() + dfu_buf_size;
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000149 dfu->i_buf = dfu->i_buf_start;
150
151 dfu->inited = 1;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200152 }
153
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000154 if (dfu->i_blk_seq_num != blk_seq_num) {
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200155 printf("%s: Wrong sequence number! [%d] [%d]\n",
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000156 __func__, dfu->i_blk_seq_num, blk_seq_num);
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200157 return -1;
158 }
159
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000160 /* DFU 1.1 standard says:
161 * The wBlockNum field is a block sequence number. It increments each
162 * time a block is transferred, wrapping to zero from 65,535. It is used
163 * to provide useful context to the DFU loader in the device."
164 *
165 * This means that it's a 16 bit counter that roll-overs at
166 * 0xffff -> 0x0000. By having a typical 4K transfer block
167 * we roll-over at exactly 256MB. Not very fun to debug.
168 *
169 * Handling rollover, and having an inited variable,
170 * makes things work.
171 */
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200172
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000173 /* handle rollover */
174 dfu->i_blk_seq_num = (dfu->i_blk_seq_num + 1) & 0xffff;
175
176 /* flush buffer if overflow */
177 if ((dfu->i_buf + size) > dfu->i_buf_end) {
178 tret = dfu_write_buffer_drain(dfu);
179 if (ret == 0)
180 ret = tret;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200181 }
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000182
183 /* we should be in buffer now (if not then size too large) */
184 if ((dfu->i_buf + size) > dfu->i_buf_end) {
Lukasz Majewskia7d2c3c2013-09-10 15:29:22 +0200185 error("Buffer overflow! (0x%p + 0x%x > 0x%p)\n", dfu->i_buf,
186 size, dfu->i_buf_end);
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000187 return -1;
188 }
189
190 memcpy(dfu->i_buf, buf, size);
191 dfu->i_buf += size;
192
193 /* if end or if buffer full flush */
194 if (size == 0 || (dfu->i_buf + size) > dfu->i_buf_end) {
195 tret = dfu_write_buffer_drain(dfu);
196 if (ret == 0)
197 ret = tret;
198 }
199
200 /* end? */
201 if (size == 0) {
202 /* Now try and flush to the medium if needed. */
203 if (dfu->flush_medium)
204 ret = dfu->flush_medium(dfu);
205 printf("\nDFU complete CRC32: 0x%08x\n", dfu->crc);
206
207 /* clear everything */
Heiko Schochere7e75c72013-06-12 06:05:51 +0200208 dfu_free_buf();
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000209 dfu->crc = 0;
210 dfu->offset = 0;
211 dfu->i_blk_seq_num = 0;
212 dfu->i_buf_start = dfu_buf;
Heiko Schochere7e75c72013-06-12 06:05:51 +0200213 dfu->i_buf_end = dfu_buf;
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000214 dfu->i_buf = dfu->i_buf_start;
215
216 dfu->inited = 0;
217
218 }
219
220 return ret = 0 ? size : ret;
221}
222
223static int dfu_read_buffer_fill(struct dfu_entity *dfu, void *buf, int size)
224{
225 long chunk;
226 int ret, readn;
227
228 readn = 0;
229 while (size > 0) {
230 /* get chunk that can be read */
231 chunk = min(size, dfu->b_left);
232 /* consume */
233 if (chunk > 0) {
234 memcpy(buf, dfu->i_buf, chunk);
235 dfu->crc = crc32(dfu->crc, buf, chunk);
236 dfu->i_buf += chunk;
237 dfu->b_left -= chunk;
Bo Shenc2617bc2013-10-16 17:26:37 +0800238 dfu->r_left -= chunk;
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000239 size -= chunk;
240 buf += chunk;
241 readn += chunk;
242 }
243
244 /* all done */
245 if (size > 0) {
246 /* no more to read */
247 if (dfu->r_left == 0)
248 break;
249
250 dfu->i_buf = dfu->i_buf_start;
251 dfu->b_left = dfu->i_buf_end - dfu->i_buf_start;
252
253 /* got to read, but buffer is empty */
254 if (dfu->b_left > dfu->r_left)
255 dfu->b_left = dfu->r_left;
256 ret = dfu->read_medium(dfu, dfu->offset, dfu->i_buf,
257 &dfu->b_left);
258 if (ret != 0) {
259 debug("%s: Read error!\n", __func__);
260 return ret;
261 }
262 dfu->offset += dfu->b_left;
263 dfu->r_left -= dfu->b_left;
264
265 puts("#");
266 }
267 }
268
269 return readn;
270}
271
272int dfu_read(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
273{
274 int ret = 0;
275
276 debug("%s: name: %s buf: 0x%p size: 0x%x p_num: 0x%x i_buf: 0x%p\n",
277 __func__, dfu->name, buf, size, blk_seq_num, dfu->i_buf);
278
279 if (!dfu->inited) {
Łukasz Majewski9df49552013-06-28 18:41:50 +0200280 dfu->i_buf_start = dfu_get_buf();
281 if (dfu->i_buf_start == NULL)
282 return -ENOMEM;
283
284 ret = dfu->read_medium(dfu, 0, dfu->i_buf_start, &dfu->r_left);
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000285 if (ret != 0) {
286 debug("%s: failed to get r_left\n", __func__);
287 return ret;
288 }
289
290 debug("%s: %s %ld [B]\n", __func__, dfu->name, dfu->r_left);
291
292 dfu->i_blk_seq_num = 0;
293 dfu->crc = 0;
294 dfu->offset = 0;
Heiko Schochere7e75c72013-06-12 06:05:51 +0200295 dfu->i_buf_end = dfu_get_buf() + dfu_buf_size;
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000296 dfu->i_buf = dfu->i_buf_start;
Bo Shen31bae4c2013-10-16 17:26:38 +0800297 dfu->b_left = min(dfu_buf_size, dfu->r_left);
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000298
Pantelis Antoniouc6631762013-03-14 05:32:52 +0000299 dfu->bad_skip = 0;
300
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000301 dfu->inited = 1;
302 }
303
304 if (dfu->i_blk_seq_num != blk_seq_num) {
305 printf("%s: Wrong sequence number! [%d] [%d]\n",
306 __func__, dfu->i_blk_seq_num, blk_seq_num);
307 return -1;
308 }
309 /* handle rollover */
310 dfu->i_blk_seq_num = (dfu->i_blk_seq_num + 1) & 0xffff;
311
312 ret = dfu_read_buffer_fill(dfu, buf, size);
313 if (ret < 0) {
314 printf("%s: Failed to fill buffer\n", __func__);
315 return -1;
316 }
317
318 if (ret < size) {
319 debug("%s: %s CRC32: 0x%x\n", __func__, dfu->name, dfu->crc);
320 puts("\nUPLOAD ... done\nCtrl+C to exit ...\n");
321
Heiko Schochere7e75c72013-06-12 06:05:51 +0200322 dfu_free_buf();
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000323 dfu->i_blk_seq_num = 0;
324 dfu->crc = 0;
325 dfu->offset = 0;
326 dfu->i_buf_start = dfu_buf;
Heiko Schochere7e75c72013-06-12 06:05:51 +0200327 dfu->i_buf_end = dfu_buf;
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000328 dfu->i_buf = dfu->i_buf_start;
329 dfu->b_left = 0;
330
Pantelis Antoniouc6631762013-03-14 05:32:52 +0000331 dfu->bad_skip = 0;
332
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000333 dfu->inited = 0;
334 }
335
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200336 return ret;
337}
338
339static int dfu_fill_entity(struct dfu_entity *dfu, char *s, int alt,
Mateusz Zalega16297cf2013-10-04 19:22:26 +0200340 char *interface, int num)
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200341{
342 char *st;
343
344 debug("%s: %s interface: %s num: %d\n", __func__, s, interface, num);
345 st = strsep(&s, " ");
346 strcpy(dfu->name, st);
347
348 dfu->dev_num = num;
349 dfu->alt = alt;
350
351 /* Specific for mmc device */
352 if (strcmp(interface, "mmc") == 0) {
353 if (dfu_fill_entity_mmc(dfu, s))
354 return -1;
Pantelis Antoniouc6631762013-03-14 05:32:52 +0000355 } else if (strcmp(interface, "nand") == 0) {
356 if (dfu_fill_entity_nand(dfu, s))
357 return -1;
Afzal Mohammeda9479f02013-09-18 01:15:24 +0530358 } else if (strcmp(interface, "ram") == 0) {
359 if (dfu_fill_entity_ram(dfu, s))
360 return -1;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200361 } else {
362 printf("%s: Device %s not (yet) supported!\n",
363 __func__, interface);
364 return -1;
365 }
366
367 return 0;
368}
369
370void dfu_free_entities(void)
371{
372 struct dfu_entity *dfu, *p, *t = NULL;
373
374 list_for_each_entry_safe_reverse(dfu, p, &dfu_list, list) {
375 list_del(&dfu->list);
376 t = dfu;
377 }
378 if (t)
379 free(t);
380 INIT_LIST_HEAD(&dfu_list);
Przemyslaw Marczakb627eb42014-02-28 18:53:37 +0100381
382 alt_num_cnt = 0;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200383}
384
385int dfu_config_entities(char *env, char *interface, int num)
386{
387 struct dfu_entity *dfu;
388 int i, ret;
389 char *s;
390
391 dfu_alt_num = dfu_find_alt_num(env);
392 debug("%s: dfu_alt_num=%d\n", __func__, dfu_alt_num);
393
394 dfu = calloc(sizeof(*dfu), dfu_alt_num);
395 if (!dfu)
396 return -1;
397 for (i = 0; i < dfu_alt_num; i++) {
398
399 s = strsep(&env, ";");
Przemyslaw Marczakb627eb42014-02-28 18:53:37 +0100400 ret = dfu_fill_entity(&dfu[i], s, alt_num_cnt, interface, num);
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200401 if (ret)
402 return -1;
403
404 list_add_tail(&dfu[i].list, &dfu_list);
Przemyslaw Marczakb627eb42014-02-28 18:53:37 +0100405 alt_num_cnt++;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200406 }
407
408 return 0;
409}
410
411const char *dfu_get_dev_type(enum dfu_device_type t)
412{
Afzal Mohammeda9479f02013-09-18 01:15:24 +0530413 const char *dev_t[] = {NULL, "eMMC", "OneNAND", "NAND", "RAM" };
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200414 return dev_t[t];
415}
416
417const char *dfu_get_layout(enum dfu_layout l)
418{
419 const char *dfu_layout[] = {NULL, "RAW_ADDR", "FAT", "EXT2",
Afzal Mohammeda9479f02013-09-18 01:15:24 +0530420 "EXT3", "EXT4", "RAM_ADDR" };
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200421 return dfu_layout[l];
422}
423
424void dfu_show_entities(void)
425{
426 struct dfu_entity *dfu;
427
428 puts("DFU alt settings list:\n");
429
430 list_for_each_entry(dfu, &dfu_list, list) {
431 printf("dev: %s alt: %d name: %s layout: %s\n",
432 dfu_get_dev_type(dfu->dev_type), dfu->alt,
433 dfu->name, dfu_get_layout(dfu->layout));
434 }
435}
436
437int dfu_get_alt_number(void)
438{
439 return dfu_alt_num;
440}
441
442struct dfu_entity *dfu_get_entity(int alt)
443{
444 struct dfu_entity *dfu;
445
446 list_for_each_entry(dfu, &dfu_list, list) {
447 if (dfu->alt == alt)
448 return dfu;
449 }
450
451 return NULL;
452}
Lukasz Majewskifed936e2013-10-08 14:30:38 +0200453
454int dfu_get_alt(char *name)
455{
456 struct dfu_entity *dfu;
457
458 list_for_each_entry(dfu, &dfu_list, list) {
459 if (!strncmp(dfu->name, name, strlen(dfu->name)))
460 return dfu->alt;
461 }
462
463 return -ENODEV;
464}